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