Merge branch 'linus' of git://git.kernel.org/pub/scm/linux/kernel/git/herbert/crypto-2.6
[linux-2.6-block.git] / include / linux / radix-tree.h
CommitLineData
de6cc651 1/* SPDX-License-Identifier: GPL-2.0-or-later */
1da177e4
LT
2/*
3 * Copyright (C) 2001 Momchil Velikov
4 * Portions Copyright (C) 2001 Christoph Hellwig
7cf9c2c7 5 * Copyright (C) 2006 Nick Piggin
78c1d784 6 * Copyright (C) 2012 Konstantin Khlebnikov
1da177e4
LT
7 */
8#ifndef _LINUX_RADIX_TREE_H
9#define _LINUX_RADIX_TREE_H
10
f67c07f0 11#include <linux/bitops.h>
7cf9c2c7 12#include <linux/kernel.h>
15f2e88d
MW
13#include <linux/list.h>
14#include <linux/preempt.h>
7cf9c2c7 15#include <linux/rcupdate.h>
15f2e88d
MW
16#include <linux/spinlock.h>
17#include <linux/types.h>
3159f943 18#include <linux/xarray.h>
7cf9c2c7 19
f8d5d0cc
MW
20/* Keep unconverted code working */
21#define radix_tree_root xarray
01959dfe 22#define radix_tree_node xa_node
f8d5d0cc 23
7cf9c2c7 24/*
3bcadd6f
MW
25 * The bottom two bits of the slot determine how the remaining bits in the
26 * slot are interpreted:
7cf9c2c7 27 *
3bcadd6f 28 * 00 - data pointer
3159f943
MW
29 * 10 - internal entry
30 * x1 - value entry
3bcadd6f
MW
31 *
32 * The internal entry may be a pointer to the next level in the tree, a
33 * sibling entry, or an indicator that the entry in this slot has been moved
34 * to another location in the tree and the lookup should be restarted. While
35 * NULL fits the 'data pointer' pattern, it means that there is no entry in
36 * the tree for this index (no matter what level of the tree it is found at).
3159f943
MW
37 * This means that storing a NULL entry in the tree is the same as deleting
38 * the entry from the tree.
7cf9c2c7 39 */
3bcadd6f 40#define RADIX_TREE_ENTRY_MASK 3UL
3159f943 41#define RADIX_TREE_INTERNAL_NODE 2UL
7cf9c2c7 42
3bcadd6f 43static inline bool radix_tree_is_internal_node(void *ptr)
7cf9c2c7 44{
3bcadd6f
MW
45 return ((unsigned long)ptr & RADIX_TREE_ENTRY_MASK) ==
46 RADIX_TREE_INTERNAL_NODE;
7cf9c2c7
NP
47}
48
49/*** radix-tree API starts here ***/
1da177e4 50
02c02bf1 51#define RADIX_TREE_MAP_SHIFT XA_CHUNK_SHIFT
139e5616
JW
52#define RADIX_TREE_MAP_SIZE (1UL << RADIX_TREE_MAP_SHIFT)
53#define RADIX_TREE_MAP_MASK (RADIX_TREE_MAP_SIZE-1)
54
01959dfe
MW
55#define RADIX_TREE_MAX_TAGS XA_MAX_MARKS
56#define RADIX_TREE_TAG_LONGS XA_MARK_LONGS
139e5616 57
449dd698
JW
58#define RADIX_TREE_INDEX_BITS (8 /* CHAR_BIT */ * sizeof(unsigned long))
59#define RADIX_TREE_MAX_PATH (DIV_ROUND_UP(RADIX_TREE_INDEX_BITS, \
60 RADIX_TREE_MAP_SHIFT))
61
f8d5d0cc 62/* The IDR tag is stored in the low bits of xa_flags */
fa290cda 63#define ROOT_IS_IDR ((__force gfp_t)4)
f8d5d0cc 64/* The top bits of xa_flags are used to store the root tags */
fa290cda 65#define ROOT_TAG_SHIFT (__GFP_BITS_SHIFT)
0a835c4f 66
f8d5d0cc 67#define RADIX_TREE_INIT(name, mask) XARRAY_INIT(name, mask)
1da177e4
LT
68
69#define RADIX_TREE(name, mask) \
f6bb2a2c 70 struct radix_tree_root name = RADIX_TREE_INIT(name, mask)
1da177e4 71
f8d5d0cc 72#define INIT_RADIX_TREE(root, mask) xa_init_flags(root, mask)
1da177e4 73
35534c86 74static inline bool radix_tree_empty(const struct radix_tree_root *root)
e9256efc 75{
f8d5d0cc 76 return root->xa_head == NULL;
e9256efc
MW
77}
78
268f42de
MW
79/**
80 * struct radix_tree_iter - radix tree iterator state
81 *
82 * @index: index of current slot
83 * @next_index: one beyond the last index for this chunk
84 * @tags: bit-mask for tag-iterating
85 * @node: node that contains current slot
268f42de
MW
86 *
87 * This radix tree iterator works in terms of "chunks" of slots. A chunk is a
88 * subinterval of slots contained within one radix tree leaf node. It is
89 * described by a pointer to its first slot and a struct radix_tree_iter
90 * which holds the chunk's position in the tree and its size. For tagged
91 * iteration radix_tree_iter also holds the slots' bit-mask for one chosen
92 * radix tree tag.
93 */
94struct radix_tree_iter {
95 unsigned long index;
96 unsigned long next_index;
97 unsigned long tags;
98 struct radix_tree_node *node;
268f42de
MW
99};
100
7cf9c2c7
NP
101/**
102 * Radix-tree synchronization
103 *
104 * The radix-tree API requires that users provide all synchronisation (with
105 * specific exceptions, noted below).
106 *
107 * Synchronization of access to the data items being stored in the tree, and
108 * management of their lifetimes must be completely managed by API users.
109 *
110 * For API usage, in general,
59c51591 111 * - any function _modifying_ the tree or tags (inserting or deleting
eb8dc5e7 112 * items, setting or clearing tags) must exclude other modifications, and
7cf9c2c7 113 * exclude any functions reading the tree.
59c51591 114 * - any function _reading_ the tree or tags (looking up items or tags,
7cf9c2c7
NP
115 * gang lookups) must exclude modifications to the tree, but may occur
116 * concurrently with other readers.
117 *
118 * The notable exceptions to this rule are the following functions:
139e5616 119 * __radix_tree_lookup
7cf9c2c7 120 * radix_tree_lookup
47feff2c 121 * radix_tree_lookup_slot
7cf9c2c7
NP
122 * radix_tree_tag_get
123 * radix_tree_gang_lookup
124 * radix_tree_gang_lookup_tag
47feff2c 125 * radix_tree_gang_lookup_tag_slot
7cf9c2c7
NP
126 * radix_tree_tagged
127 *
7b8d046f 128 * The first 7 functions are able to be called locklessly, using RCU. The
7cf9c2c7
NP
129 * caller must ensure calls to these functions are made within rcu_read_lock()
130 * regions. Other readers (lock-free or otherwise) and modifications may be
131 * running concurrently.
132 *
133 * It is still required that the caller manage the synchronization and lifetimes
134 * of the items. So if RCU lock-free lookups are used, typically this would mean
135 * that the items have their own locks, or are amenable to lock-free access; and
136 * that the items are freed by RCU (or only freed after having been deleted from
137 * the radix tree *and* a synchronize_rcu() grace period).
138 *
139 * (Note, rcu_assign_pointer and rcu_dereference are not needed to control
140 * access to data items when inserting into or looking up from the radix tree)
141 *
ce82653d
DH
142 * Note that the value returned by radix_tree_tag_get() may not be relied upon
143 * if only the RCU read lock is held. Functions to set/clear tags and to
144 * delete nodes running concurrently with it may affect its result such that
145 * two consecutive reads in the same locked section may return different
146 * values. If reliability is required, modification functions must also be
147 * excluded from concurrency.
148 *
7cf9c2c7
NP
149 * radix_tree_tagged is able to be called without locking or RCU.
150 */
151
152/**
d7b62727
MW
153 * radix_tree_deref_slot - dereference a slot
154 * @slot: slot pointer, returned by radix_tree_lookup_slot
7cf9c2c7
NP
155 *
156 * For use with radix_tree_lookup_slot(). Caller must hold tree at least read
27d20fdd
NP
157 * locked across slot lookup and dereference. Not required if write lock is
158 * held (ie. items cannot be concurrently inserted).
159 *
160 * radix_tree_deref_retry must be used to confirm validity of the pointer if
161 * only the read lock is held.
d7b62727
MW
162 *
163 * Return: entry stored in that slot.
7cf9c2c7 164 */
d7b62727 165static inline void *radix_tree_deref_slot(void __rcu **slot)
7cf9c2c7 166{
d7b62727 167 return rcu_dereference(*slot);
7cf9c2c7 168}
27d20fdd 169
29c1f677 170/**
d7b62727
MW
171 * radix_tree_deref_slot_protected - dereference a slot with tree lock held
172 * @slot: slot pointer, returned by radix_tree_lookup_slot
173 *
174 * Similar to radix_tree_deref_slot. The caller does not hold the RCU read
175 * lock but it must hold the tree lock to prevent parallel updates.
29c1f677 176 *
d7b62727 177 * Return: entry stored in that slot.
29c1f677 178 */
d7b62727 179static inline void *radix_tree_deref_slot_protected(void __rcu **slot,
29c1f677
MG
180 spinlock_t *treelock)
181{
d7b62727 182 return rcu_dereference_protected(*slot, lockdep_is_held(treelock));
29c1f677
MG
183}
184
27d20fdd
NP
185/**
186 * radix_tree_deref_retry - check radix_tree_deref_slot
187 * @arg: pointer returned by radix_tree_deref_slot
188 * Returns: 0 if retry is not required, otherwise retry is required
189 *
190 * radix_tree_deref_retry must be used with radix_tree_deref_slot.
191 */
192static inline int radix_tree_deref_retry(void *arg)
193{
b194d16c 194 return unlikely(radix_tree_is_internal_node(arg));
27d20fdd
NP
195}
196
6328650b
HD
197/**
198 * radix_tree_exception - radix_tree_deref_slot returned either exception?
199 * @arg: value returned by radix_tree_deref_slot
200 * Returns: 0 if well-aligned pointer, non-0 if either kind of exception.
201 */
202static inline int radix_tree_exception(void *arg)
203{
3bcadd6f 204 return unlikely((unsigned long)arg & RADIX_TREE_ENTRY_MASK);
6328650b
HD
205}
206
3a08cd52
MW
207int radix_tree_insert(struct radix_tree_root *, unsigned long index,
208 void *);
35534c86 209void *__radix_tree_lookup(const struct radix_tree_root *, unsigned long index,
d7b62727 210 struct radix_tree_node **nodep, void __rcu ***slotp);
35534c86 211void *radix_tree_lookup(const struct radix_tree_root *, unsigned long);
d7b62727
MW
212void __rcu **radix_tree_lookup_slot(const struct radix_tree_root *,
213 unsigned long index);
d7b62727 214void __radix_tree_replace(struct radix_tree_root *, struct radix_tree_node *,
1cf56f9d 215 void __rcu **slot, void *entry);
e157b555 216void radix_tree_iter_replace(struct radix_tree_root *,
d7b62727
MW
217 const struct radix_tree_iter *, void __rcu **slot, void *entry);
218void radix_tree_replace_slot(struct radix_tree_root *,
219 void __rcu **slot, void *entry);
0ac398ef 220void radix_tree_iter_delete(struct radix_tree_root *,
d7b62727 221 struct radix_tree_iter *iter, void __rcu **slot);
53c59f26 222void *radix_tree_delete_item(struct radix_tree_root *, unsigned long, void *);
1da177e4 223void *radix_tree_delete(struct radix_tree_root *, unsigned long);
35534c86 224unsigned int radix_tree_gang_lookup(const struct radix_tree_root *,
d604c324
MW
225 void **results, unsigned long first_index,
226 unsigned int max_items);
dd0fc66f 227int radix_tree_preload(gfp_t gfp_mask);
5e4c0d97 228int radix_tree_maybe_preload(gfp_t gfp_mask);
1da177e4 229void radix_tree_init(void);
d7b62727 230void *radix_tree_tag_set(struct radix_tree_root *,
daff89f3 231 unsigned long index, unsigned int tag);
d7b62727 232void *radix_tree_tag_clear(struct radix_tree_root *,
daff89f3 233 unsigned long index, unsigned int tag);
35534c86 234int radix_tree_tag_get(const struct radix_tree_root *,
daff89f3 235 unsigned long index, unsigned int tag);
30b888ba 236void radix_tree_iter_tag_clear(struct radix_tree_root *,
268f42de 237 const struct radix_tree_iter *iter, unsigned int tag);
d7b62727
MW
238unsigned int radix_tree_gang_lookup_tag(const struct radix_tree_root *,
239 void **results, unsigned long first_index,
240 unsigned int max_items, unsigned int tag);
241unsigned int radix_tree_gang_lookup_tag_slot(const struct radix_tree_root *,
242 void __rcu ***results, unsigned long first_index,
243 unsigned int max_items, unsigned int tag);
244int radix_tree_tagged(const struct radix_tree_root *, unsigned int tag);
1da177e4
LT
245
246static inline void radix_tree_preload_end(void)
247{
248 preempt_enable();
249}
250
460488c5 251void __rcu **idr_get_free(struct radix_tree_root *root,
388f79fd
CM
252 struct radix_tree_iter *iter, gfp_t gfp,
253 unsigned long max);
175542f5 254
0a835c4f
MW
255enum {
256 RADIX_TREE_ITER_TAG_MASK = 0x0f, /* tag index in lower nybble */
257 RADIX_TREE_ITER_TAGGED = 0x10, /* lookup tagged slots */
258 RADIX_TREE_ITER_CONTIG = 0x20, /* stop at first hole */
259};
78c1d784
KK
260
261/**
262 * radix_tree_iter_init - initialize radix tree iterator
263 *
264 * @iter: pointer to iterator state
265 * @start: iteration starting index
266 * Returns: NULL
267 */
d7b62727 268static __always_inline void __rcu **
78c1d784
KK
269radix_tree_iter_init(struct radix_tree_iter *iter, unsigned long start)
270{
271 /*
272 * Leave iter->tags uninitialized. radix_tree_next_chunk() will fill it
273 * in the case of a successful tagged chunk lookup. If the lookup was
274 * unsuccessful or non-tagged then nobody cares about ->tags.
275 *
276 * Set index to zero to bypass next_index overflow protection.
277 * See the comment in radix_tree_next_chunk() for details.
278 */
279 iter->index = 0;
280 iter->next_index = start;
281 return NULL;
282}
283
284/**
285 * radix_tree_next_chunk - find next chunk of slots for iteration
286 *
287 * @root: radix tree root
288 * @iter: iterator state
289 * @flags: RADIX_TREE_ITER_* flags and tag index
290 * Returns: pointer to chunk first slot, or NULL if there no more left
291 *
292 * This function looks up the next chunk in the radix tree starting from
293 * @iter->next_index. It returns a pointer to the chunk's first slot.
294 * Also it fills @iter with data about chunk: position in the tree (index),
295 * its end (next_index), and constructs a bit mask for tagged iterating (tags).
296 */
d7b62727 297void __rcu **radix_tree_next_chunk(const struct radix_tree_root *,
78c1d784
KK
298 struct radix_tree_iter *iter, unsigned flags);
299
0a835c4f
MW
300/**
301 * radix_tree_iter_lookup - look up an index in the radix tree
302 * @root: radix tree root
303 * @iter: iterator state
304 * @index: key to look up
305 *
306 * If @index is present in the radix tree, this function returns the slot
307 * containing it and updates @iter to describe the entry. If @index is not
308 * present, it returns NULL.
309 */
d7b62727
MW
310static inline void __rcu **
311radix_tree_iter_lookup(const struct radix_tree_root *root,
0a835c4f
MW
312 struct radix_tree_iter *iter, unsigned long index)
313{
314 radix_tree_iter_init(iter, index);
315 return radix_tree_next_chunk(root, iter, RADIX_TREE_ITER_CONTIG);
316}
317
318/**
319 * radix_tree_iter_find - find a present entry
320 * @root: radix tree root
321 * @iter: iterator state
322 * @index: start location
323 *
324 * This function returns the slot containing the entry with the lowest index
325 * which is at least @index. If @index is larger than any present entry, this
326 * function returns NULL. The @iter is updated to describe the entry found.
327 */
d7b62727
MW
328static inline void __rcu **
329radix_tree_iter_find(const struct radix_tree_root *root,
0a835c4f
MW
330 struct radix_tree_iter *iter, unsigned long index)
331{
332 radix_tree_iter_init(iter, index);
333 return radix_tree_next_chunk(root, iter, 0);
334}
335
46437f9a
MW
336/**
337 * radix_tree_iter_retry - retry this chunk of the iteration
338 * @iter: iterator state
339 *
340 * If we iterate over a tree protected only by the RCU lock, a race
341 * against deletion or creation may result in seeing a slot for which
342 * radix_tree_deref_retry() returns true. If so, call this function
343 * and continue the iteration.
344 */
345static inline __must_check
d7b62727 346void __rcu **radix_tree_iter_retry(struct radix_tree_iter *iter)
46437f9a
MW
347{
348 iter->next_index = iter->index;
3cb9185c 349 iter->tags = 0;
46437f9a
MW
350 return NULL;
351}
352
21ef5339
RZ
353static inline unsigned long
354__radix_tree_iter_add(struct radix_tree_iter *iter, unsigned long slots)
355{
3a08cd52 356 return iter->index + slots;
21ef5339
RZ
357}
358
7165092f 359/**
148deab2
MW
360 * radix_tree_iter_resume - resume iterating when the chunk may be invalid
361 * @slot: pointer to current slot
362 * @iter: iterator state
363 * Returns: New slot pointer
7165092f
MW
364 *
365 * If the iterator needs to release then reacquire a lock, the chunk may
366 * have been invalidated by an insertion or deletion. Call this function
148deab2 367 * before releasing the lock to continue the iteration from the next index.
7165092f 368 */
d7b62727 369void __rcu **__must_check radix_tree_iter_resume(void __rcu **slot,
148deab2 370 struct radix_tree_iter *iter);
7165092f 371
78c1d784
KK
372/**
373 * radix_tree_chunk_size - get current chunk size
374 *
375 * @iter: pointer to radix tree iterator
376 * Returns: current chunk size
377 */
73204282 378static __always_inline long
78c1d784
KK
379radix_tree_chunk_size(struct radix_tree_iter *iter)
380{
3a08cd52 381 return iter->next_index - iter->index;
78c1d784
KK
382}
383
384/**
385 * radix_tree_next_slot - find next slot in chunk
386 *
387 * @slot: pointer to current slot
388 * @iter: pointer to interator state
389 * @flags: RADIX_TREE_ITER_*, should be constant
390 * Returns: pointer to next slot, or NULL if there no more left
391 *
392 * This function updates @iter->index in the case of a successful lookup.
393 * For tagged lookup it also eats @iter->tags.
915045fe
RZ
394 *
395 * There are several cases where 'slot' can be passed in as NULL to this
148deab2 396 * function. These cases result from the use of radix_tree_iter_resume() or
915045fe
RZ
397 * radix_tree_iter_retry(). In these cases we don't end up dereferencing
398 * 'slot' because either:
399 * a) we are doing tagged iteration and iter->tags has been set to 0, or
400 * b) we are doing non-tagged iteration, and iter->index and iter->next_index
401 * have been set up so that radix_tree_chunk_size() returns 1 or 0.
78c1d784 402 */
d7b62727
MW
403static __always_inline void __rcu **radix_tree_next_slot(void __rcu **slot,
404 struct radix_tree_iter *iter, unsigned flags)
78c1d784
KK
405{
406 if (flags & RADIX_TREE_ITER_TAGGED) {
407 iter->tags >>= 1;
21ef5339
RZ
408 if (unlikely(!iter->tags))
409 return NULL;
78c1d784 410 if (likely(iter->tags & 1ul)) {
21ef5339 411 iter->index = __radix_tree_iter_add(iter, 1);
148deab2
MW
412 slot++;
413 goto found;
78c1d784 414 }
21ef5339 415 if (!(flags & RADIX_TREE_ITER_CONTIG)) {
78c1d784
KK
416 unsigned offset = __ffs(iter->tags);
417
148deab2
MW
418 iter->tags >>= offset++;
419 iter->index = __radix_tree_iter_add(iter, offset);
420 slot += offset;
421 goto found;
78c1d784
KK
422 }
423 } else {
21ef5339 424 long count = radix_tree_chunk_size(iter);
78c1d784 425
21ef5339 426 while (--count > 0) {
78c1d784 427 slot++;
21ef5339
RZ
428 iter->index = __radix_tree_iter_add(iter, 1);
429
78c1d784 430 if (likely(*slot))
148deab2 431 goto found;
fffaee36
KK
432 if (flags & RADIX_TREE_ITER_CONTIG) {
433 /* forbid switching to the next chunk */
434 iter->next_index = 0;
78c1d784 435 break;
fffaee36 436 }
78c1d784
KK
437 }
438 }
439 return NULL;
148deab2
MW
440
441 found:
148deab2 442 return slot;
78c1d784
KK
443}
444
78c1d784
KK
445/**
446 * radix_tree_for_each_slot - iterate over non-empty slots
447 *
448 * @slot: the void** variable for pointer to slot
449 * @root: the struct radix_tree_root pointer
450 * @iter: the struct radix_tree_iter pointer
451 * @start: iteration starting index
452 *
453 * @slot points to radix tree slot, @iter->index contains its index.
454 */
455#define radix_tree_for_each_slot(slot, root, iter, start) \
456 for (slot = radix_tree_iter_init(iter, start) ; \
457 slot || (slot = radix_tree_next_chunk(root, iter, 0)) ; \
458 slot = radix_tree_next_slot(slot, iter, 0))
459
78c1d784
KK
460/**
461 * radix_tree_for_each_tagged - iterate over tagged slots
462 *
463 * @slot: the void** variable for pointer to slot
464 * @root: the struct radix_tree_root pointer
465 * @iter: the struct radix_tree_iter pointer
466 * @start: iteration starting index
467 * @tag: tag index
468 *
469 * @slot points to radix tree slot, @iter->index contains its index.
470 */
471#define radix_tree_for_each_tagged(slot, root, iter, start, tag) \
472 for (slot = radix_tree_iter_init(iter, start) ; \
473 slot || (slot = radix_tree_next_chunk(root, iter, \
474 RADIX_TREE_ITER_TAGGED | tag)) ; \
475 slot = radix_tree_next_slot(slot, iter, \
148deab2 476 RADIX_TREE_ITER_TAGGED | tag))
78c1d784 477
1da177e4 478#endif /* _LINUX_RADIX_TREE_H */