blob: 9a0d49d4b5f07177088c67d1c538771e67662a0e [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);
235 }
236 return entry;
237}
238EXPORT_SYMBOL_GPL(xas_load);
239
Matthew Wilcox58d6ea32017-11-10 15:15:08 -0500240/* Move the radix tree node cache here */
241extern struct kmem_cache *radix_tree_node_cachep;
242extern void radix_tree_node_rcu_free(struct rcu_head *head);
243
244#define XA_RCU_FREE ((struct xarray *)1)
245
246static void xa_node_free(struct xa_node *node)
247{
248 XA_NODE_BUG_ON(node, !list_empty(&node->private_list));
249 node->array = XA_RCU_FREE;
250 call_rcu(&node->rcu_head, radix_tree_node_rcu_free);
251}
252
253/*
254 * xas_destroy() - Free any resources allocated during the XArray operation.
255 * @xas: XArray operation state.
256 *
257 * This function is now internal-only.
258 */
259static void xas_destroy(struct xa_state *xas)
260{
261 struct xa_node *node = xas->xa_alloc;
262
263 if (!node)
264 return;
265 XA_NODE_BUG_ON(node, !list_empty(&node->private_list));
266 kmem_cache_free(radix_tree_node_cachep, node);
267 xas->xa_alloc = NULL;
268}
269
270/**
271 * xas_nomem() - Allocate memory if needed.
272 * @xas: XArray operation state.
273 * @gfp: Memory allocation flags.
274 *
275 * If we need to add new nodes to the XArray, we try to allocate memory
276 * with GFP_NOWAIT while holding the lock, which will usually succeed.
277 * If it fails, @xas is flagged as needing memory to continue. The caller
278 * should drop the lock and call xas_nomem(). If xas_nomem() succeeds,
279 * the caller should retry the operation.
280 *
281 * Forward progress is guaranteed as one node is allocated here and
282 * stored in the xa_state where it will be found by xas_alloc(). More
283 * nodes will likely be found in the slab allocator, but we do not tie
284 * them up here.
285 *
286 * Return: true if memory was needed, and was successfully allocated.
287 */
288bool xas_nomem(struct xa_state *xas, gfp_t gfp)
289{
290 if (xas->xa_node != XA_ERROR(-ENOMEM)) {
291 xas_destroy(xas);
292 return false;
293 }
294 xas->xa_alloc = kmem_cache_alloc(radix_tree_node_cachep, gfp);
295 if (!xas->xa_alloc)
296 return false;
297 XA_NODE_BUG_ON(xas->xa_alloc, !list_empty(&xas->xa_alloc->private_list));
298 xas->xa_node = XAS_RESTART;
299 return true;
300}
301EXPORT_SYMBOL_GPL(xas_nomem);
302
303/*
304 * __xas_nomem() - Drop locks and allocate memory if needed.
305 * @xas: XArray operation state.
306 * @gfp: Memory allocation flags.
307 *
308 * Internal variant of xas_nomem().
309 *
310 * Return: true if memory was needed, and was successfully allocated.
311 */
312static bool __xas_nomem(struct xa_state *xas, gfp_t gfp)
313 __must_hold(xas->xa->xa_lock)
314{
315 unsigned int lock_type = xa_lock_type(xas->xa);
316
317 if (xas->xa_node != XA_ERROR(-ENOMEM)) {
318 xas_destroy(xas);
319 return false;
320 }
321 if (gfpflags_allow_blocking(gfp)) {
322 xas_unlock_type(xas, lock_type);
323 xas->xa_alloc = kmem_cache_alloc(radix_tree_node_cachep, gfp);
324 xas_lock_type(xas, lock_type);
325 } else {
326 xas->xa_alloc = kmem_cache_alloc(radix_tree_node_cachep, gfp);
327 }
328 if (!xas->xa_alloc)
329 return false;
330 XA_NODE_BUG_ON(xas->xa_alloc, !list_empty(&xas->xa_alloc->private_list));
331 xas->xa_node = XAS_RESTART;
332 return true;
333}
334
335static void xas_update(struct xa_state *xas, struct xa_node *node)
336{
337 if (xas->xa_update)
338 xas->xa_update(node);
339 else
340 XA_NODE_BUG_ON(node, !list_empty(&node->private_list));
341}
342
343static void *xas_alloc(struct xa_state *xas, unsigned int shift)
344{
345 struct xa_node *parent = xas->xa_node;
346 struct xa_node *node = xas->xa_alloc;
347
348 if (xas_invalid(xas))
349 return NULL;
350
351 if (node) {
352 xas->xa_alloc = NULL;
353 } else {
354 node = kmem_cache_alloc(radix_tree_node_cachep,
355 GFP_NOWAIT | __GFP_NOWARN);
356 if (!node) {
357 xas_set_err(xas, -ENOMEM);
358 return NULL;
359 }
360 }
361
362 if (parent) {
363 node->offset = xas->xa_offset;
364 parent->count++;
365 XA_NODE_BUG_ON(node, parent->count > XA_CHUNK_SIZE);
366 xas_update(xas, parent);
367 }
368 XA_NODE_BUG_ON(node, shift > BITS_PER_LONG);
369 XA_NODE_BUG_ON(node, !list_empty(&node->private_list));
370 node->shift = shift;
371 node->count = 0;
372 node->nr_values = 0;
373 RCU_INIT_POINTER(node->parent, xas->xa_node);
374 node->array = xas->xa;
375
376 return node;
377}
378
379/*
380 * Use this to calculate the maximum index that will need to be created
381 * in order to add the entry described by @xas. Because we cannot store a
382 * multiple-index entry at index 0, the calculation is a little more complex
383 * than you might expect.
384 */
385static unsigned long xas_max(struct xa_state *xas)
386{
387 unsigned long max = xas->xa_index;
388
389#ifdef CONFIG_XARRAY_MULTI
390 if (xas->xa_shift || xas->xa_sibs) {
391 unsigned long mask;
392 mask = (((xas->xa_sibs + 1UL) << xas->xa_shift) - 1);
393 max |= mask;
394 if (mask == max)
395 max++;
396 }
397#endif
398
399 return max;
400}
401
402/* The maximum index that can be contained in the array without expanding it */
403static unsigned long max_index(void *entry)
404{
405 if (!xa_is_node(entry))
406 return 0;
407 return (XA_CHUNK_SIZE << xa_to_node(entry)->shift) - 1;
408}
409
410static void xas_shrink(struct xa_state *xas)
411{
412 struct xarray *xa = xas->xa;
413 struct xa_node *node = xas->xa_node;
414
415 for (;;) {
416 void *entry;
417
418 XA_NODE_BUG_ON(node, node->count > XA_CHUNK_SIZE);
419 if (node->count != 1)
420 break;
421 entry = xa_entry_locked(xa, node, 0);
422 if (!entry)
423 break;
424 if (!xa_is_node(entry) && node->shift)
425 break;
426 xas->xa_node = XAS_BOUNDS;
427
428 RCU_INIT_POINTER(xa->xa_head, entry);
Matthew Wilcox371c7522018-07-04 10:50:12 -0400429 if (xa_track_free(xa) && !node_get_mark(node, 0, XA_FREE_MARK))
430 xa_mark_clear(xa, XA_FREE_MARK);
Matthew Wilcox58d6ea32017-11-10 15:15:08 -0500431
432 node->count = 0;
433 node->nr_values = 0;
434 if (!xa_is_node(entry))
435 RCU_INIT_POINTER(node->slots[0], XA_RETRY_ENTRY);
436 xas_update(xas, node);
437 xa_node_free(node);
438 if (!xa_is_node(entry))
439 break;
440 node = xa_to_node(entry);
441 node->parent = NULL;
442 }
443}
444
445/*
446 * xas_delete_node() - Attempt to delete an xa_node
447 * @xas: Array operation state.
448 *
449 * Attempts to delete the @xas->xa_node. This will fail if xa->node has
450 * a non-zero reference count.
451 */
452static void xas_delete_node(struct xa_state *xas)
453{
454 struct xa_node *node = xas->xa_node;
455
456 for (;;) {
457 struct xa_node *parent;
458
459 XA_NODE_BUG_ON(node, node->count > XA_CHUNK_SIZE);
460 if (node->count)
461 break;
462
463 parent = xa_parent_locked(xas->xa, node);
464 xas->xa_node = parent;
465 xas->xa_offset = node->offset;
466 xa_node_free(node);
467
468 if (!parent) {
469 xas->xa->xa_head = NULL;
470 xas->xa_node = XAS_BOUNDS;
471 return;
472 }
473
474 parent->slots[xas->xa_offset] = NULL;
475 parent->count--;
476 XA_NODE_BUG_ON(parent, parent->count > XA_CHUNK_SIZE);
477 node = parent;
478 xas_update(xas, node);
479 }
480
481 if (!node->parent)
482 xas_shrink(xas);
483}
484
485/**
486 * xas_free_nodes() - Free this node and all nodes that it references
487 * @xas: Array operation state.
488 * @top: Node to free
489 *
490 * This node has been removed from the tree. We must now free it and all
491 * of its subnodes. There may be RCU walkers with references into the tree,
492 * so we must replace all entries with retry markers.
493 */
494static void xas_free_nodes(struct xa_state *xas, struct xa_node *top)
495{
496 unsigned int offset = 0;
497 struct xa_node *node = top;
498
499 for (;;) {
500 void *entry = xa_entry_locked(xas->xa, node, offset);
501
502 if (xa_is_node(entry)) {
503 node = xa_to_node(entry);
504 offset = 0;
505 continue;
506 }
507 if (entry)
508 RCU_INIT_POINTER(node->slots[offset], XA_RETRY_ENTRY);
509 offset++;
510 while (offset == XA_CHUNK_SIZE) {
511 struct xa_node *parent;
512
513 parent = xa_parent_locked(xas->xa, node);
514 offset = node->offset + 1;
515 node->count = 0;
516 node->nr_values = 0;
517 xas_update(xas, node);
518 xa_node_free(node);
519 if (node == top)
520 return;
521 node = parent;
522 }
523 }
524}
525
526/*
527 * xas_expand adds nodes to the head of the tree until it has reached
528 * sufficient height to be able to contain @xas->xa_index
529 */
530static int xas_expand(struct xa_state *xas, void *head)
531{
532 struct xarray *xa = xas->xa;
533 struct xa_node *node = NULL;
534 unsigned int shift = 0;
535 unsigned long max = xas_max(xas);
536
537 if (!head) {
538 if (max == 0)
539 return 0;
540 while ((max >> shift) >= XA_CHUNK_SIZE)
541 shift += XA_CHUNK_SHIFT;
542 return shift + XA_CHUNK_SHIFT;
543 } else if (xa_is_node(head)) {
544 node = xa_to_node(head);
545 shift = node->shift + XA_CHUNK_SHIFT;
546 }
547 xas->xa_node = NULL;
548
549 while (max > max_index(head)) {
550 xa_mark_t mark = 0;
551
552 XA_NODE_BUG_ON(node, shift > BITS_PER_LONG);
553 node = xas_alloc(xas, shift);
554 if (!node)
555 return -ENOMEM;
556
557 node->count = 1;
558 if (xa_is_value(head))
559 node->nr_values = 1;
560 RCU_INIT_POINTER(node->slots[0], head);
561
562 /* Propagate the aggregated mark info to the new child */
563 for (;;) {
Matthew Wilcox371c7522018-07-04 10:50:12 -0400564 if (xa_track_free(xa) && mark == XA_FREE_MARK) {
565 node_mark_all(node, XA_FREE_MARK);
566 if (!xa_marked(xa, XA_FREE_MARK)) {
567 node_clear_mark(node, 0, XA_FREE_MARK);
568 xa_mark_set(xa, XA_FREE_MARK);
569 }
570 } else if (xa_marked(xa, mark)) {
Matthew Wilcox58d6ea32017-11-10 15:15:08 -0500571 node_set_mark(node, 0, mark);
Matthew Wilcox371c7522018-07-04 10:50:12 -0400572 }
Matthew Wilcox58d6ea32017-11-10 15:15:08 -0500573 if (mark == XA_MARK_MAX)
574 break;
575 mark_inc(mark);
576 }
577
578 /*
579 * Now that the new node is fully initialised, we can add
580 * it to the tree
581 */
582 if (xa_is_node(head)) {
583 xa_to_node(head)->offset = 0;
584 rcu_assign_pointer(xa_to_node(head)->parent, node);
585 }
586 head = xa_mk_node(node);
587 rcu_assign_pointer(xa->xa_head, head);
588 xas_update(xas, node);
589
590 shift += XA_CHUNK_SHIFT;
591 }
592
593 xas->xa_node = node;
594 return shift;
595}
596
597/*
598 * xas_create() - Create a slot to store an entry in.
599 * @xas: XArray operation state.
600 *
601 * Most users will not need to call this function directly, as it is called
602 * by xas_store(). It is useful for doing conditional store operations
603 * (see the xa_cmpxchg() implementation for an example).
604 *
605 * Return: If the slot already existed, returns the contents of this slot.
606 * If the slot was newly created, returns NULL. If it failed to create the
607 * slot, returns NULL and indicates the error in @xas.
608 */
609static void *xas_create(struct xa_state *xas)
610{
611 struct xarray *xa = xas->xa;
612 void *entry;
613 void __rcu **slot;
614 struct xa_node *node = xas->xa_node;
615 int shift;
616 unsigned int order = xas->xa_shift;
617
618 if (xas_top(node)) {
619 entry = xa_head_locked(xa);
620 xas->xa_node = NULL;
621 shift = xas_expand(xas, entry);
622 if (shift < 0)
623 return NULL;
624 entry = xa_head_locked(xa);
625 slot = &xa->xa_head;
626 } else if (xas_error(xas)) {
627 return NULL;
628 } else if (node) {
629 unsigned int offset = xas->xa_offset;
630
631 shift = node->shift;
632 entry = xa_entry_locked(xa, node, offset);
633 slot = &node->slots[offset];
634 } else {
635 shift = 0;
636 entry = xa_head_locked(xa);
637 slot = &xa->xa_head;
638 }
639
640 while (shift > order) {
641 shift -= XA_CHUNK_SHIFT;
642 if (!entry) {
643 node = xas_alloc(xas, shift);
644 if (!node)
645 break;
Matthew Wilcox371c7522018-07-04 10:50:12 -0400646 if (xa_track_free(xa))
647 node_mark_all(node, XA_FREE_MARK);
Matthew Wilcox58d6ea32017-11-10 15:15:08 -0500648 rcu_assign_pointer(*slot, xa_mk_node(node));
649 } else if (xa_is_node(entry)) {
650 node = xa_to_node(entry);
651 } else {
652 break;
653 }
654 entry = xas_descend(xas, node);
655 slot = &node->slots[xas->xa_offset];
656 }
657
658 return entry;
659}
660
Matthew Wilcox2264f512017-12-04 00:11:48 -0500661/**
662 * xas_create_range() - Ensure that stores to this range will succeed
663 * @xas: XArray operation state.
664 *
665 * Creates all of the slots in the range covered by @xas. Sets @xas to
666 * create single-index entries and positions it at the beginning of the
667 * range. This is for the benefit of users which have not yet been
668 * converted to use multi-index entries.
669 */
670void xas_create_range(struct xa_state *xas)
671{
672 unsigned long index = xas->xa_index;
673 unsigned char shift = xas->xa_shift;
674 unsigned char sibs = xas->xa_sibs;
675
676 xas->xa_index |= ((sibs + 1) << shift) - 1;
677 if (xas_is_node(xas) && xas->xa_node->shift == xas->xa_shift)
678 xas->xa_offset |= sibs;
679 xas->xa_shift = 0;
680 xas->xa_sibs = 0;
681
682 for (;;) {
683 xas_create(xas);
684 if (xas_error(xas))
685 goto restore;
686 if (xas->xa_index <= (index | XA_CHUNK_MASK))
687 goto success;
688 xas->xa_index -= XA_CHUNK_SIZE;
689
690 for (;;) {
691 struct xa_node *node = xas->xa_node;
692 xas->xa_node = xa_parent_locked(xas->xa, node);
693 xas->xa_offset = node->offset - 1;
694 if (node->offset != 0)
695 break;
696 }
697 }
698
699restore:
700 xas->xa_shift = shift;
701 xas->xa_sibs = sibs;
702 xas->xa_index = index;
703 return;
704success:
705 xas->xa_index = index;
706 if (xas->xa_node)
707 xas_set_offset(xas);
708}
709EXPORT_SYMBOL_GPL(xas_create_range);
710
Matthew Wilcox58d6ea32017-11-10 15:15:08 -0500711static void update_node(struct xa_state *xas, struct xa_node *node,
712 int count, int values)
713{
714 if (!node || (!count && !values))
715 return;
716
717 node->count += count;
718 node->nr_values += values;
719 XA_NODE_BUG_ON(node, node->count > XA_CHUNK_SIZE);
720 XA_NODE_BUG_ON(node, node->nr_values > XA_CHUNK_SIZE);
721 xas_update(xas, node);
722 if (count < 0)
723 xas_delete_node(xas);
724}
725
726/**
727 * xas_store() - Store this entry in the XArray.
728 * @xas: XArray operation state.
729 * @entry: New entry.
730 *
731 * If @xas is operating on a multi-index entry, the entry returned by this
732 * function is essentially meaningless (it may be an internal entry or it
733 * may be %NULL, even if there are non-NULL entries at some of the indices
734 * covered by the range). This is not a problem for any current users,
735 * and can be changed if needed.
736 *
737 * Return: The old entry at this index.
738 */
739void *xas_store(struct xa_state *xas, void *entry)
740{
741 struct xa_node *node;
742 void __rcu **slot = &xas->xa->xa_head;
743 unsigned int offset, max;
744 int count = 0;
745 int values = 0;
746 void *first, *next;
747 bool value = xa_is_value(entry);
748
749 if (entry)
750 first = xas_create(xas);
751 else
752 first = xas_load(xas);
753
754 if (xas_invalid(xas))
755 return first;
756 node = xas->xa_node;
757 if (node && (xas->xa_shift < node->shift))
758 xas->xa_sibs = 0;
759 if ((first == entry) && !xas->xa_sibs)
760 return first;
761
762 next = first;
763 offset = xas->xa_offset;
764 max = xas->xa_offset + xas->xa_sibs;
765 if (node) {
766 slot = &node->slots[offset];
767 if (xas->xa_sibs)
768 xas_squash_marks(xas);
769 }
770 if (!entry)
771 xas_init_marks(xas);
772
773 for (;;) {
774 /*
775 * Must clear the marks before setting the entry to NULL,
776 * otherwise xas_for_each_marked may find a NULL entry and
777 * stop early. rcu_assign_pointer contains a release barrier
778 * so the mark clearing will appear to happen before the
779 * entry is set to NULL.
780 */
781 rcu_assign_pointer(*slot, entry);
782 if (xa_is_node(next))
783 xas_free_nodes(xas, xa_to_node(next));
784 if (!node)
785 break;
786 count += !next - !entry;
787 values += !xa_is_value(first) - !value;
788 if (entry) {
789 if (offset == max)
790 break;
791 if (!xa_is_sibling(entry))
792 entry = xa_mk_sibling(xas->xa_offset);
793 } else {
794 if (offset == XA_CHUNK_MASK)
795 break;
796 }
797 next = xa_entry_locked(xas->xa, node, ++offset);
798 if (!xa_is_sibling(next)) {
799 if (!entry && (offset > max))
800 break;
801 first = next;
802 }
803 slot++;
804 }
805
806 update_node(xas, node, count, values);
807 return first;
808}
809EXPORT_SYMBOL_GPL(xas_store);
810
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -0500811/**
Matthew Wilcox9b89a032017-11-10 09:34:31 -0500812 * xas_get_mark() - Returns the state of this mark.
813 * @xas: XArray operation state.
814 * @mark: Mark number.
815 *
816 * Return: true if the mark is set, false if the mark is clear or @xas
817 * is in an error state.
818 */
819bool xas_get_mark(const struct xa_state *xas, xa_mark_t mark)
820{
821 if (xas_invalid(xas))
822 return false;
823 if (!xas->xa_node)
824 return xa_marked(xas->xa, mark);
825 return node_get_mark(xas->xa_node, xas->xa_offset, mark);
826}
827EXPORT_SYMBOL_GPL(xas_get_mark);
828
829/**
830 * xas_set_mark() - Sets the mark on this entry and its parents.
831 * @xas: XArray operation state.
832 * @mark: Mark number.
833 *
834 * Sets the specified mark on this entry, and walks up the tree setting it
835 * on all the ancestor entries. Does nothing if @xas has not been walked to
836 * an entry, or is in an error state.
837 */
838void xas_set_mark(const struct xa_state *xas, xa_mark_t mark)
839{
840 struct xa_node *node = xas->xa_node;
841 unsigned int offset = xas->xa_offset;
842
843 if (xas_invalid(xas))
844 return;
845
846 while (node) {
847 if (node_set_mark(node, offset, mark))
848 return;
849 offset = node->offset;
850 node = xa_parent_locked(xas->xa, node);
851 }
852
853 if (!xa_marked(xas->xa, mark))
854 xa_mark_set(xas->xa, mark);
855}
856EXPORT_SYMBOL_GPL(xas_set_mark);
857
858/**
859 * xas_clear_mark() - Clears the mark on this entry and its parents.
860 * @xas: XArray operation state.
861 * @mark: Mark number.
862 *
863 * Clears the specified mark on this entry, and walks back to the head
864 * attempting to clear it on all the ancestor entries. Does nothing if
865 * @xas has not been walked to an entry, or is in an error state.
866 */
867void xas_clear_mark(const struct xa_state *xas, xa_mark_t mark)
868{
869 struct xa_node *node = xas->xa_node;
870 unsigned int offset = xas->xa_offset;
871
872 if (xas_invalid(xas))
873 return;
874
875 while (node) {
876 if (!node_clear_mark(node, offset, mark))
877 return;
878 if (node_any_mark(node, mark))
879 return;
880
881 offset = node->offset;
882 node = xa_parent_locked(xas->xa, node);
883 }
884
885 if (xa_marked(xas->xa, mark))
886 xa_mark_clear(xas->xa, mark);
887}
888EXPORT_SYMBOL_GPL(xas_clear_mark);
889
890/**
Matthew Wilcox58d6ea32017-11-10 15:15:08 -0500891 * xas_init_marks() - Initialise all marks for the entry
892 * @xas: Array operations state.
893 *
894 * Initialise all marks for the entry specified by @xas. If we're tracking
895 * free entries with a mark, we need to set it on all entries. All other
896 * marks are cleared.
897 *
898 * This implementation is not as efficient as it could be; we may walk
899 * up the tree multiple times.
900 */
901void xas_init_marks(const struct xa_state *xas)
902{
903 xa_mark_t mark = 0;
904
905 for (;;) {
Matthew Wilcox371c7522018-07-04 10:50:12 -0400906 if (xa_track_free(xas->xa) && mark == XA_FREE_MARK)
907 xas_set_mark(xas, mark);
908 else
909 xas_clear_mark(xas, mark);
Matthew Wilcox58d6ea32017-11-10 15:15:08 -0500910 if (mark == XA_MARK_MAX)
911 break;
912 mark_inc(mark);
913 }
914}
915EXPORT_SYMBOL_GPL(xas_init_marks);
916
917/**
Matthew Wilcoxb803b422017-11-14 08:30:11 -0500918 * xas_pause() - Pause a walk to drop a lock.
919 * @xas: XArray operation state.
920 *
921 * Some users need to pause a walk and drop the lock they're holding in
922 * order to yield to a higher priority thread or carry out an operation
923 * on an entry. Those users should call this function before they drop
924 * the lock. It resets the @xas to be suitable for the next iteration
925 * of the loop after the user has reacquired the lock. If most entries
926 * found during a walk require you to call xas_pause(), the xa_for_each()
927 * iterator may be more appropriate.
928 *
929 * Note that xas_pause() only works for forward iteration. If a user needs
930 * to pause a reverse iteration, we will need a xas_pause_rev().
931 */
932void xas_pause(struct xa_state *xas)
933{
934 struct xa_node *node = xas->xa_node;
935
936 if (xas_invalid(xas))
937 return;
938
939 if (node) {
940 unsigned int offset = xas->xa_offset;
941 while (++offset < XA_CHUNK_SIZE) {
942 if (!xa_is_sibling(xa_entry(xas->xa, node, offset)))
943 break;
944 }
945 xas->xa_index += (offset - xas->xa_offset) << node->shift;
946 } else {
947 xas->xa_index++;
948 }
949 xas->xa_node = XAS_RESTART;
950}
951EXPORT_SYMBOL_GPL(xas_pause);
952
Matthew Wilcox64d3e9a2017-12-01 00:06:52 -0500953/*
954 * __xas_prev() - Find the previous entry in the XArray.
955 * @xas: XArray operation state.
956 *
957 * Helper function for xas_prev() which handles all the complex cases
958 * out of line.
959 */
960void *__xas_prev(struct xa_state *xas)
961{
962 void *entry;
963
964 if (!xas_frozen(xas->xa_node))
965 xas->xa_index--;
966 if (xas_not_node(xas->xa_node))
967 return xas_load(xas);
968
969 if (xas->xa_offset != get_offset(xas->xa_index, xas->xa_node))
970 xas->xa_offset--;
971
972 while (xas->xa_offset == 255) {
973 xas->xa_offset = xas->xa_node->offset - 1;
974 xas->xa_node = xa_parent(xas->xa, xas->xa_node);
975 if (!xas->xa_node)
976 return set_bounds(xas);
977 }
978
979 for (;;) {
980 entry = xa_entry(xas->xa, xas->xa_node, xas->xa_offset);
981 if (!xa_is_node(entry))
982 return entry;
983
984 xas->xa_node = xa_to_node(entry);
985 xas_set_offset(xas);
986 }
987}
988EXPORT_SYMBOL_GPL(__xas_prev);
989
990/*
991 * __xas_next() - Find the next entry in the XArray.
992 * @xas: XArray operation state.
993 *
994 * Helper function for xas_next() which handles all the complex cases
995 * out of line.
996 */
997void *__xas_next(struct xa_state *xas)
998{
999 void *entry;
1000
1001 if (!xas_frozen(xas->xa_node))
1002 xas->xa_index++;
1003 if (xas_not_node(xas->xa_node))
1004 return xas_load(xas);
1005
1006 if (xas->xa_offset != get_offset(xas->xa_index, xas->xa_node))
1007 xas->xa_offset++;
1008
1009 while (xas->xa_offset == XA_CHUNK_SIZE) {
1010 xas->xa_offset = xas->xa_node->offset + 1;
1011 xas->xa_node = xa_parent(xas->xa, xas->xa_node);
1012 if (!xas->xa_node)
1013 return set_bounds(xas);
1014 }
1015
1016 for (;;) {
1017 entry = xa_entry(xas->xa, xas->xa_node, xas->xa_offset);
1018 if (!xa_is_node(entry))
1019 return entry;
1020
1021 xas->xa_node = xa_to_node(entry);
1022 xas_set_offset(xas);
1023 }
1024}
1025EXPORT_SYMBOL_GPL(__xas_next);
1026
Matthew Wilcoxb803b422017-11-14 08:30:11 -05001027/**
1028 * xas_find() - Find the next present entry in the XArray.
1029 * @xas: XArray operation state.
1030 * @max: Highest index to return.
1031 *
1032 * If the @xas has not yet been walked to an entry, return the entry
1033 * which has an index >= xas.xa_index. If it has been walked, the entry
1034 * currently being pointed at has been processed, and so we move to the
1035 * next entry.
1036 *
1037 * If no entry is found and the array is smaller than @max, the iterator
1038 * is set to the smallest index not yet in the array. This allows @xas
1039 * to be immediately passed to xas_store().
1040 *
1041 * Return: The entry, if found, otherwise %NULL.
1042 */
1043void *xas_find(struct xa_state *xas, unsigned long max)
1044{
1045 void *entry;
1046
1047 if (xas_error(xas))
1048 return NULL;
1049
1050 if (!xas->xa_node) {
1051 xas->xa_index = 1;
1052 return set_bounds(xas);
1053 } else if (xas_top(xas->xa_node)) {
1054 entry = xas_load(xas);
1055 if (entry || xas_not_node(xas->xa_node))
1056 return entry;
1057 } else if (!xas->xa_node->shift &&
1058 xas->xa_offset != (xas->xa_index & XA_CHUNK_MASK)) {
1059 xas->xa_offset = ((xas->xa_index - 1) & XA_CHUNK_MASK) + 1;
1060 }
1061
1062 xas_advance(xas);
1063
1064 while (xas->xa_node && (xas->xa_index <= max)) {
1065 if (unlikely(xas->xa_offset == XA_CHUNK_SIZE)) {
1066 xas->xa_offset = xas->xa_node->offset + 1;
1067 xas->xa_node = xa_parent(xas->xa, xas->xa_node);
1068 continue;
1069 }
1070
1071 entry = xa_entry(xas->xa, xas->xa_node, xas->xa_offset);
1072 if (xa_is_node(entry)) {
1073 xas->xa_node = xa_to_node(entry);
1074 xas->xa_offset = 0;
1075 continue;
1076 }
1077 if (entry && !xa_is_sibling(entry))
1078 return entry;
1079
1080 xas_advance(xas);
1081 }
1082
1083 if (!xas->xa_node)
1084 xas->xa_node = XAS_BOUNDS;
1085 return NULL;
1086}
1087EXPORT_SYMBOL_GPL(xas_find);
1088
1089/**
1090 * xas_find_marked() - Find the next marked entry in the XArray.
1091 * @xas: XArray operation state.
1092 * @max: Highest index to return.
1093 * @mark: Mark number to search for.
1094 *
1095 * If the @xas has not yet been walked to an entry, return the marked entry
1096 * which has an index >= xas.xa_index. If it has been walked, the entry
1097 * currently being pointed at has been processed, and so we return the
1098 * first marked entry with an index > xas.xa_index.
1099 *
1100 * If no marked entry is found and the array is smaller than @max, @xas is
1101 * set to the bounds state and xas->xa_index is set to the smallest index
1102 * not yet in the array. This allows @xas to be immediately passed to
1103 * xas_store().
1104 *
1105 * If no entry is found before @max is reached, @xas is set to the restart
1106 * state.
1107 *
1108 * Return: The entry, if found, otherwise %NULL.
1109 */
1110void *xas_find_marked(struct xa_state *xas, unsigned long max, xa_mark_t mark)
1111{
1112 bool advance = true;
1113 unsigned int offset;
1114 void *entry;
1115
1116 if (xas_error(xas))
1117 return NULL;
1118
1119 if (!xas->xa_node) {
1120 xas->xa_index = 1;
1121 goto out;
1122 } else if (xas_top(xas->xa_node)) {
1123 advance = false;
1124 entry = xa_head(xas->xa);
1125 xas->xa_node = NULL;
1126 if (xas->xa_index > max_index(entry))
1127 goto bounds;
1128 if (!xa_is_node(entry)) {
1129 if (xa_marked(xas->xa, mark))
1130 return entry;
1131 xas->xa_index = 1;
1132 goto out;
1133 }
1134 xas->xa_node = xa_to_node(entry);
1135 xas->xa_offset = xas->xa_index >> xas->xa_node->shift;
1136 }
1137
1138 while (xas->xa_index <= max) {
1139 if (unlikely(xas->xa_offset == XA_CHUNK_SIZE)) {
1140 xas->xa_offset = xas->xa_node->offset + 1;
1141 xas->xa_node = xa_parent(xas->xa, xas->xa_node);
1142 if (!xas->xa_node)
1143 break;
1144 advance = false;
1145 continue;
1146 }
1147
1148 if (!advance) {
1149 entry = xa_entry(xas->xa, xas->xa_node, xas->xa_offset);
1150 if (xa_is_sibling(entry)) {
1151 xas->xa_offset = xa_to_sibling(entry);
1152 xas_move_index(xas, xas->xa_offset);
1153 }
1154 }
1155
1156 offset = xas_find_chunk(xas, advance, mark);
1157 if (offset > xas->xa_offset) {
1158 advance = false;
1159 xas_move_index(xas, offset);
1160 /* Mind the wrap */
1161 if ((xas->xa_index - 1) >= max)
1162 goto max;
1163 xas->xa_offset = offset;
1164 if (offset == XA_CHUNK_SIZE)
1165 continue;
1166 }
1167
1168 entry = xa_entry(xas->xa, xas->xa_node, xas->xa_offset);
1169 if (!xa_is_node(entry))
1170 return entry;
1171 xas->xa_node = xa_to_node(entry);
1172 xas_set_offset(xas);
1173 }
1174
1175out:
1176 if (!max)
1177 goto max;
1178bounds:
1179 xas->xa_node = XAS_BOUNDS;
1180 return NULL;
1181max:
1182 xas->xa_node = XAS_RESTART;
1183 return NULL;
1184}
1185EXPORT_SYMBOL_GPL(xas_find_marked);
1186
1187/**
Matthew Wilcox4e99d4e2018-06-01 22:46:02 -04001188 * xas_find_conflict() - Find the next present entry in a range.
1189 * @xas: XArray operation state.
1190 *
1191 * The @xas describes both a range and a position within that range.
1192 *
1193 * Context: Any context. Expects xa_lock to be held.
1194 * Return: The next entry in the range covered by @xas or %NULL.
1195 */
1196void *xas_find_conflict(struct xa_state *xas)
1197{
1198 void *curr;
1199
1200 if (xas_error(xas))
1201 return NULL;
1202
1203 if (!xas->xa_node)
1204 return NULL;
1205
1206 if (xas_top(xas->xa_node)) {
1207 curr = xas_start(xas);
1208 if (!curr)
1209 return NULL;
1210 while (xa_is_node(curr)) {
1211 struct xa_node *node = xa_to_node(curr);
1212 curr = xas_descend(xas, node);
1213 }
1214 if (curr)
1215 return curr;
1216 }
1217
1218 if (xas->xa_node->shift > xas->xa_shift)
1219 return NULL;
1220
1221 for (;;) {
1222 if (xas->xa_node->shift == xas->xa_shift) {
1223 if ((xas->xa_offset & xas->xa_sibs) == xas->xa_sibs)
1224 break;
1225 } else if (xas->xa_offset == XA_CHUNK_MASK) {
1226 xas->xa_offset = xas->xa_node->offset;
1227 xas->xa_node = xa_parent_locked(xas->xa, xas->xa_node);
1228 if (!xas->xa_node)
1229 break;
1230 continue;
1231 }
1232 curr = xa_entry_locked(xas->xa, xas->xa_node, ++xas->xa_offset);
1233 if (xa_is_sibling(curr))
1234 continue;
1235 while (xa_is_node(curr)) {
1236 xas->xa_node = xa_to_node(curr);
1237 xas->xa_offset = 0;
1238 curr = xa_entry_locked(xas->xa, xas->xa_node, 0);
1239 }
1240 if (curr)
1241 return curr;
1242 }
1243 xas->xa_offset -= xas->xa_sibs;
1244 return NULL;
1245}
1246EXPORT_SYMBOL_GPL(xas_find_conflict);
1247
1248/**
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -05001249 * xa_init_flags() - Initialise an empty XArray with flags.
1250 * @xa: XArray.
1251 * @flags: XA_FLAG values.
1252 *
1253 * If you need to initialise an XArray with special flags (eg you need
1254 * to take the lock from interrupt context), use this function instead
1255 * of xa_init().
1256 *
1257 * Context: Any context.
1258 */
1259void xa_init_flags(struct xarray *xa, gfp_t flags)
1260{
Matthew Wilcox58d6ea32017-11-10 15:15:08 -05001261 unsigned int lock_type;
1262 static struct lock_class_key xa_lock_irq;
1263 static struct lock_class_key xa_lock_bh;
1264
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -05001265 spin_lock_init(&xa->xa_lock);
1266 xa->xa_flags = flags;
1267 xa->xa_head = NULL;
Matthew Wilcox58d6ea32017-11-10 15:15:08 -05001268
1269 lock_type = xa_lock_type(xa);
1270 if (lock_type == XA_LOCK_IRQ)
1271 lockdep_set_class(&xa->xa_lock, &xa_lock_irq);
1272 else if (lock_type == XA_LOCK_BH)
1273 lockdep_set_class(&xa->xa_lock, &xa_lock_bh);
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -05001274}
1275EXPORT_SYMBOL(xa_init_flags);
Matthew Wilcoxad3d6c72017-11-07 14:57:46 -05001276
1277/**
1278 * xa_load() - Load an entry from an XArray.
1279 * @xa: XArray.
1280 * @index: index into array.
1281 *
1282 * Context: Any context. Takes and releases the RCU lock.
1283 * Return: The entry at @index in @xa.
1284 */
1285void *xa_load(struct xarray *xa, unsigned long index)
1286{
1287 XA_STATE(xas, xa, index);
1288 void *entry;
1289
1290 rcu_read_lock();
1291 do {
1292 entry = xas_load(&xas);
Matthew Wilcox9f14d4f2018-10-01 14:54:59 -04001293 if (xa_is_zero(entry))
1294 entry = NULL;
Matthew Wilcoxad3d6c72017-11-07 14:57:46 -05001295 } while (xas_retry(&xas, entry));
1296 rcu_read_unlock();
1297
1298 return entry;
1299}
1300EXPORT_SYMBOL(xa_load);
1301
Matthew Wilcox58d6ea32017-11-10 15:15:08 -05001302static void *xas_result(struct xa_state *xas, void *curr)
1303{
Matthew Wilcox9f14d4f2018-10-01 14:54:59 -04001304 if (xa_is_zero(curr))
1305 return NULL;
Matthew Wilcox58d6ea32017-11-10 15:15:08 -05001306 XA_NODE_BUG_ON(xas->xa_node, xa_is_internal(curr));
1307 if (xas_error(xas))
1308 curr = xas->xa_node;
1309 return curr;
1310}
1311
1312/**
1313 * __xa_erase() - Erase this entry from the XArray while locked.
1314 * @xa: XArray.
1315 * @index: Index into array.
1316 *
1317 * If the entry at this index is a multi-index entry then all indices will
1318 * be erased, and the entry will no longer be a multi-index entry.
1319 * This function expects the xa_lock to be held on entry.
1320 *
1321 * Context: Any context. Expects xa_lock to be held on entry. May
1322 * release and reacquire xa_lock if @gfp flags permit.
1323 * Return: The old entry at this index.
1324 */
1325void *__xa_erase(struct xarray *xa, unsigned long index)
1326{
1327 XA_STATE(xas, xa, index);
1328 return xas_result(&xas, xas_store(&xas, NULL));
1329}
1330EXPORT_SYMBOL_GPL(__xa_erase);
1331
1332/**
1333 * xa_store() - Store this entry in the XArray.
1334 * @xa: XArray.
1335 * @index: Index into array.
1336 * @entry: New entry.
1337 * @gfp: Memory allocation flags.
1338 *
1339 * After this function returns, loads from this index will return @entry.
1340 * Storing into an existing multislot entry updates the entry of every index.
1341 * The marks associated with @index are unaffected unless @entry is %NULL.
1342 *
1343 * Context: Process context. Takes and releases the xa_lock. May sleep
1344 * if the @gfp flags permit.
1345 * Return: The old entry at this index on success, xa_err(-EINVAL) if @entry
1346 * cannot be stored in an XArray, or xa_err(-ENOMEM) if memory allocation
1347 * failed.
1348 */
1349void *xa_store(struct xarray *xa, unsigned long index, void *entry, gfp_t gfp)
1350{
1351 XA_STATE(xas, xa, index);
1352 void *curr;
1353
1354 if (WARN_ON_ONCE(xa_is_internal(entry)))
1355 return XA_ERROR(-EINVAL);
1356
1357 do {
1358 xas_lock(&xas);
1359 curr = xas_store(&xas, entry);
Matthew Wilcox371c7522018-07-04 10:50:12 -04001360 if (xa_track_free(xa) && entry)
1361 xas_clear_mark(&xas, XA_FREE_MARK);
Matthew Wilcox58d6ea32017-11-10 15:15:08 -05001362 xas_unlock(&xas);
1363 } while (xas_nomem(&xas, gfp));
1364
1365 return xas_result(&xas, curr);
1366}
1367EXPORT_SYMBOL(xa_store);
1368
1369/**
1370 * __xa_store() - Store this entry in the XArray.
1371 * @xa: XArray.
1372 * @index: Index into array.
1373 * @entry: New entry.
1374 * @gfp: Memory allocation flags.
1375 *
1376 * You must already be holding the xa_lock when calling this function.
1377 * It will drop the lock if needed to allocate memory, and then reacquire
1378 * it afterwards.
1379 *
1380 * Context: Any context. Expects xa_lock to be held on entry. May
1381 * release and reacquire xa_lock if @gfp flags permit.
1382 * Return: The old entry at this index or xa_err() if an error happened.
1383 */
1384void *__xa_store(struct xarray *xa, unsigned long index, void *entry, gfp_t gfp)
1385{
1386 XA_STATE(xas, xa, index);
1387 void *curr;
1388
1389 if (WARN_ON_ONCE(xa_is_internal(entry)))
1390 return XA_ERROR(-EINVAL);
1391
1392 do {
1393 curr = xas_store(&xas, entry);
Matthew Wilcox371c7522018-07-04 10:50:12 -04001394 if (xa_track_free(xa) && entry)
1395 xas_clear_mark(&xas, XA_FREE_MARK);
Matthew Wilcox58d6ea32017-11-10 15:15:08 -05001396 } while (__xas_nomem(&xas, gfp));
1397
1398 return xas_result(&xas, curr);
1399}
1400EXPORT_SYMBOL(__xa_store);
1401
Matthew Wilcox9b89a032017-11-10 09:34:31 -05001402/**
Matthew Wilcox41aec912017-11-10 15:34:55 -05001403 * xa_cmpxchg() - Conditionally replace an entry in the XArray.
1404 * @xa: XArray.
1405 * @index: Index into array.
1406 * @old: Old value to test against.
1407 * @entry: New value to place in array.
1408 * @gfp: Memory allocation flags.
1409 *
1410 * If the entry at @index is the same as @old, replace it with @entry.
1411 * If the return value is equal to @old, then the exchange was successful.
1412 *
1413 * Context: Process context. Takes and releases the xa_lock. May sleep
1414 * if the @gfp flags permit.
1415 * Return: The old value at this index or xa_err() if an error happened.
1416 */
1417void *xa_cmpxchg(struct xarray *xa, unsigned long index,
1418 void *old, void *entry, gfp_t gfp)
1419{
1420 XA_STATE(xas, xa, index);
1421 void *curr;
1422
1423 if (WARN_ON_ONCE(xa_is_internal(entry)))
1424 return XA_ERROR(-EINVAL);
1425
1426 do {
1427 xas_lock(&xas);
1428 curr = xas_load(&xas);
Matthew Wilcox9f14d4f2018-10-01 14:54:59 -04001429 if (curr == XA_ZERO_ENTRY)
1430 curr = NULL;
Matthew Wilcox371c7522018-07-04 10:50:12 -04001431 if (curr == old) {
Matthew Wilcox41aec912017-11-10 15:34:55 -05001432 xas_store(&xas, entry);
Matthew Wilcox371c7522018-07-04 10:50:12 -04001433 if (xa_track_free(xa) && entry)
1434 xas_clear_mark(&xas, XA_FREE_MARK);
1435 }
Matthew Wilcox41aec912017-11-10 15:34:55 -05001436 xas_unlock(&xas);
1437 } while (xas_nomem(&xas, gfp));
1438
1439 return xas_result(&xas, curr);
1440}
1441EXPORT_SYMBOL(xa_cmpxchg);
1442
1443/**
1444 * __xa_cmpxchg() - Store this entry in the XArray.
1445 * @xa: XArray.
1446 * @index: Index into array.
1447 * @old: Old value to test against.
1448 * @entry: New entry.
1449 * @gfp: Memory allocation flags.
1450 *
1451 * You must already be holding the xa_lock when calling this function.
1452 * It will drop the lock if needed to allocate memory, and then reacquire
1453 * it afterwards.
1454 *
1455 * Context: Any context. Expects xa_lock to be held on entry. May
1456 * release and reacquire xa_lock if @gfp flags permit.
1457 * Return: The old entry at this index or xa_err() if an error happened.
1458 */
1459void *__xa_cmpxchg(struct xarray *xa, unsigned long index,
1460 void *old, void *entry, gfp_t gfp)
1461{
1462 XA_STATE(xas, xa, index);
1463 void *curr;
1464
1465 if (WARN_ON_ONCE(xa_is_internal(entry)))
1466 return XA_ERROR(-EINVAL);
1467
1468 do {
1469 curr = xas_load(&xas);
Matthew Wilcox9f14d4f2018-10-01 14:54:59 -04001470 if (curr == XA_ZERO_ENTRY)
1471 curr = NULL;
Matthew Wilcox371c7522018-07-04 10:50:12 -04001472 if (curr == old) {
Matthew Wilcox41aec912017-11-10 15:34:55 -05001473 xas_store(&xas, entry);
Matthew Wilcox371c7522018-07-04 10:50:12 -04001474 if (xa_track_free(xa) && entry)
1475 xas_clear_mark(&xas, XA_FREE_MARK);
1476 }
Matthew Wilcox41aec912017-11-10 15:34:55 -05001477 } while (__xas_nomem(&xas, gfp));
1478
1479 return xas_result(&xas, curr);
1480}
1481EXPORT_SYMBOL(__xa_cmpxchg);
1482
1483/**
Matthew Wilcox9f14d4f2018-10-01 14:54:59 -04001484 * xa_reserve() - Reserve this index in the XArray.
1485 * @xa: XArray.
1486 * @index: Index into array.
1487 * @gfp: Memory allocation flags.
1488 *
1489 * Ensures there is somewhere to store an entry at @index in the array.
1490 * If there is already something stored at @index, this function does
1491 * nothing. If there was nothing there, the entry is marked as reserved.
1492 * Loads from @index will continue to see a %NULL pointer until a
1493 * subsequent store to @index.
1494 *
1495 * If you do not use the entry that you have reserved, call xa_release()
1496 * or xa_erase() to free any unnecessary memory.
1497 *
1498 * Context: Process context. Takes and releases the xa_lock, IRQ or BH safe
1499 * if specified in XArray flags. May sleep if the @gfp flags permit.
1500 * Return: 0 if the reservation succeeded or -ENOMEM if it failed.
1501 */
1502int xa_reserve(struct xarray *xa, unsigned long index, gfp_t gfp)
1503{
1504 XA_STATE(xas, xa, index);
1505 unsigned int lock_type = xa_lock_type(xa);
1506 void *curr;
1507
1508 do {
1509 xas_lock_type(&xas, lock_type);
1510 curr = xas_load(&xas);
1511 if (!curr)
1512 xas_store(&xas, XA_ZERO_ENTRY);
1513 xas_unlock_type(&xas, lock_type);
1514 } while (xas_nomem(&xas, gfp));
1515
1516 return xas_error(&xas);
1517}
1518EXPORT_SYMBOL(xa_reserve);
1519
1520/**
Matthew Wilcox371c7522018-07-04 10:50:12 -04001521 * __xa_alloc() - Find somewhere to store this entry in the XArray.
1522 * @xa: XArray.
1523 * @id: Pointer to ID.
1524 * @max: Maximum ID to allocate (inclusive).
1525 * @entry: New entry.
1526 * @gfp: Memory allocation flags.
1527 *
1528 * Allocates an unused ID in the range specified by @id and @max.
1529 * Updates the @id pointer with the index, then stores the entry at that
1530 * index. A concurrent lookup will not see an uninitialised @id.
1531 *
1532 * Context: Any context. Expects xa_lock to be held on entry. May
1533 * release and reacquire xa_lock if @gfp flags permit.
1534 * Return: 0 on success, -ENOMEM if memory allocation fails or -ENOSPC if
1535 * there is no more space in the XArray.
1536 */
1537int __xa_alloc(struct xarray *xa, u32 *id, u32 max, void *entry, gfp_t gfp)
1538{
1539 XA_STATE(xas, xa, 0);
1540 int err;
1541
1542 if (WARN_ON_ONCE(xa_is_internal(entry)))
1543 return -EINVAL;
1544 if (WARN_ON_ONCE(!xa_track_free(xa)))
1545 return -EINVAL;
1546
1547 if (!entry)
1548 entry = XA_ZERO_ENTRY;
1549
1550 do {
1551 xas.xa_index = *id;
1552 xas_find_marked(&xas, max, XA_FREE_MARK);
1553 if (xas.xa_node == XAS_RESTART)
1554 xas_set_err(&xas, -ENOSPC);
1555 xas_store(&xas, entry);
1556 xas_clear_mark(&xas, XA_FREE_MARK);
1557 } while (__xas_nomem(&xas, gfp));
1558
1559 err = xas_error(&xas);
1560 if (!err)
1561 *id = xas.xa_index;
1562 return err;
1563}
1564EXPORT_SYMBOL(__xa_alloc);
1565
1566/**
Matthew Wilcox9b89a032017-11-10 09:34:31 -05001567 * __xa_set_mark() - Set this mark on this entry while locked.
1568 * @xa: XArray.
1569 * @index: Index of entry.
1570 * @mark: Mark number.
1571 *
1572 * Attempting to set a mark on a NULL entry does not succeed.
1573 *
1574 * Context: Any context. Expects xa_lock to be held on entry.
1575 */
1576void __xa_set_mark(struct xarray *xa, unsigned long index, xa_mark_t mark)
1577{
1578 XA_STATE(xas, xa, index);
1579 void *entry = xas_load(&xas);
1580
1581 if (entry)
1582 xas_set_mark(&xas, mark);
1583}
1584EXPORT_SYMBOL_GPL(__xa_set_mark);
1585
1586/**
1587 * __xa_clear_mark() - Clear this mark on this entry while locked.
1588 * @xa: XArray.
1589 * @index: Index of entry.
1590 * @mark: Mark number.
1591 *
1592 * Context: Any context. Expects xa_lock to be held on entry.
1593 */
1594void __xa_clear_mark(struct xarray *xa, unsigned long index, xa_mark_t mark)
1595{
1596 XA_STATE(xas, xa, index);
1597 void *entry = xas_load(&xas);
1598
1599 if (entry)
1600 xas_clear_mark(&xas, mark);
1601}
1602EXPORT_SYMBOL_GPL(__xa_clear_mark);
1603
1604/**
1605 * xa_get_mark() - Inquire whether this mark is set on this entry.
1606 * @xa: XArray.
1607 * @index: Index of entry.
1608 * @mark: Mark number.
1609 *
1610 * This function uses the RCU read lock, so the result may be out of date
1611 * by the time it returns. If you need the result to be stable, use a lock.
1612 *
1613 * Context: Any context. Takes and releases the RCU lock.
1614 * Return: True if the entry at @index has this mark set, false if it doesn't.
1615 */
1616bool xa_get_mark(struct xarray *xa, unsigned long index, xa_mark_t mark)
1617{
1618 XA_STATE(xas, xa, index);
1619 void *entry;
1620
1621 rcu_read_lock();
1622 entry = xas_start(&xas);
1623 while (xas_get_mark(&xas, mark)) {
1624 if (!xa_is_node(entry))
1625 goto found;
1626 entry = xas_descend(&xas, xa_to_node(entry));
1627 }
1628 rcu_read_unlock();
1629 return false;
1630 found:
1631 rcu_read_unlock();
1632 return true;
1633}
1634EXPORT_SYMBOL(xa_get_mark);
1635
1636/**
1637 * xa_set_mark() - Set this mark on this entry.
1638 * @xa: XArray.
1639 * @index: Index of entry.
1640 * @mark: Mark number.
1641 *
1642 * Attempting to set a mark on a NULL entry does not succeed.
1643 *
1644 * Context: Process context. Takes and releases the xa_lock.
1645 */
1646void xa_set_mark(struct xarray *xa, unsigned long index, xa_mark_t mark)
1647{
1648 xa_lock(xa);
1649 __xa_set_mark(xa, index, mark);
1650 xa_unlock(xa);
1651}
1652EXPORT_SYMBOL(xa_set_mark);
1653
1654/**
1655 * xa_clear_mark() - Clear this mark on this entry.
1656 * @xa: XArray.
1657 * @index: Index of entry.
1658 * @mark: Mark number.
1659 *
1660 * Clearing a mark always succeeds.
1661 *
1662 * Context: Process context. Takes and releases the xa_lock.
1663 */
1664void xa_clear_mark(struct xarray *xa, unsigned long index, xa_mark_t mark)
1665{
1666 xa_lock(xa);
1667 __xa_clear_mark(xa, index, mark);
1668 xa_unlock(xa);
1669}
1670EXPORT_SYMBOL(xa_clear_mark);
1671
Matthew Wilcoxb803b422017-11-14 08:30:11 -05001672/**
1673 * xa_find() - Search the XArray for an entry.
1674 * @xa: XArray.
1675 * @indexp: Pointer to an index.
1676 * @max: Maximum index to search to.
1677 * @filter: Selection criterion.
1678 *
1679 * Finds the entry in @xa which matches the @filter, and has the lowest
1680 * index that is at least @indexp and no more than @max.
1681 * If an entry is found, @indexp is updated to be the index of the entry.
1682 * This function is protected by the RCU read lock, so it may not find
1683 * entries which are being simultaneously added. It will not return an
1684 * %XA_RETRY_ENTRY; if you need to see retry entries, use xas_find().
1685 *
1686 * Context: Any context. Takes and releases the RCU lock.
1687 * Return: The entry, if found, otherwise %NULL.
1688 */
1689void *xa_find(struct xarray *xa, unsigned long *indexp,
1690 unsigned long max, xa_mark_t filter)
1691{
1692 XA_STATE(xas, xa, *indexp);
1693 void *entry;
1694
1695 rcu_read_lock();
1696 do {
1697 if ((__force unsigned int)filter < XA_MAX_MARKS)
1698 entry = xas_find_marked(&xas, max, filter);
1699 else
1700 entry = xas_find(&xas, max);
1701 } while (xas_retry(&xas, entry));
1702 rcu_read_unlock();
1703
1704 if (entry)
1705 *indexp = xas.xa_index;
1706 return entry;
1707}
1708EXPORT_SYMBOL(xa_find);
1709
1710/**
1711 * xa_find_after() - Search the XArray for a present entry.
1712 * @xa: XArray.
1713 * @indexp: Pointer to an index.
1714 * @max: Maximum index to search to.
1715 * @filter: Selection criterion.
1716 *
1717 * Finds the entry in @xa which matches the @filter and has the lowest
1718 * index that is above @indexp and no more than @max.
1719 * If an entry is found, @indexp is updated to be the index of the entry.
1720 * This function is protected by the RCU read lock, so it may miss entries
1721 * which are being simultaneously added. It will not return an
1722 * %XA_RETRY_ENTRY; if you need to see retry entries, use xas_find().
1723 *
1724 * Context: Any context. Takes and releases the RCU lock.
1725 * Return: The pointer, if found, otherwise %NULL.
1726 */
1727void *xa_find_after(struct xarray *xa, unsigned long *indexp,
1728 unsigned long max, xa_mark_t filter)
1729{
1730 XA_STATE(xas, xa, *indexp + 1);
1731 void *entry;
1732
1733 rcu_read_lock();
1734 for (;;) {
1735 if ((__force unsigned int)filter < XA_MAX_MARKS)
1736 entry = xas_find_marked(&xas, max, filter);
1737 else
1738 entry = xas_find(&xas, max);
1739 if (xas.xa_shift) {
1740 if (xas.xa_index & ((1UL << xas.xa_shift) - 1))
1741 continue;
1742 } else {
1743 if (xas.xa_offset < (xas.xa_index & XA_CHUNK_MASK))
1744 continue;
1745 }
1746 if (!xas_retry(&xas, entry))
1747 break;
1748 }
1749 rcu_read_unlock();
1750
1751 if (entry)
1752 *indexp = xas.xa_index;
1753 return entry;
1754}
1755EXPORT_SYMBOL(xa_find_after);
1756
Matthew Wilcox80a0a1a2017-11-14 16:42:22 -05001757static unsigned int xas_extract_present(struct xa_state *xas, void **dst,
1758 unsigned long max, unsigned int n)
1759{
1760 void *entry;
1761 unsigned int i = 0;
1762
1763 rcu_read_lock();
1764 xas_for_each(xas, entry, max) {
1765 if (xas_retry(xas, entry))
1766 continue;
1767 dst[i++] = entry;
1768 if (i == n)
1769 break;
1770 }
1771 rcu_read_unlock();
1772
1773 return i;
1774}
1775
1776static unsigned int xas_extract_marked(struct xa_state *xas, void **dst,
1777 unsigned long max, unsigned int n, xa_mark_t mark)
1778{
1779 void *entry;
1780 unsigned int i = 0;
1781
1782 rcu_read_lock();
1783 xas_for_each_marked(xas, entry, max, mark) {
1784 if (xas_retry(xas, entry))
1785 continue;
1786 dst[i++] = entry;
1787 if (i == n)
1788 break;
1789 }
1790 rcu_read_unlock();
1791
1792 return i;
1793}
1794
1795/**
1796 * xa_extract() - Copy selected entries from the XArray into a normal array.
1797 * @xa: The source XArray to copy from.
1798 * @dst: The buffer to copy entries into.
1799 * @start: The first index in the XArray eligible to be selected.
1800 * @max: The last index in the XArray eligible to be selected.
1801 * @n: The maximum number of entries to copy.
1802 * @filter: Selection criterion.
1803 *
1804 * Copies up to @n entries that match @filter from the XArray. The
1805 * copied entries will have indices between @start and @max, inclusive.
1806 *
1807 * The @filter may be an XArray mark value, in which case entries which are
1808 * marked with that mark will be copied. It may also be %XA_PRESENT, in
1809 * which case all entries which are not NULL will be copied.
1810 *
1811 * The entries returned may not represent a snapshot of the XArray at a
1812 * moment in time. For example, if another thread stores to index 5, then
1813 * index 10, calling xa_extract() may return the old contents of index 5
1814 * and the new contents of index 10. Indices not modified while this
1815 * function is running will not be skipped.
1816 *
1817 * If you need stronger guarantees, holding the xa_lock across calls to this
1818 * function will prevent concurrent modification.
1819 *
1820 * Context: Any context. Takes and releases the RCU lock.
1821 * Return: The number of entries copied.
1822 */
1823unsigned int xa_extract(struct xarray *xa, void **dst, unsigned long start,
1824 unsigned long max, unsigned int n, xa_mark_t filter)
1825{
1826 XA_STATE(xas, xa, start);
1827
1828 if (!n)
1829 return 0;
1830
1831 if ((__force unsigned int)filter < XA_MAX_MARKS)
1832 return xas_extract_marked(&xas, dst, max, n, filter);
1833 return xas_extract_present(&xas, dst, max, n);
1834}
1835EXPORT_SYMBOL(xa_extract);
1836
Matthew Wilcox687149f2017-11-17 08:16:34 -05001837/**
1838 * xa_destroy() - Free all internal data structures.
1839 * @xa: XArray.
1840 *
1841 * After calling this function, the XArray is empty and has freed all memory
1842 * allocated for its internal data structures. You are responsible for
1843 * freeing the objects referenced by the XArray.
1844 *
1845 * Context: Any context. Takes and releases the xa_lock, interrupt-safe.
1846 */
1847void xa_destroy(struct xarray *xa)
1848{
1849 XA_STATE(xas, xa, 0);
1850 unsigned long flags;
1851 void *entry;
1852
1853 xas.xa_node = NULL;
1854 xas_lock_irqsave(&xas, flags);
1855 entry = xa_head_locked(xa);
1856 RCU_INIT_POINTER(xa->xa_head, NULL);
1857 xas_init_marks(&xas);
1858 /* lockdep checks we're still holding the lock in xas_free_nodes() */
1859 if (xa_is_node(entry))
1860 xas_free_nodes(&xas, xa_to_node(entry));
1861 xas_unlock_irqrestore(&xas, flags);
1862}
1863EXPORT_SYMBOL(xa_destroy);
1864
Matthew Wilcoxad3d6c72017-11-07 14:57:46 -05001865#ifdef XA_DEBUG
1866void xa_dump_node(const struct xa_node *node)
1867{
1868 unsigned i, j;
1869
1870 if (!node)
1871 return;
1872 if ((unsigned long)node & 3) {
1873 pr_cont("node %px\n", node);
1874 return;
1875 }
1876
1877 pr_cont("node %px %s %d parent %px shift %d count %d values %d "
1878 "array %px list %px %px marks",
1879 node, node->parent ? "offset" : "max", node->offset,
1880 node->parent, node->shift, node->count, node->nr_values,
1881 node->array, node->private_list.prev, node->private_list.next);
1882 for (i = 0; i < XA_MAX_MARKS; i++)
1883 for (j = 0; j < XA_MARK_LONGS; j++)
1884 pr_cont(" %lx", node->marks[i][j]);
1885 pr_cont("\n");
1886}
1887
1888void xa_dump_index(unsigned long index, unsigned int shift)
1889{
1890 if (!shift)
1891 pr_info("%lu: ", index);
1892 else if (shift >= BITS_PER_LONG)
1893 pr_info("0-%lu: ", ~0UL);
1894 else
1895 pr_info("%lu-%lu: ", index, index | ((1UL << shift) - 1));
1896}
1897
1898void xa_dump_entry(const void *entry, unsigned long index, unsigned long shift)
1899{
1900 if (!entry)
1901 return;
1902
1903 xa_dump_index(index, shift);
1904
1905 if (xa_is_node(entry)) {
1906 if (shift == 0) {
1907 pr_cont("%px\n", entry);
1908 } else {
1909 unsigned long i;
1910 struct xa_node *node = xa_to_node(entry);
1911 xa_dump_node(node);
1912 for (i = 0; i < XA_CHUNK_SIZE; i++)
1913 xa_dump_entry(node->slots[i],
1914 index + (i << node->shift), node->shift);
1915 }
1916 } else if (xa_is_value(entry))
1917 pr_cont("value %ld (0x%lx) [%px]\n", xa_to_value(entry),
1918 xa_to_value(entry), entry);
1919 else if (!xa_is_internal(entry))
1920 pr_cont("%px\n", entry);
1921 else if (xa_is_retry(entry))
1922 pr_cont("retry (%ld)\n", xa_to_internal(entry));
1923 else if (xa_is_sibling(entry))
1924 pr_cont("sibling (slot %ld)\n", xa_to_sibling(entry));
Matthew Wilcox9f14d4f2018-10-01 14:54:59 -04001925 else if (xa_is_zero(entry))
1926 pr_cont("zero (%ld)\n", xa_to_internal(entry));
Matthew Wilcoxad3d6c72017-11-07 14:57:46 -05001927 else
1928 pr_cont("UNKNOWN ENTRY (%px)\n", entry);
1929}
1930
1931void xa_dump(const struct xarray *xa)
1932{
1933 void *entry = xa->xa_head;
1934 unsigned int shift = 0;
1935
1936 pr_info("xarray: %px head %px flags %x marks %d %d %d\n", xa, entry,
Matthew Wilcox9b89a032017-11-10 09:34:31 -05001937 xa->xa_flags, xa_marked(xa, XA_MARK_0),
1938 xa_marked(xa, XA_MARK_1), xa_marked(xa, XA_MARK_2));
Matthew Wilcoxad3d6c72017-11-07 14:57:46 -05001939 if (xa_is_node(entry))
1940 shift = xa_to_node(entry)->shift + XA_CHUNK_SHIFT;
1941 xa_dump_entry(entry, 0, shift);
1942}
1943#endif