xref: /linux-6.15/include/linux/xarray.h (revision 55f3f7ea)
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
359f14d4f1SMatthew Wilcox  * 256: Zero entry
369f14d4f1SMatthew Wilcox  * 257: Retry entry
37ad3d6c72SMatthew Wilcox  *
38ad3d6c72SMatthew Wilcox  * Errors are also represented as internal entries, but use the negative
39ad3d6c72SMatthew Wilcox  * space (-4094 to -2).  They're never stored in the slots array; only
40ad3d6c72SMatthew Wilcox  * returned by the normal API.
413159f943SMatthew Wilcox  */
423159f943SMatthew Wilcox 
433159f943SMatthew Wilcox #define BITS_PER_XA_VALUE	(BITS_PER_LONG - 1)
443159f943SMatthew Wilcox 
453159f943SMatthew Wilcox /**
463159f943SMatthew Wilcox  * xa_mk_value() - Create an XArray entry from an integer.
473159f943SMatthew Wilcox  * @v: Value to store in XArray.
483159f943SMatthew Wilcox  *
493159f943SMatthew Wilcox  * Context: Any context.
503159f943SMatthew Wilcox  * Return: An entry suitable for storing in the XArray.
513159f943SMatthew Wilcox  */
523159f943SMatthew Wilcox static inline void *xa_mk_value(unsigned long v)
533159f943SMatthew Wilcox {
543159f943SMatthew Wilcox 	WARN_ON((long)v < 0);
553159f943SMatthew Wilcox 	return (void *)((v << 1) | 1);
563159f943SMatthew Wilcox }
573159f943SMatthew Wilcox 
583159f943SMatthew Wilcox /**
593159f943SMatthew Wilcox  * xa_to_value() - Get value stored in an XArray entry.
603159f943SMatthew Wilcox  * @entry: XArray entry.
613159f943SMatthew Wilcox  *
623159f943SMatthew Wilcox  * Context: Any context.
633159f943SMatthew Wilcox  * Return: The value stored in the XArray entry.
643159f943SMatthew Wilcox  */
653159f943SMatthew Wilcox static inline unsigned long xa_to_value(const void *entry)
663159f943SMatthew Wilcox {
673159f943SMatthew Wilcox 	return (unsigned long)entry >> 1;
683159f943SMatthew Wilcox }
693159f943SMatthew Wilcox 
703159f943SMatthew Wilcox /**
713159f943SMatthew Wilcox  * xa_is_value() - Determine if an entry is a value.
723159f943SMatthew Wilcox  * @entry: XArray entry.
733159f943SMatthew Wilcox  *
743159f943SMatthew Wilcox  * Context: Any context.
753159f943SMatthew Wilcox  * Return: True if the entry is a value, false if it is a pointer.
763159f943SMatthew Wilcox  */
773159f943SMatthew Wilcox static inline bool xa_is_value(const void *entry)
783159f943SMatthew Wilcox {
793159f943SMatthew Wilcox 	return (unsigned long)entry & 1;
803159f943SMatthew Wilcox }
813159f943SMatthew Wilcox 
823159f943SMatthew Wilcox /**
833159f943SMatthew Wilcox  * xa_tag_pointer() - Create an XArray entry for a tagged pointer.
843159f943SMatthew Wilcox  * @p: Plain pointer.
853159f943SMatthew Wilcox  * @tag: Tag value (0, 1 or 3).
863159f943SMatthew Wilcox  *
873159f943SMatthew Wilcox  * If the user of the XArray prefers, they can tag their pointers instead
883159f943SMatthew Wilcox  * of storing value entries.  Three tags are available (0, 1 and 3).
893159f943SMatthew Wilcox  * These are distinct from the xa_mark_t as they are not replicated up
903159f943SMatthew Wilcox  * through the array and cannot be searched for.
913159f943SMatthew Wilcox  *
923159f943SMatthew Wilcox  * Context: Any context.
933159f943SMatthew Wilcox  * Return: An XArray entry.
943159f943SMatthew Wilcox  */
953159f943SMatthew Wilcox static inline void *xa_tag_pointer(void *p, unsigned long tag)
963159f943SMatthew Wilcox {
973159f943SMatthew Wilcox 	return (void *)((unsigned long)p | tag);
983159f943SMatthew Wilcox }
993159f943SMatthew Wilcox 
1003159f943SMatthew Wilcox /**
1013159f943SMatthew Wilcox  * xa_untag_pointer() - Turn an XArray entry into a plain pointer.
1023159f943SMatthew Wilcox  * @entry: XArray entry.
1033159f943SMatthew Wilcox  *
1043159f943SMatthew Wilcox  * If you have stored a tagged pointer in the XArray, call this function
1053159f943SMatthew Wilcox  * to get the untagged version of the pointer.
1063159f943SMatthew Wilcox  *
1073159f943SMatthew Wilcox  * Context: Any context.
1083159f943SMatthew Wilcox  * Return: A pointer.
1093159f943SMatthew Wilcox  */
1103159f943SMatthew Wilcox static inline void *xa_untag_pointer(void *entry)
1113159f943SMatthew Wilcox {
1123159f943SMatthew Wilcox 	return (void *)((unsigned long)entry & ~3UL);
1133159f943SMatthew Wilcox }
1143159f943SMatthew Wilcox 
1153159f943SMatthew Wilcox /**
1163159f943SMatthew Wilcox  * xa_pointer_tag() - Get the tag stored in an XArray entry.
1173159f943SMatthew Wilcox  * @entry: XArray entry.
1183159f943SMatthew Wilcox  *
1193159f943SMatthew Wilcox  * If you have stored a tagged pointer in the XArray, call this function
1203159f943SMatthew Wilcox  * to get the tag of that pointer.
1213159f943SMatthew Wilcox  *
1223159f943SMatthew Wilcox  * Context: Any context.
1233159f943SMatthew Wilcox  * Return: A tag.
1243159f943SMatthew Wilcox  */
1253159f943SMatthew Wilcox static inline unsigned int xa_pointer_tag(void *entry)
1263159f943SMatthew Wilcox {
1273159f943SMatthew Wilcox 	return (unsigned long)entry & 3UL;
1283159f943SMatthew Wilcox }
129f6bb2a2cSMatthew Wilcox 
13002c02bf1SMatthew Wilcox /*
13102c02bf1SMatthew Wilcox  * xa_mk_internal() - Create an internal entry.
13202c02bf1SMatthew Wilcox  * @v: Value to turn into an internal entry.
13302c02bf1SMatthew Wilcox  *
13402c02bf1SMatthew Wilcox  * Context: Any context.
13502c02bf1SMatthew Wilcox  * Return: An XArray internal entry corresponding to this value.
13602c02bf1SMatthew Wilcox  */
13702c02bf1SMatthew Wilcox static inline void *xa_mk_internal(unsigned long v)
13802c02bf1SMatthew Wilcox {
13902c02bf1SMatthew Wilcox 	return (void *)((v << 2) | 2);
14002c02bf1SMatthew Wilcox }
14102c02bf1SMatthew Wilcox 
14202c02bf1SMatthew Wilcox /*
14302c02bf1SMatthew Wilcox  * xa_to_internal() - Extract the value from an internal entry.
14402c02bf1SMatthew Wilcox  * @entry: XArray entry.
14502c02bf1SMatthew Wilcox  *
14602c02bf1SMatthew Wilcox  * Context: Any context.
14702c02bf1SMatthew Wilcox  * Return: The value which was stored in the internal entry.
14802c02bf1SMatthew Wilcox  */
14902c02bf1SMatthew Wilcox static inline unsigned long xa_to_internal(const void *entry)
15002c02bf1SMatthew Wilcox {
15102c02bf1SMatthew Wilcox 	return (unsigned long)entry >> 2;
15202c02bf1SMatthew Wilcox }
15302c02bf1SMatthew Wilcox 
15402c02bf1SMatthew Wilcox /*
15502c02bf1SMatthew Wilcox  * xa_is_internal() - Is the entry an internal entry?
15602c02bf1SMatthew Wilcox  * @entry: XArray entry.
15702c02bf1SMatthew Wilcox  *
15802c02bf1SMatthew Wilcox  * Context: Any context.
15902c02bf1SMatthew Wilcox  * Return: %true if the entry is an internal entry.
16002c02bf1SMatthew Wilcox  */
16102c02bf1SMatthew Wilcox static inline bool xa_is_internal(const void *entry)
16202c02bf1SMatthew Wilcox {
16302c02bf1SMatthew Wilcox 	return ((unsigned long)entry & 3) == 2;
16402c02bf1SMatthew Wilcox }
16502c02bf1SMatthew Wilcox 
166f8d5d0ccSMatthew Wilcox /**
167ad3d6c72SMatthew Wilcox  * xa_is_err() - Report whether an XArray operation returned an error
168ad3d6c72SMatthew Wilcox  * @entry: Result from calling an XArray function
169ad3d6c72SMatthew Wilcox  *
170ad3d6c72SMatthew Wilcox  * If an XArray operation cannot complete an operation, it will return
171ad3d6c72SMatthew Wilcox  * a special value indicating an error.  This function tells you
172ad3d6c72SMatthew Wilcox  * whether an error occurred; xa_err() tells you which error occurred.
173ad3d6c72SMatthew Wilcox  *
174ad3d6c72SMatthew Wilcox  * Context: Any context.
175ad3d6c72SMatthew Wilcox  * Return: %true if the entry indicates an error.
176ad3d6c72SMatthew Wilcox  */
177ad3d6c72SMatthew Wilcox static inline bool xa_is_err(const void *entry)
178ad3d6c72SMatthew Wilcox {
179ad3d6c72SMatthew Wilcox 	return unlikely(xa_is_internal(entry));
180ad3d6c72SMatthew Wilcox }
181ad3d6c72SMatthew Wilcox 
182ad3d6c72SMatthew Wilcox /**
183ad3d6c72SMatthew Wilcox  * xa_err() - Turn an XArray result into an errno.
184ad3d6c72SMatthew Wilcox  * @entry: Result from calling an XArray function.
185ad3d6c72SMatthew Wilcox  *
186ad3d6c72SMatthew Wilcox  * If an XArray operation cannot complete an operation, it will return
187ad3d6c72SMatthew Wilcox  * a special pointer value which encodes an errno.  This function extracts
188ad3d6c72SMatthew Wilcox  * the errno from the pointer value, or returns 0 if the pointer does not
189ad3d6c72SMatthew Wilcox  * represent an errno.
190ad3d6c72SMatthew Wilcox  *
191ad3d6c72SMatthew Wilcox  * Context: Any context.
192ad3d6c72SMatthew Wilcox  * Return: A negative errno or 0.
193ad3d6c72SMatthew Wilcox  */
194ad3d6c72SMatthew Wilcox static inline int xa_err(void *entry)
195ad3d6c72SMatthew Wilcox {
196ad3d6c72SMatthew Wilcox 	/* xa_to_internal() would not do sign extension. */
197ad3d6c72SMatthew Wilcox 	if (xa_is_err(entry))
198ad3d6c72SMatthew Wilcox 		return (long)entry >> 2;
199ad3d6c72SMatthew Wilcox 	return 0;
200ad3d6c72SMatthew Wilcox }
201ad3d6c72SMatthew Wilcox 
2029b89a035SMatthew Wilcox typedef unsigned __bitwise xa_mark_t;
2039b89a035SMatthew Wilcox #define XA_MARK_0		((__force xa_mark_t)0U)
2049b89a035SMatthew Wilcox #define XA_MARK_1		((__force xa_mark_t)1U)
2059b89a035SMatthew Wilcox #define XA_MARK_2		((__force xa_mark_t)2U)
2069b89a035SMatthew Wilcox #define XA_PRESENT		((__force xa_mark_t)8U)
2079b89a035SMatthew Wilcox #define XA_MARK_MAX		XA_MARK_2
208371c752dSMatthew Wilcox #define XA_FREE_MARK		XA_MARK_0
2099b89a035SMatthew Wilcox 
21058d6ea30SMatthew Wilcox enum xa_lock_type {
21158d6ea30SMatthew Wilcox 	XA_LOCK_IRQ = 1,
21258d6ea30SMatthew Wilcox 	XA_LOCK_BH = 2,
21358d6ea30SMatthew Wilcox };
21458d6ea30SMatthew Wilcox 
2159b89a035SMatthew Wilcox /*
2169b89a035SMatthew Wilcox  * Values for xa_flags.  The radix tree stores its GFP flags in the xa_flags,
2179b89a035SMatthew Wilcox  * and we remain compatible with that.
2189b89a035SMatthew Wilcox  */
21958d6ea30SMatthew Wilcox #define XA_FLAGS_LOCK_IRQ	((__force gfp_t)XA_LOCK_IRQ)
22058d6ea30SMatthew Wilcox #define XA_FLAGS_LOCK_BH	((__force gfp_t)XA_LOCK_BH)
221371c752dSMatthew Wilcox #define XA_FLAGS_TRACK_FREE	((__force gfp_t)4U)
2229b89a035SMatthew Wilcox #define XA_FLAGS_MARK(mark)	((__force gfp_t)((1U << __GFP_BITS_SHIFT) << \
2239b89a035SMatthew Wilcox 						(__force unsigned)(mark)))
2249b89a035SMatthew Wilcox 
225371c752dSMatthew Wilcox #define XA_FLAGS_ALLOC	(XA_FLAGS_TRACK_FREE | XA_FLAGS_MARK(XA_FREE_MARK))
226371c752dSMatthew Wilcox 
227ad3d6c72SMatthew Wilcox /**
228f8d5d0ccSMatthew Wilcox  * struct xarray - The anchor of the XArray.
229f8d5d0ccSMatthew Wilcox  * @xa_lock: Lock that protects the contents of the XArray.
230f8d5d0ccSMatthew Wilcox  *
231f8d5d0ccSMatthew Wilcox  * To use the xarray, define it statically or embed it in your data structure.
232f8d5d0ccSMatthew Wilcox  * It is a very small data structure, so it does not usually make sense to
233f8d5d0ccSMatthew Wilcox  * allocate it separately and keep a pointer to it in your data structure.
234f8d5d0ccSMatthew Wilcox  *
235f8d5d0ccSMatthew Wilcox  * You may use the xa_lock to protect your own data structures as well.
236f8d5d0ccSMatthew Wilcox  */
237f8d5d0ccSMatthew Wilcox /*
238f8d5d0ccSMatthew Wilcox  * If all of the entries in the array are NULL, @xa_head is a NULL pointer.
239f8d5d0ccSMatthew Wilcox  * If the only non-NULL entry in the array is at index 0, @xa_head is that
240f8d5d0ccSMatthew Wilcox  * entry.  If any other entry in the array is non-NULL, @xa_head points
241f8d5d0ccSMatthew Wilcox  * to an @xa_node.
242f8d5d0ccSMatthew Wilcox  */
243f8d5d0ccSMatthew Wilcox struct xarray {
244f8d5d0ccSMatthew Wilcox 	spinlock_t	xa_lock;
245f8d5d0ccSMatthew Wilcox /* private: The rest of the data structure is not to be used directly. */
246f8d5d0ccSMatthew Wilcox 	gfp_t		xa_flags;
247f8d5d0ccSMatthew Wilcox 	void __rcu *	xa_head;
248f8d5d0ccSMatthew Wilcox };
249f8d5d0ccSMatthew Wilcox 
250f8d5d0ccSMatthew Wilcox #define XARRAY_INIT(name, flags) {				\
251f8d5d0ccSMatthew Wilcox 	.xa_lock = __SPIN_LOCK_UNLOCKED(name.xa_lock),		\
252f8d5d0ccSMatthew Wilcox 	.xa_flags = flags,					\
253f8d5d0ccSMatthew Wilcox 	.xa_head = NULL,					\
254f8d5d0ccSMatthew Wilcox }
255f8d5d0ccSMatthew Wilcox 
256f8d5d0ccSMatthew Wilcox /**
257f8d5d0ccSMatthew Wilcox  * DEFINE_XARRAY_FLAGS() - Define an XArray with custom flags.
258f8d5d0ccSMatthew Wilcox  * @name: A string that names your XArray.
259f8d5d0ccSMatthew Wilcox  * @flags: XA_FLAG values.
260f8d5d0ccSMatthew Wilcox  *
261f8d5d0ccSMatthew Wilcox  * This is intended for file scope definitions of XArrays.  It declares
262f8d5d0ccSMatthew Wilcox  * and initialises an empty XArray with the chosen name and flags.  It is
263f8d5d0ccSMatthew Wilcox  * equivalent to calling xa_init_flags() on the array, but it does the
264f8d5d0ccSMatthew Wilcox  * initialisation at compiletime instead of runtime.
265f8d5d0ccSMatthew Wilcox  */
266f8d5d0ccSMatthew Wilcox #define DEFINE_XARRAY_FLAGS(name, flags)				\
267f8d5d0ccSMatthew Wilcox 	struct xarray name = XARRAY_INIT(name, flags)
268f8d5d0ccSMatthew Wilcox 
269f8d5d0ccSMatthew Wilcox /**
270f8d5d0ccSMatthew Wilcox  * DEFINE_XARRAY() - Define an XArray.
271f8d5d0ccSMatthew Wilcox  * @name: A string that names your XArray.
272f8d5d0ccSMatthew Wilcox  *
273f8d5d0ccSMatthew Wilcox  * This is intended for file scope definitions of XArrays.  It declares
274f8d5d0ccSMatthew Wilcox  * and initialises an empty XArray with the chosen name.  It is equivalent
275f8d5d0ccSMatthew Wilcox  * to calling xa_init() on the array, but it does the initialisation at
276f8d5d0ccSMatthew Wilcox  * compiletime instead of runtime.
277f8d5d0ccSMatthew Wilcox  */
278f8d5d0ccSMatthew Wilcox #define DEFINE_XARRAY(name) DEFINE_XARRAY_FLAGS(name, 0)
279f8d5d0ccSMatthew Wilcox 
280371c752dSMatthew Wilcox /**
281371c752dSMatthew Wilcox  * DEFINE_XARRAY_ALLOC() - Define an XArray which can allocate IDs.
282371c752dSMatthew Wilcox  * @name: A string that names your XArray.
283371c752dSMatthew Wilcox  *
284371c752dSMatthew Wilcox  * This is intended for file scope definitions of allocating XArrays.
285371c752dSMatthew Wilcox  * See also DEFINE_XARRAY().
286371c752dSMatthew Wilcox  */
287371c752dSMatthew Wilcox #define DEFINE_XARRAY_ALLOC(name) DEFINE_XARRAY_FLAGS(name, XA_FLAGS_ALLOC)
288371c752dSMatthew Wilcox 
289f8d5d0ccSMatthew Wilcox void xa_init_flags(struct xarray *, gfp_t flags);
290ad3d6c72SMatthew Wilcox void *xa_load(struct xarray *, unsigned long index);
29158d6ea30SMatthew Wilcox void *xa_store(struct xarray *, unsigned long index, void *entry, gfp_t);
2929c16bb88SMatthew Wilcox void *xa_erase(struct xarray *, unsigned long index);
2930e9446c3SMatthew Wilcox void *xa_store_range(struct xarray *, unsigned long first, unsigned long last,
2940e9446c3SMatthew Wilcox 			void *entry, gfp_t);
2959b89a035SMatthew Wilcox bool xa_get_mark(struct xarray *, unsigned long index, xa_mark_t);
2969b89a035SMatthew Wilcox void xa_set_mark(struct xarray *, unsigned long index, xa_mark_t);
2979b89a035SMatthew Wilcox void xa_clear_mark(struct xarray *, unsigned long index, xa_mark_t);
298b803b428SMatthew Wilcox void *xa_find(struct xarray *xa, unsigned long *index,
299b803b428SMatthew Wilcox 		unsigned long max, xa_mark_t) __attribute__((nonnull(2)));
300b803b428SMatthew Wilcox void *xa_find_after(struct xarray *xa, unsigned long *index,
301b803b428SMatthew Wilcox 		unsigned long max, xa_mark_t) __attribute__((nonnull(2)));
30280a0a1a9SMatthew Wilcox unsigned int xa_extract(struct xarray *, void **dst, unsigned long start,
30380a0a1a9SMatthew Wilcox 		unsigned long max, unsigned int n, xa_mark_t);
304687149fcSMatthew Wilcox void xa_destroy(struct xarray *);
305f8d5d0ccSMatthew Wilcox 
306f8d5d0ccSMatthew Wilcox /**
307f8d5d0ccSMatthew Wilcox  * xa_init() - Initialise an empty XArray.
308f8d5d0ccSMatthew Wilcox  * @xa: XArray.
309f8d5d0ccSMatthew Wilcox  *
310f8d5d0ccSMatthew Wilcox  * An empty XArray is full of NULL entries.
311f8d5d0ccSMatthew Wilcox  *
312f8d5d0ccSMatthew Wilcox  * Context: Any context.
313f8d5d0ccSMatthew Wilcox  */
314f8d5d0ccSMatthew Wilcox static inline void xa_init(struct xarray *xa)
315f8d5d0ccSMatthew Wilcox {
316f8d5d0ccSMatthew Wilcox 	xa_init_flags(xa, 0);
317f8d5d0ccSMatthew Wilcox }
318f8d5d0ccSMatthew Wilcox 
319ad3d6c72SMatthew Wilcox /**
320ad3d6c72SMatthew Wilcox  * xa_empty() - Determine if an array has any present entries.
321ad3d6c72SMatthew Wilcox  * @xa: XArray.
322ad3d6c72SMatthew Wilcox  *
323ad3d6c72SMatthew Wilcox  * Context: Any context.
324ad3d6c72SMatthew Wilcox  * Return: %true if the array contains only NULL pointers.
325ad3d6c72SMatthew Wilcox  */
326ad3d6c72SMatthew Wilcox static inline bool xa_empty(const struct xarray *xa)
327ad3d6c72SMatthew Wilcox {
328ad3d6c72SMatthew Wilcox 	return xa->xa_head == NULL;
329ad3d6c72SMatthew Wilcox }
330ad3d6c72SMatthew Wilcox 
3319b89a035SMatthew Wilcox /**
3329b89a035SMatthew Wilcox  * xa_marked() - Inquire whether any entry in this array has a mark set
3339b89a035SMatthew Wilcox  * @xa: Array
3349b89a035SMatthew Wilcox  * @mark: Mark value
3359b89a035SMatthew Wilcox  *
3369b89a035SMatthew Wilcox  * Context: Any context.
3379b89a035SMatthew Wilcox  * Return: %true if any entry has this mark set.
3389b89a035SMatthew Wilcox  */
3399b89a035SMatthew Wilcox static inline bool xa_marked(const struct xarray *xa, xa_mark_t mark)
3409b89a035SMatthew Wilcox {
3419b89a035SMatthew Wilcox 	return xa->xa_flags & XA_FLAGS_MARK(mark);
3429b89a035SMatthew Wilcox }
3439b89a035SMatthew Wilcox 
34458d6ea30SMatthew Wilcox /**
345b803b428SMatthew Wilcox  * xa_for_each() - Iterate over a portion of an XArray.
346b803b428SMatthew Wilcox  * @xa: XArray.
347b803b428SMatthew Wilcox  * @entry: Entry retrieved from array.
348b803b428SMatthew Wilcox  * @index: Index of @entry.
349b803b428SMatthew Wilcox  * @max: Maximum index to retrieve from array.
350b803b428SMatthew Wilcox  * @filter: Selection criterion.
351b803b428SMatthew Wilcox  *
352b803b428SMatthew Wilcox  * Initialise @index to the lowest index you want to retrieve from the
353b803b428SMatthew Wilcox  * array.  During the iteration, @entry will have the value of the entry
354b803b428SMatthew Wilcox  * stored in @xa at @index.  The iteration will skip all entries in the
355b803b428SMatthew Wilcox  * array which do not match @filter.  You may modify @index during the
356b803b428SMatthew Wilcox  * iteration if you want to skip or reprocess indices.  It is safe to modify
357b803b428SMatthew Wilcox  * the array during the iteration.  At the end of the iteration, @entry will
358b803b428SMatthew Wilcox  * be set to NULL and @index will have a value less than or equal to max.
359b803b428SMatthew Wilcox  *
360b803b428SMatthew Wilcox  * xa_for_each() is O(n.log(n)) while xas_for_each() is O(n).  You have
361b803b428SMatthew Wilcox  * to handle your own locking with xas_for_each(), and if you have to unlock
362b803b428SMatthew Wilcox  * after each iteration, it will also end up being O(n.log(n)).  xa_for_each()
363b803b428SMatthew Wilcox  * will spin if it hits a retry entry; if you intend to see retry entries,
364b803b428SMatthew Wilcox  * you should use the xas_for_each() iterator instead.  The xas_for_each()
365b803b428SMatthew Wilcox  * iterator will expand into more inline code than xa_for_each().
366b803b428SMatthew Wilcox  *
367b803b428SMatthew Wilcox  * Context: Any context.  Takes and releases the RCU lock.
368b803b428SMatthew Wilcox  */
369b803b428SMatthew Wilcox #define xa_for_each(xa, entry, index, max, filter) \
370b803b428SMatthew Wilcox 	for (entry = xa_find(xa, &index, max, filter); entry; \
371b803b428SMatthew Wilcox 	     entry = xa_find_after(xa, &index, max, filter))
372b803b428SMatthew Wilcox 
373f6bb2a2cSMatthew Wilcox #define xa_trylock(xa)		spin_trylock(&(xa)->xa_lock)
374f6bb2a2cSMatthew Wilcox #define xa_lock(xa)		spin_lock(&(xa)->xa_lock)
375f6bb2a2cSMatthew Wilcox #define xa_unlock(xa)		spin_unlock(&(xa)->xa_lock)
376f6bb2a2cSMatthew Wilcox #define xa_lock_bh(xa)		spin_lock_bh(&(xa)->xa_lock)
377f6bb2a2cSMatthew Wilcox #define xa_unlock_bh(xa)	spin_unlock_bh(&(xa)->xa_lock)
378f6bb2a2cSMatthew Wilcox #define xa_lock_irq(xa)		spin_lock_irq(&(xa)->xa_lock)
379f6bb2a2cSMatthew Wilcox #define xa_unlock_irq(xa)	spin_unlock_irq(&(xa)->xa_lock)
380f6bb2a2cSMatthew Wilcox #define xa_lock_irqsave(xa, flags) \
381f6bb2a2cSMatthew Wilcox 				spin_lock_irqsave(&(xa)->xa_lock, flags)
382f6bb2a2cSMatthew Wilcox #define xa_unlock_irqrestore(xa, flags) \
383f6bb2a2cSMatthew Wilcox 				spin_unlock_irqrestore(&(xa)->xa_lock, flags)
384f6bb2a2cSMatthew Wilcox 
3859b89a035SMatthew Wilcox /*
38658d6ea30SMatthew Wilcox  * Versions of the normal API which require the caller to hold the
38758d6ea30SMatthew Wilcox  * xa_lock.  If the GFP flags allow it, they will drop the lock to
38858d6ea30SMatthew Wilcox  * allocate memory, then reacquire it afterwards.  These functions
38958d6ea30SMatthew Wilcox  * may also re-enable interrupts if the XArray flags indicate the
39058d6ea30SMatthew Wilcox  * locking should be interrupt safe.
3919b89a035SMatthew Wilcox  */
39258d6ea30SMatthew Wilcox void *__xa_erase(struct xarray *, unsigned long index);
39358d6ea30SMatthew Wilcox void *__xa_store(struct xarray *, unsigned long index, void *entry, gfp_t);
39441aec91fSMatthew Wilcox void *__xa_cmpxchg(struct xarray *, unsigned long index, void *old,
39541aec91fSMatthew Wilcox 		void *entry, gfp_t);
396371c752dSMatthew Wilcox int __xa_alloc(struct xarray *, u32 *id, u32 max, void *entry, gfp_t);
3974c0608f4SMatthew Wilcox int __xa_reserve(struct xarray *, unsigned long index, gfp_t);
3989b89a035SMatthew Wilcox void __xa_set_mark(struct xarray *, unsigned long index, xa_mark_t);
3999b89a035SMatthew Wilcox void __xa_clear_mark(struct xarray *, unsigned long index, xa_mark_t);
4009b89a035SMatthew Wilcox 
40158d6ea30SMatthew Wilcox /**
40241aec91fSMatthew Wilcox  * __xa_insert() - Store this entry in the XArray unless another entry is
40341aec91fSMatthew Wilcox  *			already present.
40441aec91fSMatthew Wilcox  * @xa: XArray.
40541aec91fSMatthew Wilcox  * @index: Index into array.
40641aec91fSMatthew Wilcox  * @entry: New entry.
40741aec91fSMatthew Wilcox  * @gfp: Memory allocation flags.
40841aec91fSMatthew Wilcox  *
40941aec91fSMatthew Wilcox  * If you would rather see the existing entry in the array, use __xa_cmpxchg().
41041aec91fSMatthew Wilcox  * This function is for users who don't care what the entry is, only that
41141aec91fSMatthew Wilcox  * one is present.
41241aec91fSMatthew Wilcox  *
41341aec91fSMatthew Wilcox  * Context: Any context.  Expects xa_lock to be held on entry.  May
41441aec91fSMatthew Wilcox  *	    release and reacquire xa_lock if the @gfp flags permit.
41541aec91fSMatthew Wilcox  * Return: 0 if the store succeeded.  -EEXIST if another entry was present.
41641aec91fSMatthew Wilcox  * -ENOMEM if memory could not be allocated.
41741aec91fSMatthew Wilcox  */
41841aec91fSMatthew Wilcox static inline int __xa_insert(struct xarray *xa, unsigned long index,
41941aec91fSMatthew Wilcox 		void *entry, gfp_t gfp)
42041aec91fSMatthew Wilcox {
42141aec91fSMatthew Wilcox 	void *curr = __xa_cmpxchg(xa, index, NULL, entry, gfp);
42241aec91fSMatthew Wilcox 	if (!curr)
42341aec91fSMatthew Wilcox 		return 0;
42441aec91fSMatthew Wilcox 	if (xa_is_err(curr))
42541aec91fSMatthew Wilcox 		return xa_err(curr);
42641aec91fSMatthew Wilcox 	return -EEXIST;
42741aec91fSMatthew Wilcox }
42841aec91fSMatthew Wilcox 
42941aec91fSMatthew Wilcox /**
43084e5acb7SMatthew Wilcox  * xa_store_bh() - Store this entry in the XArray.
43184e5acb7SMatthew Wilcox  * @xa: XArray.
43284e5acb7SMatthew Wilcox  * @index: Index into array.
43384e5acb7SMatthew Wilcox  * @entry: New entry.
43484e5acb7SMatthew Wilcox  * @gfp: Memory allocation flags.
43584e5acb7SMatthew Wilcox  *
43684e5acb7SMatthew Wilcox  * This function is like calling xa_store() except it disables softirqs
43784e5acb7SMatthew Wilcox  * while holding the array lock.
43884e5acb7SMatthew Wilcox  *
43984e5acb7SMatthew Wilcox  * Context: Any context.  Takes and releases the xa_lock while
44084e5acb7SMatthew Wilcox  * disabling softirqs.
44184e5acb7SMatthew Wilcox  * Return: The entry which used to be at this index.
44284e5acb7SMatthew Wilcox  */
44384e5acb7SMatthew Wilcox static inline void *xa_store_bh(struct xarray *xa, unsigned long index,
44484e5acb7SMatthew Wilcox 		void *entry, gfp_t gfp)
44584e5acb7SMatthew Wilcox {
44684e5acb7SMatthew Wilcox 	void *curr;
44784e5acb7SMatthew Wilcox 
44884e5acb7SMatthew Wilcox 	xa_lock_bh(xa);
44984e5acb7SMatthew Wilcox 	curr = __xa_store(xa, index, entry, gfp);
45084e5acb7SMatthew Wilcox 	xa_unlock_bh(xa);
45184e5acb7SMatthew Wilcox 
45284e5acb7SMatthew Wilcox 	return curr;
45384e5acb7SMatthew Wilcox }
45484e5acb7SMatthew Wilcox 
45584e5acb7SMatthew Wilcox /**
45684e5acb7SMatthew Wilcox  * xa_store_irq() - Erase this entry from the XArray.
45784e5acb7SMatthew Wilcox  * @xa: XArray.
45884e5acb7SMatthew Wilcox  * @index: Index into array.
45984e5acb7SMatthew Wilcox  * @entry: New entry.
46084e5acb7SMatthew Wilcox  * @gfp: Memory allocation flags.
46184e5acb7SMatthew Wilcox  *
46284e5acb7SMatthew Wilcox  * This function is like calling xa_store() except it disables interrupts
46384e5acb7SMatthew Wilcox  * while holding the array lock.
46484e5acb7SMatthew Wilcox  *
46584e5acb7SMatthew Wilcox  * Context: Process context.  Takes and releases the xa_lock while
46684e5acb7SMatthew Wilcox  * disabling interrupts.
46784e5acb7SMatthew Wilcox  * Return: The entry which used to be at this index.
46884e5acb7SMatthew Wilcox  */
46984e5acb7SMatthew Wilcox static inline void *xa_store_irq(struct xarray *xa, unsigned long index,
47084e5acb7SMatthew Wilcox 		void *entry, gfp_t gfp)
47184e5acb7SMatthew Wilcox {
47284e5acb7SMatthew Wilcox 	void *curr;
47384e5acb7SMatthew Wilcox 
47484e5acb7SMatthew Wilcox 	xa_lock_irq(xa);
47584e5acb7SMatthew Wilcox 	curr = __xa_store(xa, index, entry, gfp);
47684e5acb7SMatthew Wilcox 	xa_unlock_irq(xa);
47784e5acb7SMatthew Wilcox 
47884e5acb7SMatthew Wilcox 	return curr;
47984e5acb7SMatthew Wilcox }
48084e5acb7SMatthew Wilcox 
48184e5acb7SMatthew Wilcox /**
48258d6ea30SMatthew Wilcox  * xa_erase_bh() - Erase this entry from the XArray.
48358d6ea30SMatthew Wilcox  * @xa: XArray.
48458d6ea30SMatthew Wilcox  * @index: Index of entry.
48558d6ea30SMatthew Wilcox  *
48658d6ea30SMatthew Wilcox  * This function is the equivalent of calling xa_store() with %NULL as
48758d6ea30SMatthew Wilcox  * the third argument.  The XArray does not need to allocate memory, so
48858d6ea30SMatthew Wilcox  * the user does not need to provide GFP flags.
48958d6ea30SMatthew Wilcox  *
490804dfaf0SMatthew Wilcox  * Context: Any context.  Takes and releases the xa_lock while
49158d6ea30SMatthew Wilcox  * disabling softirqs.
49258d6ea30SMatthew Wilcox  * Return: The entry which used to be at this index.
49358d6ea30SMatthew Wilcox  */
49458d6ea30SMatthew Wilcox static inline void *xa_erase_bh(struct xarray *xa, unsigned long index)
49558d6ea30SMatthew Wilcox {
49658d6ea30SMatthew Wilcox 	void *entry;
49758d6ea30SMatthew Wilcox 
49858d6ea30SMatthew Wilcox 	xa_lock_bh(xa);
49958d6ea30SMatthew Wilcox 	entry = __xa_erase(xa, index);
50058d6ea30SMatthew Wilcox 	xa_unlock_bh(xa);
50158d6ea30SMatthew Wilcox 
50258d6ea30SMatthew Wilcox 	return entry;
50358d6ea30SMatthew Wilcox }
50458d6ea30SMatthew Wilcox 
50558d6ea30SMatthew Wilcox /**
50658d6ea30SMatthew Wilcox  * xa_erase_irq() - Erase this entry from the XArray.
50758d6ea30SMatthew Wilcox  * @xa: XArray.
50858d6ea30SMatthew Wilcox  * @index: Index of entry.
50958d6ea30SMatthew Wilcox  *
51058d6ea30SMatthew Wilcox  * This function is the equivalent of calling xa_store() with %NULL as
51158d6ea30SMatthew Wilcox  * the third argument.  The XArray does not need to allocate memory, so
51258d6ea30SMatthew Wilcox  * the user does not need to provide GFP flags.
51358d6ea30SMatthew Wilcox  *
51458d6ea30SMatthew Wilcox  * Context: Process context.  Takes and releases the xa_lock while
51558d6ea30SMatthew Wilcox  * disabling interrupts.
51658d6ea30SMatthew Wilcox  * Return: The entry which used to be at this index.
51758d6ea30SMatthew Wilcox  */
51858d6ea30SMatthew Wilcox static inline void *xa_erase_irq(struct xarray *xa, unsigned long index)
51958d6ea30SMatthew Wilcox {
52058d6ea30SMatthew Wilcox 	void *entry;
52158d6ea30SMatthew Wilcox 
52258d6ea30SMatthew Wilcox 	xa_lock_irq(xa);
52358d6ea30SMatthew Wilcox 	entry = __xa_erase(xa, index);
52458d6ea30SMatthew Wilcox 	xa_unlock_irq(xa);
52558d6ea30SMatthew Wilcox 
52658d6ea30SMatthew Wilcox 	return entry;
52758d6ea30SMatthew Wilcox }
52858d6ea30SMatthew Wilcox 
529371c752dSMatthew Wilcox /**
530c5beb07eSMatthew Wilcox  * xa_cmpxchg() - Conditionally replace an entry in the XArray.
531c5beb07eSMatthew Wilcox  * @xa: XArray.
532c5beb07eSMatthew Wilcox  * @index: Index into array.
533c5beb07eSMatthew Wilcox  * @old: Old value to test against.
534c5beb07eSMatthew Wilcox  * @entry: New value to place in array.
535c5beb07eSMatthew Wilcox  * @gfp: Memory allocation flags.
536c5beb07eSMatthew Wilcox  *
537c5beb07eSMatthew Wilcox  * If the entry at @index is the same as @old, replace it with @entry.
538c5beb07eSMatthew Wilcox  * If the return value is equal to @old, then the exchange was successful.
539c5beb07eSMatthew Wilcox  *
540c5beb07eSMatthew Wilcox  * Context: Any context.  Takes and releases the xa_lock.  May sleep
541c5beb07eSMatthew Wilcox  * if the @gfp flags permit.
542c5beb07eSMatthew Wilcox  * Return: The old value at this index or xa_err() if an error happened.
543c5beb07eSMatthew Wilcox  */
544c5beb07eSMatthew Wilcox static inline void *xa_cmpxchg(struct xarray *xa, unsigned long index,
545c5beb07eSMatthew Wilcox 			void *old, void *entry, gfp_t gfp)
546c5beb07eSMatthew Wilcox {
547c5beb07eSMatthew Wilcox 	void *curr;
548c5beb07eSMatthew Wilcox 
549c5beb07eSMatthew Wilcox 	xa_lock(xa);
550c5beb07eSMatthew Wilcox 	curr = __xa_cmpxchg(xa, index, old, entry, gfp);
551c5beb07eSMatthew Wilcox 	xa_unlock(xa);
552c5beb07eSMatthew Wilcox 
553c5beb07eSMatthew Wilcox 	return curr;
554c5beb07eSMatthew Wilcox }
555c5beb07eSMatthew Wilcox 
556c5beb07eSMatthew Wilcox /**
557*55f3f7eaSMatthew Wilcox  * xa_cmpxchg_bh() - Conditionally replace an entry in the XArray.
558*55f3f7eaSMatthew Wilcox  * @xa: XArray.
559*55f3f7eaSMatthew Wilcox  * @index: Index into array.
560*55f3f7eaSMatthew Wilcox  * @old: Old value to test against.
561*55f3f7eaSMatthew Wilcox  * @entry: New value to place in array.
562*55f3f7eaSMatthew Wilcox  * @gfp: Memory allocation flags.
563*55f3f7eaSMatthew Wilcox  *
564*55f3f7eaSMatthew Wilcox  * This function is like calling xa_cmpxchg() except it disables softirqs
565*55f3f7eaSMatthew Wilcox  * while holding the array lock.
566*55f3f7eaSMatthew Wilcox  *
567*55f3f7eaSMatthew Wilcox  * Context: Any context.  Takes and releases the xa_lock while
568*55f3f7eaSMatthew Wilcox  * disabling softirqs.  May sleep if the @gfp flags permit.
569*55f3f7eaSMatthew Wilcox  * Return: The old value at this index or xa_err() if an error happened.
570*55f3f7eaSMatthew Wilcox  */
571*55f3f7eaSMatthew Wilcox static inline void *xa_cmpxchg_bh(struct xarray *xa, unsigned long index,
572*55f3f7eaSMatthew Wilcox 			void *old, void *entry, gfp_t gfp)
573*55f3f7eaSMatthew Wilcox {
574*55f3f7eaSMatthew Wilcox 	void *curr;
575*55f3f7eaSMatthew Wilcox 
576*55f3f7eaSMatthew Wilcox 	xa_lock_bh(xa);
577*55f3f7eaSMatthew Wilcox 	curr = __xa_cmpxchg(xa, index, old, entry, gfp);
578*55f3f7eaSMatthew Wilcox 	xa_unlock_bh(xa);
579*55f3f7eaSMatthew Wilcox 
580*55f3f7eaSMatthew Wilcox 	return curr;
581*55f3f7eaSMatthew Wilcox }
582*55f3f7eaSMatthew Wilcox 
583*55f3f7eaSMatthew Wilcox /**
584*55f3f7eaSMatthew Wilcox  * xa_cmpxchg_irq() - Conditionally replace an entry in the XArray.
585*55f3f7eaSMatthew Wilcox  * @xa: XArray.
586*55f3f7eaSMatthew Wilcox  * @index: Index into array.
587*55f3f7eaSMatthew Wilcox  * @old: Old value to test against.
588*55f3f7eaSMatthew Wilcox  * @entry: New value to place in array.
589*55f3f7eaSMatthew Wilcox  * @gfp: Memory allocation flags.
590*55f3f7eaSMatthew Wilcox  *
591*55f3f7eaSMatthew Wilcox  * This function is like calling xa_cmpxchg() except it disables interrupts
592*55f3f7eaSMatthew Wilcox  * while holding the array lock.
593*55f3f7eaSMatthew Wilcox  *
594*55f3f7eaSMatthew Wilcox  * Context: Process context.  Takes and releases the xa_lock while
595*55f3f7eaSMatthew Wilcox  * disabling interrupts.  May sleep if the @gfp flags permit.
596*55f3f7eaSMatthew Wilcox  * Return: The old value at this index or xa_err() if an error happened.
597*55f3f7eaSMatthew Wilcox  */
598*55f3f7eaSMatthew Wilcox static inline void *xa_cmpxchg_irq(struct xarray *xa, unsigned long index,
599*55f3f7eaSMatthew Wilcox 			void *old, void *entry, gfp_t gfp)
600*55f3f7eaSMatthew Wilcox {
601*55f3f7eaSMatthew Wilcox 	void *curr;
602*55f3f7eaSMatthew Wilcox 
603*55f3f7eaSMatthew Wilcox 	xa_lock_irq(xa);
604*55f3f7eaSMatthew Wilcox 	curr = __xa_cmpxchg(xa, index, old, entry, gfp);
605*55f3f7eaSMatthew Wilcox 	xa_unlock_irq(xa);
606*55f3f7eaSMatthew Wilcox 
607*55f3f7eaSMatthew Wilcox 	return curr;
608*55f3f7eaSMatthew Wilcox }
609*55f3f7eaSMatthew Wilcox 
610*55f3f7eaSMatthew Wilcox /**
611c5beb07eSMatthew Wilcox  * xa_insert() - Store this entry in the XArray unless another entry is
612c5beb07eSMatthew Wilcox  *			already present.
613c5beb07eSMatthew Wilcox  * @xa: XArray.
614c5beb07eSMatthew Wilcox  * @index: Index into array.
615c5beb07eSMatthew Wilcox  * @entry: New entry.
616c5beb07eSMatthew Wilcox  * @gfp: Memory allocation flags.
617c5beb07eSMatthew Wilcox  *
618c5beb07eSMatthew Wilcox  * If you would rather see the existing entry in the array, use xa_cmpxchg().
619c5beb07eSMatthew Wilcox  * This function is for users who don't care what the entry is, only that
620c5beb07eSMatthew Wilcox  * one is present.
621c5beb07eSMatthew Wilcox  *
622c5beb07eSMatthew Wilcox  * Context: Process context.  Takes and releases the xa_lock.
623c5beb07eSMatthew Wilcox  *	    May sleep if the @gfp flags permit.
624c5beb07eSMatthew Wilcox  * Return: 0 if the store succeeded.  -EEXIST if another entry was present.
625c5beb07eSMatthew Wilcox  * -ENOMEM if memory could not be allocated.
626c5beb07eSMatthew Wilcox  */
627c5beb07eSMatthew Wilcox static inline int xa_insert(struct xarray *xa, unsigned long index,
628c5beb07eSMatthew Wilcox 		void *entry, gfp_t gfp)
629c5beb07eSMatthew Wilcox {
630c5beb07eSMatthew Wilcox 	void *curr = xa_cmpxchg(xa, index, NULL, entry, gfp);
631c5beb07eSMatthew Wilcox 	if (!curr)
632c5beb07eSMatthew Wilcox 		return 0;
633c5beb07eSMatthew Wilcox 	if (xa_is_err(curr))
634c5beb07eSMatthew Wilcox 		return xa_err(curr);
635c5beb07eSMatthew Wilcox 	return -EEXIST;
636c5beb07eSMatthew Wilcox }
637c5beb07eSMatthew Wilcox 
638c5beb07eSMatthew Wilcox /**
639371c752dSMatthew Wilcox  * xa_alloc() - Find somewhere to store this entry in the XArray.
640371c752dSMatthew Wilcox  * @xa: XArray.
641371c752dSMatthew Wilcox  * @id: Pointer to ID.
642371c752dSMatthew Wilcox  * @max: Maximum ID to allocate (inclusive).
643371c752dSMatthew Wilcox  * @entry: New entry.
644371c752dSMatthew Wilcox  * @gfp: Memory allocation flags.
645371c752dSMatthew Wilcox  *
646371c752dSMatthew Wilcox  * Allocates an unused ID in the range specified by @id and @max.
647371c752dSMatthew Wilcox  * Updates the @id pointer with the index, then stores the entry at that
648371c752dSMatthew Wilcox  * index.  A concurrent lookup will not see an uninitialised @id.
649371c752dSMatthew Wilcox  *
650371c752dSMatthew Wilcox  * Context: Process context.  Takes and releases the xa_lock.  May sleep if
651371c752dSMatthew Wilcox  * the @gfp flags permit.
652371c752dSMatthew Wilcox  * Return: 0 on success, -ENOMEM if memory allocation fails or -ENOSPC if
653371c752dSMatthew Wilcox  * there is no more space in the XArray.
654371c752dSMatthew Wilcox  */
655371c752dSMatthew Wilcox static inline int xa_alloc(struct xarray *xa, u32 *id, u32 max, void *entry,
656371c752dSMatthew Wilcox 		gfp_t gfp)
657371c752dSMatthew Wilcox {
658371c752dSMatthew Wilcox 	int err;
659371c752dSMatthew Wilcox 
660371c752dSMatthew Wilcox 	xa_lock(xa);
661371c752dSMatthew Wilcox 	err = __xa_alloc(xa, id, max, entry, gfp);
662371c752dSMatthew Wilcox 	xa_unlock(xa);
663371c752dSMatthew Wilcox 
664371c752dSMatthew Wilcox 	return err;
665371c752dSMatthew Wilcox }
666371c752dSMatthew Wilcox 
667371c752dSMatthew Wilcox /**
668371c752dSMatthew Wilcox  * xa_alloc_bh() - Find somewhere to store this entry in the XArray.
669371c752dSMatthew Wilcox  * @xa: XArray.
670371c752dSMatthew Wilcox  * @id: Pointer to ID.
671371c752dSMatthew Wilcox  * @max: Maximum ID to allocate (inclusive).
672371c752dSMatthew Wilcox  * @entry: New entry.
673371c752dSMatthew Wilcox  * @gfp: Memory allocation flags.
674371c752dSMatthew Wilcox  *
675371c752dSMatthew Wilcox  * Allocates an unused ID in the range specified by @id and @max.
676371c752dSMatthew Wilcox  * Updates the @id pointer with the index, then stores the entry at that
677371c752dSMatthew Wilcox  * index.  A concurrent lookup will not see an uninitialised @id.
678371c752dSMatthew Wilcox  *
679804dfaf0SMatthew Wilcox  * Context: Any context.  Takes and releases the xa_lock while
680371c752dSMatthew Wilcox  * disabling softirqs.  May sleep if the @gfp flags permit.
681371c752dSMatthew Wilcox  * Return: 0 on success, -ENOMEM if memory allocation fails or -ENOSPC if
682371c752dSMatthew Wilcox  * there is no more space in the XArray.
683371c752dSMatthew Wilcox  */
684371c752dSMatthew Wilcox static inline int xa_alloc_bh(struct xarray *xa, u32 *id, u32 max, void *entry,
685371c752dSMatthew Wilcox 		gfp_t gfp)
686371c752dSMatthew Wilcox {
687371c752dSMatthew Wilcox 	int err;
688371c752dSMatthew Wilcox 
689371c752dSMatthew Wilcox 	xa_lock_bh(xa);
690371c752dSMatthew Wilcox 	err = __xa_alloc(xa, id, max, entry, gfp);
691371c752dSMatthew Wilcox 	xa_unlock_bh(xa);
692371c752dSMatthew Wilcox 
693371c752dSMatthew Wilcox 	return err;
694371c752dSMatthew Wilcox }
695371c752dSMatthew Wilcox 
696371c752dSMatthew Wilcox /**
697371c752dSMatthew Wilcox  * xa_alloc_irq() - Find somewhere to store this entry in the XArray.
698371c752dSMatthew Wilcox  * @xa: XArray.
699371c752dSMatthew Wilcox  * @id: Pointer to ID.
700371c752dSMatthew Wilcox  * @max: Maximum ID to allocate (inclusive).
701371c752dSMatthew Wilcox  * @entry: New entry.
702371c752dSMatthew Wilcox  * @gfp: Memory allocation flags.
703371c752dSMatthew Wilcox  *
704371c752dSMatthew Wilcox  * Allocates an unused ID in the range specified by @id and @max.
705371c752dSMatthew Wilcox  * Updates the @id pointer with the index, then stores the entry at that
706371c752dSMatthew Wilcox  * index.  A concurrent lookup will not see an uninitialised @id.
707371c752dSMatthew Wilcox  *
708371c752dSMatthew Wilcox  * Context: Process context.  Takes and releases the xa_lock while
709371c752dSMatthew Wilcox  * disabling interrupts.  May sleep if the @gfp flags permit.
710371c752dSMatthew Wilcox  * Return: 0 on success, -ENOMEM if memory allocation fails or -ENOSPC if
711371c752dSMatthew Wilcox  * there is no more space in the XArray.
712371c752dSMatthew Wilcox  */
713371c752dSMatthew Wilcox static inline int xa_alloc_irq(struct xarray *xa, u32 *id, u32 max, void *entry,
714371c752dSMatthew Wilcox 		gfp_t gfp)
715371c752dSMatthew Wilcox {
716371c752dSMatthew Wilcox 	int err;
717371c752dSMatthew Wilcox 
718371c752dSMatthew Wilcox 	xa_lock_irq(xa);
719371c752dSMatthew Wilcox 	err = __xa_alloc(xa, id, max, entry, gfp);
720371c752dSMatthew Wilcox 	xa_unlock_irq(xa);
721371c752dSMatthew Wilcox 
722371c752dSMatthew Wilcox 	return err;
723371c752dSMatthew Wilcox }
724371c752dSMatthew Wilcox 
7254c0608f4SMatthew Wilcox /**
7264c0608f4SMatthew Wilcox  * xa_reserve() - Reserve this index in the XArray.
7274c0608f4SMatthew Wilcox  * @xa: XArray.
7284c0608f4SMatthew Wilcox  * @index: Index into array.
7294c0608f4SMatthew Wilcox  * @gfp: Memory allocation flags.
7304c0608f4SMatthew Wilcox  *
7314c0608f4SMatthew Wilcox  * Ensures there is somewhere to store an entry at @index in the array.
7324c0608f4SMatthew Wilcox  * If there is already something stored at @index, this function does
7334c0608f4SMatthew Wilcox  * nothing.  If there was nothing there, the entry is marked as reserved.
7344c0608f4SMatthew Wilcox  * Loading from a reserved entry returns a %NULL pointer.
7354c0608f4SMatthew Wilcox  *
7364c0608f4SMatthew Wilcox  * If you do not use the entry that you have reserved, call xa_release()
7374c0608f4SMatthew Wilcox  * or xa_erase() to free any unnecessary memory.
7384c0608f4SMatthew Wilcox  *
7394c0608f4SMatthew Wilcox  * Context: Any context.  Takes and releases the xa_lock.
7404c0608f4SMatthew Wilcox  * May sleep if the @gfp flags permit.
7414c0608f4SMatthew Wilcox  * Return: 0 if the reservation succeeded or -ENOMEM if it failed.
7424c0608f4SMatthew Wilcox  */
7434c0608f4SMatthew Wilcox static inline
7444c0608f4SMatthew Wilcox int xa_reserve(struct xarray *xa, unsigned long index, gfp_t gfp)
7454c0608f4SMatthew Wilcox {
7464c0608f4SMatthew Wilcox 	int ret;
7474c0608f4SMatthew Wilcox 
7484c0608f4SMatthew Wilcox 	xa_lock(xa);
7494c0608f4SMatthew Wilcox 	ret = __xa_reserve(xa, index, gfp);
7504c0608f4SMatthew Wilcox 	xa_unlock(xa);
7514c0608f4SMatthew Wilcox 
7524c0608f4SMatthew Wilcox 	return ret;
7534c0608f4SMatthew Wilcox }
7544c0608f4SMatthew Wilcox 
7554c0608f4SMatthew Wilcox /**
7564c0608f4SMatthew Wilcox  * xa_reserve_bh() - Reserve this index in the XArray.
7574c0608f4SMatthew Wilcox  * @xa: XArray.
7584c0608f4SMatthew Wilcox  * @index: Index into array.
7594c0608f4SMatthew Wilcox  * @gfp: Memory allocation flags.
7604c0608f4SMatthew Wilcox  *
7614c0608f4SMatthew Wilcox  * A softirq-disabling version of xa_reserve().
7624c0608f4SMatthew Wilcox  *
7634c0608f4SMatthew Wilcox  * Context: Any context.  Takes and releases the xa_lock while
7644c0608f4SMatthew Wilcox  * disabling softirqs.
7654c0608f4SMatthew Wilcox  * Return: 0 if the reservation succeeded or -ENOMEM if it failed.
7664c0608f4SMatthew Wilcox  */
7674c0608f4SMatthew Wilcox static inline
7684c0608f4SMatthew Wilcox int xa_reserve_bh(struct xarray *xa, unsigned long index, gfp_t gfp)
7694c0608f4SMatthew Wilcox {
7704c0608f4SMatthew Wilcox 	int ret;
7714c0608f4SMatthew Wilcox 
7724c0608f4SMatthew Wilcox 	xa_lock_bh(xa);
7734c0608f4SMatthew Wilcox 	ret = __xa_reserve(xa, index, gfp);
7744c0608f4SMatthew Wilcox 	xa_unlock_bh(xa);
7754c0608f4SMatthew Wilcox 
7764c0608f4SMatthew Wilcox 	return ret;
7774c0608f4SMatthew Wilcox }
7784c0608f4SMatthew Wilcox 
7794c0608f4SMatthew Wilcox /**
7804c0608f4SMatthew Wilcox  * xa_reserve_irq() - Reserve this index in the XArray.
7814c0608f4SMatthew Wilcox  * @xa: XArray.
7824c0608f4SMatthew Wilcox  * @index: Index into array.
7834c0608f4SMatthew Wilcox  * @gfp: Memory allocation flags.
7844c0608f4SMatthew Wilcox  *
7854c0608f4SMatthew Wilcox  * An interrupt-disabling version of xa_reserve().
7864c0608f4SMatthew Wilcox  *
7874c0608f4SMatthew Wilcox  * Context: Process context.  Takes and releases the xa_lock while
7884c0608f4SMatthew Wilcox  * disabling interrupts.
7894c0608f4SMatthew Wilcox  * Return: 0 if the reservation succeeded or -ENOMEM if it failed.
7904c0608f4SMatthew Wilcox  */
7914c0608f4SMatthew Wilcox static inline
7924c0608f4SMatthew Wilcox int xa_reserve_irq(struct xarray *xa, unsigned long index, gfp_t gfp)
7934c0608f4SMatthew Wilcox {
7944c0608f4SMatthew Wilcox 	int ret;
7954c0608f4SMatthew Wilcox 
7964c0608f4SMatthew Wilcox 	xa_lock_irq(xa);
7974c0608f4SMatthew Wilcox 	ret = __xa_reserve(xa, index, gfp);
7984c0608f4SMatthew Wilcox 	xa_unlock_irq(xa);
7994c0608f4SMatthew Wilcox 
8004c0608f4SMatthew Wilcox 	return ret;
8014c0608f4SMatthew Wilcox }
8024c0608f4SMatthew Wilcox 
803c5beb07eSMatthew Wilcox /**
804c5beb07eSMatthew Wilcox  * xa_release() - Release a reserved entry.
805c5beb07eSMatthew Wilcox  * @xa: XArray.
806c5beb07eSMatthew Wilcox  * @index: Index of entry.
807c5beb07eSMatthew Wilcox  *
808c5beb07eSMatthew Wilcox  * After calling xa_reserve(), you can call this function to release the
809c5beb07eSMatthew Wilcox  * reservation.  If the entry at @index has been stored to, this function
810c5beb07eSMatthew Wilcox  * will do nothing.
811c5beb07eSMatthew Wilcox  */
812c5beb07eSMatthew Wilcox static inline void xa_release(struct xarray *xa, unsigned long index)
813c5beb07eSMatthew Wilcox {
814c5beb07eSMatthew Wilcox 	xa_cmpxchg(xa, index, NULL, NULL, 0);
815c5beb07eSMatthew Wilcox }
816c5beb07eSMatthew Wilcox 
81702c02bf1SMatthew Wilcox /* Everything below here is the Advanced API.  Proceed with caution. */
81802c02bf1SMatthew Wilcox 
81902c02bf1SMatthew Wilcox /*
82002c02bf1SMatthew Wilcox  * The xarray is constructed out of a set of 'chunks' of pointers.  Choosing
82102c02bf1SMatthew Wilcox  * the best chunk size requires some tradeoffs.  A power of two recommends
82202c02bf1SMatthew Wilcox  * itself so that we can walk the tree based purely on shifts and masks.
82302c02bf1SMatthew Wilcox  * Generally, the larger the better; as the number of slots per level of the
82402c02bf1SMatthew Wilcox  * tree increases, the less tall the tree needs to be.  But that needs to be
82502c02bf1SMatthew Wilcox  * balanced against the memory consumption of each node.  On a 64-bit system,
82602c02bf1SMatthew Wilcox  * xa_node is currently 576 bytes, and we get 7 of them per 4kB page.  If we
82702c02bf1SMatthew Wilcox  * doubled the number of slots per node, we'd get only 3 nodes per 4kB page.
82802c02bf1SMatthew Wilcox  */
82902c02bf1SMatthew Wilcox #ifndef XA_CHUNK_SHIFT
83002c02bf1SMatthew Wilcox #define XA_CHUNK_SHIFT		(CONFIG_BASE_SMALL ? 4 : 6)
83102c02bf1SMatthew Wilcox #endif
83202c02bf1SMatthew Wilcox #define XA_CHUNK_SIZE		(1UL << XA_CHUNK_SHIFT)
83302c02bf1SMatthew Wilcox #define XA_CHUNK_MASK		(XA_CHUNK_SIZE - 1)
83401959dfeSMatthew Wilcox #define XA_MAX_MARKS		3
83501959dfeSMatthew Wilcox #define XA_MARK_LONGS		DIV_ROUND_UP(XA_CHUNK_SIZE, BITS_PER_LONG)
83601959dfeSMatthew Wilcox 
83701959dfeSMatthew Wilcox /*
83801959dfeSMatthew Wilcox  * @count is the count of every non-NULL element in the ->slots array
83901959dfeSMatthew Wilcox  * whether that is a value entry, a retry entry, a user pointer,
84001959dfeSMatthew Wilcox  * a sibling entry or a pointer to the next level of the tree.
84101959dfeSMatthew Wilcox  * @nr_values is the count of every element in ->slots which is
84201959dfeSMatthew Wilcox  * either a value entry or a sibling of a value entry.
84301959dfeSMatthew Wilcox  */
84401959dfeSMatthew Wilcox struct xa_node {
84501959dfeSMatthew Wilcox 	unsigned char	shift;		/* Bits remaining in each slot */
84601959dfeSMatthew Wilcox 	unsigned char	offset;		/* Slot offset in parent */
84701959dfeSMatthew Wilcox 	unsigned char	count;		/* Total entry count */
84801959dfeSMatthew Wilcox 	unsigned char	nr_values;	/* Value entry count */
84901959dfeSMatthew Wilcox 	struct xa_node __rcu *parent;	/* NULL at top of tree */
85001959dfeSMatthew Wilcox 	struct xarray	*array;		/* The array we belong to */
85101959dfeSMatthew Wilcox 	union {
85201959dfeSMatthew Wilcox 		struct list_head private_list;	/* For tree user */
85301959dfeSMatthew Wilcox 		struct rcu_head	rcu_head;	/* Used when freeing node */
85401959dfeSMatthew Wilcox 	};
85501959dfeSMatthew Wilcox 	void __rcu	*slots[XA_CHUNK_SIZE];
85601959dfeSMatthew Wilcox 	union {
85701959dfeSMatthew Wilcox 		unsigned long	tags[XA_MAX_MARKS][XA_MARK_LONGS];
85801959dfeSMatthew Wilcox 		unsigned long	marks[XA_MAX_MARKS][XA_MARK_LONGS];
85901959dfeSMatthew Wilcox 	};
86001959dfeSMatthew Wilcox };
86102c02bf1SMatthew Wilcox 
862ad3d6c72SMatthew Wilcox void xa_dump(const struct xarray *);
863ad3d6c72SMatthew Wilcox void xa_dump_node(const struct xa_node *);
864ad3d6c72SMatthew Wilcox 
865ad3d6c72SMatthew Wilcox #ifdef XA_DEBUG
866ad3d6c72SMatthew Wilcox #define XA_BUG_ON(xa, x) do {					\
867ad3d6c72SMatthew Wilcox 		if (x) {					\
868ad3d6c72SMatthew Wilcox 			xa_dump(xa);				\
869ad3d6c72SMatthew Wilcox 			BUG();					\
870ad3d6c72SMatthew Wilcox 		}						\
871ad3d6c72SMatthew Wilcox 	} while (0)
872ad3d6c72SMatthew Wilcox #define XA_NODE_BUG_ON(node, x) do {				\
873ad3d6c72SMatthew Wilcox 		if (x) {					\
874ad3d6c72SMatthew Wilcox 			if (node) xa_dump_node(node);		\
875ad3d6c72SMatthew Wilcox 			BUG();					\
876ad3d6c72SMatthew Wilcox 		}						\
877ad3d6c72SMatthew Wilcox 	} while (0)
878ad3d6c72SMatthew Wilcox #else
879ad3d6c72SMatthew Wilcox #define XA_BUG_ON(xa, x)	do { } while (0)
880ad3d6c72SMatthew Wilcox #define XA_NODE_BUG_ON(node, x)	do { } while (0)
881ad3d6c72SMatthew Wilcox #endif
882ad3d6c72SMatthew Wilcox 
883ad3d6c72SMatthew Wilcox /* Private */
884ad3d6c72SMatthew Wilcox static inline void *xa_head(const struct xarray *xa)
885ad3d6c72SMatthew Wilcox {
886ad3d6c72SMatthew Wilcox 	return rcu_dereference_check(xa->xa_head,
887ad3d6c72SMatthew Wilcox 						lockdep_is_held(&xa->xa_lock));
888ad3d6c72SMatthew Wilcox }
889ad3d6c72SMatthew Wilcox 
890ad3d6c72SMatthew Wilcox /* Private */
891ad3d6c72SMatthew Wilcox static inline void *xa_head_locked(const struct xarray *xa)
892ad3d6c72SMatthew Wilcox {
893ad3d6c72SMatthew Wilcox 	return rcu_dereference_protected(xa->xa_head,
894ad3d6c72SMatthew Wilcox 						lockdep_is_held(&xa->xa_lock));
895ad3d6c72SMatthew Wilcox }
896ad3d6c72SMatthew Wilcox 
897ad3d6c72SMatthew Wilcox /* Private */
898ad3d6c72SMatthew Wilcox static inline void *xa_entry(const struct xarray *xa,
899ad3d6c72SMatthew Wilcox 				const struct xa_node *node, unsigned int offset)
900ad3d6c72SMatthew Wilcox {
901ad3d6c72SMatthew Wilcox 	XA_NODE_BUG_ON(node, offset >= XA_CHUNK_SIZE);
902ad3d6c72SMatthew Wilcox 	return rcu_dereference_check(node->slots[offset],
903ad3d6c72SMatthew Wilcox 						lockdep_is_held(&xa->xa_lock));
904ad3d6c72SMatthew Wilcox }
905ad3d6c72SMatthew Wilcox 
906ad3d6c72SMatthew Wilcox /* Private */
907ad3d6c72SMatthew Wilcox static inline void *xa_entry_locked(const struct xarray *xa,
908ad3d6c72SMatthew Wilcox 				const struct xa_node *node, unsigned int offset)
909ad3d6c72SMatthew Wilcox {
910ad3d6c72SMatthew Wilcox 	XA_NODE_BUG_ON(node, offset >= XA_CHUNK_SIZE);
911ad3d6c72SMatthew Wilcox 	return rcu_dereference_protected(node->slots[offset],
912ad3d6c72SMatthew Wilcox 						lockdep_is_held(&xa->xa_lock));
913ad3d6c72SMatthew Wilcox }
914ad3d6c72SMatthew Wilcox 
915ad3d6c72SMatthew Wilcox /* Private */
9169b89a035SMatthew Wilcox static inline struct xa_node *xa_parent(const struct xarray *xa,
9179b89a035SMatthew Wilcox 					const struct xa_node *node)
9189b89a035SMatthew Wilcox {
9199b89a035SMatthew Wilcox 	return rcu_dereference_check(node->parent,
9209b89a035SMatthew Wilcox 						lockdep_is_held(&xa->xa_lock));
9219b89a035SMatthew Wilcox }
9229b89a035SMatthew Wilcox 
9239b89a035SMatthew Wilcox /* Private */
9249b89a035SMatthew Wilcox static inline struct xa_node *xa_parent_locked(const struct xarray *xa,
9259b89a035SMatthew Wilcox 					const struct xa_node *node)
9269b89a035SMatthew Wilcox {
9279b89a035SMatthew Wilcox 	return rcu_dereference_protected(node->parent,
9289b89a035SMatthew Wilcox 						lockdep_is_held(&xa->xa_lock));
9299b89a035SMatthew Wilcox }
9309b89a035SMatthew Wilcox 
9319b89a035SMatthew Wilcox /* Private */
93258d6ea30SMatthew Wilcox static inline void *xa_mk_node(const struct xa_node *node)
93358d6ea30SMatthew Wilcox {
93458d6ea30SMatthew Wilcox 	return (void *)((unsigned long)node | 2);
93558d6ea30SMatthew Wilcox }
93658d6ea30SMatthew Wilcox 
93758d6ea30SMatthew Wilcox /* Private */
938ad3d6c72SMatthew Wilcox static inline struct xa_node *xa_to_node(const void *entry)
939ad3d6c72SMatthew Wilcox {
940ad3d6c72SMatthew Wilcox 	return (struct xa_node *)((unsigned long)entry - 2);
941ad3d6c72SMatthew Wilcox }
942ad3d6c72SMatthew Wilcox 
94302c02bf1SMatthew Wilcox /* Private */
94402c02bf1SMatthew Wilcox static inline bool xa_is_node(const void *entry)
94502c02bf1SMatthew Wilcox {
94602c02bf1SMatthew Wilcox 	return xa_is_internal(entry) && (unsigned long)entry > 4096;
94702c02bf1SMatthew Wilcox }
94802c02bf1SMatthew Wilcox 
94902c02bf1SMatthew Wilcox /* Private */
95002c02bf1SMatthew Wilcox static inline void *xa_mk_sibling(unsigned int offset)
95102c02bf1SMatthew Wilcox {
95202c02bf1SMatthew Wilcox 	return xa_mk_internal(offset);
95302c02bf1SMatthew Wilcox }
95402c02bf1SMatthew Wilcox 
95502c02bf1SMatthew Wilcox /* Private */
95602c02bf1SMatthew Wilcox static inline unsigned long xa_to_sibling(const void *entry)
95702c02bf1SMatthew Wilcox {
95802c02bf1SMatthew Wilcox 	return xa_to_internal(entry);
95902c02bf1SMatthew Wilcox }
96002c02bf1SMatthew Wilcox 
96102c02bf1SMatthew Wilcox /**
96202c02bf1SMatthew Wilcox  * xa_is_sibling() - Is the entry a sibling entry?
96302c02bf1SMatthew Wilcox  * @entry: Entry retrieved from the XArray
96402c02bf1SMatthew Wilcox  *
96502c02bf1SMatthew Wilcox  * Return: %true if the entry is a sibling entry.
96602c02bf1SMatthew Wilcox  */
96702c02bf1SMatthew Wilcox static inline bool xa_is_sibling(const void *entry)
96802c02bf1SMatthew Wilcox {
96902c02bf1SMatthew Wilcox 	return IS_ENABLED(CONFIG_XARRAY_MULTI) && xa_is_internal(entry) &&
97002c02bf1SMatthew Wilcox 		(entry < xa_mk_sibling(XA_CHUNK_SIZE - 1));
97102c02bf1SMatthew Wilcox }
97202c02bf1SMatthew Wilcox 
9739f14d4f1SMatthew Wilcox #define XA_ZERO_ENTRY		xa_mk_internal(256)
9749f14d4f1SMatthew Wilcox #define XA_RETRY_ENTRY		xa_mk_internal(257)
9759f14d4f1SMatthew Wilcox 
9769f14d4f1SMatthew Wilcox /**
9779f14d4f1SMatthew Wilcox  * xa_is_zero() - Is the entry a zero entry?
9789f14d4f1SMatthew Wilcox  * @entry: Entry retrieved from the XArray
9799f14d4f1SMatthew Wilcox  *
9809f14d4f1SMatthew Wilcox  * Return: %true if the entry is a zero entry.
9819f14d4f1SMatthew Wilcox  */
9829f14d4f1SMatthew Wilcox static inline bool xa_is_zero(const void *entry)
9839f14d4f1SMatthew Wilcox {
9849f14d4f1SMatthew Wilcox 	return unlikely(entry == XA_ZERO_ENTRY);
9859f14d4f1SMatthew Wilcox }
98602c02bf1SMatthew Wilcox 
987ad3d6c72SMatthew Wilcox /**
988ad3d6c72SMatthew Wilcox  * xa_is_retry() - Is the entry a retry entry?
989ad3d6c72SMatthew Wilcox  * @entry: Entry retrieved from the XArray
990ad3d6c72SMatthew Wilcox  *
991ad3d6c72SMatthew Wilcox  * Return: %true if the entry is a retry entry.
992ad3d6c72SMatthew Wilcox  */
993ad3d6c72SMatthew Wilcox static inline bool xa_is_retry(const void *entry)
994ad3d6c72SMatthew Wilcox {
995ad3d6c72SMatthew Wilcox 	return unlikely(entry == XA_RETRY_ENTRY);
996ad3d6c72SMatthew Wilcox }
997ad3d6c72SMatthew Wilcox 
998ad3d6c72SMatthew Wilcox /**
999ad3d6c72SMatthew Wilcox  * typedef xa_update_node_t - A callback function from the XArray.
1000ad3d6c72SMatthew Wilcox  * @node: The node which is being processed
1001ad3d6c72SMatthew Wilcox  *
1002ad3d6c72SMatthew Wilcox  * This function is called every time the XArray updates the count of
1003ad3d6c72SMatthew Wilcox  * present and value entries in a node.  It allows advanced users to
1004ad3d6c72SMatthew Wilcox  * maintain the private_list in the node.
1005ad3d6c72SMatthew Wilcox  *
1006ad3d6c72SMatthew Wilcox  * Context: The xa_lock is held and interrupts may be disabled.
1007ad3d6c72SMatthew Wilcox  *	    Implementations should not drop the xa_lock, nor re-enable
1008ad3d6c72SMatthew Wilcox  *	    interrupts.
1009ad3d6c72SMatthew Wilcox  */
1010ad3d6c72SMatthew Wilcox typedef void (*xa_update_node_t)(struct xa_node *node);
1011ad3d6c72SMatthew Wilcox 
1012ad3d6c72SMatthew Wilcox /*
1013ad3d6c72SMatthew Wilcox  * The xa_state is opaque to its users.  It contains various different pieces
1014ad3d6c72SMatthew Wilcox  * of state involved in the current operation on the XArray.  It should be
1015ad3d6c72SMatthew Wilcox  * declared on the stack and passed between the various internal routines.
1016ad3d6c72SMatthew Wilcox  * The various elements in it should not be accessed directly, but only
1017ad3d6c72SMatthew Wilcox  * through the provided accessor functions.  The below documentation is for
1018ad3d6c72SMatthew Wilcox  * the benefit of those working on the code, not for users of the XArray.
1019ad3d6c72SMatthew Wilcox  *
1020ad3d6c72SMatthew Wilcox  * @xa_node usually points to the xa_node containing the slot we're operating
1021ad3d6c72SMatthew Wilcox  * on (and @xa_offset is the offset in the slots array).  If there is a
1022ad3d6c72SMatthew Wilcox  * single entry in the array at index 0, there are no allocated xa_nodes to
1023ad3d6c72SMatthew Wilcox  * point to, and so we store %NULL in @xa_node.  @xa_node is set to
1024ad3d6c72SMatthew Wilcox  * the value %XAS_RESTART if the xa_state is not walked to the correct
1025ad3d6c72SMatthew Wilcox  * position in the tree of nodes for this operation.  If an error occurs
1026ad3d6c72SMatthew Wilcox  * during an operation, it is set to an %XAS_ERROR value.  If we run off the
1027ad3d6c72SMatthew Wilcox  * end of the allocated nodes, it is set to %XAS_BOUNDS.
1028ad3d6c72SMatthew Wilcox  */
1029ad3d6c72SMatthew Wilcox struct xa_state {
1030ad3d6c72SMatthew Wilcox 	struct xarray *xa;
1031ad3d6c72SMatthew Wilcox 	unsigned long xa_index;
1032ad3d6c72SMatthew Wilcox 	unsigned char xa_shift;
1033ad3d6c72SMatthew Wilcox 	unsigned char xa_sibs;
1034ad3d6c72SMatthew Wilcox 	unsigned char xa_offset;
1035ad3d6c72SMatthew Wilcox 	unsigned char xa_pad;		/* Helps gcc generate better code */
1036ad3d6c72SMatthew Wilcox 	struct xa_node *xa_node;
1037ad3d6c72SMatthew Wilcox 	struct xa_node *xa_alloc;
1038ad3d6c72SMatthew Wilcox 	xa_update_node_t xa_update;
1039ad3d6c72SMatthew Wilcox };
1040ad3d6c72SMatthew Wilcox 
1041ad3d6c72SMatthew Wilcox /*
1042ad3d6c72SMatthew Wilcox  * We encode errnos in the xas->xa_node.  If an error has happened, we need to
1043ad3d6c72SMatthew Wilcox  * drop the lock to fix it, and once we've done so the xa_state is invalid.
1044ad3d6c72SMatthew Wilcox  */
1045ad3d6c72SMatthew Wilcox #define XA_ERROR(errno) ((struct xa_node *)(((unsigned long)errno << 2) | 2UL))
1046ad3d6c72SMatthew Wilcox #define XAS_BOUNDS	((struct xa_node *)1UL)
1047ad3d6c72SMatthew Wilcox #define XAS_RESTART	((struct xa_node *)3UL)
1048ad3d6c72SMatthew Wilcox 
1049ad3d6c72SMatthew Wilcox #define __XA_STATE(array, index, shift, sibs)  {	\
1050ad3d6c72SMatthew Wilcox 	.xa = array,					\
1051ad3d6c72SMatthew Wilcox 	.xa_index = index,				\
1052ad3d6c72SMatthew Wilcox 	.xa_shift = shift,				\
1053ad3d6c72SMatthew Wilcox 	.xa_sibs = sibs,				\
1054ad3d6c72SMatthew Wilcox 	.xa_offset = 0,					\
1055ad3d6c72SMatthew Wilcox 	.xa_pad = 0,					\
1056ad3d6c72SMatthew Wilcox 	.xa_node = XAS_RESTART,				\
1057ad3d6c72SMatthew Wilcox 	.xa_alloc = NULL,				\
1058ad3d6c72SMatthew Wilcox 	.xa_update = NULL				\
1059ad3d6c72SMatthew Wilcox }
1060ad3d6c72SMatthew Wilcox 
1061ad3d6c72SMatthew Wilcox /**
1062ad3d6c72SMatthew Wilcox  * XA_STATE() - Declare an XArray operation state.
1063ad3d6c72SMatthew Wilcox  * @name: Name of this operation state (usually xas).
1064ad3d6c72SMatthew Wilcox  * @array: Array to operate on.
1065ad3d6c72SMatthew Wilcox  * @index: Initial index of interest.
1066ad3d6c72SMatthew Wilcox  *
1067ad3d6c72SMatthew Wilcox  * Declare and initialise an xa_state on the stack.
1068ad3d6c72SMatthew Wilcox  */
1069ad3d6c72SMatthew Wilcox #define XA_STATE(name, array, index)				\
1070ad3d6c72SMatthew Wilcox 	struct xa_state name = __XA_STATE(array, index, 0, 0)
1071ad3d6c72SMatthew Wilcox 
1072ad3d6c72SMatthew Wilcox /**
1073ad3d6c72SMatthew Wilcox  * XA_STATE_ORDER() - Declare an XArray operation state.
1074ad3d6c72SMatthew Wilcox  * @name: Name of this operation state (usually xas).
1075ad3d6c72SMatthew Wilcox  * @array: Array to operate on.
1076ad3d6c72SMatthew Wilcox  * @index: Initial index of interest.
1077ad3d6c72SMatthew Wilcox  * @order: Order of entry.
1078ad3d6c72SMatthew Wilcox  *
1079ad3d6c72SMatthew Wilcox  * Declare and initialise an xa_state on the stack.  This variant of
1080ad3d6c72SMatthew Wilcox  * XA_STATE() allows you to specify the 'order' of the element you
1081ad3d6c72SMatthew Wilcox  * want to operate on.`
1082ad3d6c72SMatthew Wilcox  */
1083ad3d6c72SMatthew Wilcox #define XA_STATE_ORDER(name, array, index, order)		\
1084ad3d6c72SMatthew Wilcox 	struct xa_state name = __XA_STATE(array,		\
1085ad3d6c72SMatthew Wilcox 			(index >> order) << order,		\
1086ad3d6c72SMatthew Wilcox 			order - (order % XA_CHUNK_SHIFT),	\
1087ad3d6c72SMatthew Wilcox 			(1U << (order % XA_CHUNK_SHIFT)) - 1)
1088ad3d6c72SMatthew Wilcox 
1089ad3d6c72SMatthew Wilcox #define xas_marked(xas, mark)	xa_marked((xas)->xa, (mark))
1090ad3d6c72SMatthew Wilcox #define xas_trylock(xas)	xa_trylock((xas)->xa)
1091ad3d6c72SMatthew Wilcox #define xas_lock(xas)		xa_lock((xas)->xa)
1092ad3d6c72SMatthew Wilcox #define xas_unlock(xas)		xa_unlock((xas)->xa)
1093ad3d6c72SMatthew Wilcox #define xas_lock_bh(xas)	xa_lock_bh((xas)->xa)
1094ad3d6c72SMatthew Wilcox #define xas_unlock_bh(xas)	xa_unlock_bh((xas)->xa)
1095ad3d6c72SMatthew Wilcox #define xas_lock_irq(xas)	xa_lock_irq((xas)->xa)
1096ad3d6c72SMatthew Wilcox #define xas_unlock_irq(xas)	xa_unlock_irq((xas)->xa)
1097ad3d6c72SMatthew Wilcox #define xas_lock_irqsave(xas, flags) \
1098ad3d6c72SMatthew Wilcox 				xa_lock_irqsave((xas)->xa, flags)
1099ad3d6c72SMatthew Wilcox #define xas_unlock_irqrestore(xas, flags) \
1100ad3d6c72SMatthew Wilcox 				xa_unlock_irqrestore((xas)->xa, flags)
1101ad3d6c72SMatthew Wilcox 
1102ad3d6c72SMatthew Wilcox /**
1103ad3d6c72SMatthew Wilcox  * xas_error() - Return an errno stored in the xa_state.
1104ad3d6c72SMatthew Wilcox  * @xas: XArray operation state.
1105ad3d6c72SMatthew Wilcox  *
1106ad3d6c72SMatthew Wilcox  * Return: 0 if no error has been noted.  A negative errno if one has.
1107ad3d6c72SMatthew Wilcox  */
1108ad3d6c72SMatthew Wilcox static inline int xas_error(const struct xa_state *xas)
1109ad3d6c72SMatthew Wilcox {
1110ad3d6c72SMatthew Wilcox 	return xa_err(xas->xa_node);
1111ad3d6c72SMatthew Wilcox }
1112ad3d6c72SMatthew Wilcox 
1113ad3d6c72SMatthew Wilcox /**
1114ad3d6c72SMatthew Wilcox  * xas_set_err() - Note an error in the xa_state.
1115ad3d6c72SMatthew Wilcox  * @xas: XArray operation state.
1116ad3d6c72SMatthew Wilcox  * @err: Negative error number.
1117ad3d6c72SMatthew Wilcox  *
1118ad3d6c72SMatthew Wilcox  * Only call this function with a negative @err; zero or positive errors
1119ad3d6c72SMatthew Wilcox  * will probably not behave the way you think they should.  If you want
1120ad3d6c72SMatthew Wilcox  * to clear the error from an xa_state, use xas_reset().
1121ad3d6c72SMatthew Wilcox  */
1122ad3d6c72SMatthew Wilcox static inline void xas_set_err(struct xa_state *xas, long err)
1123ad3d6c72SMatthew Wilcox {
1124ad3d6c72SMatthew Wilcox 	xas->xa_node = XA_ERROR(err);
1125ad3d6c72SMatthew Wilcox }
1126ad3d6c72SMatthew Wilcox 
1127ad3d6c72SMatthew Wilcox /**
1128ad3d6c72SMatthew Wilcox  * xas_invalid() - Is the xas in a retry or error state?
1129ad3d6c72SMatthew Wilcox  * @xas: XArray operation state.
1130ad3d6c72SMatthew Wilcox  *
1131ad3d6c72SMatthew Wilcox  * Return: %true if the xas cannot be used for operations.
1132ad3d6c72SMatthew Wilcox  */
1133ad3d6c72SMatthew Wilcox static inline bool xas_invalid(const struct xa_state *xas)
1134ad3d6c72SMatthew Wilcox {
1135ad3d6c72SMatthew Wilcox 	return (unsigned long)xas->xa_node & 3;
1136ad3d6c72SMatthew Wilcox }
1137ad3d6c72SMatthew Wilcox 
1138ad3d6c72SMatthew Wilcox /**
1139ad3d6c72SMatthew Wilcox  * xas_valid() - Is the xas a valid cursor into the array?
1140ad3d6c72SMatthew Wilcox  * @xas: XArray operation state.
1141ad3d6c72SMatthew Wilcox  *
1142ad3d6c72SMatthew Wilcox  * Return: %true if the xas can be used for operations.
1143ad3d6c72SMatthew Wilcox  */
1144ad3d6c72SMatthew Wilcox static inline bool xas_valid(const struct xa_state *xas)
1145ad3d6c72SMatthew Wilcox {
1146ad3d6c72SMatthew Wilcox 	return !xas_invalid(xas);
1147ad3d6c72SMatthew Wilcox }
1148ad3d6c72SMatthew Wilcox 
11492264f513SMatthew Wilcox /**
11502264f513SMatthew Wilcox  * xas_is_node() - Does the xas point to a node?
11512264f513SMatthew Wilcox  * @xas: XArray operation state.
11522264f513SMatthew Wilcox  *
11532264f513SMatthew Wilcox  * Return: %true if the xas currently references a node.
11542264f513SMatthew Wilcox  */
11552264f513SMatthew Wilcox static inline bool xas_is_node(const struct xa_state *xas)
11562264f513SMatthew Wilcox {
11572264f513SMatthew Wilcox 	return xas_valid(xas) && xas->xa_node;
11582264f513SMatthew Wilcox }
11592264f513SMatthew Wilcox 
11609b89a035SMatthew Wilcox /* True if the pointer is something other than a node */
11619b89a035SMatthew Wilcox static inline bool xas_not_node(struct xa_node *node)
11629b89a035SMatthew Wilcox {
11639b89a035SMatthew Wilcox 	return ((unsigned long)node & 3) || !node;
11649b89a035SMatthew Wilcox }
11659b89a035SMatthew Wilcox 
116664d3e9a9SMatthew Wilcox /* True if the node represents RESTART or an error */
116764d3e9a9SMatthew Wilcox static inline bool xas_frozen(struct xa_node *node)
116864d3e9a9SMatthew Wilcox {
116964d3e9a9SMatthew Wilcox 	return (unsigned long)node & 2;
117064d3e9a9SMatthew Wilcox }
117164d3e9a9SMatthew Wilcox 
117258d6ea30SMatthew Wilcox /* True if the node represents head-of-tree, RESTART or BOUNDS */
117358d6ea30SMatthew Wilcox static inline bool xas_top(struct xa_node *node)
117458d6ea30SMatthew Wilcox {
117558d6ea30SMatthew Wilcox 	return node <= XAS_RESTART;
117658d6ea30SMatthew Wilcox }
117758d6ea30SMatthew Wilcox 
1178ad3d6c72SMatthew Wilcox /**
1179ad3d6c72SMatthew Wilcox  * xas_reset() - Reset an XArray operation state.
1180ad3d6c72SMatthew Wilcox  * @xas: XArray operation state.
1181ad3d6c72SMatthew Wilcox  *
1182ad3d6c72SMatthew Wilcox  * Resets the error or walk state of the @xas so future walks of the
1183ad3d6c72SMatthew Wilcox  * array will start from the root.  Use this if you have dropped the
1184ad3d6c72SMatthew Wilcox  * xarray lock and want to reuse the xa_state.
1185ad3d6c72SMatthew Wilcox  *
1186ad3d6c72SMatthew Wilcox  * Context: Any context.
1187ad3d6c72SMatthew Wilcox  */
1188ad3d6c72SMatthew Wilcox static inline void xas_reset(struct xa_state *xas)
1189ad3d6c72SMatthew Wilcox {
1190ad3d6c72SMatthew Wilcox 	xas->xa_node = XAS_RESTART;
1191ad3d6c72SMatthew Wilcox }
1192ad3d6c72SMatthew Wilcox 
1193ad3d6c72SMatthew Wilcox /**
1194ad3d6c72SMatthew Wilcox  * xas_retry() - Retry the operation if appropriate.
1195ad3d6c72SMatthew Wilcox  * @xas: XArray operation state.
1196ad3d6c72SMatthew Wilcox  * @entry: Entry from xarray.
1197ad3d6c72SMatthew Wilcox  *
1198ad3d6c72SMatthew Wilcox  * The advanced functions may sometimes return an internal entry, such as
1199ad3d6c72SMatthew Wilcox  * a retry entry or a zero entry.  This function sets up the @xas to restart
1200ad3d6c72SMatthew Wilcox  * the walk from the head of the array if needed.
1201ad3d6c72SMatthew Wilcox  *
1202ad3d6c72SMatthew Wilcox  * Context: Any context.
1203ad3d6c72SMatthew Wilcox  * Return: true if the operation needs to be retried.
1204ad3d6c72SMatthew Wilcox  */
1205ad3d6c72SMatthew Wilcox static inline bool xas_retry(struct xa_state *xas, const void *entry)
1206ad3d6c72SMatthew Wilcox {
12079f14d4f1SMatthew Wilcox 	if (xa_is_zero(entry))
12089f14d4f1SMatthew Wilcox 		return true;
1209ad3d6c72SMatthew Wilcox 	if (!xa_is_retry(entry))
1210ad3d6c72SMatthew Wilcox 		return false;
1211ad3d6c72SMatthew Wilcox 	xas_reset(xas);
1212ad3d6c72SMatthew Wilcox 	return true;
1213ad3d6c72SMatthew Wilcox }
1214ad3d6c72SMatthew Wilcox 
1215ad3d6c72SMatthew Wilcox void *xas_load(struct xa_state *);
121658d6ea30SMatthew Wilcox void *xas_store(struct xa_state *, void *entry);
1217b803b428SMatthew Wilcox void *xas_find(struct xa_state *, unsigned long max);
12184e99d4e9SMatthew Wilcox void *xas_find_conflict(struct xa_state *);
1219ad3d6c72SMatthew Wilcox 
12209b89a035SMatthew Wilcox bool xas_get_mark(const struct xa_state *, xa_mark_t);
12219b89a035SMatthew Wilcox void xas_set_mark(const struct xa_state *, xa_mark_t);
12229b89a035SMatthew Wilcox void xas_clear_mark(const struct xa_state *, xa_mark_t);
1223b803b428SMatthew Wilcox void *xas_find_marked(struct xa_state *, unsigned long max, xa_mark_t);
122458d6ea30SMatthew Wilcox void xas_init_marks(const struct xa_state *);
122558d6ea30SMatthew Wilcox 
122658d6ea30SMatthew Wilcox bool xas_nomem(struct xa_state *, gfp_t);
1227b803b428SMatthew Wilcox void xas_pause(struct xa_state *);
12289b89a035SMatthew Wilcox 
12292264f513SMatthew Wilcox void xas_create_range(struct xa_state *);
12302264f513SMatthew Wilcox 
1231ad3d6c72SMatthew Wilcox /**
1232ad3d6c72SMatthew Wilcox  * xas_reload() - Refetch an entry from the xarray.
1233ad3d6c72SMatthew Wilcox  * @xas: XArray operation state.
1234ad3d6c72SMatthew Wilcox  *
1235ad3d6c72SMatthew Wilcox  * Use this function to check that a previously loaded entry still has
1236ad3d6c72SMatthew Wilcox  * the same value.  This is useful for the lockless pagecache lookup where
1237ad3d6c72SMatthew Wilcox  * we walk the array with only the RCU lock to protect us, lock the page,
1238ad3d6c72SMatthew Wilcox  * then check that the page hasn't moved since we looked it up.
1239ad3d6c72SMatthew Wilcox  *
1240ad3d6c72SMatthew Wilcox  * The caller guarantees that @xas is still valid.  If it may be in an
1241ad3d6c72SMatthew Wilcox  * error or restart state, call xas_load() instead.
1242ad3d6c72SMatthew Wilcox  *
1243ad3d6c72SMatthew Wilcox  * Return: The entry at this location in the xarray.
1244ad3d6c72SMatthew Wilcox  */
1245ad3d6c72SMatthew Wilcox static inline void *xas_reload(struct xa_state *xas)
1246ad3d6c72SMatthew Wilcox {
1247ad3d6c72SMatthew Wilcox 	struct xa_node *node = xas->xa_node;
1248ad3d6c72SMatthew Wilcox 
1249ad3d6c72SMatthew Wilcox 	if (node)
1250ad3d6c72SMatthew Wilcox 		return xa_entry(xas->xa, node, xas->xa_offset);
1251ad3d6c72SMatthew Wilcox 	return xa_head(xas->xa);
1252ad3d6c72SMatthew Wilcox }
1253ad3d6c72SMatthew Wilcox 
125458d6ea30SMatthew Wilcox /**
125558d6ea30SMatthew Wilcox  * xas_set() - Set up XArray operation state for a different index.
125658d6ea30SMatthew Wilcox  * @xas: XArray operation state.
125758d6ea30SMatthew Wilcox  * @index: New index into the XArray.
125858d6ea30SMatthew Wilcox  *
125958d6ea30SMatthew Wilcox  * Move the operation state to refer to a different index.  This will
126058d6ea30SMatthew Wilcox  * have the effect of starting a walk from the top; see xas_next()
126158d6ea30SMatthew Wilcox  * to move to an adjacent index.
126258d6ea30SMatthew Wilcox  */
126358d6ea30SMatthew Wilcox static inline void xas_set(struct xa_state *xas, unsigned long index)
126458d6ea30SMatthew Wilcox {
126558d6ea30SMatthew Wilcox 	xas->xa_index = index;
126658d6ea30SMatthew Wilcox 	xas->xa_node = XAS_RESTART;
126758d6ea30SMatthew Wilcox }
126858d6ea30SMatthew Wilcox 
126958d6ea30SMatthew Wilcox /**
127058d6ea30SMatthew Wilcox  * xas_set_order() - Set up XArray operation state for a multislot entry.
127158d6ea30SMatthew Wilcox  * @xas: XArray operation state.
127258d6ea30SMatthew Wilcox  * @index: Target of the operation.
127358d6ea30SMatthew Wilcox  * @order: Entry occupies 2^@order indices.
127458d6ea30SMatthew Wilcox  */
127558d6ea30SMatthew Wilcox static inline void xas_set_order(struct xa_state *xas, unsigned long index,
127658d6ea30SMatthew Wilcox 					unsigned int order)
127758d6ea30SMatthew Wilcox {
127858d6ea30SMatthew Wilcox #ifdef CONFIG_XARRAY_MULTI
127958d6ea30SMatthew Wilcox 	xas->xa_index = order < BITS_PER_LONG ? (index >> order) << order : 0;
128058d6ea30SMatthew Wilcox 	xas->xa_shift = order - (order % XA_CHUNK_SHIFT);
128158d6ea30SMatthew Wilcox 	xas->xa_sibs = (1 << (order % XA_CHUNK_SHIFT)) - 1;
128258d6ea30SMatthew Wilcox 	xas->xa_node = XAS_RESTART;
128358d6ea30SMatthew Wilcox #else
128458d6ea30SMatthew Wilcox 	BUG_ON(order > 0);
128558d6ea30SMatthew Wilcox 	xas_set(xas, index);
128658d6ea30SMatthew Wilcox #endif
128758d6ea30SMatthew Wilcox }
128858d6ea30SMatthew Wilcox 
128958d6ea30SMatthew Wilcox /**
129058d6ea30SMatthew Wilcox  * xas_set_update() - Set up XArray operation state for a callback.
129158d6ea30SMatthew Wilcox  * @xas: XArray operation state.
129258d6ea30SMatthew Wilcox  * @update: Function to call when updating a node.
129358d6ea30SMatthew Wilcox  *
129458d6ea30SMatthew Wilcox  * The XArray can notify a caller after it has updated an xa_node.
129558d6ea30SMatthew Wilcox  * This is advanced functionality and is only needed by the page cache.
129658d6ea30SMatthew Wilcox  */
129758d6ea30SMatthew Wilcox static inline void xas_set_update(struct xa_state *xas, xa_update_node_t update)
129858d6ea30SMatthew Wilcox {
129958d6ea30SMatthew Wilcox 	xas->xa_update = update;
130058d6ea30SMatthew Wilcox }
130158d6ea30SMatthew Wilcox 
1302b803b428SMatthew Wilcox /**
1303b803b428SMatthew Wilcox  * xas_next_entry() - Advance iterator to next present entry.
1304b803b428SMatthew Wilcox  * @xas: XArray operation state.
1305b803b428SMatthew Wilcox  * @max: Highest index to return.
1306b803b428SMatthew Wilcox  *
1307b803b428SMatthew Wilcox  * xas_next_entry() is an inline function to optimise xarray traversal for
1308b803b428SMatthew Wilcox  * speed.  It is equivalent to calling xas_find(), and will call xas_find()
1309b803b428SMatthew Wilcox  * for all the hard cases.
1310b803b428SMatthew Wilcox  *
1311b803b428SMatthew Wilcox  * Return: The next present entry after the one currently referred to by @xas.
1312b803b428SMatthew Wilcox  */
1313b803b428SMatthew Wilcox static inline void *xas_next_entry(struct xa_state *xas, unsigned long max)
1314b803b428SMatthew Wilcox {
1315b803b428SMatthew Wilcox 	struct xa_node *node = xas->xa_node;
1316b803b428SMatthew Wilcox 	void *entry;
1317b803b428SMatthew Wilcox 
1318b803b428SMatthew Wilcox 	if (unlikely(xas_not_node(node) || node->shift ||
1319b803b428SMatthew Wilcox 			xas->xa_offset != (xas->xa_index & XA_CHUNK_MASK)))
1320b803b428SMatthew Wilcox 		return xas_find(xas, max);
1321b803b428SMatthew Wilcox 
1322b803b428SMatthew Wilcox 	do {
1323b803b428SMatthew Wilcox 		if (unlikely(xas->xa_index >= max))
1324b803b428SMatthew Wilcox 			return xas_find(xas, max);
1325b803b428SMatthew Wilcox 		if (unlikely(xas->xa_offset == XA_CHUNK_MASK))
1326b803b428SMatthew Wilcox 			return xas_find(xas, max);
1327b803b428SMatthew Wilcox 		entry = xa_entry(xas->xa, node, xas->xa_offset + 1);
1328b803b428SMatthew Wilcox 		if (unlikely(xa_is_internal(entry)))
1329b803b428SMatthew Wilcox 			return xas_find(xas, max);
1330b803b428SMatthew Wilcox 		xas->xa_offset++;
1331b803b428SMatthew Wilcox 		xas->xa_index++;
1332b803b428SMatthew Wilcox 	} while (!entry);
1333b803b428SMatthew Wilcox 
1334b803b428SMatthew Wilcox 	return entry;
1335b803b428SMatthew Wilcox }
1336b803b428SMatthew Wilcox 
1337b803b428SMatthew Wilcox /* Private */
1338b803b428SMatthew Wilcox static inline unsigned int xas_find_chunk(struct xa_state *xas, bool advance,
1339b803b428SMatthew Wilcox 		xa_mark_t mark)
1340b803b428SMatthew Wilcox {
1341b803b428SMatthew Wilcox 	unsigned long *addr = xas->xa_node->marks[(__force unsigned)mark];
1342b803b428SMatthew Wilcox 	unsigned int offset = xas->xa_offset;
1343b803b428SMatthew Wilcox 
1344b803b428SMatthew Wilcox 	if (advance)
1345b803b428SMatthew Wilcox 		offset++;
1346b803b428SMatthew Wilcox 	if (XA_CHUNK_SIZE == BITS_PER_LONG) {
1347b803b428SMatthew Wilcox 		if (offset < XA_CHUNK_SIZE) {
1348b803b428SMatthew Wilcox 			unsigned long data = *addr & (~0UL << offset);
1349b803b428SMatthew Wilcox 			if (data)
1350b803b428SMatthew Wilcox 				return __ffs(data);
1351b803b428SMatthew Wilcox 		}
1352b803b428SMatthew Wilcox 		return XA_CHUNK_SIZE;
1353b803b428SMatthew Wilcox 	}
1354b803b428SMatthew Wilcox 
1355b803b428SMatthew Wilcox 	return find_next_bit(addr, XA_CHUNK_SIZE, offset);
1356b803b428SMatthew Wilcox }
1357b803b428SMatthew Wilcox 
1358b803b428SMatthew Wilcox /**
1359b803b428SMatthew Wilcox  * xas_next_marked() - Advance iterator to next marked entry.
1360b803b428SMatthew Wilcox  * @xas: XArray operation state.
1361b803b428SMatthew Wilcox  * @max: Highest index to return.
1362b803b428SMatthew Wilcox  * @mark: Mark to search for.
1363b803b428SMatthew Wilcox  *
1364b803b428SMatthew Wilcox  * xas_next_marked() is an inline function to optimise xarray traversal for
1365b803b428SMatthew Wilcox  * speed.  It is equivalent to calling xas_find_marked(), and will call
1366b803b428SMatthew Wilcox  * xas_find_marked() for all the hard cases.
1367b803b428SMatthew Wilcox  *
1368b803b428SMatthew Wilcox  * Return: The next marked entry after the one currently referred to by @xas.
1369b803b428SMatthew Wilcox  */
1370b803b428SMatthew Wilcox static inline void *xas_next_marked(struct xa_state *xas, unsigned long max,
1371b803b428SMatthew Wilcox 								xa_mark_t mark)
1372b803b428SMatthew Wilcox {
1373b803b428SMatthew Wilcox 	struct xa_node *node = xas->xa_node;
1374b803b428SMatthew Wilcox 	unsigned int offset;
1375b803b428SMatthew Wilcox 
1376b803b428SMatthew Wilcox 	if (unlikely(xas_not_node(node) || node->shift))
1377b803b428SMatthew Wilcox 		return xas_find_marked(xas, max, mark);
1378b803b428SMatthew Wilcox 	offset = xas_find_chunk(xas, true, mark);
1379b803b428SMatthew Wilcox 	xas->xa_offset = offset;
1380b803b428SMatthew Wilcox 	xas->xa_index = (xas->xa_index & ~XA_CHUNK_MASK) + offset;
1381b803b428SMatthew Wilcox 	if (xas->xa_index > max)
1382b803b428SMatthew Wilcox 		return NULL;
1383b803b428SMatthew Wilcox 	if (offset == XA_CHUNK_SIZE)
1384b803b428SMatthew Wilcox 		return xas_find_marked(xas, max, mark);
1385b803b428SMatthew Wilcox 	return xa_entry(xas->xa, node, offset);
1386b803b428SMatthew Wilcox }
1387b803b428SMatthew Wilcox 
1388b803b428SMatthew Wilcox /*
1389b803b428SMatthew Wilcox  * If iterating while holding a lock, drop the lock and reschedule
1390b803b428SMatthew Wilcox  * every %XA_CHECK_SCHED loops.
1391b803b428SMatthew Wilcox  */
1392b803b428SMatthew Wilcox enum {
1393b803b428SMatthew Wilcox 	XA_CHECK_SCHED = 4096,
1394b803b428SMatthew Wilcox };
1395b803b428SMatthew Wilcox 
1396b803b428SMatthew Wilcox /**
1397b803b428SMatthew Wilcox  * xas_for_each() - Iterate over a range of an XArray.
1398b803b428SMatthew Wilcox  * @xas: XArray operation state.
1399b803b428SMatthew Wilcox  * @entry: Entry retrieved from the array.
1400b803b428SMatthew Wilcox  * @max: Maximum index to retrieve from array.
1401b803b428SMatthew Wilcox  *
1402b803b428SMatthew Wilcox  * The loop body will be executed for each entry present in the xarray
1403b803b428SMatthew Wilcox  * between the current xas position and @max.  @entry will be set to
1404b803b428SMatthew Wilcox  * the entry retrieved from the xarray.  It is safe to delete entries
1405b803b428SMatthew Wilcox  * from the array in the loop body.  You should hold either the RCU lock
1406b803b428SMatthew Wilcox  * or the xa_lock while iterating.  If you need to drop the lock, call
1407b803b428SMatthew Wilcox  * xas_pause() first.
1408b803b428SMatthew Wilcox  */
1409b803b428SMatthew Wilcox #define xas_for_each(xas, entry, max) \
1410b803b428SMatthew Wilcox 	for (entry = xas_find(xas, max); entry; \
1411b803b428SMatthew Wilcox 	     entry = xas_next_entry(xas, max))
1412b803b428SMatthew Wilcox 
1413b803b428SMatthew Wilcox /**
1414b803b428SMatthew Wilcox  * xas_for_each_marked() - Iterate over a range of an XArray.
1415b803b428SMatthew Wilcox  * @xas: XArray operation state.
1416b803b428SMatthew Wilcox  * @entry: Entry retrieved from the array.
1417b803b428SMatthew Wilcox  * @max: Maximum index to retrieve from array.
1418b803b428SMatthew Wilcox  * @mark: Mark to search for.
1419b803b428SMatthew Wilcox  *
1420b803b428SMatthew Wilcox  * The loop body will be executed for each marked entry in the xarray
1421b803b428SMatthew Wilcox  * between the current xas position and @max.  @entry will be set to
1422b803b428SMatthew Wilcox  * the entry retrieved from the xarray.  It is safe to delete entries
1423b803b428SMatthew Wilcox  * from the array in the loop body.  You should hold either the RCU lock
1424b803b428SMatthew Wilcox  * or the xa_lock while iterating.  If you need to drop the lock, call
1425b803b428SMatthew Wilcox  * xas_pause() first.
1426b803b428SMatthew Wilcox  */
1427b803b428SMatthew Wilcox #define xas_for_each_marked(xas, entry, max, mark) \
1428b803b428SMatthew Wilcox 	for (entry = xas_find_marked(xas, max, mark); entry; \
1429b803b428SMatthew Wilcox 	     entry = xas_next_marked(xas, max, mark))
1430b803b428SMatthew Wilcox 
14314e99d4e9SMatthew Wilcox /**
14324e99d4e9SMatthew Wilcox  * xas_for_each_conflict() - Iterate over a range of an XArray.
14334e99d4e9SMatthew Wilcox  * @xas: XArray operation state.
14344e99d4e9SMatthew Wilcox  * @entry: Entry retrieved from the array.
14354e99d4e9SMatthew Wilcox  *
14364e99d4e9SMatthew Wilcox  * The loop body will be executed for each entry in the XArray that lies
14374e99d4e9SMatthew Wilcox  * within the range specified by @xas.  If the loop completes successfully,
14384e99d4e9SMatthew Wilcox  * any entries that lie in this range will be replaced by @entry.  The caller
14394e99d4e9SMatthew Wilcox  * may break out of the loop; if they do so, the contents of the XArray will
14404e99d4e9SMatthew Wilcox  * be unchanged.  The operation may fail due to an out of memory condition.
14414e99d4e9SMatthew Wilcox  * The caller may also call xa_set_err() to exit the loop while setting an
14424e99d4e9SMatthew Wilcox  * error to record the reason.
14434e99d4e9SMatthew Wilcox  */
14444e99d4e9SMatthew Wilcox #define xas_for_each_conflict(xas, entry) \
14454e99d4e9SMatthew Wilcox 	while ((entry = xas_find_conflict(xas)))
14464e99d4e9SMatthew Wilcox 
144764d3e9a9SMatthew Wilcox void *__xas_next(struct xa_state *);
144864d3e9a9SMatthew Wilcox void *__xas_prev(struct xa_state *);
144964d3e9a9SMatthew Wilcox 
145064d3e9a9SMatthew Wilcox /**
145164d3e9a9SMatthew Wilcox  * xas_prev() - Move iterator to previous index.
145264d3e9a9SMatthew Wilcox  * @xas: XArray operation state.
145364d3e9a9SMatthew Wilcox  *
145464d3e9a9SMatthew Wilcox  * If the @xas was in an error state, it will remain in an error state
145564d3e9a9SMatthew Wilcox  * and this function will return %NULL.  If the @xas has never been walked,
145664d3e9a9SMatthew Wilcox  * it will have the effect of calling xas_load().  Otherwise one will be
145764d3e9a9SMatthew Wilcox  * subtracted from the index and the state will be walked to the correct
145864d3e9a9SMatthew Wilcox  * location in the array for the next operation.
145964d3e9a9SMatthew Wilcox  *
146064d3e9a9SMatthew Wilcox  * If the iterator was referencing index 0, this function wraps
146164d3e9a9SMatthew Wilcox  * around to %ULONG_MAX.
146264d3e9a9SMatthew Wilcox  *
146364d3e9a9SMatthew Wilcox  * Return: The entry at the new index.  This may be %NULL or an internal
146464d3e9a9SMatthew Wilcox  * entry.
146564d3e9a9SMatthew Wilcox  */
146664d3e9a9SMatthew Wilcox static inline void *xas_prev(struct xa_state *xas)
146764d3e9a9SMatthew Wilcox {
146864d3e9a9SMatthew Wilcox 	struct xa_node *node = xas->xa_node;
146964d3e9a9SMatthew Wilcox 
147064d3e9a9SMatthew Wilcox 	if (unlikely(xas_not_node(node) || node->shift ||
147164d3e9a9SMatthew Wilcox 				xas->xa_offset == 0))
147264d3e9a9SMatthew Wilcox 		return __xas_prev(xas);
147364d3e9a9SMatthew Wilcox 
147464d3e9a9SMatthew Wilcox 	xas->xa_index--;
147564d3e9a9SMatthew Wilcox 	xas->xa_offset--;
147664d3e9a9SMatthew Wilcox 	return xa_entry(xas->xa, node, xas->xa_offset);
147764d3e9a9SMatthew Wilcox }
147864d3e9a9SMatthew Wilcox 
147964d3e9a9SMatthew Wilcox /**
148064d3e9a9SMatthew Wilcox  * xas_next() - Move state to next index.
148164d3e9a9SMatthew Wilcox  * @xas: XArray operation state.
148264d3e9a9SMatthew Wilcox  *
148364d3e9a9SMatthew Wilcox  * If the @xas was in an error state, it will remain in an error state
148464d3e9a9SMatthew Wilcox  * and this function will return %NULL.  If the @xas has never been walked,
148564d3e9a9SMatthew Wilcox  * it will have the effect of calling xas_load().  Otherwise one will be
148664d3e9a9SMatthew Wilcox  * added to the index and the state will be walked to the correct
148764d3e9a9SMatthew Wilcox  * location in the array for the next operation.
148864d3e9a9SMatthew Wilcox  *
148964d3e9a9SMatthew Wilcox  * If the iterator was referencing index %ULONG_MAX, this function wraps
149064d3e9a9SMatthew Wilcox  * around to 0.
149164d3e9a9SMatthew Wilcox  *
149264d3e9a9SMatthew Wilcox  * Return: The entry at the new index.  This may be %NULL or an internal
149364d3e9a9SMatthew Wilcox  * entry.
149464d3e9a9SMatthew Wilcox  */
149564d3e9a9SMatthew Wilcox static inline void *xas_next(struct xa_state *xas)
149664d3e9a9SMatthew Wilcox {
149764d3e9a9SMatthew Wilcox 	struct xa_node *node = xas->xa_node;
149864d3e9a9SMatthew Wilcox 
149964d3e9a9SMatthew Wilcox 	if (unlikely(xas_not_node(node) || node->shift ||
150064d3e9a9SMatthew Wilcox 				xas->xa_offset == XA_CHUNK_MASK))
150164d3e9a9SMatthew Wilcox 		return __xas_next(xas);
150264d3e9a9SMatthew Wilcox 
150364d3e9a9SMatthew Wilcox 	xas->xa_index++;
150464d3e9a9SMatthew Wilcox 	xas->xa_offset++;
150564d3e9a9SMatthew Wilcox 	return xa_entry(xas->xa, node, xas->xa_offset);
150664d3e9a9SMatthew Wilcox }
150764d3e9a9SMatthew Wilcox 
1508f6bb2a2cSMatthew Wilcox #endif /* _LINUX_XARRAY_H */
1509