xref: /openbmc/linux/include/linux/xarray.h (revision ebd09753)
1 /* SPDX-License-Identifier: GPL-2.0+ */
2 #ifndef _LINUX_XARRAY_H
3 #define _LINUX_XARRAY_H
4 /*
5  * eXtensible Arrays
6  * Copyright (c) 2017 Microsoft Corporation
7  * Author: Matthew Wilcox <willy@infradead.org>
8  *
9  * See Documentation/core-api/xarray.rst for how to use the XArray.
10  */
11 
12 #include <linux/bug.h>
13 #include <linux/compiler.h>
14 #include <linux/gfp.h>
15 #include <linux/kconfig.h>
16 #include <linux/kernel.h>
17 #include <linux/rcupdate.h>
18 #include <linux/spinlock.h>
19 #include <linux/types.h>
20 
21 /*
22  * The bottom two bits of the entry determine how the XArray interprets
23  * the contents:
24  *
25  * 00: Pointer entry
26  * 10: Internal entry
27  * x1: Value entry or tagged pointer
28  *
29  * Attempting to store internal entries in the XArray is a bug.
30  *
31  * Most internal entries are pointers to the next node in the tree.
32  * The following internal entries have a special meaning:
33  *
34  * 0-62: Sibling entries
35  * 256: Zero entry
36  * 257: Retry entry
37  *
38  * Errors are also represented as internal entries, but use the negative
39  * space (-4094 to -2).  They're never stored in the slots array; only
40  * returned by the normal API.
41  */
42 
43 #define BITS_PER_XA_VALUE	(BITS_PER_LONG - 1)
44 
45 /**
46  * xa_mk_value() - Create an XArray entry from an integer.
47  * @v: Value to store in XArray.
48  *
49  * Context: Any context.
50  * Return: An entry suitable for storing in the XArray.
51  */
52 static inline void *xa_mk_value(unsigned long v)
53 {
54 	WARN_ON((long)v < 0);
55 	return (void *)((v << 1) | 1);
56 }
57 
58 /**
59  * xa_to_value() - Get value stored in an XArray entry.
60  * @entry: XArray entry.
61  *
62  * Context: Any context.
63  * Return: The value stored in the XArray entry.
64  */
65 static inline unsigned long xa_to_value(const void *entry)
66 {
67 	return (unsigned long)entry >> 1;
68 }
69 
70 /**
71  * xa_is_value() - Determine if an entry is a value.
72  * @entry: XArray entry.
73  *
74  * Context: Any context.
75  * Return: True if the entry is a value, false if it is a pointer.
76  */
77 static inline bool xa_is_value(const void *entry)
78 {
79 	return (unsigned long)entry & 1;
80 }
81 
82 /**
83  * xa_tag_pointer() - Create an XArray entry for a tagged pointer.
84  * @p: Plain pointer.
85  * @tag: Tag value (0, 1 or 3).
86  *
87  * If the user of the XArray prefers, they can tag their pointers instead
88  * of storing value entries.  Three tags are available (0, 1 and 3).
89  * These are distinct from the xa_mark_t as they are not replicated up
90  * through the array and cannot be searched for.
91  *
92  * Context: Any context.
93  * Return: An XArray entry.
94  */
95 static inline void *xa_tag_pointer(void *p, unsigned long tag)
96 {
97 	return (void *)((unsigned long)p | tag);
98 }
99 
100 /**
101  * xa_untag_pointer() - Turn an XArray entry into a plain pointer.
102  * @entry: XArray entry.
103  *
104  * If you have stored a tagged pointer in the XArray, call this function
105  * to get the untagged version of the pointer.
106  *
107  * Context: Any context.
108  * Return: A pointer.
109  */
110 static inline void *xa_untag_pointer(void *entry)
111 {
112 	return (void *)((unsigned long)entry & ~3UL);
113 }
114 
115 /**
116  * xa_pointer_tag() - Get the tag stored in an XArray entry.
117  * @entry: XArray entry.
118  *
119  * If you have stored a tagged pointer in the XArray, call this function
120  * to get the tag of that pointer.
121  *
122  * Context: Any context.
123  * Return: A tag.
124  */
125 static inline unsigned int xa_pointer_tag(void *entry)
126 {
127 	return (unsigned long)entry & 3UL;
128 }
129 
130 /*
131  * xa_mk_internal() - Create an internal entry.
132  * @v: Value to turn into an internal entry.
133  *
134  * Context: Any context.
135  * Return: An XArray internal entry corresponding to this value.
136  */
137 static inline void *xa_mk_internal(unsigned long v)
138 {
139 	return (void *)((v << 2) | 2);
140 }
141 
142 /*
143  * xa_to_internal() - Extract the value from an internal entry.
144  * @entry: XArray entry.
145  *
146  * Context: Any context.
147  * Return: The value which was stored in the internal entry.
148  */
149 static inline unsigned long xa_to_internal(const void *entry)
150 {
151 	return (unsigned long)entry >> 2;
152 }
153 
154 /*
155  * xa_is_internal() - Is the entry an internal entry?
156  * @entry: XArray entry.
157  *
158  * Context: Any context.
159  * Return: %true if the entry is an internal entry.
160  */
161 static inline bool xa_is_internal(const void *entry)
162 {
163 	return ((unsigned long)entry & 3) == 2;
164 }
165 
166 /**
167  * xa_is_err() - Report whether an XArray operation returned an error
168  * @entry: Result from calling an XArray function
169  *
170  * If an XArray operation cannot complete an operation, it will return
171  * a special value indicating an error.  This function tells you
172  * whether an error occurred; xa_err() tells you which error occurred.
173  *
174  * Context: Any context.
175  * Return: %true if the entry indicates an error.
176  */
177 static inline bool xa_is_err(const void *entry)
178 {
179 	return unlikely(xa_is_internal(entry));
180 }
181 
182 /**
183  * xa_err() - Turn an XArray result into an errno.
184  * @entry: Result from calling an XArray function.
185  *
186  * If an XArray operation cannot complete an operation, it will return
187  * a special pointer value which encodes an errno.  This function extracts
188  * the errno from the pointer value, or returns 0 if the pointer does not
189  * represent an errno.
190  *
191  * Context: Any context.
192  * Return: A negative errno or 0.
193  */
194 static inline int xa_err(void *entry)
195 {
196 	/* xa_to_internal() would not do sign extension. */
197 	if (xa_is_err(entry))
198 		return (long)entry >> 2;
199 	return 0;
200 }
201 
202 typedef unsigned __bitwise xa_mark_t;
203 #define XA_MARK_0		((__force xa_mark_t)0U)
204 #define XA_MARK_1		((__force xa_mark_t)1U)
205 #define XA_MARK_2		((__force xa_mark_t)2U)
206 #define XA_PRESENT		((__force xa_mark_t)8U)
207 #define XA_MARK_MAX		XA_MARK_2
208 #define XA_FREE_MARK		XA_MARK_0
209 
210 enum xa_lock_type {
211 	XA_LOCK_IRQ = 1,
212 	XA_LOCK_BH = 2,
213 };
214 
215 /*
216  * Values for xa_flags.  The radix tree stores its GFP flags in the xa_flags,
217  * and we remain compatible with that.
218  */
219 #define XA_FLAGS_LOCK_IRQ	((__force gfp_t)XA_LOCK_IRQ)
220 #define XA_FLAGS_LOCK_BH	((__force gfp_t)XA_LOCK_BH)
221 #define XA_FLAGS_TRACK_FREE	((__force gfp_t)4U)
222 #define XA_FLAGS_MARK(mark)	((__force gfp_t)((1U << __GFP_BITS_SHIFT) << \
223 						(__force unsigned)(mark)))
224 
225 #define XA_FLAGS_ALLOC	(XA_FLAGS_TRACK_FREE | XA_FLAGS_MARK(XA_FREE_MARK))
226 
227 /**
228  * struct xarray - The anchor of the XArray.
229  * @xa_lock: Lock that protects the contents of the XArray.
230  *
231  * To use the xarray, define it statically or embed it in your data structure.
232  * It is a very small data structure, so it does not usually make sense to
233  * allocate it separately and keep a pointer to it in your data structure.
234  *
235  * You may use the xa_lock to protect your own data structures as well.
236  */
237 /*
238  * If all of the entries in the array are NULL, @xa_head is a NULL pointer.
239  * If the only non-NULL entry in the array is at index 0, @xa_head is that
240  * entry.  If any other entry in the array is non-NULL, @xa_head points
241  * to an @xa_node.
242  */
243 struct xarray {
244 	spinlock_t	xa_lock;
245 /* private: The rest of the data structure is not to be used directly. */
246 	gfp_t		xa_flags;
247 	void __rcu *	xa_head;
248 };
249 
250 #define XARRAY_INIT(name, flags) {				\
251 	.xa_lock = __SPIN_LOCK_UNLOCKED(name.xa_lock),		\
252 	.xa_flags = flags,					\
253 	.xa_head = NULL,					\
254 }
255 
256 /**
257  * DEFINE_XARRAY_FLAGS() - Define an XArray with custom flags.
258  * @name: A string that names your XArray.
259  * @flags: XA_FLAG values.
260  *
261  * This is intended for file scope definitions of XArrays.  It declares
262  * and initialises an empty XArray with the chosen name and flags.  It is
263  * equivalent to calling xa_init_flags() on the array, but it does the
264  * initialisation at compiletime instead of runtime.
265  */
266 #define DEFINE_XARRAY_FLAGS(name, flags)				\
267 	struct xarray name = XARRAY_INIT(name, flags)
268 
269 /**
270  * DEFINE_XARRAY() - Define an XArray.
271  * @name: A string that names your XArray.
272  *
273  * This is intended for file scope definitions of XArrays.  It declares
274  * and initialises an empty XArray with the chosen name.  It is equivalent
275  * to calling xa_init() on the array, but it does the initialisation at
276  * compiletime instead of runtime.
277  */
278 #define DEFINE_XARRAY(name) DEFINE_XARRAY_FLAGS(name, 0)
279 
280 /**
281  * DEFINE_XARRAY_ALLOC() - Define an XArray which can allocate IDs.
282  * @name: A string that names your XArray.
283  *
284  * This is intended for file scope definitions of allocating XArrays.
285  * See also DEFINE_XARRAY().
286  */
287 #define DEFINE_XARRAY_ALLOC(name) DEFINE_XARRAY_FLAGS(name, XA_FLAGS_ALLOC)
288 
289 void xa_init_flags(struct xarray *, gfp_t flags);
290 void *xa_load(struct xarray *, unsigned long index);
291 void *xa_store(struct xarray *, unsigned long index, void *entry, gfp_t);
292 void *xa_cmpxchg(struct xarray *, unsigned long index,
293 			void *old, void *entry, gfp_t);
294 int xa_reserve(struct xarray *, unsigned long index, gfp_t);
295 void *xa_store_range(struct xarray *, unsigned long first, unsigned long last,
296 			void *entry, gfp_t);
297 bool xa_get_mark(struct xarray *, unsigned long index, xa_mark_t);
298 void xa_set_mark(struct xarray *, unsigned long index, xa_mark_t);
299 void xa_clear_mark(struct xarray *, unsigned long index, xa_mark_t);
300 void *xa_find(struct xarray *xa, unsigned long *index,
301 		unsigned long max, xa_mark_t) __attribute__((nonnull(2)));
302 void *xa_find_after(struct xarray *xa, unsigned long *index,
303 		unsigned long max, xa_mark_t) __attribute__((nonnull(2)));
304 unsigned int xa_extract(struct xarray *, void **dst, unsigned long start,
305 		unsigned long max, unsigned int n, xa_mark_t);
306 void xa_destroy(struct xarray *);
307 
308 /**
309  * xa_init() - Initialise an empty XArray.
310  * @xa: XArray.
311  *
312  * An empty XArray is full of NULL entries.
313  *
314  * Context: Any context.
315  */
316 static inline void xa_init(struct xarray *xa)
317 {
318 	xa_init_flags(xa, 0);
319 }
320 
321 /**
322  * xa_empty() - Determine if an array has any present entries.
323  * @xa: XArray.
324  *
325  * Context: Any context.
326  * Return: %true if the array contains only NULL pointers.
327  */
328 static inline bool xa_empty(const struct xarray *xa)
329 {
330 	return xa->xa_head == NULL;
331 }
332 
333 /**
334  * xa_marked() - Inquire whether any entry in this array has a mark set
335  * @xa: Array
336  * @mark: Mark value
337  *
338  * Context: Any context.
339  * Return: %true if any entry has this mark set.
340  */
341 static inline bool xa_marked(const struct xarray *xa, xa_mark_t mark)
342 {
343 	return xa->xa_flags & XA_FLAGS_MARK(mark);
344 }
345 
346 /**
347  * xa_erase() - Erase this entry from the XArray.
348  * @xa: XArray.
349  * @index: Index of entry.
350  *
351  * This function is the equivalent of calling xa_store() with %NULL as
352  * the third argument.  The XArray does not need to allocate memory, so
353  * the user does not need to provide GFP flags.
354  *
355  * Context: Process context.  Takes and releases the xa_lock.
356  * Return: The entry which used to be at this index.
357  */
358 static inline void *xa_erase(struct xarray *xa, unsigned long index)
359 {
360 	return xa_store(xa, index, NULL, 0);
361 }
362 
363 /**
364  * xa_insert() - Store this entry in the XArray unless another entry is
365  *			already present.
366  * @xa: XArray.
367  * @index: Index into array.
368  * @entry: New entry.
369  * @gfp: Memory allocation flags.
370  *
371  * If you would rather see the existing entry in the array, use xa_cmpxchg().
372  * This function is for users who don't care what the entry is, only that
373  * one is present.
374  *
375  * Context: Process context.  Takes and releases the xa_lock.
376  *	    May sleep if the @gfp flags permit.
377  * Return: 0 if the store succeeded.  -EEXIST if another entry was present.
378  * -ENOMEM if memory could not be allocated.
379  */
380 static inline int xa_insert(struct xarray *xa, unsigned long index,
381 		void *entry, gfp_t gfp)
382 {
383 	void *curr = xa_cmpxchg(xa, index, NULL, entry, gfp);
384 	if (!curr)
385 		return 0;
386 	if (xa_is_err(curr))
387 		return xa_err(curr);
388 	return -EEXIST;
389 }
390 
391 /**
392  * xa_release() - Release a reserved entry.
393  * @xa: XArray.
394  * @index: Index of entry.
395  *
396  * After calling xa_reserve(), you can call this function to release the
397  * reservation.  If the entry at @index has been stored to, this function
398  * will do nothing.
399  */
400 static inline void xa_release(struct xarray *xa, unsigned long index)
401 {
402 	xa_cmpxchg(xa, index, NULL, NULL, 0);
403 }
404 
405 /**
406  * xa_for_each() - Iterate over a portion of an XArray.
407  * @xa: XArray.
408  * @entry: Entry retrieved from array.
409  * @index: Index of @entry.
410  * @max: Maximum index to retrieve from array.
411  * @filter: Selection criterion.
412  *
413  * Initialise @index to the lowest index you want to retrieve from the
414  * array.  During the iteration, @entry will have the value of the entry
415  * stored in @xa at @index.  The iteration will skip all entries in the
416  * array which do not match @filter.  You may modify @index during the
417  * iteration if you want to skip or reprocess indices.  It is safe to modify
418  * the array during the iteration.  At the end of the iteration, @entry will
419  * be set to NULL and @index will have a value less than or equal to max.
420  *
421  * xa_for_each() is O(n.log(n)) while xas_for_each() is O(n).  You have
422  * to handle your own locking with xas_for_each(), and if you have to unlock
423  * after each iteration, it will also end up being O(n.log(n)).  xa_for_each()
424  * will spin if it hits a retry entry; if you intend to see retry entries,
425  * you should use the xas_for_each() iterator instead.  The xas_for_each()
426  * iterator will expand into more inline code than xa_for_each().
427  *
428  * Context: Any context.  Takes and releases the RCU lock.
429  */
430 #define xa_for_each(xa, entry, index, max, filter) \
431 	for (entry = xa_find(xa, &index, max, filter); entry; \
432 	     entry = xa_find_after(xa, &index, max, filter))
433 
434 #define xa_trylock(xa)		spin_trylock(&(xa)->xa_lock)
435 #define xa_lock(xa)		spin_lock(&(xa)->xa_lock)
436 #define xa_unlock(xa)		spin_unlock(&(xa)->xa_lock)
437 #define xa_lock_bh(xa)		spin_lock_bh(&(xa)->xa_lock)
438 #define xa_unlock_bh(xa)	spin_unlock_bh(&(xa)->xa_lock)
439 #define xa_lock_irq(xa)		spin_lock_irq(&(xa)->xa_lock)
440 #define xa_unlock_irq(xa)	spin_unlock_irq(&(xa)->xa_lock)
441 #define xa_lock_irqsave(xa, flags) \
442 				spin_lock_irqsave(&(xa)->xa_lock, flags)
443 #define xa_unlock_irqrestore(xa, flags) \
444 				spin_unlock_irqrestore(&(xa)->xa_lock, flags)
445 
446 /*
447  * Versions of the normal API which require the caller to hold the
448  * xa_lock.  If the GFP flags allow it, they will drop the lock to
449  * allocate memory, then reacquire it afterwards.  These functions
450  * may also re-enable interrupts if the XArray flags indicate the
451  * locking should be interrupt safe.
452  */
453 void *__xa_erase(struct xarray *, unsigned long index);
454 void *__xa_store(struct xarray *, unsigned long index, void *entry, gfp_t);
455 void *__xa_cmpxchg(struct xarray *, unsigned long index, void *old,
456 		void *entry, gfp_t);
457 int __xa_alloc(struct xarray *, u32 *id, u32 max, void *entry, gfp_t);
458 void __xa_set_mark(struct xarray *, unsigned long index, xa_mark_t);
459 void __xa_clear_mark(struct xarray *, unsigned long index, xa_mark_t);
460 
461 /**
462  * __xa_insert() - Store this entry in the XArray unless another entry is
463  *			already present.
464  * @xa: XArray.
465  * @index: Index into array.
466  * @entry: New entry.
467  * @gfp: Memory allocation flags.
468  *
469  * If you would rather see the existing entry in the array, use __xa_cmpxchg().
470  * This function is for users who don't care what the entry is, only that
471  * one is present.
472  *
473  * Context: Any context.  Expects xa_lock to be held on entry.  May
474  *	    release and reacquire xa_lock if the @gfp flags permit.
475  * Return: 0 if the store succeeded.  -EEXIST if another entry was present.
476  * -ENOMEM if memory could not be allocated.
477  */
478 static inline int __xa_insert(struct xarray *xa, unsigned long index,
479 		void *entry, gfp_t gfp)
480 {
481 	void *curr = __xa_cmpxchg(xa, index, NULL, entry, gfp);
482 	if (!curr)
483 		return 0;
484 	if (xa_is_err(curr))
485 		return xa_err(curr);
486 	return -EEXIST;
487 }
488 
489 /**
490  * xa_erase_bh() - Erase this entry from the XArray.
491  * @xa: XArray.
492  * @index: Index of entry.
493  *
494  * This function is the equivalent of calling xa_store() with %NULL as
495  * the third argument.  The XArray does not need to allocate memory, so
496  * the user does not need to provide GFP flags.
497  *
498  * Context: Process context.  Takes and releases the xa_lock while
499  * disabling softirqs.
500  * Return: The entry which used to be at this index.
501  */
502 static inline void *xa_erase_bh(struct xarray *xa, unsigned long index)
503 {
504 	void *entry;
505 
506 	xa_lock_bh(xa);
507 	entry = __xa_erase(xa, index);
508 	xa_unlock_bh(xa);
509 
510 	return entry;
511 }
512 
513 /**
514  * xa_erase_irq() - Erase this entry from the XArray.
515  * @xa: XArray.
516  * @index: Index of entry.
517  *
518  * This function is the equivalent of calling xa_store() with %NULL as
519  * the third argument.  The XArray does not need to allocate memory, so
520  * the user does not need to provide GFP flags.
521  *
522  * Context: Process context.  Takes and releases the xa_lock while
523  * disabling interrupts.
524  * Return: The entry which used to be at this index.
525  */
526 static inline void *xa_erase_irq(struct xarray *xa, unsigned long index)
527 {
528 	void *entry;
529 
530 	xa_lock_irq(xa);
531 	entry = __xa_erase(xa, index);
532 	xa_unlock_irq(xa);
533 
534 	return entry;
535 }
536 
537 /**
538  * xa_alloc() - Find somewhere to store this entry in the XArray.
539  * @xa: XArray.
540  * @id: Pointer to ID.
541  * @max: Maximum ID to allocate (inclusive).
542  * @entry: New entry.
543  * @gfp: Memory allocation flags.
544  *
545  * Allocates an unused ID in the range specified by @id and @max.
546  * Updates the @id pointer with the index, then stores the entry at that
547  * index.  A concurrent lookup will not see an uninitialised @id.
548  *
549  * Context: Process context.  Takes and releases the xa_lock.  May sleep if
550  * the @gfp flags permit.
551  * Return: 0 on success, -ENOMEM if memory allocation fails or -ENOSPC if
552  * there is no more space in the XArray.
553  */
554 static inline int xa_alloc(struct xarray *xa, u32 *id, u32 max, void *entry,
555 		gfp_t gfp)
556 {
557 	int err;
558 
559 	xa_lock(xa);
560 	err = __xa_alloc(xa, id, max, entry, gfp);
561 	xa_unlock(xa);
562 
563 	return err;
564 }
565 
566 /**
567  * xa_alloc_bh() - Find somewhere to store this entry in the XArray.
568  * @xa: XArray.
569  * @id: Pointer to ID.
570  * @max: Maximum ID to allocate (inclusive).
571  * @entry: New entry.
572  * @gfp: Memory allocation flags.
573  *
574  * Allocates an unused ID in the range specified by @id and @max.
575  * Updates the @id pointer with the index, then stores the entry at that
576  * index.  A concurrent lookup will not see an uninitialised @id.
577  *
578  * Context: Process context.  Takes and releases the xa_lock while
579  * disabling softirqs.  May sleep if the @gfp flags permit.
580  * Return: 0 on success, -ENOMEM if memory allocation fails or -ENOSPC if
581  * there is no more space in the XArray.
582  */
583 static inline int xa_alloc_bh(struct xarray *xa, u32 *id, u32 max, void *entry,
584 		gfp_t gfp)
585 {
586 	int err;
587 
588 	xa_lock_bh(xa);
589 	err = __xa_alloc(xa, id, max, entry, gfp);
590 	xa_unlock_bh(xa);
591 
592 	return err;
593 }
594 
595 /**
596  * xa_alloc_irq() - Find somewhere to store this entry in the XArray.
597  * @xa: XArray.
598  * @id: Pointer to ID.
599  * @max: Maximum ID to allocate (inclusive).
600  * @entry: New entry.
601  * @gfp: Memory allocation flags.
602  *
603  * Allocates an unused ID in the range specified by @id and @max.
604  * Updates the @id pointer with the index, then stores the entry at that
605  * index.  A concurrent lookup will not see an uninitialised @id.
606  *
607  * Context: Process context.  Takes and releases the xa_lock while
608  * disabling interrupts.  May sleep if the @gfp flags permit.
609  * Return: 0 on success, -ENOMEM if memory allocation fails or -ENOSPC if
610  * there is no more space in the XArray.
611  */
612 static inline int xa_alloc_irq(struct xarray *xa, u32 *id, u32 max, void *entry,
613 		gfp_t gfp)
614 {
615 	int err;
616 
617 	xa_lock_irq(xa);
618 	err = __xa_alloc(xa, id, max, entry, gfp);
619 	xa_unlock_irq(xa);
620 
621 	return err;
622 }
623 
624 /* Everything below here is the Advanced API.  Proceed with caution. */
625 
626 /*
627  * The xarray is constructed out of a set of 'chunks' of pointers.  Choosing
628  * the best chunk size requires some tradeoffs.  A power of two recommends
629  * itself so that we can walk the tree based purely on shifts and masks.
630  * Generally, the larger the better; as the number of slots per level of the
631  * tree increases, the less tall the tree needs to be.  But that needs to be
632  * balanced against the memory consumption of each node.  On a 64-bit system,
633  * xa_node is currently 576 bytes, and we get 7 of them per 4kB page.  If we
634  * doubled the number of slots per node, we'd get only 3 nodes per 4kB page.
635  */
636 #ifndef XA_CHUNK_SHIFT
637 #define XA_CHUNK_SHIFT		(CONFIG_BASE_SMALL ? 4 : 6)
638 #endif
639 #define XA_CHUNK_SIZE		(1UL << XA_CHUNK_SHIFT)
640 #define XA_CHUNK_MASK		(XA_CHUNK_SIZE - 1)
641 #define XA_MAX_MARKS		3
642 #define XA_MARK_LONGS		DIV_ROUND_UP(XA_CHUNK_SIZE, BITS_PER_LONG)
643 
644 /*
645  * @count is the count of every non-NULL element in the ->slots array
646  * whether that is a value entry, a retry entry, a user pointer,
647  * a sibling entry or a pointer to the next level of the tree.
648  * @nr_values is the count of every element in ->slots which is
649  * either a value entry or a sibling of a value entry.
650  */
651 struct xa_node {
652 	unsigned char	shift;		/* Bits remaining in each slot */
653 	unsigned char	offset;		/* Slot offset in parent */
654 	unsigned char	count;		/* Total entry count */
655 	unsigned char	nr_values;	/* Value entry count */
656 	struct xa_node __rcu *parent;	/* NULL at top of tree */
657 	struct xarray	*array;		/* The array we belong to */
658 	union {
659 		struct list_head private_list;	/* For tree user */
660 		struct rcu_head	rcu_head;	/* Used when freeing node */
661 	};
662 	void __rcu	*slots[XA_CHUNK_SIZE];
663 	union {
664 		unsigned long	tags[XA_MAX_MARKS][XA_MARK_LONGS];
665 		unsigned long	marks[XA_MAX_MARKS][XA_MARK_LONGS];
666 	};
667 };
668 
669 void xa_dump(const struct xarray *);
670 void xa_dump_node(const struct xa_node *);
671 
672 #ifdef XA_DEBUG
673 #define XA_BUG_ON(xa, x) do {					\
674 		if (x) {					\
675 			xa_dump(xa);				\
676 			BUG();					\
677 		}						\
678 	} while (0)
679 #define XA_NODE_BUG_ON(node, x) do {				\
680 		if (x) {					\
681 			if (node) xa_dump_node(node);		\
682 			BUG();					\
683 		}						\
684 	} while (0)
685 #else
686 #define XA_BUG_ON(xa, x)	do { } while (0)
687 #define XA_NODE_BUG_ON(node, x)	do { } while (0)
688 #endif
689 
690 /* Private */
691 static inline void *xa_head(const struct xarray *xa)
692 {
693 	return rcu_dereference_check(xa->xa_head,
694 						lockdep_is_held(&xa->xa_lock));
695 }
696 
697 /* Private */
698 static inline void *xa_head_locked(const struct xarray *xa)
699 {
700 	return rcu_dereference_protected(xa->xa_head,
701 						lockdep_is_held(&xa->xa_lock));
702 }
703 
704 /* Private */
705 static inline void *xa_entry(const struct xarray *xa,
706 				const struct xa_node *node, unsigned int offset)
707 {
708 	XA_NODE_BUG_ON(node, offset >= XA_CHUNK_SIZE);
709 	return rcu_dereference_check(node->slots[offset],
710 						lockdep_is_held(&xa->xa_lock));
711 }
712 
713 /* Private */
714 static inline void *xa_entry_locked(const struct xarray *xa,
715 				const struct xa_node *node, unsigned int offset)
716 {
717 	XA_NODE_BUG_ON(node, offset >= XA_CHUNK_SIZE);
718 	return rcu_dereference_protected(node->slots[offset],
719 						lockdep_is_held(&xa->xa_lock));
720 }
721 
722 /* Private */
723 static inline struct xa_node *xa_parent(const struct xarray *xa,
724 					const struct xa_node *node)
725 {
726 	return rcu_dereference_check(node->parent,
727 						lockdep_is_held(&xa->xa_lock));
728 }
729 
730 /* Private */
731 static inline struct xa_node *xa_parent_locked(const struct xarray *xa,
732 					const struct xa_node *node)
733 {
734 	return rcu_dereference_protected(node->parent,
735 						lockdep_is_held(&xa->xa_lock));
736 }
737 
738 /* Private */
739 static inline void *xa_mk_node(const struct xa_node *node)
740 {
741 	return (void *)((unsigned long)node | 2);
742 }
743 
744 /* Private */
745 static inline struct xa_node *xa_to_node(const void *entry)
746 {
747 	return (struct xa_node *)((unsigned long)entry - 2);
748 }
749 
750 /* Private */
751 static inline bool xa_is_node(const void *entry)
752 {
753 	return xa_is_internal(entry) && (unsigned long)entry > 4096;
754 }
755 
756 /* Private */
757 static inline void *xa_mk_sibling(unsigned int offset)
758 {
759 	return xa_mk_internal(offset);
760 }
761 
762 /* Private */
763 static inline unsigned long xa_to_sibling(const void *entry)
764 {
765 	return xa_to_internal(entry);
766 }
767 
768 /**
769  * xa_is_sibling() - Is the entry a sibling entry?
770  * @entry: Entry retrieved from the XArray
771  *
772  * Return: %true if the entry is a sibling entry.
773  */
774 static inline bool xa_is_sibling(const void *entry)
775 {
776 	return IS_ENABLED(CONFIG_XARRAY_MULTI) && xa_is_internal(entry) &&
777 		(entry < xa_mk_sibling(XA_CHUNK_SIZE - 1));
778 }
779 
780 #define XA_ZERO_ENTRY		xa_mk_internal(256)
781 #define XA_RETRY_ENTRY		xa_mk_internal(257)
782 
783 /**
784  * xa_is_zero() - Is the entry a zero entry?
785  * @entry: Entry retrieved from the XArray
786  *
787  * Return: %true if the entry is a zero entry.
788  */
789 static inline bool xa_is_zero(const void *entry)
790 {
791 	return unlikely(entry == XA_ZERO_ENTRY);
792 }
793 
794 /**
795  * xa_is_retry() - Is the entry a retry entry?
796  * @entry: Entry retrieved from the XArray
797  *
798  * Return: %true if the entry is a retry entry.
799  */
800 static inline bool xa_is_retry(const void *entry)
801 {
802 	return unlikely(entry == XA_RETRY_ENTRY);
803 }
804 
805 /**
806  * typedef xa_update_node_t - A callback function from the XArray.
807  * @node: The node which is being processed
808  *
809  * This function is called every time the XArray updates the count of
810  * present and value entries in a node.  It allows advanced users to
811  * maintain the private_list in the node.
812  *
813  * Context: The xa_lock is held and interrupts may be disabled.
814  *	    Implementations should not drop the xa_lock, nor re-enable
815  *	    interrupts.
816  */
817 typedef void (*xa_update_node_t)(struct xa_node *node);
818 
819 /*
820  * The xa_state is opaque to its users.  It contains various different pieces
821  * of state involved in the current operation on the XArray.  It should be
822  * declared on the stack and passed between the various internal routines.
823  * The various elements in it should not be accessed directly, but only
824  * through the provided accessor functions.  The below documentation is for
825  * the benefit of those working on the code, not for users of the XArray.
826  *
827  * @xa_node usually points to the xa_node containing the slot we're operating
828  * on (and @xa_offset is the offset in the slots array).  If there is a
829  * single entry in the array at index 0, there are no allocated xa_nodes to
830  * point to, and so we store %NULL in @xa_node.  @xa_node is set to
831  * the value %XAS_RESTART if the xa_state is not walked to the correct
832  * position in the tree of nodes for this operation.  If an error occurs
833  * during an operation, it is set to an %XAS_ERROR value.  If we run off the
834  * end of the allocated nodes, it is set to %XAS_BOUNDS.
835  */
836 struct xa_state {
837 	struct xarray *xa;
838 	unsigned long xa_index;
839 	unsigned char xa_shift;
840 	unsigned char xa_sibs;
841 	unsigned char xa_offset;
842 	unsigned char xa_pad;		/* Helps gcc generate better code */
843 	struct xa_node *xa_node;
844 	struct xa_node *xa_alloc;
845 	xa_update_node_t xa_update;
846 };
847 
848 /*
849  * We encode errnos in the xas->xa_node.  If an error has happened, we need to
850  * drop the lock to fix it, and once we've done so the xa_state is invalid.
851  */
852 #define XA_ERROR(errno) ((struct xa_node *)(((unsigned long)errno << 2) | 2UL))
853 #define XAS_BOUNDS	((struct xa_node *)1UL)
854 #define XAS_RESTART	((struct xa_node *)3UL)
855 
856 #define __XA_STATE(array, index, shift, sibs)  {	\
857 	.xa = array,					\
858 	.xa_index = index,				\
859 	.xa_shift = shift,				\
860 	.xa_sibs = sibs,				\
861 	.xa_offset = 0,					\
862 	.xa_pad = 0,					\
863 	.xa_node = XAS_RESTART,				\
864 	.xa_alloc = NULL,				\
865 	.xa_update = NULL				\
866 }
867 
868 /**
869  * XA_STATE() - Declare an XArray operation state.
870  * @name: Name of this operation state (usually xas).
871  * @array: Array to operate on.
872  * @index: Initial index of interest.
873  *
874  * Declare and initialise an xa_state on the stack.
875  */
876 #define XA_STATE(name, array, index)				\
877 	struct xa_state name = __XA_STATE(array, index, 0, 0)
878 
879 /**
880  * XA_STATE_ORDER() - Declare an XArray operation state.
881  * @name: Name of this operation state (usually xas).
882  * @array: Array to operate on.
883  * @index: Initial index of interest.
884  * @order: Order of entry.
885  *
886  * Declare and initialise an xa_state on the stack.  This variant of
887  * XA_STATE() allows you to specify the 'order' of the element you
888  * want to operate on.`
889  */
890 #define XA_STATE_ORDER(name, array, index, order)		\
891 	struct xa_state name = __XA_STATE(array,		\
892 			(index >> order) << order,		\
893 			order - (order % XA_CHUNK_SHIFT),	\
894 			(1U << (order % XA_CHUNK_SHIFT)) - 1)
895 
896 #define xas_marked(xas, mark)	xa_marked((xas)->xa, (mark))
897 #define xas_trylock(xas)	xa_trylock((xas)->xa)
898 #define xas_lock(xas)		xa_lock((xas)->xa)
899 #define xas_unlock(xas)		xa_unlock((xas)->xa)
900 #define xas_lock_bh(xas)	xa_lock_bh((xas)->xa)
901 #define xas_unlock_bh(xas)	xa_unlock_bh((xas)->xa)
902 #define xas_lock_irq(xas)	xa_lock_irq((xas)->xa)
903 #define xas_unlock_irq(xas)	xa_unlock_irq((xas)->xa)
904 #define xas_lock_irqsave(xas, flags) \
905 				xa_lock_irqsave((xas)->xa, flags)
906 #define xas_unlock_irqrestore(xas, flags) \
907 				xa_unlock_irqrestore((xas)->xa, flags)
908 
909 /**
910  * xas_error() - Return an errno stored in the xa_state.
911  * @xas: XArray operation state.
912  *
913  * Return: 0 if no error has been noted.  A negative errno if one has.
914  */
915 static inline int xas_error(const struct xa_state *xas)
916 {
917 	return xa_err(xas->xa_node);
918 }
919 
920 /**
921  * xas_set_err() - Note an error in the xa_state.
922  * @xas: XArray operation state.
923  * @err: Negative error number.
924  *
925  * Only call this function with a negative @err; zero or positive errors
926  * will probably not behave the way you think they should.  If you want
927  * to clear the error from an xa_state, use xas_reset().
928  */
929 static inline void xas_set_err(struct xa_state *xas, long err)
930 {
931 	xas->xa_node = XA_ERROR(err);
932 }
933 
934 /**
935  * xas_invalid() - Is the xas in a retry or error state?
936  * @xas: XArray operation state.
937  *
938  * Return: %true if the xas cannot be used for operations.
939  */
940 static inline bool xas_invalid(const struct xa_state *xas)
941 {
942 	return (unsigned long)xas->xa_node & 3;
943 }
944 
945 /**
946  * xas_valid() - Is the xas a valid cursor into the array?
947  * @xas: XArray operation state.
948  *
949  * Return: %true if the xas can be used for operations.
950  */
951 static inline bool xas_valid(const struct xa_state *xas)
952 {
953 	return !xas_invalid(xas);
954 }
955 
956 /**
957  * xas_is_node() - Does the xas point to a node?
958  * @xas: XArray operation state.
959  *
960  * Return: %true if the xas currently references a node.
961  */
962 static inline bool xas_is_node(const struct xa_state *xas)
963 {
964 	return xas_valid(xas) && xas->xa_node;
965 }
966 
967 /* True if the pointer is something other than a node */
968 static inline bool xas_not_node(struct xa_node *node)
969 {
970 	return ((unsigned long)node & 3) || !node;
971 }
972 
973 /* True if the node represents RESTART or an error */
974 static inline bool xas_frozen(struct xa_node *node)
975 {
976 	return (unsigned long)node & 2;
977 }
978 
979 /* True if the node represents head-of-tree, RESTART or BOUNDS */
980 static inline bool xas_top(struct xa_node *node)
981 {
982 	return node <= XAS_RESTART;
983 }
984 
985 /**
986  * xas_reset() - Reset an XArray operation state.
987  * @xas: XArray operation state.
988  *
989  * Resets the error or walk state of the @xas so future walks of the
990  * array will start from the root.  Use this if you have dropped the
991  * xarray lock and want to reuse the xa_state.
992  *
993  * Context: Any context.
994  */
995 static inline void xas_reset(struct xa_state *xas)
996 {
997 	xas->xa_node = XAS_RESTART;
998 }
999 
1000 /**
1001  * xas_retry() - Retry the operation if appropriate.
1002  * @xas: XArray operation state.
1003  * @entry: Entry from xarray.
1004  *
1005  * The advanced functions may sometimes return an internal entry, such as
1006  * a retry entry or a zero entry.  This function sets up the @xas to restart
1007  * the walk from the head of the array if needed.
1008  *
1009  * Context: Any context.
1010  * Return: true if the operation needs to be retried.
1011  */
1012 static inline bool xas_retry(struct xa_state *xas, const void *entry)
1013 {
1014 	if (xa_is_zero(entry))
1015 		return true;
1016 	if (!xa_is_retry(entry))
1017 		return false;
1018 	xas_reset(xas);
1019 	return true;
1020 }
1021 
1022 void *xas_load(struct xa_state *);
1023 void *xas_store(struct xa_state *, void *entry);
1024 void *xas_find(struct xa_state *, unsigned long max);
1025 void *xas_find_conflict(struct xa_state *);
1026 
1027 bool xas_get_mark(const struct xa_state *, xa_mark_t);
1028 void xas_set_mark(const struct xa_state *, xa_mark_t);
1029 void xas_clear_mark(const struct xa_state *, xa_mark_t);
1030 void *xas_find_marked(struct xa_state *, unsigned long max, xa_mark_t);
1031 void xas_init_marks(const struct xa_state *);
1032 
1033 bool xas_nomem(struct xa_state *, gfp_t);
1034 void xas_pause(struct xa_state *);
1035 
1036 void xas_create_range(struct xa_state *);
1037 
1038 /**
1039  * xas_reload() - Refetch an entry from the xarray.
1040  * @xas: XArray operation state.
1041  *
1042  * Use this function to check that a previously loaded entry still has
1043  * the same value.  This is useful for the lockless pagecache lookup where
1044  * we walk the array with only the RCU lock to protect us, lock the page,
1045  * then check that the page hasn't moved since we looked it up.
1046  *
1047  * The caller guarantees that @xas is still valid.  If it may be in an
1048  * error or restart state, call xas_load() instead.
1049  *
1050  * Return: The entry at this location in the xarray.
1051  */
1052 static inline void *xas_reload(struct xa_state *xas)
1053 {
1054 	struct xa_node *node = xas->xa_node;
1055 
1056 	if (node)
1057 		return xa_entry(xas->xa, node, xas->xa_offset);
1058 	return xa_head(xas->xa);
1059 }
1060 
1061 /**
1062  * xas_set() - Set up XArray operation state for a different index.
1063  * @xas: XArray operation state.
1064  * @index: New index into the XArray.
1065  *
1066  * Move the operation state to refer to a different index.  This will
1067  * have the effect of starting a walk from the top; see xas_next()
1068  * to move to an adjacent index.
1069  */
1070 static inline void xas_set(struct xa_state *xas, unsigned long index)
1071 {
1072 	xas->xa_index = index;
1073 	xas->xa_node = XAS_RESTART;
1074 }
1075 
1076 /**
1077  * xas_set_order() - Set up XArray operation state for a multislot entry.
1078  * @xas: XArray operation state.
1079  * @index: Target of the operation.
1080  * @order: Entry occupies 2^@order indices.
1081  */
1082 static inline void xas_set_order(struct xa_state *xas, unsigned long index,
1083 					unsigned int order)
1084 {
1085 #ifdef CONFIG_XARRAY_MULTI
1086 	xas->xa_index = order < BITS_PER_LONG ? (index >> order) << order : 0;
1087 	xas->xa_shift = order - (order % XA_CHUNK_SHIFT);
1088 	xas->xa_sibs = (1 << (order % XA_CHUNK_SHIFT)) - 1;
1089 	xas->xa_node = XAS_RESTART;
1090 #else
1091 	BUG_ON(order > 0);
1092 	xas_set(xas, index);
1093 #endif
1094 }
1095 
1096 /**
1097  * xas_set_update() - Set up XArray operation state for a callback.
1098  * @xas: XArray operation state.
1099  * @update: Function to call when updating a node.
1100  *
1101  * The XArray can notify a caller after it has updated an xa_node.
1102  * This is advanced functionality and is only needed by the page cache.
1103  */
1104 static inline void xas_set_update(struct xa_state *xas, xa_update_node_t update)
1105 {
1106 	xas->xa_update = update;
1107 }
1108 
1109 /**
1110  * xas_next_entry() - Advance iterator to next present entry.
1111  * @xas: XArray operation state.
1112  * @max: Highest index to return.
1113  *
1114  * xas_next_entry() is an inline function to optimise xarray traversal for
1115  * speed.  It is equivalent to calling xas_find(), and will call xas_find()
1116  * for all the hard cases.
1117  *
1118  * Return: The next present entry after the one currently referred to by @xas.
1119  */
1120 static inline void *xas_next_entry(struct xa_state *xas, unsigned long max)
1121 {
1122 	struct xa_node *node = xas->xa_node;
1123 	void *entry;
1124 
1125 	if (unlikely(xas_not_node(node) || node->shift ||
1126 			xas->xa_offset != (xas->xa_index & XA_CHUNK_MASK)))
1127 		return xas_find(xas, max);
1128 
1129 	do {
1130 		if (unlikely(xas->xa_index >= max))
1131 			return xas_find(xas, max);
1132 		if (unlikely(xas->xa_offset == XA_CHUNK_MASK))
1133 			return xas_find(xas, max);
1134 		entry = xa_entry(xas->xa, node, xas->xa_offset + 1);
1135 		if (unlikely(xa_is_internal(entry)))
1136 			return xas_find(xas, max);
1137 		xas->xa_offset++;
1138 		xas->xa_index++;
1139 	} while (!entry);
1140 
1141 	return entry;
1142 }
1143 
1144 /* Private */
1145 static inline unsigned int xas_find_chunk(struct xa_state *xas, bool advance,
1146 		xa_mark_t mark)
1147 {
1148 	unsigned long *addr = xas->xa_node->marks[(__force unsigned)mark];
1149 	unsigned int offset = xas->xa_offset;
1150 
1151 	if (advance)
1152 		offset++;
1153 	if (XA_CHUNK_SIZE == BITS_PER_LONG) {
1154 		if (offset < XA_CHUNK_SIZE) {
1155 			unsigned long data = *addr & (~0UL << offset);
1156 			if (data)
1157 				return __ffs(data);
1158 		}
1159 		return XA_CHUNK_SIZE;
1160 	}
1161 
1162 	return find_next_bit(addr, XA_CHUNK_SIZE, offset);
1163 }
1164 
1165 /**
1166  * xas_next_marked() - Advance iterator to next marked entry.
1167  * @xas: XArray operation state.
1168  * @max: Highest index to return.
1169  * @mark: Mark to search for.
1170  *
1171  * xas_next_marked() is an inline function to optimise xarray traversal for
1172  * speed.  It is equivalent to calling xas_find_marked(), and will call
1173  * xas_find_marked() for all the hard cases.
1174  *
1175  * Return: The next marked entry after the one currently referred to by @xas.
1176  */
1177 static inline void *xas_next_marked(struct xa_state *xas, unsigned long max,
1178 								xa_mark_t mark)
1179 {
1180 	struct xa_node *node = xas->xa_node;
1181 	unsigned int offset;
1182 
1183 	if (unlikely(xas_not_node(node) || node->shift))
1184 		return xas_find_marked(xas, max, mark);
1185 	offset = xas_find_chunk(xas, true, mark);
1186 	xas->xa_offset = offset;
1187 	xas->xa_index = (xas->xa_index & ~XA_CHUNK_MASK) + offset;
1188 	if (xas->xa_index > max)
1189 		return NULL;
1190 	if (offset == XA_CHUNK_SIZE)
1191 		return xas_find_marked(xas, max, mark);
1192 	return xa_entry(xas->xa, node, offset);
1193 }
1194 
1195 /*
1196  * If iterating while holding a lock, drop the lock and reschedule
1197  * every %XA_CHECK_SCHED loops.
1198  */
1199 enum {
1200 	XA_CHECK_SCHED = 4096,
1201 };
1202 
1203 /**
1204  * xas_for_each() - Iterate over a range of an XArray.
1205  * @xas: XArray operation state.
1206  * @entry: Entry retrieved from the array.
1207  * @max: Maximum index to retrieve from array.
1208  *
1209  * The loop body will be executed for each entry present in the xarray
1210  * between the current xas position and @max.  @entry will be set to
1211  * the entry retrieved from the xarray.  It is safe to delete entries
1212  * from the array in the loop body.  You should hold either the RCU lock
1213  * or the xa_lock while iterating.  If you need to drop the lock, call
1214  * xas_pause() first.
1215  */
1216 #define xas_for_each(xas, entry, max) \
1217 	for (entry = xas_find(xas, max); entry; \
1218 	     entry = xas_next_entry(xas, max))
1219 
1220 /**
1221  * xas_for_each_marked() - Iterate over a range of an XArray.
1222  * @xas: XArray operation state.
1223  * @entry: Entry retrieved from the array.
1224  * @max: Maximum index to retrieve from array.
1225  * @mark: Mark to search for.
1226  *
1227  * The loop body will be executed for each marked entry in the xarray
1228  * between the current xas position and @max.  @entry will be set to
1229  * the entry retrieved from the xarray.  It is safe to delete entries
1230  * from the array in the loop body.  You should hold either the RCU lock
1231  * or the xa_lock while iterating.  If you need to drop the lock, call
1232  * xas_pause() first.
1233  */
1234 #define xas_for_each_marked(xas, entry, max, mark) \
1235 	for (entry = xas_find_marked(xas, max, mark); entry; \
1236 	     entry = xas_next_marked(xas, max, mark))
1237 
1238 /**
1239  * xas_for_each_conflict() - Iterate over a range of an XArray.
1240  * @xas: XArray operation state.
1241  * @entry: Entry retrieved from the array.
1242  *
1243  * The loop body will be executed for each entry in the XArray that lies
1244  * within the range specified by @xas.  If the loop completes successfully,
1245  * any entries that lie in this range will be replaced by @entry.  The caller
1246  * may break out of the loop; if they do so, the contents of the XArray will
1247  * be unchanged.  The operation may fail due to an out of memory condition.
1248  * The caller may also call xa_set_err() to exit the loop while setting an
1249  * error to record the reason.
1250  */
1251 #define xas_for_each_conflict(xas, entry) \
1252 	while ((entry = xas_find_conflict(xas)))
1253 
1254 void *__xas_next(struct xa_state *);
1255 void *__xas_prev(struct xa_state *);
1256 
1257 /**
1258  * xas_prev() - Move iterator to previous index.
1259  * @xas: XArray operation state.
1260  *
1261  * If the @xas was in an error state, it will remain in an error state
1262  * and this function will return %NULL.  If the @xas has never been walked,
1263  * it will have the effect of calling xas_load().  Otherwise one will be
1264  * subtracted from the index and the state will be walked to the correct
1265  * location in the array for the next operation.
1266  *
1267  * If the iterator was referencing index 0, this function wraps
1268  * around to %ULONG_MAX.
1269  *
1270  * Return: The entry at the new index.  This may be %NULL or an internal
1271  * entry.
1272  */
1273 static inline void *xas_prev(struct xa_state *xas)
1274 {
1275 	struct xa_node *node = xas->xa_node;
1276 
1277 	if (unlikely(xas_not_node(node) || node->shift ||
1278 				xas->xa_offset == 0))
1279 		return __xas_prev(xas);
1280 
1281 	xas->xa_index--;
1282 	xas->xa_offset--;
1283 	return xa_entry(xas->xa, node, xas->xa_offset);
1284 }
1285 
1286 /**
1287  * xas_next() - Move state to next index.
1288  * @xas: XArray operation state.
1289  *
1290  * If the @xas was in an error state, it will remain in an error state
1291  * and this function will return %NULL.  If the @xas has never been walked,
1292  * it will have the effect of calling xas_load().  Otherwise one will be
1293  * added to the index and the state will be walked to the correct
1294  * location in the array for the next operation.
1295  *
1296  * If the iterator was referencing index %ULONG_MAX, this function wraps
1297  * around to 0.
1298  *
1299  * Return: The entry at the new index.  This may be %NULL or an internal
1300  * entry.
1301  */
1302 static inline void *xas_next(struct xa_state *xas)
1303 {
1304 	struct xa_node *node = xas->xa_node;
1305 
1306 	if (unlikely(xas_not_node(node) || node->shift ||
1307 				xas->xa_offset == XA_CHUNK_MASK))
1308 		return __xas_next(xas);
1309 
1310 	xas->xa_index++;
1311 	xas->xa_offset++;
1312 	return xa_entry(xas->xa, node, xas->xa_offset);
1313 }
1314 
1315 #endif /* _LINUX_XARRAY_H */
1316