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