xarray: Add XArray iterators
[linux-2.6-block.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
879/**
880 * xas_find() - Find the next present entry in the XArray.
881 * @xas: XArray operation state.
882 * @max: Highest index to return.
883 *
884 * If the @xas has not yet been walked to an entry, return the entry
885 * which has an index >= xas.xa_index. If it has been walked, the entry
886 * currently being pointed at has been processed, and so we move to the
887 * next entry.
888 *
889 * If no entry is found and the array is smaller than @max, the iterator
890 * is set to the smallest index not yet in the array. This allows @xas
891 * to be immediately passed to xas_store().
892 *
893 * Return: The entry, if found, otherwise %NULL.
894 */
895void *xas_find(struct xa_state *xas, unsigned long max)
896{
897 void *entry;
898
899 if (xas_error(xas))
900 return NULL;
901
902 if (!xas->xa_node) {
903 xas->xa_index = 1;
904 return set_bounds(xas);
905 } else if (xas_top(xas->xa_node)) {
906 entry = xas_load(xas);
907 if (entry || xas_not_node(xas->xa_node))
908 return entry;
909 } else if (!xas->xa_node->shift &&
910 xas->xa_offset != (xas->xa_index & XA_CHUNK_MASK)) {
911 xas->xa_offset = ((xas->xa_index - 1) & XA_CHUNK_MASK) + 1;
912 }
913
914 xas_advance(xas);
915
916 while (xas->xa_node && (xas->xa_index <= max)) {
917 if (unlikely(xas->xa_offset == XA_CHUNK_SIZE)) {
918 xas->xa_offset = xas->xa_node->offset + 1;
919 xas->xa_node = xa_parent(xas->xa, xas->xa_node);
920 continue;
921 }
922
923 entry = xa_entry(xas->xa, xas->xa_node, xas->xa_offset);
924 if (xa_is_node(entry)) {
925 xas->xa_node = xa_to_node(entry);
926 xas->xa_offset = 0;
927 continue;
928 }
929 if (entry && !xa_is_sibling(entry))
930 return entry;
931
932 xas_advance(xas);
933 }
934
935 if (!xas->xa_node)
936 xas->xa_node = XAS_BOUNDS;
937 return NULL;
938}
939EXPORT_SYMBOL_GPL(xas_find);
940
941/**
942 * xas_find_marked() - Find the next marked entry in the XArray.
943 * @xas: XArray operation state.
944 * @max: Highest index to return.
945 * @mark: Mark number to search for.
946 *
947 * If the @xas has not yet been walked to an entry, return the marked entry
948 * which has an index >= xas.xa_index. If it has been walked, the entry
949 * currently being pointed at has been processed, and so we return the
950 * first marked entry with an index > xas.xa_index.
951 *
952 * If no marked entry is found and the array is smaller than @max, @xas is
953 * set to the bounds state and xas->xa_index is set to the smallest index
954 * not yet in the array. This allows @xas to be immediately passed to
955 * xas_store().
956 *
957 * If no entry is found before @max is reached, @xas is set to the restart
958 * state.
959 *
960 * Return: The entry, if found, otherwise %NULL.
961 */
962void *xas_find_marked(struct xa_state *xas, unsigned long max, xa_mark_t mark)
963{
964 bool advance = true;
965 unsigned int offset;
966 void *entry;
967
968 if (xas_error(xas))
969 return NULL;
970
971 if (!xas->xa_node) {
972 xas->xa_index = 1;
973 goto out;
974 } else if (xas_top(xas->xa_node)) {
975 advance = false;
976 entry = xa_head(xas->xa);
977 xas->xa_node = NULL;
978 if (xas->xa_index > max_index(entry))
979 goto bounds;
980 if (!xa_is_node(entry)) {
981 if (xa_marked(xas->xa, mark))
982 return entry;
983 xas->xa_index = 1;
984 goto out;
985 }
986 xas->xa_node = xa_to_node(entry);
987 xas->xa_offset = xas->xa_index >> xas->xa_node->shift;
988 }
989
990 while (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 if (!xas->xa_node)
995 break;
996 advance = false;
997 continue;
998 }
999
1000 if (!advance) {
1001 entry = xa_entry(xas->xa, xas->xa_node, xas->xa_offset);
1002 if (xa_is_sibling(entry)) {
1003 xas->xa_offset = xa_to_sibling(entry);
1004 xas_move_index(xas, xas->xa_offset);
1005 }
1006 }
1007
1008 offset = xas_find_chunk(xas, advance, mark);
1009 if (offset > xas->xa_offset) {
1010 advance = false;
1011 xas_move_index(xas, offset);
1012 /* Mind the wrap */
1013 if ((xas->xa_index - 1) >= max)
1014 goto max;
1015 xas->xa_offset = offset;
1016 if (offset == XA_CHUNK_SIZE)
1017 continue;
1018 }
1019
1020 entry = xa_entry(xas->xa, xas->xa_node, xas->xa_offset);
1021 if (!xa_is_node(entry))
1022 return entry;
1023 xas->xa_node = xa_to_node(entry);
1024 xas_set_offset(xas);
1025 }
1026
1027out:
1028 if (!max)
1029 goto max;
1030bounds:
1031 xas->xa_node = XAS_BOUNDS;
1032 return NULL;
1033max:
1034 xas->xa_node = XAS_RESTART;
1035 return NULL;
1036}
1037EXPORT_SYMBOL_GPL(xas_find_marked);
1038
f8d5d0cc
MW
1039/**
1040 * xa_init_flags() - Initialise an empty XArray with flags.
1041 * @xa: XArray.
1042 * @flags: XA_FLAG values.
1043 *
1044 * If you need to initialise an XArray with special flags (eg you need
1045 * to take the lock from interrupt context), use this function instead
1046 * of xa_init().
1047 *
1048 * Context: Any context.
1049 */
1050void xa_init_flags(struct xarray *xa, gfp_t flags)
1051{
58d6ea30
MW
1052 unsigned int lock_type;
1053 static struct lock_class_key xa_lock_irq;
1054 static struct lock_class_key xa_lock_bh;
1055
f8d5d0cc
MW
1056 spin_lock_init(&xa->xa_lock);
1057 xa->xa_flags = flags;
1058 xa->xa_head = NULL;
58d6ea30
MW
1059
1060 lock_type = xa_lock_type(xa);
1061 if (lock_type == XA_LOCK_IRQ)
1062 lockdep_set_class(&xa->xa_lock, &xa_lock_irq);
1063 else if (lock_type == XA_LOCK_BH)
1064 lockdep_set_class(&xa->xa_lock, &xa_lock_bh);
f8d5d0cc
MW
1065}
1066EXPORT_SYMBOL(xa_init_flags);
ad3d6c72
MW
1067
1068/**
1069 * xa_load() - Load an entry from an XArray.
1070 * @xa: XArray.
1071 * @index: index into array.
1072 *
1073 * Context: Any context. Takes and releases the RCU lock.
1074 * Return: The entry at @index in @xa.
1075 */
1076void *xa_load(struct xarray *xa, unsigned long index)
1077{
1078 XA_STATE(xas, xa, index);
1079 void *entry;
1080
1081 rcu_read_lock();
1082 do {
1083 entry = xas_load(&xas);
1084 } while (xas_retry(&xas, entry));
1085 rcu_read_unlock();
1086
1087 return entry;
1088}
1089EXPORT_SYMBOL(xa_load);
1090
58d6ea30
MW
1091static void *xas_result(struct xa_state *xas, void *curr)
1092{
1093 XA_NODE_BUG_ON(xas->xa_node, xa_is_internal(curr));
1094 if (xas_error(xas))
1095 curr = xas->xa_node;
1096 return curr;
1097}
1098
1099/**
1100 * __xa_erase() - Erase this entry from the XArray while locked.
1101 * @xa: XArray.
1102 * @index: Index into array.
1103 *
1104 * If the entry at this index is a multi-index entry then all indices will
1105 * be erased, and the entry will no longer be a multi-index entry.
1106 * This function expects the xa_lock to be held on entry.
1107 *
1108 * Context: Any context. Expects xa_lock to be held on entry. May
1109 * release and reacquire xa_lock if @gfp flags permit.
1110 * Return: The old entry at this index.
1111 */
1112void *__xa_erase(struct xarray *xa, unsigned long index)
1113{
1114 XA_STATE(xas, xa, index);
1115 return xas_result(&xas, xas_store(&xas, NULL));
1116}
1117EXPORT_SYMBOL_GPL(__xa_erase);
1118
1119/**
1120 * xa_store() - Store this entry in the XArray.
1121 * @xa: XArray.
1122 * @index: Index into array.
1123 * @entry: New entry.
1124 * @gfp: Memory allocation flags.
1125 *
1126 * After this function returns, loads from this index will return @entry.
1127 * Storing into an existing multislot entry updates the entry of every index.
1128 * The marks associated with @index are unaffected unless @entry is %NULL.
1129 *
1130 * Context: Process context. Takes and releases the xa_lock. May sleep
1131 * if the @gfp flags permit.
1132 * Return: The old entry at this index on success, xa_err(-EINVAL) if @entry
1133 * cannot be stored in an XArray, or xa_err(-ENOMEM) if memory allocation
1134 * failed.
1135 */
1136void *xa_store(struct xarray *xa, unsigned long index, void *entry, gfp_t gfp)
1137{
1138 XA_STATE(xas, xa, index);
1139 void *curr;
1140
1141 if (WARN_ON_ONCE(xa_is_internal(entry)))
1142 return XA_ERROR(-EINVAL);
1143
1144 do {
1145 xas_lock(&xas);
1146 curr = xas_store(&xas, entry);
1147 xas_unlock(&xas);
1148 } while (xas_nomem(&xas, gfp));
1149
1150 return xas_result(&xas, curr);
1151}
1152EXPORT_SYMBOL(xa_store);
1153
1154/**
1155 * __xa_store() - Store this entry in the XArray.
1156 * @xa: XArray.
1157 * @index: Index into array.
1158 * @entry: New entry.
1159 * @gfp: Memory allocation flags.
1160 *
1161 * You must already be holding the xa_lock when calling this function.
1162 * It will drop the lock if needed to allocate memory, and then reacquire
1163 * it afterwards.
1164 *
1165 * Context: Any context. Expects xa_lock to be held on entry. May
1166 * release and reacquire xa_lock if @gfp flags permit.
1167 * Return: The old entry at this index or xa_err() if an error happened.
1168 */
1169void *__xa_store(struct xarray *xa, unsigned long index, void *entry, gfp_t gfp)
1170{
1171 XA_STATE(xas, xa, index);
1172 void *curr;
1173
1174 if (WARN_ON_ONCE(xa_is_internal(entry)))
1175 return XA_ERROR(-EINVAL);
1176
1177 do {
1178 curr = xas_store(&xas, entry);
1179 } while (__xas_nomem(&xas, gfp));
1180
1181 return xas_result(&xas, curr);
1182}
1183EXPORT_SYMBOL(__xa_store);
1184
41aec91f
MW
1185/**
1186 * xa_cmpxchg() - Conditionally replace an entry in the XArray.
1187 * @xa: XArray.
1188 * @index: Index into array.
1189 * @old: Old value to test against.
1190 * @entry: New value to place in array.
1191 * @gfp: Memory allocation flags.
1192 *
1193 * If the entry at @index is the same as @old, replace it with @entry.
1194 * If the return value is equal to @old, then the exchange was successful.
1195 *
1196 * Context: Process context. Takes and releases the xa_lock. May sleep
1197 * if the @gfp flags permit.
1198 * Return: The old value at this index or xa_err() if an error happened.
1199 */
1200void *xa_cmpxchg(struct xarray *xa, unsigned long index,
1201 void *old, void *entry, gfp_t gfp)
1202{
1203 XA_STATE(xas, xa, index);
1204 void *curr;
1205
1206 if (WARN_ON_ONCE(xa_is_internal(entry)))
1207 return XA_ERROR(-EINVAL);
1208
1209 do {
1210 xas_lock(&xas);
1211 curr = xas_load(&xas);
1212 if (curr == old)
1213 xas_store(&xas, entry);
1214 xas_unlock(&xas);
1215 } while (xas_nomem(&xas, gfp));
1216
1217 return xas_result(&xas, curr);
1218}
1219EXPORT_SYMBOL(xa_cmpxchg);
1220
1221/**
1222 * __xa_cmpxchg() - Store this entry in the XArray.
1223 * @xa: XArray.
1224 * @index: Index into array.
1225 * @old: Old value to test against.
1226 * @entry: New entry.
1227 * @gfp: Memory allocation flags.
1228 *
1229 * You must already be holding the xa_lock when calling this function.
1230 * It will drop the lock if needed to allocate memory, and then reacquire
1231 * it afterwards.
1232 *
1233 * Context: Any context. Expects xa_lock to be held on entry. May
1234 * release and reacquire xa_lock if @gfp flags permit.
1235 * Return: The old entry at this index or xa_err() if an error happened.
1236 */
1237void *__xa_cmpxchg(struct xarray *xa, unsigned long index,
1238 void *old, void *entry, gfp_t gfp)
1239{
1240 XA_STATE(xas, xa, index);
1241 void *curr;
1242
1243 if (WARN_ON_ONCE(xa_is_internal(entry)))
1244 return XA_ERROR(-EINVAL);
1245
1246 do {
1247 curr = xas_load(&xas);
1248 if (curr == old)
1249 xas_store(&xas, entry);
1250 } while (__xas_nomem(&xas, gfp));
1251
1252 return xas_result(&xas, curr);
1253}
1254EXPORT_SYMBOL(__xa_cmpxchg);
1255
9b89a035
MW
1256/**
1257 * __xa_set_mark() - Set this mark on this entry while locked.
1258 * @xa: XArray.
1259 * @index: Index of entry.
1260 * @mark: Mark number.
1261 *
1262 * Attempting to set a mark on a NULL entry does not succeed.
1263 *
1264 * Context: Any context. Expects xa_lock to be held on entry.
1265 */
1266void __xa_set_mark(struct xarray *xa, unsigned long index, xa_mark_t mark)
1267{
1268 XA_STATE(xas, xa, index);
1269 void *entry = xas_load(&xas);
1270
1271 if (entry)
1272 xas_set_mark(&xas, mark);
1273}
1274EXPORT_SYMBOL_GPL(__xa_set_mark);
1275
1276/**
1277 * __xa_clear_mark() - Clear this mark on this entry while locked.
1278 * @xa: XArray.
1279 * @index: Index of entry.
1280 * @mark: Mark number.
1281 *
1282 * Context: Any context. Expects xa_lock to be held on entry.
1283 */
1284void __xa_clear_mark(struct xarray *xa, unsigned long index, xa_mark_t mark)
1285{
1286 XA_STATE(xas, xa, index);
1287 void *entry = xas_load(&xas);
1288
1289 if (entry)
1290 xas_clear_mark(&xas, mark);
1291}
1292EXPORT_SYMBOL_GPL(__xa_clear_mark);
1293
1294/**
1295 * xa_get_mark() - Inquire whether this mark is set on this entry.
1296 * @xa: XArray.
1297 * @index: Index of entry.
1298 * @mark: Mark number.
1299 *
1300 * This function uses the RCU read lock, so the result may be out of date
1301 * by the time it returns. If you need the result to be stable, use a lock.
1302 *
1303 * Context: Any context. Takes and releases the RCU lock.
1304 * Return: True if the entry at @index has this mark set, false if it doesn't.
1305 */
1306bool xa_get_mark(struct xarray *xa, unsigned long index, xa_mark_t mark)
1307{
1308 XA_STATE(xas, xa, index);
1309 void *entry;
1310
1311 rcu_read_lock();
1312 entry = xas_start(&xas);
1313 while (xas_get_mark(&xas, mark)) {
1314 if (!xa_is_node(entry))
1315 goto found;
1316 entry = xas_descend(&xas, xa_to_node(entry));
1317 }
1318 rcu_read_unlock();
1319 return false;
1320 found:
1321 rcu_read_unlock();
1322 return true;
1323}
1324EXPORT_SYMBOL(xa_get_mark);
1325
1326/**
1327 * xa_set_mark() - Set this mark on this entry.
1328 * @xa: XArray.
1329 * @index: Index of entry.
1330 * @mark: Mark number.
1331 *
1332 * Attempting to set a mark on a NULL entry does not succeed.
1333 *
1334 * Context: Process context. Takes and releases the xa_lock.
1335 */
1336void xa_set_mark(struct xarray *xa, unsigned long index, xa_mark_t mark)
1337{
1338 xa_lock(xa);
1339 __xa_set_mark(xa, index, mark);
1340 xa_unlock(xa);
1341}
1342EXPORT_SYMBOL(xa_set_mark);
1343
1344/**
1345 * xa_clear_mark() - Clear this mark on this entry.
1346 * @xa: XArray.
1347 * @index: Index of entry.
1348 * @mark: Mark number.
1349 *
1350 * Clearing a mark always succeeds.
1351 *
1352 * Context: Process context. Takes and releases the xa_lock.
1353 */
1354void xa_clear_mark(struct xarray *xa, unsigned long index, xa_mark_t mark)
1355{
1356 xa_lock(xa);
1357 __xa_clear_mark(xa, index, mark);
1358 xa_unlock(xa);
1359}
1360EXPORT_SYMBOL(xa_clear_mark);
1361
b803b428
MW
1362/**
1363 * xa_find() - Search the XArray for an entry.
1364 * @xa: XArray.
1365 * @indexp: Pointer to an index.
1366 * @max: Maximum index to search to.
1367 * @filter: Selection criterion.
1368 *
1369 * Finds the entry in @xa which matches the @filter, and has the lowest
1370 * index that is at least @indexp and no more than @max.
1371 * If an entry is found, @indexp is updated to be the index of the entry.
1372 * This function is protected by the RCU read lock, so it may not find
1373 * entries which are being simultaneously added. It will not return an
1374 * %XA_RETRY_ENTRY; if you need to see retry entries, use xas_find().
1375 *
1376 * Context: Any context. Takes and releases the RCU lock.
1377 * Return: The entry, if found, otherwise %NULL.
1378 */
1379void *xa_find(struct xarray *xa, unsigned long *indexp,
1380 unsigned long max, xa_mark_t filter)
1381{
1382 XA_STATE(xas, xa, *indexp);
1383 void *entry;
1384
1385 rcu_read_lock();
1386 do {
1387 if ((__force unsigned int)filter < XA_MAX_MARKS)
1388 entry = xas_find_marked(&xas, max, filter);
1389 else
1390 entry = xas_find(&xas, max);
1391 } while (xas_retry(&xas, entry));
1392 rcu_read_unlock();
1393
1394 if (entry)
1395 *indexp = xas.xa_index;
1396 return entry;
1397}
1398EXPORT_SYMBOL(xa_find);
1399
1400/**
1401 * xa_find_after() - Search the XArray for a present entry.
1402 * @xa: XArray.
1403 * @indexp: Pointer to an index.
1404 * @max: Maximum index to search to.
1405 * @filter: Selection criterion.
1406 *
1407 * Finds the entry in @xa which matches the @filter and has the lowest
1408 * index that is above @indexp and no more than @max.
1409 * If an entry is found, @indexp is updated to be the index of the entry.
1410 * This function is protected by the RCU read lock, so it may miss entries
1411 * which are being simultaneously added. It will not return an
1412 * %XA_RETRY_ENTRY; if you need to see retry entries, use xas_find().
1413 *
1414 * Context: Any context. Takes and releases the RCU lock.
1415 * Return: The pointer, if found, otherwise %NULL.
1416 */
1417void *xa_find_after(struct xarray *xa, unsigned long *indexp,
1418 unsigned long max, xa_mark_t filter)
1419{
1420 XA_STATE(xas, xa, *indexp + 1);
1421 void *entry;
1422
1423 rcu_read_lock();
1424 for (;;) {
1425 if ((__force unsigned int)filter < XA_MAX_MARKS)
1426 entry = xas_find_marked(&xas, max, filter);
1427 else
1428 entry = xas_find(&xas, max);
1429 if (xas.xa_shift) {
1430 if (xas.xa_index & ((1UL << xas.xa_shift) - 1))
1431 continue;
1432 } else {
1433 if (xas.xa_offset < (xas.xa_index & XA_CHUNK_MASK))
1434 continue;
1435 }
1436 if (!xas_retry(&xas, entry))
1437 break;
1438 }
1439 rcu_read_unlock();
1440
1441 if (entry)
1442 *indexp = xas.xa_index;
1443 return entry;
1444}
1445EXPORT_SYMBOL(xa_find_after);
1446
ad3d6c72
MW
1447#ifdef XA_DEBUG
1448void xa_dump_node(const struct xa_node *node)
1449{
1450 unsigned i, j;
1451
1452 if (!node)
1453 return;
1454 if ((unsigned long)node & 3) {
1455 pr_cont("node %px\n", node);
1456 return;
1457 }
1458
1459 pr_cont("node %px %s %d parent %px shift %d count %d values %d "
1460 "array %px list %px %px marks",
1461 node, node->parent ? "offset" : "max", node->offset,
1462 node->parent, node->shift, node->count, node->nr_values,
1463 node->array, node->private_list.prev, node->private_list.next);
1464 for (i = 0; i < XA_MAX_MARKS; i++)
1465 for (j = 0; j < XA_MARK_LONGS; j++)
1466 pr_cont(" %lx", node->marks[i][j]);
1467 pr_cont("\n");
1468}
1469
1470void xa_dump_index(unsigned long index, unsigned int shift)
1471{
1472 if (!shift)
1473 pr_info("%lu: ", index);
1474 else if (shift >= BITS_PER_LONG)
1475 pr_info("0-%lu: ", ~0UL);
1476 else
1477 pr_info("%lu-%lu: ", index, index | ((1UL << shift) - 1));
1478}
1479
1480void xa_dump_entry(const void *entry, unsigned long index, unsigned long shift)
1481{
1482 if (!entry)
1483 return;
1484
1485 xa_dump_index(index, shift);
1486
1487 if (xa_is_node(entry)) {
1488 if (shift == 0) {
1489 pr_cont("%px\n", entry);
1490 } else {
1491 unsigned long i;
1492 struct xa_node *node = xa_to_node(entry);
1493 xa_dump_node(node);
1494 for (i = 0; i < XA_CHUNK_SIZE; i++)
1495 xa_dump_entry(node->slots[i],
1496 index + (i << node->shift), node->shift);
1497 }
1498 } else if (xa_is_value(entry))
1499 pr_cont("value %ld (0x%lx) [%px]\n", xa_to_value(entry),
1500 xa_to_value(entry), entry);
1501 else if (!xa_is_internal(entry))
1502 pr_cont("%px\n", entry);
1503 else if (xa_is_retry(entry))
1504 pr_cont("retry (%ld)\n", xa_to_internal(entry));
1505 else if (xa_is_sibling(entry))
1506 pr_cont("sibling (slot %ld)\n", xa_to_sibling(entry));
1507 else
1508 pr_cont("UNKNOWN ENTRY (%px)\n", entry);
1509}
1510
1511void xa_dump(const struct xarray *xa)
1512{
1513 void *entry = xa->xa_head;
1514 unsigned int shift = 0;
1515
1516 pr_info("xarray: %px head %px flags %x marks %d %d %d\n", xa, entry,
9b89a035
MW
1517 xa->xa_flags, xa_marked(xa, XA_MARK_0),
1518 xa_marked(xa, XA_MARK_1), xa_marked(xa, XA_MARK_2));
ad3d6c72
MW
1519 if (xa_is_node(entry))
1520 shift = xa_to_node(entry)->shift + XA_CHUNK_SHIFT;
1521 xa_dump_entry(entry, 0, shift);
1522}
1523#endif