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