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