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 |
| 35 | * 256: Retry entry |
Matthew Wilcox | ad3d6c7 | 2017-11-07 14:57:46 -0500 | [diff] [blame] | 36 | * |
| 37 | * Errors are also represented as internal entries, but use the negative |
| 38 | * space (-4094 to -2). They're never stored in the slots array; only |
| 39 | * returned by the normal API. |
Matthew Wilcox | 3159f94 | 2017-11-03 13:30:42 -0400 | [diff] [blame] | 40 | */ |
| 41 | |
| 42 | #define BITS_PER_XA_VALUE (BITS_PER_LONG - 1) |
| 43 | |
| 44 | /** |
| 45 | * xa_mk_value() - Create an XArray entry from an integer. |
| 46 | * @v: Value to store in XArray. |
| 47 | * |
| 48 | * Context: Any context. |
| 49 | * Return: An entry suitable for storing in the XArray. |
| 50 | */ |
| 51 | static inline void *xa_mk_value(unsigned long v) |
| 52 | { |
| 53 | WARN_ON((long)v < 0); |
| 54 | return (void *)((v << 1) | 1); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * xa_to_value() - Get value stored in an XArray entry. |
| 59 | * @entry: XArray entry. |
| 60 | * |
| 61 | * Context: Any context. |
| 62 | * Return: The value stored in the XArray entry. |
| 63 | */ |
| 64 | static inline unsigned long xa_to_value(const void *entry) |
| 65 | { |
| 66 | return (unsigned long)entry >> 1; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * xa_is_value() - Determine if an entry is a value. |
| 71 | * @entry: XArray entry. |
| 72 | * |
| 73 | * Context: Any context. |
| 74 | * Return: True if the entry is a value, false if it is a pointer. |
| 75 | */ |
| 76 | static inline bool xa_is_value(const void *entry) |
| 77 | { |
| 78 | return (unsigned long)entry & 1; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * xa_tag_pointer() - Create an XArray entry for a tagged pointer. |
| 83 | * @p: Plain pointer. |
| 84 | * @tag: Tag value (0, 1 or 3). |
| 85 | * |
| 86 | * If the user of the XArray prefers, they can tag their pointers instead |
| 87 | * of storing value entries. Three tags are available (0, 1 and 3). |
| 88 | * These are distinct from the xa_mark_t as they are not replicated up |
| 89 | * through the array and cannot be searched for. |
| 90 | * |
| 91 | * Context: Any context. |
| 92 | * Return: An XArray entry. |
| 93 | */ |
| 94 | static inline void *xa_tag_pointer(void *p, unsigned long tag) |
| 95 | { |
| 96 | return (void *)((unsigned long)p | tag); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * xa_untag_pointer() - Turn an XArray entry into a plain pointer. |
| 101 | * @entry: XArray entry. |
| 102 | * |
| 103 | * If you have stored a tagged pointer in the XArray, call this function |
| 104 | * to get the untagged version of the pointer. |
| 105 | * |
| 106 | * Context: Any context. |
| 107 | * Return: A pointer. |
| 108 | */ |
| 109 | static inline void *xa_untag_pointer(void *entry) |
| 110 | { |
| 111 | return (void *)((unsigned long)entry & ~3UL); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * xa_pointer_tag() - Get the tag stored in an XArray entry. |
| 116 | * @entry: XArray entry. |
| 117 | * |
| 118 | * If you have stored a tagged pointer in the XArray, call this function |
| 119 | * to get the tag of that pointer. |
| 120 | * |
| 121 | * Context: Any context. |
| 122 | * Return: A tag. |
| 123 | */ |
| 124 | static inline unsigned int xa_pointer_tag(void *entry) |
| 125 | { |
| 126 | return (unsigned long)entry & 3UL; |
| 127 | } |
Matthew Wilcox | f6bb2a2 | 2018-04-10 16:36:52 -0700 | [diff] [blame] | 128 | |
Matthew Wilcox | 02c02bf | 2017-11-03 23:09:45 -0400 | [diff] [blame] | 129 | /* |
| 130 | * xa_mk_internal() - Create an internal entry. |
| 131 | * @v: Value to turn into an internal entry. |
| 132 | * |
| 133 | * Context: Any context. |
| 134 | * Return: An XArray internal entry corresponding to this value. |
| 135 | */ |
| 136 | static inline void *xa_mk_internal(unsigned long v) |
| 137 | { |
| 138 | return (void *)((v << 2) | 2); |
| 139 | } |
| 140 | |
| 141 | /* |
| 142 | * xa_to_internal() - Extract the value from an internal entry. |
| 143 | * @entry: XArray entry. |
| 144 | * |
| 145 | * Context: Any context. |
| 146 | * Return: The value which was stored in the internal entry. |
| 147 | */ |
| 148 | static inline unsigned long xa_to_internal(const void *entry) |
| 149 | { |
| 150 | return (unsigned long)entry >> 2; |
| 151 | } |
| 152 | |
| 153 | /* |
| 154 | * xa_is_internal() - Is the entry an internal entry? |
| 155 | * @entry: XArray entry. |
| 156 | * |
| 157 | * Context: Any context. |
| 158 | * Return: %true if the entry is an internal entry. |
| 159 | */ |
| 160 | static inline bool xa_is_internal(const void *entry) |
| 161 | { |
| 162 | return ((unsigned long)entry & 3) == 2; |
| 163 | } |
| 164 | |
Matthew Wilcox | f8d5d0c | 2017-11-07 16:30:10 -0500 | [diff] [blame] | 165 | /** |
Matthew Wilcox | ad3d6c7 | 2017-11-07 14:57:46 -0500 | [diff] [blame] | 166 | * xa_is_err() - Report whether an XArray operation returned an error |
| 167 | * @entry: Result from calling an XArray function |
| 168 | * |
| 169 | * If an XArray operation cannot complete an operation, it will return |
| 170 | * a special value indicating an error. This function tells you |
| 171 | * whether an error occurred; xa_err() tells you which error occurred. |
| 172 | * |
| 173 | * Context: Any context. |
| 174 | * Return: %true if the entry indicates an error. |
| 175 | */ |
| 176 | static inline bool xa_is_err(const void *entry) |
| 177 | { |
| 178 | return unlikely(xa_is_internal(entry)); |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * xa_err() - Turn an XArray result into an errno. |
| 183 | * @entry: Result from calling an XArray function. |
| 184 | * |
| 185 | * If an XArray operation cannot complete an operation, it will return |
| 186 | * a special pointer value which encodes an errno. This function extracts |
| 187 | * the errno from the pointer value, or returns 0 if the pointer does not |
| 188 | * represent an errno. |
| 189 | * |
| 190 | * Context: Any context. |
| 191 | * Return: A negative errno or 0. |
| 192 | */ |
| 193 | static inline int xa_err(void *entry) |
| 194 | { |
| 195 | /* xa_to_internal() would not do sign extension. */ |
| 196 | if (xa_is_err(entry)) |
| 197 | return (long)entry >> 2; |
| 198 | return 0; |
| 199 | } |
| 200 | |
Matthew Wilcox | 9b89a03 | 2017-11-10 09:34:31 -0500 | [diff] [blame] | 201 | typedef unsigned __bitwise xa_mark_t; |
| 202 | #define XA_MARK_0 ((__force xa_mark_t)0U) |
| 203 | #define XA_MARK_1 ((__force xa_mark_t)1U) |
| 204 | #define XA_MARK_2 ((__force xa_mark_t)2U) |
| 205 | #define XA_PRESENT ((__force xa_mark_t)8U) |
| 206 | #define XA_MARK_MAX XA_MARK_2 |
| 207 | |
Matthew Wilcox | 58d6ea3 | 2017-11-10 15:15:08 -0500 | [diff] [blame] | 208 | enum xa_lock_type { |
| 209 | XA_LOCK_IRQ = 1, |
| 210 | XA_LOCK_BH = 2, |
| 211 | }; |
| 212 | |
Matthew Wilcox | 9b89a03 | 2017-11-10 09:34:31 -0500 | [diff] [blame] | 213 | /* |
| 214 | * Values for xa_flags. The radix tree stores its GFP flags in the xa_flags, |
| 215 | * and we remain compatible with that. |
| 216 | */ |
Matthew Wilcox | 58d6ea3 | 2017-11-10 15:15:08 -0500 | [diff] [blame] | 217 | #define XA_FLAGS_LOCK_IRQ ((__force gfp_t)XA_LOCK_IRQ) |
| 218 | #define XA_FLAGS_LOCK_BH ((__force gfp_t)XA_LOCK_BH) |
Matthew Wilcox | 9b89a03 | 2017-11-10 09:34:31 -0500 | [diff] [blame] | 219 | #define XA_FLAGS_MARK(mark) ((__force gfp_t)((1U << __GFP_BITS_SHIFT) << \ |
| 220 | (__force unsigned)(mark))) |
| 221 | |
Matthew Wilcox | ad3d6c7 | 2017-11-07 14:57:46 -0500 | [diff] [blame] | 222 | /** |
Matthew Wilcox | f8d5d0c | 2017-11-07 16:30:10 -0500 | [diff] [blame] | 223 | * struct xarray - The anchor of the XArray. |
| 224 | * @xa_lock: Lock that protects the contents of the XArray. |
| 225 | * |
| 226 | * To use the xarray, define it statically or embed it in your data structure. |
| 227 | * It is a very small data structure, so it does not usually make sense to |
| 228 | * allocate it separately and keep a pointer to it in your data structure. |
| 229 | * |
| 230 | * You may use the xa_lock to protect your own data structures as well. |
| 231 | */ |
| 232 | /* |
| 233 | * If all of the entries in the array are NULL, @xa_head is a NULL pointer. |
| 234 | * If the only non-NULL entry in the array is at index 0, @xa_head is that |
| 235 | * entry. If any other entry in the array is non-NULL, @xa_head points |
| 236 | * to an @xa_node. |
| 237 | */ |
| 238 | struct xarray { |
| 239 | spinlock_t xa_lock; |
| 240 | /* private: The rest of the data structure is not to be used directly. */ |
| 241 | gfp_t xa_flags; |
| 242 | void __rcu * xa_head; |
| 243 | }; |
| 244 | |
| 245 | #define XARRAY_INIT(name, flags) { \ |
| 246 | .xa_lock = __SPIN_LOCK_UNLOCKED(name.xa_lock), \ |
| 247 | .xa_flags = flags, \ |
| 248 | .xa_head = NULL, \ |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * DEFINE_XARRAY_FLAGS() - Define an XArray with custom flags. |
| 253 | * @name: A string that names your XArray. |
| 254 | * @flags: XA_FLAG values. |
| 255 | * |
| 256 | * This is intended for file scope definitions of XArrays. It declares |
| 257 | * and initialises an empty XArray with the chosen name and flags. It is |
| 258 | * equivalent to calling xa_init_flags() on the array, but it does the |
| 259 | * initialisation at compiletime instead of runtime. |
| 260 | */ |
| 261 | #define DEFINE_XARRAY_FLAGS(name, flags) \ |
| 262 | struct xarray name = XARRAY_INIT(name, flags) |
| 263 | |
| 264 | /** |
| 265 | * DEFINE_XARRAY() - Define an XArray. |
| 266 | * @name: A string that names your XArray. |
| 267 | * |
| 268 | * This is intended for file scope definitions of XArrays. It declares |
| 269 | * and initialises an empty XArray with the chosen name. It is equivalent |
| 270 | * to calling xa_init() on the array, but it does the initialisation at |
| 271 | * compiletime instead of runtime. |
| 272 | */ |
| 273 | #define DEFINE_XARRAY(name) DEFINE_XARRAY_FLAGS(name, 0) |
| 274 | |
| 275 | void xa_init_flags(struct xarray *, gfp_t flags); |
Matthew Wilcox | ad3d6c7 | 2017-11-07 14:57:46 -0500 | [diff] [blame] | 276 | void *xa_load(struct xarray *, unsigned long index); |
Matthew Wilcox | 58d6ea3 | 2017-11-10 15:15:08 -0500 | [diff] [blame] | 277 | void *xa_store(struct xarray *, unsigned long index, void *entry, gfp_t); |
Matthew Wilcox | 41aec91 | 2017-11-10 15:34:55 -0500 | [diff] [blame] | 278 | void *xa_cmpxchg(struct xarray *, unsigned long index, |
| 279 | void *old, void *entry, gfp_t); |
Matthew Wilcox | 9b89a03 | 2017-11-10 09:34:31 -0500 | [diff] [blame] | 280 | bool xa_get_mark(struct xarray *, unsigned long index, xa_mark_t); |
| 281 | void xa_set_mark(struct xarray *, unsigned long index, xa_mark_t); |
| 282 | void xa_clear_mark(struct xarray *, unsigned long index, xa_mark_t); |
Matthew Wilcox | b803b42 | 2017-11-14 08:30:11 -0500 | [diff] [blame] | 283 | void *xa_find(struct xarray *xa, unsigned long *index, |
| 284 | unsigned long max, xa_mark_t) __attribute__((nonnull(2))); |
| 285 | void *xa_find_after(struct xarray *xa, unsigned long *index, |
| 286 | unsigned long max, xa_mark_t) __attribute__((nonnull(2))); |
Matthew Wilcox | 80a0a1a | 2017-11-14 16:42:22 -0500 | [diff] [blame^] | 287 | unsigned int xa_extract(struct xarray *, void **dst, unsigned long start, |
| 288 | unsigned long max, unsigned int n, xa_mark_t); |
Matthew Wilcox | f8d5d0c | 2017-11-07 16:30:10 -0500 | [diff] [blame] | 289 | |
| 290 | /** |
| 291 | * xa_init() - Initialise an empty XArray. |
| 292 | * @xa: XArray. |
| 293 | * |
| 294 | * An empty XArray is full of NULL entries. |
| 295 | * |
| 296 | * Context: Any context. |
| 297 | */ |
| 298 | static inline void xa_init(struct xarray *xa) |
| 299 | { |
| 300 | xa_init_flags(xa, 0); |
| 301 | } |
| 302 | |
Matthew Wilcox | ad3d6c7 | 2017-11-07 14:57:46 -0500 | [diff] [blame] | 303 | /** |
| 304 | * xa_empty() - Determine if an array has any present entries. |
| 305 | * @xa: XArray. |
| 306 | * |
| 307 | * Context: Any context. |
| 308 | * Return: %true if the array contains only NULL pointers. |
| 309 | */ |
| 310 | static inline bool xa_empty(const struct xarray *xa) |
| 311 | { |
| 312 | return xa->xa_head == NULL; |
| 313 | } |
| 314 | |
Matthew Wilcox | 9b89a03 | 2017-11-10 09:34:31 -0500 | [diff] [blame] | 315 | /** |
| 316 | * xa_marked() - Inquire whether any entry in this array has a mark set |
| 317 | * @xa: Array |
| 318 | * @mark: Mark value |
| 319 | * |
| 320 | * Context: Any context. |
| 321 | * Return: %true if any entry has this mark set. |
| 322 | */ |
| 323 | static inline bool xa_marked(const struct xarray *xa, xa_mark_t mark) |
| 324 | { |
| 325 | return xa->xa_flags & XA_FLAGS_MARK(mark); |
| 326 | } |
| 327 | |
Matthew Wilcox | 58d6ea3 | 2017-11-10 15:15:08 -0500 | [diff] [blame] | 328 | /** |
| 329 | * xa_erase() - Erase this entry from the XArray. |
| 330 | * @xa: XArray. |
| 331 | * @index: Index of entry. |
| 332 | * |
| 333 | * This function is the equivalent of calling xa_store() with %NULL as |
| 334 | * the third argument. The XArray does not need to allocate memory, so |
| 335 | * the user does not need to provide GFP flags. |
| 336 | * |
| 337 | * Context: Process context. Takes and releases the xa_lock. |
| 338 | * Return: The entry which used to be at this index. |
| 339 | */ |
| 340 | static inline void *xa_erase(struct xarray *xa, unsigned long index) |
| 341 | { |
| 342 | return xa_store(xa, index, NULL, 0); |
| 343 | } |
| 344 | |
Matthew Wilcox | 41aec91 | 2017-11-10 15:34:55 -0500 | [diff] [blame] | 345 | /** |
| 346 | * xa_insert() - Store this entry in the XArray unless another entry is |
| 347 | * already present. |
| 348 | * @xa: XArray. |
| 349 | * @index: Index into array. |
| 350 | * @entry: New entry. |
| 351 | * @gfp: Memory allocation flags. |
| 352 | * |
| 353 | * If you would rather see the existing entry in the array, use xa_cmpxchg(). |
| 354 | * This function is for users who don't care what the entry is, only that |
| 355 | * one is present. |
| 356 | * |
| 357 | * Context: Process context. Takes and releases the xa_lock. |
| 358 | * May sleep if the @gfp flags permit. |
| 359 | * Return: 0 if the store succeeded. -EEXIST if another entry was present. |
| 360 | * -ENOMEM if memory could not be allocated. |
| 361 | */ |
| 362 | static inline int xa_insert(struct xarray *xa, unsigned long index, |
| 363 | void *entry, gfp_t gfp) |
| 364 | { |
| 365 | void *curr = xa_cmpxchg(xa, index, NULL, entry, gfp); |
| 366 | if (!curr) |
| 367 | return 0; |
| 368 | if (xa_is_err(curr)) |
| 369 | return xa_err(curr); |
| 370 | return -EEXIST; |
| 371 | } |
| 372 | |
Matthew Wilcox | b803b42 | 2017-11-14 08:30:11 -0500 | [diff] [blame] | 373 | /** |
| 374 | * xa_for_each() - Iterate over a portion of an XArray. |
| 375 | * @xa: XArray. |
| 376 | * @entry: Entry retrieved from array. |
| 377 | * @index: Index of @entry. |
| 378 | * @max: Maximum index to retrieve from array. |
| 379 | * @filter: Selection criterion. |
| 380 | * |
| 381 | * Initialise @index to the lowest index you want to retrieve from the |
| 382 | * array. During the iteration, @entry will have the value of the entry |
| 383 | * stored in @xa at @index. The iteration will skip all entries in the |
| 384 | * array which do not match @filter. You may modify @index during the |
| 385 | * iteration if you want to skip or reprocess indices. It is safe to modify |
| 386 | * the array during the iteration. At the end of the iteration, @entry will |
| 387 | * be set to NULL and @index will have a value less than or equal to max. |
| 388 | * |
| 389 | * xa_for_each() is O(n.log(n)) while xas_for_each() is O(n). You have |
| 390 | * to handle your own locking with xas_for_each(), and if you have to unlock |
| 391 | * after each iteration, it will also end up being O(n.log(n)). xa_for_each() |
| 392 | * will spin if it hits a retry entry; if you intend to see retry entries, |
| 393 | * you should use the xas_for_each() iterator instead. The xas_for_each() |
| 394 | * iterator will expand into more inline code than xa_for_each(). |
| 395 | * |
| 396 | * Context: Any context. Takes and releases the RCU lock. |
| 397 | */ |
| 398 | #define xa_for_each(xa, entry, index, max, filter) \ |
| 399 | for (entry = xa_find(xa, &index, max, filter); entry; \ |
| 400 | entry = xa_find_after(xa, &index, max, filter)) |
| 401 | |
Matthew Wilcox | f6bb2a2 | 2018-04-10 16:36:52 -0700 | [diff] [blame] | 402 | #define xa_trylock(xa) spin_trylock(&(xa)->xa_lock) |
| 403 | #define xa_lock(xa) spin_lock(&(xa)->xa_lock) |
| 404 | #define xa_unlock(xa) spin_unlock(&(xa)->xa_lock) |
| 405 | #define xa_lock_bh(xa) spin_lock_bh(&(xa)->xa_lock) |
| 406 | #define xa_unlock_bh(xa) spin_unlock_bh(&(xa)->xa_lock) |
| 407 | #define xa_lock_irq(xa) spin_lock_irq(&(xa)->xa_lock) |
| 408 | #define xa_unlock_irq(xa) spin_unlock_irq(&(xa)->xa_lock) |
| 409 | #define xa_lock_irqsave(xa, flags) \ |
| 410 | spin_lock_irqsave(&(xa)->xa_lock, flags) |
| 411 | #define xa_unlock_irqrestore(xa, flags) \ |
| 412 | spin_unlock_irqrestore(&(xa)->xa_lock, flags) |
| 413 | |
Matthew Wilcox | 9b89a03 | 2017-11-10 09:34:31 -0500 | [diff] [blame] | 414 | /* |
Matthew Wilcox | 58d6ea3 | 2017-11-10 15:15:08 -0500 | [diff] [blame] | 415 | * Versions of the normal API which require the caller to hold the |
| 416 | * xa_lock. If the GFP flags allow it, they will drop the lock to |
| 417 | * allocate memory, then reacquire it afterwards. These functions |
| 418 | * may also re-enable interrupts if the XArray flags indicate the |
| 419 | * locking should be interrupt safe. |
Matthew Wilcox | 9b89a03 | 2017-11-10 09:34:31 -0500 | [diff] [blame] | 420 | */ |
Matthew Wilcox | 58d6ea3 | 2017-11-10 15:15:08 -0500 | [diff] [blame] | 421 | void *__xa_erase(struct xarray *, unsigned long index); |
| 422 | void *__xa_store(struct xarray *, unsigned long index, void *entry, gfp_t); |
Matthew Wilcox | 41aec91 | 2017-11-10 15:34:55 -0500 | [diff] [blame] | 423 | void *__xa_cmpxchg(struct xarray *, unsigned long index, void *old, |
| 424 | void *entry, gfp_t); |
Matthew Wilcox | 9b89a03 | 2017-11-10 09:34:31 -0500 | [diff] [blame] | 425 | void __xa_set_mark(struct xarray *, unsigned long index, xa_mark_t); |
| 426 | void __xa_clear_mark(struct xarray *, unsigned long index, xa_mark_t); |
| 427 | |
Matthew Wilcox | 58d6ea3 | 2017-11-10 15:15:08 -0500 | [diff] [blame] | 428 | /** |
Matthew Wilcox | 41aec91 | 2017-11-10 15:34:55 -0500 | [diff] [blame] | 429 | * __xa_insert() - Store this entry in the XArray unless another entry is |
| 430 | * already present. |
| 431 | * @xa: XArray. |
| 432 | * @index: Index into array. |
| 433 | * @entry: New entry. |
| 434 | * @gfp: Memory allocation flags. |
| 435 | * |
| 436 | * If you would rather see the existing entry in the array, use __xa_cmpxchg(). |
| 437 | * This function is for users who don't care what the entry is, only that |
| 438 | * one is present. |
| 439 | * |
| 440 | * Context: Any context. Expects xa_lock to be held on entry. May |
| 441 | * release and reacquire xa_lock if the @gfp flags permit. |
| 442 | * Return: 0 if the store succeeded. -EEXIST if another entry was present. |
| 443 | * -ENOMEM if memory could not be allocated. |
| 444 | */ |
| 445 | static inline int __xa_insert(struct xarray *xa, unsigned long index, |
| 446 | void *entry, gfp_t gfp) |
| 447 | { |
| 448 | void *curr = __xa_cmpxchg(xa, index, NULL, entry, gfp); |
| 449 | if (!curr) |
| 450 | return 0; |
| 451 | if (xa_is_err(curr)) |
| 452 | return xa_err(curr); |
| 453 | return -EEXIST; |
| 454 | } |
| 455 | |
| 456 | /** |
Matthew Wilcox | 58d6ea3 | 2017-11-10 15:15:08 -0500 | [diff] [blame] | 457 | * xa_erase_bh() - Erase this entry from the XArray. |
| 458 | * @xa: XArray. |
| 459 | * @index: Index of entry. |
| 460 | * |
| 461 | * This function is the equivalent of calling xa_store() with %NULL as |
| 462 | * the third argument. The XArray does not need to allocate memory, so |
| 463 | * the user does not need to provide GFP flags. |
| 464 | * |
| 465 | * Context: Process context. Takes and releases the xa_lock while |
| 466 | * disabling softirqs. |
| 467 | * Return: The entry which used to be at this index. |
| 468 | */ |
| 469 | static inline void *xa_erase_bh(struct xarray *xa, unsigned long index) |
| 470 | { |
| 471 | void *entry; |
| 472 | |
| 473 | xa_lock_bh(xa); |
| 474 | entry = __xa_erase(xa, index); |
| 475 | xa_unlock_bh(xa); |
| 476 | |
| 477 | return entry; |
| 478 | } |
| 479 | |
| 480 | /** |
| 481 | * xa_erase_irq() - Erase this entry from the XArray. |
| 482 | * @xa: XArray. |
| 483 | * @index: Index of entry. |
| 484 | * |
| 485 | * This function is the equivalent of calling xa_store() with %NULL as |
| 486 | * the third argument. The XArray does not need to allocate memory, so |
| 487 | * the user does not need to provide GFP flags. |
| 488 | * |
| 489 | * Context: Process context. Takes and releases the xa_lock while |
| 490 | * disabling interrupts. |
| 491 | * Return: The entry which used to be at this index. |
| 492 | */ |
| 493 | static inline void *xa_erase_irq(struct xarray *xa, unsigned long index) |
| 494 | { |
| 495 | void *entry; |
| 496 | |
| 497 | xa_lock_irq(xa); |
| 498 | entry = __xa_erase(xa, index); |
| 499 | xa_unlock_irq(xa); |
| 500 | |
| 501 | return entry; |
| 502 | } |
| 503 | |
Matthew Wilcox | 02c02bf | 2017-11-03 23:09:45 -0400 | [diff] [blame] | 504 | /* Everything below here is the Advanced API. Proceed with caution. */ |
| 505 | |
| 506 | /* |
| 507 | * The xarray is constructed out of a set of 'chunks' of pointers. Choosing |
| 508 | * the best chunk size requires some tradeoffs. A power of two recommends |
| 509 | * itself so that we can walk the tree based purely on shifts and masks. |
| 510 | * Generally, the larger the better; as the number of slots per level of the |
| 511 | * tree increases, the less tall the tree needs to be. But that needs to be |
| 512 | * balanced against the memory consumption of each node. On a 64-bit system, |
| 513 | * xa_node is currently 576 bytes, and we get 7 of them per 4kB page. If we |
| 514 | * doubled the number of slots per node, we'd get only 3 nodes per 4kB page. |
| 515 | */ |
| 516 | #ifndef XA_CHUNK_SHIFT |
| 517 | #define XA_CHUNK_SHIFT (CONFIG_BASE_SMALL ? 4 : 6) |
| 518 | #endif |
| 519 | #define XA_CHUNK_SIZE (1UL << XA_CHUNK_SHIFT) |
| 520 | #define XA_CHUNK_MASK (XA_CHUNK_SIZE - 1) |
Matthew Wilcox | 01959df | 2017-11-09 09:23:56 -0500 | [diff] [blame] | 521 | #define XA_MAX_MARKS 3 |
| 522 | #define XA_MARK_LONGS DIV_ROUND_UP(XA_CHUNK_SIZE, BITS_PER_LONG) |
| 523 | |
| 524 | /* |
| 525 | * @count is the count of every non-NULL element in the ->slots array |
| 526 | * whether that is a value entry, a retry entry, a user pointer, |
| 527 | * a sibling entry or a pointer to the next level of the tree. |
| 528 | * @nr_values is the count of every element in ->slots which is |
| 529 | * either a value entry or a sibling of a value entry. |
| 530 | */ |
| 531 | struct xa_node { |
| 532 | unsigned char shift; /* Bits remaining in each slot */ |
| 533 | unsigned char offset; /* Slot offset in parent */ |
| 534 | unsigned char count; /* Total entry count */ |
| 535 | unsigned char nr_values; /* Value entry count */ |
| 536 | struct xa_node __rcu *parent; /* NULL at top of tree */ |
| 537 | struct xarray *array; /* The array we belong to */ |
| 538 | union { |
| 539 | struct list_head private_list; /* For tree user */ |
| 540 | struct rcu_head rcu_head; /* Used when freeing node */ |
| 541 | }; |
| 542 | void __rcu *slots[XA_CHUNK_SIZE]; |
| 543 | union { |
| 544 | unsigned long tags[XA_MAX_MARKS][XA_MARK_LONGS]; |
| 545 | unsigned long marks[XA_MAX_MARKS][XA_MARK_LONGS]; |
| 546 | }; |
| 547 | }; |
Matthew Wilcox | 02c02bf | 2017-11-03 23:09:45 -0400 | [diff] [blame] | 548 | |
Matthew Wilcox | ad3d6c7 | 2017-11-07 14:57:46 -0500 | [diff] [blame] | 549 | void xa_dump(const struct xarray *); |
| 550 | void xa_dump_node(const struct xa_node *); |
| 551 | |
| 552 | #ifdef XA_DEBUG |
| 553 | #define XA_BUG_ON(xa, x) do { \ |
| 554 | if (x) { \ |
| 555 | xa_dump(xa); \ |
| 556 | BUG(); \ |
| 557 | } \ |
| 558 | } while (0) |
| 559 | #define XA_NODE_BUG_ON(node, x) do { \ |
| 560 | if (x) { \ |
| 561 | if (node) xa_dump_node(node); \ |
| 562 | BUG(); \ |
| 563 | } \ |
| 564 | } while (0) |
| 565 | #else |
| 566 | #define XA_BUG_ON(xa, x) do { } while (0) |
| 567 | #define XA_NODE_BUG_ON(node, x) do { } while (0) |
| 568 | #endif |
| 569 | |
| 570 | /* Private */ |
| 571 | static inline void *xa_head(const struct xarray *xa) |
| 572 | { |
| 573 | return rcu_dereference_check(xa->xa_head, |
| 574 | lockdep_is_held(&xa->xa_lock)); |
| 575 | } |
| 576 | |
| 577 | /* Private */ |
| 578 | static inline void *xa_head_locked(const struct xarray *xa) |
| 579 | { |
| 580 | return rcu_dereference_protected(xa->xa_head, |
| 581 | lockdep_is_held(&xa->xa_lock)); |
| 582 | } |
| 583 | |
| 584 | /* Private */ |
| 585 | static inline void *xa_entry(const struct xarray *xa, |
| 586 | const struct xa_node *node, unsigned int offset) |
| 587 | { |
| 588 | XA_NODE_BUG_ON(node, offset >= XA_CHUNK_SIZE); |
| 589 | return rcu_dereference_check(node->slots[offset], |
| 590 | lockdep_is_held(&xa->xa_lock)); |
| 591 | } |
| 592 | |
| 593 | /* Private */ |
| 594 | static inline void *xa_entry_locked(const struct xarray *xa, |
| 595 | const struct xa_node *node, unsigned int offset) |
| 596 | { |
| 597 | XA_NODE_BUG_ON(node, offset >= XA_CHUNK_SIZE); |
| 598 | return rcu_dereference_protected(node->slots[offset], |
| 599 | lockdep_is_held(&xa->xa_lock)); |
| 600 | } |
| 601 | |
| 602 | /* Private */ |
Matthew Wilcox | 9b89a03 | 2017-11-10 09:34:31 -0500 | [diff] [blame] | 603 | static inline struct xa_node *xa_parent(const struct xarray *xa, |
| 604 | const struct xa_node *node) |
| 605 | { |
| 606 | return rcu_dereference_check(node->parent, |
| 607 | lockdep_is_held(&xa->xa_lock)); |
| 608 | } |
| 609 | |
| 610 | /* Private */ |
| 611 | static inline struct xa_node *xa_parent_locked(const struct xarray *xa, |
| 612 | const struct xa_node *node) |
| 613 | { |
| 614 | return rcu_dereference_protected(node->parent, |
| 615 | lockdep_is_held(&xa->xa_lock)); |
| 616 | } |
| 617 | |
| 618 | /* Private */ |
Matthew Wilcox | 58d6ea3 | 2017-11-10 15:15:08 -0500 | [diff] [blame] | 619 | static inline void *xa_mk_node(const struct xa_node *node) |
| 620 | { |
| 621 | return (void *)((unsigned long)node | 2); |
| 622 | } |
| 623 | |
| 624 | /* Private */ |
Matthew Wilcox | ad3d6c7 | 2017-11-07 14:57:46 -0500 | [diff] [blame] | 625 | static inline struct xa_node *xa_to_node(const void *entry) |
| 626 | { |
| 627 | return (struct xa_node *)((unsigned long)entry - 2); |
| 628 | } |
| 629 | |
Matthew Wilcox | 02c02bf | 2017-11-03 23:09:45 -0400 | [diff] [blame] | 630 | /* Private */ |
| 631 | static inline bool xa_is_node(const void *entry) |
| 632 | { |
| 633 | return xa_is_internal(entry) && (unsigned long)entry > 4096; |
| 634 | } |
| 635 | |
| 636 | /* Private */ |
| 637 | static inline void *xa_mk_sibling(unsigned int offset) |
| 638 | { |
| 639 | return xa_mk_internal(offset); |
| 640 | } |
| 641 | |
| 642 | /* Private */ |
| 643 | static inline unsigned long xa_to_sibling(const void *entry) |
| 644 | { |
| 645 | return xa_to_internal(entry); |
| 646 | } |
| 647 | |
| 648 | /** |
| 649 | * xa_is_sibling() - Is the entry a sibling entry? |
| 650 | * @entry: Entry retrieved from the XArray |
| 651 | * |
| 652 | * Return: %true if the entry is a sibling entry. |
| 653 | */ |
| 654 | static inline bool xa_is_sibling(const void *entry) |
| 655 | { |
| 656 | return IS_ENABLED(CONFIG_XARRAY_MULTI) && xa_is_internal(entry) && |
| 657 | (entry < xa_mk_sibling(XA_CHUNK_SIZE - 1)); |
| 658 | } |
| 659 | |
| 660 | #define XA_RETRY_ENTRY xa_mk_internal(256) |
| 661 | |
Matthew Wilcox | ad3d6c7 | 2017-11-07 14:57:46 -0500 | [diff] [blame] | 662 | /** |
| 663 | * xa_is_retry() - Is the entry a retry entry? |
| 664 | * @entry: Entry retrieved from the XArray |
| 665 | * |
| 666 | * Return: %true if the entry is a retry entry. |
| 667 | */ |
| 668 | static inline bool xa_is_retry(const void *entry) |
| 669 | { |
| 670 | return unlikely(entry == XA_RETRY_ENTRY); |
| 671 | } |
| 672 | |
| 673 | /** |
| 674 | * typedef xa_update_node_t - A callback function from the XArray. |
| 675 | * @node: The node which is being processed |
| 676 | * |
| 677 | * This function is called every time the XArray updates the count of |
| 678 | * present and value entries in a node. It allows advanced users to |
| 679 | * maintain the private_list in the node. |
| 680 | * |
| 681 | * Context: The xa_lock is held and interrupts may be disabled. |
| 682 | * Implementations should not drop the xa_lock, nor re-enable |
| 683 | * interrupts. |
| 684 | */ |
| 685 | typedef void (*xa_update_node_t)(struct xa_node *node); |
| 686 | |
| 687 | /* |
| 688 | * The xa_state is opaque to its users. It contains various different pieces |
| 689 | * of state involved in the current operation on the XArray. It should be |
| 690 | * declared on the stack and passed between the various internal routines. |
| 691 | * The various elements in it should not be accessed directly, but only |
| 692 | * through the provided accessor functions. The below documentation is for |
| 693 | * the benefit of those working on the code, not for users of the XArray. |
| 694 | * |
| 695 | * @xa_node usually points to the xa_node containing the slot we're operating |
| 696 | * on (and @xa_offset is the offset in the slots array). If there is a |
| 697 | * single entry in the array at index 0, there are no allocated xa_nodes to |
| 698 | * point to, and so we store %NULL in @xa_node. @xa_node is set to |
| 699 | * the value %XAS_RESTART if the xa_state is not walked to the correct |
| 700 | * position in the tree of nodes for this operation. If an error occurs |
| 701 | * during an operation, it is set to an %XAS_ERROR value. If we run off the |
| 702 | * end of the allocated nodes, it is set to %XAS_BOUNDS. |
| 703 | */ |
| 704 | struct xa_state { |
| 705 | struct xarray *xa; |
| 706 | unsigned long xa_index; |
| 707 | unsigned char xa_shift; |
| 708 | unsigned char xa_sibs; |
| 709 | unsigned char xa_offset; |
| 710 | unsigned char xa_pad; /* Helps gcc generate better code */ |
| 711 | struct xa_node *xa_node; |
| 712 | struct xa_node *xa_alloc; |
| 713 | xa_update_node_t xa_update; |
| 714 | }; |
| 715 | |
| 716 | /* |
| 717 | * We encode errnos in the xas->xa_node. If an error has happened, we need to |
| 718 | * drop the lock to fix it, and once we've done so the xa_state is invalid. |
| 719 | */ |
| 720 | #define XA_ERROR(errno) ((struct xa_node *)(((unsigned long)errno << 2) | 2UL)) |
| 721 | #define XAS_BOUNDS ((struct xa_node *)1UL) |
| 722 | #define XAS_RESTART ((struct xa_node *)3UL) |
| 723 | |
| 724 | #define __XA_STATE(array, index, shift, sibs) { \ |
| 725 | .xa = array, \ |
| 726 | .xa_index = index, \ |
| 727 | .xa_shift = shift, \ |
| 728 | .xa_sibs = sibs, \ |
| 729 | .xa_offset = 0, \ |
| 730 | .xa_pad = 0, \ |
| 731 | .xa_node = XAS_RESTART, \ |
| 732 | .xa_alloc = NULL, \ |
| 733 | .xa_update = NULL \ |
| 734 | } |
| 735 | |
| 736 | /** |
| 737 | * XA_STATE() - Declare an XArray operation state. |
| 738 | * @name: Name of this operation state (usually xas). |
| 739 | * @array: Array to operate on. |
| 740 | * @index: Initial index of interest. |
| 741 | * |
| 742 | * Declare and initialise an xa_state on the stack. |
| 743 | */ |
| 744 | #define XA_STATE(name, array, index) \ |
| 745 | struct xa_state name = __XA_STATE(array, index, 0, 0) |
| 746 | |
| 747 | /** |
| 748 | * XA_STATE_ORDER() - Declare an XArray operation state. |
| 749 | * @name: Name of this operation state (usually xas). |
| 750 | * @array: Array to operate on. |
| 751 | * @index: Initial index of interest. |
| 752 | * @order: Order of entry. |
| 753 | * |
| 754 | * Declare and initialise an xa_state on the stack. This variant of |
| 755 | * XA_STATE() allows you to specify the 'order' of the element you |
| 756 | * want to operate on.` |
| 757 | */ |
| 758 | #define XA_STATE_ORDER(name, array, index, order) \ |
| 759 | struct xa_state name = __XA_STATE(array, \ |
| 760 | (index >> order) << order, \ |
| 761 | order - (order % XA_CHUNK_SHIFT), \ |
| 762 | (1U << (order % XA_CHUNK_SHIFT)) - 1) |
| 763 | |
| 764 | #define xas_marked(xas, mark) xa_marked((xas)->xa, (mark)) |
| 765 | #define xas_trylock(xas) xa_trylock((xas)->xa) |
| 766 | #define xas_lock(xas) xa_lock((xas)->xa) |
| 767 | #define xas_unlock(xas) xa_unlock((xas)->xa) |
| 768 | #define xas_lock_bh(xas) xa_lock_bh((xas)->xa) |
| 769 | #define xas_unlock_bh(xas) xa_unlock_bh((xas)->xa) |
| 770 | #define xas_lock_irq(xas) xa_lock_irq((xas)->xa) |
| 771 | #define xas_unlock_irq(xas) xa_unlock_irq((xas)->xa) |
| 772 | #define xas_lock_irqsave(xas, flags) \ |
| 773 | xa_lock_irqsave((xas)->xa, flags) |
| 774 | #define xas_unlock_irqrestore(xas, flags) \ |
| 775 | xa_unlock_irqrestore((xas)->xa, flags) |
| 776 | |
| 777 | /** |
| 778 | * xas_error() - Return an errno stored in the xa_state. |
| 779 | * @xas: XArray operation state. |
| 780 | * |
| 781 | * Return: 0 if no error has been noted. A negative errno if one has. |
| 782 | */ |
| 783 | static inline int xas_error(const struct xa_state *xas) |
| 784 | { |
| 785 | return xa_err(xas->xa_node); |
| 786 | } |
| 787 | |
| 788 | /** |
| 789 | * xas_set_err() - Note an error in the xa_state. |
| 790 | * @xas: XArray operation state. |
| 791 | * @err: Negative error number. |
| 792 | * |
| 793 | * Only call this function with a negative @err; zero or positive errors |
| 794 | * will probably not behave the way you think they should. If you want |
| 795 | * to clear the error from an xa_state, use xas_reset(). |
| 796 | */ |
| 797 | static inline void xas_set_err(struct xa_state *xas, long err) |
| 798 | { |
| 799 | xas->xa_node = XA_ERROR(err); |
| 800 | } |
| 801 | |
| 802 | /** |
| 803 | * xas_invalid() - Is the xas in a retry or error state? |
| 804 | * @xas: XArray operation state. |
| 805 | * |
| 806 | * Return: %true if the xas cannot be used for operations. |
| 807 | */ |
| 808 | static inline bool xas_invalid(const struct xa_state *xas) |
| 809 | { |
| 810 | return (unsigned long)xas->xa_node & 3; |
| 811 | } |
| 812 | |
| 813 | /** |
| 814 | * xas_valid() - Is the xas a valid cursor into the array? |
| 815 | * @xas: XArray operation state. |
| 816 | * |
| 817 | * Return: %true if the xas can be used for operations. |
| 818 | */ |
| 819 | static inline bool xas_valid(const struct xa_state *xas) |
| 820 | { |
| 821 | return !xas_invalid(xas); |
| 822 | } |
| 823 | |
Matthew Wilcox | 9b89a03 | 2017-11-10 09:34:31 -0500 | [diff] [blame] | 824 | /* True if the pointer is something other than a node */ |
| 825 | static inline bool xas_not_node(struct xa_node *node) |
| 826 | { |
| 827 | return ((unsigned long)node & 3) || !node; |
| 828 | } |
| 829 | |
Matthew Wilcox | 58d6ea3 | 2017-11-10 15:15:08 -0500 | [diff] [blame] | 830 | /* True if the node represents head-of-tree, RESTART or BOUNDS */ |
| 831 | static inline bool xas_top(struct xa_node *node) |
| 832 | { |
| 833 | return node <= XAS_RESTART; |
| 834 | } |
| 835 | |
Matthew Wilcox | ad3d6c7 | 2017-11-07 14:57:46 -0500 | [diff] [blame] | 836 | /** |
| 837 | * xas_reset() - Reset an XArray operation state. |
| 838 | * @xas: XArray operation state. |
| 839 | * |
| 840 | * Resets the error or walk state of the @xas so future walks of the |
| 841 | * array will start from the root. Use this if you have dropped the |
| 842 | * xarray lock and want to reuse the xa_state. |
| 843 | * |
| 844 | * Context: Any context. |
| 845 | */ |
| 846 | static inline void xas_reset(struct xa_state *xas) |
| 847 | { |
| 848 | xas->xa_node = XAS_RESTART; |
| 849 | } |
| 850 | |
| 851 | /** |
| 852 | * xas_retry() - Retry the operation if appropriate. |
| 853 | * @xas: XArray operation state. |
| 854 | * @entry: Entry from xarray. |
| 855 | * |
| 856 | * The advanced functions may sometimes return an internal entry, such as |
| 857 | * a retry entry or a zero entry. This function sets up the @xas to restart |
| 858 | * the walk from the head of the array if needed. |
| 859 | * |
| 860 | * Context: Any context. |
| 861 | * Return: true if the operation needs to be retried. |
| 862 | */ |
| 863 | static inline bool xas_retry(struct xa_state *xas, const void *entry) |
| 864 | { |
| 865 | if (!xa_is_retry(entry)) |
| 866 | return false; |
| 867 | xas_reset(xas); |
| 868 | return true; |
| 869 | } |
| 870 | |
| 871 | void *xas_load(struct xa_state *); |
Matthew Wilcox | 58d6ea3 | 2017-11-10 15:15:08 -0500 | [diff] [blame] | 872 | void *xas_store(struct xa_state *, void *entry); |
Matthew Wilcox | b803b42 | 2017-11-14 08:30:11 -0500 | [diff] [blame] | 873 | void *xas_find(struct xa_state *, unsigned long max); |
Matthew Wilcox | ad3d6c7 | 2017-11-07 14:57:46 -0500 | [diff] [blame] | 874 | |
Matthew Wilcox | 9b89a03 | 2017-11-10 09:34:31 -0500 | [diff] [blame] | 875 | bool xas_get_mark(const struct xa_state *, xa_mark_t); |
| 876 | void xas_set_mark(const struct xa_state *, xa_mark_t); |
| 877 | void xas_clear_mark(const struct xa_state *, xa_mark_t); |
Matthew Wilcox | b803b42 | 2017-11-14 08:30:11 -0500 | [diff] [blame] | 878 | 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] | 879 | void xas_init_marks(const struct xa_state *); |
| 880 | |
| 881 | bool xas_nomem(struct xa_state *, gfp_t); |
Matthew Wilcox | b803b42 | 2017-11-14 08:30:11 -0500 | [diff] [blame] | 882 | void xas_pause(struct xa_state *); |
Matthew Wilcox | 9b89a03 | 2017-11-10 09:34:31 -0500 | [diff] [blame] | 883 | |
Matthew Wilcox | ad3d6c7 | 2017-11-07 14:57:46 -0500 | [diff] [blame] | 884 | /** |
| 885 | * xas_reload() - Refetch an entry from the xarray. |
| 886 | * @xas: XArray operation state. |
| 887 | * |
| 888 | * Use this function to check that a previously loaded entry still has |
| 889 | * the same value. This is useful for the lockless pagecache lookup where |
| 890 | * we walk the array with only the RCU lock to protect us, lock the page, |
| 891 | * then check that the page hasn't moved since we looked it up. |
| 892 | * |
| 893 | * The caller guarantees that @xas is still valid. If it may be in an |
| 894 | * error or restart state, call xas_load() instead. |
| 895 | * |
| 896 | * Return: The entry at this location in the xarray. |
| 897 | */ |
| 898 | static inline void *xas_reload(struct xa_state *xas) |
| 899 | { |
| 900 | struct xa_node *node = xas->xa_node; |
| 901 | |
| 902 | if (node) |
| 903 | return xa_entry(xas->xa, node, xas->xa_offset); |
| 904 | return xa_head(xas->xa); |
| 905 | } |
| 906 | |
Matthew Wilcox | 58d6ea3 | 2017-11-10 15:15:08 -0500 | [diff] [blame] | 907 | /** |
| 908 | * xas_set() - Set up XArray operation state for a different index. |
| 909 | * @xas: XArray operation state. |
| 910 | * @index: New index into the XArray. |
| 911 | * |
| 912 | * Move the operation state to refer to a different index. This will |
| 913 | * have the effect of starting a walk from the top; see xas_next() |
| 914 | * to move to an adjacent index. |
| 915 | */ |
| 916 | static inline void xas_set(struct xa_state *xas, unsigned long index) |
| 917 | { |
| 918 | xas->xa_index = index; |
| 919 | xas->xa_node = XAS_RESTART; |
| 920 | } |
| 921 | |
| 922 | /** |
| 923 | * xas_set_order() - Set up XArray operation state for a multislot entry. |
| 924 | * @xas: XArray operation state. |
| 925 | * @index: Target of the operation. |
| 926 | * @order: Entry occupies 2^@order indices. |
| 927 | */ |
| 928 | static inline void xas_set_order(struct xa_state *xas, unsigned long index, |
| 929 | unsigned int order) |
| 930 | { |
| 931 | #ifdef CONFIG_XARRAY_MULTI |
| 932 | xas->xa_index = order < BITS_PER_LONG ? (index >> order) << order : 0; |
| 933 | xas->xa_shift = order - (order % XA_CHUNK_SHIFT); |
| 934 | xas->xa_sibs = (1 << (order % XA_CHUNK_SHIFT)) - 1; |
| 935 | xas->xa_node = XAS_RESTART; |
| 936 | #else |
| 937 | BUG_ON(order > 0); |
| 938 | xas_set(xas, index); |
| 939 | #endif |
| 940 | } |
| 941 | |
| 942 | /** |
| 943 | * xas_set_update() - Set up XArray operation state for a callback. |
| 944 | * @xas: XArray operation state. |
| 945 | * @update: Function to call when updating a node. |
| 946 | * |
| 947 | * The XArray can notify a caller after it has updated an xa_node. |
| 948 | * This is advanced functionality and is only needed by the page cache. |
| 949 | */ |
| 950 | static inline void xas_set_update(struct xa_state *xas, xa_update_node_t update) |
| 951 | { |
| 952 | xas->xa_update = update; |
| 953 | } |
| 954 | |
Matthew Wilcox | b803b42 | 2017-11-14 08:30:11 -0500 | [diff] [blame] | 955 | /** |
| 956 | * xas_next_entry() - Advance iterator to next present entry. |
| 957 | * @xas: XArray operation state. |
| 958 | * @max: Highest index to return. |
| 959 | * |
| 960 | * xas_next_entry() is an inline function to optimise xarray traversal for |
| 961 | * speed. It is equivalent to calling xas_find(), and will call xas_find() |
| 962 | * for all the hard cases. |
| 963 | * |
| 964 | * Return: The next present entry after the one currently referred to by @xas. |
| 965 | */ |
| 966 | static inline void *xas_next_entry(struct xa_state *xas, unsigned long max) |
| 967 | { |
| 968 | struct xa_node *node = xas->xa_node; |
| 969 | void *entry; |
| 970 | |
| 971 | if (unlikely(xas_not_node(node) || node->shift || |
| 972 | xas->xa_offset != (xas->xa_index & XA_CHUNK_MASK))) |
| 973 | return xas_find(xas, max); |
| 974 | |
| 975 | do { |
| 976 | if (unlikely(xas->xa_index >= max)) |
| 977 | return xas_find(xas, max); |
| 978 | if (unlikely(xas->xa_offset == XA_CHUNK_MASK)) |
| 979 | return xas_find(xas, max); |
| 980 | entry = xa_entry(xas->xa, node, xas->xa_offset + 1); |
| 981 | if (unlikely(xa_is_internal(entry))) |
| 982 | return xas_find(xas, max); |
| 983 | xas->xa_offset++; |
| 984 | xas->xa_index++; |
| 985 | } while (!entry); |
| 986 | |
| 987 | return entry; |
| 988 | } |
| 989 | |
| 990 | /* Private */ |
| 991 | static inline unsigned int xas_find_chunk(struct xa_state *xas, bool advance, |
| 992 | xa_mark_t mark) |
| 993 | { |
| 994 | unsigned long *addr = xas->xa_node->marks[(__force unsigned)mark]; |
| 995 | unsigned int offset = xas->xa_offset; |
| 996 | |
| 997 | if (advance) |
| 998 | offset++; |
| 999 | if (XA_CHUNK_SIZE == BITS_PER_LONG) { |
| 1000 | if (offset < XA_CHUNK_SIZE) { |
| 1001 | unsigned long data = *addr & (~0UL << offset); |
| 1002 | if (data) |
| 1003 | return __ffs(data); |
| 1004 | } |
| 1005 | return XA_CHUNK_SIZE; |
| 1006 | } |
| 1007 | |
| 1008 | return find_next_bit(addr, XA_CHUNK_SIZE, offset); |
| 1009 | } |
| 1010 | |
| 1011 | /** |
| 1012 | * xas_next_marked() - Advance iterator to next marked entry. |
| 1013 | * @xas: XArray operation state. |
| 1014 | * @max: Highest index to return. |
| 1015 | * @mark: Mark to search for. |
| 1016 | * |
| 1017 | * xas_next_marked() is an inline function to optimise xarray traversal for |
| 1018 | * speed. It is equivalent to calling xas_find_marked(), and will call |
| 1019 | * xas_find_marked() for all the hard cases. |
| 1020 | * |
| 1021 | * Return: The next marked entry after the one currently referred to by @xas. |
| 1022 | */ |
| 1023 | static inline void *xas_next_marked(struct xa_state *xas, unsigned long max, |
| 1024 | xa_mark_t mark) |
| 1025 | { |
| 1026 | struct xa_node *node = xas->xa_node; |
| 1027 | unsigned int offset; |
| 1028 | |
| 1029 | if (unlikely(xas_not_node(node) || node->shift)) |
| 1030 | return xas_find_marked(xas, max, mark); |
| 1031 | offset = xas_find_chunk(xas, true, mark); |
| 1032 | xas->xa_offset = offset; |
| 1033 | xas->xa_index = (xas->xa_index & ~XA_CHUNK_MASK) + offset; |
| 1034 | if (xas->xa_index > max) |
| 1035 | return NULL; |
| 1036 | if (offset == XA_CHUNK_SIZE) |
| 1037 | return xas_find_marked(xas, max, mark); |
| 1038 | return xa_entry(xas->xa, node, offset); |
| 1039 | } |
| 1040 | |
| 1041 | /* |
| 1042 | * If iterating while holding a lock, drop the lock and reschedule |
| 1043 | * every %XA_CHECK_SCHED loops. |
| 1044 | */ |
| 1045 | enum { |
| 1046 | XA_CHECK_SCHED = 4096, |
| 1047 | }; |
| 1048 | |
| 1049 | /** |
| 1050 | * xas_for_each() - Iterate over a range of an XArray. |
| 1051 | * @xas: XArray operation state. |
| 1052 | * @entry: Entry retrieved from the array. |
| 1053 | * @max: Maximum index to retrieve from array. |
| 1054 | * |
| 1055 | * The loop body will be executed for each entry present in the xarray |
| 1056 | * between the current xas position and @max. @entry will be set to |
| 1057 | * the entry retrieved from the xarray. It is safe to delete entries |
| 1058 | * from the array in the loop body. You should hold either the RCU lock |
| 1059 | * or the xa_lock while iterating. If you need to drop the lock, call |
| 1060 | * xas_pause() first. |
| 1061 | */ |
| 1062 | #define xas_for_each(xas, entry, max) \ |
| 1063 | for (entry = xas_find(xas, max); entry; \ |
| 1064 | entry = xas_next_entry(xas, max)) |
| 1065 | |
| 1066 | /** |
| 1067 | * xas_for_each_marked() - Iterate over a range of an XArray. |
| 1068 | * @xas: XArray operation state. |
| 1069 | * @entry: Entry retrieved from the array. |
| 1070 | * @max: Maximum index to retrieve from array. |
| 1071 | * @mark: Mark to search for. |
| 1072 | * |
| 1073 | * The loop body will be executed for each marked entry in the xarray |
| 1074 | * between the current xas position and @max. @entry will be set to |
| 1075 | * the entry retrieved from the xarray. It is safe to delete entries |
| 1076 | * from the array in the loop body. You should hold either the RCU lock |
| 1077 | * or the xa_lock while iterating. If you need to drop the lock, call |
| 1078 | * xas_pause() first. |
| 1079 | */ |
| 1080 | #define xas_for_each_marked(xas, entry, max, mark) \ |
| 1081 | for (entry = xas_find_marked(xas, max, mark); entry; \ |
| 1082 | entry = xas_next_marked(xas, max, mark)) |
| 1083 | |
Matthew Wilcox | f6bb2a2 | 2018-04-10 16:36:52 -0700 | [diff] [blame] | 1084 | #endif /* _LINUX_XARRAY_H */ |