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