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