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> 14f8d5d0ccSMatthew Wilcox #include <linux/kconfig.h> 15*ad3d6c72SMatthew Wilcox #include <linux/kernel.h> 16*ad3d6c72SMatthew Wilcox #include <linux/rcupdate.h> 17f6bb2a2cSMatthew Wilcox #include <linux/spinlock.h> 183159f943SMatthew Wilcox #include <linux/types.h> 193159f943SMatthew Wilcox 203159f943SMatthew Wilcox /* 213159f943SMatthew Wilcox * The bottom two bits of the entry determine how the XArray interprets 223159f943SMatthew Wilcox * the contents: 233159f943SMatthew Wilcox * 243159f943SMatthew Wilcox * 00: Pointer entry 253159f943SMatthew Wilcox * 10: Internal entry 263159f943SMatthew Wilcox * x1: Value entry or tagged pointer 273159f943SMatthew Wilcox * 283159f943SMatthew Wilcox * Attempting to store internal entries in the XArray is a bug. 2902c02bf1SMatthew Wilcox * 3002c02bf1SMatthew Wilcox * Most internal entries are pointers to the next node in the tree. 3102c02bf1SMatthew Wilcox * The following internal entries have a special meaning: 3202c02bf1SMatthew Wilcox * 3302c02bf1SMatthew Wilcox * 0-62: Sibling entries 3402c02bf1SMatthew Wilcox * 256: Retry entry 35*ad3d6c72SMatthew Wilcox * 36*ad3d6c72SMatthew Wilcox * Errors are also represented as internal entries, but use the negative 37*ad3d6c72SMatthew Wilcox * space (-4094 to -2). They're never stored in the slots array; only 38*ad3d6c72SMatthew Wilcox * returned by the normal API. 393159f943SMatthew Wilcox */ 403159f943SMatthew Wilcox 413159f943SMatthew Wilcox #define BITS_PER_XA_VALUE (BITS_PER_LONG - 1) 423159f943SMatthew Wilcox 433159f943SMatthew Wilcox /** 443159f943SMatthew Wilcox * xa_mk_value() - Create an XArray entry from an integer. 453159f943SMatthew Wilcox * @v: Value to store in XArray. 463159f943SMatthew Wilcox * 473159f943SMatthew Wilcox * Context: Any context. 483159f943SMatthew Wilcox * Return: An entry suitable for storing in the XArray. 493159f943SMatthew Wilcox */ 503159f943SMatthew Wilcox static inline void *xa_mk_value(unsigned long v) 513159f943SMatthew Wilcox { 523159f943SMatthew Wilcox WARN_ON((long)v < 0); 533159f943SMatthew Wilcox return (void *)((v << 1) | 1); 543159f943SMatthew Wilcox } 553159f943SMatthew Wilcox 563159f943SMatthew Wilcox /** 573159f943SMatthew Wilcox * xa_to_value() - Get value stored in an XArray entry. 583159f943SMatthew Wilcox * @entry: XArray entry. 593159f943SMatthew Wilcox * 603159f943SMatthew Wilcox * Context: Any context. 613159f943SMatthew Wilcox * Return: The value stored in the XArray entry. 623159f943SMatthew Wilcox */ 633159f943SMatthew Wilcox static inline unsigned long xa_to_value(const void *entry) 643159f943SMatthew Wilcox { 653159f943SMatthew Wilcox return (unsigned long)entry >> 1; 663159f943SMatthew Wilcox } 673159f943SMatthew Wilcox 683159f943SMatthew Wilcox /** 693159f943SMatthew Wilcox * xa_is_value() - Determine if an entry is a value. 703159f943SMatthew Wilcox * @entry: XArray entry. 713159f943SMatthew Wilcox * 723159f943SMatthew Wilcox * Context: Any context. 733159f943SMatthew Wilcox * Return: True if the entry is a value, false if it is a pointer. 743159f943SMatthew Wilcox */ 753159f943SMatthew Wilcox static inline bool xa_is_value(const void *entry) 763159f943SMatthew Wilcox { 773159f943SMatthew Wilcox return (unsigned long)entry & 1; 783159f943SMatthew Wilcox } 793159f943SMatthew Wilcox 803159f943SMatthew Wilcox /** 813159f943SMatthew Wilcox * xa_tag_pointer() - Create an XArray entry for a tagged pointer. 823159f943SMatthew Wilcox * @p: Plain pointer. 833159f943SMatthew Wilcox * @tag: Tag value (0, 1 or 3). 843159f943SMatthew Wilcox * 853159f943SMatthew Wilcox * If the user of the XArray prefers, they can tag their pointers instead 863159f943SMatthew Wilcox * of storing value entries. Three tags are available (0, 1 and 3). 873159f943SMatthew Wilcox * These are distinct from the xa_mark_t as they are not replicated up 883159f943SMatthew Wilcox * through the array and cannot be searched for. 893159f943SMatthew Wilcox * 903159f943SMatthew Wilcox * Context: Any context. 913159f943SMatthew Wilcox * Return: An XArray entry. 923159f943SMatthew Wilcox */ 933159f943SMatthew Wilcox static inline void *xa_tag_pointer(void *p, unsigned long tag) 943159f943SMatthew Wilcox { 953159f943SMatthew Wilcox return (void *)((unsigned long)p | tag); 963159f943SMatthew Wilcox } 973159f943SMatthew Wilcox 983159f943SMatthew Wilcox /** 993159f943SMatthew Wilcox * xa_untag_pointer() - Turn an XArray entry into a plain pointer. 1003159f943SMatthew Wilcox * @entry: XArray entry. 1013159f943SMatthew Wilcox * 1023159f943SMatthew Wilcox * If you have stored a tagged pointer in the XArray, call this function 1033159f943SMatthew Wilcox * to get the untagged version of the pointer. 1043159f943SMatthew Wilcox * 1053159f943SMatthew Wilcox * Context: Any context. 1063159f943SMatthew Wilcox * Return: A pointer. 1073159f943SMatthew Wilcox */ 1083159f943SMatthew Wilcox static inline void *xa_untag_pointer(void *entry) 1093159f943SMatthew Wilcox { 1103159f943SMatthew Wilcox return (void *)((unsigned long)entry & ~3UL); 1113159f943SMatthew Wilcox } 1123159f943SMatthew Wilcox 1133159f943SMatthew Wilcox /** 1143159f943SMatthew Wilcox * xa_pointer_tag() - Get the tag stored in an XArray entry. 1153159f943SMatthew Wilcox * @entry: XArray entry. 1163159f943SMatthew Wilcox * 1173159f943SMatthew Wilcox * If you have stored a tagged pointer in the XArray, call this function 1183159f943SMatthew Wilcox * to get the tag of that pointer. 1193159f943SMatthew Wilcox * 1203159f943SMatthew Wilcox * Context: Any context. 1213159f943SMatthew Wilcox * Return: A tag. 1223159f943SMatthew Wilcox */ 1233159f943SMatthew Wilcox static inline unsigned int xa_pointer_tag(void *entry) 1243159f943SMatthew Wilcox { 1253159f943SMatthew Wilcox return (unsigned long)entry & 3UL; 1263159f943SMatthew Wilcox } 127f6bb2a2cSMatthew Wilcox 12802c02bf1SMatthew Wilcox /* 12902c02bf1SMatthew Wilcox * xa_mk_internal() - Create an internal entry. 13002c02bf1SMatthew Wilcox * @v: Value to turn into an internal entry. 13102c02bf1SMatthew Wilcox * 13202c02bf1SMatthew Wilcox * Context: Any context. 13302c02bf1SMatthew Wilcox * Return: An XArray internal entry corresponding to this value. 13402c02bf1SMatthew Wilcox */ 13502c02bf1SMatthew Wilcox static inline void *xa_mk_internal(unsigned long v) 13602c02bf1SMatthew Wilcox { 13702c02bf1SMatthew Wilcox return (void *)((v << 2) | 2); 13802c02bf1SMatthew Wilcox } 13902c02bf1SMatthew Wilcox 14002c02bf1SMatthew Wilcox /* 14102c02bf1SMatthew Wilcox * xa_to_internal() - Extract the value from an internal entry. 14202c02bf1SMatthew Wilcox * @entry: XArray entry. 14302c02bf1SMatthew Wilcox * 14402c02bf1SMatthew Wilcox * Context: Any context. 14502c02bf1SMatthew Wilcox * Return: The value which was stored in the internal entry. 14602c02bf1SMatthew Wilcox */ 14702c02bf1SMatthew Wilcox static inline unsigned long xa_to_internal(const void *entry) 14802c02bf1SMatthew Wilcox { 14902c02bf1SMatthew Wilcox return (unsigned long)entry >> 2; 15002c02bf1SMatthew Wilcox } 15102c02bf1SMatthew Wilcox 15202c02bf1SMatthew Wilcox /* 15302c02bf1SMatthew Wilcox * xa_is_internal() - Is the entry an internal entry? 15402c02bf1SMatthew Wilcox * @entry: XArray entry. 15502c02bf1SMatthew Wilcox * 15602c02bf1SMatthew Wilcox * Context: Any context. 15702c02bf1SMatthew Wilcox * Return: %true if the entry is an internal entry. 15802c02bf1SMatthew Wilcox */ 15902c02bf1SMatthew Wilcox static inline bool xa_is_internal(const void *entry) 16002c02bf1SMatthew Wilcox { 16102c02bf1SMatthew Wilcox return ((unsigned long)entry & 3) == 2; 16202c02bf1SMatthew Wilcox } 16302c02bf1SMatthew Wilcox 164f8d5d0ccSMatthew Wilcox /** 165*ad3d6c72SMatthew Wilcox * xa_is_err() - Report whether an XArray operation returned an error 166*ad3d6c72SMatthew Wilcox * @entry: Result from calling an XArray function 167*ad3d6c72SMatthew Wilcox * 168*ad3d6c72SMatthew Wilcox * If an XArray operation cannot complete an operation, it will return 169*ad3d6c72SMatthew Wilcox * a special value indicating an error. This function tells you 170*ad3d6c72SMatthew Wilcox * whether an error occurred; xa_err() tells you which error occurred. 171*ad3d6c72SMatthew Wilcox * 172*ad3d6c72SMatthew Wilcox * Context: Any context. 173*ad3d6c72SMatthew Wilcox * Return: %true if the entry indicates an error. 174*ad3d6c72SMatthew Wilcox */ 175*ad3d6c72SMatthew Wilcox static inline bool xa_is_err(const void *entry) 176*ad3d6c72SMatthew Wilcox { 177*ad3d6c72SMatthew Wilcox return unlikely(xa_is_internal(entry)); 178*ad3d6c72SMatthew Wilcox } 179*ad3d6c72SMatthew Wilcox 180*ad3d6c72SMatthew Wilcox /** 181*ad3d6c72SMatthew Wilcox * xa_err() - Turn an XArray result into an errno. 182*ad3d6c72SMatthew Wilcox * @entry: Result from calling an XArray function. 183*ad3d6c72SMatthew Wilcox * 184*ad3d6c72SMatthew Wilcox * If an XArray operation cannot complete an operation, it will return 185*ad3d6c72SMatthew Wilcox * a special pointer value which encodes an errno. This function extracts 186*ad3d6c72SMatthew Wilcox * the errno from the pointer value, or returns 0 if the pointer does not 187*ad3d6c72SMatthew Wilcox * represent an errno. 188*ad3d6c72SMatthew Wilcox * 189*ad3d6c72SMatthew Wilcox * Context: Any context. 190*ad3d6c72SMatthew Wilcox * Return: A negative errno or 0. 191*ad3d6c72SMatthew Wilcox */ 192*ad3d6c72SMatthew Wilcox static inline int xa_err(void *entry) 193*ad3d6c72SMatthew Wilcox { 194*ad3d6c72SMatthew Wilcox /* xa_to_internal() would not do sign extension. */ 195*ad3d6c72SMatthew Wilcox if (xa_is_err(entry)) 196*ad3d6c72SMatthew Wilcox return (long)entry >> 2; 197*ad3d6c72SMatthew Wilcox return 0; 198*ad3d6c72SMatthew Wilcox } 199*ad3d6c72SMatthew Wilcox 200*ad3d6c72SMatthew Wilcox /** 201f8d5d0ccSMatthew Wilcox * struct xarray - The anchor of the XArray. 202f8d5d0ccSMatthew Wilcox * @xa_lock: Lock that protects the contents of the XArray. 203f8d5d0ccSMatthew Wilcox * 204f8d5d0ccSMatthew Wilcox * To use the xarray, define it statically or embed it in your data structure. 205f8d5d0ccSMatthew Wilcox * It is a very small data structure, so it does not usually make sense to 206f8d5d0ccSMatthew Wilcox * allocate it separately and keep a pointer to it in your data structure. 207f8d5d0ccSMatthew Wilcox * 208f8d5d0ccSMatthew Wilcox * You may use the xa_lock to protect your own data structures as well. 209f8d5d0ccSMatthew Wilcox */ 210f8d5d0ccSMatthew Wilcox /* 211f8d5d0ccSMatthew Wilcox * If all of the entries in the array are NULL, @xa_head is a NULL pointer. 212f8d5d0ccSMatthew Wilcox * If the only non-NULL entry in the array is at index 0, @xa_head is that 213f8d5d0ccSMatthew Wilcox * entry. If any other entry in the array is non-NULL, @xa_head points 214f8d5d0ccSMatthew Wilcox * to an @xa_node. 215f8d5d0ccSMatthew Wilcox */ 216f8d5d0ccSMatthew Wilcox struct xarray { 217f8d5d0ccSMatthew Wilcox spinlock_t xa_lock; 218f8d5d0ccSMatthew Wilcox /* private: The rest of the data structure is not to be used directly. */ 219f8d5d0ccSMatthew Wilcox gfp_t xa_flags; 220f8d5d0ccSMatthew Wilcox void __rcu * xa_head; 221f8d5d0ccSMatthew Wilcox }; 222f8d5d0ccSMatthew Wilcox 223f8d5d0ccSMatthew Wilcox #define XARRAY_INIT(name, flags) { \ 224f8d5d0ccSMatthew Wilcox .xa_lock = __SPIN_LOCK_UNLOCKED(name.xa_lock), \ 225f8d5d0ccSMatthew Wilcox .xa_flags = flags, \ 226f8d5d0ccSMatthew Wilcox .xa_head = NULL, \ 227f8d5d0ccSMatthew Wilcox } 228f8d5d0ccSMatthew Wilcox 229f8d5d0ccSMatthew Wilcox /** 230f8d5d0ccSMatthew Wilcox * DEFINE_XARRAY_FLAGS() - Define an XArray with custom flags. 231f8d5d0ccSMatthew Wilcox * @name: A string that names your XArray. 232f8d5d0ccSMatthew Wilcox * @flags: XA_FLAG values. 233f8d5d0ccSMatthew Wilcox * 234f8d5d0ccSMatthew Wilcox * This is intended for file scope definitions of XArrays. It declares 235f8d5d0ccSMatthew Wilcox * and initialises an empty XArray with the chosen name and flags. It is 236f8d5d0ccSMatthew Wilcox * equivalent to calling xa_init_flags() on the array, but it does the 237f8d5d0ccSMatthew Wilcox * initialisation at compiletime instead of runtime. 238f8d5d0ccSMatthew Wilcox */ 239f8d5d0ccSMatthew Wilcox #define DEFINE_XARRAY_FLAGS(name, flags) \ 240f8d5d0ccSMatthew Wilcox struct xarray name = XARRAY_INIT(name, flags) 241f8d5d0ccSMatthew Wilcox 242f8d5d0ccSMatthew Wilcox /** 243f8d5d0ccSMatthew Wilcox * DEFINE_XARRAY() - Define an XArray. 244f8d5d0ccSMatthew Wilcox * @name: A string that names your XArray. 245f8d5d0ccSMatthew Wilcox * 246f8d5d0ccSMatthew Wilcox * This is intended for file scope definitions of XArrays. It declares 247f8d5d0ccSMatthew Wilcox * and initialises an empty XArray with the chosen name. It is equivalent 248f8d5d0ccSMatthew Wilcox * to calling xa_init() on the array, but it does the initialisation at 249f8d5d0ccSMatthew Wilcox * compiletime instead of runtime. 250f8d5d0ccSMatthew Wilcox */ 251f8d5d0ccSMatthew Wilcox #define DEFINE_XARRAY(name) DEFINE_XARRAY_FLAGS(name, 0) 252f8d5d0ccSMatthew Wilcox 253f8d5d0ccSMatthew Wilcox void xa_init_flags(struct xarray *, gfp_t flags); 254*ad3d6c72SMatthew Wilcox void *xa_load(struct xarray *, unsigned long index); 255f8d5d0ccSMatthew Wilcox 256f8d5d0ccSMatthew Wilcox /** 257f8d5d0ccSMatthew Wilcox * xa_init() - Initialise an empty XArray. 258f8d5d0ccSMatthew Wilcox * @xa: XArray. 259f8d5d0ccSMatthew Wilcox * 260f8d5d0ccSMatthew Wilcox * An empty XArray is full of NULL entries. 261f8d5d0ccSMatthew Wilcox * 262f8d5d0ccSMatthew Wilcox * Context: Any context. 263f8d5d0ccSMatthew Wilcox */ 264f8d5d0ccSMatthew Wilcox static inline void xa_init(struct xarray *xa) 265f8d5d0ccSMatthew Wilcox { 266f8d5d0ccSMatthew Wilcox xa_init_flags(xa, 0); 267f8d5d0ccSMatthew Wilcox } 268f8d5d0ccSMatthew Wilcox 269*ad3d6c72SMatthew Wilcox /** 270*ad3d6c72SMatthew Wilcox * xa_empty() - Determine if an array has any present entries. 271*ad3d6c72SMatthew Wilcox * @xa: XArray. 272*ad3d6c72SMatthew Wilcox * 273*ad3d6c72SMatthew Wilcox * Context: Any context. 274*ad3d6c72SMatthew Wilcox * Return: %true if the array contains only NULL pointers. 275*ad3d6c72SMatthew Wilcox */ 276*ad3d6c72SMatthew Wilcox static inline bool xa_empty(const struct xarray *xa) 277*ad3d6c72SMatthew Wilcox { 278*ad3d6c72SMatthew Wilcox return xa->xa_head == NULL; 279*ad3d6c72SMatthew Wilcox } 280*ad3d6c72SMatthew Wilcox 281f6bb2a2cSMatthew Wilcox #define xa_trylock(xa) spin_trylock(&(xa)->xa_lock) 282f6bb2a2cSMatthew Wilcox #define xa_lock(xa) spin_lock(&(xa)->xa_lock) 283f6bb2a2cSMatthew Wilcox #define xa_unlock(xa) spin_unlock(&(xa)->xa_lock) 284f6bb2a2cSMatthew Wilcox #define xa_lock_bh(xa) spin_lock_bh(&(xa)->xa_lock) 285f6bb2a2cSMatthew Wilcox #define xa_unlock_bh(xa) spin_unlock_bh(&(xa)->xa_lock) 286f6bb2a2cSMatthew Wilcox #define xa_lock_irq(xa) spin_lock_irq(&(xa)->xa_lock) 287f6bb2a2cSMatthew Wilcox #define xa_unlock_irq(xa) spin_unlock_irq(&(xa)->xa_lock) 288f6bb2a2cSMatthew Wilcox #define xa_lock_irqsave(xa, flags) \ 289f6bb2a2cSMatthew Wilcox spin_lock_irqsave(&(xa)->xa_lock, flags) 290f6bb2a2cSMatthew Wilcox #define xa_unlock_irqrestore(xa, flags) \ 291f6bb2a2cSMatthew Wilcox spin_unlock_irqrestore(&(xa)->xa_lock, flags) 292f6bb2a2cSMatthew Wilcox 29302c02bf1SMatthew Wilcox /* Everything below here is the Advanced API. Proceed with caution. */ 29402c02bf1SMatthew Wilcox 29502c02bf1SMatthew Wilcox /* 29602c02bf1SMatthew Wilcox * The xarray is constructed out of a set of 'chunks' of pointers. Choosing 29702c02bf1SMatthew Wilcox * the best chunk size requires some tradeoffs. A power of two recommends 29802c02bf1SMatthew Wilcox * itself so that we can walk the tree based purely on shifts and masks. 29902c02bf1SMatthew Wilcox * Generally, the larger the better; as the number of slots per level of the 30002c02bf1SMatthew Wilcox * tree increases, the less tall the tree needs to be. But that needs to be 30102c02bf1SMatthew Wilcox * balanced against the memory consumption of each node. On a 64-bit system, 30202c02bf1SMatthew Wilcox * xa_node is currently 576 bytes, and we get 7 of them per 4kB page. If we 30302c02bf1SMatthew Wilcox * doubled the number of slots per node, we'd get only 3 nodes per 4kB page. 30402c02bf1SMatthew Wilcox */ 30502c02bf1SMatthew Wilcox #ifndef XA_CHUNK_SHIFT 30602c02bf1SMatthew Wilcox #define XA_CHUNK_SHIFT (CONFIG_BASE_SMALL ? 4 : 6) 30702c02bf1SMatthew Wilcox #endif 30802c02bf1SMatthew Wilcox #define XA_CHUNK_SIZE (1UL << XA_CHUNK_SHIFT) 30902c02bf1SMatthew Wilcox #define XA_CHUNK_MASK (XA_CHUNK_SIZE - 1) 31001959dfeSMatthew Wilcox #define XA_MAX_MARKS 3 31101959dfeSMatthew Wilcox #define XA_MARK_LONGS DIV_ROUND_UP(XA_CHUNK_SIZE, BITS_PER_LONG) 31201959dfeSMatthew Wilcox 31301959dfeSMatthew Wilcox /* 31401959dfeSMatthew Wilcox * @count is the count of every non-NULL element in the ->slots array 31501959dfeSMatthew Wilcox * whether that is a value entry, a retry entry, a user pointer, 31601959dfeSMatthew Wilcox * a sibling entry or a pointer to the next level of the tree. 31701959dfeSMatthew Wilcox * @nr_values is the count of every element in ->slots which is 31801959dfeSMatthew Wilcox * either a value entry or a sibling of a value entry. 31901959dfeSMatthew Wilcox */ 32001959dfeSMatthew Wilcox struct xa_node { 32101959dfeSMatthew Wilcox unsigned char shift; /* Bits remaining in each slot */ 32201959dfeSMatthew Wilcox unsigned char offset; /* Slot offset in parent */ 32301959dfeSMatthew Wilcox unsigned char count; /* Total entry count */ 32401959dfeSMatthew Wilcox unsigned char nr_values; /* Value entry count */ 32501959dfeSMatthew Wilcox struct xa_node __rcu *parent; /* NULL at top of tree */ 32601959dfeSMatthew Wilcox struct xarray *array; /* The array we belong to */ 32701959dfeSMatthew Wilcox union { 32801959dfeSMatthew Wilcox struct list_head private_list; /* For tree user */ 32901959dfeSMatthew Wilcox struct rcu_head rcu_head; /* Used when freeing node */ 33001959dfeSMatthew Wilcox }; 33101959dfeSMatthew Wilcox void __rcu *slots[XA_CHUNK_SIZE]; 33201959dfeSMatthew Wilcox union { 33301959dfeSMatthew Wilcox unsigned long tags[XA_MAX_MARKS][XA_MARK_LONGS]; 33401959dfeSMatthew Wilcox unsigned long marks[XA_MAX_MARKS][XA_MARK_LONGS]; 33501959dfeSMatthew Wilcox }; 33601959dfeSMatthew Wilcox }; 33702c02bf1SMatthew Wilcox 338*ad3d6c72SMatthew Wilcox void xa_dump(const struct xarray *); 339*ad3d6c72SMatthew Wilcox void xa_dump_node(const struct xa_node *); 340*ad3d6c72SMatthew Wilcox 341*ad3d6c72SMatthew Wilcox #ifdef XA_DEBUG 342*ad3d6c72SMatthew Wilcox #define XA_BUG_ON(xa, x) do { \ 343*ad3d6c72SMatthew Wilcox if (x) { \ 344*ad3d6c72SMatthew Wilcox xa_dump(xa); \ 345*ad3d6c72SMatthew Wilcox BUG(); \ 346*ad3d6c72SMatthew Wilcox } \ 347*ad3d6c72SMatthew Wilcox } while (0) 348*ad3d6c72SMatthew Wilcox #define XA_NODE_BUG_ON(node, x) do { \ 349*ad3d6c72SMatthew Wilcox if (x) { \ 350*ad3d6c72SMatthew Wilcox if (node) xa_dump_node(node); \ 351*ad3d6c72SMatthew Wilcox BUG(); \ 352*ad3d6c72SMatthew Wilcox } \ 353*ad3d6c72SMatthew Wilcox } while (0) 354*ad3d6c72SMatthew Wilcox #else 355*ad3d6c72SMatthew Wilcox #define XA_BUG_ON(xa, x) do { } while (0) 356*ad3d6c72SMatthew Wilcox #define XA_NODE_BUG_ON(node, x) do { } while (0) 357*ad3d6c72SMatthew Wilcox #endif 358*ad3d6c72SMatthew Wilcox 359*ad3d6c72SMatthew Wilcox /* Private */ 360*ad3d6c72SMatthew Wilcox static inline void *xa_head(const struct xarray *xa) 361*ad3d6c72SMatthew Wilcox { 362*ad3d6c72SMatthew Wilcox return rcu_dereference_check(xa->xa_head, 363*ad3d6c72SMatthew Wilcox lockdep_is_held(&xa->xa_lock)); 364*ad3d6c72SMatthew Wilcox } 365*ad3d6c72SMatthew Wilcox 366*ad3d6c72SMatthew Wilcox /* Private */ 367*ad3d6c72SMatthew Wilcox static inline void *xa_head_locked(const struct xarray *xa) 368*ad3d6c72SMatthew Wilcox { 369*ad3d6c72SMatthew Wilcox return rcu_dereference_protected(xa->xa_head, 370*ad3d6c72SMatthew Wilcox lockdep_is_held(&xa->xa_lock)); 371*ad3d6c72SMatthew Wilcox } 372*ad3d6c72SMatthew Wilcox 373*ad3d6c72SMatthew Wilcox /* Private */ 374*ad3d6c72SMatthew Wilcox static inline void *xa_entry(const struct xarray *xa, 375*ad3d6c72SMatthew Wilcox const struct xa_node *node, unsigned int offset) 376*ad3d6c72SMatthew Wilcox { 377*ad3d6c72SMatthew Wilcox XA_NODE_BUG_ON(node, offset >= XA_CHUNK_SIZE); 378*ad3d6c72SMatthew Wilcox return rcu_dereference_check(node->slots[offset], 379*ad3d6c72SMatthew Wilcox lockdep_is_held(&xa->xa_lock)); 380*ad3d6c72SMatthew Wilcox } 381*ad3d6c72SMatthew Wilcox 382*ad3d6c72SMatthew Wilcox /* Private */ 383*ad3d6c72SMatthew Wilcox static inline void *xa_entry_locked(const struct xarray *xa, 384*ad3d6c72SMatthew Wilcox const struct xa_node *node, unsigned int offset) 385*ad3d6c72SMatthew Wilcox { 386*ad3d6c72SMatthew Wilcox XA_NODE_BUG_ON(node, offset >= XA_CHUNK_SIZE); 387*ad3d6c72SMatthew Wilcox return rcu_dereference_protected(node->slots[offset], 388*ad3d6c72SMatthew Wilcox lockdep_is_held(&xa->xa_lock)); 389*ad3d6c72SMatthew Wilcox } 390*ad3d6c72SMatthew Wilcox 391*ad3d6c72SMatthew Wilcox /* Private */ 392*ad3d6c72SMatthew Wilcox static inline struct xa_node *xa_to_node(const void *entry) 393*ad3d6c72SMatthew Wilcox { 394*ad3d6c72SMatthew Wilcox return (struct xa_node *)((unsigned long)entry - 2); 395*ad3d6c72SMatthew Wilcox } 396*ad3d6c72SMatthew Wilcox 39702c02bf1SMatthew Wilcox /* Private */ 39802c02bf1SMatthew Wilcox static inline bool xa_is_node(const void *entry) 39902c02bf1SMatthew Wilcox { 40002c02bf1SMatthew Wilcox return xa_is_internal(entry) && (unsigned long)entry > 4096; 40102c02bf1SMatthew Wilcox } 40202c02bf1SMatthew Wilcox 40302c02bf1SMatthew Wilcox /* Private */ 40402c02bf1SMatthew Wilcox static inline void *xa_mk_sibling(unsigned int offset) 40502c02bf1SMatthew Wilcox { 40602c02bf1SMatthew Wilcox return xa_mk_internal(offset); 40702c02bf1SMatthew Wilcox } 40802c02bf1SMatthew Wilcox 40902c02bf1SMatthew Wilcox /* Private */ 41002c02bf1SMatthew Wilcox static inline unsigned long xa_to_sibling(const void *entry) 41102c02bf1SMatthew Wilcox { 41202c02bf1SMatthew Wilcox return xa_to_internal(entry); 41302c02bf1SMatthew Wilcox } 41402c02bf1SMatthew Wilcox 41502c02bf1SMatthew Wilcox /** 41602c02bf1SMatthew Wilcox * xa_is_sibling() - Is the entry a sibling entry? 41702c02bf1SMatthew Wilcox * @entry: Entry retrieved from the XArray 41802c02bf1SMatthew Wilcox * 41902c02bf1SMatthew Wilcox * Return: %true if the entry is a sibling entry. 42002c02bf1SMatthew Wilcox */ 42102c02bf1SMatthew Wilcox static inline bool xa_is_sibling(const void *entry) 42202c02bf1SMatthew Wilcox { 42302c02bf1SMatthew Wilcox return IS_ENABLED(CONFIG_XARRAY_MULTI) && xa_is_internal(entry) && 42402c02bf1SMatthew Wilcox (entry < xa_mk_sibling(XA_CHUNK_SIZE - 1)); 42502c02bf1SMatthew Wilcox } 42602c02bf1SMatthew Wilcox 42702c02bf1SMatthew Wilcox #define XA_RETRY_ENTRY xa_mk_internal(256) 42802c02bf1SMatthew Wilcox 429*ad3d6c72SMatthew Wilcox /** 430*ad3d6c72SMatthew Wilcox * xa_is_retry() - Is the entry a retry entry? 431*ad3d6c72SMatthew Wilcox * @entry: Entry retrieved from the XArray 432*ad3d6c72SMatthew Wilcox * 433*ad3d6c72SMatthew Wilcox * Return: %true if the entry is a retry entry. 434*ad3d6c72SMatthew Wilcox */ 435*ad3d6c72SMatthew Wilcox static inline bool xa_is_retry(const void *entry) 436*ad3d6c72SMatthew Wilcox { 437*ad3d6c72SMatthew Wilcox return unlikely(entry == XA_RETRY_ENTRY); 438*ad3d6c72SMatthew Wilcox } 439*ad3d6c72SMatthew Wilcox 440*ad3d6c72SMatthew Wilcox /** 441*ad3d6c72SMatthew Wilcox * typedef xa_update_node_t - A callback function from the XArray. 442*ad3d6c72SMatthew Wilcox * @node: The node which is being processed 443*ad3d6c72SMatthew Wilcox * 444*ad3d6c72SMatthew Wilcox * This function is called every time the XArray updates the count of 445*ad3d6c72SMatthew Wilcox * present and value entries in a node. It allows advanced users to 446*ad3d6c72SMatthew Wilcox * maintain the private_list in the node. 447*ad3d6c72SMatthew Wilcox * 448*ad3d6c72SMatthew Wilcox * Context: The xa_lock is held and interrupts may be disabled. 449*ad3d6c72SMatthew Wilcox * Implementations should not drop the xa_lock, nor re-enable 450*ad3d6c72SMatthew Wilcox * interrupts. 451*ad3d6c72SMatthew Wilcox */ 452*ad3d6c72SMatthew Wilcox typedef void (*xa_update_node_t)(struct xa_node *node); 453*ad3d6c72SMatthew Wilcox 454*ad3d6c72SMatthew Wilcox /* 455*ad3d6c72SMatthew Wilcox * The xa_state is opaque to its users. It contains various different pieces 456*ad3d6c72SMatthew Wilcox * of state involved in the current operation on the XArray. It should be 457*ad3d6c72SMatthew Wilcox * declared on the stack and passed between the various internal routines. 458*ad3d6c72SMatthew Wilcox * The various elements in it should not be accessed directly, but only 459*ad3d6c72SMatthew Wilcox * through the provided accessor functions. The below documentation is for 460*ad3d6c72SMatthew Wilcox * the benefit of those working on the code, not for users of the XArray. 461*ad3d6c72SMatthew Wilcox * 462*ad3d6c72SMatthew Wilcox * @xa_node usually points to the xa_node containing the slot we're operating 463*ad3d6c72SMatthew Wilcox * on (and @xa_offset is the offset in the slots array). If there is a 464*ad3d6c72SMatthew Wilcox * single entry in the array at index 0, there are no allocated xa_nodes to 465*ad3d6c72SMatthew Wilcox * point to, and so we store %NULL in @xa_node. @xa_node is set to 466*ad3d6c72SMatthew Wilcox * the value %XAS_RESTART if the xa_state is not walked to the correct 467*ad3d6c72SMatthew Wilcox * position in the tree of nodes for this operation. If an error occurs 468*ad3d6c72SMatthew Wilcox * during an operation, it is set to an %XAS_ERROR value. If we run off the 469*ad3d6c72SMatthew Wilcox * end of the allocated nodes, it is set to %XAS_BOUNDS. 470*ad3d6c72SMatthew Wilcox */ 471*ad3d6c72SMatthew Wilcox struct xa_state { 472*ad3d6c72SMatthew Wilcox struct xarray *xa; 473*ad3d6c72SMatthew Wilcox unsigned long xa_index; 474*ad3d6c72SMatthew Wilcox unsigned char xa_shift; 475*ad3d6c72SMatthew Wilcox unsigned char xa_sibs; 476*ad3d6c72SMatthew Wilcox unsigned char xa_offset; 477*ad3d6c72SMatthew Wilcox unsigned char xa_pad; /* Helps gcc generate better code */ 478*ad3d6c72SMatthew Wilcox struct xa_node *xa_node; 479*ad3d6c72SMatthew Wilcox struct xa_node *xa_alloc; 480*ad3d6c72SMatthew Wilcox xa_update_node_t xa_update; 481*ad3d6c72SMatthew Wilcox }; 482*ad3d6c72SMatthew Wilcox 483*ad3d6c72SMatthew Wilcox /* 484*ad3d6c72SMatthew Wilcox * We encode errnos in the xas->xa_node. If an error has happened, we need to 485*ad3d6c72SMatthew Wilcox * drop the lock to fix it, and once we've done so the xa_state is invalid. 486*ad3d6c72SMatthew Wilcox */ 487*ad3d6c72SMatthew Wilcox #define XA_ERROR(errno) ((struct xa_node *)(((unsigned long)errno << 2) | 2UL)) 488*ad3d6c72SMatthew Wilcox #define XAS_BOUNDS ((struct xa_node *)1UL) 489*ad3d6c72SMatthew Wilcox #define XAS_RESTART ((struct xa_node *)3UL) 490*ad3d6c72SMatthew Wilcox 491*ad3d6c72SMatthew Wilcox #define __XA_STATE(array, index, shift, sibs) { \ 492*ad3d6c72SMatthew Wilcox .xa = array, \ 493*ad3d6c72SMatthew Wilcox .xa_index = index, \ 494*ad3d6c72SMatthew Wilcox .xa_shift = shift, \ 495*ad3d6c72SMatthew Wilcox .xa_sibs = sibs, \ 496*ad3d6c72SMatthew Wilcox .xa_offset = 0, \ 497*ad3d6c72SMatthew Wilcox .xa_pad = 0, \ 498*ad3d6c72SMatthew Wilcox .xa_node = XAS_RESTART, \ 499*ad3d6c72SMatthew Wilcox .xa_alloc = NULL, \ 500*ad3d6c72SMatthew Wilcox .xa_update = NULL \ 501*ad3d6c72SMatthew Wilcox } 502*ad3d6c72SMatthew Wilcox 503*ad3d6c72SMatthew Wilcox /** 504*ad3d6c72SMatthew Wilcox * XA_STATE() - Declare an XArray operation state. 505*ad3d6c72SMatthew Wilcox * @name: Name of this operation state (usually xas). 506*ad3d6c72SMatthew Wilcox * @array: Array to operate on. 507*ad3d6c72SMatthew Wilcox * @index: Initial index of interest. 508*ad3d6c72SMatthew Wilcox * 509*ad3d6c72SMatthew Wilcox * Declare and initialise an xa_state on the stack. 510*ad3d6c72SMatthew Wilcox */ 511*ad3d6c72SMatthew Wilcox #define XA_STATE(name, array, index) \ 512*ad3d6c72SMatthew Wilcox struct xa_state name = __XA_STATE(array, index, 0, 0) 513*ad3d6c72SMatthew Wilcox 514*ad3d6c72SMatthew Wilcox /** 515*ad3d6c72SMatthew Wilcox * XA_STATE_ORDER() - Declare an XArray operation state. 516*ad3d6c72SMatthew Wilcox * @name: Name of this operation state (usually xas). 517*ad3d6c72SMatthew Wilcox * @array: Array to operate on. 518*ad3d6c72SMatthew Wilcox * @index: Initial index of interest. 519*ad3d6c72SMatthew Wilcox * @order: Order of entry. 520*ad3d6c72SMatthew Wilcox * 521*ad3d6c72SMatthew Wilcox * Declare and initialise an xa_state on the stack. This variant of 522*ad3d6c72SMatthew Wilcox * XA_STATE() allows you to specify the 'order' of the element you 523*ad3d6c72SMatthew Wilcox * want to operate on.` 524*ad3d6c72SMatthew Wilcox */ 525*ad3d6c72SMatthew Wilcox #define XA_STATE_ORDER(name, array, index, order) \ 526*ad3d6c72SMatthew Wilcox struct xa_state name = __XA_STATE(array, \ 527*ad3d6c72SMatthew Wilcox (index >> order) << order, \ 528*ad3d6c72SMatthew Wilcox order - (order % XA_CHUNK_SHIFT), \ 529*ad3d6c72SMatthew Wilcox (1U << (order % XA_CHUNK_SHIFT)) - 1) 530*ad3d6c72SMatthew Wilcox 531*ad3d6c72SMatthew Wilcox #define xas_marked(xas, mark) xa_marked((xas)->xa, (mark)) 532*ad3d6c72SMatthew Wilcox #define xas_trylock(xas) xa_trylock((xas)->xa) 533*ad3d6c72SMatthew Wilcox #define xas_lock(xas) xa_lock((xas)->xa) 534*ad3d6c72SMatthew Wilcox #define xas_unlock(xas) xa_unlock((xas)->xa) 535*ad3d6c72SMatthew Wilcox #define xas_lock_bh(xas) xa_lock_bh((xas)->xa) 536*ad3d6c72SMatthew Wilcox #define xas_unlock_bh(xas) xa_unlock_bh((xas)->xa) 537*ad3d6c72SMatthew Wilcox #define xas_lock_irq(xas) xa_lock_irq((xas)->xa) 538*ad3d6c72SMatthew Wilcox #define xas_unlock_irq(xas) xa_unlock_irq((xas)->xa) 539*ad3d6c72SMatthew Wilcox #define xas_lock_irqsave(xas, flags) \ 540*ad3d6c72SMatthew Wilcox xa_lock_irqsave((xas)->xa, flags) 541*ad3d6c72SMatthew Wilcox #define xas_unlock_irqrestore(xas, flags) \ 542*ad3d6c72SMatthew Wilcox xa_unlock_irqrestore((xas)->xa, flags) 543*ad3d6c72SMatthew Wilcox 544*ad3d6c72SMatthew Wilcox /** 545*ad3d6c72SMatthew Wilcox * xas_error() - Return an errno stored in the xa_state. 546*ad3d6c72SMatthew Wilcox * @xas: XArray operation state. 547*ad3d6c72SMatthew Wilcox * 548*ad3d6c72SMatthew Wilcox * Return: 0 if no error has been noted. A negative errno if one has. 549*ad3d6c72SMatthew Wilcox */ 550*ad3d6c72SMatthew Wilcox static inline int xas_error(const struct xa_state *xas) 551*ad3d6c72SMatthew Wilcox { 552*ad3d6c72SMatthew Wilcox return xa_err(xas->xa_node); 553*ad3d6c72SMatthew Wilcox } 554*ad3d6c72SMatthew Wilcox 555*ad3d6c72SMatthew Wilcox /** 556*ad3d6c72SMatthew Wilcox * xas_set_err() - Note an error in the xa_state. 557*ad3d6c72SMatthew Wilcox * @xas: XArray operation state. 558*ad3d6c72SMatthew Wilcox * @err: Negative error number. 559*ad3d6c72SMatthew Wilcox * 560*ad3d6c72SMatthew Wilcox * Only call this function with a negative @err; zero or positive errors 561*ad3d6c72SMatthew Wilcox * will probably not behave the way you think they should. If you want 562*ad3d6c72SMatthew Wilcox * to clear the error from an xa_state, use xas_reset(). 563*ad3d6c72SMatthew Wilcox */ 564*ad3d6c72SMatthew Wilcox static inline void xas_set_err(struct xa_state *xas, long err) 565*ad3d6c72SMatthew Wilcox { 566*ad3d6c72SMatthew Wilcox xas->xa_node = XA_ERROR(err); 567*ad3d6c72SMatthew Wilcox } 568*ad3d6c72SMatthew Wilcox 569*ad3d6c72SMatthew Wilcox /** 570*ad3d6c72SMatthew Wilcox * xas_invalid() - Is the xas in a retry or error state? 571*ad3d6c72SMatthew Wilcox * @xas: XArray operation state. 572*ad3d6c72SMatthew Wilcox * 573*ad3d6c72SMatthew Wilcox * Return: %true if the xas cannot be used for operations. 574*ad3d6c72SMatthew Wilcox */ 575*ad3d6c72SMatthew Wilcox static inline bool xas_invalid(const struct xa_state *xas) 576*ad3d6c72SMatthew Wilcox { 577*ad3d6c72SMatthew Wilcox return (unsigned long)xas->xa_node & 3; 578*ad3d6c72SMatthew Wilcox } 579*ad3d6c72SMatthew Wilcox 580*ad3d6c72SMatthew Wilcox /** 581*ad3d6c72SMatthew Wilcox * xas_valid() - Is the xas a valid cursor into the array? 582*ad3d6c72SMatthew Wilcox * @xas: XArray operation state. 583*ad3d6c72SMatthew Wilcox * 584*ad3d6c72SMatthew Wilcox * Return: %true if the xas can be used for operations. 585*ad3d6c72SMatthew Wilcox */ 586*ad3d6c72SMatthew Wilcox static inline bool xas_valid(const struct xa_state *xas) 587*ad3d6c72SMatthew Wilcox { 588*ad3d6c72SMatthew Wilcox return !xas_invalid(xas); 589*ad3d6c72SMatthew Wilcox } 590*ad3d6c72SMatthew Wilcox 591*ad3d6c72SMatthew Wilcox /** 592*ad3d6c72SMatthew Wilcox * xas_reset() - Reset an XArray operation state. 593*ad3d6c72SMatthew Wilcox * @xas: XArray operation state. 594*ad3d6c72SMatthew Wilcox * 595*ad3d6c72SMatthew Wilcox * Resets the error or walk state of the @xas so future walks of the 596*ad3d6c72SMatthew Wilcox * array will start from the root. Use this if you have dropped the 597*ad3d6c72SMatthew Wilcox * xarray lock and want to reuse the xa_state. 598*ad3d6c72SMatthew Wilcox * 599*ad3d6c72SMatthew Wilcox * Context: Any context. 600*ad3d6c72SMatthew Wilcox */ 601*ad3d6c72SMatthew Wilcox static inline void xas_reset(struct xa_state *xas) 602*ad3d6c72SMatthew Wilcox { 603*ad3d6c72SMatthew Wilcox xas->xa_node = XAS_RESTART; 604*ad3d6c72SMatthew Wilcox } 605*ad3d6c72SMatthew Wilcox 606*ad3d6c72SMatthew Wilcox /** 607*ad3d6c72SMatthew Wilcox * xas_retry() - Retry the operation if appropriate. 608*ad3d6c72SMatthew Wilcox * @xas: XArray operation state. 609*ad3d6c72SMatthew Wilcox * @entry: Entry from xarray. 610*ad3d6c72SMatthew Wilcox * 611*ad3d6c72SMatthew Wilcox * The advanced functions may sometimes return an internal entry, such as 612*ad3d6c72SMatthew Wilcox * a retry entry or a zero entry. This function sets up the @xas to restart 613*ad3d6c72SMatthew Wilcox * the walk from the head of the array if needed. 614*ad3d6c72SMatthew Wilcox * 615*ad3d6c72SMatthew Wilcox * Context: Any context. 616*ad3d6c72SMatthew Wilcox * Return: true if the operation needs to be retried. 617*ad3d6c72SMatthew Wilcox */ 618*ad3d6c72SMatthew Wilcox static inline bool xas_retry(struct xa_state *xas, const void *entry) 619*ad3d6c72SMatthew Wilcox { 620*ad3d6c72SMatthew Wilcox if (!xa_is_retry(entry)) 621*ad3d6c72SMatthew Wilcox return false; 622*ad3d6c72SMatthew Wilcox xas_reset(xas); 623*ad3d6c72SMatthew Wilcox return true; 624*ad3d6c72SMatthew Wilcox } 625*ad3d6c72SMatthew Wilcox 626*ad3d6c72SMatthew Wilcox void *xas_load(struct xa_state *); 627*ad3d6c72SMatthew Wilcox 628*ad3d6c72SMatthew Wilcox /** 629*ad3d6c72SMatthew Wilcox * xas_reload() - Refetch an entry from the xarray. 630*ad3d6c72SMatthew Wilcox * @xas: XArray operation state. 631*ad3d6c72SMatthew Wilcox * 632*ad3d6c72SMatthew Wilcox * Use this function to check that a previously loaded entry still has 633*ad3d6c72SMatthew Wilcox * the same value. This is useful for the lockless pagecache lookup where 634*ad3d6c72SMatthew Wilcox * we walk the array with only the RCU lock to protect us, lock the page, 635*ad3d6c72SMatthew Wilcox * then check that the page hasn't moved since we looked it up. 636*ad3d6c72SMatthew Wilcox * 637*ad3d6c72SMatthew Wilcox * The caller guarantees that @xas is still valid. If it may be in an 638*ad3d6c72SMatthew Wilcox * error or restart state, call xas_load() instead. 639*ad3d6c72SMatthew Wilcox * 640*ad3d6c72SMatthew Wilcox * Return: The entry at this location in the xarray. 641*ad3d6c72SMatthew Wilcox */ 642*ad3d6c72SMatthew Wilcox static inline void *xas_reload(struct xa_state *xas) 643*ad3d6c72SMatthew Wilcox { 644*ad3d6c72SMatthew Wilcox struct xa_node *node = xas->xa_node; 645*ad3d6c72SMatthew Wilcox 646*ad3d6c72SMatthew Wilcox if (node) 647*ad3d6c72SMatthew Wilcox return xa_entry(xas->xa, node, xas->xa_offset); 648*ad3d6c72SMatthew Wilcox return xa_head(xas->xa); 649*ad3d6c72SMatthew Wilcox } 650*ad3d6c72SMatthew Wilcox 651f6bb2a2cSMatthew Wilcox #endif /* _LINUX_XARRAY_H */ 652