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
6 * Copyright (C) 2012 Konstantin Khlebnikov
7 * Copyright (C) 2016 Intel, Matthew Wilcox
8 * Copyright (C) 2016 Intel, Ross Zwisler
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2, or (at
13 * your option) any later version.
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include <linux/bitmap.h>
26 #include <linux/bitops.h>
27 #include <linux/bug.h>
28 #include <linux/cpu.h>
29 #include <linux/errno.h>
30 #include <linux/export.h>
31 #include <linux/idr.h>
32 #include <linux/init.h>
33 #include <linux/kernel.h>
34 #include <linux/kmemleak.h>
35 #include <linux/percpu.h>
36 #include <linux/preempt.h> /* in_interrupt() */
37 #include <linux/radix-tree.h>
38 #include <linux/rcupdate.h>
39 #include <linux/slab.h>
40 #include <linux/string.h>
41 #include <linux/xarray.h>
44 /* Number of nodes in fully populated tree of given height */
45 static unsigned long height_to_maxnodes[RADIX_TREE_MAX_PATH + 1] __read_mostly;
48 * Radix tree node cache.
50 struct kmem_cache *radix_tree_node_cachep;
53 * The radix tree is variable-height, so an insert operation not only has
54 * to build the branch to its corresponding item, it also has to build the
55 * branch to existing items if the size has to be increased (by
58 * The worst case is a zero height tree with just a single item at index 0,
59 * and then inserting an item at index ULONG_MAX. This requires 2 new branches
60 * of RADIX_TREE_MAX_PATH size to be created, with only the root node shared.
63 #define RADIX_TREE_PRELOAD_SIZE (RADIX_TREE_MAX_PATH * 2 - 1)
66 * The IDR does not have to be as high as the radix tree since it uses
67 * signed integers, not unsigned longs.
69 #define IDR_INDEX_BITS (8 /* CHAR_BIT */ * sizeof(int) - 1)
70 #define IDR_MAX_PATH (DIV_ROUND_UP(IDR_INDEX_BITS, \
71 RADIX_TREE_MAP_SHIFT))
72 #define IDR_PRELOAD_SIZE (IDR_MAX_PATH * 2 - 1)
75 * The IDA is even shorter since it uses a bitmap at the last level.
77 #define IDA_INDEX_BITS (8 * sizeof(int) - 1 - ilog2(IDA_BITMAP_BITS))
78 #define IDA_MAX_PATH (DIV_ROUND_UP(IDA_INDEX_BITS, \
79 RADIX_TREE_MAP_SHIFT))
80 #define IDA_PRELOAD_SIZE (IDA_MAX_PATH * 2 - 1)
83 * Per-cpu pool of preloaded nodes
85 struct radix_tree_preload {
87 /* nodes->parent points to next preallocated node */
88 struct radix_tree_node *nodes;
90 static DEFINE_PER_CPU(struct radix_tree_preload, radix_tree_preloads) = { 0, };
92 static inline struct radix_tree_node *entry_to_node(void *ptr)
94 return (void *)((unsigned long)ptr & ~RADIX_TREE_INTERNAL_NODE);
97 static inline void *node_to_entry(void *ptr)
99 return (void *)((unsigned long)ptr | RADIX_TREE_INTERNAL_NODE);
102 #define RADIX_TREE_RETRY XA_RETRY_ENTRY
104 static inline unsigned long
105 get_slot_offset(const struct radix_tree_node *parent, void __rcu **slot)
107 return parent ? slot - parent->slots : 0;
110 static unsigned int radix_tree_descend(const struct radix_tree_node *parent,
111 struct radix_tree_node **nodep, unsigned long index)
113 unsigned int offset = (index >> parent->shift) & RADIX_TREE_MAP_MASK;
114 void __rcu **entry = rcu_dereference_raw(parent->slots[offset]);
116 if (xa_is_sibling(entry)) {
117 offset = xa_to_sibling(entry);
118 entry = rcu_dereference_raw(parent->slots[offset]);
121 *nodep = (void *)entry;
125 static inline gfp_t root_gfp_mask(const struct radix_tree_root *root)
127 return root->xa_flags & (__GFP_BITS_MASK & ~GFP_ZONEMASK);
130 static inline void tag_set(struct radix_tree_node *node, unsigned int tag,
133 __set_bit(offset, node->tags[tag]);
136 static inline void tag_clear(struct radix_tree_node *node, unsigned int tag,
139 __clear_bit(offset, node->tags[tag]);
142 static inline int tag_get(const struct radix_tree_node *node, unsigned int tag,
145 return test_bit(offset, node->tags[tag]);
148 static inline void root_tag_set(struct radix_tree_root *root, unsigned tag)
150 root->xa_flags |= (__force gfp_t)(1 << (tag + ROOT_TAG_SHIFT));
153 static inline void root_tag_clear(struct radix_tree_root *root, unsigned tag)
155 root->xa_flags &= (__force gfp_t)~(1 << (tag + ROOT_TAG_SHIFT));
158 static inline void root_tag_clear_all(struct radix_tree_root *root)
160 root->xa_flags &= (__force gfp_t)((1 << ROOT_TAG_SHIFT) - 1);
163 static inline int root_tag_get(const struct radix_tree_root *root, unsigned tag)
165 return (__force int)root->xa_flags & (1 << (tag + ROOT_TAG_SHIFT));
168 static inline unsigned root_tags_get(const struct radix_tree_root *root)
170 return (__force unsigned)root->xa_flags >> ROOT_TAG_SHIFT;
173 static inline bool is_idr(const struct radix_tree_root *root)
175 return !!(root->xa_flags & ROOT_IS_IDR);
179 * Returns 1 if any slot in the node has this tag set.
180 * Otherwise returns 0.
182 static inline int any_tag_set(const struct radix_tree_node *node,
186 for (idx = 0; idx < RADIX_TREE_TAG_LONGS; idx++) {
187 if (node->tags[tag][idx])
193 static inline void all_tag_set(struct radix_tree_node *node, unsigned int tag)
195 bitmap_fill(node->tags[tag], RADIX_TREE_MAP_SIZE);
199 * radix_tree_find_next_bit - find the next set bit in a memory region
201 * @addr: The address to base the search on
202 * @size: The bitmap size in bits
203 * @offset: The bitnumber to start searching at
205 * Unrollable variant of find_next_bit() for constant size arrays.
206 * Tail bits starting from size to roundup(size, BITS_PER_LONG) must be zero.
207 * Returns next bit offset, or size if nothing found.
209 static __always_inline unsigned long
210 radix_tree_find_next_bit(struct radix_tree_node *node, unsigned int tag,
211 unsigned long offset)
213 const unsigned long *addr = node->tags[tag];
215 if (offset < RADIX_TREE_MAP_SIZE) {
218 addr += offset / BITS_PER_LONG;
219 tmp = *addr >> (offset % BITS_PER_LONG);
221 return __ffs(tmp) + offset;
222 offset = (offset + BITS_PER_LONG) & ~(BITS_PER_LONG - 1);
223 while (offset < RADIX_TREE_MAP_SIZE) {
226 return __ffs(tmp) + offset;
227 offset += BITS_PER_LONG;
230 return RADIX_TREE_MAP_SIZE;
233 static unsigned int iter_offset(const struct radix_tree_iter *iter)
235 return (iter->index >> iter_shift(iter)) & RADIX_TREE_MAP_MASK;
239 * The maximum index which can be stored in a radix tree
241 static inline unsigned long shift_maxindex(unsigned int shift)
243 return (RADIX_TREE_MAP_SIZE << shift) - 1;
246 static inline unsigned long node_maxindex(const struct radix_tree_node *node)
248 return shift_maxindex(node->shift);
251 static unsigned long next_index(unsigned long index,
252 const struct radix_tree_node *node,
253 unsigned long offset)
255 return (index & ~node_maxindex(node)) + (offset << node->shift);
259 * This assumes that the caller has performed appropriate preallocation, and
260 * that the caller has pinned this thread of control to the current CPU.
262 static struct radix_tree_node *
263 radix_tree_node_alloc(gfp_t gfp_mask, struct radix_tree_node *parent,
264 struct radix_tree_root *root,
265 unsigned int shift, unsigned int offset,
266 unsigned int count, unsigned int nr_values)
268 struct radix_tree_node *ret = NULL;
271 * Preload code isn't irq safe and it doesn't make sense to use
272 * preloading during an interrupt anyway as all the allocations have
273 * to be atomic. So just do normal allocation when in interrupt.
275 if (!gfpflags_allow_blocking(gfp_mask) && !in_interrupt()) {
276 struct radix_tree_preload *rtp;
279 * Even if the caller has preloaded, try to allocate from the
280 * cache first for the new node to get accounted to the memory
283 ret = kmem_cache_alloc(radix_tree_node_cachep,
284 gfp_mask | __GFP_NOWARN);
289 * Provided the caller has preloaded here, we will always
290 * succeed in getting a node here (and never reach
293 rtp = this_cpu_ptr(&radix_tree_preloads);
296 rtp->nodes = ret->parent;
300 * Update the allocation stack trace as this is more useful
303 kmemleak_update_trace(ret);
306 ret = kmem_cache_alloc(radix_tree_node_cachep, gfp_mask);
308 BUG_ON(radix_tree_is_internal_node(ret));
311 ret->offset = offset;
313 ret->nr_values = nr_values;
314 ret->parent = parent;
320 void radix_tree_node_rcu_free(struct rcu_head *head)
322 struct radix_tree_node *node =
323 container_of(head, struct radix_tree_node, rcu_head);
326 * Must only free zeroed nodes into the slab. We can be left with
327 * non-NULL entries by radix_tree_free_nodes, so clear the entries
330 memset(node->slots, 0, sizeof(node->slots));
331 memset(node->tags, 0, sizeof(node->tags));
332 INIT_LIST_HEAD(&node->private_list);
334 kmem_cache_free(radix_tree_node_cachep, node);
338 radix_tree_node_free(struct radix_tree_node *node)
340 call_rcu(&node->rcu_head, radix_tree_node_rcu_free);
344 * Load up this CPU's radix_tree_node buffer with sufficient objects to
345 * ensure that the addition of a single element in the tree cannot fail. On
346 * success, return zero, with preemption disabled. On error, return -ENOMEM
347 * with preemption not disabled.
349 * To make use of this facility, the radix tree must be initialised without
350 * __GFP_DIRECT_RECLAIM being passed to INIT_RADIX_TREE().
352 static __must_check int __radix_tree_preload(gfp_t gfp_mask, unsigned nr)
354 struct radix_tree_preload *rtp;
355 struct radix_tree_node *node;
359 * Nodes preloaded by one cgroup can be be used by another cgroup, so
360 * they should never be accounted to any particular memory cgroup.
362 gfp_mask &= ~__GFP_ACCOUNT;
365 rtp = this_cpu_ptr(&radix_tree_preloads);
366 while (rtp->nr < nr) {
368 node = kmem_cache_alloc(radix_tree_node_cachep, gfp_mask);
372 rtp = this_cpu_ptr(&radix_tree_preloads);
374 node->parent = rtp->nodes;
378 kmem_cache_free(radix_tree_node_cachep, node);
387 * Load up this CPU's radix_tree_node buffer with sufficient objects to
388 * ensure that the addition of a single element in the tree cannot fail. On
389 * success, return zero, with preemption disabled. On error, return -ENOMEM
390 * with preemption not disabled.
392 * To make use of this facility, the radix tree must be initialised without
393 * __GFP_DIRECT_RECLAIM being passed to INIT_RADIX_TREE().
395 int radix_tree_preload(gfp_t gfp_mask)
397 /* Warn on non-sensical use... */
398 WARN_ON_ONCE(!gfpflags_allow_blocking(gfp_mask));
399 return __radix_tree_preload(gfp_mask, RADIX_TREE_PRELOAD_SIZE);
401 EXPORT_SYMBOL(radix_tree_preload);
404 * The same as above function, except we don't guarantee preloading happens.
405 * We do it, if we decide it helps. On success, return zero with preemption
406 * disabled. On error, return -ENOMEM with preemption not disabled.
408 int radix_tree_maybe_preload(gfp_t gfp_mask)
410 if (gfpflags_allow_blocking(gfp_mask))
411 return __radix_tree_preload(gfp_mask, RADIX_TREE_PRELOAD_SIZE);
412 /* Preloading doesn't help anything with this gfp mask, skip it */
416 EXPORT_SYMBOL(radix_tree_maybe_preload);
419 * The same as function above, but preload number of nodes required to insert
420 * (1 << order) continuous naturally-aligned elements.
422 int radix_tree_maybe_preload_order(gfp_t gfp_mask, int order)
424 unsigned long nr_subtrees;
425 int nr_nodes, subtree_height;
427 /* Preloading doesn't help anything with this gfp mask, skip it */
428 if (!gfpflags_allow_blocking(gfp_mask)) {
434 * Calculate number and height of fully populated subtrees it takes to
435 * store (1 << order) elements.
437 nr_subtrees = 1 << order;
438 for (subtree_height = 0; nr_subtrees > RADIX_TREE_MAP_SIZE;
440 nr_subtrees >>= RADIX_TREE_MAP_SHIFT;
443 * The worst case is zero height tree with a single item at index 0 and
444 * then inserting items starting at ULONG_MAX - (1 << order).
446 * This requires RADIX_TREE_MAX_PATH nodes to build branch from root to
449 nr_nodes = RADIX_TREE_MAX_PATH;
451 /* Plus branch to fully populated subtrees. */
452 nr_nodes += RADIX_TREE_MAX_PATH - subtree_height;
454 /* Root node is shared. */
457 /* Plus nodes required to build subtrees. */
458 nr_nodes += nr_subtrees * height_to_maxnodes[subtree_height];
460 return __radix_tree_preload(gfp_mask, nr_nodes);
463 static unsigned radix_tree_load_root(const struct radix_tree_root *root,
464 struct radix_tree_node **nodep, unsigned long *maxindex)
466 struct radix_tree_node *node = rcu_dereference_raw(root->xa_head);
470 if (likely(radix_tree_is_internal_node(node))) {
471 node = entry_to_node(node);
472 *maxindex = node_maxindex(node);
473 return node->shift + RADIX_TREE_MAP_SHIFT;
481 * Extend a radix tree so it can store key @index.
483 static int radix_tree_extend(struct radix_tree_root *root, gfp_t gfp,
484 unsigned long index, unsigned int shift)
487 unsigned int maxshift;
490 /* Figure out what the shift should be. */
492 while (index > shift_maxindex(maxshift))
493 maxshift += RADIX_TREE_MAP_SHIFT;
495 entry = rcu_dereference_raw(root->xa_head);
496 if (!entry && (!is_idr(root) || root_tag_get(root, IDR_FREE)))
500 struct radix_tree_node *node = radix_tree_node_alloc(gfp, NULL,
501 root, shift, 0, 1, 0);
506 all_tag_set(node, IDR_FREE);
507 if (!root_tag_get(root, IDR_FREE)) {
508 tag_clear(node, IDR_FREE, 0);
509 root_tag_set(root, IDR_FREE);
512 /* Propagate the aggregated tag info to the new child */
513 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) {
514 if (root_tag_get(root, tag))
515 tag_set(node, tag, 0);
519 BUG_ON(shift > BITS_PER_LONG);
520 if (radix_tree_is_internal_node(entry)) {
521 entry_to_node(entry)->parent = node;
522 } else if (xa_is_value(entry)) {
523 /* Moving a value entry root->xa_head to a node */
527 * entry was already in the radix tree, so we do not need
528 * rcu_assign_pointer here
530 node->slots[0] = (void __rcu *)entry;
531 entry = node_to_entry(node);
532 rcu_assign_pointer(root->xa_head, entry);
533 shift += RADIX_TREE_MAP_SHIFT;
534 } while (shift <= maxshift);
536 return maxshift + RADIX_TREE_MAP_SHIFT;
540 * radix_tree_shrink - shrink radix tree to minimum height
541 * @root radix tree root
543 static inline bool radix_tree_shrink(struct radix_tree_root *root)
548 struct radix_tree_node *node = rcu_dereference_raw(root->xa_head);
549 struct radix_tree_node *child;
551 if (!radix_tree_is_internal_node(node))
553 node = entry_to_node(node);
556 * The candidate node has more than one child, or its child
557 * is not at the leftmost slot, or the child is a multiorder
558 * entry, we cannot shrink.
560 if (node->count != 1)
562 child = rcu_dereference_raw(node->slots[0]);
565 if (!radix_tree_is_internal_node(child) && node->shift)
569 * For an IDR, we must not shrink entry 0 into the root in
570 * case somebody calls idr_replace() with a pointer that
571 * appears to be an internal entry
573 if (!node->shift && is_idr(root))
576 if (radix_tree_is_internal_node(child))
577 entry_to_node(child)->parent = NULL;
580 * We don't need rcu_assign_pointer(), since we are simply
581 * moving the node from one part of the tree to another: if it
582 * was safe to dereference the old pointer to it
583 * (node->slots[0]), it will be safe to dereference the new
584 * one (root->xa_head) as far as dependent read barriers go.
586 root->xa_head = (void __rcu *)child;
587 if (is_idr(root) && !tag_get(node, IDR_FREE, 0))
588 root_tag_clear(root, IDR_FREE);
591 * We have a dilemma here. The node's slot[0] must not be
592 * NULLed in case there are concurrent lookups expecting to
593 * find the item. However if this was a bottom-level node,
594 * then it may be subject to the slot pointer being visible
595 * to callers dereferencing it. If item corresponding to
596 * slot[0] is subsequently deleted, these callers would expect
597 * their slot to become empty sooner or later.
599 * For example, lockless pagecache will look up a slot, deref
600 * the page pointer, and if the page has 0 refcount it means it
601 * was concurrently deleted from pagecache so try the deref
602 * again. Fortunately there is already a requirement for logic
603 * to retry the entire slot lookup -- the indirect pointer
604 * problem (replacing direct root node with an indirect pointer
605 * also results in a stale slot). So tag the slot as indirect
606 * to force callers to retry.
609 if (!radix_tree_is_internal_node(child)) {
610 node->slots[0] = (void __rcu *)RADIX_TREE_RETRY;
613 WARN_ON_ONCE(!list_empty(&node->private_list));
614 radix_tree_node_free(node);
621 static bool delete_node(struct radix_tree_root *root,
622 struct radix_tree_node *node)
624 bool deleted = false;
627 struct radix_tree_node *parent;
630 if (node_to_entry(node) ==
631 rcu_dereference_raw(root->xa_head))
632 deleted |= radix_tree_shrink(root);
636 parent = node->parent;
638 parent->slots[node->offset] = NULL;
642 * Shouldn't the tags already have all been cleared
646 root_tag_clear_all(root);
647 root->xa_head = NULL;
650 WARN_ON_ONCE(!list_empty(&node->private_list));
651 radix_tree_node_free(node);
661 * __radix_tree_create - create a slot in a radix tree
662 * @root: radix tree root
664 * @order: index occupies 2^order aligned slots
665 * @nodep: returns node
666 * @slotp: returns slot
668 * Create, if necessary, and return the node and slot for an item
669 * at position @index in the radix tree @root.
671 * Until there is more than one item in the tree, no nodes are
672 * allocated and @root->xa_head is used as a direct slot instead of
673 * pointing to a node, in which case *@nodep will be NULL.
675 * Returns -ENOMEM, or 0 for success.
677 static int __radix_tree_create(struct radix_tree_root *root,
678 unsigned long index, unsigned order,
679 struct radix_tree_node **nodep, void __rcu ***slotp)
681 struct radix_tree_node *node = NULL, *child;
682 void __rcu **slot = (void __rcu **)&root->xa_head;
683 unsigned long maxindex;
684 unsigned int shift, offset = 0;
685 unsigned long max = index | ((1UL << order) - 1);
686 gfp_t gfp = root_gfp_mask(root);
688 shift = radix_tree_load_root(root, &child, &maxindex);
690 /* Make sure the tree is high enough. */
691 if (order > 0 && max == ((1UL << order) - 1))
693 if (max > maxindex) {
694 int error = radix_tree_extend(root, gfp, max, shift);
698 child = rcu_dereference_raw(root->xa_head);
701 while (shift > order) {
702 shift -= RADIX_TREE_MAP_SHIFT;
704 /* Have to add a child node. */
705 child = radix_tree_node_alloc(gfp, node, root, shift,
709 rcu_assign_pointer(*slot, node_to_entry(child));
712 } else if (!radix_tree_is_internal_node(child))
715 /* Go a level down */
716 node = entry_to_node(child);
717 offset = radix_tree_descend(node, &child, index);
718 slot = &node->slots[offset];
729 * Free any nodes below this node. The tree is presumed to not need
730 * shrinking, and any user data in the tree is presumed to not need a
731 * destructor called on it. If we need to add a destructor, we can
732 * add that functionality later. Note that we may not clear tags or
733 * slots from the tree as an RCU walker may still have a pointer into
734 * this subtree. We could replace the entries with RADIX_TREE_RETRY,
735 * but we'll still have to clear those in rcu_free.
737 static void radix_tree_free_nodes(struct radix_tree_node *node)
740 struct radix_tree_node *child = entry_to_node(node);
743 void *entry = rcu_dereference_raw(child->slots[offset]);
744 if (xa_is_node(entry) && child->shift) {
745 child = entry_to_node(entry);
750 while (offset == RADIX_TREE_MAP_SIZE) {
751 struct radix_tree_node *old = child;
752 offset = child->offset + 1;
753 child = child->parent;
754 WARN_ON_ONCE(!list_empty(&old->private_list));
755 radix_tree_node_free(old);
756 if (old == entry_to_node(node))
762 #ifdef CONFIG_RADIX_TREE_MULTIORDER
763 static inline int insert_entries(struct radix_tree_node *node,
764 void __rcu **slot, void *item, unsigned order, bool replace)
767 unsigned i, n, tag, offset, tags = 0;
770 if (order > node->shift)
771 n = 1 << (order - node->shift);
774 offset = get_slot_offset(node, slot);
781 offset = offset & ~(n - 1);
782 slot = &node->slots[offset];
784 sibling = xa_mk_sibling(offset);
786 for (i = 0; i < n; i++) {
790 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
791 if (tag_get(node, tag, offset + i))
798 for (i = 0; i < n; i++) {
799 struct radix_tree_node *old = rcu_dereference_raw(slot[i]);
801 rcu_assign_pointer(slot[i], sibling);
802 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
803 if (tags & (1 << tag))
804 tag_clear(node, tag, offset + i);
806 rcu_assign_pointer(slot[i], item);
807 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
808 if (tags & (1 << tag))
809 tag_set(node, tag, offset);
812 radix_tree_free_nodes(old);
813 if (xa_is_value(old))
818 if (xa_is_value(item))
819 node->nr_values += n;
824 static inline int insert_entries(struct radix_tree_node *node,
825 void __rcu **slot, void *item, unsigned order, bool replace)
829 rcu_assign_pointer(*slot, item);
832 if (xa_is_value(item))
840 * __radix_tree_insert - insert into a radix tree
841 * @root: radix tree root
843 * @order: key covers the 2^order indices around index
844 * @item: item to insert
846 * Insert an item into the radix tree at position @index.
848 int __radix_tree_insert(struct radix_tree_root *root, unsigned long index,
849 unsigned order, void *item)
851 struct radix_tree_node *node;
855 BUG_ON(radix_tree_is_internal_node(item));
857 error = __radix_tree_create(root, index, order, &node, &slot);
861 error = insert_entries(node, slot, item, order, false);
866 unsigned offset = get_slot_offset(node, slot);
867 BUG_ON(tag_get(node, 0, offset));
868 BUG_ON(tag_get(node, 1, offset));
869 BUG_ON(tag_get(node, 2, offset));
871 BUG_ON(root_tags_get(root));
876 EXPORT_SYMBOL(__radix_tree_insert);
879 * __radix_tree_lookup - lookup an item in a radix tree
880 * @root: radix tree root
882 * @nodep: returns node
883 * @slotp: returns slot
885 * Lookup and return the item at position @index in the radix
888 * Until there is more than one item in the tree, no nodes are
889 * allocated and @root->xa_head is used as a direct slot instead of
890 * pointing to a node, in which case *@nodep will be NULL.
892 void *__radix_tree_lookup(const struct radix_tree_root *root,
893 unsigned long index, struct radix_tree_node **nodep,
896 struct radix_tree_node *node, *parent;
897 unsigned long maxindex;
902 slot = (void __rcu **)&root->xa_head;
903 radix_tree_load_root(root, &node, &maxindex);
904 if (index > maxindex)
907 while (radix_tree_is_internal_node(node)) {
910 if (node == RADIX_TREE_RETRY)
912 parent = entry_to_node(node);
913 offset = radix_tree_descend(parent, &node, index);
914 slot = parent->slots + offset;
915 if (parent->shift == 0)
927 * radix_tree_lookup_slot - lookup a slot in a radix tree
928 * @root: radix tree root
931 * Returns: the slot corresponding to the position @index in the
932 * radix tree @root. This is useful for update-if-exists operations.
934 * This function can be called under rcu_read_lock iff the slot is not
935 * modified by radix_tree_replace_slot, otherwise it must be called
936 * exclusive from other writers. Any dereference of the slot must be done
937 * using radix_tree_deref_slot.
939 void __rcu **radix_tree_lookup_slot(const struct radix_tree_root *root,
944 if (!__radix_tree_lookup(root, index, NULL, &slot))
948 EXPORT_SYMBOL(radix_tree_lookup_slot);
951 * radix_tree_lookup - perform lookup operation on a radix tree
952 * @root: radix tree root
955 * Lookup the item at the position @index in the radix tree @root.
957 * This function can be called under rcu_read_lock, however the caller
958 * must manage lifetimes of leaf nodes (eg. RCU may also be used to free
959 * them safely). No RCU barriers are required to access or modify the
960 * returned item, however.
962 void *radix_tree_lookup(const struct radix_tree_root *root, unsigned long index)
964 return __radix_tree_lookup(root, index, NULL, NULL);
966 EXPORT_SYMBOL(radix_tree_lookup);
968 static inline void replace_sibling_entries(struct radix_tree_node *node,
969 void __rcu **slot, int count, int values)
971 #ifdef CONFIG_RADIX_TREE_MULTIORDER
972 unsigned offset = get_slot_offset(node, slot);
973 void *ptr = xa_mk_sibling(offset);
975 while (++offset < RADIX_TREE_MAP_SIZE) {
976 if (rcu_dereference_raw(node->slots[offset]) != ptr)
979 node->slots[offset] = NULL;
982 node->nr_values += values;
987 static void replace_slot(void __rcu **slot, void *item,
988 struct radix_tree_node *node, int count, int values)
990 if (node && (count || values)) {
991 node->count += count;
992 node->nr_values += values;
993 replace_sibling_entries(node, slot, count, values);
996 rcu_assign_pointer(*slot, item);
999 static bool node_tag_get(const struct radix_tree_root *root,
1000 const struct radix_tree_node *node,
1001 unsigned int tag, unsigned int offset)
1004 return tag_get(node, tag, offset);
1005 return root_tag_get(root, tag);
1009 * IDR users want to be able to store NULL in the tree, so if the slot isn't
1010 * free, don't adjust the count, even if it's transitioning between NULL and
1011 * non-NULL. For the IDA, we mark slots as being IDR_FREE while they still
1012 * have empty bits, but it only stores NULL in slots when they're being
1015 static int calculate_count(struct radix_tree_root *root,
1016 struct radix_tree_node *node, void __rcu **slot,
1017 void *item, void *old)
1020 unsigned offset = get_slot_offset(node, slot);
1021 bool free = node_tag_get(root, node, IDR_FREE, offset);
1027 return !!item - !!old;
1031 * __radix_tree_replace - replace item in a slot
1032 * @root: radix tree root
1033 * @node: pointer to tree node
1034 * @slot: pointer to slot in @node
1035 * @item: new item to store in the slot.
1037 * For use with __radix_tree_lookup(). Caller must hold tree write locked
1038 * across slot lookup and replacement.
1040 void __radix_tree_replace(struct radix_tree_root *root,
1041 struct radix_tree_node *node,
1042 void __rcu **slot, void *item)
1044 void *old = rcu_dereference_raw(*slot);
1045 int values = !!xa_is_value(item) - !!xa_is_value(old);
1046 int count = calculate_count(root, node, slot, item, old);
1049 * This function supports replacing value entries and
1050 * deleting entries, but that needs accounting against the
1051 * node unless the slot is root->xa_head.
1053 WARN_ON_ONCE(!node && (slot != (void __rcu **)&root->xa_head) &&
1055 replace_slot(slot, item, node, count, values);
1060 delete_node(root, node);
1064 * radix_tree_replace_slot - replace item in a slot
1065 * @root: radix tree root
1066 * @slot: pointer to slot
1067 * @item: new item to store in the slot.
1069 * For use with radix_tree_lookup_slot() and
1070 * radix_tree_gang_lookup_tag_slot(). Caller must hold tree write locked
1071 * across slot lookup and replacement.
1073 * NOTE: This cannot be used to switch between non-entries (empty slots),
1074 * regular entries, and value entries, as that requires accounting
1075 * inside the radix tree node. When switching from one type of entry or
1076 * deleting, use __radix_tree_lookup() and __radix_tree_replace() or
1077 * radix_tree_iter_replace().
1079 void radix_tree_replace_slot(struct radix_tree_root *root,
1080 void __rcu **slot, void *item)
1082 __radix_tree_replace(root, NULL, slot, item);
1084 EXPORT_SYMBOL(radix_tree_replace_slot);
1087 * radix_tree_iter_replace - replace item in a slot
1088 * @root: radix tree root
1089 * @slot: pointer to slot
1090 * @item: new item to store in the slot.
1092 * For use with radix_tree_for_each_slot().
1093 * Caller must hold tree write locked.
1095 void radix_tree_iter_replace(struct radix_tree_root *root,
1096 const struct radix_tree_iter *iter,
1097 void __rcu **slot, void *item)
1099 __radix_tree_replace(root, iter->node, slot, item);
1102 static void node_tag_set(struct radix_tree_root *root,
1103 struct radix_tree_node *node,
1104 unsigned int tag, unsigned int offset)
1107 if (tag_get(node, tag, offset))
1109 tag_set(node, tag, offset);
1110 offset = node->offset;
1111 node = node->parent;
1114 if (!root_tag_get(root, tag))
1115 root_tag_set(root, tag);
1119 * radix_tree_tag_set - set a tag on a radix tree node
1120 * @root: radix tree root
1124 * Set the search tag (which must be < RADIX_TREE_MAX_TAGS)
1125 * corresponding to @index in the radix tree. From
1126 * the root all the way down to the leaf node.
1128 * Returns the address of the tagged item. Setting a tag on a not-present
1131 void *radix_tree_tag_set(struct radix_tree_root *root,
1132 unsigned long index, unsigned int tag)
1134 struct radix_tree_node *node, *parent;
1135 unsigned long maxindex;
1137 radix_tree_load_root(root, &node, &maxindex);
1138 BUG_ON(index > maxindex);
1140 while (radix_tree_is_internal_node(node)) {
1143 parent = entry_to_node(node);
1144 offset = radix_tree_descend(parent, &node, index);
1147 if (!tag_get(parent, tag, offset))
1148 tag_set(parent, tag, offset);
1151 /* set the root's tag bit */
1152 if (!root_tag_get(root, tag))
1153 root_tag_set(root, tag);
1157 EXPORT_SYMBOL(radix_tree_tag_set);
1160 * radix_tree_iter_tag_set - set a tag on the current iterator entry
1161 * @root: radix tree root
1162 * @iter: iterator state
1165 void radix_tree_iter_tag_set(struct radix_tree_root *root,
1166 const struct radix_tree_iter *iter, unsigned int tag)
1168 node_tag_set(root, iter->node, tag, iter_offset(iter));
1171 static void node_tag_clear(struct radix_tree_root *root,
1172 struct radix_tree_node *node,
1173 unsigned int tag, unsigned int offset)
1176 if (!tag_get(node, tag, offset))
1178 tag_clear(node, tag, offset);
1179 if (any_tag_set(node, tag))
1182 offset = node->offset;
1183 node = node->parent;
1186 /* clear the root's tag bit */
1187 if (root_tag_get(root, tag))
1188 root_tag_clear(root, tag);
1192 * radix_tree_tag_clear - clear a tag on a radix tree node
1193 * @root: radix tree root
1197 * Clear the search tag (which must be < RADIX_TREE_MAX_TAGS)
1198 * corresponding to @index in the radix tree. If this causes
1199 * the leaf node to have no tags set then clear the tag in the
1200 * next-to-leaf node, etc.
1202 * Returns the address of the tagged item on success, else NULL. ie:
1203 * has the same return value and semantics as radix_tree_lookup().
1205 void *radix_tree_tag_clear(struct radix_tree_root *root,
1206 unsigned long index, unsigned int tag)
1208 struct radix_tree_node *node, *parent;
1209 unsigned long maxindex;
1210 int uninitialized_var(offset);
1212 radix_tree_load_root(root, &node, &maxindex);
1213 if (index > maxindex)
1218 while (radix_tree_is_internal_node(node)) {
1219 parent = entry_to_node(node);
1220 offset = radix_tree_descend(parent, &node, index);
1224 node_tag_clear(root, parent, tag, offset);
1228 EXPORT_SYMBOL(radix_tree_tag_clear);
1231 * radix_tree_iter_tag_clear - clear a tag on the current iterator entry
1232 * @root: radix tree root
1233 * @iter: iterator state
1234 * @tag: tag to clear
1236 void radix_tree_iter_tag_clear(struct radix_tree_root *root,
1237 const struct radix_tree_iter *iter, unsigned int tag)
1239 node_tag_clear(root, iter->node, tag, iter_offset(iter));
1243 * radix_tree_tag_get - get a tag on a radix tree node
1244 * @root: radix tree root
1246 * @tag: tag index (< RADIX_TREE_MAX_TAGS)
1250 * 0: tag not present or not set
1253 * Note that the return value of this function may not be relied on, even if
1254 * the RCU lock is held, unless tag modification and node deletion are excluded
1257 int radix_tree_tag_get(const struct radix_tree_root *root,
1258 unsigned long index, unsigned int tag)
1260 struct radix_tree_node *node, *parent;
1261 unsigned long maxindex;
1263 if (!root_tag_get(root, tag))
1266 radix_tree_load_root(root, &node, &maxindex);
1267 if (index > maxindex)
1270 while (radix_tree_is_internal_node(node)) {
1273 parent = entry_to_node(node);
1274 offset = radix_tree_descend(parent, &node, index);
1276 if (!tag_get(parent, tag, offset))
1278 if (node == RADIX_TREE_RETRY)
1284 EXPORT_SYMBOL(radix_tree_tag_get);
1286 static inline void __set_iter_shift(struct radix_tree_iter *iter,
1289 #ifdef CONFIG_RADIX_TREE_MULTIORDER
1290 iter->shift = shift;
1294 /* Construct iter->tags bit-mask from node->tags[tag] array */
1295 static void set_iter_tags(struct radix_tree_iter *iter,
1296 struct radix_tree_node *node, unsigned offset,
1299 unsigned tag_long = offset / BITS_PER_LONG;
1300 unsigned tag_bit = offset % BITS_PER_LONG;
1307 iter->tags = node->tags[tag][tag_long] >> tag_bit;
1309 /* This never happens if RADIX_TREE_TAG_LONGS == 1 */
1310 if (tag_long < RADIX_TREE_TAG_LONGS - 1) {
1311 /* Pick tags from next element */
1313 iter->tags |= node->tags[tag][tag_long + 1] <<
1314 (BITS_PER_LONG - tag_bit);
1315 /* Clip chunk size, here only BITS_PER_LONG tags */
1316 iter->next_index = __radix_tree_iter_add(iter, BITS_PER_LONG);
1320 #ifdef CONFIG_RADIX_TREE_MULTIORDER
1321 static void __rcu **skip_siblings(struct radix_tree_node **nodep,
1322 void __rcu **slot, struct radix_tree_iter *iter)
1324 while (iter->index < iter->next_index) {
1325 *nodep = rcu_dereference_raw(*slot);
1326 if (*nodep && !xa_is_sibling(*nodep))
1329 iter->index = __radix_tree_iter_add(iter, 1);
1337 void __rcu **__radix_tree_next_slot(void __rcu **slot,
1338 struct radix_tree_iter *iter, unsigned flags)
1340 unsigned tag = flags & RADIX_TREE_ITER_TAG_MASK;
1341 struct radix_tree_node *node;
1343 slot = skip_siblings(&node, slot, iter);
1345 while (radix_tree_is_internal_node(node)) {
1347 unsigned long next_index;
1349 if (node == RADIX_TREE_RETRY)
1351 node = entry_to_node(node);
1353 iter->shift = node->shift;
1355 if (flags & RADIX_TREE_ITER_TAGGED) {
1356 offset = radix_tree_find_next_bit(node, tag, 0);
1357 if (offset == RADIX_TREE_MAP_SIZE)
1359 slot = &node->slots[offset];
1360 iter->index = __radix_tree_iter_add(iter, offset);
1361 set_iter_tags(iter, node, offset, tag);
1362 node = rcu_dereference_raw(*slot);
1365 slot = &node->slots[0];
1367 node = rcu_dereference_raw(*slot);
1372 if (offset == RADIX_TREE_MAP_SIZE)
1375 iter->index = __radix_tree_iter_add(iter, offset);
1377 if ((flags & RADIX_TREE_ITER_CONTIG) && (offset > 0))
1379 next_index = (iter->index | shift_maxindex(iter->shift)) + 1;
1380 if (next_index < iter->next_index)
1381 iter->next_index = next_index;
1386 iter->next_index = 0;
1389 EXPORT_SYMBOL(__radix_tree_next_slot);
1391 static void __rcu **skip_siblings(struct radix_tree_node **nodep,
1392 void __rcu **slot, struct radix_tree_iter *iter)
1398 void __rcu **radix_tree_iter_resume(void __rcu **slot,
1399 struct radix_tree_iter *iter)
1401 struct radix_tree_node *node;
1404 iter->index = __radix_tree_iter_add(iter, 1);
1405 skip_siblings(&node, slot, iter);
1406 iter->next_index = iter->index;
1410 EXPORT_SYMBOL(radix_tree_iter_resume);
1413 * radix_tree_next_chunk - find next chunk of slots for iteration
1415 * @root: radix tree root
1416 * @iter: iterator state
1417 * @flags: RADIX_TREE_ITER_* flags and tag index
1418 * Returns: pointer to chunk first slot, or NULL if iteration is over
1420 void __rcu **radix_tree_next_chunk(const struct radix_tree_root *root,
1421 struct radix_tree_iter *iter, unsigned flags)
1423 unsigned tag = flags & RADIX_TREE_ITER_TAG_MASK;
1424 struct radix_tree_node *node, *child;
1425 unsigned long index, offset, maxindex;
1427 if ((flags & RADIX_TREE_ITER_TAGGED) && !root_tag_get(root, tag))
1431 * Catch next_index overflow after ~0UL. iter->index never overflows
1432 * during iterating; it can be zero only at the beginning.
1433 * And we cannot overflow iter->next_index in a single step,
1434 * because RADIX_TREE_MAP_SHIFT < BITS_PER_LONG.
1436 * This condition also used by radix_tree_next_slot() to stop
1437 * contiguous iterating, and forbid switching to the next chunk.
1439 index = iter->next_index;
1440 if (!index && iter->index)
1444 radix_tree_load_root(root, &child, &maxindex);
1445 if (index > maxindex)
1450 if (!radix_tree_is_internal_node(child)) {
1451 /* Single-slot tree */
1452 iter->index = index;
1453 iter->next_index = maxindex + 1;
1456 __set_iter_shift(iter, 0);
1457 return (void __rcu **)&root->xa_head;
1461 node = entry_to_node(child);
1462 offset = radix_tree_descend(node, &child, index);
1464 if ((flags & RADIX_TREE_ITER_TAGGED) ?
1465 !tag_get(node, tag, offset) : !child) {
1467 if (flags & RADIX_TREE_ITER_CONTIG)
1470 if (flags & RADIX_TREE_ITER_TAGGED)
1471 offset = radix_tree_find_next_bit(node, tag,
1474 while (++offset < RADIX_TREE_MAP_SIZE) {
1475 void *slot = rcu_dereference_raw(
1476 node->slots[offset]);
1477 if (xa_is_sibling(slot))
1482 index &= ~node_maxindex(node);
1483 index += offset << node->shift;
1484 /* Overflow after ~0UL */
1487 if (offset == RADIX_TREE_MAP_SIZE)
1489 child = rcu_dereference_raw(node->slots[offset]);
1494 if (child == RADIX_TREE_RETRY)
1496 } while (node->shift && radix_tree_is_internal_node(child));
1498 /* Update the iterator state */
1499 iter->index = (index &~ node_maxindex(node)) | (offset << node->shift);
1500 iter->next_index = (index | node_maxindex(node)) + 1;
1502 __set_iter_shift(iter, node->shift);
1504 if (flags & RADIX_TREE_ITER_TAGGED)
1505 set_iter_tags(iter, node, offset, tag);
1507 return node->slots + offset;
1509 EXPORT_SYMBOL(radix_tree_next_chunk);
1512 * radix_tree_gang_lookup - perform multiple lookup on a radix tree
1513 * @root: radix tree root
1514 * @results: where the results of the lookup are placed
1515 * @first_index: start the lookup from this key
1516 * @max_items: place up to this many items at *results
1518 * Performs an index-ascending scan of the tree for present items. Places
1519 * them at *@results and returns the number of items which were placed at
1522 * The implementation is naive.
1524 * Like radix_tree_lookup, radix_tree_gang_lookup may be called under
1525 * rcu_read_lock. In this case, rather than the returned results being
1526 * an atomic snapshot of the tree at a single point in time, the
1527 * semantics of an RCU protected gang lookup are as though multiple
1528 * radix_tree_lookups have been issued in individual locks, and results
1529 * stored in 'results'.
1532 radix_tree_gang_lookup(const struct radix_tree_root *root, void **results,
1533 unsigned long first_index, unsigned int max_items)
1535 struct radix_tree_iter iter;
1537 unsigned int ret = 0;
1539 if (unlikely(!max_items))
1542 radix_tree_for_each_slot(slot, root, &iter, first_index) {
1543 results[ret] = rcu_dereference_raw(*slot);
1546 if (radix_tree_is_internal_node(results[ret])) {
1547 slot = radix_tree_iter_retry(&iter);
1550 if (++ret == max_items)
1556 EXPORT_SYMBOL(radix_tree_gang_lookup);
1559 * radix_tree_gang_lookup_tag - perform multiple lookup on a radix tree
1561 * @root: radix tree root
1562 * @results: where the results of the lookup are placed
1563 * @first_index: start the lookup from this key
1564 * @max_items: place up to this many items at *results
1565 * @tag: the tag index (< RADIX_TREE_MAX_TAGS)
1567 * Performs an index-ascending scan of the tree for present items which
1568 * have the tag indexed by @tag set. Places the items at *@results and
1569 * returns the number of items which were placed at *@results.
1572 radix_tree_gang_lookup_tag(const struct radix_tree_root *root, void **results,
1573 unsigned long first_index, unsigned int max_items,
1576 struct radix_tree_iter iter;
1578 unsigned int ret = 0;
1580 if (unlikely(!max_items))
1583 radix_tree_for_each_tagged(slot, root, &iter, first_index, tag) {
1584 results[ret] = rcu_dereference_raw(*slot);
1587 if (radix_tree_is_internal_node(results[ret])) {
1588 slot = radix_tree_iter_retry(&iter);
1591 if (++ret == max_items)
1597 EXPORT_SYMBOL(radix_tree_gang_lookup_tag);
1600 * radix_tree_gang_lookup_tag_slot - perform multiple slot lookup on a
1601 * radix tree based on a tag
1602 * @root: radix tree root
1603 * @results: where the results of the lookup are placed
1604 * @first_index: start the lookup from this key
1605 * @max_items: place up to this many items at *results
1606 * @tag: the tag index (< RADIX_TREE_MAX_TAGS)
1608 * Performs an index-ascending scan of the tree for present items which
1609 * have the tag indexed by @tag set. Places the slots at *@results and
1610 * returns the number of slots which were placed at *@results.
1613 radix_tree_gang_lookup_tag_slot(const struct radix_tree_root *root,
1614 void __rcu ***results, unsigned long first_index,
1615 unsigned int max_items, unsigned int tag)
1617 struct radix_tree_iter iter;
1619 unsigned int ret = 0;
1621 if (unlikely(!max_items))
1624 radix_tree_for_each_tagged(slot, root, &iter, first_index, tag) {
1625 results[ret] = slot;
1626 if (++ret == max_items)
1632 EXPORT_SYMBOL(radix_tree_gang_lookup_tag_slot);
1634 static bool __radix_tree_delete(struct radix_tree_root *root,
1635 struct radix_tree_node *node, void __rcu **slot)
1637 void *old = rcu_dereference_raw(*slot);
1638 int values = xa_is_value(old) ? -1 : 0;
1639 unsigned offset = get_slot_offset(node, slot);
1643 node_tag_set(root, node, IDR_FREE, offset);
1645 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
1646 node_tag_clear(root, node, tag, offset);
1648 replace_slot(slot, NULL, node, -1, values);
1649 return node && delete_node(root, node);
1653 * radix_tree_iter_delete - delete the entry at this iterator position
1654 * @root: radix tree root
1655 * @iter: iterator state
1656 * @slot: pointer to slot
1658 * Delete the entry at the position currently pointed to by the iterator.
1659 * This may result in the current node being freed; if it is, the iterator
1660 * is advanced so that it will not reference the freed memory. This
1661 * function may be called without any locking if there are no other threads
1662 * which can access this tree.
1664 void radix_tree_iter_delete(struct radix_tree_root *root,
1665 struct radix_tree_iter *iter, void __rcu **slot)
1667 if (__radix_tree_delete(root, iter->node, slot))
1668 iter->index = iter->next_index;
1670 EXPORT_SYMBOL(radix_tree_iter_delete);
1673 * radix_tree_delete_item - delete an item from a radix tree
1674 * @root: radix tree root
1676 * @item: expected item
1678 * Remove @item at @index from the radix tree rooted at @root.
1680 * Return: the deleted entry, or %NULL if it was not present
1681 * or the entry at the given @index was not @item.
1683 void *radix_tree_delete_item(struct radix_tree_root *root,
1684 unsigned long index, void *item)
1686 struct radix_tree_node *node = NULL;
1687 void __rcu **slot = NULL;
1690 entry = __radix_tree_lookup(root, index, &node, &slot);
1693 if (!entry && (!is_idr(root) || node_tag_get(root, node, IDR_FREE,
1694 get_slot_offset(node, slot))))
1697 if (item && entry != item)
1700 __radix_tree_delete(root, node, slot);
1704 EXPORT_SYMBOL(radix_tree_delete_item);
1707 * radix_tree_delete - delete an entry from a radix tree
1708 * @root: radix tree root
1711 * Remove the entry at @index from the radix tree rooted at @root.
1713 * Return: The deleted entry, or %NULL if it was not present.
1715 void *radix_tree_delete(struct radix_tree_root *root, unsigned long index)
1717 return radix_tree_delete_item(root, index, NULL);
1719 EXPORT_SYMBOL(radix_tree_delete);
1721 void radix_tree_clear_tags(struct radix_tree_root *root,
1722 struct radix_tree_node *node,
1726 unsigned int tag, offset = get_slot_offset(node, slot);
1727 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
1728 node_tag_clear(root, node, tag, offset);
1730 root_tag_clear_all(root);
1735 * radix_tree_tagged - test whether any items in the tree are tagged
1736 * @root: radix tree root
1739 int radix_tree_tagged(const struct radix_tree_root *root, unsigned int tag)
1741 return root_tag_get(root, tag);
1743 EXPORT_SYMBOL(radix_tree_tagged);
1746 * idr_preload - preload for idr_alloc()
1747 * @gfp_mask: allocation mask to use for preloading
1749 * Preallocate memory to use for the next call to idr_alloc(). This function
1750 * returns with preemption disabled. It will be enabled by idr_preload_end().
1752 void idr_preload(gfp_t gfp_mask)
1754 if (__radix_tree_preload(gfp_mask, IDR_PRELOAD_SIZE))
1757 EXPORT_SYMBOL(idr_preload);
1759 void __rcu **idr_get_free(struct radix_tree_root *root,
1760 struct radix_tree_iter *iter, gfp_t gfp,
1763 struct radix_tree_node *node = NULL, *child;
1764 void __rcu **slot = (void __rcu **)&root->xa_head;
1765 unsigned long maxindex, start = iter->next_index;
1766 unsigned int shift, offset = 0;
1769 shift = radix_tree_load_root(root, &child, &maxindex);
1770 if (!radix_tree_tagged(root, IDR_FREE))
1771 start = max(start, maxindex + 1);
1773 return ERR_PTR(-ENOSPC);
1775 if (start > maxindex) {
1776 int error = radix_tree_extend(root, gfp, start, shift);
1778 return ERR_PTR(error);
1780 child = rcu_dereference_raw(root->xa_head);
1782 if (start == 0 && shift == 0)
1783 shift = RADIX_TREE_MAP_SHIFT;
1786 shift -= RADIX_TREE_MAP_SHIFT;
1787 if (child == NULL) {
1788 /* Have to add a child node. */
1789 child = radix_tree_node_alloc(gfp, node, root, shift,
1792 return ERR_PTR(-ENOMEM);
1793 all_tag_set(child, IDR_FREE);
1794 rcu_assign_pointer(*slot, node_to_entry(child));
1797 } else if (!radix_tree_is_internal_node(child))
1800 node = entry_to_node(child);
1801 offset = radix_tree_descend(node, &child, start);
1802 if (!tag_get(node, IDR_FREE, offset)) {
1803 offset = radix_tree_find_next_bit(node, IDR_FREE,
1805 start = next_index(start, node, offset);
1807 return ERR_PTR(-ENOSPC);
1808 while (offset == RADIX_TREE_MAP_SIZE) {
1809 offset = node->offset + 1;
1810 node = node->parent;
1813 shift = node->shift;
1815 child = rcu_dereference_raw(node->slots[offset]);
1817 slot = &node->slots[offset];
1820 iter->index = start;
1822 iter->next_index = 1 + min(max, (start | node_maxindex(node)));
1824 iter->next_index = 1;
1826 __set_iter_shift(iter, shift);
1827 set_iter_tags(iter, node, offset, IDR_FREE);
1833 * idr_destroy - release all internal memory from an IDR
1836 * After this function is called, the IDR is empty, and may be reused or
1837 * the data structure containing it may be freed.
1839 * A typical clean-up sequence for objects stored in an idr tree will use
1840 * idr_for_each() to free all objects, if necessary, then idr_destroy() to
1841 * free the memory used to keep track of those objects.
1843 void idr_destroy(struct idr *idr)
1845 struct radix_tree_node *node = rcu_dereference_raw(idr->idr_rt.xa_head);
1846 if (radix_tree_is_internal_node(node))
1847 radix_tree_free_nodes(node);
1848 idr->idr_rt.xa_head = NULL;
1849 root_tag_set(&idr->idr_rt, IDR_FREE);
1851 EXPORT_SYMBOL(idr_destroy);
1854 radix_tree_node_ctor(void *arg)
1856 struct radix_tree_node *node = arg;
1858 memset(node, 0, sizeof(*node));
1859 INIT_LIST_HEAD(&node->private_list);
1862 static __init unsigned long __maxindex(unsigned int height)
1864 unsigned int width = height * RADIX_TREE_MAP_SHIFT;
1865 int shift = RADIX_TREE_INDEX_BITS - width;
1869 if (shift >= BITS_PER_LONG)
1871 return ~0UL >> shift;
1874 static __init void radix_tree_init_maxnodes(void)
1876 unsigned long height_to_maxindex[RADIX_TREE_MAX_PATH + 1];
1879 for (i = 0; i < ARRAY_SIZE(height_to_maxindex); i++)
1880 height_to_maxindex[i] = __maxindex(i);
1881 for (i = 0; i < ARRAY_SIZE(height_to_maxnodes); i++) {
1882 for (j = i; j > 0; j--)
1883 height_to_maxnodes[i] += height_to_maxindex[j - 1] + 1;
1887 static int radix_tree_cpu_dead(unsigned int cpu)
1889 struct radix_tree_preload *rtp;
1890 struct radix_tree_node *node;
1892 /* Free per-cpu pool of preloaded nodes */
1893 rtp = &per_cpu(radix_tree_preloads, cpu);
1896 rtp->nodes = node->parent;
1897 kmem_cache_free(radix_tree_node_cachep, node);
1903 void __init radix_tree_init(void)
1907 BUILD_BUG_ON(RADIX_TREE_MAX_TAGS + __GFP_BITS_SHIFT > 32);
1908 BUILD_BUG_ON(ROOT_IS_IDR & ~GFP_ZONEMASK);
1909 BUILD_BUG_ON(XA_CHUNK_SIZE > 255);
1910 radix_tree_node_cachep = kmem_cache_create("radix_tree_node",
1911 sizeof(struct radix_tree_node), 0,
1912 SLAB_PANIC | SLAB_RECLAIM_ACCOUNT,
1913 radix_tree_node_ctor);
1914 radix_tree_init_maxnodes();
1915 ret = cpuhp_setup_state_nocalls(CPUHP_RADIX_DEAD, "lib/radix:dead",
1916 NULL, radix_tree_cpu_dead);