1f6bb2a2cSMatthew Wilcox /* SPDX-License-Identifier: GPL-2.0+ */ 2f6bb2a2cSMatthew Wilcox #ifndef _LINUX_XARRAY_H 3f6bb2a2cSMatthew Wilcox #define _LINUX_XARRAY_H 4f6bb2a2cSMatthew Wilcox /* 5f6bb2a2cSMatthew Wilcox * eXtensible Arrays 6f6bb2a2cSMatthew Wilcox * Copyright (c) 2017 Microsoft Corporation 73d0186bbSMatthew Wilcox * Author: Matthew Wilcox <[email protected]> 83159f943SMatthew Wilcox * 93159f943SMatthew Wilcox * See Documentation/core-api/xarray.rst for how to use the XArray. 10f6bb2a2cSMatthew Wilcox */ 11f6bb2a2cSMatthew Wilcox 123159f943SMatthew Wilcox #include <linux/bug.h> 13f8d5d0ccSMatthew Wilcox #include <linux/compiler.h> 149b89a035SMatthew Wilcox #include <linux/gfp.h> 15f8d5d0ccSMatthew Wilcox #include <linux/kconfig.h> 16ad3d6c72SMatthew Wilcox #include <linux/kernel.h> 17ad3d6c72SMatthew Wilcox #include <linux/rcupdate.h> 18f6bb2a2cSMatthew Wilcox #include <linux/spinlock.h> 193159f943SMatthew Wilcox #include <linux/types.h> 203159f943SMatthew Wilcox 213159f943SMatthew Wilcox /* 223159f943SMatthew Wilcox * The bottom two bits of the entry determine how the XArray interprets 233159f943SMatthew Wilcox * the contents: 243159f943SMatthew Wilcox * 253159f943SMatthew Wilcox * 00: Pointer entry 263159f943SMatthew Wilcox * 10: Internal entry 273159f943SMatthew Wilcox * x1: Value entry or tagged pointer 283159f943SMatthew Wilcox * 293159f943SMatthew Wilcox * Attempting to store internal entries in the XArray is a bug. 3002c02bf1SMatthew Wilcox * 3102c02bf1SMatthew Wilcox * Most internal entries are pointers to the next node in the tree. 3202c02bf1SMatthew Wilcox * The following internal entries have a special meaning: 3302c02bf1SMatthew Wilcox * 3402c02bf1SMatthew Wilcox * 0-62: Sibling entries 359f14d4f1SMatthew Wilcox * 256: Zero entry 369f14d4f1SMatthew Wilcox * 257: Retry entry 37ad3d6c72SMatthew Wilcox * 38ad3d6c72SMatthew Wilcox * Errors are also represented as internal entries, but use the negative 39ad3d6c72SMatthew Wilcox * space (-4094 to -2). They're never stored in the slots array; only 40ad3d6c72SMatthew Wilcox * returned by the normal API. 413159f943SMatthew Wilcox */ 423159f943SMatthew Wilcox 433159f943SMatthew Wilcox #define BITS_PER_XA_VALUE (BITS_PER_LONG - 1) 443159f943SMatthew Wilcox 453159f943SMatthew Wilcox /** 463159f943SMatthew Wilcox * xa_mk_value() - Create an XArray entry from an integer. 473159f943SMatthew Wilcox * @v: Value to store in XArray. 483159f943SMatthew Wilcox * 493159f943SMatthew Wilcox * Context: Any context. 503159f943SMatthew Wilcox * Return: An entry suitable for storing in the XArray. 513159f943SMatthew Wilcox */ 523159f943SMatthew Wilcox static inline void *xa_mk_value(unsigned long v) 533159f943SMatthew Wilcox { 543159f943SMatthew Wilcox WARN_ON((long)v < 0); 553159f943SMatthew Wilcox return (void *)((v << 1) | 1); 563159f943SMatthew Wilcox } 573159f943SMatthew Wilcox 583159f943SMatthew Wilcox /** 593159f943SMatthew Wilcox * xa_to_value() - Get value stored in an XArray entry. 603159f943SMatthew Wilcox * @entry: XArray entry. 613159f943SMatthew Wilcox * 623159f943SMatthew Wilcox * Context: Any context. 633159f943SMatthew Wilcox * Return: The value stored in the XArray entry. 643159f943SMatthew Wilcox */ 653159f943SMatthew Wilcox static inline unsigned long xa_to_value(const void *entry) 663159f943SMatthew Wilcox { 673159f943SMatthew Wilcox return (unsigned long)entry >> 1; 683159f943SMatthew Wilcox } 693159f943SMatthew Wilcox 703159f943SMatthew Wilcox /** 713159f943SMatthew Wilcox * xa_is_value() - Determine if an entry is a value. 723159f943SMatthew Wilcox * @entry: XArray entry. 733159f943SMatthew Wilcox * 743159f943SMatthew Wilcox * Context: Any context. 753159f943SMatthew Wilcox * Return: True if the entry is a value, false if it is a pointer. 763159f943SMatthew Wilcox */ 773159f943SMatthew Wilcox static inline bool xa_is_value(const void *entry) 783159f943SMatthew Wilcox { 793159f943SMatthew Wilcox return (unsigned long)entry & 1; 803159f943SMatthew Wilcox } 813159f943SMatthew Wilcox 823159f943SMatthew Wilcox /** 833159f943SMatthew Wilcox * xa_tag_pointer() - Create an XArray entry for a tagged pointer. 843159f943SMatthew Wilcox * @p: Plain pointer. 853159f943SMatthew Wilcox * @tag: Tag value (0, 1 or 3). 863159f943SMatthew Wilcox * 873159f943SMatthew Wilcox * If the user of the XArray prefers, they can tag their pointers instead 883159f943SMatthew Wilcox * of storing value entries. Three tags are available (0, 1 and 3). 893159f943SMatthew Wilcox * These are distinct from the xa_mark_t as they are not replicated up 903159f943SMatthew Wilcox * through the array and cannot be searched for. 913159f943SMatthew Wilcox * 923159f943SMatthew Wilcox * Context: Any context. 933159f943SMatthew Wilcox * Return: An XArray entry. 943159f943SMatthew Wilcox */ 953159f943SMatthew Wilcox static inline void *xa_tag_pointer(void *p, unsigned long tag) 963159f943SMatthew Wilcox { 973159f943SMatthew Wilcox return (void *)((unsigned long)p | tag); 983159f943SMatthew Wilcox } 993159f943SMatthew Wilcox 1003159f943SMatthew Wilcox /** 1013159f943SMatthew Wilcox * xa_untag_pointer() - Turn an XArray entry into a plain pointer. 1023159f943SMatthew Wilcox * @entry: XArray entry. 1033159f943SMatthew Wilcox * 1043159f943SMatthew Wilcox * If you have stored a tagged pointer in the XArray, call this function 1053159f943SMatthew Wilcox * to get the untagged version of the pointer. 1063159f943SMatthew Wilcox * 1073159f943SMatthew Wilcox * Context: Any context. 1083159f943SMatthew Wilcox * Return: A pointer. 1093159f943SMatthew Wilcox */ 1103159f943SMatthew Wilcox static inline void *xa_untag_pointer(void *entry) 1113159f943SMatthew Wilcox { 1123159f943SMatthew Wilcox return (void *)((unsigned long)entry & ~3UL); 1133159f943SMatthew Wilcox } 1143159f943SMatthew Wilcox 1153159f943SMatthew Wilcox /** 1163159f943SMatthew Wilcox * xa_pointer_tag() - Get the tag stored in an XArray entry. 1173159f943SMatthew Wilcox * @entry: XArray entry. 1183159f943SMatthew Wilcox * 1193159f943SMatthew Wilcox * If you have stored a tagged pointer in the XArray, call this function 1203159f943SMatthew Wilcox * to get the tag of that pointer. 1213159f943SMatthew Wilcox * 1223159f943SMatthew Wilcox * Context: Any context. 1233159f943SMatthew Wilcox * Return: A tag. 1243159f943SMatthew Wilcox */ 1253159f943SMatthew Wilcox static inline unsigned int xa_pointer_tag(void *entry) 1263159f943SMatthew Wilcox { 1273159f943SMatthew Wilcox return (unsigned long)entry & 3UL; 1283159f943SMatthew Wilcox } 129f6bb2a2cSMatthew Wilcox 13002c02bf1SMatthew Wilcox /* 13102c02bf1SMatthew Wilcox * xa_mk_internal() - Create an internal entry. 13202c02bf1SMatthew Wilcox * @v: Value to turn into an internal entry. 13302c02bf1SMatthew Wilcox * 13402c02bf1SMatthew Wilcox * Context: Any context. 13502c02bf1SMatthew Wilcox * Return: An XArray internal entry corresponding to this value. 13602c02bf1SMatthew Wilcox */ 13702c02bf1SMatthew Wilcox static inline void *xa_mk_internal(unsigned long v) 13802c02bf1SMatthew Wilcox { 13902c02bf1SMatthew Wilcox return (void *)((v << 2) | 2); 14002c02bf1SMatthew Wilcox } 14102c02bf1SMatthew Wilcox 14202c02bf1SMatthew Wilcox /* 14302c02bf1SMatthew Wilcox * xa_to_internal() - Extract the value from an internal entry. 14402c02bf1SMatthew Wilcox * @entry: XArray entry. 14502c02bf1SMatthew Wilcox * 14602c02bf1SMatthew Wilcox * Context: Any context. 14702c02bf1SMatthew Wilcox * Return: The value which was stored in the internal entry. 14802c02bf1SMatthew Wilcox */ 14902c02bf1SMatthew Wilcox static inline unsigned long xa_to_internal(const void *entry) 15002c02bf1SMatthew Wilcox { 15102c02bf1SMatthew Wilcox return (unsigned long)entry >> 2; 15202c02bf1SMatthew Wilcox } 15302c02bf1SMatthew Wilcox 15402c02bf1SMatthew Wilcox /* 15502c02bf1SMatthew Wilcox * xa_is_internal() - Is the entry an internal entry? 15602c02bf1SMatthew Wilcox * @entry: XArray entry. 15702c02bf1SMatthew Wilcox * 15802c02bf1SMatthew Wilcox * Context: Any context. 15902c02bf1SMatthew Wilcox * Return: %true if the entry is an internal entry. 16002c02bf1SMatthew Wilcox */ 16102c02bf1SMatthew Wilcox static inline bool xa_is_internal(const void *entry) 16202c02bf1SMatthew Wilcox { 16302c02bf1SMatthew Wilcox return ((unsigned long)entry & 3) == 2; 16402c02bf1SMatthew Wilcox } 16502c02bf1SMatthew Wilcox 166f8d5d0ccSMatthew Wilcox /** 167ad3d6c72SMatthew Wilcox * xa_is_err() - Report whether an XArray operation returned an error 168ad3d6c72SMatthew Wilcox * @entry: Result from calling an XArray function 169ad3d6c72SMatthew Wilcox * 170ad3d6c72SMatthew Wilcox * If an XArray operation cannot complete an operation, it will return 171ad3d6c72SMatthew Wilcox * a special value indicating an error. This function tells you 172ad3d6c72SMatthew Wilcox * whether an error occurred; xa_err() tells you which error occurred. 173ad3d6c72SMatthew Wilcox * 174ad3d6c72SMatthew Wilcox * Context: Any context. 175ad3d6c72SMatthew Wilcox * Return: %true if the entry indicates an error. 176ad3d6c72SMatthew Wilcox */ 177ad3d6c72SMatthew Wilcox static inline bool xa_is_err(const void *entry) 178ad3d6c72SMatthew Wilcox { 17976b4e529SMatthew Wilcox return unlikely(xa_is_internal(entry) && 180edcddd4cSDan Carpenter entry >= xa_mk_internal(-MAX_ERRNO)); 181ad3d6c72SMatthew Wilcox } 182ad3d6c72SMatthew Wilcox 183ad3d6c72SMatthew Wilcox /** 184ad3d6c72SMatthew Wilcox * xa_err() - Turn an XArray result into an errno. 185ad3d6c72SMatthew Wilcox * @entry: Result from calling an XArray function. 186ad3d6c72SMatthew Wilcox * 187ad3d6c72SMatthew Wilcox * If an XArray operation cannot complete an operation, it will return 188ad3d6c72SMatthew Wilcox * a special pointer value which encodes an errno. This function extracts 189ad3d6c72SMatthew Wilcox * the errno from the pointer value, or returns 0 if the pointer does not 190ad3d6c72SMatthew Wilcox * represent an errno. 191ad3d6c72SMatthew Wilcox * 192ad3d6c72SMatthew Wilcox * Context: Any context. 193ad3d6c72SMatthew Wilcox * Return: A negative errno or 0. 194ad3d6c72SMatthew Wilcox */ 195ad3d6c72SMatthew Wilcox static inline int xa_err(void *entry) 196ad3d6c72SMatthew Wilcox { 197ad3d6c72SMatthew Wilcox /* xa_to_internal() would not do sign extension. */ 198ad3d6c72SMatthew Wilcox if (xa_is_err(entry)) 199ad3d6c72SMatthew Wilcox return (long)entry >> 2; 200ad3d6c72SMatthew Wilcox return 0; 201ad3d6c72SMatthew Wilcox } 202ad3d6c72SMatthew Wilcox 203a3e4d3f9SMatthew Wilcox /** 204a3e4d3f9SMatthew Wilcox * struct xa_limit - Represents a range of IDs. 205a3e4d3f9SMatthew Wilcox * @min: The lowest ID to allocate (inclusive). 206a3e4d3f9SMatthew Wilcox * @max: The maximum ID to allocate (inclusive). 207a3e4d3f9SMatthew Wilcox * 208a3e4d3f9SMatthew Wilcox * This structure is used either directly or via the XA_LIMIT() macro 209a3e4d3f9SMatthew Wilcox * to communicate the range of IDs that are valid for allocation. 210a3e4d3f9SMatthew Wilcox * Two common ranges are predefined for you: 211a3e4d3f9SMatthew Wilcox * * xa_limit_32b - [0 - UINT_MAX] 212a3e4d3f9SMatthew Wilcox * * xa_limit_31b - [0 - INT_MAX] 213a3e4d3f9SMatthew Wilcox */ 214a3e4d3f9SMatthew Wilcox struct xa_limit { 215a3e4d3f9SMatthew Wilcox u32 max; 216a3e4d3f9SMatthew Wilcox u32 min; 217a3e4d3f9SMatthew Wilcox }; 218a3e4d3f9SMatthew Wilcox 219a3e4d3f9SMatthew Wilcox #define XA_LIMIT(_min, _max) (struct xa_limit) { .min = _min, .max = _max } 220a3e4d3f9SMatthew Wilcox 221a3e4d3f9SMatthew Wilcox #define xa_limit_32b XA_LIMIT(0, UINT_MAX) 222a3e4d3f9SMatthew Wilcox #define xa_limit_31b XA_LIMIT(0, INT_MAX) 223a3e4d3f9SMatthew Wilcox 2249b89a035SMatthew Wilcox typedef unsigned __bitwise xa_mark_t; 2259b89a035SMatthew Wilcox #define XA_MARK_0 ((__force xa_mark_t)0U) 2269b89a035SMatthew Wilcox #define XA_MARK_1 ((__force xa_mark_t)1U) 2279b89a035SMatthew Wilcox #define XA_MARK_2 ((__force xa_mark_t)2U) 2289b89a035SMatthew Wilcox #define XA_PRESENT ((__force xa_mark_t)8U) 2299b89a035SMatthew Wilcox #define XA_MARK_MAX XA_MARK_2 230371c752dSMatthew Wilcox #define XA_FREE_MARK XA_MARK_0 2319b89a035SMatthew Wilcox 23258d6ea30SMatthew Wilcox enum xa_lock_type { 23358d6ea30SMatthew Wilcox XA_LOCK_IRQ = 1, 23458d6ea30SMatthew Wilcox XA_LOCK_BH = 2, 23558d6ea30SMatthew Wilcox }; 23658d6ea30SMatthew Wilcox 2379b89a035SMatthew Wilcox /* 2389b89a035SMatthew Wilcox * Values for xa_flags. The radix tree stores its GFP flags in the xa_flags, 2399b89a035SMatthew Wilcox * and we remain compatible with that. 2409b89a035SMatthew Wilcox */ 24158d6ea30SMatthew Wilcox #define XA_FLAGS_LOCK_IRQ ((__force gfp_t)XA_LOCK_IRQ) 24258d6ea30SMatthew Wilcox #define XA_FLAGS_LOCK_BH ((__force gfp_t)XA_LOCK_BH) 243371c752dSMatthew Wilcox #define XA_FLAGS_TRACK_FREE ((__force gfp_t)4U) 2443ccaf57aSMatthew Wilcox #define XA_FLAGS_ZERO_BUSY ((__force gfp_t)8U) 245*2fa044e5SMatthew Wilcox #define XA_FLAGS_ALLOC_WRAPPED ((__force gfp_t)16U) 2469b89a035SMatthew Wilcox #define XA_FLAGS_MARK(mark) ((__force gfp_t)((1U << __GFP_BITS_SHIFT) << \ 2479b89a035SMatthew Wilcox (__force unsigned)(mark))) 2489b89a035SMatthew Wilcox 2493ccaf57aSMatthew Wilcox /* ALLOC is for a normal 0-based alloc. ALLOC1 is for an 1-based alloc */ 250371c752dSMatthew Wilcox #define XA_FLAGS_ALLOC (XA_FLAGS_TRACK_FREE | XA_FLAGS_MARK(XA_FREE_MARK)) 2513ccaf57aSMatthew Wilcox #define XA_FLAGS_ALLOC1 (XA_FLAGS_TRACK_FREE | XA_FLAGS_ZERO_BUSY) 252371c752dSMatthew Wilcox 253ad3d6c72SMatthew Wilcox /** 254f8d5d0ccSMatthew Wilcox * struct xarray - The anchor of the XArray. 255f8d5d0ccSMatthew Wilcox * @xa_lock: Lock that protects the contents of the XArray. 256f8d5d0ccSMatthew Wilcox * 257f8d5d0ccSMatthew Wilcox * To use the xarray, define it statically or embed it in your data structure. 258f8d5d0ccSMatthew Wilcox * It is a very small data structure, so it does not usually make sense to 259f8d5d0ccSMatthew Wilcox * allocate it separately and keep a pointer to it in your data structure. 260f8d5d0ccSMatthew Wilcox * 261f8d5d0ccSMatthew Wilcox * You may use the xa_lock to protect your own data structures as well. 262f8d5d0ccSMatthew Wilcox */ 263f8d5d0ccSMatthew Wilcox /* 264f8d5d0ccSMatthew Wilcox * If all of the entries in the array are NULL, @xa_head is a NULL pointer. 265f8d5d0ccSMatthew Wilcox * If the only non-NULL entry in the array is at index 0, @xa_head is that 266f8d5d0ccSMatthew Wilcox * entry. If any other entry in the array is non-NULL, @xa_head points 267f8d5d0ccSMatthew Wilcox * to an @xa_node. 268f8d5d0ccSMatthew Wilcox */ 269f8d5d0ccSMatthew Wilcox struct xarray { 270f8d5d0ccSMatthew Wilcox spinlock_t xa_lock; 271f8d5d0ccSMatthew Wilcox /* private: The rest of the data structure is not to be used directly. */ 272f8d5d0ccSMatthew Wilcox gfp_t xa_flags; 273f8d5d0ccSMatthew Wilcox void __rcu * xa_head; 274f8d5d0ccSMatthew Wilcox }; 275f8d5d0ccSMatthew Wilcox 276f8d5d0ccSMatthew Wilcox #define XARRAY_INIT(name, flags) { \ 277f8d5d0ccSMatthew Wilcox .xa_lock = __SPIN_LOCK_UNLOCKED(name.xa_lock), \ 278f8d5d0ccSMatthew Wilcox .xa_flags = flags, \ 279f8d5d0ccSMatthew Wilcox .xa_head = NULL, \ 280f8d5d0ccSMatthew Wilcox } 281f8d5d0ccSMatthew Wilcox 282f8d5d0ccSMatthew Wilcox /** 283f8d5d0ccSMatthew Wilcox * DEFINE_XARRAY_FLAGS() - Define an XArray with custom flags. 284f8d5d0ccSMatthew Wilcox * @name: A string that names your XArray. 285f8d5d0ccSMatthew Wilcox * @flags: XA_FLAG values. 286f8d5d0ccSMatthew Wilcox * 287f8d5d0ccSMatthew Wilcox * This is intended for file scope definitions of XArrays. It declares 288f8d5d0ccSMatthew Wilcox * and initialises an empty XArray with the chosen name and flags. It is 289f8d5d0ccSMatthew Wilcox * equivalent to calling xa_init_flags() on the array, but it does the 290f8d5d0ccSMatthew Wilcox * initialisation at compiletime instead of runtime. 291f8d5d0ccSMatthew Wilcox */ 292f8d5d0ccSMatthew Wilcox #define DEFINE_XARRAY_FLAGS(name, flags) \ 293f8d5d0ccSMatthew Wilcox struct xarray name = XARRAY_INIT(name, flags) 294f8d5d0ccSMatthew Wilcox 295f8d5d0ccSMatthew Wilcox /** 296f8d5d0ccSMatthew Wilcox * DEFINE_XARRAY() - Define an XArray. 297f8d5d0ccSMatthew Wilcox * @name: A string that names your XArray. 298f8d5d0ccSMatthew Wilcox * 299f8d5d0ccSMatthew Wilcox * This is intended for file scope definitions of XArrays. It declares 300f8d5d0ccSMatthew Wilcox * and initialises an empty XArray with the chosen name. It is equivalent 301f8d5d0ccSMatthew Wilcox * to calling xa_init() on the array, but it does the initialisation at 302f8d5d0ccSMatthew Wilcox * compiletime instead of runtime. 303f8d5d0ccSMatthew Wilcox */ 304f8d5d0ccSMatthew Wilcox #define DEFINE_XARRAY(name) DEFINE_XARRAY_FLAGS(name, 0) 305f8d5d0ccSMatthew Wilcox 306371c752dSMatthew Wilcox /** 3073ccaf57aSMatthew Wilcox * DEFINE_XARRAY_ALLOC() - Define an XArray which allocates IDs starting at 0. 308371c752dSMatthew Wilcox * @name: A string that names your XArray. 309371c752dSMatthew Wilcox * 310371c752dSMatthew Wilcox * This is intended for file scope definitions of allocating XArrays. 311371c752dSMatthew Wilcox * See also DEFINE_XARRAY(). 312371c752dSMatthew Wilcox */ 313371c752dSMatthew Wilcox #define DEFINE_XARRAY_ALLOC(name) DEFINE_XARRAY_FLAGS(name, XA_FLAGS_ALLOC) 314371c752dSMatthew Wilcox 3153ccaf57aSMatthew Wilcox /** 3163ccaf57aSMatthew Wilcox * DEFINE_XARRAY_ALLOC1() - Define an XArray which allocates IDs starting at 1. 3173ccaf57aSMatthew Wilcox * @name: A string that names your XArray. 3183ccaf57aSMatthew Wilcox * 3193ccaf57aSMatthew Wilcox * This is intended for file scope definitions of allocating XArrays. 3203ccaf57aSMatthew Wilcox * See also DEFINE_XARRAY(). 3213ccaf57aSMatthew Wilcox */ 3223ccaf57aSMatthew Wilcox #define DEFINE_XARRAY_ALLOC1(name) DEFINE_XARRAY_FLAGS(name, XA_FLAGS_ALLOC1) 3233ccaf57aSMatthew Wilcox 324ad3d6c72SMatthew Wilcox void *xa_load(struct xarray *, unsigned long index); 32558d6ea30SMatthew Wilcox void *xa_store(struct xarray *, unsigned long index, void *entry, gfp_t); 3269c16bb88SMatthew Wilcox void *xa_erase(struct xarray *, unsigned long index); 3270e9446c3SMatthew Wilcox void *xa_store_range(struct xarray *, unsigned long first, unsigned long last, 3280e9446c3SMatthew Wilcox void *entry, gfp_t); 3299b89a035SMatthew Wilcox bool xa_get_mark(struct xarray *, unsigned long index, xa_mark_t); 3309b89a035SMatthew Wilcox void xa_set_mark(struct xarray *, unsigned long index, xa_mark_t); 3319b89a035SMatthew Wilcox void xa_clear_mark(struct xarray *, unsigned long index, xa_mark_t); 332b803b428SMatthew Wilcox void *xa_find(struct xarray *xa, unsigned long *index, 333b803b428SMatthew Wilcox unsigned long max, xa_mark_t) __attribute__((nonnull(2))); 334b803b428SMatthew Wilcox void *xa_find_after(struct xarray *xa, unsigned long *index, 335b803b428SMatthew Wilcox unsigned long max, xa_mark_t) __attribute__((nonnull(2))); 33680a0a1a9SMatthew Wilcox unsigned int xa_extract(struct xarray *, void **dst, unsigned long start, 33780a0a1a9SMatthew Wilcox unsigned long max, unsigned int n, xa_mark_t); 338687149fcSMatthew Wilcox void xa_destroy(struct xarray *); 339f8d5d0ccSMatthew Wilcox 340f8d5d0ccSMatthew Wilcox /** 34102669b17SMatthew Wilcox * xa_init_flags() - Initialise an empty XArray with flags. 34202669b17SMatthew Wilcox * @xa: XArray. 34302669b17SMatthew Wilcox * @flags: XA_FLAG values. 34402669b17SMatthew Wilcox * 34502669b17SMatthew Wilcox * If you need to initialise an XArray with special flags (eg you need 34602669b17SMatthew Wilcox * to take the lock from interrupt context), use this function instead 34702669b17SMatthew Wilcox * of xa_init(). 34802669b17SMatthew Wilcox * 34902669b17SMatthew Wilcox * Context: Any context. 35002669b17SMatthew Wilcox */ 35102669b17SMatthew Wilcox static inline void xa_init_flags(struct xarray *xa, gfp_t flags) 35202669b17SMatthew Wilcox { 35302669b17SMatthew Wilcox spin_lock_init(&xa->xa_lock); 35402669b17SMatthew Wilcox xa->xa_flags = flags; 35502669b17SMatthew Wilcox xa->xa_head = NULL; 35602669b17SMatthew Wilcox } 35702669b17SMatthew Wilcox 35802669b17SMatthew Wilcox /** 359f8d5d0ccSMatthew Wilcox * xa_init() - Initialise an empty XArray. 360f8d5d0ccSMatthew Wilcox * @xa: XArray. 361f8d5d0ccSMatthew Wilcox * 362f8d5d0ccSMatthew Wilcox * An empty XArray is full of NULL entries. 363f8d5d0ccSMatthew Wilcox * 364f8d5d0ccSMatthew Wilcox * Context: Any context. 365f8d5d0ccSMatthew Wilcox */ 366f8d5d0ccSMatthew Wilcox static inline void xa_init(struct xarray *xa) 367f8d5d0ccSMatthew Wilcox { 368f8d5d0ccSMatthew Wilcox xa_init_flags(xa, 0); 369f8d5d0ccSMatthew Wilcox } 370f8d5d0ccSMatthew Wilcox 371ad3d6c72SMatthew Wilcox /** 372ad3d6c72SMatthew Wilcox * xa_empty() - Determine if an array has any present entries. 373ad3d6c72SMatthew Wilcox * @xa: XArray. 374ad3d6c72SMatthew Wilcox * 375ad3d6c72SMatthew Wilcox * Context: Any context. 376ad3d6c72SMatthew Wilcox * Return: %true if the array contains only NULL pointers. 377ad3d6c72SMatthew Wilcox */ 378ad3d6c72SMatthew Wilcox static inline bool xa_empty(const struct xarray *xa) 379ad3d6c72SMatthew Wilcox { 380ad3d6c72SMatthew Wilcox return xa->xa_head == NULL; 381ad3d6c72SMatthew Wilcox } 382ad3d6c72SMatthew Wilcox 3839b89a035SMatthew Wilcox /** 3849b89a035SMatthew Wilcox * xa_marked() - Inquire whether any entry in this array has a mark set 3859b89a035SMatthew Wilcox * @xa: Array 3869b89a035SMatthew Wilcox * @mark: Mark value 3879b89a035SMatthew Wilcox * 3889b89a035SMatthew Wilcox * Context: Any context. 3899b89a035SMatthew Wilcox * Return: %true if any entry has this mark set. 3909b89a035SMatthew Wilcox */ 3919b89a035SMatthew Wilcox static inline bool xa_marked(const struct xarray *xa, xa_mark_t mark) 3929b89a035SMatthew Wilcox { 3939b89a035SMatthew Wilcox return xa->xa_flags & XA_FLAGS_MARK(mark); 3949b89a035SMatthew Wilcox } 3959b89a035SMatthew Wilcox 39658d6ea30SMatthew Wilcox /** 3974a31896cSMatthew Wilcox * xa_for_each_start() - Iterate over a portion of an XArray. 398b803b428SMatthew Wilcox * @xa: XArray. 399b803b428SMatthew Wilcox * @index: Index of @entry. 4004a31896cSMatthew Wilcox * @entry: Entry retrieved from array. 4014a31896cSMatthew Wilcox * @start: First index to retrieve from array. 402b803b428SMatthew Wilcox * 4034a31896cSMatthew Wilcox * During the iteration, @entry will have the value of the entry stored 4044a31896cSMatthew Wilcox * in @xa at @index. You may modify @index during the iteration if you 4054a31896cSMatthew Wilcox * want to skip or reprocess indices. It is safe to modify the array 4064a31896cSMatthew Wilcox * during the iteration. At the end of the iteration, @entry will be set 4074a31896cSMatthew Wilcox * to NULL and @index will have a value less than or equal to max. 4084a31896cSMatthew Wilcox * 4094a31896cSMatthew Wilcox * xa_for_each_start() is O(n.log(n)) while xas_for_each() is O(n). You have 4104a31896cSMatthew Wilcox * to handle your own locking with xas_for_each(), and if you have to unlock 4114a31896cSMatthew Wilcox * after each iteration, it will also end up being O(n.log(n)). 4124a31896cSMatthew Wilcox * xa_for_each_start() will spin if it hits a retry entry; if you intend to 4134a31896cSMatthew Wilcox * see retry entries, you should use the xas_for_each() iterator instead. 4144a31896cSMatthew Wilcox * The xas_for_each() iterator will expand into more inline code than 4154a31896cSMatthew Wilcox * xa_for_each_start(). 4164a31896cSMatthew Wilcox * 4174a31896cSMatthew Wilcox * Context: Any context. Takes and releases the RCU lock. 4184a31896cSMatthew Wilcox */ 4194a31896cSMatthew Wilcox #define xa_for_each_start(xa, index, entry, start) \ 4204a31896cSMatthew Wilcox for (index = start, \ 4214a31896cSMatthew Wilcox entry = xa_find(xa, &index, ULONG_MAX, XA_PRESENT); \ 4224a31896cSMatthew Wilcox entry; \ 4234a31896cSMatthew Wilcox entry = xa_find_after(xa, &index, ULONG_MAX, XA_PRESENT)) 4244a31896cSMatthew Wilcox 4254a31896cSMatthew Wilcox /** 4264a31896cSMatthew Wilcox * xa_for_each() - Iterate over present entries in an XArray. 4274a31896cSMatthew Wilcox * @xa: XArray. 4284a31896cSMatthew Wilcox * @index: Index of @entry. 4294a31896cSMatthew Wilcox * @entry: Entry retrieved from array. 4304a31896cSMatthew Wilcox * 4314a31896cSMatthew Wilcox * During the iteration, @entry will have the value of the entry stored 4324a31896cSMatthew Wilcox * in @xa at @index. You may modify @index during the iteration if you want 4334a31896cSMatthew Wilcox * to skip or reprocess indices. It is safe to modify the array during the 4344a31896cSMatthew Wilcox * iteration. At the end of the iteration, @entry will be set to NULL and 4354a31896cSMatthew Wilcox * @index will have a value less than or equal to max. 436b803b428SMatthew Wilcox * 437b803b428SMatthew Wilcox * xa_for_each() is O(n.log(n)) while xas_for_each() is O(n). You have 438b803b428SMatthew Wilcox * to handle your own locking with xas_for_each(), and if you have to unlock 439b803b428SMatthew Wilcox * after each iteration, it will also end up being O(n.log(n)). xa_for_each() 440b803b428SMatthew Wilcox * will spin if it hits a retry entry; if you intend to see retry entries, 441b803b428SMatthew Wilcox * you should use the xas_for_each() iterator instead. The xas_for_each() 442b803b428SMatthew Wilcox * iterator will expand into more inline code than xa_for_each(). 443b803b428SMatthew Wilcox * 444b803b428SMatthew Wilcox * Context: Any context. Takes and releases the RCU lock. 445b803b428SMatthew Wilcox */ 4464a31896cSMatthew Wilcox #define xa_for_each(xa, index, entry) \ 4474a31896cSMatthew Wilcox xa_for_each_start(xa, index, entry, 0) 4484a31896cSMatthew Wilcox 4494a31896cSMatthew Wilcox /** 4504a31896cSMatthew Wilcox * xa_for_each_marked() - Iterate over marked entries in an XArray. 4514a31896cSMatthew Wilcox * @xa: XArray. 4524a31896cSMatthew Wilcox * @index: Index of @entry. 4534a31896cSMatthew Wilcox * @entry: Entry retrieved from array. 4544a31896cSMatthew Wilcox * @filter: Selection criterion. 4554a31896cSMatthew Wilcox * 4564a31896cSMatthew Wilcox * During the iteration, @entry will have the value of the entry stored 4574a31896cSMatthew Wilcox * in @xa at @index. The iteration will skip all entries in the array 4584a31896cSMatthew Wilcox * which do not match @filter. You may modify @index during the iteration 4594a31896cSMatthew Wilcox * if you want to skip or reprocess indices. It is safe to modify the array 4604a31896cSMatthew Wilcox * during the iteration. At the end of the iteration, @entry will be set to 4614a31896cSMatthew Wilcox * NULL and @index will have a value less than or equal to max. 4624a31896cSMatthew Wilcox * 4634a31896cSMatthew Wilcox * xa_for_each_marked() is O(n.log(n)) while xas_for_each_marked() is O(n). 4644a31896cSMatthew Wilcox * You have to handle your own locking with xas_for_each(), and if you have 4654a31896cSMatthew Wilcox * to unlock after each iteration, it will also end up being O(n.log(n)). 4664a31896cSMatthew Wilcox * xa_for_each_marked() will spin if it hits a retry entry; if you intend to 4674a31896cSMatthew Wilcox * see retry entries, you should use the xas_for_each_marked() iterator 4684a31896cSMatthew Wilcox * instead. The xas_for_each_marked() iterator will expand into more inline 4694a31896cSMatthew Wilcox * code than xa_for_each_marked(). 4704a31896cSMatthew Wilcox * 4714a31896cSMatthew Wilcox * Context: Any context. Takes and releases the RCU lock. 4724a31896cSMatthew Wilcox */ 4734a31896cSMatthew Wilcox #define xa_for_each_marked(xa, index, entry, filter) \ 4744a31896cSMatthew Wilcox for (index = 0, entry = xa_find(xa, &index, ULONG_MAX, filter); \ 4754a31896cSMatthew Wilcox entry; entry = xa_find_after(xa, &index, ULONG_MAX, filter)) 476b803b428SMatthew Wilcox 477f6bb2a2cSMatthew Wilcox #define xa_trylock(xa) spin_trylock(&(xa)->xa_lock) 478f6bb2a2cSMatthew Wilcox #define xa_lock(xa) spin_lock(&(xa)->xa_lock) 479f6bb2a2cSMatthew Wilcox #define xa_unlock(xa) spin_unlock(&(xa)->xa_lock) 480f6bb2a2cSMatthew Wilcox #define xa_lock_bh(xa) spin_lock_bh(&(xa)->xa_lock) 481f6bb2a2cSMatthew Wilcox #define xa_unlock_bh(xa) spin_unlock_bh(&(xa)->xa_lock) 482f6bb2a2cSMatthew Wilcox #define xa_lock_irq(xa) spin_lock_irq(&(xa)->xa_lock) 483f6bb2a2cSMatthew Wilcox #define xa_unlock_irq(xa) spin_unlock_irq(&(xa)->xa_lock) 484f6bb2a2cSMatthew Wilcox #define xa_lock_irqsave(xa, flags) \ 485f6bb2a2cSMatthew Wilcox spin_lock_irqsave(&(xa)->xa_lock, flags) 486f6bb2a2cSMatthew Wilcox #define xa_unlock_irqrestore(xa, flags) \ 487f6bb2a2cSMatthew Wilcox spin_unlock_irqrestore(&(xa)->xa_lock, flags) 488f6bb2a2cSMatthew Wilcox 4899b89a035SMatthew Wilcox /* 49058d6ea30SMatthew Wilcox * Versions of the normal API which require the caller to hold the 49158d6ea30SMatthew Wilcox * xa_lock. If the GFP flags allow it, they will drop the lock to 49258d6ea30SMatthew Wilcox * allocate memory, then reacquire it afterwards. These functions 49358d6ea30SMatthew Wilcox * may also re-enable interrupts if the XArray flags indicate the 49458d6ea30SMatthew Wilcox * locking should be interrupt safe. 4959b89a035SMatthew Wilcox */ 49658d6ea30SMatthew Wilcox void *__xa_erase(struct xarray *, unsigned long index); 49758d6ea30SMatthew Wilcox void *__xa_store(struct xarray *, unsigned long index, void *entry, gfp_t); 49841aec91fSMatthew Wilcox void *__xa_cmpxchg(struct xarray *, unsigned long index, void *old, 49941aec91fSMatthew Wilcox void *entry, gfp_t); 500b0606fedSMatthew Wilcox int __xa_insert(struct xarray *, unsigned long index, void *entry, gfp_t); 501a3e4d3f9SMatthew Wilcox int __must_check __xa_alloc(struct xarray *, u32 *id, void *entry, 502a3e4d3f9SMatthew Wilcox struct xa_limit, gfp_t); 503*2fa044e5SMatthew Wilcox int __must_check __xa_alloc_cyclic(struct xarray *, u32 *id, void *entry, 504*2fa044e5SMatthew Wilcox struct xa_limit, u32 *next, gfp_t); 5054c0608f4SMatthew Wilcox int __xa_reserve(struct xarray *, unsigned long index, gfp_t); 5069b89a035SMatthew Wilcox void __xa_set_mark(struct xarray *, unsigned long index, xa_mark_t); 5079b89a035SMatthew Wilcox void __xa_clear_mark(struct xarray *, unsigned long index, xa_mark_t); 5089b89a035SMatthew Wilcox 50958d6ea30SMatthew Wilcox /** 51084e5acb7SMatthew Wilcox * xa_store_bh() - Store this entry in the XArray. 51184e5acb7SMatthew Wilcox * @xa: XArray. 51284e5acb7SMatthew Wilcox * @index: Index into array. 51384e5acb7SMatthew Wilcox * @entry: New entry. 51484e5acb7SMatthew Wilcox * @gfp: Memory allocation flags. 51584e5acb7SMatthew Wilcox * 51684e5acb7SMatthew Wilcox * This function is like calling xa_store() except it disables softirqs 51784e5acb7SMatthew Wilcox * while holding the array lock. 51884e5acb7SMatthew Wilcox * 51984e5acb7SMatthew Wilcox * Context: Any context. Takes and releases the xa_lock while 52084e5acb7SMatthew Wilcox * disabling softirqs. 52184e5acb7SMatthew Wilcox * Return: The entry which used to be at this index. 52284e5acb7SMatthew Wilcox */ 52384e5acb7SMatthew Wilcox static inline void *xa_store_bh(struct xarray *xa, unsigned long index, 52484e5acb7SMatthew Wilcox void *entry, gfp_t gfp) 52584e5acb7SMatthew Wilcox { 52684e5acb7SMatthew Wilcox void *curr; 52784e5acb7SMatthew Wilcox 52884e5acb7SMatthew Wilcox xa_lock_bh(xa); 52984e5acb7SMatthew Wilcox curr = __xa_store(xa, index, entry, gfp); 53084e5acb7SMatthew Wilcox xa_unlock_bh(xa); 53184e5acb7SMatthew Wilcox 53284e5acb7SMatthew Wilcox return curr; 53384e5acb7SMatthew Wilcox } 53484e5acb7SMatthew Wilcox 53584e5acb7SMatthew Wilcox /** 53619ba9ecfSCyrill Gorcunov * xa_store_irq() - Store this entry in the XArray. 53784e5acb7SMatthew Wilcox * @xa: XArray. 53884e5acb7SMatthew Wilcox * @index: Index into array. 53984e5acb7SMatthew Wilcox * @entry: New entry. 54084e5acb7SMatthew Wilcox * @gfp: Memory allocation flags. 54184e5acb7SMatthew Wilcox * 54284e5acb7SMatthew Wilcox * This function is like calling xa_store() except it disables interrupts 54384e5acb7SMatthew Wilcox * while holding the array lock. 54484e5acb7SMatthew Wilcox * 54584e5acb7SMatthew Wilcox * Context: Process context. Takes and releases the xa_lock while 54684e5acb7SMatthew Wilcox * disabling interrupts. 54784e5acb7SMatthew Wilcox * Return: The entry which used to be at this index. 54884e5acb7SMatthew Wilcox */ 54984e5acb7SMatthew Wilcox static inline void *xa_store_irq(struct xarray *xa, unsigned long index, 55084e5acb7SMatthew Wilcox void *entry, gfp_t gfp) 55184e5acb7SMatthew Wilcox { 55284e5acb7SMatthew Wilcox void *curr; 55384e5acb7SMatthew Wilcox 55484e5acb7SMatthew Wilcox xa_lock_irq(xa); 55584e5acb7SMatthew Wilcox curr = __xa_store(xa, index, entry, gfp); 55684e5acb7SMatthew Wilcox xa_unlock_irq(xa); 55784e5acb7SMatthew Wilcox 55884e5acb7SMatthew Wilcox return curr; 55984e5acb7SMatthew Wilcox } 56084e5acb7SMatthew Wilcox 56184e5acb7SMatthew Wilcox /** 56258d6ea30SMatthew Wilcox * xa_erase_bh() - Erase this entry from the XArray. 56358d6ea30SMatthew Wilcox * @xa: XArray. 56458d6ea30SMatthew Wilcox * @index: Index of entry. 56558d6ea30SMatthew Wilcox * 566809ab937SMatthew Wilcox * After this function returns, loading from @index will return %NULL. 567809ab937SMatthew Wilcox * If the index is part of a multi-index entry, all indices will be erased 568809ab937SMatthew Wilcox * and none of the entries will be part of a multi-index entry. 56958d6ea30SMatthew Wilcox * 570804dfaf0SMatthew Wilcox * Context: Any context. Takes and releases the xa_lock while 57158d6ea30SMatthew Wilcox * disabling softirqs. 57258d6ea30SMatthew Wilcox * Return: The entry which used to be at this index. 57358d6ea30SMatthew Wilcox */ 57458d6ea30SMatthew Wilcox static inline void *xa_erase_bh(struct xarray *xa, unsigned long index) 57558d6ea30SMatthew Wilcox { 57658d6ea30SMatthew Wilcox void *entry; 57758d6ea30SMatthew Wilcox 57858d6ea30SMatthew Wilcox xa_lock_bh(xa); 57958d6ea30SMatthew Wilcox entry = __xa_erase(xa, index); 58058d6ea30SMatthew Wilcox xa_unlock_bh(xa); 58158d6ea30SMatthew Wilcox 58258d6ea30SMatthew Wilcox return entry; 58358d6ea30SMatthew Wilcox } 58458d6ea30SMatthew Wilcox 58558d6ea30SMatthew Wilcox /** 58658d6ea30SMatthew Wilcox * xa_erase_irq() - Erase this entry from the XArray. 58758d6ea30SMatthew Wilcox * @xa: XArray. 58858d6ea30SMatthew Wilcox * @index: Index of entry. 58958d6ea30SMatthew Wilcox * 590809ab937SMatthew Wilcox * After this function returns, loading from @index will return %NULL. 591809ab937SMatthew Wilcox * If the index is part of a multi-index entry, all indices will be erased 592809ab937SMatthew Wilcox * and none of the entries will be part of a multi-index entry. 59358d6ea30SMatthew Wilcox * 59458d6ea30SMatthew Wilcox * Context: Process context. Takes and releases the xa_lock while 59558d6ea30SMatthew Wilcox * disabling interrupts. 59658d6ea30SMatthew Wilcox * Return: The entry which used to be at this index. 59758d6ea30SMatthew Wilcox */ 59858d6ea30SMatthew Wilcox static inline void *xa_erase_irq(struct xarray *xa, unsigned long index) 59958d6ea30SMatthew Wilcox { 60058d6ea30SMatthew Wilcox void *entry; 60158d6ea30SMatthew Wilcox 60258d6ea30SMatthew Wilcox xa_lock_irq(xa); 60358d6ea30SMatthew Wilcox entry = __xa_erase(xa, index); 60458d6ea30SMatthew Wilcox xa_unlock_irq(xa); 60558d6ea30SMatthew Wilcox 60658d6ea30SMatthew Wilcox return entry; 60758d6ea30SMatthew Wilcox } 60858d6ea30SMatthew Wilcox 609371c752dSMatthew Wilcox /** 610c5beb07eSMatthew Wilcox * xa_cmpxchg() - Conditionally replace an entry in the XArray. 611c5beb07eSMatthew Wilcox * @xa: XArray. 612c5beb07eSMatthew Wilcox * @index: Index into array. 613c5beb07eSMatthew Wilcox * @old: Old value to test against. 614c5beb07eSMatthew Wilcox * @entry: New value to place in array. 615c5beb07eSMatthew Wilcox * @gfp: Memory allocation flags. 616c5beb07eSMatthew Wilcox * 617c5beb07eSMatthew Wilcox * If the entry at @index is the same as @old, replace it with @entry. 618c5beb07eSMatthew Wilcox * If the return value is equal to @old, then the exchange was successful. 619c5beb07eSMatthew Wilcox * 620c5beb07eSMatthew Wilcox * Context: Any context. Takes and releases the xa_lock. May sleep 621c5beb07eSMatthew Wilcox * if the @gfp flags permit. 622c5beb07eSMatthew Wilcox * Return: The old value at this index or xa_err() if an error happened. 623c5beb07eSMatthew Wilcox */ 624c5beb07eSMatthew Wilcox static inline void *xa_cmpxchg(struct xarray *xa, unsigned long index, 625c5beb07eSMatthew Wilcox void *old, void *entry, gfp_t gfp) 626c5beb07eSMatthew Wilcox { 627c5beb07eSMatthew Wilcox void *curr; 628c5beb07eSMatthew Wilcox 629c5beb07eSMatthew Wilcox xa_lock(xa); 630c5beb07eSMatthew Wilcox curr = __xa_cmpxchg(xa, index, old, entry, gfp); 631c5beb07eSMatthew Wilcox xa_unlock(xa); 632c5beb07eSMatthew Wilcox 633c5beb07eSMatthew Wilcox return curr; 634c5beb07eSMatthew Wilcox } 635c5beb07eSMatthew Wilcox 636c5beb07eSMatthew Wilcox /** 63755f3f7eaSMatthew Wilcox * xa_cmpxchg_bh() - Conditionally replace an entry in the XArray. 63855f3f7eaSMatthew Wilcox * @xa: XArray. 63955f3f7eaSMatthew Wilcox * @index: Index into array. 64055f3f7eaSMatthew Wilcox * @old: Old value to test against. 64155f3f7eaSMatthew Wilcox * @entry: New value to place in array. 64255f3f7eaSMatthew Wilcox * @gfp: Memory allocation flags. 64355f3f7eaSMatthew Wilcox * 64455f3f7eaSMatthew Wilcox * This function is like calling xa_cmpxchg() except it disables softirqs 64555f3f7eaSMatthew Wilcox * while holding the array lock. 64655f3f7eaSMatthew Wilcox * 64755f3f7eaSMatthew Wilcox * Context: Any context. Takes and releases the xa_lock while 64855f3f7eaSMatthew Wilcox * disabling softirqs. May sleep if the @gfp flags permit. 64955f3f7eaSMatthew Wilcox * Return: The old value at this index or xa_err() if an error happened. 65055f3f7eaSMatthew Wilcox */ 65155f3f7eaSMatthew Wilcox static inline void *xa_cmpxchg_bh(struct xarray *xa, unsigned long index, 65255f3f7eaSMatthew Wilcox void *old, void *entry, gfp_t gfp) 65355f3f7eaSMatthew Wilcox { 65455f3f7eaSMatthew Wilcox void *curr; 65555f3f7eaSMatthew Wilcox 65655f3f7eaSMatthew Wilcox xa_lock_bh(xa); 65755f3f7eaSMatthew Wilcox curr = __xa_cmpxchg(xa, index, old, entry, gfp); 65855f3f7eaSMatthew Wilcox xa_unlock_bh(xa); 65955f3f7eaSMatthew Wilcox 66055f3f7eaSMatthew Wilcox return curr; 66155f3f7eaSMatthew Wilcox } 66255f3f7eaSMatthew Wilcox 66355f3f7eaSMatthew Wilcox /** 66455f3f7eaSMatthew Wilcox * xa_cmpxchg_irq() - Conditionally replace an entry in the XArray. 66555f3f7eaSMatthew Wilcox * @xa: XArray. 66655f3f7eaSMatthew Wilcox * @index: Index into array. 66755f3f7eaSMatthew Wilcox * @old: Old value to test against. 66855f3f7eaSMatthew Wilcox * @entry: New value to place in array. 66955f3f7eaSMatthew Wilcox * @gfp: Memory allocation flags. 67055f3f7eaSMatthew Wilcox * 67155f3f7eaSMatthew Wilcox * This function is like calling xa_cmpxchg() except it disables interrupts 67255f3f7eaSMatthew Wilcox * while holding the array lock. 67355f3f7eaSMatthew Wilcox * 67455f3f7eaSMatthew Wilcox * Context: Process context. Takes and releases the xa_lock while 67555f3f7eaSMatthew Wilcox * disabling interrupts. May sleep if the @gfp flags permit. 67655f3f7eaSMatthew Wilcox * Return: The old value at this index or xa_err() if an error happened. 67755f3f7eaSMatthew Wilcox */ 67855f3f7eaSMatthew Wilcox static inline void *xa_cmpxchg_irq(struct xarray *xa, unsigned long index, 67955f3f7eaSMatthew Wilcox void *old, void *entry, gfp_t gfp) 68055f3f7eaSMatthew Wilcox { 68155f3f7eaSMatthew Wilcox void *curr; 68255f3f7eaSMatthew Wilcox 68355f3f7eaSMatthew Wilcox xa_lock_irq(xa); 68455f3f7eaSMatthew Wilcox curr = __xa_cmpxchg(xa, index, old, entry, gfp); 68555f3f7eaSMatthew Wilcox xa_unlock_irq(xa); 68655f3f7eaSMatthew Wilcox 68755f3f7eaSMatthew Wilcox return curr; 68855f3f7eaSMatthew Wilcox } 68955f3f7eaSMatthew Wilcox 69055f3f7eaSMatthew Wilcox /** 691c5beb07eSMatthew Wilcox * xa_insert() - Store this entry in the XArray unless another entry is 692c5beb07eSMatthew Wilcox * already present. 693c5beb07eSMatthew Wilcox * @xa: XArray. 694c5beb07eSMatthew Wilcox * @index: Index into array. 695c5beb07eSMatthew Wilcox * @entry: New entry. 696c5beb07eSMatthew Wilcox * @gfp: Memory allocation flags. 697c5beb07eSMatthew Wilcox * 698b0606fedSMatthew Wilcox * Inserting a NULL entry will store a reserved entry (like xa_reserve()) 699b0606fedSMatthew Wilcox * if no entry is present. Inserting will fail if a reserved entry is 700b0606fedSMatthew Wilcox * present, even though loading from this index will return NULL. 701c5beb07eSMatthew Wilcox * 702b0606fedSMatthew Wilcox * Context: Any context. Takes and releases the xa_lock. May sleep if 703b0606fedSMatthew Wilcox * the @gfp flags permit. 704fd9dc93eSMatthew Wilcox * Return: 0 if the store succeeded. -EBUSY if another entry was present. 705c5beb07eSMatthew Wilcox * -ENOMEM if memory could not be allocated. 706c5beb07eSMatthew Wilcox */ 707c5beb07eSMatthew Wilcox static inline int xa_insert(struct xarray *xa, unsigned long index, 708c5beb07eSMatthew Wilcox void *entry, gfp_t gfp) 709c5beb07eSMatthew Wilcox { 710b0606fedSMatthew Wilcox int err; 711b0606fedSMatthew Wilcox 712b0606fedSMatthew Wilcox xa_lock(xa); 713b0606fedSMatthew Wilcox err = __xa_insert(xa, index, entry, gfp); 714b0606fedSMatthew Wilcox xa_unlock(xa); 715b0606fedSMatthew Wilcox 716b0606fedSMatthew Wilcox return err; 717b0606fedSMatthew Wilcox } 718b0606fedSMatthew Wilcox 719b0606fedSMatthew Wilcox /** 720b0606fedSMatthew Wilcox * xa_insert_bh() - Store this entry in the XArray unless another entry is 721b0606fedSMatthew Wilcox * already present. 722b0606fedSMatthew Wilcox * @xa: XArray. 723b0606fedSMatthew Wilcox * @index: Index into array. 724b0606fedSMatthew Wilcox * @entry: New entry. 725b0606fedSMatthew Wilcox * @gfp: Memory allocation flags. 726b0606fedSMatthew Wilcox * 727b0606fedSMatthew Wilcox * Inserting a NULL entry will store a reserved entry (like xa_reserve()) 728b0606fedSMatthew Wilcox * if no entry is present. Inserting will fail if a reserved entry is 729b0606fedSMatthew Wilcox * present, even though loading from this index will return NULL. 730b0606fedSMatthew Wilcox * 731b0606fedSMatthew Wilcox * Context: Any context. Takes and releases the xa_lock while 732b0606fedSMatthew Wilcox * disabling softirqs. May sleep if the @gfp flags permit. 733fd9dc93eSMatthew Wilcox * Return: 0 if the store succeeded. -EBUSY if another entry was present. 734b0606fedSMatthew Wilcox * -ENOMEM if memory could not be allocated. 735b0606fedSMatthew Wilcox */ 736b0606fedSMatthew Wilcox static inline int xa_insert_bh(struct xarray *xa, unsigned long index, 737b0606fedSMatthew Wilcox void *entry, gfp_t gfp) 738b0606fedSMatthew Wilcox { 739b0606fedSMatthew Wilcox int err; 740b0606fedSMatthew Wilcox 741b0606fedSMatthew Wilcox xa_lock_bh(xa); 742b0606fedSMatthew Wilcox err = __xa_insert(xa, index, entry, gfp); 743b0606fedSMatthew Wilcox xa_unlock_bh(xa); 744b0606fedSMatthew Wilcox 745b0606fedSMatthew Wilcox return err; 746b0606fedSMatthew Wilcox } 747b0606fedSMatthew Wilcox 748b0606fedSMatthew Wilcox /** 749b0606fedSMatthew Wilcox * xa_insert_irq() - Store this entry in the XArray unless another entry is 750b0606fedSMatthew Wilcox * already present. 751b0606fedSMatthew Wilcox * @xa: XArray. 752b0606fedSMatthew Wilcox * @index: Index into array. 753b0606fedSMatthew Wilcox * @entry: New entry. 754b0606fedSMatthew Wilcox * @gfp: Memory allocation flags. 755b0606fedSMatthew Wilcox * 756b0606fedSMatthew Wilcox * Inserting a NULL entry will store a reserved entry (like xa_reserve()) 757b0606fedSMatthew Wilcox * if no entry is present. Inserting will fail if a reserved entry is 758b0606fedSMatthew Wilcox * present, even though loading from this index will return NULL. 759b0606fedSMatthew Wilcox * 760b0606fedSMatthew Wilcox * Context: Process context. Takes and releases the xa_lock while 761b0606fedSMatthew Wilcox * disabling interrupts. May sleep if the @gfp flags permit. 762fd9dc93eSMatthew Wilcox * Return: 0 if the store succeeded. -EBUSY if another entry was present. 763b0606fedSMatthew Wilcox * -ENOMEM if memory could not be allocated. 764b0606fedSMatthew Wilcox */ 765b0606fedSMatthew Wilcox static inline int xa_insert_irq(struct xarray *xa, unsigned long index, 766b0606fedSMatthew Wilcox void *entry, gfp_t gfp) 767b0606fedSMatthew Wilcox { 768b0606fedSMatthew Wilcox int err; 769b0606fedSMatthew Wilcox 770b0606fedSMatthew Wilcox xa_lock_irq(xa); 771b0606fedSMatthew Wilcox err = __xa_insert(xa, index, entry, gfp); 772b0606fedSMatthew Wilcox xa_unlock_irq(xa); 773b0606fedSMatthew Wilcox 774b0606fedSMatthew Wilcox return err; 775c5beb07eSMatthew Wilcox } 776c5beb07eSMatthew Wilcox 777c5beb07eSMatthew Wilcox /** 778371c752dSMatthew Wilcox * xa_alloc() - Find somewhere to store this entry in the XArray. 779371c752dSMatthew Wilcox * @xa: XArray. 780371c752dSMatthew Wilcox * @id: Pointer to ID. 781371c752dSMatthew Wilcox * @entry: New entry. 782a3e4d3f9SMatthew Wilcox * @limit: Range of ID to allocate. 783371c752dSMatthew Wilcox * @gfp: Memory allocation flags. 784371c752dSMatthew Wilcox * 785a3e4d3f9SMatthew Wilcox * Finds an empty entry in @xa between @limit.min and @limit.max, 786a3e4d3f9SMatthew Wilcox * stores the index into the @id pointer, then stores the entry at 787a3e4d3f9SMatthew Wilcox * that index. A concurrent lookup will not see an uninitialised @id. 788371c752dSMatthew Wilcox * 789a3e4d3f9SMatthew Wilcox * Context: Any context. Takes and releases the xa_lock. May sleep if 790371c752dSMatthew Wilcox * the @gfp flags permit. 791a3e4d3f9SMatthew Wilcox * Return: 0 on success, -ENOMEM if memory could not be allocated or 792a3e4d3f9SMatthew Wilcox * -EBUSY if there are no free entries in @limit. 793371c752dSMatthew Wilcox */ 794a3e4d3f9SMatthew Wilcox static inline __must_check int xa_alloc(struct xarray *xa, u32 *id, 795a3e4d3f9SMatthew Wilcox void *entry, struct xa_limit limit, gfp_t gfp) 796371c752dSMatthew Wilcox { 797371c752dSMatthew Wilcox int err; 798371c752dSMatthew Wilcox 799371c752dSMatthew Wilcox xa_lock(xa); 800a3e4d3f9SMatthew Wilcox err = __xa_alloc(xa, id, entry, limit, gfp); 801371c752dSMatthew Wilcox xa_unlock(xa); 802371c752dSMatthew Wilcox 803371c752dSMatthew Wilcox return err; 804371c752dSMatthew Wilcox } 805371c752dSMatthew Wilcox 806371c752dSMatthew Wilcox /** 807371c752dSMatthew Wilcox * xa_alloc_bh() - Find somewhere to store this entry in the XArray. 808371c752dSMatthew Wilcox * @xa: XArray. 809371c752dSMatthew Wilcox * @id: Pointer to ID. 810371c752dSMatthew Wilcox * @entry: New entry. 811a3e4d3f9SMatthew Wilcox * @limit: Range of ID to allocate. 812371c752dSMatthew Wilcox * @gfp: Memory allocation flags. 813371c752dSMatthew Wilcox * 814a3e4d3f9SMatthew Wilcox * Finds an empty entry in @xa between @limit.min and @limit.max, 815a3e4d3f9SMatthew Wilcox * stores the index into the @id pointer, then stores the entry at 816a3e4d3f9SMatthew Wilcox * that index. A concurrent lookup will not see an uninitialised @id. 817371c752dSMatthew Wilcox * 818804dfaf0SMatthew Wilcox * Context: Any context. Takes and releases the xa_lock while 819371c752dSMatthew Wilcox * disabling softirqs. May sleep if the @gfp flags permit. 820a3e4d3f9SMatthew Wilcox * Return: 0 on success, -ENOMEM if memory could not be allocated or 821a3e4d3f9SMatthew Wilcox * -EBUSY if there are no free entries in @limit. 822371c752dSMatthew Wilcox */ 823a3e4d3f9SMatthew Wilcox static inline int __must_check xa_alloc_bh(struct xarray *xa, u32 *id, 824a3e4d3f9SMatthew Wilcox void *entry, struct xa_limit limit, gfp_t gfp) 825371c752dSMatthew Wilcox { 826371c752dSMatthew Wilcox int err; 827371c752dSMatthew Wilcox 828371c752dSMatthew Wilcox xa_lock_bh(xa); 829a3e4d3f9SMatthew Wilcox err = __xa_alloc(xa, id, entry, limit, gfp); 830371c752dSMatthew Wilcox xa_unlock_bh(xa); 831371c752dSMatthew Wilcox 832371c752dSMatthew Wilcox return err; 833371c752dSMatthew Wilcox } 834371c752dSMatthew Wilcox 835371c752dSMatthew Wilcox /** 836371c752dSMatthew Wilcox * xa_alloc_irq() - Find somewhere to store this entry in the XArray. 837371c752dSMatthew Wilcox * @xa: XArray. 838371c752dSMatthew Wilcox * @id: Pointer to ID. 839371c752dSMatthew Wilcox * @entry: New entry. 840a3e4d3f9SMatthew Wilcox * @limit: Range of ID to allocate. 841371c752dSMatthew Wilcox * @gfp: Memory allocation flags. 842371c752dSMatthew Wilcox * 843a3e4d3f9SMatthew Wilcox * Finds an empty entry in @xa between @limit.min and @limit.max, 844a3e4d3f9SMatthew Wilcox * stores the index into the @id pointer, then stores the entry at 845a3e4d3f9SMatthew Wilcox * that index. A concurrent lookup will not see an uninitialised @id. 846371c752dSMatthew Wilcox * 847371c752dSMatthew Wilcox * Context: Process context. Takes and releases the xa_lock while 848371c752dSMatthew Wilcox * disabling interrupts. May sleep if the @gfp flags permit. 849a3e4d3f9SMatthew Wilcox * Return: 0 on success, -ENOMEM if memory could not be allocated or 850a3e4d3f9SMatthew Wilcox * -EBUSY if there are no free entries in @limit. 851371c752dSMatthew Wilcox */ 852a3e4d3f9SMatthew Wilcox static inline int __must_check xa_alloc_irq(struct xarray *xa, u32 *id, 853a3e4d3f9SMatthew Wilcox void *entry, struct xa_limit limit, gfp_t gfp) 854371c752dSMatthew Wilcox { 855371c752dSMatthew Wilcox int err; 856371c752dSMatthew Wilcox 857371c752dSMatthew Wilcox xa_lock_irq(xa); 858a3e4d3f9SMatthew Wilcox err = __xa_alloc(xa, id, entry, limit, gfp); 859371c752dSMatthew Wilcox xa_unlock_irq(xa); 860371c752dSMatthew Wilcox 861371c752dSMatthew Wilcox return err; 862371c752dSMatthew Wilcox } 863371c752dSMatthew Wilcox 8644c0608f4SMatthew Wilcox /** 865*2fa044e5SMatthew Wilcox * xa_alloc_cyclic() - Find somewhere to store this entry in the XArray. 866*2fa044e5SMatthew Wilcox * @xa: XArray. 867*2fa044e5SMatthew Wilcox * @id: Pointer to ID. 868*2fa044e5SMatthew Wilcox * @entry: New entry. 869*2fa044e5SMatthew Wilcox * @limit: Range of allocated ID. 870*2fa044e5SMatthew Wilcox * @next: Pointer to next ID to allocate. 871*2fa044e5SMatthew Wilcox * @gfp: Memory allocation flags. 872*2fa044e5SMatthew Wilcox * 873*2fa044e5SMatthew Wilcox * Finds an empty entry in @xa between @limit.min and @limit.max, 874*2fa044e5SMatthew Wilcox * stores the index into the @id pointer, then stores the entry at 875*2fa044e5SMatthew Wilcox * that index. A concurrent lookup will not see an uninitialised @id. 876*2fa044e5SMatthew Wilcox * The search for an empty entry will start at @next and will wrap 877*2fa044e5SMatthew Wilcox * around if necessary. 878*2fa044e5SMatthew Wilcox * 879*2fa044e5SMatthew Wilcox * Context: Any context. Takes and releases the xa_lock. May sleep if 880*2fa044e5SMatthew Wilcox * the @gfp flags permit. 881*2fa044e5SMatthew Wilcox * Return: 0 if the allocation succeeded without wrapping. 1 if the 882*2fa044e5SMatthew Wilcox * allocation succeeded after wrapping, -ENOMEM if memory could not be 883*2fa044e5SMatthew Wilcox * allocated or -EBUSY if there are no free entries in @limit. 884*2fa044e5SMatthew Wilcox */ 885*2fa044e5SMatthew Wilcox static inline int xa_alloc_cyclic(struct xarray *xa, u32 *id, void *entry, 886*2fa044e5SMatthew Wilcox struct xa_limit limit, u32 *next, gfp_t gfp) 887*2fa044e5SMatthew Wilcox { 888*2fa044e5SMatthew Wilcox int err; 889*2fa044e5SMatthew Wilcox 890*2fa044e5SMatthew Wilcox xa_lock(xa); 891*2fa044e5SMatthew Wilcox err = __xa_alloc_cyclic(xa, id, entry, limit, next, gfp); 892*2fa044e5SMatthew Wilcox xa_unlock(xa); 893*2fa044e5SMatthew Wilcox 894*2fa044e5SMatthew Wilcox return err; 895*2fa044e5SMatthew Wilcox } 896*2fa044e5SMatthew Wilcox 897*2fa044e5SMatthew Wilcox /** 898*2fa044e5SMatthew Wilcox * xa_alloc_cyclic_bh() - Find somewhere to store this entry in the XArray. 899*2fa044e5SMatthew Wilcox * @xa: XArray. 900*2fa044e5SMatthew Wilcox * @id: Pointer to ID. 901*2fa044e5SMatthew Wilcox * @entry: New entry. 902*2fa044e5SMatthew Wilcox * @limit: Range of allocated ID. 903*2fa044e5SMatthew Wilcox * @next: Pointer to next ID to allocate. 904*2fa044e5SMatthew Wilcox * @gfp: Memory allocation flags. 905*2fa044e5SMatthew Wilcox * 906*2fa044e5SMatthew Wilcox * Finds an empty entry in @xa between @limit.min and @limit.max, 907*2fa044e5SMatthew Wilcox * stores the index into the @id pointer, then stores the entry at 908*2fa044e5SMatthew Wilcox * that index. A concurrent lookup will not see an uninitialised @id. 909*2fa044e5SMatthew Wilcox * The search for an empty entry will start at @next and will wrap 910*2fa044e5SMatthew Wilcox * around if necessary. 911*2fa044e5SMatthew Wilcox * 912*2fa044e5SMatthew Wilcox * Context: Any context. Takes and releases the xa_lock while 913*2fa044e5SMatthew Wilcox * disabling softirqs. May sleep if the @gfp flags permit. 914*2fa044e5SMatthew Wilcox * Return: 0 if the allocation succeeded without wrapping. 1 if the 915*2fa044e5SMatthew Wilcox * allocation succeeded after wrapping, -ENOMEM if memory could not be 916*2fa044e5SMatthew Wilcox * allocated or -EBUSY if there are no free entries in @limit. 917*2fa044e5SMatthew Wilcox */ 918*2fa044e5SMatthew Wilcox static inline int xa_alloc_cyclic_bh(struct xarray *xa, u32 *id, void *entry, 919*2fa044e5SMatthew Wilcox struct xa_limit limit, u32 *next, gfp_t gfp) 920*2fa044e5SMatthew Wilcox { 921*2fa044e5SMatthew Wilcox int err; 922*2fa044e5SMatthew Wilcox 923*2fa044e5SMatthew Wilcox xa_lock_bh(xa); 924*2fa044e5SMatthew Wilcox err = __xa_alloc_cyclic(xa, id, entry, limit, next, gfp); 925*2fa044e5SMatthew Wilcox xa_unlock_bh(xa); 926*2fa044e5SMatthew Wilcox 927*2fa044e5SMatthew Wilcox return err; 928*2fa044e5SMatthew Wilcox } 929*2fa044e5SMatthew Wilcox 930*2fa044e5SMatthew Wilcox /** 931*2fa044e5SMatthew Wilcox * xa_alloc_cyclic_irq() - Find somewhere to store this entry in the XArray. 932*2fa044e5SMatthew Wilcox * @xa: XArray. 933*2fa044e5SMatthew Wilcox * @id: Pointer to ID. 934*2fa044e5SMatthew Wilcox * @entry: New entry. 935*2fa044e5SMatthew Wilcox * @limit: Range of allocated ID. 936*2fa044e5SMatthew Wilcox * @next: Pointer to next ID to allocate. 937*2fa044e5SMatthew Wilcox * @gfp: Memory allocation flags. 938*2fa044e5SMatthew Wilcox * 939*2fa044e5SMatthew Wilcox * Finds an empty entry in @xa between @limit.min and @limit.max, 940*2fa044e5SMatthew Wilcox * stores the index into the @id pointer, then stores the entry at 941*2fa044e5SMatthew Wilcox * that index. A concurrent lookup will not see an uninitialised @id. 942*2fa044e5SMatthew Wilcox * The search for an empty entry will start at @next and will wrap 943*2fa044e5SMatthew Wilcox * around if necessary. 944*2fa044e5SMatthew Wilcox * 945*2fa044e5SMatthew Wilcox * Context: Process context. Takes and releases the xa_lock while 946*2fa044e5SMatthew Wilcox * disabling interrupts. May sleep if the @gfp flags permit. 947*2fa044e5SMatthew Wilcox * Return: 0 if the allocation succeeded without wrapping. 1 if the 948*2fa044e5SMatthew Wilcox * allocation succeeded after wrapping, -ENOMEM if memory could not be 949*2fa044e5SMatthew Wilcox * allocated or -EBUSY if there are no free entries in @limit. 950*2fa044e5SMatthew Wilcox */ 951*2fa044e5SMatthew Wilcox static inline int xa_alloc_cyclic_irq(struct xarray *xa, u32 *id, void *entry, 952*2fa044e5SMatthew Wilcox struct xa_limit limit, u32 *next, gfp_t gfp) 953*2fa044e5SMatthew Wilcox { 954*2fa044e5SMatthew Wilcox int err; 955*2fa044e5SMatthew Wilcox 956*2fa044e5SMatthew Wilcox xa_lock_irq(xa); 957*2fa044e5SMatthew Wilcox err = __xa_alloc_cyclic(xa, id, entry, limit, next, gfp); 958*2fa044e5SMatthew Wilcox xa_unlock_irq(xa); 959*2fa044e5SMatthew Wilcox 960*2fa044e5SMatthew Wilcox return err; 961*2fa044e5SMatthew Wilcox } 962*2fa044e5SMatthew Wilcox 963*2fa044e5SMatthew Wilcox /** 9644c0608f4SMatthew Wilcox * xa_reserve() - Reserve this index in the XArray. 9654c0608f4SMatthew Wilcox * @xa: XArray. 9664c0608f4SMatthew Wilcox * @index: Index into array. 9674c0608f4SMatthew Wilcox * @gfp: Memory allocation flags. 9684c0608f4SMatthew Wilcox * 9694c0608f4SMatthew Wilcox * Ensures there is somewhere to store an entry at @index in the array. 9704c0608f4SMatthew Wilcox * If there is already something stored at @index, this function does 9714c0608f4SMatthew Wilcox * nothing. If there was nothing there, the entry is marked as reserved. 9724c0608f4SMatthew Wilcox * Loading from a reserved entry returns a %NULL pointer. 9734c0608f4SMatthew Wilcox * 9744c0608f4SMatthew Wilcox * If you do not use the entry that you have reserved, call xa_release() 9754c0608f4SMatthew Wilcox * or xa_erase() to free any unnecessary memory. 9764c0608f4SMatthew Wilcox * 9774c0608f4SMatthew Wilcox * Context: Any context. Takes and releases the xa_lock. 9784c0608f4SMatthew Wilcox * May sleep if the @gfp flags permit. 9794c0608f4SMatthew Wilcox * Return: 0 if the reservation succeeded or -ENOMEM if it failed. 9804c0608f4SMatthew Wilcox */ 9814c0608f4SMatthew Wilcox static inline 9824c0608f4SMatthew Wilcox int xa_reserve(struct xarray *xa, unsigned long index, gfp_t gfp) 9834c0608f4SMatthew Wilcox { 9844c0608f4SMatthew Wilcox int ret; 9854c0608f4SMatthew Wilcox 9864c0608f4SMatthew Wilcox xa_lock(xa); 9874c0608f4SMatthew Wilcox ret = __xa_reserve(xa, index, gfp); 9884c0608f4SMatthew Wilcox xa_unlock(xa); 9894c0608f4SMatthew Wilcox 9904c0608f4SMatthew Wilcox return ret; 9914c0608f4SMatthew Wilcox } 9924c0608f4SMatthew Wilcox 9934c0608f4SMatthew Wilcox /** 9944c0608f4SMatthew Wilcox * xa_reserve_bh() - Reserve this index in the XArray. 9954c0608f4SMatthew Wilcox * @xa: XArray. 9964c0608f4SMatthew Wilcox * @index: Index into array. 9974c0608f4SMatthew Wilcox * @gfp: Memory allocation flags. 9984c0608f4SMatthew Wilcox * 9994c0608f4SMatthew Wilcox * A softirq-disabling version of xa_reserve(). 10004c0608f4SMatthew Wilcox * 10014c0608f4SMatthew Wilcox * Context: Any context. Takes and releases the xa_lock while 10024c0608f4SMatthew Wilcox * disabling softirqs. 10034c0608f4SMatthew Wilcox * Return: 0 if the reservation succeeded or -ENOMEM if it failed. 10044c0608f4SMatthew Wilcox */ 10054c0608f4SMatthew Wilcox static inline 10064c0608f4SMatthew Wilcox int xa_reserve_bh(struct xarray *xa, unsigned long index, gfp_t gfp) 10074c0608f4SMatthew Wilcox { 10084c0608f4SMatthew Wilcox int ret; 10094c0608f4SMatthew Wilcox 10104c0608f4SMatthew Wilcox xa_lock_bh(xa); 10114c0608f4SMatthew Wilcox ret = __xa_reserve(xa, index, gfp); 10124c0608f4SMatthew Wilcox xa_unlock_bh(xa); 10134c0608f4SMatthew Wilcox 10144c0608f4SMatthew Wilcox return ret; 10154c0608f4SMatthew Wilcox } 10164c0608f4SMatthew Wilcox 10174c0608f4SMatthew Wilcox /** 10184c0608f4SMatthew Wilcox * xa_reserve_irq() - Reserve this index in the XArray. 10194c0608f4SMatthew Wilcox * @xa: XArray. 10204c0608f4SMatthew Wilcox * @index: Index into array. 10214c0608f4SMatthew Wilcox * @gfp: Memory allocation flags. 10224c0608f4SMatthew Wilcox * 10234c0608f4SMatthew Wilcox * An interrupt-disabling version of xa_reserve(). 10244c0608f4SMatthew Wilcox * 10254c0608f4SMatthew Wilcox * Context: Process context. Takes and releases the xa_lock while 10264c0608f4SMatthew Wilcox * disabling interrupts. 10274c0608f4SMatthew Wilcox * Return: 0 if the reservation succeeded or -ENOMEM if it failed. 10284c0608f4SMatthew Wilcox */ 10294c0608f4SMatthew Wilcox static inline 10304c0608f4SMatthew Wilcox int xa_reserve_irq(struct xarray *xa, unsigned long index, gfp_t gfp) 10314c0608f4SMatthew Wilcox { 10324c0608f4SMatthew Wilcox int ret; 10334c0608f4SMatthew Wilcox 10344c0608f4SMatthew Wilcox xa_lock_irq(xa); 10354c0608f4SMatthew Wilcox ret = __xa_reserve(xa, index, gfp); 10364c0608f4SMatthew Wilcox xa_unlock_irq(xa); 10374c0608f4SMatthew Wilcox 10384c0608f4SMatthew Wilcox return ret; 10394c0608f4SMatthew Wilcox } 10404c0608f4SMatthew Wilcox 1041c5beb07eSMatthew Wilcox /** 1042c5beb07eSMatthew Wilcox * xa_release() - Release a reserved entry. 1043c5beb07eSMatthew Wilcox * @xa: XArray. 1044c5beb07eSMatthew Wilcox * @index: Index of entry. 1045c5beb07eSMatthew Wilcox * 1046c5beb07eSMatthew Wilcox * After calling xa_reserve(), you can call this function to release the 1047c5beb07eSMatthew Wilcox * reservation. If the entry at @index has been stored to, this function 1048c5beb07eSMatthew Wilcox * will do nothing. 1049c5beb07eSMatthew Wilcox */ 1050c5beb07eSMatthew Wilcox static inline void xa_release(struct xarray *xa, unsigned long index) 1051c5beb07eSMatthew Wilcox { 1052c5beb07eSMatthew Wilcox xa_cmpxchg(xa, index, NULL, NULL, 0); 1053c5beb07eSMatthew Wilcox } 1054c5beb07eSMatthew Wilcox 105502c02bf1SMatthew Wilcox /* Everything below here is the Advanced API. Proceed with caution. */ 105602c02bf1SMatthew Wilcox 105702c02bf1SMatthew Wilcox /* 105802c02bf1SMatthew Wilcox * The xarray is constructed out of a set of 'chunks' of pointers. Choosing 105902c02bf1SMatthew Wilcox * the best chunk size requires some tradeoffs. A power of two recommends 106002c02bf1SMatthew Wilcox * itself so that we can walk the tree based purely on shifts and masks. 106102c02bf1SMatthew Wilcox * Generally, the larger the better; as the number of slots per level of the 106202c02bf1SMatthew Wilcox * tree increases, the less tall the tree needs to be. But that needs to be 106302c02bf1SMatthew Wilcox * balanced against the memory consumption of each node. On a 64-bit system, 106402c02bf1SMatthew Wilcox * xa_node is currently 576 bytes, and we get 7 of them per 4kB page. If we 106502c02bf1SMatthew Wilcox * doubled the number of slots per node, we'd get only 3 nodes per 4kB page. 106602c02bf1SMatthew Wilcox */ 106702c02bf1SMatthew Wilcox #ifndef XA_CHUNK_SHIFT 106802c02bf1SMatthew Wilcox #define XA_CHUNK_SHIFT (CONFIG_BASE_SMALL ? 4 : 6) 106902c02bf1SMatthew Wilcox #endif 107002c02bf1SMatthew Wilcox #define XA_CHUNK_SIZE (1UL << XA_CHUNK_SHIFT) 107102c02bf1SMatthew Wilcox #define XA_CHUNK_MASK (XA_CHUNK_SIZE - 1) 107201959dfeSMatthew Wilcox #define XA_MAX_MARKS 3 107301959dfeSMatthew Wilcox #define XA_MARK_LONGS DIV_ROUND_UP(XA_CHUNK_SIZE, BITS_PER_LONG) 107401959dfeSMatthew Wilcox 107501959dfeSMatthew Wilcox /* 107601959dfeSMatthew Wilcox * @count is the count of every non-NULL element in the ->slots array 107701959dfeSMatthew Wilcox * whether that is a value entry, a retry entry, a user pointer, 107801959dfeSMatthew Wilcox * a sibling entry or a pointer to the next level of the tree. 107901959dfeSMatthew Wilcox * @nr_values is the count of every element in ->slots which is 108001959dfeSMatthew Wilcox * either a value entry or a sibling of a value entry. 108101959dfeSMatthew Wilcox */ 108201959dfeSMatthew Wilcox struct xa_node { 108301959dfeSMatthew Wilcox unsigned char shift; /* Bits remaining in each slot */ 108401959dfeSMatthew Wilcox unsigned char offset; /* Slot offset in parent */ 108501959dfeSMatthew Wilcox unsigned char count; /* Total entry count */ 108601959dfeSMatthew Wilcox unsigned char nr_values; /* Value entry count */ 108701959dfeSMatthew Wilcox struct xa_node __rcu *parent; /* NULL at top of tree */ 108801959dfeSMatthew Wilcox struct xarray *array; /* The array we belong to */ 108901959dfeSMatthew Wilcox union { 109001959dfeSMatthew Wilcox struct list_head private_list; /* For tree user */ 109101959dfeSMatthew Wilcox struct rcu_head rcu_head; /* Used when freeing node */ 109201959dfeSMatthew Wilcox }; 109301959dfeSMatthew Wilcox void __rcu *slots[XA_CHUNK_SIZE]; 109401959dfeSMatthew Wilcox union { 109501959dfeSMatthew Wilcox unsigned long tags[XA_MAX_MARKS][XA_MARK_LONGS]; 109601959dfeSMatthew Wilcox unsigned long marks[XA_MAX_MARKS][XA_MARK_LONGS]; 109701959dfeSMatthew Wilcox }; 109801959dfeSMatthew Wilcox }; 109902c02bf1SMatthew Wilcox 1100ad3d6c72SMatthew Wilcox void xa_dump(const struct xarray *); 1101ad3d6c72SMatthew Wilcox void xa_dump_node(const struct xa_node *); 1102ad3d6c72SMatthew Wilcox 1103ad3d6c72SMatthew Wilcox #ifdef XA_DEBUG 1104ad3d6c72SMatthew Wilcox #define XA_BUG_ON(xa, x) do { \ 1105ad3d6c72SMatthew Wilcox if (x) { \ 1106ad3d6c72SMatthew Wilcox xa_dump(xa); \ 1107ad3d6c72SMatthew Wilcox BUG(); \ 1108ad3d6c72SMatthew Wilcox } \ 1109ad3d6c72SMatthew Wilcox } while (0) 1110ad3d6c72SMatthew Wilcox #define XA_NODE_BUG_ON(node, x) do { \ 1111ad3d6c72SMatthew Wilcox if (x) { \ 1112ad3d6c72SMatthew Wilcox if (node) xa_dump_node(node); \ 1113ad3d6c72SMatthew Wilcox BUG(); \ 1114ad3d6c72SMatthew Wilcox } \ 1115ad3d6c72SMatthew Wilcox } while (0) 1116ad3d6c72SMatthew Wilcox #else 1117ad3d6c72SMatthew Wilcox #define XA_BUG_ON(xa, x) do { } while (0) 1118ad3d6c72SMatthew Wilcox #define XA_NODE_BUG_ON(node, x) do { } while (0) 1119ad3d6c72SMatthew Wilcox #endif 1120ad3d6c72SMatthew Wilcox 1121ad3d6c72SMatthew Wilcox /* Private */ 1122ad3d6c72SMatthew Wilcox static inline void *xa_head(const struct xarray *xa) 1123ad3d6c72SMatthew Wilcox { 1124ad3d6c72SMatthew Wilcox return rcu_dereference_check(xa->xa_head, 1125ad3d6c72SMatthew Wilcox lockdep_is_held(&xa->xa_lock)); 1126ad3d6c72SMatthew Wilcox } 1127ad3d6c72SMatthew Wilcox 1128ad3d6c72SMatthew Wilcox /* Private */ 1129ad3d6c72SMatthew Wilcox static inline void *xa_head_locked(const struct xarray *xa) 1130ad3d6c72SMatthew Wilcox { 1131ad3d6c72SMatthew Wilcox return rcu_dereference_protected(xa->xa_head, 1132ad3d6c72SMatthew Wilcox lockdep_is_held(&xa->xa_lock)); 1133ad3d6c72SMatthew Wilcox } 1134ad3d6c72SMatthew Wilcox 1135ad3d6c72SMatthew Wilcox /* Private */ 1136ad3d6c72SMatthew Wilcox static inline void *xa_entry(const struct xarray *xa, 1137ad3d6c72SMatthew Wilcox const struct xa_node *node, unsigned int offset) 1138ad3d6c72SMatthew Wilcox { 1139ad3d6c72SMatthew Wilcox XA_NODE_BUG_ON(node, offset >= XA_CHUNK_SIZE); 1140ad3d6c72SMatthew Wilcox return rcu_dereference_check(node->slots[offset], 1141ad3d6c72SMatthew Wilcox lockdep_is_held(&xa->xa_lock)); 1142ad3d6c72SMatthew Wilcox } 1143ad3d6c72SMatthew Wilcox 1144ad3d6c72SMatthew Wilcox /* Private */ 1145ad3d6c72SMatthew Wilcox static inline void *xa_entry_locked(const struct xarray *xa, 1146ad3d6c72SMatthew Wilcox const struct xa_node *node, unsigned int offset) 1147ad3d6c72SMatthew Wilcox { 1148ad3d6c72SMatthew Wilcox XA_NODE_BUG_ON(node, offset >= XA_CHUNK_SIZE); 1149ad3d6c72SMatthew Wilcox return rcu_dereference_protected(node->slots[offset], 1150ad3d6c72SMatthew Wilcox lockdep_is_held(&xa->xa_lock)); 1151ad3d6c72SMatthew Wilcox } 1152ad3d6c72SMatthew Wilcox 1153ad3d6c72SMatthew Wilcox /* Private */ 11549b89a035SMatthew Wilcox static inline struct xa_node *xa_parent(const struct xarray *xa, 11559b89a035SMatthew Wilcox const struct xa_node *node) 11569b89a035SMatthew Wilcox { 11579b89a035SMatthew Wilcox return rcu_dereference_check(node->parent, 11589b89a035SMatthew Wilcox lockdep_is_held(&xa->xa_lock)); 11599b89a035SMatthew Wilcox } 11609b89a035SMatthew Wilcox 11619b89a035SMatthew Wilcox /* Private */ 11629b89a035SMatthew Wilcox static inline struct xa_node *xa_parent_locked(const struct xarray *xa, 11639b89a035SMatthew Wilcox const struct xa_node *node) 11649b89a035SMatthew Wilcox { 11659b89a035SMatthew Wilcox return rcu_dereference_protected(node->parent, 11669b89a035SMatthew Wilcox lockdep_is_held(&xa->xa_lock)); 11679b89a035SMatthew Wilcox } 11689b89a035SMatthew Wilcox 11699b89a035SMatthew Wilcox /* Private */ 117058d6ea30SMatthew Wilcox static inline void *xa_mk_node(const struct xa_node *node) 117158d6ea30SMatthew Wilcox { 117258d6ea30SMatthew Wilcox return (void *)((unsigned long)node | 2); 117358d6ea30SMatthew Wilcox } 117458d6ea30SMatthew Wilcox 117558d6ea30SMatthew Wilcox /* Private */ 1176ad3d6c72SMatthew Wilcox static inline struct xa_node *xa_to_node(const void *entry) 1177ad3d6c72SMatthew Wilcox { 1178ad3d6c72SMatthew Wilcox return (struct xa_node *)((unsigned long)entry - 2); 1179ad3d6c72SMatthew Wilcox } 1180ad3d6c72SMatthew Wilcox 118102c02bf1SMatthew Wilcox /* Private */ 118202c02bf1SMatthew Wilcox static inline bool xa_is_node(const void *entry) 118302c02bf1SMatthew Wilcox { 118402c02bf1SMatthew Wilcox return xa_is_internal(entry) && (unsigned long)entry > 4096; 118502c02bf1SMatthew Wilcox } 118602c02bf1SMatthew Wilcox 118702c02bf1SMatthew Wilcox /* Private */ 118802c02bf1SMatthew Wilcox static inline void *xa_mk_sibling(unsigned int offset) 118902c02bf1SMatthew Wilcox { 119002c02bf1SMatthew Wilcox return xa_mk_internal(offset); 119102c02bf1SMatthew Wilcox } 119202c02bf1SMatthew Wilcox 119302c02bf1SMatthew Wilcox /* Private */ 119402c02bf1SMatthew Wilcox static inline unsigned long xa_to_sibling(const void *entry) 119502c02bf1SMatthew Wilcox { 119602c02bf1SMatthew Wilcox return xa_to_internal(entry); 119702c02bf1SMatthew Wilcox } 119802c02bf1SMatthew Wilcox 119902c02bf1SMatthew Wilcox /** 120002c02bf1SMatthew Wilcox * xa_is_sibling() - Is the entry a sibling entry? 120102c02bf1SMatthew Wilcox * @entry: Entry retrieved from the XArray 120202c02bf1SMatthew Wilcox * 120302c02bf1SMatthew Wilcox * Return: %true if the entry is a sibling entry. 120402c02bf1SMatthew Wilcox */ 120502c02bf1SMatthew Wilcox static inline bool xa_is_sibling(const void *entry) 120602c02bf1SMatthew Wilcox { 120702c02bf1SMatthew Wilcox return IS_ENABLED(CONFIG_XARRAY_MULTI) && xa_is_internal(entry) && 120802c02bf1SMatthew Wilcox (entry < xa_mk_sibling(XA_CHUNK_SIZE - 1)); 120902c02bf1SMatthew Wilcox } 121002c02bf1SMatthew Wilcox 121176b4e529SMatthew Wilcox #define XA_RETRY_ENTRY xa_mk_internal(256) 121276b4e529SMatthew Wilcox #define XA_ZERO_ENTRY xa_mk_internal(257) 12139f14d4f1SMatthew Wilcox 12149f14d4f1SMatthew Wilcox /** 12159f14d4f1SMatthew Wilcox * xa_is_zero() - Is the entry a zero entry? 12169f14d4f1SMatthew Wilcox * @entry: Entry retrieved from the XArray 12179f14d4f1SMatthew Wilcox * 12189f14d4f1SMatthew Wilcox * Return: %true if the entry is a zero entry. 12199f14d4f1SMatthew Wilcox */ 12209f14d4f1SMatthew Wilcox static inline bool xa_is_zero(const void *entry) 12219f14d4f1SMatthew Wilcox { 12229f14d4f1SMatthew Wilcox return unlikely(entry == XA_ZERO_ENTRY); 12239f14d4f1SMatthew Wilcox } 122402c02bf1SMatthew Wilcox 1225ad3d6c72SMatthew Wilcox /** 1226ad3d6c72SMatthew Wilcox * xa_is_retry() - Is the entry a retry entry? 1227ad3d6c72SMatthew Wilcox * @entry: Entry retrieved from the XArray 1228ad3d6c72SMatthew Wilcox * 1229ad3d6c72SMatthew Wilcox * Return: %true if the entry is a retry entry. 1230ad3d6c72SMatthew Wilcox */ 1231ad3d6c72SMatthew Wilcox static inline bool xa_is_retry(const void *entry) 1232ad3d6c72SMatthew Wilcox { 1233ad3d6c72SMatthew Wilcox return unlikely(entry == XA_RETRY_ENTRY); 1234ad3d6c72SMatthew Wilcox } 1235ad3d6c72SMatthew Wilcox 1236ad3d6c72SMatthew Wilcox /** 123776b4e529SMatthew Wilcox * xa_is_advanced() - Is the entry only permitted for the advanced API? 123876b4e529SMatthew Wilcox * @entry: Entry to be stored in the XArray. 123976b4e529SMatthew Wilcox * 124076b4e529SMatthew Wilcox * Return: %true if the entry cannot be stored by the normal API. 124176b4e529SMatthew Wilcox */ 124276b4e529SMatthew Wilcox static inline bool xa_is_advanced(const void *entry) 124376b4e529SMatthew Wilcox { 124476b4e529SMatthew Wilcox return xa_is_internal(entry) && (entry <= XA_RETRY_ENTRY); 124576b4e529SMatthew Wilcox } 124676b4e529SMatthew Wilcox 124776b4e529SMatthew Wilcox /** 1248ad3d6c72SMatthew Wilcox * typedef xa_update_node_t - A callback function from the XArray. 1249ad3d6c72SMatthew Wilcox * @node: The node which is being processed 1250ad3d6c72SMatthew Wilcox * 1251ad3d6c72SMatthew Wilcox * This function is called every time the XArray updates the count of 1252ad3d6c72SMatthew Wilcox * present and value entries in a node. It allows advanced users to 1253ad3d6c72SMatthew Wilcox * maintain the private_list in the node. 1254ad3d6c72SMatthew Wilcox * 1255ad3d6c72SMatthew Wilcox * Context: The xa_lock is held and interrupts may be disabled. 1256ad3d6c72SMatthew Wilcox * Implementations should not drop the xa_lock, nor re-enable 1257ad3d6c72SMatthew Wilcox * interrupts. 1258ad3d6c72SMatthew Wilcox */ 1259ad3d6c72SMatthew Wilcox typedef void (*xa_update_node_t)(struct xa_node *node); 1260ad3d6c72SMatthew Wilcox 1261ad3d6c72SMatthew Wilcox /* 1262ad3d6c72SMatthew Wilcox * The xa_state is opaque to its users. It contains various different pieces 1263ad3d6c72SMatthew Wilcox * of state involved in the current operation on the XArray. It should be 1264ad3d6c72SMatthew Wilcox * declared on the stack and passed between the various internal routines. 1265ad3d6c72SMatthew Wilcox * The various elements in it should not be accessed directly, but only 1266ad3d6c72SMatthew Wilcox * through the provided accessor functions. The below documentation is for 1267ad3d6c72SMatthew Wilcox * the benefit of those working on the code, not for users of the XArray. 1268ad3d6c72SMatthew Wilcox * 1269ad3d6c72SMatthew Wilcox * @xa_node usually points to the xa_node containing the slot we're operating 1270ad3d6c72SMatthew Wilcox * on (and @xa_offset is the offset in the slots array). If there is a 1271ad3d6c72SMatthew Wilcox * single entry in the array at index 0, there are no allocated xa_nodes to 1272ad3d6c72SMatthew Wilcox * point to, and so we store %NULL in @xa_node. @xa_node is set to 1273ad3d6c72SMatthew Wilcox * the value %XAS_RESTART if the xa_state is not walked to the correct 1274ad3d6c72SMatthew Wilcox * position in the tree of nodes for this operation. If an error occurs 1275ad3d6c72SMatthew Wilcox * during an operation, it is set to an %XAS_ERROR value. If we run off the 1276ad3d6c72SMatthew Wilcox * end of the allocated nodes, it is set to %XAS_BOUNDS. 1277ad3d6c72SMatthew Wilcox */ 1278ad3d6c72SMatthew Wilcox struct xa_state { 1279ad3d6c72SMatthew Wilcox struct xarray *xa; 1280ad3d6c72SMatthew Wilcox unsigned long xa_index; 1281ad3d6c72SMatthew Wilcox unsigned char xa_shift; 1282ad3d6c72SMatthew Wilcox unsigned char xa_sibs; 1283ad3d6c72SMatthew Wilcox unsigned char xa_offset; 1284ad3d6c72SMatthew Wilcox unsigned char xa_pad; /* Helps gcc generate better code */ 1285ad3d6c72SMatthew Wilcox struct xa_node *xa_node; 1286ad3d6c72SMatthew Wilcox struct xa_node *xa_alloc; 1287ad3d6c72SMatthew Wilcox xa_update_node_t xa_update; 1288ad3d6c72SMatthew Wilcox }; 1289ad3d6c72SMatthew Wilcox 1290ad3d6c72SMatthew Wilcox /* 1291ad3d6c72SMatthew Wilcox * We encode errnos in the xas->xa_node. If an error has happened, we need to 1292ad3d6c72SMatthew Wilcox * drop the lock to fix it, and once we've done so the xa_state is invalid. 1293ad3d6c72SMatthew Wilcox */ 1294ad3d6c72SMatthew Wilcox #define XA_ERROR(errno) ((struct xa_node *)(((unsigned long)errno << 2) | 2UL)) 1295ad3d6c72SMatthew Wilcox #define XAS_BOUNDS ((struct xa_node *)1UL) 1296ad3d6c72SMatthew Wilcox #define XAS_RESTART ((struct xa_node *)3UL) 1297ad3d6c72SMatthew Wilcox 1298ad3d6c72SMatthew Wilcox #define __XA_STATE(array, index, shift, sibs) { \ 1299ad3d6c72SMatthew Wilcox .xa = array, \ 1300ad3d6c72SMatthew Wilcox .xa_index = index, \ 1301ad3d6c72SMatthew Wilcox .xa_shift = shift, \ 1302ad3d6c72SMatthew Wilcox .xa_sibs = sibs, \ 1303ad3d6c72SMatthew Wilcox .xa_offset = 0, \ 1304ad3d6c72SMatthew Wilcox .xa_pad = 0, \ 1305ad3d6c72SMatthew Wilcox .xa_node = XAS_RESTART, \ 1306ad3d6c72SMatthew Wilcox .xa_alloc = NULL, \ 1307ad3d6c72SMatthew Wilcox .xa_update = NULL \ 1308ad3d6c72SMatthew Wilcox } 1309ad3d6c72SMatthew Wilcox 1310ad3d6c72SMatthew Wilcox /** 1311ad3d6c72SMatthew Wilcox * XA_STATE() - Declare an XArray operation state. 1312ad3d6c72SMatthew Wilcox * @name: Name of this operation state (usually xas). 1313ad3d6c72SMatthew Wilcox * @array: Array to operate on. 1314ad3d6c72SMatthew Wilcox * @index: Initial index of interest. 1315ad3d6c72SMatthew Wilcox * 1316ad3d6c72SMatthew Wilcox * Declare and initialise an xa_state on the stack. 1317ad3d6c72SMatthew Wilcox */ 1318ad3d6c72SMatthew Wilcox #define XA_STATE(name, array, index) \ 1319ad3d6c72SMatthew Wilcox struct xa_state name = __XA_STATE(array, index, 0, 0) 1320ad3d6c72SMatthew Wilcox 1321ad3d6c72SMatthew Wilcox /** 1322ad3d6c72SMatthew Wilcox * XA_STATE_ORDER() - Declare an XArray operation state. 1323ad3d6c72SMatthew Wilcox * @name: Name of this operation state (usually xas). 1324ad3d6c72SMatthew Wilcox * @array: Array to operate on. 1325ad3d6c72SMatthew Wilcox * @index: Initial index of interest. 1326ad3d6c72SMatthew Wilcox * @order: Order of entry. 1327ad3d6c72SMatthew Wilcox * 1328ad3d6c72SMatthew Wilcox * Declare and initialise an xa_state on the stack. This variant of 1329ad3d6c72SMatthew Wilcox * XA_STATE() allows you to specify the 'order' of the element you 1330ad3d6c72SMatthew Wilcox * want to operate on.` 1331ad3d6c72SMatthew Wilcox */ 1332ad3d6c72SMatthew Wilcox #define XA_STATE_ORDER(name, array, index, order) \ 1333ad3d6c72SMatthew Wilcox struct xa_state name = __XA_STATE(array, \ 1334ad3d6c72SMatthew Wilcox (index >> order) << order, \ 1335ad3d6c72SMatthew Wilcox order - (order % XA_CHUNK_SHIFT), \ 1336ad3d6c72SMatthew Wilcox (1U << (order % XA_CHUNK_SHIFT)) - 1) 1337ad3d6c72SMatthew Wilcox 1338ad3d6c72SMatthew Wilcox #define xas_marked(xas, mark) xa_marked((xas)->xa, (mark)) 1339ad3d6c72SMatthew Wilcox #define xas_trylock(xas) xa_trylock((xas)->xa) 1340ad3d6c72SMatthew Wilcox #define xas_lock(xas) xa_lock((xas)->xa) 1341ad3d6c72SMatthew Wilcox #define xas_unlock(xas) xa_unlock((xas)->xa) 1342ad3d6c72SMatthew Wilcox #define xas_lock_bh(xas) xa_lock_bh((xas)->xa) 1343ad3d6c72SMatthew Wilcox #define xas_unlock_bh(xas) xa_unlock_bh((xas)->xa) 1344ad3d6c72SMatthew Wilcox #define xas_lock_irq(xas) xa_lock_irq((xas)->xa) 1345ad3d6c72SMatthew Wilcox #define xas_unlock_irq(xas) xa_unlock_irq((xas)->xa) 1346ad3d6c72SMatthew Wilcox #define xas_lock_irqsave(xas, flags) \ 1347ad3d6c72SMatthew Wilcox xa_lock_irqsave((xas)->xa, flags) 1348ad3d6c72SMatthew Wilcox #define xas_unlock_irqrestore(xas, flags) \ 1349ad3d6c72SMatthew Wilcox xa_unlock_irqrestore((xas)->xa, flags) 1350ad3d6c72SMatthew Wilcox 1351ad3d6c72SMatthew Wilcox /** 1352ad3d6c72SMatthew Wilcox * xas_error() - Return an errno stored in the xa_state. 1353ad3d6c72SMatthew Wilcox * @xas: XArray operation state. 1354ad3d6c72SMatthew Wilcox * 1355ad3d6c72SMatthew Wilcox * Return: 0 if no error has been noted. A negative errno if one has. 1356ad3d6c72SMatthew Wilcox */ 1357ad3d6c72SMatthew Wilcox static inline int xas_error(const struct xa_state *xas) 1358ad3d6c72SMatthew Wilcox { 1359ad3d6c72SMatthew Wilcox return xa_err(xas->xa_node); 1360ad3d6c72SMatthew Wilcox } 1361ad3d6c72SMatthew Wilcox 1362ad3d6c72SMatthew Wilcox /** 1363ad3d6c72SMatthew Wilcox * xas_set_err() - Note an error in the xa_state. 1364ad3d6c72SMatthew Wilcox * @xas: XArray operation state. 1365ad3d6c72SMatthew Wilcox * @err: Negative error number. 1366ad3d6c72SMatthew Wilcox * 1367ad3d6c72SMatthew Wilcox * Only call this function with a negative @err; zero or positive errors 1368ad3d6c72SMatthew Wilcox * will probably not behave the way you think they should. If you want 1369ad3d6c72SMatthew Wilcox * to clear the error from an xa_state, use xas_reset(). 1370ad3d6c72SMatthew Wilcox */ 1371ad3d6c72SMatthew Wilcox static inline void xas_set_err(struct xa_state *xas, long err) 1372ad3d6c72SMatthew Wilcox { 1373ad3d6c72SMatthew Wilcox xas->xa_node = XA_ERROR(err); 1374ad3d6c72SMatthew Wilcox } 1375ad3d6c72SMatthew Wilcox 1376ad3d6c72SMatthew Wilcox /** 1377ad3d6c72SMatthew Wilcox * xas_invalid() - Is the xas in a retry or error state? 1378ad3d6c72SMatthew Wilcox * @xas: XArray operation state. 1379ad3d6c72SMatthew Wilcox * 1380ad3d6c72SMatthew Wilcox * Return: %true if the xas cannot be used for operations. 1381ad3d6c72SMatthew Wilcox */ 1382ad3d6c72SMatthew Wilcox static inline bool xas_invalid(const struct xa_state *xas) 1383ad3d6c72SMatthew Wilcox { 1384ad3d6c72SMatthew Wilcox return (unsigned long)xas->xa_node & 3; 1385ad3d6c72SMatthew Wilcox } 1386ad3d6c72SMatthew Wilcox 1387ad3d6c72SMatthew Wilcox /** 1388ad3d6c72SMatthew Wilcox * xas_valid() - Is the xas a valid cursor into the array? 1389ad3d6c72SMatthew Wilcox * @xas: XArray operation state. 1390ad3d6c72SMatthew Wilcox * 1391ad3d6c72SMatthew Wilcox * Return: %true if the xas can be used for operations. 1392ad3d6c72SMatthew Wilcox */ 1393ad3d6c72SMatthew Wilcox static inline bool xas_valid(const struct xa_state *xas) 1394ad3d6c72SMatthew Wilcox { 1395ad3d6c72SMatthew Wilcox return !xas_invalid(xas); 1396ad3d6c72SMatthew Wilcox } 1397ad3d6c72SMatthew Wilcox 13982264f513SMatthew Wilcox /** 13992264f513SMatthew Wilcox * xas_is_node() - Does the xas point to a node? 14002264f513SMatthew Wilcox * @xas: XArray operation state. 14012264f513SMatthew Wilcox * 14022264f513SMatthew Wilcox * Return: %true if the xas currently references a node. 14032264f513SMatthew Wilcox */ 14042264f513SMatthew Wilcox static inline bool xas_is_node(const struct xa_state *xas) 14052264f513SMatthew Wilcox { 14062264f513SMatthew Wilcox return xas_valid(xas) && xas->xa_node; 14072264f513SMatthew Wilcox } 14082264f513SMatthew Wilcox 14099b89a035SMatthew Wilcox /* True if the pointer is something other than a node */ 14109b89a035SMatthew Wilcox static inline bool xas_not_node(struct xa_node *node) 14119b89a035SMatthew Wilcox { 14129b89a035SMatthew Wilcox return ((unsigned long)node & 3) || !node; 14139b89a035SMatthew Wilcox } 14149b89a035SMatthew Wilcox 141564d3e9a9SMatthew Wilcox /* True if the node represents RESTART or an error */ 141664d3e9a9SMatthew Wilcox static inline bool xas_frozen(struct xa_node *node) 141764d3e9a9SMatthew Wilcox { 141864d3e9a9SMatthew Wilcox return (unsigned long)node & 2; 141964d3e9a9SMatthew Wilcox } 142064d3e9a9SMatthew Wilcox 142158d6ea30SMatthew Wilcox /* True if the node represents head-of-tree, RESTART or BOUNDS */ 142258d6ea30SMatthew Wilcox static inline bool xas_top(struct xa_node *node) 142358d6ea30SMatthew Wilcox { 142458d6ea30SMatthew Wilcox return node <= XAS_RESTART; 142558d6ea30SMatthew Wilcox } 142658d6ea30SMatthew Wilcox 1427ad3d6c72SMatthew Wilcox /** 1428ad3d6c72SMatthew Wilcox * xas_reset() - Reset an XArray operation state. 1429ad3d6c72SMatthew Wilcox * @xas: XArray operation state. 1430ad3d6c72SMatthew Wilcox * 1431ad3d6c72SMatthew Wilcox * Resets the error or walk state of the @xas so future walks of the 1432ad3d6c72SMatthew Wilcox * array will start from the root. Use this if you have dropped the 1433ad3d6c72SMatthew Wilcox * xarray lock and want to reuse the xa_state. 1434ad3d6c72SMatthew Wilcox * 1435ad3d6c72SMatthew Wilcox * Context: Any context. 1436ad3d6c72SMatthew Wilcox */ 1437ad3d6c72SMatthew Wilcox static inline void xas_reset(struct xa_state *xas) 1438ad3d6c72SMatthew Wilcox { 1439ad3d6c72SMatthew Wilcox xas->xa_node = XAS_RESTART; 1440ad3d6c72SMatthew Wilcox } 1441ad3d6c72SMatthew Wilcox 1442ad3d6c72SMatthew Wilcox /** 1443ad3d6c72SMatthew Wilcox * xas_retry() - Retry the operation if appropriate. 1444ad3d6c72SMatthew Wilcox * @xas: XArray operation state. 1445ad3d6c72SMatthew Wilcox * @entry: Entry from xarray. 1446ad3d6c72SMatthew Wilcox * 1447ad3d6c72SMatthew Wilcox * The advanced functions may sometimes return an internal entry, such as 1448ad3d6c72SMatthew Wilcox * a retry entry or a zero entry. This function sets up the @xas to restart 1449ad3d6c72SMatthew Wilcox * the walk from the head of the array if needed. 1450ad3d6c72SMatthew Wilcox * 1451ad3d6c72SMatthew Wilcox * Context: Any context. 1452ad3d6c72SMatthew Wilcox * Return: true if the operation needs to be retried. 1453ad3d6c72SMatthew Wilcox */ 1454ad3d6c72SMatthew Wilcox static inline bool xas_retry(struct xa_state *xas, const void *entry) 1455ad3d6c72SMatthew Wilcox { 14569f14d4f1SMatthew Wilcox if (xa_is_zero(entry)) 14579f14d4f1SMatthew Wilcox return true; 1458ad3d6c72SMatthew Wilcox if (!xa_is_retry(entry)) 1459ad3d6c72SMatthew Wilcox return false; 1460ad3d6c72SMatthew Wilcox xas_reset(xas); 1461ad3d6c72SMatthew Wilcox return true; 1462ad3d6c72SMatthew Wilcox } 1463ad3d6c72SMatthew Wilcox 1464ad3d6c72SMatthew Wilcox void *xas_load(struct xa_state *); 146558d6ea30SMatthew Wilcox void *xas_store(struct xa_state *, void *entry); 1466b803b428SMatthew Wilcox void *xas_find(struct xa_state *, unsigned long max); 14674e99d4e9SMatthew Wilcox void *xas_find_conflict(struct xa_state *); 1468ad3d6c72SMatthew Wilcox 14699b89a035SMatthew Wilcox bool xas_get_mark(const struct xa_state *, xa_mark_t); 14709b89a035SMatthew Wilcox void xas_set_mark(const struct xa_state *, xa_mark_t); 14719b89a035SMatthew Wilcox void xas_clear_mark(const struct xa_state *, xa_mark_t); 1472b803b428SMatthew Wilcox void *xas_find_marked(struct xa_state *, unsigned long max, xa_mark_t); 147358d6ea30SMatthew Wilcox void xas_init_marks(const struct xa_state *); 147458d6ea30SMatthew Wilcox 147558d6ea30SMatthew Wilcox bool xas_nomem(struct xa_state *, gfp_t); 1476b803b428SMatthew Wilcox void xas_pause(struct xa_state *); 14779b89a035SMatthew Wilcox 14782264f513SMatthew Wilcox void xas_create_range(struct xa_state *); 14792264f513SMatthew Wilcox 1480ad3d6c72SMatthew Wilcox /** 1481ad3d6c72SMatthew Wilcox * xas_reload() - Refetch an entry from the xarray. 1482ad3d6c72SMatthew Wilcox * @xas: XArray operation state. 1483ad3d6c72SMatthew Wilcox * 1484ad3d6c72SMatthew Wilcox * Use this function to check that a previously loaded entry still has 1485ad3d6c72SMatthew Wilcox * the same value. This is useful for the lockless pagecache lookup where 1486ad3d6c72SMatthew Wilcox * we walk the array with only the RCU lock to protect us, lock the page, 1487ad3d6c72SMatthew Wilcox * then check that the page hasn't moved since we looked it up. 1488ad3d6c72SMatthew Wilcox * 1489ad3d6c72SMatthew Wilcox * The caller guarantees that @xas is still valid. If it may be in an 1490ad3d6c72SMatthew Wilcox * error or restart state, call xas_load() instead. 1491ad3d6c72SMatthew Wilcox * 1492ad3d6c72SMatthew Wilcox * Return: The entry at this location in the xarray. 1493ad3d6c72SMatthew Wilcox */ 1494ad3d6c72SMatthew Wilcox static inline void *xas_reload(struct xa_state *xas) 1495ad3d6c72SMatthew Wilcox { 1496ad3d6c72SMatthew Wilcox struct xa_node *node = xas->xa_node; 1497ad3d6c72SMatthew Wilcox 1498ad3d6c72SMatthew Wilcox if (node) 1499ad3d6c72SMatthew Wilcox return xa_entry(xas->xa, node, xas->xa_offset); 1500ad3d6c72SMatthew Wilcox return xa_head(xas->xa); 1501ad3d6c72SMatthew Wilcox } 1502ad3d6c72SMatthew Wilcox 150358d6ea30SMatthew Wilcox /** 150458d6ea30SMatthew Wilcox * xas_set() - Set up XArray operation state for a different index. 150558d6ea30SMatthew Wilcox * @xas: XArray operation state. 150658d6ea30SMatthew Wilcox * @index: New index into the XArray. 150758d6ea30SMatthew Wilcox * 150858d6ea30SMatthew Wilcox * Move the operation state to refer to a different index. This will 150958d6ea30SMatthew Wilcox * have the effect of starting a walk from the top; see xas_next() 151058d6ea30SMatthew Wilcox * to move to an adjacent index. 151158d6ea30SMatthew Wilcox */ 151258d6ea30SMatthew Wilcox static inline void xas_set(struct xa_state *xas, unsigned long index) 151358d6ea30SMatthew Wilcox { 151458d6ea30SMatthew Wilcox xas->xa_index = index; 151558d6ea30SMatthew Wilcox xas->xa_node = XAS_RESTART; 151658d6ea30SMatthew Wilcox } 151758d6ea30SMatthew Wilcox 151858d6ea30SMatthew Wilcox /** 151958d6ea30SMatthew Wilcox * xas_set_order() - Set up XArray operation state for a multislot entry. 152058d6ea30SMatthew Wilcox * @xas: XArray operation state. 152158d6ea30SMatthew Wilcox * @index: Target of the operation. 152258d6ea30SMatthew Wilcox * @order: Entry occupies 2^@order indices. 152358d6ea30SMatthew Wilcox */ 152458d6ea30SMatthew Wilcox static inline void xas_set_order(struct xa_state *xas, unsigned long index, 152558d6ea30SMatthew Wilcox unsigned int order) 152658d6ea30SMatthew Wilcox { 152758d6ea30SMatthew Wilcox #ifdef CONFIG_XARRAY_MULTI 152858d6ea30SMatthew Wilcox xas->xa_index = order < BITS_PER_LONG ? (index >> order) << order : 0; 152958d6ea30SMatthew Wilcox xas->xa_shift = order - (order % XA_CHUNK_SHIFT); 153058d6ea30SMatthew Wilcox xas->xa_sibs = (1 << (order % XA_CHUNK_SHIFT)) - 1; 153158d6ea30SMatthew Wilcox xas->xa_node = XAS_RESTART; 153258d6ea30SMatthew Wilcox #else 153358d6ea30SMatthew Wilcox BUG_ON(order > 0); 153458d6ea30SMatthew Wilcox xas_set(xas, index); 153558d6ea30SMatthew Wilcox #endif 153658d6ea30SMatthew Wilcox } 153758d6ea30SMatthew Wilcox 153858d6ea30SMatthew Wilcox /** 153958d6ea30SMatthew Wilcox * xas_set_update() - Set up XArray operation state for a callback. 154058d6ea30SMatthew Wilcox * @xas: XArray operation state. 154158d6ea30SMatthew Wilcox * @update: Function to call when updating a node. 154258d6ea30SMatthew Wilcox * 154358d6ea30SMatthew Wilcox * The XArray can notify a caller after it has updated an xa_node. 154458d6ea30SMatthew Wilcox * This is advanced functionality and is only needed by the page cache. 154558d6ea30SMatthew Wilcox */ 154658d6ea30SMatthew Wilcox static inline void xas_set_update(struct xa_state *xas, xa_update_node_t update) 154758d6ea30SMatthew Wilcox { 154858d6ea30SMatthew Wilcox xas->xa_update = update; 154958d6ea30SMatthew Wilcox } 155058d6ea30SMatthew Wilcox 1551b803b428SMatthew Wilcox /** 1552b803b428SMatthew Wilcox * xas_next_entry() - Advance iterator to next present entry. 1553b803b428SMatthew Wilcox * @xas: XArray operation state. 1554b803b428SMatthew Wilcox * @max: Highest index to return. 1555b803b428SMatthew Wilcox * 1556b803b428SMatthew Wilcox * xas_next_entry() is an inline function to optimise xarray traversal for 1557b803b428SMatthew Wilcox * speed. It is equivalent to calling xas_find(), and will call xas_find() 1558b803b428SMatthew Wilcox * for all the hard cases. 1559b803b428SMatthew Wilcox * 1560b803b428SMatthew Wilcox * Return: The next present entry after the one currently referred to by @xas. 1561b803b428SMatthew Wilcox */ 1562b803b428SMatthew Wilcox static inline void *xas_next_entry(struct xa_state *xas, unsigned long max) 1563b803b428SMatthew Wilcox { 1564b803b428SMatthew Wilcox struct xa_node *node = xas->xa_node; 1565b803b428SMatthew Wilcox void *entry; 1566b803b428SMatthew Wilcox 1567b803b428SMatthew Wilcox if (unlikely(xas_not_node(node) || node->shift || 1568b803b428SMatthew Wilcox xas->xa_offset != (xas->xa_index & XA_CHUNK_MASK))) 1569b803b428SMatthew Wilcox return xas_find(xas, max); 1570b803b428SMatthew Wilcox 1571b803b428SMatthew Wilcox do { 1572b803b428SMatthew Wilcox if (unlikely(xas->xa_index >= max)) 1573b803b428SMatthew Wilcox return xas_find(xas, max); 1574b803b428SMatthew Wilcox if (unlikely(xas->xa_offset == XA_CHUNK_MASK)) 1575b803b428SMatthew Wilcox return xas_find(xas, max); 1576b803b428SMatthew Wilcox entry = xa_entry(xas->xa, node, xas->xa_offset + 1); 1577b803b428SMatthew Wilcox if (unlikely(xa_is_internal(entry))) 1578b803b428SMatthew Wilcox return xas_find(xas, max); 1579b803b428SMatthew Wilcox xas->xa_offset++; 1580b803b428SMatthew Wilcox xas->xa_index++; 1581b803b428SMatthew Wilcox } while (!entry); 1582b803b428SMatthew Wilcox 1583b803b428SMatthew Wilcox return entry; 1584b803b428SMatthew Wilcox } 1585b803b428SMatthew Wilcox 1586b803b428SMatthew Wilcox /* Private */ 1587b803b428SMatthew Wilcox static inline unsigned int xas_find_chunk(struct xa_state *xas, bool advance, 1588b803b428SMatthew Wilcox xa_mark_t mark) 1589b803b428SMatthew Wilcox { 1590b803b428SMatthew Wilcox unsigned long *addr = xas->xa_node->marks[(__force unsigned)mark]; 1591b803b428SMatthew Wilcox unsigned int offset = xas->xa_offset; 1592b803b428SMatthew Wilcox 1593b803b428SMatthew Wilcox if (advance) 1594b803b428SMatthew Wilcox offset++; 1595b803b428SMatthew Wilcox if (XA_CHUNK_SIZE == BITS_PER_LONG) { 1596b803b428SMatthew Wilcox if (offset < XA_CHUNK_SIZE) { 1597b803b428SMatthew Wilcox unsigned long data = *addr & (~0UL << offset); 1598b803b428SMatthew Wilcox if (data) 1599b803b428SMatthew Wilcox return __ffs(data); 1600b803b428SMatthew Wilcox } 1601b803b428SMatthew Wilcox return XA_CHUNK_SIZE; 1602b803b428SMatthew Wilcox } 1603b803b428SMatthew Wilcox 1604b803b428SMatthew Wilcox return find_next_bit(addr, XA_CHUNK_SIZE, offset); 1605b803b428SMatthew Wilcox } 1606b803b428SMatthew Wilcox 1607b803b428SMatthew Wilcox /** 1608b803b428SMatthew Wilcox * xas_next_marked() - Advance iterator to next marked entry. 1609b803b428SMatthew Wilcox * @xas: XArray operation state. 1610b803b428SMatthew Wilcox * @max: Highest index to return. 1611b803b428SMatthew Wilcox * @mark: Mark to search for. 1612b803b428SMatthew Wilcox * 1613b803b428SMatthew Wilcox * xas_next_marked() is an inline function to optimise xarray traversal for 1614b803b428SMatthew Wilcox * speed. It is equivalent to calling xas_find_marked(), and will call 1615b803b428SMatthew Wilcox * xas_find_marked() for all the hard cases. 1616b803b428SMatthew Wilcox * 1617b803b428SMatthew Wilcox * Return: The next marked entry after the one currently referred to by @xas. 1618b803b428SMatthew Wilcox */ 1619b803b428SMatthew Wilcox static inline void *xas_next_marked(struct xa_state *xas, unsigned long max, 1620b803b428SMatthew Wilcox xa_mark_t mark) 1621b803b428SMatthew Wilcox { 1622b803b428SMatthew Wilcox struct xa_node *node = xas->xa_node; 1623b803b428SMatthew Wilcox unsigned int offset; 1624b803b428SMatthew Wilcox 1625b803b428SMatthew Wilcox if (unlikely(xas_not_node(node) || node->shift)) 1626b803b428SMatthew Wilcox return xas_find_marked(xas, max, mark); 1627b803b428SMatthew Wilcox offset = xas_find_chunk(xas, true, mark); 1628b803b428SMatthew Wilcox xas->xa_offset = offset; 1629b803b428SMatthew Wilcox xas->xa_index = (xas->xa_index & ~XA_CHUNK_MASK) + offset; 1630b803b428SMatthew Wilcox if (xas->xa_index > max) 1631b803b428SMatthew Wilcox return NULL; 1632b803b428SMatthew Wilcox if (offset == XA_CHUNK_SIZE) 1633b803b428SMatthew Wilcox return xas_find_marked(xas, max, mark); 1634b803b428SMatthew Wilcox return xa_entry(xas->xa, node, offset); 1635b803b428SMatthew Wilcox } 1636b803b428SMatthew Wilcox 1637b803b428SMatthew Wilcox /* 1638b803b428SMatthew Wilcox * If iterating while holding a lock, drop the lock and reschedule 1639b803b428SMatthew Wilcox * every %XA_CHECK_SCHED loops. 1640b803b428SMatthew Wilcox */ 1641b803b428SMatthew Wilcox enum { 1642b803b428SMatthew Wilcox XA_CHECK_SCHED = 4096, 1643b803b428SMatthew Wilcox }; 1644b803b428SMatthew Wilcox 1645b803b428SMatthew Wilcox /** 1646b803b428SMatthew Wilcox * xas_for_each() - Iterate over a range of an XArray. 1647b803b428SMatthew Wilcox * @xas: XArray operation state. 1648b803b428SMatthew Wilcox * @entry: Entry retrieved from the array. 1649b803b428SMatthew Wilcox * @max: Maximum index to retrieve from array. 1650b803b428SMatthew Wilcox * 1651b803b428SMatthew Wilcox * The loop body will be executed for each entry present in the xarray 1652b803b428SMatthew Wilcox * between the current xas position and @max. @entry will be set to 1653b803b428SMatthew Wilcox * the entry retrieved from the xarray. It is safe to delete entries 1654b803b428SMatthew Wilcox * from the array in the loop body. You should hold either the RCU lock 1655b803b428SMatthew Wilcox * or the xa_lock while iterating. If you need to drop the lock, call 1656b803b428SMatthew Wilcox * xas_pause() first. 1657b803b428SMatthew Wilcox */ 1658b803b428SMatthew Wilcox #define xas_for_each(xas, entry, max) \ 1659b803b428SMatthew Wilcox for (entry = xas_find(xas, max); entry; \ 1660b803b428SMatthew Wilcox entry = xas_next_entry(xas, max)) 1661b803b428SMatthew Wilcox 1662b803b428SMatthew Wilcox /** 1663b803b428SMatthew Wilcox * xas_for_each_marked() - Iterate over a range of an XArray. 1664b803b428SMatthew Wilcox * @xas: XArray operation state. 1665b803b428SMatthew Wilcox * @entry: Entry retrieved from the array. 1666b803b428SMatthew Wilcox * @max: Maximum index to retrieve from array. 1667b803b428SMatthew Wilcox * @mark: Mark to search for. 1668b803b428SMatthew Wilcox * 1669b803b428SMatthew Wilcox * The loop body will be executed for each marked entry in the xarray 1670b803b428SMatthew Wilcox * between the current xas position and @max. @entry will be set to 1671b803b428SMatthew Wilcox * the entry retrieved from the xarray. It is safe to delete entries 1672b803b428SMatthew Wilcox * from the array in the loop body. You should hold either the RCU lock 1673b803b428SMatthew Wilcox * or the xa_lock while iterating. If you need to drop the lock, call 1674b803b428SMatthew Wilcox * xas_pause() first. 1675b803b428SMatthew Wilcox */ 1676b803b428SMatthew Wilcox #define xas_for_each_marked(xas, entry, max, mark) \ 1677b803b428SMatthew Wilcox for (entry = xas_find_marked(xas, max, mark); entry; \ 1678b803b428SMatthew Wilcox entry = xas_next_marked(xas, max, mark)) 1679b803b428SMatthew Wilcox 16804e99d4e9SMatthew Wilcox /** 16814e99d4e9SMatthew Wilcox * xas_for_each_conflict() - Iterate over a range of an XArray. 16824e99d4e9SMatthew Wilcox * @xas: XArray operation state. 16834e99d4e9SMatthew Wilcox * @entry: Entry retrieved from the array. 16844e99d4e9SMatthew Wilcox * 16854e99d4e9SMatthew Wilcox * The loop body will be executed for each entry in the XArray that lies 16864e99d4e9SMatthew Wilcox * within the range specified by @xas. If the loop completes successfully, 16874e99d4e9SMatthew Wilcox * any entries that lie in this range will be replaced by @entry. The caller 16884e99d4e9SMatthew Wilcox * may break out of the loop; if they do so, the contents of the XArray will 16894e99d4e9SMatthew Wilcox * be unchanged. The operation may fail due to an out of memory condition. 16904e99d4e9SMatthew Wilcox * The caller may also call xa_set_err() to exit the loop while setting an 16914e99d4e9SMatthew Wilcox * error to record the reason. 16924e99d4e9SMatthew Wilcox */ 16934e99d4e9SMatthew Wilcox #define xas_for_each_conflict(xas, entry) \ 16944e99d4e9SMatthew Wilcox while ((entry = xas_find_conflict(xas))) 16954e99d4e9SMatthew Wilcox 169664d3e9a9SMatthew Wilcox void *__xas_next(struct xa_state *); 169764d3e9a9SMatthew Wilcox void *__xas_prev(struct xa_state *); 169864d3e9a9SMatthew Wilcox 169964d3e9a9SMatthew Wilcox /** 170064d3e9a9SMatthew Wilcox * xas_prev() - Move iterator to previous index. 170164d3e9a9SMatthew Wilcox * @xas: XArray operation state. 170264d3e9a9SMatthew Wilcox * 170364d3e9a9SMatthew Wilcox * If the @xas was in an error state, it will remain in an error state 170464d3e9a9SMatthew Wilcox * and this function will return %NULL. If the @xas has never been walked, 170564d3e9a9SMatthew Wilcox * it will have the effect of calling xas_load(). Otherwise one will be 170664d3e9a9SMatthew Wilcox * subtracted from the index and the state will be walked to the correct 170764d3e9a9SMatthew Wilcox * location in the array for the next operation. 170864d3e9a9SMatthew Wilcox * 170964d3e9a9SMatthew Wilcox * If the iterator was referencing index 0, this function wraps 171064d3e9a9SMatthew Wilcox * around to %ULONG_MAX. 171164d3e9a9SMatthew Wilcox * 171264d3e9a9SMatthew Wilcox * Return: The entry at the new index. This may be %NULL or an internal 171364d3e9a9SMatthew Wilcox * entry. 171464d3e9a9SMatthew Wilcox */ 171564d3e9a9SMatthew Wilcox static inline void *xas_prev(struct xa_state *xas) 171664d3e9a9SMatthew Wilcox { 171764d3e9a9SMatthew Wilcox struct xa_node *node = xas->xa_node; 171864d3e9a9SMatthew Wilcox 171964d3e9a9SMatthew Wilcox if (unlikely(xas_not_node(node) || node->shift || 172064d3e9a9SMatthew Wilcox xas->xa_offset == 0)) 172164d3e9a9SMatthew Wilcox return __xas_prev(xas); 172264d3e9a9SMatthew Wilcox 172364d3e9a9SMatthew Wilcox xas->xa_index--; 172464d3e9a9SMatthew Wilcox xas->xa_offset--; 172564d3e9a9SMatthew Wilcox return xa_entry(xas->xa, node, xas->xa_offset); 172664d3e9a9SMatthew Wilcox } 172764d3e9a9SMatthew Wilcox 172864d3e9a9SMatthew Wilcox /** 172964d3e9a9SMatthew Wilcox * xas_next() - Move state to next index. 173064d3e9a9SMatthew Wilcox * @xas: XArray operation state. 173164d3e9a9SMatthew Wilcox * 173264d3e9a9SMatthew Wilcox * If the @xas was in an error state, it will remain in an error state 173364d3e9a9SMatthew Wilcox * and this function will return %NULL. If the @xas has never been walked, 173464d3e9a9SMatthew Wilcox * it will have the effect of calling xas_load(). Otherwise one will be 173564d3e9a9SMatthew Wilcox * added to the index and the state will be walked to the correct 173664d3e9a9SMatthew Wilcox * location in the array for the next operation. 173764d3e9a9SMatthew Wilcox * 173864d3e9a9SMatthew Wilcox * If the iterator was referencing index %ULONG_MAX, this function wraps 173964d3e9a9SMatthew Wilcox * around to 0. 174064d3e9a9SMatthew Wilcox * 174164d3e9a9SMatthew Wilcox * Return: The entry at the new index. This may be %NULL or an internal 174264d3e9a9SMatthew Wilcox * entry. 174364d3e9a9SMatthew Wilcox */ 174464d3e9a9SMatthew Wilcox static inline void *xas_next(struct xa_state *xas) 174564d3e9a9SMatthew Wilcox { 174664d3e9a9SMatthew Wilcox struct xa_node *node = xas->xa_node; 174764d3e9a9SMatthew Wilcox 174864d3e9a9SMatthew Wilcox if (unlikely(xas_not_node(node) || node->shift || 174964d3e9a9SMatthew Wilcox xas->xa_offset == XA_CHUNK_MASK)) 175064d3e9a9SMatthew Wilcox return __xas_next(xas); 175164d3e9a9SMatthew Wilcox 175264d3e9a9SMatthew Wilcox xas->xa_index++; 175364d3e9a9SMatthew Wilcox xas->xa_offset++; 175464d3e9a9SMatthew Wilcox return xa_entry(xas->xa, node, xas->xa_offset); 175564d3e9a9SMatthew Wilcox } 175664d3e9a9SMatthew Wilcox 1757f6bb2a2cSMatthew Wilcox #endif /* _LINUX_XARRAY_H */ 1758