[PATCH] USB: new devices for the Option driver
[usb.git] / lib / radix-tree.c
blob7097bb239e406b2ccca5772cb9516e852498f9ef
1 /*
2 * Copyright (C) 2001 Momchil Velikov
3 * Portions Copyright (C) 2001 Christoph Hellwig
4 * Copyright (C) 2005 SGI, Christoph Lameter <clameter@sgi.com>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2, or (at
9 * your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include <linux/errno.h>
22 #include <linux/init.h>
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/radix-tree.h>
26 #include <linux/percpu.h>
27 #include <linux/slab.h>
28 #include <linux/notifier.h>
29 #include <linux/cpu.h>
30 #include <linux/gfp.h>
31 #include <linux/string.h>
32 #include <linux/bitops.h>
35 #ifdef __KERNEL__
36 #define RADIX_TREE_MAP_SHIFT 6
37 #else
38 #define RADIX_TREE_MAP_SHIFT 3 /* For more stressful testing */
39 #endif
41 #define RADIX_TREE_MAP_SIZE (1UL << RADIX_TREE_MAP_SHIFT)
42 #define RADIX_TREE_MAP_MASK (RADIX_TREE_MAP_SIZE-1)
44 #define RADIX_TREE_TAG_LONGS \
45 ((RADIX_TREE_MAP_SIZE + BITS_PER_LONG - 1) / BITS_PER_LONG)
47 struct radix_tree_node {
48 unsigned int count;
49 void *slots[RADIX_TREE_MAP_SIZE];
50 unsigned long tags[RADIX_TREE_MAX_TAGS][RADIX_TREE_TAG_LONGS];
53 struct radix_tree_path {
54 struct radix_tree_node *node;
55 int offset;
58 #define RADIX_TREE_INDEX_BITS (8 /* CHAR_BIT */ * sizeof(unsigned long))
59 #define RADIX_TREE_MAX_PATH (RADIX_TREE_INDEX_BITS/RADIX_TREE_MAP_SHIFT + 2)
61 static unsigned long height_to_maxindex[RADIX_TREE_MAX_PATH] __read_mostly;
64 * Radix tree node cache.
66 static kmem_cache_t *radix_tree_node_cachep;
69 * Per-cpu pool of preloaded nodes
71 struct radix_tree_preload {
72 int nr;
73 struct radix_tree_node *nodes[RADIX_TREE_MAX_PATH];
75 DEFINE_PER_CPU(struct radix_tree_preload, radix_tree_preloads) = { 0, };
78 * This assumes that the caller has performed appropriate preallocation, and
79 * that the caller has pinned this thread of control to the current CPU.
81 static struct radix_tree_node *
82 radix_tree_node_alloc(struct radix_tree_root *root)
84 struct radix_tree_node *ret;
86 ret = kmem_cache_alloc(radix_tree_node_cachep, root->gfp_mask);
87 if (ret == NULL && !(root->gfp_mask & __GFP_WAIT)) {
88 struct radix_tree_preload *rtp;
90 rtp = &__get_cpu_var(radix_tree_preloads);
91 if (rtp->nr) {
92 ret = rtp->nodes[rtp->nr - 1];
93 rtp->nodes[rtp->nr - 1] = NULL;
94 rtp->nr--;
97 return ret;
100 static inline void
101 radix_tree_node_free(struct radix_tree_node *node)
103 kmem_cache_free(radix_tree_node_cachep, node);
107 * Load up this CPU's radix_tree_node buffer with sufficient objects to
108 * ensure that the addition of a single element in the tree cannot fail. On
109 * success, return zero, with preemption disabled. On error, return -ENOMEM
110 * with preemption not disabled.
112 int radix_tree_preload(gfp_t gfp_mask)
114 struct radix_tree_preload *rtp;
115 struct radix_tree_node *node;
116 int ret = -ENOMEM;
118 preempt_disable();
119 rtp = &__get_cpu_var(radix_tree_preloads);
120 while (rtp->nr < ARRAY_SIZE(rtp->nodes)) {
121 preempt_enable();
122 node = kmem_cache_alloc(radix_tree_node_cachep, gfp_mask);
123 if (node == NULL)
124 goto out;
125 preempt_disable();
126 rtp = &__get_cpu_var(radix_tree_preloads);
127 if (rtp->nr < ARRAY_SIZE(rtp->nodes))
128 rtp->nodes[rtp->nr++] = node;
129 else
130 kmem_cache_free(radix_tree_node_cachep, node);
132 ret = 0;
133 out:
134 return ret;
137 static inline void tag_set(struct radix_tree_node *node, unsigned int tag,
138 int offset)
140 __set_bit(offset, node->tags[tag]);
143 static inline void tag_clear(struct radix_tree_node *node, unsigned int tag,
144 int offset)
146 __clear_bit(offset, node->tags[tag]);
149 static inline int tag_get(struct radix_tree_node *node, unsigned int tag,
150 int offset)
152 return test_bit(offset, node->tags[tag]);
156 * Returns 1 if any slot in the node has this tag set.
157 * Otherwise returns 0.
159 static inline int any_tag_set(struct radix_tree_node *node, unsigned int tag)
161 int idx;
162 for (idx = 0; idx < RADIX_TREE_TAG_LONGS; idx++) {
163 if (node->tags[tag][idx])
164 return 1;
166 return 0;
170 * Return the maximum key which can be store into a
171 * radix tree with height HEIGHT.
173 static inline unsigned long radix_tree_maxindex(unsigned int height)
175 return height_to_maxindex[height];
179 * Extend a radix tree so it can store key @index.
181 static int radix_tree_extend(struct radix_tree_root *root, unsigned long index)
183 struct radix_tree_node *node;
184 unsigned int height;
185 char tags[RADIX_TREE_MAX_TAGS];
186 int tag;
188 /* Figure out what the height should be. */
189 height = root->height + 1;
190 while (index > radix_tree_maxindex(height))
191 height++;
193 if (root->rnode == NULL) {
194 root->height = height;
195 goto out;
199 * Prepare the tag status of the top-level node for propagation
200 * into the newly-pushed top-level node(s)
202 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) {
203 tags[tag] = 0;
204 if (any_tag_set(root->rnode, tag))
205 tags[tag] = 1;
208 do {
209 if (!(node = radix_tree_node_alloc(root)))
210 return -ENOMEM;
212 /* Increase the height. */
213 node->slots[0] = root->rnode;
215 /* Propagate the aggregated tag info into the new root */
216 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) {
217 if (tags[tag])
218 tag_set(node, tag, 0);
221 node->count = 1;
222 root->rnode = node;
223 root->height++;
224 } while (height > root->height);
225 out:
226 return 0;
230 * radix_tree_insert - insert into a radix tree
231 * @root: radix tree root
232 * @index: index key
233 * @item: item to insert
235 * Insert an item into the radix tree at position @index.
237 int radix_tree_insert(struct radix_tree_root *root,
238 unsigned long index, void *item)
240 struct radix_tree_node *node = NULL, *slot;
241 unsigned int height, shift;
242 int offset;
243 int error;
245 /* Make sure the tree is high enough. */
246 if ((!index && !root->rnode) ||
247 index > radix_tree_maxindex(root->height)) {
248 error = radix_tree_extend(root, index);
249 if (error)
250 return error;
253 slot = root->rnode;
254 height = root->height;
255 shift = (height-1) * RADIX_TREE_MAP_SHIFT;
257 offset = 0; /* uninitialised var warning */
258 do {
259 if (slot == NULL) {
260 /* Have to add a child node. */
261 if (!(slot = radix_tree_node_alloc(root)))
262 return -ENOMEM;
263 if (node) {
264 node->slots[offset] = slot;
265 node->count++;
266 } else
267 root->rnode = slot;
270 /* Go a level down */
271 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
272 node = slot;
273 slot = node->slots[offset];
274 shift -= RADIX_TREE_MAP_SHIFT;
275 height--;
276 } while (height > 0);
278 if (slot != NULL)
279 return -EEXIST;
281 BUG_ON(!node);
282 node->count++;
283 node->slots[offset] = item;
284 BUG_ON(tag_get(node, 0, offset));
285 BUG_ON(tag_get(node, 1, offset));
287 return 0;
289 EXPORT_SYMBOL(radix_tree_insert);
291 static inline void **__lookup_slot(struct radix_tree_root *root,
292 unsigned long index)
294 unsigned int height, shift;
295 struct radix_tree_node **slot;
297 height = root->height;
298 if (index > radix_tree_maxindex(height))
299 return NULL;
301 shift = (height-1) * RADIX_TREE_MAP_SHIFT;
302 slot = &root->rnode;
304 while (height > 0) {
305 if (*slot == NULL)
306 return NULL;
308 slot = (struct radix_tree_node **)
309 ((*slot)->slots +
310 ((index >> shift) & RADIX_TREE_MAP_MASK));
311 shift -= RADIX_TREE_MAP_SHIFT;
312 height--;
315 return (void **)slot;
319 * radix_tree_lookup_slot - lookup a slot in a radix tree
320 * @root: radix tree root
321 * @index: index key
323 * Lookup the slot corresponding to the position @index in the radix tree
324 * @root. This is useful for update-if-exists operations.
326 void **radix_tree_lookup_slot(struct radix_tree_root *root, unsigned long index)
328 return __lookup_slot(root, index);
330 EXPORT_SYMBOL(radix_tree_lookup_slot);
333 * radix_tree_lookup - perform lookup operation on a radix tree
334 * @root: radix tree root
335 * @index: index key
337 * Lookup the item at the position @index in the radix tree @root.
339 void *radix_tree_lookup(struct radix_tree_root *root, unsigned long index)
341 void **slot;
343 slot = __lookup_slot(root, index);
344 return slot != NULL ? *slot : NULL;
346 EXPORT_SYMBOL(radix_tree_lookup);
349 * radix_tree_tag_set - set a tag on a radix tree node
350 * @root: radix tree root
351 * @index: index key
352 * @tag: tag index
354 * Set the search tag (which must be < RADIX_TREE_MAX_TAGS)
355 * corresponding to @index in the radix tree. From
356 * the root all the way down to the leaf node.
358 * Returns the address of the tagged item. Setting a tag on a not-present
359 * item is a bug.
361 void *radix_tree_tag_set(struct radix_tree_root *root,
362 unsigned long index, unsigned int tag)
364 unsigned int height, shift;
365 struct radix_tree_node *slot;
367 height = root->height;
368 if (index > radix_tree_maxindex(height))
369 return NULL;
371 shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
372 slot = root->rnode;
374 while (height > 0) {
375 int offset;
377 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
378 if (!tag_get(slot, tag, offset))
379 tag_set(slot, tag, offset);
380 slot = slot->slots[offset];
381 BUG_ON(slot == NULL);
382 shift -= RADIX_TREE_MAP_SHIFT;
383 height--;
386 return slot;
388 EXPORT_SYMBOL(radix_tree_tag_set);
391 * radix_tree_tag_clear - clear a tag on a radix tree node
392 * @root: radix tree root
393 * @index: index key
394 * @tag: tag index
396 * Clear the search tag (which must be < RADIX_TREE_MAX_TAGS)
397 * corresponding to @index in the radix tree. If
398 * this causes the leaf node to have no tags set then clear the tag in the
399 * next-to-leaf node, etc.
401 * Returns the address of the tagged item on success, else NULL. ie:
402 * has the same return value and semantics as radix_tree_lookup().
404 void *radix_tree_tag_clear(struct radix_tree_root *root,
405 unsigned long index, unsigned int tag)
407 struct radix_tree_path path[RADIX_TREE_MAX_PATH], *pathp = path;
408 struct radix_tree_node *slot;
409 unsigned int height, shift;
410 void *ret = NULL;
412 height = root->height;
413 if (index > radix_tree_maxindex(height))
414 goto out;
416 shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
417 pathp->node = NULL;
418 slot = root->rnode;
420 while (height > 0) {
421 int offset;
423 if (slot == NULL)
424 goto out;
426 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
427 pathp[1].offset = offset;
428 pathp[1].node = slot;
429 slot = slot->slots[offset];
430 pathp++;
431 shift -= RADIX_TREE_MAP_SHIFT;
432 height--;
435 ret = slot;
436 if (ret == NULL)
437 goto out;
439 do {
440 if (!tag_get(pathp->node, tag, pathp->offset))
441 goto out;
442 tag_clear(pathp->node, tag, pathp->offset);
443 if (any_tag_set(pathp->node, tag))
444 goto out;
445 pathp--;
446 } while (pathp->node);
447 out:
448 return ret;
450 EXPORT_SYMBOL(radix_tree_tag_clear);
452 #ifndef __KERNEL__ /* Only the test harness uses this at present */
454 * radix_tree_tag_get - get a tag on a radix tree node
455 * @root: radix tree root
456 * @index: index key
457 * @tag: tag index (< RADIX_TREE_MAX_TAGS)
459 * Return values:
461 * 0: tag not present
462 * 1: tag present, set
463 * -1: tag present, unset
465 int radix_tree_tag_get(struct radix_tree_root *root,
466 unsigned long index, unsigned int tag)
468 unsigned int height, shift;
469 struct radix_tree_node *slot;
470 int saw_unset_tag = 0;
472 height = root->height;
473 if (index > radix_tree_maxindex(height))
474 return 0;
476 shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
477 slot = root->rnode;
479 for ( ; ; ) {
480 int offset;
482 if (slot == NULL)
483 return 0;
485 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
488 * This is just a debug check. Later, we can bale as soon as
489 * we see an unset tag.
491 if (!tag_get(slot, tag, offset))
492 saw_unset_tag = 1;
493 if (height == 1) {
494 int ret = tag_get(slot, tag, offset);
496 BUG_ON(ret && saw_unset_tag);
497 return ret ? 1 : -1;
499 slot = slot->slots[offset];
500 shift -= RADIX_TREE_MAP_SHIFT;
501 height--;
504 EXPORT_SYMBOL(radix_tree_tag_get);
505 #endif
507 static unsigned int
508 __lookup(struct radix_tree_root *root, void **results, unsigned long index,
509 unsigned int max_items, unsigned long *next_index)
511 unsigned int nr_found = 0;
512 unsigned int shift, height;
513 struct radix_tree_node *slot;
514 unsigned long i;
516 height = root->height;
517 if (height == 0)
518 goto out;
520 shift = (height-1) * RADIX_TREE_MAP_SHIFT;
521 slot = root->rnode;
523 for ( ; height > 1; height--) {
525 for (i = (index >> shift) & RADIX_TREE_MAP_MASK ;
526 i < RADIX_TREE_MAP_SIZE; i++) {
527 if (slot->slots[i] != NULL)
528 break;
529 index &= ~((1UL << shift) - 1);
530 index += 1UL << shift;
531 if (index == 0)
532 goto out; /* 32-bit wraparound */
534 if (i == RADIX_TREE_MAP_SIZE)
535 goto out;
537 shift -= RADIX_TREE_MAP_SHIFT;
538 slot = slot->slots[i];
541 /* Bottom level: grab some items */
542 for (i = index & RADIX_TREE_MAP_MASK; i < RADIX_TREE_MAP_SIZE; i++) {
543 index++;
544 if (slot->slots[i]) {
545 results[nr_found++] = slot->slots[i];
546 if (nr_found == max_items)
547 goto out;
550 out:
551 *next_index = index;
552 return nr_found;
556 * radix_tree_gang_lookup - perform multiple lookup on a radix tree
557 * @root: radix tree root
558 * @results: where the results of the lookup are placed
559 * @first_index: start the lookup from this key
560 * @max_items: place up to this many items at *results
562 * Performs an index-ascending scan of the tree for present items. Places
563 * them at *@results and returns the number of items which were placed at
564 * *@results.
566 * The implementation is naive.
568 unsigned int
569 radix_tree_gang_lookup(struct radix_tree_root *root, void **results,
570 unsigned long first_index, unsigned int max_items)
572 const unsigned long max_index = radix_tree_maxindex(root->height);
573 unsigned long cur_index = first_index;
574 unsigned int ret = 0;
576 while (ret < max_items) {
577 unsigned int nr_found;
578 unsigned long next_index; /* Index of next search */
580 if (cur_index > max_index)
581 break;
582 nr_found = __lookup(root, results + ret, cur_index,
583 max_items - ret, &next_index);
584 ret += nr_found;
585 if (next_index == 0)
586 break;
587 cur_index = next_index;
589 return ret;
591 EXPORT_SYMBOL(radix_tree_gang_lookup);
594 * FIXME: the two tag_get()s here should use find_next_bit() instead of
595 * open-coding the search.
597 static unsigned int
598 __lookup_tag(struct radix_tree_root *root, void **results, unsigned long index,
599 unsigned int max_items, unsigned long *next_index, unsigned int tag)
601 unsigned int nr_found = 0;
602 unsigned int shift;
603 unsigned int height = root->height;
604 struct radix_tree_node *slot;
606 shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
607 slot = root->rnode;
609 while (height > 0) {
610 unsigned long i = (index >> shift) & RADIX_TREE_MAP_MASK;
612 for ( ; i < RADIX_TREE_MAP_SIZE; i++) {
613 if (tag_get(slot, tag, i)) {
614 BUG_ON(slot->slots[i] == NULL);
615 break;
617 index &= ~((1UL << shift) - 1);
618 index += 1UL << shift;
619 if (index == 0)
620 goto out; /* 32-bit wraparound */
622 if (i == RADIX_TREE_MAP_SIZE)
623 goto out;
624 height--;
625 if (height == 0) { /* Bottom level: grab some items */
626 unsigned long j = index & RADIX_TREE_MAP_MASK;
628 for ( ; j < RADIX_TREE_MAP_SIZE; j++) {
629 index++;
630 if (tag_get(slot, tag, j)) {
631 BUG_ON(slot->slots[j] == NULL);
632 results[nr_found++] = slot->slots[j];
633 if (nr_found == max_items)
634 goto out;
638 shift -= RADIX_TREE_MAP_SHIFT;
639 slot = slot->slots[i];
641 out:
642 *next_index = index;
643 return nr_found;
647 * radix_tree_gang_lookup_tag - perform multiple lookup on a radix tree
648 * based on a tag
649 * @root: radix tree root
650 * @results: where the results of the lookup are placed
651 * @first_index: start the lookup from this key
652 * @max_items: place up to this many items at *results
653 * @tag: the tag index (< RADIX_TREE_MAX_TAGS)
655 * Performs an index-ascending scan of the tree for present items which
656 * have the tag indexed by @tag set. Places the items at *@results and
657 * returns the number of items which were placed at *@results.
659 unsigned int
660 radix_tree_gang_lookup_tag(struct radix_tree_root *root, void **results,
661 unsigned long first_index, unsigned int max_items,
662 unsigned int tag)
664 const unsigned long max_index = radix_tree_maxindex(root->height);
665 unsigned long cur_index = first_index;
666 unsigned int ret = 0;
668 while (ret < max_items) {
669 unsigned int nr_found;
670 unsigned long next_index; /* Index of next search */
672 if (cur_index > max_index)
673 break;
674 nr_found = __lookup_tag(root, results + ret, cur_index,
675 max_items - ret, &next_index, tag);
676 ret += nr_found;
677 if (next_index == 0)
678 break;
679 cur_index = next_index;
681 return ret;
683 EXPORT_SYMBOL(radix_tree_gang_lookup_tag);
686 * radix_tree_shrink - shrink height of a radix tree to minimal
687 * @root radix tree root
689 static inline void radix_tree_shrink(struct radix_tree_root *root)
691 /* try to shrink tree height */
692 while (root->height > 1 &&
693 root->rnode->count == 1 &&
694 root->rnode->slots[0]) {
695 struct radix_tree_node *to_free = root->rnode;
697 root->rnode = to_free->slots[0];
698 root->height--;
699 /* must only free zeroed nodes into the slab */
700 tag_clear(to_free, 0, 0);
701 tag_clear(to_free, 1, 0);
702 to_free->slots[0] = NULL;
703 to_free->count = 0;
704 radix_tree_node_free(to_free);
709 * radix_tree_delete - delete an item from a radix tree
710 * @root: radix tree root
711 * @index: index key
713 * Remove the item at @index from the radix tree rooted at @root.
715 * Returns the address of the deleted item, or NULL if it was not present.
717 void *radix_tree_delete(struct radix_tree_root *root, unsigned long index)
719 struct radix_tree_path path[RADIX_TREE_MAX_PATH], *pathp = path;
720 struct radix_tree_path *orig_pathp;
721 struct radix_tree_node *slot;
722 unsigned int height, shift;
723 void *ret = NULL;
724 char tags[RADIX_TREE_MAX_TAGS];
725 int nr_cleared_tags;
726 int tag;
727 int offset;
729 height = root->height;
730 if (index > radix_tree_maxindex(height))
731 goto out;
733 shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
734 pathp->node = NULL;
735 slot = root->rnode;
737 for ( ; height > 0; height--) {
738 if (slot == NULL)
739 goto out;
741 pathp++;
742 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
743 pathp->offset = offset;
744 pathp->node = slot;
745 slot = slot->slots[offset];
746 shift -= RADIX_TREE_MAP_SHIFT;
749 ret = slot;
750 if (ret == NULL)
751 goto out;
753 orig_pathp = pathp;
756 * Clear all tags associated with the just-deleted item
758 nr_cleared_tags = 0;
759 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) {
760 tags[tag] = 1;
761 if (tag_get(pathp->node, tag, pathp->offset)) {
762 tag_clear(pathp->node, tag, pathp->offset);
763 if (!any_tag_set(pathp->node, tag)) {
764 tags[tag] = 0;
765 nr_cleared_tags++;
770 for (pathp--; nr_cleared_tags && pathp->node; pathp--) {
771 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) {
772 if (tags[tag])
773 continue;
775 tag_clear(pathp->node, tag, pathp->offset);
776 if (any_tag_set(pathp->node, tag)) {
777 tags[tag] = 1;
778 nr_cleared_tags--;
783 /* Now free the nodes we do not need anymore */
784 for (pathp = orig_pathp; pathp->node; pathp--) {
785 pathp->node->slots[pathp->offset] = NULL;
786 pathp->node->count--;
788 if (pathp->node->count) {
789 if (pathp->node == root->rnode)
790 radix_tree_shrink(root);
791 goto out;
794 /* Node with zero slots in use so free it */
795 radix_tree_node_free(pathp->node);
797 root->rnode = NULL;
798 root->height = 0;
799 out:
800 return ret;
802 EXPORT_SYMBOL(radix_tree_delete);
805 * radix_tree_tagged - test whether any items in the tree are tagged
806 * @root: radix tree root
807 * @tag: tag to test
809 int radix_tree_tagged(struct radix_tree_root *root, unsigned int tag)
811 struct radix_tree_node *rnode;
812 rnode = root->rnode;
813 if (!rnode)
814 return 0;
815 return any_tag_set(rnode, tag);
817 EXPORT_SYMBOL(radix_tree_tagged);
819 static void
820 radix_tree_node_ctor(void *node, kmem_cache_t *cachep, unsigned long flags)
822 memset(node, 0, sizeof(struct radix_tree_node));
825 static __init unsigned long __maxindex(unsigned int height)
827 unsigned int tmp = height * RADIX_TREE_MAP_SHIFT;
828 unsigned long index = (~0UL >> (RADIX_TREE_INDEX_BITS - tmp - 1)) >> 1;
830 if (tmp >= RADIX_TREE_INDEX_BITS)
831 index = ~0UL;
832 return index;
835 static __init void radix_tree_init_maxindex(void)
837 unsigned int i;
839 for (i = 0; i < ARRAY_SIZE(height_to_maxindex); i++)
840 height_to_maxindex[i] = __maxindex(i);
843 #ifdef CONFIG_HOTPLUG_CPU
844 static int radix_tree_callback(struct notifier_block *nfb,
845 unsigned long action,
846 void *hcpu)
848 int cpu = (long)hcpu;
849 struct radix_tree_preload *rtp;
851 /* Free per-cpu pool of perloaded nodes */
852 if (action == CPU_DEAD) {
853 rtp = &per_cpu(radix_tree_preloads, cpu);
854 while (rtp->nr) {
855 kmem_cache_free(radix_tree_node_cachep,
856 rtp->nodes[rtp->nr-1]);
857 rtp->nodes[rtp->nr-1] = NULL;
858 rtp->nr--;
861 return NOTIFY_OK;
863 #endif /* CONFIG_HOTPLUG_CPU */
865 void __init radix_tree_init(void)
867 radix_tree_node_cachep = kmem_cache_create("radix_tree_node",
868 sizeof(struct radix_tree_node), 0,
869 SLAB_PANIC, radix_tree_node_ctor, NULL);
870 radix_tree_init_maxindex();
871 hotcpu_notifier(radix_tree_callback, 0);