prio_tree.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. /*
  2. * lib/prio_tree.c - priority search tree
  3. *
  4. * Copyright (C) 2004, Rajesh Venkatasubramanian <vrajesh@umich.edu>
  5. *
  6. * This file is released under the GPL v2.
  7. *
  8. * Based on the radix priority search tree proposed by Edward M. McCreight
  9. * SIAM Journal of Computing, vol. 14, no.2, pages 257-276, May 1985
  10. *
  11. * 02Feb2004 Initial version
  12. */
  13. #include <linux/init.h>
  14. #include <linux/mm.h>
  15. #include <linux/prio_tree.h>
  16. #include <linux/export.h>
  17. /*
  18. * A clever mix of heap and radix trees forms a radix priority search tree (PST)
  19. * which is useful for storing intervals, e.g, we can consider a vma as a closed
  20. * interval of file pages [offset_begin, offset_end], and store all vmas that
  21. * map a file in a PST. Then, using the PST, we can answer a stabbing query,
  22. * i.e., selecting a set of stored intervals (vmas) that overlap with (map) a
  23. * given input interval X (a set of consecutive file pages), in "O(log n + m)"
  24. * time where 'log n' is the height of the PST, and 'm' is the number of stored
  25. * intervals (vmas) that overlap (map) with the input interval X (the set of
  26. * consecutive file pages).
  27. *
  28. * In our implementation, we store closed intervals of the form [radix_index,
  29. * heap_index]. We assume that always radix_index <= heap_index. McCreight's PST
  30. * is designed for storing intervals with unique radix indices, i.e., each
  31. * interval have different radix_index. However, this limitation can be easily
  32. * overcome by using the size, i.e., heap_index - radix_index, as part of the
  33. * index, so we index the tree using [(radix_index,size), heap_index].
  34. *
  35. * When the above-mentioned indexing scheme is used, theoretically, in a 32 bit
  36. * machine, the maximum height of a PST can be 64. We can use a balanced version
  37. * of the priority search tree to optimize the tree height, but the balanced
  38. * tree proposed by McCreight is too complex and memory-hungry for our purpose.
  39. */
  40. /*
  41. * The following macros are used for implementing prio_tree for i_mmap
  42. */
  43. static void get_index(const struct prio_tree_root *root,
  44. const struct prio_tree_node *node,
  45. unsigned long *radix, unsigned long *heap)
  46. {
  47. *radix = node->start;
  48. *heap = node->last;
  49. }
  50. static unsigned long index_bits_to_maxindex[BITS_PER_LONG];
  51. void __init prio_tree_init(void)
  52. {
  53. unsigned int i;
  54. for (i = 0; i < ARRAY_SIZE(index_bits_to_maxindex) - 1; i++)
  55. index_bits_to_maxindex[i] = (1UL << (i + 1)) - 1;
  56. index_bits_to_maxindex[ARRAY_SIZE(index_bits_to_maxindex) - 1] = ~0UL;
  57. }
  58. /*
  59. * Maximum heap_index that can be stored in a PST with index_bits bits
  60. */
  61. static inline unsigned long prio_tree_maxindex(unsigned int bits)
  62. {
  63. return index_bits_to_maxindex[bits - 1];
  64. }
  65. static void prio_set_parent(struct prio_tree_node *parent,
  66. struct prio_tree_node *child, bool left)
  67. {
  68. if (left)
  69. parent->left = child;
  70. else
  71. parent->right = child;
  72. child->parent = parent;
  73. }
  74. /*
  75. * Extend a priority search tree so that it can store a node with heap_index
  76. * max_heap_index. In the worst case, this algorithm takes O((log n)^2).
  77. * However, this function is used rarely and the common case performance is
  78. * not bad.
  79. */
  80. static struct prio_tree_node *prio_tree_expand(struct prio_tree_root *root,
  81. struct prio_tree_node *node, unsigned long max_heap_index)
  82. {
  83. struct prio_tree_node *prev;
  84. if (max_heap_index > prio_tree_maxindex(root->index_bits))
  85. root->index_bits++;
  86. prev = node;
  87. INIT_PRIO_TREE_NODE(node);
  88. while (max_heap_index > prio_tree_maxindex(root->index_bits)) {
  89. struct prio_tree_node *tmp = root->prio_tree_node;
  90. root->index_bits++;
  91. if (prio_tree_empty(root))
  92. continue;
  93. prio_tree_remove(root, root->prio_tree_node);
  94. INIT_PRIO_TREE_NODE(tmp);
  95. prio_set_parent(prev, tmp, true);
  96. prev = tmp;
  97. }
  98. if (!prio_tree_empty(root))
  99. prio_set_parent(prev, root->prio_tree_node, true);
  100. root->prio_tree_node = node;
  101. return node;
  102. }
  103. /*
  104. * Replace a prio_tree_node with a new node and return the old node
  105. */
  106. struct prio_tree_node *prio_tree_replace(struct prio_tree_root *root,
  107. struct prio_tree_node *old, struct prio_tree_node *node)
  108. {
  109. INIT_PRIO_TREE_NODE(node);
  110. if (prio_tree_root(old)) {
  111. BUG_ON(root->prio_tree_node != old);
  112. /*
  113. * We can reduce root->index_bits here. However, it is complex
  114. * and does not help much to improve performance (IMO).
  115. */
  116. root->prio_tree_node = node;
  117. } else
  118. prio_set_parent(old->parent, node, old->parent->left == old);
  119. if (!prio_tree_left_empty(old))
  120. prio_set_parent(node, old->left, true);
  121. if (!prio_tree_right_empty(old))
  122. prio_set_parent(node, old->right, false);
  123. return old;
  124. }
  125. /*
  126. * Insert a prio_tree_node @node into a radix priority search tree @root. The
  127. * algorithm typically takes O(log n) time where 'log n' is the number of bits
  128. * required to represent the maximum heap_index. In the worst case, the algo
  129. * can take O((log n)^2) - check prio_tree_expand.
  130. *
  131. * If a prior node with same radix_index and heap_index is already found in
  132. * the tree, then returns the address of the prior node. Otherwise, inserts
  133. * @node into the tree and returns @node.
  134. */
  135. struct prio_tree_node *prio_tree_insert(struct prio_tree_root *root,
  136. struct prio_tree_node *node)
  137. {
  138. struct prio_tree_node *cur, *res = node;
  139. unsigned long radix_index, heap_index;
  140. unsigned long r_index, h_index, index, mask;
  141. int size_flag = 0;
  142. get_index(root, node, &radix_index, &heap_index);
  143. if (prio_tree_empty(root) ||
  144. heap_index > prio_tree_maxindex(root->index_bits))
  145. return prio_tree_expand(root, node, heap_index);
  146. cur = root->prio_tree_node;
  147. mask = 1UL << (root->index_bits - 1);
  148. while (mask) {
  149. get_index(root, cur, &r_index, &h_index);
  150. if (r_index == radix_index && h_index == heap_index)
  151. return cur;
  152. if (h_index < heap_index ||
  153. (h_index == heap_index && r_index > radix_index)) {
  154. struct prio_tree_node *tmp = node;
  155. node = prio_tree_replace(root, cur, node);
  156. cur = tmp;
  157. /* swap indices */
  158. index = r_index;
  159. r_index = radix_index;
  160. radix_index = index;
  161. index = h_index;
  162. h_index = heap_index;
  163. heap_index = index;
  164. }
  165. if (size_flag)
  166. index = heap_index - radix_index;
  167. else
  168. index = radix_index;
  169. if (index & mask) {
  170. if (prio_tree_right_empty(cur)) {
  171. INIT_PRIO_TREE_NODE(node);
  172. prio_set_parent(cur, node, false);
  173. return res;
  174. } else
  175. cur = cur->right;
  176. } else {
  177. if (prio_tree_left_empty(cur)) {
  178. INIT_PRIO_TREE_NODE(node);
  179. prio_set_parent(cur, node, true);
  180. return res;
  181. } else
  182. cur = cur->left;
  183. }
  184. mask >>= 1;
  185. if (!mask) {
  186. mask = 1UL << (BITS_PER_LONG - 1);
  187. size_flag = 1;
  188. }
  189. }
  190. /* Should not reach here */
  191. BUG();
  192. return NULL;
  193. }
  194. EXPORT_SYMBOL(prio_tree_insert);
  195. /*
  196. * Remove a prio_tree_node @node from a radix priority search tree @root. The
  197. * algorithm takes O(log n) time where 'log n' is the number of bits required
  198. * to represent the maximum heap_index.
  199. */
  200. void prio_tree_remove(struct prio_tree_root *root, struct prio_tree_node *node)
  201. {
  202. struct prio_tree_node *cur;
  203. unsigned long r_index, h_index_right, h_index_left;
  204. cur = node;
  205. while (!prio_tree_left_empty(cur) || !prio_tree_right_empty(cur)) {
  206. if (!prio_tree_left_empty(cur))
  207. get_index(root, cur->left, &r_index, &h_index_left);
  208. else {
  209. cur = cur->right;
  210. continue;
  211. }
  212. if (!prio_tree_right_empty(cur))
  213. get_index(root, cur->right, &r_index, &h_index_right);
  214. else {
  215. cur = cur->left;
  216. continue;
  217. }
  218. /* both h_index_left and h_index_right cannot be 0 */
  219. if (h_index_left >= h_index_right)
  220. cur = cur->left;
  221. else
  222. cur = cur->right;
  223. }
  224. if (prio_tree_root(cur)) {
  225. BUG_ON(root->prio_tree_node != cur);
  226. __INIT_PRIO_TREE_ROOT(root, root->raw);
  227. return;
  228. }
  229. if (cur->parent->right == cur)
  230. cur->parent->right = cur->parent;
  231. else
  232. cur->parent->left = cur->parent;
  233. while (cur != node)
  234. cur = prio_tree_replace(root, cur->parent, cur);
  235. }
  236. EXPORT_SYMBOL(prio_tree_remove);
  237. static void iter_walk_down(struct prio_tree_iter *iter)
  238. {
  239. iter->mask >>= 1;
  240. if (iter->mask) {
  241. if (iter->size_level)
  242. iter->size_level++;
  243. return;
  244. }
  245. if (iter->size_level) {
  246. BUG_ON(!prio_tree_left_empty(iter->cur));
  247. BUG_ON(!prio_tree_right_empty(iter->cur));
  248. iter->size_level++;
  249. iter->mask = ULONG_MAX;
  250. } else {
  251. iter->size_level = 1;
  252. iter->mask = 1UL << (BITS_PER_LONG - 1);
  253. }
  254. }
  255. static void iter_walk_up(struct prio_tree_iter *iter)
  256. {
  257. if (iter->mask == ULONG_MAX)
  258. iter->mask = 1UL;
  259. else if (iter->size_level == 1)
  260. iter->mask = 1UL;
  261. else
  262. iter->mask <<= 1;
  263. if (iter->size_level)
  264. iter->size_level--;
  265. if (!iter->size_level && (iter->value & iter->mask))
  266. iter->value ^= iter->mask;
  267. }
  268. /*
  269. * Following functions help to enumerate all prio_tree_nodes in the tree that
  270. * overlap with the input interval X [radix_index, heap_index]. The enumeration
  271. * takes O(log n + m) time where 'log n' is the height of the tree (which is
  272. * proportional to # of bits required to represent the maximum heap_index) and
  273. * 'm' is the number of prio_tree_nodes that overlap the interval X.
  274. */
  275. static struct prio_tree_node *prio_tree_left(struct prio_tree_iter *iter,
  276. unsigned long *r_index, unsigned long *h_index)
  277. {
  278. if (prio_tree_left_empty(iter->cur))
  279. return NULL;
  280. get_index(iter->root, iter->cur->left, r_index, h_index);
  281. if (iter->r_index <= *h_index) {
  282. iter->cur = iter->cur->left;
  283. iter_walk_down(iter);
  284. return iter->cur;
  285. }
  286. return NULL;
  287. }
  288. static struct prio_tree_node *prio_tree_right(struct prio_tree_iter *iter,
  289. unsigned long *r_index, unsigned long *h_index)
  290. {
  291. unsigned long value;
  292. if (prio_tree_right_empty(iter->cur))
  293. return NULL;
  294. if (iter->size_level)
  295. value = iter->value;
  296. else
  297. value = iter->value | iter->mask;
  298. if (iter->h_index < value)
  299. return NULL;
  300. get_index(iter->root, iter->cur->right, r_index, h_index);
  301. if (iter->r_index <= *h_index) {
  302. iter->cur = iter->cur->right;
  303. iter_walk_down(iter);
  304. return iter->cur;
  305. }
  306. return NULL;
  307. }
  308. static struct prio_tree_node *prio_tree_parent(struct prio_tree_iter *iter)
  309. {
  310. iter->cur = iter->cur->parent;
  311. iter_walk_up(iter);
  312. return iter->cur;
  313. }
  314. static inline int overlap(struct prio_tree_iter *iter,
  315. unsigned long r_index, unsigned long h_index)
  316. {
  317. return iter->h_index >= r_index && iter->r_index <= h_index;
  318. }
  319. /*
  320. * prio_tree_first:
  321. *
  322. * Get the first prio_tree_node that overlaps with the interval [radix_index,
  323. * heap_index]. Note that always radix_index <= heap_index. We do a pre-order
  324. * traversal of the tree.
  325. */
  326. static struct prio_tree_node *prio_tree_first(struct prio_tree_iter *iter)
  327. {
  328. struct prio_tree_root *root;
  329. unsigned long r_index, h_index;
  330. INIT_PRIO_TREE_ITER(iter);
  331. root = iter->root;
  332. if (prio_tree_empty(root))
  333. return NULL;
  334. get_index(root, root->prio_tree_node, &r_index, &h_index);
  335. if (iter->r_index > h_index)
  336. return NULL;
  337. iter->mask = 1UL << (root->index_bits - 1);
  338. iter->cur = root->prio_tree_node;
  339. while (1) {
  340. if (overlap(iter, r_index, h_index))
  341. return iter->cur;
  342. if (prio_tree_left(iter, &r_index, &h_index))
  343. continue;
  344. if (prio_tree_right(iter, &r_index, &h_index))
  345. continue;
  346. break;
  347. }
  348. return NULL;
  349. }
  350. /*
  351. * prio_tree_next:
  352. *
  353. * Get the next prio_tree_node that overlaps with the input interval in iter
  354. */
  355. struct prio_tree_node *prio_tree_next(struct prio_tree_iter *iter)
  356. {
  357. unsigned long r_index, h_index;
  358. if (iter->cur == NULL)
  359. return prio_tree_first(iter);
  360. repeat:
  361. while (prio_tree_left(iter, &r_index, &h_index))
  362. if (overlap(iter, r_index, h_index))
  363. return iter->cur;
  364. while (!prio_tree_right(iter, &r_index, &h_index)) {
  365. while (!prio_tree_root(iter->cur) &&
  366. iter->cur->parent->right == iter->cur)
  367. prio_tree_parent(iter);
  368. if (prio_tree_root(iter->cur))
  369. return NULL;
  370. prio_tree_parent(iter);
  371. }
  372. if (overlap(iter, r_index, h_index))
  373. return iter->cur;
  374. goto repeat;
  375. }
  376. EXPORT_SYMBOL(prio_tree_next);