1 /* SPDX-License-Identifier: GPL-2.0+ */
2 #ifndef _LINUX_XARRAY_H
3 #define _LINUX_XARRAY_H
6 * Copyright (c) 2017 Microsoft Corporation
9 * See Documentation/core-api/xarray.rst for how to use the XArray.
12 #include <linux/bug.h>
13 #include <linux/compiler.h>
14 #include <linux/gfp.h>
15 #include <linux/kconfig.h>
16 #include <linux/kernel.h>
17 #include <linux/rcupdate.h>
18 #include <linux/spinlock.h>
19 #include <linux/types.h>
22 * The bottom two bits of the entry determine how the XArray interprets
27 * x1: Value entry or tagged pointer
29 * Attempting to store internal entries in the XArray is a bug.
31 * Most internal entries are pointers to the next node in the tree.
32 * The following internal entries have a special meaning:
34 * 0-62: Sibling entries
38 * Errors are also represented as internal entries, but use the negative
39 * space (-4094 to -2). They're never stored in the slots array; only
40 * returned by the normal API.
43 #define BITS_PER_XA_VALUE (BITS_PER_LONG - 1)
46 * xa_mk_value() - Create an XArray entry from an integer.
47 * @v: Value to store in XArray.
49 * Context: Any context.
50 * Return: An entry suitable for storing in the XArray.
52 static inline void *xa_mk_value(unsigned long v)
55 return (void *)((v << 1) | 1);
59 * xa_to_value() - Get value stored in an XArray entry.
60 * @entry: XArray entry.
62 * Context: Any context.
63 * Return: The value stored in the XArray entry.
65 static inline unsigned long xa_to_value(const void *entry)
67 return (unsigned long)entry >> 1;
71 * xa_is_value() - Determine if an entry is a value.
72 * @entry: XArray entry.
74 * Context: Any context.
75 * Return: True if the entry is a value, false if it is a pointer.
77 static inline bool xa_is_value(const void *entry)
79 return (unsigned long)entry & 1;
83 * xa_tag_pointer() - Create an XArray entry for a tagged pointer.
85 * @tag: Tag value (0, 1 or 3).
87 * If the user of the XArray prefers, they can tag their pointers instead
88 * of storing value entries. Three tags are available (0, 1 and 3).
89 * These are distinct from the xa_mark_t as they are not replicated up
90 * through the array and cannot be searched for.
92 * Context: Any context.
93 * Return: An XArray entry.
95 static inline void *xa_tag_pointer(void *p, unsigned long tag)
97 return (void *)((unsigned long)p | tag);
101 * xa_untag_pointer() - Turn an XArray entry into a plain pointer.
102 * @entry: XArray entry.
104 * If you have stored a tagged pointer in the XArray, call this function
105 * to get the untagged version of the pointer.
107 * Context: Any context.
110 static inline void *xa_untag_pointer(void *entry)
112 return (void *)((unsigned long)entry & ~3UL);
116 * xa_pointer_tag() - Get the tag stored in an XArray entry.
117 * @entry: XArray entry.
119 * If you have stored a tagged pointer in the XArray, call this function
120 * to get the tag of that pointer.
122 * Context: Any context.
125 static inline unsigned int xa_pointer_tag(void *entry)
127 return (unsigned long)entry & 3UL;
131 * xa_mk_internal() - Create an internal entry.
132 * @v: Value to turn into an internal entry.
134 * Context: Any context.
135 * Return: An XArray internal entry corresponding to this value.
137 static inline void *xa_mk_internal(unsigned long v)
139 return (void *)((v << 2) | 2);
143 * xa_to_internal() - Extract the value from an internal entry.
144 * @entry: XArray entry.
146 * Context: Any context.
147 * Return: The value which was stored in the internal entry.
149 static inline unsigned long xa_to_internal(const void *entry)
151 return (unsigned long)entry >> 2;
155 * xa_is_internal() - Is the entry an internal entry?
156 * @entry: XArray entry.
158 * Context: Any context.
159 * Return: %true if the entry is an internal entry.
161 static inline bool xa_is_internal(const void *entry)
163 return ((unsigned long)entry & 3) == 2;
167 * xa_is_err() - Report whether an XArray operation returned an error
168 * @entry: Result from calling an XArray function
170 * If an XArray operation cannot complete an operation, it will return
171 * a special value indicating an error. This function tells you
172 * whether an error occurred; xa_err() tells you which error occurred.
174 * Context: Any context.
175 * Return: %true if the entry indicates an error.
177 static inline bool xa_is_err(const void *entry)
179 return unlikely(xa_is_internal(entry));
183 * xa_err() - Turn an XArray result into an errno.
184 * @entry: Result from calling an XArray function.
186 * If an XArray operation cannot complete an operation, it will return
187 * a special pointer value which encodes an errno. This function extracts
188 * the errno from the pointer value, or returns 0 if the pointer does not
189 * represent an errno.
191 * Context: Any context.
192 * Return: A negative errno or 0.
194 static inline int xa_err(void *entry)
196 /* xa_to_internal() would not do sign extension. */
197 if (xa_is_err(entry))
198 return (long)entry >> 2;
202 typedef unsigned __bitwise xa_mark_t;
203 #define XA_MARK_0 ((__force xa_mark_t)0U)
204 #define XA_MARK_1 ((__force xa_mark_t)1U)
205 #define XA_MARK_2 ((__force xa_mark_t)2U)
206 #define XA_PRESENT ((__force xa_mark_t)8U)
207 #define XA_MARK_MAX XA_MARK_2
208 #define XA_FREE_MARK XA_MARK_0
216 * Values for xa_flags. The radix tree stores its GFP flags in the xa_flags,
217 * and we remain compatible with that.
219 #define XA_FLAGS_LOCK_IRQ ((__force gfp_t)XA_LOCK_IRQ)
220 #define XA_FLAGS_LOCK_BH ((__force gfp_t)XA_LOCK_BH)
221 #define XA_FLAGS_TRACK_FREE ((__force gfp_t)4U)
222 #define XA_FLAGS_MARK(mark) ((__force gfp_t)((1U << __GFP_BITS_SHIFT) << \
223 (__force unsigned)(mark)))
225 #define XA_FLAGS_ALLOC (XA_FLAGS_TRACK_FREE | XA_FLAGS_MARK(XA_FREE_MARK))
228 * struct xarray - The anchor of the XArray.
229 * @xa_lock: Lock that protects the contents of the XArray.
231 * To use the xarray, define it statically or embed it in your data structure.
232 * It is a very small data structure, so it does not usually make sense to
233 * allocate it separately and keep a pointer to it in your data structure.
235 * You may use the xa_lock to protect your own data structures as well.
238 * If all of the entries in the array are NULL, @xa_head is a NULL pointer.
239 * If the only non-NULL entry in the array is at index 0, @xa_head is that
240 * entry. If any other entry in the array is non-NULL, @xa_head points
245 /* private: The rest of the data structure is not to be used directly. */
247 void __rcu * xa_head;
250 #define XARRAY_INIT(name, flags) { \
251 .xa_lock = __SPIN_LOCK_UNLOCKED(name.xa_lock), \
257 * DEFINE_XARRAY_FLAGS() - Define an XArray with custom flags.
258 * @name: A string that names your XArray.
259 * @flags: XA_FLAG values.
261 * This is intended for file scope definitions of XArrays. It declares
262 * and initialises an empty XArray with the chosen name and flags. It is
263 * equivalent to calling xa_init_flags() on the array, but it does the
264 * initialisation at compiletime instead of runtime.
266 #define DEFINE_XARRAY_FLAGS(name, flags) \
267 struct xarray name = XARRAY_INIT(name, flags)
270 * DEFINE_XARRAY() - Define an XArray.
271 * @name: A string that names your XArray.
273 * This is intended for file scope definitions of XArrays. It declares
274 * and initialises an empty XArray with the chosen name. It is equivalent
275 * to calling xa_init() on the array, but it does the initialisation at
276 * compiletime instead of runtime.
278 #define DEFINE_XARRAY(name) DEFINE_XARRAY_FLAGS(name, 0)
281 * DEFINE_XARRAY_ALLOC() - Define an XArray which can allocate IDs.
282 * @name: A string that names your XArray.
284 * This is intended for file scope definitions of allocating XArrays.
285 * See also DEFINE_XARRAY().
287 #define DEFINE_XARRAY_ALLOC(name) DEFINE_XARRAY_FLAGS(name, XA_FLAGS_ALLOC)
289 void xa_init_flags(struct xarray *, gfp_t flags);
290 void *xa_load(struct xarray *, unsigned long index);
291 void *xa_store(struct xarray *, unsigned long index, void *entry, gfp_t);
292 void *xa_cmpxchg(struct xarray *, unsigned long index,
293 void *old, void *entry, gfp_t);
294 void *xa_store_range(struct xarray *, unsigned long first, unsigned long last,
296 bool xa_get_mark(struct xarray *, unsigned long index, xa_mark_t);
297 void xa_set_mark(struct xarray *, unsigned long index, xa_mark_t);
298 void xa_clear_mark(struct xarray *, unsigned long index, xa_mark_t);
299 void *xa_find(struct xarray *xa, unsigned long *index,
300 unsigned long max, xa_mark_t) __attribute__((nonnull(2)));
301 void *xa_find_after(struct xarray *xa, unsigned long *index,
302 unsigned long max, xa_mark_t) __attribute__((nonnull(2)));
303 unsigned int xa_extract(struct xarray *, void **dst, unsigned long start,
304 unsigned long max, unsigned int n, xa_mark_t);
305 void xa_destroy(struct xarray *);
308 * xa_init() - Initialise an empty XArray.
311 * An empty XArray is full of NULL entries.
313 * Context: Any context.
315 static inline void xa_init(struct xarray *xa)
317 xa_init_flags(xa, 0);
321 * xa_empty() - Determine if an array has any present entries.
324 * Context: Any context.
325 * Return: %true if the array contains only NULL pointers.
327 static inline bool xa_empty(const struct xarray *xa)
329 return xa->xa_head == NULL;
333 * xa_marked() - Inquire whether any entry in this array has a mark set
337 * Context: Any context.
338 * Return: %true if any entry has this mark set.
340 static inline bool xa_marked(const struct xarray *xa, xa_mark_t mark)
342 return xa->xa_flags & XA_FLAGS_MARK(mark);
346 * xa_erase() - Erase this entry from the XArray.
348 * @index: Index of entry.
350 * This function is the equivalent of calling xa_store() with %NULL as
351 * the third argument. The XArray does not need to allocate memory, so
352 * the user does not need to provide GFP flags.
354 * Context: Process context. Takes and releases the xa_lock.
355 * Return: The entry which used to be at this index.
357 static inline void *xa_erase(struct xarray *xa, unsigned long index)
359 return xa_store(xa, index, NULL, 0);
363 * xa_insert() - Store this entry in the XArray unless another entry is
366 * @index: Index into array.
368 * @gfp: Memory allocation flags.
370 * If you would rather see the existing entry in the array, use xa_cmpxchg().
371 * This function is for users who don't care what the entry is, only that
374 * Context: Process context. Takes and releases the xa_lock.
375 * May sleep if the @gfp flags permit.
376 * Return: 0 if the store succeeded. -EEXIST if another entry was present.
377 * -ENOMEM if memory could not be allocated.
379 static inline int xa_insert(struct xarray *xa, unsigned long index,
380 void *entry, gfp_t gfp)
382 void *curr = xa_cmpxchg(xa, index, NULL, entry, gfp);
391 * xa_release() - Release a reserved entry.
393 * @index: Index of entry.
395 * After calling xa_reserve(), you can call this function to release the
396 * reservation. If the entry at @index has been stored to, this function
399 static inline void xa_release(struct xarray *xa, unsigned long index)
401 xa_cmpxchg(xa, index, NULL, NULL, 0);
405 * xa_for_each() - Iterate over a portion of an XArray.
407 * @entry: Entry retrieved from array.
408 * @index: Index of @entry.
409 * @max: Maximum index to retrieve from array.
410 * @filter: Selection criterion.
412 * Initialise @index to the lowest index you want to retrieve from the
413 * array. During the iteration, @entry will have the value of the entry
414 * stored in @xa at @index. The iteration will skip all entries in the
415 * array which do not match @filter. You may modify @index during the
416 * iteration if you want to skip or reprocess indices. It is safe to modify
417 * the array during the iteration. At the end of the iteration, @entry will
418 * be set to NULL and @index will have a value less than or equal to max.
420 * xa_for_each() is O(n.log(n)) while xas_for_each() is O(n). You have
421 * to handle your own locking with xas_for_each(), and if you have to unlock
422 * after each iteration, it will also end up being O(n.log(n)). xa_for_each()
423 * will spin if it hits a retry entry; if you intend to see retry entries,
424 * you should use the xas_for_each() iterator instead. The xas_for_each()
425 * iterator will expand into more inline code than xa_for_each().
427 * Context: Any context. Takes and releases the RCU lock.
429 #define xa_for_each(xa, entry, index, max, filter) \
430 for (entry = xa_find(xa, &index, max, filter); entry; \
431 entry = xa_find_after(xa, &index, max, filter))
433 #define xa_trylock(xa) spin_trylock(&(xa)->xa_lock)
434 #define xa_lock(xa) spin_lock(&(xa)->xa_lock)
435 #define xa_unlock(xa) spin_unlock(&(xa)->xa_lock)
436 #define xa_lock_bh(xa) spin_lock_bh(&(xa)->xa_lock)
437 #define xa_unlock_bh(xa) spin_unlock_bh(&(xa)->xa_lock)
438 #define xa_lock_irq(xa) spin_lock_irq(&(xa)->xa_lock)
439 #define xa_unlock_irq(xa) spin_unlock_irq(&(xa)->xa_lock)
440 #define xa_lock_irqsave(xa, flags) \
441 spin_lock_irqsave(&(xa)->xa_lock, flags)
442 #define xa_unlock_irqrestore(xa, flags) \
443 spin_unlock_irqrestore(&(xa)->xa_lock, flags)
446 * Versions of the normal API which require the caller to hold the
447 * xa_lock. If the GFP flags allow it, they will drop the lock to
448 * allocate memory, then reacquire it afterwards. These functions
449 * may also re-enable interrupts if the XArray flags indicate the
450 * locking should be interrupt safe.
452 void *__xa_erase(struct xarray *, unsigned long index);
453 void *__xa_store(struct xarray *, unsigned long index, void *entry, gfp_t);
454 void *__xa_cmpxchg(struct xarray *, unsigned long index, void *old,
456 int __xa_alloc(struct xarray *, u32 *id, u32 max, void *entry, gfp_t);
457 int __xa_reserve(struct xarray *, unsigned long index, gfp_t);
458 void __xa_set_mark(struct xarray *, unsigned long index, xa_mark_t);
459 void __xa_clear_mark(struct xarray *, unsigned long index, xa_mark_t);
462 * __xa_insert() - Store this entry in the XArray unless another entry is
465 * @index: Index into array.
467 * @gfp: Memory allocation flags.
469 * If you would rather see the existing entry in the array, use __xa_cmpxchg().
470 * This function is for users who don't care what the entry is, only that
473 * Context: Any context. Expects xa_lock to be held on entry. May
474 * release and reacquire xa_lock if the @gfp flags permit.
475 * Return: 0 if the store succeeded. -EEXIST if another entry was present.
476 * -ENOMEM if memory could not be allocated.
478 static inline int __xa_insert(struct xarray *xa, unsigned long index,
479 void *entry, gfp_t gfp)
481 void *curr = __xa_cmpxchg(xa, index, NULL, entry, gfp);
490 * xa_erase_bh() - Erase this entry from the XArray.
492 * @index: Index of entry.
494 * This function is the equivalent of calling xa_store() with %NULL as
495 * the third argument. The XArray does not need to allocate memory, so
496 * the user does not need to provide GFP flags.
498 * Context: Process context. Takes and releases the xa_lock while
499 * disabling softirqs.
500 * Return: The entry which used to be at this index.
502 static inline void *xa_erase_bh(struct xarray *xa, unsigned long index)
507 entry = __xa_erase(xa, index);
514 * xa_erase_irq() - Erase this entry from the XArray.
516 * @index: Index of entry.
518 * This function is the equivalent of calling xa_store() with %NULL as
519 * the third argument. The XArray does not need to allocate memory, so
520 * the user does not need to provide GFP flags.
522 * Context: Process context. Takes and releases the xa_lock while
523 * disabling interrupts.
524 * Return: The entry which used to be at this index.
526 static inline void *xa_erase_irq(struct xarray *xa, unsigned long index)
531 entry = __xa_erase(xa, index);
538 * xa_alloc() - Find somewhere to store this entry in the XArray.
540 * @id: Pointer to ID.
541 * @max: Maximum ID to allocate (inclusive).
543 * @gfp: Memory allocation flags.
545 * Allocates an unused ID in the range specified by @id and @max.
546 * Updates the @id pointer with the index, then stores the entry at that
547 * index. A concurrent lookup will not see an uninitialised @id.
549 * Context: Process context. Takes and releases the xa_lock. May sleep if
550 * the @gfp flags permit.
551 * Return: 0 on success, -ENOMEM if memory allocation fails or -ENOSPC if
552 * there is no more space in the XArray.
554 static inline int xa_alloc(struct xarray *xa, u32 *id, u32 max, void *entry,
560 err = __xa_alloc(xa, id, max, entry, gfp);
567 * xa_alloc_bh() - Find somewhere to store this entry in the XArray.
569 * @id: Pointer to ID.
570 * @max: Maximum ID to allocate (inclusive).
572 * @gfp: Memory allocation flags.
574 * Allocates an unused ID in the range specified by @id and @max.
575 * Updates the @id pointer with the index, then stores the entry at that
576 * index. A concurrent lookup will not see an uninitialised @id.
578 * Context: Process context. Takes and releases the xa_lock while
579 * disabling softirqs. May sleep if the @gfp flags permit.
580 * Return: 0 on success, -ENOMEM if memory allocation fails or -ENOSPC if
581 * there is no more space in the XArray.
583 static inline int xa_alloc_bh(struct xarray *xa, u32 *id, u32 max, void *entry,
589 err = __xa_alloc(xa, id, max, entry, gfp);
596 * xa_alloc_irq() - Find somewhere to store this entry in the XArray.
598 * @id: Pointer to ID.
599 * @max: Maximum ID to allocate (inclusive).
601 * @gfp: Memory allocation flags.
603 * Allocates an unused ID in the range specified by @id and @max.
604 * Updates the @id pointer with the index, then stores the entry at that
605 * index. A concurrent lookup will not see an uninitialised @id.
607 * Context: Process context. Takes and releases the xa_lock while
608 * disabling interrupts. May sleep if the @gfp flags permit.
609 * Return: 0 on success, -ENOMEM if memory allocation fails or -ENOSPC if
610 * there is no more space in the XArray.
612 static inline int xa_alloc_irq(struct xarray *xa, u32 *id, u32 max, void *entry,
618 err = __xa_alloc(xa, id, max, entry, gfp);
625 * xa_reserve() - Reserve this index in the XArray.
627 * @index: Index into array.
628 * @gfp: Memory allocation flags.
630 * Ensures there is somewhere to store an entry at @index in the array.
631 * If there is already something stored at @index, this function does
632 * nothing. If there was nothing there, the entry is marked as reserved.
633 * Loading from a reserved entry returns a %NULL pointer.
635 * If you do not use the entry that you have reserved, call xa_release()
636 * or xa_erase() to free any unnecessary memory.
638 * Context: Any context. Takes and releases the xa_lock.
639 * May sleep if the @gfp flags permit.
640 * Return: 0 if the reservation succeeded or -ENOMEM if it failed.
643 int xa_reserve(struct xarray *xa, unsigned long index, gfp_t gfp)
648 ret = __xa_reserve(xa, index, gfp);
655 * xa_reserve_bh() - Reserve this index in the XArray.
657 * @index: Index into array.
658 * @gfp: Memory allocation flags.
660 * A softirq-disabling version of xa_reserve().
662 * Context: Any context. Takes and releases the xa_lock while
663 * disabling softirqs.
664 * Return: 0 if the reservation succeeded or -ENOMEM if it failed.
667 int xa_reserve_bh(struct xarray *xa, unsigned long index, gfp_t gfp)
672 ret = __xa_reserve(xa, index, gfp);
679 * xa_reserve_irq() - Reserve this index in the XArray.
681 * @index: Index into array.
682 * @gfp: Memory allocation flags.
684 * An interrupt-disabling version of xa_reserve().
686 * Context: Process context. Takes and releases the xa_lock while
687 * disabling interrupts.
688 * Return: 0 if the reservation succeeded or -ENOMEM if it failed.
691 int xa_reserve_irq(struct xarray *xa, unsigned long index, gfp_t gfp)
696 ret = __xa_reserve(xa, index, gfp);
702 /* Everything below here is the Advanced API. Proceed with caution. */
705 * The xarray is constructed out of a set of 'chunks' of pointers. Choosing
706 * the best chunk size requires some tradeoffs. A power of two recommends
707 * itself so that we can walk the tree based purely on shifts and masks.
708 * Generally, the larger the better; as the number of slots per level of the
709 * tree increases, the less tall the tree needs to be. But that needs to be
710 * balanced against the memory consumption of each node. On a 64-bit system,
711 * xa_node is currently 576 bytes, and we get 7 of them per 4kB page. If we
712 * doubled the number of slots per node, we'd get only 3 nodes per 4kB page.
714 #ifndef XA_CHUNK_SHIFT
715 #define XA_CHUNK_SHIFT (CONFIG_BASE_SMALL ? 4 : 6)
717 #define XA_CHUNK_SIZE (1UL << XA_CHUNK_SHIFT)
718 #define XA_CHUNK_MASK (XA_CHUNK_SIZE - 1)
719 #define XA_MAX_MARKS 3
720 #define XA_MARK_LONGS DIV_ROUND_UP(XA_CHUNK_SIZE, BITS_PER_LONG)
723 * @count is the count of every non-NULL element in the ->slots array
724 * whether that is a value entry, a retry entry, a user pointer,
725 * a sibling entry or a pointer to the next level of the tree.
726 * @nr_values is the count of every element in ->slots which is
727 * either a value entry or a sibling of a value entry.
730 unsigned char shift; /* Bits remaining in each slot */
731 unsigned char offset; /* Slot offset in parent */
732 unsigned char count; /* Total entry count */
733 unsigned char nr_values; /* Value entry count */
734 struct xa_node __rcu *parent; /* NULL at top of tree */
735 struct xarray *array; /* The array we belong to */
737 struct list_head private_list; /* For tree user */
738 struct rcu_head rcu_head; /* Used when freeing node */
740 void __rcu *slots[XA_CHUNK_SIZE];
742 unsigned long tags[XA_MAX_MARKS][XA_MARK_LONGS];
743 unsigned long marks[XA_MAX_MARKS][XA_MARK_LONGS];
747 void xa_dump(const struct xarray *);
748 void xa_dump_node(const struct xa_node *);
751 #define XA_BUG_ON(xa, x) do { \
757 #define XA_NODE_BUG_ON(node, x) do { \
759 if (node) xa_dump_node(node); \
764 #define XA_BUG_ON(xa, x) do { } while (0)
765 #define XA_NODE_BUG_ON(node, x) do { } while (0)
769 static inline void *xa_head(const struct xarray *xa)
771 return rcu_dereference_check(xa->xa_head,
772 lockdep_is_held(&xa->xa_lock));
776 static inline void *xa_head_locked(const struct xarray *xa)
778 return rcu_dereference_protected(xa->xa_head,
779 lockdep_is_held(&xa->xa_lock));
783 static inline void *xa_entry(const struct xarray *xa,
784 const struct xa_node *node, unsigned int offset)
786 XA_NODE_BUG_ON(node, offset >= XA_CHUNK_SIZE);
787 return rcu_dereference_check(node->slots[offset],
788 lockdep_is_held(&xa->xa_lock));
792 static inline void *xa_entry_locked(const struct xarray *xa,
793 const struct xa_node *node, unsigned int offset)
795 XA_NODE_BUG_ON(node, offset >= XA_CHUNK_SIZE);
796 return rcu_dereference_protected(node->slots[offset],
797 lockdep_is_held(&xa->xa_lock));
801 static inline struct xa_node *xa_parent(const struct xarray *xa,
802 const struct xa_node *node)
804 return rcu_dereference_check(node->parent,
805 lockdep_is_held(&xa->xa_lock));
809 static inline struct xa_node *xa_parent_locked(const struct xarray *xa,
810 const struct xa_node *node)
812 return rcu_dereference_protected(node->parent,
813 lockdep_is_held(&xa->xa_lock));
817 static inline void *xa_mk_node(const struct xa_node *node)
819 return (void *)((unsigned long)node | 2);
823 static inline struct xa_node *xa_to_node(const void *entry)
825 return (struct xa_node *)((unsigned long)entry - 2);
829 static inline bool xa_is_node(const void *entry)
831 return xa_is_internal(entry) && (unsigned long)entry > 4096;
835 static inline void *xa_mk_sibling(unsigned int offset)
837 return xa_mk_internal(offset);
841 static inline unsigned long xa_to_sibling(const void *entry)
843 return xa_to_internal(entry);
847 * xa_is_sibling() - Is the entry a sibling entry?
848 * @entry: Entry retrieved from the XArray
850 * Return: %true if the entry is a sibling entry.
852 static inline bool xa_is_sibling(const void *entry)
854 return IS_ENABLED(CONFIG_XARRAY_MULTI) && xa_is_internal(entry) &&
855 (entry < xa_mk_sibling(XA_CHUNK_SIZE - 1));
858 #define XA_ZERO_ENTRY xa_mk_internal(256)
859 #define XA_RETRY_ENTRY xa_mk_internal(257)
862 * xa_is_zero() - Is the entry a zero entry?
863 * @entry: Entry retrieved from the XArray
865 * Return: %true if the entry is a zero entry.
867 static inline bool xa_is_zero(const void *entry)
869 return unlikely(entry == XA_ZERO_ENTRY);
873 * xa_is_retry() - Is the entry a retry entry?
874 * @entry: Entry retrieved from the XArray
876 * Return: %true if the entry is a retry entry.
878 static inline bool xa_is_retry(const void *entry)
880 return unlikely(entry == XA_RETRY_ENTRY);
884 * typedef xa_update_node_t - A callback function from the XArray.
885 * @node: The node which is being processed
887 * This function is called every time the XArray updates the count of
888 * present and value entries in a node. It allows advanced users to
889 * maintain the private_list in the node.
891 * Context: The xa_lock is held and interrupts may be disabled.
892 * Implementations should not drop the xa_lock, nor re-enable
895 typedef void (*xa_update_node_t)(struct xa_node *node);
898 * The xa_state is opaque to its users. It contains various different pieces
899 * of state involved in the current operation on the XArray. It should be
900 * declared on the stack and passed between the various internal routines.
901 * The various elements in it should not be accessed directly, but only
902 * through the provided accessor functions. The below documentation is for
903 * the benefit of those working on the code, not for users of the XArray.
905 * @xa_node usually points to the xa_node containing the slot we're operating
906 * on (and @xa_offset is the offset in the slots array). If there is a
907 * single entry in the array at index 0, there are no allocated xa_nodes to
908 * point to, and so we store %NULL in @xa_node. @xa_node is set to
909 * the value %XAS_RESTART if the xa_state is not walked to the correct
910 * position in the tree of nodes for this operation. If an error occurs
911 * during an operation, it is set to an %XAS_ERROR value. If we run off the
912 * end of the allocated nodes, it is set to %XAS_BOUNDS.
916 unsigned long xa_index;
917 unsigned char xa_shift;
918 unsigned char xa_sibs;
919 unsigned char xa_offset;
920 unsigned char xa_pad; /* Helps gcc generate better code */
921 struct xa_node *xa_node;
922 struct xa_node *xa_alloc;
923 xa_update_node_t xa_update;
927 * We encode errnos in the xas->xa_node. If an error has happened, we need to
928 * drop the lock to fix it, and once we've done so the xa_state is invalid.
930 #define XA_ERROR(errno) ((struct xa_node *)(((unsigned long)errno << 2) | 2UL))
931 #define XAS_BOUNDS ((struct xa_node *)1UL)
932 #define XAS_RESTART ((struct xa_node *)3UL)
934 #define __XA_STATE(array, index, shift, sibs) { \
941 .xa_node = XAS_RESTART, \
947 * XA_STATE() - Declare an XArray operation state.
948 * @name: Name of this operation state (usually xas).
949 * @array: Array to operate on.
950 * @index: Initial index of interest.
952 * Declare and initialise an xa_state on the stack.
954 #define XA_STATE(name, array, index) \
955 struct xa_state name = __XA_STATE(array, index, 0, 0)
958 * XA_STATE_ORDER() - Declare an XArray operation state.
959 * @name: Name of this operation state (usually xas).
960 * @array: Array to operate on.
961 * @index: Initial index of interest.
962 * @order: Order of entry.
964 * Declare and initialise an xa_state on the stack. This variant of
965 * XA_STATE() allows you to specify the 'order' of the element you
966 * want to operate on.`
968 #define XA_STATE_ORDER(name, array, index, order) \
969 struct xa_state name = __XA_STATE(array, \
970 (index >> order) << order, \
971 order - (order % XA_CHUNK_SHIFT), \
972 (1U << (order % XA_CHUNK_SHIFT)) - 1)
974 #define xas_marked(xas, mark) xa_marked((xas)->xa, (mark))
975 #define xas_trylock(xas) xa_trylock((xas)->xa)
976 #define xas_lock(xas) xa_lock((xas)->xa)
977 #define xas_unlock(xas) xa_unlock((xas)->xa)
978 #define xas_lock_bh(xas) xa_lock_bh((xas)->xa)
979 #define xas_unlock_bh(xas) xa_unlock_bh((xas)->xa)
980 #define xas_lock_irq(xas) xa_lock_irq((xas)->xa)
981 #define xas_unlock_irq(xas) xa_unlock_irq((xas)->xa)
982 #define xas_lock_irqsave(xas, flags) \
983 xa_lock_irqsave((xas)->xa, flags)
984 #define xas_unlock_irqrestore(xas, flags) \
985 xa_unlock_irqrestore((xas)->xa, flags)
988 * xas_error() - Return an errno stored in the xa_state.
989 * @xas: XArray operation state.
991 * Return: 0 if no error has been noted. A negative errno if one has.
993 static inline int xas_error(const struct xa_state *xas)
995 return xa_err(xas->xa_node);
999 * xas_set_err() - Note an error in the xa_state.
1000 * @xas: XArray operation state.
1001 * @err: Negative error number.
1003 * Only call this function with a negative @err; zero or positive errors
1004 * will probably not behave the way you think they should. If you want
1005 * to clear the error from an xa_state, use xas_reset().
1007 static inline void xas_set_err(struct xa_state *xas, long err)
1009 xas->xa_node = XA_ERROR(err);
1013 * xas_invalid() - Is the xas in a retry or error state?
1014 * @xas: XArray operation state.
1016 * Return: %true if the xas cannot be used for operations.
1018 static inline bool xas_invalid(const struct xa_state *xas)
1020 return (unsigned long)xas->xa_node & 3;
1024 * xas_valid() - Is the xas a valid cursor into the array?
1025 * @xas: XArray operation state.
1027 * Return: %true if the xas can be used for operations.
1029 static inline bool xas_valid(const struct xa_state *xas)
1031 return !xas_invalid(xas);
1035 * xas_is_node() - Does the xas point to a node?
1036 * @xas: XArray operation state.
1038 * Return: %true if the xas currently references a node.
1040 static inline bool xas_is_node(const struct xa_state *xas)
1042 return xas_valid(xas) && xas->xa_node;
1045 /* True if the pointer is something other than a node */
1046 static inline bool xas_not_node(struct xa_node *node)
1048 return ((unsigned long)node & 3) || !node;
1051 /* True if the node represents RESTART or an error */
1052 static inline bool xas_frozen(struct xa_node *node)
1054 return (unsigned long)node & 2;
1057 /* True if the node represents head-of-tree, RESTART or BOUNDS */
1058 static inline bool xas_top(struct xa_node *node)
1060 return node <= XAS_RESTART;
1064 * xas_reset() - Reset an XArray operation state.
1065 * @xas: XArray operation state.
1067 * Resets the error or walk state of the @xas so future walks of the
1068 * array will start from the root. Use this if you have dropped the
1069 * xarray lock and want to reuse the xa_state.
1071 * Context: Any context.
1073 static inline void xas_reset(struct xa_state *xas)
1075 xas->xa_node = XAS_RESTART;
1079 * xas_retry() - Retry the operation if appropriate.
1080 * @xas: XArray operation state.
1081 * @entry: Entry from xarray.
1083 * The advanced functions may sometimes return an internal entry, such as
1084 * a retry entry or a zero entry. This function sets up the @xas to restart
1085 * the walk from the head of the array if needed.
1087 * Context: Any context.
1088 * Return: true if the operation needs to be retried.
1090 static inline bool xas_retry(struct xa_state *xas, const void *entry)
1092 if (xa_is_zero(entry))
1094 if (!xa_is_retry(entry))
1100 void *xas_load(struct xa_state *);
1101 void *xas_store(struct xa_state *, void *entry);
1102 void *xas_find(struct xa_state *, unsigned long max);
1103 void *xas_find_conflict(struct xa_state *);
1105 bool xas_get_mark(const struct xa_state *, xa_mark_t);
1106 void xas_set_mark(const struct xa_state *, xa_mark_t);
1107 void xas_clear_mark(const struct xa_state *, xa_mark_t);
1108 void *xas_find_marked(struct xa_state *, unsigned long max, xa_mark_t);
1109 void xas_init_marks(const struct xa_state *);
1111 bool xas_nomem(struct xa_state *, gfp_t);
1112 void xas_pause(struct xa_state *);
1114 void xas_create_range(struct xa_state *);
1117 * xas_reload() - Refetch an entry from the xarray.
1118 * @xas: XArray operation state.
1120 * Use this function to check that a previously loaded entry still has
1121 * the same value. This is useful for the lockless pagecache lookup where
1122 * we walk the array with only the RCU lock to protect us, lock the page,
1123 * then check that the page hasn't moved since we looked it up.
1125 * The caller guarantees that @xas is still valid. If it may be in an
1126 * error or restart state, call xas_load() instead.
1128 * Return: The entry at this location in the xarray.
1130 static inline void *xas_reload(struct xa_state *xas)
1132 struct xa_node *node = xas->xa_node;
1135 return xa_entry(xas->xa, node, xas->xa_offset);
1136 return xa_head(xas->xa);
1140 * xas_set() - Set up XArray operation state for a different index.
1141 * @xas: XArray operation state.
1142 * @index: New index into the XArray.
1144 * Move the operation state to refer to a different index. This will
1145 * have the effect of starting a walk from the top; see xas_next()
1146 * to move to an adjacent index.
1148 static inline void xas_set(struct xa_state *xas, unsigned long index)
1150 xas->xa_index = index;
1151 xas->xa_node = XAS_RESTART;
1155 * xas_set_order() - Set up XArray operation state for a multislot entry.
1156 * @xas: XArray operation state.
1157 * @index: Target of the operation.
1158 * @order: Entry occupies 2^@order indices.
1160 static inline void xas_set_order(struct xa_state *xas, unsigned long index,
1163 #ifdef CONFIG_XARRAY_MULTI
1164 xas->xa_index = order < BITS_PER_LONG ? (index >> order) << order : 0;
1165 xas->xa_shift = order - (order % XA_CHUNK_SHIFT);
1166 xas->xa_sibs = (1 << (order % XA_CHUNK_SHIFT)) - 1;
1167 xas->xa_node = XAS_RESTART;
1170 xas_set(xas, index);
1175 * xas_set_update() - Set up XArray operation state for a callback.
1176 * @xas: XArray operation state.
1177 * @update: Function to call when updating a node.
1179 * The XArray can notify a caller after it has updated an xa_node.
1180 * This is advanced functionality and is only needed by the page cache.
1182 static inline void xas_set_update(struct xa_state *xas, xa_update_node_t update)
1184 xas->xa_update = update;
1188 * xas_next_entry() - Advance iterator to next present entry.
1189 * @xas: XArray operation state.
1190 * @max: Highest index to return.
1192 * xas_next_entry() is an inline function to optimise xarray traversal for
1193 * speed. It is equivalent to calling xas_find(), and will call xas_find()
1194 * for all the hard cases.
1196 * Return: The next present entry after the one currently referred to by @xas.
1198 static inline void *xas_next_entry(struct xa_state *xas, unsigned long max)
1200 struct xa_node *node = xas->xa_node;
1203 if (unlikely(xas_not_node(node) || node->shift ||
1204 xas->xa_offset != (xas->xa_index & XA_CHUNK_MASK)))
1205 return xas_find(xas, max);
1208 if (unlikely(xas->xa_index >= max))
1209 return xas_find(xas, max);
1210 if (unlikely(xas->xa_offset == XA_CHUNK_MASK))
1211 return xas_find(xas, max);
1212 entry = xa_entry(xas->xa, node, xas->xa_offset + 1);
1213 if (unlikely(xa_is_internal(entry)))
1214 return xas_find(xas, max);
1223 static inline unsigned int xas_find_chunk(struct xa_state *xas, bool advance,
1226 unsigned long *addr = xas->xa_node->marks[(__force unsigned)mark];
1227 unsigned int offset = xas->xa_offset;
1231 if (XA_CHUNK_SIZE == BITS_PER_LONG) {
1232 if (offset < XA_CHUNK_SIZE) {
1233 unsigned long data = *addr & (~0UL << offset);
1237 return XA_CHUNK_SIZE;
1240 return find_next_bit(addr, XA_CHUNK_SIZE, offset);
1244 * xas_next_marked() - Advance iterator to next marked entry.
1245 * @xas: XArray operation state.
1246 * @max: Highest index to return.
1247 * @mark: Mark to search for.
1249 * xas_next_marked() is an inline function to optimise xarray traversal for
1250 * speed. It is equivalent to calling xas_find_marked(), and will call
1251 * xas_find_marked() for all the hard cases.
1253 * Return: The next marked entry after the one currently referred to by @xas.
1255 static inline void *xas_next_marked(struct xa_state *xas, unsigned long max,
1258 struct xa_node *node = xas->xa_node;
1259 unsigned int offset;
1261 if (unlikely(xas_not_node(node) || node->shift))
1262 return xas_find_marked(xas, max, mark);
1263 offset = xas_find_chunk(xas, true, mark);
1264 xas->xa_offset = offset;
1265 xas->xa_index = (xas->xa_index & ~XA_CHUNK_MASK) + offset;
1266 if (xas->xa_index > max)
1268 if (offset == XA_CHUNK_SIZE)
1269 return xas_find_marked(xas, max, mark);
1270 return xa_entry(xas->xa, node, offset);
1274 * If iterating while holding a lock, drop the lock and reschedule
1275 * every %XA_CHECK_SCHED loops.
1278 XA_CHECK_SCHED = 4096,
1282 * xas_for_each() - Iterate over a range of an XArray.
1283 * @xas: XArray operation state.
1284 * @entry: Entry retrieved from the array.
1285 * @max: Maximum index to retrieve from array.
1287 * The loop body will be executed for each entry present in the xarray
1288 * between the current xas position and @max. @entry will be set to
1289 * the entry retrieved from the xarray. It is safe to delete entries
1290 * from the array in the loop body. You should hold either the RCU lock
1291 * or the xa_lock while iterating. If you need to drop the lock, call
1292 * xas_pause() first.
1294 #define xas_for_each(xas, entry, max) \
1295 for (entry = xas_find(xas, max); entry; \
1296 entry = xas_next_entry(xas, max))
1299 * xas_for_each_marked() - Iterate over a range of an XArray.
1300 * @xas: XArray operation state.
1301 * @entry: Entry retrieved from the array.
1302 * @max: Maximum index to retrieve from array.
1303 * @mark: Mark to search for.
1305 * The loop body will be executed for each marked entry in the xarray
1306 * between the current xas position and @max. @entry will be set to
1307 * the entry retrieved from the xarray. It is safe to delete entries
1308 * from the array in the loop body. You should hold either the RCU lock
1309 * or the xa_lock while iterating. If you need to drop the lock, call
1310 * xas_pause() first.
1312 #define xas_for_each_marked(xas, entry, max, mark) \
1313 for (entry = xas_find_marked(xas, max, mark); entry; \
1314 entry = xas_next_marked(xas, max, mark))
1317 * xas_for_each_conflict() - Iterate over a range of an XArray.
1318 * @xas: XArray operation state.
1319 * @entry: Entry retrieved from the array.
1321 * The loop body will be executed for each entry in the XArray that lies
1322 * within the range specified by @xas. If the loop completes successfully,
1323 * any entries that lie in this range will be replaced by @entry. The caller
1324 * may break out of the loop; if they do so, the contents of the XArray will
1325 * be unchanged. The operation may fail due to an out of memory condition.
1326 * The caller may also call xa_set_err() to exit the loop while setting an
1327 * error to record the reason.
1329 #define xas_for_each_conflict(xas, entry) \
1330 while ((entry = xas_find_conflict(xas)))
1332 void *__xas_next(struct xa_state *);
1333 void *__xas_prev(struct xa_state *);
1336 * xas_prev() - Move iterator to previous index.
1337 * @xas: XArray operation state.
1339 * If the @xas was in an error state, it will remain in an error state
1340 * and this function will return %NULL. If the @xas has never been walked,
1341 * it will have the effect of calling xas_load(). Otherwise one will be
1342 * subtracted from the index and the state will be walked to the correct
1343 * location in the array for the next operation.
1345 * If the iterator was referencing index 0, this function wraps
1346 * around to %ULONG_MAX.
1348 * Return: The entry at the new index. This may be %NULL or an internal
1351 static inline void *xas_prev(struct xa_state *xas)
1353 struct xa_node *node = xas->xa_node;
1355 if (unlikely(xas_not_node(node) || node->shift ||
1356 xas->xa_offset == 0))
1357 return __xas_prev(xas);
1361 return xa_entry(xas->xa, node, xas->xa_offset);
1365 * xas_next() - Move state to next index.
1366 * @xas: XArray operation state.
1368 * If the @xas was in an error state, it will remain in an error state
1369 * and this function will return %NULL. If the @xas has never been walked,
1370 * it will have the effect of calling xas_load(). Otherwise one will be
1371 * added to the index and the state will be walked to the correct
1372 * location in the array for the next operation.
1374 * If the iterator was referencing index %ULONG_MAX, this function wraps
1377 * Return: The entry at the new index. This may be %NULL or an internal
1380 static inline void *xas_next(struct xa_state *xas)
1382 struct xa_node *node = xas->xa_node;
1384 if (unlikely(xas_not_node(node) || node->shift ||
1385 xas->xa_offset == XA_CHUNK_MASK))
1386 return __xas_next(xas);
1390 return xa_entry(xas->xa, node, xas->xa_offset);
1393 #endif /* _LINUX_XARRAY_H */