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