]> rtime.felk.cvut.cz Git - linux-imx.git/blob - lib/radix-tree.c
davinci: DM365 EVM: fix video input mux bits
[linux-imx.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  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License as
9  * published by the Free Software Foundation; either version 2, or (at
10  * your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21
22 #include <linux/errno.h>
23 #include <linux/init.h>
24 #include <linux/kernel.h>
25 #include <linux/module.h>
26 #include <linux/radix-tree.h>
27 #include <linux/percpu.h>
28 #include <linux/slab.h>
29 #include <linux/notifier.h>
30 #include <linux/cpu.h>
31 #include <linux/string.h>
32 #include <linux/bitops.h>
33 #include <linux/rcupdate.h>
34
35
36 #ifdef __KERNEL__
37 #define RADIX_TREE_MAP_SHIFT    (CONFIG_BASE_SMALL ? 4 : 6)
38 #else
39 #define RADIX_TREE_MAP_SHIFT    3       /* For more stressful testing */
40 #endif
41
42 #define RADIX_TREE_MAP_SIZE     (1UL << RADIX_TREE_MAP_SHIFT)
43 #define RADIX_TREE_MAP_MASK     (RADIX_TREE_MAP_SIZE-1)
44
45 #define RADIX_TREE_TAG_LONGS    \
46         ((RADIX_TREE_MAP_SIZE + BITS_PER_LONG - 1) / BITS_PER_LONG)
47
48 struct radix_tree_node {
49         unsigned int    height;         /* Height from the bottom */
50         unsigned int    count;
51         struct rcu_head rcu_head;
52         void            *slots[RADIX_TREE_MAP_SIZE];
53         unsigned long   tags[RADIX_TREE_MAX_TAGS][RADIX_TREE_TAG_LONGS];
54 };
55
56 struct radix_tree_path {
57         struct radix_tree_node *node;
58         int offset;
59 };
60
61 #define RADIX_TREE_INDEX_BITS  (8 /* CHAR_BIT */ * sizeof(unsigned long))
62 #define RADIX_TREE_MAX_PATH (DIV_ROUND_UP(RADIX_TREE_INDEX_BITS, \
63                                           RADIX_TREE_MAP_SHIFT))
64
65 /*
66  * The height_to_maxindex array needs to be one deeper than the maximum
67  * path as height 0 holds only 1 entry.
68  */
69 static unsigned long height_to_maxindex[RADIX_TREE_MAX_PATH + 1] __read_mostly;
70
71 /*
72  * Radix tree node cache.
73  */
74 static struct kmem_cache *radix_tree_node_cachep;
75
76 /*
77  * Per-cpu pool of preloaded nodes
78  */
79 struct radix_tree_preload {
80         int nr;
81         struct radix_tree_node *nodes[RADIX_TREE_MAX_PATH];
82 };
83 static DEFINE_PER_CPU(struct radix_tree_preload, radix_tree_preloads) = { 0, };
84
85 static inline void *ptr_to_indirect(void *ptr)
86 {
87         return (void *)((unsigned long)ptr | RADIX_TREE_INDIRECT_PTR);
88 }
89
90 static inline void *indirect_to_ptr(void *ptr)
91 {
92         return (void *)((unsigned long)ptr & ~RADIX_TREE_INDIRECT_PTR);
93 }
94
95 static inline gfp_t root_gfp_mask(struct radix_tree_root *root)
96 {
97         return root->gfp_mask & __GFP_BITS_MASK;
98 }
99
100 static inline void tag_set(struct radix_tree_node *node, unsigned int tag,
101                 int offset)
102 {
103         __set_bit(offset, node->tags[tag]);
104 }
105
106 static inline void tag_clear(struct radix_tree_node *node, unsigned int tag,
107                 int offset)
108 {
109         __clear_bit(offset, node->tags[tag]);
110 }
111
112 static inline int tag_get(struct radix_tree_node *node, unsigned int tag,
113                 int offset)
114 {
115         return test_bit(offset, node->tags[tag]);
116 }
117
118 static inline void root_tag_set(struct radix_tree_root *root, unsigned int tag)
119 {
120         root->gfp_mask |= (__force gfp_t)(1 << (tag + __GFP_BITS_SHIFT));
121 }
122
123 static inline void root_tag_clear(struct radix_tree_root *root, unsigned int tag)
124 {
125         root->gfp_mask &= (__force gfp_t)~(1 << (tag + __GFP_BITS_SHIFT));
126 }
127
128 static inline void root_tag_clear_all(struct radix_tree_root *root)
129 {
130         root->gfp_mask &= __GFP_BITS_MASK;
131 }
132
133 static inline int root_tag_get(struct radix_tree_root *root, unsigned int tag)
134 {
135         return (__force unsigned)root->gfp_mask & (1 << (tag + __GFP_BITS_SHIFT));
136 }
137
138 /*
139  * Returns 1 if any slot in the node has this tag set.
140  * Otherwise returns 0.
141  */
142 static inline int any_tag_set(struct radix_tree_node *node, unsigned int tag)
143 {
144         int idx;
145         for (idx = 0; idx < RADIX_TREE_TAG_LONGS; idx++) {
146                 if (node->tags[tag][idx])
147                         return 1;
148         }
149         return 0;
150 }
151 /*
152  * This assumes that the caller has performed appropriate preallocation, and
153  * that the caller has pinned this thread of control to the current CPU.
154  */
155 static struct radix_tree_node *
156 radix_tree_node_alloc(struct radix_tree_root *root)
157 {
158         struct radix_tree_node *ret = NULL;
159         gfp_t gfp_mask = root_gfp_mask(root);
160
161         if (!(gfp_mask & __GFP_WAIT)) {
162                 struct radix_tree_preload *rtp;
163
164                 /*
165                  * Provided the caller has preloaded here, we will always
166                  * succeed in getting a node here (and never reach
167                  * kmem_cache_alloc)
168                  */
169                 rtp = &__get_cpu_var(radix_tree_preloads);
170                 if (rtp->nr) {
171                         ret = rtp->nodes[rtp->nr - 1];
172                         rtp->nodes[rtp->nr - 1] = NULL;
173                         rtp->nr--;
174                 }
175         }
176         if (ret == NULL)
177                 ret = kmem_cache_alloc(radix_tree_node_cachep, gfp_mask);
178
179         BUG_ON(radix_tree_is_indirect_ptr(ret));
180         return ret;
181 }
182
183 static void radix_tree_node_rcu_free(struct rcu_head *head)
184 {
185         struct radix_tree_node *node =
186                         container_of(head, struct radix_tree_node, rcu_head);
187
188         /*
189          * must only free zeroed nodes into the slab. radix_tree_shrink
190          * can leave us with a non-NULL entry in the first slot, so clear
191          * that here to make sure.
192          */
193         tag_clear(node, 0, 0);
194         tag_clear(node, 1, 0);
195         node->slots[0] = NULL;
196         node->count = 0;
197
198         kmem_cache_free(radix_tree_node_cachep, node);
199 }
200
201 static inline void
202 radix_tree_node_free(struct radix_tree_node *node)
203 {
204         call_rcu(&node->rcu_head, radix_tree_node_rcu_free);
205 }
206
207 /*
208  * Load up this CPU's radix_tree_node buffer with sufficient objects to
209  * ensure that the addition of a single element in the tree cannot fail.  On
210  * success, return zero, with preemption disabled.  On error, return -ENOMEM
211  * with preemption not disabled.
212  *
213  * To make use of this facility, the radix tree must be initialised without
214  * __GFP_WAIT being passed to INIT_RADIX_TREE().
215  */
216 int radix_tree_preload(gfp_t gfp_mask)
217 {
218         struct radix_tree_preload *rtp;
219         struct radix_tree_node *node;
220         int ret = -ENOMEM;
221
222         preempt_disable();
223         rtp = &__get_cpu_var(radix_tree_preloads);
224         while (rtp->nr < ARRAY_SIZE(rtp->nodes)) {
225                 preempt_enable();
226                 node = kmem_cache_alloc(radix_tree_node_cachep, gfp_mask);
227                 if (node == NULL)
228                         goto out;
229                 preempt_disable();
230                 rtp = &__get_cpu_var(radix_tree_preloads);
231                 if (rtp->nr < ARRAY_SIZE(rtp->nodes))
232                         rtp->nodes[rtp->nr++] = node;
233                 else
234                         kmem_cache_free(radix_tree_node_cachep, node);
235         }
236         ret = 0;
237 out:
238         return ret;
239 }
240 EXPORT_SYMBOL(radix_tree_preload);
241
242 /*
243  *      Return the maximum key which can be store into a
244  *      radix tree with height HEIGHT.
245  */
246 static inline unsigned long radix_tree_maxindex(unsigned int height)
247 {
248         return height_to_maxindex[height];
249 }
250
251 /*
252  *      Extend a radix tree so it can store key @index.
253  */
254 static int radix_tree_extend(struct radix_tree_root *root, unsigned long index)
255 {
256         struct radix_tree_node *node;
257         unsigned int height;
258         int tag;
259
260         /* Figure out what the height should be.  */
261         height = root->height + 1;
262         while (index > radix_tree_maxindex(height))
263                 height++;
264
265         if (root->rnode == NULL) {
266                 root->height = height;
267                 goto out;
268         }
269
270         do {
271                 unsigned int newheight;
272                 if (!(node = radix_tree_node_alloc(root)))
273                         return -ENOMEM;
274
275                 /* Increase the height.  */
276                 node->slots[0] = indirect_to_ptr(root->rnode);
277
278                 /* Propagate the aggregated tag info into the new root */
279                 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) {
280                         if (root_tag_get(root, tag))
281                                 tag_set(node, tag, 0);
282                 }
283
284                 newheight = root->height+1;
285                 node->height = newheight;
286                 node->count = 1;
287                 node = ptr_to_indirect(node);
288                 rcu_assign_pointer(root->rnode, node);
289                 root->height = newheight;
290         } while (height > root->height);
291 out:
292         return 0;
293 }
294
295 /**
296  *      radix_tree_insert    -    insert into a radix tree
297  *      @root:          radix tree root
298  *      @index:         index key
299  *      @item:          item to insert
300  *
301  *      Insert an item into the radix tree at position @index.
302  */
303 int radix_tree_insert(struct radix_tree_root *root,
304                         unsigned long index, void *item)
305 {
306         struct radix_tree_node *node = NULL, *slot;
307         unsigned int height, shift;
308         int offset;
309         int error;
310
311         BUG_ON(radix_tree_is_indirect_ptr(item));
312
313         /* Make sure the tree is high enough.  */
314         if (index > radix_tree_maxindex(root->height)) {
315                 error = radix_tree_extend(root, index);
316                 if (error)
317                         return error;
318         }
319
320         slot = indirect_to_ptr(root->rnode);
321
322         height = root->height;
323         shift = (height-1) * RADIX_TREE_MAP_SHIFT;
324
325         offset = 0;                     /* uninitialised var warning */
326         while (height > 0) {
327                 if (slot == NULL) {
328                         /* Have to add a child node.  */
329                         if (!(slot = radix_tree_node_alloc(root)))
330                                 return -ENOMEM;
331                         slot->height = height;
332                         if (node) {
333                                 rcu_assign_pointer(node->slots[offset], slot);
334                                 node->count++;
335                         } else
336                                 rcu_assign_pointer(root->rnode, ptr_to_indirect(slot));
337                 }
338
339                 /* Go a level down */
340                 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
341                 node = slot;
342                 slot = node->slots[offset];
343                 shift -= RADIX_TREE_MAP_SHIFT;
344                 height--;
345         }
346
347         if (slot != NULL)
348                 return -EEXIST;
349
350         if (node) {
351                 node->count++;
352                 rcu_assign_pointer(node->slots[offset], item);
353                 BUG_ON(tag_get(node, 0, offset));
354                 BUG_ON(tag_get(node, 1, offset));
355         } else {
356                 rcu_assign_pointer(root->rnode, item);
357                 BUG_ON(root_tag_get(root, 0));
358                 BUG_ON(root_tag_get(root, 1));
359         }
360
361         return 0;
362 }
363 EXPORT_SYMBOL(radix_tree_insert);
364
365 /*
366  * is_slot == 1 : search for the slot.
367  * is_slot == 0 : search for the node.
368  */
369 static void *radix_tree_lookup_element(struct radix_tree_root *root,
370                                 unsigned long index, int is_slot)
371 {
372         unsigned int height, shift;
373         struct radix_tree_node *node, **slot;
374
375         node = rcu_dereference_raw(root->rnode);
376         if (node == NULL)
377                 return NULL;
378
379         if (!radix_tree_is_indirect_ptr(node)) {
380                 if (index > 0)
381                         return NULL;
382                 return is_slot ? (void *)&root->rnode : node;
383         }
384         node = indirect_to_ptr(node);
385
386         height = node->height;
387         if (index > radix_tree_maxindex(height))
388                 return NULL;
389
390         shift = (height-1) * RADIX_TREE_MAP_SHIFT;
391
392         do {
393                 slot = (struct radix_tree_node **)
394                         (node->slots + ((index>>shift) & RADIX_TREE_MAP_MASK));
395                 node = rcu_dereference_raw(*slot);
396                 if (node == NULL)
397                         return NULL;
398
399                 shift -= RADIX_TREE_MAP_SHIFT;
400                 height--;
401         } while (height > 0);
402
403         return is_slot ? (void *)slot : indirect_to_ptr(node);
404 }
405
406 /**
407  *      radix_tree_lookup_slot    -    lookup a slot in a radix tree
408  *      @root:          radix tree root
409  *      @index:         index key
410  *
411  *      Returns:  the slot corresponding to the position @index in the
412  *      radix tree @root. This is useful for update-if-exists operations.
413  *
414  *      This function can be called under rcu_read_lock iff the slot is not
415  *      modified by radix_tree_replace_slot, otherwise it must be called
416  *      exclusive from other writers. Any dereference of the slot must be done
417  *      using radix_tree_deref_slot.
418  */
419 void **radix_tree_lookup_slot(struct radix_tree_root *root, unsigned long index)
420 {
421         return (void **)radix_tree_lookup_element(root, index, 1);
422 }
423 EXPORT_SYMBOL(radix_tree_lookup_slot);
424
425 /**
426  *      radix_tree_lookup    -    perform lookup operation on a radix tree
427  *      @root:          radix tree root
428  *      @index:         index key
429  *
430  *      Lookup the item at the position @index in the radix tree @root.
431  *
432  *      This function can be called under rcu_read_lock, however the caller
433  *      must manage lifetimes of leaf nodes (eg. RCU may also be used to free
434  *      them safely). No RCU barriers are required to access or modify the
435  *      returned item, however.
436  */
437 void *radix_tree_lookup(struct radix_tree_root *root, unsigned long index)
438 {
439         return radix_tree_lookup_element(root, index, 0);
440 }
441 EXPORT_SYMBOL(radix_tree_lookup);
442
443 /**
444  *      radix_tree_tag_set - set a tag on a radix tree node
445  *      @root:          radix tree root
446  *      @index:         index key
447  *      @tag:           tag index
448  *
449  *      Set the search tag (which must be < RADIX_TREE_MAX_TAGS)
450  *      corresponding to @index in the radix tree.  From
451  *      the root all the way down to the leaf node.
452  *
453  *      Returns the address of the tagged item.   Setting a tag on a not-present
454  *      item is a bug.
455  */
456 void *radix_tree_tag_set(struct radix_tree_root *root,
457                         unsigned long index, unsigned int tag)
458 {
459         unsigned int height, shift;
460         struct radix_tree_node *slot;
461
462         height = root->height;
463         BUG_ON(index > radix_tree_maxindex(height));
464
465         slot = indirect_to_ptr(root->rnode);
466         shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
467
468         while (height > 0) {
469                 int offset;
470
471                 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
472                 if (!tag_get(slot, tag, offset))
473                         tag_set(slot, tag, offset);
474                 slot = slot->slots[offset];
475                 BUG_ON(slot == NULL);
476                 shift -= RADIX_TREE_MAP_SHIFT;
477                 height--;
478         }
479
480         /* set the root's tag bit */
481         if (slot && !root_tag_get(root, tag))
482                 root_tag_set(root, tag);
483
484         return slot;
485 }
486 EXPORT_SYMBOL(radix_tree_tag_set);
487
488 /**
489  *      radix_tree_tag_clear - clear a tag on a radix tree node
490  *      @root:          radix tree root
491  *      @index:         index key
492  *      @tag:           tag index
493  *
494  *      Clear the search tag (which must be < RADIX_TREE_MAX_TAGS)
495  *      corresponding to @index in the radix tree.  If
496  *      this causes the leaf node to have no tags set then clear the tag in the
497  *      next-to-leaf node, etc.
498  *
499  *      Returns the address of the tagged item on success, else NULL.  ie:
500  *      has the same return value and semantics as radix_tree_lookup().
501  */
502 void *radix_tree_tag_clear(struct radix_tree_root *root,
503                         unsigned long index, unsigned int tag)
504 {
505         /*
506          * The radix tree path needs to be one longer than the maximum path
507          * since the "list" is null terminated.
508          */
509         struct radix_tree_path path[RADIX_TREE_MAX_PATH + 1], *pathp = path;
510         struct radix_tree_node *slot = NULL;
511         unsigned int height, shift;
512
513         height = root->height;
514         if (index > radix_tree_maxindex(height))
515                 goto out;
516
517         shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
518         pathp->node = NULL;
519         slot = indirect_to_ptr(root->rnode);
520
521         while (height > 0) {
522                 int offset;
523
524                 if (slot == NULL)
525                         goto out;
526
527                 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
528                 pathp[1].offset = offset;
529                 pathp[1].node = slot;
530                 slot = slot->slots[offset];
531                 pathp++;
532                 shift -= RADIX_TREE_MAP_SHIFT;
533                 height--;
534         }
535
536         if (slot == NULL)
537                 goto out;
538
539         while (pathp->node) {
540                 if (!tag_get(pathp->node, tag, pathp->offset))
541                         goto out;
542                 tag_clear(pathp->node, tag, pathp->offset);
543                 if (any_tag_set(pathp->node, tag))
544                         goto out;
545                 pathp--;
546         }
547
548         /* clear the root's tag bit */
549         if (root_tag_get(root, tag))
550                 root_tag_clear(root, tag);
551
552 out:
553         return slot;
554 }
555 EXPORT_SYMBOL(radix_tree_tag_clear);
556
557 /**
558  * radix_tree_tag_get - get a tag on a radix tree node
559  * @root:               radix tree root
560  * @index:              index key
561  * @tag:                tag index (< RADIX_TREE_MAX_TAGS)
562  *
563  * Return values:
564  *
565  *  0: tag not present or not set
566  *  1: tag set
567  *
568  * Note that the return value of this function may not be relied on, even if
569  * the RCU lock is held, unless tag modification and node deletion are excluded
570  * from concurrency.
571  */
572 int radix_tree_tag_get(struct radix_tree_root *root,
573                         unsigned long index, unsigned int tag)
574 {
575         unsigned int height, shift;
576         struct radix_tree_node *node;
577         int saw_unset_tag = 0;
578
579         /* check the root's tag bit */
580         if (!root_tag_get(root, tag))
581                 return 0;
582
583         node = rcu_dereference_raw(root->rnode);
584         if (node == NULL)
585                 return 0;
586
587         if (!radix_tree_is_indirect_ptr(node))
588                 return (index == 0);
589         node = indirect_to_ptr(node);
590
591         height = node->height;
592         if (index > radix_tree_maxindex(height))
593                 return 0;
594
595         shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
596
597         for ( ; ; ) {
598                 int offset;
599
600                 if (node == NULL)
601                         return 0;
602
603                 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
604
605                 /*
606                  * This is just a debug check.  Later, we can bale as soon as
607                  * we see an unset tag.
608                  */
609                 if (!tag_get(node, tag, offset))
610                         saw_unset_tag = 1;
611                 if (height == 1)
612                         return !!tag_get(node, tag, offset);
613                 node = rcu_dereference_raw(node->slots[offset]);
614                 shift -= RADIX_TREE_MAP_SHIFT;
615                 height--;
616         }
617 }
618 EXPORT_SYMBOL(radix_tree_tag_get);
619
620 /**
621  *      radix_tree_next_hole    -    find the next hole (not-present entry)
622  *      @root:          tree root
623  *      @index:         index key
624  *      @max_scan:      maximum range to search
625  *
626  *      Search the set [index, min(index+max_scan-1, MAX_INDEX)] for the lowest
627  *      indexed hole.
628  *
629  *      Returns: the index of the hole if found, otherwise returns an index
630  *      outside of the set specified (in which case 'return - index >= max_scan'
631  *      will be true). In rare cases of index wrap-around, 0 will be returned.
632  *
633  *      radix_tree_next_hole may be called under rcu_read_lock. However, like
634  *      radix_tree_gang_lookup, this will not atomically search a snapshot of
635  *      the tree at a single point in time. For example, if a hole is created
636  *      at index 5, then subsequently a hole is created at index 10,
637  *      radix_tree_next_hole covering both indexes may return 10 if called
638  *      under rcu_read_lock.
639  */
640 unsigned long radix_tree_next_hole(struct radix_tree_root *root,
641                                 unsigned long index, unsigned long max_scan)
642 {
643         unsigned long i;
644
645         for (i = 0; i < max_scan; i++) {
646                 if (!radix_tree_lookup(root, index))
647                         break;
648                 index++;
649                 if (index == 0)
650                         break;
651         }
652
653         return index;
654 }
655 EXPORT_SYMBOL(radix_tree_next_hole);
656
657 /**
658  *      radix_tree_prev_hole    -    find the prev hole (not-present entry)
659  *      @root:          tree root
660  *      @index:         index key
661  *      @max_scan:      maximum range to search
662  *
663  *      Search backwards in the range [max(index-max_scan+1, 0), index]
664  *      for the first hole.
665  *
666  *      Returns: the index of the hole if found, otherwise returns an index
667  *      outside of the set specified (in which case 'index - return >= max_scan'
668  *      will be true). In rare cases of wrap-around, ULONG_MAX will be returned.
669  *
670  *      radix_tree_next_hole may be called under rcu_read_lock. However, like
671  *      radix_tree_gang_lookup, this will not atomically search a snapshot of
672  *      the tree at a single point in time. For example, if a hole is created
673  *      at index 10, then subsequently a hole is created at index 5,
674  *      radix_tree_prev_hole covering both indexes may return 5 if called under
675  *      rcu_read_lock.
676  */
677 unsigned long radix_tree_prev_hole(struct radix_tree_root *root,
678                                    unsigned long index, unsigned long max_scan)
679 {
680         unsigned long i;
681
682         for (i = 0; i < max_scan; i++) {
683                 if (!radix_tree_lookup(root, index))
684                         break;
685                 index--;
686                 if (index == ULONG_MAX)
687                         break;
688         }
689
690         return index;
691 }
692 EXPORT_SYMBOL(radix_tree_prev_hole);
693
694 static unsigned int
695 __lookup(struct radix_tree_node *slot, void ***results, unsigned long index,
696         unsigned int max_items, unsigned long *next_index)
697 {
698         unsigned int nr_found = 0;
699         unsigned int shift, height;
700         unsigned long i;
701
702         height = slot->height;
703         if (height == 0)
704                 goto out;
705         shift = (height-1) * RADIX_TREE_MAP_SHIFT;
706
707         for ( ; height > 1; height--) {
708                 i = (index >> shift) & RADIX_TREE_MAP_MASK;
709                 for (;;) {
710                         if (slot->slots[i] != NULL)
711                                 break;
712                         index &= ~((1UL << shift) - 1);
713                         index += 1UL << shift;
714                         if (index == 0)
715                                 goto out;       /* 32-bit wraparound */
716                         i++;
717                         if (i == RADIX_TREE_MAP_SIZE)
718                                 goto out;
719                 }
720
721                 shift -= RADIX_TREE_MAP_SHIFT;
722                 slot = rcu_dereference_raw(slot->slots[i]);
723                 if (slot == NULL)
724                         goto out;
725         }
726
727         /* Bottom level: grab some items */
728         for (i = index & RADIX_TREE_MAP_MASK; i < RADIX_TREE_MAP_SIZE; i++) {
729                 index++;
730                 if (slot->slots[i]) {
731                         results[nr_found++] = &(slot->slots[i]);
732                         if (nr_found == max_items)
733                                 goto out;
734                 }
735         }
736 out:
737         *next_index = index;
738         return nr_found;
739 }
740
741 /**
742  *      radix_tree_gang_lookup - perform multiple lookup on a radix tree
743  *      @root:          radix tree root
744  *      @results:       where the results of the lookup are placed
745  *      @first_index:   start the lookup from this key
746  *      @max_items:     place up to this many items at *results
747  *
748  *      Performs an index-ascending scan of the tree for present items.  Places
749  *      them at *@results and returns the number of items which were placed at
750  *      *@results.
751  *
752  *      The implementation is naive.
753  *
754  *      Like radix_tree_lookup, radix_tree_gang_lookup may be called under
755  *      rcu_read_lock. In this case, rather than the returned results being
756  *      an atomic snapshot of the tree at a single point in time, the semantics
757  *      of an RCU protected gang lookup are as though multiple radix_tree_lookups
758  *      have been issued in individual locks, and results stored in 'results'.
759  */
760 unsigned int
761 radix_tree_gang_lookup(struct radix_tree_root *root, void **results,
762                         unsigned long first_index, unsigned int max_items)
763 {
764         unsigned long max_index;
765         struct radix_tree_node *node;
766         unsigned long cur_index = first_index;
767         unsigned int ret;
768
769         node = rcu_dereference_raw(root->rnode);
770         if (!node)
771                 return 0;
772
773         if (!radix_tree_is_indirect_ptr(node)) {
774                 if (first_index > 0)
775                         return 0;
776                 results[0] = node;
777                 return 1;
778         }
779         node = indirect_to_ptr(node);
780
781         max_index = radix_tree_maxindex(node->height);
782
783         ret = 0;
784         while (ret < max_items) {
785                 unsigned int nr_found, slots_found, i;
786                 unsigned long next_index;       /* Index of next search */
787
788                 if (cur_index > max_index)
789                         break;
790                 slots_found = __lookup(node, (void ***)results + ret, cur_index,
791                                         max_items - ret, &next_index);
792                 nr_found = 0;
793                 for (i = 0; i < slots_found; i++) {
794                         struct radix_tree_node *slot;
795                         slot = *(((void ***)results)[ret + i]);
796                         if (!slot)
797                                 continue;
798                         results[ret + nr_found] = rcu_dereference_raw(slot);
799                         nr_found++;
800                 }
801                 ret += nr_found;
802                 if (next_index == 0)
803                         break;
804                 cur_index = next_index;
805         }
806
807         return ret;
808 }
809 EXPORT_SYMBOL(radix_tree_gang_lookup);
810
811 /**
812  *      radix_tree_gang_lookup_slot - perform multiple slot lookup on radix tree
813  *      @root:          radix tree root
814  *      @results:       where the results of the lookup are placed
815  *      @first_index:   start the lookup from this key
816  *      @max_items:     place up to this many items at *results
817  *
818  *      Performs an index-ascending scan of the tree for present items.  Places
819  *      their slots at *@results and returns the number of items which were
820  *      placed at *@results.
821  *
822  *      The implementation is naive.
823  *
824  *      Like radix_tree_gang_lookup as far as RCU and locking goes. Slots must
825  *      be dereferenced with radix_tree_deref_slot, and if using only RCU
826  *      protection, radix_tree_deref_slot may fail requiring a retry.
827  */
828 unsigned int
829 radix_tree_gang_lookup_slot(struct radix_tree_root *root, void ***results,
830                         unsigned long first_index, unsigned int max_items)
831 {
832         unsigned long max_index;
833         struct radix_tree_node *node;
834         unsigned long cur_index = first_index;
835         unsigned int ret;
836
837         node = rcu_dereference_raw(root->rnode);
838         if (!node)
839                 return 0;
840
841         if (!radix_tree_is_indirect_ptr(node)) {
842                 if (first_index > 0)
843                         return 0;
844                 results[0] = (void **)&root->rnode;
845                 return 1;
846         }
847         node = indirect_to_ptr(node);
848
849         max_index = radix_tree_maxindex(node->height);
850
851         ret = 0;
852         while (ret < max_items) {
853                 unsigned int slots_found;
854                 unsigned long next_index;       /* Index of next search */
855
856                 if (cur_index > max_index)
857                         break;
858                 slots_found = __lookup(node, results + ret, cur_index,
859                                         max_items - ret, &next_index);
860                 ret += slots_found;
861                 if (next_index == 0)
862                         break;
863                 cur_index = next_index;
864         }
865
866         return ret;
867 }
868 EXPORT_SYMBOL(radix_tree_gang_lookup_slot);
869
870 /*
871  * FIXME: the two tag_get()s here should use find_next_bit() instead of
872  * open-coding the search.
873  */
874 static unsigned int
875 __lookup_tag(struct radix_tree_node *slot, void ***results, unsigned long index,
876         unsigned int max_items, unsigned long *next_index, unsigned int tag)
877 {
878         unsigned int nr_found = 0;
879         unsigned int shift, height;
880
881         height = slot->height;
882         if (height == 0)
883                 goto out;
884         shift = (height-1) * RADIX_TREE_MAP_SHIFT;
885
886         while (height > 0) {
887                 unsigned long i = (index >> shift) & RADIX_TREE_MAP_MASK ;
888
889                 for (;;) {
890                         if (tag_get(slot, tag, i))
891                                 break;
892                         index &= ~((1UL << shift) - 1);
893                         index += 1UL << shift;
894                         if (index == 0)
895                                 goto out;       /* 32-bit wraparound */
896                         i++;
897                         if (i == RADIX_TREE_MAP_SIZE)
898                                 goto out;
899                 }
900                 height--;
901                 if (height == 0) {      /* Bottom level: grab some items */
902                         unsigned long j = index & RADIX_TREE_MAP_MASK;
903
904                         for ( ; j < RADIX_TREE_MAP_SIZE; j++) {
905                                 index++;
906                                 if (!tag_get(slot, tag, j))
907                                         continue;
908                                 /*
909                                  * Even though the tag was found set, we need to
910                                  * recheck that we have a non-NULL node, because
911                                  * if this lookup is lockless, it may have been
912                                  * subsequently deleted.
913                                  *
914                                  * Similar care must be taken in any place that
915                                  * lookup ->slots[x] without a lock (ie. can't
916                                  * rely on its value remaining the same).
917                                  */
918                                 if (slot->slots[j]) {
919                                         results[nr_found++] = &(slot->slots[j]);
920                                         if (nr_found == max_items)
921                                                 goto out;
922                                 }
923                         }
924                 }
925                 shift -= RADIX_TREE_MAP_SHIFT;
926                 slot = rcu_dereference_raw(slot->slots[i]);
927                 if (slot == NULL)
928                         break;
929         }
930 out:
931         *next_index = index;
932         return nr_found;
933 }
934
935 /**
936  *      radix_tree_gang_lookup_tag - perform multiple lookup on a radix tree
937  *                                   based on a tag
938  *      @root:          radix tree root
939  *      @results:       where the results of the lookup are placed
940  *      @first_index:   start the lookup from this key
941  *      @max_items:     place up to this many items at *results
942  *      @tag:           the tag index (< RADIX_TREE_MAX_TAGS)
943  *
944  *      Performs an index-ascending scan of the tree for present items which
945  *      have the tag indexed by @tag set.  Places the items at *@results and
946  *      returns the number of items which were placed at *@results.
947  */
948 unsigned int
949 radix_tree_gang_lookup_tag(struct radix_tree_root *root, void **results,
950                 unsigned long first_index, unsigned int max_items,
951                 unsigned int tag)
952 {
953         struct radix_tree_node *node;
954         unsigned long max_index;
955         unsigned long cur_index = first_index;
956         unsigned int ret;
957
958         /* check the root's tag bit */
959         if (!root_tag_get(root, tag))
960                 return 0;
961
962         node = rcu_dereference_raw(root->rnode);
963         if (!node)
964                 return 0;
965
966         if (!radix_tree_is_indirect_ptr(node)) {
967                 if (first_index > 0)
968                         return 0;
969                 results[0] = node;
970                 return 1;
971         }
972         node = indirect_to_ptr(node);
973
974         max_index = radix_tree_maxindex(node->height);
975
976         ret = 0;
977         while (ret < max_items) {
978                 unsigned int nr_found, slots_found, i;
979                 unsigned long next_index;       /* Index of next search */
980
981                 if (cur_index > max_index)
982                         break;
983                 slots_found = __lookup_tag(node, (void ***)results + ret,
984                                 cur_index, max_items - ret, &next_index, tag);
985                 nr_found = 0;
986                 for (i = 0; i < slots_found; i++) {
987                         struct radix_tree_node *slot;
988                         slot = *(((void ***)results)[ret + i]);
989                         if (!slot)
990                                 continue;
991                         results[ret + nr_found] =
992                                 indirect_to_ptr(rcu_dereference_raw(slot));
993                         nr_found++;
994                 }
995                 ret += nr_found;
996                 if (next_index == 0)
997                         break;
998                 cur_index = next_index;
999         }
1000
1001         return ret;
1002 }
1003 EXPORT_SYMBOL(radix_tree_gang_lookup_tag);
1004
1005 /**
1006  *      radix_tree_gang_lookup_tag_slot - perform multiple slot lookup on a
1007  *                                        radix tree based on a tag
1008  *      @root:          radix tree root
1009  *      @results:       where the results of the lookup are placed
1010  *      @first_index:   start the lookup from this key
1011  *      @max_items:     place up to this many items at *results
1012  *      @tag:           the tag index (< RADIX_TREE_MAX_TAGS)
1013  *
1014  *      Performs an index-ascending scan of the tree for present items which
1015  *      have the tag indexed by @tag set.  Places the slots at *@results and
1016  *      returns the number of slots which were placed at *@results.
1017  */
1018 unsigned int
1019 radix_tree_gang_lookup_tag_slot(struct radix_tree_root *root, void ***results,
1020                 unsigned long first_index, unsigned int max_items,
1021                 unsigned int tag)
1022 {
1023         struct radix_tree_node *node;
1024         unsigned long max_index;
1025         unsigned long cur_index = first_index;
1026         unsigned int ret;
1027
1028         /* check the root's tag bit */
1029         if (!root_tag_get(root, tag))
1030                 return 0;
1031
1032         node = rcu_dereference_raw(root->rnode);
1033         if (!node)
1034                 return 0;
1035
1036         if (!radix_tree_is_indirect_ptr(node)) {
1037                 if (first_index > 0)
1038                         return 0;
1039                 results[0] = (void **)&root->rnode;
1040                 return 1;
1041         }
1042         node = indirect_to_ptr(node);
1043
1044         max_index = radix_tree_maxindex(node->height);
1045
1046         ret = 0;
1047         while (ret < max_items) {
1048                 unsigned int slots_found;
1049                 unsigned long next_index;       /* Index of next search */
1050
1051                 if (cur_index > max_index)
1052                         break;
1053                 slots_found = __lookup_tag(node, results + ret,
1054                                 cur_index, max_items - ret, &next_index, tag);
1055                 ret += slots_found;
1056                 if (next_index == 0)
1057                         break;
1058                 cur_index = next_index;
1059         }
1060
1061         return ret;
1062 }
1063 EXPORT_SYMBOL(radix_tree_gang_lookup_tag_slot);
1064
1065
1066 /**
1067  *      radix_tree_shrink    -    shrink height of a radix tree to minimal
1068  *      @root           radix tree root
1069  */
1070 static inline void radix_tree_shrink(struct radix_tree_root *root)
1071 {
1072         /* try to shrink tree height */
1073         while (root->height > 0) {
1074                 struct radix_tree_node *to_free = root->rnode;
1075                 void *newptr;
1076
1077                 BUG_ON(!radix_tree_is_indirect_ptr(to_free));
1078                 to_free = indirect_to_ptr(to_free);
1079
1080                 /*
1081                  * The candidate node has more than one child, or its child
1082                  * is not at the leftmost slot, we cannot shrink.
1083                  */
1084                 if (to_free->count != 1)
1085                         break;
1086                 if (!to_free->slots[0])
1087                         break;
1088
1089                 /*
1090                  * We don't need rcu_assign_pointer(), since we are simply
1091                  * moving the node from one part of the tree to another: if it
1092                  * was safe to dereference the old pointer to it
1093                  * (to_free->slots[0]), it will be safe to dereference the new
1094                  * one (root->rnode) as far as dependent read barriers go.
1095                  */
1096                 newptr = to_free->slots[0];
1097                 if (root->height > 1)
1098                         newptr = ptr_to_indirect(newptr);
1099                 root->rnode = newptr;
1100                 root->height--;
1101
1102                 /*
1103                  * We have a dilemma here. The node's slot[0] must not be
1104                  * NULLed in case there are concurrent lookups expecting to
1105                  * find the item. However if this was a bottom-level node,
1106                  * then it may be subject to the slot pointer being visible
1107                  * to callers dereferencing it. If item corresponding to
1108                  * slot[0] is subsequently deleted, these callers would expect
1109                  * their slot to become empty sooner or later.
1110                  *
1111                  * For example, lockless pagecache will look up a slot, deref
1112                  * the page pointer, and if the page is 0 refcount it means it
1113                  * was concurrently deleted from pagecache so try the deref
1114                  * again. Fortunately there is already a requirement for logic
1115                  * to retry the entire slot lookup -- the indirect pointer
1116                  * problem (replacing direct root node with an indirect pointer
1117                  * also results in a stale slot). So tag the slot as indirect
1118                  * to force callers to retry.
1119                  */
1120                 if (root->height == 0)
1121                         *((unsigned long *)&to_free->slots[0]) |=
1122                                                 RADIX_TREE_INDIRECT_PTR;
1123
1124                 radix_tree_node_free(to_free);
1125         }
1126 }
1127
1128 /**
1129  *      radix_tree_delete    -    delete an item from a radix tree
1130  *      @root:          radix tree root
1131  *      @index:         index key
1132  *
1133  *      Remove the item at @index from the radix tree rooted at @root.
1134  *
1135  *      Returns the address of the deleted item, or NULL if it was not present.
1136  */
1137 void *radix_tree_delete(struct radix_tree_root *root, unsigned long index)
1138 {
1139         /*
1140          * The radix tree path needs to be one longer than the maximum path
1141          * since the "list" is null terminated.
1142          */
1143         struct radix_tree_path path[RADIX_TREE_MAX_PATH + 1], *pathp = path;
1144         struct radix_tree_node *slot = NULL;
1145         struct radix_tree_node *to_free;
1146         unsigned int height, shift;
1147         int tag;
1148         int offset;
1149
1150         height = root->height;
1151         if (index > radix_tree_maxindex(height))
1152                 goto out;
1153
1154         slot = root->rnode;
1155         if (height == 0) {
1156                 root_tag_clear_all(root);
1157                 root->rnode = NULL;
1158                 goto out;
1159         }
1160         slot = indirect_to_ptr(slot);
1161
1162         shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
1163         pathp->node = NULL;
1164
1165         do {
1166                 if (slot == NULL)
1167                         goto out;
1168
1169                 pathp++;
1170                 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
1171                 pathp->offset = offset;
1172                 pathp->node = slot;
1173                 slot = slot->slots[offset];
1174                 shift -= RADIX_TREE_MAP_SHIFT;
1175                 height--;
1176         } while (height > 0);
1177
1178         if (slot == NULL)
1179                 goto out;
1180
1181         /*
1182          * Clear all tags associated with the just-deleted item
1183          */
1184         for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) {
1185                 if (tag_get(pathp->node, tag, pathp->offset))
1186                         radix_tree_tag_clear(root, index, tag);
1187         }
1188
1189         to_free = NULL;
1190         /* Now free the nodes we do not need anymore */
1191         while (pathp->node) {
1192                 pathp->node->slots[pathp->offset] = NULL;
1193                 pathp->node->count--;
1194                 /*
1195                  * Queue the node for deferred freeing after the
1196                  * last reference to it disappears (set NULL, above).
1197                  */
1198                 if (to_free)
1199                         radix_tree_node_free(to_free);
1200
1201                 if (pathp->node->count) {
1202                         if (pathp->node == indirect_to_ptr(root->rnode))
1203                                 radix_tree_shrink(root);
1204                         goto out;
1205                 }
1206
1207                 /* Node with zero slots in use so free it */
1208                 to_free = pathp->node;
1209                 pathp--;
1210
1211         }
1212         root_tag_clear_all(root);
1213         root->height = 0;
1214         root->rnode = NULL;
1215         if (to_free)
1216                 radix_tree_node_free(to_free);
1217
1218 out:
1219         return slot;
1220 }
1221 EXPORT_SYMBOL(radix_tree_delete);
1222
1223 /**
1224  *      radix_tree_tagged - test whether any items in the tree are tagged
1225  *      @root:          radix tree root
1226  *      @tag:           tag to test
1227  */
1228 int radix_tree_tagged(struct radix_tree_root *root, unsigned int tag)
1229 {
1230         return root_tag_get(root, tag);
1231 }
1232 EXPORT_SYMBOL(radix_tree_tagged);
1233
1234 static void
1235 radix_tree_node_ctor(void *node)
1236 {
1237         memset(node, 0, sizeof(struct radix_tree_node));
1238 }
1239
1240 static __init unsigned long __maxindex(unsigned int height)
1241 {
1242         unsigned int width = height * RADIX_TREE_MAP_SHIFT;
1243         int shift = RADIX_TREE_INDEX_BITS - width;
1244
1245         if (shift < 0)
1246                 return ~0UL;
1247         if (shift >= BITS_PER_LONG)
1248                 return 0UL;
1249         return ~0UL >> shift;
1250 }
1251
1252 static __init void radix_tree_init_maxindex(void)
1253 {
1254         unsigned int i;
1255
1256         for (i = 0; i < ARRAY_SIZE(height_to_maxindex); i++)
1257                 height_to_maxindex[i] = __maxindex(i);
1258 }
1259
1260 static int radix_tree_callback(struct notifier_block *nfb,
1261                             unsigned long action,
1262                             void *hcpu)
1263 {
1264        int cpu = (long)hcpu;
1265        struct radix_tree_preload *rtp;
1266
1267        /* Free per-cpu pool of perloaded nodes */
1268        if (action == CPU_DEAD || action == CPU_DEAD_FROZEN) {
1269                rtp = &per_cpu(radix_tree_preloads, cpu);
1270                while (rtp->nr) {
1271                        kmem_cache_free(radix_tree_node_cachep,
1272                                        rtp->nodes[rtp->nr-1]);
1273                        rtp->nodes[rtp->nr-1] = NULL;
1274                        rtp->nr--;
1275                }
1276        }
1277        return NOTIFY_OK;
1278 }
1279
1280 void __init radix_tree_init(void)
1281 {
1282         radix_tree_node_cachep = kmem_cache_create("radix_tree_node",
1283                         sizeof(struct radix_tree_node), 0,
1284                         SLAB_PANIC | SLAB_RECLAIM_ACCOUNT,
1285                         radix_tree_node_ctor);
1286         radix_tree_init_maxindex();
1287         hotcpu_notifier(radix_tree_callback, 0);
1288 }