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