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
37 * Errors are also represented as internal entries, but use the negative
38 * space (-4094 to -2). They're never stored in the slots array; only
39 * returned by the normal API.
42 #define BITS_PER_XA_VALUE (BITS_PER_LONG - 1)
45 * xa_mk_value() - Create an XArray entry from an integer.
46 * @v: Value to store in XArray.
48 * Context: Any context.
49 * Return: An entry suitable for storing in the XArray.
51 static inline void *xa_mk_value(unsigned long v)
54 return (void *)((v << 1) | 1);
58 * xa_to_value() - Get value stored in an XArray entry.
59 * @entry: XArray entry.
61 * Context: Any context.
62 * Return: The value stored in the XArray entry.
64 static inline unsigned long xa_to_value(const void *entry)
66 return (unsigned long)entry >> 1;
70 * xa_is_value() - Determine if an entry is a value.
71 * @entry: XArray entry.
73 * Context: Any context.
74 * Return: True if the entry is a value, false if it is a pointer.
76 static inline bool xa_is_value(const void *entry)
78 return (unsigned long)entry & 1;
82 * xa_tag_pointer() - Create an XArray entry for a tagged pointer.
84 * @tag: Tag value (0, 1 or 3).
86 * If the user of the XArray prefers, they can tag their pointers instead
87 * of storing value entries. Three tags are available (0, 1 and 3).
88 * These are distinct from the xa_mark_t as they are not replicated up
89 * through the array and cannot be searched for.
91 * Context: Any context.
92 * Return: An XArray entry.
94 static inline void *xa_tag_pointer(void *p, unsigned long tag)
96 return (void *)((unsigned long)p | tag);
100 * xa_untag_pointer() - Turn an XArray entry into a plain pointer.
101 * @entry: XArray entry.
103 * If you have stored a tagged pointer in the XArray, call this function
104 * to get the untagged version of the pointer.
106 * Context: Any context.
109 static inline void *xa_untag_pointer(void *entry)
111 return (void *)((unsigned long)entry & ~3UL);
115 * xa_pointer_tag() - Get the tag stored in an XArray entry.
116 * @entry: XArray entry.
118 * If you have stored a tagged pointer in the XArray, call this function
119 * to get the tag of that pointer.
121 * Context: Any context.
124 static inline unsigned int xa_pointer_tag(void *entry)
126 return (unsigned long)entry & 3UL;
130 * xa_mk_internal() - Create an internal entry.
131 * @v: Value to turn into an internal entry.
133 * Context: Any context.
134 * Return: An XArray internal entry corresponding to this value.
136 static inline void *xa_mk_internal(unsigned long v)
138 return (void *)((v << 2) | 2);
142 * xa_to_internal() - Extract the value from an internal entry.
143 * @entry: XArray entry.
145 * Context: Any context.
146 * Return: The value which was stored in the internal entry.
148 static inline unsigned long xa_to_internal(const void *entry)
150 return (unsigned long)entry >> 2;
154 * xa_is_internal() - Is the entry an internal entry?
155 * @entry: XArray entry.
157 * Context: Any context.
158 * Return: %true if the entry is an internal entry.
160 static inline bool xa_is_internal(const void *entry)
162 return ((unsigned long)entry & 3) == 2;
166 * xa_is_err() - Report whether an XArray operation returned an error
167 * @entry: Result from calling an XArray function
169 * If an XArray operation cannot complete an operation, it will return
170 * a special value indicating an error. This function tells you
171 * whether an error occurred; xa_err() tells you which error occurred.
173 * Context: Any context.
174 * Return: %true if the entry indicates an error.
176 static inline bool xa_is_err(const void *entry)
178 return unlikely(xa_is_internal(entry));
182 * xa_err() - Turn an XArray result into an errno.
183 * @entry: Result from calling an XArray function.
185 * If an XArray operation cannot complete an operation, it will return
186 * a special pointer value which encodes an errno. This function extracts
187 * the errno from the pointer value, or returns 0 if the pointer does not
188 * represent an errno.
190 * Context: Any context.
191 * Return: A negative errno or 0.
193 static inline int xa_err(void *entry)
195 /* xa_to_internal() would not do sign extension. */
196 if (xa_is_err(entry))
197 return (long)entry >> 2;
201 typedef unsigned __bitwise xa_mark_t;
202 #define XA_MARK_0 ((__force xa_mark_t)0U)
203 #define XA_MARK_1 ((__force xa_mark_t)1U)
204 #define XA_MARK_2 ((__force xa_mark_t)2U)
205 #define XA_PRESENT ((__force xa_mark_t)8U)
206 #define XA_MARK_MAX XA_MARK_2
214 * Values for xa_flags. The radix tree stores its GFP flags in the xa_flags,
215 * and we remain compatible with that.
217 #define XA_FLAGS_LOCK_IRQ ((__force gfp_t)XA_LOCK_IRQ)
218 #define XA_FLAGS_LOCK_BH ((__force gfp_t)XA_LOCK_BH)
219 #define XA_FLAGS_MARK(mark) ((__force gfp_t)((1U << __GFP_BITS_SHIFT) << \
220 (__force unsigned)(mark)))
223 * struct xarray - The anchor of the XArray.
224 * @xa_lock: Lock that protects the contents of the XArray.
226 * To use the xarray, define it statically or embed it in your data structure.
227 * It is a very small data structure, so it does not usually make sense to
228 * allocate it separately and keep a pointer to it in your data structure.
230 * You may use the xa_lock to protect your own data structures as well.
233 * If all of the entries in the array are NULL, @xa_head is a NULL pointer.
234 * If the only non-NULL entry in the array is at index 0, @xa_head is that
235 * entry. If any other entry in the array is non-NULL, @xa_head points
240 /* private: The rest of the data structure is not to be used directly. */
242 void __rcu * xa_head;
245 #define XARRAY_INIT(name, flags) { \
246 .xa_lock = __SPIN_LOCK_UNLOCKED(name.xa_lock), \
252 * DEFINE_XARRAY_FLAGS() - Define an XArray with custom flags.
253 * @name: A string that names your XArray.
254 * @flags: XA_FLAG values.
256 * This is intended for file scope definitions of XArrays. It declares
257 * and initialises an empty XArray with the chosen name and flags. It is
258 * equivalent to calling xa_init_flags() on the array, but it does the
259 * initialisation at compiletime instead of runtime.
261 #define DEFINE_XARRAY_FLAGS(name, flags) \
262 struct xarray name = XARRAY_INIT(name, flags)
265 * DEFINE_XARRAY() - Define an XArray.
266 * @name: A string that names your XArray.
268 * This is intended for file scope definitions of XArrays. It declares
269 * and initialises an empty XArray with the chosen name. It is equivalent
270 * to calling xa_init() on the array, but it does the initialisation at
271 * compiletime instead of runtime.
273 #define DEFINE_XARRAY(name) DEFINE_XARRAY_FLAGS(name, 0)
275 void xa_init_flags(struct xarray *, gfp_t flags);
276 void *xa_load(struct xarray *, unsigned long index);
277 void *xa_store(struct xarray *, unsigned long index, void *entry, gfp_t);
278 void *xa_cmpxchg(struct xarray *, unsigned long index,
279 void *old, void *entry, gfp_t);
280 bool xa_get_mark(struct xarray *, unsigned long index, xa_mark_t);
281 void xa_set_mark(struct xarray *, unsigned long index, xa_mark_t);
282 void xa_clear_mark(struct xarray *, unsigned long index, xa_mark_t);
285 * xa_init() - Initialise an empty XArray.
288 * An empty XArray is full of NULL entries.
290 * Context: Any context.
292 static inline void xa_init(struct xarray *xa)
294 xa_init_flags(xa, 0);
298 * xa_empty() - Determine if an array has any present entries.
301 * Context: Any context.
302 * Return: %true if the array contains only NULL pointers.
304 static inline bool xa_empty(const struct xarray *xa)
306 return xa->xa_head == NULL;
310 * xa_marked() - Inquire whether any entry in this array has a mark set
314 * Context: Any context.
315 * Return: %true if any entry has this mark set.
317 static inline bool xa_marked(const struct xarray *xa, xa_mark_t mark)
319 return xa->xa_flags & XA_FLAGS_MARK(mark);
323 * xa_erase() - Erase this entry from the XArray.
325 * @index: Index of entry.
327 * This function is the equivalent of calling xa_store() with %NULL as
328 * the third argument. The XArray does not need to allocate memory, so
329 * the user does not need to provide GFP flags.
331 * Context: Process context. Takes and releases the xa_lock.
332 * Return: The entry which used to be at this index.
334 static inline void *xa_erase(struct xarray *xa, unsigned long index)
336 return xa_store(xa, index, NULL, 0);
340 * xa_insert() - Store this entry in the XArray unless another entry is
343 * @index: Index into array.
345 * @gfp: Memory allocation flags.
347 * If you would rather see the existing entry in the array, use xa_cmpxchg().
348 * This function is for users who don't care what the entry is, only that
351 * Context: Process context. Takes and releases the xa_lock.
352 * May sleep if the @gfp flags permit.
353 * Return: 0 if the store succeeded. -EEXIST if another entry was present.
354 * -ENOMEM if memory could not be allocated.
356 static inline int xa_insert(struct xarray *xa, unsigned long index,
357 void *entry, gfp_t gfp)
359 void *curr = xa_cmpxchg(xa, index, NULL, entry, gfp);
367 #define xa_trylock(xa) spin_trylock(&(xa)->xa_lock)
368 #define xa_lock(xa) spin_lock(&(xa)->xa_lock)
369 #define xa_unlock(xa) spin_unlock(&(xa)->xa_lock)
370 #define xa_lock_bh(xa) spin_lock_bh(&(xa)->xa_lock)
371 #define xa_unlock_bh(xa) spin_unlock_bh(&(xa)->xa_lock)
372 #define xa_lock_irq(xa) spin_lock_irq(&(xa)->xa_lock)
373 #define xa_unlock_irq(xa) spin_unlock_irq(&(xa)->xa_lock)
374 #define xa_lock_irqsave(xa, flags) \
375 spin_lock_irqsave(&(xa)->xa_lock, flags)
376 #define xa_unlock_irqrestore(xa, flags) \
377 spin_unlock_irqrestore(&(xa)->xa_lock, flags)
380 * Versions of the normal API which require the caller to hold the
381 * xa_lock. If the GFP flags allow it, they will drop the lock to
382 * allocate memory, then reacquire it afterwards. These functions
383 * may also re-enable interrupts if the XArray flags indicate the
384 * locking should be interrupt safe.
386 void *__xa_erase(struct xarray *, unsigned long index);
387 void *__xa_store(struct xarray *, unsigned long index, void *entry, gfp_t);
388 void *__xa_cmpxchg(struct xarray *, unsigned long index, void *old,
390 void __xa_set_mark(struct xarray *, unsigned long index, xa_mark_t);
391 void __xa_clear_mark(struct xarray *, unsigned long index, xa_mark_t);
394 * __xa_insert() - Store this entry in the XArray unless another entry is
397 * @index: Index into array.
399 * @gfp: Memory allocation flags.
401 * If you would rather see the existing entry in the array, use __xa_cmpxchg().
402 * This function is for users who don't care what the entry is, only that
405 * Context: Any context. Expects xa_lock to be held on entry. May
406 * release and reacquire xa_lock if the @gfp flags permit.
407 * Return: 0 if the store succeeded. -EEXIST if another entry was present.
408 * -ENOMEM if memory could not be allocated.
410 static inline int __xa_insert(struct xarray *xa, unsigned long index,
411 void *entry, gfp_t gfp)
413 void *curr = __xa_cmpxchg(xa, index, NULL, entry, gfp);
422 * xa_erase_bh() - Erase this entry from the XArray.
424 * @index: Index of entry.
426 * This function is the equivalent of calling xa_store() with %NULL as
427 * the third argument. The XArray does not need to allocate memory, so
428 * the user does not need to provide GFP flags.
430 * Context: Process context. Takes and releases the xa_lock while
431 * disabling softirqs.
432 * Return: The entry which used to be at this index.
434 static inline void *xa_erase_bh(struct xarray *xa, unsigned long index)
439 entry = __xa_erase(xa, index);
446 * xa_erase_irq() - Erase this entry from the XArray.
448 * @index: Index of entry.
450 * This function is the equivalent of calling xa_store() with %NULL as
451 * the third argument. The XArray does not need to allocate memory, so
452 * the user does not need to provide GFP flags.
454 * Context: Process context. Takes and releases the xa_lock while
455 * disabling interrupts.
456 * Return: The entry which used to be at this index.
458 static inline void *xa_erase_irq(struct xarray *xa, unsigned long index)
463 entry = __xa_erase(xa, index);
469 /* Everything below here is the Advanced API. Proceed with caution. */
472 * The xarray is constructed out of a set of 'chunks' of pointers. Choosing
473 * the best chunk size requires some tradeoffs. A power of two recommends
474 * itself so that we can walk the tree based purely on shifts and masks.
475 * Generally, the larger the better; as the number of slots per level of the
476 * tree increases, the less tall the tree needs to be. But that needs to be
477 * balanced against the memory consumption of each node. On a 64-bit system,
478 * xa_node is currently 576 bytes, and we get 7 of them per 4kB page. If we
479 * doubled the number of slots per node, we'd get only 3 nodes per 4kB page.
481 #ifndef XA_CHUNK_SHIFT
482 #define XA_CHUNK_SHIFT (CONFIG_BASE_SMALL ? 4 : 6)
484 #define XA_CHUNK_SIZE (1UL << XA_CHUNK_SHIFT)
485 #define XA_CHUNK_MASK (XA_CHUNK_SIZE - 1)
486 #define XA_MAX_MARKS 3
487 #define XA_MARK_LONGS DIV_ROUND_UP(XA_CHUNK_SIZE, BITS_PER_LONG)
490 * @count is the count of every non-NULL element in the ->slots array
491 * whether that is a value entry, a retry entry, a user pointer,
492 * a sibling entry or a pointer to the next level of the tree.
493 * @nr_values is the count of every element in ->slots which is
494 * either a value entry or a sibling of a value entry.
497 unsigned char shift; /* Bits remaining in each slot */
498 unsigned char offset; /* Slot offset in parent */
499 unsigned char count; /* Total entry count */
500 unsigned char nr_values; /* Value entry count */
501 struct xa_node __rcu *parent; /* NULL at top of tree */
502 struct xarray *array; /* The array we belong to */
504 struct list_head private_list; /* For tree user */
505 struct rcu_head rcu_head; /* Used when freeing node */
507 void __rcu *slots[XA_CHUNK_SIZE];
509 unsigned long tags[XA_MAX_MARKS][XA_MARK_LONGS];
510 unsigned long marks[XA_MAX_MARKS][XA_MARK_LONGS];
514 void xa_dump(const struct xarray *);
515 void xa_dump_node(const struct xa_node *);
518 #define XA_BUG_ON(xa, x) do { \
524 #define XA_NODE_BUG_ON(node, x) do { \
526 if (node) xa_dump_node(node); \
531 #define XA_BUG_ON(xa, x) do { } while (0)
532 #define XA_NODE_BUG_ON(node, x) do { } while (0)
536 static inline void *xa_head(const struct xarray *xa)
538 return rcu_dereference_check(xa->xa_head,
539 lockdep_is_held(&xa->xa_lock));
543 static inline void *xa_head_locked(const struct xarray *xa)
545 return rcu_dereference_protected(xa->xa_head,
546 lockdep_is_held(&xa->xa_lock));
550 static inline void *xa_entry(const struct xarray *xa,
551 const struct xa_node *node, unsigned int offset)
553 XA_NODE_BUG_ON(node, offset >= XA_CHUNK_SIZE);
554 return rcu_dereference_check(node->slots[offset],
555 lockdep_is_held(&xa->xa_lock));
559 static inline void *xa_entry_locked(const struct xarray *xa,
560 const struct xa_node *node, unsigned int offset)
562 XA_NODE_BUG_ON(node, offset >= XA_CHUNK_SIZE);
563 return rcu_dereference_protected(node->slots[offset],
564 lockdep_is_held(&xa->xa_lock));
568 static inline struct xa_node *xa_parent(const struct xarray *xa,
569 const struct xa_node *node)
571 return rcu_dereference_check(node->parent,
572 lockdep_is_held(&xa->xa_lock));
576 static inline struct xa_node *xa_parent_locked(const struct xarray *xa,
577 const struct xa_node *node)
579 return rcu_dereference_protected(node->parent,
580 lockdep_is_held(&xa->xa_lock));
584 static inline void *xa_mk_node(const struct xa_node *node)
586 return (void *)((unsigned long)node | 2);
590 static inline struct xa_node *xa_to_node(const void *entry)
592 return (struct xa_node *)((unsigned long)entry - 2);
596 static inline bool xa_is_node(const void *entry)
598 return xa_is_internal(entry) && (unsigned long)entry > 4096;
602 static inline void *xa_mk_sibling(unsigned int offset)
604 return xa_mk_internal(offset);
608 static inline unsigned long xa_to_sibling(const void *entry)
610 return xa_to_internal(entry);
614 * xa_is_sibling() - Is the entry a sibling entry?
615 * @entry: Entry retrieved from the XArray
617 * Return: %true if the entry is a sibling entry.
619 static inline bool xa_is_sibling(const void *entry)
621 return IS_ENABLED(CONFIG_XARRAY_MULTI) && xa_is_internal(entry) &&
622 (entry < xa_mk_sibling(XA_CHUNK_SIZE - 1));
625 #define XA_RETRY_ENTRY xa_mk_internal(256)
628 * xa_is_retry() - Is the entry a retry entry?
629 * @entry: Entry retrieved from the XArray
631 * Return: %true if the entry is a retry entry.
633 static inline bool xa_is_retry(const void *entry)
635 return unlikely(entry == XA_RETRY_ENTRY);
639 * typedef xa_update_node_t - A callback function from the XArray.
640 * @node: The node which is being processed
642 * This function is called every time the XArray updates the count of
643 * present and value entries in a node. It allows advanced users to
644 * maintain the private_list in the node.
646 * Context: The xa_lock is held and interrupts may be disabled.
647 * Implementations should not drop the xa_lock, nor re-enable
650 typedef void (*xa_update_node_t)(struct xa_node *node);
653 * The xa_state is opaque to its users. It contains various different pieces
654 * of state involved in the current operation on the XArray. It should be
655 * declared on the stack and passed between the various internal routines.
656 * The various elements in it should not be accessed directly, but only
657 * through the provided accessor functions. The below documentation is for
658 * the benefit of those working on the code, not for users of the XArray.
660 * @xa_node usually points to the xa_node containing the slot we're operating
661 * on (and @xa_offset is the offset in the slots array). If there is a
662 * single entry in the array at index 0, there are no allocated xa_nodes to
663 * point to, and so we store %NULL in @xa_node. @xa_node is set to
664 * the value %XAS_RESTART if the xa_state is not walked to the correct
665 * position in the tree of nodes for this operation. If an error occurs
666 * during an operation, it is set to an %XAS_ERROR value. If we run off the
667 * end of the allocated nodes, it is set to %XAS_BOUNDS.
671 unsigned long xa_index;
672 unsigned char xa_shift;
673 unsigned char xa_sibs;
674 unsigned char xa_offset;
675 unsigned char xa_pad; /* Helps gcc generate better code */
676 struct xa_node *xa_node;
677 struct xa_node *xa_alloc;
678 xa_update_node_t xa_update;
682 * We encode errnos in the xas->xa_node. If an error has happened, we need to
683 * drop the lock to fix it, and once we've done so the xa_state is invalid.
685 #define XA_ERROR(errno) ((struct xa_node *)(((unsigned long)errno << 2) | 2UL))
686 #define XAS_BOUNDS ((struct xa_node *)1UL)
687 #define XAS_RESTART ((struct xa_node *)3UL)
689 #define __XA_STATE(array, index, shift, sibs) { \
696 .xa_node = XAS_RESTART, \
702 * XA_STATE() - Declare an XArray operation state.
703 * @name: Name of this operation state (usually xas).
704 * @array: Array to operate on.
705 * @index: Initial index of interest.
707 * Declare and initialise an xa_state on the stack.
709 #define XA_STATE(name, array, index) \
710 struct xa_state name = __XA_STATE(array, index, 0, 0)
713 * XA_STATE_ORDER() - Declare an XArray operation state.
714 * @name: Name of this operation state (usually xas).
715 * @array: Array to operate on.
716 * @index: Initial index of interest.
717 * @order: Order of entry.
719 * Declare and initialise an xa_state on the stack. This variant of
720 * XA_STATE() allows you to specify the 'order' of the element you
721 * want to operate on.`
723 #define XA_STATE_ORDER(name, array, index, order) \
724 struct xa_state name = __XA_STATE(array, \
725 (index >> order) << order, \
726 order - (order % XA_CHUNK_SHIFT), \
727 (1U << (order % XA_CHUNK_SHIFT)) - 1)
729 #define xas_marked(xas, mark) xa_marked((xas)->xa, (mark))
730 #define xas_trylock(xas) xa_trylock((xas)->xa)
731 #define xas_lock(xas) xa_lock((xas)->xa)
732 #define xas_unlock(xas) xa_unlock((xas)->xa)
733 #define xas_lock_bh(xas) xa_lock_bh((xas)->xa)
734 #define xas_unlock_bh(xas) xa_unlock_bh((xas)->xa)
735 #define xas_lock_irq(xas) xa_lock_irq((xas)->xa)
736 #define xas_unlock_irq(xas) xa_unlock_irq((xas)->xa)
737 #define xas_lock_irqsave(xas, flags) \
738 xa_lock_irqsave((xas)->xa, flags)
739 #define xas_unlock_irqrestore(xas, flags) \
740 xa_unlock_irqrestore((xas)->xa, flags)
743 * xas_error() - Return an errno stored in the xa_state.
744 * @xas: XArray operation state.
746 * Return: 0 if no error has been noted. A negative errno if one has.
748 static inline int xas_error(const struct xa_state *xas)
750 return xa_err(xas->xa_node);
754 * xas_set_err() - Note an error in the xa_state.
755 * @xas: XArray operation state.
756 * @err: Negative error number.
758 * Only call this function with a negative @err; zero or positive errors
759 * will probably not behave the way you think they should. If you want
760 * to clear the error from an xa_state, use xas_reset().
762 static inline void xas_set_err(struct xa_state *xas, long err)
764 xas->xa_node = XA_ERROR(err);
768 * xas_invalid() - Is the xas in a retry or error state?
769 * @xas: XArray operation state.
771 * Return: %true if the xas cannot be used for operations.
773 static inline bool xas_invalid(const struct xa_state *xas)
775 return (unsigned long)xas->xa_node & 3;
779 * xas_valid() - Is the xas a valid cursor into the array?
780 * @xas: XArray operation state.
782 * Return: %true if the xas can be used for operations.
784 static inline bool xas_valid(const struct xa_state *xas)
786 return !xas_invalid(xas);
789 /* True if the pointer is something other than a node */
790 static inline bool xas_not_node(struct xa_node *node)
792 return ((unsigned long)node & 3) || !node;
795 /* True if the node represents head-of-tree, RESTART or BOUNDS */
796 static inline bool xas_top(struct xa_node *node)
798 return node <= XAS_RESTART;
802 * xas_reset() - Reset an XArray operation state.
803 * @xas: XArray operation state.
805 * Resets the error or walk state of the @xas so future walks of the
806 * array will start from the root. Use this if you have dropped the
807 * xarray lock and want to reuse the xa_state.
809 * Context: Any context.
811 static inline void xas_reset(struct xa_state *xas)
813 xas->xa_node = XAS_RESTART;
817 * xas_retry() - Retry the operation if appropriate.
818 * @xas: XArray operation state.
819 * @entry: Entry from xarray.
821 * The advanced functions may sometimes return an internal entry, such as
822 * a retry entry or a zero entry. This function sets up the @xas to restart
823 * the walk from the head of the array if needed.
825 * Context: Any context.
826 * Return: true if the operation needs to be retried.
828 static inline bool xas_retry(struct xa_state *xas, const void *entry)
830 if (!xa_is_retry(entry))
836 void *xas_load(struct xa_state *);
837 void *xas_store(struct xa_state *, void *entry);
839 bool xas_get_mark(const struct xa_state *, xa_mark_t);
840 void xas_set_mark(const struct xa_state *, xa_mark_t);
841 void xas_clear_mark(const struct xa_state *, xa_mark_t);
842 void xas_init_marks(const struct xa_state *);
844 bool xas_nomem(struct xa_state *, gfp_t);
847 * xas_reload() - Refetch an entry from the xarray.
848 * @xas: XArray operation state.
850 * Use this function to check that a previously loaded entry still has
851 * the same value. This is useful for the lockless pagecache lookup where
852 * we walk the array with only the RCU lock to protect us, lock the page,
853 * then check that the page hasn't moved since we looked it up.
855 * The caller guarantees that @xas is still valid. If it may be in an
856 * error or restart state, call xas_load() instead.
858 * Return: The entry at this location in the xarray.
860 static inline void *xas_reload(struct xa_state *xas)
862 struct xa_node *node = xas->xa_node;
865 return xa_entry(xas->xa, node, xas->xa_offset);
866 return xa_head(xas->xa);
870 * xas_set() - Set up XArray operation state for a different index.
871 * @xas: XArray operation state.
872 * @index: New index into the XArray.
874 * Move the operation state to refer to a different index. This will
875 * have the effect of starting a walk from the top; see xas_next()
876 * to move to an adjacent index.
878 static inline void xas_set(struct xa_state *xas, unsigned long index)
880 xas->xa_index = index;
881 xas->xa_node = XAS_RESTART;
885 * xas_set_order() - Set up XArray operation state for a multislot entry.
886 * @xas: XArray operation state.
887 * @index: Target of the operation.
888 * @order: Entry occupies 2^@order indices.
890 static inline void xas_set_order(struct xa_state *xas, unsigned long index,
893 #ifdef CONFIG_XARRAY_MULTI
894 xas->xa_index = order < BITS_PER_LONG ? (index >> order) << order : 0;
895 xas->xa_shift = order - (order % XA_CHUNK_SHIFT);
896 xas->xa_sibs = (1 << (order % XA_CHUNK_SHIFT)) - 1;
897 xas->xa_node = XAS_RESTART;
905 * xas_set_update() - Set up XArray operation state for a callback.
906 * @xas: XArray operation state.
907 * @update: Function to call when updating a node.
909 * The XArray can notify a caller after it has updated an xa_node.
910 * This is advanced functionality and is only needed by the page cache.
912 static inline void xas_set_update(struct xa_state *xas, xa_update_node_t update)
914 xas->xa_update = update;
917 #endif /* _LINUX_XARRAY_H */