]> git.ipfire.org Git - thirdparty/linux.git/blame - lib/radix-tree.c
radix-tree: delete radix_tree_range_tag_if_tagged()
[thirdparty/linux.git] / lib / radix-tree.c
CommitLineData
1da177e4
LT
1/*
2 * Copyright (C) 2001 Momchil Velikov
3 * Portions Copyright (C) 2001 Christoph Hellwig
cde53535 4 * Copyright (C) 2005 SGI, Christoph Lameter
7cf9c2c7 5 * Copyright (C) 2006 Nick Piggin
78c1d784 6 * Copyright (C) 2012 Konstantin Khlebnikov
6b053b8e
MW
7 * Copyright (C) 2016 Intel, Matthew Wilcox
8 * Copyright (C) 2016 Intel, Ross Zwisler
1da177e4
LT
9 *
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.
14 *
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.
19 *
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.
23 */
24
25#include <linux/errno.h>
26#include <linux/init.h>
27#include <linux/kernel.h>
8bc3bcc9 28#include <linux/export.h>
1da177e4
LT
29#include <linux/radix-tree.h>
30#include <linux/percpu.h>
31#include <linux/slab.h>
ce80b067 32#include <linux/kmemleak.h>
1da177e4
LT
33#include <linux/notifier.h>
34#include <linux/cpu.h>
1da177e4
LT
35#include <linux/string.h>
36#include <linux/bitops.h>
7cf9c2c7 37#include <linux/rcupdate.h>
92cf2118 38#include <linux/preempt.h> /* in_interrupt() */
1da177e4
LT
39
40
c78c66d1
KS
41/* Number of nodes in fully populated tree of given height */
42static unsigned long height_to_maxnodes[RADIX_TREE_MAX_PATH + 1] __read_mostly;
43
1da177e4
LT
44/*
45 * Radix tree node cache.
46 */
e18b890b 47static struct kmem_cache *radix_tree_node_cachep;
1da177e4 48
55368052
NP
49/*
50 * The radix tree is variable-height, so an insert operation not only has
51 * to build the branch to its corresponding item, it also has to build the
52 * branch to existing items if the size has to be increased (by
53 * radix_tree_extend).
54 *
55 * The worst case is a zero height tree with just a single item at index 0,
56 * and then inserting an item at index ULONG_MAX. This requires 2 new branches
57 * of RADIX_TREE_MAX_PATH size to be created, with only the root node shared.
58 * Hence:
59 */
60#define RADIX_TREE_PRELOAD_SIZE (RADIX_TREE_MAX_PATH * 2 - 1)
61
1da177e4
LT
62/*
63 * Per-cpu pool of preloaded nodes
64 */
65struct radix_tree_preload {
2fcd9005 66 unsigned nr;
9d2a8da0
KS
67 /* nodes->private_data points to next preallocated node */
68 struct radix_tree_node *nodes;
1da177e4 69};
8cef7d57 70static DEFINE_PER_CPU(struct radix_tree_preload, radix_tree_preloads) = { 0, };
1da177e4 71
148deab2
MW
72static inline struct radix_tree_node *entry_to_node(void *ptr)
73{
74 return (void *)((unsigned long)ptr & ~RADIX_TREE_INTERNAL_NODE);
75}
76
a4db4dce 77static inline void *node_to_entry(void *ptr)
27d20fdd 78{
30ff46cc 79 return (void *)((unsigned long)ptr | RADIX_TREE_INTERNAL_NODE);
27d20fdd
NP
80}
81
a4db4dce 82#define RADIX_TREE_RETRY node_to_entry(NULL)
afe0e395 83
db050f29
MW
84#ifdef CONFIG_RADIX_TREE_MULTIORDER
85/* Sibling slots point directly to another slot in the same node */
86static inline bool is_sibling_entry(struct radix_tree_node *parent, void *node)
87{
88 void **ptr = node;
89 return (parent->slots <= ptr) &&
90 (ptr < parent->slots + RADIX_TREE_MAP_SIZE);
91}
92#else
93static inline bool is_sibling_entry(struct radix_tree_node *parent, void *node)
94{
95 return false;
96}
97#endif
98
99static inline unsigned long get_slot_offset(struct radix_tree_node *parent,
100 void **slot)
101{
102 return slot - parent->slots;
103}
104
9e85d811
MW
105static unsigned int radix_tree_descend(struct radix_tree_node *parent,
106 struct radix_tree_node **nodep, unsigned long index)
db050f29 107{
9e85d811 108 unsigned int offset = (index >> parent->shift) & RADIX_TREE_MAP_MASK;
db050f29
MW
109 void **entry = rcu_dereference_raw(parent->slots[offset]);
110
111#ifdef CONFIG_RADIX_TREE_MULTIORDER
b194d16c 112 if (radix_tree_is_internal_node(entry)) {
8d2c0d36
LT
113 if (is_sibling_entry(parent, entry)) {
114 void **sibentry = (void **) entry_to_node(entry);
115 offset = get_slot_offset(parent, sibentry);
116 entry = rcu_dereference_raw(*sibentry);
db050f29
MW
117 }
118 }
119#endif
120
121 *nodep = (void *)entry;
122 return offset;
123}
124
612d6c19
NP
125static inline gfp_t root_gfp_mask(struct radix_tree_root *root)
126{
127 return root->gfp_mask & __GFP_BITS_MASK;
128}
129
643b52b9
NP
130static inline void tag_set(struct radix_tree_node *node, unsigned int tag,
131 int offset)
132{
133 __set_bit(offset, node->tags[tag]);
134}
135
136static inline void tag_clear(struct radix_tree_node *node, unsigned int tag,
137 int offset)
138{
139 __clear_bit(offset, node->tags[tag]);
140}
141
142static inline int tag_get(struct radix_tree_node *node, unsigned int tag,
143 int offset)
144{
145 return test_bit(offset, node->tags[tag]);
146}
147
148static inline void root_tag_set(struct radix_tree_root *root, unsigned int tag)
149{
150 root->gfp_mask |= (__force gfp_t)(1 << (tag + __GFP_BITS_SHIFT));
151}
152
2fcd9005 153static inline void root_tag_clear(struct radix_tree_root *root, unsigned tag)
643b52b9
NP
154{
155 root->gfp_mask &= (__force gfp_t)~(1 << (tag + __GFP_BITS_SHIFT));
156}
157
158static inline void root_tag_clear_all(struct radix_tree_root *root)
159{
160 root->gfp_mask &= __GFP_BITS_MASK;
161}
162
163static inline int root_tag_get(struct radix_tree_root *root, unsigned int tag)
164{
2fcd9005 165 return (__force int)root->gfp_mask & (1 << (tag + __GFP_BITS_SHIFT));
643b52b9
NP
166}
167
7b60e9ad
MW
168static inline unsigned root_tags_get(struct radix_tree_root *root)
169{
170 return (__force unsigned)root->gfp_mask >> __GFP_BITS_SHIFT;
171}
172
643b52b9
NP
173/*
174 * Returns 1 if any slot in the node has this tag set.
175 * Otherwise returns 0.
176 */
177static inline int any_tag_set(struct radix_tree_node *node, unsigned int tag)
178{
2fcd9005 179 unsigned idx;
643b52b9
NP
180 for (idx = 0; idx < RADIX_TREE_TAG_LONGS; idx++) {
181 if (node->tags[tag][idx])
182 return 1;
183 }
184 return 0;
185}
78c1d784
KK
186
187/**
188 * radix_tree_find_next_bit - find the next set bit in a memory region
189 *
190 * @addr: The address to base the search on
191 * @size: The bitmap size in bits
192 * @offset: The bitnumber to start searching at
193 *
194 * Unrollable variant of find_next_bit() for constant size arrays.
195 * Tail bits starting from size to roundup(size, BITS_PER_LONG) must be zero.
196 * Returns next bit offset, or size if nothing found.
197 */
198static __always_inline unsigned long
bc412fca
MW
199radix_tree_find_next_bit(struct radix_tree_node *node, unsigned int tag,
200 unsigned long offset)
78c1d784 201{
bc412fca 202 const unsigned long *addr = node->tags[tag];
78c1d784 203
bc412fca 204 if (offset < RADIX_TREE_MAP_SIZE) {
78c1d784
KK
205 unsigned long tmp;
206
207 addr += offset / BITS_PER_LONG;
208 tmp = *addr >> (offset % BITS_PER_LONG);
209 if (tmp)
210 return __ffs(tmp) + offset;
211 offset = (offset + BITS_PER_LONG) & ~(BITS_PER_LONG - 1);
bc412fca 212 while (offset < RADIX_TREE_MAP_SIZE) {
78c1d784
KK
213 tmp = *++addr;
214 if (tmp)
215 return __ffs(tmp) + offset;
216 offset += BITS_PER_LONG;
217 }
218 }
bc412fca 219 return RADIX_TREE_MAP_SIZE;
78c1d784
KK
220}
221
268f42de
MW
222static unsigned int iter_offset(const struct radix_tree_iter *iter)
223{
224 return (iter->index >> iter_shift(iter)) & RADIX_TREE_MAP_MASK;
225}
226
218ed750
MW
227/*
228 * The maximum index which can be stored in a radix tree
229 */
230static inline unsigned long shift_maxindex(unsigned int shift)
231{
232 return (RADIX_TREE_MAP_SIZE << shift) - 1;
233}
234
235static inline unsigned long node_maxindex(struct radix_tree_node *node)
236{
237 return shift_maxindex(node->shift);
238}
239
0796c583 240#ifndef __KERNEL__
d0891265 241static void dump_node(struct radix_tree_node *node, unsigned long index)
7cf19af4 242{
0796c583 243 unsigned long i;
7cf19af4 244
218ed750
MW
245 pr_debug("radix node: %p offset %d indices %lu-%lu parent %p tags %lx %lx %lx shift %d count %d exceptional %d\n",
246 node, node->offset, index, index | node_maxindex(node),
247 node->parent,
0796c583 248 node->tags[0][0], node->tags[1][0], node->tags[2][0],
218ed750 249 node->shift, node->count, node->exceptional);
0796c583
RZ
250
251 for (i = 0; i < RADIX_TREE_MAP_SIZE; i++) {
d0891265
MW
252 unsigned long first = index | (i << node->shift);
253 unsigned long last = first | ((1UL << node->shift) - 1);
0796c583
RZ
254 void *entry = node->slots[i];
255 if (!entry)
256 continue;
218ed750
MW
257 if (entry == RADIX_TREE_RETRY) {
258 pr_debug("radix retry offset %ld indices %lu-%lu parent %p\n",
259 i, first, last, node);
b194d16c 260 } else if (!radix_tree_is_internal_node(entry)) {
218ed750
MW
261 pr_debug("radix entry %p offset %ld indices %lu-%lu parent %p\n",
262 entry, i, first, last, node);
263 } else if (is_sibling_entry(node, entry)) {
264 pr_debug("radix sblng %p offset %ld indices %lu-%lu parent %p val %p\n",
265 entry, i, first, last, node,
266 *(void **)entry_to_node(entry));
0796c583 267 } else {
4dd6c098 268 dump_node(entry_to_node(entry), first);
0796c583
RZ
269 }
270 }
7cf19af4
MW
271}
272
273/* For debug */
274static void radix_tree_dump(struct radix_tree_root *root)
275{
d0891265
MW
276 pr_debug("radix root: %p rnode %p tags %x\n",
277 root, root->rnode,
7cf19af4 278 root->gfp_mask >> __GFP_BITS_SHIFT);
b194d16c 279 if (!radix_tree_is_internal_node(root->rnode))
7cf19af4 280 return;
4dd6c098 281 dump_node(entry_to_node(root->rnode), 0);
7cf19af4
MW
282}
283#endif
284
1da177e4
LT
285/*
286 * This assumes that the caller has performed appropriate preallocation, and
287 * that the caller has pinned this thread of control to the current CPU.
288 */
289static struct radix_tree_node *
290radix_tree_node_alloc(struct radix_tree_root *root)
291{
e2848a0e 292 struct radix_tree_node *ret = NULL;
612d6c19 293 gfp_t gfp_mask = root_gfp_mask(root);
1da177e4 294
5e4c0d97 295 /*
2fcd9005
MW
296 * Preload code isn't irq safe and it doesn't make sense to use
297 * preloading during an interrupt anyway as all the allocations have
298 * to be atomic. So just do normal allocation when in interrupt.
5e4c0d97 299 */
d0164adc 300 if (!gfpflags_allow_blocking(gfp_mask) && !in_interrupt()) {
1da177e4
LT
301 struct radix_tree_preload *rtp;
302
58e698af
VD
303 /*
304 * Even if the caller has preloaded, try to allocate from the
05eb6e72
VD
305 * cache first for the new node to get accounted to the memory
306 * cgroup.
58e698af
VD
307 */
308 ret = kmem_cache_alloc(radix_tree_node_cachep,
05eb6e72 309 gfp_mask | __GFP_NOWARN);
58e698af
VD
310 if (ret)
311 goto out;
312
e2848a0e
NP
313 /*
314 * Provided the caller has preloaded here, we will always
315 * succeed in getting a node here (and never reach
316 * kmem_cache_alloc)
317 */
7c8e0181 318 rtp = this_cpu_ptr(&radix_tree_preloads);
1da177e4 319 if (rtp->nr) {
9d2a8da0
KS
320 ret = rtp->nodes;
321 rtp->nodes = ret->private_data;
322 ret->private_data = NULL;
1da177e4
LT
323 rtp->nr--;
324 }
ce80b067
CM
325 /*
326 * Update the allocation stack trace as this is more useful
327 * for debugging.
328 */
329 kmemleak_update_trace(ret);
58e698af 330 goto out;
1da177e4 331 }
05eb6e72 332 ret = kmem_cache_alloc(radix_tree_node_cachep, gfp_mask);
58e698af 333out:
b194d16c 334 BUG_ON(radix_tree_is_internal_node(ret));
1da177e4
LT
335 return ret;
336}
337
7cf9c2c7
NP
338static void radix_tree_node_rcu_free(struct rcu_head *head)
339{
340 struct radix_tree_node *node =
341 container_of(head, struct radix_tree_node, rcu_head);
b6dd0865 342 int i;
643b52b9
NP
343
344 /*
345 * must only free zeroed nodes into the slab. radix_tree_shrink
346 * can leave us with a non-NULL entry in the first slot, so clear
347 * that here to make sure.
348 */
b6dd0865
DC
349 for (i = 0; i < RADIX_TREE_MAX_TAGS; i++)
350 tag_clear(node, i, 0);
351
643b52b9 352 node->slots[0] = NULL;
91d9c05a 353 INIT_LIST_HEAD(&node->private_list);
643b52b9 354
7cf9c2c7
NP
355 kmem_cache_free(radix_tree_node_cachep, node);
356}
357
1da177e4
LT
358static inline void
359radix_tree_node_free(struct radix_tree_node *node)
360{
7cf9c2c7 361 call_rcu(&node->rcu_head, radix_tree_node_rcu_free);
1da177e4
LT
362}
363
364/*
365 * Load up this CPU's radix_tree_node buffer with sufficient objects to
366 * ensure that the addition of a single element in the tree cannot fail. On
367 * success, return zero, with preemption disabled. On error, return -ENOMEM
368 * with preemption not disabled.
b34df792
DH
369 *
370 * To make use of this facility, the radix tree must be initialised without
d0164adc 371 * __GFP_DIRECT_RECLAIM being passed to INIT_RADIX_TREE().
1da177e4 372 */
c78c66d1 373static int __radix_tree_preload(gfp_t gfp_mask, int nr)
1da177e4
LT
374{
375 struct radix_tree_preload *rtp;
376 struct radix_tree_node *node;
377 int ret = -ENOMEM;
378
05eb6e72
VD
379 /*
380 * Nodes preloaded by one cgroup can be be used by another cgroup, so
381 * they should never be accounted to any particular memory cgroup.
382 */
383 gfp_mask &= ~__GFP_ACCOUNT;
384
1da177e4 385 preempt_disable();
7c8e0181 386 rtp = this_cpu_ptr(&radix_tree_preloads);
c78c66d1 387 while (rtp->nr < nr) {
1da177e4 388 preempt_enable();
488514d1 389 node = kmem_cache_alloc(radix_tree_node_cachep, gfp_mask);
1da177e4
LT
390 if (node == NULL)
391 goto out;
392 preempt_disable();
7c8e0181 393 rtp = this_cpu_ptr(&radix_tree_preloads);
c78c66d1 394 if (rtp->nr < nr) {
9d2a8da0
KS
395 node->private_data = rtp->nodes;
396 rtp->nodes = node;
397 rtp->nr++;
398 } else {
1da177e4 399 kmem_cache_free(radix_tree_node_cachep, node);
9d2a8da0 400 }
1da177e4
LT
401 }
402 ret = 0;
403out:
404 return ret;
405}
5e4c0d97
JK
406
407/*
408 * Load up this CPU's radix_tree_node buffer with sufficient objects to
409 * ensure that the addition of a single element in the tree cannot fail. On
410 * success, return zero, with preemption disabled. On error, return -ENOMEM
411 * with preemption not disabled.
412 *
413 * To make use of this facility, the radix tree must be initialised without
d0164adc 414 * __GFP_DIRECT_RECLAIM being passed to INIT_RADIX_TREE().
5e4c0d97
JK
415 */
416int radix_tree_preload(gfp_t gfp_mask)
417{
418 /* Warn on non-sensical use... */
d0164adc 419 WARN_ON_ONCE(!gfpflags_allow_blocking(gfp_mask));
c78c66d1 420 return __radix_tree_preload(gfp_mask, RADIX_TREE_PRELOAD_SIZE);
5e4c0d97 421}
d7f0923d 422EXPORT_SYMBOL(radix_tree_preload);
1da177e4 423
5e4c0d97
JK
424/*
425 * The same as above function, except we don't guarantee preloading happens.
426 * We do it, if we decide it helps. On success, return zero with preemption
427 * disabled. On error, return -ENOMEM with preemption not disabled.
428 */
429int radix_tree_maybe_preload(gfp_t gfp_mask)
430{
d0164adc 431 if (gfpflags_allow_blocking(gfp_mask))
c78c66d1 432 return __radix_tree_preload(gfp_mask, RADIX_TREE_PRELOAD_SIZE);
5e4c0d97
JK
433 /* Preloading doesn't help anything with this gfp mask, skip it */
434 preempt_disable();
435 return 0;
436}
437EXPORT_SYMBOL(radix_tree_maybe_preload);
438
c78c66d1
KS
439/*
440 * The same as function above, but preload number of nodes required to insert
441 * (1 << order) continuous naturally-aligned elements.
442 */
443int radix_tree_maybe_preload_order(gfp_t gfp_mask, int order)
444{
445 unsigned long nr_subtrees;
446 int nr_nodes, subtree_height;
447
448 /* Preloading doesn't help anything with this gfp mask, skip it */
449 if (!gfpflags_allow_blocking(gfp_mask)) {
450 preempt_disable();
451 return 0;
452 }
453
454 /*
455 * Calculate number and height of fully populated subtrees it takes to
456 * store (1 << order) elements.
457 */
458 nr_subtrees = 1 << order;
459 for (subtree_height = 0; nr_subtrees > RADIX_TREE_MAP_SIZE;
460 subtree_height++)
461 nr_subtrees >>= RADIX_TREE_MAP_SHIFT;
462
463 /*
464 * The worst case is zero height tree with a single item at index 0 and
465 * then inserting items starting at ULONG_MAX - (1 << order).
466 *
467 * This requires RADIX_TREE_MAX_PATH nodes to build branch from root to
468 * 0-index item.
469 */
470 nr_nodes = RADIX_TREE_MAX_PATH;
471
472 /* Plus branch to fully populated subtrees. */
473 nr_nodes += RADIX_TREE_MAX_PATH - subtree_height;
474
475 /* Root node is shared. */
476 nr_nodes--;
477
478 /* Plus nodes required to build subtrees. */
479 nr_nodes += nr_subtrees * height_to_maxnodes[subtree_height];
480
481 return __radix_tree_preload(gfp_mask, nr_nodes);
482}
483
1456a439
MW
484static unsigned radix_tree_load_root(struct radix_tree_root *root,
485 struct radix_tree_node **nodep, unsigned long *maxindex)
486{
487 struct radix_tree_node *node = rcu_dereference_raw(root->rnode);
488
489 *nodep = node;
490
b194d16c 491 if (likely(radix_tree_is_internal_node(node))) {
4dd6c098 492 node = entry_to_node(node);
1456a439 493 *maxindex = node_maxindex(node);
c12e51b0 494 return node->shift + RADIX_TREE_MAP_SHIFT;
1456a439
MW
495 }
496
497 *maxindex = 0;
498 return 0;
499}
500
1da177e4
LT
501/*
502 * Extend a radix tree so it can store key @index.
503 */
e6145236 504static int radix_tree_extend(struct radix_tree_root *root,
d0891265 505 unsigned long index, unsigned int shift)
1da177e4 506{
e2bdb933 507 struct radix_tree_node *slot;
d0891265 508 unsigned int maxshift;
1da177e4
LT
509 int tag;
510
d0891265
MW
511 /* Figure out what the shift should be. */
512 maxshift = shift;
513 while (index > shift_maxindex(maxshift))
514 maxshift += RADIX_TREE_MAP_SHIFT;
1da177e4 515
d0891265
MW
516 slot = root->rnode;
517 if (!slot)
1da177e4 518 goto out;
1da177e4 519
1da177e4 520 do {
2fcd9005
MW
521 struct radix_tree_node *node = radix_tree_node_alloc(root);
522
523 if (!node)
1da177e4
LT
524 return -ENOMEM;
525
1da177e4 526 /* Propagate the aggregated tag info into the new root */
daff89f3 527 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) {
612d6c19 528 if (root_tag_get(root, tag))
1da177e4
LT
529 tag_set(node, tag, 0);
530 }
531
d0891265
MW
532 BUG_ON(shift > BITS_PER_LONG);
533 node->shift = shift;
0c7fa0a8 534 node->offset = 0;
1da177e4 535 node->count = 1;
e2bdb933 536 node->parent = NULL;
f7942430 537 if (radix_tree_is_internal_node(slot)) {
4dd6c098 538 entry_to_node(slot)->parent = node;
f7942430
JW
539 } else {
540 /* Moving an exceptional root->rnode to a node */
541 if (radix_tree_exceptional_entry(slot))
542 node->exceptional = 1;
543 }
e2bdb933 544 node->slots[0] = slot;
a4db4dce
MW
545 slot = node_to_entry(node);
546 rcu_assign_pointer(root->rnode, slot);
d0891265 547 shift += RADIX_TREE_MAP_SHIFT;
d0891265 548 } while (shift <= maxshift);
1da177e4 549out:
d0891265 550 return maxshift + RADIX_TREE_MAP_SHIFT;
1da177e4
LT
551}
552
f4b109c6
JW
553/**
554 * radix_tree_shrink - shrink radix tree to minimum height
555 * @root radix tree root
556 */
14b46879 557static inline void radix_tree_shrink(struct radix_tree_root *root,
4d693d08
JW
558 radix_tree_update_node_t update_node,
559 void *private)
f4b109c6 560{
f4b109c6
JW
561 for (;;) {
562 struct radix_tree_node *node = root->rnode;
563 struct radix_tree_node *child;
564
565 if (!radix_tree_is_internal_node(node))
566 break;
567 node = entry_to_node(node);
568
569 /*
570 * The candidate node has more than one child, or its child
571 * is not at the leftmost slot, or the child is a multiorder
572 * entry, we cannot shrink.
573 */
574 if (node->count != 1)
575 break;
576 child = node->slots[0];
577 if (!child)
578 break;
579 if (!radix_tree_is_internal_node(child) && node->shift)
580 break;
581
582 if (radix_tree_is_internal_node(child))
583 entry_to_node(child)->parent = NULL;
584
585 /*
586 * We don't need rcu_assign_pointer(), since we are simply
587 * moving the node from one part of the tree to another: if it
588 * was safe to dereference the old pointer to it
589 * (node->slots[0]), it will be safe to dereference the new
590 * one (root->rnode) as far as dependent read barriers go.
591 */
592 root->rnode = child;
593
594 /*
595 * We have a dilemma here. The node's slot[0] must not be
596 * NULLed in case there are concurrent lookups expecting to
597 * find the item. However if this was a bottom-level node,
598 * then it may be subject to the slot pointer being visible
599 * to callers dereferencing it. If item corresponding to
600 * slot[0] is subsequently deleted, these callers would expect
601 * their slot to become empty sooner or later.
602 *
603 * For example, lockless pagecache will look up a slot, deref
604 * the page pointer, and if the page has 0 refcount it means it
605 * was concurrently deleted from pagecache so try the deref
606 * again. Fortunately there is already a requirement for logic
607 * to retry the entire slot lookup -- the indirect pointer
608 * problem (replacing direct root node with an indirect pointer
609 * also results in a stale slot). So tag the slot as indirect
610 * to force callers to retry.
611 */
4d693d08
JW
612 node->count = 0;
613 if (!radix_tree_is_internal_node(child)) {
f4b109c6 614 node->slots[0] = RADIX_TREE_RETRY;
4d693d08
JW
615 if (update_node)
616 update_node(node, private);
617 }
f4b109c6
JW
618
619 radix_tree_node_free(node);
f4b109c6 620 }
f4b109c6
JW
621}
622
14b46879 623static void delete_node(struct radix_tree_root *root,
4d693d08
JW
624 struct radix_tree_node *node,
625 radix_tree_update_node_t update_node, void *private)
f4b109c6 626{
f4b109c6
JW
627 do {
628 struct radix_tree_node *parent;
629
630 if (node->count) {
631 if (node == entry_to_node(root->rnode))
14b46879
JW
632 radix_tree_shrink(root, update_node, private);
633 return;
f4b109c6
JW
634 }
635
636 parent = node->parent;
637 if (parent) {
638 parent->slots[node->offset] = NULL;
639 parent->count--;
640 } else {
641 root_tag_clear_all(root);
642 root->rnode = NULL;
643 }
644
645 radix_tree_node_free(node);
f4b109c6
JW
646
647 node = parent;
648 } while (node);
f4b109c6
JW
649}
650
1da177e4 651/**
139e5616 652 * __radix_tree_create - create a slot in a radix tree
1da177e4
LT
653 * @root: radix tree root
654 * @index: index key
e6145236 655 * @order: index occupies 2^order aligned slots
139e5616
JW
656 * @nodep: returns node
657 * @slotp: returns slot
1da177e4 658 *
139e5616
JW
659 * Create, if necessary, and return the node and slot for an item
660 * at position @index in the radix tree @root.
661 *
662 * Until there is more than one item in the tree, no nodes are
663 * allocated and @root->rnode is used as a direct slot instead of
664 * pointing to a node, in which case *@nodep will be NULL.
665 *
666 * Returns -ENOMEM, or 0 for success.
1da177e4 667 */
139e5616 668int __radix_tree_create(struct radix_tree_root *root, unsigned long index,
e6145236
MW
669 unsigned order, struct radix_tree_node **nodep,
670 void ***slotp)
1da177e4 671{
89148aa4
MW
672 struct radix_tree_node *node = NULL, *child;
673 void **slot = (void **)&root->rnode;
49ea6ebc 674 unsigned long maxindex;
89148aa4 675 unsigned int shift, offset = 0;
49ea6ebc
MW
676 unsigned long max = index | ((1UL << order) - 1);
677
89148aa4 678 shift = radix_tree_load_root(root, &child, &maxindex);
1da177e4
LT
679
680 /* Make sure the tree is high enough. */
49ea6ebc 681 if (max > maxindex) {
d0891265 682 int error = radix_tree_extend(root, max, shift);
49ea6ebc 683 if (error < 0)
1da177e4 684 return error;
49ea6ebc 685 shift = error;
89148aa4 686 child = root->rnode;
d0891265 687 if (order == shift)
49ea6ebc 688 shift += RADIX_TREE_MAP_SHIFT;
1da177e4
LT
689 }
690
e6145236 691 while (shift > order) {
c12e51b0 692 shift -= RADIX_TREE_MAP_SHIFT;
89148aa4 693 if (child == NULL) {
1da177e4 694 /* Have to add a child node. */
89148aa4
MW
695 child = radix_tree_node_alloc(root);
696 if (!child)
1da177e4 697 return -ENOMEM;
89148aa4
MW
698 child->shift = shift;
699 child->offset = offset;
700 child->parent = node;
701 rcu_assign_pointer(*slot, node_to_entry(child));
702 if (node)
1da177e4 703 node->count++;
89148aa4 704 } else if (!radix_tree_is_internal_node(child))
e6145236 705 break;
1da177e4
LT
706
707 /* Go a level down */
89148aa4 708 node = entry_to_node(child);
9e85d811 709 offset = radix_tree_descend(node, &child, index);
89148aa4 710 slot = &node->slots[offset];
e6145236
MW
711 }
712
57578c2e 713#ifdef CONFIG_RADIX_TREE_MULTIORDER
e6145236 714 /* Insert pointers to the canonical entry */
3b8c00f6 715 if (order > shift) {
89148aa4 716 unsigned i, n = 1 << (order - shift);
e6145236 717 offset = offset & ~(n - 1);
89148aa4
MW
718 slot = &node->slots[offset];
719 child = node_to_entry(slot);
e6145236 720 for (i = 0; i < n; i++) {
89148aa4 721 if (slot[i])
e6145236
MW
722 return -EEXIST;
723 }
724
725 for (i = 1; i < n; i++) {
89148aa4 726 rcu_assign_pointer(slot[i], child);
e6145236
MW
727 node->count++;
728 }
612d6c19 729 }
57578c2e 730#endif
1da177e4 731
139e5616
JW
732 if (nodep)
733 *nodep = node;
734 if (slotp)
89148aa4 735 *slotp = slot;
139e5616
JW
736 return 0;
737}
738
739/**
e6145236 740 * __radix_tree_insert - insert into a radix tree
139e5616
JW
741 * @root: radix tree root
742 * @index: index key
e6145236 743 * @order: key covers the 2^order indices around index
139e5616
JW
744 * @item: item to insert
745 *
746 * Insert an item into the radix tree at position @index.
747 */
e6145236
MW
748int __radix_tree_insert(struct radix_tree_root *root, unsigned long index,
749 unsigned order, void *item)
139e5616
JW
750{
751 struct radix_tree_node *node;
752 void **slot;
753 int error;
754
b194d16c 755 BUG_ON(radix_tree_is_internal_node(item));
139e5616 756
e6145236 757 error = __radix_tree_create(root, index, order, &node, &slot);
139e5616
JW
758 if (error)
759 return error;
760 if (*slot != NULL)
1da177e4 761 return -EEXIST;
139e5616 762 rcu_assign_pointer(*slot, item);
201b6264 763
612d6c19 764 if (node) {
7b60e9ad 765 unsigned offset = get_slot_offset(node, slot);
612d6c19 766 node->count++;
f7942430
JW
767 if (radix_tree_exceptional_entry(item))
768 node->exceptional++;
7b60e9ad
MW
769 BUG_ON(tag_get(node, 0, offset));
770 BUG_ON(tag_get(node, 1, offset));
771 BUG_ON(tag_get(node, 2, offset));
612d6c19 772 } else {
7b60e9ad 773 BUG_ON(root_tags_get(root));
612d6c19 774 }
1da177e4 775
1da177e4
LT
776 return 0;
777}
e6145236 778EXPORT_SYMBOL(__radix_tree_insert);
1da177e4 779
139e5616
JW
780/**
781 * __radix_tree_lookup - lookup an item in a radix tree
782 * @root: radix tree root
783 * @index: index key
784 * @nodep: returns node
785 * @slotp: returns slot
786 *
787 * Lookup and return the item at position @index in the radix
788 * tree @root.
789 *
790 * Until there is more than one item in the tree, no nodes are
791 * allocated and @root->rnode is used as a direct slot instead of
792 * pointing to a node, in which case *@nodep will be NULL.
7cf9c2c7 793 */
139e5616
JW
794void *__radix_tree_lookup(struct radix_tree_root *root, unsigned long index,
795 struct radix_tree_node **nodep, void ***slotp)
1da177e4 796{
139e5616 797 struct radix_tree_node *node, *parent;
85829954 798 unsigned long maxindex;
139e5616 799 void **slot;
612d6c19 800
85829954
MW
801 restart:
802 parent = NULL;
803 slot = (void **)&root->rnode;
9e85d811 804 radix_tree_load_root(root, &node, &maxindex);
85829954 805 if (index > maxindex)
1da177e4
LT
806 return NULL;
807
b194d16c 808 while (radix_tree_is_internal_node(node)) {
85829954 809 unsigned offset;
1da177e4 810
85829954
MW
811 if (node == RADIX_TREE_RETRY)
812 goto restart;
4dd6c098 813 parent = entry_to_node(node);
9e85d811 814 offset = radix_tree_descend(parent, &node, index);
85829954
MW
815 slot = parent->slots + offset;
816 }
1da177e4 817
139e5616
JW
818 if (nodep)
819 *nodep = parent;
820 if (slotp)
821 *slotp = slot;
822 return node;
b72b71c6
HS
823}
824
825/**
826 * radix_tree_lookup_slot - lookup a slot in a radix tree
827 * @root: radix tree root
828 * @index: index key
829 *
830 * Returns: the slot corresponding to the position @index in the
831 * radix tree @root. This is useful for update-if-exists operations.
832 *
833 * This function can be called under rcu_read_lock iff the slot is not
834 * modified by radix_tree_replace_slot, otherwise it must be called
835 * exclusive from other writers. Any dereference of the slot must be done
836 * using radix_tree_deref_slot.
837 */
838void **radix_tree_lookup_slot(struct radix_tree_root *root, unsigned long index)
839{
139e5616
JW
840 void **slot;
841
842 if (!__radix_tree_lookup(root, index, NULL, &slot))
843 return NULL;
844 return slot;
a4331366 845}
a4331366
HR
846EXPORT_SYMBOL(radix_tree_lookup_slot);
847
848/**
849 * radix_tree_lookup - perform lookup operation on a radix tree
850 * @root: radix tree root
851 * @index: index key
852 *
853 * Lookup the item at the position @index in the radix tree @root.
7cf9c2c7
NP
854 *
855 * This function can be called under rcu_read_lock, however the caller
856 * must manage lifetimes of leaf nodes (eg. RCU may also be used to free
857 * them safely). No RCU barriers are required to access or modify the
858 * returned item, however.
a4331366
HR
859 */
860void *radix_tree_lookup(struct radix_tree_root *root, unsigned long index)
861{
139e5616 862 return __radix_tree_lookup(root, index, NULL, NULL);
1da177e4
LT
863}
864EXPORT_SYMBOL(radix_tree_lookup);
865
6d75f366
JW
866static void replace_slot(struct radix_tree_root *root,
867 struct radix_tree_node *node,
868 void **slot, void *item,
869 bool warn_typeswitch)
f7942430
JW
870{
871 void *old = rcu_dereference_raw(*slot);
f4b109c6 872 int count, exceptional;
f7942430
JW
873
874 WARN_ON_ONCE(radix_tree_is_internal_node(item));
f7942430 875
f4b109c6 876 count = !!item - !!old;
f7942430
JW
877 exceptional = !!radix_tree_exceptional_entry(item) -
878 !!radix_tree_exceptional_entry(old);
879
f4b109c6 880 WARN_ON_ONCE(warn_typeswitch && (count || exceptional));
f7942430 881
f4b109c6
JW
882 if (node) {
883 node->count += count;
f7942430 884 node->exceptional += exceptional;
f4b109c6 885 }
f7942430
JW
886
887 rcu_assign_pointer(*slot, item);
888}
889
6d75f366
JW
890/**
891 * __radix_tree_replace - replace item in a slot
4d693d08
JW
892 * @root: radix tree root
893 * @node: pointer to tree node
894 * @slot: pointer to slot in @node
895 * @item: new item to store in the slot.
896 * @update_node: callback for changing leaf nodes
897 * @private: private data to pass to @update_node
6d75f366
JW
898 *
899 * For use with __radix_tree_lookup(). Caller must hold tree write locked
900 * across slot lookup and replacement.
901 */
902void __radix_tree_replace(struct radix_tree_root *root,
903 struct radix_tree_node *node,
4d693d08
JW
904 void **slot, void *item,
905 radix_tree_update_node_t update_node, void *private)
6d75f366
JW
906{
907 /*
f4b109c6
JW
908 * This function supports replacing exceptional entries and
909 * deleting entries, but that needs accounting against the
910 * node unless the slot is root->rnode.
6d75f366
JW
911 */
912 replace_slot(root, node, slot, item,
913 !node && slot != (void **)&root->rnode);
f4b109c6 914
4d693d08
JW
915 if (!node)
916 return;
917
918 if (update_node)
919 update_node(node, private);
920
921 delete_node(root, node, update_node, private);
6d75f366
JW
922}
923
924/**
925 * radix_tree_replace_slot - replace item in a slot
926 * @root: radix tree root
927 * @slot: pointer to slot
928 * @item: new item to store in the slot.
929 *
930 * For use with radix_tree_lookup_slot(), radix_tree_gang_lookup_slot(),
931 * radix_tree_gang_lookup_tag_slot(). Caller must hold tree write locked
932 * across slot lookup and replacement.
933 *
934 * NOTE: This cannot be used to switch between non-entries (empty slots),
935 * regular entries, and exceptional entries, as that requires accounting
f4b109c6
JW
936 * inside the radix tree node. When switching from one type of entry or
937 * deleting, use __radix_tree_lookup() and __radix_tree_replace().
6d75f366
JW
938 */
939void radix_tree_replace_slot(struct radix_tree_root *root,
940 void **slot, void *item)
941{
942 replace_slot(root, NULL, slot, item, true);
943}
944
1da177e4
LT
945/**
946 * radix_tree_tag_set - set a tag on a radix tree node
947 * @root: radix tree root
948 * @index: index key
2fcd9005 949 * @tag: tag index
1da177e4 950 *
daff89f3
JC
951 * Set the search tag (which must be < RADIX_TREE_MAX_TAGS)
952 * corresponding to @index in the radix tree. From
1da177e4
LT
953 * the root all the way down to the leaf node.
954 *
2fcd9005 955 * Returns the address of the tagged item. Setting a tag on a not-present
1da177e4
LT
956 * item is a bug.
957 */
958void *radix_tree_tag_set(struct radix_tree_root *root,
daff89f3 959 unsigned long index, unsigned int tag)
1da177e4 960{
fb969909
RZ
961 struct radix_tree_node *node, *parent;
962 unsigned long maxindex;
1da177e4 963
9e85d811 964 radix_tree_load_root(root, &node, &maxindex);
fb969909 965 BUG_ON(index > maxindex);
1da177e4 966
b194d16c 967 while (radix_tree_is_internal_node(node)) {
fb969909 968 unsigned offset;
1da177e4 969
4dd6c098 970 parent = entry_to_node(node);
9e85d811 971 offset = radix_tree_descend(parent, &node, index);
fb969909
RZ
972 BUG_ON(!node);
973
974 if (!tag_get(parent, tag, offset))
975 tag_set(parent, tag, offset);
1da177e4
LT
976 }
977
612d6c19 978 /* set the root's tag bit */
fb969909 979 if (!root_tag_get(root, tag))
612d6c19
NP
980 root_tag_set(root, tag);
981
fb969909 982 return node;
1da177e4
LT
983}
984EXPORT_SYMBOL(radix_tree_tag_set);
985
d604c324
MW
986static void node_tag_clear(struct radix_tree_root *root,
987 struct radix_tree_node *node,
988 unsigned int tag, unsigned int offset)
989{
990 while (node) {
991 if (!tag_get(node, tag, offset))
992 return;
993 tag_clear(node, tag, offset);
994 if (any_tag_set(node, tag))
995 return;
996
997 offset = node->offset;
998 node = node->parent;
999 }
1000
1001 /* clear the root's tag bit */
1002 if (root_tag_get(root, tag))
1003 root_tag_clear(root, tag);
1004}
1005
9498d2bb
MW
1006static void node_tag_set(struct radix_tree_root *root,
1007 struct radix_tree_node *node,
1008 unsigned int tag, unsigned int offset)
1009{
1010 while (node) {
1011 if (tag_get(node, tag, offset))
1012 return;
1013 tag_set(node, tag, offset);
1014 offset = node->offset;
1015 node = node->parent;
1016 }
1017
1018 if (!root_tag_get(root, tag))
1019 root_tag_set(root, tag);
1020}
1021
268f42de
MW
1022/**
1023 * radix_tree_iter_tag_set - set a tag on the current iterator entry
1024 * @root: radix tree root
1025 * @iter: iterator state
1026 * @tag: tag to set
1027 */
1028void radix_tree_iter_tag_set(struct radix_tree_root *root,
1029 const struct radix_tree_iter *iter, unsigned int tag)
1030{
1031 node_tag_set(root, iter->node, tag, iter_offset(iter));
1032}
1033
1da177e4
LT
1034/**
1035 * radix_tree_tag_clear - clear a tag on a radix tree node
1036 * @root: radix tree root
1037 * @index: index key
2fcd9005 1038 * @tag: tag index
1da177e4 1039 *
daff89f3 1040 * Clear the search tag (which must be < RADIX_TREE_MAX_TAGS)
2fcd9005
MW
1041 * corresponding to @index in the radix tree. If this causes
1042 * the leaf node to have no tags set then clear the tag in the
1da177e4
LT
1043 * next-to-leaf node, etc.
1044 *
1045 * Returns the address of the tagged item on success, else NULL. ie:
1046 * has the same return value and semantics as radix_tree_lookup().
1047 */
1048void *radix_tree_tag_clear(struct radix_tree_root *root,
daff89f3 1049 unsigned long index, unsigned int tag)
1da177e4 1050{
00f47b58
RZ
1051 struct radix_tree_node *node, *parent;
1052 unsigned long maxindex;
e2bdb933 1053 int uninitialized_var(offset);
1da177e4 1054
9e85d811 1055 radix_tree_load_root(root, &node, &maxindex);
00f47b58
RZ
1056 if (index > maxindex)
1057 return NULL;
1da177e4 1058
00f47b58 1059 parent = NULL;
1da177e4 1060
b194d16c 1061 while (radix_tree_is_internal_node(node)) {
4dd6c098 1062 parent = entry_to_node(node);
9e85d811 1063 offset = radix_tree_descend(parent, &node, index);
1da177e4
LT
1064 }
1065
d604c324
MW
1066 if (node)
1067 node_tag_clear(root, parent, tag, offset);
1da177e4 1068
00f47b58 1069 return node;
1da177e4
LT
1070}
1071EXPORT_SYMBOL(radix_tree_tag_clear);
1072
1da177e4 1073/**
32605a18
MT
1074 * radix_tree_tag_get - get a tag on a radix tree node
1075 * @root: radix tree root
1076 * @index: index key
2fcd9005 1077 * @tag: tag index (< RADIX_TREE_MAX_TAGS)
1da177e4 1078 *
32605a18 1079 * Return values:
1da177e4 1080 *
612d6c19
NP
1081 * 0: tag not present or not set
1082 * 1: tag set
ce82653d
DH
1083 *
1084 * Note that the return value of this function may not be relied on, even if
1085 * the RCU lock is held, unless tag modification and node deletion are excluded
1086 * from concurrency.
1da177e4
LT
1087 */
1088int radix_tree_tag_get(struct radix_tree_root *root,
daff89f3 1089 unsigned long index, unsigned int tag)
1da177e4 1090{
4589ba6d
RZ
1091 struct radix_tree_node *node, *parent;
1092 unsigned long maxindex;
1da177e4 1093
612d6c19
NP
1094 if (!root_tag_get(root, tag))
1095 return 0;
1096
9e85d811 1097 radix_tree_load_root(root, &node, &maxindex);
4589ba6d
RZ
1098 if (index > maxindex)
1099 return 0;
7cf9c2c7
NP
1100 if (node == NULL)
1101 return 0;
1102
b194d16c 1103 while (radix_tree_is_internal_node(node)) {
9e85d811 1104 unsigned offset;
1da177e4 1105
4dd6c098 1106 parent = entry_to_node(node);
9e85d811 1107 offset = radix_tree_descend(parent, &node, index);
1da177e4 1108
4589ba6d 1109 if (!node)
1da177e4 1110 return 0;
4589ba6d 1111 if (!tag_get(parent, tag, offset))
3fa36acb 1112 return 0;
4589ba6d
RZ
1113 if (node == RADIX_TREE_RETRY)
1114 break;
1da177e4 1115 }
4589ba6d
RZ
1116
1117 return 1;
1da177e4
LT
1118}
1119EXPORT_SYMBOL(radix_tree_tag_get);
1da177e4 1120
21ef5339
RZ
1121static inline void __set_iter_shift(struct radix_tree_iter *iter,
1122 unsigned int shift)
1123{
1124#ifdef CONFIG_RADIX_TREE_MULTIORDER
1125 iter->shift = shift;
1126#endif
1127}
1128
148deab2
MW
1129/* Construct iter->tags bit-mask from node->tags[tag] array */
1130static void set_iter_tags(struct radix_tree_iter *iter,
1131 struct radix_tree_node *node, unsigned offset,
1132 unsigned tag)
1133{
1134 unsigned tag_long = offset / BITS_PER_LONG;
1135 unsigned tag_bit = offset % BITS_PER_LONG;
1136
1137 iter->tags = node->tags[tag][tag_long] >> tag_bit;
1138
1139 /* This never happens if RADIX_TREE_TAG_LONGS == 1 */
1140 if (tag_long < RADIX_TREE_TAG_LONGS - 1) {
1141 /* Pick tags from next element */
1142 if (tag_bit)
1143 iter->tags |= node->tags[tag][tag_long + 1] <<
1144 (BITS_PER_LONG - tag_bit);
1145 /* Clip chunk size, here only BITS_PER_LONG tags */
1146 iter->next_index = __radix_tree_iter_add(iter, BITS_PER_LONG);
1147 }
1148}
1149
1150#ifdef CONFIG_RADIX_TREE_MULTIORDER
1151static void **skip_siblings(struct radix_tree_node **nodep,
1152 void **slot, struct radix_tree_iter *iter)
1153{
1154 void *sib = node_to_entry(slot - 1);
1155
1156 while (iter->index < iter->next_index) {
1157 *nodep = rcu_dereference_raw(*slot);
1158 if (*nodep && *nodep != sib)
1159 return slot;
1160 slot++;
1161 iter->index = __radix_tree_iter_add(iter, 1);
1162 iter->tags >>= 1;
1163 }
1164
1165 *nodep = NULL;
1166 return NULL;
1167}
1168
1169void ** __radix_tree_next_slot(void **slot, struct radix_tree_iter *iter,
1170 unsigned flags)
1171{
1172 unsigned tag = flags & RADIX_TREE_ITER_TAG_MASK;
1173 struct radix_tree_node *node = rcu_dereference_raw(*slot);
1174
1175 slot = skip_siblings(&node, slot, iter);
1176
1177 while (radix_tree_is_internal_node(node)) {
1178 unsigned offset;
1179 unsigned long next_index;
1180
1181 if (node == RADIX_TREE_RETRY)
1182 return slot;
1183 node = entry_to_node(node);
268f42de 1184 iter->node = node;
148deab2
MW
1185 iter->shift = node->shift;
1186
1187 if (flags & RADIX_TREE_ITER_TAGGED) {
1188 offset = radix_tree_find_next_bit(node, tag, 0);
1189 if (offset == RADIX_TREE_MAP_SIZE)
1190 return NULL;
1191 slot = &node->slots[offset];
1192 iter->index = __radix_tree_iter_add(iter, offset);
1193 set_iter_tags(iter, node, offset, tag);
1194 node = rcu_dereference_raw(*slot);
1195 } else {
1196 offset = 0;
1197 slot = &node->slots[0];
1198 for (;;) {
1199 node = rcu_dereference_raw(*slot);
1200 if (node)
1201 break;
1202 slot++;
1203 offset++;
1204 if (offset == RADIX_TREE_MAP_SIZE)
1205 return NULL;
1206 }
1207 iter->index = __radix_tree_iter_add(iter, offset);
1208 }
1209 if ((flags & RADIX_TREE_ITER_CONTIG) && (offset > 0))
1210 goto none;
1211 next_index = (iter->index | shift_maxindex(iter->shift)) + 1;
1212 if (next_index < iter->next_index)
1213 iter->next_index = next_index;
1214 }
1215
1216 return slot;
1217 none:
1218 iter->next_index = 0;
1219 return NULL;
1220}
1221EXPORT_SYMBOL(__radix_tree_next_slot);
1222#else
1223static void **skip_siblings(struct radix_tree_node **nodep,
1224 void **slot, struct radix_tree_iter *iter)
1225{
1226 return slot;
1227}
1228#endif
1229
1230void **radix_tree_iter_resume(void **slot, struct radix_tree_iter *iter)
1231{
1232 struct radix_tree_node *node;
1233
1234 slot++;
1235 iter->index = __radix_tree_iter_add(iter, 1);
1236 node = rcu_dereference_raw(*slot);
1237 skip_siblings(&node, slot, iter);
1238 iter->next_index = iter->index;
1239 iter->tags = 0;
1240 return NULL;
1241}
1242EXPORT_SYMBOL(radix_tree_iter_resume);
1243
78c1d784
KK
1244/**
1245 * radix_tree_next_chunk - find next chunk of slots for iteration
1246 *
1247 * @root: radix tree root
1248 * @iter: iterator state
1249 * @flags: RADIX_TREE_ITER_* flags and tag index
1250 * Returns: pointer to chunk first slot, or NULL if iteration is over
1251 */
1252void **radix_tree_next_chunk(struct radix_tree_root *root,
1253 struct radix_tree_iter *iter, unsigned flags)
1254{
9e85d811 1255 unsigned tag = flags & RADIX_TREE_ITER_TAG_MASK;
8c1244de 1256 struct radix_tree_node *node, *child;
21ef5339 1257 unsigned long index, offset, maxindex;
78c1d784
KK
1258
1259 if ((flags & RADIX_TREE_ITER_TAGGED) && !root_tag_get(root, tag))
1260 return NULL;
1261
1262 /*
1263 * Catch next_index overflow after ~0UL. iter->index never overflows
1264 * during iterating; it can be zero only at the beginning.
1265 * And we cannot overflow iter->next_index in a single step,
1266 * because RADIX_TREE_MAP_SHIFT < BITS_PER_LONG.
fffaee36
KK
1267 *
1268 * This condition also used by radix_tree_next_slot() to stop
91b9677c 1269 * contiguous iterating, and forbid switching to the next chunk.
78c1d784
KK
1270 */
1271 index = iter->next_index;
1272 if (!index && iter->index)
1273 return NULL;
1274
21ef5339 1275 restart:
9e85d811 1276 radix_tree_load_root(root, &child, &maxindex);
21ef5339
RZ
1277 if (index > maxindex)
1278 return NULL;
8c1244de
MW
1279 if (!child)
1280 return NULL;
21ef5339 1281
8c1244de 1282 if (!radix_tree_is_internal_node(child)) {
78c1d784 1283 /* Single-slot tree */
21ef5339
RZ
1284 iter->index = index;
1285 iter->next_index = maxindex + 1;
78c1d784 1286 iter->tags = 1;
268f42de 1287 iter->node = NULL;
8c1244de 1288 __set_iter_shift(iter, 0);
78c1d784 1289 return (void **)&root->rnode;
8c1244de 1290 }
21ef5339 1291
8c1244de
MW
1292 do {
1293 node = entry_to_node(child);
9e85d811 1294 offset = radix_tree_descend(node, &child, index);
21ef5339 1295
78c1d784 1296 if ((flags & RADIX_TREE_ITER_TAGGED) ?
8c1244de 1297 !tag_get(node, tag, offset) : !child) {
78c1d784
KK
1298 /* Hole detected */
1299 if (flags & RADIX_TREE_ITER_CONTIG)
1300 return NULL;
1301
1302 if (flags & RADIX_TREE_ITER_TAGGED)
bc412fca 1303 offset = radix_tree_find_next_bit(node, tag,
78c1d784
KK
1304 offset + 1);
1305 else
1306 while (++offset < RADIX_TREE_MAP_SIZE) {
21ef5339
RZ
1307 void *slot = node->slots[offset];
1308 if (is_sibling_entry(node, slot))
1309 continue;
1310 if (slot)
78c1d784
KK
1311 break;
1312 }
8c1244de 1313 index &= ~node_maxindex(node);
9e85d811 1314 index += offset << node->shift;
78c1d784
KK
1315 /* Overflow after ~0UL */
1316 if (!index)
1317 return NULL;
1318 if (offset == RADIX_TREE_MAP_SIZE)
1319 goto restart;
8c1244de 1320 child = rcu_dereference_raw(node->slots[offset]);
78c1d784
KK
1321 }
1322
8c1244de 1323 if ((child == NULL) || (child == RADIX_TREE_RETRY))
78c1d784 1324 goto restart;
8c1244de 1325 } while (radix_tree_is_internal_node(child));
78c1d784
KK
1326
1327 /* Update the iterator state */
8c1244de
MW
1328 iter->index = (index &~ node_maxindex(node)) | (offset << node->shift);
1329 iter->next_index = (index | node_maxindex(node)) + 1;
268f42de 1330 iter->node = node;
9e85d811 1331 __set_iter_shift(iter, node->shift);
78c1d784 1332
148deab2
MW
1333 if (flags & RADIX_TREE_ITER_TAGGED)
1334 set_iter_tags(iter, node, offset, tag);
78c1d784
KK
1335
1336 return node->slots + offset;
1337}
1338EXPORT_SYMBOL(radix_tree_next_chunk);
1339
1da177e4
LT
1340/**
1341 * radix_tree_gang_lookup - perform multiple lookup on a radix tree
1342 * @root: radix tree root
1343 * @results: where the results of the lookup are placed
1344 * @first_index: start the lookup from this key
1345 * @max_items: place up to this many items at *results
1346 *
1347 * Performs an index-ascending scan of the tree for present items. Places
1348 * them at *@results and returns the number of items which were placed at
1349 * *@results.
1350 *
1351 * The implementation is naive.
7cf9c2c7
NP
1352 *
1353 * Like radix_tree_lookup, radix_tree_gang_lookup may be called under
1354 * rcu_read_lock. In this case, rather than the returned results being
2fcd9005
MW
1355 * an atomic snapshot of the tree at a single point in time, the
1356 * semantics of an RCU protected gang lookup are as though multiple
1357 * radix_tree_lookups have been issued in individual locks, and results
1358 * stored in 'results'.
1da177e4
LT
1359 */
1360unsigned int
1361radix_tree_gang_lookup(struct radix_tree_root *root, void **results,
1362 unsigned long first_index, unsigned int max_items)
1363{
cebbd29e
KK
1364 struct radix_tree_iter iter;
1365 void **slot;
1366 unsigned int ret = 0;
7cf9c2c7 1367
cebbd29e 1368 if (unlikely(!max_items))
7cf9c2c7 1369 return 0;
1da177e4 1370
cebbd29e 1371 radix_tree_for_each_slot(slot, root, &iter, first_index) {
46437f9a 1372 results[ret] = rcu_dereference_raw(*slot);
cebbd29e
KK
1373 if (!results[ret])
1374 continue;
b194d16c 1375 if (radix_tree_is_internal_node(results[ret])) {
46437f9a
MW
1376 slot = radix_tree_iter_retry(&iter);
1377 continue;
1378 }
cebbd29e 1379 if (++ret == max_items)
1da177e4 1380 break;
1da177e4 1381 }
7cf9c2c7 1382
1da177e4
LT
1383 return ret;
1384}
1385EXPORT_SYMBOL(radix_tree_gang_lookup);
1386
47feff2c
NP
1387/**
1388 * radix_tree_gang_lookup_slot - perform multiple slot lookup on radix tree
1389 * @root: radix tree root
1390 * @results: where the results of the lookup are placed
6328650b 1391 * @indices: where their indices should be placed (but usually NULL)
47feff2c
NP
1392 * @first_index: start the lookup from this key
1393 * @max_items: place up to this many items at *results
1394 *
1395 * Performs an index-ascending scan of the tree for present items. Places
1396 * their slots at *@results and returns the number of items which were
1397 * placed at *@results.
1398 *
1399 * The implementation is naive.
1400 *
1401 * Like radix_tree_gang_lookup as far as RCU and locking goes. Slots must
1402 * be dereferenced with radix_tree_deref_slot, and if using only RCU
1403 * protection, radix_tree_deref_slot may fail requiring a retry.
1404 */
1405unsigned int
6328650b
HD
1406radix_tree_gang_lookup_slot(struct radix_tree_root *root,
1407 void ***results, unsigned long *indices,
47feff2c
NP
1408 unsigned long first_index, unsigned int max_items)
1409{
cebbd29e
KK
1410 struct radix_tree_iter iter;
1411 void **slot;
1412 unsigned int ret = 0;
47feff2c 1413
cebbd29e 1414 if (unlikely(!max_items))
47feff2c
NP
1415 return 0;
1416
cebbd29e
KK
1417 radix_tree_for_each_slot(slot, root, &iter, first_index) {
1418 results[ret] = slot;
6328650b 1419 if (indices)
cebbd29e
KK
1420 indices[ret] = iter.index;
1421 if (++ret == max_items)
47feff2c 1422 break;
47feff2c
NP
1423 }
1424
1425 return ret;
1426}
1427EXPORT_SYMBOL(radix_tree_gang_lookup_slot);
1428
1da177e4
LT
1429/**
1430 * radix_tree_gang_lookup_tag - perform multiple lookup on a radix tree
1431 * based on a tag
1432 * @root: radix tree root
1433 * @results: where the results of the lookup are placed
1434 * @first_index: start the lookup from this key
1435 * @max_items: place up to this many items at *results
daff89f3 1436 * @tag: the tag index (< RADIX_TREE_MAX_TAGS)
1da177e4
LT
1437 *
1438 * Performs an index-ascending scan of the tree for present items which
1439 * have the tag indexed by @tag set. Places the items at *@results and
1440 * returns the number of items which were placed at *@results.
1441 */
1442unsigned int
1443radix_tree_gang_lookup_tag(struct radix_tree_root *root, void **results,
daff89f3
JC
1444 unsigned long first_index, unsigned int max_items,
1445 unsigned int tag)
1da177e4 1446{
cebbd29e
KK
1447 struct radix_tree_iter iter;
1448 void **slot;
1449 unsigned int ret = 0;
612d6c19 1450
cebbd29e 1451 if (unlikely(!max_items))
7cf9c2c7
NP
1452 return 0;
1453
cebbd29e 1454 radix_tree_for_each_tagged(slot, root, &iter, first_index, tag) {
46437f9a 1455 results[ret] = rcu_dereference_raw(*slot);
cebbd29e
KK
1456 if (!results[ret])
1457 continue;
b194d16c 1458 if (radix_tree_is_internal_node(results[ret])) {
46437f9a
MW
1459 slot = radix_tree_iter_retry(&iter);
1460 continue;
1461 }
cebbd29e 1462 if (++ret == max_items)
1da177e4 1463 break;
1da177e4 1464 }
7cf9c2c7 1465
1da177e4
LT
1466 return ret;
1467}
1468EXPORT_SYMBOL(radix_tree_gang_lookup_tag);
1469
47feff2c
NP
1470/**
1471 * radix_tree_gang_lookup_tag_slot - perform multiple slot lookup on a
1472 * radix tree based on a tag
1473 * @root: radix tree root
1474 * @results: where the results of the lookup are placed
1475 * @first_index: start the lookup from this key
1476 * @max_items: place up to this many items at *results
1477 * @tag: the tag index (< RADIX_TREE_MAX_TAGS)
1478 *
1479 * Performs an index-ascending scan of the tree for present items which
1480 * have the tag indexed by @tag set. Places the slots at *@results and
1481 * returns the number of slots which were placed at *@results.
1482 */
1483unsigned int
1484radix_tree_gang_lookup_tag_slot(struct radix_tree_root *root, void ***results,
1485 unsigned long first_index, unsigned int max_items,
1486 unsigned int tag)
1487{
cebbd29e
KK
1488 struct radix_tree_iter iter;
1489 void **slot;
1490 unsigned int ret = 0;
47feff2c 1491
cebbd29e 1492 if (unlikely(!max_items))
47feff2c
NP
1493 return 0;
1494
cebbd29e
KK
1495 radix_tree_for_each_tagged(slot, root, &iter, first_index, tag) {
1496 results[ret] = slot;
1497 if (++ret == max_items)
47feff2c 1498 break;
47feff2c
NP
1499 }
1500
1501 return ret;
1502}
1503EXPORT_SYMBOL(radix_tree_gang_lookup_tag_slot);
1504
139e5616
JW
1505/**
1506 * __radix_tree_delete_node - try to free node after clearing a slot
1507 * @root: radix tree root
139e5616
JW
1508 * @node: node containing @index
1509 *
1510 * After clearing the slot at @index in @node from radix tree
1511 * rooted at @root, call this function to attempt freeing the
1512 * node and shrinking the tree.
139e5616 1513 */
14b46879 1514void __radix_tree_delete_node(struct radix_tree_root *root,
139e5616
JW
1515 struct radix_tree_node *node)
1516{
14b46879 1517 delete_node(root, node, NULL, NULL);
139e5616
JW
1518}
1519
57578c2e
MW
1520static inline void delete_sibling_entries(struct radix_tree_node *node,
1521 void *ptr, unsigned offset)
1522{
1523#ifdef CONFIG_RADIX_TREE_MULTIORDER
1524 int i;
1525 for (i = 1; offset + i < RADIX_TREE_MAP_SIZE; i++) {
1526 if (node->slots[offset + i] != ptr)
1527 break;
1528 node->slots[offset + i] = NULL;
1529 node->count--;
1530 }
1531#endif
1532}
1533
1da177e4 1534/**
53c59f26 1535 * radix_tree_delete_item - delete an item from a radix tree
1da177e4
LT
1536 * @root: radix tree root
1537 * @index: index key
53c59f26 1538 * @item: expected item
1da177e4 1539 *
53c59f26 1540 * Remove @item at @index from the radix tree rooted at @root.
1da177e4 1541 *
53c59f26
JW
1542 * Returns the address of the deleted item, or NULL if it was not present
1543 * or the entry at the given @index was not @item.
1da177e4 1544 */
53c59f26
JW
1545void *radix_tree_delete_item(struct radix_tree_root *root,
1546 unsigned long index, void *item)
1da177e4 1547{
139e5616 1548 struct radix_tree_node *node;
57578c2e 1549 unsigned int offset;
139e5616
JW
1550 void **slot;
1551 void *entry;
d5274261 1552 int tag;
1da177e4 1553
139e5616
JW
1554 entry = __radix_tree_lookup(root, index, &node, &slot);
1555 if (!entry)
1556 return NULL;
1da177e4 1557
139e5616
JW
1558 if (item && entry != item)
1559 return NULL;
1560
1561 if (!node) {
612d6c19
NP
1562 root_tag_clear_all(root);
1563 root->rnode = NULL;
139e5616 1564 return entry;
612d6c19 1565 }
1da177e4 1566
29e0967c 1567 offset = get_slot_offset(node, slot);
53c59f26 1568
d604c324
MW
1569 /* Clear all tags associated with the item to be deleted. */
1570 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
1571 node_tag_clear(root, node, tag, offset);
1da177e4 1572
a4db4dce 1573 delete_sibling_entries(node, node_to_entry(slot), offset);
4d693d08 1574 __radix_tree_replace(root, node, slot, NULL, NULL, NULL);
612d6c19 1575
139e5616 1576 return entry;
1da177e4 1577}
53c59f26
JW
1578EXPORT_SYMBOL(radix_tree_delete_item);
1579
1580/**
1581 * radix_tree_delete - delete an item from a radix tree
1582 * @root: radix tree root
1583 * @index: index key
1584 *
1585 * Remove the item at @index from the radix tree rooted at @root.
1586 *
1587 * Returns the address of the deleted item, or NULL if it was not present.
1588 */
1589void *radix_tree_delete(struct radix_tree_root *root, unsigned long index)
1590{
1591 return radix_tree_delete_item(root, index, NULL);
1592}
1da177e4
LT
1593EXPORT_SYMBOL(radix_tree_delete);
1594
d3798ae8
JW
1595void radix_tree_clear_tags(struct radix_tree_root *root,
1596 struct radix_tree_node *node,
1597 void **slot)
d604c324 1598{
d604c324
MW
1599 if (node) {
1600 unsigned int tag, offset = get_slot_offset(node, slot);
1601 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
1602 node_tag_clear(root, node, tag, offset);
1603 } else {
1604 /* Clear root node tags */
1605 root->gfp_mask &= __GFP_BITS_MASK;
1606 }
d604c324
MW
1607}
1608
1da177e4
LT
1609/**
1610 * radix_tree_tagged - test whether any items in the tree are tagged
1611 * @root: radix tree root
1612 * @tag: tag to test
1613 */
daff89f3 1614int radix_tree_tagged(struct radix_tree_root *root, unsigned int tag)
1da177e4 1615{
612d6c19 1616 return root_tag_get(root, tag);
1da177e4
LT
1617}
1618EXPORT_SYMBOL(radix_tree_tagged);
1619
1620static void
449dd698 1621radix_tree_node_ctor(void *arg)
1da177e4 1622{
449dd698
JW
1623 struct radix_tree_node *node = arg;
1624
1625 memset(node, 0, sizeof(*node));
1626 INIT_LIST_HEAD(&node->private_list);
1da177e4
LT
1627}
1628
c78c66d1
KS
1629static __init unsigned long __maxindex(unsigned int height)
1630{
1631 unsigned int width = height * RADIX_TREE_MAP_SHIFT;
1632 int shift = RADIX_TREE_INDEX_BITS - width;
1633
1634 if (shift < 0)
1635 return ~0UL;
1636 if (shift >= BITS_PER_LONG)
1637 return 0UL;
1638 return ~0UL >> shift;
1639}
1640
1641static __init void radix_tree_init_maxnodes(void)
1642{
1643 unsigned long height_to_maxindex[RADIX_TREE_MAX_PATH + 1];
1644 unsigned int i, j;
1645
1646 for (i = 0; i < ARRAY_SIZE(height_to_maxindex); i++)
1647 height_to_maxindex[i] = __maxindex(i);
1648 for (i = 0; i < ARRAY_SIZE(height_to_maxnodes); i++) {
1649 for (j = i; j > 0; j--)
1650 height_to_maxnodes[i] += height_to_maxindex[j - 1] + 1;
1651 }
1652}
1653
d544abd5 1654static int radix_tree_cpu_dead(unsigned int cpu)
1da177e4 1655{
2fcd9005
MW
1656 struct radix_tree_preload *rtp;
1657 struct radix_tree_node *node;
1658
1659 /* Free per-cpu pool of preloaded nodes */
d544abd5
SAS
1660 rtp = &per_cpu(radix_tree_preloads, cpu);
1661 while (rtp->nr) {
1662 node = rtp->nodes;
1663 rtp->nodes = node->private_data;
1664 kmem_cache_free(radix_tree_node_cachep, node);
1665 rtp->nr--;
2fcd9005 1666 }
d544abd5 1667 return 0;
1da177e4 1668}
1da177e4
LT
1669
1670void __init radix_tree_init(void)
1671{
d544abd5 1672 int ret;
1da177e4
LT
1673 radix_tree_node_cachep = kmem_cache_create("radix_tree_node",
1674 sizeof(struct radix_tree_node), 0,
488514d1
CL
1675 SLAB_PANIC | SLAB_RECLAIM_ACCOUNT,
1676 radix_tree_node_ctor);
c78c66d1 1677 radix_tree_init_maxnodes();
d544abd5
SAS
1678 ret = cpuhp_setup_state_nocalls(CPUHP_RADIX_DEAD, "lib/radix:dead",
1679 NULL, radix_tree_cpu_dead);
1680 WARN_ON(ret < 0);
1da177e4 1681}