xref: /openbmc/linux/include/linux/xarray.h (revision 6b342707)
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: Retry entry
36  * 257: Zero 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  * Internal entries are used for a number of purposes.  Entries 0-255 are
135  * used for sibling entries (only 0-62 are used by the current code).  256
136  * is used for the retry entry.  257 is used for the reserved / zero entry.
137  * Negative internal entries are used to represent errnos.  Node pointers
138  * are also tagged as internal entries in some situations.
139  *
140  * Context: Any context.
141  * Return: An XArray internal entry corresponding to this value.
142  */
143 static inline void *xa_mk_internal(unsigned long v)
144 {
145 	return (void *)((v << 2) | 2);
146 }
147 
148 /*
149  * xa_to_internal() - Extract the value from an internal entry.
150  * @entry: XArray entry.
151  *
152  * Context: Any context.
153  * Return: The value which was stored in the internal entry.
154  */
155 static inline unsigned long xa_to_internal(const void *entry)
156 {
157 	return (unsigned long)entry >> 2;
158 }
159 
160 /*
161  * xa_is_internal() - Is the entry an internal entry?
162  * @entry: XArray entry.
163  *
164  * Context: Any context.
165  * Return: %true if the entry is an internal entry.
166  */
167 static inline bool xa_is_internal(const void *entry)
168 {
169 	return ((unsigned long)entry & 3) == 2;
170 }
171 
172 #define XA_ZERO_ENTRY		xa_mk_internal(257)
173 
174 /**
175  * xa_is_zero() - Is the entry a zero entry?
176  * @entry: Entry retrieved from the XArray
177  *
178  * The normal API will return NULL as the contents of a slot containing
179  * a zero entry.  You can only see zero entries by using the advanced API.
180  *
181  * Return: %true if the entry is a zero entry.
182  */
183 static inline bool xa_is_zero(const void *entry)
184 {
185 	return unlikely(entry == XA_ZERO_ENTRY);
186 }
187 
188 /**
189  * xa_is_err() - Report whether an XArray operation returned an error
190  * @entry: Result from calling an XArray function
191  *
192  * If an XArray operation cannot complete an operation, it will return
193  * a special value indicating an error.  This function tells you
194  * whether an error occurred; xa_err() tells you which error occurred.
195  *
196  * Context: Any context.
197  * Return: %true if the entry indicates an error.
198  */
199 static inline bool xa_is_err(const void *entry)
200 {
201 	return unlikely(xa_is_internal(entry) &&
202 			entry >= xa_mk_internal(-MAX_ERRNO));
203 }
204 
205 /**
206  * xa_err() - Turn an XArray result into an errno.
207  * @entry: Result from calling an XArray function.
208  *
209  * If an XArray operation cannot complete an operation, it will return
210  * a special pointer value which encodes an errno.  This function extracts
211  * the errno from the pointer value, or returns 0 if the pointer does not
212  * represent an errno.
213  *
214  * Context: Any context.
215  * Return: A negative errno or 0.
216  */
217 static inline int xa_err(void *entry)
218 {
219 	/* xa_to_internal() would not do sign extension. */
220 	if (xa_is_err(entry))
221 		return (long)entry >> 2;
222 	return 0;
223 }
224 
225 /**
226  * struct xa_limit - Represents a range of IDs.
227  * @min: The lowest ID to allocate (inclusive).
228  * @max: The maximum ID to allocate (inclusive).
229  *
230  * This structure is used either directly or via the XA_LIMIT() macro
231  * to communicate the range of IDs that are valid for allocation.
232  * Three common ranges are predefined for you:
233  * * xa_limit_32b	- [0 - UINT_MAX]
234  * * xa_limit_31b	- [0 - INT_MAX]
235  * * xa_limit_16b	- [0 - USHRT_MAX]
236  */
237 struct xa_limit {
238 	u32 max;
239 	u32 min;
240 };
241 
242 #define XA_LIMIT(_min, _max) (struct xa_limit) { .min = _min, .max = _max }
243 
244 #define xa_limit_32b	XA_LIMIT(0, UINT_MAX)
245 #define xa_limit_31b	XA_LIMIT(0, INT_MAX)
246 #define xa_limit_16b	XA_LIMIT(0, USHRT_MAX)
247 
248 typedef unsigned __bitwise xa_mark_t;
249 #define XA_MARK_0		((__force xa_mark_t)0U)
250 #define XA_MARK_1		((__force xa_mark_t)1U)
251 #define XA_MARK_2		((__force xa_mark_t)2U)
252 #define XA_PRESENT		((__force xa_mark_t)8U)
253 #define XA_MARK_MAX		XA_MARK_2
254 #define XA_FREE_MARK		XA_MARK_0
255 
256 enum xa_lock_type {
257 	XA_LOCK_IRQ = 1,
258 	XA_LOCK_BH = 2,
259 };
260 
261 /*
262  * Values for xa_flags.  The radix tree stores its GFP flags in the xa_flags,
263  * and we remain compatible with that.
264  */
265 #define XA_FLAGS_LOCK_IRQ	((__force gfp_t)XA_LOCK_IRQ)
266 #define XA_FLAGS_LOCK_BH	((__force gfp_t)XA_LOCK_BH)
267 #define XA_FLAGS_TRACK_FREE	((__force gfp_t)4U)
268 #define XA_FLAGS_ZERO_BUSY	((__force gfp_t)8U)
269 #define XA_FLAGS_ALLOC_WRAPPED	((__force gfp_t)16U)
270 #define XA_FLAGS_ACCOUNT	((__force gfp_t)32U)
271 #define XA_FLAGS_MARK(mark)	((__force gfp_t)((1U << __GFP_BITS_SHIFT) << \
272 						(__force unsigned)(mark)))
273 
274 /* ALLOC is for a normal 0-based alloc.  ALLOC1 is for an 1-based alloc */
275 #define XA_FLAGS_ALLOC	(XA_FLAGS_TRACK_FREE | XA_FLAGS_MARK(XA_FREE_MARK))
276 #define XA_FLAGS_ALLOC1	(XA_FLAGS_TRACK_FREE | XA_FLAGS_ZERO_BUSY)
277 
278 /**
279  * struct xarray - The anchor of the XArray.
280  * @xa_lock: Lock that protects the contents of the XArray.
281  *
282  * To use the xarray, define it statically or embed it in your data structure.
283  * It is a very small data structure, so it does not usually make sense to
284  * allocate it separately and keep a pointer to it in your data structure.
285  *
286  * You may use the xa_lock to protect your own data structures as well.
287  */
288 /*
289  * If all of the entries in the array are NULL, @xa_head is a NULL pointer.
290  * If the only non-NULL entry in the array is at index 0, @xa_head is that
291  * entry.  If any other entry in the array is non-NULL, @xa_head points
292  * to an @xa_node.
293  */
294 struct xarray {
295 	spinlock_t	xa_lock;
296 /* private: The rest of the data structure is not to be used directly. */
297 	gfp_t		xa_flags;
298 	void __rcu *	xa_head;
299 };
300 
301 #define XARRAY_INIT(name, flags) {				\
302 	.xa_lock = __SPIN_LOCK_UNLOCKED(name.xa_lock),		\
303 	.xa_flags = flags,					\
304 	.xa_head = NULL,					\
305 }
306 
307 /**
308  * DEFINE_XARRAY_FLAGS() - Define an XArray with custom flags.
309  * @name: A string that names your XArray.
310  * @flags: XA_FLAG values.
311  *
312  * This is intended for file scope definitions of XArrays.  It declares
313  * and initialises an empty XArray with the chosen name and flags.  It is
314  * equivalent to calling xa_init_flags() on the array, but it does the
315  * initialisation at compiletime instead of runtime.
316  */
317 #define DEFINE_XARRAY_FLAGS(name, flags)				\
318 	struct xarray name = XARRAY_INIT(name, flags)
319 
320 /**
321  * DEFINE_XARRAY() - Define an XArray.
322  * @name: A string that names your XArray.
323  *
324  * This is intended for file scope definitions of XArrays.  It declares
325  * and initialises an empty XArray with the chosen name.  It is equivalent
326  * to calling xa_init() on the array, but it does the initialisation at
327  * compiletime instead of runtime.
328  */
329 #define DEFINE_XARRAY(name) DEFINE_XARRAY_FLAGS(name, 0)
330 
331 /**
332  * DEFINE_XARRAY_ALLOC() - Define an XArray which allocates IDs starting at 0.
333  * @name: A string that names your XArray.
334  *
335  * This is intended for file scope definitions of allocating XArrays.
336  * See also DEFINE_XARRAY().
337  */
338 #define DEFINE_XARRAY_ALLOC(name) DEFINE_XARRAY_FLAGS(name, XA_FLAGS_ALLOC)
339 
340 /**
341  * DEFINE_XARRAY_ALLOC1() - Define an XArray which allocates IDs starting at 1.
342  * @name: A string that names your XArray.
343  *
344  * This is intended for file scope definitions of allocating XArrays.
345  * See also DEFINE_XARRAY().
346  */
347 #define DEFINE_XARRAY_ALLOC1(name) DEFINE_XARRAY_FLAGS(name, XA_FLAGS_ALLOC1)
348 
349 void *xa_load(struct xarray *, unsigned long index);
350 void *xa_store(struct xarray *, unsigned long index, void *entry, gfp_t);
351 void *xa_erase(struct xarray *, unsigned long index);
352 void *xa_store_range(struct xarray *, unsigned long first, unsigned long last,
353 			void *entry, gfp_t);
354 bool xa_get_mark(struct xarray *, unsigned long index, xa_mark_t);
355 void xa_set_mark(struct xarray *, unsigned long index, xa_mark_t);
356 void xa_clear_mark(struct xarray *, unsigned long index, xa_mark_t);
357 void *xa_find(struct xarray *xa, unsigned long *index,
358 		unsigned long max, xa_mark_t) __attribute__((nonnull(2)));
359 void *xa_find_after(struct xarray *xa, unsigned long *index,
360 		unsigned long max, xa_mark_t) __attribute__((nonnull(2)));
361 unsigned int xa_extract(struct xarray *, void **dst, unsigned long start,
362 		unsigned long max, unsigned int n, xa_mark_t);
363 void xa_destroy(struct xarray *);
364 
365 /**
366  * xa_init_flags() - Initialise an empty XArray with flags.
367  * @xa: XArray.
368  * @flags: XA_FLAG values.
369  *
370  * If you need to initialise an XArray with special flags (eg you need
371  * to take the lock from interrupt context), use this function instead
372  * of xa_init().
373  *
374  * Context: Any context.
375  */
376 static inline void xa_init_flags(struct xarray *xa, gfp_t flags)
377 {
378 	spin_lock_init(&xa->xa_lock);
379 	xa->xa_flags = flags;
380 	xa->xa_head = NULL;
381 }
382 
383 /**
384  * xa_init() - Initialise an empty XArray.
385  * @xa: XArray.
386  *
387  * An empty XArray is full of NULL entries.
388  *
389  * Context: Any context.
390  */
391 static inline void xa_init(struct xarray *xa)
392 {
393 	xa_init_flags(xa, 0);
394 }
395 
396 /**
397  * xa_empty() - Determine if an array has any present entries.
398  * @xa: XArray.
399  *
400  * Context: Any context.
401  * Return: %true if the array contains only NULL pointers.
402  */
403 static inline bool xa_empty(const struct xarray *xa)
404 {
405 	return xa->xa_head == NULL;
406 }
407 
408 /**
409  * xa_marked() - Inquire whether any entry in this array has a mark set
410  * @xa: Array
411  * @mark: Mark value
412  *
413  * Context: Any context.
414  * Return: %true if any entry has this mark set.
415  */
416 static inline bool xa_marked(const struct xarray *xa, xa_mark_t mark)
417 {
418 	return xa->xa_flags & XA_FLAGS_MARK(mark);
419 }
420 
421 /**
422  * xa_for_each_range() - Iterate over a portion of an XArray.
423  * @xa: XArray.
424  * @index: Index of @entry.
425  * @entry: Entry retrieved from array.
426  * @start: First index to retrieve from array.
427  * @last: Last index to retrieve from array.
428  *
429  * During the iteration, @entry will have the value of the entry stored
430  * in @xa at @index.  You may modify @index during the iteration if you
431  * want to skip or reprocess indices.  It is safe to modify the array
432  * during the iteration.  At the end of the iteration, @entry will be set
433  * to NULL and @index will have a value less than or equal to max.
434  *
435  * xa_for_each_range() is O(n.log(n)) while xas_for_each() is O(n).  You have
436  * to handle your own locking with xas_for_each(), and if you have to unlock
437  * after each iteration, it will also end up being O(n.log(n)).
438  * xa_for_each_range() will spin if it hits a retry entry; if you intend to
439  * see retry entries, you should use the xas_for_each() iterator instead.
440  * The xas_for_each() iterator will expand into more inline code than
441  * xa_for_each_range().
442  *
443  * Context: Any context.  Takes and releases the RCU lock.
444  */
445 #define xa_for_each_range(xa, index, entry, start, last)		\
446 	for (index = start,						\
447 	     entry = xa_find(xa, &index, last, XA_PRESENT);		\
448 	     entry;							\
449 	     entry = xa_find_after(xa, &index, last, XA_PRESENT))
450 
451 /**
452  * xa_for_each_start() - Iterate over a portion of an XArray.
453  * @xa: XArray.
454  * @index: Index of @entry.
455  * @entry: Entry retrieved from array.
456  * @start: First index to retrieve from array.
457  *
458  * During the iteration, @entry will have the value of the entry stored
459  * in @xa at @index.  You may modify @index during the iteration if you
460  * want to skip or reprocess indices.  It is safe to modify the array
461  * during the iteration.  At the end of the iteration, @entry will be set
462  * to NULL and @index will have a value less than or equal to max.
463  *
464  * xa_for_each_start() is O(n.log(n)) while xas_for_each() is O(n).  You have
465  * to handle your own locking with xas_for_each(), and if you have to unlock
466  * after each iteration, it will also end up being O(n.log(n)).
467  * xa_for_each_start() will spin if it hits a retry entry; if you intend to
468  * see retry entries, you should use the xas_for_each() iterator instead.
469  * The xas_for_each() iterator will expand into more inline code than
470  * xa_for_each_start().
471  *
472  * Context: Any context.  Takes and releases the RCU lock.
473  */
474 #define xa_for_each_start(xa, index, entry, start) \
475 	xa_for_each_range(xa, index, entry, start, ULONG_MAX)
476 
477 /**
478  * xa_for_each() - Iterate over present entries in an XArray.
479  * @xa: XArray.
480  * @index: Index of @entry.
481  * @entry: Entry retrieved from array.
482  *
483  * During the iteration, @entry will have the value of the entry stored
484  * in @xa at @index.  You may modify @index during the iteration if you want
485  * to skip or reprocess indices.  It is safe to modify the array during the
486  * iteration.  At the end of the iteration, @entry will be set to NULL and
487  * @index will have a value less than or equal to max.
488  *
489  * xa_for_each() is O(n.log(n)) while xas_for_each() is O(n).  You have
490  * to handle your own locking with xas_for_each(), and if you have to unlock
491  * after each iteration, it will also end up being O(n.log(n)).  xa_for_each()
492  * will spin if it hits a retry entry; if you intend to see retry entries,
493  * you should use the xas_for_each() iterator instead.  The xas_for_each()
494  * iterator will expand into more inline code than xa_for_each().
495  *
496  * Context: Any context.  Takes and releases the RCU lock.
497  */
498 #define xa_for_each(xa, index, entry) \
499 	xa_for_each_start(xa, index, entry, 0)
500 
501 /**
502  * xa_for_each_marked() - Iterate over marked entries in an XArray.
503  * @xa: XArray.
504  * @index: Index of @entry.
505  * @entry: Entry retrieved from array.
506  * @filter: Selection criterion.
507  *
508  * During the iteration, @entry will have the value of the entry stored
509  * in @xa at @index.  The iteration will skip all entries in the array
510  * which do not match @filter.  You may modify @index during the iteration
511  * if you want to skip or reprocess indices.  It is safe to modify the array
512  * during the iteration.  At the end of the iteration, @entry will be set to
513  * NULL and @index will have a value less than or equal to max.
514  *
515  * xa_for_each_marked() is O(n.log(n)) while xas_for_each_marked() is O(n).
516  * You have to handle your own locking with xas_for_each(), and if you have
517  * to unlock after each iteration, it will also end up being O(n.log(n)).
518  * xa_for_each_marked() will spin if it hits a retry entry; if you intend to
519  * see retry entries, you should use the xas_for_each_marked() iterator
520  * instead.  The xas_for_each_marked() iterator will expand into more inline
521  * code than xa_for_each_marked().
522  *
523  * Context: Any context.  Takes and releases the RCU lock.
524  */
525 #define xa_for_each_marked(xa, index, entry, filter) \
526 	for (index = 0, entry = xa_find(xa, &index, ULONG_MAX, filter); \
527 	     entry; entry = xa_find_after(xa, &index, ULONG_MAX, filter))
528 
529 #define xa_trylock(xa)		spin_trylock(&(xa)->xa_lock)
530 #define xa_lock(xa)		spin_lock(&(xa)->xa_lock)
531 #define xa_unlock(xa)		spin_unlock(&(xa)->xa_lock)
532 #define xa_lock_bh(xa)		spin_lock_bh(&(xa)->xa_lock)
533 #define xa_unlock_bh(xa)	spin_unlock_bh(&(xa)->xa_lock)
534 #define xa_lock_irq(xa)		spin_lock_irq(&(xa)->xa_lock)
535 #define xa_unlock_irq(xa)	spin_unlock_irq(&(xa)->xa_lock)
536 #define xa_lock_irqsave(xa, flags) \
537 				spin_lock_irqsave(&(xa)->xa_lock, flags)
538 #define xa_unlock_irqrestore(xa, flags) \
539 				spin_unlock_irqrestore(&(xa)->xa_lock, flags)
540 #define xa_lock_nested(xa, subclass) \
541 				spin_lock_nested(&(xa)->xa_lock, subclass)
542 #define xa_lock_bh_nested(xa, subclass) \
543 				spin_lock_bh_nested(&(xa)->xa_lock, subclass)
544 #define xa_lock_irq_nested(xa, subclass) \
545 				spin_lock_irq_nested(&(xa)->xa_lock, subclass)
546 #define xa_lock_irqsave_nested(xa, flags, subclass) \
547 		spin_lock_irqsave_nested(&(xa)->xa_lock, flags, subclass)
548 
549 /*
550  * Versions of the normal API which require the caller to hold the
551  * xa_lock.  If the GFP flags allow it, they will drop the lock to
552  * allocate memory, then reacquire it afterwards.  These functions
553  * may also re-enable interrupts if the XArray flags indicate the
554  * locking should be interrupt safe.
555  */
556 void *__xa_erase(struct xarray *, unsigned long index);
557 void *__xa_store(struct xarray *, unsigned long index, void *entry, gfp_t);
558 void *__xa_cmpxchg(struct xarray *, unsigned long index, void *old,
559 		void *entry, gfp_t);
560 int __must_check __xa_insert(struct xarray *, unsigned long index,
561 		void *entry, gfp_t);
562 int __must_check __xa_alloc(struct xarray *, u32 *id, void *entry,
563 		struct xa_limit, gfp_t);
564 int __must_check __xa_alloc_cyclic(struct xarray *, u32 *id, void *entry,
565 		struct xa_limit, u32 *next, gfp_t);
566 void __xa_set_mark(struct xarray *, unsigned long index, xa_mark_t);
567 void __xa_clear_mark(struct xarray *, unsigned long index, xa_mark_t);
568 
569 /**
570  * xa_store_bh() - Store this entry in the XArray.
571  * @xa: XArray.
572  * @index: Index into array.
573  * @entry: New entry.
574  * @gfp: Memory allocation flags.
575  *
576  * This function is like calling xa_store() except it disables softirqs
577  * while holding the array lock.
578  *
579  * Context: Any context.  Takes and releases the xa_lock while
580  * disabling softirqs.
581  * Return: The old entry at this index or xa_err() if an error happened.
582  */
583 static inline void *xa_store_bh(struct xarray *xa, unsigned long index,
584 		void *entry, gfp_t gfp)
585 {
586 	void *curr;
587 
588 	xa_lock_bh(xa);
589 	curr = __xa_store(xa, index, entry, gfp);
590 	xa_unlock_bh(xa);
591 
592 	return curr;
593 }
594 
595 /**
596  * xa_store_irq() - Store this entry in the XArray.
597  * @xa: XArray.
598  * @index: Index into array.
599  * @entry: New entry.
600  * @gfp: Memory allocation flags.
601  *
602  * This function is like calling xa_store() except it disables interrupts
603  * while holding the array lock.
604  *
605  * Context: Process context.  Takes and releases the xa_lock while
606  * disabling interrupts.
607  * Return: The old entry at this index or xa_err() if an error happened.
608  */
609 static inline void *xa_store_irq(struct xarray *xa, unsigned long index,
610 		void *entry, gfp_t gfp)
611 {
612 	void *curr;
613 
614 	xa_lock_irq(xa);
615 	curr = __xa_store(xa, index, entry, gfp);
616 	xa_unlock_irq(xa);
617 
618 	return curr;
619 }
620 
621 /**
622  * xa_erase_bh() - Erase this entry from the XArray.
623  * @xa: XArray.
624  * @index: Index of entry.
625  *
626  * After this function returns, loading from @index will return %NULL.
627  * If the index is part of a multi-index entry, all indices will be erased
628  * and none of the entries will be part of a multi-index entry.
629  *
630  * Context: Any context.  Takes and releases the xa_lock while
631  * disabling softirqs.
632  * Return: The entry which used to be at this index.
633  */
634 static inline void *xa_erase_bh(struct xarray *xa, unsigned long index)
635 {
636 	void *entry;
637 
638 	xa_lock_bh(xa);
639 	entry = __xa_erase(xa, index);
640 	xa_unlock_bh(xa);
641 
642 	return entry;
643 }
644 
645 /**
646  * xa_erase_irq() - Erase this entry from the XArray.
647  * @xa: XArray.
648  * @index: Index of entry.
649  *
650  * After this function returns, loading from @index will return %NULL.
651  * If the index is part of a multi-index entry, all indices will be erased
652  * and none of the entries will be part of a multi-index entry.
653  *
654  * Context: Process context.  Takes and releases the xa_lock while
655  * disabling interrupts.
656  * Return: The entry which used to be at this index.
657  */
658 static inline void *xa_erase_irq(struct xarray *xa, unsigned long index)
659 {
660 	void *entry;
661 
662 	xa_lock_irq(xa);
663 	entry = __xa_erase(xa, index);
664 	xa_unlock_irq(xa);
665 
666 	return entry;
667 }
668 
669 /**
670  * xa_cmpxchg() - Conditionally replace an entry in the XArray.
671  * @xa: XArray.
672  * @index: Index into array.
673  * @old: Old value to test against.
674  * @entry: New value to place in array.
675  * @gfp: Memory allocation flags.
676  *
677  * If the entry at @index is the same as @old, replace it with @entry.
678  * If the return value is equal to @old, then the exchange was successful.
679  *
680  * Context: Any context.  Takes and releases the xa_lock.  May sleep
681  * if the @gfp flags permit.
682  * Return: The old value at this index or xa_err() if an error happened.
683  */
684 static inline void *xa_cmpxchg(struct xarray *xa, unsigned long index,
685 			void *old, void *entry, gfp_t gfp)
686 {
687 	void *curr;
688 
689 	xa_lock(xa);
690 	curr = __xa_cmpxchg(xa, index, old, entry, gfp);
691 	xa_unlock(xa);
692 
693 	return curr;
694 }
695 
696 /**
697  * xa_cmpxchg_bh() - Conditionally replace an entry in the XArray.
698  * @xa: XArray.
699  * @index: Index into array.
700  * @old: Old value to test against.
701  * @entry: New value to place in array.
702  * @gfp: Memory allocation flags.
703  *
704  * This function is like calling xa_cmpxchg() except it disables softirqs
705  * while holding the array lock.
706  *
707  * Context: Any context.  Takes and releases the xa_lock while
708  * disabling softirqs.  May sleep if the @gfp flags permit.
709  * Return: The old value at this index or xa_err() if an error happened.
710  */
711 static inline void *xa_cmpxchg_bh(struct xarray *xa, unsigned long index,
712 			void *old, void *entry, gfp_t gfp)
713 {
714 	void *curr;
715 
716 	xa_lock_bh(xa);
717 	curr = __xa_cmpxchg(xa, index, old, entry, gfp);
718 	xa_unlock_bh(xa);
719 
720 	return curr;
721 }
722 
723 /**
724  * xa_cmpxchg_irq() - Conditionally replace an entry in the XArray.
725  * @xa: XArray.
726  * @index: Index into array.
727  * @old: Old value to test against.
728  * @entry: New value to place in array.
729  * @gfp: Memory allocation flags.
730  *
731  * This function is like calling xa_cmpxchg() except it disables interrupts
732  * while holding the array lock.
733  *
734  * Context: Process context.  Takes and releases the xa_lock while
735  * disabling interrupts.  May sleep if the @gfp flags permit.
736  * Return: The old value at this index or xa_err() if an error happened.
737  */
738 static inline void *xa_cmpxchg_irq(struct xarray *xa, unsigned long index,
739 			void *old, void *entry, gfp_t gfp)
740 {
741 	void *curr;
742 
743 	xa_lock_irq(xa);
744 	curr = __xa_cmpxchg(xa, index, old, entry, gfp);
745 	xa_unlock_irq(xa);
746 
747 	return curr;
748 }
749 
750 /**
751  * xa_insert() - Store this entry in the XArray unless another entry is
752  *			already present.
753  * @xa: XArray.
754  * @index: Index into array.
755  * @entry: New entry.
756  * @gfp: Memory allocation flags.
757  *
758  * Inserting a NULL entry will store a reserved entry (like xa_reserve())
759  * if no entry is present.  Inserting will fail if a reserved entry is
760  * present, even though loading from this index will return NULL.
761  *
762  * Context: Any context.  Takes and releases the xa_lock.  May sleep if
763  * the @gfp flags permit.
764  * Return: 0 if the store succeeded.  -EBUSY if another entry was present.
765  * -ENOMEM if memory could not be allocated.
766  */
767 static inline int __must_check xa_insert(struct xarray *xa,
768 		unsigned long index, void *entry, gfp_t gfp)
769 {
770 	int err;
771 
772 	xa_lock(xa);
773 	err = __xa_insert(xa, index, entry, gfp);
774 	xa_unlock(xa);
775 
776 	return err;
777 }
778 
779 /**
780  * xa_insert_bh() - Store this entry in the XArray unless another entry is
781  *			already present.
782  * @xa: XArray.
783  * @index: Index into array.
784  * @entry: New entry.
785  * @gfp: Memory allocation flags.
786  *
787  * Inserting a NULL entry will store a reserved entry (like xa_reserve())
788  * if no entry is present.  Inserting will fail if a reserved entry is
789  * present, even though loading from this index will return NULL.
790  *
791  * Context: Any context.  Takes and releases the xa_lock while
792  * disabling softirqs.  May sleep if the @gfp flags permit.
793  * Return: 0 if the store succeeded.  -EBUSY if another entry was present.
794  * -ENOMEM if memory could not be allocated.
795  */
796 static inline int __must_check xa_insert_bh(struct xarray *xa,
797 		unsigned long index, void *entry, gfp_t gfp)
798 {
799 	int err;
800 
801 	xa_lock_bh(xa);
802 	err = __xa_insert(xa, index, entry, gfp);
803 	xa_unlock_bh(xa);
804 
805 	return err;
806 }
807 
808 /**
809  * xa_insert_irq() - Store this entry in the XArray unless another entry is
810  *			already present.
811  * @xa: XArray.
812  * @index: Index into array.
813  * @entry: New entry.
814  * @gfp: Memory allocation flags.
815  *
816  * Inserting a NULL entry will store a reserved entry (like xa_reserve())
817  * if no entry is present.  Inserting will fail if a reserved entry is
818  * present, even though loading from this index will return NULL.
819  *
820  * Context: Process context.  Takes and releases the xa_lock while
821  * disabling interrupts.  May sleep if the @gfp flags permit.
822  * Return: 0 if the store succeeded.  -EBUSY if another entry was present.
823  * -ENOMEM if memory could not be allocated.
824  */
825 static inline int __must_check xa_insert_irq(struct xarray *xa,
826 		unsigned long index, void *entry, gfp_t gfp)
827 {
828 	int err;
829 
830 	xa_lock_irq(xa);
831 	err = __xa_insert(xa, index, entry, gfp);
832 	xa_unlock_irq(xa);
833 
834 	return err;
835 }
836 
837 /**
838  * xa_alloc() - Find somewhere to store this entry in the XArray.
839  * @xa: XArray.
840  * @id: Pointer to ID.
841  * @entry: New entry.
842  * @limit: Range of ID to allocate.
843  * @gfp: Memory allocation flags.
844  *
845  * Finds an empty entry in @xa between @limit.min and @limit.max,
846  * stores the index into the @id pointer, then stores the entry at
847  * that index.  A concurrent lookup will not see an uninitialised @id.
848  *
849  * Context: Any context.  Takes and releases the xa_lock.  May sleep if
850  * the @gfp flags permit.
851  * Return: 0 on success, -ENOMEM if memory could not be allocated or
852  * -EBUSY if there are no free entries in @limit.
853  */
854 static inline __must_check int xa_alloc(struct xarray *xa, u32 *id,
855 		void *entry, struct xa_limit limit, gfp_t gfp)
856 {
857 	int err;
858 
859 	xa_lock(xa);
860 	err = __xa_alloc(xa, id, entry, limit, gfp);
861 	xa_unlock(xa);
862 
863 	return err;
864 }
865 
866 /**
867  * xa_alloc_bh() - Find somewhere to store this entry in the XArray.
868  * @xa: XArray.
869  * @id: Pointer to ID.
870  * @entry: New entry.
871  * @limit: Range of ID to allocate.
872  * @gfp: Memory allocation flags.
873  *
874  * Finds an empty entry in @xa between @limit.min and @limit.max,
875  * stores the index into the @id pointer, then stores the entry at
876  * that index.  A concurrent lookup will not see an uninitialised @id.
877  *
878  * Context: Any context.  Takes and releases the xa_lock while
879  * disabling softirqs.  May sleep if the @gfp flags permit.
880  * Return: 0 on success, -ENOMEM if memory could not be allocated or
881  * -EBUSY if there are no free entries in @limit.
882  */
883 static inline int __must_check xa_alloc_bh(struct xarray *xa, u32 *id,
884 		void *entry, struct xa_limit limit, gfp_t gfp)
885 {
886 	int err;
887 
888 	xa_lock_bh(xa);
889 	err = __xa_alloc(xa, id, entry, limit, gfp);
890 	xa_unlock_bh(xa);
891 
892 	return err;
893 }
894 
895 /**
896  * xa_alloc_irq() - Find somewhere to store this entry in the XArray.
897  * @xa: XArray.
898  * @id: Pointer to ID.
899  * @entry: New entry.
900  * @limit: Range of ID to allocate.
901  * @gfp: Memory allocation flags.
902  *
903  * Finds an empty entry in @xa between @limit.min and @limit.max,
904  * stores the index into the @id pointer, then stores the entry at
905  * that index.  A concurrent lookup will not see an uninitialised @id.
906  *
907  * Context: Process context.  Takes and releases the xa_lock while
908  * disabling interrupts.  May sleep if the @gfp flags permit.
909  * Return: 0 on success, -ENOMEM if memory could not be allocated or
910  * -EBUSY if there are no free entries in @limit.
911  */
912 static inline int __must_check xa_alloc_irq(struct xarray *xa, u32 *id,
913 		void *entry, struct xa_limit limit, gfp_t gfp)
914 {
915 	int err;
916 
917 	xa_lock_irq(xa);
918 	err = __xa_alloc(xa, id, entry, limit, gfp);
919 	xa_unlock_irq(xa);
920 
921 	return err;
922 }
923 
924 /**
925  * xa_alloc_cyclic() - Find somewhere to store this entry in the XArray.
926  * @xa: XArray.
927  * @id: Pointer to ID.
928  * @entry: New entry.
929  * @limit: Range of allocated ID.
930  * @next: Pointer to next ID to allocate.
931  * @gfp: Memory allocation flags.
932  *
933  * Finds an empty entry in @xa between @limit.min and @limit.max,
934  * stores the index into the @id pointer, then stores the entry at
935  * that index.  A concurrent lookup will not see an uninitialised @id.
936  * The search for an empty entry will start at @next and will wrap
937  * around if necessary.
938  *
939  * Context: Any context.  Takes and releases the xa_lock.  May sleep if
940  * the @gfp flags permit.
941  * Return: 0 if the allocation succeeded without wrapping.  1 if the
942  * allocation succeeded after wrapping, -ENOMEM if memory could not be
943  * allocated or -EBUSY if there are no free entries in @limit.
944  */
945 static inline int xa_alloc_cyclic(struct xarray *xa, u32 *id, void *entry,
946 		struct xa_limit limit, u32 *next, gfp_t gfp)
947 {
948 	int err;
949 
950 	xa_lock(xa);
951 	err = __xa_alloc_cyclic(xa, id, entry, limit, next, gfp);
952 	xa_unlock(xa);
953 
954 	return err;
955 }
956 
957 /**
958  * xa_alloc_cyclic_bh() - Find somewhere to store this entry in the XArray.
959  * @xa: XArray.
960  * @id: Pointer to ID.
961  * @entry: New entry.
962  * @limit: Range of allocated ID.
963  * @next: Pointer to next ID to allocate.
964  * @gfp: Memory allocation flags.
965  *
966  * Finds an empty entry in @xa between @limit.min and @limit.max,
967  * stores the index into the @id pointer, then stores the entry at
968  * that index.  A concurrent lookup will not see an uninitialised @id.
969  * The search for an empty entry will start at @next and will wrap
970  * around if necessary.
971  *
972  * Context: Any context.  Takes and releases the xa_lock while
973  * disabling softirqs.  May sleep if the @gfp flags permit.
974  * Return: 0 if the allocation succeeded without wrapping.  1 if the
975  * allocation succeeded after wrapping, -ENOMEM if memory could not be
976  * allocated or -EBUSY if there are no free entries in @limit.
977  */
978 static inline int xa_alloc_cyclic_bh(struct xarray *xa, u32 *id, void *entry,
979 		struct xa_limit limit, u32 *next, gfp_t gfp)
980 {
981 	int err;
982 
983 	xa_lock_bh(xa);
984 	err = __xa_alloc_cyclic(xa, id, entry, limit, next, gfp);
985 	xa_unlock_bh(xa);
986 
987 	return err;
988 }
989 
990 /**
991  * xa_alloc_cyclic_irq() - Find somewhere to store this entry in the XArray.
992  * @xa: XArray.
993  * @id: Pointer to ID.
994  * @entry: New entry.
995  * @limit: Range of allocated ID.
996  * @next: Pointer to next ID to allocate.
997  * @gfp: Memory allocation flags.
998  *
999  * Finds an empty entry in @xa between @limit.min and @limit.max,
1000  * stores the index into the @id pointer, then stores the entry at
1001  * that index.  A concurrent lookup will not see an uninitialised @id.
1002  * The search for an empty entry will start at @next and will wrap
1003  * around if necessary.
1004  *
1005  * Context: Process context.  Takes and releases the xa_lock while
1006  * disabling interrupts.  May sleep if the @gfp flags permit.
1007  * Return: 0 if the allocation succeeded without wrapping.  1 if the
1008  * allocation succeeded after wrapping, -ENOMEM if memory could not be
1009  * allocated or -EBUSY if there are no free entries in @limit.
1010  */
1011 static inline int xa_alloc_cyclic_irq(struct xarray *xa, u32 *id, void *entry,
1012 		struct xa_limit limit, u32 *next, gfp_t gfp)
1013 {
1014 	int err;
1015 
1016 	xa_lock_irq(xa);
1017 	err = __xa_alloc_cyclic(xa, id, entry, limit, next, gfp);
1018 	xa_unlock_irq(xa);
1019 
1020 	return err;
1021 }
1022 
1023 /**
1024  * xa_reserve() - Reserve this index in the XArray.
1025  * @xa: XArray.
1026  * @index: Index into array.
1027  * @gfp: Memory allocation flags.
1028  *
1029  * Ensures there is somewhere to store an entry at @index in the array.
1030  * If there is already something stored at @index, this function does
1031  * nothing.  If there was nothing there, the entry is marked as reserved.
1032  * Loading from a reserved entry returns a %NULL pointer.
1033  *
1034  * If you do not use the entry that you have reserved, call xa_release()
1035  * or xa_erase() to free any unnecessary memory.
1036  *
1037  * Context: Any context.  Takes and releases the xa_lock.
1038  * May sleep if the @gfp flags permit.
1039  * Return: 0 if the reservation succeeded or -ENOMEM if it failed.
1040  */
1041 static inline __must_check
1042 int xa_reserve(struct xarray *xa, unsigned long index, gfp_t gfp)
1043 {
1044 	return xa_err(xa_cmpxchg(xa, index, NULL, XA_ZERO_ENTRY, gfp));
1045 }
1046 
1047 /**
1048  * xa_reserve_bh() - Reserve this index in the XArray.
1049  * @xa: XArray.
1050  * @index: Index into array.
1051  * @gfp: Memory allocation flags.
1052  *
1053  * A softirq-disabling version of xa_reserve().
1054  *
1055  * Context: Any context.  Takes and releases the xa_lock while
1056  * disabling softirqs.
1057  * Return: 0 if the reservation succeeded or -ENOMEM if it failed.
1058  */
1059 static inline __must_check
1060 int xa_reserve_bh(struct xarray *xa, unsigned long index, gfp_t gfp)
1061 {
1062 	return xa_err(xa_cmpxchg_bh(xa, index, NULL, XA_ZERO_ENTRY, gfp));
1063 }
1064 
1065 /**
1066  * xa_reserve_irq() - Reserve this index in the XArray.
1067  * @xa: XArray.
1068  * @index: Index into array.
1069  * @gfp: Memory allocation flags.
1070  *
1071  * An interrupt-disabling version of xa_reserve().
1072  *
1073  * Context: Process context.  Takes and releases the xa_lock while
1074  * disabling interrupts.
1075  * Return: 0 if the reservation succeeded or -ENOMEM if it failed.
1076  */
1077 static inline __must_check
1078 int xa_reserve_irq(struct xarray *xa, unsigned long index, gfp_t gfp)
1079 {
1080 	return xa_err(xa_cmpxchg_irq(xa, index, NULL, XA_ZERO_ENTRY, gfp));
1081 }
1082 
1083 /**
1084  * xa_release() - Release a reserved entry.
1085  * @xa: XArray.
1086  * @index: Index of entry.
1087  *
1088  * After calling xa_reserve(), you can call this function to release the
1089  * reservation.  If the entry at @index has been stored to, this function
1090  * will do nothing.
1091  */
1092 static inline void xa_release(struct xarray *xa, unsigned long index)
1093 {
1094 	xa_cmpxchg(xa, index, XA_ZERO_ENTRY, NULL, 0);
1095 }
1096 
1097 /* Everything below here is the Advanced API.  Proceed with caution. */
1098 
1099 /*
1100  * The xarray is constructed out of a set of 'chunks' of pointers.  Choosing
1101  * the best chunk size requires some tradeoffs.  A power of two recommends
1102  * itself so that we can walk the tree based purely on shifts and masks.
1103  * Generally, the larger the better; as the number of slots per level of the
1104  * tree increases, the less tall the tree needs to be.  But that needs to be
1105  * balanced against the memory consumption of each node.  On a 64-bit system,
1106  * xa_node is currently 576 bytes, and we get 7 of them per 4kB page.  If we
1107  * doubled the number of slots per node, we'd get only 3 nodes per 4kB page.
1108  */
1109 #ifndef XA_CHUNK_SHIFT
1110 #define XA_CHUNK_SHIFT		(CONFIG_BASE_SMALL ? 4 : 6)
1111 #endif
1112 #define XA_CHUNK_SIZE		(1UL << XA_CHUNK_SHIFT)
1113 #define XA_CHUNK_MASK		(XA_CHUNK_SIZE - 1)
1114 #define XA_MAX_MARKS		3
1115 #define XA_MARK_LONGS		DIV_ROUND_UP(XA_CHUNK_SIZE, BITS_PER_LONG)
1116 
1117 /*
1118  * @count is the count of every non-NULL element in the ->slots array
1119  * whether that is a value entry, a retry entry, a user pointer,
1120  * a sibling entry or a pointer to the next level of the tree.
1121  * @nr_values is the count of every element in ->slots which is
1122  * either a value entry or a sibling of a value entry.
1123  */
1124 struct xa_node {
1125 	unsigned char	shift;		/* Bits remaining in each slot */
1126 	unsigned char	offset;		/* Slot offset in parent */
1127 	unsigned char	count;		/* Total entry count */
1128 	unsigned char	nr_values;	/* Value entry count */
1129 	struct xa_node __rcu *parent;	/* NULL at top of tree */
1130 	struct xarray	*array;		/* The array we belong to */
1131 	union {
1132 		struct list_head private_list;	/* For tree user */
1133 		struct rcu_head	rcu_head;	/* Used when freeing node */
1134 	};
1135 	void __rcu	*slots[XA_CHUNK_SIZE];
1136 	union {
1137 		unsigned long	tags[XA_MAX_MARKS][XA_MARK_LONGS];
1138 		unsigned long	marks[XA_MAX_MARKS][XA_MARK_LONGS];
1139 	};
1140 };
1141 
1142 void xa_dump(const struct xarray *);
1143 void xa_dump_node(const struct xa_node *);
1144 
1145 #ifdef XA_DEBUG
1146 #define XA_BUG_ON(xa, x) do {					\
1147 		if (x) {					\
1148 			xa_dump(xa);				\
1149 			BUG();					\
1150 		}						\
1151 	} while (0)
1152 #define XA_NODE_BUG_ON(node, x) do {				\
1153 		if (x) {					\
1154 			if (node) xa_dump_node(node);		\
1155 			BUG();					\
1156 		}						\
1157 	} while (0)
1158 #else
1159 #define XA_BUG_ON(xa, x)	do { } while (0)
1160 #define XA_NODE_BUG_ON(node, x)	do { } while (0)
1161 #endif
1162 
1163 /* Private */
1164 static inline void *xa_head(const struct xarray *xa)
1165 {
1166 	return rcu_dereference_check(xa->xa_head,
1167 						lockdep_is_held(&xa->xa_lock));
1168 }
1169 
1170 /* Private */
1171 static inline void *xa_head_locked(const struct xarray *xa)
1172 {
1173 	return rcu_dereference_protected(xa->xa_head,
1174 						lockdep_is_held(&xa->xa_lock));
1175 }
1176 
1177 /* Private */
1178 static inline void *xa_entry(const struct xarray *xa,
1179 				const struct xa_node *node, unsigned int offset)
1180 {
1181 	XA_NODE_BUG_ON(node, offset >= XA_CHUNK_SIZE);
1182 	return rcu_dereference_check(node->slots[offset],
1183 						lockdep_is_held(&xa->xa_lock));
1184 }
1185 
1186 /* Private */
1187 static inline void *xa_entry_locked(const struct xarray *xa,
1188 				const struct xa_node *node, unsigned int offset)
1189 {
1190 	XA_NODE_BUG_ON(node, offset >= XA_CHUNK_SIZE);
1191 	return rcu_dereference_protected(node->slots[offset],
1192 						lockdep_is_held(&xa->xa_lock));
1193 }
1194 
1195 /* Private */
1196 static inline struct xa_node *xa_parent(const struct xarray *xa,
1197 					const struct xa_node *node)
1198 {
1199 	return rcu_dereference_check(node->parent,
1200 						lockdep_is_held(&xa->xa_lock));
1201 }
1202 
1203 /* Private */
1204 static inline struct xa_node *xa_parent_locked(const struct xarray *xa,
1205 					const struct xa_node *node)
1206 {
1207 	return rcu_dereference_protected(node->parent,
1208 						lockdep_is_held(&xa->xa_lock));
1209 }
1210 
1211 /* Private */
1212 static inline void *xa_mk_node(const struct xa_node *node)
1213 {
1214 	return (void *)((unsigned long)node | 2);
1215 }
1216 
1217 /* Private */
1218 static inline struct xa_node *xa_to_node(const void *entry)
1219 {
1220 	return (struct xa_node *)((unsigned long)entry - 2);
1221 }
1222 
1223 /* Private */
1224 static inline bool xa_is_node(const void *entry)
1225 {
1226 	return xa_is_internal(entry) && (unsigned long)entry > 4096;
1227 }
1228 
1229 /* Private */
1230 static inline void *xa_mk_sibling(unsigned int offset)
1231 {
1232 	return xa_mk_internal(offset);
1233 }
1234 
1235 /* Private */
1236 static inline unsigned long xa_to_sibling(const void *entry)
1237 {
1238 	return xa_to_internal(entry);
1239 }
1240 
1241 /**
1242  * xa_is_sibling() - Is the entry a sibling entry?
1243  * @entry: Entry retrieved from the XArray
1244  *
1245  * Return: %true if the entry is a sibling entry.
1246  */
1247 static inline bool xa_is_sibling(const void *entry)
1248 {
1249 	return IS_ENABLED(CONFIG_XARRAY_MULTI) && xa_is_internal(entry) &&
1250 		(entry < xa_mk_sibling(XA_CHUNK_SIZE - 1));
1251 }
1252 
1253 #define XA_RETRY_ENTRY		xa_mk_internal(256)
1254 
1255 /**
1256  * xa_is_retry() - Is the entry a retry entry?
1257  * @entry: Entry retrieved from the XArray
1258  *
1259  * Return: %true if the entry is a retry entry.
1260  */
1261 static inline bool xa_is_retry(const void *entry)
1262 {
1263 	return unlikely(entry == XA_RETRY_ENTRY);
1264 }
1265 
1266 /**
1267  * xa_is_advanced() - Is the entry only permitted for the advanced API?
1268  * @entry: Entry to be stored in the XArray.
1269  *
1270  * Return: %true if the entry cannot be stored by the normal API.
1271  */
1272 static inline bool xa_is_advanced(const void *entry)
1273 {
1274 	return xa_is_internal(entry) && (entry <= XA_RETRY_ENTRY);
1275 }
1276 
1277 /**
1278  * typedef xa_update_node_t - A callback function from the XArray.
1279  * @node: The node which is being processed
1280  *
1281  * This function is called every time the XArray updates the count of
1282  * present and value entries in a node.  It allows advanced users to
1283  * maintain the private_list in the node.
1284  *
1285  * Context: The xa_lock is held and interrupts may be disabled.
1286  *	    Implementations should not drop the xa_lock, nor re-enable
1287  *	    interrupts.
1288  */
1289 typedef void (*xa_update_node_t)(struct xa_node *node);
1290 
1291 void xa_delete_node(struct xa_node *, xa_update_node_t);
1292 
1293 /*
1294  * The xa_state is opaque to its users.  It contains various different pieces
1295  * of state involved in the current operation on the XArray.  It should be
1296  * declared on the stack and passed between the various internal routines.
1297  * The various elements in it should not be accessed directly, but only
1298  * through the provided accessor functions.  The below documentation is for
1299  * the benefit of those working on the code, not for users of the XArray.
1300  *
1301  * @xa_node usually points to the xa_node containing the slot we're operating
1302  * on (and @xa_offset is the offset in the slots array).  If there is a
1303  * single entry in the array at index 0, there are no allocated xa_nodes to
1304  * point to, and so we store %NULL in @xa_node.  @xa_node is set to
1305  * the value %XAS_RESTART if the xa_state is not walked to the correct
1306  * position in the tree of nodes for this operation.  If an error occurs
1307  * during an operation, it is set to an %XAS_ERROR value.  If we run off the
1308  * end of the allocated nodes, it is set to %XAS_BOUNDS.
1309  */
1310 struct xa_state {
1311 	struct xarray *xa;
1312 	unsigned long xa_index;
1313 	unsigned char xa_shift;
1314 	unsigned char xa_sibs;
1315 	unsigned char xa_offset;
1316 	unsigned char xa_pad;		/* Helps gcc generate better code */
1317 	struct xa_node *xa_node;
1318 	struct xa_node *xa_alloc;
1319 	xa_update_node_t xa_update;
1320 };
1321 
1322 /*
1323  * We encode errnos in the xas->xa_node.  If an error has happened, we need to
1324  * drop the lock to fix it, and once we've done so the xa_state is invalid.
1325  */
1326 #define XA_ERROR(errno) ((struct xa_node *)(((unsigned long)errno << 2) | 2UL))
1327 #define XAS_BOUNDS	((struct xa_node *)1UL)
1328 #define XAS_RESTART	((struct xa_node *)3UL)
1329 
1330 #define __XA_STATE(array, index, shift, sibs)  {	\
1331 	.xa = array,					\
1332 	.xa_index = index,				\
1333 	.xa_shift = shift,				\
1334 	.xa_sibs = sibs,				\
1335 	.xa_offset = 0,					\
1336 	.xa_pad = 0,					\
1337 	.xa_node = XAS_RESTART,				\
1338 	.xa_alloc = NULL,				\
1339 	.xa_update = NULL				\
1340 }
1341 
1342 /**
1343  * XA_STATE() - Declare an XArray operation state.
1344  * @name: Name of this operation state (usually xas).
1345  * @array: Array to operate on.
1346  * @index: Initial index of interest.
1347  *
1348  * Declare and initialise an xa_state on the stack.
1349  */
1350 #define XA_STATE(name, array, index)				\
1351 	struct xa_state name = __XA_STATE(array, index, 0, 0)
1352 
1353 /**
1354  * XA_STATE_ORDER() - Declare an XArray operation state.
1355  * @name: Name of this operation state (usually xas).
1356  * @array: Array to operate on.
1357  * @index: Initial index of interest.
1358  * @order: Order of entry.
1359  *
1360  * Declare and initialise an xa_state on the stack.  This variant of
1361  * XA_STATE() allows you to specify the 'order' of the element you
1362  * want to operate on.`
1363  */
1364 #define XA_STATE_ORDER(name, array, index, order)		\
1365 	struct xa_state name = __XA_STATE(array,		\
1366 			(index >> order) << order,		\
1367 			order - (order % XA_CHUNK_SHIFT),	\
1368 			(1U << (order % XA_CHUNK_SHIFT)) - 1)
1369 
1370 #define xas_marked(xas, mark)	xa_marked((xas)->xa, (mark))
1371 #define xas_trylock(xas)	xa_trylock((xas)->xa)
1372 #define xas_lock(xas)		xa_lock((xas)->xa)
1373 #define xas_unlock(xas)		xa_unlock((xas)->xa)
1374 #define xas_lock_bh(xas)	xa_lock_bh((xas)->xa)
1375 #define xas_unlock_bh(xas)	xa_unlock_bh((xas)->xa)
1376 #define xas_lock_irq(xas)	xa_lock_irq((xas)->xa)
1377 #define xas_unlock_irq(xas)	xa_unlock_irq((xas)->xa)
1378 #define xas_lock_irqsave(xas, flags) \
1379 				xa_lock_irqsave((xas)->xa, flags)
1380 #define xas_unlock_irqrestore(xas, flags) \
1381 				xa_unlock_irqrestore((xas)->xa, flags)
1382 
1383 /**
1384  * xas_error() - Return an errno stored in the xa_state.
1385  * @xas: XArray operation state.
1386  *
1387  * Return: 0 if no error has been noted.  A negative errno if one has.
1388  */
1389 static inline int xas_error(const struct xa_state *xas)
1390 {
1391 	return xa_err(xas->xa_node);
1392 }
1393 
1394 /**
1395  * xas_set_err() - Note an error in the xa_state.
1396  * @xas: XArray operation state.
1397  * @err: Negative error number.
1398  *
1399  * Only call this function with a negative @err; zero or positive errors
1400  * will probably not behave the way you think they should.  If you want
1401  * to clear the error from an xa_state, use xas_reset().
1402  */
1403 static inline void xas_set_err(struct xa_state *xas, long err)
1404 {
1405 	xas->xa_node = XA_ERROR(err);
1406 }
1407 
1408 /**
1409  * xas_invalid() - Is the xas in a retry or error state?
1410  * @xas: XArray operation state.
1411  *
1412  * Return: %true if the xas cannot be used for operations.
1413  */
1414 static inline bool xas_invalid(const struct xa_state *xas)
1415 {
1416 	return (unsigned long)xas->xa_node & 3;
1417 }
1418 
1419 /**
1420  * xas_valid() - Is the xas a valid cursor into the array?
1421  * @xas: XArray operation state.
1422  *
1423  * Return: %true if the xas can be used for operations.
1424  */
1425 static inline bool xas_valid(const struct xa_state *xas)
1426 {
1427 	return !xas_invalid(xas);
1428 }
1429 
1430 /**
1431  * xas_is_node() - Does the xas point to a node?
1432  * @xas: XArray operation state.
1433  *
1434  * Return: %true if the xas currently references a node.
1435  */
1436 static inline bool xas_is_node(const struct xa_state *xas)
1437 {
1438 	return xas_valid(xas) && xas->xa_node;
1439 }
1440 
1441 /* True if the pointer is something other than a node */
1442 static inline bool xas_not_node(struct xa_node *node)
1443 {
1444 	return ((unsigned long)node & 3) || !node;
1445 }
1446 
1447 /* True if the node represents RESTART or an error */
1448 static inline bool xas_frozen(struct xa_node *node)
1449 {
1450 	return (unsigned long)node & 2;
1451 }
1452 
1453 /* True if the node represents head-of-tree, RESTART or BOUNDS */
1454 static inline bool xas_top(struct xa_node *node)
1455 {
1456 	return node <= XAS_RESTART;
1457 }
1458 
1459 /**
1460  * xas_reset() - Reset an XArray operation state.
1461  * @xas: XArray operation state.
1462  *
1463  * Resets the error or walk state of the @xas so future walks of the
1464  * array will start from the root.  Use this if you have dropped the
1465  * xarray lock and want to reuse the xa_state.
1466  *
1467  * Context: Any context.
1468  */
1469 static inline void xas_reset(struct xa_state *xas)
1470 {
1471 	xas->xa_node = XAS_RESTART;
1472 }
1473 
1474 /**
1475  * xas_retry() - Retry the operation if appropriate.
1476  * @xas: XArray operation state.
1477  * @entry: Entry from xarray.
1478  *
1479  * The advanced functions may sometimes return an internal entry, such as
1480  * a retry entry or a zero entry.  This function sets up the @xas to restart
1481  * the walk from the head of the array if needed.
1482  *
1483  * Context: Any context.
1484  * Return: true if the operation needs to be retried.
1485  */
1486 static inline bool xas_retry(struct xa_state *xas, const void *entry)
1487 {
1488 	if (xa_is_zero(entry))
1489 		return true;
1490 	if (!xa_is_retry(entry))
1491 		return false;
1492 	xas_reset(xas);
1493 	return true;
1494 }
1495 
1496 void *xas_load(struct xa_state *);
1497 void *xas_store(struct xa_state *, void *entry);
1498 void *xas_find(struct xa_state *, unsigned long max);
1499 void *xas_find_conflict(struct xa_state *);
1500 
1501 bool xas_get_mark(const struct xa_state *, xa_mark_t);
1502 void xas_set_mark(const struct xa_state *, xa_mark_t);
1503 void xas_clear_mark(const struct xa_state *, xa_mark_t);
1504 void *xas_find_marked(struct xa_state *, unsigned long max, xa_mark_t);
1505 void xas_init_marks(const struct xa_state *);
1506 
1507 bool xas_nomem(struct xa_state *, gfp_t);
1508 void xas_pause(struct xa_state *);
1509 
1510 void xas_create_range(struct xa_state *);
1511 
1512 #ifdef CONFIG_XARRAY_MULTI
1513 int xa_get_order(struct xarray *, unsigned long index);
1514 void xas_split(struct xa_state *, void *entry, unsigned int order);
1515 void xas_split_alloc(struct xa_state *, void *entry, unsigned int order, gfp_t);
1516 #else
1517 static inline int xa_get_order(struct xarray *xa, unsigned long index)
1518 {
1519 	return 0;
1520 }
1521 
1522 static inline void xas_split(struct xa_state *xas, void *entry,
1523 		unsigned int order)
1524 {
1525 	xas_store(xas, entry);
1526 }
1527 
1528 static inline void xas_split_alloc(struct xa_state *xas, void *entry,
1529 		unsigned int order, gfp_t gfp)
1530 {
1531 }
1532 #endif
1533 
1534 /**
1535  * xas_reload() - Refetch an entry from the xarray.
1536  * @xas: XArray operation state.
1537  *
1538  * Use this function to check that a previously loaded entry still has
1539  * the same value.  This is useful for the lockless pagecache lookup where
1540  * we walk the array with only the RCU lock to protect us, lock the page,
1541  * then check that the page hasn't moved since we looked it up.
1542  *
1543  * The caller guarantees that @xas is still valid.  If it may be in an
1544  * error or restart state, call xas_load() instead.
1545  *
1546  * Return: The entry at this location in the xarray.
1547  */
1548 static inline void *xas_reload(struct xa_state *xas)
1549 {
1550 	struct xa_node *node = xas->xa_node;
1551 	void *entry;
1552 	char offset;
1553 
1554 	if (!node)
1555 		return xa_head(xas->xa);
1556 	if (IS_ENABLED(CONFIG_XARRAY_MULTI)) {
1557 		offset = (xas->xa_index >> node->shift) & XA_CHUNK_MASK;
1558 		entry = xa_entry(xas->xa, node, offset);
1559 		if (!xa_is_sibling(entry))
1560 			return entry;
1561 		offset = xa_to_sibling(entry);
1562 	} else {
1563 		offset = xas->xa_offset;
1564 	}
1565 	return xa_entry(xas->xa, node, offset);
1566 }
1567 
1568 /**
1569  * xas_set() - Set up XArray operation state for a different index.
1570  * @xas: XArray operation state.
1571  * @index: New index into the XArray.
1572  *
1573  * Move the operation state to refer to a different index.  This will
1574  * have the effect of starting a walk from the top; see xas_next()
1575  * to move to an adjacent index.
1576  */
1577 static inline void xas_set(struct xa_state *xas, unsigned long index)
1578 {
1579 	xas->xa_index = index;
1580 	xas->xa_node = XAS_RESTART;
1581 }
1582 
1583 /**
1584  * xas_advance() - Skip over sibling entries.
1585  * @xas: XArray operation state.
1586  * @index: Index of last sibling entry.
1587  *
1588  * Move the operation state to refer to the last sibling entry.
1589  * This is useful for loops that normally want to see sibling
1590  * entries but sometimes want to skip them.  Use xas_set() if you
1591  * want to move to an index which is not part of this entry.
1592  */
1593 static inline void xas_advance(struct xa_state *xas, unsigned long index)
1594 {
1595 	unsigned char shift = xas_is_node(xas) ? xas->xa_node->shift : 0;
1596 
1597 	xas->xa_index = index;
1598 	xas->xa_offset = (index >> shift) & XA_CHUNK_MASK;
1599 }
1600 
1601 /**
1602  * xas_set_order() - Set up XArray operation state for a multislot entry.
1603  * @xas: XArray operation state.
1604  * @index: Target of the operation.
1605  * @order: Entry occupies 2^@order indices.
1606  */
1607 static inline void xas_set_order(struct xa_state *xas, unsigned long index,
1608 					unsigned int order)
1609 {
1610 #ifdef CONFIG_XARRAY_MULTI
1611 	xas->xa_index = order < BITS_PER_LONG ? (index >> order) << order : 0;
1612 	xas->xa_shift = order - (order % XA_CHUNK_SHIFT);
1613 	xas->xa_sibs = (1 << (order % XA_CHUNK_SHIFT)) - 1;
1614 	xas->xa_node = XAS_RESTART;
1615 #else
1616 	BUG_ON(order > 0);
1617 	xas_set(xas, index);
1618 #endif
1619 }
1620 
1621 /**
1622  * xas_set_update() - Set up XArray operation state for a callback.
1623  * @xas: XArray operation state.
1624  * @update: Function to call when updating a node.
1625  *
1626  * The XArray can notify a caller after it has updated an xa_node.
1627  * This is advanced functionality and is only needed by the page cache.
1628  */
1629 static inline void xas_set_update(struct xa_state *xas, xa_update_node_t update)
1630 {
1631 	xas->xa_update = update;
1632 }
1633 
1634 /**
1635  * xas_next_entry() - Advance iterator to next present entry.
1636  * @xas: XArray operation state.
1637  * @max: Highest index to return.
1638  *
1639  * xas_next_entry() is an inline function to optimise xarray traversal for
1640  * speed.  It is equivalent to calling xas_find(), and will call xas_find()
1641  * for all the hard cases.
1642  *
1643  * Return: The next present entry after the one currently referred to by @xas.
1644  */
1645 static inline void *xas_next_entry(struct xa_state *xas, unsigned long max)
1646 {
1647 	struct xa_node *node = xas->xa_node;
1648 	void *entry;
1649 
1650 	if (unlikely(xas_not_node(node) || node->shift ||
1651 			xas->xa_offset != (xas->xa_index & XA_CHUNK_MASK)))
1652 		return xas_find(xas, max);
1653 
1654 	do {
1655 		if (unlikely(xas->xa_index >= max))
1656 			return xas_find(xas, max);
1657 		if (unlikely(xas->xa_offset == XA_CHUNK_MASK))
1658 			return xas_find(xas, max);
1659 		entry = xa_entry(xas->xa, node, xas->xa_offset + 1);
1660 		if (unlikely(xa_is_internal(entry)))
1661 			return xas_find(xas, max);
1662 		xas->xa_offset++;
1663 		xas->xa_index++;
1664 	} while (!entry);
1665 
1666 	return entry;
1667 }
1668 
1669 /* Private */
1670 static inline unsigned int xas_find_chunk(struct xa_state *xas, bool advance,
1671 		xa_mark_t mark)
1672 {
1673 	unsigned long *addr = xas->xa_node->marks[(__force unsigned)mark];
1674 	unsigned int offset = xas->xa_offset;
1675 
1676 	if (advance)
1677 		offset++;
1678 	if (XA_CHUNK_SIZE == BITS_PER_LONG) {
1679 		if (offset < XA_CHUNK_SIZE) {
1680 			unsigned long data = *addr & (~0UL << offset);
1681 			if (data)
1682 				return __ffs(data);
1683 		}
1684 		return XA_CHUNK_SIZE;
1685 	}
1686 
1687 	return find_next_bit(addr, XA_CHUNK_SIZE, offset);
1688 }
1689 
1690 /**
1691  * xas_next_marked() - Advance iterator to next marked entry.
1692  * @xas: XArray operation state.
1693  * @max: Highest index to return.
1694  * @mark: Mark to search for.
1695  *
1696  * xas_next_marked() is an inline function to optimise xarray traversal for
1697  * speed.  It is equivalent to calling xas_find_marked(), and will call
1698  * xas_find_marked() for all the hard cases.
1699  *
1700  * Return: The next marked entry after the one currently referred to by @xas.
1701  */
1702 static inline void *xas_next_marked(struct xa_state *xas, unsigned long max,
1703 								xa_mark_t mark)
1704 {
1705 	struct xa_node *node = xas->xa_node;
1706 	void *entry;
1707 	unsigned int offset;
1708 
1709 	if (unlikely(xas_not_node(node) || node->shift))
1710 		return xas_find_marked(xas, max, mark);
1711 	offset = xas_find_chunk(xas, true, mark);
1712 	xas->xa_offset = offset;
1713 	xas->xa_index = (xas->xa_index & ~XA_CHUNK_MASK) + offset;
1714 	if (xas->xa_index > max)
1715 		return NULL;
1716 	if (offset == XA_CHUNK_SIZE)
1717 		return xas_find_marked(xas, max, mark);
1718 	entry = xa_entry(xas->xa, node, offset);
1719 	if (!entry)
1720 		return xas_find_marked(xas, max, mark);
1721 	return entry;
1722 }
1723 
1724 /*
1725  * If iterating while holding a lock, drop the lock and reschedule
1726  * every %XA_CHECK_SCHED loops.
1727  */
1728 enum {
1729 	XA_CHECK_SCHED = 4096,
1730 };
1731 
1732 /**
1733  * xas_for_each() - Iterate over a range of an XArray.
1734  * @xas: XArray operation state.
1735  * @entry: Entry retrieved from the array.
1736  * @max: Maximum index to retrieve from array.
1737  *
1738  * The loop body will be executed for each entry present in the xarray
1739  * between the current xas position and @max.  @entry will be set to
1740  * the entry retrieved from the xarray.  It is safe to delete entries
1741  * from the array in the loop body.  You should hold either the RCU lock
1742  * or the xa_lock while iterating.  If you need to drop the lock, call
1743  * xas_pause() first.
1744  */
1745 #define xas_for_each(xas, entry, max) \
1746 	for (entry = xas_find(xas, max); entry; \
1747 	     entry = xas_next_entry(xas, max))
1748 
1749 /**
1750  * xas_for_each_marked() - Iterate over a range of an XArray.
1751  * @xas: XArray operation state.
1752  * @entry: Entry retrieved from the array.
1753  * @max: Maximum index to retrieve from array.
1754  * @mark: Mark to search for.
1755  *
1756  * The loop body will be executed for each marked entry in the xarray
1757  * between the current xas position and @max.  @entry will be set to
1758  * the entry retrieved from the xarray.  It is safe to delete entries
1759  * from the array in the loop body.  You should hold either the RCU lock
1760  * or the xa_lock while iterating.  If you need to drop the lock, call
1761  * xas_pause() first.
1762  */
1763 #define xas_for_each_marked(xas, entry, max, mark) \
1764 	for (entry = xas_find_marked(xas, max, mark); entry; \
1765 	     entry = xas_next_marked(xas, max, mark))
1766 
1767 /**
1768  * xas_for_each_conflict() - Iterate over a range of an XArray.
1769  * @xas: XArray operation state.
1770  * @entry: Entry retrieved from the array.
1771  *
1772  * The loop body will be executed for each entry in the XArray that
1773  * lies within the range specified by @xas.  If the loop terminates
1774  * normally, @entry will be %NULL.  The user may break out of the loop,
1775  * which will leave @entry set to the conflicting entry.  The caller
1776  * may also call xa_set_err() to exit the loop while setting an error
1777  * to record the reason.
1778  */
1779 #define xas_for_each_conflict(xas, entry) \
1780 	while ((entry = xas_find_conflict(xas)))
1781 
1782 void *__xas_next(struct xa_state *);
1783 void *__xas_prev(struct xa_state *);
1784 
1785 /**
1786  * xas_prev() - Move iterator to previous index.
1787  * @xas: XArray operation state.
1788  *
1789  * If the @xas was in an error state, it will remain in an error state
1790  * and this function will return %NULL.  If the @xas has never been walked,
1791  * it will have the effect of calling xas_load().  Otherwise one will be
1792  * subtracted from the index and the state will be walked to the correct
1793  * location in the array for the next operation.
1794  *
1795  * If the iterator was referencing index 0, this function wraps
1796  * around to %ULONG_MAX.
1797  *
1798  * Return: The entry at the new index.  This may be %NULL or an internal
1799  * entry.
1800  */
1801 static inline void *xas_prev(struct xa_state *xas)
1802 {
1803 	struct xa_node *node = xas->xa_node;
1804 
1805 	if (unlikely(xas_not_node(node) || node->shift ||
1806 				xas->xa_offset == 0))
1807 		return __xas_prev(xas);
1808 
1809 	xas->xa_index--;
1810 	xas->xa_offset--;
1811 	return xa_entry(xas->xa, node, xas->xa_offset);
1812 }
1813 
1814 /**
1815  * xas_next() - Move state to next index.
1816  * @xas: XArray operation state.
1817  *
1818  * If the @xas was in an error state, it will remain in an error state
1819  * and this function will return %NULL.  If the @xas has never been walked,
1820  * it will have the effect of calling xas_load().  Otherwise one will be
1821  * added to the index and the state will be walked to the correct
1822  * location in the array for the next operation.
1823  *
1824  * If the iterator was referencing index %ULONG_MAX, this function wraps
1825  * around to 0.
1826  *
1827  * Return: The entry at the new index.  This may be %NULL or an internal
1828  * entry.
1829  */
1830 static inline void *xas_next(struct xa_state *xas)
1831 {
1832 	struct xa_node *node = xas->xa_node;
1833 
1834 	if (unlikely(xas_not_node(node) || node->shift ||
1835 				xas->xa_offset == XA_CHUNK_MASK))
1836 		return __xas_next(xas);
1837 
1838 	xas->xa_index++;
1839 	xas->xa_offset++;
1840 	return xa_entry(xas->xa, node, xas->xa_offset);
1841 }
1842 
1843 #endif /* _LINUX_XARRAY_H */
1844