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