xref: /linux-6.15/include/linux/xarray.h (revision 9b89a035)
1f6bb2a2cSMatthew Wilcox /* SPDX-License-Identifier: GPL-2.0+ */
2f6bb2a2cSMatthew Wilcox #ifndef _LINUX_XARRAY_H
3f6bb2a2cSMatthew Wilcox #define _LINUX_XARRAY_H
4f6bb2a2cSMatthew Wilcox /*
5f6bb2a2cSMatthew Wilcox  * eXtensible Arrays
6f6bb2a2cSMatthew Wilcox  * Copyright (c) 2017 Microsoft Corporation
73d0186bbSMatthew Wilcox  * Author: Matthew Wilcox <[email protected]>
83159f943SMatthew Wilcox  *
93159f943SMatthew Wilcox  * See Documentation/core-api/xarray.rst for how to use the XArray.
10f6bb2a2cSMatthew Wilcox  */
11f6bb2a2cSMatthew Wilcox 
123159f943SMatthew Wilcox #include <linux/bug.h>
13f8d5d0ccSMatthew Wilcox #include <linux/compiler.h>
14*9b89a035SMatthew Wilcox #include <linux/gfp.h>
15f8d5d0ccSMatthew Wilcox #include <linux/kconfig.h>
16ad3d6c72SMatthew Wilcox #include <linux/kernel.h>
17ad3d6c72SMatthew Wilcox #include <linux/rcupdate.h>
18f6bb2a2cSMatthew Wilcox #include <linux/spinlock.h>
193159f943SMatthew Wilcox #include <linux/types.h>
203159f943SMatthew Wilcox 
213159f943SMatthew Wilcox /*
223159f943SMatthew Wilcox  * The bottom two bits of the entry determine how the XArray interprets
233159f943SMatthew Wilcox  * the contents:
243159f943SMatthew Wilcox  *
253159f943SMatthew Wilcox  * 00: Pointer entry
263159f943SMatthew Wilcox  * 10: Internal entry
273159f943SMatthew Wilcox  * x1: Value entry or tagged pointer
283159f943SMatthew Wilcox  *
293159f943SMatthew Wilcox  * Attempting to store internal entries in the XArray is a bug.
3002c02bf1SMatthew Wilcox  *
3102c02bf1SMatthew Wilcox  * Most internal entries are pointers to the next node in the tree.
3202c02bf1SMatthew Wilcox  * The following internal entries have a special meaning:
3302c02bf1SMatthew Wilcox  *
3402c02bf1SMatthew Wilcox  * 0-62: Sibling entries
3502c02bf1SMatthew Wilcox  * 256: Retry entry
36ad3d6c72SMatthew Wilcox  *
37ad3d6c72SMatthew Wilcox  * Errors are also represented as internal entries, but use the negative
38ad3d6c72SMatthew Wilcox  * space (-4094 to -2).  They're never stored in the slots array; only
39ad3d6c72SMatthew Wilcox  * returned by the normal API.
403159f943SMatthew Wilcox  */
413159f943SMatthew Wilcox 
423159f943SMatthew Wilcox #define BITS_PER_XA_VALUE	(BITS_PER_LONG - 1)
433159f943SMatthew Wilcox 
443159f943SMatthew Wilcox /**
453159f943SMatthew Wilcox  * xa_mk_value() - Create an XArray entry from an integer.
463159f943SMatthew Wilcox  * @v: Value to store in XArray.
473159f943SMatthew Wilcox  *
483159f943SMatthew Wilcox  * Context: Any context.
493159f943SMatthew Wilcox  * Return: An entry suitable for storing in the XArray.
503159f943SMatthew Wilcox  */
513159f943SMatthew Wilcox static inline void *xa_mk_value(unsigned long v)
523159f943SMatthew Wilcox {
533159f943SMatthew Wilcox 	WARN_ON((long)v < 0);
543159f943SMatthew Wilcox 	return (void *)((v << 1) | 1);
553159f943SMatthew Wilcox }
563159f943SMatthew Wilcox 
573159f943SMatthew Wilcox /**
583159f943SMatthew Wilcox  * xa_to_value() - Get value stored in an XArray entry.
593159f943SMatthew Wilcox  * @entry: XArray entry.
603159f943SMatthew Wilcox  *
613159f943SMatthew Wilcox  * Context: Any context.
623159f943SMatthew Wilcox  * Return: The value stored in the XArray entry.
633159f943SMatthew Wilcox  */
643159f943SMatthew Wilcox static inline unsigned long xa_to_value(const void *entry)
653159f943SMatthew Wilcox {
663159f943SMatthew Wilcox 	return (unsigned long)entry >> 1;
673159f943SMatthew Wilcox }
683159f943SMatthew Wilcox 
693159f943SMatthew Wilcox /**
703159f943SMatthew Wilcox  * xa_is_value() - Determine if an entry is a value.
713159f943SMatthew Wilcox  * @entry: XArray entry.
723159f943SMatthew Wilcox  *
733159f943SMatthew Wilcox  * Context: Any context.
743159f943SMatthew Wilcox  * Return: True if the entry is a value, false if it is a pointer.
753159f943SMatthew Wilcox  */
763159f943SMatthew Wilcox static inline bool xa_is_value(const void *entry)
773159f943SMatthew Wilcox {
783159f943SMatthew Wilcox 	return (unsigned long)entry & 1;
793159f943SMatthew Wilcox }
803159f943SMatthew Wilcox 
813159f943SMatthew Wilcox /**
823159f943SMatthew Wilcox  * xa_tag_pointer() - Create an XArray entry for a tagged pointer.
833159f943SMatthew Wilcox  * @p: Plain pointer.
843159f943SMatthew Wilcox  * @tag: Tag value (0, 1 or 3).
853159f943SMatthew Wilcox  *
863159f943SMatthew Wilcox  * If the user of the XArray prefers, they can tag their pointers instead
873159f943SMatthew Wilcox  * of storing value entries.  Three tags are available (0, 1 and 3).
883159f943SMatthew Wilcox  * These are distinct from the xa_mark_t as they are not replicated up
893159f943SMatthew Wilcox  * through the array and cannot be searched for.
903159f943SMatthew Wilcox  *
913159f943SMatthew Wilcox  * Context: Any context.
923159f943SMatthew Wilcox  * Return: An XArray entry.
933159f943SMatthew Wilcox  */
943159f943SMatthew Wilcox static inline void *xa_tag_pointer(void *p, unsigned long tag)
953159f943SMatthew Wilcox {
963159f943SMatthew Wilcox 	return (void *)((unsigned long)p | tag);
973159f943SMatthew Wilcox }
983159f943SMatthew Wilcox 
993159f943SMatthew Wilcox /**
1003159f943SMatthew Wilcox  * xa_untag_pointer() - Turn an XArray entry into a plain pointer.
1013159f943SMatthew Wilcox  * @entry: XArray entry.
1023159f943SMatthew Wilcox  *
1033159f943SMatthew Wilcox  * If you have stored a tagged pointer in the XArray, call this function
1043159f943SMatthew Wilcox  * to get the untagged version of the pointer.
1053159f943SMatthew Wilcox  *
1063159f943SMatthew Wilcox  * Context: Any context.
1073159f943SMatthew Wilcox  * Return: A pointer.
1083159f943SMatthew Wilcox  */
1093159f943SMatthew Wilcox static inline void *xa_untag_pointer(void *entry)
1103159f943SMatthew Wilcox {
1113159f943SMatthew Wilcox 	return (void *)((unsigned long)entry & ~3UL);
1123159f943SMatthew Wilcox }
1133159f943SMatthew Wilcox 
1143159f943SMatthew Wilcox /**
1153159f943SMatthew Wilcox  * xa_pointer_tag() - Get the tag stored in an XArray entry.
1163159f943SMatthew Wilcox  * @entry: XArray entry.
1173159f943SMatthew Wilcox  *
1183159f943SMatthew Wilcox  * If you have stored a tagged pointer in the XArray, call this function
1193159f943SMatthew Wilcox  * to get the tag of that pointer.
1203159f943SMatthew Wilcox  *
1213159f943SMatthew Wilcox  * Context: Any context.
1223159f943SMatthew Wilcox  * Return: A tag.
1233159f943SMatthew Wilcox  */
1243159f943SMatthew Wilcox static inline unsigned int xa_pointer_tag(void *entry)
1253159f943SMatthew Wilcox {
1263159f943SMatthew Wilcox 	return (unsigned long)entry & 3UL;
1273159f943SMatthew Wilcox }
128f6bb2a2cSMatthew Wilcox 
12902c02bf1SMatthew Wilcox /*
13002c02bf1SMatthew Wilcox  * xa_mk_internal() - Create an internal entry.
13102c02bf1SMatthew Wilcox  * @v: Value to turn into an internal entry.
13202c02bf1SMatthew Wilcox  *
13302c02bf1SMatthew Wilcox  * Context: Any context.
13402c02bf1SMatthew Wilcox  * Return: An XArray internal entry corresponding to this value.
13502c02bf1SMatthew Wilcox  */
13602c02bf1SMatthew Wilcox static inline void *xa_mk_internal(unsigned long v)
13702c02bf1SMatthew Wilcox {
13802c02bf1SMatthew Wilcox 	return (void *)((v << 2) | 2);
13902c02bf1SMatthew Wilcox }
14002c02bf1SMatthew Wilcox 
14102c02bf1SMatthew Wilcox /*
14202c02bf1SMatthew Wilcox  * xa_to_internal() - Extract the value from an internal entry.
14302c02bf1SMatthew Wilcox  * @entry: XArray entry.
14402c02bf1SMatthew Wilcox  *
14502c02bf1SMatthew Wilcox  * Context: Any context.
14602c02bf1SMatthew Wilcox  * Return: The value which was stored in the internal entry.
14702c02bf1SMatthew Wilcox  */
14802c02bf1SMatthew Wilcox static inline unsigned long xa_to_internal(const void *entry)
14902c02bf1SMatthew Wilcox {
15002c02bf1SMatthew Wilcox 	return (unsigned long)entry >> 2;
15102c02bf1SMatthew Wilcox }
15202c02bf1SMatthew Wilcox 
15302c02bf1SMatthew Wilcox /*
15402c02bf1SMatthew Wilcox  * xa_is_internal() - Is the entry an internal entry?
15502c02bf1SMatthew Wilcox  * @entry: XArray entry.
15602c02bf1SMatthew Wilcox  *
15702c02bf1SMatthew Wilcox  * Context: Any context.
15802c02bf1SMatthew Wilcox  * Return: %true if the entry is an internal entry.
15902c02bf1SMatthew Wilcox  */
16002c02bf1SMatthew Wilcox static inline bool xa_is_internal(const void *entry)
16102c02bf1SMatthew Wilcox {
16202c02bf1SMatthew Wilcox 	return ((unsigned long)entry & 3) == 2;
16302c02bf1SMatthew Wilcox }
16402c02bf1SMatthew Wilcox 
165f8d5d0ccSMatthew Wilcox /**
166ad3d6c72SMatthew Wilcox  * xa_is_err() - Report whether an XArray operation returned an error
167ad3d6c72SMatthew Wilcox  * @entry: Result from calling an XArray function
168ad3d6c72SMatthew Wilcox  *
169ad3d6c72SMatthew Wilcox  * If an XArray operation cannot complete an operation, it will return
170ad3d6c72SMatthew Wilcox  * a special value indicating an error.  This function tells you
171ad3d6c72SMatthew Wilcox  * whether an error occurred; xa_err() tells you which error occurred.
172ad3d6c72SMatthew Wilcox  *
173ad3d6c72SMatthew Wilcox  * Context: Any context.
174ad3d6c72SMatthew Wilcox  * Return: %true if the entry indicates an error.
175ad3d6c72SMatthew Wilcox  */
176ad3d6c72SMatthew Wilcox static inline bool xa_is_err(const void *entry)
177ad3d6c72SMatthew Wilcox {
178ad3d6c72SMatthew Wilcox 	return unlikely(xa_is_internal(entry));
179ad3d6c72SMatthew Wilcox }
180ad3d6c72SMatthew Wilcox 
181ad3d6c72SMatthew Wilcox /**
182ad3d6c72SMatthew Wilcox  * xa_err() - Turn an XArray result into an errno.
183ad3d6c72SMatthew Wilcox  * @entry: Result from calling an XArray function.
184ad3d6c72SMatthew Wilcox  *
185ad3d6c72SMatthew Wilcox  * If an XArray operation cannot complete an operation, it will return
186ad3d6c72SMatthew Wilcox  * a special pointer value which encodes an errno.  This function extracts
187ad3d6c72SMatthew Wilcox  * the errno from the pointer value, or returns 0 if the pointer does not
188ad3d6c72SMatthew Wilcox  * represent an errno.
189ad3d6c72SMatthew Wilcox  *
190ad3d6c72SMatthew Wilcox  * Context: Any context.
191ad3d6c72SMatthew Wilcox  * Return: A negative errno or 0.
192ad3d6c72SMatthew Wilcox  */
193ad3d6c72SMatthew Wilcox static inline int xa_err(void *entry)
194ad3d6c72SMatthew Wilcox {
195ad3d6c72SMatthew Wilcox 	/* xa_to_internal() would not do sign extension. */
196ad3d6c72SMatthew Wilcox 	if (xa_is_err(entry))
197ad3d6c72SMatthew Wilcox 		return (long)entry >> 2;
198ad3d6c72SMatthew Wilcox 	return 0;
199ad3d6c72SMatthew Wilcox }
200ad3d6c72SMatthew Wilcox 
201*9b89a035SMatthew Wilcox typedef unsigned __bitwise xa_mark_t;
202*9b89a035SMatthew Wilcox #define XA_MARK_0		((__force xa_mark_t)0U)
203*9b89a035SMatthew Wilcox #define XA_MARK_1		((__force xa_mark_t)1U)
204*9b89a035SMatthew Wilcox #define XA_MARK_2		((__force xa_mark_t)2U)
205*9b89a035SMatthew Wilcox #define XA_PRESENT		((__force xa_mark_t)8U)
206*9b89a035SMatthew Wilcox #define XA_MARK_MAX		XA_MARK_2
207*9b89a035SMatthew Wilcox 
208*9b89a035SMatthew Wilcox /*
209*9b89a035SMatthew Wilcox  * Values for xa_flags.  The radix tree stores its GFP flags in the xa_flags,
210*9b89a035SMatthew Wilcox  * and we remain compatible with that.
211*9b89a035SMatthew Wilcox  */
212*9b89a035SMatthew Wilcox #define XA_FLAGS_MARK(mark)	((__force gfp_t)((1U << __GFP_BITS_SHIFT) << \
213*9b89a035SMatthew Wilcox 						(__force unsigned)(mark)))
214*9b89a035SMatthew Wilcox 
215ad3d6c72SMatthew Wilcox /**
216f8d5d0ccSMatthew Wilcox  * struct xarray - The anchor of the XArray.
217f8d5d0ccSMatthew Wilcox  * @xa_lock: Lock that protects the contents of the XArray.
218f8d5d0ccSMatthew Wilcox  *
219f8d5d0ccSMatthew Wilcox  * To use the xarray, define it statically or embed it in your data structure.
220f8d5d0ccSMatthew Wilcox  * It is a very small data structure, so it does not usually make sense to
221f8d5d0ccSMatthew Wilcox  * allocate it separately and keep a pointer to it in your data structure.
222f8d5d0ccSMatthew Wilcox  *
223f8d5d0ccSMatthew Wilcox  * You may use the xa_lock to protect your own data structures as well.
224f8d5d0ccSMatthew Wilcox  */
225f8d5d0ccSMatthew Wilcox /*
226f8d5d0ccSMatthew Wilcox  * If all of the entries in the array are NULL, @xa_head is a NULL pointer.
227f8d5d0ccSMatthew Wilcox  * If the only non-NULL entry in the array is at index 0, @xa_head is that
228f8d5d0ccSMatthew Wilcox  * entry.  If any other entry in the array is non-NULL, @xa_head points
229f8d5d0ccSMatthew Wilcox  * to an @xa_node.
230f8d5d0ccSMatthew Wilcox  */
231f8d5d0ccSMatthew Wilcox struct xarray {
232f8d5d0ccSMatthew Wilcox 	spinlock_t	xa_lock;
233f8d5d0ccSMatthew Wilcox /* private: The rest of the data structure is not to be used directly. */
234f8d5d0ccSMatthew Wilcox 	gfp_t		xa_flags;
235f8d5d0ccSMatthew Wilcox 	void __rcu *	xa_head;
236f8d5d0ccSMatthew Wilcox };
237f8d5d0ccSMatthew Wilcox 
238f8d5d0ccSMatthew Wilcox #define XARRAY_INIT(name, flags) {				\
239f8d5d0ccSMatthew Wilcox 	.xa_lock = __SPIN_LOCK_UNLOCKED(name.xa_lock),		\
240f8d5d0ccSMatthew Wilcox 	.xa_flags = flags,					\
241f8d5d0ccSMatthew Wilcox 	.xa_head = NULL,					\
242f8d5d0ccSMatthew Wilcox }
243f8d5d0ccSMatthew Wilcox 
244f8d5d0ccSMatthew Wilcox /**
245f8d5d0ccSMatthew Wilcox  * DEFINE_XARRAY_FLAGS() - Define an XArray with custom flags.
246f8d5d0ccSMatthew Wilcox  * @name: A string that names your XArray.
247f8d5d0ccSMatthew Wilcox  * @flags: XA_FLAG values.
248f8d5d0ccSMatthew Wilcox  *
249f8d5d0ccSMatthew Wilcox  * This is intended for file scope definitions of XArrays.  It declares
250f8d5d0ccSMatthew Wilcox  * and initialises an empty XArray with the chosen name and flags.  It is
251f8d5d0ccSMatthew Wilcox  * equivalent to calling xa_init_flags() on the array, but it does the
252f8d5d0ccSMatthew Wilcox  * initialisation at compiletime instead of runtime.
253f8d5d0ccSMatthew Wilcox  */
254f8d5d0ccSMatthew Wilcox #define DEFINE_XARRAY_FLAGS(name, flags)				\
255f8d5d0ccSMatthew Wilcox 	struct xarray name = XARRAY_INIT(name, flags)
256f8d5d0ccSMatthew Wilcox 
257f8d5d0ccSMatthew Wilcox /**
258f8d5d0ccSMatthew Wilcox  * DEFINE_XARRAY() - Define an XArray.
259f8d5d0ccSMatthew Wilcox  * @name: A string that names your XArray.
260f8d5d0ccSMatthew Wilcox  *
261f8d5d0ccSMatthew Wilcox  * This is intended for file scope definitions of XArrays.  It declares
262f8d5d0ccSMatthew Wilcox  * and initialises an empty XArray with the chosen name.  It is equivalent
263f8d5d0ccSMatthew Wilcox  * to calling xa_init() on the array, but it does the initialisation at
264f8d5d0ccSMatthew Wilcox  * compiletime instead of runtime.
265f8d5d0ccSMatthew Wilcox  */
266f8d5d0ccSMatthew Wilcox #define DEFINE_XARRAY(name) DEFINE_XARRAY_FLAGS(name, 0)
267f8d5d0ccSMatthew Wilcox 
268f8d5d0ccSMatthew Wilcox void xa_init_flags(struct xarray *, gfp_t flags);
269ad3d6c72SMatthew Wilcox void *xa_load(struct xarray *, unsigned long index);
270*9b89a035SMatthew Wilcox bool xa_get_mark(struct xarray *, unsigned long index, xa_mark_t);
271*9b89a035SMatthew Wilcox void xa_set_mark(struct xarray *, unsigned long index, xa_mark_t);
272*9b89a035SMatthew Wilcox void xa_clear_mark(struct xarray *, unsigned long index, xa_mark_t);
273f8d5d0ccSMatthew Wilcox 
274f8d5d0ccSMatthew Wilcox /**
275f8d5d0ccSMatthew Wilcox  * xa_init() - Initialise an empty XArray.
276f8d5d0ccSMatthew Wilcox  * @xa: XArray.
277f8d5d0ccSMatthew Wilcox  *
278f8d5d0ccSMatthew Wilcox  * An empty XArray is full of NULL entries.
279f8d5d0ccSMatthew Wilcox  *
280f8d5d0ccSMatthew Wilcox  * Context: Any context.
281f8d5d0ccSMatthew Wilcox  */
282f8d5d0ccSMatthew Wilcox static inline void xa_init(struct xarray *xa)
283f8d5d0ccSMatthew Wilcox {
284f8d5d0ccSMatthew Wilcox 	xa_init_flags(xa, 0);
285f8d5d0ccSMatthew Wilcox }
286f8d5d0ccSMatthew Wilcox 
287ad3d6c72SMatthew Wilcox /**
288ad3d6c72SMatthew Wilcox  * xa_empty() - Determine if an array has any present entries.
289ad3d6c72SMatthew Wilcox  * @xa: XArray.
290ad3d6c72SMatthew Wilcox  *
291ad3d6c72SMatthew Wilcox  * Context: Any context.
292ad3d6c72SMatthew Wilcox  * Return: %true if the array contains only NULL pointers.
293ad3d6c72SMatthew Wilcox  */
294ad3d6c72SMatthew Wilcox static inline bool xa_empty(const struct xarray *xa)
295ad3d6c72SMatthew Wilcox {
296ad3d6c72SMatthew Wilcox 	return xa->xa_head == NULL;
297ad3d6c72SMatthew Wilcox }
298ad3d6c72SMatthew Wilcox 
299*9b89a035SMatthew Wilcox /**
300*9b89a035SMatthew Wilcox  * xa_marked() - Inquire whether any entry in this array has a mark set
301*9b89a035SMatthew Wilcox  * @xa: Array
302*9b89a035SMatthew Wilcox  * @mark: Mark value
303*9b89a035SMatthew Wilcox  *
304*9b89a035SMatthew Wilcox  * Context: Any context.
305*9b89a035SMatthew Wilcox  * Return: %true if any entry has this mark set.
306*9b89a035SMatthew Wilcox  */
307*9b89a035SMatthew Wilcox static inline bool xa_marked(const struct xarray *xa, xa_mark_t mark)
308*9b89a035SMatthew Wilcox {
309*9b89a035SMatthew Wilcox 	return xa->xa_flags & XA_FLAGS_MARK(mark);
310*9b89a035SMatthew Wilcox }
311*9b89a035SMatthew Wilcox 
312f6bb2a2cSMatthew Wilcox #define xa_trylock(xa)		spin_trylock(&(xa)->xa_lock)
313f6bb2a2cSMatthew Wilcox #define xa_lock(xa)		spin_lock(&(xa)->xa_lock)
314f6bb2a2cSMatthew Wilcox #define xa_unlock(xa)		spin_unlock(&(xa)->xa_lock)
315f6bb2a2cSMatthew Wilcox #define xa_lock_bh(xa)		spin_lock_bh(&(xa)->xa_lock)
316f6bb2a2cSMatthew Wilcox #define xa_unlock_bh(xa)	spin_unlock_bh(&(xa)->xa_lock)
317f6bb2a2cSMatthew Wilcox #define xa_lock_irq(xa)		spin_lock_irq(&(xa)->xa_lock)
318f6bb2a2cSMatthew Wilcox #define xa_unlock_irq(xa)	spin_unlock_irq(&(xa)->xa_lock)
319f6bb2a2cSMatthew Wilcox #define xa_lock_irqsave(xa, flags) \
320f6bb2a2cSMatthew Wilcox 				spin_lock_irqsave(&(xa)->xa_lock, flags)
321f6bb2a2cSMatthew Wilcox #define xa_unlock_irqrestore(xa, flags) \
322f6bb2a2cSMatthew Wilcox 				spin_unlock_irqrestore(&(xa)->xa_lock, flags)
323f6bb2a2cSMatthew Wilcox 
324*9b89a035SMatthew Wilcox /*
325*9b89a035SMatthew Wilcox  * Versions of the normal API which require the caller to hold the xa_lock.
326*9b89a035SMatthew Wilcox  */
327*9b89a035SMatthew Wilcox void __xa_set_mark(struct xarray *, unsigned long index, xa_mark_t);
328*9b89a035SMatthew Wilcox void __xa_clear_mark(struct xarray *, unsigned long index, xa_mark_t);
329*9b89a035SMatthew Wilcox 
33002c02bf1SMatthew Wilcox /* Everything below here is the Advanced API.  Proceed with caution. */
33102c02bf1SMatthew Wilcox 
33202c02bf1SMatthew Wilcox /*
33302c02bf1SMatthew Wilcox  * The xarray is constructed out of a set of 'chunks' of pointers.  Choosing
33402c02bf1SMatthew Wilcox  * the best chunk size requires some tradeoffs.  A power of two recommends
33502c02bf1SMatthew Wilcox  * itself so that we can walk the tree based purely on shifts and masks.
33602c02bf1SMatthew Wilcox  * Generally, the larger the better; as the number of slots per level of the
33702c02bf1SMatthew Wilcox  * tree increases, the less tall the tree needs to be.  But that needs to be
33802c02bf1SMatthew Wilcox  * balanced against the memory consumption of each node.  On a 64-bit system,
33902c02bf1SMatthew Wilcox  * xa_node is currently 576 bytes, and we get 7 of them per 4kB page.  If we
34002c02bf1SMatthew Wilcox  * doubled the number of slots per node, we'd get only 3 nodes per 4kB page.
34102c02bf1SMatthew Wilcox  */
34202c02bf1SMatthew Wilcox #ifndef XA_CHUNK_SHIFT
34302c02bf1SMatthew Wilcox #define XA_CHUNK_SHIFT		(CONFIG_BASE_SMALL ? 4 : 6)
34402c02bf1SMatthew Wilcox #endif
34502c02bf1SMatthew Wilcox #define XA_CHUNK_SIZE		(1UL << XA_CHUNK_SHIFT)
34602c02bf1SMatthew Wilcox #define XA_CHUNK_MASK		(XA_CHUNK_SIZE - 1)
34701959dfeSMatthew Wilcox #define XA_MAX_MARKS		3
34801959dfeSMatthew Wilcox #define XA_MARK_LONGS		DIV_ROUND_UP(XA_CHUNK_SIZE, BITS_PER_LONG)
34901959dfeSMatthew Wilcox 
35001959dfeSMatthew Wilcox /*
35101959dfeSMatthew Wilcox  * @count is the count of every non-NULL element in the ->slots array
35201959dfeSMatthew Wilcox  * whether that is a value entry, a retry entry, a user pointer,
35301959dfeSMatthew Wilcox  * a sibling entry or a pointer to the next level of the tree.
35401959dfeSMatthew Wilcox  * @nr_values is the count of every element in ->slots which is
35501959dfeSMatthew Wilcox  * either a value entry or a sibling of a value entry.
35601959dfeSMatthew Wilcox  */
35701959dfeSMatthew Wilcox struct xa_node {
35801959dfeSMatthew Wilcox 	unsigned char	shift;		/* Bits remaining in each slot */
35901959dfeSMatthew Wilcox 	unsigned char	offset;		/* Slot offset in parent */
36001959dfeSMatthew Wilcox 	unsigned char	count;		/* Total entry count */
36101959dfeSMatthew Wilcox 	unsigned char	nr_values;	/* Value entry count */
36201959dfeSMatthew Wilcox 	struct xa_node __rcu *parent;	/* NULL at top of tree */
36301959dfeSMatthew Wilcox 	struct xarray	*array;		/* The array we belong to */
36401959dfeSMatthew Wilcox 	union {
36501959dfeSMatthew Wilcox 		struct list_head private_list;	/* For tree user */
36601959dfeSMatthew Wilcox 		struct rcu_head	rcu_head;	/* Used when freeing node */
36701959dfeSMatthew Wilcox 	};
36801959dfeSMatthew Wilcox 	void __rcu	*slots[XA_CHUNK_SIZE];
36901959dfeSMatthew Wilcox 	union {
37001959dfeSMatthew Wilcox 		unsigned long	tags[XA_MAX_MARKS][XA_MARK_LONGS];
37101959dfeSMatthew Wilcox 		unsigned long	marks[XA_MAX_MARKS][XA_MARK_LONGS];
37201959dfeSMatthew Wilcox 	};
37301959dfeSMatthew Wilcox };
37402c02bf1SMatthew Wilcox 
375ad3d6c72SMatthew Wilcox void xa_dump(const struct xarray *);
376ad3d6c72SMatthew Wilcox void xa_dump_node(const struct xa_node *);
377ad3d6c72SMatthew Wilcox 
378ad3d6c72SMatthew Wilcox #ifdef XA_DEBUG
379ad3d6c72SMatthew Wilcox #define XA_BUG_ON(xa, x) do {					\
380ad3d6c72SMatthew Wilcox 		if (x) {					\
381ad3d6c72SMatthew Wilcox 			xa_dump(xa);				\
382ad3d6c72SMatthew Wilcox 			BUG();					\
383ad3d6c72SMatthew Wilcox 		}						\
384ad3d6c72SMatthew Wilcox 	} while (0)
385ad3d6c72SMatthew Wilcox #define XA_NODE_BUG_ON(node, x) do {				\
386ad3d6c72SMatthew Wilcox 		if (x) {					\
387ad3d6c72SMatthew Wilcox 			if (node) xa_dump_node(node);		\
388ad3d6c72SMatthew Wilcox 			BUG();					\
389ad3d6c72SMatthew Wilcox 		}						\
390ad3d6c72SMatthew Wilcox 	} while (0)
391ad3d6c72SMatthew Wilcox #else
392ad3d6c72SMatthew Wilcox #define XA_BUG_ON(xa, x)	do { } while (0)
393ad3d6c72SMatthew Wilcox #define XA_NODE_BUG_ON(node, x)	do { } while (0)
394ad3d6c72SMatthew Wilcox #endif
395ad3d6c72SMatthew Wilcox 
396ad3d6c72SMatthew Wilcox /* Private */
397ad3d6c72SMatthew Wilcox static inline void *xa_head(const struct xarray *xa)
398ad3d6c72SMatthew Wilcox {
399ad3d6c72SMatthew Wilcox 	return rcu_dereference_check(xa->xa_head,
400ad3d6c72SMatthew Wilcox 						lockdep_is_held(&xa->xa_lock));
401ad3d6c72SMatthew Wilcox }
402ad3d6c72SMatthew Wilcox 
403ad3d6c72SMatthew Wilcox /* Private */
404ad3d6c72SMatthew Wilcox static inline void *xa_head_locked(const struct xarray *xa)
405ad3d6c72SMatthew Wilcox {
406ad3d6c72SMatthew Wilcox 	return rcu_dereference_protected(xa->xa_head,
407ad3d6c72SMatthew Wilcox 						lockdep_is_held(&xa->xa_lock));
408ad3d6c72SMatthew Wilcox }
409ad3d6c72SMatthew Wilcox 
410ad3d6c72SMatthew Wilcox /* Private */
411ad3d6c72SMatthew Wilcox static inline void *xa_entry(const struct xarray *xa,
412ad3d6c72SMatthew Wilcox 				const struct xa_node *node, unsigned int offset)
413ad3d6c72SMatthew Wilcox {
414ad3d6c72SMatthew Wilcox 	XA_NODE_BUG_ON(node, offset >= XA_CHUNK_SIZE);
415ad3d6c72SMatthew Wilcox 	return rcu_dereference_check(node->slots[offset],
416ad3d6c72SMatthew Wilcox 						lockdep_is_held(&xa->xa_lock));
417ad3d6c72SMatthew Wilcox }
418ad3d6c72SMatthew Wilcox 
419ad3d6c72SMatthew Wilcox /* Private */
420ad3d6c72SMatthew Wilcox static inline void *xa_entry_locked(const struct xarray *xa,
421ad3d6c72SMatthew Wilcox 				const struct xa_node *node, unsigned int offset)
422ad3d6c72SMatthew Wilcox {
423ad3d6c72SMatthew Wilcox 	XA_NODE_BUG_ON(node, offset >= XA_CHUNK_SIZE);
424ad3d6c72SMatthew Wilcox 	return rcu_dereference_protected(node->slots[offset],
425ad3d6c72SMatthew Wilcox 						lockdep_is_held(&xa->xa_lock));
426ad3d6c72SMatthew Wilcox }
427ad3d6c72SMatthew Wilcox 
428ad3d6c72SMatthew Wilcox /* Private */
429*9b89a035SMatthew Wilcox static inline struct xa_node *xa_parent(const struct xarray *xa,
430*9b89a035SMatthew Wilcox 					const struct xa_node *node)
431*9b89a035SMatthew Wilcox {
432*9b89a035SMatthew Wilcox 	return rcu_dereference_check(node->parent,
433*9b89a035SMatthew Wilcox 						lockdep_is_held(&xa->xa_lock));
434*9b89a035SMatthew Wilcox }
435*9b89a035SMatthew Wilcox 
436*9b89a035SMatthew Wilcox /* Private */
437*9b89a035SMatthew Wilcox static inline struct xa_node *xa_parent_locked(const struct xarray *xa,
438*9b89a035SMatthew Wilcox 					const struct xa_node *node)
439*9b89a035SMatthew Wilcox {
440*9b89a035SMatthew Wilcox 	return rcu_dereference_protected(node->parent,
441*9b89a035SMatthew Wilcox 						lockdep_is_held(&xa->xa_lock));
442*9b89a035SMatthew Wilcox }
443*9b89a035SMatthew Wilcox 
444*9b89a035SMatthew Wilcox /* Private */
445ad3d6c72SMatthew Wilcox static inline struct xa_node *xa_to_node(const void *entry)
446ad3d6c72SMatthew Wilcox {
447ad3d6c72SMatthew Wilcox 	return (struct xa_node *)((unsigned long)entry - 2);
448ad3d6c72SMatthew Wilcox }
449ad3d6c72SMatthew Wilcox 
45002c02bf1SMatthew Wilcox /* Private */
45102c02bf1SMatthew Wilcox static inline bool xa_is_node(const void *entry)
45202c02bf1SMatthew Wilcox {
45302c02bf1SMatthew Wilcox 	return xa_is_internal(entry) && (unsigned long)entry > 4096;
45402c02bf1SMatthew Wilcox }
45502c02bf1SMatthew Wilcox 
45602c02bf1SMatthew Wilcox /* Private */
45702c02bf1SMatthew Wilcox static inline void *xa_mk_sibling(unsigned int offset)
45802c02bf1SMatthew Wilcox {
45902c02bf1SMatthew Wilcox 	return xa_mk_internal(offset);
46002c02bf1SMatthew Wilcox }
46102c02bf1SMatthew Wilcox 
46202c02bf1SMatthew Wilcox /* Private */
46302c02bf1SMatthew Wilcox static inline unsigned long xa_to_sibling(const void *entry)
46402c02bf1SMatthew Wilcox {
46502c02bf1SMatthew Wilcox 	return xa_to_internal(entry);
46602c02bf1SMatthew Wilcox }
46702c02bf1SMatthew Wilcox 
46802c02bf1SMatthew Wilcox /**
46902c02bf1SMatthew Wilcox  * xa_is_sibling() - Is the entry a sibling entry?
47002c02bf1SMatthew Wilcox  * @entry: Entry retrieved from the XArray
47102c02bf1SMatthew Wilcox  *
47202c02bf1SMatthew Wilcox  * Return: %true if the entry is a sibling entry.
47302c02bf1SMatthew Wilcox  */
47402c02bf1SMatthew Wilcox static inline bool xa_is_sibling(const void *entry)
47502c02bf1SMatthew Wilcox {
47602c02bf1SMatthew Wilcox 	return IS_ENABLED(CONFIG_XARRAY_MULTI) && xa_is_internal(entry) &&
47702c02bf1SMatthew Wilcox 		(entry < xa_mk_sibling(XA_CHUNK_SIZE - 1));
47802c02bf1SMatthew Wilcox }
47902c02bf1SMatthew Wilcox 
48002c02bf1SMatthew Wilcox #define XA_RETRY_ENTRY		xa_mk_internal(256)
48102c02bf1SMatthew Wilcox 
482ad3d6c72SMatthew Wilcox /**
483ad3d6c72SMatthew Wilcox  * xa_is_retry() - Is the entry a retry entry?
484ad3d6c72SMatthew Wilcox  * @entry: Entry retrieved from the XArray
485ad3d6c72SMatthew Wilcox  *
486ad3d6c72SMatthew Wilcox  * Return: %true if the entry is a retry entry.
487ad3d6c72SMatthew Wilcox  */
488ad3d6c72SMatthew Wilcox static inline bool xa_is_retry(const void *entry)
489ad3d6c72SMatthew Wilcox {
490ad3d6c72SMatthew Wilcox 	return unlikely(entry == XA_RETRY_ENTRY);
491ad3d6c72SMatthew Wilcox }
492ad3d6c72SMatthew Wilcox 
493ad3d6c72SMatthew Wilcox /**
494ad3d6c72SMatthew Wilcox  * typedef xa_update_node_t - A callback function from the XArray.
495ad3d6c72SMatthew Wilcox  * @node: The node which is being processed
496ad3d6c72SMatthew Wilcox  *
497ad3d6c72SMatthew Wilcox  * This function is called every time the XArray updates the count of
498ad3d6c72SMatthew Wilcox  * present and value entries in a node.  It allows advanced users to
499ad3d6c72SMatthew Wilcox  * maintain the private_list in the node.
500ad3d6c72SMatthew Wilcox  *
501ad3d6c72SMatthew Wilcox  * Context: The xa_lock is held and interrupts may be disabled.
502ad3d6c72SMatthew Wilcox  *	    Implementations should not drop the xa_lock, nor re-enable
503ad3d6c72SMatthew Wilcox  *	    interrupts.
504ad3d6c72SMatthew Wilcox  */
505ad3d6c72SMatthew Wilcox typedef void (*xa_update_node_t)(struct xa_node *node);
506ad3d6c72SMatthew Wilcox 
507ad3d6c72SMatthew Wilcox /*
508ad3d6c72SMatthew Wilcox  * The xa_state is opaque to its users.  It contains various different pieces
509ad3d6c72SMatthew Wilcox  * of state involved in the current operation on the XArray.  It should be
510ad3d6c72SMatthew Wilcox  * declared on the stack and passed between the various internal routines.
511ad3d6c72SMatthew Wilcox  * The various elements in it should not be accessed directly, but only
512ad3d6c72SMatthew Wilcox  * through the provided accessor functions.  The below documentation is for
513ad3d6c72SMatthew Wilcox  * the benefit of those working on the code, not for users of the XArray.
514ad3d6c72SMatthew Wilcox  *
515ad3d6c72SMatthew Wilcox  * @xa_node usually points to the xa_node containing the slot we're operating
516ad3d6c72SMatthew Wilcox  * on (and @xa_offset is the offset in the slots array).  If there is a
517ad3d6c72SMatthew Wilcox  * single entry in the array at index 0, there are no allocated xa_nodes to
518ad3d6c72SMatthew Wilcox  * point to, and so we store %NULL in @xa_node.  @xa_node is set to
519ad3d6c72SMatthew Wilcox  * the value %XAS_RESTART if the xa_state is not walked to the correct
520ad3d6c72SMatthew Wilcox  * position in the tree of nodes for this operation.  If an error occurs
521ad3d6c72SMatthew Wilcox  * during an operation, it is set to an %XAS_ERROR value.  If we run off the
522ad3d6c72SMatthew Wilcox  * end of the allocated nodes, it is set to %XAS_BOUNDS.
523ad3d6c72SMatthew Wilcox  */
524ad3d6c72SMatthew Wilcox struct xa_state {
525ad3d6c72SMatthew Wilcox 	struct xarray *xa;
526ad3d6c72SMatthew Wilcox 	unsigned long xa_index;
527ad3d6c72SMatthew Wilcox 	unsigned char xa_shift;
528ad3d6c72SMatthew Wilcox 	unsigned char xa_sibs;
529ad3d6c72SMatthew Wilcox 	unsigned char xa_offset;
530ad3d6c72SMatthew Wilcox 	unsigned char xa_pad;		/* Helps gcc generate better code */
531ad3d6c72SMatthew Wilcox 	struct xa_node *xa_node;
532ad3d6c72SMatthew Wilcox 	struct xa_node *xa_alloc;
533ad3d6c72SMatthew Wilcox 	xa_update_node_t xa_update;
534ad3d6c72SMatthew Wilcox };
535ad3d6c72SMatthew Wilcox 
536ad3d6c72SMatthew Wilcox /*
537ad3d6c72SMatthew Wilcox  * We encode errnos in the xas->xa_node.  If an error has happened, we need to
538ad3d6c72SMatthew Wilcox  * drop the lock to fix it, and once we've done so the xa_state is invalid.
539ad3d6c72SMatthew Wilcox  */
540ad3d6c72SMatthew Wilcox #define XA_ERROR(errno) ((struct xa_node *)(((unsigned long)errno << 2) | 2UL))
541ad3d6c72SMatthew Wilcox #define XAS_BOUNDS	((struct xa_node *)1UL)
542ad3d6c72SMatthew Wilcox #define XAS_RESTART	((struct xa_node *)3UL)
543ad3d6c72SMatthew Wilcox 
544ad3d6c72SMatthew Wilcox #define __XA_STATE(array, index, shift, sibs)  {	\
545ad3d6c72SMatthew Wilcox 	.xa = array,					\
546ad3d6c72SMatthew Wilcox 	.xa_index = index,				\
547ad3d6c72SMatthew Wilcox 	.xa_shift = shift,				\
548ad3d6c72SMatthew Wilcox 	.xa_sibs = sibs,				\
549ad3d6c72SMatthew Wilcox 	.xa_offset = 0,					\
550ad3d6c72SMatthew Wilcox 	.xa_pad = 0,					\
551ad3d6c72SMatthew Wilcox 	.xa_node = XAS_RESTART,				\
552ad3d6c72SMatthew Wilcox 	.xa_alloc = NULL,				\
553ad3d6c72SMatthew Wilcox 	.xa_update = NULL				\
554ad3d6c72SMatthew Wilcox }
555ad3d6c72SMatthew Wilcox 
556ad3d6c72SMatthew Wilcox /**
557ad3d6c72SMatthew Wilcox  * XA_STATE() - Declare an XArray operation state.
558ad3d6c72SMatthew Wilcox  * @name: Name of this operation state (usually xas).
559ad3d6c72SMatthew Wilcox  * @array: Array to operate on.
560ad3d6c72SMatthew Wilcox  * @index: Initial index of interest.
561ad3d6c72SMatthew Wilcox  *
562ad3d6c72SMatthew Wilcox  * Declare and initialise an xa_state on the stack.
563ad3d6c72SMatthew Wilcox  */
564ad3d6c72SMatthew Wilcox #define XA_STATE(name, array, index)				\
565ad3d6c72SMatthew Wilcox 	struct xa_state name = __XA_STATE(array, index, 0, 0)
566ad3d6c72SMatthew Wilcox 
567ad3d6c72SMatthew Wilcox /**
568ad3d6c72SMatthew Wilcox  * XA_STATE_ORDER() - Declare an XArray operation state.
569ad3d6c72SMatthew Wilcox  * @name: Name of this operation state (usually xas).
570ad3d6c72SMatthew Wilcox  * @array: Array to operate on.
571ad3d6c72SMatthew Wilcox  * @index: Initial index of interest.
572ad3d6c72SMatthew Wilcox  * @order: Order of entry.
573ad3d6c72SMatthew Wilcox  *
574ad3d6c72SMatthew Wilcox  * Declare and initialise an xa_state on the stack.  This variant of
575ad3d6c72SMatthew Wilcox  * XA_STATE() allows you to specify the 'order' of the element you
576ad3d6c72SMatthew Wilcox  * want to operate on.`
577ad3d6c72SMatthew Wilcox  */
578ad3d6c72SMatthew Wilcox #define XA_STATE_ORDER(name, array, index, order)		\
579ad3d6c72SMatthew Wilcox 	struct xa_state name = __XA_STATE(array,		\
580ad3d6c72SMatthew Wilcox 			(index >> order) << order,		\
581ad3d6c72SMatthew Wilcox 			order - (order % XA_CHUNK_SHIFT),	\
582ad3d6c72SMatthew Wilcox 			(1U << (order % XA_CHUNK_SHIFT)) - 1)
583ad3d6c72SMatthew Wilcox 
584ad3d6c72SMatthew Wilcox #define xas_marked(xas, mark)	xa_marked((xas)->xa, (mark))
585ad3d6c72SMatthew Wilcox #define xas_trylock(xas)	xa_trylock((xas)->xa)
586ad3d6c72SMatthew Wilcox #define xas_lock(xas)		xa_lock((xas)->xa)
587ad3d6c72SMatthew Wilcox #define xas_unlock(xas)		xa_unlock((xas)->xa)
588ad3d6c72SMatthew Wilcox #define xas_lock_bh(xas)	xa_lock_bh((xas)->xa)
589ad3d6c72SMatthew Wilcox #define xas_unlock_bh(xas)	xa_unlock_bh((xas)->xa)
590ad3d6c72SMatthew Wilcox #define xas_lock_irq(xas)	xa_lock_irq((xas)->xa)
591ad3d6c72SMatthew Wilcox #define xas_unlock_irq(xas)	xa_unlock_irq((xas)->xa)
592ad3d6c72SMatthew Wilcox #define xas_lock_irqsave(xas, flags) \
593ad3d6c72SMatthew Wilcox 				xa_lock_irqsave((xas)->xa, flags)
594ad3d6c72SMatthew Wilcox #define xas_unlock_irqrestore(xas, flags) \
595ad3d6c72SMatthew Wilcox 				xa_unlock_irqrestore((xas)->xa, flags)
596ad3d6c72SMatthew Wilcox 
597ad3d6c72SMatthew Wilcox /**
598ad3d6c72SMatthew Wilcox  * xas_error() - Return an errno stored in the xa_state.
599ad3d6c72SMatthew Wilcox  * @xas: XArray operation state.
600ad3d6c72SMatthew Wilcox  *
601ad3d6c72SMatthew Wilcox  * Return: 0 if no error has been noted.  A negative errno if one has.
602ad3d6c72SMatthew Wilcox  */
603ad3d6c72SMatthew Wilcox static inline int xas_error(const struct xa_state *xas)
604ad3d6c72SMatthew Wilcox {
605ad3d6c72SMatthew Wilcox 	return xa_err(xas->xa_node);
606ad3d6c72SMatthew Wilcox }
607ad3d6c72SMatthew Wilcox 
608ad3d6c72SMatthew Wilcox /**
609ad3d6c72SMatthew Wilcox  * xas_set_err() - Note an error in the xa_state.
610ad3d6c72SMatthew Wilcox  * @xas: XArray operation state.
611ad3d6c72SMatthew Wilcox  * @err: Negative error number.
612ad3d6c72SMatthew Wilcox  *
613ad3d6c72SMatthew Wilcox  * Only call this function with a negative @err; zero or positive errors
614ad3d6c72SMatthew Wilcox  * will probably not behave the way you think they should.  If you want
615ad3d6c72SMatthew Wilcox  * to clear the error from an xa_state, use xas_reset().
616ad3d6c72SMatthew Wilcox  */
617ad3d6c72SMatthew Wilcox static inline void xas_set_err(struct xa_state *xas, long err)
618ad3d6c72SMatthew Wilcox {
619ad3d6c72SMatthew Wilcox 	xas->xa_node = XA_ERROR(err);
620ad3d6c72SMatthew Wilcox }
621ad3d6c72SMatthew Wilcox 
622ad3d6c72SMatthew Wilcox /**
623ad3d6c72SMatthew Wilcox  * xas_invalid() - Is the xas in a retry or error state?
624ad3d6c72SMatthew Wilcox  * @xas: XArray operation state.
625ad3d6c72SMatthew Wilcox  *
626ad3d6c72SMatthew Wilcox  * Return: %true if the xas cannot be used for operations.
627ad3d6c72SMatthew Wilcox  */
628ad3d6c72SMatthew Wilcox static inline bool xas_invalid(const struct xa_state *xas)
629ad3d6c72SMatthew Wilcox {
630ad3d6c72SMatthew Wilcox 	return (unsigned long)xas->xa_node & 3;
631ad3d6c72SMatthew Wilcox }
632ad3d6c72SMatthew Wilcox 
633ad3d6c72SMatthew Wilcox /**
634ad3d6c72SMatthew Wilcox  * xas_valid() - Is the xas a valid cursor into the array?
635ad3d6c72SMatthew Wilcox  * @xas: XArray operation state.
636ad3d6c72SMatthew Wilcox  *
637ad3d6c72SMatthew Wilcox  * Return: %true if the xas can be used for operations.
638ad3d6c72SMatthew Wilcox  */
639ad3d6c72SMatthew Wilcox static inline bool xas_valid(const struct xa_state *xas)
640ad3d6c72SMatthew Wilcox {
641ad3d6c72SMatthew Wilcox 	return !xas_invalid(xas);
642ad3d6c72SMatthew Wilcox }
643ad3d6c72SMatthew Wilcox 
644*9b89a035SMatthew Wilcox /* True if the pointer is something other than a node */
645*9b89a035SMatthew Wilcox static inline bool xas_not_node(struct xa_node *node)
646*9b89a035SMatthew Wilcox {
647*9b89a035SMatthew Wilcox 	return ((unsigned long)node & 3) || !node;
648*9b89a035SMatthew Wilcox }
649*9b89a035SMatthew Wilcox 
650ad3d6c72SMatthew Wilcox /**
651ad3d6c72SMatthew Wilcox  * xas_reset() - Reset an XArray operation state.
652ad3d6c72SMatthew Wilcox  * @xas: XArray operation state.
653ad3d6c72SMatthew Wilcox  *
654ad3d6c72SMatthew Wilcox  * Resets the error or walk state of the @xas so future walks of the
655ad3d6c72SMatthew Wilcox  * array will start from the root.  Use this if you have dropped the
656ad3d6c72SMatthew Wilcox  * xarray lock and want to reuse the xa_state.
657ad3d6c72SMatthew Wilcox  *
658ad3d6c72SMatthew Wilcox  * Context: Any context.
659ad3d6c72SMatthew Wilcox  */
660ad3d6c72SMatthew Wilcox static inline void xas_reset(struct xa_state *xas)
661ad3d6c72SMatthew Wilcox {
662ad3d6c72SMatthew Wilcox 	xas->xa_node = XAS_RESTART;
663ad3d6c72SMatthew Wilcox }
664ad3d6c72SMatthew Wilcox 
665ad3d6c72SMatthew Wilcox /**
666ad3d6c72SMatthew Wilcox  * xas_retry() - Retry the operation if appropriate.
667ad3d6c72SMatthew Wilcox  * @xas: XArray operation state.
668ad3d6c72SMatthew Wilcox  * @entry: Entry from xarray.
669ad3d6c72SMatthew Wilcox  *
670ad3d6c72SMatthew Wilcox  * The advanced functions may sometimes return an internal entry, such as
671ad3d6c72SMatthew Wilcox  * a retry entry or a zero entry.  This function sets up the @xas to restart
672ad3d6c72SMatthew Wilcox  * the walk from the head of the array if needed.
673ad3d6c72SMatthew Wilcox  *
674ad3d6c72SMatthew Wilcox  * Context: Any context.
675ad3d6c72SMatthew Wilcox  * Return: true if the operation needs to be retried.
676ad3d6c72SMatthew Wilcox  */
677ad3d6c72SMatthew Wilcox static inline bool xas_retry(struct xa_state *xas, const void *entry)
678ad3d6c72SMatthew Wilcox {
679ad3d6c72SMatthew Wilcox 	if (!xa_is_retry(entry))
680ad3d6c72SMatthew Wilcox 		return false;
681ad3d6c72SMatthew Wilcox 	xas_reset(xas);
682ad3d6c72SMatthew Wilcox 	return true;
683ad3d6c72SMatthew Wilcox }
684ad3d6c72SMatthew Wilcox 
685ad3d6c72SMatthew Wilcox void *xas_load(struct xa_state *);
686ad3d6c72SMatthew Wilcox 
687*9b89a035SMatthew Wilcox bool xas_get_mark(const struct xa_state *, xa_mark_t);
688*9b89a035SMatthew Wilcox void xas_set_mark(const struct xa_state *, xa_mark_t);
689*9b89a035SMatthew Wilcox void xas_clear_mark(const struct xa_state *, xa_mark_t);
690*9b89a035SMatthew Wilcox 
691ad3d6c72SMatthew Wilcox /**
692ad3d6c72SMatthew Wilcox  * xas_reload() - Refetch an entry from the xarray.
693ad3d6c72SMatthew Wilcox  * @xas: XArray operation state.
694ad3d6c72SMatthew Wilcox  *
695ad3d6c72SMatthew Wilcox  * Use this function to check that a previously loaded entry still has
696ad3d6c72SMatthew Wilcox  * the same value.  This is useful for the lockless pagecache lookup where
697ad3d6c72SMatthew Wilcox  * we walk the array with only the RCU lock to protect us, lock the page,
698ad3d6c72SMatthew Wilcox  * then check that the page hasn't moved since we looked it up.
699ad3d6c72SMatthew Wilcox  *
700ad3d6c72SMatthew Wilcox  * The caller guarantees that @xas is still valid.  If it may be in an
701ad3d6c72SMatthew Wilcox  * error or restart state, call xas_load() instead.
702ad3d6c72SMatthew Wilcox  *
703ad3d6c72SMatthew Wilcox  * Return: The entry at this location in the xarray.
704ad3d6c72SMatthew Wilcox  */
705ad3d6c72SMatthew Wilcox static inline void *xas_reload(struct xa_state *xas)
706ad3d6c72SMatthew Wilcox {
707ad3d6c72SMatthew Wilcox 	struct xa_node *node = xas->xa_node;
708ad3d6c72SMatthew Wilcox 
709ad3d6c72SMatthew Wilcox 	if (node)
710ad3d6c72SMatthew Wilcox 		return xa_entry(xas->xa, node, xas->xa_offset);
711ad3d6c72SMatthew Wilcox 	return xa_head(xas->xa);
712ad3d6c72SMatthew Wilcox }
713ad3d6c72SMatthew Wilcox 
714f6bb2a2cSMatthew Wilcox #endif /* _LINUX_XARRAY_H */
715