]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - lib/radix-tree.c
radix-tree: Add radix_tree_iter_tag_clear()
[mirror_ubuntu-artful-kernel.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 */
14b46879 584static inline void radix_tree_shrink(struct radix_tree_root *root,
4d693d08
JW
585 radix_tree_update_node_t update_node,
586 void *private)
f4b109c6 587{
f4b109c6
JW
588 for (;;) {
589 struct radix_tree_node *node = root->rnode;
590 struct radix_tree_node *child;
591
592 if (!radix_tree_is_internal_node(node))
593 break;
594 node = entry_to_node(node);
595
596 /*
597 * The candidate node has more than one child, or its child
598 * is not at the leftmost slot, or the child is a multiorder
599 * entry, we cannot shrink.
600 */
601 if (node->count != 1)
602 break;
603 child = node->slots[0];
604 if (!child)
605 break;
606 if (!radix_tree_is_internal_node(child) && node->shift)
607 break;
608
609 if (radix_tree_is_internal_node(child))
610 entry_to_node(child)->parent = NULL;
611
612 /*
613 * We don't need rcu_assign_pointer(), since we are simply
614 * moving the node from one part of the tree to another: if it
615 * was safe to dereference the old pointer to it
616 * (node->slots[0]), it will be safe to dereference the new
617 * one (root->rnode) as far as dependent read barriers go.
618 */
619 root->rnode = child;
620
621 /*
622 * We have a dilemma here. The node's slot[0] must not be
623 * NULLed in case there are concurrent lookups expecting to
624 * find the item. However if this was a bottom-level node,
625 * then it may be subject to the slot pointer being visible
626 * to callers dereferencing it. If item corresponding to
627 * slot[0] is subsequently deleted, these callers would expect
628 * their slot to become empty sooner or later.
629 *
630 * For example, lockless pagecache will look up a slot, deref
631 * the page pointer, and if the page has 0 refcount it means it
632 * was concurrently deleted from pagecache so try the deref
633 * again. Fortunately there is already a requirement for logic
634 * to retry the entire slot lookup -- the indirect pointer
635 * problem (replacing direct root node with an indirect pointer
636 * also results in a stale slot). So tag the slot as indirect
637 * to force callers to retry.
638 */
4d693d08
JW
639 node->count = 0;
640 if (!radix_tree_is_internal_node(child)) {
f4b109c6 641 node->slots[0] = RADIX_TREE_RETRY;
4d693d08
JW
642 if (update_node)
643 update_node(node, private);
644 }
f4b109c6 645
ea07b862 646 WARN_ON_ONCE(!list_empty(&node->private_list));
f4b109c6 647 radix_tree_node_free(node);
f4b109c6 648 }
f4b109c6
JW
649}
650
14b46879 651static void delete_node(struct radix_tree_root *root,
4d693d08
JW
652 struct radix_tree_node *node,
653 radix_tree_update_node_t update_node, void *private)
f4b109c6 654{
f4b109c6
JW
655 do {
656 struct radix_tree_node *parent;
657
658 if (node->count) {
659 if (node == entry_to_node(root->rnode))
14b46879
JW
660 radix_tree_shrink(root, update_node, private);
661 return;
f4b109c6
JW
662 }
663
664 parent = node->parent;
665 if (parent) {
666 parent->slots[node->offset] = NULL;
667 parent->count--;
668 } else {
669 root_tag_clear_all(root);
670 root->rnode = NULL;
671 }
672
ea07b862 673 WARN_ON_ONCE(!list_empty(&node->private_list));
f4b109c6 674 radix_tree_node_free(node);
f4b109c6
JW
675
676 node = parent;
677 } while (node);
f4b109c6
JW
678}
679
1da177e4 680/**
139e5616 681 * __radix_tree_create - create a slot in a radix tree
1da177e4
LT
682 * @root: radix tree root
683 * @index: index key
e6145236 684 * @order: index occupies 2^order aligned slots
139e5616
JW
685 * @nodep: returns node
686 * @slotp: returns slot
1da177e4 687 *
139e5616
JW
688 * Create, if necessary, and return the node and slot for an item
689 * at position @index in the radix tree @root.
690 *
691 * Until there is more than one item in the tree, no nodes are
692 * allocated and @root->rnode is used as a direct slot instead of
693 * pointing to a node, in which case *@nodep will be NULL.
694 *
695 * Returns -ENOMEM, or 0 for success.
1da177e4 696 */
139e5616 697int __radix_tree_create(struct radix_tree_root *root, unsigned long index,
e6145236
MW
698 unsigned order, struct radix_tree_node **nodep,
699 void ***slotp)
1da177e4 700{
89148aa4
MW
701 struct radix_tree_node *node = NULL, *child;
702 void **slot = (void **)&root->rnode;
49ea6ebc 703 unsigned long maxindex;
89148aa4 704 unsigned int shift, offset = 0;
49ea6ebc
MW
705 unsigned long max = index | ((1UL << order) - 1);
706
89148aa4 707 shift = radix_tree_load_root(root, &child, &maxindex);
1da177e4
LT
708
709 /* Make sure the tree is high enough. */
175542f5
MW
710 if (order > 0 && max == ((1UL << order) - 1))
711 max++;
49ea6ebc 712 if (max > maxindex) {
d0891265 713 int error = radix_tree_extend(root, max, shift);
49ea6ebc 714 if (error < 0)
1da177e4 715 return error;
49ea6ebc 716 shift = error;
89148aa4 717 child = root->rnode;
1da177e4
LT
718 }
719
e6145236 720 while (shift > order) {
c12e51b0 721 shift -= RADIX_TREE_MAP_SHIFT;
89148aa4 722 if (child == NULL) {
1da177e4 723 /* Have to add a child node. */
e8de4340
MW
724 child = radix_tree_node_alloc(root, node, shift,
725 offset, 0, 0);
89148aa4 726 if (!child)
1da177e4 727 return -ENOMEM;
89148aa4
MW
728 rcu_assign_pointer(*slot, node_to_entry(child));
729 if (node)
1da177e4 730 node->count++;
89148aa4 731 } else if (!radix_tree_is_internal_node(child))
e6145236 732 break;
1da177e4
LT
733
734 /* Go a level down */
89148aa4 735 node = entry_to_node(child);
9e85d811 736 offset = radix_tree_descend(node, &child, index);
89148aa4 737 slot = &node->slots[offset];
e6145236
MW
738 }
739
175542f5
MW
740 if (nodep)
741 *nodep = node;
742 if (slotp)
743 *slotp = slot;
744 return 0;
745}
746
57578c2e 747#ifdef CONFIG_RADIX_TREE_MULTIORDER
175542f5
MW
748/*
749 * Free any nodes below this node. The tree is presumed to not need
750 * shrinking, and any user data in the tree is presumed to not need a
751 * destructor called on it. If we need to add a destructor, we can
752 * add that functionality later. Note that we may not clear tags or
753 * slots from the tree as an RCU walker may still have a pointer into
754 * this subtree. We could replace the entries with RADIX_TREE_RETRY,
755 * but we'll still have to clear those in rcu_free.
756 */
757static void radix_tree_free_nodes(struct radix_tree_node *node)
758{
759 unsigned offset = 0;
760 struct radix_tree_node *child = entry_to_node(node);
761
762 for (;;) {
763 void *entry = child->slots[offset];
764 if (radix_tree_is_internal_node(entry) &&
765 !is_sibling_entry(child, entry)) {
766 child = entry_to_node(entry);
767 offset = 0;
768 continue;
769 }
770 offset++;
771 while (offset == RADIX_TREE_MAP_SIZE) {
772 struct radix_tree_node *old = child;
773 offset = child->offset + 1;
774 child = child->parent;
dd040b6f 775 WARN_ON_ONCE(!list_empty(&old->private_list));
175542f5
MW
776 radix_tree_node_free(old);
777 if (old == entry_to_node(node))
778 return;
779 }
780 }
781}
782
783static inline int insert_entries(struct radix_tree_node *node, void **slot,
784 void *item, unsigned order, bool replace)
785{
786 struct radix_tree_node *child;
787 unsigned i, n, tag, offset, tags = 0;
788
789 if (node) {
e157b555
MW
790 if (order > node->shift)
791 n = 1 << (order - node->shift);
792 else
793 n = 1;
175542f5
MW
794 offset = get_slot_offset(node, slot);
795 } else {
796 n = 1;
797 offset = 0;
798 }
799
800 if (n > 1) {
e6145236 801 offset = offset & ~(n - 1);
89148aa4 802 slot = &node->slots[offset];
175542f5
MW
803 }
804 child = node_to_entry(slot);
805
806 for (i = 0; i < n; i++) {
807 if (slot[i]) {
808 if (replace) {
809 node->count--;
810 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
811 if (tag_get(node, tag, offset + i))
812 tags |= 1 << tag;
813 } else
e6145236
MW
814 return -EEXIST;
815 }
175542f5 816 }
e6145236 817
175542f5
MW
818 for (i = 0; i < n; i++) {
819 struct radix_tree_node *old = slot[i];
820 if (i) {
89148aa4 821 rcu_assign_pointer(slot[i], child);
175542f5
MW
822 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
823 if (tags & (1 << tag))
824 tag_clear(node, tag, offset + i);
825 } else {
826 rcu_assign_pointer(slot[i], item);
827 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
828 if (tags & (1 << tag))
829 tag_set(node, tag, offset);
e6145236 830 }
175542f5 831 if (radix_tree_is_internal_node(old) &&
e157b555
MW
832 !is_sibling_entry(node, old) &&
833 (old != RADIX_TREE_RETRY))
175542f5
MW
834 radix_tree_free_nodes(old);
835 if (radix_tree_exceptional_entry(old))
836 node->exceptional--;
612d6c19 837 }
175542f5
MW
838 if (node) {
839 node->count += n;
840 if (radix_tree_exceptional_entry(item))
841 node->exceptional += n;
842 }
843 return n;
139e5616 844}
175542f5
MW
845#else
846static inline int insert_entries(struct radix_tree_node *node, void **slot,
847 void *item, unsigned order, bool replace)
848{
849 if (*slot)
850 return -EEXIST;
851 rcu_assign_pointer(*slot, item);
852 if (node) {
853 node->count++;
854 if (radix_tree_exceptional_entry(item))
855 node->exceptional++;
856 }
857 return 1;
858}
859#endif
139e5616
JW
860
861/**
e6145236 862 * __radix_tree_insert - insert into a radix tree
139e5616
JW
863 * @root: radix tree root
864 * @index: index key
e6145236 865 * @order: key covers the 2^order indices around index
139e5616
JW
866 * @item: item to insert
867 *
868 * Insert an item into the radix tree at position @index.
869 */
e6145236
MW
870int __radix_tree_insert(struct radix_tree_root *root, unsigned long index,
871 unsigned order, void *item)
139e5616
JW
872{
873 struct radix_tree_node *node;
874 void **slot;
875 int error;
876
b194d16c 877 BUG_ON(radix_tree_is_internal_node(item));
139e5616 878
e6145236 879 error = __radix_tree_create(root, index, order, &node, &slot);
139e5616
JW
880 if (error)
881 return error;
175542f5
MW
882
883 error = insert_entries(node, slot, item, order, false);
884 if (error < 0)
885 return error;
201b6264 886
612d6c19 887 if (node) {
7b60e9ad 888 unsigned offset = get_slot_offset(node, slot);
7b60e9ad
MW
889 BUG_ON(tag_get(node, 0, offset));
890 BUG_ON(tag_get(node, 1, offset));
891 BUG_ON(tag_get(node, 2, offset));
612d6c19 892 } else {
7b60e9ad 893 BUG_ON(root_tags_get(root));
612d6c19 894 }
1da177e4 895
1da177e4
LT
896 return 0;
897}
e6145236 898EXPORT_SYMBOL(__radix_tree_insert);
1da177e4 899
139e5616
JW
900/**
901 * __radix_tree_lookup - lookup an item in a radix tree
902 * @root: radix tree root
903 * @index: index key
904 * @nodep: returns node
905 * @slotp: returns slot
906 *
907 * Lookup and return the item at position @index in the radix
908 * tree @root.
909 *
910 * Until there is more than one item in the tree, no nodes are
911 * allocated and @root->rnode is used as a direct slot instead of
912 * pointing to a node, in which case *@nodep will be NULL.
7cf9c2c7 913 */
35534c86
MW
914void *__radix_tree_lookup(const struct radix_tree_root *root,
915 unsigned long index, struct radix_tree_node **nodep,
916 void ***slotp)
1da177e4 917{
139e5616 918 struct radix_tree_node *node, *parent;
85829954 919 unsigned long maxindex;
139e5616 920 void **slot;
612d6c19 921
85829954
MW
922 restart:
923 parent = NULL;
924 slot = (void **)&root->rnode;
9e85d811 925 radix_tree_load_root(root, &node, &maxindex);
85829954 926 if (index > maxindex)
1da177e4
LT
927 return NULL;
928
b194d16c 929 while (radix_tree_is_internal_node(node)) {
85829954 930 unsigned offset;
1da177e4 931
85829954
MW
932 if (node == RADIX_TREE_RETRY)
933 goto restart;
4dd6c098 934 parent = entry_to_node(node);
9e85d811 935 offset = radix_tree_descend(parent, &node, index);
85829954
MW
936 slot = parent->slots + offset;
937 }
1da177e4 938
139e5616
JW
939 if (nodep)
940 *nodep = parent;
941 if (slotp)
942 *slotp = slot;
943 return node;
b72b71c6
HS
944}
945
946/**
947 * radix_tree_lookup_slot - lookup a slot in a radix tree
948 * @root: radix tree root
949 * @index: index key
950 *
951 * Returns: the slot corresponding to the position @index in the
952 * radix tree @root. This is useful for update-if-exists operations.
953 *
954 * This function can be called under rcu_read_lock iff the slot is not
955 * modified by radix_tree_replace_slot, otherwise it must be called
956 * exclusive from other writers. Any dereference of the slot must be done
957 * using radix_tree_deref_slot.
958 */
35534c86
MW
959void **radix_tree_lookup_slot(const struct radix_tree_root *root,
960 unsigned long index)
b72b71c6 961{
139e5616
JW
962 void **slot;
963
964 if (!__radix_tree_lookup(root, index, NULL, &slot))
965 return NULL;
966 return slot;
a4331366 967}
a4331366
HR
968EXPORT_SYMBOL(radix_tree_lookup_slot);
969
970/**
971 * radix_tree_lookup - perform lookup operation on a radix tree
972 * @root: radix tree root
973 * @index: index key
974 *
975 * Lookup the item at the position @index in the radix tree @root.
7cf9c2c7
NP
976 *
977 * This function can be called under rcu_read_lock, however the caller
978 * must manage lifetimes of leaf nodes (eg. RCU may also be used to free
979 * them safely). No RCU barriers are required to access or modify the
980 * returned item, however.
a4331366 981 */
35534c86 982void *radix_tree_lookup(const struct radix_tree_root *root, unsigned long index)
a4331366 983{
139e5616 984 return __radix_tree_lookup(root, index, NULL, NULL);
1da177e4
LT
985}
986EXPORT_SYMBOL(radix_tree_lookup);
987
a90eb3a2
MW
988static inline int slot_count(struct radix_tree_node *node,
989 void **slot)
990{
991 int n = 1;
992#ifdef CONFIG_RADIX_TREE_MULTIORDER
993 void *ptr = node_to_entry(slot);
994 unsigned offset = get_slot_offset(node, slot);
995 int i;
996
997 for (i = 1; offset + i < RADIX_TREE_MAP_SIZE; i++) {
998 if (node->slots[offset + i] != ptr)
999 break;
1000 n++;
1001 }
1002#endif
1003 return n;
1004}
1005
6d75f366
JW
1006static void replace_slot(struct radix_tree_root *root,
1007 struct radix_tree_node *node,
1008 void **slot, void *item,
1009 bool warn_typeswitch)
f7942430
JW
1010{
1011 void *old = rcu_dereference_raw(*slot);
f4b109c6 1012 int count, exceptional;
f7942430
JW
1013
1014 WARN_ON_ONCE(radix_tree_is_internal_node(item));
f7942430 1015
f4b109c6 1016 count = !!item - !!old;
f7942430
JW
1017 exceptional = !!radix_tree_exceptional_entry(item) -
1018 !!radix_tree_exceptional_entry(old);
1019
f4b109c6 1020 WARN_ON_ONCE(warn_typeswitch && (count || exceptional));
f7942430 1021
f4b109c6
JW
1022 if (node) {
1023 node->count += count;
a90eb3a2
MW
1024 if (exceptional) {
1025 exceptional *= slot_count(node, slot);
1026 node->exceptional += exceptional;
1027 }
f4b109c6 1028 }
f7942430
JW
1029
1030 rcu_assign_pointer(*slot, item);
1031}
1032
a90eb3a2
MW
1033static inline void delete_sibling_entries(struct radix_tree_node *node,
1034 void **slot)
1035{
1036#ifdef CONFIG_RADIX_TREE_MULTIORDER
1037 bool exceptional = radix_tree_exceptional_entry(*slot);
1038 void *ptr = node_to_entry(slot);
1039 unsigned offset = get_slot_offset(node, slot);
1040 int i;
1041
1042 for (i = 1; offset + i < RADIX_TREE_MAP_SIZE; i++) {
1043 if (node->slots[offset + i] != ptr)
1044 break;
1045 node->slots[offset + i] = NULL;
1046 node->count--;
1047 if (exceptional)
1048 node->exceptional--;
1049 }
1050#endif
1051}
1052
6d75f366
JW
1053/**
1054 * __radix_tree_replace - replace item in a slot
4d693d08
JW
1055 * @root: radix tree root
1056 * @node: pointer to tree node
1057 * @slot: pointer to slot in @node
1058 * @item: new item to store in the slot.
1059 * @update_node: callback for changing leaf nodes
1060 * @private: private data to pass to @update_node
6d75f366
JW
1061 *
1062 * For use with __radix_tree_lookup(). Caller must hold tree write locked
1063 * across slot lookup and replacement.
1064 */
1065void __radix_tree_replace(struct radix_tree_root *root,
1066 struct radix_tree_node *node,
4d693d08
JW
1067 void **slot, void *item,
1068 radix_tree_update_node_t update_node, void *private)
6d75f366 1069{
a90eb3a2
MW
1070 if (!item)
1071 delete_sibling_entries(node, slot);
6d75f366 1072 /*
f4b109c6
JW
1073 * This function supports replacing exceptional entries and
1074 * deleting entries, but that needs accounting against the
1075 * node unless the slot is root->rnode.
6d75f366
JW
1076 */
1077 replace_slot(root, node, slot, item,
1078 !node && slot != (void **)&root->rnode);
f4b109c6 1079
4d693d08
JW
1080 if (!node)
1081 return;
1082
1083 if (update_node)
1084 update_node(node, private);
1085
1086 delete_node(root, node, update_node, private);
6d75f366
JW
1087}
1088
1089/**
1090 * radix_tree_replace_slot - replace item in a slot
1091 * @root: radix tree root
1092 * @slot: pointer to slot
1093 * @item: new item to store in the slot.
1094 *
1095 * For use with radix_tree_lookup_slot(), radix_tree_gang_lookup_slot(),
1096 * radix_tree_gang_lookup_tag_slot(). Caller must hold tree write locked
1097 * across slot lookup and replacement.
1098 *
1099 * NOTE: This cannot be used to switch between non-entries (empty slots),
1100 * regular entries, and exceptional entries, as that requires accounting
f4b109c6 1101 * inside the radix tree node. When switching from one type of entry or
e157b555
MW
1102 * deleting, use __radix_tree_lookup() and __radix_tree_replace() or
1103 * radix_tree_iter_replace().
6d75f366
JW
1104 */
1105void radix_tree_replace_slot(struct radix_tree_root *root,
1106 void **slot, void *item)
1107{
1108 replace_slot(root, NULL, slot, item, true);
1109}
1110
e157b555
MW
1111/**
1112 * radix_tree_iter_replace - replace item in a slot
1113 * @root: radix tree root
1114 * @slot: pointer to slot
1115 * @item: new item to store in the slot.
1116 *
1117 * For use with radix_tree_split() and radix_tree_for_each_slot().
1118 * Caller must hold tree write locked across split and replacement.
1119 */
1120void radix_tree_iter_replace(struct radix_tree_root *root,
1121 const struct radix_tree_iter *iter, void **slot, void *item)
1122{
1123 __radix_tree_replace(root, iter->node, slot, item, NULL, NULL);
1124}
1125
175542f5
MW
1126#ifdef CONFIG_RADIX_TREE_MULTIORDER
1127/**
1128 * radix_tree_join - replace multiple entries with one multiorder entry
1129 * @root: radix tree root
1130 * @index: an index inside the new entry
1131 * @order: order of the new entry
1132 * @item: new entry
1133 *
1134 * Call this function to replace several entries with one larger entry.
1135 * The existing entries are presumed to not need freeing as a result of
1136 * this call.
1137 *
1138 * The replacement entry will have all the tags set on it that were set
1139 * on any of the entries it is replacing.
1140 */
1141int radix_tree_join(struct radix_tree_root *root, unsigned long index,
1142 unsigned order, void *item)
1143{
1144 struct radix_tree_node *node;
1145 void **slot;
1146 int error;
1147
1148 BUG_ON(radix_tree_is_internal_node(item));
1149
1150 error = __radix_tree_create(root, index, order, &node, &slot);
1151 if (!error)
1152 error = insert_entries(node, slot, item, order, true);
1153 if (error > 0)
1154 error = 0;
1155
1156 return error;
1157}
e157b555
MW
1158
1159/**
1160 * radix_tree_split - Split an entry into smaller entries
1161 * @root: radix tree root
1162 * @index: An index within the large entry
1163 * @order: Order of new entries
1164 *
1165 * Call this function as the first step in replacing a multiorder entry
1166 * with several entries of lower order. After this function returns,
1167 * loop over the relevant portion of the tree using radix_tree_for_each_slot()
1168 * and call radix_tree_iter_replace() to set up each new entry.
1169 *
1170 * The tags from this entry are replicated to all the new entries.
1171 *
1172 * The radix tree should be locked against modification during the entire
1173 * replacement operation. Lock-free lookups will see RADIX_TREE_RETRY which
1174 * should prompt RCU walkers to restart the lookup from the root.
1175 */
1176int radix_tree_split(struct radix_tree_root *root, unsigned long index,
1177 unsigned order)
1178{
1179 struct radix_tree_node *parent, *node, *child;
1180 void **slot;
1181 unsigned int offset, end;
1182 unsigned n, tag, tags = 0;
1183
1184 if (!__radix_tree_lookup(root, index, &parent, &slot))
1185 return -ENOENT;
1186 if (!parent)
1187 return -ENOENT;
1188
1189 offset = get_slot_offset(parent, slot);
1190
1191 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
1192 if (tag_get(parent, tag, offset))
1193 tags |= 1 << tag;
1194
1195 for (end = offset + 1; end < RADIX_TREE_MAP_SIZE; end++) {
1196 if (!is_sibling_entry(parent, parent->slots[end]))
1197 break;
1198 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
1199 if (tags & (1 << tag))
1200 tag_set(parent, tag, end);
1201 /* rcu_assign_pointer ensures tags are set before RETRY */
1202 rcu_assign_pointer(parent->slots[end], RADIX_TREE_RETRY);
1203 }
1204 rcu_assign_pointer(parent->slots[offset], RADIX_TREE_RETRY);
1205 parent->exceptional -= (end - offset);
1206
1207 if (order == parent->shift)
1208 return 0;
1209 if (order > parent->shift) {
1210 while (offset < end)
1211 offset += insert_entries(parent, &parent->slots[offset],
1212 RADIX_TREE_RETRY, order, true);
1213 return 0;
1214 }
1215
1216 node = parent;
1217
1218 for (;;) {
1219 if (node->shift > order) {
e8de4340
MW
1220 child = radix_tree_node_alloc(root, node,
1221 node->shift - RADIX_TREE_MAP_SHIFT,
1222 offset, 0, 0);
e157b555
MW
1223 if (!child)
1224 goto nomem;
e157b555
MW
1225 if (node != parent) {
1226 node->count++;
1227 node->slots[offset] = node_to_entry(child);
1228 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
1229 if (tags & (1 << tag))
1230 tag_set(node, tag, offset);
1231 }
1232
1233 node = child;
1234 offset = 0;
1235 continue;
1236 }
1237
1238 n = insert_entries(node, &node->slots[offset],
1239 RADIX_TREE_RETRY, order, false);
1240 BUG_ON(n > RADIX_TREE_MAP_SIZE);
1241
1242 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
1243 if (tags & (1 << tag))
1244 tag_set(node, tag, offset);
1245 offset += n;
1246
1247 while (offset == RADIX_TREE_MAP_SIZE) {
1248 if (node == parent)
1249 break;
1250 offset = node->offset;
1251 child = node;
1252 node = node->parent;
1253 rcu_assign_pointer(node->slots[offset],
1254 node_to_entry(child));
1255 offset++;
1256 }
1257 if ((node == parent) && (offset == end))
1258 return 0;
1259 }
1260
1261 nomem:
1262 /* Shouldn't happen; did user forget to preload? */
1263 /* TODO: free all the allocated nodes */
1264 WARN_ON(1);
1265 return -ENOMEM;
1266}
175542f5
MW
1267#endif
1268
30b888ba
MW
1269static void node_tag_set(struct radix_tree_root *root,
1270 struct radix_tree_node *node,
1271 unsigned int tag, unsigned int offset)
1272{
1273 while (node) {
1274 if (tag_get(node, tag, offset))
1275 return;
1276 tag_set(node, tag, offset);
1277 offset = node->offset;
1278 node = node->parent;
1279 }
1280
1281 if (!root_tag_get(root, tag))
1282 root_tag_set(root, tag);
1283}
1284
1da177e4
LT
1285/**
1286 * radix_tree_tag_set - set a tag on a radix tree node
1287 * @root: radix tree root
1288 * @index: index key
2fcd9005 1289 * @tag: tag index
1da177e4 1290 *
daff89f3
JC
1291 * Set the search tag (which must be < RADIX_TREE_MAX_TAGS)
1292 * corresponding to @index in the radix tree. From
1da177e4
LT
1293 * the root all the way down to the leaf node.
1294 *
2fcd9005 1295 * Returns the address of the tagged item. Setting a tag on a not-present
1da177e4
LT
1296 * item is a bug.
1297 */
1298void *radix_tree_tag_set(struct radix_tree_root *root,
daff89f3 1299 unsigned long index, unsigned int tag)
1da177e4 1300{
fb969909
RZ
1301 struct radix_tree_node *node, *parent;
1302 unsigned long maxindex;
1da177e4 1303
9e85d811 1304 radix_tree_load_root(root, &node, &maxindex);
fb969909 1305 BUG_ON(index > maxindex);
1da177e4 1306
b194d16c 1307 while (radix_tree_is_internal_node(node)) {
fb969909 1308 unsigned offset;
1da177e4 1309
4dd6c098 1310 parent = entry_to_node(node);
9e85d811 1311 offset = radix_tree_descend(parent, &node, index);
fb969909
RZ
1312 BUG_ON(!node);
1313
1314 if (!tag_get(parent, tag, offset))
1315 tag_set(parent, tag, offset);
1da177e4
LT
1316 }
1317
612d6c19 1318 /* set the root's tag bit */
fb969909 1319 if (!root_tag_get(root, tag))
612d6c19
NP
1320 root_tag_set(root, tag);
1321
fb969909 1322 return node;
1da177e4
LT
1323}
1324EXPORT_SYMBOL(radix_tree_tag_set);
1325
30b888ba
MW
1326/**
1327 * radix_tree_iter_tag_set - set a tag on the current iterator entry
1328 * @root: radix tree root
1329 * @iter: iterator state
1330 * @tag: tag to set
1331 */
1332void radix_tree_iter_tag_set(struct radix_tree_root *root,
1333 const struct radix_tree_iter *iter, unsigned int tag)
1334{
1335 node_tag_set(root, iter->node, tag, iter_offset(iter));
1336}
1337
d604c324
MW
1338static void node_tag_clear(struct radix_tree_root *root,
1339 struct radix_tree_node *node,
1340 unsigned int tag, unsigned int offset)
1341{
1342 while (node) {
1343 if (!tag_get(node, tag, offset))
1344 return;
1345 tag_clear(node, tag, offset);
1346 if (any_tag_set(node, tag))
1347 return;
1348
1349 offset = node->offset;
1350 node = node->parent;
1351 }
1352
1353 /* clear the root's tag bit */
1354 if (root_tag_get(root, tag))
1355 root_tag_clear(root, tag);
1356}
1357
1da177e4
LT
1358/**
1359 * radix_tree_tag_clear - clear a tag on a radix tree node
1360 * @root: radix tree root
1361 * @index: index key
2fcd9005 1362 * @tag: tag index
1da177e4 1363 *
daff89f3 1364 * Clear the search tag (which must be < RADIX_TREE_MAX_TAGS)
2fcd9005
MW
1365 * corresponding to @index in the radix tree. If this causes
1366 * the leaf node to have no tags set then clear the tag in the
1da177e4
LT
1367 * next-to-leaf node, etc.
1368 *
1369 * Returns the address of the tagged item on success, else NULL. ie:
1370 * has the same return value and semantics as radix_tree_lookup().
1371 */
1372void *radix_tree_tag_clear(struct radix_tree_root *root,
daff89f3 1373 unsigned long index, unsigned int tag)
1da177e4 1374{
00f47b58
RZ
1375 struct radix_tree_node *node, *parent;
1376 unsigned long maxindex;
e2bdb933 1377 int uninitialized_var(offset);
1da177e4 1378
9e85d811 1379 radix_tree_load_root(root, &node, &maxindex);
00f47b58
RZ
1380 if (index > maxindex)
1381 return NULL;
1da177e4 1382
00f47b58 1383 parent = NULL;
1da177e4 1384
b194d16c 1385 while (radix_tree_is_internal_node(node)) {
4dd6c098 1386 parent = entry_to_node(node);
9e85d811 1387 offset = radix_tree_descend(parent, &node, index);
1da177e4
LT
1388 }
1389
d604c324
MW
1390 if (node)
1391 node_tag_clear(root, parent, tag, offset);
1da177e4 1392
00f47b58 1393 return node;
1da177e4
LT
1394}
1395EXPORT_SYMBOL(radix_tree_tag_clear);
1396
30b888ba
MW
1397/**
1398 * radix_tree_iter_tag_clear - clear a tag on the current iterator entry
1399 * @root: radix tree root
1400 * @iter: iterator state
1401 * @tag: tag to clear
1402 */
1403void radix_tree_iter_tag_clear(struct radix_tree_root *root,
1404 const struct radix_tree_iter *iter, unsigned int tag)
1405{
1406 node_tag_clear(root, iter->node, tag, iter_offset(iter));
1407}
1408
1da177e4 1409/**
32605a18
MT
1410 * radix_tree_tag_get - get a tag on a radix tree node
1411 * @root: radix tree root
1412 * @index: index key
2fcd9005 1413 * @tag: tag index (< RADIX_TREE_MAX_TAGS)
1da177e4 1414 *
32605a18 1415 * Return values:
1da177e4 1416 *
612d6c19
NP
1417 * 0: tag not present or not set
1418 * 1: tag set
ce82653d
DH
1419 *
1420 * Note that the return value of this function may not be relied on, even if
1421 * the RCU lock is held, unless tag modification and node deletion are excluded
1422 * from concurrency.
1da177e4 1423 */
35534c86 1424int radix_tree_tag_get(const struct radix_tree_root *root,
daff89f3 1425 unsigned long index, unsigned int tag)
1da177e4 1426{
4589ba6d
RZ
1427 struct radix_tree_node *node, *parent;
1428 unsigned long maxindex;
1da177e4 1429
612d6c19
NP
1430 if (!root_tag_get(root, tag))
1431 return 0;
1432
9e85d811 1433 radix_tree_load_root(root, &node, &maxindex);
4589ba6d
RZ
1434 if (index > maxindex)
1435 return 0;
7cf9c2c7
NP
1436 if (node == NULL)
1437 return 0;
1438
b194d16c 1439 while (radix_tree_is_internal_node(node)) {
9e85d811 1440 unsigned offset;
1da177e4 1441
4dd6c098 1442 parent = entry_to_node(node);
9e85d811 1443 offset = radix_tree_descend(parent, &node, index);
1da177e4 1444
4589ba6d 1445 if (!node)
1da177e4 1446 return 0;
4589ba6d 1447 if (!tag_get(parent, tag, offset))
3fa36acb 1448 return 0;
4589ba6d
RZ
1449 if (node == RADIX_TREE_RETRY)
1450 break;
1da177e4 1451 }
4589ba6d
RZ
1452
1453 return 1;
1da177e4
LT
1454}
1455EXPORT_SYMBOL(radix_tree_tag_get);
1da177e4 1456
21ef5339
RZ
1457static inline void __set_iter_shift(struct radix_tree_iter *iter,
1458 unsigned int shift)
1459{
1460#ifdef CONFIG_RADIX_TREE_MULTIORDER
1461 iter->shift = shift;
1462#endif
1463}
1464
148deab2
MW
1465/* Construct iter->tags bit-mask from node->tags[tag] array */
1466static void set_iter_tags(struct radix_tree_iter *iter,
1467 struct radix_tree_node *node, unsigned offset,
1468 unsigned tag)
1469{
1470 unsigned tag_long = offset / BITS_PER_LONG;
1471 unsigned tag_bit = offset % BITS_PER_LONG;
1472
1473 iter->tags = node->tags[tag][tag_long] >> tag_bit;
1474
1475 /* This never happens if RADIX_TREE_TAG_LONGS == 1 */
1476 if (tag_long < RADIX_TREE_TAG_LONGS - 1) {
1477 /* Pick tags from next element */
1478 if (tag_bit)
1479 iter->tags |= node->tags[tag][tag_long + 1] <<
1480 (BITS_PER_LONG - tag_bit);
1481 /* Clip chunk size, here only BITS_PER_LONG tags */
1482 iter->next_index = __radix_tree_iter_add(iter, BITS_PER_LONG);
1483 }
1484}
1485
1486#ifdef CONFIG_RADIX_TREE_MULTIORDER
1487static void **skip_siblings(struct radix_tree_node **nodep,
1488 void **slot, struct radix_tree_iter *iter)
1489{
1490 void *sib = node_to_entry(slot - 1);
1491
1492 while (iter->index < iter->next_index) {
1493 *nodep = rcu_dereference_raw(*slot);
1494 if (*nodep && *nodep != sib)
1495 return slot;
1496 slot++;
1497 iter->index = __radix_tree_iter_add(iter, 1);
1498 iter->tags >>= 1;
1499 }
1500
1501 *nodep = NULL;
1502 return NULL;
1503}
1504
1505void ** __radix_tree_next_slot(void **slot, struct radix_tree_iter *iter,
1506 unsigned flags)
1507{
1508 unsigned tag = flags & RADIX_TREE_ITER_TAG_MASK;
1509 struct radix_tree_node *node = rcu_dereference_raw(*slot);
1510
1511 slot = skip_siblings(&node, slot, iter);
1512
1513 while (radix_tree_is_internal_node(node)) {
1514 unsigned offset;
1515 unsigned long next_index;
1516
1517 if (node == RADIX_TREE_RETRY)
1518 return slot;
1519 node = entry_to_node(node);
268f42de 1520 iter->node = node;
148deab2
MW
1521 iter->shift = node->shift;
1522
1523 if (flags & RADIX_TREE_ITER_TAGGED) {
1524 offset = radix_tree_find_next_bit(node, tag, 0);
1525 if (offset == RADIX_TREE_MAP_SIZE)
1526 return NULL;
1527 slot = &node->slots[offset];
1528 iter->index = __radix_tree_iter_add(iter, offset);
1529 set_iter_tags(iter, node, offset, tag);
1530 node = rcu_dereference_raw(*slot);
1531 } else {
1532 offset = 0;
1533 slot = &node->slots[0];
1534 for (;;) {
1535 node = rcu_dereference_raw(*slot);
1536 if (node)
1537 break;
1538 slot++;
1539 offset++;
1540 if (offset == RADIX_TREE_MAP_SIZE)
1541 return NULL;
1542 }
1543 iter->index = __radix_tree_iter_add(iter, offset);
1544 }
1545 if ((flags & RADIX_TREE_ITER_CONTIG) && (offset > 0))
1546 goto none;
1547 next_index = (iter->index | shift_maxindex(iter->shift)) + 1;
1548 if (next_index < iter->next_index)
1549 iter->next_index = next_index;
1550 }
1551
1552 return slot;
1553 none:
1554 iter->next_index = 0;
1555 return NULL;
1556}
1557EXPORT_SYMBOL(__radix_tree_next_slot);
1558#else
1559static void **skip_siblings(struct radix_tree_node **nodep,
1560 void **slot, struct radix_tree_iter *iter)
1561{
1562 return slot;
1563}
1564#endif
1565
1566void **radix_tree_iter_resume(void **slot, struct radix_tree_iter *iter)
1567{
1568 struct radix_tree_node *node;
1569
1570 slot++;
1571 iter->index = __radix_tree_iter_add(iter, 1);
1572 node = rcu_dereference_raw(*slot);
1573 skip_siblings(&node, slot, iter);
1574 iter->next_index = iter->index;
1575 iter->tags = 0;
1576 return NULL;
1577}
1578EXPORT_SYMBOL(radix_tree_iter_resume);
1579
78c1d784
KK
1580/**
1581 * radix_tree_next_chunk - find next chunk of slots for iteration
1582 *
1583 * @root: radix tree root
1584 * @iter: iterator state
1585 * @flags: RADIX_TREE_ITER_* flags and tag index
1586 * Returns: pointer to chunk first slot, or NULL if iteration is over
1587 */
35534c86 1588void **radix_tree_next_chunk(const struct radix_tree_root *root,
78c1d784
KK
1589 struct radix_tree_iter *iter, unsigned flags)
1590{
9e85d811 1591 unsigned tag = flags & RADIX_TREE_ITER_TAG_MASK;
8c1244de 1592 struct radix_tree_node *node, *child;
21ef5339 1593 unsigned long index, offset, maxindex;
78c1d784
KK
1594
1595 if ((flags & RADIX_TREE_ITER_TAGGED) && !root_tag_get(root, tag))
1596 return NULL;
1597
1598 /*
1599 * Catch next_index overflow after ~0UL. iter->index never overflows
1600 * during iterating; it can be zero only at the beginning.
1601 * And we cannot overflow iter->next_index in a single step,
1602 * because RADIX_TREE_MAP_SHIFT < BITS_PER_LONG.
fffaee36
KK
1603 *
1604 * This condition also used by radix_tree_next_slot() to stop
91b9677c 1605 * contiguous iterating, and forbid switching to the next chunk.
78c1d784
KK
1606 */
1607 index = iter->next_index;
1608 if (!index && iter->index)
1609 return NULL;
1610
21ef5339 1611 restart:
9e85d811 1612 radix_tree_load_root(root, &child, &maxindex);
21ef5339
RZ
1613 if (index > maxindex)
1614 return NULL;
8c1244de
MW
1615 if (!child)
1616 return NULL;
21ef5339 1617
8c1244de 1618 if (!radix_tree_is_internal_node(child)) {
78c1d784 1619 /* Single-slot tree */
21ef5339
RZ
1620 iter->index = index;
1621 iter->next_index = maxindex + 1;
78c1d784 1622 iter->tags = 1;
268f42de 1623 iter->node = NULL;
8c1244de 1624 __set_iter_shift(iter, 0);
78c1d784 1625 return (void **)&root->rnode;
8c1244de 1626 }
21ef5339 1627
8c1244de
MW
1628 do {
1629 node = entry_to_node(child);
9e85d811 1630 offset = radix_tree_descend(node, &child, index);
21ef5339 1631
78c1d784 1632 if ((flags & RADIX_TREE_ITER_TAGGED) ?
8c1244de 1633 !tag_get(node, tag, offset) : !child) {
78c1d784
KK
1634 /* Hole detected */
1635 if (flags & RADIX_TREE_ITER_CONTIG)
1636 return NULL;
1637
1638 if (flags & RADIX_TREE_ITER_TAGGED)
bc412fca 1639 offset = radix_tree_find_next_bit(node, tag,
78c1d784
KK
1640 offset + 1);
1641 else
1642 while (++offset < RADIX_TREE_MAP_SIZE) {
21ef5339
RZ
1643 void *slot = node->slots[offset];
1644 if (is_sibling_entry(node, slot))
1645 continue;
1646 if (slot)
78c1d784
KK
1647 break;
1648 }
8c1244de 1649 index &= ~node_maxindex(node);
9e85d811 1650 index += offset << node->shift;
78c1d784
KK
1651 /* Overflow after ~0UL */
1652 if (!index)
1653 return NULL;
1654 if (offset == RADIX_TREE_MAP_SIZE)
1655 goto restart;
8c1244de 1656 child = rcu_dereference_raw(node->slots[offset]);
78c1d784
KK
1657 }
1658
e157b555 1659 if (!child)
78c1d784 1660 goto restart;
e157b555
MW
1661 if (child == RADIX_TREE_RETRY)
1662 break;
8c1244de 1663 } while (radix_tree_is_internal_node(child));
78c1d784
KK
1664
1665 /* Update the iterator state */
8c1244de
MW
1666 iter->index = (index &~ node_maxindex(node)) | (offset << node->shift);
1667 iter->next_index = (index | node_maxindex(node)) + 1;
268f42de 1668 iter->node = node;
9e85d811 1669 __set_iter_shift(iter, node->shift);
78c1d784 1670
148deab2
MW
1671 if (flags & RADIX_TREE_ITER_TAGGED)
1672 set_iter_tags(iter, node, offset, tag);
78c1d784
KK
1673
1674 return node->slots + offset;
1675}
1676EXPORT_SYMBOL(radix_tree_next_chunk);
1677
1da177e4
LT
1678/**
1679 * radix_tree_gang_lookup - perform multiple lookup on a radix tree
1680 * @root: radix tree root
1681 * @results: where the results of the lookup are placed
1682 * @first_index: start the lookup from this key
1683 * @max_items: place up to this many items at *results
1684 *
1685 * Performs an index-ascending scan of the tree for present items. Places
1686 * them at *@results and returns the number of items which were placed at
1687 * *@results.
1688 *
1689 * The implementation is naive.
7cf9c2c7
NP
1690 *
1691 * Like radix_tree_lookup, radix_tree_gang_lookup may be called under
1692 * rcu_read_lock. In this case, rather than the returned results being
2fcd9005
MW
1693 * an atomic snapshot of the tree at a single point in time, the
1694 * semantics of an RCU protected gang lookup are as though multiple
1695 * radix_tree_lookups have been issued in individual locks, and results
1696 * stored in 'results'.
1da177e4
LT
1697 */
1698unsigned int
35534c86 1699radix_tree_gang_lookup(const struct radix_tree_root *root, void **results,
1da177e4
LT
1700 unsigned long first_index, unsigned int max_items)
1701{
cebbd29e
KK
1702 struct radix_tree_iter iter;
1703 void **slot;
1704 unsigned int ret = 0;
7cf9c2c7 1705
cebbd29e 1706 if (unlikely(!max_items))
7cf9c2c7 1707 return 0;
1da177e4 1708
cebbd29e 1709 radix_tree_for_each_slot(slot, root, &iter, first_index) {
46437f9a 1710 results[ret] = rcu_dereference_raw(*slot);
cebbd29e
KK
1711 if (!results[ret])
1712 continue;
b194d16c 1713 if (radix_tree_is_internal_node(results[ret])) {
46437f9a
MW
1714 slot = radix_tree_iter_retry(&iter);
1715 continue;
1716 }
cebbd29e 1717 if (++ret == max_items)
1da177e4 1718 break;
1da177e4 1719 }
7cf9c2c7 1720
1da177e4
LT
1721 return ret;
1722}
1723EXPORT_SYMBOL(radix_tree_gang_lookup);
1724
47feff2c
NP
1725/**
1726 * radix_tree_gang_lookup_slot - perform multiple slot lookup on radix tree
1727 * @root: radix tree root
1728 * @results: where the results of the lookup are placed
6328650b 1729 * @indices: where their indices should be placed (but usually NULL)
47feff2c
NP
1730 * @first_index: start the lookup from this key
1731 * @max_items: place up to this many items at *results
1732 *
1733 * Performs an index-ascending scan of the tree for present items. Places
1734 * their slots at *@results and returns the number of items which were
1735 * placed at *@results.
1736 *
1737 * The implementation is naive.
1738 *
1739 * Like radix_tree_gang_lookup as far as RCU and locking goes. Slots must
1740 * be dereferenced with radix_tree_deref_slot, and if using only RCU
1741 * protection, radix_tree_deref_slot may fail requiring a retry.
1742 */
1743unsigned int
35534c86 1744radix_tree_gang_lookup_slot(const struct radix_tree_root *root,
6328650b 1745 void ***results, unsigned long *indices,
47feff2c
NP
1746 unsigned long first_index, unsigned int max_items)
1747{
cebbd29e
KK
1748 struct radix_tree_iter iter;
1749 void **slot;
1750 unsigned int ret = 0;
47feff2c 1751
cebbd29e 1752 if (unlikely(!max_items))
47feff2c
NP
1753 return 0;
1754
cebbd29e
KK
1755 radix_tree_for_each_slot(slot, root, &iter, first_index) {
1756 results[ret] = slot;
6328650b 1757 if (indices)
cebbd29e
KK
1758 indices[ret] = iter.index;
1759 if (++ret == max_items)
47feff2c 1760 break;
47feff2c
NP
1761 }
1762
1763 return ret;
1764}
1765EXPORT_SYMBOL(radix_tree_gang_lookup_slot);
1766
1da177e4
LT
1767/**
1768 * radix_tree_gang_lookup_tag - perform multiple lookup on a radix tree
1769 * based on a tag
1770 * @root: radix tree root
1771 * @results: where the results of the lookup are placed
1772 * @first_index: start the lookup from this key
1773 * @max_items: place up to this many items at *results
daff89f3 1774 * @tag: the tag index (< RADIX_TREE_MAX_TAGS)
1da177e4
LT
1775 *
1776 * Performs an index-ascending scan of the tree for present items which
1777 * have the tag indexed by @tag set. Places the items at *@results and
1778 * returns the number of items which were placed at *@results.
1779 */
1780unsigned int
35534c86 1781radix_tree_gang_lookup_tag(const struct radix_tree_root *root, void **results,
daff89f3
JC
1782 unsigned long first_index, unsigned int max_items,
1783 unsigned int tag)
1da177e4 1784{
cebbd29e
KK
1785 struct radix_tree_iter iter;
1786 void **slot;
1787 unsigned int ret = 0;
612d6c19 1788
cebbd29e 1789 if (unlikely(!max_items))
7cf9c2c7
NP
1790 return 0;
1791
cebbd29e 1792 radix_tree_for_each_tagged(slot, root, &iter, first_index, tag) {
46437f9a 1793 results[ret] = rcu_dereference_raw(*slot);
cebbd29e
KK
1794 if (!results[ret])
1795 continue;
b194d16c 1796 if (radix_tree_is_internal_node(results[ret])) {
46437f9a
MW
1797 slot = radix_tree_iter_retry(&iter);
1798 continue;
1799 }
cebbd29e 1800 if (++ret == max_items)
1da177e4 1801 break;
1da177e4 1802 }
7cf9c2c7 1803
1da177e4
LT
1804 return ret;
1805}
1806EXPORT_SYMBOL(radix_tree_gang_lookup_tag);
1807
47feff2c
NP
1808/**
1809 * radix_tree_gang_lookup_tag_slot - perform multiple slot lookup on a
1810 * radix tree based on a tag
1811 * @root: radix tree root
1812 * @results: where the results of the lookup are placed
1813 * @first_index: start the lookup from this key
1814 * @max_items: place up to this many items at *results
1815 * @tag: the tag index (< RADIX_TREE_MAX_TAGS)
1816 *
1817 * Performs an index-ascending scan of the tree for present items which
1818 * have the tag indexed by @tag set. Places the slots at *@results and
1819 * returns the number of slots which were placed at *@results.
1820 */
1821unsigned int
35534c86
MW
1822radix_tree_gang_lookup_tag_slot(const struct radix_tree_root *root,
1823 void ***results, unsigned long first_index,
1824 unsigned int max_items, unsigned int tag)
47feff2c 1825{
cebbd29e
KK
1826 struct radix_tree_iter iter;
1827 void **slot;
1828 unsigned int ret = 0;
47feff2c 1829
cebbd29e 1830 if (unlikely(!max_items))
47feff2c
NP
1831 return 0;
1832
cebbd29e
KK
1833 radix_tree_for_each_tagged(slot, root, &iter, first_index, tag) {
1834 results[ret] = slot;
1835 if (++ret == max_items)
47feff2c 1836 break;
47feff2c
NP
1837 }
1838
1839 return ret;
1840}
1841EXPORT_SYMBOL(radix_tree_gang_lookup_tag_slot);
1842
139e5616
JW
1843/**
1844 * __radix_tree_delete_node - try to free node after clearing a slot
1845 * @root: radix tree root
139e5616 1846 * @node: node containing @index
ea07b862
JW
1847 * @update_node: callback for changing leaf nodes
1848 * @private: private data to pass to @update_node
139e5616
JW
1849 *
1850 * After clearing the slot at @index in @node from radix tree
1851 * rooted at @root, call this function to attempt freeing the
1852 * node and shrinking the tree.
139e5616 1853 */
14b46879 1854void __radix_tree_delete_node(struct radix_tree_root *root,
ea07b862
JW
1855 struct radix_tree_node *node,
1856 radix_tree_update_node_t update_node,
1857 void *private)
139e5616 1858{
ea07b862 1859 delete_node(root, node, update_node, private);
139e5616
JW
1860}
1861
1da177e4 1862/**
53c59f26 1863 * radix_tree_delete_item - delete an item from a radix tree
1da177e4
LT
1864 * @root: radix tree root
1865 * @index: index key
53c59f26 1866 * @item: expected item
1da177e4 1867 *
53c59f26 1868 * Remove @item at @index from the radix tree rooted at @root.
1da177e4 1869 *
53c59f26
JW
1870 * Returns the address of the deleted item, or NULL if it was not present
1871 * or the entry at the given @index was not @item.
1da177e4 1872 */
53c59f26
JW
1873void *radix_tree_delete_item(struct radix_tree_root *root,
1874 unsigned long index, void *item)
1da177e4 1875{
139e5616 1876 struct radix_tree_node *node;
57578c2e 1877 unsigned int offset;
139e5616
JW
1878 void **slot;
1879 void *entry;
d5274261 1880 int tag;
1da177e4 1881
139e5616
JW
1882 entry = __radix_tree_lookup(root, index, &node, &slot);
1883 if (!entry)
1884 return NULL;
1da177e4 1885
139e5616
JW
1886 if (item && entry != item)
1887 return NULL;
1888
1889 if (!node) {
612d6c19
NP
1890 root_tag_clear_all(root);
1891 root->rnode = NULL;
139e5616 1892 return entry;
612d6c19 1893 }
1da177e4 1894
29e0967c 1895 offset = get_slot_offset(node, slot);
53c59f26 1896
d604c324
MW
1897 /* Clear all tags associated with the item to be deleted. */
1898 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
1899 node_tag_clear(root, node, tag, offset);
1da177e4 1900
4d693d08 1901 __radix_tree_replace(root, node, slot, NULL, NULL, NULL);
612d6c19 1902
139e5616 1903 return entry;
1da177e4 1904}
53c59f26
JW
1905EXPORT_SYMBOL(radix_tree_delete_item);
1906
1907/**
1908 * radix_tree_delete - delete an item from a radix tree
1909 * @root: radix tree root
1910 * @index: index key
1911 *
1912 * Remove the item at @index from the radix tree rooted at @root.
1913 *
1914 * Returns the address of the deleted item, or NULL if it was not present.
1915 */
1916void *radix_tree_delete(struct radix_tree_root *root, unsigned long index)
1917{
1918 return radix_tree_delete_item(root, index, NULL);
1919}
1da177e4
LT
1920EXPORT_SYMBOL(radix_tree_delete);
1921
d3798ae8
JW
1922void radix_tree_clear_tags(struct radix_tree_root *root,
1923 struct radix_tree_node *node,
1924 void **slot)
d604c324 1925{
d604c324
MW
1926 if (node) {
1927 unsigned int tag, offset = get_slot_offset(node, slot);
1928 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
1929 node_tag_clear(root, node, tag, offset);
1930 } else {
1931 /* Clear root node tags */
1932 root->gfp_mask &= __GFP_BITS_MASK;
1933 }
d604c324
MW
1934}
1935
1da177e4
LT
1936/**
1937 * radix_tree_tagged - test whether any items in the tree are tagged
1938 * @root: radix tree root
1939 * @tag: tag to test
1940 */
35534c86 1941int radix_tree_tagged(const struct radix_tree_root *root, unsigned int tag)
1da177e4 1942{
612d6c19 1943 return root_tag_get(root, tag);
1da177e4
LT
1944}
1945EXPORT_SYMBOL(radix_tree_tagged);
1946
1947static void
449dd698 1948radix_tree_node_ctor(void *arg)
1da177e4 1949{
449dd698
JW
1950 struct radix_tree_node *node = arg;
1951
1952 memset(node, 0, sizeof(*node));
1953 INIT_LIST_HEAD(&node->private_list);
1da177e4
LT
1954}
1955
c78c66d1
KS
1956static __init unsigned long __maxindex(unsigned int height)
1957{
1958 unsigned int width = height * RADIX_TREE_MAP_SHIFT;
1959 int shift = RADIX_TREE_INDEX_BITS - width;
1960
1961 if (shift < 0)
1962 return ~0UL;
1963 if (shift >= BITS_PER_LONG)
1964 return 0UL;
1965 return ~0UL >> shift;
1966}
1967
1968static __init void radix_tree_init_maxnodes(void)
1969{
1970 unsigned long height_to_maxindex[RADIX_TREE_MAX_PATH + 1];
1971 unsigned int i, j;
1972
1973 for (i = 0; i < ARRAY_SIZE(height_to_maxindex); i++)
1974 height_to_maxindex[i] = __maxindex(i);
1975 for (i = 0; i < ARRAY_SIZE(height_to_maxnodes); i++) {
1976 for (j = i; j > 0; j--)
1977 height_to_maxnodes[i] += height_to_maxindex[j - 1] + 1;
1978 }
1979}
1980
d544abd5 1981static int radix_tree_cpu_dead(unsigned int cpu)
1da177e4 1982{
2fcd9005
MW
1983 struct radix_tree_preload *rtp;
1984 struct radix_tree_node *node;
1985
1986 /* Free per-cpu pool of preloaded nodes */
d544abd5
SAS
1987 rtp = &per_cpu(radix_tree_preloads, cpu);
1988 while (rtp->nr) {
1989 node = rtp->nodes;
1990 rtp->nodes = node->private_data;
1991 kmem_cache_free(radix_tree_node_cachep, node);
1992 rtp->nr--;
2fcd9005 1993 }
d544abd5 1994 return 0;
1da177e4 1995}
1da177e4
LT
1996
1997void __init radix_tree_init(void)
1998{
d544abd5 1999 int ret;
1da177e4
LT
2000 radix_tree_node_cachep = kmem_cache_create("radix_tree_node",
2001 sizeof(struct radix_tree_node), 0,
488514d1
CL
2002 SLAB_PANIC | SLAB_RECLAIM_ACCOUNT,
2003 radix_tree_node_ctor);
c78c66d1 2004 radix_tree_init_maxnodes();
d544abd5
SAS
2005 ret = cpuhp_setup_state_nocalls(CPUHP_RADIX_DEAD, "lib/radix:dead",
2006 NULL, radix_tree_cpu_dead);
2007 WARN_ON(ret < 0);
1da177e4 2008}