1 /* SPDX-License-Identifier: GPL-2.0+ */
2 #ifndef _LINUX_XARRAY_H
3 #define _LINUX_XARRAY_H
6 * Copyright (c) 2017 Microsoft Corporation
7 * Author: Matthew Wilcox <willy@infradead.org>
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_erase(struct xarray
*, unsigned long index
);
293 void *xa_store_range(struct xarray
*, unsigned long first
, unsigned long last
,
295 bool xa_get_mark(struct xarray
*, unsigned long index
, xa_mark_t
);
296 void xa_set_mark(struct xarray
*, unsigned long index
, xa_mark_t
);
297 void xa_clear_mark(struct xarray
*, unsigned long index
, xa_mark_t
);
298 void *xa_find(struct xarray
*xa
, unsigned long *index
,
299 unsigned long max
, xa_mark_t
) __attribute__((nonnull(2)));
300 void *xa_find_after(struct xarray
*xa
, unsigned long *index
,
301 unsigned long max
, xa_mark_t
) __attribute__((nonnull(2)));
302 unsigned int xa_extract(struct xarray
*, void **dst
, unsigned long start
,
303 unsigned long max
, unsigned int n
, xa_mark_t
);
304 void xa_destroy(struct xarray
*);
307 * xa_init() - Initialise an empty XArray.
310 * An empty XArray is full of NULL entries.
312 * Context: Any context.
314 static inline void xa_init(struct xarray
*xa
)
316 xa_init_flags(xa
, 0);
320 * xa_empty() - Determine if an array has any present entries.
323 * Context: Any context.
324 * Return: %true if the array contains only NULL pointers.
326 static inline bool xa_empty(const struct xarray
*xa
)
328 return xa
->xa_head
== NULL
;
332 * xa_marked() - Inquire whether any entry in this array has a mark set
336 * Context: Any context.
337 * Return: %true if any entry has this mark set.
339 static inline bool xa_marked(const struct xarray
*xa
, xa_mark_t mark
)
341 return xa
->xa_flags
& XA_FLAGS_MARK(mark
);
345 * xa_for_each() - Iterate over a portion of an XArray.
347 * @entry: Entry retrieved from array.
348 * @index: Index of @entry.
349 * @max: Maximum index to retrieve from array.
350 * @filter: Selection criterion.
352 * Initialise @index to the lowest index you want to retrieve from the
353 * array. During the iteration, @entry will have the value of the entry
354 * stored in @xa at @index. The iteration will skip all entries in the
355 * array which do not match @filter. You may modify @index during the
356 * iteration if you want to skip or reprocess indices. It is safe to modify
357 * the array during the iteration. At the end of the iteration, @entry will
358 * be set to NULL and @index will have a value less than or equal to max.
360 * xa_for_each() is O(n.log(n)) while xas_for_each() is O(n). You have
361 * to handle your own locking with xas_for_each(), and if you have to unlock
362 * after each iteration, it will also end up being O(n.log(n)). xa_for_each()
363 * will spin if it hits a retry entry; if you intend to see retry entries,
364 * you should use the xas_for_each() iterator instead. The xas_for_each()
365 * iterator will expand into more inline code than xa_for_each().
367 * Context: Any context. Takes and releases the RCU lock.
369 #define xa_for_each(xa, entry, index, max, filter) \
370 for (entry = xa_find(xa, &index, max, filter); entry; \
371 entry = xa_find_after(xa, &index, max, filter))
373 #define xa_trylock(xa) spin_trylock(&(xa)->xa_lock)
374 #define xa_lock(xa) spin_lock(&(xa)->xa_lock)
375 #define xa_unlock(xa) spin_unlock(&(xa)->xa_lock)
376 #define xa_lock_bh(xa) spin_lock_bh(&(xa)->xa_lock)
377 #define xa_unlock_bh(xa) spin_unlock_bh(&(xa)->xa_lock)
378 #define xa_lock_irq(xa) spin_lock_irq(&(xa)->xa_lock)
379 #define xa_unlock_irq(xa) spin_unlock_irq(&(xa)->xa_lock)
380 #define xa_lock_irqsave(xa, flags) \
381 spin_lock_irqsave(&(xa)->xa_lock, flags)
382 #define xa_unlock_irqrestore(xa, flags) \
383 spin_unlock_irqrestore(&(xa)->xa_lock, flags)
386 * Versions of the normal API which require the caller to hold the
387 * xa_lock. If the GFP flags allow it, they will drop the lock to
388 * allocate memory, then reacquire it afterwards. These functions
389 * may also re-enable interrupts if the XArray flags indicate the
390 * locking should be interrupt safe.
392 void *__xa_erase(struct xarray
*, unsigned long index
);
393 void *__xa_store(struct xarray
*, unsigned long index
, void *entry
, gfp_t
);
394 void *__xa_cmpxchg(struct xarray
*, unsigned long index
, void *old
,
396 int __xa_alloc(struct xarray
*, u32
*id
, u32 max
, void *entry
, gfp_t
);
397 int __xa_reserve(struct xarray
*, unsigned long index
, gfp_t
);
398 void __xa_set_mark(struct xarray
*, unsigned long index
, xa_mark_t
);
399 void __xa_clear_mark(struct xarray
*, unsigned long index
, xa_mark_t
);
402 * __xa_insert() - Store this entry in the XArray unless another entry is
405 * @index: Index into array.
407 * @gfp: Memory allocation flags.
409 * If you would rather see the existing entry in the array, use __xa_cmpxchg().
410 * This function is for users who don't care what the entry is, only that
413 * Context: Any context. Expects xa_lock to be held on entry. May
414 * release and reacquire xa_lock if the @gfp flags permit.
415 * Return: 0 if the store succeeded. -EEXIST if another entry was present.
416 * -ENOMEM if memory could not be allocated.
418 static inline int __xa_insert(struct xarray
*xa
, unsigned long index
,
419 void *entry
, gfp_t gfp
)
421 void *curr
= __xa_cmpxchg(xa
, index
, NULL
, entry
, gfp
);
430 * xa_store_bh() - Store this entry in the XArray.
432 * @index: Index into array.
434 * @gfp: Memory allocation flags.
436 * This function is like calling xa_store() except it disables softirqs
437 * while holding the array lock.
439 * Context: Any context. Takes and releases the xa_lock while
440 * disabling softirqs.
441 * Return: The entry which used to be at this index.
443 static inline void *xa_store_bh(struct xarray
*xa
, unsigned long index
,
444 void *entry
, gfp_t gfp
)
449 curr
= __xa_store(xa
, index
, entry
, gfp
);
456 * xa_store_irq() - Erase this entry from the XArray.
458 * @index: Index into array.
460 * @gfp: Memory allocation flags.
462 * This function is like calling xa_store() except it disables interrupts
463 * while holding the array lock.
465 * Context: Process context. Takes and releases the xa_lock while
466 * disabling interrupts.
467 * Return: The entry which used to be at this index.
469 static inline void *xa_store_irq(struct xarray
*xa
, unsigned long index
,
470 void *entry
, gfp_t gfp
)
475 curr
= __xa_store(xa
, index
, entry
, gfp
);
482 * xa_erase_bh() - Erase this entry from the XArray.
484 * @index: Index of entry.
486 * This function is the equivalent of calling xa_store() with %NULL as
487 * the third argument. The XArray does not need to allocate memory, so
488 * the user does not need to provide GFP flags.
490 * Context: Any context. Takes and releases the xa_lock while
491 * disabling softirqs.
492 * Return: The entry which used to be at this index.
494 static inline void *xa_erase_bh(struct xarray
*xa
, unsigned long index
)
499 entry
= __xa_erase(xa
, index
);
506 * xa_erase_irq() - Erase this entry from the XArray.
508 * @index: Index of entry.
510 * This function is the equivalent of calling xa_store() with %NULL as
511 * the third argument. The XArray does not need to allocate memory, so
512 * the user does not need to provide GFP flags.
514 * Context: Process context. Takes and releases the xa_lock while
515 * disabling interrupts.
516 * Return: The entry which used to be at this index.
518 static inline void *xa_erase_irq(struct xarray
*xa
, unsigned long index
)
523 entry
= __xa_erase(xa
, index
);
530 * xa_cmpxchg() - Conditionally replace an entry in the XArray.
532 * @index: Index into array.
533 * @old: Old value to test against.
534 * @entry: New value to place in array.
535 * @gfp: Memory allocation flags.
537 * If the entry at @index is the same as @old, replace it with @entry.
538 * If the return value is equal to @old, then the exchange was successful.
540 * Context: Any context. Takes and releases the xa_lock. May sleep
541 * if the @gfp flags permit.
542 * Return: The old value at this index or xa_err() if an error happened.
544 static inline void *xa_cmpxchg(struct xarray
*xa
, unsigned long index
,
545 void *old
, void *entry
, gfp_t gfp
)
550 curr
= __xa_cmpxchg(xa
, index
, old
, entry
, gfp
);
557 * xa_insert() - Store this entry in the XArray unless another entry is
560 * @index: Index into array.
562 * @gfp: Memory allocation flags.
564 * If you would rather see the existing entry in the array, use xa_cmpxchg().
565 * This function is for users who don't care what the entry is, only that
568 * Context: Process context. Takes and releases the xa_lock.
569 * May sleep if the @gfp flags permit.
570 * Return: 0 if the store succeeded. -EEXIST if another entry was present.
571 * -ENOMEM if memory could not be allocated.
573 static inline int xa_insert(struct xarray
*xa
, unsigned long index
,
574 void *entry
, gfp_t gfp
)
576 void *curr
= xa_cmpxchg(xa
, index
, NULL
, entry
, gfp
);
585 * xa_alloc() - Find somewhere to store this entry in the XArray.
587 * @id: Pointer to ID.
588 * @max: Maximum ID to allocate (inclusive).
590 * @gfp: Memory allocation flags.
592 * Allocates an unused ID in the range specified by @id and @max.
593 * Updates the @id pointer with the index, then stores the entry at that
594 * index. A concurrent lookup will not see an uninitialised @id.
596 * Context: Process context. Takes and releases the xa_lock. May sleep if
597 * the @gfp flags permit.
598 * Return: 0 on success, -ENOMEM if memory allocation fails or -ENOSPC if
599 * there is no more space in the XArray.
601 static inline int xa_alloc(struct xarray
*xa
, u32
*id
, u32 max
, void *entry
,
607 err
= __xa_alloc(xa
, id
, max
, entry
, gfp
);
614 * xa_alloc_bh() - Find somewhere to store this entry in the XArray.
616 * @id: Pointer to ID.
617 * @max: Maximum ID to allocate (inclusive).
619 * @gfp: Memory allocation flags.
621 * Allocates an unused ID in the range specified by @id and @max.
622 * Updates the @id pointer with the index, then stores the entry at that
623 * index. A concurrent lookup will not see an uninitialised @id.
625 * Context: Any context. Takes and releases the xa_lock while
626 * disabling softirqs. May sleep if the @gfp flags permit.
627 * Return: 0 on success, -ENOMEM if memory allocation fails or -ENOSPC if
628 * there is no more space in the XArray.
630 static inline int xa_alloc_bh(struct xarray
*xa
, u32
*id
, u32 max
, void *entry
,
636 err
= __xa_alloc(xa
, id
, max
, entry
, gfp
);
643 * xa_alloc_irq() - Find somewhere to store this entry in the XArray.
645 * @id: Pointer to ID.
646 * @max: Maximum ID to allocate (inclusive).
648 * @gfp: Memory allocation flags.
650 * Allocates an unused ID in the range specified by @id and @max.
651 * Updates the @id pointer with the index, then stores the entry at that
652 * index. A concurrent lookup will not see an uninitialised @id.
654 * Context: Process context. Takes and releases the xa_lock while
655 * disabling interrupts. May sleep if the @gfp flags permit.
656 * Return: 0 on success, -ENOMEM if memory allocation fails or -ENOSPC if
657 * there is no more space in the XArray.
659 static inline int xa_alloc_irq(struct xarray
*xa
, u32
*id
, u32 max
, void *entry
,
665 err
= __xa_alloc(xa
, id
, max
, entry
, gfp
);
672 * xa_reserve() - Reserve this index in the XArray.
674 * @index: Index into array.
675 * @gfp: Memory allocation flags.
677 * Ensures there is somewhere to store an entry at @index in the array.
678 * If there is already something stored at @index, this function does
679 * nothing. If there was nothing there, the entry is marked as reserved.
680 * Loading from a reserved entry returns a %NULL pointer.
682 * If you do not use the entry that you have reserved, call xa_release()
683 * or xa_erase() to free any unnecessary memory.
685 * Context: Any context. Takes and releases the xa_lock.
686 * May sleep if the @gfp flags permit.
687 * Return: 0 if the reservation succeeded or -ENOMEM if it failed.
690 int xa_reserve(struct xarray
*xa
, unsigned long index
, gfp_t gfp
)
695 ret
= __xa_reserve(xa
, index
, gfp
);
702 * xa_reserve_bh() - Reserve this index in the XArray.
704 * @index: Index into array.
705 * @gfp: Memory allocation flags.
707 * A softirq-disabling version of xa_reserve().
709 * Context: Any context. Takes and releases the xa_lock while
710 * disabling softirqs.
711 * Return: 0 if the reservation succeeded or -ENOMEM if it failed.
714 int xa_reserve_bh(struct xarray
*xa
, unsigned long index
, gfp_t gfp
)
719 ret
= __xa_reserve(xa
, index
, gfp
);
726 * xa_reserve_irq() - Reserve this index in the XArray.
728 * @index: Index into array.
729 * @gfp: Memory allocation flags.
731 * An interrupt-disabling version of xa_reserve().
733 * Context: Process context. Takes and releases the xa_lock while
734 * disabling interrupts.
735 * Return: 0 if the reservation succeeded or -ENOMEM if it failed.
738 int xa_reserve_irq(struct xarray
*xa
, unsigned long index
, gfp_t gfp
)
743 ret
= __xa_reserve(xa
, index
, gfp
);
750 * xa_release() - Release a reserved entry.
752 * @index: Index of entry.
754 * After calling xa_reserve(), you can call this function to release the
755 * reservation. If the entry at @index has been stored to, this function
758 static inline void xa_release(struct xarray
*xa
, unsigned long index
)
760 xa_cmpxchg(xa
, index
, NULL
, NULL
, 0);
763 /* Everything below here is the Advanced API. Proceed with caution. */
766 * The xarray is constructed out of a set of 'chunks' of pointers. Choosing
767 * the best chunk size requires some tradeoffs. A power of two recommends
768 * itself so that we can walk the tree based purely on shifts and masks.
769 * Generally, the larger the better; as the number of slots per level of the
770 * tree increases, the less tall the tree needs to be. But that needs to be
771 * balanced against the memory consumption of each node. On a 64-bit system,
772 * xa_node is currently 576 bytes, and we get 7 of them per 4kB page. If we
773 * doubled the number of slots per node, we'd get only 3 nodes per 4kB page.
775 #ifndef XA_CHUNK_SHIFT
776 #define XA_CHUNK_SHIFT (CONFIG_BASE_SMALL ? 4 : 6)
778 #define XA_CHUNK_SIZE (1UL << XA_CHUNK_SHIFT)
779 #define XA_CHUNK_MASK (XA_CHUNK_SIZE - 1)
780 #define XA_MAX_MARKS 3
781 #define XA_MARK_LONGS DIV_ROUND_UP(XA_CHUNK_SIZE, BITS_PER_LONG)
784 * @count is the count of every non-NULL element in the ->slots array
785 * whether that is a value entry, a retry entry, a user pointer,
786 * a sibling entry or a pointer to the next level of the tree.
787 * @nr_values is the count of every element in ->slots which is
788 * either a value entry or a sibling of a value entry.
791 unsigned char shift
; /* Bits remaining in each slot */
792 unsigned char offset
; /* Slot offset in parent */
793 unsigned char count
; /* Total entry count */
794 unsigned char nr_values
; /* Value entry count */
795 struct xa_node __rcu
*parent
; /* NULL at top of tree */
796 struct xarray
*array
; /* The array we belong to */
798 struct list_head private_list
; /* For tree user */
799 struct rcu_head rcu_head
; /* Used when freeing node */
801 void __rcu
*slots
[XA_CHUNK_SIZE
];
803 unsigned long tags
[XA_MAX_MARKS
][XA_MARK_LONGS
];
804 unsigned long marks
[XA_MAX_MARKS
][XA_MARK_LONGS
];
808 void xa_dump(const struct xarray
*);
809 void xa_dump_node(const struct xa_node
*);
812 #define XA_BUG_ON(xa, x) do { \
818 #define XA_NODE_BUG_ON(node, x) do { \
820 if (node) xa_dump_node(node); \
825 #define XA_BUG_ON(xa, x) do { } while (0)
826 #define XA_NODE_BUG_ON(node, x) do { } while (0)
830 static inline void *xa_head(const struct xarray
*xa
)
832 return rcu_dereference_check(xa
->xa_head
,
833 lockdep_is_held(&xa
->xa_lock
));
837 static inline void *xa_head_locked(const struct xarray
*xa
)
839 return rcu_dereference_protected(xa
->xa_head
,
840 lockdep_is_held(&xa
->xa_lock
));
844 static inline void *xa_entry(const struct xarray
*xa
,
845 const struct xa_node
*node
, unsigned int offset
)
847 XA_NODE_BUG_ON(node
, offset
>= XA_CHUNK_SIZE
);
848 return rcu_dereference_check(node
->slots
[offset
],
849 lockdep_is_held(&xa
->xa_lock
));
853 static inline void *xa_entry_locked(const struct xarray
*xa
,
854 const struct xa_node
*node
, unsigned int offset
)
856 XA_NODE_BUG_ON(node
, offset
>= XA_CHUNK_SIZE
);
857 return rcu_dereference_protected(node
->slots
[offset
],
858 lockdep_is_held(&xa
->xa_lock
));
862 static inline struct xa_node
*xa_parent(const struct xarray
*xa
,
863 const struct xa_node
*node
)
865 return rcu_dereference_check(node
->parent
,
866 lockdep_is_held(&xa
->xa_lock
));
870 static inline struct xa_node
*xa_parent_locked(const struct xarray
*xa
,
871 const struct xa_node
*node
)
873 return rcu_dereference_protected(node
->parent
,
874 lockdep_is_held(&xa
->xa_lock
));
878 static inline void *xa_mk_node(const struct xa_node
*node
)
880 return (void *)((unsigned long)node
| 2);
884 static inline struct xa_node
*xa_to_node(const void *entry
)
886 return (struct xa_node
*)((unsigned long)entry
- 2);
890 static inline bool xa_is_node(const void *entry
)
892 return xa_is_internal(entry
) && (unsigned long)entry
> 4096;
896 static inline void *xa_mk_sibling(unsigned int offset
)
898 return xa_mk_internal(offset
);
902 static inline unsigned long xa_to_sibling(const void *entry
)
904 return xa_to_internal(entry
);
908 * xa_is_sibling() - Is the entry a sibling entry?
909 * @entry: Entry retrieved from the XArray
911 * Return: %true if the entry is a sibling entry.
913 static inline bool xa_is_sibling(const void *entry
)
915 return IS_ENABLED(CONFIG_XARRAY_MULTI
) && xa_is_internal(entry
) &&
916 (entry
< xa_mk_sibling(XA_CHUNK_SIZE
- 1));
919 #define XA_ZERO_ENTRY xa_mk_internal(256)
920 #define XA_RETRY_ENTRY xa_mk_internal(257)
923 * xa_is_zero() - Is the entry a zero entry?
924 * @entry: Entry retrieved from the XArray
926 * Return: %true if the entry is a zero entry.
928 static inline bool xa_is_zero(const void *entry
)
930 return unlikely(entry
== XA_ZERO_ENTRY
);
934 * xa_is_retry() - Is the entry a retry entry?
935 * @entry: Entry retrieved from the XArray
937 * Return: %true if the entry is a retry entry.
939 static inline bool xa_is_retry(const void *entry
)
941 return unlikely(entry
== XA_RETRY_ENTRY
);
945 * typedef xa_update_node_t - A callback function from the XArray.
946 * @node: The node which is being processed
948 * This function is called every time the XArray updates the count of
949 * present and value entries in a node. It allows advanced users to
950 * maintain the private_list in the node.
952 * Context: The xa_lock is held and interrupts may be disabled.
953 * Implementations should not drop the xa_lock, nor re-enable
956 typedef void (*xa_update_node_t
)(struct xa_node
*node
);
959 * The xa_state is opaque to its users. It contains various different pieces
960 * of state involved in the current operation on the XArray. It should be
961 * declared on the stack and passed between the various internal routines.
962 * The various elements in it should not be accessed directly, but only
963 * through the provided accessor functions. The below documentation is for
964 * the benefit of those working on the code, not for users of the XArray.
966 * @xa_node usually points to the xa_node containing the slot we're operating
967 * on (and @xa_offset is the offset in the slots array). If there is a
968 * single entry in the array at index 0, there are no allocated xa_nodes to
969 * point to, and so we store %NULL in @xa_node. @xa_node is set to
970 * the value %XAS_RESTART if the xa_state is not walked to the correct
971 * position in the tree of nodes for this operation. If an error occurs
972 * during an operation, it is set to an %XAS_ERROR value. If we run off the
973 * end of the allocated nodes, it is set to %XAS_BOUNDS.
977 unsigned long xa_index
;
978 unsigned char xa_shift
;
979 unsigned char xa_sibs
;
980 unsigned char xa_offset
;
981 unsigned char xa_pad
; /* Helps gcc generate better code */
982 struct xa_node
*xa_node
;
983 struct xa_node
*xa_alloc
;
984 xa_update_node_t xa_update
;
988 * We encode errnos in the xas->xa_node. If an error has happened, we need to
989 * drop the lock to fix it, and once we've done so the xa_state is invalid.
991 #define XA_ERROR(errno) ((struct xa_node *)(((unsigned long)errno << 2) | 2UL))
992 #define XAS_BOUNDS ((struct xa_node *)1UL)
993 #define XAS_RESTART ((struct xa_node *)3UL)
995 #define __XA_STATE(array, index, shift, sibs) { \
1002 .xa_node = XAS_RESTART, \
1008 * XA_STATE() - Declare an XArray operation state.
1009 * @name: Name of this operation state (usually xas).
1010 * @array: Array to operate on.
1011 * @index: Initial index of interest.
1013 * Declare and initialise an xa_state on the stack.
1015 #define XA_STATE(name, array, index) \
1016 struct xa_state name = __XA_STATE(array, index, 0, 0)
1019 * XA_STATE_ORDER() - Declare an XArray operation state.
1020 * @name: Name of this operation state (usually xas).
1021 * @array: Array to operate on.
1022 * @index: Initial index of interest.
1023 * @order: Order of entry.
1025 * Declare and initialise an xa_state on the stack. This variant of
1026 * XA_STATE() allows you to specify the 'order' of the element you
1027 * want to operate on.`
1029 #define XA_STATE_ORDER(name, array, index, order) \
1030 struct xa_state name = __XA_STATE(array, \
1031 (index >> order) << order, \
1032 order - (order % XA_CHUNK_SHIFT), \
1033 (1U << (order % XA_CHUNK_SHIFT)) - 1)
1035 #define xas_marked(xas, mark) xa_marked((xas)->xa, (mark))
1036 #define xas_trylock(xas) xa_trylock((xas)->xa)
1037 #define xas_lock(xas) xa_lock((xas)->xa)
1038 #define xas_unlock(xas) xa_unlock((xas)->xa)
1039 #define xas_lock_bh(xas) xa_lock_bh((xas)->xa)
1040 #define xas_unlock_bh(xas) xa_unlock_bh((xas)->xa)
1041 #define xas_lock_irq(xas) xa_lock_irq((xas)->xa)
1042 #define xas_unlock_irq(xas) xa_unlock_irq((xas)->xa)
1043 #define xas_lock_irqsave(xas, flags) \
1044 xa_lock_irqsave((xas)->xa, flags)
1045 #define xas_unlock_irqrestore(xas, flags) \
1046 xa_unlock_irqrestore((xas)->xa, flags)
1049 * xas_error() - Return an errno stored in the xa_state.
1050 * @xas: XArray operation state.
1052 * Return: 0 if no error has been noted. A negative errno if one has.
1054 static inline int xas_error(const struct xa_state
*xas
)
1056 return xa_err(xas
->xa_node
);
1060 * xas_set_err() - Note an error in the xa_state.
1061 * @xas: XArray operation state.
1062 * @err: Negative error number.
1064 * Only call this function with a negative @err; zero or positive errors
1065 * will probably not behave the way you think they should. If you want
1066 * to clear the error from an xa_state, use xas_reset().
1068 static inline void xas_set_err(struct xa_state
*xas
, long err
)
1070 xas
->xa_node
= XA_ERROR(err
);
1074 * xas_invalid() - Is the xas in a retry or error state?
1075 * @xas: XArray operation state.
1077 * Return: %true if the xas cannot be used for operations.
1079 static inline bool xas_invalid(const struct xa_state
*xas
)
1081 return (unsigned long)xas
->xa_node
& 3;
1085 * xas_valid() - Is the xas a valid cursor into the array?
1086 * @xas: XArray operation state.
1088 * Return: %true if the xas can be used for operations.
1090 static inline bool xas_valid(const struct xa_state
*xas
)
1092 return !xas_invalid(xas
);
1096 * xas_is_node() - Does the xas point to a node?
1097 * @xas: XArray operation state.
1099 * Return: %true if the xas currently references a node.
1101 static inline bool xas_is_node(const struct xa_state
*xas
)
1103 return xas_valid(xas
) && xas
->xa_node
;
1106 /* True if the pointer is something other than a node */
1107 static inline bool xas_not_node(struct xa_node
*node
)
1109 return ((unsigned long)node
& 3) || !node
;
1112 /* True if the node represents RESTART or an error */
1113 static inline bool xas_frozen(struct xa_node
*node
)
1115 return (unsigned long)node
& 2;
1118 /* True if the node represents head-of-tree, RESTART or BOUNDS */
1119 static inline bool xas_top(struct xa_node
*node
)
1121 return node
<= XAS_RESTART
;
1125 * xas_reset() - Reset an XArray operation state.
1126 * @xas: XArray operation state.
1128 * Resets the error or walk state of the @xas so future walks of the
1129 * array will start from the root. Use this if you have dropped the
1130 * xarray lock and want to reuse the xa_state.
1132 * Context: Any context.
1134 static inline void xas_reset(struct xa_state
*xas
)
1136 xas
->xa_node
= XAS_RESTART
;
1140 * xas_retry() - Retry the operation if appropriate.
1141 * @xas: XArray operation state.
1142 * @entry: Entry from xarray.
1144 * The advanced functions may sometimes return an internal entry, such as
1145 * a retry entry or a zero entry. This function sets up the @xas to restart
1146 * the walk from the head of the array if needed.
1148 * Context: Any context.
1149 * Return: true if the operation needs to be retried.
1151 static inline bool xas_retry(struct xa_state
*xas
, const void *entry
)
1153 if (xa_is_zero(entry
))
1155 if (!xa_is_retry(entry
))
1161 void *xas_load(struct xa_state
*);
1162 void *xas_store(struct xa_state
*, void *entry
);
1163 void *xas_find(struct xa_state
*, unsigned long max
);
1164 void *xas_find_conflict(struct xa_state
*);
1166 bool xas_get_mark(const struct xa_state
*, xa_mark_t
);
1167 void xas_set_mark(const struct xa_state
*, xa_mark_t
);
1168 void xas_clear_mark(const struct xa_state
*, xa_mark_t
);
1169 void *xas_find_marked(struct xa_state
*, unsigned long max
, xa_mark_t
);
1170 void xas_init_marks(const struct xa_state
*);
1172 bool xas_nomem(struct xa_state
*, gfp_t
);
1173 void xas_pause(struct xa_state
*);
1175 void xas_create_range(struct xa_state
*);
1178 * xas_reload() - Refetch an entry from the xarray.
1179 * @xas: XArray operation state.
1181 * Use this function to check that a previously loaded entry still has
1182 * the same value. This is useful for the lockless pagecache lookup where
1183 * we walk the array with only the RCU lock to protect us, lock the page,
1184 * then check that the page hasn't moved since we looked it up.
1186 * The caller guarantees that @xas is still valid. If it may be in an
1187 * error or restart state, call xas_load() instead.
1189 * Return: The entry at this location in the xarray.
1191 static inline void *xas_reload(struct xa_state
*xas
)
1193 struct xa_node
*node
= xas
->xa_node
;
1196 return xa_entry(xas
->xa
, node
, xas
->xa_offset
);
1197 return xa_head(xas
->xa
);
1201 * xas_set() - Set up XArray operation state for a different index.
1202 * @xas: XArray operation state.
1203 * @index: New index into the XArray.
1205 * Move the operation state to refer to a different index. This will
1206 * have the effect of starting a walk from the top; see xas_next()
1207 * to move to an adjacent index.
1209 static inline void xas_set(struct xa_state
*xas
, unsigned long index
)
1211 xas
->xa_index
= index
;
1212 xas
->xa_node
= XAS_RESTART
;
1216 * xas_set_order() - Set up XArray operation state for a multislot entry.
1217 * @xas: XArray operation state.
1218 * @index: Target of the operation.
1219 * @order: Entry occupies 2^@order indices.
1221 static inline void xas_set_order(struct xa_state
*xas
, unsigned long index
,
1224 #ifdef CONFIG_XARRAY_MULTI
1225 xas
->xa_index
= order
< BITS_PER_LONG
? (index
>> order
) << order
: 0;
1226 xas
->xa_shift
= order
- (order
% XA_CHUNK_SHIFT
);
1227 xas
->xa_sibs
= (1 << (order
% XA_CHUNK_SHIFT
)) - 1;
1228 xas
->xa_node
= XAS_RESTART
;
1231 xas_set(xas
, index
);
1236 * xas_set_update() - Set up XArray operation state for a callback.
1237 * @xas: XArray operation state.
1238 * @update: Function to call when updating a node.
1240 * The XArray can notify a caller after it has updated an xa_node.
1241 * This is advanced functionality and is only needed by the page cache.
1243 static inline void xas_set_update(struct xa_state
*xas
, xa_update_node_t update
)
1245 xas
->xa_update
= update
;
1249 * xas_next_entry() - Advance iterator to next present entry.
1250 * @xas: XArray operation state.
1251 * @max: Highest index to return.
1253 * xas_next_entry() is an inline function to optimise xarray traversal for
1254 * speed. It is equivalent to calling xas_find(), and will call xas_find()
1255 * for all the hard cases.
1257 * Return: The next present entry after the one currently referred to by @xas.
1259 static inline void *xas_next_entry(struct xa_state
*xas
, unsigned long max
)
1261 struct xa_node
*node
= xas
->xa_node
;
1264 if (unlikely(xas_not_node(node
) || node
->shift
||
1265 xas
->xa_offset
!= (xas
->xa_index
& XA_CHUNK_MASK
)))
1266 return xas_find(xas
, max
);
1269 if (unlikely(xas
->xa_index
>= max
))
1270 return xas_find(xas
, max
);
1271 if (unlikely(xas
->xa_offset
== XA_CHUNK_MASK
))
1272 return xas_find(xas
, max
);
1273 entry
= xa_entry(xas
->xa
, node
, xas
->xa_offset
+ 1);
1274 if (unlikely(xa_is_internal(entry
)))
1275 return xas_find(xas
, max
);
1284 static inline unsigned int xas_find_chunk(struct xa_state
*xas
, bool advance
,
1287 unsigned long *addr
= xas
->xa_node
->marks
[(__force
unsigned)mark
];
1288 unsigned int offset
= xas
->xa_offset
;
1292 if (XA_CHUNK_SIZE
== BITS_PER_LONG
) {
1293 if (offset
< XA_CHUNK_SIZE
) {
1294 unsigned long data
= *addr
& (~0UL << offset
);
1298 return XA_CHUNK_SIZE
;
1301 return find_next_bit(addr
, XA_CHUNK_SIZE
, offset
);
1305 * xas_next_marked() - Advance iterator to next marked entry.
1306 * @xas: XArray operation state.
1307 * @max: Highest index to return.
1308 * @mark: Mark to search for.
1310 * xas_next_marked() is an inline function to optimise xarray traversal for
1311 * speed. It is equivalent to calling xas_find_marked(), and will call
1312 * xas_find_marked() for all the hard cases.
1314 * Return: The next marked entry after the one currently referred to by @xas.
1316 static inline void *xas_next_marked(struct xa_state
*xas
, unsigned long max
,
1319 struct xa_node
*node
= xas
->xa_node
;
1320 unsigned int offset
;
1322 if (unlikely(xas_not_node(node
) || node
->shift
))
1323 return xas_find_marked(xas
, max
, mark
);
1324 offset
= xas_find_chunk(xas
, true, mark
);
1325 xas
->xa_offset
= offset
;
1326 xas
->xa_index
= (xas
->xa_index
& ~XA_CHUNK_MASK
) + offset
;
1327 if (xas
->xa_index
> max
)
1329 if (offset
== XA_CHUNK_SIZE
)
1330 return xas_find_marked(xas
, max
, mark
);
1331 return xa_entry(xas
->xa
, node
, offset
);
1335 * If iterating while holding a lock, drop the lock and reschedule
1336 * every %XA_CHECK_SCHED loops.
1339 XA_CHECK_SCHED
= 4096,
1343 * xas_for_each() - Iterate over a range of an XArray.
1344 * @xas: XArray operation state.
1345 * @entry: Entry retrieved from the array.
1346 * @max: Maximum index to retrieve from array.
1348 * The loop body will be executed for each entry present in the xarray
1349 * between the current xas position and @max. @entry will be set to
1350 * the entry retrieved from the xarray. It is safe to delete entries
1351 * from the array in the loop body. You should hold either the RCU lock
1352 * or the xa_lock while iterating. If you need to drop the lock, call
1353 * xas_pause() first.
1355 #define xas_for_each(xas, entry, max) \
1356 for (entry = xas_find(xas, max); entry; \
1357 entry = xas_next_entry(xas, max))
1360 * xas_for_each_marked() - Iterate over a range of an XArray.
1361 * @xas: XArray operation state.
1362 * @entry: Entry retrieved from the array.
1363 * @max: Maximum index to retrieve from array.
1364 * @mark: Mark to search for.
1366 * The loop body will be executed for each marked entry in the xarray
1367 * between the current xas position and @max. @entry will be set to
1368 * the entry retrieved from the xarray. It is safe to delete entries
1369 * from the array in the loop body. You should hold either the RCU lock
1370 * or the xa_lock while iterating. If you need to drop the lock, call
1371 * xas_pause() first.
1373 #define xas_for_each_marked(xas, entry, max, mark) \
1374 for (entry = xas_find_marked(xas, max, mark); entry; \
1375 entry = xas_next_marked(xas, max, mark))
1378 * xas_for_each_conflict() - Iterate over a range of an XArray.
1379 * @xas: XArray operation state.
1380 * @entry: Entry retrieved from the array.
1382 * The loop body will be executed for each entry in the XArray that lies
1383 * within the range specified by @xas. If the loop completes successfully,
1384 * any entries that lie in this range will be replaced by @entry. The caller
1385 * may break out of the loop; if they do so, the contents of the XArray will
1386 * be unchanged. The operation may fail due to an out of memory condition.
1387 * The caller may also call xa_set_err() to exit the loop while setting an
1388 * error to record the reason.
1390 #define xas_for_each_conflict(xas, entry) \
1391 while ((entry = xas_find_conflict(xas)))
1393 void *__xas_next(struct xa_state
*);
1394 void *__xas_prev(struct xa_state
*);
1397 * xas_prev() - Move iterator to previous index.
1398 * @xas: XArray operation state.
1400 * If the @xas was in an error state, it will remain in an error state
1401 * and this function will return %NULL. If the @xas has never been walked,
1402 * it will have the effect of calling xas_load(). Otherwise one will be
1403 * subtracted from the index and the state will be walked to the correct
1404 * location in the array for the next operation.
1406 * If the iterator was referencing index 0, this function wraps
1407 * around to %ULONG_MAX.
1409 * Return: The entry at the new index. This may be %NULL or an internal
1412 static inline void *xas_prev(struct xa_state
*xas
)
1414 struct xa_node
*node
= xas
->xa_node
;
1416 if (unlikely(xas_not_node(node
) || node
->shift
||
1417 xas
->xa_offset
== 0))
1418 return __xas_prev(xas
);
1422 return xa_entry(xas
->xa
, node
, xas
->xa_offset
);
1426 * xas_next() - Move state to next index.
1427 * @xas: XArray operation state.
1429 * If the @xas was in an error state, it will remain in an error state
1430 * and this function will return %NULL. If the @xas has never been walked,
1431 * it will have the effect of calling xas_load(). Otherwise one will be
1432 * added to the index and the state will be walked to the correct
1433 * location in the array for the next operation.
1435 * If the iterator was referencing index %ULONG_MAX, this function wraps
1438 * Return: The entry at the new index. This may be %NULL or an internal
1441 static inline void *xas_next(struct xa_state
*xas
)
1443 struct xa_node
*node
= xas
->xa_node
;
1445 if (unlikely(xas_not_node(node
) || node
->shift
||
1446 xas
->xa_offset
== XA_CHUNK_MASK
))
1447 return __xas_next(xas
);
1451 return xa_entry(xas
->xa
, node
, xas
->xa_offset
);
1454 #endif /* _LINUX_XARRAY_H */