diff options
author | Linus Torvalds <torvalds@linux-foundation.org> | 2016-12-14 20:25:18 -0500 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2016-12-14 20:25:18 -0500 |
commit | a57cb1c1d7974c62a5c80f7869e35b492ace12cd (patch) | |
tree | 5a42ee9a668f171143464bc86013954c1bbe94ad /lib/radix-tree.c | |
parent | cf1b3341afab9d3ad02a76b3a619ea027dcf4e28 (diff) | |
parent | e1e14ab8411df344a17687821f8f78f0a1e73cbb (diff) |
Merge branch 'akpm' (patches from Andrew)
Merge more updates from Andrew Morton:
- a few misc things
- kexec updates
- DMA-mapping updates to better support networking DMA operations
- IPC updates
- various MM changes to improve DAX fault handling
- lots of radix-tree changes, mainly to the test suite. All leading up
to reimplementing the IDA/IDR code to be a wrapper layer over the
radix-tree. However the final trigger-pulling patch is held off for
4.11.
* emailed patches from Andrew Morton <akpm@linux-foundation.org>: (114 commits)
radix tree test suite: delete unused rcupdate.c
radix tree test suite: add new tag check
radix-tree: ensure counts are initialised
radix tree test suite: cache recently freed objects
radix tree test suite: add some more functionality
idr: reduce the number of bits per level from 8 to 6
rxrpc: abstract away knowledge of IDR internals
tpm: use idr_find(), not idr_find_slowpath()
idr: add ida_is_empty
radix tree test suite: check multiorder iteration
radix-tree: fix replacement for multiorder entries
radix-tree: add radix_tree_split_preload()
radix-tree: add radix_tree_split
radix-tree: add radix_tree_join
radix-tree: delete radix_tree_range_tag_if_tagged()
radix-tree: delete radix_tree_locate_item()
radix-tree: improve multiorder iterators
btrfs: fix race in btrfs_free_dummy_fs_info()
radix-tree: improve dump output
radix-tree: make radix_tree_find_next_bit more useful
...
Diffstat (limited to 'lib/radix-tree.c')
-rw-r--r-- | lib/radix-tree.c | 890 |
1 files changed, 560 insertions, 330 deletions
diff --git a/lib/radix-tree.c b/lib/radix-tree.c index 2e8c6f7aa56e..0019aca0f328 100644 --- a/lib/radix-tree.c +++ b/lib/radix-tree.c | |||
@@ -22,6 +22,7 @@ | |||
22 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | 22 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
23 | */ | 23 | */ |
24 | 24 | ||
25 | #include <linux/cpu.h> | ||
25 | #include <linux/errno.h> | 26 | #include <linux/errno.h> |
26 | #include <linux/init.h> | 27 | #include <linux/init.h> |
27 | #include <linux/kernel.h> | 28 | #include <linux/kernel.h> |
@@ -69,6 +70,11 @@ struct radix_tree_preload { | |||
69 | }; | 70 | }; |
70 | static DEFINE_PER_CPU(struct radix_tree_preload, radix_tree_preloads) = { 0, }; | 71 | static DEFINE_PER_CPU(struct radix_tree_preload, radix_tree_preloads) = { 0, }; |
71 | 72 | ||
73 | static inline struct radix_tree_node *entry_to_node(void *ptr) | ||
74 | { | ||
75 | return (void *)((unsigned long)ptr & ~RADIX_TREE_INTERNAL_NODE); | ||
76 | } | ||
77 | |||
72 | static inline void *node_to_entry(void *ptr) | 78 | static inline void *node_to_entry(void *ptr) |
73 | { | 79 | { |
74 | return (void *)((unsigned long)ptr | RADIX_TREE_INTERNAL_NODE); | 80 | return (void *)((unsigned long)ptr | RADIX_TREE_INTERNAL_NODE); |
@@ -191,13 +197,12 @@ static inline int any_tag_set(struct radix_tree_node *node, unsigned int tag) | |||
191 | * Returns next bit offset, or size if nothing found. | 197 | * Returns next bit offset, or size if nothing found. |
192 | */ | 198 | */ |
193 | static __always_inline unsigned long | 199 | static __always_inline unsigned long |
194 | radix_tree_find_next_bit(const unsigned long *addr, | 200 | radix_tree_find_next_bit(struct radix_tree_node *node, unsigned int tag, |
195 | unsigned long size, unsigned long offset) | 201 | unsigned long offset) |
196 | { | 202 | { |
197 | if (!__builtin_constant_p(size)) | 203 | const unsigned long *addr = node->tags[tag]; |
198 | return find_next_bit(addr, size, offset); | ||
199 | 204 | ||
200 | if (offset < size) { | 205 | if (offset < RADIX_TREE_MAP_SIZE) { |
201 | unsigned long tmp; | 206 | unsigned long tmp; |
202 | 207 | ||
203 | addr += offset / BITS_PER_LONG; | 208 | addr += offset / BITS_PER_LONG; |
@@ -205,14 +210,32 @@ radix_tree_find_next_bit(const unsigned long *addr, | |||
205 | if (tmp) | 210 | if (tmp) |
206 | return __ffs(tmp) + offset; | 211 | return __ffs(tmp) + offset; |
207 | offset = (offset + BITS_PER_LONG) & ~(BITS_PER_LONG - 1); | 212 | offset = (offset + BITS_PER_LONG) & ~(BITS_PER_LONG - 1); |
208 | while (offset < size) { | 213 | while (offset < RADIX_TREE_MAP_SIZE) { |
209 | tmp = *++addr; | 214 | tmp = *++addr; |
210 | if (tmp) | 215 | if (tmp) |
211 | return __ffs(tmp) + offset; | 216 | return __ffs(tmp) + offset; |
212 | offset += BITS_PER_LONG; | 217 | offset += BITS_PER_LONG; |
213 | } | 218 | } |
214 | } | 219 | } |
215 | return size; | 220 | return RADIX_TREE_MAP_SIZE; |
221 | } | ||
222 | |||
223 | static unsigned int iter_offset(const struct radix_tree_iter *iter) | ||
224 | { | ||
225 | return (iter->index >> iter_shift(iter)) & RADIX_TREE_MAP_MASK; | ||
226 | } | ||
227 | |||
228 | /* | ||
229 | * The maximum index which can be stored in a radix tree | ||
230 | */ | ||
231 | static inline unsigned long shift_maxindex(unsigned int shift) | ||
232 | { | ||
233 | return (RADIX_TREE_MAP_SIZE << shift) - 1; | ||
234 | } | ||
235 | |||
236 | static inline unsigned long node_maxindex(struct radix_tree_node *node) | ||
237 | { | ||
238 | return shift_maxindex(node->shift); | ||
216 | } | 239 | } |
217 | 240 | ||
218 | #ifndef __KERNEL__ | 241 | #ifndef __KERNEL__ |
@@ -220,10 +243,11 @@ static void dump_node(struct radix_tree_node *node, unsigned long index) | |||
220 | { | 243 | { |
221 | unsigned long i; | 244 | unsigned long i; |
222 | 245 | ||
223 | pr_debug("radix node: %p offset %d tags %lx %lx %lx shift %d count %d exceptional %d parent %p\n", | 246 | pr_debug("radix node: %p offset %d indices %lu-%lu parent %p tags %lx %lx %lx shift %d count %d exceptional %d\n", |
224 | node, node->offset, | 247 | node, node->offset, index, index | node_maxindex(node), |
248 | node->parent, | ||
225 | node->tags[0][0], node->tags[1][0], node->tags[2][0], | 249 | node->tags[0][0], node->tags[1][0], node->tags[2][0], |
226 | node->shift, node->count, node->exceptional, node->parent); | 250 | node->shift, node->count, node->exceptional); |
227 | 251 | ||
228 | for (i = 0; i < RADIX_TREE_MAP_SIZE; i++) { | 252 | for (i = 0; i < RADIX_TREE_MAP_SIZE; i++) { |
229 | unsigned long first = index | (i << node->shift); | 253 | unsigned long first = index | (i << node->shift); |
@@ -231,14 +255,16 @@ static void dump_node(struct radix_tree_node *node, unsigned long index) | |||
231 | void *entry = node->slots[i]; | 255 | void *entry = node->slots[i]; |
232 | if (!entry) | 256 | if (!entry) |
233 | continue; | 257 | continue; |
234 | if (is_sibling_entry(node, entry)) { | 258 | if (entry == RADIX_TREE_RETRY) { |
235 | pr_debug("radix sblng %p offset %ld val %p indices %ld-%ld\n", | 259 | pr_debug("radix retry offset %ld indices %lu-%lu parent %p\n", |
236 | entry, i, | 260 | i, first, last, node); |
237 | *(void **)entry_to_node(entry), | ||
238 | first, last); | ||
239 | } else if (!radix_tree_is_internal_node(entry)) { | 261 | } else if (!radix_tree_is_internal_node(entry)) { |
240 | pr_debug("radix entry %p offset %ld indices %ld-%ld\n", | 262 | pr_debug("radix entry %p offset %ld indices %lu-%lu parent %p\n", |
241 | entry, i, first, last); | 263 | entry, i, first, last, node); |
264 | } else if (is_sibling_entry(node, entry)) { | ||
265 | pr_debug("radix sblng %p offset %ld indices %lu-%lu parent %p val %p\n", | ||
266 | entry, i, first, last, node, | ||
267 | *(void **)entry_to_node(entry)); | ||
242 | } else { | 268 | } else { |
243 | dump_node(entry_to_node(entry), first); | 269 | dump_node(entry_to_node(entry), first); |
244 | } | 270 | } |
@@ -262,7 +288,10 @@ static void radix_tree_dump(struct radix_tree_root *root) | |||
262 | * that the caller has pinned this thread of control to the current CPU. | 288 | * that the caller has pinned this thread of control to the current CPU. |
263 | */ | 289 | */ |
264 | static struct radix_tree_node * | 290 | static struct radix_tree_node * |
265 | radix_tree_node_alloc(struct radix_tree_root *root) | 291 | radix_tree_node_alloc(struct radix_tree_root *root, |
292 | struct radix_tree_node *parent, | ||
293 | unsigned int shift, unsigned int offset, | ||
294 | unsigned int count, unsigned int exceptional) | ||
266 | { | 295 | { |
267 | struct radix_tree_node *ret = NULL; | 296 | struct radix_tree_node *ret = NULL; |
268 | gfp_t gfp_mask = root_gfp_mask(root); | 297 | gfp_t gfp_mask = root_gfp_mask(root); |
@@ -307,6 +336,13 @@ radix_tree_node_alloc(struct radix_tree_root *root) | |||
307 | ret = kmem_cache_alloc(radix_tree_node_cachep, gfp_mask); | 336 | ret = kmem_cache_alloc(radix_tree_node_cachep, gfp_mask); |
308 | out: | 337 | out: |
309 | BUG_ON(radix_tree_is_internal_node(ret)); | 338 | BUG_ON(radix_tree_is_internal_node(ret)); |
339 | if (ret) { | ||
340 | ret->parent = parent; | ||
341 | ret->shift = shift; | ||
342 | ret->offset = offset; | ||
343 | ret->count = count; | ||
344 | ret->exceptional = exceptional; | ||
345 | } | ||
310 | return ret; | 346 | return ret; |
311 | } | 347 | } |
312 | 348 | ||
@@ -314,17 +350,15 @@ static void radix_tree_node_rcu_free(struct rcu_head *head) | |||
314 | { | 350 | { |
315 | struct radix_tree_node *node = | 351 | struct radix_tree_node *node = |
316 | container_of(head, struct radix_tree_node, rcu_head); | 352 | container_of(head, struct radix_tree_node, rcu_head); |
317 | int i; | ||
318 | 353 | ||
319 | /* | 354 | /* |
320 | * must only free zeroed nodes into the slab. radix_tree_shrink | 355 | * Must only free zeroed nodes into the slab. We can be left with |
321 | * can leave us with a non-NULL entry in the first slot, so clear | 356 | * non-NULL entries by radix_tree_free_nodes, so clear the entries |
322 | * that here to make sure. | 357 | * and tags here. |
323 | */ | 358 | */ |
324 | for (i = 0; i < RADIX_TREE_MAX_TAGS; i++) | 359 | memset(node->slots, 0, sizeof(node->slots)); |
325 | tag_clear(node, i, 0); | 360 | memset(node->tags, 0, sizeof(node->tags)); |
326 | 361 | INIT_LIST_HEAD(&node->private_list); | |
327 | node->slots[0] = NULL; | ||
328 | 362 | ||
329 | kmem_cache_free(radix_tree_node_cachep, node); | 363 | kmem_cache_free(radix_tree_node_cachep, node); |
330 | } | 364 | } |
@@ -344,7 +378,7 @@ radix_tree_node_free(struct radix_tree_node *node) | |||
344 | * To make use of this facility, the radix tree must be initialised without | 378 | * To make use of this facility, the radix tree must be initialised without |
345 | * __GFP_DIRECT_RECLAIM being passed to INIT_RADIX_TREE(). | 379 | * __GFP_DIRECT_RECLAIM being passed to INIT_RADIX_TREE(). |
346 | */ | 380 | */ |
347 | static int __radix_tree_preload(gfp_t gfp_mask, int nr) | 381 | static int __radix_tree_preload(gfp_t gfp_mask, unsigned nr) |
348 | { | 382 | { |
349 | struct radix_tree_preload *rtp; | 383 | struct radix_tree_preload *rtp; |
350 | struct radix_tree_node *node; | 384 | struct radix_tree_node *node; |
@@ -410,6 +444,28 @@ int radix_tree_maybe_preload(gfp_t gfp_mask) | |||
410 | } | 444 | } |
411 | EXPORT_SYMBOL(radix_tree_maybe_preload); | 445 | EXPORT_SYMBOL(radix_tree_maybe_preload); |
412 | 446 | ||
447 | #ifdef CONFIG_RADIX_TREE_MULTIORDER | ||
448 | /* | ||
449 | * Preload with enough objects to ensure that we can split a single entry | ||
450 | * of order @old_order into many entries of size @new_order | ||
451 | */ | ||
452 | int radix_tree_split_preload(unsigned int old_order, unsigned int new_order, | ||
453 | gfp_t gfp_mask) | ||
454 | { | ||
455 | unsigned top = 1 << (old_order % RADIX_TREE_MAP_SHIFT); | ||
456 | unsigned layers = (old_order / RADIX_TREE_MAP_SHIFT) - | ||
457 | (new_order / RADIX_TREE_MAP_SHIFT); | ||
458 | unsigned nr = 0; | ||
459 | |||
460 | WARN_ON_ONCE(!gfpflags_allow_blocking(gfp_mask)); | ||
461 | BUG_ON(new_order >= old_order); | ||
462 | |||
463 | while (layers--) | ||
464 | nr = nr * RADIX_TREE_MAP_SIZE + 1; | ||
465 | return __radix_tree_preload(gfp_mask, top * nr); | ||
466 | } | ||
467 | #endif | ||
468 | |||
413 | /* | 469 | /* |
414 | * The same as function above, but preload number of nodes required to insert | 470 | * The same as function above, but preload number of nodes required to insert |
415 | * (1 << order) continuous naturally-aligned elements. | 471 | * (1 << order) continuous naturally-aligned elements. |
@@ -455,19 +511,6 @@ int radix_tree_maybe_preload_order(gfp_t gfp_mask, int order) | |||
455 | return __radix_tree_preload(gfp_mask, nr_nodes); | 511 | return __radix_tree_preload(gfp_mask, nr_nodes); |
456 | } | 512 | } |
457 | 513 | ||
458 | /* | ||
459 | * The maximum index which can be stored in a radix tree | ||
460 | */ | ||
461 | static inline unsigned long shift_maxindex(unsigned int shift) | ||
462 | { | ||
463 | return (RADIX_TREE_MAP_SIZE << shift) - 1; | ||
464 | } | ||
465 | |||
466 | static inline unsigned long node_maxindex(struct radix_tree_node *node) | ||
467 | { | ||
468 | return shift_maxindex(node->shift); | ||
469 | } | ||
470 | |||
471 | static unsigned radix_tree_load_root(struct radix_tree_root *root, | 514 | static unsigned radix_tree_load_root(struct radix_tree_root *root, |
472 | struct radix_tree_node **nodep, unsigned long *maxindex) | 515 | struct radix_tree_node **nodep, unsigned long *maxindex) |
473 | { | 516 | { |
@@ -505,8 +548,8 @@ static int radix_tree_extend(struct radix_tree_root *root, | |||
505 | goto out; | 548 | goto out; |
506 | 549 | ||
507 | do { | 550 | do { |
508 | struct radix_tree_node *node = radix_tree_node_alloc(root); | 551 | struct radix_tree_node *node = radix_tree_node_alloc(root, |
509 | 552 | NULL, shift, 0, 1, 0); | |
510 | if (!node) | 553 | if (!node) |
511 | return -ENOMEM; | 554 | return -ENOMEM; |
512 | 555 | ||
@@ -517,16 +560,11 @@ static int radix_tree_extend(struct radix_tree_root *root, | |||
517 | } | 560 | } |
518 | 561 | ||
519 | BUG_ON(shift > BITS_PER_LONG); | 562 | BUG_ON(shift > BITS_PER_LONG); |
520 | node->shift = shift; | ||
521 | node->offset = 0; | ||
522 | node->count = 1; | ||
523 | node->parent = NULL; | ||
524 | if (radix_tree_is_internal_node(slot)) { | 563 | if (radix_tree_is_internal_node(slot)) { |
525 | entry_to_node(slot)->parent = node; | 564 | entry_to_node(slot)->parent = node; |
526 | } else { | 565 | } else if (radix_tree_exceptional_entry(slot)) { |
527 | /* Moving an exceptional root->rnode to a node */ | 566 | /* Moving an exceptional root->rnode to a node */ |
528 | if (radix_tree_exceptional_entry(slot)) | 567 | node->exceptional = 1; |
529 | node->exceptional = 1; | ||
530 | } | 568 | } |
531 | node->slots[0] = slot; | 569 | node->slots[0] = slot; |
532 | slot = node_to_entry(node); | 570 | slot = node_to_entry(node); |
@@ -665,26 +703,24 @@ int __radix_tree_create(struct radix_tree_root *root, unsigned long index, | |||
665 | shift = radix_tree_load_root(root, &child, &maxindex); | 703 | shift = radix_tree_load_root(root, &child, &maxindex); |
666 | 704 | ||
667 | /* Make sure the tree is high enough. */ | 705 | /* Make sure the tree is high enough. */ |
706 | if (order > 0 && max == ((1UL << order) - 1)) | ||
707 | max++; | ||
668 | if (max > maxindex) { | 708 | if (max > maxindex) { |
669 | int error = radix_tree_extend(root, max, shift); | 709 | int error = radix_tree_extend(root, max, shift); |
670 | if (error < 0) | 710 | if (error < 0) |
671 | return error; | 711 | return error; |
672 | shift = error; | 712 | shift = error; |
673 | child = root->rnode; | 713 | child = root->rnode; |
674 | if (order == shift) | ||
675 | shift += RADIX_TREE_MAP_SHIFT; | ||
676 | } | 714 | } |
677 | 715 | ||
678 | while (shift > order) { | 716 | while (shift > order) { |
679 | shift -= RADIX_TREE_MAP_SHIFT; | 717 | shift -= RADIX_TREE_MAP_SHIFT; |
680 | if (child == NULL) { | 718 | if (child == NULL) { |
681 | /* Have to add a child node. */ | 719 | /* Have to add a child node. */ |
682 | child = radix_tree_node_alloc(root); | 720 | child = radix_tree_node_alloc(root, node, shift, |
721 | offset, 0, 0); | ||
683 | if (!child) | 722 | if (!child) |
684 | return -ENOMEM; | 723 | return -ENOMEM; |
685 | child->shift = shift; | ||
686 | child->offset = offset; | ||
687 | child->parent = node; | ||
688 | rcu_assign_pointer(*slot, node_to_entry(child)); | 724 | rcu_assign_pointer(*slot, node_to_entry(child)); |
689 | if (node) | 725 | if (node) |
690 | node->count++; | 726 | node->count++; |
@@ -697,31 +733,125 @@ int __radix_tree_create(struct radix_tree_root *root, unsigned long index, | |||
697 | slot = &node->slots[offset]; | 733 | slot = &node->slots[offset]; |
698 | } | 734 | } |
699 | 735 | ||
736 | if (nodep) | ||
737 | *nodep = node; | ||
738 | if (slotp) | ||
739 | *slotp = slot; | ||
740 | return 0; | ||
741 | } | ||
742 | |||
700 | #ifdef CONFIG_RADIX_TREE_MULTIORDER | 743 | #ifdef CONFIG_RADIX_TREE_MULTIORDER |
701 | /* Insert pointers to the canonical entry */ | 744 | /* |
702 | if (order > shift) { | 745 | * Free any nodes below this node. The tree is presumed to not need |
703 | unsigned i, n = 1 << (order - shift); | 746 | * shrinking, and any user data in the tree is presumed to not need a |
747 | * destructor called on it. If we need to add a destructor, we can | ||
748 | * add that functionality later. Note that we may not clear tags or | ||
749 | * slots from the tree as an RCU walker may still have a pointer into | ||
750 | * this subtree. We could replace the entries with RADIX_TREE_RETRY, | ||
751 | * but we'll still have to clear those in rcu_free. | ||
752 | */ | ||
753 | static void radix_tree_free_nodes(struct radix_tree_node *node) | ||
754 | { | ||
755 | unsigned offset = 0; | ||
756 | struct radix_tree_node *child = entry_to_node(node); | ||
757 | |||
758 | for (;;) { | ||
759 | void *entry = child->slots[offset]; | ||
760 | if (radix_tree_is_internal_node(entry) && | ||
761 | !is_sibling_entry(child, entry)) { | ||
762 | child = entry_to_node(entry); | ||
763 | offset = 0; | ||
764 | continue; | ||
765 | } | ||
766 | offset++; | ||
767 | while (offset == RADIX_TREE_MAP_SIZE) { | ||
768 | struct radix_tree_node *old = child; | ||
769 | offset = child->offset + 1; | ||
770 | child = child->parent; | ||
771 | radix_tree_node_free(old); | ||
772 | if (old == entry_to_node(node)) | ||
773 | return; | ||
774 | } | ||
775 | } | ||
776 | } | ||
777 | |||
778 | static inline int insert_entries(struct radix_tree_node *node, void **slot, | ||
779 | void *item, unsigned order, bool replace) | ||
780 | { | ||
781 | struct radix_tree_node *child; | ||
782 | unsigned i, n, tag, offset, tags = 0; | ||
783 | |||
784 | if (node) { | ||
785 | if (order > node->shift) | ||
786 | n = 1 << (order - node->shift); | ||
787 | else | ||
788 | n = 1; | ||
789 | offset = get_slot_offset(node, slot); | ||
790 | } else { | ||
791 | n = 1; | ||
792 | offset = 0; | ||
793 | } | ||
794 | |||
795 | if (n > 1) { | ||
704 | offset = offset & ~(n - 1); | 796 | offset = offset & ~(n - 1); |
705 | slot = &node->slots[offset]; | 797 | slot = &node->slots[offset]; |
706 | child = node_to_entry(slot); | 798 | } |
707 | for (i = 0; i < n; i++) { | 799 | child = node_to_entry(slot); |
708 | if (slot[i]) | 800 | |
801 | for (i = 0; i < n; i++) { | ||
802 | if (slot[i]) { | ||
803 | if (replace) { | ||
804 | node->count--; | ||
805 | for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) | ||
806 | if (tag_get(node, tag, offset + i)) | ||
807 | tags |= 1 << tag; | ||
808 | } else | ||
709 | return -EEXIST; | 809 | return -EEXIST; |
710 | } | 810 | } |
811 | } | ||
711 | 812 | ||
712 | for (i = 1; i < n; i++) { | 813 | for (i = 0; i < n; i++) { |
814 | struct radix_tree_node *old = slot[i]; | ||
815 | if (i) { | ||
713 | rcu_assign_pointer(slot[i], child); | 816 | rcu_assign_pointer(slot[i], child); |
714 | node->count++; | 817 | for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) |
818 | if (tags & (1 << tag)) | ||
819 | tag_clear(node, tag, offset + i); | ||
820 | } else { | ||
821 | rcu_assign_pointer(slot[i], item); | ||
822 | for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) | ||
823 | if (tags & (1 << tag)) | ||
824 | tag_set(node, tag, offset); | ||
715 | } | 825 | } |
826 | if (radix_tree_is_internal_node(old) && | ||
827 | !is_sibling_entry(node, old) && | ||
828 | (old != RADIX_TREE_RETRY)) | ||
829 | radix_tree_free_nodes(old); | ||
830 | if (radix_tree_exceptional_entry(old)) | ||
831 | node->exceptional--; | ||
716 | } | 832 | } |
717 | #endif | 833 | if (node) { |
718 | 834 | node->count += n; | |
719 | if (nodep) | 835 | if (radix_tree_exceptional_entry(item)) |
720 | *nodep = node; | 836 | node->exceptional += n; |
721 | if (slotp) | 837 | } |
722 | *slotp = slot; | 838 | return n; |
723 | return 0; | 839 | } |
840 | #else | ||
841 | static inline int insert_entries(struct radix_tree_node *node, void **slot, | ||
842 | void *item, unsigned order, bool replace) | ||
843 | { | ||
844 | if (*slot) | ||
845 | return -EEXIST; | ||
846 | rcu_assign_pointer(*slot, item); | ||
847 | if (node) { | ||
848 | node->count++; | ||
849 | if (radix_tree_exceptional_entry(item)) | ||
850 | node->exceptional++; | ||
851 | } | ||
852 | return 1; | ||
724 | } | 853 | } |
854 | #endif | ||
725 | 855 | ||
726 | /** | 856 | /** |
727 | * __radix_tree_insert - insert into a radix tree | 857 | * __radix_tree_insert - insert into a radix tree |
@@ -744,15 +874,13 @@ int __radix_tree_insert(struct radix_tree_root *root, unsigned long index, | |||
744 | error = __radix_tree_create(root, index, order, &node, &slot); | 874 | error = __radix_tree_create(root, index, order, &node, &slot); |
745 | if (error) | 875 | if (error) |
746 | return error; | 876 | return error; |
747 | if (*slot != NULL) | 877 | |
748 | return -EEXIST; | 878 | error = insert_entries(node, slot, item, order, false); |
749 | rcu_assign_pointer(*slot, item); | 879 | if (error < 0) |
880 | return error; | ||
750 | 881 | ||
751 | if (node) { | 882 | if (node) { |
752 | unsigned offset = get_slot_offset(node, slot); | 883 | unsigned offset = get_slot_offset(node, slot); |
753 | node->count++; | ||
754 | if (radix_tree_exceptional_entry(item)) | ||
755 | node->exceptional++; | ||
756 | BUG_ON(tag_get(node, 0, offset)); | 884 | BUG_ON(tag_get(node, 0, offset)); |
757 | BUG_ON(tag_get(node, 1, offset)); | 885 | BUG_ON(tag_get(node, 1, offset)); |
758 | BUG_ON(tag_get(node, 2, offset)); | 886 | BUG_ON(tag_get(node, 2, offset)); |
@@ -850,6 +978,24 @@ void *radix_tree_lookup(struct radix_tree_root *root, unsigned long index) | |||
850 | } | 978 | } |
851 | EXPORT_SYMBOL(radix_tree_lookup); | 979 | EXPORT_SYMBOL(radix_tree_lookup); |
852 | 980 | ||
981 | static inline int slot_count(struct radix_tree_node *node, | ||
982 | void **slot) | ||
983 | { | ||
984 | int n = 1; | ||
985 | #ifdef CONFIG_RADIX_TREE_MULTIORDER | ||
986 | void *ptr = node_to_entry(slot); | ||
987 | unsigned offset = get_slot_offset(node, slot); | ||
988 | int i; | ||
989 | |||
990 | for (i = 1; offset + i < RADIX_TREE_MAP_SIZE; i++) { | ||
991 | if (node->slots[offset + i] != ptr) | ||
992 | break; | ||
993 | n++; | ||
994 | } | ||
995 | #endif | ||
996 | return n; | ||
997 | } | ||
998 | |||
853 | static void replace_slot(struct radix_tree_root *root, | 999 | static void replace_slot(struct radix_tree_root *root, |
854 | struct radix_tree_node *node, | 1000 | struct radix_tree_node *node, |
855 | void **slot, void *item, | 1001 | void **slot, void *item, |
@@ -868,12 +1014,35 @@ static void replace_slot(struct radix_tree_root *root, | |||
868 | 1014 | ||
869 | if (node) { | 1015 | if (node) { |
870 | node->count += count; | 1016 | node->count += count; |
871 | node->exceptional += exceptional; | 1017 | if (exceptional) { |
1018 | exceptional *= slot_count(node, slot); | ||
1019 | node->exceptional += exceptional; | ||
1020 | } | ||
872 | } | 1021 | } |
873 | 1022 | ||
874 | rcu_assign_pointer(*slot, item); | 1023 | rcu_assign_pointer(*slot, item); |
875 | } | 1024 | } |
876 | 1025 | ||
1026 | static inline void delete_sibling_entries(struct radix_tree_node *node, | ||
1027 | void **slot) | ||
1028 | { | ||
1029 | #ifdef CONFIG_RADIX_TREE_MULTIORDER | ||
1030 | bool exceptional = radix_tree_exceptional_entry(*slot); | ||
1031 | void *ptr = node_to_entry(slot); | ||
1032 | unsigned offset = get_slot_offset(node, slot); | ||
1033 | int i; | ||
1034 | |||
1035 | for (i = 1; offset + i < RADIX_TREE_MAP_SIZE; i++) { | ||
1036 | if (node->slots[offset + i] != ptr) | ||
1037 | break; | ||
1038 | node->slots[offset + i] = NULL; | ||
1039 | node->count--; | ||
1040 | if (exceptional) | ||
1041 | node->exceptional--; | ||
1042 | } | ||
1043 | #endif | ||
1044 | } | ||
1045 | |||
877 | /** | 1046 | /** |
878 | * __radix_tree_replace - replace item in a slot | 1047 | * __radix_tree_replace - replace item in a slot |
879 | * @root: radix tree root | 1048 | * @root: radix tree root |
@@ -891,6 +1060,8 @@ void __radix_tree_replace(struct radix_tree_root *root, | |||
891 | void **slot, void *item, | 1060 | void **slot, void *item, |
892 | radix_tree_update_node_t update_node, void *private) | 1061 | radix_tree_update_node_t update_node, void *private) |
893 | { | 1062 | { |
1063 | if (!item) | ||
1064 | delete_sibling_entries(node, slot); | ||
894 | /* | 1065 | /* |
895 | * This function supports replacing exceptional entries and | 1066 | * This function supports replacing exceptional entries and |
896 | * deleting entries, but that needs accounting against the | 1067 | * deleting entries, but that needs accounting against the |
@@ -921,7 +1092,8 @@ void __radix_tree_replace(struct radix_tree_root *root, | |||
921 | * NOTE: This cannot be used to switch between non-entries (empty slots), | 1092 | * NOTE: This cannot be used to switch between non-entries (empty slots), |
922 | * regular entries, and exceptional entries, as that requires accounting | 1093 | * regular entries, and exceptional entries, as that requires accounting |
923 | * inside the radix tree node. When switching from one type of entry or | 1094 | * inside the radix tree node. When switching from one type of entry or |
924 | * deleting, use __radix_tree_lookup() and __radix_tree_replace(). | 1095 | * deleting, use __radix_tree_lookup() and __radix_tree_replace() or |
1096 | * radix_tree_iter_replace(). | ||
925 | */ | 1097 | */ |
926 | void radix_tree_replace_slot(struct radix_tree_root *root, | 1098 | void radix_tree_replace_slot(struct radix_tree_root *root, |
927 | void **slot, void *item) | 1099 | void **slot, void *item) |
@@ -930,6 +1102,164 @@ void radix_tree_replace_slot(struct radix_tree_root *root, | |||
930 | } | 1102 | } |
931 | 1103 | ||
932 | /** | 1104 | /** |
1105 | * radix_tree_iter_replace - replace item in a slot | ||
1106 | * @root: radix tree root | ||
1107 | * @slot: pointer to slot | ||
1108 | * @item: new item to store in the slot. | ||
1109 | * | ||
1110 | * For use with radix_tree_split() and radix_tree_for_each_slot(). | ||
1111 | * Caller must hold tree write locked across split and replacement. | ||
1112 | */ | ||
1113 | void radix_tree_iter_replace(struct radix_tree_root *root, | ||
1114 | const struct radix_tree_iter *iter, void **slot, void *item) | ||
1115 | { | ||
1116 | __radix_tree_replace(root, iter->node, slot, item, NULL, NULL); | ||
1117 | } | ||
1118 | |||
1119 | #ifdef CONFIG_RADIX_TREE_MULTIORDER | ||
1120 | /** | ||
1121 | * radix_tree_join - replace multiple entries with one multiorder entry | ||
1122 | * @root: radix tree root | ||
1123 | * @index: an index inside the new entry | ||
1124 | * @order: order of the new entry | ||
1125 | * @item: new entry | ||
1126 | * | ||
1127 | * Call this function to replace several entries with one larger entry. | ||
1128 | * The existing entries are presumed to not need freeing as a result of | ||
1129 | * this call. | ||
1130 | * | ||
1131 | * The replacement entry will have all the tags set on it that were set | ||
1132 | * on any of the entries it is replacing. | ||
1133 | */ | ||
1134 | int radix_tree_join(struct radix_tree_root *root, unsigned long index, | ||
1135 | unsigned order, void *item) | ||
1136 | { | ||
1137 | struct radix_tree_node *node; | ||
1138 | void **slot; | ||
1139 | int error; | ||
1140 | |||
1141 | BUG_ON(radix_tree_is_internal_node(item)); | ||
1142 | |||
1143 | error = __radix_tree_create(root, index, order, &node, &slot); | ||
1144 | if (!error) | ||
1145 | error = insert_entries(node, slot, item, order, true); | ||
1146 | if (error > 0) | ||
1147 | error = 0; | ||
1148 | |||
1149 | return error; | ||
1150 | } | ||
1151 | |||
1152 | /** | ||
1153 | * radix_tree_split - Split an entry into smaller entries | ||
1154 | * @root: radix tree root | ||
1155 | * @index: An index within the large entry | ||
1156 | * @order: Order of new entries | ||
1157 | * | ||
1158 | * Call this function as the first step in replacing a multiorder entry | ||
1159 | * with several entries of lower order. After this function returns, | ||
1160 | * loop over the relevant portion of the tree using radix_tree_for_each_slot() | ||
1161 | * and call radix_tree_iter_replace() to set up each new entry. | ||
1162 | * | ||
1163 | * The tags from this entry are replicated to all the new entries. | ||
1164 | * | ||
1165 | * The radix tree should be locked against modification during the entire | ||
1166 | * replacement operation. Lock-free lookups will see RADIX_TREE_RETRY which | ||
1167 | * should prompt RCU walkers to restart the lookup from the root. | ||
1168 | */ | ||
1169 | int radix_tree_split(struct radix_tree_root *root, unsigned long index, | ||
1170 | unsigned order) | ||
1171 | { | ||
1172 | struct radix_tree_node *parent, *node, *child; | ||
1173 | void **slot; | ||
1174 | unsigned int offset, end; | ||
1175 | unsigned n, tag, tags = 0; | ||
1176 | |||
1177 | if (!__radix_tree_lookup(root, index, &parent, &slot)) | ||
1178 | return -ENOENT; | ||
1179 | if (!parent) | ||
1180 | return -ENOENT; | ||
1181 | |||
1182 | offset = get_slot_offset(parent, slot); | ||
1183 | |||
1184 | for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) | ||
1185 | if (tag_get(parent, tag, offset)) | ||
1186 | tags |= 1 << tag; | ||
1187 | |||
1188 | for (end = offset + 1; end < RADIX_TREE_MAP_SIZE; end++) { | ||
1189 | if (!is_sibling_entry(parent, parent->slots[end])) | ||
1190 | break; | ||
1191 | for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) | ||
1192 | if (tags & (1 << tag)) | ||
1193 | tag_set(parent, tag, end); | ||
1194 | /* rcu_assign_pointer ensures tags are set before RETRY */ | ||
1195 | rcu_assign_pointer(parent->slots[end], RADIX_TREE_RETRY); | ||
1196 | } | ||
1197 | rcu_assign_pointer(parent->slots[offset], RADIX_TREE_RETRY); | ||
1198 | parent->exceptional -= (end - offset); | ||
1199 | |||
1200 | if (order == parent->shift) | ||
1201 | return 0; | ||
1202 | if (order > parent->shift) { | ||
1203 | while (offset < end) | ||
1204 | offset += insert_entries(parent, &parent->slots[offset], | ||
1205 | RADIX_TREE_RETRY, order, true); | ||
1206 | return 0; | ||
1207 | } | ||
1208 | |||
1209 | node = parent; | ||
1210 | |||
1211 | for (;;) { | ||
1212 | if (node->shift > order) { | ||
1213 | child = radix_tree_node_alloc(root, node, | ||
1214 | node->shift - RADIX_TREE_MAP_SHIFT, | ||
1215 | offset, 0, 0); | ||
1216 | if (!child) | ||
1217 | goto nomem; | ||
1218 | if (node != parent) { | ||
1219 | node->count++; | ||
1220 | node->slots[offset] = node_to_entry(child); | ||
1221 | for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) | ||
1222 | if (tags & (1 << tag)) | ||
1223 | tag_set(node, tag, offset); | ||
1224 | } | ||
1225 | |||
1226 | node = child; | ||
1227 | offset = 0; | ||
1228 | continue; | ||
1229 | } | ||
1230 | |||
1231 | n = insert_entries(node, &node->slots[offset], | ||
1232 | RADIX_TREE_RETRY, order, false); | ||
1233 | BUG_ON(n > RADIX_TREE_MAP_SIZE); | ||
1234 | |||
1235 | for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) | ||
1236 | if (tags & (1 << tag)) | ||
1237 | tag_set(node, tag, offset); | ||
1238 | offset += n; | ||
1239 | |||
1240 | while (offset == RADIX_TREE_MAP_SIZE) { | ||
1241 | if (node == parent) | ||
1242 | break; | ||
1243 | offset = node->offset; | ||
1244 | child = node; | ||
1245 | node = node->parent; | ||
1246 | rcu_assign_pointer(node->slots[offset], | ||
1247 | node_to_entry(child)); | ||
1248 | offset++; | ||
1249 | } | ||
1250 | if ((node == parent) && (offset == end)) | ||
1251 | return 0; | ||
1252 | } | ||
1253 | |||
1254 | nomem: | ||
1255 | /* Shouldn't happen; did user forget to preload? */ | ||
1256 | /* TODO: free all the allocated nodes */ | ||
1257 | WARN_ON(1); | ||
1258 | return -ENOMEM; | ||
1259 | } | ||
1260 | #endif | ||
1261 | |||
1262 | /** | ||
933 | * radix_tree_tag_set - set a tag on a radix tree node | 1263 | * radix_tree_tag_set - set a tag on a radix tree node |
934 | * @root: radix tree root | 1264 | * @root: radix tree root |
935 | * @index: index key | 1265 | * @index: index key |
@@ -990,6 +1320,34 @@ static void node_tag_clear(struct radix_tree_root *root, | |||
990 | root_tag_clear(root, tag); | 1320 | root_tag_clear(root, tag); |
991 | } | 1321 | } |
992 | 1322 | ||
1323 | static void node_tag_set(struct radix_tree_root *root, | ||
1324 | struct radix_tree_node *node, | ||
1325 | unsigned int tag, unsigned int offset) | ||
1326 | { | ||
1327 | while (node) { | ||
1328 | if (tag_get(node, tag, offset)) | ||
1329 | return; | ||
1330 | tag_set(node, tag, offset); | ||
1331 | offset = node->offset; | ||
1332 | node = node->parent; | ||
1333 | } | ||
1334 | |||
1335 | if (!root_tag_get(root, tag)) | ||
1336 | root_tag_set(root, tag); | ||
1337 | } | ||
1338 | |||
1339 | /** | ||
1340 | * radix_tree_iter_tag_set - set a tag on the current iterator entry | ||
1341 | * @root: radix tree root | ||
1342 | * @iter: iterator state | ||
1343 | * @tag: tag to set | ||
1344 | */ | ||
1345 | void radix_tree_iter_tag_set(struct radix_tree_root *root, | ||
1346 | const struct radix_tree_iter *iter, unsigned int tag) | ||
1347 | { | ||
1348 | node_tag_set(root, iter->node, tag, iter_offset(iter)); | ||
1349 | } | ||
1350 | |||
993 | /** | 1351 | /** |
994 | * radix_tree_tag_clear - clear a tag on a radix tree node | 1352 | * radix_tree_tag_clear - clear a tag on a radix tree node |
995 | * @root: radix tree root | 1353 | * @root: radix tree root |
@@ -1085,6 +1443,121 @@ static inline void __set_iter_shift(struct radix_tree_iter *iter, | |||
1085 | #endif | 1443 | #endif |
1086 | } | 1444 | } |
1087 | 1445 | ||
1446 | /* Construct iter->tags bit-mask from node->tags[tag] array */ | ||
1447 | static void set_iter_tags(struct radix_tree_iter *iter, | ||
1448 | struct radix_tree_node *node, unsigned offset, | ||
1449 | unsigned tag) | ||
1450 | { | ||
1451 | unsigned tag_long = offset / BITS_PER_LONG; | ||
1452 | unsigned tag_bit = offset % BITS_PER_LONG; | ||
1453 | |||
1454 | iter->tags = node->tags[tag][tag_long] >> tag_bit; | ||
1455 | |||
1456 | /* This never happens if RADIX_TREE_TAG_LONGS == 1 */ | ||
1457 | if (tag_long < RADIX_TREE_TAG_LONGS - 1) { | ||
1458 | /* Pick tags from next element */ | ||
1459 | if (tag_bit) | ||
1460 | iter->tags |= node->tags[tag][tag_long + 1] << | ||
1461 | (BITS_PER_LONG - tag_bit); | ||
1462 | /* Clip chunk size, here only BITS_PER_LONG tags */ | ||
1463 | iter->next_index = __radix_tree_iter_add(iter, BITS_PER_LONG); | ||
1464 | } | ||
1465 | } | ||
1466 | |||
1467 | #ifdef CONFIG_RADIX_TREE_MULTIORDER | ||
1468 | static void **skip_siblings(struct radix_tree_node **nodep, | ||
1469 | void **slot, struct radix_tree_iter *iter) | ||
1470 | { | ||
1471 | void *sib = node_to_entry(slot - 1); | ||
1472 | |||
1473 | while (iter->index < iter->next_index) { | ||
1474 | *nodep = rcu_dereference_raw(*slot); | ||
1475 | if (*nodep && *nodep != sib) | ||
1476 | return slot; | ||
1477 | slot++; | ||
1478 | iter->index = __radix_tree_iter_add(iter, 1); | ||
1479 | iter->tags >>= 1; | ||
1480 | } | ||
1481 | |||
1482 | *nodep = NULL; | ||
1483 | return NULL; | ||
1484 | } | ||
1485 | |||
1486 | void ** __radix_tree_next_slot(void **slot, struct radix_tree_iter *iter, | ||
1487 | unsigned flags) | ||
1488 | { | ||
1489 | unsigned tag = flags & RADIX_TREE_ITER_TAG_MASK; | ||
1490 | struct radix_tree_node *node = rcu_dereference_raw(*slot); | ||
1491 | |||
1492 | slot = skip_siblings(&node, slot, iter); | ||
1493 | |||
1494 | while (radix_tree_is_internal_node(node)) { | ||
1495 | unsigned offset; | ||
1496 | unsigned long next_index; | ||
1497 | |||
1498 | if (node == RADIX_TREE_RETRY) | ||
1499 | return slot; | ||
1500 | node = entry_to_node(node); | ||
1501 | iter->node = node; | ||
1502 | iter->shift = node->shift; | ||
1503 | |||
1504 | if (flags & RADIX_TREE_ITER_TAGGED) { | ||
1505 | offset = radix_tree_find_next_bit(node, tag, 0); | ||
1506 | if (offset == RADIX_TREE_MAP_SIZE) | ||
1507 | return NULL; | ||
1508 | slot = &node->slots[offset]; | ||
1509 | iter->index = __radix_tree_iter_add(iter, offset); | ||
1510 | set_iter_tags(iter, node, offset, tag); | ||
1511 | node = rcu_dereference_raw(*slot); | ||
1512 | } else { | ||
1513 | offset = 0; | ||
1514 | slot = &node->slots[0]; | ||
1515 | for (;;) { | ||
1516 | node = rcu_dereference_raw(*slot); | ||
1517 | if (node) | ||
1518 | break; | ||
1519 | slot++; | ||
1520 | offset++; | ||
1521 | if (offset == RADIX_TREE_MAP_SIZE) | ||
1522 | return NULL; | ||
1523 | } | ||
1524 | iter->index = __radix_tree_iter_add(iter, offset); | ||
1525 | } | ||
1526 | if ((flags & RADIX_TREE_ITER_CONTIG) && (offset > 0)) | ||
1527 | goto none; | ||
1528 | next_index = (iter->index | shift_maxindex(iter->shift)) + 1; | ||
1529 | if (next_index < iter->next_index) | ||
1530 | iter->next_index = next_index; | ||
1531 | } | ||
1532 | |||
1533 | return slot; | ||
1534 | none: | ||
1535 | iter->next_index = 0; | ||
1536 | return NULL; | ||
1537 | } | ||
1538 | EXPORT_SYMBOL(__radix_tree_next_slot); | ||
1539 | #else | ||
1540 | static void **skip_siblings(struct radix_tree_node **nodep, | ||
1541 | void **slot, struct radix_tree_iter *iter) | ||
1542 | { | ||
1543 | return slot; | ||
1544 | } | ||
1545 | #endif | ||
1546 | |||
1547 | void **radix_tree_iter_resume(void **slot, struct radix_tree_iter *iter) | ||
1548 | { | ||
1549 | struct radix_tree_node *node; | ||
1550 | |||
1551 | slot++; | ||
1552 | iter->index = __radix_tree_iter_add(iter, 1); | ||
1553 | node = rcu_dereference_raw(*slot); | ||
1554 | skip_siblings(&node, slot, iter); | ||
1555 | iter->next_index = iter->index; | ||
1556 | iter->tags = 0; | ||
1557 | return NULL; | ||
1558 | } | ||
1559 | EXPORT_SYMBOL(radix_tree_iter_resume); | ||
1560 | |||
1088 | /** | 1561 | /** |
1089 | * radix_tree_next_chunk - find next chunk of slots for iteration | 1562 | * radix_tree_next_chunk - find next chunk of slots for iteration |
1090 | * | 1563 | * |
@@ -1110,7 +1583,7 @@ void **radix_tree_next_chunk(struct radix_tree_root *root, | |||
1110 | * because RADIX_TREE_MAP_SHIFT < BITS_PER_LONG. | 1583 | * because RADIX_TREE_MAP_SHIFT < BITS_PER_LONG. |
1111 | * | 1584 | * |
1112 | * This condition also used by radix_tree_next_slot() to stop | 1585 | * This condition also used by radix_tree_next_slot() to stop |
1113 | * contiguous iterating, and forbid swithing to the next chunk. | 1586 | * contiguous iterating, and forbid switching to the next chunk. |
1114 | */ | 1587 | */ |
1115 | index = iter->next_index; | 1588 | index = iter->next_index; |
1116 | if (!index && iter->index) | 1589 | if (!index && iter->index) |
@@ -1128,6 +1601,7 @@ void **radix_tree_next_chunk(struct radix_tree_root *root, | |||
1128 | iter->index = index; | 1601 | iter->index = index; |
1129 | iter->next_index = maxindex + 1; | 1602 | iter->next_index = maxindex + 1; |
1130 | iter->tags = 1; | 1603 | iter->tags = 1; |
1604 | iter->node = NULL; | ||
1131 | __set_iter_shift(iter, 0); | 1605 | __set_iter_shift(iter, 0); |
1132 | return (void **)&root->rnode; | 1606 | return (void **)&root->rnode; |
1133 | } | 1607 | } |
@@ -1143,9 +1617,7 @@ void **radix_tree_next_chunk(struct radix_tree_root *root, | |||
1143 | return NULL; | 1617 | return NULL; |
1144 | 1618 | ||
1145 | if (flags & RADIX_TREE_ITER_TAGGED) | 1619 | if (flags & RADIX_TREE_ITER_TAGGED) |
1146 | offset = radix_tree_find_next_bit( | 1620 | offset = radix_tree_find_next_bit(node, tag, |
1147 | node->tags[tag], | ||
1148 | RADIX_TREE_MAP_SIZE, | ||
1149 | offset + 1); | 1621 | offset + 1); |
1150 | else | 1622 | else |
1151 | while (++offset < RADIX_TREE_MAP_SIZE) { | 1623 | while (++offset < RADIX_TREE_MAP_SIZE) { |
@@ -1165,154 +1637,26 @@ void **radix_tree_next_chunk(struct radix_tree_root *root, | |||
1165 | child = rcu_dereference_raw(node->slots[offset]); | 1637 | child = rcu_dereference_raw(node->slots[offset]); |
1166 | } | 1638 | } |
1167 | 1639 | ||
1168 | if ((child == NULL) || (child == RADIX_TREE_RETRY)) | 1640 | if (!child) |
1169 | goto restart; | 1641 | goto restart; |
1642 | if (child == RADIX_TREE_RETRY) | ||
1643 | break; | ||
1170 | } while (radix_tree_is_internal_node(child)); | 1644 | } while (radix_tree_is_internal_node(child)); |
1171 | 1645 | ||
1172 | /* Update the iterator state */ | 1646 | /* Update the iterator state */ |
1173 | iter->index = (index &~ node_maxindex(node)) | (offset << node->shift); | 1647 | iter->index = (index &~ node_maxindex(node)) | (offset << node->shift); |
1174 | iter->next_index = (index | node_maxindex(node)) + 1; | 1648 | iter->next_index = (index | node_maxindex(node)) + 1; |
1649 | iter->node = node; | ||
1175 | __set_iter_shift(iter, node->shift); | 1650 | __set_iter_shift(iter, node->shift); |
1176 | 1651 | ||
1177 | /* Construct iter->tags bit-mask from node->tags[tag] array */ | 1652 | if (flags & RADIX_TREE_ITER_TAGGED) |
1178 | if (flags & RADIX_TREE_ITER_TAGGED) { | 1653 | set_iter_tags(iter, node, offset, tag); |
1179 | unsigned tag_long, tag_bit; | ||
1180 | |||
1181 | tag_long = offset / BITS_PER_LONG; | ||
1182 | tag_bit = offset % BITS_PER_LONG; | ||
1183 | iter->tags = node->tags[tag][tag_long] >> tag_bit; | ||
1184 | /* This never happens if RADIX_TREE_TAG_LONGS == 1 */ | ||
1185 | if (tag_long < RADIX_TREE_TAG_LONGS - 1) { | ||
1186 | /* Pick tags from next element */ | ||
1187 | if (tag_bit) | ||
1188 | iter->tags |= node->tags[tag][tag_long + 1] << | ||
1189 | (BITS_PER_LONG - tag_bit); | ||
1190 | /* Clip chunk size, here only BITS_PER_LONG tags */ | ||
1191 | iter->next_index = index + BITS_PER_LONG; | ||
1192 | } | ||
1193 | } | ||
1194 | 1654 | ||
1195 | return node->slots + offset; | 1655 | return node->slots + offset; |
1196 | } | 1656 | } |
1197 | EXPORT_SYMBOL(radix_tree_next_chunk); | 1657 | EXPORT_SYMBOL(radix_tree_next_chunk); |
1198 | 1658 | ||
1199 | /** | 1659 | /** |
1200 | * radix_tree_range_tag_if_tagged - for each item in given range set given | ||
1201 | * tag if item has another tag set | ||
1202 | * @root: radix tree root | ||
1203 | * @first_indexp: pointer to a starting index of a range to scan | ||
1204 | * @last_index: last index of a range to scan | ||
1205 | * @nr_to_tag: maximum number items to tag | ||
1206 | * @iftag: tag index to test | ||
1207 | * @settag: tag index to set if tested tag is set | ||
1208 | * | ||
1209 | * This function scans range of radix tree from first_index to last_index | ||
1210 | * (inclusive). For each item in the range if iftag is set, the function sets | ||
1211 | * also settag. The function stops either after tagging nr_to_tag items or | ||
1212 | * after reaching last_index. | ||
1213 | * | ||
1214 | * The tags must be set from the leaf level only and propagated back up the | ||
1215 | * path to the root. We must do this so that we resolve the full path before | ||
1216 | * setting any tags on intermediate nodes. If we set tags as we descend, then | ||
1217 | * we can get to the leaf node and find that the index that has the iftag | ||
1218 | * set is outside the range we are scanning. This reults in dangling tags and | ||
1219 | * can lead to problems with later tag operations (e.g. livelocks on lookups). | ||
1220 | * | ||
1221 | * The function returns the number of leaves where the tag was set and sets | ||
1222 | * *first_indexp to the first unscanned index. | ||
1223 | * WARNING! *first_indexp can wrap if last_index is ULONG_MAX. Caller must | ||
1224 | * be prepared to handle that. | ||
1225 | */ | ||
1226 | unsigned long radix_tree_range_tag_if_tagged(struct radix_tree_root *root, | ||
1227 | unsigned long *first_indexp, unsigned long last_index, | ||
1228 | unsigned long nr_to_tag, | ||
1229 | unsigned int iftag, unsigned int settag) | ||
1230 | { | ||
1231 | struct radix_tree_node *parent, *node, *child; | ||
1232 | unsigned long maxindex; | ||
1233 | unsigned long tagged = 0; | ||
1234 | unsigned long index = *first_indexp; | ||
1235 | |||
1236 | radix_tree_load_root(root, &child, &maxindex); | ||
1237 | last_index = min(last_index, maxindex); | ||
1238 | if (index > last_index) | ||
1239 | return 0; | ||
1240 | if (!nr_to_tag) | ||
1241 | return 0; | ||
1242 | if (!root_tag_get(root, iftag)) { | ||
1243 | *first_indexp = last_index + 1; | ||
1244 | return 0; | ||
1245 | } | ||
1246 | if (!radix_tree_is_internal_node(child)) { | ||
1247 | *first_indexp = last_index + 1; | ||
1248 | root_tag_set(root, settag); | ||
1249 | return 1; | ||
1250 | } | ||
1251 | |||
1252 | node = entry_to_node(child); | ||
1253 | |||
1254 | for (;;) { | ||
1255 | unsigned offset = radix_tree_descend(node, &child, index); | ||
1256 | if (!child) | ||
1257 | goto next; | ||
1258 | if (!tag_get(node, iftag, offset)) | ||
1259 | goto next; | ||
1260 | /* Sibling slots never have tags set on them */ | ||
1261 | if (radix_tree_is_internal_node(child)) { | ||
1262 | node = entry_to_node(child); | ||
1263 | continue; | ||
1264 | } | ||
1265 | |||
1266 | /* tag the leaf */ | ||
1267 | tagged++; | ||
1268 | tag_set(node, settag, offset); | ||
1269 | |||
1270 | /* walk back up the path tagging interior nodes */ | ||
1271 | parent = node; | ||
1272 | for (;;) { | ||
1273 | offset = parent->offset; | ||
1274 | parent = parent->parent; | ||
1275 | if (!parent) | ||
1276 | break; | ||
1277 | /* stop if we find a node with the tag already set */ | ||
1278 | if (tag_get(parent, settag, offset)) | ||
1279 | break; | ||
1280 | tag_set(parent, settag, offset); | ||
1281 | } | ||
1282 | next: | ||
1283 | /* Go to next entry in node */ | ||
1284 | index = ((index >> node->shift) + 1) << node->shift; | ||
1285 | /* Overflow can happen when last_index is ~0UL... */ | ||
1286 | if (index > last_index || !index) | ||
1287 | break; | ||
1288 | offset = (index >> node->shift) & RADIX_TREE_MAP_MASK; | ||
1289 | while (offset == 0) { | ||
1290 | /* | ||
1291 | * We've fully scanned this node. Go up. Because | ||
1292 | * last_index is guaranteed to be in the tree, what | ||
1293 | * we do below cannot wander astray. | ||
1294 | */ | ||
1295 | node = node->parent; | ||
1296 | offset = (index >> node->shift) & RADIX_TREE_MAP_MASK; | ||
1297 | } | ||
1298 | if (is_sibling_entry(node, node->slots[offset])) | ||
1299 | goto next; | ||
1300 | if (tagged >= nr_to_tag) | ||
1301 | break; | ||
1302 | } | ||
1303 | /* | ||
1304 | * We need not to tag the root tag if there is no tag which is set with | ||
1305 | * settag within the range from *first_indexp to last_index. | ||
1306 | */ | ||
1307 | if (tagged > 0) | ||
1308 | root_tag_set(root, settag); | ||
1309 | *first_indexp = index; | ||
1310 | |||
1311 | return tagged; | ||
1312 | } | ||
1313 | EXPORT_SYMBOL(radix_tree_range_tag_if_tagged); | ||
1314 | |||
1315 | /** | ||
1316 | * radix_tree_gang_lookup - perform multiple lookup on a radix tree | 1660 | * radix_tree_gang_lookup - perform multiple lookup on a radix tree |
1317 | * @root: radix tree root | 1661 | * @root: radix tree root |
1318 | * @results: where the results of the lookup are placed | 1662 | * @results: where the results of the lookup are placed |
@@ -1477,105 +1821,6 @@ radix_tree_gang_lookup_tag_slot(struct radix_tree_root *root, void ***results, | |||
1477 | } | 1821 | } |
1478 | EXPORT_SYMBOL(radix_tree_gang_lookup_tag_slot); | 1822 | EXPORT_SYMBOL(radix_tree_gang_lookup_tag_slot); |
1479 | 1823 | ||
1480 | #if defined(CONFIG_SHMEM) && defined(CONFIG_SWAP) | ||
1481 | #include <linux/sched.h> /* for cond_resched() */ | ||
1482 | |||
1483 | struct locate_info { | ||
1484 | unsigned long found_index; | ||
1485 | bool stop; | ||
1486 | }; | ||
1487 | |||
1488 | /* | ||
1489 | * This linear search is at present only useful to shmem_unuse_inode(). | ||
1490 | */ | ||
1491 | static unsigned long __locate(struct radix_tree_node *slot, void *item, | ||
1492 | unsigned long index, struct locate_info *info) | ||
1493 | { | ||
1494 | unsigned long i; | ||
1495 | |||
1496 | do { | ||
1497 | unsigned int shift = slot->shift; | ||
1498 | |||
1499 | for (i = (index >> shift) & RADIX_TREE_MAP_MASK; | ||
1500 | i < RADIX_TREE_MAP_SIZE; | ||
1501 | i++, index += (1UL << shift)) { | ||
1502 | struct radix_tree_node *node = | ||
1503 | rcu_dereference_raw(slot->slots[i]); | ||
1504 | if (node == RADIX_TREE_RETRY) | ||
1505 | goto out; | ||
1506 | if (!radix_tree_is_internal_node(node)) { | ||
1507 | if (node == item) { | ||
1508 | info->found_index = index; | ||
1509 | info->stop = true; | ||
1510 | goto out; | ||
1511 | } | ||
1512 | continue; | ||
1513 | } | ||
1514 | node = entry_to_node(node); | ||
1515 | if (is_sibling_entry(slot, node)) | ||
1516 | continue; | ||
1517 | slot = node; | ||
1518 | break; | ||
1519 | } | ||
1520 | } while (i < RADIX_TREE_MAP_SIZE); | ||
1521 | |||
1522 | out: | ||
1523 | if ((index == 0) && (i == RADIX_TREE_MAP_SIZE)) | ||
1524 | info->stop = true; | ||
1525 | return index; | ||
1526 | } | ||
1527 | |||
1528 | /** | ||
1529 | * radix_tree_locate_item - search through radix tree for item | ||
1530 | * @root: radix tree root | ||
1531 | * @item: item to be found | ||
1532 | * | ||
1533 | * Returns index where item was found, or -1 if not found. | ||
1534 | * Caller must hold no lock (since this time-consuming function needs | ||
1535 | * to be preemptible), and must check afterwards if item is still there. | ||
1536 | */ | ||
1537 | unsigned long radix_tree_locate_item(struct radix_tree_root *root, void *item) | ||
1538 | { | ||
1539 | struct radix_tree_node *node; | ||
1540 | unsigned long max_index; | ||
1541 | unsigned long cur_index = 0; | ||
1542 | struct locate_info info = { | ||
1543 | .found_index = -1, | ||
1544 | .stop = false, | ||
1545 | }; | ||
1546 | |||
1547 | do { | ||
1548 | rcu_read_lock(); | ||
1549 | node = rcu_dereference_raw(root->rnode); | ||
1550 | if (!radix_tree_is_internal_node(node)) { | ||
1551 | rcu_read_unlock(); | ||
1552 | if (node == item) | ||
1553 | info.found_index = 0; | ||
1554 | break; | ||
1555 | } | ||
1556 | |||
1557 | node = entry_to_node(node); | ||
1558 | |||
1559 | max_index = node_maxindex(node); | ||
1560 | if (cur_index > max_index) { | ||
1561 | rcu_read_unlock(); | ||
1562 | break; | ||
1563 | } | ||
1564 | |||
1565 | cur_index = __locate(node, item, cur_index, &info); | ||
1566 | rcu_read_unlock(); | ||
1567 | cond_resched(); | ||
1568 | } while (!info.stop && cur_index <= max_index); | ||
1569 | |||
1570 | return info.found_index; | ||
1571 | } | ||
1572 | #else | ||
1573 | unsigned long radix_tree_locate_item(struct radix_tree_root *root, void *item) | ||
1574 | { | ||
1575 | return -1; | ||
1576 | } | ||
1577 | #endif /* CONFIG_SHMEM && CONFIG_SWAP */ | ||
1578 | |||
1579 | /** | 1824 | /** |
1580 | * __radix_tree_delete_node - try to free node after clearing a slot | 1825 | * __radix_tree_delete_node - try to free node after clearing a slot |
1581 | * @root: radix tree root | 1826 | * @root: radix tree root |
@@ -1591,20 +1836,6 @@ void __radix_tree_delete_node(struct radix_tree_root *root, | |||
1591 | delete_node(root, node, NULL, NULL); | 1836 | delete_node(root, node, NULL, NULL); |
1592 | } | 1837 | } |
1593 | 1838 | ||
1594 | static inline void delete_sibling_entries(struct radix_tree_node *node, | ||
1595 | void *ptr, unsigned offset) | ||
1596 | { | ||
1597 | #ifdef CONFIG_RADIX_TREE_MULTIORDER | ||
1598 | int i; | ||
1599 | for (i = 1; offset + i < RADIX_TREE_MAP_SIZE; i++) { | ||
1600 | if (node->slots[offset + i] != ptr) | ||
1601 | break; | ||
1602 | node->slots[offset + i] = NULL; | ||
1603 | node->count--; | ||
1604 | } | ||
1605 | #endif | ||
1606 | } | ||
1607 | |||
1608 | /** | 1839 | /** |
1609 | * radix_tree_delete_item - delete an item from a radix tree | 1840 | * radix_tree_delete_item - delete an item from a radix tree |
1610 | * @root: radix tree root | 1841 | * @root: radix tree root |
@@ -1644,7 +1875,6 @@ void *radix_tree_delete_item(struct radix_tree_root *root, | |||
1644 | for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) | 1875 | for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) |
1645 | node_tag_clear(root, node, tag, offset); | 1876 | node_tag_clear(root, node, tag, offset); |
1646 | 1877 | ||
1647 | delete_sibling_entries(node, node_to_entry(slot), offset); | ||
1648 | __radix_tree_replace(root, node, slot, NULL, NULL, NULL); | 1878 | __radix_tree_replace(root, node, slot, NULL, NULL, NULL); |
1649 | 1879 | ||
1650 | return entry; | 1880 | return entry; |