xref: /linux-6.15/include/linux/xarray.h (revision edcddd4c)
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 {
17976b4e529SMatthew Wilcox 	return unlikely(xa_is_internal(entry) &&
180*edcddd4cSDan Carpenter 			entry >= xa_mk_internal(-MAX_ERRNO));
181ad3d6c72SMatthew Wilcox }
182ad3d6c72SMatthew Wilcox 
183ad3d6c72SMatthew Wilcox /**
184ad3d6c72SMatthew Wilcox  * xa_err() - Turn an XArray result into an errno.
185ad3d6c72SMatthew Wilcox  * @entry: Result from calling an XArray function.
186ad3d6c72SMatthew Wilcox  *
187ad3d6c72SMatthew Wilcox  * If an XArray operation cannot complete an operation, it will return
188ad3d6c72SMatthew Wilcox  * a special pointer value which encodes an errno.  This function extracts
189ad3d6c72SMatthew Wilcox  * the errno from the pointer value, or returns 0 if the pointer does not
190ad3d6c72SMatthew Wilcox  * represent an errno.
191ad3d6c72SMatthew Wilcox  *
192ad3d6c72SMatthew Wilcox  * Context: Any context.
193ad3d6c72SMatthew Wilcox  * Return: A negative errno or 0.
194ad3d6c72SMatthew Wilcox  */
195ad3d6c72SMatthew Wilcox static inline int xa_err(void *entry)
196ad3d6c72SMatthew Wilcox {
197ad3d6c72SMatthew Wilcox 	/* xa_to_internal() would not do sign extension. */
198ad3d6c72SMatthew Wilcox 	if (xa_is_err(entry))
199ad3d6c72SMatthew Wilcox 		return (long)entry >> 2;
200ad3d6c72SMatthew Wilcox 	return 0;
201ad3d6c72SMatthew Wilcox }
202ad3d6c72SMatthew Wilcox 
2039b89a035SMatthew Wilcox typedef unsigned __bitwise xa_mark_t;
2049b89a035SMatthew Wilcox #define XA_MARK_0		((__force xa_mark_t)0U)
2059b89a035SMatthew Wilcox #define XA_MARK_1		((__force xa_mark_t)1U)
2069b89a035SMatthew Wilcox #define XA_MARK_2		((__force xa_mark_t)2U)
2079b89a035SMatthew Wilcox #define XA_PRESENT		((__force xa_mark_t)8U)
2089b89a035SMatthew Wilcox #define XA_MARK_MAX		XA_MARK_2
209371c752dSMatthew Wilcox #define XA_FREE_MARK		XA_MARK_0
2109b89a035SMatthew Wilcox 
21158d6ea30SMatthew Wilcox enum xa_lock_type {
21258d6ea30SMatthew Wilcox 	XA_LOCK_IRQ = 1,
21358d6ea30SMatthew Wilcox 	XA_LOCK_BH = 2,
21458d6ea30SMatthew Wilcox };
21558d6ea30SMatthew Wilcox 
2169b89a035SMatthew Wilcox /*
2179b89a035SMatthew Wilcox  * Values for xa_flags.  The radix tree stores its GFP flags in the xa_flags,
2189b89a035SMatthew Wilcox  * and we remain compatible with that.
2199b89a035SMatthew Wilcox  */
22058d6ea30SMatthew Wilcox #define XA_FLAGS_LOCK_IRQ	((__force gfp_t)XA_LOCK_IRQ)
22158d6ea30SMatthew Wilcox #define XA_FLAGS_LOCK_BH	((__force gfp_t)XA_LOCK_BH)
222371c752dSMatthew Wilcox #define XA_FLAGS_TRACK_FREE	((__force gfp_t)4U)
2239b89a035SMatthew Wilcox #define XA_FLAGS_MARK(mark)	((__force gfp_t)((1U << __GFP_BITS_SHIFT) << \
2249b89a035SMatthew Wilcox 						(__force unsigned)(mark)))
2259b89a035SMatthew Wilcox 
226371c752dSMatthew Wilcox #define XA_FLAGS_ALLOC	(XA_FLAGS_TRACK_FREE | XA_FLAGS_MARK(XA_FREE_MARK))
227371c752dSMatthew Wilcox 
228ad3d6c72SMatthew Wilcox /**
229f8d5d0ccSMatthew Wilcox  * struct xarray - The anchor of the XArray.
230f8d5d0ccSMatthew Wilcox  * @xa_lock: Lock that protects the contents of the XArray.
231f8d5d0ccSMatthew Wilcox  *
232f8d5d0ccSMatthew Wilcox  * To use the xarray, define it statically or embed it in your data structure.
233f8d5d0ccSMatthew Wilcox  * It is a very small data structure, so it does not usually make sense to
234f8d5d0ccSMatthew Wilcox  * allocate it separately and keep a pointer to it in your data structure.
235f8d5d0ccSMatthew Wilcox  *
236f8d5d0ccSMatthew Wilcox  * You may use the xa_lock to protect your own data structures as well.
237f8d5d0ccSMatthew Wilcox  */
238f8d5d0ccSMatthew Wilcox /*
239f8d5d0ccSMatthew Wilcox  * If all of the entries in the array are NULL, @xa_head is a NULL pointer.
240f8d5d0ccSMatthew Wilcox  * If the only non-NULL entry in the array is at index 0, @xa_head is that
241f8d5d0ccSMatthew Wilcox  * entry.  If any other entry in the array is non-NULL, @xa_head points
242f8d5d0ccSMatthew Wilcox  * to an @xa_node.
243f8d5d0ccSMatthew Wilcox  */
244f8d5d0ccSMatthew Wilcox struct xarray {
245f8d5d0ccSMatthew Wilcox 	spinlock_t	xa_lock;
246f8d5d0ccSMatthew Wilcox /* private: The rest of the data structure is not to be used directly. */
247f8d5d0ccSMatthew Wilcox 	gfp_t		xa_flags;
248f8d5d0ccSMatthew Wilcox 	void __rcu *	xa_head;
249f8d5d0ccSMatthew Wilcox };
250f8d5d0ccSMatthew Wilcox 
251f8d5d0ccSMatthew Wilcox #define XARRAY_INIT(name, flags) {				\
252f8d5d0ccSMatthew Wilcox 	.xa_lock = __SPIN_LOCK_UNLOCKED(name.xa_lock),		\
253f8d5d0ccSMatthew Wilcox 	.xa_flags = flags,					\
254f8d5d0ccSMatthew Wilcox 	.xa_head = NULL,					\
255f8d5d0ccSMatthew Wilcox }
256f8d5d0ccSMatthew Wilcox 
257f8d5d0ccSMatthew Wilcox /**
258f8d5d0ccSMatthew Wilcox  * DEFINE_XARRAY_FLAGS() - Define an XArray with custom flags.
259f8d5d0ccSMatthew Wilcox  * @name: A string that names your XArray.
260f8d5d0ccSMatthew Wilcox  * @flags: XA_FLAG values.
261f8d5d0ccSMatthew Wilcox  *
262f8d5d0ccSMatthew Wilcox  * This is intended for file scope definitions of XArrays.  It declares
263f8d5d0ccSMatthew Wilcox  * and initialises an empty XArray with the chosen name and flags.  It is
264f8d5d0ccSMatthew Wilcox  * equivalent to calling xa_init_flags() on the array, but it does the
265f8d5d0ccSMatthew Wilcox  * initialisation at compiletime instead of runtime.
266f8d5d0ccSMatthew Wilcox  */
267f8d5d0ccSMatthew Wilcox #define DEFINE_XARRAY_FLAGS(name, flags)				\
268f8d5d0ccSMatthew Wilcox 	struct xarray name = XARRAY_INIT(name, flags)
269f8d5d0ccSMatthew Wilcox 
270f8d5d0ccSMatthew Wilcox /**
271f8d5d0ccSMatthew Wilcox  * DEFINE_XARRAY() - Define an XArray.
272f8d5d0ccSMatthew Wilcox  * @name: A string that names your XArray.
273f8d5d0ccSMatthew Wilcox  *
274f8d5d0ccSMatthew Wilcox  * This is intended for file scope definitions of XArrays.  It declares
275f8d5d0ccSMatthew Wilcox  * and initialises an empty XArray with the chosen name.  It is equivalent
276f8d5d0ccSMatthew Wilcox  * to calling xa_init() on the array, but it does the initialisation at
277f8d5d0ccSMatthew Wilcox  * compiletime instead of runtime.
278f8d5d0ccSMatthew Wilcox  */
279f8d5d0ccSMatthew Wilcox #define DEFINE_XARRAY(name) DEFINE_XARRAY_FLAGS(name, 0)
280f8d5d0ccSMatthew Wilcox 
281371c752dSMatthew Wilcox /**
282371c752dSMatthew Wilcox  * DEFINE_XARRAY_ALLOC() - Define an XArray which can allocate IDs.
283371c752dSMatthew Wilcox  * @name: A string that names your XArray.
284371c752dSMatthew Wilcox  *
285371c752dSMatthew Wilcox  * This is intended for file scope definitions of allocating XArrays.
286371c752dSMatthew Wilcox  * See also DEFINE_XARRAY().
287371c752dSMatthew Wilcox  */
288371c752dSMatthew Wilcox #define DEFINE_XARRAY_ALLOC(name) DEFINE_XARRAY_FLAGS(name, XA_FLAGS_ALLOC)
289371c752dSMatthew Wilcox 
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 /**
30702669b17SMatthew Wilcox  * xa_init_flags() - Initialise an empty XArray with flags.
30802669b17SMatthew Wilcox  * @xa: XArray.
30902669b17SMatthew Wilcox  * @flags: XA_FLAG values.
31002669b17SMatthew Wilcox  *
31102669b17SMatthew Wilcox  * If you need to initialise an XArray with special flags (eg you need
31202669b17SMatthew Wilcox  * to take the lock from interrupt context), use this function instead
31302669b17SMatthew Wilcox  * of xa_init().
31402669b17SMatthew Wilcox  *
31502669b17SMatthew Wilcox  * Context: Any context.
31602669b17SMatthew Wilcox  */
31702669b17SMatthew Wilcox static inline void xa_init_flags(struct xarray *xa, gfp_t flags)
31802669b17SMatthew Wilcox {
31902669b17SMatthew Wilcox 	spin_lock_init(&xa->xa_lock);
32002669b17SMatthew Wilcox 	xa->xa_flags = flags;
32102669b17SMatthew Wilcox 	xa->xa_head = NULL;
32202669b17SMatthew Wilcox }
32302669b17SMatthew Wilcox 
32402669b17SMatthew Wilcox /**
325f8d5d0ccSMatthew Wilcox  * xa_init() - Initialise an empty XArray.
326f8d5d0ccSMatthew Wilcox  * @xa: XArray.
327f8d5d0ccSMatthew Wilcox  *
328f8d5d0ccSMatthew Wilcox  * An empty XArray is full of NULL entries.
329f8d5d0ccSMatthew Wilcox  *
330f8d5d0ccSMatthew Wilcox  * Context: Any context.
331f8d5d0ccSMatthew Wilcox  */
332f8d5d0ccSMatthew Wilcox static inline void xa_init(struct xarray *xa)
333f8d5d0ccSMatthew Wilcox {
334f8d5d0ccSMatthew Wilcox 	xa_init_flags(xa, 0);
335f8d5d0ccSMatthew Wilcox }
336f8d5d0ccSMatthew Wilcox 
337ad3d6c72SMatthew Wilcox /**
338ad3d6c72SMatthew Wilcox  * xa_empty() - Determine if an array has any present entries.
339ad3d6c72SMatthew Wilcox  * @xa: XArray.
340ad3d6c72SMatthew Wilcox  *
341ad3d6c72SMatthew Wilcox  * Context: Any context.
342ad3d6c72SMatthew Wilcox  * Return: %true if the array contains only NULL pointers.
343ad3d6c72SMatthew Wilcox  */
344ad3d6c72SMatthew Wilcox static inline bool xa_empty(const struct xarray *xa)
345ad3d6c72SMatthew Wilcox {
346ad3d6c72SMatthew Wilcox 	return xa->xa_head == NULL;
347ad3d6c72SMatthew Wilcox }
348ad3d6c72SMatthew Wilcox 
3499b89a035SMatthew Wilcox /**
3509b89a035SMatthew Wilcox  * xa_marked() - Inquire whether any entry in this array has a mark set
3519b89a035SMatthew Wilcox  * @xa: Array
3529b89a035SMatthew Wilcox  * @mark: Mark value
3539b89a035SMatthew Wilcox  *
3549b89a035SMatthew Wilcox  * Context: Any context.
3559b89a035SMatthew Wilcox  * Return: %true if any entry has this mark set.
3569b89a035SMatthew Wilcox  */
3579b89a035SMatthew Wilcox static inline bool xa_marked(const struct xarray *xa, xa_mark_t mark)
3589b89a035SMatthew Wilcox {
3599b89a035SMatthew Wilcox 	return xa->xa_flags & XA_FLAGS_MARK(mark);
3609b89a035SMatthew Wilcox }
3619b89a035SMatthew Wilcox 
36258d6ea30SMatthew Wilcox /**
3634a31896cSMatthew Wilcox  * xa_for_each_start() - Iterate over a portion of an XArray.
364b803b428SMatthew Wilcox  * @xa: XArray.
365b803b428SMatthew Wilcox  * @index: Index of @entry.
3664a31896cSMatthew Wilcox  * @entry: Entry retrieved from array.
3674a31896cSMatthew Wilcox  * @start: First index to retrieve from array.
368b803b428SMatthew Wilcox  *
3694a31896cSMatthew Wilcox  * During the iteration, @entry will have the value of the entry stored
3704a31896cSMatthew Wilcox  * in @xa at @index.  You may modify @index during the iteration if you
3714a31896cSMatthew Wilcox  * want to skip or reprocess indices.  It is safe to modify the array
3724a31896cSMatthew Wilcox  * during the iteration.  At the end of the iteration, @entry will be set
3734a31896cSMatthew Wilcox  * to NULL and @index will have a value less than or equal to max.
3744a31896cSMatthew Wilcox  *
3754a31896cSMatthew Wilcox  * xa_for_each_start() is O(n.log(n)) while xas_for_each() is O(n).  You have
3764a31896cSMatthew Wilcox  * to handle your own locking with xas_for_each(), and if you have to unlock
3774a31896cSMatthew Wilcox  * after each iteration, it will also end up being O(n.log(n)).
3784a31896cSMatthew Wilcox  * xa_for_each_start() will spin if it hits a retry entry; if you intend to
3794a31896cSMatthew Wilcox  * see retry entries, you should use the xas_for_each() iterator instead.
3804a31896cSMatthew Wilcox  * The xas_for_each() iterator will expand into more inline code than
3814a31896cSMatthew Wilcox  * xa_for_each_start().
3824a31896cSMatthew Wilcox  *
3834a31896cSMatthew Wilcox  * Context: Any context.  Takes and releases the RCU lock.
3844a31896cSMatthew Wilcox  */
3854a31896cSMatthew Wilcox #define xa_for_each_start(xa, index, entry, start)			\
3864a31896cSMatthew Wilcox 	for (index = start,						\
3874a31896cSMatthew Wilcox 	     entry = xa_find(xa, &index, ULONG_MAX, XA_PRESENT);	\
3884a31896cSMatthew Wilcox 	     entry;							\
3894a31896cSMatthew Wilcox 	     entry = xa_find_after(xa, &index, ULONG_MAX, XA_PRESENT))
3904a31896cSMatthew Wilcox 
3914a31896cSMatthew Wilcox /**
3924a31896cSMatthew Wilcox  * xa_for_each() - Iterate over present entries in an XArray.
3934a31896cSMatthew Wilcox  * @xa: XArray.
3944a31896cSMatthew Wilcox  * @index: Index of @entry.
3954a31896cSMatthew Wilcox  * @entry: Entry retrieved from array.
3964a31896cSMatthew Wilcox  *
3974a31896cSMatthew Wilcox  * During the iteration, @entry will have the value of the entry stored
3984a31896cSMatthew Wilcox  * in @xa at @index.  You may modify @index during the iteration if you want
3994a31896cSMatthew Wilcox  * to skip or reprocess indices.  It is safe to modify the array during the
4004a31896cSMatthew Wilcox  * iteration.  At the end of the iteration, @entry will be set to NULL and
4014a31896cSMatthew Wilcox  * @index will have a value less than or equal to max.
402b803b428SMatthew Wilcox  *
403b803b428SMatthew Wilcox  * xa_for_each() is O(n.log(n)) while xas_for_each() is O(n).  You have
404b803b428SMatthew Wilcox  * to handle your own locking with xas_for_each(), and if you have to unlock
405b803b428SMatthew Wilcox  * after each iteration, it will also end up being O(n.log(n)).  xa_for_each()
406b803b428SMatthew Wilcox  * will spin if it hits a retry entry; if you intend to see retry entries,
407b803b428SMatthew Wilcox  * you should use the xas_for_each() iterator instead.  The xas_for_each()
408b803b428SMatthew Wilcox  * iterator will expand into more inline code than xa_for_each().
409b803b428SMatthew Wilcox  *
410b803b428SMatthew Wilcox  * Context: Any context.  Takes and releases the RCU lock.
411b803b428SMatthew Wilcox  */
4124a31896cSMatthew Wilcox #define xa_for_each(xa, index, entry) \
4134a31896cSMatthew Wilcox 	xa_for_each_start(xa, index, entry, 0)
4144a31896cSMatthew Wilcox 
4154a31896cSMatthew Wilcox /**
4164a31896cSMatthew Wilcox  * xa_for_each_marked() - Iterate over marked entries in an XArray.
4174a31896cSMatthew Wilcox  * @xa: XArray.
4184a31896cSMatthew Wilcox  * @index: Index of @entry.
4194a31896cSMatthew Wilcox  * @entry: Entry retrieved from array.
4204a31896cSMatthew Wilcox  * @filter: Selection criterion.
4214a31896cSMatthew Wilcox  *
4224a31896cSMatthew Wilcox  * During the iteration, @entry will have the value of the entry stored
4234a31896cSMatthew Wilcox  * in @xa at @index.  The iteration will skip all entries in the array
4244a31896cSMatthew Wilcox  * which do not match @filter.  You may modify @index during the iteration
4254a31896cSMatthew Wilcox  * if you want to skip or reprocess indices.  It is safe to modify the array
4264a31896cSMatthew Wilcox  * during the iteration.  At the end of the iteration, @entry will be set to
4274a31896cSMatthew Wilcox  * NULL and @index will have a value less than or equal to max.
4284a31896cSMatthew Wilcox  *
4294a31896cSMatthew Wilcox  * xa_for_each_marked() is O(n.log(n)) while xas_for_each_marked() is O(n).
4304a31896cSMatthew Wilcox  * You have to handle your own locking with xas_for_each(), and if you have
4314a31896cSMatthew Wilcox  * to unlock after each iteration, it will also end up being O(n.log(n)).
4324a31896cSMatthew Wilcox  * xa_for_each_marked() will spin if it hits a retry entry; if you intend to
4334a31896cSMatthew Wilcox  * see retry entries, you should use the xas_for_each_marked() iterator
4344a31896cSMatthew Wilcox  * instead.  The xas_for_each_marked() iterator will expand into more inline
4354a31896cSMatthew Wilcox  * code than xa_for_each_marked().
4364a31896cSMatthew Wilcox  *
4374a31896cSMatthew Wilcox  * Context: Any context.  Takes and releases the RCU lock.
4384a31896cSMatthew Wilcox  */
4394a31896cSMatthew Wilcox #define xa_for_each_marked(xa, index, entry, filter) \
4404a31896cSMatthew Wilcox 	for (index = 0, entry = xa_find(xa, &index, ULONG_MAX, filter); \
4414a31896cSMatthew Wilcox 	     entry; entry = xa_find_after(xa, &index, ULONG_MAX, filter))
442b803b428SMatthew Wilcox 
443f6bb2a2cSMatthew Wilcox #define xa_trylock(xa)		spin_trylock(&(xa)->xa_lock)
444f6bb2a2cSMatthew Wilcox #define xa_lock(xa)		spin_lock(&(xa)->xa_lock)
445f6bb2a2cSMatthew Wilcox #define xa_unlock(xa)		spin_unlock(&(xa)->xa_lock)
446f6bb2a2cSMatthew Wilcox #define xa_lock_bh(xa)		spin_lock_bh(&(xa)->xa_lock)
447f6bb2a2cSMatthew Wilcox #define xa_unlock_bh(xa)	spin_unlock_bh(&(xa)->xa_lock)
448f6bb2a2cSMatthew Wilcox #define xa_lock_irq(xa)		spin_lock_irq(&(xa)->xa_lock)
449f6bb2a2cSMatthew Wilcox #define xa_unlock_irq(xa)	spin_unlock_irq(&(xa)->xa_lock)
450f6bb2a2cSMatthew Wilcox #define xa_lock_irqsave(xa, flags) \
451f6bb2a2cSMatthew Wilcox 				spin_lock_irqsave(&(xa)->xa_lock, flags)
452f6bb2a2cSMatthew Wilcox #define xa_unlock_irqrestore(xa, flags) \
453f6bb2a2cSMatthew Wilcox 				spin_unlock_irqrestore(&(xa)->xa_lock, flags)
454f6bb2a2cSMatthew Wilcox 
4559b89a035SMatthew Wilcox /*
45658d6ea30SMatthew Wilcox  * Versions of the normal API which require the caller to hold the
45758d6ea30SMatthew Wilcox  * xa_lock.  If the GFP flags allow it, they will drop the lock to
45858d6ea30SMatthew Wilcox  * allocate memory, then reacquire it afterwards.  These functions
45958d6ea30SMatthew Wilcox  * may also re-enable interrupts if the XArray flags indicate the
46058d6ea30SMatthew Wilcox  * locking should be interrupt safe.
4619b89a035SMatthew Wilcox  */
46258d6ea30SMatthew Wilcox void *__xa_erase(struct xarray *, unsigned long index);
46358d6ea30SMatthew Wilcox void *__xa_store(struct xarray *, unsigned long index, void *entry, gfp_t);
46441aec91fSMatthew Wilcox void *__xa_cmpxchg(struct xarray *, unsigned long index, void *old,
46541aec91fSMatthew Wilcox 		void *entry, gfp_t);
466b0606fedSMatthew Wilcox int __xa_insert(struct xarray *, unsigned long index, void *entry, gfp_t);
467371c752dSMatthew Wilcox int __xa_alloc(struct xarray *, u32 *id, u32 max, void *entry, gfp_t);
4684c0608f4SMatthew Wilcox int __xa_reserve(struct xarray *, unsigned long index, gfp_t);
4699b89a035SMatthew Wilcox void __xa_set_mark(struct xarray *, unsigned long index, xa_mark_t);
4709b89a035SMatthew Wilcox void __xa_clear_mark(struct xarray *, unsigned long index, xa_mark_t);
4719b89a035SMatthew Wilcox 
47258d6ea30SMatthew Wilcox /**
47384e5acb7SMatthew Wilcox  * xa_store_bh() - Store this entry in the XArray.
47484e5acb7SMatthew Wilcox  * @xa: XArray.
47584e5acb7SMatthew Wilcox  * @index: Index into array.
47684e5acb7SMatthew Wilcox  * @entry: New entry.
47784e5acb7SMatthew Wilcox  * @gfp: Memory allocation flags.
47884e5acb7SMatthew Wilcox  *
47984e5acb7SMatthew Wilcox  * This function is like calling xa_store() except it disables softirqs
48084e5acb7SMatthew Wilcox  * while holding the array lock.
48184e5acb7SMatthew Wilcox  *
48284e5acb7SMatthew Wilcox  * Context: Any context.  Takes and releases the xa_lock while
48384e5acb7SMatthew Wilcox  * disabling softirqs.
48484e5acb7SMatthew Wilcox  * Return: The entry which used to be at this index.
48584e5acb7SMatthew Wilcox  */
48684e5acb7SMatthew Wilcox static inline void *xa_store_bh(struct xarray *xa, unsigned long index,
48784e5acb7SMatthew Wilcox 		void *entry, gfp_t gfp)
48884e5acb7SMatthew Wilcox {
48984e5acb7SMatthew Wilcox 	void *curr;
49084e5acb7SMatthew Wilcox 
49184e5acb7SMatthew Wilcox 	xa_lock_bh(xa);
49284e5acb7SMatthew Wilcox 	curr = __xa_store(xa, index, entry, gfp);
49384e5acb7SMatthew Wilcox 	xa_unlock_bh(xa);
49484e5acb7SMatthew Wilcox 
49584e5acb7SMatthew Wilcox 	return curr;
49684e5acb7SMatthew Wilcox }
49784e5acb7SMatthew Wilcox 
49884e5acb7SMatthew Wilcox /**
49919ba9ecfSCyrill Gorcunov  * xa_store_irq() - Store this entry in the XArray.
50084e5acb7SMatthew Wilcox  * @xa: XArray.
50184e5acb7SMatthew Wilcox  * @index: Index into array.
50284e5acb7SMatthew Wilcox  * @entry: New entry.
50384e5acb7SMatthew Wilcox  * @gfp: Memory allocation flags.
50484e5acb7SMatthew Wilcox  *
50584e5acb7SMatthew Wilcox  * This function is like calling xa_store() except it disables interrupts
50684e5acb7SMatthew Wilcox  * while holding the array lock.
50784e5acb7SMatthew Wilcox  *
50884e5acb7SMatthew Wilcox  * Context: Process context.  Takes and releases the xa_lock while
50984e5acb7SMatthew Wilcox  * disabling interrupts.
51084e5acb7SMatthew Wilcox  * Return: The entry which used to be at this index.
51184e5acb7SMatthew Wilcox  */
51284e5acb7SMatthew Wilcox static inline void *xa_store_irq(struct xarray *xa, unsigned long index,
51384e5acb7SMatthew Wilcox 		void *entry, gfp_t gfp)
51484e5acb7SMatthew Wilcox {
51584e5acb7SMatthew Wilcox 	void *curr;
51684e5acb7SMatthew Wilcox 
51784e5acb7SMatthew Wilcox 	xa_lock_irq(xa);
51884e5acb7SMatthew Wilcox 	curr = __xa_store(xa, index, entry, gfp);
51984e5acb7SMatthew Wilcox 	xa_unlock_irq(xa);
52084e5acb7SMatthew Wilcox 
52184e5acb7SMatthew Wilcox 	return curr;
52284e5acb7SMatthew Wilcox }
52384e5acb7SMatthew Wilcox 
52484e5acb7SMatthew Wilcox /**
52558d6ea30SMatthew Wilcox  * xa_erase_bh() - Erase this entry from the XArray.
52658d6ea30SMatthew Wilcox  * @xa: XArray.
52758d6ea30SMatthew Wilcox  * @index: Index of entry.
52858d6ea30SMatthew Wilcox  *
52958d6ea30SMatthew Wilcox  * This function is the equivalent of calling xa_store() with %NULL as
53058d6ea30SMatthew Wilcox  * the third argument.  The XArray does not need to allocate memory, so
53158d6ea30SMatthew Wilcox  * the user does not need to provide GFP flags.
53258d6ea30SMatthew Wilcox  *
533804dfaf0SMatthew Wilcox  * Context: Any context.  Takes and releases the xa_lock while
53458d6ea30SMatthew Wilcox  * disabling softirqs.
53558d6ea30SMatthew Wilcox  * Return: The entry which used to be at this index.
53658d6ea30SMatthew Wilcox  */
53758d6ea30SMatthew Wilcox static inline void *xa_erase_bh(struct xarray *xa, unsigned long index)
53858d6ea30SMatthew Wilcox {
53958d6ea30SMatthew Wilcox 	void *entry;
54058d6ea30SMatthew Wilcox 
54158d6ea30SMatthew Wilcox 	xa_lock_bh(xa);
54258d6ea30SMatthew Wilcox 	entry = __xa_erase(xa, index);
54358d6ea30SMatthew Wilcox 	xa_unlock_bh(xa);
54458d6ea30SMatthew Wilcox 
54558d6ea30SMatthew Wilcox 	return entry;
54658d6ea30SMatthew Wilcox }
54758d6ea30SMatthew Wilcox 
54858d6ea30SMatthew Wilcox /**
54958d6ea30SMatthew Wilcox  * xa_erase_irq() - Erase this entry from the XArray.
55058d6ea30SMatthew Wilcox  * @xa: XArray.
55158d6ea30SMatthew Wilcox  * @index: Index of entry.
55258d6ea30SMatthew Wilcox  *
55358d6ea30SMatthew Wilcox  * This function is the equivalent of calling xa_store() with %NULL as
55458d6ea30SMatthew Wilcox  * the third argument.  The XArray does not need to allocate memory, so
55558d6ea30SMatthew Wilcox  * the user does not need to provide GFP flags.
55658d6ea30SMatthew Wilcox  *
55758d6ea30SMatthew Wilcox  * Context: Process context.  Takes and releases the xa_lock while
55858d6ea30SMatthew Wilcox  * disabling interrupts.
55958d6ea30SMatthew Wilcox  * Return: The entry which used to be at this index.
56058d6ea30SMatthew Wilcox  */
56158d6ea30SMatthew Wilcox static inline void *xa_erase_irq(struct xarray *xa, unsigned long index)
56258d6ea30SMatthew Wilcox {
56358d6ea30SMatthew Wilcox 	void *entry;
56458d6ea30SMatthew Wilcox 
56558d6ea30SMatthew Wilcox 	xa_lock_irq(xa);
56658d6ea30SMatthew Wilcox 	entry = __xa_erase(xa, index);
56758d6ea30SMatthew Wilcox 	xa_unlock_irq(xa);
56858d6ea30SMatthew Wilcox 
56958d6ea30SMatthew Wilcox 	return entry;
57058d6ea30SMatthew Wilcox }
57158d6ea30SMatthew Wilcox 
572371c752dSMatthew Wilcox /**
573c5beb07eSMatthew Wilcox  * xa_cmpxchg() - Conditionally replace an entry in the XArray.
574c5beb07eSMatthew Wilcox  * @xa: XArray.
575c5beb07eSMatthew Wilcox  * @index: Index into array.
576c5beb07eSMatthew Wilcox  * @old: Old value to test against.
577c5beb07eSMatthew Wilcox  * @entry: New value to place in array.
578c5beb07eSMatthew Wilcox  * @gfp: Memory allocation flags.
579c5beb07eSMatthew Wilcox  *
580c5beb07eSMatthew Wilcox  * If the entry at @index is the same as @old, replace it with @entry.
581c5beb07eSMatthew Wilcox  * If the return value is equal to @old, then the exchange was successful.
582c5beb07eSMatthew Wilcox  *
583c5beb07eSMatthew Wilcox  * Context: Any context.  Takes and releases the xa_lock.  May sleep
584c5beb07eSMatthew Wilcox  * if the @gfp flags permit.
585c5beb07eSMatthew Wilcox  * Return: The old value at this index or xa_err() if an error happened.
586c5beb07eSMatthew Wilcox  */
587c5beb07eSMatthew Wilcox static inline void *xa_cmpxchg(struct xarray *xa, unsigned long index,
588c5beb07eSMatthew Wilcox 			void *old, void *entry, gfp_t gfp)
589c5beb07eSMatthew Wilcox {
590c5beb07eSMatthew Wilcox 	void *curr;
591c5beb07eSMatthew Wilcox 
592c5beb07eSMatthew Wilcox 	xa_lock(xa);
593c5beb07eSMatthew Wilcox 	curr = __xa_cmpxchg(xa, index, old, entry, gfp);
594c5beb07eSMatthew Wilcox 	xa_unlock(xa);
595c5beb07eSMatthew Wilcox 
596c5beb07eSMatthew Wilcox 	return curr;
597c5beb07eSMatthew Wilcox }
598c5beb07eSMatthew Wilcox 
599c5beb07eSMatthew Wilcox /**
60055f3f7eaSMatthew Wilcox  * xa_cmpxchg_bh() - Conditionally replace an entry in the XArray.
60155f3f7eaSMatthew Wilcox  * @xa: XArray.
60255f3f7eaSMatthew Wilcox  * @index: Index into array.
60355f3f7eaSMatthew Wilcox  * @old: Old value to test against.
60455f3f7eaSMatthew Wilcox  * @entry: New value to place in array.
60555f3f7eaSMatthew Wilcox  * @gfp: Memory allocation flags.
60655f3f7eaSMatthew Wilcox  *
60755f3f7eaSMatthew Wilcox  * This function is like calling xa_cmpxchg() except it disables softirqs
60855f3f7eaSMatthew Wilcox  * while holding the array lock.
60955f3f7eaSMatthew Wilcox  *
61055f3f7eaSMatthew Wilcox  * Context: Any context.  Takes and releases the xa_lock while
61155f3f7eaSMatthew Wilcox  * disabling softirqs.  May sleep if the @gfp flags permit.
61255f3f7eaSMatthew Wilcox  * Return: The old value at this index or xa_err() if an error happened.
61355f3f7eaSMatthew Wilcox  */
61455f3f7eaSMatthew Wilcox static inline void *xa_cmpxchg_bh(struct xarray *xa, unsigned long index,
61555f3f7eaSMatthew Wilcox 			void *old, void *entry, gfp_t gfp)
61655f3f7eaSMatthew Wilcox {
61755f3f7eaSMatthew Wilcox 	void *curr;
61855f3f7eaSMatthew Wilcox 
61955f3f7eaSMatthew Wilcox 	xa_lock_bh(xa);
62055f3f7eaSMatthew Wilcox 	curr = __xa_cmpxchg(xa, index, old, entry, gfp);
62155f3f7eaSMatthew Wilcox 	xa_unlock_bh(xa);
62255f3f7eaSMatthew Wilcox 
62355f3f7eaSMatthew Wilcox 	return curr;
62455f3f7eaSMatthew Wilcox }
62555f3f7eaSMatthew Wilcox 
62655f3f7eaSMatthew Wilcox /**
62755f3f7eaSMatthew Wilcox  * xa_cmpxchg_irq() - Conditionally replace an entry in the XArray.
62855f3f7eaSMatthew Wilcox  * @xa: XArray.
62955f3f7eaSMatthew Wilcox  * @index: Index into array.
63055f3f7eaSMatthew Wilcox  * @old: Old value to test against.
63155f3f7eaSMatthew Wilcox  * @entry: New value to place in array.
63255f3f7eaSMatthew Wilcox  * @gfp: Memory allocation flags.
63355f3f7eaSMatthew Wilcox  *
63455f3f7eaSMatthew Wilcox  * This function is like calling xa_cmpxchg() except it disables interrupts
63555f3f7eaSMatthew Wilcox  * while holding the array lock.
63655f3f7eaSMatthew Wilcox  *
63755f3f7eaSMatthew Wilcox  * Context: Process context.  Takes and releases the xa_lock while
63855f3f7eaSMatthew Wilcox  * disabling interrupts.  May sleep if the @gfp flags permit.
63955f3f7eaSMatthew Wilcox  * Return: The old value at this index or xa_err() if an error happened.
64055f3f7eaSMatthew Wilcox  */
64155f3f7eaSMatthew Wilcox static inline void *xa_cmpxchg_irq(struct xarray *xa, unsigned long index,
64255f3f7eaSMatthew Wilcox 			void *old, void *entry, gfp_t gfp)
64355f3f7eaSMatthew Wilcox {
64455f3f7eaSMatthew Wilcox 	void *curr;
64555f3f7eaSMatthew Wilcox 
64655f3f7eaSMatthew Wilcox 	xa_lock_irq(xa);
64755f3f7eaSMatthew Wilcox 	curr = __xa_cmpxchg(xa, index, old, entry, gfp);
64855f3f7eaSMatthew Wilcox 	xa_unlock_irq(xa);
64955f3f7eaSMatthew Wilcox 
65055f3f7eaSMatthew Wilcox 	return curr;
65155f3f7eaSMatthew Wilcox }
65255f3f7eaSMatthew Wilcox 
65355f3f7eaSMatthew Wilcox /**
654c5beb07eSMatthew Wilcox  * xa_insert() - Store this entry in the XArray unless another entry is
655c5beb07eSMatthew Wilcox  *			already present.
656c5beb07eSMatthew Wilcox  * @xa: XArray.
657c5beb07eSMatthew Wilcox  * @index: Index into array.
658c5beb07eSMatthew Wilcox  * @entry: New entry.
659c5beb07eSMatthew Wilcox  * @gfp: Memory allocation flags.
660c5beb07eSMatthew Wilcox  *
661b0606fedSMatthew Wilcox  * Inserting a NULL entry will store a reserved entry (like xa_reserve())
662b0606fedSMatthew Wilcox  * if no entry is present.  Inserting will fail if a reserved entry is
663b0606fedSMatthew Wilcox  * present, even though loading from this index will return NULL.
664c5beb07eSMatthew Wilcox  *
665b0606fedSMatthew Wilcox  * Context: Any context.  Takes and releases the xa_lock.  May sleep if
666b0606fedSMatthew Wilcox  * the @gfp flags permit.
667c5beb07eSMatthew Wilcox  * Return: 0 if the store succeeded.  -EEXIST if another entry was present.
668c5beb07eSMatthew Wilcox  * -ENOMEM if memory could not be allocated.
669c5beb07eSMatthew Wilcox  */
670c5beb07eSMatthew Wilcox static inline int xa_insert(struct xarray *xa, unsigned long index,
671c5beb07eSMatthew Wilcox 		void *entry, gfp_t gfp)
672c5beb07eSMatthew Wilcox {
673b0606fedSMatthew Wilcox 	int err;
674b0606fedSMatthew Wilcox 
675b0606fedSMatthew Wilcox 	xa_lock(xa);
676b0606fedSMatthew Wilcox 	err = __xa_insert(xa, index, entry, gfp);
677b0606fedSMatthew Wilcox 	xa_unlock(xa);
678b0606fedSMatthew Wilcox 
679b0606fedSMatthew Wilcox 	return err;
680b0606fedSMatthew Wilcox }
681b0606fedSMatthew Wilcox 
682b0606fedSMatthew Wilcox /**
683b0606fedSMatthew Wilcox  * xa_insert_bh() - Store this entry in the XArray unless another entry is
684b0606fedSMatthew Wilcox  *			already present.
685b0606fedSMatthew Wilcox  * @xa: XArray.
686b0606fedSMatthew Wilcox  * @index: Index into array.
687b0606fedSMatthew Wilcox  * @entry: New entry.
688b0606fedSMatthew Wilcox  * @gfp: Memory allocation flags.
689b0606fedSMatthew Wilcox  *
690b0606fedSMatthew Wilcox  * Inserting a NULL entry will store a reserved entry (like xa_reserve())
691b0606fedSMatthew Wilcox  * if no entry is present.  Inserting will fail if a reserved entry is
692b0606fedSMatthew Wilcox  * present, even though loading from this index will return NULL.
693b0606fedSMatthew Wilcox  *
694b0606fedSMatthew Wilcox  * Context: Any context.  Takes and releases the xa_lock while
695b0606fedSMatthew Wilcox  * disabling softirqs.  May sleep if the @gfp flags permit.
696b0606fedSMatthew Wilcox  * Return: 0 if the store succeeded.  -EEXIST if another entry was present.
697b0606fedSMatthew Wilcox  * -ENOMEM if memory could not be allocated.
698b0606fedSMatthew Wilcox  */
699b0606fedSMatthew Wilcox static inline int xa_insert_bh(struct xarray *xa, unsigned long index,
700b0606fedSMatthew Wilcox 		void *entry, gfp_t gfp)
701b0606fedSMatthew Wilcox {
702b0606fedSMatthew Wilcox 	int err;
703b0606fedSMatthew Wilcox 
704b0606fedSMatthew Wilcox 	xa_lock_bh(xa);
705b0606fedSMatthew Wilcox 	err = __xa_insert(xa, index, entry, gfp);
706b0606fedSMatthew Wilcox 	xa_unlock_bh(xa);
707b0606fedSMatthew Wilcox 
708b0606fedSMatthew Wilcox 	return err;
709b0606fedSMatthew Wilcox }
710b0606fedSMatthew Wilcox 
711b0606fedSMatthew Wilcox /**
712b0606fedSMatthew Wilcox  * xa_insert_irq() - Store this entry in the XArray unless another entry is
713b0606fedSMatthew Wilcox  *			already present.
714b0606fedSMatthew Wilcox  * @xa: XArray.
715b0606fedSMatthew Wilcox  * @index: Index into array.
716b0606fedSMatthew Wilcox  * @entry: New entry.
717b0606fedSMatthew Wilcox  * @gfp: Memory allocation flags.
718b0606fedSMatthew Wilcox  *
719b0606fedSMatthew Wilcox  * Inserting a NULL entry will store a reserved entry (like xa_reserve())
720b0606fedSMatthew Wilcox  * if no entry is present.  Inserting will fail if a reserved entry is
721b0606fedSMatthew Wilcox  * present, even though loading from this index will return NULL.
722b0606fedSMatthew Wilcox  *
723b0606fedSMatthew Wilcox  * Context: Process context.  Takes and releases the xa_lock while
724b0606fedSMatthew Wilcox  * disabling interrupts.  May sleep if the @gfp flags permit.
725b0606fedSMatthew Wilcox  * Return: 0 if the store succeeded.  -EEXIST if another entry was present.
726b0606fedSMatthew Wilcox  * -ENOMEM if memory could not be allocated.
727b0606fedSMatthew Wilcox  */
728b0606fedSMatthew Wilcox static inline int xa_insert_irq(struct xarray *xa, unsigned long index,
729b0606fedSMatthew Wilcox 		void *entry, gfp_t gfp)
730b0606fedSMatthew Wilcox {
731b0606fedSMatthew Wilcox 	int err;
732b0606fedSMatthew Wilcox 
733b0606fedSMatthew Wilcox 	xa_lock_irq(xa);
734b0606fedSMatthew Wilcox 	err = __xa_insert(xa, index, entry, gfp);
735b0606fedSMatthew Wilcox 	xa_unlock_irq(xa);
736b0606fedSMatthew Wilcox 
737b0606fedSMatthew Wilcox 	return err;
738c5beb07eSMatthew Wilcox }
739c5beb07eSMatthew Wilcox 
740c5beb07eSMatthew Wilcox /**
741371c752dSMatthew Wilcox  * xa_alloc() - Find somewhere to store this entry in the XArray.
742371c752dSMatthew Wilcox  * @xa: XArray.
743371c752dSMatthew Wilcox  * @id: Pointer to ID.
744371c752dSMatthew Wilcox  * @max: Maximum ID to allocate (inclusive).
745371c752dSMatthew Wilcox  * @entry: New entry.
746371c752dSMatthew Wilcox  * @gfp: Memory allocation flags.
747371c752dSMatthew Wilcox  *
748371c752dSMatthew Wilcox  * Allocates an unused ID in the range specified by @id and @max.
749371c752dSMatthew Wilcox  * Updates the @id pointer with the index, then stores the entry at that
750371c752dSMatthew Wilcox  * index.  A concurrent lookup will not see an uninitialised @id.
751371c752dSMatthew Wilcox  *
752371c752dSMatthew Wilcox  * Context: Process context.  Takes and releases the xa_lock.  May sleep if
753371c752dSMatthew Wilcox  * the @gfp flags permit.
754371c752dSMatthew Wilcox  * Return: 0 on success, -ENOMEM if memory allocation fails or -ENOSPC if
755371c752dSMatthew Wilcox  * there is no more space in the XArray.
756371c752dSMatthew Wilcox  */
757371c752dSMatthew Wilcox static inline int xa_alloc(struct xarray *xa, u32 *id, u32 max, void *entry,
758371c752dSMatthew Wilcox 		gfp_t gfp)
759371c752dSMatthew Wilcox {
760371c752dSMatthew Wilcox 	int err;
761371c752dSMatthew Wilcox 
762371c752dSMatthew Wilcox 	xa_lock(xa);
763371c752dSMatthew Wilcox 	err = __xa_alloc(xa, id, max, entry, gfp);
764371c752dSMatthew Wilcox 	xa_unlock(xa);
765371c752dSMatthew Wilcox 
766371c752dSMatthew Wilcox 	return err;
767371c752dSMatthew Wilcox }
768371c752dSMatthew Wilcox 
769371c752dSMatthew Wilcox /**
770371c752dSMatthew Wilcox  * xa_alloc_bh() - Find somewhere to store this entry in the XArray.
771371c752dSMatthew Wilcox  * @xa: XArray.
772371c752dSMatthew Wilcox  * @id: Pointer to ID.
773371c752dSMatthew Wilcox  * @max: Maximum ID to allocate (inclusive).
774371c752dSMatthew Wilcox  * @entry: New entry.
775371c752dSMatthew Wilcox  * @gfp: Memory allocation flags.
776371c752dSMatthew Wilcox  *
777371c752dSMatthew Wilcox  * Allocates an unused ID in the range specified by @id and @max.
778371c752dSMatthew Wilcox  * Updates the @id pointer with the index, then stores the entry at that
779371c752dSMatthew Wilcox  * index.  A concurrent lookup will not see an uninitialised @id.
780371c752dSMatthew Wilcox  *
781804dfaf0SMatthew Wilcox  * Context: Any context.  Takes and releases the xa_lock while
782371c752dSMatthew Wilcox  * disabling softirqs.  May sleep if the @gfp flags permit.
783371c752dSMatthew Wilcox  * Return: 0 on success, -ENOMEM if memory allocation fails or -ENOSPC if
784371c752dSMatthew Wilcox  * there is no more space in the XArray.
785371c752dSMatthew Wilcox  */
786371c752dSMatthew Wilcox static inline int xa_alloc_bh(struct xarray *xa, u32 *id, u32 max, void *entry,
787371c752dSMatthew Wilcox 		gfp_t gfp)
788371c752dSMatthew Wilcox {
789371c752dSMatthew Wilcox 	int err;
790371c752dSMatthew Wilcox 
791371c752dSMatthew Wilcox 	xa_lock_bh(xa);
792371c752dSMatthew Wilcox 	err = __xa_alloc(xa, id, max, entry, gfp);
793371c752dSMatthew Wilcox 	xa_unlock_bh(xa);
794371c752dSMatthew Wilcox 
795371c752dSMatthew Wilcox 	return err;
796371c752dSMatthew Wilcox }
797371c752dSMatthew Wilcox 
798371c752dSMatthew Wilcox /**
799371c752dSMatthew Wilcox  * xa_alloc_irq() - Find somewhere to store this entry in the XArray.
800371c752dSMatthew Wilcox  * @xa: XArray.
801371c752dSMatthew Wilcox  * @id: Pointer to ID.
802371c752dSMatthew Wilcox  * @max: Maximum ID to allocate (inclusive).
803371c752dSMatthew Wilcox  * @entry: New entry.
804371c752dSMatthew Wilcox  * @gfp: Memory allocation flags.
805371c752dSMatthew Wilcox  *
806371c752dSMatthew Wilcox  * Allocates an unused ID in the range specified by @id and @max.
807371c752dSMatthew Wilcox  * Updates the @id pointer with the index, then stores the entry at that
808371c752dSMatthew Wilcox  * index.  A concurrent lookup will not see an uninitialised @id.
809371c752dSMatthew Wilcox  *
810371c752dSMatthew Wilcox  * Context: Process context.  Takes and releases the xa_lock while
811371c752dSMatthew Wilcox  * disabling interrupts.  May sleep if the @gfp flags permit.
812371c752dSMatthew Wilcox  * Return: 0 on success, -ENOMEM if memory allocation fails or -ENOSPC if
813371c752dSMatthew Wilcox  * there is no more space in the XArray.
814371c752dSMatthew Wilcox  */
815371c752dSMatthew Wilcox static inline int xa_alloc_irq(struct xarray *xa, u32 *id, u32 max, void *entry,
816371c752dSMatthew Wilcox 		gfp_t gfp)
817371c752dSMatthew Wilcox {
818371c752dSMatthew Wilcox 	int err;
819371c752dSMatthew Wilcox 
820371c752dSMatthew Wilcox 	xa_lock_irq(xa);
821371c752dSMatthew Wilcox 	err = __xa_alloc(xa, id, max, entry, gfp);
822371c752dSMatthew Wilcox 	xa_unlock_irq(xa);
823371c752dSMatthew Wilcox 
824371c752dSMatthew Wilcox 	return err;
825371c752dSMatthew Wilcox }
826371c752dSMatthew Wilcox 
8274c0608f4SMatthew Wilcox /**
8284c0608f4SMatthew Wilcox  * xa_reserve() - Reserve this index in the XArray.
8294c0608f4SMatthew Wilcox  * @xa: XArray.
8304c0608f4SMatthew Wilcox  * @index: Index into array.
8314c0608f4SMatthew Wilcox  * @gfp: Memory allocation flags.
8324c0608f4SMatthew Wilcox  *
8334c0608f4SMatthew Wilcox  * Ensures there is somewhere to store an entry at @index in the array.
8344c0608f4SMatthew Wilcox  * If there is already something stored at @index, this function does
8354c0608f4SMatthew Wilcox  * nothing.  If there was nothing there, the entry is marked as reserved.
8364c0608f4SMatthew Wilcox  * Loading from a reserved entry returns a %NULL pointer.
8374c0608f4SMatthew Wilcox  *
8384c0608f4SMatthew Wilcox  * If you do not use the entry that you have reserved, call xa_release()
8394c0608f4SMatthew Wilcox  * or xa_erase() to free any unnecessary memory.
8404c0608f4SMatthew Wilcox  *
8414c0608f4SMatthew Wilcox  * Context: Any context.  Takes and releases the xa_lock.
8424c0608f4SMatthew Wilcox  * May sleep if the @gfp flags permit.
8434c0608f4SMatthew Wilcox  * Return: 0 if the reservation succeeded or -ENOMEM if it failed.
8444c0608f4SMatthew Wilcox  */
8454c0608f4SMatthew Wilcox static inline
8464c0608f4SMatthew Wilcox int xa_reserve(struct xarray *xa, unsigned long index, gfp_t gfp)
8474c0608f4SMatthew Wilcox {
8484c0608f4SMatthew Wilcox 	int ret;
8494c0608f4SMatthew Wilcox 
8504c0608f4SMatthew Wilcox 	xa_lock(xa);
8514c0608f4SMatthew Wilcox 	ret = __xa_reserve(xa, index, gfp);
8524c0608f4SMatthew Wilcox 	xa_unlock(xa);
8534c0608f4SMatthew Wilcox 
8544c0608f4SMatthew Wilcox 	return ret;
8554c0608f4SMatthew Wilcox }
8564c0608f4SMatthew Wilcox 
8574c0608f4SMatthew Wilcox /**
8584c0608f4SMatthew Wilcox  * xa_reserve_bh() - Reserve this index in the XArray.
8594c0608f4SMatthew Wilcox  * @xa: XArray.
8604c0608f4SMatthew Wilcox  * @index: Index into array.
8614c0608f4SMatthew Wilcox  * @gfp: Memory allocation flags.
8624c0608f4SMatthew Wilcox  *
8634c0608f4SMatthew Wilcox  * A softirq-disabling version of xa_reserve().
8644c0608f4SMatthew Wilcox  *
8654c0608f4SMatthew Wilcox  * Context: Any context.  Takes and releases the xa_lock while
8664c0608f4SMatthew Wilcox  * disabling softirqs.
8674c0608f4SMatthew Wilcox  * Return: 0 if the reservation succeeded or -ENOMEM if it failed.
8684c0608f4SMatthew Wilcox  */
8694c0608f4SMatthew Wilcox static inline
8704c0608f4SMatthew Wilcox int xa_reserve_bh(struct xarray *xa, unsigned long index, gfp_t gfp)
8714c0608f4SMatthew Wilcox {
8724c0608f4SMatthew Wilcox 	int ret;
8734c0608f4SMatthew Wilcox 
8744c0608f4SMatthew Wilcox 	xa_lock_bh(xa);
8754c0608f4SMatthew Wilcox 	ret = __xa_reserve(xa, index, gfp);
8764c0608f4SMatthew Wilcox 	xa_unlock_bh(xa);
8774c0608f4SMatthew Wilcox 
8784c0608f4SMatthew Wilcox 	return ret;
8794c0608f4SMatthew Wilcox }
8804c0608f4SMatthew Wilcox 
8814c0608f4SMatthew Wilcox /**
8824c0608f4SMatthew Wilcox  * xa_reserve_irq() - Reserve this index in the XArray.
8834c0608f4SMatthew Wilcox  * @xa: XArray.
8844c0608f4SMatthew Wilcox  * @index: Index into array.
8854c0608f4SMatthew Wilcox  * @gfp: Memory allocation flags.
8864c0608f4SMatthew Wilcox  *
8874c0608f4SMatthew Wilcox  * An interrupt-disabling version of xa_reserve().
8884c0608f4SMatthew Wilcox  *
8894c0608f4SMatthew Wilcox  * Context: Process context.  Takes and releases the xa_lock while
8904c0608f4SMatthew Wilcox  * disabling interrupts.
8914c0608f4SMatthew Wilcox  * Return: 0 if the reservation succeeded or -ENOMEM if it failed.
8924c0608f4SMatthew Wilcox  */
8934c0608f4SMatthew Wilcox static inline
8944c0608f4SMatthew Wilcox int xa_reserve_irq(struct xarray *xa, unsigned long index, gfp_t gfp)
8954c0608f4SMatthew Wilcox {
8964c0608f4SMatthew Wilcox 	int ret;
8974c0608f4SMatthew Wilcox 
8984c0608f4SMatthew Wilcox 	xa_lock_irq(xa);
8994c0608f4SMatthew Wilcox 	ret = __xa_reserve(xa, index, gfp);
9004c0608f4SMatthew Wilcox 	xa_unlock_irq(xa);
9014c0608f4SMatthew Wilcox 
9024c0608f4SMatthew Wilcox 	return ret;
9034c0608f4SMatthew Wilcox }
9044c0608f4SMatthew Wilcox 
905c5beb07eSMatthew Wilcox /**
906c5beb07eSMatthew Wilcox  * xa_release() - Release a reserved entry.
907c5beb07eSMatthew Wilcox  * @xa: XArray.
908c5beb07eSMatthew Wilcox  * @index: Index of entry.
909c5beb07eSMatthew Wilcox  *
910c5beb07eSMatthew Wilcox  * After calling xa_reserve(), you can call this function to release the
911c5beb07eSMatthew Wilcox  * reservation.  If the entry at @index has been stored to, this function
912c5beb07eSMatthew Wilcox  * will do nothing.
913c5beb07eSMatthew Wilcox  */
914c5beb07eSMatthew Wilcox static inline void xa_release(struct xarray *xa, unsigned long index)
915c5beb07eSMatthew Wilcox {
916c5beb07eSMatthew Wilcox 	xa_cmpxchg(xa, index, NULL, NULL, 0);
917c5beb07eSMatthew Wilcox }
918c5beb07eSMatthew Wilcox 
91902c02bf1SMatthew Wilcox /* Everything below here is the Advanced API.  Proceed with caution. */
92002c02bf1SMatthew Wilcox 
92102c02bf1SMatthew Wilcox /*
92202c02bf1SMatthew Wilcox  * The xarray is constructed out of a set of 'chunks' of pointers.  Choosing
92302c02bf1SMatthew Wilcox  * the best chunk size requires some tradeoffs.  A power of two recommends
92402c02bf1SMatthew Wilcox  * itself so that we can walk the tree based purely on shifts and masks.
92502c02bf1SMatthew Wilcox  * Generally, the larger the better; as the number of slots per level of the
92602c02bf1SMatthew Wilcox  * tree increases, the less tall the tree needs to be.  But that needs to be
92702c02bf1SMatthew Wilcox  * balanced against the memory consumption of each node.  On a 64-bit system,
92802c02bf1SMatthew Wilcox  * xa_node is currently 576 bytes, and we get 7 of them per 4kB page.  If we
92902c02bf1SMatthew Wilcox  * doubled the number of slots per node, we'd get only 3 nodes per 4kB page.
93002c02bf1SMatthew Wilcox  */
93102c02bf1SMatthew Wilcox #ifndef XA_CHUNK_SHIFT
93202c02bf1SMatthew Wilcox #define XA_CHUNK_SHIFT		(CONFIG_BASE_SMALL ? 4 : 6)
93302c02bf1SMatthew Wilcox #endif
93402c02bf1SMatthew Wilcox #define XA_CHUNK_SIZE		(1UL << XA_CHUNK_SHIFT)
93502c02bf1SMatthew Wilcox #define XA_CHUNK_MASK		(XA_CHUNK_SIZE - 1)
93601959dfeSMatthew Wilcox #define XA_MAX_MARKS		3
93701959dfeSMatthew Wilcox #define XA_MARK_LONGS		DIV_ROUND_UP(XA_CHUNK_SIZE, BITS_PER_LONG)
93801959dfeSMatthew Wilcox 
93901959dfeSMatthew Wilcox /*
94001959dfeSMatthew Wilcox  * @count is the count of every non-NULL element in the ->slots array
94101959dfeSMatthew Wilcox  * whether that is a value entry, a retry entry, a user pointer,
94201959dfeSMatthew Wilcox  * a sibling entry or a pointer to the next level of the tree.
94301959dfeSMatthew Wilcox  * @nr_values is the count of every element in ->slots which is
94401959dfeSMatthew Wilcox  * either a value entry or a sibling of a value entry.
94501959dfeSMatthew Wilcox  */
94601959dfeSMatthew Wilcox struct xa_node {
94701959dfeSMatthew Wilcox 	unsigned char	shift;		/* Bits remaining in each slot */
94801959dfeSMatthew Wilcox 	unsigned char	offset;		/* Slot offset in parent */
94901959dfeSMatthew Wilcox 	unsigned char	count;		/* Total entry count */
95001959dfeSMatthew Wilcox 	unsigned char	nr_values;	/* Value entry count */
95101959dfeSMatthew Wilcox 	struct xa_node __rcu *parent;	/* NULL at top of tree */
95201959dfeSMatthew Wilcox 	struct xarray	*array;		/* The array we belong to */
95301959dfeSMatthew Wilcox 	union {
95401959dfeSMatthew Wilcox 		struct list_head private_list;	/* For tree user */
95501959dfeSMatthew Wilcox 		struct rcu_head	rcu_head;	/* Used when freeing node */
95601959dfeSMatthew Wilcox 	};
95701959dfeSMatthew Wilcox 	void __rcu	*slots[XA_CHUNK_SIZE];
95801959dfeSMatthew Wilcox 	union {
95901959dfeSMatthew Wilcox 		unsigned long	tags[XA_MAX_MARKS][XA_MARK_LONGS];
96001959dfeSMatthew Wilcox 		unsigned long	marks[XA_MAX_MARKS][XA_MARK_LONGS];
96101959dfeSMatthew Wilcox 	};
96201959dfeSMatthew Wilcox };
96302c02bf1SMatthew Wilcox 
964ad3d6c72SMatthew Wilcox void xa_dump(const struct xarray *);
965ad3d6c72SMatthew Wilcox void xa_dump_node(const struct xa_node *);
966ad3d6c72SMatthew Wilcox 
967ad3d6c72SMatthew Wilcox #ifdef XA_DEBUG
968ad3d6c72SMatthew Wilcox #define XA_BUG_ON(xa, x) do {					\
969ad3d6c72SMatthew Wilcox 		if (x) {					\
970ad3d6c72SMatthew Wilcox 			xa_dump(xa);				\
971ad3d6c72SMatthew Wilcox 			BUG();					\
972ad3d6c72SMatthew Wilcox 		}						\
973ad3d6c72SMatthew Wilcox 	} while (0)
974ad3d6c72SMatthew Wilcox #define XA_NODE_BUG_ON(node, x) do {				\
975ad3d6c72SMatthew Wilcox 		if (x) {					\
976ad3d6c72SMatthew Wilcox 			if (node) xa_dump_node(node);		\
977ad3d6c72SMatthew Wilcox 			BUG();					\
978ad3d6c72SMatthew Wilcox 		}						\
979ad3d6c72SMatthew Wilcox 	} while (0)
980ad3d6c72SMatthew Wilcox #else
981ad3d6c72SMatthew Wilcox #define XA_BUG_ON(xa, x)	do { } while (0)
982ad3d6c72SMatthew Wilcox #define XA_NODE_BUG_ON(node, x)	do { } while (0)
983ad3d6c72SMatthew Wilcox #endif
984ad3d6c72SMatthew Wilcox 
985ad3d6c72SMatthew Wilcox /* Private */
986ad3d6c72SMatthew Wilcox static inline void *xa_head(const struct xarray *xa)
987ad3d6c72SMatthew Wilcox {
988ad3d6c72SMatthew Wilcox 	return rcu_dereference_check(xa->xa_head,
989ad3d6c72SMatthew Wilcox 						lockdep_is_held(&xa->xa_lock));
990ad3d6c72SMatthew Wilcox }
991ad3d6c72SMatthew Wilcox 
992ad3d6c72SMatthew Wilcox /* Private */
993ad3d6c72SMatthew Wilcox static inline void *xa_head_locked(const struct xarray *xa)
994ad3d6c72SMatthew Wilcox {
995ad3d6c72SMatthew Wilcox 	return rcu_dereference_protected(xa->xa_head,
996ad3d6c72SMatthew Wilcox 						lockdep_is_held(&xa->xa_lock));
997ad3d6c72SMatthew Wilcox }
998ad3d6c72SMatthew Wilcox 
999ad3d6c72SMatthew Wilcox /* Private */
1000ad3d6c72SMatthew Wilcox static inline void *xa_entry(const struct xarray *xa,
1001ad3d6c72SMatthew Wilcox 				const struct xa_node *node, unsigned int offset)
1002ad3d6c72SMatthew Wilcox {
1003ad3d6c72SMatthew Wilcox 	XA_NODE_BUG_ON(node, offset >= XA_CHUNK_SIZE);
1004ad3d6c72SMatthew Wilcox 	return rcu_dereference_check(node->slots[offset],
1005ad3d6c72SMatthew Wilcox 						lockdep_is_held(&xa->xa_lock));
1006ad3d6c72SMatthew Wilcox }
1007ad3d6c72SMatthew Wilcox 
1008ad3d6c72SMatthew Wilcox /* Private */
1009ad3d6c72SMatthew Wilcox static inline void *xa_entry_locked(const struct xarray *xa,
1010ad3d6c72SMatthew Wilcox 				const struct xa_node *node, unsigned int offset)
1011ad3d6c72SMatthew Wilcox {
1012ad3d6c72SMatthew Wilcox 	XA_NODE_BUG_ON(node, offset >= XA_CHUNK_SIZE);
1013ad3d6c72SMatthew Wilcox 	return rcu_dereference_protected(node->slots[offset],
1014ad3d6c72SMatthew Wilcox 						lockdep_is_held(&xa->xa_lock));
1015ad3d6c72SMatthew Wilcox }
1016ad3d6c72SMatthew Wilcox 
1017ad3d6c72SMatthew Wilcox /* Private */
10189b89a035SMatthew Wilcox static inline struct xa_node *xa_parent(const struct xarray *xa,
10199b89a035SMatthew Wilcox 					const struct xa_node *node)
10209b89a035SMatthew Wilcox {
10219b89a035SMatthew Wilcox 	return rcu_dereference_check(node->parent,
10229b89a035SMatthew Wilcox 						lockdep_is_held(&xa->xa_lock));
10239b89a035SMatthew Wilcox }
10249b89a035SMatthew Wilcox 
10259b89a035SMatthew Wilcox /* Private */
10269b89a035SMatthew Wilcox static inline struct xa_node *xa_parent_locked(const struct xarray *xa,
10279b89a035SMatthew Wilcox 					const struct xa_node *node)
10289b89a035SMatthew Wilcox {
10299b89a035SMatthew Wilcox 	return rcu_dereference_protected(node->parent,
10309b89a035SMatthew Wilcox 						lockdep_is_held(&xa->xa_lock));
10319b89a035SMatthew Wilcox }
10329b89a035SMatthew Wilcox 
10339b89a035SMatthew Wilcox /* Private */
103458d6ea30SMatthew Wilcox static inline void *xa_mk_node(const struct xa_node *node)
103558d6ea30SMatthew Wilcox {
103658d6ea30SMatthew Wilcox 	return (void *)((unsigned long)node | 2);
103758d6ea30SMatthew Wilcox }
103858d6ea30SMatthew Wilcox 
103958d6ea30SMatthew Wilcox /* Private */
1040ad3d6c72SMatthew Wilcox static inline struct xa_node *xa_to_node(const void *entry)
1041ad3d6c72SMatthew Wilcox {
1042ad3d6c72SMatthew Wilcox 	return (struct xa_node *)((unsigned long)entry - 2);
1043ad3d6c72SMatthew Wilcox }
1044ad3d6c72SMatthew Wilcox 
104502c02bf1SMatthew Wilcox /* Private */
104602c02bf1SMatthew Wilcox static inline bool xa_is_node(const void *entry)
104702c02bf1SMatthew Wilcox {
104802c02bf1SMatthew Wilcox 	return xa_is_internal(entry) && (unsigned long)entry > 4096;
104902c02bf1SMatthew Wilcox }
105002c02bf1SMatthew Wilcox 
105102c02bf1SMatthew Wilcox /* Private */
105202c02bf1SMatthew Wilcox static inline void *xa_mk_sibling(unsigned int offset)
105302c02bf1SMatthew Wilcox {
105402c02bf1SMatthew Wilcox 	return xa_mk_internal(offset);
105502c02bf1SMatthew Wilcox }
105602c02bf1SMatthew Wilcox 
105702c02bf1SMatthew Wilcox /* Private */
105802c02bf1SMatthew Wilcox static inline unsigned long xa_to_sibling(const void *entry)
105902c02bf1SMatthew Wilcox {
106002c02bf1SMatthew Wilcox 	return xa_to_internal(entry);
106102c02bf1SMatthew Wilcox }
106202c02bf1SMatthew Wilcox 
106302c02bf1SMatthew Wilcox /**
106402c02bf1SMatthew Wilcox  * xa_is_sibling() - Is the entry a sibling entry?
106502c02bf1SMatthew Wilcox  * @entry: Entry retrieved from the XArray
106602c02bf1SMatthew Wilcox  *
106702c02bf1SMatthew Wilcox  * Return: %true if the entry is a sibling entry.
106802c02bf1SMatthew Wilcox  */
106902c02bf1SMatthew Wilcox static inline bool xa_is_sibling(const void *entry)
107002c02bf1SMatthew Wilcox {
107102c02bf1SMatthew Wilcox 	return IS_ENABLED(CONFIG_XARRAY_MULTI) && xa_is_internal(entry) &&
107202c02bf1SMatthew Wilcox 		(entry < xa_mk_sibling(XA_CHUNK_SIZE - 1));
107302c02bf1SMatthew Wilcox }
107402c02bf1SMatthew Wilcox 
107576b4e529SMatthew Wilcox #define XA_RETRY_ENTRY		xa_mk_internal(256)
107676b4e529SMatthew Wilcox #define XA_ZERO_ENTRY		xa_mk_internal(257)
10779f14d4f1SMatthew Wilcox 
10789f14d4f1SMatthew Wilcox /**
10799f14d4f1SMatthew Wilcox  * xa_is_zero() - Is the entry a zero entry?
10809f14d4f1SMatthew Wilcox  * @entry: Entry retrieved from the XArray
10819f14d4f1SMatthew Wilcox  *
10829f14d4f1SMatthew Wilcox  * Return: %true if the entry is a zero entry.
10839f14d4f1SMatthew Wilcox  */
10849f14d4f1SMatthew Wilcox static inline bool xa_is_zero(const void *entry)
10859f14d4f1SMatthew Wilcox {
10869f14d4f1SMatthew Wilcox 	return unlikely(entry == XA_ZERO_ENTRY);
10879f14d4f1SMatthew Wilcox }
108802c02bf1SMatthew Wilcox 
1089ad3d6c72SMatthew Wilcox /**
1090ad3d6c72SMatthew Wilcox  * xa_is_retry() - Is the entry a retry entry?
1091ad3d6c72SMatthew Wilcox  * @entry: Entry retrieved from the XArray
1092ad3d6c72SMatthew Wilcox  *
1093ad3d6c72SMatthew Wilcox  * Return: %true if the entry is a retry entry.
1094ad3d6c72SMatthew Wilcox  */
1095ad3d6c72SMatthew Wilcox static inline bool xa_is_retry(const void *entry)
1096ad3d6c72SMatthew Wilcox {
1097ad3d6c72SMatthew Wilcox 	return unlikely(entry == XA_RETRY_ENTRY);
1098ad3d6c72SMatthew Wilcox }
1099ad3d6c72SMatthew Wilcox 
1100ad3d6c72SMatthew Wilcox /**
110176b4e529SMatthew Wilcox  * xa_is_advanced() - Is the entry only permitted for the advanced API?
110276b4e529SMatthew Wilcox  * @entry: Entry to be stored in the XArray.
110376b4e529SMatthew Wilcox  *
110476b4e529SMatthew Wilcox  * Return: %true if the entry cannot be stored by the normal API.
110576b4e529SMatthew Wilcox  */
110676b4e529SMatthew Wilcox static inline bool xa_is_advanced(const void *entry)
110776b4e529SMatthew Wilcox {
110876b4e529SMatthew Wilcox 	return xa_is_internal(entry) && (entry <= XA_RETRY_ENTRY);
110976b4e529SMatthew Wilcox }
111076b4e529SMatthew Wilcox 
111176b4e529SMatthew Wilcox /**
1112ad3d6c72SMatthew Wilcox  * typedef xa_update_node_t - A callback function from the XArray.
1113ad3d6c72SMatthew Wilcox  * @node: The node which is being processed
1114ad3d6c72SMatthew Wilcox  *
1115ad3d6c72SMatthew Wilcox  * This function is called every time the XArray updates the count of
1116ad3d6c72SMatthew Wilcox  * present and value entries in a node.  It allows advanced users to
1117ad3d6c72SMatthew Wilcox  * maintain the private_list in the node.
1118ad3d6c72SMatthew Wilcox  *
1119ad3d6c72SMatthew Wilcox  * Context: The xa_lock is held and interrupts may be disabled.
1120ad3d6c72SMatthew Wilcox  *	    Implementations should not drop the xa_lock, nor re-enable
1121ad3d6c72SMatthew Wilcox  *	    interrupts.
1122ad3d6c72SMatthew Wilcox  */
1123ad3d6c72SMatthew Wilcox typedef void (*xa_update_node_t)(struct xa_node *node);
1124ad3d6c72SMatthew Wilcox 
1125ad3d6c72SMatthew Wilcox /*
1126ad3d6c72SMatthew Wilcox  * The xa_state is opaque to its users.  It contains various different pieces
1127ad3d6c72SMatthew Wilcox  * of state involved in the current operation on the XArray.  It should be
1128ad3d6c72SMatthew Wilcox  * declared on the stack and passed between the various internal routines.
1129ad3d6c72SMatthew Wilcox  * The various elements in it should not be accessed directly, but only
1130ad3d6c72SMatthew Wilcox  * through the provided accessor functions.  The below documentation is for
1131ad3d6c72SMatthew Wilcox  * the benefit of those working on the code, not for users of the XArray.
1132ad3d6c72SMatthew Wilcox  *
1133ad3d6c72SMatthew Wilcox  * @xa_node usually points to the xa_node containing the slot we're operating
1134ad3d6c72SMatthew Wilcox  * on (and @xa_offset is the offset in the slots array).  If there is a
1135ad3d6c72SMatthew Wilcox  * single entry in the array at index 0, there are no allocated xa_nodes to
1136ad3d6c72SMatthew Wilcox  * point to, and so we store %NULL in @xa_node.  @xa_node is set to
1137ad3d6c72SMatthew Wilcox  * the value %XAS_RESTART if the xa_state is not walked to the correct
1138ad3d6c72SMatthew Wilcox  * position in the tree of nodes for this operation.  If an error occurs
1139ad3d6c72SMatthew Wilcox  * during an operation, it is set to an %XAS_ERROR value.  If we run off the
1140ad3d6c72SMatthew Wilcox  * end of the allocated nodes, it is set to %XAS_BOUNDS.
1141ad3d6c72SMatthew Wilcox  */
1142ad3d6c72SMatthew Wilcox struct xa_state {
1143ad3d6c72SMatthew Wilcox 	struct xarray *xa;
1144ad3d6c72SMatthew Wilcox 	unsigned long xa_index;
1145ad3d6c72SMatthew Wilcox 	unsigned char xa_shift;
1146ad3d6c72SMatthew Wilcox 	unsigned char xa_sibs;
1147ad3d6c72SMatthew Wilcox 	unsigned char xa_offset;
1148ad3d6c72SMatthew Wilcox 	unsigned char xa_pad;		/* Helps gcc generate better code */
1149ad3d6c72SMatthew Wilcox 	struct xa_node *xa_node;
1150ad3d6c72SMatthew Wilcox 	struct xa_node *xa_alloc;
1151ad3d6c72SMatthew Wilcox 	xa_update_node_t xa_update;
1152ad3d6c72SMatthew Wilcox };
1153ad3d6c72SMatthew Wilcox 
1154ad3d6c72SMatthew Wilcox /*
1155ad3d6c72SMatthew Wilcox  * We encode errnos in the xas->xa_node.  If an error has happened, we need to
1156ad3d6c72SMatthew Wilcox  * drop the lock to fix it, and once we've done so the xa_state is invalid.
1157ad3d6c72SMatthew Wilcox  */
1158ad3d6c72SMatthew Wilcox #define XA_ERROR(errno) ((struct xa_node *)(((unsigned long)errno << 2) | 2UL))
1159ad3d6c72SMatthew Wilcox #define XAS_BOUNDS	((struct xa_node *)1UL)
1160ad3d6c72SMatthew Wilcox #define XAS_RESTART	((struct xa_node *)3UL)
1161ad3d6c72SMatthew Wilcox 
1162ad3d6c72SMatthew Wilcox #define __XA_STATE(array, index, shift, sibs)  {	\
1163ad3d6c72SMatthew Wilcox 	.xa = array,					\
1164ad3d6c72SMatthew Wilcox 	.xa_index = index,				\
1165ad3d6c72SMatthew Wilcox 	.xa_shift = shift,				\
1166ad3d6c72SMatthew Wilcox 	.xa_sibs = sibs,				\
1167ad3d6c72SMatthew Wilcox 	.xa_offset = 0,					\
1168ad3d6c72SMatthew Wilcox 	.xa_pad = 0,					\
1169ad3d6c72SMatthew Wilcox 	.xa_node = XAS_RESTART,				\
1170ad3d6c72SMatthew Wilcox 	.xa_alloc = NULL,				\
1171ad3d6c72SMatthew Wilcox 	.xa_update = NULL				\
1172ad3d6c72SMatthew Wilcox }
1173ad3d6c72SMatthew Wilcox 
1174ad3d6c72SMatthew Wilcox /**
1175ad3d6c72SMatthew Wilcox  * XA_STATE() - Declare an XArray operation state.
1176ad3d6c72SMatthew Wilcox  * @name: Name of this operation state (usually xas).
1177ad3d6c72SMatthew Wilcox  * @array: Array to operate on.
1178ad3d6c72SMatthew Wilcox  * @index: Initial index of interest.
1179ad3d6c72SMatthew Wilcox  *
1180ad3d6c72SMatthew Wilcox  * Declare and initialise an xa_state on the stack.
1181ad3d6c72SMatthew Wilcox  */
1182ad3d6c72SMatthew Wilcox #define XA_STATE(name, array, index)				\
1183ad3d6c72SMatthew Wilcox 	struct xa_state name = __XA_STATE(array, index, 0, 0)
1184ad3d6c72SMatthew Wilcox 
1185ad3d6c72SMatthew Wilcox /**
1186ad3d6c72SMatthew Wilcox  * XA_STATE_ORDER() - Declare an XArray operation state.
1187ad3d6c72SMatthew Wilcox  * @name: Name of this operation state (usually xas).
1188ad3d6c72SMatthew Wilcox  * @array: Array to operate on.
1189ad3d6c72SMatthew Wilcox  * @index: Initial index of interest.
1190ad3d6c72SMatthew Wilcox  * @order: Order of entry.
1191ad3d6c72SMatthew Wilcox  *
1192ad3d6c72SMatthew Wilcox  * Declare and initialise an xa_state on the stack.  This variant of
1193ad3d6c72SMatthew Wilcox  * XA_STATE() allows you to specify the 'order' of the element you
1194ad3d6c72SMatthew Wilcox  * want to operate on.`
1195ad3d6c72SMatthew Wilcox  */
1196ad3d6c72SMatthew Wilcox #define XA_STATE_ORDER(name, array, index, order)		\
1197ad3d6c72SMatthew Wilcox 	struct xa_state name = __XA_STATE(array,		\
1198ad3d6c72SMatthew Wilcox 			(index >> order) << order,		\
1199ad3d6c72SMatthew Wilcox 			order - (order % XA_CHUNK_SHIFT),	\
1200ad3d6c72SMatthew Wilcox 			(1U << (order % XA_CHUNK_SHIFT)) - 1)
1201ad3d6c72SMatthew Wilcox 
1202ad3d6c72SMatthew Wilcox #define xas_marked(xas, mark)	xa_marked((xas)->xa, (mark))
1203ad3d6c72SMatthew Wilcox #define xas_trylock(xas)	xa_trylock((xas)->xa)
1204ad3d6c72SMatthew Wilcox #define xas_lock(xas)		xa_lock((xas)->xa)
1205ad3d6c72SMatthew Wilcox #define xas_unlock(xas)		xa_unlock((xas)->xa)
1206ad3d6c72SMatthew Wilcox #define xas_lock_bh(xas)	xa_lock_bh((xas)->xa)
1207ad3d6c72SMatthew Wilcox #define xas_unlock_bh(xas)	xa_unlock_bh((xas)->xa)
1208ad3d6c72SMatthew Wilcox #define xas_lock_irq(xas)	xa_lock_irq((xas)->xa)
1209ad3d6c72SMatthew Wilcox #define xas_unlock_irq(xas)	xa_unlock_irq((xas)->xa)
1210ad3d6c72SMatthew Wilcox #define xas_lock_irqsave(xas, flags) \
1211ad3d6c72SMatthew Wilcox 				xa_lock_irqsave((xas)->xa, flags)
1212ad3d6c72SMatthew Wilcox #define xas_unlock_irqrestore(xas, flags) \
1213ad3d6c72SMatthew Wilcox 				xa_unlock_irqrestore((xas)->xa, flags)
1214ad3d6c72SMatthew Wilcox 
1215ad3d6c72SMatthew Wilcox /**
1216ad3d6c72SMatthew Wilcox  * xas_error() - Return an errno stored in the xa_state.
1217ad3d6c72SMatthew Wilcox  * @xas: XArray operation state.
1218ad3d6c72SMatthew Wilcox  *
1219ad3d6c72SMatthew Wilcox  * Return: 0 if no error has been noted.  A negative errno if one has.
1220ad3d6c72SMatthew Wilcox  */
1221ad3d6c72SMatthew Wilcox static inline int xas_error(const struct xa_state *xas)
1222ad3d6c72SMatthew Wilcox {
1223ad3d6c72SMatthew Wilcox 	return xa_err(xas->xa_node);
1224ad3d6c72SMatthew Wilcox }
1225ad3d6c72SMatthew Wilcox 
1226ad3d6c72SMatthew Wilcox /**
1227ad3d6c72SMatthew Wilcox  * xas_set_err() - Note an error in the xa_state.
1228ad3d6c72SMatthew Wilcox  * @xas: XArray operation state.
1229ad3d6c72SMatthew Wilcox  * @err: Negative error number.
1230ad3d6c72SMatthew Wilcox  *
1231ad3d6c72SMatthew Wilcox  * Only call this function with a negative @err; zero or positive errors
1232ad3d6c72SMatthew Wilcox  * will probably not behave the way you think they should.  If you want
1233ad3d6c72SMatthew Wilcox  * to clear the error from an xa_state, use xas_reset().
1234ad3d6c72SMatthew Wilcox  */
1235ad3d6c72SMatthew Wilcox static inline void xas_set_err(struct xa_state *xas, long err)
1236ad3d6c72SMatthew Wilcox {
1237ad3d6c72SMatthew Wilcox 	xas->xa_node = XA_ERROR(err);
1238ad3d6c72SMatthew Wilcox }
1239ad3d6c72SMatthew Wilcox 
1240ad3d6c72SMatthew Wilcox /**
1241ad3d6c72SMatthew Wilcox  * xas_invalid() - Is the xas in a retry or error state?
1242ad3d6c72SMatthew Wilcox  * @xas: XArray operation state.
1243ad3d6c72SMatthew Wilcox  *
1244ad3d6c72SMatthew Wilcox  * Return: %true if the xas cannot be used for operations.
1245ad3d6c72SMatthew Wilcox  */
1246ad3d6c72SMatthew Wilcox static inline bool xas_invalid(const struct xa_state *xas)
1247ad3d6c72SMatthew Wilcox {
1248ad3d6c72SMatthew Wilcox 	return (unsigned long)xas->xa_node & 3;
1249ad3d6c72SMatthew Wilcox }
1250ad3d6c72SMatthew Wilcox 
1251ad3d6c72SMatthew Wilcox /**
1252ad3d6c72SMatthew Wilcox  * xas_valid() - Is the xas a valid cursor into the array?
1253ad3d6c72SMatthew Wilcox  * @xas: XArray operation state.
1254ad3d6c72SMatthew Wilcox  *
1255ad3d6c72SMatthew Wilcox  * Return: %true if the xas can be used for operations.
1256ad3d6c72SMatthew Wilcox  */
1257ad3d6c72SMatthew Wilcox static inline bool xas_valid(const struct xa_state *xas)
1258ad3d6c72SMatthew Wilcox {
1259ad3d6c72SMatthew Wilcox 	return !xas_invalid(xas);
1260ad3d6c72SMatthew Wilcox }
1261ad3d6c72SMatthew Wilcox 
12622264f513SMatthew Wilcox /**
12632264f513SMatthew Wilcox  * xas_is_node() - Does the xas point to a node?
12642264f513SMatthew Wilcox  * @xas: XArray operation state.
12652264f513SMatthew Wilcox  *
12662264f513SMatthew Wilcox  * Return: %true if the xas currently references a node.
12672264f513SMatthew Wilcox  */
12682264f513SMatthew Wilcox static inline bool xas_is_node(const struct xa_state *xas)
12692264f513SMatthew Wilcox {
12702264f513SMatthew Wilcox 	return xas_valid(xas) && xas->xa_node;
12712264f513SMatthew Wilcox }
12722264f513SMatthew Wilcox 
12739b89a035SMatthew Wilcox /* True if the pointer is something other than a node */
12749b89a035SMatthew Wilcox static inline bool xas_not_node(struct xa_node *node)
12759b89a035SMatthew Wilcox {
12769b89a035SMatthew Wilcox 	return ((unsigned long)node & 3) || !node;
12779b89a035SMatthew Wilcox }
12789b89a035SMatthew Wilcox 
127964d3e9a9SMatthew Wilcox /* True if the node represents RESTART or an error */
128064d3e9a9SMatthew Wilcox static inline bool xas_frozen(struct xa_node *node)
128164d3e9a9SMatthew Wilcox {
128264d3e9a9SMatthew Wilcox 	return (unsigned long)node & 2;
128364d3e9a9SMatthew Wilcox }
128464d3e9a9SMatthew Wilcox 
128558d6ea30SMatthew Wilcox /* True if the node represents head-of-tree, RESTART or BOUNDS */
128658d6ea30SMatthew Wilcox static inline bool xas_top(struct xa_node *node)
128758d6ea30SMatthew Wilcox {
128858d6ea30SMatthew Wilcox 	return node <= XAS_RESTART;
128958d6ea30SMatthew Wilcox }
129058d6ea30SMatthew Wilcox 
1291ad3d6c72SMatthew Wilcox /**
1292ad3d6c72SMatthew Wilcox  * xas_reset() - Reset an XArray operation state.
1293ad3d6c72SMatthew Wilcox  * @xas: XArray operation state.
1294ad3d6c72SMatthew Wilcox  *
1295ad3d6c72SMatthew Wilcox  * Resets the error or walk state of the @xas so future walks of the
1296ad3d6c72SMatthew Wilcox  * array will start from the root.  Use this if you have dropped the
1297ad3d6c72SMatthew Wilcox  * xarray lock and want to reuse the xa_state.
1298ad3d6c72SMatthew Wilcox  *
1299ad3d6c72SMatthew Wilcox  * Context: Any context.
1300ad3d6c72SMatthew Wilcox  */
1301ad3d6c72SMatthew Wilcox static inline void xas_reset(struct xa_state *xas)
1302ad3d6c72SMatthew Wilcox {
1303ad3d6c72SMatthew Wilcox 	xas->xa_node = XAS_RESTART;
1304ad3d6c72SMatthew Wilcox }
1305ad3d6c72SMatthew Wilcox 
1306ad3d6c72SMatthew Wilcox /**
1307ad3d6c72SMatthew Wilcox  * xas_retry() - Retry the operation if appropriate.
1308ad3d6c72SMatthew Wilcox  * @xas: XArray operation state.
1309ad3d6c72SMatthew Wilcox  * @entry: Entry from xarray.
1310ad3d6c72SMatthew Wilcox  *
1311ad3d6c72SMatthew Wilcox  * The advanced functions may sometimes return an internal entry, such as
1312ad3d6c72SMatthew Wilcox  * a retry entry or a zero entry.  This function sets up the @xas to restart
1313ad3d6c72SMatthew Wilcox  * the walk from the head of the array if needed.
1314ad3d6c72SMatthew Wilcox  *
1315ad3d6c72SMatthew Wilcox  * Context: Any context.
1316ad3d6c72SMatthew Wilcox  * Return: true if the operation needs to be retried.
1317ad3d6c72SMatthew Wilcox  */
1318ad3d6c72SMatthew Wilcox static inline bool xas_retry(struct xa_state *xas, const void *entry)
1319ad3d6c72SMatthew Wilcox {
13209f14d4f1SMatthew Wilcox 	if (xa_is_zero(entry))
13219f14d4f1SMatthew Wilcox 		return true;
1322ad3d6c72SMatthew Wilcox 	if (!xa_is_retry(entry))
1323ad3d6c72SMatthew Wilcox 		return false;
1324ad3d6c72SMatthew Wilcox 	xas_reset(xas);
1325ad3d6c72SMatthew Wilcox 	return true;
1326ad3d6c72SMatthew Wilcox }
1327ad3d6c72SMatthew Wilcox 
1328ad3d6c72SMatthew Wilcox void *xas_load(struct xa_state *);
132958d6ea30SMatthew Wilcox void *xas_store(struct xa_state *, void *entry);
1330b803b428SMatthew Wilcox void *xas_find(struct xa_state *, unsigned long max);
13314e99d4e9SMatthew Wilcox void *xas_find_conflict(struct xa_state *);
1332ad3d6c72SMatthew Wilcox 
13339b89a035SMatthew Wilcox bool xas_get_mark(const struct xa_state *, xa_mark_t);
13349b89a035SMatthew Wilcox void xas_set_mark(const struct xa_state *, xa_mark_t);
13359b89a035SMatthew Wilcox void xas_clear_mark(const struct xa_state *, xa_mark_t);
1336b803b428SMatthew Wilcox void *xas_find_marked(struct xa_state *, unsigned long max, xa_mark_t);
133758d6ea30SMatthew Wilcox void xas_init_marks(const struct xa_state *);
133858d6ea30SMatthew Wilcox 
133958d6ea30SMatthew Wilcox bool xas_nomem(struct xa_state *, gfp_t);
1340b803b428SMatthew Wilcox void xas_pause(struct xa_state *);
13419b89a035SMatthew Wilcox 
13422264f513SMatthew Wilcox void xas_create_range(struct xa_state *);
13432264f513SMatthew Wilcox 
1344ad3d6c72SMatthew Wilcox /**
1345ad3d6c72SMatthew Wilcox  * xas_reload() - Refetch an entry from the xarray.
1346ad3d6c72SMatthew Wilcox  * @xas: XArray operation state.
1347ad3d6c72SMatthew Wilcox  *
1348ad3d6c72SMatthew Wilcox  * Use this function to check that a previously loaded entry still has
1349ad3d6c72SMatthew Wilcox  * the same value.  This is useful for the lockless pagecache lookup where
1350ad3d6c72SMatthew Wilcox  * we walk the array with only the RCU lock to protect us, lock the page,
1351ad3d6c72SMatthew Wilcox  * then check that the page hasn't moved since we looked it up.
1352ad3d6c72SMatthew Wilcox  *
1353ad3d6c72SMatthew Wilcox  * The caller guarantees that @xas is still valid.  If it may be in an
1354ad3d6c72SMatthew Wilcox  * error or restart state, call xas_load() instead.
1355ad3d6c72SMatthew Wilcox  *
1356ad3d6c72SMatthew Wilcox  * Return: The entry at this location in the xarray.
1357ad3d6c72SMatthew Wilcox  */
1358ad3d6c72SMatthew Wilcox static inline void *xas_reload(struct xa_state *xas)
1359ad3d6c72SMatthew Wilcox {
1360ad3d6c72SMatthew Wilcox 	struct xa_node *node = xas->xa_node;
1361ad3d6c72SMatthew Wilcox 
1362ad3d6c72SMatthew Wilcox 	if (node)
1363ad3d6c72SMatthew Wilcox 		return xa_entry(xas->xa, node, xas->xa_offset);
1364ad3d6c72SMatthew Wilcox 	return xa_head(xas->xa);
1365ad3d6c72SMatthew Wilcox }
1366ad3d6c72SMatthew Wilcox 
136758d6ea30SMatthew Wilcox /**
136858d6ea30SMatthew Wilcox  * xas_set() - Set up XArray operation state for a different index.
136958d6ea30SMatthew Wilcox  * @xas: XArray operation state.
137058d6ea30SMatthew Wilcox  * @index: New index into the XArray.
137158d6ea30SMatthew Wilcox  *
137258d6ea30SMatthew Wilcox  * Move the operation state to refer to a different index.  This will
137358d6ea30SMatthew Wilcox  * have the effect of starting a walk from the top; see xas_next()
137458d6ea30SMatthew Wilcox  * to move to an adjacent index.
137558d6ea30SMatthew Wilcox  */
137658d6ea30SMatthew Wilcox static inline void xas_set(struct xa_state *xas, unsigned long index)
137758d6ea30SMatthew Wilcox {
137858d6ea30SMatthew Wilcox 	xas->xa_index = index;
137958d6ea30SMatthew Wilcox 	xas->xa_node = XAS_RESTART;
138058d6ea30SMatthew Wilcox }
138158d6ea30SMatthew Wilcox 
138258d6ea30SMatthew Wilcox /**
138358d6ea30SMatthew Wilcox  * xas_set_order() - Set up XArray operation state for a multislot entry.
138458d6ea30SMatthew Wilcox  * @xas: XArray operation state.
138558d6ea30SMatthew Wilcox  * @index: Target of the operation.
138658d6ea30SMatthew Wilcox  * @order: Entry occupies 2^@order indices.
138758d6ea30SMatthew Wilcox  */
138858d6ea30SMatthew Wilcox static inline void xas_set_order(struct xa_state *xas, unsigned long index,
138958d6ea30SMatthew Wilcox 					unsigned int order)
139058d6ea30SMatthew Wilcox {
139158d6ea30SMatthew Wilcox #ifdef CONFIG_XARRAY_MULTI
139258d6ea30SMatthew Wilcox 	xas->xa_index = order < BITS_PER_LONG ? (index >> order) << order : 0;
139358d6ea30SMatthew Wilcox 	xas->xa_shift = order - (order % XA_CHUNK_SHIFT);
139458d6ea30SMatthew Wilcox 	xas->xa_sibs = (1 << (order % XA_CHUNK_SHIFT)) - 1;
139558d6ea30SMatthew Wilcox 	xas->xa_node = XAS_RESTART;
139658d6ea30SMatthew Wilcox #else
139758d6ea30SMatthew Wilcox 	BUG_ON(order > 0);
139858d6ea30SMatthew Wilcox 	xas_set(xas, index);
139958d6ea30SMatthew Wilcox #endif
140058d6ea30SMatthew Wilcox }
140158d6ea30SMatthew Wilcox 
140258d6ea30SMatthew Wilcox /**
140358d6ea30SMatthew Wilcox  * xas_set_update() - Set up XArray operation state for a callback.
140458d6ea30SMatthew Wilcox  * @xas: XArray operation state.
140558d6ea30SMatthew Wilcox  * @update: Function to call when updating a node.
140658d6ea30SMatthew Wilcox  *
140758d6ea30SMatthew Wilcox  * The XArray can notify a caller after it has updated an xa_node.
140858d6ea30SMatthew Wilcox  * This is advanced functionality and is only needed by the page cache.
140958d6ea30SMatthew Wilcox  */
141058d6ea30SMatthew Wilcox static inline void xas_set_update(struct xa_state *xas, xa_update_node_t update)
141158d6ea30SMatthew Wilcox {
141258d6ea30SMatthew Wilcox 	xas->xa_update = update;
141358d6ea30SMatthew Wilcox }
141458d6ea30SMatthew Wilcox 
1415b803b428SMatthew Wilcox /**
1416b803b428SMatthew Wilcox  * xas_next_entry() - Advance iterator to next present entry.
1417b803b428SMatthew Wilcox  * @xas: XArray operation state.
1418b803b428SMatthew Wilcox  * @max: Highest index to return.
1419b803b428SMatthew Wilcox  *
1420b803b428SMatthew Wilcox  * xas_next_entry() is an inline function to optimise xarray traversal for
1421b803b428SMatthew Wilcox  * speed.  It is equivalent to calling xas_find(), and will call xas_find()
1422b803b428SMatthew Wilcox  * for all the hard cases.
1423b803b428SMatthew Wilcox  *
1424b803b428SMatthew Wilcox  * Return: The next present entry after the one currently referred to by @xas.
1425b803b428SMatthew Wilcox  */
1426b803b428SMatthew Wilcox static inline void *xas_next_entry(struct xa_state *xas, unsigned long max)
1427b803b428SMatthew Wilcox {
1428b803b428SMatthew Wilcox 	struct xa_node *node = xas->xa_node;
1429b803b428SMatthew Wilcox 	void *entry;
1430b803b428SMatthew Wilcox 
1431b803b428SMatthew Wilcox 	if (unlikely(xas_not_node(node) || node->shift ||
1432b803b428SMatthew Wilcox 			xas->xa_offset != (xas->xa_index & XA_CHUNK_MASK)))
1433b803b428SMatthew Wilcox 		return xas_find(xas, max);
1434b803b428SMatthew Wilcox 
1435b803b428SMatthew Wilcox 	do {
1436b803b428SMatthew Wilcox 		if (unlikely(xas->xa_index >= max))
1437b803b428SMatthew Wilcox 			return xas_find(xas, max);
1438b803b428SMatthew Wilcox 		if (unlikely(xas->xa_offset == XA_CHUNK_MASK))
1439b803b428SMatthew Wilcox 			return xas_find(xas, max);
1440b803b428SMatthew Wilcox 		entry = xa_entry(xas->xa, node, xas->xa_offset + 1);
1441b803b428SMatthew Wilcox 		if (unlikely(xa_is_internal(entry)))
1442b803b428SMatthew Wilcox 			return xas_find(xas, max);
1443b803b428SMatthew Wilcox 		xas->xa_offset++;
1444b803b428SMatthew Wilcox 		xas->xa_index++;
1445b803b428SMatthew Wilcox 	} while (!entry);
1446b803b428SMatthew Wilcox 
1447b803b428SMatthew Wilcox 	return entry;
1448b803b428SMatthew Wilcox }
1449b803b428SMatthew Wilcox 
1450b803b428SMatthew Wilcox /* Private */
1451b803b428SMatthew Wilcox static inline unsigned int xas_find_chunk(struct xa_state *xas, bool advance,
1452b803b428SMatthew Wilcox 		xa_mark_t mark)
1453b803b428SMatthew Wilcox {
1454b803b428SMatthew Wilcox 	unsigned long *addr = xas->xa_node->marks[(__force unsigned)mark];
1455b803b428SMatthew Wilcox 	unsigned int offset = xas->xa_offset;
1456b803b428SMatthew Wilcox 
1457b803b428SMatthew Wilcox 	if (advance)
1458b803b428SMatthew Wilcox 		offset++;
1459b803b428SMatthew Wilcox 	if (XA_CHUNK_SIZE == BITS_PER_LONG) {
1460b803b428SMatthew Wilcox 		if (offset < XA_CHUNK_SIZE) {
1461b803b428SMatthew Wilcox 			unsigned long data = *addr & (~0UL << offset);
1462b803b428SMatthew Wilcox 			if (data)
1463b803b428SMatthew Wilcox 				return __ffs(data);
1464b803b428SMatthew Wilcox 		}
1465b803b428SMatthew Wilcox 		return XA_CHUNK_SIZE;
1466b803b428SMatthew Wilcox 	}
1467b803b428SMatthew Wilcox 
1468b803b428SMatthew Wilcox 	return find_next_bit(addr, XA_CHUNK_SIZE, offset);
1469b803b428SMatthew Wilcox }
1470b803b428SMatthew Wilcox 
1471b803b428SMatthew Wilcox /**
1472b803b428SMatthew Wilcox  * xas_next_marked() - Advance iterator to next marked entry.
1473b803b428SMatthew Wilcox  * @xas: XArray operation state.
1474b803b428SMatthew Wilcox  * @max: Highest index to return.
1475b803b428SMatthew Wilcox  * @mark: Mark to search for.
1476b803b428SMatthew Wilcox  *
1477b803b428SMatthew Wilcox  * xas_next_marked() is an inline function to optimise xarray traversal for
1478b803b428SMatthew Wilcox  * speed.  It is equivalent to calling xas_find_marked(), and will call
1479b803b428SMatthew Wilcox  * xas_find_marked() for all the hard cases.
1480b803b428SMatthew Wilcox  *
1481b803b428SMatthew Wilcox  * Return: The next marked entry after the one currently referred to by @xas.
1482b803b428SMatthew Wilcox  */
1483b803b428SMatthew Wilcox static inline void *xas_next_marked(struct xa_state *xas, unsigned long max,
1484b803b428SMatthew Wilcox 								xa_mark_t mark)
1485b803b428SMatthew Wilcox {
1486b803b428SMatthew Wilcox 	struct xa_node *node = xas->xa_node;
1487b803b428SMatthew Wilcox 	unsigned int offset;
1488b803b428SMatthew Wilcox 
1489b803b428SMatthew Wilcox 	if (unlikely(xas_not_node(node) || node->shift))
1490b803b428SMatthew Wilcox 		return xas_find_marked(xas, max, mark);
1491b803b428SMatthew Wilcox 	offset = xas_find_chunk(xas, true, mark);
1492b803b428SMatthew Wilcox 	xas->xa_offset = offset;
1493b803b428SMatthew Wilcox 	xas->xa_index = (xas->xa_index & ~XA_CHUNK_MASK) + offset;
1494b803b428SMatthew Wilcox 	if (xas->xa_index > max)
1495b803b428SMatthew Wilcox 		return NULL;
1496b803b428SMatthew Wilcox 	if (offset == XA_CHUNK_SIZE)
1497b803b428SMatthew Wilcox 		return xas_find_marked(xas, max, mark);
1498b803b428SMatthew Wilcox 	return xa_entry(xas->xa, node, offset);
1499b803b428SMatthew Wilcox }
1500b803b428SMatthew Wilcox 
1501b803b428SMatthew Wilcox /*
1502b803b428SMatthew Wilcox  * If iterating while holding a lock, drop the lock and reschedule
1503b803b428SMatthew Wilcox  * every %XA_CHECK_SCHED loops.
1504b803b428SMatthew Wilcox  */
1505b803b428SMatthew Wilcox enum {
1506b803b428SMatthew Wilcox 	XA_CHECK_SCHED = 4096,
1507b803b428SMatthew Wilcox };
1508b803b428SMatthew Wilcox 
1509b803b428SMatthew Wilcox /**
1510b803b428SMatthew Wilcox  * xas_for_each() - Iterate over a range of an XArray.
1511b803b428SMatthew Wilcox  * @xas: XArray operation state.
1512b803b428SMatthew Wilcox  * @entry: Entry retrieved from the array.
1513b803b428SMatthew Wilcox  * @max: Maximum index to retrieve from array.
1514b803b428SMatthew Wilcox  *
1515b803b428SMatthew Wilcox  * The loop body will be executed for each entry present in the xarray
1516b803b428SMatthew Wilcox  * between the current xas position and @max.  @entry will be set to
1517b803b428SMatthew Wilcox  * the entry retrieved from the xarray.  It is safe to delete entries
1518b803b428SMatthew Wilcox  * from the array in the loop body.  You should hold either the RCU lock
1519b803b428SMatthew Wilcox  * or the xa_lock while iterating.  If you need to drop the lock, call
1520b803b428SMatthew Wilcox  * xas_pause() first.
1521b803b428SMatthew Wilcox  */
1522b803b428SMatthew Wilcox #define xas_for_each(xas, entry, max) \
1523b803b428SMatthew Wilcox 	for (entry = xas_find(xas, max); entry; \
1524b803b428SMatthew Wilcox 	     entry = xas_next_entry(xas, max))
1525b803b428SMatthew Wilcox 
1526b803b428SMatthew Wilcox /**
1527b803b428SMatthew Wilcox  * xas_for_each_marked() - Iterate over a range of an XArray.
1528b803b428SMatthew Wilcox  * @xas: XArray operation state.
1529b803b428SMatthew Wilcox  * @entry: Entry retrieved from the array.
1530b803b428SMatthew Wilcox  * @max: Maximum index to retrieve from array.
1531b803b428SMatthew Wilcox  * @mark: Mark to search for.
1532b803b428SMatthew Wilcox  *
1533b803b428SMatthew Wilcox  * The loop body will be executed for each marked entry in the xarray
1534b803b428SMatthew Wilcox  * between the current xas position and @max.  @entry will be set to
1535b803b428SMatthew Wilcox  * the entry retrieved from the xarray.  It is safe to delete entries
1536b803b428SMatthew Wilcox  * from the array in the loop body.  You should hold either the RCU lock
1537b803b428SMatthew Wilcox  * or the xa_lock while iterating.  If you need to drop the lock, call
1538b803b428SMatthew Wilcox  * xas_pause() first.
1539b803b428SMatthew Wilcox  */
1540b803b428SMatthew Wilcox #define xas_for_each_marked(xas, entry, max, mark) \
1541b803b428SMatthew Wilcox 	for (entry = xas_find_marked(xas, max, mark); entry; \
1542b803b428SMatthew Wilcox 	     entry = xas_next_marked(xas, max, mark))
1543b803b428SMatthew Wilcox 
15444e99d4e9SMatthew Wilcox /**
15454e99d4e9SMatthew Wilcox  * xas_for_each_conflict() - Iterate over a range of an XArray.
15464e99d4e9SMatthew Wilcox  * @xas: XArray operation state.
15474e99d4e9SMatthew Wilcox  * @entry: Entry retrieved from the array.
15484e99d4e9SMatthew Wilcox  *
15494e99d4e9SMatthew Wilcox  * The loop body will be executed for each entry in the XArray that lies
15504e99d4e9SMatthew Wilcox  * within the range specified by @xas.  If the loop completes successfully,
15514e99d4e9SMatthew Wilcox  * any entries that lie in this range will be replaced by @entry.  The caller
15524e99d4e9SMatthew Wilcox  * may break out of the loop; if they do so, the contents of the XArray will
15534e99d4e9SMatthew Wilcox  * be unchanged.  The operation may fail due to an out of memory condition.
15544e99d4e9SMatthew Wilcox  * The caller may also call xa_set_err() to exit the loop while setting an
15554e99d4e9SMatthew Wilcox  * error to record the reason.
15564e99d4e9SMatthew Wilcox  */
15574e99d4e9SMatthew Wilcox #define xas_for_each_conflict(xas, entry) \
15584e99d4e9SMatthew Wilcox 	while ((entry = xas_find_conflict(xas)))
15594e99d4e9SMatthew Wilcox 
156064d3e9a9SMatthew Wilcox void *__xas_next(struct xa_state *);
156164d3e9a9SMatthew Wilcox void *__xas_prev(struct xa_state *);
156264d3e9a9SMatthew Wilcox 
156364d3e9a9SMatthew Wilcox /**
156464d3e9a9SMatthew Wilcox  * xas_prev() - Move iterator to previous index.
156564d3e9a9SMatthew Wilcox  * @xas: XArray operation state.
156664d3e9a9SMatthew Wilcox  *
156764d3e9a9SMatthew Wilcox  * If the @xas was in an error state, it will remain in an error state
156864d3e9a9SMatthew Wilcox  * and this function will return %NULL.  If the @xas has never been walked,
156964d3e9a9SMatthew Wilcox  * it will have the effect of calling xas_load().  Otherwise one will be
157064d3e9a9SMatthew Wilcox  * subtracted from the index and the state will be walked to the correct
157164d3e9a9SMatthew Wilcox  * location in the array for the next operation.
157264d3e9a9SMatthew Wilcox  *
157364d3e9a9SMatthew Wilcox  * If the iterator was referencing index 0, this function wraps
157464d3e9a9SMatthew Wilcox  * around to %ULONG_MAX.
157564d3e9a9SMatthew Wilcox  *
157664d3e9a9SMatthew Wilcox  * Return: The entry at the new index.  This may be %NULL or an internal
157764d3e9a9SMatthew Wilcox  * entry.
157864d3e9a9SMatthew Wilcox  */
157964d3e9a9SMatthew Wilcox static inline void *xas_prev(struct xa_state *xas)
158064d3e9a9SMatthew Wilcox {
158164d3e9a9SMatthew Wilcox 	struct xa_node *node = xas->xa_node;
158264d3e9a9SMatthew Wilcox 
158364d3e9a9SMatthew Wilcox 	if (unlikely(xas_not_node(node) || node->shift ||
158464d3e9a9SMatthew Wilcox 				xas->xa_offset == 0))
158564d3e9a9SMatthew Wilcox 		return __xas_prev(xas);
158664d3e9a9SMatthew Wilcox 
158764d3e9a9SMatthew Wilcox 	xas->xa_index--;
158864d3e9a9SMatthew Wilcox 	xas->xa_offset--;
158964d3e9a9SMatthew Wilcox 	return xa_entry(xas->xa, node, xas->xa_offset);
159064d3e9a9SMatthew Wilcox }
159164d3e9a9SMatthew Wilcox 
159264d3e9a9SMatthew Wilcox /**
159364d3e9a9SMatthew Wilcox  * xas_next() - Move state to next index.
159464d3e9a9SMatthew Wilcox  * @xas: XArray operation state.
159564d3e9a9SMatthew Wilcox  *
159664d3e9a9SMatthew Wilcox  * If the @xas was in an error state, it will remain in an error state
159764d3e9a9SMatthew Wilcox  * and this function will return %NULL.  If the @xas has never been walked,
159864d3e9a9SMatthew Wilcox  * it will have the effect of calling xas_load().  Otherwise one will be
159964d3e9a9SMatthew Wilcox  * added to the index and the state will be walked to the correct
160064d3e9a9SMatthew Wilcox  * location in the array for the next operation.
160164d3e9a9SMatthew Wilcox  *
160264d3e9a9SMatthew Wilcox  * If the iterator was referencing index %ULONG_MAX, this function wraps
160364d3e9a9SMatthew Wilcox  * around to 0.
160464d3e9a9SMatthew Wilcox  *
160564d3e9a9SMatthew Wilcox  * Return: The entry at the new index.  This may be %NULL or an internal
160664d3e9a9SMatthew Wilcox  * entry.
160764d3e9a9SMatthew Wilcox  */
160864d3e9a9SMatthew Wilcox static inline void *xas_next(struct xa_state *xas)
160964d3e9a9SMatthew Wilcox {
161064d3e9a9SMatthew Wilcox 	struct xa_node *node = xas->xa_node;
161164d3e9a9SMatthew Wilcox 
161264d3e9a9SMatthew Wilcox 	if (unlikely(xas_not_node(node) || node->shift ||
161364d3e9a9SMatthew Wilcox 				xas->xa_offset == XA_CHUNK_MASK))
161464d3e9a9SMatthew Wilcox 		return __xas_next(xas);
161564d3e9a9SMatthew Wilcox 
161664d3e9a9SMatthew Wilcox 	xas->xa_index++;
161764d3e9a9SMatthew Wilcox 	xas->xa_offset++;
161864d3e9a9SMatthew Wilcox 	return xa_entry(xas->xa, node, xas->xa_offset);
161964d3e9a9SMatthew Wilcox }
162064d3e9a9SMatthew Wilcox 
1621f6bb2a2cSMatthew Wilcox #endif /* _LINUX_XARRAY_H */
1622