xref: /linux-6.15/lib/xarray.c (revision 3e3c6580)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * XArray implementation
4  * Copyright (c) 2017-2018 Microsoft Corporation
5  * Copyright (c) 2018-2020 Oracle
6  * Author: Matthew Wilcox <[email protected]>
7  */
8 
9 #include <linux/bitmap.h>
10 #include <linux/export.h>
11 #include <linux/list.h>
12 #include <linux/slab.h>
13 #include <linux/xarray.h>
14 
15 /*
16  * Coding conventions in this file:
17  *
18  * @xa is used to refer to the entire xarray.
19  * @xas is the 'xarray operation state'.  It may be either a pointer to
20  * an xa_state, or an xa_state stored on the stack.  This is an unfortunate
21  * ambiguity.
22  * @index is the index of the entry being operated on
23  * @mark is an xa_mark_t; a small number indicating one of the mark bits.
24  * @node refers to an xa_node; usually the primary one being operated on by
25  * this function.
26  * @offset is the index into the slots array inside an xa_node.
27  * @parent refers to the @xa_node closer to the head than @node.
28  * @entry refers to something stored in a slot in the xarray
29  */
30 
31 static inline unsigned int xa_lock_type(const struct xarray *xa)
32 {
33 	return (__force unsigned int)xa->xa_flags & 3;
34 }
35 
36 static inline void xas_lock_type(struct xa_state *xas, unsigned int lock_type)
37 {
38 	if (lock_type == XA_LOCK_IRQ)
39 		xas_lock_irq(xas);
40 	else if (lock_type == XA_LOCK_BH)
41 		xas_lock_bh(xas);
42 	else
43 		xas_lock(xas);
44 }
45 
46 static inline void xas_unlock_type(struct xa_state *xas, unsigned int lock_type)
47 {
48 	if (lock_type == XA_LOCK_IRQ)
49 		xas_unlock_irq(xas);
50 	else if (lock_type == XA_LOCK_BH)
51 		xas_unlock_bh(xas);
52 	else
53 		xas_unlock(xas);
54 }
55 
56 static inline bool xa_track_free(const struct xarray *xa)
57 {
58 	return xa->xa_flags & XA_FLAGS_TRACK_FREE;
59 }
60 
61 static inline bool xa_zero_busy(const struct xarray *xa)
62 {
63 	return xa->xa_flags & XA_FLAGS_ZERO_BUSY;
64 }
65 
66 static inline void xa_mark_set(struct xarray *xa, xa_mark_t mark)
67 {
68 	if (!(xa->xa_flags & XA_FLAGS_MARK(mark)))
69 		xa->xa_flags |= XA_FLAGS_MARK(mark);
70 }
71 
72 static inline void xa_mark_clear(struct xarray *xa, xa_mark_t mark)
73 {
74 	if (xa->xa_flags & XA_FLAGS_MARK(mark))
75 		xa->xa_flags &= ~(XA_FLAGS_MARK(mark));
76 }
77 
78 static inline unsigned long *node_marks(struct xa_node *node, xa_mark_t mark)
79 {
80 	return node->marks[(__force unsigned)mark];
81 }
82 
83 static inline bool node_get_mark(struct xa_node *node,
84 		unsigned int offset, xa_mark_t mark)
85 {
86 	return test_bit(offset, node_marks(node, mark));
87 }
88 
89 /* returns true if the bit was set */
90 static inline bool node_set_mark(struct xa_node *node, unsigned int offset,
91 				xa_mark_t mark)
92 {
93 	return __test_and_set_bit(offset, node_marks(node, mark));
94 }
95 
96 /* returns true if the bit was set */
97 static inline bool node_clear_mark(struct xa_node *node, unsigned int offset,
98 				xa_mark_t mark)
99 {
100 	return __test_and_clear_bit(offset, node_marks(node, mark));
101 }
102 
103 static inline bool node_any_mark(struct xa_node *node, xa_mark_t mark)
104 {
105 	return !bitmap_empty(node_marks(node, mark), XA_CHUNK_SIZE);
106 }
107 
108 static inline void node_mark_all(struct xa_node *node, xa_mark_t mark)
109 {
110 	bitmap_fill(node_marks(node, mark), XA_CHUNK_SIZE);
111 }
112 
113 #define mark_inc(mark) do { \
114 	mark = (__force xa_mark_t)((__force unsigned)(mark) + 1); \
115 } while (0)
116 
117 /*
118  * xas_squash_marks() - Merge all marks to the first entry
119  * @xas: Array operation state.
120  *
121  * Set a mark on the first entry if any entry has it set.  Clear marks on
122  * all sibling entries.
123  */
124 static void xas_squash_marks(const struct xa_state *xas)
125 {
126 	unsigned int mark = 0;
127 	unsigned int limit = xas->xa_offset + xas->xa_sibs + 1;
128 
129 	if (!xas->xa_sibs)
130 		return;
131 
132 	do {
133 		unsigned long *marks = xas->xa_node->marks[mark];
134 		if (find_next_bit(marks, limit, xas->xa_offset + 1) == limit)
135 			continue;
136 		__set_bit(xas->xa_offset, marks);
137 		bitmap_clear(marks, xas->xa_offset + 1, xas->xa_sibs);
138 	} while (mark++ != (__force unsigned)XA_MARK_MAX);
139 }
140 
141 /* extracts the offset within this node from the index */
142 static unsigned int get_offset(unsigned long index, struct xa_node *node)
143 {
144 	return (index >> node->shift) & XA_CHUNK_MASK;
145 }
146 
147 static void xas_set_offset(struct xa_state *xas)
148 {
149 	xas->xa_offset = get_offset(xas->xa_index, xas->xa_node);
150 }
151 
152 /* move the index either forwards (find) or backwards (sibling slot) */
153 static void xas_move_index(struct xa_state *xas, unsigned long offset)
154 {
155 	unsigned int shift = xas->xa_node->shift;
156 	xas->xa_index &= ~XA_CHUNK_MASK << shift;
157 	xas->xa_index += offset << shift;
158 }
159 
160 static void xas_next_offset(struct xa_state *xas)
161 {
162 	xas->xa_offset++;
163 	xas_move_index(xas, xas->xa_offset);
164 }
165 
166 static void *set_bounds(struct xa_state *xas)
167 {
168 	xas->xa_node = XAS_BOUNDS;
169 	return NULL;
170 }
171 
172 /*
173  * Starts a walk.  If the @xas is already valid, we assume that it's on
174  * the right path and just return where we've got to.  If we're in an
175  * error state, return NULL.  If the index is outside the current scope
176  * of the xarray, return NULL without changing @xas->xa_node.  Otherwise
177  * set @xas->xa_node to NULL and return the current head of the array.
178  */
179 static void *xas_start(struct xa_state *xas)
180 {
181 	void *entry;
182 
183 	if (xas_valid(xas))
184 		return xas_reload(xas);
185 	if (xas_error(xas))
186 		return NULL;
187 
188 	entry = xa_head(xas->xa);
189 	if (!xa_is_node(entry)) {
190 		if (xas->xa_index)
191 			return set_bounds(xas);
192 	} else {
193 		if ((xas->xa_index >> xa_to_node(entry)->shift) > XA_CHUNK_MASK)
194 			return set_bounds(xas);
195 	}
196 
197 	xas->xa_node = NULL;
198 	return entry;
199 }
200 
201 static void *xas_descend(struct xa_state *xas, struct xa_node *node)
202 {
203 	unsigned int offset = get_offset(xas->xa_index, node);
204 	void *entry = xa_entry(xas->xa, node, offset);
205 
206 	xas->xa_node = node;
207 	if (xa_is_sibling(entry)) {
208 		offset = xa_to_sibling(entry);
209 		entry = xa_entry(xas->xa, node, offset);
210 	}
211 
212 	xas->xa_offset = offset;
213 	return entry;
214 }
215 
216 /**
217  * xas_load() - Load an entry from the XArray (advanced).
218  * @xas: XArray operation state.
219  *
220  * Usually walks the @xas to the appropriate state to load the entry
221  * stored at xa_index.  However, it will do nothing and return %NULL if
222  * @xas is in an error state.  xas_load() will never expand the tree.
223  *
224  * If the xa_state is set up to operate on a multi-index entry, xas_load()
225  * may return %NULL or an internal entry, even if there are entries
226  * present within the range specified by @xas.
227  *
228  * Context: Any context.  The caller should hold the xa_lock or the RCU lock.
229  * Return: Usually an entry in the XArray, but see description for exceptions.
230  */
231 void *xas_load(struct xa_state *xas)
232 {
233 	void *entry = xas_start(xas);
234 
235 	while (xa_is_node(entry)) {
236 		struct xa_node *node = xa_to_node(entry);
237 
238 		if (xas->xa_shift > node->shift)
239 			break;
240 		entry = xas_descend(xas, node);
241 		if (node->shift == 0)
242 			break;
243 	}
244 	return entry;
245 }
246 EXPORT_SYMBOL_GPL(xas_load);
247 
248 /* Move the radix tree node cache here */
249 extern struct kmem_cache *radix_tree_node_cachep;
250 extern void radix_tree_node_rcu_free(struct rcu_head *head);
251 
252 #define XA_RCU_FREE	((struct xarray *)1)
253 
254 static void xa_node_free(struct xa_node *node)
255 {
256 	XA_NODE_BUG_ON(node, !list_empty(&node->private_list));
257 	node->array = XA_RCU_FREE;
258 	call_rcu(&node->rcu_head, radix_tree_node_rcu_free);
259 }
260 
261 /*
262  * xas_destroy() - Free any resources allocated during the XArray operation.
263  * @xas: XArray operation state.
264  *
265  * This function is now internal-only.
266  */
267 static void xas_destroy(struct xa_state *xas)
268 {
269 	struct xa_node *next, *node = xas->xa_alloc;
270 
271 	while (node) {
272 		XA_NODE_BUG_ON(node, !list_empty(&node->private_list));
273 		next = rcu_dereference_raw(node->parent);
274 		radix_tree_node_rcu_free(&node->rcu_head);
275 		xas->xa_alloc = node = next;
276 	}
277 }
278 
279 /**
280  * xas_nomem() - Allocate memory if needed.
281  * @xas: XArray operation state.
282  * @gfp: Memory allocation flags.
283  *
284  * If we need to add new nodes to the XArray, we try to allocate memory
285  * with GFP_NOWAIT while holding the lock, which will usually succeed.
286  * If it fails, @xas is flagged as needing memory to continue.  The caller
287  * should drop the lock and call xas_nomem().  If xas_nomem() succeeds,
288  * the caller should retry the operation.
289  *
290  * Forward progress is guaranteed as one node is allocated here and
291  * stored in the xa_state where it will be found by xas_alloc().  More
292  * nodes will likely be found in the slab allocator, but we do not tie
293  * them up here.
294  *
295  * Return: true if memory was needed, and was successfully allocated.
296  */
297 bool xas_nomem(struct xa_state *xas, gfp_t gfp)
298 {
299 	if (xas->xa_node != XA_ERROR(-ENOMEM)) {
300 		xas_destroy(xas);
301 		return false;
302 	}
303 	if (xas->xa->xa_flags & XA_FLAGS_ACCOUNT)
304 		gfp |= __GFP_ACCOUNT;
305 	xas->xa_alloc = kmem_cache_alloc(radix_tree_node_cachep, gfp);
306 	if (!xas->xa_alloc)
307 		return false;
308 	xas->xa_alloc->parent = NULL;
309 	XA_NODE_BUG_ON(xas->xa_alloc, !list_empty(&xas->xa_alloc->private_list));
310 	xas->xa_node = XAS_RESTART;
311 	return true;
312 }
313 EXPORT_SYMBOL_GPL(xas_nomem);
314 
315 /*
316  * __xas_nomem() - Drop locks and allocate memory if needed.
317  * @xas: XArray operation state.
318  * @gfp: Memory allocation flags.
319  *
320  * Internal variant of xas_nomem().
321  *
322  * Return: true if memory was needed, and was successfully allocated.
323  */
324 static bool __xas_nomem(struct xa_state *xas, gfp_t gfp)
325 	__must_hold(xas->xa->xa_lock)
326 {
327 	unsigned int lock_type = xa_lock_type(xas->xa);
328 
329 	if (xas->xa_node != XA_ERROR(-ENOMEM)) {
330 		xas_destroy(xas);
331 		return false;
332 	}
333 	if (xas->xa->xa_flags & XA_FLAGS_ACCOUNT)
334 		gfp |= __GFP_ACCOUNT;
335 	if (gfpflags_allow_blocking(gfp)) {
336 		xas_unlock_type(xas, lock_type);
337 		xas->xa_alloc = kmem_cache_alloc(radix_tree_node_cachep, gfp);
338 		xas_lock_type(xas, lock_type);
339 	} else {
340 		xas->xa_alloc = kmem_cache_alloc(radix_tree_node_cachep, gfp);
341 	}
342 	if (!xas->xa_alloc)
343 		return false;
344 	xas->xa_alloc->parent = NULL;
345 	XA_NODE_BUG_ON(xas->xa_alloc, !list_empty(&xas->xa_alloc->private_list));
346 	xas->xa_node = XAS_RESTART;
347 	return true;
348 }
349 
350 static void xas_update(struct xa_state *xas, struct xa_node *node)
351 {
352 	if (xas->xa_update)
353 		xas->xa_update(node);
354 	else
355 		XA_NODE_BUG_ON(node, !list_empty(&node->private_list));
356 }
357 
358 static void *xas_alloc(struct xa_state *xas, unsigned int shift)
359 {
360 	struct xa_node *parent = xas->xa_node;
361 	struct xa_node *node = xas->xa_alloc;
362 
363 	if (xas_invalid(xas))
364 		return NULL;
365 
366 	if (node) {
367 		xas->xa_alloc = NULL;
368 	} else {
369 		gfp_t gfp = GFP_NOWAIT | __GFP_NOWARN;
370 
371 		if (xas->xa->xa_flags & XA_FLAGS_ACCOUNT)
372 			gfp |= __GFP_ACCOUNT;
373 
374 		node = kmem_cache_alloc(radix_tree_node_cachep, gfp);
375 		if (!node) {
376 			xas_set_err(xas, -ENOMEM);
377 			return NULL;
378 		}
379 	}
380 
381 	if (parent) {
382 		node->offset = xas->xa_offset;
383 		parent->count++;
384 		XA_NODE_BUG_ON(node, parent->count > XA_CHUNK_SIZE);
385 		xas_update(xas, parent);
386 	}
387 	XA_NODE_BUG_ON(node, shift > BITS_PER_LONG);
388 	XA_NODE_BUG_ON(node, !list_empty(&node->private_list));
389 	node->shift = shift;
390 	node->count = 0;
391 	node->nr_values = 0;
392 	RCU_INIT_POINTER(node->parent, xas->xa_node);
393 	node->array = xas->xa;
394 
395 	return node;
396 }
397 
398 #ifdef CONFIG_XARRAY_MULTI
399 /* Returns the number of indices covered by a given xa_state */
400 static unsigned long xas_size(const struct xa_state *xas)
401 {
402 	return (xas->xa_sibs + 1UL) << xas->xa_shift;
403 }
404 #endif
405 
406 /*
407  * Use this to calculate the maximum index that will need to be created
408  * in order to add the entry described by @xas.  Because we cannot store a
409  * multi-index entry at index 0, the calculation is a little more complex
410  * than you might expect.
411  */
412 static unsigned long xas_max(struct xa_state *xas)
413 {
414 	unsigned long max = xas->xa_index;
415 
416 #ifdef CONFIG_XARRAY_MULTI
417 	if (xas->xa_shift || xas->xa_sibs) {
418 		unsigned long mask = xas_size(xas) - 1;
419 		max |= mask;
420 		if (mask == max)
421 			max++;
422 	}
423 #endif
424 
425 	return max;
426 }
427 
428 /* The maximum index that can be contained in the array without expanding it */
429 static unsigned long max_index(void *entry)
430 {
431 	if (!xa_is_node(entry))
432 		return 0;
433 	return (XA_CHUNK_SIZE << xa_to_node(entry)->shift) - 1;
434 }
435 
436 static void xas_shrink(struct xa_state *xas)
437 {
438 	struct xarray *xa = xas->xa;
439 	struct xa_node *node = xas->xa_node;
440 
441 	for (;;) {
442 		void *entry;
443 
444 		XA_NODE_BUG_ON(node, node->count > XA_CHUNK_SIZE);
445 		if (node->count != 1)
446 			break;
447 		entry = xa_entry_locked(xa, node, 0);
448 		if (!entry)
449 			break;
450 		if (!xa_is_node(entry) && node->shift)
451 			break;
452 		if (xa_is_zero(entry) && xa_zero_busy(xa))
453 			entry = NULL;
454 		xas->xa_node = XAS_BOUNDS;
455 
456 		RCU_INIT_POINTER(xa->xa_head, entry);
457 		if (xa_track_free(xa) && !node_get_mark(node, 0, XA_FREE_MARK))
458 			xa_mark_clear(xa, XA_FREE_MARK);
459 
460 		node->count = 0;
461 		node->nr_values = 0;
462 		if (!xa_is_node(entry))
463 			RCU_INIT_POINTER(node->slots[0], XA_RETRY_ENTRY);
464 		xas_update(xas, node);
465 		xa_node_free(node);
466 		if (!xa_is_node(entry))
467 			break;
468 		node = xa_to_node(entry);
469 		node->parent = NULL;
470 	}
471 }
472 
473 /*
474  * xas_delete_node() - Attempt to delete an xa_node
475  * @xas: Array operation state.
476  *
477  * Attempts to delete the @xas->xa_node.  This will fail if xa->node has
478  * a non-zero reference count.
479  */
480 static void xas_delete_node(struct xa_state *xas)
481 {
482 	struct xa_node *node = xas->xa_node;
483 
484 	for (;;) {
485 		struct xa_node *parent;
486 
487 		XA_NODE_BUG_ON(node, node->count > XA_CHUNK_SIZE);
488 		if (node->count)
489 			break;
490 
491 		parent = xa_parent_locked(xas->xa, node);
492 		xas->xa_node = parent;
493 		xas->xa_offset = node->offset;
494 		xa_node_free(node);
495 
496 		if (!parent) {
497 			xas->xa->xa_head = NULL;
498 			xas->xa_node = XAS_BOUNDS;
499 			return;
500 		}
501 
502 		parent->slots[xas->xa_offset] = NULL;
503 		parent->count--;
504 		XA_NODE_BUG_ON(parent, parent->count > XA_CHUNK_SIZE);
505 		node = parent;
506 		xas_update(xas, node);
507 	}
508 
509 	if (!node->parent)
510 		xas_shrink(xas);
511 }
512 
513 /**
514  * xas_free_nodes() - Free this node and all nodes that it references
515  * @xas: Array operation state.
516  * @top: Node to free
517  *
518  * This node has been removed from the tree.  We must now free it and all
519  * of its subnodes.  There may be RCU walkers with references into the tree,
520  * so we must replace all entries with retry markers.
521  */
522 static void xas_free_nodes(struct xa_state *xas, struct xa_node *top)
523 {
524 	unsigned int offset = 0;
525 	struct xa_node *node = top;
526 
527 	for (;;) {
528 		void *entry = xa_entry_locked(xas->xa, node, offset);
529 
530 		if (node->shift && xa_is_node(entry)) {
531 			node = xa_to_node(entry);
532 			offset = 0;
533 			continue;
534 		}
535 		if (entry)
536 			RCU_INIT_POINTER(node->slots[offset], XA_RETRY_ENTRY);
537 		offset++;
538 		while (offset == XA_CHUNK_SIZE) {
539 			struct xa_node *parent;
540 
541 			parent = xa_parent_locked(xas->xa, node);
542 			offset = node->offset + 1;
543 			node->count = 0;
544 			node->nr_values = 0;
545 			xas_update(xas, node);
546 			xa_node_free(node);
547 			if (node == top)
548 				return;
549 			node = parent;
550 		}
551 	}
552 }
553 
554 /*
555  * xas_expand adds nodes to the head of the tree until it has reached
556  * sufficient height to be able to contain @xas->xa_index
557  */
558 static int xas_expand(struct xa_state *xas, void *head)
559 {
560 	struct xarray *xa = xas->xa;
561 	struct xa_node *node = NULL;
562 	unsigned int shift = 0;
563 	unsigned long max = xas_max(xas);
564 
565 	if (!head) {
566 		if (max == 0)
567 			return 0;
568 		while ((max >> shift) >= XA_CHUNK_SIZE)
569 			shift += XA_CHUNK_SHIFT;
570 		return shift + XA_CHUNK_SHIFT;
571 	} else if (xa_is_node(head)) {
572 		node = xa_to_node(head);
573 		shift = node->shift + XA_CHUNK_SHIFT;
574 	}
575 	xas->xa_node = NULL;
576 
577 	while (max > max_index(head)) {
578 		xa_mark_t mark = 0;
579 
580 		XA_NODE_BUG_ON(node, shift > BITS_PER_LONG);
581 		node = xas_alloc(xas, shift);
582 		if (!node)
583 			return -ENOMEM;
584 
585 		node->count = 1;
586 		if (xa_is_value(head))
587 			node->nr_values = 1;
588 		RCU_INIT_POINTER(node->slots[0], head);
589 
590 		/* Propagate the aggregated mark info to the new child */
591 		for (;;) {
592 			if (xa_track_free(xa) && mark == XA_FREE_MARK) {
593 				node_mark_all(node, XA_FREE_MARK);
594 				if (!xa_marked(xa, XA_FREE_MARK)) {
595 					node_clear_mark(node, 0, XA_FREE_MARK);
596 					xa_mark_set(xa, XA_FREE_MARK);
597 				}
598 			} else if (xa_marked(xa, mark)) {
599 				node_set_mark(node, 0, mark);
600 			}
601 			if (mark == XA_MARK_MAX)
602 				break;
603 			mark_inc(mark);
604 		}
605 
606 		/*
607 		 * Now that the new node is fully initialised, we can add
608 		 * it to the tree
609 		 */
610 		if (xa_is_node(head)) {
611 			xa_to_node(head)->offset = 0;
612 			rcu_assign_pointer(xa_to_node(head)->parent, node);
613 		}
614 		head = xa_mk_node(node);
615 		rcu_assign_pointer(xa->xa_head, head);
616 		xas_update(xas, node);
617 
618 		shift += XA_CHUNK_SHIFT;
619 	}
620 
621 	xas->xa_node = node;
622 	return shift;
623 }
624 
625 /*
626  * xas_create() - Create a slot to store an entry in.
627  * @xas: XArray operation state.
628  * @allow_root: %true if we can store the entry in the root directly
629  *
630  * Most users will not need to call this function directly, as it is called
631  * by xas_store().  It is useful for doing conditional store operations
632  * (see the xa_cmpxchg() implementation for an example).
633  *
634  * Return: If the slot already existed, returns the contents of this slot.
635  * If the slot was newly created, returns %NULL.  If it failed to create the
636  * slot, returns %NULL and indicates the error in @xas.
637  */
638 static void *xas_create(struct xa_state *xas, bool allow_root)
639 {
640 	struct xarray *xa = xas->xa;
641 	void *entry;
642 	void __rcu **slot;
643 	struct xa_node *node = xas->xa_node;
644 	int shift;
645 	unsigned int order = xas->xa_shift;
646 
647 	if (xas_top(node)) {
648 		entry = xa_head_locked(xa);
649 		xas->xa_node = NULL;
650 		if (!entry && xa_zero_busy(xa))
651 			entry = XA_ZERO_ENTRY;
652 		shift = xas_expand(xas, entry);
653 		if (shift < 0)
654 			return NULL;
655 		if (!shift && !allow_root)
656 			shift = XA_CHUNK_SHIFT;
657 		entry = xa_head_locked(xa);
658 		slot = &xa->xa_head;
659 	} else if (xas_error(xas)) {
660 		return NULL;
661 	} else if (node) {
662 		unsigned int offset = xas->xa_offset;
663 
664 		shift = node->shift;
665 		entry = xa_entry_locked(xa, node, offset);
666 		slot = &node->slots[offset];
667 	} else {
668 		shift = 0;
669 		entry = xa_head_locked(xa);
670 		slot = &xa->xa_head;
671 	}
672 
673 	while (shift > order) {
674 		shift -= XA_CHUNK_SHIFT;
675 		if (!entry) {
676 			node = xas_alloc(xas, shift);
677 			if (!node)
678 				break;
679 			if (xa_track_free(xa))
680 				node_mark_all(node, XA_FREE_MARK);
681 			rcu_assign_pointer(*slot, xa_mk_node(node));
682 		} else if (xa_is_node(entry)) {
683 			node = xa_to_node(entry);
684 		} else {
685 			break;
686 		}
687 		entry = xas_descend(xas, node);
688 		slot = &node->slots[xas->xa_offset];
689 	}
690 
691 	return entry;
692 }
693 
694 /**
695  * xas_create_range() - Ensure that stores to this range will succeed
696  * @xas: XArray operation state.
697  *
698  * Creates all of the slots in the range covered by @xas.  Sets @xas to
699  * create single-index entries and positions it at the beginning of the
700  * range.  This is for the benefit of users which have not yet been
701  * converted to use multi-index entries.
702  */
703 void xas_create_range(struct xa_state *xas)
704 {
705 	unsigned long index = xas->xa_index;
706 	unsigned char shift = xas->xa_shift;
707 	unsigned char sibs = xas->xa_sibs;
708 
709 	xas->xa_index |= ((sibs + 1UL) << shift) - 1;
710 	if (xas_is_node(xas) && xas->xa_node->shift == xas->xa_shift)
711 		xas->xa_offset |= sibs;
712 	xas->xa_shift = 0;
713 	xas->xa_sibs = 0;
714 
715 	for (;;) {
716 		xas_create(xas, true);
717 		if (xas_error(xas))
718 			goto restore;
719 		if (xas->xa_index <= (index | XA_CHUNK_MASK))
720 			goto success;
721 		xas->xa_index -= XA_CHUNK_SIZE;
722 
723 		for (;;) {
724 			struct xa_node *node = xas->xa_node;
725 			if (node->shift >= shift)
726 				break;
727 			xas->xa_node = xa_parent_locked(xas->xa, node);
728 			xas->xa_offset = node->offset - 1;
729 			if (node->offset != 0)
730 				break;
731 		}
732 	}
733 
734 restore:
735 	xas->xa_shift = shift;
736 	xas->xa_sibs = sibs;
737 	xas->xa_index = index;
738 	return;
739 success:
740 	xas->xa_index = index;
741 	if (xas->xa_node)
742 		xas_set_offset(xas);
743 }
744 EXPORT_SYMBOL_GPL(xas_create_range);
745 
746 static void update_node(struct xa_state *xas, struct xa_node *node,
747 		int count, int values)
748 {
749 	if (!node || (!count && !values))
750 		return;
751 
752 	node->count += count;
753 	node->nr_values += values;
754 	XA_NODE_BUG_ON(node, node->count > XA_CHUNK_SIZE);
755 	XA_NODE_BUG_ON(node, node->nr_values > XA_CHUNK_SIZE);
756 	xas_update(xas, node);
757 	if (count < 0)
758 		xas_delete_node(xas);
759 }
760 
761 /**
762  * xas_store() - Store this entry in the XArray.
763  * @xas: XArray operation state.
764  * @entry: New entry.
765  *
766  * If @xas is operating on a multi-index entry, the entry returned by this
767  * function is essentially meaningless (it may be an internal entry or it
768  * may be %NULL, even if there are non-NULL entries at some of the indices
769  * covered by the range).  This is not a problem for any current users,
770  * and can be changed if needed.
771  *
772  * Return: The old entry at this index.
773  */
774 void *xas_store(struct xa_state *xas, void *entry)
775 {
776 	struct xa_node *node;
777 	void __rcu **slot = &xas->xa->xa_head;
778 	unsigned int offset, max;
779 	int count = 0;
780 	int values = 0;
781 	void *first, *next;
782 	bool value = xa_is_value(entry);
783 
784 	if (entry) {
785 		bool allow_root = !xa_is_node(entry) && !xa_is_zero(entry);
786 		first = xas_create(xas, allow_root);
787 	} else {
788 		first = xas_load(xas);
789 	}
790 
791 	if (xas_invalid(xas))
792 		return first;
793 	node = xas->xa_node;
794 	if (node && (xas->xa_shift < node->shift))
795 		xas->xa_sibs = 0;
796 	if ((first == entry) && !xas->xa_sibs)
797 		return first;
798 
799 	next = first;
800 	offset = xas->xa_offset;
801 	max = xas->xa_offset + xas->xa_sibs;
802 	if (node) {
803 		slot = &node->slots[offset];
804 		if (xas->xa_sibs)
805 			xas_squash_marks(xas);
806 	}
807 	if (!entry)
808 		xas_init_marks(xas);
809 
810 	for (;;) {
811 		/*
812 		 * Must clear the marks before setting the entry to NULL,
813 		 * otherwise xas_for_each_marked may find a NULL entry and
814 		 * stop early.  rcu_assign_pointer contains a release barrier
815 		 * so the mark clearing will appear to happen before the
816 		 * entry is set to NULL.
817 		 */
818 		rcu_assign_pointer(*slot, entry);
819 		if (xa_is_node(next) && (!node || node->shift))
820 			xas_free_nodes(xas, xa_to_node(next));
821 		if (!node)
822 			break;
823 		count += !next - !entry;
824 		values += !xa_is_value(first) - !value;
825 		if (entry) {
826 			if (offset == max)
827 				break;
828 			if (!xa_is_sibling(entry))
829 				entry = xa_mk_sibling(xas->xa_offset);
830 		} else {
831 			if (offset == XA_CHUNK_MASK)
832 				break;
833 		}
834 		next = xa_entry_locked(xas->xa, node, ++offset);
835 		if (!xa_is_sibling(next)) {
836 			if (!entry && (offset > max))
837 				break;
838 			first = next;
839 		}
840 		slot++;
841 	}
842 
843 	update_node(xas, node, count, values);
844 	return first;
845 }
846 EXPORT_SYMBOL_GPL(xas_store);
847 
848 /**
849  * xas_get_mark() - Returns the state of this mark.
850  * @xas: XArray operation state.
851  * @mark: Mark number.
852  *
853  * Return: true if the mark is set, false if the mark is clear or @xas
854  * is in an error state.
855  */
856 bool xas_get_mark(const struct xa_state *xas, xa_mark_t mark)
857 {
858 	if (xas_invalid(xas))
859 		return false;
860 	if (!xas->xa_node)
861 		return xa_marked(xas->xa, mark);
862 	return node_get_mark(xas->xa_node, xas->xa_offset, mark);
863 }
864 EXPORT_SYMBOL_GPL(xas_get_mark);
865 
866 /**
867  * xas_set_mark() - Sets the mark on this entry and its parents.
868  * @xas: XArray operation state.
869  * @mark: Mark number.
870  *
871  * Sets the specified mark on this entry, and walks up the tree setting it
872  * on all the ancestor entries.  Does nothing if @xas has not been walked to
873  * an entry, or is in an error state.
874  */
875 void xas_set_mark(const struct xa_state *xas, xa_mark_t mark)
876 {
877 	struct xa_node *node = xas->xa_node;
878 	unsigned int offset = xas->xa_offset;
879 
880 	if (xas_invalid(xas))
881 		return;
882 
883 	while (node) {
884 		if (node_set_mark(node, offset, mark))
885 			return;
886 		offset = node->offset;
887 		node = xa_parent_locked(xas->xa, node);
888 	}
889 
890 	if (!xa_marked(xas->xa, mark))
891 		xa_mark_set(xas->xa, mark);
892 }
893 EXPORT_SYMBOL_GPL(xas_set_mark);
894 
895 /**
896  * xas_clear_mark() - Clears the mark on this entry and its parents.
897  * @xas: XArray operation state.
898  * @mark: Mark number.
899  *
900  * Clears the specified mark on this entry, and walks back to the head
901  * attempting to clear it on all the ancestor entries.  Does nothing if
902  * @xas has not been walked to an entry, or is in an error state.
903  */
904 void xas_clear_mark(const struct xa_state *xas, xa_mark_t mark)
905 {
906 	struct xa_node *node = xas->xa_node;
907 	unsigned int offset = xas->xa_offset;
908 
909 	if (xas_invalid(xas))
910 		return;
911 
912 	while (node) {
913 		if (!node_clear_mark(node, offset, mark))
914 			return;
915 		if (node_any_mark(node, mark))
916 			return;
917 
918 		offset = node->offset;
919 		node = xa_parent_locked(xas->xa, node);
920 	}
921 
922 	if (xa_marked(xas->xa, mark))
923 		xa_mark_clear(xas->xa, mark);
924 }
925 EXPORT_SYMBOL_GPL(xas_clear_mark);
926 
927 /**
928  * xas_init_marks() - Initialise all marks for the entry
929  * @xas: Array operations state.
930  *
931  * Initialise all marks for the entry specified by @xas.  If we're tracking
932  * free entries with a mark, we need to set it on all entries.  All other
933  * marks are cleared.
934  *
935  * This implementation is not as efficient as it could be; we may walk
936  * up the tree multiple times.
937  */
938 void xas_init_marks(const struct xa_state *xas)
939 {
940 	xa_mark_t mark = 0;
941 
942 	for (;;) {
943 		if (xa_track_free(xas->xa) && mark == XA_FREE_MARK)
944 			xas_set_mark(xas, mark);
945 		else
946 			xas_clear_mark(xas, mark);
947 		if (mark == XA_MARK_MAX)
948 			break;
949 		mark_inc(mark);
950 	}
951 }
952 EXPORT_SYMBOL_GPL(xas_init_marks);
953 
954 #ifdef CONFIG_XARRAY_MULTI
955 static unsigned int node_get_marks(struct xa_node *node, unsigned int offset)
956 {
957 	unsigned int marks = 0;
958 	xa_mark_t mark = XA_MARK_0;
959 
960 	for (;;) {
961 		if (node_get_mark(node, offset, mark))
962 			marks |= 1 << (__force unsigned int)mark;
963 		if (mark == XA_MARK_MAX)
964 			break;
965 		mark_inc(mark);
966 	}
967 
968 	return marks;
969 }
970 
971 static void node_set_marks(struct xa_node *node, unsigned int offset,
972 			struct xa_node *child, unsigned int marks)
973 {
974 	xa_mark_t mark = XA_MARK_0;
975 
976 	for (;;) {
977 		if (marks & (1 << (__force unsigned int)mark)) {
978 			node_set_mark(node, offset, mark);
979 			if (child)
980 				node_mark_all(child, mark);
981 		}
982 		if (mark == XA_MARK_MAX)
983 			break;
984 		mark_inc(mark);
985 	}
986 }
987 
988 /**
989  * xas_split_alloc() - Allocate memory for splitting an entry.
990  * @xas: XArray operation state.
991  * @entry: New entry which will be stored in the array.
992  * @order: Current entry order.
993  * @gfp: Memory allocation flags.
994  *
995  * This function should be called before calling xas_split().
996  * If necessary, it will allocate new nodes (and fill them with @entry)
997  * to prepare for the upcoming split of an entry of @order size into
998  * entries of the order stored in the @xas.
999  *
1000  * Context: May sleep if @gfp flags permit.
1001  */
1002 void xas_split_alloc(struct xa_state *xas, void *entry, unsigned int order,
1003 		gfp_t gfp)
1004 {
1005 	unsigned int sibs = (1 << (order % XA_CHUNK_SHIFT)) - 1;
1006 	unsigned int mask = xas->xa_sibs;
1007 
1008 	/* XXX: no support for splitting really large entries yet */
1009 	if (WARN_ON(xas->xa_shift + 2 * XA_CHUNK_SHIFT < order))
1010 		goto nomem;
1011 	if (xas->xa_shift + XA_CHUNK_SHIFT > order)
1012 		return;
1013 
1014 	do {
1015 		unsigned int i;
1016 		void *sibling = NULL;
1017 		struct xa_node *node;
1018 
1019 		node = kmem_cache_alloc(radix_tree_node_cachep, gfp);
1020 		if (!node)
1021 			goto nomem;
1022 		node->array = xas->xa;
1023 		for (i = 0; i < XA_CHUNK_SIZE; i++) {
1024 			if ((i & mask) == 0) {
1025 				RCU_INIT_POINTER(node->slots[i], entry);
1026 				sibling = xa_mk_sibling(i);
1027 			} else {
1028 				RCU_INIT_POINTER(node->slots[i], sibling);
1029 			}
1030 		}
1031 		RCU_INIT_POINTER(node->parent, xas->xa_alloc);
1032 		xas->xa_alloc = node;
1033 	} while (sibs-- > 0);
1034 
1035 	return;
1036 nomem:
1037 	xas_destroy(xas);
1038 	xas_set_err(xas, -ENOMEM);
1039 }
1040 EXPORT_SYMBOL_GPL(xas_split_alloc);
1041 
1042 /**
1043  * xas_split() - Split a multi-index entry into smaller entries.
1044  * @xas: XArray operation state.
1045  * @entry: New entry to store in the array.
1046  * @order: Current entry order.
1047  *
1048  * The size of the new entries is set in @xas.  The value in @entry is
1049  * copied to all the replacement entries.
1050  *
1051  * Context: Any context.  The caller should hold the xa_lock.
1052  */
1053 void xas_split(struct xa_state *xas, void *entry, unsigned int order)
1054 {
1055 	unsigned int sibs = (1 << (order % XA_CHUNK_SHIFT)) - 1;
1056 	unsigned int offset, marks;
1057 	struct xa_node *node;
1058 	void *curr = xas_load(xas);
1059 	int values = 0;
1060 
1061 	node = xas->xa_node;
1062 	if (xas_top(node))
1063 		return;
1064 
1065 	marks = node_get_marks(node, xas->xa_offset);
1066 
1067 	offset = xas->xa_offset + sibs;
1068 	do {
1069 		if (xas->xa_shift < node->shift) {
1070 			struct xa_node *child = xas->xa_alloc;
1071 
1072 			xas->xa_alloc = rcu_dereference_raw(child->parent);
1073 			child->shift = node->shift - XA_CHUNK_SHIFT;
1074 			child->offset = offset;
1075 			child->count = XA_CHUNK_SIZE;
1076 			child->nr_values = xa_is_value(entry) ?
1077 					XA_CHUNK_SIZE : 0;
1078 			RCU_INIT_POINTER(child->parent, node);
1079 			node_set_marks(node, offset, child, marks);
1080 			rcu_assign_pointer(node->slots[offset],
1081 					xa_mk_node(child));
1082 			if (xa_is_value(curr))
1083 				values--;
1084 		} else {
1085 			unsigned int canon = offset - xas->xa_sibs;
1086 
1087 			node_set_marks(node, canon, NULL, marks);
1088 			rcu_assign_pointer(node->slots[canon], entry);
1089 			while (offset > canon)
1090 				rcu_assign_pointer(node->slots[offset--],
1091 						xa_mk_sibling(canon));
1092 			values += (xa_is_value(entry) - xa_is_value(curr)) *
1093 					(xas->xa_sibs + 1);
1094 		}
1095 	} while (offset-- > xas->xa_offset);
1096 
1097 	node->nr_values += values;
1098 }
1099 EXPORT_SYMBOL_GPL(xas_split);
1100 #endif
1101 
1102 /**
1103  * xas_pause() - Pause a walk to drop a lock.
1104  * @xas: XArray operation state.
1105  *
1106  * Some users need to pause a walk and drop the lock they're holding in
1107  * order to yield to a higher priority thread or carry out an operation
1108  * on an entry.  Those users should call this function before they drop
1109  * the lock.  It resets the @xas to be suitable for the next iteration
1110  * of the loop after the user has reacquired the lock.  If most entries
1111  * found during a walk require you to call xas_pause(), the xa_for_each()
1112  * iterator may be more appropriate.
1113  *
1114  * Note that xas_pause() only works for forward iteration.  If a user needs
1115  * to pause a reverse iteration, we will need a xas_pause_rev().
1116  */
1117 void xas_pause(struct xa_state *xas)
1118 {
1119 	struct xa_node *node = xas->xa_node;
1120 
1121 	if (xas_invalid(xas))
1122 		return;
1123 
1124 	xas->xa_node = XAS_RESTART;
1125 	if (node) {
1126 		unsigned long offset = xas->xa_offset;
1127 		while (++offset < XA_CHUNK_SIZE) {
1128 			if (!xa_is_sibling(xa_entry(xas->xa, node, offset)))
1129 				break;
1130 		}
1131 		xas->xa_index += (offset - xas->xa_offset) << node->shift;
1132 		if (xas->xa_index == 0)
1133 			xas->xa_node = XAS_BOUNDS;
1134 	} else {
1135 		xas->xa_index++;
1136 	}
1137 }
1138 EXPORT_SYMBOL_GPL(xas_pause);
1139 
1140 /*
1141  * __xas_prev() - Find the previous entry in the XArray.
1142  * @xas: XArray operation state.
1143  *
1144  * Helper function for xas_prev() which handles all the complex cases
1145  * out of line.
1146  */
1147 void *__xas_prev(struct xa_state *xas)
1148 {
1149 	void *entry;
1150 
1151 	if (!xas_frozen(xas->xa_node))
1152 		xas->xa_index--;
1153 	if (!xas->xa_node)
1154 		return set_bounds(xas);
1155 	if (xas_not_node(xas->xa_node))
1156 		return xas_load(xas);
1157 
1158 	if (xas->xa_offset != get_offset(xas->xa_index, xas->xa_node))
1159 		xas->xa_offset--;
1160 
1161 	while (xas->xa_offset == 255) {
1162 		xas->xa_offset = xas->xa_node->offset - 1;
1163 		xas->xa_node = xa_parent(xas->xa, xas->xa_node);
1164 		if (!xas->xa_node)
1165 			return set_bounds(xas);
1166 	}
1167 
1168 	for (;;) {
1169 		entry = xa_entry(xas->xa, xas->xa_node, xas->xa_offset);
1170 		if (!xa_is_node(entry))
1171 			return entry;
1172 
1173 		xas->xa_node = xa_to_node(entry);
1174 		xas_set_offset(xas);
1175 	}
1176 }
1177 EXPORT_SYMBOL_GPL(__xas_prev);
1178 
1179 /*
1180  * __xas_next() - Find the next entry in the XArray.
1181  * @xas: XArray operation state.
1182  *
1183  * Helper function for xas_next() which handles all the complex cases
1184  * out of line.
1185  */
1186 void *__xas_next(struct xa_state *xas)
1187 {
1188 	void *entry;
1189 
1190 	if (!xas_frozen(xas->xa_node))
1191 		xas->xa_index++;
1192 	if (!xas->xa_node)
1193 		return set_bounds(xas);
1194 	if (xas_not_node(xas->xa_node))
1195 		return xas_load(xas);
1196 
1197 	if (xas->xa_offset != get_offset(xas->xa_index, xas->xa_node))
1198 		xas->xa_offset++;
1199 
1200 	while (xas->xa_offset == XA_CHUNK_SIZE) {
1201 		xas->xa_offset = xas->xa_node->offset + 1;
1202 		xas->xa_node = xa_parent(xas->xa, xas->xa_node);
1203 		if (!xas->xa_node)
1204 			return set_bounds(xas);
1205 	}
1206 
1207 	for (;;) {
1208 		entry = xa_entry(xas->xa, xas->xa_node, xas->xa_offset);
1209 		if (!xa_is_node(entry))
1210 			return entry;
1211 
1212 		xas->xa_node = xa_to_node(entry);
1213 		xas_set_offset(xas);
1214 	}
1215 }
1216 EXPORT_SYMBOL_GPL(__xas_next);
1217 
1218 /**
1219  * xas_find() - Find the next present entry in the XArray.
1220  * @xas: XArray operation state.
1221  * @max: Highest index to return.
1222  *
1223  * If the @xas has not yet been walked to an entry, return the entry
1224  * which has an index >= xas.xa_index.  If it has been walked, the entry
1225  * currently being pointed at has been processed, and so we move to the
1226  * next entry.
1227  *
1228  * If no entry is found and the array is smaller than @max, the iterator
1229  * is set to the smallest index not yet in the array.  This allows @xas
1230  * to be immediately passed to xas_store().
1231  *
1232  * Return: The entry, if found, otherwise %NULL.
1233  */
1234 void *xas_find(struct xa_state *xas, unsigned long max)
1235 {
1236 	void *entry;
1237 
1238 	if (xas_error(xas) || xas->xa_node == XAS_BOUNDS)
1239 		return NULL;
1240 	if (xas->xa_index > max)
1241 		return set_bounds(xas);
1242 
1243 	if (!xas->xa_node) {
1244 		xas->xa_index = 1;
1245 		return set_bounds(xas);
1246 	} else if (xas->xa_node == XAS_RESTART) {
1247 		entry = xas_load(xas);
1248 		if (entry || xas_not_node(xas->xa_node))
1249 			return entry;
1250 	} else if (!xas->xa_node->shift &&
1251 		    xas->xa_offset != (xas->xa_index & XA_CHUNK_MASK)) {
1252 		xas->xa_offset = ((xas->xa_index - 1) & XA_CHUNK_MASK) + 1;
1253 	}
1254 
1255 	xas_next_offset(xas);
1256 
1257 	while (xas->xa_node && (xas->xa_index <= max)) {
1258 		if (unlikely(xas->xa_offset == XA_CHUNK_SIZE)) {
1259 			xas->xa_offset = xas->xa_node->offset + 1;
1260 			xas->xa_node = xa_parent(xas->xa, xas->xa_node);
1261 			continue;
1262 		}
1263 
1264 		entry = xa_entry(xas->xa, xas->xa_node, xas->xa_offset);
1265 		if (xa_is_node(entry)) {
1266 			xas->xa_node = xa_to_node(entry);
1267 			xas->xa_offset = 0;
1268 			continue;
1269 		}
1270 		if (entry && !xa_is_sibling(entry))
1271 			return entry;
1272 
1273 		xas_next_offset(xas);
1274 	}
1275 
1276 	if (!xas->xa_node)
1277 		xas->xa_node = XAS_BOUNDS;
1278 	return NULL;
1279 }
1280 EXPORT_SYMBOL_GPL(xas_find);
1281 
1282 /**
1283  * xas_find_marked() - Find the next marked entry in the XArray.
1284  * @xas: XArray operation state.
1285  * @max: Highest index to return.
1286  * @mark: Mark number to search for.
1287  *
1288  * If the @xas has not yet been walked to an entry, return the marked entry
1289  * which has an index >= xas.xa_index.  If it has been walked, the entry
1290  * currently being pointed at has been processed, and so we return the
1291  * first marked entry with an index > xas.xa_index.
1292  *
1293  * If no marked entry is found and the array is smaller than @max, @xas is
1294  * set to the bounds state and xas->xa_index is set to the smallest index
1295  * not yet in the array.  This allows @xas to be immediately passed to
1296  * xas_store().
1297  *
1298  * If no entry is found before @max is reached, @xas is set to the restart
1299  * state.
1300  *
1301  * Return: The entry, if found, otherwise %NULL.
1302  */
1303 void *xas_find_marked(struct xa_state *xas, unsigned long max, xa_mark_t mark)
1304 {
1305 	bool advance = true;
1306 	unsigned int offset;
1307 	void *entry;
1308 
1309 	if (xas_error(xas))
1310 		return NULL;
1311 	if (xas->xa_index > max)
1312 		goto max;
1313 
1314 	if (!xas->xa_node) {
1315 		xas->xa_index = 1;
1316 		goto out;
1317 	} else if (xas_top(xas->xa_node)) {
1318 		advance = false;
1319 		entry = xa_head(xas->xa);
1320 		xas->xa_node = NULL;
1321 		if (xas->xa_index > max_index(entry))
1322 			goto out;
1323 		if (!xa_is_node(entry)) {
1324 			if (xa_marked(xas->xa, mark))
1325 				return entry;
1326 			xas->xa_index = 1;
1327 			goto out;
1328 		}
1329 		xas->xa_node = xa_to_node(entry);
1330 		xas->xa_offset = xas->xa_index >> xas->xa_node->shift;
1331 	}
1332 
1333 	while (xas->xa_index <= max) {
1334 		if (unlikely(xas->xa_offset == XA_CHUNK_SIZE)) {
1335 			xas->xa_offset = xas->xa_node->offset + 1;
1336 			xas->xa_node = xa_parent(xas->xa, xas->xa_node);
1337 			if (!xas->xa_node)
1338 				break;
1339 			advance = false;
1340 			continue;
1341 		}
1342 
1343 		if (!advance) {
1344 			entry = xa_entry(xas->xa, xas->xa_node, xas->xa_offset);
1345 			if (xa_is_sibling(entry)) {
1346 				xas->xa_offset = xa_to_sibling(entry);
1347 				xas_move_index(xas, xas->xa_offset);
1348 			}
1349 		}
1350 
1351 		offset = xas_find_chunk(xas, advance, mark);
1352 		if (offset > xas->xa_offset) {
1353 			advance = false;
1354 			xas_move_index(xas, offset);
1355 			/* Mind the wrap */
1356 			if ((xas->xa_index - 1) >= max)
1357 				goto max;
1358 			xas->xa_offset = offset;
1359 			if (offset == XA_CHUNK_SIZE)
1360 				continue;
1361 		}
1362 
1363 		entry = xa_entry(xas->xa, xas->xa_node, xas->xa_offset);
1364 		if (!entry && !(xa_track_free(xas->xa) && mark == XA_FREE_MARK))
1365 			continue;
1366 		if (!xa_is_node(entry))
1367 			return entry;
1368 		xas->xa_node = xa_to_node(entry);
1369 		xas_set_offset(xas);
1370 	}
1371 
1372 out:
1373 	if (xas->xa_index > max)
1374 		goto max;
1375 	return set_bounds(xas);
1376 max:
1377 	xas->xa_node = XAS_RESTART;
1378 	return NULL;
1379 }
1380 EXPORT_SYMBOL_GPL(xas_find_marked);
1381 
1382 /**
1383  * xas_find_conflict() - Find the next present entry in a range.
1384  * @xas: XArray operation state.
1385  *
1386  * The @xas describes both a range and a position within that range.
1387  *
1388  * Context: Any context.  Expects xa_lock to be held.
1389  * Return: The next entry in the range covered by @xas or %NULL.
1390  */
1391 void *xas_find_conflict(struct xa_state *xas)
1392 {
1393 	void *curr;
1394 
1395 	if (xas_error(xas))
1396 		return NULL;
1397 
1398 	if (!xas->xa_node)
1399 		return NULL;
1400 
1401 	if (xas_top(xas->xa_node)) {
1402 		curr = xas_start(xas);
1403 		if (!curr)
1404 			return NULL;
1405 		while (xa_is_node(curr)) {
1406 			struct xa_node *node = xa_to_node(curr);
1407 			curr = xas_descend(xas, node);
1408 		}
1409 		if (curr)
1410 			return curr;
1411 	}
1412 
1413 	if (xas->xa_node->shift > xas->xa_shift)
1414 		return NULL;
1415 
1416 	for (;;) {
1417 		if (xas->xa_node->shift == xas->xa_shift) {
1418 			if ((xas->xa_offset & xas->xa_sibs) == xas->xa_sibs)
1419 				break;
1420 		} else if (xas->xa_offset == XA_CHUNK_MASK) {
1421 			xas->xa_offset = xas->xa_node->offset;
1422 			xas->xa_node = xa_parent_locked(xas->xa, xas->xa_node);
1423 			if (!xas->xa_node)
1424 				break;
1425 			continue;
1426 		}
1427 		curr = xa_entry_locked(xas->xa, xas->xa_node, ++xas->xa_offset);
1428 		if (xa_is_sibling(curr))
1429 			continue;
1430 		while (xa_is_node(curr)) {
1431 			xas->xa_node = xa_to_node(curr);
1432 			xas->xa_offset = 0;
1433 			curr = xa_entry_locked(xas->xa, xas->xa_node, 0);
1434 		}
1435 		if (curr)
1436 			return curr;
1437 	}
1438 	xas->xa_offset -= xas->xa_sibs;
1439 	return NULL;
1440 }
1441 EXPORT_SYMBOL_GPL(xas_find_conflict);
1442 
1443 /**
1444  * xa_load() - Load an entry from an XArray.
1445  * @xa: XArray.
1446  * @index: index into array.
1447  *
1448  * Context: Any context.  Takes and releases the RCU lock.
1449  * Return: The entry at @index in @xa.
1450  */
1451 void *xa_load(struct xarray *xa, unsigned long index)
1452 {
1453 	XA_STATE(xas, xa, index);
1454 	void *entry;
1455 
1456 	rcu_read_lock();
1457 	do {
1458 		entry = xas_load(&xas);
1459 		if (xa_is_zero(entry))
1460 			entry = NULL;
1461 	} while (xas_retry(&xas, entry));
1462 	rcu_read_unlock();
1463 
1464 	return entry;
1465 }
1466 EXPORT_SYMBOL(xa_load);
1467 
1468 static void *xas_result(struct xa_state *xas, void *curr)
1469 {
1470 	if (xa_is_zero(curr))
1471 		return NULL;
1472 	if (xas_error(xas))
1473 		curr = xas->xa_node;
1474 	return curr;
1475 }
1476 
1477 /**
1478  * __xa_erase() - Erase this entry from the XArray while locked.
1479  * @xa: XArray.
1480  * @index: Index into array.
1481  *
1482  * After this function returns, loading from @index will return %NULL.
1483  * If the index is part of a multi-index entry, all indices will be erased
1484  * and none of the entries will be part of a multi-index entry.
1485  *
1486  * Context: Any context.  Expects xa_lock to be held on entry.
1487  * Return: The entry which used to be at this index.
1488  */
1489 void *__xa_erase(struct xarray *xa, unsigned long index)
1490 {
1491 	XA_STATE(xas, xa, index);
1492 	return xas_result(&xas, xas_store(&xas, NULL));
1493 }
1494 EXPORT_SYMBOL(__xa_erase);
1495 
1496 /**
1497  * xa_erase() - Erase this entry from the XArray.
1498  * @xa: XArray.
1499  * @index: Index of entry.
1500  *
1501  * After this function returns, loading from @index will return %NULL.
1502  * If the index is part of a multi-index entry, all indices will be erased
1503  * and none of the entries will be part of a multi-index entry.
1504  *
1505  * Context: Any context.  Takes and releases the xa_lock.
1506  * Return: The entry which used to be at this index.
1507  */
1508 void *xa_erase(struct xarray *xa, unsigned long index)
1509 {
1510 	void *entry;
1511 
1512 	xa_lock(xa);
1513 	entry = __xa_erase(xa, index);
1514 	xa_unlock(xa);
1515 
1516 	return entry;
1517 }
1518 EXPORT_SYMBOL(xa_erase);
1519 
1520 /**
1521  * __xa_store() - Store this entry in the XArray.
1522  * @xa: XArray.
1523  * @index: Index into array.
1524  * @entry: New entry.
1525  * @gfp: Memory allocation flags.
1526  *
1527  * You must already be holding the xa_lock when calling this function.
1528  * It will drop the lock if needed to allocate memory, and then reacquire
1529  * it afterwards.
1530  *
1531  * Context: Any context.  Expects xa_lock to be held on entry.  May
1532  * release and reacquire xa_lock if @gfp flags permit.
1533  * Return: The old entry at this index or xa_err() if an error happened.
1534  */
1535 void *__xa_store(struct xarray *xa, unsigned long index, void *entry, gfp_t gfp)
1536 {
1537 	XA_STATE(xas, xa, index);
1538 	void *curr;
1539 
1540 	if (WARN_ON_ONCE(xa_is_advanced(entry)))
1541 		return XA_ERROR(-EINVAL);
1542 	if (xa_track_free(xa) && !entry)
1543 		entry = XA_ZERO_ENTRY;
1544 
1545 	do {
1546 		curr = xas_store(&xas, entry);
1547 		if (xa_track_free(xa))
1548 			xas_clear_mark(&xas, XA_FREE_MARK);
1549 	} while (__xas_nomem(&xas, gfp));
1550 
1551 	return xas_result(&xas, curr);
1552 }
1553 EXPORT_SYMBOL(__xa_store);
1554 
1555 /**
1556  * xa_store() - Store this entry in the XArray.
1557  * @xa: XArray.
1558  * @index: Index into array.
1559  * @entry: New entry.
1560  * @gfp: Memory allocation flags.
1561  *
1562  * After this function returns, loads from this index will return @entry.
1563  * Storing into an existing multi-index entry updates the entry of every index.
1564  * The marks associated with @index are unaffected unless @entry is %NULL.
1565  *
1566  * Context: Any context.  Takes and releases the xa_lock.
1567  * May sleep if the @gfp flags permit.
1568  * Return: The old entry at this index on success, xa_err(-EINVAL) if @entry
1569  * cannot be stored in an XArray, or xa_err(-ENOMEM) if memory allocation
1570  * failed.
1571  */
1572 void *xa_store(struct xarray *xa, unsigned long index, void *entry, gfp_t gfp)
1573 {
1574 	void *curr;
1575 
1576 	xa_lock(xa);
1577 	curr = __xa_store(xa, index, entry, gfp);
1578 	xa_unlock(xa);
1579 
1580 	return curr;
1581 }
1582 EXPORT_SYMBOL(xa_store);
1583 
1584 /**
1585  * __xa_cmpxchg() - Store this entry in the XArray.
1586  * @xa: XArray.
1587  * @index: Index into array.
1588  * @old: Old value to test against.
1589  * @entry: New entry.
1590  * @gfp: Memory allocation flags.
1591  *
1592  * You must already be holding the xa_lock when calling this function.
1593  * It will drop the lock if needed to allocate memory, and then reacquire
1594  * it afterwards.
1595  *
1596  * Context: Any context.  Expects xa_lock to be held on entry.  May
1597  * release and reacquire xa_lock if @gfp flags permit.
1598  * Return: The old entry at this index or xa_err() if an error happened.
1599  */
1600 void *__xa_cmpxchg(struct xarray *xa, unsigned long index,
1601 			void *old, void *entry, gfp_t gfp)
1602 {
1603 	XA_STATE(xas, xa, index);
1604 	void *curr;
1605 
1606 	if (WARN_ON_ONCE(xa_is_advanced(entry)))
1607 		return XA_ERROR(-EINVAL);
1608 
1609 	do {
1610 		curr = xas_load(&xas);
1611 		if (curr == old) {
1612 			xas_store(&xas, entry);
1613 			if (xa_track_free(xa) && entry && !curr)
1614 				xas_clear_mark(&xas, XA_FREE_MARK);
1615 		}
1616 	} while (__xas_nomem(&xas, gfp));
1617 
1618 	return xas_result(&xas, curr);
1619 }
1620 EXPORT_SYMBOL(__xa_cmpxchg);
1621 
1622 /**
1623  * __xa_insert() - Store this entry in the XArray if no entry is present.
1624  * @xa: XArray.
1625  * @index: Index into array.
1626  * @entry: New entry.
1627  * @gfp: Memory allocation flags.
1628  *
1629  * Inserting a NULL entry will store a reserved entry (like xa_reserve())
1630  * if no entry is present.  Inserting will fail if a reserved entry is
1631  * present, even though loading from this index will return NULL.
1632  *
1633  * Context: Any context.  Expects xa_lock to be held on entry.  May
1634  * release and reacquire xa_lock if @gfp flags permit.
1635  * Return: 0 if the store succeeded.  -EBUSY if another entry was present.
1636  * -ENOMEM if memory could not be allocated.
1637  */
1638 int __xa_insert(struct xarray *xa, unsigned long index, void *entry, gfp_t gfp)
1639 {
1640 	XA_STATE(xas, xa, index);
1641 	void *curr;
1642 
1643 	if (WARN_ON_ONCE(xa_is_advanced(entry)))
1644 		return -EINVAL;
1645 	if (!entry)
1646 		entry = XA_ZERO_ENTRY;
1647 
1648 	do {
1649 		curr = xas_load(&xas);
1650 		if (!curr) {
1651 			xas_store(&xas, entry);
1652 			if (xa_track_free(xa))
1653 				xas_clear_mark(&xas, XA_FREE_MARK);
1654 		} else {
1655 			xas_set_err(&xas, -EBUSY);
1656 		}
1657 	} while (__xas_nomem(&xas, gfp));
1658 
1659 	return xas_error(&xas);
1660 }
1661 EXPORT_SYMBOL(__xa_insert);
1662 
1663 #ifdef CONFIG_XARRAY_MULTI
1664 static void xas_set_range(struct xa_state *xas, unsigned long first,
1665 		unsigned long last)
1666 {
1667 	unsigned int shift = 0;
1668 	unsigned long sibs = last - first;
1669 	unsigned int offset = XA_CHUNK_MASK;
1670 
1671 	xas_set(xas, first);
1672 
1673 	while ((first & XA_CHUNK_MASK) == 0) {
1674 		if (sibs < XA_CHUNK_MASK)
1675 			break;
1676 		if ((sibs == XA_CHUNK_MASK) && (offset < XA_CHUNK_MASK))
1677 			break;
1678 		shift += XA_CHUNK_SHIFT;
1679 		if (offset == XA_CHUNK_MASK)
1680 			offset = sibs & XA_CHUNK_MASK;
1681 		sibs >>= XA_CHUNK_SHIFT;
1682 		first >>= XA_CHUNK_SHIFT;
1683 	}
1684 
1685 	offset = first & XA_CHUNK_MASK;
1686 	if (offset + sibs > XA_CHUNK_MASK)
1687 		sibs = XA_CHUNK_MASK - offset;
1688 	if ((((first + sibs + 1) << shift) - 1) > last)
1689 		sibs -= 1;
1690 
1691 	xas->xa_shift = shift;
1692 	xas->xa_sibs = sibs;
1693 }
1694 
1695 /**
1696  * xa_store_range() - Store this entry at a range of indices in the XArray.
1697  * @xa: XArray.
1698  * @first: First index to affect.
1699  * @last: Last index to affect.
1700  * @entry: New entry.
1701  * @gfp: Memory allocation flags.
1702  *
1703  * After this function returns, loads from any index between @first and @last,
1704  * inclusive will return @entry.
1705  * Storing into an existing multi-index entry updates the entry of every index.
1706  * The marks associated with @index are unaffected unless @entry is %NULL.
1707  *
1708  * Context: Process context.  Takes and releases the xa_lock.  May sleep
1709  * if the @gfp flags permit.
1710  * Return: %NULL on success, xa_err(-EINVAL) if @entry cannot be stored in
1711  * an XArray, or xa_err(-ENOMEM) if memory allocation failed.
1712  */
1713 void *xa_store_range(struct xarray *xa, unsigned long first,
1714 		unsigned long last, void *entry, gfp_t gfp)
1715 {
1716 	XA_STATE(xas, xa, 0);
1717 
1718 	if (WARN_ON_ONCE(xa_is_internal(entry)))
1719 		return XA_ERROR(-EINVAL);
1720 	if (last < first)
1721 		return XA_ERROR(-EINVAL);
1722 
1723 	do {
1724 		xas_lock(&xas);
1725 		if (entry) {
1726 			unsigned int order = BITS_PER_LONG;
1727 			if (last + 1)
1728 				order = __ffs(last + 1);
1729 			xas_set_order(&xas, last, order);
1730 			xas_create(&xas, true);
1731 			if (xas_error(&xas))
1732 				goto unlock;
1733 		}
1734 		do {
1735 			xas_set_range(&xas, first, last);
1736 			xas_store(&xas, entry);
1737 			if (xas_error(&xas))
1738 				goto unlock;
1739 			first += xas_size(&xas);
1740 		} while (first <= last);
1741 unlock:
1742 		xas_unlock(&xas);
1743 	} while (xas_nomem(&xas, gfp));
1744 
1745 	return xas_result(&xas, NULL);
1746 }
1747 EXPORT_SYMBOL(xa_store_range);
1748 
1749 /**
1750  * xa_get_order() - Get the order of an entry.
1751  * @xa: XArray.
1752  * @index: Index of the entry.
1753  *
1754  * Return: A number between 0 and 63 indicating the order of the entry.
1755  */
1756 int xa_get_order(struct xarray *xa, unsigned long index)
1757 {
1758 	XA_STATE(xas, xa, index);
1759 	void *entry;
1760 	int order = 0;
1761 
1762 	rcu_read_lock();
1763 	entry = xas_load(&xas);
1764 
1765 	if (!entry)
1766 		goto unlock;
1767 
1768 	if (!xas.xa_node)
1769 		goto unlock;
1770 
1771 	for (;;) {
1772 		unsigned int slot = xas.xa_offset + (1 << order);
1773 
1774 		if (slot >= XA_CHUNK_SIZE)
1775 			break;
1776 		if (!xa_is_sibling(xas.xa_node->slots[slot]))
1777 			break;
1778 		order++;
1779 	}
1780 
1781 	order += xas.xa_node->shift;
1782 unlock:
1783 	rcu_read_unlock();
1784 
1785 	return order;
1786 }
1787 EXPORT_SYMBOL(xa_get_order);
1788 #endif /* CONFIG_XARRAY_MULTI */
1789 
1790 /**
1791  * __xa_alloc() - Find somewhere to store this entry in the XArray.
1792  * @xa: XArray.
1793  * @id: Pointer to ID.
1794  * @limit: Range for allocated ID.
1795  * @entry: New entry.
1796  * @gfp: Memory allocation flags.
1797  *
1798  * Finds an empty entry in @xa between @limit.min and @limit.max,
1799  * stores the index into the @id pointer, then stores the entry at
1800  * that index.  A concurrent lookup will not see an uninitialised @id.
1801  *
1802  * Context: Any context.  Expects xa_lock to be held on entry.  May
1803  * release and reacquire xa_lock if @gfp flags permit.
1804  * Return: 0 on success, -ENOMEM if memory could not be allocated or
1805  * -EBUSY if there are no free entries in @limit.
1806  */
1807 int __xa_alloc(struct xarray *xa, u32 *id, void *entry,
1808 		struct xa_limit limit, gfp_t gfp)
1809 {
1810 	XA_STATE(xas, xa, 0);
1811 
1812 	if (WARN_ON_ONCE(xa_is_advanced(entry)))
1813 		return -EINVAL;
1814 	if (WARN_ON_ONCE(!xa_track_free(xa)))
1815 		return -EINVAL;
1816 
1817 	if (!entry)
1818 		entry = XA_ZERO_ENTRY;
1819 
1820 	do {
1821 		xas.xa_index = limit.min;
1822 		xas_find_marked(&xas, limit.max, XA_FREE_MARK);
1823 		if (xas.xa_node == XAS_RESTART)
1824 			xas_set_err(&xas, -EBUSY);
1825 		else
1826 			*id = xas.xa_index;
1827 		xas_store(&xas, entry);
1828 		xas_clear_mark(&xas, XA_FREE_MARK);
1829 	} while (__xas_nomem(&xas, gfp));
1830 
1831 	return xas_error(&xas);
1832 }
1833 EXPORT_SYMBOL(__xa_alloc);
1834 
1835 /**
1836  * __xa_alloc_cyclic() - Find somewhere to store this entry in the XArray.
1837  * @xa: XArray.
1838  * @id: Pointer to ID.
1839  * @entry: New entry.
1840  * @limit: Range of allocated ID.
1841  * @next: Pointer to next ID to allocate.
1842  * @gfp: Memory allocation flags.
1843  *
1844  * Finds an empty entry in @xa between @limit.min and @limit.max,
1845  * stores the index into the @id pointer, then stores the entry at
1846  * that index.  A concurrent lookup will not see an uninitialised @id.
1847  * The search for an empty entry will start at @next and will wrap
1848  * around if necessary.
1849  *
1850  * Context: Any context.  Expects xa_lock to be held on entry.  May
1851  * release and reacquire xa_lock if @gfp flags permit.
1852  * Return: 0 if the allocation succeeded without wrapping.  1 if the
1853  * allocation succeeded after wrapping, -ENOMEM if memory could not be
1854  * allocated or -EBUSY if there are no free entries in @limit.
1855  */
1856 int __xa_alloc_cyclic(struct xarray *xa, u32 *id, void *entry,
1857 		struct xa_limit limit, u32 *next, gfp_t gfp)
1858 {
1859 	u32 min = limit.min;
1860 	int ret;
1861 
1862 	limit.min = max(min, *next);
1863 	ret = __xa_alloc(xa, id, entry, limit, gfp);
1864 	if ((xa->xa_flags & XA_FLAGS_ALLOC_WRAPPED) && ret == 0) {
1865 		xa->xa_flags &= ~XA_FLAGS_ALLOC_WRAPPED;
1866 		ret = 1;
1867 	}
1868 
1869 	if (ret < 0 && limit.min > min) {
1870 		limit.min = min;
1871 		ret = __xa_alloc(xa, id, entry, limit, gfp);
1872 		if (ret == 0)
1873 			ret = 1;
1874 	}
1875 
1876 	if (ret >= 0) {
1877 		*next = *id + 1;
1878 		if (*next == 0)
1879 			xa->xa_flags |= XA_FLAGS_ALLOC_WRAPPED;
1880 	}
1881 	return ret;
1882 }
1883 EXPORT_SYMBOL(__xa_alloc_cyclic);
1884 
1885 /**
1886  * __xa_set_mark() - Set this mark on this entry while locked.
1887  * @xa: XArray.
1888  * @index: Index of entry.
1889  * @mark: Mark number.
1890  *
1891  * Attempting to set a mark on a %NULL entry does not succeed.
1892  *
1893  * Context: Any context.  Expects xa_lock to be held on entry.
1894  */
1895 void __xa_set_mark(struct xarray *xa, unsigned long index, xa_mark_t mark)
1896 {
1897 	XA_STATE(xas, xa, index);
1898 	void *entry = xas_load(&xas);
1899 
1900 	if (entry)
1901 		xas_set_mark(&xas, mark);
1902 }
1903 EXPORT_SYMBOL(__xa_set_mark);
1904 
1905 /**
1906  * __xa_clear_mark() - Clear this mark on this entry while locked.
1907  * @xa: XArray.
1908  * @index: Index of entry.
1909  * @mark: Mark number.
1910  *
1911  * Context: Any context.  Expects xa_lock to be held on entry.
1912  */
1913 void __xa_clear_mark(struct xarray *xa, unsigned long index, xa_mark_t mark)
1914 {
1915 	XA_STATE(xas, xa, index);
1916 	void *entry = xas_load(&xas);
1917 
1918 	if (entry)
1919 		xas_clear_mark(&xas, mark);
1920 }
1921 EXPORT_SYMBOL(__xa_clear_mark);
1922 
1923 /**
1924  * xa_get_mark() - Inquire whether this mark is set on this entry.
1925  * @xa: XArray.
1926  * @index: Index of entry.
1927  * @mark: Mark number.
1928  *
1929  * This function uses the RCU read lock, so the result may be out of date
1930  * by the time it returns.  If you need the result to be stable, use a lock.
1931  *
1932  * Context: Any context.  Takes and releases the RCU lock.
1933  * Return: True if the entry at @index has this mark set, false if it doesn't.
1934  */
1935 bool xa_get_mark(struct xarray *xa, unsigned long index, xa_mark_t mark)
1936 {
1937 	XA_STATE(xas, xa, index);
1938 	void *entry;
1939 
1940 	rcu_read_lock();
1941 	entry = xas_start(&xas);
1942 	while (xas_get_mark(&xas, mark)) {
1943 		if (!xa_is_node(entry))
1944 			goto found;
1945 		entry = xas_descend(&xas, xa_to_node(entry));
1946 	}
1947 	rcu_read_unlock();
1948 	return false;
1949  found:
1950 	rcu_read_unlock();
1951 	return true;
1952 }
1953 EXPORT_SYMBOL(xa_get_mark);
1954 
1955 /**
1956  * xa_set_mark() - Set this mark on this entry.
1957  * @xa: XArray.
1958  * @index: Index of entry.
1959  * @mark: Mark number.
1960  *
1961  * Attempting to set a mark on a %NULL entry does not succeed.
1962  *
1963  * Context: Process context.  Takes and releases the xa_lock.
1964  */
1965 void xa_set_mark(struct xarray *xa, unsigned long index, xa_mark_t mark)
1966 {
1967 	xa_lock(xa);
1968 	__xa_set_mark(xa, index, mark);
1969 	xa_unlock(xa);
1970 }
1971 EXPORT_SYMBOL(xa_set_mark);
1972 
1973 /**
1974  * xa_clear_mark() - Clear this mark on this entry.
1975  * @xa: XArray.
1976  * @index: Index of entry.
1977  * @mark: Mark number.
1978  *
1979  * Clearing a mark always succeeds.
1980  *
1981  * Context: Process context.  Takes and releases the xa_lock.
1982  */
1983 void xa_clear_mark(struct xarray *xa, unsigned long index, xa_mark_t mark)
1984 {
1985 	xa_lock(xa);
1986 	__xa_clear_mark(xa, index, mark);
1987 	xa_unlock(xa);
1988 }
1989 EXPORT_SYMBOL(xa_clear_mark);
1990 
1991 /**
1992  * xa_find() - Search the XArray for an entry.
1993  * @xa: XArray.
1994  * @indexp: Pointer to an index.
1995  * @max: Maximum index to search to.
1996  * @filter: Selection criterion.
1997  *
1998  * Finds the entry in @xa which matches the @filter, and has the lowest
1999  * index that is at least @indexp and no more than @max.
2000  * If an entry is found, @indexp is updated to be the index of the entry.
2001  * This function is protected by the RCU read lock, so it may not find
2002  * entries which are being simultaneously added.  It will not return an
2003  * %XA_RETRY_ENTRY; if you need to see retry entries, use xas_find().
2004  *
2005  * Context: Any context.  Takes and releases the RCU lock.
2006  * Return: The entry, if found, otherwise %NULL.
2007  */
2008 void *xa_find(struct xarray *xa, unsigned long *indexp,
2009 			unsigned long max, xa_mark_t filter)
2010 {
2011 	XA_STATE(xas, xa, *indexp);
2012 	void *entry;
2013 
2014 	rcu_read_lock();
2015 	do {
2016 		if ((__force unsigned int)filter < XA_MAX_MARKS)
2017 			entry = xas_find_marked(&xas, max, filter);
2018 		else
2019 			entry = xas_find(&xas, max);
2020 	} while (xas_retry(&xas, entry));
2021 	rcu_read_unlock();
2022 
2023 	if (entry)
2024 		*indexp = xas.xa_index;
2025 	return entry;
2026 }
2027 EXPORT_SYMBOL(xa_find);
2028 
2029 static bool xas_sibling(struct xa_state *xas)
2030 {
2031 	struct xa_node *node = xas->xa_node;
2032 	unsigned long mask;
2033 
2034 	if (!IS_ENABLED(CONFIG_XARRAY_MULTI) || !node)
2035 		return false;
2036 	mask = (XA_CHUNK_SIZE << node->shift) - 1;
2037 	return (xas->xa_index & mask) >
2038 		((unsigned long)xas->xa_offset << node->shift);
2039 }
2040 
2041 /**
2042  * xa_find_after() - Search the XArray for a present entry.
2043  * @xa: XArray.
2044  * @indexp: Pointer to an index.
2045  * @max: Maximum index to search to.
2046  * @filter: Selection criterion.
2047  *
2048  * Finds the entry in @xa which matches the @filter and has the lowest
2049  * index that is above @indexp and no more than @max.
2050  * If an entry is found, @indexp is updated to be the index of the entry.
2051  * This function is protected by the RCU read lock, so it may miss entries
2052  * which are being simultaneously added.  It will not return an
2053  * %XA_RETRY_ENTRY; if you need to see retry entries, use xas_find().
2054  *
2055  * Context: Any context.  Takes and releases the RCU lock.
2056  * Return: The pointer, if found, otherwise %NULL.
2057  */
2058 void *xa_find_after(struct xarray *xa, unsigned long *indexp,
2059 			unsigned long max, xa_mark_t filter)
2060 {
2061 	XA_STATE(xas, xa, *indexp + 1);
2062 	void *entry;
2063 
2064 	if (xas.xa_index == 0)
2065 		return NULL;
2066 
2067 	rcu_read_lock();
2068 	for (;;) {
2069 		if ((__force unsigned int)filter < XA_MAX_MARKS)
2070 			entry = xas_find_marked(&xas, max, filter);
2071 		else
2072 			entry = xas_find(&xas, max);
2073 
2074 		if (xas_invalid(&xas))
2075 			break;
2076 		if (xas_sibling(&xas))
2077 			continue;
2078 		if (!xas_retry(&xas, entry))
2079 			break;
2080 	}
2081 	rcu_read_unlock();
2082 
2083 	if (entry)
2084 		*indexp = xas.xa_index;
2085 	return entry;
2086 }
2087 EXPORT_SYMBOL(xa_find_after);
2088 
2089 static unsigned int xas_extract_present(struct xa_state *xas, void **dst,
2090 			unsigned long max, unsigned int n)
2091 {
2092 	void *entry;
2093 	unsigned int i = 0;
2094 
2095 	rcu_read_lock();
2096 	xas_for_each(xas, entry, max) {
2097 		if (xas_retry(xas, entry))
2098 			continue;
2099 		dst[i++] = entry;
2100 		if (i == n)
2101 			break;
2102 	}
2103 	rcu_read_unlock();
2104 
2105 	return i;
2106 }
2107 
2108 static unsigned int xas_extract_marked(struct xa_state *xas, void **dst,
2109 			unsigned long max, unsigned int n, xa_mark_t mark)
2110 {
2111 	void *entry;
2112 	unsigned int i = 0;
2113 
2114 	rcu_read_lock();
2115 	xas_for_each_marked(xas, entry, max, mark) {
2116 		if (xas_retry(xas, entry))
2117 			continue;
2118 		dst[i++] = entry;
2119 		if (i == n)
2120 			break;
2121 	}
2122 	rcu_read_unlock();
2123 
2124 	return i;
2125 }
2126 
2127 /**
2128  * xa_extract() - Copy selected entries from the XArray into a normal array.
2129  * @xa: The source XArray to copy from.
2130  * @dst: The buffer to copy entries into.
2131  * @start: The first index in the XArray eligible to be selected.
2132  * @max: The last index in the XArray eligible to be selected.
2133  * @n: The maximum number of entries to copy.
2134  * @filter: Selection criterion.
2135  *
2136  * Copies up to @n entries that match @filter from the XArray.  The
2137  * copied entries will have indices between @start and @max, inclusive.
2138  *
2139  * The @filter may be an XArray mark value, in which case entries which are
2140  * marked with that mark will be copied.  It may also be %XA_PRESENT, in
2141  * which case all entries which are not %NULL will be copied.
2142  *
2143  * The entries returned may not represent a snapshot of the XArray at a
2144  * moment in time.  For example, if another thread stores to index 5, then
2145  * index 10, calling xa_extract() may return the old contents of index 5
2146  * and the new contents of index 10.  Indices not modified while this
2147  * function is running will not be skipped.
2148  *
2149  * If you need stronger guarantees, holding the xa_lock across calls to this
2150  * function will prevent concurrent modification.
2151  *
2152  * Context: Any context.  Takes and releases the RCU lock.
2153  * Return: The number of entries copied.
2154  */
2155 unsigned int xa_extract(struct xarray *xa, void **dst, unsigned long start,
2156 			unsigned long max, unsigned int n, xa_mark_t filter)
2157 {
2158 	XA_STATE(xas, xa, start);
2159 
2160 	if (!n)
2161 		return 0;
2162 
2163 	if ((__force unsigned int)filter < XA_MAX_MARKS)
2164 		return xas_extract_marked(&xas, dst, max, n, filter);
2165 	return xas_extract_present(&xas, dst, max, n);
2166 }
2167 EXPORT_SYMBOL(xa_extract);
2168 
2169 /**
2170  * xa_delete_node() - Private interface for workingset code.
2171  * @node: Node to be removed from the tree.
2172  * @update: Function to call to update ancestor nodes.
2173  *
2174  * Context: xa_lock must be held on entry and will not be released.
2175  */
2176 void xa_delete_node(struct xa_node *node, xa_update_node_t update)
2177 {
2178 	struct xa_state xas = {
2179 		.xa = node->array,
2180 		.xa_index = (unsigned long)node->offset <<
2181 				(node->shift + XA_CHUNK_SHIFT),
2182 		.xa_shift = node->shift + XA_CHUNK_SHIFT,
2183 		.xa_offset = node->offset,
2184 		.xa_node = xa_parent_locked(node->array, node),
2185 		.xa_update = update,
2186 	};
2187 
2188 	xas_store(&xas, NULL);
2189 }
2190 EXPORT_SYMBOL_GPL(xa_delete_node);	/* For the benefit of the test suite */
2191 
2192 /**
2193  * xa_destroy() - Free all internal data structures.
2194  * @xa: XArray.
2195  *
2196  * After calling this function, the XArray is empty and has freed all memory
2197  * allocated for its internal data structures.  You are responsible for
2198  * freeing the objects referenced by the XArray.
2199  *
2200  * Context: Any context.  Takes and releases the xa_lock, interrupt-safe.
2201  */
2202 void xa_destroy(struct xarray *xa)
2203 {
2204 	XA_STATE(xas, xa, 0);
2205 	unsigned long flags;
2206 	void *entry;
2207 
2208 	xas.xa_node = NULL;
2209 	xas_lock_irqsave(&xas, flags);
2210 	entry = xa_head_locked(xa);
2211 	RCU_INIT_POINTER(xa->xa_head, NULL);
2212 	xas_init_marks(&xas);
2213 	if (xa_zero_busy(xa))
2214 		xa_mark_clear(xa, XA_FREE_MARK);
2215 	/* lockdep checks we're still holding the lock in xas_free_nodes() */
2216 	if (xa_is_node(entry))
2217 		xas_free_nodes(&xas, xa_to_node(entry));
2218 	xas_unlock_irqrestore(&xas, flags);
2219 }
2220 EXPORT_SYMBOL(xa_destroy);
2221 
2222 #ifdef XA_DEBUG
2223 void xa_dump_node(const struct xa_node *node)
2224 {
2225 	unsigned i, j;
2226 
2227 	if (!node)
2228 		return;
2229 	if ((unsigned long)node & 3) {
2230 		pr_cont("node %px\n", node);
2231 		return;
2232 	}
2233 
2234 	pr_cont("node %px %s %d parent %px shift %d count %d values %d "
2235 		"array %px list %px %px marks",
2236 		node, node->parent ? "offset" : "max", node->offset,
2237 		node->parent, node->shift, node->count, node->nr_values,
2238 		node->array, node->private_list.prev, node->private_list.next);
2239 	for (i = 0; i < XA_MAX_MARKS; i++)
2240 		for (j = 0; j < XA_MARK_LONGS; j++)
2241 			pr_cont(" %lx", node->marks[i][j]);
2242 	pr_cont("\n");
2243 }
2244 
2245 void xa_dump_index(unsigned long index, unsigned int shift)
2246 {
2247 	if (!shift)
2248 		pr_info("%lu: ", index);
2249 	else if (shift >= BITS_PER_LONG)
2250 		pr_info("0-%lu: ", ~0UL);
2251 	else
2252 		pr_info("%lu-%lu: ", index, index | ((1UL << shift) - 1));
2253 }
2254 
2255 void xa_dump_entry(const void *entry, unsigned long index, unsigned long shift)
2256 {
2257 	if (!entry)
2258 		return;
2259 
2260 	xa_dump_index(index, shift);
2261 
2262 	if (xa_is_node(entry)) {
2263 		if (shift == 0) {
2264 			pr_cont("%px\n", entry);
2265 		} else {
2266 			unsigned long i;
2267 			struct xa_node *node = xa_to_node(entry);
2268 			xa_dump_node(node);
2269 			for (i = 0; i < XA_CHUNK_SIZE; i++)
2270 				xa_dump_entry(node->slots[i],
2271 				      index + (i << node->shift), node->shift);
2272 		}
2273 	} else if (xa_is_value(entry))
2274 		pr_cont("value %ld (0x%lx) [%px]\n", xa_to_value(entry),
2275 						xa_to_value(entry), entry);
2276 	else if (!xa_is_internal(entry))
2277 		pr_cont("%px\n", entry);
2278 	else if (xa_is_retry(entry))
2279 		pr_cont("retry (%ld)\n", xa_to_internal(entry));
2280 	else if (xa_is_sibling(entry))
2281 		pr_cont("sibling (slot %ld)\n", xa_to_sibling(entry));
2282 	else if (xa_is_zero(entry))
2283 		pr_cont("zero (%ld)\n", xa_to_internal(entry));
2284 	else
2285 		pr_cont("UNKNOWN ENTRY (%px)\n", entry);
2286 }
2287 
2288 void xa_dump(const struct xarray *xa)
2289 {
2290 	void *entry = xa->xa_head;
2291 	unsigned int shift = 0;
2292 
2293 	pr_info("xarray: %px head %px flags %x marks %d %d %d\n", xa, entry,
2294 			xa->xa_flags, xa_marked(xa, XA_MARK_0),
2295 			xa_marked(xa, XA_MARK_1), xa_marked(xa, XA_MARK_2));
2296 	if (xa_is_node(entry))
2297 		shift = xa_to_node(entry)->shift + XA_CHUNK_SHIFT;
2298 	xa_dump_entry(entry, 0, shift);
2299 }
2300 #endif
2301