blob: bed63d3cfea88658e38e7c1124cdb70731c128ae [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
208
Matthew Wilcox58d6ea32017-11-10 15:15:08 -0500209enum xa_lock_type {
210 XA_LOCK_IRQ = 1,
211 XA_LOCK_BH = 2,
212};
213
Matthew Wilcox9b89a032017-11-10 09:34:31 -0500214/*
215 * Values for xa_flags. The radix tree stores its GFP flags in the xa_flags,
216 * and we remain compatible with that.
217 */
Matthew Wilcox58d6ea32017-11-10 15:15:08 -0500218#define XA_FLAGS_LOCK_IRQ ((__force gfp_t)XA_LOCK_IRQ)
219#define XA_FLAGS_LOCK_BH ((__force gfp_t)XA_LOCK_BH)
Matthew Wilcox9b89a032017-11-10 09:34:31 -0500220#define XA_FLAGS_MARK(mark) ((__force gfp_t)((1U << __GFP_BITS_SHIFT) << \
221 (__force unsigned)(mark)))
222
Matthew Wilcoxad3d6c72017-11-07 14:57:46 -0500223/**
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -0500224 * struct xarray - The anchor of the XArray.
225 * @xa_lock: Lock that protects the contents of the XArray.
226 *
227 * To use the xarray, define it statically or embed it in your data structure.
228 * It is a very small data structure, so it does not usually make sense to
229 * allocate it separately and keep a pointer to it in your data structure.
230 *
231 * You may use the xa_lock to protect your own data structures as well.
232 */
233/*
234 * If all of the entries in the array are NULL, @xa_head is a NULL pointer.
235 * If the only non-NULL entry in the array is at index 0, @xa_head is that
236 * entry. If any other entry in the array is non-NULL, @xa_head points
237 * to an @xa_node.
238 */
239struct xarray {
240 spinlock_t xa_lock;
241/* private: The rest of the data structure is not to be used directly. */
242 gfp_t xa_flags;
243 void __rcu * xa_head;
244};
245
246#define XARRAY_INIT(name, flags) { \
247 .xa_lock = __SPIN_LOCK_UNLOCKED(name.xa_lock), \
248 .xa_flags = flags, \
249 .xa_head = NULL, \
250}
251
252/**
253 * DEFINE_XARRAY_FLAGS() - Define an XArray with custom flags.
254 * @name: A string that names your XArray.
255 * @flags: XA_FLAG values.
256 *
257 * This is intended for file scope definitions of XArrays. It declares
258 * and initialises an empty XArray with the chosen name and flags. It is
259 * equivalent to calling xa_init_flags() on the array, but it does the
260 * initialisation at compiletime instead of runtime.
261 */
262#define DEFINE_XARRAY_FLAGS(name, flags) \
263 struct xarray name = XARRAY_INIT(name, flags)
264
265/**
266 * DEFINE_XARRAY() - Define an XArray.
267 * @name: A string that names your XArray.
268 *
269 * This is intended for file scope definitions of XArrays. It declares
270 * and initialises an empty XArray with the chosen name. It is equivalent
271 * to calling xa_init() on the array, but it does the initialisation at
272 * compiletime instead of runtime.
273 */
274#define DEFINE_XARRAY(name) DEFINE_XARRAY_FLAGS(name, 0)
275
276void xa_init_flags(struct xarray *, gfp_t flags);
Matthew Wilcoxad3d6c72017-11-07 14:57:46 -0500277void *xa_load(struct xarray *, unsigned long index);
Matthew Wilcox58d6ea32017-11-10 15:15:08 -0500278void *xa_store(struct xarray *, unsigned long index, void *entry, gfp_t);
Matthew Wilcox41aec912017-11-10 15:34:55 -0500279void *xa_cmpxchg(struct xarray *, unsigned long index,
280 void *old, void *entry, gfp_t);
Matthew Wilcox9f14d4f2018-10-01 14:54:59 -0400281int xa_reserve(struct xarray *, unsigned long index, gfp_t);
Matthew Wilcox9b89a032017-11-10 09:34:31 -0500282bool xa_get_mark(struct xarray *, unsigned long index, xa_mark_t);
283void xa_set_mark(struct xarray *, unsigned long index, xa_mark_t);
284void xa_clear_mark(struct xarray *, unsigned long index, xa_mark_t);
Matthew Wilcoxb803b422017-11-14 08:30:11 -0500285void *xa_find(struct xarray *xa, unsigned long *index,
286 unsigned long max, xa_mark_t) __attribute__((nonnull(2)));
287void *xa_find_after(struct xarray *xa, unsigned long *index,
288 unsigned long max, xa_mark_t) __attribute__((nonnull(2)));
Matthew Wilcox80a0a1a2017-11-14 16:42:22 -0500289unsigned int xa_extract(struct xarray *, void **dst, unsigned long start,
290 unsigned long max, unsigned int n, xa_mark_t);
Matthew Wilcox687149f2017-11-17 08:16:34 -0500291void xa_destroy(struct xarray *);
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -0500292
293/**
294 * xa_init() - Initialise an empty XArray.
295 * @xa: XArray.
296 *
297 * An empty XArray is full of NULL entries.
298 *
299 * Context: Any context.
300 */
301static inline void xa_init(struct xarray *xa)
302{
303 xa_init_flags(xa, 0);
304}
305
Matthew Wilcoxad3d6c72017-11-07 14:57:46 -0500306/**
307 * xa_empty() - Determine if an array has any present entries.
308 * @xa: XArray.
309 *
310 * Context: Any context.
311 * Return: %true if the array contains only NULL pointers.
312 */
313static inline bool xa_empty(const struct xarray *xa)
314{
315 return xa->xa_head == NULL;
316}
317
Matthew Wilcox9b89a032017-11-10 09:34:31 -0500318/**
319 * xa_marked() - Inquire whether any entry in this array has a mark set
320 * @xa: Array
321 * @mark: Mark value
322 *
323 * Context: Any context.
324 * Return: %true if any entry has this mark set.
325 */
326static inline bool xa_marked(const struct xarray *xa, xa_mark_t mark)
327{
328 return xa->xa_flags & XA_FLAGS_MARK(mark);
329}
330
Matthew Wilcox58d6ea32017-11-10 15:15:08 -0500331/**
332 * xa_erase() - Erase this entry from the XArray.
333 * @xa: XArray.
334 * @index: Index of entry.
335 *
336 * This function is the equivalent of calling xa_store() with %NULL as
337 * the third argument. The XArray does not need to allocate memory, so
338 * the user does not need to provide GFP flags.
339 *
340 * Context: Process context. Takes and releases the xa_lock.
341 * Return: The entry which used to be at this index.
342 */
343static inline void *xa_erase(struct xarray *xa, unsigned long index)
344{
345 return xa_store(xa, index, NULL, 0);
346}
347
Matthew Wilcox41aec912017-11-10 15:34:55 -0500348/**
349 * xa_insert() - Store this entry in the XArray unless another entry is
350 * already present.
351 * @xa: XArray.
352 * @index: Index into array.
353 * @entry: New entry.
354 * @gfp: Memory allocation flags.
355 *
356 * If you would rather see the existing entry in the array, use xa_cmpxchg().
357 * This function is for users who don't care what the entry is, only that
358 * one is present.
359 *
360 * Context: Process context. Takes and releases the xa_lock.
361 * May sleep if the @gfp flags permit.
362 * Return: 0 if the store succeeded. -EEXIST if another entry was present.
363 * -ENOMEM if memory could not be allocated.
364 */
365static inline int xa_insert(struct xarray *xa, unsigned long index,
366 void *entry, gfp_t gfp)
367{
368 void *curr = xa_cmpxchg(xa, index, NULL, entry, gfp);
369 if (!curr)
370 return 0;
371 if (xa_is_err(curr))
372 return xa_err(curr);
373 return -EEXIST;
374}
375
Matthew Wilcoxb803b422017-11-14 08:30:11 -0500376/**
Matthew Wilcox9f14d4f2018-10-01 14:54:59 -0400377 * xa_release() - Release a reserved entry.
378 * @xa: XArray.
379 * @index: Index of entry.
380 *
381 * After calling xa_reserve(), you can call this function to release the
382 * reservation. If the entry at @index has been stored to, this function
383 * will do nothing.
384 */
385static inline void xa_release(struct xarray *xa, unsigned long index)
386{
387 xa_cmpxchg(xa, index, NULL, NULL, 0);
388}
389
390/**
Matthew Wilcoxb803b422017-11-14 08:30:11 -0500391 * xa_for_each() - Iterate over a portion of an XArray.
392 * @xa: XArray.
393 * @entry: Entry retrieved from array.
394 * @index: Index of @entry.
395 * @max: Maximum index to retrieve from array.
396 * @filter: Selection criterion.
397 *
398 * Initialise @index to the lowest index you want to retrieve from the
399 * array. During the iteration, @entry will have the value of the entry
400 * stored in @xa at @index. The iteration will skip all entries in the
401 * array which do not match @filter. You may modify @index during the
402 * iteration if you want to skip or reprocess indices. It is safe to modify
403 * the array during the iteration. At the end of the iteration, @entry will
404 * be set to NULL and @index will have a value less than or equal to max.
405 *
406 * xa_for_each() is O(n.log(n)) while xas_for_each() is O(n). You have
407 * to handle your own locking with xas_for_each(), and if you have to unlock
408 * after each iteration, it will also end up being O(n.log(n)). xa_for_each()
409 * will spin if it hits a retry entry; if you intend to see retry entries,
410 * you should use the xas_for_each() iterator instead. The xas_for_each()
411 * iterator will expand into more inline code than xa_for_each().
412 *
413 * Context: Any context. Takes and releases the RCU lock.
414 */
415#define xa_for_each(xa, entry, index, max, filter) \
416 for (entry = xa_find(xa, &index, max, filter); entry; \
417 entry = xa_find_after(xa, &index, max, filter))
418
Matthew Wilcoxf6bb2a22018-04-10 16:36:52 -0700419#define xa_trylock(xa) spin_trylock(&(xa)->xa_lock)
420#define xa_lock(xa) spin_lock(&(xa)->xa_lock)
421#define xa_unlock(xa) spin_unlock(&(xa)->xa_lock)
422#define xa_lock_bh(xa) spin_lock_bh(&(xa)->xa_lock)
423#define xa_unlock_bh(xa) spin_unlock_bh(&(xa)->xa_lock)
424#define xa_lock_irq(xa) spin_lock_irq(&(xa)->xa_lock)
425#define xa_unlock_irq(xa) spin_unlock_irq(&(xa)->xa_lock)
426#define xa_lock_irqsave(xa, flags) \
427 spin_lock_irqsave(&(xa)->xa_lock, flags)
428#define xa_unlock_irqrestore(xa, flags) \
429 spin_unlock_irqrestore(&(xa)->xa_lock, flags)
430
Matthew Wilcox9b89a032017-11-10 09:34:31 -0500431/*
Matthew Wilcox58d6ea32017-11-10 15:15:08 -0500432 * Versions of the normal API which require the caller to hold the
433 * xa_lock. If the GFP flags allow it, they will drop the lock to
434 * allocate memory, then reacquire it afterwards. These functions
435 * may also re-enable interrupts if the XArray flags indicate the
436 * locking should be interrupt safe.
Matthew Wilcox9b89a032017-11-10 09:34:31 -0500437 */
Matthew Wilcox58d6ea32017-11-10 15:15:08 -0500438void *__xa_erase(struct xarray *, unsigned long index);
439void *__xa_store(struct xarray *, unsigned long index, void *entry, gfp_t);
Matthew Wilcox41aec912017-11-10 15:34:55 -0500440void *__xa_cmpxchg(struct xarray *, unsigned long index, void *old,
441 void *entry, gfp_t);
Matthew Wilcox9b89a032017-11-10 09:34:31 -0500442void __xa_set_mark(struct xarray *, unsigned long index, xa_mark_t);
443void __xa_clear_mark(struct xarray *, unsigned long index, xa_mark_t);
444
Matthew Wilcox58d6ea32017-11-10 15:15:08 -0500445/**
Matthew Wilcox41aec912017-11-10 15:34:55 -0500446 * __xa_insert() - Store this entry in the XArray unless another entry is
447 * already present.
448 * @xa: XArray.
449 * @index: Index into array.
450 * @entry: New entry.
451 * @gfp: Memory allocation flags.
452 *
453 * If you would rather see the existing entry in the array, use __xa_cmpxchg().
454 * This function is for users who don't care what the entry is, only that
455 * one is present.
456 *
457 * Context: Any context. Expects xa_lock to be held on entry. May
458 * release and reacquire xa_lock if the @gfp flags permit.
459 * Return: 0 if the store succeeded. -EEXIST if another entry was present.
460 * -ENOMEM if memory could not be allocated.
461 */
462static inline int __xa_insert(struct xarray *xa, unsigned long index,
463 void *entry, gfp_t gfp)
464{
465 void *curr = __xa_cmpxchg(xa, index, NULL, entry, gfp);
466 if (!curr)
467 return 0;
468 if (xa_is_err(curr))
469 return xa_err(curr);
470 return -EEXIST;
471}
472
473/**
Matthew Wilcox58d6ea32017-11-10 15:15:08 -0500474 * xa_erase_bh() - Erase this entry from the XArray.
475 * @xa: XArray.
476 * @index: Index of entry.
477 *
478 * This function is the equivalent of calling xa_store() with %NULL as
479 * the third argument. The XArray does not need to allocate memory, so
480 * the user does not need to provide GFP flags.
481 *
482 * Context: Process context. Takes and releases the xa_lock while
483 * disabling softirqs.
484 * Return: The entry which used to be at this index.
485 */
486static inline void *xa_erase_bh(struct xarray *xa, unsigned long index)
487{
488 void *entry;
489
490 xa_lock_bh(xa);
491 entry = __xa_erase(xa, index);
492 xa_unlock_bh(xa);
493
494 return entry;
495}
496
497/**
498 * xa_erase_irq() - Erase this entry from the XArray.
499 * @xa: XArray.
500 * @index: Index of entry.
501 *
502 * This function is the equivalent of calling xa_store() with %NULL as
503 * the third argument. The XArray does not need to allocate memory, so
504 * the user does not need to provide GFP flags.
505 *
506 * Context: Process context. Takes and releases the xa_lock while
507 * disabling interrupts.
508 * Return: The entry which used to be at this index.
509 */
510static inline void *xa_erase_irq(struct xarray *xa, unsigned long index)
511{
512 void *entry;
513
514 xa_lock_irq(xa);
515 entry = __xa_erase(xa, index);
516 xa_unlock_irq(xa);
517
518 return entry;
519}
520
Matthew Wilcox02c02bf2017-11-03 23:09:45 -0400521/* Everything below here is the Advanced API. Proceed with caution. */
522
523/*
524 * The xarray is constructed out of a set of 'chunks' of pointers. Choosing
525 * the best chunk size requires some tradeoffs. A power of two recommends
526 * itself so that we can walk the tree based purely on shifts and masks.
527 * Generally, the larger the better; as the number of slots per level of the
528 * tree increases, the less tall the tree needs to be. But that needs to be
529 * balanced against the memory consumption of each node. On a 64-bit system,
530 * xa_node is currently 576 bytes, and we get 7 of them per 4kB page. If we
531 * doubled the number of slots per node, we'd get only 3 nodes per 4kB page.
532 */
533#ifndef XA_CHUNK_SHIFT
534#define XA_CHUNK_SHIFT (CONFIG_BASE_SMALL ? 4 : 6)
535#endif
536#define XA_CHUNK_SIZE (1UL << XA_CHUNK_SHIFT)
537#define XA_CHUNK_MASK (XA_CHUNK_SIZE - 1)
Matthew Wilcox01959df2017-11-09 09:23:56 -0500538#define XA_MAX_MARKS 3
539#define XA_MARK_LONGS DIV_ROUND_UP(XA_CHUNK_SIZE, BITS_PER_LONG)
540
541/*
542 * @count is the count of every non-NULL element in the ->slots array
543 * whether that is a value entry, a retry entry, a user pointer,
544 * a sibling entry or a pointer to the next level of the tree.
545 * @nr_values is the count of every element in ->slots which is
546 * either a value entry or a sibling of a value entry.
547 */
548struct xa_node {
549 unsigned char shift; /* Bits remaining in each slot */
550 unsigned char offset; /* Slot offset in parent */
551 unsigned char count; /* Total entry count */
552 unsigned char nr_values; /* Value entry count */
553 struct xa_node __rcu *parent; /* NULL at top of tree */
554 struct xarray *array; /* The array we belong to */
555 union {
556 struct list_head private_list; /* For tree user */
557 struct rcu_head rcu_head; /* Used when freeing node */
558 };
559 void __rcu *slots[XA_CHUNK_SIZE];
560 union {
561 unsigned long tags[XA_MAX_MARKS][XA_MARK_LONGS];
562 unsigned long marks[XA_MAX_MARKS][XA_MARK_LONGS];
563 };
564};
Matthew Wilcox02c02bf2017-11-03 23:09:45 -0400565
Matthew Wilcoxad3d6c72017-11-07 14:57:46 -0500566void xa_dump(const struct xarray *);
567void xa_dump_node(const struct xa_node *);
568
569#ifdef XA_DEBUG
570#define XA_BUG_ON(xa, x) do { \
571 if (x) { \
572 xa_dump(xa); \
573 BUG(); \
574 } \
575 } while (0)
576#define XA_NODE_BUG_ON(node, x) do { \
577 if (x) { \
578 if (node) xa_dump_node(node); \
579 BUG(); \
580 } \
581 } while (0)
582#else
583#define XA_BUG_ON(xa, x) do { } while (0)
584#define XA_NODE_BUG_ON(node, x) do { } while (0)
585#endif
586
587/* Private */
588static inline void *xa_head(const struct xarray *xa)
589{
590 return rcu_dereference_check(xa->xa_head,
591 lockdep_is_held(&xa->xa_lock));
592}
593
594/* Private */
595static inline void *xa_head_locked(const struct xarray *xa)
596{
597 return rcu_dereference_protected(xa->xa_head,
598 lockdep_is_held(&xa->xa_lock));
599}
600
601/* Private */
602static inline void *xa_entry(const struct xarray *xa,
603 const struct xa_node *node, unsigned int offset)
604{
605 XA_NODE_BUG_ON(node, offset >= XA_CHUNK_SIZE);
606 return rcu_dereference_check(node->slots[offset],
607 lockdep_is_held(&xa->xa_lock));
608}
609
610/* Private */
611static inline void *xa_entry_locked(const struct xarray *xa,
612 const struct xa_node *node, unsigned int offset)
613{
614 XA_NODE_BUG_ON(node, offset >= XA_CHUNK_SIZE);
615 return rcu_dereference_protected(node->slots[offset],
616 lockdep_is_held(&xa->xa_lock));
617}
618
619/* Private */
Matthew Wilcox9b89a032017-11-10 09:34:31 -0500620static inline struct xa_node *xa_parent(const struct xarray *xa,
621 const struct xa_node *node)
622{
623 return rcu_dereference_check(node->parent,
624 lockdep_is_held(&xa->xa_lock));
625}
626
627/* Private */
628static inline struct xa_node *xa_parent_locked(const struct xarray *xa,
629 const struct xa_node *node)
630{
631 return rcu_dereference_protected(node->parent,
632 lockdep_is_held(&xa->xa_lock));
633}
634
635/* Private */
Matthew Wilcox58d6ea32017-11-10 15:15:08 -0500636static inline void *xa_mk_node(const struct xa_node *node)
637{
638 return (void *)((unsigned long)node | 2);
639}
640
641/* Private */
Matthew Wilcoxad3d6c72017-11-07 14:57:46 -0500642static inline struct xa_node *xa_to_node(const void *entry)
643{
644 return (struct xa_node *)((unsigned long)entry - 2);
645}
646
Matthew Wilcox02c02bf2017-11-03 23:09:45 -0400647/* Private */
648static inline bool xa_is_node(const void *entry)
649{
650 return xa_is_internal(entry) && (unsigned long)entry > 4096;
651}
652
653/* Private */
654static inline void *xa_mk_sibling(unsigned int offset)
655{
656 return xa_mk_internal(offset);
657}
658
659/* Private */
660static inline unsigned long xa_to_sibling(const void *entry)
661{
662 return xa_to_internal(entry);
663}
664
665/**
666 * xa_is_sibling() - Is the entry a sibling entry?
667 * @entry: Entry retrieved from the XArray
668 *
669 * Return: %true if the entry is a sibling entry.
670 */
671static inline bool xa_is_sibling(const void *entry)
672{
673 return IS_ENABLED(CONFIG_XARRAY_MULTI) && xa_is_internal(entry) &&
674 (entry < xa_mk_sibling(XA_CHUNK_SIZE - 1));
675}
676
Matthew Wilcox9f14d4f2018-10-01 14:54:59 -0400677#define XA_ZERO_ENTRY xa_mk_internal(256)
678#define XA_RETRY_ENTRY xa_mk_internal(257)
679
680/**
681 * xa_is_zero() - Is the entry a zero entry?
682 * @entry: Entry retrieved from the XArray
683 *
684 * Return: %true if the entry is a zero entry.
685 */
686static inline bool xa_is_zero(const void *entry)
687{
688 return unlikely(entry == XA_ZERO_ENTRY);
689}
Matthew Wilcox02c02bf2017-11-03 23:09:45 -0400690
Matthew Wilcoxad3d6c72017-11-07 14:57:46 -0500691/**
692 * xa_is_retry() - Is the entry a retry entry?
693 * @entry: Entry retrieved from the XArray
694 *
695 * Return: %true if the entry is a retry entry.
696 */
697static inline bool xa_is_retry(const void *entry)
698{
699 return unlikely(entry == XA_RETRY_ENTRY);
700}
701
702/**
703 * typedef xa_update_node_t - A callback function from the XArray.
704 * @node: The node which is being processed
705 *
706 * This function is called every time the XArray updates the count of
707 * present and value entries in a node. It allows advanced users to
708 * maintain the private_list in the node.
709 *
710 * Context: The xa_lock is held and interrupts may be disabled.
711 * Implementations should not drop the xa_lock, nor re-enable
712 * interrupts.
713 */
714typedef void (*xa_update_node_t)(struct xa_node *node);
715
716/*
717 * The xa_state is opaque to its users. It contains various different pieces
718 * of state involved in the current operation on the XArray. It should be
719 * declared on the stack and passed between the various internal routines.
720 * The various elements in it should not be accessed directly, but only
721 * through the provided accessor functions. The below documentation is for
722 * the benefit of those working on the code, not for users of the XArray.
723 *
724 * @xa_node usually points to the xa_node containing the slot we're operating
725 * on (and @xa_offset is the offset in the slots array). If there is a
726 * single entry in the array at index 0, there are no allocated xa_nodes to
727 * point to, and so we store %NULL in @xa_node. @xa_node is set to
728 * the value %XAS_RESTART if the xa_state is not walked to the correct
729 * position in the tree of nodes for this operation. If an error occurs
730 * during an operation, it is set to an %XAS_ERROR value. If we run off the
731 * end of the allocated nodes, it is set to %XAS_BOUNDS.
732 */
733struct xa_state {
734 struct xarray *xa;
735 unsigned long xa_index;
736 unsigned char xa_shift;
737 unsigned char xa_sibs;
738 unsigned char xa_offset;
739 unsigned char xa_pad; /* Helps gcc generate better code */
740 struct xa_node *xa_node;
741 struct xa_node *xa_alloc;
742 xa_update_node_t xa_update;
743};
744
745/*
746 * We encode errnos in the xas->xa_node. If an error has happened, we need to
747 * drop the lock to fix it, and once we've done so the xa_state is invalid.
748 */
749#define XA_ERROR(errno) ((struct xa_node *)(((unsigned long)errno << 2) | 2UL))
750#define XAS_BOUNDS ((struct xa_node *)1UL)
751#define XAS_RESTART ((struct xa_node *)3UL)
752
753#define __XA_STATE(array, index, shift, sibs) { \
754 .xa = array, \
755 .xa_index = index, \
756 .xa_shift = shift, \
757 .xa_sibs = sibs, \
758 .xa_offset = 0, \
759 .xa_pad = 0, \
760 .xa_node = XAS_RESTART, \
761 .xa_alloc = NULL, \
762 .xa_update = NULL \
763}
764
765/**
766 * XA_STATE() - Declare an XArray operation state.
767 * @name: Name of this operation state (usually xas).
768 * @array: Array to operate on.
769 * @index: Initial index of interest.
770 *
771 * Declare and initialise an xa_state on the stack.
772 */
773#define XA_STATE(name, array, index) \
774 struct xa_state name = __XA_STATE(array, index, 0, 0)
775
776/**
777 * XA_STATE_ORDER() - Declare an XArray operation state.
778 * @name: Name of this operation state (usually xas).
779 * @array: Array to operate on.
780 * @index: Initial index of interest.
781 * @order: Order of entry.
782 *
783 * Declare and initialise an xa_state on the stack. This variant of
784 * XA_STATE() allows you to specify the 'order' of the element you
785 * want to operate on.`
786 */
787#define XA_STATE_ORDER(name, array, index, order) \
788 struct xa_state name = __XA_STATE(array, \
789 (index >> order) << order, \
790 order - (order % XA_CHUNK_SHIFT), \
791 (1U << (order % XA_CHUNK_SHIFT)) - 1)
792
793#define xas_marked(xas, mark) xa_marked((xas)->xa, (mark))
794#define xas_trylock(xas) xa_trylock((xas)->xa)
795#define xas_lock(xas) xa_lock((xas)->xa)
796#define xas_unlock(xas) xa_unlock((xas)->xa)
797#define xas_lock_bh(xas) xa_lock_bh((xas)->xa)
798#define xas_unlock_bh(xas) xa_unlock_bh((xas)->xa)
799#define xas_lock_irq(xas) xa_lock_irq((xas)->xa)
800#define xas_unlock_irq(xas) xa_unlock_irq((xas)->xa)
801#define xas_lock_irqsave(xas, flags) \
802 xa_lock_irqsave((xas)->xa, flags)
803#define xas_unlock_irqrestore(xas, flags) \
804 xa_unlock_irqrestore((xas)->xa, flags)
805
806/**
807 * xas_error() - Return an errno stored in the xa_state.
808 * @xas: XArray operation state.
809 *
810 * Return: 0 if no error has been noted. A negative errno if one has.
811 */
812static inline int xas_error(const struct xa_state *xas)
813{
814 return xa_err(xas->xa_node);
815}
816
817/**
818 * xas_set_err() - Note an error in the xa_state.
819 * @xas: XArray operation state.
820 * @err: Negative error number.
821 *
822 * Only call this function with a negative @err; zero or positive errors
823 * will probably not behave the way you think they should. If you want
824 * to clear the error from an xa_state, use xas_reset().
825 */
826static inline void xas_set_err(struct xa_state *xas, long err)
827{
828 xas->xa_node = XA_ERROR(err);
829}
830
831/**
832 * xas_invalid() - Is the xas in a retry or error state?
833 * @xas: XArray operation state.
834 *
835 * Return: %true if the xas cannot be used for operations.
836 */
837static inline bool xas_invalid(const struct xa_state *xas)
838{
839 return (unsigned long)xas->xa_node & 3;
840}
841
842/**
843 * xas_valid() - Is the xas a valid cursor into the array?
844 * @xas: XArray operation state.
845 *
846 * Return: %true if the xas can be used for operations.
847 */
848static inline bool xas_valid(const struct xa_state *xas)
849{
850 return !xas_invalid(xas);
851}
852
Matthew Wilcox2264f512017-12-04 00:11:48 -0500853/**
854 * xas_is_node() - Does the xas point to a node?
855 * @xas: XArray operation state.
856 *
857 * Return: %true if the xas currently references a node.
858 */
859static inline bool xas_is_node(const struct xa_state *xas)
860{
861 return xas_valid(xas) && xas->xa_node;
862}
863
Matthew Wilcox9b89a032017-11-10 09:34:31 -0500864/* True if the pointer is something other than a node */
865static inline bool xas_not_node(struct xa_node *node)
866{
867 return ((unsigned long)node & 3) || !node;
868}
869
Matthew Wilcox64d3e9a2017-12-01 00:06:52 -0500870/* True if the node represents RESTART or an error */
871static inline bool xas_frozen(struct xa_node *node)
872{
873 return (unsigned long)node & 2;
874}
875
Matthew Wilcox58d6ea32017-11-10 15:15:08 -0500876/* True if the node represents head-of-tree, RESTART or BOUNDS */
877static inline bool xas_top(struct xa_node *node)
878{
879 return node <= XAS_RESTART;
880}
881
Matthew Wilcoxad3d6c72017-11-07 14:57:46 -0500882/**
883 * xas_reset() - Reset an XArray operation state.
884 * @xas: XArray operation state.
885 *
886 * Resets the error or walk state of the @xas so future walks of the
887 * array will start from the root. Use this if you have dropped the
888 * xarray lock and want to reuse the xa_state.
889 *
890 * Context: Any context.
891 */
892static inline void xas_reset(struct xa_state *xas)
893{
894 xas->xa_node = XAS_RESTART;
895}
896
897/**
898 * xas_retry() - Retry the operation if appropriate.
899 * @xas: XArray operation state.
900 * @entry: Entry from xarray.
901 *
902 * The advanced functions may sometimes return an internal entry, such as
903 * a retry entry or a zero entry. This function sets up the @xas to restart
904 * the walk from the head of the array if needed.
905 *
906 * Context: Any context.
907 * Return: true if the operation needs to be retried.
908 */
909static inline bool xas_retry(struct xa_state *xas, const void *entry)
910{
Matthew Wilcox9f14d4f2018-10-01 14:54:59 -0400911 if (xa_is_zero(entry))
912 return true;
Matthew Wilcoxad3d6c72017-11-07 14:57:46 -0500913 if (!xa_is_retry(entry))
914 return false;
915 xas_reset(xas);
916 return true;
917}
918
919void *xas_load(struct xa_state *);
Matthew Wilcox58d6ea32017-11-10 15:15:08 -0500920void *xas_store(struct xa_state *, void *entry);
Matthew Wilcoxb803b422017-11-14 08:30:11 -0500921void *xas_find(struct xa_state *, unsigned long max);
Matthew Wilcox4e99d4e2018-06-01 22:46:02 -0400922void *xas_find_conflict(struct xa_state *);
Matthew Wilcoxad3d6c72017-11-07 14:57:46 -0500923
Matthew Wilcox9b89a032017-11-10 09:34:31 -0500924bool xas_get_mark(const struct xa_state *, xa_mark_t);
925void xas_set_mark(const struct xa_state *, xa_mark_t);
926void xas_clear_mark(const struct xa_state *, xa_mark_t);
Matthew Wilcoxb803b422017-11-14 08:30:11 -0500927void *xas_find_marked(struct xa_state *, unsigned long max, xa_mark_t);
Matthew Wilcox58d6ea32017-11-10 15:15:08 -0500928void xas_init_marks(const struct xa_state *);
929
930bool xas_nomem(struct xa_state *, gfp_t);
Matthew Wilcoxb803b422017-11-14 08:30:11 -0500931void xas_pause(struct xa_state *);
Matthew Wilcox9b89a032017-11-10 09:34:31 -0500932
Matthew Wilcox2264f512017-12-04 00:11:48 -0500933void xas_create_range(struct xa_state *);
934
Matthew Wilcoxad3d6c72017-11-07 14:57:46 -0500935/**
936 * xas_reload() - Refetch an entry from the xarray.
937 * @xas: XArray operation state.
938 *
939 * Use this function to check that a previously loaded entry still has
940 * the same value. This is useful for the lockless pagecache lookup where
941 * we walk the array with only the RCU lock to protect us, lock the page,
942 * then check that the page hasn't moved since we looked it up.
943 *
944 * The caller guarantees that @xas is still valid. If it may be in an
945 * error or restart state, call xas_load() instead.
946 *
947 * Return: The entry at this location in the xarray.
948 */
949static inline void *xas_reload(struct xa_state *xas)
950{
951 struct xa_node *node = xas->xa_node;
952
953 if (node)
954 return xa_entry(xas->xa, node, xas->xa_offset);
955 return xa_head(xas->xa);
956}
957
Matthew Wilcox58d6ea32017-11-10 15:15:08 -0500958/**
959 * xas_set() - Set up XArray operation state for a different index.
960 * @xas: XArray operation state.
961 * @index: New index into the XArray.
962 *
963 * Move the operation state to refer to a different index. This will
964 * have the effect of starting a walk from the top; see xas_next()
965 * to move to an adjacent index.
966 */
967static inline void xas_set(struct xa_state *xas, unsigned long index)
968{
969 xas->xa_index = index;
970 xas->xa_node = XAS_RESTART;
971}
972
973/**
974 * xas_set_order() - Set up XArray operation state for a multislot entry.
975 * @xas: XArray operation state.
976 * @index: Target of the operation.
977 * @order: Entry occupies 2^@order indices.
978 */
979static inline void xas_set_order(struct xa_state *xas, unsigned long index,
980 unsigned int order)
981{
982#ifdef CONFIG_XARRAY_MULTI
983 xas->xa_index = order < BITS_PER_LONG ? (index >> order) << order : 0;
984 xas->xa_shift = order - (order % XA_CHUNK_SHIFT);
985 xas->xa_sibs = (1 << (order % XA_CHUNK_SHIFT)) - 1;
986 xas->xa_node = XAS_RESTART;
987#else
988 BUG_ON(order > 0);
989 xas_set(xas, index);
990#endif
991}
992
993/**
994 * xas_set_update() - Set up XArray operation state for a callback.
995 * @xas: XArray operation state.
996 * @update: Function to call when updating a node.
997 *
998 * The XArray can notify a caller after it has updated an xa_node.
999 * This is advanced functionality and is only needed by the page cache.
1000 */
1001static inline void xas_set_update(struct xa_state *xas, xa_update_node_t update)
1002{
1003 xas->xa_update = update;
1004}
1005
Matthew Wilcoxb803b422017-11-14 08:30:11 -05001006/**
1007 * xas_next_entry() - Advance iterator to next present entry.
1008 * @xas: XArray operation state.
1009 * @max: Highest index to return.
1010 *
1011 * xas_next_entry() is an inline function to optimise xarray traversal for
1012 * speed. It is equivalent to calling xas_find(), and will call xas_find()
1013 * for all the hard cases.
1014 *
1015 * Return: The next present entry after the one currently referred to by @xas.
1016 */
1017static inline void *xas_next_entry(struct xa_state *xas, unsigned long max)
1018{
1019 struct xa_node *node = xas->xa_node;
1020 void *entry;
1021
1022 if (unlikely(xas_not_node(node) || node->shift ||
1023 xas->xa_offset != (xas->xa_index & XA_CHUNK_MASK)))
1024 return xas_find(xas, max);
1025
1026 do {
1027 if (unlikely(xas->xa_index >= max))
1028 return xas_find(xas, max);
1029 if (unlikely(xas->xa_offset == XA_CHUNK_MASK))
1030 return xas_find(xas, max);
1031 entry = xa_entry(xas->xa, node, xas->xa_offset + 1);
1032 if (unlikely(xa_is_internal(entry)))
1033 return xas_find(xas, max);
1034 xas->xa_offset++;
1035 xas->xa_index++;
1036 } while (!entry);
1037
1038 return entry;
1039}
1040
1041/* Private */
1042static inline unsigned int xas_find_chunk(struct xa_state *xas, bool advance,
1043 xa_mark_t mark)
1044{
1045 unsigned long *addr = xas->xa_node->marks[(__force unsigned)mark];
1046 unsigned int offset = xas->xa_offset;
1047
1048 if (advance)
1049 offset++;
1050 if (XA_CHUNK_SIZE == BITS_PER_LONG) {
1051 if (offset < XA_CHUNK_SIZE) {
1052 unsigned long data = *addr & (~0UL << offset);
1053 if (data)
1054 return __ffs(data);
1055 }
1056 return XA_CHUNK_SIZE;
1057 }
1058
1059 return find_next_bit(addr, XA_CHUNK_SIZE, offset);
1060}
1061
1062/**
1063 * xas_next_marked() - Advance iterator to next marked entry.
1064 * @xas: XArray operation state.
1065 * @max: Highest index to return.
1066 * @mark: Mark to search for.
1067 *
1068 * xas_next_marked() is an inline function to optimise xarray traversal for
1069 * speed. It is equivalent to calling xas_find_marked(), and will call
1070 * xas_find_marked() for all the hard cases.
1071 *
1072 * Return: The next marked entry after the one currently referred to by @xas.
1073 */
1074static inline void *xas_next_marked(struct xa_state *xas, unsigned long max,
1075 xa_mark_t mark)
1076{
1077 struct xa_node *node = xas->xa_node;
1078 unsigned int offset;
1079
1080 if (unlikely(xas_not_node(node) || node->shift))
1081 return xas_find_marked(xas, max, mark);
1082 offset = xas_find_chunk(xas, true, mark);
1083 xas->xa_offset = offset;
1084 xas->xa_index = (xas->xa_index & ~XA_CHUNK_MASK) + offset;
1085 if (xas->xa_index > max)
1086 return NULL;
1087 if (offset == XA_CHUNK_SIZE)
1088 return xas_find_marked(xas, max, mark);
1089 return xa_entry(xas->xa, node, offset);
1090}
1091
1092/*
1093 * If iterating while holding a lock, drop the lock and reschedule
1094 * every %XA_CHECK_SCHED loops.
1095 */
1096enum {
1097 XA_CHECK_SCHED = 4096,
1098};
1099
1100/**
1101 * xas_for_each() - Iterate over a range of an XArray.
1102 * @xas: XArray operation state.
1103 * @entry: Entry retrieved from the array.
1104 * @max: Maximum index to retrieve from array.
1105 *
1106 * The loop body will be executed for each entry present in the xarray
1107 * between the current xas position and @max. @entry will be set to
1108 * the entry retrieved from the xarray. It is safe to delete entries
1109 * from the array in the loop body. You should hold either the RCU lock
1110 * or the xa_lock while iterating. If you need to drop the lock, call
1111 * xas_pause() first.
1112 */
1113#define xas_for_each(xas, entry, max) \
1114 for (entry = xas_find(xas, max); entry; \
1115 entry = xas_next_entry(xas, max))
1116
1117/**
1118 * xas_for_each_marked() - Iterate over a range of an XArray.
1119 * @xas: XArray operation state.
1120 * @entry: Entry retrieved from the array.
1121 * @max: Maximum index to retrieve from array.
1122 * @mark: Mark to search for.
1123 *
1124 * The loop body will be executed for each marked entry in the xarray
1125 * between the current xas position and @max. @entry will be set to
1126 * the entry retrieved from the xarray. It is safe to delete entries
1127 * from the array in the loop body. You should hold either the RCU lock
1128 * or the xa_lock while iterating. If you need to drop the lock, call
1129 * xas_pause() first.
1130 */
1131#define xas_for_each_marked(xas, entry, max, mark) \
1132 for (entry = xas_find_marked(xas, max, mark); entry; \
1133 entry = xas_next_marked(xas, max, mark))
1134
Matthew Wilcox4e99d4e2018-06-01 22:46:02 -04001135/**
1136 * xas_for_each_conflict() - Iterate over a range of an XArray.
1137 * @xas: XArray operation state.
1138 * @entry: Entry retrieved from the array.
1139 *
1140 * The loop body will be executed for each entry in the XArray that lies
1141 * within the range specified by @xas. If the loop completes successfully,
1142 * any entries that lie in this range will be replaced by @entry. The caller
1143 * may break out of the loop; if they do so, the contents of the XArray will
1144 * be unchanged. The operation may fail due to an out of memory condition.
1145 * The caller may also call xa_set_err() to exit the loop while setting an
1146 * error to record the reason.
1147 */
1148#define xas_for_each_conflict(xas, entry) \
1149 while ((entry = xas_find_conflict(xas)))
1150
Matthew Wilcox64d3e9a2017-12-01 00:06:52 -05001151void *__xas_next(struct xa_state *);
1152void *__xas_prev(struct xa_state *);
1153
1154/**
1155 * xas_prev() - Move iterator to previous index.
1156 * @xas: XArray operation state.
1157 *
1158 * If the @xas was in an error state, it will remain in an error state
1159 * and this function will return %NULL. If the @xas has never been walked,
1160 * it will have the effect of calling xas_load(). Otherwise one will be
1161 * subtracted from the index and the state will be walked to the correct
1162 * location in the array for the next operation.
1163 *
1164 * If the iterator was referencing index 0, this function wraps
1165 * around to %ULONG_MAX.
1166 *
1167 * Return: The entry at the new index. This may be %NULL or an internal
1168 * entry.
1169 */
1170static inline void *xas_prev(struct xa_state *xas)
1171{
1172 struct xa_node *node = xas->xa_node;
1173
1174 if (unlikely(xas_not_node(node) || node->shift ||
1175 xas->xa_offset == 0))
1176 return __xas_prev(xas);
1177
1178 xas->xa_index--;
1179 xas->xa_offset--;
1180 return xa_entry(xas->xa, node, xas->xa_offset);
1181}
1182
1183/**
1184 * xas_next() - Move state to next index.
1185 * @xas: XArray operation state.
1186 *
1187 * If the @xas was in an error state, it will remain in an error state
1188 * and this function will return %NULL. If the @xas has never been walked,
1189 * it will have the effect of calling xas_load(). Otherwise one will be
1190 * added to the index and the state will be walked to the correct
1191 * location in the array for the next operation.
1192 *
1193 * If the iterator was referencing index %ULONG_MAX, this function wraps
1194 * around to 0.
1195 *
1196 * Return: The entry at the new index. This may be %NULL or an internal
1197 * entry.
1198 */
1199static inline void *xas_next(struct xa_state *xas)
1200{
1201 struct xa_node *node = xas->xa_node;
1202
1203 if (unlikely(xas_not_node(node) || node->shift ||
1204 xas->xa_offset == XA_CHUNK_MASK))
1205 return __xas_next(xas);
1206
1207 xas->xa_index++;
1208 xas->xa_offset++;
1209 return xa_entry(xas->xa, node, xas->xa_offset);
1210}
1211
Matthew Wilcoxf6bb2a22018-04-10 16:36:52 -07001212#endif /* _LINUX_XARRAY_H */