xref: /OK3568_Linux_fs/kernel/include/linux/xarray.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0+ */
2*4882a593Smuzhiyun #ifndef _LINUX_XARRAY_H
3*4882a593Smuzhiyun #define _LINUX_XARRAY_H
4*4882a593Smuzhiyun /*
5*4882a593Smuzhiyun  * eXtensible Arrays
6*4882a593Smuzhiyun  * Copyright (c) 2017 Microsoft Corporation
7*4882a593Smuzhiyun  * Author: Matthew Wilcox <willy@infradead.org>
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  * See Documentation/core-api/xarray.rst for how to use the XArray.
10*4882a593Smuzhiyun  */
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun #include <linux/bug.h>
13*4882a593Smuzhiyun #include <linux/compiler.h>
14*4882a593Smuzhiyun #include <linux/gfp.h>
15*4882a593Smuzhiyun #include <linux/kconfig.h>
16*4882a593Smuzhiyun #include <linux/kernel.h>
17*4882a593Smuzhiyun #include <linux/rcupdate.h>
18*4882a593Smuzhiyun #include <linux/spinlock.h>
19*4882a593Smuzhiyun #include <linux/types.h>
20*4882a593Smuzhiyun 
21*4882a593Smuzhiyun /*
22*4882a593Smuzhiyun  * The bottom two bits of the entry determine how the XArray interprets
23*4882a593Smuzhiyun  * the contents:
24*4882a593Smuzhiyun  *
25*4882a593Smuzhiyun  * 00: Pointer entry
26*4882a593Smuzhiyun  * 10: Internal entry
27*4882a593Smuzhiyun  * x1: Value entry or tagged pointer
28*4882a593Smuzhiyun  *
29*4882a593Smuzhiyun  * Attempting to store internal entries in the XArray is a bug.
30*4882a593Smuzhiyun  *
31*4882a593Smuzhiyun  * Most internal entries are pointers to the next node in the tree.
32*4882a593Smuzhiyun  * The following internal entries have a special meaning:
33*4882a593Smuzhiyun  *
34*4882a593Smuzhiyun  * 0-62: Sibling entries
35*4882a593Smuzhiyun  * 256: Retry entry
36*4882a593Smuzhiyun  * 257: Zero entry
37*4882a593Smuzhiyun  *
38*4882a593Smuzhiyun  * Errors are also represented as internal entries, but use the negative
39*4882a593Smuzhiyun  * space (-4094 to -2).  They're never stored in the slots array; only
40*4882a593Smuzhiyun  * returned by the normal API.
41*4882a593Smuzhiyun  */
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun #define BITS_PER_XA_VALUE	(BITS_PER_LONG - 1)
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun /**
46*4882a593Smuzhiyun  * xa_mk_value() - Create an XArray entry from an integer.
47*4882a593Smuzhiyun  * @v: Value to store in XArray.
48*4882a593Smuzhiyun  *
49*4882a593Smuzhiyun  * Context: Any context.
50*4882a593Smuzhiyun  * Return: An entry suitable for storing in the XArray.
51*4882a593Smuzhiyun  */
xa_mk_value(unsigned long v)52*4882a593Smuzhiyun static inline void *xa_mk_value(unsigned long v)
53*4882a593Smuzhiyun {
54*4882a593Smuzhiyun 	WARN_ON((long)v < 0);
55*4882a593Smuzhiyun 	return (void *)((v << 1) | 1);
56*4882a593Smuzhiyun }
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun /**
59*4882a593Smuzhiyun  * xa_to_value() - Get value stored in an XArray entry.
60*4882a593Smuzhiyun  * @entry: XArray entry.
61*4882a593Smuzhiyun  *
62*4882a593Smuzhiyun  * Context: Any context.
63*4882a593Smuzhiyun  * Return: The value stored in the XArray entry.
64*4882a593Smuzhiyun  */
xa_to_value(const void * entry)65*4882a593Smuzhiyun static inline unsigned long xa_to_value(const void *entry)
66*4882a593Smuzhiyun {
67*4882a593Smuzhiyun 	return (unsigned long)entry >> 1;
68*4882a593Smuzhiyun }
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun /**
71*4882a593Smuzhiyun  * xa_is_value() - Determine if an entry is a value.
72*4882a593Smuzhiyun  * @entry: XArray entry.
73*4882a593Smuzhiyun  *
74*4882a593Smuzhiyun  * Context: Any context.
75*4882a593Smuzhiyun  * Return: True if the entry is a value, false if it is a pointer.
76*4882a593Smuzhiyun  */
xa_is_value(const void * entry)77*4882a593Smuzhiyun static inline bool xa_is_value(const void *entry)
78*4882a593Smuzhiyun {
79*4882a593Smuzhiyun 	return (unsigned long)entry & 1;
80*4882a593Smuzhiyun }
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun /**
83*4882a593Smuzhiyun  * xa_tag_pointer() - Create an XArray entry for a tagged pointer.
84*4882a593Smuzhiyun  * @p: Plain pointer.
85*4882a593Smuzhiyun  * @tag: Tag value (0, 1 or 3).
86*4882a593Smuzhiyun  *
87*4882a593Smuzhiyun  * If the user of the XArray prefers, they can tag their pointers instead
88*4882a593Smuzhiyun  * of storing value entries.  Three tags are available (0, 1 and 3).
89*4882a593Smuzhiyun  * These are distinct from the xa_mark_t as they are not replicated up
90*4882a593Smuzhiyun  * through the array and cannot be searched for.
91*4882a593Smuzhiyun  *
92*4882a593Smuzhiyun  * Context: Any context.
93*4882a593Smuzhiyun  * Return: An XArray entry.
94*4882a593Smuzhiyun  */
xa_tag_pointer(void * p,unsigned long tag)95*4882a593Smuzhiyun static inline void *xa_tag_pointer(void *p, unsigned long tag)
96*4882a593Smuzhiyun {
97*4882a593Smuzhiyun 	return (void *)((unsigned long)p | tag);
98*4882a593Smuzhiyun }
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun /**
101*4882a593Smuzhiyun  * xa_untag_pointer() - Turn an XArray entry into a plain pointer.
102*4882a593Smuzhiyun  * @entry: XArray entry.
103*4882a593Smuzhiyun  *
104*4882a593Smuzhiyun  * If you have stored a tagged pointer in the XArray, call this function
105*4882a593Smuzhiyun  * to get the untagged version of the pointer.
106*4882a593Smuzhiyun  *
107*4882a593Smuzhiyun  * Context: Any context.
108*4882a593Smuzhiyun  * Return: A pointer.
109*4882a593Smuzhiyun  */
xa_untag_pointer(void * entry)110*4882a593Smuzhiyun static inline void *xa_untag_pointer(void *entry)
111*4882a593Smuzhiyun {
112*4882a593Smuzhiyun 	return (void *)((unsigned long)entry & ~3UL);
113*4882a593Smuzhiyun }
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun /**
116*4882a593Smuzhiyun  * xa_pointer_tag() - Get the tag stored in an XArray entry.
117*4882a593Smuzhiyun  * @entry: XArray entry.
118*4882a593Smuzhiyun  *
119*4882a593Smuzhiyun  * If you have stored a tagged pointer in the XArray, call this function
120*4882a593Smuzhiyun  * to get the tag of that pointer.
121*4882a593Smuzhiyun  *
122*4882a593Smuzhiyun  * Context: Any context.
123*4882a593Smuzhiyun  * Return: A tag.
124*4882a593Smuzhiyun  */
xa_pointer_tag(void * entry)125*4882a593Smuzhiyun static inline unsigned int xa_pointer_tag(void *entry)
126*4882a593Smuzhiyun {
127*4882a593Smuzhiyun 	return (unsigned long)entry & 3UL;
128*4882a593Smuzhiyun }
129*4882a593Smuzhiyun 
130*4882a593Smuzhiyun /*
131*4882a593Smuzhiyun  * xa_mk_internal() - Create an internal entry.
132*4882a593Smuzhiyun  * @v: Value to turn into an internal entry.
133*4882a593Smuzhiyun  *
134*4882a593Smuzhiyun  * Internal entries are used for a number of purposes.  Entries 0-255 are
135*4882a593Smuzhiyun  * used for sibling entries (only 0-62 are used by the current code).  256
136*4882a593Smuzhiyun  * is used for the retry entry.  257 is used for the reserved / zero entry.
137*4882a593Smuzhiyun  * Negative internal entries are used to represent errnos.  Node pointers
138*4882a593Smuzhiyun  * are also tagged as internal entries in some situations.
139*4882a593Smuzhiyun  *
140*4882a593Smuzhiyun  * Context: Any context.
141*4882a593Smuzhiyun  * Return: An XArray internal entry corresponding to this value.
142*4882a593Smuzhiyun  */
xa_mk_internal(unsigned long v)143*4882a593Smuzhiyun static inline void *xa_mk_internal(unsigned long v)
144*4882a593Smuzhiyun {
145*4882a593Smuzhiyun 	return (void *)((v << 2) | 2);
146*4882a593Smuzhiyun }
147*4882a593Smuzhiyun 
148*4882a593Smuzhiyun /*
149*4882a593Smuzhiyun  * xa_to_internal() - Extract the value from an internal entry.
150*4882a593Smuzhiyun  * @entry: XArray entry.
151*4882a593Smuzhiyun  *
152*4882a593Smuzhiyun  * Context: Any context.
153*4882a593Smuzhiyun  * Return: The value which was stored in the internal entry.
154*4882a593Smuzhiyun  */
xa_to_internal(const void * entry)155*4882a593Smuzhiyun static inline unsigned long xa_to_internal(const void *entry)
156*4882a593Smuzhiyun {
157*4882a593Smuzhiyun 	return (unsigned long)entry >> 2;
158*4882a593Smuzhiyun }
159*4882a593Smuzhiyun 
160*4882a593Smuzhiyun /*
161*4882a593Smuzhiyun  * xa_is_internal() - Is the entry an internal entry?
162*4882a593Smuzhiyun  * @entry: XArray entry.
163*4882a593Smuzhiyun  *
164*4882a593Smuzhiyun  * Context: Any context.
165*4882a593Smuzhiyun  * Return: %true if the entry is an internal entry.
166*4882a593Smuzhiyun  */
xa_is_internal(const void * entry)167*4882a593Smuzhiyun static inline bool xa_is_internal(const void *entry)
168*4882a593Smuzhiyun {
169*4882a593Smuzhiyun 	return ((unsigned long)entry & 3) == 2;
170*4882a593Smuzhiyun }
171*4882a593Smuzhiyun 
172*4882a593Smuzhiyun #define XA_ZERO_ENTRY		xa_mk_internal(257)
173*4882a593Smuzhiyun 
174*4882a593Smuzhiyun /**
175*4882a593Smuzhiyun  * xa_is_zero() - Is the entry a zero entry?
176*4882a593Smuzhiyun  * @entry: Entry retrieved from the XArray
177*4882a593Smuzhiyun  *
178*4882a593Smuzhiyun  * The normal API will return NULL as the contents of a slot containing
179*4882a593Smuzhiyun  * a zero entry.  You can only see zero entries by using the advanced API.
180*4882a593Smuzhiyun  *
181*4882a593Smuzhiyun  * Return: %true if the entry is a zero entry.
182*4882a593Smuzhiyun  */
xa_is_zero(const void * entry)183*4882a593Smuzhiyun static inline bool xa_is_zero(const void *entry)
184*4882a593Smuzhiyun {
185*4882a593Smuzhiyun 	return unlikely(entry == XA_ZERO_ENTRY);
186*4882a593Smuzhiyun }
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun /**
189*4882a593Smuzhiyun  * xa_is_err() - Report whether an XArray operation returned an error
190*4882a593Smuzhiyun  * @entry: Result from calling an XArray function
191*4882a593Smuzhiyun  *
192*4882a593Smuzhiyun  * If an XArray operation cannot complete an operation, it will return
193*4882a593Smuzhiyun  * a special value indicating an error.  This function tells you
194*4882a593Smuzhiyun  * whether an error occurred; xa_err() tells you which error occurred.
195*4882a593Smuzhiyun  *
196*4882a593Smuzhiyun  * Context: Any context.
197*4882a593Smuzhiyun  * Return: %true if the entry indicates an error.
198*4882a593Smuzhiyun  */
xa_is_err(const void * entry)199*4882a593Smuzhiyun static inline bool xa_is_err(const void *entry)
200*4882a593Smuzhiyun {
201*4882a593Smuzhiyun 	return unlikely(xa_is_internal(entry) &&
202*4882a593Smuzhiyun 			entry >= xa_mk_internal(-MAX_ERRNO));
203*4882a593Smuzhiyun }
204*4882a593Smuzhiyun 
205*4882a593Smuzhiyun /**
206*4882a593Smuzhiyun  * xa_err() - Turn an XArray result into an errno.
207*4882a593Smuzhiyun  * @entry: Result from calling an XArray function.
208*4882a593Smuzhiyun  *
209*4882a593Smuzhiyun  * If an XArray operation cannot complete an operation, it will return
210*4882a593Smuzhiyun  * a special pointer value which encodes an errno.  This function extracts
211*4882a593Smuzhiyun  * the errno from the pointer value, or returns 0 if the pointer does not
212*4882a593Smuzhiyun  * represent an errno.
213*4882a593Smuzhiyun  *
214*4882a593Smuzhiyun  * Context: Any context.
215*4882a593Smuzhiyun  * Return: A negative errno or 0.
216*4882a593Smuzhiyun  */
xa_err(void * entry)217*4882a593Smuzhiyun static inline int xa_err(void *entry)
218*4882a593Smuzhiyun {
219*4882a593Smuzhiyun 	/* xa_to_internal() would not do sign extension. */
220*4882a593Smuzhiyun 	if (xa_is_err(entry))
221*4882a593Smuzhiyun 		return (long)entry >> 2;
222*4882a593Smuzhiyun 	return 0;
223*4882a593Smuzhiyun }
224*4882a593Smuzhiyun 
225*4882a593Smuzhiyun /**
226*4882a593Smuzhiyun  * struct xa_limit - Represents a range of IDs.
227*4882a593Smuzhiyun  * @min: The lowest ID to allocate (inclusive).
228*4882a593Smuzhiyun  * @max: The maximum ID to allocate (inclusive).
229*4882a593Smuzhiyun  *
230*4882a593Smuzhiyun  * This structure is used either directly or via the XA_LIMIT() macro
231*4882a593Smuzhiyun  * to communicate the range of IDs that are valid for allocation.
232*4882a593Smuzhiyun  * Two common ranges are predefined for you:
233*4882a593Smuzhiyun  * * xa_limit_32b	- [0 - UINT_MAX]
234*4882a593Smuzhiyun  * * xa_limit_31b	- [0 - INT_MAX]
235*4882a593Smuzhiyun  */
236*4882a593Smuzhiyun struct xa_limit {
237*4882a593Smuzhiyun 	u32 max;
238*4882a593Smuzhiyun 	u32 min;
239*4882a593Smuzhiyun };
240*4882a593Smuzhiyun 
241*4882a593Smuzhiyun #define XA_LIMIT(_min, _max) (struct xa_limit) { .min = _min, .max = _max }
242*4882a593Smuzhiyun 
243*4882a593Smuzhiyun #define xa_limit_32b	XA_LIMIT(0, UINT_MAX)
244*4882a593Smuzhiyun #define xa_limit_31b	XA_LIMIT(0, INT_MAX)
245*4882a593Smuzhiyun 
246*4882a593Smuzhiyun typedef unsigned __bitwise xa_mark_t;
247*4882a593Smuzhiyun #define XA_MARK_0		((__force xa_mark_t)0U)
248*4882a593Smuzhiyun #define XA_MARK_1		((__force xa_mark_t)1U)
249*4882a593Smuzhiyun #define XA_MARK_2		((__force xa_mark_t)2U)
250*4882a593Smuzhiyun #define XA_PRESENT		((__force xa_mark_t)8U)
251*4882a593Smuzhiyun #define XA_MARK_MAX		XA_MARK_2
252*4882a593Smuzhiyun #define XA_FREE_MARK		XA_MARK_0
253*4882a593Smuzhiyun 
254*4882a593Smuzhiyun enum xa_lock_type {
255*4882a593Smuzhiyun 	XA_LOCK_IRQ = 1,
256*4882a593Smuzhiyun 	XA_LOCK_BH = 2,
257*4882a593Smuzhiyun };
258*4882a593Smuzhiyun 
259*4882a593Smuzhiyun /*
260*4882a593Smuzhiyun  * Values for xa_flags.  The radix tree stores its GFP flags in the xa_flags,
261*4882a593Smuzhiyun  * and we remain compatible with that.
262*4882a593Smuzhiyun  */
263*4882a593Smuzhiyun #define XA_FLAGS_LOCK_IRQ	((__force gfp_t)XA_LOCK_IRQ)
264*4882a593Smuzhiyun #define XA_FLAGS_LOCK_BH	((__force gfp_t)XA_LOCK_BH)
265*4882a593Smuzhiyun #define XA_FLAGS_TRACK_FREE	((__force gfp_t)4U)
266*4882a593Smuzhiyun #define XA_FLAGS_ZERO_BUSY	((__force gfp_t)8U)
267*4882a593Smuzhiyun #define XA_FLAGS_ALLOC_WRAPPED	((__force gfp_t)16U)
268*4882a593Smuzhiyun #define XA_FLAGS_ACCOUNT	((__force gfp_t)32U)
269*4882a593Smuzhiyun #define XA_FLAGS_MARK(mark)	((__force gfp_t)((1U << __GFP_BITS_SHIFT) << \
270*4882a593Smuzhiyun 						(__force unsigned)(mark)))
271*4882a593Smuzhiyun 
272*4882a593Smuzhiyun /* ALLOC is for a normal 0-based alloc.  ALLOC1 is for an 1-based alloc */
273*4882a593Smuzhiyun #define XA_FLAGS_ALLOC	(XA_FLAGS_TRACK_FREE | XA_FLAGS_MARK(XA_FREE_MARK))
274*4882a593Smuzhiyun #define XA_FLAGS_ALLOC1	(XA_FLAGS_TRACK_FREE | XA_FLAGS_ZERO_BUSY)
275*4882a593Smuzhiyun 
276*4882a593Smuzhiyun /**
277*4882a593Smuzhiyun  * struct xarray - The anchor of the XArray.
278*4882a593Smuzhiyun  * @xa_lock: Lock that protects the contents of the XArray.
279*4882a593Smuzhiyun  *
280*4882a593Smuzhiyun  * To use the xarray, define it statically or embed it in your data structure.
281*4882a593Smuzhiyun  * It is a very small data structure, so it does not usually make sense to
282*4882a593Smuzhiyun  * allocate it separately and keep a pointer to it in your data structure.
283*4882a593Smuzhiyun  *
284*4882a593Smuzhiyun  * You may use the xa_lock to protect your own data structures as well.
285*4882a593Smuzhiyun  */
286*4882a593Smuzhiyun /*
287*4882a593Smuzhiyun  * If all of the entries in the array are NULL, @xa_head is a NULL pointer.
288*4882a593Smuzhiyun  * If the only non-NULL entry in the array is at index 0, @xa_head is that
289*4882a593Smuzhiyun  * entry.  If any other entry in the array is non-NULL, @xa_head points
290*4882a593Smuzhiyun  * to an @xa_node.
291*4882a593Smuzhiyun  */
292*4882a593Smuzhiyun struct xarray {
293*4882a593Smuzhiyun 	spinlock_t	xa_lock;
294*4882a593Smuzhiyun /* private: The rest of the data structure is not to be used directly. */
295*4882a593Smuzhiyun 	gfp_t		xa_flags;
296*4882a593Smuzhiyun 	void __rcu *	xa_head;
297*4882a593Smuzhiyun };
298*4882a593Smuzhiyun 
299*4882a593Smuzhiyun #define XARRAY_INIT(name, flags) {				\
300*4882a593Smuzhiyun 	.xa_lock = __SPIN_LOCK_UNLOCKED(name.xa_lock),		\
301*4882a593Smuzhiyun 	.xa_flags = flags,					\
302*4882a593Smuzhiyun 	.xa_head = NULL,					\
303*4882a593Smuzhiyun }
304*4882a593Smuzhiyun 
305*4882a593Smuzhiyun /**
306*4882a593Smuzhiyun  * DEFINE_XARRAY_FLAGS() - Define an XArray with custom flags.
307*4882a593Smuzhiyun  * @name: A string that names your XArray.
308*4882a593Smuzhiyun  * @flags: XA_FLAG values.
309*4882a593Smuzhiyun  *
310*4882a593Smuzhiyun  * This is intended for file scope definitions of XArrays.  It declares
311*4882a593Smuzhiyun  * and initialises an empty XArray with the chosen name and flags.  It is
312*4882a593Smuzhiyun  * equivalent to calling xa_init_flags() on the array, but it does the
313*4882a593Smuzhiyun  * initialisation at compiletime instead of runtime.
314*4882a593Smuzhiyun  */
315*4882a593Smuzhiyun #define DEFINE_XARRAY_FLAGS(name, flags)				\
316*4882a593Smuzhiyun 	struct xarray name = XARRAY_INIT(name, flags)
317*4882a593Smuzhiyun 
318*4882a593Smuzhiyun /**
319*4882a593Smuzhiyun  * DEFINE_XARRAY() - Define an XArray.
320*4882a593Smuzhiyun  * @name: A string that names your XArray.
321*4882a593Smuzhiyun  *
322*4882a593Smuzhiyun  * This is intended for file scope definitions of XArrays.  It declares
323*4882a593Smuzhiyun  * and initialises an empty XArray with the chosen name.  It is equivalent
324*4882a593Smuzhiyun  * to calling xa_init() on the array, but it does the initialisation at
325*4882a593Smuzhiyun  * compiletime instead of runtime.
326*4882a593Smuzhiyun  */
327*4882a593Smuzhiyun #define DEFINE_XARRAY(name) DEFINE_XARRAY_FLAGS(name, 0)
328*4882a593Smuzhiyun 
329*4882a593Smuzhiyun /**
330*4882a593Smuzhiyun  * DEFINE_XARRAY_ALLOC() - Define an XArray which allocates IDs starting at 0.
331*4882a593Smuzhiyun  * @name: A string that names your XArray.
332*4882a593Smuzhiyun  *
333*4882a593Smuzhiyun  * This is intended for file scope definitions of allocating XArrays.
334*4882a593Smuzhiyun  * See also DEFINE_XARRAY().
335*4882a593Smuzhiyun  */
336*4882a593Smuzhiyun #define DEFINE_XARRAY_ALLOC(name) DEFINE_XARRAY_FLAGS(name, XA_FLAGS_ALLOC)
337*4882a593Smuzhiyun 
338*4882a593Smuzhiyun /**
339*4882a593Smuzhiyun  * DEFINE_XARRAY_ALLOC1() - Define an XArray which allocates IDs starting at 1.
340*4882a593Smuzhiyun  * @name: A string that names your XArray.
341*4882a593Smuzhiyun  *
342*4882a593Smuzhiyun  * This is intended for file scope definitions of allocating XArrays.
343*4882a593Smuzhiyun  * See also DEFINE_XARRAY().
344*4882a593Smuzhiyun  */
345*4882a593Smuzhiyun #define DEFINE_XARRAY_ALLOC1(name) DEFINE_XARRAY_FLAGS(name, XA_FLAGS_ALLOC1)
346*4882a593Smuzhiyun 
347*4882a593Smuzhiyun void *xa_load(struct xarray *, unsigned long index);
348*4882a593Smuzhiyun void *xa_store(struct xarray *, unsigned long index, void *entry, gfp_t);
349*4882a593Smuzhiyun void *xa_erase(struct xarray *, unsigned long index);
350*4882a593Smuzhiyun void *xa_store_range(struct xarray *, unsigned long first, unsigned long last,
351*4882a593Smuzhiyun 			void *entry, gfp_t);
352*4882a593Smuzhiyun bool xa_get_mark(struct xarray *, unsigned long index, xa_mark_t);
353*4882a593Smuzhiyun void xa_set_mark(struct xarray *, unsigned long index, xa_mark_t);
354*4882a593Smuzhiyun void xa_clear_mark(struct xarray *, unsigned long index, xa_mark_t);
355*4882a593Smuzhiyun void *xa_find(struct xarray *xa, unsigned long *index,
356*4882a593Smuzhiyun 		unsigned long max, xa_mark_t) __attribute__((nonnull(2)));
357*4882a593Smuzhiyun void *xa_find_after(struct xarray *xa, unsigned long *index,
358*4882a593Smuzhiyun 		unsigned long max, xa_mark_t) __attribute__((nonnull(2)));
359*4882a593Smuzhiyun unsigned int xa_extract(struct xarray *, void **dst, unsigned long start,
360*4882a593Smuzhiyun 		unsigned long max, unsigned int n, xa_mark_t);
361*4882a593Smuzhiyun void xa_destroy(struct xarray *);
362*4882a593Smuzhiyun 
363*4882a593Smuzhiyun /**
364*4882a593Smuzhiyun  * xa_init_flags() - Initialise an empty XArray with flags.
365*4882a593Smuzhiyun  * @xa: XArray.
366*4882a593Smuzhiyun  * @flags: XA_FLAG values.
367*4882a593Smuzhiyun  *
368*4882a593Smuzhiyun  * If you need to initialise an XArray with special flags (eg you need
369*4882a593Smuzhiyun  * to take the lock from interrupt context), use this function instead
370*4882a593Smuzhiyun  * of xa_init().
371*4882a593Smuzhiyun  *
372*4882a593Smuzhiyun  * Context: Any context.
373*4882a593Smuzhiyun  */
xa_init_flags(struct xarray * xa,gfp_t flags)374*4882a593Smuzhiyun static inline void xa_init_flags(struct xarray *xa, gfp_t flags)
375*4882a593Smuzhiyun {
376*4882a593Smuzhiyun 	spin_lock_init(&xa->xa_lock);
377*4882a593Smuzhiyun 	xa->xa_flags = flags;
378*4882a593Smuzhiyun 	xa->xa_head = NULL;
379*4882a593Smuzhiyun }
380*4882a593Smuzhiyun 
381*4882a593Smuzhiyun /**
382*4882a593Smuzhiyun  * xa_init() - Initialise an empty XArray.
383*4882a593Smuzhiyun  * @xa: XArray.
384*4882a593Smuzhiyun  *
385*4882a593Smuzhiyun  * An empty XArray is full of NULL entries.
386*4882a593Smuzhiyun  *
387*4882a593Smuzhiyun  * Context: Any context.
388*4882a593Smuzhiyun  */
xa_init(struct xarray * xa)389*4882a593Smuzhiyun static inline void xa_init(struct xarray *xa)
390*4882a593Smuzhiyun {
391*4882a593Smuzhiyun 	xa_init_flags(xa, 0);
392*4882a593Smuzhiyun }
393*4882a593Smuzhiyun 
394*4882a593Smuzhiyun /**
395*4882a593Smuzhiyun  * xa_empty() - Determine if an array has any present entries.
396*4882a593Smuzhiyun  * @xa: XArray.
397*4882a593Smuzhiyun  *
398*4882a593Smuzhiyun  * Context: Any context.
399*4882a593Smuzhiyun  * Return: %true if the array contains only NULL pointers.
400*4882a593Smuzhiyun  */
xa_empty(const struct xarray * xa)401*4882a593Smuzhiyun static inline bool xa_empty(const struct xarray *xa)
402*4882a593Smuzhiyun {
403*4882a593Smuzhiyun 	return xa->xa_head == NULL;
404*4882a593Smuzhiyun }
405*4882a593Smuzhiyun 
406*4882a593Smuzhiyun /**
407*4882a593Smuzhiyun  * xa_marked() - Inquire whether any entry in this array has a mark set
408*4882a593Smuzhiyun  * @xa: Array
409*4882a593Smuzhiyun  * @mark: Mark value
410*4882a593Smuzhiyun  *
411*4882a593Smuzhiyun  * Context: Any context.
412*4882a593Smuzhiyun  * Return: %true if any entry has this mark set.
413*4882a593Smuzhiyun  */
xa_marked(const struct xarray * xa,xa_mark_t mark)414*4882a593Smuzhiyun static inline bool xa_marked(const struct xarray *xa, xa_mark_t mark)
415*4882a593Smuzhiyun {
416*4882a593Smuzhiyun 	return xa->xa_flags & XA_FLAGS_MARK(mark);
417*4882a593Smuzhiyun }
418*4882a593Smuzhiyun 
419*4882a593Smuzhiyun /**
420*4882a593Smuzhiyun  * xa_for_each_range() - Iterate over a portion of an XArray.
421*4882a593Smuzhiyun  * @xa: XArray.
422*4882a593Smuzhiyun  * @index: Index of @entry.
423*4882a593Smuzhiyun  * @entry: Entry retrieved from array.
424*4882a593Smuzhiyun  * @start: First index to retrieve from array.
425*4882a593Smuzhiyun  * @last: Last index to retrieve from array.
426*4882a593Smuzhiyun  *
427*4882a593Smuzhiyun  * During the iteration, @entry will have the value of the entry stored
428*4882a593Smuzhiyun  * in @xa at @index.  You may modify @index during the iteration if you
429*4882a593Smuzhiyun  * want to skip or reprocess indices.  It is safe to modify the array
430*4882a593Smuzhiyun  * during the iteration.  At the end of the iteration, @entry will be set
431*4882a593Smuzhiyun  * to NULL and @index will have a value less than or equal to max.
432*4882a593Smuzhiyun  *
433*4882a593Smuzhiyun  * xa_for_each_range() is O(n.log(n)) while xas_for_each() is O(n).  You have
434*4882a593Smuzhiyun  * to handle your own locking with xas_for_each(), and if you have to unlock
435*4882a593Smuzhiyun  * after each iteration, it will also end up being O(n.log(n)).
436*4882a593Smuzhiyun  * xa_for_each_range() will spin if it hits a retry entry; if you intend to
437*4882a593Smuzhiyun  * see retry entries, you should use the xas_for_each() iterator instead.
438*4882a593Smuzhiyun  * The xas_for_each() iterator will expand into more inline code than
439*4882a593Smuzhiyun  * xa_for_each_range().
440*4882a593Smuzhiyun  *
441*4882a593Smuzhiyun  * Context: Any context.  Takes and releases the RCU lock.
442*4882a593Smuzhiyun  */
443*4882a593Smuzhiyun #define xa_for_each_range(xa, index, entry, start, last)		\
444*4882a593Smuzhiyun 	for (index = start,						\
445*4882a593Smuzhiyun 	     entry = xa_find(xa, &index, last, XA_PRESENT);		\
446*4882a593Smuzhiyun 	     entry;							\
447*4882a593Smuzhiyun 	     entry = xa_find_after(xa, &index, last, XA_PRESENT))
448*4882a593Smuzhiyun 
449*4882a593Smuzhiyun /**
450*4882a593Smuzhiyun  * xa_for_each_start() - Iterate over a portion of an XArray.
451*4882a593Smuzhiyun  * @xa: XArray.
452*4882a593Smuzhiyun  * @index: Index of @entry.
453*4882a593Smuzhiyun  * @entry: Entry retrieved from array.
454*4882a593Smuzhiyun  * @start: First index to retrieve from array.
455*4882a593Smuzhiyun  *
456*4882a593Smuzhiyun  * During the iteration, @entry will have the value of the entry stored
457*4882a593Smuzhiyun  * in @xa at @index.  You may modify @index during the iteration if you
458*4882a593Smuzhiyun  * want to skip or reprocess indices.  It is safe to modify the array
459*4882a593Smuzhiyun  * during the iteration.  At the end of the iteration, @entry will be set
460*4882a593Smuzhiyun  * to NULL and @index will have a value less than or equal to max.
461*4882a593Smuzhiyun  *
462*4882a593Smuzhiyun  * xa_for_each_start() is O(n.log(n)) while xas_for_each() is O(n).  You have
463*4882a593Smuzhiyun  * to handle your own locking with xas_for_each(), and if you have to unlock
464*4882a593Smuzhiyun  * after each iteration, it will also end up being O(n.log(n)).
465*4882a593Smuzhiyun  * xa_for_each_start() will spin if it hits a retry entry; if you intend to
466*4882a593Smuzhiyun  * see retry entries, you should use the xas_for_each() iterator instead.
467*4882a593Smuzhiyun  * The xas_for_each() iterator will expand into more inline code than
468*4882a593Smuzhiyun  * xa_for_each_start().
469*4882a593Smuzhiyun  *
470*4882a593Smuzhiyun  * Context: Any context.  Takes and releases the RCU lock.
471*4882a593Smuzhiyun  */
472*4882a593Smuzhiyun #define xa_for_each_start(xa, index, entry, start) \
473*4882a593Smuzhiyun 	xa_for_each_range(xa, index, entry, start, ULONG_MAX)
474*4882a593Smuzhiyun 
475*4882a593Smuzhiyun /**
476*4882a593Smuzhiyun  * xa_for_each() - Iterate over present entries in an XArray.
477*4882a593Smuzhiyun  * @xa: XArray.
478*4882a593Smuzhiyun  * @index: Index of @entry.
479*4882a593Smuzhiyun  * @entry: Entry retrieved from array.
480*4882a593Smuzhiyun  *
481*4882a593Smuzhiyun  * During the iteration, @entry will have the value of the entry stored
482*4882a593Smuzhiyun  * in @xa at @index.  You may modify @index during the iteration if you want
483*4882a593Smuzhiyun  * to skip or reprocess indices.  It is safe to modify the array during the
484*4882a593Smuzhiyun  * iteration.  At the end of the iteration, @entry will be set to NULL and
485*4882a593Smuzhiyun  * @index will have a value less than or equal to max.
486*4882a593Smuzhiyun  *
487*4882a593Smuzhiyun  * xa_for_each() is O(n.log(n)) while xas_for_each() is O(n).  You have
488*4882a593Smuzhiyun  * to handle your own locking with xas_for_each(), and if you have to unlock
489*4882a593Smuzhiyun  * after each iteration, it will also end up being O(n.log(n)).  xa_for_each()
490*4882a593Smuzhiyun  * will spin if it hits a retry entry; if you intend to see retry entries,
491*4882a593Smuzhiyun  * you should use the xas_for_each() iterator instead.  The xas_for_each()
492*4882a593Smuzhiyun  * iterator will expand into more inline code than xa_for_each().
493*4882a593Smuzhiyun  *
494*4882a593Smuzhiyun  * Context: Any context.  Takes and releases the RCU lock.
495*4882a593Smuzhiyun  */
496*4882a593Smuzhiyun #define xa_for_each(xa, index, entry) \
497*4882a593Smuzhiyun 	xa_for_each_start(xa, index, entry, 0)
498*4882a593Smuzhiyun 
499*4882a593Smuzhiyun /**
500*4882a593Smuzhiyun  * xa_for_each_marked() - Iterate over marked entries in an XArray.
501*4882a593Smuzhiyun  * @xa: XArray.
502*4882a593Smuzhiyun  * @index: Index of @entry.
503*4882a593Smuzhiyun  * @entry: Entry retrieved from array.
504*4882a593Smuzhiyun  * @filter: Selection criterion.
505*4882a593Smuzhiyun  *
506*4882a593Smuzhiyun  * During the iteration, @entry will have the value of the entry stored
507*4882a593Smuzhiyun  * in @xa at @index.  The iteration will skip all entries in the array
508*4882a593Smuzhiyun  * which do not match @filter.  You may modify @index during the iteration
509*4882a593Smuzhiyun  * if you want to skip or reprocess indices.  It is safe to modify the array
510*4882a593Smuzhiyun  * during the iteration.  At the end of the iteration, @entry will be set to
511*4882a593Smuzhiyun  * NULL and @index will have a value less than or equal to max.
512*4882a593Smuzhiyun  *
513*4882a593Smuzhiyun  * xa_for_each_marked() is O(n.log(n)) while xas_for_each_marked() is O(n).
514*4882a593Smuzhiyun  * You have to handle your own locking with xas_for_each(), and if you have
515*4882a593Smuzhiyun  * to unlock after each iteration, it will also end up being O(n.log(n)).
516*4882a593Smuzhiyun  * xa_for_each_marked() will spin if it hits a retry entry; if you intend to
517*4882a593Smuzhiyun  * see retry entries, you should use the xas_for_each_marked() iterator
518*4882a593Smuzhiyun  * instead.  The xas_for_each_marked() iterator will expand into more inline
519*4882a593Smuzhiyun  * code than xa_for_each_marked().
520*4882a593Smuzhiyun  *
521*4882a593Smuzhiyun  * Context: Any context.  Takes and releases the RCU lock.
522*4882a593Smuzhiyun  */
523*4882a593Smuzhiyun #define xa_for_each_marked(xa, index, entry, filter) \
524*4882a593Smuzhiyun 	for (index = 0, entry = xa_find(xa, &index, ULONG_MAX, filter); \
525*4882a593Smuzhiyun 	     entry; entry = xa_find_after(xa, &index, ULONG_MAX, filter))
526*4882a593Smuzhiyun 
527*4882a593Smuzhiyun #define xa_trylock(xa)		spin_trylock(&(xa)->xa_lock)
528*4882a593Smuzhiyun #define xa_lock(xa)		spin_lock(&(xa)->xa_lock)
529*4882a593Smuzhiyun #define xa_unlock(xa)		spin_unlock(&(xa)->xa_lock)
530*4882a593Smuzhiyun #define xa_lock_bh(xa)		spin_lock_bh(&(xa)->xa_lock)
531*4882a593Smuzhiyun #define xa_unlock_bh(xa)	spin_unlock_bh(&(xa)->xa_lock)
532*4882a593Smuzhiyun #define xa_lock_irq(xa)		spin_lock_irq(&(xa)->xa_lock)
533*4882a593Smuzhiyun #define xa_unlock_irq(xa)	spin_unlock_irq(&(xa)->xa_lock)
534*4882a593Smuzhiyun #define xa_lock_irqsave(xa, flags) \
535*4882a593Smuzhiyun 				spin_lock_irqsave(&(xa)->xa_lock, flags)
536*4882a593Smuzhiyun #define xa_unlock_irqrestore(xa, flags) \
537*4882a593Smuzhiyun 				spin_unlock_irqrestore(&(xa)->xa_lock, flags)
538*4882a593Smuzhiyun #define xa_lock_nested(xa, subclass) \
539*4882a593Smuzhiyun 				spin_lock_nested(&(xa)->xa_lock, subclass)
540*4882a593Smuzhiyun #define xa_lock_bh_nested(xa, subclass) \
541*4882a593Smuzhiyun 				spin_lock_bh_nested(&(xa)->xa_lock, subclass)
542*4882a593Smuzhiyun #define xa_lock_irq_nested(xa, subclass) \
543*4882a593Smuzhiyun 				spin_lock_irq_nested(&(xa)->xa_lock, subclass)
544*4882a593Smuzhiyun #define xa_lock_irqsave_nested(xa, flags, subclass) \
545*4882a593Smuzhiyun 		spin_lock_irqsave_nested(&(xa)->xa_lock, flags, subclass)
546*4882a593Smuzhiyun 
547*4882a593Smuzhiyun /*
548*4882a593Smuzhiyun  * Versions of the normal API which require the caller to hold the
549*4882a593Smuzhiyun  * xa_lock.  If the GFP flags allow it, they will drop the lock to
550*4882a593Smuzhiyun  * allocate memory, then reacquire it afterwards.  These functions
551*4882a593Smuzhiyun  * may also re-enable interrupts if the XArray flags indicate the
552*4882a593Smuzhiyun  * locking should be interrupt safe.
553*4882a593Smuzhiyun  */
554*4882a593Smuzhiyun void *__xa_erase(struct xarray *, unsigned long index);
555*4882a593Smuzhiyun void *__xa_store(struct xarray *, unsigned long index, void *entry, gfp_t);
556*4882a593Smuzhiyun void *__xa_cmpxchg(struct xarray *, unsigned long index, void *old,
557*4882a593Smuzhiyun 		void *entry, gfp_t);
558*4882a593Smuzhiyun int __must_check __xa_insert(struct xarray *, unsigned long index,
559*4882a593Smuzhiyun 		void *entry, gfp_t);
560*4882a593Smuzhiyun int __must_check __xa_alloc(struct xarray *, u32 *id, void *entry,
561*4882a593Smuzhiyun 		struct xa_limit, gfp_t);
562*4882a593Smuzhiyun int __must_check __xa_alloc_cyclic(struct xarray *, u32 *id, void *entry,
563*4882a593Smuzhiyun 		struct xa_limit, u32 *next, gfp_t);
564*4882a593Smuzhiyun void __xa_set_mark(struct xarray *, unsigned long index, xa_mark_t);
565*4882a593Smuzhiyun void __xa_clear_mark(struct xarray *, unsigned long index, xa_mark_t);
566*4882a593Smuzhiyun 
567*4882a593Smuzhiyun /**
568*4882a593Smuzhiyun  * xa_store_bh() - Store this entry in the XArray.
569*4882a593Smuzhiyun  * @xa: XArray.
570*4882a593Smuzhiyun  * @index: Index into array.
571*4882a593Smuzhiyun  * @entry: New entry.
572*4882a593Smuzhiyun  * @gfp: Memory allocation flags.
573*4882a593Smuzhiyun  *
574*4882a593Smuzhiyun  * This function is like calling xa_store() except it disables softirqs
575*4882a593Smuzhiyun  * while holding the array lock.
576*4882a593Smuzhiyun  *
577*4882a593Smuzhiyun  * Context: Any context.  Takes and releases the xa_lock while
578*4882a593Smuzhiyun  * disabling softirqs.
579*4882a593Smuzhiyun  * Return: The old entry at this index or xa_err() if an error happened.
580*4882a593Smuzhiyun  */
xa_store_bh(struct xarray * xa,unsigned long index,void * entry,gfp_t gfp)581*4882a593Smuzhiyun static inline void *xa_store_bh(struct xarray *xa, unsigned long index,
582*4882a593Smuzhiyun 		void *entry, gfp_t gfp)
583*4882a593Smuzhiyun {
584*4882a593Smuzhiyun 	void *curr;
585*4882a593Smuzhiyun 
586*4882a593Smuzhiyun 	xa_lock_bh(xa);
587*4882a593Smuzhiyun 	curr = __xa_store(xa, index, entry, gfp);
588*4882a593Smuzhiyun 	xa_unlock_bh(xa);
589*4882a593Smuzhiyun 
590*4882a593Smuzhiyun 	return curr;
591*4882a593Smuzhiyun }
592*4882a593Smuzhiyun 
593*4882a593Smuzhiyun /**
594*4882a593Smuzhiyun  * xa_store_irq() - Store this entry in the XArray.
595*4882a593Smuzhiyun  * @xa: XArray.
596*4882a593Smuzhiyun  * @index: Index into array.
597*4882a593Smuzhiyun  * @entry: New entry.
598*4882a593Smuzhiyun  * @gfp: Memory allocation flags.
599*4882a593Smuzhiyun  *
600*4882a593Smuzhiyun  * This function is like calling xa_store() except it disables interrupts
601*4882a593Smuzhiyun  * while holding the array lock.
602*4882a593Smuzhiyun  *
603*4882a593Smuzhiyun  * Context: Process context.  Takes and releases the xa_lock while
604*4882a593Smuzhiyun  * disabling interrupts.
605*4882a593Smuzhiyun  * Return: The old entry at this index or xa_err() if an error happened.
606*4882a593Smuzhiyun  */
xa_store_irq(struct xarray * xa,unsigned long index,void * entry,gfp_t gfp)607*4882a593Smuzhiyun static inline void *xa_store_irq(struct xarray *xa, unsigned long index,
608*4882a593Smuzhiyun 		void *entry, gfp_t gfp)
609*4882a593Smuzhiyun {
610*4882a593Smuzhiyun 	void *curr;
611*4882a593Smuzhiyun 
612*4882a593Smuzhiyun 	xa_lock_irq(xa);
613*4882a593Smuzhiyun 	curr = __xa_store(xa, index, entry, gfp);
614*4882a593Smuzhiyun 	xa_unlock_irq(xa);
615*4882a593Smuzhiyun 
616*4882a593Smuzhiyun 	return curr;
617*4882a593Smuzhiyun }
618*4882a593Smuzhiyun 
619*4882a593Smuzhiyun /**
620*4882a593Smuzhiyun  * xa_erase_bh() - Erase this entry from the XArray.
621*4882a593Smuzhiyun  * @xa: XArray.
622*4882a593Smuzhiyun  * @index: Index of entry.
623*4882a593Smuzhiyun  *
624*4882a593Smuzhiyun  * After this function returns, loading from @index will return %NULL.
625*4882a593Smuzhiyun  * If the index is part of a multi-index entry, all indices will be erased
626*4882a593Smuzhiyun  * and none of the entries will be part of a multi-index entry.
627*4882a593Smuzhiyun  *
628*4882a593Smuzhiyun  * Context: Any context.  Takes and releases the xa_lock while
629*4882a593Smuzhiyun  * disabling softirqs.
630*4882a593Smuzhiyun  * Return: The entry which used to be at this index.
631*4882a593Smuzhiyun  */
xa_erase_bh(struct xarray * xa,unsigned long index)632*4882a593Smuzhiyun static inline void *xa_erase_bh(struct xarray *xa, unsigned long index)
633*4882a593Smuzhiyun {
634*4882a593Smuzhiyun 	void *entry;
635*4882a593Smuzhiyun 
636*4882a593Smuzhiyun 	xa_lock_bh(xa);
637*4882a593Smuzhiyun 	entry = __xa_erase(xa, index);
638*4882a593Smuzhiyun 	xa_unlock_bh(xa);
639*4882a593Smuzhiyun 
640*4882a593Smuzhiyun 	return entry;
641*4882a593Smuzhiyun }
642*4882a593Smuzhiyun 
643*4882a593Smuzhiyun /**
644*4882a593Smuzhiyun  * xa_erase_irq() - Erase this entry from the XArray.
645*4882a593Smuzhiyun  * @xa: XArray.
646*4882a593Smuzhiyun  * @index: Index of entry.
647*4882a593Smuzhiyun  *
648*4882a593Smuzhiyun  * After this function returns, loading from @index will return %NULL.
649*4882a593Smuzhiyun  * If the index is part of a multi-index entry, all indices will be erased
650*4882a593Smuzhiyun  * and none of the entries will be part of a multi-index entry.
651*4882a593Smuzhiyun  *
652*4882a593Smuzhiyun  * Context: Process context.  Takes and releases the xa_lock while
653*4882a593Smuzhiyun  * disabling interrupts.
654*4882a593Smuzhiyun  * Return: The entry which used to be at this index.
655*4882a593Smuzhiyun  */
xa_erase_irq(struct xarray * xa,unsigned long index)656*4882a593Smuzhiyun static inline void *xa_erase_irq(struct xarray *xa, unsigned long index)
657*4882a593Smuzhiyun {
658*4882a593Smuzhiyun 	void *entry;
659*4882a593Smuzhiyun 
660*4882a593Smuzhiyun 	xa_lock_irq(xa);
661*4882a593Smuzhiyun 	entry = __xa_erase(xa, index);
662*4882a593Smuzhiyun 	xa_unlock_irq(xa);
663*4882a593Smuzhiyun 
664*4882a593Smuzhiyun 	return entry;
665*4882a593Smuzhiyun }
666*4882a593Smuzhiyun 
667*4882a593Smuzhiyun /**
668*4882a593Smuzhiyun  * xa_cmpxchg() - Conditionally replace an entry in the XArray.
669*4882a593Smuzhiyun  * @xa: XArray.
670*4882a593Smuzhiyun  * @index: Index into array.
671*4882a593Smuzhiyun  * @old: Old value to test against.
672*4882a593Smuzhiyun  * @entry: New value to place in array.
673*4882a593Smuzhiyun  * @gfp: Memory allocation flags.
674*4882a593Smuzhiyun  *
675*4882a593Smuzhiyun  * If the entry at @index is the same as @old, replace it with @entry.
676*4882a593Smuzhiyun  * If the return value is equal to @old, then the exchange was successful.
677*4882a593Smuzhiyun  *
678*4882a593Smuzhiyun  * Context: Any context.  Takes and releases the xa_lock.  May sleep
679*4882a593Smuzhiyun  * if the @gfp flags permit.
680*4882a593Smuzhiyun  * Return: The old value at this index or xa_err() if an error happened.
681*4882a593Smuzhiyun  */
xa_cmpxchg(struct xarray * xa,unsigned long index,void * old,void * entry,gfp_t gfp)682*4882a593Smuzhiyun static inline void *xa_cmpxchg(struct xarray *xa, unsigned long index,
683*4882a593Smuzhiyun 			void *old, void *entry, gfp_t gfp)
684*4882a593Smuzhiyun {
685*4882a593Smuzhiyun 	void *curr;
686*4882a593Smuzhiyun 
687*4882a593Smuzhiyun 	xa_lock(xa);
688*4882a593Smuzhiyun 	curr = __xa_cmpxchg(xa, index, old, entry, gfp);
689*4882a593Smuzhiyun 	xa_unlock(xa);
690*4882a593Smuzhiyun 
691*4882a593Smuzhiyun 	return curr;
692*4882a593Smuzhiyun }
693*4882a593Smuzhiyun 
694*4882a593Smuzhiyun /**
695*4882a593Smuzhiyun  * xa_cmpxchg_bh() - Conditionally replace an entry in the XArray.
696*4882a593Smuzhiyun  * @xa: XArray.
697*4882a593Smuzhiyun  * @index: Index into array.
698*4882a593Smuzhiyun  * @old: Old value to test against.
699*4882a593Smuzhiyun  * @entry: New value to place in array.
700*4882a593Smuzhiyun  * @gfp: Memory allocation flags.
701*4882a593Smuzhiyun  *
702*4882a593Smuzhiyun  * This function is like calling xa_cmpxchg() except it disables softirqs
703*4882a593Smuzhiyun  * while holding the array lock.
704*4882a593Smuzhiyun  *
705*4882a593Smuzhiyun  * Context: Any context.  Takes and releases the xa_lock while
706*4882a593Smuzhiyun  * disabling softirqs.  May sleep if the @gfp flags permit.
707*4882a593Smuzhiyun  * Return: The old value at this index or xa_err() if an error happened.
708*4882a593Smuzhiyun  */
xa_cmpxchg_bh(struct xarray * xa,unsigned long index,void * old,void * entry,gfp_t gfp)709*4882a593Smuzhiyun static inline void *xa_cmpxchg_bh(struct xarray *xa, unsigned long index,
710*4882a593Smuzhiyun 			void *old, void *entry, gfp_t gfp)
711*4882a593Smuzhiyun {
712*4882a593Smuzhiyun 	void *curr;
713*4882a593Smuzhiyun 
714*4882a593Smuzhiyun 	xa_lock_bh(xa);
715*4882a593Smuzhiyun 	curr = __xa_cmpxchg(xa, index, old, entry, gfp);
716*4882a593Smuzhiyun 	xa_unlock_bh(xa);
717*4882a593Smuzhiyun 
718*4882a593Smuzhiyun 	return curr;
719*4882a593Smuzhiyun }
720*4882a593Smuzhiyun 
721*4882a593Smuzhiyun /**
722*4882a593Smuzhiyun  * xa_cmpxchg_irq() - Conditionally replace an entry in the XArray.
723*4882a593Smuzhiyun  * @xa: XArray.
724*4882a593Smuzhiyun  * @index: Index into array.
725*4882a593Smuzhiyun  * @old: Old value to test against.
726*4882a593Smuzhiyun  * @entry: New value to place in array.
727*4882a593Smuzhiyun  * @gfp: Memory allocation flags.
728*4882a593Smuzhiyun  *
729*4882a593Smuzhiyun  * This function is like calling xa_cmpxchg() except it disables interrupts
730*4882a593Smuzhiyun  * while holding the array lock.
731*4882a593Smuzhiyun  *
732*4882a593Smuzhiyun  * Context: Process context.  Takes and releases the xa_lock while
733*4882a593Smuzhiyun  * disabling interrupts.  May sleep if the @gfp flags permit.
734*4882a593Smuzhiyun  * Return: The old value at this index or xa_err() if an error happened.
735*4882a593Smuzhiyun  */
xa_cmpxchg_irq(struct xarray * xa,unsigned long index,void * old,void * entry,gfp_t gfp)736*4882a593Smuzhiyun static inline void *xa_cmpxchg_irq(struct xarray *xa, unsigned long index,
737*4882a593Smuzhiyun 			void *old, void *entry, gfp_t gfp)
738*4882a593Smuzhiyun {
739*4882a593Smuzhiyun 	void *curr;
740*4882a593Smuzhiyun 
741*4882a593Smuzhiyun 	xa_lock_irq(xa);
742*4882a593Smuzhiyun 	curr = __xa_cmpxchg(xa, index, old, entry, gfp);
743*4882a593Smuzhiyun 	xa_unlock_irq(xa);
744*4882a593Smuzhiyun 
745*4882a593Smuzhiyun 	return curr;
746*4882a593Smuzhiyun }
747*4882a593Smuzhiyun 
748*4882a593Smuzhiyun /**
749*4882a593Smuzhiyun  * xa_insert() - Store this entry in the XArray unless another entry is
750*4882a593Smuzhiyun  *			already present.
751*4882a593Smuzhiyun  * @xa: XArray.
752*4882a593Smuzhiyun  * @index: Index into array.
753*4882a593Smuzhiyun  * @entry: New entry.
754*4882a593Smuzhiyun  * @gfp: Memory allocation flags.
755*4882a593Smuzhiyun  *
756*4882a593Smuzhiyun  * Inserting a NULL entry will store a reserved entry (like xa_reserve())
757*4882a593Smuzhiyun  * if no entry is present.  Inserting will fail if a reserved entry is
758*4882a593Smuzhiyun  * present, even though loading from this index will return NULL.
759*4882a593Smuzhiyun  *
760*4882a593Smuzhiyun  * Context: Any context.  Takes and releases the xa_lock.  May sleep if
761*4882a593Smuzhiyun  * the @gfp flags permit.
762*4882a593Smuzhiyun  * Return: 0 if the store succeeded.  -EBUSY if another entry was present.
763*4882a593Smuzhiyun  * -ENOMEM if memory could not be allocated.
764*4882a593Smuzhiyun  */
xa_insert(struct xarray * xa,unsigned long index,void * entry,gfp_t gfp)765*4882a593Smuzhiyun static inline int __must_check xa_insert(struct xarray *xa,
766*4882a593Smuzhiyun 		unsigned long index, void *entry, gfp_t gfp)
767*4882a593Smuzhiyun {
768*4882a593Smuzhiyun 	int err;
769*4882a593Smuzhiyun 
770*4882a593Smuzhiyun 	xa_lock(xa);
771*4882a593Smuzhiyun 	err = __xa_insert(xa, index, entry, gfp);
772*4882a593Smuzhiyun 	xa_unlock(xa);
773*4882a593Smuzhiyun 
774*4882a593Smuzhiyun 	return err;
775*4882a593Smuzhiyun }
776*4882a593Smuzhiyun 
777*4882a593Smuzhiyun /**
778*4882a593Smuzhiyun  * xa_insert_bh() - Store this entry in the XArray unless another entry is
779*4882a593Smuzhiyun  *			already present.
780*4882a593Smuzhiyun  * @xa: XArray.
781*4882a593Smuzhiyun  * @index: Index into array.
782*4882a593Smuzhiyun  * @entry: New entry.
783*4882a593Smuzhiyun  * @gfp: Memory allocation flags.
784*4882a593Smuzhiyun  *
785*4882a593Smuzhiyun  * Inserting a NULL entry will store a reserved entry (like xa_reserve())
786*4882a593Smuzhiyun  * if no entry is present.  Inserting will fail if a reserved entry is
787*4882a593Smuzhiyun  * present, even though loading from this index will return NULL.
788*4882a593Smuzhiyun  *
789*4882a593Smuzhiyun  * Context: Any context.  Takes and releases the xa_lock while
790*4882a593Smuzhiyun  * disabling softirqs.  May sleep if the @gfp flags permit.
791*4882a593Smuzhiyun  * Return: 0 if the store succeeded.  -EBUSY if another entry was present.
792*4882a593Smuzhiyun  * -ENOMEM if memory could not be allocated.
793*4882a593Smuzhiyun  */
xa_insert_bh(struct xarray * xa,unsigned long index,void * entry,gfp_t gfp)794*4882a593Smuzhiyun static inline int __must_check xa_insert_bh(struct xarray *xa,
795*4882a593Smuzhiyun 		unsigned long index, void *entry, gfp_t gfp)
796*4882a593Smuzhiyun {
797*4882a593Smuzhiyun 	int err;
798*4882a593Smuzhiyun 
799*4882a593Smuzhiyun 	xa_lock_bh(xa);
800*4882a593Smuzhiyun 	err = __xa_insert(xa, index, entry, gfp);
801*4882a593Smuzhiyun 	xa_unlock_bh(xa);
802*4882a593Smuzhiyun 
803*4882a593Smuzhiyun 	return err;
804*4882a593Smuzhiyun }
805*4882a593Smuzhiyun 
806*4882a593Smuzhiyun /**
807*4882a593Smuzhiyun  * xa_insert_irq() - Store this entry in the XArray unless another entry is
808*4882a593Smuzhiyun  *			already present.
809*4882a593Smuzhiyun  * @xa: XArray.
810*4882a593Smuzhiyun  * @index: Index into array.
811*4882a593Smuzhiyun  * @entry: New entry.
812*4882a593Smuzhiyun  * @gfp: Memory allocation flags.
813*4882a593Smuzhiyun  *
814*4882a593Smuzhiyun  * Inserting a NULL entry will store a reserved entry (like xa_reserve())
815*4882a593Smuzhiyun  * if no entry is present.  Inserting will fail if a reserved entry is
816*4882a593Smuzhiyun  * present, even though loading from this index will return NULL.
817*4882a593Smuzhiyun  *
818*4882a593Smuzhiyun  * Context: Process context.  Takes and releases the xa_lock while
819*4882a593Smuzhiyun  * disabling interrupts.  May sleep if the @gfp flags permit.
820*4882a593Smuzhiyun  * Return: 0 if the store succeeded.  -EBUSY if another entry was present.
821*4882a593Smuzhiyun  * -ENOMEM if memory could not be allocated.
822*4882a593Smuzhiyun  */
xa_insert_irq(struct xarray * xa,unsigned long index,void * entry,gfp_t gfp)823*4882a593Smuzhiyun static inline int __must_check xa_insert_irq(struct xarray *xa,
824*4882a593Smuzhiyun 		unsigned long index, void *entry, gfp_t gfp)
825*4882a593Smuzhiyun {
826*4882a593Smuzhiyun 	int err;
827*4882a593Smuzhiyun 
828*4882a593Smuzhiyun 	xa_lock_irq(xa);
829*4882a593Smuzhiyun 	err = __xa_insert(xa, index, entry, gfp);
830*4882a593Smuzhiyun 	xa_unlock_irq(xa);
831*4882a593Smuzhiyun 
832*4882a593Smuzhiyun 	return err;
833*4882a593Smuzhiyun }
834*4882a593Smuzhiyun 
835*4882a593Smuzhiyun /**
836*4882a593Smuzhiyun  * xa_alloc() - Find somewhere to store this entry in the XArray.
837*4882a593Smuzhiyun  * @xa: XArray.
838*4882a593Smuzhiyun  * @id: Pointer to ID.
839*4882a593Smuzhiyun  * @entry: New entry.
840*4882a593Smuzhiyun  * @limit: Range of ID to allocate.
841*4882a593Smuzhiyun  * @gfp: Memory allocation flags.
842*4882a593Smuzhiyun  *
843*4882a593Smuzhiyun  * Finds an empty entry in @xa between @limit.min and @limit.max,
844*4882a593Smuzhiyun  * stores the index into the @id pointer, then stores the entry at
845*4882a593Smuzhiyun  * that index.  A concurrent lookup will not see an uninitialised @id.
846*4882a593Smuzhiyun  *
847*4882a593Smuzhiyun  * Context: Any context.  Takes and releases the xa_lock.  May sleep if
848*4882a593Smuzhiyun  * the @gfp flags permit.
849*4882a593Smuzhiyun  * Return: 0 on success, -ENOMEM if memory could not be allocated or
850*4882a593Smuzhiyun  * -EBUSY if there are no free entries in @limit.
851*4882a593Smuzhiyun  */
xa_alloc(struct xarray * xa,u32 * id,void * entry,struct xa_limit limit,gfp_t gfp)852*4882a593Smuzhiyun static inline __must_check int xa_alloc(struct xarray *xa, u32 *id,
853*4882a593Smuzhiyun 		void *entry, struct xa_limit limit, gfp_t gfp)
854*4882a593Smuzhiyun {
855*4882a593Smuzhiyun 	int err;
856*4882a593Smuzhiyun 
857*4882a593Smuzhiyun 	xa_lock(xa);
858*4882a593Smuzhiyun 	err = __xa_alloc(xa, id, entry, limit, gfp);
859*4882a593Smuzhiyun 	xa_unlock(xa);
860*4882a593Smuzhiyun 
861*4882a593Smuzhiyun 	return err;
862*4882a593Smuzhiyun }
863*4882a593Smuzhiyun 
864*4882a593Smuzhiyun /**
865*4882a593Smuzhiyun  * xa_alloc_bh() - Find somewhere to store this entry in the XArray.
866*4882a593Smuzhiyun  * @xa: XArray.
867*4882a593Smuzhiyun  * @id: Pointer to ID.
868*4882a593Smuzhiyun  * @entry: New entry.
869*4882a593Smuzhiyun  * @limit: Range of ID to allocate.
870*4882a593Smuzhiyun  * @gfp: Memory allocation flags.
871*4882a593Smuzhiyun  *
872*4882a593Smuzhiyun  * Finds an empty entry in @xa between @limit.min and @limit.max,
873*4882a593Smuzhiyun  * stores the index into the @id pointer, then stores the entry at
874*4882a593Smuzhiyun  * that index.  A concurrent lookup will not see an uninitialised @id.
875*4882a593Smuzhiyun  *
876*4882a593Smuzhiyun  * Context: Any context.  Takes and releases the xa_lock while
877*4882a593Smuzhiyun  * disabling softirqs.  May sleep if the @gfp flags permit.
878*4882a593Smuzhiyun  * Return: 0 on success, -ENOMEM if memory could not be allocated or
879*4882a593Smuzhiyun  * -EBUSY if there are no free entries in @limit.
880*4882a593Smuzhiyun  */
xa_alloc_bh(struct xarray * xa,u32 * id,void * entry,struct xa_limit limit,gfp_t gfp)881*4882a593Smuzhiyun static inline int __must_check xa_alloc_bh(struct xarray *xa, u32 *id,
882*4882a593Smuzhiyun 		void *entry, struct xa_limit limit, gfp_t gfp)
883*4882a593Smuzhiyun {
884*4882a593Smuzhiyun 	int err;
885*4882a593Smuzhiyun 
886*4882a593Smuzhiyun 	xa_lock_bh(xa);
887*4882a593Smuzhiyun 	err = __xa_alloc(xa, id, entry, limit, gfp);
888*4882a593Smuzhiyun 	xa_unlock_bh(xa);
889*4882a593Smuzhiyun 
890*4882a593Smuzhiyun 	return err;
891*4882a593Smuzhiyun }
892*4882a593Smuzhiyun 
893*4882a593Smuzhiyun /**
894*4882a593Smuzhiyun  * xa_alloc_irq() - Find somewhere to store this entry in the XArray.
895*4882a593Smuzhiyun  * @xa: XArray.
896*4882a593Smuzhiyun  * @id: Pointer to ID.
897*4882a593Smuzhiyun  * @entry: New entry.
898*4882a593Smuzhiyun  * @limit: Range of ID to allocate.
899*4882a593Smuzhiyun  * @gfp: Memory allocation flags.
900*4882a593Smuzhiyun  *
901*4882a593Smuzhiyun  * Finds an empty entry in @xa between @limit.min and @limit.max,
902*4882a593Smuzhiyun  * stores the index into the @id pointer, then stores the entry at
903*4882a593Smuzhiyun  * that index.  A concurrent lookup will not see an uninitialised @id.
904*4882a593Smuzhiyun  *
905*4882a593Smuzhiyun  * Context: Process context.  Takes and releases the xa_lock while
906*4882a593Smuzhiyun  * disabling interrupts.  May sleep if the @gfp flags permit.
907*4882a593Smuzhiyun  * Return: 0 on success, -ENOMEM if memory could not be allocated or
908*4882a593Smuzhiyun  * -EBUSY if there are no free entries in @limit.
909*4882a593Smuzhiyun  */
xa_alloc_irq(struct xarray * xa,u32 * id,void * entry,struct xa_limit limit,gfp_t gfp)910*4882a593Smuzhiyun static inline int __must_check xa_alloc_irq(struct xarray *xa, u32 *id,
911*4882a593Smuzhiyun 		void *entry, struct xa_limit limit, gfp_t gfp)
912*4882a593Smuzhiyun {
913*4882a593Smuzhiyun 	int err;
914*4882a593Smuzhiyun 
915*4882a593Smuzhiyun 	xa_lock_irq(xa);
916*4882a593Smuzhiyun 	err = __xa_alloc(xa, id, entry, limit, gfp);
917*4882a593Smuzhiyun 	xa_unlock_irq(xa);
918*4882a593Smuzhiyun 
919*4882a593Smuzhiyun 	return err;
920*4882a593Smuzhiyun }
921*4882a593Smuzhiyun 
922*4882a593Smuzhiyun /**
923*4882a593Smuzhiyun  * xa_alloc_cyclic() - Find somewhere to store this entry in the XArray.
924*4882a593Smuzhiyun  * @xa: XArray.
925*4882a593Smuzhiyun  * @id: Pointer to ID.
926*4882a593Smuzhiyun  * @entry: New entry.
927*4882a593Smuzhiyun  * @limit: Range of allocated ID.
928*4882a593Smuzhiyun  * @next: Pointer to next ID to allocate.
929*4882a593Smuzhiyun  * @gfp: Memory allocation flags.
930*4882a593Smuzhiyun  *
931*4882a593Smuzhiyun  * Finds an empty entry in @xa between @limit.min and @limit.max,
932*4882a593Smuzhiyun  * stores the index into the @id pointer, then stores the entry at
933*4882a593Smuzhiyun  * that index.  A concurrent lookup will not see an uninitialised @id.
934*4882a593Smuzhiyun  * The search for an empty entry will start at @next and will wrap
935*4882a593Smuzhiyun  * around if necessary.
936*4882a593Smuzhiyun  *
937*4882a593Smuzhiyun  * Context: Any context.  Takes and releases the xa_lock.  May sleep if
938*4882a593Smuzhiyun  * the @gfp flags permit.
939*4882a593Smuzhiyun  * Return: 0 if the allocation succeeded without wrapping.  1 if the
940*4882a593Smuzhiyun  * allocation succeeded after wrapping, -ENOMEM if memory could not be
941*4882a593Smuzhiyun  * allocated or -EBUSY if there are no free entries in @limit.
942*4882a593Smuzhiyun  */
xa_alloc_cyclic(struct xarray * xa,u32 * id,void * entry,struct xa_limit limit,u32 * next,gfp_t gfp)943*4882a593Smuzhiyun static inline int xa_alloc_cyclic(struct xarray *xa, u32 *id, void *entry,
944*4882a593Smuzhiyun 		struct xa_limit limit, u32 *next, gfp_t gfp)
945*4882a593Smuzhiyun {
946*4882a593Smuzhiyun 	int err;
947*4882a593Smuzhiyun 
948*4882a593Smuzhiyun 	xa_lock(xa);
949*4882a593Smuzhiyun 	err = __xa_alloc_cyclic(xa, id, entry, limit, next, gfp);
950*4882a593Smuzhiyun 	xa_unlock(xa);
951*4882a593Smuzhiyun 
952*4882a593Smuzhiyun 	return err;
953*4882a593Smuzhiyun }
954*4882a593Smuzhiyun 
955*4882a593Smuzhiyun /**
956*4882a593Smuzhiyun  * xa_alloc_cyclic_bh() - Find somewhere to store this entry in the XArray.
957*4882a593Smuzhiyun  * @xa: XArray.
958*4882a593Smuzhiyun  * @id: Pointer to ID.
959*4882a593Smuzhiyun  * @entry: New entry.
960*4882a593Smuzhiyun  * @limit: Range of allocated ID.
961*4882a593Smuzhiyun  * @next: Pointer to next ID to allocate.
962*4882a593Smuzhiyun  * @gfp: Memory allocation flags.
963*4882a593Smuzhiyun  *
964*4882a593Smuzhiyun  * Finds an empty entry in @xa between @limit.min and @limit.max,
965*4882a593Smuzhiyun  * stores the index into the @id pointer, then stores the entry at
966*4882a593Smuzhiyun  * that index.  A concurrent lookup will not see an uninitialised @id.
967*4882a593Smuzhiyun  * The search for an empty entry will start at @next and will wrap
968*4882a593Smuzhiyun  * around if necessary.
969*4882a593Smuzhiyun  *
970*4882a593Smuzhiyun  * Context: Any context.  Takes and releases the xa_lock while
971*4882a593Smuzhiyun  * disabling softirqs.  May sleep if the @gfp flags permit.
972*4882a593Smuzhiyun  * Return: 0 if the allocation succeeded without wrapping.  1 if the
973*4882a593Smuzhiyun  * allocation succeeded after wrapping, -ENOMEM if memory could not be
974*4882a593Smuzhiyun  * allocated or -EBUSY if there are no free entries in @limit.
975*4882a593Smuzhiyun  */
xa_alloc_cyclic_bh(struct xarray * xa,u32 * id,void * entry,struct xa_limit limit,u32 * next,gfp_t gfp)976*4882a593Smuzhiyun static inline int xa_alloc_cyclic_bh(struct xarray *xa, u32 *id, void *entry,
977*4882a593Smuzhiyun 		struct xa_limit limit, u32 *next, gfp_t gfp)
978*4882a593Smuzhiyun {
979*4882a593Smuzhiyun 	int err;
980*4882a593Smuzhiyun 
981*4882a593Smuzhiyun 	xa_lock_bh(xa);
982*4882a593Smuzhiyun 	err = __xa_alloc_cyclic(xa, id, entry, limit, next, gfp);
983*4882a593Smuzhiyun 	xa_unlock_bh(xa);
984*4882a593Smuzhiyun 
985*4882a593Smuzhiyun 	return err;
986*4882a593Smuzhiyun }
987*4882a593Smuzhiyun 
988*4882a593Smuzhiyun /**
989*4882a593Smuzhiyun  * xa_alloc_cyclic_irq() - Find somewhere to store this entry in the XArray.
990*4882a593Smuzhiyun  * @xa: XArray.
991*4882a593Smuzhiyun  * @id: Pointer to ID.
992*4882a593Smuzhiyun  * @entry: New entry.
993*4882a593Smuzhiyun  * @limit: Range of allocated ID.
994*4882a593Smuzhiyun  * @next: Pointer to next ID to allocate.
995*4882a593Smuzhiyun  * @gfp: Memory allocation flags.
996*4882a593Smuzhiyun  *
997*4882a593Smuzhiyun  * Finds an empty entry in @xa between @limit.min and @limit.max,
998*4882a593Smuzhiyun  * stores the index into the @id pointer, then stores the entry at
999*4882a593Smuzhiyun  * that index.  A concurrent lookup will not see an uninitialised @id.
1000*4882a593Smuzhiyun  * The search for an empty entry will start at @next and will wrap
1001*4882a593Smuzhiyun  * around if necessary.
1002*4882a593Smuzhiyun  *
1003*4882a593Smuzhiyun  * Context: Process context.  Takes and releases the xa_lock while
1004*4882a593Smuzhiyun  * disabling interrupts.  May sleep if the @gfp flags permit.
1005*4882a593Smuzhiyun  * Return: 0 if the allocation succeeded without wrapping.  1 if the
1006*4882a593Smuzhiyun  * allocation succeeded after wrapping, -ENOMEM if memory could not be
1007*4882a593Smuzhiyun  * allocated or -EBUSY if there are no free entries in @limit.
1008*4882a593Smuzhiyun  */
xa_alloc_cyclic_irq(struct xarray * xa,u32 * id,void * entry,struct xa_limit limit,u32 * next,gfp_t gfp)1009*4882a593Smuzhiyun static inline int xa_alloc_cyclic_irq(struct xarray *xa, u32 *id, void *entry,
1010*4882a593Smuzhiyun 		struct xa_limit limit, u32 *next, gfp_t gfp)
1011*4882a593Smuzhiyun {
1012*4882a593Smuzhiyun 	int err;
1013*4882a593Smuzhiyun 
1014*4882a593Smuzhiyun 	xa_lock_irq(xa);
1015*4882a593Smuzhiyun 	err = __xa_alloc_cyclic(xa, id, entry, limit, next, gfp);
1016*4882a593Smuzhiyun 	xa_unlock_irq(xa);
1017*4882a593Smuzhiyun 
1018*4882a593Smuzhiyun 	return err;
1019*4882a593Smuzhiyun }
1020*4882a593Smuzhiyun 
1021*4882a593Smuzhiyun /**
1022*4882a593Smuzhiyun  * xa_reserve() - Reserve this index in the XArray.
1023*4882a593Smuzhiyun  * @xa: XArray.
1024*4882a593Smuzhiyun  * @index: Index into array.
1025*4882a593Smuzhiyun  * @gfp: Memory allocation flags.
1026*4882a593Smuzhiyun  *
1027*4882a593Smuzhiyun  * Ensures there is somewhere to store an entry at @index in the array.
1028*4882a593Smuzhiyun  * If there is already something stored at @index, this function does
1029*4882a593Smuzhiyun  * nothing.  If there was nothing there, the entry is marked as reserved.
1030*4882a593Smuzhiyun  * Loading from a reserved entry returns a %NULL pointer.
1031*4882a593Smuzhiyun  *
1032*4882a593Smuzhiyun  * If you do not use the entry that you have reserved, call xa_release()
1033*4882a593Smuzhiyun  * or xa_erase() to free any unnecessary memory.
1034*4882a593Smuzhiyun  *
1035*4882a593Smuzhiyun  * Context: Any context.  Takes and releases the xa_lock.
1036*4882a593Smuzhiyun  * May sleep if the @gfp flags permit.
1037*4882a593Smuzhiyun  * Return: 0 if the reservation succeeded or -ENOMEM if it failed.
1038*4882a593Smuzhiyun  */
1039*4882a593Smuzhiyun static inline __must_check
xa_reserve(struct xarray * xa,unsigned long index,gfp_t gfp)1040*4882a593Smuzhiyun int xa_reserve(struct xarray *xa, unsigned long index, gfp_t gfp)
1041*4882a593Smuzhiyun {
1042*4882a593Smuzhiyun 	return xa_err(xa_cmpxchg(xa, index, NULL, XA_ZERO_ENTRY, gfp));
1043*4882a593Smuzhiyun }
1044*4882a593Smuzhiyun 
1045*4882a593Smuzhiyun /**
1046*4882a593Smuzhiyun  * xa_reserve_bh() - Reserve this index in the XArray.
1047*4882a593Smuzhiyun  * @xa: XArray.
1048*4882a593Smuzhiyun  * @index: Index into array.
1049*4882a593Smuzhiyun  * @gfp: Memory allocation flags.
1050*4882a593Smuzhiyun  *
1051*4882a593Smuzhiyun  * A softirq-disabling version of xa_reserve().
1052*4882a593Smuzhiyun  *
1053*4882a593Smuzhiyun  * Context: Any context.  Takes and releases the xa_lock while
1054*4882a593Smuzhiyun  * disabling softirqs.
1055*4882a593Smuzhiyun  * Return: 0 if the reservation succeeded or -ENOMEM if it failed.
1056*4882a593Smuzhiyun  */
1057*4882a593Smuzhiyun static inline __must_check
xa_reserve_bh(struct xarray * xa,unsigned long index,gfp_t gfp)1058*4882a593Smuzhiyun int xa_reserve_bh(struct xarray *xa, unsigned long index, gfp_t gfp)
1059*4882a593Smuzhiyun {
1060*4882a593Smuzhiyun 	return xa_err(xa_cmpxchg_bh(xa, index, NULL, XA_ZERO_ENTRY, gfp));
1061*4882a593Smuzhiyun }
1062*4882a593Smuzhiyun 
1063*4882a593Smuzhiyun /**
1064*4882a593Smuzhiyun  * xa_reserve_irq() - Reserve this index in the XArray.
1065*4882a593Smuzhiyun  * @xa: XArray.
1066*4882a593Smuzhiyun  * @index: Index into array.
1067*4882a593Smuzhiyun  * @gfp: Memory allocation flags.
1068*4882a593Smuzhiyun  *
1069*4882a593Smuzhiyun  * An interrupt-disabling version of xa_reserve().
1070*4882a593Smuzhiyun  *
1071*4882a593Smuzhiyun  * Context: Process context.  Takes and releases the xa_lock while
1072*4882a593Smuzhiyun  * disabling interrupts.
1073*4882a593Smuzhiyun  * Return: 0 if the reservation succeeded or -ENOMEM if it failed.
1074*4882a593Smuzhiyun  */
1075*4882a593Smuzhiyun static inline __must_check
xa_reserve_irq(struct xarray * xa,unsigned long index,gfp_t gfp)1076*4882a593Smuzhiyun int xa_reserve_irq(struct xarray *xa, unsigned long index, gfp_t gfp)
1077*4882a593Smuzhiyun {
1078*4882a593Smuzhiyun 	return xa_err(xa_cmpxchg_irq(xa, index, NULL, XA_ZERO_ENTRY, gfp));
1079*4882a593Smuzhiyun }
1080*4882a593Smuzhiyun 
1081*4882a593Smuzhiyun /**
1082*4882a593Smuzhiyun  * xa_release() - Release a reserved entry.
1083*4882a593Smuzhiyun  * @xa: XArray.
1084*4882a593Smuzhiyun  * @index: Index of entry.
1085*4882a593Smuzhiyun  *
1086*4882a593Smuzhiyun  * After calling xa_reserve(), you can call this function to release the
1087*4882a593Smuzhiyun  * reservation.  If the entry at @index has been stored to, this function
1088*4882a593Smuzhiyun  * will do nothing.
1089*4882a593Smuzhiyun  */
xa_release(struct xarray * xa,unsigned long index)1090*4882a593Smuzhiyun static inline void xa_release(struct xarray *xa, unsigned long index)
1091*4882a593Smuzhiyun {
1092*4882a593Smuzhiyun 	xa_cmpxchg(xa, index, XA_ZERO_ENTRY, NULL, 0);
1093*4882a593Smuzhiyun }
1094*4882a593Smuzhiyun 
1095*4882a593Smuzhiyun /* Everything below here is the Advanced API.  Proceed with caution. */
1096*4882a593Smuzhiyun 
1097*4882a593Smuzhiyun /*
1098*4882a593Smuzhiyun  * The xarray is constructed out of a set of 'chunks' of pointers.  Choosing
1099*4882a593Smuzhiyun  * the best chunk size requires some tradeoffs.  A power of two recommends
1100*4882a593Smuzhiyun  * itself so that we can walk the tree based purely on shifts and masks.
1101*4882a593Smuzhiyun  * Generally, the larger the better; as the number of slots per level of the
1102*4882a593Smuzhiyun  * tree increases, the less tall the tree needs to be.  But that needs to be
1103*4882a593Smuzhiyun  * balanced against the memory consumption of each node.  On a 64-bit system,
1104*4882a593Smuzhiyun  * xa_node is currently 576 bytes, and we get 7 of them per 4kB page.  If we
1105*4882a593Smuzhiyun  * doubled the number of slots per node, we'd get only 3 nodes per 4kB page.
1106*4882a593Smuzhiyun  */
1107*4882a593Smuzhiyun #ifndef XA_CHUNK_SHIFT
1108*4882a593Smuzhiyun #define XA_CHUNK_SHIFT		(CONFIG_BASE_SMALL ? 4 : 6)
1109*4882a593Smuzhiyun #endif
1110*4882a593Smuzhiyun #define XA_CHUNK_SIZE		(1UL << XA_CHUNK_SHIFT)
1111*4882a593Smuzhiyun #define XA_CHUNK_MASK		(XA_CHUNK_SIZE - 1)
1112*4882a593Smuzhiyun #define XA_MAX_MARKS		3
1113*4882a593Smuzhiyun #define XA_MARK_LONGS		DIV_ROUND_UP(XA_CHUNK_SIZE, BITS_PER_LONG)
1114*4882a593Smuzhiyun 
1115*4882a593Smuzhiyun /*
1116*4882a593Smuzhiyun  * @count is the count of every non-NULL element in the ->slots array
1117*4882a593Smuzhiyun  * whether that is a value entry, a retry entry, a user pointer,
1118*4882a593Smuzhiyun  * a sibling entry or a pointer to the next level of the tree.
1119*4882a593Smuzhiyun  * @nr_values is the count of every element in ->slots which is
1120*4882a593Smuzhiyun  * either a value entry or a sibling of a value entry.
1121*4882a593Smuzhiyun  */
1122*4882a593Smuzhiyun struct xa_node {
1123*4882a593Smuzhiyun 	unsigned char	shift;		/* Bits remaining in each slot */
1124*4882a593Smuzhiyun 	unsigned char	offset;		/* Slot offset in parent */
1125*4882a593Smuzhiyun 	unsigned char	count;		/* Total entry count */
1126*4882a593Smuzhiyun 	unsigned char	nr_values;	/* Value entry count */
1127*4882a593Smuzhiyun 	struct xa_node __rcu *parent;	/* NULL at top of tree */
1128*4882a593Smuzhiyun 	struct xarray	*array;		/* The array we belong to */
1129*4882a593Smuzhiyun 	union {
1130*4882a593Smuzhiyun 		struct list_head private_list;	/* For tree user */
1131*4882a593Smuzhiyun 		struct rcu_head	rcu_head;	/* Used when freeing node */
1132*4882a593Smuzhiyun 	};
1133*4882a593Smuzhiyun 	void __rcu	*slots[XA_CHUNK_SIZE];
1134*4882a593Smuzhiyun 	union {
1135*4882a593Smuzhiyun 		unsigned long	tags[XA_MAX_MARKS][XA_MARK_LONGS];
1136*4882a593Smuzhiyun 		unsigned long	marks[XA_MAX_MARKS][XA_MARK_LONGS];
1137*4882a593Smuzhiyun 	};
1138*4882a593Smuzhiyun };
1139*4882a593Smuzhiyun 
1140*4882a593Smuzhiyun void xa_dump(const struct xarray *);
1141*4882a593Smuzhiyun void xa_dump_node(const struct xa_node *);
1142*4882a593Smuzhiyun 
1143*4882a593Smuzhiyun #ifdef XA_DEBUG
1144*4882a593Smuzhiyun #define XA_BUG_ON(xa, x) do {					\
1145*4882a593Smuzhiyun 		if (x) {					\
1146*4882a593Smuzhiyun 			xa_dump(xa);				\
1147*4882a593Smuzhiyun 			BUG();					\
1148*4882a593Smuzhiyun 		}						\
1149*4882a593Smuzhiyun 	} while (0)
1150*4882a593Smuzhiyun #define XA_NODE_BUG_ON(node, x) do {				\
1151*4882a593Smuzhiyun 		if (x) {					\
1152*4882a593Smuzhiyun 			if (node) xa_dump_node(node);		\
1153*4882a593Smuzhiyun 			BUG();					\
1154*4882a593Smuzhiyun 		}						\
1155*4882a593Smuzhiyun 	} while (0)
1156*4882a593Smuzhiyun #else
1157*4882a593Smuzhiyun #define XA_BUG_ON(xa, x)	do { } while (0)
1158*4882a593Smuzhiyun #define XA_NODE_BUG_ON(node, x)	do { } while (0)
1159*4882a593Smuzhiyun #endif
1160*4882a593Smuzhiyun 
1161*4882a593Smuzhiyun /* Private */
xa_head(const struct xarray * xa)1162*4882a593Smuzhiyun static inline void *xa_head(const struct xarray *xa)
1163*4882a593Smuzhiyun {
1164*4882a593Smuzhiyun 	return rcu_dereference_check(xa->xa_head,
1165*4882a593Smuzhiyun 						lockdep_is_held(&xa->xa_lock));
1166*4882a593Smuzhiyun }
1167*4882a593Smuzhiyun 
1168*4882a593Smuzhiyun /* Private */
xa_head_locked(const struct xarray * xa)1169*4882a593Smuzhiyun static inline void *xa_head_locked(const struct xarray *xa)
1170*4882a593Smuzhiyun {
1171*4882a593Smuzhiyun 	return rcu_dereference_protected(xa->xa_head,
1172*4882a593Smuzhiyun 						lockdep_is_held(&xa->xa_lock));
1173*4882a593Smuzhiyun }
1174*4882a593Smuzhiyun 
1175*4882a593Smuzhiyun /* Private */
xa_entry(const struct xarray * xa,const struct xa_node * node,unsigned int offset)1176*4882a593Smuzhiyun static inline void *xa_entry(const struct xarray *xa,
1177*4882a593Smuzhiyun 				const struct xa_node *node, unsigned int offset)
1178*4882a593Smuzhiyun {
1179*4882a593Smuzhiyun 	XA_NODE_BUG_ON(node, offset >= XA_CHUNK_SIZE);
1180*4882a593Smuzhiyun 	return rcu_dereference_check(node->slots[offset],
1181*4882a593Smuzhiyun 						lockdep_is_held(&xa->xa_lock));
1182*4882a593Smuzhiyun }
1183*4882a593Smuzhiyun 
1184*4882a593Smuzhiyun /* Private */
xa_entry_locked(const struct xarray * xa,const struct xa_node * node,unsigned int offset)1185*4882a593Smuzhiyun static inline void *xa_entry_locked(const struct xarray *xa,
1186*4882a593Smuzhiyun 				const struct xa_node *node, unsigned int offset)
1187*4882a593Smuzhiyun {
1188*4882a593Smuzhiyun 	XA_NODE_BUG_ON(node, offset >= XA_CHUNK_SIZE);
1189*4882a593Smuzhiyun 	return rcu_dereference_protected(node->slots[offset],
1190*4882a593Smuzhiyun 						lockdep_is_held(&xa->xa_lock));
1191*4882a593Smuzhiyun }
1192*4882a593Smuzhiyun 
1193*4882a593Smuzhiyun /* Private */
xa_parent(const struct xarray * xa,const struct xa_node * node)1194*4882a593Smuzhiyun static inline struct xa_node *xa_parent(const struct xarray *xa,
1195*4882a593Smuzhiyun 					const struct xa_node *node)
1196*4882a593Smuzhiyun {
1197*4882a593Smuzhiyun 	return rcu_dereference_check(node->parent,
1198*4882a593Smuzhiyun 						lockdep_is_held(&xa->xa_lock));
1199*4882a593Smuzhiyun }
1200*4882a593Smuzhiyun 
1201*4882a593Smuzhiyun /* Private */
xa_parent_locked(const struct xarray * xa,const struct xa_node * node)1202*4882a593Smuzhiyun static inline struct xa_node *xa_parent_locked(const struct xarray *xa,
1203*4882a593Smuzhiyun 					const struct xa_node *node)
1204*4882a593Smuzhiyun {
1205*4882a593Smuzhiyun 	return rcu_dereference_protected(node->parent,
1206*4882a593Smuzhiyun 						lockdep_is_held(&xa->xa_lock));
1207*4882a593Smuzhiyun }
1208*4882a593Smuzhiyun 
1209*4882a593Smuzhiyun /* Private */
xa_mk_node(const struct xa_node * node)1210*4882a593Smuzhiyun static inline void *xa_mk_node(const struct xa_node *node)
1211*4882a593Smuzhiyun {
1212*4882a593Smuzhiyun 	return (void *)((unsigned long)node | 2);
1213*4882a593Smuzhiyun }
1214*4882a593Smuzhiyun 
1215*4882a593Smuzhiyun /* Private */
xa_to_node(const void * entry)1216*4882a593Smuzhiyun static inline struct xa_node *xa_to_node(const void *entry)
1217*4882a593Smuzhiyun {
1218*4882a593Smuzhiyun 	return (struct xa_node *)((unsigned long)entry - 2);
1219*4882a593Smuzhiyun }
1220*4882a593Smuzhiyun 
1221*4882a593Smuzhiyun /* Private */
xa_is_node(const void * entry)1222*4882a593Smuzhiyun static inline bool xa_is_node(const void *entry)
1223*4882a593Smuzhiyun {
1224*4882a593Smuzhiyun 	return xa_is_internal(entry) && (unsigned long)entry > 4096;
1225*4882a593Smuzhiyun }
1226*4882a593Smuzhiyun 
1227*4882a593Smuzhiyun /* Private */
xa_mk_sibling(unsigned int offset)1228*4882a593Smuzhiyun static inline void *xa_mk_sibling(unsigned int offset)
1229*4882a593Smuzhiyun {
1230*4882a593Smuzhiyun 	return xa_mk_internal(offset);
1231*4882a593Smuzhiyun }
1232*4882a593Smuzhiyun 
1233*4882a593Smuzhiyun /* Private */
xa_to_sibling(const void * entry)1234*4882a593Smuzhiyun static inline unsigned long xa_to_sibling(const void *entry)
1235*4882a593Smuzhiyun {
1236*4882a593Smuzhiyun 	return xa_to_internal(entry);
1237*4882a593Smuzhiyun }
1238*4882a593Smuzhiyun 
1239*4882a593Smuzhiyun /**
1240*4882a593Smuzhiyun  * xa_is_sibling() - Is the entry a sibling entry?
1241*4882a593Smuzhiyun  * @entry: Entry retrieved from the XArray
1242*4882a593Smuzhiyun  *
1243*4882a593Smuzhiyun  * Return: %true if the entry is a sibling entry.
1244*4882a593Smuzhiyun  */
xa_is_sibling(const void * entry)1245*4882a593Smuzhiyun static inline bool xa_is_sibling(const void *entry)
1246*4882a593Smuzhiyun {
1247*4882a593Smuzhiyun 	return IS_ENABLED(CONFIG_XARRAY_MULTI) && xa_is_internal(entry) &&
1248*4882a593Smuzhiyun 		(entry < xa_mk_sibling(XA_CHUNK_SIZE - 1));
1249*4882a593Smuzhiyun }
1250*4882a593Smuzhiyun 
1251*4882a593Smuzhiyun #define XA_RETRY_ENTRY		xa_mk_internal(256)
1252*4882a593Smuzhiyun 
1253*4882a593Smuzhiyun /**
1254*4882a593Smuzhiyun  * xa_is_retry() - Is the entry a retry entry?
1255*4882a593Smuzhiyun  * @entry: Entry retrieved from the XArray
1256*4882a593Smuzhiyun  *
1257*4882a593Smuzhiyun  * Return: %true if the entry is a retry entry.
1258*4882a593Smuzhiyun  */
xa_is_retry(const void * entry)1259*4882a593Smuzhiyun static inline bool xa_is_retry(const void *entry)
1260*4882a593Smuzhiyun {
1261*4882a593Smuzhiyun 	return unlikely(entry == XA_RETRY_ENTRY);
1262*4882a593Smuzhiyun }
1263*4882a593Smuzhiyun 
1264*4882a593Smuzhiyun /**
1265*4882a593Smuzhiyun  * xa_is_advanced() - Is the entry only permitted for the advanced API?
1266*4882a593Smuzhiyun  * @entry: Entry to be stored in the XArray.
1267*4882a593Smuzhiyun  *
1268*4882a593Smuzhiyun  * Return: %true if the entry cannot be stored by the normal API.
1269*4882a593Smuzhiyun  */
xa_is_advanced(const void * entry)1270*4882a593Smuzhiyun static inline bool xa_is_advanced(const void *entry)
1271*4882a593Smuzhiyun {
1272*4882a593Smuzhiyun 	return xa_is_internal(entry) && (entry <= XA_RETRY_ENTRY);
1273*4882a593Smuzhiyun }
1274*4882a593Smuzhiyun 
1275*4882a593Smuzhiyun /**
1276*4882a593Smuzhiyun  * typedef xa_update_node_t - A callback function from the XArray.
1277*4882a593Smuzhiyun  * @node: The node which is being processed
1278*4882a593Smuzhiyun  *
1279*4882a593Smuzhiyun  * This function is called every time the XArray updates the count of
1280*4882a593Smuzhiyun  * present and value entries in a node.  It allows advanced users to
1281*4882a593Smuzhiyun  * maintain the private_list in the node.
1282*4882a593Smuzhiyun  *
1283*4882a593Smuzhiyun  * Context: The xa_lock is held and interrupts may be disabled.
1284*4882a593Smuzhiyun  *	    Implementations should not drop the xa_lock, nor re-enable
1285*4882a593Smuzhiyun  *	    interrupts.
1286*4882a593Smuzhiyun  */
1287*4882a593Smuzhiyun typedef void (*xa_update_node_t)(struct xa_node *node);
1288*4882a593Smuzhiyun 
1289*4882a593Smuzhiyun void xa_delete_node(struct xa_node *, xa_update_node_t);
1290*4882a593Smuzhiyun 
1291*4882a593Smuzhiyun /*
1292*4882a593Smuzhiyun  * The xa_state is opaque to its users.  It contains various different pieces
1293*4882a593Smuzhiyun  * of state involved in the current operation on the XArray.  It should be
1294*4882a593Smuzhiyun  * declared on the stack and passed between the various internal routines.
1295*4882a593Smuzhiyun  * The various elements in it should not be accessed directly, but only
1296*4882a593Smuzhiyun  * through the provided accessor functions.  The below documentation is for
1297*4882a593Smuzhiyun  * the benefit of those working on the code, not for users of the XArray.
1298*4882a593Smuzhiyun  *
1299*4882a593Smuzhiyun  * @xa_node usually points to the xa_node containing the slot we're operating
1300*4882a593Smuzhiyun  * on (and @xa_offset is the offset in the slots array).  If there is a
1301*4882a593Smuzhiyun  * single entry in the array at index 0, there are no allocated xa_nodes to
1302*4882a593Smuzhiyun  * point to, and so we store %NULL in @xa_node.  @xa_node is set to
1303*4882a593Smuzhiyun  * the value %XAS_RESTART if the xa_state is not walked to the correct
1304*4882a593Smuzhiyun  * position in the tree of nodes for this operation.  If an error occurs
1305*4882a593Smuzhiyun  * during an operation, it is set to an %XAS_ERROR value.  If we run off the
1306*4882a593Smuzhiyun  * end of the allocated nodes, it is set to %XAS_BOUNDS.
1307*4882a593Smuzhiyun  */
1308*4882a593Smuzhiyun struct xa_state {
1309*4882a593Smuzhiyun 	struct xarray *xa;
1310*4882a593Smuzhiyun 	unsigned long xa_index;
1311*4882a593Smuzhiyun 	unsigned char xa_shift;
1312*4882a593Smuzhiyun 	unsigned char xa_sibs;
1313*4882a593Smuzhiyun 	unsigned char xa_offset;
1314*4882a593Smuzhiyun 	unsigned char xa_pad;		/* Helps gcc generate better code */
1315*4882a593Smuzhiyun 	struct xa_node *xa_node;
1316*4882a593Smuzhiyun 	struct xa_node *xa_alloc;
1317*4882a593Smuzhiyun 	xa_update_node_t xa_update;
1318*4882a593Smuzhiyun };
1319*4882a593Smuzhiyun 
1320*4882a593Smuzhiyun /*
1321*4882a593Smuzhiyun  * We encode errnos in the xas->xa_node.  If an error has happened, we need to
1322*4882a593Smuzhiyun  * drop the lock to fix it, and once we've done so the xa_state is invalid.
1323*4882a593Smuzhiyun  */
1324*4882a593Smuzhiyun #define XA_ERROR(errno) ((struct xa_node *)(((unsigned long)errno << 2) | 2UL))
1325*4882a593Smuzhiyun #define XAS_BOUNDS	((struct xa_node *)1UL)
1326*4882a593Smuzhiyun #define XAS_RESTART	((struct xa_node *)3UL)
1327*4882a593Smuzhiyun 
1328*4882a593Smuzhiyun #define __XA_STATE(array, index, shift, sibs)  {	\
1329*4882a593Smuzhiyun 	.xa = array,					\
1330*4882a593Smuzhiyun 	.xa_index = index,				\
1331*4882a593Smuzhiyun 	.xa_shift = shift,				\
1332*4882a593Smuzhiyun 	.xa_sibs = sibs,				\
1333*4882a593Smuzhiyun 	.xa_offset = 0,					\
1334*4882a593Smuzhiyun 	.xa_pad = 0,					\
1335*4882a593Smuzhiyun 	.xa_node = XAS_RESTART,				\
1336*4882a593Smuzhiyun 	.xa_alloc = NULL,				\
1337*4882a593Smuzhiyun 	.xa_update = NULL				\
1338*4882a593Smuzhiyun }
1339*4882a593Smuzhiyun 
1340*4882a593Smuzhiyun /**
1341*4882a593Smuzhiyun  * XA_STATE() - Declare an XArray operation state.
1342*4882a593Smuzhiyun  * @name: Name of this operation state (usually xas).
1343*4882a593Smuzhiyun  * @array: Array to operate on.
1344*4882a593Smuzhiyun  * @index: Initial index of interest.
1345*4882a593Smuzhiyun  *
1346*4882a593Smuzhiyun  * Declare and initialise an xa_state on the stack.
1347*4882a593Smuzhiyun  */
1348*4882a593Smuzhiyun #define XA_STATE(name, array, index)				\
1349*4882a593Smuzhiyun 	struct xa_state name = __XA_STATE(array, index, 0, 0)
1350*4882a593Smuzhiyun 
1351*4882a593Smuzhiyun /**
1352*4882a593Smuzhiyun  * XA_STATE_ORDER() - Declare an XArray operation state.
1353*4882a593Smuzhiyun  * @name: Name of this operation state (usually xas).
1354*4882a593Smuzhiyun  * @array: Array to operate on.
1355*4882a593Smuzhiyun  * @index: Initial index of interest.
1356*4882a593Smuzhiyun  * @order: Order of entry.
1357*4882a593Smuzhiyun  *
1358*4882a593Smuzhiyun  * Declare and initialise an xa_state on the stack.  This variant of
1359*4882a593Smuzhiyun  * XA_STATE() allows you to specify the 'order' of the element you
1360*4882a593Smuzhiyun  * want to operate on.`
1361*4882a593Smuzhiyun  */
1362*4882a593Smuzhiyun #define XA_STATE_ORDER(name, array, index, order)		\
1363*4882a593Smuzhiyun 	struct xa_state name = __XA_STATE(array,		\
1364*4882a593Smuzhiyun 			(index >> order) << order,		\
1365*4882a593Smuzhiyun 			order - (order % XA_CHUNK_SHIFT),	\
1366*4882a593Smuzhiyun 			(1U << (order % XA_CHUNK_SHIFT)) - 1)
1367*4882a593Smuzhiyun 
1368*4882a593Smuzhiyun #define xas_marked(xas, mark)	xa_marked((xas)->xa, (mark))
1369*4882a593Smuzhiyun #define xas_trylock(xas)	xa_trylock((xas)->xa)
1370*4882a593Smuzhiyun #define xas_lock(xas)		xa_lock((xas)->xa)
1371*4882a593Smuzhiyun #define xas_unlock(xas)		xa_unlock((xas)->xa)
1372*4882a593Smuzhiyun #define xas_lock_bh(xas)	xa_lock_bh((xas)->xa)
1373*4882a593Smuzhiyun #define xas_unlock_bh(xas)	xa_unlock_bh((xas)->xa)
1374*4882a593Smuzhiyun #define xas_lock_irq(xas)	xa_lock_irq((xas)->xa)
1375*4882a593Smuzhiyun #define xas_unlock_irq(xas)	xa_unlock_irq((xas)->xa)
1376*4882a593Smuzhiyun #define xas_lock_irqsave(xas, flags) \
1377*4882a593Smuzhiyun 				xa_lock_irqsave((xas)->xa, flags)
1378*4882a593Smuzhiyun #define xas_unlock_irqrestore(xas, flags) \
1379*4882a593Smuzhiyun 				xa_unlock_irqrestore((xas)->xa, flags)
1380*4882a593Smuzhiyun 
1381*4882a593Smuzhiyun /**
1382*4882a593Smuzhiyun  * xas_error() - Return an errno stored in the xa_state.
1383*4882a593Smuzhiyun  * @xas: XArray operation state.
1384*4882a593Smuzhiyun  *
1385*4882a593Smuzhiyun  * Return: 0 if no error has been noted.  A negative errno if one has.
1386*4882a593Smuzhiyun  */
xas_error(const struct xa_state * xas)1387*4882a593Smuzhiyun static inline int xas_error(const struct xa_state *xas)
1388*4882a593Smuzhiyun {
1389*4882a593Smuzhiyun 	return xa_err(xas->xa_node);
1390*4882a593Smuzhiyun }
1391*4882a593Smuzhiyun 
1392*4882a593Smuzhiyun /**
1393*4882a593Smuzhiyun  * xas_set_err() - Note an error in the xa_state.
1394*4882a593Smuzhiyun  * @xas: XArray operation state.
1395*4882a593Smuzhiyun  * @err: Negative error number.
1396*4882a593Smuzhiyun  *
1397*4882a593Smuzhiyun  * Only call this function with a negative @err; zero or positive errors
1398*4882a593Smuzhiyun  * will probably not behave the way you think they should.  If you want
1399*4882a593Smuzhiyun  * to clear the error from an xa_state, use xas_reset().
1400*4882a593Smuzhiyun  */
xas_set_err(struct xa_state * xas,long err)1401*4882a593Smuzhiyun static inline void xas_set_err(struct xa_state *xas, long err)
1402*4882a593Smuzhiyun {
1403*4882a593Smuzhiyun 	xas->xa_node = XA_ERROR(err);
1404*4882a593Smuzhiyun }
1405*4882a593Smuzhiyun 
1406*4882a593Smuzhiyun /**
1407*4882a593Smuzhiyun  * xas_invalid() - Is the xas in a retry or error state?
1408*4882a593Smuzhiyun  * @xas: XArray operation state.
1409*4882a593Smuzhiyun  *
1410*4882a593Smuzhiyun  * Return: %true if the xas cannot be used for operations.
1411*4882a593Smuzhiyun  */
xas_invalid(const struct xa_state * xas)1412*4882a593Smuzhiyun static inline bool xas_invalid(const struct xa_state *xas)
1413*4882a593Smuzhiyun {
1414*4882a593Smuzhiyun 	return (unsigned long)xas->xa_node & 3;
1415*4882a593Smuzhiyun }
1416*4882a593Smuzhiyun 
1417*4882a593Smuzhiyun /**
1418*4882a593Smuzhiyun  * xas_valid() - Is the xas a valid cursor into the array?
1419*4882a593Smuzhiyun  * @xas: XArray operation state.
1420*4882a593Smuzhiyun  *
1421*4882a593Smuzhiyun  * Return: %true if the xas can be used for operations.
1422*4882a593Smuzhiyun  */
xas_valid(const struct xa_state * xas)1423*4882a593Smuzhiyun static inline bool xas_valid(const struct xa_state *xas)
1424*4882a593Smuzhiyun {
1425*4882a593Smuzhiyun 	return !xas_invalid(xas);
1426*4882a593Smuzhiyun }
1427*4882a593Smuzhiyun 
1428*4882a593Smuzhiyun /**
1429*4882a593Smuzhiyun  * xas_is_node() - Does the xas point to a node?
1430*4882a593Smuzhiyun  * @xas: XArray operation state.
1431*4882a593Smuzhiyun  *
1432*4882a593Smuzhiyun  * Return: %true if the xas currently references a node.
1433*4882a593Smuzhiyun  */
xas_is_node(const struct xa_state * xas)1434*4882a593Smuzhiyun static inline bool xas_is_node(const struct xa_state *xas)
1435*4882a593Smuzhiyun {
1436*4882a593Smuzhiyun 	return xas_valid(xas) && xas->xa_node;
1437*4882a593Smuzhiyun }
1438*4882a593Smuzhiyun 
1439*4882a593Smuzhiyun /* True if the pointer is something other than a node */
xas_not_node(struct xa_node * node)1440*4882a593Smuzhiyun static inline bool xas_not_node(struct xa_node *node)
1441*4882a593Smuzhiyun {
1442*4882a593Smuzhiyun 	return ((unsigned long)node & 3) || !node;
1443*4882a593Smuzhiyun }
1444*4882a593Smuzhiyun 
1445*4882a593Smuzhiyun /* True if the node represents RESTART or an error */
xas_frozen(struct xa_node * node)1446*4882a593Smuzhiyun static inline bool xas_frozen(struct xa_node *node)
1447*4882a593Smuzhiyun {
1448*4882a593Smuzhiyun 	return (unsigned long)node & 2;
1449*4882a593Smuzhiyun }
1450*4882a593Smuzhiyun 
1451*4882a593Smuzhiyun /* True if the node represents head-of-tree, RESTART or BOUNDS */
xas_top(struct xa_node * node)1452*4882a593Smuzhiyun static inline bool xas_top(struct xa_node *node)
1453*4882a593Smuzhiyun {
1454*4882a593Smuzhiyun 	return node <= XAS_RESTART;
1455*4882a593Smuzhiyun }
1456*4882a593Smuzhiyun 
1457*4882a593Smuzhiyun /**
1458*4882a593Smuzhiyun  * xas_reset() - Reset an XArray operation state.
1459*4882a593Smuzhiyun  * @xas: XArray operation state.
1460*4882a593Smuzhiyun  *
1461*4882a593Smuzhiyun  * Resets the error or walk state of the @xas so future walks of the
1462*4882a593Smuzhiyun  * array will start from the root.  Use this if you have dropped the
1463*4882a593Smuzhiyun  * xarray lock and want to reuse the xa_state.
1464*4882a593Smuzhiyun  *
1465*4882a593Smuzhiyun  * Context: Any context.
1466*4882a593Smuzhiyun  */
xas_reset(struct xa_state * xas)1467*4882a593Smuzhiyun static inline void xas_reset(struct xa_state *xas)
1468*4882a593Smuzhiyun {
1469*4882a593Smuzhiyun 	xas->xa_node = XAS_RESTART;
1470*4882a593Smuzhiyun }
1471*4882a593Smuzhiyun 
1472*4882a593Smuzhiyun /**
1473*4882a593Smuzhiyun  * xas_retry() - Retry the operation if appropriate.
1474*4882a593Smuzhiyun  * @xas: XArray operation state.
1475*4882a593Smuzhiyun  * @entry: Entry from xarray.
1476*4882a593Smuzhiyun  *
1477*4882a593Smuzhiyun  * The advanced functions may sometimes return an internal entry, such as
1478*4882a593Smuzhiyun  * a retry entry or a zero entry.  This function sets up the @xas to restart
1479*4882a593Smuzhiyun  * the walk from the head of the array if needed.
1480*4882a593Smuzhiyun  *
1481*4882a593Smuzhiyun  * Context: Any context.
1482*4882a593Smuzhiyun  * Return: true if the operation needs to be retried.
1483*4882a593Smuzhiyun  */
xas_retry(struct xa_state * xas,const void * entry)1484*4882a593Smuzhiyun static inline bool xas_retry(struct xa_state *xas, const void *entry)
1485*4882a593Smuzhiyun {
1486*4882a593Smuzhiyun 	if (xa_is_zero(entry))
1487*4882a593Smuzhiyun 		return true;
1488*4882a593Smuzhiyun 	if (!xa_is_retry(entry))
1489*4882a593Smuzhiyun 		return false;
1490*4882a593Smuzhiyun 	xas_reset(xas);
1491*4882a593Smuzhiyun 	return true;
1492*4882a593Smuzhiyun }
1493*4882a593Smuzhiyun 
1494*4882a593Smuzhiyun void *xas_load(struct xa_state *);
1495*4882a593Smuzhiyun void *xas_store(struct xa_state *, void *entry);
1496*4882a593Smuzhiyun void *xas_find(struct xa_state *, unsigned long max);
1497*4882a593Smuzhiyun void *xas_find_conflict(struct xa_state *);
1498*4882a593Smuzhiyun 
1499*4882a593Smuzhiyun bool xas_get_mark(const struct xa_state *, xa_mark_t);
1500*4882a593Smuzhiyun void xas_set_mark(const struct xa_state *, xa_mark_t);
1501*4882a593Smuzhiyun void xas_clear_mark(const struct xa_state *, xa_mark_t);
1502*4882a593Smuzhiyun void *xas_find_marked(struct xa_state *, unsigned long max, xa_mark_t);
1503*4882a593Smuzhiyun void xas_init_marks(const struct xa_state *);
1504*4882a593Smuzhiyun 
1505*4882a593Smuzhiyun bool xas_nomem(struct xa_state *, gfp_t);
1506*4882a593Smuzhiyun void xas_pause(struct xa_state *);
1507*4882a593Smuzhiyun 
1508*4882a593Smuzhiyun void xas_create_range(struct xa_state *);
1509*4882a593Smuzhiyun 
1510*4882a593Smuzhiyun #ifdef CONFIG_XARRAY_MULTI
1511*4882a593Smuzhiyun int xa_get_order(struct xarray *, unsigned long index);
1512*4882a593Smuzhiyun void xas_split(struct xa_state *, void *entry, unsigned int order);
1513*4882a593Smuzhiyun void xas_split_alloc(struct xa_state *, void *entry, unsigned int order, gfp_t);
1514*4882a593Smuzhiyun #else
xa_get_order(struct xarray * xa,unsigned long index)1515*4882a593Smuzhiyun static inline int xa_get_order(struct xarray *xa, unsigned long index)
1516*4882a593Smuzhiyun {
1517*4882a593Smuzhiyun 	return 0;
1518*4882a593Smuzhiyun }
1519*4882a593Smuzhiyun 
xas_split(struct xa_state * xas,void * entry,unsigned int order)1520*4882a593Smuzhiyun static inline void xas_split(struct xa_state *xas, void *entry,
1521*4882a593Smuzhiyun 		unsigned int order)
1522*4882a593Smuzhiyun {
1523*4882a593Smuzhiyun 	xas_store(xas, entry);
1524*4882a593Smuzhiyun }
1525*4882a593Smuzhiyun 
xas_split_alloc(struct xa_state * xas,void * entry,unsigned int order,gfp_t gfp)1526*4882a593Smuzhiyun static inline void xas_split_alloc(struct xa_state *xas, void *entry,
1527*4882a593Smuzhiyun 		unsigned int order, gfp_t gfp)
1528*4882a593Smuzhiyun {
1529*4882a593Smuzhiyun }
1530*4882a593Smuzhiyun #endif
1531*4882a593Smuzhiyun 
1532*4882a593Smuzhiyun /**
1533*4882a593Smuzhiyun  * xas_reload() - Refetch an entry from the xarray.
1534*4882a593Smuzhiyun  * @xas: XArray operation state.
1535*4882a593Smuzhiyun  *
1536*4882a593Smuzhiyun  * Use this function to check that a previously loaded entry still has
1537*4882a593Smuzhiyun  * the same value.  This is useful for the lockless pagecache lookup where
1538*4882a593Smuzhiyun  * we walk the array with only the RCU lock to protect us, lock the page,
1539*4882a593Smuzhiyun  * then check that the page hasn't moved since we looked it up.
1540*4882a593Smuzhiyun  *
1541*4882a593Smuzhiyun  * The caller guarantees that @xas is still valid.  If it may be in an
1542*4882a593Smuzhiyun  * error or restart state, call xas_load() instead.
1543*4882a593Smuzhiyun  *
1544*4882a593Smuzhiyun  * Return: The entry at this location in the xarray.
1545*4882a593Smuzhiyun  */
xas_reload(struct xa_state * xas)1546*4882a593Smuzhiyun static inline void *xas_reload(struct xa_state *xas)
1547*4882a593Smuzhiyun {
1548*4882a593Smuzhiyun 	struct xa_node *node = xas->xa_node;
1549*4882a593Smuzhiyun 	void *entry;
1550*4882a593Smuzhiyun 	char offset;
1551*4882a593Smuzhiyun 
1552*4882a593Smuzhiyun 	if (!node)
1553*4882a593Smuzhiyun 		return xa_head(xas->xa);
1554*4882a593Smuzhiyun 	if (IS_ENABLED(CONFIG_XARRAY_MULTI)) {
1555*4882a593Smuzhiyun 		offset = (xas->xa_index >> node->shift) & XA_CHUNK_MASK;
1556*4882a593Smuzhiyun 		entry = xa_entry(xas->xa, node, offset);
1557*4882a593Smuzhiyun 		if (!xa_is_sibling(entry))
1558*4882a593Smuzhiyun 			return entry;
1559*4882a593Smuzhiyun 		offset = xa_to_sibling(entry);
1560*4882a593Smuzhiyun 	} else {
1561*4882a593Smuzhiyun 		offset = xas->xa_offset;
1562*4882a593Smuzhiyun 	}
1563*4882a593Smuzhiyun 	return xa_entry(xas->xa, node, offset);
1564*4882a593Smuzhiyun }
1565*4882a593Smuzhiyun 
1566*4882a593Smuzhiyun /**
1567*4882a593Smuzhiyun  * xas_set() - Set up XArray operation state for a different index.
1568*4882a593Smuzhiyun  * @xas: XArray operation state.
1569*4882a593Smuzhiyun  * @index: New index into the XArray.
1570*4882a593Smuzhiyun  *
1571*4882a593Smuzhiyun  * Move the operation state to refer to a different index.  This will
1572*4882a593Smuzhiyun  * have the effect of starting a walk from the top; see xas_next()
1573*4882a593Smuzhiyun  * to move to an adjacent index.
1574*4882a593Smuzhiyun  */
xas_set(struct xa_state * xas,unsigned long index)1575*4882a593Smuzhiyun static inline void xas_set(struct xa_state *xas, unsigned long index)
1576*4882a593Smuzhiyun {
1577*4882a593Smuzhiyun 	xas->xa_index = index;
1578*4882a593Smuzhiyun 	xas->xa_node = XAS_RESTART;
1579*4882a593Smuzhiyun }
1580*4882a593Smuzhiyun 
1581*4882a593Smuzhiyun /**
1582*4882a593Smuzhiyun  * xas_set_order() - Set up XArray operation state for a multislot entry.
1583*4882a593Smuzhiyun  * @xas: XArray operation state.
1584*4882a593Smuzhiyun  * @index: Target of the operation.
1585*4882a593Smuzhiyun  * @order: Entry occupies 2^@order indices.
1586*4882a593Smuzhiyun  */
xas_set_order(struct xa_state * xas,unsigned long index,unsigned int order)1587*4882a593Smuzhiyun static inline void xas_set_order(struct xa_state *xas, unsigned long index,
1588*4882a593Smuzhiyun 					unsigned int order)
1589*4882a593Smuzhiyun {
1590*4882a593Smuzhiyun #ifdef CONFIG_XARRAY_MULTI
1591*4882a593Smuzhiyun 	xas->xa_index = order < BITS_PER_LONG ? (index >> order) << order : 0;
1592*4882a593Smuzhiyun 	xas->xa_shift = order - (order % XA_CHUNK_SHIFT);
1593*4882a593Smuzhiyun 	xas->xa_sibs = (1 << (order % XA_CHUNK_SHIFT)) - 1;
1594*4882a593Smuzhiyun 	xas->xa_node = XAS_RESTART;
1595*4882a593Smuzhiyun #else
1596*4882a593Smuzhiyun 	BUG_ON(order > 0);
1597*4882a593Smuzhiyun 	xas_set(xas, index);
1598*4882a593Smuzhiyun #endif
1599*4882a593Smuzhiyun }
1600*4882a593Smuzhiyun 
1601*4882a593Smuzhiyun /**
1602*4882a593Smuzhiyun  * xas_set_update() - Set up XArray operation state for a callback.
1603*4882a593Smuzhiyun  * @xas: XArray operation state.
1604*4882a593Smuzhiyun  * @update: Function to call when updating a node.
1605*4882a593Smuzhiyun  *
1606*4882a593Smuzhiyun  * The XArray can notify a caller after it has updated an xa_node.
1607*4882a593Smuzhiyun  * This is advanced functionality and is only needed by the page cache.
1608*4882a593Smuzhiyun  */
xas_set_update(struct xa_state * xas,xa_update_node_t update)1609*4882a593Smuzhiyun static inline void xas_set_update(struct xa_state *xas, xa_update_node_t update)
1610*4882a593Smuzhiyun {
1611*4882a593Smuzhiyun 	xas->xa_update = update;
1612*4882a593Smuzhiyun }
1613*4882a593Smuzhiyun 
1614*4882a593Smuzhiyun /**
1615*4882a593Smuzhiyun  * xas_next_entry() - Advance iterator to next present entry.
1616*4882a593Smuzhiyun  * @xas: XArray operation state.
1617*4882a593Smuzhiyun  * @max: Highest index to return.
1618*4882a593Smuzhiyun  *
1619*4882a593Smuzhiyun  * xas_next_entry() is an inline function to optimise xarray traversal for
1620*4882a593Smuzhiyun  * speed.  It is equivalent to calling xas_find(), and will call xas_find()
1621*4882a593Smuzhiyun  * for all the hard cases.
1622*4882a593Smuzhiyun  *
1623*4882a593Smuzhiyun  * Return: The next present entry after the one currently referred to by @xas.
1624*4882a593Smuzhiyun  */
xas_next_entry(struct xa_state * xas,unsigned long max)1625*4882a593Smuzhiyun static inline void *xas_next_entry(struct xa_state *xas, unsigned long max)
1626*4882a593Smuzhiyun {
1627*4882a593Smuzhiyun 	struct xa_node *node = xas->xa_node;
1628*4882a593Smuzhiyun 	void *entry;
1629*4882a593Smuzhiyun 
1630*4882a593Smuzhiyun 	if (unlikely(xas_not_node(node) || node->shift ||
1631*4882a593Smuzhiyun 			xas->xa_offset != (xas->xa_index & XA_CHUNK_MASK)))
1632*4882a593Smuzhiyun 		return xas_find(xas, max);
1633*4882a593Smuzhiyun 
1634*4882a593Smuzhiyun 	do {
1635*4882a593Smuzhiyun 		if (unlikely(xas->xa_index >= max))
1636*4882a593Smuzhiyun 			return xas_find(xas, max);
1637*4882a593Smuzhiyun 		if (unlikely(xas->xa_offset == XA_CHUNK_MASK))
1638*4882a593Smuzhiyun 			return xas_find(xas, max);
1639*4882a593Smuzhiyun 		entry = xa_entry(xas->xa, node, xas->xa_offset + 1);
1640*4882a593Smuzhiyun 		if (unlikely(xa_is_internal(entry)))
1641*4882a593Smuzhiyun 			return xas_find(xas, max);
1642*4882a593Smuzhiyun 		xas->xa_offset++;
1643*4882a593Smuzhiyun 		xas->xa_index++;
1644*4882a593Smuzhiyun 	} while (!entry);
1645*4882a593Smuzhiyun 
1646*4882a593Smuzhiyun 	return entry;
1647*4882a593Smuzhiyun }
1648*4882a593Smuzhiyun 
1649*4882a593Smuzhiyun /* Private */
xas_find_chunk(struct xa_state * xas,bool advance,xa_mark_t mark)1650*4882a593Smuzhiyun static inline unsigned int xas_find_chunk(struct xa_state *xas, bool advance,
1651*4882a593Smuzhiyun 		xa_mark_t mark)
1652*4882a593Smuzhiyun {
1653*4882a593Smuzhiyun 	unsigned long *addr = xas->xa_node->marks[(__force unsigned)mark];
1654*4882a593Smuzhiyun 	unsigned int offset = xas->xa_offset;
1655*4882a593Smuzhiyun 
1656*4882a593Smuzhiyun 	if (advance)
1657*4882a593Smuzhiyun 		offset++;
1658*4882a593Smuzhiyun 	if (XA_CHUNK_SIZE == BITS_PER_LONG) {
1659*4882a593Smuzhiyun 		if (offset < XA_CHUNK_SIZE) {
1660*4882a593Smuzhiyun 			unsigned long data = *addr & (~0UL << offset);
1661*4882a593Smuzhiyun 			if (data)
1662*4882a593Smuzhiyun 				return __ffs(data);
1663*4882a593Smuzhiyun 		}
1664*4882a593Smuzhiyun 		return XA_CHUNK_SIZE;
1665*4882a593Smuzhiyun 	}
1666*4882a593Smuzhiyun 
1667*4882a593Smuzhiyun 	return find_next_bit(addr, XA_CHUNK_SIZE, offset);
1668*4882a593Smuzhiyun }
1669*4882a593Smuzhiyun 
1670*4882a593Smuzhiyun /**
1671*4882a593Smuzhiyun  * xas_next_marked() - Advance iterator to next marked entry.
1672*4882a593Smuzhiyun  * @xas: XArray operation state.
1673*4882a593Smuzhiyun  * @max: Highest index to return.
1674*4882a593Smuzhiyun  * @mark: Mark to search for.
1675*4882a593Smuzhiyun  *
1676*4882a593Smuzhiyun  * xas_next_marked() is an inline function to optimise xarray traversal for
1677*4882a593Smuzhiyun  * speed.  It is equivalent to calling xas_find_marked(), and will call
1678*4882a593Smuzhiyun  * xas_find_marked() for all the hard cases.
1679*4882a593Smuzhiyun  *
1680*4882a593Smuzhiyun  * Return: The next marked entry after the one currently referred to by @xas.
1681*4882a593Smuzhiyun  */
xas_next_marked(struct xa_state * xas,unsigned long max,xa_mark_t mark)1682*4882a593Smuzhiyun static inline void *xas_next_marked(struct xa_state *xas, unsigned long max,
1683*4882a593Smuzhiyun 								xa_mark_t mark)
1684*4882a593Smuzhiyun {
1685*4882a593Smuzhiyun 	struct xa_node *node = xas->xa_node;
1686*4882a593Smuzhiyun 	void *entry;
1687*4882a593Smuzhiyun 	unsigned int offset;
1688*4882a593Smuzhiyun 
1689*4882a593Smuzhiyun 	if (unlikely(xas_not_node(node) || node->shift))
1690*4882a593Smuzhiyun 		return xas_find_marked(xas, max, mark);
1691*4882a593Smuzhiyun 	offset = xas_find_chunk(xas, true, mark);
1692*4882a593Smuzhiyun 	xas->xa_offset = offset;
1693*4882a593Smuzhiyun 	xas->xa_index = (xas->xa_index & ~XA_CHUNK_MASK) + offset;
1694*4882a593Smuzhiyun 	if (xas->xa_index > max)
1695*4882a593Smuzhiyun 		return NULL;
1696*4882a593Smuzhiyun 	if (offset == XA_CHUNK_SIZE)
1697*4882a593Smuzhiyun 		return xas_find_marked(xas, max, mark);
1698*4882a593Smuzhiyun 	entry = xa_entry(xas->xa, node, offset);
1699*4882a593Smuzhiyun 	if (!entry)
1700*4882a593Smuzhiyun 		return xas_find_marked(xas, max, mark);
1701*4882a593Smuzhiyun 	return entry;
1702*4882a593Smuzhiyun }
1703*4882a593Smuzhiyun 
1704*4882a593Smuzhiyun /*
1705*4882a593Smuzhiyun  * If iterating while holding a lock, drop the lock and reschedule
1706*4882a593Smuzhiyun  * every %XA_CHECK_SCHED loops.
1707*4882a593Smuzhiyun  */
1708*4882a593Smuzhiyun enum {
1709*4882a593Smuzhiyun 	XA_CHECK_SCHED = 4096,
1710*4882a593Smuzhiyun };
1711*4882a593Smuzhiyun 
1712*4882a593Smuzhiyun /**
1713*4882a593Smuzhiyun  * xas_for_each() - Iterate over a range of an XArray.
1714*4882a593Smuzhiyun  * @xas: XArray operation state.
1715*4882a593Smuzhiyun  * @entry: Entry retrieved from the array.
1716*4882a593Smuzhiyun  * @max: Maximum index to retrieve from array.
1717*4882a593Smuzhiyun  *
1718*4882a593Smuzhiyun  * The loop body will be executed for each entry present in the xarray
1719*4882a593Smuzhiyun  * between the current xas position and @max.  @entry will be set to
1720*4882a593Smuzhiyun  * the entry retrieved from the xarray.  It is safe to delete entries
1721*4882a593Smuzhiyun  * from the array in the loop body.  You should hold either the RCU lock
1722*4882a593Smuzhiyun  * or the xa_lock while iterating.  If you need to drop the lock, call
1723*4882a593Smuzhiyun  * xas_pause() first.
1724*4882a593Smuzhiyun  */
1725*4882a593Smuzhiyun #define xas_for_each(xas, entry, max) \
1726*4882a593Smuzhiyun 	for (entry = xas_find(xas, max); entry; \
1727*4882a593Smuzhiyun 	     entry = xas_next_entry(xas, max))
1728*4882a593Smuzhiyun 
1729*4882a593Smuzhiyun /**
1730*4882a593Smuzhiyun  * xas_for_each_marked() - Iterate over a range of an XArray.
1731*4882a593Smuzhiyun  * @xas: XArray operation state.
1732*4882a593Smuzhiyun  * @entry: Entry retrieved from the array.
1733*4882a593Smuzhiyun  * @max: Maximum index to retrieve from array.
1734*4882a593Smuzhiyun  * @mark: Mark to search for.
1735*4882a593Smuzhiyun  *
1736*4882a593Smuzhiyun  * The loop body will be executed for each marked entry in the xarray
1737*4882a593Smuzhiyun  * between the current xas position and @max.  @entry will be set to
1738*4882a593Smuzhiyun  * the entry retrieved from the xarray.  It is safe to delete entries
1739*4882a593Smuzhiyun  * from the array in the loop body.  You should hold either the RCU lock
1740*4882a593Smuzhiyun  * or the xa_lock while iterating.  If you need to drop the lock, call
1741*4882a593Smuzhiyun  * xas_pause() first.
1742*4882a593Smuzhiyun  */
1743*4882a593Smuzhiyun #define xas_for_each_marked(xas, entry, max, mark) \
1744*4882a593Smuzhiyun 	for (entry = xas_find_marked(xas, max, mark); entry; \
1745*4882a593Smuzhiyun 	     entry = xas_next_marked(xas, max, mark))
1746*4882a593Smuzhiyun 
1747*4882a593Smuzhiyun /**
1748*4882a593Smuzhiyun  * xas_for_each_conflict() - Iterate over a range of an XArray.
1749*4882a593Smuzhiyun  * @xas: XArray operation state.
1750*4882a593Smuzhiyun  * @entry: Entry retrieved from the array.
1751*4882a593Smuzhiyun  *
1752*4882a593Smuzhiyun  * The loop body will be executed for each entry in the XArray that
1753*4882a593Smuzhiyun  * lies within the range specified by @xas.  If the loop terminates
1754*4882a593Smuzhiyun  * normally, @entry will be %NULL.  The user may break out of the loop,
1755*4882a593Smuzhiyun  * which will leave @entry set to the conflicting entry.  The caller
1756*4882a593Smuzhiyun  * may also call xa_set_err() to exit the loop while setting an error
1757*4882a593Smuzhiyun  * to record the reason.
1758*4882a593Smuzhiyun  */
1759*4882a593Smuzhiyun #define xas_for_each_conflict(xas, entry) \
1760*4882a593Smuzhiyun 	while ((entry = xas_find_conflict(xas)))
1761*4882a593Smuzhiyun 
1762*4882a593Smuzhiyun void *__xas_next(struct xa_state *);
1763*4882a593Smuzhiyun void *__xas_prev(struct xa_state *);
1764*4882a593Smuzhiyun 
1765*4882a593Smuzhiyun /**
1766*4882a593Smuzhiyun  * xas_prev() - Move iterator to previous index.
1767*4882a593Smuzhiyun  * @xas: XArray operation state.
1768*4882a593Smuzhiyun  *
1769*4882a593Smuzhiyun  * If the @xas was in an error state, it will remain in an error state
1770*4882a593Smuzhiyun  * and this function will return %NULL.  If the @xas has never been walked,
1771*4882a593Smuzhiyun  * it will have the effect of calling xas_load().  Otherwise one will be
1772*4882a593Smuzhiyun  * subtracted from the index and the state will be walked to the correct
1773*4882a593Smuzhiyun  * location in the array for the next operation.
1774*4882a593Smuzhiyun  *
1775*4882a593Smuzhiyun  * If the iterator was referencing index 0, this function wraps
1776*4882a593Smuzhiyun  * around to %ULONG_MAX.
1777*4882a593Smuzhiyun  *
1778*4882a593Smuzhiyun  * Return: The entry at the new index.  This may be %NULL or an internal
1779*4882a593Smuzhiyun  * entry.
1780*4882a593Smuzhiyun  */
xas_prev(struct xa_state * xas)1781*4882a593Smuzhiyun static inline void *xas_prev(struct xa_state *xas)
1782*4882a593Smuzhiyun {
1783*4882a593Smuzhiyun 	struct xa_node *node = xas->xa_node;
1784*4882a593Smuzhiyun 
1785*4882a593Smuzhiyun 	if (unlikely(xas_not_node(node) || node->shift ||
1786*4882a593Smuzhiyun 				xas->xa_offset == 0))
1787*4882a593Smuzhiyun 		return __xas_prev(xas);
1788*4882a593Smuzhiyun 
1789*4882a593Smuzhiyun 	xas->xa_index--;
1790*4882a593Smuzhiyun 	xas->xa_offset--;
1791*4882a593Smuzhiyun 	return xa_entry(xas->xa, node, xas->xa_offset);
1792*4882a593Smuzhiyun }
1793*4882a593Smuzhiyun 
1794*4882a593Smuzhiyun /**
1795*4882a593Smuzhiyun  * xas_next() - Move state to next index.
1796*4882a593Smuzhiyun  * @xas: XArray operation state.
1797*4882a593Smuzhiyun  *
1798*4882a593Smuzhiyun  * If the @xas was in an error state, it will remain in an error state
1799*4882a593Smuzhiyun  * and this function will return %NULL.  If the @xas has never been walked,
1800*4882a593Smuzhiyun  * it will have the effect of calling xas_load().  Otherwise one will be
1801*4882a593Smuzhiyun  * added to the index and the state will be walked to the correct
1802*4882a593Smuzhiyun  * location in the array for the next operation.
1803*4882a593Smuzhiyun  *
1804*4882a593Smuzhiyun  * If the iterator was referencing index %ULONG_MAX, this function wraps
1805*4882a593Smuzhiyun  * around to 0.
1806*4882a593Smuzhiyun  *
1807*4882a593Smuzhiyun  * Return: The entry at the new index.  This may be %NULL or an internal
1808*4882a593Smuzhiyun  * entry.
1809*4882a593Smuzhiyun  */
xas_next(struct xa_state * xas)1810*4882a593Smuzhiyun static inline void *xas_next(struct xa_state *xas)
1811*4882a593Smuzhiyun {
1812*4882a593Smuzhiyun 	struct xa_node *node = xas->xa_node;
1813*4882a593Smuzhiyun 
1814*4882a593Smuzhiyun 	if (unlikely(xas_not_node(node) || node->shift ||
1815*4882a593Smuzhiyun 				xas->xa_offset == XA_CHUNK_MASK))
1816*4882a593Smuzhiyun 		return __xas_next(xas);
1817*4882a593Smuzhiyun 
1818*4882a593Smuzhiyun 	xas->xa_index++;
1819*4882a593Smuzhiyun 	xas->xa_offset++;
1820*4882a593Smuzhiyun 	return xa_entry(xas->xa, node, xas->xa_offset);
1821*4882a593Smuzhiyun }
1822*4882a593Smuzhiyun 
1823*4882a593Smuzhiyun #endif /* _LINUX_XARRAY_H */
1824