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