blob: e0b57af52e9b2a469079a25171efa2034192f86d [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 Wilcoxf8d5d0c2017-11-07 16:30:10 -0500289void xa_init_flags(struct xarray *, gfp_t flags);
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 Wilcox41aec912017-11-10 15:34:55 -0500292void *xa_cmpxchg(struct xarray *, unsigned long index,
293 void *old, void *entry, gfp_t);
Matthew Wilcox9f14d4f2018-10-01 14:54:59 -0400294int xa_reserve(struct xarray *, unsigned long index, 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/**
307 * xa_init() - Initialise an empty XArray.
308 * @xa: XArray.
309 *
310 * An empty XArray is full of NULL entries.
311 *
312 * Context: Any context.
313 */
314static inline void xa_init(struct xarray *xa)
315{
316 xa_init_flags(xa, 0);
317}
318
Matthew Wilcoxad3d6c72017-11-07 14:57:46 -0500319/**
320 * xa_empty() - Determine if an array has any present entries.
321 * @xa: XArray.
322 *
323 * Context: Any context.
324 * Return: %true if the array contains only NULL pointers.
325 */
326static inline bool xa_empty(const struct xarray *xa)
327{
328 return xa->xa_head == NULL;
329}
330
Matthew Wilcox9b89a032017-11-10 09:34:31 -0500331/**
332 * xa_marked() - Inquire whether any entry in this array has a mark set
333 * @xa: Array
334 * @mark: Mark value
335 *
336 * Context: Any context.
337 * Return: %true if any entry has this mark set.
338 */
339static inline bool xa_marked(const struct xarray *xa, xa_mark_t mark)
340{
341 return xa->xa_flags & XA_FLAGS_MARK(mark);
342}
343
Matthew Wilcox58d6ea32017-11-10 15:15:08 -0500344/**
345 * xa_erase() - Erase this entry from the XArray.
346 * @xa: XArray.
347 * @index: Index of entry.
348 *
349 * This function is the equivalent of calling xa_store() with %NULL as
350 * the third argument. The XArray does not need to allocate memory, so
351 * the user does not need to provide GFP flags.
352 *
353 * Context: Process context. Takes and releases the xa_lock.
354 * Return: The entry which used to be at this index.
355 */
356static inline void *xa_erase(struct xarray *xa, unsigned long index)
357{
358 return xa_store(xa, index, NULL, 0);
359}
360
Matthew Wilcox41aec912017-11-10 15:34:55 -0500361/**
362 * xa_insert() - Store this entry in the XArray unless another entry is
363 * already present.
364 * @xa: XArray.
365 * @index: Index into array.
366 * @entry: New entry.
367 * @gfp: Memory allocation flags.
368 *
369 * If you would rather see the existing entry in the array, use xa_cmpxchg().
370 * This function is for users who don't care what the entry is, only that
371 * one is present.
372 *
373 * Context: Process context. Takes and releases the xa_lock.
374 * May sleep if the @gfp flags permit.
375 * Return: 0 if the store succeeded. -EEXIST if another entry was present.
376 * -ENOMEM if memory could not be allocated.
377 */
378static inline int xa_insert(struct xarray *xa, unsigned long index,
379 void *entry, gfp_t gfp)
380{
381 void *curr = xa_cmpxchg(xa, index, NULL, entry, gfp);
382 if (!curr)
383 return 0;
384 if (xa_is_err(curr))
385 return xa_err(curr);
386 return -EEXIST;
387}
388
Matthew Wilcoxb803b422017-11-14 08:30:11 -0500389/**
Matthew Wilcox9f14d4f2018-10-01 14:54:59 -0400390 * xa_release() - Release a reserved entry.
391 * @xa: XArray.
392 * @index: Index of entry.
393 *
394 * After calling xa_reserve(), you can call this function to release the
395 * reservation. If the entry at @index has been stored to, this function
396 * will do nothing.
397 */
398static inline void xa_release(struct xarray *xa, unsigned long index)
399{
400 xa_cmpxchg(xa, index, NULL, NULL, 0);
401}
402
403/**
Matthew Wilcoxb803b422017-11-14 08:30:11 -0500404 * xa_for_each() - Iterate over a portion of an XArray.
405 * @xa: XArray.
406 * @entry: Entry retrieved from array.
407 * @index: Index of @entry.
408 * @max: Maximum index to retrieve from array.
409 * @filter: Selection criterion.
410 *
411 * Initialise @index to the lowest index you want to retrieve from the
412 * array. During the iteration, @entry will have the value of the entry
413 * stored in @xa at @index. The iteration will skip all entries in the
414 * array which do not match @filter. You may modify @index during the
415 * iteration if you want to skip or reprocess indices. It is safe to modify
416 * the array during the iteration. At the end of the iteration, @entry will
417 * be set to NULL and @index will have a value less than or equal to max.
418 *
419 * xa_for_each() is O(n.log(n)) while xas_for_each() is O(n). You have
420 * to handle your own locking with xas_for_each(), and if you have to unlock
421 * after each iteration, it will also end up being O(n.log(n)). xa_for_each()
422 * will spin if it hits a retry entry; if you intend to see retry entries,
423 * you should use the xas_for_each() iterator instead. The xas_for_each()
424 * iterator will expand into more inline code than xa_for_each().
425 *
426 * Context: Any context. Takes and releases the RCU lock.
427 */
428#define xa_for_each(xa, entry, index, max, filter) \
429 for (entry = xa_find(xa, &index, max, filter); entry; \
430 entry = xa_find_after(xa, &index, max, filter))
431
Matthew Wilcoxf6bb2a22018-04-10 16:36:52 -0700432#define xa_trylock(xa) spin_trylock(&(xa)->xa_lock)
433#define xa_lock(xa) spin_lock(&(xa)->xa_lock)
434#define xa_unlock(xa) spin_unlock(&(xa)->xa_lock)
435#define xa_lock_bh(xa) spin_lock_bh(&(xa)->xa_lock)
436#define xa_unlock_bh(xa) spin_unlock_bh(&(xa)->xa_lock)
437#define xa_lock_irq(xa) spin_lock_irq(&(xa)->xa_lock)
438#define xa_unlock_irq(xa) spin_unlock_irq(&(xa)->xa_lock)
439#define xa_lock_irqsave(xa, flags) \
440 spin_lock_irqsave(&(xa)->xa_lock, flags)
441#define xa_unlock_irqrestore(xa, flags) \
442 spin_unlock_irqrestore(&(xa)->xa_lock, flags)
443
Matthew Wilcox9b89a032017-11-10 09:34:31 -0500444/*
Matthew Wilcox58d6ea32017-11-10 15:15:08 -0500445 * Versions of the normal API which require the caller to hold the
446 * xa_lock. If the GFP flags allow it, they will drop the lock to
447 * allocate memory, then reacquire it afterwards. These functions
448 * may also re-enable interrupts if the XArray flags indicate the
449 * locking should be interrupt safe.
Matthew Wilcox9b89a032017-11-10 09:34:31 -0500450 */
Matthew Wilcox58d6ea32017-11-10 15:15:08 -0500451void *__xa_erase(struct xarray *, unsigned long index);
452void *__xa_store(struct xarray *, unsigned long index, void *entry, gfp_t);
Matthew Wilcox41aec912017-11-10 15:34:55 -0500453void *__xa_cmpxchg(struct xarray *, unsigned long index, void *old,
454 void *entry, gfp_t);
Matthew Wilcox371c7522018-07-04 10:50:12 -0400455int __xa_alloc(struct xarray *, u32 *id, u32 max, void *entry, gfp_t);
Matthew Wilcox9b89a032017-11-10 09:34:31 -0500456void __xa_set_mark(struct xarray *, unsigned long index, xa_mark_t);
457void __xa_clear_mark(struct xarray *, unsigned long index, xa_mark_t);
458
Matthew Wilcox58d6ea32017-11-10 15:15:08 -0500459/**
Matthew Wilcox41aec912017-11-10 15:34:55 -0500460 * __xa_insert() - Store this entry in the XArray unless another entry is
461 * already present.
462 * @xa: XArray.
463 * @index: Index into array.
464 * @entry: New entry.
465 * @gfp: Memory allocation flags.
466 *
467 * If you would rather see the existing entry in the array, use __xa_cmpxchg().
468 * This function is for users who don't care what the entry is, only that
469 * one is present.
470 *
471 * Context: Any context. Expects xa_lock to be held on entry. May
472 * release and reacquire xa_lock if the @gfp flags permit.
473 * Return: 0 if the store succeeded. -EEXIST if another entry was present.
474 * -ENOMEM if memory could not be allocated.
475 */
476static inline int __xa_insert(struct xarray *xa, unsigned long index,
477 void *entry, gfp_t gfp)
478{
479 void *curr = __xa_cmpxchg(xa, index, NULL, entry, gfp);
480 if (!curr)
481 return 0;
482 if (xa_is_err(curr))
483 return xa_err(curr);
484 return -EEXIST;
485}
486
487/**
Matthew Wilcox58d6ea32017-11-10 15:15:08 -0500488 * xa_erase_bh() - Erase this entry from the XArray.
489 * @xa: XArray.
490 * @index: Index of entry.
491 *
492 * This function is the equivalent of calling xa_store() with %NULL as
493 * the third argument. The XArray does not need to allocate memory, so
494 * the user does not need to provide GFP flags.
495 *
496 * Context: Process context. Takes and releases the xa_lock while
497 * disabling softirqs.
498 * Return: The entry which used to be at this index.
499 */
500static inline void *xa_erase_bh(struct xarray *xa, unsigned long index)
501{
502 void *entry;
503
504 xa_lock_bh(xa);
505 entry = __xa_erase(xa, index);
506 xa_unlock_bh(xa);
507
508 return entry;
509}
510
511/**
512 * xa_erase_irq() - Erase this entry from the XArray.
513 * @xa: XArray.
514 * @index: Index of entry.
515 *
516 * This function is the equivalent of calling xa_store() with %NULL as
517 * the third argument. The XArray does not need to allocate memory, so
518 * the user does not need to provide GFP flags.
519 *
520 * Context: Process context. Takes and releases the xa_lock while
521 * disabling interrupts.
522 * Return: The entry which used to be at this index.
523 */
524static inline void *xa_erase_irq(struct xarray *xa, unsigned long index)
525{
526 void *entry;
527
528 xa_lock_irq(xa);
529 entry = __xa_erase(xa, index);
530 xa_unlock_irq(xa);
531
532 return entry;
533}
534
Matthew Wilcox371c7522018-07-04 10:50:12 -0400535/**
536 * xa_alloc() - Find somewhere to store this entry in the XArray.
537 * @xa: XArray.
538 * @id: Pointer to ID.
539 * @max: Maximum ID to allocate (inclusive).
540 * @entry: New entry.
541 * @gfp: Memory allocation flags.
542 *
543 * Allocates an unused ID in the range specified by @id and @max.
544 * Updates the @id pointer with the index, then stores the entry at that
545 * index. A concurrent lookup will not see an uninitialised @id.
546 *
547 * Context: Process context. Takes and releases the xa_lock. May sleep if
548 * the @gfp flags permit.
549 * Return: 0 on success, -ENOMEM if memory allocation fails or -ENOSPC if
550 * there is no more space in the XArray.
551 */
552static inline int xa_alloc(struct xarray *xa, u32 *id, u32 max, void *entry,
553 gfp_t gfp)
554{
555 int err;
556
557 xa_lock(xa);
558 err = __xa_alloc(xa, id, max, entry, gfp);
559 xa_unlock(xa);
560
561 return err;
562}
563
564/**
565 * xa_alloc_bh() - Find somewhere to store this entry in the XArray.
566 * @xa: XArray.
567 * @id: Pointer to ID.
568 * @max: Maximum ID to allocate (inclusive).
569 * @entry: New entry.
570 * @gfp: Memory allocation flags.
571 *
572 * Allocates an unused ID in the range specified by @id and @max.
573 * Updates the @id pointer with the index, then stores the entry at that
574 * index. A concurrent lookup will not see an uninitialised @id.
575 *
576 * Context: Process context. Takes and releases the xa_lock while
577 * disabling softirqs. May sleep if the @gfp flags permit.
578 * Return: 0 on success, -ENOMEM if memory allocation fails or -ENOSPC if
579 * there is no more space in the XArray.
580 */
581static inline int xa_alloc_bh(struct xarray *xa, u32 *id, u32 max, void *entry,
582 gfp_t gfp)
583{
584 int err;
585
586 xa_lock_bh(xa);
587 err = __xa_alloc(xa, id, max, entry, gfp);
588 xa_unlock_bh(xa);
589
590 return err;
591}
592
593/**
594 * xa_alloc_irq() - Find somewhere to store this entry in the XArray.
595 * @xa: XArray.
596 * @id: Pointer to ID.
597 * @max: Maximum ID to allocate (inclusive).
598 * @entry: New entry.
599 * @gfp: Memory allocation flags.
600 *
601 * Allocates an unused ID in the range specified by @id and @max.
602 * Updates the @id pointer with the index, then stores the entry at that
603 * index. A concurrent lookup will not see an uninitialised @id.
604 *
605 * Context: Process context. Takes and releases the xa_lock while
606 * disabling interrupts. May sleep if the @gfp flags permit.
607 * Return: 0 on success, -ENOMEM if memory allocation fails or -ENOSPC if
608 * there is no more space in the XArray.
609 */
610static inline int xa_alloc_irq(struct xarray *xa, u32 *id, u32 max, void *entry,
611 gfp_t gfp)
612{
613 int err;
614
615 xa_lock_irq(xa);
616 err = __xa_alloc(xa, id, max, entry, gfp);
617 xa_unlock_irq(xa);
618
619 return err;
620}
621
Matthew Wilcox02c02bf2017-11-03 23:09:45 -0400622/* Everything below here is the Advanced API. Proceed with caution. */
623
624/*
625 * The xarray is constructed out of a set of 'chunks' of pointers. Choosing
626 * the best chunk size requires some tradeoffs. A power of two recommends
627 * itself so that we can walk the tree based purely on shifts and masks.
628 * Generally, the larger the better; as the number of slots per level of the
629 * tree increases, the less tall the tree needs to be. But that needs to be
630 * balanced against the memory consumption of each node. On a 64-bit system,
631 * xa_node is currently 576 bytes, and we get 7 of them per 4kB page. If we
632 * doubled the number of slots per node, we'd get only 3 nodes per 4kB page.
633 */
634#ifndef XA_CHUNK_SHIFT
635#define XA_CHUNK_SHIFT (CONFIG_BASE_SMALL ? 4 : 6)
636#endif
637#define XA_CHUNK_SIZE (1UL << XA_CHUNK_SHIFT)
638#define XA_CHUNK_MASK (XA_CHUNK_SIZE - 1)
Matthew Wilcox01959df2017-11-09 09:23:56 -0500639#define XA_MAX_MARKS 3
640#define XA_MARK_LONGS DIV_ROUND_UP(XA_CHUNK_SIZE, BITS_PER_LONG)
641
642/*
643 * @count is the count of every non-NULL element in the ->slots array
644 * whether that is a value entry, a retry entry, a user pointer,
645 * a sibling entry or a pointer to the next level of the tree.
646 * @nr_values is the count of every element in ->slots which is
647 * either a value entry or a sibling of a value entry.
648 */
649struct xa_node {
650 unsigned char shift; /* Bits remaining in each slot */
651 unsigned char offset; /* Slot offset in parent */
652 unsigned char count; /* Total entry count */
653 unsigned char nr_values; /* Value entry count */
654 struct xa_node __rcu *parent; /* NULL at top of tree */
655 struct xarray *array; /* The array we belong to */
656 union {
657 struct list_head private_list; /* For tree user */
658 struct rcu_head rcu_head; /* Used when freeing node */
659 };
660 void __rcu *slots[XA_CHUNK_SIZE];
661 union {
662 unsigned long tags[XA_MAX_MARKS][XA_MARK_LONGS];
663 unsigned long marks[XA_MAX_MARKS][XA_MARK_LONGS];
664 };
665};
Matthew Wilcox02c02bf2017-11-03 23:09:45 -0400666
Matthew Wilcoxad3d6c72017-11-07 14:57:46 -0500667void xa_dump(const struct xarray *);
668void xa_dump_node(const struct xa_node *);
669
670#ifdef XA_DEBUG
671#define XA_BUG_ON(xa, x) do { \
672 if (x) { \
673 xa_dump(xa); \
674 BUG(); \
675 } \
676 } while (0)
677#define XA_NODE_BUG_ON(node, x) do { \
678 if (x) { \
679 if (node) xa_dump_node(node); \
680 BUG(); \
681 } \
682 } while (0)
683#else
684#define XA_BUG_ON(xa, x) do { } while (0)
685#define XA_NODE_BUG_ON(node, x) do { } while (0)
686#endif
687
688/* Private */
689static inline void *xa_head(const struct xarray *xa)
690{
691 return rcu_dereference_check(xa->xa_head,
692 lockdep_is_held(&xa->xa_lock));
693}
694
695/* Private */
696static inline void *xa_head_locked(const struct xarray *xa)
697{
698 return rcu_dereference_protected(xa->xa_head,
699 lockdep_is_held(&xa->xa_lock));
700}
701
702/* Private */
703static inline void *xa_entry(const struct xarray *xa,
704 const struct xa_node *node, unsigned int offset)
705{
706 XA_NODE_BUG_ON(node, offset >= XA_CHUNK_SIZE);
707 return rcu_dereference_check(node->slots[offset],
708 lockdep_is_held(&xa->xa_lock));
709}
710
711/* Private */
712static inline void *xa_entry_locked(const struct xarray *xa,
713 const struct xa_node *node, unsigned int offset)
714{
715 XA_NODE_BUG_ON(node, offset >= XA_CHUNK_SIZE);
716 return rcu_dereference_protected(node->slots[offset],
717 lockdep_is_held(&xa->xa_lock));
718}
719
720/* Private */
Matthew Wilcox9b89a032017-11-10 09:34:31 -0500721static inline struct xa_node *xa_parent(const struct xarray *xa,
722 const struct xa_node *node)
723{
724 return rcu_dereference_check(node->parent,
725 lockdep_is_held(&xa->xa_lock));
726}
727
728/* Private */
729static inline struct xa_node *xa_parent_locked(const struct xarray *xa,
730 const struct xa_node *node)
731{
732 return rcu_dereference_protected(node->parent,
733 lockdep_is_held(&xa->xa_lock));
734}
735
736/* Private */
Matthew Wilcox58d6ea32017-11-10 15:15:08 -0500737static inline void *xa_mk_node(const struct xa_node *node)
738{
739 return (void *)((unsigned long)node | 2);
740}
741
742/* Private */
Matthew Wilcoxad3d6c72017-11-07 14:57:46 -0500743static inline struct xa_node *xa_to_node(const void *entry)
744{
745 return (struct xa_node *)((unsigned long)entry - 2);
746}
747
Matthew Wilcox02c02bf2017-11-03 23:09:45 -0400748/* Private */
749static inline bool xa_is_node(const void *entry)
750{
751 return xa_is_internal(entry) && (unsigned long)entry > 4096;
752}
753
754/* Private */
755static inline void *xa_mk_sibling(unsigned int offset)
756{
757 return xa_mk_internal(offset);
758}
759
760/* Private */
761static inline unsigned long xa_to_sibling(const void *entry)
762{
763 return xa_to_internal(entry);
764}
765
766/**
767 * xa_is_sibling() - Is the entry a sibling entry?
768 * @entry: Entry retrieved from the XArray
769 *
770 * Return: %true if the entry is a sibling entry.
771 */
772static inline bool xa_is_sibling(const void *entry)
773{
774 return IS_ENABLED(CONFIG_XARRAY_MULTI) && xa_is_internal(entry) &&
775 (entry < xa_mk_sibling(XA_CHUNK_SIZE - 1));
776}
777
Matthew Wilcox9f14d4f2018-10-01 14:54:59 -0400778#define XA_ZERO_ENTRY xa_mk_internal(256)
779#define XA_RETRY_ENTRY xa_mk_internal(257)
780
781/**
782 * xa_is_zero() - Is the entry a zero entry?
783 * @entry: Entry retrieved from the XArray
784 *
785 * Return: %true if the entry is a zero entry.
786 */
787static inline bool xa_is_zero(const void *entry)
788{
789 return unlikely(entry == XA_ZERO_ENTRY);
790}
Matthew Wilcox02c02bf2017-11-03 23:09:45 -0400791
Matthew Wilcoxad3d6c72017-11-07 14:57:46 -0500792/**
793 * xa_is_retry() - Is the entry a retry entry?
794 * @entry: Entry retrieved from the XArray
795 *
796 * Return: %true if the entry is a retry entry.
797 */
798static inline bool xa_is_retry(const void *entry)
799{
800 return unlikely(entry == XA_RETRY_ENTRY);
801}
802
803/**
804 * typedef xa_update_node_t - A callback function from the XArray.
805 * @node: The node which is being processed
806 *
807 * This function is called every time the XArray updates the count of
808 * present and value entries in a node. It allows advanced users to
809 * maintain the private_list in the node.
810 *
811 * Context: The xa_lock is held and interrupts may be disabled.
812 * Implementations should not drop the xa_lock, nor re-enable
813 * interrupts.
814 */
815typedef void (*xa_update_node_t)(struct xa_node *node);
816
817/*
818 * The xa_state is opaque to its users. It contains various different pieces
819 * of state involved in the current operation on the XArray. It should be
820 * declared on the stack and passed between the various internal routines.
821 * The various elements in it should not be accessed directly, but only
822 * through the provided accessor functions. The below documentation is for
823 * the benefit of those working on the code, not for users of the XArray.
824 *
825 * @xa_node usually points to the xa_node containing the slot we're operating
826 * on (and @xa_offset is the offset in the slots array). If there is a
827 * single entry in the array at index 0, there are no allocated xa_nodes to
828 * point to, and so we store %NULL in @xa_node. @xa_node is set to
829 * the value %XAS_RESTART if the xa_state is not walked to the correct
830 * position in the tree of nodes for this operation. If an error occurs
831 * during an operation, it is set to an %XAS_ERROR value. If we run off the
832 * end of the allocated nodes, it is set to %XAS_BOUNDS.
833 */
834struct xa_state {
835 struct xarray *xa;
836 unsigned long xa_index;
837 unsigned char xa_shift;
838 unsigned char xa_sibs;
839 unsigned char xa_offset;
840 unsigned char xa_pad; /* Helps gcc generate better code */
841 struct xa_node *xa_node;
842 struct xa_node *xa_alloc;
843 xa_update_node_t xa_update;
844};
845
846/*
847 * We encode errnos in the xas->xa_node. If an error has happened, we need to
848 * drop the lock to fix it, and once we've done so the xa_state is invalid.
849 */
850#define XA_ERROR(errno) ((struct xa_node *)(((unsigned long)errno << 2) | 2UL))
851#define XAS_BOUNDS ((struct xa_node *)1UL)
852#define XAS_RESTART ((struct xa_node *)3UL)
853
854#define __XA_STATE(array, index, shift, sibs) { \
855 .xa = array, \
856 .xa_index = index, \
857 .xa_shift = shift, \
858 .xa_sibs = sibs, \
859 .xa_offset = 0, \
860 .xa_pad = 0, \
861 .xa_node = XAS_RESTART, \
862 .xa_alloc = NULL, \
863 .xa_update = NULL \
864}
865
866/**
867 * XA_STATE() - Declare an XArray operation state.
868 * @name: Name of this operation state (usually xas).
869 * @array: Array to operate on.
870 * @index: Initial index of interest.
871 *
872 * Declare and initialise an xa_state on the stack.
873 */
874#define XA_STATE(name, array, index) \
875 struct xa_state name = __XA_STATE(array, index, 0, 0)
876
877/**
878 * XA_STATE_ORDER() - Declare an XArray operation state.
879 * @name: Name of this operation state (usually xas).
880 * @array: Array to operate on.
881 * @index: Initial index of interest.
882 * @order: Order of entry.
883 *
884 * Declare and initialise an xa_state on the stack. This variant of
885 * XA_STATE() allows you to specify the 'order' of the element you
886 * want to operate on.`
887 */
888#define XA_STATE_ORDER(name, array, index, order) \
889 struct xa_state name = __XA_STATE(array, \
890 (index >> order) << order, \
891 order - (order % XA_CHUNK_SHIFT), \
892 (1U << (order % XA_CHUNK_SHIFT)) - 1)
893
894#define xas_marked(xas, mark) xa_marked((xas)->xa, (mark))
895#define xas_trylock(xas) xa_trylock((xas)->xa)
896#define xas_lock(xas) xa_lock((xas)->xa)
897#define xas_unlock(xas) xa_unlock((xas)->xa)
898#define xas_lock_bh(xas) xa_lock_bh((xas)->xa)
899#define xas_unlock_bh(xas) xa_unlock_bh((xas)->xa)
900#define xas_lock_irq(xas) xa_lock_irq((xas)->xa)
901#define xas_unlock_irq(xas) xa_unlock_irq((xas)->xa)
902#define xas_lock_irqsave(xas, flags) \
903 xa_lock_irqsave((xas)->xa, flags)
904#define xas_unlock_irqrestore(xas, flags) \
905 xa_unlock_irqrestore((xas)->xa, flags)
906
907/**
908 * xas_error() - Return an errno stored in the xa_state.
909 * @xas: XArray operation state.
910 *
911 * Return: 0 if no error has been noted. A negative errno if one has.
912 */
913static inline int xas_error(const struct xa_state *xas)
914{
915 return xa_err(xas->xa_node);
916}
917
918/**
919 * xas_set_err() - Note an error in the xa_state.
920 * @xas: XArray operation state.
921 * @err: Negative error number.
922 *
923 * Only call this function with a negative @err; zero or positive errors
924 * will probably not behave the way you think they should. If you want
925 * to clear the error from an xa_state, use xas_reset().
926 */
927static inline void xas_set_err(struct xa_state *xas, long err)
928{
929 xas->xa_node = XA_ERROR(err);
930}
931
932/**
933 * xas_invalid() - Is the xas in a retry or error state?
934 * @xas: XArray operation state.
935 *
936 * Return: %true if the xas cannot be used for operations.
937 */
938static inline bool xas_invalid(const struct xa_state *xas)
939{
940 return (unsigned long)xas->xa_node & 3;
941}
942
943/**
944 * xas_valid() - Is the xas a valid cursor into the array?
945 * @xas: XArray operation state.
946 *
947 * Return: %true if the xas can be used for operations.
948 */
949static inline bool xas_valid(const struct xa_state *xas)
950{
951 return !xas_invalid(xas);
952}
953
Matthew Wilcox2264f512017-12-04 00:11:48 -0500954/**
955 * xas_is_node() - Does the xas point to a node?
956 * @xas: XArray operation state.
957 *
958 * Return: %true if the xas currently references a node.
959 */
960static inline bool xas_is_node(const struct xa_state *xas)
961{
962 return xas_valid(xas) && xas->xa_node;
963}
964
Matthew Wilcox9b89a032017-11-10 09:34:31 -0500965/* True if the pointer is something other than a node */
966static inline bool xas_not_node(struct xa_node *node)
967{
968 return ((unsigned long)node & 3) || !node;
969}
970
Matthew Wilcox64d3e9a2017-12-01 00:06:52 -0500971/* True if the node represents RESTART or an error */
972static inline bool xas_frozen(struct xa_node *node)
973{
974 return (unsigned long)node & 2;
975}
976
Matthew Wilcox58d6ea32017-11-10 15:15:08 -0500977/* True if the node represents head-of-tree, RESTART or BOUNDS */
978static inline bool xas_top(struct xa_node *node)
979{
980 return node <= XAS_RESTART;
981}
982
Matthew Wilcoxad3d6c72017-11-07 14:57:46 -0500983/**
984 * xas_reset() - Reset an XArray operation state.
985 * @xas: XArray operation state.
986 *
987 * Resets the error or walk state of the @xas so future walks of the
988 * array will start from the root. Use this if you have dropped the
989 * xarray lock and want to reuse the xa_state.
990 *
991 * Context: Any context.
992 */
993static inline void xas_reset(struct xa_state *xas)
994{
995 xas->xa_node = XAS_RESTART;
996}
997
998/**
999 * xas_retry() - Retry the operation if appropriate.
1000 * @xas: XArray operation state.
1001 * @entry: Entry from xarray.
1002 *
1003 * The advanced functions may sometimes return an internal entry, such as
1004 * a retry entry or a zero entry. This function sets up the @xas to restart
1005 * the walk from the head of the array if needed.
1006 *
1007 * Context: Any context.
1008 * Return: true if the operation needs to be retried.
1009 */
1010static inline bool xas_retry(struct xa_state *xas, const void *entry)
1011{
Matthew Wilcox9f14d4f2018-10-01 14:54:59 -04001012 if (xa_is_zero(entry))
1013 return true;
Matthew Wilcoxad3d6c72017-11-07 14:57:46 -05001014 if (!xa_is_retry(entry))
1015 return false;
1016 xas_reset(xas);
1017 return true;
1018}
1019
1020void *xas_load(struct xa_state *);
Matthew Wilcox58d6ea32017-11-10 15:15:08 -05001021void *xas_store(struct xa_state *, void *entry);
Matthew Wilcoxb803b422017-11-14 08:30:11 -05001022void *xas_find(struct xa_state *, unsigned long max);
Matthew Wilcox4e99d4e2018-06-01 22:46:02 -04001023void *xas_find_conflict(struct xa_state *);
Matthew Wilcoxad3d6c72017-11-07 14:57:46 -05001024
Matthew Wilcox9b89a032017-11-10 09:34:31 -05001025bool xas_get_mark(const struct xa_state *, xa_mark_t);
1026void xas_set_mark(const struct xa_state *, xa_mark_t);
1027void xas_clear_mark(const struct xa_state *, xa_mark_t);
Matthew Wilcoxb803b422017-11-14 08:30:11 -05001028void *xas_find_marked(struct xa_state *, unsigned long max, xa_mark_t);
Matthew Wilcox58d6ea32017-11-10 15:15:08 -05001029void xas_init_marks(const struct xa_state *);
1030
1031bool xas_nomem(struct xa_state *, gfp_t);
Matthew Wilcoxb803b422017-11-14 08:30:11 -05001032void xas_pause(struct xa_state *);
Matthew Wilcox9b89a032017-11-10 09:34:31 -05001033
Matthew Wilcox2264f512017-12-04 00:11:48 -05001034void xas_create_range(struct xa_state *);
1035
Matthew Wilcoxad3d6c72017-11-07 14:57:46 -05001036/**
1037 * xas_reload() - Refetch an entry from the xarray.
1038 * @xas: XArray operation state.
1039 *
1040 * Use this function to check that a previously loaded entry still has
1041 * the same value. This is useful for the lockless pagecache lookup where
1042 * we walk the array with only the RCU lock to protect us, lock the page,
1043 * then check that the page hasn't moved since we looked it up.
1044 *
1045 * The caller guarantees that @xas is still valid. If it may be in an
1046 * error or restart state, call xas_load() instead.
1047 *
1048 * Return: The entry at this location in the xarray.
1049 */
1050static inline void *xas_reload(struct xa_state *xas)
1051{
1052 struct xa_node *node = xas->xa_node;
1053
1054 if (node)
1055 return xa_entry(xas->xa, node, xas->xa_offset);
1056 return xa_head(xas->xa);
1057}
1058
Matthew Wilcox58d6ea32017-11-10 15:15:08 -05001059/**
1060 * xas_set() - Set up XArray operation state for a different index.
1061 * @xas: XArray operation state.
1062 * @index: New index into the XArray.
1063 *
1064 * Move the operation state to refer to a different index. This will
1065 * have the effect of starting a walk from the top; see xas_next()
1066 * to move to an adjacent index.
1067 */
1068static inline void xas_set(struct xa_state *xas, unsigned long index)
1069{
1070 xas->xa_index = index;
1071 xas->xa_node = XAS_RESTART;
1072}
1073
1074/**
1075 * xas_set_order() - Set up XArray operation state for a multislot entry.
1076 * @xas: XArray operation state.
1077 * @index: Target of the operation.
1078 * @order: Entry occupies 2^@order indices.
1079 */
1080static inline void xas_set_order(struct xa_state *xas, unsigned long index,
1081 unsigned int order)
1082{
1083#ifdef CONFIG_XARRAY_MULTI
1084 xas->xa_index = order < BITS_PER_LONG ? (index >> order) << order : 0;
1085 xas->xa_shift = order - (order % XA_CHUNK_SHIFT);
1086 xas->xa_sibs = (1 << (order % XA_CHUNK_SHIFT)) - 1;
1087 xas->xa_node = XAS_RESTART;
1088#else
1089 BUG_ON(order > 0);
1090 xas_set(xas, index);
1091#endif
1092}
1093
1094/**
1095 * xas_set_update() - Set up XArray operation state for a callback.
1096 * @xas: XArray operation state.
1097 * @update: Function to call when updating a node.
1098 *
1099 * The XArray can notify a caller after it has updated an xa_node.
1100 * This is advanced functionality and is only needed by the page cache.
1101 */
1102static inline void xas_set_update(struct xa_state *xas, xa_update_node_t update)
1103{
1104 xas->xa_update = update;
1105}
1106
Matthew Wilcoxb803b422017-11-14 08:30:11 -05001107/**
1108 * xas_next_entry() - Advance iterator to next present entry.
1109 * @xas: XArray operation state.
1110 * @max: Highest index to return.
1111 *
1112 * xas_next_entry() is an inline function to optimise xarray traversal for
1113 * speed. It is equivalent to calling xas_find(), and will call xas_find()
1114 * for all the hard cases.
1115 *
1116 * Return: The next present entry after the one currently referred to by @xas.
1117 */
1118static inline void *xas_next_entry(struct xa_state *xas, unsigned long max)
1119{
1120 struct xa_node *node = xas->xa_node;
1121 void *entry;
1122
1123 if (unlikely(xas_not_node(node) || node->shift ||
1124 xas->xa_offset != (xas->xa_index & XA_CHUNK_MASK)))
1125 return xas_find(xas, max);
1126
1127 do {
1128 if (unlikely(xas->xa_index >= max))
1129 return xas_find(xas, max);
1130 if (unlikely(xas->xa_offset == XA_CHUNK_MASK))
1131 return xas_find(xas, max);
1132 entry = xa_entry(xas->xa, node, xas->xa_offset + 1);
1133 if (unlikely(xa_is_internal(entry)))
1134 return xas_find(xas, max);
1135 xas->xa_offset++;
1136 xas->xa_index++;
1137 } while (!entry);
1138
1139 return entry;
1140}
1141
1142/* Private */
1143static inline unsigned int xas_find_chunk(struct xa_state *xas, bool advance,
1144 xa_mark_t mark)
1145{
1146 unsigned long *addr = xas->xa_node->marks[(__force unsigned)mark];
1147 unsigned int offset = xas->xa_offset;
1148
1149 if (advance)
1150 offset++;
1151 if (XA_CHUNK_SIZE == BITS_PER_LONG) {
1152 if (offset < XA_CHUNK_SIZE) {
1153 unsigned long data = *addr & (~0UL << offset);
1154 if (data)
1155 return __ffs(data);
1156 }
1157 return XA_CHUNK_SIZE;
1158 }
1159
1160 return find_next_bit(addr, XA_CHUNK_SIZE, offset);
1161}
1162
1163/**
1164 * xas_next_marked() - Advance iterator to next marked entry.
1165 * @xas: XArray operation state.
1166 * @max: Highest index to return.
1167 * @mark: Mark to search for.
1168 *
1169 * xas_next_marked() is an inline function to optimise xarray traversal for
1170 * speed. It is equivalent to calling xas_find_marked(), and will call
1171 * xas_find_marked() for all the hard cases.
1172 *
1173 * Return: The next marked entry after the one currently referred to by @xas.
1174 */
1175static inline void *xas_next_marked(struct xa_state *xas, unsigned long max,
1176 xa_mark_t mark)
1177{
1178 struct xa_node *node = xas->xa_node;
1179 unsigned int offset;
1180
1181 if (unlikely(xas_not_node(node) || node->shift))
1182 return xas_find_marked(xas, max, mark);
1183 offset = xas_find_chunk(xas, true, mark);
1184 xas->xa_offset = offset;
1185 xas->xa_index = (xas->xa_index & ~XA_CHUNK_MASK) + offset;
1186 if (xas->xa_index > max)
1187 return NULL;
1188 if (offset == XA_CHUNK_SIZE)
1189 return xas_find_marked(xas, max, mark);
1190 return xa_entry(xas->xa, node, offset);
1191}
1192
1193/*
1194 * If iterating while holding a lock, drop the lock and reschedule
1195 * every %XA_CHECK_SCHED loops.
1196 */
1197enum {
1198 XA_CHECK_SCHED = 4096,
1199};
1200
1201/**
1202 * xas_for_each() - Iterate over a range of an XArray.
1203 * @xas: XArray operation state.
1204 * @entry: Entry retrieved from the array.
1205 * @max: Maximum index to retrieve from array.
1206 *
1207 * The loop body will be executed for each entry present in the xarray
1208 * between the current xas position and @max. @entry will be set to
1209 * the entry retrieved from the xarray. It is safe to delete entries
1210 * from the array in the loop body. You should hold either the RCU lock
1211 * or the xa_lock while iterating. If you need to drop the lock, call
1212 * xas_pause() first.
1213 */
1214#define xas_for_each(xas, entry, max) \
1215 for (entry = xas_find(xas, max); entry; \
1216 entry = xas_next_entry(xas, max))
1217
1218/**
1219 * xas_for_each_marked() - Iterate over a range of an XArray.
1220 * @xas: XArray operation state.
1221 * @entry: Entry retrieved from the array.
1222 * @max: Maximum index to retrieve from array.
1223 * @mark: Mark to search for.
1224 *
1225 * The loop body will be executed for each marked entry in the xarray
1226 * between the current xas position and @max. @entry will be set to
1227 * the entry retrieved from the xarray. It is safe to delete entries
1228 * from the array in the loop body. You should hold either the RCU lock
1229 * or the xa_lock while iterating. If you need to drop the lock, call
1230 * xas_pause() first.
1231 */
1232#define xas_for_each_marked(xas, entry, max, mark) \
1233 for (entry = xas_find_marked(xas, max, mark); entry; \
1234 entry = xas_next_marked(xas, max, mark))
1235
Matthew Wilcox4e99d4e2018-06-01 22:46:02 -04001236/**
1237 * xas_for_each_conflict() - Iterate over a range of an XArray.
1238 * @xas: XArray operation state.
1239 * @entry: Entry retrieved from the array.
1240 *
1241 * The loop body will be executed for each entry in the XArray that lies
1242 * within the range specified by @xas. If the loop completes successfully,
1243 * any entries that lie in this range will be replaced by @entry. The caller
1244 * may break out of the loop; if they do so, the contents of the XArray will
1245 * be unchanged. The operation may fail due to an out of memory condition.
1246 * The caller may also call xa_set_err() to exit the loop while setting an
1247 * error to record the reason.
1248 */
1249#define xas_for_each_conflict(xas, entry) \
1250 while ((entry = xas_find_conflict(xas)))
1251
Matthew Wilcox64d3e9a2017-12-01 00:06:52 -05001252void *__xas_next(struct xa_state *);
1253void *__xas_prev(struct xa_state *);
1254
1255/**
1256 * xas_prev() - Move iterator to previous index.
1257 * @xas: XArray operation state.
1258 *
1259 * If the @xas was in an error state, it will remain in an error state
1260 * and this function will return %NULL. If the @xas has never been walked,
1261 * it will have the effect of calling xas_load(). Otherwise one will be
1262 * subtracted from the index and the state will be walked to the correct
1263 * location in the array for the next operation.
1264 *
1265 * If the iterator was referencing index 0, this function wraps
1266 * around to %ULONG_MAX.
1267 *
1268 * Return: The entry at the new index. This may be %NULL or an internal
1269 * entry.
1270 */
1271static inline void *xas_prev(struct xa_state *xas)
1272{
1273 struct xa_node *node = xas->xa_node;
1274
1275 if (unlikely(xas_not_node(node) || node->shift ||
1276 xas->xa_offset == 0))
1277 return __xas_prev(xas);
1278
1279 xas->xa_index--;
1280 xas->xa_offset--;
1281 return xa_entry(xas->xa, node, xas->xa_offset);
1282}
1283
1284/**
1285 * xas_next() - Move state to next index.
1286 * @xas: XArray operation state.
1287 *
1288 * If the @xas was in an error state, it will remain in an error state
1289 * and this function will return %NULL. If the @xas has never been walked,
1290 * it will have the effect of calling xas_load(). Otherwise one will be
1291 * added to the index and the state will be walked to the correct
1292 * location in the array for the next operation.
1293 *
1294 * If the iterator was referencing index %ULONG_MAX, this function wraps
1295 * around to 0.
1296 *
1297 * Return: The entry at the new index. This may be %NULL or an internal
1298 * entry.
1299 */
1300static inline void *xas_next(struct xa_state *xas)
1301{
1302 struct xa_node *node = xas->xa_node;
1303
1304 if (unlikely(xas_not_node(node) || node->shift ||
1305 xas->xa_offset == XA_CHUNK_MASK))
1306 return __xas_next(xas);
1307
1308 xas->xa_index++;
1309 xas->xa_offset++;
1310 return xa_entry(xas->xa, node, xas->xa_offset);
1311}
1312
Matthew Wilcoxf6bb2a22018-04-10 16:36:52 -07001313#endif /* _LINUX_XARRAY_H */