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
);
238 EXPORT_SYMBOL_GPL(xas_load
);
240 /* Move the radix tree node cache here */
241 extern struct kmem_cache
*radix_tree_node_cachep
;
242 extern void radix_tree_node_rcu_free(struct rcu_head
*head
);
244 #define XA_RCU_FREE ((struct xarray *)1)
246 static void xa_node_free(struct xa_node
*node
)
248 XA_NODE_BUG_ON(node
, !list_empty(&node
->private_list
));
249 node
->array
= XA_RCU_FREE
;
250 call_rcu(&node
->rcu_head
, radix_tree_node_rcu_free
);
254 * xas_destroy() - Free any resources allocated during the XArray operation.
255 * @xas: XArray operation state.
257 * This function is now internal-only.
259 static void xas_destroy(struct xa_state
*xas
)
261 struct xa_node
*node
= xas
->xa_alloc
;
265 XA_NODE_BUG_ON(node
, !list_empty(&node
->private_list
));
266 kmem_cache_free(radix_tree_node_cachep
, node
);
267 xas
->xa_alloc
= NULL
;
271 * xas_nomem() - Allocate memory if needed.
272 * @xas: XArray operation state.
273 * @gfp: Memory allocation flags.
275 * If we need to add new nodes to the XArray, we try to allocate memory
276 * with GFP_NOWAIT while holding the lock, which will usually succeed.
277 * If it fails, @xas is flagged as needing memory to continue. The caller
278 * should drop the lock and call xas_nomem(). If xas_nomem() succeeds,
279 * the caller should retry the operation.
281 * Forward progress is guaranteed as one node is allocated here and
282 * stored in the xa_state where it will be found by xas_alloc(). More
283 * nodes will likely be found in the slab allocator, but we do not tie
286 * Return: true if memory was needed, and was successfully allocated.
288 bool xas_nomem(struct xa_state
*xas
, gfp_t gfp
)
290 if (xas
->xa_node
!= XA_ERROR(-ENOMEM
)) {
294 xas
->xa_alloc
= kmem_cache_alloc(radix_tree_node_cachep
, gfp
);
297 XA_NODE_BUG_ON(xas
->xa_alloc
, !list_empty(&xas
->xa_alloc
->private_list
));
298 xas
->xa_node
= XAS_RESTART
;
301 EXPORT_SYMBOL_GPL(xas_nomem
);
304 * __xas_nomem() - Drop locks and allocate memory if needed.
305 * @xas: XArray operation state.
306 * @gfp: Memory allocation flags.
308 * Internal variant of xas_nomem().
310 * Return: true if memory was needed, and was successfully allocated.
312 static bool __xas_nomem(struct xa_state
*xas
, gfp_t gfp
)
313 __must_hold(xas
->xa
->xa_lock
)
315 unsigned int lock_type
= xa_lock_type(xas
->xa
);
317 if (xas
->xa_node
!= XA_ERROR(-ENOMEM
)) {
321 if (gfpflags_allow_blocking(gfp
)) {
322 xas_unlock_type(xas
, lock_type
);
323 xas
->xa_alloc
= kmem_cache_alloc(radix_tree_node_cachep
, gfp
);
324 xas_lock_type(xas
, lock_type
);
326 xas
->xa_alloc
= kmem_cache_alloc(radix_tree_node_cachep
, gfp
);
330 XA_NODE_BUG_ON(xas
->xa_alloc
, !list_empty(&xas
->xa_alloc
->private_list
));
331 xas
->xa_node
= XAS_RESTART
;
335 static void xas_update(struct xa_state
*xas
, struct xa_node
*node
)
338 xas
->xa_update(node
);
340 XA_NODE_BUG_ON(node
, !list_empty(&node
->private_list
));
343 static void *xas_alloc(struct xa_state
*xas
, unsigned int shift
)
345 struct xa_node
*parent
= xas
->xa_node
;
346 struct xa_node
*node
= xas
->xa_alloc
;
348 if (xas_invalid(xas
))
352 xas
->xa_alloc
= NULL
;
354 node
= kmem_cache_alloc(radix_tree_node_cachep
,
355 GFP_NOWAIT
| __GFP_NOWARN
);
357 xas_set_err(xas
, -ENOMEM
);
363 node
->offset
= xas
->xa_offset
;
365 XA_NODE_BUG_ON(node
, parent
->count
> XA_CHUNK_SIZE
);
366 xas_update(xas
, parent
);
368 XA_NODE_BUG_ON(node
, shift
> BITS_PER_LONG
);
369 XA_NODE_BUG_ON(node
, !list_empty(&node
->private_list
));
373 RCU_INIT_POINTER(node
->parent
, xas
->xa_node
);
374 node
->array
= xas
->xa
;
379 #ifdef CONFIG_XARRAY_MULTI
380 /* Returns the number of indices covered by a given xa_state */
381 static unsigned long xas_size(const struct xa_state
*xas
)
383 return (xas
->xa_sibs
+ 1UL) << xas
->xa_shift
;
388 * Use this to calculate the maximum index that will need to be created
389 * in order to add the entry described by @xas. Because we cannot store a
390 * multiple-index entry at index 0, the calculation is a little more complex
391 * than you might expect.
393 static unsigned long xas_max(struct xa_state
*xas
)
395 unsigned long max
= xas
->xa_index
;
397 #ifdef CONFIG_XARRAY_MULTI
398 if (xas
->xa_shift
|| xas
->xa_sibs
) {
399 unsigned long mask
= xas_size(xas
) - 1;
409 /* The maximum index that can be contained in the array without expanding it */
410 static unsigned long max_index(void *entry
)
412 if (!xa_is_node(entry
))
414 return (XA_CHUNK_SIZE
<< xa_to_node(entry
)->shift
) - 1;
417 static void xas_shrink(struct xa_state
*xas
)
419 struct xarray
*xa
= xas
->xa
;
420 struct xa_node
*node
= xas
->xa_node
;
425 XA_NODE_BUG_ON(node
, node
->count
> XA_CHUNK_SIZE
);
426 if (node
->count
!= 1)
428 entry
= xa_entry_locked(xa
, node
, 0);
431 if (!xa_is_node(entry
) && node
->shift
)
433 xas
->xa_node
= XAS_BOUNDS
;
435 RCU_INIT_POINTER(xa
->xa_head
, entry
);
436 if (xa_track_free(xa
) && !node_get_mark(node
, 0, XA_FREE_MARK
))
437 xa_mark_clear(xa
, XA_FREE_MARK
);
441 if (!xa_is_node(entry
))
442 RCU_INIT_POINTER(node
->slots
[0], XA_RETRY_ENTRY
);
443 xas_update(xas
, node
);
445 if (!xa_is_node(entry
))
447 node
= xa_to_node(entry
);
453 * xas_delete_node() - Attempt to delete an xa_node
454 * @xas: Array operation state.
456 * Attempts to delete the @xas->xa_node. This will fail if xa->node has
457 * a non-zero reference count.
459 static void xas_delete_node(struct xa_state
*xas
)
461 struct xa_node
*node
= xas
->xa_node
;
464 struct xa_node
*parent
;
466 XA_NODE_BUG_ON(node
, node
->count
> XA_CHUNK_SIZE
);
470 parent
= xa_parent_locked(xas
->xa
, node
);
471 xas
->xa_node
= parent
;
472 xas
->xa_offset
= node
->offset
;
476 xas
->xa
->xa_head
= NULL
;
477 xas
->xa_node
= XAS_BOUNDS
;
481 parent
->slots
[xas
->xa_offset
] = NULL
;
483 XA_NODE_BUG_ON(parent
, parent
->count
> XA_CHUNK_SIZE
);
485 xas_update(xas
, node
);
493 * xas_free_nodes() - Free this node and all nodes that it references
494 * @xas: Array operation state.
497 * This node has been removed from the tree. We must now free it and all
498 * of its subnodes. There may be RCU walkers with references into the tree,
499 * so we must replace all entries with retry markers.
501 static void xas_free_nodes(struct xa_state
*xas
, struct xa_node
*top
)
503 unsigned int offset
= 0;
504 struct xa_node
*node
= top
;
507 void *entry
= xa_entry_locked(xas
->xa
, node
, offset
);
509 if (xa_is_node(entry
)) {
510 node
= xa_to_node(entry
);
515 RCU_INIT_POINTER(node
->slots
[offset
], XA_RETRY_ENTRY
);
517 while (offset
== XA_CHUNK_SIZE
) {
518 struct xa_node
*parent
;
520 parent
= xa_parent_locked(xas
->xa
, node
);
521 offset
= node
->offset
+ 1;
524 xas_update(xas
, node
);
534 * xas_expand adds nodes to the head of the tree until it has reached
535 * sufficient height to be able to contain @xas->xa_index
537 static int xas_expand(struct xa_state
*xas
, void *head
)
539 struct xarray
*xa
= xas
->xa
;
540 struct xa_node
*node
= NULL
;
541 unsigned int shift
= 0;
542 unsigned long max
= xas_max(xas
);
547 while ((max
>> shift
) >= XA_CHUNK_SIZE
)
548 shift
+= XA_CHUNK_SHIFT
;
549 return shift
+ XA_CHUNK_SHIFT
;
550 } else if (xa_is_node(head
)) {
551 node
= xa_to_node(head
);
552 shift
= node
->shift
+ XA_CHUNK_SHIFT
;
556 while (max
> max_index(head
)) {
559 XA_NODE_BUG_ON(node
, shift
> BITS_PER_LONG
);
560 node
= xas_alloc(xas
, shift
);
565 if (xa_is_value(head
))
567 RCU_INIT_POINTER(node
->slots
[0], head
);
569 /* Propagate the aggregated mark info to the new child */
571 if (xa_track_free(xa
) && mark
== XA_FREE_MARK
) {
572 node_mark_all(node
, XA_FREE_MARK
);
573 if (!xa_marked(xa
, XA_FREE_MARK
)) {
574 node_clear_mark(node
, 0, XA_FREE_MARK
);
575 xa_mark_set(xa
, XA_FREE_MARK
);
577 } else if (xa_marked(xa
, mark
)) {
578 node_set_mark(node
, 0, mark
);
580 if (mark
== XA_MARK_MAX
)
586 * Now that the new node is fully initialised, we can add
589 if (xa_is_node(head
)) {
590 xa_to_node(head
)->offset
= 0;
591 rcu_assign_pointer(xa_to_node(head
)->parent
, node
);
593 head
= xa_mk_node(node
);
594 rcu_assign_pointer(xa
->xa_head
, head
);
595 xas_update(xas
, node
);
597 shift
+= XA_CHUNK_SHIFT
;
605 * xas_create() - Create a slot to store an entry in.
606 * @xas: XArray operation state.
608 * Most users will not need to call this function directly, as it is called
609 * by xas_store(). It is useful for doing conditional store operations
610 * (see the xa_cmpxchg() implementation for an example).
612 * Return: If the slot already existed, returns the contents of this slot.
613 * If the slot was newly created, returns NULL. If it failed to create the
614 * slot, returns NULL and indicates the error in @xas.
616 static void *xas_create(struct xa_state
*xas
)
618 struct xarray
*xa
= xas
->xa
;
621 struct xa_node
*node
= xas
->xa_node
;
623 unsigned int order
= xas
->xa_shift
;
626 entry
= xa_head_locked(xa
);
628 shift
= xas_expand(xas
, entry
);
631 entry
= xa_head_locked(xa
);
633 } else if (xas_error(xas
)) {
636 unsigned int offset
= xas
->xa_offset
;
639 entry
= xa_entry_locked(xa
, node
, offset
);
640 slot
= &node
->slots
[offset
];
643 entry
= xa_head_locked(xa
);
647 while (shift
> order
) {
648 shift
-= XA_CHUNK_SHIFT
;
650 node
= xas_alloc(xas
, shift
);
653 if (xa_track_free(xa
))
654 node_mark_all(node
, XA_FREE_MARK
);
655 rcu_assign_pointer(*slot
, xa_mk_node(node
));
656 } else if (xa_is_node(entry
)) {
657 node
= xa_to_node(entry
);
661 entry
= xas_descend(xas
, node
);
662 slot
= &node
->slots
[xas
->xa_offset
];
669 * xas_create_range() - Ensure that stores to this range will succeed
670 * @xas: XArray operation state.
672 * Creates all of the slots in the range covered by @xas. Sets @xas to
673 * create single-index entries and positions it at the beginning of the
674 * range. This is for the benefit of users which have not yet been
675 * converted to use multi-index entries.
677 void xas_create_range(struct xa_state
*xas
)
679 unsigned long index
= xas
->xa_index
;
680 unsigned char shift
= xas
->xa_shift
;
681 unsigned char sibs
= xas
->xa_sibs
;
683 xas
->xa_index
|= ((sibs
+ 1) << shift
) - 1;
684 if (xas_is_node(xas
) && xas
->xa_node
->shift
== xas
->xa_shift
)
685 xas
->xa_offset
|= sibs
;
693 if (xas
->xa_index
<= (index
| XA_CHUNK_MASK
))
695 xas
->xa_index
-= XA_CHUNK_SIZE
;
698 struct xa_node
*node
= xas
->xa_node
;
699 xas
->xa_node
= xa_parent_locked(xas
->xa
, node
);
700 xas
->xa_offset
= node
->offset
- 1;
701 if (node
->offset
!= 0)
707 xas
->xa_shift
= shift
;
709 xas
->xa_index
= index
;
712 xas
->xa_index
= index
;
716 EXPORT_SYMBOL_GPL(xas_create_range
);
718 static void update_node(struct xa_state
*xas
, struct xa_node
*node
,
719 int count
, int values
)
721 if (!node
|| (!count
&& !values
))
724 node
->count
+= count
;
725 node
->nr_values
+= values
;
726 XA_NODE_BUG_ON(node
, node
->count
> XA_CHUNK_SIZE
);
727 XA_NODE_BUG_ON(node
, node
->nr_values
> XA_CHUNK_SIZE
);
728 xas_update(xas
, node
);
730 xas_delete_node(xas
);
734 * xas_store() - Store this entry in the XArray.
735 * @xas: XArray operation state.
738 * If @xas is operating on a multi-index entry, the entry returned by this
739 * function is essentially meaningless (it may be an internal entry or it
740 * may be %NULL, even if there are non-NULL entries at some of the indices
741 * covered by the range). This is not a problem for any current users,
742 * and can be changed if needed.
744 * Return: The old entry at this index.
746 void *xas_store(struct xa_state
*xas
, void *entry
)
748 struct xa_node
*node
;
749 void __rcu
**slot
= &xas
->xa
->xa_head
;
750 unsigned int offset
, max
;
754 bool value
= xa_is_value(entry
);
757 first
= xas_create(xas
);
759 first
= xas_load(xas
);
761 if (xas_invalid(xas
))
764 if (node
&& (xas
->xa_shift
< node
->shift
))
766 if ((first
== entry
) && !xas
->xa_sibs
)
770 offset
= xas
->xa_offset
;
771 max
= xas
->xa_offset
+ xas
->xa_sibs
;
773 slot
= &node
->slots
[offset
];
775 xas_squash_marks(xas
);
782 * Must clear the marks before setting the entry to NULL,
783 * otherwise xas_for_each_marked may find a NULL entry and
784 * stop early. rcu_assign_pointer contains a release barrier
785 * so the mark clearing will appear to happen before the
786 * entry is set to NULL.
788 rcu_assign_pointer(*slot
, entry
);
789 if (xa_is_node(next
))
790 xas_free_nodes(xas
, xa_to_node(next
));
793 count
+= !next
- !entry
;
794 values
+= !xa_is_value(first
) - !value
;
798 if (!xa_is_sibling(entry
))
799 entry
= xa_mk_sibling(xas
->xa_offset
);
801 if (offset
== XA_CHUNK_MASK
)
804 next
= xa_entry_locked(xas
->xa
, node
, ++offset
);
805 if (!xa_is_sibling(next
)) {
806 if (!entry
&& (offset
> max
))
813 update_node(xas
, node
, count
, values
);
816 EXPORT_SYMBOL_GPL(xas_store
);
819 * xas_get_mark() - Returns the state of this mark.
820 * @xas: XArray operation state.
821 * @mark: Mark number.
823 * Return: true if the mark is set, false if the mark is clear or @xas
824 * is in an error state.
826 bool xas_get_mark(const struct xa_state
*xas
, xa_mark_t mark
)
828 if (xas_invalid(xas
))
831 return xa_marked(xas
->xa
, mark
);
832 return node_get_mark(xas
->xa_node
, xas
->xa_offset
, mark
);
834 EXPORT_SYMBOL_GPL(xas_get_mark
);
837 * xas_set_mark() - Sets the mark on this entry and its parents.
838 * @xas: XArray operation state.
839 * @mark: Mark number.
841 * Sets the specified mark on this entry, and walks up the tree setting it
842 * on all the ancestor entries. Does nothing if @xas has not been walked to
843 * an entry, or is in an error state.
845 void xas_set_mark(const struct xa_state
*xas
, xa_mark_t mark
)
847 struct xa_node
*node
= xas
->xa_node
;
848 unsigned int offset
= xas
->xa_offset
;
850 if (xas_invalid(xas
))
854 if (node_set_mark(node
, offset
, mark
))
856 offset
= node
->offset
;
857 node
= xa_parent_locked(xas
->xa
, node
);
860 if (!xa_marked(xas
->xa
, mark
))
861 xa_mark_set(xas
->xa
, mark
);
863 EXPORT_SYMBOL_GPL(xas_set_mark
);
866 * xas_clear_mark() - Clears the mark on this entry and its parents.
867 * @xas: XArray operation state.
868 * @mark: Mark number.
870 * Clears the specified mark on this entry, and walks back to the head
871 * attempting to clear it on all the ancestor entries. Does nothing if
872 * @xas has not been walked to an entry, or is in an error state.
874 void xas_clear_mark(const struct xa_state
*xas
, xa_mark_t mark
)
876 struct xa_node
*node
= xas
->xa_node
;
877 unsigned int offset
= xas
->xa_offset
;
879 if (xas_invalid(xas
))
883 if (!node_clear_mark(node
, offset
, mark
))
885 if (node_any_mark(node
, mark
))
888 offset
= node
->offset
;
889 node
= xa_parent_locked(xas
->xa
, node
);
892 if (xa_marked(xas
->xa
, mark
))
893 xa_mark_clear(xas
->xa
, mark
);
895 EXPORT_SYMBOL_GPL(xas_clear_mark
);
898 * xas_init_marks() - Initialise all marks for the entry
899 * @xas: Array operations state.
901 * Initialise all marks for the entry specified by @xas. If we're tracking
902 * free entries with a mark, we need to set it on all entries. All other
905 * This implementation is not as efficient as it could be; we may walk
906 * up the tree multiple times.
908 void xas_init_marks(const struct xa_state
*xas
)
913 if (xa_track_free(xas
->xa
) && mark
== XA_FREE_MARK
)
914 xas_set_mark(xas
, mark
);
916 xas_clear_mark(xas
, mark
);
917 if (mark
== XA_MARK_MAX
)
922 EXPORT_SYMBOL_GPL(xas_init_marks
);
925 * xas_pause() - Pause a walk to drop a lock.
926 * @xas: XArray operation state.
928 * Some users need to pause a walk and drop the lock they're holding in
929 * order to yield to a higher priority thread or carry out an operation
930 * on an entry. Those users should call this function before they drop
931 * the lock. It resets the @xas to be suitable for the next iteration
932 * of the loop after the user has reacquired the lock. If most entries
933 * found during a walk require you to call xas_pause(), the xa_for_each()
934 * iterator may be more appropriate.
936 * Note that xas_pause() only works for forward iteration. If a user needs
937 * to pause a reverse iteration, we will need a xas_pause_rev().
939 void xas_pause(struct xa_state
*xas
)
941 struct xa_node
*node
= xas
->xa_node
;
943 if (xas_invalid(xas
))
947 unsigned int offset
= xas
->xa_offset
;
948 while (++offset
< XA_CHUNK_SIZE
) {
949 if (!xa_is_sibling(xa_entry(xas
->xa
, node
, offset
)))
952 xas
->xa_index
+= (offset
- xas
->xa_offset
) << node
->shift
;
956 xas
->xa_node
= XAS_RESTART
;
958 EXPORT_SYMBOL_GPL(xas_pause
);
961 * __xas_prev() - Find the previous entry in the XArray.
962 * @xas: XArray operation state.
964 * Helper function for xas_prev() which handles all the complex cases
967 void *__xas_prev(struct xa_state
*xas
)
971 if (!xas_frozen(xas
->xa_node
))
973 if (xas_not_node(xas
->xa_node
))
974 return xas_load(xas
);
976 if (xas
->xa_offset
!= get_offset(xas
->xa_index
, xas
->xa_node
))
979 while (xas
->xa_offset
== 255) {
980 xas
->xa_offset
= xas
->xa_node
->offset
- 1;
981 xas
->xa_node
= xa_parent(xas
->xa
, xas
->xa_node
);
983 return set_bounds(xas
);
987 entry
= xa_entry(xas
->xa
, xas
->xa_node
, xas
->xa_offset
);
988 if (!xa_is_node(entry
))
991 xas
->xa_node
= xa_to_node(entry
);
995 EXPORT_SYMBOL_GPL(__xas_prev
);
998 * __xas_next() - Find the next entry in the XArray.
999 * @xas: XArray operation state.
1001 * Helper function for xas_next() which handles all the complex cases
1004 void *__xas_next(struct xa_state
*xas
)
1008 if (!xas_frozen(xas
->xa_node
))
1010 if (xas_not_node(xas
->xa_node
))
1011 return xas_load(xas
);
1013 if (xas
->xa_offset
!= get_offset(xas
->xa_index
, xas
->xa_node
))
1016 while (xas
->xa_offset
== XA_CHUNK_SIZE
) {
1017 xas
->xa_offset
= xas
->xa_node
->offset
+ 1;
1018 xas
->xa_node
= xa_parent(xas
->xa
, xas
->xa_node
);
1020 return set_bounds(xas
);
1024 entry
= xa_entry(xas
->xa
, xas
->xa_node
, xas
->xa_offset
);
1025 if (!xa_is_node(entry
))
1028 xas
->xa_node
= xa_to_node(entry
);
1029 xas_set_offset(xas
);
1032 EXPORT_SYMBOL_GPL(__xas_next
);
1035 * xas_find() - Find the next present entry in the XArray.
1036 * @xas: XArray operation state.
1037 * @max: Highest index to return.
1039 * If the @xas has not yet been walked to an entry, return the entry
1040 * which has an index >= xas.xa_index. If it has been walked, the entry
1041 * currently being pointed at has been processed, and so we move to the
1044 * If no entry is found and the array is smaller than @max, the iterator
1045 * is set to the smallest index not yet in the array. This allows @xas
1046 * to be immediately passed to xas_store().
1048 * Return: The entry, if found, otherwise %NULL.
1050 void *xas_find(struct xa_state
*xas
, unsigned long max
)
1057 if (!xas
->xa_node
) {
1059 return set_bounds(xas
);
1060 } else if (xas_top(xas
->xa_node
)) {
1061 entry
= xas_load(xas
);
1062 if (entry
|| xas_not_node(xas
->xa_node
))
1064 } else if (!xas
->xa_node
->shift
&&
1065 xas
->xa_offset
!= (xas
->xa_index
& XA_CHUNK_MASK
)) {
1066 xas
->xa_offset
= ((xas
->xa_index
- 1) & XA_CHUNK_MASK
) + 1;
1071 while (xas
->xa_node
&& (xas
->xa_index
<= max
)) {
1072 if (unlikely(xas
->xa_offset
== XA_CHUNK_SIZE
)) {
1073 xas
->xa_offset
= xas
->xa_node
->offset
+ 1;
1074 xas
->xa_node
= xa_parent(xas
->xa
, xas
->xa_node
);
1078 entry
= xa_entry(xas
->xa
, xas
->xa_node
, xas
->xa_offset
);
1079 if (xa_is_node(entry
)) {
1080 xas
->xa_node
= xa_to_node(entry
);
1084 if (entry
&& !xa_is_sibling(entry
))
1091 xas
->xa_node
= XAS_BOUNDS
;
1094 EXPORT_SYMBOL_GPL(xas_find
);
1097 * xas_find_marked() - Find the next marked entry in the XArray.
1098 * @xas: XArray operation state.
1099 * @max: Highest index to return.
1100 * @mark: Mark number to search for.
1102 * If the @xas has not yet been walked to an entry, return the marked 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 return the
1105 * first marked entry with an index > xas.xa_index.
1107 * If no marked entry is found and the array is smaller than @max, @xas is
1108 * set to the bounds state and xas->xa_index is set to the smallest index
1109 * not yet in the array. This allows @xas to be immediately passed to
1112 * If no entry is found before @max is reached, @xas is set to the restart
1115 * Return: The entry, if found, otherwise %NULL.
1117 void *xas_find_marked(struct xa_state
*xas
, unsigned long max
, xa_mark_t mark
)
1119 bool advance
= true;
1120 unsigned int offset
;
1126 if (!xas
->xa_node
) {
1129 } else if (xas_top(xas
->xa_node
)) {
1131 entry
= xa_head(xas
->xa
);
1132 xas
->xa_node
= NULL
;
1133 if (xas
->xa_index
> max_index(entry
))
1135 if (!xa_is_node(entry
)) {
1136 if (xa_marked(xas
->xa
, mark
))
1141 xas
->xa_node
= xa_to_node(entry
);
1142 xas
->xa_offset
= xas
->xa_index
>> xas
->xa_node
->shift
;
1145 while (xas
->xa_index
<= max
) {
1146 if (unlikely(xas
->xa_offset
== XA_CHUNK_SIZE
)) {
1147 xas
->xa_offset
= xas
->xa_node
->offset
+ 1;
1148 xas
->xa_node
= xa_parent(xas
->xa
, xas
->xa_node
);
1156 entry
= xa_entry(xas
->xa
, xas
->xa_node
, xas
->xa_offset
);
1157 if (xa_is_sibling(entry
)) {
1158 xas
->xa_offset
= xa_to_sibling(entry
);
1159 xas_move_index(xas
, xas
->xa_offset
);
1163 offset
= xas_find_chunk(xas
, advance
, mark
);
1164 if (offset
> xas
->xa_offset
) {
1166 xas_move_index(xas
, offset
);
1168 if ((xas
->xa_index
- 1) >= max
)
1170 xas
->xa_offset
= offset
;
1171 if (offset
== XA_CHUNK_SIZE
)
1175 entry
= xa_entry(xas
->xa
, xas
->xa_node
, xas
->xa_offset
);
1176 if (!xa_is_node(entry
))
1178 xas
->xa_node
= xa_to_node(entry
);
1179 xas_set_offset(xas
);
1186 xas
->xa_node
= XAS_BOUNDS
;
1189 xas
->xa_node
= XAS_RESTART
;
1192 EXPORT_SYMBOL_GPL(xas_find_marked
);
1195 * xas_find_conflict() - Find the next present entry in a range.
1196 * @xas: XArray operation state.
1198 * The @xas describes both a range and a position within that range.
1200 * Context: Any context. Expects xa_lock to be held.
1201 * Return: The next entry in the range covered by @xas or %NULL.
1203 void *xas_find_conflict(struct xa_state
*xas
)
1213 if (xas_top(xas
->xa_node
)) {
1214 curr
= xas_start(xas
);
1217 while (xa_is_node(curr
)) {
1218 struct xa_node
*node
= xa_to_node(curr
);
1219 curr
= xas_descend(xas
, node
);
1225 if (xas
->xa_node
->shift
> xas
->xa_shift
)
1229 if (xas
->xa_node
->shift
== xas
->xa_shift
) {
1230 if ((xas
->xa_offset
& xas
->xa_sibs
) == xas
->xa_sibs
)
1232 } else if (xas
->xa_offset
== XA_CHUNK_MASK
) {
1233 xas
->xa_offset
= xas
->xa_node
->offset
;
1234 xas
->xa_node
= xa_parent_locked(xas
->xa
, xas
->xa_node
);
1239 curr
= xa_entry_locked(xas
->xa
, xas
->xa_node
, ++xas
->xa_offset
);
1240 if (xa_is_sibling(curr
))
1242 while (xa_is_node(curr
)) {
1243 xas
->xa_node
= xa_to_node(curr
);
1245 curr
= xa_entry_locked(xas
->xa
, xas
->xa_node
, 0);
1250 xas
->xa_offset
-= xas
->xa_sibs
;
1253 EXPORT_SYMBOL_GPL(xas_find_conflict
);
1256 * xa_init_flags() - Initialise an empty XArray with flags.
1258 * @flags: XA_FLAG values.
1260 * If you need to initialise an XArray with special flags (eg you need
1261 * to take the lock from interrupt context), use this function instead
1264 * Context: Any context.
1266 void xa_init_flags(struct xarray
*xa
, gfp_t flags
)
1268 unsigned int lock_type
;
1269 static struct lock_class_key xa_lock_irq
;
1270 static struct lock_class_key xa_lock_bh
;
1272 spin_lock_init(&xa
->xa_lock
);
1273 xa
->xa_flags
= flags
;
1276 lock_type
= xa_lock_type(xa
);
1277 if (lock_type
== XA_LOCK_IRQ
)
1278 lockdep_set_class(&xa
->xa_lock
, &xa_lock_irq
);
1279 else if (lock_type
== XA_LOCK_BH
)
1280 lockdep_set_class(&xa
->xa_lock
, &xa_lock_bh
);
1282 EXPORT_SYMBOL(xa_init_flags
);
1285 * xa_load() - Load an entry from an XArray.
1287 * @index: index into array.
1289 * Context: Any context. Takes and releases the RCU lock.
1290 * Return: The entry at @index in @xa.
1292 void *xa_load(struct xarray
*xa
, unsigned long index
)
1294 XA_STATE(xas
, xa
, index
);
1299 entry
= xas_load(&xas
);
1300 if (xa_is_zero(entry
))
1302 } while (xas_retry(&xas
, entry
));
1307 EXPORT_SYMBOL(xa_load
);
1309 static void *xas_result(struct xa_state
*xas
, void *curr
)
1311 if (xa_is_zero(curr
))
1313 XA_NODE_BUG_ON(xas
->xa_node
, xa_is_internal(curr
));
1315 curr
= xas
->xa_node
;
1320 * __xa_erase() - Erase this entry from the XArray while locked.
1322 * @index: Index into array.
1324 * If the entry at this index is a multi-index entry then all indices will
1325 * be erased, and the entry will no longer be a multi-index entry.
1326 * This function expects the xa_lock to be held on entry.
1328 * Context: Any context. Expects xa_lock to be held on entry. May
1329 * release and reacquire xa_lock if @gfp flags permit.
1330 * Return: The old entry at this index.
1332 void *__xa_erase(struct xarray
*xa
, unsigned long index
)
1334 XA_STATE(xas
, xa
, index
);
1335 return xas_result(&xas
, xas_store(&xas
, NULL
));
1337 EXPORT_SYMBOL(__xa_erase
);
1340 * xa_store() - Store this entry in the XArray.
1342 * @index: Index into array.
1343 * @entry: New entry.
1344 * @gfp: Memory allocation flags.
1346 * After this function returns, loads from this index will return @entry.
1347 * Storing into an existing multislot entry updates the entry of every index.
1348 * The marks associated with @index are unaffected unless @entry is %NULL.
1350 * Context: Process context. Takes and releases the xa_lock. May sleep
1351 * if the @gfp flags permit.
1352 * Return: The old entry at this index on success, xa_err(-EINVAL) if @entry
1353 * cannot be stored in an XArray, or xa_err(-ENOMEM) if memory allocation
1356 void *xa_store(struct xarray
*xa
, unsigned long index
, void *entry
, gfp_t gfp
)
1358 XA_STATE(xas
, xa
, index
);
1361 if (WARN_ON_ONCE(xa_is_internal(entry
)))
1362 return XA_ERROR(-EINVAL
);
1366 curr
= xas_store(&xas
, entry
);
1367 if (xa_track_free(xa
) && entry
)
1368 xas_clear_mark(&xas
, XA_FREE_MARK
);
1370 } while (xas_nomem(&xas
, gfp
));
1372 return xas_result(&xas
, curr
);
1374 EXPORT_SYMBOL(xa_store
);
1377 * __xa_store() - Store this entry in the XArray.
1379 * @index: Index into array.
1380 * @entry: New entry.
1381 * @gfp: Memory allocation flags.
1383 * You must already be holding the xa_lock when calling this function.
1384 * It will drop the lock if needed to allocate memory, and then reacquire
1387 * Context: Any context. Expects xa_lock to be held on entry. May
1388 * release and reacquire xa_lock if @gfp flags permit.
1389 * Return: The old entry at this index or xa_err() if an error happened.
1391 void *__xa_store(struct xarray
*xa
, unsigned long index
, void *entry
, gfp_t gfp
)
1393 XA_STATE(xas
, xa
, index
);
1396 if (WARN_ON_ONCE(xa_is_internal(entry
)))
1397 return XA_ERROR(-EINVAL
);
1400 curr
= xas_store(&xas
, entry
);
1401 if (xa_track_free(xa
) && entry
)
1402 xas_clear_mark(&xas
, XA_FREE_MARK
);
1403 } while (__xas_nomem(&xas
, gfp
));
1405 return xas_result(&xas
, curr
);
1407 EXPORT_SYMBOL(__xa_store
);
1410 * __xa_cmpxchg() - Store this entry in the XArray.
1412 * @index: Index into array.
1413 * @old: Old value to test against.
1414 * @entry: New entry.
1415 * @gfp: Memory allocation flags.
1417 * You must already be holding the xa_lock when calling this function.
1418 * It will drop the lock if needed to allocate memory, and then reacquire
1421 * Context: Any context. Expects xa_lock to be held on entry. May
1422 * release and reacquire xa_lock if @gfp flags permit.
1423 * Return: The old entry at this index or xa_err() if an error happened.
1425 void *__xa_cmpxchg(struct xarray
*xa
, unsigned long index
,
1426 void *old
, void *entry
, gfp_t gfp
)
1428 XA_STATE(xas
, xa
, index
);
1431 if (WARN_ON_ONCE(xa_is_internal(entry
)))
1432 return XA_ERROR(-EINVAL
);
1435 curr
= xas_load(&xas
);
1436 if (curr
== XA_ZERO_ENTRY
)
1439 xas_store(&xas
, entry
);
1440 if (xa_track_free(xa
) && entry
)
1441 xas_clear_mark(&xas
, XA_FREE_MARK
);
1443 } while (__xas_nomem(&xas
, gfp
));
1445 return xas_result(&xas
, curr
);
1447 EXPORT_SYMBOL(__xa_cmpxchg
);
1450 * __xa_reserve() - Reserve this index in the XArray.
1452 * @index: Index into array.
1453 * @gfp: Memory allocation flags.
1455 * Ensures there is somewhere to store an entry at @index in the array.
1456 * If there is already something stored at @index, this function does
1457 * nothing. If there was nothing there, the entry is marked as reserved.
1458 * Loading from a reserved entry returns a %NULL pointer.
1460 * If you do not use the entry that you have reserved, call xa_release()
1461 * or xa_erase() to free any unnecessary memory.
1463 * Context: Any context. Expects the xa_lock to be held on entry. May
1464 * release the lock, sleep and reacquire the lock if the @gfp flags permit.
1465 * Return: 0 if the reservation succeeded or -ENOMEM if it failed.
1467 int __xa_reserve(struct xarray
*xa
, unsigned long index
, gfp_t gfp
)
1469 XA_STATE(xas
, xa
, index
);
1473 curr
= xas_load(&xas
);
1475 xas_store(&xas
, XA_ZERO_ENTRY
);
1476 } while (__xas_nomem(&xas
, gfp
));
1478 return xas_error(&xas
);
1480 EXPORT_SYMBOL(__xa_reserve
);
1482 #ifdef CONFIG_XARRAY_MULTI
1483 static void xas_set_range(struct xa_state
*xas
, unsigned long first
,
1486 unsigned int shift
= 0;
1487 unsigned long sibs
= last
- first
;
1488 unsigned int offset
= XA_CHUNK_MASK
;
1490 xas_set(xas
, first
);
1492 while ((first
& XA_CHUNK_MASK
) == 0) {
1493 if (sibs
< XA_CHUNK_MASK
)
1495 if ((sibs
== XA_CHUNK_MASK
) && (offset
< XA_CHUNK_MASK
))
1497 shift
+= XA_CHUNK_SHIFT
;
1498 if (offset
== XA_CHUNK_MASK
)
1499 offset
= sibs
& XA_CHUNK_MASK
;
1500 sibs
>>= XA_CHUNK_SHIFT
;
1501 first
>>= XA_CHUNK_SHIFT
;
1504 offset
= first
& XA_CHUNK_MASK
;
1505 if (offset
+ sibs
> XA_CHUNK_MASK
)
1506 sibs
= XA_CHUNK_MASK
- offset
;
1507 if ((((first
+ sibs
+ 1) << shift
) - 1) > last
)
1510 xas
->xa_shift
= shift
;
1511 xas
->xa_sibs
= sibs
;
1515 * xa_store_range() - Store this entry at a range of indices in the XArray.
1517 * @first: First index to affect.
1518 * @last: Last index to affect.
1519 * @entry: New entry.
1520 * @gfp: Memory allocation flags.
1522 * After this function returns, loads from any index between @first and @last,
1523 * inclusive will return @entry.
1524 * Storing into an existing multislot entry updates the entry of every index.
1525 * The marks associated with @index are unaffected unless @entry is %NULL.
1527 * Context: Process context. Takes and releases the xa_lock. May sleep
1528 * if the @gfp flags permit.
1529 * Return: %NULL on success, xa_err(-EINVAL) if @entry cannot be stored in
1530 * an XArray, or xa_err(-ENOMEM) if memory allocation failed.
1532 void *xa_store_range(struct xarray
*xa
, unsigned long first
,
1533 unsigned long last
, void *entry
, gfp_t gfp
)
1535 XA_STATE(xas
, xa
, 0);
1537 if (WARN_ON_ONCE(xa_is_internal(entry
)))
1538 return XA_ERROR(-EINVAL
);
1540 return XA_ERROR(-EINVAL
);
1545 unsigned int order
= (last
== ~0UL) ? 64 :
1547 xas_set_order(&xas
, last
, order
);
1549 if (xas_error(&xas
))
1553 xas_set_range(&xas
, first
, last
);
1554 xas_store(&xas
, entry
);
1555 if (xas_error(&xas
))
1557 first
+= xas_size(&xas
);
1558 } while (first
<= last
);
1561 } while (xas_nomem(&xas
, gfp
));
1563 return xas_result(&xas
, NULL
);
1565 EXPORT_SYMBOL(xa_store_range
);
1566 #endif /* CONFIG_XARRAY_MULTI */
1569 * __xa_alloc() - Find somewhere to store this entry in the XArray.
1571 * @id: Pointer to ID.
1572 * @max: Maximum ID to allocate (inclusive).
1573 * @entry: New entry.
1574 * @gfp: Memory allocation flags.
1576 * Allocates an unused ID in the range specified by @id and @max.
1577 * Updates the @id pointer with the index, then stores the entry at that
1578 * index. A concurrent lookup will not see an uninitialised @id.
1580 * Context: Any context. Expects xa_lock to be held on entry. May
1581 * release and reacquire xa_lock if @gfp flags permit.
1582 * Return: 0 on success, -ENOMEM if memory allocation fails or -ENOSPC if
1583 * there is no more space in the XArray.
1585 int __xa_alloc(struct xarray
*xa
, u32
*id
, u32 max
, void *entry
, gfp_t gfp
)
1587 XA_STATE(xas
, xa
, 0);
1590 if (WARN_ON_ONCE(xa_is_internal(entry
)))
1592 if (WARN_ON_ONCE(!xa_track_free(xa
)))
1596 entry
= XA_ZERO_ENTRY
;
1600 xas_find_marked(&xas
, max
, XA_FREE_MARK
);
1601 if (xas
.xa_node
== XAS_RESTART
)
1602 xas_set_err(&xas
, -ENOSPC
);
1603 xas_store(&xas
, entry
);
1604 xas_clear_mark(&xas
, XA_FREE_MARK
);
1605 } while (__xas_nomem(&xas
, gfp
));
1607 err
= xas_error(&xas
);
1612 EXPORT_SYMBOL(__xa_alloc
);
1615 * __xa_set_mark() - Set this mark on this entry while locked.
1617 * @index: Index of entry.
1618 * @mark: Mark number.
1620 * Attempting to set a mark on a NULL entry does not succeed.
1622 * Context: Any context. Expects xa_lock to be held on entry.
1624 void __xa_set_mark(struct xarray
*xa
, unsigned long index
, xa_mark_t mark
)
1626 XA_STATE(xas
, xa
, index
);
1627 void *entry
= xas_load(&xas
);
1630 xas_set_mark(&xas
, mark
);
1632 EXPORT_SYMBOL(__xa_set_mark
);
1635 * __xa_clear_mark() - Clear this mark on this entry while locked.
1637 * @index: Index of entry.
1638 * @mark: Mark number.
1640 * Context: Any context. Expects xa_lock to be held on entry.
1642 void __xa_clear_mark(struct xarray
*xa
, unsigned long index
, xa_mark_t mark
)
1644 XA_STATE(xas
, xa
, index
);
1645 void *entry
= xas_load(&xas
);
1648 xas_clear_mark(&xas
, mark
);
1650 EXPORT_SYMBOL(__xa_clear_mark
);
1653 * xa_get_mark() - Inquire whether this mark is set on this entry.
1655 * @index: Index of entry.
1656 * @mark: Mark number.
1658 * This function uses the RCU read lock, so the result may be out of date
1659 * by the time it returns. If you need the result to be stable, use a lock.
1661 * Context: Any context. Takes and releases the RCU lock.
1662 * Return: True if the entry at @index has this mark set, false if it doesn't.
1664 bool xa_get_mark(struct xarray
*xa
, unsigned long index
, xa_mark_t mark
)
1666 XA_STATE(xas
, xa
, index
);
1670 entry
= xas_start(&xas
);
1671 while (xas_get_mark(&xas
, mark
)) {
1672 if (!xa_is_node(entry
))
1674 entry
= xas_descend(&xas
, xa_to_node(entry
));
1682 EXPORT_SYMBOL(xa_get_mark
);
1685 * xa_set_mark() - Set this mark on this entry.
1687 * @index: Index of entry.
1688 * @mark: Mark number.
1690 * Attempting to set a mark on a NULL entry does not succeed.
1692 * Context: Process context. Takes and releases the xa_lock.
1694 void xa_set_mark(struct xarray
*xa
, unsigned long index
, xa_mark_t mark
)
1697 __xa_set_mark(xa
, index
, mark
);
1700 EXPORT_SYMBOL(xa_set_mark
);
1703 * xa_clear_mark() - Clear this mark on this entry.
1705 * @index: Index of entry.
1706 * @mark: Mark number.
1708 * Clearing a mark always succeeds.
1710 * Context: Process context. Takes and releases the xa_lock.
1712 void xa_clear_mark(struct xarray
*xa
, unsigned long index
, xa_mark_t mark
)
1715 __xa_clear_mark(xa
, index
, mark
);
1718 EXPORT_SYMBOL(xa_clear_mark
);
1721 * xa_find() - Search the XArray for an entry.
1723 * @indexp: Pointer to an index.
1724 * @max: Maximum index to search to.
1725 * @filter: Selection criterion.
1727 * Finds the entry in @xa which matches the @filter, and has the lowest
1728 * index that is at least @indexp and no more than @max.
1729 * If an entry is found, @indexp is updated to be the index of the entry.
1730 * This function is protected by the RCU read lock, so it may not find
1731 * entries which are being simultaneously added. It will not return an
1732 * %XA_RETRY_ENTRY; if you need to see retry entries, use xas_find().
1734 * Context: Any context. Takes and releases the RCU lock.
1735 * Return: The entry, if found, otherwise %NULL.
1737 void *xa_find(struct xarray
*xa
, unsigned long *indexp
,
1738 unsigned long max
, xa_mark_t filter
)
1740 XA_STATE(xas
, xa
, *indexp
);
1745 if ((__force
unsigned int)filter
< XA_MAX_MARKS
)
1746 entry
= xas_find_marked(&xas
, max
, filter
);
1748 entry
= xas_find(&xas
, max
);
1749 } while (xas_retry(&xas
, entry
));
1753 *indexp
= xas
.xa_index
;
1756 EXPORT_SYMBOL(xa_find
);
1759 * xa_find_after() - Search the XArray for a present 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 above @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 miss entries
1769 * 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 pointer, if found, otherwise %NULL.
1775 void *xa_find_after(struct xarray
*xa
, unsigned long *indexp
,
1776 unsigned long max
, xa_mark_t filter
)
1778 XA_STATE(xas
, xa
, *indexp
+ 1);
1783 if ((__force
unsigned int)filter
< XA_MAX_MARKS
)
1784 entry
= xas_find_marked(&xas
, max
, filter
);
1786 entry
= xas_find(&xas
, max
);
1787 if (xas
.xa_node
== XAS_BOUNDS
)
1790 if (xas
.xa_index
& ((1UL << xas
.xa_shift
) - 1))
1793 if (xas
.xa_offset
< (xas
.xa_index
& XA_CHUNK_MASK
))
1796 if (!xas_retry(&xas
, entry
))
1802 *indexp
= xas
.xa_index
;
1805 EXPORT_SYMBOL(xa_find_after
);
1807 static unsigned int xas_extract_present(struct xa_state
*xas
, void **dst
,
1808 unsigned long max
, unsigned int n
)
1814 xas_for_each(xas
, entry
, max
) {
1815 if (xas_retry(xas
, entry
))
1826 static unsigned int xas_extract_marked(struct xa_state
*xas
, void **dst
,
1827 unsigned long max
, unsigned int n
, xa_mark_t mark
)
1833 xas_for_each_marked(xas
, entry
, max
, mark
) {
1834 if (xas_retry(xas
, entry
))
1846 * xa_extract() - Copy selected entries from the XArray into a normal array.
1847 * @xa: The source XArray to copy from.
1848 * @dst: The buffer to copy entries into.
1849 * @start: The first index in the XArray eligible to be selected.
1850 * @max: The last index in the XArray eligible to be selected.
1851 * @n: The maximum number of entries to copy.
1852 * @filter: Selection criterion.
1854 * Copies up to @n entries that match @filter from the XArray. The
1855 * copied entries will have indices between @start and @max, inclusive.
1857 * The @filter may be an XArray mark value, in which case entries which are
1858 * marked with that mark will be copied. It may also be %XA_PRESENT, in
1859 * which case all entries which are not NULL will be copied.
1861 * The entries returned may not represent a snapshot of the XArray at a
1862 * moment in time. For example, if another thread stores to index 5, then
1863 * index 10, calling xa_extract() may return the old contents of index 5
1864 * and the new contents of index 10. Indices not modified while this
1865 * function is running will not be skipped.
1867 * If you need stronger guarantees, holding the xa_lock across calls to this
1868 * function will prevent concurrent modification.
1870 * Context: Any context. Takes and releases the RCU lock.
1871 * Return: The number of entries copied.
1873 unsigned int xa_extract(struct xarray
*xa
, void **dst
, unsigned long start
,
1874 unsigned long max
, unsigned int n
, xa_mark_t filter
)
1876 XA_STATE(xas
, xa
, start
);
1881 if ((__force
unsigned int)filter
< XA_MAX_MARKS
)
1882 return xas_extract_marked(&xas
, dst
, max
, n
, filter
);
1883 return xas_extract_present(&xas
, dst
, max
, n
);
1885 EXPORT_SYMBOL(xa_extract
);
1888 * xa_destroy() - Free all internal data structures.
1891 * After calling this function, the XArray is empty and has freed all memory
1892 * allocated for its internal data structures. You are responsible for
1893 * freeing the objects referenced by the XArray.
1895 * Context: Any context. Takes and releases the xa_lock, interrupt-safe.
1897 void xa_destroy(struct xarray
*xa
)
1899 XA_STATE(xas
, xa
, 0);
1900 unsigned long flags
;
1904 xas_lock_irqsave(&xas
, flags
);
1905 entry
= xa_head_locked(xa
);
1906 RCU_INIT_POINTER(xa
->xa_head
, NULL
);
1907 xas_init_marks(&xas
);
1908 /* lockdep checks we're still holding the lock in xas_free_nodes() */
1909 if (xa_is_node(entry
))
1910 xas_free_nodes(&xas
, xa_to_node(entry
));
1911 xas_unlock_irqrestore(&xas
, flags
);
1913 EXPORT_SYMBOL(xa_destroy
);
1916 void xa_dump_node(const struct xa_node
*node
)
1922 if ((unsigned long)node
& 3) {
1923 pr_cont("node %px\n", node
);
1927 pr_cont("node %px %s %d parent %px shift %d count %d values %d "
1928 "array %px list %px %px marks",
1929 node
, node
->parent
? "offset" : "max", node
->offset
,
1930 node
->parent
, node
->shift
, node
->count
, node
->nr_values
,
1931 node
->array
, node
->private_list
.prev
, node
->private_list
.next
);
1932 for (i
= 0; i
< XA_MAX_MARKS
; i
++)
1933 for (j
= 0; j
< XA_MARK_LONGS
; j
++)
1934 pr_cont(" %lx", node
->marks
[i
][j
]);
1938 void xa_dump_index(unsigned long index
, unsigned int shift
)
1941 pr_info("%lu: ", index
);
1942 else if (shift
>= BITS_PER_LONG
)
1943 pr_info("0-%lu: ", ~0UL);
1945 pr_info("%lu-%lu: ", index
, index
| ((1UL << shift
) - 1));
1948 void xa_dump_entry(const void *entry
, unsigned long index
, unsigned long shift
)
1953 xa_dump_index(index
, shift
);
1955 if (xa_is_node(entry
)) {
1957 pr_cont("%px\n", entry
);
1960 struct xa_node
*node
= xa_to_node(entry
);
1962 for (i
= 0; i
< XA_CHUNK_SIZE
; i
++)
1963 xa_dump_entry(node
->slots
[i
],
1964 index
+ (i
<< node
->shift
), node
->shift
);
1966 } else if (xa_is_value(entry
))
1967 pr_cont("value %ld (0x%lx) [%px]\n", xa_to_value(entry
),
1968 xa_to_value(entry
), entry
);
1969 else if (!xa_is_internal(entry
))
1970 pr_cont("%px\n", entry
);
1971 else if (xa_is_retry(entry
))
1972 pr_cont("retry (%ld)\n", xa_to_internal(entry
));
1973 else if (xa_is_sibling(entry
))
1974 pr_cont("sibling (slot %ld)\n", xa_to_sibling(entry
));
1975 else if (xa_is_zero(entry
))
1976 pr_cont("zero (%ld)\n", xa_to_internal(entry
));
1978 pr_cont("UNKNOWN ENTRY (%px)\n", entry
);
1981 void xa_dump(const struct xarray
*xa
)
1983 void *entry
= xa
->xa_head
;
1984 unsigned int shift
= 0;
1986 pr_info("xarray: %px head %px flags %x marks %d %d %d\n", xa
, entry
,
1987 xa
->xa_flags
, xa_marked(xa
, XA_MARK_0
),
1988 xa_marked(xa
, XA_MARK_1
), xa_marked(xa
, XA_MARK_2
));
1989 if (xa_is_node(entry
))
1990 shift
= xa_to_node(entry
)->shift
+ XA_CHUNK_SHIFT
;
1991 xa_dump_entry(entry
, 0, shift
);