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 (CONFIG_BASE_SMALL ? 4 : 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, };
77 static inline gfp_t root_gfp_mask(struct radix_tree_root *root)
79 return root->gfp_mask & __GFP_BITS_MASK;
83 * This assumes that the caller has performed appropriate preallocation, and
84 * that the caller has pinned this thread of control to the current CPU.
86 static struct radix_tree_node *
87 radix_tree_node_alloc(struct radix_tree_root *root)
89 struct radix_tree_node *ret;
90 gfp_t gfp_mask = root_gfp_mask(root);
92 ret = kmem_cache_alloc(radix_tree_node_cachep, gfp_mask);
93 if (ret == NULL && !(gfp_mask & __GFP_WAIT)) {
94 struct radix_tree_preload *rtp;
96 rtp = &__get_cpu_var(radix_tree_preloads);
98 ret = rtp->nodes[rtp->nr - 1];
99 rtp->nodes[rtp->nr - 1] = NULL;
107 radix_tree_node_free(struct radix_tree_node *node)
109 kmem_cache_free(radix_tree_node_cachep, node);
113 * Load up this CPU's radix_tree_node buffer with sufficient objects to
114 * ensure that the addition of a single element in the tree cannot fail. On
115 * success, return zero, with preemption disabled. On error, return -ENOMEM
116 * with preemption not disabled.
118 int radix_tree_preload(gfp_t gfp_mask)
120 struct radix_tree_preload *rtp;
121 struct radix_tree_node *node;
125 rtp = &__get_cpu_var(radix_tree_preloads);
126 while (rtp->nr < ARRAY_SIZE(rtp->nodes)) {
128 node = kmem_cache_alloc(radix_tree_node_cachep, gfp_mask);
132 rtp = &__get_cpu_var(radix_tree_preloads);
133 if (rtp->nr < ARRAY_SIZE(rtp->nodes))
134 rtp->nodes[rtp->nr++] = node;
136 kmem_cache_free(radix_tree_node_cachep, node);
143 static inline void tag_set(struct radix_tree_node *node, unsigned int tag,
146 __set_bit(offset, node->tags[tag]);
149 static inline void tag_clear(struct radix_tree_node *node, unsigned int tag,
152 __clear_bit(offset, node->tags[tag]);
155 static inline int tag_get(struct radix_tree_node *node, unsigned int tag,
158 return test_bit(offset, node->tags[tag]);
161 static inline void root_tag_set(struct radix_tree_root *root, unsigned int tag)
163 root->gfp_mask |= (__force gfp_t)(1 << (tag + __GFP_BITS_SHIFT));
167 static inline void root_tag_clear(struct radix_tree_root *root, unsigned int tag)
169 root->gfp_mask &= (__force gfp_t)~(1 << (tag + __GFP_BITS_SHIFT));
172 static inline void root_tag_clear_all(struct radix_tree_root *root)
174 root->gfp_mask &= __GFP_BITS_MASK;
177 static inline int root_tag_get(struct radix_tree_root *root, unsigned int tag)
179 return (__force unsigned)root->gfp_mask & (1 << (tag + __GFP_BITS_SHIFT));
183 * Returns 1 if any slot in the node has this tag set.
184 * Otherwise returns 0.
186 static inline int any_tag_set(struct radix_tree_node *node, unsigned int tag)
189 for (idx = 0; idx < RADIX_TREE_TAG_LONGS; idx++) {
190 if (node->tags[tag][idx])
197 * Return the maximum key which can be store into a
198 * radix tree with height HEIGHT.
200 static inline unsigned long radix_tree_maxindex(unsigned int height)
202 return height_to_maxindex[height];
206 * Extend a radix tree so it can store key @index.
208 static int radix_tree_extend(struct radix_tree_root *root, unsigned long index)
210 struct radix_tree_node *node;
214 /* Figure out what the height should be. */
215 height = root->height + 1;
216 while (index > radix_tree_maxindex(height))
219 if (root->rnode == NULL) {
220 root->height = height;
225 if (!(node = radix_tree_node_alloc(root)))
228 /* Increase the height. */
229 node->slots[0] = root->rnode;
231 /* Propagate the aggregated tag info into the new root */
232 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) {
233 if (root_tag_get(root, tag))
234 tag_set(node, tag, 0);
240 } while (height > root->height);
246 * radix_tree_insert - insert into a radix tree
247 * @root: radix tree root
249 * @item: item to insert
251 * Insert an item into the radix tree at position @index.
253 int radix_tree_insert(struct radix_tree_root *root,
254 unsigned long index, void *item)
256 struct radix_tree_node *node = NULL, *slot;
257 unsigned int height, shift;
261 /* Make sure the tree is high enough. */
262 if (index > radix_tree_maxindex(root->height)) {
263 error = radix_tree_extend(root, index);
269 height = root->height;
270 shift = (height-1) * RADIX_TREE_MAP_SHIFT;
272 offset = 0; /* uninitialised var warning */
275 /* Have to add a child node. */
276 if (!(slot = radix_tree_node_alloc(root)))
279 node->slots[offset] = slot;
285 /* Go a level down */
286 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
288 slot = node->slots[offset];
289 shift -= RADIX_TREE_MAP_SHIFT;
298 node->slots[offset] = item;
299 BUG_ON(tag_get(node, 0, offset));
300 BUG_ON(tag_get(node, 1, offset));
303 BUG_ON(root_tag_get(root, 0));
304 BUG_ON(root_tag_get(root, 1));
309 EXPORT_SYMBOL(radix_tree_insert);
311 static inline void **__lookup_slot(struct radix_tree_root *root,
314 unsigned int height, shift;
315 struct radix_tree_node **slot;
317 height = root->height;
319 if (index > radix_tree_maxindex(height))
322 if (height == 0 && root->rnode)
323 return (void **)&root->rnode;
325 shift = (height-1) * RADIX_TREE_MAP_SHIFT;
332 slot = (struct radix_tree_node **)
334 ((index >> shift) & RADIX_TREE_MAP_MASK));
335 shift -= RADIX_TREE_MAP_SHIFT;
339 return (void **)slot;
343 * radix_tree_lookup_slot - lookup a slot in a radix tree
344 * @root: radix tree root
347 * Lookup the slot corresponding to the position @index in the radix tree
348 * @root. This is useful for update-if-exists operations.
350 void **radix_tree_lookup_slot(struct radix_tree_root *root, unsigned long index)
352 return __lookup_slot(root, index);
354 EXPORT_SYMBOL(radix_tree_lookup_slot);
357 * radix_tree_lookup - perform lookup operation on a radix tree
358 * @root: radix tree root
361 * Lookup the item at the position @index in the radix tree @root.
363 void *radix_tree_lookup(struct radix_tree_root *root, unsigned long index)
367 slot = __lookup_slot(root, index);
368 return slot != NULL ? *slot : NULL;
370 EXPORT_SYMBOL(radix_tree_lookup);
373 * radix_tree_tag_set - set a tag on a radix tree node
374 * @root: radix tree root
378 * Set the search tag (which must be < RADIX_TREE_MAX_TAGS)
379 * corresponding to @index in the radix tree. From
380 * the root all the way down to the leaf node.
382 * Returns the address of the tagged item. Setting a tag on a not-present
385 void *radix_tree_tag_set(struct radix_tree_root *root,
386 unsigned long index, unsigned int tag)
388 unsigned int height, shift;
389 struct radix_tree_node *slot;
391 height = root->height;
392 BUG_ON(index > radix_tree_maxindex(height));
395 shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
400 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
401 if (!tag_get(slot, tag, offset))
402 tag_set(slot, tag, offset);
403 slot = slot->slots[offset];
404 BUG_ON(slot == NULL);
405 shift -= RADIX_TREE_MAP_SHIFT;
409 /* set the root's tag bit */
410 if (slot && !root_tag_get(root, tag))
411 root_tag_set(root, tag);
415 EXPORT_SYMBOL(radix_tree_tag_set);
418 * radix_tree_tag_clear - clear a tag on a radix tree node
419 * @root: radix tree root
423 * Clear the search tag (which must be < RADIX_TREE_MAX_TAGS)
424 * corresponding to @index in the radix tree. If
425 * this causes the leaf node to have no tags set then clear the tag in the
426 * next-to-leaf node, etc.
428 * Returns the address of the tagged item on success, else NULL. ie:
429 * has the same return value and semantics as radix_tree_lookup().
431 void *radix_tree_tag_clear(struct radix_tree_root *root,
432 unsigned long index, unsigned int tag)
434 struct radix_tree_path path[RADIX_TREE_MAX_PATH], *pathp = path;
435 struct radix_tree_node *slot = NULL;
436 unsigned int height, shift;
438 height = root->height;
439 if (index > radix_tree_maxindex(height))
442 shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
452 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
453 pathp[1].offset = offset;
454 pathp[1].node = slot;
455 slot = slot->slots[offset];
457 shift -= RADIX_TREE_MAP_SHIFT;
464 while (pathp->node) {
465 if (!tag_get(pathp->node, tag, pathp->offset))
467 tag_clear(pathp->node, tag, pathp->offset);
468 if (any_tag_set(pathp->node, tag))
473 /* clear the root's tag bit */
474 if (root_tag_get(root, tag))
475 root_tag_clear(root, tag);
480 EXPORT_SYMBOL(radix_tree_tag_clear);
482 #ifndef __KERNEL__ /* Only the test harness uses this at present */
484 * radix_tree_tag_get - get a tag on a radix tree node
485 * @root: radix tree root
487 * @tag: tag index (< RADIX_TREE_MAX_TAGS)
491 * 0: tag not present or not set
494 int radix_tree_tag_get(struct radix_tree_root *root,
495 unsigned long index, unsigned int tag)
497 unsigned int height, shift;
498 struct radix_tree_node *slot;
499 int saw_unset_tag = 0;
501 height = root->height;
502 if (index > radix_tree_maxindex(height))
505 /* check the root's tag bit */
506 if (!root_tag_get(root, tag))
512 shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
521 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
524 * This is just a debug check. Later, we can bale as soon as
525 * we see an unset tag.
527 if (!tag_get(slot, tag, offset))
530 int ret = tag_get(slot, tag, offset);
532 BUG_ON(ret && saw_unset_tag);
535 slot = slot->slots[offset];
536 shift -= RADIX_TREE_MAP_SHIFT;
540 EXPORT_SYMBOL(radix_tree_tag_get);
544 __lookup(struct radix_tree_root *root, void **results, unsigned long index,
545 unsigned int max_items, unsigned long *next_index)
547 unsigned int nr_found = 0;
548 unsigned int shift, height;
549 struct radix_tree_node *slot;
552 height = root->height;
554 if (root->rnode && index == 0)
555 results[nr_found++] = root->rnode;
559 shift = (height-1) * RADIX_TREE_MAP_SHIFT;
562 for ( ; height > 1; height--) {
564 for (i = (index >> shift) & RADIX_TREE_MAP_MASK ;
565 i < RADIX_TREE_MAP_SIZE; i++) {
566 if (slot->slots[i] != NULL)
568 index &= ~((1UL << shift) - 1);
569 index += 1UL << shift;
571 goto out; /* 32-bit wraparound */
573 if (i == RADIX_TREE_MAP_SIZE)
576 shift -= RADIX_TREE_MAP_SHIFT;
577 slot = slot->slots[i];
580 /* Bottom level: grab some items */
581 for (i = index & RADIX_TREE_MAP_MASK; i < RADIX_TREE_MAP_SIZE; i++) {
583 if (slot->slots[i]) {
584 results[nr_found++] = slot->slots[i];
585 if (nr_found == max_items)
595 * radix_tree_gang_lookup - perform multiple lookup on a radix tree
596 * @root: radix tree root
597 * @results: where the results of the lookup are placed
598 * @first_index: start the lookup from this key
599 * @max_items: place up to this many items at *results
601 * Performs an index-ascending scan of the tree for present items. Places
602 * them at *@results and returns the number of items which were placed at
605 * The implementation is naive.
608 radix_tree_gang_lookup(struct radix_tree_root *root, void **results,
609 unsigned long first_index, unsigned int max_items)
611 const unsigned long max_index = radix_tree_maxindex(root->height);
612 unsigned long cur_index = first_index;
613 unsigned int ret = 0;
615 while (ret < max_items) {
616 unsigned int nr_found;
617 unsigned long next_index; /* Index of next search */
619 if (cur_index > max_index)
621 nr_found = __lookup(root, results + ret, cur_index,
622 max_items - ret, &next_index);
626 cur_index = next_index;
630 EXPORT_SYMBOL(radix_tree_gang_lookup);
633 * FIXME: the two tag_get()s here should use find_next_bit() instead of
634 * open-coding the search.
637 __lookup_tag(struct radix_tree_root *root, void **results, unsigned long index,
638 unsigned int max_items, unsigned long *next_index, unsigned int tag)
640 unsigned int nr_found = 0;
642 unsigned int height = root->height;
643 struct radix_tree_node *slot;
646 if (root->rnode && index == 0)
647 results[nr_found++] = root->rnode;
651 shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
655 unsigned long i = (index >> shift) & RADIX_TREE_MAP_MASK;
657 for ( ; i < RADIX_TREE_MAP_SIZE; i++) {
658 if (tag_get(slot, tag, i)) {
659 BUG_ON(slot->slots[i] == NULL);
662 index &= ~((1UL << shift) - 1);
663 index += 1UL << shift;
665 goto out; /* 32-bit wraparound */
667 if (i == RADIX_TREE_MAP_SIZE)
670 if (height == 0) { /* Bottom level: grab some items */
671 unsigned long j = index & RADIX_TREE_MAP_MASK;
673 for ( ; j < RADIX_TREE_MAP_SIZE; j++) {
675 if (tag_get(slot, tag, j)) {
676 BUG_ON(slot->slots[j] == NULL);
677 results[nr_found++] = slot->slots[j];
678 if (nr_found == max_items)
683 shift -= RADIX_TREE_MAP_SHIFT;
684 slot = slot->slots[i];
685 } while (height > 0);
692 * radix_tree_gang_lookup_tag - perform multiple lookup on a radix tree
694 * @root: radix tree root
695 * @results: where the results of the lookup are placed
696 * @first_index: start the lookup from this key
697 * @max_items: place up to this many items at *results
698 * @tag: the tag index (< RADIX_TREE_MAX_TAGS)
700 * Performs an index-ascending scan of the tree for present items which
701 * have the tag indexed by @tag set. Places the items at *@results and
702 * returns the number of items which were placed at *@results.
705 radix_tree_gang_lookup_tag(struct radix_tree_root *root, void **results,
706 unsigned long first_index, unsigned int max_items,
709 const unsigned long max_index = radix_tree_maxindex(root->height);
710 unsigned long cur_index = first_index;
711 unsigned int ret = 0;
713 /* check the root's tag bit */
714 if (!root_tag_get(root, tag))
717 while (ret < max_items) {
718 unsigned int nr_found;
719 unsigned long next_index; /* Index of next search */
721 if (cur_index > max_index)
723 nr_found = __lookup_tag(root, results + ret, cur_index,
724 max_items - ret, &next_index, tag);
728 cur_index = next_index;
732 EXPORT_SYMBOL(radix_tree_gang_lookup_tag);
735 * radix_tree_shrink - shrink height of a radix tree to minimal
736 * @root radix tree root
738 static inline void radix_tree_shrink(struct radix_tree_root *root)
740 /* try to shrink tree height */
741 while (root->height > 0 &&
742 root->rnode->count == 1 &&
743 root->rnode->slots[0]) {
744 struct radix_tree_node *to_free = root->rnode;
746 root->rnode = to_free->slots[0];
748 /* must only free zeroed nodes into the slab */
749 tag_clear(to_free, 0, 0);
750 tag_clear(to_free, 1, 0);
751 to_free->slots[0] = NULL;
753 radix_tree_node_free(to_free);
758 * radix_tree_delete - delete an item from a radix tree
759 * @root: radix tree root
762 * Remove the item at @index from the radix tree rooted at @root.
764 * Returns the address of the deleted item, or NULL if it was not present.
766 void *radix_tree_delete(struct radix_tree_root *root, unsigned long index)
768 struct radix_tree_path path[RADIX_TREE_MAX_PATH], *pathp = path;
769 struct radix_tree_node *slot = NULL;
770 unsigned int height, shift;
774 height = root->height;
775 if (index > radix_tree_maxindex(height))
779 if (height == 0 && root->rnode) {
780 root_tag_clear_all(root);
785 shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
793 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
794 pathp->offset = offset;
796 slot = slot->slots[offset];
797 shift -= RADIX_TREE_MAP_SHIFT;
799 } while (height > 0);
805 * Clear all tags associated with the just-deleted item
807 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) {
808 if (tag_get(pathp->node, tag, pathp->offset))
809 radix_tree_tag_clear(root, index, tag);
812 /* Now free the nodes we do not need anymore */
813 while (pathp->node) {
814 pathp->node->slots[pathp->offset] = NULL;
815 pathp->node->count--;
817 if (pathp->node->count) {
818 if (pathp->node == root->rnode)
819 radix_tree_shrink(root);
823 /* Node with zero slots in use so free it */
824 radix_tree_node_free(pathp->node);
828 root_tag_clear_all(root);
835 EXPORT_SYMBOL(radix_tree_delete);
838 * radix_tree_tagged - test whether any items in the tree are tagged
839 * @root: radix tree root
842 int radix_tree_tagged(struct radix_tree_root *root, unsigned int tag)
844 return root_tag_get(root, tag);
846 EXPORT_SYMBOL(radix_tree_tagged);
849 radix_tree_node_ctor(void *node, kmem_cache_t *cachep, unsigned long flags)
851 memset(node, 0, sizeof(struct radix_tree_node));
854 static __init unsigned long __maxindex(unsigned int height)
856 unsigned int tmp = height * RADIX_TREE_MAP_SHIFT;
857 unsigned long index = (~0UL >> (RADIX_TREE_INDEX_BITS - tmp - 1)) >> 1;
859 if (tmp >= RADIX_TREE_INDEX_BITS)
864 static __init void radix_tree_init_maxindex(void)
868 for (i = 0; i < ARRAY_SIZE(height_to_maxindex); i++)
869 height_to_maxindex[i] = __maxindex(i);
872 #ifdef CONFIG_HOTPLUG_CPU
873 static int radix_tree_callback(struct notifier_block *nfb,
874 unsigned long action,
877 int cpu = (long)hcpu;
878 struct radix_tree_preload *rtp;
880 /* Free per-cpu pool of perloaded nodes */
881 if (action == CPU_DEAD) {
882 rtp = &per_cpu(radix_tree_preloads, cpu);
884 kmem_cache_free(radix_tree_node_cachep,
885 rtp->nodes[rtp->nr-1]);
886 rtp->nodes[rtp->nr-1] = NULL;
892 #endif /* CONFIG_HOTPLUG_CPU */
894 void __init radix_tree_init(void)
896 radix_tree_node_cachep = kmem_cache_create("radix_tree_node",
897 sizeof(struct radix_tree_node), 0,
898 SLAB_PANIC, radix_tree_node_ctor, NULL);
899 radix_tree_init_maxindex();
900 hotcpu_notifier(radix_tree_callback, 0);