xref: /linux-6.15/include/linux/xarray.h (revision b803b428)
1f6bb2a2cSMatthew Wilcox /* SPDX-License-Identifier: GPL-2.0+ */
2f6bb2a2cSMatthew Wilcox #ifndef _LINUX_XARRAY_H
3f6bb2a2cSMatthew Wilcox #define _LINUX_XARRAY_H
4f6bb2a2cSMatthew Wilcox /*
5f6bb2a2cSMatthew Wilcox  * eXtensible Arrays
6f6bb2a2cSMatthew Wilcox  * Copyright (c) 2017 Microsoft Corporation
73d0186bbSMatthew Wilcox  * Author: Matthew Wilcox <[email protected]>
83159f943SMatthew Wilcox  *
93159f943SMatthew Wilcox  * See Documentation/core-api/xarray.rst for how to use the XArray.
10f6bb2a2cSMatthew Wilcox  */
11f6bb2a2cSMatthew Wilcox 
123159f943SMatthew Wilcox #include <linux/bug.h>
13f8d5d0ccSMatthew Wilcox #include <linux/compiler.h>
149b89a035SMatthew Wilcox #include <linux/gfp.h>
15f8d5d0ccSMatthew Wilcox #include <linux/kconfig.h>
16ad3d6c72SMatthew Wilcox #include <linux/kernel.h>
17ad3d6c72SMatthew Wilcox #include <linux/rcupdate.h>
18f6bb2a2cSMatthew Wilcox #include <linux/spinlock.h>
193159f943SMatthew Wilcox #include <linux/types.h>
203159f943SMatthew Wilcox 
213159f943SMatthew Wilcox /*
223159f943SMatthew Wilcox  * The bottom two bits of the entry determine how the XArray interprets
233159f943SMatthew Wilcox  * the contents:
243159f943SMatthew Wilcox  *
253159f943SMatthew Wilcox  * 00: Pointer entry
263159f943SMatthew Wilcox  * 10: Internal entry
273159f943SMatthew Wilcox  * x1: Value entry or tagged pointer
283159f943SMatthew Wilcox  *
293159f943SMatthew Wilcox  * Attempting to store internal entries in the XArray is a bug.
3002c02bf1SMatthew Wilcox  *
3102c02bf1SMatthew Wilcox  * Most internal entries are pointers to the next node in the tree.
3202c02bf1SMatthew Wilcox  * The following internal entries have a special meaning:
3302c02bf1SMatthew Wilcox  *
3402c02bf1SMatthew Wilcox  * 0-62: Sibling entries
3502c02bf1SMatthew Wilcox  * 256: Retry entry
36ad3d6c72SMatthew Wilcox  *
37ad3d6c72SMatthew Wilcox  * Errors are also represented as internal entries, but use the negative
38ad3d6c72SMatthew Wilcox  * space (-4094 to -2).  They're never stored in the slots array; only
39ad3d6c72SMatthew Wilcox  * returned by the normal API.
403159f943SMatthew Wilcox  */
413159f943SMatthew Wilcox 
423159f943SMatthew Wilcox #define BITS_PER_XA_VALUE	(BITS_PER_LONG - 1)
433159f943SMatthew Wilcox 
443159f943SMatthew Wilcox /**
453159f943SMatthew Wilcox  * xa_mk_value() - Create an XArray entry from an integer.
463159f943SMatthew Wilcox  * @v: Value to store in XArray.
473159f943SMatthew Wilcox  *
483159f943SMatthew Wilcox  * Context: Any context.
493159f943SMatthew Wilcox  * Return: An entry suitable for storing in the XArray.
503159f943SMatthew Wilcox  */
513159f943SMatthew Wilcox static inline void *xa_mk_value(unsigned long v)
523159f943SMatthew Wilcox {
533159f943SMatthew Wilcox 	WARN_ON((long)v < 0);
543159f943SMatthew Wilcox 	return (void *)((v << 1) | 1);
553159f943SMatthew Wilcox }
563159f943SMatthew Wilcox 
573159f943SMatthew Wilcox /**
583159f943SMatthew Wilcox  * xa_to_value() - Get value stored in an XArray entry.
593159f943SMatthew Wilcox  * @entry: XArray entry.
603159f943SMatthew Wilcox  *
613159f943SMatthew Wilcox  * Context: Any context.
623159f943SMatthew Wilcox  * Return: The value stored in the XArray entry.
633159f943SMatthew Wilcox  */
643159f943SMatthew Wilcox static inline unsigned long xa_to_value(const void *entry)
653159f943SMatthew Wilcox {
663159f943SMatthew Wilcox 	return (unsigned long)entry >> 1;
673159f943SMatthew Wilcox }
683159f943SMatthew Wilcox 
693159f943SMatthew Wilcox /**
703159f943SMatthew Wilcox  * xa_is_value() - Determine if an entry is a value.
713159f943SMatthew Wilcox  * @entry: XArray entry.
723159f943SMatthew Wilcox  *
733159f943SMatthew Wilcox  * Context: Any context.
743159f943SMatthew Wilcox  * Return: True if the entry is a value, false if it is a pointer.
753159f943SMatthew Wilcox  */
763159f943SMatthew Wilcox static inline bool xa_is_value(const void *entry)
773159f943SMatthew Wilcox {
783159f943SMatthew Wilcox 	return (unsigned long)entry & 1;
793159f943SMatthew Wilcox }
803159f943SMatthew Wilcox 
813159f943SMatthew Wilcox /**
823159f943SMatthew Wilcox  * xa_tag_pointer() - Create an XArray entry for a tagged pointer.
833159f943SMatthew Wilcox  * @p: Plain pointer.
843159f943SMatthew Wilcox  * @tag: Tag value (0, 1 or 3).
853159f943SMatthew Wilcox  *
863159f943SMatthew Wilcox  * If the user of the XArray prefers, they can tag their pointers instead
873159f943SMatthew Wilcox  * of storing value entries.  Three tags are available (0, 1 and 3).
883159f943SMatthew Wilcox  * These are distinct from the xa_mark_t as they are not replicated up
893159f943SMatthew Wilcox  * through the array and cannot be searched for.
903159f943SMatthew Wilcox  *
913159f943SMatthew Wilcox  * Context: Any context.
923159f943SMatthew Wilcox  * Return: An XArray entry.
933159f943SMatthew Wilcox  */
943159f943SMatthew Wilcox static inline void *xa_tag_pointer(void *p, unsigned long tag)
953159f943SMatthew Wilcox {
963159f943SMatthew Wilcox 	return (void *)((unsigned long)p | tag);
973159f943SMatthew Wilcox }
983159f943SMatthew Wilcox 
993159f943SMatthew Wilcox /**
1003159f943SMatthew Wilcox  * xa_untag_pointer() - Turn an XArray entry into a plain pointer.
1013159f943SMatthew Wilcox  * @entry: XArray entry.
1023159f943SMatthew Wilcox  *
1033159f943SMatthew Wilcox  * If you have stored a tagged pointer in the XArray, call this function
1043159f943SMatthew Wilcox  * to get the untagged version of the pointer.
1053159f943SMatthew Wilcox  *
1063159f943SMatthew Wilcox  * Context: Any context.
1073159f943SMatthew Wilcox  * Return: A pointer.
1083159f943SMatthew Wilcox  */
1093159f943SMatthew Wilcox static inline void *xa_untag_pointer(void *entry)
1103159f943SMatthew Wilcox {
1113159f943SMatthew Wilcox 	return (void *)((unsigned long)entry & ~3UL);
1123159f943SMatthew Wilcox }
1133159f943SMatthew Wilcox 
1143159f943SMatthew Wilcox /**
1153159f943SMatthew Wilcox  * xa_pointer_tag() - Get the tag stored in an XArray entry.
1163159f943SMatthew Wilcox  * @entry: XArray entry.
1173159f943SMatthew Wilcox  *
1183159f943SMatthew Wilcox  * If you have stored a tagged pointer in the XArray, call this function
1193159f943SMatthew Wilcox  * to get the tag of that pointer.
1203159f943SMatthew Wilcox  *
1213159f943SMatthew Wilcox  * Context: Any context.
1223159f943SMatthew Wilcox  * Return: A tag.
1233159f943SMatthew Wilcox  */
1243159f943SMatthew Wilcox static inline unsigned int xa_pointer_tag(void *entry)
1253159f943SMatthew Wilcox {
1263159f943SMatthew Wilcox 	return (unsigned long)entry & 3UL;
1273159f943SMatthew Wilcox }
128f6bb2a2cSMatthew Wilcox 
12902c02bf1SMatthew Wilcox /*
13002c02bf1SMatthew Wilcox  * xa_mk_internal() - Create an internal entry.
13102c02bf1SMatthew Wilcox  * @v: Value to turn into an internal entry.
13202c02bf1SMatthew Wilcox  *
13302c02bf1SMatthew Wilcox  * Context: Any context.
13402c02bf1SMatthew Wilcox  * Return: An XArray internal entry corresponding to this value.
13502c02bf1SMatthew Wilcox  */
13602c02bf1SMatthew Wilcox static inline void *xa_mk_internal(unsigned long v)
13702c02bf1SMatthew Wilcox {
13802c02bf1SMatthew Wilcox 	return (void *)((v << 2) | 2);
13902c02bf1SMatthew Wilcox }
14002c02bf1SMatthew Wilcox 
14102c02bf1SMatthew Wilcox /*
14202c02bf1SMatthew Wilcox  * xa_to_internal() - Extract the value from an internal entry.
14302c02bf1SMatthew Wilcox  * @entry: XArray entry.
14402c02bf1SMatthew Wilcox  *
14502c02bf1SMatthew Wilcox  * Context: Any context.
14602c02bf1SMatthew Wilcox  * Return: The value which was stored in the internal entry.
14702c02bf1SMatthew Wilcox  */
14802c02bf1SMatthew Wilcox static inline unsigned long xa_to_internal(const void *entry)
14902c02bf1SMatthew Wilcox {
15002c02bf1SMatthew Wilcox 	return (unsigned long)entry >> 2;
15102c02bf1SMatthew Wilcox }
15202c02bf1SMatthew Wilcox 
15302c02bf1SMatthew Wilcox /*
15402c02bf1SMatthew Wilcox  * xa_is_internal() - Is the entry an internal entry?
15502c02bf1SMatthew Wilcox  * @entry: XArray entry.
15602c02bf1SMatthew Wilcox  *
15702c02bf1SMatthew Wilcox  * Context: Any context.
15802c02bf1SMatthew Wilcox  * Return: %true if the entry is an internal entry.
15902c02bf1SMatthew Wilcox  */
16002c02bf1SMatthew Wilcox static inline bool xa_is_internal(const void *entry)
16102c02bf1SMatthew Wilcox {
16202c02bf1SMatthew Wilcox 	return ((unsigned long)entry & 3) == 2;
16302c02bf1SMatthew Wilcox }
16402c02bf1SMatthew Wilcox 
165f8d5d0ccSMatthew Wilcox /**
166ad3d6c72SMatthew Wilcox  * xa_is_err() - Report whether an XArray operation returned an error
167ad3d6c72SMatthew Wilcox  * @entry: Result from calling an XArray function
168ad3d6c72SMatthew Wilcox  *
169ad3d6c72SMatthew Wilcox  * If an XArray operation cannot complete an operation, it will return
170ad3d6c72SMatthew Wilcox  * a special value indicating an error.  This function tells you
171ad3d6c72SMatthew Wilcox  * whether an error occurred; xa_err() tells you which error occurred.
172ad3d6c72SMatthew Wilcox  *
173ad3d6c72SMatthew Wilcox  * Context: Any context.
174ad3d6c72SMatthew Wilcox  * Return: %true if the entry indicates an error.
175ad3d6c72SMatthew Wilcox  */
176ad3d6c72SMatthew Wilcox static inline bool xa_is_err(const void *entry)
177ad3d6c72SMatthew Wilcox {
178ad3d6c72SMatthew Wilcox 	return unlikely(xa_is_internal(entry));
179ad3d6c72SMatthew Wilcox }
180ad3d6c72SMatthew Wilcox 
181ad3d6c72SMatthew Wilcox /**
182ad3d6c72SMatthew Wilcox  * xa_err() - Turn an XArray result into an errno.
183ad3d6c72SMatthew Wilcox  * @entry: Result from calling an XArray function.
184ad3d6c72SMatthew Wilcox  *
185ad3d6c72SMatthew Wilcox  * If an XArray operation cannot complete an operation, it will return
186ad3d6c72SMatthew Wilcox  * a special pointer value which encodes an errno.  This function extracts
187ad3d6c72SMatthew Wilcox  * the errno from the pointer value, or returns 0 if the pointer does not
188ad3d6c72SMatthew Wilcox  * represent an errno.
189ad3d6c72SMatthew Wilcox  *
190ad3d6c72SMatthew Wilcox  * Context: Any context.
191ad3d6c72SMatthew Wilcox  * Return: A negative errno or 0.
192ad3d6c72SMatthew Wilcox  */
193ad3d6c72SMatthew Wilcox static inline int xa_err(void *entry)
194ad3d6c72SMatthew Wilcox {
195ad3d6c72SMatthew Wilcox 	/* xa_to_internal() would not do sign extension. */
196ad3d6c72SMatthew Wilcox 	if (xa_is_err(entry))
197ad3d6c72SMatthew Wilcox 		return (long)entry >> 2;
198ad3d6c72SMatthew Wilcox 	return 0;
199ad3d6c72SMatthew Wilcox }
200ad3d6c72SMatthew Wilcox 
2019b89a035SMatthew Wilcox typedef unsigned __bitwise xa_mark_t;
2029b89a035SMatthew Wilcox #define XA_MARK_0		((__force xa_mark_t)0U)
2039b89a035SMatthew Wilcox #define XA_MARK_1		((__force xa_mark_t)1U)
2049b89a035SMatthew Wilcox #define XA_MARK_2		((__force xa_mark_t)2U)
2059b89a035SMatthew Wilcox #define XA_PRESENT		((__force xa_mark_t)8U)
2069b89a035SMatthew Wilcox #define XA_MARK_MAX		XA_MARK_2
2079b89a035SMatthew Wilcox 
20858d6ea30SMatthew Wilcox enum xa_lock_type {
20958d6ea30SMatthew Wilcox 	XA_LOCK_IRQ = 1,
21058d6ea30SMatthew Wilcox 	XA_LOCK_BH = 2,
21158d6ea30SMatthew Wilcox };
21258d6ea30SMatthew Wilcox 
2139b89a035SMatthew Wilcox /*
2149b89a035SMatthew Wilcox  * Values for xa_flags.  The radix tree stores its GFP flags in the xa_flags,
2159b89a035SMatthew Wilcox  * and we remain compatible with that.
2169b89a035SMatthew Wilcox  */
21758d6ea30SMatthew Wilcox #define XA_FLAGS_LOCK_IRQ	((__force gfp_t)XA_LOCK_IRQ)
21858d6ea30SMatthew Wilcox #define XA_FLAGS_LOCK_BH	((__force gfp_t)XA_LOCK_BH)
2199b89a035SMatthew Wilcox #define XA_FLAGS_MARK(mark)	((__force gfp_t)((1U << __GFP_BITS_SHIFT) << \
2209b89a035SMatthew Wilcox 						(__force unsigned)(mark)))
2219b89a035SMatthew Wilcox 
222ad3d6c72SMatthew Wilcox /**
223f8d5d0ccSMatthew Wilcox  * struct xarray - The anchor of the XArray.
224f8d5d0ccSMatthew Wilcox  * @xa_lock: Lock that protects the contents of the XArray.
225f8d5d0ccSMatthew Wilcox  *
226f8d5d0ccSMatthew Wilcox  * To use the xarray, define it statically or embed it in your data structure.
227f8d5d0ccSMatthew Wilcox  * It is a very small data structure, so it does not usually make sense to
228f8d5d0ccSMatthew Wilcox  * allocate it separately and keep a pointer to it in your data structure.
229f8d5d0ccSMatthew Wilcox  *
230f8d5d0ccSMatthew Wilcox  * You may use the xa_lock to protect your own data structures as well.
231f8d5d0ccSMatthew Wilcox  */
232f8d5d0ccSMatthew Wilcox /*
233f8d5d0ccSMatthew Wilcox  * If all of the entries in the array are NULL, @xa_head is a NULL pointer.
234f8d5d0ccSMatthew Wilcox  * If the only non-NULL entry in the array is at index 0, @xa_head is that
235f8d5d0ccSMatthew Wilcox  * entry.  If any other entry in the array is non-NULL, @xa_head points
236f8d5d0ccSMatthew Wilcox  * to an @xa_node.
237f8d5d0ccSMatthew Wilcox  */
238f8d5d0ccSMatthew Wilcox struct xarray {
239f8d5d0ccSMatthew Wilcox 	spinlock_t	xa_lock;
240f8d5d0ccSMatthew Wilcox /* private: The rest of the data structure is not to be used directly. */
241f8d5d0ccSMatthew Wilcox 	gfp_t		xa_flags;
242f8d5d0ccSMatthew Wilcox 	void __rcu *	xa_head;
243f8d5d0ccSMatthew Wilcox };
244f8d5d0ccSMatthew Wilcox 
245f8d5d0ccSMatthew Wilcox #define XARRAY_INIT(name, flags) {				\
246f8d5d0ccSMatthew Wilcox 	.xa_lock = __SPIN_LOCK_UNLOCKED(name.xa_lock),		\
247f8d5d0ccSMatthew Wilcox 	.xa_flags = flags,					\
248f8d5d0ccSMatthew Wilcox 	.xa_head = NULL,					\
249f8d5d0ccSMatthew Wilcox }
250f8d5d0ccSMatthew Wilcox 
251f8d5d0ccSMatthew Wilcox /**
252f8d5d0ccSMatthew Wilcox  * DEFINE_XARRAY_FLAGS() - Define an XArray with custom flags.
253f8d5d0ccSMatthew Wilcox  * @name: A string that names your XArray.
254f8d5d0ccSMatthew Wilcox  * @flags: XA_FLAG values.
255f8d5d0ccSMatthew Wilcox  *
256f8d5d0ccSMatthew Wilcox  * This is intended for file scope definitions of XArrays.  It declares
257f8d5d0ccSMatthew Wilcox  * and initialises an empty XArray with the chosen name and flags.  It is
258f8d5d0ccSMatthew Wilcox  * equivalent to calling xa_init_flags() on the array, but it does the
259f8d5d0ccSMatthew Wilcox  * initialisation at compiletime instead of runtime.
260f8d5d0ccSMatthew Wilcox  */
261f8d5d0ccSMatthew Wilcox #define DEFINE_XARRAY_FLAGS(name, flags)				\
262f8d5d0ccSMatthew Wilcox 	struct xarray name = XARRAY_INIT(name, flags)
263f8d5d0ccSMatthew Wilcox 
264f8d5d0ccSMatthew Wilcox /**
265f8d5d0ccSMatthew Wilcox  * DEFINE_XARRAY() - Define an XArray.
266f8d5d0ccSMatthew Wilcox  * @name: A string that names your XArray.
267f8d5d0ccSMatthew Wilcox  *
268f8d5d0ccSMatthew Wilcox  * This is intended for file scope definitions of XArrays.  It declares
269f8d5d0ccSMatthew Wilcox  * and initialises an empty XArray with the chosen name.  It is equivalent
270f8d5d0ccSMatthew Wilcox  * to calling xa_init() on the array, but it does the initialisation at
271f8d5d0ccSMatthew Wilcox  * compiletime instead of runtime.
272f8d5d0ccSMatthew Wilcox  */
273f8d5d0ccSMatthew Wilcox #define DEFINE_XARRAY(name) DEFINE_XARRAY_FLAGS(name, 0)
274f8d5d0ccSMatthew Wilcox 
275f8d5d0ccSMatthew Wilcox void xa_init_flags(struct xarray *, gfp_t flags);
276ad3d6c72SMatthew Wilcox void *xa_load(struct xarray *, unsigned long index);
27758d6ea30SMatthew Wilcox void *xa_store(struct xarray *, unsigned long index, void *entry, gfp_t);
27841aec91fSMatthew Wilcox void *xa_cmpxchg(struct xarray *, unsigned long index,
27941aec91fSMatthew Wilcox 			void *old, void *entry, gfp_t);
2809b89a035SMatthew Wilcox bool xa_get_mark(struct xarray *, unsigned long index, xa_mark_t);
2819b89a035SMatthew Wilcox void xa_set_mark(struct xarray *, unsigned long index, xa_mark_t);
2829b89a035SMatthew Wilcox void xa_clear_mark(struct xarray *, unsigned long index, xa_mark_t);
283*b803b428SMatthew Wilcox void *xa_find(struct xarray *xa, unsigned long *index,
284*b803b428SMatthew Wilcox 		unsigned long max, xa_mark_t) __attribute__((nonnull(2)));
285*b803b428SMatthew Wilcox void *xa_find_after(struct xarray *xa, unsigned long *index,
286*b803b428SMatthew Wilcox 		unsigned long max, xa_mark_t) __attribute__((nonnull(2)));
287f8d5d0ccSMatthew Wilcox 
288f8d5d0ccSMatthew Wilcox /**
289f8d5d0ccSMatthew Wilcox  * xa_init() - Initialise an empty XArray.
290f8d5d0ccSMatthew Wilcox  * @xa: XArray.
291f8d5d0ccSMatthew Wilcox  *
292f8d5d0ccSMatthew Wilcox  * An empty XArray is full of NULL entries.
293f8d5d0ccSMatthew Wilcox  *
294f8d5d0ccSMatthew Wilcox  * Context: Any context.
295f8d5d0ccSMatthew Wilcox  */
296f8d5d0ccSMatthew Wilcox static inline void xa_init(struct xarray *xa)
297f8d5d0ccSMatthew Wilcox {
298f8d5d0ccSMatthew Wilcox 	xa_init_flags(xa, 0);
299f8d5d0ccSMatthew Wilcox }
300f8d5d0ccSMatthew Wilcox 
301ad3d6c72SMatthew Wilcox /**
302ad3d6c72SMatthew Wilcox  * xa_empty() - Determine if an array has any present entries.
303ad3d6c72SMatthew Wilcox  * @xa: XArray.
304ad3d6c72SMatthew Wilcox  *
305ad3d6c72SMatthew Wilcox  * Context: Any context.
306ad3d6c72SMatthew Wilcox  * Return: %true if the array contains only NULL pointers.
307ad3d6c72SMatthew Wilcox  */
308ad3d6c72SMatthew Wilcox static inline bool xa_empty(const struct xarray *xa)
309ad3d6c72SMatthew Wilcox {
310ad3d6c72SMatthew Wilcox 	return xa->xa_head == NULL;
311ad3d6c72SMatthew Wilcox }
312ad3d6c72SMatthew Wilcox 
3139b89a035SMatthew Wilcox /**
3149b89a035SMatthew Wilcox  * xa_marked() - Inquire whether any entry in this array has a mark set
3159b89a035SMatthew Wilcox  * @xa: Array
3169b89a035SMatthew Wilcox  * @mark: Mark value
3179b89a035SMatthew Wilcox  *
3189b89a035SMatthew Wilcox  * Context: Any context.
3199b89a035SMatthew Wilcox  * Return: %true if any entry has this mark set.
3209b89a035SMatthew Wilcox  */
3219b89a035SMatthew Wilcox static inline bool xa_marked(const struct xarray *xa, xa_mark_t mark)
3229b89a035SMatthew Wilcox {
3239b89a035SMatthew Wilcox 	return xa->xa_flags & XA_FLAGS_MARK(mark);
3249b89a035SMatthew Wilcox }
3259b89a035SMatthew Wilcox 
32658d6ea30SMatthew Wilcox /**
32758d6ea30SMatthew Wilcox  * xa_erase() - Erase this entry from the XArray.
32858d6ea30SMatthew Wilcox  * @xa: XArray.
32958d6ea30SMatthew Wilcox  * @index: Index of entry.
33058d6ea30SMatthew Wilcox  *
33158d6ea30SMatthew Wilcox  * This function is the equivalent of calling xa_store() with %NULL as
33258d6ea30SMatthew Wilcox  * the third argument.  The XArray does not need to allocate memory, so
33358d6ea30SMatthew Wilcox  * the user does not need to provide GFP flags.
33458d6ea30SMatthew Wilcox  *
33558d6ea30SMatthew Wilcox  * Context: Process context.  Takes and releases the xa_lock.
33658d6ea30SMatthew Wilcox  * Return: The entry which used to be at this index.
33758d6ea30SMatthew Wilcox  */
33858d6ea30SMatthew Wilcox static inline void *xa_erase(struct xarray *xa, unsigned long index)
33958d6ea30SMatthew Wilcox {
34058d6ea30SMatthew Wilcox 	return xa_store(xa, index, NULL, 0);
34158d6ea30SMatthew Wilcox }
34258d6ea30SMatthew Wilcox 
34341aec91fSMatthew Wilcox /**
34441aec91fSMatthew Wilcox  * xa_insert() - Store this entry in the XArray unless another entry is
34541aec91fSMatthew Wilcox  *			already present.
34641aec91fSMatthew Wilcox  * @xa: XArray.
34741aec91fSMatthew Wilcox  * @index: Index into array.
34841aec91fSMatthew Wilcox  * @entry: New entry.
34941aec91fSMatthew Wilcox  * @gfp: Memory allocation flags.
35041aec91fSMatthew Wilcox  *
35141aec91fSMatthew Wilcox  * If you would rather see the existing entry in the array, use xa_cmpxchg().
35241aec91fSMatthew Wilcox  * This function is for users who don't care what the entry is, only that
35341aec91fSMatthew Wilcox  * one is present.
35441aec91fSMatthew Wilcox  *
35541aec91fSMatthew Wilcox  * Context: Process context.  Takes and releases the xa_lock.
35641aec91fSMatthew Wilcox  *	    May sleep if the @gfp flags permit.
35741aec91fSMatthew Wilcox  * Return: 0 if the store succeeded.  -EEXIST if another entry was present.
35841aec91fSMatthew Wilcox  * -ENOMEM if memory could not be allocated.
35941aec91fSMatthew Wilcox  */
36041aec91fSMatthew Wilcox static inline int xa_insert(struct xarray *xa, unsigned long index,
36141aec91fSMatthew Wilcox 		void *entry, gfp_t gfp)
36241aec91fSMatthew Wilcox {
36341aec91fSMatthew Wilcox 	void *curr = xa_cmpxchg(xa, index, NULL, entry, gfp);
36441aec91fSMatthew Wilcox 	if (!curr)
36541aec91fSMatthew Wilcox 		return 0;
36641aec91fSMatthew Wilcox 	if (xa_is_err(curr))
36741aec91fSMatthew Wilcox 		return xa_err(curr);
36841aec91fSMatthew Wilcox 	return -EEXIST;
36941aec91fSMatthew Wilcox }
37041aec91fSMatthew Wilcox 
371*b803b428SMatthew Wilcox /**
372*b803b428SMatthew Wilcox  * xa_for_each() - Iterate over a portion of an XArray.
373*b803b428SMatthew Wilcox  * @xa: XArray.
374*b803b428SMatthew Wilcox  * @entry: Entry retrieved from array.
375*b803b428SMatthew Wilcox  * @index: Index of @entry.
376*b803b428SMatthew Wilcox  * @max: Maximum index to retrieve from array.
377*b803b428SMatthew Wilcox  * @filter: Selection criterion.
378*b803b428SMatthew Wilcox  *
379*b803b428SMatthew Wilcox  * Initialise @index to the lowest index you want to retrieve from the
380*b803b428SMatthew Wilcox  * array.  During the iteration, @entry will have the value of the entry
381*b803b428SMatthew Wilcox  * stored in @xa at @index.  The iteration will skip all entries in the
382*b803b428SMatthew Wilcox  * array which do not match @filter.  You may modify @index during the
383*b803b428SMatthew Wilcox  * iteration if you want to skip or reprocess indices.  It is safe to modify
384*b803b428SMatthew Wilcox  * the array during the iteration.  At the end of the iteration, @entry will
385*b803b428SMatthew Wilcox  * be set to NULL and @index will have a value less than or equal to max.
386*b803b428SMatthew Wilcox  *
387*b803b428SMatthew Wilcox  * xa_for_each() is O(n.log(n)) while xas_for_each() is O(n).  You have
388*b803b428SMatthew Wilcox  * to handle your own locking with xas_for_each(), and if you have to unlock
389*b803b428SMatthew Wilcox  * after each iteration, it will also end up being O(n.log(n)).  xa_for_each()
390*b803b428SMatthew Wilcox  * will spin if it hits a retry entry; if you intend to see retry entries,
391*b803b428SMatthew Wilcox  * you should use the xas_for_each() iterator instead.  The xas_for_each()
392*b803b428SMatthew Wilcox  * iterator will expand into more inline code than xa_for_each().
393*b803b428SMatthew Wilcox  *
394*b803b428SMatthew Wilcox  * Context: Any context.  Takes and releases the RCU lock.
395*b803b428SMatthew Wilcox  */
396*b803b428SMatthew Wilcox #define xa_for_each(xa, entry, index, max, filter) \
397*b803b428SMatthew Wilcox 	for (entry = xa_find(xa, &index, max, filter); entry; \
398*b803b428SMatthew Wilcox 	     entry = xa_find_after(xa, &index, max, filter))
399*b803b428SMatthew Wilcox 
400f6bb2a2cSMatthew Wilcox #define xa_trylock(xa)		spin_trylock(&(xa)->xa_lock)
401f6bb2a2cSMatthew Wilcox #define xa_lock(xa)		spin_lock(&(xa)->xa_lock)
402f6bb2a2cSMatthew Wilcox #define xa_unlock(xa)		spin_unlock(&(xa)->xa_lock)
403f6bb2a2cSMatthew Wilcox #define xa_lock_bh(xa)		spin_lock_bh(&(xa)->xa_lock)
404f6bb2a2cSMatthew Wilcox #define xa_unlock_bh(xa)	spin_unlock_bh(&(xa)->xa_lock)
405f6bb2a2cSMatthew Wilcox #define xa_lock_irq(xa)		spin_lock_irq(&(xa)->xa_lock)
406f6bb2a2cSMatthew Wilcox #define xa_unlock_irq(xa)	spin_unlock_irq(&(xa)->xa_lock)
407f6bb2a2cSMatthew Wilcox #define xa_lock_irqsave(xa, flags) \
408f6bb2a2cSMatthew Wilcox 				spin_lock_irqsave(&(xa)->xa_lock, flags)
409f6bb2a2cSMatthew Wilcox #define xa_unlock_irqrestore(xa, flags) \
410f6bb2a2cSMatthew Wilcox 				spin_unlock_irqrestore(&(xa)->xa_lock, flags)
411f6bb2a2cSMatthew Wilcox 
4129b89a035SMatthew Wilcox /*
41358d6ea30SMatthew Wilcox  * Versions of the normal API which require the caller to hold the
41458d6ea30SMatthew Wilcox  * xa_lock.  If the GFP flags allow it, they will drop the lock to
41558d6ea30SMatthew Wilcox  * allocate memory, then reacquire it afterwards.  These functions
41658d6ea30SMatthew Wilcox  * may also re-enable interrupts if the XArray flags indicate the
41758d6ea30SMatthew Wilcox  * locking should be interrupt safe.
4189b89a035SMatthew Wilcox  */
41958d6ea30SMatthew Wilcox void *__xa_erase(struct xarray *, unsigned long index);
42058d6ea30SMatthew Wilcox void *__xa_store(struct xarray *, unsigned long index, void *entry, gfp_t);
42141aec91fSMatthew Wilcox void *__xa_cmpxchg(struct xarray *, unsigned long index, void *old,
42241aec91fSMatthew Wilcox 		void *entry, gfp_t);
4239b89a035SMatthew Wilcox void __xa_set_mark(struct xarray *, unsigned long index, xa_mark_t);
4249b89a035SMatthew Wilcox void __xa_clear_mark(struct xarray *, unsigned long index, xa_mark_t);
4259b89a035SMatthew Wilcox 
42658d6ea30SMatthew Wilcox /**
42741aec91fSMatthew Wilcox  * __xa_insert() - Store this entry in the XArray unless another entry is
42841aec91fSMatthew Wilcox  *			already present.
42941aec91fSMatthew Wilcox  * @xa: XArray.
43041aec91fSMatthew Wilcox  * @index: Index into array.
43141aec91fSMatthew Wilcox  * @entry: New entry.
43241aec91fSMatthew Wilcox  * @gfp: Memory allocation flags.
43341aec91fSMatthew Wilcox  *
43441aec91fSMatthew Wilcox  * If you would rather see the existing entry in the array, use __xa_cmpxchg().
43541aec91fSMatthew Wilcox  * This function is for users who don't care what the entry is, only that
43641aec91fSMatthew Wilcox  * one is present.
43741aec91fSMatthew Wilcox  *
43841aec91fSMatthew Wilcox  * Context: Any context.  Expects xa_lock to be held on entry.  May
43941aec91fSMatthew Wilcox  *	    release and reacquire xa_lock if the @gfp flags permit.
44041aec91fSMatthew Wilcox  * Return: 0 if the store succeeded.  -EEXIST if another entry was present.
44141aec91fSMatthew Wilcox  * -ENOMEM if memory could not be allocated.
44241aec91fSMatthew Wilcox  */
44341aec91fSMatthew Wilcox static inline int __xa_insert(struct xarray *xa, unsigned long index,
44441aec91fSMatthew Wilcox 		void *entry, gfp_t gfp)
44541aec91fSMatthew Wilcox {
44641aec91fSMatthew Wilcox 	void *curr = __xa_cmpxchg(xa, index, NULL, entry, gfp);
44741aec91fSMatthew Wilcox 	if (!curr)
44841aec91fSMatthew Wilcox 		return 0;
44941aec91fSMatthew Wilcox 	if (xa_is_err(curr))
45041aec91fSMatthew Wilcox 		return xa_err(curr);
45141aec91fSMatthew Wilcox 	return -EEXIST;
45241aec91fSMatthew Wilcox }
45341aec91fSMatthew Wilcox 
45441aec91fSMatthew Wilcox /**
45558d6ea30SMatthew Wilcox  * xa_erase_bh() - Erase this entry from the XArray.
45658d6ea30SMatthew Wilcox  * @xa: XArray.
45758d6ea30SMatthew Wilcox  * @index: Index of entry.
45858d6ea30SMatthew Wilcox  *
45958d6ea30SMatthew Wilcox  * This function is the equivalent of calling xa_store() with %NULL as
46058d6ea30SMatthew Wilcox  * the third argument.  The XArray does not need to allocate memory, so
46158d6ea30SMatthew Wilcox  * the user does not need to provide GFP flags.
46258d6ea30SMatthew Wilcox  *
46358d6ea30SMatthew Wilcox  * Context: Process context.  Takes and releases the xa_lock while
46458d6ea30SMatthew Wilcox  * disabling softirqs.
46558d6ea30SMatthew Wilcox  * Return: The entry which used to be at this index.
46658d6ea30SMatthew Wilcox  */
46758d6ea30SMatthew Wilcox static inline void *xa_erase_bh(struct xarray *xa, unsigned long index)
46858d6ea30SMatthew Wilcox {
46958d6ea30SMatthew Wilcox 	void *entry;
47058d6ea30SMatthew Wilcox 
47158d6ea30SMatthew Wilcox 	xa_lock_bh(xa);
47258d6ea30SMatthew Wilcox 	entry = __xa_erase(xa, index);
47358d6ea30SMatthew Wilcox 	xa_unlock_bh(xa);
47458d6ea30SMatthew Wilcox 
47558d6ea30SMatthew Wilcox 	return entry;
47658d6ea30SMatthew Wilcox }
47758d6ea30SMatthew Wilcox 
47858d6ea30SMatthew Wilcox /**
47958d6ea30SMatthew Wilcox  * xa_erase_irq() - Erase this entry from the XArray.
48058d6ea30SMatthew Wilcox  * @xa: XArray.
48158d6ea30SMatthew Wilcox  * @index: Index of entry.
48258d6ea30SMatthew Wilcox  *
48358d6ea30SMatthew Wilcox  * This function is the equivalent of calling xa_store() with %NULL as
48458d6ea30SMatthew Wilcox  * the third argument.  The XArray does not need to allocate memory, so
48558d6ea30SMatthew Wilcox  * the user does not need to provide GFP flags.
48658d6ea30SMatthew Wilcox  *
48758d6ea30SMatthew Wilcox  * Context: Process context.  Takes and releases the xa_lock while
48858d6ea30SMatthew Wilcox  * disabling interrupts.
48958d6ea30SMatthew Wilcox  * Return: The entry which used to be at this index.
49058d6ea30SMatthew Wilcox  */
49158d6ea30SMatthew Wilcox static inline void *xa_erase_irq(struct xarray *xa, unsigned long index)
49258d6ea30SMatthew Wilcox {
49358d6ea30SMatthew Wilcox 	void *entry;
49458d6ea30SMatthew Wilcox 
49558d6ea30SMatthew Wilcox 	xa_lock_irq(xa);
49658d6ea30SMatthew Wilcox 	entry = __xa_erase(xa, index);
49758d6ea30SMatthew Wilcox 	xa_unlock_irq(xa);
49858d6ea30SMatthew Wilcox 
49958d6ea30SMatthew Wilcox 	return entry;
50058d6ea30SMatthew Wilcox }
50158d6ea30SMatthew Wilcox 
50202c02bf1SMatthew Wilcox /* Everything below here is the Advanced API.  Proceed with caution. */
50302c02bf1SMatthew Wilcox 
50402c02bf1SMatthew Wilcox /*
50502c02bf1SMatthew Wilcox  * The xarray is constructed out of a set of 'chunks' of pointers.  Choosing
50602c02bf1SMatthew Wilcox  * the best chunk size requires some tradeoffs.  A power of two recommends
50702c02bf1SMatthew Wilcox  * itself so that we can walk the tree based purely on shifts and masks.
50802c02bf1SMatthew Wilcox  * Generally, the larger the better; as the number of slots per level of the
50902c02bf1SMatthew Wilcox  * tree increases, the less tall the tree needs to be.  But that needs to be
51002c02bf1SMatthew Wilcox  * balanced against the memory consumption of each node.  On a 64-bit system,
51102c02bf1SMatthew Wilcox  * xa_node is currently 576 bytes, and we get 7 of them per 4kB page.  If we
51202c02bf1SMatthew Wilcox  * doubled the number of slots per node, we'd get only 3 nodes per 4kB page.
51302c02bf1SMatthew Wilcox  */
51402c02bf1SMatthew Wilcox #ifndef XA_CHUNK_SHIFT
51502c02bf1SMatthew Wilcox #define XA_CHUNK_SHIFT		(CONFIG_BASE_SMALL ? 4 : 6)
51602c02bf1SMatthew Wilcox #endif
51702c02bf1SMatthew Wilcox #define XA_CHUNK_SIZE		(1UL << XA_CHUNK_SHIFT)
51802c02bf1SMatthew Wilcox #define XA_CHUNK_MASK		(XA_CHUNK_SIZE - 1)
51901959dfeSMatthew Wilcox #define XA_MAX_MARKS		3
52001959dfeSMatthew Wilcox #define XA_MARK_LONGS		DIV_ROUND_UP(XA_CHUNK_SIZE, BITS_PER_LONG)
52101959dfeSMatthew Wilcox 
52201959dfeSMatthew Wilcox /*
52301959dfeSMatthew Wilcox  * @count is the count of every non-NULL element in the ->slots array
52401959dfeSMatthew Wilcox  * whether that is a value entry, a retry entry, a user pointer,
52501959dfeSMatthew Wilcox  * a sibling entry or a pointer to the next level of the tree.
52601959dfeSMatthew Wilcox  * @nr_values is the count of every element in ->slots which is
52701959dfeSMatthew Wilcox  * either a value entry or a sibling of a value entry.
52801959dfeSMatthew Wilcox  */
52901959dfeSMatthew Wilcox struct xa_node {
53001959dfeSMatthew Wilcox 	unsigned char	shift;		/* Bits remaining in each slot */
53101959dfeSMatthew Wilcox 	unsigned char	offset;		/* Slot offset in parent */
53201959dfeSMatthew Wilcox 	unsigned char	count;		/* Total entry count */
53301959dfeSMatthew Wilcox 	unsigned char	nr_values;	/* Value entry count */
53401959dfeSMatthew Wilcox 	struct xa_node __rcu *parent;	/* NULL at top of tree */
53501959dfeSMatthew Wilcox 	struct xarray	*array;		/* The array we belong to */
53601959dfeSMatthew Wilcox 	union {
53701959dfeSMatthew Wilcox 		struct list_head private_list;	/* For tree user */
53801959dfeSMatthew Wilcox 		struct rcu_head	rcu_head;	/* Used when freeing node */
53901959dfeSMatthew Wilcox 	};
54001959dfeSMatthew Wilcox 	void __rcu	*slots[XA_CHUNK_SIZE];
54101959dfeSMatthew Wilcox 	union {
54201959dfeSMatthew Wilcox 		unsigned long	tags[XA_MAX_MARKS][XA_MARK_LONGS];
54301959dfeSMatthew Wilcox 		unsigned long	marks[XA_MAX_MARKS][XA_MARK_LONGS];
54401959dfeSMatthew Wilcox 	};
54501959dfeSMatthew Wilcox };
54602c02bf1SMatthew Wilcox 
547ad3d6c72SMatthew Wilcox void xa_dump(const struct xarray *);
548ad3d6c72SMatthew Wilcox void xa_dump_node(const struct xa_node *);
549ad3d6c72SMatthew Wilcox 
550ad3d6c72SMatthew Wilcox #ifdef XA_DEBUG
551ad3d6c72SMatthew Wilcox #define XA_BUG_ON(xa, x) do {					\
552ad3d6c72SMatthew Wilcox 		if (x) {					\
553ad3d6c72SMatthew Wilcox 			xa_dump(xa);				\
554ad3d6c72SMatthew Wilcox 			BUG();					\
555ad3d6c72SMatthew Wilcox 		}						\
556ad3d6c72SMatthew Wilcox 	} while (0)
557ad3d6c72SMatthew Wilcox #define XA_NODE_BUG_ON(node, x) do {				\
558ad3d6c72SMatthew Wilcox 		if (x) {					\
559ad3d6c72SMatthew Wilcox 			if (node) xa_dump_node(node);		\
560ad3d6c72SMatthew Wilcox 			BUG();					\
561ad3d6c72SMatthew Wilcox 		}						\
562ad3d6c72SMatthew Wilcox 	} while (0)
563ad3d6c72SMatthew Wilcox #else
564ad3d6c72SMatthew Wilcox #define XA_BUG_ON(xa, x)	do { } while (0)
565ad3d6c72SMatthew Wilcox #define XA_NODE_BUG_ON(node, x)	do { } while (0)
566ad3d6c72SMatthew Wilcox #endif
567ad3d6c72SMatthew Wilcox 
568ad3d6c72SMatthew Wilcox /* Private */
569ad3d6c72SMatthew Wilcox static inline void *xa_head(const struct xarray *xa)
570ad3d6c72SMatthew Wilcox {
571ad3d6c72SMatthew Wilcox 	return rcu_dereference_check(xa->xa_head,
572ad3d6c72SMatthew Wilcox 						lockdep_is_held(&xa->xa_lock));
573ad3d6c72SMatthew Wilcox }
574ad3d6c72SMatthew Wilcox 
575ad3d6c72SMatthew Wilcox /* Private */
576ad3d6c72SMatthew Wilcox static inline void *xa_head_locked(const struct xarray *xa)
577ad3d6c72SMatthew Wilcox {
578ad3d6c72SMatthew Wilcox 	return rcu_dereference_protected(xa->xa_head,
579ad3d6c72SMatthew Wilcox 						lockdep_is_held(&xa->xa_lock));
580ad3d6c72SMatthew Wilcox }
581ad3d6c72SMatthew Wilcox 
582ad3d6c72SMatthew Wilcox /* Private */
583ad3d6c72SMatthew Wilcox static inline void *xa_entry(const struct xarray *xa,
584ad3d6c72SMatthew Wilcox 				const struct xa_node *node, unsigned int offset)
585ad3d6c72SMatthew Wilcox {
586ad3d6c72SMatthew Wilcox 	XA_NODE_BUG_ON(node, offset >= XA_CHUNK_SIZE);
587ad3d6c72SMatthew Wilcox 	return rcu_dereference_check(node->slots[offset],
588ad3d6c72SMatthew Wilcox 						lockdep_is_held(&xa->xa_lock));
589ad3d6c72SMatthew Wilcox }
590ad3d6c72SMatthew Wilcox 
591ad3d6c72SMatthew Wilcox /* Private */
592ad3d6c72SMatthew Wilcox static inline void *xa_entry_locked(const struct xarray *xa,
593ad3d6c72SMatthew Wilcox 				const struct xa_node *node, unsigned int offset)
594ad3d6c72SMatthew Wilcox {
595ad3d6c72SMatthew Wilcox 	XA_NODE_BUG_ON(node, offset >= XA_CHUNK_SIZE);
596ad3d6c72SMatthew Wilcox 	return rcu_dereference_protected(node->slots[offset],
597ad3d6c72SMatthew Wilcox 						lockdep_is_held(&xa->xa_lock));
598ad3d6c72SMatthew Wilcox }
599ad3d6c72SMatthew Wilcox 
600ad3d6c72SMatthew Wilcox /* Private */
6019b89a035SMatthew Wilcox static inline struct xa_node *xa_parent(const struct xarray *xa,
6029b89a035SMatthew Wilcox 					const struct xa_node *node)
6039b89a035SMatthew Wilcox {
6049b89a035SMatthew Wilcox 	return rcu_dereference_check(node->parent,
6059b89a035SMatthew Wilcox 						lockdep_is_held(&xa->xa_lock));
6069b89a035SMatthew Wilcox }
6079b89a035SMatthew Wilcox 
6089b89a035SMatthew Wilcox /* Private */
6099b89a035SMatthew Wilcox static inline struct xa_node *xa_parent_locked(const struct xarray *xa,
6109b89a035SMatthew Wilcox 					const struct xa_node *node)
6119b89a035SMatthew Wilcox {
6129b89a035SMatthew Wilcox 	return rcu_dereference_protected(node->parent,
6139b89a035SMatthew Wilcox 						lockdep_is_held(&xa->xa_lock));
6149b89a035SMatthew Wilcox }
6159b89a035SMatthew Wilcox 
6169b89a035SMatthew Wilcox /* Private */
61758d6ea30SMatthew Wilcox static inline void *xa_mk_node(const struct xa_node *node)
61858d6ea30SMatthew Wilcox {
61958d6ea30SMatthew Wilcox 	return (void *)((unsigned long)node | 2);
62058d6ea30SMatthew Wilcox }
62158d6ea30SMatthew Wilcox 
62258d6ea30SMatthew Wilcox /* Private */
623ad3d6c72SMatthew Wilcox static inline struct xa_node *xa_to_node(const void *entry)
624ad3d6c72SMatthew Wilcox {
625ad3d6c72SMatthew Wilcox 	return (struct xa_node *)((unsigned long)entry - 2);
626ad3d6c72SMatthew Wilcox }
627ad3d6c72SMatthew Wilcox 
62802c02bf1SMatthew Wilcox /* Private */
62902c02bf1SMatthew Wilcox static inline bool xa_is_node(const void *entry)
63002c02bf1SMatthew Wilcox {
63102c02bf1SMatthew Wilcox 	return xa_is_internal(entry) && (unsigned long)entry > 4096;
63202c02bf1SMatthew Wilcox }
63302c02bf1SMatthew Wilcox 
63402c02bf1SMatthew Wilcox /* Private */
63502c02bf1SMatthew Wilcox static inline void *xa_mk_sibling(unsigned int offset)
63602c02bf1SMatthew Wilcox {
63702c02bf1SMatthew Wilcox 	return xa_mk_internal(offset);
63802c02bf1SMatthew Wilcox }
63902c02bf1SMatthew Wilcox 
64002c02bf1SMatthew Wilcox /* Private */
64102c02bf1SMatthew Wilcox static inline unsigned long xa_to_sibling(const void *entry)
64202c02bf1SMatthew Wilcox {
64302c02bf1SMatthew Wilcox 	return xa_to_internal(entry);
64402c02bf1SMatthew Wilcox }
64502c02bf1SMatthew Wilcox 
64602c02bf1SMatthew Wilcox /**
64702c02bf1SMatthew Wilcox  * xa_is_sibling() - Is the entry a sibling entry?
64802c02bf1SMatthew Wilcox  * @entry: Entry retrieved from the XArray
64902c02bf1SMatthew Wilcox  *
65002c02bf1SMatthew Wilcox  * Return: %true if the entry is a sibling entry.
65102c02bf1SMatthew Wilcox  */
65202c02bf1SMatthew Wilcox static inline bool xa_is_sibling(const void *entry)
65302c02bf1SMatthew Wilcox {
65402c02bf1SMatthew Wilcox 	return IS_ENABLED(CONFIG_XARRAY_MULTI) && xa_is_internal(entry) &&
65502c02bf1SMatthew Wilcox 		(entry < xa_mk_sibling(XA_CHUNK_SIZE - 1));
65602c02bf1SMatthew Wilcox }
65702c02bf1SMatthew Wilcox 
65802c02bf1SMatthew Wilcox #define XA_RETRY_ENTRY		xa_mk_internal(256)
65902c02bf1SMatthew Wilcox 
660ad3d6c72SMatthew Wilcox /**
661ad3d6c72SMatthew Wilcox  * xa_is_retry() - Is the entry a retry entry?
662ad3d6c72SMatthew Wilcox  * @entry: Entry retrieved from the XArray
663ad3d6c72SMatthew Wilcox  *
664ad3d6c72SMatthew Wilcox  * Return: %true if the entry is a retry entry.
665ad3d6c72SMatthew Wilcox  */
666ad3d6c72SMatthew Wilcox static inline bool xa_is_retry(const void *entry)
667ad3d6c72SMatthew Wilcox {
668ad3d6c72SMatthew Wilcox 	return unlikely(entry == XA_RETRY_ENTRY);
669ad3d6c72SMatthew Wilcox }
670ad3d6c72SMatthew Wilcox 
671ad3d6c72SMatthew Wilcox /**
672ad3d6c72SMatthew Wilcox  * typedef xa_update_node_t - A callback function from the XArray.
673ad3d6c72SMatthew Wilcox  * @node: The node which is being processed
674ad3d6c72SMatthew Wilcox  *
675ad3d6c72SMatthew Wilcox  * This function is called every time the XArray updates the count of
676ad3d6c72SMatthew Wilcox  * present and value entries in a node.  It allows advanced users to
677ad3d6c72SMatthew Wilcox  * maintain the private_list in the node.
678ad3d6c72SMatthew Wilcox  *
679ad3d6c72SMatthew Wilcox  * Context: The xa_lock is held and interrupts may be disabled.
680ad3d6c72SMatthew Wilcox  *	    Implementations should not drop the xa_lock, nor re-enable
681ad3d6c72SMatthew Wilcox  *	    interrupts.
682ad3d6c72SMatthew Wilcox  */
683ad3d6c72SMatthew Wilcox typedef void (*xa_update_node_t)(struct xa_node *node);
684ad3d6c72SMatthew Wilcox 
685ad3d6c72SMatthew Wilcox /*
686ad3d6c72SMatthew Wilcox  * The xa_state is opaque to its users.  It contains various different pieces
687ad3d6c72SMatthew Wilcox  * of state involved in the current operation on the XArray.  It should be
688ad3d6c72SMatthew Wilcox  * declared on the stack and passed between the various internal routines.
689ad3d6c72SMatthew Wilcox  * The various elements in it should not be accessed directly, but only
690ad3d6c72SMatthew Wilcox  * through the provided accessor functions.  The below documentation is for
691ad3d6c72SMatthew Wilcox  * the benefit of those working on the code, not for users of the XArray.
692ad3d6c72SMatthew Wilcox  *
693ad3d6c72SMatthew Wilcox  * @xa_node usually points to the xa_node containing the slot we're operating
694ad3d6c72SMatthew Wilcox  * on (and @xa_offset is the offset in the slots array).  If there is a
695ad3d6c72SMatthew Wilcox  * single entry in the array at index 0, there are no allocated xa_nodes to
696ad3d6c72SMatthew Wilcox  * point to, and so we store %NULL in @xa_node.  @xa_node is set to
697ad3d6c72SMatthew Wilcox  * the value %XAS_RESTART if the xa_state is not walked to the correct
698ad3d6c72SMatthew Wilcox  * position in the tree of nodes for this operation.  If an error occurs
699ad3d6c72SMatthew Wilcox  * during an operation, it is set to an %XAS_ERROR value.  If we run off the
700ad3d6c72SMatthew Wilcox  * end of the allocated nodes, it is set to %XAS_BOUNDS.
701ad3d6c72SMatthew Wilcox  */
702ad3d6c72SMatthew Wilcox struct xa_state {
703ad3d6c72SMatthew Wilcox 	struct xarray *xa;
704ad3d6c72SMatthew Wilcox 	unsigned long xa_index;
705ad3d6c72SMatthew Wilcox 	unsigned char xa_shift;
706ad3d6c72SMatthew Wilcox 	unsigned char xa_sibs;
707ad3d6c72SMatthew Wilcox 	unsigned char xa_offset;
708ad3d6c72SMatthew Wilcox 	unsigned char xa_pad;		/* Helps gcc generate better code */
709ad3d6c72SMatthew Wilcox 	struct xa_node *xa_node;
710ad3d6c72SMatthew Wilcox 	struct xa_node *xa_alloc;
711ad3d6c72SMatthew Wilcox 	xa_update_node_t xa_update;
712ad3d6c72SMatthew Wilcox };
713ad3d6c72SMatthew Wilcox 
714ad3d6c72SMatthew Wilcox /*
715ad3d6c72SMatthew Wilcox  * We encode errnos in the xas->xa_node.  If an error has happened, we need to
716ad3d6c72SMatthew Wilcox  * drop the lock to fix it, and once we've done so the xa_state is invalid.
717ad3d6c72SMatthew Wilcox  */
718ad3d6c72SMatthew Wilcox #define XA_ERROR(errno) ((struct xa_node *)(((unsigned long)errno << 2) | 2UL))
719ad3d6c72SMatthew Wilcox #define XAS_BOUNDS	((struct xa_node *)1UL)
720ad3d6c72SMatthew Wilcox #define XAS_RESTART	((struct xa_node *)3UL)
721ad3d6c72SMatthew Wilcox 
722ad3d6c72SMatthew Wilcox #define __XA_STATE(array, index, shift, sibs)  {	\
723ad3d6c72SMatthew Wilcox 	.xa = array,					\
724ad3d6c72SMatthew Wilcox 	.xa_index = index,				\
725ad3d6c72SMatthew Wilcox 	.xa_shift = shift,				\
726ad3d6c72SMatthew Wilcox 	.xa_sibs = sibs,				\
727ad3d6c72SMatthew Wilcox 	.xa_offset = 0,					\
728ad3d6c72SMatthew Wilcox 	.xa_pad = 0,					\
729ad3d6c72SMatthew Wilcox 	.xa_node = XAS_RESTART,				\
730ad3d6c72SMatthew Wilcox 	.xa_alloc = NULL,				\
731ad3d6c72SMatthew Wilcox 	.xa_update = NULL				\
732ad3d6c72SMatthew Wilcox }
733ad3d6c72SMatthew Wilcox 
734ad3d6c72SMatthew Wilcox /**
735ad3d6c72SMatthew Wilcox  * XA_STATE() - Declare an XArray operation state.
736ad3d6c72SMatthew Wilcox  * @name: Name of this operation state (usually xas).
737ad3d6c72SMatthew Wilcox  * @array: Array to operate on.
738ad3d6c72SMatthew Wilcox  * @index: Initial index of interest.
739ad3d6c72SMatthew Wilcox  *
740ad3d6c72SMatthew Wilcox  * Declare and initialise an xa_state on the stack.
741ad3d6c72SMatthew Wilcox  */
742ad3d6c72SMatthew Wilcox #define XA_STATE(name, array, index)				\
743ad3d6c72SMatthew Wilcox 	struct xa_state name = __XA_STATE(array, index, 0, 0)
744ad3d6c72SMatthew Wilcox 
745ad3d6c72SMatthew Wilcox /**
746ad3d6c72SMatthew Wilcox  * XA_STATE_ORDER() - Declare an XArray operation state.
747ad3d6c72SMatthew Wilcox  * @name: Name of this operation state (usually xas).
748ad3d6c72SMatthew Wilcox  * @array: Array to operate on.
749ad3d6c72SMatthew Wilcox  * @index: Initial index of interest.
750ad3d6c72SMatthew Wilcox  * @order: Order of entry.
751ad3d6c72SMatthew Wilcox  *
752ad3d6c72SMatthew Wilcox  * Declare and initialise an xa_state on the stack.  This variant of
753ad3d6c72SMatthew Wilcox  * XA_STATE() allows you to specify the 'order' of the element you
754ad3d6c72SMatthew Wilcox  * want to operate on.`
755ad3d6c72SMatthew Wilcox  */
756ad3d6c72SMatthew Wilcox #define XA_STATE_ORDER(name, array, index, order)		\
757ad3d6c72SMatthew Wilcox 	struct xa_state name = __XA_STATE(array,		\
758ad3d6c72SMatthew Wilcox 			(index >> order) << order,		\
759ad3d6c72SMatthew Wilcox 			order - (order % XA_CHUNK_SHIFT),	\
760ad3d6c72SMatthew Wilcox 			(1U << (order % XA_CHUNK_SHIFT)) - 1)
761ad3d6c72SMatthew Wilcox 
762ad3d6c72SMatthew Wilcox #define xas_marked(xas, mark)	xa_marked((xas)->xa, (mark))
763ad3d6c72SMatthew Wilcox #define xas_trylock(xas)	xa_trylock((xas)->xa)
764ad3d6c72SMatthew Wilcox #define xas_lock(xas)		xa_lock((xas)->xa)
765ad3d6c72SMatthew Wilcox #define xas_unlock(xas)		xa_unlock((xas)->xa)
766ad3d6c72SMatthew Wilcox #define xas_lock_bh(xas)	xa_lock_bh((xas)->xa)
767ad3d6c72SMatthew Wilcox #define xas_unlock_bh(xas)	xa_unlock_bh((xas)->xa)
768ad3d6c72SMatthew Wilcox #define xas_lock_irq(xas)	xa_lock_irq((xas)->xa)
769ad3d6c72SMatthew Wilcox #define xas_unlock_irq(xas)	xa_unlock_irq((xas)->xa)
770ad3d6c72SMatthew Wilcox #define xas_lock_irqsave(xas, flags) \
771ad3d6c72SMatthew Wilcox 				xa_lock_irqsave((xas)->xa, flags)
772ad3d6c72SMatthew Wilcox #define xas_unlock_irqrestore(xas, flags) \
773ad3d6c72SMatthew Wilcox 				xa_unlock_irqrestore((xas)->xa, flags)
774ad3d6c72SMatthew Wilcox 
775ad3d6c72SMatthew Wilcox /**
776ad3d6c72SMatthew Wilcox  * xas_error() - Return an errno stored in the xa_state.
777ad3d6c72SMatthew Wilcox  * @xas: XArray operation state.
778ad3d6c72SMatthew Wilcox  *
779ad3d6c72SMatthew Wilcox  * Return: 0 if no error has been noted.  A negative errno if one has.
780ad3d6c72SMatthew Wilcox  */
781ad3d6c72SMatthew Wilcox static inline int xas_error(const struct xa_state *xas)
782ad3d6c72SMatthew Wilcox {
783ad3d6c72SMatthew Wilcox 	return xa_err(xas->xa_node);
784ad3d6c72SMatthew Wilcox }
785ad3d6c72SMatthew Wilcox 
786ad3d6c72SMatthew Wilcox /**
787ad3d6c72SMatthew Wilcox  * xas_set_err() - Note an error in the xa_state.
788ad3d6c72SMatthew Wilcox  * @xas: XArray operation state.
789ad3d6c72SMatthew Wilcox  * @err: Negative error number.
790ad3d6c72SMatthew Wilcox  *
791ad3d6c72SMatthew Wilcox  * Only call this function with a negative @err; zero or positive errors
792ad3d6c72SMatthew Wilcox  * will probably not behave the way you think they should.  If you want
793ad3d6c72SMatthew Wilcox  * to clear the error from an xa_state, use xas_reset().
794ad3d6c72SMatthew Wilcox  */
795ad3d6c72SMatthew Wilcox static inline void xas_set_err(struct xa_state *xas, long err)
796ad3d6c72SMatthew Wilcox {
797ad3d6c72SMatthew Wilcox 	xas->xa_node = XA_ERROR(err);
798ad3d6c72SMatthew Wilcox }
799ad3d6c72SMatthew Wilcox 
800ad3d6c72SMatthew Wilcox /**
801ad3d6c72SMatthew Wilcox  * xas_invalid() - Is the xas in a retry or error state?
802ad3d6c72SMatthew Wilcox  * @xas: XArray operation state.
803ad3d6c72SMatthew Wilcox  *
804ad3d6c72SMatthew Wilcox  * Return: %true if the xas cannot be used for operations.
805ad3d6c72SMatthew Wilcox  */
806ad3d6c72SMatthew Wilcox static inline bool xas_invalid(const struct xa_state *xas)
807ad3d6c72SMatthew Wilcox {
808ad3d6c72SMatthew Wilcox 	return (unsigned long)xas->xa_node & 3;
809ad3d6c72SMatthew Wilcox }
810ad3d6c72SMatthew Wilcox 
811ad3d6c72SMatthew Wilcox /**
812ad3d6c72SMatthew Wilcox  * xas_valid() - Is the xas a valid cursor into the array?
813ad3d6c72SMatthew Wilcox  * @xas: XArray operation state.
814ad3d6c72SMatthew Wilcox  *
815ad3d6c72SMatthew Wilcox  * Return: %true if the xas can be used for operations.
816ad3d6c72SMatthew Wilcox  */
817ad3d6c72SMatthew Wilcox static inline bool xas_valid(const struct xa_state *xas)
818ad3d6c72SMatthew Wilcox {
819ad3d6c72SMatthew Wilcox 	return !xas_invalid(xas);
820ad3d6c72SMatthew Wilcox }
821ad3d6c72SMatthew Wilcox 
8229b89a035SMatthew Wilcox /* True if the pointer is something other than a node */
8239b89a035SMatthew Wilcox static inline bool xas_not_node(struct xa_node *node)
8249b89a035SMatthew Wilcox {
8259b89a035SMatthew Wilcox 	return ((unsigned long)node & 3) || !node;
8269b89a035SMatthew Wilcox }
8279b89a035SMatthew Wilcox 
82858d6ea30SMatthew Wilcox /* True if the node represents head-of-tree, RESTART or BOUNDS */
82958d6ea30SMatthew Wilcox static inline bool xas_top(struct xa_node *node)
83058d6ea30SMatthew Wilcox {
83158d6ea30SMatthew Wilcox 	return node <= XAS_RESTART;
83258d6ea30SMatthew Wilcox }
83358d6ea30SMatthew Wilcox 
834ad3d6c72SMatthew Wilcox /**
835ad3d6c72SMatthew Wilcox  * xas_reset() - Reset an XArray operation state.
836ad3d6c72SMatthew Wilcox  * @xas: XArray operation state.
837ad3d6c72SMatthew Wilcox  *
838ad3d6c72SMatthew Wilcox  * Resets the error or walk state of the @xas so future walks of the
839ad3d6c72SMatthew Wilcox  * array will start from the root.  Use this if you have dropped the
840ad3d6c72SMatthew Wilcox  * xarray lock and want to reuse the xa_state.
841ad3d6c72SMatthew Wilcox  *
842ad3d6c72SMatthew Wilcox  * Context: Any context.
843ad3d6c72SMatthew Wilcox  */
844ad3d6c72SMatthew Wilcox static inline void xas_reset(struct xa_state *xas)
845ad3d6c72SMatthew Wilcox {
846ad3d6c72SMatthew Wilcox 	xas->xa_node = XAS_RESTART;
847ad3d6c72SMatthew Wilcox }
848ad3d6c72SMatthew Wilcox 
849ad3d6c72SMatthew Wilcox /**
850ad3d6c72SMatthew Wilcox  * xas_retry() - Retry the operation if appropriate.
851ad3d6c72SMatthew Wilcox  * @xas: XArray operation state.
852ad3d6c72SMatthew Wilcox  * @entry: Entry from xarray.
853ad3d6c72SMatthew Wilcox  *
854ad3d6c72SMatthew Wilcox  * The advanced functions may sometimes return an internal entry, such as
855ad3d6c72SMatthew Wilcox  * a retry entry or a zero entry.  This function sets up the @xas to restart
856ad3d6c72SMatthew Wilcox  * the walk from the head of the array if needed.
857ad3d6c72SMatthew Wilcox  *
858ad3d6c72SMatthew Wilcox  * Context: Any context.
859ad3d6c72SMatthew Wilcox  * Return: true if the operation needs to be retried.
860ad3d6c72SMatthew Wilcox  */
861ad3d6c72SMatthew Wilcox static inline bool xas_retry(struct xa_state *xas, const void *entry)
862ad3d6c72SMatthew Wilcox {
863ad3d6c72SMatthew Wilcox 	if (!xa_is_retry(entry))
864ad3d6c72SMatthew Wilcox 		return false;
865ad3d6c72SMatthew Wilcox 	xas_reset(xas);
866ad3d6c72SMatthew Wilcox 	return true;
867ad3d6c72SMatthew Wilcox }
868ad3d6c72SMatthew Wilcox 
869ad3d6c72SMatthew Wilcox void *xas_load(struct xa_state *);
87058d6ea30SMatthew Wilcox void *xas_store(struct xa_state *, void *entry);
871*b803b428SMatthew Wilcox void *xas_find(struct xa_state *, unsigned long max);
872ad3d6c72SMatthew Wilcox 
8739b89a035SMatthew Wilcox bool xas_get_mark(const struct xa_state *, xa_mark_t);
8749b89a035SMatthew Wilcox void xas_set_mark(const struct xa_state *, xa_mark_t);
8759b89a035SMatthew Wilcox void xas_clear_mark(const struct xa_state *, xa_mark_t);
876*b803b428SMatthew Wilcox void *xas_find_marked(struct xa_state *, unsigned long max, xa_mark_t);
87758d6ea30SMatthew Wilcox void xas_init_marks(const struct xa_state *);
87858d6ea30SMatthew Wilcox 
87958d6ea30SMatthew Wilcox bool xas_nomem(struct xa_state *, gfp_t);
880*b803b428SMatthew Wilcox void xas_pause(struct xa_state *);
8819b89a035SMatthew Wilcox 
882ad3d6c72SMatthew Wilcox /**
883ad3d6c72SMatthew Wilcox  * xas_reload() - Refetch an entry from the xarray.
884ad3d6c72SMatthew Wilcox  * @xas: XArray operation state.
885ad3d6c72SMatthew Wilcox  *
886ad3d6c72SMatthew Wilcox  * Use this function to check that a previously loaded entry still has
887ad3d6c72SMatthew Wilcox  * the same value.  This is useful for the lockless pagecache lookup where
888ad3d6c72SMatthew Wilcox  * we walk the array with only the RCU lock to protect us, lock the page,
889ad3d6c72SMatthew Wilcox  * then check that the page hasn't moved since we looked it up.
890ad3d6c72SMatthew Wilcox  *
891ad3d6c72SMatthew Wilcox  * The caller guarantees that @xas is still valid.  If it may be in an
892ad3d6c72SMatthew Wilcox  * error or restart state, call xas_load() instead.
893ad3d6c72SMatthew Wilcox  *
894ad3d6c72SMatthew Wilcox  * Return: The entry at this location in the xarray.
895ad3d6c72SMatthew Wilcox  */
896ad3d6c72SMatthew Wilcox static inline void *xas_reload(struct xa_state *xas)
897ad3d6c72SMatthew Wilcox {
898ad3d6c72SMatthew Wilcox 	struct xa_node *node = xas->xa_node;
899ad3d6c72SMatthew Wilcox 
900ad3d6c72SMatthew Wilcox 	if (node)
901ad3d6c72SMatthew Wilcox 		return xa_entry(xas->xa, node, xas->xa_offset);
902ad3d6c72SMatthew Wilcox 	return xa_head(xas->xa);
903ad3d6c72SMatthew Wilcox }
904ad3d6c72SMatthew Wilcox 
90558d6ea30SMatthew Wilcox /**
90658d6ea30SMatthew Wilcox  * xas_set() - Set up XArray operation state for a different index.
90758d6ea30SMatthew Wilcox  * @xas: XArray operation state.
90858d6ea30SMatthew Wilcox  * @index: New index into the XArray.
90958d6ea30SMatthew Wilcox  *
91058d6ea30SMatthew Wilcox  * Move the operation state to refer to a different index.  This will
91158d6ea30SMatthew Wilcox  * have the effect of starting a walk from the top; see xas_next()
91258d6ea30SMatthew Wilcox  * to move to an adjacent index.
91358d6ea30SMatthew Wilcox  */
91458d6ea30SMatthew Wilcox static inline void xas_set(struct xa_state *xas, unsigned long index)
91558d6ea30SMatthew Wilcox {
91658d6ea30SMatthew Wilcox 	xas->xa_index = index;
91758d6ea30SMatthew Wilcox 	xas->xa_node = XAS_RESTART;
91858d6ea30SMatthew Wilcox }
91958d6ea30SMatthew Wilcox 
92058d6ea30SMatthew Wilcox /**
92158d6ea30SMatthew Wilcox  * xas_set_order() - Set up XArray operation state for a multislot entry.
92258d6ea30SMatthew Wilcox  * @xas: XArray operation state.
92358d6ea30SMatthew Wilcox  * @index: Target of the operation.
92458d6ea30SMatthew Wilcox  * @order: Entry occupies 2^@order indices.
92558d6ea30SMatthew Wilcox  */
92658d6ea30SMatthew Wilcox static inline void xas_set_order(struct xa_state *xas, unsigned long index,
92758d6ea30SMatthew Wilcox 					unsigned int order)
92858d6ea30SMatthew Wilcox {
92958d6ea30SMatthew Wilcox #ifdef CONFIG_XARRAY_MULTI
93058d6ea30SMatthew Wilcox 	xas->xa_index = order < BITS_PER_LONG ? (index >> order) << order : 0;
93158d6ea30SMatthew Wilcox 	xas->xa_shift = order - (order % XA_CHUNK_SHIFT);
93258d6ea30SMatthew Wilcox 	xas->xa_sibs = (1 << (order % XA_CHUNK_SHIFT)) - 1;
93358d6ea30SMatthew Wilcox 	xas->xa_node = XAS_RESTART;
93458d6ea30SMatthew Wilcox #else
93558d6ea30SMatthew Wilcox 	BUG_ON(order > 0);
93658d6ea30SMatthew Wilcox 	xas_set(xas, index);
93758d6ea30SMatthew Wilcox #endif
93858d6ea30SMatthew Wilcox }
93958d6ea30SMatthew Wilcox 
94058d6ea30SMatthew Wilcox /**
94158d6ea30SMatthew Wilcox  * xas_set_update() - Set up XArray operation state for a callback.
94258d6ea30SMatthew Wilcox  * @xas: XArray operation state.
94358d6ea30SMatthew Wilcox  * @update: Function to call when updating a node.
94458d6ea30SMatthew Wilcox  *
94558d6ea30SMatthew Wilcox  * The XArray can notify a caller after it has updated an xa_node.
94658d6ea30SMatthew Wilcox  * This is advanced functionality and is only needed by the page cache.
94758d6ea30SMatthew Wilcox  */
94858d6ea30SMatthew Wilcox static inline void xas_set_update(struct xa_state *xas, xa_update_node_t update)
94958d6ea30SMatthew Wilcox {
95058d6ea30SMatthew Wilcox 	xas->xa_update = update;
95158d6ea30SMatthew Wilcox }
95258d6ea30SMatthew Wilcox 
953*b803b428SMatthew Wilcox /**
954*b803b428SMatthew Wilcox  * xas_next_entry() - Advance iterator to next present entry.
955*b803b428SMatthew Wilcox  * @xas: XArray operation state.
956*b803b428SMatthew Wilcox  * @max: Highest index to return.
957*b803b428SMatthew Wilcox  *
958*b803b428SMatthew Wilcox  * xas_next_entry() is an inline function to optimise xarray traversal for
959*b803b428SMatthew Wilcox  * speed.  It is equivalent to calling xas_find(), and will call xas_find()
960*b803b428SMatthew Wilcox  * for all the hard cases.
961*b803b428SMatthew Wilcox  *
962*b803b428SMatthew Wilcox  * Return: The next present entry after the one currently referred to by @xas.
963*b803b428SMatthew Wilcox  */
964*b803b428SMatthew Wilcox static inline void *xas_next_entry(struct xa_state *xas, unsigned long max)
965*b803b428SMatthew Wilcox {
966*b803b428SMatthew Wilcox 	struct xa_node *node = xas->xa_node;
967*b803b428SMatthew Wilcox 	void *entry;
968*b803b428SMatthew Wilcox 
969*b803b428SMatthew Wilcox 	if (unlikely(xas_not_node(node) || node->shift ||
970*b803b428SMatthew Wilcox 			xas->xa_offset != (xas->xa_index & XA_CHUNK_MASK)))
971*b803b428SMatthew Wilcox 		return xas_find(xas, max);
972*b803b428SMatthew Wilcox 
973*b803b428SMatthew Wilcox 	do {
974*b803b428SMatthew Wilcox 		if (unlikely(xas->xa_index >= max))
975*b803b428SMatthew Wilcox 			return xas_find(xas, max);
976*b803b428SMatthew Wilcox 		if (unlikely(xas->xa_offset == XA_CHUNK_MASK))
977*b803b428SMatthew Wilcox 			return xas_find(xas, max);
978*b803b428SMatthew Wilcox 		entry = xa_entry(xas->xa, node, xas->xa_offset + 1);
979*b803b428SMatthew Wilcox 		if (unlikely(xa_is_internal(entry)))
980*b803b428SMatthew Wilcox 			return xas_find(xas, max);
981*b803b428SMatthew Wilcox 		xas->xa_offset++;
982*b803b428SMatthew Wilcox 		xas->xa_index++;
983*b803b428SMatthew Wilcox 	} while (!entry);
984*b803b428SMatthew Wilcox 
985*b803b428SMatthew Wilcox 	return entry;
986*b803b428SMatthew Wilcox }
987*b803b428SMatthew Wilcox 
988*b803b428SMatthew Wilcox /* Private */
989*b803b428SMatthew Wilcox static inline unsigned int xas_find_chunk(struct xa_state *xas, bool advance,
990*b803b428SMatthew Wilcox 		xa_mark_t mark)
991*b803b428SMatthew Wilcox {
992*b803b428SMatthew Wilcox 	unsigned long *addr = xas->xa_node->marks[(__force unsigned)mark];
993*b803b428SMatthew Wilcox 	unsigned int offset = xas->xa_offset;
994*b803b428SMatthew Wilcox 
995*b803b428SMatthew Wilcox 	if (advance)
996*b803b428SMatthew Wilcox 		offset++;
997*b803b428SMatthew Wilcox 	if (XA_CHUNK_SIZE == BITS_PER_LONG) {
998*b803b428SMatthew Wilcox 		if (offset < XA_CHUNK_SIZE) {
999*b803b428SMatthew Wilcox 			unsigned long data = *addr & (~0UL << offset);
1000*b803b428SMatthew Wilcox 			if (data)
1001*b803b428SMatthew Wilcox 				return __ffs(data);
1002*b803b428SMatthew Wilcox 		}
1003*b803b428SMatthew Wilcox 		return XA_CHUNK_SIZE;
1004*b803b428SMatthew Wilcox 	}
1005*b803b428SMatthew Wilcox 
1006*b803b428SMatthew Wilcox 	return find_next_bit(addr, XA_CHUNK_SIZE, offset);
1007*b803b428SMatthew Wilcox }
1008*b803b428SMatthew Wilcox 
1009*b803b428SMatthew Wilcox /**
1010*b803b428SMatthew Wilcox  * xas_next_marked() - Advance iterator to next marked entry.
1011*b803b428SMatthew Wilcox  * @xas: XArray operation state.
1012*b803b428SMatthew Wilcox  * @max: Highest index to return.
1013*b803b428SMatthew Wilcox  * @mark: Mark to search for.
1014*b803b428SMatthew Wilcox  *
1015*b803b428SMatthew Wilcox  * xas_next_marked() is an inline function to optimise xarray traversal for
1016*b803b428SMatthew Wilcox  * speed.  It is equivalent to calling xas_find_marked(), and will call
1017*b803b428SMatthew Wilcox  * xas_find_marked() for all the hard cases.
1018*b803b428SMatthew Wilcox  *
1019*b803b428SMatthew Wilcox  * Return: The next marked entry after the one currently referred to by @xas.
1020*b803b428SMatthew Wilcox  */
1021*b803b428SMatthew Wilcox static inline void *xas_next_marked(struct xa_state *xas, unsigned long max,
1022*b803b428SMatthew Wilcox 								xa_mark_t mark)
1023*b803b428SMatthew Wilcox {
1024*b803b428SMatthew Wilcox 	struct xa_node *node = xas->xa_node;
1025*b803b428SMatthew Wilcox 	unsigned int offset;
1026*b803b428SMatthew Wilcox 
1027*b803b428SMatthew Wilcox 	if (unlikely(xas_not_node(node) || node->shift))
1028*b803b428SMatthew Wilcox 		return xas_find_marked(xas, max, mark);
1029*b803b428SMatthew Wilcox 	offset = xas_find_chunk(xas, true, mark);
1030*b803b428SMatthew Wilcox 	xas->xa_offset = offset;
1031*b803b428SMatthew Wilcox 	xas->xa_index = (xas->xa_index & ~XA_CHUNK_MASK) + offset;
1032*b803b428SMatthew Wilcox 	if (xas->xa_index > max)
1033*b803b428SMatthew Wilcox 		return NULL;
1034*b803b428SMatthew Wilcox 	if (offset == XA_CHUNK_SIZE)
1035*b803b428SMatthew Wilcox 		return xas_find_marked(xas, max, mark);
1036*b803b428SMatthew Wilcox 	return xa_entry(xas->xa, node, offset);
1037*b803b428SMatthew Wilcox }
1038*b803b428SMatthew Wilcox 
1039*b803b428SMatthew Wilcox /*
1040*b803b428SMatthew Wilcox  * If iterating while holding a lock, drop the lock and reschedule
1041*b803b428SMatthew Wilcox  * every %XA_CHECK_SCHED loops.
1042*b803b428SMatthew Wilcox  */
1043*b803b428SMatthew Wilcox enum {
1044*b803b428SMatthew Wilcox 	XA_CHECK_SCHED = 4096,
1045*b803b428SMatthew Wilcox };
1046*b803b428SMatthew Wilcox 
1047*b803b428SMatthew Wilcox /**
1048*b803b428SMatthew Wilcox  * xas_for_each() - Iterate over a range of an XArray.
1049*b803b428SMatthew Wilcox  * @xas: XArray operation state.
1050*b803b428SMatthew Wilcox  * @entry: Entry retrieved from the array.
1051*b803b428SMatthew Wilcox  * @max: Maximum index to retrieve from array.
1052*b803b428SMatthew Wilcox  *
1053*b803b428SMatthew Wilcox  * The loop body will be executed for each entry present in the xarray
1054*b803b428SMatthew Wilcox  * between the current xas position and @max.  @entry will be set to
1055*b803b428SMatthew Wilcox  * the entry retrieved from the xarray.  It is safe to delete entries
1056*b803b428SMatthew Wilcox  * from the array in the loop body.  You should hold either the RCU lock
1057*b803b428SMatthew Wilcox  * or the xa_lock while iterating.  If you need to drop the lock, call
1058*b803b428SMatthew Wilcox  * xas_pause() first.
1059*b803b428SMatthew Wilcox  */
1060*b803b428SMatthew Wilcox #define xas_for_each(xas, entry, max) \
1061*b803b428SMatthew Wilcox 	for (entry = xas_find(xas, max); entry; \
1062*b803b428SMatthew Wilcox 	     entry = xas_next_entry(xas, max))
1063*b803b428SMatthew Wilcox 
1064*b803b428SMatthew Wilcox /**
1065*b803b428SMatthew Wilcox  * xas_for_each_marked() - Iterate over a range of an XArray.
1066*b803b428SMatthew Wilcox  * @xas: XArray operation state.
1067*b803b428SMatthew Wilcox  * @entry: Entry retrieved from the array.
1068*b803b428SMatthew Wilcox  * @max: Maximum index to retrieve from array.
1069*b803b428SMatthew Wilcox  * @mark: Mark to search for.
1070*b803b428SMatthew Wilcox  *
1071*b803b428SMatthew Wilcox  * The loop body will be executed for each marked entry in the xarray
1072*b803b428SMatthew Wilcox  * between the current xas position and @max.  @entry will be set to
1073*b803b428SMatthew Wilcox  * the entry retrieved from the xarray.  It is safe to delete entries
1074*b803b428SMatthew Wilcox  * from the array in the loop body.  You should hold either the RCU lock
1075*b803b428SMatthew Wilcox  * or the xa_lock while iterating.  If you need to drop the lock, call
1076*b803b428SMatthew Wilcox  * xas_pause() first.
1077*b803b428SMatthew Wilcox  */
1078*b803b428SMatthew Wilcox #define xas_for_each_marked(xas, entry, max, mark) \
1079*b803b428SMatthew Wilcox 	for (entry = xas_find_marked(xas, max, mark); entry; \
1080*b803b428SMatthew Wilcox 	     entry = xas_next_marked(xas, max, mark))
1081*b803b428SMatthew Wilcox 
1082f6bb2a2cSMatthew Wilcox #endif /* _LINUX_XARRAY_H */
1083