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