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