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