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