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>
36 #define RADIX_TREE_MAP_SHIFT 6
38 #define RADIX_TREE_MAP_SHIFT 3 /* For more stressful testing */
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 {
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;
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 {
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);
92 ret = rtp->nodes[rtp->nr - 1];
93 rtp->nodes[rtp->nr - 1] = NULL;
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;
119 rtp = &__get_cpu_var(radix_tree_preloads);
120 while (rtp->nr < ARRAY_SIZE(rtp->nodes)) {
122 node = kmem_cache_alloc(radix_tree_node_cachep, gfp_mask);
126 rtp = &__get_cpu_var(radix_tree_preloads);
127 if (rtp->nr < ARRAY_SIZE(rtp->nodes))
128 rtp->nodes[rtp->nr++] = node;
130 kmem_cache_free(radix_tree_node_cachep, node);
137 static inline void tag_set(struct radix_tree_node *node, unsigned int tag,
140 __set_bit(offset, node->tags[tag]);
143 static inline void tag_clear(struct radix_tree_node *node, unsigned int tag,
146 __clear_bit(offset, node->tags[tag]);
149 static inline int tag_get(struct radix_tree_node *node, unsigned int tag,
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)
162 for (idx = 0; idx < RADIX_TREE_TAG_LONGS; idx++) {
163 if (node->tags[tag][idx])
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;
185 char tags[RADIX_TREE_MAX_TAGS];
188 /* Figure out what the height should be. */
189 height = root->height + 1;
190 while (index > radix_tree_maxindex(height))
193 if (root->rnode == NULL) {
194 root->height = height;
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++) {
204 if (any_tag_set(root->rnode, tag))
209 if (!(node = radix_tree_node_alloc(root)))
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++) {
218 tag_set(node, tag, 0);
224 } while (height > root->height);
230 * radix_tree_insert - insert into a radix tree
231 * @root: radix tree root
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;
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);
254 height = root->height;
255 shift = (height-1) * RADIX_TREE_MAP_SHIFT;
257 offset = 0; /* uninitialised var warning */
260 /* Have to add a child node. */
261 if (!(slot = radix_tree_node_alloc(root)))
264 node->slots[offset] = slot;
270 /* Go a level down */
271 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
273 slot = node->slots[offset];
274 shift -= RADIX_TREE_MAP_SHIFT;
276 } while (height > 0);
283 node->slots[offset] = item;
284 BUG_ON(tag_get(node, 0, offset));
285 BUG_ON(tag_get(node, 1, offset));
289 EXPORT_SYMBOL(radix_tree_insert);
291 static inline void **__lookup_slot(struct radix_tree_root *root,
294 unsigned int height, shift;
295 struct radix_tree_node **slot;
297 height = root->height;
298 if (index > radix_tree_maxindex(height))
301 shift = (height-1) * RADIX_TREE_MAP_SHIFT;
308 slot = (struct radix_tree_node **)
310 ((index >> shift) & RADIX_TREE_MAP_MASK));
311 shift -= RADIX_TREE_MAP_SHIFT;
315 return (void **)slot;
319 * radix_tree_lookup_slot - lookup a slot in a radix tree
320 * @root: radix tree root
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
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)
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
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
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))
371 shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
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;
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
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;
412 height = root->height;
413 if (index > radix_tree_maxindex(height))
416 shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
426 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
427 pathp[1].offset = offset;
428 pathp[1].node = slot;
429 slot = slot->slots[offset];
431 shift -= RADIX_TREE_MAP_SHIFT;
440 if (!tag_get(pathp->node, tag, pathp->offset))
442 tag_clear(pathp->node, tag, pathp->offset);
443 if (any_tag_set(pathp->node, tag))
446 } while (pathp->node);
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
457 * @tag: tag index (< RADIX_TREE_MAX_TAGS)
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))
476 shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
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))
494 int ret = tag_get(slot, tag, offset);
496 BUG_ON(ret && saw_unset_tag);
499 slot = slot->slots[offset];
500 shift -= RADIX_TREE_MAP_SHIFT;
504 EXPORT_SYMBOL(radix_tree_tag_get);
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;
516 height = root->height;
520 shift = (height-1) * RADIX_TREE_MAP_SHIFT;
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)
529 index &= ~((1UL << shift) - 1);
530 index += 1UL << shift;
532 goto out; /* 32-bit wraparound */
534 if (i == RADIX_TREE_MAP_SIZE)
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++) {
544 if (slot->slots[i]) {
545 results[nr_found++] = slot->slots[i];
546 if (nr_found == max_items)
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
566 * The implementation is naive.
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)
582 nr_found = __lookup(root, results + ret, cur_index,
583 max_items - ret, &next_index);
587 cur_index = next_index;
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.
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;
603 unsigned int height = root->height;
604 struct radix_tree_node *slot;
606 shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
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);
617 index &= ~((1UL << shift) - 1);
618 index += 1UL << shift;
620 goto out; /* 32-bit wraparound */
622 if (i == RADIX_TREE_MAP_SIZE)
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++) {
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)
638 shift -= RADIX_TREE_MAP_SHIFT;
639 slot = slot->slots[i];
647 * radix_tree_gang_lookup_tag - perform multiple lookup on a radix tree
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.
660 radix_tree_gang_lookup_tag(struct radix_tree_root *root, void **results,
661 unsigned long first_index, unsigned int max_items,
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)
674 nr_found = __lookup_tag(root, results + ret, cur_index,
675 max_items - ret, &next_index, tag);
679 cur_index = next_index;
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];
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;
704 radix_tree_node_free(to_free);
709 * radix_tree_delete - delete an item from a radix tree
710 * @root: radix tree root
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;
724 char tags[RADIX_TREE_MAX_TAGS];
729 height = root->height;
730 if (index > radix_tree_maxindex(height))
733 shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
737 for ( ; height > 0; height--) {
742 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
743 pathp->offset = offset;
745 slot = slot->slots[offset];
746 shift -= RADIX_TREE_MAP_SHIFT;
756 * Clear all tags associated with the just-deleted item
759 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) {
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)) {
770 for (pathp--; nr_cleared_tags && pathp->node; pathp--) {
771 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) {
775 tag_clear(pathp->node, tag, pathp->offset);
776 if (any_tag_set(pathp->node, tag)) {
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);
794 /* Node with zero slots in use so free it */
795 radix_tree_node_free(pathp->node);
802 EXPORT_SYMBOL(radix_tree_delete);
805 * radix_tree_tagged - test whether any items in the tree are tagged
806 * @root: radix tree root
809 int radix_tree_tagged(struct radix_tree_root *root, unsigned int tag)
811 struct radix_tree_node *rnode;
815 return any_tag_set(rnode, tag);
817 EXPORT_SYMBOL(radix_tree_tagged);
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)
835 static __init void radix_tree_init_maxindex(void)
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,
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);
855 kmem_cache_free(radix_tree_node_cachep,
856 rtp->nodes[rtp->nr-1]);
857 rtp->nodes[rtp->nr-1] = NULL;
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);