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