2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
20 * Copyright (C) 2001 Momchil Velikov
21 * Portions Copyright (C) 2001 Christoph Hellwig
22 * Copyright (C) 2005 SGI, Christoph Lameter <clameter@sgi.com>
24 * This program is free software; you can redistribute it and/or
25 * modify it under the terms of the GNU General Public License as
26 * published by the Free Software Foundation; either version 2, or (at
27 * your option) any later version.
29 * This program is distributed in the hope that it will be useful, but
30 * WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
32 * General Public License for more details.
34 * You should have received a copy of the GNU General Public License
35 * along with this program; if not, write to the Free Software
36 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
39 #include "kerncompat.h"
40 #include "radix-tree.h"
42 #define RADIX_TREE_MAP_SHIFT (CONFIG_BASE_SMALL ? 4 : 6)
44 #define RADIX_TREE_MAP_SHIFT 3 /* For more stressful testing */
47 #define RADIX_TREE_MAP_SIZE (1UL << RADIX_TREE_MAP_SHIFT)
48 #define RADIX_TREE_MAP_MASK (RADIX_TREE_MAP_SIZE-1)
50 #define RADIX_TREE_TAG_LONGS \
51 ((RADIX_TREE_MAP_SIZE + BITS_PER_LONG - 1) / BITS_PER_LONG)
53 struct radix_tree_node {
55 void *slots[RADIX_TREE_MAP_SIZE];
56 unsigned long tags[RADIX_TREE_MAX_TAGS][RADIX_TREE_TAG_LONGS];
59 struct radix_tree_path {
60 struct radix_tree_node *node;
64 #define RADIX_TREE_INDEX_BITS (8 /* CHAR_BIT */ * sizeof(unsigned long))
65 #define RADIX_TREE_MAX_PATH (RADIX_TREE_INDEX_BITS/RADIX_TREE_MAP_SHIFT + 2)
67 static unsigned long height_to_maxindex[RADIX_TREE_MAX_PATH] __read_mostly;
70 * Per-cpu pool of preloaded nodes
72 struct radix_tree_preload {
74 struct radix_tree_node *nodes[RADIX_TREE_MAX_PATH];
76 static struct radix_tree_preload radix_tree_preloads = { 0, };
78 static int internal_nodes = 0;
80 * This assumes that the caller has performed appropriate preallocation, and
81 * that the caller has pinned this thread of control to the current CPU.
83 static struct radix_tree_node *
84 radix_tree_node_alloc(struct radix_tree_root *root)
86 struct radix_tree_node *ret;
87 ret = malloc(sizeof(struct radix_tree_node));
89 memset(ret, 0, sizeof(struct radix_tree_node));
96 radix_tree_node_free(struct radix_tree_node *node)
103 * Load up this CPU's radix_tree_node buffer with sufficient objects to
104 * ensure that the addition of a single element in the tree cannot fail. On
105 * success, return zero, with preemption disabled. On error, return -ENOMEM
106 * with preemption not disabled.
108 int radix_tree_preload(gfp_t gfp_mask)
110 struct radix_tree_preload *rtp;
111 struct radix_tree_node *node;
115 rtp = &__get_cpu_var(radix_tree_preloads);
116 while (rtp->nr < ARRAY_SIZE(rtp->nodes)) {
118 node = radix_tree_node_alloc(NULL);
122 rtp = &__get_cpu_var(radix_tree_preloads);
123 if (rtp->nr < ARRAY_SIZE(rtp->nodes))
124 rtp->nodes[rtp->nr++] = node;
126 radix_tree_node_free(node);
133 static inline void tag_set(struct radix_tree_node *node, unsigned int tag,
136 __set_bit(offset, node->tags[tag]);
139 static inline void tag_clear(struct radix_tree_node *node, unsigned int tag,
142 __clear_bit(offset, node->tags[tag]);
145 static inline int tag_get(struct radix_tree_node *node, unsigned int tag,
148 return test_bit(offset, node->tags[tag]);
151 static inline void root_tag_set(struct radix_tree_root *root, unsigned int tag)
153 root->gfp_mask |= (__force gfp_t)(1 << (tag + __GFP_BITS_SHIFT));
157 static inline void root_tag_clear(struct radix_tree_root *root, unsigned int tag)
159 root->gfp_mask &= (__force gfp_t)~(1 << (tag + __GFP_BITS_SHIFT));
162 static inline void root_tag_clear_all(struct radix_tree_root *root)
164 root->gfp_mask &= __GFP_BITS_MASK;
167 static inline int root_tag_get(struct radix_tree_root *root, unsigned int tag)
169 return (__force unsigned)root->gfp_mask & (1 << (tag + __GFP_BITS_SHIFT));
173 * Returns 1 if any slot in the node has this tag set.
174 * Otherwise returns 0.
176 static inline int any_tag_set(struct radix_tree_node *node, unsigned int tag)
179 for (idx = 0; idx < RADIX_TREE_TAG_LONGS; idx++) {
180 if (node->tags[tag][idx])
187 * Return the maximum key which can be store into a
188 * radix tree with height HEIGHT.
190 static inline unsigned long radix_tree_maxindex(unsigned int height)
192 return height_to_maxindex[height];
196 * Extend a radix tree so it can store key @index.
198 static int radix_tree_extend(struct radix_tree_root *root, unsigned long index)
200 struct radix_tree_node *node;
204 /* Figure out what the height should be. */
205 height = root->height + 1;
206 while (index > radix_tree_maxindex(height))
209 if (root->rnode == NULL) {
210 root->height = height;
215 if (!(node = radix_tree_node_alloc(root)))
218 /* Increase the height. */
219 node->slots[0] = root->rnode;
221 /* Propagate the aggregated tag info into the new root */
222 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) {
223 if (root_tag_get(root, tag))
224 tag_set(node, tag, 0);
230 } while (height > root->height);
236 * radix_tree_insert - insert into a radix tree
237 * @root: radix tree root
239 * @item: item to insert
241 * Insert an item into the radix tree at position @index.
243 int radix_tree_insert(struct radix_tree_root *root,
244 unsigned long index, void *item)
246 struct radix_tree_node *node = NULL, *slot;
247 unsigned int height, shift;
251 /* Make sure the tree is high enough. */
252 if (index > radix_tree_maxindex(root->height)) {
253 error = radix_tree_extend(root, index);
259 height = root->height;
260 shift = (height-1) * RADIX_TREE_MAP_SHIFT;
262 offset = 0; /* uninitialised var warning */
265 /* Have to add a child node. */
266 if (!(slot = radix_tree_node_alloc(root)))
269 node->slots[offset] = slot;
275 /* Go a level down */
276 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
278 slot = node->slots[offset];
279 shift -= RADIX_TREE_MAP_SHIFT;
288 node->slots[offset] = item;
289 BUG_ON(tag_get(node, 0, offset));
290 BUG_ON(tag_get(node, 1, offset));
293 BUG_ON(root_tag_get(root, 0));
294 BUG_ON(root_tag_get(root, 1));
300 static inline void **__lookup_slot(struct radix_tree_root *root,
303 unsigned int height, shift;
304 struct radix_tree_node **slot;
306 height = root->height;
308 if (index > radix_tree_maxindex(height))
311 if (height == 0 && root->rnode)
312 return (void *)&root->rnode;
314 shift = (height-1) * RADIX_TREE_MAP_SHIFT;
321 slot = (struct radix_tree_node **)
323 ((index >> shift) & RADIX_TREE_MAP_MASK));
324 shift -= RADIX_TREE_MAP_SHIFT;
328 return (void **)slot;
332 * radix_tree_lookup_slot - lookup a slot in a radix tree
333 * @root: radix tree root
336 * Lookup the slot corresponding to the position @index in the radix tree
337 * @root. This is useful for update-if-exists operations.
339 void **radix_tree_lookup_slot(struct radix_tree_root *root, unsigned long index)
341 return __lookup_slot(root, index);
345 * radix_tree_lookup - perform lookup operation on a radix tree
346 * @root: radix tree root
349 * Lookup the item at the position @index in the radix tree @root.
351 void *radix_tree_lookup(struct radix_tree_root *root, unsigned long index)
355 slot = __lookup_slot(root, index);
356 return slot != NULL ? *slot : NULL;
360 * radix_tree_tag_set - set a tag on a radix tree node
361 * @root: radix tree root
365 * Set the search tag (which must be < RADIX_TREE_MAX_TAGS)
366 * corresponding to @index in the radix tree. From
367 * the root all the way down to the leaf node.
369 * Returns the address of the tagged item. Setting a tag on a not-present
372 void *radix_tree_tag_set(struct radix_tree_root *root,
373 unsigned long index, unsigned int tag)
375 unsigned int height, shift;
376 struct radix_tree_node *slot;
378 height = root->height;
379 BUG_ON(index > radix_tree_maxindex(height));
382 shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
387 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
388 if (!tag_get(slot, tag, offset))
389 tag_set(slot, tag, offset);
390 slot = slot->slots[offset];
391 BUG_ON(slot == NULL);
392 shift -= RADIX_TREE_MAP_SHIFT;
396 /* set the root's tag bit */
397 if (slot && !root_tag_get(root, tag))
398 root_tag_set(root, tag);
404 * radix_tree_tag_clear - clear a tag on a radix tree node
405 * @root: radix tree root
409 * Clear the search tag (which must be < RADIX_TREE_MAX_TAGS)
410 * corresponding to @index in the radix tree. If
411 * this causes the leaf node to have no tags set then clear the tag in the
412 * next-to-leaf node, etc.
414 * Returns the address of the tagged item on success, else NULL. ie:
415 * has the same return value and semantics as radix_tree_lookup().
417 void *radix_tree_tag_clear(struct radix_tree_root *root,
418 unsigned long index, unsigned int tag)
420 struct radix_tree_path path[RADIX_TREE_MAX_PATH], *pathp = path;
421 struct radix_tree_node *slot = NULL;
422 unsigned int height, shift;
424 height = root->height;
425 if (index > radix_tree_maxindex(height))
428 shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
438 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
439 pathp[1].offset = offset;
440 pathp[1].node = slot;
441 slot = slot->slots[offset];
443 shift -= RADIX_TREE_MAP_SHIFT;
450 while (pathp->node) {
451 if (!tag_get(pathp->node, tag, pathp->offset))
453 tag_clear(pathp->node, tag, pathp->offset);
454 if (any_tag_set(pathp->node, tag))
459 /* clear the root's tag bit */
460 if (root_tag_get(root, tag))
461 root_tag_clear(root, tag);
467 #ifndef __KERNEL__ /* Only the test harness uses this at present */
469 * radix_tree_tag_get - get a tag on a radix tree node
470 * @root: radix tree root
472 * @tag: tag index (< RADIX_TREE_MAX_TAGS)
476 * 0: tag not present or not set
479 int radix_tree_tag_get(struct radix_tree_root *root,
480 unsigned long index, unsigned int tag)
482 unsigned int height, shift;
483 struct radix_tree_node *slot;
484 int saw_unset_tag = 0;
486 height = root->height;
487 if (index > radix_tree_maxindex(height))
490 /* check the root's tag bit */
491 if (!root_tag_get(root, tag))
497 shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
506 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
509 * This is just a debug check. Later, we can bale as soon as
510 * we see an unset tag.
512 if (!tag_get(slot, tag, offset))
515 int ret = tag_get(slot, tag, offset);
517 BUG_ON(ret && saw_unset_tag);
520 slot = slot->slots[offset];
521 shift -= RADIX_TREE_MAP_SHIFT;
528 __lookup(struct radix_tree_root *root, void **results, unsigned long index,
529 unsigned int max_items, unsigned long *next_index)
531 unsigned int nr_found = 0;
532 unsigned int shift, height;
533 struct radix_tree_node *slot;
536 height = root->height;
538 if (root->rnode && index == 0)
539 results[nr_found++] = root->rnode;
543 shift = (height-1) * RADIX_TREE_MAP_SHIFT;
546 for ( ; height > 1; height--) {
548 for (i = (index >> shift) & RADIX_TREE_MAP_MASK ;
549 i < RADIX_TREE_MAP_SIZE; i++) {
550 if (slot->slots[i] != NULL)
552 index &= ~((1UL << shift) - 1);
553 index += 1UL << shift;
555 goto out; /* 32-bit wraparound */
557 if (i == RADIX_TREE_MAP_SIZE)
560 shift -= RADIX_TREE_MAP_SHIFT;
561 slot = slot->slots[i];
564 /* Bottom level: grab some items */
565 for (i = index & RADIX_TREE_MAP_MASK; i < RADIX_TREE_MAP_SIZE; i++) {
567 if (slot->slots[i]) {
568 results[nr_found++] = slot->slots[i];
569 if (nr_found == max_items)
579 * radix_tree_gang_lookup - perform multiple lookup on a radix tree
580 * @root: radix tree root
581 * @results: where the results of the lookup are placed
582 * @first_index: start the lookup from this key
583 * @max_items: place up to this many items at *results
585 * Performs an index-ascending scan of the tree for present items. Places
586 * them at *@results and returns the number of items which were placed at
589 * The implementation is naive.
592 radix_tree_gang_lookup(struct radix_tree_root *root, void **results,
593 unsigned long first_index, unsigned int max_items)
595 const unsigned long max_index = radix_tree_maxindex(root->height);
596 unsigned long cur_index = first_index;
597 unsigned int ret = 0;
599 while (ret < max_items) {
600 unsigned int nr_found;
601 unsigned long next_index; /* Index of next search */
603 if (cur_index > max_index)
605 nr_found = __lookup(root, results + ret, cur_index,
606 max_items - ret, &next_index);
610 cur_index = next_index;
616 * FIXME: the two tag_get()s here should use find_next_bit() instead of
617 * open-coding the search.
620 __lookup_tag(struct radix_tree_root *root, void **results, unsigned long index,
621 unsigned int max_items, unsigned long *next_index, unsigned int tag)
623 unsigned int nr_found = 0;
625 unsigned int height = root->height;
626 struct radix_tree_node *slot;
629 if (root->rnode && index == 0)
630 results[nr_found++] = root->rnode;
634 shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
638 unsigned long i = (index >> shift) & RADIX_TREE_MAP_MASK;
640 for ( ; i < RADIX_TREE_MAP_SIZE; i++) {
641 if (tag_get(slot, tag, i)) {
642 BUG_ON(slot->slots[i] == NULL);
645 index &= ~((1UL << shift) - 1);
646 index += 1UL << shift;
648 goto out; /* 32-bit wraparound */
650 if (i == RADIX_TREE_MAP_SIZE)
653 if (height == 0) { /* Bottom level: grab some items */
654 unsigned long j = index & RADIX_TREE_MAP_MASK;
656 for ( ; j < RADIX_TREE_MAP_SIZE; j++) {
658 if (tag_get(slot, tag, j)) {
659 BUG_ON(slot->slots[j] == NULL);
660 results[nr_found++] = slot->slots[j];
661 if (nr_found == max_items)
666 shift -= RADIX_TREE_MAP_SHIFT;
667 slot = slot->slots[i];
668 } while (height > 0);
675 * radix_tree_gang_lookup_tag - perform multiple lookup on a radix tree
677 * @root: radix tree root
678 * @results: where the results of the lookup are placed
679 * @first_index: start the lookup from this key
680 * @max_items: place up to this many items at *results
681 * @tag: the tag index (< RADIX_TREE_MAX_TAGS)
683 * Performs an index-ascending scan of the tree for present items which
684 * have the tag indexed by @tag set. Places the items at *@results and
685 * returns the number of items which were placed at *@results.
688 radix_tree_gang_lookup_tag(struct radix_tree_root *root, void **results,
689 unsigned long first_index, unsigned int max_items,
692 const unsigned long max_index = radix_tree_maxindex(root->height);
693 unsigned long cur_index = first_index;
694 unsigned int ret = 0;
696 /* check the root's tag bit */
697 if (!root_tag_get(root, tag))
700 while (ret < max_items) {
701 unsigned int nr_found;
702 unsigned long next_index; /* Index of next search */
704 if (cur_index > max_index)
706 nr_found = __lookup_tag(root, results + ret, cur_index,
707 max_items - ret, &next_index, tag);
711 cur_index = next_index;
717 * radix_tree_shrink - shrink height of a radix tree to minimal
718 * @root radix tree root
720 static inline void radix_tree_shrink(struct radix_tree_root *root)
722 /* try to shrink tree height */
723 while (root->height > 0 &&
724 root->rnode->count == 1 &&
725 root->rnode->slots[0]) {
726 struct radix_tree_node *to_free = root->rnode;
728 root->rnode = to_free->slots[0];
730 /* must only free zeroed nodes into the slab */
731 tag_clear(to_free, 0, 0);
732 tag_clear(to_free, 1, 0);
733 to_free->slots[0] = NULL;
735 radix_tree_node_free(to_free);
740 * radix_tree_delete - delete an item from a radix tree
741 * @root: radix tree root
744 * Remove the item at @index from the radix tree rooted at @root.
746 * Returns the address of the deleted item, or NULL if it was not present.
748 void *radix_tree_delete(struct radix_tree_root *root, unsigned long index)
750 struct radix_tree_path path[RADIX_TREE_MAX_PATH], *pathp = path;
751 struct radix_tree_node *slot = NULL;
752 unsigned int height, shift;
756 height = root->height;
757 if (index > radix_tree_maxindex(height))
761 if (height == 0 && root->rnode) {
762 root_tag_clear_all(root);
767 shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
775 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
776 pathp->offset = offset;
778 slot = slot->slots[offset];
779 shift -= RADIX_TREE_MAP_SHIFT;
781 } while (height > 0);
787 * Clear all tags associated with the just-deleted item
789 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) {
790 if (tag_get(pathp->node, tag, pathp->offset))
791 radix_tree_tag_clear(root, index, tag);
794 /* Now free the nodes we do not need anymore */
795 while (pathp->node) {
796 pathp->node->slots[pathp->offset] = NULL;
797 pathp->node->count--;
799 if (pathp->node->count) {
800 if (pathp->node == root->rnode)
801 radix_tree_shrink(root);
805 /* Node with zero slots in use so free it */
806 radix_tree_node_free(pathp->node);
810 root_tag_clear_all(root);
819 * radix_tree_tagged - test whether any items in the tree are tagged
820 * @root: radix tree root
823 int radix_tree_tagged(struct radix_tree_root *root, unsigned int tag)
825 return root_tag_get(root, tag);
828 static unsigned long __maxindex(unsigned int height)
830 unsigned int tmp = height * RADIX_TREE_MAP_SHIFT;
831 unsigned long index = ~0UL;
833 if (tmp < RADIX_TREE_INDEX_BITS)
834 index = (index >> (RADIX_TREE_INDEX_BITS - tmp - 1)) >> 1;
838 static void radix_tree_init_maxindex(void)
842 for (i = 0; i < ARRAY_SIZE(height_to_maxindex); i++)
843 height_to_maxindex[i] = __maxindex(i);
846 void radix_tree_init(void)
848 radix_tree_init_maxindex();