Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Copyright (C) 2001 Momchil Velikov | |
3 | * Portions Copyright (C) 2001 Christoph Hellwig | |
cde53535 | 4 | * Copyright (C) 2005 SGI, Christoph Lameter |
7cf9c2c7 | 5 | * Copyright (C) 2006 Nick Piggin |
78c1d784 | 6 | * Copyright (C) 2012 Konstantin Khlebnikov |
6b053b8e MW |
7 | * Copyright (C) 2016 Intel, Matthew Wilcox |
8 | * Copyright (C) 2016 Intel, Ross Zwisler | |
1da177e4 LT |
9 | * |
10 | * This program is free software; you can redistribute it and/or | |
11 | * modify it under the terms of the GNU General Public License as | |
12 | * published by the Free Software Foundation; either version 2, or (at | |
13 | * your option) any later version. | |
14 | * | |
15 | * This program is distributed in the hope that it will be useful, but | |
16 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
18 | * General Public License for more details. | |
19 | * | |
20 | * You should have received a copy of the GNU General Public License | |
21 | * along with this program; if not, write to the Free Software | |
22 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
23 | */ | |
24 | ||
0a835c4f MW |
25 | #include <linux/bitmap.h> |
26 | #include <linux/bitops.h> | |
460488c5 | 27 | #include <linux/bug.h> |
e157b555 | 28 | #include <linux/cpu.h> |
1da177e4 | 29 | #include <linux/errno.h> |
0a835c4f MW |
30 | #include <linux/export.h> |
31 | #include <linux/idr.h> | |
1da177e4 LT |
32 | #include <linux/init.h> |
33 | #include <linux/kernel.h> | |
0a835c4f | 34 | #include <linux/kmemleak.h> |
1da177e4 | 35 | #include <linux/percpu.h> |
0a835c4f MW |
36 | #include <linux/preempt.h> /* in_interrupt() */ |
37 | #include <linux/radix-tree.h> | |
38 | #include <linux/rcupdate.h> | |
1da177e4 | 39 | #include <linux/slab.h> |
1da177e4 | 40 | #include <linux/string.h> |
02c02bf1 | 41 | #include <linux/xarray.h> |
1da177e4 LT |
42 | |
43 | ||
c78c66d1 KS |
44 | /* Number of nodes in fully populated tree of given height */ |
45 | static unsigned long height_to_maxnodes[RADIX_TREE_MAX_PATH + 1] __read_mostly; | |
46 | ||
1da177e4 LT |
47 | /* |
48 | * Radix tree node cache. | |
49 | */ | |
58d6ea30 | 50 | struct kmem_cache *radix_tree_node_cachep; |
1da177e4 | 51 | |
55368052 NP |
52 | /* |
53 | * The radix tree is variable-height, so an insert operation not only has | |
54 | * to build the branch to its corresponding item, it also has to build the | |
55 | * branch to existing items if the size has to be increased (by | |
56 | * radix_tree_extend). | |
57 | * | |
58 | * The worst case is a zero height tree with just a single item at index 0, | |
59 | * and then inserting an item at index ULONG_MAX. This requires 2 new branches | |
60 | * of RADIX_TREE_MAX_PATH size to be created, with only the root node shared. | |
61 | * Hence: | |
62 | */ | |
63 | #define RADIX_TREE_PRELOAD_SIZE (RADIX_TREE_MAX_PATH * 2 - 1) | |
64 | ||
0a835c4f MW |
65 | /* |
66 | * The IDR does not have to be as high as the radix tree since it uses | |
67 | * signed integers, not unsigned longs. | |
68 | */ | |
69 | #define IDR_INDEX_BITS (8 /* CHAR_BIT */ * sizeof(int) - 1) | |
70 | #define IDR_MAX_PATH (DIV_ROUND_UP(IDR_INDEX_BITS, \ | |
71 | RADIX_TREE_MAP_SHIFT)) | |
72 | #define IDR_PRELOAD_SIZE (IDR_MAX_PATH * 2 - 1) | |
73 | ||
7ad3d4d8 MW |
74 | /* |
75 | * The IDA is even shorter since it uses a bitmap at the last level. | |
76 | */ | |
77 | #define IDA_INDEX_BITS (8 * sizeof(int) - 1 - ilog2(IDA_BITMAP_BITS)) | |
78 | #define IDA_MAX_PATH (DIV_ROUND_UP(IDA_INDEX_BITS, \ | |
79 | RADIX_TREE_MAP_SHIFT)) | |
80 | #define IDA_PRELOAD_SIZE (IDA_MAX_PATH * 2 - 1) | |
81 | ||
1da177e4 LT |
82 | /* |
83 | * Per-cpu pool of preloaded nodes | |
84 | */ | |
85 | struct radix_tree_preload { | |
2fcd9005 | 86 | unsigned nr; |
1293d5c5 | 87 | /* nodes->parent points to next preallocated node */ |
9d2a8da0 | 88 | struct radix_tree_node *nodes; |
1da177e4 | 89 | }; |
8cef7d57 | 90 | static DEFINE_PER_CPU(struct radix_tree_preload, radix_tree_preloads) = { 0, }; |
1da177e4 | 91 | |
148deab2 MW |
92 | static inline struct radix_tree_node *entry_to_node(void *ptr) |
93 | { | |
94 | return (void *)((unsigned long)ptr & ~RADIX_TREE_INTERNAL_NODE); | |
95 | } | |
96 | ||
a4db4dce | 97 | static inline void *node_to_entry(void *ptr) |
27d20fdd | 98 | { |
30ff46cc | 99 | return (void *)((unsigned long)ptr | RADIX_TREE_INTERNAL_NODE); |
27d20fdd NP |
100 | } |
101 | ||
02c02bf1 | 102 | #define RADIX_TREE_RETRY XA_RETRY_ENTRY |
db050f29 | 103 | |
d7b62727 MW |
104 | static inline unsigned long |
105 | get_slot_offset(const struct radix_tree_node *parent, void __rcu **slot) | |
db050f29 | 106 | { |
76f070b4 | 107 | return parent ? slot - parent->slots : 0; |
db050f29 MW |
108 | } |
109 | ||
35534c86 | 110 | static unsigned int radix_tree_descend(const struct radix_tree_node *parent, |
9e85d811 | 111 | struct radix_tree_node **nodep, unsigned long index) |
db050f29 | 112 | { |
9e85d811 | 113 | unsigned int offset = (index >> parent->shift) & RADIX_TREE_MAP_MASK; |
d7b62727 | 114 | void __rcu **entry = rcu_dereference_raw(parent->slots[offset]); |
db050f29 | 115 | |
02c02bf1 MW |
116 | if (xa_is_sibling(entry)) { |
117 | offset = xa_to_sibling(entry); | |
118 | entry = rcu_dereference_raw(parent->slots[offset]); | |
db050f29 | 119 | } |
db050f29 MW |
120 | |
121 | *nodep = (void *)entry; | |
122 | return offset; | |
123 | } | |
124 | ||
35534c86 | 125 | static inline gfp_t root_gfp_mask(const struct radix_tree_root *root) |
612d6c19 | 126 | { |
f8d5d0cc | 127 | return root->xa_flags & (__GFP_BITS_MASK & ~GFP_ZONEMASK); |
612d6c19 NP |
128 | } |
129 | ||
643b52b9 NP |
130 | static inline void tag_set(struct radix_tree_node *node, unsigned int tag, |
131 | int offset) | |
132 | { | |
133 | __set_bit(offset, node->tags[tag]); | |
134 | } | |
135 | ||
136 | static inline void tag_clear(struct radix_tree_node *node, unsigned int tag, | |
137 | int offset) | |
138 | { | |
139 | __clear_bit(offset, node->tags[tag]); | |
140 | } | |
141 | ||
35534c86 | 142 | static inline int tag_get(const struct radix_tree_node *node, unsigned int tag, |
643b52b9 NP |
143 | int offset) |
144 | { | |
145 | return test_bit(offset, node->tags[tag]); | |
146 | } | |
147 | ||
35534c86 | 148 | static inline void root_tag_set(struct radix_tree_root *root, unsigned tag) |
643b52b9 | 149 | { |
f8d5d0cc | 150 | root->xa_flags |= (__force gfp_t)(1 << (tag + ROOT_TAG_SHIFT)); |
643b52b9 NP |
151 | } |
152 | ||
2fcd9005 | 153 | static inline void root_tag_clear(struct radix_tree_root *root, unsigned tag) |
643b52b9 | 154 | { |
f8d5d0cc | 155 | root->xa_flags &= (__force gfp_t)~(1 << (tag + ROOT_TAG_SHIFT)); |
643b52b9 NP |
156 | } |
157 | ||
158 | static inline void root_tag_clear_all(struct radix_tree_root *root) | |
159 | { | |
f8d5d0cc | 160 | root->xa_flags &= (__force gfp_t)((1 << ROOT_TAG_SHIFT) - 1); |
643b52b9 NP |
161 | } |
162 | ||
35534c86 | 163 | static inline int root_tag_get(const struct radix_tree_root *root, unsigned tag) |
643b52b9 | 164 | { |
f8d5d0cc | 165 | return (__force int)root->xa_flags & (1 << (tag + ROOT_TAG_SHIFT)); |
643b52b9 NP |
166 | } |
167 | ||
35534c86 | 168 | static inline unsigned root_tags_get(const struct radix_tree_root *root) |
643b52b9 | 169 | { |
f8d5d0cc | 170 | return (__force unsigned)root->xa_flags >> ROOT_TAG_SHIFT; |
643b52b9 NP |
171 | } |
172 | ||
0a835c4f | 173 | static inline bool is_idr(const struct radix_tree_root *root) |
7b60e9ad | 174 | { |
f8d5d0cc | 175 | return !!(root->xa_flags & ROOT_IS_IDR); |
7b60e9ad MW |
176 | } |
177 | ||
643b52b9 NP |
178 | /* |
179 | * Returns 1 if any slot in the node has this tag set. | |
180 | * Otherwise returns 0. | |
181 | */ | |
35534c86 MW |
182 | static inline int any_tag_set(const struct radix_tree_node *node, |
183 | unsigned int tag) | |
643b52b9 | 184 | { |
2fcd9005 | 185 | unsigned idx; |
643b52b9 NP |
186 | for (idx = 0; idx < RADIX_TREE_TAG_LONGS; idx++) { |
187 | if (node->tags[tag][idx]) | |
188 | return 1; | |
189 | } | |
190 | return 0; | |
191 | } | |
78c1d784 | 192 | |
0a835c4f MW |
193 | static inline void all_tag_set(struct radix_tree_node *node, unsigned int tag) |
194 | { | |
195 | bitmap_fill(node->tags[tag], RADIX_TREE_MAP_SIZE); | |
196 | } | |
197 | ||
78c1d784 KK |
198 | /** |
199 | * radix_tree_find_next_bit - find the next set bit in a memory region | |
200 | * | |
201 | * @addr: The address to base the search on | |
202 | * @size: The bitmap size in bits | |
203 | * @offset: The bitnumber to start searching at | |
204 | * | |
205 | * Unrollable variant of find_next_bit() for constant size arrays. | |
206 | * Tail bits starting from size to roundup(size, BITS_PER_LONG) must be zero. | |
207 | * Returns next bit offset, or size if nothing found. | |
208 | */ | |
209 | static __always_inline unsigned long | |
bc412fca MW |
210 | radix_tree_find_next_bit(struct radix_tree_node *node, unsigned int tag, |
211 | unsigned long offset) | |
78c1d784 | 212 | { |
bc412fca | 213 | const unsigned long *addr = node->tags[tag]; |
78c1d784 | 214 | |
bc412fca | 215 | if (offset < RADIX_TREE_MAP_SIZE) { |
78c1d784 KK |
216 | unsigned long tmp; |
217 | ||
218 | addr += offset / BITS_PER_LONG; | |
219 | tmp = *addr >> (offset % BITS_PER_LONG); | |
220 | if (tmp) | |
221 | return __ffs(tmp) + offset; | |
222 | offset = (offset + BITS_PER_LONG) & ~(BITS_PER_LONG - 1); | |
bc412fca | 223 | while (offset < RADIX_TREE_MAP_SIZE) { |
78c1d784 KK |
224 | tmp = *++addr; |
225 | if (tmp) | |
226 | return __ffs(tmp) + offset; | |
227 | offset += BITS_PER_LONG; | |
228 | } | |
229 | } | |
bc412fca | 230 | return RADIX_TREE_MAP_SIZE; |
78c1d784 KK |
231 | } |
232 | ||
268f42de MW |
233 | static unsigned int iter_offset(const struct radix_tree_iter *iter) |
234 | { | |
235 | return (iter->index >> iter_shift(iter)) & RADIX_TREE_MAP_MASK; | |
236 | } | |
237 | ||
218ed750 MW |
238 | /* |
239 | * The maximum index which can be stored in a radix tree | |
240 | */ | |
241 | static inline unsigned long shift_maxindex(unsigned int shift) | |
242 | { | |
243 | return (RADIX_TREE_MAP_SIZE << shift) - 1; | |
244 | } | |
245 | ||
35534c86 | 246 | static inline unsigned long node_maxindex(const struct radix_tree_node *node) |
218ed750 MW |
247 | { |
248 | return shift_maxindex(node->shift); | |
249 | } | |
250 | ||
0a835c4f MW |
251 | static unsigned long next_index(unsigned long index, |
252 | const struct radix_tree_node *node, | |
253 | unsigned long offset) | |
254 | { | |
255 | return (index & ~node_maxindex(node)) + (offset << node->shift); | |
256 | } | |
257 | ||
1da177e4 LT |
258 | /* |
259 | * This assumes that the caller has performed appropriate preallocation, and | |
260 | * that the caller has pinned this thread of control to the current CPU. | |
261 | */ | |
262 | static struct radix_tree_node * | |
0a835c4f | 263 | radix_tree_node_alloc(gfp_t gfp_mask, struct radix_tree_node *parent, |
d58275bc | 264 | struct radix_tree_root *root, |
e8de4340 | 265 | unsigned int shift, unsigned int offset, |
01959dfe | 266 | unsigned int count, unsigned int nr_values) |
1da177e4 | 267 | { |
e2848a0e | 268 | struct radix_tree_node *ret = NULL; |
1da177e4 | 269 | |
5e4c0d97 | 270 | /* |
2fcd9005 MW |
271 | * Preload code isn't irq safe and it doesn't make sense to use |
272 | * preloading during an interrupt anyway as all the allocations have | |
273 | * to be atomic. So just do normal allocation when in interrupt. | |
5e4c0d97 | 274 | */ |
d0164adc | 275 | if (!gfpflags_allow_blocking(gfp_mask) && !in_interrupt()) { |
1da177e4 LT |
276 | struct radix_tree_preload *rtp; |
277 | ||
58e698af VD |
278 | /* |
279 | * Even if the caller has preloaded, try to allocate from the | |
05eb6e72 VD |
280 | * cache first for the new node to get accounted to the memory |
281 | * cgroup. | |
58e698af VD |
282 | */ |
283 | ret = kmem_cache_alloc(radix_tree_node_cachep, | |
05eb6e72 | 284 | gfp_mask | __GFP_NOWARN); |
58e698af VD |
285 | if (ret) |
286 | goto out; | |
287 | ||
e2848a0e NP |
288 | /* |
289 | * Provided the caller has preloaded here, we will always | |
290 | * succeed in getting a node here (and never reach | |
291 | * kmem_cache_alloc) | |
292 | */ | |
7c8e0181 | 293 | rtp = this_cpu_ptr(&radix_tree_preloads); |
1da177e4 | 294 | if (rtp->nr) { |
9d2a8da0 | 295 | ret = rtp->nodes; |
1293d5c5 | 296 | rtp->nodes = ret->parent; |
1da177e4 LT |
297 | rtp->nr--; |
298 | } | |
ce80b067 CM |
299 | /* |
300 | * Update the allocation stack trace as this is more useful | |
301 | * for debugging. | |
302 | */ | |
303 | kmemleak_update_trace(ret); | |
58e698af | 304 | goto out; |
1da177e4 | 305 | } |
05eb6e72 | 306 | ret = kmem_cache_alloc(radix_tree_node_cachep, gfp_mask); |
58e698af | 307 | out: |
b194d16c | 308 | BUG_ON(radix_tree_is_internal_node(ret)); |
e8de4340 | 309 | if (ret) { |
e8de4340 MW |
310 | ret->shift = shift; |
311 | ret->offset = offset; | |
312 | ret->count = count; | |
01959dfe | 313 | ret->nr_values = nr_values; |
d58275bc | 314 | ret->parent = parent; |
01959dfe | 315 | ret->array = root; |
e8de4340 | 316 | } |
1da177e4 LT |
317 | return ret; |
318 | } | |
319 | ||
58d6ea30 | 320 | void radix_tree_node_rcu_free(struct rcu_head *head) |
7cf9c2c7 NP |
321 | { |
322 | struct radix_tree_node *node = | |
323 | container_of(head, struct radix_tree_node, rcu_head); | |
643b52b9 NP |
324 | |
325 | /* | |
175542f5 MW |
326 | * Must only free zeroed nodes into the slab. We can be left with |
327 | * non-NULL entries by radix_tree_free_nodes, so clear the entries | |
328 | * and tags here. | |
643b52b9 | 329 | */ |
175542f5 MW |
330 | memset(node->slots, 0, sizeof(node->slots)); |
331 | memset(node->tags, 0, sizeof(node->tags)); | |
91d9c05a | 332 | INIT_LIST_HEAD(&node->private_list); |
643b52b9 | 333 | |
7cf9c2c7 NP |
334 | kmem_cache_free(radix_tree_node_cachep, node); |
335 | } | |
336 | ||
1da177e4 LT |
337 | static inline void |
338 | radix_tree_node_free(struct radix_tree_node *node) | |
339 | { | |
7cf9c2c7 | 340 | call_rcu(&node->rcu_head, radix_tree_node_rcu_free); |
1da177e4 LT |
341 | } |
342 | ||
343 | /* | |
344 | * Load up this CPU's radix_tree_node buffer with sufficient objects to | |
345 | * ensure that the addition of a single element in the tree cannot fail. On | |
346 | * success, return zero, with preemption disabled. On error, return -ENOMEM | |
347 | * with preemption not disabled. | |
b34df792 DH |
348 | * |
349 | * To make use of this facility, the radix tree must be initialised without | |
d0164adc | 350 | * __GFP_DIRECT_RECLAIM being passed to INIT_RADIX_TREE(). |
1da177e4 | 351 | */ |
bc9ae224 | 352 | static __must_check int __radix_tree_preload(gfp_t gfp_mask, unsigned nr) |
1da177e4 LT |
353 | { |
354 | struct radix_tree_preload *rtp; | |
355 | struct radix_tree_node *node; | |
356 | int ret = -ENOMEM; | |
357 | ||
05eb6e72 VD |
358 | /* |
359 | * Nodes preloaded by one cgroup can be be used by another cgroup, so | |
360 | * they should never be accounted to any particular memory cgroup. | |
361 | */ | |
362 | gfp_mask &= ~__GFP_ACCOUNT; | |
363 | ||
1da177e4 | 364 | preempt_disable(); |
7c8e0181 | 365 | rtp = this_cpu_ptr(&radix_tree_preloads); |
c78c66d1 | 366 | while (rtp->nr < nr) { |
1da177e4 | 367 | preempt_enable(); |
488514d1 | 368 | node = kmem_cache_alloc(radix_tree_node_cachep, gfp_mask); |
1da177e4 LT |
369 | if (node == NULL) |
370 | goto out; | |
371 | preempt_disable(); | |
7c8e0181 | 372 | rtp = this_cpu_ptr(&radix_tree_preloads); |
c78c66d1 | 373 | if (rtp->nr < nr) { |
1293d5c5 | 374 | node->parent = rtp->nodes; |
9d2a8da0 KS |
375 | rtp->nodes = node; |
376 | rtp->nr++; | |
377 | } else { | |
1da177e4 | 378 | kmem_cache_free(radix_tree_node_cachep, node); |
9d2a8da0 | 379 | } |
1da177e4 LT |
380 | } |
381 | ret = 0; | |
382 | out: | |
383 | return ret; | |
384 | } | |
5e4c0d97 JK |
385 | |
386 | /* | |
387 | * Load up this CPU's radix_tree_node buffer with sufficient objects to | |
388 | * ensure that the addition of a single element in the tree cannot fail. On | |
389 | * success, return zero, with preemption disabled. On error, return -ENOMEM | |
390 | * with preemption not disabled. | |
391 | * | |
392 | * To make use of this facility, the radix tree must be initialised without | |
d0164adc | 393 | * __GFP_DIRECT_RECLAIM being passed to INIT_RADIX_TREE(). |
5e4c0d97 JK |
394 | */ |
395 | int radix_tree_preload(gfp_t gfp_mask) | |
396 | { | |
397 | /* Warn on non-sensical use... */ | |
d0164adc | 398 | WARN_ON_ONCE(!gfpflags_allow_blocking(gfp_mask)); |
c78c66d1 | 399 | return __radix_tree_preload(gfp_mask, RADIX_TREE_PRELOAD_SIZE); |
5e4c0d97 | 400 | } |
d7f0923d | 401 | EXPORT_SYMBOL(radix_tree_preload); |
1da177e4 | 402 | |
5e4c0d97 JK |
403 | /* |
404 | * The same as above function, except we don't guarantee preloading happens. | |
405 | * We do it, if we decide it helps. On success, return zero with preemption | |
406 | * disabled. On error, return -ENOMEM with preemption not disabled. | |
407 | */ | |
408 | int radix_tree_maybe_preload(gfp_t gfp_mask) | |
409 | { | |
d0164adc | 410 | if (gfpflags_allow_blocking(gfp_mask)) |
c78c66d1 | 411 | return __radix_tree_preload(gfp_mask, RADIX_TREE_PRELOAD_SIZE); |
5e4c0d97 JK |
412 | /* Preloading doesn't help anything with this gfp mask, skip it */ |
413 | preempt_disable(); | |
414 | return 0; | |
415 | } | |
416 | EXPORT_SYMBOL(radix_tree_maybe_preload); | |
417 | ||
2791653a MW |
418 | #ifdef CONFIG_RADIX_TREE_MULTIORDER |
419 | /* | |
420 | * Preload with enough objects to ensure that we can split a single entry | |
421 | * of order @old_order into many entries of size @new_order | |
422 | */ | |
423 | int radix_tree_split_preload(unsigned int old_order, unsigned int new_order, | |
424 | gfp_t gfp_mask) | |
425 | { | |
426 | unsigned top = 1 << (old_order % RADIX_TREE_MAP_SHIFT); | |
427 | unsigned layers = (old_order / RADIX_TREE_MAP_SHIFT) - | |
428 | (new_order / RADIX_TREE_MAP_SHIFT); | |
429 | unsigned nr = 0; | |
430 | ||
431 | WARN_ON_ONCE(!gfpflags_allow_blocking(gfp_mask)); | |
432 | BUG_ON(new_order >= old_order); | |
433 | ||
434 | while (layers--) | |
435 | nr = nr * RADIX_TREE_MAP_SIZE + 1; | |
436 | return __radix_tree_preload(gfp_mask, top * nr); | |
437 | } | |
438 | #endif | |
439 | ||
c78c66d1 KS |
440 | /* |
441 | * The same as function above, but preload number of nodes required to insert | |
442 | * (1 << order) continuous naturally-aligned elements. | |
443 | */ | |
444 | int radix_tree_maybe_preload_order(gfp_t gfp_mask, int order) | |
445 | { | |
446 | unsigned long nr_subtrees; | |
447 | int nr_nodes, subtree_height; | |
448 | ||
449 | /* Preloading doesn't help anything with this gfp mask, skip it */ | |
450 | if (!gfpflags_allow_blocking(gfp_mask)) { | |
451 | preempt_disable(); | |
452 | return 0; | |
453 | } | |
454 | ||
455 | /* | |
456 | * Calculate number and height of fully populated subtrees it takes to | |
457 | * store (1 << order) elements. | |
458 | */ | |
459 | nr_subtrees = 1 << order; | |
460 | for (subtree_height = 0; nr_subtrees > RADIX_TREE_MAP_SIZE; | |
461 | subtree_height++) | |
462 | nr_subtrees >>= RADIX_TREE_MAP_SHIFT; | |
463 | ||
464 | /* | |
465 | * The worst case is zero height tree with a single item at index 0 and | |
466 | * then inserting items starting at ULONG_MAX - (1 << order). | |
467 | * | |
468 | * This requires RADIX_TREE_MAX_PATH nodes to build branch from root to | |
469 | * 0-index item. | |
470 | */ | |
471 | nr_nodes = RADIX_TREE_MAX_PATH; | |
472 | ||
473 | /* Plus branch to fully populated subtrees. */ | |
474 | nr_nodes += RADIX_TREE_MAX_PATH - subtree_height; | |
475 | ||
476 | /* Root node is shared. */ | |
477 | nr_nodes--; | |
478 | ||
479 | /* Plus nodes required to build subtrees. */ | |
480 | nr_nodes += nr_subtrees * height_to_maxnodes[subtree_height]; | |
481 | ||
482 | return __radix_tree_preload(gfp_mask, nr_nodes); | |
483 | } | |
484 | ||
35534c86 | 485 | static unsigned radix_tree_load_root(const struct radix_tree_root *root, |
1456a439 MW |
486 | struct radix_tree_node **nodep, unsigned long *maxindex) |
487 | { | |
f8d5d0cc | 488 | struct radix_tree_node *node = rcu_dereference_raw(root->xa_head); |
1456a439 MW |
489 | |
490 | *nodep = node; | |
491 | ||
b194d16c | 492 | if (likely(radix_tree_is_internal_node(node))) { |
4dd6c098 | 493 | node = entry_to_node(node); |
1456a439 | 494 | *maxindex = node_maxindex(node); |
c12e51b0 | 495 | return node->shift + RADIX_TREE_MAP_SHIFT; |
1456a439 MW |
496 | } |
497 | ||
498 | *maxindex = 0; | |
499 | return 0; | |
500 | } | |
501 | ||
1da177e4 LT |
502 | /* |
503 | * Extend a radix tree so it can store key @index. | |
504 | */ | |
0a835c4f | 505 | static int radix_tree_extend(struct radix_tree_root *root, gfp_t gfp, |
d0891265 | 506 | unsigned long index, unsigned int shift) |
1da177e4 | 507 | { |
d7b62727 | 508 | void *entry; |
d0891265 | 509 | unsigned int maxshift; |
1da177e4 LT |
510 | int tag; |
511 | ||
d0891265 MW |
512 | /* Figure out what the shift should be. */ |
513 | maxshift = shift; | |
514 | while (index > shift_maxindex(maxshift)) | |
515 | maxshift += RADIX_TREE_MAP_SHIFT; | |
1da177e4 | 516 | |
f8d5d0cc | 517 | entry = rcu_dereference_raw(root->xa_head); |
d7b62727 | 518 | if (!entry && (!is_idr(root) || root_tag_get(root, IDR_FREE))) |
1da177e4 | 519 | goto out; |
1da177e4 | 520 | |
1da177e4 | 521 | do { |
0a835c4f | 522 | struct radix_tree_node *node = radix_tree_node_alloc(gfp, NULL, |
d58275bc | 523 | root, shift, 0, 1, 0); |
2fcd9005 | 524 | if (!node) |
1da177e4 LT |
525 | return -ENOMEM; |
526 | ||
0a835c4f MW |
527 | if (is_idr(root)) { |
528 | all_tag_set(node, IDR_FREE); | |
529 | if (!root_tag_get(root, IDR_FREE)) { | |
530 | tag_clear(node, IDR_FREE, 0); | |
531 | root_tag_set(root, IDR_FREE); | |
532 | } | |
533 | } else { | |
534 | /* Propagate the aggregated tag info to the new child */ | |
535 | for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) { | |
536 | if (root_tag_get(root, tag)) | |
537 | tag_set(node, tag, 0); | |
538 | } | |
1da177e4 LT |
539 | } |
540 | ||
d0891265 | 541 | BUG_ON(shift > BITS_PER_LONG); |
d7b62727 MW |
542 | if (radix_tree_is_internal_node(entry)) { |
543 | entry_to_node(entry)->parent = node; | |
3159f943 | 544 | } else if (xa_is_value(entry)) { |
01959dfe MW |
545 | /* Moving a value entry root->xa_head to a node */ |
546 | node->nr_values = 1; | |
f7942430 | 547 | } |
d7b62727 MW |
548 | /* |
549 | * entry was already in the radix tree, so we do not need | |
550 | * rcu_assign_pointer here | |
551 | */ | |
552 | node->slots[0] = (void __rcu *)entry; | |
553 | entry = node_to_entry(node); | |
f8d5d0cc | 554 | rcu_assign_pointer(root->xa_head, entry); |
d0891265 | 555 | shift += RADIX_TREE_MAP_SHIFT; |
d0891265 | 556 | } while (shift <= maxshift); |
1da177e4 | 557 | out: |
d0891265 | 558 | return maxshift + RADIX_TREE_MAP_SHIFT; |
1da177e4 LT |
559 | } |
560 | ||
f4b109c6 JW |
561 | /** |
562 | * radix_tree_shrink - shrink radix tree to minimum height | |
563 | * @root radix tree root | |
564 | */ | |
0ac398ef | 565 | static inline bool radix_tree_shrink(struct radix_tree_root *root, |
c7df8ad2 | 566 | radix_tree_update_node_t update_node) |
f4b109c6 | 567 | { |
0ac398ef MW |
568 | bool shrunk = false; |
569 | ||
f4b109c6 | 570 | for (;;) { |
f8d5d0cc | 571 | struct radix_tree_node *node = rcu_dereference_raw(root->xa_head); |
f4b109c6 JW |
572 | struct radix_tree_node *child; |
573 | ||
574 | if (!radix_tree_is_internal_node(node)) | |
575 | break; | |
576 | node = entry_to_node(node); | |
577 | ||
578 | /* | |
579 | * The candidate node has more than one child, or its child | |
580 | * is not at the leftmost slot, or the child is a multiorder | |
581 | * entry, we cannot shrink. | |
582 | */ | |
583 | if (node->count != 1) | |
584 | break; | |
12320d0f | 585 | child = rcu_dereference_raw(node->slots[0]); |
f4b109c6 JW |
586 | if (!child) |
587 | break; | |
588 | if (!radix_tree_is_internal_node(child) && node->shift) | |
589 | break; | |
590 | ||
66ee620f MW |
591 | /* |
592 | * For an IDR, we must not shrink entry 0 into the root in | |
593 | * case somebody calls idr_replace() with a pointer that | |
594 | * appears to be an internal entry | |
595 | */ | |
596 | if (!node->shift && is_idr(root)) | |
597 | break; | |
598 | ||
f4b109c6 JW |
599 | if (radix_tree_is_internal_node(child)) |
600 | entry_to_node(child)->parent = NULL; | |
601 | ||
602 | /* | |
603 | * We don't need rcu_assign_pointer(), since we are simply | |
604 | * moving the node from one part of the tree to another: if it | |
605 | * was safe to dereference the old pointer to it | |
606 | * (node->slots[0]), it will be safe to dereference the new | |
f8d5d0cc | 607 | * one (root->xa_head) as far as dependent read barriers go. |
f4b109c6 | 608 | */ |
f8d5d0cc | 609 | root->xa_head = (void __rcu *)child; |
0a835c4f MW |
610 | if (is_idr(root) && !tag_get(node, IDR_FREE, 0)) |
611 | root_tag_clear(root, IDR_FREE); | |
f4b109c6 JW |
612 | |
613 | /* | |
614 | * We have a dilemma here. The node's slot[0] must not be | |
615 | * NULLed in case there are concurrent lookups expecting to | |
616 | * find the item. However if this was a bottom-level node, | |
617 | * then it may be subject to the slot pointer being visible | |
618 | * to callers dereferencing it. If item corresponding to | |
619 | * slot[0] is subsequently deleted, these callers would expect | |
620 | * their slot to become empty sooner or later. | |
621 | * | |
622 | * For example, lockless pagecache will look up a slot, deref | |
623 | * the page pointer, and if the page has 0 refcount it means it | |
624 | * was concurrently deleted from pagecache so try the deref | |
625 | * again. Fortunately there is already a requirement for logic | |
626 | * to retry the entire slot lookup -- the indirect pointer | |
627 | * problem (replacing direct root node with an indirect pointer | |
628 | * also results in a stale slot). So tag the slot as indirect | |
629 | * to force callers to retry. | |
630 | */ | |
4d693d08 JW |
631 | node->count = 0; |
632 | if (!radix_tree_is_internal_node(child)) { | |
d7b62727 | 633 | node->slots[0] = (void __rcu *)RADIX_TREE_RETRY; |
4d693d08 | 634 | if (update_node) |
c7df8ad2 | 635 | update_node(node); |
4d693d08 | 636 | } |
f4b109c6 | 637 | |
ea07b862 | 638 | WARN_ON_ONCE(!list_empty(&node->private_list)); |
f4b109c6 | 639 | radix_tree_node_free(node); |
0ac398ef | 640 | shrunk = true; |
f4b109c6 | 641 | } |
0ac398ef MW |
642 | |
643 | return shrunk; | |
f4b109c6 JW |
644 | } |
645 | ||
0ac398ef | 646 | static bool delete_node(struct radix_tree_root *root, |
4d693d08 | 647 | struct radix_tree_node *node, |
c7df8ad2 | 648 | radix_tree_update_node_t update_node) |
f4b109c6 | 649 | { |
0ac398ef MW |
650 | bool deleted = false; |
651 | ||
f4b109c6 JW |
652 | do { |
653 | struct radix_tree_node *parent; | |
654 | ||
655 | if (node->count) { | |
12320d0f | 656 | if (node_to_entry(node) == |
f8d5d0cc MW |
657 | rcu_dereference_raw(root->xa_head)) |
658 | deleted |= radix_tree_shrink(root, update_node); | |
0ac398ef | 659 | return deleted; |
f4b109c6 JW |
660 | } |
661 | ||
662 | parent = node->parent; | |
663 | if (parent) { | |
664 | parent->slots[node->offset] = NULL; | |
665 | parent->count--; | |
666 | } else { | |
0a835c4f MW |
667 | /* |
668 | * Shouldn't the tags already have all been cleared | |
669 | * by the caller? | |
670 | */ | |
671 | if (!is_idr(root)) | |
672 | root_tag_clear_all(root); | |
f8d5d0cc | 673 | root->xa_head = NULL; |
f4b109c6 JW |
674 | } |
675 | ||
ea07b862 | 676 | WARN_ON_ONCE(!list_empty(&node->private_list)); |
f4b109c6 | 677 | radix_tree_node_free(node); |
0ac398ef | 678 | deleted = true; |
f4b109c6 JW |
679 | |
680 | node = parent; | |
681 | } while (node); | |
0ac398ef MW |
682 | |
683 | return deleted; | |
f4b109c6 JW |
684 | } |
685 | ||
1da177e4 | 686 | /** |
139e5616 | 687 | * __radix_tree_create - create a slot in a radix tree |
1da177e4 LT |
688 | * @root: radix tree root |
689 | * @index: index key | |
e6145236 | 690 | * @order: index occupies 2^order aligned slots |
139e5616 JW |
691 | * @nodep: returns node |
692 | * @slotp: returns slot | |
1da177e4 | 693 | * |
139e5616 JW |
694 | * Create, if necessary, and return the node and slot for an item |
695 | * at position @index in the radix tree @root. | |
696 | * | |
697 | * Until there is more than one item in the tree, no nodes are | |
f8d5d0cc | 698 | * allocated and @root->xa_head is used as a direct slot instead of |
139e5616 JW |
699 | * pointing to a node, in which case *@nodep will be NULL. |
700 | * | |
701 | * Returns -ENOMEM, or 0 for success. | |
1da177e4 | 702 | */ |
74d60958 MW |
703 | static int __radix_tree_create(struct radix_tree_root *root, |
704 | unsigned long index, unsigned order, | |
705 | struct radix_tree_node **nodep, void __rcu ***slotp) | |
1da177e4 | 706 | { |
89148aa4 | 707 | struct radix_tree_node *node = NULL, *child; |
f8d5d0cc | 708 | void __rcu **slot = (void __rcu **)&root->xa_head; |
49ea6ebc | 709 | unsigned long maxindex; |
89148aa4 | 710 | unsigned int shift, offset = 0; |
49ea6ebc | 711 | unsigned long max = index | ((1UL << order) - 1); |
0a835c4f | 712 | gfp_t gfp = root_gfp_mask(root); |
49ea6ebc | 713 | |
89148aa4 | 714 | shift = radix_tree_load_root(root, &child, &maxindex); |
1da177e4 LT |
715 | |
716 | /* Make sure the tree is high enough. */ | |
175542f5 MW |
717 | if (order > 0 && max == ((1UL << order) - 1)) |
718 | max++; | |
49ea6ebc | 719 | if (max > maxindex) { |
0a835c4f | 720 | int error = radix_tree_extend(root, gfp, max, shift); |
49ea6ebc | 721 | if (error < 0) |
1da177e4 | 722 | return error; |
49ea6ebc | 723 | shift = error; |
f8d5d0cc | 724 | child = rcu_dereference_raw(root->xa_head); |
1da177e4 LT |
725 | } |
726 | ||
e6145236 | 727 | while (shift > order) { |
c12e51b0 | 728 | shift -= RADIX_TREE_MAP_SHIFT; |
89148aa4 | 729 | if (child == NULL) { |
1da177e4 | 730 | /* Have to add a child node. */ |
d58275bc | 731 | child = radix_tree_node_alloc(gfp, node, root, shift, |
e8de4340 | 732 | offset, 0, 0); |
89148aa4 | 733 | if (!child) |
1da177e4 | 734 | return -ENOMEM; |
89148aa4 MW |
735 | rcu_assign_pointer(*slot, node_to_entry(child)); |
736 | if (node) | |
1da177e4 | 737 | node->count++; |
89148aa4 | 738 | } else if (!radix_tree_is_internal_node(child)) |
e6145236 | 739 | break; |
1da177e4 LT |
740 | |
741 | /* Go a level down */ | |
89148aa4 | 742 | node = entry_to_node(child); |
9e85d811 | 743 | offset = radix_tree_descend(node, &child, index); |
89148aa4 | 744 | slot = &node->slots[offset]; |
e6145236 MW |
745 | } |
746 | ||
175542f5 MW |
747 | if (nodep) |
748 | *nodep = node; | |
749 | if (slotp) | |
750 | *slotp = slot; | |
751 | return 0; | |
752 | } | |
753 | ||
175542f5 MW |
754 | /* |
755 | * Free any nodes below this node. The tree is presumed to not need | |
756 | * shrinking, and any user data in the tree is presumed to not need a | |
757 | * destructor called on it. If we need to add a destructor, we can | |
758 | * add that functionality later. Note that we may not clear tags or | |
759 | * slots from the tree as an RCU walker may still have a pointer into | |
760 | * this subtree. We could replace the entries with RADIX_TREE_RETRY, | |
761 | * but we'll still have to clear those in rcu_free. | |
762 | */ | |
763 | static void radix_tree_free_nodes(struct radix_tree_node *node) | |
764 | { | |
765 | unsigned offset = 0; | |
766 | struct radix_tree_node *child = entry_to_node(node); | |
767 | ||
768 | for (;;) { | |
12320d0f | 769 | void *entry = rcu_dereference_raw(child->slots[offset]); |
02c02bf1 | 770 | if (xa_is_node(entry) && child->shift) { |
175542f5 MW |
771 | child = entry_to_node(entry); |
772 | offset = 0; | |
773 | continue; | |
774 | } | |
775 | offset++; | |
776 | while (offset == RADIX_TREE_MAP_SIZE) { | |
777 | struct radix_tree_node *old = child; | |
778 | offset = child->offset + 1; | |
779 | child = child->parent; | |
dd040b6f | 780 | WARN_ON_ONCE(!list_empty(&old->private_list)); |
175542f5 MW |
781 | radix_tree_node_free(old); |
782 | if (old == entry_to_node(node)) | |
783 | return; | |
784 | } | |
785 | } | |
786 | } | |
787 | ||
0a835c4f | 788 | #ifdef CONFIG_RADIX_TREE_MULTIORDER |
d7b62727 MW |
789 | static inline int insert_entries(struct radix_tree_node *node, |
790 | void __rcu **slot, void *item, unsigned order, bool replace) | |
175542f5 | 791 | { |
02c02bf1 | 792 | void *sibling; |
175542f5 MW |
793 | unsigned i, n, tag, offset, tags = 0; |
794 | ||
795 | if (node) { | |
e157b555 MW |
796 | if (order > node->shift) |
797 | n = 1 << (order - node->shift); | |
798 | else | |
799 | n = 1; | |
175542f5 MW |
800 | offset = get_slot_offset(node, slot); |
801 | } else { | |
802 | n = 1; | |
803 | offset = 0; | |
804 | } | |
805 | ||
806 | if (n > 1) { | |
e6145236 | 807 | offset = offset & ~(n - 1); |
89148aa4 | 808 | slot = &node->slots[offset]; |
175542f5 | 809 | } |
02c02bf1 | 810 | sibling = xa_mk_sibling(offset); |
175542f5 MW |
811 | |
812 | for (i = 0; i < n; i++) { | |
813 | if (slot[i]) { | |
814 | if (replace) { | |
815 | node->count--; | |
816 | for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) | |
817 | if (tag_get(node, tag, offset + i)) | |
818 | tags |= 1 << tag; | |
819 | } else | |
e6145236 MW |
820 | return -EEXIST; |
821 | } | |
175542f5 | 822 | } |
e6145236 | 823 | |
175542f5 | 824 | for (i = 0; i < n; i++) { |
12320d0f | 825 | struct radix_tree_node *old = rcu_dereference_raw(slot[i]); |
175542f5 | 826 | if (i) { |
02c02bf1 | 827 | rcu_assign_pointer(slot[i], sibling); |
175542f5 MW |
828 | for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) |
829 | if (tags & (1 << tag)) | |
830 | tag_clear(node, tag, offset + i); | |
831 | } else { | |
832 | rcu_assign_pointer(slot[i], item); | |
833 | for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) | |
834 | if (tags & (1 << tag)) | |
835 | tag_set(node, tag, offset); | |
e6145236 | 836 | } |
02c02bf1 | 837 | if (xa_is_node(old)) |
175542f5 | 838 | radix_tree_free_nodes(old); |
3159f943 | 839 | if (xa_is_value(old)) |
01959dfe | 840 | node->nr_values--; |
612d6c19 | 841 | } |
175542f5 MW |
842 | if (node) { |
843 | node->count += n; | |
3159f943 | 844 | if (xa_is_value(item)) |
01959dfe | 845 | node->nr_values += n; |
175542f5 MW |
846 | } |
847 | return n; | |
139e5616 | 848 | } |
175542f5 | 849 | #else |
d7b62727 MW |
850 | static inline int insert_entries(struct radix_tree_node *node, |
851 | void __rcu **slot, void *item, unsigned order, bool replace) | |
175542f5 MW |
852 | { |
853 | if (*slot) | |
854 | return -EEXIST; | |
855 | rcu_assign_pointer(*slot, item); | |
856 | if (node) { | |
857 | node->count++; | |
3159f943 | 858 | if (xa_is_value(item)) |
01959dfe | 859 | node->nr_values++; |
175542f5 MW |
860 | } |
861 | return 1; | |
862 | } | |
863 | #endif | |
139e5616 JW |
864 | |
865 | /** | |
e6145236 | 866 | * __radix_tree_insert - insert into a radix tree |
139e5616 JW |
867 | * @root: radix tree root |
868 | * @index: index key | |
e6145236 | 869 | * @order: key covers the 2^order indices around index |
139e5616 JW |
870 | * @item: item to insert |
871 | * | |
872 | * Insert an item into the radix tree at position @index. | |
873 | */ | |
e6145236 MW |
874 | int __radix_tree_insert(struct radix_tree_root *root, unsigned long index, |
875 | unsigned order, void *item) | |
139e5616 JW |
876 | { |
877 | struct radix_tree_node *node; | |
d7b62727 | 878 | void __rcu **slot; |
139e5616 JW |
879 | int error; |
880 | ||
b194d16c | 881 | BUG_ON(radix_tree_is_internal_node(item)); |
139e5616 | 882 | |
e6145236 | 883 | error = __radix_tree_create(root, index, order, &node, &slot); |
139e5616 JW |
884 | if (error) |
885 | return error; | |
175542f5 MW |
886 | |
887 | error = insert_entries(node, slot, item, order, false); | |
888 | if (error < 0) | |
889 | return error; | |
201b6264 | 890 | |
612d6c19 | 891 | if (node) { |
7b60e9ad | 892 | unsigned offset = get_slot_offset(node, slot); |
7b60e9ad MW |
893 | BUG_ON(tag_get(node, 0, offset)); |
894 | BUG_ON(tag_get(node, 1, offset)); | |
895 | BUG_ON(tag_get(node, 2, offset)); | |
612d6c19 | 896 | } else { |
7b60e9ad | 897 | BUG_ON(root_tags_get(root)); |
612d6c19 | 898 | } |
1da177e4 | 899 | |
1da177e4 LT |
900 | return 0; |
901 | } | |
e6145236 | 902 | EXPORT_SYMBOL(__radix_tree_insert); |
1da177e4 | 903 | |
139e5616 JW |
904 | /** |
905 | * __radix_tree_lookup - lookup an item in a radix tree | |
906 | * @root: radix tree root | |
907 | * @index: index key | |
908 | * @nodep: returns node | |
909 | * @slotp: returns slot | |
910 | * | |
911 | * Lookup and return the item at position @index in the radix | |
912 | * tree @root. | |
913 | * | |
914 | * Until there is more than one item in the tree, no nodes are | |
f8d5d0cc | 915 | * allocated and @root->xa_head is used as a direct slot instead of |
139e5616 | 916 | * pointing to a node, in which case *@nodep will be NULL. |
7cf9c2c7 | 917 | */ |
35534c86 MW |
918 | void *__radix_tree_lookup(const struct radix_tree_root *root, |
919 | unsigned long index, struct radix_tree_node **nodep, | |
d7b62727 | 920 | void __rcu ***slotp) |
1da177e4 | 921 | { |
139e5616 | 922 | struct radix_tree_node *node, *parent; |
85829954 | 923 | unsigned long maxindex; |
d7b62727 | 924 | void __rcu **slot; |
612d6c19 | 925 | |
85829954 MW |
926 | restart: |
927 | parent = NULL; | |
f8d5d0cc | 928 | slot = (void __rcu **)&root->xa_head; |
9e85d811 | 929 | radix_tree_load_root(root, &node, &maxindex); |
85829954 | 930 | if (index > maxindex) |
1da177e4 LT |
931 | return NULL; |
932 | ||
b194d16c | 933 | while (radix_tree_is_internal_node(node)) { |
85829954 | 934 | unsigned offset; |
1da177e4 | 935 | |
85829954 MW |
936 | if (node == RADIX_TREE_RETRY) |
937 | goto restart; | |
4dd6c098 | 938 | parent = entry_to_node(node); |
9e85d811 | 939 | offset = radix_tree_descend(parent, &node, index); |
85829954 | 940 | slot = parent->slots + offset; |
66ee620f MW |
941 | if (parent->shift == 0) |
942 | break; | |
85829954 | 943 | } |
1da177e4 | 944 | |
139e5616 JW |
945 | if (nodep) |
946 | *nodep = parent; | |
947 | if (slotp) | |
948 | *slotp = slot; | |
949 | return node; | |
b72b71c6 HS |
950 | } |
951 | ||
952 | /** | |
953 | * radix_tree_lookup_slot - lookup a slot in a radix tree | |
954 | * @root: radix tree root | |
955 | * @index: index key | |
956 | * | |
957 | * Returns: the slot corresponding to the position @index in the | |
958 | * radix tree @root. This is useful for update-if-exists operations. | |
959 | * | |
960 | * This function can be called under rcu_read_lock iff the slot is not | |
961 | * modified by radix_tree_replace_slot, otherwise it must be called | |
962 | * exclusive from other writers. Any dereference of the slot must be done | |
963 | * using radix_tree_deref_slot. | |
964 | */ | |
d7b62727 | 965 | void __rcu **radix_tree_lookup_slot(const struct radix_tree_root *root, |
35534c86 | 966 | unsigned long index) |
b72b71c6 | 967 | { |
d7b62727 | 968 | void __rcu **slot; |
139e5616 JW |
969 | |
970 | if (!__radix_tree_lookup(root, index, NULL, &slot)) | |
971 | return NULL; | |
972 | return slot; | |
a4331366 | 973 | } |
a4331366 HR |
974 | EXPORT_SYMBOL(radix_tree_lookup_slot); |
975 | ||
976 | /** | |
977 | * radix_tree_lookup - perform lookup operation on a radix tree | |
978 | * @root: radix tree root | |
979 | * @index: index key | |
980 | * | |
981 | * Lookup the item at the position @index in the radix tree @root. | |
7cf9c2c7 NP |
982 | * |
983 | * This function can be called under rcu_read_lock, however the caller | |
984 | * must manage lifetimes of leaf nodes (eg. RCU may also be used to free | |
985 | * them safely). No RCU barriers are required to access or modify the | |
986 | * returned item, however. | |
a4331366 | 987 | */ |
35534c86 | 988 | void *radix_tree_lookup(const struct radix_tree_root *root, unsigned long index) |
a4331366 | 989 | { |
139e5616 | 990 | return __radix_tree_lookup(root, index, NULL, NULL); |
1da177e4 LT |
991 | } |
992 | EXPORT_SYMBOL(radix_tree_lookup); | |
993 | ||
0a835c4f | 994 | static inline void replace_sibling_entries(struct radix_tree_node *node, |
01959dfe | 995 | void __rcu **slot, int count, int values) |
a90eb3a2 | 996 | { |
a90eb3a2 | 997 | #ifdef CONFIG_RADIX_TREE_MULTIORDER |
02c02bf1 MW |
998 | unsigned offset = get_slot_offset(node, slot); |
999 | void *ptr = xa_mk_sibling(offset); | |
a90eb3a2 | 1000 | |
02c02bf1 | 1001 | while (++offset < RADIX_TREE_MAP_SIZE) { |
12320d0f | 1002 | if (rcu_dereference_raw(node->slots[offset]) != ptr) |
a90eb3a2 | 1003 | break; |
0a835c4f MW |
1004 | if (count < 0) { |
1005 | node->slots[offset] = NULL; | |
1006 | node->count--; | |
1007 | } | |
01959dfe | 1008 | node->nr_values += values; |
a90eb3a2 MW |
1009 | } |
1010 | #endif | |
a90eb3a2 MW |
1011 | } |
1012 | ||
d7b62727 | 1013 | static void replace_slot(void __rcu **slot, void *item, |
01959dfe | 1014 | struct radix_tree_node *node, int count, int values) |
f7942430 | 1015 | { |
01959dfe | 1016 | if (node && (count || values)) { |
f4b109c6 | 1017 | node->count += count; |
01959dfe MW |
1018 | node->nr_values += values; |
1019 | replace_sibling_entries(node, slot, count, values); | |
f4b109c6 | 1020 | } |
f7942430 JW |
1021 | |
1022 | rcu_assign_pointer(*slot, item); | |
1023 | } | |
1024 | ||
0a835c4f MW |
1025 | static bool node_tag_get(const struct radix_tree_root *root, |
1026 | const struct radix_tree_node *node, | |
1027 | unsigned int tag, unsigned int offset) | |
a90eb3a2 | 1028 | { |
0a835c4f MW |
1029 | if (node) |
1030 | return tag_get(node, tag, offset); | |
1031 | return root_tag_get(root, tag); | |
1032 | } | |
a90eb3a2 | 1033 | |
0a835c4f MW |
1034 | /* |
1035 | * IDR users want to be able to store NULL in the tree, so if the slot isn't | |
1036 | * free, don't adjust the count, even if it's transitioning between NULL and | |
1037 | * non-NULL. For the IDA, we mark slots as being IDR_FREE while they still | |
1038 | * have empty bits, but it only stores NULL in slots when they're being | |
1039 | * deleted. | |
1040 | */ | |
1041 | static int calculate_count(struct radix_tree_root *root, | |
d7b62727 | 1042 | struct radix_tree_node *node, void __rcu **slot, |
0a835c4f MW |
1043 | void *item, void *old) |
1044 | { | |
1045 | if (is_idr(root)) { | |
1046 | unsigned offset = get_slot_offset(node, slot); | |
1047 | bool free = node_tag_get(root, node, IDR_FREE, offset); | |
1048 | if (!free) | |
1049 | return 0; | |
1050 | if (!old) | |
1051 | return 1; | |
a90eb3a2 | 1052 | } |
0a835c4f | 1053 | return !!item - !!old; |
a90eb3a2 MW |
1054 | } |
1055 | ||
6d75f366 JW |
1056 | /** |
1057 | * __radix_tree_replace - replace item in a slot | |
4d693d08 JW |
1058 | * @root: radix tree root |
1059 | * @node: pointer to tree node | |
1060 | * @slot: pointer to slot in @node | |
1061 | * @item: new item to store in the slot. | |
1062 | * @update_node: callback for changing leaf nodes | |
6d75f366 JW |
1063 | * |
1064 | * For use with __radix_tree_lookup(). Caller must hold tree write locked | |
1065 | * across slot lookup and replacement. | |
1066 | */ | |
1067 | void __radix_tree_replace(struct radix_tree_root *root, | |
1068 | struct radix_tree_node *node, | |
d7b62727 | 1069 | void __rcu **slot, void *item, |
c7df8ad2 | 1070 | radix_tree_update_node_t update_node) |
6d75f366 | 1071 | { |
0a835c4f | 1072 | void *old = rcu_dereference_raw(*slot); |
01959dfe | 1073 | int values = !!xa_is_value(item) - !!xa_is_value(old); |
0a835c4f MW |
1074 | int count = calculate_count(root, node, slot, item, old); |
1075 | ||
6d75f366 | 1076 | /* |
01959dfe | 1077 | * This function supports replacing value entries and |
f4b109c6 | 1078 | * deleting entries, but that needs accounting against the |
f8d5d0cc | 1079 | * node unless the slot is root->xa_head. |
6d75f366 | 1080 | */ |
f8d5d0cc | 1081 | WARN_ON_ONCE(!node && (slot != (void __rcu **)&root->xa_head) && |
01959dfe MW |
1082 | (count || values)); |
1083 | replace_slot(slot, item, node, count, values); | |
f4b109c6 | 1084 | |
4d693d08 JW |
1085 | if (!node) |
1086 | return; | |
1087 | ||
1088 | if (update_node) | |
c7df8ad2 | 1089 | update_node(node); |
4d693d08 | 1090 | |
c7df8ad2 | 1091 | delete_node(root, node, update_node); |
6d75f366 JW |
1092 | } |
1093 | ||
1094 | /** | |
1095 | * radix_tree_replace_slot - replace item in a slot | |
1096 | * @root: radix tree root | |
1097 | * @slot: pointer to slot | |
1098 | * @item: new item to store in the slot. | |
1099 | * | |
1100 | * For use with radix_tree_lookup_slot(), radix_tree_gang_lookup_slot(), | |
1101 | * radix_tree_gang_lookup_tag_slot(). Caller must hold tree write locked | |
1102 | * across slot lookup and replacement. | |
1103 | * | |
1104 | * NOTE: This cannot be used to switch between non-entries (empty slots), | |
01959dfe | 1105 | * regular entries, and value entries, as that requires accounting |
f4b109c6 | 1106 | * inside the radix tree node. When switching from one type of entry or |
e157b555 MW |
1107 | * deleting, use __radix_tree_lookup() and __radix_tree_replace() or |
1108 | * radix_tree_iter_replace(). | |
6d75f366 JW |
1109 | */ |
1110 | void radix_tree_replace_slot(struct radix_tree_root *root, | |
d7b62727 | 1111 | void __rcu **slot, void *item) |
6d75f366 | 1112 | { |
c7df8ad2 | 1113 | __radix_tree_replace(root, NULL, slot, item, NULL); |
6d75f366 | 1114 | } |
10257d71 | 1115 | EXPORT_SYMBOL(radix_tree_replace_slot); |
6d75f366 | 1116 | |
e157b555 MW |
1117 | /** |
1118 | * radix_tree_iter_replace - replace item in a slot | |
1119 | * @root: radix tree root | |
1120 | * @slot: pointer to slot | |
1121 | * @item: new item to store in the slot. | |
1122 | * | |
1123 | * For use with radix_tree_split() and radix_tree_for_each_slot(). | |
1124 | * Caller must hold tree write locked across split and replacement. | |
1125 | */ | |
1126 | void radix_tree_iter_replace(struct radix_tree_root *root, | |
d7b62727 MW |
1127 | const struct radix_tree_iter *iter, |
1128 | void __rcu **slot, void *item) | |
e157b555 | 1129 | { |
c7df8ad2 | 1130 | __radix_tree_replace(root, iter->node, slot, item, NULL); |
e157b555 MW |
1131 | } |
1132 | ||
175542f5 MW |
1133 | #ifdef CONFIG_RADIX_TREE_MULTIORDER |
1134 | /** | |
1135 | * radix_tree_join - replace multiple entries with one multiorder entry | |
1136 | * @root: radix tree root | |
1137 | * @index: an index inside the new entry | |
1138 | * @order: order of the new entry | |
1139 | * @item: new entry | |
1140 | * | |
1141 | * Call this function to replace several entries with one larger entry. | |
1142 | * The existing entries are presumed to not need freeing as a result of | |
1143 | * this call. | |
1144 | * | |
1145 | * The replacement entry will have all the tags set on it that were set | |
1146 | * on any of the entries it is replacing. | |
1147 | */ | |
1148 | int radix_tree_join(struct radix_tree_root *root, unsigned long index, | |
1149 | unsigned order, void *item) | |
1150 | { | |
1151 | struct radix_tree_node *node; | |
d7b62727 | 1152 | void __rcu **slot; |
175542f5 MW |
1153 | int error; |
1154 | ||
1155 | BUG_ON(radix_tree_is_internal_node(item)); | |
1156 | ||
1157 | error = __radix_tree_create(root, index, order, &node, &slot); | |
1158 | if (!error) | |
1159 | error = insert_entries(node, slot, item, order, true); | |
1160 | if (error > 0) | |
1161 | error = 0; | |
1162 | ||
1163 | return error; | |
1164 | } | |
e157b555 MW |
1165 | |
1166 | /** | |
1167 | * radix_tree_split - Split an entry into smaller entries | |
1168 | * @root: radix tree root | |
1169 | * @index: An index within the large entry | |
1170 | * @order: Order of new entries | |
1171 | * | |
1172 | * Call this function as the first step in replacing a multiorder entry | |
1173 | * with several entries of lower order. After this function returns, | |
1174 | * loop over the relevant portion of the tree using radix_tree_for_each_slot() | |
1175 | * and call radix_tree_iter_replace() to set up each new entry. | |
1176 | * | |
1177 | * The tags from this entry are replicated to all the new entries. | |
1178 | * | |
1179 | * The radix tree should be locked against modification during the entire | |
1180 | * replacement operation. Lock-free lookups will see RADIX_TREE_RETRY which | |
1181 | * should prompt RCU walkers to restart the lookup from the root. | |
1182 | */ | |
1183 | int radix_tree_split(struct radix_tree_root *root, unsigned long index, | |
1184 | unsigned order) | |
1185 | { | |
1186 | struct radix_tree_node *parent, *node, *child; | |
d7b62727 | 1187 | void __rcu **slot; |
e157b555 MW |
1188 | unsigned int offset, end; |
1189 | unsigned n, tag, tags = 0; | |
0a835c4f | 1190 | gfp_t gfp = root_gfp_mask(root); |
e157b555 MW |
1191 | |
1192 | if (!__radix_tree_lookup(root, index, &parent, &slot)) | |
1193 | return -ENOENT; | |
1194 | if (!parent) | |
1195 | return -ENOENT; | |
1196 | ||
1197 | offset = get_slot_offset(parent, slot); | |
1198 | ||
1199 | for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) | |
1200 | if (tag_get(parent, tag, offset)) | |
1201 | tags |= 1 << tag; | |
1202 | ||
1203 | for (end = offset + 1; end < RADIX_TREE_MAP_SIZE; end++) { | |
02c02bf1 | 1204 | if (!xa_is_sibling(rcu_dereference_raw(parent->slots[end]))) |
e157b555 MW |
1205 | break; |
1206 | for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) | |
1207 | if (tags & (1 << tag)) | |
1208 | tag_set(parent, tag, end); | |
1209 | /* rcu_assign_pointer ensures tags are set before RETRY */ | |
1210 | rcu_assign_pointer(parent->slots[end], RADIX_TREE_RETRY); | |
1211 | } | |
1212 | rcu_assign_pointer(parent->slots[offset], RADIX_TREE_RETRY); | |
01959dfe | 1213 | parent->nr_values -= (end - offset); |
e157b555 MW |
1214 | |
1215 | if (order == parent->shift) | |
1216 | return 0; | |
1217 | if (order > parent->shift) { | |
1218 | while (offset < end) | |
1219 | offset += insert_entries(parent, &parent->slots[offset], | |
1220 | RADIX_TREE_RETRY, order, true); | |
1221 | return 0; | |
1222 | } | |
1223 | ||
1224 | node = parent; | |
1225 | ||
1226 | for (;;) { | |
1227 | if (node->shift > order) { | |
d58275bc | 1228 | child = radix_tree_node_alloc(gfp, node, root, |
e8de4340 MW |
1229 | node->shift - RADIX_TREE_MAP_SHIFT, |
1230 | offset, 0, 0); | |
e157b555 MW |
1231 | if (!child) |
1232 | goto nomem; | |
e157b555 MW |
1233 | if (node != parent) { |
1234 | node->count++; | |
12320d0f MW |
1235 | rcu_assign_pointer(node->slots[offset], |
1236 | node_to_entry(child)); | |
e157b555 MW |
1237 | for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) |
1238 | if (tags & (1 << tag)) | |
1239 | tag_set(node, tag, offset); | |
1240 | } | |
1241 | ||
1242 | node = child; | |
1243 | offset = 0; | |
1244 | continue; | |
1245 | } | |
1246 | ||
1247 | n = insert_entries(node, &node->slots[offset], | |
1248 | RADIX_TREE_RETRY, order, false); | |
1249 | BUG_ON(n > RADIX_TREE_MAP_SIZE); | |
1250 | ||
1251 | for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) | |
1252 | if (tags & (1 << tag)) | |
1253 | tag_set(node, tag, offset); | |
1254 | offset += n; | |
1255 | ||
1256 | while (offset == RADIX_TREE_MAP_SIZE) { | |
1257 | if (node == parent) | |
1258 | break; | |
1259 | offset = node->offset; | |
1260 | child = node; | |
1261 | node = node->parent; | |
1262 | rcu_assign_pointer(node->slots[offset], | |
1263 | node_to_entry(child)); | |
1264 | offset++; | |
1265 | } | |
1266 | if ((node == parent) && (offset == end)) | |
1267 | return 0; | |
1268 | } | |
1269 | ||
1270 | nomem: | |
1271 | /* Shouldn't happen; did user forget to preload? */ | |
1272 | /* TODO: free all the allocated nodes */ | |
1273 | WARN_ON(1); | |
1274 | return -ENOMEM; | |
1275 | } | |
175542f5 MW |
1276 | #endif |
1277 | ||
30b888ba MW |
1278 | static void node_tag_set(struct radix_tree_root *root, |
1279 | struct radix_tree_node *node, | |
1280 | unsigned int tag, unsigned int offset) | |
1281 | { | |
1282 | while (node) { | |
1283 | if (tag_get(node, tag, offset)) | |
1284 | return; | |
1285 | tag_set(node, tag, offset); | |
1286 | offset = node->offset; | |
1287 | node = node->parent; | |
1288 | } | |
1289 | ||
1290 | if (!root_tag_get(root, tag)) | |
1291 | root_tag_set(root, tag); | |
1292 | } | |
1293 | ||
1da177e4 LT |
1294 | /** |
1295 | * radix_tree_tag_set - set a tag on a radix tree node | |
1296 | * @root: radix tree root | |
1297 | * @index: index key | |
2fcd9005 | 1298 | * @tag: tag index |
1da177e4 | 1299 | * |
daff89f3 JC |
1300 | * Set the search tag (which must be < RADIX_TREE_MAX_TAGS) |
1301 | * corresponding to @index in the radix tree. From | |
1da177e4 LT |
1302 | * the root all the way down to the leaf node. |
1303 | * | |
2fcd9005 | 1304 | * Returns the address of the tagged item. Setting a tag on a not-present |
1da177e4 LT |
1305 | * item is a bug. |
1306 | */ | |
1307 | void *radix_tree_tag_set(struct radix_tree_root *root, | |
daff89f3 | 1308 | unsigned long index, unsigned int tag) |
1da177e4 | 1309 | { |
fb969909 RZ |
1310 | struct radix_tree_node *node, *parent; |
1311 | unsigned long maxindex; | |
1da177e4 | 1312 | |
9e85d811 | 1313 | radix_tree_load_root(root, &node, &maxindex); |
fb969909 | 1314 | BUG_ON(index > maxindex); |
1da177e4 | 1315 | |
b194d16c | 1316 | while (radix_tree_is_internal_node(node)) { |
fb969909 | 1317 | unsigned offset; |
1da177e4 | 1318 | |
4dd6c098 | 1319 | parent = entry_to_node(node); |
9e85d811 | 1320 | offset = radix_tree_descend(parent, &node, index); |
fb969909 RZ |
1321 | BUG_ON(!node); |
1322 | ||
1323 | if (!tag_get(parent, tag, offset)) | |
1324 | tag_set(parent, tag, offset); | |
1da177e4 LT |
1325 | } |
1326 | ||
612d6c19 | 1327 | /* set the root's tag bit */ |
fb969909 | 1328 | if (!root_tag_get(root, tag)) |
612d6c19 NP |
1329 | root_tag_set(root, tag); |
1330 | ||
fb969909 | 1331 | return node; |
1da177e4 LT |
1332 | } |
1333 | EXPORT_SYMBOL(radix_tree_tag_set); | |
1334 | ||
30b888ba MW |
1335 | /** |
1336 | * radix_tree_iter_tag_set - set a tag on the current iterator entry | |
1337 | * @root: radix tree root | |
1338 | * @iter: iterator state | |
1339 | * @tag: tag to set | |
1340 | */ | |
1341 | void radix_tree_iter_tag_set(struct radix_tree_root *root, | |
1342 | const struct radix_tree_iter *iter, unsigned int tag) | |
1343 | { | |
1344 | node_tag_set(root, iter->node, tag, iter_offset(iter)); | |
1345 | } | |
1346 | ||
d604c324 MW |
1347 | static void node_tag_clear(struct radix_tree_root *root, |
1348 | struct radix_tree_node *node, | |
1349 | unsigned int tag, unsigned int offset) | |
1350 | { | |
1351 | while (node) { | |
1352 | if (!tag_get(node, tag, offset)) | |
1353 | return; | |
1354 | tag_clear(node, tag, offset); | |
1355 | if (any_tag_set(node, tag)) | |
1356 | return; | |
1357 | ||
1358 | offset = node->offset; | |
1359 | node = node->parent; | |
1360 | } | |
1361 | ||
1362 | /* clear the root's tag bit */ | |
1363 | if (root_tag_get(root, tag)) | |
1364 | root_tag_clear(root, tag); | |
1365 | } | |
1366 | ||
1da177e4 LT |
1367 | /** |
1368 | * radix_tree_tag_clear - clear a tag on a radix tree node | |
1369 | * @root: radix tree root | |
1370 | * @index: index key | |
2fcd9005 | 1371 | * @tag: tag index |
1da177e4 | 1372 | * |
daff89f3 | 1373 | * Clear the search tag (which must be < RADIX_TREE_MAX_TAGS) |
2fcd9005 MW |
1374 | * corresponding to @index in the radix tree. If this causes |
1375 | * the leaf node to have no tags set then clear the tag in the | |
1da177e4 LT |
1376 | * next-to-leaf node, etc. |
1377 | * | |
1378 | * Returns the address of the tagged item on success, else NULL. ie: | |
1379 | * has the same return value and semantics as radix_tree_lookup(). | |
1380 | */ | |
1381 | void *radix_tree_tag_clear(struct radix_tree_root *root, | |
daff89f3 | 1382 | unsigned long index, unsigned int tag) |
1da177e4 | 1383 | { |
00f47b58 RZ |
1384 | struct radix_tree_node *node, *parent; |
1385 | unsigned long maxindex; | |
e2bdb933 | 1386 | int uninitialized_var(offset); |
1da177e4 | 1387 | |
9e85d811 | 1388 | radix_tree_load_root(root, &node, &maxindex); |
00f47b58 RZ |
1389 | if (index > maxindex) |
1390 | return NULL; | |
1da177e4 | 1391 | |
00f47b58 | 1392 | parent = NULL; |
1da177e4 | 1393 | |
b194d16c | 1394 | while (radix_tree_is_internal_node(node)) { |
4dd6c098 | 1395 | parent = entry_to_node(node); |
9e85d811 | 1396 | offset = radix_tree_descend(parent, &node, index); |
1da177e4 LT |
1397 | } |
1398 | ||
d604c324 MW |
1399 | if (node) |
1400 | node_tag_clear(root, parent, tag, offset); | |
1da177e4 | 1401 | |
00f47b58 | 1402 | return node; |
1da177e4 LT |
1403 | } |
1404 | EXPORT_SYMBOL(radix_tree_tag_clear); | |
1405 | ||
30b888ba MW |
1406 | /** |
1407 | * radix_tree_iter_tag_clear - clear a tag on the current iterator entry | |
1408 | * @root: radix tree root | |
1409 | * @iter: iterator state | |
1410 | * @tag: tag to clear | |
1411 | */ | |
1412 | void radix_tree_iter_tag_clear(struct radix_tree_root *root, | |
1413 | const struct radix_tree_iter *iter, unsigned int tag) | |
1414 | { | |
1415 | node_tag_clear(root, iter->node, tag, iter_offset(iter)); | |
1416 | } | |
1417 | ||
1da177e4 | 1418 | /** |
32605a18 MT |
1419 | * radix_tree_tag_get - get a tag on a radix tree node |
1420 | * @root: radix tree root | |
1421 | * @index: index key | |
2fcd9005 | 1422 | * @tag: tag index (< RADIX_TREE_MAX_TAGS) |
1da177e4 | 1423 | * |
32605a18 | 1424 | * Return values: |
1da177e4 | 1425 | * |
612d6c19 NP |
1426 | * 0: tag not present or not set |
1427 | * 1: tag set | |
ce82653d DH |
1428 | * |
1429 | * Note that the return value of this function may not be relied on, even if | |
1430 | * the RCU lock is held, unless tag modification and node deletion are excluded | |
1431 | * from concurrency. | |
1da177e4 | 1432 | */ |
35534c86 | 1433 | int radix_tree_tag_get(const struct radix_tree_root *root, |
daff89f3 | 1434 | unsigned long index, unsigned int tag) |
1da177e4 | 1435 | { |
4589ba6d RZ |
1436 | struct radix_tree_node *node, *parent; |
1437 | unsigned long maxindex; | |
1da177e4 | 1438 | |
612d6c19 NP |
1439 | if (!root_tag_get(root, tag)) |
1440 | return 0; | |
1441 | ||
9e85d811 | 1442 | radix_tree_load_root(root, &node, &maxindex); |
4589ba6d RZ |
1443 | if (index > maxindex) |
1444 | return 0; | |
7cf9c2c7 | 1445 | |
b194d16c | 1446 | while (radix_tree_is_internal_node(node)) { |
9e85d811 | 1447 | unsigned offset; |
1da177e4 | 1448 | |
4dd6c098 | 1449 | parent = entry_to_node(node); |
9e85d811 | 1450 | offset = radix_tree_descend(parent, &node, index); |
1da177e4 | 1451 | |
4589ba6d | 1452 | if (!tag_get(parent, tag, offset)) |
3fa36acb | 1453 | return 0; |
4589ba6d RZ |
1454 | if (node == RADIX_TREE_RETRY) |
1455 | break; | |
1da177e4 | 1456 | } |
4589ba6d RZ |
1457 | |
1458 | return 1; | |
1da177e4 LT |
1459 | } |
1460 | EXPORT_SYMBOL(radix_tree_tag_get); | |
1da177e4 | 1461 | |
21ef5339 RZ |
1462 | static inline void __set_iter_shift(struct radix_tree_iter *iter, |
1463 | unsigned int shift) | |
1464 | { | |
1465 | #ifdef CONFIG_RADIX_TREE_MULTIORDER | |
1466 | iter->shift = shift; | |
1467 | #endif | |
1468 | } | |
1469 | ||
148deab2 MW |
1470 | /* Construct iter->tags bit-mask from node->tags[tag] array */ |
1471 | static void set_iter_tags(struct radix_tree_iter *iter, | |
1472 | struct radix_tree_node *node, unsigned offset, | |
1473 | unsigned tag) | |
1474 | { | |
1475 | unsigned tag_long = offset / BITS_PER_LONG; | |
1476 | unsigned tag_bit = offset % BITS_PER_LONG; | |
1477 | ||
0a835c4f MW |
1478 | if (!node) { |
1479 | iter->tags = 1; | |
1480 | return; | |
1481 | } | |
1482 | ||
148deab2 MW |
1483 | iter->tags = node->tags[tag][tag_long] >> tag_bit; |
1484 | ||
1485 | /* This never happens if RADIX_TREE_TAG_LONGS == 1 */ | |
1486 | if (tag_long < RADIX_TREE_TAG_LONGS - 1) { | |
1487 | /* Pick tags from next element */ | |
1488 | if (tag_bit) | |
1489 | iter->tags |= node->tags[tag][tag_long + 1] << | |
1490 | (BITS_PER_LONG - tag_bit); | |
1491 | /* Clip chunk size, here only BITS_PER_LONG tags */ | |
1492 | iter->next_index = __radix_tree_iter_add(iter, BITS_PER_LONG); | |
1493 | } | |
1494 | } | |
1495 | ||
1496 | #ifdef CONFIG_RADIX_TREE_MULTIORDER | |
d7b62727 MW |
1497 | static void __rcu **skip_siblings(struct radix_tree_node **nodep, |
1498 | void __rcu **slot, struct radix_tree_iter *iter) | |
148deab2 | 1499 | { |
148deab2 MW |
1500 | while (iter->index < iter->next_index) { |
1501 | *nodep = rcu_dereference_raw(*slot); | |
02c02bf1 | 1502 | if (*nodep && !xa_is_sibling(*nodep)) |
148deab2 MW |
1503 | return slot; |
1504 | slot++; | |
1505 | iter->index = __radix_tree_iter_add(iter, 1); | |
1506 | iter->tags >>= 1; | |
1507 | } | |
1508 | ||
1509 | *nodep = NULL; | |
1510 | return NULL; | |
1511 | } | |
1512 | ||
d7b62727 MW |
1513 | void __rcu **__radix_tree_next_slot(void __rcu **slot, |
1514 | struct radix_tree_iter *iter, unsigned flags) | |
148deab2 MW |
1515 | { |
1516 | unsigned tag = flags & RADIX_TREE_ITER_TAG_MASK; | |
9f418224 | 1517 | struct radix_tree_node *node; |
148deab2 MW |
1518 | |
1519 | slot = skip_siblings(&node, slot, iter); | |
1520 | ||
1521 | while (radix_tree_is_internal_node(node)) { | |
1522 | unsigned offset; | |
1523 | unsigned long next_index; | |
1524 | ||
1525 | if (node == RADIX_TREE_RETRY) | |
1526 | return slot; | |
1527 | node = entry_to_node(node); | |
268f42de | 1528 | iter->node = node; |
148deab2 MW |
1529 | iter->shift = node->shift; |
1530 | ||
1531 | if (flags & RADIX_TREE_ITER_TAGGED) { | |
1532 | offset = radix_tree_find_next_bit(node, tag, 0); | |
1533 | if (offset == RADIX_TREE_MAP_SIZE) | |
1534 | return NULL; | |
1535 | slot = &node->slots[offset]; | |
1536 | iter->index = __radix_tree_iter_add(iter, offset); | |
1537 | set_iter_tags(iter, node, offset, tag); | |
1538 | node = rcu_dereference_raw(*slot); | |
1539 | } else { | |
1540 | offset = 0; | |
1541 | slot = &node->slots[0]; | |
1542 | for (;;) { | |
1543 | node = rcu_dereference_raw(*slot); | |
1544 | if (node) | |
1545 | break; | |
1546 | slot++; | |
1547 | offset++; | |
1548 | if (offset == RADIX_TREE_MAP_SIZE) | |
1549 | return NULL; | |
1550 | } | |
1551 | iter->index = __radix_tree_iter_add(iter, offset); | |
1552 | } | |
1553 | if ((flags & RADIX_TREE_ITER_CONTIG) && (offset > 0)) | |
1554 | goto none; | |
1555 | next_index = (iter->index | shift_maxindex(iter->shift)) + 1; | |
1556 | if (next_index < iter->next_index) | |
1557 | iter->next_index = next_index; | |
1558 | } | |
1559 | ||
1560 | return slot; | |
1561 | none: | |
1562 | iter->next_index = 0; | |
1563 | return NULL; | |
1564 | } | |
1565 | EXPORT_SYMBOL(__radix_tree_next_slot); | |
1566 | #else | |
d7b62727 MW |
1567 | static void __rcu **skip_siblings(struct radix_tree_node **nodep, |
1568 | void __rcu **slot, struct radix_tree_iter *iter) | |
148deab2 MW |
1569 | { |
1570 | return slot; | |
1571 | } | |
1572 | #endif | |
1573 | ||
d7b62727 MW |
1574 | void __rcu **radix_tree_iter_resume(void __rcu **slot, |
1575 | struct radix_tree_iter *iter) | |
148deab2 MW |
1576 | { |
1577 | struct radix_tree_node *node; | |
1578 | ||
1579 | slot++; | |
1580 | iter->index = __radix_tree_iter_add(iter, 1); | |
148deab2 MW |
1581 | skip_siblings(&node, slot, iter); |
1582 | iter->next_index = iter->index; | |
1583 | iter->tags = 0; | |
1584 | return NULL; | |
1585 | } | |
1586 | EXPORT_SYMBOL(radix_tree_iter_resume); | |
1587 | ||
78c1d784 KK |
1588 | /** |
1589 | * radix_tree_next_chunk - find next chunk of slots for iteration | |
1590 | * | |
1591 | * @root: radix tree root | |
1592 | * @iter: iterator state | |
1593 | * @flags: RADIX_TREE_ITER_* flags and tag index | |
1594 | * Returns: pointer to chunk first slot, or NULL if iteration is over | |
1595 | */ | |
d7b62727 | 1596 | void __rcu **radix_tree_next_chunk(const struct radix_tree_root *root, |
78c1d784 KK |
1597 | struct radix_tree_iter *iter, unsigned flags) |
1598 | { | |
9e85d811 | 1599 | unsigned tag = flags & RADIX_TREE_ITER_TAG_MASK; |
8c1244de | 1600 | struct radix_tree_node *node, *child; |
21ef5339 | 1601 | unsigned long index, offset, maxindex; |
78c1d784 KK |
1602 | |
1603 | if ((flags & RADIX_TREE_ITER_TAGGED) && !root_tag_get(root, tag)) | |
1604 | return NULL; | |
1605 | ||
1606 | /* | |
1607 | * Catch next_index overflow after ~0UL. iter->index never overflows | |
1608 | * during iterating; it can be zero only at the beginning. | |
1609 | * And we cannot overflow iter->next_index in a single step, | |
1610 | * because RADIX_TREE_MAP_SHIFT < BITS_PER_LONG. | |
fffaee36 KK |
1611 | * |
1612 | * This condition also used by radix_tree_next_slot() to stop | |
91b9677c | 1613 | * contiguous iterating, and forbid switching to the next chunk. |
78c1d784 KK |
1614 | */ |
1615 | index = iter->next_index; | |
1616 | if (!index && iter->index) | |
1617 | return NULL; | |
1618 | ||
21ef5339 | 1619 | restart: |
9e85d811 | 1620 | radix_tree_load_root(root, &child, &maxindex); |
21ef5339 RZ |
1621 | if (index > maxindex) |
1622 | return NULL; | |
8c1244de MW |
1623 | if (!child) |
1624 | return NULL; | |
21ef5339 | 1625 | |
8c1244de | 1626 | if (!radix_tree_is_internal_node(child)) { |
78c1d784 | 1627 | /* Single-slot tree */ |
21ef5339 RZ |
1628 | iter->index = index; |
1629 | iter->next_index = maxindex + 1; | |
78c1d784 | 1630 | iter->tags = 1; |
268f42de | 1631 | iter->node = NULL; |
8c1244de | 1632 | __set_iter_shift(iter, 0); |
f8d5d0cc | 1633 | return (void __rcu **)&root->xa_head; |
8c1244de | 1634 | } |
21ef5339 | 1635 | |
8c1244de MW |
1636 | do { |
1637 | node = entry_to_node(child); | |
9e85d811 | 1638 | offset = radix_tree_descend(node, &child, index); |
21ef5339 | 1639 | |
78c1d784 | 1640 | if ((flags & RADIX_TREE_ITER_TAGGED) ? |
8c1244de | 1641 | !tag_get(node, tag, offset) : !child) { |
78c1d784 KK |
1642 | /* Hole detected */ |
1643 | if (flags & RADIX_TREE_ITER_CONTIG) | |
1644 | return NULL; | |
1645 | ||
1646 | if (flags & RADIX_TREE_ITER_TAGGED) | |
bc412fca | 1647 | offset = radix_tree_find_next_bit(node, tag, |
78c1d784 KK |
1648 | offset + 1); |
1649 | else | |
1650 | while (++offset < RADIX_TREE_MAP_SIZE) { | |
12320d0f MW |
1651 | void *slot = rcu_dereference_raw( |
1652 | node->slots[offset]); | |
02c02bf1 | 1653 | if (xa_is_sibling(slot)) |
21ef5339 RZ |
1654 | continue; |
1655 | if (slot) | |
78c1d784 KK |
1656 | break; |
1657 | } | |
8c1244de | 1658 | index &= ~node_maxindex(node); |
9e85d811 | 1659 | index += offset << node->shift; |
78c1d784 KK |
1660 | /* Overflow after ~0UL */ |
1661 | if (!index) | |
1662 | return NULL; | |
1663 | if (offset == RADIX_TREE_MAP_SIZE) | |
1664 | goto restart; | |
8c1244de | 1665 | child = rcu_dereference_raw(node->slots[offset]); |
78c1d784 KK |
1666 | } |
1667 | ||
e157b555 | 1668 | if (!child) |
78c1d784 | 1669 | goto restart; |
e157b555 MW |
1670 | if (child == RADIX_TREE_RETRY) |
1671 | break; | |
66ee620f | 1672 | } while (node->shift && radix_tree_is_internal_node(child)); |
78c1d784 KK |
1673 | |
1674 | /* Update the iterator state */ | |
8c1244de MW |
1675 | iter->index = (index &~ node_maxindex(node)) | (offset << node->shift); |
1676 | iter->next_index = (index | node_maxindex(node)) + 1; | |
268f42de | 1677 | iter->node = node; |
9e85d811 | 1678 | __set_iter_shift(iter, node->shift); |
78c1d784 | 1679 | |
148deab2 MW |
1680 | if (flags & RADIX_TREE_ITER_TAGGED) |
1681 | set_iter_tags(iter, node, offset, tag); | |
78c1d784 KK |
1682 | |
1683 | return node->slots + offset; | |
1684 | } | |
1685 | EXPORT_SYMBOL(radix_tree_next_chunk); | |
1686 | ||
1da177e4 LT |
1687 | /** |
1688 | * radix_tree_gang_lookup - perform multiple lookup on a radix tree | |
1689 | * @root: radix tree root | |
1690 | * @results: where the results of the lookup are placed | |
1691 | * @first_index: start the lookup from this key | |
1692 | * @max_items: place up to this many items at *results | |
1693 | * | |
1694 | * Performs an index-ascending scan of the tree for present items. Places | |
1695 | * them at *@results and returns the number of items which were placed at | |
1696 | * *@results. | |
1697 | * | |
1698 | * The implementation is naive. | |
7cf9c2c7 NP |
1699 | * |
1700 | * Like radix_tree_lookup, radix_tree_gang_lookup may be called under | |
1701 | * rcu_read_lock. In this case, rather than the returned results being | |
2fcd9005 MW |
1702 | * an atomic snapshot of the tree at a single point in time, the |
1703 | * semantics of an RCU protected gang lookup are as though multiple | |
1704 | * radix_tree_lookups have been issued in individual locks, and results | |
1705 | * stored in 'results'. | |
1da177e4 LT |
1706 | */ |
1707 | unsigned int | |
35534c86 | 1708 | radix_tree_gang_lookup(const struct radix_tree_root *root, void **results, |
1da177e4 LT |
1709 | unsigned long first_index, unsigned int max_items) |
1710 | { | |
cebbd29e | 1711 | struct radix_tree_iter iter; |
d7b62727 | 1712 | void __rcu **slot; |
cebbd29e | 1713 | unsigned int ret = 0; |
7cf9c2c7 | 1714 | |
cebbd29e | 1715 | if (unlikely(!max_items)) |
7cf9c2c7 | 1716 | return 0; |
1da177e4 | 1717 | |
cebbd29e | 1718 | radix_tree_for_each_slot(slot, root, &iter, first_index) { |
46437f9a | 1719 | results[ret] = rcu_dereference_raw(*slot); |
cebbd29e KK |
1720 | if (!results[ret]) |
1721 | continue; | |
b194d16c | 1722 | if (radix_tree_is_internal_node(results[ret])) { |
46437f9a MW |
1723 | slot = radix_tree_iter_retry(&iter); |
1724 | continue; | |
1725 | } | |
cebbd29e | 1726 | if (++ret == max_items) |
1da177e4 | 1727 | break; |
1da177e4 | 1728 | } |
7cf9c2c7 | 1729 | |
1da177e4 LT |
1730 | return ret; |
1731 | } | |
1732 | EXPORT_SYMBOL(radix_tree_gang_lookup); | |
1733 | ||
47feff2c NP |
1734 | /** |
1735 | * radix_tree_gang_lookup_slot - perform multiple slot lookup on radix tree | |
1736 | * @root: radix tree root | |
1737 | * @results: where the results of the lookup are placed | |
6328650b | 1738 | * @indices: where their indices should be placed (but usually NULL) |
47feff2c NP |
1739 | * @first_index: start the lookup from this key |
1740 | * @max_items: place up to this many items at *results | |
1741 | * | |
1742 | * Performs an index-ascending scan of the tree for present items. Places | |
1743 | * their slots at *@results and returns the number of items which were | |
1744 | * placed at *@results. | |
1745 | * | |
1746 | * The implementation is naive. | |
1747 | * | |
1748 | * Like radix_tree_gang_lookup as far as RCU and locking goes. Slots must | |
1749 | * be dereferenced with radix_tree_deref_slot, and if using only RCU | |
1750 | * protection, radix_tree_deref_slot may fail requiring a retry. | |
1751 | */ | |
1752 | unsigned int | |
35534c86 | 1753 | radix_tree_gang_lookup_slot(const struct radix_tree_root *root, |
d7b62727 | 1754 | void __rcu ***results, unsigned long *indices, |
47feff2c NP |
1755 | unsigned long first_index, unsigned int max_items) |
1756 | { | |
cebbd29e | 1757 | struct radix_tree_iter iter; |
d7b62727 | 1758 | void __rcu **slot; |
cebbd29e | 1759 | unsigned int ret = 0; |
47feff2c | 1760 | |
cebbd29e | 1761 | if (unlikely(!max_items)) |
47feff2c NP |
1762 | return 0; |
1763 | ||
cebbd29e KK |
1764 | radix_tree_for_each_slot(slot, root, &iter, first_index) { |
1765 | results[ret] = slot; | |
6328650b | 1766 | if (indices) |
cebbd29e KK |
1767 | indices[ret] = iter.index; |
1768 | if (++ret == max_items) | |
47feff2c | 1769 | break; |
47feff2c NP |
1770 | } |
1771 | ||
1772 | return ret; | |
1773 | } | |
1774 | EXPORT_SYMBOL(radix_tree_gang_lookup_slot); | |
1775 | ||
1da177e4 LT |
1776 | /** |
1777 | * radix_tree_gang_lookup_tag - perform multiple lookup on a radix tree | |
1778 | * based on a tag | |
1779 | * @root: radix tree root | |
1780 | * @results: where the results of the lookup are placed | |
1781 | * @first_index: start the lookup from this key | |
1782 | * @max_items: place up to this many items at *results | |
daff89f3 | 1783 | * @tag: the tag index (< RADIX_TREE_MAX_TAGS) |
1da177e4 LT |
1784 | * |
1785 | * Performs an index-ascending scan of the tree for present items which | |
1786 | * have the tag indexed by @tag set. Places the items at *@results and | |
1787 | * returns the number of items which were placed at *@results. | |
1788 | */ | |
1789 | unsigned int | |
35534c86 | 1790 | radix_tree_gang_lookup_tag(const struct radix_tree_root *root, void **results, |
daff89f3 JC |
1791 | unsigned long first_index, unsigned int max_items, |
1792 | unsigned int tag) | |
1da177e4 | 1793 | { |
cebbd29e | 1794 | struct radix_tree_iter iter; |
d7b62727 | 1795 | void __rcu **slot; |
cebbd29e | 1796 | unsigned int ret = 0; |
612d6c19 | 1797 | |
cebbd29e | 1798 | if (unlikely(!max_items)) |
7cf9c2c7 NP |
1799 | return 0; |
1800 | ||
cebbd29e | 1801 | radix_tree_for_each_tagged(slot, root, &iter, first_index, tag) { |
46437f9a | 1802 | results[ret] = rcu_dereference_raw(*slot); |
cebbd29e KK |
1803 | if (!results[ret]) |
1804 | continue; | |
b194d16c | 1805 | if (radix_tree_is_internal_node(results[ret])) { |
46437f9a MW |
1806 | slot = radix_tree_iter_retry(&iter); |
1807 | continue; | |
1808 | } | |
cebbd29e | 1809 | if (++ret == max_items) |
1da177e4 | 1810 | break; |
1da177e4 | 1811 | } |
7cf9c2c7 | 1812 | |
1da177e4 LT |
1813 | return ret; |
1814 | } | |
1815 | EXPORT_SYMBOL(radix_tree_gang_lookup_tag); | |
1816 | ||
47feff2c NP |
1817 | /** |
1818 | * radix_tree_gang_lookup_tag_slot - perform multiple slot lookup on a | |
1819 | * radix tree based on a tag | |
1820 | * @root: radix tree root | |
1821 | * @results: where the results of the lookup are placed | |
1822 | * @first_index: start the lookup from this key | |
1823 | * @max_items: place up to this many items at *results | |
1824 | * @tag: the tag index (< RADIX_TREE_MAX_TAGS) | |
1825 | * | |
1826 | * Performs an index-ascending scan of the tree for present items which | |
1827 | * have the tag indexed by @tag set. Places the slots at *@results and | |
1828 | * returns the number of slots which were placed at *@results. | |
1829 | */ | |
1830 | unsigned int | |
35534c86 | 1831 | radix_tree_gang_lookup_tag_slot(const struct radix_tree_root *root, |
d7b62727 | 1832 | void __rcu ***results, unsigned long first_index, |
35534c86 | 1833 | unsigned int max_items, unsigned int tag) |
47feff2c | 1834 | { |
cebbd29e | 1835 | struct radix_tree_iter iter; |
d7b62727 | 1836 | void __rcu **slot; |
cebbd29e | 1837 | unsigned int ret = 0; |
47feff2c | 1838 | |
cebbd29e | 1839 | if (unlikely(!max_items)) |
47feff2c NP |
1840 | return 0; |
1841 | ||
cebbd29e KK |
1842 | radix_tree_for_each_tagged(slot, root, &iter, first_index, tag) { |
1843 | results[ret] = slot; | |
1844 | if (++ret == max_items) | |
47feff2c | 1845 | break; |
47feff2c NP |
1846 | } |
1847 | ||
1848 | return ret; | |
1849 | } | |
1850 | EXPORT_SYMBOL(radix_tree_gang_lookup_tag_slot); | |
1851 | ||
139e5616 JW |
1852 | /** |
1853 | * __radix_tree_delete_node - try to free node after clearing a slot | |
1854 | * @root: radix tree root | |
139e5616 | 1855 | * @node: node containing @index |
ea07b862 | 1856 | * @update_node: callback for changing leaf nodes |
139e5616 JW |
1857 | * |
1858 | * After clearing the slot at @index in @node from radix tree | |
1859 | * rooted at @root, call this function to attempt freeing the | |
1860 | * node and shrinking the tree. | |
139e5616 | 1861 | */ |
14b46879 | 1862 | void __radix_tree_delete_node(struct radix_tree_root *root, |
ea07b862 | 1863 | struct radix_tree_node *node, |
c7df8ad2 | 1864 | radix_tree_update_node_t update_node) |
139e5616 | 1865 | { |
c7df8ad2 | 1866 | delete_node(root, node, update_node); |
139e5616 JW |
1867 | } |
1868 | ||
0ac398ef | 1869 | static bool __radix_tree_delete(struct radix_tree_root *root, |
d7b62727 | 1870 | struct radix_tree_node *node, void __rcu **slot) |
0ac398ef | 1871 | { |
0a835c4f | 1872 | void *old = rcu_dereference_raw(*slot); |
01959dfe | 1873 | int values = xa_is_value(old) ? -1 : 0; |
0ac398ef MW |
1874 | unsigned offset = get_slot_offset(node, slot); |
1875 | int tag; | |
1876 | ||
0a835c4f MW |
1877 | if (is_idr(root)) |
1878 | node_tag_set(root, node, IDR_FREE, offset); | |
1879 | else | |
1880 | for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) | |
1881 | node_tag_clear(root, node, tag, offset); | |
0ac398ef | 1882 | |
01959dfe | 1883 | replace_slot(slot, NULL, node, -1, values); |
c7df8ad2 | 1884 | return node && delete_node(root, node, NULL); |
0ac398ef MW |
1885 | } |
1886 | ||
1da177e4 | 1887 | /** |
0ac398ef MW |
1888 | * radix_tree_iter_delete - delete the entry at this iterator position |
1889 | * @root: radix tree root | |
1890 | * @iter: iterator state | |
1891 | * @slot: pointer to slot | |
1da177e4 | 1892 | * |
0ac398ef MW |
1893 | * Delete the entry at the position currently pointed to by the iterator. |
1894 | * This may result in the current node being freed; if it is, the iterator | |
1895 | * is advanced so that it will not reference the freed memory. This | |
1896 | * function may be called without any locking if there are no other threads | |
1897 | * which can access this tree. | |
1898 | */ | |
1899 | void radix_tree_iter_delete(struct radix_tree_root *root, | |
d7b62727 | 1900 | struct radix_tree_iter *iter, void __rcu **slot) |
0ac398ef MW |
1901 | { |
1902 | if (__radix_tree_delete(root, iter->node, slot)) | |
1903 | iter->index = iter->next_index; | |
1904 | } | |
d1b48c1e | 1905 | EXPORT_SYMBOL(radix_tree_iter_delete); |
0ac398ef MW |
1906 | |
1907 | /** | |
1908 | * radix_tree_delete_item - delete an item from a radix tree | |
1909 | * @root: radix tree root | |
1910 | * @index: index key | |
1911 | * @item: expected item | |
1da177e4 | 1912 | * |
0ac398ef | 1913 | * Remove @item at @index from the radix tree rooted at @root. |
1da177e4 | 1914 | * |
0ac398ef MW |
1915 | * Return: the deleted entry, or %NULL if it was not present |
1916 | * or the entry at the given @index was not @item. | |
1da177e4 | 1917 | */ |
53c59f26 JW |
1918 | void *radix_tree_delete_item(struct radix_tree_root *root, |
1919 | unsigned long index, void *item) | |
1da177e4 | 1920 | { |
0a835c4f | 1921 | struct radix_tree_node *node = NULL; |
7a4deea1 | 1922 | void __rcu **slot = NULL; |
139e5616 | 1923 | void *entry; |
1da177e4 | 1924 | |
139e5616 | 1925 | entry = __radix_tree_lookup(root, index, &node, &slot); |
7a4deea1 MW |
1926 | if (!slot) |
1927 | return NULL; | |
0a835c4f MW |
1928 | if (!entry && (!is_idr(root) || node_tag_get(root, node, IDR_FREE, |
1929 | get_slot_offset(node, slot)))) | |
139e5616 | 1930 | return NULL; |
1da177e4 | 1931 | |
139e5616 JW |
1932 | if (item && entry != item) |
1933 | return NULL; | |
1934 | ||
0ac398ef | 1935 | __radix_tree_delete(root, node, slot); |
612d6c19 | 1936 | |
139e5616 | 1937 | return entry; |
1da177e4 | 1938 | } |
53c59f26 JW |
1939 | EXPORT_SYMBOL(radix_tree_delete_item); |
1940 | ||
1941 | /** | |
0ac398ef MW |
1942 | * radix_tree_delete - delete an entry from a radix tree |
1943 | * @root: radix tree root | |
1944 | * @index: index key | |
53c59f26 | 1945 | * |
0ac398ef | 1946 | * Remove the entry at @index from the radix tree rooted at @root. |
53c59f26 | 1947 | * |
0ac398ef | 1948 | * Return: The deleted entry, or %NULL if it was not present. |
53c59f26 JW |
1949 | */ |
1950 | void *radix_tree_delete(struct radix_tree_root *root, unsigned long index) | |
1951 | { | |
1952 | return radix_tree_delete_item(root, index, NULL); | |
1953 | } | |
1da177e4 LT |
1954 | EXPORT_SYMBOL(radix_tree_delete); |
1955 | ||
d3798ae8 JW |
1956 | void radix_tree_clear_tags(struct radix_tree_root *root, |
1957 | struct radix_tree_node *node, | |
d7b62727 | 1958 | void __rcu **slot) |
d604c324 | 1959 | { |
d604c324 MW |
1960 | if (node) { |
1961 | unsigned int tag, offset = get_slot_offset(node, slot); | |
1962 | for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) | |
1963 | node_tag_clear(root, node, tag, offset); | |
1964 | } else { | |
0a835c4f | 1965 | root_tag_clear_all(root); |
d604c324 | 1966 | } |
d604c324 MW |
1967 | } |
1968 | ||
1da177e4 LT |
1969 | /** |
1970 | * radix_tree_tagged - test whether any items in the tree are tagged | |
1971 | * @root: radix tree root | |
1972 | * @tag: tag to test | |
1973 | */ | |
35534c86 | 1974 | int radix_tree_tagged(const struct radix_tree_root *root, unsigned int tag) |
1da177e4 | 1975 | { |
612d6c19 | 1976 | return root_tag_get(root, tag); |
1da177e4 LT |
1977 | } |
1978 | EXPORT_SYMBOL(radix_tree_tagged); | |
1979 | ||
0a835c4f MW |
1980 | /** |
1981 | * idr_preload - preload for idr_alloc() | |
1982 | * @gfp_mask: allocation mask to use for preloading | |
1983 | * | |
1984 | * Preallocate memory to use for the next call to idr_alloc(). This function | |
1985 | * returns with preemption disabled. It will be enabled by idr_preload_end(). | |
1986 | */ | |
1987 | void idr_preload(gfp_t gfp_mask) | |
1988 | { | |
bc9ae224 ED |
1989 | if (__radix_tree_preload(gfp_mask, IDR_PRELOAD_SIZE)) |
1990 | preempt_disable(); | |
0a835c4f MW |
1991 | } |
1992 | EXPORT_SYMBOL(idr_preload); | |
1993 | ||
460488c5 | 1994 | void __rcu **idr_get_free(struct radix_tree_root *root, |
388f79fd CM |
1995 | struct radix_tree_iter *iter, gfp_t gfp, |
1996 | unsigned long max) | |
0a835c4f MW |
1997 | { |
1998 | struct radix_tree_node *node = NULL, *child; | |
f8d5d0cc | 1999 | void __rcu **slot = (void __rcu **)&root->xa_head; |
0a835c4f | 2000 | unsigned long maxindex, start = iter->next_index; |
0a835c4f MW |
2001 | unsigned int shift, offset = 0; |
2002 | ||
2003 | grow: | |
2004 | shift = radix_tree_load_root(root, &child, &maxindex); | |
2005 | if (!radix_tree_tagged(root, IDR_FREE)) | |
2006 | start = max(start, maxindex + 1); | |
2007 | if (start > max) | |
2008 | return ERR_PTR(-ENOSPC); | |
2009 | ||
2010 | if (start > maxindex) { | |
2011 | int error = radix_tree_extend(root, gfp, start, shift); | |
2012 | if (error < 0) | |
2013 | return ERR_PTR(error); | |
2014 | shift = error; | |
f8d5d0cc | 2015 | child = rcu_dereference_raw(root->xa_head); |
0a835c4f | 2016 | } |
66ee620f MW |
2017 | if (start == 0 && shift == 0) |
2018 | shift = RADIX_TREE_MAP_SHIFT; | |
0a835c4f MW |
2019 | |
2020 | while (shift) { | |
2021 | shift -= RADIX_TREE_MAP_SHIFT; | |
2022 | if (child == NULL) { | |
2023 | /* Have to add a child node. */ | |
d58275bc MW |
2024 | child = radix_tree_node_alloc(gfp, node, root, shift, |
2025 | offset, 0, 0); | |
0a835c4f MW |
2026 | if (!child) |
2027 | return ERR_PTR(-ENOMEM); | |
2028 | all_tag_set(child, IDR_FREE); | |
2029 | rcu_assign_pointer(*slot, node_to_entry(child)); | |
2030 | if (node) | |
2031 | node->count++; | |
2032 | } else if (!radix_tree_is_internal_node(child)) | |
2033 | break; | |
2034 | ||
2035 | node = entry_to_node(child); | |
2036 | offset = radix_tree_descend(node, &child, start); | |
2037 | if (!tag_get(node, IDR_FREE, offset)) { | |
2038 | offset = radix_tree_find_next_bit(node, IDR_FREE, | |
2039 | offset + 1); | |
2040 | start = next_index(start, node, offset); | |
2041 | if (start > max) | |
2042 | return ERR_PTR(-ENOSPC); | |
2043 | while (offset == RADIX_TREE_MAP_SIZE) { | |
2044 | offset = node->offset + 1; | |
2045 | node = node->parent; | |
2046 | if (!node) | |
2047 | goto grow; | |
2048 | shift = node->shift; | |
2049 | } | |
2050 | child = rcu_dereference_raw(node->slots[offset]); | |
2051 | } | |
2052 | slot = &node->slots[offset]; | |
2053 | } | |
2054 | ||
2055 | iter->index = start; | |
2056 | if (node) | |
2057 | iter->next_index = 1 + min(max, (start | node_maxindex(node))); | |
2058 | else | |
2059 | iter->next_index = 1; | |
2060 | iter->node = node; | |
2061 | __set_iter_shift(iter, shift); | |
2062 | set_iter_tags(iter, node, offset, IDR_FREE); | |
2063 | ||
2064 | return slot; | |
2065 | } | |
2066 | ||
2067 | /** | |
2068 | * idr_destroy - release all internal memory from an IDR | |
2069 | * @idr: idr handle | |
2070 | * | |
2071 | * After this function is called, the IDR is empty, and may be reused or | |
2072 | * the data structure containing it may be freed. | |
2073 | * | |
2074 | * A typical clean-up sequence for objects stored in an idr tree will use | |
2075 | * idr_for_each() to free all objects, if necessary, then idr_destroy() to | |
2076 | * free the memory used to keep track of those objects. | |
2077 | */ | |
2078 | void idr_destroy(struct idr *idr) | |
2079 | { | |
f8d5d0cc | 2080 | struct radix_tree_node *node = rcu_dereference_raw(idr->idr_rt.xa_head); |
0a835c4f MW |
2081 | if (radix_tree_is_internal_node(node)) |
2082 | radix_tree_free_nodes(node); | |
f8d5d0cc | 2083 | idr->idr_rt.xa_head = NULL; |
0a835c4f MW |
2084 | root_tag_set(&idr->idr_rt, IDR_FREE); |
2085 | } | |
2086 | EXPORT_SYMBOL(idr_destroy); | |
2087 | ||
1da177e4 | 2088 | static void |
449dd698 | 2089 | radix_tree_node_ctor(void *arg) |
1da177e4 | 2090 | { |
449dd698 JW |
2091 | struct radix_tree_node *node = arg; |
2092 | ||
2093 | memset(node, 0, sizeof(*node)); | |
2094 | INIT_LIST_HEAD(&node->private_list); | |
1da177e4 LT |
2095 | } |
2096 | ||
c78c66d1 KS |
2097 | static __init unsigned long __maxindex(unsigned int height) |
2098 | { | |
2099 | unsigned int width = height * RADIX_TREE_MAP_SHIFT; | |
2100 | int shift = RADIX_TREE_INDEX_BITS - width; | |
2101 | ||
2102 | if (shift < 0) | |
2103 | return ~0UL; | |
2104 | if (shift >= BITS_PER_LONG) | |
2105 | return 0UL; | |
2106 | return ~0UL >> shift; | |
2107 | } | |
2108 | ||
2109 | static __init void radix_tree_init_maxnodes(void) | |
2110 | { | |
2111 | unsigned long height_to_maxindex[RADIX_TREE_MAX_PATH + 1]; | |
2112 | unsigned int i, j; | |
2113 | ||
2114 | for (i = 0; i < ARRAY_SIZE(height_to_maxindex); i++) | |
2115 | height_to_maxindex[i] = __maxindex(i); | |
2116 | for (i = 0; i < ARRAY_SIZE(height_to_maxnodes); i++) { | |
2117 | for (j = i; j > 0; j--) | |
2118 | height_to_maxnodes[i] += height_to_maxindex[j - 1] + 1; | |
2119 | } | |
2120 | } | |
2121 | ||
d544abd5 | 2122 | static int radix_tree_cpu_dead(unsigned int cpu) |
1da177e4 | 2123 | { |
2fcd9005 MW |
2124 | struct radix_tree_preload *rtp; |
2125 | struct radix_tree_node *node; | |
2126 | ||
2127 | /* Free per-cpu pool of preloaded nodes */ | |
d544abd5 SAS |
2128 | rtp = &per_cpu(radix_tree_preloads, cpu); |
2129 | while (rtp->nr) { | |
2130 | node = rtp->nodes; | |
1293d5c5 | 2131 | rtp->nodes = node->parent; |
d544abd5 SAS |
2132 | kmem_cache_free(radix_tree_node_cachep, node); |
2133 | rtp->nr--; | |
2fcd9005 | 2134 | } |
d544abd5 | 2135 | return 0; |
1da177e4 | 2136 | } |
1da177e4 LT |
2137 | |
2138 | void __init radix_tree_init(void) | |
2139 | { | |
d544abd5 | 2140 | int ret; |
7e784422 MH |
2141 | |
2142 | BUILD_BUG_ON(RADIX_TREE_MAX_TAGS + __GFP_BITS_SHIFT > 32); | |
fa290cda | 2143 | BUILD_BUG_ON(ROOT_IS_IDR & ~GFP_ZONEMASK); |
02c02bf1 | 2144 | BUILD_BUG_ON(XA_CHUNK_SIZE > 255); |
1da177e4 LT |
2145 | radix_tree_node_cachep = kmem_cache_create("radix_tree_node", |
2146 | sizeof(struct radix_tree_node), 0, | |
488514d1 CL |
2147 | SLAB_PANIC | SLAB_RECLAIM_ACCOUNT, |
2148 | radix_tree_node_ctor); | |
c78c66d1 | 2149 | radix_tree_init_maxnodes(); |
d544abd5 SAS |
2150 | ret = cpuhp_setup_state_nocalls(CPUHP_RADIX_DEAD, "lib/radix:dead", |
2151 | NULL, radix_tree_cpu_dead); | |
2152 | WARN_ON(ret < 0); | |
1da177e4 | 2153 | } |