prio_tree.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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. #define RADIX_INDEX(vma) ((vma)->vm_pgoff)
  44. #define VMA_SIZE(vma) (((vma)->vm_end - (vma)->vm_start) >> PAGE_SHIFT)
  45. /* avoid overflow */
  46. #define HEAP_INDEX(vma) ((vma)->vm_pgoff + (VMA_SIZE(vma) - 1))
  47. static void get_index(const struct prio_tree_root *root,
  48. const struct prio_tree_node *node,
  49. unsigned long *radix, unsigned long *heap)
  50. {
  51. if (root->raw) {
  52. struct vm_area_struct *vma = prio_tree_entry(
  53. node, struct vm_area_struct, shared.prio_tree_node);
  54. *radix = RADIX_INDEX(vma);
  55. *heap = HEAP_INDEX(vma);
  56. }
  57. else {
  58. *radix = node->start;
  59. *heap = node->last;
  60. }
  61. }
  62. static unsigned long index_bits_to_maxindex[BITS_PER_LONG];
  63. void __init prio_tree_init(void)
  64. {
  65. unsigned int i;
  66. for (i = 0; i < ARRAY_SIZE(index_bits_to_maxindex) - 1; i++)
  67. index_bits_to_maxindex[i] = (1UL << (i + 1)) - 1;
  68. index_bits_to_maxindex[ARRAY_SIZE(index_bits_to_maxindex) - 1] = ~0UL;
  69. }
  70. /*
  71. * Maximum heap_index that can be stored in a PST with index_bits bits
  72. */
  73. static inline unsigned long prio_tree_maxindex(unsigned int bits)
  74. {
  75. return index_bits_to_maxindex[bits - 1];
  76. }
  77. static void prio_set_parent(struct prio_tree_node *parent,
  78. struct prio_tree_node *child, bool left)
  79. {
  80. if (left)
  81. parent->left = child;
  82. else
  83. parent->right = child;
  84. child->parent = parent;
  85. }
  86. /*
  87. * Extend a priority search tree so that it can store a node with heap_index
  88. * max_heap_index. In the worst case, this algorithm takes O((log n)^2).
  89. * However, this function is used rarely and the common case performance is
  90. * not bad.
  91. */
  92. static struct prio_tree_node *prio_tree_expand(struct prio_tree_root *root,
  93. struct prio_tree_node *node, unsigned long max_heap_index)
  94. {
  95. struct prio_tree_node *prev;
  96. if (max_heap_index > prio_tree_maxindex(root->index_bits))
  97. root->index_bits++;
  98. prev = node;
  99. INIT_PRIO_TREE_NODE(node);
  100. while (max_heap_index > prio_tree_maxindex(root->index_bits)) {
  101. struct prio_tree_node *tmp = root->prio_tree_node;
  102. root->index_bits++;
  103. if (prio_tree_empty(root))
  104. continue;
  105. prio_tree_remove(root, root->prio_tree_node);
  106. INIT_PRIO_TREE_NODE(tmp);
  107. prio_set_parent(prev, tmp, true);
  108. prev = tmp;
  109. }
  110. if (!prio_tree_empty(root))
  111. prio_set_parent(prev, root->prio_tree_node, true);
  112. root->prio_tree_node = node;
  113. return node;
  114. }
  115. /*
  116. * Replace a prio_tree_node with a new node and return the old node
  117. */
  118. struct prio_tree_node *prio_tree_replace(struct prio_tree_root *root,
  119. struct prio_tree_node *old, struct prio_tree_node *node)
  120. {
  121. INIT_PRIO_TREE_NODE(node);
  122. if (prio_tree_root(old)) {
  123. BUG_ON(root->prio_tree_node != old);
  124. /*
  125. * We can reduce root->index_bits here. However, it is complex
  126. * and does not help much to improve performance (IMO).
  127. */
  128. root->prio_tree_node = node;
  129. } else
  130. prio_set_parent(old->parent, node, old->parent->left == old);
  131. if (!prio_tree_left_empty(old))
  132. prio_set_parent(node, old->left, true);
  133. if (!prio_tree_right_empty(old))
  134. prio_set_parent(node, old->right, false);
  135. return old;
  136. }
  137. /*
  138. * Insert a prio_tree_node @node into a radix priority search tree @root. The
  139. * algorithm typically takes O(log n) time where 'log n' is the number of bits
  140. * required to represent the maximum heap_index. In the worst case, the algo
  141. * can take O((log n)^2) - check prio_tree_expand.
  142. *
  143. * If a prior node with same radix_index and heap_index is already found in
  144. * the tree, then returns the address of the prior node. Otherwise, inserts
  145. * @node into the tree and returns @node.
  146. */
  147. struct prio_tree_node *prio_tree_insert(struct prio_tree_root *root,
  148. struct prio_tree_node *node)
  149. {
  150. struct prio_tree_node *cur, *res = node;
  151. unsigned long radix_index, heap_index;
  152. unsigned long r_index, h_index, index, mask;
  153. int size_flag = 0;
  154. get_index(root, node, &radix_index, &heap_index);
  155. if (prio_tree_empty(root) ||
  156. heap_index > prio_tree_maxindex(root->index_bits))
  157. return prio_tree_expand(root, node, heap_index);
  158. cur = root->prio_tree_node;
  159. mask = 1UL << (root->index_bits - 1);
  160. while (mask) {
  161. get_index(root, cur, &r_index, &h_index);
  162. if (r_index == radix_index && h_index == heap_index)
  163. return cur;
  164. if (h_index < heap_index ||
  165. (h_index == heap_index && r_index > radix_index)) {
  166. struct prio_tree_node *tmp = node;
  167. node = prio_tree_replace(root, cur, node);
  168. cur = tmp;
  169. /* swap indices */
  170. index = r_index;
  171. r_index = radix_index;
  172. radix_index = index;
  173. index = h_index;
  174. h_index = heap_index;
  175. heap_index = index;
  176. }
  177. if (size_flag)
  178. index = heap_index - radix_index;
  179. else
  180. index = radix_index;
  181. if (index & mask) {
  182. if (prio_tree_right_empty(cur)) {
  183. INIT_PRIO_TREE_NODE(node);
  184. prio_set_parent(cur, node, false);
  185. return res;
  186. } else
  187. cur = cur->right;
  188. } else {
  189. if (prio_tree_left_empty(cur)) {
  190. INIT_PRIO_TREE_NODE(node);
  191. prio_set_parent(cur, node, true);
  192. return res;
  193. } else
  194. cur = cur->left;
  195. }
  196. mask >>= 1;
  197. if (!mask) {
  198. mask = 1UL << (BITS_PER_LONG - 1);
  199. size_flag = 1;
  200. }
  201. }
  202. /* Should not reach here */
  203. BUG();
  204. return NULL;
  205. }
  206. EXPORT_SYMBOL(prio_tree_insert);
  207. /*
  208. * Remove a prio_tree_node @node from a radix priority search tree @root. The
  209. * algorithm takes O(log n) time where 'log n' is the number of bits required
  210. * to represent the maximum heap_index.
  211. */
  212. void prio_tree_remove(struct prio_tree_root *root, struct prio_tree_node *node)
  213. {
  214. struct prio_tree_node *cur;
  215. unsigned long r_index, h_index_right, h_index_left;
  216. cur = node;
  217. while (!prio_tree_left_empty(cur) || !prio_tree_right_empty(cur)) {
  218. if (!prio_tree_left_empty(cur))
  219. get_index(root, cur->left, &r_index, &h_index_left);
  220. else {
  221. cur = cur->right;
  222. continue;
  223. }
  224. if (!prio_tree_right_empty(cur))
  225. get_index(root, cur->right, &r_index, &h_index_right);
  226. else {
  227. cur = cur->left;
  228. continue;
  229. }
  230. /* both h_index_left and h_index_right cannot be 0 */
  231. if (h_index_left >= h_index_right)
  232. cur = cur->left;
  233. else
  234. cur = cur->right;
  235. }
  236. if (prio_tree_root(cur)) {
  237. BUG_ON(root->prio_tree_node != cur);
  238. __INIT_PRIO_TREE_ROOT(root, root->raw);
  239. return;
  240. }
  241. if (cur->parent->right == cur)
  242. cur->parent->right = cur->parent;
  243. else
  244. cur->parent->left = cur->parent;
  245. while (cur != node)
  246. cur = prio_tree_replace(root, cur->parent, cur);
  247. }
  248. EXPORT_SYMBOL(prio_tree_remove);
  249. static void iter_walk_down(struct prio_tree_iter *iter)
  250. {
  251. iter->mask >>= 1;
  252. if (iter->mask) {
  253. if (iter->size_level)
  254. iter->size_level++;
  255. return;
  256. }
  257. if (iter->size_level) {
  258. BUG_ON(!prio_tree_left_empty(iter->cur));
  259. BUG_ON(!prio_tree_right_empty(iter->cur));
  260. iter->size_level++;
  261. iter->mask = ULONG_MAX;
  262. } else {
  263. iter->size_level = 1;
  264. iter->mask = 1UL << (BITS_PER_LONG - 1);
  265. }
  266. }
  267. static void iter_walk_up(struct prio_tree_iter *iter)
  268. {
  269. if (iter->mask == ULONG_MAX)
  270. iter->mask = 1UL;
  271. else if (iter->size_level == 1)
  272. iter->mask = 1UL;
  273. else
  274. iter->mask <<= 1;
  275. if (iter->size_level)
  276. iter->size_level--;
  277. if (!iter->size_level && (iter->value & iter->mask))
  278. iter->value ^= iter->mask;
  279. }
  280. /*
  281. * Following functions help to enumerate all prio_tree_nodes in the tree that
  282. * overlap with the input interval X [radix_index, heap_index]. The enumeration
  283. * takes O(log n + m) time where 'log n' is the height of the tree (which is
  284. * proportional to # of bits required to represent the maximum heap_index) and
  285. * 'm' is the number of prio_tree_nodes that overlap the interval X.
  286. */
  287. static struct prio_tree_node *prio_tree_left(struct prio_tree_iter *iter,
  288. unsigned long *r_index, unsigned long *h_index)
  289. {
  290. if (prio_tree_left_empty(iter->cur))
  291. return NULL;
  292. get_index(iter->root, iter->cur->left, r_index, h_index);
  293. if (iter->r_index <= *h_index) {
  294. iter->cur = iter->cur->left;
  295. iter_walk_down(iter);
  296. return iter->cur;
  297. }
  298. return NULL;
  299. }
  300. static struct prio_tree_node *prio_tree_right(struct prio_tree_iter *iter,
  301. unsigned long *r_index, unsigned long *h_index)
  302. {
  303. unsigned long value;
  304. if (prio_tree_right_empty(iter->cur))
  305. return NULL;
  306. if (iter->size_level)
  307. value = iter->value;
  308. else
  309. value = iter->value | iter->mask;
  310. if (iter->h_index < value)
  311. return NULL;
  312. get_index(iter->root, iter->cur->right, r_index, h_index);
  313. if (iter->r_index <= *h_index) {
  314. iter->cur = iter->cur->right;
  315. iter_walk_down(iter);
  316. return iter->cur;
  317. }
  318. return NULL;
  319. }
  320. static struct prio_tree_node *prio_tree_parent(struct prio_tree_iter *iter)
  321. {
  322. iter->cur = iter->cur->parent;
  323. iter_walk_up(iter);
  324. return iter->cur;
  325. }
  326. static inline int overlap(struct prio_tree_iter *iter,
  327. unsigned long r_index, unsigned long h_index)
  328. {
  329. return iter->h_index >= r_index && iter->r_index <= h_index;
  330. }
  331. /*
  332. * prio_tree_first:
  333. *
  334. * Get the first prio_tree_node that overlaps with the interval [radix_index,
  335. * heap_index]. Note that always radix_index <= heap_index. We do a pre-order
  336. * traversal of the tree.
  337. */
  338. static struct prio_tree_node *prio_tree_first(struct prio_tree_iter *iter)
  339. {
  340. struct prio_tree_root *root;
  341. unsigned long r_index, h_index;
  342. INIT_PRIO_TREE_ITER(iter);
  343. root = iter->root;
  344. if (prio_tree_empty(root))
  345. return NULL;
  346. get_index(root, root->prio_tree_node, &r_index, &h_index);
  347. if (iter->r_index > h_index)
  348. return NULL;
  349. iter->mask = 1UL << (root->index_bits - 1);
  350. iter->cur = root->prio_tree_node;
  351. while (1) {
  352. if (overlap(iter, r_index, h_index))
  353. return iter->cur;
  354. if (prio_tree_left(iter, &r_index, &h_index))
  355. continue;
  356. if (prio_tree_right(iter, &r_index, &h_index))
  357. continue;
  358. break;
  359. }
  360. return NULL;
  361. }
  362. /*
  363. * prio_tree_next:
  364. *
  365. * Get the next prio_tree_node that overlaps with the input interval in iter
  366. */
  367. struct prio_tree_node *prio_tree_next(struct prio_tree_iter *iter)
  368. {
  369. unsigned long r_index, h_index;
  370. if (iter->cur == NULL)
  371. return prio_tree_first(iter);
  372. repeat:
  373. while (prio_tree_left(iter, &r_index, &h_index))
  374. if (overlap(iter, r_index, h_index))
  375. return iter->cur;
  376. while (!prio_tree_right(iter, &r_index, &h_index)) {
  377. while (!prio_tree_root(iter->cur) &&
  378. iter->cur->parent->right == iter->cur)
  379. prio_tree_parent(iter);
  380. if (prio_tree_root(iter->cur))
  381. return NULL;
  382. prio_tree_parent(iter);
  383. }
  384. if (overlap(iter, r_index, h_index))
  385. return iter->cur;
  386. goto repeat;
  387. }
  388. EXPORT_SYMBOL(prio_tree_next);