]>
git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blob - include/linux/radix-tree.h
2 * Copyright (C) 2001 Momchil Velikov
3 * Portions Copyright (C) 2001 Christoph Hellwig
4 * Copyright (C) 2006 Nick Piggin
5 * Copyright (C) 2012 Konstantin Khlebnikov
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.
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.
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.
21 #ifndef _LINUX_RADIX_TREE_H
22 #define _LINUX_RADIX_TREE_H
24 #include <linux/bitops.h>
25 #include <linux/kernel.h>
26 #include <linux/list.h>
27 #include <linux/preempt.h>
28 #include <linux/rcupdate.h>
29 #include <linux/spinlock.h>
30 #include <linux/types.h>
31 #include <linux/xarray.h>
33 /* Keep unconverted code working */
34 #define radix_tree_root xarray
35 #define radix_tree_node xa_node
38 * The bottom two bits of the slot determine how the remaining bits in the
39 * slot are interpreted:
45 * The internal entry may be a pointer to the next level in the tree, a
46 * sibling entry, or an indicator that the entry in this slot has been moved
47 * to another location in the tree and the lookup should be restarted. While
48 * NULL fits the 'data pointer' pattern, it means that there is no entry in
49 * the tree for this index (no matter what level of the tree it is found at).
50 * This means that storing a NULL entry in the tree is the same as deleting
51 * the entry from the tree.
53 #define RADIX_TREE_ENTRY_MASK 3UL
54 #define RADIX_TREE_INTERNAL_NODE 2UL
56 static inline bool radix_tree_is_internal_node(void *ptr
)
58 return ((unsigned long)ptr
& RADIX_TREE_ENTRY_MASK
) ==
59 RADIX_TREE_INTERNAL_NODE
;
62 /*** radix-tree API starts here ***/
64 #define RADIX_TREE_MAP_SHIFT XA_CHUNK_SHIFT
65 #define RADIX_TREE_MAP_SIZE (1UL << RADIX_TREE_MAP_SHIFT)
66 #define RADIX_TREE_MAP_MASK (RADIX_TREE_MAP_SIZE-1)
68 #define RADIX_TREE_MAX_TAGS XA_MAX_MARKS
69 #define RADIX_TREE_TAG_LONGS XA_MARK_LONGS
71 #define RADIX_TREE_INDEX_BITS (8 /* CHAR_BIT */ * sizeof(unsigned long))
72 #define RADIX_TREE_MAX_PATH (DIV_ROUND_UP(RADIX_TREE_INDEX_BITS, \
73 RADIX_TREE_MAP_SHIFT))
75 /* The IDR tag is stored in the low bits of xa_flags */
76 #define ROOT_IS_IDR ((__force gfp_t)4)
77 /* The top bits of xa_flags are used to store the root tags */
78 #define ROOT_TAG_SHIFT (__GFP_BITS_SHIFT)
80 #define RADIX_TREE_INIT(name, mask) XARRAY_INIT(name, mask)
82 #define RADIX_TREE(name, mask) \
83 struct radix_tree_root name = RADIX_TREE_INIT(name, mask)
85 #define INIT_RADIX_TREE(root, mask) xa_init_flags(root, mask)
87 static inline bool radix_tree_empty(const struct radix_tree_root
*root
)
89 return root
->xa_head
== NULL
;
93 * struct radix_tree_iter - radix tree iterator state
95 * @index: index of current slot
96 * @next_index: one beyond the last index for this chunk
97 * @tags: bit-mask for tag-iterating
98 * @node: node that contains current slot
99 * @shift: shift for the node that holds our slots
101 * This radix tree iterator works in terms of "chunks" of slots. A chunk is a
102 * subinterval of slots contained within one radix tree leaf node. It is
103 * described by a pointer to its first slot and a struct radix_tree_iter
104 * which holds the chunk's position in the tree and its size. For tagged
105 * iteration radix_tree_iter also holds the slots' bit-mask for one chosen
108 struct radix_tree_iter
{
110 unsigned long next_index
;
112 struct radix_tree_node
*node
;
113 #ifdef CONFIG_RADIX_TREE_MULTIORDER
118 static inline unsigned int iter_shift(const struct radix_tree_iter
*iter
)
120 #ifdef CONFIG_RADIX_TREE_MULTIORDER
128 * Radix-tree synchronization
130 * The radix-tree API requires that users provide all synchronisation (with
131 * specific exceptions, noted below).
133 * Synchronization of access to the data items being stored in the tree, and
134 * management of their lifetimes must be completely managed by API users.
136 * For API usage, in general,
137 * - any function _modifying_ the tree or tags (inserting or deleting
138 * items, setting or clearing tags) must exclude other modifications, and
139 * exclude any functions reading the tree.
140 * - any function _reading_ the tree or tags (looking up items or tags,
141 * gang lookups) must exclude modifications to the tree, but may occur
142 * concurrently with other readers.
144 * The notable exceptions to this rule are the following functions:
145 * __radix_tree_lookup
147 * radix_tree_lookup_slot
149 * radix_tree_gang_lookup
150 * radix_tree_gang_lookup_tag
151 * radix_tree_gang_lookup_tag_slot
154 * The first 7 functions are able to be called locklessly, using RCU. The
155 * caller must ensure calls to these functions are made within rcu_read_lock()
156 * regions. Other readers (lock-free or otherwise) and modifications may be
157 * running concurrently.
159 * It is still required that the caller manage the synchronization and lifetimes
160 * of the items. So if RCU lock-free lookups are used, typically this would mean
161 * that the items have their own locks, or are amenable to lock-free access; and
162 * that the items are freed by RCU (or only freed after having been deleted from
163 * the radix tree *and* a synchronize_rcu() grace period).
165 * (Note, rcu_assign_pointer and rcu_dereference are not needed to control
166 * access to data items when inserting into or looking up from the radix tree)
168 * Note that the value returned by radix_tree_tag_get() may not be relied upon
169 * if only the RCU read lock is held. Functions to set/clear tags and to
170 * delete nodes running concurrently with it may affect its result such that
171 * two consecutive reads in the same locked section may return different
172 * values. If reliability is required, modification functions must also be
173 * excluded from concurrency.
175 * radix_tree_tagged is able to be called without locking or RCU.
179 * radix_tree_deref_slot - dereference a slot
180 * @slot: slot pointer, returned by radix_tree_lookup_slot
182 * For use with radix_tree_lookup_slot(). Caller must hold tree at least read
183 * locked across slot lookup and dereference. Not required if write lock is
184 * held (ie. items cannot be concurrently inserted).
186 * radix_tree_deref_retry must be used to confirm validity of the pointer if
187 * only the read lock is held.
189 * Return: entry stored in that slot.
191 static inline void *radix_tree_deref_slot(void __rcu
**slot
)
193 return rcu_dereference(*slot
);
197 * radix_tree_deref_slot_protected - dereference a slot with tree lock held
198 * @slot: slot pointer, returned by radix_tree_lookup_slot
200 * Similar to radix_tree_deref_slot. The caller does not hold the RCU read
201 * lock but it must hold the tree lock to prevent parallel updates.
203 * Return: entry stored in that slot.
205 static inline void *radix_tree_deref_slot_protected(void __rcu
**slot
,
206 spinlock_t
*treelock
)
208 return rcu_dereference_protected(*slot
, lockdep_is_held(treelock
));
212 * radix_tree_deref_retry - check radix_tree_deref_slot
213 * @arg: pointer returned by radix_tree_deref_slot
214 * Returns: 0 if retry is not required, otherwise retry is required
216 * radix_tree_deref_retry must be used with radix_tree_deref_slot.
218 static inline int radix_tree_deref_retry(void *arg
)
220 return unlikely(radix_tree_is_internal_node(arg
));
224 * radix_tree_exception - radix_tree_deref_slot returned either exception?
225 * @arg: value returned by radix_tree_deref_slot
226 * Returns: 0 if well-aligned pointer, non-0 if either kind of exception.
228 static inline int radix_tree_exception(void *arg
)
230 return unlikely((unsigned long)arg
& RADIX_TREE_ENTRY_MASK
);
233 int __radix_tree_insert(struct radix_tree_root
*, unsigned long index
,
234 unsigned order
, void *);
235 static inline int radix_tree_insert(struct radix_tree_root
*root
,
236 unsigned long index
, void *entry
)
238 return __radix_tree_insert(root
, index
, 0, entry
);
240 void *__radix_tree_lookup(const struct radix_tree_root
*, unsigned long index
,
241 struct radix_tree_node
**nodep
, void __rcu
***slotp
);
242 void *radix_tree_lookup(const struct radix_tree_root
*, unsigned long);
243 void __rcu
**radix_tree_lookup_slot(const struct radix_tree_root
*,
244 unsigned long index
);
245 void __radix_tree_replace(struct radix_tree_root
*, struct radix_tree_node
*,
246 void __rcu
**slot
, void *entry
);
247 void radix_tree_iter_replace(struct radix_tree_root
*,
248 const struct radix_tree_iter
*, void __rcu
**slot
, void *entry
);
249 void radix_tree_replace_slot(struct radix_tree_root
*,
250 void __rcu
**slot
, void *entry
);
251 void radix_tree_iter_delete(struct radix_tree_root
*,
252 struct radix_tree_iter
*iter
, void __rcu
**slot
);
253 void *radix_tree_delete_item(struct radix_tree_root
*, unsigned long, void *);
254 void *radix_tree_delete(struct radix_tree_root
*, unsigned long);
255 void radix_tree_clear_tags(struct radix_tree_root
*, struct radix_tree_node
*,
257 unsigned int radix_tree_gang_lookup(const struct radix_tree_root
*,
258 void **results
, unsigned long first_index
,
259 unsigned int max_items
);
260 int radix_tree_preload(gfp_t gfp_mask
);
261 int radix_tree_maybe_preload(gfp_t gfp_mask
);
262 int radix_tree_maybe_preload_order(gfp_t gfp_mask
, int order
);
263 void radix_tree_init(void);
264 void *radix_tree_tag_set(struct radix_tree_root
*,
265 unsigned long index
, unsigned int tag
);
266 void *radix_tree_tag_clear(struct radix_tree_root
*,
267 unsigned long index
, unsigned int tag
);
268 int radix_tree_tag_get(const struct radix_tree_root
*,
269 unsigned long index
, unsigned int tag
);
270 void radix_tree_iter_tag_set(struct radix_tree_root
*,
271 const struct radix_tree_iter
*iter
, unsigned int tag
);
272 void radix_tree_iter_tag_clear(struct radix_tree_root
*,
273 const struct radix_tree_iter
*iter
, unsigned int tag
);
274 unsigned int radix_tree_gang_lookup_tag(const struct radix_tree_root
*,
275 void **results
, unsigned long first_index
,
276 unsigned int max_items
, unsigned int tag
);
277 unsigned int radix_tree_gang_lookup_tag_slot(const struct radix_tree_root
*,
278 void __rcu
***results
, unsigned long first_index
,
279 unsigned int max_items
, unsigned int tag
);
280 int radix_tree_tagged(const struct radix_tree_root
*, unsigned int tag
);
282 static inline void radix_tree_preload_end(void)
287 void __rcu
**idr_get_free(struct radix_tree_root
*root
,
288 struct radix_tree_iter
*iter
, gfp_t gfp
,
292 RADIX_TREE_ITER_TAG_MASK
= 0x0f, /* tag index in lower nybble */
293 RADIX_TREE_ITER_TAGGED
= 0x10, /* lookup tagged slots */
294 RADIX_TREE_ITER_CONTIG
= 0x20, /* stop at first hole */
298 * radix_tree_iter_init - initialize radix tree iterator
300 * @iter: pointer to iterator state
301 * @start: iteration starting index
304 static __always_inline
void __rcu
**
305 radix_tree_iter_init(struct radix_tree_iter
*iter
, unsigned long start
)
308 * Leave iter->tags uninitialized. radix_tree_next_chunk() will fill it
309 * in the case of a successful tagged chunk lookup. If the lookup was
310 * unsuccessful or non-tagged then nobody cares about ->tags.
312 * Set index to zero to bypass next_index overflow protection.
313 * See the comment in radix_tree_next_chunk() for details.
316 iter
->next_index
= start
;
321 * radix_tree_next_chunk - find next chunk of slots for iteration
323 * @root: radix tree root
324 * @iter: iterator state
325 * @flags: RADIX_TREE_ITER_* flags and tag index
326 * Returns: pointer to chunk first slot, or NULL if there no more left
328 * This function looks up the next chunk in the radix tree starting from
329 * @iter->next_index. It returns a pointer to the chunk's first slot.
330 * Also it fills @iter with data about chunk: position in the tree (index),
331 * its end (next_index), and constructs a bit mask for tagged iterating (tags).
333 void __rcu
**radix_tree_next_chunk(const struct radix_tree_root
*,
334 struct radix_tree_iter
*iter
, unsigned flags
);
337 * radix_tree_iter_lookup - look up an index in the radix tree
338 * @root: radix tree root
339 * @iter: iterator state
340 * @index: key to look up
342 * If @index is present in the radix tree, this function returns the slot
343 * containing it and updates @iter to describe the entry. If @index is not
344 * present, it returns NULL.
346 static inline void __rcu
**
347 radix_tree_iter_lookup(const struct radix_tree_root
*root
,
348 struct radix_tree_iter
*iter
, unsigned long index
)
350 radix_tree_iter_init(iter
, index
);
351 return radix_tree_next_chunk(root
, iter
, RADIX_TREE_ITER_CONTIG
);
355 * radix_tree_iter_find - find a present entry
356 * @root: radix tree root
357 * @iter: iterator state
358 * @index: start location
360 * This function returns the slot containing the entry with the lowest index
361 * which is at least @index. If @index is larger than any present entry, this
362 * function returns NULL. The @iter is updated to describe the entry found.
364 static inline void __rcu
**
365 radix_tree_iter_find(const struct radix_tree_root
*root
,
366 struct radix_tree_iter
*iter
, unsigned long index
)
368 radix_tree_iter_init(iter
, index
);
369 return radix_tree_next_chunk(root
, iter
, 0);
373 * radix_tree_iter_retry - retry this chunk of the iteration
374 * @iter: iterator state
376 * If we iterate over a tree protected only by the RCU lock, a race
377 * against deletion or creation may result in seeing a slot for which
378 * radix_tree_deref_retry() returns true. If so, call this function
379 * and continue the iteration.
381 static inline __must_check
382 void __rcu
**radix_tree_iter_retry(struct radix_tree_iter
*iter
)
384 iter
->next_index
= iter
->index
;
389 static inline unsigned long
390 __radix_tree_iter_add(struct radix_tree_iter
*iter
, unsigned long slots
)
392 return iter
->index
+ (slots
<< iter_shift(iter
));
396 * radix_tree_iter_resume - resume iterating when the chunk may be invalid
397 * @slot: pointer to current slot
398 * @iter: iterator state
399 * Returns: New slot pointer
401 * If the iterator needs to release then reacquire a lock, the chunk may
402 * have been invalidated by an insertion or deletion. Call this function
403 * before releasing the lock to continue the iteration from the next index.
405 void __rcu
**__must_check
radix_tree_iter_resume(void __rcu
**slot
,
406 struct radix_tree_iter
*iter
);
409 * radix_tree_chunk_size - get current chunk size
411 * @iter: pointer to radix tree iterator
412 * Returns: current chunk size
414 static __always_inline
long
415 radix_tree_chunk_size(struct radix_tree_iter
*iter
)
417 return (iter
->next_index
- iter
->index
) >> iter_shift(iter
);
420 #ifdef CONFIG_RADIX_TREE_MULTIORDER
421 void __rcu
**__radix_tree_next_slot(void __rcu
**slot
,
422 struct radix_tree_iter
*iter
, unsigned flags
);
424 /* Can't happen without sibling entries, but the compiler can't tell that */
425 static inline void __rcu
**__radix_tree_next_slot(void __rcu
**slot
,
426 struct radix_tree_iter
*iter
, unsigned flags
)
433 * radix_tree_next_slot - find next slot in chunk
435 * @slot: pointer to current slot
436 * @iter: pointer to interator state
437 * @flags: RADIX_TREE_ITER_*, should be constant
438 * Returns: pointer to next slot, or NULL if there no more left
440 * This function updates @iter->index in the case of a successful lookup.
441 * For tagged lookup it also eats @iter->tags.
443 * There are several cases where 'slot' can be passed in as NULL to this
444 * function. These cases result from the use of radix_tree_iter_resume() or
445 * radix_tree_iter_retry(). In these cases we don't end up dereferencing
446 * 'slot' because either:
447 * a) we are doing tagged iteration and iter->tags has been set to 0, or
448 * b) we are doing non-tagged iteration, and iter->index and iter->next_index
449 * have been set up so that radix_tree_chunk_size() returns 1 or 0.
451 static __always_inline
void __rcu
**radix_tree_next_slot(void __rcu
**slot
,
452 struct radix_tree_iter
*iter
, unsigned flags
)
454 if (flags
& RADIX_TREE_ITER_TAGGED
) {
456 if (unlikely(!iter
->tags
))
458 if (likely(iter
->tags
& 1ul)) {
459 iter
->index
= __radix_tree_iter_add(iter
, 1);
463 if (!(flags
& RADIX_TREE_ITER_CONTIG
)) {
464 unsigned offset
= __ffs(iter
->tags
);
466 iter
->tags
>>= offset
++;
467 iter
->index
= __radix_tree_iter_add(iter
, offset
);
472 long count
= radix_tree_chunk_size(iter
);
474 while (--count
> 0) {
476 iter
->index
= __radix_tree_iter_add(iter
, 1);
480 if (flags
& RADIX_TREE_ITER_CONTIG
) {
481 /* forbid switching to the next chunk */
482 iter
->next_index
= 0;
490 if (unlikely(radix_tree_is_internal_node(rcu_dereference_raw(*slot
))))
491 return __radix_tree_next_slot(slot
, iter
, flags
);
496 * radix_tree_for_each_slot - iterate over non-empty slots
498 * @slot: the void** variable for pointer to slot
499 * @root: the struct radix_tree_root pointer
500 * @iter: the struct radix_tree_iter pointer
501 * @start: iteration starting index
503 * @slot points to radix tree slot, @iter->index contains its index.
505 #define radix_tree_for_each_slot(slot, root, iter, start) \
506 for (slot = radix_tree_iter_init(iter, start) ; \
507 slot || (slot = radix_tree_next_chunk(root, iter, 0)) ; \
508 slot = radix_tree_next_slot(slot, iter, 0))
511 * radix_tree_for_each_tagged - iterate over tagged slots
513 * @slot: the void** variable for pointer to slot
514 * @root: the struct radix_tree_root pointer
515 * @iter: the struct radix_tree_iter pointer
516 * @start: iteration starting index
519 * @slot points to radix tree slot, @iter->index contains its index.
521 #define radix_tree_for_each_tagged(slot, root, iter, start, tag) \
522 for (slot = radix_tree_iter_init(iter, start) ; \
523 slot || (slot = radix_tree_next_chunk(root, iter, \
524 RADIX_TREE_ITER_TAGGED | tag)) ; \
525 slot = radix_tree_next_slot(slot, iter, \
526 RADIX_TREE_ITER_TAGGED | tag))
528 #endif /* _LINUX_RADIX_TREE_H */