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/cpu.h>
28 #include <linux/errno.h>
29 #include <linux/export.h>
30 #include <linux/idr.h>
31 #include <linux/init.h>
32 #include <linux/kernel.h>
33 #include <linux/kmemleak.h>
34 #include <linux/percpu.h>
35 #include <linux/preempt.h> /* in_interrupt() */
36 #include <linux/radix-tree.h>
37 #include <linux/rcupdate.h>
38 #include <linux/slab.h>
39 #include <linux/string.h>
42 /* Number of nodes in fully populated tree of given height */
43 static unsigned long height_to_maxnodes
[RADIX_TREE_MAX_PATH
+ 1] __read_mostly
;
46 * Radix tree node cache.
48 static struct kmem_cache
*radix_tree_node_cachep
;
51 * The radix tree is variable-height, so an insert operation not only has
52 * to build the branch to its corresponding item, it also has to build the
53 * branch to existing items if the size has to be increased (by
56 * The worst case is a zero height tree with just a single item at index 0,
57 * and then inserting an item at index ULONG_MAX. This requires 2 new branches
58 * of RADIX_TREE_MAX_PATH size to be created, with only the root node shared.
61 #define RADIX_TREE_PRELOAD_SIZE (RADIX_TREE_MAX_PATH * 2 - 1)
64 * The IDR does not have to be as high as the radix tree since it uses
65 * signed integers, not unsigned longs.
67 #define IDR_INDEX_BITS (8 /* CHAR_BIT */ * sizeof(int) - 1)
68 #define IDR_MAX_PATH (DIV_ROUND_UP(IDR_INDEX_BITS, \
69 RADIX_TREE_MAP_SHIFT))
70 #define IDR_PRELOAD_SIZE (IDR_MAX_PATH * 2 - 1)
73 * The IDA is even shorter since it uses a bitmap at the last level.
75 #define IDA_INDEX_BITS (8 * sizeof(int) - 1 - ilog2(IDA_BITMAP_BITS))
76 #define IDA_MAX_PATH (DIV_ROUND_UP(IDA_INDEX_BITS, \
77 RADIX_TREE_MAP_SHIFT))
78 #define IDA_PRELOAD_SIZE (IDA_MAX_PATH * 2 - 1)
81 * Per-cpu pool of preloaded nodes
83 struct radix_tree_preload
{
85 /* nodes->private_data points to next preallocated node */
86 struct radix_tree_node
*nodes
;
88 static DEFINE_PER_CPU(struct radix_tree_preload
, radix_tree_preloads
) = { 0, };
90 static inline struct radix_tree_node
*entry_to_node(void *ptr
)
92 return (void *)((unsigned long)ptr
& ~RADIX_TREE_INTERNAL_NODE
);
95 static inline void *node_to_entry(void *ptr
)
97 return (void *)((unsigned long)ptr
| RADIX_TREE_INTERNAL_NODE
);
100 #define RADIX_TREE_RETRY node_to_entry(NULL)
102 #ifdef CONFIG_RADIX_TREE_MULTIORDER
103 /* Sibling slots point directly to another slot in the same node */
105 bool is_sibling_entry(const struct radix_tree_node
*parent
, void *node
)
108 return (parent
->slots
<= ptr
) &&
109 (ptr
< parent
->slots
+ RADIX_TREE_MAP_SIZE
);
113 bool is_sibling_entry(const struct radix_tree_node
*parent
, void *node
)
120 unsigned long get_slot_offset(const struct radix_tree_node
*parent
, void **slot
)
122 return slot
- parent
->slots
;
125 static unsigned int radix_tree_descend(const struct radix_tree_node
*parent
,
126 struct radix_tree_node
**nodep
, unsigned long index
)
128 unsigned int offset
= (index
>> parent
->shift
) & RADIX_TREE_MAP_MASK
;
129 void **entry
= rcu_dereference_raw(parent
->slots
[offset
]);
131 #ifdef CONFIG_RADIX_TREE_MULTIORDER
132 if (radix_tree_is_internal_node(entry
)) {
133 if (is_sibling_entry(parent
, entry
)) {
134 void **sibentry
= (void **) entry_to_node(entry
);
135 offset
= get_slot_offset(parent
, sibentry
);
136 entry
= rcu_dereference_raw(*sibentry
);
141 *nodep
= (void *)entry
;
145 static inline gfp_t
root_gfp_mask(const struct radix_tree_root
*root
)
147 return root
->gfp_mask
& __GFP_BITS_MASK
;
150 static inline void tag_set(struct radix_tree_node
*node
, unsigned int tag
,
153 __set_bit(offset
, node
->tags
[tag
]);
156 static inline void tag_clear(struct radix_tree_node
*node
, unsigned int tag
,
159 __clear_bit(offset
, node
->tags
[tag
]);
162 static inline int tag_get(const struct radix_tree_node
*node
, unsigned int tag
,
165 return test_bit(offset
, node
->tags
[tag
]);
168 static inline void root_tag_set(struct radix_tree_root
*root
, unsigned tag
)
170 root
->gfp_mask
|= (__force gfp_t
)(1 << (tag
+ ROOT_TAG_SHIFT
));
173 static inline void root_tag_clear(struct radix_tree_root
*root
, unsigned tag
)
175 root
->gfp_mask
&= (__force gfp_t
)~(1 << (tag
+ ROOT_TAG_SHIFT
));
178 static inline void root_tag_clear_all(struct radix_tree_root
*root
)
180 root
->gfp_mask
&= (1 << ROOT_TAG_SHIFT
) - 1;
183 static inline int root_tag_get(const struct radix_tree_root
*root
, unsigned tag
)
185 return (__force
int)root
->gfp_mask
& (1 << (tag
+ ROOT_TAG_SHIFT
));
188 static inline unsigned root_tags_get(const struct radix_tree_root
*root
)
190 return (__force
unsigned)root
->gfp_mask
>> ROOT_TAG_SHIFT
;
193 static inline bool is_idr(const struct radix_tree_root
*root
)
195 return !!(root
->gfp_mask
& ROOT_IS_IDR
);
199 * Returns 1 if any slot in the node has this tag set.
200 * Otherwise returns 0.
202 static inline int any_tag_set(const struct radix_tree_node
*node
,
206 for (idx
= 0; idx
< RADIX_TREE_TAG_LONGS
; idx
++) {
207 if (node
->tags
[tag
][idx
])
213 static inline void all_tag_set(struct radix_tree_node
*node
, unsigned int tag
)
215 bitmap_fill(node
->tags
[tag
], RADIX_TREE_MAP_SIZE
);
219 * radix_tree_find_next_bit - find the next set bit in a memory region
221 * @addr: The address to base the search on
222 * @size: The bitmap size in bits
223 * @offset: The bitnumber to start searching at
225 * Unrollable variant of find_next_bit() for constant size arrays.
226 * Tail bits starting from size to roundup(size, BITS_PER_LONG) must be zero.
227 * Returns next bit offset, or size if nothing found.
229 static __always_inline
unsigned long
230 radix_tree_find_next_bit(struct radix_tree_node
*node
, unsigned int tag
,
231 unsigned long offset
)
233 const unsigned long *addr
= node
->tags
[tag
];
235 if (offset
< RADIX_TREE_MAP_SIZE
) {
238 addr
+= offset
/ BITS_PER_LONG
;
239 tmp
= *addr
>> (offset
% BITS_PER_LONG
);
241 return __ffs(tmp
) + offset
;
242 offset
= (offset
+ BITS_PER_LONG
) & ~(BITS_PER_LONG
- 1);
243 while (offset
< RADIX_TREE_MAP_SIZE
) {
246 return __ffs(tmp
) + offset
;
247 offset
+= BITS_PER_LONG
;
250 return RADIX_TREE_MAP_SIZE
;
253 static unsigned int iter_offset(const struct radix_tree_iter
*iter
)
255 return (iter
->index
>> iter_shift(iter
)) & RADIX_TREE_MAP_MASK
;
259 * The maximum index which can be stored in a radix tree
261 static inline unsigned long shift_maxindex(unsigned int shift
)
263 return (RADIX_TREE_MAP_SIZE
<< shift
) - 1;
266 static inline unsigned long node_maxindex(const struct radix_tree_node
*node
)
268 return shift_maxindex(node
->shift
);
271 static unsigned long next_index(unsigned long index
,
272 const struct radix_tree_node
*node
,
273 unsigned long offset
)
275 return (index
& ~node_maxindex(node
)) + (offset
<< node
->shift
);
279 static void dump_node(struct radix_tree_node
*node
, unsigned long index
)
283 pr_debug("radix node: %p offset %d indices %lu-%lu parent %p tags %lx %lx %lx shift %d count %d exceptional %d\n",
284 node
, node
->offset
, index
, index
| node_maxindex(node
),
286 node
->tags
[0][0], node
->tags
[1][0], node
->tags
[2][0],
287 node
->shift
, node
->count
, node
->exceptional
);
289 for (i
= 0; i
< RADIX_TREE_MAP_SIZE
; i
++) {
290 unsigned long first
= index
| (i
<< node
->shift
);
291 unsigned long last
= first
| ((1UL << node
->shift
) - 1);
292 void *entry
= node
->slots
[i
];
295 if (entry
== RADIX_TREE_RETRY
) {
296 pr_debug("radix retry offset %ld indices %lu-%lu parent %p\n",
297 i
, first
, last
, node
);
298 } else if (!radix_tree_is_internal_node(entry
)) {
299 pr_debug("radix entry %p offset %ld indices %lu-%lu parent %p\n",
300 entry
, i
, first
, last
, node
);
301 } else if (is_sibling_entry(node
, entry
)) {
302 pr_debug("radix sblng %p offset %ld indices %lu-%lu parent %p val %p\n",
303 entry
, i
, first
, last
, node
,
304 *(void **)entry_to_node(entry
));
306 dump_node(entry_to_node(entry
), first
);
312 static void radix_tree_dump(struct radix_tree_root
*root
)
314 pr_debug("radix root: %p rnode %p tags %x\n",
316 root
->gfp_mask
>> ROOT_TAG_SHIFT
);
317 if (!radix_tree_is_internal_node(root
->rnode
))
319 dump_node(entry_to_node(root
->rnode
), 0);
322 static void dump_ida_node(void *entry
, unsigned long index
)
329 if (radix_tree_is_internal_node(entry
)) {
330 struct radix_tree_node
*node
= entry_to_node(entry
);
332 pr_debug("ida node: %p offset %d indices %lu-%lu parent %p free %lx shift %d count %d\n",
333 node
, node
->offset
, index
* IDA_BITMAP_BITS
,
334 ((index
| node_maxindex(node
)) + 1) *
336 node
->parent
, node
->tags
[0][0], node
->shift
,
338 for (i
= 0; i
< RADIX_TREE_MAP_SIZE
; i
++)
339 dump_ida_node(node
->slots
[i
],
340 index
| (i
<< node
->shift
));
341 } else if (radix_tree_exceptional_entry(entry
)) {
342 pr_debug("ida excp: %p offset %d indices %lu-%lu data %lx\n",
343 entry
, (int)(index
& RADIX_TREE_MAP_MASK
),
344 index
* IDA_BITMAP_BITS
,
345 index
* IDA_BITMAP_BITS
+ BITS_PER_LONG
-
346 RADIX_TREE_EXCEPTIONAL_SHIFT
,
347 (unsigned long)entry
>>
348 RADIX_TREE_EXCEPTIONAL_SHIFT
);
350 struct ida_bitmap
*bitmap
= entry
;
352 pr_debug("ida btmp: %p offset %d indices %lu-%lu data", bitmap
,
353 (int)(index
& RADIX_TREE_MAP_MASK
),
354 index
* IDA_BITMAP_BITS
,
355 (index
+ 1) * IDA_BITMAP_BITS
- 1);
356 for (i
= 0; i
< IDA_BITMAP_LONGS
; i
++)
357 pr_cont(" %lx", bitmap
->bitmap
[i
]);
362 static void ida_dump(struct ida
*ida
)
364 struct radix_tree_root
*root
= &ida
->ida_rt
;
365 pr_debug("ida: %p node %p free %d\n", ida
, root
->rnode
,
366 root
->gfp_mask
>> ROOT_TAG_SHIFT
);
367 dump_ida_node(root
->rnode
, 0);
372 * This assumes that the caller has performed appropriate preallocation, and
373 * that the caller has pinned this thread of control to the current CPU.
375 static struct radix_tree_node
*
376 radix_tree_node_alloc(gfp_t gfp_mask
, struct radix_tree_node
*parent
,
377 unsigned int shift
, unsigned int offset
,
378 unsigned int count
, unsigned int exceptional
)
380 struct radix_tree_node
*ret
= NULL
;
383 * Preload code isn't irq safe and it doesn't make sense to use
384 * preloading during an interrupt anyway as all the allocations have
385 * to be atomic. So just do normal allocation when in interrupt.
387 if (!gfpflags_allow_blocking(gfp_mask
) && !in_interrupt()) {
388 struct radix_tree_preload
*rtp
;
391 * Even if the caller has preloaded, try to allocate from the
392 * cache first for the new node to get accounted to the memory
395 ret
= kmem_cache_alloc(radix_tree_node_cachep
,
396 gfp_mask
| __GFP_NOWARN
);
401 * Provided the caller has preloaded here, we will always
402 * succeed in getting a node here (and never reach
405 rtp
= this_cpu_ptr(&radix_tree_preloads
);
408 rtp
->nodes
= ret
->private_data
;
409 ret
->private_data
= NULL
;
413 * Update the allocation stack trace as this is more useful
416 kmemleak_update_trace(ret
);
419 ret
= kmem_cache_alloc(radix_tree_node_cachep
, gfp_mask
);
421 BUG_ON(radix_tree_is_internal_node(ret
));
423 ret
->parent
= parent
;
425 ret
->offset
= offset
;
427 ret
->exceptional
= exceptional
;
432 static void radix_tree_node_rcu_free(struct rcu_head
*head
)
434 struct radix_tree_node
*node
=
435 container_of(head
, struct radix_tree_node
, rcu_head
);
438 * Must only free zeroed nodes into the slab. We can be left with
439 * non-NULL entries by radix_tree_free_nodes, so clear the entries
442 memset(node
->slots
, 0, sizeof(node
->slots
));
443 memset(node
->tags
, 0, sizeof(node
->tags
));
444 INIT_LIST_HEAD(&node
->private_list
);
446 kmem_cache_free(radix_tree_node_cachep
, node
);
450 radix_tree_node_free(struct radix_tree_node
*node
)
452 call_rcu(&node
->rcu_head
, radix_tree_node_rcu_free
);
456 * Load up this CPU's radix_tree_node buffer with sufficient objects to
457 * ensure that the addition of a single element in the tree cannot fail. On
458 * success, return zero, with preemption disabled. On error, return -ENOMEM
459 * with preemption not disabled.
461 * To make use of this facility, the radix tree must be initialised without
462 * __GFP_DIRECT_RECLAIM being passed to INIT_RADIX_TREE().
464 static int __radix_tree_preload(gfp_t gfp_mask
, unsigned nr
)
466 struct radix_tree_preload
*rtp
;
467 struct radix_tree_node
*node
;
471 * Nodes preloaded by one cgroup can be be used by another cgroup, so
472 * they should never be accounted to any particular memory cgroup.
474 gfp_mask
&= ~__GFP_ACCOUNT
;
477 rtp
= this_cpu_ptr(&radix_tree_preloads
);
478 while (rtp
->nr
< nr
) {
480 node
= kmem_cache_alloc(radix_tree_node_cachep
, gfp_mask
);
484 rtp
= this_cpu_ptr(&radix_tree_preloads
);
486 node
->private_data
= rtp
->nodes
;
490 kmem_cache_free(radix_tree_node_cachep
, node
);
499 * Load up this CPU's radix_tree_node buffer with sufficient objects to
500 * ensure that the addition of a single element in the tree cannot fail. On
501 * success, return zero, with preemption disabled. On error, return -ENOMEM
502 * with preemption not disabled.
504 * To make use of this facility, the radix tree must be initialised without
505 * __GFP_DIRECT_RECLAIM being passed to INIT_RADIX_TREE().
507 int radix_tree_preload(gfp_t gfp_mask
)
509 /* Warn on non-sensical use... */
510 WARN_ON_ONCE(!gfpflags_allow_blocking(gfp_mask
));
511 return __radix_tree_preload(gfp_mask
, RADIX_TREE_PRELOAD_SIZE
);
513 EXPORT_SYMBOL(radix_tree_preload
);
516 * The same as above function, except we don't guarantee preloading happens.
517 * We do it, if we decide it helps. On success, return zero with preemption
518 * disabled. On error, return -ENOMEM with preemption not disabled.
520 int radix_tree_maybe_preload(gfp_t gfp_mask
)
522 if (gfpflags_allow_blocking(gfp_mask
))
523 return __radix_tree_preload(gfp_mask
, RADIX_TREE_PRELOAD_SIZE
);
524 /* Preloading doesn't help anything with this gfp mask, skip it */
528 EXPORT_SYMBOL(radix_tree_maybe_preload
);
530 #ifdef CONFIG_RADIX_TREE_MULTIORDER
532 * Preload with enough objects to ensure that we can split a single entry
533 * of order @old_order into many entries of size @new_order
535 int radix_tree_split_preload(unsigned int old_order
, unsigned int new_order
,
538 unsigned top
= 1 << (old_order
% RADIX_TREE_MAP_SHIFT
);
539 unsigned layers
= (old_order
/ RADIX_TREE_MAP_SHIFT
) -
540 (new_order
/ RADIX_TREE_MAP_SHIFT
);
543 WARN_ON_ONCE(!gfpflags_allow_blocking(gfp_mask
));
544 BUG_ON(new_order
>= old_order
);
547 nr
= nr
* RADIX_TREE_MAP_SIZE
+ 1;
548 return __radix_tree_preload(gfp_mask
, top
* nr
);
553 * The same as function above, but preload number of nodes required to insert
554 * (1 << order) continuous naturally-aligned elements.
556 int radix_tree_maybe_preload_order(gfp_t gfp_mask
, int order
)
558 unsigned long nr_subtrees
;
559 int nr_nodes
, subtree_height
;
561 /* Preloading doesn't help anything with this gfp mask, skip it */
562 if (!gfpflags_allow_blocking(gfp_mask
)) {
568 * Calculate number and height of fully populated subtrees it takes to
569 * store (1 << order) elements.
571 nr_subtrees
= 1 << order
;
572 for (subtree_height
= 0; nr_subtrees
> RADIX_TREE_MAP_SIZE
;
574 nr_subtrees
>>= RADIX_TREE_MAP_SHIFT
;
577 * The worst case is zero height tree with a single item at index 0 and
578 * then inserting items starting at ULONG_MAX - (1 << order).
580 * This requires RADIX_TREE_MAX_PATH nodes to build branch from root to
583 nr_nodes
= RADIX_TREE_MAX_PATH
;
585 /* Plus branch to fully populated subtrees. */
586 nr_nodes
+= RADIX_TREE_MAX_PATH
- subtree_height
;
588 /* Root node is shared. */
591 /* Plus nodes required to build subtrees. */
592 nr_nodes
+= nr_subtrees
* height_to_maxnodes
[subtree_height
];
594 return __radix_tree_preload(gfp_mask
, nr_nodes
);
597 static unsigned radix_tree_load_root(const struct radix_tree_root
*root
,
598 struct radix_tree_node
**nodep
, unsigned long *maxindex
)
600 struct radix_tree_node
*node
= rcu_dereference_raw(root
->rnode
);
604 if (likely(radix_tree_is_internal_node(node
))) {
605 node
= entry_to_node(node
);
606 *maxindex
= node_maxindex(node
);
607 return node
->shift
+ RADIX_TREE_MAP_SHIFT
;
615 * Extend a radix tree so it can store key @index.
617 static int radix_tree_extend(struct radix_tree_root
*root
, gfp_t gfp
,
618 unsigned long index
, unsigned int shift
)
620 struct radix_tree_node
*slot
;
621 unsigned int maxshift
;
624 /* Figure out what the shift should be. */
626 while (index
> shift_maxindex(maxshift
))
627 maxshift
+= RADIX_TREE_MAP_SHIFT
;
630 if (!slot
&& (!is_idr(root
) || root_tag_get(root
, IDR_FREE
)))
634 struct radix_tree_node
*node
= radix_tree_node_alloc(gfp
, NULL
,
640 all_tag_set(node
, IDR_FREE
);
641 if (!root_tag_get(root
, IDR_FREE
)) {
642 tag_clear(node
, IDR_FREE
, 0);
643 root_tag_set(root
, IDR_FREE
);
646 /* Propagate the aggregated tag info to the new child */
647 for (tag
= 0; tag
< RADIX_TREE_MAX_TAGS
; tag
++) {
648 if (root_tag_get(root
, tag
))
649 tag_set(node
, tag
, 0);
653 BUG_ON(shift
> BITS_PER_LONG
);
654 if (radix_tree_is_internal_node(slot
)) {
655 entry_to_node(slot
)->parent
= node
;
656 } else if (radix_tree_exceptional_entry(slot
)) {
657 /* Moving an exceptional root->rnode to a node */
658 node
->exceptional
= 1;
660 node
->slots
[0] = slot
;
661 slot
= node_to_entry(node
);
662 rcu_assign_pointer(root
->rnode
, slot
);
663 shift
+= RADIX_TREE_MAP_SHIFT
;
664 } while (shift
<= maxshift
);
666 return maxshift
+ RADIX_TREE_MAP_SHIFT
;
670 * radix_tree_shrink - shrink radix tree to minimum height
671 * @root radix tree root
673 static inline bool radix_tree_shrink(struct radix_tree_root
*root
,
674 radix_tree_update_node_t update_node
,
680 struct radix_tree_node
*node
= root
->rnode
;
681 struct radix_tree_node
*child
;
683 if (!radix_tree_is_internal_node(node
))
685 node
= entry_to_node(node
);
688 * The candidate node has more than one child, or its child
689 * is not at the leftmost slot, or the child is a multiorder
690 * entry, we cannot shrink.
692 if (node
->count
!= 1)
694 child
= node
->slots
[0];
697 if (!radix_tree_is_internal_node(child
) && node
->shift
)
700 if (radix_tree_is_internal_node(child
))
701 entry_to_node(child
)->parent
= NULL
;
704 * We don't need rcu_assign_pointer(), since we are simply
705 * moving the node from one part of the tree to another: if it
706 * was safe to dereference the old pointer to it
707 * (node->slots[0]), it will be safe to dereference the new
708 * one (root->rnode) as far as dependent read barriers go.
711 if (is_idr(root
) && !tag_get(node
, IDR_FREE
, 0))
712 root_tag_clear(root
, IDR_FREE
);
715 * We have a dilemma here. The node's slot[0] must not be
716 * NULLed in case there are concurrent lookups expecting to
717 * find the item. However if this was a bottom-level node,
718 * then it may be subject to the slot pointer being visible
719 * to callers dereferencing it. If item corresponding to
720 * slot[0] is subsequently deleted, these callers would expect
721 * their slot to become empty sooner or later.
723 * For example, lockless pagecache will look up a slot, deref
724 * the page pointer, and if the page has 0 refcount it means it
725 * was concurrently deleted from pagecache so try the deref
726 * again. Fortunately there is already a requirement for logic
727 * to retry the entire slot lookup -- the indirect pointer
728 * problem (replacing direct root node with an indirect pointer
729 * also results in a stale slot). So tag the slot as indirect
730 * to force callers to retry.
733 if (!radix_tree_is_internal_node(child
)) {
734 node
->slots
[0] = RADIX_TREE_RETRY
;
736 update_node(node
, private);
739 WARN_ON_ONCE(!list_empty(&node
->private_list
));
740 radix_tree_node_free(node
);
747 static bool delete_node(struct radix_tree_root
*root
,
748 struct radix_tree_node
*node
,
749 radix_tree_update_node_t update_node
, void *private)
751 bool deleted
= false;
754 struct radix_tree_node
*parent
;
757 if (node
== entry_to_node(root
->rnode
))
758 deleted
|= radix_tree_shrink(root
, update_node
,
763 parent
= node
->parent
;
765 parent
->slots
[node
->offset
] = NULL
;
769 * Shouldn't the tags already have all been cleared
773 root_tag_clear_all(root
);
777 WARN_ON_ONCE(!list_empty(&node
->private_list
));
778 radix_tree_node_free(node
);
788 * __radix_tree_create - create a slot in a radix tree
789 * @root: radix tree root
791 * @order: index occupies 2^order aligned slots
792 * @nodep: returns node
793 * @slotp: returns slot
795 * Create, if necessary, and return the node and slot for an item
796 * at position @index in the radix tree @root.
798 * Until there is more than one item in the tree, no nodes are
799 * allocated and @root->rnode is used as a direct slot instead of
800 * pointing to a node, in which case *@nodep will be NULL.
802 * Returns -ENOMEM, or 0 for success.
804 int __radix_tree_create(struct radix_tree_root
*root
, unsigned long index
,
805 unsigned order
, struct radix_tree_node
**nodep
,
808 struct radix_tree_node
*node
= NULL
, *child
;
809 void **slot
= (void **)&root
->rnode
;
810 unsigned long maxindex
;
811 unsigned int shift
, offset
= 0;
812 unsigned long max
= index
| ((1UL << order
) - 1);
813 gfp_t gfp
= root_gfp_mask(root
);
815 shift
= radix_tree_load_root(root
, &child
, &maxindex
);
817 /* Make sure the tree is high enough. */
818 if (order
> 0 && max
== ((1UL << order
) - 1))
820 if (max
> maxindex
) {
821 int error
= radix_tree_extend(root
, gfp
, max
, shift
);
828 while (shift
> order
) {
829 shift
-= RADIX_TREE_MAP_SHIFT
;
831 /* Have to add a child node. */
832 child
= radix_tree_node_alloc(gfp
, node
, shift
,
836 rcu_assign_pointer(*slot
, node_to_entry(child
));
839 } else if (!radix_tree_is_internal_node(child
))
842 /* Go a level down */
843 node
= entry_to_node(child
);
844 offset
= radix_tree_descend(node
, &child
, index
);
845 slot
= &node
->slots
[offset
];
856 * Free any nodes below this node. The tree is presumed to not need
857 * shrinking, and any user data in the tree is presumed to not need a
858 * destructor called on it. If we need to add a destructor, we can
859 * add that functionality later. Note that we may not clear tags or
860 * slots from the tree as an RCU walker may still have a pointer into
861 * this subtree. We could replace the entries with RADIX_TREE_RETRY,
862 * but we'll still have to clear those in rcu_free.
864 static void radix_tree_free_nodes(struct radix_tree_node
*node
)
867 struct radix_tree_node
*child
= entry_to_node(node
);
870 void *entry
= child
->slots
[offset
];
871 if (radix_tree_is_internal_node(entry
) &&
872 !is_sibling_entry(child
, entry
)) {
873 child
= entry_to_node(entry
);
878 while (offset
== RADIX_TREE_MAP_SIZE
) {
879 struct radix_tree_node
*old
= child
;
880 offset
= child
->offset
+ 1;
881 child
= child
->parent
;
882 WARN_ON_ONCE(!list_empty(&old
->private_list
));
883 radix_tree_node_free(old
);
884 if (old
== entry_to_node(node
))
890 #ifdef CONFIG_RADIX_TREE_MULTIORDER
891 static inline int insert_entries(struct radix_tree_node
*node
, void **slot
,
892 void *item
, unsigned order
, bool replace
)
894 struct radix_tree_node
*child
;
895 unsigned i
, n
, tag
, offset
, tags
= 0;
898 if (order
> node
->shift
)
899 n
= 1 << (order
- node
->shift
);
902 offset
= get_slot_offset(node
, slot
);
909 offset
= offset
& ~(n
- 1);
910 slot
= &node
->slots
[offset
];
912 child
= node_to_entry(slot
);
914 for (i
= 0; i
< n
; i
++) {
918 for (tag
= 0; tag
< RADIX_TREE_MAX_TAGS
; tag
++)
919 if (tag_get(node
, tag
, offset
+ i
))
926 for (i
= 0; i
< n
; i
++) {
927 struct radix_tree_node
*old
= slot
[i
];
929 rcu_assign_pointer(slot
[i
], child
);
930 for (tag
= 0; tag
< RADIX_TREE_MAX_TAGS
; tag
++)
931 if (tags
& (1 << tag
))
932 tag_clear(node
, tag
, offset
+ i
);
934 rcu_assign_pointer(slot
[i
], item
);
935 for (tag
= 0; tag
< RADIX_TREE_MAX_TAGS
; tag
++)
936 if (tags
& (1 << tag
))
937 tag_set(node
, tag
, offset
);
939 if (radix_tree_is_internal_node(old
) &&
940 !is_sibling_entry(node
, old
) &&
941 (old
!= RADIX_TREE_RETRY
))
942 radix_tree_free_nodes(old
);
943 if (radix_tree_exceptional_entry(old
))
948 if (radix_tree_exceptional_entry(item
))
949 node
->exceptional
+= n
;
954 static inline int insert_entries(struct radix_tree_node
*node
, void **slot
,
955 void *item
, unsigned order
, bool replace
)
959 rcu_assign_pointer(*slot
, item
);
962 if (radix_tree_exceptional_entry(item
))
970 * __radix_tree_insert - insert into a radix tree
971 * @root: radix tree root
973 * @order: key covers the 2^order indices around index
974 * @item: item to insert
976 * Insert an item into the radix tree at position @index.
978 int __radix_tree_insert(struct radix_tree_root
*root
, unsigned long index
,
979 unsigned order
, void *item
)
981 struct radix_tree_node
*node
;
985 BUG_ON(radix_tree_is_internal_node(item
));
987 error
= __radix_tree_create(root
, index
, order
, &node
, &slot
);
991 error
= insert_entries(node
, slot
, item
, order
, false);
996 unsigned offset
= get_slot_offset(node
, slot
);
997 BUG_ON(tag_get(node
, 0, offset
));
998 BUG_ON(tag_get(node
, 1, offset
));
999 BUG_ON(tag_get(node
, 2, offset
));
1001 BUG_ON(root_tags_get(root
));
1006 EXPORT_SYMBOL(__radix_tree_insert
);
1009 * __radix_tree_lookup - lookup an item in a radix tree
1010 * @root: radix tree root
1012 * @nodep: returns node
1013 * @slotp: returns slot
1015 * Lookup and return the item at position @index in the radix
1018 * Until there is more than one item in the tree, no nodes are
1019 * allocated and @root->rnode is used as a direct slot instead of
1020 * pointing to a node, in which case *@nodep will be NULL.
1022 void *__radix_tree_lookup(const struct radix_tree_root
*root
,
1023 unsigned long index
, struct radix_tree_node
**nodep
,
1026 struct radix_tree_node
*node
, *parent
;
1027 unsigned long maxindex
;
1032 slot
= (void **)&root
->rnode
;
1033 radix_tree_load_root(root
, &node
, &maxindex
);
1034 if (index
> maxindex
)
1037 while (radix_tree_is_internal_node(node
)) {
1040 if (node
== RADIX_TREE_RETRY
)
1042 parent
= entry_to_node(node
);
1043 offset
= radix_tree_descend(parent
, &node
, index
);
1044 slot
= parent
->slots
+ offset
;
1055 * radix_tree_lookup_slot - lookup a slot in a radix tree
1056 * @root: radix tree root
1059 * Returns: the slot corresponding to the position @index in the
1060 * radix tree @root. This is useful for update-if-exists operations.
1062 * This function can be called under rcu_read_lock iff the slot is not
1063 * modified by radix_tree_replace_slot, otherwise it must be called
1064 * exclusive from other writers. Any dereference of the slot must be done
1065 * using radix_tree_deref_slot.
1067 void **radix_tree_lookup_slot(const struct radix_tree_root
*root
,
1068 unsigned long index
)
1072 if (!__radix_tree_lookup(root
, index
, NULL
, &slot
))
1076 EXPORT_SYMBOL(radix_tree_lookup_slot
);
1079 * radix_tree_lookup - perform lookup operation on a radix tree
1080 * @root: radix tree root
1083 * Lookup the item at the position @index in the radix tree @root.
1085 * This function can be called under rcu_read_lock, however the caller
1086 * must manage lifetimes of leaf nodes (eg. RCU may also be used to free
1087 * them safely). No RCU barriers are required to access or modify the
1088 * returned item, however.
1090 void *radix_tree_lookup(const struct radix_tree_root
*root
, unsigned long index
)
1092 return __radix_tree_lookup(root
, index
, NULL
, NULL
);
1094 EXPORT_SYMBOL(radix_tree_lookup
);
1096 static inline void replace_sibling_entries(struct radix_tree_node
*node
,
1097 void **slot
, int count
, int exceptional
)
1099 #ifdef CONFIG_RADIX_TREE_MULTIORDER
1100 void *ptr
= node_to_entry(slot
);
1101 unsigned offset
= get_slot_offset(node
, slot
) + 1;
1103 while (offset
< RADIX_TREE_MAP_SIZE
) {
1104 if (node
->slots
[offset
] != ptr
)
1107 node
->slots
[offset
] = NULL
;
1110 node
->exceptional
+= exceptional
;
1116 static void replace_slot(void **slot
, void *item
, struct radix_tree_node
*node
,
1117 int count
, int exceptional
)
1119 if (WARN_ON_ONCE(radix_tree_is_internal_node(item
)))
1122 if (node
&& (count
|| exceptional
)) {
1123 node
->count
+= count
;
1124 node
->exceptional
+= exceptional
;
1125 replace_sibling_entries(node
, slot
, count
, exceptional
);
1128 rcu_assign_pointer(*slot
, item
);
1131 static bool node_tag_get(const struct radix_tree_root
*root
,
1132 const struct radix_tree_node
*node
,
1133 unsigned int tag
, unsigned int offset
)
1136 return tag_get(node
, tag
, offset
);
1137 return root_tag_get(root
, tag
);
1141 * IDR users want to be able to store NULL in the tree, so if the slot isn't
1142 * free, don't adjust the count, even if it's transitioning between NULL and
1143 * non-NULL. For the IDA, we mark slots as being IDR_FREE while they still
1144 * have empty bits, but it only stores NULL in slots when they're being
1147 static int calculate_count(struct radix_tree_root
*root
,
1148 struct radix_tree_node
*node
, void **slot
,
1149 void *item
, void *old
)
1152 unsigned offset
= get_slot_offset(node
, slot
);
1153 bool free
= node_tag_get(root
, node
, IDR_FREE
, offset
);
1159 return !!item
- !!old
;
1163 * __radix_tree_replace - replace item in a slot
1164 * @root: radix tree root
1165 * @node: pointer to tree node
1166 * @slot: pointer to slot in @node
1167 * @item: new item to store in the slot.
1168 * @update_node: callback for changing leaf nodes
1169 * @private: private data to pass to @update_node
1171 * For use with __radix_tree_lookup(). Caller must hold tree write locked
1172 * across slot lookup and replacement.
1174 void __radix_tree_replace(struct radix_tree_root
*root
,
1175 struct radix_tree_node
*node
,
1176 void **slot
, void *item
,
1177 radix_tree_update_node_t update_node
, void *private)
1179 void *old
= rcu_dereference_raw(*slot
);
1180 int exceptional
= !!radix_tree_exceptional_entry(item
) -
1181 !!radix_tree_exceptional_entry(old
);
1182 int count
= calculate_count(root
, node
, slot
, item
, old
);
1185 * This function supports replacing exceptional entries and
1186 * deleting entries, but that needs accounting against the
1187 * node unless the slot is root->rnode.
1189 WARN_ON_ONCE(!node
&& (slot
!= (void **)&root
->rnode
) &&
1190 (count
|| exceptional
));
1191 replace_slot(slot
, item
, node
, count
, exceptional
);
1197 update_node(node
, private);
1199 delete_node(root
, node
, update_node
, private);
1203 * radix_tree_replace_slot - replace item in a slot
1204 * @root: radix tree root
1205 * @slot: pointer to slot
1206 * @item: new item to store in the slot.
1208 * For use with radix_tree_lookup_slot(), radix_tree_gang_lookup_slot(),
1209 * radix_tree_gang_lookup_tag_slot(). Caller must hold tree write locked
1210 * across slot lookup and replacement.
1212 * NOTE: This cannot be used to switch between non-entries (empty slots),
1213 * regular entries, and exceptional entries, as that requires accounting
1214 * inside the radix tree node. When switching from one type of entry or
1215 * deleting, use __radix_tree_lookup() and __radix_tree_replace() or
1216 * radix_tree_iter_replace().
1218 void radix_tree_replace_slot(struct radix_tree_root
*root
,
1219 void **slot
, void *item
)
1221 __radix_tree_replace(root
, NULL
, slot
, item
, NULL
, NULL
);
1225 * radix_tree_iter_replace - replace item in a slot
1226 * @root: radix tree root
1227 * @slot: pointer to slot
1228 * @item: new item to store in the slot.
1230 * For use with radix_tree_split() and radix_tree_for_each_slot().
1231 * Caller must hold tree write locked across split and replacement.
1233 void radix_tree_iter_replace(struct radix_tree_root
*root
,
1234 const struct radix_tree_iter
*iter
, void **slot
, void *item
)
1236 __radix_tree_replace(root
, iter
->node
, slot
, item
, NULL
, NULL
);
1239 #ifdef CONFIG_RADIX_TREE_MULTIORDER
1241 * radix_tree_join - replace multiple entries with one multiorder entry
1242 * @root: radix tree root
1243 * @index: an index inside the new entry
1244 * @order: order of the new entry
1247 * Call this function to replace several entries with one larger entry.
1248 * The existing entries are presumed to not need freeing as a result of
1251 * The replacement entry will have all the tags set on it that were set
1252 * on any of the entries it is replacing.
1254 int radix_tree_join(struct radix_tree_root
*root
, unsigned long index
,
1255 unsigned order
, void *item
)
1257 struct radix_tree_node
*node
;
1261 BUG_ON(radix_tree_is_internal_node(item
));
1263 error
= __radix_tree_create(root
, index
, order
, &node
, &slot
);
1265 error
= insert_entries(node
, slot
, item
, order
, true);
1273 * radix_tree_split - Split an entry into smaller entries
1274 * @root: radix tree root
1275 * @index: An index within the large entry
1276 * @order: Order of new entries
1278 * Call this function as the first step in replacing a multiorder entry
1279 * with several entries of lower order. After this function returns,
1280 * loop over the relevant portion of the tree using radix_tree_for_each_slot()
1281 * and call radix_tree_iter_replace() to set up each new entry.
1283 * The tags from this entry are replicated to all the new entries.
1285 * The radix tree should be locked against modification during the entire
1286 * replacement operation. Lock-free lookups will see RADIX_TREE_RETRY which
1287 * should prompt RCU walkers to restart the lookup from the root.
1289 int radix_tree_split(struct radix_tree_root
*root
, unsigned long index
,
1292 struct radix_tree_node
*parent
, *node
, *child
;
1294 unsigned int offset
, end
;
1295 unsigned n
, tag
, tags
= 0;
1296 gfp_t gfp
= root_gfp_mask(root
);
1298 if (!__radix_tree_lookup(root
, index
, &parent
, &slot
))
1303 offset
= get_slot_offset(parent
, slot
);
1305 for (tag
= 0; tag
< RADIX_TREE_MAX_TAGS
; tag
++)
1306 if (tag_get(parent
, tag
, offset
))
1309 for (end
= offset
+ 1; end
< RADIX_TREE_MAP_SIZE
; end
++) {
1310 if (!is_sibling_entry(parent
, parent
->slots
[end
]))
1312 for (tag
= 0; tag
< RADIX_TREE_MAX_TAGS
; tag
++)
1313 if (tags
& (1 << tag
))
1314 tag_set(parent
, tag
, end
);
1315 /* rcu_assign_pointer ensures tags are set before RETRY */
1316 rcu_assign_pointer(parent
->slots
[end
], RADIX_TREE_RETRY
);
1318 rcu_assign_pointer(parent
->slots
[offset
], RADIX_TREE_RETRY
);
1319 parent
->exceptional
-= (end
- offset
);
1321 if (order
== parent
->shift
)
1323 if (order
> parent
->shift
) {
1324 while (offset
< end
)
1325 offset
+= insert_entries(parent
, &parent
->slots
[offset
],
1326 RADIX_TREE_RETRY
, order
, true);
1333 if (node
->shift
> order
) {
1334 child
= radix_tree_node_alloc(gfp
, node
,
1335 node
->shift
- RADIX_TREE_MAP_SHIFT
,
1339 if (node
!= parent
) {
1341 node
->slots
[offset
] = node_to_entry(child
);
1342 for (tag
= 0; tag
< RADIX_TREE_MAX_TAGS
; tag
++)
1343 if (tags
& (1 << tag
))
1344 tag_set(node
, tag
, offset
);
1352 n
= insert_entries(node
, &node
->slots
[offset
],
1353 RADIX_TREE_RETRY
, order
, false);
1354 BUG_ON(n
> RADIX_TREE_MAP_SIZE
);
1356 for (tag
= 0; tag
< RADIX_TREE_MAX_TAGS
; tag
++)
1357 if (tags
& (1 << tag
))
1358 tag_set(node
, tag
, offset
);
1361 while (offset
== RADIX_TREE_MAP_SIZE
) {
1364 offset
= node
->offset
;
1366 node
= node
->parent
;
1367 rcu_assign_pointer(node
->slots
[offset
],
1368 node_to_entry(child
));
1371 if ((node
== parent
) && (offset
== end
))
1376 /* Shouldn't happen; did user forget to preload? */
1377 /* TODO: free all the allocated nodes */
1383 static void node_tag_set(struct radix_tree_root
*root
,
1384 struct radix_tree_node
*node
,
1385 unsigned int tag
, unsigned int offset
)
1388 if (tag_get(node
, tag
, offset
))
1390 tag_set(node
, tag
, offset
);
1391 offset
= node
->offset
;
1392 node
= node
->parent
;
1395 if (!root_tag_get(root
, tag
))
1396 root_tag_set(root
, tag
);
1400 * radix_tree_tag_set - set a tag on a radix tree node
1401 * @root: radix tree root
1405 * Set the search tag (which must be < RADIX_TREE_MAX_TAGS)
1406 * corresponding to @index in the radix tree. From
1407 * the root all the way down to the leaf node.
1409 * Returns the address of the tagged item. Setting a tag on a not-present
1412 void *radix_tree_tag_set(struct radix_tree_root
*root
,
1413 unsigned long index
, unsigned int tag
)
1415 struct radix_tree_node
*node
, *parent
;
1416 unsigned long maxindex
;
1418 radix_tree_load_root(root
, &node
, &maxindex
);
1419 BUG_ON(index
> maxindex
);
1421 while (radix_tree_is_internal_node(node
)) {
1424 parent
= entry_to_node(node
);
1425 offset
= radix_tree_descend(parent
, &node
, index
);
1428 if (!tag_get(parent
, tag
, offset
))
1429 tag_set(parent
, tag
, offset
);
1432 /* set the root's tag bit */
1433 if (!root_tag_get(root
, tag
))
1434 root_tag_set(root
, tag
);
1438 EXPORT_SYMBOL(radix_tree_tag_set
);
1441 * radix_tree_iter_tag_set - set a tag on the current iterator entry
1442 * @root: radix tree root
1443 * @iter: iterator state
1446 void radix_tree_iter_tag_set(struct radix_tree_root
*root
,
1447 const struct radix_tree_iter
*iter
, unsigned int tag
)
1449 node_tag_set(root
, iter
->node
, tag
, iter_offset(iter
));
1452 static void node_tag_clear(struct radix_tree_root
*root
,
1453 struct radix_tree_node
*node
,
1454 unsigned int tag
, unsigned int offset
)
1457 if (!tag_get(node
, tag
, offset
))
1459 tag_clear(node
, tag
, offset
);
1460 if (any_tag_set(node
, tag
))
1463 offset
= node
->offset
;
1464 node
= node
->parent
;
1467 /* clear the root's tag bit */
1468 if (root_tag_get(root
, tag
))
1469 root_tag_clear(root
, tag
);
1473 * radix_tree_tag_clear - clear a tag on a radix tree node
1474 * @root: radix tree root
1478 * Clear the search tag (which must be < RADIX_TREE_MAX_TAGS)
1479 * corresponding to @index in the radix tree. If this causes
1480 * the leaf node to have no tags set then clear the tag in the
1481 * next-to-leaf node, etc.
1483 * Returns the address of the tagged item on success, else NULL. ie:
1484 * has the same return value and semantics as radix_tree_lookup().
1486 void *radix_tree_tag_clear(struct radix_tree_root
*root
,
1487 unsigned long index
, unsigned int tag
)
1489 struct radix_tree_node
*node
, *parent
;
1490 unsigned long maxindex
;
1491 int uninitialized_var(offset
);
1493 radix_tree_load_root(root
, &node
, &maxindex
);
1494 if (index
> maxindex
)
1499 while (radix_tree_is_internal_node(node
)) {
1500 parent
= entry_to_node(node
);
1501 offset
= radix_tree_descend(parent
, &node
, index
);
1505 node_tag_clear(root
, parent
, tag
, offset
);
1509 EXPORT_SYMBOL(radix_tree_tag_clear
);
1512 * radix_tree_iter_tag_clear - clear a tag on the current iterator entry
1513 * @root: radix tree root
1514 * @iter: iterator state
1515 * @tag: tag to clear
1517 void radix_tree_iter_tag_clear(struct radix_tree_root
*root
,
1518 const struct radix_tree_iter
*iter
, unsigned int tag
)
1520 node_tag_clear(root
, iter
->node
, tag
, iter_offset(iter
));
1524 * radix_tree_tag_get - get a tag on a radix tree node
1525 * @root: radix tree root
1527 * @tag: tag index (< RADIX_TREE_MAX_TAGS)
1531 * 0: tag not present or not set
1534 * Note that the return value of this function may not be relied on, even if
1535 * the RCU lock is held, unless tag modification and node deletion are excluded
1538 int radix_tree_tag_get(const struct radix_tree_root
*root
,
1539 unsigned long index
, unsigned int tag
)
1541 struct radix_tree_node
*node
, *parent
;
1542 unsigned long maxindex
;
1544 if (!root_tag_get(root
, tag
))
1547 radix_tree_load_root(root
, &node
, &maxindex
);
1548 if (index
> maxindex
)
1551 while (radix_tree_is_internal_node(node
)) {
1554 parent
= entry_to_node(node
);
1555 offset
= radix_tree_descend(parent
, &node
, index
);
1557 if (!tag_get(parent
, tag
, offset
))
1559 if (node
== RADIX_TREE_RETRY
)
1565 EXPORT_SYMBOL(radix_tree_tag_get
);
1567 static inline void __set_iter_shift(struct radix_tree_iter
*iter
,
1570 #ifdef CONFIG_RADIX_TREE_MULTIORDER
1571 iter
->shift
= shift
;
1575 /* Construct iter->tags bit-mask from node->tags[tag] array */
1576 static void set_iter_tags(struct radix_tree_iter
*iter
,
1577 struct radix_tree_node
*node
, unsigned offset
,
1580 unsigned tag_long
= offset
/ BITS_PER_LONG
;
1581 unsigned tag_bit
= offset
% BITS_PER_LONG
;
1588 iter
->tags
= node
->tags
[tag
][tag_long
] >> tag_bit
;
1590 /* This never happens if RADIX_TREE_TAG_LONGS == 1 */
1591 if (tag_long
< RADIX_TREE_TAG_LONGS
- 1) {
1592 /* Pick tags from next element */
1594 iter
->tags
|= node
->tags
[tag
][tag_long
+ 1] <<
1595 (BITS_PER_LONG
- tag_bit
);
1596 /* Clip chunk size, here only BITS_PER_LONG tags */
1597 iter
->next_index
= __radix_tree_iter_add(iter
, BITS_PER_LONG
);
1601 #ifdef CONFIG_RADIX_TREE_MULTIORDER
1602 static void **skip_siblings(struct radix_tree_node
**nodep
,
1603 void **slot
, struct radix_tree_iter
*iter
)
1605 void *sib
= node_to_entry(slot
- 1);
1607 while (iter
->index
< iter
->next_index
) {
1608 *nodep
= rcu_dereference_raw(*slot
);
1609 if (*nodep
&& *nodep
!= sib
)
1612 iter
->index
= __radix_tree_iter_add(iter
, 1);
1620 void ** __radix_tree_next_slot(void **slot
, struct radix_tree_iter
*iter
,
1623 unsigned tag
= flags
& RADIX_TREE_ITER_TAG_MASK
;
1624 struct radix_tree_node
*node
= rcu_dereference_raw(*slot
);
1626 slot
= skip_siblings(&node
, slot
, iter
);
1628 while (radix_tree_is_internal_node(node
)) {
1630 unsigned long next_index
;
1632 if (node
== RADIX_TREE_RETRY
)
1634 node
= entry_to_node(node
);
1636 iter
->shift
= node
->shift
;
1638 if (flags
& RADIX_TREE_ITER_TAGGED
) {
1639 offset
= radix_tree_find_next_bit(node
, tag
, 0);
1640 if (offset
== RADIX_TREE_MAP_SIZE
)
1642 slot
= &node
->slots
[offset
];
1643 iter
->index
= __radix_tree_iter_add(iter
, offset
);
1644 set_iter_tags(iter
, node
, offset
, tag
);
1645 node
= rcu_dereference_raw(*slot
);
1648 slot
= &node
->slots
[0];
1650 node
= rcu_dereference_raw(*slot
);
1655 if (offset
== RADIX_TREE_MAP_SIZE
)
1658 iter
->index
= __radix_tree_iter_add(iter
, offset
);
1660 if ((flags
& RADIX_TREE_ITER_CONTIG
) && (offset
> 0))
1662 next_index
= (iter
->index
| shift_maxindex(iter
->shift
)) + 1;
1663 if (next_index
< iter
->next_index
)
1664 iter
->next_index
= next_index
;
1669 iter
->next_index
= 0;
1672 EXPORT_SYMBOL(__radix_tree_next_slot
);
1674 static void **skip_siblings(struct radix_tree_node
**nodep
,
1675 void **slot
, struct radix_tree_iter
*iter
)
1681 void **radix_tree_iter_resume(void **slot
, struct radix_tree_iter
*iter
)
1683 struct radix_tree_node
*node
;
1686 iter
->index
= __radix_tree_iter_add(iter
, 1);
1687 node
= rcu_dereference_raw(*slot
);
1688 skip_siblings(&node
, slot
, iter
);
1689 iter
->next_index
= iter
->index
;
1693 EXPORT_SYMBOL(radix_tree_iter_resume
);
1696 * radix_tree_next_chunk - find next chunk of slots for iteration
1698 * @root: radix tree root
1699 * @iter: iterator state
1700 * @flags: RADIX_TREE_ITER_* flags and tag index
1701 * Returns: pointer to chunk first slot, or NULL if iteration is over
1703 void **radix_tree_next_chunk(const struct radix_tree_root
*root
,
1704 struct radix_tree_iter
*iter
, unsigned flags
)
1706 unsigned tag
= flags
& RADIX_TREE_ITER_TAG_MASK
;
1707 struct radix_tree_node
*node
, *child
;
1708 unsigned long index
, offset
, maxindex
;
1710 if ((flags
& RADIX_TREE_ITER_TAGGED
) && !root_tag_get(root
, tag
))
1714 * Catch next_index overflow after ~0UL. iter->index never overflows
1715 * during iterating; it can be zero only at the beginning.
1716 * And we cannot overflow iter->next_index in a single step,
1717 * because RADIX_TREE_MAP_SHIFT < BITS_PER_LONG.
1719 * This condition also used by radix_tree_next_slot() to stop
1720 * contiguous iterating, and forbid switching to the next chunk.
1722 index
= iter
->next_index
;
1723 if (!index
&& iter
->index
)
1727 radix_tree_load_root(root
, &child
, &maxindex
);
1728 if (index
> maxindex
)
1733 if (!radix_tree_is_internal_node(child
)) {
1734 /* Single-slot tree */
1735 iter
->index
= index
;
1736 iter
->next_index
= maxindex
+ 1;
1739 __set_iter_shift(iter
, 0);
1740 return (void **)&root
->rnode
;
1744 node
= entry_to_node(child
);
1745 offset
= radix_tree_descend(node
, &child
, index
);
1747 if ((flags
& RADIX_TREE_ITER_TAGGED
) ?
1748 !tag_get(node
, tag
, offset
) : !child
) {
1750 if (flags
& RADIX_TREE_ITER_CONTIG
)
1753 if (flags
& RADIX_TREE_ITER_TAGGED
)
1754 offset
= radix_tree_find_next_bit(node
, tag
,
1757 while (++offset
< RADIX_TREE_MAP_SIZE
) {
1758 void *slot
= node
->slots
[offset
];
1759 if (is_sibling_entry(node
, slot
))
1764 index
&= ~node_maxindex(node
);
1765 index
+= offset
<< node
->shift
;
1766 /* Overflow after ~0UL */
1769 if (offset
== RADIX_TREE_MAP_SIZE
)
1771 child
= rcu_dereference_raw(node
->slots
[offset
]);
1776 if (child
== RADIX_TREE_RETRY
)
1778 } while (radix_tree_is_internal_node(child
));
1780 /* Update the iterator state */
1781 iter
->index
= (index
&~ node_maxindex(node
)) | (offset
<< node
->shift
);
1782 iter
->next_index
= (index
| node_maxindex(node
)) + 1;
1784 __set_iter_shift(iter
, node
->shift
);
1786 if (flags
& RADIX_TREE_ITER_TAGGED
)
1787 set_iter_tags(iter
, node
, offset
, tag
);
1789 return node
->slots
+ offset
;
1791 EXPORT_SYMBOL(radix_tree_next_chunk
);
1794 * radix_tree_gang_lookup - perform multiple lookup on a radix tree
1795 * @root: radix tree root
1796 * @results: where the results of the lookup are placed
1797 * @first_index: start the lookup from this key
1798 * @max_items: place up to this many items at *results
1800 * Performs an index-ascending scan of the tree for present items. Places
1801 * them at *@results and returns the number of items which were placed at
1804 * The implementation is naive.
1806 * Like radix_tree_lookup, radix_tree_gang_lookup may be called under
1807 * rcu_read_lock. In this case, rather than the returned results being
1808 * an atomic snapshot of the tree at a single point in time, the
1809 * semantics of an RCU protected gang lookup are as though multiple
1810 * radix_tree_lookups have been issued in individual locks, and results
1811 * stored in 'results'.
1814 radix_tree_gang_lookup(const struct radix_tree_root
*root
, void **results
,
1815 unsigned long first_index
, unsigned int max_items
)
1817 struct radix_tree_iter iter
;
1819 unsigned int ret
= 0;
1821 if (unlikely(!max_items
))
1824 radix_tree_for_each_slot(slot
, root
, &iter
, first_index
) {
1825 results
[ret
] = rcu_dereference_raw(*slot
);
1828 if (radix_tree_is_internal_node(results
[ret
])) {
1829 slot
= radix_tree_iter_retry(&iter
);
1832 if (++ret
== max_items
)
1838 EXPORT_SYMBOL(radix_tree_gang_lookup
);
1841 * radix_tree_gang_lookup_slot - perform multiple slot lookup on radix tree
1842 * @root: radix tree root
1843 * @results: where the results of the lookup are placed
1844 * @indices: where their indices should be placed (but usually NULL)
1845 * @first_index: start the lookup from this key
1846 * @max_items: place up to this many items at *results
1848 * Performs an index-ascending scan of the tree for present items. Places
1849 * their slots at *@results and returns the number of items which were
1850 * placed at *@results.
1852 * The implementation is naive.
1854 * Like radix_tree_gang_lookup as far as RCU and locking goes. Slots must
1855 * be dereferenced with radix_tree_deref_slot, and if using only RCU
1856 * protection, radix_tree_deref_slot may fail requiring a retry.
1859 radix_tree_gang_lookup_slot(const struct radix_tree_root
*root
,
1860 void ***results
, unsigned long *indices
,
1861 unsigned long first_index
, unsigned int max_items
)
1863 struct radix_tree_iter iter
;
1865 unsigned int ret
= 0;
1867 if (unlikely(!max_items
))
1870 radix_tree_for_each_slot(slot
, root
, &iter
, first_index
) {
1871 results
[ret
] = slot
;
1873 indices
[ret
] = iter
.index
;
1874 if (++ret
== max_items
)
1880 EXPORT_SYMBOL(radix_tree_gang_lookup_slot
);
1883 * radix_tree_gang_lookup_tag - perform multiple lookup on a radix tree
1885 * @root: radix tree root
1886 * @results: where the results of the lookup are placed
1887 * @first_index: start the lookup from this key
1888 * @max_items: place up to this many items at *results
1889 * @tag: the tag index (< RADIX_TREE_MAX_TAGS)
1891 * Performs an index-ascending scan of the tree for present items which
1892 * have the tag indexed by @tag set. Places the items at *@results and
1893 * returns the number of items which were placed at *@results.
1896 radix_tree_gang_lookup_tag(const struct radix_tree_root
*root
, void **results
,
1897 unsigned long first_index
, unsigned int max_items
,
1900 struct radix_tree_iter iter
;
1902 unsigned int ret
= 0;
1904 if (unlikely(!max_items
))
1907 radix_tree_for_each_tagged(slot
, root
, &iter
, first_index
, tag
) {
1908 results
[ret
] = rcu_dereference_raw(*slot
);
1911 if (radix_tree_is_internal_node(results
[ret
])) {
1912 slot
= radix_tree_iter_retry(&iter
);
1915 if (++ret
== max_items
)
1921 EXPORT_SYMBOL(radix_tree_gang_lookup_tag
);
1924 * radix_tree_gang_lookup_tag_slot - perform multiple slot lookup on a
1925 * radix tree based on a tag
1926 * @root: radix tree root
1927 * @results: where the results of the lookup are placed
1928 * @first_index: start the lookup from this key
1929 * @max_items: place up to this many items at *results
1930 * @tag: the tag index (< RADIX_TREE_MAX_TAGS)
1932 * Performs an index-ascending scan of the tree for present items which
1933 * have the tag indexed by @tag set. Places the slots at *@results and
1934 * returns the number of slots which were placed at *@results.
1937 radix_tree_gang_lookup_tag_slot(const struct radix_tree_root
*root
,
1938 void ***results
, unsigned long first_index
,
1939 unsigned int max_items
, unsigned int tag
)
1941 struct radix_tree_iter iter
;
1943 unsigned int ret
= 0;
1945 if (unlikely(!max_items
))
1948 radix_tree_for_each_tagged(slot
, root
, &iter
, first_index
, tag
) {
1949 results
[ret
] = slot
;
1950 if (++ret
== max_items
)
1956 EXPORT_SYMBOL(radix_tree_gang_lookup_tag_slot
);
1959 * __radix_tree_delete_node - try to free node after clearing a slot
1960 * @root: radix tree root
1961 * @node: node containing @index
1962 * @update_node: callback for changing leaf nodes
1963 * @private: private data to pass to @update_node
1965 * After clearing the slot at @index in @node from radix tree
1966 * rooted at @root, call this function to attempt freeing the
1967 * node and shrinking the tree.
1969 void __radix_tree_delete_node(struct radix_tree_root
*root
,
1970 struct radix_tree_node
*node
,
1971 radix_tree_update_node_t update_node
,
1974 delete_node(root
, node
, update_node
, private);
1977 static bool __radix_tree_delete(struct radix_tree_root
*root
,
1978 struct radix_tree_node
*node
, void **slot
)
1980 void *old
= rcu_dereference_raw(*slot
);
1981 int exceptional
= radix_tree_exceptional_entry(old
) ? -1 : 0;
1982 unsigned offset
= get_slot_offset(node
, slot
);
1986 node_tag_set(root
, node
, IDR_FREE
, offset
);
1988 for (tag
= 0; tag
< RADIX_TREE_MAX_TAGS
; tag
++)
1989 node_tag_clear(root
, node
, tag
, offset
);
1991 replace_slot(slot
, NULL
, node
, -1, exceptional
);
1992 return node
&& delete_node(root
, node
, NULL
, NULL
);
1996 * radix_tree_iter_delete - delete the entry at this iterator position
1997 * @root: radix tree root
1998 * @iter: iterator state
1999 * @slot: pointer to slot
2001 * Delete the entry at the position currently pointed to by the iterator.
2002 * This may result in the current node being freed; if it is, the iterator
2003 * is advanced so that it will not reference the freed memory. This
2004 * function may be called without any locking if there are no other threads
2005 * which can access this tree.
2007 void radix_tree_iter_delete(struct radix_tree_root
*root
,
2008 struct radix_tree_iter
*iter
, void **slot
)
2010 if (__radix_tree_delete(root
, iter
->node
, slot
))
2011 iter
->index
= iter
->next_index
;
2015 * radix_tree_delete_item - delete an item from a radix tree
2016 * @root: radix tree root
2018 * @item: expected item
2020 * Remove @item at @index from the radix tree rooted at @root.
2022 * Return: the deleted entry, or %NULL if it was not present
2023 * or the entry at the given @index was not @item.
2025 void *radix_tree_delete_item(struct radix_tree_root
*root
,
2026 unsigned long index
, void *item
)
2028 struct radix_tree_node
*node
= NULL
;
2032 entry
= __radix_tree_lookup(root
, index
, &node
, &slot
);
2033 if (!entry
&& (!is_idr(root
) || node_tag_get(root
, node
, IDR_FREE
,
2034 get_slot_offset(node
, slot
))))
2037 if (item
&& entry
!= item
)
2040 __radix_tree_delete(root
, node
, slot
);
2044 EXPORT_SYMBOL(radix_tree_delete_item
);
2047 * radix_tree_delete - delete an entry from a radix tree
2048 * @root: radix tree root
2051 * Remove the entry at @index from the radix tree rooted at @root.
2053 * Return: The deleted entry, or %NULL if it was not present.
2055 void *radix_tree_delete(struct radix_tree_root
*root
, unsigned long index
)
2057 return radix_tree_delete_item(root
, index
, NULL
);
2059 EXPORT_SYMBOL(radix_tree_delete
);
2061 void radix_tree_clear_tags(struct radix_tree_root
*root
,
2062 struct radix_tree_node
*node
,
2066 unsigned int tag
, offset
= get_slot_offset(node
, slot
);
2067 for (tag
= 0; tag
< RADIX_TREE_MAX_TAGS
; tag
++)
2068 node_tag_clear(root
, node
, tag
, offset
);
2070 root_tag_clear_all(root
);
2075 * radix_tree_tagged - test whether any items in the tree are tagged
2076 * @root: radix tree root
2079 int radix_tree_tagged(const struct radix_tree_root
*root
, unsigned int tag
)
2081 return root_tag_get(root
, tag
);
2083 EXPORT_SYMBOL(radix_tree_tagged
);
2086 * idr_preload - preload for idr_alloc()
2087 * @gfp_mask: allocation mask to use for preloading
2089 * Preallocate memory to use for the next call to idr_alloc(). This function
2090 * returns with preemption disabled. It will be enabled by idr_preload_end().
2092 void idr_preload(gfp_t gfp_mask
)
2094 __radix_tree_preload(gfp_mask
, IDR_PRELOAD_SIZE
);
2096 EXPORT_SYMBOL(idr_preload
);
2099 * ida_pre_get - reserve resources for ida allocation
2101 * @gfp: memory allocation flags
2103 * This function should be called before calling ida_get_new_above(). If it
2104 * is unable to allocate memory, it will return %0. On success, it returns %1.
2106 int ida_pre_get(struct ida
*ida
, gfp_t gfp
)
2108 __radix_tree_preload(gfp
, IDA_PRELOAD_SIZE
);
2110 * The IDA API has no preload_end() equivalent. Instead,
2111 * ida_get_new() can return -EAGAIN, prompting the caller
2112 * to return to the ida_pre_get() step.
2116 if (!this_cpu_read(ida_bitmap
)) {
2117 struct ida_bitmap
*bitmap
= kmalloc(sizeof(*bitmap
), gfp
);
2120 bitmap
= this_cpu_cmpxchg(ida_bitmap
, NULL
, bitmap
);
2126 EXPORT_SYMBOL(ida_pre_get
);
2128 void **idr_get_free(struct radix_tree_root
*root
,
2129 struct radix_tree_iter
*iter
, gfp_t gfp
, int end
)
2131 struct radix_tree_node
*node
= NULL
, *child
;
2132 void **slot
= (void **)&root
->rnode
;
2133 unsigned long maxindex
, start
= iter
->next_index
;
2134 unsigned long max
= end
> 0 ? end
- 1 : INT_MAX
;
2135 unsigned int shift
, offset
= 0;
2138 shift
= radix_tree_load_root(root
, &child
, &maxindex
);
2139 if (!radix_tree_tagged(root
, IDR_FREE
))
2140 start
= max(start
, maxindex
+ 1);
2142 return ERR_PTR(-ENOSPC
);
2144 if (start
> maxindex
) {
2145 int error
= radix_tree_extend(root
, gfp
, start
, shift
);
2147 return ERR_PTR(error
);
2149 child
= rcu_dereference_raw(root
->rnode
);
2153 shift
-= RADIX_TREE_MAP_SHIFT
;
2154 if (child
== NULL
) {
2155 /* Have to add a child node. */
2156 child
= radix_tree_node_alloc(gfp
, node
, shift
, offset
,
2159 return ERR_PTR(-ENOMEM
);
2160 all_tag_set(child
, IDR_FREE
);
2161 rcu_assign_pointer(*slot
, node_to_entry(child
));
2164 } else if (!radix_tree_is_internal_node(child
))
2167 node
= entry_to_node(child
);
2168 offset
= radix_tree_descend(node
, &child
, start
);
2169 if (!tag_get(node
, IDR_FREE
, offset
)) {
2170 offset
= radix_tree_find_next_bit(node
, IDR_FREE
,
2172 start
= next_index(start
, node
, offset
);
2174 return ERR_PTR(-ENOSPC
);
2175 while (offset
== RADIX_TREE_MAP_SIZE
) {
2176 offset
= node
->offset
+ 1;
2177 node
= node
->parent
;
2180 shift
= node
->shift
;
2182 child
= rcu_dereference_raw(node
->slots
[offset
]);
2184 slot
= &node
->slots
[offset
];
2187 iter
->index
= start
;
2189 iter
->next_index
= 1 + min(max
, (start
| node_maxindex(node
)));
2191 iter
->next_index
= 1;
2193 __set_iter_shift(iter
, shift
);
2194 set_iter_tags(iter
, node
, offset
, IDR_FREE
);
2200 * idr_destroy - release all internal memory from an IDR
2203 * After this function is called, the IDR is empty, and may be reused or
2204 * the data structure containing it may be freed.
2206 * A typical clean-up sequence for objects stored in an idr tree will use
2207 * idr_for_each() to free all objects, if necessary, then idr_destroy() to
2208 * free the memory used to keep track of those objects.
2210 void idr_destroy(struct idr
*idr
)
2212 struct radix_tree_node
*node
= rcu_dereference_raw(idr
->idr_rt
.rnode
);
2213 if (radix_tree_is_internal_node(node
))
2214 radix_tree_free_nodes(node
);
2215 idr
->idr_rt
.rnode
= NULL
;
2216 root_tag_set(&idr
->idr_rt
, IDR_FREE
);
2218 EXPORT_SYMBOL(idr_destroy
);
2221 radix_tree_node_ctor(void *arg
)
2223 struct radix_tree_node
*node
= arg
;
2225 memset(node
, 0, sizeof(*node
));
2226 INIT_LIST_HEAD(&node
->private_list
);
2229 static __init
unsigned long __maxindex(unsigned int height
)
2231 unsigned int width
= height
* RADIX_TREE_MAP_SHIFT
;
2232 int shift
= RADIX_TREE_INDEX_BITS
- width
;
2236 if (shift
>= BITS_PER_LONG
)
2238 return ~0UL >> shift
;
2241 static __init
void radix_tree_init_maxnodes(void)
2243 unsigned long height_to_maxindex
[RADIX_TREE_MAX_PATH
+ 1];
2246 for (i
= 0; i
< ARRAY_SIZE(height_to_maxindex
); i
++)
2247 height_to_maxindex
[i
] = __maxindex(i
);
2248 for (i
= 0; i
< ARRAY_SIZE(height_to_maxnodes
); i
++) {
2249 for (j
= i
; j
> 0; j
--)
2250 height_to_maxnodes
[i
] += height_to_maxindex
[j
- 1] + 1;
2254 static int radix_tree_cpu_dead(unsigned int cpu
)
2256 struct radix_tree_preload
*rtp
;
2257 struct radix_tree_node
*node
;
2259 /* Free per-cpu pool of preloaded nodes */
2260 rtp
= &per_cpu(radix_tree_preloads
, cpu
);
2263 rtp
->nodes
= node
->private_data
;
2264 kmem_cache_free(radix_tree_node_cachep
, node
);
2267 kfree(per_cpu(ida_bitmap
, cpu
));
2268 per_cpu(ida_bitmap
, cpu
) = NULL
;
2272 void __init
radix_tree_init(void)
2275 radix_tree_node_cachep
= kmem_cache_create("radix_tree_node",
2276 sizeof(struct radix_tree_node
), 0,
2277 SLAB_PANIC
| SLAB_RECLAIM_ACCOUNT
,
2278 radix_tree_node_ctor
);
2279 radix_tree_init_maxnodes();
2280 ret
= cpuhp_setup_state_nocalls(CPUHP_RADIX_DEAD
, "lib/radix:dead",
2281 NULL
, radix_tree_cpu_dead
);