blob: 63e62372443a501d33d42e6333419004ae614b6f [file] [log] [blame]
Thomas Gleixnerde6cc652019-05-27 08:55:02 +02001/* SPDX-License-Identifier: GPL-2.0-or-later */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * Copyright (C) 2001 Momchil Velikov
4 * Portions Copyright (C) 2001 Christoph Hellwig
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08005 * Copyright (C) 2006 Nick Piggin
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07006 * Copyright (C) 2012 Konstantin Khlebnikov
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 */
8#ifndef _LINUX_RADIX_TREE_H
9#define _LINUX_RADIX_TREE_H
10
Matthew Wilcoxf67c07f2016-03-17 14:21:42 -070011#include <linux/bitops.h>
Nick Piggin7cf9c2c2006-12-06 20:33:44 -080012#include <linux/kernel.h>
Matthew Wilcox15f2e88d2016-12-16 14:46:09 -050013#include <linux/list.h>
14#include <linux/preempt.h>
Nick Piggin7cf9c2c2006-12-06 20:33:44 -080015#include <linux/rcupdate.h>
Matthew Wilcox15f2e88d2016-12-16 14:46:09 -050016#include <linux/spinlock.h>
17#include <linux/types.h>
Matthew Wilcox3159f942017-11-03 13:30:42 -040018#include <linux/xarray.h>
Nick Piggin7cf9c2c2006-12-06 20:33:44 -080019
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -050020/* Keep unconverted code working */
21#define radix_tree_root xarray
Matthew Wilcox01959df2017-11-09 09:23:56 -050022#define radix_tree_node xa_node
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -050023
Nick Piggin7cf9c2c2006-12-06 20:33:44 -080024/*
Matthew Wilcox3bcadd62016-05-20 17:03:54 -070025 * The bottom two bits of the slot determine how the remaining bits in the
26 * slot are interpreted:
Nick Piggin7cf9c2c2006-12-06 20:33:44 -080027 *
Matthew Wilcox3bcadd62016-05-20 17:03:54 -070028 * 00 - data pointer
Matthew Wilcox3159f942017-11-03 13:30:42 -040029 * 10 - internal entry
30 * x1 - value entry
Matthew Wilcox3bcadd62016-05-20 17:03:54 -070031 *
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).
Matthew Wilcox3159f942017-11-03 13:30:42 -040037 * This means that storing a NULL entry in the tree is the same as deleting
38 * the entry from the tree.
Nick Piggin7cf9c2c2006-12-06 20:33:44 -080039 */
Matthew Wilcox3bcadd62016-05-20 17:03:54 -070040#define RADIX_TREE_ENTRY_MASK 3UL
Matthew Wilcox3159f942017-11-03 13:30:42 -040041#define RADIX_TREE_INTERNAL_NODE 2UL
Nick Piggin7cf9c2c2006-12-06 20:33:44 -080042
Matthew Wilcox3bcadd62016-05-20 17:03:54 -070043static inline bool radix_tree_is_internal_node(void *ptr)
Nick Piggin7cf9c2c2006-12-06 20:33:44 -080044{
Matthew Wilcox3bcadd62016-05-20 17:03:54 -070045 return ((unsigned long)ptr & RADIX_TREE_ENTRY_MASK) ==
46 RADIX_TREE_INTERNAL_NODE;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -080047}
48
49/*** radix-tree API starts here ***/
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Matthew Wilcox02c02bf2017-11-03 23:09:45 -040051#define RADIX_TREE_MAP_SHIFT XA_CHUNK_SHIFT
Johannes Weiner139e5612014-04-03 14:47:54 -070052#define RADIX_TREE_MAP_SIZE (1UL << RADIX_TREE_MAP_SHIFT)
53#define RADIX_TREE_MAP_MASK (RADIX_TREE_MAP_SIZE-1)
54
Matthew Wilcox01959df2017-11-09 09:23:56 -050055#define RADIX_TREE_MAX_TAGS XA_MAX_MARKS
56#define RADIX_TREE_TAG_LONGS XA_MARK_LONGS
Johannes Weiner139e5612014-04-03 14:47:54 -070057
Johannes Weiner139e5612014-04-03 14:47:54 -070058#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
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -050062/* The IDR tag is stored in the low bits of xa_flags */
Matthew Wilcoxfa290cd2018-04-10 16:36:28 -070063#define ROOT_IS_IDR ((__force gfp_t)4)
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -050064/* The top bits of xa_flags are used to store the root tags */
Matthew Wilcoxfa290cd2018-04-10 16:36:28 -070065#define ROOT_TAG_SHIFT (__GFP_BITS_SHIFT)
Matthew Wilcox0a835c42016-12-20 10:27:56 -050066
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -050067#define RADIX_TREE_INIT(name, mask) XARRAY_INIT(name, mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69#define RADIX_TREE(name, mask) \
Matthew Wilcoxf6bb2a22018-04-10 16:36:52 -070070 struct radix_tree_root name = RADIX_TREE_INIT(name, mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -050072#define INIT_RADIX_TREE(root, mask) xa_init_flags(root, mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Matthew Wilcox35534c82016-12-19 17:43:19 -050074static inline bool radix_tree_empty(const struct radix_tree_root *root)
Matthew Wilcoxe9256ef2016-05-20 17:01:33 -070075{
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -050076 return root->xa_head == NULL;
Matthew Wilcoxe9256ef2016-05-20 17:01:33 -070077}
78
Nick Piggin7cf9c2c2006-12-06 20:33:44 -080079/**
Matthew Wilcox268f42d2016-12-14 15:08:55 -080080 * 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
Matthew Wilcox268f42d2016-12-14 15:08:55 -080086 *
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;
Matthew Wilcox268f42d2016-12-14 15:08:55 -080099};
100
Matthew Wilcox268f42d2016-12-14 15:08:55 -0800101/**
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800102 * 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,
Michael Opdenacker59c51592007-05-09 08:57:56 +0200111 * - any function _modifying_ the tree or tags (inserting or deleting
Tim Peppereb8dc5e2008-02-03 16:12:47 +0200112 * items, setting or clearing tags) must exclude other modifications, and
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800113 * exclude any functions reading the tree.
Michael Opdenacker59c51592007-05-09 08:57:56 +0200114 * - any function _reading_ the tree or tags (looking up items or tags,
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800115 * 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:
Johannes Weiner139e5612014-04-03 14:47:54 -0700119 * __radix_tree_lookup
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800120 * radix_tree_lookup
Nick Piggin47feff22008-07-25 19:45:29 -0700121 * radix_tree_lookup_slot
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800122 * radix_tree_tag_get
123 * radix_tree_gang_lookup
124 * radix_tree_gang_lookup_tag
Nick Piggin47feff22008-07-25 19:45:29 -0700125 * radix_tree_gang_lookup_tag_slot
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800126 * radix_tree_tagged
127 *
Matthew Wilcox7b8d0462017-12-01 22:13:06 -0500128 * The first 7 functions are able to be called locklessly, using RCU. The
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800129 * 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 *
David Howellsce826532010-04-06 22:36:20 +0100142 * 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 *
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800149 * radix_tree_tagged is able to be called without locking or RCU.
150 */
151
152/**
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500153 * radix_tree_deref_slot - dereference a slot
154 * @slot: slot pointer, returned by radix_tree_lookup_slot
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800155 *
156 * For use with radix_tree_lookup_slot(). Caller must hold tree at least read
Nick Piggin27d20fd2010-11-11 14:05:19 -0800157 * 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.
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500162 *
163 * Return: entry stored in that slot.
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800164 */
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500165static inline void *radix_tree_deref_slot(void __rcu **slot)
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800166{
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500167 return rcu_dereference(*slot);
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800168}
Nick Piggin27d20fd2010-11-11 14:05:19 -0800169
170/**
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500171 * radix_tree_deref_slot_protected - dereference a slot with tree lock held
172 * @slot: slot pointer, returned by radix_tree_lookup_slot
Mel Gorman29c1f672011-01-13 15:47:21 -0800173 *
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500174 * 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.
176 *
177 * Return: entry stored in that slot.
Mel Gorman29c1f672011-01-13 15:47:21 -0800178 */
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500179static inline void *radix_tree_deref_slot_protected(void __rcu **slot,
Mel Gorman29c1f672011-01-13 15:47:21 -0800180 spinlock_t *treelock)
181{
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500182 return rcu_dereference_protected(*slot, lockdep_is_held(treelock));
Mel Gorman29c1f672011-01-13 15:47:21 -0800183}
184
185/**
Nick Piggin27d20fd2010-11-11 14:05:19 -0800186 * 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{
Matthew Wilcoxb194d162016-05-20 17:03:30 -0700194 return unlikely(radix_tree_is_internal_node(arg));
Nick Piggin27d20fd2010-11-11 14:05:19 -0800195}
196
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800197/**
Hugh Dickins63286502011-08-03 16:21:18 -0700198 * 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{
Matthew Wilcox3bcadd62016-05-20 17:03:54 -0700204 return unlikely((unsigned long)arg & RADIX_TREE_ENTRY_MASK);
Hugh Dickins63286502011-08-03 16:21:18 -0700205}
206
Matthew Wilcox3a08cd52018-09-22 16:14:30 -0400207int radix_tree_insert(struct radix_tree_root *, unsigned long index,
208 void *);
Matthew Wilcox35534c82016-12-19 17:43:19 -0500209void *__radix_tree_lookup(const struct radix_tree_root *, unsigned long index,
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500210 struct radix_tree_node **nodep, void __rcu ***slotp);
Matthew Wilcox35534c82016-12-19 17:43:19 -0500211void *radix_tree_lookup(const struct radix_tree_root *, unsigned long);
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500212void __rcu **radix_tree_lookup_slot(const struct radix_tree_root *,
213 unsigned long index);
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500214void __radix_tree_replace(struct radix_tree_root *, struct radix_tree_node *,
Matthew Wilcox1cf56f92018-04-09 16:24:45 -0400215 void __rcu **slot, void *entry);
Matthew Wilcoxe157b552016-12-14 15:09:01 -0800216void radix_tree_iter_replace(struct radix_tree_root *,
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500217 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);
Matthew Wilcox0ac398e2017-01-28 09:56:22 -0500220void radix_tree_iter_delete(struct radix_tree_root *,
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500221 struct radix_tree_iter *iter, void __rcu **slot);
Johannes Weiner53c59f22014-04-03 14:47:39 -0700222void *radix_tree_delete_item(struct radix_tree_root *, unsigned long, void *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223void *radix_tree_delete(struct radix_tree_root *, unsigned long);
Matthew Wilcox35534c82016-12-19 17:43:19 -0500224unsigned int radix_tree_gang_lookup(const struct radix_tree_root *,
Matthew Wilcoxd604c322016-05-20 17:03:45 -0700225 void **results, unsigned long first_index,
226 unsigned int max_items);
Al Virodd0fc662005-10-07 07:46:04 +0100227int radix_tree_preload(gfp_t gfp_mask);
Jan Kara5e4c0d972013-09-11 14:26:05 -0700228int radix_tree_maybe_preload(gfp_t gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229void radix_tree_init(void);
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500230void *radix_tree_tag_set(struct radix_tree_root *,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800231 unsigned long index, unsigned int tag);
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500232void *radix_tree_tag_clear(struct radix_tree_root *,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800233 unsigned long index, unsigned int tag);
Matthew Wilcox35534c82016-12-19 17:43:19 -0500234int radix_tree_tag_get(const struct radix_tree_root *,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800235 unsigned long index, unsigned int tag);
Matthew Wilcox30b888b2017-01-28 09:55:20 -0500236void radix_tree_iter_tag_clear(struct radix_tree_root *,
Matthew Wilcox268f42d2016-12-14 15:08:55 -0800237 const struct radix_tree_iter *iter, unsigned int tag);
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500238unsigned 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
246static inline void radix_tree_preload_end(void)
247{
248 preempt_enable();
249}
250
Matthew Wilcox460488c2017-11-28 15:16:24 -0500251void __rcu **idr_get_free(struct radix_tree_root *root,
Chris Mi388f79f2017-08-30 02:31:57 -0400252 struct radix_tree_iter *iter, gfp_t gfp,
253 unsigned long max);
Matthew Wilcox175542f2016-12-14 15:08:58 -0800254
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500255enum {
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};
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700260
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 */
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500268static __always_inline void __rcu **
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700269radix_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 */
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500297void __rcu **radix_tree_next_chunk(const struct radix_tree_root *,
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700298 struct radix_tree_iter *iter, unsigned flags);
299
300/**
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500301 * 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 */
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500310static inline void __rcu **
311radix_tree_iter_lookup(const struct radix_tree_root *root,
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500312 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/**
Matthew Wilcox46437f92016-02-02 16:57:52 -0800319 * radix_tree_iter_retry - retry this chunk of the iteration
320 * @iter: iterator state
321 *
322 * If we iterate over a tree protected only by the RCU lock, a race
323 * against deletion or creation may result in seeing a slot for which
324 * radix_tree_deref_retry() returns true. If so, call this function
325 * and continue the iteration.
326 */
327static inline __must_check
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500328void __rcu **radix_tree_iter_retry(struct radix_tree_iter *iter)
Matthew Wilcox46437f92016-02-02 16:57:52 -0800329{
330 iter->next_index = iter->index;
Andrey Ryabinin3cb91852016-07-20 15:45:00 -0700331 iter->tags = 0;
Matthew Wilcox46437f92016-02-02 16:57:52 -0800332 return NULL;
333}
334
Ross Zwisler21ef5332016-05-20 17:02:26 -0700335static inline unsigned long
336__radix_tree_iter_add(struct radix_tree_iter *iter, unsigned long slots)
337{
Matthew Wilcox3a08cd52018-09-22 16:14:30 -0400338 return iter->index + slots;
Ross Zwisler21ef5332016-05-20 17:02:26 -0700339}
340
Matthew Wilcox46437f92016-02-02 16:57:52 -0800341/**
Matthew Wilcox148deab2016-12-14 15:08:49 -0800342 * radix_tree_iter_resume - resume iterating when the chunk may be invalid
343 * @slot: pointer to current slot
344 * @iter: iterator state
345 * Returns: New slot pointer
Matthew Wilcox71650922016-03-17 14:22:06 -0700346 *
347 * If the iterator needs to release then reacquire a lock, the chunk may
348 * have been invalidated by an insertion or deletion. Call this function
Matthew Wilcox148deab2016-12-14 15:08:49 -0800349 * before releasing the lock to continue the iteration from the next index.
Matthew Wilcox71650922016-03-17 14:22:06 -0700350 */
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500351void __rcu **__must_check radix_tree_iter_resume(void __rcu **slot,
Matthew Wilcox148deab2016-12-14 15:08:49 -0800352 struct radix_tree_iter *iter);
Matthew Wilcox71650922016-03-17 14:22:06 -0700353
354/**
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700355 * radix_tree_chunk_size - get current chunk size
356 *
357 * @iter: pointer to radix tree iterator
358 * Returns: current chunk size
359 */
Konstantin Khlebnikov73204282016-02-05 15:37:01 -0800360static __always_inline long
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700361radix_tree_chunk_size(struct radix_tree_iter *iter)
362{
Matthew Wilcox3a08cd52018-09-22 16:14:30 -0400363 return iter->next_index - iter->index;
Ross Zwisler21ef5332016-05-20 17:02:26 -0700364}
365
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700366/**
367 * radix_tree_next_slot - find next slot in chunk
368 *
369 * @slot: pointer to current slot
370 * @iter: pointer to interator state
371 * @flags: RADIX_TREE_ITER_*, should be constant
372 * Returns: pointer to next slot, or NULL if there no more left
373 *
374 * This function updates @iter->index in the case of a successful lookup.
375 * For tagged lookup it also eats @iter->tags.
Ross Zwisler915045f2016-10-11 13:51:18 -0700376 *
377 * There are several cases where 'slot' can be passed in as NULL to this
Matthew Wilcox148deab2016-12-14 15:08:49 -0800378 * function. These cases result from the use of radix_tree_iter_resume() or
Ross Zwisler915045f2016-10-11 13:51:18 -0700379 * radix_tree_iter_retry(). In these cases we don't end up dereferencing
380 * 'slot' because either:
381 * a) we are doing tagged iteration and iter->tags has been set to 0, or
382 * b) we are doing non-tagged iteration, and iter->index and iter->next_index
383 * have been set up so that radix_tree_chunk_size() returns 1 or 0.
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700384 */
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500385static __always_inline void __rcu **radix_tree_next_slot(void __rcu **slot,
386 struct radix_tree_iter *iter, unsigned flags)
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700387{
388 if (flags & RADIX_TREE_ITER_TAGGED) {
389 iter->tags >>= 1;
Ross Zwisler21ef5332016-05-20 17:02:26 -0700390 if (unlikely(!iter->tags))
391 return NULL;
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700392 if (likely(iter->tags & 1ul)) {
Ross Zwisler21ef5332016-05-20 17:02:26 -0700393 iter->index = __radix_tree_iter_add(iter, 1);
Matthew Wilcox148deab2016-12-14 15:08:49 -0800394 slot++;
395 goto found;
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700396 }
Ross Zwisler21ef5332016-05-20 17:02:26 -0700397 if (!(flags & RADIX_TREE_ITER_CONTIG)) {
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700398 unsigned offset = __ffs(iter->tags);
399
Matthew Wilcox148deab2016-12-14 15:08:49 -0800400 iter->tags >>= offset++;
401 iter->index = __radix_tree_iter_add(iter, offset);
402 slot += offset;
403 goto found;
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700404 }
405 } else {
Ross Zwisler21ef5332016-05-20 17:02:26 -0700406 long count = radix_tree_chunk_size(iter);
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700407
Ross Zwisler21ef5332016-05-20 17:02:26 -0700408 while (--count > 0) {
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700409 slot++;
Ross Zwisler21ef5332016-05-20 17:02:26 -0700410 iter->index = __radix_tree_iter_add(iter, 1);
411
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700412 if (likely(*slot))
Matthew Wilcox148deab2016-12-14 15:08:49 -0800413 goto found;
Konstantin Khlebnikovfffaee32012-06-05 21:36:33 +0400414 if (flags & RADIX_TREE_ITER_CONTIG) {
415 /* forbid switching to the next chunk */
416 iter->next_index = 0;
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700417 break;
Konstantin Khlebnikovfffaee32012-06-05 21:36:33 +0400418 }
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700419 }
420 }
421 return NULL;
Matthew Wilcox148deab2016-12-14 15:08:49 -0800422
423 found:
Matthew Wilcox148deab2016-12-14 15:08:49 -0800424 return slot;
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700425}
426
427/**
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700428 * radix_tree_for_each_slot - iterate over non-empty slots
429 *
430 * @slot: the void** variable for pointer to slot
431 * @root: the struct radix_tree_root pointer
432 * @iter: the struct radix_tree_iter pointer
433 * @start: iteration starting index
434 *
435 * @slot points to radix tree slot, @iter->index contains its index.
436 */
437#define radix_tree_for_each_slot(slot, root, iter, start) \
438 for (slot = radix_tree_iter_init(iter, start) ; \
439 slot || (slot = radix_tree_next_chunk(root, iter, 0)) ; \
440 slot = radix_tree_next_slot(slot, iter, 0))
441
442/**
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700443 * radix_tree_for_each_tagged - iterate over tagged slots
444 *
445 * @slot: the void** variable for pointer to slot
446 * @root: the struct radix_tree_root pointer
447 * @iter: the struct radix_tree_iter pointer
448 * @start: iteration starting index
449 * @tag: tag index
450 *
451 * @slot points to radix tree slot, @iter->index contains its index.
452 */
453#define radix_tree_for_each_tagged(slot, root, iter, start, tag) \
454 for (slot = radix_tree_iter_init(iter, start) ; \
455 slot || (slot = radix_tree_next_chunk(root, iter, \
456 RADIX_TREE_ITER_TAGGED | tag)) ; \
457 slot = radix_tree_next_slot(slot, iter, \
Matthew Wilcox148deab2016-12-14 15:08:49 -0800458 RADIX_TREE_ITER_TAGGED | tag))
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460#endif /* _LINUX_RADIX_TREE_H */