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
1222f56b8eSMatthew Wilcox (Oracle) #include <linux/bitmap.h>
133159f943SMatthew Wilcox #include <linux/bug.h>
14f8d5d0ccSMatthew Wilcox #include <linux/compiler.h>
15d0aea4dcSAndy Shevchenko #include <linux/err.h>
169b89a035SMatthew Wilcox #include <linux/gfp.h>
17f8d5d0ccSMatthew Wilcox #include <linux/kconfig.h>
18d0aea4dcSAndy Shevchenko #include <linux/limits.h>
19d0aea4dcSAndy Shevchenko #include <linux/lockdep.h>
20ad3d6c72SMatthew Wilcox #include <linux/rcupdate.h>
211dd685c4SMatthew Wilcox (Oracle) #include <linux/sched/mm.h>
22f6bb2a2cSMatthew Wilcox #include <linux/spinlock.h>
233159f943SMatthew Wilcox #include <linux/types.h>
243159f943SMatthew Wilcox
25d0aea4dcSAndy Shevchenko struct list_lru;
26d0aea4dcSAndy Shevchenko
273159f943SMatthew Wilcox /*
283159f943SMatthew Wilcox * The bottom two bits of the entry determine how the XArray interprets
293159f943SMatthew Wilcox * the contents:
303159f943SMatthew Wilcox *
313159f943SMatthew Wilcox * 00: Pointer entry
323159f943SMatthew Wilcox * 10: Internal entry
333159f943SMatthew Wilcox * x1: Value entry or tagged pointer
343159f943SMatthew Wilcox *
353159f943SMatthew Wilcox * Attempting to store internal entries in the XArray is a bug.
3602c02bf1SMatthew Wilcox *
3702c02bf1SMatthew Wilcox * Most internal entries are pointers to the next node in the tree.
3802c02bf1SMatthew Wilcox * The following internal entries have a special meaning:
3902c02bf1SMatthew Wilcox *
4002c02bf1SMatthew Wilcox * 0-62: Sibling entries
4124a448b1SChengguang Xu * 256: Retry entry
4224a448b1SChengguang Xu * 257: Zero entry
43ad3d6c72SMatthew Wilcox *
44ad3d6c72SMatthew Wilcox * Errors are also represented as internal entries, but use the negative
45ad3d6c72SMatthew Wilcox * space (-4094 to -2). They're never stored in the slots array; only
46ad3d6c72SMatthew Wilcox * returned by the normal API.
473159f943SMatthew Wilcox */
483159f943SMatthew Wilcox
493159f943SMatthew Wilcox #define BITS_PER_XA_VALUE (BITS_PER_LONG - 1)
503159f943SMatthew Wilcox
513159f943SMatthew Wilcox /**
523159f943SMatthew Wilcox * xa_mk_value() - Create an XArray entry from an integer.
533159f943SMatthew Wilcox * @v: Value to store in XArray.
543159f943SMatthew Wilcox *
553159f943SMatthew Wilcox * Context: Any context.
563159f943SMatthew Wilcox * Return: An entry suitable for storing in the XArray.
573159f943SMatthew Wilcox */
xa_mk_value(unsigned long v)583159f943SMatthew Wilcox static inline void *xa_mk_value(unsigned long v)
593159f943SMatthew Wilcox {
603159f943SMatthew Wilcox WARN_ON((long)v < 0);
613159f943SMatthew Wilcox return (void *)((v << 1) | 1);
623159f943SMatthew Wilcox }
633159f943SMatthew Wilcox
643159f943SMatthew Wilcox /**
653159f943SMatthew Wilcox * xa_to_value() - Get value stored in an XArray entry.
663159f943SMatthew Wilcox * @entry: XArray entry.
673159f943SMatthew Wilcox *
683159f943SMatthew Wilcox * Context: Any context.
693159f943SMatthew Wilcox * Return: The value stored in the XArray entry.
703159f943SMatthew Wilcox */
xa_to_value(const void * entry)713159f943SMatthew Wilcox static inline unsigned long xa_to_value(const void *entry)
723159f943SMatthew Wilcox {
733159f943SMatthew Wilcox return (unsigned long)entry >> 1;
743159f943SMatthew Wilcox }
753159f943SMatthew Wilcox
763159f943SMatthew Wilcox /**
773159f943SMatthew Wilcox * xa_is_value() - Determine if an entry is a value.
783159f943SMatthew Wilcox * @entry: XArray entry.
793159f943SMatthew Wilcox *
803159f943SMatthew Wilcox * Context: Any context.
813159f943SMatthew Wilcox * Return: True if the entry is a value, false if it is a pointer.
823159f943SMatthew Wilcox */
xa_is_value(const void * entry)833159f943SMatthew Wilcox static inline bool xa_is_value(const void *entry)
843159f943SMatthew Wilcox {
853159f943SMatthew Wilcox return (unsigned long)entry & 1;
863159f943SMatthew Wilcox }
873159f943SMatthew Wilcox
883159f943SMatthew Wilcox /**
893159f943SMatthew Wilcox * xa_tag_pointer() - Create an XArray entry for a tagged pointer.
903159f943SMatthew Wilcox * @p: Plain pointer.
913159f943SMatthew Wilcox * @tag: Tag value (0, 1 or 3).
923159f943SMatthew Wilcox *
933159f943SMatthew Wilcox * If the user of the XArray prefers, they can tag their pointers instead
943159f943SMatthew Wilcox * of storing value entries. Three tags are available (0, 1 and 3).
953159f943SMatthew Wilcox * These are distinct from the xa_mark_t as they are not replicated up
963159f943SMatthew Wilcox * through the array and cannot be searched for.
973159f943SMatthew Wilcox *
983159f943SMatthew Wilcox * Context: Any context.
993159f943SMatthew Wilcox * Return: An XArray entry.
1003159f943SMatthew Wilcox */
xa_tag_pointer(void * p,unsigned long tag)1013159f943SMatthew Wilcox static inline void *xa_tag_pointer(void *p, unsigned long tag)
1023159f943SMatthew Wilcox {
1033159f943SMatthew Wilcox return (void *)((unsigned long)p | tag);
1043159f943SMatthew Wilcox }
1053159f943SMatthew Wilcox
1063159f943SMatthew Wilcox /**
1073159f943SMatthew Wilcox * xa_untag_pointer() - Turn an XArray entry into a plain pointer.
1083159f943SMatthew Wilcox * @entry: XArray entry.
1093159f943SMatthew Wilcox *
1103159f943SMatthew Wilcox * If you have stored a tagged pointer in the XArray, call this function
1113159f943SMatthew Wilcox * to get the untagged version of the pointer.
1123159f943SMatthew Wilcox *
1133159f943SMatthew Wilcox * Context: Any context.
1143159f943SMatthew Wilcox * Return: A pointer.
1153159f943SMatthew Wilcox */
xa_untag_pointer(void * entry)1163159f943SMatthew Wilcox static inline void *xa_untag_pointer(void *entry)
1173159f943SMatthew Wilcox {
1183159f943SMatthew Wilcox return (void *)((unsigned long)entry & ~3UL);
1193159f943SMatthew Wilcox }
1203159f943SMatthew Wilcox
1213159f943SMatthew Wilcox /**
1223159f943SMatthew Wilcox * xa_pointer_tag() - Get the tag stored in an XArray entry.
1233159f943SMatthew Wilcox * @entry: XArray entry.
1243159f943SMatthew Wilcox *
1253159f943SMatthew Wilcox * If you have stored a tagged pointer in the XArray, call this function
1263159f943SMatthew Wilcox * to get the tag of that pointer.
1273159f943SMatthew Wilcox *
1283159f943SMatthew Wilcox * Context: Any context.
1293159f943SMatthew Wilcox * Return: A tag.
1303159f943SMatthew Wilcox */
xa_pointer_tag(void * entry)1313159f943SMatthew Wilcox static inline unsigned int xa_pointer_tag(void *entry)
1323159f943SMatthew Wilcox {
1333159f943SMatthew Wilcox return (unsigned long)entry & 3UL;
1343159f943SMatthew Wilcox }
135f6bb2a2cSMatthew Wilcox
13602c02bf1SMatthew Wilcox /*
13702c02bf1SMatthew Wilcox * xa_mk_internal() - Create an internal entry.
13802c02bf1SMatthew Wilcox * @v: Value to turn into an internal entry.
13902c02bf1SMatthew Wilcox *
140b38f6c50SMatthew Wilcox * Internal entries are used for a number of purposes. Entries 0-255 are
141b38f6c50SMatthew Wilcox * used for sibling entries (only 0-62 are used by the current code). 256
142b38f6c50SMatthew Wilcox * is used for the retry entry. 257 is used for the reserved / zero entry.
143b38f6c50SMatthew Wilcox * Negative internal entries are used to represent errnos. Node pointers
144b38f6c50SMatthew Wilcox * are also tagged as internal entries in some situations.
145b38f6c50SMatthew Wilcox *
14602c02bf1SMatthew Wilcox * Context: Any context.
14702c02bf1SMatthew Wilcox * Return: An XArray internal entry corresponding to this value.
14802c02bf1SMatthew Wilcox */
xa_mk_internal(unsigned long v)14902c02bf1SMatthew Wilcox static inline void *xa_mk_internal(unsigned long v)
15002c02bf1SMatthew Wilcox {
15102c02bf1SMatthew Wilcox return (void *)((v << 2) | 2);
15202c02bf1SMatthew Wilcox }
15302c02bf1SMatthew Wilcox
15402c02bf1SMatthew Wilcox /*
15502c02bf1SMatthew Wilcox * xa_to_internal() - Extract the value from an internal entry.
15602c02bf1SMatthew Wilcox * @entry: XArray entry.
15702c02bf1SMatthew Wilcox *
15802c02bf1SMatthew Wilcox * Context: Any context.
15902c02bf1SMatthew Wilcox * Return: The value which was stored in the internal entry.
16002c02bf1SMatthew Wilcox */
xa_to_internal(const void * entry)16102c02bf1SMatthew Wilcox static inline unsigned long xa_to_internal(const void *entry)
16202c02bf1SMatthew Wilcox {
16302c02bf1SMatthew Wilcox return (unsigned long)entry >> 2;
16402c02bf1SMatthew Wilcox }
16502c02bf1SMatthew Wilcox
16602c02bf1SMatthew Wilcox /*
16702c02bf1SMatthew Wilcox * xa_is_internal() - Is the entry an internal entry?
16802c02bf1SMatthew Wilcox * @entry: XArray entry.
16902c02bf1SMatthew Wilcox *
17002c02bf1SMatthew Wilcox * Context: Any context.
17102c02bf1SMatthew Wilcox * Return: %true if the entry is an internal entry.
17202c02bf1SMatthew Wilcox */
xa_is_internal(const void * entry)17302c02bf1SMatthew Wilcox static inline bool xa_is_internal(const void *entry)
17402c02bf1SMatthew Wilcox {
17502c02bf1SMatthew Wilcox return ((unsigned long)entry & 3) == 2;
17602c02bf1SMatthew Wilcox }
17702c02bf1SMatthew Wilcox
178b38f6c50SMatthew Wilcox #define XA_ZERO_ENTRY xa_mk_internal(257)
179b38f6c50SMatthew Wilcox
180b38f6c50SMatthew Wilcox /**
181b38f6c50SMatthew Wilcox * xa_is_zero() - Is the entry a zero entry?
182b38f6c50SMatthew Wilcox * @entry: Entry retrieved from the XArray
183b38f6c50SMatthew Wilcox *
184b38f6c50SMatthew Wilcox * The normal API will return NULL as the contents of a slot containing
185b38f6c50SMatthew Wilcox * a zero entry. You can only see zero entries by using the advanced API.
186b38f6c50SMatthew Wilcox *
187b38f6c50SMatthew Wilcox * Return: %true if the entry is a zero entry.
188b38f6c50SMatthew Wilcox */
xa_is_zero(const void * entry)189b38f6c50SMatthew Wilcox static inline bool xa_is_zero(const void *entry)
190b38f6c50SMatthew Wilcox {
191b38f6c50SMatthew Wilcox return unlikely(entry == XA_ZERO_ENTRY);
192b38f6c50SMatthew Wilcox }
193b38f6c50SMatthew Wilcox
194f8d5d0ccSMatthew Wilcox /**
195ad3d6c72SMatthew Wilcox * xa_is_err() - Report whether an XArray operation returned an error
196ad3d6c72SMatthew Wilcox * @entry: Result from calling an XArray function
197ad3d6c72SMatthew Wilcox *
198ad3d6c72SMatthew Wilcox * If an XArray operation cannot complete an operation, it will return
199ad3d6c72SMatthew Wilcox * a special value indicating an error. This function tells you
200ad3d6c72SMatthew Wilcox * whether an error occurred; xa_err() tells you which error occurred.
201ad3d6c72SMatthew Wilcox *
202ad3d6c72SMatthew Wilcox * Context: Any context.
203ad3d6c72SMatthew Wilcox * Return: %true if the entry indicates an error.
204ad3d6c72SMatthew Wilcox */
xa_is_err(const void * entry)205ad3d6c72SMatthew Wilcox static inline bool xa_is_err(const void *entry)
206ad3d6c72SMatthew Wilcox {
20776b4e529SMatthew Wilcox return unlikely(xa_is_internal(entry) &&
208edcddd4cSDan Carpenter entry >= xa_mk_internal(-MAX_ERRNO));
209ad3d6c72SMatthew Wilcox }
210ad3d6c72SMatthew Wilcox
211ad3d6c72SMatthew Wilcox /**
212ad3d6c72SMatthew Wilcox * xa_err() - Turn an XArray result into an errno.
213ad3d6c72SMatthew Wilcox * @entry: Result from calling an XArray function.
214ad3d6c72SMatthew Wilcox *
215ad3d6c72SMatthew Wilcox * If an XArray operation cannot complete an operation, it will return
216ad3d6c72SMatthew Wilcox * a special pointer value which encodes an errno. This function extracts
217ad3d6c72SMatthew Wilcox * the errno from the pointer value, or returns 0 if the pointer does not
218ad3d6c72SMatthew Wilcox * represent an errno.
219ad3d6c72SMatthew Wilcox *
220ad3d6c72SMatthew Wilcox * Context: Any context.
221ad3d6c72SMatthew Wilcox * Return: A negative errno or 0.
222ad3d6c72SMatthew Wilcox */
xa_err(void * entry)223ad3d6c72SMatthew Wilcox static inline int xa_err(void *entry)
224ad3d6c72SMatthew Wilcox {
225ad3d6c72SMatthew Wilcox /* xa_to_internal() would not do sign extension. */
226ad3d6c72SMatthew Wilcox if (xa_is_err(entry))
227ad3d6c72SMatthew Wilcox return (long)entry >> 2;
228ad3d6c72SMatthew Wilcox return 0;
229ad3d6c72SMatthew Wilcox }
230ad3d6c72SMatthew Wilcox
231a3e4d3f9SMatthew Wilcox /**
232a3e4d3f9SMatthew Wilcox * struct xa_limit - Represents a range of IDs.
233a3e4d3f9SMatthew Wilcox * @min: The lowest ID to allocate (inclusive).
234a3e4d3f9SMatthew Wilcox * @max: The maximum ID to allocate (inclusive).
235a3e4d3f9SMatthew Wilcox *
236a3e4d3f9SMatthew Wilcox * This structure is used either directly or via the XA_LIMIT() macro
237a3e4d3f9SMatthew Wilcox * to communicate the range of IDs that are valid for allocation.
238df59d0a4SMatthew Wilcox (Oracle) * Three common ranges are predefined for you:
239a3e4d3f9SMatthew Wilcox * * xa_limit_32b - [0 - UINT_MAX]
240a3e4d3f9SMatthew Wilcox * * xa_limit_31b - [0 - INT_MAX]
241df59d0a4SMatthew Wilcox (Oracle) * * xa_limit_16b - [0 - USHRT_MAX]
242a3e4d3f9SMatthew Wilcox */
243a3e4d3f9SMatthew Wilcox struct xa_limit {
244a3e4d3f9SMatthew Wilcox u32 max;
245a3e4d3f9SMatthew Wilcox u32 min;
246a3e4d3f9SMatthew Wilcox };
247a3e4d3f9SMatthew Wilcox
248a3e4d3f9SMatthew Wilcox #define XA_LIMIT(_min, _max) (struct xa_limit) { .min = _min, .max = _max }
249a3e4d3f9SMatthew Wilcox
250a3e4d3f9SMatthew Wilcox #define xa_limit_32b XA_LIMIT(0, UINT_MAX)
251a3e4d3f9SMatthew Wilcox #define xa_limit_31b XA_LIMIT(0, INT_MAX)
252df59d0a4SMatthew Wilcox (Oracle) #define xa_limit_16b XA_LIMIT(0, USHRT_MAX)
253a3e4d3f9SMatthew Wilcox
2549b89a035SMatthew Wilcox typedef unsigned __bitwise xa_mark_t;
2559b89a035SMatthew Wilcox #define XA_MARK_0 ((__force xa_mark_t)0U)
2569b89a035SMatthew Wilcox #define XA_MARK_1 ((__force xa_mark_t)1U)
2579b89a035SMatthew Wilcox #define XA_MARK_2 ((__force xa_mark_t)2U)
2589b89a035SMatthew Wilcox #define XA_PRESENT ((__force xa_mark_t)8U)
2599b89a035SMatthew Wilcox #define XA_MARK_MAX XA_MARK_2
260371c752dSMatthew Wilcox #define XA_FREE_MARK XA_MARK_0
2619b89a035SMatthew Wilcox
26258d6ea30SMatthew Wilcox enum xa_lock_type {
26358d6ea30SMatthew Wilcox XA_LOCK_IRQ = 1,
26458d6ea30SMatthew Wilcox XA_LOCK_BH = 2,
26558d6ea30SMatthew Wilcox };
26658d6ea30SMatthew Wilcox
2679b89a035SMatthew Wilcox /*
2689b89a035SMatthew Wilcox * Values for xa_flags. The radix tree stores its GFP flags in the xa_flags,
2699b89a035SMatthew Wilcox * and we remain compatible with that.
2709b89a035SMatthew Wilcox */
27158d6ea30SMatthew Wilcox #define XA_FLAGS_LOCK_IRQ ((__force gfp_t)XA_LOCK_IRQ)
27258d6ea30SMatthew Wilcox #define XA_FLAGS_LOCK_BH ((__force gfp_t)XA_LOCK_BH)
273371c752dSMatthew Wilcox #define XA_FLAGS_TRACK_FREE ((__force gfp_t)4U)
2743ccaf57aSMatthew Wilcox #define XA_FLAGS_ZERO_BUSY ((__force gfp_t)8U)
2752fa044e5SMatthew Wilcox #define XA_FLAGS_ALLOC_WRAPPED ((__force gfp_t)16U)
2767b785645SJohannes Weiner #define XA_FLAGS_ACCOUNT ((__force gfp_t)32U)
2779b89a035SMatthew Wilcox #define XA_FLAGS_MARK(mark) ((__force gfp_t)((1U << __GFP_BITS_SHIFT) << \
2789b89a035SMatthew Wilcox (__force unsigned)(mark)))
2799b89a035SMatthew Wilcox
2803ccaf57aSMatthew Wilcox /* ALLOC is for a normal 0-based alloc. ALLOC1 is for an 1-based alloc */
281371c752dSMatthew Wilcox #define XA_FLAGS_ALLOC (XA_FLAGS_TRACK_FREE | XA_FLAGS_MARK(XA_FREE_MARK))
2823ccaf57aSMatthew Wilcox #define XA_FLAGS_ALLOC1 (XA_FLAGS_TRACK_FREE | XA_FLAGS_ZERO_BUSY)
283371c752dSMatthew Wilcox
284ad3d6c72SMatthew Wilcox /**
285f8d5d0ccSMatthew Wilcox * struct xarray - The anchor of the XArray.
286f8d5d0ccSMatthew Wilcox * @xa_lock: Lock that protects the contents of the XArray.
287f8d5d0ccSMatthew Wilcox *
288f8d5d0ccSMatthew Wilcox * To use the xarray, define it statically or embed it in your data structure.
289f8d5d0ccSMatthew Wilcox * It is a very small data structure, so it does not usually make sense to
290f8d5d0ccSMatthew Wilcox * allocate it separately and keep a pointer to it in your data structure.
291f8d5d0ccSMatthew Wilcox *
292f8d5d0ccSMatthew Wilcox * You may use the xa_lock to protect your own data structures as well.
293f8d5d0ccSMatthew Wilcox */
294f8d5d0ccSMatthew Wilcox /*
295f8d5d0ccSMatthew Wilcox * If all of the entries in the array are NULL, @xa_head is a NULL pointer.
296f8d5d0ccSMatthew Wilcox * If the only non-NULL entry in the array is at index 0, @xa_head is that
297f8d5d0ccSMatthew Wilcox * entry. If any other entry in the array is non-NULL, @xa_head points
298f8d5d0ccSMatthew Wilcox * to an @xa_node.
299f8d5d0ccSMatthew Wilcox */
300f8d5d0ccSMatthew Wilcox struct xarray {
301f8d5d0ccSMatthew Wilcox spinlock_t xa_lock;
302f8d5d0ccSMatthew Wilcox /* private: The rest of the data structure is not to be used directly. */
303f8d5d0ccSMatthew Wilcox gfp_t xa_flags;
304f8d5d0ccSMatthew Wilcox void __rcu * xa_head;
305f8d5d0ccSMatthew Wilcox };
306f8d5d0ccSMatthew Wilcox
307f8d5d0ccSMatthew Wilcox #define XARRAY_INIT(name, flags) { \
308f8d5d0ccSMatthew Wilcox .xa_lock = __SPIN_LOCK_UNLOCKED(name.xa_lock), \
309f8d5d0ccSMatthew Wilcox .xa_flags = flags, \
310f8d5d0ccSMatthew Wilcox .xa_head = NULL, \
311f8d5d0ccSMatthew Wilcox }
312f8d5d0ccSMatthew Wilcox
313f8d5d0ccSMatthew Wilcox /**
314f8d5d0ccSMatthew Wilcox * DEFINE_XARRAY_FLAGS() - Define an XArray with custom flags.
315f8d5d0ccSMatthew Wilcox * @name: A string that names your XArray.
316f8d5d0ccSMatthew Wilcox * @flags: XA_FLAG values.
317f8d5d0ccSMatthew Wilcox *
318f8d5d0ccSMatthew Wilcox * This is intended for file scope definitions of XArrays. It declares
319f8d5d0ccSMatthew Wilcox * and initialises an empty XArray with the chosen name and flags. It is
320f8d5d0ccSMatthew Wilcox * equivalent to calling xa_init_flags() on the array, but it does the
321f8d5d0ccSMatthew Wilcox * initialisation at compiletime instead of runtime.
322f8d5d0ccSMatthew Wilcox */
323f8d5d0ccSMatthew Wilcox #define DEFINE_XARRAY_FLAGS(name, flags) \
324f8d5d0ccSMatthew Wilcox struct xarray name = XARRAY_INIT(name, flags)
325f8d5d0ccSMatthew Wilcox
326f8d5d0ccSMatthew Wilcox /**
327f8d5d0ccSMatthew Wilcox * DEFINE_XARRAY() - Define an XArray.
328f8d5d0ccSMatthew Wilcox * @name: A string that names your XArray.
329f8d5d0ccSMatthew Wilcox *
330f8d5d0ccSMatthew Wilcox * This is intended for file scope definitions of XArrays. It declares
331f8d5d0ccSMatthew Wilcox * and initialises an empty XArray with the chosen name. It is equivalent
332f8d5d0ccSMatthew Wilcox * to calling xa_init() on the array, but it does the initialisation at
333f8d5d0ccSMatthew Wilcox * compiletime instead of runtime.
334f8d5d0ccSMatthew Wilcox */
335f8d5d0ccSMatthew Wilcox #define DEFINE_XARRAY(name) DEFINE_XARRAY_FLAGS(name, 0)
336f8d5d0ccSMatthew Wilcox
337371c752dSMatthew Wilcox /**
3383ccaf57aSMatthew Wilcox * DEFINE_XARRAY_ALLOC() - Define an XArray which allocates IDs starting at 0.
339371c752dSMatthew Wilcox * @name: A string that names your XArray.
340371c752dSMatthew Wilcox *
341371c752dSMatthew Wilcox * This is intended for file scope definitions of allocating XArrays.
342371c752dSMatthew Wilcox * See also DEFINE_XARRAY().
343371c752dSMatthew Wilcox */
344371c752dSMatthew Wilcox #define DEFINE_XARRAY_ALLOC(name) DEFINE_XARRAY_FLAGS(name, XA_FLAGS_ALLOC)
345371c752dSMatthew Wilcox
3463ccaf57aSMatthew Wilcox /**
3473ccaf57aSMatthew Wilcox * DEFINE_XARRAY_ALLOC1() - Define an XArray which allocates IDs starting at 1.
3483ccaf57aSMatthew Wilcox * @name: A string that names your XArray.
3493ccaf57aSMatthew Wilcox *
3503ccaf57aSMatthew Wilcox * This is intended for file scope definitions of allocating XArrays.
3513ccaf57aSMatthew Wilcox * See also DEFINE_XARRAY().
3523ccaf57aSMatthew Wilcox */
3533ccaf57aSMatthew Wilcox #define DEFINE_XARRAY_ALLOC1(name) DEFINE_XARRAY_FLAGS(name, XA_FLAGS_ALLOC1)
3543ccaf57aSMatthew Wilcox
355ad3d6c72SMatthew Wilcox void *xa_load(struct xarray *, unsigned long index);
35658d6ea30SMatthew Wilcox void *xa_store(struct xarray *, unsigned long index, void *entry, gfp_t);
3579c16bb88SMatthew Wilcox void *xa_erase(struct xarray *, unsigned long index);
3580e9446c3SMatthew Wilcox void *xa_store_range(struct xarray *, unsigned long first, unsigned long last,
3590e9446c3SMatthew Wilcox void *entry, gfp_t);
3609b89a035SMatthew Wilcox bool xa_get_mark(struct xarray *, unsigned long index, xa_mark_t);
3619b89a035SMatthew Wilcox void xa_set_mark(struct xarray *, unsigned long index, xa_mark_t);
3629b89a035SMatthew Wilcox void xa_clear_mark(struct xarray *, unsigned long index, xa_mark_t);
363b803b428SMatthew Wilcox void *xa_find(struct xarray *xa, unsigned long *index,
364b803b428SMatthew Wilcox unsigned long max, xa_mark_t) __attribute__((nonnull(2)));
365b803b428SMatthew Wilcox void *xa_find_after(struct xarray *xa, unsigned long *index,
366b803b428SMatthew Wilcox unsigned long max, xa_mark_t) __attribute__((nonnull(2)));
36780a0a1a9SMatthew Wilcox unsigned int xa_extract(struct xarray *, void **dst, unsigned long start,
36880a0a1a9SMatthew Wilcox unsigned long max, unsigned int n, xa_mark_t);
369687149fcSMatthew Wilcox void xa_destroy(struct xarray *);
370f8d5d0ccSMatthew Wilcox
371f8d5d0ccSMatthew Wilcox /**
37202669b17SMatthew Wilcox * xa_init_flags() - Initialise an empty XArray with flags.
37302669b17SMatthew Wilcox * @xa: XArray.
37402669b17SMatthew Wilcox * @flags: XA_FLAG values.
37502669b17SMatthew Wilcox *
37602669b17SMatthew Wilcox * If you need to initialise an XArray with special flags (eg you need
37702669b17SMatthew Wilcox * to take the lock from interrupt context), use this function instead
37802669b17SMatthew Wilcox * of xa_init().
37902669b17SMatthew Wilcox *
38002669b17SMatthew Wilcox * Context: Any context.
38102669b17SMatthew Wilcox */
xa_init_flags(struct xarray * xa,gfp_t flags)38202669b17SMatthew Wilcox static inline void xa_init_flags(struct xarray *xa, gfp_t flags)
38302669b17SMatthew Wilcox {
38402669b17SMatthew Wilcox spin_lock_init(&xa->xa_lock);
38502669b17SMatthew Wilcox xa->xa_flags = flags;
38602669b17SMatthew Wilcox xa->xa_head = NULL;
38702669b17SMatthew Wilcox }
38802669b17SMatthew Wilcox
38902669b17SMatthew Wilcox /**
390f8d5d0ccSMatthew Wilcox * xa_init() - Initialise an empty XArray.
391f8d5d0ccSMatthew Wilcox * @xa: XArray.
392f8d5d0ccSMatthew Wilcox *
393f8d5d0ccSMatthew Wilcox * An empty XArray is full of NULL entries.
394f8d5d0ccSMatthew Wilcox *
395f8d5d0ccSMatthew Wilcox * Context: Any context.
396f8d5d0ccSMatthew Wilcox */
xa_init(struct xarray * xa)397f8d5d0ccSMatthew Wilcox static inline void xa_init(struct xarray *xa)
398f8d5d0ccSMatthew Wilcox {
399f8d5d0ccSMatthew Wilcox xa_init_flags(xa, 0);
400f8d5d0ccSMatthew Wilcox }
401f8d5d0ccSMatthew Wilcox
402ad3d6c72SMatthew Wilcox /**
403ad3d6c72SMatthew Wilcox * xa_empty() - Determine if an array has any present entries.
404ad3d6c72SMatthew Wilcox * @xa: XArray.
405ad3d6c72SMatthew Wilcox *
406ad3d6c72SMatthew Wilcox * Context: Any context.
407ad3d6c72SMatthew Wilcox * Return: %true if the array contains only NULL pointers.
408ad3d6c72SMatthew Wilcox */
xa_empty(const struct xarray * xa)409ad3d6c72SMatthew Wilcox static inline bool xa_empty(const struct xarray *xa)
410ad3d6c72SMatthew Wilcox {
411ad3d6c72SMatthew Wilcox return xa->xa_head == NULL;
412ad3d6c72SMatthew Wilcox }
413ad3d6c72SMatthew Wilcox
4149b89a035SMatthew Wilcox /**
4159b89a035SMatthew Wilcox * xa_marked() - Inquire whether any entry in this array has a mark set
4169b89a035SMatthew Wilcox * @xa: Array
4179b89a035SMatthew Wilcox * @mark: Mark value
4189b89a035SMatthew Wilcox *
4199b89a035SMatthew Wilcox * Context: Any context.
4209b89a035SMatthew Wilcox * Return: %true if any entry has this mark set.
4219b89a035SMatthew Wilcox */
xa_marked(const struct xarray * xa,xa_mark_t mark)4229b89a035SMatthew Wilcox static inline bool xa_marked(const struct xarray *xa, xa_mark_t mark)
4239b89a035SMatthew Wilcox {
4249b89a035SMatthew Wilcox return xa->xa_flags & XA_FLAGS_MARK(mark);
4259b89a035SMatthew Wilcox }
4269b89a035SMatthew Wilcox
42758d6ea30SMatthew Wilcox /**
42800ed452cSMatthew Wilcox (Oracle) * xa_for_each_range() - Iterate over a portion of an XArray.
42900ed452cSMatthew Wilcox (Oracle) * @xa: XArray.
43000ed452cSMatthew Wilcox (Oracle) * @index: Index of @entry.
43100ed452cSMatthew Wilcox (Oracle) * @entry: Entry retrieved from array.
43200ed452cSMatthew Wilcox (Oracle) * @start: First index to retrieve from array.
43300ed452cSMatthew Wilcox (Oracle) * @last: Last index to retrieve from array.
43400ed452cSMatthew Wilcox (Oracle) *
43500ed452cSMatthew Wilcox (Oracle) * During the iteration, @entry will have the value of the entry stored
43600ed452cSMatthew Wilcox (Oracle) * in @xa at @index. You may modify @index during the iteration if you
43700ed452cSMatthew Wilcox (Oracle) * want to skip or reprocess indices. It is safe to modify the array
43800ed452cSMatthew Wilcox (Oracle) * during the iteration. At the end of the iteration, @entry will be set
43900ed452cSMatthew Wilcox (Oracle) * to NULL and @index will have a value less than or equal to max.
44000ed452cSMatthew Wilcox (Oracle) *
44100ed452cSMatthew Wilcox (Oracle) * xa_for_each_range() is O(n.log(n)) while xas_for_each() is O(n). You have
44200ed452cSMatthew Wilcox (Oracle) * to handle your own locking with xas_for_each(), and if you have to unlock
44300ed452cSMatthew Wilcox (Oracle) * after each iteration, it will also end up being O(n.log(n)).
44400ed452cSMatthew Wilcox (Oracle) * xa_for_each_range() will spin if it hits a retry entry; if you intend to
44500ed452cSMatthew Wilcox (Oracle) * see retry entries, you should use the xas_for_each() iterator instead.
44600ed452cSMatthew Wilcox (Oracle) * The xas_for_each() iterator will expand into more inline code than
44700ed452cSMatthew Wilcox (Oracle) * xa_for_each_range().
44800ed452cSMatthew Wilcox (Oracle) *
44900ed452cSMatthew Wilcox (Oracle) * Context: Any context. Takes and releases the RCU lock.
45000ed452cSMatthew Wilcox (Oracle) */
45100ed452cSMatthew Wilcox (Oracle) #define xa_for_each_range(xa, index, entry, start, last) \
45200ed452cSMatthew Wilcox (Oracle) for (index = start, \
45300ed452cSMatthew Wilcox (Oracle) entry = xa_find(xa, &index, last, XA_PRESENT); \
45400ed452cSMatthew Wilcox (Oracle) entry; \
45500ed452cSMatthew Wilcox (Oracle) entry = xa_find_after(xa, &index, last, XA_PRESENT))
45600ed452cSMatthew Wilcox (Oracle)
45700ed452cSMatthew Wilcox (Oracle) /**
4584a31896cSMatthew Wilcox * xa_for_each_start() - Iterate over a portion of an XArray.
459b803b428SMatthew Wilcox * @xa: XArray.
460b803b428SMatthew Wilcox * @index: Index of @entry.
4614a31896cSMatthew Wilcox * @entry: Entry retrieved from array.
4624a31896cSMatthew Wilcox * @start: First index to retrieve from array.
463b803b428SMatthew Wilcox *
4644a31896cSMatthew Wilcox * During the iteration, @entry will have the value of the entry stored
4654a31896cSMatthew Wilcox * in @xa at @index. You may modify @index during the iteration if you
4664a31896cSMatthew Wilcox * want to skip or reprocess indices. It is safe to modify the array
4674a31896cSMatthew Wilcox * during the iteration. At the end of the iteration, @entry will be set
4684a31896cSMatthew Wilcox * to NULL and @index will have a value less than or equal to max.
4694a31896cSMatthew Wilcox *
4704a31896cSMatthew Wilcox * xa_for_each_start() is O(n.log(n)) while xas_for_each() is O(n). You have
4714a31896cSMatthew Wilcox * to handle your own locking with xas_for_each(), and if you have to unlock
4724a31896cSMatthew Wilcox * after each iteration, it will also end up being O(n.log(n)).
4734a31896cSMatthew Wilcox * xa_for_each_start() will spin if it hits a retry entry; if you intend to
4744a31896cSMatthew Wilcox * see retry entries, you should use the xas_for_each() iterator instead.
4754a31896cSMatthew Wilcox * The xas_for_each() iterator will expand into more inline code than
4764a31896cSMatthew Wilcox * xa_for_each_start().
4774a31896cSMatthew Wilcox *
4784a31896cSMatthew Wilcox * Context: Any context. Takes and releases the RCU lock.
4794a31896cSMatthew Wilcox */
4804a31896cSMatthew Wilcox #define xa_for_each_start(xa, index, entry, start) \
48100ed452cSMatthew Wilcox (Oracle) xa_for_each_range(xa, index, entry, start, ULONG_MAX)
4824a31896cSMatthew Wilcox
4834a31896cSMatthew Wilcox /**
4844a31896cSMatthew Wilcox * xa_for_each() - Iterate over present entries in an XArray.
4854a31896cSMatthew Wilcox * @xa: XArray.
4864a31896cSMatthew Wilcox * @index: Index of @entry.
4874a31896cSMatthew Wilcox * @entry: Entry retrieved from array.
4884a31896cSMatthew Wilcox *
4894a31896cSMatthew Wilcox * During the iteration, @entry will have the value of the entry stored
4904a31896cSMatthew Wilcox * in @xa at @index. You may modify @index during the iteration if you want
4914a31896cSMatthew Wilcox * to skip or reprocess indices. It is safe to modify the array during the
4924a31896cSMatthew Wilcox * iteration. At the end of the iteration, @entry will be set to NULL and
4934a31896cSMatthew Wilcox * @index will have a value less than or equal to max.
494b803b428SMatthew Wilcox *
495b803b428SMatthew Wilcox * xa_for_each() is O(n.log(n)) while xas_for_each() is O(n). You have
496b803b428SMatthew Wilcox * to handle your own locking with xas_for_each(), and if you have to unlock
497b803b428SMatthew Wilcox * after each iteration, it will also end up being O(n.log(n)). xa_for_each()
498b803b428SMatthew Wilcox * will spin if it hits a retry entry; if you intend to see retry entries,
499b803b428SMatthew Wilcox * you should use the xas_for_each() iterator instead. The xas_for_each()
500b803b428SMatthew Wilcox * iterator will expand into more inline code than xa_for_each().
501b803b428SMatthew Wilcox *
502b803b428SMatthew Wilcox * Context: Any context. Takes and releases the RCU lock.
503b803b428SMatthew Wilcox */
5044a31896cSMatthew Wilcox #define xa_for_each(xa, index, entry) \
5054a31896cSMatthew Wilcox xa_for_each_start(xa, index, entry, 0)
5064a31896cSMatthew Wilcox
5074a31896cSMatthew Wilcox /**
5084a31896cSMatthew Wilcox * xa_for_each_marked() - Iterate over marked entries in an XArray.
5094a31896cSMatthew Wilcox * @xa: XArray.
5104a31896cSMatthew Wilcox * @index: Index of @entry.
5114a31896cSMatthew Wilcox * @entry: Entry retrieved from array.
5124a31896cSMatthew Wilcox * @filter: Selection criterion.
5134a31896cSMatthew Wilcox *
5144a31896cSMatthew Wilcox * During the iteration, @entry will have the value of the entry stored
5154a31896cSMatthew Wilcox * in @xa at @index. The iteration will skip all entries in the array
5164a31896cSMatthew Wilcox * which do not match @filter. You may modify @index during the iteration
5174a31896cSMatthew Wilcox * if you want to skip or reprocess indices. It is safe to modify the array
5184a31896cSMatthew Wilcox * during the iteration. At the end of the iteration, @entry will be set to
5194a31896cSMatthew Wilcox * NULL and @index will have a value less than or equal to max.
5204a31896cSMatthew Wilcox *
5214a31896cSMatthew Wilcox * xa_for_each_marked() is O(n.log(n)) while xas_for_each_marked() is O(n).
5224a31896cSMatthew Wilcox * You have to handle your own locking with xas_for_each(), and if you have
5234a31896cSMatthew Wilcox * to unlock after each iteration, it will also end up being O(n.log(n)).
5244a31896cSMatthew Wilcox * xa_for_each_marked() will spin if it hits a retry entry; if you intend to
5254a31896cSMatthew Wilcox * see retry entries, you should use the xas_for_each_marked() iterator
5264a31896cSMatthew Wilcox * instead. The xas_for_each_marked() iterator will expand into more inline
5274a31896cSMatthew Wilcox * code than xa_for_each_marked().
5284a31896cSMatthew Wilcox *
5294a31896cSMatthew Wilcox * Context: Any context. Takes and releases the RCU lock.
5304a31896cSMatthew Wilcox */
5314a31896cSMatthew Wilcox #define xa_for_each_marked(xa, index, entry, filter) \
5324a31896cSMatthew Wilcox for (index = 0, entry = xa_find(xa, &index, ULONG_MAX, filter); \
5334a31896cSMatthew Wilcox entry; entry = xa_find_after(xa, &index, ULONG_MAX, filter))
534b803b428SMatthew Wilcox
535f6bb2a2cSMatthew Wilcox #define xa_trylock(xa) spin_trylock(&(xa)->xa_lock)
536f6bb2a2cSMatthew Wilcox #define xa_lock(xa) spin_lock(&(xa)->xa_lock)
537f6bb2a2cSMatthew Wilcox #define xa_unlock(xa) spin_unlock(&(xa)->xa_lock)
538f6bb2a2cSMatthew Wilcox #define xa_lock_bh(xa) spin_lock_bh(&(xa)->xa_lock)
539f6bb2a2cSMatthew Wilcox #define xa_unlock_bh(xa) spin_unlock_bh(&(xa)->xa_lock)
540f6bb2a2cSMatthew Wilcox #define xa_lock_irq(xa) spin_lock_irq(&(xa)->xa_lock)
541f6bb2a2cSMatthew Wilcox #define xa_unlock_irq(xa) spin_unlock_irq(&(xa)->xa_lock)
542f6bb2a2cSMatthew Wilcox #define xa_lock_irqsave(xa, flags) \
543f6bb2a2cSMatthew Wilcox spin_lock_irqsave(&(xa)->xa_lock, flags)
544f6bb2a2cSMatthew Wilcox #define xa_unlock_irqrestore(xa, flags) \
545f6bb2a2cSMatthew Wilcox spin_unlock_irqrestore(&(xa)->xa_lock, flags)
54682a95849SMatthew Wilcox (Oracle) #define xa_lock_nested(xa, subclass) \
54782a95849SMatthew Wilcox (Oracle) spin_lock_nested(&(xa)->xa_lock, subclass)
54882a95849SMatthew Wilcox (Oracle) #define xa_lock_bh_nested(xa, subclass) \
54982a95849SMatthew Wilcox (Oracle) spin_lock_bh_nested(&(xa)->xa_lock, subclass)
55082a95849SMatthew Wilcox (Oracle) #define xa_lock_irq_nested(xa, subclass) \
55182a95849SMatthew Wilcox (Oracle) spin_lock_irq_nested(&(xa)->xa_lock, subclass)
55282a95849SMatthew Wilcox (Oracle) #define xa_lock_irqsave_nested(xa, flags, subclass) \
55382a95849SMatthew Wilcox (Oracle) spin_lock_irqsave_nested(&(xa)->xa_lock, flags, subclass)
554f6bb2a2cSMatthew Wilcox
5559b89a035SMatthew Wilcox /*
55658d6ea30SMatthew Wilcox * Versions of the normal API which require the caller to hold the
55758d6ea30SMatthew Wilcox * xa_lock. If the GFP flags allow it, they will drop the lock to
55858d6ea30SMatthew Wilcox * allocate memory, then reacquire it afterwards. These functions
55958d6ea30SMatthew Wilcox * may also re-enable interrupts if the XArray flags indicate the
56058d6ea30SMatthew Wilcox * locking should be interrupt safe.
5619b89a035SMatthew Wilcox */
56258d6ea30SMatthew Wilcox void *__xa_erase(struct xarray *, unsigned long index);
56358d6ea30SMatthew Wilcox void *__xa_store(struct xarray *, unsigned long index, void *entry, gfp_t);
56441aec91fSMatthew Wilcox void *__xa_cmpxchg(struct xarray *, unsigned long index, void *old,
56541aec91fSMatthew Wilcox void *entry, gfp_t);
566f818b82bSMatthew Wilcox int __must_check __xa_insert(struct xarray *, unsigned long index,
567f818b82bSMatthew Wilcox void *entry, gfp_t);
568a3e4d3f9SMatthew Wilcox int __must_check __xa_alloc(struct xarray *, u32 *id, void *entry,
569a3e4d3f9SMatthew Wilcox struct xa_limit, gfp_t);
5702fa044e5SMatthew Wilcox int __must_check __xa_alloc_cyclic(struct xarray *, u32 *id, void *entry,
5712fa044e5SMatthew Wilcox struct xa_limit, u32 *next, gfp_t);
5729b89a035SMatthew Wilcox void __xa_set_mark(struct xarray *, unsigned long index, xa_mark_t);
5739b89a035SMatthew Wilcox void __xa_clear_mark(struct xarray *, unsigned long index, xa_mark_t);
5749b89a035SMatthew Wilcox
57558d6ea30SMatthew Wilcox /**
57684e5acb7SMatthew Wilcox * xa_store_bh() - Store this entry in the XArray.
57784e5acb7SMatthew Wilcox * @xa: XArray.
57884e5acb7SMatthew Wilcox * @index: Index into array.
57984e5acb7SMatthew Wilcox * @entry: New entry.
58084e5acb7SMatthew Wilcox * @gfp: Memory allocation flags.
58184e5acb7SMatthew Wilcox *
58284e5acb7SMatthew Wilcox * This function is like calling xa_store() except it disables softirqs
58384e5acb7SMatthew Wilcox * while holding the array lock.
58484e5acb7SMatthew Wilcox *
58584e5acb7SMatthew Wilcox * Context: Any context. Takes and releases the xa_lock while
58684e5acb7SMatthew Wilcox * disabling softirqs.
58701f39c1cSManfred Spraul * Return: The old entry at this index or xa_err() if an error happened.
58884e5acb7SMatthew Wilcox */
xa_store_bh(struct xarray * xa,unsigned long index,void * entry,gfp_t gfp)58984e5acb7SMatthew Wilcox static inline void *xa_store_bh(struct xarray *xa, unsigned long index,
59084e5acb7SMatthew Wilcox void *entry, gfp_t gfp)
59184e5acb7SMatthew Wilcox {
59284e5acb7SMatthew Wilcox void *curr;
59384e5acb7SMatthew Wilcox
5941dd685c4SMatthew Wilcox (Oracle) might_alloc(gfp);
59584e5acb7SMatthew Wilcox xa_lock_bh(xa);
59684e5acb7SMatthew Wilcox curr = __xa_store(xa, index, entry, gfp);
59784e5acb7SMatthew Wilcox xa_unlock_bh(xa);
59884e5acb7SMatthew Wilcox
59984e5acb7SMatthew Wilcox return curr;
60084e5acb7SMatthew Wilcox }
60184e5acb7SMatthew Wilcox
60284e5acb7SMatthew Wilcox /**
60319ba9ecfSCyrill Gorcunov * xa_store_irq() - Store this entry in the XArray.
60484e5acb7SMatthew Wilcox * @xa: XArray.
60584e5acb7SMatthew Wilcox * @index: Index into array.
60684e5acb7SMatthew Wilcox * @entry: New entry.
60784e5acb7SMatthew Wilcox * @gfp: Memory allocation flags.
60884e5acb7SMatthew Wilcox *
60984e5acb7SMatthew Wilcox * This function is like calling xa_store() except it disables interrupts
61084e5acb7SMatthew Wilcox * while holding the array lock.
61184e5acb7SMatthew Wilcox *
61284e5acb7SMatthew Wilcox * Context: Process context. Takes and releases the xa_lock while
61384e5acb7SMatthew Wilcox * disabling interrupts.
61401f39c1cSManfred Spraul * Return: The old entry at this index or xa_err() if an error happened.
61584e5acb7SMatthew Wilcox */
xa_store_irq(struct xarray * xa,unsigned long index,void * entry,gfp_t gfp)61684e5acb7SMatthew Wilcox static inline void *xa_store_irq(struct xarray *xa, unsigned long index,
61784e5acb7SMatthew Wilcox void *entry, gfp_t gfp)
61884e5acb7SMatthew Wilcox {
61984e5acb7SMatthew Wilcox void *curr;
62084e5acb7SMatthew Wilcox
6211dd685c4SMatthew Wilcox (Oracle) might_alloc(gfp);
62284e5acb7SMatthew Wilcox xa_lock_irq(xa);
62384e5acb7SMatthew Wilcox curr = __xa_store(xa, index, entry, gfp);
62484e5acb7SMatthew Wilcox xa_unlock_irq(xa);
62584e5acb7SMatthew Wilcox
62684e5acb7SMatthew Wilcox return curr;
62784e5acb7SMatthew Wilcox }
62884e5acb7SMatthew Wilcox
62984e5acb7SMatthew Wilcox /**
63058d6ea30SMatthew Wilcox * xa_erase_bh() - Erase this entry from the XArray.
63158d6ea30SMatthew Wilcox * @xa: XArray.
63258d6ea30SMatthew Wilcox * @index: Index of entry.
63358d6ea30SMatthew Wilcox *
634809ab937SMatthew Wilcox * After this function returns, loading from @index will return %NULL.
635809ab937SMatthew Wilcox * If the index is part of a multi-index entry, all indices will be erased
636809ab937SMatthew Wilcox * and none of the entries will be part of a multi-index entry.
63758d6ea30SMatthew Wilcox *
638804dfaf0SMatthew Wilcox * Context: Any context. Takes and releases the xa_lock while
63958d6ea30SMatthew Wilcox * disabling softirqs.
64058d6ea30SMatthew Wilcox * Return: The entry which used to be at this index.
64158d6ea30SMatthew Wilcox */
xa_erase_bh(struct xarray * xa,unsigned long index)64258d6ea30SMatthew Wilcox static inline void *xa_erase_bh(struct xarray *xa, unsigned long index)
64358d6ea30SMatthew Wilcox {
64458d6ea30SMatthew Wilcox void *entry;
64558d6ea30SMatthew Wilcox
64658d6ea30SMatthew Wilcox xa_lock_bh(xa);
64758d6ea30SMatthew Wilcox entry = __xa_erase(xa, index);
64858d6ea30SMatthew Wilcox xa_unlock_bh(xa);
64958d6ea30SMatthew Wilcox
65058d6ea30SMatthew Wilcox return entry;
65158d6ea30SMatthew Wilcox }
65258d6ea30SMatthew Wilcox
65358d6ea30SMatthew Wilcox /**
65458d6ea30SMatthew Wilcox * xa_erase_irq() - Erase this entry from the XArray.
65558d6ea30SMatthew Wilcox * @xa: XArray.
65658d6ea30SMatthew Wilcox * @index: Index of entry.
65758d6ea30SMatthew Wilcox *
658809ab937SMatthew Wilcox * After this function returns, loading from @index will return %NULL.
659809ab937SMatthew Wilcox * If the index is part of a multi-index entry, all indices will be erased
660809ab937SMatthew Wilcox * and none of the entries will be part of a multi-index entry.
66158d6ea30SMatthew Wilcox *
66258d6ea30SMatthew Wilcox * Context: Process context. Takes and releases the xa_lock while
66358d6ea30SMatthew Wilcox * disabling interrupts.
66458d6ea30SMatthew Wilcox * Return: The entry which used to be at this index.
66558d6ea30SMatthew Wilcox */
xa_erase_irq(struct xarray * xa,unsigned long index)66658d6ea30SMatthew Wilcox static inline void *xa_erase_irq(struct xarray *xa, unsigned long index)
66758d6ea30SMatthew Wilcox {
66858d6ea30SMatthew Wilcox void *entry;
66958d6ea30SMatthew Wilcox
67058d6ea30SMatthew Wilcox xa_lock_irq(xa);
67158d6ea30SMatthew Wilcox entry = __xa_erase(xa, index);
67258d6ea30SMatthew Wilcox xa_unlock_irq(xa);
67358d6ea30SMatthew Wilcox
67458d6ea30SMatthew Wilcox return entry;
67558d6ea30SMatthew Wilcox }
67658d6ea30SMatthew Wilcox
677371c752dSMatthew Wilcox /**
678c5beb07eSMatthew Wilcox * xa_cmpxchg() - Conditionally replace an entry in the XArray.
679c5beb07eSMatthew Wilcox * @xa: XArray.
680c5beb07eSMatthew Wilcox * @index: Index into array.
681c5beb07eSMatthew Wilcox * @old: Old value to test against.
682c5beb07eSMatthew Wilcox * @entry: New value to place in array.
683c5beb07eSMatthew Wilcox * @gfp: Memory allocation flags.
684c5beb07eSMatthew Wilcox *
685c5beb07eSMatthew Wilcox * If the entry at @index is the same as @old, replace it with @entry.
686c5beb07eSMatthew Wilcox * If the return value is equal to @old, then the exchange was successful.
687c5beb07eSMatthew Wilcox *
688c5beb07eSMatthew Wilcox * Context: Any context. Takes and releases the xa_lock. May sleep
689c5beb07eSMatthew Wilcox * if the @gfp flags permit.
690c5beb07eSMatthew Wilcox * Return: The old value at this index or xa_err() if an error happened.
691c5beb07eSMatthew Wilcox */
xa_cmpxchg(struct xarray * xa,unsigned long index,void * old,void * entry,gfp_t gfp)692c5beb07eSMatthew Wilcox static inline void *xa_cmpxchg(struct xarray *xa, unsigned long index,
693c5beb07eSMatthew Wilcox void *old, void *entry, gfp_t gfp)
694c5beb07eSMatthew Wilcox {
695c5beb07eSMatthew Wilcox void *curr;
696c5beb07eSMatthew Wilcox
6971dd685c4SMatthew Wilcox (Oracle) might_alloc(gfp);
698c5beb07eSMatthew Wilcox xa_lock(xa);
699c5beb07eSMatthew Wilcox curr = __xa_cmpxchg(xa, index, old, entry, gfp);
700c5beb07eSMatthew Wilcox xa_unlock(xa);
701c5beb07eSMatthew Wilcox
702c5beb07eSMatthew Wilcox return curr;
703c5beb07eSMatthew Wilcox }
704c5beb07eSMatthew Wilcox
705c5beb07eSMatthew Wilcox /**
70655f3f7eaSMatthew Wilcox * xa_cmpxchg_bh() - Conditionally replace an entry in the XArray.
70755f3f7eaSMatthew Wilcox * @xa: XArray.
70855f3f7eaSMatthew Wilcox * @index: Index into array.
70955f3f7eaSMatthew Wilcox * @old: Old value to test against.
71055f3f7eaSMatthew Wilcox * @entry: New value to place in array.
71155f3f7eaSMatthew Wilcox * @gfp: Memory allocation flags.
71255f3f7eaSMatthew Wilcox *
71355f3f7eaSMatthew Wilcox * This function is like calling xa_cmpxchg() except it disables softirqs
71455f3f7eaSMatthew Wilcox * while holding the array lock.
71555f3f7eaSMatthew Wilcox *
71655f3f7eaSMatthew Wilcox * Context: Any context. Takes and releases the xa_lock while
71755f3f7eaSMatthew Wilcox * disabling softirqs. May sleep if the @gfp flags permit.
71855f3f7eaSMatthew Wilcox * Return: The old value at this index or xa_err() if an error happened.
71955f3f7eaSMatthew Wilcox */
xa_cmpxchg_bh(struct xarray * xa,unsigned long index,void * old,void * entry,gfp_t gfp)72055f3f7eaSMatthew Wilcox static inline void *xa_cmpxchg_bh(struct xarray *xa, unsigned long index,
72155f3f7eaSMatthew Wilcox void *old, void *entry, gfp_t gfp)
72255f3f7eaSMatthew Wilcox {
72355f3f7eaSMatthew Wilcox void *curr;
72455f3f7eaSMatthew Wilcox
7251dd685c4SMatthew Wilcox (Oracle) might_alloc(gfp);
72655f3f7eaSMatthew Wilcox xa_lock_bh(xa);
72755f3f7eaSMatthew Wilcox curr = __xa_cmpxchg(xa, index, old, entry, gfp);
72855f3f7eaSMatthew Wilcox xa_unlock_bh(xa);
72955f3f7eaSMatthew Wilcox
73055f3f7eaSMatthew Wilcox return curr;
73155f3f7eaSMatthew Wilcox }
73255f3f7eaSMatthew Wilcox
73355f3f7eaSMatthew Wilcox /**
73455f3f7eaSMatthew Wilcox * xa_cmpxchg_irq() - Conditionally replace an entry in the XArray.
73555f3f7eaSMatthew Wilcox * @xa: XArray.
73655f3f7eaSMatthew Wilcox * @index: Index into array.
73755f3f7eaSMatthew Wilcox * @old: Old value to test against.
73855f3f7eaSMatthew Wilcox * @entry: New value to place in array.
73955f3f7eaSMatthew Wilcox * @gfp: Memory allocation flags.
74055f3f7eaSMatthew Wilcox *
74155f3f7eaSMatthew Wilcox * This function is like calling xa_cmpxchg() except it disables interrupts
74255f3f7eaSMatthew Wilcox * while holding the array lock.
74355f3f7eaSMatthew Wilcox *
74455f3f7eaSMatthew Wilcox * Context: Process context. Takes and releases the xa_lock while
74555f3f7eaSMatthew Wilcox * disabling interrupts. May sleep if the @gfp flags permit.
74655f3f7eaSMatthew Wilcox * Return: The old value at this index or xa_err() if an error happened.
74755f3f7eaSMatthew Wilcox */
xa_cmpxchg_irq(struct xarray * xa,unsigned long index,void * old,void * entry,gfp_t gfp)74855f3f7eaSMatthew Wilcox static inline void *xa_cmpxchg_irq(struct xarray *xa, unsigned long index,
74955f3f7eaSMatthew Wilcox void *old, void *entry, gfp_t gfp)
75055f3f7eaSMatthew Wilcox {
75155f3f7eaSMatthew Wilcox void *curr;
75255f3f7eaSMatthew Wilcox
7531dd685c4SMatthew Wilcox (Oracle) might_alloc(gfp);
75455f3f7eaSMatthew Wilcox xa_lock_irq(xa);
75555f3f7eaSMatthew Wilcox curr = __xa_cmpxchg(xa, index, old, entry, gfp);
75655f3f7eaSMatthew Wilcox xa_unlock_irq(xa);
75755f3f7eaSMatthew Wilcox
75855f3f7eaSMatthew Wilcox return curr;
75955f3f7eaSMatthew Wilcox }
76055f3f7eaSMatthew Wilcox
76155f3f7eaSMatthew Wilcox /**
762c5beb07eSMatthew Wilcox * xa_insert() - Store this entry in the XArray unless another entry is
763c5beb07eSMatthew Wilcox * already present.
764c5beb07eSMatthew Wilcox * @xa: XArray.
765c5beb07eSMatthew Wilcox * @index: Index into array.
766c5beb07eSMatthew Wilcox * @entry: New entry.
767c5beb07eSMatthew Wilcox * @gfp: Memory allocation flags.
768c5beb07eSMatthew Wilcox *
769b0606fedSMatthew Wilcox * Inserting a NULL entry will store a reserved entry (like xa_reserve())
770b0606fedSMatthew Wilcox * if no entry is present. Inserting will fail if a reserved entry is
771b0606fedSMatthew Wilcox * present, even though loading from this index will return NULL.
772c5beb07eSMatthew Wilcox *
773b0606fedSMatthew Wilcox * Context: Any context. Takes and releases the xa_lock. May sleep if
774b0606fedSMatthew Wilcox * the @gfp flags permit.
775fd9dc93eSMatthew Wilcox * Return: 0 if the store succeeded. -EBUSY if another entry was present.
776c5beb07eSMatthew Wilcox * -ENOMEM if memory could not be allocated.
777c5beb07eSMatthew Wilcox */
xa_insert(struct xarray * xa,unsigned long index,void * entry,gfp_t gfp)778f818b82bSMatthew Wilcox static inline int __must_check xa_insert(struct xarray *xa,
779f818b82bSMatthew Wilcox unsigned long index, void *entry, gfp_t gfp)
780c5beb07eSMatthew Wilcox {
781b0606fedSMatthew Wilcox int err;
782b0606fedSMatthew Wilcox
7831dd685c4SMatthew Wilcox (Oracle) might_alloc(gfp);
784b0606fedSMatthew Wilcox xa_lock(xa);
785b0606fedSMatthew Wilcox err = __xa_insert(xa, index, entry, gfp);
786b0606fedSMatthew Wilcox xa_unlock(xa);
787b0606fedSMatthew Wilcox
788b0606fedSMatthew Wilcox return err;
789b0606fedSMatthew Wilcox }
790b0606fedSMatthew Wilcox
791b0606fedSMatthew Wilcox /**
792b0606fedSMatthew Wilcox * xa_insert_bh() - Store this entry in the XArray unless another entry is
793b0606fedSMatthew Wilcox * already present.
794b0606fedSMatthew Wilcox * @xa: XArray.
795b0606fedSMatthew Wilcox * @index: Index into array.
796b0606fedSMatthew Wilcox * @entry: New entry.
797b0606fedSMatthew Wilcox * @gfp: Memory allocation flags.
798b0606fedSMatthew Wilcox *
799b0606fedSMatthew Wilcox * Inserting a NULL entry will store a reserved entry (like xa_reserve())
800b0606fedSMatthew Wilcox * if no entry is present. Inserting will fail if a reserved entry is
801b0606fedSMatthew Wilcox * present, even though loading from this index will return NULL.
802b0606fedSMatthew Wilcox *
803b0606fedSMatthew Wilcox * Context: Any context. Takes and releases the xa_lock while
804b0606fedSMatthew Wilcox * disabling softirqs. May sleep if the @gfp flags permit.
805fd9dc93eSMatthew Wilcox * Return: 0 if the store succeeded. -EBUSY if another entry was present.
806b0606fedSMatthew Wilcox * -ENOMEM if memory could not be allocated.
807b0606fedSMatthew Wilcox */
xa_insert_bh(struct xarray * xa,unsigned long index,void * entry,gfp_t gfp)808f818b82bSMatthew Wilcox static inline int __must_check xa_insert_bh(struct xarray *xa,
809f818b82bSMatthew Wilcox unsigned long index, void *entry, gfp_t gfp)
810b0606fedSMatthew Wilcox {
811b0606fedSMatthew Wilcox int err;
812b0606fedSMatthew Wilcox
8131dd685c4SMatthew Wilcox (Oracle) might_alloc(gfp);
814b0606fedSMatthew Wilcox xa_lock_bh(xa);
815b0606fedSMatthew Wilcox err = __xa_insert(xa, index, entry, gfp);
816b0606fedSMatthew Wilcox xa_unlock_bh(xa);
817b0606fedSMatthew Wilcox
818b0606fedSMatthew Wilcox return err;
819b0606fedSMatthew Wilcox }
820b0606fedSMatthew Wilcox
821b0606fedSMatthew Wilcox /**
822b0606fedSMatthew Wilcox * xa_insert_irq() - Store this entry in the XArray unless another entry is
823b0606fedSMatthew Wilcox * already present.
824b0606fedSMatthew Wilcox * @xa: XArray.
825b0606fedSMatthew Wilcox * @index: Index into array.
826b0606fedSMatthew Wilcox * @entry: New entry.
827b0606fedSMatthew Wilcox * @gfp: Memory allocation flags.
828b0606fedSMatthew Wilcox *
829b0606fedSMatthew Wilcox * Inserting a NULL entry will store a reserved entry (like xa_reserve())
830b0606fedSMatthew Wilcox * if no entry is present. Inserting will fail if a reserved entry is
831b0606fedSMatthew Wilcox * present, even though loading from this index will return NULL.
832b0606fedSMatthew Wilcox *
833b0606fedSMatthew Wilcox * Context: Process context. Takes and releases the xa_lock while
834b0606fedSMatthew Wilcox * disabling interrupts. May sleep if the @gfp flags permit.
835fd9dc93eSMatthew Wilcox * Return: 0 if the store succeeded. -EBUSY if another entry was present.
836b0606fedSMatthew Wilcox * -ENOMEM if memory could not be allocated.
837b0606fedSMatthew Wilcox */
xa_insert_irq(struct xarray * xa,unsigned long index,void * entry,gfp_t gfp)838f818b82bSMatthew Wilcox static inline int __must_check xa_insert_irq(struct xarray *xa,
839f818b82bSMatthew Wilcox unsigned long index, void *entry, gfp_t gfp)
840b0606fedSMatthew Wilcox {
841b0606fedSMatthew Wilcox int err;
842b0606fedSMatthew Wilcox
8431dd685c4SMatthew Wilcox (Oracle) might_alloc(gfp);
844b0606fedSMatthew Wilcox xa_lock_irq(xa);
845b0606fedSMatthew Wilcox err = __xa_insert(xa, index, entry, gfp);
846b0606fedSMatthew Wilcox xa_unlock_irq(xa);
847b0606fedSMatthew Wilcox
848b0606fedSMatthew Wilcox return err;
849c5beb07eSMatthew Wilcox }
850c5beb07eSMatthew Wilcox
851c5beb07eSMatthew Wilcox /**
852371c752dSMatthew Wilcox * xa_alloc() - Find somewhere to store this entry in the XArray.
853371c752dSMatthew Wilcox * @xa: XArray.
854371c752dSMatthew Wilcox * @id: Pointer to ID.
855371c752dSMatthew Wilcox * @entry: New entry.
856a3e4d3f9SMatthew Wilcox * @limit: Range of ID to allocate.
857371c752dSMatthew Wilcox * @gfp: Memory allocation flags.
858371c752dSMatthew Wilcox *
859a3e4d3f9SMatthew Wilcox * Finds an empty entry in @xa between @limit.min and @limit.max,
860a3e4d3f9SMatthew Wilcox * stores the index into the @id pointer, then stores the entry at
861a3e4d3f9SMatthew Wilcox * that index. A concurrent lookup will not see an uninitialised @id.
862371c752dSMatthew Wilcox *
863e7716c74SPhilipp Stanner * Must only be operated on an xarray initialized with flag XA_FLAGS_ALLOC set
864e7716c74SPhilipp Stanner * in xa_init_flags().
865e7716c74SPhilipp Stanner *
866a3e4d3f9SMatthew Wilcox * Context: Any context. Takes and releases the xa_lock. May sleep if
867371c752dSMatthew Wilcox * the @gfp flags permit.
868a3e4d3f9SMatthew Wilcox * Return: 0 on success, -ENOMEM if memory could not be allocated or
869a3e4d3f9SMatthew Wilcox * -EBUSY if there are no free entries in @limit.
870371c752dSMatthew Wilcox */
xa_alloc(struct xarray * xa,u32 * id,void * entry,struct xa_limit limit,gfp_t gfp)871a3e4d3f9SMatthew Wilcox static inline __must_check int xa_alloc(struct xarray *xa, u32 *id,
872a3e4d3f9SMatthew Wilcox void *entry, struct xa_limit limit, gfp_t gfp)
873371c752dSMatthew Wilcox {
874371c752dSMatthew Wilcox int err;
875371c752dSMatthew Wilcox
8761dd685c4SMatthew Wilcox (Oracle) might_alloc(gfp);
877371c752dSMatthew Wilcox xa_lock(xa);
878a3e4d3f9SMatthew Wilcox err = __xa_alloc(xa, id, entry, limit, gfp);
879371c752dSMatthew Wilcox xa_unlock(xa);
880371c752dSMatthew Wilcox
881371c752dSMatthew Wilcox return err;
882371c752dSMatthew Wilcox }
883371c752dSMatthew Wilcox
884371c752dSMatthew Wilcox /**
885371c752dSMatthew Wilcox * xa_alloc_bh() - Find somewhere to store this entry in the XArray.
886371c752dSMatthew Wilcox * @xa: XArray.
887371c752dSMatthew Wilcox * @id: Pointer to ID.
888371c752dSMatthew Wilcox * @entry: New entry.
889a3e4d3f9SMatthew Wilcox * @limit: Range of ID to allocate.
890371c752dSMatthew Wilcox * @gfp: Memory allocation flags.
891371c752dSMatthew Wilcox *
892a3e4d3f9SMatthew Wilcox * Finds an empty entry in @xa between @limit.min and @limit.max,
893a3e4d3f9SMatthew Wilcox * stores the index into the @id pointer, then stores the entry at
894a3e4d3f9SMatthew Wilcox * that index. A concurrent lookup will not see an uninitialised @id.
895371c752dSMatthew Wilcox *
896e7716c74SPhilipp Stanner * Must only be operated on an xarray initialized with flag XA_FLAGS_ALLOC set
897e7716c74SPhilipp Stanner * in xa_init_flags().
898e7716c74SPhilipp Stanner *
899804dfaf0SMatthew Wilcox * Context: Any context. Takes and releases the xa_lock while
900371c752dSMatthew Wilcox * disabling softirqs. May sleep if the @gfp flags permit.
901a3e4d3f9SMatthew Wilcox * Return: 0 on success, -ENOMEM if memory could not be allocated or
902a3e4d3f9SMatthew Wilcox * -EBUSY if there are no free entries in @limit.
903371c752dSMatthew Wilcox */
xa_alloc_bh(struct xarray * xa,u32 * id,void * entry,struct xa_limit limit,gfp_t gfp)904a3e4d3f9SMatthew Wilcox static inline int __must_check xa_alloc_bh(struct xarray *xa, u32 *id,
905a3e4d3f9SMatthew Wilcox void *entry, struct xa_limit limit, gfp_t gfp)
906371c752dSMatthew Wilcox {
907371c752dSMatthew Wilcox int err;
908371c752dSMatthew Wilcox
9091dd685c4SMatthew Wilcox (Oracle) might_alloc(gfp);
910371c752dSMatthew Wilcox xa_lock_bh(xa);
911a3e4d3f9SMatthew Wilcox err = __xa_alloc(xa, id, entry, limit, gfp);
912371c752dSMatthew Wilcox xa_unlock_bh(xa);
913371c752dSMatthew Wilcox
914371c752dSMatthew Wilcox return err;
915371c752dSMatthew Wilcox }
916371c752dSMatthew Wilcox
917371c752dSMatthew Wilcox /**
918371c752dSMatthew Wilcox * xa_alloc_irq() - Find somewhere to store this entry in the XArray.
919371c752dSMatthew Wilcox * @xa: XArray.
920371c752dSMatthew Wilcox * @id: Pointer to ID.
921371c752dSMatthew Wilcox * @entry: New entry.
922a3e4d3f9SMatthew Wilcox * @limit: Range of ID to allocate.
923371c752dSMatthew Wilcox * @gfp: Memory allocation flags.
924371c752dSMatthew Wilcox *
925a3e4d3f9SMatthew Wilcox * Finds an empty entry in @xa between @limit.min and @limit.max,
926a3e4d3f9SMatthew Wilcox * stores the index into the @id pointer, then stores the entry at
927a3e4d3f9SMatthew Wilcox * that index. A concurrent lookup will not see an uninitialised @id.
928371c752dSMatthew Wilcox *
929e7716c74SPhilipp Stanner * Must only be operated on an xarray initialized with flag XA_FLAGS_ALLOC set
930e7716c74SPhilipp Stanner * in xa_init_flags().
931e7716c74SPhilipp Stanner *
932371c752dSMatthew Wilcox * Context: Process context. Takes and releases the xa_lock while
933371c752dSMatthew Wilcox * disabling interrupts. May sleep if the @gfp flags permit.
934a3e4d3f9SMatthew Wilcox * Return: 0 on success, -ENOMEM if memory could not be allocated or
935a3e4d3f9SMatthew Wilcox * -EBUSY if there are no free entries in @limit.
936371c752dSMatthew Wilcox */
xa_alloc_irq(struct xarray * xa,u32 * id,void * entry,struct xa_limit limit,gfp_t gfp)937a3e4d3f9SMatthew Wilcox static inline int __must_check xa_alloc_irq(struct xarray *xa, u32 *id,
938a3e4d3f9SMatthew Wilcox void *entry, struct xa_limit limit, gfp_t gfp)
939371c752dSMatthew Wilcox {
940371c752dSMatthew Wilcox int err;
941371c752dSMatthew Wilcox
9421dd685c4SMatthew Wilcox (Oracle) might_alloc(gfp);
943371c752dSMatthew Wilcox xa_lock_irq(xa);
944a3e4d3f9SMatthew Wilcox err = __xa_alloc(xa, id, entry, limit, gfp);
945371c752dSMatthew Wilcox xa_unlock_irq(xa);
946371c752dSMatthew Wilcox
947371c752dSMatthew Wilcox return err;
948371c752dSMatthew Wilcox }
949371c752dSMatthew Wilcox
9504c0608f4SMatthew Wilcox /**
9512fa044e5SMatthew Wilcox * xa_alloc_cyclic() - Find somewhere to store this entry in the XArray.
9522fa044e5SMatthew Wilcox * @xa: XArray.
9532fa044e5SMatthew Wilcox * @id: Pointer to ID.
9542fa044e5SMatthew Wilcox * @entry: New entry.
9552fa044e5SMatthew Wilcox * @limit: Range of allocated ID.
9562fa044e5SMatthew Wilcox * @next: Pointer to next ID to allocate.
9572fa044e5SMatthew Wilcox * @gfp: Memory allocation flags.
9582fa044e5SMatthew Wilcox *
9592fa044e5SMatthew Wilcox * Finds an empty entry in @xa between @limit.min and @limit.max,
9602fa044e5SMatthew Wilcox * stores the index into the @id pointer, then stores the entry at
9612fa044e5SMatthew Wilcox * that index. A concurrent lookup will not see an uninitialised @id.
9622fa044e5SMatthew Wilcox * The search for an empty entry will start at @next and will wrap
9632fa044e5SMatthew Wilcox * around if necessary.
9642fa044e5SMatthew Wilcox *
965e7716c74SPhilipp Stanner * Must only be operated on an xarray initialized with flag XA_FLAGS_ALLOC set
966e7716c74SPhilipp Stanner * in xa_init_flags().
967e7716c74SPhilipp Stanner *
9682fa044e5SMatthew Wilcox * Context: Any context. Takes and releases the xa_lock. May sleep if
9692fa044e5SMatthew Wilcox * the @gfp flags permit.
9702fa044e5SMatthew Wilcox * Return: 0 if the allocation succeeded without wrapping. 1 if the
9712fa044e5SMatthew Wilcox * allocation succeeded after wrapping, -ENOMEM if memory could not be
9722fa044e5SMatthew Wilcox * allocated or -EBUSY if there are no free entries in @limit.
9732fa044e5SMatthew Wilcox */
xa_alloc_cyclic(struct xarray * xa,u32 * id,void * entry,struct xa_limit limit,u32 * next,gfp_t gfp)9742fa044e5SMatthew Wilcox static inline int xa_alloc_cyclic(struct xarray *xa, u32 *id, void *entry,
9752fa044e5SMatthew Wilcox struct xa_limit limit, u32 *next, gfp_t gfp)
9762fa044e5SMatthew Wilcox {
9772fa044e5SMatthew Wilcox int err;
9782fa044e5SMatthew Wilcox
9791dd685c4SMatthew Wilcox (Oracle) might_alloc(gfp);
9802fa044e5SMatthew Wilcox xa_lock(xa);
9812fa044e5SMatthew Wilcox err = __xa_alloc_cyclic(xa, id, entry, limit, next, gfp);
9822fa044e5SMatthew Wilcox xa_unlock(xa);
9832fa044e5SMatthew Wilcox
9842fa044e5SMatthew Wilcox return err;
9852fa044e5SMatthew Wilcox }
9862fa044e5SMatthew Wilcox
9872fa044e5SMatthew Wilcox /**
9882fa044e5SMatthew Wilcox * xa_alloc_cyclic_bh() - Find somewhere to store this entry in the XArray.
9892fa044e5SMatthew Wilcox * @xa: XArray.
9902fa044e5SMatthew Wilcox * @id: Pointer to ID.
9912fa044e5SMatthew Wilcox * @entry: New entry.
9922fa044e5SMatthew Wilcox * @limit: Range of allocated ID.
9932fa044e5SMatthew Wilcox * @next: Pointer to next ID to allocate.
9942fa044e5SMatthew Wilcox * @gfp: Memory allocation flags.
9952fa044e5SMatthew Wilcox *
9962fa044e5SMatthew Wilcox * Finds an empty entry in @xa between @limit.min and @limit.max,
9972fa044e5SMatthew Wilcox * stores the index into the @id pointer, then stores the entry at
9982fa044e5SMatthew Wilcox * that index. A concurrent lookup will not see an uninitialised @id.
9992fa044e5SMatthew Wilcox * The search for an empty entry will start at @next and will wrap
10002fa044e5SMatthew Wilcox * around if necessary.
10012fa044e5SMatthew Wilcox *
1002e7716c74SPhilipp Stanner * Must only be operated on an xarray initialized with flag XA_FLAGS_ALLOC set
1003e7716c74SPhilipp Stanner * in xa_init_flags().
1004e7716c74SPhilipp Stanner *
10052fa044e5SMatthew Wilcox * Context: Any context. Takes and releases the xa_lock while
10062fa044e5SMatthew Wilcox * disabling softirqs. May sleep if the @gfp flags permit.
10072fa044e5SMatthew Wilcox * Return: 0 if the allocation succeeded without wrapping. 1 if the
10082fa044e5SMatthew Wilcox * allocation succeeded after wrapping, -ENOMEM if memory could not be
10092fa044e5SMatthew Wilcox * allocated or -EBUSY if there are no free entries in @limit.
10102fa044e5SMatthew Wilcox */
xa_alloc_cyclic_bh(struct xarray * xa,u32 * id,void * entry,struct xa_limit limit,u32 * next,gfp_t gfp)10112fa044e5SMatthew Wilcox static inline int xa_alloc_cyclic_bh(struct xarray *xa, u32 *id, void *entry,
10122fa044e5SMatthew Wilcox struct xa_limit limit, u32 *next, gfp_t gfp)
10132fa044e5SMatthew Wilcox {
10142fa044e5SMatthew Wilcox int err;
10152fa044e5SMatthew Wilcox
10161dd685c4SMatthew Wilcox (Oracle) might_alloc(gfp);
10172fa044e5SMatthew Wilcox xa_lock_bh(xa);
10182fa044e5SMatthew Wilcox err = __xa_alloc_cyclic(xa, id, entry, limit, next, gfp);
10192fa044e5SMatthew Wilcox xa_unlock_bh(xa);
10202fa044e5SMatthew Wilcox
10212fa044e5SMatthew Wilcox return err;
10222fa044e5SMatthew Wilcox }
10232fa044e5SMatthew Wilcox
10242fa044e5SMatthew Wilcox /**
10252fa044e5SMatthew Wilcox * xa_alloc_cyclic_irq() - Find somewhere to store this entry in the XArray.
10262fa044e5SMatthew Wilcox * @xa: XArray.
10272fa044e5SMatthew Wilcox * @id: Pointer to ID.
10282fa044e5SMatthew Wilcox * @entry: New entry.
10292fa044e5SMatthew Wilcox * @limit: Range of allocated ID.
10302fa044e5SMatthew Wilcox * @next: Pointer to next ID to allocate.
10312fa044e5SMatthew Wilcox * @gfp: Memory allocation flags.
10322fa044e5SMatthew Wilcox *
10332fa044e5SMatthew Wilcox * Finds an empty entry in @xa between @limit.min and @limit.max,
10342fa044e5SMatthew Wilcox * stores the index into the @id pointer, then stores the entry at
10352fa044e5SMatthew Wilcox * that index. A concurrent lookup will not see an uninitialised @id.
10362fa044e5SMatthew Wilcox * The search for an empty entry will start at @next and will wrap
10372fa044e5SMatthew Wilcox * around if necessary.
10382fa044e5SMatthew Wilcox *
1039e7716c74SPhilipp Stanner * Must only be operated on an xarray initialized with flag XA_FLAGS_ALLOC set
1040e7716c74SPhilipp Stanner * in xa_init_flags().
1041e7716c74SPhilipp Stanner *
10422fa044e5SMatthew Wilcox * Context: Process context. Takes and releases the xa_lock while
10432fa044e5SMatthew Wilcox * disabling interrupts. May sleep if the @gfp flags permit.
10442fa044e5SMatthew Wilcox * Return: 0 if the allocation succeeded without wrapping. 1 if the
10452fa044e5SMatthew Wilcox * allocation succeeded after wrapping, -ENOMEM if memory could not be
10462fa044e5SMatthew Wilcox * allocated or -EBUSY if there are no free entries in @limit.
10472fa044e5SMatthew Wilcox */
xa_alloc_cyclic_irq(struct xarray * xa,u32 * id,void * entry,struct xa_limit limit,u32 * next,gfp_t gfp)10482fa044e5SMatthew Wilcox static inline int xa_alloc_cyclic_irq(struct xarray *xa, u32 *id, void *entry,
10492fa044e5SMatthew Wilcox struct xa_limit limit, u32 *next, gfp_t gfp)
10502fa044e5SMatthew Wilcox {
10512fa044e5SMatthew Wilcox int err;
10522fa044e5SMatthew Wilcox
10531dd685c4SMatthew Wilcox (Oracle) might_alloc(gfp);
10542fa044e5SMatthew Wilcox xa_lock_irq(xa);
10552fa044e5SMatthew Wilcox err = __xa_alloc_cyclic(xa, id, entry, limit, next, gfp);
10562fa044e5SMatthew Wilcox xa_unlock_irq(xa);
10572fa044e5SMatthew Wilcox
10582fa044e5SMatthew Wilcox return err;
10592fa044e5SMatthew Wilcox }
10602fa044e5SMatthew Wilcox
10612fa044e5SMatthew Wilcox /**
10624c0608f4SMatthew Wilcox * xa_reserve() - Reserve this index in the XArray.
10634c0608f4SMatthew Wilcox * @xa: XArray.
10644c0608f4SMatthew Wilcox * @index: Index into array.
10654c0608f4SMatthew Wilcox * @gfp: Memory allocation flags.
10664c0608f4SMatthew Wilcox *
10674c0608f4SMatthew Wilcox * Ensures there is somewhere to store an entry at @index in the array.
10684c0608f4SMatthew Wilcox * If there is already something stored at @index, this function does
10694c0608f4SMatthew Wilcox * nothing. If there was nothing there, the entry is marked as reserved.
10704c0608f4SMatthew Wilcox * Loading from a reserved entry returns a %NULL pointer.
10714c0608f4SMatthew Wilcox *
10724c0608f4SMatthew Wilcox * If you do not use the entry that you have reserved, call xa_release()
10734c0608f4SMatthew Wilcox * or xa_erase() to free any unnecessary memory.
10744c0608f4SMatthew Wilcox *
10754c0608f4SMatthew Wilcox * Context: Any context. Takes and releases the xa_lock.
10764c0608f4SMatthew Wilcox * May sleep if the @gfp flags permit.
10774c0608f4SMatthew Wilcox * Return: 0 if the reservation succeeded or -ENOMEM if it failed.
10784c0608f4SMatthew Wilcox */
1079f818b82bSMatthew Wilcox static inline __must_check
xa_reserve(struct xarray * xa,unsigned long index,gfp_t gfp)10804c0608f4SMatthew Wilcox int xa_reserve(struct xarray *xa, unsigned long index, gfp_t gfp)
10814c0608f4SMatthew Wilcox {
1082962033d5SMatthew Wilcox return xa_err(xa_cmpxchg(xa, index, NULL, XA_ZERO_ENTRY, gfp));
10834c0608f4SMatthew Wilcox }
10844c0608f4SMatthew Wilcox
10854c0608f4SMatthew Wilcox /**
10864c0608f4SMatthew Wilcox * xa_reserve_bh() - Reserve this index in the XArray.
10874c0608f4SMatthew Wilcox * @xa: XArray.
10884c0608f4SMatthew Wilcox * @index: Index into array.
10894c0608f4SMatthew Wilcox * @gfp: Memory allocation flags.
10904c0608f4SMatthew Wilcox *
10914c0608f4SMatthew Wilcox * A softirq-disabling version of xa_reserve().
10924c0608f4SMatthew Wilcox *
10934c0608f4SMatthew Wilcox * Context: Any context. Takes and releases the xa_lock while
10944c0608f4SMatthew Wilcox * disabling softirqs.
10954c0608f4SMatthew Wilcox * Return: 0 if the reservation succeeded or -ENOMEM if it failed.
10964c0608f4SMatthew Wilcox */
1097f818b82bSMatthew Wilcox static inline __must_check
xa_reserve_bh(struct xarray * xa,unsigned long index,gfp_t gfp)10984c0608f4SMatthew Wilcox int xa_reserve_bh(struct xarray *xa, unsigned long index, gfp_t gfp)
10994c0608f4SMatthew Wilcox {
1100962033d5SMatthew Wilcox return xa_err(xa_cmpxchg_bh(xa, index, NULL, XA_ZERO_ENTRY, gfp));
11014c0608f4SMatthew Wilcox }
11024c0608f4SMatthew Wilcox
11034c0608f4SMatthew Wilcox /**
11044c0608f4SMatthew Wilcox * xa_reserve_irq() - Reserve this index in the XArray.
11054c0608f4SMatthew Wilcox * @xa: XArray.
11064c0608f4SMatthew Wilcox * @index: Index into array.
11074c0608f4SMatthew Wilcox * @gfp: Memory allocation flags.
11084c0608f4SMatthew Wilcox *
11094c0608f4SMatthew Wilcox * An interrupt-disabling version of xa_reserve().
11104c0608f4SMatthew Wilcox *
11114c0608f4SMatthew Wilcox * Context: Process context. Takes and releases the xa_lock while
11124c0608f4SMatthew Wilcox * disabling interrupts.
11134c0608f4SMatthew Wilcox * Return: 0 if the reservation succeeded or -ENOMEM if it failed.
11144c0608f4SMatthew Wilcox */
1115f818b82bSMatthew Wilcox static inline __must_check
xa_reserve_irq(struct xarray * xa,unsigned long index,gfp_t gfp)11164c0608f4SMatthew Wilcox int xa_reserve_irq(struct xarray *xa, unsigned long index, gfp_t gfp)
11174c0608f4SMatthew Wilcox {
1118962033d5SMatthew Wilcox return xa_err(xa_cmpxchg_irq(xa, index, NULL, XA_ZERO_ENTRY, gfp));
11194c0608f4SMatthew Wilcox }
11204c0608f4SMatthew Wilcox
1121c5beb07eSMatthew Wilcox /**
1122c5beb07eSMatthew Wilcox * xa_release() - Release a reserved entry.
1123c5beb07eSMatthew Wilcox * @xa: XArray.
1124c5beb07eSMatthew Wilcox * @index: Index of entry.
1125c5beb07eSMatthew Wilcox *
1126c5beb07eSMatthew Wilcox * After calling xa_reserve(), you can call this function to release the
1127c5beb07eSMatthew Wilcox * reservation. If the entry at @index has been stored to, this function
1128c5beb07eSMatthew Wilcox * will do nothing.
1129c5beb07eSMatthew Wilcox */
xa_release(struct xarray * xa,unsigned long index)1130c5beb07eSMatthew Wilcox static inline void xa_release(struct xarray *xa, unsigned long index)
1131c5beb07eSMatthew Wilcox {
1132b38f6c50SMatthew Wilcox xa_cmpxchg(xa, index, XA_ZERO_ENTRY, NULL, 0);
1133c5beb07eSMatthew Wilcox }
1134c5beb07eSMatthew Wilcox
113502c02bf1SMatthew Wilcox /* Everything below here is the Advanced API. Proceed with caution. */
113602c02bf1SMatthew Wilcox
113702c02bf1SMatthew Wilcox /*
113802c02bf1SMatthew Wilcox * The xarray is constructed out of a set of 'chunks' of pointers. Choosing
113902c02bf1SMatthew Wilcox * the best chunk size requires some tradeoffs. A power of two recommends
114002c02bf1SMatthew Wilcox * itself so that we can walk the tree based purely on shifts and masks.
114102c02bf1SMatthew Wilcox * Generally, the larger the better; as the number of slots per level of the
114202c02bf1SMatthew Wilcox * tree increases, the less tall the tree needs to be. But that needs to be
114302c02bf1SMatthew Wilcox * balanced against the memory consumption of each node. On a 64-bit system,
114402c02bf1SMatthew Wilcox * xa_node is currently 576 bytes, and we get 7 of them per 4kB page. If we
114502c02bf1SMatthew Wilcox * doubled the number of slots per node, we'd get only 3 nodes per 4kB page.
114602c02bf1SMatthew Wilcox */
114702c02bf1SMatthew Wilcox #ifndef XA_CHUNK_SHIFT
1148b3e90f37SYoann Congal #define XA_CHUNK_SHIFT (IS_ENABLED(CONFIG_BASE_SMALL) ? 4 : 6)
114902c02bf1SMatthew Wilcox #endif
115002c02bf1SMatthew Wilcox #define XA_CHUNK_SIZE (1UL << XA_CHUNK_SHIFT)
115102c02bf1SMatthew Wilcox #define XA_CHUNK_MASK (XA_CHUNK_SIZE - 1)
115201959dfeSMatthew Wilcox #define XA_MAX_MARKS 3
1153ccde70f4SAndy Shevchenko #define XA_MARK_LONGS BITS_TO_LONGS(XA_CHUNK_SIZE)
115401959dfeSMatthew Wilcox
115501959dfeSMatthew Wilcox /*
115601959dfeSMatthew Wilcox * @count is the count of every non-NULL element in the ->slots array
115701959dfeSMatthew Wilcox * whether that is a value entry, a retry entry, a user pointer,
115801959dfeSMatthew Wilcox * a sibling entry or a pointer to the next level of the tree.
115901959dfeSMatthew Wilcox * @nr_values is the count of every element in ->slots which is
116001959dfeSMatthew Wilcox * either a value entry or a sibling of a value entry.
116101959dfeSMatthew Wilcox */
116201959dfeSMatthew Wilcox struct xa_node {
116301959dfeSMatthew Wilcox unsigned char shift; /* Bits remaining in each slot */
116401959dfeSMatthew Wilcox unsigned char offset; /* Slot offset in parent */
116501959dfeSMatthew Wilcox unsigned char count; /* Total entry count */
116601959dfeSMatthew Wilcox unsigned char nr_values; /* Value entry count */
116701959dfeSMatthew Wilcox struct xa_node __rcu *parent; /* NULL at top of tree */
116801959dfeSMatthew Wilcox struct xarray *array; /* The array we belong to */
116901959dfeSMatthew Wilcox union {
117001959dfeSMatthew Wilcox struct list_head private_list; /* For tree user */
117101959dfeSMatthew Wilcox struct rcu_head rcu_head; /* Used when freeing node */
117201959dfeSMatthew Wilcox };
117301959dfeSMatthew Wilcox void __rcu *slots[XA_CHUNK_SIZE];
117401959dfeSMatthew Wilcox union {
117501959dfeSMatthew Wilcox unsigned long tags[XA_MAX_MARKS][XA_MARK_LONGS];
117601959dfeSMatthew Wilcox unsigned long marks[XA_MAX_MARKS][XA_MARK_LONGS];
117701959dfeSMatthew Wilcox };
117801959dfeSMatthew Wilcox };
117902c02bf1SMatthew Wilcox
1180ad3d6c72SMatthew Wilcox void xa_dump(const struct xarray *);
1181ad3d6c72SMatthew Wilcox void xa_dump_node(const struct xa_node *);
1182ad3d6c72SMatthew Wilcox
1183ad3d6c72SMatthew Wilcox #ifdef XA_DEBUG
1184ad3d6c72SMatthew Wilcox #define XA_BUG_ON(xa, x) do { \
1185ad3d6c72SMatthew Wilcox if (x) { \
1186ad3d6c72SMatthew Wilcox xa_dump(xa); \
1187ad3d6c72SMatthew Wilcox BUG(); \
1188ad3d6c72SMatthew Wilcox } \
1189ad3d6c72SMatthew Wilcox } while (0)
1190ad3d6c72SMatthew Wilcox #define XA_NODE_BUG_ON(node, x) do { \
1191ad3d6c72SMatthew Wilcox if (x) { \
1192ad3d6c72SMatthew Wilcox if (node) xa_dump_node(node); \
1193ad3d6c72SMatthew Wilcox BUG(); \
1194ad3d6c72SMatthew Wilcox } \
1195ad3d6c72SMatthew Wilcox } while (0)
1196ad3d6c72SMatthew Wilcox #else
1197ad3d6c72SMatthew Wilcox #define XA_BUG_ON(xa, x) do { } while (0)
1198ad3d6c72SMatthew Wilcox #define XA_NODE_BUG_ON(node, x) do { } while (0)
1199ad3d6c72SMatthew Wilcox #endif
1200ad3d6c72SMatthew Wilcox
1201ad3d6c72SMatthew Wilcox /* Private */
xa_head(const struct xarray * xa)1202ad3d6c72SMatthew Wilcox static inline void *xa_head(const struct xarray *xa)
1203ad3d6c72SMatthew Wilcox {
1204ad3d6c72SMatthew Wilcox return rcu_dereference_check(xa->xa_head,
1205ad3d6c72SMatthew Wilcox lockdep_is_held(&xa->xa_lock));
1206ad3d6c72SMatthew Wilcox }
1207ad3d6c72SMatthew Wilcox
1208ad3d6c72SMatthew Wilcox /* Private */
xa_head_locked(const struct xarray * xa)1209ad3d6c72SMatthew Wilcox static inline void *xa_head_locked(const struct xarray *xa)
1210ad3d6c72SMatthew Wilcox {
1211ad3d6c72SMatthew Wilcox return rcu_dereference_protected(xa->xa_head,
1212ad3d6c72SMatthew Wilcox lockdep_is_held(&xa->xa_lock));
1213ad3d6c72SMatthew Wilcox }
1214ad3d6c72SMatthew Wilcox
1215ad3d6c72SMatthew Wilcox /* Private */
xa_entry(const struct xarray * xa,const struct xa_node * node,unsigned int offset)1216ad3d6c72SMatthew Wilcox static inline void *xa_entry(const struct xarray *xa,
1217ad3d6c72SMatthew Wilcox const struct xa_node *node, unsigned int offset)
1218ad3d6c72SMatthew Wilcox {
1219ad3d6c72SMatthew Wilcox XA_NODE_BUG_ON(node, offset >= XA_CHUNK_SIZE);
1220ad3d6c72SMatthew Wilcox return rcu_dereference_check(node->slots[offset],
1221ad3d6c72SMatthew Wilcox lockdep_is_held(&xa->xa_lock));
1222ad3d6c72SMatthew Wilcox }
1223ad3d6c72SMatthew Wilcox
1224ad3d6c72SMatthew Wilcox /* Private */
xa_entry_locked(const struct xarray * xa,const struct xa_node * node,unsigned int offset)1225ad3d6c72SMatthew Wilcox static inline void *xa_entry_locked(const struct xarray *xa,
1226ad3d6c72SMatthew Wilcox const struct xa_node *node, unsigned int offset)
1227ad3d6c72SMatthew Wilcox {
1228ad3d6c72SMatthew Wilcox XA_NODE_BUG_ON(node, offset >= XA_CHUNK_SIZE);
1229ad3d6c72SMatthew Wilcox return rcu_dereference_protected(node->slots[offset],
1230ad3d6c72SMatthew Wilcox lockdep_is_held(&xa->xa_lock));
1231ad3d6c72SMatthew Wilcox }
1232ad3d6c72SMatthew Wilcox
1233ad3d6c72SMatthew Wilcox /* Private */
xa_parent(const struct xarray * xa,const struct xa_node * node)12349b89a035SMatthew Wilcox static inline struct xa_node *xa_parent(const struct xarray *xa,
12359b89a035SMatthew Wilcox const struct xa_node *node)
12369b89a035SMatthew Wilcox {
12379b89a035SMatthew Wilcox return rcu_dereference_check(node->parent,
12389b89a035SMatthew Wilcox lockdep_is_held(&xa->xa_lock));
12399b89a035SMatthew Wilcox }
12409b89a035SMatthew Wilcox
12419b89a035SMatthew Wilcox /* Private */
xa_parent_locked(const struct xarray * xa,const struct xa_node * node)12429b89a035SMatthew Wilcox static inline struct xa_node *xa_parent_locked(const struct xarray *xa,
12439b89a035SMatthew Wilcox const struct xa_node *node)
12449b89a035SMatthew Wilcox {
12459b89a035SMatthew Wilcox return rcu_dereference_protected(node->parent,
12469b89a035SMatthew Wilcox lockdep_is_held(&xa->xa_lock));
12479b89a035SMatthew Wilcox }
12489b89a035SMatthew Wilcox
12499b89a035SMatthew Wilcox /* Private */
xa_mk_node(const struct xa_node * node)125058d6ea30SMatthew Wilcox static inline void *xa_mk_node(const struct xa_node *node)
125158d6ea30SMatthew Wilcox {
125258d6ea30SMatthew Wilcox return (void *)((unsigned long)node | 2);
125358d6ea30SMatthew Wilcox }
125458d6ea30SMatthew Wilcox
125558d6ea30SMatthew Wilcox /* Private */
xa_to_node(const void * entry)1256ad3d6c72SMatthew Wilcox static inline struct xa_node *xa_to_node(const void *entry)
1257ad3d6c72SMatthew Wilcox {
1258ad3d6c72SMatthew Wilcox return (struct xa_node *)((unsigned long)entry - 2);
1259ad3d6c72SMatthew Wilcox }
1260ad3d6c72SMatthew Wilcox
126102c02bf1SMatthew Wilcox /* Private */
xa_is_node(const void * entry)126202c02bf1SMatthew Wilcox static inline bool xa_is_node(const void *entry)
126302c02bf1SMatthew Wilcox {
126402c02bf1SMatthew Wilcox return xa_is_internal(entry) && (unsigned long)entry > 4096;
126502c02bf1SMatthew Wilcox }
126602c02bf1SMatthew Wilcox
126702c02bf1SMatthew Wilcox /* Private */
xa_mk_sibling(unsigned int offset)126802c02bf1SMatthew Wilcox static inline void *xa_mk_sibling(unsigned int offset)
126902c02bf1SMatthew Wilcox {
127002c02bf1SMatthew Wilcox return xa_mk_internal(offset);
127102c02bf1SMatthew Wilcox }
127202c02bf1SMatthew Wilcox
127302c02bf1SMatthew Wilcox /* Private */
xa_to_sibling(const void * entry)127402c02bf1SMatthew Wilcox static inline unsigned long xa_to_sibling(const void *entry)
127502c02bf1SMatthew Wilcox {
127602c02bf1SMatthew Wilcox return xa_to_internal(entry);
127702c02bf1SMatthew Wilcox }
127802c02bf1SMatthew Wilcox
127902c02bf1SMatthew Wilcox /**
128002c02bf1SMatthew Wilcox * xa_is_sibling() - Is the entry a sibling entry?
128102c02bf1SMatthew Wilcox * @entry: Entry retrieved from the XArray
128202c02bf1SMatthew Wilcox *
128302c02bf1SMatthew Wilcox * Return: %true if the entry is a sibling entry.
128402c02bf1SMatthew Wilcox */
xa_is_sibling(const void * entry)128502c02bf1SMatthew Wilcox static inline bool xa_is_sibling(const void *entry)
128602c02bf1SMatthew Wilcox {
128702c02bf1SMatthew Wilcox return IS_ENABLED(CONFIG_XARRAY_MULTI) && xa_is_internal(entry) &&
128802c02bf1SMatthew Wilcox (entry < xa_mk_sibling(XA_CHUNK_SIZE - 1));
128902c02bf1SMatthew Wilcox }
129002c02bf1SMatthew Wilcox
129176b4e529SMatthew Wilcox #define XA_RETRY_ENTRY xa_mk_internal(256)
129202c02bf1SMatthew Wilcox
1293ad3d6c72SMatthew Wilcox /**
1294ad3d6c72SMatthew Wilcox * xa_is_retry() - Is the entry a retry entry?
1295ad3d6c72SMatthew Wilcox * @entry: Entry retrieved from the XArray
1296ad3d6c72SMatthew Wilcox *
1297ad3d6c72SMatthew Wilcox * Return: %true if the entry is a retry entry.
1298ad3d6c72SMatthew Wilcox */
xa_is_retry(const void * entry)1299ad3d6c72SMatthew Wilcox static inline bool xa_is_retry(const void *entry)
1300ad3d6c72SMatthew Wilcox {
1301ad3d6c72SMatthew Wilcox return unlikely(entry == XA_RETRY_ENTRY);
1302ad3d6c72SMatthew Wilcox }
1303ad3d6c72SMatthew Wilcox
1304ad3d6c72SMatthew Wilcox /**
130576b4e529SMatthew Wilcox * xa_is_advanced() - Is the entry only permitted for the advanced API?
130676b4e529SMatthew Wilcox * @entry: Entry to be stored in the XArray.
130776b4e529SMatthew Wilcox *
130876b4e529SMatthew Wilcox * Return: %true if the entry cannot be stored by the normal API.
130976b4e529SMatthew Wilcox */
xa_is_advanced(const void * entry)131076b4e529SMatthew Wilcox static inline bool xa_is_advanced(const void *entry)
131176b4e529SMatthew Wilcox {
131276b4e529SMatthew Wilcox return xa_is_internal(entry) && (entry <= XA_RETRY_ENTRY);
131376b4e529SMatthew Wilcox }
131476b4e529SMatthew Wilcox
131576b4e529SMatthew Wilcox /**
1316ad3d6c72SMatthew Wilcox * typedef xa_update_node_t - A callback function from the XArray.
1317ad3d6c72SMatthew Wilcox * @node: The node which is being processed
1318ad3d6c72SMatthew Wilcox *
1319ad3d6c72SMatthew Wilcox * This function is called every time the XArray updates the count of
1320ad3d6c72SMatthew Wilcox * present and value entries in a node. It allows advanced users to
1321ad3d6c72SMatthew Wilcox * maintain the private_list in the node.
1322ad3d6c72SMatthew Wilcox *
1323ad3d6c72SMatthew Wilcox * Context: The xa_lock is held and interrupts may be disabled.
1324ad3d6c72SMatthew Wilcox * Implementations should not drop the xa_lock, nor re-enable
1325ad3d6c72SMatthew Wilcox * interrupts.
1326ad3d6c72SMatthew Wilcox */
1327ad3d6c72SMatthew Wilcox typedef void (*xa_update_node_t)(struct xa_node *node);
1328ad3d6c72SMatthew Wilcox
1329f82cd2f0SMatthew Wilcox (Oracle) void xa_delete_node(struct xa_node *, xa_update_node_t);
1330f82cd2f0SMatthew Wilcox (Oracle)
1331ad3d6c72SMatthew Wilcox /*
1332ad3d6c72SMatthew Wilcox * The xa_state is opaque to its users. It contains various different pieces
1333ad3d6c72SMatthew Wilcox * of state involved in the current operation on the XArray. It should be
1334ad3d6c72SMatthew Wilcox * declared on the stack and passed between the various internal routines.
1335ad3d6c72SMatthew Wilcox * The various elements in it should not be accessed directly, but only
1336ad3d6c72SMatthew Wilcox * through the provided accessor functions. The below documentation is for
1337ad3d6c72SMatthew Wilcox * the benefit of those working on the code, not for users of the XArray.
1338ad3d6c72SMatthew Wilcox *
1339ad3d6c72SMatthew Wilcox * @xa_node usually points to the xa_node containing the slot we're operating
1340ad3d6c72SMatthew Wilcox * on (and @xa_offset is the offset in the slots array). If there is a
1341ad3d6c72SMatthew Wilcox * single entry in the array at index 0, there are no allocated xa_nodes to
1342ad3d6c72SMatthew Wilcox * point to, and so we store %NULL in @xa_node. @xa_node is set to
1343ad3d6c72SMatthew Wilcox * the value %XAS_RESTART if the xa_state is not walked to the correct
1344ad3d6c72SMatthew Wilcox * position in the tree of nodes for this operation. If an error occurs
1345ad3d6c72SMatthew Wilcox * during an operation, it is set to an %XAS_ERROR value. If we run off the
1346ad3d6c72SMatthew Wilcox * end of the allocated nodes, it is set to %XAS_BOUNDS.
1347ad3d6c72SMatthew Wilcox */
1348ad3d6c72SMatthew Wilcox struct xa_state {
1349ad3d6c72SMatthew Wilcox struct xarray *xa;
1350ad3d6c72SMatthew Wilcox unsigned long xa_index;
1351ad3d6c72SMatthew Wilcox unsigned char xa_shift;
1352ad3d6c72SMatthew Wilcox unsigned char xa_sibs;
1353ad3d6c72SMatthew Wilcox unsigned char xa_offset;
1354ad3d6c72SMatthew Wilcox unsigned char xa_pad; /* Helps gcc generate better code */
1355ad3d6c72SMatthew Wilcox struct xa_node *xa_node;
1356ad3d6c72SMatthew Wilcox struct xa_node *xa_alloc;
1357ad3d6c72SMatthew Wilcox xa_update_node_t xa_update;
13589bbdc0f3SMuchun Song struct list_lru *xa_lru;
1359ad3d6c72SMatthew Wilcox };
1360ad3d6c72SMatthew Wilcox
1361ad3d6c72SMatthew Wilcox /*
1362ad3d6c72SMatthew Wilcox * We encode errnos in the xas->xa_node. If an error has happened, we need to
1363ad3d6c72SMatthew Wilcox * drop the lock to fix it, and once we've done so the xa_state is invalid.
1364ad3d6c72SMatthew Wilcox */
1365ad3d6c72SMatthew Wilcox #define XA_ERROR(errno) ((struct xa_node *)(((unsigned long)errno << 2) | 2UL))
1366ad3d6c72SMatthew Wilcox #define XAS_BOUNDS ((struct xa_node *)1UL)
1367ad3d6c72SMatthew Wilcox #define XAS_RESTART ((struct xa_node *)3UL)
1368ad3d6c72SMatthew Wilcox
1369ad3d6c72SMatthew Wilcox #define __XA_STATE(array, index, shift, sibs) { \
1370ad3d6c72SMatthew Wilcox .xa = array, \
1371ad3d6c72SMatthew Wilcox .xa_index = index, \
1372ad3d6c72SMatthew Wilcox .xa_shift = shift, \
1373ad3d6c72SMatthew Wilcox .xa_sibs = sibs, \
1374ad3d6c72SMatthew Wilcox .xa_offset = 0, \
1375ad3d6c72SMatthew Wilcox .xa_pad = 0, \
1376ad3d6c72SMatthew Wilcox .xa_node = XAS_RESTART, \
1377ad3d6c72SMatthew Wilcox .xa_alloc = NULL, \
13789bbdc0f3SMuchun Song .xa_update = NULL, \
13799bbdc0f3SMuchun Song .xa_lru = NULL, \
1380ad3d6c72SMatthew Wilcox }
1381ad3d6c72SMatthew Wilcox
1382ad3d6c72SMatthew Wilcox /**
1383ad3d6c72SMatthew Wilcox * XA_STATE() - Declare an XArray operation state.
1384ad3d6c72SMatthew Wilcox * @name: Name of this operation state (usually xas).
1385ad3d6c72SMatthew Wilcox * @array: Array to operate on.
1386ad3d6c72SMatthew Wilcox * @index: Initial index of interest.
1387ad3d6c72SMatthew Wilcox *
1388ad3d6c72SMatthew Wilcox * Declare and initialise an xa_state on the stack.
1389ad3d6c72SMatthew Wilcox */
1390ad3d6c72SMatthew Wilcox #define XA_STATE(name, array, index) \
1391ad3d6c72SMatthew Wilcox struct xa_state name = __XA_STATE(array, index, 0, 0)
1392ad3d6c72SMatthew Wilcox
1393ad3d6c72SMatthew Wilcox /**
1394ad3d6c72SMatthew Wilcox * XA_STATE_ORDER() - Declare an XArray operation state.
1395ad3d6c72SMatthew Wilcox * @name: Name of this operation state (usually xas).
1396ad3d6c72SMatthew Wilcox * @array: Array to operate on.
1397ad3d6c72SMatthew Wilcox * @index: Initial index of interest.
1398ad3d6c72SMatthew Wilcox * @order: Order of entry.
1399ad3d6c72SMatthew Wilcox *
1400ad3d6c72SMatthew Wilcox * Declare and initialise an xa_state on the stack. This variant of
1401ad3d6c72SMatthew Wilcox * XA_STATE() allows you to specify the 'order' of the element you
1402ad3d6c72SMatthew Wilcox * want to operate on.`
1403ad3d6c72SMatthew Wilcox */
1404ad3d6c72SMatthew Wilcox #define XA_STATE_ORDER(name, array, index, order) \
1405ad3d6c72SMatthew Wilcox struct xa_state name = __XA_STATE(array, \
1406ad3d6c72SMatthew Wilcox (index >> order) << order, \
1407ad3d6c72SMatthew Wilcox order - (order % XA_CHUNK_SHIFT), \
1408ad3d6c72SMatthew Wilcox (1U << (order % XA_CHUNK_SHIFT)) - 1)
1409ad3d6c72SMatthew Wilcox
1410ad3d6c72SMatthew Wilcox #define xas_marked(xas, mark) xa_marked((xas)->xa, (mark))
1411ad3d6c72SMatthew Wilcox #define xas_trylock(xas) xa_trylock((xas)->xa)
1412ad3d6c72SMatthew Wilcox #define xas_lock(xas) xa_lock((xas)->xa)
1413ad3d6c72SMatthew Wilcox #define xas_unlock(xas) xa_unlock((xas)->xa)
1414ad3d6c72SMatthew Wilcox #define xas_lock_bh(xas) xa_lock_bh((xas)->xa)
1415ad3d6c72SMatthew Wilcox #define xas_unlock_bh(xas) xa_unlock_bh((xas)->xa)
1416ad3d6c72SMatthew Wilcox #define xas_lock_irq(xas) xa_lock_irq((xas)->xa)
1417ad3d6c72SMatthew Wilcox #define xas_unlock_irq(xas) xa_unlock_irq((xas)->xa)
1418ad3d6c72SMatthew Wilcox #define xas_lock_irqsave(xas, flags) \
1419ad3d6c72SMatthew Wilcox xa_lock_irqsave((xas)->xa, flags)
1420ad3d6c72SMatthew Wilcox #define xas_unlock_irqrestore(xas, flags) \
1421ad3d6c72SMatthew Wilcox xa_unlock_irqrestore((xas)->xa, flags)
1422ad3d6c72SMatthew Wilcox
1423ad3d6c72SMatthew Wilcox /**
1424ad3d6c72SMatthew Wilcox * xas_error() - Return an errno stored in the xa_state.
1425ad3d6c72SMatthew Wilcox * @xas: XArray operation state.
1426ad3d6c72SMatthew Wilcox *
1427ad3d6c72SMatthew Wilcox * Return: 0 if no error has been noted. A negative errno if one has.
1428ad3d6c72SMatthew Wilcox */
xas_error(const struct xa_state * xas)1429ad3d6c72SMatthew Wilcox static inline int xas_error(const struct xa_state *xas)
1430ad3d6c72SMatthew Wilcox {
1431ad3d6c72SMatthew Wilcox return xa_err(xas->xa_node);
1432ad3d6c72SMatthew Wilcox }
1433ad3d6c72SMatthew Wilcox
1434ad3d6c72SMatthew Wilcox /**
1435ad3d6c72SMatthew Wilcox * xas_set_err() - Note an error in the xa_state.
1436ad3d6c72SMatthew Wilcox * @xas: XArray operation state.
1437ad3d6c72SMatthew Wilcox * @err: Negative error number.
1438ad3d6c72SMatthew Wilcox *
1439ad3d6c72SMatthew Wilcox * Only call this function with a negative @err; zero or positive errors
1440ad3d6c72SMatthew Wilcox * will probably not behave the way you think they should. If you want
1441ad3d6c72SMatthew Wilcox * to clear the error from an xa_state, use xas_reset().
1442ad3d6c72SMatthew Wilcox */
xas_set_err(struct xa_state * xas,long err)1443ad3d6c72SMatthew Wilcox static inline void xas_set_err(struct xa_state *xas, long err)
1444ad3d6c72SMatthew Wilcox {
1445ad3d6c72SMatthew Wilcox xas->xa_node = XA_ERROR(err);
1446ad3d6c72SMatthew Wilcox }
1447ad3d6c72SMatthew Wilcox
1448ad3d6c72SMatthew Wilcox /**
1449ad3d6c72SMatthew Wilcox * xas_invalid() - Is the xas in a retry or error state?
1450ad3d6c72SMatthew Wilcox * @xas: XArray operation state.
1451ad3d6c72SMatthew Wilcox *
1452ad3d6c72SMatthew Wilcox * Return: %true if the xas cannot be used for operations.
1453ad3d6c72SMatthew Wilcox */
xas_invalid(const struct xa_state * xas)1454ad3d6c72SMatthew Wilcox static inline bool xas_invalid(const struct xa_state *xas)
1455ad3d6c72SMatthew Wilcox {
1456ad3d6c72SMatthew Wilcox return (unsigned long)xas->xa_node & 3;
1457ad3d6c72SMatthew Wilcox }
1458ad3d6c72SMatthew Wilcox
1459ad3d6c72SMatthew Wilcox /**
1460ad3d6c72SMatthew Wilcox * xas_valid() - Is the xas a valid cursor into the array?
1461ad3d6c72SMatthew Wilcox * @xas: XArray operation state.
1462ad3d6c72SMatthew Wilcox *
1463ad3d6c72SMatthew Wilcox * Return: %true if the xas can be used for operations.
1464ad3d6c72SMatthew Wilcox */
xas_valid(const struct xa_state * xas)1465ad3d6c72SMatthew Wilcox static inline bool xas_valid(const struct xa_state *xas)
1466ad3d6c72SMatthew Wilcox {
1467ad3d6c72SMatthew Wilcox return !xas_invalid(xas);
1468ad3d6c72SMatthew Wilcox }
1469ad3d6c72SMatthew Wilcox
14702264f513SMatthew Wilcox /**
14712264f513SMatthew Wilcox * xas_is_node() - Does the xas point to a node?
14722264f513SMatthew Wilcox * @xas: XArray operation state.
14732264f513SMatthew Wilcox *
14742264f513SMatthew Wilcox * Return: %true if the xas currently references a node.
14752264f513SMatthew Wilcox */
xas_is_node(const struct xa_state * xas)14762264f513SMatthew Wilcox static inline bool xas_is_node(const struct xa_state *xas)
14772264f513SMatthew Wilcox {
14782264f513SMatthew Wilcox return xas_valid(xas) && xas->xa_node;
14792264f513SMatthew Wilcox }
14802264f513SMatthew Wilcox
14819b89a035SMatthew Wilcox /* True if the pointer is something other than a node */
xas_not_node(struct xa_node * node)14829b89a035SMatthew Wilcox static inline bool xas_not_node(struct xa_node *node)
14839b89a035SMatthew Wilcox {
14849b89a035SMatthew Wilcox return ((unsigned long)node & 3) || !node;
14859b89a035SMatthew Wilcox }
14869b89a035SMatthew Wilcox
148764d3e9a9SMatthew Wilcox /* True if the node represents RESTART or an error */
xas_frozen(struct xa_node * node)148864d3e9a9SMatthew Wilcox static inline bool xas_frozen(struct xa_node *node)
148964d3e9a9SMatthew Wilcox {
149064d3e9a9SMatthew Wilcox return (unsigned long)node & 2;
149164d3e9a9SMatthew Wilcox }
149264d3e9a9SMatthew Wilcox
149358d6ea30SMatthew Wilcox /* True if the node represents head-of-tree, RESTART or BOUNDS */
xas_top(struct xa_node * node)149458d6ea30SMatthew Wilcox static inline bool xas_top(struct xa_node *node)
149558d6ea30SMatthew Wilcox {
149658d6ea30SMatthew Wilcox return node <= XAS_RESTART;
149758d6ea30SMatthew Wilcox }
149858d6ea30SMatthew Wilcox
1499ad3d6c72SMatthew Wilcox /**
1500ad3d6c72SMatthew Wilcox * xas_reset() - Reset an XArray operation state.
1501ad3d6c72SMatthew Wilcox * @xas: XArray operation state.
1502ad3d6c72SMatthew Wilcox *
1503ad3d6c72SMatthew Wilcox * Resets the error or walk state of the @xas so future walks of the
1504ad3d6c72SMatthew Wilcox * array will start from the root. Use this if you have dropped the
1505ad3d6c72SMatthew Wilcox * xarray lock and want to reuse the xa_state.
1506ad3d6c72SMatthew Wilcox *
1507ad3d6c72SMatthew Wilcox * Context: Any context.
1508ad3d6c72SMatthew Wilcox */
xas_reset(struct xa_state * xas)1509ad3d6c72SMatthew Wilcox static inline void xas_reset(struct xa_state *xas)
1510ad3d6c72SMatthew Wilcox {
1511ad3d6c72SMatthew Wilcox xas->xa_node = XAS_RESTART;
1512ad3d6c72SMatthew Wilcox }
1513ad3d6c72SMatthew Wilcox
1514ad3d6c72SMatthew Wilcox /**
1515ad3d6c72SMatthew Wilcox * xas_retry() - Retry the operation if appropriate.
1516ad3d6c72SMatthew Wilcox * @xas: XArray operation state.
1517ad3d6c72SMatthew Wilcox * @entry: Entry from xarray.
1518ad3d6c72SMatthew Wilcox *
1519ad3d6c72SMatthew Wilcox * The advanced functions may sometimes return an internal entry, such as
1520ad3d6c72SMatthew Wilcox * a retry entry or a zero entry. This function sets up the @xas to restart
1521ad3d6c72SMatthew Wilcox * the walk from the head of the array if needed.
1522ad3d6c72SMatthew Wilcox *
1523ad3d6c72SMatthew Wilcox * Context: Any context.
1524ad3d6c72SMatthew Wilcox * Return: true if the operation needs to be retried.
1525ad3d6c72SMatthew Wilcox */
xas_retry(struct xa_state * xas,const void * entry)1526ad3d6c72SMatthew Wilcox static inline bool xas_retry(struct xa_state *xas, const void *entry)
1527ad3d6c72SMatthew Wilcox {
15289f14d4f1SMatthew Wilcox if (xa_is_zero(entry))
15299f14d4f1SMatthew Wilcox return true;
1530ad3d6c72SMatthew Wilcox if (!xa_is_retry(entry))
1531ad3d6c72SMatthew Wilcox return false;
1532ad3d6c72SMatthew Wilcox xas_reset(xas);
1533ad3d6c72SMatthew Wilcox return true;
1534ad3d6c72SMatthew Wilcox }
1535ad3d6c72SMatthew Wilcox
1536ad3d6c72SMatthew Wilcox void *xas_load(struct xa_state *);
153758d6ea30SMatthew Wilcox void *xas_store(struct xa_state *, void *entry);
1538b803b428SMatthew Wilcox void *xas_find(struct xa_state *, unsigned long max);
15394e99d4e9SMatthew Wilcox void *xas_find_conflict(struct xa_state *);
1540ad3d6c72SMatthew Wilcox
15419b89a035SMatthew Wilcox bool xas_get_mark(const struct xa_state *, xa_mark_t);
15429b89a035SMatthew Wilcox void xas_set_mark(const struct xa_state *, xa_mark_t);
15439b89a035SMatthew Wilcox void xas_clear_mark(const struct xa_state *, xa_mark_t);
1544b803b428SMatthew Wilcox void *xas_find_marked(struct xa_state *, unsigned long max, xa_mark_t);
154558d6ea30SMatthew Wilcox void xas_init_marks(const struct xa_state *);
154658d6ea30SMatthew Wilcox
154758d6ea30SMatthew Wilcox bool xas_nomem(struct xa_state *, gfp_t);
154869a37a8bSMatthew Wilcox (Oracle) void xas_destroy(struct xa_state *);
1549b803b428SMatthew Wilcox void xas_pause(struct xa_state *);
15509b89a035SMatthew Wilcox
15512264f513SMatthew Wilcox void xas_create_range(struct xa_state *);
15522264f513SMatthew Wilcox
155357417cebSMatthew Wilcox (Oracle) #ifdef CONFIG_XARRAY_MULTI
155457417cebSMatthew Wilcox (Oracle) int xa_get_order(struct xarray *, unsigned long index);
1555a4864671SKairui Song int xas_get_order(struct xa_state *xas);
15568fc75643SMatthew Wilcox (Oracle) void xas_split(struct xa_state *, void *entry, unsigned int order);
15578fc75643SMatthew Wilcox (Oracle) void xas_split_alloc(struct xa_state *, void *entry, unsigned int order, gfp_t);
15583fec86f8SZi Yan void xas_try_split(struct xa_state *xas, void *entry, unsigned int order);
1559*200a89c1SZi Yan unsigned int xas_try_split_min_order(unsigned int order);
156057417cebSMatthew Wilcox (Oracle) #else
xa_get_order(struct xarray * xa,unsigned long index)156157417cebSMatthew Wilcox (Oracle) static inline int xa_get_order(struct xarray *xa, unsigned long index)
156257417cebSMatthew Wilcox (Oracle) {
156357417cebSMatthew Wilcox (Oracle) return 0;
156457417cebSMatthew Wilcox (Oracle) }
15658fc75643SMatthew Wilcox (Oracle)
xas_get_order(struct xa_state * xas)1566a4864671SKairui Song static inline int xas_get_order(struct xa_state *xas)
1567a4864671SKairui Song {
1568a4864671SKairui Song return 0;
1569a4864671SKairui Song }
1570a4864671SKairui Song
xas_split(struct xa_state * xas,void * entry,unsigned int order)15718fc75643SMatthew Wilcox (Oracle) static inline void xas_split(struct xa_state *xas, void *entry,
15728fc75643SMatthew Wilcox (Oracle) unsigned int order)
15738fc75643SMatthew Wilcox (Oracle) {
15748fc75643SMatthew Wilcox (Oracle) xas_store(xas, entry);
15758fc75643SMatthew Wilcox (Oracle) }
15768fc75643SMatthew Wilcox (Oracle)
xas_split_alloc(struct xa_state * xas,void * entry,unsigned int order,gfp_t gfp)15778fc75643SMatthew Wilcox (Oracle) static inline void xas_split_alloc(struct xa_state *xas, void *entry,
15788fc75643SMatthew Wilcox (Oracle) unsigned int order, gfp_t gfp)
15798fc75643SMatthew Wilcox (Oracle) {
15808fc75643SMatthew Wilcox (Oracle) }
15813fec86f8SZi Yan
xas_try_split(struct xa_state * xas,void * entry,unsigned int order)15823fec86f8SZi Yan static inline void xas_try_split(struct xa_state *xas, void *entry,
15833fec86f8SZi Yan unsigned int order)
15843fec86f8SZi Yan {
15853fec86f8SZi Yan }
1586*200a89c1SZi Yan
xas_try_split_min_order(unsigned int order)1587*200a89c1SZi Yan static inline unsigned int xas_try_split_min_order(unsigned int order)
1588*200a89c1SZi Yan {
1589*200a89c1SZi Yan return 0;
1590*200a89c1SZi Yan }
1591*200a89c1SZi Yan
159257417cebSMatthew Wilcox (Oracle) #endif
159357417cebSMatthew Wilcox (Oracle)
1594ad3d6c72SMatthew Wilcox /**
1595ad3d6c72SMatthew Wilcox * xas_reload() - Refetch an entry from the xarray.
1596ad3d6c72SMatthew Wilcox * @xas: XArray operation state.
1597ad3d6c72SMatthew Wilcox *
1598ad3d6c72SMatthew Wilcox * Use this function to check that a previously loaded entry still has
1599ad3d6c72SMatthew Wilcox * the same value. This is useful for the lockless pagecache lookup where
1600ad3d6c72SMatthew Wilcox * we walk the array with only the RCU lock to protect us, lock the page,
1601ad3d6c72SMatthew Wilcox * then check that the page hasn't moved since we looked it up.
1602ad3d6c72SMatthew Wilcox *
1603ad3d6c72SMatthew Wilcox * The caller guarantees that @xas is still valid. If it may be in an
1604ad3d6c72SMatthew Wilcox * error or restart state, call xas_load() instead.
1605ad3d6c72SMatthew Wilcox *
1606ad3d6c72SMatthew Wilcox * Return: The entry at this location in the xarray.
1607ad3d6c72SMatthew Wilcox */
xas_reload(struct xa_state * xas)1608ad3d6c72SMatthew Wilcox static inline void *xas_reload(struct xa_state *xas)
1609ad3d6c72SMatthew Wilcox {
1610ad3d6c72SMatthew Wilcox struct xa_node *node = xas->xa_node;
1611ca7b639eSMatthew Wilcox (Oracle) void *entry;
1612ca7b639eSMatthew Wilcox (Oracle) char offset;
1613ad3d6c72SMatthew Wilcox
1614ca7b639eSMatthew Wilcox (Oracle) if (!node)
1615ad3d6c72SMatthew Wilcox return xa_head(xas->xa);
1616ca7b639eSMatthew Wilcox (Oracle) if (IS_ENABLED(CONFIG_XARRAY_MULTI)) {
1617ca7b639eSMatthew Wilcox (Oracle) offset = (xas->xa_index >> node->shift) & XA_CHUNK_MASK;
1618ca7b639eSMatthew Wilcox (Oracle) entry = xa_entry(xas->xa, node, offset);
1619ca7b639eSMatthew Wilcox (Oracle) if (!xa_is_sibling(entry))
1620ca7b639eSMatthew Wilcox (Oracle) return entry;
1621ca7b639eSMatthew Wilcox (Oracle) offset = xa_to_sibling(entry);
1622ca7b639eSMatthew Wilcox (Oracle) } else {
1623ca7b639eSMatthew Wilcox (Oracle) offset = xas->xa_offset;
1624ca7b639eSMatthew Wilcox (Oracle) }
1625ca7b639eSMatthew Wilcox (Oracle) return xa_entry(xas->xa, node, offset);
1626ad3d6c72SMatthew Wilcox }
1627ad3d6c72SMatthew Wilcox
162858d6ea30SMatthew Wilcox /**
162958d6ea30SMatthew Wilcox * xas_set() - Set up XArray operation state for a different index.
163058d6ea30SMatthew Wilcox * @xas: XArray operation state.
163158d6ea30SMatthew Wilcox * @index: New index into the XArray.
163258d6ea30SMatthew Wilcox *
163358d6ea30SMatthew Wilcox * Move the operation state to refer to a different index. This will
163458d6ea30SMatthew Wilcox * have the effect of starting a walk from the top; see xas_next()
163558d6ea30SMatthew Wilcox * to move to an adjacent index.
163658d6ea30SMatthew Wilcox */
xas_set(struct xa_state * xas,unsigned long index)163758d6ea30SMatthew Wilcox static inline void xas_set(struct xa_state *xas, unsigned long index)
163858d6ea30SMatthew Wilcox {
163958d6ea30SMatthew Wilcox xas->xa_index = index;
164058d6ea30SMatthew Wilcox xas->xa_node = XAS_RESTART;
164158d6ea30SMatthew Wilcox }
164258d6ea30SMatthew Wilcox
164358d6ea30SMatthew Wilcox /**
164425a8de7fSMatthew Wilcox (Oracle) * xas_advance() - Skip over sibling entries.
164525a8de7fSMatthew Wilcox (Oracle) * @xas: XArray operation state.
164625a8de7fSMatthew Wilcox (Oracle) * @index: Index of last sibling entry.
164725a8de7fSMatthew Wilcox (Oracle) *
164825a8de7fSMatthew Wilcox (Oracle) * Move the operation state to refer to the last sibling entry.
164925a8de7fSMatthew Wilcox (Oracle) * This is useful for loops that normally want to see sibling
165025a8de7fSMatthew Wilcox (Oracle) * entries but sometimes want to skip them. Use xas_set() if you
165125a8de7fSMatthew Wilcox (Oracle) * want to move to an index which is not part of this entry.
165225a8de7fSMatthew Wilcox (Oracle) */
xas_advance(struct xa_state * xas,unsigned long index)165325a8de7fSMatthew Wilcox (Oracle) static inline void xas_advance(struct xa_state *xas, unsigned long index)
165425a8de7fSMatthew Wilcox (Oracle) {
165525a8de7fSMatthew Wilcox (Oracle) unsigned char shift = xas_is_node(xas) ? xas->xa_node->shift : 0;
165625a8de7fSMatthew Wilcox (Oracle)
165725a8de7fSMatthew Wilcox (Oracle) xas->xa_index = index;
165825a8de7fSMatthew Wilcox (Oracle) xas->xa_offset = (index >> shift) & XA_CHUNK_MASK;
165925a8de7fSMatthew Wilcox (Oracle) }
166025a8de7fSMatthew Wilcox (Oracle)
166125a8de7fSMatthew Wilcox (Oracle) /**
166258d6ea30SMatthew Wilcox * xas_set_order() - Set up XArray operation state for a multislot entry.
166358d6ea30SMatthew Wilcox * @xas: XArray operation state.
166458d6ea30SMatthew Wilcox * @index: Target of the operation.
166558d6ea30SMatthew Wilcox * @order: Entry occupies 2^@order indices.
166658d6ea30SMatthew Wilcox */
xas_set_order(struct xa_state * xas,unsigned long index,unsigned int order)166758d6ea30SMatthew Wilcox static inline void xas_set_order(struct xa_state *xas, unsigned long index,
166858d6ea30SMatthew Wilcox unsigned int order)
166958d6ea30SMatthew Wilcox {
167058d6ea30SMatthew Wilcox #ifdef CONFIG_XARRAY_MULTI
167158d6ea30SMatthew Wilcox xas->xa_index = order < BITS_PER_LONG ? (index >> order) << order : 0;
167258d6ea30SMatthew Wilcox xas->xa_shift = order - (order % XA_CHUNK_SHIFT);
167358d6ea30SMatthew Wilcox xas->xa_sibs = (1 << (order % XA_CHUNK_SHIFT)) - 1;
167458d6ea30SMatthew Wilcox xas->xa_node = XAS_RESTART;
167558d6ea30SMatthew Wilcox #else
167658d6ea30SMatthew Wilcox BUG_ON(order > 0);
167758d6ea30SMatthew Wilcox xas_set(xas, index);
167858d6ea30SMatthew Wilcox #endif
167958d6ea30SMatthew Wilcox }
168058d6ea30SMatthew Wilcox
168158d6ea30SMatthew Wilcox /**
168258d6ea30SMatthew Wilcox * xas_set_update() - Set up XArray operation state for a callback.
168358d6ea30SMatthew Wilcox * @xas: XArray operation state.
168458d6ea30SMatthew Wilcox * @update: Function to call when updating a node.
168558d6ea30SMatthew Wilcox *
168658d6ea30SMatthew Wilcox * The XArray can notify a caller after it has updated an xa_node.
16875649d113SYang Yang * This is advanced functionality and is only needed by the page
16885649d113SYang Yang * cache and swap cache.
168958d6ea30SMatthew Wilcox */
xas_set_update(struct xa_state * xas,xa_update_node_t update)169058d6ea30SMatthew Wilcox static inline void xas_set_update(struct xa_state *xas, xa_update_node_t update)
169158d6ea30SMatthew Wilcox {
169258d6ea30SMatthew Wilcox xas->xa_update = update;
169358d6ea30SMatthew Wilcox }
169458d6ea30SMatthew Wilcox
xas_set_lru(struct xa_state * xas,struct list_lru * lru)16959bbdc0f3SMuchun Song static inline void xas_set_lru(struct xa_state *xas, struct list_lru *lru)
16969bbdc0f3SMuchun Song {
16979bbdc0f3SMuchun Song xas->xa_lru = lru;
16989bbdc0f3SMuchun Song }
16999bbdc0f3SMuchun Song
1700b803b428SMatthew Wilcox /**
1701b803b428SMatthew Wilcox * xas_next_entry() - Advance iterator to next present entry.
1702b803b428SMatthew Wilcox * @xas: XArray operation state.
1703b803b428SMatthew Wilcox * @max: Highest index to return.
1704b803b428SMatthew Wilcox *
1705b803b428SMatthew Wilcox * xas_next_entry() is an inline function to optimise xarray traversal for
1706b803b428SMatthew Wilcox * speed. It is equivalent to calling xas_find(), and will call xas_find()
1707b803b428SMatthew Wilcox * for all the hard cases.
1708b803b428SMatthew Wilcox *
1709b803b428SMatthew Wilcox * Return: The next present entry after the one currently referred to by @xas.
1710b803b428SMatthew Wilcox */
xas_next_entry(struct xa_state * xas,unsigned long max)1711b803b428SMatthew Wilcox static inline void *xas_next_entry(struct xa_state *xas, unsigned long max)
1712b803b428SMatthew Wilcox {
1713b803b428SMatthew Wilcox struct xa_node *node = xas->xa_node;
1714b803b428SMatthew Wilcox void *entry;
1715b803b428SMatthew Wilcox
1716b803b428SMatthew Wilcox if (unlikely(xas_not_node(node) || node->shift ||
1717b803b428SMatthew Wilcox xas->xa_offset != (xas->xa_index & XA_CHUNK_MASK)))
1718b803b428SMatthew Wilcox return xas_find(xas, max);
1719b803b428SMatthew Wilcox
1720b803b428SMatthew Wilcox do {
1721b803b428SMatthew Wilcox if (unlikely(xas->xa_index >= max))
1722b803b428SMatthew Wilcox return xas_find(xas, max);
1723b803b428SMatthew Wilcox if (unlikely(xas->xa_offset == XA_CHUNK_MASK))
1724b803b428SMatthew Wilcox return xas_find(xas, max);
1725b803b428SMatthew Wilcox entry = xa_entry(xas->xa, node, xas->xa_offset + 1);
1726b803b428SMatthew Wilcox if (unlikely(xa_is_internal(entry)))
1727b803b428SMatthew Wilcox return xas_find(xas, max);
1728b803b428SMatthew Wilcox xas->xa_offset++;
1729b803b428SMatthew Wilcox xas->xa_index++;
1730b803b428SMatthew Wilcox } while (!entry);
1731b803b428SMatthew Wilcox
1732b803b428SMatthew Wilcox return entry;
1733b803b428SMatthew Wilcox }
1734b803b428SMatthew Wilcox
1735b803b428SMatthew Wilcox /* Private */
xas_find_chunk(struct xa_state * xas,bool advance,xa_mark_t mark)1736b803b428SMatthew Wilcox static inline unsigned int xas_find_chunk(struct xa_state *xas, bool advance,
1737b803b428SMatthew Wilcox xa_mark_t mark)
1738b803b428SMatthew Wilcox {
1739b803b428SMatthew Wilcox unsigned long *addr = xas->xa_node->marks[(__force unsigned)mark];
1740b803b428SMatthew Wilcox unsigned int offset = xas->xa_offset;
1741b803b428SMatthew Wilcox
1742b803b428SMatthew Wilcox if (advance)
1743b803b428SMatthew Wilcox offset++;
1744b803b428SMatthew Wilcox if (XA_CHUNK_SIZE == BITS_PER_LONG) {
1745b803b428SMatthew Wilcox if (offset < XA_CHUNK_SIZE) {
1746b803b428SMatthew Wilcox unsigned long data = *addr & (~0UL << offset);
1747b803b428SMatthew Wilcox if (data)
1748b803b428SMatthew Wilcox return __ffs(data);
1749b803b428SMatthew Wilcox }
1750b803b428SMatthew Wilcox return XA_CHUNK_SIZE;
1751b803b428SMatthew Wilcox }
1752b803b428SMatthew Wilcox
1753b803b428SMatthew Wilcox return find_next_bit(addr, XA_CHUNK_SIZE, offset);
1754b803b428SMatthew Wilcox }
1755b803b428SMatthew Wilcox
1756b803b428SMatthew Wilcox /**
1757b803b428SMatthew Wilcox * xas_next_marked() - Advance iterator to next marked entry.
1758b803b428SMatthew Wilcox * @xas: XArray operation state.
1759b803b428SMatthew Wilcox * @max: Highest index to return.
1760b803b428SMatthew Wilcox * @mark: Mark to search for.
1761b803b428SMatthew Wilcox *
1762b803b428SMatthew Wilcox * xas_next_marked() is an inline function to optimise xarray traversal for
1763b803b428SMatthew Wilcox * speed. It is equivalent to calling xas_find_marked(), and will call
1764b803b428SMatthew Wilcox * xas_find_marked() for all the hard cases.
1765b803b428SMatthew Wilcox *
1766b803b428SMatthew Wilcox * Return: The next marked entry after the one currently referred to by @xas.
1767b803b428SMatthew Wilcox */
xas_next_marked(struct xa_state * xas,unsigned long max,xa_mark_t mark)1768b803b428SMatthew Wilcox static inline void *xas_next_marked(struct xa_state *xas, unsigned long max,
1769b803b428SMatthew Wilcox xa_mark_t mark)
1770b803b428SMatthew Wilcox {
1771b803b428SMatthew Wilcox struct xa_node *node = xas->xa_node;
17727e934cf5SMatthew Wilcox (Oracle) void *entry;
1773b803b428SMatthew Wilcox unsigned int offset;
1774b803b428SMatthew Wilcox
1775b803b428SMatthew Wilcox if (unlikely(xas_not_node(node) || node->shift))
1776b803b428SMatthew Wilcox return xas_find_marked(xas, max, mark);
1777b803b428SMatthew Wilcox offset = xas_find_chunk(xas, true, mark);
1778b803b428SMatthew Wilcox xas->xa_offset = offset;
1779b803b428SMatthew Wilcox xas->xa_index = (xas->xa_index & ~XA_CHUNK_MASK) + offset;
1780b803b428SMatthew Wilcox if (xas->xa_index > max)
1781b803b428SMatthew Wilcox return NULL;
1782b803b428SMatthew Wilcox if (offset == XA_CHUNK_SIZE)
1783b803b428SMatthew Wilcox return xas_find_marked(xas, max, mark);
17847e934cf5SMatthew Wilcox (Oracle) entry = xa_entry(xas->xa, node, offset);
17857e934cf5SMatthew Wilcox (Oracle) if (!entry)
17867e934cf5SMatthew Wilcox (Oracle) return xas_find_marked(xas, max, mark);
17877e934cf5SMatthew Wilcox (Oracle) return entry;
1788b803b428SMatthew Wilcox }
1789b803b428SMatthew Wilcox
1790b803b428SMatthew Wilcox /*
1791b803b428SMatthew Wilcox * If iterating while holding a lock, drop the lock and reschedule
1792b803b428SMatthew Wilcox * every %XA_CHECK_SCHED loops.
1793b803b428SMatthew Wilcox */
1794b803b428SMatthew Wilcox enum {
1795b803b428SMatthew Wilcox XA_CHECK_SCHED = 4096,
1796b803b428SMatthew Wilcox };
1797b803b428SMatthew Wilcox
1798b803b428SMatthew Wilcox /**
1799b803b428SMatthew Wilcox * xas_for_each() - Iterate over a range of an XArray.
1800b803b428SMatthew Wilcox * @xas: XArray operation state.
1801b803b428SMatthew Wilcox * @entry: Entry retrieved from the array.
1802b803b428SMatthew Wilcox * @max: Maximum index to retrieve from array.
1803b803b428SMatthew Wilcox *
1804b803b428SMatthew Wilcox * The loop body will be executed for each entry present in the xarray
1805b803b428SMatthew Wilcox * between the current xas position and @max. @entry will be set to
1806b803b428SMatthew Wilcox * the entry retrieved from the xarray. It is safe to delete entries
1807b803b428SMatthew Wilcox * from the array in the loop body. You should hold either the RCU lock
1808b803b428SMatthew Wilcox * or the xa_lock while iterating. If you need to drop the lock, call
1809b803b428SMatthew Wilcox * xas_pause() first.
1810b803b428SMatthew Wilcox */
1811b803b428SMatthew Wilcox #define xas_for_each(xas, entry, max) \
1812b803b428SMatthew Wilcox for (entry = xas_find(xas, max); entry; \
1813b803b428SMatthew Wilcox entry = xas_next_entry(xas, max))
1814b803b428SMatthew Wilcox
1815b803b428SMatthew Wilcox /**
1816b803b428SMatthew Wilcox * xas_for_each_marked() - Iterate over a range of an XArray.
1817b803b428SMatthew Wilcox * @xas: XArray operation state.
1818b803b428SMatthew Wilcox * @entry: Entry retrieved from the array.
1819b803b428SMatthew Wilcox * @max: Maximum index to retrieve from array.
1820b803b428SMatthew Wilcox * @mark: Mark to search for.
1821b803b428SMatthew Wilcox *
1822b803b428SMatthew Wilcox * The loop body will be executed for each marked entry in the xarray
1823b803b428SMatthew Wilcox * between the current xas position and @max. @entry will be set to
1824b803b428SMatthew Wilcox * the entry retrieved from the xarray. It is safe to delete entries
1825b803b428SMatthew Wilcox * from the array in the loop body. You should hold either the RCU lock
1826b803b428SMatthew Wilcox * or the xa_lock while iterating. If you need to drop the lock, call
1827b803b428SMatthew Wilcox * xas_pause() first.
1828b803b428SMatthew Wilcox */
1829b803b428SMatthew Wilcox #define xas_for_each_marked(xas, entry, max, mark) \
1830b803b428SMatthew Wilcox for (entry = xas_find_marked(xas, max, mark); entry; \
1831b803b428SMatthew Wilcox entry = xas_next_marked(xas, max, mark))
1832b803b428SMatthew Wilcox
18334e99d4e9SMatthew Wilcox /**
18344e99d4e9SMatthew Wilcox * xas_for_each_conflict() - Iterate over a range of an XArray.
18354e99d4e9SMatthew Wilcox * @xas: XArray operation state.
18364e99d4e9SMatthew Wilcox * @entry: Entry retrieved from the array.
18374e99d4e9SMatthew Wilcox *
18388446466cSMatthew Wilcox (Oracle) * The loop body will be executed for each entry in the XArray that
18398446466cSMatthew Wilcox (Oracle) * lies within the range specified by @xas. If the loop terminates
18408446466cSMatthew Wilcox (Oracle) * normally, @entry will be %NULL. The user may break out of the loop,
18418446466cSMatthew Wilcox (Oracle) * which will leave @entry set to the conflicting entry. The caller
18428446466cSMatthew Wilcox (Oracle) * may also call xa_set_err() to exit the loop while setting an error
18438446466cSMatthew Wilcox (Oracle) * to record the reason.
18444e99d4e9SMatthew Wilcox */
18454e99d4e9SMatthew Wilcox #define xas_for_each_conflict(xas, entry) \
18464e99d4e9SMatthew Wilcox while ((entry = xas_find_conflict(xas)))
18474e99d4e9SMatthew Wilcox
184864d3e9a9SMatthew Wilcox void *__xas_next(struct xa_state *);
184964d3e9a9SMatthew Wilcox void *__xas_prev(struct xa_state *);
185064d3e9a9SMatthew Wilcox
185164d3e9a9SMatthew Wilcox /**
185264d3e9a9SMatthew Wilcox * xas_prev() - Move iterator to previous index.
185364d3e9a9SMatthew Wilcox * @xas: XArray operation state.
185464d3e9a9SMatthew Wilcox *
185564d3e9a9SMatthew Wilcox * If the @xas was in an error state, it will remain in an error state
185664d3e9a9SMatthew Wilcox * and this function will return %NULL. If the @xas has never been walked,
185764d3e9a9SMatthew Wilcox * it will have the effect of calling xas_load(). Otherwise one will be
185864d3e9a9SMatthew Wilcox * subtracted from the index and the state will be walked to the correct
185964d3e9a9SMatthew Wilcox * location in the array for the next operation.
186064d3e9a9SMatthew Wilcox *
186164d3e9a9SMatthew Wilcox * If the iterator was referencing index 0, this function wraps
186264d3e9a9SMatthew Wilcox * around to %ULONG_MAX.
186364d3e9a9SMatthew Wilcox *
186464d3e9a9SMatthew Wilcox * Return: The entry at the new index. This may be %NULL or an internal
186564d3e9a9SMatthew Wilcox * entry.
186664d3e9a9SMatthew Wilcox */
xas_prev(struct xa_state * xas)186764d3e9a9SMatthew Wilcox static inline void *xas_prev(struct xa_state *xas)
186864d3e9a9SMatthew Wilcox {
186964d3e9a9SMatthew Wilcox struct xa_node *node = xas->xa_node;
187064d3e9a9SMatthew Wilcox
187164d3e9a9SMatthew Wilcox if (unlikely(xas_not_node(node) || node->shift ||
187264d3e9a9SMatthew Wilcox xas->xa_offset == 0))
187364d3e9a9SMatthew Wilcox return __xas_prev(xas);
187464d3e9a9SMatthew Wilcox
187564d3e9a9SMatthew Wilcox xas->xa_index--;
187664d3e9a9SMatthew Wilcox xas->xa_offset--;
187764d3e9a9SMatthew Wilcox return xa_entry(xas->xa, node, xas->xa_offset);
187864d3e9a9SMatthew Wilcox }
187964d3e9a9SMatthew Wilcox
188064d3e9a9SMatthew Wilcox /**
188164d3e9a9SMatthew Wilcox * xas_next() - Move state to next index.
188264d3e9a9SMatthew Wilcox * @xas: XArray operation state.
188364d3e9a9SMatthew Wilcox *
188464d3e9a9SMatthew Wilcox * If the @xas was in an error state, it will remain in an error state
188564d3e9a9SMatthew Wilcox * and this function will return %NULL. If the @xas has never been walked,
188664d3e9a9SMatthew Wilcox * it will have the effect of calling xas_load(). Otherwise one will be
188764d3e9a9SMatthew Wilcox * added to the index and the state will be walked to the correct
188864d3e9a9SMatthew Wilcox * location in the array for the next operation.
188964d3e9a9SMatthew Wilcox *
189064d3e9a9SMatthew Wilcox * If the iterator was referencing index %ULONG_MAX, this function wraps
189164d3e9a9SMatthew Wilcox * around to 0.
189264d3e9a9SMatthew Wilcox *
189364d3e9a9SMatthew Wilcox * Return: The entry at the new index. This may be %NULL or an internal
189464d3e9a9SMatthew Wilcox * entry.
189564d3e9a9SMatthew Wilcox */
xas_next(struct xa_state * xas)189664d3e9a9SMatthew Wilcox static inline void *xas_next(struct xa_state *xas)
189764d3e9a9SMatthew Wilcox {
189864d3e9a9SMatthew Wilcox struct xa_node *node = xas->xa_node;
189964d3e9a9SMatthew Wilcox
190064d3e9a9SMatthew Wilcox if (unlikely(xas_not_node(node) || node->shift ||
190164d3e9a9SMatthew Wilcox xas->xa_offset == XA_CHUNK_MASK))
190264d3e9a9SMatthew Wilcox return __xas_next(xas);
190364d3e9a9SMatthew Wilcox
190464d3e9a9SMatthew Wilcox xas->xa_index++;
190564d3e9a9SMatthew Wilcox xas->xa_offset++;
190664d3e9a9SMatthew Wilcox return xa_entry(xas->xa, node, xas->xa_offset);
190764d3e9a9SMatthew Wilcox }
190864d3e9a9SMatthew Wilcox
1909f6bb2a2cSMatthew Wilcox #endif /* _LINUX_XARRAY_H */
1910