Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Copyright (C) 2001 Momchil Velikov | |
3 | * Portions Copyright (C) 2001 Christoph Hellwig | |
7cf9c2c7 | 4 | * Copyright (C) 2006 Nick Piggin |
78c1d784 | 5 | * Copyright (C) 2012 Konstantin Khlebnikov |
1da177e4 LT |
6 | * |
7 | * This program is free software; you can redistribute it and/or | |
8 | * modify it under the terms of the GNU General Public License as | |
9 | * published by the Free Software Foundation; either version 2, or (at | |
10 | * your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, but | |
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | * General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program; if not, write to the Free Software | |
19 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
20 | */ | |
21 | #ifndef _LINUX_RADIX_TREE_H | |
22 | #define _LINUX_RADIX_TREE_H | |
23 | ||
f67c07f0 | 24 | #include <linux/bitops.h> |
187f1882 | 25 | #include <linux/bug.h> |
7cf9c2c7 | 26 | #include <linux/kernel.h> |
15f2e88d MW |
27 | #include <linux/list.h> |
28 | #include <linux/preempt.h> | |
7cf9c2c7 | 29 | #include <linux/rcupdate.h> |
15f2e88d MW |
30 | #include <linux/spinlock.h> |
31 | #include <linux/types.h> | |
7cf9c2c7 NP |
32 | |
33 | /* | |
3bcadd6f MW |
34 | * The bottom two bits of the slot determine how the remaining bits in the |
35 | * slot are interpreted: | |
7cf9c2c7 | 36 | * |
3bcadd6f MW |
37 | * 00 - data pointer |
38 | * 01 - internal entry | |
39 | * 10 - exceptional entry | |
a23216a2 | 40 | * 11 - this bit combination is currently unused/reserved |
3bcadd6f MW |
41 | * |
42 | * The internal entry may be a pointer to the next level in the tree, a | |
43 | * sibling entry, or an indicator that the entry in this slot has been moved | |
44 | * to another location in the tree and the lookup should be restarted. While | |
45 | * NULL fits the 'data pointer' pattern, it means that there is no entry in | |
46 | * the tree for this index (no matter what level of the tree it is found at). | |
47 | * This means that you cannot store NULL in the tree as a value for the index. | |
7cf9c2c7 | 48 | */ |
3bcadd6f MW |
49 | #define RADIX_TREE_ENTRY_MASK 3UL |
50 | #define RADIX_TREE_INTERNAL_NODE 1UL | |
30ff46cc | 51 | |
6328650b | 52 | /* |
3bcadd6f MW |
53 | * Most users of the radix tree store pointers but shmem/tmpfs stores swap |
54 | * entries in the same tree. They are marked as exceptional entries to | |
55 | * distinguish them from pointers to struct page. | |
6328650b HD |
56 | * EXCEPTIONAL_ENTRY tests the bit, EXCEPTIONAL_SHIFT shifts content past it. |
57 | */ | |
58 | #define RADIX_TREE_EXCEPTIONAL_ENTRY 2 | |
59 | #define RADIX_TREE_EXCEPTIONAL_SHIFT 2 | |
7cf9c2c7 | 60 | |
3bcadd6f | 61 | static inline bool radix_tree_is_internal_node(void *ptr) |
7cf9c2c7 | 62 | { |
3bcadd6f MW |
63 | return ((unsigned long)ptr & RADIX_TREE_ENTRY_MASK) == |
64 | RADIX_TREE_INTERNAL_NODE; | |
7cf9c2c7 NP |
65 | } |
66 | ||
67 | /*** radix-tree API starts here ***/ | |
1da177e4 | 68 | |
f446daae | 69 | #define RADIX_TREE_MAX_TAGS 3 |
612d6c19 | 70 | |
97d778b2 | 71 | #ifndef RADIX_TREE_MAP_SHIFT |
139e5616 | 72 | #define RADIX_TREE_MAP_SHIFT (CONFIG_BASE_SMALL ? 4 : 6) |
139e5616 JW |
73 | #endif |
74 | ||
75 | #define RADIX_TREE_MAP_SIZE (1UL << RADIX_TREE_MAP_SHIFT) | |
76 | #define RADIX_TREE_MAP_MASK (RADIX_TREE_MAP_SIZE-1) | |
77 | ||
78 | #define RADIX_TREE_TAG_LONGS \ | |
79 | ((RADIX_TREE_MAP_SIZE + BITS_PER_LONG - 1) / BITS_PER_LONG) | |
80 | ||
449dd698 JW |
81 | #define RADIX_TREE_INDEX_BITS (8 /* CHAR_BIT */ * sizeof(unsigned long)) |
82 | #define RADIX_TREE_MAX_PATH (DIV_ROUND_UP(RADIX_TREE_INDEX_BITS, \ | |
83 | RADIX_TREE_MAP_SHIFT)) | |
84 | ||
e157b555 MW |
85 | /* |
86 | * @count is the count of every non-NULL element in the ->slots array | |
87 | * whether that is an exceptional entry, a retry entry, a user pointer, | |
88 | * a sibling entry or a pointer to the next level of the tree. | |
89 | * @exceptional is the count of every element in ->slots which is | |
90 | * either radix_tree_exceptional_entry() or is a sibling entry for an | |
91 | * exceptional entry. | |
92 | */ | |
139e5616 | 93 | struct radix_tree_node { |
f7942430 JW |
94 | unsigned char shift; /* Bits remaining in each slot */ |
95 | unsigned char offset; /* Slot offset in parent */ | |
14b46879 | 96 | unsigned char count; /* Total entry count */ |
f7942430 | 97 | unsigned char exceptional; /* Exceptional entry count */ |
91d9c05a | 98 | struct radix_tree_node *parent; /* Used when ascending tree */ |
d58275bc | 99 | struct radix_tree_root *root; /* The tree we belong to */ |
139e5616 | 100 | union { |
91d9c05a MW |
101 | struct list_head private_list; /* For tree user */ |
102 | struct rcu_head rcu_head; /* Used when freeing node */ | |
139e5616 JW |
103 | }; |
104 | void __rcu *slots[RADIX_TREE_MAP_SIZE]; | |
105 | unsigned long tags[RADIX_TREE_MAX_TAGS][RADIX_TREE_TAG_LONGS]; | |
106 | }; | |
107 | ||
0a835c4f MW |
108 | /* The top bits of gfp_mask are used to store the root tags and the IDR flag */ |
109 | #define ROOT_IS_IDR ((__force gfp_t)(1 << __GFP_BITS_SHIFT)) | |
110 | #define ROOT_TAG_SHIFT (__GFP_BITS_SHIFT + 1) | |
111 | ||
1da177e4 | 112 | struct radix_tree_root { |
fd4f2df2 | 113 | gfp_t gfp_mask; |
a1115570 | 114 | struct radix_tree_node __rcu *rnode; |
1da177e4 LT |
115 | }; |
116 | ||
117 | #define RADIX_TREE_INIT(mask) { \ | |
1da177e4 LT |
118 | .gfp_mask = (mask), \ |
119 | .rnode = NULL, \ | |
120 | } | |
121 | ||
122 | #define RADIX_TREE(name, mask) \ | |
123 | struct radix_tree_root name = RADIX_TREE_INIT(mask) | |
124 | ||
125 | #define INIT_RADIX_TREE(root, mask) \ | |
126 | do { \ | |
1da177e4 LT |
127 | (root)->gfp_mask = (mask); \ |
128 | (root)->rnode = NULL; \ | |
129 | } while (0) | |
130 | ||
35534c86 | 131 | static inline bool radix_tree_empty(const struct radix_tree_root *root) |
e9256efc MW |
132 | { |
133 | return root->rnode == NULL; | |
134 | } | |
135 | ||
268f42de MW |
136 | /** |
137 | * struct radix_tree_iter - radix tree iterator state | |
138 | * | |
139 | * @index: index of current slot | |
140 | * @next_index: one beyond the last index for this chunk | |
141 | * @tags: bit-mask for tag-iterating | |
142 | * @node: node that contains current slot | |
143 | * @shift: shift for the node that holds our slots | |
144 | * | |
145 | * This radix tree iterator works in terms of "chunks" of slots. A chunk is a | |
146 | * subinterval of slots contained within one radix tree leaf node. It is | |
147 | * described by a pointer to its first slot and a struct radix_tree_iter | |
148 | * which holds the chunk's position in the tree and its size. For tagged | |
149 | * iteration radix_tree_iter also holds the slots' bit-mask for one chosen | |
150 | * radix tree tag. | |
151 | */ | |
152 | struct radix_tree_iter { | |
153 | unsigned long index; | |
154 | unsigned long next_index; | |
155 | unsigned long tags; | |
156 | struct radix_tree_node *node; | |
157 | #ifdef CONFIG_RADIX_TREE_MULTIORDER | |
158 | unsigned int shift; | |
159 | #endif | |
160 | }; | |
161 | ||
162 | static inline unsigned int iter_shift(const struct radix_tree_iter *iter) | |
163 | { | |
164 | #ifdef CONFIG_RADIX_TREE_MULTIORDER | |
165 | return iter->shift; | |
166 | #else | |
167 | return 0; | |
168 | #endif | |
169 | } | |
170 | ||
7cf9c2c7 NP |
171 | /** |
172 | * Radix-tree synchronization | |
173 | * | |
174 | * The radix-tree API requires that users provide all synchronisation (with | |
175 | * specific exceptions, noted below). | |
176 | * | |
177 | * Synchronization of access to the data items being stored in the tree, and | |
178 | * management of their lifetimes must be completely managed by API users. | |
179 | * | |
180 | * For API usage, in general, | |
59c51591 | 181 | * - any function _modifying_ the tree or tags (inserting or deleting |
eb8dc5e7 | 182 | * items, setting or clearing tags) must exclude other modifications, and |
7cf9c2c7 | 183 | * exclude any functions reading the tree. |
59c51591 | 184 | * - any function _reading_ the tree or tags (looking up items or tags, |
7cf9c2c7 NP |
185 | * gang lookups) must exclude modifications to the tree, but may occur |
186 | * concurrently with other readers. | |
187 | * | |
188 | * The notable exceptions to this rule are the following functions: | |
139e5616 | 189 | * __radix_tree_lookup |
7cf9c2c7 | 190 | * radix_tree_lookup |
47feff2c | 191 | * radix_tree_lookup_slot |
7cf9c2c7 NP |
192 | * radix_tree_tag_get |
193 | * radix_tree_gang_lookup | |
47feff2c | 194 | * radix_tree_gang_lookup_slot |
7cf9c2c7 | 195 | * radix_tree_gang_lookup_tag |
47feff2c | 196 | * radix_tree_gang_lookup_tag_slot |
7cf9c2c7 NP |
197 | * radix_tree_tagged |
198 | * | |
243c2137 | 199 | * The first 8 functions are able to be called locklessly, using RCU. The |
7cf9c2c7 NP |
200 | * caller must ensure calls to these functions are made within rcu_read_lock() |
201 | * regions. Other readers (lock-free or otherwise) and modifications may be | |
202 | * running concurrently. | |
203 | * | |
204 | * It is still required that the caller manage the synchronization and lifetimes | |
205 | * of the items. So if RCU lock-free lookups are used, typically this would mean | |
206 | * that the items have their own locks, or are amenable to lock-free access; and | |
207 | * that the items are freed by RCU (or only freed after having been deleted from | |
208 | * the radix tree *and* a synchronize_rcu() grace period). | |
209 | * | |
210 | * (Note, rcu_assign_pointer and rcu_dereference are not needed to control | |
211 | * access to data items when inserting into or looking up from the radix tree) | |
212 | * | |
ce82653d DH |
213 | * Note that the value returned by radix_tree_tag_get() may not be relied upon |
214 | * if only the RCU read lock is held. Functions to set/clear tags and to | |
215 | * delete nodes running concurrently with it may affect its result such that | |
216 | * two consecutive reads in the same locked section may return different | |
217 | * values. If reliability is required, modification functions must also be | |
218 | * excluded from concurrency. | |
219 | * | |
7cf9c2c7 NP |
220 | * radix_tree_tagged is able to be called without locking or RCU. |
221 | */ | |
222 | ||
223 | /** | |
d7b62727 MW |
224 | * radix_tree_deref_slot - dereference a slot |
225 | * @slot: slot pointer, returned by radix_tree_lookup_slot | |
7cf9c2c7 NP |
226 | * |
227 | * For use with radix_tree_lookup_slot(). Caller must hold tree at least read | |
27d20fdd NP |
228 | * locked across slot lookup and dereference. Not required if write lock is |
229 | * held (ie. items cannot be concurrently inserted). | |
230 | * | |
231 | * radix_tree_deref_retry must be used to confirm validity of the pointer if | |
232 | * only the read lock is held. | |
d7b62727 MW |
233 | * |
234 | * Return: entry stored in that slot. | |
7cf9c2c7 | 235 | */ |
d7b62727 | 236 | static inline void *radix_tree_deref_slot(void __rcu **slot) |
7cf9c2c7 | 237 | { |
d7b62727 | 238 | return rcu_dereference(*slot); |
7cf9c2c7 | 239 | } |
27d20fdd | 240 | |
29c1f677 | 241 | /** |
d7b62727 MW |
242 | * radix_tree_deref_slot_protected - dereference a slot with tree lock held |
243 | * @slot: slot pointer, returned by radix_tree_lookup_slot | |
244 | * | |
245 | * Similar to radix_tree_deref_slot. The caller does not hold the RCU read | |
246 | * lock but it must hold the tree lock to prevent parallel updates. | |
29c1f677 | 247 | * |
d7b62727 | 248 | * Return: entry stored in that slot. |
29c1f677 | 249 | */ |
d7b62727 | 250 | static inline void *radix_tree_deref_slot_protected(void __rcu **slot, |
29c1f677 MG |
251 | spinlock_t *treelock) |
252 | { | |
d7b62727 | 253 | return rcu_dereference_protected(*slot, lockdep_is_held(treelock)); |
29c1f677 MG |
254 | } |
255 | ||
27d20fdd NP |
256 | /** |
257 | * radix_tree_deref_retry - check radix_tree_deref_slot | |
258 | * @arg: pointer returned by radix_tree_deref_slot | |
259 | * Returns: 0 if retry is not required, otherwise retry is required | |
260 | * | |
261 | * radix_tree_deref_retry must be used with radix_tree_deref_slot. | |
262 | */ | |
263 | static inline int radix_tree_deref_retry(void *arg) | |
264 | { | |
b194d16c | 265 | return unlikely(radix_tree_is_internal_node(arg)); |
27d20fdd NP |
266 | } |
267 | ||
6328650b HD |
268 | /** |
269 | * radix_tree_exceptional_entry - radix_tree_deref_slot gave exceptional entry? | |
270 | * @arg: value returned by radix_tree_deref_slot | |
271 | * Returns: 0 if well-aligned pointer, non-0 if exceptional entry. | |
272 | */ | |
273 | static inline int radix_tree_exceptional_entry(void *arg) | |
274 | { | |
275 | /* Not unlikely because radix_tree_exception often tested first */ | |
276 | return (unsigned long)arg & RADIX_TREE_EXCEPTIONAL_ENTRY; | |
277 | } | |
278 | ||
279 | /** | |
280 | * radix_tree_exception - radix_tree_deref_slot returned either exception? | |
281 | * @arg: value returned by radix_tree_deref_slot | |
282 | * Returns: 0 if well-aligned pointer, non-0 if either kind of exception. | |
283 | */ | |
284 | static inline int radix_tree_exception(void *arg) | |
285 | { | |
3bcadd6f | 286 | return unlikely((unsigned long)arg & RADIX_TREE_ENTRY_MASK); |
6328650b HD |
287 | } |
288 | ||
d7b62727 | 289 | int __radix_tree_create(struct radix_tree_root *, unsigned long index, |
e6145236 | 290 | unsigned order, struct radix_tree_node **nodep, |
d7b62727 | 291 | void __rcu ***slotp); |
e6145236 MW |
292 | int __radix_tree_insert(struct radix_tree_root *, unsigned long index, |
293 | unsigned order, void *); | |
294 | static inline int radix_tree_insert(struct radix_tree_root *root, | |
295 | unsigned long index, void *entry) | |
296 | { | |
297 | return __radix_tree_insert(root, index, 0, entry); | |
298 | } | |
35534c86 | 299 | void *__radix_tree_lookup(const struct radix_tree_root *, unsigned long index, |
d7b62727 | 300 | struct radix_tree_node **nodep, void __rcu ***slotp); |
35534c86 | 301 | void *radix_tree_lookup(const struct radix_tree_root *, unsigned long); |
d7b62727 MW |
302 | void __rcu **radix_tree_lookup_slot(const struct radix_tree_root *, |
303 | unsigned long index); | |
c7df8ad2 | 304 | typedef void (*radix_tree_update_node_t)(struct radix_tree_node *); |
d7b62727 MW |
305 | void __radix_tree_replace(struct radix_tree_root *, struct radix_tree_node *, |
306 | void __rcu **slot, void *entry, | |
c7df8ad2 | 307 | radix_tree_update_node_t update_node); |
e157b555 | 308 | void radix_tree_iter_replace(struct radix_tree_root *, |
d7b62727 MW |
309 | const struct radix_tree_iter *, void __rcu **slot, void *entry); |
310 | void radix_tree_replace_slot(struct radix_tree_root *, | |
311 | void __rcu **slot, void *entry); | |
312 | void __radix_tree_delete_node(struct radix_tree_root *, | |
313 | struct radix_tree_node *, | |
c7df8ad2 | 314 | radix_tree_update_node_t update_node); |
0ac398ef | 315 | void radix_tree_iter_delete(struct radix_tree_root *, |
d7b62727 | 316 | struct radix_tree_iter *iter, void __rcu **slot); |
53c59f26 | 317 | void *radix_tree_delete_item(struct radix_tree_root *, unsigned long, void *); |
1da177e4 | 318 | void *radix_tree_delete(struct radix_tree_root *, unsigned long); |
d7b62727 MW |
319 | void radix_tree_clear_tags(struct radix_tree_root *, struct radix_tree_node *, |
320 | void __rcu **slot); | |
35534c86 | 321 | unsigned int radix_tree_gang_lookup(const struct radix_tree_root *, |
d604c324 MW |
322 | void **results, unsigned long first_index, |
323 | unsigned int max_items); | |
35534c86 | 324 | unsigned int radix_tree_gang_lookup_slot(const struct radix_tree_root *, |
d7b62727 | 325 | void __rcu ***results, unsigned long *indices, |
47feff2c | 326 | unsigned long first_index, unsigned int max_items); |
dd0fc66f | 327 | int radix_tree_preload(gfp_t gfp_mask); |
5e4c0d97 | 328 | int radix_tree_maybe_preload(gfp_t gfp_mask); |
c78c66d1 | 329 | int radix_tree_maybe_preload_order(gfp_t gfp_mask, int order); |
1da177e4 | 330 | void radix_tree_init(void); |
d7b62727 | 331 | void *radix_tree_tag_set(struct radix_tree_root *, |
daff89f3 | 332 | unsigned long index, unsigned int tag); |
d7b62727 | 333 | void *radix_tree_tag_clear(struct radix_tree_root *, |
daff89f3 | 334 | unsigned long index, unsigned int tag); |
35534c86 | 335 | int radix_tree_tag_get(const struct radix_tree_root *, |
daff89f3 | 336 | unsigned long index, unsigned int tag); |
30b888ba MW |
337 | void radix_tree_iter_tag_set(struct radix_tree_root *, |
338 | const struct radix_tree_iter *iter, unsigned int tag); | |
339 | void radix_tree_iter_tag_clear(struct radix_tree_root *, | |
268f42de | 340 | const struct radix_tree_iter *iter, unsigned int tag); |
d7b62727 MW |
341 | unsigned int radix_tree_gang_lookup_tag(const struct radix_tree_root *, |
342 | void **results, unsigned long first_index, | |
343 | unsigned int max_items, unsigned int tag); | |
344 | unsigned int radix_tree_gang_lookup_tag_slot(const struct radix_tree_root *, | |
345 | void __rcu ***results, unsigned long first_index, | |
346 | unsigned int max_items, unsigned int tag); | |
347 | int radix_tree_tagged(const struct radix_tree_root *, unsigned int tag); | |
1da177e4 LT |
348 | |
349 | static inline void radix_tree_preload_end(void) | |
350 | { | |
351 | preempt_enable(); | |
352 | } | |
353 | ||
2791653a | 354 | int radix_tree_split_preload(unsigned old_order, unsigned new_order, gfp_t); |
e157b555 MW |
355 | int radix_tree_split(struct radix_tree_root *, unsigned long index, |
356 | unsigned new_order); | |
175542f5 MW |
357 | int radix_tree_join(struct radix_tree_root *, unsigned long index, |
358 | unsigned new_order, void *); | |
388f79fd CM |
359 | |
360 | void __rcu **idr_get_free_cmn(struct radix_tree_root *root, | |
361 | struct radix_tree_iter *iter, gfp_t gfp, | |
362 | unsigned long max); | |
363 | static inline void __rcu **idr_get_free(struct radix_tree_root *root, | |
364 | struct radix_tree_iter *iter, | |
365 | gfp_t gfp, | |
366 | int end) | |
367 | { | |
368 | return idr_get_free_cmn(root, iter, gfp, end > 0 ? end - 1 : INT_MAX); | |
369 | } | |
370 | ||
371 | static inline void __rcu **idr_get_free_ext(struct radix_tree_root *root, | |
372 | struct radix_tree_iter *iter, | |
373 | gfp_t gfp, | |
374 | unsigned long end) | |
375 | { | |
376 | return idr_get_free_cmn(root, iter, gfp, end - 1); | |
377 | } | |
175542f5 | 378 | |
0a835c4f MW |
379 | enum { |
380 | RADIX_TREE_ITER_TAG_MASK = 0x0f, /* tag index in lower nybble */ | |
381 | RADIX_TREE_ITER_TAGGED = 0x10, /* lookup tagged slots */ | |
382 | RADIX_TREE_ITER_CONTIG = 0x20, /* stop at first hole */ | |
383 | }; | |
78c1d784 KK |
384 | |
385 | /** | |
386 | * radix_tree_iter_init - initialize radix tree iterator | |
387 | * | |
388 | * @iter: pointer to iterator state | |
389 | * @start: iteration starting index | |
390 | * Returns: NULL | |
391 | */ | |
d7b62727 | 392 | static __always_inline void __rcu ** |
78c1d784 KK |
393 | radix_tree_iter_init(struct radix_tree_iter *iter, unsigned long start) |
394 | { | |
395 | /* | |
396 | * Leave iter->tags uninitialized. radix_tree_next_chunk() will fill it | |
397 | * in the case of a successful tagged chunk lookup. If the lookup was | |
398 | * unsuccessful or non-tagged then nobody cares about ->tags. | |
399 | * | |
400 | * Set index to zero to bypass next_index overflow protection. | |
401 | * See the comment in radix_tree_next_chunk() for details. | |
402 | */ | |
403 | iter->index = 0; | |
404 | iter->next_index = start; | |
405 | return NULL; | |
406 | } | |
407 | ||
408 | /** | |
409 | * radix_tree_next_chunk - find next chunk of slots for iteration | |
410 | * | |
411 | * @root: radix tree root | |
412 | * @iter: iterator state | |
413 | * @flags: RADIX_TREE_ITER_* flags and tag index | |
414 | * Returns: pointer to chunk first slot, or NULL if there no more left | |
415 | * | |
416 | * This function looks up the next chunk in the radix tree starting from | |
417 | * @iter->next_index. It returns a pointer to the chunk's first slot. | |
418 | * Also it fills @iter with data about chunk: position in the tree (index), | |
419 | * its end (next_index), and constructs a bit mask for tagged iterating (tags). | |
420 | */ | |
d7b62727 | 421 | void __rcu **radix_tree_next_chunk(const struct radix_tree_root *, |
78c1d784 KK |
422 | struct radix_tree_iter *iter, unsigned flags); |
423 | ||
0a835c4f MW |
424 | /** |
425 | * radix_tree_iter_lookup - look up an index in the radix tree | |
426 | * @root: radix tree root | |
427 | * @iter: iterator state | |
428 | * @index: key to look up | |
429 | * | |
430 | * If @index is present in the radix tree, this function returns the slot | |
431 | * containing it and updates @iter to describe the entry. If @index is not | |
432 | * present, it returns NULL. | |
433 | */ | |
d7b62727 MW |
434 | static inline void __rcu ** |
435 | radix_tree_iter_lookup(const struct radix_tree_root *root, | |
0a835c4f MW |
436 | struct radix_tree_iter *iter, unsigned long index) |
437 | { | |
438 | radix_tree_iter_init(iter, index); | |
439 | return radix_tree_next_chunk(root, iter, RADIX_TREE_ITER_CONTIG); | |
440 | } | |
441 | ||
442 | /** | |
443 | * radix_tree_iter_find - find a present entry | |
444 | * @root: radix tree root | |
445 | * @iter: iterator state | |
446 | * @index: start location | |
447 | * | |
448 | * This function returns the slot containing the entry with the lowest index | |
449 | * which is at least @index. If @index is larger than any present entry, this | |
450 | * function returns NULL. The @iter is updated to describe the entry found. | |
451 | */ | |
d7b62727 MW |
452 | static inline void __rcu ** |
453 | radix_tree_iter_find(const struct radix_tree_root *root, | |
0a835c4f MW |
454 | struct radix_tree_iter *iter, unsigned long index) |
455 | { | |
456 | radix_tree_iter_init(iter, index); | |
457 | return radix_tree_next_chunk(root, iter, 0); | |
458 | } | |
459 | ||
46437f9a MW |
460 | /** |
461 | * radix_tree_iter_retry - retry this chunk of the iteration | |
462 | * @iter: iterator state | |
463 | * | |
464 | * If we iterate over a tree protected only by the RCU lock, a race | |
465 | * against deletion or creation may result in seeing a slot for which | |
466 | * radix_tree_deref_retry() returns true. If so, call this function | |
467 | * and continue the iteration. | |
468 | */ | |
469 | static inline __must_check | |
d7b62727 | 470 | void __rcu **radix_tree_iter_retry(struct radix_tree_iter *iter) |
46437f9a MW |
471 | { |
472 | iter->next_index = iter->index; | |
3cb9185c | 473 | iter->tags = 0; |
46437f9a MW |
474 | return NULL; |
475 | } | |
476 | ||
21ef5339 RZ |
477 | static inline unsigned long |
478 | __radix_tree_iter_add(struct radix_tree_iter *iter, unsigned long slots) | |
479 | { | |
480 | return iter->index + (slots << iter_shift(iter)); | |
481 | } | |
482 | ||
7165092f | 483 | /** |
148deab2 MW |
484 | * radix_tree_iter_resume - resume iterating when the chunk may be invalid |
485 | * @slot: pointer to current slot | |
486 | * @iter: iterator state | |
487 | * Returns: New slot pointer | |
7165092f MW |
488 | * |
489 | * If the iterator needs to release then reacquire a lock, the chunk may | |
490 | * have been invalidated by an insertion or deletion. Call this function | |
148deab2 | 491 | * before releasing the lock to continue the iteration from the next index. |
7165092f | 492 | */ |
d7b62727 | 493 | void __rcu **__must_check radix_tree_iter_resume(void __rcu **slot, |
148deab2 | 494 | struct radix_tree_iter *iter); |
7165092f | 495 | |
78c1d784 KK |
496 | /** |
497 | * radix_tree_chunk_size - get current chunk size | |
498 | * | |
499 | * @iter: pointer to radix tree iterator | |
500 | * Returns: current chunk size | |
501 | */ | |
73204282 | 502 | static __always_inline long |
78c1d784 KK |
503 | radix_tree_chunk_size(struct radix_tree_iter *iter) |
504 | { | |
21ef5339 RZ |
505 | return (iter->next_index - iter->index) >> iter_shift(iter); |
506 | } | |
507 | ||
148deab2 | 508 | #ifdef CONFIG_RADIX_TREE_MULTIORDER |
d7b62727 MW |
509 | void __rcu **__radix_tree_next_slot(void __rcu **slot, |
510 | struct radix_tree_iter *iter, unsigned flags); | |
148deab2 MW |
511 | #else |
512 | /* Can't happen without sibling entries, but the compiler can't tell that */ | |
d7b62727 | 513 | static inline void __rcu **__radix_tree_next_slot(void __rcu **slot, |
148deab2 | 514 | struct radix_tree_iter *iter, unsigned flags) |
21ef5339 | 515 | { |
148deab2 | 516 | return slot; |
78c1d784 | 517 | } |
148deab2 | 518 | #endif |
78c1d784 KK |
519 | |
520 | /** | |
521 | * radix_tree_next_slot - find next slot in chunk | |
522 | * | |
523 | * @slot: pointer to current slot | |
524 | * @iter: pointer to interator state | |
525 | * @flags: RADIX_TREE_ITER_*, should be constant | |
526 | * Returns: pointer to next slot, or NULL if there no more left | |
527 | * | |
528 | * This function updates @iter->index in the case of a successful lookup. | |
529 | * For tagged lookup it also eats @iter->tags. | |
915045fe RZ |
530 | * |
531 | * There are several cases where 'slot' can be passed in as NULL to this | |
148deab2 | 532 | * function. These cases result from the use of radix_tree_iter_resume() or |
915045fe RZ |
533 | * radix_tree_iter_retry(). In these cases we don't end up dereferencing |
534 | * 'slot' because either: | |
535 | * a) we are doing tagged iteration and iter->tags has been set to 0, or | |
536 | * b) we are doing non-tagged iteration, and iter->index and iter->next_index | |
537 | * have been set up so that radix_tree_chunk_size() returns 1 or 0. | |
78c1d784 | 538 | */ |
d7b62727 MW |
539 | static __always_inline void __rcu **radix_tree_next_slot(void __rcu **slot, |
540 | struct radix_tree_iter *iter, unsigned flags) | |
78c1d784 KK |
541 | { |
542 | if (flags & RADIX_TREE_ITER_TAGGED) { | |
543 | iter->tags >>= 1; | |
21ef5339 RZ |
544 | if (unlikely(!iter->tags)) |
545 | return NULL; | |
78c1d784 | 546 | if (likely(iter->tags & 1ul)) { |
21ef5339 | 547 | iter->index = __radix_tree_iter_add(iter, 1); |
148deab2 MW |
548 | slot++; |
549 | goto found; | |
78c1d784 | 550 | } |
21ef5339 | 551 | if (!(flags & RADIX_TREE_ITER_CONTIG)) { |
78c1d784 KK |
552 | unsigned offset = __ffs(iter->tags); |
553 | ||
148deab2 MW |
554 | iter->tags >>= offset++; |
555 | iter->index = __radix_tree_iter_add(iter, offset); | |
556 | slot += offset; | |
557 | goto found; | |
78c1d784 KK |
558 | } |
559 | } else { | |
21ef5339 | 560 | long count = radix_tree_chunk_size(iter); |
78c1d784 | 561 | |
21ef5339 | 562 | while (--count > 0) { |
78c1d784 | 563 | slot++; |
21ef5339 RZ |
564 | iter->index = __radix_tree_iter_add(iter, 1); |
565 | ||
78c1d784 | 566 | if (likely(*slot)) |
148deab2 | 567 | goto found; |
fffaee36 KK |
568 | if (flags & RADIX_TREE_ITER_CONTIG) { |
569 | /* forbid switching to the next chunk */ | |
570 | iter->next_index = 0; | |
78c1d784 | 571 | break; |
fffaee36 | 572 | } |
78c1d784 KK |
573 | } |
574 | } | |
575 | return NULL; | |
148deab2 MW |
576 | |
577 | found: | |
12320d0f | 578 | if (unlikely(radix_tree_is_internal_node(rcu_dereference_raw(*slot)))) |
148deab2 MW |
579 | return __radix_tree_next_slot(slot, iter, flags); |
580 | return slot; | |
78c1d784 KK |
581 | } |
582 | ||
78c1d784 KK |
583 | /** |
584 | * radix_tree_for_each_slot - iterate over non-empty slots | |
585 | * | |
586 | * @slot: the void** variable for pointer to slot | |
587 | * @root: the struct radix_tree_root pointer | |
588 | * @iter: the struct radix_tree_iter pointer | |
589 | * @start: iteration starting index | |
590 | * | |
591 | * @slot points to radix tree slot, @iter->index contains its index. | |
592 | */ | |
593 | #define radix_tree_for_each_slot(slot, root, iter, start) \ | |
594 | for (slot = radix_tree_iter_init(iter, start) ; \ | |
595 | slot || (slot = radix_tree_next_chunk(root, iter, 0)) ; \ | |
596 | slot = radix_tree_next_slot(slot, iter, 0)) | |
597 | ||
598 | /** | |
599 | * radix_tree_for_each_contig - iterate over contiguous slots | |
600 | * | |
601 | * @slot: the void** variable for pointer to slot | |
602 | * @root: the struct radix_tree_root pointer | |
603 | * @iter: the struct radix_tree_iter pointer | |
604 | * @start: iteration starting index | |
605 | * | |
606 | * @slot points to radix tree slot, @iter->index contains its index. | |
607 | */ | |
608 | #define radix_tree_for_each_contig(slot, root, iter, start) \ | |
609 | for (slot = radix_tree_iter_init(iter, start) ; \ | |
610 | slot || (slot = radix_tree_next_chunk(root, iter, \ | |
611 | RADIX_TREE_ITER_CONTIG)) ; \ | |
612 | slot = radix_tree_next_slot(slot, iter, \ | |
613 | RADIX_TREE_ITER_CONTIG)) | |
614 | ||
615 | /** | |
616 | * radix_tree_for_each_tagged - iterate over tagged slots | |
617 | * | |
618 | * @slot: the void** variable for pointer to slot | |
619 | * @root: the struct radix_tree_root pointer | |
620 | * @iter: the struct radix_tree_iter pointer | |
621 | * @start: iteration starting index | |
622 | * @tag: tag index | |
623 | * | |
624 | * @slot points to radix tree slot, @iter->index contains its index. | |
625 | */ | |
626 | #define radix_tree_for_each_tagged(slot, root, iter, start, tag) \ | |
627 | for (slot = radix_tree_iter_init(iter, start) ; \ | |
628 | slot || (slot = radix_tree_next_chunk(root, iter, \ | |
629 | RADIX_TREE_ITER_TAGGED | tag)) ; \ | |
630 | slot = radix_tree_next_slot(slot, iter, \ | |
148deab2 | 631 | RADIX_TREE_ITER_TAGGED | tag)) |
78c1d784 | 632 | |
1da177e4 | 633 | #endif /* _LINUX_RADIX_TREE_H */ |