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>
35 #define RADIX_TREE_MAP_SHIFT 6
37 #define RADIX_TREE_MAP_SHIFT 3 /* For more stressful testing */
39 #define RADIX_TREE_TAGS 2
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_TAGS
][RADIX_TREE_TAG_LONGS
];
53 struct radix_tree_path
{
54 struct radix_tree_node
*node
, **slot
;
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
];
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(int 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
, int tag
, int offset
)
139 if (!test_bit(offset
, &node
->tags
[tag
][0]))
140 __set_bit(offset
, &node
->tags
[tag
][0]);
143 static inline void tag_clear(struct radix_tree_node
*node
, int tag
, int offset
)
145 __clear_bit(offset
, &node
->tags
[tag
][0]);
148 static inline int tag_get(struct radix_tree_node
*node
, int tag
, int offset
)
150 return test_bit(offset
, &node
->tags
[tag
][0]);
154 * Return the maximum key which can be store into a
155 * radix tree with height HEIGHT.
157 static inline unsigned long radix_tree_maxindex(unsigned int height
)
159 return height_to_maxindex
[height
];
163 * Extend a radix tree so it can store key @index.
165 static int radix_tree_extend(struct radix_tree_root
*root
, unsigned long index
)
167 struct radix_tree_node
*node
;
169 char tags
[RADIX_TREE_TAGS
];
172 /* Figure out what the height should be. */
173 height
= root
->height
+ 1;
174 while (index
> radix_tree_maxindex(height
))
177 if (root
->rnode
== NULL
) {
178 root
->height
= height
;
183 * Prepare the tag status of the top-level node for propagation
184 * into the newly-pushed top-level node(s)
186 for (tag
= 0; tag
< RADIX_TREE_TAGS
; tag
++) {
190 for (idx
= 0; idx
< RADIX_TREE_TAG_LONGS
; idx
++) {
191 if (root
->rnode
->tags
[tag
][idx
]) {
199 if (!(node
= radix_tree_node_alloc(root
)))
202 /* Increase the height. */
203 node
->slots
[0] = root
->rnode
;
205 /* Propagate the aggregated tag info into the new root */
206 for (tag
= 0; tag
< RADIX_TREE_TAGS
; tag
++) {
208 tag_set(node
, tag
, 0);
214 } while (height
> root
->height
);
220 * radix_tree_insert - insert into a radix tree
221 * @root: radix tree root
223 * @item: item to insert
225 * Insert an item into the radix tree at position @index.
227 int radix_tree_insert(struct radix_tree_root
*root
,
228 unsigned long index
, void *item
)
230 struct radix_tree_node
*node
= NULL
, *tmp
, **slot
;
231 unsigned int height
, shift
;
235 /* Make sure the tree is high enough. */
236 if ((!index
&& !root
->rnode
) ||
237 index
> radix_tree_maxindex(root
->height
)) {
238 error
= radix_tree_extend(root
, index
);
244 height
= root
->height
;
245 shift
= (height
-1) * RADIX_TREE_MAP_SHIFT
;
247 offset
= 0; /* uninitialised var warning */
250 /* Have to add a child node. */
251 if (!(tmp
= radix_tree_node_alloc(root
)))
258 /* Go a level down */
259 offset
= (index
>> shift
) & RADIX_TREE_MAP_MASK
;
261 slot
= (struct radix_tree_node
**)(node
->slots
+ offset
);
262 shift
-= RADIX_TREE_MAP_SHIFT
;
270 BUG_ON(tag_get(node
, 0, offset
));
271 BUG_ON(tag_get(node
, 1, offset
));
277 EXPORT_SYMBOL(radix_tree_insert
);
280 * radix_tree_lookup - perform lookup operation on a radix tree
281 * @root: radix tree root
284 * Lookup the item at the position @index in the radix tree @root.
286 void *radix_tree_lookup(struct radix_tree_root
*root
, unsigned long index
)
288 unsigned int height
, shift
;
289 struct radix_tree_node
**slot
;
291 height
= root
->height
;
292 if (index
> radix_tree_maxindex(height
))
295 shift
= (height
-1) * RADIX_TREE_MAP_SHIFT
;
302 slot
= (struct radix_tree_node
**)
304 ((index
>> shift
) & RADIX_TREE_MAP_MASK
));
305 shift
-= RADIX_TREE_MAP_SHIFT
;
311 EXPORT_SYMBOL(radix_tree_lookup
);
314 * radix_tree_tag_set - set a tag on a radix tree node
315 * @root: radix tree root
319 * Set the search tag corresponging to @index in the radix tree. From
320 * the root all the way down to the leaf node.
322 * Returns the address of the tagged item. Setting a tag on a not-present
325 void *radix_tree_tag_set(struct radix_tree_root
*root
,
326 unsigned long index
, int tag
)
328 unsigned int height
, shift
;
329 struct radix_tree_node
**slot
;
331 height
= root
->height
;
332 if (index
> radix_tree_maxindex(height
))
335 shift
= (height
- 1) * RADIX_TREE_MAP_SHIFT
;
341 offset
= (index
>> shift
) & RADIX_TREE_MAP_MASK
;
342 tag_set(*slot
, tag
, offset
);
343 slot
= (struct radix_tree_node
**)((*slot
)->slots
+ offset
);
344 BUG_ON(*slot
== NULL
);
345 shift
-= RADIX_TREE_MAP_SHIFT
;
351 EXPORT_SYMBOL(radix_tree_tag_set
);
354 * radix_tree_tag_clear - clear a tag on a radix tree node
355 * @root: radix tree root
359 * Clear the search tag corresponging to @index in the radix tree. If
360 * this causes the leaf node to have no tags set then clear the tag in the
361 * next-to-leaf node, etc.
363 * Returns the address of the tagged item on success, else NULL. ie:
364 * has the same return value and semantics as radix_tree_lookup().
366 void *radix_tree_tag_clear(struct radix_tree_root
*root
,
367 unsigned long index
, int tag
)
369 struct radix_tree_path path
[RADIX_TREE_MAX_PATH
], *pathp
= path
;
370 unsigned int height
, shift
;
373 height
= root
->height
;
374 if (index
> radix_tree_maxindex(height
))
377 shift
= (height
- 1) * RADIX_TREE_MAP_SHIFT
;
379 pathp
->slot
= &root
->rnode
;
384 if (*pathp
->slot
== NULL
)
387 offset
= (index
>> shift
) & RADIX_TREE_MAP_MASK
;
388 pathp
[1].offset
= offset
;
389 pathp
[1].node
= *pathp
[0].slot
;
390 pathp
[1].slot
= (struct radix_tree_node
**)
391 (pathp
[1].node
->slots
+ offset
);
393 shift
-= RADIX_TREE_MAP_SHIFT
;
397 ret
= *pathp
[0].slot
;
404 tag_clear(pathp
[0].node
, tag
, pathp
[0].offset
);
405 for (idx
= 0; idx
< RADIX_TREE_TAG_LONGS
; idx
++) {
406 if (pathp
[0].node
->tags
[tag
][idx
])
410 } while (pathp
[0].node
);
414 EXPORT_SYMBOL(radix_tree_tag_clear
);
416 #ifndef __KERNEL__ /* Only the test harness uses this at present */
418 * radix_tree_tag_get - get a tag on a radix tree node
419 * @root: radix tree root
423 * Return the search tag corresponging to @index in the radix tree.
425 * Returns zero if the tag is unset, or if there is no corresponding item
428 int radix_tree_tag_get(struct radix_tree_root
*root
,
429 unsigned long index
, int tag
)
431 unsigned int height
, shift
;
432 struct radix_tree_node
**slot
;
433 int saw_unset_tag
= 0;
435 height
= root
->height
;
436 if (index
> radix_tree_maxindex(height
))
439 shift
= (height
- 1) * RADIX_TREE_MAP_SHIFT
;
448 offset
= (index
>> shift
) & RADIX_TREE_MAP_MASK
;
451 * This is just a debug check. Later, we can bale as soon as
452 * we see an unset tag.
454 if (!tag_get(*slot
, tag
, offset
))
457 int ret
= tag_get(*slot
, tag
, offset
);
459 BUG_ON(ret
&& saw_unset_tag
);
462 slot
= (struct radix_tree_node
**)((*slot
)->slots
+ offset
);
463 shift
-= RADIX_TREE_MAP_SHIFT
;
467 EXPORT_SYMBOL(radix_tree_tag_get
);
471 __lookup(struct radix_tree_root
*root
, void **results
, unsigned long index
,
472 unsigned int max_items
, unsigned long *next_index
)
474 unsigned int nr_found
= 0;
476 unsigned int height
= root
->height
;
477 struct radix_tree_node
*slot
;
479 shift
= (height
-1) * RADIX_TREE_MAP_SHIFT
;
483 unsigned long i
= (index
>> shift
) & RADIX_TREE_MAP_MASK
;
485 for ( ; i
< RADIX_TREE_MAP_SIZE
; i
++) {
486 if (slot
->slots
[i
] != NULL
)
488 index
&= ~((1UL << shift
) - 1);
489 index
+= 1UL << shift
;
491 goto out
; /* 32-bit wraparound */
493 if (i
== RADIX_TREE_MAP_SIZE
)
496 if (height
== 0) { /* Bottom level: grab some items */
497 unsigned long j
= index
& RADIX_TREE_MAP_MASK
;
499 for ( ; j
< RADIX_TREE_MAP_SIZE
; j
++) {
501 if (slot
->slots
[j
]) {
502 results
[nr_found
++] = slot
->slots
[j
];
503 if (nr_found
== max_items
)
508 shift
-= RADIX_TREE_MAP_SHIFT
;
509 slot
= slot
->slots
[i
];
517 * radix_tree_gang_lookup - perform multiple lookup on a radix tree
518 * @root: radix tree root
519 * @results: where the results of the lookup are placed
520 * @first_index: start the lookup from this key
521 * @max_items: place up to this many items at *results
523 * Performs an index-ascending scan of the tree for present items. Places
524 * them at *@results and returns the number of items which were placed at
527 * The implementation is naive.
530 radix_tree_gang_lookup(struct radix_tree_root
*root
, void **results
,
531 unsigned long first_index
, unsigned int max_items
)
533 const unsigned long max_index
= radix_tree_maxindex(root
->height
);
534 unsigned long cur_index
= first_index
;
535 unsigned int ret
= 0;
537 while (ret
< max_items
) {
538 unsigned int nr_found
;
539 unsigned long next_index
; /* Index of next search */
541 if (cur_index
> max_index
)
543 nr_found
= __lookup(root
, results
+ ret
, cur_index
,
544 max_items
- ret
, &next_index
);
548 cur_index
= next_index
;
552 EXPORT_SYMBOL(radix_tree_gang_lookup
);
555 * FIXME: the two tag_get()s here should use find_next_bit() instead of
556 * open-coding the search.
559 __lookup_tag(struct radix_tree_root
*root
, void **results
, unsigned long index
,
560 unsigned int max_items
, unsigned long *next_index
, int tag
)
562 unsigned int nr_found
= 0;
564 unsigned int height
= root
->height
;
565 struct radix_tree_node
*slot
;
567 shift
= (height
- 1) * RADIX_TREE_MAP_SHIFT
;
571 unsigned long i
= (index
>> shift
) & RADIX_TREE_MAP_MASK
;
573 for ( ; i
< RADIX_TREE_MAP_SIZE
; i
++) {
574 if (tag_get(slot
, tag
, i
)) {
575 BUG_ON(slot
->slots
[i
] == NULL
);
578 index
&= ~((1UL << shift
) - 1);
579 index
+= 1UL << shift
;
581 goto out
; /* 32-bit wraparound */
583 if (i
== RADIX_TREE_MAP_SIZE
)
586 if (height
== 0) { /* Bottom level: grab some items */
587 unsigned long j
= index
& RADIX_TREE_MAP_MASK
;
589 for ( ; j
< RADIX_TREE_MAP_SIZE
; j
++) {
591 if (tag_get(slot
, tag
, j
)) {
592 BUG_ON(slot
->slots
[j
] == NULL
);
593 results
[nr_found
++] = slot
->slots
[j
];
594 if (nr_found
== max_items
)
599 shift
-= RADIX_TREE_MAP_SHIFT
;
600 slot
= slot
->slots
[i
];
608 * radix_tree_gang_lookup_tag - perform multiple lookup on a radix tree
610 * @root: radix tree root
611 * @results: where the results of the lookup are placed
612 * @first_index: start the lookup from this key
613 * @max_items: place up to this many items at *results
614 * @tag: the tag index
616 * Performs an index-ascending scan of the tree for present items which
617 * have the tag indexed by @tag set. Places the items at *@results and
618 * returns the number of items which were placed at *@results.
621 radix_tree_gang_lookup_tag(struct radix_tree_root
*root
, void **results
,
622 unsigned long first_index
, unsigned int max_items
, int tag
)
624 const unsigned long max_index
= radix_tree_maxindex(root
->height
);
625 unsigned long cur_index
= first_index
;
626 unsigned int ret
= 0;
628 while (ret
< max_items
) {
629 unsigned int nr_found
;
630 unsigned long next_index
; /* Index of next search */
632 if (cur_index
> max_index
)
634 nr_found
= __lookup_tag(root
, results
+ ret
, cur_index
,
635 max_items
- ret
, &next_index
, tag
);
639 cur_index
= next_index
;
643 EXPORT_SYMBOL(radix_tree_gang_lookup_tag
);
646 * radix_tree_delete - delete an item from a radix tree
647 * @root: radix tree root
650 * Remove the item at @index from the radix tree rooted at @root.
652 * Returns the address of the deleted item, or NULL if it was not present.
654 void *radix_tree_delete(struct radix_tree_root
*root
, unsigned long index
)
656 struct radix_tree_path path
[RADIX_TREE_MAX_PATH
], *pathp
= path
;
657 struct radix_tree_path
*orig_pathp
;
658 unsigned int height
, shift
;
660 char tags
[RADIX_TREE_TAGS
];
663 height
= root
->height
;
664 if (index
> radix_tree_maxindex(height
))
667 shift
= (height
- 1) * RADIX_TREE_MAP_SHIFT
;
669 pathp
->slot
= &root
->rnode
;
674 if (*pathp
->slot
== NULL
)
677 offset
= (index
>> shift
) & RADIX_TREE_MAP_MASK
;
678 pathp
[1].offset
= offset
;
679 pathp
[1].node
= *pathp
[0].slot
;
680 pathp
[1].slot
= (struct radix_tree_node
**)
681 (pathp
[1].node
->slots
+ offset
);
683 shift
-= RADIX_TREE_MAP_SHIFT
;
687 ret
= *pathp
[0].slot
;
694 * Clear all tags associated with the just-deleted item
696 memset(tags
, 0, sizeof(tags
));
700 nr_cleared_tags
= RADIX_TREE_TAGS
;
701 for (tag
= 0; tag
< RADIX_TREE_TAGS
; tag
++) {
705 tag_clear(pathp
[0].node
, tag
, pathp
[0].offset
);
707 for (idx
= 0; idx
< RADIX_TREE_TAG_LONGS
; idx
++) {
708 if (pathp
[0].node
->tags
[tag
][idx
]) {
716 } while (pathp
[0].node
&& nr_cleared_tags
);
719 *pathp
[0].slot
= NULL
;
720 while (pathp
[0].node
&& --pathp
[0].node
->count
== 0) {
722 BUG_ON(*pathp
[0].slot
== NULL
);
723 *pathp
[0].slot
= NULL
;
724 radix_tree_node_free(pathp
[1].node
);
726 if (root
->rnode
== NULL
)
731 EXPORT_SYMBOL(radix_tree_delete
);
734 * radix_tree_tagged - test whether any items in the tree are tagged
735 * @root: radix tree root
738 int radix_tree_tagged(struct radix_tree_root
*root
, int tag
)
744 for (idx
= 0; idx
< RADIX_TREE_TAG_LONGS
; idx
++) {
745 if (root
->rnode
->tags
[tag
][idx
])
750 EXPORT_SYMBOL(radix_tree_tagged
);
753 radix_tree_node_ctor(void *node
, kmem_cache_t
*cachep
, unsigned long flags
)
755 memset(node
, 0, sizeof(struct radix_tree_node
));
758 static __init
unsigned long __maxindex(unsigned int height
)
760 unsigned int tmp
= height
* RADIX_TREE_MAP_SHIFT
;
761 unsigned long index
= (~0UL >> (RADIX_TREE_INDEX_BITS
- tmp
- 1)) >> 1;
763 if (tmp
>= RADIX_TREE_INDEX_BITS
)
768 static __init
void radix_tree_init_maxindex(void)
772 for (i
= 0; i
< ARRAY_SIZE(height_to_maxindex
); i
++)
773 height_to_maxindex
[i
] = __maxindex(i
);
776 #ifdef CONFIG_HOTPLUG_CPU
777 static int radix_tree_callback(struct notifier_block
*nfb
,
778 unsigned long action
,
781 int cpu
= (long)hcpu
;
782 struct radix_tree_preload
*rtp
;
784 /* Free per-cpu pool of perloaded nodes */
785 if (action
== CPU_DEAD
) {
786 rtp
= &per_cpu(radix_tree_preloads
, cpu
);
788 kmem_cache_free(radix_tree_node_cachep
,
789 rtp
->nodes
[rtp
->nr
-1]);
790 rtp
->nodes
[rtp
->nr
-1] = NULL
;
796 #endif /* CONFIG_HOTPLUG_CPU */
798 void __init
radix_tree_init(void)
800 radix_tree_node_cachep
= kmem_cache_create("radix_tree_node",
801 sizeof(struct radix_tree_node
), 0,
802 SLAB_PANIC
, radix_tree_node_ctor
, NULL
);
803 radix_tree_init_maxindex();
804 hotcpu_notifier(radix_tree_callback
, 0);