blob: 15eab63286ed184f314c1c577276233d38c6b701 [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
35 * 256: Retry entry
Matthew Wilcoxad3d6c72017-11-07 14:57:46 -050036 *
37 * Errors are also represented as internal entries, but use the negative
38 * space (-4094 to -2). They're never stored in the slots array; only
39 * returned by the normal API.
Matthew Wilcox3159f942017-11-03 13:30:42 -040040 */
41
42#define BITS_PER_XA_VALUE (BITS_PER_LONG - 1)
43
44/**
45 * xa_mk_value() - Create an XArray entry from an integer.
46 * @v: Value to store in XArray.
47 *
48 * Context: Any context.
49 * Return: An entry suitable for storing in the XArray.
50 */
51static inline void *xa_mk_value(unsigned long v)
52{
53 WARN_ON((long)v < 0);
54 return (void *)((v << 1) | 1);
55}
56
57/**
58 * xa_to_value() - Get value stored in an XArray entry.
59 * @entry: XArray entry.
60 *
61 * Context: Any context.
62 * Return: The value stored in the XArray entry.
63 */
64static inline unsigned long xa_to_value(const void *entry)
65{
66 return (unsigned long)entry >> 1;
67}
68
69/**
70 * xa_is_value() - Determine if an entry is a value.
71 * @entry: XArray entry.
72 *
73 * Context: Any context.
74 * Return: True if the entry is a value, false if it is a pointer.
75 */
76static inline bool xa_is_value(const void *entry)
77{
78 return (unsigned long)entry & 1;
79}
80
81/**
82 * xa_tag_pointer() - Create an XArray entry for a tagged pointer.
83 * @p: Plain pointer.
84 * @tag: Tag value (0, 1 or 3).
85 *
86 * If the user of the XArray prefers, they can tag their pointers instead
87 * of storing value entries. Three tags are available (0, 1 and 3).
88 * These are distinct from the xa_mark_t as they are not replicated up
89 * through the array and cannot be searched for.
90 *
91 * Context: Any context.
92 * Return: An XArray entry.
93 */
94static inline void *xa_tag_pointer(void *p, unsigned long tag)
95{
96 return (void *)((unsigned long)p | tag);
97}
98
99/**
100 * xa_untag_pointer() - Turn an XArray entry into a plain pointer.
101 * @entry: XArray entry.
102 *
103 * If you have stored a tagged pointer in the XArray, call this function
104 * to get the untagged version of the pointer.
105 *
106 * Context: Any context.
107 * Return: A pointer.
108 */
109static inline void *xa_untag_pointer(void *entry)
110{
111 return (void *)((unsigned long)entry & ~3UL);
112}
113
114/**
115 * xa_pointer_tag() - Get the tag stored in an XArray entry.
116 * @entry: XArray entry.
117 *
118 * If you have stored a tagged pointer in the XArray, call this function
119 * to get the tag of that pointer.
120 *
121 * Context: Any context.
122 * Return: A tag.
123 */
124static inline unsigned int xa_pointer_tag(void *entry)
125{
126 return (unsigned long)entry & 3UL;
127}
Matthew Wilcoxf6bb2a22018-04-10 16:36:52 -0700128
Matthew Wilcox02c02bf2017-11-03 23:09:45 -0400129/*
130 * xa_mk_internal() - Create an internal entry.
131 * @v: Value to turn into an internal entry.
132 *
133 * Context: Any context.
134 * Return: An XArray internal entry corresponding to this value.
135 */
136static inline void *xa_mk_internal(unsigned long v)
137{
138 return (void *)((v << 2) | 2);
139}
140
141/*
142 * xa_to_internal() - Extract the value from an internal entry.
143 * @entry: XArray entry.
144 *
145 * Context: Any context.
146 * Return: The value which was stored in the internal entry.
147 */
148static inline unsigned long xa_to_internal(const void *entry)
149{
150 return (unsigned long)entry >> 2;
151}
152
153/*
154 * xa_is_internal() - Is the entry an internal entry?
155 * @entry: XArray entry.
156 *
157 * Context: Any context.
158 * Return: %true if the entry is an internal entry.
159 */
160static inline bool xa_is_internal(const void *entry)
161{
162 return ((unsigned long)entry & 3) == 2;
163}
164
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -0500165/**
Matthew Wilcoxad3d6c72017-11-07 14:57:46 -0500166 * xa_is_err() - Report whether an XArray operation returned an error
167 * @entry: Result from calling an XArray function
168 *
169 * If an XArray operation cannot complete an operation, it will return
170 * a special value indicating an error. This function tells you
171 * whether an error occurred; xa_err() tells you which error occurred.
172 *
173 * Context: Any context.
174 * Return: %true if the entry indicates an error.
175 */
176static inline bool xa_is_err(const void *entry)
177{
178 return unlikely(xa_is_internal(entry));
179}
180
181/**
182 * xa_err() - Turn an XArray result into an errno.
183 * @entry: Result from calling an XArray function.
184 *
185 * If an XArray operation cannot complete an operation, it will return
186 * a special pointer value which encodes an errno. This function extracts
187 * the errno from the pointer value, or returns 0 if the pointer does not
188 * represent an errno.
189 *
190 * Context: Any context.
191 * Return: A negative errno or 0.
192 */
193static inline int xa_err(void *entry)
194{
195 /* xa_to_internal() would not do sign extension. */
196 if (xa_is_err(entry))
197 return (long)entry >> 2;
198 return 0;
199}
200
Matthew Wilcox9b89a032017-11-10 09:34:31 -0500201typedef unsigned __bitwise xa_mark_t;
202#define XA_MARK_0 ((__force xa_mark_t)0U)
203#define XA_MARK_1 ((__force xa_mark_t)1U)
204#define XA_MARK_2 ((__force xa_mark_t)2U)
205#define XA_PRESENT ((__force xa_mark_t)8U)
206#define XA_MARK_MAX XA_MARK_2
207
Matthew Wilcox58d6ea32017-11-10 15:15:08 -0500208enum xa_lock_type {
209 XA_LOCK_IRQ = 1,
210 XA_LOCK_BH = 2,
211};
212
Matthew Wilcox9b89a032017-11-10 09:34:31 -0500213/*
214 * Values for xa_flags. The radix tree stores its GFP flags in the xa_flags,
215 * and we remain compatible with that.
216 */
Matthew Wilcox58d6ea32017-11-10 15:15:08 -0500217#define XA_FLAGS_LOCK_IRQ ((__force gfp_t)XA_LOCK_IRQ)
218#define XA_FLAGS_LOCK_BH ((__force gfp_t)XA_LOCK_BH)
Matthew Wilcox9b89a032017-11-10 09:34:31 -0500219#define XA_FLAGS_MARK(mark) ((__force gfp_t)((1U << __GFP_BITS_SHIFT) << \
220 (__force unsigned)(mark)))
221
Matthew Wilcoxad3d6c72017-11-07 14:57:46 -0500222/**
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -0500223 * struct xarray - The anchor of the XArray.
224 * @xa_lock: Lock that protects the contents of the XArray.
225 *
226 * To use the xarray, define it statically or embed it in your data structure.
227 * It is a very small data structure, so it does not usually make sense to
228 * allocate it separately and keep a pointer to it in your data structure.
229 *
230 * You may use the xa_lock to protect your own data structures as well.
231 */
232/*
233 * If all of the entries in the array are NULL, @xa_head is a NULL pointer.
234 * If the only non-NULL entry in the array is at index 0, @xa_head is that
235 * entry. If any other entry in the array is non-NULL, @xa_head points
236 * to an @xa_node.
237 */
238struct xarray {
239 spinlock_t xa_lock;
240/* private: The rest of the data structure is not to be used directly. */
241 gfp_t xa_flags;
242 void __rcu * xa_head;
243};
244
245#define XARRAY_INIT(name, flags) { \
246 .xa_lock = __SPIN_LOCK_UNLOCKED(name.xa_lock), \
247 .xa_flags = flags, \
248 .xa_head = NULL, \
249}
250
251/**
252 * DEFINE_XARRAY_FLAGS() - Define an XArray with custom flags.
253 * @name: A string that names your XArray.
254 * @flags: XA_FLAG values.
255 *
256 * This is intended for file scope definitions of XArrays. It declares
257 * and initialises an empty XArray with the chosen name and flags. It is
258 * equivalent to calling xa_init_flags() on the array, but it does the
259 * initialisation at compiletime instead of runtime.
260 */
261#define DEFINE_XARRAY_FLAGS(name, flags) \
262 struct xarray name = XARRAY_INIT(name, flags)
263
264/**
265 * DEFINE_XARRAY() - Define an XArray.
266 * @name: A string that names your XArray.
267 *
268 * This is intended for file scope definitions of XArrays. It declares
269 * and initialises an empty XArray with the chosen name. It is equivalent
270 * to calling xa_init() on the array, but it does the initialisation at
271 * compiletime instead of runtime.
272 */
273#define DEFINE_XARRAY(name) DEFINE_XARRAY_FLAGS(name, 0)
274
275void xa_init_flags(struct xarray *, gfp_t flags);
Matthew Wilcoxad3d6c72017-11-07 14:57:46 -0500276void *xa_load(struct xarray *, unsigned long index);
Matthew Wilcox58d6ea32017-11-10 15:15:08 -0500277void *xa_store(struct xarray *, unsigned long index, void *entry, gfp_t);
Matthew Wilcox9b89a032017-11-10 09:34:31 -0500278bool xa_get_mark(struct xarray *, unsigned long index, xa_mark_t);
279void xa_set_mark(struct xarray *, unsigned long index, xa_mark_t);
280void xa_clear_mark(struct xarray *, unsigned long index, xa_mark_t);
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -0500281
282/**
283 * xa_init() - Initialise an empty XArray.
284 * @xa: XArray.
285 *
286 * An empty XArray is full of NULL entries.
287 *
288 * Context: Any context.
289 */
290static inline void xa_init(struct xarray *xa)
291{
292 xa_init_flags(xa, 0);
293}
294
Matthew Wilcoxad3d6c72017-11-07 14:57:46 -0500295/**
296 * xa_empty() - Determine if an array has any present entries.
297 * @xa: XArray.
298 *
299 * Context: Any context.
300 * Return: %true if the array contains only NULL pointers.
301 */
302static inline bool xa_empty(const struct xarray *xa)
303{
304 return xa->xa_head == NULL;
305}
306
Matthew Wilcox9b89a032017-11-10 09:34:31 -0500307/**
308 * xa_marked() - Inquire whether any entry in this array has a mark set
309 * @xa: Array
310 * @mark: Mark value
311 *
312 * Context: Any context.
313 * Return: %true if any entry has this mark set.
314 */
315static inline bool xa_marked(const struct xarray *xa, xa_mark_t mark)
316{
317 return xa->xa_flags & XA_FLAGS_MARK(mark);
318}
319
Matthew Wilcox58d6ea32017-11-10 15:15:08 -0500320/**
321 * xa_erase() - Erase this entry from the XArray.
322 * @xa: XArray.
323 * @index: Index of entry.
324 *
325 * This function is the equivalent of calling xa_store() with %NULL as
326 * the third argument. The XArray does not need to allocate memory, so
327 * the user does not need to provide GFP flags.
328 *
329 * Context: Process context. Takes and releases the xa_lock.
330 * Return: The entry which used to be at this index.
331 */
332static inline void *xa_erase(struct xarray *xa, unsigned long index)
333{
334 return xa_store(xa, index, NULL, 0);
335}
336
Matthew Wilcoxf6bb2a22018-04-10 16:36:52 -0700337#define xa_trylock(xa) spin_trylock(&(xa)->xa_lock)
338#define xa_lock(xa) spin_lock(&(xa)->xa_lock)
339#define xa_unlock(xa) spin_unlock(&(xa)->xa_lock)
340#define xa_lock_bh(xa) spin_lock_bh(&(xa)->xa_lock)
341#define xa_unlock_bh(xa) spin_unlock_bh(&(xa)->xa_lock)
342#define xa_lock_irq(xa) spin_lock_irq(&(xa)->xa_lock)
343#define xa_unlock_irq(xa) spin_unlock_irq(&(xa)->xa_lock)
344#define xa_lock_irqsave(xa, flags) \
345 spin_lock_irqsave(&(xa)->xa_lock, flags)
346#define xa_unlock_irqrestore(xa, flags) \
347 spin_unlock_irqrestore(&(xa)->xa_lock, flags)
348
Matthew Wilcox9b89a032017-11-10 09:34:31 -0500349/*
Matthew Wilcox58d6ea32017-11-10 15:15:08 -0500350 * Versions of the normal API which require the caller to hold the
351 * xa_lock. If the GFP flags allow it, they will drop the lock to
352 * allocate memory, then reacquire it afterwards. These functions
353 * may also re-enable interrupts if the XArray flags indicate the
354 * locking should be interrupt safe.
Matthew Wilcox9b89a032017-11-10 09:34:31 -0500355 */
Matthew Wilcox58d6ea32017-11-10 15:15:08 -0500356void *__xa_erase(struct xarray *, unsigned long index);
357void *__xa_store(struct xarray *, unsigned long index, void *entry, gfp_t);
Matthew Wilcox9b89a032017-11-10 09:34:31 -0500358void __xa_set_mark(struct xarray *, unsigned long index, xa_mark_t);
359void __xa_clear_mark(struct xarray *, unsigned long index, xa_mark_t);
360
Matthew Wilcox58d6ea32017-11-10 15:15:08 -0500361/**
362 * xa_erase_bh() - Erase this entry from the XArray.
363 * @xa: XArray.
364 * @index: Index of entry.
365 *
366 * This function is the equivalent of calling xa_store() with %NULL as
367 * the third argument. The XArray does not need to allocate memory, so
368 * the user does not need to provide GFP flags.
369 *
370 * Context: Process context. Takes and releases the xa_lock while
371 * disabling softirqs.
372 * Return: The entry which used to be at this index.
373 */
374static inline void *xa_erase_bh(struct xarray *xa, unsigned long index)
375{
376 void *entry;
377
378 xa_lock_bh(xa);
379 entry = __xa_erase(xa, index);
380 xa_unlock_bh(xa);
381
382 return entry;
383}
384
385/**
386 * xa_erase_irq() - Erase this entry from the XArray.
387 * @xa: XArray.
388 * @index: Index of entry.
389 *
390 * This function is the equivalent of calling xa_store() with %NULL as
391 * the third argument. The XArray does not need to allocate memory, so
392 * the user does not need to provide GFP flags.
393 *
394 * Context: Process context. Takes and releases the xa_lock while
395 * disabling interrupts.
396 * Return: The entry which used to be at this index.
397 */
398static inline void *xa_erase_irq(struct xarray *xa, unsigned long index)
399{
400 void *entry;
401
402 xa_lock_irq(xa);
403 entry = __xa_erase(xa, index);
404 xa_unlock_irq(xa);
405
406 return entry;
407}
408
Matthew Wilcox02c02bf2017-11-03 23:09:45 -0400409/* Everything below here is the Advanced API. Proceed with caution. */
410
411/*
412 * The xarray is constructed out of a set of 'chunks' of pointers. Choosing
413 * the best chunk size requires some tradeoffs. A power of two recommends
414 * itself so that we can walk the tree based purely on shifts and masks.
415 * Generally, the larger the better; as the number of slots per level of the
416 * tree increases, the less tall the tree needs to be. But that needs to be
417 * balanced against the memory consumption of each node. On a 64-bit system,
418 * xa_node is currently 576 bytes, and we get 7 of them per 4kB page. If we
419 * doubled the number of slots per node, we'd get only 3 nodes per 4kB page.
420 */
421#ifndef XA_CHUNK_SHIFT
422#define XA_CHUNK_SHIFT (CONFIG_BASE_SMALL ? 4 : 6)
423#endif
424#define XA_CHUNK_SIZE (1UL << XA_CHUNK_SHIFT)
425#define XA_CHUNK_MASK (XA_CHUNK_SIZE - 1)
Matthew Wilcox01959df2017-11-09 09:23:56 -0500426#define XA_MAX_MARKS 3
427#define XA_MARK_LONGS DIV_ROUND_UP(XA_CHUNK_SIZE, BITS_PER_LONG)
428
429/*
430 * @count is the count of every non-NULL element in the ->slots array
431 * whether that is a value entry, a retry entry, a user pointer,
432 * a sibling entry or a pointer to the next level of the tree.
433 * @nr_values is the count of every element in ->slots which is
434 * either a value entry or a sibling of a value entry.
435 */
436struct xa_node {
437 unsigned char shift; /* Bits remaining in each slot */
438 unsigned char offset; /* Slot offset in parent */
439 unsigned char count; /* Total entry count */
440 unsigned char nr_values; /* Value entry count */
441 struct xa_node __rcu *parent; /* NULL at top of tree */
442 struct xarray *array; /* The array we belong to */
443 union {
444 struct list_head private_list; /* For tree user */
445 struct rcu_head rcu_head; /* Used when freeing node */
446 };
447 void __rcu *slots[XA_CHUNK_SIZE];
448 union {
449 unsigned long tags[XA_MAX_MARKS][XA_MARK_LONGS];
450 unsigned long marks[XA_MAX_MARKS][XA_MARK_LONGS];
451 };
452};
Matthew Wilcox02c02bf2017-11-03 23:09:45 -0400453
Matthew Wilcoxad3d6c72017-11-07 14:57:46 -0500454void xa_dump(const struct xarray *);
455void xa_dump_node(const struct xa_node *);
456
457#ifdef XA_DEBUG
458#define XA_BUG_ON(xa, x) do { \
459 if (x) { \
460 xa_dump(xa); \
461 BUG(); \
462 } \
463 } while (0)
464#define XA_NODE_BUG_ON(node, x) do { \
465 if (x) { \
466 if (node) xa_dump_node(node); \
467 BUG(); \
468 } \
469 } while (0)
470#else
471#define XA_BUG_ON(xa, x) do { } while (0)
472#define XA_NODE_BUG_ON(node, x) do { } while (0)
473#endif
474
475/* Private */
476static inline void *xa_head(const struct xarray *xa)
477{
478 return rcu_dereference_check(xa->xa_head,
479 lockdep_is_held(&xa->xa_lock));
480}
481
482/* Private */
483static inline void *xa_head_locked(const struct xarray *xa)
484{
485 return rcu_dereference_protected(xa->xa_head,
486 lockdep_is_held(&xa->xa_lock));
487}
488
489/* Private */
490static inline void *xa_entry(const struct xarray *xa,
491 const struct xa_node *node, unsigned int offset)
492{
493 XA_NODE_BUG_ON(node, offset >= XA_CHUNK_SIZE);
494 return rcu_dereference_check(node->slots[offset],
495 lockdep_is_held(&xa->xa_lock));
496}
497
498/* Private */
499static inline void *xa_entry_locked(const struct xarray *xa,
500 const struct xa_node *node, unsigned int offset)
501{
502 XA_NODE_BUG_ON(node, offset >= XA_CHUNK_SIZE);
503 return rcu_dereference_protected(node->slots[offset],
504 lockdep_is_held(&xa->xa_lock));
505}
506
507/* Private */
Matthew Wilcox9b89a032017-11-10 09:34:31 -0500508static inline struct xa_node *xa_parent(const struct xarray *xa,
509 const struct xa_node *node)
510{
511 return rcu_dereference_check(node->parent,
512 lockdep_is_held(&xa->xa_lock));
513}
514
515/* Private */
516static inline struct xa_node *xa_parent_locked(const struct xarray *xa,
517 const struct xa_node *node)
518{
519 return rcu_dereference_protected(node->parent,
520 lockdep_is_held(&xa->xa_lock));
521}
522
523/* Private */
Matthew Wilcox58d6ea32017-11-10 15:15:08 -0500524static inline void *xa_mk_node(const struct xa_node *node)
525{
526 return (void *)((unsigned long)node | 2);
527}
528
529/* Private */
Matthew Wilcoxad3d6c72017-11-07 14:57:46 -0500530static inline struct xa_node *xa_to_node(const void *entry)
531{
532 return (struct xa_node *)((unsigned long)entry - 2);
533}
534
Matthew Wilcox02c02bf2017-11-03 23:09:45 -0400535/* Private */
536static inline bool xa_is_node(const void *entry)
537{
538 return xa_is_internal(entry) && (unsigned long)entry > 4096;
539}
540
541/* Private */
542static inline void *xa_mk_sibling(unsigned int offset)
543{
544 return xa_mk_internal(offset);
545}
546
547/* Private */
548static inline unsigned long xa_to_sibling(const void *entry)
549{
550 return xa_to_internal(entry);
551}
552
553/**
554 * xa_is_sibling() - Is the entry a sibling entry?
555 * @entry: Entry retrieved from the XArray
556 *
557 * Return: %true if the entry is a sibling entry.
558 */
559static inline bool xa_is_sibling(const void *entry)
560{
561 return IS_ENABLED(CONFIG_XARRAY_MULTI) && xa_is_internal(entry) &&
562 (entry < xa_mk_sibling(XA_CHUNK_SIZE - 1));
563}
564
565#define XA_RETRY_ENTRY xa_mk_internal(256)
566
Matthew Wilcoxad3d6c72017-11-07 14:57:46 -0500567/**
568 * xa_is_retry() - Is the entry a retry entry?
569 * @entry: Entry retrieved from the XArray
570 *
571 * Return: %true if the entry is a retry entry.
572 */
573static inline bool xa_is_retry(const void *entry)
574{
575 return unlikely(entry == XA_RETRY_ENTRY);
576}
577
578/**
579 * typedef xa_update_node_t - A callback function from the XArray.
580 * @node: The node which is being processed
581 *
582 * This function is called every time the XArray updates the count of
583 * present and value entries in a node. It allows advanced users to
584 * maintain the private_list in the node.
585 *
586 * Context: The xa_lock is held and interrupts may be disabled.
587 * Implementations should not drop the xa_lock, nor re-enable
588 * interrupts.
589 */
590typedef void (*xa_update_node_t)(struct xa_node *node);
591
592/*
593 * The xa_state is opaque to its users. It contains various different pieces
594 * of state involved in the current operation on the XArray. It should be
595 * declared on the stack and passed between the various internal routines.
596 * The various elements in it should not be accessed directly, but only
597 * through the provided accessor functions. The below documentation is for
598 * the benefit of those working on the code, not for users of the XArray.
599 *
600 * @xa_node usually points to the xa_node containing the slot we're operating
601 * on (and @xa_offset is the offset in the slots array). If there is a
602 * single entry in the array at index 0, there are no allocated xa_nodes to
603 * point to, and so we store %NULL in @xa_node. @xa_node is set to
604 * the value %XAS_RESTART if the xa_state is not walked to the correct
605 * position in the tree of nodes for this operation. If an error occurs
606 * during an operation, it is set to an %XAS_ERROR value. If we run off the
607 * end of the allocated nodes, it is set to %XAS_BOUNDS.
608 */
609struct xa_state {
610 struct xarray *xa;
611 unsigned long xa_index;
612 unsigned char xa_shift;
613 unsigned char xa_sibs;
614 unsigned char xa_offset;
615 unsigned char xa_pad; /* Helps gcc generate better code */
616 struct xa_node *xa_node;
617 struct xa_node *xa_alloc;
618 xa_update_node_t xa_update;
619};
620
621/*
622 * We encode errnos in the xas->xa_node. If an error has happened, we need to
623 * drop the lock to fix it, and once we've done so the xa_state is invalid.
624 */
625#define XA_ERROR(errno) ((struct xa_node *)(((unsigned long)errno << 2) | 2UL))
626#define XAS_BOUNDS ((struct xa_node *)1UL)
627#define XAS_RESTART ((struct xa_node *)3UL)
628
629#define __XA_STATE(array, index, shift, sibs) { \
630 .xa = array, \
631 .xa_index = index, \
632 .xa_shift = shift, \
633 .xa_sibs = sibs, \
634 .xa_offset = 0, \
635 .xa_pad = 0, \
636 .xa_node = XAS_RESTART, \
637 .xa_alloc = NULL, \
638 .xa_update = NULL \
639}
640
641/**
642 * XA_STATE() - Declare an XArray operation state.
643 * @name: Name of this operation state (usually xas).
644 * @array: Array to operate on.
645 * @index: Initial index of interest.
646 *
647 * Declare and initialise an xa_state on the stack.
648 */
649#define XA_STATE(name, array, index) \
650 struct xa_state name = __XA_STATE(array, index, 0, 0)
651
652/**
653 * XA_STATE_ORDER() - Declare an XArray operation state.
654 * @name: Name of this operation state (usually xas).
655 * @array: Array to operate on.
656 * @index: Initial index of interest.
657 * @order: Order of entry.
658 *
659 * Declare and initialise an xa_state on the stack. This variant of
660 * XA_STATE() allows you to specify the 'order' of the element you
661 * want to operate on.`
662 */
663#define XA_STATE_ORDER(name, array, index, order) \
664 struct xa_state name = __XA_STATE(array, \
665 (index >> order) << order, \
666 order - (order % XA_CHUNK_SHIFT), \
667 (1U << (order % XA_CHUNK_SHIFT)) - 1)
668
669#define xas_marked(xas, mark) xa_marked((xas)->xa, (mark))
670#define xas_trylock(xas) xa_trylock((xas)->xa)
671#define xas_lock(xas) xa_lock((xas)->xa)
672#define xas_unlock(xas) xa_unlock((xas)->xa)
673#define xas_lock_bh(xas) xa_lock_bh((xas)->xa)
674#define xas_unlock_bh(xas) xa_unlock_bh((xas)->xa)
675#define xas_lock_irq(xas) xa_lock_irq((xas)->xa)
676#define xas_unlock_irq(xas) xa_unlock_irq((xas)->xa)
677#define xas_lock_irqsave(xas, flags) \
678 xa_lock_irqsave((xas)->xa, flags)
679#define xas_unlock_irqrestore(xas, flags) \
680 xa_unlock_irqrestore((xas)->xa, flags)
681
682/**
683 * xas_error() - Return an errno stored in the xa_state.
684 * @xas: XArray operation state.
685 *
686 * Return: 0 if no error has been noted. A negative errno if one has.
687 */
688static inline int xas_error(const struct xa_state *xas)
689{
690 return xa_err(xas->xa_node);
691}
692
693/**
694 * xas_set_err() - Note an error in the xa_state.
695 * @xas: XArray operation state.
696 * @err: Negative error number.
697 *
698 * Only call this function with a negative @err; zero or positive errors
699 * will probably not behave the way you think they should. If you want
700 * to clear the error from an xa_state, use xas_reset().
701 */
702static inline void xas_set_err(struct xa_state *xas, long err)
703{
704 xas->xa_node = XA_ERROR(err);
705}
706
707/**
708 * xas_invalid() - Is the xas in a retry or error state?
709 * @xas: XArray operation state.
710 *
711 * Return: %true if the xas cannot be used for operations.
712 */
713static inline bool xas_invalid(const struct xa_state *xas)
714{
715 return (unsigned long)xas->xa_node & 3;
716}
717
718/**
719 * xas_valid() - Is the xas a valid cursor into the array?
720 * @xas: XArray operation state.
721 *
722 * Return: %true if the xas can be used for operations.
723 */
724static inline bool xas_valid(const struct xa_state *xas)
725{
726 return !xas_invalid(xas);
727}
728
Matthew Wilcox9b89a032017-11-10 09:34:31 -0500729/* True if the pointer is something other than a node */
730static inline bool xas_not_node(struct xa_node *node)
731{
732 return ((unsigned long)node & 3) || !node;
733}
734
Matthew Wilcox58d6ea32017-11-10 15:15:08 -0500735/* True if the node represents head-of-tree, RESTART or BOUNDS */
736static inline bool xas_top(struct xa_node *node)
737{
738 return node <= XAS_RESTART;
739}
740
Matthew Wilcoxad3d6c72017-11-07 14:57:46 -0500741/**
742 * xas_reset() - Reset an XArray operation state.
743 * @xas: XArray operation state.
744 *
745 * Resets the error or walk state of the @xas so future walks of the
746 * array will start from the root. Use this if you have dropped the
747 * xarray lock and want to reuse the xa_state.
748 *
749 * Context: Any context.
750 */
751static inline void xas_reset(struct xa_state *xas)
752{
753 xas->xa_node = XAS_RESTART;
754}
755
756/**
757 * xas_retry() - Retry the operation if appropriate.
758 * @xas: XArray operation state.
759 * @entry: Entry from xarray.
760 *
761 * The advanced functions may sometimes return an internal entry, such as
762 * a retry entry or a zero entry. This function sets up the @xas to restart
763 * the walk from the head of the array if needed.
764 *
765 * Context: Any context.
766 * Return: true if the operation needs to be retried.
767 */
768static inline bool xas_retry(struct xa_state *xas, const void *entry)
769{
770 if (!xa_is_retry(entry))
771 return false;
772 xas_reset(xas);
773 return true;
774}
775
776void *xas_load(struct xa_state *);
Matthew Wilcox58d6ea32017-11-10 15:15:08 -0500777void *xas_store(struct xa_state *, void *entry);
Matthew Wilcoxad3d6c72017-11-07 14:57:46 -0500778
Matthew Wilcox9b89a032017-11-10 09:34:31 -0500779bool xas_get_mark(const struct xa_state *, xa_mark_t);
780void xas_set_mark(const struct xa_state *, xa_mark_t);
781void xas_clear_mark(const struct xa_state *, xa_mark_t);
Matthew Wilcox58d6ea32017-11-10 15:15:08 -0500782void xas_init_marks(const struct xa_state *);
783
784bool xas_nomem(struct xa_state *, gfp_t);
Matthew Wilcox9b89a032017-11-10 09:34:31 -0500785
Matthew Wilcoxad3d6c72017-11-07 14:57:46 -0500786/**
787 * xas_reload() - Refetch an entry from the xarray.
788 * @xas: XArray operation state.
789 *
790 * Use this function to check that a previously loaded entry still has
791 * the same value. This is useful for the lockless pagecache lookup where
792 * we walk the array with only the RCU lock to protect us, lock the page,
793 * then check that the page hasn't moved since we looked it up.
794 *
795 * The caller guarantees that @xas is still valid. If it may be in an
796 * error or restart state, call xas_load() instead.
797 *
798 * Return: The entry at this location in the xarray.
799 */
800static inline void *xas_reload(struct xa_state *xas)
801{
802 struct xa_node *node = xas->xa_node;
803
804 if (node)
805 return xa_entry(xas->xa, node, xas->xa_offset);
806 return xa_head(xas->xa);
807}
808
Matthew Wilcox58d6ea32017-11-10 15:15:08 -0500809/**
810 * xas_set() - Set up XArray operation state for a different index.
811 * @xas: XArray operation state.
812 * @index: New index into the XArray.
813 *
814 * Move the operation state to refer to a different index. This will
815 * have the effect of starting a walk from the top; see xas_next()
816 * to move to an adjacent index.
817 */
818static inline void xas_set(struct xa_state *xas, unsigned long index)
819{
820 xas->xa_index = index;
821 xas->xa_node = XAS_RESTART;
822}
823
824/**
825 * xas_set_order() - Set up XArray operation state for a multislot entry.
826 * @xas: XArray operation state.
827 * @index: Target of the operation.
828 * @order: Entry occupies 2^@order indices.
829 */
830static inline void xas_set_order(struct xa_state *xas, unsigned long index,
831 unsigned int order)
832{
833#ifdef CONFIG_XARRAY_MULTI
834 xas->xa_index = order < BITS_PER_LONG ? (index >> order) << order : 0;
835 xas->xa_shift = order - (order % XA_CHUNK_SHIFT);
836 xas->xa_sibs = (1 << (order % XA_CHUNK_SHIFT)) - 1;
837 xas->xa_node = XAS_RESTART;
838#else
839 BUG_ON(order > 0);
840 xas_set(xas, index);
841#endif
842}
843
844/**
845 * xas_set_update() - Set up XArray operation state for a callback.
846 * @xas: XArray operation state.
847 * @update: Function to call when updating a node.
848 *
849 * The XArray can notify a caller after it has updated an xa_node.
850 * This is advanced functionality and is only needed by the page cache.
851 */
852static inline void xas_set_update(struct xa_state *xas, xa_update_node_t update)
853{
854 xas->xa_update = update;
855}
856
Matthew Wilcoxf6bb2a22018-04-10 16:36:52 -0700857#endif /* _LINUX_XARRAY_H */