2 * Copyright (C) 2001 Momchil Velikov
3 * Portions Copyright (C) 2001 Christoph Hellwig
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2, or (at
8 * your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include <linux/errno.h>
21 #include <linux/init.h>
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/radix-tree.h>
25 #include <linux/percpu.h>
26 #include <linux/slab.h>
27 #include <linux/notifier.h>
28 #include <linux/cpu.h>
29 #include <linux/gfp.h>
30 #include <linux/string.h>
31 #include <linux/bitops.h>
34 * Radix tree node definition.
36 * RADIX_TREE_MAP_SHIFT must be >= log2(BITS_PER_LONG). Otherwise the tags
37 * array will have zero size and the set_tag() arithmetic will go wrong.
40 #define RADIX_TREE_MAP_SHIFT 6
42 #define RADIX_TREE_MAP_SHIFT 3 /* For more stressful testing */
44 #define RADIX_TREE_TAGS 2
46 #define RADIX_TREE_MAP_SIZE (1UL << RADIX_TREE_MAP_SHIFT)
47 #define RADIX_TREE_MAP_MASK (RADIX_TREE_MAP_SIZE-1)
49 #define RADIX_TREE_TAG_LONGS \
50 ((RADIX_TREE_MAP_SIZE + BITS_PER_LONG - 1) / BITS_PER_LONG)
52 struct radix_tree_node {
54 void *slots[RADIX_TREE_MAP_SIZE];
55 unsigned long tags[RADIX_TREE_TAGS][RADIX_TREE_TAG_LONGS];
58 struct radix_tree_path {
59 struct radix_tree_node *node, **slot;
63 #define RADIX_TREE_INDEX_BITS (8 /* CHAR_BIT */ * sizeof(unsigned long))
64 #define RADIX_TREE_MAX_PATH (RADIX_TREE_INDEX_BITS/RADIX_TREE_MAP_SHIFT + 2)
66 static unsigned long height_to_maxindex[RADIX_TREE_MAX_PATH];
69 * Radix tree node cache.
71 static kmem_cache_t *radix_tree_node_cachep;
74 * Per-cpu pool of preloaded nodes
76 struct radix_tree_preload {
78 struct radix_tree_node *nodes[RADIX_TREE_MAX_PATH];
80 DEFINE_PER_CPU(struct radix_tree_preload, radix_tree_preloads) = { 0, };
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;
91 ret = kmem_cache_alloc(radix_tree_node_cachep, root->gfp_mask);
92 if (ret == NULL && !(root->gfp_mask & __GFP_WAIT)) {
93 struct radix_tree_preload *rtp;
95 rtp = &__get_cpu_var(radix_tree_preloads);
97 ret = rtp->nodes[rtp->nr - 1];
98 rtp->nodes[rtp->nr - 1] = NULL;
106 radix_tree_node_free(struct radix_tree_node *node)
108 kmem_cache_free(radix_tree_node_cachep, node);
112 * Load up this CPU's radix_tree_node buffer with sufficient objects to
113 * ensure that the addition of a single element in the tree cannot fail. On
114 * success, return zero, with preemption disabled. On error, return -ENOMEM
115 * with preemption not disabled.
117 int radix_tree_preload(int gfp_mask)
119 struct radix_tree_preload *rtp;
120 struct radix_tree_node *node;
124 rtp = &__get_cpu_var(radix_tree_preloads);
125 while (rtp->nr < ARRAY_SIZE(rtp->nodes)) {
127 node = kmem_cache_alloc(radix_tree_node_cachep, gfp_mask);
131 rtp = &__get_cpu_var(radix_tree_preloads);
132 if (rtp->nr < ARRAY_SIZE(rtp->nodes))
133 rtp->nodes[rtp->nr++] = node;
135 kmem_cache_free(radix_tree_node_cachep, node);
142 static inline void tag_set(struct radix_tree_node *node, int tag, int offset)
144 if (!test_bit(offset, &node->tags[tag][0]))
145 __set_bit(offset, &node->tags[tag][0]);
148 static inline void tag_clear(struct radix_tree_node *node, int tag, int offset)
150 __clear_bit(offset, &node->tags[tag][0]);
153 static inline int tag_get(struct radix_tree_node *node, int tag, int offset)
155 return test_bit(offset, &node->tags[tag][0]);
159 * Return the maximum key which can be store into a
160 * radix tree with height HEIGHT.
162 static inline unsigned long radix_tree_maxindex(unsigned int height)
164 return height_to_maxindex[height];
168 * Extend a radix tree so it can store key @index.
170 static int radix_tree_extend(struct radix_tree_root *root, unsigned long index)
172 struct radix_tree_node *node;
174 char tags[RADIX_TREE_TAGS];
177 /* Figure out what the height should be. */
178 height = root->height + 1;
179 while (index > radix_tree_maxindex(height))
182 if (root->rnode == NULL) {
183 root->height = height;
188 * Prepare the tag status of the top-level node for propagation
189 * into the newly-pushed top-level node(s)
191 for (tag = 0; tag < RADIX_TREE_TAGS; tag++) {
195 for (idx = 0; idx < RADIX_TREE_TAG_LONGS; idx++) {
196 if (root->rnode->tags[tag][idx]) {
204 if (!(node = radix_tree_node_alloc(root)))
207 /* Increase the height. */
208 node->slots[0] = root->rnode;
210 /* Propagate the aggregated tag info into the new root */
211 for (tag = 0; tag < RADIX_TREE_TAGS; tag++) {
213 tag_set(node, tag, 0);
219 } while (height > root->height);
225 * radix_tree_insert - insert into a radix tree
226 * @root: radix tree root
228 * @item: item to insert
230 * Insert an item into the radix tree at position @index.
232 int radix_tree_insert(struct radix_tree_root *root,
233 unsigned long index, void *item)
235 struct radix_tree_node *node = NULL, *tmp, **slot;
236 unsigned int height, shift;
240 /* Make sure the tree is high enough. */
241 if ((!index && !root->rnode) ||
242 index > radix_tree_maxindex(root->height)) {
243 error = radix_tree_extend(root, index);
249 height = root->height;
250 shift = (height-1) * RADIX_TREE_MAP_SHIFT;
252 offset = 0; /* uninitialised var warning */
255 /* Have to add a child node. */
256 if (!(tmp = radix_tree_node_alloc(root)))
263 /* Go a level down */
264 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
266 slot = (struct radix_tree_node **)(node->slots + offset);
267 shift -= RADIX_TREE_MAP_SHIFT;
275 BUG_ON(tag_get(node, 0, offset));
276 BUG_ON(tag_get(node, 1, offset));
282 EXPORT_SYMBOL(radix_tree_insert);
285 * radix_tree_lookup - perform lookup operation on a radix tree
286 * @root: radix tree root
289 * Lookup the item at the position @index in the radix tree @root.
291 void *radix_tree_lookup(struct radix_tree_root *root, unsigned long index)
293 unsigned int height, shift;
294 struct radix_tree_node **slot;
296 height = root->height;
297 if (index > radix_tree_maxindex(height))
300 shift = (height-1) * RADIX_TREE_MAP_SHIFT;
307 slot = (struct radix_tree_node **)
309 ((index >> shift) & RADIX_TREE_MAP_MASK));
310 shift -= RADIX_TREE_MAP_SHIFT;
316 EXPORT_SYMBOL(radix_tree_lookup);
319 * radix_tree_tag_set - set a tag on a radix tree node
320 * @root: radix tree root
324 * Set the search tag corresponging to @index in the radix tree. From
325 * the root all the way down to the leaf node.
327 * Returns the address of the tagged item. Setting a tag on a not-present
330 void *radix_tree_tag_set(struct radix_tree_root *root,
331 unsigned long index, int tag)
333 unsigned int height, shift;
334 struct radix_tree_node **slot;
336 height = root->height;
337 if (index > radix_tree_maxindex(height))
340 shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
346 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
347 tag_set(*slot, tag, offset);
348 slot = (struct radix_tree_node **)((*slot)->slots + offset);
349 BUG_ON(*slot == NULL);
350 shift -= RADIX_TREE_MAP_SHIFT;
356 EXPORT_SYMBOL(radix_tree_tag_set);
359 * radix_tree_tag_clear - clear a tag on a radix tree node
360 * @root: radix tree root
364 * Clear the search tag corresponging to @index in the radix tree. If
365 * this causes the leaf node to have no tags set then clear the tag in the
366 * next-to-leaf node, etc.
368 * Returns the address of the tagged item on success, else NULL. ie:
369 * has the same return value and semantics as radix_tree_lookup().
371 void *radix_tree_tag_clear(struct radix_tree_root *root,
372 unsigned long index, int tag)
374 struct radix_tree_path path[RADIX_TREE_MAX_PATH], *pathp = path;
375 unsigned int height, shift;
378 height = root->height;
379 if (index > radix_tree_maxindex(height))
382 shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
384 pathp->slot = &root->rnode;
389 if (*pathp->slot == NULL)
392 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
393 pathp[1].offset = offset;
394 pathp[1].node = *pathp[0].slot;
395 pathp[1].slot = (struct radix_tree_node **)
396 (pathp[1].node->slots + offset);
398 shift -= RADIX_TREE_MAP_SHIFT;
402 ret = *pathp[0].slot;
409 tag_clear(pathp[0].node, tag, pathp[0].offset);
410 for (idx = 0; idx < RADIX_TREE_TAG_LONGS; idx++) {
411 if (pathp[0].node->tags[tag][idx])
415 } while (pathp[0].node);
419 EXPORT_SYMBOL(radix_tree_tag_clear);
421 #ifndef __KERNEL__ /* Only the test harness uses this at present */
423 * radix_tree_tag_get - get a tag on a radix tree node
424 * @root: radix tree root
428 * Return the search tag corresponging to @index in the radix tree.
430 * Returns zero if the tag is unset, or if there is no corresponding item
433 int radix_tree_tag_get(struct radix_tree_root *root,
434 unsigned long index, int tag)
436 unsigned int height, shift;
437 struct radix_tree_node **slot;
438 int saw_unset_tag = 0;
440 height = root->height;
441 if (index > radix_tree_maxindex(height))
444 shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
453 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
456 * This is just a debug check. Later, we can bale as soon as
457 * we see an unset tag.
459 if (!tag_get(*slot, tag, offset))
462 int ret = tag_get(*slot, tag, offset);
464 BUG_ON(ret && saw_unset_tag);
467 slot = (struct radix_tree_node **)((*slot)->slots + offset);
468 shift -= RADIX_TREE_MAP_SHIFT;
472 EXPORT_SYMBOL(radix_tree_tag_get);
476 __lookup(struct radix_tree_root *root, void **results, unsigned long index,
477 unsigned int max_items, unsigned long *next_index)
479 unsigned int nr_found = 0;
481 unsigned int height = root->height;
482 struct radix_tree_node *slot;
484 shift = (height-1) * RADIX_TREE_MAP_SHIFT;
488 unsigned long i = (index >> shift) & RADIX_TREE_MAP_MASK;
490 for ( ; i < RADIX_TREE_MAP_SIZE; i++) {
491 if (slot->slots[i] != NULL)
493 index &= ~((1 << shift) - 1);
496 goto out; /* 32-bit wraparound */
498 if (i == RADIX_TREE_MAP_SIZE)
501 if (height == 0) { /* Bottom level: grab some items */
502 unsigned long j = index & RADIX_TREE_MAP_MASK;
504 for ( ; j < RADIX_TREE_MAP_SIZE; j++) {
506 if (slot->slots[j]) {
507 results[nr_found++] = slot->slots[j];
508 if (nr_found == max_items)
513 shift -= RADIX_TREE_MAP_SHIFT;
514 slot = slot->slots[i];
522 * radix_tree_gang_lookup - perform multiple lookup on a radix tree
523 * @root: radix tree root
524 * @results: where the results of the lookup are placed
525 * @first_index: start the lookup from this key
526 * @max_items: place up to this many items at *results
528 * Performs an index-ascending scan of the tree for present items. Places
529 * them at *@results and returns the number of items which were placed at
532 * The implementation is naive.
535 radix_tree_gang_lookup(struct radix_tree_root *root, void **results,
536 unsigned long first_index, unsigned int max_items)
538 const unsigned long max_index = radix_tree_maxindex(root->height);
539 unsigned long cur_index = first_index;
540 unsigned int ret = 0;
542 while (ret < max_items) {
543 unsigned int nr_found;
544 unsigned long next_index; /* Index of next search */
546 if (cur_index > max_index)
548 nr_found = __lookup(root, results + ret, cur_index,
549 max_items - ret, &next_index);
553 cur_index = next_index;
557 EXPORT_SYMBOL(radix_tree_gang_lookup);
560 * FIXME: the two tag_get()s here should use find_next_bit() instead of
561 * open-coding the search.
564 __lookup_tag(struct radix_tree_root *root, void **results, unsigned long index,
565 unsigned int max_items, unsigned long *next_index, int tag)
567 unsigned int nr_found = 0;
569 unsigned int height = root->height;
570 struct radix_tree_node *slot;
572 shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
576 unsigned long i = (index >> shift) & RADIX_TREE_MAP_MASK;
578 for ( ; i < RADIX_TREE_MAP_SIZE; i++) {
579 if (tag_get(slot, tag, i)) {
580 BUG_ON(slot->slots[i] == NULL);
583 index &= ~((1 << shift) - 1);
586 goto out; /* 32-bit wraparound */
588 if (i == RADIX_TREE_MAP_SIZE)
591 if (height == 0) { /* Bottom level: grab some items */
592 unsigned long j = index & RADIX_TREE_MAP_MASK;
594 for ( ; j < RADIX_TREE_MAP_SIZE; j++) {
596 if (tag_get(slot, tag, j)) {
597 BUG_ON(slot->slots[j] == NULL);
598 results[nr_found++] = slot->slots[j];
599 if (nr_found == max_items)
604 shift -= RADIX_TREE_MAP_SHIFT;
605 slot = slot->slots[i];
613 * radix_tree_gang_lookup_tag - perform multiple lookup on a radix tree
615 * @root: radix tree root
616 * @results: where the results of the lookup are placed
617 * @first_index: start the lookup from this key
618 * @max_items: place up to this many items at *results
619 * @tag: the tag index
621 * Performs an index-ascending scan of the tree for present items which
622 * have the tag indexed by @tag set. Places the items at *@results and
623 * returns the number of items which were placed at *@results.
626 radix_tree_gang_lookup_tag(struct radix_tree_root *root, void **results,
627 unsigned long first_index, unsigned int max_items, int tag)
629 const unsigned long max_index = radix_tree_maxindex(root->height);
630 unsigned long cur_index = first_index;
631 unsigned int ret = 0;
633 while (ret < max_items) {
634 unsigned int nr_found;
635 unsigned long next_index; /* Index of next search */
637 if (cur_index > max_index)
639 nr_found = __lookup_tag(root, results + ret, cur_index,
640 max_items - ret, &next_index, tag);
644 cur_index = next_index;
648 EXPORT_SYMBOL(radix_tree_gang_lookup_tag);
651 * radix_tree_delete - delete an item from a radix tree
652 * @root: radix tree root
655 * Remove the item at @index from the radix tree rooted at @root.
657 * Returns the address of the deleted item, or NULL if it was not present.
659 void *radix_tree_delete(struct radix_tree_root *root, unsigned long index)
661 struct radix_tree_path path[RADIX_TREE_MAX_PATH], *pathp = path;
662 struct radix_tree_path *orig_pathp;
663 unsigned int height, shift;
665 char tags[RADIX_TREE_TAGS];
668 height = root->height;
669 if (index > radix_tree_maxindex(height))
672 shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
674 pathp->slot = &root->rnode;
679 if (*pathp->slot == NULL)
682 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
683 pathp[1].offset = offset;
684 pathp[1].node = *pathp[0].slot;
685 pathp[1].slot = (struct radix_tree_node **)
686 (pathp[1].node->slots + offset);
688 shift -= RADIX_TREE_MAP_SHIFT;
692 ret = *pathp[0].slot;
699 * Clear all tags associated with the just-deleted item
701 memset(tags, 0, sizeof(tags));
705 nr_cleared_tags = RADIX_TREE_TAGS;
706 for (tag = 0; tag < RADIX_TREE_TAGS; tag++) {
710 tag_clear(pathp[0].node, tag, pathp[0].offset);
712 for (idx = 0; idx < RADIX_TREE_TAG_LONGS; idx++) {
713 if (pathp[0].node->tags[tag][idx]) {
721 } while (pathp[0].node && nr_cleared_tags);
724 *pathp[0].slot = NULL;
725 while (pathp[0].node && --pathp[0].node->count == 0) {
727 BUG_ON(*pathp[0].slot == NULL);
728 *pathp[0].slot = NULL;
729 radix_tree_node_free(pathp[1].node);
731 if (root->rnode == NULL)
736 EXPORT_SYMBOL(radix_tree_delete);
739 * radix_tree_tagged - test whether any items in the tree are tagged
740 * @root: radix tree root
743 int radix_tree_tagged(struct radix_tree_root *root, int tag)
749 for (idx = 0; idx < RADIX_TREE_TAG_LONGS; idx++) {
750 if (root->rnode->tags[tag][idx])
755 EXPORT_SYMBOL(radix_tree_tagged);
758 radix_tree_node_ctor(void *node, kmem_cache_t *cachep, unsigned long flags)
760 memset(node, 0, sizeof(struct radix_tree_node));
763 static __init unsigned long __maxindex(unsigned int height)
765 unsigned int tmp = height * RADIX_TREE_MAP_SHIFT;
766 unsigned long index = (~0UL >> (RADIX_TREE_INDEX_BITS - tmp - 1)) >> 1;
768 if (tmp >= RADIX_TREE_INDEX_BITS)
773 static __init void radix_tree_init_maxindex(void)
777 for (i = 0; i < ARRAY_SIZE(height_to_maxindex); i++)
778 height_to_maxindex[i] = __maxindex(i);
781 #ifdef CONFIG_HOTPLUG_CPU
782 static int radix_tree_callback(struct notifier_block *nfb,
783 unsigned long action,
786 int cpu = (long)hcpu;
787 struct radix_tree_preload *rtp;
789 /* Free per-cpu pool of perloaded nodes */
790 if (action == CPU_DEAD) {
791 rtp = &per_cpu(radix_tree_preloads, cpu);
793 kmem_cache_free(radix_tree_node_cachep,
794 rtp->nodes[rtp->nr-1]);
795 rtp->nodes[rtp->nr-1] = NULL;
801 #endif /* CONFIG_HOTPLUG_CPU */
803 void __init radix_tree_init(void)
805 radix_tree_node_cachep = kmem_cache_create("radix_tree_node",
806 sizeof(struct radix_tree_node), 0,
807 0, radix_tree_node_ctor, NULL);
808 if (!radix_tree_node_cachep)
809 panic ("Failed to create radix_tree_node cache\n");
810 radix_tree_init_maxindex();
811 hotcpu_notifier(radix_tree_callback, 0);