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 <libcfs/linux/xarray.h>
21 * Coding conventions in this file:
23 * @xa is used to refer to the entire xarray.
24 * @xas is the 'xarray operation state'. It may be either a pointer to
25 * an xa_state, or an xa_state stored on the stack. This is an unfortunate
27 * @index is the index of the entry being operated on
28 * @mark is an xa_mark_t; a small number indicating one of the mark bits.
29 * @node refers to an xa_node; usually the primary one being operated on by
31 * @offset is the index into the slots array inside an xa_node.
32 * @parent refers to the @xa_node closer to the head than @node.
33 * @entry refers to something stored in a slot in the xarray
36 static inline unsigned int xa_lock_type(const struct xarray *xa)
38 return (__force unsigned int)xa->xa_flags & 3;
41 static inline void xas_lock_type(struct xa_state *xas, unsigned int lock_type)
43 if (lock_type == XA_LOCK_IRQ)
45 else if (lock_type == XA_LOCK_BH)
51 static inline void xas_unlock_type(struct xa_state *xas, unsigned int lock_type)
53 if (lock_type == XA_LOCK_IRQ)
55 else if (lock_type == XA_LOCK_BH)
61 static inline bool xa_track_free(const struct xarray *xa)
63 return xa->xa_flags & XA_FLAGS_TRACK_FREE;
66 static inline bool xa_zero_busy(const struct xarray *xa)
68 return xa->xa_flags & XA_FLAGS_ZERO_BUSY;
71 static inline void xa_mark_set(struct xarray *xa, xa_mark_t mark)
73 if (!(xa->xa_flags & XA_FLAGS_MARK(mark)))
74 xa->xa_flags |= XA_FLAGS_MARK(mark);
77 static inline void xa_mark_clear(struct xarray *xa, xa_mark_t mark)
79 if (xa->xa_flags & XA_FLAGS_MARK(mark))
80 xa->xa_flags &= ~(XA_FLAGS_MARK(mark));
83 static inline unsigned long *node_marks(struct xa_node *node, xa_mark_t mark)
85 return node->marks[(__force unsigned)mark];
88 static inline bool node_get_mark(struct xa_node *node,
89 unsigned int offset, xa_mark_t mark)
91 return test_bit(offset, node_marks(node, mark));
94 /* returns true if the bit was set */
95 static inline bool node_set_mark(struct xa_node *node, unsigned int offset,
98 return __test_and_set_bit(offset, node_marks(node, mark));
101 /* returns true if the bit was set */
102 static inline bool node_clear_mark(struct xa_node *node, unsigned int offset,
105 return __test_and_clear_bit(offset, node_marks(node, mark));
108 static inline bool node_any_mark(struct xa_node *node, xa_mark_t mark)
110 return !bitmap_empty(node_marks(node, mark), XA_CHUNK_SIZE);
113 static inline void node_mark_all(struct xa_node *node, xa_mark_t mark)
115 bitmap_fill(node_marks(node, mark), XA_CHUNK_SIZE);
118 #define mark_inc(mark) do { \
119 mark = (__force xa_mark_t)((__force unsigned)(mark) + 1); \
123 * xas_squash_marks() - Merge all marks to the first entry
124 * @xas: Array operation state.
126 * Set a mark on the first entry if any entry has it set. Clear marks on
127 * all sibling entries.
129 static void xas_squash_marks(const struct xa_state *xas)
131 unsigned int mark = 0;
132 unsigned int limit = xas->xa_offset + xas->xa_sibs + 1;
138 unsigned long *marks = xas->xa_node->marks[mark];
139 if (find_next_bit(marks, limit, xas->xa_offset + 1) == limit)
141 __set_bit(xas->xa_offset, marks);
142 bitmap_clear(marks, xas->xa_offset + 1, xas->xa_sibs);
143 } while (mark++ != (__force unsigned)XA_MARK_MAX);
146 /* extracts the offset within this node from the index */
147 static unsigned int get_offset(unsigned long index, struct xa_node *node)
149 return (index >> node->shift) & XA_CHUNK_MASK;
152 static void xas_set_offset(struct xa_state *xas)
154 xas->xa_offset = get_offset(xas->xa_index, xas->xa_node);
157 /* move the index either forwards (find) or backwards (sibling slot) */
158 static void xas_move_index(struct xa_state *xas, unsigned long offset)
160 unsigned int shift = xas->xa_node->shift;
161 xas->xa_index &= ~XA_CHUNK_MASK << shift;
162 xas->xa_index += offset << shift;
165 static void xas_advance(struct xa_state *xas)
168 xas_move_index(xas, xas->xa_offset);
171 static void *set_bounds(struct xa_state *xas)
173 xas->xa_node = XAS_BOUNDS;
178 * Starts a walk. If the @xas is already valid, we assume that it's on
179 * the right path and just return where we've got to. If we're in an
180 * error state, return NULL. If the index is outside the current scope
181 * of the xarray, return NULL without changing @xas->xa_node. Otherwise
182 * set @xas->xa_node to NULL and return the current head of the array.
184 static void *xas_start(struct xa_state *xas)
189 return xas_reload(xas);
193 entry = xa_head(xas->xa);
194 if (!xa_is_node(entry)) {
196 return set_bounds(xas);
198 if ((xas->xa_index >> xa_to_node(entry)->shift) > XA_CHUNK_MASK)
199 return set_bounds(xas);
206 static void *xas_descend(struct xa_state *xas, struct xa_node *node)
208 unsigned int offset = get_offset(xas->xa_index, node);
209 void *entry = xa_entry(xas->xa, node, offset);
212 if (xa_is_sibling(entry)) {
213 offset = xa_to_sibling(entry);
214 entry = xa_entry(xas->xa, node, offset);
217 xas->xa_offset = offset;
222 * xas_load() - Load an entry from the XArray (advanced).
223 * @xas: XArray operation state.
225 * Usually walks the @xas to the appropriate state to load the entry
226 * stored at xa_index. However, it will do nothing and return %NULL if
227 * @xas is in an error state. xas_load() will never expand the tree.
229 * If the xa_state is set up to operate on a multi-index entry, xas_load()
230 * may return %NULL or an internal entry, even if there are entries
231 * present within the range specified by @xas.
233 * Context: Any context. The caller should hold the xa_lock or the RCU lock.
234 * Return: Usually an entry in the XArray, but see description for exceptions.
236 void *xas_load(struct xa_state *xas)
238 void *entry = xas_start(xas);
240 while (xa_is_node(entry)) {
241 struct xa_node *node = xa_to_node(entry);
243 if (xas->xa_shift > node->shift)
245 entry = xas_descend(xas, node);
246 if (node->shift == 0)
251 EXPORT_SYMBOL_GPL(xas_load);
253 /* Move the radix tree node cache here */
254 extern struct kmem_cache *xarray_cachep;
256 static inline void tag_clear(struct xa_node *node, unsigned int tag,
259 __clear_bit(offset, node->tags[tag]);
262 static void xarray_node_rcu_free(struct rcu_head *head)
264 struct xa_node *node =
265 container_of(head, struct xa_node, rcu_head);
269 * must only free zeroed nodes into the slab. radix_tree_shrink
270 * can leave us with a non-NULL entry in the first slot, so clear
271 * that here to make sure.
273 for (i = 0; i < XA_MAX_MARKS; i++)
274 tag_clear(node, i, 0);
276 node->slots[0] = NULL;
279 kmem_cache_free(xarray_cachep, node);
282 #define XA_RCU_FREE ((struct xarray *)1)
284 static void xa_node_free(struct xa_node *node)
286 XA_NODE_BUG_ON(node, !list_empty(&node->private_list));
287 node->array = XA_RCU_FREE;
288 call_rcu(&node->rcu_head, xarray_node_rcu_free);
292 * xas_destroy() - Free any resources allocated during the XArray operation.
293 * @xas: XArray operation state.
295 * This function is now internal-only.
297 static void xas_destroy(struct xa_state *xas)
299 struct xa_node *node = xas->xa_alloc;
303 XA_NODE_BUG_ON(node, !list_empty(&node->private_list));
304 kmem_cache_free(xarray_cachep, node);
305 xas->xa_alloc = NULL;
309 * xas_nomem() - Allocate memory if needed.
310 * @xas: XArray operation state.
311 * @gfp: Memory allocation flags.
313 * If we need to add new nodes to the XArray, we try to allocate memory
314 * with GFP_NOWAIT while holding the lock, which will usually succeed.
315 * If it fails, @xas is flagged as needing memory to continue. The caller
316 * should drop the lock and call xas_nomem(). If xas_nomem() succeeds,
317 * the caller should retry the operation.
319 * Forward progress is guaranteed as one node is allocated here and
320 * stored in the xa_state where it will be found by xas_alloc(). More
321 * nodes will likely be found in the slab allocator, but we do not tie
324 * Return: true if memory was needed, and was successfully allocated.
326 bool xas_nomem(struct xa_state *xas, gfp_t gfp)
328 if (xas->xa_node != XA_ERROR(-ENOMEM)) {
333 if (xas->xa->xa_flags & XA_FLAGS_ACCOUNT)
334 gfp |= __GFP_ACCOUNT;
336 xas->xa_alloc = kmem_cache_alloc(xarray_cachep, gfp);
339 XA_NODE_BUG_ON(xas->xa_alloc, !list_empty(&xas->xa_alloc->private_list));
340 xas->xa_node = XAS_RESTART;
343 EXPORT_SYMBOL_GPL(xas_nomem);
346 * __xas_nomem() - Drop locks and allocate memory if needed.
347 * @xas: XArray operation state.
348 * @gfp: Memory allocation flags.
350 * Internal variant of xas_nomem().
352 * Return: true if memory was needed, and was successfully allocated.
354 static bool __xas_nomem(struct xa_state *xas, gfp_t gfp)
355 __must_hold(xas->xa->xa_lock)
357 unsigned int lock_type = xa_lock_type(xas->xa);
359 if (xas->xa_node != XA_ERROR(-ENOMEM)) {
364 if (xas->xa->xa_flags & XA_FLAGS_ACCOUNT)
365 gfp |= __GFP_ACCOUNT;
367 if (gfpflags_allow_blocking(gfp)) {
368 xas_unlock_type(xas, lock_type);
369 xas->xa_alloc = kmem_cache_alloc(xarray_cachep, gfp);
370 xas_lock_type(xas, lock_type);
372 xas->xa_alloc = kmem_cache_alloc(xarray_cachep, gfp);
376 XA_NODE_BUG_ON(xas->xa_alloc, !list_empty(&xas->xa_alloc->private_list));
377 xas->xa_node = XAS_RESTART;
381 static void xas_update(struct xa_state *xas, struct xa_node *node)
384 xas->xa_update(node);
386 XA_NODE_BUG_ON(node, !list_empty(&node->private_list));
389 static void *xas_alloc(struct xa_state *xas, unsigned int shift)
391 struct xa_node *parent = xas->xa_node;
392 struct xa_node *node = xas->xa_alloc;
394 if (xas_invalid(xas))
398 xas->xa_alloc = NULL;
400 gfp_t gfp = GFP_NOWAIT | __GFP_NOWARN;
402 if (xas->xa->xa_flags & XA_FLAGS_ACCOUNT)
403 gfp |= __GFP_ACCOUNT;
405 node = kmem_cache_alloc(xarray_cachep, gfp);
407 xas_set_err(xas, -ENOMEM);
413 node->offset = xas->xa_offset;
415 XA_NODE_BUG_ON(node, parent->count > XA_CHUNK_SIZE);
416 xas_update(xas, parent);
418 XA_NODE_BUG_ON(node, shift > BITS_PER_LONG);
419 XA_NODE_BUG_ON(node, !list_empty(&node->private_list));
423 RCU_INIT_POINTER(node->parent, xas->xa_node);
424 node->array = xas->xa;
429 #ifdef CONFIG_XARRAY_MULTI
430 /* Returns the number of indices covered by a given xa_state */
431 static unsigned long xas_size(const struct xa_state *xas)
433 return (xas->xa_sibs + 1UL) << xas->xa_shift;
438 * Use this to calculate the maximum index that will need to be created
439 * in order to add the entry described by @xas. Because we cannot store a
440 * multiple-index entry at index 0, the calculation is a little more complex
441 * than you might expect.
443 static unsigned long xas_max(struct xa_state *xas)
445 unsigned long max = xas->xa_index;
447 #ifdef CONFIG_XARRAY_MULTI
448 if (xas->xa_shift || xas->xa_sibs) {
449 unsigned long mask = xas_size(xas) - 1;
459 /* The maximum index that can be contained in the array without expanding it */
460 static unsigned long max_index(void *entry)
462 if (!xa_is_node(entry))
464 return (XA_CHUNK_SIZE << xa_to_node(entry)->shift) - 1;
467 static void xas_shrink(struct xa_state *xas)
469 struct xarray *xa = xas->xa;
470 struct xa_node *node = xas->xa_node;
475 XA_NODE_BUG_ON(node, node->count > XA_CHUNK_SIZE);
476 if (node->count != 1)
478 entry = xa_entry_locked(xa, node, 0);
481 if (!xa_is_node(entry) && node->shift)
483 if (xa_is_zero(entry) && xa_zero_busy(xa))
485 xas->xa_node = XAS_BOUNDS;
487 RCU_INIT_POINTER(xa->xa_head, entry);
488 if (xa_track_free(xa) && !node_get_mark(node, 0, XA_FREE_MARK))
489 xa_mark_clear(xa, XA_FREE_MARK);
493 if (!xa_is_node(entry))
494 RCU_INIT_POINTER(node->slots[0], XA_RETRY_ENTRY);
495 xas_update(xas, node);
497 if (!xa_is_node(entry))
499 node = xa_to_node(entry);
505 * xas_delete_node() - Attempt to delete an xa_node
506 * @xas: Array operation state.
508 * Attempts to delete the @xas->xa_node. This will fail if xa->node has
509 * a non-zero reference count.
511 static void xas_delete_node(struct xa_state *xas)
513 struct xa_node *node = xas->xa_node;
516 struct xa_node *parent;
518 XA_NODE_BUG_ON(node, node->count > XA_CHUNK_SIZE);
522 parent = xa_parent_locked(xas->xa, node);
523 xas->xa_node = parent;
524 xas->xa_offset = node->offset;
528 xas->xa->xa_head = NULL;
529 xas->xa_node = XAS_BOUNDS;
533 parent->slots[xas->xa_offset] = NULL;
535 XA_NODE_BUG_ON(parent, parent->count > XA_CHUNK_SIZE);
537 xas_update(xas, node);
545 * xas_free_nodes() - Free this node and all nodes that it references
546 * @xas: Array operation state.
549 * This node has been removed from the tree. We must now free it and all
550 * of its subnodes. There may be RCU walkers with references into the tree,
551 * so we must replace all entries with retry markers.
553 static void xas_free_nodes(struct xa_state *xas, struct xa_node *top)
555 unsigned int offset = 0;
556 struct xa_node *node = top;
559 void *entry = xa_entry_locked(xas->xa, node, offset);
561 if (node->shift && xa_is_node(entry)) {
562 node = xa_to_node(entry);
567 RCU_INIT_POINTER(node->slots[offset], XA_RETRY_ENTRY);
569 while (offset == XA_CHUNK_SIZE) {
570 struct xa_node *parent;
572 parent = xa_parent_locked(xas->xa, node);
573 offset = node->offset + 1;
576 xas_update(xas, node);
586 * xas_expand adds nodes to the head of the tree until it has reached
587 * sufficient height to be able to contain @xas->xa_index
589 static int xas_expand(struct xa_state *xas, void *head)
591 struct xarray *xa = xas->xa;
592 struct xa_node *node = NULL;
593 unsigned int shift = 0;
594 unsigned long max = xas_max(xas);
599 while ((max >> shift) >= XA_CHUNK_SIZE)
600 shift += XA_CHUNK_SHIFT;
601 return shift + XA_CHUNK_SHIFT;
602 } else if (xa_is_node(head)) {
603 node = xa_to_node(head);
604 shift = node->shift + XA_CHUNK_SHIFT;
608 while (max > max_index(head)) {
611 XA_NODE_BUG_ON(node, shift > BITS_PER_LONG);
612 node = xas_alloc(xas, shift);
617 if (xa_is_value(head))
619 RCU_INIT_POINTER(node->slots[0], head);
621 /* Propagate the aggregated mark info to the new child */
623 if (xa_track_free(xa) && mark == XA_FREE_MARK) {
624 node_mark_all(node, XA_FREE_MARK);
625 if (!xa_marked(xa, XA_FREE_MARK)) {
626 node_clear_mark(node, 0, XA_FREE_MARK);
627 xa_mark_set(xa, XA_FREE_MARK);
629 } else if (xa_marked(xa, mark)) {
630 node_set_mark(node, 0, mark);
632 if (mark == XA_MARK_MAX)
638 * Now that the new node is fully initialised, we can add
641 if (xa_is_node(head)) {
642 xa_to_node(head)->offset = 0;
643 rcu_assign_pointer(xa_to_node(head)->parent, node);
645 head = xa_mk_node(node);
646 rcu_assign_pointer(xa->xa_head, head);
647 xas_update(xas, node);
649 shift += XA_CHUNK_SHIFT;
657 * xas_create() - Create a slot to store an entry in.
658 * @xas: XArray operation state.
659 * @allow_root: %true if we can store the entry in the root directly
661 * Most users will not need to call this function directly, as it is called
662 * by xas_store(). It is useful for doing conditional store operations
663 * (see the xa_cmpxchg() implementation for an example).
665 * Return: If the slot already existed, returns the contents of this slot.
666 * If the slot was newly created, returns %NULL. If it failed to create the
667 * slot, returns %NULL and indicates the error in @xas.
669 static void *xas_create(struct xa_state *xas, bool allow_root)
671 struct xarray *xa = xas->xa;
674 struct xa_node *node = xas->xa_node;
676 unsigned int order = xas->xa_shift;
679 entry = xa_head_locked(xa);
681 if (!entry && xa_zero_busy(xa))
682 entry = XA_ZERO_ENTRY;
683 shift = xas_expand(xas, entry);
686 if (!shift && !allow_root)
687 shift = XA_CHUNK_SHIFT;
688 entry = xa_head_locked(xa);
690 } else if (xas_error(xas)) {
693 unsigned int offset = xas->xa_offset;
696 entry = xa_entry_locked(xa, node, offset);
697 slot = &node->slots[offset];
700 entry = xa_head_locked(xa);
704 while (shift > order) {
705 shift -= XA_CHUNK_SHIFT;
707 node = xas_alloc(xas, shift);
710 if (xa_track_free(xa))
711 node_mark_all(node, XA_FREE_MARK);
712 rcu_assign_pointer(*slot, xa_mk_node(node));
713 } else if (xa_is_node(entry)) {
714 node = xa_to_node(entry);
718 entry = xas_descend(xas, node);
719 slot = &node->slots[xas->xa_offset];
726 * xas_create_range() - Ensure that stores to this range will succeed
727 * @xas: XArray operation state.
729 * Creates all of the slots in the range covered by @xas. Sets @xas to
730 * create single-index entries and positions it at the beginning of the
731 * range. This is for the benefit of users which have not yet been
732 * converted to use multi-index entries.
734 void xas_create_range(struct xa_state *xas)
736 unsigned long index = xas->xa_index;
737 unsigned char shift = xas->xa_shift;
738 unsigned char sibs = xas->xa_sibs;
740 xas->xa_index |= ((sibs + 1) << shift) - 1;
741 if (xas_is_node(xas) && xas->xa_node->shift == xas->xa_shift)
742 xas->xa_offset |= sibs;
747 xas_create(xas, true);
750 if (xas->xa_index <= (index | XA_CHUNK_MASK))
752 xas->xa_index -= XA_CHUNK_SIZE;
755 struct xa_node *node = xas->xa_node;
756 xas->xa_node = xa_parent_locked(xas->xa, node);
757 xas->xa_offset = node->offset - 1;
758 if (node->offset != 0)
764 xas->xa_shift = shift;
766 xas->xa_index = index;
769 xas->xa_index = index;
773 EXPORT_SYMBOL_GPL(xas_create_range);
775 static void update_node(struct xa_state *xas, struct xa_node *node,
776 int count, int values)
778 if (!node || (!count && !values))
781 node->count += count;
782 node->nr_values += values;
783 XA_NODE_BUG_ON(node, node->count > XA_CHUNK_SIZE);
784 XA_NODE_BUG_ON(node, node->nr_values > XA_CHUNK_SIZE);
785 xas_update(xas, node);
787 xas_delete_node(xas);
791 * xas_store() - Store this entry in the XArray.
792 * @xas: XArray operation state.
795 * If @xas is operating on a multi-index entry, the entry returned by this
796 * function is essentially meaningless (it may be an internal entry or it
797 * may be %NULL, even if there are non-NULL entries at some of the indices
798 * covered by the range). This is not a problem for any current users,
799 * and can be changed if needed.
801 * Return: The old entry at this index.
803 void *xas_store(struct xa_state *xas, void *entry)
805 struct xa_node *node;
806 void __rcu **slot = &xas->xa->xa_head;
807 unsigned int offset, max;
811 bool value = xa_is_value(entry);
814 bool allow_root = !xa_is_node(entry) && !xa_is_zero(entry);
815 first = xas_create(xas, allow_root);
817 first = xas_load(xas);
820 if (xas_invalid(xas))
823 if (node && (xas->xa_shift < node->shift))
825 if ((first == entry) && !xas->xa_sibs)
829 offset = xas->xa_offset;
830 max = xas->xa_offset + xas->xa_sibs;
832 slot = &node->slots[offset];
834 xas_squash_marks(xas);
841 * Must clear the marks before setting the entry to NULL,
842 * otherwise xas_for_each_marked may find a NULL entry and
843 * stop early. rcu_assign_pointer contains a release barrier
844 * so the mark clearing will appear to happen before the
845 * entry is set to NULL.
847 rcu_assign_pointer(*slot, entry);
848 if (xa_is_node(next) && (!node || node->shift))
849 xas_free_nodes(xas, xa_to_node(next));
852 count += !next - !entry;
853 values += !xa_is_value(first) - !value;
857 if (!xa_is_sibling(entry))
858 entry = xa_mk_sibling(xas->xa_offset);
860 if (offset == XA_CHUNK_MASK)
863 next = xa_entry_locked(xas->xa, node, ++offset);
864 if (!xa_is_sibling(next)) {
865 if (!entry && (offset > max))
872 update_node(xas, node, count, values);
875 EXPORT_SYMBOL_GPL(xas_store);
878 * xas_get_mark() - Returns the state of this mark.
879 * @xas: XArray operation state.
880 * @mark: Mark number.
882 * Return: true if the mark is set, false if the mark is clear or @xas
883 * is in an error state.
885 bool xas_get_mark(const struct xa_state *xas, xa_mark_t mark)
887 if (xas_invalid(xas))
890 return xa_marked(xas->xa, mark);
891 return node_get_mark(xas->xa_node, xas->xa_offset, mark);
893 EXPORT_SYMBOL_GPL(xas_get_mark);
896 * xas_set_mark() - Sets the mark on this entry and its parents.
897 * @xas: XArray operation state.
898 * @mark: Mark number.
900 * Sets the specified mark on this entry, and walks up the tree setting it
901 * on all the ancestor entries. Does nothing if @xas has not been walked to
902 * an entry, or is in an error state.
904 void xas_set_mark(const struct xa_state *xas, xa_mark_t mark)
906 struct xa_node *node = xas->xa_node;
907 unsigned int offset = xas->xa_offset;
909 if (xas_invalid(xas))
913 if (node_set_mark(node, offset, mark))
915 offset = node->offset;
916 node = xa_parent_locked(xas->xa, node);
919 if (!xa_marked(xas->xa, mark))
920 xa_mark_set(xas->xa, mark);
922 EXPORT_SYMBOL_GPL(xas_set_mark);
925 * xas_clear_mark() - Clears the mark on this entry and its parents.
926 * @xas: XArray operation state.
927 * @mark: Mark number.
929 * Clears the specified mark on this entry, and walks back to the head
930 * attempting to clear it on all the ancestor entries. Does nothing if
931 * @xas has not been walked to an entry, or is in an error state.
933 void xas_clear_mark(const struct xa_state *xas, xa_mark_t mark)
935 struct xa_node *node = xas->xa_node;
936 unsigned int offset = xas->xa_offset;
938 if (xas_invalid(xas))
942 if (!node_clear_mark(node, offset, mark))
944 if (node_any_mark(node, mark))
947 offset = node->offset;
948 node = xa_parent_locked(xas->xa, node);
951 if (xa_marked(xas->xa, mark))
952 xa_mark_clear(xas->xa, mark);
954 EXPORT_SYMBOL_GPL(xas_clear_mark);
957 * xas_init_marks() - Initialise all marks for the entry
958 * @xas: Array operations state.
960 * Initialise all marks for the entry specified by @xas. If we're tracking
961 * free entries with a mark, we need to set it on all entries. All other
964 * This implementation is not as efficient as it could be; we may walk
965 * up the tree multiple times.
967 void xas_init_marks(const struct xa_state *xas)
972 if (xa_track_free(xas->xa) && mark == XA_FREE_MARK)
973 xas_set_mark(xas, mark);
975 xas_clear_mark(xas, mark);
976 if (mark == XA_MARK_MAX)
981 EXPORT_SYMBOL_GPL(xas_init_marks);
984 * xas_pause() - Pause a walk to drop a lock.
985 * @xas: XArray operation state.
987 * Some users need to pause a walk and drop the lock they're holding in
988 * order to yield to a higher priority thread or carry out an operation
989 * on an entry. Those users should call this function before they drop
990 * the lock. It resets the @xas to be suitable for the next iteration
991 * of the loop after the user has reacquired the lock. If most entries
992 * found during a walk require you to call xas_pause(), the xa_for_each()
993 * iterator may be more appropriate.
995 * Note that xas_pause() only works for forward iteration. If a user needs
996 * to pause a reverse iteration, we will need a xas_pause_rev().
998 void xas_pause(struct xa_state *xas)
1000 struct xa_node *node = xas->xa_node;
1002 if (xas_invalid(xas))
1006 unsigned int offset = xas->xa_offset;
1007 while (++offset < XA_CHUNK_SIZE) {
1008 if (!xa_is_sibling(xa_entry(xas->xa, node, offset)))
1011 xas->xa_index += (offset - xas->xa_offset) << node->shift;
1015 xas->xa_node = XAS_RESTART;
1017 EXPORT_SYMBOL_GPL(xas_pause);
1020 * __xas_prev() - Find the previous entry in the XArray.
1021 * @xas: XArray operation state.
1023 * Helper function for xas_prev() which handles all the complex cases
1026 void *__xas_prev(struct xa_state *xas)
1030 if (!xas_frozen(xas->xa_node))
1033 return set_bounds(xas);
1034 if (xas_not_node(xas->xa_node))
1035 return xas_load(xas);
1037 if (xas->xa_offset != get_offset(xas->xa_index, xas->xa_node))
1040 while (xas->xa_offset == 255) {
1041 xas->xa_offset = xas->xa_node->offset - 1;
1042 xas->xa_node = xa_parent(xas->xa, xas->xa_node);
1044 return set_bounds(xas);
1048 entry = xa_entry(xas->xa, xas->xa_node, xas->xa_offset);
1049 if (!xa_is_node(entry))
1052 xas->xa_node = xa_to_node(entry);
1053 xas_set_offset(xas);
1056 EXPORT_SYMBOL_GPL(__xas_prev);
1059 * __xas_next() - Find the next entry in the XArray.
1060 * @xas: XArray operation state.
1062 * Helper function for xas_next() which handles all the complex cases
1065 void *__xas_next(struct xa_state *xas)
1069 if (!xas_frozen(xas->xa_node))
1072 return set_bounds(xas);
1073 if (xas_not_node(xas->xa_node))
1074 return xas_load(xas);
1076 if (xas->xa_offset != get_offset(xas->xa_index, xas->xa_node))
1079 while (xas->xa_offset == XA_CHUNK_SIZE) {
1080 xas->xa_offset = xas->xa_node->offset + 1;
1081 xas->xa_node = xa_parent(xas->xa, xas->xa_node);
1083 return set_bounds(xas);
1087 entry = xa_entry(xas->xa, xas->xa_node, xas->xa_offset);
1088 if (!xa_is_node(entry))
1091 xas->xa_node = xa_to_node(entry);
1092 xas_set_offset(xas);
1095 EXPORT_SYMBOL_GPL(__xas_next);
1098 * xas_find() - Find the next present entry in the XArray.
1099 * @xas: XArray operation state.
1100 * @max: Highest index to return.
1102 * If the @xas has not yet been walked to an entry, return the entry
1103 * which has an index >= xas.xa_index. If it has been walked, the entry
1104 * currently being pointed at has been processed, and so we move to the
1107 * If no entry is found and the array is smaller than @max, the iterator
1108 * is set to the smallest index not yet in the array. This allows @xas
1109 * to be immediately passed to xas_store().
1111 * Return: The entry, if found, otherwise %NULL.
1113 void *xas_find(struct xa_state *xas, unsigned long max)
1120 if (!xas->xa_node) {
1122 return set_bounds(xas);
1123 } else if (xas_top(xas->xa_node)) {
1124 entry = xas_load(xas);
1125 if (entry || xas_not_node(xas->xa_node))
1127 } else if (!xas->xa_node->shift &&
1128 xas->xa_offset != (xas->xa_index & XA_CHUNK_MASK)) {
1129 xas->xa_offset = ((xas->xa_index - 1) & XA_CHUNK_MASK) + 1;
1134 while (xas->xa_node && (xas->xa_index <= max)) {
1135 if (unlikely(xas->xa_offset == XA_CHUNK_SIZE)) {
1136 xas->xa_offset = xas->xa_node->offset + 1;
1137 xas->xa_node = xa_parent(xas->xa, xas->xa_node);
1141 entry = xa_entry(xas->xa, xas->xa_node, xas->xa_offset);
1142 if (xa_is_node(entry)) {
1143 xas->xa_node = xa_to_node(entry);
1147 if (entry && !xa_is_sibling(entry))
1154 xas->xa_node = XAS_BOUNDS;
1157 EXPORT_SYMBOL_GPL(xas_find);
1160 * xas_find_marked() - Find the next marked entry in the XArray.
1161 * @xas: XArray operation state.
1162 * @max: Highest index to return.
1163 * @mark: Mark number to search for.
1165 * If the @xas has not yet been walked to an entry, return the marked entry
1166 * which has an index >= xas.xa_index. If it has been walked, the entry
1167 * currently being pointed at has been processed, and so we return the
1168 * first marked entry with an index > xas.xa_index.
1170 * If no marked entry is found and the array is smaller than @max, @xas is
1171 * set to the bounds state and xas->xa_index is set to the smallest index
1172 * not yet in the array. This allows @xas to be immediately passed to
1175 * If no entry is found before @max is reached, @xas is set to the restart
1178 * Return: The entry, if found, otherwise %NULL.
1180 void *xas_find_marked(struct xa_state *xas, unsigned long max, xa_mark_t mark)
1182 bool advance = true;
1183 unsigned int offset;
1189 if (!xas->xa_node) {
1192 } else if (xas_top(xas->xa_node)) {
1194 entry = xa_head(xas->xa);
1195 xas->xa_node = NULL;
1196 if (xas->xa_index > max_index(entry))
1198 if (!xa_is_node(entry)) {
1199 if (xa_marked(xas->xa, mark))
1204 xas->xa_node = xa_to_node(entry);
1205 xas->xa_offset = xas->xa_index >> xas->xa_node->shift;
1208 while (xas->xa_index <= max) {
1209 if (unlikely(xas->xa_offset == XA_CHUNK_SIZE)) {
1210 xas->xa_offset = xas->xa_node->offset + 1;
1211 xas->xa_node = xa_parent(xas->xa, xas->xa_node);
1219 entry = xa_entry(xas->xa, xas->xa_node, xas->xa_offset);
1220 if (xa_is_sibling(entry)) {
1221 xas->xa_offset = xa_to_sibling(entry);
1222 xas_move_index(xas, xas->xa_offset);
1226 offset = xas_find_chunk(xas, advance, mark);
1227 if (offset > xas->xa_offset) {
1229 xas_move_index(xas, offset);
1231 if ((xas->xa_index - 1) >= max)
1233 xas->xa_offset = offset;
1234 if (offset == XA_CHUNK_SIZE)
1238 entry = xa_entry(xas->xa, xas->xa_node, xas->xa_offset);
1239 if (!xa_is_node(entry))
1241 xas->xa_node = xa_to_node(entry);
1242 xas_set_offset(xas);
1246 if (xas->xa_index > max)
1248 return set_bounds(xas);
1250 xas->xa_node = XAS_RESTART;
1253 EXPORT_SYMBOL_GPL(xas_find_marked);
1256 * xas_find_conflict() - Find the next present entry in a range.
1257 * @xas: XArray operation state.
1259 * The @xas describes both a range and a position within that range.
1261 * Context: Any context. Expects xa_lock to be held.
1262 * Return: The next entry in the range covered by @xas or %NULL.
1264 void *xas_find_conflict(struct xa_state *xas)
1274 if (xas_top(xas->xa_node)) {
1275 curr = xas_start(xas);
1278 while (xa_is_node(curr)) {
1279 struct xa_node *node = xa_to_node(curr);
1280 curr = xas_descend(xas, node);
1286 if (xas->xa_node->shift > xas->xa_shift)
1290 if (xas->xa_node->shift == xas->xa_shift) {
1291 if ((xas->xa_offset & xas->xa_sibs) == xas->xa_sibs)
1293 } else if (xas->xa_offset == XA_CHUNK_MASK) {
1294 xas->xa_offset = xas->xa_node->offset;
1295 xas->xa_node = xa_parent_locked(xas->xa, xas->xa_node);
1300 curr = xa_entry_locked(xas->xa, xas->xa_node, ++xas->xa_offset);
1301 if (xa_is_sibling(curr))
1303 while (xa_is_node(curr)) {
1304 xas->xa_node = xa_to_node(curr);
1306 curr = xa_entry_locked(xas->xa, xas->xa_node, 0);
1311 xas->xa_offset -= xas->xa_sibs;
1314 EXPORT_SYMBOL_GPL(xas_find_conflict);
1317 * xa_load() - Load an entry from an XArray.
1319 * @index: index into array.
1321 * Context: Any context. Takes and releases the RCU lock.
1322 * Return: The entry at @index in @xa.
1324 void *xa_load(struct xarray *xa, unsigned long index)
1326 XA_STATE(xas, xa, index);
1331 entry = xas_load(&xas);
1332 if (xa_is_zero(entry))
1334 } while (xas_retry(&xas, entry));
1339 EXPORT_SYMBOL(xa_load);
1341 static void *xas_result(struct xa_state *xas, void *curr)
1343 if (xa_is_zero(curr))
1346 curr = xas->xa_node;
1351 * __xa_erase() - Erase this entry from the XArray while locked.
1353 * @index: Index into array.
1355 * After this function returns, loading from @index will return %NULL.
1356 * If the index is part of a multi-index entry, all indices will be erased
1357 * and none of the entries will be part of a multi-index entry.
1359 * Context: Any context. Expects xa_lock to be held on entry.
1360 * Return: The entry which used to be at this index.
1362 void *__xa_erase(struct xarray *xa, unsigned long index)
1364 XA_STATE(xas, xa, index);
1365 return xas_result(&xas, xas_store(&xas, NULL));
1367 EXPORT_SYMBOL(__xa_erase);
1370 * xa_erase() - Erase this entry from the XArray.
1372 * @index: Index of entry.
1374 * After this function returns, loading from @index will return %NULL.
1375 * If the index is part of a multi-index entry, all indices will be erased
1376 * and none of the entries will be part of a multi-index entry.
1378 * Context: Any context. Takes and releases the xa_lock.
1379 * Return: The entry which used to be at this index.
1381 void *xa_erase(struct xarray *xa, unsigned long index)
1386 entry = __xa_erase(xa, index);
1391 EXPORT_SYMBOL(xa_erase);
1394 * __xa_store() - Store this entry in the XArray.
1396 * @index: Index into array.
1397 * @entry: New entry.
1398 * @gfp: Memory allocation flags.
1400 * You must already be holding the xa_lock when calling this function.
1401 * It will drop the lock if needed to allocate memory, and then reacquire
1404 * Context: Any context. Expects xa_lock to be held on entry. May
1405 * release and reacquire xa_lock if @gfp flags permit.
1406 * Return: The old entry at this index or xa_err() if an error happened.
1408 void *__xa_store(struct xarray *xa, unsigned long index, void *entry, gfp_t gfp)
1410 XA_STATE(xas, xa, index);
1413 if (WARN_ON_ONCE(xa_is_advanced(entry)))
1414 return XA_ERROR(-EINVAL);
1415 if (xa_track_free(xa) && !entry)
1416 entry = XA_ZERO_ENTRY;
1419 curr = xas_store(&xas, entry);
1420 if (xa_track_free(xa))
1421 xas_clear_mark(&xas, XA_FREE_MARK);
1422 } while (__xas_nomem(&xas, gfp));
1424 return xas_result(&xas, curr);
1426 EXPORT_SYMBOL(__xa_store);
1429 * xa_store() - Store this entry in the XArray.
1431 * @index: Index into array.
1432 * @entry: New entry.
1433 * @gfp: Memory allocation flags.
1435 * After this function returns, loads from this index will return @entry.
1436 * Storing into an existing multislot entry updates the entry of every index.
1437 * The marks associated with @index are unaffected unless @entry is %NULL.
1439 * Context: Any context. Takes and releases the xa_lock.
1440 * May sleep if the @gfp flags permit.
1441 * Return: The old entry at this index on success, xa_err(-EINVAL) if @entry
1442 * cannot be stored in an XArray, or xa_err(-ENOMEM) if memory allocation
1445 void *xa_store(struct xarray *xa, unsigned long index, void *entry, gfp_t gfp)
1450 curr = __xa_store(xa, index, entry, gfp);
1455 EXPORT_SYMBOL(xa_store);
1458 * __xa_cmpxchg() - Store this entry in the XArray.
1460 * @index: Index into array.
1461 * @old: Old value to test against.
1462 * @entry: New entry.
1463 * @gfp: Memory allocation flags.
1465 * You must already be holding the xa_lock when calling this function.
1466 * It will drop the lock if needed to allocate memory, and then reacquire
1469 * Context: Any context. Expects xa_lock to be held on entry. May
1470 * release and reacquire xa_lock if @gfp flags permit.
1471 * Return: The old entry at this index or xa_err() if an error happened.
1473 void *__xa_cmpxchg(struct xarray *xa, unsigned long index,
1474 void *old, void *entry, gfp_t gfp)
1476 XA_STATE(xas, xa, index);
1479 if (WARN_ON_ONCE(xa_is_advanced(entry)))
1480 return XA_ERROR(-EINVAL);
1483 curr = xas_load(&xas);
1485 xas_store(&xas, entry);
1486 if (xa_track_free(xa) && entry && !curr)
1487 xas_clear_mark(&xas, XA_FREE_MARK);
1489 } while (__xas_nomem(&xas, gfp));
1491 return xas_result(&xas, curr);
1493 EXPORT_SYMBOL(__xa_cmpxchg);
1496 * __xa_insert() - Store this entry in the XArray if no entry is present.
1498 * @index: Index into array.
1499 * @entry: New entry.
1500 * @gfp: Memory allocation flags.
1502 * Inserting a NULL entry will store a reserved entry (like xa_reserve())
1503 * if no entry is present. Inserting will fail if a reserved entry is
1504 * present, even though loading from this index will return NULL.
1506 * Context: Any context. Expects xa_lock to be held on entry. May
1507 * release and reacquire xa_lock if @gfp flags permit.
1508 * Return: 0 if the store succeeded. -EBUSY if another entry was present.
1509 * -ENOMEM if memory could not be allocated.
1511 int __xa_insert(struct xarray *xa, unsigned long index, void *entry, gfp_t gfp)
1513 XA_STATE(xas, xa, index);
1516 if (WARN_ON_ONCE(xa_is_advanced(entry)))
1519 entry = XA_ZERO_ENTRY;
1522 curr = xas_load(&xas);
1524 xas_store(&xas, entry);
1525 if (xa_track_free(xa))
1526 xas_clear_mark(&xas, XA_FREE_MARK);
1528 xas_set_err(&xas, -EBUSY);
1530 } while (__xas_nomem(&xas, gfp));
1532 return xas_error(&xas);
1534 EXPORT_SYMBOL(__xa_insert);
1536 #ifdef CONFIG_XARRAY_MULTI
1537 static void xas_set_range(struct xa_state *xas, unsigned long first,
1540 unsigned int shift = 0;
1541 unsigned long sibs = last - first;
1542 unsigned int offset = XA_CHUNK_MASK;
1544 xas_set(xas, first);
1546 while ((first & XA_CHUNK_MASK) == 0) {
1547 if (sibs < XA_CHUNK_MASK)
1549 if ((sibs == XA_CHUNK_MASK) && (offset < XA_CHUNK_MASK))
1551 shift += XA_CHUNK_SHIFT;
1552 if (offset == XA_CHUNK_MASK)
1553 offset = sibs & XA_CHUNK_MASK;
1554 sibs >>= XA_CHUNK_SHIFT;
1555 first >>= XA_CHUNK_SHIFT;
1558 offset = first & XA_CHUNK_MASK;
1559 if (offset + sibs > XA_CHUNK_MASK)
1560 sibs = XA_CHUNK_MASK - offset;
1561 if ((((first + sibs + 1) << shift) - 1) > last)
1564 xas->xa_shift = shift;
1565 xas->xa_sibs = sibs;
1569 * xa_store_range() - Store this entry at a range of indices in the XArray.
1571 * @first: First index to affect.
1572 * @last: Last index to affect.
1573 * @entry: New entry.
1574 * @gfp: Memory allocation flags.
1576 * After this function returns, loads from any index between @first and @last,
1577 * inclusive will return @entry.
1578 * Storing into an existing multislot entry updates the entry of every index.
1579 * The marks associated with @index are unaffected unless @entry is %NULL.
1581 * Context: Process context. Takes and releases the xa_lock. May sleep
1582 * if the @gfp flags permit.
1583 * Return: %NULL on success, xa_err(-EINVAL) if @entry cannot be stored in
1584 * an XArray, or xa_err(-ENOMEM) if memory allocation failed.
1586 void *xa_store_range(struct xarray *xa, unsigned long first,
1587 unsigned long last, void *entry, gfp_t gfp)
1589 XA_STATE(xas, xa, 0);
1591 if (WARN_ON_ONCE(xa_is_internal(entry)))
1592 return XA_ERROR(-EINVAL);
1594 return XA_ERROR(-EINVAL);
1599 unsigned int order = BITS_PER_LONG;
1601 order = __ffs(last + 1);
1602 xas_set_order(&xas, last, order);
1603 xas_create(&xas, true);
1604 if (xas_error(&xas))
1608 xas_set_range(&xas, first, last);
1609 xas_store(&xas, entry);
1610 if (xas_error(&xas))
1612 first += xas_size(&xas);
1613 } while (first <= last);
1616 } while (xas_nomem(&xas, gfp));
1618 return xas_result(&xas, NULL);
1620 EXPORT_SYMBOL(xa_store_range);
1621 #endif /* CONFIG_XARRAY_MULTI */
1624 * __xa_alloc() - Find somewhere to store this entry in the XArray.
1626 * @id: Pointer to ID.
1627 * @limit: Range for allocated ID.
1628 * @entry: New entry.
1629 * @gfp: Memory allocation flags.
1631 * Finds an empty entry in @xa between @limit.min and @limit.max,
1632 * stores the index into the @id pointer, then stores the entry at
1633 * that index. A concurrent lookup will not see an uninitialised @id.
1635 * Context: Any context. Expects xa_lock to be held on entry. May
1636 * release and reacquire xa_lock if @gfp flags permit.
1637 * Return: 0 on success, -ENOMEM if memory could not be allocated or
1638 * -EBUSY if there are no free entries in @limit.
1640 int __xa_alloc(struct xarray *xa, u32 *id, void *entry,
1641 struct xa_limit limit, gfp_t gfp)
1643 XA_STATE(xas, xa, 0);
1645 if (WARN_ON_ONCE(xa_is_advanced(entry)))
1647 if (WARN_ON_ONCE(!xa_track_free(xa)))
1651 entry = XA_ZERO_ENTRY;
1654 xas.xa_index = limit.min;
1655 xas_find_marked(&xas, limit.max, XA_FREE_MARK);
1656 if (xas.xa_node == XAS_RESTART)
1657 xas_set_err(&xas, -EBUSY);
1660 xas_store(&xas, entry);
1661 xas_clear_mark(&xas, XA_FREE_MARK);
1662 } while (__xas_nomem(&xas, gfp));
1664 return xas_error(&xas);
1666 EXPORT_SYMBOL(__xa_alloc);
1669 * __xa_alloc_cyclic() - Find somewhere to store this entry in the XArray.
1671 * @id: Pointer to ID.
1672 * @entry: New entry.
1673 * @limit: Range of allocated ID.
1674 * @next: Pointer to next ID to allocate.
1675 * @gfp: Memory allocation flags.
1677 * Finds an empty entry in @xa between @limit.min and @limit.max,
1678 * stores the index into the @id pointer, then stores the entry at
1679 * that index. A concurrent lookup will not see an uninitialised @id.
1680 * The search for an empty entry will start at @next and will wrap
1681 * around if necessary.
1683 * Context: Any context. Expects xa_lock to be held on entry. May
1684 * release and reacquire xa_lock if @gfp flags permit.
1685 * Return: 0 if the allocation succeeded without wrapping. 1 if the
1686 * allocation succeeded after wrapping, -ENOMEM if memory could not be
1687 * allocated or -EBUSY if there are no free entries in @limit.
1689 int __xa_alloc_cyclic(struct xarray *xa, u32 *id, void *entry,
1690 struct xa_limit limit, u32 *next, gfp_t gfp)
1692 u32 min = limit.min;
1695 limit.min = max(min, *next);
1696 ret = __xa_alloc(xa, id, entry, limit, gfp);
1697 if ((xa->xa_flags & XA_FLAGS_ALLOC_WRAPPED) && ret == 0) {
1698 xa->xa_flags &= ~XA_FLAGS_ALLOC_WRAPPED;
1702 if (ret < 0 && limit.min > min) {
1704 ret = __xa_alloc(xa, id, entry, limit, gfp);
1712 xa->xa_flags |= XA_FLAGS_ALLOC_WRAPPED;
1716 EXPORT_SYMBOL(__xa_alloc_cyclic);
1719 * __xa_set_mark() - Set this mark on this entry while locked.
1721 * @index: Index of entry.
1722 * @mark: Mark number.
1724 * Attempting to set a mark on a %NULL entry does not succeed.
1726 * Context: Any context. Expects xa_lock to be held on entry.
1728 void __xa_set_mark(struct xarray *xa, unsigned long index, xa_mark_t mark)
1730 XA_STATE(xas, xa, index);
1731 void *entry = xas_load(&xas);
1734 xas_set_mark(&xas, mark);
1736 EXPORT_SYMBOL(__xa_set_mark);
1739 * __xa_clear_mark() - Clear this mark on this entry while locked.
1741 * @index: Index of entry.
1742 * @mark: Mark number.
1744 * Context: Any context. Expects xa_lock to be held on entry.
1746 void __xa_clear_mark(struct xarray *xa, unsigned long index, xa_mark_t mark)
1748 XA_STATE(xas, xa, index);
1749 void *entry = xas_load(&xas);
1752 xas_clear_mark(&xas, mark);
1754 EXPORT_SYMBOL(__xa_clear_mark);
1757 * xa_get_mark() - Inquire whether this mark is set on this entry.
1759 * @index: Index of entry.
1760 * @mark: Mark number.
1762 * This function uses the RCU read lock, so the result may be out of date
1763 * by the time it returns. If you need the result to be stable, use a lock.
1765 * Context: Any context. Takes and releases the RCU lock.
1766 * Return: True if the entry at @index has this mark set, false if it doesn't.
1768 bool xa_get_mark(struct xarray *xa, unsigned long index, xa_mark_t mark)
1770 XA_STATE(xas, xa, index);
1774 entry = xas_start(&xas);
1775 while (xas_get_mark(&xas, mark)) {
1776 if (!xa_is_node(entry))
1778 entry = xas_descend(&xas, xa_to_node(entry));
1786 EXPORT_SYMBOL(xa_get_mark);
1789 * xa_set_mark() - Set this mark on this entry.
1791 * @index: Index of entry.
1792 * @mark: Mark number.
1794 * Attempting to set a mark on a %NULL entry does not succeed.
1796 * Context: Process context. Takes and releases the xa_lock.
1798 void xa_set_mark(struct xarray *xa, unsigned long index, xa_mark_t mark)
1801 __xa_set_mark(xa, index, mark);
1804 EXPORT_SYMBOL(xa_set_mark);
1807 * xa_clear_mark() - Clear this mark on this entry.
1809 * @index: Index of entry.
1810 * @mark: Mark number.
1812 * Clearing a mark always succeeds.
1814 * Context: Process context. Takes and releases the xa_lock.
1816 void xa_clear_mark(struct xarray *xa, unsigned long index, xa_mark_t mark)
1819 __xa_clear_mark(xa, index, mark);
1822 EXPORT_SYMBOL(xa_clear_mark);
1825 * xa_find() - Search the XArray for an entry.
1827 * @indexp: Pointer to an index.
1828 * @max: Maximum index to search to.
1829 * @filter: Selection criterion.
1831 * Finds the entry in @xa which matches the @filter, and has the lowest
1832 * index that is at least @indexp and no more than @max.
1833 * If an entry is found, @indexp is updated to be the index of the entry.
1834 * This function is protected by the RCU read lock, so it may not find
1835 * entries which are being simultaneously added. It will not return an
1836 * %XA_RETRY_ENTRY; if you need to see retry entries, use xas_find().
1838 * Context: Any context. Takes and releases the RCU lock.
1839 * Return: The entry, if found, otherwise %NULL.
1841 void *xa_find(struct xarray *xa, unsigned long *indexp,
1842 unsigned long max, xa_mark_t filter)
1844 XA_STATE(xas, xa, *indexp);
1849 if ((__force unsigned int)filter < XA_MAX_MARKS)
1850 entry = xas_find_marked(&xas, max, filter);
1852 entry = xas_find(&xas, max);
1853 } while (xas_retry(&xas, entry));
1857 *indexp = xas.xa_index;
1860 EXPORT_SYMBOL(xa_find);
1863 * xa_find_after() - Search the XArray for a present entry.
1865 * @indexp: Pointer to an index.
1866 * @max: Maximum index to search to.
1867 * @filter: Selection criterion.
1869 * Finds the entry in @xa which matches the @filter and has the lowest
1870 * index that is above @indexp and no more than @max.
1871 * If an entry is found, @indexp is updated to be the index of the entry.
1872 * This function is protected by the RCU read lock, so it may miss entries
1873 * which are being simultaneously added. It will not return an
1874 * %XA_RETRY_ENTRY; if you need to see retry entries, use xas_find().
1876 * Context: Any context. Takes and releases the RCU lock.
1877 * Return: The pointer, if found, otherwise %NULL.
1879 void *xa_find_after(struct xarray *xa, unsigned long *indexp,
1880 unsigned long max, xa_mark_t filter)
1882 XA_STATE(xas, xa, *indexp + 1);
1887 if ((__force unsigned int)filter < XA_MAX_MARKS)
1888 entry = xas_find_marked(&xas, max, filter);
1890 entry = xas_find(&xas, max);
1891 if (xas.xa_node == XAS_BOUNDS)
1894 if (xas.xa_index & ((1UL << xas.xa_shift) - 1))
1897 if (xas.xa_offset < (xas.xa_index & XA_CHUNK_MASK))
1900 if (!xas_retry(&xas, entry))
1906 *indexp = xas.xa_index;
1909 EXPORT_SYMBOL(xa_find_after);
1911 static unsigned int xas_extract_present(struct xa_state *xas, void **dst,
1912 unsigned long max, unsigned int n)
1918 xas_for_each(xas, entry, max) {
1919 if (xas_retry(xas, entry))
1930 static unsigned int xas_extract_marked(struct xa_state *xas, void **dst,
1931 unsigned long max, unsigned int n, xa_mark_t mark)
1937 xas_for_each_marked(xas, entry, max, mark) {
1938 if (xas_retry(xas, entry))
1950 * xa_extract() - Copy selected entries from the XArray into a normal array.
1951 * @xa: The source XArray to copy from.
1952 * @dst: The buffer to copy entries into.
1953 * @start: The first index in the XArray eligible to be selected.
1954 * @max: The last index in the XArray eligible to be selected.
1955 * @n: The maximum number of entries to copy.
1956 * @filter: Selection criterion.
1958 * Copies up to @n entries that match @filter from the XArray. The
1959 * copied entries will have indices between @start and @max, inclusive.
1961 * The @filter may be an XArray mark value, in which case entries which are
1962 * marked with that mark will be copied. It may also be %XA_PRESENT, in
1963 * which case all entries which are not %NULL will be copied.
1965 * The entries returned may not represent a snapshot of the XArray at a
1966 * moment in time. For example, if another thread stores to index 5, then
1967 * index 10, calling xa_extract() may return the old contents of index 5
1968 * and the new contents of index 10. Indices not modified while this
1969 * function is running will not be skipped.
1971 * If you need stronger guarantees, holding the xa_lock across calls to this
1972 * function will prevent concurrent modification.
1974 * Context: Any context. Takes and releases the RCU lock.
1975 * Return: The number of entries copied.
1977 unsigned int xa_extract(struct xarray *xa, void **dst, unsigned long start,
1978 unsigned long max, unsigned int n, xa_mark_t filter)
1980 XA_STATE(xas, xa, start);
1985 if ((__force unsigned int)filter < XA_MAX_MARKS)
1986 return xas_extract_marked(&xas, dst, max, n, filter);
1987 return xas_extract_present(&xas, dst, max, n);
1989 EXPORT_SYMBOL(xa_extract);
1992 * xa_destroy() - Free all internal data structures.
1995 * After calling this function, the XArray is empty and has freed all memory
1996 * allocated for its internal data structures. You are responsible for
1997 * freeing the objects referenced by the XArray.
1999 * Context: Any context. Takes and releases the xa_lock, interrupt-safe.
2001 void xa_destroy(struct xarray *xa)
2003 XA_STATE(xas, xa, 0);
2004 unsigned long flags;
2008 xas_lock_irqsave(&xas, flags);
2009 entry = xa_head_locked(xa);
2010 RCU_INIT_POINTER(xa->xa_head, NULL);
2011 xas_init_marks(&xas);
2012 if (xa_zero_busy(xa))
2013 xa_mark_clear(xa, XA_FREE_MARK);
2014 /* lockdep checks we're still holding the lock in xas_free_nodes() */
2015 if (xa_is_node(entry))
2016 xas_free_nodes(&xas, xa_to_node(entry));
2017 xas_unlock_irqrestore(&xas, flags);
2019 EXPORT_SYMBOL(xa_destroy);
2022 void xa_dump_node(const struct xa_node *node)
2028 if ((unsigned long)node & 3) {
2029 pr_cont("node %px\n", node);
2033 pr_cont("node %px %s %d parent %px shift %d count %d values %d "
2034 "array %px list %px %px marks",
2035 node, node->parent ? "offset" : "max", node->offset,
2036 node->parent, node->shift, node->count, node->nr_values,
2037 node->array, node->private_list.prev, node->private_list.next);
2038 for (i = 0; i < XA_MAX_MARKS; i++)
2039 for (j = 0; j < XA_MARK_LONGS; j++)
2040 pr_cont(" %lx", node->marks[i][j]);
2044 void xa_dump_index(unsigned long index, unsigned int shift)
2047 pr_info("%lu: ", index);
2048 else if (shift >= BITS_PER_LONG)
2049 pr_info("0-%lu: ", ~0UL);
2051 pr_info("%lu-%lu: ", index, index | ((1UL << shift) - 1));
2054 void xa_dump_entry(const void *entry, unsigned long index, unsigned long shift)
2059 xa_dump_index(index, shift);
2061 if (xa_is_node(entry)) {
2063 pr_cont("%px\n", entry);
2066 struct xa_node *node = xa_to_node(entry);
2068 for (i = 0; i < XA_CHUNK_SIZE; i++)
2069 xa_dump_entry(node->slots[i],
2070 index + (i << node->shift), node->shift);
2072 } else if (xa_is_value(entry))
2073 pr_cont("value %ld (0x%lx) [%px]\n", xa_to_value(entry),
2074 xa_to_value(entry), entry);
2075 else if (!xa_is_internal(entry))
2076 pr_cont("%px\n", entry);
2077 else if (xa_is_retry(entry))
2078 pr_cont("retry (%ld)\n", xa_to_internal(entry));
2079 else if (xa_is_sibling(entry))
2080 pr_cont("sibling (slot %ld)\n", xa_to_sibling(entry));
2081 else if (xa_is_zero(entry))
2082 pr_cont("zero (%ld)\n", xa_to_internal(entry));
2084 pr_cont("UNKNOWN ENTRY (%px)\n", entry);
2087 void xa_dump(const struct xarray *xa)
2089 void *entry = xa->xa_head;
2090 unsigned int shift = 0;
2092 pr_info("xarray: %px head %px flags %x marks %d %d %d\n", xa, entry,
2093 xa->xa_flags, xa_marked(xa, XA_MARK_0),
2094 xa_marked(xa, XA_MARK_1), xa_marked(xa, XA_MARK_2));
2095 if (xa_is_node(entry))
2096 shift = xa_to_node(entry)->shift + XA_CHUNK_SHIFT;
2097 xa_dump_entry(entry, 0, shift);
2100 #endif /* !HAVE_XARRAY_SUPPORT */