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