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