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