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