1 // SPDX-License-Identifier: GPL-2.0+
3 * XArray implementation
4 * Copyright (c) 2017 Microsoft Corporation
5 * Author: Matthew Wilcox <willy@infradead.org>
7 * This is taken from kernel commit:
9 * 7b785645e ("mm: fix page cache convergence regression")
11 * at kernel verison 5.2-rc2
13 #ifndef HAVE_XARRAY_SUPPORT
14 #include <linux/bitmap.h>
15 #include <linux/export.h>
16 #include <linux/list.h>
17 #include <linux/slab.h>
18 #include <linux/radix-tree.h>
19 #include <libcfs/linux/xarray.h>
22 * Coding conventions in this file:
24 * @xa is used to refer to the entire xarray.
25 * @xas is the 'xarray operation state'. It may be either a pointer to
26 * an xa_state, or an xa_state stored on the stack. This is an unfortunate
28 * @index is the index of the entry being operated on
29 * @mark is an xa_mark_t; a small number indicating one of the mark bits.
30 * @node refers to an xa_node; usually the primary one being operated on by
32 * @offset is the index into the slots array inside an xa_node.
33 * @parent refers to the @xa_node closer to the head than @node.
34 * @entry refers to something stored in a slot in the xarray
37 static inline unsigned int xa_lock_type(const struct xarray *xa)
39 return (__force unsigned int)xa->xa_flags & 3;
42 static inline void xas_lock_type(struct xa_state *xas, unsigned int lock_type)
44 if (lock_type == XA_LOCK_IRQ)
46 else if (lock_type == XA_LOCK_BH)
52 static inline void xas_unlock_type(struct xa_state *xas, unsigned int lock_type)
54 if (lock_type == XA_LOCK_IRQ)
56 else if (lock_type == XA_LOCK_BH)
62 static inline bool xa_track_free(const struct xarray *xa)
64 return xa->xa_flags & XA_FLAGS_TRACK_FREE;
67 static inline bool xa_zero_busy(const struct xarray *xa)
69 return xa->xa_flags & XA_FLAGS_ZERO_BUSY;
72 static inline void xa_mark_set(struct xarray *xa, xa_mark_t mark)
74 if (!(xa->xa_flags & XA_FLAGS_MARK(mark)))
75 xa->xa_flags |= XA_FLAGS_MARK(mark);
78 static inline void xa_mark_clear(struct xarray *xa, xa_mark_t mark)
80 if (xa->xa_flags & XA_FLAGS_MARK(mark))
81 xa->xa_flags &= ~(XA_FLAGS_MARK(mark));
84 static inline unsigned long *node_marks(struct xa_node *node, xa_mark_t mark)
86 return node->marks[(__force unsigned)mark];
89 static inline bool node_get_mark(struct xa_node *node,
90 unsigned int offset, xa_mark_t mark)
92 return test_bit(offset, node_marks(node, mark));
95 /* returns true if the bit was set */
96 static inline bool node_set_mark(struct xa_node *node, unsigned int offset,
99 return __test_and_set_bit(offset, node_marks(node, mark));
102 /* returns true if the bit was set */
103 static inline bool node_clear_mark(struct xa_node *node, unsigned int offset,
106 return __test_and_clear_bit(offset, node_marks(node, mark));
109 static inline bool node_any_mark(struct xa_node *node, xa_mark_t mark)
111 return !bitmap_empty(node_marks(node, mark), XA_CHUNK_SIZE);
114 static inline void node_mark_all(struct xa_node *node, xa_mark_t mark)
116 bitmap_fill(node_marks(node, mark), XA_CHUNK_SIZE);
119 #define mark_inc(mark) do { \
120 mark = (__force xa_mark_t)((__force unsigned)(mark) + 1); \
124 * xas_squash_marks() - Merge all marks to the first entry
125 * @xas: Array operation state.
127 * Set a mark on the first entry if any entry has it set. Clear marks on
128 * all sibling entries.
130 static void xas_squash_marks(const struct xa_state *xas)
132 unsigned int mark = 0;
133 unsigned int limit = xas->xa_offset + xas->xa_sibs + 1;
139 unsigned long *marks = xas->xa_node->marks[mark];
140 if (find_next_bit(marks, limit, xas->xa_offset + 1) == limit)
142 __set_bit(xas->xa_offset, marks);
143 bitmap_clear(marks, xas->xa_offset + 1, xas->xa_sibs);
144 } while (mark++ != (__force unsigned)XA_MARK_MAX);
147 /* extracts the offset within this node from the index */
148 static unsigned int get_offset(unsigned long index, struct xa_node *node)
150 return (index >> node->shift) & XA_CHUNK_MASK;
153 static void xas_set_offset(struct xa_state *xas)
155 xas->xa_offset = get_offset(xas->xa_index, xas->xa_node);
158 /* move the index either forwards (find) or backwards (sibling slot) */
159 static void xas_move_index(struct xa_state *xas, unsigned long offset)
161 unsigned int shift = xas->xa_node->shift;
162 xas->xa_index &= ~XA_CHUNK_MASK << shift;
163 xas->xa_index += offset << shift;
166 static void xas_advance(struct xa_state *xas)
169 xas_move_index(xas, xas->xa_offset);
172 static void *set_bounds(struct xa_state *xas)
174 xas->xa_node = XAS_BOUNDS;
179 * Starts a walk. If the @xas is already valid, we assume that it's on
180 * the right path and just return where we've got to. If we're in an
181 * error state, return NULL. If the index is outside the current scope
182 * of the xarray, return NULL without changing @xas->xa_node. Otherwise
183 * set @xas->xa_node to NULL and return the current head of the array.
185 static void *xas_start(struct xa_state *xas)
190 return xas_reload(xas);
194 entry = xa_head(xas->xa);
195 if (!xa_is_node(entry)) {
197 return set_bounds(xas);
199 if ((xas->xa_index >> xa_to_node(entry)->shift) > XA_CHUNK_MASK)
200 return set_bounds(xas);
207 static void *xas_descend(struct xa_state *xas, struct xa_node *node)
209 unsigned int offset = get_offset(xas->xa_index, node);
210 void *entry = xa_entry(xas->xa, node, offset);
213 if (xa_is_sibling(entry)) {
214 offset = xa_to_sibling(entry);
215 entry = xa_entry(xas->xa, node, offset);
218 xas->xa_offset = offset;
223 * xas_load() - Load an entry from the XArray (advanced).
224 * @xas: XArray operation state.
226 * Usually walks the @xas to the appropriate state to load the entry
227 * stored at xa_index. However, it will do nothing and return %NULL if
228 * @xas is in an error state. xas_load() will never expand the tree.
230 * If the xa_state is set up to operate on a multi-index entry, xas_load()
231 * may return %NULL or an internal entry, even if there are entries
232 * present within the range specified by @xas.
234 * Context: Any context. The caller should hold the xa_lock or the RCU lock.
235 * Return: Usually an entry in the XArray, but see description for exceptions.
237 void *xas_load(struct xa_state *xas)
239 void *entry = xas_start(xas);
241 while (xa_is_node(entry)) {
242 struct xa_node *node = xa_to_node(entry);
244 if (xas->xa_shift > node->shift)
246 entry = xas_descend(xas, node);
247 if (node->shift == 0)
252 EXPORT_SYMBOL_GPL(xas_load);
254 /* Move the radix tree node cache here */
255 extern struct kmem_cache *radix_tree_node_cachep;
257 static inline void tag_clear(struct radix_tree_node *node, unsigned int tag,
260 __clear_bit(offset, node->tags[tag]);
263 static void radix_tree_node_rcu_free(struct rcu_head *head)
265 struct radix_tree_node *node =
266 container_of(head, struct radix_tree_node, rcu_head);
270 * must only free zeroed nodes into the slab. radix_tree_shrink
271 * can leave us with a non-NULL entry in the first slot, so clear
272 * that here to make sure.
274 for (i = 0; i < RADIX_TREE_MAX_TAGS; i++)
275 tag_clear(node, i, 0);
277 node->slots[0] = NULL;
280 kmem_cache_free(radix_tree_node_cachep, node);
283 #define XA_RCU_FREE ((struct xarray *)1)
285 static void xa_node_free(struct xa_node *node)
287 XA_NODE_BUG_ON(node, !list_empty(&node->private_list));
288 node->array = XA_RCU_FREE;
289 call_rcu(&node->rcu_head, radix_tree_node_rcu_free);
293 * xas_destroy() - Free any resources allocated during the XArray operation.
294 * @xas: XArray operation state.
296 * This function is now internal-only.
298 static void xas_destroy(struct xa_state *xas)
300 struct xa_node *node = xas->xa_alloc;
304 XA_NODE_BUG_ON(node, !list_empty(&node->private_list));
305 kmem_cache_free(radix_tree_node_cachep, node);
306 xas->xa_alloc = NULL;
310 * xas_nomem() - Allocate memory if needed.
311 * @xas: XArray operation state.
312 * @gfp: Memory allocation flags.
314 * If we need to add new nodes to the XArray, we try to allocate memory
315 * with GFP_NOWAIT while holding the lock, which will usually succeed.
316 * If it fails, @xas is flagged as needing memory to continue. The caller
317 * should drop the lock and call xas_nomem(). If xas_nomem() succeeds,
318 * the caller should retry the operation.
320 * Forward progress is guaranteed as one node is allocated here and
321 * stored in the xa_state where it will be found by xas_alloc(). More
322 * nodes will likely be found in the slab allocator, but we do not tie
325 * Return: true if memory was needed, and was successfully allocated.
327 bool xas_nomem(struct xa_state *xas, gfp_t gfp)
329 if (xas->xa_node != XA_ERROR(-ENOMEM)) {
334 if (xas->xa->xa_flags & XA_FLAGS_ACCOUNT)
335 gfp |= __GFP_ACCOUNT;
337 xas->xa_alloc = kmem_cache_alloc(radix_tree_node_cachep, gfp);
340 XA_NODE_BUG_ON(xas->xa_alloc, !list_empty(&xas->xa_alloc->private_list));
341 xas->xa_node = XAS_RESTART;
344 EXPORT_SYMBOL_GPL(xas_nomem);
347 * __xas_nomem() - Drop locks and allocate memory if needed.
348 * @xas: XArray operation state.
349 * @gfp: Memory allocation flags.
351 * Internal variant of xas_nomem().
353 * Return: true if memory was needed, and was successfully allocated.
355 static bool __xas_nomem(struct xa_state *xas, gfp_t gfp)
356 __must_hold(xas->xa->xa_lock)
358 unsigned int lock_type = xa_lock_type(xas->xa);
360 if (xas->xa_node != XA_ERROR(-ENOMEM)) {
365 if (xas->xa->xa_flags & XA_FLAGS_ACCOUNT)
366 gfp |= __GFP_ACCOUNT;
368 if (gfpflags_allow_blocking(gfp)) {
369 xas_unlock_type(xas, lock_type);
370 xas->xa_alloc = kmem_cache_alloc(radix_tree_node_cachep, gfp);
371 xas_lock_type(xas, lock_type);
373 xas->xa_alloc = kmem_cache_alloc(radix_tree_node_cachep, gfp);
377 XA_NODE_BUG_ON(xas->xa_alloc, !list_empty(&xas->xa_alloc->private_list));
378 xas->xa_node = XAS_RESTART;
382 static void xas_update(struct xa_state *xas, struct xa_node *node)
385 xas->xa_update(node);
387 XA_NODE_BUG_ON(node, !list_empty(&node->private_list));
390 static void *xas_alloc(struct xa_state *xas, unsigned int shift)
392 struct xa_node *parent = xas->xa_node;
393 struct xa_node *node = xas->xa_alloc;
395 if (xas_invalid(xas))
399 xas->xa_alloc = NULL;
401 gfp_t gfp = GFP_NOWAIT | __GFP_NOWARN;
403 if (xas->xa->xa_flags & XA_FLAGS_ACCOUNT)
404 gfp |= __GFP_ACCOUNT;
406 node = kmem_cache_alloc(radix_tree_node_cachep, gfp);
408 xas_set_err(xas, -ENOMEM);
414 node->offset = xas->xa_offset;
416 XA_NODE_BUG_ON(node, parent->count > XA_CHUNK_SIZE);
417 xas_update(xas, parent);
419 XA_NODE_BUG_ON(node, shift > BITS_PER_LONG);
420 XA_NODE_BUG_ON(node, !list_empty(&node->private_list));
424 RCU_INIT_POINTER(node->parent, xas->xa_node);
425 node->array = xas->xa;
430 #ifdef CONFIG_XARRAY_MULTI
431 /* Returns the number of indices covered by a given xa_state */
432 static unsigned long xas_size(const struct xa_state *xas)
434 return (xas->xa_sibs + 1UL) << xas->xa_shift;
439 * Use this to calculate the maximum index that will need to be created
440 * in order to add the entry described by @xas. Because we cannot store a
441 * multiple-index entry at index 0, the calculation is a little more complex
442 * than you might expect.
444 static unsigned long xas_max(struct xa_state *xas)
446 unsigned long max = xas->xa_index;
448 #ifdef CONFIG_XARRAY_MULTI
449 if (xas->xa_shift || xas->xa_sibs) {
450 unsigned long mask = xas_size(xas) - 1;
460 /* The maximum index that can be contained in the array without expanding it */
461 static unsigned long max_index(void *entry)
463 if (!xa_is_node(entry))
465 return (XA_CHUNK_SIZE << xa_to_node(entry)->shift) - 1;
468 static void xas_shrink(struct xa_state *xas)
470 struct xarray *xa = xas->xa;
471 struct xa_node *node = xas->xa_node;
476 XA_NODE_BUG_ON(node, node->count > XA_CHUNK_SIZE);
477 if (node->count != 1)
479 entry = xa_entry_locked(xa, node, 0);
482 if (!xa_is_node(entry) && node->shift)
484 if (xa_is_zero(entry) && xa_zero_busy(xa))
486 xas->xa_node = XAS_BOUNDS;
488 RCU_INIT_POINTER(xa->xa_head, entry);
489 if (xa_track_free(xa) && !node_get_mark(node, 0, XA_FREE_MARK))
490 xa_mark_clear(xa, XA_FREE_MARK);
494 if (!xa_is_node(entry))
495 RCU_INIT_POINTER(node->slots[0], XA_RETRY_ENTRY);
496 xas_update(xas, node);
498 if (!xa_is_node(entry))
500 node = xa_to_node(entry);
506 * xas_delete_node() - Attempt to delete an xa_node
507 * @xas: Array operation state.
509 * Attempts to delete the @xas->xa_node. This will fail if xa->node has
510 * a non-zero reference count.
512 static void xas_delete_node(struct xa_state *xas)
514 struct xa_node *node = xas->xa_node;
517 struct xa_node *parent;
519 XA_NODE_BUG_ON(node, node->count > XA_CHUNK_SIZE);
523 parent = xa_parent_locked(xas->xa, node);
524 xas->xa_node = parent;
525 xas->xa_offset = node->offset;
529 xas->xa->xa_head = NULL;
530 xas->xa_node = XAS_BOUNDS;
534 parent->slots[xas->xa_offset] = NULL;
536 XA_NODE_BUG_ON(parent, parent->count > XA_CHUNK_SIZE);
538 xas_update(xas, node);
546 * xas_free_nodes() - Free this node and all nodes that it references
547 * @xas: Array operation state.
550 * This node has been removed from the tree. We must now free it and all
551 * of its subnodes. There may be RCU walkers with references into the tree,
552 * so we must replace all entries with retry markers.
554 static void xas_free_nodes(struct xa_state *xas, struct xa_node *top)
556 unsigned int offset = 0;
557 struct xa_node *node = top;
560 void *entry = xa_entry_locked(xas->xa, node, offset);
562 if (node->shift && xa_is_node(entry)) {
563 node = xa_to_node(entry);
568 RCU_INIT_POINTER(node->slots[offset], XA_RETRY_ENTRY);
570 while (offset == XA_CHUNK_SIZE) {
571 struct xa_node *parent;
573 parent = xa_parent_locked(xas->xa, node);
574 offset = node->offset + 1;
577 xas_update(xas, node);
587 * xas_expand adds nodes to the head of the tree until it has reached
588 * sufficient height to be able to contain @xas->xa_index
590 static int xas_expand(struct xa_state *xas, void *head)
592 struct xarray *xa = xas->xa;
593 struct xa_node *node = NULL;
594 unsigned int shift = 0;
595 unsigned long max = xas_max(xas);
600 while ((max >> shift) >= XA_CHUNK_SIZE)
601 shift += XA_CHUNK_SHIFT;
602 return shift + XA_CHUNK_SHIFT;
603 } else if (xa_is_node(head)) {
604 node = xa_to_node(head);
605 shift = node->shift + XA_CHUNK_SHIFT;
609 while (max > max_index(head)) {
612 XA_NODE_BUG_ON(node, shift > BITS_PER_LONG);
613 node = xas_alloc(xas, shift);
618 if (xa_is_value(head))
620 RCU_INIT_POINTER(node->slots[0], head);
622 /* Propagate the aggregated mark info to the new child */
624 if (xa_track_free(xa) && mark == XA_FREE_MARK) {
625 node_mark_all(node, XA_FREE_MARK);
626 if (!xa_marked(xa, XA_FREE_MARK)) {
627 node_clear_mark(node, 0, XA_FREE_MARK);
628 xa_mark_set(xa, XA_FREE_MARK);
630 } else if (xa_marked(xa, mark)) {
631 node_set_mark(node, 0, mark);
633 if (mark == XA_MARK_MAX)
639 * Now that the new node is fully initialised, we can add
642 if (xa_is_node(head)) {
643 xa_to_node(head)->offset = 0;
644 rcu_assign_pointer(xa_to_node(head)->parent, node);
646 head = xa_mk_node(node);
647 rcu_assign_pointer(xa->xa_head, head);
648 xas_update(xas, node);
650 shift += XA_CHUNK_SHIFT;
658 * xas_create() - Create a slot to store an entry in.
659 * @xas: XArray operation state.
660 * @allow_root: %true if we can store the entry in the root directly
662 * Most users will not need to call this function directly, as it is called
663 * by xas_store(). It is useful for doing conditional store operations
664 * (see the xa_cmpxchg() implementation for an example).
666 * Return: If the slot already existed, returns the contents of this slot.
667 * If the slot was newly created, returns %NULL. If it failed to create the
668 * slot, returns %NULL and indicates the error in @xas.
670 static void *xas_create(struct xa_state *xas, bool allow_root)
672 struct xarray *xa = xas->xa;
675 struct xa_node *node = xas->xa_node;
677 unsigned int order = xas->xa_shift;
680 entry = xa_head_locked(xa);
682 if (!entry && xa_zero_busy(xa))
683 entry = XA_ZERO_ENTRY;
684 shift = xas_expand(xas, entry);
687 if (!shift && !allow_root)
688 shift = XA_CHUNK_SHIFT;
689 entry = xa_head_locked(xa);
691 } else if (xas_error(xas)) {
694 unsigned int offset = xas->xa_offset;
697 entry = xa_entry_locked(xa, node, offset);
698 slot = &node->slots[offset];
701 entry = xa_head_locked(xa);
705 while (shift > order) {
706 shift -= XA_CHUNK_SHIFT;
708 node = xas_alloc(xas, shift);
711 if (xa_track_free(xa))
712 node_mark_all(node, XA_FREE_MARK);
713 rcu_assign_pointer(*slot, xa_mk_node(node));
714 } else if (xa_is_node(entry)) {
715 node = xa_to_node(entry);
719 entry = xas_descend(xas, node);
720 slot = &node->slots[xas->xa_offset];
727 * xas_create_range() - Ensure that stores to this range will succeed
728 * @xas: XArray operation state.
730 * Creates all of the slots in the range covered by @xas. Sets @xas to
731 * create single-index entries and positions it at the beginning of the
732 * range. This is for the benefit of users which have not yet been
733 * converted to use multi-index entries.
735 void xas_create_range(struct xa_state *xas)
737 unsigned long index = xas->xa_index;
738 unsigned char shift = xas->xa_shift;
739 unsigned char sibs = xas->xa_sibs;
741 xas->xa_index |= ((sibs + 1) << shift) - 1;
742 if (xas_is_node(xas) && xas->xa_node->shift == xas->xa_shift)
743 xas->xa_offset |= sibs;
748 xas_create(xas, true);
751 if (xas->xa_index <= (index | XA_CHUNK_MASK))
753 xas->xa_index -= XA_CHUNK_SIZE;
756 struct xa_node *node = xas->xa_node;
757 xas->xa_node = xa_parent_locked(xas->xa, node);
758 xas->xa_offset = node->offset - 1;
759 if (node->offset != 0)
765 xas->xa_shift = shift;
767 xas->xa_index = index;
770 xas->xa_index = index;
774 EXPORT_SYMBOL_GPL(xas_create_range);
776 static void update_node(struct xa_state *xas, struct xa_node *node,
777 int count, int values)
779 if (!node || (!count && !values))
782 node->count += count;
783 node->nr_values += values;
784 XA_NODE_BUG_ON(node, node->count > XA_CHUNK_SIZE);
785 XA_NODE_BUG_ON(node, node->nr_values > XA_CHUNK_SIZE);
786 xas_update(xas, node);
788 xas_delete_node(xas);
792 * xas_store() - Store this entry in the XArray.
793 * @xas: XArray operation state.
796 * If @xas is operating on a multi-index entry, the entry returned by this
797 * function is essentially meaningless (it may be an internal entry or it
798 * may be %NULL, even if there are non-NULL entries at some of the indices
799 * covered by the range). This is not a problem for any current users,
800 * and can be changed if needed.
802 * Return: The old entry at this index.
804 void *xas_store(struct xa_state *xas, void *entry)
806 struct xa_node *node;
807 void __rcu **slot = &xas->xa->xa_head;
808 unsigned int offset, max;
812 bool value = xa_is_value(entry);
815 bool allow_root = !xa_is_node(entry) && !xa_is_zero(entry);
816 first = xas_create(xas, allow_root);
818 first = xas_load(xas);
821 if (xas_invalid(xas))
824 if (node && (xas->xa_shift < node->shift))
826 if ((first == entry) && !xas->xa_sibs)
830 offset = xas->xa_offset;
831 max = xas->xa_offset + xas->xa_sibs;
833 slot = &node->slots[offset];
835 xas_squash_marks(xas);
842 * Must clear the marks before setting the entry to NULL,
843 * otherwise xas_for_each_marked may find a NULL entry and
844 * stop early. rcu_assign_pointer contains a release barrier
845 * so the mark clearing will appear to happen before the
846 * entry is set to NULL.
848 rcu_assign_pointer(*slot, entry);
849 if (xa_is_node(next) && (!node || node->shift))
850 xas_free_nodes(xas, xa_to_node(next));
853 count += !next - !entry;
854 values += !xa_is_value(first) - !value;
858 if (!xa_is_sibling(entry))
859 entry = xa_mk_sibling(xas->xa_offset);
861 if (offset == XA_CHUNK_MASK)
864 next = xa_entry_locked(xas->xa, node, ++offset);
865 if (!xa_is_sibling(next)) {
866 if (!entry && (offset > max))
873 update_node(xas, node, count, values);
876 EXPORT_SYMBOL_GPL(xas_store);
879 * xas_get_mark() - Returns the state of this mark.
880 * @xas: XArray operation state.
881 * @mark: Mark number.
883 * Return: true if the mark is set, false if the mark is clear or @xas
884 * is in an error state.
886 bool xas_get_mark(const struct xa_state *xas, xa_mark_t mark)
888 if (xas_invalid(xas))
891 return xa_marked(xas->xa, mark);
892 return node_get_mark(xas->xa_node, xas->xa_offset, mark);
894 EXPORT_SYMBOL_GPL(xas_get_mark);
897 * xas_set_mark() - Sets the mark on this entry and its parents.
898 * @xas: XArray operation state.
899 * @mark: Mark number.
901 * Sets the specified mark on this entry, and walks up the tree setting it
902 * on all the ancestor entries. Does nothing if @xas has not been walked to
903 * an entry, or is in an error state.
905 void xas_set_mark(const struct xa_state *xas, xa_mark_t mark)
907 struct xa_node *node = xas->xa_node;
908 unsigned int offset = xas->xa_offset;
910 if (xas_invalid(xas))
914 if (node_set_mark(node, offset, mark))
916 offset = node->offset;
917 node = xa_parent_locked(xas->xa, node);
920 if (!xa_marked(xas->xa, mark))
921 xa_mark_set(xas->xa, mark);
923 EXPORT_SYMBOL_GPL(xas_set_mark);
926 * xas_clear_mark() - Clears the mark on this entry and its parents.
927 * @xas: XArray operation state.
928 * @mark: Mark number.
930 * Clears the specified mark on this entry, and walks back to the head
931 * attempting to clear it on all the ancestor entries. Does nothing if
932 * @xas has not been walked to an entry, or is in an error state.
934 void xas_clear_mark(const struct xa_state *xas, xa_mark_t mark)
936 struct xa_node *node = xas->xa_node;
937 unsigned int offset = xas->xa_offset;
939 if (xas_invalid(xas))
943 if (!node_clear_mark(node, offset, mark))
945 if (node_any_mark(node, mark))
948 offset = node->offset;
949 node = xa_parent_locked(xas->xa, node);
952 if (xa_marked(xas->xa, mark))
953 xa_mark_clear(xas->xa, mark);
955 EXPORT_SYMBOL_GPL(xas_clear_mark);
958 * xas_init_marks() - Initialise all marks for the entry
959 * @xas: Array operations state.
961 * Initialise all marks for the entry specified by @xas. If we're tracking
962 * free entries with a mark, we need to set it on all entries. All other
965 * This implementation is not as efficient as it could be; we may walk
966 * up the tree multiple times.
968 void xas_init_marks(const struct xa_state *xas)
973 if (xa_track_free(xas->xa) && mark == XA_FREE_MARK)
974 xas_set_mark(xas, mark);
976 xas_clear_mark(xas, mark);
977 if (mark == XA_MARK_MAX)
982 EXPORT_SYMBOL_GPL(xas_init_marks);
985 * xas_pause() - Pause a walk to drop a lock.
986 * @xas: XArray operation state.
988 * Some users need to pause a walk and drop the lock they're holding in
989 * order to yield to a higher priority thread or carry out an operation
990 * on an entry. Those users should call this function before they drop
991 * the lock. It resets the @xas to be suitable for the next iteration
992 * of the loop after the user has reacquired the lock. If most entries
993 * found during a walk require you to call xas_pause(), the xa_for_each()
994 * iterator may be more appropriate.
996 * Note that xas_pause() only works for forward iteration. If a user needs
997 * to pause a reverse iteration, we will need a xas_pause_rev().
999 void xas_pause(struct xa_state *xas)
1001 struct xa_node *node = xas->xa_node;
1003 if (xas_invalid(xas))
1007 unsigned int offset = xas->xa_offset;
1008 while (++offset < XA_CHUNK_SIZE) {
1009 if (!xa_is_sibling(xa_entry(xas->xa, node, offset)))
1012 xas->xa_index += (offset - xas->xa_offset) << node->shift;
1016 xas->xa_node = XAS_RESTART;
1018 EXPORT_SYMBOL_GPL(xas_pause);
1021 * __xas_prev() - Find the previous entry in the XArray.
1022 * @xas: XArray operation state.
1024 * Helper function for xas_prev() which handles all the complex cases
1027 void *__xas_prev(struct xa_state *xas)
1031 if (!xas_frozen(xas->xa_node))
1034 return set_bounds(xas);
1035 if (xas_not_node(xas->xa_node))
1036 return xas_load(xas);
1038 if (xas->xa_offset != get_offset(xas->xa_index, xas->xa_node))
1041 while (xas->xa_offset == 255) {
1042 xas->xa_offset = xas->xa_node->offset - 1;
1043 xas->xa_node = xa_parent(xas->xa, xas->xa_node);
1045 return set_bounds(xas);
1049 entry = xa_entry(xas->xa, xas->xa_node, xas->xa_offset);
1050 if (!xa_is_node(entry))
1053 xas->xa_node = xa_to_node(entry);
1054 xas_set_offset(xas);
1057 EXPORT_SYMBOL_GPL(__xas_prev);
1060 * __xas_next() - Find the next entry in the XArray.
1061 * @xas: XArray operation state.
1063 * Helper function for xas_next() which handles all the complex cases
1066 void *__xas_next(struct xa_state *xas)
1070 if (!xas_frozen(xas->xa_node))
1073 return set_bounds(xas);
1074 if (xas_not_node(xas->xa_node))
1075 return xas_load(xas);
1077 if (xas->xa_offset != get_offset(xas->xa_index, xas->xa_node))
1080 while (xas->xa_offset == XA_CHUNK_SIZE) {
1081 xas->xa_offset = xas->xa_node->offset + 1;
1082 xas->xa_node = xa_parent(xas->xa, xas->xa_node);
1084 return set_bounds(xas);
1088 entry = xa_entry(xas->xa, xas->xa_node, xas->xa_offset);
1089 if (!xa_is_node(entry))
1092 xas->xa_node = xa_to_node(entry);
1093 xas_set_offset(xas);
1096 EXPORT_SYMBOL_GPL(__xas_next);
1099 * xas_find() - Find the next present entry in the XArray.
1100 * @xas: XArray operation state.
1101 * @max: Highest index to return.
1103 * If the @xas has not yet been walked to an entry, return the entry
1104 * which has an index >= xas.xa_index. If it has been walked, the entry
1105 * currently being pointed at has been processed, and so we move to the
1108 * If no entry is found and the array is smaller than @max, the iterator
1109 * is set to the smallest index not yet in the array. This allows @xas
1110 * to be immediately passed to xas_store().
1112 * Return: The entry, if found, otherwise %NULL.
1114 void *xas_find(struct xa_state *xas, unsigned long max)
1121 if (!xas->xa_node) {
1123 return set_bounds(xas);
1124 } else if (xas_top(xas->xa_node)) {
1125 entry = xas_load(xas);
1126 if (entry || xas_not_node(xas->xa_node))
1128 } else if (!xas->xa_node->shift &&
1129 xas->xa_offset != (xas->xa_index & XA_CHUNK_MASK)) {
1130 xas->xa_offset = ((xas->xa_index - 1) & XA_CHUNK_MASK) + 1;
1135 while (xas->xa_node && (xas->xa_index <= max)) {
1136 if (unlikely(xas->xa_offset == XA_CHUNK_SIZE)) {
1137 xas->xa_offset = xas->xa_node->offset + 1;
1138 xas->xa_node = xa_parent(xas->xa, xas->xa_node);
1142 entry = xa_entry(xas->xa, xas->xa_node, xas->xa_offset);
1143 if (xa_is_node(entry)) {
1144 xas->xa_node = xa_to_node(entry);
1148 if (entry && !xa_is_sibling(entry))
1155 xas->xa_node = XAS_BOUNDS;
1158 EXPORT_SYMBOL_GPL(xas_find);
1161 * xas_find_marked() - Find the next marked entry in the XArray.
1162 * @xas: XArray operation state.
1163 * @max: Highest index to return.
1164 * @mark: Mark number to search for.
1166 * If the @xas has not yet been walked to an entry, return the marked entry
1167 * which has an index >= xas.xa_index. If it has been walked, the entry
1168 * currently being pointed at has been processed, and so we return the
1169 * first marked entry with an index > xas.xa_index.
1171 * If no marked entry is found and the array is smaller than @max, @xas is
1172 * set to the bounds state and xas->xa_index is set to the smallest index
1173 * not yet in the array. This allows @xas to be immediately passed to
1176 * If no entry is found before @max is reached, @xas is set to the restart
1179 * Return: The entry, if found, otherwise %NULL.
1181 void *xas_find_marked(struct xa_state *xas, unsigned long max, xa_mark_t mark)
1183 bool advance = true;
1184 unsigned int offset;
1190 if (!xas->xa_node) {
1193 } else if (xas_top(xas->xa_node)) {
1195 entry = xa_head(xas->xa);
1196 xas->xa_node = NULL;
1197 if (xas->xa_index > max_index(entry))
1199 if (!xa_is_node(entry)) {
1200 if (xa_marked(xas->xa, mark))
1205 xas->xa_node = xa_to_node(entry);
1206 xas->xa_offset = xas->xa_index >> xas->xa_node->shift;
1209 while (xas->xa_index <= max) {
1210 if (unlikely(xas->xa_offset == XA_CHUNK_SIZE)) {
1211 xas->xa_offset = xas->xa_node->offset + 1;
1212 xas->xa_node = xa_parent(xas->xa, xas->xa_node);
1220 entry = xa_entry(xas->xa, xas->xa_node, xas->xa_offset);
1221 if (xa_is_sibling(entry)) {
1222 xas->xa_offset = xa_to_sibling(entry);
1223 xas_move_index(xas, xas->xa_offset);
1227 offset = xas_find_chunk(xas, advance, mark);
1228 if (offset > xas->xa_offset) {
1230 xas_move_index(xas, offset);
1232 if ((xas->xa_index - 1) >= max)
1234 xas->xa_offset = offset;
1235 if (offset == XA_CHUNK_SIZE)
1239 entry = xa_entry(xas->xa, xas->xa_node, xas->xa_offset);
1240 if (!xa_is_node(entry))
1242 xas->xa_node = xa_to_node(entry);
1243 xas_set_offset(xas);
1247 if (xas->xa_index > max)
1249 return set_bounds(xas);
1251 xas->xa_node = XAS_RESTART;
1254 EXPORT_SYMBOL_GPL(xas_find_marked);
1257 * xas_find_conflict() - Find the next present entry in a range.
1258 * @xas: XArray operation state.
1260 * The @xas describes both a range and a position within that range.
1262 * Context: Any context. Expects xa_lock to be held.
1263 * Return: The next entry in the range covered by @xas or %NULL.
1265 void *xas_find_conflict(struct xa_state *xas)
1275 if (xas_top(xas->xa_node)) {
1276 curr = xas_start(xas);
1279 while (xa_is_node(curr)) {
1280 struct xa_node *node = xa_to_node(curr);
1281 curr = xas_descend(xas, node);
1287 if (xas->xa_node->shift > xas->xa_shift)
1291 if (xas->xa_node->shift == xas->xa_shift) {
1292 if ((xas->xa_offset & xas->xa_sibs) == xas->xa_sibs)
1294 } else if (xas->xa_offset == XA_CHUNK_MASK) {
1295 xas->xa_offset = xas->xa_node->offset;
1296 xas->xa_node = xa_parent_locked(xas->xa, xas->xa_node);
1301 curr = xa_entry_locked(xas->xa, xas->xa_node, ++xas->xa_offset);
1302 if (xa_is_sibling(curr))
1304 while (xa_is_node(curr)) {
1305 xas->xa_node = xa_to_node(curr);
1307 curr = xa_entry_locked(xas->xa, xas->xa_node, 0);
1312 xas->xa_offset -= xas->xa_sibs;
1315 EXPORT_SYMBOL_GPL(xas_find_conflict);
1318 * xa_load() - Load an entry from an XArray.
1320 * @index: index into array.
1322 * Context: Any context. Takes and releases the RCU lock.
1323 * Return: The entry at @index in @xa.
1325 void *xa_load(struct xarray *xa, unsigned long index)
1327 XA_STATE(xas, xa, index);
1332 entry = xas_load(&xas);
1333 if (xa_is_zero(entry))
1335 } while (xas_retry(&xas, entry));
1340 EXPORT_SYMBOL(xa_load);
1342 static void *xas_result(struct xa_state *xas, void *curr)
1344 if (xa_is_zero(curr))
1347 curr = xas->xa_node;
1352 * __xa_erase() - Erase this entry from the XArray while locked.
1354 * @index: Index into array.
1356 * After this function returns, loading from @index will return %NULL.
1357 * If the index is part of a multi-index entry, all indices will be erased
1358 * and none of the entries will be part of a multi-index entry.
1360 * Context: Any context. Expects xa_lock to be held on entry.
1361 * Return: The entry which used to be at this index.
1363 void *__xa_erase(struct xarray *xa, unsigned long index)
1365 XA_STATE(xas, xa, index);
1366 return xas_result(&xas, xas_store(&xas, NULL));
1368 EXPORT_SYMBOL(__xa_erase);
1371 * xa_erase() - Erase this entry from the XArray.
1373 * @index: Index of entry.
1375 * After this function returns, loading from @index will return %NULL.
1376 * If the index is part of a multi-index entry, all indices will be erased
1377 * and none of the entries will be part of a multi-index entry.
1379 * Context: Any context. Takes and releases the xa_lock.
1380 * Return: The entry which used to be at this index.
1382 void *xa_erase(struct xarray *xa, unsigned long index)
1387 entry = __xa_erase(xa, index);
1392 EXPORT_SYMBOL(xa_erase);
1395 * __xa_store() - Store this entry in the XArray.
1397 * @index: Index into array.
1398 * @entry: New entry.
1399 * @gfp: Memory allocation flags.
1401 * You must already be holding the xa_lock when calling this function.
1402 * It will drop the lock if needed to allocate memory, and then reacquire
1405 * Context: Any context. Expects xa_lock to be held on entry. May
1406 * release and reacquire xa_lock if @gfp flags permit.
1407 * Return: The old entry at this index or xa_err() if an error happened.
1409 void *__xa_store(struct xarray *xa, unsigned long index, void *entry, gfp_t gfp)
1411 XA_STATE(xas, xa, index);
1414 if (WARN_ON_ONCE(xa_is_advanced(entry)))
1415 return XA_ERROR(-EINVAL);
1416 if (xa_track_free(xa) && !entry)
1417 entry = XA_ZERO_ENTRY;
1420 curr = xas_store(&xas, entry);
1421 if (xa_track_free(xa))
1422 xas_clear_mark(&xas, XA_FREE_MARK);
1423 } while (__xas_nomem(&xas, gfp));
1425 return xas_result(&xas, curr);
1427 EXPORT_SYMBOL(__xa_store);
1430 * xa_store() - Store this entry in the XArray.
1432 * @index: Index into array.
1433 * @entry: New entry.
1434 * @gfp: Memory allocation flags.
1436 * After this function returns, loads from this index will return @entry.
1437 * Storing into an existing multislot entry updates the entry of every index.
1438 * The marks associated with @index are unaffected unless @entry is %NULL.
1440 * Context: Any context. Takes and releases the xa_lock.
1441 * May sleep if the @gfp flags permit.
1442 * Return: The old entry at this index on success, xa_err(-EINVAL) if @entry
1443 * cannot be stored in an XArray, or xa_err(-ENOMEM) if memory allocation
1446 void *xa_store(struct xarray *xa, unsigned long index, void *entry, gfp_t gfp)
1451 curr = __xa_store(xa, index, entry, gfp);
1456 EXPORT_SYMBOL(xa_store);
1459 * __xa_cmpxchg() - Store this entry in the XArray.
1461 * @index: Index into array.
1462 * @old: Old value to test against.
1463 * @entry: New entry.
1464 * @gfp: Memory allocation flags.
1466 * You must already be holding the xa_lock when calling this function.
1467 * It will drop the lock if needed to allocate memory, and then reacquire
1470 * Context: Any context. Expects xa_lock to be held on entry. May
1471 * release and reacquire xa_lock if @gfp flags permit.
1472 * Return: The old entry at this index or xa_err() if an error happened.
1474 void *__xa_cmpxchg(struct xarray *xa, unsigned long index,
1475 void *old, void *entry, gfp_t gfp)
1477 XA_STATE(xas, xa, index);
1480 if (WARN_ON_ONCE(xa_is_advanced(entry)))
1481 return XA_ERROR(-EINVAL);
1484 curr = xas_load(&xas);
1486 xas_store(&xas, entry);
1487 if (xa_track_free(xa) && entry && !curr)
1488 xas_clear_mark(&xas, XA_FREE_MARK);
1490 } while (__xas_nomem(&xas, gfp));
1492 return xas_result(&xas, curr);
1494 EXPORT_SYMBOL(__xa_cmpxchg);
1497 * __xa_insert() - Store this entry in the XArray if no entry is present.
1499 * @index: Index into array.
1500 * @entry: New entry.
1501 * @gfp: Memory allocation flags.
1503 * Inserting a NULL entry will store a reserved entry (like xa_reserve())
1504 * if no entry is present. Inserting will fail if a reserved entry is
1505 * present, even though loading from this index will return NULL.
1507 * Context: Any context. Expects xa_lock to be held on entry. May
1508 * release and reacquire xa_lock if @gfp flags permit.
1509 * Return: 0 if the store succeeded. -EBUSY if another entry was present.
1510 * -ENOMEM if memory could not be allocated.
1512 int __xa_insert(struct xarray *xa, unsigned long index, void *entry, gfp_t gfp)
1514 XA_STATE(xas, xa, index);
1517 if (WARN_ON_ONCE(xa_is_advanced(entry)))
1520 entry = XA_ZERO_ENTRY;
1523 curr = xas_load(&xas);
1525 xas_store(&xas, entry);
1526 if (xa_track_free(xa))
1527 xas_clear_mark(&xas, XA_FREE_MARK);
1529 xas_set_err(&xas, -EBUSY);
1531 } while (__xas_nomem(&xas, gfp));
1533 return xas_error(&xas);
1535 EXPORT_SYMBOL(__xa_insert);
1537 #ifdef CONFIG_XARRAY_MULTI
1538 static void xas_set_range(struct xa_state *xas, unsigned long first,
1541 unsigned int shift = 0;
1542 unsigned long sibs = last - first;
1543 unsigned int offset = XA_CHUNK_MASK;
1545 xas_set(xas, first);
1547 while ((first & XA_CHUNK_MASK) == 0) {
1548 if (sibs < XA_CHUNK_MASK)
1550 if ((sibs == XA_CHUNK_MASK) && (offset < XA_CHUNK_MASK))
1552 shift += XA_CHUNK_SHIFT;
1553 if (offset == XA_CHUNK_MASK)
1554 offset = sibs & XA_CHUNK_MASK;
1555 sibs >>= XA_CHUNK_SHIFT;
1556 first >>= XA_CHUNK_SHIFT;
1559 offset = first & XA_CHUNK_MASK;
1560 if (offset + sibs > XA_CHUNK_MASK)
1561 sibs = XA_CHUNK_MASK - offset;
1562 if ((((first + sibs + 1) << shift) - 1) > last)
1565 xas->xa_shift = shift;
1566 xas->xa_sibs = sibs;
1570 * xa_store_range() - Store this entry at a range of indices in the XArray.
1572 * @first: First index to affect.
1573 * @last: Last index to affect.
1574 * @entry: New entry.
1575 * @gfp: Memory allocation flags.
1577 * After this function returns, loads from any index between @first and @last,
1578 * inclusive will return @entry.
1579 * Storing into an existing multislot entry updates the entry of every index.
1580 * The marks associated with @index are unaffected unless @entry is %NULL.
1582 * Context: Process context. Takes and releases the xa_lock. May sleep
1583 * if the @gfp flags permit.
1584 * Return: %NULL on success, xa_err(-EINVAL) if @entry cannot be stored in
1585 * an XArray, or xa_err(-ENOMEM) if memory allocation failed.
1587 void *xa_store_range(struct xarray *xa, unsigned long first,
1588 unsigned long last, void *entry, gfp_t gfp)
1590 XA_STATE(xas, xa, 0);
1592 if (WARN_ON_ONCE(xa_is_internal(entry)))
1593 return XA_ERROR(-EINVAL);
1595 return XA_ERROR(-EINVAL);
1600 unsigned int order = BITS_PER_LONG;
1602 order = __ffs(last + 1);
1603 xas_set_order(&xas, last, order);
1604 xas_create(&xas, true);
1605 if (xas_error(&xas))
1609 xas_set_range(&xas, first, last);
1610 xas_store(&xas, entry);
1611 if (xas_error(&xas))
1613 first += xas_size(&xas);
1614 } while (first <= last);
1617 } while (xas_nomem(&xas, gfp));
1619 return xas_result(&xas, NULL);
1621 EXPORT_SYMBOL(xa_store_range);
1622 #endif /* CONFIG_XARRAY_MULTI */
1625 * __xa_alloc() - Find somewhere to store this entry in the XArray.
1627 * @id: Pointer to ID.
1628 * @limit: Range for allocated ID.
1629 * @entry: New entry.
1630 * @gfp: Memory allocation flags.
1632 * Finds an empty entry in @xa between @limit.min and @limit.max,
1633 * stores the index into the @id pointer, then stores the entry at
1634 * that index. A concurrent lookup will not see an uninitialised @id.
1636 * Context: Any context. Expects xa_lock to be held on entry. May
1637 * release and reacquire xa_lock if @gfp flags permit.
1638 * Return: 0 on success, -ENOMEM if memory could not be allocated or
1639 * -EBUSY if there are no free entries in @limit.
1641 int __xa_alloc(struct xarray *xa, u32 *id, void *entry,
1642 struct xa_limit limit, gfp_t gfp)
1644 XA_STATE(xas, xa, 0);
1646 if (WARN_ON_ONCE(xa_is_advanced(entry)))
1648 if (WARN_ON_ONCE(!xa_track_free(xa)))
1652 entry = XA_ZERO_ENTRY;
1655 xas.xa_index = limit.min;
1656 xas_find_marked(&xas, limit.max, XA_FREE_MARK);
1657 if (xas.xa_node == XAS_RESTART)
1658 xas_set_err(&xas, -EBUSY);
1661 xas_store(&xas, entry);
1662 xas_clear_mark(&xas, XA_FREE_MARK);
1663 } while (__xas_nomem(&xas, gfp));
1665 return xas_error(&xas);
1667 EXPORT_SYMBOL(__xa_alloc);
1670 * __xa_alloc_cyclic() - Find somewhere to store this entry in the XArray.
1672 * @id: Pointer to ID.
1673 * @entry: New entry.
1674 * @limit: Range of allocated ID.
1675 * @next: Pointer to next ID to allocate.
1676 * @gfp: Memory allocation flags.
1678 * Finds an empty entry in @xa between @limit.min and @limit.max,
1679 * stores the index into the @id pointer, then stores the entry at
1680 * that index. A concurrent lookup will not see an uninitialised @id.
1681 * The search for an empty entry will start at @next and will wrap
1682 * around if necessary.
1684 * Context: Any context. Expects xa_lock to be held on entry. May
1685 * release and reacquire xa_lock if @gfp flags permit.
1686 * Return: 0 if the allocation succeeded without wrapping. 1 if the
1687 * allocation succeeded after wrapping, -ENOMEM if memory could not be
1688 * allocated or -EBUSY if there are no free entries in @limit.
1690 int __xa_alloc_cyclic(struct xarray *xa, u32 *id, void *entry,
1691 struct xa_limit limit, u32 *next, gfp_t gfp)
1693 u32 min = limit.min;
1696 limit.min = max(min, *next);
1697 ret = __xa_alloc(xa, id, entry, limit, gfp);
1698 if ((xa->xa_flags & XA_FLAGS_ALLOC_WRAPPED) && ret == 0) {
1699 xa->xa_flags &= ~XA_FLAGS_ALLOC_WRAPPED;
1703 if (ret < 0 && limit.min > min) {
1705 ret = __xa_alloc(xa, id, entry, limit, gfp);
1713 xa->xa_flags |= XA_FLAGS_ALLOC_WRAPPED;
1717 EXPORT_SYMBOL(__xa_alloc_cyclic);
1720 * __xa_set_mark() - Set this mark on this entry while locked.
1722 * @index: Index of entry.
1723 * @mark: Mark number.
1725 * Attempting to set a mark on a %NULL entry does not succeed.
1727 * Context: Any context. Expects xa_lock to be held on entry.
1729 void __xa_set_mark(struct xarray *xa, unsigned long index, xa_mark_t mark)
1731 XA_STATE(xas, xa, index);
1732 void *entry = xas_load(&xas);
1735 xas_set_mark(&xas, mark);
1737 EXPORT_SYMBOL(__xa_set_mark);
1740 * __xa_clear_mark() - Clear this mark on this entry while locked.
1742 * @index: Index of entry.
1743 * @mark: Mark number.
1745 * Context: Any context. Expects xa_lock to be held on entry.
1747 void __xa_clear_mark(struct xarray *xa, unsigned long index, xa_mark_t mark)
1749 XA_STATE(xas, xa, index);
1750 void *entry = xas_load(&xas);
1753 xas_clear_mark(&xas, mark);
1755 EXPORT_SYMBOL(__xa_clear_mark);
1758 * xa_get_mark() - Inquire whether this mark is set on this entry.
1760 * @index: Index of entry.
1761 * @mark: Mark number.
1763 * This function uses the RCU read lock, so the result may be out of date
1764 * by the time it returns. If you need the result to be stable, use a lock.
1766 * Context: Any context. Takes and releases the RCU lock.
1767 * Return: True if the entry at @index has this mark set, false if it doesn't.
1769 bool xa_get_mark(struct xarray *xa, unsigned long index, xa_mark_t mark)
1771 XA_STATE(xas, xa, index);
1775 entry = xas_start(&xas);
1776 while (xas_get_mark(&xas, mark)) {
1777 if (!xa_is_node(entry))
1779 entry = xas_descend(&xas, xa_to_node(entry));
1787 EXPORT_SYMBOL(xa_get_mark);
1790 * xa_set_mark() - Set this mark on this entry.
1792 * @index: Index of entry.
1793 * @mark: Mark number.
1795 * Attempting to set a mark on a %NULL entry does not succeed.
1797 * Context: Process context. Takes and releases the xa_lock.
1799 void xa_set_mark(struct xarray *xa, unsigned long index, xa_mark_t mark)
1802 __xa_set_mark(xa, index, mark);
1805 EXPORT_SYMBOL(xa_set_mark);
1808 * xa_clear_mark() - Clear this mark on this entry.
1810 * @index: Index of entry.
1811 * @mark: Mark number.
1813 * Clearing a mark always succeeds.
1815 * Context: Process context. Takes and releases the xa_lock.
1817 void xa_clear_mark(struct xarray *xa, unsigned long index, xa_mark_t mark)
1820 __xa_clear_mark(xa, index, mark);
1823 EXPORT_SYMBOL(xa_clear_mark);
1826 * xa_find() - Search the XArray for an entry.
1828 * @indexp: Pointer to an index.
1829 * @max: Maximum index to search to.
1830 * @filter: Selection criterion.
1832 * Finds the entry in @xa which matches the @filter, and has the lowest
1833 * index that is at least @indexp and no more than @max.
1834 * If an entry is found, @indexp is updated to be the index of the entry.
1835 * This function is protected by the RCU read lock, so it may not find
1836 * entries which are being simultaneously added. It will not return an
1837 * %XA_RETRY_ENTRY; if you need to see retry entries, use xas_find().
1839 * Context: Any context. Takes and releases the RCU lock.
1840 * Return: The entry, if found, otherwise %NULL.
1842 void *xa_find(struct xarray *xa, unsigned long *indexp,
1843 unsigned long max, xa_mark_t filter)
1845 XA_STATE(xas, xa, *indexp);
1850 if ((__force unsigned int)filter < XA_MAX_MARKS)
1851 entry = xas_find_marked(&xas, max, filter);
1853 entry = xas_find(&xas, max);
1854 } while (xas_retry(&xas, entry));
1858 *indexp = xas.xa_index;
1861 EXPORT_SYMBOL(xa_find);
1864 * xa_find_after() - Search the XArray for a present entry.
1866 * @indexp: Pointer to an index.
1867 * @max: Maximum index to search to.
1868 * @filter: Selection criterion.
1870 * Finds the entry in @xa which matches the @filter and has the lowest
1871 * index that is above @indexp and no more than @max.
1872 * If an entry is found, @indexp is updated to be the index of the entry.
1873 * This function is protected by the RCU read lock, so it may miss entries
1874 * which are being simultaneously added. It will not return an
1875 * %XA_RETRY_ENTRY; if you need to see retry entries, use xas_find().
1877 * Context: Any context. Takes and releases the RCU lock.
1878 * Return: The pointer, if found, otherwise %NULL.
1880 void *xa_find_after(struct xarray *xa, unsigned long *indexp,
1881 unsigned long max, xa_mark_t filter)
1883 XA_STATE(xas, xa, *indexp + 1);
1888 if ((__force unsigned int)filter < XA_MAX_MARKS)
1889 entry = xas_find_marked(&xas, max, filter);
1891 entry = xas_find(&xas, max);
1892 if (xas.xa_node == XAS_BOUNDS)
1895 if (xas.xa_index & ((1UL << xas.xa_shift) - 1))
1898 if (xas.xa_offset < (xas.xa_index & XA_CHUNK_MASK))
1901 if (!xas_retry(&xas, entry))
1907 *indexp = xas.xa_index;
1910 EXPORT_SYMBOL(xa_find_after);
1912 static unsigned int xas_extract_present(struct xa_state *xas, void **dst,
1913 unsigned long max, unsigned int n)
1919 xas_for_each(xas, entry, max) {
1920 if (xas_retry(xas, entry))
1931 static unsigned int xas_extract_marked(struct xa_state *xas, void **dst,
1932 unsigned long max, unsigned int n, xa_mark_t mark)
1938 xas_for_each_marked(xas, entry, max, mark) {
1939 if (xas_retry(xas, entry))
1951 * xa_extract() - Copy selected entries from the XArray into a normal array.
1952 * @xa: The source XArray to copy from.
1953 * @dst: The buffer to copy entries into.
1954 * @start: The first index in the XArray eligible to be selected.
1955 * @max: The last index in the XArray eligible to be selected.
1956 * @n: The maximum number of entries to copy.
1957 * @filter: Selection criterion.
1959 * Copies up to @n entries that match @filter from the XArray. The
1960 * copied entries will have indices between @start and @max, inclusive.
1962 * The @filter may be an XArray mark value, in which case entries which are
1963 * marked with that mark will be copied. It may also be %XA_PRESENT, in
1964 * which case all entries which are not %NULL will be copied.
1966 * The entries returned may not represent a snapshot of the XArray at a
1967 * moment in time. For example, if another thread stores to index 5, then
1968 * index 10, calling xa_extract() may return the old contents of index 5
1969 * and the new contents of index 10. Indices not modified while this
1970 * function is running will not be skipped.
1972 * If you need stronger guarantees, holding the xa_lock across calls to this
1973 * function will prevent concurrent modification.
1975 * Context: Any context. Takes and releases the RCU lock.
1976 * Return: The number of entries copied.
1978 unsigned int xa_extract(struct xarray *xa, void **dst, unsigned long start,
1979 unsigned long max, unsigned int n, xa_mark_t filter)
1981 XA_STATE(xas, xa, start);
1986 if ((__force unsigned int)filter < XA_MAX_MARKS)
1987 return xas_extract_marked(&xas, dst, max, n, filter);
1988 return xas_extract_present(&xas, dst, max, n);
1990 EXPORT_SYMBOL(xa_extract);
1993 * xa_destroy() - Free all internal data structures.
1996 * After calling this function, the XArray is empty and has freed all memory
1997 * allocated for its internal data structures. You are responsible for
1998 * freeing the objects referenced by the XArray.
2000 * Context: Any context. Takes and releases the xa_lock, interrupt-safe.
2002 void xa_destroy(struct xarray *xa)
2004 XA_STATE(xas, xa, 0);
2005 unsigned long flags;
2009 xas_lock_irqsave(&xas, flags);
2010 entry = xa_head_locked(xa);
2011 RCU_INIT_POINTER(xa->xa_head, NULL);
2012 xas_init_marks(&xas);
2013 if (xa_zero_busy(xa))
2014 xa_mark_clear(xa, XA_FREE_MARK);
2015 /* lockdep checks we're still holding the lock in xas_free_nodes() */
2016 if (xa_is_node(entry))
2017 xas_free_nodes(&xas, xa_to_node(entry));
2018 xas_unlock_irqrestore(&xas, flags);
2020 EXPORT_SYMBOL(xa_destroy);
2023 void xa_dump_node(const struct xa_node *node)
2029 if ((unsigned long)node & 3) {
2030 pr_cont("node %px\n", node);
2034 pr_cont("node %px %s %d parent %px shift %d count %d values %d "
2035 "array %px list %px %px marks",
2036 node, node->parent ? "offset" : "max", node->offset,
2037 node->parent, node->shift, node->count, node->nr_values,
2038 node->array, node->private_list.prev, node->private_list.next);
2039 for (i = 0; i < XA_MAX_MARKS; i++)
2040 for (j = 0; j < XA_MARK_LONGS; j++)
2041 pr_cont(" %lx", node->marks[i][j]);
2045 void xa_dump_index(unsigned long index, unsigned int shift)
2048 pr_info("%lu: ", index);
2049 else if (shift >= BITS_PER_LONG)
2050 pr_info("0-%lu: ", ~0UL);
2052 pr_info("%lu-%lu: ", index, index | ((1UL << shift) - 1));
2055 void xa_dump_entry(const void *entry, unsigned long index, unsigned long shift)
2060 xa_dump_index(index, shift);
2062 if (xa_is_node(entry)) {
2064 pr_cont("%px\n", entry);
2067 struct xa_node *node = xa_to_node(entry);
2069 for (i = 0; i < XA_CHUNK_SIZE; i++)
2070 xa_dump_entry(node->slots[i],
2071 index + (i << node->shift), node->shift);
2073 } else if (xa_is_value(entry))
2074 pr_cont("value %ld (0x%lx) [%px]\n", xa_to_value(entry),
2075 xa_to_value(entry), entry);
2076 else if (!xa_is_internal(entry))
2077 pr_cont("%px\n", entry);
2078 else if (xa_is_retry(entry))
2079 pr_cont("retry (%ld)\n", xa_to_internal(entry));
2080 else if (xa_is_sibling(entry))
2081 pr_cont("sibling (slot %ld)\n", xa_to_sibling(entry));
2082 else if (xa_is_zero(entry))
2083 pr_cont("zero (%ld)\n", xa_to_internal(entry));
2085 pr_cont("UNKNOWN ENTRY (%px)\n", entry);
2088 void xa_dump(const struct xarray *xa)
2090 void *entry = xa->xa_head;
2091 unsigned int shift = 0;
2093 pr_info("xarray: %px head %px flags %x marks %d %d %d\n", xa, entry,
2094 xa->xa_flags, xa_marked(xa, XA_MARK_0),
2095 xa_marked(xa, XA_MARK_1), xa_marked(xa, XA_MARK_2));
2096 if (xa_is_node(entry))
2097 shift = xa_to_node(entry)->shift + XA_CHUNK_SHIFT;
2098 xa_dump_entry(entry, 0, shift);
2101 #endif /* !HAVE_XARRAY_SUPPORT */