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