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