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