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