1f8d5d0ccSMatthew Wilcox // SPDX-License-Identifier: GPL-2.0+
2f8d5d0ccSMatthew Wilcox /*
3f8d5d0ccSMatthew Wilcox * XArray implementation
4c44aa5e8SMatthew Wilcox (Oracle) * Copyright (c) 2017-2018 Microsoft Corporation
5c44aa5e8SMatthew Wilcox (Oracle) * Copyright (c) 2018-2020 Oracle
6f8d5d0ccSMatthew Wilcox * Author: Matthew Wilcox <willy@infradead.org>
7f8d5d0ccSMatthew Wilcox */
8f8d5d0ccSMatthew Wilcox
99b89a035SMatthew Wilcox #include <linux/bitmap.h>
10f8d5d0ccSMatthew Wilcox #include <linux/export.h>
1158d6ea30SMatthew Wilcox #include <linux/list.h>
1258d6ea30SMatthew Wilcox #include <linux/slab.h>
13f8d5d0ccSMatthew Wilcox #include <linux/xarray.h>
14f8d5d0ccSMatthew Wilcox
15bde1597dSArnd Bergmann #include "radix-tree.h"
16bde1597dSArnd Bergmann
17f8d5d0ccSMatthew Wilcox /*
18f8d5d0ccSMatthew Wilcox * Coding conventions in this file:
19f8d5d0ccSMatthew Wilcox *
20f8d5d0ccSMatthew Wilcox * @xa is used to refer to the entire xarray.
21f8d5d0ccSMatthew Wilcox * @xas is the 'xarray operation state'. It may be either a pointer to
22f8d5d0ccSMatthew Wilcox * an xa_state, or an xa_state stored on the stack. This is an unfortunate
23f8d5d0ccSMatthew Wilcox * ambiguity.
24f8d5d0ccSMatthew Wilcox * @index is the index of the entry being operated on
25f8d5d0ccSMatthew Wilcox * @mark is an xa_mark_t; a small number indicating one of the mark bits.
26f8d5d0ccSMatthew Wilcox * @node refers to an xa_node; usually the primary one being operated on by
27f8d5d0ccSMatthew Wilcox * this function.
28f8d5d0ccSMatthew Wilcox * @offset is the index into the slots array inside an xa_node.
29f8d5d0ccSMatthew Wilcox * @parent refers to the @xa_node closer to the head than @node.
30f8d5d0ccSMatthew Wilcox * @entry refers to something stored in a slot in the xarray
31f8d5d0ccSMatthew Wilcox */
32f8d5d0ccSMatthew Wilcox
xa_lock_type(const struct xarray * xa)3358d6ea30SMatthew Wilcox static inline unsigned int xa_lock_type(const struct xarray *xa)
3458d6ea30SMatthew Wilcox {
3558d6ea30SMatthew Wilcox return (__force unsigned int)xa->xa_flags & 3;
3658d6ea30SMatthew Wilcox }
3758d6ea30SMatthew Wilcox
xas_lock_type(struct xa_state * xas,unsigned int lock_type)3858d6ea30SMatthew Wilcox static inline void xas_lock_type(struct xa_state *xas, unsigned int lock_type)
3958d6ea30SMatthew Wilcox {
4058d6ea30SMatthew Wilcox if (lock_type == XA_LOCK_IRQ)
4158d6ea30SMatthew Wilcox xas_lock_irq(xas);
4258d6ea30SMatthew Wilcox else if (lock_type == XA_LOCK_BH)
4358d6ea30SMatthew Wilcox xas_lock_bh(xas);
4458d6ea30SMatthew Wilcox else
4558d6ea30SMatthew Wilcox xas_lock(xas);
4658d6ea30SMatthew Wilcox }
4758d6ea30SMatthew Wilcox
xas_unlock_type(struct xa_state * xas,unsigned int lock_type)4858d6ea30SMatthew Wilcox static inline void xas_unlock_type(struct xa_state *xas, unsigned int lock_type)
4958d6ea30SMatthew Wilcox {
5058d6ea30SMatthew Wilcox if (lock_type == XA_LOCK_IRQ)
5158d6ea30SMatthew Wilcox xas_unlock_irq(xas);
5258d6ea30SMatthew Wilcox else if (lock_type == XA_LOCK_BH)
5358d6ea30SMatthew Wilcox xas_unlock_bh(xas);
5458d6ea30SMatthew Wilcox else
5558d6ea30SMatthew Wilcox xas_unlock(xas);
5658d6ea30SMatthew Wilcox }
5758d6ea30SMatthew Wilcox
xa_track_free(const struct xarray * xa)58371c752dSMatthew Wilcox static inline bool xa_track_free(const struct xarray *xa)
59371c752dSMatthew Wilcox {
60371c752dSMatthew Wilcox return xa->xa_flags & XA_FLAGS_TRACK_FREE;
61371c752dSMatthew Wilcox }
62371c752dSMatthew Wilcox
xa_zero_busy(const struct xarray * xa)633ccaf57aSMatthew Wilcox static inline bool xa_zero_busy(const struct xarray *xa)
643ccaf57aSMatthew Wilcox {
653ccaf57aSMatthew Wilcox return xa->xa_flags & XA_FLAGS_ZERO_BUSY;
663ccaf57aSMatthew Wilcox }
673ccaf57aSMatthew Wilcox
xa_mark_set(struct xarray * xa,xa_mark_t mark)689b89a035SMatthew Wilcox static inline void xa_mark_set(struct xarray *xa, xa_mark_t mark)
699b89a035SMatthew Wilcox {
709b89a035SMatthew Wilcox if (!(xa->xa_flags & XA_FLAGS_MARK(mark)))
719b89a035SMatthew Wilcox xa->xa_flags |= XA_FLAGS_MARK(mark);
729b89a035SMatthew Wilcox }
739b89a035SMatthew Wilcox
xa_mark_clear(struct xarray * xa,xa_mark_t mark)749b89a035SMatthew Wilcox static inline void xa_mark_clear(struct xarray *xa, xa_mark_t mark)
759b89a035SMatthew Wilcox {
769b89a035SMatthew Wilcox if (xa->xa_flags & XA_FLAGS_MARK(mark))
779b89a035SMatthew Wilcox xa->xa_flags &= ~(XA_FLAGS_MARK(mark));
789b89a035SMatthew Wilcox }
799b89a035SMatthew Wilcox
node_marks(struct xa_node * node,xa_mark_t mark)809b89a035SMatthew Wilcox static inline unsigned long *node_marks(struct xa_node *node, xa_mark_t mark)
819b89a035SMatthew Wilcox {
829b89a035SMatthew Wilcox return node->marks[(__force unsigned)mark];
839b89a035SMatthew Wilcox }
849b89a035SMatthew Wilcox
node_get_mark(struct xa_node * node,unsigned int offset,xa_mark_t mark)859b89a035SMatthew Wilcox static inline bool node_get_mark(struct xa_node *node,
869b89a035SMatthew Wilcox unsigned int offset, xa_mark_t mark)
879b89a035SMatthew Wilcox {
889b89a035SMatthew Wilcox return test_bit(offset, node_marks(node, mark));
899b89a035SMatthew Wilcox }
909b89a035SMatthew Wilcox
919b89a035SMatthew Wilcox /* returns true if the bit was set */
node_set_mark(struct xa_node * node,unsigned int offset,xa_mark_t mark)929b89a035SMatthew Wilcox static inline bool node_set_mark(struct xa_node *node, unsigned int offset,
939b89a035SMatthew Wilcox xa_mark_t mark)
949b89a035SMatthew Wilcox {
959b89a035SMatthew Wilcox return __test_and_set_bit(offset, node_marks(node, mark));
969b89a035SMatthew Wilcox }
979b89a035SMatthew Wilcox
989b89a035SMatthew Wilcox /* returns true if the bit was set */
node_clear_mark(struct xa_node * node,unsigned int offset,xa_mark_t mark)999b89a035SMatthew Wilcox static inline bool node_clear_mark(struct xa_node *node, unsigned int offset,
1009b89a035SMatthew Wilcox xa_mark_t mark)
1019b89a035SMatthew Wilcox {
1029b89a035SMatthew Wilcox return __test_and_clear_bit(offset, node_marks(node, mark));
1039b89a035SMatthew Wilcox }
1049b89a035SMatthew Wilcox
node_any_mark(struct xa_node * node,xa_mark_t mark)1059b89a035SMatthew Wilcox static inline bool node_any_mark(struct xa_node *node, xa_mark_t mark)
1069b89a035SMatthew Wilcox {
1079b89a035SMatthew Wilcox return !bitmap_empty(node_marks(node, mark), XA_CHUNK_SIZE);
1089b89a035SMatthew Wilcox }
1099b89a035SMatthew Wilcox
node_mark_all(struct xa_node * node,xa_mark_t mark)110371c752dSMatthew Wilcox static inline void node_mark_all(struct xa_node *node, xa_mark_t mark)
111371c752dSMatthew Wilcox {
112371c752dSMatthew Wilcox bitmap_fill(node_marks(node, mark), XA_CHUNK_SIZE);
113371c752dSMatthew Wilcox }
114371c752dSMatthew Wilcox
11558d6ea30SMatthew Wilcox #define mark_inc(mark) do { \
11658d6ea30SMatthew Wilcox mark = (__force xa_mark_t)((__force unsigned)(mark) + 1); \
11758d6ea30SMatthew Wilcox } while (0)
11858d6ea30SMatthew Wilcox
11958d6ea30SMatthew Wilcox /*
12058d6ea30SMatthew Wilcox * xas_squash_marks() - Merge all marks to the first entry
12158d6ea30SMatthew Wilcox * @xas: Array operation state.
12258d6ea30SMatthew Wilcox *
12358d6ea30SMatthew Wilcox * Set a mark on the first entry if any entry has it set. Clear marks on
12458d6ea30SMatthew Wilcox * all sibling entries.
12558d6ea30SMatthew Wilcox */
xas_squash_marks(const struct xa_state * xas)12658d6ea30SMatthew Wilcox static void xas_squash_marks(const struct xa_state *xas)
12758d6ea30SMatthew Wilcox {
12858d6ea30SMatthew Wilcox unsigned int mark = 0;
12958d6ea30SMatthew Wilcox unsigned int limit = xas->xa_offset + xas->xa_sibs + 1;
13058d6ea30SMatthew Wilcox
13158d6ea30SMatthew Wilcox if (!xas->xa_sibs)
13258d6ea30SMatthew Wilcox return;
13358d6ea30SMatthew Wilcox
13458d6ea30SMatthew Wilcox do {
13558d6ea30SMatthew Wilcox unsigned long *marks = xas->xa_node->marks[mark];
13658d6ea30SMatthew Wilcox if (find_next_bit(marks, limit, xas->xa_offset + 1) == limit)
13758d6ea30SMatthew Wilcox continue;
13858d6ea30SMatthew Wilcox __set_bit(xas->xa_offset, marks);
13958d6ea30SMatthew Wilcox bitmap_clear(marks, xas->xa_offset + 1, xas->xa_sibs);
14058d6ea30SMatthew Wilcox } while (mark++ != (__force unsigned)XA_MARK_MAX);
14158d6ea30SMatthew Wilcox }
14258d6ea30SMatthew Wilcox
143ad3d6c72SMatthew Wilcox /* extracts the offset within this node from the index */
get_offset(unsigned long index,struct xa_node * node)144ad3d6c72SMatthew Wilcox static unsigned int get_offset(unsigned long index, struct xa_node *node)
145ad3d6c72SMatthew Wilcox {
146ad3d6c72SMatthew Wilcox return (index >> node->shift) & XA_CHUNK_MASK;
147ad3d6c72SMatthew Wilcox }
148ad3d6c72SMatthew Wilcox
xas_set_offset(struct xa_state * xas)149b803b428SMatthew Wilcox static void xas_set_offset(struct xa_state *xas)
150b803b428SMatthew Wilcox {
151b803b428SMatthew Wilcox xas->xa_offset = get_offset(xas->xa_index, xas->xa_node);
152b803b428SMatthew Wilcox }
153b803b428SMatthew Wilcox
154ad3d6c72SMatthew Wilcox /* move the index either forwards (find) or backwards (sibling slot) */
xas_move_index(struct xa_state * xas,unsigned long offset)155ad3d6c72SMatthew Wilcox static void xas_move_index(struct xa_state *xas, unsigned long offset)
156ad3d6c72SMatthew Wilcox {
157ad3d6c72SMatthew Wilcox unsigned int shift = xas->xa_node->shift;
158ad3d6c72SMatthew Wilcox xas->xa_index &= ~XA_CHUNK_MASK << shift;
159ad3d6c72SMatthew Wilcox xas->xa_index += offset << shift;
160ad3d6c72SMatthew Wilcox }
161ad3d6c72SMatthew Wilcox
xas_next_offset(struct xa_state * xas)16225a8de7fSMatthew Wilcox (Oracle) static void xas_next_offset(struct xa_state *xas)
163b803b428SMatthew Wilcox {
164b803b428SMatthew Wilcox xas->xa_offset++;
165b803b428SMatthew Wilcox xas_move_index(xas, xas->xa_offset);
166b803b428SMatthew Wilcox }
167b803b428SMatthew Wilcox
set_bounds(struct xa_state * xas)168ad3d6c72SMatthew Wilcox static void *set_bounds(struct xa_state *xas)
169ad3d6c72SMatthew Wilcox {
170ad3d6c72SMatthew Wilcox xas->xa_node = XAS_BOUNDS;
171ad3d6c72SMatthew Wilcox return NULL;
172ad3d6c72SMatthew Wilcox }
173ad3d6c72SMatthew Wilcox
174ad3d6c72SMatthew Wilcox /*
175ad3d6c72SMatthew Wilcox * Starts a walk. If the @xas is already valid, we assume that it's on
176ad3d6c72SMatthew Wilcox * the right path and just return where we've got to. If we're in an
177ad3d6c72SMatthew Wilcox * error state, return NULL. If the index is outside the current scope
178ad3d6c72SMatthew Wilcox * of the xarray, return NULL without changing @xas->xa_node. Otherwise
179ad3d6c72SMatthew Wilcox * set @xas->xa_node to NULL and return the current head of the array.
180ad3d6c72SMatthew Wilcox */
xas_start(struct xa_state * xas)181ad3d6c72SMatthew Wilcox static void *xas_start(struct xa_state *xas)
182ad3d6c72SMatthew Wilcox {
183ad3d6c72SMatthew Wilcox void *entry;
184ad3d6c72SMatthew Wilcox
185ad3d6c72SMatthew Wilcox if (xas_valid(xas))
186ad3d6c72SMatthew Wilcox return xas_reload(xas);
187ad3d6c72SMatthew Wilcox if (xas_error(xas))
188ad3d6c72SMatthew Wilcox return NULL;
189ad3d6c72SMatthew Wilcox
190ad3d6c72SMatthew Wilcox entry = xa_head(xas->xa);
191ad3d6c72SMatthew Wilcox if (!xa_is_node(entry)) {
192ad3d6c72SMatthew Wilcox if (xas->xa_index)
193ad3d6c72SMatthew Wilcox return set_bounds(xas);
194ad3d6c72SMatthew Wilcox } else {
195ad3d6c72SMatthew Wilcox if ((xas->xa_index >> xa_to_node(entry)->shift) > XA_CHUNK_MASK)
196ad3d6c72SMatthew Wilcox return set_bounds(xas);
197ad3d6c72SMatthew Wilcox }
198ad3d6c72SMatthew Wilcox
199ad3d6c72SMatthew Wilcox xas->xa_node = NULL;
200ad3d6c72SMatthew Wilcox return entry;
201ad3d6c72SMatthew Wilcox }
202ad3d6c72SMatthew Wilcox
xas_descend(struct xa_state * xas,struct xa_node * node)203ad3d6c72SMatthew Wilcox static void *xas_descend(struct xa_state *xas, struct xa_node *node)
204ad3d6c72SMatthew Wilcox {
205ad3d6c72SMatthew Wilcox unsigned int offset = get_offset(xas->xa_index, node);
206ad3d6c72SMatthew Wilcox void *entry = xa_entry(xas->xa, node, offset);
207ad3d6c72SMatthew Wilcox
208ad3d6c72SMatthew Wilcox xas->xa_node = node;
209cbc02854SMatthew Wilcox (Oracle) while (xa_is_sibling(entry)) {
210ad3d6c72SMatthew Wilcox offset = xa_to_sibling(entry);
211ad3d6c72SMatthew Wilcox entry = xa_entry(xas->xa, node, offset);
21263b1898fSMatthew Wilcox (Oracle) if (node->shift && xa_is_node(entry))
21363b1898fSMatthew Wilcox (Oracle) entry = XA_RETRY_ENTRY;
214ad3d6c72SMatthew Wilcox }
215ad3d6c72SMatthew Wilcox
216ad3d6c72SMatthew Wilcox xas->xa_offset = offset;
217ad3d6c72SMatthew Wilcox return entry;
218ad3d6c72SMatthew Wilcox }
219ad3d6c72SMatthew Wilcox
220ad3d6c72SMatthew Wilcox /**
221ad3d6c72SMatthew Wilcox * xas_load() - Load an entry from the XArray (advanced).
222ad3d6c72SMatthew Wilcox * @xas: XArray operation state.
223ad3d6c72SMatthew Wilcox *
224ad3d6c72SMatthew Wilcox * Usually walks the @xas to the appropriate state to load the entry
225ad3d6c72SMatthew Wilcox * stored at xa_index. However, it will do nothing and return %NULL if
226ad3d6c72SMatthew Wilcox * @xas is in an error state. xas_load() will never expand the tree.
227ad3d6c72SMatthew Wilcox *
228ad3d6c72SMatthew Wilcox * If the xa_state is set up to operate on a multi-index entry, xas_load()
229ad3d6c72SMatthew Wilcox * may return %NULL or an internal entry, even if there are entries
230ad3d6c72SMatthew Wilcox * present within the range specified by @xas.
231ad3d6c72SMatthew Wilcox *
232ad3d6c72SMatthew Wilcox * Context: Any context. The caller should hold the xa_lock or the RCU lock.
233ad3d6c72SMatthew Wilcox * Return: Usually an entry in the XArray, but see description for exceptions.
234ad3d6c72SMatthew Wilcox */
xas_load(struct xa_state * xas)235ad3d6c72SMatthew Wilcox void *xas_load(struct xa_state *xas)
236ad3d6c72SMatthew Wilcox {
237ad3d6c72SMatthew Wilcox void *entry = xas_start(xas);
238ad3d6c72SMatthew Wilcox
239ad3d6c72SMatthew Wilcox while (xa_is_node(entry)) {
240ad3d6c72SMatthew Wilcox struct xa_node *node = xa_to_node(entry);
241ad3d6c72SMatthew Wilcox
242ad3d6c72SMatthew Wilcox if (xas->xa_shift > node->shift)
243ad3d6c72SMatthew Wilcox break;
244ad3d6c72SMatthew Wilcox entry = xas_descend(xas, node);
24576b4e529SMatthew Wilcox if (node->shift == 0)
24676b4e529SMatthew Wilcox break;
247ad3d6c72SMatthew Wilcox }
248ad3d6c72SMatthew Wilcox return entry;
249ad3d6c72SMatthew Wilcox }
250ad3d6c72SMatthew Wilcox EXPORT_SYMBOL_GPL(xas_load);
251ad3d6c72SMatthew Wilcox
25258d6ea30SMatthew Wilcox #define XA_RCU_FREE ((struct xarray *)1)
25358d6ea30SMatthew Wilcox
xa_node_free(struct xa_node * node)25458d6ea30SMatthew Wilcox static void xa_node_free(struct xa_node *node)
25558d6ea30SMatthew Wilcox {
25658d6ea30SMatthew Wilcox XA_NODE_BUG_ON(node, !list_empty(&node->private_list));
25758d6ea30SMatthew Wilcox node->array = XA_RCU_FREE;
25858d6ea30SMatthew Wilcox call_rcu(&node->rcu_head, radix_tree_node_rcu_free);
25958d6ea30SMatthew Wilcox }
26058d6ea30SMatthew Wilcox
26158d6ea30SMatthew Wilcox /*
26258d6ea30SMatthew Wilcox * xas_destroy() - Free any resources allocated during the XArray operation.
26358d6ea30SMatthew Wilcox * @xas: XArray operation state.
26458d6ea30SMatthew Wilcox *
26569a37a8bSMatthew Wilcox (Oracle) * Most users will not need to call this function; it is called for you
26669a37a8bSMatthew Wilcox (Oracle) * by xas_nomem().
26758d6ea30SMatthew Wilcox */
xas_destroy(struct xa_state * xas)26869a37a8bSMatthew Wilcox (Oracle) void xas_destroy(struct xa_state *xas)
26958d6ea30SMatthew Wilcox {
2708fc75643SMatthew Wilcox (Oracle) struct xa_node *next, *node = xas->xa_alloc;
27158d6ea30SMatthew Wilcox
2728fc75643SMatthew Wilcox (Oracle) while (node) {
27358d6ea30SMatthew Wilcox XA_NODE_BUG_ON(node, !list_empty(&node->private_list));
2748fc75643SMatthew Wilcox (Oracle) next = rcu_dereference_raw(node->parent);
2758fc75643SMatthew Wilcox (Oracle) radix_tree_node_rcu_free(&node->rcu_head);
2768fc75643SMatthew Wilcox (Oracle) xas->xa_alloc = node = next;
2778fc75643SMatthew Wilcox (Oracle) }
27858d6ea30SMatthew Wilcox }
27958d6ea30SMatthew Wilcox
28058d6ea30SMatthew Wilcox /**
28158d6ea30SMatthew Wilcox * xas_nomem() - Allocate memory if needed.
28258d6ea30SMatthew Wilcox * @xas: XArray operation state.
28358d6ea30SMatthew Wilcox * @gfp: Memory allocation flags.
28458d6ea30SMatthew Wilcox *
28558d6ea30SMatthew Wilcox * If we need to add new nodes to the XArray, we try to allocate memory
28658d6ea30SMatthew Wilcox * with GFP_NOWAIT while holding the lock, which will usually succeed.
28758d6ea30SMatthew Wilcox * If it fails, @xas is flagged as needing memory to continue. The caller
28858d6ea30SMatthew Wilcox * should drop the lock and call xas_nomem(). If xas_nomem() succeeds,
28958d6ea30SMatthew Wilcox * the caller should retry the operation.
29058d6ea30SMatthew Wilcox *
29158d6ea30SMatthew Wilcox * Forward progress is guaranteed as one node is allocated here and
29258d6ea30SMatthew Wilcox * stored in the xa_state where it will be found by xas_alloc(). More
29358d6ea30SMatthew Wilcox * nodes will likely be found in the slab allocator, but we do not tie
29458d6ea30SMatthew Wilcox * them up here.
29558d6ea30SMatthew Wilcox *
29658d6ea30SMatthew Wilcox * Return: true if memory was needed, and was successfully allocated.
29758d6ea30SMatthew Wilcox */
xas_nomem(struct xa_state * xas,gfp_t gfp)29858d6ea30SMatthew Wilcox bool xas_nomem(struct xa_state *xas, gfp_t gfp)
29958d6ea30SMatthew Wilcox {
30058d6ea30SMatthew Wilcox if (xas->xa_node != XA_ERROR(-ENOMEM)) {
30158d6ea30SMatthew Wilcox xas_destroy(xas);
30258d6ea30SMatthew Wilcox return false;
30358d6ea30SMatthew Wilcox }
3047b785645SJohannes Weiner if (xas->xa->xa_flags & XA_FLAGS_ACCOUNT)
3057b785645SJohannes Weiner gfp |= __GFP_ACCOUNT;
3069bbdc0f3SMuchun Song xas->xa_alloc = kmem_cache_alloc_lru(radix_tree_node_cachep, xas->xa_lru, gfp);
30758d6ea30SMatthew Wilcox if (!xas->xa_alloc)
30858d6ea30SMatthew Wilcox return false;
3098fc75643SMatthew Wilcox (Oracle) xas->xa_alloc->parent = NULL;
31058d6ea30SMatthew Wilcox XA_NODE_BUG_ON(xas->xa_alloc, !list_empty(&xas->xa_alloc->private_list));
31158d6ea30SMatthew Wilcox xas->xa_node = XAS_RESTART;
31258d6ea30SMatthew Wilcox return true;
31358d6ea30SMatthew Wilcox }
31458d6ea30SMatthew Wilcox EXPORT_SYMBOL_GPL(xas_nomem);
31558d6ea30SMatthew Wilcox
31658d6ea30SMatthew Wilcox /*
31758d6ea30SMatthew Wilcox * __xas_nomem() - Drop locks and allocate memory if needed.
31858d6ea30SMatthew Wilcox * @xas: XArray operation state.
31958d6ea30SMatthew Wilcox * @gfp: Memory allocation flags.
32058d6ea30SMatthew Wilcox *
32158d6ea30SMatthew Wilcox * Internal variant of xas_nomem().
32258d6ea30SMatthew Wilcox *
32358d6ea30SMatthew Wilcox * Return: true if memory was needed, and was successfully allocated.
32458d6ea30SMatthew Wilcox */
__xas_nomem(struct xa_state * xas,gfp_t gfp)32558d6ea30SMatthew Wilcox static bool __xas_nomem(struct xa_state *xas, gfp_t gfp)
32658d6ea30SMatthew Wilcox __must_hold(xas->xa->xa_lock)
32758d6ea30SMatthew Wilcox {
32858d6ea30SMatthew Wilcox unsigned int lock_type = xa_lock_type(xas->xa);
32958d6ea30SMatthew Wilcox
33058d6ea30SMatthew Wilcox if (xas->xa_node != XA_ERROR(-ENOMEM)) {
33158d6ea30SMatthew Wilcox xas_destroy(xas);
33258d6ea30SMatthew Wilcox return false;
33358d6ea30SMatthew Wilcox }
3347b785645SJohannes Weiner if (xas->xa->xa_flags & XA_FLAGS_ACCOUNT)
3357b785645SJohannes Weiner gfp |= __GFP_ACCOUNT;
33658d6ea30SMatthew Wilcox if (gfpflags_allow_blocking(gfp)) {
33758d6ea30SMatthew Wilcox xas_unlock_type(xas, lock_type);
3389bbdc0f3SMuchun Song xas->xa_alloc = kmem_cache_alloc_lru(radix_tree_node_cachep, xas->xa_lru, gfp);
33958d6ea30SMatthew Wilcox xas_lock_type(xas, lock_type);
34058d6ea30SMatthew Wilcox } else {
3419bbdc0f3SMuchun Song xas->xa_alloc = kmem_cache_alloc_lru(radix_tree_node_cachep, xas->xa_lru, gfp);
34258d6ea30SMatthew Wilcox }
34358d6ea30SMatthew Wilcox if (!xas->xa_alloc)
34458d6ea30SMatthew Wilcox return false;
3458fc75643SMatthew Wilcox (Oracle) xas->xa_alloc->parent = NULL;
34658d6ea30SMatthew Wilcox XA_NODE_BUG_ON(xas->xa_alloc, !list_empty(&xas->xa_alloc->private_list));
34758d6ea30SMatthew Wilcox xas->xa_node = XAS_RESTART;
34858d6ea30SMatthew Wilcox return true;
34958d6ea30SMatthew Wilcox }
35058d6ea30SMatthew Wilcox
xas_update(struct xa_state * xas,struct xa_node * node)35158d6ea30SMatthew Wilcox static void xas_update(struct xa_state *xas, struct xa_node *node)
35258d6ea30SMatthew Wilcox {
35358d6ea30SMatthew Wilcox if (xas->xa_update)
35458d6ea30SMatthew Wilcox xas->xa_update(node);
35558d6ea30SMatthew Wilcox else
35658d6ea30SMatthew Wilcox XA_NODE_BUG_ON(node, !list_empty(&node->private_list));
35758d6ea30SMatthew Wilcox }
35858d6ea30SMatthew Wilcox
xas_alloc(struct xa_state * xas,unsigned int shift)35958d6ea30SMatthew Wilcox static void *xas_alloc(struct xa_state *xas, unsigned int shift)
36058d6ea30SMatthew Wilcox {
36158d6ea30SMatthew Wilcox struct xa_node *parent = xas->xa_node;
36258d6ea30SMatthew Wilcox struct xa_node *node = xas->xa_alloc;
36358d6ea30SMatthew Wilcox
36458d6ea30SMatthew Wilcox if (xas_invalid(xas))
36558d6ea30SMatthew Wilcox return NULL;
36658d6ea30SMatthew Wilcox
36758d6ea30SMatthew Wilcox if (node) {
36858d6ea30SMatthew Wilcox xas->xa_alloc = NULL;
36958d6ea30SMatthew Wilcox } else {
3707b785645SJohannes Weiner gfp_t gfp = GFP_NOWAIT | __GFP_NOWARN;
3717b785645SJohannes Weiner
3727b785645SJohannes Weiner if (xas->xa->xa_flags & XA_FLAGS_ACCOUNT)
3737b785645SJohannes Weiner gfp |= __GFP_ACCOUNT;
3747b785645SJohannes Weiner
3759bbdc0f3SMuchun Song node = kmem_cache_alloc_lru(radix_tree_node_cachep, xas->xa_lru, gfp);
37658d6ea30SMatthew Wilcox if (!node) {
37758d6ea30SMatthew Wilcox xas_set_err(xas, -ENOMEM);
37858d6ea30SMatthew Wilcox return NULL;
37958d6ea30SMatthew Wilcox }
38058d6ea30SMatthew Wilcox }
38158d6ea30SMatthew Wilcox
38258d6ea30SMatthew Wilcox if (parent) {
38358d6ea30SMatthew Wilcox node->offset = xas->xa_offset;
38458d6ea30SMatthew Wilcox parent->count++;
38558d6ea30SMatthew Wilcox XA_NODE_BUG_ON(node, parent->count > XA_CHUNK_SIZE);
38658d6ea30SMatthew Wilcox xas_update(xas, parent);
38758d6ea30SMatthew Wilcox }
38858d6ea30SMatthew Wilcox XA_NODE_BUG_ON(node, shift > BITS_PER_LONG);
38958d6ea30SMatthew Wilcox XA_NODE_BUG_ON(node, !list_empty(&node->private_list));
39058d6ea30SMatthew Wilcox node->shift = shift;
39158d6ea30SMatthew Wilcox node->count = 0;
39258d6ea30SMatthew Wilcox node->nr_values = 0;
39358d6ea30SMatthew Wilcox RCU_INIT_POINTER(node->parent, xas->xa_node);
39458d6ea30SMatthew Wilcox node->array = xas->xa;
39558d6ea30SMatthew Wilcox
39658d6ea30SMatthew Wilcox return node;
39758d6ea30SMatthew Wilcox }
39858d6ea30SMatthew Wilcox
3990e9446c3SMatthew Wilcox #ifdef CONFIG_XARRAY_MULTI
4000e9446c3SMatthew Wilcox /* Returns the number of indices covered by a given xa_state */
xas_size(const struct xa_state * xas)4010e9446c3SMatthew Wilcox static unsigned long xas_size(const struct xa_state *xas)
4020e9446c3SMatthew Wilcox {
4030e9446c3SMatthew Wilcox return (xas->xa_sibs + 1UL) << xas->xa_shift;
4040e9446c3SMatthew Wilcox }
4050e9446c3SMatthew Wilcox #endif
4060e9446c3SMatthew Wilcox
40758d6ea30SMatthew Wilcox /*
40858d6ea30SMatthew Wilcox * Use this to calculate the maximum index that will need to be created
40958d6ea30SMatthew Wilcox * in order to add the entry described by @xas. Because we cannot store a
4108fc75643SMatthew Wilcox (Oracle) * multi-index entry at index 0, the calculation is a little more complex
41158d6ea30SMatthew Wilcox * than you might expect.
41258d6ea30SMatthew Wilcox */
xas_max(struct xa_state * xas)41358d6ea30SMatthew Wilcox static unsigned long xas_max(struct xa_state *xas)
41458d6ea30SMatthew Wilcox {
41558d6ea30SMatthew Wilcox unsigned long max = xas->xa_index;
41658d6ea30SMatthew Wilcox
41758d6ea30SMatthew Wilcox #ifdef CONFIG_XARRAY_MULTI
41858d6ea30SMatthew Wilcox if (xas->xa_shift || xas->xa_sibs) {
4190e9446c3SMatthew Wilcox unsigned long mask = xas_size(xas) - 1;
42058d6ea30SMatthew Wilcox max |= mask;
42158d6ea30SMatthew Wilcox if (mask == max)
42258d6ea30SMatthew Wilcox max++;
42358d6ea30SMatthew Wilcox }
42458d6ea30SMatthew Wilcox #endif
42558d6ea30SMatthew Wilcox
42658d6ea30SMatthew Wilcox return max;
42758d6ea30SMatthew Wilcox }
42858d6ea30SMatthew Wilcox
42958d6ea30SMatthew Wilcox /* The maximum index that can be contained in the array without expanding it */
max_index(void * entry)43058d6ea30SMatthew Wilcox static unsigned long max_index(void *entry)
43158d6ea30SMatthew Wilcox {
43258d6ea30SMatthew Wilcox if (!xa_is_node(entry))
43358d6ea30SMatthew Wilcox return 0;
43458d6ea30SMatthew Wilcox return (XA_CHUNK_SIZE << xa_to_node(entry)->shift) - 1;
43558d6ea30SMatthew Wilcox }
43658d6ea30SMatthew Wilcox
xas_shrink(struct xa_state * xas)43758d6ea30SMatthew Wilcox static void xas_shrink(struct xa_state *xas)
43858d6ea30SMatthew Wilcox {
43958d6ea30SMatthew Wilcox struct xarray *xa = xas->xa;
44058d6ea30SMatthew Wilcox struct xa_node *node = xas->xa_node;
44158d6ea30SMatthew Wilcox
44258d6ea30SMatthew Wilcox for (;;) {
44358d6ea30SMatthew Wilcox void *entry;
44458d6ea30SMatthew Wilcox
44558d6ea30SMatthew Wilcox XA_NODE_BUG_ON(node, node->count > XA_CHUNK_SIZE);
44658d6ea30SMatthew Wilcox if (node->count != 1)
44758d6ea30SMatthew Wilcox break;
44858d6ea30SMatthew Wilcox entry = xa_entry_locked(xa, node, 0);
44958d6ea30SMatthew Wilcox if (!entry)
45058d6ea30SMatthew Wilcox break;
45158d6ea30SMatthew Wilcox if (!xa_is_node(entry) && node->shift)
45258d6ea30SMatthew Wilcox break;
4533ccaf57aSMatthew Wilcox if (xa_is_zero(entry) && xa_zero_busy(xa))
4543ccaf57aSMatthew Wilcox entry = NULL;
45558d6ea30SMatthew Wilcox xas->xa_node = XAS_BOUNDS;
45658d6ea30SMatthew Wilcox
45758d6ea30SMatthew Wilcox RCU_INIT_POINTER(xa->xa_head, entry);
458371c752dSMatthew Wilcox if (xa_track_free(xa) && !node_get_mark(node, 0, XA_FREE_MARK))
459371c752dSMatthew Wilcox xa_mark_clear(xa, XA_FREE_MARK);
46058d6ea30SMatthew Wilcox
46158d6ea30SMatthew Wilcox node->count = 0;
46258d6ea30SMatthew Wilcox node->nr_values = 0;
46358d6ea30SMatthew Wilcox if (!xa_is_node(entry))
46458d6ea30SMatthew Wilcox RCU_INIT_POINTER(node->slots[0], XA_RETRY_ENTRY);
46558d6ea30SMatthew Wilcox xas_update(xas, node);
46658d6ea30SMatthew Wilcox xa_node_free(node);
46758d6ea30SMatthew Wilcox if (!xa_is_node(entry))
46858d6ea30SMatthew Wilcox break;
46958d6ea30SMatthew Wilcox node = xa_to_node(entry);
47058d6ea30SMatthew Wilcox node->parent = NULL;
47158d6ea30SMatthew Wilcox }
47258d6ea30SMatthew Wilcox }
47358d6ea30SMatthew Wilcox
47458d6ea30SMatthew Wilcox /*
47558d6ea30SMatthew Wilcox * xas_delete_node() - Attempt to delete an xa_node
47658d6ea30SMatthew Wilcox * @xas: Array operation state.
47758d6ea30SMatthew Wilcox *
47858d6ea30SMatthew Wilcox * Attempts to delete the @xas->xa_node. This will fail if xa->node has
47958d6ea30SMatthew Wilcox * a non-zero reference count.
48058d6ea30SMatthew Wilcox */
xas_delete_node(struct xa_state * xas)48158d6ea30SMatthew Wilcox static void xas_delete_node(struct xa_state *xas)
48258d6ea30SMatthew Wilcox {
48358d6ea30SMatthew Wilcox struct xa_node *node = xas->xa_node;
48458d6ea30SMatthew Wilcox
48558d6ea30SMatthew Wilcox for (;;) {
48658d6ea30SMatthew Wilcox struct xa_node *parent;
48758d6ea30SMatthew Wilcox
48858d6ea30SMatthew Wilcox XA_NODE_BUG_ON(node, node->count > XA_CHUNK_SIZE);
48958d6ea30SMatthew Wilcox if (node->count)
49058d6ea30SMatthew Wilcox break;
49158d6ea30SMatthew Wilcox
49258d6ea30SMatthew Wilcox parent = xa_parent_locked(xas->xa, node);
49358d6ea30SMatthew Wilcox xas->xa_node = parent;
49458d6ea30SMatthew Wilcox xas->xa_offset = node->offset;
49558d6ea30SMatthew Wilcox xa_node_free(node);
49658d6ea30SMatthew Wilcox
49758d6ea30SMatthew Wilcox if (!parent) {
49858d6ea30SMatthew Wilcox xas->xa->xa_head = NULL;
49958d6ea30SMatthew Wilcox xas->xa_node = XAS_BOUNDS;
50058d6ea30SMatthew Wilcox return;
50158d6ea30SMatthew Wilcox }
50258d6ea30SMatthew Wilcox
50358d6ea30SMatthew Wilcox parent->slots[xas->xa_offset] = NULL;
50458d6ea30SMatthew Wilcox parent->count--;
50558d6ea30SMatthew Wilcox XA_NODE_BUG_ON(parent, parent->count > XA_CHUNK_SIZE);
50658d6ea30SMatthew Wilcox node = parent;
50758d6ea30SMatthew Wilcox xas_update(xas, node);
50858d6ea30SMatthew Wilcox }
50958d6ea30SMatthew Wilcox
51058d6ea30SMatthew Wilcox if (!node->parent)
51158d6ea30SMatthew Wilcox xas_shrink(xas);
51258d6ea30SMatthew Wilcox }
51358d6ea30SMatthew Wilcox
51458d6ea30SMatthew Wilcox /**
51558d6ea30SMatthew Wilcox * xas_free_nodes() - Free this node and all nodes that it references
51658d6ea30SMatthew Wilcox * @xas: Array operation state.
51758d6ea30SMatthew Wilcox * @top: Node to free
51858d6ea30SMatthew Wilcox *
51958d6ea30SMatthew Wilcox * This node has been removed from the tree. We must now free it and all
52058d6ea30SMatthew Wilcox * of its subnodes. There may be RCU walkers with references into the tree,
52158d6ea30SMatthew Wilcox * so we must replace all entries with retry markers.
52258d6ea30SMatthew Wilcox */
xas_free_nodes(struct xa_state * xas,struct xa_node * top)52358d6ea30SMatthew Wilcox static void xas_free_nodes(struct xa_state *xas, struct xa_node *top)
52458d6ea30SMatthew Wilcox {
52558d6ea30SMatthew Wilcox unsigned int offset = 0;
52658d6ea30SMatthew Wilcox struct xa_node *node = top;
52758d6ea30SMatthew Wilcox
52858d6ea30SMatthew Wilcox for (;;) {
52958d6ea30SMatthew Wilcox void *entry = xa_entry_locked(xas->xa, node, offset);
53058d6ea30SMatthew Wilcox
53176b4e529SMatthew Wilcox if (node->shift && xa_is_node(entry)) {
53258d6ea30SMatthew Wilcox node = xa_to_node(entry);
53358d6ea30SMatthew Wilcox offset = 0;
53458d6ea30SMatthew Wilcox continue;
53558d6ea30SMatthew Wilcox }
53658d6ea30SMatthew Wilcox if (entry)
53758d6ea30SMatthew Wilcox RCU_INIT_POINTER(node->slots[offset], XA_RETRY_ENTRY);
53858d6ea30SMatthew Wilcox offset++;
53958d6ea30SMatthew Wilcox while (offset == XA_CHUNK_SIZE) {
54058d6ea30SMatthew Wilcox struct xa_node *parent;
54158d6ea30SMatthew Wilcox
54258d6ea30SMatthew Wilcox parent = xa_parent_locked(xas->xa, node);
54358d6ea30SMatthew Wilcox offset = node->offset + 1;
54458d6ea30SMatthew Wilcox node->count = 0;
54558d6ea30SMatthew Wilcox node->nr_values = 0;
54658d6ea30SMatthew Wilcox xas_update(xas, node);
54758d6ea30SMatthew Wilcox xa_node_free(node);
54858d6ea30SMatthew Wilcox if (node == top)
54958d6ea30SMatthew Wilcox return;
55058d6ea30SMatthew Wilcox node = parent;
55158d6ea30SMatthew Wilcox }
55258d6ea30SMatthew Wilcox }
55358d6ea30SMatthew Wilcox }
55458d6ea30SMatthew Wilcox
55558d6ea30SMatthew Wilcox /*
55658d6ea30SMatthew Wilcox * xas_expand adds nodes to the head of the tree until it has reached
55758d6ea30SMatthew Wilcox * sufficient height to be able to contain @xas->xa_index
55858d6ea30SMatthew Wilcox */
xas_expand(struct xa_state * xas,void * head)55958d6ea30SMatthew Wilcox static int xas_expand(struct xa_state *xas, void *head)
56058d6ea30SMatthew Wilcox {
56158d6ea30SMatthew Wilcox struct xarray *xa = xas->xa;
56258d6ea30SMatthew Wilcox struct xa_node *node = NULL;
56358d6ea30SMatthew Wilcox unsigned int shift = 0;
56458d6ea30SMatthew Wilcox unsigned long max = xas_max(xas);
56558d6ea30SMatthew Wilcox
56658d6ea30SMatthew Wilcox if (!head) {
56758d6ea30SMatthew Wilcox if (max == 0)
56858d6ea30SMatthew Wilcox return 0;
56958d6ea30SMatthew Wilcox while ((max >> shift) >= XA_CHUNK_SIZE)
57058d6ea30SMatthew Wilcox shift += XA_CHUNK_SHIFT;
57158d6ea30SMatthew Wilcox return shift + XA_CHUNK_SHIFT;
57258d6ea30SMatthew Wilcox } else if (xa_is_node(head)) {
57358d6ea30SMatthew Wilcox node = xa_to_node(head);
57458d6ea30SMatthew Wilcox shift = node->shift + XA_CHUNK_SHIFT;
57558d6ea30SMatthew Wilcox }
57658d6ea30SMatthew Wilcox xas->xa_node = NULL;
57758d6ea30SMatthew Wilcox
57858d6ea30SMatthew Wilcox while (max > max_index(head)) {
57958d6ea30SMatthew Wilcox xa_mark_t mark = 0;
58058d6ea30SMatthew Wilcox
58158d6ea30SMatthew Wilcox XA_NODE_BUG_ON(node, shift > BITS_PER_LONG);
58258d6ea30SMatthew Wilcox node = xas_alloc(xas, shift);
58358d6ea30SMatthew Wilcox if (!node)
58458d6ea30SMatthew Wilcox return -ENOMEM;
58558d6ea30SMatthew Wilcox
58658d6ea30SMatthew Wilcox node->count = 1;
58758d6ea30SMatthew Wilcox if (xa_is_value(head))
58858d6ea30SMatthew Wilcox node->nr_values = 1;
58958d6ea30SMatthew Wilcox RCU_INIT_POINTER(node->slots[0], head);
59058d6ea30SMatthew Wilcox
59158d6ea30SMatthew Wilcox /* Propagate the aggregated mark info to the new child */
59258d6ea30SMatthew Wilcox for (;;) {
593371c752dSMatthew Wilcox if (xa_track_free(xa) && mark == XA_FREE_MARK) {
594371c752dSMatthew Wilcox node_mark_all(node, XA_FREE_MARK);
595371c752dSMatthew Wilcox if (!xa_marked(xa, XA_FREE_MARK)) {
596371c752dSMatthew Wilcox node_clear_mark(node, 0, XA_FREE_MARK);
597371c752dSMatthew Wilcox xa_mark_set(xa, XA_FREE_MARK);
598371c752dSMatthew Wilcox }
599371c752dSMatthew Wilcox } else if (xa_marked(xa, mark)) {
60058d6ea30SMatthew Wilcox node_set_mark(node, 0, mark);
601371c752dSMatthew Wilcox }
60258d6ea30SMatthew Wilcox if (mark == XA_MARK_MAX)
60358d6ea30SMatthew Wilcox break;
60458d6ea30SMatthew Wilcox mark_inc(mark);
60558d6ea30SMatthew Wilcox }
60658d6ea30SMatthew Wilcox
60758d6ea30SMatthew Wilcox /*
60858d6ea30SMatthew Wilcox * Now that the new node is fully initialised, we can add
60958d6ea30SMatthew Wilcox * it to the tree
61058d6ea30SMatthew Wilcox */
61158d6ea30SMatthew Wilcox if (xa_is_node(head)) {
61258d6ea30SMatthew Wilcox xa_to_node(head)->offset = 0;
61358d6ea30SMatthew Wilcox rcu_assign_pointer(xa_to_node(head)->parent, node);
61458d6ea30SMatthew Wilcox }
61558d6ea30SMatthew Wilcox head = xa_mk_node(node);
61658d6ea30SMatthew Wilcox rcu_assign_pointer(xa->xa_head, head);
61758d6ea30SMatthew Wilcox xas_update(xas, node);
61858d6ea30SMatthew Wilcox
61958d6ea30SMatthew Wilcox shift += XA_CHUNK_SHIFT;
62058d6ea30SMatthew Wilcox }
62158d6ea30SMatthew Wilcox
62258d6ea30SMatthew Wilcox xas->xa_node = node;
62358d6ea30SMatthew Wilcox return shift;
62458d6ea30SMatthew Wilcox }
62558d6ea30SMatthew Wilcox
62658d6ea30SMatthew Wilcox /*
62758d6ea30SMatthew Wilcox * xas_create() - Create a slot to store an entry in.
62858d6ea30SMatthew Wilcox * @xas: XArray operation state.
62976b4e529SMatthew Wilcox * @allow_root: %true if we can store the entry in the root directly
63058d6ea30SMatthew Wilcox *
63158d6ea30SMatthew Wilcox * Most users will not need to call this function directly, as it is called
63258d6ea30SMatthew Wilcox * by xas_store(). It is useful for doing conditional store operations
63358d6ea30SMatthew Wilcox * (see the xa_cmpxchg() implementation for an example).
63458d6ea30SMatthew Wilcox *
63558d6ea30SMatthew Wilcox * Return: If the slot already existed, returns the contents of this slot.
636804dfaf0SMatthew Wilcox * If the slot was newly created, returns %NULL. If it failed to create the
637804dfaf0SMatthew Wilcox * slot, returns %NULL and indicates the error in @xas.
63858d6ea30SMatthew Wilcox */
xas_create(struct xa_state * xas,bool allow_root)63976b4e529SMatthew Wilcox static void *xas_create(struct xa_state *xas, bool allow_root)
64058d6ea30SMatthew Wilcox {
64158d6ea30SMatthew Wilcox struct xarray *xa = xas->xa;
64258d6ea30SMatthew Wilcox void *entry;
64358d6ea30SMatthew Wilcox void __rcu **slot;
64458d6ea30SMatthew Wilcox struct xa_node *node = xas->xa_node;
64558d6ea30SMatthew Wilcox int shift;
64658d6ea30SMatthew Wilcox unsigned int order = xas->xa_shift;
64758d6ea30SMatthew Wilcox
64858d6ea30SMatthew Wilcox if (xas_top(node)) {
64958d6ea30SMatthew Wilcox entry = xa_head_locked(xa);
65058d6ea30SMatthew Wilcox xas->xa_node = NULL;
6513ccaf57aSMatthew Wilcox if (!entry && xa_zero_busy(xa))
6523ccaf57aSMatthew Wilcox entry = XA_ZERO_ENTRY;
65358d6ea30SMatthew Wilcox shift = xas_expand(xas, entry);
65458d6ea30SMatthew Wilcox if (shift < 0)
65558d6ea30SMatthew Wilcox return NULL;
65676b4e529SMatthew Wilcox if (!shift && !allow_root)
65776b4e529SMatthew Wilcox shift = XA_CHUNK_SHIFT;
65858d6ea30SMatthew Wilcox entry = xa_head_locked(xa);
65958d6ea30SMatthew Wilcox slot = &xa->xa_head;
66058d6ea30SMatthew Wilcox } else if (xas_error(xas)) {
66158d6ea30SMatthew Wilcox return NULL;
66258d6ea30SMatthew Wilcox } else if (node) {
66358d6ea30SMatthew Wilcox unsigned int offset = xas->xa_offset;
66458d6ea30SMatthew Wilcox
66558d6ea30SMatthew Wilcox shift = node->shift;
66658d6ea30SMatthew Wilcox entry = xa_entry_locked(xa, node, offset);
66758d6ea30SMatthew Wilcox slot = &node->slots[offset];
66858d6ea30SMatthew Wilcox } else {
66958d6ea30SMatthew Wilcox shift = 0;
67058d6ea30SMatthew Wilcox entry = xa_head_locked(xa);
67158d6ea30SMatthew Wilcox slot = &xa->xa_head;
67258d6ea30SMatthew Wilcox }
67358d6ea30SMatthew Wilcox
67458d6ea30SMatthew Wilcox while (shift > order) {
67558d6ea30SMatthew Wilcox shift -= XA_CHUNK_SHIFT;
67658d6ea30SMatthew Wilcox if (!entry) {
67758d6ea30SMatthew Wilcox node = xas_alloc(xas, shift);
67858d6ea30SMatthew Wilcox if (!node)
67958d6ea30SMatthew Wilcox break;
680371c752dSMatthew Wilcox if (xa_track_free(xa))
681371c752dSMatthew Wilcox node_mark_all(node, XA_FREE_MARK);
68258d6ea30SMatthew Wilcox rcu_assign_pointer(*slot, xa_mk_node(node));
68358d6ea30SMatthew Wilcox } else if (xa_is_node(entry)) {
68458d6ea30SMatthew Wilcox node = xa_to_node(entry);
68558d6ea30SMatthew Wilcox } else {
68658d6ea30SMatthew Wilcox break;
68758d6ea30SMatthew Wilcox }
68858d6ea30SMatthew Wilcox entry = xas_descend(xas, node);
68958d6ea30SMatthew Wilcox slot = &node->slots[xas->xa_offset];
69058d6ea30SMatthew Wilcox }
69158d6ea30SMatthew Wilcox
69258d6ea30SMatthew Wilcox return entry;
69358d6ea30SMatthew Wilcox }
69458d6ea30SMatthew Wilcox
6952264f513SMatthew Wilcox /**
6962264f513SMatthew Wilcox * xas_create_range() - Ensure that stores to this range will succeed
6972264f513SMatthew Wilcox * @xas: XArray operation state.
6982264f513SMatthew Wilcox *
6992264f513SMatthew Wilcox * Creates all of the slots in the range covered by @xas. Sets @xas to
7002264f513SMatthew Wilcox * create single-index entries and positions it at the beginning of the
7012264f513SMatthew Wilcox * range. This is for the benefit of users which have not yet been
7022264f513SMatthew Wilcox * converted to use multi-index entries.
7032264f513SMatthew Wilcox */
xas_create_range(struct xa_state * xas)7042264f513SMatthew Wilcox void xas_create_range(struct xa_state *xas)
7052264f513SMatthew Wilcox {
7062264f513SMatthew Wilcox unsigned long index = xas->xa_index;
7072264f513SMatthew Wilcox unsigned char shift = xas->xa_shift;
7082264f513SMatthew Wilcox unsigned char sibs = xas->xa_sibs;
7092264f513SMatthew Wilcox
71084c34df1SMatthew Wilcox (Oracle) xas->xa_index |= ((sibs + 1UL) << shift) - 1;
7112264f513SMatthew Wilcox if (xas_is_node(xas) && xas->xa_node->shift == xas->xa_shift)
7122264f513SMatthew Wilcox xas->xa_offset |= sibs;
7132264f513SMatthew Wilcox xas->xa_shift = 0;
7142264f513SMatthew Wilcox xas->xa_sibs = 0;
7152264f513SMatthew Wilcox
7162264f513SMatthew Wilcox for (;;) {
71776b4e529SMatthew Wilcox xas_create(xas, true);
7182264f513SMatthew Wilcox if (xas_error(xas))
7192264f513SMatthew Wilcox goto restore;
7202264f513SMatthew Wilcox if (xas->xa_index <= (index | XA_CHUNK_MASK))
7212264f513SMatthew Wilcox goto success;
7222264f513SMatthew Wilcox xas->xa_index -= XA_CHUNK_SIZE;
7232264f513SMatthew Wilcox
7242264f513SMatthew Wilcox for (;;) {
7252264f513SMatthew Wilcox struct xa_node *node = xas->xa_node;
7263e3c6580SMatthew Wilcox (Oracle) if (node->shift >= shift)
7273e3c6580SMatthew Wilcox (Oracle) break;
7282264f513SMatthew Wilcox xas->xa_node = xa_parent_locked(xas->xa, node);
7292264f513SMatthew Wilcox xas->xa_offset = node->offset - 1;
7302264f513SMatthew Wilcox if (node->offset != 0)
7312264f513SMatthew Wilcox break;
7322264f513SMatthew Wilcox }
7332264f513SMatthew Wilcox }
7342264f513SMatthew Wilcox
7352264f513SMatthew Wilcox restore:
7362264f513SMatthew Wilcox xas->xa_shift = shift;
7372264f513SMatthew Wilcox xas->xa_sibs = sibs;
7382264f513SMatthew Wilcox xas->xa_index = index;
7392264f513SMatthew Wilcox return;
7402264f513SMatthew Wilcox success:
7412264f513SMatthew Wilcox xas->xa_index = index;
7422264f513SMatthew Wilcox if (xas->xa_node)
7432264f513SMatthew Wilcox xas_set_offset(xas);
7442264f513SMatthew Wilcox }
7452264f513SMatthew Wilcox EXPORT_SYMBOL_GPL(xas_create_range);
7462264f513SMatthew Wilcox
update_node(struct xa_state * xas,struct xa_node * node,int count,int values)74758d6ea30SMatthew Wilcox static void update_node(struct xa_state *xas, struct xa_node *node,
74858d6ea30SMatthew Wilcox int count, int values)
74958d6ea30SMatthew Wilcox {
75058d6ea30SMatthew Wilcox if (!node || (!count && !values))
75158d6ea30SMatthew Wilcox return;
75258d6ea30SMatthew Wilcox
75358d6ea30SMatthew Wilcox node->count += count;
75458d6ea30SMatthew Wilcox node->nr_values += values;
75558d6ea30SMatthew Wilcox XA_NODE_BUG_ON(node, node->count > XA_CHUNK_SIZE);
75658d6ea30SMatthew Wilcox XA_NODE_BUG_ON(node, node->nr_values > XA_CHUNK_SIZE);
75758d6ea30SMatthew Wilcox xas_update(xas, node);
75858d6ea30SMatthew Wilcox if (count < 0)
75958d6ea30SMatthew Wilcox xas_delete_node(xas);
76058d6ea30SMatthew Wilcox }
76158d6ea30SMatthew Wilcox
76258d6ea30SMatthew Wilcox /**
76358d6ea30SMatthew Wilcox * xas_store() - Store this entry in the XArray.
76458d6ea30SMatthew Wilcox * @xas: XArray operation state.
76558d6ea30SMatthew Wilcox * @entry: New entry.
76658d6ea30SMatthew Wilcox *
76758d6ea30SMatthew Wilcox * If @xas is operating on a multi-index entry, the entry returned by this
76858d6ea30SMatthew Wilcox * function is essentially meaningless (it may be an internal entry or it
76958d6ea30SMatthew Wilcox * may be %NULL, even if there are non-NULL entries at some of the indices
77058d6ea30SMatthew Wilcox * covered by the range). This is not a problem for any current users,
77158d6ea30SMatthew Wilcox * and can be changed if needed.
77258d6ea30SMatthew Wilcox *
77358d6ea30SMatthew Wilcox * Return: The old entry at this index.
77458d6ea30SMatthew Wilcox */
xas_store(struct xa_state * xas,void * entry)77558d6ea30SMatthew Wilcox void *xas_store(struct xa_state *xas, void *entry)
77658d6ea30SMatthew Wilcox {
77758d6ea30SMatthew Wilcox struct xa_node *node;
77858d6ea30SMatthew Wilcox void __rcu **slot = &xas->xa->xa_head;
77958d6ea30SMatthew Wilcox unsigned int offset, max;
78058d6ea30SMatthew Wilcox int count = 0;
78158d6ea30SMatthew Wilcox int values = 0;
78258d6ea30SMatthew Wilcox void *first, *next;
78358d6ea30SMatthew Wilcox bool value = xa_is_value(entry);
78458d6ea30SMatthew Wilcox
7854a5c8d89SMatthew Wilcox if (entry) {
7864a5c8d89SMatthew Wilcox bool allow_root = !xa_is_node(entry) && !xa_is_zero(entry);
7874a5c8d89SMatthew Wilcox first = xas_create(xas, allow_root);
7884a5c8d89SMatthew Wilcox } else {
78958d6ea30SMatthew Wilcox first = xas_load(xas);
7904a5c8d89SMatthew Wilcox }
79158d6ea30SMatthew Wilcox
79258d6ea30SMatthew Wilcox if (xas_invalid(xas))
79358d6ea30SMatthew Wilcox return first;
79458d6ea30SMatthew Wilcox node = xas->xa_node;
79558d6ea30SMatthew Wilcox if (node && (xas->xa_shift < node->shift))
79658d6ea30SMatthew Wilcox xas->xa_sibs = 0;
79758d6ea30SMatthew Wilcox if ((first == entry) && !xas->xa_sibs)
79858d6ea30SMatthew Wilcox return first;
79958d6ea30SMatthew Wilcox
80058d6ea30SMatthew Wilcox next = first;
80158d6ea30SMatthew Wilcox offset = xas->xa_offset;
80258d6ea30SMatthew Wilcox max = xas->xa_offset + xas->xa_sibs;
80358d6ea30SMatthew Wilcox if (node) {
80458d6ea30SMatthew Wilcox slot = &node->slots[offset];
80558d6ea30SMatthew Wilcox if (xas->xa_sibs)
80658d6ea30SMatthew Wilcox xas_squash_marks(xas);
80758d6ea30SMatthew Wilcox }
80858d6ea30SMatthew Wilcox if (!entry)
80958d6ea30SMatthew Wilcox xas_init_marks(xas);
81058d6ea30SMatthew Wilcox
81158d6ea30SMatthew Wilcox for (;;) {
81258d6ea30SMatthew Wilcox /*
81358d6ea30SMatthew Wilcox * Must clear the marks before setting the entry to NULL,
81458d6ea30SMatthew Wilcox * otherwise xas_for_each_marked may find a NULL entry and
81558d6ea30SMatthew Wilcox * stop early. rcu_assign_pointer contains a release barrier
81658d6ea30SMatthew Wilcox * so the mark clearing will appear to happen before the
81758d6ea30SMatthew Wilcox * entry is set to NULL.
81858d6ea30SMatthew Wilcox */
81958d6ea30SMatthew Wilcox rcu_assign_pointer(*slot, entry);
8202fbe967bSMatthew Wilcox if (xa_is_node(next) && (!node || node->shift))
82158d6ea30SMatthew Wilcox xas_free_nodes(xas, xa_to_node(next));
82258d6ea30SMatthew Wilcox if (!node)
82358d6ea30SMatthew Wilcox break;
82458d6ea30SMatthew Wilcox count += !next - !entry;
82558d6ea30SMatthew Wilcox values += !xa_is_value(first) - !value;
82658d6ea30SMatthew Wilcox if (entry) {
82758d6ea30SMatthew Wilcox if (offset == max)
82858d6ea30SMatthew Wilcox break;
82958d6ea30SMatthew Wilcox if (!xa_is_sibling(entry))
83058d6ea30SMatthew Wilcox entry = xa_mk_sibling(xas->xa_offset);
83158d6ea30SMatthew Wilcox } else {
83258d6ea30SMatthew Wilcox if (offset == XA_CHUNK_MASK)
83358d6ea30SMatthew Wilcox break;
83458d6ea30SMatthew Wilcox }
83558d6ea30SMatthew Wilcox next = xa_entry_locked(xas->xa, node, ++offset);
83658d6ea30SMatthew Wilcox if (!xa_is_sibling(next)) {
83758d6ea30SMatthew Wilcox if (!entry && (offset > max))
83858d6ea30SMatthew Wilcox break;
83958d6ea30SMatthew Wilcox first = next;
84058d6ea30SMatthew Wilcox }
84158d6ea30SMatthew Wilcox slot++;
84258d6ea30SMatthew Wilcox }
84358d6ea30SMatthew Wilcox
84458d6ea30SMatthew Wilcox update_node(xas, node, count, values);
84558d6ea30SMatthew Wilcox return first;
84658d6ea30SMatthew Wilcox }
84758d6ea30SMatthew Wilcox EXPORT_SYMBOL_GPL(xas_store);
84858d6ea30SMatthew Wilcox
849f8d5d0ccSMatthew Wilcox /**
8509b89a035SMatthew Wilcox * xas_get_mark() - Returns the state of this mark.
8519b89a035SMatthew Wilcox * @xas: XArray operation state.
8529b89a035SMatthew Wilcox * @mark: Mark number.
8539b89a035SMatthew Wilcox *
8549b89a035SMatthew Wilcox * Return: true if the mark is set, false if the mark is clear or @xas
8559b89a035SMatthew Wilcox * is in an error state.
8569b89a035SMatthew Wilcox */
xas_get_mark(const struct xa_state * xas,xa_mark_t mark)8579b89a035SMatthew Wilcox bool xas_get_mark(const struct xa_state *xas, xa_mark_t mark)
8589b89a035SMatthew Wilcox {
8599b89a035SMatthew Wilcox if (xas_invalid(xas))
8609b89a035SMatthew Wilcox return false;
8619b89a035SMatthew Wilcox if (!xas->xa_node)
8629b89a035SMatthew Wilcox return xa_marked(xas->xa, mark);
8639b89a035SMatthew Wilcox return node_get_mark(xas->xa_node, xas->xa_offset, mark);
8649b89a035SMatthew Wilcox }
8659b89a035SMatthew Wilcox EXPORT_SYMBOL_GPL(xas_get_mark);
8669b89a035SMatthew Wilcox
8679b89a035SMatthew Wilcox /**
8689b89a035SMatthew Wilcox * xas_set_mark() - Sets the mark on this entry and its parents.
8699b89a035SMatthew Wilcox * @xas: XArray operation state.
8709b89a035SMatthew Wilcox * @mark: Mark number.
8719b89a035SMatthew Wilcox *
8729b89a035SMatthew Wilcox * Sets the specified mark on this entry, and walks up the tree setting it
8739b89a035SMatthew Wilcox * on all the ancestor entries. Does nothing if @xas has not been walked to
8749b89a035SMatthew Wilcox * an entry, or is in an error state.
8759b89a035SMatthew Wilcox */
xas_set_mark(const struct xa_state * xas,xa_mark_t mark)8769b89a035SMatthew Wilcox void xas_set_mark(const struct xa_state *xas, xa_mark_t mark)
8779b89a035SMatthew Wilcox {
8789b89a035SMatthew Wilcox struct xa_node *node = xas->xa_node;
8799b89a035SMatthew Wilcox unsigned int offset = xas->xa_offset;
8809b89a035SMatthew Wilcox
8819b89a035SMatthew Wilcox if (xas_invalid(xas))
8829b89a035SMatthew Wilcox return;
8839b89a035SMatthew Wilcox
8849b89a035SMatthew Wilcox while (node) {
8859b89a035SMatthew Wilcox if (node_set_mark(node, offset, mark))
8869b89a035SMatthew Wilcox return;
8879b89a035SMatthew Wilcox offset = node->offset;
8889b89a035SMatthew Wilcox node = xa_parent_locked(xas->xa, node);
8899b89a035SMatthew Wilcox }
8909b89a035SMatthew Wilcox
8919b89a035SMatthew Wilcox if (!xa_marked(xas->xa, mark))
8929b89a035SMatthew Wilcox xa_mark_set(xas->xa, mark);
8939b89a035SMatthew Wilcox }
8949b89a035SMatthew Wilcox EXPORT_SYMBOL_GPL(xas_set_mark);
8959b89a035SMatthew Wilcox
8969b89a035SMatthew Wilcox /**
8979b89a035SMatthew Wilcox * xas_clear_mark() - Clears the mark on this entry and its parents.
8989b89a035SMatthew Wilcox * @xas: XArray operation state.
8999b89a035SMatthew Wilcox * @mark: Mark number.
9009b89a035SMatthew Wilcox *
9019b89a035SMatthew Wilcox * Clears the specified mark on this entry, and walks back to the head
9029b89a035SMatthew Wilcox * attempting to clear it on all the ancestor entries. Does nothing if
9039b89a035SMatthew Wilcox * @xas has not been walked to an entry, or is in an error state.
9049b89a035SMatthew Wilcox */
xas_clear_mark(const struct xa_state * xas,xa_mark_t mark)9059b89a035SMatthew Wilcox void xas_clear_mark(const struct xa_state *xas, xa_mark_t mark)
9069b89a035SMatthew Wilcox {
9079b89a035SMatthew Wilcox struct xa_node *node = xas->xa_node;
9089b89a035SMatthew Wilcox unsigned int offset = xas->xa_offset;
9099b89a035SMatthew Wilcox
9109b89a035SMatthew Wilcox if (xas_invalid(xas))
9119b89a035SMatthew Wilcox return;
9129b89a035SMatthew Wilcox
9139b89a035SMatthew Wilcox while (node) {
9149b89a035SMatthew Wilcox if (!node_clear_mark(node, offset, mark))
9159b89a035SMatthew Wilcox return;
9169b89a035SMatthew Wilcox if (node_any_mark(node, mark))
9179b89a035SMatthew Wilcox return;
9189b89a035SMatthew Wilcox
9199b89a035SMatthew Wilcox offset = node->offset;
9209b89a035SMatthew Wilcox node = xa_parent_locked(xas->xa, node);
9219b89a035SMatthew Wilcox }
9229b89a035SMatthew Wilcox
9239b89a035SMatthew Wilcox if (xa_marked(xas->xa, mark))
9249b89a035SMatthew Wilcox xa_mark_clear(xas->xa, mark);
9259b89a035SMatthew Wilcox }
9269b89a035SMatthew Wilcox EXPORT_SYMBOL_GPL(xas_clear_mark);
9279b89a035SMatthew Wilcox
9289b89a035SMatthew Wilcox /**
92958d6ea30SMatthew Wilcox * xas_init_marks() - Initialise all marks for the entry
93058d6ea30SMatthew Wilcox * @xas: Array operations state.
93158d6ea30SMatthew Wilcox *
93258d6ea30SMatthew Wilcox * Initialise all marks for the entry specified by @xas. If we're tracking
93358d6ea30SMatthew Wilcox * free entries with a mark, we need to set it on all entries. All other
93458d6ea30SMatthew Wilcox * marks are cleared.
93558d6ea30SMatthew Wilcox *
93658d6ea30SMatthew Wilcox * This implementation is not as efficient as it could be; we may walk
93758d6ea30SMatthew Wilcox * up the tree multiple times.
93858d6ea30SMatthew Wilcox */
xas_init_marks(const struct xa_state * xas)93958d6ea30SMatthew Wilcox void xas_init_marks(const struct xa_state *xas)
94058d6ea30SMatthew Wilcox {
94158d6ea30SMatthew Wilcox xa_mark_t mark = 0;
94258d6ea30SMatthew Wilcox
94358d6ea30SMatthew Wilcox for (;;) {
944371c752dSMatthew Wilcox if (xa_track_free(xas->xa) && mark == XA_FREE_MARK)
945371c752dSMatthew Wilcox xas_set_mark(xas, mark);
946371c752dSMatthew Wilcox else
94758d6ea30SMatthew Wilcox xas_clear_mark(xas, mark);
94858d6ea30SMatthew Wilcox if (mark == XA_MARK_MAX)
94958d6ea30SMatthew Wilcox break;
95058d6ea30SMatthew Wilcox mark_inc(mark);
95158d6ea30SMatthew Wilcox }
95258d6ea30SMatthew Wilcox }
95358d6ea30SMatthew Wilcox EXPORT_SYMBOL_GPL(xas_init_marks);
95458d6ea30SMatthew Wilcox
9558fc75643SMatthew Wilcox (Oracle) #ifdef CONFIG_XARRAY_MULTI
node_get_marks(struct xa_node * node,unsigned int offset)9568fc75643SMatthew Wilcox (Oracle) static unsigned int node_get_marks(struct xa_node *node, unsigned int offset)
9578fc75643SMatthew Wilcox (Oracle) {
9588fc75643SMatthew Wilcox (Oracle) unsigned int marks = 0;
9598fc75643SMatthew Wilcox (Oracle) xa_mark_t mark = XA_MARK_0;
9608fc75643SMatthew Wilcox (Oracle)
9618fc75643SMatthew Wilcox (Oracle) for (;;) {
9628fc75643SMatthew Wilcox (Oracle) if (node_get_mark(node, offset, mark))
9638fc75643SMatthew Wilcox (Oracle) marks |= 1 << (__force unsigned int)mark;
9648fc75643SMatthew Wilcox (Oracle) if (mark == XA_MARK_MAX)
9658fc75643SMatthew Wilcox (Oracle) break;
9668fc75643SMatthew Wilcox (Oracle) mark_inc(mark);
9678fc75643SMatthew Wilcox (Oracle) }
9688fc75643SMatthew Wilcox (Oracle)
9698fc75643SMatthew Wilcox (Oracle) return marks;
9708fc75643SMatthew Wilcox (Oracle) }
9718fc75643SMatthew Wilcox (Oracle)
node_set_marks(struct xa_node * node,unsigned int offset,struct xa_node * child,unsigned int marks)9728fc75643SMatthew Wilcox (Oracle) static void node_set_marks(struct xa_node *node, unsigned int offset,
9738fc75643SMatthew Wilcox (Oracle) struct xa_node *child, unsigned int marks)
9748fc75643SMatthew Wilcox (Oracle) {
9758fc75643SMatthew Wilcox (Oracle) xa_mark_t mark = XA_MARK_0;
9768fc75643SMatthew Wilcox (Oracle)
9778fc75643SMatthew Wilcox (Oracle) for (;;) {
9788fc75643SMatthew Wilcox (Oracle) if (marks & (1 << (__force unsigned int)mark)) {
9798fc75643SMatthew Wilcox (Oracle) node_set_mark(node, offset, mark);
9808fc75643SMatthew Wilcox (Oracle) if (child)
9818fc75643SMatthew Wilcox (Oracle) node_mark_all(child, mark);
9828fc75643SMatthew Wilcox (Oracle) }
9838fc75643SMatthew Wilcox (Oracle) if (mark == XA_MARK_MAX)
9848fc75643SMatthew Wilcox (Oracle) break;
9858fc75643SMatthew Wilcox (Oracle) mark_inc(mark);
9868fc75643SMatthew Wilcox (Oracle) }
9878fc75643SMatthew Wilcox (Oracle) }
9888fc75643SMatthew Wilcox (Oracle)
9898fc75643SMatthew Wilcox (Oracle) /**
9908fc75643SMatthew Wilcox (Oracle) * xas_split_alloc() - Allocate memory for splitting an entry.
9918fc75643SMatthew Wilcox (Oracle) * @xas: XArray operation state.
9928fc75643SMatthew Wilcox (Oracle) * @entry: New entry which will be stored in the array.
99312efebabSMatthew Wilcox (Oracle) * @order: Current entry order.
9948fc75643SMatthew Wilcox (Oracle) * @gfp: Memory allocation flags.
9958fc75643SMatthew Wilcox (Oracle) *
9968fc75643SMatthew Wilcox (Oracle) * This function should be called before calling xas_split().
9978fc75643SMatthew Wilcox (Oracle) * If necessary, it will allocate new nodes (and fill them with @entry)
9988fc75643SMatthew Wilcox (Oracle) * to prepare for the upcoming split of an entry of @order size into
9998fc75643SMatthew Wilcox (Oracle) * entries of the order stored in the @xas.
10008fc75643SMatthew Wilcox (Oracle) *
10018fc75643SMatthew Wilcox (Oracle) * Context: May sleep if @gfp flags permit.
10028fc75643SMatthew Wilcox (Oracle) */
xas_split_alloc(struct xa_state * xas,void * entry,unsigned int order,gfp_t gfp)10038fc75643SMatthew Wilcox (Oracle) void xas_split_alloc(struct xa_state *xas, void *entry, unsigned int order,
10048fc75643SMatthew Wilcox (Oracle) gfp_t gfp)
10058fc75643SMatthew Wilcox (Oracle) {
10068fc75643SMatthew Wilcox (Oracle) unsigned int sibs = (1 << (order % XA_CHUNK_SHIFT)) - 1;
10078fc75643SMatthew Wilcox (Oracle) unsigned int mask = xas->xa_sibs;
10088fc75643SMatthew Wilcox (Oracle)
10098fc75643SMatthew Wilcox (Oracle) /* XXX: no support for splitting really large entries yet */
10108fc75643SMatthew Wilcox (Oracle) if (WARN_ON(xas->xa_shift + 2 * XA_CHUNK_SHIFT < order))
10118fc75643SMatthew Wilcox (Oracle) goto nomem;
10128fc75643SMatthew Wilcox (Oracle) if (xas->xa_shift + XA_CHUNK_SHIFT > order)
10138fc75643SMatthew Wilcox (Oracle) return;
10148fc75643SMatthew Wilcox (Oracle)
10158fc75643SMatthew Wilcox (Oracle) do {
10168fc75643SMatthew Wilcox (Oracle) unsigned int i;
10173012110dSMatthew Wilcox (Oracle) void *sibling = NULL;
10188fc75643SMatthew Wilcox (Oracle) struct xa_node *node;
10198fc75643SMatthew Wilcox (Oracle)
10209bbdc0f3SMuchun Song node = kmem_cache_alloc_lru(radix_tree_node_cachep, xas->xa_lru, gfp);
10218fc75643SMatthew Wilcox (Oracle) if (!node)
10228fc75643SMatthew Wilcox (Oracle) goto nomem;
10238fc75643SMatthew Wilcox (Oracle) node->array = xas->xa;
10248fc75643SMatthew Wilcox (Oracle) for (i = 0; i < XA_CHUNK_SIZE; i++) {
10258fc75643SMatthew Wilcox (Oracle) if ((i & mask) == 0) {
10268fc75643SMatthew Wilcox (Oracle) RCU_INIT_POINTER(node->slots[i], entry);
10273012110dSMatthew Wilcox (Oracle) sibling = xa_mk_sibling(i);
10288fc75643SMatthew Wilcox (Oracle) } else {
10298fc75643SMatthew Wilcox (Oracle) RCU_INIT_POINTER(node->slots[i], sibling);
10308fc75643SMatthew Wilcox (Oracle) }
10318fc75643SMatthew Wilcox (Oracle) }
10328fc75643SMatthew Wilcox (Oracle) RCU_INIT_POINTER(node->parent, xas->xa_alloc);
10338fc75643SMatthew Wilcox (Oracle) xas->xa_alloc = node;
10348fc75643SMatthew Wilcox (Oracle) } while (sibs-- > 0);
10358fc75643SMatthew Wilcox (Oracle)
10368fc75643SMatthew Wilcox (Oracle) return;
10378fc75643SMatthew Wilcox (Oracle) nomem:
10388fc75643SMatthew Wilcox (Oracle) xas_destroy(xas);
10398fc75643SMatthew Wilcox (Oracle) xas_set_err(xas, -ENOMEM);
10408fc75643SMatthew Wilcox (Oracle) }
10418fc75643SMatthew Wilcox (Oracle) EXPORT_SYMBOL_GPL(xas_split_alloc);
10428fc75643SMatthew Wilcox (Oracle)
10438fc75643SMatthew Wilcox (Oracle) /**
10448fc75643SMatthew Wilcox (Oracle) * xas_split() - Split a multi-index entry into smaller entries.
10458fc75643SMatthew Wilcox (Oracle) * @xas: XArray operation state.
10468fc75643SMatthew Wilcox (Oracle) * @entry: New entry to store in the array.
104712efebabSMatthew Wilcox (Oracle) * @order: Current entry order.
10488fc75643SMatthew Wilcox (Oracle) *
104912efebabSMatthew Wilcox (Oracle) * The size of the new entries is set in @xas. The value in @entry is
105012efebabSMatthew Wilcox (Oracle) * copied to all the replacement entries.
10518fc75643SMatthew Wilcox (Oracle) *
10528fc75643SMatthew Wilcox (Oracle) * Context: Any context. The caller should hold the xa_lock.
10538fc75643SMatthew Wilcox (Oracle) */
xas_split(struct xa_state * xas,void * entry,unsigned int order)10548fc75643SMatthew Wilcox (Oracle) void xas_split(struct xa_state *xas, void *entry, unsigned int order)
10558fc75643SMatthew Wilcox (Oracle) {
10568fc75643SMatthew Wilcox (Oracle) unsigned int sibs = (1 << (order % XA_CHUNK_SHIFT)) - 1;
10578fc75643SMatthew Wilcox (Oracle) unsigned int offset, marks;
10588fc75643SMatthew Wilcox (Oracle) struct xa_node *node;
10598fc75643SMatthew Wilcox (Oracle) void *curr = xas_load(xas);
10608fc75643SMatthew Wilcox (Oracle) int values = 0;
10618fc75643SMatthew Wilcox (Oracle)
10628fc75643SMatthew Wilcox (Oracle) node = xas->xa_node;
10638fc75643SMatthew Wilcox (Oracle) if (xas_top(node))
10648fc75643SMatthew Wilcox (Oracle) return;
10658fc75643SMatthew Wilcox (Oracle)
10668fc75643SMatthew Wilcox (Oracle) marks = node_get_marks(node, xas->xa_offset);
10678fc75643SMatthew Wilcox (Oracle)
10688fc75643SMatthew Wilcox (Oracle) offset = xas->xa_offset + sibs;
10698fc75643SMatthew Wilcox (Oracle) do {
10708fc75643SMatthew Wilcox (Oracle) if (xas->xa_shift < node->shift) {
10718fc75643SMatthew Wilcox (Oracle) struct xa_node *child = xas->xa_alloc;
10728fc75643SMatthew Wilcox (Oracle)
10738fc75643SMatthew Wilcox (Oracle) xas->xa_alloc = rcu_dereference_raw(child->parent);
10748fc75643SMatthew Wilcox (Oracle) child->shift = node->shift - XA_CHUNK_SHIFT;
10758fc75643SMatthew Wilcox (Oracle) child->offset = offset;
10768fc75643SMatthew Wilcox (Oracle) child->count = XA_CHUNK_SIZE;
10778fc75643SMatthew Wilcox (Oracle) child->nr_values = xa_is_value(entry) ?
10788fc75643SMatthew Wilcox (Oracle) XA_CHUNK_SIZE : 0;
10798fc75643SMatthew Wilcox (Oracle) RCU_INIT_POINTER(child->parent, node);
10808fc75643SMatthew Wilcox (Oracle) node_set_marks(node, offset, child, marks);
10818fc75643SMatthew Wilcox (Oracle) rcu_assign_pointer(node->slots[offset],
10828fc75643SMatthew Wilcox (Oracle) xa_mk_node(child));
10838fc75643SMatthew Wilcox (Oracle) if (xa_is_value(curr))
10848fc75643SMatthew Wilcox (Oracle) values--;
10853ed4bb77SMatthew Wilcox (Oracle) xas_update(xas, child);
10868fc75643SMatthew Wilcox (Oracle) } else {
10878fc75643SMatthew Wilcox (Oracle) unsigned int canon = offset - xas->xa_sibs;
10888fc75643SMatthew Wilcox (Oracle)
10898fc75643SMatthew Wilcox (Oracle) node_set_marks(node, canon, NULL, marks);
10908fc75643SMatthew Wilcox (Oracle) rcu_assign_pointer(node->slots[canon], entry);
10918fc75643SMatthew Wilcox (Oracle) while (offset > canon)
10928fc75643SMatthew Wilcox (Oracle) rcu_assign_pointer(node->slots[offset--],
10938fc75643SMatthew Wilcox (Oracle) xa_mk_sibling(canon));
10948fc75643SMatthew Wilcox (Oracle) values += (xa_is_value(entry) - xa_is_value(curr)) *
10958fc75643SMatthew Wilcox (Oracle) (xas->xa_sibs + 1);
10968fc75643SMatthew Wilcox (Oracle) }
10978fc75643SMatthew Wilcox (Oracle) } while (offset-- > xas->xa_offset);
10988fc75643SMatthew Wilcox (Oracle)
10998fc75643SMatthew Wilcox (Oracle) node->nr_values += values;
11003ed4bb77SMatthew Wilcox (Oracle) xas_update(xas, node);
11018fc75643SMatthew Wilcox (Oracle) }
11028fc75643SMatthew Wilcox (Oracle) EXPORT_SYMBOL_GPL(xas_split);
11038fc75643SMatthew Wilcox (Oracle) #endif
11048fc75643SMatthew Wilcox (Oracle)
110558d6ea30SMatthew Wilcox /**
1106b803b428SMatthew Wilcox * xas_pause() - Pause a walk to drop a lock.
1107b803b428SMatthew Wilcox * @xas: XArray operation state.
1108b803b428SMatthew Wilcox *
1109b803b428SMatthew Wilcox * Some users need to pause a walk and drop the lock they're holding in
1110b803b428SMatthew Wilcox * order to yield to a higher priority thread or carry out an operation
1111b803b428SMatthew Wilcox * on an entry. Those users should call this function before they drop
1112b803b428SMatthew Wilcox * the lock. It resets the @xas to be suitable for the next iteration
1113b803b428SMatthew Wilcox * of the loop after the user has reacquired the lock. If most entries
1114b803b428SMatthew Wilcox * found during a walk require you to call xas_pause(), the xa_for_each()
1115b803b428SMatthew Wilcox * iterator may be more appropriate.
1116b803b428SMatthew Wilcox *
1117b803b428SMatthew Wilcox * Note that xas_pause() only works for forward iteration. If a user needs
1118b803b428SMatthew Wilcox * to pause a reverse iteration, we will need a xas_pause_rev().
1119b803b428SMatthew Wilcox */
xas_pause(struct xa_state * xas)1120b803b428SMatthew Wilcox void xas_pause(struct xa_state *xas)
1121b803b428SMatthew Wilcox {
1122b803b428SMatthew Wilcox struct xa_node *node = xas->xa_node;
1123b803b428SMatthew Wilcox
1124b803b428SMatthew Wilcox if (xas_invalid(xas))
1125b803b428SMatthew Wilcox return;
1126b803b428SMatthew Wilcox
112782a22311SMatthew Wilcox (Oracle) xas->xa_node = XAS_RESTART;
1128b803b428SMatthew Wilcox if (node) {
1129c36d451aSMatthew Wilcox (Oracle) unsigned long offset = xas->xa_offset;
1130b803b428SMatthew Wilcox while (++offset < XA_CHUNK_SIZE) {
1131b803b428SMatthew Wilcox if (!xa_is_sibling(xa_entry(xas->xa, node, offset)))
1132b803b428SMatthew Wilcox break;
1133b803b428SMatthew Wilcox }
1134b803b428SMatthew Wilcox xas->xa_index += (offset - xas->xa_offset) << node->shift;
113582a22311SMatthew Wilcox (Oracle) if (xas->xa_index == 0)
113682a22311SMatthew Wilcox (Oracle) xas->xa_node = XAS_BOUNDS;
1137b803b428SMatthew Wilcox } else {
1138b803b428SMatthew Wilcox xas->xa_index++;
1139b803b428SMatthew Wilcox }
1140b803b428SMatthew Wilcox }
1141b803b428SMatthew Wilcox EXPORT_SYMBOL_GPL(xas_pause);
1142b803b428SMatthew Wilcox
114364d3e9a9SMatthew Wilcox /*
114464d3e9a9SMatthew Wilcox * __xas_prev() - Find the previous entry in the XArray.
114564d3e9a9SMatthew Wilcox * @xas: XArray operation state.
114664d3e9a9SMatthew Wilcox *
114764d3e9a9SMatthew Wilcox * Helper function for xas_prev() which handles all the complex cases
114864d3e9a9SMatthew Wilcox * out of line.
114964d3e9a9SMatthew Wilcox */
__xas_prev(struct xa_state * xas)115064d3e9a9SMatthew Wilcox void *__xas_prev(struct xa_state *xas)
115164d3e9a9SMatthew Wilcox {
115264d3e9a9SMatthew Wilcox void *entry;
115364d3e9a9SMatthew Wilcox
115464d3e9a9SMatthew Wilcox if (!xas_frozen(xas->xa_node))
115564d3e9a9SMatthew Wilcox xas->xa_index--;
115691abab83SMatthew Wilcox (Oracle) if (!xas->xa_node)
115791abab83SMatthew Wilcox (Oracle) return set_bounds(xas);
115864d3e9a9SMatthew Wilcox if (xas_not_node(xas->xa_node))
115964d3e9a9SMatthew Wilcox return xas_load(xas);
116064d3e9a9SMatthew Wilcox
116164d3e9a9SMatthew Wilcox if (xas->xa_offset != get_offset(xas->xa_index, xas->xa_node))
116264d3e9a9SMatthew Wilcox xas->xa_offset--;
116364d3e9a9SMatthew Wilcox
116464d3e9a9SMatthew Wilcox while (xas->xa_offset == 255) {
116564d3e9a9SMatthew Wilcox xas->xa_offset = xas->xa_node->offset - 1;
116664d3e9a9SMatthew Wilcox xas->xa_node = xa_parent(xas->xa, xas->xa_node);
116764d3e9a9SMatthew Wilcox if (!xas->xa_node)
116864d3e9a9SMatthew Wilcox return set_bounds(xas);
116964d3e9a9SMatthew Wilcox }
117064d3e9a9SMatthew Wilcox
117164d3e9a9SMatthew Wilcox for (;;) {
117264d3e9a9SMatthew Wilcox entry = xa_entry(xas->xa, xas->xa_node, xas->xa_offset);
117364d3e9a9SMatthew Wilcox if (!xa_is_node(entry))
117464d3e9a9SMatthew Wilcox return entry;
117564d3e9a9SMatthew Wilcox
117664d3e9a9SMatthew Wilcox xas->xa_node = xa_to_node(entry);
117764d3e9a9SMatthew Wilcox xas_set_offset(xas);
117864d3e9a9SMatthew Wilcox }
117964d3e9a9SMatthew Wilcox }
118064d3e9a9SMatthew Wilcox EXPORT_SYMBOL_GPL(__xas_prev);
118164d3e9a9SMatthew Wilcox
118264d3e9a9SMatthew Wilcox /*
118364d3e9a9SMatthew Wilcox * __xas_next() - Find the next entry in the XArray.
118464d3e9a9SMatthew Wilcox * @xas: XArray operation state.
118564d3e9a9SMatthew Wilcox *
118664d3e9a9SMatthew Wilcox * Helper function for xas_next() which handles all the complex cases
118764d3e9a9SMatthew Wilcox * out of line.
118864d3e9a9SMatthew Wilcox */
__xas_next(struct xa_state * xas)118964d3e9a9SMatthew Wilcox void *__xas_next(struct xa_state *xas)
119064d3e9a9SMatthew Wilcox {
119164d3e9a9SMatthew Wilcox void *entry;
119264d3e9a9SMatthew Wilcox
119364d3e9a9SMatthew Wilcox if (!xas_frozen(xas->xa_node))
119464d3e9a9SMatthew Wilcox xas->xa_index++;
119591abab83SMatthew Wilcox (Oracle) if (!xas->xa_node)
119691abab83SMatthew Wilcox (Oracle) return set_bounds(xas);
119764d3e9a9SMatthew Wilcox if (xas_not_node(xas->xa_node))
119864d3e9a9SMatthew Wilcox return xas_load(xas);
119964d3e9a9SMatthew Wilcox
120064d3e9a9SMatthew Wilcox if (xas->xa_offset != get_offset(xas->xa_index, xas->xa_node))
120164d3e9a9SMatthew Wilcox xas->xa_offset++;
120264d3e9a9SMatthew Wilcox
120364d3e9a9SMatthew Wilcox while (xas->xa_offset == XA_CHUNK_SIZE) {
120464d3e9a9SMatthew Wilcox xas->xa_offset = xas->xa_node->offset + 1;
120564d3e9a9SMatthew Wilcox xas->xa_node = xa_parent(xas->xa, xas->xa_node);
120664d3e9a9SMatthew Wilcox if (!xas->xa_node)
120764d3e9a9SMatthew Wilcox return set_bounds(xas);
120864d3e9a9SMatthew Wilcox }
120964d3e9a9SMatthew Wilcox
121064d3e9a9SMatthew Wilcox for (;;) {
121164d3e9a9SMatthew Wilcox entry = xa_entry(xas->xa, xas->xa_node, xas->xa_offset);
121264d3e9a9SMatthew Wilcox if (!xa_is_node(entry))
121364d3e9a9SMatthew Wilcox return entry;
121464d3e9a9SMatthew Wilcox
121564d3e9a9SMatthew Wilcox xas->xa_node = xa_to_node(entry);
121664d3e9a9SMatthew Wilcox xas_set_offset(xas);
121764d3e9a9SMatthew Wilcox }
121864d3e9a9SMatthew Wilcox }
121964d3e9a9SMatthew Wilcox EXPORT_SYMBOL_GPL(__xas_next);
122064d3e9a9SMatthew Wilcox
1221b803b428SMatthew Wilcox /**
1222b803b428SMatthew Wilcox * xas_find() - Find the next present entry in the XArray.
1223b803b428SMatthew Wilcox * @xas: XArray operation state.
1224b803b428SMatthew Wilcox * @max: Highest index to return.
1225b803b428SMatthew Wilcox *
1226b803b428SMatthew Wilcox * If the @xas has not yet been walked to an entry, return the entry
1227b803b428SMatthew Wilcox * which has an index >= xas.xa_index. If it has been walked, the entry
1228b803b428SMatthew Wilcox * currently being pointed at has been processed, and so we move to the
1229b803b428SMatthew Wilcox * next entry.
1230b803b428SMatthew Wilcox *
1231b803b428SMatthew Wilcox * If no entry is found and the array is smaller than @max, the iterator
1232b803b428SMatthew Wilcox * is set to the smallest index not yet in the array. This allows @xas
1233b803b428SMatthew Wilcox * to be immediately passed to xas_store().
1234b803b428SMatthew Wilcox *
1235b803b428SMatthew Wilcox * Return: The entry, if found, otherwise %NULL.
1236b803b428SMatthew Wilcox */
xas_find(struct xa_state * xas,unsigned long max)1237b803b428SMatthew Wilcox void *xas_find(struct xa_state *xas, unsigned long max)
1238b803b428SMatthew Wilcox {
1239b803b428SMatthew Wilcox void *entry;
1240b803b428SMatthew Wilcox
124182a22311SMatthew Wilcox (Oracle) if (xas_error(xas) || xas->xa_node == XAS_BOUNDS)
1242b803b428SMatthew Wilcox return NULL;
1243c44aa5e8SMatthew Wilcox (Oracle) if (xas->xa_index > max)
1244c44aa5e8SMatthew Wilcox (Oracle) return set_bounds(xas);
1245b803b428SMatthew Wilcox
1246b803b428SMatthew Wilcox if (!xas->xa_node) {
1247b803b428SMatthew Wilcox xas->xa_index = 1;
1248b803b428SMatthew Wilcox return set_bounds(xas);
124982a22311SMatthew Wilcox (Oracle) } else if (xas->xa_node == XAS_RESTART) {
1250b803b428SMatthew Wilcox entry = xas_load(xas);
1251b803b428SMatthew Wilcox if (entry || xas_not_node(xas->xa_node))
1252b803b428SMatthew Wilcox return entry;
1253b803b428SMatthew Wilcox } else if (!xas->xa_node->shift &&
1254b803b428SMatthew Wilcox xas->xa_offset != (xas->xa_index & XA_CHUNK_MASK)) {
1255b803b428SMatthew Wilcox xas->xa_offset = ((xas->xa_index - 1) & XA_CHUNK_MASK) + 1;
1256b803b428SMatthew Wilcox }
1257b803b428SMatthew Wilcox
125825a8de7fSMatthew Wilcox (Oracle) xas_next_offset(xas);
1259b803b428SMatthew Wilcox
1260b803b428SMatthew Wilcox while (xas->xa_node && (xas->xa_index <= max)) {
1261b803b428SMatthew Wilcox if (unlikely(xas->xa_offset == XA_CHUNK_SIZE)) {
1262b803b428SMatthew Wilcox xas->xa_offset = xas->xa_node->offset + 1;
1263b803b428SMatthew Wilcox xas->xa_node = xa_parent(xas->xa, xas->xa_node);
1264b803b428SMatthew Wilcox continue;
1265b803b428SMatthew Wilcox }
1266b803b428SMatthew Wilcox
1267b803b428SMatthew Wilcox entry = xa_entry(xas->xa, xas->xa_node, xas->xa_offset);
1268b803b428SMatthew Wilcox if (xa_is_node(entry)) {
1269b803b428SMatthew Wilcox xas->xa_node = xa_to_node(entry);
1270b803b428SMatthew Wilcox xas->xa_offset = 0;
1271b803b428SMatthew Wilcox continue;
1272b803b428SMatthew Wilcox }
1273b803b428SMatthew Wilcox if (entry && !xa_is_sibling(entry))
1274b803b428SMatthew Wilcox return entry;
1275b803b428SMatthew Wilcox
127625a8de7fSMatthew Wilcox (Oracle) xas_next_offset(xas);
1277b803b428SMatthew Wilcox }
1278b803b428SMatthew Wilcox
1279b803b428SMatthew Wilcox if (!xas->xa_node)
1280b803b428SMatthew Wilcox xas->xa_node = XAS_BOUNDS;
1281b803b428SMatthew Wilcox return NULL;
1282b803b428SMatthew Wilcox }
1283b803b428SMatthew Wilcox EXPORT_SYMBOL_GPL(xas_find);
1284b803b428SMatthew Wilcox
1285b803b428SMatthew Wilcox /**
1286b803b428SMatthew Wilcox * xas_find_marked() - Find the next marked entry in the XArray.
1287b803b428SMatthew Wilcox * @xas: XArray operation state.
1288b803b428SMatthew Wilcox * @max: Highest index to return.
1289b803b428SMatthew Wilcox * @mark: Mark number to search for.
1290b803b428SMatthew Wilcox *
1291b803b428SMatthew Wilcox * If the @xas has not yet been walked to an entry, return the marked entry
1292b803b428SMatthew Wilcox * which has an index >= xas.xa_index. If it has been walked, the entry
1293b803b428SMatthew Wilcox * currently being pointed at has been processed, and so we return the
1294b803b428SMatthew Wilcox * first marked entry with an index > xas.xa_index.
1295b803b428SMatthew Wilcox *
1296b803b428SMatthew Wilcox * If no marked entry is found and the array is smaller than @max, @xas is
1297b803b428SMatthew Wilcox * set to the bounds state and xas->xa_index is set to the smallest index
1298b803b428SMatthew Wilcox * not yet in the array. This allows @xas to be immediately passed to
1299b803b428SMatthew Wilcox * xas_store().
1300b803b428SMatthew Wilcox *
1301b803b428SMatthew Wilcox * If no entry is found before @max is reached, @xas is set to the restart
1302b803b428SMatthew Wilcox * state.
1303b803b428SMatthew Wilcox *
1304b803b428SMatthew Wilcox * Return: The entry, if found, otherwise %NULL.
1305b803b428SMatthew Wilcox */
xas_find_marked(struct xa_state * xas,unsigned long max,xa_mark_t mark)1306b803b428SMatthew Wilcox void *xas_find_marked(struct xa_state *xas, unsigned long max, xa_mark_t mark)
1307b803b428SMatthew Wilcox {
1308b803b428SMatthew Wilcox bool advance = true;
1309b803b428SMatthew Wilcox unsigned int offset;
1310b803b428SMatthew Wilcox void *entry;
1311b803b428SMatthew Wilcox
1312b803b428SMatthew Wilcox if (xas_error(xas))
1313b803b428SMatthew Wilcox return NULL;
1314c44aa5e8SMatthew Wilcox (Oracle) if (xas->xa_index > max)
1315c44aa5e8SMatthew Wilcox (Oracle) goto max;
1316b803b428SMatthew Wilcox
1317b803b428SMatthew Wilcox if (!xas->xa_node) {
1318b803b428SMatthew Wilcox xas->xa_index = 1;
1319b803b428SMatthew Wilcox goto out;
1320b803b428SMatthew Wilcox } else if (xas_top(xas->xa_node)) {
1321b803b428SMatthew Wilcox advance = false;
1322b803b428SMatthew Wilcox entry = xa_head(xas->xa);
1323b803b428SMatthew Wilcox xas->xa_node = NULL;
1324b803b428SMatthew Wilcox if (xas->xa_index > max_index(entry))
132548483614SMatthew Wilcox goto out;
1326b803b428SMatthew Wilcox if (!xa_is_node(entry)) {
1327b803b428SMatthew Wilcox if (xa_marked(xas->xa, mark))
1328b803b428SMatthew Wilcox return entry;
1329b803b428SMatthew Wilcox xas->xa_index = 1;
1330b803b428SMatthew Wilcox goto out;
1331b803b428SMatthew Wilcox }
1332b803b428SMatthew Wilcox xas->xa_node = xa_to_node(entry);
1333b803b428SMatthew Wilcox xas->xa_offset = xas->xa_index >> xas->xa_node->shift;
1334b803b428SMatthew Wilcox }
1335b803b428SMatthew Wilcox
1336b803b428SMatthew Wilcox while (xas->xa_index <= max) {
1337b803b428SMatthew Wilcox if (unlikely(xas->xa_offset == XA_CHUNK_SIZE)) {
1338b803b428SMatthew Wilcox xas->xa_offset = xas->xa_node->offset + 1;
1339b803b428SMatthew Wilcox xas->xa_node = xa_parent(xas->xa, xas->xa_node);
1340b803b428SMatthew Wilcox if (!xas->xa_node)
1341b803b428SMatthew Wilcox break;
1342b803b428SMatthew Wilcox advance = false;
1343b803b428SMatthew Wilcox continue;
1344b803b428SMatthew Wilcox }
1345b803b428SMatthew Wilcox
1346b803b428SMatthew Wilcox if (!advance) {
1347b803b428SMatthew Wilcox entry = xa_entry(xas->xa, xas->xa_node, xas->xa_offset);
1348b803b428SMatthew Wilcox if (xa_is_sibling(entry)) {
1349b803b428SMatthew Wilcox xas->xa_offset = xa_to_sibling(entry);
1350b803b428SMatthew Wilcox xas_move_index(xas, xas->xa_offset);
1351b803b428SMatthew Wilcox }
1352b803b428SMatthew Wilcox }
1353b803b428SMatthew Wilcox
1354b803b428SMatthew Wilcox offset = xas_find_chunk(xas, advance, mark);
1355b803b428SMatthew Wilcox if (offset > xas->xa_offset) {
1356b803b428SMatthew Wilcox advance = false;
1357b803b428SMatthew Wilcox xas_move_index(xas, offset);
1358b803b428SMatthew Wilcox /* Mind the wrap */
1359b803b428SMatthew Wilcox if ((xas->xa_index - 1) >= max)
1360b803b428SMatthew Wilcox goto max;
1361b803b428SMatthew Wilcox xas->xa_offset = offset;
1362b803b428SMatthew Wilcox if (offset == XA_CHUNK_SIZE)
1363b803b428SMatthew Wilcox continue;
1364b803b428SMatthew Wilcox }
1365b803b428SMatthew Wilcox
1366b803b428SMatthew Wilcox entry = xa_entry(xas->xa, xas->xa_node, xas->xa_offset);
13677e934cf5SMatthew Wilcox (Oracle) if (!entry && !(xa_track_free(xas->xa) && mark == XA_FREE_MARK))
13687e934cf5SMatthew Wilcox (Oracle) continue;
1369b803b428SMatthew Wilcox if (!xa_is_node(entry))
1370b803b428SMatthew Wilcox return entry;
1371b803b428SMatthew Wilcox xas->xa_node = xa_to_node(entry);
1372b803b428SMatthew Wilcox xas_set_offset(xas);
1373b803b428SMatthew Wilcox }
1374b803b428SMatthew Wilcox
1375b803b428SMatthew Wilcox out:
137648483614SMatthew Wilcox if (xas->xa_index > max)
1377b803b428SMatthew Wilcox goto max;
137848483614SMatthew Wilcox return set_bounds(xas);
1379b803b428SMatthew Wilcox max:
1380b803b428SMatthew Wilcox xas->xa_node = XAS_RESTART;
1381b803b428SMatthew Wilcox return NULL;
1382b803b428SMatthew Wilcox }
1383b803b428SMatthew Wilcox EXPORT_SYMBOL_GPL(xas_find_marked);
1384b803b428SMatthew Wilcox
1385b803b428SMatthew Wilcox /**
13864e99d4e9SMatthew Wilcox * xas_find_conflict() - Find the next present entry in a range.
13874e99d4e9SMatthew Wilcox * @xas: XArray operation state.
13884e99d4e9SMatthew Wilcox *
13894e99d4e9SMatthew Wilcox * The @xas describes both a range and a position within that range.
13904e99d4e9SMatthew Wilcox *
13914e99d4e9SMatthew Wilcox * Context: Any context. Expects xa_lock to be held.
13924e99d4e9SMatthew Wilcox * Return: The next entry in the range covered by @xas or %NULL.
13934e99d4e9SMatthew Wilcox */
xas_find_conflict(struct xa_state * xas)13944e99d4e9SMatthew Wilcox void *xas_find_conflict(struct xa_state *xas)
13954e99d4e9SMatthew Wilcox {
13964e99d4e9SMatthew Wilcox void *curr;
13974e99d4e9SMatthew Wilcox
13984e99d4e9SMatthew Wilcox if (xas_error(xas))
13994e99d4e9SMatthew Wilcox return NULL;
14004e99d4e9SMatthew Wilcox
14014e99d4e9SMatthew Wilcox if (!xas->xa_node)
14024e99d4e9SMatthew Wilcox return NULL;
14034e99d4e9SMatthew Wilcox
14044e99d4e9SMatthew Wilcox if (xas_top(xas->xa_node)) {
14054e99d4e9SMatthew Wilcox curr = xas_start(xas);
14064e99d4e9SMatthew Wilcox if (!curr)
14074e99d4e9SMatthew Wilcox return NULL;
14084e99d4e9SMatthew Wilcox while (xa_is_node(curr)) {
14094e99d4e9SMatthew Wilcox struct xa_node *node = xa_to_node(curr);
14104e99d4e9SMatthew Wilcox curr = xas_descend(xas, node);
14114e99d4e9SMatthew Wilcox }
14124e99d4e9SMatthew Wilcox if (curr)
14134e99d4e9SMatthew Wilcox return curr;
14144e99d4e9SMatthew Wilcox }
14154e99d4e9SMatthew Wilcox
14164e99d4e9SMatthew Wilcox if (xas->xa_node->shift > xas->xa_shift)
14174e99d4e9SMatthew Wilcox return NULL;
14184e99d4e9SMatthew Wilcox
14194e99d4e9SMatthew Wilcox for (;;) {
14204e99d4e9SMatthew Wilcox if (xas->xa_node->shift == xas->xa_shift) {
14214e99d4e9SMatthew Wilcox if ((xas->xa_offset & xas->xa_sibs) == xas->xa_sibs)
14224e99d4e9SMatthew Wilcox break;
14234e99d4e9SMatthew Wilcox } else if (xas->xa_offset == XA_CHUNK_MASK) {
14244e99d4e9SMatthew Wilcox xas->xa_offset = xas->xa_node->offset;
14254e99d4e9SMatthew Wilcox xas->xa_node = xa_parent_locked(xas->xa, xas->xa_node);
14264e99d4e9SMatthew Wilcox if (!xas->xa_node)
14274e99d4e9SMatthew Wilcox break;
14284e99d4e9SMatthew Wilcox continue;
14294e99d4e9SMatthew Wilcox }
14304e99d4e9SMatthew Wilcox curr = xa_entry_locked(xas->xa, xas->xa_node, ++xas->xa_offset);
14314e99d4e9SMatthew Wilcox if (xa_is_sibling(curr))
14324e99d4e9SMatthew Wilcox continue;
14334e99d4e9SMatthew Wilcox while (xa_is_node(curr)) {
14344e99d4e9SMatthew Wilcox xas->xa_node = xa_to_node(curr);
14354e99d4e9SMatthew Wilcox xas->xa_offset = 0;
14364e99d4e9SMatthew Wilcox curr = xa_entry_locked(xas->xa, xas->xa_node, 0);
14374e99d4e9SMatthew Wilcox }
14384e99d4e9SMatthew Wilcox if (curr)
14394e99d4e9SMatthew Wilcox return curr;
14404e99d4e9SMatthew Wilcox }
14414e99d4e9SMatthew Wilcox xas->xa_offset -= xas->xa_sibs;
14424e99d4e9SMatthew Wilcox return NULL;
14434e99d4e9SMatthew Wilcox }
14444e99d4e9SMatthew Wilcox EXPORT_SYMBOL_GPL(xas_find_conflict);
14454e99d4e9SMatthew Wilcox
14464e99d4e9SMatthew Wilcox /**
1447ad3d6c72SMatthew Wilcox * xa_load() - Load an entry from an XArray.
1448ad3d6c72SMatthew Wilcox * @xa: XArray.
1449ad3d6c72SMatthew Wilcox * @index: index into array.
1450ad3d6c72SMatthew Wilcox *
1451ad3d6c72SMatthew Wilcox * Context: Any context. Takes and releases the RCU lock.
1452ad3d6c72SMatthew Wilcox * Return: The entry at @index in @xa.
1453ad3d6c72SMatthew Wilcox */
xa_load(struct xarray * xa,unsigned long index)1454ad3d6c72SMatthew Wilcox void *xa_load(struct xarray *xa, unsigned long index)
1455ad3d6c72SMatthew Wilcox {
1456ad3d6c72SMatthew Wilcox XA_STATE(xas, xa, index);
1457ad3d6c72SMatthew Wilcox void *entry;
1458ad3d6c72SMatthew Wilcox
1459ad3d6c72SMatthew Wilcox rcu_read_lock();
1460ad3d6c72SMatthew Wilcox do {
1461ad3d6c72SMatthew Wilcox entry = xas_load(&xas);
14629f14d4f1SMatthew Wilcox if (xa_is_zero(entry))
14639f14d4f1SMatthew Wilcox entry = NULL;
1464ad3d6c72SMatthew Wilcox } while (xas_retry(&xas, entry));
1465ad3d6c72SMatthew Wilcox rcu_read_unlock();
1466ad3d6c72SMatthew Wilcox
1467ad3d6c72SMatthew Wilcox return entry;
1468ad3d6c72SMatthew Wilcox }
1469ad3d6c72SMatthew Wilcox EXPORT_SYMBOL(xa_load);
1470ad3d6c72SMatthew Wilcox
xas_result(struct xa_state * xas,void * curr)147158d6ea30SMatthew Wilcox static void *xas_result(struct xa_state *xas, void *curr)
147258d6ea30SMatthew Wilcox {
14739f14d4f1SMatthew Wilcox if (xa_is_zero(curr))
14749f14d4f1SMatthew Wilcox return NULL;
147558d6ea30SMatthew Wilcox if (xas_error(xas))
147658d6ea30SMatthew Wilcox curr = xas->xa_node;
147758d6ea30SMatthew Wilcox return curr;
147858d6ea30SMatthew Wilcox }
147958d6ea30SMatthew Wilcox
148058d6ea30SMatthew Wilcox /**
148158d6ea30SMatthew Wilcox * __xa_erase() - Erase this entry from the XArray while locked.
148258d6ea30SMatthew Wilcox * @xa: XArray.
148358d6ea30SMatthew Wilcox * @index: Index into array.
148458d6ea30SMatthew Wilcox *
1485809ab937SMatthew Wilcox * After this function returns, loading from @index will return %NULL.
1486809ab937SMatthew Wilcox * If the index is part of a multi-index entry, all indices will be erased
1487809ab937SMatthew Wilcox * and none of the entries will be part of a multi-index entry.
148858d6ea30SMatthew Wilcox *
1489809ab937SMatthew Wilcox * Context: Any context. Expects xa_lock to be held on entry.
1490809ab937SMatthew Wilcox * Return: The entry which used to be at this index.
149158d6ea30SMatthew Wilcox */
__xa_erase(struct xarray * xa,unsigned long index)149258d6ea30SMatthew Wilcox void *__xa_erase(struct xarray *xa, unsigned long index)
149358d6ea30SMatthew Wilcox {
149458d6ea30SMatthew Wilcox XA_STATE(xas, xa, index);
149558d6ea30SMatthew Wilcox return xas_result(&xas, xas_store(&xas, NULL));
149658d6ea30SMatthew Wilcox }
14979ee5a3b7SMatthew Wilcox EXPORT_SYMBOL(__xa_erase);
149858d6ea30SMatthew Wilcox
149958d6ea30SMatthew Wilcox /**
15009c16bb88SMatthew Wilcox * xa_erase() - Erase this entry from the XArray.
15019c16bb88SMatthew Wilcox * @xa: XArray.
15029c16bb88SMatthew Wilcox * @index: Index of entry.
15039c16bb88SMatthew Wilcox *
1504809ab937SMatthew Wilcox * After this function returns, loading from @index will return %NULL.
1505809ab937SMatthew Wilcox * If the index is part of a multi-index entry, all indices will be erased
1506809ab937SMatthew Wilcox * and none of the entries will be part of a multi-index entry.
15079c16bb88SMatthew Wilcox *
15089c16bb88SMatthew Wilcox * Context: Any context. Takes and releases the xa_lock.
15099c16bb88SMatthew Wilcox * Return: The entry which used to be at this index.
15109c16bb88SMatthew Wilcox */
xa_erase(struct xarray * xa,unsigned long index)15119c16bb88SMatthew Wilcox void *xa_erase(struct xarray *xa, unsigned long index)
15129c16bb88SMatthew Wilcox {
15139c16bb88SMatthew Wilcox void *entry;
15149c16bb88SMatthew Wilcox
15159c16bb88SMatthew Wilcox xa_lock(xa);
15169c16bb88SMatthew Wilcox entry = __xa_erase(xa, index);
15179c16bb88SMatthew Wilcox xa_unlock(xa);
15189c16bb88SMatthew Wilcox
15199c16bb88SMatthew Wilcox return entry;
15209c16bb88SMatthew Wilcox }
15219c16bb88SMatthew Wilcox EXPORT_SYMBOL(xa_erase);
15229c16bb88SMatthew Wilcox
15239c16bb88SMatthew Wilcox /**
152458d6ea30SMatthew Wilcox * __xa_store() - Store this entry in the XArray.
152558d6ea30SMatthew Wilcox * @xa: XArray.
152658d6ea30SMatthew Wilcox * @index: Index into array.
152758d6ea30SMatthew Wilcox * @entry: New entry.
152858d6ea30SMatthew Wilcox * @gfp: Memory allocation flags.
152958d6ea30SMatthew Wilcox *
153058d6ea30SMatthew Wilcox * You must already be holding the xa_lock when calling this function.
153158d6ea30SMatthew Wilcox * It will drop the lock if needed to allocate memory, and then reacquire
153258d6ea30SMatthew Wilcox * it afterwards.
153358d6ea30SMatthew Wilcox *
153458d6ea30SMatthew Wilcox * Context: Any context. Expects xa_lock to be held on entry. May
153558d6ea30SMatthew Wilcox * release and reacquire xa_lock if @gfp flags permit.
153658d6ea30SMatthew Wilcox * Return: The old entry at this index or xa_err() if an error happened.
153758d6ea30SMatthew Wilcox */
__xa_store(struct xarray * xa,unsigned long index,void * entry,gfp_t gfp)153858d6ea30SMatthew Wilcox void *__xa_store(struct xarray *xa, unsigned long index, void *entry, gfp_t gfp)
153958d6ea30SMatthew Wilcox {
154058d6ea30SMatthew Wilcox XA_STATE(xas, xa, index);
154158d6ea30SMatthew Wilcox void *curr;
154258d6ea30SMatthew Wilcox
154376b4e529SMatthew Wilcox if (WARN_ON_ONCE(xa_is_advanced(entry)))
154458d6ea30SMatthew Wilcox return XA_ERROR(-EINVAL);
1545d9c48043SMatthew Wilcox if (xa_track_free(xa) && !entry)
1546d9c48043SMatthew Wilcox entry = XA_ZERO_ENTRY;
154758d6ea30SMatthew Wilcox
154858d6ea30SMatthew Wilcox do {
154958d6ea30SMatthew Wilcox curr = xas_store(&xas, entry);
1550d9c48043SMatthew Wilcox if (xa_track_free(xa))
1551371c752dSMatthew Wilcox xas_clear_mark(&xas, XA_FREE_MARK);
155258d6ea30SMatthew Wilcox } while (__xas_nomem(&xas, gfp));
155358d6ea30SMatthew Wilcox
155458d6ea30SMatthew Wilcox return xas_result(&xas, curr);
155558d6ea30SMatthew Wilcox }
155658d6ea30SMatthew Wilcox EXPORT_SYMBOL(__xa_store);
155758d6ea30SMatthew Wilcox
15589b89a035SMatthew Wilcox /**
1559611f3186SMatthew Wilcox * xa_store() - Store this entry in the XArray.
1560611f3186SMatthew Wilcox * @xa: XArray.
1561611f3186SMatthew Wilcox * @index: Index into array.
1562611f3186SMatthew Wilcox * @entry: New entry.
1563611f3186SMatthew Wilcox * @gfp: Memory allocation flags.
1564611f3186SMatthew Wilcox *
1565611f3186SMatthew Wilcox * After this function returns, loads from this index will return @entry.
15668fc75643SMatthew Wilcox (Oracle) * Storing into an existing multi-index entry updates the entry of every index.
1567611f3186SMatthew Wilcox * The marks associated with @index are unaffected unless @entry is %NULL.
1568611f3186SMatthew Wilcox *
1569611f3186SMatthew Wilcox * Context: Any context. Takes and releases the xa_lock.
1570611f3186SMatthew Wilcox * May sleep if the @gfp flags permit.
1571611f3186SMatthew Wilcox * Return: The old entry at this index on success, xa_err(-EINVAL) if @entry
1572611f3186SMatthew Wilcox * cannot be stored in an XArray, or xa_err(-ENOMEM) if memory allocation
1573611f3186SMatthew Wilcox * failed.
1574611f3186SMatthew Wilcox */
xa_store(struct xarray * xa,unsigned long index,void * entry,gfp_t gfp)1575611f3186SMatthew Wilcox void *xa_store(struct xarray *xa, unsigned long index, void *entry, gfp_t gfp)
1576611f3186SMatthew Wilcox {
1577611f3186SMatthew Wilcox void *curr;
1578611f3186SMatthew Wilcox
1579611f3186SMatthew Wilcox xa_lock(xa);
1580611f3186SMatthew Wilcox curr = __xa_store(xa, index, entry, gfp);
1581611f3186SMatthew Wilcox xa_unlock(xa);
1582611f3186SMatthew Wilcox
1583611f3186SMatthew Wilcox return curr;
1584611f3186SMatthew Wilcox }
1585611f3186SMatthew Wilcox EXPORT_SYMBOL(xa_store);
1586611f3186SMatthew Wilcox
1587611f3186SMatthew Wilcox /**
158841aec91fSMatthew Wilcox * __xa_cmpxchg() - Store this entry in the XArray.
158941aec91fSMatthew Wilcox * @xa: XArray.
159041aec91fSMatthew Wilcox * @index: Index into array.
159141aec91fSMatthew Wilcox * @old: Old value to test against.
159241aec91fSMatthew Wilcox * @entry: New entry.
159341aec91fSMatthew Wilcox * @gfp: Memory allocation flags.
159441aec91fSMatthew Wilcox *
159541aec91fSMatthew Wilcox * You must already be holding the xa_lock when calling this function.
159641aec91fSMatthew Wilcox * It will drop the lock if needed to allocate memory, and then reacquire
159741aec91fSMatthew Wilcox * it afterwards.
159841aec91fSMatthew Wilcox *
159941aec91fSMatthew Wilcox * Context: Any context. Expects xa_lock to be held on entry. May
160041aec91fSMatthew Wilcox * release and reacquire xa_lock if @gfp flags permit.
160141aec91fSMatthew Wilcox * Return: The old entry at this index or xa_err() if an error happened.
160241aec91fSMatthew Wilcox */
__xa_cmpxchg(struct xarray * xa,unsigned long index,void * old,void * entry,gfp_t gfp)160341aec91fSMatthew Wilcox void *__xa_cmpxchg(struct xarray *xa, unsigned long index,
160441aec91fSMatthew Wilcox void *old, void *entry, gfp_t gfp)
160541aec91fSMatthew Wilcox {
160641aec91fSMatthew Wilcox XA_STATE(xas, xa, index);
160741aec91fSMatthew Wilcox void *curr;
160841aec91fSMatthew Wilcox
160976b4e529SMatthew Wilcox if (WARN_ON_ONCE(xa_is_advanced(entry)))
161041aec91fSMatthew Wilcox return XA_ERROR(-EINVAL);
161141aec91fSMatthew Wilcox
161241aec91fSMatthew Wilcox do {
161341aec91fSMatthew Wilcox curr = xas_load(&xas);
1614371c752dSMatthew Wilcox if (curr == old) {
161541aec91fSMatthew Wilcox xas_store(&xas, entry);
1616b38f6c50SMatthew Wilcox if (xa_track_free(xa) && entry && !curr)
1617371c752dSMatthew Wilcox xas_clear_mark(&xas, XA_FREE_MARK);
1618371c752dSMatthew Wilcox }
161941aec91fSMatthew Wilcox } while (__xas_nomem(&xas, gfp));
162041aec91fSMatthew Wilcox
162141aec91fSMatthew Wilcox return xas_result(&xas, curr);
162241aec91fSMatthew Wilcox }
162341aec91fSMatthew Wilcox EXPORT_SYMBOL(__xa_cmpxchg);
162441aec91fSMatthew Wilcox
162541aec91fSMatthew Wilcox /**
1626b0606fedSMatthew Wilcox * __xa_insert() - Store this entry in the XArray if no entry is present.
1627b0606fedSMatthew Wilcox * @xa: XArray.
1628b0606fedSMatthew Wilcox * @index: Index into array.
1629b0606fedSMatthew Wilcox * @entry: New entry.
1630b0606fedSMatthew Wilcox * @gfp: Memory allocation flags.
1631b0606fedSMatthew Wilcox *
1632b0606fedSMatthew Wilcox * Inserting a NULL entry will store a reserved entry (like xa_reserve())
1633b0606fedSMatthew Wilcox * if no entry is present. Inserting will fail if a reserved entry is
1634b0606fedSMatthew Wilcox * present, even though loading from this index will return NULL.
1635b0606fedSMatthew Wilcox *
1636b0606fedSMatthew Wilcox * Context: Any context. Expects xa_lock to be held on entry. May
1637b0606fedSMatthew Wilcox * release and reacquire xa_lock if @gfp flags permit.
1638fd9dc93eSMatthew Wilcox * Return: 0 if the store succeeded. -EBUSY if another entry was present.
1639b0606fedSMatthew Wilcox * -ENOMEM if memory could not be allocated.
1640b0606fedSMatthew Wilcox */
__xa_insert(struct xarray * xa,unsigned long index,void * entry,gfp_t gfp)1641b0606fedSMatthew Wilcox int __xa_insert(struct xarray *xa, unsigned long index, void *entry, gfp_t gfp)
1642b0606fedSMatthew Wilcox {
1643b0606fedSMatthew Wilcox XA_STATE(xas, xa, index);
1644b0606fedSMatthew Wilcox void *curr;
1645b0606fedSMatthew Wilcox
1646b0606fedSMatthew Wilcox if (WARN_ON_ONCE(xa_is_advanced(entry)))
1647b0606fedSMatthew Wilcox return -EINVAL;
1648b0606fedSMatthew Wilcox if (!entry)
1649b0606fedSMatthew Wilcox entry = XA_ZERO_ENTRY;
1650b0606fedSMatthew Wilcox
1651b0606fedSMatthew Wilcox do {
1652b0606fedSMatthew Wilcox curr = xas_load(&xas);
1653b0606fedSMatthew Wilcox if (!curr) {
1654b0606fedSMatthew Wilcox xas_store(&xas, entry);
1655b0606fedSMatthew Wilcox if (xa_track_free(xa))
1656b0606fedSMatthew Wilcox xas_clear_mark(&xas, XA_FREE_MARK);
1657b0606fedSMatthew Wilcox } else {
1658fd9dc93eSMatthew Wilcox xas_set_err(&xas, -EBUSY);
1659b0606fedSMatthew Wilcox }
1660b0606fedSMatthew Wilcox } while (__xas_nomem(&xas, gfp));
1661b0606fedSMatthew Wilcox
1662b0606fedSMatthew Wilcox return xas_error(&xas);
1663b0606fedSMatthew Wilcox }
1664b0606fedSMatthew Wilcox EXPORT_SYMBOL(__xa_insert);
1665b0606fedSMatthew Wilcox
16660e9446c3SMatthew Wilcox #ifdef CONFIG_XARRAY_MULTI
xas_set_range(struct xa_state * xas,unsigned long first,unsigned long last)16670e9446c3SMatthew Wilcox static void xas_set_range(struct xa_state *xas, unsigned long first,
16680e9446c3SMatthew Wilcox unsigned long last)
16690e9446c3SMatthew Wilcox {
16700e9446c3SMatthew Wilcox unsigned int shift = 0;
16710e9446c3SMatthew Wilcox unsigned long sibs = last - first;
16720e9446c3SMatthew Wilcox unsigned int offset = XA_CHUNK_MASK;
16730e9446c3SMatthew Wilcox
16740e9446c3SMatthew Wilcox xas_set(xas, first);
16750e9446c3SMatthew Wilcox
16760e9446c3SMatthew Wilcox while ((first & XA_CHUNK_MASK) == 0) {
16770e9446c3SMatthew Wilcox if (sibs < XA_CHUNK_MASK)
16780e9446c3SMatthew Wilcox break;
16790e9446c3SMatthew Wilcox if ((sibs == XA_CHUNK_MASK) && (offset < XA_CHUNK_MASK))
16800e9446c3SMatthew Wilcox break;
16810e9446c3SMatthew Wilcox shift += XA_CHUNK_SHIFT;
16820e9446c3SMatthew Wilcox if (offset == XA_CHUNK_MASK)
16830e9446c3SMatthew Wilcox offset = sibs & XA_CHUNK_MASK;
16840e9446c3SMatthew Wilcox sibs >>= XA_CHUNK_SHIFT;
16850e9446c3SMatthew Wilcox first >>= XA_CHUNK_SHIFT;
16860e9446c3SMatthew Wilcox }
16870e9446c3SMatthew Wilcox
16880e9446c3SMatthew Wilcox offset = first & XA_CHUNK_MASK;
16890e9446c3SMatthew Wilcox if (offset + sibs > XA_CHUNK_MASK)
16900e9446c3SMatthew Wilcox sibs = XA_CHUNK_MASK - offset;
16910e9446c3SMatthew Wilcox if ((((first + sibs + 1) << shift) - 1) > last)
16920e9446c3SMatthew Wilcox sibs -= 1;
16930e9446c3SMatthew Wilcox
16940e9446c3SMatthew Wilcox xas->xa_shift = shift;
16950e9446c3SMatthew Wilcox xas->xa_sibs = sibs;
16960e9446c3SMatthew Wilcox }
16970e9446c3SMatthew Wilcox
16980e9446c3SMatthew Wilcox /**
16990e9446c3SMatthew Wilcox * xa_store_range() - Store this entry at a range of indices in the XArray.
17000e9446c3SMatthew Wilcox * @xa: XArray.
17010e9446c3SMatthew Wilcox * @first: First index to affect.
17020e9446c3SMatthew Wilcox * @last: Last index to affect.
17030e9446c3SMatthew Wilcox * @entry: New entry.
17040e9446c3SMatthew Wilcox * @gfp: Memory allocation flags.
17050e9446c3SMatthew Wilcox *
17060e9446c3SMatthew Wilcox * After this function returns, loads from any index between @first and @last,
17070e9446c3SMatthew Wilcox * inclusive will return @entry.
17088fc75643SMatthew Wilcox (Oracle) * Storing into an existing multi-index entry updates the entry of every index.
17090e9446c3SMatthew Wilcox * The marks associated with @index are unaffected unless @entry is %NULL.
17100e9446c3SMatthew Wilcox *
17110e9446c3SMatthew Wilcox * Context: Process context. Takes and releases the xa_lock. May sleep
17120e9446c3SMatthew Wilcox * if the @gfp flags permit.
17130e9446c3SMatthew Wilcox * Return: %NULL on success, xa_err(-EINVAL) if @entry cannot be stored in
17140e9446c3SMatthew Wilcox * an XArray, or xa_err(-ENOMEM) if memory allocation failed.
17150e9446c3SMatthew Wilcox */
xa_store_range(struct xarray * xa,unsigned long first,unsigned long last,void * entry,gfp_t gfp)17160e9446c3SMatthew Wilcox void *xa_store_range(struct xarray *xa, unsigned long first,
17170e9446c3SMatthew Wilcox unsigned long last, void *entry, gfp_t gfp)
17180e9446c3SMatthew Wilcox {
17190e9446c3SMatthew Wilcox XA_STATE(xas, xa, 0);
17200e9446c3SMatthew Wilcox
17210e9446c3SMatthew Wilcox if (WARN_ON_ONCE(xa_is_internal(entry)))
17220e9446c3SMatthew Wilcox return XA_ERROR(-EINVAL);
17230e9446c3SMatthew Wilcox if (last < first)
17240e9446c3SMatthew Wilcox return XA_ERROR(-EINVAL);
17250e9446c3SMatthew Wilcox
17260e9446c3SMatthew Wilcox do {
17270e9446c3SMatthew Wilcox xas_lock(&xas);
17280e9446c3SMatthew Wilcox if (entry) {
172944a4a66bSMatthew Wilcox unsigned int order = BITS_PER_LONG;
173044a4a66bSMatthew Wilcox if (last + 1)
173144a4a66bSMatthew Wilcox order = __ffs(last + 1);
17320e9446c3SMatthew Wilcox xas_set_order(&xas, last, order);
173376b4e529SMatthew Wilcox xas_create(&xas, true);
17340e9446c3SMatthew Wilcox if (xas_error(&xas))
17350e9446c3SMatthew Wilcox goto unlock;
17360e9446c3SMatthew Wilcox }
17370e9446c3SMatthew Wilcox do {
17380e9446c3SMatthew Wilcox xas_set_range(&xas, first, last);
17390e9446c3SMatthew Wilcox xas_store(&xas, entry);
17400e9446c3SMatthew Wilcox if (xas_error(&xas))
17410e9446c3SMatthew Wilcox goto unlock;
17420e9446c3SMatthew Wilcox first += xas_size(&xas);
17430e9446c3SMatthew Wilcox } while (first <= last);
17440e9446c3SMatthew Wilcox unlock:
17450e9446c3SMatthew Wilcox xas_unlock(&xas);
17460e9446c3SMatthew Wilcox } while (xas_nomem(&xas, gfp));
17470e9446c3SMatthew Wilcox
17480e9446c3SMatthew Wilcox return xas_result(&xas, NULL);
17490e9446c3SMatthew Wilcox }
17500e9446c3SMatthew Wilcox EXPORT_SYMBOL(xa_store_range);
175157417cebSMatthew Wilcox (Oracle)
175257417cebSMatthew Wilcox (Oracle) /**
1753*734594d4SKairui Song * xas_get_order() - Get the order of an entry.
1754*734594d4SKairui Song * @xas: XArray operation state.
1755*734594d4SKairui Song *
1756*734594d4SKairui Song * Called after xas_load, the xas should not be in an error state.
1757*734594d4SKairui Song *
1758*734594d4SKairui Song * Return: A number between 0 and 63 indicating the order of the entry.
1759*734594d4SKairui Song */
xas_get_order(struct xa_state * xas)1760*734594d4SKairui Song int xas_get_order(struct xa_state *xas)
1761*734594d4SKairui Song {
1762*734594d4SKairui Song int order = 0;
1763*734594d4SKairui Song
1764*734594d4SKairui Song if (!xas->xa_node)
1765*734594d4SKairui Song return 0;
1766*734594d4SKairui Song
1767*734594d4SKairui Song for (;;) {
1768*734594d4SKairui Song unsigned int slot = xas->xa_offset + (1 << order);
1769*734594d4SKairui Song
1770*734594d4SKairui Song if (slot >= XA_CHUNK_SIZE)
1771*734594d4SKairui Song break;
1772*734594d4SKairui Song if (!xa_is_sibling(xa_entry(xas->xa, xas->xa_node, slot)))
1773*734594d4SKairui Song break;
1774*734594d4SKairui Song order++;
1775*734594d4SKairui Song }
1776*734594d4SKairui Song
1777*734594d4SKairui Song order += xas->xa_node->shift;
1778*734594d4SKairui Song return order;
1779*734594d4SKairui Song }
1780*734594d4SKairui Song EXPORT_SYMBOL_GPL(xas_get_order);
1781*734594d4SKairui Song
1782*734594d4SKairui Song /**
178357417cebSMatthew Wilcox (Oracle) * xa_get_order() - Get the order of an entry.
178457417cebSMatthew Wilcox (Oracle) * @xa: XArray.
178557417cebSMatthew Wilcox (Oracle) * @index: Index of the entry.
178657417cebSMatthew Wilcox (Oracle) *
178757417cebSMatthew Wilcox (Oracle) * Return: A number between 0 and 63 indicating the order of the entry.
178857417cebSMatthew Wilcox (Oracle) */
xa_get_order(struct xarray * xa,unsigned long index)178957417cebSMatthew Wilcox (Oracle) int xa_get_order(struct xarray *xa, unsigned long index)
179057417cebSMatthew Wilcox (Oracle) {
179157417cebSMatthew Wilcox (Oracle) XA_STATE(xas, xa, index);
179257417cebSMatthew Wilcox (Oracle) int order = 0;
1793*734594d4SKairui Song void *entry;
179457417cebSMatthew Wilcox (Oracle)
179557417cebSMatthew Wilcox (Oracle) rcu_read_lock();
179657417cebSMatthew Wilcox (Oracle) entry = xas_load(&xas);
1797*734594d4SKairui Song if (entry)
1798*734594d4SKairui Song order = xas_get_order(&xas);
179957417cebSMatthew Wilcox (Oracle) rcu_read_unlock();
180057417cebSMatthew Wilcox (Oracle)
180157417cebSMatthew Wilcox (Oracle) return order;
180257417cebSMatthew Wilcox (Oracle) }
180357417cebSMatthew Wilcox (Oracle) EXPORT_SYMBOL(xa_get_order);
18040e9446c3SMatthew Wilcox #endif /* CONFIG_XARRAY_MULTI */
18050e9446c3SMatthew Wilcox
18069f14d4f1SMatthew Wilcox /**
1807371c752dSMatthew Wilcox * __xa_alloc() - Find somewhere to store this entry in the XArray.
1808371c752dSMatthew Wilcox * @xa: XArray.
1809371c752dSMatthew Wilcox * @id: Pointer to ID.
1810a3e4d3f9SMatthew Wilcox * @limit: Range for allocated ID.
1811371c752dSMatthew Wilcox * @entry: New entry.
1812371c752dSMatthew Wilcox * @gfp: Memory allocation flags.
1813371c752dSMatthew Wilcox *
1814a3e4d3f9SMatthew Wilcox * Finds an empty entry in @xa between @limit.min and @limit.max,
1815a3e4d3f9SMatthew Wilcox * stores the index into the @id pointer, then stores the entry at
1816a3e4d3f9SMatthew Wilcox * that index. A concurrent lookup will not see an uninitialised @id.
1817371c752dSMatthew Wilcox *
1818e7716c74SPhilipp Stanner * Must only be operated on an xarray initialized with flag XA_FLAGS_ALLOC set
1819e7716c74SPhilipp Stanner * in xa_init_flags().
1820e7716c74SPhilipp Stanner *
1821371c752dSMatthew Wilcox * Context: Any context. Expects xa_lock to be held on entry. May
1822371c752dSMatthew Wilcox * release and reacquire xa_lock if @gfp flags permit.
1823a3e4d3f9SMatthew Wilcox * Return: 0 on success, -ENOMEM if memory could not be allocated or
1824a3e4d3f9SMatthew Wilcox * -EBUSY if there are no free entries in @limit.
1825371c752dSMatthew Wilcox */
__xa_alloc(struct xarray * xa,u32 * id,void * entry,struct xa_limit limit,gfp_t gfp)1826a3e4d3f9SMatthew Wilcox int __xa_alloc(struct xarray *xa, u32 *id, void *entry,
1827a3e4d3f9SMatthew Wilcox struct xa_limit limit, gfp_t gfp)
1828371c752dSMatthew Wilcox {
1829371c752dSMatthew Wilcox XA_STATE(xas, xa, 0);
1830371c752dSMatthew Wilcox
183176b4e529SMatthew Wilcox if (WARN_ON_ONCE(xa_is_advanced(entry)))
1832371c752dSMatthew Wilcox return -EINVAL;
1833371c752dSMatthew Wilcox if (WARN_ON_ONCE(!xa_track_free(xa)))
1834371c752dSMatthew Wilcox return -EINVAL;
1835371c752dSMatthew Wilcox
1836371c752dSMatthew Wilcox if (!entry)
1837371c752dSMatthew Wilcox entry = XA_ZERO_ENTRY;
1838371c752dSMatthew Wilcox
1839371c752dSMatthew Wilcox do {
1840a3e4d3f9SMatthew Wilcox xas.xa_index = limit.min;
1841a3e4d3f9SMatthew Wilcox xas_find_marked(&xas, limit.max, XA_FREE_MARK);
1842371c752dSMatthew Wilcox if (xas.xa_node == XAS_RESTART)
1843a3e4d3f9SMatthew Wilcox xas_set_err(&xas, -EBUSY);
1844a3e4d3f9SMatthew Wilcox else
1845a3e4d3f9SMatthew Wilcox *id = xas.xa_index;
1846371c752dSMatthew Wilcox xas_store(&xas, entry);
1847371c752dSMatthew Wilcox xas_clear_mark(&xas, XA_FREE_MARK);
1848371c752dSMatthew Wilcox } while (__xas_nomem(&xas, gfp));
1849371c752dSMatthew Wilcox
1850a3e4d3f9SMatthew Wilcox return xas_error(&xas);
1851371c752dSMatthew Wilcox }
1852371c752dSMatthew Wilcox EXPORT_SYMBOL(__xa_alloc);
1853371c752dSMatthew Wilcox
1854371c752dSMatthew Wilcox /**
18552fa044e5SMatthew Wilcox * __xa_alloc_cyclic() - Find somewhere to store this entry in the XArray.
18562fa044e5SMatthew Wilcox * @xa: XArray.
18572fa044e5SMatthew Wilcox * @id: Pointer to ID.
18582fa044e5SMatthew Wilcox * @entry: New entry.
18592fa044e5SMatthew Wilcox * @limit: Range of allocated ID.
18602fa044e5SMatthew Wilcox * @next: Pointer to next ID to allocate.
18612fa044e5SMatthew Wilcox * @gfp: Memory allocation flags.
18622fa044e5SMatthew Wilcox *
18632fa044e5SMatthew Wilcox * Finds an empty entry in @xa between @limit.min and @limit.max,
18642fa044e5SMatthew Wilcox * stores the index into the @id pointer, then stores the entry at
18652fa044e5SMatthew Wilcox * that index. A concurrent lookup will not see an uninitialised @id.
18662fa044e5SMatthew Wilcox * The search for an empty entry will start at @next and will wrap
18672fa044e5SMatthew Wilcox * around if necessary.
18682fa044e5SMatthew Wilcox *
1869e7716c74SPhilipp Stanner * Must only be operated on an xarray initialized with flag XA_FLAGS_ALLOC set
1870e7716c74SPhilipp Stanner * in xa_init_flags().
1871e7716c74SPhilipp Stanner *
18722fa044e5SMatthew Wilcox * Context: Any context. Expects xa_lock to be held on entry. May
18732fa044e5SMatthew Wilcox * release and reacquire xa_lock if @gfp flags permit.
18742fa044e5SMatthew Wilcox * Return: 0 if the allocation succeeded without wrapping. 1 if the
18752fa044e5SMatthew Wilcox * allocation succeeded after wrapping, -ENOMEM if memory could not be
18762fa044e5SMatthew Wilcox * allocated or -EBUSY if there are no free entries in @limit.
18772fa044e5SMatthew Wilcox */
__xa_alloc_cyclic(struct xarray * xa,u32 * id,void * entry,struct xa_limit limit,u32 * next,gfp_t gfp)18782fa044e5SMatthew Wilcox int __xa_alloc_cyclic(struct xarray *xa, u32 *id, void *entry,
18792fa044e5SMatthew Wilcox struct xa_limit limit, u32 *next, gfp_t gfp)
18802fa044e5SMatthew Wilcox {
18812fa044e5SMatthew Wilcox u32 min = limit.min;
18822fa044e5SMatthew Wilcox int ret;
18832fa044e5SMatthew Wilcox
18842fa044e5SMatthew Wilcox limit.min = max(min, *next);
18852fa044e5SMatthew Wilcox ret = __xa_alloc(xa, id, entry, limit, gfp);
18862fa044e5SMatthew Wilcox if ((xa->xa_flags & XA_FLAGS_ALLOC_WRAPPED) && ret == 0) {
18872fa044e5SMatthew Wilcox xa->xa_flags &= ~XA_FLAGS_ALLOC_WRAPPED;
18882fa044e5SMatthew Wilcox ret = 1;
18892fa044e5SMatthew Wilcox }
18902fa044e5SMatthew Wilcox
18912fa044e5SMatthew Wilcox if (ret < 0 && limit.min > min) {
18922fa044e5SMatthew Wilcox limit.min = min;
18932fa044e5SMatthew Wilcox ret = __xa_alloc(xa, id, entry, limit, gfp);
18942fa044e5SMatthew Wilcox if (ret == 0)
18952fa044e5SMatthew Wilcox ret = 1;
18962fa044e5SMatthew Wilcox }
18972fa044e5SMatthew Wilcox
18982fa044e5SMatthew Wilcox if (ret >= 0) {
18992fa044e5SMatthew Wilcox *next = *id + 1;
19002fa044e5SMatthew Wilcox if (*next == 0)
19012fa044e5SMatthew Wilcox xa->xa_flags |= XA_FLAGS_ALLOC_WRAPPED;
19022fa044e5SMatthew Wilcox }
19032fa044e5SMatthew Wilcox return ret;
19042fa044e5SMatthew Wilcox }
19052fa044e5SMatthew Wilcox EXPORT_SYMBOL(__xa_alloc_cyclic);
19062fa044e5SMatthew Wilcox
19072fa044e5SMatthew Wilcox /**
19089b89a035SMatthew Wilcox * __xa_set_mark() - Set this mark on this entry while locked.
19099b89a035SMatthew Wilcox * @xa: XArray.
19109b89a035SMatthew Wilcox * @index: Index of entry.
19119b89a035SMatthew Wilcox * @mark: Mark number.
19129b89a035SMatthew Wilcox *
1913804dfaf0SMatthew Wilcox * Attempting to set a mark on a %NULL entry does not succeed.
19149b89a035SMatthew Wilcox *
19159b89a035SMatthew Wilcox * Context: Any context. Expects xa_lock to be held on entry.
19169b89a035SMatthew Wilcox */
__xa_set_mark(struct xarray * xa,unsigned long index,xa_mark_t mark)19179b89a035SMatthew Wilcox void __xa_set_mark(struct xarray *xa, unsigned long index, xa_mark_t mark)
19189b89a035SMatthew Wilcox {
19199b89a035SMatthew Wilcox XA_STATE(xas, xa, index);
19209b89a035SMatthew Wilcox void *entry = xas_load(&xas);
19219b89a035SMatthew Wilcox
19229b89a035SMatthew Wilcox if (entry)
19239b89a035SMatthew Wilcox xas_set_mark(&xas, mark);
19249b89a035SMatthew Wilcox }
19259ee5a3b7SMatthew Wilcox EXPORT_SYMBOL(__xa_set_mark);
19269b89a035SMatthew Wilcox
19279b89a035SMatthew Wilcox /**
19289b89a035SMatthew Wilcox * __xa_clear_mark() - Clear this mark on this entry while locked.
19299b89a035SMatthew Wilcox * @xa: XArray.
19309b89a035SMatthew Wilcox * @index: Index of entry.
19319b89a035SMatthew Wilcox * @mark: Mark number.
19329b89a035SMatthew Wilcox *
19339b89a035SMatthew Wilcox * Context: Any context. Expects xa_lock to be held on entry.
19349b89a035SMatthew Wilcox */
__xa_clear_mark(struct xarray * xa,unsigned long index,xa_mark_t mark)19359b89a035SMatthew Wilcox void __xa_clear_mark(struct xarray *xa, unsigned long index, xa_mark_t mark)
19369b89a035SMatthew Wilcox {
19379b89a035SMatthew Wilcox XA_STATE(xas, xa, index);
19389b89a035SMatthew Wilcox void *entry = xas_load(&xas);
19399b89a035SMatthew Wilcox
19409b89a035SMatthew Wilcox if (entry)
19419b89a035SMatthew Wilcox xas_clear_mark(&xas, mark);
19429b89a035SMatthew Wilcox }
19439ee5a3b7SMatthew Wilcox EXPORT_SYMBOL(__xa_clear_mark);
19449b89a035SMatthew Wilcox
19459b89a035SMatthew Wilcox /**
19469b89a035SMatthew Wilcox * xa_get_mark() - Inquire whether this mark is set on this entry.
19479b89a035SMatthew Wilcox * @xa: XArray.
19489b89a035SMatthew Wilcox * @index: Index of entry.
19499b89a035SMatthew Wilcox * @mark: Mark number.
19509b89a035SMatthew Wilcox *
19519b89a035SMatthew Wilcox * This function uses the RCU read lock, so the result may be out of date
19529b89a035SMatthew Wilcox * by the time it returns. If you need the result to be stable, use a lock.
19539b89a035SMatthew Wilcox *
19549b89a035SMatthew Wilcox * Context: Any context. Takes and releases the RCU lock.
19559b89a035SMatthew Wilcox * Return: True if the entry at @index has this mark set, false if it doesn't.
19569b89a035SMatthew Wilcox */
xa_get_mark(struct xarray * xa,unsigned long index,xa_mark_t mark)19579b89a035SMatthew Wilcox bool xa_get_mark(struct xarray *xa, unsigned long index, xa_mark_t mark)
19589b89a035SMatthew Wilcox {
19599b89a035SMatthew Wilcox XA_STATE(xas, xa, index);
19609b89a035SMatthew Wilcox void *entry;
19619b89a035SMatthew Wilcox
19629b89a035SMatthew Wilcox rcu_read_lock();
19639b89a035SMatthew Wilcox entry = xas_start(&xas);
19649b89a035SMatthew Wilcox while (xas_get_mark(&xas, mark)) {
19659b89a035SMatthew Wilcox if (!xa_is_node(entry))
19669b89a035SMatthew Wilcox goto found;
19679b89a035SMatthew Wilcox entry = xas_descend(&xas, xa_to_node(entry));
19689b89a035SMatthew Wilcox }
19699b89a035SMatthew Wilcox rcu_read_unlock();
19709b89a035SMatthew Wilcox return false;
19719b89a035SMatthew Wilcox found:
19729b89a035SMatthew Wilcox rcu_read_unlock();
19739b89a035SMatthew Wilcox return true;
19749b89a035SMatthew Wilcox }
19759b89a035SMatthew Wilcox EXPORT_SYMBOL(xa_get_mark);
19769b89a035SMatthew Wilcox
19779b89a035SMatthew Wilcox /**
19789b89a035SMatthew Wilcox * xa_set_mark() - Set this mark on this entry.
19799b89a035SMatthew Wilcox * @xa: XArray.
19809b89a035SMatthew Wilcox * @index: Index of entry.
19819b89a035SMatthew Wilcox * @mark: Mark number.
19829b89a035SMatthew Wilcox *
1983804dfaf0SMatthew Wilcox * Attempting to set a mark on a %NULL entry does not succeed.
19849b89a035SMatthew Wilcox *
19859b89a035SMatthew Wilcox * Context: Process context. Takes and releases the xa_lock.
19869b89a035SMatthew Wilcox */
xa_set_mark(struct xarray * xa,unsigned long index,xa_mark_t mark)19879b89a035SMatthew Wilcox void xa_set_mark(struct xarray *xa, unsigned long index, xa_mark_t mark)
19889b89a035SMatthew Wilcox {
19899b89a035SMatthew Wilcox xa_lock(xa);
19909b89a035SMatthew Wilcox __xa_set_mark(xa, index, mark);
19919b89a035SMatthew Wilcox xa_unlock(xa);
19929b89a035SMatthew Wilcox }
19939b89a035SMatthew Wilcox EXPORT_SYMBOL(xa_set_mark);
19949b89a035SMatthew Wilcox
19959b89a035SMatthew Wilcox /**
19969b89a035SMatthew Wilcox * xa_clear_mark() - Clear this mark on this entry.
19979b89a035SMatthew Wilcox * @xa: XArray.
19989b89a035SMatthew Wilcox * @index: Index of entry.
19999b89a035SMatthew Wilcox * @mark: Mark number.
20009b89a035SMatthew Wilcox *
20019b89a035SMatthew Wilcox * Clearing a mark always succeeds.
20029b89a035SMatthew Wilcox *
20039b89a035SMatthew Wilcox * Context: Process context. Takes and releases the xa_lock.
20049b89a035SMatthew Wilcox */
xa_clear_mark(struct xarray * xa,unsigned long index,xa_mark_t mark)20059b89a035SMatthew Wilcox void xa_clear_mark(struct xarray *xa, unsigned long index, xa_mark_t mark)
20069b89a035SMatthew Wilcox {
20079b89a035SMatthew Wilcox xa_lock(xa);
20089b89a035SMatthew Wilcox __xa_clear_mark(xa, index, mark);
20099b89a035SMatthew Wilcox xa_unlock(xa);
20109b89a035SMatthew Wilcox }
20119b89a035SMatthew Wilcox EXPORT_SYMBOL(xa_clear_mark);
20129b89a035SMatthew Wilcox
2013b803b428SMatthew Wilcox /**
2014b803b428SMatthew Wilcox * xa_find() - Search the XArray for an entry.
2015b803b428SMatthew Wilcox * @xa: XArray.
2016b803b428SMatthew Wilcox * @indexp: Pointer to an index.
2017b803b428SMatthew Wilcox * @max: Maximum index to search to.
2018b803b428SMatthew Wilcox * @filter: Selection criterion.
2019b803b428SMatthew Wilcox *
2020b803b428SMatthew Wilcox * Finds the entry in @xa which matches the @filter, and has the lowest
2021b803b428SMatthew Wilcox * index that is at least @indexp and no more than @max.
2022b803b428SMatthew Wilcox * If an entry is found, @indexp is updated to be the index of the entry.
2023b803b428SMatthew Wilcox * This function is protected by the RCU read lock, so it may not find
2024b803b428SMatthew Wilcox * entries which are being simultaneously added. It will not return an
2025b803b428SMatthew Wilcox * %XA_RETRY_ENTRY; if you need to see retry entries, use xas_find().
2026b803b428SMatthew Wilcox *
2027b803b428SMatthew Wilcox * Context: Any context. Takes and releases the RCU lock.
2028b803b428SMatthew Wilcox * Return: The entry, if found, otherwise %NULL.
2029b803b428SMatthew Wilcox */
xa_find(struct xarray * xa,unsigned long * indexp,unsigned long max,xa_mark_t filter)2030b803b428SMatthew Wilcox void *xa_find(struct xarray *xa, unsigned long *indexp,
2031b803b428SMatthew Wilcox unsigned long max, xa_mark_t filter)
2032b803b428SMatthew Wilcox {
2033b803b428SMatthew Wilcox XA_STATE(xas, xa, *indexp);
2034b803b428SMatthew Wilcox void *entry;
2035b803b428SMatthew Wilcox
2036b803b428SMatthew Wilcox rcu_read_lock();
2037b803b428SMatthew Wilcox do {
2038b803b428SMatthew Wilcox if ((__force unsigned int)filter < XA_MAX_MARKS)
2039b803b428SMatthew Wilcox entry = xas_find_marked(&xas, max, filter);
2040b803b428SMatthew Wilcox else
2041b803b428SMatthew Wilcox entry = xas_find(&xas, max);
2042b803b428SMatthew Wilcox } while (xas_retry(&xas, entry));
2043b803b428SMatthew Wilcox rcu_read_unlock();
2044b803b428SMatthew Wilcox
2045b803b428SMatthew Wilcox if (entry)
2046b803b428SMatthew Wilcox *indexp = xas.xa_index;
2047b803b428SMatthew Wilcox return entry;
2048b803b428SMatthew Wilcox }
2049b803b428SMatthew Wilcox EXPORT_SYMBOL(xa_find);
2050b803b428SMatthew Wilcox
xas_sibling(struct xa_state * xas)205119c30f4dSMatthew Wilcox (Oracle) static bool xas_sibling(struct xa_state *xas)
205219c30f4dSMatthew Wilcox (Oracle) {
205319c30f4dSMatthew Wilcox (Oracle) struct xa_node *node = xas->xa_node;
205419c30f4dSMatthew Wilcox (Oracle) unsigned long mask;
205519c30f4dSMatthew Wilcox (Oracle)
2056d8e93e3fSMatthew Wilcox (Oracle) if (!IS_ENABLED(CONFIG_XARRAY_MULTI) || !node)
205719c30f4dSMatthew Wilcox (Oracle) return false;
205819c30f4dSMatthew Wilcox (Oracle) mask = (XA_CHUNK_SIZE << node->shift) - 1;
2059bd40b17cSMatthew Wilcox (Oracle) return (xas->xa_index & mask) >
2060bd40b17cSMatthew Wilcox (Oracle) ((unsigned long)xas->xa_offset << node->shift);
206119c30f4dSMatthew Wilcox (Oracle) }
206219c30f4dSMatthew Wilcox (Oracle)
2063b803b428SMatthew Wilcox /**
2064b803b428SMatthew Wilcox * xa_find_after() - Search the XArray for a present entry.
2065b803b428SMatthew Wilcox * @xa: XArray.
2066b803b428SMatthew Wilcox * @indexp: Pointer to an index.
2067b803b428SMatthew Wilcox * @max: Maximum index to search to.
2068b803b428SMatthew Wilcox * @filter: Selection criterion.
2069b803b428SMatthew Wilcox *
2070b803b428SMatthew Wilcox * Finds the entry in @xa which matches the @filter and has the lowest
2071b803b428SMatthew Wilcox * index that is above @indexp and no more than @max.
2072b803b428SMatthew Wilcox * If an entry is found, @indexp is updated to be the index of the entry.
2073b803b428SMatthew Wilcox * This function is protected by the RCU read lock, so it may miss entries
2074b803b428SMatthew Wilcox * which are being simultaneously added. It will not return an
2075b803b428SMatthew Wilcox * %XA_RETRY_ENTRY; if you need to see retry entries, use xas_find().
2076b803b428SMatthew Wilcox *
2077b803b428SMatthew Wilcox * Context: Any context. Takes and releases the RCU lock.
2078b803b428SMatthew Wilcox * Return: The pointer, if found, otherwise %NULL.
2079b803b428SMatthew Wilcox */
xa_find_after(struct xarray * xa,unsigned long * indexp,unsigned long max,xa_mark_t filter)2080b803b428SMatthew Wilcox void *xa_find_after(struct xarray *xa, unsigned long *indexp,
2081b803b428SMatthew Wilcox unsigned long max, xa_mark_t filter)
2082b803b428SMatthew Wilcox {
2083b803b428SMatthew Wilcox XA_STATE(xas, xa, *indexp + 1);
2084b803b428SMatthew Wilcox void *entry;
2085b803b428SMatthew Wilcox
2086430f24f9SMatthew Wilcox (Oracle) if (xas.xa_index == 0)
2087430f24f9SMatthew Wilcox (Oracle) return NULL;
2088430f24f9SMatthew Wilcox (Oracle)
2089b803b428SMatthew Wilcox rcu_read_lock();
2090b803b428SMatthew Wilcox for (;;) {
2091b803b428SMatthew Wilcox if ((__force unsigned int)filter < XA_MAX_MARKS)
2092b803b428SMatthew Wilcox entry = xas_find_marked(&xas, max, filter);
2093b803b428SMatthew Wilcox else
2094b803b428SMatthew Wilcox entry = xas_find(&xas, max);
2095c44aa5e8SMatthew Wilcox (Oracle)
2096c44aa5e8SMatthew Wilcox (Oracle) if (xas_invalid(&xas))
20978229706eSMatthew Wilcox break;
209819c30f4dSMatthew Wilcox (Oracle) if (xas_sibling(&xas))
2099b803b428SMatthew Wilcox continue;
2100b803b428SMatthew Wilcox if (!xas_retry(&xas, entry))
2101b803b428SMatthew Wilcox break;
2102b803b428SMatthew Wilcox }
2103b803b428SMatthew Wilcox rcu_read_unlock();
2104b803b428SMatthew Wilcox
2105b803b428SMatthew Wilcox if (entry)
2106b803b428SMatthew Wilcox *indexp = xas.xa_index;
2107b803b428SMatthew Wilcox return entry;
2108b803b428SMatthew Wilcox }
2109b803b428SMatthew Wilcox EXPORT_SYMBOL(xa_find_after);
2110b803b428SMatthew Wilcox
xas_extract_present(struct xa_state * xas,void ** dst,unsigned long max,unsigned int n)211180a0a1a9SMatthew Wilcox static unsigned int xas_extract_present(struct xa_state *xas, void **dst,
211280a0a1a9SMatthew Wilcox unsigned long max, unsigned int n)
211380a0a1a9SMatthew Wilcox {
211480a0a1a9SMatthew Wilcox void *entry;
211580a0a1a9SMatthew Wilcox unsigned int i = 0;
211680a0a1a9SMatthew Wilcox
211780a0a1a9SMatthew Wilcox rcu_read_lock();
211880a0a1a9SMatthew Wilcox xas_for_each(xas, entry, max) {
211980a0a1a9SMatthew Wilcox if (xas_retry(xas, entry))
212080a0a1a9SMatthew Wilcox continue;
212180a0a1a9SMatthew Wilcox dst[i++] = entry;
212280a0a1a9SMatthew Wilcox if (i == n)
212380a0a1a9SMatthew Wilcox break;
212480a0a1a9SMatthew Wilcox }
212580a0a1a9SMatthew Wilcox rcu_read_unlock();
212680a0a1a9SMatthew Wilcox
212780a0a1a9SMatthew Wilcox return i;
212880a0a1a9SMatthew Wilcox }
212980a0a1a9SMatthew Wilcox
xas_extract_marked(struct xa_state * xas,void ** dst,unsigned long max,unsigned int n,xa_mark_t mark)213080a0a1a9SMatthew Wilcox static unsigned int xas_extract_marked(struct xa_state *xas, void **dst,
213180a0a1a9SMatthew Wilcox unsigned long max, unsigned int n, xa_mark_t mark)
213280a0a1a9SMatthew Wilcox {
213380a0a1a9SMatthew Wilcox void *entry;
213480a0a1a9SMatthew Wilcox unsigned int i = 0;
213580a0a1a9SMatthew Wilcox
213680a0a1a9SMatthew Wilcox rcu_read_lock();
213780a0a1a9SMatthew Wilcox xas_for_each_marked(xas, entry, max, mark) {
213880a0a1a9SMatthew Wilcox if (xas_retry(xas, entry))
213980a0a1a9SMatthew Wilcox continue;
214080a0a1a9SMatthew Wilcox dst[i++] = entry;
214180a0a1a9SMatthew Wilcox if (i == n)
214280a0a1a9SMatthew Wilcox break;
214380a0a1a9SMatthew Wilcox }
214480a0a1a9SMatthew Wilcox rcu_read_unlock();
214580a0a1a9SMatthew Wilcox
214680a0a1a9SMatthew Wilcox return i;
214780a0a1a9SMatthew Wilcox }
214880a0a1a9SMatthew Wilcox
214980a0a1a9SMatthew Wilcox /**
215080a0a1a9SMatthew Wilcox * xa_extract() - Copy selected entries from the XArray into a normal array.
215180a0a1a9SMatthew Wilcox * @xa: The source XArray to copy from.
215280a0a1a9SMatthew Wilcox * @dst: The buffer to copy entries into.
215380a0a1a9SMatthew Wilcox * @start: The first index in the XArray eligible to be selected.
215480a0a1a9SMatthew Wilcox * @max: The last index in the XArray eligible to be selected.
215580a0a1a9SMatthew Wilcox * @n: The maximum number of entries to copy.
215680a0a1a9SMatthew Wilcox * @filter: Selection criterion.
215780a0a1a9SMatthew Wilcox *
215880a0a1a9SMatthew Wilcox * Copies up to @n entries that match @filter from the XArray. The
215980a0a1a9SMatthew Wilcox * copied entries will have indices between @start and @max, inclusive.
216080a0a1a9SMatthew Wilcox *
216180a0a1a9SMatthew Wilcox * The @filter may be an XArray mark value, in which case entries which are
216280a0a1a9SMatthew Wilcox * marked with that mark will be copied. It may also be %XA_PRESENT, in
2163804dfaf0SMatthew Wilcox * which case all entries which are not %NULL will be copied.
216480a0a1a9SMatthew Wilcox *
216580a0a1a9SMatthew Wilcox * The entries returned may not represent a snapshot of the XArray at a
216680a0a1a9SMatthew Wilcox * moment in time. For example, if another thread stores to index 5, then
216780a0a1a9SMatthew Wilcox * index 10, calling xa_extract() may return the old contents of index 5
216880a0a1a9SMatthew Wilcox * and the new contents of index 10. Indices not modified while this
216980a0a1a9SMatthew Wilcox * function is running will not be skipped.
217080a0a1a9SMatthew Wilcox *
217180a0a1a9SMatthew Wilcox * If you need stronger guarantees, holding the xa_lock across calls to this
217280a0a1a9SMatthew Wilcox * function will prevent concurrent modification.
217380a0a1a9SMatthew Wilcox *
217480a0a1a9SMatthew Wilcox * Context: Any context. Takes and releases the RCU lock.
217580a0a1a9SMatthew Wilcox * Return: The number of entries copied.
217680a0a1a9SMatthew Wilcox */
xa_extract(struct xarray * xa,void ** dst,unsigned long start,unsigned long max,unsigned int n,xa_mark_t filter)217780a0a1a9SMatthew Wilcox unsigned int xa_extract(struct xarray *xa, void **dst, unsigned long start,
217880a0a1a9SMatthew Wilcox unsigned long max, unsigned int n, xa_mark_t filter)
217980a0a1a9SMatthew Wilcox {
218080a0a1a9SMatthew Wilcox XA_STATE(xas, xa, start);
218180a0a1a9SMatthew Wilcox
218280a0a1a9SMatthew Wilcox if (!n)
218380a0a1a9SMatthew Wilcox return 0;
218480a0a1a9SMatthew Wilcox
218580a0a1a9SMatthew Wilcox if ((__force unsigned int)filter < XA_MAX_MARKS)
218680a0a1a9SMatthew Wilcox return xas_extract_marked(&xas, dst, max, n, filter);
218780a0a1a9SMatthew Wilcox return xas_extract_present(&xas, dst, max, n);
218880a0a1a9SMatthew Wilcox }
218980a0a1a9SMatthew Wilcox EXPORT_SYMBOL(xa_extract);
219080a0a1a9SMatthew Wilcox
2191687149fcSMatthew Wilcox /**
2192f82cd2f0SMatthew Wilcox (Oracle) * xa_delete_node() - Private interface for workingset code.
2193f82cd2f0SMatthew Wilcox (Oracle) * @node: Node to be removed from the tree.
2194f82cd2f0SMatthew Wilcox (Oracle) * @update: Function to call to update ancestor nodes.
2195f82cd2f0SMatthew Wilcox (Oracle) *
2196f82cd2f0SMatthew Wilcox (Oracle) * Context: xa_lock must be held on entry and will not be released.
2197f82cd2f0SMatthew Wilcox (Oracle) */
xa_delete_node(struct xa_node * node,xa_update_node_t update)2198f82cd2f0SMatthew Wilcox (Oracle) void xa_delete_node(struct xa_node *node, xa_update_node_t update)
2199f82cd2f0SMatthew Wilcox (Oracle) {
2200f82cd2f0SMatthew Wilcox (Oracle) struct xa_state xas = {
2201f82cd2f0SMatthew Wilcox (Oracle) .xa = node->array,
2202f82cd2f0SMatthew Wilcox (Oracle) .xa_index = (unsigned long)node->offset <<
2203f82cd2f0SMatthew Wilcox (Oracle) (node->shift + XA_CHUNK_SHIFT),
2204f82cd2f0SMatthew Wilcox (Oracle) .xa_shift = node->shift + XA_CHUNK_SHIFT,
2205f82cd2f0SMatthew Wilcox (Oracle) .xa_offset = node->offset,
2206f82cd2f0SMatthew Wilcox (Oracle) .xa_node = xa_parent_locked(node->array, node),
2207f82cd2f0SMatthew Wilcox (Oracle) .xa_update = update,
2208f82cd2f0SMatthew Wilcox (Oracle) };
2209f82cd2f0SMatthew Wilcox (Oracle)
2210f82cd2f0SMatthew Wilcox (Oracle) xas_store(&xas, NULL);
2211f82cd2f0SMatthew Wilcox (Oracle) }
2212f82cd2f0SMatthew Wilcox (Oracle) EXPORT_SYMBOL_GPL(xa_delete_node); /* For the benefit of the test suite */
2213f82cd2f0SMatthew Wilcox (Oracle)
2214f82cd2f0SMatthew Wilcox (Oracle) /**
2215687149fcSMatthew Wilcox * xa_destroy() - Free all internal data structures.
2216687149fcSMatthew Wilcox * @xa: XArray.
2217687149fcSMatthew Wilcox *
2218687149fcSMatthew Wilcox * After calling this function, the XArray is empty and has freed all memory
2219687149fcSMatthew Wilcox * allocated for its internal data structures. You are responsible for
2220687149fcSMatthew Wilcox * freeing the objects referenced by the XArray.
2221687149fcSMatthew Wilcox *
2222687149fcSMatthew Wilcox * Context: Any context. Takes and releases the xa_lock, interrupt-safe.
2223687149fcSMatthew Wilcox */
xa_destroy(struct xarray * xa)2224687149fcSMatthew Wilcox void xa_destroy(struct xarray *xa)
2225687149fcSMatthew Wilcox {
2226687149fcSMatthew Wilcox XA_STATE(xas, xa, 0);
2227687149fcSMatthew Wilcox unsigned long flags;
2228687149fcSMatthew Wilcox void *entry;
2229687149fcSMatthew Wilcox
2230687149fcSMatthew Wilcox xas.xa_node = NULL;
2231687149fcSMatthew Wilcox xas_lock_irqsave(&xas, flags);
2232687149fcSMatthew Wilcox entry = xa_head_locked(xa);
2233687149fcSMatthew Wilcox RCU_INIT_POINTER(xa->xa_head, NULL);
2234687149fcSMatthew Wilcox xas_init_marks(&xas);
22353ccaf57aSMatthew Wilcox if (xa_zero_busy(xa))
22363ccaf57aSMatthew Wilcox xa_mark_clear(xa, XA_FREE_MARK);
2237687149fcSMatthew Wilcox /* lockdep checks we're still holding the lock in xas_free_nodes() */
2238687149fcSMatthew Wilcox if (xa_is_node(entry))
2239687149fcSMatthew Wilcox xas_free_nodes(&xas, xa_to_node(entry));
2240687149fcSMatthew Wilcox xas_unlock_irqrestore(&xas, flags);
2241687149fcSMatthew Wilcox }
2242687149fcSMatthew Wilcox EXPORT_SYMBOL(xa_destroy);
2243687149fcSMatthew Wilcox
2244ad3d6c72SMatthew Wilcox #ifdef XA_DEBUG
xa_dump_node(const struct xa_node * node)2245ad3d6c72SMatthew Wilcox void xa_dump_node(const struct xa_node *node)
2246ad3d6c72SMatthew Wilcox {
2247ad3d6c72SMatthew Wilcox unsigned i, j;
2248ad3d6c72SMatthew Wilcox
2249ad3d6c72SMatthew Wilcox if (!node)
2250ad3d6c72SMatthew Wilcox return;
2251ad3d6c72SMatthew Wilcox if ((unsigned long)node & 3) {
2252ad3d6c72SMatthew Wilcox pr_cont("node %px\n", node);
2253ad3d6c72SMatthew Wilcox return;
2254ad3d6c72SMatthew Wilcox }
2255ad3d6c72SMatthew Wilcox
2256ad3d6c72SMatthew Wilcox pr_cont("node %px %s %d parent %px shift %d count %d values %d "
2257ad3d6c72SMatthew Wilcox "array %px list %px %px marks",
2258ad3d6c72SMatthew Wilcox node, node->parent ? "offset" : "max", node->offset,
2259ad3d6c72SMatthew Wilcox node->parent, node->shift, node->count, node->nr_values,
2260ad3d6c72SMatthew Wilcox node->array, node->private_list.prev, node->private_list.next);
2261ad3d6c72SMatthew Wilcox for (i = 0; i < XA_MAX_MARKS; i++)
2262ad3d6c72SMatthew Wilcox for (j = 0; j < XA_MARK_LONGS; j++)
2263ad3d6c72SMatthew Wilcox pr_cont(" %lx", node->marks[i][j]);
2264ad3d6c72SMatthew Wilcox pr_cont("\n");
2265ad3d6c72SMatthew Wilcox }
2266ad3d6c72SMatthew Wilcox
xa_dump_index(unsigned long index,unsigned int shift)2267ad3d6c72SMatthew Wilcox void xa_dump_index(unsigned long index, unsigned int shift)
2268ad3d6c72SMatthew Wilcox {
2269ad3d6c72SMatthew Wilcox if (!shift)
2270ad3d6c72SMatthew Wilcox pr_info("%lu: ", index);
2271ad3d6c72SMatthew Wilcox else if (shift >= BITS_PER_LONG)
2272ad3d6c72SMatthew Wilcox pr_info("0-%lu: ", ~0UL);
2273ad3d6c72SMatthew Wilcox else
2274ad3d6c72SMatthew Wilcox pr_info("%lu-%lu: ", index, index | ((1UL << shift) - 1));
2275ad3d6c72SMatthew Wilcox }
2276ad3d6c72SMatthew Wilcox
xa_dump_entry(const void * entry,unsigned long index,unsigned long shift)2277ad3d6c72SMatthew Wilcox void xa_dump_entry(const void *entry, unsigned long index, unsigned long shift)
2278ad3d6c72SMatthew Wilcox {
2279ad3d6c72SMatthew Wilcox if (!entry)
2280ad3d6c72SMatthew Wilcox return;
2281ad3d6c72SMatthew Wilcox
2282ad3d6c72SMatthew Wilcox xa_dump_index(index, shift);
2283ad3d6c72SMatthew Wilcox
2284ad3d6c72SMatthew Wilcox if (xa_is_node(entry)) {
2285ad3d6c72SMatthew Wilcox if (shift == 0) {
2286ad3d6c72SMatthew Wilcox pr_cont("%px\n", entry);
2287ad3d6c72SMatthew Wilcox } else {
2288ad3d6c72SMatthew Wilcox unsigned long i;
2289ad3d6c72SMatthew Wilcox struct xa_node *node = xa_to_node(entry);
2290ad3d6c72SMatthew Wilcox xa_dump_node(node);
2291ad3d6c72SMatthew Wilcox for (i = 0; i < XA_CHUNK_SIZE; i++)
2292ad3d6c72SMatthew Wilcox xa_dump_entry(node->slots[i],
2293ad3d6c72SMatthew Wilcox index + (i << node->shift), node->shift);
2294ad3d6c72SMatthew Wilcox }
2295ad3d6c72SMatthew Wilcox } else if (xa_is_value(entry))
2296ad3d6c72SMatthew Wilcox pr_cont("value %ld (0x%lx) [%px]\n", xa_to_value(entry),
2297ad3d6c72SMatthew Wilcox xa_to_value(entry), entry);
2298ad3d6c72SMatthew Wilcox else if (!xa_is_internal(entry))
2299ad3d6c72SMatthew Wilcox pr_cont("%px\n", entry);
2300ad3d6c72SMatthew Wilcox else if (xa_is_retry(entry))
2301ad3d6c72SMatthew Wilcox pr_cont("retry (%ld)\n", xa_to_internal(entry));
2302ad3d6c72SMatthew Wilcox else if (xa_is_sibling(entry))
2303ad3d6c72SMatthew Wilcox pr_cont("sibling (slot %ld)\n", xa_to_sibling(entry));
23049f14d4f1SMatthew Wilcox else if (xa_is_zero(entry))
23059f14d4f1SMatthew Wilcox pr_cont("zero (%ld)\n", xa_to_internal(entry));
2306ad3d6c72SMatthew Wilcox else
2307ad3d6c72SMatthew Wilcox pr_cont("UNKNOWN ENTRY (%px)\n", entry);
2308ad3d6c72SMatthew Wilcox }
2309ad3d6c72SMatthew Wilcox
xa_dump(const struct xarray * xa)2310ad3d6c72SMatthew Wilcox void xa_dump(const struct xarray *xa)
2311ad3d6c72SMatthew Wilcox {
2312ad3d6c72SMatthew Wilcox void *entry = xa->xa_head;
2313ad3d6c72SMatthew Wilcox unsigned int shift = 0;
2314ad3d6c72SMatthew Wilcox
2315ad3d6c72SMatthew Wilcox pr_info("xarray: %px head %px flags %x marks %d %d %d\n", xa, entry,
23169b89a035SMatthew Wilcox xa->xa_flags, xa_marked(xa, XA_MARK_0),
23179b89a035SMatthew Wilcox xa_marked(xa, XA_MARK_1), xa_marked(xa, XA_MARK_2));
2318ad3d6c72SMatthew Wilcox if (xa_is_node(entry))
2319ad3d6c72SMatthew Wilcox shift = xa_to_node(entry)->shift + XA_CHUNK_SHIFT;
2320ad3d6c72SMatthew Wilcox xa_dump_entry(entry, 0, shift);
2321ad3d6c72SMatthew Wilcox }
2322ad3d6c72SMatthew Wilcox #endif
2323