diff options
Diffstat (limited to 'lib/radix-tree.c')
| -rw-r--r-- | lib/radix-tree.c | 762 | 
1 files changed, 533 insertions, 229 deletions
| diff --git a/lib/radix-tree.c b/lib/radix-tree.c index 84812a9fb16f..691a9ad48497 100644 --- a/lib/radix-tree.c +++ b/lib/radix-tree.c | |||
| @@ -22,20 +22,21 @@ | |||
| 22 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | 22 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | 
| 23 | */ | 23 | */ | 
| 24 | 24 | ||
| 25 | #include <linux/bitmap.h> | ||
| 26 | #include <linux/bitops.h> | ||
| 25 | #include <linux/cpu.h> | 27 | #include <linux/cpu.h> | 
| 26 | #include <linux/errno.h> | 28 | #include <linux/errno.h> | 
| 29 | #include <linux/export.h> | ||
| 30 | #include <linux/idr.h> | ||
| 27 | #include <linux/init.h> | 31 | #include <linux/init.h> | 
| 28 | #include <linux/kernel.h> | 32 | #include <linux/kernel.h> | 
| 29 | #include <linux/export.h> | 33 | #include <linux/kmemleak.h> | 
| 30 | #include <linux/radix-tree.h> | ||
| 31 | #include <linux/percpu.h> | 34 | #include <linux/percpu.h> | 
| 35 | #include <linux/preempt.h> /* in_interrupt() */ | ||
| 36 | #include <linux/radix-tree.h> | ||
| 37 | #include <linux/rcupdate.h> | ||
| 32 | #include <linux/slab.h> | 38 | #include <linux/slab.h> | 
| 33 | #include <linux/kmemleak.h> | ||
| 34 | #include <linux/cpu.h> | ||
| 35 | #include <linux/string.h> | 39 | #include <linux/string.h> | 
| 36 | #include <linux/bitops.h> | ||
| 37 | #include <linux/rcupdate.h> | ||
| 38 | #include <linux/preempt.h> /* in_interrupt() */ | ||
| 39 | 40 | ||
| 40 | 41 | ||
| 41 | /* Number of nodes in fully populated tree of given height */ | 42 | /* Number of nodes in fully populated tree of given height */ | 
| @@ -60,11 +61,28 @@ static struct kmem_cache *radix_tree_node_cachep; | |||
| 60 | #define RADIX_TREE_PRELOAD_SIZE (RADIX_TREE_MAX_PATH * 2 - 1) | 61 | #define RADIX_TREE_PRELOAD_SIZE (RADIX_TREE_MAX_PATH * 2 - 1) | 
| 61 | 62 | ||
| 62 | /* | 63 | /* | 
| 64 | * The IDR does not have to be as high as the radix tree since it uses | ||
| 65 | * signed integers, not unsigned longs. | ||
| 66 | */ | ||
| 67 | #define IDR_INDEX_BITS (8 /* CHAR_BIT */ * sizeof(int) - 1) | ||
| 68 | #define IDR_MAX_PATH (DIV_ROUND_UP(IDR_INDEX_BITS, \ | ||
| 69 | RADIX_TREE_MAP_SHIFT)) | ||
| 70 | #define IDR_PRELOAD_SIZE (IDR_MAX_PATH * 2 - 1) | ||
| 71 | |||
| 72 | /* | ||
| 73 | * The IDA is even shorter since it uses a bitmap at the last level. | ||
| 74 | */ | ||
| 75 | #define IDA_INDEX_BITS (8 * sizeof(int) - 1 - ilog2(IDA_BITMAP_BITS)) | ||
| 76 | #define IDA_MAX_PATH (DIV_ROUND_UP(IDA_INDEX_BITS, \ | ||
| 77 | RADIX_TREE_MAP_SHIFT)) | ||
| 78 | #define IDA_PRELOAD_SIZE (IDA_MAX_PATH * 2 - 1) | ||
| 79 | |||
| 80 | /* | ||
| 63 | * Per-cpu pool of preloaded nodes | 81 | * Per-cpu pool of preloaded nodes | 
| 64 | */ | 82 | */ | 
| 65 | struct radix_tree_preload { | 83 | struct radix_tree_preload { | 
| 66 | unsigned nr; | 84 | unsigned nr; | 
| 67 | /* nodes->private_data points to next preallocated node */ | 85 | /* nodes->parent points to next preallocated node */ | 
| 68 | struct radix_tree_node *nodes; | 86 | struct radix_tree_node *nodes; | 
| 69 | }; | 87 | }; | 
| 70 | static DEFINE_PER_CPU(struct radix_tree_preload, radix_tree_preloads) = { 0, }; | 88 | static DEFINE_PER_CPU(struct radix_tree_preload, radix_tree_preloads) = { 0, }; | 
| @@ -83,35 +101,38 @@ static inline void *node_to_entry(void *ptr) | |||
| 83 | 101 | ||
| 84 | #ifdef CONFIG_RADIX_TREE_MULTIORDER | 102 | #ifdef CONFIG_RADIX_TREE_MULTIORDER | 
| 85 | /* Sibling slots point directly to another slot in the same node */ | 103 | /* Sibling slots point directly to another slot in the same node */ | 
| 86 | static inline bool is_sibling_entry(struct radix_tree_node *parent, void *node) | 104 | static inline | 
| 105 | bool is_sibling_entry(const struct radix_tree_node *parent, void *node) | ||
| 87 | { | 106 | { | 
| 88 | void **ptr = node; | 107 | void __rcu **ptr = node; | 
| 89 | return (parent->slots <= ptr) && | 108 | return (parent->slots <= ptr) && | 
| 90 | (ptr < parent->slots + RADIX_TREE_MAP_SIZE); | 109 | (ptr < parent->slots + RADIX_TREE_MAP_SIZE); | 
| 91 | } | 110 | } | 
| 92 | #else | 111 | #else | 
| 93 | static inline bool is_sibling_entry(struct radix_tree_node *parent, void *node) | 112 | static inline | 
| 113 | bool is_sibling_entry(const struct radix_tree_node *parent, void *node) | ||
| 94 | { | 114 | { | 
| 95 | return false; | 115 | return false; | 
| 96 | } | 116 | } | 
| 97 | #endif | 117 | #endif | 
| 98 | 118 | ||
| 99 | static inline unsigned long get_slot_offset(struct radix_tree_node *parent, | 119 | static inline unsigned long | 
| 100 | void **slot) | 120 | get_slot_offset(const struct radix_tree_node *parent, void __rcu **slot) | 
| 101 | { | 121 | { | 
| 102 | return slot - parent->slots; | 122 | return slot - parent->slots; | 
| 103 | } | 123 | } | 
| 104 | 124 | ||
| 105 | static unsigned int radix_tree_descend(struct radix_tree_node *parent, | 125 | static unsigned int radix_tree_descend(const struct radix_tree_node *parent, | 
| 106 | struct radix_tree_node **nodep, unsigned long index) | 126 | struct radix_tree_node **nodep, unsigned long index) | 
| 107 | { | 127 | { | 
| 108 | unsigned int offset = (index >> parent->shift) & RADIX_TREE_MAP_MASK; | 128 | unsigned int offset = (index >> parent->shift) & RADIX_TREE_MAP_MASK; | 
| 109 | void **entry = rcu_dereference_raw(parent->slots[offset]); | 129 | void __rcu **entry = rcu_dereference_raw(parent->slots[offset]); | 
| 110 | 130 | ||
| 111 | #ifdef CONFIG_RADIX_TREE_MULTIORDER | 131 | #ifdef CONFIG_RADIX_TREE_MULTIORDER | 
| 112 | if (radix_tree_is_internal_node(entry)) { | 132 | if (radix_tree_is_internal_node(entry)) { | 
| 113 | if (is_sibling_entry(parent, entry)) { | 133 | if (is_sibling_entry(parent, entry)) { | 
| 114 | void **sibentry = (void **) entry_to_node(entry); | 134 | void __rcu **sibentry; | 
| 135 | sibentry = (void __rcu **) entry_to_node(entry); | ||
| 115 | offset = get_slot_offset(parent, sibentry); | 136 | offset = get_slot_offset(parent, sibentry); | 
| 116 | entry = rcu_dereference_raw(*sibentry); | 137 | entry = rcu_dereference_raw(*sibentry); | 
| 117 | } | 138 | } | 
| @@ -122,7 +143,7 @@ static unsigned int radix_tree_descend(struct radix_tree_node *parent, | |||
| 122 | return offset; | 143 | return offset; | 
| 123 | } | 144 | } | 
| 124 | 145 | ||
| 125 | static inline gfp_t root_gfp_mask(struct radix_tree_root *root) | 146 | static inline gfp_t root_gfp_mask(const struct radix_tree_root *root) | 
| 126 | { | 147 | { | 
| 127 | return root->gfp_mask & __GFP_BITS_MASK; | 148 | return root->gfp_mask & __GFP_BITS_MASK; | 
| 128 | } | 149 | } | 
| @@ -139,42 +160,48 @@ static inline void tag_clear(struct radix_tree_node *node, unsigned int tag, | |||
| 139 | __clear_bit(offset, node->tags[tag]); | 160 | __clear_bit(offset, node->tags[tag]); | 
| 140 | } | 161 | } | 
| 141 | 162 | ||
| 142 | static inline int tag_get(struct radix_tree_node *node, unsigned int tag, | 163 | static inline int tag_get(const struct radix_tree_node *node, unsigned int tag, | 
| 143 | int offset) | 164 | int offset) | 
| 144 | { | 165 | { | 
| 145 | return test_bit(offset, node->tags[tag]); | 166 | return test_bit(offset, node->tags[tag]); | 
| 146 | } | 167 | } | 
| 147 | 168 | ||
| 148 | static inline void root_tag_set(struct radix_tree_root *root, unsigned int tag) | 169 | static inline void root_tag_set(struct radix_tree_root *root, unsigned tag) | 
| 149 | { | 170 | { | 
| 150 | root->gfp_mask |= (__force gfp_t)(1 << (tag + __GFP_BITS_SHIFT)); | 171 | root->gfp_mask |= (__force gfp_t)(1 << (tag + ROOT_TAG_SHIFT)); | 
| 151 | } | 172 | } | 
| 152 | 173 | ||
| 153 | static inline void root_tag_clear(struct radix_tree_root *root, unsigned tag) | 174 | static inline void root_tag_clear(struct radix_tree_root *root, unsigned tag) | 
| 154 | { | 175 | { | 
| 155 | root->gfp_mask &= (__force gfp_t)~(1 << (tag + __GFP_BITS_SHIFT)); | 176 | root->gfp_mask &= (__force gfp_t)~(1 << (tag + ROOT_TAG_SHIFT)); | 
| 156 | } | 177 | } | 
| 157 | 178 | ||
| 158 | static inline void root_tag_clear_all(struct radix_tree_root *root) | 179 | static inline void root_tag_clear_all(struct radix_tree_root *root) | 
| 159 | { | 180 | { | 
| 160 | root->gfp_mask &= __GFP_BITS_MASK; | 181 | root->gfp_mask &= (1 << ROOT_TAG_SHIFT) - 1; | 
| 182 | } | ||
| 183 | |||
| 184 | static inline int root_tag_get(const struct radix_tree_root *root, unsigned tag) | ||
| 185 | { | ||
| 186 | return (__force int)root->gfp_mask & (1 << (tag + ROOT_TAG_SHIFT)); | ||
| 161 | } | 187 | } | 
| 162 | 188 | ||
| 163 | static inline int root_tag_get(struct radix_tree_root *root, unsigned int tag) | 189 | static inline unsigned root_tags_get(const struct radix_tree_root *root) | 
| 164 | { | 190 | { | 
| 165 | return (__force int)root->gfp_mask & (1 << (tag + __GFP_BITS_SHIFT)); | 191 | return (__force unsigned)root->gfp_mask >> ROOT_TAG_SHIFT; | 
| 166 | } | 192 | } | 
| 167 | 193 | ||
| 168 | static inline unsigned root_tags_get(struct radix_tree_root *root) | 194 | static inline bool is_idr(const struct radix_tree_root *root) | 
| 169 | { | 195 | { | 
| 170 | return (__force unsigned)root->gfp_mask >> __GFP_BITS_SHIFT; | 196 | return !!(root->gfp_mask & ROOT_IS_IDR); | 
| 171 | } | 197 | } | 
| 172 | 198 | ||
| 173 | /* | 199 | /* | 
| 174 | * Returns 1 if any slot in the node has this tag set. | 200 | * Returns 1 if any slot in the node has this tag set. | 
| 175 | * Otherwise returns 0. | 201 | * Otherwise returns 0. | 
| 176 | */ | 202 | */ | 
| 177 | static inline int any_tag_set(struct radix_tree_node *node, unsigned int tag) | 203 | static inline int any_tag_set(const struct radix_tree_node *node, | 
| 204 | unsigned int tag) | ||
| 178 | { | 205 | { | 
| 179 | unsigned idx; | 206 | unsigned idx; | 
| 180 | for (idx = 0; idx < RADIX_TREE_TAG_LONGS; idx++) { | 207 | for (idx = 0; idx < RADIX_TREE_TAG_LONGS; idx++) { | 
| @@ -184,6 +211,11 @@ static inline int any_tag_set(struct radix_tree_node *node, unsigned int tag) | |||
| 184 | return 0; | 211 | return 0; | 
| 185 | } | 212 | } | 
| 186 | 213 | ||
| 214 | static inline void all_tag_set(struct radix_tree_node *node, unsigned int tag) | ||
| 215 | { | ||
| 216 | bitmap_fill(node->tags[tag], RADIX_TREE_MAP_SIZE); | ||
| 217 | } | ||
| 218 | |||
| 187 | /** | 219 | /** | 
| 188 | * radix_tree_find_next_bit - find the next set bit in a memory region | 220 | * radix_tree_find_next_bit - find the next set bit in a memory region | 
| 189 | * | 221 | * | 
| @@ -232,11 +264,18 @@ static inline unsigned long shift_maxindex(unsigned int shift) | |||
| 232 | return (RADIX_TREE_MAP_SIZE << shift) - 1; | 264 | return (RADIX_TREE_MAP_SIZE << shift) - 1; | 
| 233 | } | 265 | } | 
| 234 | 266 | ||
| 235 | static inline unsigned long node_maxindex(struct radix_tree_node *node) | 267 | static inline unsigned long node_maxindex(const struct radix_tree_node *node) | 
| 236 | { | 268 | { | 
| 237 | return shift_maxindex(node->shift); | 269 | return shift_maxindex(node->shift); | 
| 238 | } | 270 | } | 
| 239 | 271 | ||
| 272 | static unsigned long next_index(unsigned long index, | ||
| 273 | const struct radix_tree_node *node, | ||
| 274 | unsigned long offset) | ||
| 275 | { | ||
| 276 | return (index & ~node_maxindex(node)) + (offset << node->shift); | ||
| 277 | } | ||
| 278 | |||
| 240 | #ifndef __KERNEL__ | 279 | #ifndef __KERNEL__ | 
| 241 | static void dump_node(struct radix_tree_node *node, unsigned long index) | 280 | static void dump_node(struct radix_tree_node *node, unsigned long index) | 
| 242 | { | 281 | { | 
| @@ -275,11 +314,59 @@ static void radix_tree_dump(struct radix_tree_root *root) | |||
| 275 | { | 314 | { | 
| 276 | pr_debug("radix root: %p rnode %p tags %x\n", | 315 | pr_debug("radix root: %p rnode %p tags %x\n", | 
| 277 | root, root->rnode, | 316 | root, root->rnode, | 
| 278 | root->gfp_mask >> __GFP_BITS_SHIFT); | 317 | root->gfp_mask >> ROOT_TAG_SHIFT); | 
| 279 | if (!radix_tree_is_internal_node(root->rnode)) | 318 | if (!radix_tree_is_internal_node(root->rnode)) | 
| 280 | return; | 319 | return; | 
| 281 | dump_node(entry_to_node(root->rnode), 0); | 320 | dump_node(entry_to_node(root->rnode), 0); | 
| 282 | } | 321 | } | 
| 322 | |||
| 323 | static void dump_ida_node(void *entry, unsigned long index) | ||
| 324 | { | ||
| 325 | unsigned long i; | ||
| 326 | |||
| 327 | if (!entry) | ||
| 328 | return; | ||
| 329 | |||
| 330 | if (radix_tree_is_internal_node(entry)) { | ||
| 331 | struct radix_tree_node *node = entry_to_node(entry); | ||
| 332 | |||
| 333 | pr_debug("ida node: %p offset %d indices %lu-%lu parent %p free %lx shift %d count %d\n", | ||
| 334 | node, node->offset, index * IDA_BITMAP_BITS, | ||
| 335 | ((index | node_maxindex(node)) + 1) * | ||
| 336 | IDA_BITMAP_BITS - 1, | ||
| 337 | node->parent, node->tags[0][0], node->shift, | ||
| 338 | node->count); | ||
| 339 | for (i = 0; i < RADIX_TREE_MAP_SIZE; i++) | ||
| 340 | dump_ida_node(node->slots[i], | ||
| 341 | index | (i << node->shift)); | ||
| 342 | } else if (radix_tree_exceptional_entry(entry)) { | ||
| 343 | pr_debug("ida excp: %p offset %d indices %lu-%lu data %lx\n", | ||
| 344 | entry, (int)(index & RADIX_TREE_MAP_MASK), | ||
| 345 | index * IDA_BITMAP_BITS, | ||
| 346 | index * IDA_BITMAP_BITS + BITS_PER_LONG - | ||
| 347 | RADIX_TREE_EXCEPTIONAL_SHIFT, | ||
| 348 | (unsigned long)entry >> | ||
| 349 | RADIX_TREE_EXCEPTIONAL_SHIFT); | ||
| 350 | } else { | ||
| 351 | struct ida_bitmap *bitmap = entry; | ||
| 352 | |||
| 353 | pr_debug("ida btmp: %p offset %d indices %lu-%lu data", bitmap, | ||
| 354 | (int)(index & RADIX_TREE_MAP_MASK), | ||
| 355 | index * IDA_BITMAP_BITS, | ||
| 356 | (index + 1) * IDA_BITMAP_BITS - 1); | ||
| 357 | for (i = 0; i < IDA_BITMAP_LONGS; i++) | ||
| 358 | pr_cont(" %lx", bitmap->bitmap[i]); | ||
| 359 | pr_cont("\n"); | ||
| 360 | } | ||
| 361 | } | ||
| 362 | |||
| 363 | static void ida_dump(struct ida *ida) | ||
| 364 | { | ||
| 365 | struct radix_tree_root *root = &ida->ida_rt; | ||
| 366 | pr_debug("ida: %p node %p free %d\n", ida, root->rnode, | ||
| 367 | root->gfp_mask >> ROOT_TAG_SHIFT); | ||
| 368 | dump_ida_node(root->rnode, 0); | ||
| 369 | } | ||
| 283 | #endif | 370 | #endif | 
| 284 | 371 | ||
| 285 | /* | 372 | /* | 
| @@ -287,13 +374,12 @@ static void radix_tree_dump(struct radix_tree_root *root) | |||
| 287 | * that the caller has pinned this thread of control to the current CPU. | 374 | * that the caller has pinned this thread of control to the current CPU. | 
| 288 | */ | 375 | */ | 
| 289 | static struct radix_tree_node * | 376 | static struct radix_tree_node * | 
| 290 | radix_tree_node_alloc(struct radix_tree_root *root, | 377 | radix_tree_node_alloc(gfp_t gfp_mask, struct radix_tree_node *parent, | 
| 291 | struct radix_tree_node *parent, | 378 | struct radix_tree_root *root, | 
| 292 | unsigned int shift, unsigned int offset, | 379 | unsigned int shift, unsigned int offset, | 
| 293 | unsigned int count, unsigned int exceptional) | 380 | unsigned int count, unsigned int exceptional) | 
| 294 | { | 381 | { | 
| 295 | struct radix_tree_node *ret = NULL; | 382 | struct radix_tree_node *ret = NULL; | 
| 296 | gfp_t gfp_mask = root_gfp_mask(root); | ||
| 297 | 383 | ||
| 298 | /* | 384 | /* | 
| 299 | * Preload code isn't irq safe and it doesn't make sense to use | 385 | * Preload code isn't irq safe and it doesn't make sense to use | 
| @@ -321,8 +407,7 @@ radix_tree_node_alloc(struct radix_tree_root *root, | |||
| 321 | rtp = this_cpu_ptr(&radix_tree_preloads); | 407 | rtp = this_cpu_ptr(&radix_tree_preloads); | 
| 322 | if (rtp->nr) { | 408 | if (rtp->nr) { | 
| 323 | ret = rtp->nodes; | 409 | ret = rtp->nodes; | 
| 324 | rtp->nodes = ret->private_data; | 410 | rtp->nodes = ret->parent; | 
| 325 | ret->private_data = NULL; | ||
| 326 | rtp->nr--; | 411 | rtp->nr--; | 
| 327 | } | 412 | } | 
| 328 | /* | 413 | /* | 
| @@ -336,11 +421,12 @@ radix_tree_node_alloc(struct radix_tree_root *root, | |||
| 336 | out: | 421 | out: | 
| 337 | BUG_ON(radix_tree_is_internal_node(ret)); | 422 | BUG_ON(radix_tree_is_internal_node(ret)); | 
| 338 | if (ret) { | 423 | if (ret) { | 
| 339 | ret->parent = parent; | ||
| 340 | ret->shift = shift; | 424 | ret->shift = shift; | 
| 341 | ret->offset = offset; | 425 | ret->offset = offset; | 
| 342 | ret->count = count; | 426 | ret->count = count; | 
| 343 | ret->exceptional = exceptional; | 427 | ret->exceptional = exceptional; | 
| 428 | ret->parent = parent; | ||
| 429 | ret->root = root; | ||
| 344 | } | 430 | } | 
| 345 | return ret; | 431 | return ret; | 
| 346 | } | 432 | } | 
| @@ -399,7 +485,7 @@ static int __radix_tree_preload(gfp_t gfp_mask, unsigned nr) | |||
| 399 | preempt_disable(); | 485 | preempt_disable(); | 
| 400 | rtp = this_cpu_ptr(&radix_tree_preloads); | 486 | rtp = this_cpu_ptr(&radix_tree_preloads); | 
| 401 | if (rtp->nr < nr) { | 487 | if (rtp->nr < nr) { | 
| 402 | node->private_data = rtp->nodes; | 488 | node->parent = rtp->nodes; | 
| 403 | rtp->nodes = node; | 489 | rtp->nodes = node; | 
| 404 | rtp->nr++; | 490 | rtp->nr++; | 
| 405 | } else { | 491 | } else { | 
| @@ -510,7 +596,7 @@ int radix_tree_maybe_preload_order(gfp_t gfp_mask, int order) | |||
| 510 | return __radix_tree_preload(gfp_mask, nr_nodes); | 596 | return __radix_tree_preload(gfp_mask, nr_nodes); | 
| 511 | } | 597 | } | 
| 512 | 598 | ||
| 513 | static unsigned radix_tree_load_root(struct radix_tree_root *root, | 599 | static unsigned radix_tree_load_root(const struct radix_tree_root *root, | 
| 514 | struct radix_tree_node **nodep, unsigned long *maxindex) | 600 | struct radix_tree_node **nodep, unsigned long *maxindex) | 
| 515 | { | 601 | { | 
| 516 | struct radix_tree_node *node = rcu_dereference_raw(root->rnode); | 602 | struct radix_tree_node *node = rcu_dereference_raw(root->rnode); | 
| @@ -530,10 +616,10 @@ static unsigned radix_tree_load_root(struct radix_tree_root *root, | |||
| 530 | /* | 616 | /* | 
| 531 | * Extend a radix tree so it can store key @index. | 617 | * Extend a radix tree so it can store key @index. | 
| 532 | */ | 618 | */ | 
| 533 | static int radix_tree_extend(struct radix_tree_root *root, | 619 | static int radix_tree_extend(struct radix_tree_root *root, gfp_t gfp, | 
| 534 | unsigned long index, unsigned int shift) | 620 | unsigned long index, unsigned int shift) | 
| 535 | { | 621 | { | 
| 536 | struct radix_tree_node *slot; | 622 | void *entry; | 
| 537 | unsigned int maxshift; | 623 | unsigned int maxshift; | 
| 538 | int tag; | 624 | int tag; | 
| 539 | 625 | ||
| @@ -542,32 +628,44 @@ static int radix_tree_extend(struct radix_tree_root *root, | |||
| 542 | while (index > shift_maxindex(maxshift)) | 628 | while (index > shift_maxindex(maxshift)) | 
| 543 | maxshift += RADIX_TREE_MAP_SHIFT; | 629 | maxshift += RADIX_TREE_MAP_SHIFT; | 
| 544 | 630 | ||
| 545 | slot = root->rnode; | 631 | entry = rcu_dereference_raw(root->rnode); | 
| 546 | if (!slot) | 632 | if (!entry && (!is_idr(root) || root_tag_get(root, IDR_FREE))) | 
| 547 | goto out; | 633 | goto out; | 
| 548 | 634 | ||
| 549 | do { | 635 | do { | 
| 550 | struct radix_tree_node *node = radix_tree_node_alloc(root, | 636 | struct radix_tree_node *node = radix_tree_node_alloc(gfp, NULL, | 
| 551 | NULL, shift, 0, 1, 0); | 637 | root, shift, 0, 1, 0); | 
| 552 | if (!node) | 638 | if (!node) | 
| 553 | return -ENOMEM; | 639 | return -ENOMEM; | 
| 554 | 640 | ||
| 555 | /* Propagate the aggregated tag info into the new root */ | 641 | if (is_idr(root)) { | 
| 556 | for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) { | 642 | all_tag_set(node, IDR_FREE); | 
| 557 | if (root_tag_get(root, tag)) | 643 | if (!root_tag_get(root, IDR_FREE)) { | 
| 558 | tag_set(node, tag, 0); | 644 | tag_clear(node, IDR_FREE, 0); | 
| 645 | root_tag_set(root, IDR_FREE); | ||
| 646 | } | ||
| 647 | } else { | ||
| 648 | /* Propagate the aggregated tag info to the new child */ | ||
| 649 | for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) { | ||
| 650 | if (root_tag_get(root, tag)) | ||
| 651 | tag_set(node, tag, 0); | ||
| 652 | } | ||
| 559 | } | 653 | } | 
| 560 | 654 | ||
| 561 | BUG_ON(shift > BITS_PER_LONG); | 655 | BUG_ON(shift > BITS_PER_LONG); | 
| 562 | if (radix_tree_is_internal_node(slot)) { | 656 | if (radix_tree_is_internal_node(entry)) { | 
| 563 | entry_to_node(slot)->parent = node; | 657 | entry_to_node(entry)->parent = node; | 
| 564 | } else if (radix_tree_exceptional_entry(slot)) { | 658 | } else if (radix_tree_exceptional_entry(entry)) { | 
| 565 | /* Moving an exceptional root->rnode to a node */ | 659 | /* Moving an exceptional root->rnode to a node */ | 
| 566 | node->exceptional = 1; | 660 | node->exceptional = 1; | 
| 567 | } | 661 | } | 
| 568 | node->slots[0] = slot; | 662 | /* | 
| 569 | slot = node_to_entry(node); | 663 | * entry was already in the radix tree, so we do not need | 
| 570 | rcu_assign_pointer(root->rnode, slot); | 664 | * rcu_assign_pointer here | 
| 665 | */ | ||
| 666 | node->slots[0] = (void __rcu *)entry; | ||
| 667 | entry = node_to_entry(node); | ||
| 668 | rcu_assign_pointer(root->rnode, entry); | ||
| 571 | shift += RADIX_TREE_MAP_SHIFT; | 669 | shift += RADIX_TREE_MAP_SHIFT; | 
| 572 | } while (shift <= maxshift); | 670 | } while (shift <= maxshift); | 
| 573 | out: | 671 | out: | 
| @@ -578,12 +676,14 @@ out: | |||
| 578 | * radix_tree_shrink - shrink radix tree to minimum height | 676 | * radix_tree_shrink - shrink radix tree to minimum height | 
| 579 | * @root radix tree root | 677 | * @root radix tree root | 
| 580 | */ | 678 | */ | 
| 581 | static inline void radix_tree_shrink(struct radix_tree_root *root, | 679 | static inline bool radix_tree_shrink(struct radix_tree_root *root, | 
| 582 | radix_tree_update_node_t update_node, | 680 | radix_tree_update_node_t update_node, | 
| 583 | void *private) | 681 | void *private) | 
| 584 | { | 682 | { | 
| 683 | bool shrunk = false; | ||
| 684 | |||
| 585 | for (;;) { | 685 | for (;;) { | 
| 586 | struct radix_tree_node *node = root->rnode; | 686 | struct radix_tree_node *node = rcu_dereference_raw(root->rnode); | 
| 587 | struct radix_tree_node *child; | 687 | struct radix_tree_node *child; | 
| 588 | 688 | ||
| 589 | if (!radix_tree_is_internal_node(node)) | 689 | if (!radix_tree_is_internal_node(node)) | 
| @@ -597,7 +697,7 @@ static inline void radix_tree_shrink(struct radix_tree_root *root, | |||
| 597 | */ | 697 | */ | 
| 598 | if (node->count != 1) | 698 | if (node->count != 1) | 
| 599 | break; | 699 | break; | 
| 600 | child = node->slots[0]; | 700 | child = rcu_dereference_raw(node->slots[0]); | 
| 601 | if (!child) | 701 | if (!child) | 
| 602 | break; | 702 | break; | 
| 603 | if (!radix_tree_is_internal_node(child) && node->shift) | 703 | if (!radix_tree_is_internal_node(child) && node->shift) | 
| @@ -613,7 +713,9 @@ static inline void radix_tree_shrink(struct radix_tree_root *root, | |||
| 613 | * (node->slots[0]), it will be safe to dereference the new | 713 | * (node->slots[0]), it will be safe to dereference the new | 
| 614 | * one (root->rnode) as far as dependent read barriers go. | 714 | * one (root->rnode) as far as dependent read barriers go. | 
| 615 | */ | 715 | */ | 
| 616 | root->rnode = child; | 716 | root->rnode = (void __rcu *)child; | 
| 717 | if (is_idr(root) && !tag_get(node, IDR_FREE, 0)) | ||
| 718 | root_tag_clear(root, IDR_FREE); | ||
| 617 | 719 | ||
| 618 | /* | 720 | /* | 
| 619 | * We have a dilemma here. The node's slot[0] must not be | 721 | * We have a dilemma here. The node's slot[0] must not be | 
| @@ -635,27 +737,34 @@ static inline void radix_tree_shrink(struct radix_tree_root *root, | |||
| 635 | */ | 737 | */ | 
| 636 | node->count = 0; | 738 | node->count = 0; | 
| 637 | if (!radix_tree_is_internal_node(child)) { | 739 | if (!radix_tree_is_internal_node(child)) { | 
| 638 | node->slots[0] = RADIX_TREE_RETRY; | 740 | node->slots[0] = (void __rcu *)RADIX_TREE_RETRY; | 
| 639 | if (update_node) | 741 | if (update_node) | 
| 640 | update_node(node, private); | 742 | update_node(node, private); | 
| 641 | } | 743 | } | 
| 642 | 744 | ||
| 643 | WARN_ON_ONCE(!list_empty(&node->private_list)); | 745 | WARN_ON_ONCE(!list_empty(&node->private_list)); | 
| 644 | radix_tree_node_free(node); | 746 | radix_tree_node_free(node); | 
| 747 | shrunk = true; | ||
| 645 | } | 748 | } | 
| 749 | |||
| 750 | return shrunk; | ||
| 646 | } | 751 | } | 
| 647 | 752 | ||
| 648 | static void delete_node(struct radix_tree_root *root, | 753 | static bool delete_node(struct radix_tree_root *root, | 
| 649 | struct radix_tree_node *node, | 754 | struct radix_tree_node *node, | 
| 650 | radix_tree_update_node_t update_node, void *private) | 755 | radix_tree_update_node_t update_node, void *private) | 
| 651 | { | 756 | { | 
| 757 | bool deleted = false; | ||
| 758 | |||
| 652 | do { | 759 | do { | 
| 653 | struct radix_tree_node *parent; | 760 | struct radix_tree_node *parent; | 
| 654 | 761 | ||
| 655 | if (node->count) { | 762 | if (node->count) { | 
| 656 | if (node == entry_to_node(root->rnode)) | 763 | if (node_to_entry(node) == | 
| 657 | radix_tree_shrink(root, update_node, private); | 764 | rcu_dereference_raw(root->rnode)) | 
| 658 | return; | 765 | deleted |= radix_tree_shrink(root, update_node, | 
| 766 | private); | ||
| 767 | return deleted; | ||
| 659 | } | 768 | } | 
| 660 | 769 | ||
| 661 | parent = node->parent; | 770 | parent = node->parent; | 
| @@ -663,15 +772,23 @@ static void delete_node(struct radix_tree_root *root, | |||
| 663 | parent->slots[node->offset] = NULL; | 772 | parent->slots[node->offset] = NULL; | 
| 664 | parent->count--; | 773 | parent->count--; | 
| 665 | } else { | 774 | } else { | 
| 666 | root_tag_clear_all(root); | 775 | /* | 
| 776 | * Shouldn't the tags already have all been cleared | ||
| 777 | * by the caller? | ||
| 778 | */ | ||
| 779 | if (!is_idr(root)) | ||
| 780 | root_tag_clear_all(root); | ||
| 667 | root->rnode = NULL; | 781 | root->rnode = NULL; | 
| 668 | } | 782 | } | 
| 669 | 783 | ||
| 670 | WARN_ON_ONCE(!list_empty(&node->private_list)); | 784 | WARN_ON_ONCE(!list_empty(&node->private_list)); | 
| 671 | radix_tree_node_free(node); | 785 | radix_tree_node_free(node); | 
| 786 | deleted = true; | ||
| 672 | 787 | ||
| 673 | node = parent; | 788 | node = parent; | 
| 674 | } while (node); | 789 | } while (node); | 
| 790 | |||
| 791 | return deleted; | ||
| 675 | } | 792 | } | 
| 676 | 793 | ||
| 677 | /** | 794 | /** | 
| @@ -693,13 +810,14 @@ static void delete_node(struct radix_tree_root *root, | |||
| 693 | */ | 810 | */ | 
| 694 | int __radix_tree_create(struct radix_tree_root *root, unsigned long index, | 811 | int __radix_tree_create(struct radix_tree_root *root, unsigned long index, | 
| 695 | unsigned order, struct radix_tree_node **nodep, | 812 | unsigned order, struct radix_tree_node **nodep, | 
| 696 | void ***slotp) | 813 | void __rcu ***slotp) | 
| 697 | { | 814 | { | 
| 698 | struct radix_tree_node *node = NULL, *child; | 815 | struct radix_tree_node *node = NULL, *child; | 
| 699 | void **slot = (void **)&root->rnode; | 816 | void __rcu **slot = (void __rcu **)&root->rnode; | 
| 700 | unsigned long maxindex; | 817 | unsigned long maxindex; | 
| 701 | unsigned int shift, offset = 0; | 818 | unsigned int shift, offset = 0; | 
| 702 | unsigned long max = index | ((1UL << order) - 1); | 819 | unsigned long max = index | ((1UL << order) - 1); | 
| 820 | gfp_t gfp = root_gfp_mask(root); | ||
| 703 | 821 | ||
| 704 | shift = radix_tree_load_root(root, &child, &maxindex); | 822 | shift = radix_tree_load_root(root, &child, &maxindex); | 
| 705 | 823 | ||
| @@ -707,18 +825,18 @@ int __radix_tree_create(struct radix_tree_root *root, unsigned long index, | |||
| 707 | if (order > 0 && max == ((1UL << order) - 1)) | 825 | if (order > 0 && max == ((1UL << order) - 1)) | 
| 708 | max++; | 826 | max++; | 
| 709 | if (max > maxindex) { | 827 | if (max > maxindex) { | 
| 710 | int error = radix_tree_extend(root, max, shift); | 828 | int error = radix_tree_extend(root, gfp, max, shift); | 
| 711 | if (error < 0) | 829 | if (error < 0) | 
| 712 | return error; | 830 | return error; | 
| 713 | shift = error; | 831 | shift = error; | 
| 714 | child = root->rnode; | 832 | child = rcu_dereference_raw(root->rnode); | 
| 715 | } | 833 | } | 
| 716 | 834 | ||
| 717 | while (shift > order) { | 835 | while (shift > order) { | 
| 718 | shift -= RADIX_TREE_MAP_SHIFT; | 836 | shift -= RADIX_TREE_MAP_SHIFT; | 
| 719 | if (child == NULL) { | 837 | if (child == NULL) { | 
| 720 | /* Have to add a child node. */ | 838 | /* Have to add a child node. */ | 
| 721 | child = radix_tree_node_alloc(root, node, shift, | 839 | child = radix_tree_node_alloc(gfp, node, root, shift, | 
| 722 | offset, 0, 0); | 840 | offset, 0, 0); | 
| 723 | if (!child) | 841 | if (!child) | 
| 724 | return -ENOMEM; | 842 | return -ENOMEM; | 
| @@ -741,7 +859,6 @@ int __radix_tree_create(struct radix_tree_root *root, unsigned long index, | |||
| 741 | return 0; | 859 | return 0; | 
| 742 | } | 860 | } | 
| 743 | 861 | ||
| 744 | #ifdef CONFIG_RADIX_TREE_MULTIORDER | ||
| 745 | /* | 862 | /* | 
| 746 | * Free any nodes below this node. The tree is presumed to not need | 863 | * Free any nodes below this node. The tree is presumed to not need | 
| 747 | * shrinking, and any user data in the tree is presumed to not need a | 864 | * shrinking, and any user data in the tree is presumed to not need a | 
| @@ -757,7 +874,7 @@ static void radix_tree_free_nodes(struct radix_tree_node *node) | |||
| 757 | struct radix_tree_node *child = entry_to_node(node); | 874 | struct radix_tree_node *child = entry_to_node(node); | 
| 758 | 875 | ||
| 759 | for (;;) { | 876 | for (;;) { | 
| 760 | void *entry = child->slots[offset]; | 877 | void *entry = rcu_dereference_raw(child->slots[offset]); | 
| 761 | if (radix_tree_is_internal_node(entry) && | 878 | if (radix_tree_is_internal_node(entry) && | 
| 762 | !is_sibling_entry(child, entry)) { | 879 | !is_sibling_entry(child, entry)) { | 
| 763 | child = entry_to_node(entry); | 880 | child = entry_to_node(entry); | 
| @@ -777,8 +894,9 @@ static void radix_tree_free_nodes(struct radix_tree_node *node) | |||
| 777 | } | 894 | } | 
| 778 | } | 895 | } | 
| 779 | 896 | ||
| 780 | static inline int insert_entries(struct radix_tree_node *node, void **slot, | 897 | #ifdef CONFIG_RADIX_TREE_MULTIORDER | 
| 781 | void *item, unsigned order, bool replace) | 898 | static inline int insert_entries(struct radix_tree_node *node, | 
| 899 | void __rcu **slot, void *item, unsigned order, bool replace) | ||
| 782 | { | 900 | { | 
| 783 | struct radix_tree_node *child; | 901 | struct radix_tree_node *child; | 
| 784 | unsigned i, n, tag, offset, tags = 0; | 902 | unsigned i, n, tag, offset, tags = 0; | 
| @@ -813,7 +931,7 @@ static inline int insert_entries(struct radix_tree_node *node, void **slot, | |||
| 813 | } | 931 | } | 
| 814 | 932 | ||
| 815 | for (i = 0; i < n; i++) { | 933 | for (i = 0; i < n; i++) { | 
| 816 | struct radix_tree_node *old = slot[i]; | 934 | struct radix_tree_node *old = rcu_dereference_raw(slot[i]); | 
| 817 | if (i) { | 935 | if (i) { | 
| 818 | rcu_assign_pointer(slot[i], child); | 936 | rcu_assign_pointer(slot[i], child); | 
| 819 | for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) | 937 | for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) | 
| @@ -840,8 +958,8 @@ static inline int insert_entries(struct radix_tree_node *node, void **slot, | |||
| 840 | return n; | 958 | return n; | 
| 841 | } | 959 | } | 
| 842 | #else | 960 | #else | 
| 843 | static inline int insert_entries(struct radix_tree_node *node, void **slot, | 961 | static inline int insert_entries(struct radix_tree_node *node, | 
| 844 | void *item, unsigned order, bool replace) | 962 | void __rcu **slot, void *item, unsigned order, bool replace) | 
| 845 | { | 963 | { | 
| 846 | if (*slot) | 964 | if (*slot) | 
| 847 | return -EEXIST; | 965 | return -EEXIST; | 
| @@ -868,7 +986,7 @@ int __radix_tree_insert(struct radix_tree_root *root, unsigned long index, | |||
| 868 | unsigned order, void *item) | 986 | unsigned order, void *item) | 
| 869 | { | 987 | { | 
| 870 | struct radix_tree_node *node; | 988 | struct radix_tree_node *node; | 
| 871 | void **slot; | 989 | void __rcu **slot; | 
| 872 | int error; | 990 | int error; | 
| 873 | 991 | ||
| 874 | BUG_ON(radix_tree_is_internal_node(item)); | 992 | BUG_ON(radix_tree_is_internal_node(item)); | 
| @@ -908,16 +1026,17 @@ EXPORT_SYMBOL(__radix_tree_insert); | |||
| 908 | * allocated and @root->rnode is used as a direct slot instead of | 1026 | * allocated and @root->rnode is used as a direct slot instead of | 
| 909 | * pointing to a node, in which case *@nodep will be NULL. | 1027 | * pointing to a node, in which case *@nodep will be NULL. | 
| 910 | */ | 1028 | */ | 
| 911 | void *__radix_tree_lookup(struct radix_tree_root *root, unsigned long index, | 1029 | void *__radix_tree_lookup(const struct radix_tree_root *root, | 
| 912 | struct radix_tree_node **nodep, void ***slotp) | 1030 | unsigned long index, struct radix_tree_node **nodep, | 
| 1031 | void __rcu ***slotp) | ||
| 913 | { | 1032 | { | 
| 914 | struct radix_tree_node *node, *parent; | 1033 | struct radix_tree_node *node, *parent; | 
| 915 | unsigned long maxindex; | 1034 | unsigned long maxindex; | 
| 916 | void **slot; | 1035 | void __rcu **slot; | 
| 917 | 1036 | ||
| 918 | restart: | 1037 | restart: | 
| 919 | parent = NULL; | 1038 | parent = NULL; | 
| 920 | slot = (void **)&root->rnode; | 1039 | slot = (void __rcu **)&root->rnode; | 
| 921 | radix_tree_load_root(root, &node, &maxindex); | 1040 | radix_tree_load_root(root, &node, &maxindex); | 
| 922 | if (index > maxindex) | 1041 | if (index > maxindex) | 
| 923 | return NULL; | 1042 | return NULL; | 
| @@ -952,9 +1071,10 @@ void *__radix_tree_lookup(struct radix_tree_root *root, unsigned long index, | |||
| 952 | * exclusive from other writers. Any dereference of the slot must be done | 1071 | * exclusive from other writers. Any dereference of the slot must be done | 
| 953 | * using radix_tree_deref_slot. | 1072 | * using radix_tree_deref_slot. | 
| 954 | */ | 1073 | */ | 
| 955 | void **radix_tree_lookup_slot(struct radix_tree_root *root, unsigned long index) | 1074 | void __rcu **radix_tree_lookup_slot(const struct radix_tree_root *root, | 
| 1075 | unsigned long index) | ||
| 956 | { | 1076 | { | 
| 957 | void **slot; | 1077 | void __rcu **slot; | 
| 958 | 1078 | ||
| 959 | if (!__radix_tree_lookup(root, index, NULL, &slot)) | 1079 | if (!__radix_tree_lookup(root, index, NULL, &slot)) | 
| 960 | return NULL; | 1080 | return NULL; | 
| @@ -974,75 +1094,76 @@ EXPORT_SYMBOL(radix_tree_lookup_slot); | |||
| 974 | * them safely). No RCU barriers are required to access or modify the | 1094 | * them safely). No RCU barriers are required to access or modify the | 
| 975 | * returned item, however. | 1095 | * returned item, however. | 
| 976 | */ | 1096 | */ | 
| 977 | void *radix_tree_lookup(struct radix_tree_root *root, unsigned long index) | 1097 | void *radix_tree_lookup(const struct radix_tree_root *root, unsigned long index) | 
| 978 | { | 1098 | { | 
| 979 | return __radix_tree_lookup(root, index, NULL, NULL); | 1099 | return __radix_tree_lookup(root, index, NULL, NULL); | 
| 980 | } | 1100 | } | 
| 981 | EXPORT_SYMBOL(radix_tree_lookup); | 1101 | EXPORT_SYMBOL(radix_tree_lookup); | 
| 982 | 1102 | ||
| 983 | static inline int slot_count(struct radix_tree_node *node, | 1103 | static inline void replace_sibling_entries(struct radix_tree_node *node, | 
| 984 | void **slot) | 1104 | void __rcu **slot, int count, int exceptional) | 
| 985 | { | 1105 | { | 
| 986 | int n = 1; | ||
| 987 | #ifdef CONFIG_RADIX_TREE_MULTIORDER | 1106 | #ifdef CONFIG_RADIX_TREE_MULTIORDER | 
| 988 | void *ptr = node_to_entry(slot); | 1107 | void *ptr = node_to_entry(slot); | 
| 989 | unsigned offset = get_slot_offset(node, slot); | 1108 | unsigned offset = get_slot_offset(node, slot) + 1; | 
| 990 | int i; | ||
| 991 | 1109 | ||
| 992 | for (i = 1; offset + i < RADIX_TREE_MAP_SIZE; i++) { | 1110 | while (offset < RADIX_TREE_MAP_SIZE) { | 
| 993 | if (node->slots[offset + i] != ptr) | 1111 | if (rcu_dereference_raw(node->slots[offset]) != ptr) | 
| 994 | break; | 1112 | break; | 
| 995 | n++; | 1113 | if (count < 0) { | 
| 1114 | node->slots[offset] = NULL; | ||
| 1115 | node->count--; | ||
| 1116 | } | ||
| 1117 | node->exceptional += exceptional; | ||
| 1118 | offset++; | ||
| 996 | } | 1119 | } | 
| 997 | #endif | 1120 | #endif | 
| 998 | return n; | ||
| 999 | } | 1121 | } | 
| 1000 | 1122 | ||
| 1001 | static void replace_slot(struct radix_tree_root *root, | 1123 | static void replace_slot(void __rcu **slot, void *item, | 
| 1002 | struct radix_tree_node *node, | 1124 | struct radix_tree_node *node, int count, int exceptional) | 
| 1003 | void **slot, void *item, | ||
| 1004 | bool warn_typeswitch) | ||
| 1005 | { | 1125 | { | 
| 1006 | void *old = rcu_dereference_raw(*slot); | 1126 | if (WARN_ON_ONCE(radix_tree_is_internal_node(item))) | 
| 1007 | int count, exceptional; | 1127 | return; | 
| 1008 | |||
| 1009 | WARN_ON_ONCE(radix_tree_is_internal_node(item)); | ||
| 1010 | |||
| 1011 | count = !!item - !!old; | ||
| 1012 | exceptional = !!radix_tree_exceptional_entry(item) - | ||
| 1013 | !!radix_tree_exceptional_entry(old); | ||
| 1014 | |||
| 1015 | WARN_ON_ONCE(warn_typeswitch && (count || exceptional)); | ||
| 1016 | 1128 | ||
| 1017 | if (node) { | 1129 | if (node && (count || exceptional)) { | 
| 1018 | node->count += count; | 1130 | node->count += count; | 
| 1019 | if (exceptional) { | 1131 | node->exceptional += exceptional; | 
| 1020 | exceptional *= slot_count(node, slot); | 1132 | replace_sibling_entries(node, slot, count, exceptional); | 
| 1021 | node->exceptional += exceptional; | ||
| 1022 | } | ||
| 1023 | } | 1133 | } | 
| 1024 | 1134 | ||
| 1025 | rcu_assign_pointer(*slot, item); | 1135 | rcu_assign_pointer(*slot, item); | 
| 1026 | } | 1136 | } | 
| 1027 | 1137 | ||
| 1028 | static inline void delete_sibling_entries(struct radix_tree_node *node, | 1138 | static bool node_tag_get(const struct radix_tree_root *root, | 
| 1029 | void **slot) | 1139 | const struct radix_tree_node *node, | 
| 1140 | unsigned int tag, unsigned int offset) | ||
| 1030 | { | 1141 | { | 
| 1031 | #ifdef CONFIG_RADIX_TREE_MULTIORDER | 1142 | if (node) | 
| 1032 | bool exceptional = radix_tree_exceptional_entry(*slot); | 1143 | return tag_get(node, tag, offset); | 
| 1033 | void *ptr = node_to_entry(slot); | 1144 | return root_tag_get(root, tag); | 
| 1034 | unsigned offset = get_slot_offset(node, slot); | 1145 | } | 
| 1035 | int i; | ||
| 1036 | 1146 | ||
| 1037 | for (i = 1; offset + i < RADIX_TREE_MAP_SIZE; i++) { | 1147 | /* | 
| 1038 | if (node->slots[offset + i] != ptr) | 1148 | * IDR users want to be able to store NULL in the tree, so if the slot isn't | 
| 1039 | break; | 1149 | * free, don't adjust the count, even if it's transitioning between NULL and | 
| 1040 | node->slots[offset + i] = NULL; | 1150 | * non-NULL. For the IDA, we mark slots as being IDR_FREE while they still | 
| 1041 | node->count--; | 1151 | * have empty bits, but it only stores NULL in slots when they're being | 
| 1042 | if (exceptional) | 1152 | * deleted. | 
| 1043 | node->exceptional--; | 1153 | */ | 
| 1154 | static int calculate_count(struct radix_tree_root *root, | ||
| 1155 | struct radix_tree_node *node, void __rcu **slot, | ||
| 1156 | void *item, void *old) | ||
| 1157 | { | ||
| 1158 | if (is_idr(root)) { | ||
| 1159 | unsigned offset = get_slot_offset(node, slot); | ||
| 1160 | bool free = node_tag_get(root, node, IDR_FREE, offset); | ||
| 1161 | if (!free) | ||
| 1162 | return 0; | ||
| 1163 | if (!old) | ||
| 1164 | return 1; | ||
| 1044 | } | 1165 | } | 
| 1045 | #endif | 1166 | return !!item - !!old; | 
| 1046 | } | 1167 | } | 
| 1047 | 1168 | ||
| 1048 | /** | 1169 | /** | 
| @@ -1059,18 +1180,22 @@ static inline void delete_sibling_entries(struct radix_tree_node *node, | |||
| 1059 | */ | 1180 | */ | 
| 1060 | void __radix_tree_replace(struct radix_tree_root *root, | 1181 | void __radix_tree_replace(struct radix_tree_root *root, | 
| 1061 | struct radix_tree_node *node, | 1182 | struct radix_tree_node *node, | 
| 1062 | void **slot, void *item, | 1183 | void __rcu **slot, void *item, | 
| 1063 | radix_tree_update_node_t update_node, void *private) | 1184 | radix_tree_update_node_t update_node, void *private) | 
| 1064 | { | 1185 | { | 
| 1065 | if (!item) | 1186 | void *old = rcu_dereference_raw(*slot); | 
| 1066 | delete_sibling_entries(node, slot); | 1187 | int exceptional = !!radix_tree_exceptional_entry(item) - | 
| 1188 | !!radix_tree_exceptional_entry(old); | ||
| 1189 | int count = calculate_count(root, node, slot, item, old); | ||
| 1190 | |||
| 1067 | /* | 1191 | /* | 
| 1068 | * This function supports replacing exceptional entries and | 1192 | * This function supports replacing exceptional entries and | 
| 1069 | * deleting entries, but that needs accounting against the | 1193 | * deleting entries, but that needs accounting against the | 
| 1070 | * node unless the slot is root->rnode. | 1194 | * node unless the slot is root->rnode. | 
| 1071 | */ | 1195 | */ | 
| 1072 | replace_slot(root, node, slot, item, | 1196 | WARN_ON_ONCE(!node && (slot != (void __rcu **)&root->rnode) && | 
| 1073 | !node && slot != (void **)&root->rnode); | 1197 | (count || exceptional)); | 
| 1198 | replace_slot(slot, item, node, count, exceptional); | ||
| 1074 | 1199 | ||
| 1075 | if (!node) | 1200 | if (!node) | 
| 1076 | return; | 1201 | return; | 
| @@ -1098,10 +1223,11 @@ void __radix_tree_replace(struct radix_tree_root *root, | |||
| 1098 | * radix_tree_iter_replace(). | 1223 | * radix_tree_iter_replace(). | 
| 1099 | */ | 1224 | */ | 
| 1100 | void radix_tree_replace_slot(struct radix_tree_root *root, | 1225 | void radix_tree_replace_slot(struct radix_tree_root *root, | 
| 1101 | void **slot, void *item) | 1226 | void __rcu **slot, void *item) | 
| 1102 | { | 1227 | { | 
| 1103 | replace_slot(root, NULL, slot, item, true); | 1228 | __radix_tree_replace(root, NULL, slot, item, NULL, NULL); | 
| 1104 | } | 1229 | } | 
| 1230 | EXPORT_SYMBOL(radix_tree_replace_slot); | ||
| 1105 | 1231 | ||
| 1106 | /** | 1232 | /** | 
| 1107 | * radix_tree_iter_replace - replace item in a slot | 1233 | * radix_tree_iter_replace - replace item in a slot | 
| @@ -1113,7 +1239,8 @@ void radix_tree_replace_slot(struct radix_tree_root *root, | |||
| 1113 | * Caller must hold tree write locked across split and replacement. | 1239 | * Caller must hold tree write locked across split and replacement. | 
| 1114 | */ | 1240 | */ | 
| 1115 | void radix_tree_iter_replace(struct radix_tree_root *root, | 1241 | void radix_tree_iter_replace(struct radix_tree_root *root, | 
| 1116 | const struct radix_tree_iter *iter, void **slot, void *item) | 1242 | const struct radix_tree_iter *iter, | 
| 1243 | void __rcu **slot, void *item) | ||
| 1117 | { | 1244 | { | 
| 1118 | __radix_tree_replace(root, iter->node, slot, item, NULL, NULL); | 1245 | __radix_tree_replace(root, iter->node, slot, item, NULL, NULL); | 
| 1119 | } | 1246 | } | 
| @@ -1137,7 +1264,7 @@ int radix_tree_join(struct radix_tree_root *root, unsigned long index, | |||
| 1137 | unsigned order, void *item) | 1264 | unsigned order, void *item) | 
| 1138 | { | 1265 | { | 
| 1139 | struct radix_tree_node *node; | 1266 | struct radix_tree_node *node; | 
| 1140 | void **slot; | 1267 | void __rcu **slot; | 
| 1141 | int error; | 1268 | int error; | 
| 1142 | 1269 | ||
| 1143 | BUG_ON(radix_tree_is_internal_node(item)); | 1270 | BUG_ON(radix_tree_is_internal_node(item)); | 
| @@ -1172,9 +1299,10 @@ int radix_tree_split(struct radix_tree_root *root, unsigned long index, | |||
| 1172 | unsigned order) | 1299 | unsigned order) | 
| 1173 | { | 1300 | { | 
| 1174 | struct radix_tree_node *parent, *node, *child; | 1301 | struct radix_tree_node *parent, *node, *child; | 
| 1175 | void **slot; | 1302 | void __rcu **slot; | 
| 1176 | unsigned int offset, end; | 1303 | unsigned int offset, end; | 
| 1177 | unsigned n, tag, tags = 0; | 1304 | unsigned n, tag, tags = 0; | 
| 1305 | gfp_t gfp = root_gfp_mask(root); | ||
| 1178 | 1306 | ||
| 1179 | if (!__radix_tree_lookup(root, index, &parent, &slot)) | 1307 | if (!__radix_tree_lookup(root, index, &parent, &slot)) | 
| 1180 | return -ENOENT; | 1308 | return -ENOENT; | 
| @@ -1188,7 +1316,8 @@ int radix_tree_split(struct radix_tree_root *root, unsigned long index, | |||
| 1188 | tags |= 1 << tag; | 1316 | tags |= 1 << tag; | 
| 1189 | 1317 | ||
| 1190 | for (end = offset + 1; end < RADIX_TREE_MAP_SIZE; end++) { | 1318 | for (end = offset + 1; end < RADIX_TREE_MAP_SIZE; end++) { | 
| 1191 | if (!is_sibling_entry(parent, parent->slots[end])) | 1319 | if (!is_sibling_entry(parent, | 
| 1320 | rcu_dereference_raw(parent->slots[end]))) | ||
| 1192 | break; | 1321 | break; | 
| 1193 | for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) | 1322 | for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) | 
| 1194 | if (tags & (1 << tag)) | 1323 | if (tags & (1 << tag)) | 
| @@ -1212,14 +1341,15 @@ int radix_tree_split(struct radix_tree_root *root, unsigned long index, | |||
| 1212 | 1341 | ||
| 1213 | for (;;) { | 1342 | for (;;) { | 
| 1214 | if (node->shift > order) { | 1343 | if (node->shift > order) { | 
| 1215 | child = radix_tree_node_alloc(root, node, | 1344 | child = radix_tree_node_alloc(gfp, node, root, | 
| 1216 | node->shift - RADIX_TREE_MAP_SHIFT, | 1345 | node->shift - RADIX_TREE_MAP_SHIFT, | 
| 1217 | offset, 0, 0); | 1346 | offset, 0, 0); | 
| 1218 | if (!child) | 1347 | if (!child) | 
| 1219 | goto nomem; | 1348 | goto nomem; | 
| 1220 | if (node != parent) { | 1349 | if (node != parent) { | 
| 1221 | node->count++; | 1350 | node->count++; | 
| 1222 | node->slots[offset] = node_to_entry(child); | 1351 | rcu_assign_pointer(node->slots[offset], | 
| 1352 | node_to_entry(child)); | ||
| 1223 | for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) | 1353 | for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) | 
| 1224 | if (tags & (1 << tag)) | 1354 | if (tags & (1 << tag)) | 
| 1225 | tag_set(node, tag, offset); | 1355 | tag_set(node, tag, offset); | 
| @@ -1261,6 +1391,22 @@ int radix_tree_split(struct radix_tree_root *root, unsigned long index, | |||
| 1261 | } | 1391 | } | 
| 1262 | #endif | 1392 | #endif | 
| 1263 | 1393 | ||
| 1394 | static void node_tag_set(struct radix_tree_root *root, | ||
| 1395 | struct radix_tree_node *node, | ||
| 1396 | unsigned int tag, unsigned int offset) | ||
| 1397 | { | ||
| 1398 | while (node) { | ||
| 1399 | if (tag_get(node, tag, offset)) | ||
| 1400 | return; | ||
| 1401 | tag_set(node, tag, offset); | ||
| 1402 | offset = node->offset; | ||
| 1403 | node = node->parent; | ||
| 1404 | } | ||
| 1405 | |||
| 1406 | if (!root_tag_get(root, tag)) | ||
| 1407 | root_tag_set(root, tag); | ||
| 1408 | } | ||
| 1409 | |||
| 1264 | /** | 1410 | /** | 
| 1265 | * radix_tree_tag_set - set a tag on a radix tree node | 1411 | * radix_tree_tag_set - set a tag on a radix tree node | 
| 1266 | * @root: radix tree root | 1412 | * @root: radix tree root | 
| @@ -1302,6 +1448,18 @@ void *radix_tree_tag_set(struct radix_tree_root *root, | |||
| 1302 | } | 1448 | } | 
| 1303 | EXPORT_SYMBOL(radix_tree_tag_set); | 1449 | EXPORT_SYMBOL(radix_tree_tag_set); | 
| 1304 | 1450 | ||
| 1451 | /** | ||
| 1452 | * radix_tree_iter_tag_set - set a tag on the current iterator entry | ||
| 1453 | * @root: radix tree root | ||
| 1454 | * @iter: iterator state | ||
| 1455 | * @tag: tag to set | ||
| 1456 | */ | ||
| 1457 | void radix_tree_iter_tag_set(struct radix_tree_root *root, | ||
| 1458 | const struct radix_tree_iter *iter, unsigned int tag) | ||
| 1459 | { | ||
| 1460 | node_tag_set(root, iter->node, tag, iter_offset(iter)); | ||
| 1461 | } | ||
| 1462 | |||
| 1305 | static void node_tag_clear(struct radix_tree_root *root, | 1463 | static void node_tag_clear(struct radix_tree_root *root, | 
| 1306 | struct radix_tree_node *node, | 1464 | struct radix_tree_node *node, | 
| 1307 | unsigned int tag, unsigned int offset) | 1465 | unsigned int tag, unsigned int offset) | 
| @@ -1322,34 +1480,6 @@ static void node_tag_clear(struct radix_tree_root *root, | |||
| 1322 | root_tag_clear(root, tag); | 1480 | root_tag_clear(root, tag); | 
| 1323 | } | 1481 | } | 
| 1324 | 1482 | ||
| 1325 | static void node_tag_set(struct radix_tree_root *root, | ||
| 1326 | struct radix_tree_node *node, | ||
| 1327 | unsigned int tag, unsigned int offset) | ||
| 1328 | { | ||
| 1329 | while (node) { | ||
| 1330 | if (tag_get(node, tag, offset)) | ||
| 1331 | return; | ||
| 1332 | tag_set(node, tag, offset); | ||
| 1333 | offset = node->offset; | ||
| 1334 | node = node->parent; | ||
| 1335 | } | ||
| 1336 | |||
| 1337 | if (!root_tag_get(root, tag)) | ||
| 1338 | root_tag_set(root, tag); | ||
| 1339 | } | ||
| 1340 | |||
| 1341 | /** | ||
| 1342 | * radix_tree_iter_tag_set - set a tag on the current iterator entry | ||
| 1343 | * @root: radix tree root | ||
| 1344 | * @iter: iterator state | ||
| 1345 | * @tag: tag to set | ||
| 1346 | */ | ||
| 1347 | void radix_tree_iter_tag_set(struct radix_tree_root *root, | ||
| 1348 | const struct radix_tree_iter *iter, unsigned int tag) | ||
| 1349 | { | ||
| 1350 | node_tag_set(root, iter->node, tag, iter_offset(iter)); | ||
| 1351 | } | ||
| 1352 | |||
| 1353 | /** | 1483 | /** | 
| 1354 | * radix_tree_tag_clear - clear a tag on a radix tree node | 1484 | * radix_tree_tag_clear - clear a tag on a radix tree node | 
| 1355 | * @root: radix tree root | 1485 | * @root: radix tree root | 
| @@ -1390,6 +1520,18 @@ void *radix_tree_tag_clear(struct radix_tree_root *root, | |||
| 1390 | EXPORT_SYMBOL(radix_tree_tag_clear); | 1520 | EXPORT_SYMBOL(radix_tree_tag_clear); | 
| 1391 | 1521 | ||
| 1392 | /** | 1522 | /** | 
| 1523 | * radix_tree_iter_tag_clear - clear a tag on the current iterator entry | ||
| 1524 | * @root: radix tree root | ||
| 1525 | * @iter: iterator state | ||
| 1526 | * @tag: tag to clear | ||
| 1527 | */ | ||
| 1528 | void radix_tree_iter_tag_clear(struct radix_tree_root *root, | ||
| 1529 | const struct radix_tree_iter *iter, unsigned int tag) | ||
| 1530 | { | ||
| 1531 | node_tag_clear(root, iter->node, tag, iter_offset(iter)); | ||
| 1532 | } | ||
| 1533 | |||
| 1534 | /** | ||
| 1393 | * radix_tree_tag_get - get a tag on a radix tree node | 1535 | * radix_tree_tag_get - get a tag on a radix tree node | 
| 1394 | * @root: radix tree root | 1536 | * @root: radix tree root | 
| 1395 | * @index: index key | 1537 | * @index: index key | 
| @@ -1404,7 +1546,7 @@ EXPORT_SYMBOL(radix_tree_tag_clear); | |||
| 1404 | * the RCU lock is held, unless tag modification and node deletion are excluded | 1546 | * the RCU lock is held, unless tag modification and node deletion are excluded | 
| 1405 | * from concurrency. | 1547 | * from concurrency. | 
| 1406 | */ | 1548 | */ | 
| 1407 | int radix_tree_tag_get(struct radix_tree_root *root, | 1549 | int radix_tree_tag_get(const struct radix_tree_root *root, | 
| 1408 | unsigned long index, unsigned int tag) | 1550 | unsigned long index, unsigned int tag) | 
| 1409 | { | 1551 | { | 
| 1410 | struct radix_tree_node *node, *parent; | 1552 | struct radix_tree_node *node, *parent; | 
| @@ -1416,8 +1558,6 @@ int radix_tree_tag_get(struct radix_tree_root *root, | |||
| 1416 | radix_tree_load_root(root, &node, &maxindex); | 1558 | radix_tree_load_root(root, &node, &maxindex); | 
| 1417 | if (index > maxindex) | 1559 | if (index > maxindex) | 
| 1418 | return 0; | 1560 | return 0; | 
| 1419 | if (node == NULL) | ||
| 1420 | return 0; | ||
| 1421 | 1561 | ||
| 1422 | while (radix_tree_is_internal_node(node)) { | 1562 | while (radix_tree_is_internal_node(node)) { | 
| 1423 | unsigned offset; | 1563 | unsigned offset; | 
| @@ -1425,8 +1565,6 @@ int radix_tree_tag_get(struct radix_tree_root *root, | |||
| 1425 | parent = entry_to_node(node); | 1565 | parent = entry_to_node(node); | 
| 1426 | offset = radix_tree_descend(parent, &node, index); | 1566 | offset = radix_tree_descend(parent, &node, index); | 
| 1427 | 1567 | ||
| 1428 | if (!node) | ||
| 1429 | return 0; | ||
| 1430 | if (!tag_get(parent, tag, offset)) | 1568 | if (!tag_get(parent, tag, offset)) | 
| 1431 | return 0; | 1569 | return 0; | 
| 1432 | if (node == RADIX_TREE_RETRY) | 1570 | if (node == RADIX_TREE_RETRY) | 
| @@ -1453,6 +1591,11 @@ static void set_iter_tags(struct radix_tree_iter *iter, | |||
| 1453 | unsigned tag_long = offset / BITS_PER_LONG; | 1591 | unsigned tag_long = offset / BITS_PER_LONG; | 
| 1454 | unsigned tag_bit = offset % BITS_PER_LONG; | 1592 | unsigned tag_bit = offset % BITS_PER_LONG; | 
| 1455 | 1593 | ||
| 1594 | if (!node) { | ||
| 1595 | iter->tags = 1; | ||
| 1596 | return; | ||
| 1597 | } | ||
| 1598 | |||
| 1456 | iter->tags = node->tags[tag][tag_long] >> tag_bit; | 1599 | iter->tags = node->tags[tag][tag_long] >> tag_bit; | 
| 1457 | 1600 | ||
| 1458 | /* This never happens if RADIX_TREE_TAG_LONGS == 1 */ | 1601 | /* This never happens if RADIX_TREE_TAG_LONGS == 1 */ | 
| @@ -1467,8 +1610,8 @@ static void set_iter_tags(struct radix_tree_iter *iter, | |||
| 1467 | } | 1610 | } | 
| 1468 | 1611 | ||
| 1469 | #ifdef CONFIG_RADIX_TREE_MULTIORDER | 1612 | #ifdef CONFIG_RADIX_TREE_MULTIORDER | 
| 1470 | static void **skip_siblings(struct radix_tree_node **nodep, | 1613 | static void __rcu **skip_siblings(struct radix_tree_node **nodep, | 
| 1471 | void **slot, struct radix_tree_iter *iter) | 1614 | void __rcu **slot, struct radix_tree_iter *iter) | 
| 1472 | { | 1615 | { | 
| 1473 | void *sib = node_to_entry(slot - 1); | 1616 | void *sib = node_to_entry(slot - 1); | 
| 1474 | 1617 | ||
| @@ -1485,8 +1628,8 @@ static void **skip_siblings(struct radix_tree_node **nodep, | |||
| 1485 | return NULL; | 1628 | return NULL; | 
| 1486 | } | 1629 | } | 
| 1487 | 1630 | ||
| 1488 | void ** __radix_tree_next_slot(void **slot, struct radix_tree_iter *iter, | 1631 | void __rcu **__radix_tree_next_slot(void __rcu **slot, | 
| 1489 | unsigned flags) | 1632 | struct radix_tree_iter *iter, unsigned flags) | 
| 1490 | { | 1633 | { | 
| 1491 | unsigned tag = flags & RADIX_TREE_ITER_TAG_MASK; | 1634 | unsigned tag = flags & RADIX_TREE_ITER_TAG_MASK; | 
| 1492 | struct radix_tree_node *node = rcu_dereference_raw(*slot); | 1635 | struct radix_tree_node *node = rcu_dereference_raw(*slot); | 
| @@ -1539,20 +1682,20 @@ void ** __radix_tree_next_slot(void **slot, struct radix_tree_iter *iter, | |||
| 1539 | } | 1682 | } | 
| 1540 | EXPORT_SYMBOL(__radix_tree_next_slot); | 1683 | EXPORT_SYMBOL(__radix_tree_next_slot); | 
| 1541 | #else | 1684 | #else | 
| 1542 | static void **skip_siblings(struct radix_tree_node **nodep, | 1685 | static void __rcu **skip_siblings(struct radix_tree_node **nodep, | 
| 1543 | void **slot, struct radix_tree_iter *iter) | 1686 | void __rcu **slot, struct radix_tree_iter *iter) | 
| 1544 | { | 1687 | { | 
| 1545 | return slot; | 1688 | return slot; | 
| 1546 | } | 1689 | } | 
| 1547 | #endif | 1690 | #endif | 
| 1548 | 1691 | ||
| 1549 | void **radix_tree_iter_resume(void **slot, struct radix_tree_iter *iter) | 1692 | void __rcu **radix_tree_iter_resume(void __rcu **slot, | 
| 1693 | struct radix_tree_iter *iter) | ||
| 1550 | { | 1694 | { | 
| 1551 | struct radix_tree_node *node; | 1695 | struct radix_tree_node *node; | 
| 1552 | 1696 | ||
| 1553 | slot++; | 1697 | slot++; | 
| 1554 | iter->index = __radix_tree_iter_add(iter, 1); | 1698 | iter->index = __radix_tree_iter_add(iter, 1); | 
| 1555 | node = rcu_dereference_raw(*slot); | ||
| 1556 | skip_siblings(&node, slot, iter); | 1699 | skip_siblings(&node, slot, iter); | 
| 1557 | iter->next_index = iter->index; | 1700 | iter->next_index = iter->index; | 
| 1558 | iter->tags = 0; | 1701 | iter->tags = 0; | 
| @@ -1568,7 +1711,7 @@ EXPORT_SYMBOL(radix_tree_iter_resume); | |||
| 1568 | * @flags: RADIX_TREE_ITER_* flags and tag index | 1711 | * @flags: RADIX_TREE_ITER_* flags and tag index | 
| 1569 | * Returns: pointer to chunk first slot, or NULL if iteration is over | 1712 | * Returns: pointer to chunk first slot, or NULL if iteration is over | 
| 1570 | */ | 1713 | */ | 
| 1571 | void **radix_tree_next_chunk(struct radix_tree_root *root, | 1714 | void __rcu **radix_tree_next_chunk(const struct radix_tree_root *root, | 
| 1572 | struct radix_tree_iter *iter, unsigned flags) | 1715 | struct radix_tree_iter *iter, unsigned flags) | 
| 1573 | { | 1716 | { | 
| 1574 | unsigned tag = flags & RADIX_TREE_ITER_TAG_MASK; | 1717 | unsigned tag = flags & RADIX_TREE_ITER_TAG_MASK; | 
| @@ -1605,7 +1748,7 @@ void **radix_tree_next_chunk(struct radix_tree_root *root, | |||
| 1605 | iter->tags = 1; | 1748 | iter->tags = 1; | 
| 1606 | iter->node = NULL; | 1749 | iter->node = NULL; | 
| 1607 | __set_iter_shift(iter, 0); | 1750 | __set_iter_shift(iter, 0); | 
| 1608 | return (void **)&root->rnode; | 1751 | return (void __rcu **)&root->rnode; | 
| 1609 | } | 1752 | } | 
| 1610 | 1753 | ||
| 1611 | do { | 1754 | do { | 
| @@ -1623,7 +1766,8 @@ void **radix_tree_next_chunk(struct radix_tree_root *root, | |||
| 1623 | offset + 1); | 1766 | offset + 1); | 
| 1624 | else | 1767 | else | 
| 1625 | while (++offset < RADIX_TREE_MAP_SIZE) { | 1768 | while (++offset < RADIX_TREE_MAP_SIZE) { | 
| 1626 | void *slot = node->slots[offset]; | 1769 | void *slot = rcu_dereference_raw( | 
| 1770 | node->slots[offset]); | ||
| 1627 | if (is_sibling_entry(node, slot)) | 1771 | if (is_sibling_entry(node, slot)) | 
| 1628 | continue; | 1772 | continue; | 
| 1629 | if (slot) | 1773 | if (slot) | 
| @@ -1679,11 +1823,11 @@ EXPORT_SYMBOL(radix_tree_next_chunk); | |||
| 1679 | * stored in 'results'. | 1823 | * stored in 'results'. | 
| 1680 | */ | 1824 | */ | 
| 1681 | unsigned int | 1825 | unsigned int | 
| 1682 | radix_tree_gang_lookup(struct radix_tree_root *root, void **results, | 1826 | radix_tree_gang_lookup(const struct radix_tree_root *root, void **results, | 
| 1683 | unsigned long first_index, unsigned int max_items) | 1827 | unsigned long first_index, unsigned int max_items) | 
| 1684 | { | 1828 | { | 
| 1685 | struct radix_tree_iter iter; | 1829 | struct radix_tree_iter iter; | 
| 1686 | void **slot; | 1830 | void __rcu **slot; | 
| 1687 | unsigned int ret = 0; | 1831 | unsigned int ret = 0; | 
| 1688 | 1832 | ||
| 1689 | if (unlikely(!max_items)) | 1833 | if (unlikely(!max_items)) | 
| @@ -1724,12 +1868,12 @@ EXPORT_SYMBOL(radix_tree_gang_lookup); | |||
| 1724 | * protection, radix_tree_deref_slot may fail requiring a retry. | 1868 | * protection, radix_tree_deref_slot may fail requiring a retry. | 
| 1725 | */ | 1869 | */ | 
| 1726 | unsigned int | 1870 | unsigned int | 
| 1727 | radix_tree_gang_lookup_slot(struct radix_tree_root *root, | 1871 | radix_tree_gang_lookup_slot(const struct radix_tree_root *root, | 
| 1728 | void ***results, unsigned long *indices, | 1872 | void __rcu ***results, unsigned long *indices, | 
| 1729 | unsigned long first_index, unsigned int max_items) | 1873 | unsigned long first_index, unsigned int max_items) | 
| 1730 | { | 1874 | { | 
| 1731 | struct radix_tree_iter iter; | 1875 | struct radix_tree_iter iter; | 
| 1732 | void **slot; | 1876 | void __rcu **slot; | 
| 1733 | unsigned int ret = 0; | 1877 | unsigned int ret = 0; | 
| 1734 | 1878 | ||
| 1735 | if (unlikely(!max_items)) | 1879 | if (unlikely(!max_items)) | 
| @@ -1761,12 +1905,12 @@ EXPORT_SYMBOL(radix_tree_gang_lookup_slot); | |||
| 1761 | * returns the number of items which were placed at *@results. | 1905 | * returns the number of items which were placed at *@results. | 
| 1762 | */ | 1906 | */ | 
| 1763 | unsigned int | 1907 | unsigned int | 
| 1764 | radix_tree_gang_lookup_tag(struct radix_tree_root *root, void **results, | 1908 | radix_tree_gang_lookup_tag(const struct radix_tree_root *root, void **results, | 
| 1765 | unsigned long first_index, unsigned int max_items, | 1909 | unsigned long first_index, unsigned int max_items, | 
| 1766 | unsigned int tag) | 1910 | unsigned int tag) | 
| 1767 | { | 1911 | { | 
| 1768 | struct radix_tree_iter iter; | 1912 | struct radix_tree_iter iter; | 
| 1769 | void **slot; | 1913 | void __rcu **slot; | 
| 1770 | unsigned int ret = 0; | 1914 | unsigned int ret = 0; | 
| 1771 | 1915 | ||
| 1772 | if (unlikely(!max_items)) | 1916 | if (unlikely(!max_items)) | 
| @@ -1802,12 +1946,12 @@ EXPORT_SYMBOL(radix_tree_gang_lookup_tag); | |||
| 1802 | * returns the number of slots which were placed at *@results. | 1946 | * returns the number of slots which were placed at *@results. | 
| 1803 | */ | 1947 | */ | 
| 1804 | unsigned int | 1948 | unsigned int | 
| 1805 | radix_tree_gang_lookup_tag_slot(struct radix_tree_root *root, void ***results, | 1949 | radix_tree_gang_lookup_tag_slot(const struct radix_tree_root *root, | 
| 1806 | unsigned long first_index, unsigned int max_items, | 1950 | void __rcu ***results, unsigned long first_index, | 
| 1807 | unsigned int tag) | 1951 | unsigned int max_items, unsigned int tag) | 
| 1808 | { | 1952 | { | 
| 1809 | struct radix_tree_iter iter; | 1953 | struct radix_tree_iter iter; | 
| 1810 | void **slot; | 1954 | void __rcu **slot; | 
| 1811 | unsigned int ret = 0; | 1955 | unsigned int ret = 0; | 
| 1812 | 1956 | ||
| 1813 | if (unlikely(!max_items)) | 1957 | if (unlikely(!max_items)) | 
| @@ -1842,59 +1986,83 @@ void __radix_tree_delete_node(struct radix_tree_root *root, | |||
| 1842 | delete_node(root, node, update_node, private); | 1986 | delete_node(root, node, update_node, private); | 
| 1843 | } | 1987 | } | 
| 1844 | 1988 | ||
| 1989 | static bool __radix_tree_delete(struct radix_tree_root *root, | ||
| 1990 | struct radix_tree_node *node, void __rcu **slot) | ||
| 1991 | { | ||
| 1992 | void *old = rcu_dereference_raw(*slot); | ||
| 1993 | int exceptional = radix_tree_exceptional_entry(old) ? -1 : 0; | ||
| 1994 | unsigned offset = get_slot_offset(node, slot); | ||
| 1995 | int tag; | ||
| 1996 | |||
| 1997 | if (is_idr(root)) | ||
| 1998 | node_tag_set(root, node, IDR_FREE, offset); | ||
| 1999 | else | ||
| 2000 | for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) | ||
| 2001 | node_tag_clear(root, node, tag, offset); | ||
| 2002 | |||
| 2003 | replace_slot(slot, NULL, node, -1, exceptional); | ||
| 2004 | return node && delete_node(root, node, NULL, NULL); | ||
| 2005 | } | ||
| 2006 | |||
| 1845 | /** | 2007 | /** | 
| 1846 | * radix_tree_delete_item - delete an item from a radix tree | 2008 | * radix_tree_iter_delete - delete the entry at this iterator position | 
| 1847 | * @root: radix tree root | 2009 | * @root: radix tree root | 
| 1848 | * @index: index key | 2010 | * @iter: iterator state | 
| 1849 | * @item: expected item | 2011 | * @slot: pointer to slot | 
| 1850 | * | 2012 | * | 
| 1851 | * Remove @item at @index from the radix tree rooted at @root. | 2013 | * Delete the entry at the position currently pointed to by the iterator. | 
| 2014 | * This may result in the current node being freed; if it is, the iterator | ||
| 2015 | * is advanced so that it will not reference the freed memory. This | ||
| 2016 | * function may be called without any locking if there are no other threads | ||
| 2017 | * which can access this tree. | ||
| 2018 | */ | ||
| 2019 | void radix_tree_iter_delete(struct radix_tree_root *root, | ||
| 2020 | struct radix_tree_iter *iter, void __rcu **slot) | ||
| 2021 | { | ||
| 2022 | if (__radix_tree_delete(root, iter->node, slot)) | ||
| 2023 | iter->index = iter->next_index; | ||
| 2024 | } | ||
| 2025 | |||
| 2026 | /** | ||
| 2027 | * radix_tree_delete_item - delete an item from a radix tree | ||
| 2028 | * @root: radix tree root | ||
| 2029 | * @index: index key | ||
| 2030 | * @item: expected item | ||
| 1852 | * | 2031 | * | 
| 1853 | * Returns the address of the deleted item, or NULL if it was not present | 2032 | * Remove @item at @index from the radix tree rooted at @root. | 
| 1854 | * or the entry at the given @index was not @item. | 2033 | * | 
| 2034 | * Return: the deleted entry, or %NULL if it was not present | ||
| 2035 | * or the entry at the given @index was not @item. | ||
| 1855 | */ | 2036 | */ | 
| 1856 | void *radix_tree_delete_item(struct radix_tree_root *root, | 2037 | void *radix_tree_delete_item(struct radix_tree_root *root, | 
| 1857 | unsigned long index, void *item) | 2038 | unsigned long index, void *item) | 
| 1858 | { | 2039 | { | 
| 1859 | struct radix_tree_node *node; | 2040 | struct radix_tree_node *node = NULL; | 
| 1860 | unsigned int offset; | 2041 | void __rcu **slot; | 
| 1861 | void **slot; | ||
| 1862 | void *entry; | 2042 | void *entry; | 
| 1863 | int tag; | ||
| 1864 | 2043 | ||
| 1865 | entry = __radix_tree_lookup(root, index, &node, &slot); | 2044 | entry = __radix_tree_lookup(root, index, &node, &slot); | 
| 1866 | if (!entry) | 2045 | if (!entry && (!is_idr(root) || node_tag_get(root, node, IDR_FREE, | 
| 2046 | get_slot_offset(node, slot)))) | ||
| 1867 | return NULL; | 2047 | return NULL; | 
| 1868 | 2048 | ||
| 1869 | if (item && entry != item) | 2049 | if (item && entry != item) | 
| 1870 | return NULL; | 2050 | return NULL; | 
| 1871 | 2051 | ||
| 1872 | if (!node) { | 2052 | __radix_tree_delete(root, node, slot); | 
| 1873 | root_tag_clear_all(root); | ||
| 1874 | root->rnode = NULL; | ||
| 1875 | return entry; | ||
| 1876 | } | ||
| 1877 | |||
| 1878 | offset = get_slot_offset(node, slot); | ||
| 1879 | |||
| 1880 | /* Clear all tags associated with the item to be deleted. */ | ||
| 1881 | for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) | ||
| 1882 | node_tag_clear(root, node, tag, offset); | ||
| 1883 | |||
| 1884 | __radix_tree_replace(root, node, slot, NULL, NULL, NULL); | ||
| 1885 | 2053 | ||
| 1886 | return entry; | 2054 | return entry; | 
| 1887 | } | 2055 | } | 
| 1888 | EXPORT_SYMBOL(radix_tree_delete_item); | 2056 | EXPORT_SYMBOL(radix_tree_delete_item); | 
| 1889 | 2057 | ||
| 1890 | /** | 2058 | /** | 
| 1891 | * radix_tree_delete - delete an item from a radix tree | 2059 | * radix_tree_delete - delete an entry from a radix tree | 
| 1892 | * @root: radix tree root | 2060 | * @root: radix tree root | 
| 1893 | * @index: index key | 2061 | * @index: index key | 
| 1894 | * | 2062 | * | 
| 1895 | * Remove the item at @index from the radix tree rooted at @root. | 2063 | * Remove the entry at @index from the radix tree rooted at @root. | 
| 1896 | * | 2064 | * | 
| 1897 | * Returns the address of the deleted item, or NULL if it was not present. | 2065 | * Return: The deleted entry, or %NULL if it was not present. | 
| 1898 | */ | 2066 | */ | 
| 1899 | void *radix_tree_delete(struct radix_tree_root *root, unsigned long index) | 2067 | void *radix_tree_delete(struct radix_tree_root *root, unsigned long index) | 
| 1900 | { | 2068 | { | 
| @@ -1904,15 +2072,14 @@ EXPORT_SYMBOL(radix_tree_delete); | |||
| 1904 | 2072 | ||
| 1905 | void radix_tree_clear_tags(struct radix_tree_root *root, | 2073 | void radix_tree_clear_tags(struct radix_tree_root *root, | 
| 1906 | struct radix_tree_node *node, | 2074 | struct radix_tree_node *node, | 
| 1907 | void **slot) | 2075 | void __rcu **slot) | 
| 1908 | { | 2076 | { | 
| 1909 | if (node) { | 2077 | if (node) { | 
| 1910 | unsigned int tag, offset = get_slot_offset(node, slot); | 2078 | unsigned int tag, offset = get_slot_offset(node, slot); | 
| 1911 | for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) | 2079 | for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) | 
| 1912 | node_tag_clear(root, node, tag, offset); | 2080 | node_tag_clear(root, node, tag, offset); | 
| 1913 | } else { | 2081 | } else { | 
| 1914 | /* Clear root node tags */ | 2082 | root_tag_clear_all(root); | 
| 1915 | root->gfp_mask &= __GFP_BITS_MASK; | ||
| 1916 | } | 2083 | } | 
| 1917 | } | 2084 | } | 
| 1918 | 2085 | ||
| @@ -1921,12 +2088,147 @@ void radix_tree_clear_tags(struct radix_tree_root *root, | |||
| 1921 | * @root: radix tree root | 2088 | * @root: radix tree root | 
| 1922 | * @tag: tag to test | 2089 | * @tag: tag to test | 
| 1923 | */ | 2090 | */ | 
| 1924 | int radix_tree_tagged(struct radix_tree_root *root, unsigned int tag) | 2091 | int radix_tree_tagged(const struct radix_tree_root *root, unsigned int tag) | 
| 1925 | { | 2092 | { | 
| 1926 | return root_tag_get(root, tag); | 2093 | return root_tag_get(root, tag); | 
| 1927 | } | 2094 | } | 
| 1928 | EXPORT_SYMBOL(radix_tree_tagged); | 2095 | EXPORT_SYMBOL(radix_tree_tagged); | 
| 1929 | 2096 | ||
| 2097 | /** | ||
| 2098 | * idr_preload - preload for idr_alloc() | ||
| 2099 | * @gfp_mask: allocation mask to use for preloading | ||
| 2100 | * | ||
| 2101 | * Preallocate memory to use for the next call to idr_alloc(). This function | ||
| 2102 | * returns with preemption disabled. It will be enabled by idr_preload_end(). | ||
| 2103 | */ | ||
| 2104 | void idr_preload(gfp_t gfp_mask) | ||
| 2105 | { | ||
| 2106 | __radix_tree_preload(gfp_mask, IDR_PRELOAD_SIZE); | ||
| 2107 | } | ||
| 2108 | EXPORT_SYMBOL(idr_preload); | ||
| 2109 | |||
| 2110 | /** | ||
| 2111 | * ida_pre_get - reserve resources for ida allocation | ||
| 2112 | * @ida: ida handle | ||
| 2113 | * @gfp: memory allocation flags | ||
| 2114 | * | ||
| 2115 | * This function should be called before calling ida_get_new_above(). If it | ||
| 2116 | * is unable to allocate memory, it will return %0. On success, it returns %1. | ||
| 2117 | */ | ||
| 2118 | int ida_pre_get(struct ida *ida, gfp_t gfp) | ||
| 2119 | { | ||
| 2120 | __radix_tree_preload(gfp, IDA_PRELOAD_SIZE); | ||
| 2121 | /* | ||
| 2122 | * The IDA API has no preload_end() equivalent. Instead, | ||
| 2123 | * ida_get_new() can return -EAGAIN, prompting the caller | ||
| 2124 | * to return to the ida_pre_get() step. | ||
| 2125 | */ | ||
| 2126 | preempt_enable(); | ||
| 2127 | |||
| 2128 | if (!this_cpu_read(ida_bitmap)) { | ||
| 2129 | struct ida_bitmap *bitmap = kmalloc(sizeof(*bitmap), gfp); | ||
| 2130 | if (!bitmap) | ||
| 2131 | return 0; | ||
| 2132 | if (this_cpu_cmpxchg(ida_bitmap, NULL, bitmap)) | ||
| 2133 | kfree(bitmap); | ||
| 2134 | } | ||
| 2135 | |||
| 2136 | return 1; | ||
| 2137 | } | ||
| 2138 | EXPORT_SYMBOL(ida_pre_get); | ||
| 2139 | |||
| 2140 | void __rcu **idr_get_free(struct radix_tree_root *root, | ||
| 2141 | struct radix_tree_iter *iter, gfp_t gfp, int end) | ||
| 2142 | { | ||
| 2143 | struct radix_tree_node *node = NULL, *child; | ||
| 2144 | void __rcu **slot = (void __rcu **)&root->rnode; | ||
| 2145 | unsigned long maxindex, start = iter->next_index; | ||
| 2146 | unsigned long max = end > 0 ? end - 1 : INT_MAX; | ||
| 2147 | unsigned int shift, offset = 0; | ||
| 2148 | |||
| 2149 | grow: | ||
| 2150 | shift = radix_tree_load_root(root, &child, &maxindex); | ||
| 2151 | if (!radix_tree_tagged(root, IDR_FREE)) | ||
| 2152 | start = max(start, maxindex + 1); | ||
| 2153 | if (start > max) | ||
| 2154 | return ERR_PTR(-ENOSPC); | ||
| 2155 | |||
| 2156 | if (start > maxindex) { | ||
| 2157 | int error = radix_tree_extend(root, gfp, start, shift); | ||
| 2158 | if (error < 0) | ||
| 2159 | return ERR_PTR(error); | ||
| 2160 | shift = error; | ||
| 2161 | child = rcu_dereference_raw(root->rnode); | ||
| 2162 | } | ||
| 2163 | |||
| 2164 | while (shift) { | ||
| 2165 | shift -= RADIX_TREE_MAP_SHIFT; | ||
| 2166 | if (child == NULL) { | ||
| 2167 | /* Have to add a child node. */ | ||
| 2168 | child = radix_tree_node_alloc(gfp, node, root, shift, | ||
| 2169 | offset, 0, 0); | ||
| 2170 | if (!child) | ||
| 2171 | return ERR_PTR(-ENOMEM); | ||
| 2172 | all_tag_set(child, IDR_FREE); | ||
| 2173 | rcu_assign_pointer(*slot, node_to_entry(child)); | ||
| 2174 | if (node) | ||
| 2175 | node->count++; | ||
| 2176 | } else if (!radix_tree_is_internal_node(child)) | ||
| 2177 | break; | ||
| 2178 | |||
| 2179 | node = entry_to_node(child); | ||
| 2180 | offset = radix_tree_descend(node, &child, start); | ||
| 2181 | if (!tag_get(node, IDR_FREE, offset)) { | ||
| 2182 | offset = radix_tree_find_next_bit(node, IDR_FREE, | ||
| 2183 | offset + 1); | ||
| 2184 | start = next_index(start, node, offset); | ||
| 2185 | if (start > max) | ||
| 2186 | return ERR_PTR(-ENOSPC); | ||
| 2187 | while (offset == RADIX_TREE_MAP_SIZE) { | ||
| 2188 | offset = node->offset + 1; | ||
| 2189 | node = node->parent; | ||
| 2190 | if (!node) | ||
| 2191 | goto grow; | ||
| 2192 | shift = node->shift; | ||
| 2193 | } | ||
| 2194 | child = rcu_dereference_raw(node->slots[offset]); | ||
| 2195 | } | ||
| 2196 | slot = &node->slots[offset]; | ||
| 2197 | } | ||
| 2198 | |||
| 2199 | iter->index = start; | ||
| 2200 | if (node) | ||
| 2201 | iter->next_index = 1 + min(max, (start | node_maxindex(node))); | ||
| 2202 | else | ||
| 2203 | iter->next_index = 1; | ||
| 2204 | iter->node = node; | ||
| 2205 | __set_iter_shift(iter, shift); | ||
| 2206 | set_iter_tags(iter, node, offset, IDR_FREE); | ||
| 2207 | |||
| 2208 | return slot; | ||
| 2209 | } | ||
| 2210 | |||
| 2211 | /** | ||
| 2212 | * idr_destroy - release all internal memory from an IDR | ||
| 2213 | * @idr: idr handle | ||
| 2214 | * | ||
| 2215 | * After this function is called, the IDR is empty, and may be reused or | ||
| 2216 | * the data structure containing it may be freed. | ||
| 2217 | * | ||
| 2218 | * A typical clean-up sequence for objects stored in an idr tree will use | ||
| 2219 | * idr_for_each() to free all objects, if necessary, then idr_destroy() to | ||
| 2220 | * free the memory used to keep track of those objects. | ||
| 2221 | */ | ||
| 2222 | void idr_destroy(struct idr *idr) | ||
| 2223 | { | ||
| 2224 | struct radix_tree_node *node = rcu_dereference_raw(idr->idr_rt.rnode); | ||
| 2225 | if (radix_tree_is_internal_node(node)) | ||
| 2226 | radix_tree_free_nodes(node); | ||
| 2227 | idr->idr_rt.rnode = NULL; | ||
| 2228 | root_tag_set(&idr->idr_rt, IDR_FREE); | ||
| 2229 | } | ||
| 2230 | EXPORT_SYMBOL(idr_destroy); | ||
| 2231 | |||
| 1930 | static void | 2232 | static void | 
| 1931 | radix_tree_node_ctor(void *arg) | 2233 | radix_tree_node_ctor(void *arg) | 
| 1932 | { | 2234 | { | 
| @@ -1970,10 +2272,12 @@ static int radix_tree_cpu_dead(unsigned int cpu) | |||
| 1970 | rtp = &per_cpu(radix_tree_preloads, cpu); | 2272 | rtp = &per_cpu(radix_tree_preloads, cpu); | 
| 1971 | while (rtp->nr) { | 2273 | while (rtp->nr) { | 
| 1972 | node = rtp->nodes; | 2274 | node = rtp->nodes; | 
| 1973 | rtp->nodes = node->private_data; | 2275 | rtp->nodes = node->parent; | 
| 1974 | kmem_cache_free(radix_tree_node_cachep, node); | 2276 | kmem_cache_free(radix_tree_node_cachep, node); | 
| 1975 | rtp->nr--; | 2277 | rtp->nr--; | 
| 1976 | } | 2278 | } | 
| 2279 | kfree(per_cpu(ida_bitmap, cpu)); | ||
| 2280 | per_cpu(ida_bitmap, cpu) = NULL; | ||
| 1977 | return 0; | 2281 | return 0; | 
| 1978 | } | 2282 | } | 
| 1979 | 2283 | ||
