1 // SPDX-License-Identifier: GPL-2.0+
3 * XArray implementation
4 * Copyright (c) 2017 Microsoft Corporation
5 * Author: Matthew Wilcox <willy@infradead.org>
8 #include <linux/bitmap.h>
9 #include <linux/export.h>
10 #include <linux/list.h>
11 #include <linux/slab.h>
12 #include <linux/xarray.h>
15 * Coding conventions in this file:
17 * @xa is used to refer to the entire xarray.
18 * @xas is the 'xarray operation state'. It may be either a pointer to
19 * an xa_state, or an xa_state stored on the stack. This is an unfortunate
21 * @index is the index of the entry being operated on
22 * @mark is an xa_mark_t; a small number indicating one of the mark bits.
23 * @node refers to an xa_node; usually the primary one being operated on by
25 * @offset is the index into the slots array inside an xa_node.
26 * @parent refers to the @xa_node closer to the head than @node.
27 * @entry refers to something stored in a slot in the xarray
30 static inline unsigned int xa_lock_type(const struct xarray
*xa
)
32 return (__force
unsigned int)xa
->xa_flags
& 3;
35 static inline void xas_lock_type(struct xa_state
*xas
, unsigned int lock_type
)
37 if (lock_type
== XA_LOCK_IRQ
)
39 else if (lock_type
== XA_LOCK_BH
)
45 static inline void xas_unlock_type(struct xa_state
*xas
, unsigned int lock_type
)
47 if (lock_type
== XA_LOCK_IRQ
)
49 else if (lock_type
== XA_LOCK_BH
)
55 static inline bool xa_track_free(const struct xarray
*xa
)
57 return xa
->xa_flags
& XA_FLAGS_TRACK_FREE
;
60 static inline void xa_mark_set(struct xarray
*xa
, xa_mark_t mark
)
62 if (!(xa
->xa_flags
& XA_FLAGS_MARK(mark
)))
63 xa
->xa_flags
|= XA_FLAGS_MARK(mark
);
66 static inline void xa_mark_clear(struct xarray
*xa
, xa_mark_t mark
)
68 if (xa
->xa_flags
& XA_FLAGS_MARK(mark
))
69 xa
->xa_flags
&= ~(XA_FLAGS_MARK(mark
));
72 static inline unsigned long *node_marks(struct xa_node
*node
, xa_mark_t mark
)
74 return node
->marks
[(__force
unsigned)mark
];
77 static inline bool node_get_mark(struct xa_node
*node
,
78 unsigned int offset
, xa_mark_t mark
)
80 return test_bit(offset
, node_marks(node
, mark
));
83 /* returns true if the bit was set */
84 static inline bool node_set_mark(struct xa_node
*node
, unsigned int offset
,
87 return __test_and_set_bit(offset
, node_marks(node
, mark
));
90 /* returns true if the bit was set */
91 static inline bool node_clear_mark(struct xa_node
*node
, unsigned int offset
,
94 return __test_and_clear_bit(offset
, node_marks(node
, mark
));
97 static inline bool node_any_mark(struct xa_node
*node
, xa_mark_t mark
)
99 return !bitmap_empty(node_marks(node
, mark
), XA_CHUNK_SIZE
);
102 static inline void node_mark_all(struct xa_node
*node
, xa_mark_t mark
)
104 bitmap_fill(node_marks(node
, mark
), XA_CHUNK_SIZE
);
107 #define mark_inc(mark) do { \
108 mark = (__force xa_mark_t)((__force unsigned)(mark) + 1); \
112 * xas_squash_marks() - Merge all marks to the first entry
113 * @xas: Array operation state.
115 * Set a mark on the first entry if any entry has it set. Clear marks on
116 * all sibling entries.
118 static void xas_squash_marks(const struct xa_state
*xas
)
120 unsigned int mark
= 0;
121 unsigned int limit
= xas
->xa_offset
+ xas
->xa_sibs
+ 1;
127 unsigned long *marks
= xas
->xa_node
->marks
[mark
];
128 if (find_next_bit(marks
, limit
, xas
->xa_offset
+ 1) == limit
)
130 __set_bit(xas
->xa_offset
, marks
);
131 bitmap_clear(marks
, xas
->xa_offset
+ 1, xas
->xa_sibs
);
132 } while (mark
++ != (__force
unsigned)XA_MARK_MAX
);
135 /* extracts the offset within this node from the index */
136 static unsigned int get_offset(unsigned long index
, struct xa_node
*node
)
138 return (index
>> node
->shift
) & XA_CHUNK_MASK
;
141 static void xas_set_offset(struct xa_state
*xas
)
143 xas
->xa_offset
= get_offset(xas
->xa_index
, xas
->xa_node
);
146 /* move the index either forwards (find) or backwards (sibling slot) */
147 static void xas_move_index(struct xa_state
*xas
, unsigned long offset
)
149 unsigned int shift
= xas
->xa_node
->shift
;
150 xas
->xa_index
&= ~XA_CHUNK_MASK
<< shift
;
151 xas
->xa_index
+= offset
<< shift
;
154 static void xas_advance(struct xa_state
*xas
)
157 xas_move_index(xas
, xas
->xa_offset
);
160 static void *set_bounds(struct xa_state
*xas
)
162 xas
->xa_node
= XAS_BOUNDS
;
167 * Starts a walk. If the @xas is already valid, we assume that it's on
168 * the right path and just return where we've got to. If we're in an
169 * error state, return NULL. If the index is outside the current scope
170 * of the xarray, return NULL without changing @xas->xa_node. Otherwise
171 * set @xas->xa_node to NULL and return the current head of the array.
173 static void *xas_start(struct xa_state
*xas
)
178 return xas_reload(xas
);
182 entry
= xa_head(xas
->xa
);
183 if (!xa_is_node(entry
)) {
185 return set_bounds(xas
);
187 if ((xas
->xa_index
>> xa_to_node(entry
)->shift
) > XA_CHUNK_MASK
)
188 return set_bounds(xas
);
195 static void *xas_descend(struct xa_state
*xas
, struct xa_node
*node
)
197 unsigned int offset
= get_offset(xas
->xa_index
, node
);
198 void *entry
= xa_entry(xas
->xa
, node
, offset
);
201 if (xa_is_sibling(entry
)) {
202 offset
= xa_to_sibling(entry
);
203 entry
= xa_entry(xas
->xa
, node
, offset
);
206 xas
->xa_offset
= offset
;
211 * xas_load() - Load an entry from the XArray (advanced).
212 * @xas: XArray operation state.
214 * Usually walks the @xas to the appropriate state to load the entry
215 * stored at xa_index. However, it will do nothing and return %NULL if
216 * @xas is in an error state. xas_load() will never expand the tree.
218 * If the xa_state is set up to operate on a multi-index entry, xas_load()
219 * may return %NULL or an internal entry, even if there are entries
220 * present within the range specified by @xas.
222 * Context: Any context. The caller should hold the xa_lock or the RCU lock.
223 * Return: Usually an entry in the XArray, but see description for exceptions.
225 void *xas_load(struct xa_state
*xas
)
227 void *entry
= xas_start(xas
);
229 while (xa_is_node(entry
)) {
230 struct xa_node
*node
= xa_to_node(entry
);
232 if (xas
->xa_shift
> node
->shift
)
234 entry
= xas_descend(xas
, node
);
235 if (node
->shift
== 0)
240 EXPORT_SYMBOL_GPL(xas_load
);
242 /* Move the radix tree node cache here */
243 extern struct kmem_cache
*radix_tree_node_cachep
;
244 extern void radix_tree_node_rcu_free(struct rcu_head
*head
);
246 #define XA_RCU_FREE ((struct xarray *)1)
248 static void xa_node_free(struct xa_node
*node
)
250 XA_NODE_BUG_ON(node
, !list_empty(&node
->private_list
));
251 node
->array
= XA_RCU_FREE
;
252 call_rcu(&node
->rcu_head
, radix_tree_node_rcu_free
);
256 * xas_destroy() - Free any resources allocated during the XArray operation.
257 * @xas: XArray operation state.
259 * This function is now internal-only.
261 static void xas_destroy(struct xa_state
*xas
)
263 struct xa_node
*node
= xas
->xa_alloc
;
267 XA_NODE_BUG_ON(node
, !list_empty(&node
->private_list
));
268 kmem_cache_free(radix_tree_node_cachep
, node
);
269 xas
->xa_alloc
= NULL
;
273 * xas_nomem() - Allocate memory if needed.
274 * @xas: XArray operation state.
275 * @gfp: Memory allocation flags.
277 * If we need to add new nodes to the XArray, we try to allocate memory
278 * with GFP_NOWAIT while holding the lock, which will usually succeed.
279 * If it fails, @xas is flagged as needing memory to continue. The caller
280 * should drop the lock and call xas_nomem(). If xas_nomem() succeeds,
281 * the caller should retry the operation.
283 * Forward progress is guaranteed as one node is allocated here and
284 * stored in the xa_state where it will be found by xas_alloc(). More
285 * nodes will likely be found in the slab allocator, but we do not tie
288 * Return: true if memory was needed, and was successfully allocated.
290 bool xas_nomem(struct xa_state
*xas
, gfp_t gfp
)
292 if (xas
->xa_node
!= XA_ERROR(-ENOMEM
)) {
296 xas
->xa_alloc
= kmem_cache_alloc(radix_tree_node_cachep
, gfp
);
299 XA_NODE_BUG_ON(xas
->xa_alloc
, !list_empty(&xas
->xa_alloc
->private_list
));
300 xas
->xa_node
= XAS_RESTART
;
303 EXPORT_SYMBOL_GPL(xas_nomem
);
306 * __xas_nomem() - Drop locks and allocate memory if needed.
307 * @xas: XArray operation state.
308 * @gfp: Memory allocation flags.
310 * Internal variant of xas_nomem().
312 * Return: true if memory was needed, and was successfully allocated.
314 static bool __xas_nomem(struct xa_state
*xas
, gfp_t gfp
)
315 __must_hold(xas
->xa
->xa_lock
)
317 unsigned int lock_type
= xa_lock_type(xas
->xa
);
319 if (xas
->xa_node
!= XA_ERROR(-ENOMEM
)) {
323 if (gfpflags_allow_blocking(gfp
)) {
324 xas_unlock_type(xas
, lock_type
);
325 xas
->xa_alloc
= kmem_cache_alloc(radix_tree_node_cachep
, gfp
);
326 xas_lock_type(xas
, lock_type
);
328 xas
->xa_alloc
= kmem_cache_alloc(radix_tree_node_cachep
, gfp
);
332 XA_NODE_BUG_ON(xas
->xa_alloc
, !list_empty(&xas
->xa_alloc
->private_list
));
333 xas
->xa_node
= XAS_RESTART
;
337 static void xas_update(struct xa_state
*xas
, struct xa_node
*node
)
340 xas
->xa_update(node
);
342 XA_NODE_BUG_ON(node
, !list_empty(&node
->private_list
));
345 static void *xas_alloc(struct xa_state
*xas
, unsigned int shift
)
347 struct xa_node
*parent
= xas
->xa_node
;
348 struct xa_node
*node
= xas
->xa_alloc
;
350 if (xas_invalid(xas
))
354 xas
->xa_alloc
= NULL
;
356 node
= kmem_cache_alloc(radix_tree_node_cachep
,
357 GFP_NOWAIT
| __GFP_NOWARN
);
359 xas_set_err(xas
, -ENOMEM
);
365 node
->offset
= xas
->xa_offset
;
367 XA_NODE_BUG_ON(node
, parent
->count
> XA_CHUNK_SIZE
);
368 xas_update(xas
, parent
);
370 XA_NODE_BUG_ON(node
, shift
> BITS_PER_LONG
);
371 XA_NODE_BUG_ON(node
, !list_empty(&node
->private_list
));
375 RCU_INIT_POINTER(node
->parent
, xas
->xa_node
);
376 node
->array
= xas
->xa
;
381 #ifdef CONFIG_XARRAY_MULTI
382 /* Returns the number of indices covered by a given xa_state */
383 static unsigned long xas_size(const struct xa_state
*xas
)
385 return (xas
->xa_sibs
+ 1UL) << xas
->xa_shift
;
390 * Use this to calculate the maximum index that will need to be created
391 * in order to add the entry described by @xas. Because we cannot store a
392 * multiple-index entry at index 0, the calculation is a little more complex
393 * than you might expect.
395 static unsigned long xas_max(struct xa_state
*xas
)
397 unsigned long max
= xas
->xa_index
;
399 #ifdef CONFIG_XARRAY_MULTI
400 if (xas
->xa_shift
|| xas
->xa_sibs
) {
401 unsigned long mask
= xas_size(xas
) - 1;
411 /* The maximum index that can be contained in the array without expanding it */
412 static unsigned long max_index(void *entry
)
414 if (!xa_is_node(entry
))
416 return (XA_CHUNK_SIZE
<< xa_to_node(entry
)->shift
) - 1;
419 static void xas_shrink(struct xa_state
*xas
)
421 struct xarray
*xa
= xas
->xa
;
422 struct xa_node
*node
= xas
->xa_node
;
427 XA_NODE_BUG_ON(node
, node
->count
> XA_CHUNK_SIZE
);
428 if (node
->count
!= 1)
430 entry
= xa_entry_locked(xa
, node
, 0);
433 if (!xa_is_node(entry
) && node
->shift
)
435 xas
->xa_node
= XAS_BOUNDS
;
437 RCU_INIT_POINTER(xa
->xa_head
, entry
);
438 if (xa_track_free(xa
) && !node_get_mark(node
, 0, XA_FREE_MARK
))
439 xa_mark_clear(xa
, XA_FREE_MARK
);
443 if (!xa_is_node(entry
))
444 RCU_INIT_POINTER(node
->slots
[0], XA_RETRY_ENTRY
);
445 xas_update(xas
, node
);
447 if (!xa_is_node(entry
))
449 node
= xa_to_node(entry
);
455 * xas_delete_node() - Attempt to delete an xa_node
456 * @xas: Array operation state.
458 * Attempts to delete the @xas->xa_node. This will fail if xa->node has
459 * a non-zero reference count.
461 static void xas_delete_node(struct xa_state
*xas
)
463 struct xa_node
*node
= xas
->xa_node
;
466 struct xa_node
*parent
;
468 XA_NODE_BUG_ON(node
, node
->count
> XA_CHUNK_SIZE
);
472 parent
= xa_parent_locked(xas
->xa
, node
);
473 xas
->xa_node
= parent
;
474 xas
->xa_offset
= node
->offset
;
478 xas
->xa
->xa_head
= NULL
;
479 xas
->xa_node
= XAS_BOUNDS
;
483 parent
->slots
[xas
->xa_offset
] = NULL
;
485 XA_NODE_BUG_ON(parent
, parent
->count
> XA_CHUNK_SIZE
);
487 xas_update(xas
, node
);
495 * xas_free_nodes() - Free this node and all nodes that it references
496 * @xas: Array operation state.
499 * This node has been removed from the tree. We must now free it and all
500 * of its subnodes. There may be RCU walkers with references into the tree,
501 * so we must replace all entries with retry markers.
503 static void xas_free_nodes(struct xa_state
*xas
, struct xa_node
*top
)
505 unsigned int offset
= 0;
506 struct xa_node
*node
= top
;
509 void *entry
= xa_entry_locked(xas
->xa
, node
, offset
);
511 if (node
->shift
&& xa_is_node(entry
)) {
512 node
= xa_to_node(entry
);
517 RCU_INIT_POINTER(node
->slots
[offset
], XA_RETRY_ENTRY
);
519 while (offset
== XA_CHUNK_SIZE
) {
520 struct xa_node
*parent
;
522 parent
= xa_parent_locked(xas
->xa
, node
);
523 offset
= node
->offset
+ 1;
526 xas_update(xas
, node
);
536 * xas_expand adds nodes to the head of the tree until it has reached
537 * sufficient height to be able to contain @xas->xa_index
539 static int xas_expand(struct xa_state
*xas
, void *head
)
541 struct xarray
*xa
= xas
->xa
;
542 struct xa_node
*node
= NULL
;
543 unsigned int shift
= 0;
544 unsigned long max
= xas_max(xas
);
549 while ((max
>> shift
) >= XA_CHUNK_SIZE
)
550 shift
+= XA_CHUNK_SHIFT
;
551 return shift
+ XA_CHUNK_SHIFT
;
552 } else if (xa_is_node(head
)) {
553 node
= xa_to_node(head
);
554 shift
= node
->shift
+ XA_CHUNK_SHIFT
;
558 while (max
> max_index(head
)) {
561 XA_NODE_BUG_ON(node
, shift
> BITS_PER_LONG
);
562 node
= xas_alloc(xas
, shift
);
567 if (xa_is_value(head
))
569 RCU_INIT_POINTER(node
->slots
[0], head
);
571 /* Propagate the aggregated mark info to the new child */
573 if (xa_track_free(xa
) && mark
== XA_FREE_MARK
) {
574 node_mark_all(node
, XA_FREE_MARK
);
575 if (!xa_marked(xa
, XA_FREE_MARK
)) {
576 node_clear_mark(node
, 0, XA_FREE_MARK
);
577 xa_mark_set(xa
, XA_FREE_MARK
);
579 } else if (xa_marked(xa
, mark
)) {
580 node_set_mark(node
, 0, mark
);
582 if (mark
== XA_MARK_MAX
)
588 * Now that the new node is fully initialised, we can add
591 if (xa_is_node(head
)) {
592 xa_to_node(head
)->offset
= 0;
593 rcu_assign_pointer(xa_to_node(head
)->parent
, node
);
595 head
= xa_mk_node(node
);
596 rcu_assign_pointer(xa
->xa_head
, head
);
597 xas_update(xas
, node
);
599 shift
+= XA_CHUNK_SHIFT
;
607 * xas_create() - Create a slot to store an entry in.
608 * @xas: XArray operation state.
609 * @allow_root: %true if we can store the entry in the root directly
611 * Most users will not need to call this function directly, as it is called
612 * by xas_store(). It is useful for doing conditional store operations
613 * (see the xa_cmpxchg() implementation for an example).
615 * Return: If the slot already existed, returns the contents of this slot.
616 * If the slot was newly created, returns %NULL. If it failed to create the
617 * slot, returns %NULL and indicates the error in @xas.
619 static void *xas_create(struct xa_state
*xas
, bool allow_root
)
621 struct xarray
*xa
= xas
->xa
;
624 struct xa_node
*node
= xas
->xa_node
;
626 unsigned int order
= xas
->xa_shift
;
629 entry
= xa_head_locked(xa
);
631 shift
= xas_expand(xas
, entry
);
634 if (!shift
&& !allow_root
)
635 shift
= XA_CHUNK_SHIFT
;
636 entry
= xa_head_locked(xa
);
638 } else if (xas_error(xas
)) {
641 unsigned int offset
= xas
->xa_offset
;
644 entry
= xa_entry_locked(xa
, node
, offset
);
645 slot
= &node
->slots
[offset
];
648 entry
= xa_head_locked(xa
);
652 while (shift
> order
) {
653 shift
-= XA_CHUNK_SHIFT
;
655 node
= xas_alloc(xas
, shift
);
658 if (xa_track_free(xa
))
659 node_mark_all(node
, XA_FREE_MARK
);
660 rcu_assign_pointer(*slot
, xa_mk_node(node
));
661 } else if (xa_is_node(entry
)) {
662 node
= xa_to_node(entry
);
666 entry
= xas_descend(xas
, node
);
667 slot
= &node
->slots
[xas
->xa_offset
];
674 * xas_create_range() - Ensure that stores to this range will succeed
675 * @xas: XArray operation state.
677 * Creates all of the slots in the range covered by @xas. Sets @xas to
678 * create single-index entries and positions it at the beginning of the
679 * range. This is for the benefit of users which have not yet been
680 * converted to use multi-index entries.
682 void xas_create_range(struct xa_state
*xas
)
684 unsigned long index
= xas
->xa_index
;
685 unsigned char shift
= xas
->xa_shift
;
686 unsigned char sibs
= xas
->xa_sibs
;
688 xas
->xa_index
|= ((sibs
+ 1) << shift
) - 1;
689 if (xas_is_node(xas
) && xas
->xa_node
->shift
== xas
->xa_shift
)
690 xas
->xa_offset
|= sibs
;
695 xas_create(xas
, true);
698 if (xas
->xa_index
<= (index
| XA_CHUNK_MASK
))
700 xas
->xa_index
-= XA_CHUNK_SIZE
;
703 struct xa_node
*node
= xas
->xa_node
;
704 xas
->xa_node
= xa_parent_locked(xas
->xa
, node
);
705 xas
->xa_offset
= node
->offset
- 1;
706 if (node
->offset
!= 0)
712 xas
->xa_shift
= shift
;
714 xas
->xa_index
= index
;
717 xas
->xa_index
= index
;
721 EXPORT_SYMBOL_GPL(xas_create_range
);
723 static void update_node(struct xa_state
*xas
, struct xa_node
*node
,
724 int count
, int values
)
726 if (!node
|| (!count
&& !values
))
729 node
->count
+= count
;
730 node
->nr_values
+= values
;
731 XA_NODE_BUG_ON(node
, node
->count
> XA_CHUNK_SIZE
);
732 XA_NODE_BUG_ON(node
, node
->nr_values
> XA_CHUNK_SIZE
);
733 xas_update(xas
, node
);
735 xas_delete_node(xas
);
739 * xas_store() - Store this entry in the XArray.
740 * @xas: XArray operation state.
743 * If @xas is operating on a multi-index entry, the entry returned by this
744 * function is essentially meaningless (it may be an internal entry or it
745 * may be %NULL, even if there are non-NULL entries at some of the indices
746 * covered by the range). This is not a problem for any current users,
747 * and can be changed if needed.
749 * Return: The old entry at this index.
751 void *xas_store(struct xa_state
*xas
, void *entry
)
753 struct xa_node
*node
;
754 void __rcu
**slot
= &xas
->xa
->xa_head
;
755 unsigned int offset
, max
;
759 bool value
= xa_is_value(entry
);
762 first
= xas_create(xas
, !xa_is_node(entry
));
764 first
= xas_load(xas
);
766 if (xas_invalid(xas
))
769 if (node
&& (xas
->xa_shift
< node
->shift
))
771 if ((first
== entry
) && !xas
->xa_sibs
)
775 offset
= xas
->xa_offset
;
776 max
= xas
->xa_offset
+ xas
->xa_sibs
;
778 slot
= &node
->slots
[offset
];
780 xas_squash_marks(xas
);
787 * Must clear the marks before setting the entry to NULL,
788 * otherwise xas_for_each_marked may find a NULL entry and
789 * stop early. rcu_assign_pointer contains a release barrier
790 * so the mark clearing will appear to happen before the
791 * entry is set to NULL.
793 rcu_assign_pointer(*slot
, entry
);
794 if (xa_is_node(next
))
795 xas_free_nodes(xas
, xa_to_node(next
));
798 count
+= !next
- !entry
;
799 values
+= !xa_is_value(first
) - !value
;
803 if (!xa_is_sibling(entry
))
804 entry
= xa_mk_sibling(xas
->xa_offset
);
806 if (offset
== XA_CHUNK_MASK
)
809 next
= xa_entry_locked(xas
->xa
, node
, ++offset
);
810 if (!xa_is_sibling(next
)) {
811 if (!entry
&& (offset
> max
))
818 update_node(xas
, node
, count
, values
);
821 EXPORT_SYMBOL_GPL(xas_store
);
824 * xas_get_mark() - Returns the state of this mark.
825 * @xas: XArray operation state.
826 * @mark: Mark number.
828 * Return: true if the mark is set, false if the mark is clear or @xas
829 * is in an error state.
831 bool xas_get_mark(const struct xa_state
*xas
, xa_mark_t mark
)
833 if (xas_invalid(xas
))
836 return xa_marked(xas
->xa
, mark
);
837 return node_get_mark(xas
->xa_node
, xas
->xa_offset
, mark
);
839 EXPORT_SYMBOL_GPL(xas_get_mark
);
842 * xas_set_mark() - Sets the mark on this entry and its parents.
843 * @xas: XArray operation state.
844 * @mark: Mark number.
846 * Sets the specified mark on this entry, and walks up the tree setting it
847 * on all the ancestor entries. Does nothing if @xas has not been walked to
848 * an entry, or is in an error state.
850 void xas_set_mark(const struct xa_state
*xas
, xa_mark_t mark
)
852 struct xa_node
*node
= xas
->xa_node
;
853 unsigned int offset
= xas
->xa_offset
;
855 if (xas_invalid(xas
))
859 if (node_set_mark(node
, offset
, mark
))
861 offset
= node
->offset
;
862 node
= xa_parent_locked(xas
->xa
, node
);
865 if (!xa_marked(xas
->xa
, mark
))
866 xa_mark_set(xas
->xa
, mark
);
868 EXPORT_SYMBOL_GPL(xas_set_mark
);
871 * xas_clear_mark() - Clears the mark on this entry and its parents.
872 * @xas: XArray operation state.
873 * @mark: Mark number.
875 * Clears the specified mark on this entry, and walks back to the head
876 * attempting to clear it on all the ancestor entries. Does nothing if
877 * @xas has not been walked to an entry, or is in an error state.
879 void xas_clear_mark(const struct xa_state
*xas
, xa_mark_t mark
)
881 struct xa_node
*node
= xas
->xa_node
;
882 unsigned int offset
= xas
->xa_offset
;
884 if (xas_invalid(xas
))
888 if (!node_clear_mark(node
, offset
, mark
))
890 if (node_any_mark(node
, mark
))
893 offset
= node
->offset
;
894 node
= xa_parent_locked(xas
->xa
, node
);
897 if (xa_marked(xas
->xa
, mark
))
898 xa_mark_clear(xas
->xa
, mark
);
900 EXPORT_SYMBOL_GPL(xas_clear_mark
);
903 * xas_init_marks() - Initialise all marks for the entry
904 * @xas: Array operations state.
906 * Initialise all marks for the entry specified by @xas. If we're tracking
907 * free entries with a mark, we need to set it on all entries. All other
910 * This implementation is not as efficient as it could be; we may walk
911 * up the tree multiple times.
913 void xas_init_marks(const struct xa_state
*xas
)
918 if (xa_track_free(xas
->xa
) && mark
== XA_FREE_MARK
)
919 xas_set_mark(xas
, mark
);
921 xas_clear_mark(xas
, mark
);
922 if (mark
== XA_MARK_MAX
)
927 EXPORT_SYMBOL_GPL(xas_init_marks
);
930 * xas_pause() - Pause a walk to drop a lock.
931 * @xas: XArray operation state.
933 * Some users need to pause a walk and drop the lock they're holding in
934 * order to yield to a higher priority thread or carry out an operation
935 * on an entry. Those users should call this function before they drop
936 * the lock. It resets the @xas to be suitable for the next iteration
937 * of the loop after the user has reacquired the lock. If most entries
938 * found during a walk require you to call xas_pause(), the xa_for_each()
939 * iterator may be more appropriate.
941 * Note that xas_pause() only works for forward iteration. If a user needs
942 * to pause a reverse iteration, we will need a xas_pause_rev().
944 void xas_pause(struct xa_state
*xas
)
946 struct xa_node
*node
= xas
->xa_node
;
948 if (xas_invalid(xas
))
952 unsigned int offset
= xas
->xa_offset
;
953 while (++offset
< XA_CHUNK_SIZE
) {
954 if (!xa_is_sibling(xa_entry(xas
->xa
, node
, offset
)))
957 xas
->xa_index
+= (offset
- xas
->xa_offset
) << node
->shift
;
961 xas
->xa_node
= XAS_RESTART
;
963 EXPORT_SYMBOL_GPL(xas_pause
);
966 * __xas_prev() - Find the previous entry in the XArray.
967 * @xas: XArray operation state.
969 * Helper function for xas_prev() which handles all the complex cases
972 void *__xas_prev(struct xa_state
*xas
)
976 if (!xas_frozen(xas
->xa_node
))
978 if (xas_not_node(xas
->xa_node
))
979 return xas_load(xas
);
981 if (xas
->xa_offset
!= get_offset(xas
->xa_index
, xas
->xa_node
))
984 while (xas
->xa_offset
== 255) {
985 xas
->xa_offset
= xas
->xa_node
->offset
- 1;
986 xas
->xa_node
= xa_parent(xas
->xa
, xas
->xa_node
);
988 return set_bounds(xas
);
992 entry
= xa_entry(xas
->xa
, xas
->xa_node
, xas
->xa_offset
);
993 if (!xa_is_node(entry
))
996 xas
->xa_node
= xa_to_node(entry
);
1000 EXPORT_SYMBOL_GPL(__xas_prev
);
1003 * __xas_next() - Find the next entry in the XArray.
1004 * @xas: XArray operation state.
1006 * Helper function for xas_next() which handles all the complex cases
1009 void *__xas_next(struct xa_state
*xas
)
1013 if (!xas_frozen(xas
->xa_node
))
1015 if (xas_not_node(xas
->xa_node
))
1016 return xas_load(xas
);
1018 if (xas
->xa_offset
!= get_offset(xas
->xa_index
, xas
->xa_node
))
1021 while (xas
->xa_offset
== XA_CHUNK_SIZE
) {
1022 xas
->xa_offset
= xas
->xa_node
->offset
+ 1;
1023 xas
->xa_node
= xa_parent(xas
->xa
, xas
->xa_node
);
1025 return set_bounds(xas
);
1029 entry
= xa_entry(xas
->xa
, xas
->xa_node
, xas
->xa_offset
);
1030 if (!xa_is_node(entry
))
1033 xas
->xa_node
= xa_to_node(entry
);
1034 xas_set_offset(xas
);
1037 EXPORT_SYMBOL_GPL(__xas_next
);
1040 * xas_find() - Find the next present entry in the XArray.
1041 * @xas: XArray operation state.
1042 * @max: Highest index to return.
1044 * If the @xas has not yet been walked to an entry, return the entry
1045 * which has an index >= xas.xa_index. If it has been walked, the entry
1046 * currently being pointed at has been processed, and so we move to the
1049 * If no entry is found and the array is smaller than @max, the iterator
1050 * is set to the smallest index not yet in the array. This allows @xas
1051 * to be immediately passed to xas_store().
1053 * Return: The entry, if found, otherwise %NULL.
1055 void *xas_find(struct xa_state
*xas
, unsigned long max
)
1062 if (!xas
->xa_node
) {
1064 return set_bounds(xas
);
1065 } else if (xas_top(xas
->xa_node
)) {
1066 entry
= xas_load(xas
);
1067 if (entry
|| xas_not_node(xas
->xa_node
))
1069 } else if (!xas
->xa_node
->shift
&&
1070 xas
->xa_offset
!= (xas
->xa_index
& XA_CHUNK_MASK
)) {
1071 xas
->xa_offset
= ((xas
->xa_index
- 1) & XA_CHUNK_MASK
) + 1;
1076 while (xas
->xa_node
&& (xas
->xa_index
<= max
)) {
1077 if (unlikely(xas
->xa_offset
== XA_CHUNK_SIZE
)) {
1078 xas
->xa_offset
= xas
->xa_node
->offset
+ 1;
1079 xas
->xa_node
= xa_parent(xas
->xa
, xas
->xa_node
);
1083 entry
= xa_entry(xas
->xa
, xas
->xa_node
, xas
->xa_offset
);
1084 if (xa_is_node(entry
)) {
1085 xas
->xa_node
= xa_to_node(entry
);
1089 if (entry
&& !xa_is_sibling(entry
))
1096 xas
->xa_node
= XAS_BOUNDS
;
1099 EXPORT_SYMBOL_GPL(xas_find
);
1102 * xas_find_marked() - Find the next marked entry in the XArray.
1103 * @xas: XArray operation state.
1104 * @max: Highest index to return.
1105 * @mark: Mark number to search for.
1107 * If the @xas has not yet been walked to an entry, return the marked entry
1108 * which has an index >= xas.xa_index. If it has been walked, the entry
1109 * currently being pointed at has been processed, and so we return the
1110 * first marked entry with an index > xas.xa_index.
1112 * If no marked entry is found and the array is smaller than @max, @xas is
1113 * set to the bounds state and xas->xa_index is set to the smallest index
1114 * not yet in the array. This allows @xas to be immediately passed to
1117 * If no entry is found before @max is reached, @xas is set to the restart
1120 * Return: The entry, if found, otherwise %NULL.
1122 void *xas_find_marked(struct xa_state
*xas
, unsigned long max
, xa_mark_t mark
)
1124 bool advance
= true;
1125 unsigned int offset
;
1131 if (!xas
->xa_node
) {
1134 } else if (xas_top(xas
->xa_node
)) {
1136 entry
= xa_head(xas
->xa
);
1137 xas
->xa_node
= NULL
;
1138 if (xas
->xa_index
> max_index(entry
))
1140 if (!xa_is_node(entry
)) {
1141 if (xa_marked(xas
->xa
, mark
))
1146 xas
->xa_node
= xa_to_node(entry
);
1147 xas
->xa_offset
= xas
->xa_index
>> xas
->xa_node
->shift
;
1150 while (xas
->xa_index
<= max
) {
1151 if (unlikely(xas
->xa_offset
== XA_CHUNK_SIZE
)) {
1152 xas
->xa_offset
= xas
->xa_node
->offset
+ 1;
1153 xas
->xa_node
= xa_parent(xas
->xa
, xas
->xa_node
);
1161 entry
= xa_entry(xas
->xa
, xas
->xa_node
, xas
->xa_offset
);
1162 if (xa_is_sibling(entry
)) {
1163 xas
->xa_offset
= xa_to_sibling(entry
);
1164 xas_move_index(xas
, xas
->xa_offset
);
1168 offset
= xas_find_chunk(xas
, advance
, mark
);
1169 if (offset
> xas
->xa_offset
) {
1171 xas_move_index(xas
, offset
);
1173 if ((xas
->xa_index
- 1) >= max
)
1175 xas
->xa_offset
= offset
;
1176 if (offset
== XA_CHUNK_SIZE
)
1180 entry
= xa_entry(xas
->xa
, xas
->xa_node
, xas
->xa_offset
);
1181 if (!xa_is_node(entry
))
1183 xas
->xa_node
= xa_to_node(entry
);
1184 xas_set_offset(xas
);
1188 if (xas
->xa_index
> max
)
1190 return set_bounds(xas
);
1192 xas
->xa_node
= XAS_RESTART
;
1195 EXPORT_SYMBOL_GPL(xas_find_marked
);
1198 * xas_find_conflict() - Find the next present entry in a range.
1199 * @xas: XArray operation state.
1201 * The @xas describes both a range and a position within that range.
1203 * Context: Any context. Expects xa_lock to be held.
1204 * Return: The next entry in the range covered by @xas or %NULL.
1206 void *xas_find_conflict(struct xa_state
*xas
)
1216 if (xas_top(xas
->xa_node
)) {
1217 curr
= xas_start(xas
);
1220 while (xa_is_node(curr
)) {
1221 struct xa_node
*node
= xa_to_node(curr
);
1222 curr
= xas_descend(xas
, node
);
1228 if (xas
->xa_node
->shift
> xas
->xa_shift
)
1232 if (xas
->xa_node
->shift
== xas
->xa_shift
) {
1233 if ((xas
->xa_offset
& xas
->xa_sibs
) == xas
->xa_sibs
)
1235 } else if (xas
->xa_offset
== XA_CHUNK_MASK
) {
1236 xas
->xa_offset
= xas
->xa_node
->offset
;
1237 xas
->xa_node
= xa_parent_locked(xas
->xa
, xas
->xa_node
);
1242 curr
= xa_entry_locked(xas
->xa
, xas
->xa_node
, ++xas
->xa_offset
);
1243 if (xa_is_sibling(curr
))
1245 while (xa_is_node(curr
)) {
1246 xas
->xa_node
= xa_to_node(curr
);
1248 curr
= xa_entry_locked(xas
->xa
, xas
->xa_node
, 0);
1253 xas
->xa_offset
-= xas
->xa_sibs
;
1256 EXPORT_SYMBOL_GPL(xas_find_conflict
);
1259 * xa_load() - Load an entry from an XArray.
1261 * @index: index into array.
1263 * Context: Any context. Takes and releases the RCU lock.
1264 * Return: The entry at @index in @xa.
1266 void *xa_load(struct xarray
*xa
, unsigned long index
)
1268 XA_STATE(xas
, xa
, index
);
1273 entry
= xas_load(&xas
);
1274 if (xa_is_zero(entry
))
1276 } while (xas_retry(&xas
, entry
));
1281 EXPORT_SYMBOL(xa_load
);
1283 static void *xas_result(struct xa_state
*xas
, void *curr
)
1285 if (xa_is_zero(curr
))
1288 curr
= xas
->xa_node
;
1293 * __xa_erase() - Erase this entry from the XArray while locked.
1295 * @index: Index into array.
1297 * If the entry at this index is a multi-index entry then all indices will
1298 * be erased, and the entry will no longer be a multi-index entry.
1299 * This function expects the xa_lock to be held on entry.
1301 * Context: Any context. Expects xa_lock to be held on entry. May
1302 * release and reacquire xa_lock if @gfp flags permit.
1303 * Return: The old entry at this index.
1305 void *__xa_erase(struct xarray
*xa
, unsigned long index
)
1307 XA_STATE(xas
, xa
, index
);
1308 return xas_result(&xas
, xas_store(&xas
, NULL
));
1310 EXPORT_SYMBOL(__xa_erase
);
1313 * xa_erase() - Erase this entry from the XArray.
1315 * @index: Index of entry.
1317 * This function is the equivalent of calling xa_store() with %NULL as
1318 * the third argument. The XArray does not need to allocate memory, so
1319 * the user does not need to provide GFP flags.
1321 * Context: Any context. Takes and releases the xa_lock.
1322 * Return: The entry which used to be at this index.
1324 void *xa_erase(struct xarray
*xa
, unsigned long index
)
1329 entry
= __xa_erase(xa
, index
);
1334 EXPORT_SYMBOL(xa_erase
);
1337 * __xa_store() - Store this entry in the XArray.
1339 * @index: Index into array.
1340 * @entry: New entry.
1341 * @gfp: Memory allocation flags.
1343 * You must already be holding the xa_lock when calling this function.
1344 * It will drop the lock if needed to allocate memory, and then reacquire
1347 * Context: Any context. Expects xa_lock to be held on entry. May
1348 * release and reacquire xa_lock if @gfp flags permit.
1349 * Return: The old entry at this index or xa_err() if an error happened.
1351 void *__xa_store(struct xarray
*xa
, unsigned long index
, void *entry
, gfp_t gfp
)
1353 XA_STATE(xas
, xa
, index
);
1356 if (WARN_ON_ONCE(xa_is_advanced(entry
)))
1357 return XA_ERROR(-EINVAL
);
1358 if (xa_track_free(xa
) && !entry
)
1359 entry
= XA_ZERO_ENTRY
;
1362 curr
= xas_store(&xas
, entry
);
1363 if (xa_track_free(xa
))
1364 xas_clear_mark(&xas
, XA_FREE_MARK
);
1365 } while (__xas_nomem(&xas
, gfp
));
1367 return xas_result(&xas
, curr
);
1369 EXPORT_SYMBOL(__xa_store
);
1372 * xa_store() - Store this entry in the XArray.
1374 * @index: Index into array.
1375 * @entry: New entry.
1376 * @gfp: Memory allocation flags.
1378 * After this function returns, loads from this index will return @entry.
1379 * Storing into an existing multislot entry updates the entry of every index.
1380 * The marks associated with @index are unaffected unless @entry is %NULL.
1382 * Context: Any context. Takes and releases the xa_lock.
1383 * May sleep if the @gfp flags permit.
1384 * Return: The old entry at this index on success, xa_err(-EINVAL) if @entry
1385 * cannot be stored in an XArray, or xa_err(-ENOMEM) if memory allocation
1388 void *xa_store(struct xarray
*xa
, unsigned long index
, void *entry
, gfp_t gfp
)
1393 curr
= __xa_store(xa
, index
, entry
, gfp
);
1398 EXPORT_SYMBOL(xa_store
);
1401 * __xa_cmpxchg() - Store this entry in the XArray.
1403 * @index: Index into array.
1404 * @old: Old value to test against.
1405 * @entry: New entry.
1406 * @gfp: Memory allocation flags.
1408 * You must already be holding the xa_lock when calling this function.
1409 * It will drop the lock if needed to allocate memory, and then reacquire
1412 * Context: Any context. Expects xa_lock to be held on entry. May
1413 * release and reacquire xa_lock if @gfp flags permit.
1414 * Return: The old entry at this index or xa_err() if an error happened.
1416 void *__xa_cmpxchg(struct xarray
*xa
, unsigned long index
,
1417 void *old
, void *entry
, gfp_t gfp
)
1419 XA_STATE(xas
, xa
, index
);
1422 if (WARN_ON_ONCE(xa_is_advanced(entry
)))
1423 return XA_ERROR(-EINVAL
);
1424 if (xa_track_free(xa
) && !entry
)
1425 entry
= XA_ZERO_ENTRY
;
1428 curr
= xas_load(&xas
);
1429 if (curr
== XA_ZERO_ENTRY
)
1432 xas_store(&xas
, entry
);
1433 if (xa_track_free(xa
))
1434 xas_clear_mark(&xas
, XA_FREE_MARK
);
1436 } while (__xas_nomem(&xas
, gfp
));
1438 return xas_result(&xas
, curr
);
1440 EXPORT_SYMBOL(__xa_cmpxchg
);
1443 * __xa_insert() - Store this entry in the XArray if no entry is present.
1445 * @index: Index into array.
1446 * @entry: New entry.
1447 * @gfp: Memory allocation flags.
1449 * Inserting a NULL entry will store a reserved entry (like xa_reserve())
1450 * if no entry is present. Inserting will fail if a reserved entry is
1451 * present, even though loading from this index will return NULL.
1453 * Context: Any context. Expects xa_lock to be held on entry. May
1454 * release and reacquire xa_lock if @gfp flags permit.
1455 * Return: 0 if the store succeeded. -EEXIST if another entry was present.
1456 * -ENOMEM if memory could not be allocated.
1458 int __xa_insert(struct xarray
*xa
, unsigned long index
, void *entry
, gfp_t gfp
)
1460 XA_STATE(xas
, xa
, index
);
1463 if (WARN_ON_ONCE(xa_is_advanced(entry
)))
1466 entry
= XA_ZERO_ENTRY
;
1469 curr
= xas_load(&xas
);
1471 xas_store(&xas
, entry
);
1472 if (xa_track_free(xa
))
1473 xas_clear_mark(&xas
, XA_FREE_MARK
);
1475 xas_set_err(&xas
, -EEXIST
);
1477 } while (__xas_nomem(&xas
, gfp
));
1479 return xas_error(&xas
);
1481 EXPORT_SYMBOL(__xa_insert
);
1484 * __xa_reserve() - Reserve this index in the XArray.
1486 * @index: Index into array.
1487 * @gfp: Memory allocation flags.
1489 * Ensures there is somewhere to store an entry at @index in the array.
1490 * If there is already something stored at @index, this function does
1491 * nothing. If there was nothing there, the entry is marked as reserved.
1492 * Loading from a reserved entry returns a %NULL pointer.
1494 * If you do not use the entry that you have reserved, call xa_release()
1495 * or xa_erase() to free any unnecessary memory.
1497 * Context: Any context. Expects the xa_lock to be held on entry. May
1498 * release the lock, sleep and reacquire the lock if the @gfp flags permit.
1499 * Return: 0 if the reservation succeeded or -ENOMEM if it failed.
1501 int __xa_reserve(struct xarray
*xa
, unsigned long index
, gfp_t gfp
)
1503 XA_STATE(xas
, xa
, index
);
1507 curr
= xas_load(&xas
);
1509 xas_store(&xas
, XA_ZERO_ENTRY
);
1510 if (xa_track_free(xa
))
1511 xas_clear_mark(&xas
, XA_FREE_MARK
);
1513 } while (__xas_nomem(&xas
, gfp
));
1515 return xas_error(&xas
);
1517 EXPORT_SYMBOL(__xa_reserve
);
1519 #ifdef CONFIG_XARRAY_MULTI
1520 static void xas_set_range(struct xa_state
*xas
, unsigned long first
,
1523 unsigned int shift
= 0;
1524 unsigned long sibs
= last
- first
;
1525 unsigned int offset
= XA_CHUNK_MASK
;
1527 xas_set(xas
, first
);
1529 while ((first
& XA_CHUNK_MASK
) == 0) {
1530 if (sibs
< XA_CHUNK_MASK
)
1532 if ((sibs
== XA_CHUNK_MASK
) && (offset
< XA_CHUNK_MASK
))
1534 shift
+= XA_CHUNK_SHIFT
;
1535 if (offset
== XA_CHUNK_MASK
)
1536 offset
= sibs
& XA_CHUNK_MASK
;
1537 sibs
>>= XA_CHUNK_SHIFT
;
1538 first
>>= XA_CHUNK_SHIFT
;
1541 offset
= first
& XA_CHUNK_MASK
;
1542 if (offset
+ sibs
> XA_CHUNK_MASK
)
1543 sibs
= XA_CHUNK_MASK
- offset
;
1544 if ((((first
+ sibs
+ 1) << shift
) - 1) > last
)
1547 xas
->xa_shift
= shift
;
1548 xas
->xa_sibs
= sibs
;
1552 * xa_store_range() - Store this entry at a range of indices in the XArray.
1554 * @first: First index to affect.
1555 * @last: Last index to affect.
1556 * @entry: New entry.
1557 * @gfp: Memory allocation flags.
1559 * After this function returns, loads from any index between @first and @last,
1560 * inclusive will return @entry.
1561 * Storing into an existing multislot entry updates the entry of every index.
1562 * The marks associated with @index are unaffected unless @entry is %NULL.
1564 * Context: Process context. Takes and releases the xa_lock. May sleep
1565 * if the @gfp flags permit.
1566 * Return: %NULL on success, xa_err(-EINVAL) if @entry cannot be stored in
1567 * an XArray, or xa_err(-ENOMEM) if memory allocation failed.
1569 void *xa_store_range(struct xarray
*xa
, unsigned long first
,
1570 unsigned long last
, void *entry
, gfp_t gfp
)
1572 XA_STATE(xas
, xa
, 0);
1574 if (WARN_ON_ONCE(xa_is_internal(entry
)))
1575 return XA_ERROR(-EINVAL
);
1577 return XA_ERROR(-EINVAL
);
1582 unsigned int order
= BITS_PER_LONG
;
1584 order
= __ffs(last
+ 1);
1585 xas_set_order(&xas
, last
, order
);
1586 xas_create(&xas
, true);
1587 if (xas_error(&xas
))
1591 xas_set_range(&xas
, first
, last
);
1592 xas_store(&xas
, entry
);
1593 if (xas_error(&xas
))
1595 first
+= xas_size(&xas
);
1596 } while (first
<= last
);
1599 } while (xas_nomem(&xas
, gfp
));
1601 return xas_result(&xas
, NULL
);
1603 EXPORT_SYMBOL(xa_store_range
);
1604 #endif /* CONFIG_XARRAY_MULTI */
1607 * __xa_alloc() - Find somewhere to store this entry in the XArray.
1609 * @id: Pointer to ID.
1610 * @max: Maximum ID to allocate (inclusive).
1611 * @entry: New entry.
1612 * @gfp: Memory allocation flags.
1614 * Allocates an unused ID in the range specified by @id and @max.
1615 * Updates the @id pointer with the index, then stores the entry at that
1616 * index. A concurrent lookup will not see an uninitialised @id.
1618 * Context: Any context. Expects xa_lock to be held on entry. May
1619 * release and reacquire xa_lock if @gfp flags permit.
1620 * Return: 0 on success, -ENOMEM if memory allocation fails or -ENOSPC if
1621 * there is no more space in the XArray.
1623 int __xa_alloc(struct xarray
*xa
, u32
*id
, u32 max
, void *entry
, gfp_t gfp
)
1625 XA_STATE(xas
, xa
, 0);
1628 if (WARN_ON_ONCE(xa_is_advanced(entry
)))
1630 if (WARN_ON_ONCE(!xa_track_free(xa
)))
1634 entry
= XA_ZERO_ENTRY
;
1638 xas_find_marked(&xas
, max
, XA_FREE_MARK
);
1639 if (xas
.xa_node
== XAS_RESTART
)
1640 xas_set_err(&xas
, -ENOSPC
);
1641 xas_store(&xas
, entry
);
1642 xas_clear_mark(&xas
, XA_FREE_MARK
);
1643 } while (__xas_nomem(&xas
, gfp
));
1645 err
= xas_error(&xas
);
1650 EXPORT_SYMBOL(__xa_alloc
);
1653 * __xa_set_mark() - Set this mark on this entry while locked.
1655 * @index: Index of entry.
1656 * @mark: Mark number.
1658 * Attempting to set a mark on a %NULL entry does not succeed.
1660 * Context: Any context. Expects xa_lock to be held on entry.
1662 void __xa_set_mark(struct xarray
*xa
, unsigned long index
, xa_mark_t mark
)
1664 XA_STATE(xas
, xa
, index
);
1665 void *entry
= xas_load(&xas
);
1668 xas_set_mark(&xas
, mark
);
1670 EXPORT_SYMBOL(__xa_set_mark
);
1673 * __xa_clear_mark() - Clear this mark on this entry while locked.
1675 * @index: Index of entry.
1676 * @mark: Mark number.
1678 * Context: Any context. Expects xa_lock to be held on entry.
1680 void __xa_clear_mark(struct xarray
*xa
, unsigned long index
, xa_mark_t mark
)
1682 XA_STATE(xas
, xa
, index
);
1683 void *entry
= xas_load(&xas
);
1686 xas_clear_mark(&xas
, mark
);
1688 EXPORT_SYMBOL(__xa_clear_mark
);
1691 * xa_get_mark() - Inquire whether this mark is set on this entry.
1693 * @index: Index of entry.
1694 * @mark: Mark number.
1696 * This function uses the RCU read lock, so the result may be out of date
1697 * by the time it returns. If you need the result to be stable, use a lock.
1699 * Context: Any context. Takes and releases the RCU lock.
1700 * Return: True if the entry at @index has this mark set, false if it doesn't.
1702 bool xa_get_mark(struct xarray
*xa
, unsigned long index
, xa_mark_t mark
)
1704 XA_STATE(xas
, xa
, index
);
1708 entry
= xas_start(&xas
);
1709 while (xas_get_mark(&xas
, mark
)) {
1710 if (!xa_is_node(entry
))
1712 entry
= xas_descend(&xas
, xa_to_node(entry
));
1720 EXPORT_SYMBOL(xa_get_mark
);
1723 * xa_set_mark() - Set this mark on this entry.
1725 * @index: Index of entry.
1726 * @mark: Mark number.
1728 * Attempting to set a mark on a %NULL entry does not succeed.
1730 * Context: Process context. Takes and releases the xa_lock.
1732 void xa_set_mark(struct xarray
*xa
, unsigned long index
, xa_mark_t mark
)
1735 __xa_set_mark(xa
, index
, mark
);
1738 EXPORT_SYMBOL(xa_set_mark
);
1741 * xa_clear_mark() - Clear this mark on this entry.
1743 * @index: Index of entry.
1744 * @mark: Mark number.
1746 * Clearing a mark always succeeds.
1748 * Context: Process context. Takes and releases the xa_lock.
1750 void xa_clear_mark(struct xarray
*xa
, unsigned long index
, xa_mark_t mark
)
1753 __xa_clear_mark(xa
, index
, mark
);
1756 EXPORT_SYMBOL(xa_clear_mark
);
1759 * xa_find() - Search the XArray for an entry.
1761 * @indexp: Pointer to an index.
1762 * @max: Maximum index to search to.
1763 * @filter: Selection criterion.
1765 * Finds the entry in @xa which matches the @filter, and has the lowest
1766 * index that is at least @indexp and no more than @max.
1767 * If an entry is found, @indexp is updated to be the index of the entry.
1768 * This function is protected by the RCU read lock, so it may not find
1769 * entries which are being simultaneously added. It will not return an
1770 * %XA_RETRY_ENTRY; if you need to see retry entries, use xas_find().
1772 * Context: Any context. Takes and releases the RCU lock.
1773 * Return: The entry, if found, otherwise %NULL.
1775 void *xa_find(struct xarray
*xa
, unsigned long *indexp
,
1776 unsigned long max
, xa_mark_t filter
)
1778 XA_STATE(xas
, xa
, *indexp
);
1783 if ((__force
unsigned int)filter
< XA_MAX_MARKS
)
1784 entry
= xas_find_marked(&xas
, max
, filter
);
1786 entry
= xas_find(&xas
, max
);
1787 } while (xas_retry(&xas
, entry
));
1791 *indexp
= xas
.xa_index
;
1794 EXPORT_SYMBOL(xa_find
);
1797 * xa_find_after() - Search the XArray for a present entry.
1799 * @indexp: Pointer to an index.
1800 * @max: Maximum index to search to.
1801 * @filter: Selection criterion.
1803 * Finds the entry in @xa which matches the @filter and has the lowest
1804 * index that is above @indexp and no more than @max.
1805 * If an entry is found, @indexp is updated to be the index of the entry.
1806 * This function is protected by the RCU read lock, so it may miss entries
1807 * which are being simultaneously added. It will not return an
1808 * %XA_RETRY_ENTRY; if you need to see retry entries, use xas_find().
1810 * Context: Any context. Takes and releases the RCU lock.
1811 * Return: The pointer, if found, otherwise %NULL.
1813 void *xa_find_after(struct xarray
*xa
, unsigned long *indexp
,
1814 unsigned long max
, xa_mark_t filter
)
1816 XA_STATE(xas
, xa
, *indexp
+ 1);
1821 if ((__force
unsigned int)filter
< XA_MAX_MARKS
)
1822 entry
= xas_find_marked(&xas
, max
, filter
);
1824 entry
= xas_find(&xas
, max
);
1825 if (xas
.xa_node
== XAS_BOUNDS
)
1828 if (xas
.xa_index
& ((1UL << xas
.xa_shift
) - 1))
1831 if (xas
.xa_offset
< (xas
.xa_index
& XA_CHUNK_MASK
))
1834 if (!xas_retry(&xas
, entry
))
1840 *indexp
= xas
.xa_index
;
1843 EXPORT_SYMBOL(xa_find_after
);
1845 static unsigned int xas_extract_present(struct xa_state
*xas
, void **dst
,
1846 unsigned long max
, unsigned int n
)
1852 xas_for_each(xas
, entry
, max
) {
1853 if (xas_retry(xas
, entry
))
1864 static unsigned int xas_extract_marked(struct xa_state
*xas
, void **dst
,
1865 unsigned long max
, unsigned int n
, xa_mark_t mark
)
1871 xas_for_each_marked(xas
, entry
, max
, mark
) {
1872 if (xas_retry(xas
, entry
))
1884 * xa_extract() - Copy selected entries from the XArray into a normal array.
1885 * @xa: The source XArray to copy from.
1886 * @dst: The buffer to copy entries into.
1887 * @start: The first index in the XArray eligible to be selected.
1888 * @max: The last index in the XArray eligible to be selected.
1889 * @n: The maximum number of entries to copy.
1890 * @filter: Selection criterion.
1892 * Copies up to @n entries that match @filter from the XArray. The
1893 * copied entries will have indices between @start and @max, inclusive.
1895 * The @filter may be an XArray mark value, in which case entries which are
1896 * marked with that mark will be copied. It may also be %XA_PRESENT, in
1897 * which case all entries which are not %NULL will be copied.
1899 * The entries returned may not represent a snapshot of the XArray at a
1900 * moment in time. For example, if another thread stores to index 5, then
1901 * index 10, calling xa_extract() may return the old contents of index 5
1902 * and the new contents of index 10. Indices not modified while this
1903 * function is running will not be skipped.
1905 * If you need stronger guarantees, holding the xa_lock across calls to this
1906 * function will prevent concurrent modification.
1908 * Context: Any context. Takes and releases the RCU lock.
1909 * Return: The number of entries copied.
1911 unsigned int xa_extract(struct xarray
*xa
, void **dst
, unsigned long start
,
1912 unsigned long max
, unsigned int n
, xa_mark_t filter
)
1914 XA_STATE(xas
, xa
, start
);
1919 if ((__force
unsigned int)filter
< XA_MAX_MARKS
)
1920 return xas_extract_marked(&xas
, dst
, max
, n
, filter
);
1921 return xas_extract_present(&xas
, dst
, max
, n
);
1923 EXPORT_SYMBOL(xa_extract
);
1926 * xa_destroy() - Free all internal data structures.
1929 * After calling this function, the XArray is empty and has freed all memory
1930 * allocated for its internal data structures. You are responsible for
1931 * freeing the objects referenced by the XArray.
1933 * Context: Any context. Takes and releases the xa_lock, interrupt-safe.
1935 void xa_destroy(struct xarray
*xa
)
1937 XA_STATE(xas
, xa
, 0);
1938 unsigned long flags
;
1942 xas_lock_irqsave(&xas
, flags
);
1943 entry
= xa_head_locked(xa
);
1944 RCU_INIT_POINTER(xa
->xa_head
, NULL
);
1945 xas_init_marks(&xas
);
1946 /* lockdep checks we're still holding the lock in xas_free_nodes() */
1947 if (xa_is_node(entry
))
1948 xas_free_nodes(&xas
, xa_to_node(entry
));
1949 xas_unlock_irqrestore(&xas
, flags
);
1951 EXPORT_SYMBOL(xa_destroy
);
1954 void xa_dump_node(const struct xa_node
*node
)
1960 if ((unsigned long)node
& 3) {
1961 pr_cont("node %px\n", node
);
1965 pr_cont("node %px %s %d parent %px shift %d count %d values %d "
1966 "array %px list %px %px marks",
1967 node
, node
->parent
? "offset" : "max", node
->offset
,
1968 node
->parent
, node
->shift
, node
->count
, node
->nr_values
,
1969 node
->array
, node
->private_list
.prev
, node
->private_list
.next
);
1970 for (i
= 0; i
< XA_MAX_MARKS
; i
++)
1971 for (j
= 0; j
< XA_MARK_LONGS
; j
++)
1972 pr_cont(" %lx", node
->marks
[i
][j
]);
1976 void xa_dump_index(unsigned long index
, unsigned int shift
)
1979 pr_info("%lu: ", index
);
1980 else if (shift
>= BITS_PER_LONG
)
1981 pr_info("0-%lu: ", ~0UL);
1983 pr_info("%lu-%lu: ", index
, index
| ((1UL << shift
) - 1));
1986 void xa_dump_entry(const void *entry
, unsigned long index
, unsigned long shift
)
1991 xa_dump_index(index
, shift
);
1993 if (xa_is_node(entry
)) {
1995 pr_cont("%px\n", entry
);
1998 struct xa_node
*node
= xa_to_node(entry
);
2000 for (i
= 0; i
< XA_CHUNK_SIZE
; i
++)
2001 xa_dump_entry(node
->slots
[i
],
2002 index
+ (i
<< node
->shift
), node
->shift
);
2004 } else if (xa_is_value(entry
))
2005 pr_cont("value %ld (0x%lx) [%px]\n", xa_to_value(entry
),
2006 xa_to_value(entry
), entry
);
2007 else if (!xa_is_internal(entry
))
2008 pr_cont("%px\n", entry
);
2009 else if (xa_is_retry(entry
))
2010 pr_cont("retry (%ld)\n", xa_to_internal(entry
));
2011 else if (xa_is_sibling(entry
))
2012 pr_cont("sibling (slot %ld)\n", xa_to_sibling(entry
));
2013 else if (xa_is_zero(entry
))
2014 pr_cont("zero (%ld)\n", xa_to_internal(entry
));
2016 pr_cont("UNKNOWN ENTRY (%px)\n", entry
);
2019 void xa_dump(const struct xarray
*xa
)
2021 void *entry
= xa
->xa_head
;
2022 unsigned int shift
= 0;
2024 pr_info("xarray: %px head %px flags %x marks %d %d %d\n", xa
, entry
,
2025 xa
->xa_flags
, xa_marked(xa
, XA_MARK_0
),
2026 xa_marked(xa
, XA_MARK_1
), xa_marked(xa
, XA_MARK_2
));
2027 if (xa_is_node(entry
))
2028 shift
= xa_to_node(entry
)->shift
+ XA_CHUNK_SHIFT
;
2029 xa_dump_entry(entry
, 0, shift
);