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