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