1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0+
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * XArray implementation
4*4882a593Smuzhiyun * Copyright (c) 2017-2018 Microsoft Corporation
5*4882a593Smuzhiyun * Copyright (c) 2018-2020 Oracle
6*4882a593Smuzhiyun * Author: Matthew Wilcox <willy@infradead.org>
7*4882a593Smuzhiyun */
8*4882a593Smuzhiyun
9*4882a593Smuzhiyun #include <linux/bitmap.h>
10*4882a593Smuzhiyun #include <linux/export.h>
11*4882a593Smuzhiyun #include <linux/list.h>
12*4882a593Smuzhiyun #include <linux/slab.h>
13*4882a593Smuzhiyun #include <linux/xarray.h>
14*4882a593Smuzhiyun
15*4882a593Smuzhiyun /*
16*4882a593Smuzhiyun * Coding conventions in this file:
17*4882a593Smuzhiyun *
18*4882a593Smuzhiyun * @xa is used to refer to the entire xarray.
19*4882a593Smuzhiyun * @xas is the 'xarray operation state'. It may be either a pointer to
20*4882a593Smuzhiyun * an xa_state, or an xa_state stored on the stack. This is an unfortunate
21*4882a593Smuzhiyun * ambiguity.
22*4882a593Smuzhiyun * @index is the index of the entry being operated on
23*4882a593Smuzhiyun * @mark is an xa_mark_t; a small number indicating one of the mark bits.
24*4882a593Smuzhiyun * @node refers to an xa_node; usually the primary one being operated on by
25*4882a593Smuzhiyun * this function.
26*4882a593Smuzhiyun * @offset is the index into the slots array inside an xa_node.
27*4882a593Smuzhiyun * @parent refers to the @xa_node closer to the head than @node.
28*4882a593Smuzhiyun * @entry refers to something stored in a slot in the xarray
29*4882a593Smuzhiyun */
30*4882a593Smuzhiyun
xa_lock_type(const struct xarray * xa)31*4882a593Smuzhiyun static inline unsigned int xa_lock_type(const struct xarray *xa)
32*4882a593Smuzhiyun {
33*4882a593Smuzhiyun return (__force unsigned int)xa->xa_flags & 3;
34*4882a593Smuzhiyun }
35*4882a593Smuzhiyun
xas_lock_type(struct xa_state * xas,unsigned int lock_type)36*4882a593Smuzhiyun static inline void xas_lock_type(struct xa_state *xas, unsigned int lock_type)
37*4882a593Smuzhiyun {
38*4882a593Smuzhiyun if (lock_type == XA_LOCK_IRQ)
39*4882a593Smuzhiyun xas_lock_irq(xas);
40*4882a593Smuzhiyun else if (lock_type == XA_LOCK_BH)
41*4882a593Smuzhiyun xas_lock_bh(xas);
42*4882a593Smuzhiyun else
43*4882a593Smuzhiyun xas_lock(xas);
44*4882a593Smuzhiyun }
45*4882a593Smuzhiyun
xas_unlock_type(struct xa_state * xas,unsigned int lock_type)46*4882a593Smuzhiyun static inline void xas_unlock_type(struct xa_state *xas, unsigned int lock_type)
47*4882a593Smuzhiyun {
48*4882a593Smuzhiyun if (lock_type == XA_LOCK_IRQ)
49*4882a593Smuzhiyun xas_unlock_irq(xas);
50*4882a593Smuzhiyun else if (lock_type == XA_LOCK_BH)
51*4882a593Smuzhiyun xas_unlock_bh(xas);
52*4882a593Smuzhiyun else
53*4882a593Smuzhiyun xas_unlock(xas);
54*4882a593Smuzhiyun }
55*4882a593Smuzhiyun
xa_track_free(const struct xarray * xa)56*4882a593Smuzhiyun static inline bool xa_track_free(const struct xarray *xa)
57*4882a593Smuzhiyun {
58*4882a593Smuzhiyun return xa->xa_flags & XA_FLAGS_TRACK_FREE;
59*4882a593Smuzhiyun }
60*4882a593Smuzhiyun
xa_zero_busy(const struct xarray * xa)61*4882a593Smuzhiyun static inline bool xa_zero_busy(const struct xarray *xa)
62*4882a593Smuzhiyun {
63*4882a593Smuzhiyun return xa->xa_flags & XA_FLAGS_ZERO_BUSY;
64*4882a593Smuzhiyun }
65*4882a593Smuzhiyun
xa_mark_set(struct xarray * xa,xa_mark_t mark)66*4882a593Smuzhiyun static inline void xa_mark_set(struct xarray *xa, xa_mark_t mark)
67*4882a593Smuzhiyun {
68*4882a593Smuzhiyun if (!(xa->xa_flags & XA_FLAGS_MARK(mark)))
69*4882a593Smuzhiyun xa->xa_flags |= XA_FLAGS_MARK(mark);
70*4882a593Smuzhiyun }
71*4882a593Smuzhiyun
xa_mark_clear(struct xarray * xa,xa_mark_t mark)72*4882a593Smuzhiyun static inline void xa_mark_clear(struct xarray *xa, xa_mark_t mark)
73*4882a593Smuzhiyun {
74*4882a593Smuzhiyun if (xa->xa_flags & XA_FLAGS_MARK(mark))
75*4882a593Smuzhiyun xa->xa_flags &= ~(XA_FLAGS_MARK(mark));
76*4882a593Smuzhiyun }
77*4882a593Smuzhiyun
node_marks(struct xa_node * node,xa_mark_t mark)78*4882a593Smuzhiyun static inline unsigned long *node_marks(struct xa_node *node, xa_mark_t mark)
79*4882a593Smuzhiyun {
80*4882a593Smuzhiyun return node->marks[(__force unsigned)mark];
81*4882a593Smuzhiyun }
82*4882a593Smuzhiyun
node_get_mark(struct xa_node * node,unsigned int offset,xa_mark_t mark)83*4882a593Smuzhiyun static inline bool node_get_mark(struct xa_node *node,
84*4882a593Smuzhiyun unsigned int offset, xa_mark_t mark)
85*4882a593Smuzhiyun {
86*4882a593Smuzhiyun return test_bit(offset, node_marks(node, mark));
87*4882a593Smuzhiyun }
88*4882a593Smuzhiyun
89*4882a593Smuzhiyun /* returns true if the bit was set */
node_set_mark(struct xa_node * node,unsigned int offset,xa_mark_t mark)90*4882a593Smuzhiyun static inline bool node_set_mark(struct xa_node *node, unsigned int offset,
91*4882a593Smuzhiyun xa_mark_t mark)
92*4882a593Smuzhiyun {
93*4882a593Smuzhiyun return __test_and_set_bit(offset, node_marks(node, mark));
94*4882a593Smuzhiyun }
95*4882a593Smuzhiyun
96*4882a593Smuzhiyun /* returns true if the bit was set */
node_clear_mark(struct xa_node * node,unsigned int offset,xa_mark_t mark)97*4882a593Smuzhiyun static inline bool node_clear_mark(struct xa_node *node, unsigned int offset,
98*4882a593Smuzhiyun xa_mark_t mark)
99*4882a593Smuzhiyun {
100*4882a593Smuzhiyun return __test_and_clear_bit(offset, node_marks(node, mark));
101*4882a593Smuzhiyun }
102*4882a593Smuzhiyun
node_any_mark(struct xa_node * node,xa_mark_t mark)103*4882a593Smuzhiyun static inline bool node_any_mark(struct xa_node *node, xa_mark_t mark)
104*4882a593Smuzhiyun {
105*4882a593Smuzhiyun return !bitmap_empty(node_marks(node, mark), XA_CHUNK_SIZE);
106*4882a593Smuzhiyun }
107*4882a593Smuzhiyun
node_mark_all(struct xa_node * node,xa_mark_t mark)108*4882a593Smuzhiyun static inline void node_mark_all(struct xa_node *node, xa_mark_t mark)
109*4882a593Smuzhiyun {
110*4882a593Smuzhiyun bitmap_fill(node_marks(node, mark), XA_CHUNK_SIZE);
111*4882a593Smuzhiyun }
112*4882a593Smuzhiyun
113*4882a593Smuzhiyun #define mark_inc(mark) do { \
114*4882a593Smuzhiyun mark = (__force xa_mark_t)((__force unsigned)(mark) + 1); \
115*4882a593Smuzhiyun } while (0)
116*4882a593Smuzhiyun
117*4882a593Smuzhiyun /*
118*4882a593Smuzhiyun * xas_squash_marks() - Merge all marks to the first entry
119*4882a593Smuzhiyun * @xas: Array operation state.
120*4882a593Smuzhiyun *
121*4882a593Smuzhiyun * Set a mark on the first entry if any entry has it set. Clear marks on
122*4882a593Smuzhiyun * all sibling entries.
123*4882a593Smuzhiyun */
xas_squash_marks(const struct xa_state * xas)124*4882a593Smuzhiyun static void xas_squash_marks(const struct xa_state *xas)
125*4882a593Smuzhiyun {
126*4882a593Smuzhiyun unsigned int mark = 0;
127*4882a593Smuzhiyun unsigned int limit = xas->xa_offset + xas->xa_sibs + 1;
128*4882a593Smuzhiyun
129*4882a593Smuzhiyun if (!xas->xa_sibs)
130*4882a593Smuzhiyun return;
131*4882a593Smuzhiyun
132*4882a593Smuzhiyun do {
133*4882a593Smuzhiyun unsigned long *marks = xas->xa_node->marks[mark];
134*4882a593Smuzhiyun if (find_next_bit(marks, limit, xas->xa_offset + 1) == limit)
135*4882a593Smuzhiyun continue;
136*4882a593Smuzhiyun __set_bit(xas->xa_offset, marks);
137*4882a593Smuzhiyun bitmap_clear(marks, xas->xa_offset + 1, xas->xa_sibs);
138*4882a593Smuzhiyun } while (mark++ != (__force unsigned)XA_MARK_MAX);
139*4882a593Smuzhiyun }
140*4882a593Smuzhiyun
141*4882a593Smuzhiyun /* extracts the offset within this node from the index */
get_offset(unsigned long index,struct xa_node * node)142*4882a593Smuzhiyun static unsigned int get_offset(unsigned long index, struct xa_node *node)
143*4882a593Smuzhiyun {
144*4882a593Smuzhiyun return (index >> node->shift) & XA_CHUNK_MASK;
145*4882a593Smuzhiyun }
146*4882a593Smuzhiyun
xas_set_offset(struct xa_state * xas)147*4882a593Smuzhiyun static void xas_set_offset(struct xa_state *xas)
148*4882a593Smuzhiyun {
149*4882a593Smuzhiyun xas->xa_offset = get_offset(xas->xa_index, xas->xa_node);
150*4882a593Smuzhiyun }
151*4882a593Smuzhiyun
152*4882a593Smuzhiyun /* move the index either forwards (find) or backwards (sibling slot) */
xas_move_index(struct xa_state * xas,unsigned long offset)153*4882a593Smuzhiyun static void xas_move_index(struct xa_state *xas, unsigned long offset)
154*4882a593Smuzhiyun {
155*4882a593Smuzhiyun unsigned int shift = xas->xa_node->shift;
156*4882a593Smuzhiyun xas->xa_index &= ~XA_CHUNK_MASK << shift;
157*4882a593Smuzhiyun xas->xa_index += offset << shift;
158*4882a593Smuzhiyun }
159*4882a593Smuzhiyun
xas_advance(struct xa_state * xas)160*4882a593Smuzhiyun static void xas_advance(struct xa_state *xas)
161*4882a593Smuzhiyun {
162*4882a593Smuzhiyun xas->xa_offset++;
163*4882a593Smuzhiyun xas_move_index(xas, xas->xa_offset);
164*4882a593Smuzhiyun }
165*4882a593Smuzhiyun
set_bounds(struct xa_state * xas)166*4882a593Smuzhiyun static void *set_bounds(struct xa_state *xas)
167*4882a593Smuzhiyun {
168*4882a593Smuzhiyun xas->xa_node = XAS_BOUNDS;
169*4882a593Smuzhiyun return NULL;
170*4882a593Smuzhiyun }
171*4882a593Smuzhiyun
172*4882a593Smuzhiyun /*
173*4882a593Smuzhiyun * Starts a walk. If the @xas is already valid, we assume that it's on
174*4882a593Smuzhiyun * the right path and just return where we've got to. If we're in an
175*4882a593Smuzhiyun * error state, return NULL. If the index is outside the current scope
176*4882a593Smuzhiyun * of the xarray, return NULL without changing @xas->xa_node. Otherwise
177*4882a593Smuzhiyun * set @xas->xa_node to NULL and return the current head of the array.
178*4882a593Smuzhiyun */
xas_start(struct xa_state * xas)179*4882a593Smuzhiyun static void *xas_start(struct xa_state *xas)
180*4882a593Smuzhiyun {
181*4882a593Smuzhiyun void *entry;
182*4882a593Smuzhiyun
183*4882a593Smuzhiyun if (xas_valid(xas))
184*4882a593Smuzhiyun return xas_reload(xas);
185*4882a593Smuzhiyun if (xas_error(xas))
186*4882a593Smuzhiyun return NULL;
187*4882a593Smuzhiyun
188*4882a593Smuzhiyun entry = xa_head(xas->xa);
189*4882a593Smuzhiyun if (!xa_is_node(entry)) {
190*4882a593Smuzhiyun if (xas->xa_index)
191*4882a593Smuzhiyun return set_bounds(xas);
192*4882a593Smuzhiyun } else {
193*4882a593Smuzhiyun if ((xas->xa_index >> xa_to_node(entry)->shift) > XA_CHUNK_MASK)
194*4882a593Smuzhiyun return set_bounds(xas);
195*4882a593Smuzhiyun }
196*4882a593Smuzhiyun
197*4882a593Smuzhiyun xas->xa_node = NULL;
198*4882a593Smuzhiyun return entry;
199*4882a593Smuzhiyun }
200*4882a593Smuzhiyun
xas_descend(struct xa_state * xas,struct xa_node * node)201*4882a593Smuzhiyun static void *xas_descend(struct xa_state *xas, struct xa_node *node)
202*4882a593Smuzhiyun {
203*4882a593Smuzhiyun unsigned int offset = get_offset(xas->xa_index, node);
204*4882a593Smuzhiyun void *entry = xa_entry(xas->xa, node, offset);
205*4882a593Smuzhiyun
206*4882a593Smuzhiyun xas->xa_node = node;
207*4882a593Smuzhiyun if (xa_is_sibling(entry)) {
208*4882a593Smuzhiyun offset = xa_to_sibling(entry);
209*4882a593Smuzhiyun entry = xa_entry(xas->xa, node, offset);
210*4882a593Smuzhiyun }
211*4882a593Smuzhiyun
212*4882a593Smuzhiyun xas->xa_offset = offset;
213*4882a593Smuzhiyun return entry;
214*4882a593Smuzhiyun }
215*4882a593Smuzhiyun
216*4882a593Smuzhiyun /**
217*4882a593Smuzhiyun * xas_load() - Load an entry from the XArray (advanced).
218*4882a593Smuzhiyun * @xas: XArray operation state.
219*4882a593Smuzhiyun *
220*4882a593Smuzhiyun * Usually walks the @xas to the appropriate state to load the entry
221*4882a593Smuzhiyun * stored at xa_index. However, it will do nothing and return %NULL if
222*4882a593Smuzhiyun * @xas is in an error state. xas_load() will never expand the tree.
223*4882a593Smuzhiyun *
224*4882a593Smuzhiyun * If the xa_state is set up to operate on a multi-index entry, xas_load()
225*4882a593Smuzhiyun * may return %NULL or an internal entry, even if there are entries
226*4882a593Smuzhiyun * present within the range specified by @xas.
227*4882a593Smuzhiyun *
228*4882a593Smuzhiyun * Context: Any context. The caller should hold the xa_lock or the RCU lock.
229*4882a593Smuzhiyun * Return: Usually an entry in the XArray, but see description for exceptions.
230*4882a593Smuzhiyun */
xas_load(struct xa_state * xas)231*4882a593Smuzhiyun void *xas_load(struct xa_state *xas)
232*4882a593Smuzhiyun {
233*4882a593Smuzhiyun void *entry = xas_start(xas);
234*4882a593Smuzhiyun
235*4882a593Smuzhiyun while (xa_is_node(entry)) {
236*4882a593Smuzhiyun struct xa_node *node = xa_to_node(entry);
237*4882a593Smuzhiyun
238*4882a593Smuzhiyun if (xas->xa_shift > node->shift)
239*4882a593Smuzhiyun break;
240*4882a593Smuzhiyun entry = xas_descend(xas, node);
241*4882a593Smuzhiyun if (node->shift == 0)
242*4882a593Smuzhiyun break;
243*4882a593Smuzhiyun }
244*4882a593Smuzhiyun return entry;
245*4882a593Smuzhiyun }
246*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(xas_load);
247*4882a593Smuzhiyun
248*4882a593Smuzhiyun /* Move the radix tree node cache here */
249*4882a593Smuzhiyun extern struct kmem_cache *radix_tree_node_cachep;
250*4882a593Smuzhiyun extern void radix_tree_node_rcu_free(struct rcu_head *head);
251*4882a593Smuzhiyun
252*4882a593Smuzhiyun #define XA_RCU_FREE ((struct xarray *)1)
253*4882a593Smuzhiyun
xa_node_free(struct xa_node * node)254*4882a593Smuzhiyun static void xa_node_free(struct xa_node *node)
255*4882a593Smuzhiyun {
256*4882a593Smuzhiyun XA_NODE_BUG_ON(node, !list_empty(&node->private_list));
257*4882a593Smuzhiyun node->array = XA_RCU_FREE;
258*4882a593Smuzhiyun call_rcu(&node->rcu_head, radix_tree_node_rcu_free);
259*4882a593Smuzhiyun }
260*4882a593Smuzhiyun
261*4882a593Smuzhiyun /*
262*4882a593Smuzhiyun * xas_destroy() - Free any resources allocated during the XArray operation.
263*4882a593Smuzhiyun * @xas: XArray operation state.
264*4882a593Smuzhiyun *
265*4882a593Smuzhiyun * This function is now internal-only.
266*4882a593Smuzhiyun */
xas_destroy(struct xa_state * xas)267*4882a593Smuzhiyun static void xas_destroy(struct xa_state *xas)
268*4882a593Smuzhiyun {
269*4882a593Smuzhiyun struct xa_node *next, *node = xas->xa_alloc;
270*4882a593Smuzhiyun
271*4882a593Smuzhiyun while (node) {
272*4882a593Smuzhiyun XA_NODE_BUG_ON(node, !list_empty(&node->private_list));
273*4882a593Smuzhiyun next = rcu_dereference_raw(node->parent);
274*4882a593Smuzhiyun radix_tree_node_rcu_free(&node->rcu_head);
275*4882a593Smuzhiyun xas->xa_alloc = node = next;
276*4882a593Smuzhiyun }
277*4882a593Smuzhiyun }
278*4882a593Smuzhiyun
279*4882a593Smuzhiyun /**
280*4882a593Smuzhiyun * xas_nomem() - Allocate memory if needed.
281*4882a593Smuzhiyun * @xas: XArray operation state.
282*4882a593Smuzhiyun * @gfp: Memory allocation flags.
283*4882a593Smuzhiyun *
284*4882a593Smuzhiyun * If we need to add new nodes to the XArray, we try to allocate memory
285*4882a593Smuzhiyun * with GFP_NOWAIT while holding the lock, which will usually succeed.
286*4882a593Smuzhiyun * If it fails, @xas is flagged as needing memory to continue. The caller
287*4882a593Smuzhiyun * should drop the lock and call xas_nomem(). If xas_nomem() succeeds,
288*4882a593Smuzhiyun * the caller should retry the operation.
289*4882a593Smuzhiyun *
290*4882a593Smuzhiyun * Forward progress is guaranteed as one node is allocated here and
291*4882a593Smuzhiyun * stored in the xa_state where it will be found by xas_alloc(). More
292*4882a593Smuzhiyun * nodes will likely be found in the slab allocator, but we do not tie
293*4882a593Smuzhiyun * them up here.
294*4882a593Smuzhiyun *
295*4882a593Smuzhiyun * Return: true if memory was needed, and was successfully allocated.
296*4882a593Smuzhiyun */
xas_nomem(struct xa_state * xas,gfp_t gfp)297*4882a593Smuzhiyun bool xas_nomem(struct xa_state *xas, gfp_t gfp)
298*4882a593Smuzhiyun {
299*4882a593Smuzhiyun if (xas->xa_node != XA_ERROR(-ENOMEM)) {
300*4882a593Smuzhiyun xas_destroy(xas);
301*4882a593Smuzhiyun return false;
302*4882a593Smuzhiyun }
303*4882a593Smuzhiyun if (xas->xa->xa_flags & XA_FLAGS_ACCOUNT)
304*4882a593Smuzhiyun gfp |= __GFP_ACCOUNT;
305*4882a593Smuzhiyun xas->xa_alloc = kmem_cache_alloc(radix_tree_node_cachep, gfp);
306*4882a593Smuzhiyun if (!xas->xa_alloc)
307*4882a593Smuzhiyun return false;
308*4882a593Smuzhiyun xas->xa_alloc->parent = NULL;
309*4882a593Smuzhiyun XA_NODE_BUG_ON(xas->xa_alloc, !list_empty(&xas->xa_alloc->private_list));
310*4882a593Smuzhiyun xas->xa_node = XAS_RESTART;
311*4882a593Smuzhiyun return true;
312*4882a593Smuzhiyun }
313*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(xas_nomem);
314*4882a593Smuzhiyun
315*4882a593Smuzhiyun /*
316*4882a593Smuzhiyun * __xas_nomem() - Drop locks and allocate memory if needed.
317*4882a593Smuzhiyun * @xas: XArray operation state.
318*4882a593Smuzhiyun * @gfp: Memory allocation flags.
319*4882a593Smuzhiyun *
320*4882a593Smuzhiyun * Internal variant of xas_nomem().
321*4882a593Smuzhiyun *
322*4882a593Smuzhiyun * Return: true if memory was needed, and was successfully allocated.
323*4882a593Smuzhiyun */
__xas_nomem(struct xa_state * xas,gfp_t gfp)324*4882a593Smuzhiyun static bool __xas_nomem(struct xa_state *xas, gfp_t gfp)
325*4882a593Smuzhiyun __must_hold(xas->xa->xa_lock)
326*4882a593Smuzhiyun {
327*4882a593Smuzhiyun unsigned int lock_type = xa_lock_type(xas->xa);
328*4882a593Smuzhiyun
329*4882a593Smuzhiyun if (xas->xa_node != XA_ERROR(-ENOMEM)) {
330*4882a593Smuzhiyun xas_destroy(xas);
331*4882a593Smuzhiyun return false;
332*4882a593Smuzhiyun }
333*4882a593Smuzhiyun if (xas->xa->xa_flags & XA_FLAGS_ACCOUNT)
334*4882a593Smuzhiyun gfp |= __GFP_ACCOUNT;
335*4882a593Smuzhiyun if (gfpflags_allow_blocking(gfp)) {
336*4882a593Smuzhiyun xas_unlock_type(xas, lock_type);
337*4882a593Smuzhiyun xas->xa_alloc = kmem_cache_alloc(radix_tree_node_cachep, gfp);
338*4882a593Smuzhiyun xas_lock_type(xas, lock_type);
339*4882a593Smuzhiyun } else {
340*4882a593Smuzhiyun xas->xa_alloc = kmem_cache_alloc(radix_tree_node_cachep, gfp);
341*4882a593Smuzhiyun }
342*4882a593Smuzhiyun if (!xas->xa_alloc)
343*4882a593Smuzhiyun return false;
344*4882a593Smuzhiyun xas->xa_alloc->parent = NULL;
345*4882a593Smuzhiyun XA_NODE_BUG_ON(xas->xa_alloc, !list_empty(&xas->xa_alloc->private_list));
346*4882a593Smuzhiyun xas->xa_node = XAS_RESTART;
347*4882a593Smuzhiyun return true;
348*4882a593Smuzhiyun }
349*4882a593Smuzhiyun
xas_update(struct xa_state * xas,struct xa_node * node)350*4882a593Smuzhiyun static void xas_update(struct xa_state *xas, struct xa_node *node)
351*4882a593Smuzhiyun {
352*4882a593Smuzhiyun if (xas->xa_update)
353*4882a593Smuzhiyun xas->xa_update(node);
354*4882a593Smuzhiyun else
355*4882a593Smuzhiyun XA_NODE_BUG_ON(node, !list_empty(&node->private_list));
356*4882a593Smuzhiyun }
357*4882a593Smuzhiyun
xas_alloc(struct xa_state * xas,unsigned int shift)358*4882a593Smuzhiyun static void *xas_alloc(struct xa_state *xas, unsigned int shift)
359*4882a593Smuzhiyun {
360*4882a593Smuzhiyun struct xa_node *parent = xas->xa_node;
361*4882a593Smuzhiyun struct xa_node *node = xas->xa_alloc;
362*4882a593Smuzhiyun
363*4882a593Smuzhiyun if (xas_invalid(xas))
364*4882a593Smuzhiyun return NULL;
365*4882a593Smuzhiyun
366*4882a593Smuzhiyun if (node) {
367*4882a593Smuzhiyun xas->xa_alloc = NULL;
368*4882a593Smuzhiyun } else {
369*4882a593Smuzhiyun gfp_t gfp = GFP_NOWAIT | __GFP_NOWARN;
370*4882a593Smuzhiyun
371*4882a593Smuzhiyun if (xas->xa->xa_flags & XA_FLAGS_ACCOUNT)
372*4882a593Smuzhiyun gfp |= __GFP_ACCOUNT;
373*4882a593Smuzhiyun
374*4882a593Smuzhiyun node = kmem_cache_alloc(radix_tree_node_cachep, gfp);
375*4882a593Smuzhiyun if (!node) {
376*4882a593Smuzhiyun xas_set_err(xas, -ENOMEM);
377*4882a593Smuzhiyun return NULL;
378*4882a593Smuzhiyun }
379*4882a593Smuzhiyun }
380*4882a593Smuzhiyun
381*4882a593Smuzhiyun if (parent) {
382*4882a593Smuzhiyun node->offset = xas->xa_offset;
383*4882a593Smuzhiyun parent->count++;
384*4882a593Smuzhiyun XA_NODE_BUG_ON(node, parent->count > XA_CHUNK_SIZE);
385*4882a593Smuzhiyun xas_update(xas, parent);
386*4882a593Smuzhiyun }
387*4882a593Smuzhiyun XA_NODE_BUG_ON(node, shift > BITS_PER_LONG);
388*4882a593Smuzhiyun XA_NODE_BUG_ON(node, !list_empty(&node->private_list));
389*4882a593Smuzhiyun node->shift = shift;
390*4882a593Smuzhiyun node->count = 0;
391*4882a593Smuzhiyun node->nr_values = 0;
392*4882a593Smuzhiyun RCU_INIT_POINTER(node->parent, xas->xa_node);
393*4882a593Smuzhiyun node->array = xas->xa;
394*4882a593Smuzhiyun
395*4882a593Smuzhiyun return node;
396*4882a593Smuzhiyun }
397*4882a593Smuzhiyun
398*4882a593Smuzhiyun #ifdef CONFIG_XARRAY_MULTI
399*4882a593Smuzhiyun /* Returns the number of indices covered by a given xa_state */
xas_size(const struct xa_state * xas)400*4882a593Smuzhiyun static unsigned long xas_size(const struct xa_state *xas)
401*4882a593Smuzhiyun {
402*4882a593Smuzhiyun return (xas->xa_sibs + 1UL) << xas->xa_shift;
403*4882a593Smuzhiyun }
404*4882a593Smuzhiyun #endif
405*4882a593Smuzhiyun
406*4882a593Smuzhiyun /*
407*4882a593Smuzhiyun * Use this to calculate the maximum index that will need to be created
408*4882a593Smuzhiyun * in order to add the entry described by @xas. Because we cannot store a
409*4882a593Smuzhiyun * multi-index entry at index 0, the calculation is a little more complex
410*4882a593Smuzhiyun * than you might expect.
411*4882a593Smuzhiyun */
xas_max(struct xa_state * xas)412*4882a593Smuzhiyun static unsigned long xas_max(struct xa_state *xas)
413*4882a593Smuzhiyun {
414*4882a593Smuzhiyun unsigned long max = xas->xa_index;
415*4882a593Smuzhiyun
416*4882a593Smuzhiyun #ifdef CONFIG_XARRAY_MULTI
417*4882a593Smuzhiyun if (xas->xa_shift || xas->xa_sibs) {
418*4882a593Smuzhiyun unsigned long mask = xas_size(xas) - 1;
419*4882a593Smuzhiyun max |= mask;
420*4882a593Smuzhiyun if (mask == max)
421*4882a593Smuzhiyun max++;
422*4882a593Smuzhiyun }
423*4882a593Smuzhiyun #endif
424*4882a593Smuzhiyun
425*4882a593Smuzhiyun return max;
426*4882a593Smuzhiyun }
427*4882a593Smuzhiyun
428*4882a593Smuzhiyun /* The maximum index that can be contained in the array without expanding it */
max_index(void * entry)429*4882a593Smuzhiyun static unsigned long max_index(void *entry)
430*4882a593Smuzhiyun {
431*4882a593Smuzhiyun if (!xa_is_node(entry))
432*4882a593Smuzhiyun return 0;
433*4882a593Smuzhiyun return (XA_CHUNK_SIZE << xa_to_node(entry)->shift) - 1;
434*4882a593Smuzhiyun }
435*4882a593Smuzhiyun
xas_shrink(struct xa_state * xas)436*4882a593Smuzhiyun static void xas_shrink(struct xa_state *xas)
437*4882a593Smuzhiyun {
438*4882a593Smuzhiyun struct xarray *xa = xas->xa;
439*4882a593Smuzhiyun struct xa_node *node = xas->xa_node;
440*4882a593Smuzhiyun
441*4882a593Smuzhiyun for (;;) {
442*4882a593Smuzhiyun void *entry;
443*4882a593Smuzhiyun
444*4882a593Smuzhiyun XA_NODE_BUG_ON(node, node->count > XA_CHUNK_SIZE);
445*4882a593Smuzhiyun if (node->count != 1)
446*4882a593Smuzhiyun break;
447*4882a593Smuzhiyun entry = xa_entry_locked(xa, node, 0);
448*4882a593Smuzhiyun if (!entry)
449*4882a593Smuzhiyun break;
450*4882a593Smuzhiyun if (!xa_is_node(entry) && node->shift)
451*4882a593Smuzhiyun break;
452*4882a593Smuzhiyun if (xa_is_zero(entry) && xa_zero_busy(xa))
453*4882a593Smuzhiyun entry = NULL;
454*4882a593Smuzhiyun xas->xa_node = XAS_BOUNDS;
455*4882a593Smuzhiyun
456*4882a593Smuzhiyun RCU_INIT_POINTER(xa->xa_head, entry);
457*4882a593Smuzhiyun if (xa_track_free(xa) && !node_get_mark(node, 0, XA_FREE_MARK))
458*4882a593Smuzhiyun xa_mark_clear(xa, XA_FREE_MARK);
459*4882a593Smuzhiyun
460*4882a593Smuzhiyun node->count = 0;
461*4882a593Smuzhiyun node->nr_values = 0;
462*4882a593Smuzhiyun if (!xa_is_node(entry))
463*4882a593Smuzhiyun RCU_INIT_POINTER(node->slots[0], XA_RETRY_ENTRY);
464*4882a593Smuzhiyun xas_update(xas, node);
465*4882a593Smuzhiyun xa_node_free(node);
466*4882a593Smuzhiyun if (!xa_is_node(entry))
467*4882a593Smuzhiyun break;
468*4882a593Smuzhiyun node = xa_to_node(entry);
469*4882a593Smuzhiyun node->parent = NULL;
470*4882a593Smuzhiyun }
471*4882a593Smuzhiyun }
472*4882a593Smuzhiyun
473*4882a593Smuzhiyun /*
474*4882a593Smuzhiyun * xas_delete_node() - Attempt to delete an xa_node
475*4882a593Smuzhiyun * @xas: Array operation state.
476*4882a593Smuzhiyun *
477*4882a593Smuzhiyun * Attempts to delete the @xas->xa_node. This will fail if xa->node has
478*4882a593Smuzhiyun * a non-zero reference count.
479*4882a593Smuzhiyun */
xas_delete_node(struct xa_state * xas)480*4882a593Smuzhiyun static void xas_delete_node(struct xa_state *xas)
481*4882a593Smuzhiyun {
482*4882a593Smuzhiyun struct xa_node *node = xas->xa_node;
483*4882a593Smuzhiyun
484*4882a593Smuzhiyun for (;;) {
485*4882a593Smuzhiyun struct xa_node *parent;
486*4882a593Smuzhiyun
487*4882a593Smuzhiyun XA_NODE_BUG_ON(node, node->count > XA_CHUNK_SIZE);
488*4882a593Smuzhiyun if (node->count)
489*4882a593Smuzhiyun break;
490*4882a593Smuzhiyun
491*4882a593Smuzhiyun parent = xa_parent_locked(xas->xa, node);
492*4882a593Smuzhiyun xas->xa_node = parent;
493*4882a593Smuzhiyun xas->xa_offset = node->offset;
494*4882a593Smuzhiyun xa_node_free(node);
495*4882a593Smuzhiyun
496*4882a593Smuzhiyun if (!parent) {
497*4882a593Smuzhiyun xas->xa->xa_head = NULL;
498*4882a593Smuzhiyun xas->xa_node = XAS_BOUNDS;
499*4882a593Smuzhiyun return;
500*4882a593Smuzhiyun }
501*4882a593Smuzhiyun
502*4882a593Smuzhiyun parent->slots[xas->xa_offset] = NULL;
503*4882a593Smuzhiyun parent->count--;
504*4882a593Smuzhiyun XA_NODE_BUG_ON(parent, parent->count > XA_CHUNK_SIZE);
505*4882a593Smuzhiyun node = parent;
506*4882a593Smuzhiyun xas_update(xas, node);
507*4882a593Smuzhiyun }
508*4882a593Smuzhiyun
509*4882a593Smuzhiyun if (!node->parent)
510*4882a593Smuzhiyun xas_shrink(xas);
511*4882a593Smuzhiyun }
512*4882a593Smuzhiyun
513*4882a593Smuzhiyun /**
514*4882a593Smuzhiyun * xas_free_nodes() - Free this node and all nodes that it references
515*4882a593Smuzhiyun * @xas: Array operation state.
516*4882a593Smuzhiyun * @top: Node to free
517*4882a593Smuzhiyun *
518*4882a593Smuzhiyun * This node has been removed from the tree. We must now free it and all
519*4882a593Smuzhiyun * of its subnodes. There may be RCU walkers with references into the tree,
520*4882a593Smuzhiyun * so we must replace all entries with retry markers.
521*4882a593Smuzhiyun */
xas_free_nodes(struct xa_state * xas,struct xa_node * top)522*4882a593Smuzhiyun static void xas_free_nodes(struct xa_state *xas, struct xa_node *top)
523*4882a593Smuzhiyun {
524*4882a593Smuzhiyun unsigned int offset = 0;
525*4882a593Smuzhiyun struct xa_node *node = top;
526*4882a593Smuzhiyun
527*4882a593Smuzhiyun for (;;) {
528*4882a593Smuzhiyun void *entry = xa_entry_locked(xas->xa, node, offset);
529*4882a593Smuzhiyun
530*4882a593Smuzhiyun if (node->shift && xa_is_node(entry)) {
531*4882a593Smuzhiyun node = xa_to_node(entry);
532*4882a593Smuzhiyun offset = 0;
533*4882a593Smuzhiyun continue;
534*4882a593Smuzhiyun }
535*4882a593Smuzhiyun if (entry)
536*4882a593Smuzhiyun RCU_INIT_POINTER(node->slots[offset], XA_RETRY_ENTRY);
537*4882a593Smuzhiyun offset++;
538*4882a593Smuzhiyun while (offset == XA_CHUNK_SIZE) {
539*4882a593Smuzhiyun struct xa_node *parent;
540*4882a593Smuzhiyun
541*4882a593Smuzhiyun parent = xa_parent_locked(xas->xa, node);
542*4882a593Smuzhiyun offset = node->offset + 1;
543*4882a593Smuzhiyun node->count = 0;
544*4882a593Smuzhiyun node->nr_values = 0;
545*4882a593Smuzhiyun xas_update(xas, node);
546*4882a593Smuzhiyun xa_node_free(node);
547*4882a593Smuzhiyun if (node == top)
548*4882a593Smuzhiyun return;
549*4882a593Smuzhiyun node = parent;
550*4882a593Smuzhiyun }
551*4882a593Smuzhiyun }
552*4882a593Smuzhiyun }
553*4882a593Smuzhiyun
554*4882a593Smuzhiyun /*
555*4882a593Smuzhiyun * xas_expand adds nodes to the head of the tree until it has reached
556*4882a593Smuzhiyun * sufficient height to be able to contain @xas->xa_index
557*4882a593Smuzhiyun */
xas_expand(struct xa_state * xas,void * head)558*4882a593Smuzhiyun static int xas_expand(struct xa_state *xas, void *head)
559*4882a593Smuzhiyun {
560*4882a593Smuzhiyun struct xarray *xa = xas->xa;
561*4882a593Smuzhiyun struct xa_node *node = NULL;
562*4882a593Smuzhiyun unsigned int shift = 0;
563*4882a593Smuzhiyun unsigned long max = xas_max(xas);
564*4882a593Smuzhiyun
565*4882a593Smuzhiyun if (!head) {
566*4882a593Smuzhiyun if (max == 0)
567*4882a593Smuzhiyun return 0;
568*4882a593Smuzhiyun while ((max >> shift) >= XA_CHUNK_SIZE)
569*4882a593Smuzhiyun shift += XA_CHUNK_SHIFT;
570*4882a593Smuzhiyun return shift + XA_CHUNK_SHIFT;
571*4882a593Smuzhiyun } else if (xa_is_node(head)) {
572*4882a593Smuzhiyun node = xa_to_node(head);
573*4882a593Smuzhiyun shift = node->shift + XA_CHUNK_SHIFT;
574*4882a593Smuzhiyun }
575*4882a593Smuzhiyun xas->xa_node = NULL;
576*4882a593Smuzhiyun
577*4882a593Smuzhiyun while (max > max_index(head)) {
578*4882a593Smuzhiyun xa_mark_t mark = 0;
579*4882a593Smuzhiyun
580*4882a593Smuzhiyun XA_NODE_BUG_ON(node, shift > BITS_PER_LONG);
581*4882a593Smuzhiyun node = xas_alloc(xas, shift);
582*4882a593Smuzhiyun if (!node)
583*4882a593Smuzhiyun return -ENOMEM;
584*4882a593Smuzhiyun
585*4882a593Smuzhiyun node->count = 1;
586*4882a593Smuzhiyun if (xa_is_value(head))
587*4882a593Smuzhiyun node->nr_values = 1;
588*4882a593Smuzhiyun RCU_INIT_POINTER(node->slots[0], head);
589*4882a593Smuzhiyun
590*4882a593Smuzhiyun /* Propagate the aggregated mark info to the new child */
591*4882a593Smuzhiyun for (;;) {
592*4882a593Smuzhiyun if (xa_track_free(xa) && mark == XA_FREE_MARK) {
593*4882a593Smuzhiyun node_mark_all(node, XA_FREE_MARK);
594*4882a593Smuzhiyun if (!xa_marked(xa, XA_FREE_MARK)) {
595*4882a593Smuzhiyun node_clear_mark(node, 0, XA_FREE_MARK);
596*4882a593Smuzhiyun xa_mark_set(xa, XA_FREE_MARK);
597*4882a593Smuzhiyun }
598*4882a593Smuzhiyun } else if (xa_marked(xa, mark)) {
599*4882a593Smuzhiyun node_set_mark(node, 0, mark);
600*4882a593Smuzhiyun }
601*4882a593Smuzhiyun if (mark == XA_MARK_MAX)
602*4882a593Smuzhiyun break;
603*4882a593Smuzhiyun mark_inc(mark);
604*4882a593Smuzhiyun }
605*4882a593Smuzhiyun
606*4882a593Smuzhiyun /*
607*4882a593Smuzhiyun * Now that the new node is fully initialised, we can add
608*4882a593Smuzhiyun * it to the tree
609*4882a593Smuzhiyun */
610*4882a593Smuzhiyun if (xa_is_node(head)) {
611*4882a593Smuzhiyun xa_to_node(head)->offset = 0;
612*4882a593Smuzhiyun rcu_assign_pointer(xa_to_node(head)->parent, node);
613*4882a593Smuzhiyun }
614*4882a593Smuzhiyun head = xa_mk_node(node);
615*4882a593Smuzhiyun rcu_assign_pointer(xa->xa_head, head);
616*4882a593Smuzhiyun xas_update(xas, node);
617*4882a593Smuzhiyun
618*4882a593Smuzhiyun shift += XA_CHUNK_SHIFT;
619*4882a593Smuzhiyun }
620*4882a593Smuzhiyun
621*4882a593Smuzhiyun xas->xa_node = node;
622*4882a593Smuzhiyun return shift;
623*4882a593Smuzhiyun }
624*4882a593Smuzhiyun
625*4882a593Smuzhiyun /*
626*4882a593Smuzhiyun * xas_create() - Create a slot to store an entry in.
627*4882a593Smuzhiyun * @xas: XArray operation state.
628*4882a593Smuzhiyun * @allow_root: %true if we can store the entry in the root directly
629*4882a593Smuzhiyun *
630*4882a593Smuzhiyun * Most users will not need to call this function directly, as it is called
631*4882a593Smuzhiyun * by xas_store(). It is useful for doing conditional store operations
632*4882a593Smuzhiyun * (see the xa_cmpxchg() implementation for an example).
633*4882a593Smuzhiyun *
634*4882a593Smuzhiyun * Return: If the slot already existed, returns the contents of this slot.
635*4882a593Smuzhiyun * If the slot was newly created, returns %NULL. If it failed to create the
636*4882a593Smuzhiyun * slot, returns %NULL and indicates the error in @xas.
637*4882a593Smuzhiyun */
xas_create(struct xa_state * xas,bool allow_root)638*4882a593Smuzhiyun static void *xas_create(struct xa_state *xas, bool allow_root)
639*4882a593Smuzhiyun {
640*4882a593Smuzhiyun struct xarray *xa = xas->xa;
641*4882a593Smuzhiyun void *entry;
642*4882a593Smuzhiyun void __rcu **slot;
643*4882a593Smuzhiyun struct xa_node *node = xas->xa_node;
644*4882a593Smuzhiyun int shift;
645*4882a593Smuzhiyun unsigned int order = xas->xa_shift;
646*4882a593Smuzhiyun
647*4882a593Smuzhiyun if (xas_top(node)) {
648*4882a593Smuzhiyun entry = xa_head_locked(xa);
649*4882a593Smuzhiyun xas->xa_node = NULL;
650*4882a593Smuzhiyun if (!entry && xa_zero_busy(xa))
651*4882a593Smuzhiyun entry = XA_ZERO_ENTRY;
652*4882a593Smuzhiyun shift = xas_expand(xas, entry);
653*4882a593Smuzhiyun if (shift < 0)
654*4882a593Smuzhiyun return NULL;
655*4882a593Smuzhiyun if (!shift && !allow_root)
656*4882a593Smuzhiyun shift = XA_CHUNK_SHIFT;
657*4882a593Smuzhiyun entry = xa_head_locked(xa);
658*4882a593Smuzhiyun slot = &xa->xa_head;
659*4882a593Smuzhiyun } else if (xas_error(xas)) {
660*4882a593Smuzhiyun return NULL;
661*4882a593Smuzhiyun } else if (node) {
662*4882a593Smuzhiyun unsigned int offset = xas->xa_offset;
663*4882a593Smuzhiyun
664*4882a593Smuzhiyun shift = node->shift;
665*4882a593Smuzhiyun entry = xa_entry_locked(xa, node, offset);
666*4882a593Smuzhiyun slot = &node->slots[offset];
667*4882a593Smuzhiyun } else {
668*4882a593Smuzhiyun shift = 0;
669*4882a593Smuzhiyun entry = xa_head_locked(xa);
670*4882a593Smuzhiyun slot = &xa->xa_head;
671*4882a593Smuzhiyun }
672*4882a593Smuzhiyun
673*4882a593Smuzhiyun while (shift > order) {
674*4882a593Smuzhiyun shift -= XA_CHUNK_SHIFT;
675*4882a593Smuzhiyun if (!entry) {
676*4882a593Smuzhiyun node = xas_alloc(xas, shift);
677*4882a593Smuzhiyun if (!node)
678*4882a593Smuzhiyun break;
679*4882a593Smuzhiyun if (xa_track_free(xa))
680*4882a593Smuzhiyun node_mark_all(node, XA_FREE_MARK);
681*4882a593Smuzhiyun rcu_assign_pointer(*slot, xa_mk_node(node));
682*4882a593Smuzhiyun } else if (xa_is_node(entry)) {
683*4882a593Smuzhiyun node = xa_to_node(entry);
684*4882a593Smuzhiyun } else {
685*4882a593Smuzhiyun break;
686*4882a593Smuzhiyun }
687*4882a593Smuzhiyun entry = xas_descend(xas, node);
688*4882a593Smuzhiyun slot = &node->slots[xas->xa_offset];
689*4882a593Smuzhiyun }
690*4882a593Smuzhiyun
691*4882a593Smuzhiyun return entry;
692*4882a593Smuzhiyun }
693*4882a593Smuzhiyun
694*4882a593Smuzhiyun /**
695*4882a593Smuzhiyun * xas_create_range() - Ensure that stores to this range will succeed
696*4882a593Smuzhiyun * @xas: XArray operation state.
697*4882a593Smuzhiyun *
698*4882a593Smuzhiyun * Creates all of the slots in the range covered by @xas. Sets @xas to
699*4882a593Smuzhiyun * create single-index entries and positions it at the beginning of the
700*4882a593Smuzhiyun * range. This is for the benefit of users which have not yet been
701*4882a593Smuzhiyun * converted to use multi-index entries.
702*4882a593Smuzhiyun */
xas_create_range(struct xa_state * xas)703*4882a593Smuzhiyun void xas_create_range(struct xa_state *xas)
704*4882a593Smuzhiyun {
705*4882a593Smuzhiyun unsigned long index = xas->xa_index;
706*4882a593Smuzhiyun unsigned char shift = xas->xa_shift;
707*4882a593Smuzhiyun unsigned char sibs = xas->xa_sibs;
708*4882a593Smuzhiyun
709*4882a593Smuzhiyun xas->xa_index |= ((sibs + 1UL) << shift) - 1;
710*4882a593Smuzhiyun if (xas_is_node(xas) && xas->xa_node->shift == xas->xa_shift)
711*4882a593Smuzhiyun xas->xa_offset |= sibs;
712*4882a593Smuzhiyun xas->xa_shift = 0;
713*4882a593Smuzhiyun xas->xa_sibs = 0;
714*4882a593Smuzhiyun
715*4882a593Smuzhiyun for (;;) {
716*4882a593Smuzhiyun xas_create(xas, true);
717*4882a593Smuzhiyun if (xas_error(xas))
718*4882a593Smuzhiyun goto restore;
719*4882a593Smuzhiyun if (xas->xa_index <= (index | XA_CHUNK_MASK))
720*4882a593Smuzhiyun goto success;
721*4882a593Smuzhiyun xas->xa_index -= XA_CHUNK_SIZE;
722*4882a593Smuzhiyun
723*4882a593Smuzhiyun for (;;) {
724*4882a593Smuzhiyun struct xa_node *node = xas->xa_node;
725*4882a593Smuzhiyun if (node->shift >= shift)
726*4882a593Smuzhiyun break;
727*4882a593Smuzhiyun xas->xa_node = xa_parent_locked(xas->xa, node);
728*4882a593Smuzhiyun xas->xa_offset = node->offset - 1;
729*4882a593Smuzhiyun if (node->offset != 0)
730*4882a593Smuzhiyun break;
731*4882a593Smuzhiyun }
732*4882a593Smuzhiyun }
733*4882a593Smuzhiyun
734*4882a593Smuzhiyun restore:
735*4882a593Smuzhiyun xas->xa_shift = shift;
736*4882a593Smuzhiyun xas->xa_sibs = sibs;
737*4882a593Smuzhiyun xas->xa_index = index;
738*4882a593Smuzhiyun return;
739*4882a593Smuzhiyun success:
740*4882a593Smuzhiyun xas->xa_index = index;
741*4882a593Smuzhiyun if (xas->xa_node)
742*4882a593Smuzhiyun xas_set_offset(xas);
743*4882a593Smuzhiyun }
744*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(xas_create_range);
745*4882a593Smuzhiyun
update_node(struct xa_state * xas,struct xa_node * node,int count,int values)746*4882a593Smuzhiyun static void update_node(struct xa_state *xas, struct xa_node *node,
747*4882a593Smuzhiyun int count, int values)
748*4882a593Smuzhiyun {
749*4882a593Smuzhiyun if (!node || (!count && !values))
750*4882a593Smuzhiyun return;
751*4882a593Smuzhiyun
752*4882a593Smuzhiyun node->count += count;
753*4882a593Smuzhiyun node->nr_values += values;
754*4882a593Smuzhiyun XA_NODE_BUG_ON(node, node->count > XA_CHUNK_SIZE);
755*4882a593Smuzhiyun XA_NODE_BUG_ON(node, node->nr_values > XA_CHUNK_SIZE);
756*4882a593Smuzhiyun xas_update(xas, node);
757*4882a593Smuzhiyun if (count < 0)
758*4882a593Smuzhiyun xas_delete_node(xas);
759*4882a593Smuzhiyun }
760*4882a593Smuzhiyun
761*4882a593Smuzhiyun /**
762*4882a593Smuzhiyun * xas_store() - Store this entry in the XArray.
763*4882a593Smuzhiyun * @xas: XArray operation state.
764*4882a593Smuzhiyun * @entry: New entry.
765*4882a593Smuzhiyun *
766*4882a593Smuzhiyun * If @xas is operating on a multi-index entry, the entry returned by this
767*4882a593Smuzhiyun * function is essentially meaningless (it may be an internal entry or it
768*4882a593Smuzhiyun * may be %NULL, even if there are non-NULL entries at some of the indices
769*4882a593Smuzhiyun * covered by the range). This is not a problem for any current users,
770*4882a593Smuzhiyun * and can be changed if needed.
771*4882a593Smuzhiyun *
772*4882a593Smuzhiyun * Return: The old entry at this index.
773*4882a593Smuzhiyun */
xas_store(struct xa_state * xas,void * entry)774*4882a593Smuzhiyun void *xas_store(struct xa_state *xas, void *entry)
775*4882a593Smuzhiyun {
776*4882a593Smuzhiyun struct xa_node *node;
777*4882a593Smuzhiyun void __rcu **slot = &xas->xa->xa_head;
778*4882a593Smuzhiyun unsigned int offset, max;
779*4882a593Smuzhiyun int count = 0;
780*4882a593Smuzhiyun int values = 0;
781*4882a593Smuzhiyun void *first, *next;
782*4882a593Smuzhiyun bool value = xa_is_value(entry);
783*4882a593Smuzhiyun
784*4882a593Smuzhiyun if (entry) {
785*4882a593Smuzhiyun bool allow_root = !xa_is_node(entry) && !xa_is_zero(entry);
786*4882a593Smuzhiyun first = xas_create(xas, allow_root);
787*4882a593Smuzhiyun } else {
788*4882a593Smuzhiyun first = xas_load(xas);
789*4882a593Smuzhiyun }
790*4882a593Smuzhiyun
791*4882a593Smuzhiyun if (xas_invalid(xas))
792*4882a593Smuzhiyun return first;
793*4882a593Smuzhiyun node = xas->xa_node;
794*4882a593Smuzhiyun if (node && (xas->xa_shift < node->shift))
795*4882a593Smuzhiyun xas->xa_sibs = 0;
796*4882a593Smuzhiyun if ((first == entry) && !xas->xa_sibs)
797*4882a593Smuzhiyun return first;
798*4882a593Smuzhiyun
799*4882a593Smuzhiyun next = first;
800*4882a593Smuzhiyun offset = xas->xa_offset;
801*4882a593Smuzhiyun max = xas->xa_offset + xas->xa_sibs;
802*4882a593Smuzhiyun if (node) {
803*4882a593Smuzhiyun slot = &node->slots[offset];
804*4882a593Smuzhiyun if (xas->xa_sibs)
805*4882a593Smuzhiyun xas_squash_marks(xas);
806*4882a593Smuzhiyun }
807*4882a593Smuzhiyun if (!entry)
808*4882a593Smuzhiyun xas_init_marks(xas);
809*4882a593Smuzhiyun
810*4882a593Smuzhiyun for (;;) {
811*4882a593Smuzhiyun /*
812*4882a593Smuzhiyun * Must clear the marks before setting the entry to NULL,
813*4882a593Smuzhiyun * otherwise xas_for_each_marked may find a NULL entry and
814*4882a593Smuzhiyun * stop early. rcu_assign_pointer contains a release barrier
815*4882a593Smuzhiyun * so the mark clearing will appear to happen before the
816*4882a593Smuzhiyun * entry is set to NULL.
817*4882a593Smuzhiyun */
818*4882a593Smuzhiyun rcu_assign_pointer(*slot, entry);
819*4882a593Smuzhiyun if (xa_is_node(next) && (!node || node->shift))
820*4882a593Smuzhiyun xas_free_nodes(xas, xa_to_node(next));
821*4882a593Smuzhiyun if (!node)
822*4882a593Smuzhiyun break;
823*4882a593Smuzhiyun count += !next - !entry;
824*4882a593Smuzhiyun values += !xa_is_value(first) - !value;
825*4882a593Smuzhiyun if (entry) {
826*4882a593Smuzhiyun if (offset == max)
827*4882a593Smuzhiyun break;
828*4882a593Smuzhiyun if (!xa_is_sibling(entry))
829*4882a593Smuzhiyun entry = xa_mk_sibling(xas->xa_offset);
830*4882a593Smuzhiyun } else {
831*4882a593Smuzhiyun if (offset == XA_CHUNK_MASK)
832*4882a593Smuzhiyun break;
833*4882a593Smuzhiyun }
834*4882a593Smuzhiyun next = xa_entry_locked(xas->xa, node, ++offset);
835*4882a593Smuzhiyun if (!xa_is_sibling(next)) {
836*4882a593Smuzhiyun if (!entry && (offset > max))
837*4882a593Smuzhiyun break;
838*4882a593Smuzhiyun first = next;
839*4882a593Smuzhiyun }
840*4882a593Smuzhiyun slot++;
841*4882a593Smuzhiyun }
842*4882a593Smuzhiyun
843*4882a593Smuzhiyun update_node(xas, node, count, values);
844*4882a593Smuzhiyun return first;
845*4882a593Smuzhiyun }
846*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(xas_store);
847*4882a593Smuzhiyun
848*4882a593Smuzhiyun /**
849*4882a593Smuzhiyun * xas_get_mark() - Returns the state of this mark.
850*4882a593Smuzhiyun * @xas: XArray operation state.
851*4882a593Smuzhiyun * @mark: Mark number.
852*4882a593Smuzhiyun *
853*4882a593Smuzhiyun * Return: true if the mark is set, false if the mark is clear or @xas
854*4882a593Smuzhiyun * is in an error state.
855*4882a593Smuzhiyun */
xas_get_mark(const struct xa_state * xas,xa_mark_t mark)856*4882a593Smuzhiyun bool xas_get_mark(const struct xa_state *xas, xa_mark_t mark)
857*4882a593Smuzhiyun {
858*4882a593Smuzhiyun if (xas_invalid(xas))
859*4882a593Smuzhiyun return false;
860*4882a593Smuzhiyun if (!xas->xa_node)
861*4882a593Smuzhiyun return xa_marked(xas->xa, mark);
862*4882a593Smuzhiyun return node_get_mark(xas->xa_node, xas->xa_offset, mark);
863*4882a593Smuzhiyun }
864*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(xas_get_mark);
865*4882a593Smuzhiyun
866*4882a593Smuzhiyun /**
867*4882a593Smuzhiyun * xas_set_mark() - Sets the mark on this entry and its parents.
868*4882a593Smuzhiyun * @xas: XArray operation state.
869*4882a593Smuzhiyun * @mark: Mark number.
870*4882a593Smuzhiyun *
871*4882a593Smuzhiyun * Sets the specified mark on this entry, and walks up the tree setting it
872*4882a593Smuzhiyun * on all the ancestor entries. Does nothing if @xas has not been walked to
873*4882a593Smuzhiyun * an entry, or is in an error state.
874*4882a593Smuzhiyun */
xas_set_mark(const struct xa_state * xas,xa_mark_t mark)875*4882a593Smuzhiyun void xas_set_mark(const struct xa_state *xas, xa_mark_t mark)
876*4882a593Smuzhiyun {
877*4882a593Smuzhiyun struct xa_node *node = xas->xa_node;
878*4882a593Smuzhiyun unsigned int offset = xas->xa_offset;
879*4882a593Smuzhiyun
880*4882a593Smuzhiyun if (xas_invalid(xas))
881*4882a593Smuzhiyun return;
882*4882a593Smuzhiyun
883*4882a593Smuzhiyun while (node) {
884*4882a593Smuzhiyun if (node_set_mark(node, offset, mark))
885*4882a593Smuzhiyun return;
886*4882a593Smuzhiyun offset = node->offset;
887*4882a593Smuzhiyun node = xa_parent_locked(xas->xa, node);
888*4882a593Smuzhiyun }
889*4882a593Smuzhiyun
890*4882a593Smuzhiyun if (!xa_marked(xas->xa, mark))
891*4882a593Smuzhiyun xa_mark_set(xas->xa, mark);
892*4882a593Smuzhiyun }
893*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(xas_set_mark);
894*4882a593Smuzhiyun
895*4882a593Smuzhiyun /**
896*4882a593Smuzhiyun * xas_clear_mark() - Clears the mark on this entry and its parents.
897*4882a593Smuzhiyun * @xas: XArray operation state.
898*4882a593Smuzhiyun * @mark: Mark number.
899*4882a593Smuzhiyun *
900*4882a593Smuzhiyun * Clears the specified mark on this entry, and walks back to the head
901*4882a593Smuzhiyun * attempting to clear it on all the ancestor entries. Does nothing if
902*4882a593Smuzhiyun * @xas has not been walked to an entry, or is in an error state.
903*4882a593Smuzhiyun */
xas_clear_mark(const struct xa_state * xas,xa_mark_t mark)904*4882a593Smuzhiyun void xas_clear_mark(const struct xa_state *xas, xa_mark_t mark)
905*4882a593Smuzhiyun {
906*4882a593Smuzhiyun struct xa_node *node = xas->xa_node;
907*4882a593Smuzhiyun unsigned int offset = xas->xa_offset;
908*4882a593Smuzhiyun
909*4882a593Smuzhiyun if (xas_invalid(xas))
910*4882a593Smuzhiyun return;
911*4882a593Smuzhiyun
912*4882a593Smuzhiyun while (node) {
913*4882a593Smuzhiyun if (!node_clear_mark(node, offset, mark))
914*4882a593Smuzhiyun return;
915*4882a593Smuzhiyun if (node_any_mark(node, mark))
916*4882a593Smuzhiyun return;
917*4882a593Smuzhiyun
918*4882a593Smuzhiyun offset = node->offset;
919*4882a593Smuzhiyun node = xa_parent_locked(xas->xa, node);
920*4882a593Smuzhiyun }
921*4882a593Smuzhiyun
922*4882a593Smuzhiyun if (xa_marked(xas->xa, mark))
923*4882a593Smuzhiyun xa_mark_clear(xas->xa, mark);
924*4882a593Smuzhiyun }
925*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(xas_clear_mark);
926*4882a593Smuzhiyun
927*4882a593Smuzhiyun /**
928*4882a593Smuzhiyun * xas_init_marks() - Initialise all marks for the entry
929*4882a593Smuzhiyun * @xas: Array operations state.
930*4882a593Smuzhiyun *
931*4882a593Smuzhiyun * Initialise all marks for the entry specified by @xas. If we're tracking
932*4882a593Smuzhiyun * free entries with a mark, we need to set it on all entries. All other
933*4882a593Smuzhiyun * marks are cleared.
934*4882a593Smuzhiyun *
935*4882a593Smuzhiyun * This implementation is not as efficient as it could be; we may walk
936*4882a593Smuzhiyun * up the tree multiple times.
937*4882a593Smuzhiyun */
xas_init_marks(const struct xa_state * xas)938*4882a593Smuzhiyun void xas_init_marks(const struct xa_state *xas)
939*4882a593Smuzhiyun {
940*4882a593Smuzhiyun xa_mark_t mark = 0;
941*4882a593Smuzhiyun
942*4882a593Smuzhiyun for (;;) {
943*4882a593Smuzhiyun if (xa_track_free(xas->xa) && mark == XA_FREE_MARK)
944*4882a593Smuzhiyun xas_set_mark(xas, mark);
945*4882a593Smuzhiyun else
946*4882a593Smuzhiyun xas_clear_mark(xas, mark);
947*4882a593Smuzhiyun if (mark == XA_MARK_MAX)
948*4882a593Smuzhiyun break;
949*4882a593Smuzhiyun mark_inc(mark);
950*4882a593Smuzhiyun }
951*4882a593Smuzhiyun }
952*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(xas_init_marks);
953*4882a593Smuzhiyun
954*4882a593Smuzhiyun #ifdef CONFIG_XARRAY_MULTI
node_get_marks(struct xa_node * node,unsigned int offset)955*4882a593Smuzhiyun static unsigned int node_get_marks(struct xa_node *node, unsigned int offset)
956*4882a593Smuzhiyun {
957*4882a593Smuzhiyun unsigned int marks = 0;
958*4882a593Smuzhiyun xa_mark_t mark = XA_MARK_0;
959*4882a593Smuzhiyun
960*4882a593Smuzhiyun for (;;) {
961*4882a593Smuzhiyun if (node_get_mark(node, offset, mark))
962*4882a593Smuzhiyun marks |= 1 << (__force unsigned int)mark;
963*4882a593Smuzhiyun if (mark == XA_MARK_MAX)
964*4882a593Smuzhiyun break;
965*4882a593Smuzhiyun mark_inc(mark);
966*4882a593Smuzhiyun }
967*4882a593Smuzhiyun
968*4882a593Smuzhiyun return marks;
969*4882a593Smuzhiyun }
970*4882a593Smuzhiyun
node_set_marks(struct xa_node * node,unsigned int offset,struct xa_node * child,unsigned int marks)971*4882a593Smuzhiyun static void node_set_marks(struct xa_node *node, unsigned int offset,
972*4882a593Smuzhiyun struct xa_node *child, unsigned int marks)
973*4882a593Smuzhiyun {
974*4882a593Smuzhiyun xa_mark_t mark = XA_MARK_0;
975*4882a593Smuzhiyun
976*4882a593Smuzhiyun for (;;) {
977*4882a593Smuzhiyun if (marks & (1 << (__force unsigned int)mark)) {
978*4882a593Smuzhiyun node_set_mark(node, offset, mark);
979*4882a593Smuzhiyun if (child)
980*4882a593Smuzhiyun node_mark_all(child, mark);
981*4882a593Smuzhiyun }
982*4882a593Smuzhiyun if (mark == XA_MARK_MAX)
983*4882a593Smuzhiyun break;
984*4882a593Smuzhiyun mark_inc(mark);
985*4882a593Smuzhiyun }
986*4882a593Smuzhiyun }
987*4882a593Smuzhiyun
988*4882a593Smuzhiyun /**
989*4882a593Smuzhiyun * xas_split_alloc() - Allocate memory for splitting an entry.
990*4882a593Smuzhiyun * @xas: XArray operation state.
991*4882a593Smuzhiyun * @entry: New entry which will be stored in the array.
992*4882a593Smuzhiyun * @order: New entry order.
993*4882a593Smuzhiyun * @gfp: Memory allocation flags.
994*4882a593Smuzhiyun *
995*4882a593Smuzhiyun * This function should be called before calling xas_split().
996*4882a593Smuzhiyun * If necessary, it will allocate new nodes (and fill them with @entry)
997*4882a593Smuzhiyun * to prepare for the upcoming split of an entry of @order size into
998*4882a593Smuzhiyun * entries of the order stored in the @xas.
999*4882a593Smuzhiyun *
1000*4882a593Smuzhiyun * Context: May sleep if @gfp flags permit.
1001*4882a593Smuzhiyun */
xas_split_alloc(struct xa_state * xas,void * entry,unsigned int order,gfp_t gfp)1002*4882a593Smuzhiyun void xas_split_alloc(struct xa_state *xas, void *entry, unsigned int order,
1003*4882a593Smuzhiyun gfp_t gfp)
1004*4882a593Smuzhiyun {
1005*4882a593Smuzhiyun unsigned int sibs = (1 << (order % XA_CHUNK_SHIFT)) - 1;
1006*4882a593Smuzhiyun unsigned int mask = xas->xa_sibs;
1007*4882a593Smuzhiyun
1008*4882a593Smuzhiyun /* XXX: no support for splitting really large entries yet */
1009*4882a593Smuzhiyun if (WARN_ON(xas->xa_shift + 2 * XA_CHUNK_SHIFT < order))
1010*4882a593Smuzhiyun goto nomem;
1011*4882a593Smuzhiyun if (xas->xa_shift + XA_CHUNK_SHIFT > order)
1012*4882a593Smuzhiyun return;
1013*4882a593Smuzhiyun
1014*4882a593Smuzhiyun do {
1015*4882a593Smuzhiyun unsigned int i;
1016*4882a593Smuzhiyun void *sibling = NULL;
1017*4882a593Smuzhiyun struct xa_node *node;
1018*4882a593Smuzhiyun
1019*4882a593Smuzhiyun node = kmem_cache_alloc(radix_tree_node_cachep, gfp);
1020*4882a593Smuzhiyun if (!node)
1021*4882a593Smuzhiyun goto nomem;
1022*4882a593Smuzhiyun node->array = xas->xa;
1023*4882a593Smuzhiyun for (i = 0; i < XA_CHUNK_SIZE; i++) {
1024*4882a593Smuzhiyun if ((i & mask) == 0) {
1025*4882a593Smuzhiyun RCU_INIT_POINTER(node->slots[i], entry);
1026*4882a593Smuzhiyun sibling = xa_mk_sibling(i);
1027*4882a593Smuzhiyun } else {
1028*4882a593Smuzhiyun RCU_INIT_POINTER(node->slots[i], sibling);
1029*4882a593Smuzhiyun }
1030*4882a593Smuzhiyun }
1031*4882a593Smuzhiyun RCU_INIT_POINTER(node->parent, xas->xa_alloc);
1032*4882a593Smuzhiyun xas->xa_alloc = node;
1033*4882a593Smuzhiyun } while (sibs-- > 0);
1034*4882a593Smuzhiyun
1035*4882a593Smuzhiyun return;
1036*4882a593Smuzhiyun nomem:
1037*4882a593Smuzhiyun xas_destroy(xas);
1038*4882a593Smuzhiyun xas_set_err(xas, -ENOMEM);
1039*4882a593Smuzhiyun }
1040*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(xas_split_alloc);
1041*4882a593Smuzhiyun
1042*4882a593Smuzhiyun /**
1043*4882a593Smuzhiyun * xas_split() - Split a multi-index entry into smaller entries.
1044*4882a593Smuzhiyun * @xas: XArray operation state.
1045*4882a593Smuzhiyun * @entry: New entry to store in the array.
1046*4882a593Smuzhiyun * @order: New entry order.
1047*4882a593Smuzhiyun *
1048*4882a593Smuzhiyun * The value in the entry is copied to all the replacement entries.
1049*4882a593Smuzhiyun *
1050*4882a593Smuzhiyun * Context: Any context. The caller should hold the xa_lock.
1051*4882a593Smuzhiyun */
xas_split(struct xa_state * xas,void * entry,unsigned int order)1052*4882a593Smuzhiyun void xas_split(struct xa_state *xas, void *entry, unsigned int order)
1053*4882a593Smuzhiyun {
1054*4882a593Smuzhiyun unsigned int sibs = (1 << (order % XA_CHUNK_SHIFT)) - 1;
1055*4882a593Smuzhiyun unsigned int offset, marks;
1056*4882a593Smuzhiyun struct xa_node *node;
1057*4882a593Smuzhiyun void *curr = xas_load(xas);
1058*4882a593Smuzhiyun int values = 0;
1059*4882a593Smuzhiyun
1060*4882a593Smuzhiyun node = xas->xa_node;
1061*4882a593Smuzhiyun if (xas_top(node))
1062*4882a593Smuzhiyun return;
1063*4882a593Smuzhiyun
1064*4882a593Smuzhiyun marks = node_get_marks(node, xas->xa_offset);
1065*4882a593Smuzhiyun
1066*4882a593Smuzhiyun offset = xas->xa_offset + sibs;
1067*4882a593Smuzhiyun do {
1068*4882a593Smuzhiyun if (xas->xa_shift < node->shift) {
1069*4882a593Smuzhiyun struct xa_node *child = xas->xa_alloc;
1070*4882a593Smuzhiyun
1071*4882a593Smuzhiyun xas->xa_alloc = rcu_dereference_raw(child->parent);
1072*4882a593Smuzhiyun child->shift = node->shift - XA_CHUNK_SHIFT;
1073*4882a593Smuzhiyun child->offset = offset;
1074*4882a593Smuzhiyun child->count = XA_CHUNK_SIZE;
1075*4882a593Smuzhiyun child->nr_values = xa_is_value(entry) ?
1076*4882a593Smuzhiyun XA_CHUNK_SIZE : 0;
1077*4882a593Smuzhiyun RCU_INIT_POINTER(child->parent, node);
1078*4882a593Smuzhiyun node_set_marks(node, offset, child, marks);
1079*4882a593Smuzhiyun rcu_assign_pointer(node->slots[offset],
1080*4882a593Smuzhiyun xa_mk_node(child));
1081*4882a593Smuzhiyun if (xa_is_value(curr))
1082*4882a593Smuzhiyun values--;
1083*4882a593Smuzhiyun xas_update(xas, child);
1084*4882a593Smuzhiyun } else {
1085*4882a593Smuzhiyun unsigned int canon = offset - xas->xa_sibs;
1086*4882a593Smuzhiyun
1087*4882a593Smuzhiyun node_set_marks(node, canon, NULL, marks);
1088*4882a593Smuzhiyun rcu_assign_pointer(node->slots[canon], entry);
1089*4882a593Smuzhiyun while (offset > canon)
1090*4882a593Smuzhiyun rcu_assign_pointer(node->slots[offset--],
1091*4882a593Smuzhiyun xa_mk_sibling(canon));
1092*4882a593Smuzhiyun values += (xa_is_value(entry) - xa_is_value(curr)) *
1093*4882a593Smuzhiyun (xas->xa_sibs + 1);
1094*4882a593Smuzhiyun }
1095*4882a593Smuzhiyun } while (offset-- > xas->xa_offset);
1096*4882a593Smuzhiyun
1097*4882a593Smuzhiyun node->nr_values += values;
1098*4882a593Smuzhiyun xas_update(xas, node);
1099*4882a593Smuzhiyun }
1100*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(xas_split);
1101*4882a593Smuzhiyun #endif
1102*4882a593Smuzhiyun
1103*4882a593Smuzhiyun /**
1104*4882a593Smuzhiyun * xas_pause() - Pause a walk to drop a lock.
1105*4882a593Smuzhiyun * @xas: XArray operation state.
1106*4882a593Smuzhiyun *
1107*4882a593Smuzhiyun * Some users need to pause a walk and drop the lock they're holding in
1108*4882a593Smuzhiyun * order to yield to a higher priority thread or carry out an operation
1109*4882a593Smuzhiyun * on an entry. Those users should call this function before they drop
1110*4882a593Smuzhiyun * the lock. It resets the @xas to be suitable for the next iteration
1111*4882a593Smuzhiyun * of the loop after the user has reacquired the lock. If most entries
1112*4882a593Smuzhiyun * found during a walk require you to call xas_pause(), the xa_for_each()
1113*4882a593Smuzhiyun * iterator may be more appropriate.
1114*4882a593Smuzhiyun *
1115*4882a593Smuzhiyun * Note that xas_pause() only works for forward iteration. If a user needs
1116*4882a593Smuzhiyun * to pause a reverse iteration, we will need a xas_pause_rev().
1117*4882a593Smuzhiyun */
xas_pause(struct xa_state * xas)1118*4882a593Smuzhiyun void xas_pause(struct xa_state *xas)
1119*4882a593Smuzhiyun {
1120*4882a593Smuzhiyun struct xa_node *node = xas->xa_node;
1121*4882a593Smuzhiyun
1122*4882a593Smuzhiyun if (xas_invalid(xas))
1123*4882a593Smuzhiyun return;
1124*4882a593Smuzhiyun
1125*4882a593Smuzhiyun xas->xa_node = XAS_RESTART;
1126*4882a593Smuzhiyun if (node) {
1127*4882a593Smuzhiyun unsigned long offset = xas->xa_offset;
1128*4882a593Smuzhiyun while (++offset < XA_CHUNK_SIZE) {
1129*4882a593Smuzhiyun if (!xa_is_sibling(xa_entry(xas->xa, node, offset)))
1130*4882a593Smuzhiyun break;
1131*4882a593Smuzhiyun }
1132*4882a593Smuzhiyun xas->xa_index += (offset - xas->xa_offset) << node->shift;
1133*4882a593Smuzhiyun if (xas->xa_index == 0)
1134*4882a593Smuzhiyun xas->xa_node = XAS_BOUNDS;
1135*4882a593Smuzhiyun } else {
1136*4882a593Smuzhiyun xas->xa_index++;
1137*4882a593Smuzhiyun }
1138*4882a593Smuzhiyun }
1139*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(xas_pause);
1140*4882a593Smuzhiyun
1141*4882a593Smuzhiyun /*
1142*4882a593Smuzhiyun * __xas_prev() - Find the previous entry in the XArray.
1143*4882a593Smuzhiyun * @xas: XArray operation state.
1144*4882a593Smuzhiyun *
1145*4882a593Smuzhiyun * Helper function for xas_prev() which handles all the complex cases
1146*4882a593Smuzhiyun * out of line.
1147*4882a593Smuzhiyun */
__xas_prev(struct xa_state * xas)1148*4882a593Smuzhiyun void *__xas_prev(struct xa_state *xas)
1149*4882a593Smuzhiyun {
1150*4882a593Smuzhiyun void *entry;
1151*4882a593Smuzhiyun
1152*4882a593Smuzhiyun if (!xas_frozen(xas->xa_node))
1153*4882a593Smuzhiyun xas->xa_index--;
1154*4882a593Smuzhiyun if (!xas->xa_node)
1155*4882a593Smuzhiyun return set_bounds(xas);
1156*4882a593Smuzhiyun if (xas_not_node(xas->xa_node))
1157*4882a593Smuzhiyun return xas_load(xas);
1158*4882a593Smuzhiyun
1159*4882a593Smuzhiyun if (xas->xa_offset != get_offset(xas->xa_index, xas->xa_node))
1160*4882a593Smuzhiyun xas->xa_offset--;
1161*4882a593Smuzhiyun
1162*4882a593Smuzhiyun while (xas->xa_offset == 255) {
1163*4882a593Smuzhiyun xas->xa_offset = xas->xa_node->offset - 1;
1164*4882a593Smuzhiyun xas->xa_node = xa_parent(xas->xa, xas->xa_node);
1165*4882a593Smuzhiyun if (!xas->xa_node)
1166*4882a593Smuzhiyun return set_bounds(xas);
1167*4882a593Smuzhiyun }
1168*4882a593Smuzhiyun
1169*4882a593Smuzhiyun for (;;) {
1170*4882a593Smuzhiyun entry = xa_entry(xas->xa, xas->xa_node, xas->xa_offset);
1171*4882a593Smuzhiyun if (!xa_is_node(entry))
1172*4882a593Smuzhiyun return entry;
1173*4882a593Smuzhiyun
1174*4882a593Smuzhiyun xas->xa_node = xa_to_node(entry);
1175*4882a593Smuzhiyun xas_set_offset(xas);
1176*4882a593Smuzhiyun }
1177*4882a593Smuzhiyun }
1178*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(__xas_prev);
1179*4882a593Smuzhiyun
1180*4882a593Smuzhiyun /*
1181*4882a593Smuzhiyun * __xas_next() - Find the next entry in the XArray.
1182*4882a593Smuzhiyun * @xas: XArray operation state.
1183*4882a593Smuzhiyun *
1184*4882a593Smuzhiyun * Helper function for xas_next() which handles all the complex cases
1185*4882a593Smuzhiyun * out of line.
1186*4882a593Smuzhiyun */
__xas_next(struct xa_state * xas)1187*4882a593Smuzhiyun void *__xas_next(struct xa_state *xas)
1188*4882a593Smuzhiyun {
1189*4882a593Smuzhiyun void *entry;
1190*4882a593Smuzhiyun
1191*4882a593Smuzhiyun if (!xas_frozen(xas->xa_node))
1192*4882a593Smuzhiyun xas->xa_index++;
1193*4882a593Smuzhiyun if (!xas->xa_node)
1194*4882a593Smuzhiyun return set_bounds(xas);
1195*4882a593Smuzhiyun if (xas_not_node(xas->xa_node))
1196*4882a593Smuzhiyun return xas_load(xas);
1197*4882a593Smuzhiyun
1198*4882a593Smuzhiyun if (xas->xa_offset != get_offset(xas->xa_index, xas->xa_node))
1199*4882a593Smuzhiyun xas->xa_offset++;
1200*4882a593Smuzhiyun
1201*4882a593Smuzhiyun while (xas->xa_offset == XA_CHUNK_SIZE) {
1202*4882a593Smuzhiyun xas->xa_offset = xas->xa_node->offset + 1;
1203*4882a593Smuzhiyun xas->xa_node = xa_parent(xas->xa, xas->xa_node);
1204*4882a593Smuzhiyun if (!xas->xa_node)
1205*4882a593Smuzhiyun return set_bounds(xas);
1206*4882a593Smuzhiyun }
1207*4882a593Smuzhiyun
1208*4882a593Smuzhiyun for (;;) {
1209*4882a593Smuzhiyun entry = xa_entry(xas->xa, xas->xa_node, xas->xa_offset);
1210*4882a593Smuzhiyun if (!xa_is_node(entry))
1211*4882a593Smuzhiyun return entry;
1212*4882a593Smuzhiyun
1213*4882a593Smuzhiyun xas->xa_node = xa_to_node(entry);
1214*4882a593Smuzhiyun xas_set_offset(xas);
1215*4882a593Smuzhiyun }
1216*4882a593Smuzhiyun }
1217*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(__xas_next);
1218*4882a593Smuzhiyun
1219*4882a593Smuzhiyun /**
1220*4882a593Smuzhiyun * xas_find() - Find the next present entry in the XArray.
1221*4882a593Smuzhiyun * @xas: XArray operation state.
1222*4882a593Smuzhiyun * @max: Highest index to return.
1223*4882a593Smuzhiyun *
1224*4882a593Smuzhiyun * If the @xas has not yet been walked to an entry, return the entry
1225*4882a593Smuzhiyun * which has an index >= xas.xa_index. If it has been walked, the entry
1226*4882a593Smuzhiyun * currently being pointed at has been processed, and so we move to the
1227*4882a593Smuzhiyun * next entry.
1228*4882a593Smuzhiyun *
1229*4882a593Smuzhiyun * If no entry is found and the array is smaller than @max, the iterator
1230*4882a593Smuzhiyun * is set to the smallest index not yet in the array. This allows @xas
1231*4882a593Smuzhiyun * to be immediately passed to xas_store().
1232*4882a593Smuzhiyun *
1233*4882a593Smuzhiyun * Return: The entry, if found, otherwise %NULL.
1234*4882a593Smuzhiyun */
xas_find(struct xa_state * xas,unsigned long max)1235*4882a593Smuzhiyun void *xas_find(struct xa_state *xas, unsigned long max)
1236*4882a593Smuzhiyun {
1237*4882a593Smuzhiyun void *entry;
1238*4882a593Smuzhiyun
1239*4882a593Smuzhiyun if (xas_error(xas) || xas->xa_node == XAS_BOUNDS)
1240*4882a593Smuzhiyun return NULL;
1241*4882a593Smuzhiyun if (xas->xa_index > max)
1242*4882a593Smuzhiyun return set_bounds(xas);
1243*4882a593Smuzhiyun
1244*4882a593Smuzhiyun if (!xas->xa_node) {
1245*4882a593Smuzhiyun xas->xa_index = 1;
1246*4882a593Smuzhiyun return set_bounds(xas);
1247*4882a593Smuzhiyun } else if (xas->xa_node == XAS_RESTART) {
1248*4882a593Smuzhiyun entry = xas_load(xas);
1249*4882a593Smuzhiyun if (entry || xas_not_node(xas->xa_node))
1250*4882a593Smuzhiyun return entry;
1251*4882a593Smuzhiyun } else if (!xas->xa_node->shift &&
1252*4882a593Smuzhiyun xas->xa_offset != (xas->xa_index & XA_CHUNK_MASK)) {
1253*4882a593Smuzhiyun xas->xa_offset = ((xas->xa_index - 1) & XA_CHUNK_MASK) + 1;
1254*4882a593Smuzhiyun }
1255*4882a593Smuzhiyun
1256*4882a593Smuzhiyun xas_advance(xas);
1257*4882a593Smuzhiyun
1258*4882a593Smuzhiyun while (xas->xa_node && (xas->xa_index <= max)) {
1259*4882a593Smuzhiyun if (unlikely(xas->xa_offset == XA_CHUNK_SIZE)) {
1260*4882a593Smuzhiyun xas->xa_offset = xas->xa_node->offset + 1;
1261*4882a593Smuzhiyun xas->xa_node = xa_parent(xas->xa, xas->xa_node);
1262*4882a593Smuzhiyun continue;
1263*4882a593Smuzhiyun }
1264*4882a593Smuzhiyun
1265*4882a593Smuzhiyun entry = xa_entry(xas->xa, xas->xa_node, xas->xa_offset);
1266*4882a593Smuzhiyun if (xa_is_node(entry)) {
1267*4882a593Smuzhiyun xas->xa_node = xa_to_node(entry);
1268*4882a593Smuzhiyun xas->xa_offset = 0;
1269*4882a593Smuzhiyun continue;
1270*4882a593Smuzhiyun }
1271*4882a593Smuzhiyun if (entry && !xa_is_sibling(entry))
1272*4882a593Smuzhiyun return entry;
1273*4882a593Smuzhiyun
1274*4882a593Smuzhiyun xas_advance(xas);
1275*4882a593Smuzhiyun }
1276*4882a593Smuzhiyun
1277*4882a593Smuzhiyun if (!xas->xa_node)
1278*4882a593Smuzhiyun xas->xa_node = XAS_BOUNDS;
1279*4882a593Smuzhiyun return NULL;
1280*4882a593Smuzhiyun }
1281*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(xas_find);
1282*4882a593Smuzhiyun
1283*4882a593Smuzhiyun /**
1284*4882a593Smuzhiyun * xas_find_marked() - Find the next marked entry in the XArray.
1285*4882a593Smuzhiyun * @xas: XArray operation state.
1286*4882a593Smuzhiyun * @max: Highest index to return.
1287*4882a593Smuzhiyun * @mark: Mark number to search for.
1288*4882a593Smuzhiyun *
1289*4882a593Smuzhiyun * If the @xas has not yet been walked to an entry, return the marked entry
1290*4882a593Smuzhiyun * which has an index >= xas.xa_index. If it has been walked, the entry
1291*4882a593Smuzhiyun * currently being pointed at has been processed, and so we return the
1292*4882a593Smuzhiyun * first marked entry with an index > xas.xa_index.
1293*4882a593Smuzhiyun *
1294*4882a593Smuzhiyun * If no marked entry is found and the array is smaller than @max, @xas is
1295*4882a593Smuzhiyun * set to the bounds state and xas->xa_index is set to the smallest index
1296*4882a593Smuzhiyun * not yet in the array. This allows @xas to be immediately passed to
1297*4882a593Smuzhiyun * xas_store().
1298*4882a593Smuzhiyun *
1299*4882a593Smuzhiyun * If no entry is found before @max is reached, @xas is set to the restart
1300*4882a593Smuzhiyun * state.
1301*4882a593Smuzhiyun *
1302*4882a593Smuzhiyun * Return: The entry, if found, otherwise %NULL.
1303*4882a593Smuzhiyun */
xas_find_marked(struct xa_state * xas,unsigned long max,xa_mark_t mark)1304*4882a593Smuzhiyun void *xas_find_marked(struct xa_state *xas, unsigned long max, xa_mark_t mark)
1305*4882a593Smuzhiyun {
1306*4882a593Smuzhiyun bool advance = true;
1307*4882a593Smuzhiyun unsigned int offset;
1308*4882a593Smuzhiyun void *entry;
1309*4882a593Smuzhiyun
1310*4882a593Smuzhiyun if (xas_error(xas))
1311*4882a593Smuzhiyun return NULL;
1312*4882a593Smuzhiyun if (xas->xa_index > max)
1313*4882a593Smuzhiyun goto max;
1314*4882a593Smuzhiyun
1315*4882a593Smuzhiyun if (!xas->xa_node) {
1316*4882a593Smuzhiyun xas->xa_index = 1;
1317*4882a593Smuzhiyun goto out;
1318*4882a593Smuzhiyun } else if (xas_top(xas->xa_node)) {
1319*4882a593Smuzhiyun advance = false;
1320*4882a593Smuzhiyun entry = xa_head(xas->xa);
1321*4882a593Smuzhiyun xas->xa_node = NULL;
1322*4882a593Smuzhiyun if (xas->xa_index > max_index(entry))
1323*4882a593Smuzhiyun goto out;
1324*4882a593Smuzhiyun if (!xa_is_node(entry)) {
1325*4882a593Smuzhiyun if (xa_marked(xas->xa, mark))
1326*4882a593Smuzhiyun return entry;
1327*4882a593Smuzhiyun xas->xa_index = 1;
1328*4882a593Smuzhiyun goto out;
1329*4882a593Smuzhiyun }
1330*4882a593Smuzhiyun xas->xa_node = xa_to_node(entry);
1331*4882a593Smuzhiyun xas->xa_offset = xas->xa_index >> xas->xa_node->shift;
1332*4882a593Smuzhiyun }
1333*4882a593Smuzhiyun
1334*4882a593Smuzhiyun while (xas->xa_index <= max) {
1335*4882a593Smuzhiyun if (unlikely(xas->xa_offset == XA_CHUNK_SIZE)) {
1336*4882a593Smuzhiyun xas->xa_offset = xas->xa_node->offset + 1;
1337*4882a593Smuzhiyun xas->xa_node = xa_parent(xas->xa, xas->xa_node);
1338*4882a593Smuzhiyun if (!xas->xa_node)
1339*4882a593Smuzhiyun break;
1340*4882a593Smuzhiyun advance = false;
1341*4882a593Smuzhiyun continue;
1342*4882a593Smuzhiyun }
1343*4882a593Smuzhiyun
1344*4882a593Smuzhiyun if (!advance) {
1345*4882a593Smuzhiyun entry = xa_entry(xas->xa, xas->xa_node, xas->xa_offset);
1346*4882a593Smuzhiyun if (xa_is_sibling(entry)) {
1347*4882a593Smuzhiyun xas->xa_offset = xa_to_sibling(entry);
1348*4882a593Smuzhiyun xas_move_index(xas, xas->xa_offset);
1349*4882a593Smuzhiyun }
1350*4882a593Smuzhiyun }
1351*4882a593Smuzhiyun
1352*4882a593Smuzhiyun offset = xas_find_chunk(xas, advance, mark);
1353*4882a593Smuzhiyun if (offset > xas->xa_offset) {
1354*4882a593Smuzhiyun advance = false;
1355*4882a593Smuzhiyun xas_move_index(xas, offset);
1356*4882a593Smuzhiyun /* Mind the wrap */
1357*4882a593Smuzhiyun if ((xas->xa_index - 1) >= max)
1358*4882a593Smuzhiyun goto max;
1359*4882a593Smuzhiyun xas->xa_offset = offset;
1360*4882a593Smuzhiyun if (offset == XA_CHUNK_SIZE)
1361*4882a593Smuzhiyun continue;
1362*4882a593Smuzhiyun }
1363*4882a593Smuzhiyun
1364*4882a593Smuzhiyun entry = xa_entry(xas->xa, xas->xa_node, xas->xa_offset);
1365*4882a593Smuzhiyun if (!entry && !(xa_track_free(xas->xa) && mark == XA_FREE_MARK))
1366*4882a593Smuzhiyun continue;
1367*4882a593Smuzhiyun if (!xa_is_node(entry))
1368*4882a593Smuzhiyun return entry;
1369*4882a593Smuzhiyun xas->xa_node = xa_to_node(entry);
1370*4882a593Smuzhiyun xas_set_offset(xas);
1371*4882a593Smuzhiyun }
1372*4882a593Smuzhiyun
1373*4882a593Smuzhiyun out:
1374*4882a593Smuzhiyun if (xas->xa_index > max)
1375*4882a593Smuzhiyun goto max;
1376*4882a593Smuzhiyun return set_bounds(xas);
1377*4882a593Smuzhiyun max:
1378*4882a593Smuzhiyun xas->xa_node = XAS_RESTART;
1379*4882a593Smuzhiyun return NULL;
1380*4882a593Smuzhiyun }
1381*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(xas_find_marked);
1382*4882a593Smuzhiyun
1383*4882a593Smuzhiyun /**
1384*4882a593Smuzhiyun * xas_find_conflict() - Find the next present entry in a range.
1385*4882a593Smuzhiyun * @xas: XArray operation state.
1386*4882a593Smuzhiyun *
1387*4882a593Smuzhiyun * The @xas describes both a range and a position within that range.
1388*4882a593Smuzhiyun *
1389*4882a593Smuzhiyun * Context: Any context. Expects xa_lock to be held.
1390*4882a593Smuzhiyun * Return: The next entry in the range covered by @xas or %NULL.
1391*4882a593Smuzhiyun */
xas_find_conflict(struct xa_state * xas)1392*4882a593Smuzhiyun void *xas_find_conflict(struct xa_state *xas)
1393*4882a593Smuzhiyun {
1394*4882a593Smuzhiyun void *curr;
1395*4882a593Smuzhiyun
1396*4882a593Smuzhiyun if (xas_error(xas))
1397*4882a593Smuzhiyun return NULL;
1398*4882a593Smuzhiyun
1399*4882a593Smuzhiyun if (!xas->xa_node)
1400*4882a593Smuzhiyun return NULL;
1401*4882a593Smuzhiyun
1402*4882a593Smuzhiyun if (xas_top(xas->xa_node)) {
1403*4882a593Smuzhiyun curr = xas_start(xas);
1404*4882a593Smuzhiyun if (!curr)
1405*4882a593Smuzhiyun return NULL;
1406*4882a593Smuzhiyun while (xa_is_node(curr)) {
1407*4882a593Smuzhiyun struct xa_node *node = xa_to_node(curr);
1408*4882a593Smuzhiyun curr = xas_descend(xas, node);
1409*4882a593Smuzhiyun }
1410*4882a593Smuzhiyun if (curr)
1411*4882a593Smuzhiyun return curr;
1412*4882a593Smuzhiyun }
1413*4882a593Smuzhiyun
1414*4882a593Smuzhiyun if (xas->xa_node->shift > xas->xa_shift)
1415*4882a593Smuzhiyun return NULL;
1416*4882a593Smuzhiyun
1417*4882a593Smuzhiyun for (;;) {
1418*4882a593Smuzhiyun if (xas->xa_node->shift == xas->xa_shift) {
1419*4882a593Smuzhiyun if ((xas->xa_offset & xas->xa_sibs) == xas->xa_sibs)
1420*4882a593Smuzhiyun break;
1421*4882a593Smuzhiyun } else if (xas->xa_offset == XA_CHUNK_MASK) {
1422*4882a593Smuzhiyun xas->xa_offset = xas->xa_node->offset;
1423*4882a593Smuzhiyun xas->xa_node = xa_parent_locked(xas->xa, xas->xa_node);
1424*4882a593Smuzhiyun if (!xas->xa_node)
1425*4882a593Smuzhiyun break;
1426*4882a593Smuzhiyun continue;
1427*4882a593Smuzhiyun }
1428*4882a593Smuzhiyun curr = xa_entry_locked(xas->xa, xas->xa_node, ++xas->xa_offset);
1429*4882a593Smuzhiyun if (xa_is_sibling(curr))
1430*4882a593Smuzhiyun continue;
1431*4882a593Smuzhiyun while (xa_is_node(curr)) {
1432*4882a593Smuzhiyun xas->xa_node = xa_to_node(curr);
1433*4882a593Smuzhiyun xas->xa_offset = 0;
1434*4882a593Smuzhiyun curr = xa_entry_locked(xas->xa, xas->xa_node, 0);
1435*4882a593Smuzhiyun }
1436*4882a593Smuzhiyun if (curr)
1437*4882a593Smuzhiyun return curr;
1438*4882a593Smuzhiyun }
1439*4882a593Smuzhiyun xas->xa_offset -= xas->xa_sibs;
1440*4882a593Smuzhiyun return NULL;
1441*4882a593Smuzhiyun }
1442*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(xas_find_conflict);
1443*4882a593Smuzhiyun
1444*4882a593Smuzhiyun /**
1445*4882a593Smuzhiyun * xa_load() - Load an entry from an XArray.
1446*4882a593Smuzhiyun * @xa: XArray.
1447*4882a593Smuzhiyun * @index: index into array.
1448*4882a593Smuzhiyun *
1449*4882a593Smuzhiyun * Context: Any context. Takes and releases the RCU lock.
1450*4882a593Smuzhiyun * Return: The entry at @index in @xa.
1451*4882a593Smuzhiyun */
xa_load(struct xarray * xa,unsigned long index)1452*4882a593Smuzhiyun void *xa_load(struct xarray *xa, unsigned long index)
1453*4882a593Smuzhiyun {
1454*4882a593Smuzhiyun XA_STATE(xas, xa, index);
1455*4882a593Smuzhiyun void *entry;
1456*4882a593Smuzhiyun
1457*4882a593Smuzhiyun rcu_read_lock();
1458*4882a593Smuzhiyun do {
1459*4882a593Smuzhiyun entry = xas_load(&xas);
1460*4882a593Smuzhiyun if (xa_is_zero(entry))
1461*4882a593Smuzhiyun entry = NULL;
1462*4882a593Smuzhiyun } while (xas_retry(&xas, entry));
1463*4882a593Smuzhiyun rcu_read_unlock();
1464*4882a593Smuzhiyun
1465*4882a593Smuzhiyun return entry;
1466*4882a593Smuzhiyun }
1467*4882a593Smuzhiyun EXPORT_SYMBOL(xa_load);
1468*4882a593Smuzhiyun
xas_result(struct xa_state * xas,void * curr)1469*4882a593Smuzhiyun static void *xas_result(struct xa_state *xas, void *curr)
1470*4882a593Smuzhiyun {
1471*4882a593Smuzhiyun if (xa_is_zero(curr))
1472*4882a593Smuzhiyun return NULL;
1473*4882a593Smuzhiyun if (xas_error(xas))
1474*4882a593Smuzhiyun curr = xas->xa_node;
1475*4882a593Smuzhiyun return curr;
1476*4882a593Smuzhiyun }
1477*4882a593Smuzhiyun
1478*4882a593Smuzhiyun /**
1479*4882a593Smuzhiyun * __xa_erase() - Erase this entry from the XArray while locked.
1480*4882a593Smuzhiyun * @xa: XArray.
1481*4882a593Smuzhiyun * @index: Index into array.
1482*4882a593Smuzhiyun *
1483*4882a593Smuzhiyun * After this function returns, loading from @index will return %NULL.
1484*4882a593Smuzhiyun * If the index is part of a multi-index entry, all indices will be erased
1485*4882a593Smuzhiyun * and none of the entries will be part of a multi-index entry.
1486*4882a593Smuzhiyun *
1487*4882a593Smuzhiyun * Context: Any context. Expects xa_lock to be held on entry.
1488*4882a593Smuzhiyun * Return: The entry which used to be at this index.
1489*4882a593Smuzhiyun */
__xa_erase(struct xarray * xa,unsigned long index)1490*4882a593Smuzhiyun void *__xa_erase(struct xarray *xa, unsigned long index)
1491*4882a593Smuzhiyun {
1492*4882a593Smuzhiyun XA_STATE(xas, xa, index);
1493*4882a593Smuzhiyun return xas_result(&xas, xas_store(&xas, NULL));
1494*4882a593Smuzhiyun }
1495*4882a593Smuzhiyun EXPORT_SYMBOL(__xa_erase);
1496*4882a593Smuzhiyun
1497*4882a593Smuzhiyun /**
1498*4882a593Smuzhiyun * xa_erase() - Erase this entry from the XArray.
1499*4882a593Smuzhiyun * @xa: XArray.
1500*4882a593Smuzhiyun * @index: Index of entry.
1501*4882a593Smuzhiyun *
1502*4882a593Smuzhiyun * After this function returns, loading from @index will return %NULL.
1503*4882a593Smuzhiyun * If the index is part of a multi-index entry, all indices will be erased
1504*4882a593Smuzhiyun * and none of the entries will be part of a multi-index entry.
1505*4882a593Smuzhiyun *
1506*4882a593Smuzhiyun * Context: Any context. Takes and releases the xa_lock.
1507*4882a593Smuzhiyun * Return: The entry which used to be at this index.
1508*4882a593Smuzhiyun */
xa_erase(struct xarray * xa,unsigned long index)1509*4882a593Smuzhiyun void *xa_erase(struct xarray *xa, unsigned long index)
1510*4882a593Smuzhiyun {
1511*4882a593Smuzhiyun void *entry;
1512*4882a593Smuzhiyun
1513*4882a593Smuzhiyun xa_lock(xa);
1514*4882a593Smuzhiyun entry = __xa_erase(xa, index);
1515*4882a593Smuzhiyun xa_unlock(xa);
1516*4882a593Smuzhiyun
1517*4882a593Smuzhiyun return entry;
1518*4882a593Smuzhiyun }
1519*4882a593Smuzhiyun EXPORT_SYMBOL(xa_erase);
1520*4882a593Smuzhiyun
1521*4882a593Smuzhiyun /**
1522*4882a593Smuzhiyun * __xa_store() - Store this entry in the XArray.
1523*4882a593Smuzhiyun * @xa: XArray.
1524*4882a593Smuzhiyun * @index: Index into array.
1525*4882a593Smuzhiyun * @entry: New entry.
1526*4882a593Smuzhiyun * @gfp: Memory allocation flags.
1527*4882a593Smuzhiyun *
1528*4882a593Smuzhiyun * You must already be holding the xa_lock when calling this function.
1529*4882a593Smuzhiyun * It will drop the lock if needed to allocate memory, and then reacquire
1530*4882a593Smuzhiyun * it afterwards.
1531*4882a593Smuzhiyun *
1532*4882a593Smuzhiyun * Context: Any context. Expects xa_lock to be held on entry. May
1533*4882a593Smuzhiyun * release and reacquire xa_lock if @gfp flags permit.
1534*4882a593Smuzhiyun * Return: The old entry at this index or xa_err() if an error happened.
1535*4882a593Smuzhiyun */
__xa_store(struct xarray * xa,unsigned long index,void * entry,gfp_t gfp)1536*4882a593Smuzhiyun void *__xa_store(struct xarray *xa, unsigned long index, void *entry, gfp_t gfp)
1537*4882a593Smuzhiyun {
1538*4882a593Smuzhiyun XA_STATE(xas, xa, index);
1539*4882a593Smuzhiyun void *curr;
1540*4882a593Smuzhiyun
1541*4882a593Smuzhiyun if (WARN_ON_ONCE(xa_is_advanced(entry)))
1542*4882a593Smuzhiyun return XA_ERROR(-EINVAL);
1543*4882a593Smuzhiyun if (xa_track_free(xa) && !entry)
1544*4882a593Smuzhiyun entry = XA_ZERO_ENTRY;
1545*4882a593Smuzhiyun
1546*4882a593Smuzhiyun do {
1547*4882a593Smuzhiyun curr = xas_store(&xas, entry);
1548*4882a593Smuzhiyun if (xa_track_free(xa))
1549*4882a593Smuzhiyun xas_clear_mark(&xas, XA_FREE_MARK);
1550*4882a593Smuzhiyun } while (__xas_nomem(&xas, gfp));
1551*4882a593Smuzhiyun
1552*4882a593Smuzhiyun return xas_result(&xas, curr);
1553*4882a593Smuzhiyun }
1554*4882a593Smuzhiyun EXPORT_SYMBOL(__xa_store);
1555*4882a593Smuzhiyun
1556*4882a593Smuzhiyun /**
1557*4882a593Smuzhiyun * xa_store() - Store this entry in the XArray.
1558*4882a593Smuzhiyun * @xa: XArray.
1559*4882a593Smuzhiyun * @index: Index into array.
1560*4882a593Smuzhiyun * @entry: New entry.
1561*4882a593Smuzhiyun * @gfp: Memory allocation flags.
1562*4882a593Smuzhiyun *
1563*4882a593Smuzhiyun * After this function returns, loads from this index will return @entry.
1564*4882a593Smuzhiyun * Storing into an existing multi-index entry updates the entry of every index.
1565*4882a593Smuzhiyun * The marks associated with @index are unaffected unless @entry is %NULL.
1566*4882a593Smuzhiyun *
1567*4882a593Smuzhiyun * Context: Any context. Takes and releases the xa_lock.
1568*4882a593Smuzhiyun * May sleep if the @gfp flags permit.
1569*4882a593Smuzhiyun * Return: The old entry at this index on success, xa_err(-EINVAL) if @entry
1570*4882a593Smuzhiyun * cannot be stored in an XArray, or xa_err(-ENOMEM) if memory allocation
1571*4882a593Smuzhiyun * failed.
1572*4882a593Smuzhiyun */
xa_store(struct xarray * xa,unsigned long index,void * entry,gfp_t gfp)1573*4882a593Smuzhiyun void *xa_store(struct xarray *xa, unsigned long index, void *entry, gfp_t gfp)
1574*4882a593Smuzhiyun {
1575*4882a593Smuzhiyun void *curr;
1576*4882a593Smuzhiyun
1577*4882a593Smuzhiyun xa_lock(xa);
1578*4882a593Smuzhiyun curr = __xa_store(xa, index, entry, gfp);
1579*4882a593Smuzhiyun xa_unlock(xa);
1580*4882a593Smuzhiyun
1581*4882a593Smuzhiyun return curr;
1582*4882a593Smuzhiyun }
1583*4882a593Smuzhiyun EXPORT_SYMBOL(xa_store);
1584*4882a593Smuzhiyun
1585*4882a593Smuzhiyun /**
1586*4882a593Smuzhiyun * __xa_cmpxchg() - Store this entry in the XArray.
1587*4882a593Smuzhiyun * @xa: XArray.
1588*4882a593Smuzhiyun * @index: Index into array.
1589*4882a593Smuzhiyun * @old: Old value to test against.
1590*4882a593Smuzhiyun * @entry: New entry.
1591*4882a593Smuzhiyun * @gfp: Memory allocation flags.
1592*4882a593Smuzhiyun *
1593*4882a593Smuzhiyun * You must already be holding the xa_lock when calling this function.
1594*4882a593Smuzhiyun * It will drop the lock if needed to allocate memory, and then reacquire
1595*4882a593Smuzhiyun * it afterwards.
1596*4882a593Smuzhiyun *
1597*4882a593Smuzhiyun * Context: Any context. Expects xa_lock to be held on entry. May
1598*4882a593Smuzhiyun * release and reacquire xa_lock if @gfp flags permit.
1599*4882a593Smuzhiyun * Return: The old entry at this index or xa_err() if an error happened.
1600*4882a593Smuzhiyun */
__xa_cmpxchg(struct xarray * xa,unsigned long index,void * old,void * entry,gfp_t gfp)1601*4882a593Smuzhiyun void *__xa_cmpxchg(struct xarray *xa, unsigned long index,
1602*4882a593Smuzhiyun void *old, void *entry, gfp_t gfp)
1603*4882a593Smuzhiyun {
1604*4882a593Smuzhiyun XA_STATE(xas, xa, index);
1605*4882a593Smuzhiyun void *curr;
1606*4882a593Smuzhiyun
1607*4882a593Smuzhiyun if (WARN_ON_ONCE(xa_is_advanced(entry)))
1608*4882a593Smuzhiyun return XA_ERROR(-EINVAL);
1609*4882a593Smuzhiyun
1610*4882a593Smuzhiyun do {
1611*4882a593Smuzhiyun curr = xas_load(&xas);
1612*4882a593Smuzhiyun if (curr == old) {
1613*4882a593Smuzhiyun xas_store(&xas, entry);
1614*4882a593Smuzhiyun if (xa_track_free(xa) && entry && !curr)
1615*4882a593Smuzhiyun xas_clear_mark(&xas, XA_FREE_MARK);
1616*4882a593Smuzhiyun }
1617*4882a593Smuzhiyun } while (__xas_nomem(&xas, gfp));
1618*4882a593Smuzhiyun
1619*4882a593Smuzhiyun return xas_result(&xas, curr);
1620*4882a593Smuzhiyun }
1621*4882a593Smuzhiyun EXPORT_SYMBOL(__xa_cmpxchg);
1622*4882a593Smuzhiyun
1623*4882a593Smuzhiyun /**
1624*4882a593Smuzhiyun * __xa_insert() - Store this entry in the XArray if no entry is present.
1625*4882a593Smuzhiyun * @xa: XArray.
1626*4882a593Smuzhiyun * @index: Index into array.
1627*4882a593Smuzhiyun * @entry: New entry.
1628*4882a593Smuzhiyun * @gfp: Memory allocation flags.
1629*4882a593Smuzhiyun *
1630*4882a593Smuzhiyun * Inserting a NULL entry will store a reserved entry (like xa_reserve())
1631*4882a593Smuzhiyun * if no entry is present. Inserting will fail if a reserved entry is
1632*4882a593Smuzhiyun * present, even though loading from this index will return NULL.
1633*4882a593Smuzhiyun *
1634*4882a593Smuzhiyun * Context: Any context. Expects xa_lock to be held on entry. May
1635*4882a593Smuzhiyun * release and reacquire xa_lock if @gfp flags permit.
1636*4882a593Smuzhiyun * Return: 0 if the store succeeded. -EBUSY if another entry was present.
1637*4882a593Smuzhiyun * -ENOMEM if memory could not be allocated.
1638*4882a593Smuzhiyun */
__xa_insert(struct xarray * xa,unsigned long index,void * entry,gfp_t gfp)1639*4882a593Smuzhiyun int __xa_insert(struct xarray *xa, unsigned long index, void *entry, gfp_t gfp)
1640*4882a593Smuzhiyun {
1641*4882a593Smuzhiyun XA_STATE(xas, xa, index);
1642*4882a593Smuzhiyun void *curr;
1643*4882a593Smuzhiyun
1644*4882a593Smuzhiyun if (WARN_ON_ONCE(xa_is_advanced(entry)))
1645*4882a593Smuzhiyun return -EINVAL;
1646*4882a593Smuzhiyun if (!entry)
1647*4882a593Smuzhiyun entry = XA_ZERO_ENTRY;
1648*4882a593Smuzhiyun
1649*4882a593Smuzhiyun do {
1650*4882a593Smuzhiyun curr = xas_load(&xas);
1651*4882a593Smuzhiyun if (!curr) {
1652*4882a593Smuzhiyun xas_store(&xas, entry);
1653*4882a593Smuzhiyun if (xa_track_free(xa))
1654*4882a593Smuzhiyun xas_clear_mark(&xas, XA_FREE_MARK);
1655*4882a593Smuzhiyun } else {
1656*4882a593Smuzhiyun xas_set_err(&xas, -EBUSY);
1657*4882a593Smuzhiyun }
1658*4882a593Smuzhiyun } while (__xas_nomem(&xas, gfp));
1659*4882a593Smuzhiyun
1660*4882a593Smuzhiyun return xas_error(&xas);
1661*4882a593Smuzhiyun }
1662*4882a593Smuzhiyun EXPORT_SYMBOL(__xa_insert);
1663*4882a593Smuzhiyun
1664*4882a593Smuzhiyun #ifdef CONFIG_XARRAY_MULTI
xas_set_range(struct xa_state * xas,unsigned long first,unsigned long last)1665*4882a593Smuzhiyun static void xas_set_range(struct xa_state *xas, unsigned long first,
1666*4882a593Smuzhiyun unsigned long last)
1667*4882a593Smuzhiyun {
1668*4882a593Smuzhiyun unsigned int shift = 0;
1669*4882a593Smuzhiyun unsigned long sibs = last - first;
1670*4882a593Smuzhiyun unsigned int offset = XA_CHUNK_MASK;
1671*4882a593Smuzhiyun
1672*4882a593Smuzhiyun xas_set(xas, first);
1673*4882a593Smuzhiyun
1674*4882a593Smuzhiyun while ((first & XA_CHUNK_MASK) == 0) {
1675*4882a593Smuzhiyun if (sibs < XA_CHUNK_MASK)
1676*4882a593Smuzhiyun break;
1677*4882a593Smuzhiyun if ((sibs == XA_CHUNK_MASK) && (offset < XA_CHUNK_MASK))
1678*4882a593Smuzhiyun break;
1679*4882a593Smuzhiyun shift += XA_CHUNK_SHIFT;
1680*4882a593Smuzhiyun if (offset == XA_CHUNK_MASK)
1681*4882a593Smuzhiyun offset = sibs & XA_CHUNK_MASK;
1682*4882a593Smuzhiyun sibs >>= XA_CHUNK_SHIFT;
1683*4882a593Smuzhiyun first >>= XA_CHUNK_SHIFT;
1684*4882a593Smuzhiyun }
1685*4882a593Smuzhiyun
1686*4882a593Smuzhiyun offset = first & XA_CHUNK_MASK;
1687*4882a593Smuzhiyun if (offset + sibs > XA_CHUNK_MASK)
1688*4882a593Smuzhiyun sibs = XA_CHUNK_MASK - offset;
1689*4882a593Smuzhiyun if ((((first + sibs + 1) << shift) - 1) > last)
1690*4882a593Smuzhiyun sibs -= 1;
1691*4882a593Smuzhiyun
1692*4882a593Smuzhiyun xas->xa_shift = shift;
1693*4882a593Smuzhiyun xas->xa_sibs = sibs;
1694*4882a593Smuzhiyun }
1695*4882a593Smuzhiyun
1696*4882a593Smuzhiyun /**
1697*4882a593Smuzhiyun * xa_store_range() - Store this entry at a range of indices in the XArray.
1698*4882a593Smuzhiyun * @xa: XArray.
1699*4882a593Smuzhiyun * @first: First index to affect.
1700*4882a593Smuzhiyun * @last: Last index to affect.
1701*4882a593Smuzhiyun * @entry: New entry.
1702*4882a593Smuzhiyun * @gfp: Memory allocation flags.
1703*4882a593Smuzhiyun *
1704*4882a593Smuzhiyun * After this function returns, loads from any index between @first and @last,
1705*4882a593Smuzhiyun * inclusive will return @entry.
1706*4882a593Smuzhiyun * Storing into an existing multi-index entry updates the entry of every index.
1707*4882a593Smuzhiyun * The marks associated with @index are unaffected unless @entry is %NULL.
1708*4882a593Smuzhiyun *
1709*4882a593Smuzhiyun * Context: Process context. Takes and releases the xa_lock. May sleep
1710*4882a593Smuzhiyun * if the @gfp flags permit.
1711*4882a593Smuzhiyun * Return: %NULL on success, xa_err(-EINVAL) if @entry cannot be stored in
1712*4882a593Smuzhiyun * an XArray, or xa_err(-ENOMEM) if memory allocation failed.
1713*4882a593Smuzhiyun */
xa_store_range(struct xarray * xa,unsigned long first,unsigned long last,void * entry,gfp_t gfp)1714*4882a593Smuzhiyun void *xa_store_range(struct xarray *xa, unsigned long first,
1715*4882a593Smuzhiyun unsigned long last, void *entry, gfp_t gfp)
1716*4882a593Smuzhiyun {
1717*4882a593Smuzhiyun XA_STATE(xas, xa, 0);
1718*4882a593Smuzhiyun
1719*4882a593Smuzhiyun if (WARN_ON_ONCE(xa_is_internal(entry)))
1720*4882a593Smuzhiyun return XA_ERROR(-EINVAL);
1721*4882a593Smuzhiyun if (last < first)
1722*4882a593Smuzhiyun return XA_ERROR(-EINVAL);
1723*4882a593Smuzhiyun
1724*4882a593Smuzhiyun do {
1725*4882a593Smuzhiyun xas_lock(&xas);
1726*4882a593Smuzhiyun if (entry) {
1727*4882a593Smuzhiyun unsigned int order = BITS_PER_LONG;
1728*4882a593Smuzhiyun if (last + 1)
1729*4882a593Smuzhiyun order = __ffs(last + 1);
1730*4882a593Smuzhiyun xas_set_order(&xas, last, order);
1731*4882a593Smuzhiyun xas_create(&xas, true);
1732*4882a593Smuzhiyun if (xas_error(&xas))
1733*4882a593Smuzhiyun goto unlock;
1734*4882a593Smuzhiyun }
1735*4882a593Smuzhiyun do {
1736*4882a593Smuzhiyun xas_set_range(&xas, first, last);
1737*4882a593Smuzhiyun xas_store(&xas, entry);
1738*4882a593Smuzhiyun if (xas_error(&xas))
1739*4882a593Smuzhiyun goto unlock;
1740*4882a593Smuzhiyun first += xas_size(&xas);
1741*4882a593Smuzhiyun } while (first <= last);
1742*4882a593Smuzhiyun unlock:
1743*4882a593Smuzhiyun xas_unlock(&xas);
1744*4882a593Smuzhiyun } while (xas_nomem(&xas, gfp));
1745*4882a593Smuzhiyun
1746*4882a593Smuzhiyun return xas_result(&xas, NULL);
1747*4882a593Smuzhiyun }
1748*4882a593Smuzhiyun EXPORT_SYMBOL(xa_store_range);
1749*4882a593Smuzhiyun
1750*4882a593Smuzhiyun /**
1751*4882a593Smuzhiyun * xa_get_order() - Get the order of an entry.
1752*4882a593Smuzhiyun * @xa: XArray.
1753*4882a593Smuzhiyun * @index: Index of the entry.
1754*4882a593Smuzhiyun *
1755*4882a593Smuzhiyun * Return: A number between 0 and 63 indicating the order of the entry.
1756*4882a593Smuzhiyun */
xa_get_order(struct xarray * xa,unsigned long index)1757*4882a593Smuzhiyun int xa_get_order(struct xarray *xa, unsigned long index)
1758*4882a593Smuzhiyun {
1759*4882a593Smuzhiyun XA_STATE(xas, xa, index);
1760*4882a593Smuzhiyun void *entry;
1761*4882a593Smuzhiyun int order = 0;
1762*4882a593Smuzhiyun
1763*4882a593Smuzhiyun rcu_read_lock();
1764*4882a593Smuzhiyun entry = xas_load(&xas);
1765*4882a593Smuzhiyun
1766*4882a593Smuzhiyun if (!entry)
1767*4882a593Smuzhiyun goto unlock;
1768*4882a593Smuzhiyun
1769*4882a593Smuzhiyun if (!xas.xa_node)
1770*4882a593Smuzhiyun goto unlock;
1771*4882a593Smuzhiyun
1772*4882a593Smuzhiyun for (;;) {
1773*4882a593Smuzhiyun unsigned int slot = xas.xa_offset + (1 << order);
1774*4882a593Smuzhiyun
1775*4882a593Smuzhiyun if (slot >= XA_CHUNK_SIZE)
1776*4882a593Smuzhiyun break;
1777*4882a593Smuzhiyun if (!xa_is_sibling(xas.xa_node->slots[slot]))
1778*4882a593Smuzhiyun break;
1779*4882a593Smuzhiyun order++;
1780*4882a593Smuzhiyun }
1781*4882a593Smuzhiyun
1782*4882a593Smuzhiyun order += xas.xa_node->shift;
1783*4882a593Smuzhiyun unlock:
1784*4882a593Smuzhiyun rcu_read_unlock();
1785*4882a593Smuzhiyun
1786*4882a593Smuzhiyun return order;
1787*4882a593Smuzhiyun }
1788*4882a593Smuzhiyun EXPORT_SYMBOL(xa_get_order);
1789*4882a593Smuzhiyun #endif /* CONFIG_XARRAY_MULTI */
1790*4882a593Smuzhiyun
1791*4882a593Smuzhiyun /**
1792*4882a593Smuzhiyun * __xa_alloc() - Find somewhere to store this entry in the XArray.
1793*4882a593Smuzhiyun * @xa: XArray.
1794*4882a593Smuzhiyun * @id: Pointer to ID.
1795*4882a593Smuzhiyun * @limit: Range for allocated ID.
1796*4882a593Smuzhiyun * @entry: New entry.
1797*4882a593Smuzhiyun * @gfp: Memory allocation flags.
1798*4882a593Smuzhiyun *
1799*4882a593Smuzhiyun * Finds an empty entry in @xa between @limit.min and @limit.max,
1800*4882a593Smuzhiyun * stores the index into the @id pointer, then stores the entry at
1801*4882a593Smuzhiyun * that index. A concurrent lookup will not see an uninitialised @id.
1802*4882a593Smuzhiyun *
1803*4882a593Smuzhiyun * Context: Any context. Expects xa_lock to be held on entry. May
1804*4882a593Smuzhiyun * release and reacquire xa_lock if @gfp flags permit.
1805*4882a593Smuzhiyun * Return: 0 on success, -ENOMEM if memory could not be allocated or
1806*4882a593Smuzhiyun * -EBUSY if there are no free entries in @limit.
1807*4882a593Smuzhiyun */
__xa_alloc(struct xarray * xa,u32 * id,void * entry,struct xa_limit limit,gfp_t gfp)1808*4882a593Smuzhiyun int __xa_alloc(struct xarray *xa, u32 *id, void *entry,
1809*4882a593Smuzhiyun struct xa_limit limit, gfp_t gfp)
1810*4882a593Smuzhiyun {
1811*4882a593Smuzhiyun XA_STATE(xas, xa, 0);
1812*4882a593Smuzhiyun
1813*4882a593Smuzhiyun if (WARN_ON_ONCE(xa_is_advanced(entry)))
1814*4882a593Smuzhiyun return -EINVAL;
1815*4882a593Smuzhiyun if (WARN_ON_ONCE(!xa_track_free(xa)))
1816*4882a593Smuzhiyun return -EINVAL;
1817*4882a593Smuzhiyun
1818*4882a593Smuzhiyun if (!entry)
1819*4882a593Smuzhiyun entry = XA_ZERO_ENTRY;
1820*4882a593Smuzhiyun
1821*4882a593Smuzhiyun do {
1822*4882a593Smuzhiyun xas.xa_index = limit.min;
1823*4882a593Smuzhiyun xas_find_marked(&xas, limit.max, XA_FREE_MARK);
1824*4882a593Smuzhiyun if (xas.xa_node == XAS_RESTART)
1825*4882a593Smuzhiyun xas_set_err(&xas, -EBUSY);
1826*4882a593Smuzhiyun else
1827*4882a593Smuzhiyun *id = xas.xa_index;
1828*4882a593Smuzhiyun xas_store(&xas, entry);
1829*4882a593Smuzhiyun xas_clear_mark(&xas, XA_FREE_MARK);
1830*4882a593Smuzhiyun } while (__xas_nomem(&xas, gfp));
1831*4882a593Smuzhiyun
1832*4882a593Smuzhiyun return xas_error(&xas);
1833*4882a593Smuzhiyun }
1834*4882a593Smuzhiyun EXPORT_SYMBOL(__xa_alloc);
1835*4882a593Smuzhiyun
1836*4882a593Smuzhiyun /**
1837*4882a593Smuzhiyun * __xa_alloc_cyclic() - Find somewhere to store this entry in the XArray.
1838*4882a593Smuzhiyun * @xa: XArray.
1839*4882a593Smuzhiyun * @id: Pointer to ID.
1840*4882a593Smuzhiyun * @entry: New entry.
1841*4882a593Smuzhiyun * @limit: Range of allocated ID.
1842*4882a593Smuzhiyun * @next: Pointer to next ID to allocate.
1843*4882a593Smuzhiyun * @gfp: Memory allocation flags.
1844*4882a593Smuzhiyun *
1845*4882a593Smuzhiyun * Finds an empty entry in @xa between @limit.min and @limit.max,
1846*4882a593Smuzhiyun * stores the index into the @id pointer, then stores the entry at
1847*4882a593Smuzhiyun * that index. A concurrent lookup will not see an uninitialised @id.
1848*4882a593Smuzhiyun * The search for an empty entry will start at @next and will wrap
1849*4882a593Smuzhiyun * around if necessary.
1850*4882a593Smuzhiyun *
1851*4882a593Smuzhiyun * Context: Any context. Expects xa_lock to be held on entry. May
1852*4882a593Smuzhiyun * release and reacquire xa_lock if @gfp flags permit.
1853*4882a593Smuzhiyun * Return: 0 if the allocation succeeded without wrapping. 1 if the
1854*4882a593Smuzhiyun * allocation succeeded after wrapping, -ENOMEM if memory could not be
1855*4882a593Smuzhiyun * allocated or -EBUSY if there are no free entries in @limit.
1856*4882a593Smuzhiyun */
__xa_alloc_cyclic(struct xarray * xa,u32 * id,void * entry,struct xa_limit limit,u32 * next,gfp_t gfp)1857*4882a593Smuzhiyun int __xa_alloc_cyclic(struct xarray *xa, u32 *id, void *entry,
1858*4882a593Smuzhiyun struct xa_limit limit, u32 *next, gfp_t gfp)
1859*4882a593Smuzhiyun {
1860*4882a593Smuzhiyun u32 min = limit.min;
1861*4882a593Smuzhiyun int ret;
1862*4882a593Smuzhiyun
1863*4882a593Smuzhiyun limit.min = max(min, *next);
1864*4882a593Smuzhiyun ret = __xa_alloc(xa, id, entry, limit, gfp);
1865*4882a593Smuzhiyun if ((xa->xa_flags & XA_FLAGS_ALLOC_WRAPPED) && ret == 0) {
1866*4882a593Smuzhiyun xa->xa_flags &= ~XA_FLAGS_ALLOC_WRAPPED;
1867*4882a593Smuzhiyun ret = 1;
1868*4882a593Smuzhiyun }
1869*4882a593Smuzhiyun
1870*4882a593Smuzhiyun if (ret < 0 && limit.min > min) {
1871*4882a593Smuzhiyun limit.min = min;
1872*4882a593Smuzhiyun ret = __xa_alloc(xa, id, entry, limit, gfp);
1873*4882a593Smuzhiyun if (ret == 0)
1874*4882a593Smuzhiyun ret = 1;
1875*4882a593Smuzhiyun }
1876*4882a593Smuzhiyun
1877*4882a593Smuzhiyun if (ret >= 0) {
1878*4882a593Smuzhiyun *next = *id + 1;
1879*4882a593Smuzhiyun if (*next == 0)
1880*4882a593Smuzhiyun xa->xa_flags |= XA_FLAGS_ALLOC_WRAPPED;
1881*4882a593Smuzhiyun }
1882*4882a593Smuzhiyun return ret;
1883*4882a593Smuzhiyun }
1884*4882a593Smuzhiyun EXPORT_SYMBOL(__xa_alloc_cyclic);
1885*4882a593Smuzhiyun
1886*4882a593Smuzhiyun /**
1887*4882a593Smuzhiyun * __xa_set_mark() - Set this mark on this entry while locked.
1888*4882a593Smuzhiyun * @xa: XArray.
1889*4882a593Smuzhiyun * @index: Index of entry.
1890*4882a593Smuzhiyun * @mark: Mark number.
1891*4882a593Smuzhiyun *
1892*4882a593Smuzhiyun * Attempting to set a mark on a %NULL entry does not succeed.
1893*4882a593Smuzhiyun *
1894*4882a593Smuzhiyun * Context: Any context. Expects xa_lock to be held on entry.
1895*4882a593Smuzhiyun */
__xa_set_mark(struct xarray * xa,unsigned long index,xa_mark_t mark)1896*4882a593Smuzhiyun void __xa_set_mark(struct xarray *xa, unsigned long index, xa_mark_t mark)
1897*4882a593Smuzhiyun {
1898*4882a593Smuzhiyun XA_STATE(xas, xa, index);
1899*4882a593Smuzhiyun void *entry = xas_load(&xas);
1900*4882a593Smuzhiyun
1901*4882a593Smuzhiyun if (entry)
1902*4882a593Smuzhiyun xas_set_mark(&xas, mark);
1903*4882a593Smuzhiyun }
1904*4882a593Smuzhiyun EXPORT_SYMBOL(__xa_set_mark);
1905*4882a593Smuzhiyun
1906*4882a593Smuzhiyun /**
1907*4882a593Smuzhiyun * __xa_clear_mark() - Clear this mark on this entry while locked.
1908*4882a593Smuzhiyun * @xa: XArray.
1909*4882a593Smuzhiyun * @index: Index of entry.
1910*4882a593Smuzhiyun * @mark: Mark number.
1911*4882a593Smuzhiyun *
1912*4882a593Smuzhiyun * Context: Any context. Expects xa_lock to be held on entry.
1913*4882a593Smuzhiyun */
__xa_clear_mark(struct xarray * xa,unsigned long index,xa_mark_t mark)1914*4882a593Smuzhiyun void __xa_clear_mark(struct xarray *xa, unsigned long index, xa_mark_t mark)
1915*4882a593Smuzhiyun {
1916*4882a593Smuzhiyun XA_STATE(xas, xa, index);
1917*4882a593Smuzhiyun void *entry = xas_load(&xas);
1918*4882a593Smuzhiyun
1919*4882a593Smuzhiyun if (entry)
1920*4882a593Smuzhiyun xas_clear_mark(&xas, mark);
1921*4882a593Smuzhiyun }
1922*4882a593Smuzhiyun EXPORT_SYMBOL(__xa_clear_mark);
1923*4882a593Smuzhiyun
1924*4882a593Smuzhiyun /**
1925*4882a593Smuzhiyun * xa_get_mark() - Inquire whether this mark is set on this entry.
1926*4882a593Smuzhiyun * @xa: XArray.
1927*4882a593Smuzhiyun * @index: Index of entry.
1928*4882a593Smuzhiyun * @mark: Mark number.
1929*4882a593Smuzhiyun *
1930*4882a593Smuzhiyun * This function uses the RCU read lock, so the result may be out of date
1931*4882a593Smuzhiyun * by the time it returns. If you need the result to be stable, use a lock.
1932*4882a593Smuzhiyun *
1933*4882a593Smuzhiyun * Context: Any context. Takes and releases the RCU lock.
1934*4882a593Smuzhiyun * Return: True if the entry at @index has this mark set, false if it doesn't.
1935*4882a593Smuzhiyun */
xa_get_mark(struct xarray * xa,unsigned long index,xa_mark_t mark)1936*4882a593Smuzhiyun bool xa_get_mark(struct xarray *xa, unsigned long index, xa_mark_t mark)
1937*4882a593Smuzhiyun {
1938*4882a593Smuzhiyun XA_STATE(xas, xa, index);
1939*4882a593Smuzhiyun void *entry;
1940*4882a593Smuzhiyun
1941*4882a593Smuzhiyun rcu_read_lock();
1942*4882a593Smuzhiyun entry = xas_start(&xas);
1943*4882a593Smuzhiyun while (xas_get_mark(&xas, mark)) {
1944*4882a593Smuzhiyun if (!xa_is_node(entry))
1945*4882a593Smuzhiyun goto found;
1946*4882a593Smuzhiyun entry = xas_descend(&xas, xa_to_node(entry));
1947*4882a593Smuzhiyun }
1948*4882a593Smuzhiyun rcu_read_unlock();
1949*4882a593Smuzhiyun return false;
1950*4882a593Smuzhiyun found:
1951*4882a593Smuzhiyun rcu_read_unlock();
1952*4882a593Smuzhiyun return true;
1953*4882a593Smuzhiyun }
1954*4882a593Smuzhiyun EXPORT_SYMBOL(xa_get_mark);
1955*4882a593Smuzhiyun
1956*4882a593Smuzhiyun /**
1957*4882a593Smuzhiyun * xa_set_mark() - Set this mark on this entry.
1958*4882a593Smuzhiyun * @xa: XArray.
1959*4882a593Smuzhiyun * @index: Index of entry.
1960*4882a593Smuzhiyun * @mark: Mark number.
1961*4882a593Smuzhiyun *
1962*4882a593Smuzhiyun * Attempting to set a mark on a %NULL entry does not succeed.
1963*4882a593Smuzhiyun *
1964*4882a593Smuzhiyun * Context: Process context. Takes and releases the xa_lock.
1965*4882a593Smuzhiyun */
xa_set_mark(struct xarray * xa,unsigned long index,xa_mark_t mark)1966*4882a593Smuzhiyun void xa_set_mark(struct xarray *xa, unsigned long index, xa_mark_t mark)
1967*4882a593Smuzhiyun {
1968*4882a593Smuzhiyun xa_lock(xa);
1969*4882a593Smuzhiyun __xa_set_mark(xa, index, mark);
1970*4882a593Smuzhiyun xa_unlock(xa);
1971*4882a593Smuzhiyun }
1972*4882a593Smuzhiyun EXPORT_SYMBOL(xa_set_mark);
1973*4882a593Smuzhiyun
1974*4882a593Smuzhiyun /**
1975*4882a593Smuzhiyun * xa_clear_mark() - Clear this mark on this entry.
1976*4882a593Smuzhiyun * @xa: XArray.
1977*4882a593Smuzhiyun * @index: Index of entry.
1978*4882a593Smuzhiyun * @mark: Mark number.
1979*4882a593Smuzhiyun *
1980*4882a593Smuzhiyun * Clearing a mark always succeeds.
1981*4882a593Smuzhiyun *
1982*4882a593Smuzhiyun * Context: Process context. Takes and releases the xa_lock.
1983*4882a593Smuzhiyun */
xa_clear_mark(struct xarray * xa,unsigned long index,xa_mark_t mark)1984*4882a593Smuzhiyun void xa_clear_mark(struct xarray *xa, unsigned long index, xa_mark_t mark)
1985*4882a593Smuzhiyun {
1986*4882a593Smuzhiyun xa_lock(xa);
1987*4882a593Smuzhiyun __xa_clear_mark(xa, index, mark);
1988*4882a593Smuzhiyun xa_unlock(xa);
1989*4882a593Smuzhiyun }
1990*4882a593Smuzhiyun EXPORT_SYMBOL(xa_clear_mark);
1991*4882a593Smuzhiyun
1992*4882a593Smuzhiyun /**
1993*4882a593Smuzhiyun * xa_find() - Search the XArray for an entry.
1994*4882a593Smuzhiyun * @xa: XArray.
1995*4882a593Smuzhiyun * @indexp: Pointer to an index.
1996*4882a593Smuzhiyun * @max: Maximum index to search to.
1997*4882a593Smuzhiyun * @filter: Selection criterion.
1998*4882a593Smuzhiyun *
1999*4882a593Smuzhiyun * Finds the entry in @xa which matches the @filter, and has the lowest
2000*4882a593Smuzhiyun * index that is at least @indexp and no more than @max.
2001*4882a593Smuzhiyun * If an entry is found, @indexp is updated to be the index of the entry.
2002*4882a593Smuzhiyun * This function is protected by the RCU read lock, so it may not find
2003*4882a593Smuzhiyun * entries which are being simultaneously added. It will not return an
2004*4882a593Smuzhiyun * %XA_RETRY_ENTRY; if you need to see retry entries, use xas_find().
2005*4882a593Smuzhiyun *
2006*4882a593Smuzhiyun * Context: Any context. Takes and releases the RCU lock.
2007*4882a593Smuzhiyun * Return: The entry, if found, otherwise %NULL.
2008*4882a593Smuzhiyun */
xa_find(struct xarray * xa,unsigned long * indexp,unsigned long max,xa_mark_t filter)2009*4882a593Smuzhiyun void *xa_find(struct xarray *xa, unsigned long *indexp,
2010*4882a593Smuzhiyun unsigned long max, xa_mark_t filter)
2011*4882a593Smuzhiyun {
2012*4882a593Smuzhiyun XA_STATE(xas, xa, *indexp);
2013*4882a593Smuzhiyun void *entry;
2014*4882a593Smuzhiyun
2015*4882a593Smuzhiyun rcu_read_lock();
2016*4882a593Smuzhiyun do {
2017*4882a593Smuzhiyun if ((__force unsigned int)filter < XA_MAX_MARKS)
2018*4882a593Smuzhiyun entry = xas_find_marked(&xas, max, filter);
2019*4882a593Smuzhiyun else
2020*4882a593Smuzhiyun entry = xas_find(&xas, max);
2021*4882a593Smuzhiyun } while (xas_retry(&xas, entry));
2022*4882a593Smuzhiyun rcu_read_unlock();
2023*4882a593Smuzhiyun
2024*4882a593Smuzhiyun if (entry)
2025*4882a593Smuzhiyun *indexp = xas.xa_index;
2026*4882a593Smuzhiyun return entry;
2027*4882a593Smuzhiyun }
2028*4882a593Smuzhiyun EXPORT_SYMBOL(xa_find);
2029*4882a593Smuzhiyun
xas_sibling(struct xa_state * xas)2030*4882a593Smuzhiyun static bool xas_sibling(struct xa_state *xas)
2031*4882a593Smuzhiyun {
2032*4882a593Smuzhiyun struct xa_node *node = xas->xa_node;
2033*4882a593Smuzhiyun unsigned long mask;
2034*4882a593Smuzhiyun
2035*4882a593Smuzhiyun if (!IS_ENABLED(CONFIG_XARRAY_MULTI) || !node)
2036*4882a593Smuzhiyun return false;
2037*4882a593Smuzhiyun mask = (XA_CHUNK_SIZE << node->shift) - 1;
2038*4882a593Smuzhiyun return (xas->xa_index & mask) >
2039*4882a593Smuzhiyun ((unsigned long)xas->xa_offset << node->shift);
2040*4882a593Smuzhiyun }
2041*4882a593Smuzhiyun
2042*4882a593Smuzhiyun /**
2043*4882a593Smuzhiyun * xa_find_after() - Search the XArray for a present entry.
2044*4882a593Smuzhiyun * @xa: XArray.
2045*4882a593Smuzhiyun * @indexp: Pointer to an index.
2046*4882a593Smuzhiyun * @max: Maximum index to search to.
2047*4882a593Smuzhiyun * @filter: Selection criterion.
2048*4882a593Smuzhiyun *
2049*4882a593Smuzhiyun * Finds the entry in @xa which matches the @filter and has the lowest
2050*4882a593Smuzhiyun * index that is above @indexp and no more than @max.
2051*4882a593Smuzhiyun * If an entry is found, @indexp is updated to be the index of the entry.
2052*4882a593Smuzhiyun * This function is protected by the RCU read lock, so it may miss entries
2053*4882a593Smuzhiyun * which are being simultaneously added. It will not return an
2054*4882a593Smuzhiyun * %XA_RETRY_ENTRY; if you need to see retry entries, use xas_find().
2055*4882a593Smuzhiyun *
2056*4882a593Smuzhiyun * Context: Any context. Takes and releases the RCU lock.
2057*4882a593Smuzhiyun * Return: The pointer, if found, otherwise %NULL.
2058*4882a593Smuzhiyun */
xa_find_after(struct xarray * xa,unsigned long * indexp,unsigned long max,xa_mark_t filter)2059*4882a593Smuzhiyun void *xa_find_after(struct xarray *xa, unsigned long *indexp,
2060*4882a593Smuzhiyun unsigned long max, xa_mark_t filter)
2061*4882a593Smuzhiyun {
2062*4882a593Smuzhiyun XA_STATE(xas, xa, *indexp + 1);
2063*4882a593Smuzhiyun void *entry;
2064*4882a593Smuzhiyun
2065*4882a593Smuzhiyun if (xas.xa_index == 0)
2066*4882a593Smuzhiyun return NULL;
2067*4882a593Smuzhiyun
2068*4882a593Smuzhiyun rcu_read_lock();
2069*4882a593Smuzhiyun for (;;) {
2070*4882a593Smuzhiyun if ((__force unsigned int)filter < XA_MAX_MARKS)
2071*4882a593Smuzhiyun entry = xas_find_marked(&xas, max, filter);
2072*4882a593Smuzhiyun else
2073*4882a593Smuzhiyun entry = xas_find(&xas, max);
2074*4882a593Smuzhiyun
2075*4882a593Smuzhiyun if (xas_invalid(&xas))
2076*4882a593Smuzhiyun break;
2077*4882a593Smuzhiyun if (xas_sibling(&xas))
2078*4882a593Smuzhiyun continue;
2079*4882a593Smuzhiyun if (!xas_retry(&xas, entry))
2080*4882a593Smuzhiyun break;
2081*4882a593Smuzhiyun }
2082*4882a593Smuzhiyun rcu_read_unlock();
2083*4882a593Smuzhiyun
2084*4882a593Smuzhiyun if (entry)
2085*4882a593Smuzhiyun *indexp = xas.xa_index;
2086*4882a593Smuzhiyun return entry;
2087*4882a593Smuzhiyun }
2088*4882a593Smuzhiyun EXPORT_SYMBOL(xa_find_after);
2089*4882a593Smuzhiyun
xas_extract_present(struct xa_state * xas,void ** dst,unsigned long max,unsigned int n)2090*4882a593Smuzhiyun static unsigned int xas_extract_present(struct xa_state *xas, void **dst,
2091*4882a593Smuzhiyun unsigned long max, unsigned int n)
2092*4882a593Smuzhiyun {
2093*4882a593Smuzhiyun void *entry;
2094*4882a593Smuzhiyun unsigned int i = 0;
2095*4882a593Smuzhiyun
2096*4882a593Smuzhiyun rcu_read_lock();
2097*4882a593Smuzhiyun xas_for_each(xas, entry, max) {
2098*4882a593Smuzhiyun if (xas_retry(xas, entry))
2099*4882a593Smuzhiyun continue;
2100*4882a593Smuzhiyun dst[i++] = entry;
2101*4882a593Smuzhiyun if (i == n)
2102*4882a593Smuzhiyun break;
2103*4882a593Smuzhiyun }
2104*4882a593Smuzhiyun rcu_read_unlock();
2105*4882a593Smuzhiyun
2106*4882a593Smuzhiyun return i;
2107*4882a593Smuzhiyun }
2108*4882a593Smuzhiyun
xas_extract_marked(struct xa_state * xas,void ** dst,unsigned long max,unsigned int n,xa_mark_t mark)2109*4882a593Smuzhiyun static unsigned int xas_extract_marked(struct xa_state *xas, void **dst,
2110*4882a593Smuzhiyun unsigned long max, unsigned int n, xa_mark_t mark)
2111*4882a593Smuzhiyun {
2112*4882a593Smuzhiyun void *entry;
2113*4882a593Smuzhiyun unsigned int i = 0;
2114*4882a593Smuzhiyun
2115*4882a593Smuzhiyun rcu_read_lock();
2116*4882a593Smuzhiyun xas_for_each_marked(xas, entry, max, mark) {
2117*4882a593Smuzhiyun if (xas_retry(xas, entry))
2118*4882a593Smuzhiyun continue;
2119*4882a593Smuzhiyun dst[i++] = entry;
2120*4882a593Smuzhiyun if (i == n)
2121*4882a593Smuzhiyun break;
2122*4882a593Smuzhiyun }
2123*4882a593Smuzhiyun rcu_read_unlock();
2124*4882a593Smuzhiyun
2125*4882a593Smuzhiyun return i;
2126*4882a593Smuzhiyun }
2127*4882a593Smuzhiyun
2128*4882a593Smuzhiyun /**
2129*4882a593Smuzhiyun * xa_extract() - Copy selected entries from the XArray into a normal array.
2130*4882a593Smuzhiyun * @xa: The source XArray to copy from.
2131*4882a593Smuzhiyun * @dst: The buffer to copy entries into.
2132*4882a593Smuzhiyun * @start: The first index in the XArray eligible to be selected.
2133*4882a593Smuzhiyun * @max: The last index in the XArray eligible to be selected.
2134*4882a593Smuzhiyun * @n: The maximum number of entries to copy.
2135*4882a593Smuzhiyun * @filter: Selection criterion.
2136*4882a593Smuzhiyun *
2137*4882a593Smuzhiyun * Copies up to @n entries that match @filter from the XArray. The
2138*4882a593Smuzhiyun * copied entries will have indices between @start and @max, inclusive.
2139*4882a593Smuzhiyun *
2140*4882a593Smuzhiyun * The @filter may be an XArray mark value, in which case entries which are
2141*4882a593Smuzhiyun * marked with that mark will be copied. It may also be %XA_PRESENT, in
2142*4882a593Smuzhiyun * which case all entries which are not %NULL will be copied.
2143*4882a593Smuzhiyun *
2144*4882a593Smuzhiyun * The entries returned may not represent a snapshot of the XArray at a
2145*4882a593Smuzhiyun * moment in time. For example, if another thread stores to index 5, then
2146*4882a593Smuzhiyun * index 10, calling xa_extract() may return the old contents of index 5
2147*4882a593Smuzhiyun * and the new contents of index 10. Indices not modified while this
2148*4882a593Smuzhiyun * function is running will not be skipped.
2149*4882a593Smuzhiyun *
2150*4882a593Smuzhiyun * If you need stronger guarantees, holding the xa_lock across calls to this
2151*4882a593Smuzhiyun * function will prevent concurrent modification.
2152*4882a593Smuzhiyun *
2153*4882a593Smuzhiyun * Context: Any context. Takes and releases the RCU lock.
2154*4882a593Smuzhiyun * Return: The number of entries copied.
2155*4882a593Smuzhiyun */
xa_extract(struct xarray * xa,void ** dst,unsigned long start,unsigned long max,unsigned int n,xa_mark_t filter)2156*4882a593Smuzhiyun unsigned int xa_extract(struct xarray *xa, void **dst, unsigned long start,
2157*4882a593Smuzhiyun unsigned long max, unsigned int n, xa_mark_t filter)
2158*4882a593Smuzhiyun {
2159*4882a593Smuzhiyun XA_STATE(xas, xa, start);
2160*4882a593Smuzhiyun
2161*4882a593Smuzhiyun if (!n)
2162*4882a593Smuzhiyun return 0;
2163*4882a593Smuzhiyun
2164*4882a593Smuzhiyun if ((__force unsigned int)filter < XA_MAX_MARKS)
2165*4882a593Smuzhiyun return xas_extract_marked(&xas, dst, max, n, filter);
2166*4882a593Smuzhiyun return xas_extract_present(&xas, dst, max, n);
2167*4882a593Smuzhiyun }
2168*4882a593Smuzhiyun EXPORT_SYMBOL(xa_extract);
2169*4882a593Smuzhiyun
2170*4882a593Smuzhiyun /**
2171*4882a593Smuzhiyun * xa_delete_node() - Private interface for workingset code.
2172*4882a593Smuzhiyun * @node: Node to be removed from the tree.
2173*4882a593Smuzhiyun * @update: Function to call to update ancestor nodes.
2174*4882a593Smuzhiyun *
2175*4882a593Smuzhiyun * Context: xa_lock must be held on entry and will not be released.
2176*4882a593Smuzhiyun */
xa_delete_node(struct xa_node * node,xa_update_node_t update)2177*4882a593Smuzhiyun void xa_delete_node(struct xa_node *node, xa_update_node_t update)
2178*4882a593Smuzhiyun {
2179*4882a593Smuzhiyun struct xa_state xas = {
2180*4882a593Smuzhiyun .xa = node->array,
2181*4882a593Smuzhiyun .xa_index = (unsigned long)node->offset <<
2182*4882a593Smuzhiyun (node->shift + XA_CHUNK_SHIFT),
2183*4882a593Smuzhiyun .xa_shift = node->shift + XA_CHUNK_SHIFT,
2184*4882a593Smuzhiyun .xa_offset = node->offset,
2185*4882a593Smuzhiyun .xa_node = xa_parent_locked(node->array, node),
2186*4882a593Smuzhiyun .xa_update = update,
2187*4882a593Smuzhiyun };
2188*4882a593Smuzhiyun
2189*4882a593Smuzhiyun xas_store(&xas, NULL);
2190*4882a593Smuzhiyun }
2191*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(xa_delete_node); /* For the benefit of the test suite */
2192*4882a593Smuzhiyun
2193*4882a593Smuzhiyun /**
2194*4882a593Smuzhiyun * xa_destroy() - Free all internal data structures.
2195*4882a593Smuzhiyun * @xa: XArray.
2196*4882a593Smuzhiyun *
2197*4882a593Smuzhiyun * After calling this function, the XArray is empty and has freed all memory
2198*4882a593Smuzhiyun * allocated for its internal data structures. You are responsible for
2199*4882a593Smuzhiyun * freeing the objects referenced by the XArray.
2200*4882a593Smuzhiyun *
2201*4882a593Smuzhiyun * Context: Any context. Takes and releases the xa_lock, interrupt-safe.
2202*4882a593Smuzhiyun */
xa_destroy(struct xarray * xa)2203*4882a593Smuzhiyun void xa_destroy(struct xarray *xa)
2204*4882a593Smuzhiyun {
2205*4882a593Smuzhiyun XA_STATE(xas, xa, 0);
2206*4882a593Smuzhiyun unsigned long flags;
2207*4882a593Smuzhiyun void *entry;
2208*4882a593Smuzhiyun
2209*4882a593Smuzhiyun xas.xa_node = NULL;
2210*4882a593Smuzhiyun xas_lock_irqsave(&xas, flags);
2211*4882a593Smuzhiyun entry = xa_head_locked(xa);
2212*4882a593Smuzhiyun RCU_INIT_POINTER(xa->xa_head, NULL);
2213*4882a593Smuzhiyun xas_init_marks(&xas);
2214*4882a593Smuzhiyun if (xa_zero_busy(xa))
2215*4882a593Smuzhiyun xa_mark_clear(xa, XA_FREE_MARK);
2216*4882a593Smuzhiyun /* lockdep checks we're still holding the lock in xas_free_nodes() */
2217*4882a593Smuzhiyun if (xa_is_node(entry))
2218*4882a593Smuzhiyun xas_free_nodes(&xas, xa_to_node(entry));
2219*4882a593Smuzhiyun xas_unlock_irqrestore(&xas, flags);
2220*4882a593Smuzhiyun }
2221*4882a593Smuzhiyun EXPORT_SYMBOL(xa_destroy);
2222*4882a593Smuzhiyun
2223*4882a593Smuzhiyun #ifdef XA_DEBUG
xa_dump_node(const struct xa_node * node)2224*4882a593Smuzhiyun void xa_dump_node(const struct xa_node *node)
2225*4882a593Smuzhiyun {
2226*4882a593Smuzhiyun unsigned i, j;
2227*4882a593Smuzhiyun
2228*4882a593Smuzhiyun if (!node)
2229*4882a593Smuzhiyun return;
2230*4882a593Smuzhiyun if ((unsigned long)node & 3) {
2231*4882a593Smuzhiyun pr_cont("node %px\n", node);
2232*4882a593Smuzhiyun return;
2233*4882a593Smuzhiyun }
2234*4882a593Smuzhiyun
2235*4882a593Smuzhiyun pr_cont("node %px %s %d parent %px shift %d count %d values %d "
2236*4882a593Smuzhiyun "array %px list %px %px marks",
2237*4882a593Smuzhiyun node, node->parent ? "offset" : "max", node->offset,
2238*4882a593Smuzhiyun node->parent, node->shift, node->count, node->nr_values,
2239*4882a593Smuzhiyun node->array, node->private_list.prev, node->private_list.next);
2240*4882a593Smuzhiyun for (i = 0; i < XA_MAX_MARKS; i++)
2241*4882a593Smuzhiyun for (j = 0; j < XA_MARK_LONGS; j++)
2242*4882a593Smuzhiyun pr_cont(" %lx", node->marks[i][j]);
2243*4882a593Smuzhiyun pr_cont("\n");
2244*4882a593Smuzhiyun }
2245*4882a593Smuzhiyun
xa_dump_index(unsigned long index,unsigned int shift)2246*4882a593Smuzhiyun void xa_dump_index(unsigned long index, unsigned int shift)
2247*4882a593Smuzhiyun {
2248*4882a593Smuzhiyun if (!shift)
2249*4882a593Smuzhiyun pr_info("%lu: ", index);
2250*4882a593Smuzhiyun else if (shift >= BITS_PER_LONG)
2251*4882a593Smuzhiyun pr_info("0-%lu: ", ~0UL);
2252*4882a593Smuzhiyun else
2253*4882a593Smuzhiyun pr_info("%lu-%lu: ", index, index | ((1UL << shift) - 1));
2254*4882a593Smuzhiyun }
2255*4882a593Smuzhiyun
xa_dump_entry(const void * entry,unsigned long index,unsigned long shift)2256*4882a593Smuzhiyun void xa_dump_entry(const void *entry, unsigned long index, unsigned long shift)
2257*4882a593Smuzhiyun {
2258*4882a593Smuzhiyun if (!entry)
2259*4882a593Smuzhiyun return;
2260*4882a593Smuzhiyun
2261*4882a593Smuzhiyun xa_dump_index(index, shift);
2262*4882a593Smuzhiyun
2263*4882a593Smuzhiyun if (xa_is_node(entry)) {
2264*4882a593Smuzhiyun if (shift == 0) {
2265*4882a593Smuzhiyun pr_cont("%px\n", entry);
2266*4882a593Smuzhiyun } else {
2267*4882a593Smuzhiyun unsigned long i;
2268*4882a593Smuzhiyun struct xa_node *node = xa_to_node(entry);
2269*4882a593Smuzhiyun xa_dump_node(node);
2270*4882a593Smuzhiyun for (i = 0; i < XA_CHUNK_SIZE; i++)
2271*4882a593Smuzhiyun xa_dump_entry(node->slots[i],
2272*4882a593Smuzhiyun index + (i << node->shift), node->shift);
2273*4882a593Smuzhiyun }
2274*4882a593Smuzhiyun } else if (xa_is_value(entry))
2275*4882a593Smuzhiyun pr_cont("value %ld (0x%lx) [%px]\n", xa_to_value(entry),
2276*4882a593Smuzhiyun xa_to_value(entry), entry);
2277*4882a593Smuzhiyun else if (!xa_is_internal(entry))
2278*4882a593Smuzhiyun pr_cont("%px\n", entry);
2279*4882a593Smuzhiyun else if (xa_is_retry(entry))
2280*4882a593Smuzhiyun pr_cont("retry (%ld)\n", xa_to_internal(entry));
2281*4882a593Smuzhiyun else if (xa_is_sibling(entry))
2282*4882a593Smuzhiyun pr_cont("sibling (slot %ld)\n", xa_to_sibling(entry));
2283*4882a593Smuzhiyun else if (xa_is_zero(entry))
2284*4882a593Smuzhiyun pr_cont("zero (%ld)\n", xa_to_internal(entry));
2285*4882a593Smuzhiyun else
2286*4882a593Smuzhiyun pr_cont("UNKNOWN ENTRY (%px)\n", entry);
2287*4882a593Smuzhiyun }
2288*4882a593Smuzhiyun
xa_dump(const struct xarray * xa)2289*4882a593Smuzhiyun void xa_dump(const struct xarray *xa)
2290*4882a593Smuzhiyun {
2291*4882a593Smuzhiyun void *entry = xa->xa_head;
2292*4882a593Smuzhiyun unsigned int shift = 0;
2293*4882a593Smuzhiyun
2294*4882a593Smuzhiyun pr_info("xarray: %px head %px flags %x marks %d %d %d\n", xa, entry,
2295*4882a593Smuzhiyun xa->xa_flags, xa_marked(xa, XA_MARK_0),
2296*4882a593Smuzhiyun xa_marked(xa, XA_MARK_1), xa_marked(xa, XA_MARK_2));
2297*4882a593Smuzhiyun if (xa_is_node(entry))
2298*4882a593Smuzhiyun shift = xa_to_node(entry)->shift + XA_CHUNK_SHIFT;
2299*4882a593Smuzhiyun xa_dump_entry(entry, 0, shift);
2300*4882a593Smuzhiyun }
2301*4882a593Smuzhiyun #endif
2302