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