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 */
40 #define RADIX_TREE_TAGS 2
42 #define RADIX_TREE_MAP_SIZE (1UL << RADIX_TREE_MAP_SHIFT)
43 #define RADIX_TREE_MAP_MASK (RADIX_TREE_MAP_SIZE-1)
45 #define RADIX_TREE_TAG_LONGS \
46 ((RADIX_TREE_MAP_SIZE + BITS_PER_LONG - 1) / BITS_PER_LONG)
48 struct radix_tree_node {
50 void *slots[RADIX_TREE_MAP_SIZE];
51 unsigned long tags[RADIX_TREE_TAGS][RADIX_TREE_TAG_LONGS];
54 struct radix_tree_path {
55 struct radix_tree_node *node;
59 #define RADIX_TREE_INDEX_BITS (8 /* CHAR_BIT */ * sizeof(unsigned long))
60 #define RADIX_TREE_MAX_PATH (RADIX_TREE_INDEX_BITS/RADIX_TREE_MAP_SHIFT + 2)
62 static unsigned long height_to_maxindex[RADIX_TREE_MAX_PATH] __read_mostly;
65 * Radix tree node cache.
67 static kmem_cache_t *radix_tree_node_cachep;
70 * Per-cpu pool of preloaded nodes
72 struct radix_tree_preload {
74 struct radix_tree_node *nodes[RADIX_TREE_MAX_PATH];
76 DEFINE_PER_CPU(struct radix_tree_preload, radix_tree_preloads) = { 0, };
79 * This assumes that the caller has performed appropriate preallocation, and
80 * that the caller has pinned this thread of control to the current CPU.
82 static struct radix_tree_node *
83 radix_tree_node_alloc(struct radix_tree_root *root)
85 struct radix_tree_node *ret;
87 ret = kmem_cache_alloc(radix_tree_node_cachep, root->gfp_mask);
88 if (ret == NULL && !(root->gfp_mask & __GFP_WAIT)) {
89 struct radix_tree_preload *rtp;
91 rtp = &__get_cpu_var(radix_tree_preloads);
93 ret = rtp->nodes[rtp->nr - 1];
94 rtp->nodes[rtp->nr - 1] = NULL;
102 radix_tree_node_free(struct radix_tree_node *node)
104 kmem_cache_free(radix_tree_node_cachep, node);
108 * Load up this CPU's radix_tree_node buffer with sufficient objects to
109 * ensure that the addition of a single element in the tree cannot fail. On
110 * success, return zero, with preemption disabled. On error, return -ENOMEM
111 * with preemption not disabled.
113 int radix_tree_preload(gfp_t gfp_mask)
115 struct radix_tree_preload *rtp;
116 struct radix_tree_node *node;
120 rtp = &__get_cpu_var(radix_tree_preloads);
121 while (rtp->nr < ARRAY_SIZE(rtp->nodes)) {
123 node = kmem_cache_alloc(radix_tree_node_cachep, gfp_mask);
127 rtp = &__get_cpu_var(radix_tree_preloads);
128 if (rtp->nr < ARRAY_SIZE(rtp->nodes))
129 rtp->nodes[rtp->nr++] = node;
131 kmem_cache_free(radix_tree_node_cachep, node);
138 static inline void tag_set(struct radix_tree_node *node, int tag, int offset)
140 __set_bit(offset, node->tags[tag]);
143 static inline void tag_clear(struct radix_tree_node *node, int tag, int offset)
145 __clear_bit(offset, node->tags[tag]);
148 static inline int tag_get(struct radix_tree_node *node, int tag, int offset)
150 return test_bit(offset, node->tags[tag]);
154 * Returns 1 if any slot in the node has this tag set.
155 * Otherwise returns 0.
157 static inline int any_tag_set(struct radix_tree_node *node, int tag)
160 for (idx = 0; idx < RADIX_TREE_TAG_LONGS; idx++) {
161 if (node->tags[tag][idx])
168 * Return the maximum key which can be store into a
169 * radix tree with height HEIGHT.
171 static inline unsigned long radix_tree_maxindex(unsigned int height)
173 return height_to_maxindex[height];
177 * Extend a radix tree so it can store key @index.
179 static int radix_tree_extend(struct radix_tree_root *root, unsigned long index)
181 struct radix_tree_node *node;
183 char tags[RADIX_TREE_TAGS];
186 /* Figure out what the height should be. */
187 height = root->height + 1;
188 while (index > radix_tree_maxindex(height))
191 if (root->rnode == NULL) {
192 root->height = height;
197 * Prepare the tag status of the top-level node for propagation
198 * into the newly-pushed top-level node(s)
200 for (tag = 0; tag < RADIX_TREE_TAGS; tag++) {
202 if (any_tag_set(root->rnode, tag))
207 if (!(node = radix_tree_node_alloc(root)))
210 /* Increase the height. */
211 node->slots[0] = root->rnode;
213 /* Propagate the aggregated tag info into the new root */
214 for (tag = 0; tag < RADIX_TREE_TAGS; tag++) {
216 tag_set(node, tag, 0);
222 } while (height > root->height);
228 * radix_tree_insert - insert into a radix tree
229 * @root: radix tree root
231 * @item: item to insert
233 * Insert an item into the radix tree at position @index.
235 int radix_tree_insert(struct radix_tree_root *root,
236 unsigned long index, void *item)
238 struct radix_tree_node *node = NULL, *slot;
239 unsigned int height, shift;
243 /* Make sure the tree is high enough. */
244 if ((!index && !root->rnode) ||
245 index > radix_tree_maxindex(root->height)) {
246 error = radix_tree_extend(root, index);
252 height = root->height;
253 shift = (height-1) * RADIX_TREE_MAP_SHIFT;
255 offset = 0; /* uninitialised var warning */
258 /* Have to add a child node. */
259 if (!(slot = radix_tree_node_alloc(root)))
262 node->slots[offset] = slot;
268 /* Go a level down */
269 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
271 slot = node->slots[offset];
272 shift -= RADIX_TREE_MAP_SHIFT;
281 node->slots[offset] = item;
282 BUG_ON(tag_get(node, 0, offset));
283 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 corresponging to @index in the radix tree. From
355 * the root all the way down to the leaf node.
357 * Returns the address of the tagged item. Setting a tag on a not-present
360 void *radix_tree_tag_set(struct radix_tree_root *root,
361 unsigned long index, int tag)
363 unsigned int height, shift;
364 struct radix_tree_node *slot;
366 height = root->height;
367 if (index > radix_tree_maxindex(height))
370 shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
376 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
377 if (!tag_get(slot, tag, offset))
378 tag_set(slot, tag, offset);
379 slot = slot->slots[offset];
380 BUG_ON(slot == NULL);
381 shift -= RADIX_TREE_MAP_SHIFT;
387 EXPORT_SYMBOL(radix_tree_tag_set);
390 * radix_tree_tag_clear - clear a tag on a radix tree node
391 * @root: radix tree root
395 * Clear the search tag corresponging to @index in the radix tree. If
396 * this causes the leaf node to have no tags set then clear the tag in the
397 * next-to-leaf node, etc.
399 * Returns the address of the tagged item on success, else NULL. ie:
400 * has the same return value and semantics as radix_tree_lookup().
402 void *radix_tree_tag_clear(struct radix_tree_root *root,
403 unsigned long index, int tag)
405 struct radix_tree_path path[RADIX_TREE_MAX_PATH], *pathp = path;
406 struct radix_tree_node *slot;
407 unsigned int height, shift;
410 height = root->height;
411 if (index > radix_tree_maxindex(height))
414 shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
424 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
425 pathp[1].offset = offset;
426 pathp[1].node = slot;
427 slot = slot->slots[offset];
429 shift -= RADIX_TREE_MAP_SHIFT;
438 if (!tag_get(pathp->node, tag, pathp->offset))
440 tag_clear(pathp->node, tag, pathp->offset);
441 if (any_tag_set(pathp->node, tag))
444 } while (pathp->node);
448 EXPORT_SYMBOL(radix_tree_tag_clear);
450 #ifndef __KERNEL__ /* Only the test harness uses this at present */
452 * radix_tree_tag_get - get a tag on a radix tree node
453 * @root: radix tree root
460 * 1: tag present, set
461 * -1: tag present, unset
463 int radix_tree_tag_get(struct radix_tree_root *root,
464 unsigned long index, int tag)
466 unsigned int height, shift;
467 struct radix_tree_node *slot;
468 int saw_unset_tag = 0;
470 height = root->height;
471 if (index > radix_tree_maxindex(height))
474 shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
483 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
486 * This is just a debug check. Later, we can bale as soon as
487 * we see an unset tag.
489 if (!tag_get(slot, tag, offset))
492 int ret = tag_get(slot, tag, offset);
494 BUG_ON(ret && saw_unset_tag);
497 slot = slot->slots[offset];
498 shift -= RADIX_TREE_MAP_SHIFT;
502 EXPORT_SYMBOL(radix_tree_tag_get);
506 __lookup(struct radix_tree_root *root, void **results, unsigned long index,
507 unsigned int max_items, unsigned long *next_index)
509 unsigned int nr_found = 0;
510 unsigned int shift, height;
511 struct radix_tree_node *slot;
514 height = root->height;
518 shift = (height-1) * RADIX_TREE_MAP_SHIFT;
521 for ( ; height > 1; height--) {
523 for (i = (index >> shift) & RADIX_TREE_MAP_MASK ;
524 i < RADIX_TREE_MAP_SIZE; i++) {
525 if (slot->slots[i] != NULL)
527 index &= ~((1UL << shift) - 1);
528 index += 1UL << shift;
530 goto out; /* 32-bit wraparound */
532 if (i == RADIX_TREE_MAP_SIZE)
535 shift -= RADIX_TREE_MAP_SHIFT;
536 slot = slot->slots[i];
539 /* Bottom level: grab some items */
540 for (i = index & RADIX_TREE_MAP_MASK; i < RADIX_TREE_MAP_SIZE; i++) {
542 if (slot->slots[i]) {
543 results[nr_found++] = slot->slots[i];
544 if (nr_found == max_items)
554 * radix_tree_gang_lookup - perform multiple lookup on a radix tree
555 * @root: radix tree root
556 * @results: where the results of the lookup are placed
557 * @first_index: start the lookup from this key
558 * @max_items: place up to this many items at *results
560 * Performs an index-ascending scan of the tree for present items. Places
561 * them at *@results and returns the number of items which were placed at
564 * The implementation is naive.
567 radix_tree_gang_lookup(struct radix_tree_root *root, void **results,
568 unsigned long first_index, unsigned int max_items)
570 const unsigned long max_index = radix_tree_maxindex(root->height);
571 unsigned long cur_index = first_index;
572 unsigned int ret = 0;
574 while (ret < max_items) {
575 unsigned int nr_found;
576 unsigned long next_index; /* Index of next search */
578 if (cur_index > max_index)
580 nr_found = __lookup(root, results + ret, cur_index,
581 max_items - ret, &next_index);
585 cur_index = next_index;
589 EXPORT_SYMBOL(radix_tree_gang_lookup);
592 * FIXME: the two tag_get()s here should use find_next_bit() instead of
593 * open-coding the search.
596 __lookup_tag(struct radix_tree_root *root, void **results, unsigned long index,
597 unsigned int max_items, unsigned long *next_index, int tag)
599 unsigned int nr_found = 0;
601 unsigned int height = root->height;
602 struct radix_tree_node *slot;
604 shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
608 unsigned long i = (index >> shift) & RADIX_TREE_MAP_MASK;
610 for ( ; i < RADIX_TREE_MAP_SIZE; i++) {
611 if (tag_get(slot, tag, i)) {
612 BUG_ON(slot->slots[i] == NULL);
615 index &= ~((1UL << shift) - 1);
616 index += 1UL << shift;
618 goto out; /* 32-bit wraparound */
620 if (i == RADIX_TREE_MAP_SIZE)
623 if (height == 0) { /* Bottom level: grab some items */
624 unsigned long j = index & RADIX_TREE_MAP_MASK;
626 for ( ; j < RADIX_TREE_MAP_SIZE; j++) {
628 if (tag_get(slot, tag, j)) {
629 BUG_ON(slot->slots[j] == NULL);
630 results[nr_found++] = slot->slots[j];
631 if (nr_found == max_items)
636 shift -= RADIX_TREE_MAP_SHIFT;
637 slot = slot->slots[i];
645 * radix_tree_gang_lookup_tag - perform multiple lookup on a radix tree
647 * @root: radix tree root
648 * @results: where the results of the lookup are placed
649 * @first_index: start the lookup from this key
650 * @max_items: place up to this many items at *results
651 * @tag: the tag index
653 * Performs an index-ascending scan of the tree for present items which
654 * have the tag indexed by @tag set. Places the items at *@results and
655 * returns the number of items which were placed at *@results.
658 radix_tree_gang_lookup_tag(struct radix_tree_root *root, void **results,
659 unsigned long first_index, unsigned int max_items, int tag)
661 const unsigned long max_index = radix_tree_maxindex(root->height);
662 unsigned long cur_index = first_index;
663 unsigned int ret = 0;
665 while (ret < max_items) {
666 unsigned int nr_found;
667 unsigned long next_index; /* Index of next search */
669 if (cur_index > max_index)
671 nr_found = __lookup_tag(root, results + ret, cur_index,
672 max_items - ret, &next_index, tag);
676 cur_index = next_index;
680 EXPORT_SYMBOL(radix_tree_gang_lookup_tag);
683 * radix_tree_delete - delete an item from a radix tree
684 * @root: radix tree root
687 * Remove the item at @index from the radix tree rooted at @root.
689 * Returns the address of the deleted item, or NULL if it was not present.
691 void *radix_tree_delete(struct radix_tree_root *root, unsigned long index)
693 struct radix_tree_path path[RADIX_TREE_MAX_PATH], *pathp = path;
694 struct radix_tree_path *orig_pathp;
695 struct radix_tree_node *slot;
696 unsigned int height, shift;
698 char tags[RADIX_TREE_TAGS];
703 height = root->height;
704 if (index > radix_tree_maxindex(height))
707 shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
711 for ( ; height > 0; height--) {
716 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
717 pathp->offset = offset;
719 slot = slot->slots[offset];
720 shift -= RADIX_TREE_MAP_SHIFT;
730 * Clear all tags associated with the just-deleted item
733 for (tag = 0; tag < RADIX_TREE_TAGS; tag++) {
734 if (tag_get(pathp->node, tag, pathp->offset)) {
735 tag_clear(pathp->node, tag, pathp->offset);
742 for (pathp--; nr_cleared_tags && pathp->node; pathp--) {
743 for (tag = 0; tag < RADIX_TREE_TAGS; tag++) {
747 tag_clear(pathp->node, tag, pathp->offset);
748 if (any_tag_set(pathp->node, tag)) {
755 /* Now free the nodes we do not need anymore */
756 for (pathp = orig_pathp; pathp->node; pathp--) {
757 pathp->node->slots[pathp->offset] = NULL;
758 if (--pathp->node->count)
761 /* Node with zero slots in use so free it */
762 radix_tree_node_free(pathp->node);
769 EXPORT_SYMBOL(radix_tree_delete);
772 * radix_tree_tagged - test whether any items in the tree are tagged
773 * @root: radix tree root
776 int radix_tree_tagged(struct radix_tree_root *root, int tag)
778 struct radix_tree_node *rnode;
782 return any_tag_set(rnode, tag);
784 EXPORT_SYMBOL(radix_tree_tagged);
787 radix_tree_node_ctor(void *node, kmem_cache_t *cachep, unsigned long flags)
789 memset(node, 0, sizeof(struct radix_tree_node));
792 static __init unsigned long __maxindex(unsigned int height)
794 unsigned int tmp = height * RADIX_TREE_MAP_SHIFT;
795 unsigned long index = (~0UL >> (RADIX_TREE_INDEX_BITS - tmp - 1)) >> 1;
797 if (tmp >= RADIX_TREE_INDEX_BITS)
802 static __init void radix_tree_init_maxindex(void)
806 for (i = 0; i < ARRAY_SIZE(height_to_maxindex); i++)
807 height_to_maxindex[i] = __maxindex(i);
810 #ifdef CONFIG_HOTPLUG_CPU
811 static int radix_tree_callback(struct notifier_block *nfb,
812 unsigned long action,
815 int cpu = (long)hcpu;
816 struct radix_tree_preload *rtp;
818 /* Free per-cpu pool of perloaded nodes */
819 if (action == CPU_DEAD) {
820 rtp = &per_cpu(radix_tree_preloads, cpu);
822 kmem_cache_free(radix_tree_node_cachep,
823 rtp->nodes[rtp->nr-1]);
824 rtp->nodes[rtp->nr-1] = NULL;
830 #endif /* CONFIG_HOTPLUG_CPU */
832 void __init radix_tree_init(void)
834 radix_tree_node_cachep = kmem_cache_create("radix_tree_node",
835 sizeof(struct radix_tree_node), 0,
836 SLAB_PANIC, radix_tree_node_ctor, NULL);
837 radix_tree_init_maxindex();
838 hotcpu_notifier(radix_tree_callback, 0);