usnic_uiom_interval_tree.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. * Copyright (c) 2014, Cisco Systems, Inc. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. *
  32. */
  33. #include <linux/init.h>
  34. #include <linux/list.h>
  35. #include <linux/slab.h>
  36. #include <linux/list_sort.h>
  37. #include <linux/interval_tree_generic.h>
  38. #include "usnic_uiom_interval_tree.h"
  39. #define START(node) ((node)->start)
  40. #define LAST(node) ((node)->last)
  41. #define MAKE_NODE(node, start, end, ref_cnt, flags, err, err_out) \
  42. do { \
  43. node = usnic_uiom_interval_node_alloc(start, \
  44. end, ref_cnt, flags); \
  45. if (!node) { \
  46. err = -ENOMEM; \
  47. goto err_out; \
  48. } \
  49. } while (0)
  50. #define MARK_FOR_ADD(node, list) (list_add_tail(&node->link, list))
  51. #define MAKE_NODE_AND_APPEND(node, start, end, ref_cnt, flags, err, \
  52. err_out, list) \
  53. do { \
  54. MAKE_NODE(node, start, end, \
  55. ref_cnt, flags, err, \
  56. err_out); \
  57. MARK_FOR_ADD(node, list); \
  58. } while (0)
  59. #define FLAGS_EQUAL(flags1, flags2, mask) \
  60. (((flags1) & (mask)) == ((flags2) & (mask)))
  61. static struct usnic_uiom_interval_node*
  62. usnic_uiom_interval_node_alloc(long int start, long int last, int ref_cnt,
  63. int flags)
  64. {
  65. struct usnic_uiom_interval_node *interval = kzalloc(sizeof(*interval),
  66. GFP_ATOMIC);
  67. if (!interval)
  68. return NULL;
  69. interval->start = start;
  70. interval->last = last;
  71. interval->flags = flags;
  72. interval->ref_cnt = ref_cnt;
  73. return interval;
  74. }
  75. static int interval_cmp(void *priv, struct list_head *a, struct list_head *b)
  76. {
  77. struct usnic_uiom_interval_node *node_a, *node_b;
  78. node_a = list_entry(a, struct usnic_uiom_interval_node, link);
  79. node_b = list_entry(b, struct usnic_uiom_interval_node, link);
  80. /* long to int */
  81. if (node_a->start < node_b->start)
  82. return -1;
  83. else if (node_a->start > node_b->start)
  84. return 1;
  85. return 0;
  86. }
  87. static void
  88. find_intervals_intersection_sorted(struct rb_root_cached *root,
  89. unsigned long start, unsigned long last,
  90. struct list_head *list)
  91. {
  92. struct usnic_uiom_interval_node *node;
  93. INIT_LIST_HEAD(list);
  94. for (node = usnic_uiom_interval_tree_iter_first(root, start, last);
  95. node;
  96. node = usnic_uiom_interval_tree_iter_next(node, start, last))
  97. list_add_tail(&node->link, list);
  98. list_sort(NULL, list, interval_cmp);
  99. }
  100. int usnic_uiom_get_intervals_diff(unsigned long start, unsigned long last,
  101. int flags, int flag_mask,
  102. struct rb_root_cached *root,
  103. struct list_head *diff_set)
  104. {
  105. struct usnic_uiom_interval_node *interval, *tmp;
  106. int err = 0;
  107. long int pivot = start;
  108. LIST_HEAD(intersection_set);
  109. INIT_LIST_HEAD(diff_set);
  110. find_intervals_intersection_sorted(root, start, last,
  111. &intersection_set);
  112. list_for_each_entry(interval, &intersection_set, link) {
  113. if (pivot < interval->start) {
  114. MAKE_NODE_AND_APPEND(tmp, pivot, interval->start - 1,
  115. 1, flags, err, err_out,
  116. diff_set);
  117. pivot = interval->start;
  118. }
  119. /*
  120. * Invariant: Set [start, pivot] is either in diff_set or root,
  121. * but not in both.
  122. */
  123. if (pivot > interval->last) {
  124. continue;
  125. } else if (pivot <= interval->last &&
  126. FLAGS_EQUAL(interval->flags, flags,
  127. flag_mask)) {
  128. pivot = interval->last + 1;
  129. }
  130. }
  131. if (pivot <= last)
  132. MAKE_NODE_AND_APPEND(tmp, pivot, last, 1, flags, err, err_out,
  133. diff_set);
  134. return 0;
  135. err_out:
  136. list_for_each_entry_safe(interval, tmp, diff_set, link) {
  137. list_del(&interval->link);
  138. kfree(interval);
  139. }
  140. return err;
  141. }
  142. void usnic_uiom_put_interval_set(struct list_head *intervals)
  143. {
  144. struct usnic_uiom_interval_node *interval, *tmp;
  145. list_for_each_entry_safe(interval, tmp, intervals, link)
  146. kfree(interval);
  147. }
  148. int usnic_uiom_insert_interval(struct rb_root_cached *root, unsigned long start,
  149. unsigned long last, int flags)
  150. {
  151. struct usnic_uiom_interval_node *interval, *tmp;
  152. unsigned long istart, ilast;
  153. int iref_cnt, iflags;
  154. unsigned long lpivot = start;
  155. int err = 0;
  156. LIST_HEAD(to_add);
  157. LIST_HEAD(intersection_set);
  158. find_intervals_intersection_sorted(root, start, last,
  159. &intersection_set);
  160. list_for_each_entry(interval, &intersection_set, link) {
  161. /*
  162. * Invariant - lpivot is the left edge of next interval to be
  163. * inserted
  164. */
  165. istart = interval->start;
  166. ilast = interval->last;
  167. iref_cnt = interval->ref_cnt;
  168. iflags = interval->flags;
  169. if (istart < lpivot) {
  170. MAKE_NODE_AND_APPEND(tmp, istart, lpivot - 1, iref_cnt,
  171. iflags, err, err_out, &to_add);
  172. } else if (istart > lpivot) {
  173. MAKE_NODE_AND_APPEND(tmp, lpivot, istart - 1, 1, flags,
  174. err, err_out, &to_add);
  175. lpivot = istart;
  176. } else {
  177. lpivot = istart;
  178. }
  179. if (ilast > last) {
  180. MAKE_NODE_AND_APPEND(tmp, lpivot, last, iref_cnt + 1,
  181. iflags | flags, err, err_out,
  182. &to_add);
  183. MAKE_NODE_AND_APPEND(tmp, last + 1, ilast, iref_cnt,
  184. iflags, err, err_out, &to_add);
  185. } else {
  186. MAKE_NODE_AND_APPEND(tmp, lpivot, ilast, iref_cnt + 1,
  187. iflags | flags, err, err_out,
  188. &to_add);
  189. }
  190. lpivot = ilast + 1;
  191. }
  192. if (lpivot <= last)
  193. MAKE_NODE_AND_APPEND(tmp, lpivot, last, 1, flags, err, err_out,
  194. &to_add);
  195. list_for_each_entry_safe(interval, tmp, &intersection_set, link) {
  196. usnic_uiom_interval_tree_remove(interval, root);
  197. kfree(interval);
  198. }
  199. list_for_each_entry(interval, &to_add, link)
  200. usnic_uiom_interval_tree_insert(interval, root);
  201. return 0;
  202. err_out:
  203. list_for_each_entry_safe(interval, tmp, &to_add, link)
  204. kfree(interval);
  205. return err;
  206. }
  207. void usnic_uiom_remove_interval(struct rb_root_cached *root,
  208. unsigned long start, unsigned long last,
  209. struct list_head *removed)
  210. {
  211. struct usnic_uiom_interval_node *interval;
  212. for (interval = usnic_uiom_interval_tree_iter_first(root, start, last);
  213. interval;
  214. interval = usnic_uiom_interval_tree_iter_next(interval,
  215. start,
  216. last)) {
  217. if (--interval->ref_cnt == 0)
  218. list_add_tail(&interval->link, removed);
  219. }
  220. list_for_each_entry(interval, removed, link)
  221. usnic_uiom_interval_tree_remove(interval, root);
  222. }
  223. INTERVAL_TREE_DEFINE(struct usnic_uiom_interval_node, rb,
  224. unsigned long, __subtree_last,
  225. START, LAST, , usnic_uiom_interval_tree)