Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

radix-tree: add support for multi-order iterating

This enables the macros radix_tree_for_each_slot() and friends to be
used with multi-order entries.

The way that this works is that we treat all entries in a given slots[]
array as a single chunk. If the index given to radix_tree_next_chunk()
happens to point us to a sibling entry, we will back up iter->index so
that it points to the canonical entry, and that will be the place where
we start our iteration.

As we're processing a chunk in radix_tree_next_slot(), we process
canonical entries, skip over sibling entries, and restart the chunk
lookup if we find a non-sibling indirect pointer. This drops back to
the radix_tree_next_chunk() code, which will re-walk the tree and look
for another chunk.

This allows us to properly handle multi-order entries mixed with other
entries that are at various heights in the radix tree.

Signed-off-by: Ross Zwisler <ross.zwisler@linux.intel.com>
Signed-off-by: Matthew Wilcox <willy@linux.intel.com>
Cc: Konstantin Khlebnikov <koct9i@gmail.com>
Cc: Kirill Shutemov <kirill.shutemov@linux.intel.com>
Cc: Jan Kara <jack@suse.com>
Cc: Neil Brown <neilb@suse.de>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

authored by

Ross Zwisler and committed by
Linus Torvalds
21ef5339 7b60e9ad

+102 -41
+60 -9
include/linux/radix-tree.h
··· 330 330 * struct radix_tree_iter - radix tree iterator state 331 331 * 332 332 * @index: index of current slot 333 - * @next_index: next-to-last index for this chunk 333 + * @next_index: one beyond the last index for this chunk 334 334 * @tags: bit-mask for tag-iterating 335 + * @shift: shift for the node that holds our slots 335 336 * 336 337 * This radix tree iterator works in terms of "chunks" of slots. A chunk is a 337 338 * subinterval of slots contained within one radix tree leaf node. It is ··· 345 344 unsigned long index; 346 345 unsigned long next_index; 347 346 unsigned long tags; 347 + #ifdef CONFIG_RADIX_TREE_MULTIORDER 348 + unsigned int shift; 349 + #endif 348 350 }; 351 + 352 + static inline unsigned int iter_shift(struct radix_tree_iter *iter) 353 + { 354 + #ifdef CONFIG_RADIX_TREE_MULTIORDER 355 + return iter->shift; 356 + #else 357 + return 0; 358 + #endif 359 + } 349 360 350 361 #define RADIX_TREE_ITER_TAG_MASK 0x00FF /* tag index in lower byte */ 351 362 #define RADIX_TREE_ITER_TAGGED 0x0100 /* lookup tagged slots */ ··· 418 405 return NULL; 419 406 } 420 407 408 + static inline unsigned long 409 + __radix_tree_iter_add(struct radix_tree_iter *iter, unsigned long slots) 410 + { 411 + return iter->index + (slots << iter_shift(iter)); 412 + } 413 + 421 414 /** 422 415 * radix_tree_iter_next - resume iterating when the chunk may be invalid 423 416 * @iter: iterator state ··· 435 416 static inline __must_check 436 417 void **radix_tree_iter_next(struct radix_tree_iter *iter) 437 418 { 438 - iter->next_index = iter->index + 1; 419 + iter->next_index = __radix_tree_iter_add(iter, 1); 439 420 iter->tags = 0; 440 421 return NULL; 441 422 } ··· 449 430 static __always_inline long 450 431 radix_tree_chunk_size(struct radix_tree_iter *iter) 451 432 { 452 - return iter->next_index - iter->index; 433 + return (iter->next_index - iter->index) >> iter_shift(iter); 434 + } 435 + 436 + static inline void *indirect_to_ptr(void *ptr) 437 + { 438 + return (void *)((unsigned long)ptr & ~RADIX_TREE_INDIRECT_PTR); 453 439 } 454 440 455 441 /** ··· 472 448 radix_tree_next_slot(void **slot, struct radix_tree_iter *iter, unsigned flags) 473 449 { 474 450 if (flags & RADIX_TREE_ITER_TAGGED) { 451 + void *canon = slot; 452 + 475 453 iter->tags >>= 1; 454 + if (unlikely(!iter->tags)) 455 + return NULL; 456 + while (IS_ENABLED(CONFIG_RADIX_TREE_MULTIORDER) && 457 + radix_tree_is_indirect_ptr(slot[1])) { 458 + if (indirect_to_ptr(slot[1]) == canon) { 459 + iter->tags >>= 1; 460 + iter->index = __radix_tree_iter_add(iter, 1); 461 + slot++; 462 + continue; 463 + } 464 + iter->next_index = __radix_tree_iter_add(iter, 1); 465 + return NULL; 466 + } 476 467 if (likely(iter->tags & 1ul)) { 477 - iter->index++; 468 + iter->index = __radix_tree_iter_add(iter, 1); 478 469 return slot + 1; 479 470 } 480 - if (!(flags & RADIX_TREE_ITER_CONTIG) && likely(iter->tags)) { 471 + if (!(flags & RADIX_TREE_ITER_CONTIG)) { 481 472 unsigned offset = __ffs(iter->tags); 482 473 483 474 iter->tags >>= offset; 484 - iter->index += offset + 1; 475 + iter->index = __radix_tree_iter_add(iter, offset + 1); 485 476 return slot + offset + 1; 486 477 } 487 478 } else { 488 - long size = radix_tree_chunk_size(iter); 479 + long count = radix_tree_chunk_size(iter); 480 + void *canon = slot; 489 481 490 - while (--size > 0) { 482 + while (--count > 0) { 491 483 slot++; 492 - iter->index++; 484 + iter->index = __radix_tree_iter_add(iter, 1); 485 + 486 + if (IS_ENABLED(CONFIG_RADIX_TREE_MULTIORDER) && 487 + radix_tree_is_indirect_ptr(*slot)) { 488 + if (indirect_to_ptr(*slot) == canon) 489 + continue; 490 + else { 491 + iter->next_index = iter->index; 492 + break; 493 + } 494 + } 495 + 493 496 if (likely(*slot)) 494 497 return slot; 495 498 if (flags & RADIX_TREE_ITER_CONTIG) {
+38 -28
lib/radix-tree.c
··· 75 75 return (void *)((unsigned long)ptr | RADIX_TREE_INDIRECT_PTR); 76 76 } 77 77 78 - static inline void *indirect_to_ptr(void *ptr) 79 - { 80 - return (void *)((unsigned long)ptr & ~RADIX_TREE_INDIRECT_PTR); 81 - } 82 - 83 78 #define RADIX_TREE_RETRY ptr_to_indirect(NULL) 84 79 85 80 #ifdef CONFIG_RADIX_TREE_MULTIORDER ··· 880 885 } 881 886 EXPORT_SYMBOL(radix_tree_tag_get); 882 887 888 + static inline void __set_iter_shift(struct radix_tree_iter *iter, 889 + unsigned int shift) 890 + { 891 + #ifdef CONFIG_RADIX_TREE_MULTIORDER 892 + iter->shift = shift; 893 + #endif 894 + } 895 + 883 896 /** 884 897 * radix_tree_next_chunk - find next chunk of slots for iteration 885 898 * ··· 901 898 { 902 899 unsigned shift, tag = flags & RADIX_TREE_ITER_TAG_MASK; 903 900 struct radix_tree_node *rnode, *node; 904 - unsigned long index, offset, height; 901 + unsigned long index, offset, maxindex; 905 902 906 903 if ((flags & RADIX_TREE_ITER_TAGGED) && !root_tag_get(root, tag)) 907 904 return NULL; ··· 919 916 if (!index && iter->index) 920 917 return NULL; 921 918 922 - rnode = rcu_dereference_raw(root->rnode); 919 + restart: 920 + shift = radix_tree_load_root(root, &rnode, &maxindex); 921 + if (index > maxindex) 922 + return NULL; 923 + 923 924 if (radix_tree_is_indirect_ptr(rnode)) { 924 925 rnode = indirect_to_ptr(rnode); 925 - } else if (rnode && !index) { 926 + } else if (rnode) { 926 927 /* Single-slot tree */ 927 - iter->index = 0; 928 - iter->next_index = 1; 928 + iter->index = index; 929 + iter->next_index = maxindex + 1; 929 930 iter->tags = 1; 931 + __set_iter_shift(iter, shift); 930 932 return (void **)&root->rnode; 931 933 } else 932 934 return NULL; 933 935 934 - restart: 935 - height = rnode->path & RADIX_TREE_HEIGHT_MASK; 936 - shift = (height - 1) * RADIX_TREE_MAP_SHIFT; 936 + shift -= RADIX_TREE_MAP_SHIFT; 937 937 offset = index >> shift; 938 - 939 - /* Index outside of the tree */ 940 - if (offset >= RADIX_TREE_MAP_SIZE) 941 - return NULL; 942 938 943 939 node = rnode; 944 940 while (1) { 945 941 struct radix_tree_node *slot; 942 + unsigned new_off = radix_tree_descend(node, &slot, offset); 943 + 944 + if (new_off < offset) { 945 + offset = new_off; 946 + index &= ~((RADIX_TREE_MAP_SIZE << shift) - 1); 947 + index |= offset << shift; 948 + } 949 + 946 950 if ((flags & RADIX_TREE_ITER_TAGGED) ? 947 - !test_bit(offset, node->tags[tag]) : 948 - !node->slots[offset]) { 951 + !tag_get(node, tag, offset) : !slot) { 949 952 /* Hole detected */ 950 953 if (flags & RADIX_TREE_ITER_CONTIG) 951 954 return NULL; ··· 963 954 offset + 1); 964 955 else 965 956 while (++offset < RADIX_TREE_MAP_SIZE) { 966 - if (node->slots[offset]) 957 + void *slot = node->slots[offset]; 958 + if (is_sibling_entry(node, slot)) 959 + continue; 960 + if (slot) 967 961 break; 968 962 } 969 963 index &= ~((RADIX_TREE_MAP_SIZE << shift) - 1); ··· 976 964 return NULL; 977 965 if (offset == RADIX_TREE_MAP_SIZE) 978 966 goto restart; 967 + slot = rcu_dereference_raw(node->slots[offset]); 979 968 } 980 969 981 - /* This is leaf-node */ 982 - if (!shift) 983 - break; 984 - 985 - slot = rcu_dereference_raw(node->slots[offset]); 986 - if (slot == NULL) 970 + if ((slot == NULL) || (slot == RADIX_TREE_RETRY)) 987 971 goto restart; 988 972 if (!radix_tree_is_indirect_ptr(slot)) 989 973 break; 974 + 990 975 node = indirect_to_ptr(slot); 991 976 shift -= RADIX_TREE_MAP_SHIFT; 992 977 offset = (index >> shift) & RADIX_TREE_MAP_MASK; 993 978 } 994 979 995 980 /* Update the iterator state */ 996 - iter->index = index; 997 - iter->next_index = (index | RADIX_TREE_MAP_MASK) + 1; 981 + iter->index = index & ~((1 << shift) - 1); 982 + iter->next_index = (index | ((RADIX_TREE_MAP_SIZE << shift) - 1)) + 1; 983 + __set_iter_shift(iter, shift); 998 984 999 985 /* Construct iter->tags bit-mask from node->tags[tag] array */ 1000 986 if (flags & RADIX_TREE_ITER_TAGGED) {
+3
tools/testing/radix-tree/generated/autoconf.h
··· 1 + #define CONFIG_RADIX_TREE_MULTIORDER 1 2 + #define CONFIG_SHMEM 1 3 + #define CONFIG_SWAP 1
+1 -4
tools/testing/radix-tree/linux/kernel.h
··· 8 8 #include <limits.h> 9 9 10 10 #include "../../include/linux/compiler.h" 11 - 12 - #define CONFIG_RADIX_TREE_MULTIORDER 13 - #define CONFIG_SHMEM 14 - #define CONFIG_SWAP 11 + #include "../../../include/linux/kconfig.h" 15 12 16 13 #define RADIX_TREE_MAP_SHIFT 3 17 14