23 #include <linux/errno.h>
25 #include <linux/kernel.h>
26 #include <linux/export.h>
29 #include <linux/slab.h>
32 #include <linux/string.h>
33 #include <linux/bitops.h>
38 #define RADIX_TREE_MAP_SHIFT (CONFIG_BASE_SMALL ? 4 : 6)
40 #define RADIX_TREE_MAP_SHIFT 3
43 #define RADIX_TREE_MAP_SIZE (1UL << RADIX_TREE_MAP_SHIFT)
44 #define RADIX_TREE_MAP_MASK (RADIX_TREE_MAP_SIZE-1)
46 #define RADIX_TREE_TAG_LONGS \
47 ((RADIX_TREE_MAP_SIZE + BITS_PER_LONG - 1) / BITS_PER_LONG)
60 #define RADIX_TREE_INDEX_BITS (8 * sizeof(unsigned long))
61 #define RADIX_TREE_MAX_PATH (DIV_ROUND_UP(RADIX_TREE_INDEX_BITS, \
62 RADIX_TREE_MAP_SHIFT))
73 static struct kmem_cache *radix_tree_node_cachep;
86 #define RADIX_TREE_PRELOAD_SIZE (RADIX_TREE_MAX_PATH * 2 - 1)
97 static inline void *ptr_to_indirect(
void *
ptr)
102 static inline void *indirect_to_ptr(
void *ptr)
135 static inline void root_tag_clear(
struct radix_tree_root *root,
unsigned int tag)
145 static inline int root_tag_get(
struct radix_tree_root *root,
unsigned int tag)
158 if (node->
tags[tag][idx])
176 radix_tree_find_next_bit(
const unsigned long *
addr,
179 if (!__builtin_constant_p(size))
190 while (offset < size) {
228 BUG_ON(radix_tree_is_indirect_ptr(ret));
232 static void radix_tree_node_rcu_free(
struct rcu_head *
head)
244 tag_clear(node, i, 0);
297 static inline unsigned long radix_tree_maxindex(
unsigned int height)
299 return height_to_maxindex[
height];
313 height = root->
height + 1;
314 while (index > radix_tree_maxindex(height))
323 unsigned int newheight;
324 if (!(node = radix_tree_node_alloc(root)))
329 if (root_tag_get(root, tag))
330 tag_set(node, tag, 0);
334 newheight = root->
height+1;
340 slot = indirect_to_ptr(slot);
344 node = ptr_to_indirect(node);
347 }
while (height > root->
height);
361 unsigned long index,
void *
item)
364 unsigned int height, shift;
368 BUG_ON(radix_tree_is_indirect_ptr(item));
371 if (index > radix_tree_maxindex(root->
height)) {
372 error = radix_tree_extend(root, index);
377 slot = indirect_to_ptr(root->
rnode);
386 if (!(slot = radix_tree_node_alloc(root)))
411 BUG_ON(tag_get(node, 0, offset));
412 BUG_ON(tag_get(node, 1, offset));
415 BUG_ON(root_tag_get(root, 0));
416 BUG_ON(root_tag_get(root, 1));
428 unsigned long index,
int is_slot)
430 unsigned int height, shift;
437 if (!radix_tree_is_indirect_ptr(node)) {
440 return is_slot ? (
void *)&root->
rnode : node;
442 node = indirect_to_ptr(node);
445 if (index > radix_tree_maxindex(height))
459 }
while (height > 0);
461 return is_slot ? (
void *)slot : indirect_to_ptr(node);
479 return (
void **)radix_tree_lookup_element(root, index, 1);
497 return radix_tree_lookup_element(root, index, 0);
515 unsigned long index,
unsigned int tag)
517 unsigned int height, shift;
521 BUG_ON(index > radix_tree_maxindex(height));
523 slot = indirect_to_ptr(root->
rnode);
530 if (!tag_get(slot, tag, offset))
531 tag_set(slot, tag, offset);
539 if (slot && !root_tag_get(root, tag))
540 root_tag_set(root, tag);
561 unsigned long index,
unsigned int tag)
565 unsigned int height, shift;
569 if (index > radix_tree_maxindex(height))
573 slot = indirect_to_ptr(root->
rnode);
589 if (!tag_get(node, tag, offset))
591 tag_clear(node, tag, offset);
592 if (any_tag_set(node, tag))
601 if (root_tag_get(root, tag))
602 root_tag_clear(root, tag);
625 unsigned long index,
unsigned int tag)
627 unsigned int height, shift;
631 if (!root_tag_get(root, tag))
638 if (!radix_tree_is_indirect_ptr(node))
640 node = indirect_to_ptr(node);
643 if (index > radix_tree_maxindex(height))
655 if (!tag_get(node, tag, offset))
694 if (!index && iter->
index)
698 if (radix_tree_is_indirect_ptr(rnode)) {
699 rnode = indirect_to_ptr(rnode);
700 }
else if (rnode && !index) {
705 return (
void **)&root->
rnode;
711 offset = index >> shift;
719 if ((flags & RADIX_TREE_ITER_TAGGED) ?
726 if (flags & RADIX_TREE_ITER_TAGGED)
727 offset = radix_tree_find_next_bit(
733 if (node->
slots[offset])
737 index += offset << shift;
761 if (flags & RADIX_TREE_ITER_TAGGED) {
762 unsigned tag_long, tag_bit;
768 if (tag_long < RADIX_TREE_TAG_LONGS - 1) {
810 unsigned long *first_indexp,
unsigned long last_index,
811 unsigned long nr_to_tag,
812 unsigned int iftag,
unsigned int settag)
814 unsigned int height = root->
height;
818 unsigned long tagged = 0;
819 unsigned long index = *first_indexp;
821 last_index =
min(last_index, radix_tree_maxindex(height));
822 if (index > last_index)
826 if (!root_tag_get(root, iftag)) {
827 *first_indexp = last_index + 1;
831 *first_indexp = last_index + 1;
832 root_tag_set(root, settag);
837 slot = indirect_to_ptr(root->
rnode);
840 unsigned long upindex;
844 if (!slot->
slots[offset])
846 if (!tag_get(slot, iftag, offset))
858 tag_set(slot, settag, offset);
867 if (tag_get(node, settag, offset))
869 tag_set(node, settag, offset);
884 index = ((index >> shift) + 1) << shift;
886 if (index > last_index || !index)
888 if (tagged >= nr_to_tag)
905 root_tag_set(root, settag);
906 *first_indexp =
index;
934 unsigned long index,
unsigned long max_scan)
938 for (i = 0; i < max_scan; i++) {
971 unsigned long index,
unsigned long max_scan)
975 for (i = 0; i < max_scan; i++) {
1008 unsigned long first_index,
unsigned int max_items)
1012 unsigned int ret = 0;
1021 if (++ret == max_items)
1049 void ***results,
unsigned long *indices,
1050 unsigned long first_index,
unsigned int max_items)
1054 unsigned int ret = 0;
1063 if (++ret == max_items)
1086 unsigned long first_index,
unsigned int max_items,
1091 unsigned int ret = 0;
1100 if (++ret == max_items)
1123 unsigned long first_index,
unsigned int max_items,
1128 unsigned int ret = 0;
1135 if (++ret == max_items)
1143 #if defined(CONFIG_SHMEM) && defined(CONFIG_SWAP)
1144 #include <linux/sched.h>
1150 unsigned long index,
unsigned long *found_index)
1152 unsigned int shift,
height;
1158 for ( ; height > 1; height--) {
1163 index &= ~((1
UL << shift) - 1);
1164 index += 1
UL << shift;
1180 if (slot->
slots[i] == item) {
1181 *found_index = index +
i;
1203 unsigned long max_index;
1204 unsigned long cur_index = 0;
1205 unsigned long found_index = -1;
1210 if (!radix_tree_is_indirect_ptr(node)) {
1217 node = indirect_to_ptr(node);
1218 max_index = radix_tree_maxindex(node->
height);
1219 if (cur_index > max_index)
1222 cur_index = __locate(node, item, cur_index, &found_index);
1225 }
while (cur_index != 0 && cur_index <= max_index);
1243 while (root->
height > 0) {
1247 BUG_ON(!radix_tree_is_indirect_ptr(to_free));
1248 to_free = indirect_to_ptr(to_free);
1254 if (to_free->
count != 1)
1256 if (!to_free->
slots[0])
1266 slot = to_free->
slots[0];
1269 slot = ptr_to_indirect(slot);
1293 *((
unsigned long *)&to_free->
slots[0]) |=
1296 radix_tree_node_free(to_free);
1314 unsigned int height, shift;
1319 if (index > radix_tree_maxindex(height))
1324 root_tag_clear_all(root);
1328 slot = indirect_to_ptr(slot);
1349 if (tag_get(node, tag, offset))
1363 radix_tree_node_free(to_free);
1366 if (node == indirect_to_ptr(root->
rnode))
1367 radix_tree_shrink(root);
1379 root_tag_clear_all(root);
1383 radix_tree_node_free(to_free);
1397 return root_tag_get(root, tag);
1402 radix_tree_node_ctor(
void *node)
1407 static __init unsigned long __maxindex(
unsigned int height)
1416 return ~0
UL >> shift;
1419 static __init void radix_tree_init_maxindex(
void)
1423 for (i = 0; i <
ARRAY_SIZE(height_to_maxindex); i++)
1424 height_to_maxindex[i] = __maxindex(i);
1436 rtp = &
per_cpu(radix_tree_preloads, cpu);
1452 radix_tree_node_ctor);
1453 radix_tree_init_maxindex();