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