]>
git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blob - include/linux/radix-tree.h
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * Copyright (C) 2001 Momchil Velikov
4 * Portions Copyright (C) 2001 Christoph Hellwig
5 * Copyright (C) 2006 Nick Piggin
6 * Copyright (C) 2012 Konstantin Khlebnikov
8 #ifndef _LINUX_RADIX_TREE_H
9 #define _LINUX_RADIX_TREE_H
11 #include <linux/bitops.h>
12 #include <linux/kernel.h>
13 #include <linux/list.h>
14 #include <linux/percpu.h>
15 #include <linux/preempt.h>
16 #include <linux/rcupdate.h>
17 #include <linux/spinlock.h>
18 #include <linux/types.h>
19 #include <linux/xarray.h>
21 /* Keep unconverted code working */
22 #define radix_tree_root xarray
23 #define radix_tree_node xa_node
26 * The bottom two bits of the slot determine how the remaining bits in the
27 * slot are interpreted:
33 * The internal entry may be a pointer to the next level in the tree, a
34 * sibling entry, or an indicator that the entry in this slot has been moved
35 * to another location in the tree and the lookup should be restarted. While
36 * NULL fits the 'data pointer' pattern, it means that there is no entry in
37 * the tree for this index (no matter what level of the tree it is found at).
38 * This means that storing a NULL entry in the tree is the same as deleting
39 * the entry from the tree.
41 #define RADIX_TREE_ENTRY_MASK 3UL
42 #define RADIX_TREE_INTERNAL_NODE 2UL
44 static inline bool radix_tree_is_internal_node(void *ptr
)
46 return ((unsigned long)ptr
& RADIX_TREE_ENTRY_MASK
) ==
47 RADIX_TREE_INTERNAL_NODE
;
50 /*** radix-tree API starts here ***/
52 #define RADIX_TREE_MAP_SHIFT XA_CHUNK_SHIFT
53 #define RADIX_TREE_MAP_SIZE (1UL << RADIX_TREE_MAP_SHIFT)
54 #define RADIX_TREE_MAP_MASK (RADIX_TREE_MAP_SIZE-1)
56 #define RADIX_TREE_MAX_TAGS XA_MAX_MARKS
57 #define RADIX_TREE_TAG_LONGS XA_MARK_LONGS
59 #define RADIX_TREE_INDEX_BITS (8 /* CHAR_BIT */ * sizeof(unsigned long))
60 #define RADIX_TREE_MAX_PATH (DIV_ROUND_UP(RADIX_TREE_INDEX_BITS, \
61 RADIX_TREE_MAP_SHIFT))
63 /* The IDR tag is stored in the low bits of xa_flags */
64 #define ROOT_IS_IDR ((__force gfp_t)4)
65 /* The top bits of xa_flags are used to store the root tags */
66 #define ROOT_TAG_SHIFT (__GFP_BITS_SHIFT)
68 #define RADIX_TREE_INIT(name, mask) XARRAY_INIT(name, mask)
70 #define RADIX_TREE(name, mask) \
71 struct radix_tree_root name = RADIX_TREE_INIT(name, mask)
73 #define INIT_RADIX_TREE(root, mask) xa_init_flags(root, mask)
75 static inline bool radix_tree_empty(const struct radix_tree_root
*root
)
77 return root
->xa_head
== NULL
;
81 * struct radix_tree_iter - radix tree iterator state
83 * @index: index of current slot
84 * @next_index: one beyond the last index for this chunk
85 * @tags: bit-mask for tag-iterating
86 * @node: node that contains current slot
88 * This radix tree iterator works in terms of "chunks" of slots. A chunk is a
89 * subinterval of slots contained within one radix tree leaf node. It is
90 * described by a pointer to its first slot and a struct radix_tree_iter
91 * which holds the chunk's position in the tree and its size. For tagged
92 * iteration radix_tree_iter also holds the slots' bit-mask for one chosen
95 struct radix_tree_iter
{
97 unsigned long next_index
;
99 struct radix_tree_node
*node
;
103 * Radix-tree synchronization
105 * The radix-tree API requires that users provide all synchronisation (with
106 * specific exceptions, noted below).
108 * Synchronization of access to the data items being stored in the tree, and
109 * management of their lifetimes must be completely managed by API users.
111 * For API usage, in general,
112 * - any function _modifying_ the tree or tags (inserting or deleting
113 * items, setting or clearing tags) must exclude other modifications, and
114 * exclude any functions reading the tree.
115 * - any function _reading_ the tree or tags (looking up items or tags,
116 * gang lookups) must exclude modifications to the tree, but may occur
117 * concurrently with other readers.
119 * The notable exceptions to this rule are the following functions:
120 * __radix_tree_lookup
122 * radix_tree_lookup_slot
124 * radix_tree_gang_lookup
125 * radix_tree_gang_lookup_tag
126 * radix_tree_gang_lookup_tag_slot
129 * The first 7 functions are able to be called locklessly, using RCU. The
130 * caller must ensure calls to these functions are made within rcu_read_lock()
131 * regions. Other readers (lock-free or otherwise) and modifications may be
132 * running concurrently.
134 * It is still required that the caller manage the synchronization and lifetimes
135 * of the items. So if RCU lock-free lookups are used, typically this would mean
136 * that the items have their own locks, or are amenable to lock-free access; and
137 * that the items are freed by RCU (or only freed after having been deleted from
138 * the radix tree *and* a synchronize_rcu() grace period).
140 * (Note, rcu_assign_pointer and rcu_dereference are not needed to control
141 * access to data items when inserting into or looking up from the radix tree)
143 * Note that the value returned by radix_tree_tag_get() may not be relied upon
144 * if only the RCU read lock is held. Functions to set/clear tags and to
145 * delete nodes running concurrently with it may affect its result such that
146 * two consecutive reads in the same locked section may return different
147 * values. If reliability is required, modification functions must also be
148 * excluded from concurrency.
150 * radix_tree_tagged is able to be called without locking or RCU.
154 * radix_tree_deref_slot - dereference a slot
155 * @slot: slot pointer, returned by radix_tree_lookup_slot
157 * For use with radix_tree_lookup_slot(). Caller must hold tree at least read
158 * locked across slot lookup and dereference. Not required if write lock is
159 * held (ie. items cannot be concurrently inserted).
161 * radix_tree_deref_retry must be used to confirm validity of the pointer if
162 * only the read lock is held.
164 * Return: entry stored in that slot.
166 static inline void *radix_tree_deref_slot(void __rcu
**slot
)
168 return rcu_dereference(*slot
);
172 * radix_tree_deref_slot_protected - dereference a slot with tree lock held
173 * @slot: slot pointer, returned by radix_tree_lookup_slot
175 * Similar to radix_tree_deref_slot. The caller does not hold the RCU read
176 * lock but it must hold the tree lock to prevent parallel updates.
178 * Return: entry stored in that slot.
180 static inline void *radix_tree_deref_slot_protected(void __rcu
**slot
,
181 spinlock_t
*treelock
)
183 return rcu_dereference_protected(*slot
, lockdep_is_held(treelock
));
187 * radix_tree_deref_retry - check radix_tree_deref_slot
188 * @arg: pointer returned by radix_tree_deref_slot
189 * Returns: 0 if retry is not required, otherwise retry is required
191 * radix_tree_deref_retry must be used with radix_tree_deref_slot.
193 static inline int radix_tree_deref_retry(void *arg
)
195 return unlikely(radix_tree_is_internal_node(arg
));
199 * radix_tree_exception - radix_tree_deref_slot returned either exception?
200 * @arg: value returned by radix_tree_deref_slot
201 * Returns: 0 if well-aligned pointer, non-0 if either kind of exception.
203 static inline int radix_tree_exception(void *arg
)
205 return unlikely((unsigned long)arg
& RADIX_TREE_ENTRY_MASK
);
208 int radix_tree_insert(struct radix_tree_root
*, unsigned long index
,
210 void *__radix_tree_lookup(const struct radix_tree_root
*, unsigned long index
,
211 struct radix_tree_node
**nodep
, void __rcu
***slotp
);
212 void *radix_tree_lookup(const struct radix_tree_root
*, unsigned long);
213 void __rcu
**radix_tree_lookup_slot(const struct radix_tree_root
*,
214 unsigned long index
);
215 void __radix_tree_replace(struct radix_tree_root
*, struct radix_tree_node
*,
216 void __rcu
**slot
, void *entry
);
217 void radix_tree_iter_replace(struct radix_tree_root
*,
218 const struct radix_tree_iter
*, void __rcu
**slot
, void *entry
);
219 void radix_tree_replace_slot(struct radix_tree_root
*,
220 void __rcu
**slot
, void *entry
);
221 void radix_tree_iter_delete(struct radix_tree_root
*,
222 struct radix_tree_iter
*iter
, void __rcu
**slot
);
223 void *radix_tree_delete_item(struct radix_tree_root
*, unsigned long, void *);
224 void *radix_tree_delete(struct radix_tree_root
*, unsigned long);
225 unsigned int radix_tree_gang_lookup(const struct radix_tree_root
*,
226 void **results
, unsigned long first_index
,
227 unsigned int max_items
);
228 int radix_tree_preload(gfp_t gfp_mask
);
229 int radix_tree_maybe_preload(gfp_t gfp_mask
);
230 void radix_tree_init(void);
231 void *radix_tree_tag_set(struct radix_tree_root
*,
232 unsigned long index
, unsigned int tag
);
233 void *radix_tree_tag_clear(struct radix_tree_root
*,
234 unsigned long index
, unsigned int tag
);
235 int radix_tree_tag_get(const struct radix_tree_root
*,
236 unsigned long index
, unsigned int tag
);
237 void radix_tree_iter_tag_clear(struct radix_tree_root
*,
238 const struct radix_tree_iter
*iter
, unsigned int tag
);
239 unsigned int radix_tree_gang_lookup_tag(const struct radix_tree_root
*,
240 void **results
, unsigned long first_index
,
241 unsigned int max_items
, unsigned int tag
);
242 unsigned int radix_tree_gang_lookup_tag_slot(const struct radix_tree_root
*,
243 void __rcu
***results
, unsigned long first_index
,
244 unsigned int max_items
, unsigned int tag
);
245 int radix_tree_tagged(const struct radix_tree_root
*, unsigned int tag
);
247 static inline void radix_tree_preload_end(void)
252 void __rcu
**idr_get_free(struct radix_tree_root
*root
,
253 struct radix_tree_iter
*iter
, gfp_t gfp
,
257 RADIX_TREE_ITER_TAG_MASK
= 0x0f, /* tag index in lower nybble */
258 RADIX_TREE_ITER_TAGGED
= 0x10, /* lookup tagged slots */
259 RADIX_TREE_ITER_CONTIG
= 0x20, /* stop at first hole */
263 * radix_tree_iter_init - initialize radix tree iterator
265 * @iter: pointer to iterator state
266 * @start: iteration starting index
269 static __always_inline
void __rcu
**
270 radix_tree_iter_init(struct radix_tree_iter
*iter
, unsigned long start
)
273 * Leave iter->tags uninitialized. radix_tree_next_chunk() will fill it
274 * in the case of a successful tagged chunk lookup. If the lookup was
275 * unsuccessful or non-tagged then nobody cares about ->tags.
277 * Set index to zero to bypass next_index overflow protection.
278 * See the comment in radix_tree_next_chunk() for details.
281 iter
->next_index
= start
;
286 * radix_tree_next_chunk - find next chunk of slots for iteration
288 * @root: radix tree root
289 * @iter: iterator state
290 * @flags: RADIX_TREE_ITER_* flags and tag index
291 * Returns: pointer to chunk first slot, or NULL if there no more left
293 * This function looks up the next chunk in the radix tree starting from
294 * @iter->next_index. It returns a pointer to the chunk's first slot.
295 * Also it fills @iter with data about chunk: position in the tree (index),
296 * its end (next_index), and constructs a bit mask for tagged iterating (tags).
298 void __rcu
**radix_tree_next_chunk(const struct radix_tree_root
*,
299 struct radix_tree_iter
*iter
, unsigned flags
);
302 * radix_tree_iter_lookup - look up an index in the radix tree
303 * @root: radix tree root
304 * @iter: iterator state
305 * @index: key to look up
307 * If @index is present in the radix tree, this function returns the slot
308 * containing it and updates @iter to describe the entry. If @index is not
309 * present, it returns NULL.
311 static inline void __rcu
**
312 radix_tree_iter_lookup(const struct radix_tree_root
*root
,
313 struct radix_tree_iter
*iter
, unsigned long index
)
315 radix_tree_iter_init(iter
, index
);
316 return radix_tree_next_chunk(root
, iter
, RADIX_TREE_ITER_CONTIG
);
320 * radix_tree_iter_retry - retry this chunk of the iteration
321 * @iter: iterator state
323 * If we iterate over a tree protected only by the RCU lock, a race
324 * against deletion or creation may result in seeing a slot for which
325 * radix_tree_deref_retry() returns true. If so, call this function
326 * and continue the iteration.
328 static inline __must_check
329 void __rcu
**radix_tree_iter_retry(struct radix_tree_iter
*iter
)
331 iter
->next_index
= iter
->index
;
336 static inline unsigned long
337 __radix_tree_iter_add(struct radix_tree_iter
*iter
, unsigned long slots
)
339 return iter
->index
+ slots
;
343 * radix_tree_iter_resume - resume iterating when the chunk may be invalid
344 * @slot: pointer to current slot
345 * @iter: iterator state
346 * Returns: New slot pointer
348 * If the iterator needs to release then reacquire a lock, the chunk may
349 * have been invalidated by an insertion or deletion. Call this function
350 * before releasing the lock to continue the iteration from the next index.
352 void __rcu
**__must_check
radix_tree_iter_resume(void __rcu
**slot
,
353 struct radix_tree_iter
*iter
);
356 * radix_tree_chunk_size - get current chunk size
358 * @iter: pointer to radix tree iterator
359 * Returns: current chunk size
361 static __always_inline
long
362 radix_tree_chunk_size(struct radix_tree_iter
*iter
)
364 return iter
->next_index
- iter
->index
;
368 * radix_tree_next_slot - find next slot in chunk
370 * @slot: pointer to current slot
371 * @iter: pointer to iterator state
372 * @flags: RADIX_TREE_ITER_*, should be constant
373 * Returns: pointer to next slot, or NULL if there no more left
375 * This function updates @iter->index in the case of a successful lookup.
376 * For tagged lookup it also eats @iter->tags.
378 * There are several cases where 'slot' can be passed in as NULL to this
379 * function. These cases result from the use of radix_tree_iter_resume() or
380 * radix_tree_iter_retry(). In these cases we don't end up dereferencing
381 * 'slot' because either:
382 * a) we are doing tagged iteration and iter->tags has been set to 0, or
383 * b) we are doing non-tagged iteration, and iter->index and iter->next_index
384 * have been set up so that radix_tree_chunk_size() returns 1 or 0.
386 static __always_inline
void __rcu
**radix_tree_next_slot(void __rcu
**slot
,
387 struct radix_tree_iter
*iter
, unsigned flags
)
389 if (flags
& RADIX_TREE_ITER_TAGGED
) {
391 if (unlikely(!iter
->tags
))
393 if (likely(iter
->tags
& 1ul)) {
394 iter
->index
= __radix_tree_iter_add(iter
, 1);
398 if (!(flags
& RADIX_TREE_ITER_CONTIG
)) {
399 unsigned offset
= __ffs(iter
->tags
);
401 iter
->tags
>>= offset
++;
402 iter
->index
= __radix_tree_iter_add(iter
, offset
);
407 long count
= radix_tree_chunk_size(iter
);
409 while (--count
> 0) {
411 iter
->index
= __radix_tree_iter_add(iter
, 1);
415 if (flags
& RADIX_TREE_ITER_CONTIG
) {
416 /* forbid switching to the next chunk */
417 iter
->next_index
= 0;
429 * radix_tree_for_each_slot - iterate over non-empty slots
431 * @slot: the void** variable for pointer to slot
432 * @root: the struct radix_tree_root pointer
433 * @iter: the struct radix_tree_iter pointer
434 * @start: iteration starting index
436 * @slot points to radix tree slot, @iter->index contains its index.
438 #define radix_tree_for_each_slot(slot, root, iter, start) \
439 for (slot = radix_tree_iter_init(iter, start) ; \
440 slot || (slot = radix_tree_next_chunk(root, iter, 0)) ; \
441 slot = radix_tree_next_slot(slot, iter, 0))
444 * radix_tree_for_each_tagged - iterate over tagged slots
446 * @slot: the void** variable for pointer to slot
447 * @root: the struct radix_tree_root pointer
448 * @iter: the struct radix_tree_iter pointer
449 * @start: iteration starting index
452 * @slot points to radix tree slot, @iter->index contains its index.
454 #define radix_tree_for_each_tagged(slot, root, iter, start, tag) \
455 for (slot = radix_tree_iter_init(iter, start) ; \
456 slot || (slot = radix_tree_next_chunk(root, iter, \
457 RADIX_TREE_ITER_TAGGED | tag)) ; \
458 slot = radix_tree_next_slot(slot, iter, \
459 RADIX_TREE_ITER_TAGGED | tag))
461 #endif /* _LINUX_RADIX_TREE_H */