2 * Copyright (C) 2001 Momchil Velikov
3 * Portions Copyright (C) 2001 Christoph Hellwig
4 * Copyright (C) 2005 SGI, Christoph Lameter
5 * Copyright (C) 2006 Nick Piggin
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2, or (at
10 * your option) any later version.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <linux/errno.h>
23 #include <linux/init.h>
24 #include <linux/kernel.h>
25 #include <linux/module.h>
26 #include <linux/radix-tree.h>
27 #include <linux/percpu.h>
28 #include <linux/slab.h>
29 #include <linux/notifier.h>
30 #include <linux/cpu.h>
31 #include <linux/string.h>
32 #include <linux/bitops.h>
33 #include <linux/rcupdate.h>
37 #define RADIX_TREE_MAP_SHIFT (CONFIG_BASE_SMALL ? 4 : 6)
39 #define RADIX_TREE_MAP_SHIFT 3 /* For more stressful testing */
42 #define RADIX_TREE_MAP_SIZE (1UL << RADIX_TREE_MAP_SHIFT)
43 #define RADIX_TREE_MAP_MASK (RADIX_TREE_MAP_SIZE-1)
45 #define RADIX_TREE_TAG_LONGS \
46 ((RADIX_TREE_MAP_SIZE + BITS_PER_LONG - 1) / BITS_PER_LONG)
48 struct radix_tree_node {
49 unsigned int height; /* Height from the bottom */
51 struct rcu_head rcu_head;
52 void *slots[RADIX_TREE_MAP_SIZE];
53 unsigned long tags[RADIX_TREE_MAX_TAGS][RADIX_TREE_TAG_LONGS];
56 struct radix_tree_path {
57 struct radix_tree_node *node;
61 #define RADIX_TREE_INDEX_BITS (8 /* CHAR_BIT */ * sizeof(unsigned long))
62 #define RADIX_TREE_MAX_PATH (DIV_ROUND_UP(RADIX_TREE_INDEX_BITS, \
63 RADIX_TREE_MAP_SHIFT))
66 * The height_to_maxindex array needs to be one deeper than the maximum
67 * path as height 0 holds only 1 entry.
69 static unsigned long height_to_maxindex[RADIX_TREE_MAX_PATH + 1] __read_mostly;
72 * Radix tree node cache.
74 static struct kmem_cache *radix_tree_node_cachep;
77 * Per-cpu pool of preloaded nodes
79 struct radix_tree_preload {
81 struct radix_tree_node *nodes[RADIX_TREE_MAX_PATH];
83 static DEFINE_PER_CPU(struct radix_tree_preload, radix_tree_preloads) = { 0, };
85 static inline void *ptr_to_indirect(void *ptr)
87 return (void *)((unsigned long)ptr | RADIX_TREE_INDIRECT_PTR);
90 static inline void *indirect_to_ptr(void *ptr)
92 return (void *)((unsigned long)ptr & ~RADIX_TREE_INDIRECT_PTR);
95 static inline gfp_t root_gfp_mask(struct radix_tree_root *root)
97 return root->gfp_mask & __GFP_BITS_MASK;
100 static inline void tag_set(struct radix_tree_node *node, unsigned int tag,
103 __set_bit(offset, node->tags[tag]);
106 static inline void tag_clear(struct radix_tree_node *node, unsigned int tag,
109 __clear_bit(offset, node->tags[tag]);
112 static inline int tag_get(struct radix_tree_node *node, unsigned int tag,
115 return test_bit(offset, node->tags[tag]);
118 static inline void root_tag_set(struct radix_tree_root *root, unsigned int tag)
120 root->gfp_mask |= (__force gfp_t)(1 << (tag + __GFP_BITS_SHIFT));
123 static inline void root_tag_clear(struct radix_tree_root *root, unsigned int tag)
125 root->gfp_mask &= (__force gfp_t)~(1 << (tag + __GFP_BITS_SHIFT));
128 static inline void root_tag_clear_all(struct radix_tree_root *root)
130 root->gfp_mask &= __GFP_BITS_MASK;
133 static inline int root_tag_get(struct radix_tree_root *root, unsigned int tag)
135 return (__force unsigned)root->gfp_mask & (1 << (tag + __GFP_BITS_SHIFT));
139 * Returns 1 if any slot in the node has this tag set.
140 * Otherwise returns 0.
142 static inline int any_tag_set(struct radix_tree_node *node, unsigned int tag)
145 for (idx = 0; idx < RADIX_TREE_TAG_LONGS; idx++) {
146 if (node->tags[tag][idx])
152 * This assumes that the caller has performed appropriate preallocation, and
153 * that the caller has pinned this thread of control to the current CPU.
155 static struct radix_tree_node *
156 radix_tree_node_alloc(struct radix_tree_root *root)
158 struct radix_tree_node *ret = NULL;
159 gfp_t gfp_mask = root_gfp_mask(root);
161 if (!(gfp_mask & __GFP_WAIT)) {
162 struct radix_tree_preload *rtp;
165 * Provided the caller has preloaded here, we will always
166 * succeed in getting a node here (and never reach
169 rtp = &__get_cpu_var(radix_tree_preloads);
171 ret = rtp->nodes[rtp->nr - 1];
172 rtp->nodes[rtp->nr - 1] = NULL;
177 ret = kmem_cache_alloc(radix_tree_node_cachep, gfp_mask);
179 BUG_ON(radix_tree_is_indirect_ptr(ret));
183 static void radix_tree_node_rcu_free(struct rcu_head *head)
185 struct radix_tree_node *node =
186 container_of(head, struct radix_tree_node, rcu_head);
189 * must only free zeroed nodes into the slab. radix_tree_shrink
190 * can leave us with a non-NULL entry in the first slot, so clear
191 * that here to make sure.
193 tag_clear(node, 0, 0);
194 tag_clear(node, 1, 0);
195 node->slots[0] = NULL;
198 kmem_cache_free(radix_tree_node_cachep, node);
202 radix_tree_node_free(struct radix_tree_node *node)
204 call_rcu(&node->rcu_head, radix_tree_node_rcu_free);
208 * Load up this CPU's radix_tree_node buffer with sufficient objects to
209 * ensure that the addition of a single element in the tree cannot fail. On
210 * success, return zero, with preemption disabled. On error, return -ENOMEM
211 * with preemption not disabled.
213 * To make use of this facility, the radix tree must be initialised without
214 * __GFP_WAIT being passed to INIT_RADIX_TREE().
216 int radix_tree_preload(gfp_t gfp_mask)
218 struct radix_tree_preload *rtp;
219 struct radix_tree_node *node;
223 rtp = &__get_cpu_var(radix_tree_preloads);
224 while (rtp->nr < ARRAY_SIZE(rtp->nodes)) {
226 node = kmem_cache_alloc(radix_tree_node_cachep, gfp_mask);
230 rtp = &__get_cpu_var(radix_tree_preloads);
231 if (rtp->nr < ARRAY_SIZE(rtp->nodes))
232 rtp->nodes[rtp->nr++] = node;
234 kmem_cache_free(radix_tree_node_cachep, node);
240 EXPORT_SYMBOL(radix_tree_preload);
243 * Return the maximum key which can be store into a
244 * radix tree with height HEIGHT.
246 static inline unsigned long radix_tree_maxindex(unsigned int height)
248 return height_to_maxindex[height];
252 * Extend a radix tree so it can store key @index.
254 static int radix_tree_extend(struct radix_tree_root *root, unsigned long index)
256 struct radix_tree_node *node;
260 /* Figure out what the height should be. */
261 height = root->height + 1;
262 while (index > radix_tree_maxindex(height))
265 if (root->rnode == NULL) {
266 root->height = height;
271 unsigned int newheight;
272 if (!(node = radix_tree_node_alloc(root)))
275 /* Increase the height. */
276 node->slots[0] = indirect_to_ptr(root->rnode);
278 /* Propagate the aggregated tag info into the new root */
279 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) {
280 if (root_tag_get(root, tag))
281 tag_set(node, tag, 0);
284 newheight = root->height+1;
285 node->height = newheight;
287 node = ptr_to_indirect(node);
288 rcu_assign_pointer(root->rnode, node);
289 root->height = newheight;
290 } while (height > root->height);
296 * radix_tree_insert - insert into a radix tree
297 * @root: radix tree root
299 * @item: item to insert
301 * Insert an item into the radix tree at position @index.
303 int radix_tree_insert(struct radix_tree_root *root,
304 unsigned long index, void *item)
306 struct radix_tree_node *node = NULL, *slot;
307 unsigned int height, shift;
311 BUG_ON(radix_tree_is_indirect_ptr(item));
313 /* Make sure the tree is high enough. */
314 if (index > radix_tree_maxindex(root->height)) {
315 error = radix_tree_extend(root, index);
320 slot = indirect_to_ptr(root->rnode);
322 height = root->height;
323 shift = (height-1) * RADIX_TREE_MAP_SHIFT;
325 offset = 0; /* uninitialised var warning */
328 /* Have to add a child node. */
329 if (!(slot = radix_tree_node_alloc(root)))
331 slot->height = height;
333 rcu_assign_pointer(node->slots[offset], slot);
336 rcu_assign_pointer(root->rnode, ptr_to_indirect(slot));
339 /* Go a level down */
340 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
342 slot = node->slots[offset];
343 shift -= RADIX_TREE_MAP_SHIFT;
352 rcu_assign_pointer(node->slots[offset], item);
353 BUG_ON(tag_get(node, 0, offset));
354 BUG_ON(tag_get(node, 1, offset));
356 rcu_assign_pointer(root->rnode, item);
357 BUG_ON(root_tag_get(root, 0));
358 BUG_ON(root_tag_get(root, 1));
363 EXPORT_SYMBOL(radix_tree_insert);
366 * is_slot == 1 : search for the slot.
367 * is_slot == 0 : search for the node.
369 static void *radix_tree_lookup_element(struct radix_tree_root *root,
370 unsigned long index, int is_slot)
372 unsigned int height, shift;
373 struct radix_tree_node *node, **slot;
375 node = rcu_dereference_raw(root->rnode);
379 if (!radix_tree_is_indirect_ptr(node)) {
382 return is_slot ? (void *)&root->rnode : node;
384 node = indirect_to_ptr(node);
386 height = node->height;
387 if (index > radix_tree_maxindex(height))
390 shift = (height-1) * RADIX_TREE_MAP_SHIFT;
393 slot = (struct radix_tree_node **)
394 (node->slots + ((index>>shift) & RADIX_TREE_MAP_MASK));
395 node = rcu_dereference_raw(*slot);
399 shift -= RADIX_TREE_MAP_SHIFT;
401 } while (height > 0);
403 return is_slot ? (void *)slot : indirect_to_ptr(node);
407 * radix_tree_lookup_slot - lookup a slot in a radix tree
408 * @root: radix tree root
411 * Returns: the slot corresponding to the position @index in the
412 * radix tree @root. This is useful for update-if-exists operations.
414 * This function can be called under rcu_read_lock iff the slot is not
415 * modified by radix_tree_replace_slot, otherwise it must be called
416 * exclusive from other writers. Any dereference of the slot must be done
417 * using radix_tree_deref_slot.
419 void **radix_tree_lookup_slot(struct radix_tree_root *root, unsigned long index)
421 return (void **)radix_tree_lookup_element(root, index, 1);
423 EXPORT_SYMBOL(radix_tree_lookup_slot);
426 * radix_tree_lookup - perform lookup operation on a radix tree
427 * @root: radix tree root
430 * Lookup the item at the position @index in the radix tree @root.
432 * This function can be called under rcu_read_lock, however the caller
433 * must manage lifetimes of leaf nodes (eg. RCU may also be used to free
434 * them safely). No RCU barriers are required to access or modify the
435 * returned item, however.
437 void *radix_tree_lookup(struct radix_tree_root *root, unsigned long index)
439 return radix_tree_lookup_element(root, index, 0);
441 EXPORT_SYMBOL(radix_tree_lookup);
444 * radix_tree_tag_set - set a tag on a radix tree node
445 * @root: radix tree root
449 * Set the search tag (which must be < RADIX_TREE_MAX_TAGS)
450 * corresponding to @index in the radix tree. From
451 * the root all the way down to the leaf node.
453 * Returns the address of the tagged item. Setting a tag on a not-present
456 void *radix_tree_tag_set(struct radix_tree_root *root,
457 unsigned long index, unsigned int tag)
459 unsigned int height, shift;
460 struct radix_tree_node *slot;
462 height = root->height;
463 BUG_ON(index > radix_tree_maxindex(height));
465 slot = indirect_to_ptr(root->rnode);
466 shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
471 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
472 if (!tag_get(slot, tag, offset))
473 tag_set(slot, tag, offset);
474 slot = slot->slots[offset];
475 BUG_ON(slot == NULL);
476 shift -= RADIX_TREE_MAP_SHIFT;
480 /* set the root's tag bit */
481 if (slot && !root_tag_get(root, tag))
482 root_tag_set(root, tag);
486 EXPORT_SYMBOL(radix_tree_tag_set);
489 * radix_tree_tag_clear - clear a tag on a radix tree node
490 * @root: radix tree root
494 * Clear the search tag (which must be < RADIX_TREE_MAX_TAGS)
495 * corresponding to @index in the radix tree. If
496 * this causes the leaf node to have no tags set then clear the tag in the
497 * next-to-leaf node, etc.
499 * Returns the address of the tagged item on success, else NULL. ie:
500 * has the same return value and semantics as radix_tree_lookup().
502 void *radix_tree_tag_clear(struct radix_tree_root *root,
503 unsigned long index, unsigned int tag)
506 * The radix tree path needs to be one longer than the maximum path
507 * since the "list" is null terminated.
509 struct radix_tree_path path[RADIX_TREE_MAX_PATH + 1], *pathp = path;
510 struct radix_tree_node *slot = NULL;
511 unsigned int height, shift;
513 height = root->height;
514 if (index > radix_tree_maxindex(height))
517 shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
519 slot = indirect_to_ptr(root->rnode);
527 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
528 pathp[1].offset = offset;
529 pathp[1].node = slot;
530 slot = slot->slots[offset];
532 shift -= RADIX_TREE_MAP_SHIFT;
539 while (pathp->node) {
540 if (!tag_get(pathp->node, tag, pathp->offset))
542 tag_clear(pathp->node, tag, pathp->offset);
543 if (any_tag_set(pathp->node, tag))
548 /* clear the root's tag bit */
549 if (root_tag_get(root, tag))
550 root_tag_clear(root, tag);
555 EXPORT_SYMBOL(radix_tree_tag_clear);
558 * radix_tree_tag_get - get a tag on a radix tree node
559 * @root: radix tree root
561 * @tag: tag index (< RADIX_TREE_MAX_TAGS)
565 * 0: tag not present or not set
568 * Note that the return value of this function may not be relied on, even if
569 * the RCU lock is held, unless tag modification and node deletion are excluded
572 int radix_tree_tag_get(struct radix_tree_root *root,
573 unsigned long index, unsigned int tag)
575 unsigned int height, shift;
576 struct radix_tree_node *node;
577 int saw_unset_tag = 0;
579 /* check the root's tag bit */
580 if (!root_tag_get(root, tag))
583 node = rcu_dereference_raw(root->rnode);
587 if (!radix_tree_is_indirect_ptr(node))
589 node = indirect_to_ptr(node);
591 height = node->height;
592 if (index > radix_tree_maxindex(height))
595 shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
603 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
606 * This is just a debug check. Later, we can bale as soon as
607 * we see an unset tag.
609 if (!tag_get(node, tag, offset))
612 return !!tag_get(node, tag, offset);
613 node = rcu_dereference_raw(node->slots[offset]);
614 shift -= RADIX_TREE_MAP_SHIFT;
618 EXPORT_SYMBOL(radix_tree_tag_get);
621 * radix_tree_next_hole - find the next hole (not-present entry)
624 * @max_scan: maximum range to search
626 * Search the set [index, min(index+max_scan-1, MAX_INDEX)] for the lowest
629 * Returns: the index of the hole if found, otherwise returns an index
630 * outside of the set specified (in which case 'return - index >= max_scan'
631 * will be true). In rare cases of index wrap-around, 0 will be returned.
633 * radix_tree_next_hole may be called under rcu_read_lock. However, like
634 * radix_tree_gang_lookup, this will not atomically search a snapshot of
635 * the tree at a single point in time. For example, if a hole is created
636 * at index 5, then subsequently a hole is created at index 10,
637 * radix_tree_next_hole covering both indexes may return 10 if called
638 * under rcu_read_lock.
640 unsigned long radix_tree_next_hole(struct radix_tree_root *root,
641 unsigned long index, unsigned long max_scan)
645 for (i = 0; i < max_scan; i++) {
646 if (!radix_tree_lookup(root, index))
655 EXPORT_SYMBOL(radix_tree_next_hole);
658 * radix_tree_prev_hole - find the prev hole (not-present entry)
661 * @max_scan: maximum range to search
663 * Search backwards in the range [max(index-max_scan+1, 0), index]
664 * for the first hole.
666 * Returns: the index of the hole if found, otherwise returns an index
667 * outside of the set specified (in which case 'index - return >= max_scan'
668 * will be true). In rare cases of wrap-around, ULONG_MAX will be returned.
670 * radix_tree_next_hole may be called under rcu_read_lock. However, like
671 * radix_tree_gang_lookup, this will not atomically search a snapshot of
672 * the tree at a single point in time. For example, if a hole is created
673 * at index 10, then subsequently a hole is created at index 5,
674 * radix_tree_prev_hole covering both indexes may return 5 if called under
677 unsigned long radix_tree_prev_hole(struct radix_tree_root *root,
678 unsigned long index, unsigned long max_scan)
682 for (i = 0; i < max_scan; i++) {
683 if (!radix_tree_lookup(root, index))
686 if (index == ULONG_MAX)
692 EXPORT_SYMBOL(radix_tree_prev_hole);
695 __lookup(struct radix_tree_node *slot, void ***results, unsigned long index,
696 unsigned int max_items, unsigned long *next_index)
698 unsigned int nr_found = 0;
699 unsigned int shift, height;
702 height = slot->height;
705 shift = (height-1) * RADIX_TREE_MAP_SHIFT;
707 for ( ; height > 1; height--) {
708 i = (index >> shift) & RADIX_TREE_MAP_MASK;
710 if (slot->slots[i] != NULL)
712 index &= ~((1UL << shift) - 1);
713 index += 1UL << shift;
715 goto out; /* 32-bit wraparound */
717 if (i == RADIX_TREE_MAP_SIZE)
721 shift -= RADIX_TREE_MAP_SHIFT;
722 slot = rcu_dereference_raw(slot->slots[i]);
727 /* Bottom level: grab some items */
728 for (i = index & RADIX_TREE_MAP_MASK; i < RADIX_TREE_MAP_SIZE; i++) {
730 if (slot->slots[i]) {
731 results[nr_found++] = &(slot->slots[i]);
732 if (nr_found == max_items)
742 * radix_tree_gang_lookup - perform multiple lookup on a radix tree
743 * @root: radix tree root
744 * @results: where the results of the lookup are placed
745 * @first_index: start the lookup from this key
746 * @max_items: place up to this many items at *results
748 * Performs an index-ascending scan of the tree for present items. Places
749 * them at *@results and returns the number of items which were placed at
752 * The implementation is naive.
754 * Like radix_tree_lookup, radix_tree_gang_lookup may be called under
755 * rcu_read_lock. In this case, rather than the returned results being
756 * an atomic snapshot of the tree at a single point in time, the semantics
757 * of an RCU protected gang lookup are as though multiple radix_tree_lookups
758 * have been issued in individual locks, and results stored in 'results'.
761 radix_tree_gang_lookup(struct radix_tree_root *root, void **results,
762 unsigned long first_index, unsigned int max_items)
764 unsigned long max_index;
765 struct radix_tree_node *node;
766 unsigned long cur_index = first_index;
769 node = rcu_dereference_raw(root->rnode);
773 if (!radix_tree_is_indirect_ptr(node)) {
779 node = indirect_to_ptr(node);
781 max_index = radix_tree_maxindex(node->height);
784 while (ret < max_items) {
785 unsigned int nr_found, slots_found, i;
786 unsigned long next_index; /* Index of next search */
788 if (cur_index > max_index)
790 slots_found = __lookup(node, (void ***)results + ret, cur_index,
791 max_items - ret, &next_index);
793 for (i = 0; i < slots_found; i++) {
794 struct radix_tree_node *slot;
795 slot = *(((void ***)results)[ret + i]);
798 results[ret + nr_found] = rcu_dereference_raw(slot);
804 cur_index = next_index;
809 EXPORT_SYMBOL(radix_tree_gang_lookup);
812 * radix_tree_gang_lookup_slot - perform multiple slot lookup on radix tree
813 * @root: radix tree root
814 * @results: where the results of the lookup are placed
815 * @first_index: start the lookup from this key
816 * @max_items: place up to this many items at *results
818 * Performs an index-ascending scan of the tree for present items. Places
819 * their slots at *@results and returns the number of items which were
820 * placed at *@results.
822 * The implementation is naive.
824 * Like radix_tree_gang_lookup as far as RCU and locking goes. Slots must
825 * be dereferenced with radix_tree_deref_slot, and if using only RCU
826 * protection, radix_tree_deref_slot may fail requiring a retry.
829 radix_tree_gang_lookup_slot(struct radix_tree_root *root, void ***results,
830 unsigned long first_index, unsigned int max_items)
832 unsigned long max_index;
833 struct radix_tree_node *node;
834 unsigned long cur_index = first_index;
837 node = rcu_dereference_raw(root->rnode);
841 if (!radix_tree_is_indirect_ptr(node)) {
844 results[0] = (void **)&root->rnode;
847 node = indirect_to_ptr(node);
849 max_index = radix_tree_maxindex(node->height);
852 while (ret < max_items) {
853 unsigned int slots_found;
854 unsigned long next_index; /* Index of next search */
856 if (cur_index > max_index)
858 slots_found = __lookup(node, results + ret, cur_index,
859 max_items - ret, &next_index);
863 cur_index = next_index;
868 EXPORT_SYMBOL(radix_tree_gang_lookup_slot);
871 * FIXME: the two tag_get()s here should use find_next_bit() instead of
872 * open-coding the search.
875 __lookup_tag(struct radix_tree_node *slot, void ***results, unsigned long index,
876 unsigned int max_items, unsigned long *next_index, unsigned int tag)
878 unsigned int nr_found = 0;
879 unsigned int shift, height;
881 height = slot->height;
884 shift = (height-1) * RADIX_TREE_MAP_SHIFT;
887 unsigned long i = (index >> shift) & RADIX_TREE_MAP_MASK ;
890 if (tag_get(slot, tag, i))
892 index &= ~((1UL << shift) - 1);
893 index += 1UL << shift;
895 goto out; /* 32-bit wraparound */
897 if (i == RADIX_TREE_MAP_SIZE)
901 if (height == 0) { /* Bottom level: grab some items */
902 unsigned long j = index & RADIX_TREE_MAP_MASK;
904 for ( ; j < RADIX_TREE_MAP_SIZE; j++) {
906 if (!tag_get(slot, tag, j))
909 * Even though the tag was found set, we need to
910 * recheck that we have a non-NULL node, because
911 * if this lookup is lockless, it may have been
912 * subsequently deleted.
914 * Similar care must be taken in any place that
915 * lookup ->slots[x] without a lock (ie. can't
916 * rely on its value remaining the same).
918 if (slot->slots[j]) {
919 results[nr_found++] = &(slot->slots[j]);
920 if (nr_found == max_items)
925 shift -= RADIX_TREE_MAP_SHIFT;
926 slot = rcu_dereference_raw(slot->slots[i]);
936 * radix_tree_gang_lookup_tag - perform multiple lookup on a radix tree
938 * @root: radix tree root
939 * @results: where the results of the lookup are placed
940 * @first_index: start the lookup from this key
941 * @max_items: place up to this many items at *results
942 * @tag: the tag index (< RADIX_TREE_MAX_TAGS)
944 * Performs an index-ascending scan of the tree for present items which
945 * have the tag indexed by @tag set. Places the items at *@results and
946 * returns the number of items which were placed at *@results.
949 radix_tree_gang_lookup_tag(struct radix_tree_root *root, void **results,
950 unsigned long first_index, unsigned int max_items,
953 struct radix_tree_node *node;
954 unsigned long max_index;
955 unsigned long cur_index = first_index;
958 /* check the root's tag bit */
959 if (!root_tag_get(root, tag))
962 node = rcu_dereference_raw(root->rnode);
966 if (!radix_tree_is_indirect_ptr(node)) {
972 node = indirect_to_ptr(node);
974 max_index = radix_tree_maxindex(node->height);
977 while (ret < max_items) {
978 unsigned int nr_found, slots_found, i;
979 unsigned long next_index; /* Index of next search */
981 if (cur_index > max_index)
983 slots_found = __lookup_tag(node, (void ***)results + ret,
984 cur_index, max_items - ret, &next_index, tag);
986 for (i = 0; i < slots_found; i++) {
987 struct radix_tree_node *slot;
988 slot = *(((void ***)results)[ret + i]);
991 results[ret + nr_found] =
992 indirect_to_ptr(rcu_dereference_raw(slot));
998 cur_index = next_index;
1003 EXPORT_SYMBOL(radix_tree_gang_lookup_tag);
1006 * radix_tree_gang_lookup_tag_slot - perform multiple slot lookup on a
1007 * radix tree based on a tag
1008 * @root: radix tree root
1009 * @results: where the results of the lookup are placed
1010 * @first_index: start the lookup from this key
1011 * @max_items: place up to this many items at *results
1012 * @tag: the tag index (< RADIX_TREE_MAX_TAGS)
1014 * Performs an index-ascending scan of the tree for present items which
1015 * have the tag indexed by @tag set. Places the slots at *@results and
1016 * returns the number of slots which were placed at *@results.
1019 radix_tree_gang_lookup_tag_slot(struct radix_tree_root *root, void ***results,
1020 unsigned long first_index, unsigned int max_items,
1023 struct radix_tree_node *node;
1024 unsigned long max_index;
1025 unsigned long cur_index = first_index;
1028 /* check the root's tag bit */
1029 if (!root_tag_get(root, tag))
1032 node = rcu_dereference_raw(root->rnode);
1036 if (!radix_tree_is_indirect_ptr(node)) {
1037 if (first_index > 0)
1039 results[0] = (void **)&root->rnode;
1042 node = indirect_to_ptr(node);
1044 max_index = radix_tree_maxindex(node->height);
1047 while (ret < max_items) {
1048 unsigned int slots_found;
1049 unsigned long next_index; /* Index of next search */
1051 if (cur_index > max_index)
1053 slots_found = __lookup_tag(node, results + ret,
1054 cur_index, max_items - ret, &next_index, tag);
1056 if (next_index == 0)
1058 cur_index = next_index;
1063 EXPORT_SYMBOL(radix_tree_gang_lookup_tag_slot);
1067 * radix_tree_shrink - shrink height of a radix tree to minimal
1068 * @root radix tree root
1070 static inline void radix_tree_shrink(struct radix_tree_root *root)
1072 /* try to shrink tree height */
1073 while (root->height > 0) {
1074 struct radix_tree_node *to_free = root->rnode;
1077 BUG_ON(!radix_tree_is_indirect_ptr(to_free));
1078 to_free = indirect_to_ptr(to_free);
1081 * The candidate node has more than one child, or its child
1082 * is not at the leftmost slot, we cannot shrink.
1084 if (to_free->count != 1)
1086 if (!to_free->slots[0])
1090 * We don't need rcu_assign_pointer(), since we are simply
1091 * moving the node from one part of the tree to another: if it
1092 * was safe to dereference the old pointer to it
1093 * (to_free->slots[0]), it will be safe to dereference the new
1094 * one (root->rnode) as far as dependent read barriers go.
1096 newptr = to_free->slots[0];
1097 if (root->height > 1)
1098 newptr = ptr_to_indirect(newptr);
1099 root->rnode = newptr;
1103 * We have a dilemma here. The node's slot[0] must not be
1104 * NULLed in case there are concurrent lookups expecting to
1105 * find the item. However if this was a bottom-level node,
1106 * then it may be subject to the slot pointer being visible
1107 * to callers dereferencing it. If item corresponding to
1108 * slot[0] is subsequently deleted, these callers would expect
1109 * their slot to become empty sooner or later.
1111 * For example, lockless pagecache will look up a slot, deref
1112 * the page pointer, and if the page is 0 refcount it means it
1113 * was concurrently deleted from pagecache so try the deref
1114 * again. Fortunately there is already a requirement for logic
1115 * to retry the entire slot lookup -- the indirect pointer
1116 * problem (replacing direct root node with an indirect pointer
1117 * also results in a stale slot). So tag the slot as indirect
1118 * to force callers to retry.
1120 if (root->height == 0)
1121 *((unsigned long *)&to_free->slots[0]) |=
1122 RADIX_TREE_INDIRECT_PTR;
1124 radix_tree_node_free(to_free);
1129 * radix_tree_delete - delete an item from a radix tree
1130 * @root: radix tree root
1133 * Remove the item at @index from the radix tree rooted at @root.
1135 * Returns the address of the deleted item, or NULL if it was not present.
1137 void *radix_tree_delete(struct radix_tree_root *root, unsigned long index)
1140 * The radix tree path needs to be one longer than the maximum path
1141 * since the "list" is null terminated.
1143 struct radix_tree_path path[RADIX_TREE_MAX_PATH + 1], *pathp = path;
1144 struct radix_tree_node *slot = NULL;
1145 struct radix_tree_node *to_free;
1146 unsigned int height, shift;
1150 height = root->height;
1151 if (index > radix_tree_maxindex(height))
1156 root_tag_clear_all(root);
1160 slot = indirect_to_ptr(slot);
1162 shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
1170 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
1171 pathp->offset = offset;
1173 slot = slot->slots[offset];
1174 shift -= RADIX_TREE_MAP_SHIFT;
1176 } while (height > 0);
1182 * Clear all tags associated with the just-deleted item
1184 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) {
1185 if (tag_get(pathp->node, tag, pathp->offset))
1186 radix_tree_tag_clear(root, index, tag);
1190 /* Now free the nodes we do not need anymore */
1191 while (pathp->node) {
1192 pathp->node->slots[pathp->offset] = NULL;
1193 pathp->node->count--;
1195 * Queue the node for deferred freeing after the
1196 * last reference to it disappears (set NULL, above).
1199 radix_tree_node_free(to_free);
1201 if (pathp->node->count) {
1202 if (pathp->node == indirect_to_ptr(root->rnode))
1203 radix_tree_shrink(root);
1207 /* Node with zero slots in use so free it */
1208 to_free = pathp->node;
1212 root_tag_clear_all(root);
1216 radix_tree_node_free(to_free);
1221 EXPORT_SYMBOL(radix_tree_delete);
1224 * radix_tree_tagged - test whether any items in the tree are tagged
1225 * @root: radix tree root
1228 int radix_tree_tagged(struct radix_tree_root *root, unsigned int tag)
1230 return root_tag_get(root, tag);
1232 EXPORT_SYMBOL(radix_tree_tagged);
1235 radix_tree_node_ctor(void *node)
1237 memset(node, 0, sizeof(struct radix_tree_node));
1240 static __init unsigned long __maxindex(unsigned int height)
1242 unsigned int width = height * RADIX_TREE_MAP_SHIFT;
1243 int shift = RADIX_TREE_INDEX_BITS - width;
1247 if (shift >= BITS_PER_LONG)
1249 return ~0UL >> shift;
1252 static __init void radix_tree_init_maxindex(void)
1256 for (i = 0; i < ARRAY_SIZE(height_to_maxindex); i++)
1257 height_to_maxindex[i] = __maxindex(i);
1260 static int radix_tree_callback(struct notifier_block *nfb,
1261 unsigned long action,
1264 int cpu = (long)hcpu;
1265 struct radix_tree_preload *rtp;
1267 /* Free per-cpu pool of perloaded nodes */
1268 if (action == CPU_DEAD || action == CPU_DEAD_FROZEN) {
1269 rtp = &per_cpu(radix_tree_preloads, cpu);
1271 kmem_cache_free(radix_tree_node_cachep,
1272 rtp->nodes[rtp->nr-1]);
1273 rtp->nodes[rtp->nr-1] = NULL;
1280 void __init radix_tree_init(void)
1282 radix_tree_node_cachep = kmem_cache_create("radix_tree_node",
1283 sizeof(struct radix_tree_node), 0,
1284 SLAB_PANIC | SLAB_RECLAIM_ACCOUNT,
1285 radix_tree_node_ctor);
1286 radix_tree_init_maxindex();
1287 hotcpu_notifier(radix_tree_callback, 0);