xref: /linux/lib/maple_tree.c (revision c5e67d40a10234541e220750297304df79aaedd0)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Maple Tree implementation
4  * Copyright (c) 2018-2022 Oracle Corporation
5  * Authors: Liam R. Howlett <Liam.Howlett@oracle.com>
6  *	    Matthew Wilcox <willy@infradead.org>
7  * Copyright (c) 2023 ByteDance
8  * Author: Peng Zhang <zhangpeng.00@bytedance.com>
9  */
10 
11 /*
12  * DOC: Interesting implementation details of the Maple Tree
13  *
14  * Each node type has a number of slots for entries and a number of slots for
15  * pivots.  In the case of dense nodes, the pivots are implied by the position
16  * and are simply the slot index + the minimum of the node.
17  *
18  * In regular B-Tree terms, pivots are called keys.  The term pivot is used to
19  * indicate that the tree is specifying ranges.  Pivots may appear in the
20  * subtree with an entry attached to the value whereas keys are unique to a
21  * specific position of a B-tree.  Pivot values are inclusive of the slot with
22  * the same index.
23  *
24  *
25  * The following illustrates the layout of a range64 nodes slots and pivots.
26  *
27  *
28  *  Slots -> | 0 | 1 | 2 | ... | 12 | 13 | 14 | 15 |
29  *           ┬   ┬   ┬   ┬     ┬    ┬    ┬    ┬    ┬
30  *           │   │   │   │     │    │    │    │    └─ Implied maximum
31  *           │   │   │   │     │    │    │    └─ Pivot 14
32  *           │   │   │   │     │    │    └─ Pivot 13
33  *           │   │   │   │     │    └─ Pivot 12
34  *           │   │   │   │     └─ Pivot 11
35  *           │   │   │   └─ Pivot 2
36  *           │   │   └─ Pivot 1
37  *           │   └─ Pivot 0
38  *           └─  Implied minimum
39  *
40  * Slot contents:
41  *  Internal (non-leaf) nodes contain pointers to other nodes.
42  *  Leaf nodes contain entries.
43  *
44  * The location of interest is often referred to as an offset.  All offsets have
45  * a slot, but the last offset has an implied pivot from the node above (or
46  * UINT_MAX for the root node.
47  *
48  * Ranges complicate certain write activities.  When modifying any of
49  * the B-tree variants, it is known that one entry will either be added or
50  * deleted.  When modifying the Maple Tree, one store operation may overwrite
51  * the entire data set, or one half of the tree, or the middle half of the tree.
52  *
53  */
54 
55 
56 #include <linux/maple_tree.h>
57 #include <linux/xarray.h>
58 #include <linux/types.h>
59 #include <linux/export.h>
60 #include <linux/slab.h>
61 #include <linux/limits.h>
62 #include <asm/barrier.h>
63 
64 #define CREATE_TRACE_POINTS
65 #include <trace/events/maple_tree.h>
66 
67 /*
68  * Kernel pointer hashing renders much of the maple tree dump useless as tagged
69  * pointers get hashed to arbitrary values.
70  *
71  * If CONFIG_DEBUG_VM_MAPLE_TREE is set we are in a debug mode where it is
72  * permissible to bypass this. Otherwise remain cautious and retain the hashing.
73  *
74  * Userland doesn't know about %px so also use %p there.
75  */
76 #if defined(__KERNEL__) && defined(CONFIG_DEBUG_VM_MAPLE_TREE)
77 #define PTR_FMT "%px"
78 #else
79 #define PTR_FMT "%p"
80 #endif
81 
82 #define MA_ROOT_PARENT 1
83 
84 /*
85  * Maple state flags
86  * * MA_STATE_BULK		- Bulk insert mode
87  * * MA_STATE_REBALANCE		- Indicate a rebalance during bulk insert
88  * * MA_STATE_PREALLOC		- Preallocated nodes, WARN_ON allocation
89  */
90 #define MA_STATE_BULK		1
91 #define MA_STATE_REBALANCE	2
92 #define MA_STATE_PREALLOC	4
93 
94 #define ma_parent_ptr(x) ((struct maple_pnode *)(x))
95 #define mas_tree_parent(x) ((unsigned long)(x->tree) | MA_ROOT_PARENT)
96 #define ma_mnode_ptr(x) ((struct maple_node *)(x))
97 #define ma_enode_ptr(x) ((struct maple_enode *)(x))
98 static struct kmem_cache *maple_node_cache;
99 
100 #ifdef CONFIG_DEBUG_MAPLE_TREE
101 static const unsigned long mt_max[] = {
102 	[maple_dense]		= MAPLE_NODE_SLOTS,
103 	[maple_leaf_64]		= ULONG_MAX,
104 	[maple_range_64]	= ULONG_MAX,
105 	[maple_arange_64]	= ULONG_MAX,
106 };
107 #define mt_node_max(x) mt_max[mte_node_type(x)]
108 #endif
109 
110 static const unsigned char mt_slots[] = {
111 	[maple_dense]		= MAPLE_NODE_SLOTS,
112 	[maple_leaf_64]		= MAPLE_RANGE64_SLOTS,
113 	[maple_range_64]	= MAPLE_RANGE64_SLOTS,
114 	[maple_arange_64]	= MAPLE_ARANGE64_SLOTS,
115 };
116 #define mt_slot_count(x) mt_slots[mte_node_type(x)]
117 
118 static const unsigned char mt_pivots[] = {
119 	[maple_dense]		= 0,
120 	[maple_leaf_64]		= MAPLE_RANGE64_SLOTS - 1,
121 	[maple_range_64]	= MAPLE_RANGE64_SLOTS - 1,
122 	[maple_arange_64]	= MAPLE_ARANGE64_SLOTS - 1,
123 };
124 #define mt_pivot_count(x) mt_pivots[mte_node_type(x)]
125 
126 static const unsigned char mt_min_slots[] = {
127 	[maple_dense]		= MAPLE_NODE_SLOTS / 2,
128 	[maple_leaf_64]		= (MAPLE_RANGE64_SLOTS / 2) - 2,
129 	[maple_range_64]	= (MAPLE_RANGE64_SLOTS / 2) - 2,
130 	[maple_arange_64]	= (MAPLE_ARANGE64_SLOTS / 2) - 1,
131 };
132 #define mt_min_slot_count(x) mt_min_slots[mte_node_type(x)]
133 
134 #define MAPLE_BIG_NODE_SLOTS	(MAPLE_RANGE64_SLOTS * 2 + 2)
135 #define MAPLE_BIG_NODE_GAPS	(MAPLE_ARANGE64_SLOTS * 2 + 1)
136 
137 struct maple_big_node {
138 	unsigned long pivot[MAPLE_BIG_NODE_SLOTS - 1];
139 	union {
140 		struct maple_enode *slot[MAPLE_BIG_NODE_SLOTS];
141 		struct {
142 			unsigned long padding[MAPLE_BIG_NODE_GAPS];
143 			unsigned long gap[MAPLE_BIG_NODE_GAPS];
144 		};
145 	};
146 	unsigned char b_end;
147 	enum maple_type type;
148 };
149 
150 /*
151  * The maple_subtree_state is used to build a tree to replace a segment of an
152  * existing tree in a more atomic way.  Any walkers of the older tree will hit a
153  * dead node and restart on updates.
154  */
155 struct maple_subtree_state {
156 	struct ma_state *orig_l;	/* Original left side of subtree */
157 	struct ma_state *orig_r;	/* Original right side of subtree */
158 	struct ma_state *l;		/* New left side of subtree */
159 	struct ma_state *m;		/* New middle of subtree (rare) */
160 	struct ma_state *r;		/* New right side of subtree */
161 	struct ma_topiary *free;	/* nodes to be freed */
162 	struct ma_topiary *destroy;	/* Nodes to be destroyed (walked and freed) */
163 	struct maple_big_node *bn;
164 };
165 
166 #ifdef CONFIG_KASAN_STACK
167 /* Prevent mas_wr_bnode() from exceeding the stack frame limit */
168 #define noinline_for_kasan noinline_for_stack
169 #else
170 #define noinline_for_kasan inline
171 #endif
172 
173 /* Functions */
174 static inline struct maple_node *mt_alloc_one(gfp_t gfp)
175 {
176 	return kmem_cache_alloc(maple_node_cache, gfp);
177 }
178 
179 static inline int mt_alloc_bulk(gfp_t gfp, size_t size, void **nodes)
180 {
181 	return kmem_cache_alloc_bulk(maple_node_cache, gfp, size, nodes);
182 }
183 
184 static inline void mt_free_one(struct maple_node *node)
185 {
186 	kmem_cache_free(maple_node_cache, node);
187 }
188 
189 static inline void mt_free_bulk(size_t size, void __rcu **nodes)
190 {
191 	kmem_cache_free_bulk(maple_node_cache, size, (void **)nodes);
192 }
193 
194 static void mt_free_rcu(struct rcu_head *head)
195 {
196 	struct maple_node *node = container_of(head, struct maple_node, rcu);
197 
198 	kmem_cache_free(maple_node_cache, node);
199 }
200 
201 /*
202  * ma_free_rcu() - Use rcu callback to free a maple node
203  * @node: The node to free
204  *
205  * The maple tree uses the parent pointer to indicate this node is no longer in
206  * use and will be freed.
207  */
208 static void ma_free_rcu(struct maple_node *node)
209 {
210 	WARN_ON(node->parent != ma_parent_ptr(node));
211 	call_rcu(&node->rcu, mt_free_rcu);
212 }
213 
214 static void mt_set_height(struct maple_tree *mt, unsigned char height)
215 {
216 	unsigned int new_flags = mt->ma_flags;
217 
218 	new_flags &= ~MT_FLAGS_HEIGHT_MASK;
219 	MT_BUG_ON(mt, height > MAPLE_HEIGHT_MAX);
220 	new_flags |= height << MT_FLAGS_HEIGHT_OFFSET;
221 	mt->ma_flags = new_flags;
222 }
223 
224 static unsigned int mas_mt_height(struct ma_state *mas)
225 {
226 	return mt_height(mas->tree);
227 }
228 
229 static inline unsigned int mt_attr(struct maple_tree *mt)
230 {
231 	return mt->ma_flags & ~MT_FLAGS_HEIGHT_MASK;
232 }
233 
234 static __always_inline enum maple_type mte_node_type(
235 		const struct maple_enode *entry)
236 {
237 	return ((unsigned long)entry >> MAPLE_NODE_TYPE_SHIFT) &
238 		MAPLE_NODE_TYPE_MASK;
239 }
240 
241 static __always_inline bool ma_is_dense(const enum maple_type type)
242 {
243 	return type < maple_leaf_64;
244 }
245 
246 static __always_inline bool ma_is_leaf(const enum maple_type type)
247 {
248 	return type < maple_range_64;
249 }
250 
251 static __always_inline bool mte_is_leaf(const struct maple_enode *entry)
252 {
253 	return ma_is_leaf(mte_node_type(entry));
254 }
255 
256 /*
257  * We also reserve values with the bottom two bits set to '10' which are
258  * below 4096
259  */
260 static __always_inline bool mt_is_reserved(const void *entry)
261 {
262 	return ((unsigned long)entry < MAPLE_RESERVED_RANGE) &&
263 		xa_is_internal(entry);
264 }
265 
266 static __always_inline void mas_set_err(struct ma_state *mas, long err)
267 {
268 	mas->node = MA_ERROR(err);
269 	mas->status = ma_error;
270 }
271 
272 static __always_inline bool mas_is_ptr(const struct ma_state *mas)
273 {
274 	return mas->status == ma_root;
275 }
276 
277 static __always_inline bool mas_is_start(const struct ma_state *mas)
278 {
279 	return mas->status == ma_start;
280 }
281 
282 static __always_inline bool mas_is_none(const struct ma_state *mas)
283 {
284 	return mas->status == ma_none;
285 }
286 
287 static __always_inline bool mas_is_paused(const struct ma_state *mas)
288 {
289 	return mas->status == ma_pause;
290 }
291 
292 static __always_inline bool mas_is_overflow(struct ma_state *mas)
293 {
294 	return mas->status == ma_overflow;
295 }
296 
297 static inline bool mas_is_underflow(struct ma_state *mas)
298 {
299 	return mas->status == ma_underflow;
300 }
301 
302 static __always_inline struct maple_node *mte_to_node(
303 		const struct maple_enode *entry)
304 {
305 	return (struct maple_node *)((unsigned long)entry & ~MAPLE_NODE_MASK);
306 }
307 
308 /*
309  * mte_to_mat() - Convert a maple encoded node to a maple topiary node.
310  * @entry: The maple encoded node
311  *
312  * Return: a maple topiary pointer
313  */
314 static inline struct maple_topiary *mte_to_mat(const struct maple_enode *entry)
315 {
316 	return (struct maple_topiary *)
317 		((unsigned long)entry & ~MAPLE_NODE_MASK);
318 }
319 
320 /*
321  * mas_mn() - Get the maple state node.
322  * @mas: The maple state
323  *
324  * Return: the maple node (not encoded - bare pointer).
325  */
326 static inline struct maple_node *mas_mn(const struct ma_state *mas)
327 {
328 	return mte_to_node(mas->node);
329 }
330 
331 /*
332  * mte_set_node_dead() - Set a maple encoded node as dead.
333  * @mn: The maple encoded node.
334  */
335 static inline void mte_set_node_dead(struct maple_enode *mn)
336 {
337 	mte_to_node(mn)->parent = ma_parent_ptr(mte_to_node(mn));
338 	smp_wmb(); /* Needed for RCU */
339 }
340 
341 /* Bit 1 indicates the root is a node */
342 #define MAPLE_ROOT_NODE			0x02
343 /* maple_type stored bit 3-6 */
344 #define MAPLE_ENODE_TYPE_SHIFT		0x03
345 /* Bit 2 means a NULL somewhere below */
346 #define MAPLE_ENODE_NULL		0x04
347 
348 static inline struct maple_enode *mt_mk_node(const struct maple_node *node,
349 					     enum maple_type type)
350 {
351 	return (void *)((unsigned long)node |
352 			(type << MAPLE_ENODE_TYPE_SHIFT) | MAPLE_ENODE_NULL);
353 }
354 
355 static inline void *mte_mk_root(const struct maple_enode *node)
356 {
357 	return (void *)((unsigned long)node | MAPLE_ROOT_NODE);
358 }
359 
360 static inline void *mte_safe_root(const struct maple_enode *node)
361 {
362 	return (void *)((unsigned long)node & ~MAPLE_ROOT_NODE);
363 }
364 
365 static inline void __maybe_unused *mte_set_full(const struct maple_enode *node)
366 {
367 	return (void *)((unsigned long)node & ~MAPLE_ENODE_NULL);
368 }
369 
370 static inline void __maybe_unused *mte_clear_full(const struct maple_enode *node)
371 {
372 	return (void *)((unsigned long)node | MAPLE_ENODE_NULL);
373 }
374 
375 static inline bool __maybe_unused mte_has_null(const struct maple_enode *node)
376 {
377 	return (unsigned long)node & MAPLE_ENODE_NULL;
378 }
379 
380 static __always_inline bool ma_is_root(struct maple_node *node)
381 {
382 	return ((unsigned long)node->parent & MA_ROOT_PARENT);
383 }
384 
385 static __always_inline bool mte_is_root(const struct maple_enode *node)
386 {
387 	return ma_is_root(mte_to_node(node));
388 }
389 
390 static inline bool mas_is_root_limits(const struct ma_state *mas)
391 {
392 	return !mas->min && mas->max == ULONG_MAX;
393 }
394 
395 static __always_inline bool mt_is_alloc(struct maple_tree *mt)
396 {
397 	return (mt->ma_flags & MT_FLAGS_ALLOC_RANGE);
398 }
399 
400 /*
401  * The Parent Pointer
402  * Excluding root, the parent pointer is 256B aligned like all other tree nodes.
403  * When storing a 32 or 64 bit values, the offset can fit into 5 bits.  The 16
404  * bit values need an extra bit to store the offset.  This extra bit comes from
405  * a reuse of the last bit in the node type.  This is possible by using bit 1 to
406  * indicate if bit 2 is part of the type or the slot.
407  *
408  * Note types:
409  *  0x??1 = Root
410  *  0x?00 = 16 bit nodes
411  *  0x010 = 32 bit nodes
412  *  0x110 = 64 bit nodes
413  *
414  * Slot size and alignment
415  *  0b??1 : Root
416  *  0b?00 : 16 bit values, type in 0-1, slot in 2-7
417  *  0b010 : 32 bit values, type in 0-2, slot in 3-7
418  *  0b110 : 64 bit values, type in 0-2, slot in 3-7
419  */
420 
421 #define MAPLE_PARENT_ROOT		0x01
422 
423 #define MAPLE_PARENT_SLOT_SHIFT		0x03
424 #define MAPLE_PARENT_SLOT_MASK		0xF8
425 
426 #define MAPLE_PARENT_16B_SLOT_SHIFT	0x02
427 #define MAPLE_PARENT_16B_SLOT_MASK	0xFC
428 
429 #define MAPLE_PARENT_RANGE64		0x06
430 #define MAPLE_PARENT_RANGE32		0x04
431 #define MAPLE_PARENT_NOT_RANGE16	0x02
432 
433 /*
434  * mte_parent_shift() - Get the parent shift for the slot storage.
435  * @parent: The parent pointer cast as an unsigned long
436  * Return: The shift into that pointer to the star to of the slot
437  */
438 static inline unsigned long mte_parent_shift(unsigned long parent)
439 {
440 	/* Note bit 1 == 0 means 16B */
441 	if (likely(parent & MAPLE_PARENT_NOT_RANGE16))
442 		return MAPLE_PARENT_SLOT_SHIFT;
443 
444 	return MAPLE_PARENT_16B_SLOT_SHIFT;
445 }
446 
447 /*
448  * mte_parent_slot_mask() - Get the slot mask for the parent.
449  * @parent: The parent pointer cast as an unsigned long.
450  * Return: The slot mask for that parent.
451  */
452 static inline unsigned long mte_parent_slot_mask(unsigned long parent)
453 {
454 	/* Note bit 1 == 0 means 16B */
455 	if (likely(parent & MAPLE_PARENT_NOT_RANGE16))
456 		return MAPLE_PARENT_SLOT_MASK;
457 
458 	return MAPLE_PARENT_16B_SLOT_MASK;
459 }
460 
461 /*
462  * mas_parent_type() - Return the maple_type of the parent from the stored
463  * parent type.
464  * @mas: The maple state
465  * @enode: The maple_enode to extract the parent's enum
466  * Return: The node->parent maple_type
467  */
468 static inline
469 enum maple_type mas_parent_type(struct ma_state *mas, struct maple_enode *enode)
470 {
471 	unsigned long p_type;
472 
473 	p_type = (unsigned long)mte_to_node(enode)->parent;
474 	if (WARN_ON(p_type & MAPLE_PARENT_ROOT))
475 		return 0;
476 
477 	p_type &= MAPLE_NODE_MASK;
478 	p_type &= ~mte_parent_slot_mask(p_type);
479 	switch (p_type) {
480 	case MAPLE_PARENT_RANGE64: /* or MAPLE_PARENT_ARANGE64 */
481 		if (mt_is_alloc(mas->tree))
482 			return maple_arange_64;
483 		return maple_range_64;
484 	}
485 
486 	return 0;
487 }
488 
489 /*
490  * mas_set_parent() - Set the parent node and encode the slot
491  * @mas: The maple state
492  * @enode: The encoded maple node.
493  * @parent: The encoded maple node that is the parent of @enode.
494  * @slot: The slot that @enode resides in @parent.
495  *
496  * Slot number is encoded in the enode->parent bit 3-6 or 2-6, depending on the
497  * parent type.
498  */
499 static inline
500 void mas_set_parent(struct ma_state *mas, struct maple_enode *enode,
501 		    const struct maple_enode *parent, unsigned char slot)
502 {
503 	unsigned long val = (unsigned long)parent;
504 	unsigned long shift;
505 	unsigned long type;
506 	enum maple_type p_type = mte_node_type(parent);
507 
508 	MAS_BUG_ON(mas, p_type == maple_dense);
509 	MAS_BUG_ON(mas, p_type == maple_leaf_64);
510 
511 	switch (p_type) {
512 	case maple_range_64:
513 	case maple_arange_64:
514 		shift = MAPLE_PARENT_SLOT_SHIFT;
515 		type = MAPLE_PARENT_RANGE64;
516 		break;
517 	default:
518 	case maple_dense:
519 	case maple_leaf_64:
520 		shift = type = 0;
521 		break;
522 	}
523 
524 	val &= ~MAPLE_NODE_MASK; /* Clear all node metadata in parent */
525 	val |= (slot << shift) | type;
526 	mte_to_node(enode)->parent = ma_parent_ptr(val);
527 }
528 
529 /*
530  * mte_parent_slot() - get the parent slot of @enode.
531  * @enode: The encoded maple node.
532  *
533  * Return: The slot in the parent node where @enode resides.
534  */
535 static __always_inline
536 unsigned int mte_parent_slot(const struct maple_enode *enode)
537 {
538 	unsigned long val = (unsigned long)mte_to_node(enode)->parent;
539 
540 	if (unlikely(val & MA_ROOT_PARENT))
541 		return 0;
542 
543 	/*
544 	 * Okay to use MAPLE_PARENT_16B_SLOT_MASK as the last bit will be lost
545 	 * by shift if the parent shift is MAPLE_PARENT_SLOT_SHIFT
546 	 */
547 	return (val & MAPLE_PARENT_16B_SLOT_MASK) >> mte_parent_shift(val);
548 }
549 
550 /*
551  * mte_parent() - Get the parent of @node.
552  * @enode: The encoded maple node.
553  *
554  * Return: The parent maple node.
555  */
556 static __always_inline
557 struct maple_node *mte_parent(const struct maple_enode *enode)
558 {
559 	return (void *)((unsigned long)
560 			(mte_to_node(enode)->parent) & ~MAPLE_NODE_MASK);
561 }
562 
563 /*
564  * ma_dead_node() - check if the @enode is dead.
565  * @enode: The encoded maple node
566  *
567  * Return: true if dead, false otherwise.
568  */
569 static __always_inline bool ma_dead_node(const struct maple_node *node)
570 {
571 	struct maple_node *parent;
572 
573 	/* Do not reorder reads from the node prior to the parent check */
574 	smp_rmb();
575 	parent = (void *)((unsigned long) node->parent & ~MAPLE_NODE_MASK);
576 	return (parent == node);
577 }
578 
579 /*
580  * mte_dead_node() - check if the @enode is dead.
581  * @enode: The encoded maple node
582  *
583  * Return: true if dead, false otherwise.
584  */
585 static __always_inline bool mte_dead_node(const struct maple_enode *enode)
586 {
587 	struct maple_node *node;
588 
589 	node = mte_to_node(enode);
590 	return ma_dead_node(node);
591 }
592 
593 /*
594  * mas_allocated() - Get the number of nodes allocated in a maple state.
595  * @mas: The maple state
596  *
597  * The ma_state alloc member is overloaded to hold a pointer to the first
598  * allocated node or to the number of requested nodes to allocate.  If bit 0 is
599  * set, then the alloc contains the number of requested nodes.  If there is an
600  * allocated node, then the total allocated nodes is in that node.
601  *
602  * Return: The total number of nodes allocated
603  */
604 static inline unsigned long mas_allocated(const struct ma_state *mas)
605 {
606 	if (!mas->alloc || ((unsigned long)mas->alloc & 0x1))
607 		return 0;
608 
609 	return mas->alloc->total;
610 }
611 
612 /*
613  * mas_set_alloc_req() - Set the requested number of allocations.
614  * @mas: the maple state
615  * @count: the number of allocations.
616  *
617  * The requested number of allocations is either in the first allocated node,
618  * located in @mas->alloc->request_count, or directly in @mas->alloc if there is
619  * no allocated node.  Set the request either in the node or do the necessary
620  * encoding to store in @mas->alloc directly.
621  */
622 static inline void mas_set_alloc_req(struct ma_state *mas, unsigned long count)
623 {
624 	if (!mas->alloc || ((unsigned long)mas->alloc & 0x1)) {
625 		if (!count)
626 			mas->alloc = NULL;
627 		else
628 			mas->alloc = (struct maple_alloc *)(((count) << 1U) | 1U);
629 		return;
630 	}
631 
632 	mas->alloc->request_count = count;
633 }
634 
635 /*
636  * mas_alloc_req() - get the requested number of allocations.
637  * @mas: The maple state
638  *
639  * The alloc count is either stored directly in @mas, or in
640  * @mas->alloc->request_count if there is at least one node allocated.  Decode
641  * the request count if it's stored directly in @mas->alloc.
642  *
643  * Return: The allocation request count.
644  */
645 static inline unsigned int mas_alloc_req(const struct ma_state *mas)
646 {
647 	if ((unsigned long)mas->alloc & 0x1)
648 		return (unsigned long)(mas->alloc) >> 1;
649 	else if (mas->alloc)
650 		return mas->alloc->request_count;
651 	return 0;
652 }
653 
654 /*
655  * ma_pivots() - Get a pointer to the maple node pivots.
656  * @node: the maple node
657  * @type: the node type
658  *
659  * In the event of a dead node, this array may be %NULL
660  *
661  * Return: A pointer to the maple node pivots
662  */
663 static inline unsigned long *ma_pivots(struct maple_node *node,
664 					   enum maple_type type)
665 {
666 	switch (type) {
667 	case maple_arange_64:
668 		return node->ma64.pivot;
669 	case maple_range_64:
670 	case maple_leaf_64:
671 		return node->mr64.pivot;
672 	case maple_dense:
673 		return NULL;
674 	}
675 	return NULL;
676 }
677 
678 /*
679  * ma_gaps() - Get a pointer to the maple node gaps.
680  * @node: the maple node
681  * @type: the node type
682  *
683  * Return: A pointer to the maple node gaps
684  */
685 static inline unsigned long *ma_gaps(struct maple_node *node,
686 				     enum maple_type type)
687 {
688 	switch (type) {
689 	case maple_arange_64:
690 		return node->ma64.gap;
691 	case maple_range_64:
692 	case maple_leaf_64:
693 	case maple_dense:
694 		return NULL;
695 	}
696 	return NULL;
697 }
698 
699 /*
700  * mas_safe_pivot() - get the pivot at @piv or mas->max.
701  * @mas: The maple state
702  * @pivots: The pointer to the maple node pivots
703  * @piv: The pivot to fetch
704  * @type: The maple node type
705  *
706  * Return: The pivot at @piv within the limit of the @pivots array, @mas->max
707  * otherwise.
708  */
709 static __always_inline unsigned long
710 mas_safe_pivot(const struct ma_state *mas, unsigned long *pivots,
711 	       unsigned char piv, enum maple_type type)
712 {
713 	if (piv >= mt_pivots[type])
714 		return mas->max;
715 
716 	return pivots[piv];
717 }
718 
719 /*
720  * mas_safe_min() - Return the minimum for a given offset.
721  * @mas: The maple state
722  * @pivots: The pointer to the maple node pivots
723  * @offset: The offset into the pivot array
724  *
725  * Return: The minimum range value that is contained in @offset.
726  */
727 static inline unsigned long
728 mas_safe_min(struct ma_state *mas, unsigned long *pivots, unsigned char offset)
729 {
730 	if (likely(offset))
731 		return pivots[offset - 1] + 1;
732 
733 	return mas->min;
734 }
735 
736 /*
737  * mte_set_pivot() - Set a pivot to a value in an encoded maple node.
738  * @mn: The encoded maple node
739  * @piv: The pivot offset
740  * @val: The value of the pivot
741  */
742 static inline void mte_set_pivot(struct maple_enode *mn, unsigned char piv,
743 				unsigned long val)
744 {
745 	struct maple_node *node = mte_to_node(mn);
746 	enum maple_type type = mte_node_type(mn);
747 
748 	BUG_ON(piv >= mt_pivots[type]);
749 	switch (type) {
750 	case maple_range_64:
751 	case maple_leaf_64:
752 		node->mr64.pivot[piv] = val;
753 		break;
754 	case maple_arange_64:
755 		node->ma64.pivot[piv] = val;
756 		break;
757 	case maple_dense:
758 		break;
759 	}
760 
761 }
762 
763 /*
764  * ma_slots() - Get a pointer to the maple node slots.
765  * @mn: The maple node
766  * @mt: The maple node type
767  *
768  * Return: A pointer to the maple node slots
769  */
770 static inline void __rcu **ma_slots(struct maple_node *mn, enum maple_type mt)
771 {
772 	switch (mt) {
773 	case maple_arange_64:
774 		return mn->ma64.slot;
775 	case maple_range_64:
776 	case maple_leaf_64:
777 		return mn->mr64.slot;
778 	case maple_dense:
779 		return mn->slot;
780 	}
781 
782 	return NULL;
783 }
784 
785 static inline bool mt_write_locked(const struct maple_tree *mt)
786 {
787 	return mt_external_lock(mt) ? mt_write_lock_is_held(mt) :
788 		lockdep_is_held(&mt->ma_lock);
789 }
790 
791 static __always_inline bool mt_locked(const struct maple_tree *mt)
792 {
793 	return mt_external_lock(mt) ? mt_lock_is_held(mt) :
794 		lockdep_is_held(&mt->ma_lock);
795 }
796 
797 static __always_inline void *mt_slot(const struct maple_tree *mt,
798 		void __rcu **slots, unsigned char offset)
799 {
800 	return rcu_dereference_check(slots[offset], mt_locked(mt));
801 }
802 
803 static __always_inline void *mt_slot_locked(struct maple_tree *mt,
804 		void __rcu **slots, unsigned char offset)
805 {
806 	return rcu_dereference_protected(slots[offset], mt_write_locked(mt));
807 }
808 /*
809  * mas_slot_locked() - Get the slot value when holding the maple tree lock.
810  * @mas: The maple state
811  * @slots: The pointer to the slots
812  * @offset: The offset into the slots array to fetch
813  *
814  * Return: The entry stored in @slots at the @offset.
815  */
816 static __always_inline void *mas_slot_locked(struct ma_state *mas,
817 		void __rcu **slots, unsigned char offset)
818 {
819 	return mt_slot_locked(mas->tree, slots, offset);
820 }
821 
822 /*
823  * mas_slot() - Get the slot value when not holding the maple tree lock.
824  * @mas: The maple state
825  * @slots: The pointer to the slots
826  * @offset: The offset into the slots array to fetch
827  *
828  * Return: The entry stored in @slots at the @offset
829  */
830 static __always_inline void *mas_slot(struct ma_state *mas, void __rcu **slots,
831 		unsigned char offset)
832 {
833 	return mt_slot(mas->tree, slots, offset);
834 }
835 
836 /*
837  * mas_root() - Get the maple tree root.
838  * @mas: The maple state.
839  *
840  * Return: The pointer to the root of the tree
841  */
842 static __always_inline void *mas_root(struct ma_state *mas)
843 {
844 	return rcu_dereference_check(mas->tree->ma_root, mt_locked(mas->tree));
845 }
846 
847 static inline void *mt_root_locked(struct maple_tree *mt)
848 {
849 	return rcu_dereference_protected(mt->ma_root, mt_write_locked(mt));
850 }
851 
852 /*
853  * mas_root_locked() - Get the maple tree root when holding the maple tree lock.
854  * @mas: The maple state.
855  *
856  * Return: The pointer to the root of the tree
857  */
858 static inline void *mas_root_locked(struct ma_state *mas)
859 {
860 	return mt_root_locked(mas->tree);
861 }
862 
863 static inline struct maple_metadata *ma_meta(struct maple_node *mn,
864 					     enum maple_type mt)
865 {
866 	switch (mt) {
867 	case maple_arange_64:
868 		return &mn->ma64.meta;
869 	default:
870 		return &mn->mr64.meta;
871 	}
872 }
873 
874 /*
875  * ma_set_meta() - Set the metadata information of a node.
876  * @mn: The maple node
877  * @mt: The maple node type
878  * @offset: The offset of the highest sub-gap in this node.
879  * @end: The end of the data in this node.
880  */
881 static inline void ma_set_meta(struct maple_node *mn, enum maple_type mt,
882 			       unsigned char offset, unsigned char end)
883 {
884 	struct maple_metadata *meta = ma_meta(mn, mt);
885 
886 	meta->gap = offset;
887 	meta->end = end;
888 }
889 
890 /*
891  * mt_clear_meta() - clear the metadata information of a node, if it exists
892  * @mt: The maple tree
893  * @mn: The maple node
894  * @type: The maple node type
895  */
896 static inline void mt_clear_meta(struct maple_tree *mt, struct maple_node *mn,
897 				  enum maple_type type)
898 {
899 	struct maple_metadata *meta;
900 	unsigned long *pivots;
901 	void __rcu **slots;
902 	void *next;
903 
904 	switch (type) {
905 	case maple_range_64:
906 		pivots = mn->mr64.pivot;
907 		if (unlikely(pivots[MAPLE_RANGE64_SLOTS - 2])) {
908 			slots = mn->mr64.slot;
909 			next = mt_slot_locked(mt, slots,
910 					      MAPLE_RANGE64_SLOTS - 1);
911 			if (unlikely((mte_to_node(next) &&
912 				      mte_node_type(next))))
913 				return; /* no metadata, could be node */
914 		}
915 		fallthrough;
916 	case maple_arange_64:
917 		meta = ma_meta(mn, type);
918 		break;
919 	default:
920 		return;
921 	}
922 
923 	meta->gap = 0;
924 	meta->end = 0;
925 }
926 
927 /*
928  * ma_meta_end() - Get the data end of a node from the metadata
929  * @mn: The maple node
930  * @mt: The maple node type
931  */
932 static inline unsigned char ma_meta_end(struct maple_node *mn,
933 					enum maple_type mt)
934 {
935 	struct maple_metadata *meta = ma_meta(mn, mt);
936 
937 	return meta->end;
938 }
939 
940 /*
941  * ma_meta_gap() - Get the largest gap location of a node from the metadata
942  * @mn: The maple node
943  */
944 static inline unsigned char ma_meta_gap(struct maple_node *mn)
945 {
946 	return mn->ma64.meta.gap;
947 }
948 
949 /*
950  * ma_set_meta_gap() - Set the largest gap location in a nodes metadata
951  * @mn: The maple node
952  * @mt: The maple node type
953  * @offset: The location of the largest gap.
954  */
955 static inline void ma_set_meta_gap(struct maple_node *mn, enum maple_type mt,
956 				   unsigned char offset)
957 {
958 
959 	struct maple_metadata *meta = ma_meta(mn, mt);
960 
961 	meta->gap = offset;
962 }
963 
964 /*
965  * mat_add() - Add a @dead_enode to the ma_topiary of a list of dead nodes.
966  * @mat: the ma_topiary, a linked list of dead nodes.
967  * @dead_enode: the node to be marked as dead and added to the tail of the list
968  *
969  * Add the @dead_enode to the linked list in @mat.
970  */
971 static inline void mat_add(struct ma_topiary *mat,
972 			   struct maple_enode *dead_enode)
973 {
974 	mte_set_node_dead(dead_enode);
975 	mte_to_mat(dead_enode)->next = NULL;
976 	if (!mat->tail) {
977 		mat->tail = mat->head = dead_enode;
978 		return;
979 	}
980 
981 	mte_to_mat(mat->tail)->next = dead_enode;
982 	mat->tail = dead_enode;
983 }
984 
985 static void mt_free_walk(struct rcu_head *head);
986 static void mt_destroy_walk(struct maple_enode *enode, struct maple_tree *mt,
987 			    bool free);
988 /*
989  * mas_mat_destroy() - Free all nodes and subtrees in a dead list.
990  * @mas: the maple state
991  * @mat: the ma_topiary linked list of dead nodes to free.
992  *
993  * Destroy walk a dead list.
994  */
995 static void mas_mat_destroy(struct ma_state *mas, struct ma_topiary *mat)
996 {
997 	struct maple_enode *next;
998 	struct maple_node *node;
999 	bool in_rcu = mt_in_rcu(mas->tree);
1000 
1001 	while (mat->head) {
1002 		next = mte_to_mat(mat->head)->next;
1003 		node = mte_to_node(mat->head);
1004 		mt_destroy_walk(mat->head, mas->tree, !in_rcu);
1005 		if (in_rcu)
1006 			call_rcu(&node->rcu, mt_free_walk);
1007 		mat->head = next;
1008 	}
1009 }
1010 /*
1011  * mas_descend() - Descend into the slot stored in the ma_state.
1012  * @mas: the maple state.
1013  *
1014  * Note: Not RCU safe, only use in write side or debug code.
1015  */
1016 static inline void mas_descend(struct ma_state *mas)
1017 {
1018 	enum maple_type type;
1019 	unsigned long *pivots;
1020 	struct maple_node *node;
1021 	void __rcu **slots;
1022 
1023 	node = mas_mn(mas);
1024 	type = mte_node_type(mas->node);
1025 	pivots = ma_pivots(node, type);
1026 	slots = ma_slots(node, type);
1027 
1028 	if (mas->offset)
1029 		mas->min = pivots[mas->offset - 1] + 1;
1030 	mas->max = mas_safe_pivot(mas, pivots, mas->offset, type);
1031 	mas->node = mas_slot(mas, slots, mas->offset);
1032 }
1033 
1034 /*
1035  * mte_set_gap() - Set a maple node gap.
1036  * @mn: The encoded maple node
1037  * @gap: The offset of the gap to set
1038  * @val: The gap value
1039  */
1040 static inline void mte_set_gap(const struct maple_enode *mn,
1041 				 unsigned char gap, unsigned long val)
1042 {
1043 	switch (mte_node_type(mn)) {
1044 	default:
1045 		break;
1046 	case maple_arange_64:
1047 		mte_to_node(mn)->ma64.gap[gap] = val;
1048 		break;
1049 	}
1050 }
1051 
1052 /*
1053  * mas_ascend() - Walk up a level of the tree.
1054  * @mas: The maple state
1055  *
1056  * Sets the @mas->max and @mas->min to the correct values when walking up.  This
1057  * may cause several levels of walking up to find the correct min and max.
1058  * May find a dead node which will cause a premature return.
1059  * Return: 1 on dead node, 0 otherwise
1060  */
1061 static int mas_ascend(struct ma_state *mas)
1062 {
1063 	struct maple_enode *p_enode; /* parent enode. */
1064 	struct maple_enode *a_enode; /* ancestor enode. */
1065 	struct maple_node *a_node; /* ancestor node. */
1066 	struct maple_node *p_node; /* parent node. */
1067 	unsigned char a_slot;
1068 	enum maple_type a_type;
1069 	unsigned long min, max;
1070 	unsigned long *pivots;
1071 	bool set_max = false, set_min = false;
1072 
1073 	a_node = mas_mn(mas);
1074 	if (ma_is_root(a_node)) {
1075 		mas->offset = 0;
1076 		return 0;
1077 	}
1078 
1079 	p_node = mte_parent(mas->node);
1080 	if (unlikely(a_node == p_node))
1081 		return 1;
1082 
1083 	a_type = mas_parent_type(mas, mas->node);
1084 	mas->offset = mte_parent_slot(mas->node);
1085 	a_enode = mt_mk_node(p_node, a_type);
1086 
1087 	/* Check to make sure all parent information is still accurate */
1088 	if (p_node != mte_parent(mas->node))
1089 		return 1;
1090 
1091 	mas->node = a_enode;
1092 
1093 	if (mte_is_root(a_enode)) {
1094 		mas->max = ULONG_MAX;
1095 		mas->min = 0;
1096 		return 0;
1097 	}
1098 
1099 	min = 0;
1100 	max = ULONG_MAX;
1101 	if (!mas->offset) {
1102 		min = mas->min;
1103 		set_min = true;
1104 	}
1105 
1106 	if (mas->max == ULONG_MAX)
1107 		set_max = true;
1108 
1109 	do {
1110 		p_enode = a_enode;
1111 		a_type = mas_parent_type(mas, p_enode);
1112 		a_node = mte_parent(p_enode);
1113 		a_slot = mte_parent_slot(p_enode);
1114 		a_enode = mt_mk_node(a_node, a_type);
1115 		pivots = ma_pivots(a_node, a_type);
1116 
1117 		if (unlikely(ma_dead_node(a_node)))
1118 			return 1;
1119 
1120 		if (!set_min && a_slot) {
1121 			set_min = true;
1122 			min = pivots[a_slot - 1] + 1;
1123 		}
1124 
1125 		if (!set_max && a_slot < mt_pivots[a_type]) {
1126 			set_max = true;
1127 			max = pivots[a_slot];
1128 		}
1129 
1130 		if (unlikely(ma_dead_node(a_node)))
1131 			return 1;
1132 
1133 		if (unlikely(ma_is_root(a_node)))
1134 			break;
1135 
1136 	} while (!set_min || !set_max);
1137 
1138 	mas->max = max;
1139 	mas->min = min;
1140 	return 0;
1141 }
1142 
1143 /*
1144  * mas_pop_node() - Get a previously allocated maple node from the maple state.
1145  * @mas: The maple state
1146  *
1147  * Return: A pointer to a maple node.
1148  */
1149 static inline struct maple_node *mas_pop_node(struct ma_state *mas)
1150 {
1151 	struct maple_alloc *ret, *node = mas->alloc;
1152 	unsigned long total = mas_allocated(mas);
1153 	unsigned int req = mas_alloc_req(mas);
1154 
1155 	/* nothing or a request pending. */
1156 	if (WARN_ON(!total))
1157 		return NULL;
1158 
1159 	if (total == 1) {
1160 		/* single allocation in this ma_state */
1161 		mas->alloc = NULL;
1162 		ret = node;
1163 		goto single_node;
1164 	}
1165 
1166 	if (node->node_count == 1) {
1167 		/* Single allocation in this node. */
1168 		mas->alloc = node->slot[0];
1169 		mas->alloc->total = node->total - 1;
1170 		ret = node;
1171 		goto new_head;
1172 	}
1173 	node->total--;
1174 	ret = node->slot[--node->node_count];
1175 	node->slot[node->node_count] = NULL;
1176 
1177 single_node:
1178 new_head:
1179 	if (req) {
1180 		req++;
1181 		mas_set_alloc_req(mas, req);
1182 	}
1183 
1184 	memset(ret, 0, sizeof(*ret));
1185 	return (struct maple_node *)ret;
1186 }
1187 
1188 /*
1189  * mas_push_node() - Push a node back on the maple state allocation.
1190  * @mas: The maple state
1191  * @used: The used maple node
1192  *
1193  * Stores the maple node back into @mas->alloc for reuse.  Updates allocated and
1194  * requested node count as necessary.
1195  */
1196 static inline void mas_push_node(struct ma_state *mas, struct maple_node *used)
1197 {
1198 	struct maple_alloc *reuse = (struct maple_alloc *)used;
1199 	struct maple_alloc *head = mas->alloc;
1200 	unsigned long count;
1201 	unsigned int requested = mas_alloc_req(mas);
1202 
1203 	count = mas_allocated(mas);
1204 
1205 	reuse->request_count = 0;
1206 	reuse->node_count = 0;
1207 	if (count) {
1208 		if (head->node_count < MAPLE_ALLOC_SLOTS) {
1209 			head->slot[head->node_count++] = reuse;
1210 			head->total++;
1211 			goto done;
1212 		}
1213 		reuse->slot[0] = head;
1214 		reuse->node_count = 1;
1215 	}
1216 
1217 	reuse->total = count + 1;
1218 	mas->alloc = reuse;
1219 done:
1220 	if (requested > 1)
1221 		mas_set_alloc_req(mas, requested - 1);
1222 }
1223 
1224 /*
1225  * mas_alloc_nodes() - Allocate nodes into a maple state
1226  * @mas: The maple state
1227  * @gfp: The GFP Flags
1228  */
1229 static inline void mas_alloc_nodes(struct ma_state *mas, gfp_t gfp)
1230 {
1231 	struct maple_alloc *node;
1232 	unsigned long allocated = mas_allocated(mas);
1233 	unsigned int requested = mas_alloc_req(mas);
1234 	unsigned int count;
1235 	void **slots = NULL;
1236 	unsigned int max_req = 0;
1237 
1238 	if (!requested)
1239 		return;
1240 
1241 	mas_set_alloc_req(mas, 0);
1242 	if (mas->mas_flags & MA_STATE_PREALLOC) {
1243 		if (allocated)
1244 			return;
1245 		WARN_ON(!allocated);
1246 	}
1247 
1248 	if (!allocated || mas->alloc->node_count == MAPLE_ALLOC_SLOTS) {
1249 		node = (struct maple_alloc *)mt_alloc_one(gfp);
1250 		if (!node)
1251 			goto nomem_one;
1252 
1253 		if (allocated) {
1254 			node->slot[0] = mas->alloc;
1255 			node->node_count = 1;
1256 		} else {
1257 			node->node_count = 0;
1258 		}
1259 
1260 		mas->alloc = node;
1261 		node->total = ++allocated;
1262 		node->request_count = 0;
1263 		requested--;
1264 	}
1265 
1266 	node = mas->alloc;
1267 	while (requested) {
1268 		max_req = MAPLE_ALLOC_SLOTS - node->node_count;
1269 		slots = (void **)&node->slot[node->node_count];
1270 		max_req = min(requested, max_req);
1271 		count = mt_alloc_bulk(gfp, max_req, slots);
1272 		if (!count)
1273 			goto nomem_bulk;
1274 
1275 		if (node->node_count == 0) {
1276 			node->slot[0]->node_count = 0;
1277 			node->slot[0]->request_count = 0;
1278 		}
1279 
1280 		node->node_count += count;
1281 		allocated += count;
1282 		/* find a non-full node*/
1283 		do {
1284 			node = node->slot[0];
1285 		} while (unlikely(node->node_count == MAPLE_ALLOC_SLOTS));
1286 		requested -= count;
1287 	}
1288 	mas->alloc->total = allocated;
1289 	return;
1290 
1291 nomem_bulk:
1292 	/* Clean up potential freed allocations on bulk failure */
1293 	memset(slots, 0, max_req * sizeof(unsigned long));
1294 	mas->alloc->total = allocated;
1295 nomem_one:
1296 	mas_set_alloc_req(mas, requested);
1297 	mas_set_err(mas, -ENOMEM);
1298 }
1299 
1300 /*
1301  * mas_free() - Free an encoded maple node
1302  * @mas: The maple state
1303  * @used: The encoded maple node to free.
1304  *
1305  * Uses rcu free if necessary, pushes @used back on the maple state allocations
1306  * otherwise.
1307  */
1308 static inline void mas_free(struct ma_state *mas, struct maple_enode *used)
1309 {
1310 	struct maple_node *tmp = mte_to_node(used);
1311 
1312 	if (mt_in_rcu(mas->tree))
1313 		ma_free_rcu(tmp);
1314 	else
1315 		mas_push_node(mas, tmp);
1316 }
1317 
1318 /*
1319  * mas_node_count_gfp() - Check if enough nodes are allocated and request more
1320  * if there is not enough nodes.
1321  * @mas: The maple state
1322  * @count: The number of nodes needed
1323  * @gfp: the gfp flags
1324  */
1325 static void mas_node_count_gfp(struct ma_state *mas, int count, gfp_t gfp)
1326 {
1327 	unsigned long allocated = mas_allocated(mas);
1328 
1329 	if (allocated < count) {
1330 		mas_set_alloc_req(mas, count - allocated);
1331 		mas_alloc_nodes(mas, gfp);
1332 	}
1333 }
1334 
1335 /*
1336  * mas_node_count() - Check if enough nodes are allocated and request more if
1337  * there is not enough nodes.
1338  * @mas: The maple state
1339  * @count: The number of nodes needed
1340  *
1341  * Note: Uses GFP_NOWAIT | __GFP_NOWARN for gfp flags.
1342  */
1343 static void mas_node_count(struct ma_state *mas, int count)
1344 {
1345 	return mas_node_count_gfp(mas, count, GFP_NOWAIT | __GFP_NOWARN);
1346 }
1347 
1348 /*
1349  * mas_start() - Sets up maple state for operations.
1350  * @mas: The maple state.
1351  *
1352  * If mas->status == ma_start, then set the min, max and depth to
1353  * defaults.
1354  *
1355  * Return:
1356  * - If mas->node is an error or not mas_start, return NULL.
1357  * - If it's an empty tree:     NULL & mas->status == ma_none
1358  * - If it's a single entry:    The entry & mas->status == ma_root
1359  * - If it's a tree:            NULL & mas->status == ma_active
1360  */
1361 static inline struct maple_enode *mas_start(struct ma_state *mas)
1362 {
1363 	if (likely(mas_is_start(mas))) {
1364 		struct maple_enode *root;
1365 
1366 		mas->min = 0;
1367 		mas->max = ULONG_MAX;
1368 
1369 retry:
1370 		mas->depth = 0;
1371 		root = mas_root(mas);
1372 		/* Tree with nodes */
1373 		if (likely(xa_is_node(root))) {
1374 			mas->depth = 0;
1375 			mas->status = ma_active;
1376 			mas->node = mte_safe_root(root);
1377 			mas->offset = 0;
1378 			if (mte_dead_node(mas->node))
1379 				goto retry;
1380 
1381 			return NULL;
1382 		}
1383 
1384 		mas->node = NULL;
1385 		/* empty tree */
1386 		if (unlikely(!root)) {
1387 			mas->status = ma_none;
1388 			mas->offset = MAPLE_NODE_SLOTS;
1389 			return NULL;
1390 		}
1391 
1392 		/* Single entry tree */
1393 		mas->status = ma_root;
1394 		mas->offset = MAPLE_NODE_SLOTS;
1395 
1396 		/* Single entry tree. */
1397 		if (mas->index > 0)
1398 			return NULL;
1399 
1400 		return root;
1401 	}
1402 
1403 	return NULL;
1404 }
1405 
1406 /*
1407  * ma_data_end() - Find the end of the data in a node.
1408  * @node: The maple node
1409  * @type: The maple node type
1410  * @pivots: The array of pivots in the node
1411  * @max: The maximum value in the node
1412  *
1413  * Uses metadata to find the end of the data when possible.
1414  * Return: The zero indexed last slot with data (may be null).
1415  */
1416 static __always_inline unsigned char ma_data_end(struct maple_node *node,
1417 		enum maple_type type, unsigned long *pivots, unsigned long max)
1418 {
1419 	unsigned char offset;
1420 
1421 	if (!pivots)
1422 		return 0;
1423 
1424 	if (type == maple_arange_64)
1425 		return ma_meta_end(node, type);
1426 
1427 	offset = mt_pivots[type] - 1;
1428 	if (likely(!pivots[offset]))
1429 		return ma_meta_end(node, type);
1430 
1431 	if (likely(pivots[offset] == max))
1432 		return offset;
1433 
1434 	return mt_pivots[type];
1435 }
1436 
1437 /*
1438  * mas_data_end() - Find the end of the data (slot).
1439  * @mas: the maple state
1440  *
1441  * This method is optimized to check the metadata of a node if the node type
1442  * supports data end metadata.
1443  *
1444  * Return: The zero indexed last slot with data (may be null).
1445  */
1446 static inline unsigned char mas_data_end(struct ma_state *mas)
1447 {
1448 	enum maple_type type;
1449 	struct maple_node *node;
1450 	unsigned char offset;
1451 	unsigned long *pivots;
1452 
1453 	type = mte_node_type(mas->node);
1454 	node = mas_mn(mas);
1455 	if (type == maple_arange_64)
1456 		return ma_meta_end(node, type);
1457 
1458 	pivots = ma_pivots(node, type);
1459 	if (unlikely(ma_dead_node(node)))
1460 		return 0;
1461 
1462 	offset = mt_pivots[type] - 1;
1463 	if (likely(!pivots[offset]))
1464 		return ma_meta_end(node, type);
1465 
1466 	if (likely(pivots[offset] == mas->max))
1467 		return offset;
1468 
1469 	return mt_pivots[type];
1470 }
1471 
1472 /*
1473  * mas_leaf_max_gap() - Returns the largest gap in a leaf node
1474  * @mas: the maple state
1475  *
1476  * Return: The maximum gap in the leaf.
1477  */
1478 static unsigned long mas_leaf_max_gap(struct ma_state *mas)
1479 {
1480 	enum maple_type mt;
1481 	unsigned long pstart, gap, max_gap;
1482 	struct maple_node *mn;
1483 	unsigned long *pivots;
1484 	void __rcu **slots;
1485 	unsigned char i;
1486 	unsigned char max_piv;
1487 
1488 	mt = mte_node_type(mas->node);
1489 	mn = mas_mn(mas);
1490 	slots = ma_slots(mn, mt);
1491 	max_gap = 0;
1492 	if (unlikely(ma_is_dense(mt))) {
1493 		gap = 0;
1494 		for (i = 0; i < mt_slots[mt]; i++) {
1495 			if (slots[i]) {
1496 				if (gap > max_gap)
1497 					max_gap = gap;
1498 				gap = 0;
1499 			} else {
1500 				gap++;
1501 			}
1502 		}
1503 		if (gap > max_gap)
1504 			max_gap = gap;
1505 		return max_gap;
1506 	}
1507 
1508 	/*
1509 	 * Check the first implied pivot optimizes the loop below and slot 1 may
1510 	 * be skipped if there is a gap in slot 0.
1511 	 */
1512 	pivots = ma_pivots(mn, mt);
1513 	if (likely(!slots[0])) {
1514 		max_gap = pivots[0] - mas->min + 1;
1515 		i = 2;
1516 	} else {
1517 		i = 1;
1518 	}
1519 
1520 	/* reduce max_piv as the special case is checked before the loop */
1521 	max_piv = ma_data_end(mn, mt, pivots, mas->max) - 1;
1522 	/*
1523 	 * Check end implied pivot which can only be a gap on the right most
1524 	 * node.
1525 	 */
1526 	if (unlikely(mas->max == ULONG_MAX) && !slots[max_piv + 1]) {
1527 		gap = ULONG_MAX - pivots[max_piv];
1528 		if (gap > max_gap)
1529 			max_gap = gap;
1530 
1531 		if (max_gap > pivots[max_piv] - mas->min)
1532 			return max_gap;
1533 	}
1534 
1535 	for (; i <= max_piv; i++) {
1536 		/* data == no gap. */
1537 		if (likely(slots[i]))
1538 			continue;
1539 
1540 		pstart = pivots[i - 1];
1541 		gap = pivots[i] - pstart;
1542 		if (gap > max_gap)
1543 			max_gap = gap;
1544 
1545 		/* There cannot be two gaps in a row. */
1546 		i++;
1547 	}
1548 	return max_gap;
1549 }
1550 
1551 /*
1552  * ma_max_gap() - Get the maximum gap in a maple node (non-leaf)
1553  * @node: The maple node
1554  * @gaps: The pointer to the gaps
1555  * @mt: The maple node type
1556  * @off: Pointer to store the offset location of the gap.
1557  *
1558  * Uses the metadata data end to scan backwards across set gaps.
1559  *
1560  * Return: The maximum gap value
1561  */
1562 static inline unsigned long
1563 ma_max_gap(struct maple_node *node, unsigned long *gaps, enum maple_type mt,
1564 	    unsigned char *off)
1565 {
1566 	unsigned char offset, i;
1567 	unsigned long max_gap = 0;
1568 
1569 	i = offset = ma_meta_end(node, mt);
1570 	do {
1571 		if (gaps[i] > max_gap) {
1572 			max_gap = gaps[i];
1573 			offset = i;
1574 		}
1575 	} while (i--);
1576 
1577 	*off = offset;
1578 	return max_gap;
1579 }
1580 
1581 /*
1582  * mas_max_gap() - find the largest gap in a non-leaf node and set the slot.
1583  * @mas: The maple state.
1584  *
1585  * Return: The gap value.
1586  */
1587 static inline unsigned long mas_max_gap(struct ma_state *mas)
1588 {
1589 	unsigned long *gaps;
1590 	unsigned char offset;
1591 	enum maple_type mt;
1592 	struct maple_node *node;
1593 
1594 	mt = mte_node_type(mas->node);
1595 	if (ma_is_leaf(mt))
1596 		return mas_leaf_max_gap(mas);
1597 
1598 	node = mas_mn(mas);
1599 	MAS_BUG_ON(mas, mt != maple_arange_64);
1600 	offset = ma_meta_gap(node);
1601 	gaps = ma_gaps(node, mt);
1602 	return gaps[offset];
1603 }
1604 
1605 /*
1606  * mas_parent_gap() - Set the parent gap and any gaps above, as needed
1607  * @mas: The maple state
1608  * @offset: The gap offset in the parent to set
1609  * @new: The new gap value.
1610  *
1611  * Set the parent gap then continue to set the gap upwards, using the metadata
1612  * of the parent to see if it is necessary to check the node above.
1613  */
1614 static inline void mas_parent_gap(struct ma_state *mas, unsigned char offset,
1615 		unsigned long new)
1616 {
1617 	unsigned long meta_gap = 0;
1618 	struct maple_node *pnode;
1619 	struct maple_enode *penode;
1620 	unsigned long *pgaps;
1621 	unsigned char meta_offset;
1622 	enum maple_type pmt;
1623 
1624 	pnode = mte_parent(mas->node);
1625 	pmt = mas_parent_type(mas, mas->node);
1626 	penode = mt_mk_node(pnode, pmt);
1627 	pgaps = ma_gaps(pnode, pmt);
1628 
1629 ascend:
1630 	MAS_BUG_ON(mas, pmt != maple_arange_64);
1631 	meta_offset = ma_meta_gap(pnode);
1632 	meta_gap = pgaps[meta_offset];
1633 
1634 	pgaps[offset] = new;
1635 
1636 	if (meta_gap == new)
1637 		return;
1638 
1639 	if (offset != meta_offset) {
1640 		if (meta_gap > new)
1641 			return;
1642 
1643 		ma_set_meta_gap(pnode, pmt, offset);
1644 	} else if (new < meta_gap) {
1645 		new = ma_max_gap(pnode, pgaps, pmt, &meta_offset);
1646 		ma_set_meta_gap(pnode, pmt, meta_offset);
1647 	}
1648 
1649 	if (ma_is_root(pnode))
1650 		return;
1651 
1652 	/* Go to the parent node. */
1653 	pnode = mte_parent(penode);
1654 	pmt = mas_parent_type(mas, penode);
1655 	pgaps = ma_gaps(pnode, pmt);
1656 	offset = mte_parent_slot(penode);
1657 	penode = mt_mk_node(pnode, pmt);
1658 	goto ascend;
1659 }
1660 
1661 /*
1662  * mas_update_gap() - Update a nodes gaps and propagate up if necessary.
1663  * @mas: the maple state.
1664  */
1665 static inline void mas_update_gap(struct ma_state *mas)
1666 {
1667 	unsigned char pslot;
1668 	unsigned long p_gap;
1669 	unsigned long max_gap;
1670 
1671 	if (!mt_is_alloc(mas->tree))
1672 		return;
1673 
1674 	if (mte_is_root(mas->node))
1675 		return;
1676 
1677 	max_gap = mas_max_gap(mas);
1678 
1679 	pslot = mte_parent_slot(mas->node);
1680 	p_gap = ma_gaps(mte_parent(mas->node),
1681 			mas_parent_type(mas, mas->node))[pslot];
1682 
1683 	if (p_gap != max_gap)
1684 		mas_parent_gap(mas, pslot, max_gap);
1685 }
1686 
1687 /*
1688  * mas_adopt_children() - Set the parent pointer of all nodes in @parent to
1689  * @parent with the slot encoded.
1690  * @mas: the maple state (for the tree)
1691  * @parent: the maple encoded node containing the children.
1692  */
1693 static inline void mas_adopt_children(struct ma_state *mas,
1694 		struct maple_enode *parent)
1695 {
1696 	enum maple_type type = mte_node_type(parent);
1697 	struct maple_node *node = mte_to_node(parent);
1698 	void __rcu **slots = ma_slots(node, type);
1699 	unsigned long *pivots = ma_pivots(node, type);
1700 	struct maple_enode *child;
1701 	unsigned char offset;
1702 
1703 	offset = ma_data_end(node, type, pivots, mas->max);
1704 	do {
1705 		child = mas_slot_locked(mas, slots, offset);
1706 		mas_set_parent(mas, child, parent, offset);
1707 	} while (offset--);
1708 }
1709 
1710 /*
1711  * mas_put_in_tree() - Put a new node in the tree, smp_wmb(), and mark the old
1712  * node as dead.
1713  * @mas: the maple state with the new node
1714  * @old_enode: The old maple encoded node to replace.
1715  * @new_height: if we are inserting a root node, update the height of the tree
1716  */
1717 static inline void mas_put_in_tree(struct ma_state *mas,
1718 		struct maple_enode *old_enode, char new_height)
1719 	__must_hold(mas->tree->ma_lock)
1720 {
1721 	unsigned char offset;
1722 	void __rcu **slots;
1723 
1724 	if (mte_is_root(mas->node)) {
1725 		mas_mn(mas)->parent = ma_parent_ptr(mas_tree_parent(mas));
1726 		rcu_assign_pointer(mas->tree->ma_root, mte_mk_root(mas->node));
1727 		mt_set_height(mas->tree, new_height);
1728 	} else {
1729 
1730 		offset = mte_parent_slot(mas->node);
1731 		slots = ma_slots(mte_parent(mas->node),
1732 				 mas_parent_type(mas, mas->node));
1733 		rcu_assign_pointer(slots[offset], mas->node);
1734 	}
1735 
1736 	mte_set_node_dead(old_enode);
1737 }
1738 
1739 /*
1740  * mas_replace_node() - Replace a node by putting it in the tree, marking it
1741  * dead, and freeing it.
1742  * the parent encoding to locate the maple node in the tree.
1743  * @mas: the ma_state with @mas->node pointing to the new node.
1744  * @old_enode: The old maple encoded node.
1745  * @new_height: The new height of the tree as a result of the operation
1746  */
1747 static inline void mas_replace_node(struct ma_state *mas,
1748 		struct maple_enode *old_enode, unsigned char new_height)
1749 	__must_hold(mas->tree->ma_lock)
1750 {
1751 	mas_put_in_tree(mas, old_enode, new_height);
1752 	mas_free(mas, old_enode);
1753 }
1754 
1755 /*
1756  * mas_find_child() - Find a child who has the parent @mas->node.
1757  * @mas: the maple state with the parent.
1758  * @child: the maple state to store the child.
1759  */
1760 static inline bool mas_find_child(struct ma_state *mas, struct ma_state *child)
1761 	__must_hold(mas->tree->ma_lock)
1762 {
1763 	enum maple_type mt;
1764 	unsigned char offset;
1765 	unsigned char end;
1766 	unsigned long *pivots;
1767 	struct maple_enode *entry;
1768 	struct maple_node *node;
1769 	void __rcu **slots;
1770 
1771 	mt = mte_node_type(mas->node);
1772 	node = mas_mn(mas);
1773 	slots = ma_slots(node, mt);
1774 	pivots = ma_pivots(node, mt);
1775 	end = ma_data_end(node, mt, pivots, mas->max);
1776 	for (offset = mas->offset; offset <= end; offset++) {
1777 		entry = mas_slot_locked(mas, slots, offset);
1778 		if (mte_parent(entry) == node) {
1779 			*child = *mas;
1780 			mas->offset = offset + 1;
1781 			child->offset = offset;
1782 			mas_descend(child);
1783 			child->offset = 0;
1784 			return true;
1785 		}
1786 	}
1787 	return false;
1788 }
1789 
1790 /*
1791  * mab_shift_right() - Shift the data in mab right. Note, does not clean out the
1792  * old data or set b_node->b_end.
1793  * @b_node: the maple_big_node
1794  * @shift: the shift count
1795  */
1796 static inline void mab_shift_right(struct maple_big_node *b_node,
1797 				 unsigned char shift)
1798 {
1799 	unsigned long size = b_node->b_end * sizeof(unsigned long);
1800 
1801 	memmove(b_node->pivot + shift, b_node->pivot, size);
1802 	memmove(b_node->slot + shift, b_node->slot, size);
1803 	if (b_node->type == maple_arange_64)
1804 		memmove(b_node->gap + shift, b_node->gap, size);
1805 }
1806 
1807 /*
1808  * mab_middle_node() - Check if a middle node is needed (unlikely)
1809  * @b_node: the maple_big_node that contains the data.
1810  * @split: the potential split location
1811  * @slot_count: the size that can be stored in a single node being considered.
1812  *
1813  * Return: true if a middle node is required.
1814  */
1815 static inline bool mab_middle_node(struct maple_big_node *b_node, int split,
1816 				   unsigned char slot_count)
1817 {
1818 	unsigned char size = b_node->b_end;
1819 
1820 	if (size >= 2 * slot_count)
1821 		return true;
1822 
1823 	if (!b_node->slot[split] && (size >= 2 * slot_count - 1))
1824 		return true;
1825 
1826 	return false;
1827 }
1828 
1829 /*
1830  * mab_no_null_split() - ensure the split doesn't fall on a NULL
1831  * @b_node: the maple_big_node with the data
1832  * @split: the suggested split location
1833  * @slot_count: the number of slots in the node being considered.
1834  *
1835  * Return: the split location.
1836  */
1837 static inline int mab_no_null_split(struct maple_big_node *b_node,
1838 				    unsigned char split, unsigned char slot_count)
1839 {
1840 	if (!b_node->slot[split]) {
1841 		/*
1842 		 * If the split is less than the max slot && the right side will
1843 		 * still be sufficient, then increment the split on NULL.
1844 		 */
1845 		if ((split < slot_count - 1) &&
1846 		    (b_node->b_end - split) > (mt_min_slots[b_node->type]))
1847 			split++;
1848 		else
1849 			split--;
1850 	}
1851 	return split;
1852 }
1853 
1854 /*
1855  * mab_calc_split() - Calculate the split location and if there needs to be two
1856  * splits.
1857  * @mas: The maple state
1858  * @bn: The maple_big_node with the data
1859  * @mid_split: The second split, if required.  0 otherwise.
1860  *
1861  * Return: The first split location.  The middle split is set in @mid_split.
1862  */
1863 static inline int mab_calc_split(struct ma_state *mas,
1864 	 struct maple_big_node *bn, unsigned char *mid_split)
1865 {
1866 	unsigned char b_end = bn->b_end;
1867 	int split = b_end / 2; /* Assume equal split. */
1868 	unsigned char slot_count = mt_slots[bn->type];
1869 
1870 	/*
1871 	 * To support gap tracking, all NULL entries are kept together and a node cannot
1872 	 * end on a NULL entry, with the exception of the left-most leaf.  The
1873 	 * limitation means that the split of a node must be checked for this condition
1874 	 * and be able to put more data in one direction or the other.
1875 	 */
1876 	if (unlikely((mas->mas_flags & MA_STATE_BULK))) {
1877 		*mid_split = 0;
1878 		split = b_end - mt_min_slots[bn->type];
1879 
1880 		if (!ma_is_leaf(bn->type))
1881 			return split;
1882 
1883 		mas->mas_flags |= MA_STATE_REBALANCE;
1884 		if (!bn->slot[split])
1885 			split--;
1886 		return split;
1887 	}
1888 
1889 	/*
1890 	 * Although extremely rare, it is possible to enter what is known as the 3-way
1891 	 * split scenario.  The 3-way split comes about by means of a store of a range
1892 	 * that overwrites the end and beginning of two full nodes.  The result is a set
1893 	 * of entries that cannot be stored in 2 nodes.  Sometimes, these two nodes can
1894 	 * also be located in different parent nodes which are also full.  This can
1895 	 * carry upwards all the way to the root in the worst case.
1896 	 */
1897 	if (unlikely(mab_middle_node(bn, split, slot_count))) {
1898 		split = b_end / 3;
1899 		*mid_split = split * 2;
1900 	} else {
1901 		*mid_split = 0;
1902 	}
1903 
1904 	/* Avoid ending a node on a NULL entry */
1905 	split = mab_no_null_split(bn, split, slot_count);
1906 
1907 	if (unlikely(*mid_split))
1908 		*mid_split = mab_no_null_split(bn, *mid_split, slot_count);
1909 
1910 	return split;
1911 }
1912 
1913 /*
1914  * mas_mab_cp() - Copy data from a maple state inclusively to a maple_big_node
1915  * and set @b_node->b_end to the next free slot.
1916  * @mas: The maple state
1917  * @mas_start: The starting slot to copy
1918  * @mas_end: The end slot to copy (inclusively)
1919  * @b_node: The maple_big_node to place the data
1920  * @mab_start: The starting location in maple_big_node to store the data.
1921  */
1922 static inline void mas_mab_cp(struct ma_state *mas, unsigned char mas_start,
1923 			unsigned char mas_end, struct maple_big_node *b_node,
1924 			unsigned char mab_start)
1925 {
1926 	enum maple_type mt;
1927 	struct maple_node *node;
1928 	void __rcu **slots;
1929 	unsigned long *pivots, *gaps;
1930 	int i = mas_start, j = mab_start;
1931 	unsigned char piv_end;
1932 
1933 	node = mas_mn(mas);
1934 	mt = mte_node_type(mas->node);
1935 	pivots = ma_pivots(node, mt);
1936 	if (!i) {
1937 		b_node->pivot[j] = pivots[i++];
1938 		if (unlikely(i > mas_end))
1939 			goto complete;
1940 		j++;
1941 	}
1942 
1943 	piv_end = min(mas_end, mt_pivots[mt]);
1944 	for (; i < piv_end; i++, j++) {
1945 		b_node->pivot[j] = pivots[i];
1946 		if (unlikely(!b_node->pivot[j]))
1947 			goto complete;
1948 
1949 		if (unlikely(mas->max == b_node->pivot[j]))
1950 			goto complete;
1951 	}
1952 
1953 	b_node->pivot[j] = mas_safe_pivot(mas, pivots, i, mt);
1954 
1955 complete:
1956 	b_node->b_end = ++j;
1957 	j -= mab_start;
1958 	slots = ma_slots(node, mt);
1959 	memcpy(b_node->slot + mab_start, slots + mas_start, sizeof(void *) * j);
1960 	if (!ma_is_leaf(mt) && mt_is_alloc(mas->tree)) {
1961 		gaps = ma_gaps(node, mt);
1962 		memcpy(b_node->gap + mab_start, gaps + mas_start,
1963 		       sizeof(unsigned long) * j);
1964 	}
1965 }
1966 
1967 /*
1968  * mas_leaf_set_meta() - Set the metadata of a leaf if possible.
1969  * @node: The maple node
1970  * @mt: The maple type
1971  * @end: The node end
1972  */
1973 static inline void mas_leaf_set_meta(struct maple_node *node,
1974 		enum maple_type mt, unsigned char end)
1975 {
1976 	if (end < mt_slots[mt] - 1)
1977 		ma_set_meta(node, mt, 0, end);
1978 }
1979 
1980 /*
1981  * mab_mas_cp() - Copy data from maple_big_node to a maple encoded node.
1982  * @b_node: the maple_big_node that has the data
1983  * @mab_start: the start location in @b_node.
1984  * @mab_end: The end location in @b_node (inclusively)
1985  * @mas: The maple state with the maple encoded node.
1986  */
1987 static inline void mab_mas_cp(struct maple_big_node *b_node,
1988 			      unsigned char mab_start, unsigned char mab_end,
1989 			      struct ma_state *mas, bool new_max)
1990 {
1991 	int i, j = 0;
1992 	enum maple_type mt = mte_node_type(mas->node);
1993 	struct maple_node *node = mte_to_node(mas->node);
1994 	void __rcu **slots = ma_slots(node, mt);
1995 	unsigned long *pivots = ma_pivots(node, mt);
1996 	unsigned long *gaps = NULL;
1997 	unsigned char end;
1998 
1999 	if (mab_end - mab_start > mt_pivots[mt])
2000 		mab_end--;
2001 
2002 	if (!pivots[mt_pivots[mt] - 1])
2003 		slots[mt_pivots[mt]] = NULL;
2004 
2005 	i = mab_start;
2006 	do {
2007 		pivots[j++] = b_node->pivot[i++];
2008 	} while (i <= mab_end && likely(b_node->pivot[i]));
2009 
2010 	memcpy(slots, b_node->slot + mab_start,
2011 	       sizeof(void *) * (i - mab_start));
2012 
2013 	if (new_max)
2014 		mas->max = b_node->pivot[i - 1];
2015 
2016 	end = j - 1;
2017 	if (likely(!ma_is_leaf(mt) && mt_is_alloc(mas->tree))) {
2018 		unsigned long max_gap = 0;
2019 		unsigned char offset = 0;
2020 
2021 		gaps = ma_gaps(node, mt);
2022 		do {
2023 			gaps[--j] = b_node->gap[--i];
2024 			if (gaps[j] > max_gap) {
2025 				offset = j;
2026 				max_gap = gaps[j];
2027 			}
2028 		} while (j);
2029 
2030 		ma_set_meta(node, mt, offset, end);
2031 	} else {
2032 		mas_leaf_set_meta(node, mt, end);
2033 	}
2034 }
2035 
2036 /*
2037  * mas_bulk_rebalance() - Rebalance the end of a tree after a bulk insert.
2038  * @mas: The maple state
2039  * @end: The maple node end
2040  * @mt: The maple node type
2041  */
2042 static inline void mas_bulk_rebalance(struct ma_state *mas, unsigned char end,
2043 				      enum maple_type mt)
2044 {
2045 	if (!(mas->mas_flags & MA_STATE_BULK))
2046 		return;
2047 
2048 	if (mte_is_root(mas->node))
2049 		return;
2050 
2051 	if (end > mt_min_slots[mt]) {
2052 		mas->mas_flags &= ~MA_STATE_REBALANCE;
2053 		return;
2054 	}
2055 }
2056 
2057 /*
2058  * mas_store_b_node() - Store an @entry into the b_node while also copying the
2059  * data from a maple encoded node.
2060  * @wr_mas: the maple write state
2061  * @b_node: the maple_big_node to fill with data
2062  * @offset_end: the offset to end copying
2063  *
2064  * Return: The actual end of the data stored in @b_node
2065  */
2066 static noinline_for_kasan void mas_store_b_node(struct ma_wr_state *wr_mas,
2067 		struct maple_big_node *b_node, unsigned char offset_end)
2068 {
2069 	unsigned char slot;
2070 	unsigned char b_end;
2071 	/* Possible underflow of piv will wrap back to 0 before use. */
2072 	unsigned long piv;
2073 	struct ma_state *mas = wr_mas->mas;
2074 
2075 	b_node->type = wr_mas->type;
2076 	b_end = 0;
2077 	slot = mas->offset;
2078 	if (slot) {
2079 		/* Copy start data up to insert. */
2080 		mas_mab_cp(mas, 0, slot - 1, b_node, 0);
2081 		b_end = b_node->b_end;
2082 		piv = b_node->pivot[b_end - 1];
2083 	} else
2084 		piv = mas->min - 1;
2085 
2086 	if (piv + 1 < mas->index) {
2087 		/* Handle range starting after old range */
2088 		b_node->slot[b_end] = wr_mas->content;
2089 		if (!wr_mas->content)
2090 			b_node->gap[b_end] = mas->index - 1 - piv;
2091 		b_node->pivot[b_end++] = mas->index - 1;
2092 	}
2093 
2094 	/* Store the new entry. */
2095 	mas->offset = b_end;
2096 	b_node->slot[b_end] = wr_mas->entry;
2097 	b_node->pivot[b_end] = mas->last;
2098 
2099 	/* Appended. */
2100 	if (mas->last >= mas->max)
2101 		goto b_end;
2102 
2103 	/* Handle new range ending before old range ends */
2104 	piv = mas_safe_pivot(mas, wr_mas->pivots, offset_end, wr_mas->type);
2105 	if (piv > mas->last) {
2106 		if (piv == ULONG_MAX)
2107 			mas_bulk_rebalance(mas, b_node->b_end, wr_mas->type);
2108 
2109 		if (offset_end != slot)
2110 			wr_mas->content = mas_slot_locked(mas, wr_mas->slots,
2111 							  offset_end);
2112 
2113 		b_node->slot[++b_end] = wr_mas->content;
2114 		if (!wr_mas->content)
2115 			b_node->gap[b_end] = piv - mas->last + 1;
2116 		b_node->pivot[b_end] = piv;
2117 	}
2118 
2119 	slot = offset_end + 1;
2120 	if (slot > mas->end)
2121 		goto b_end;
2122 
2123 	/* Copy end data to the end of the node. */
2124 	mas_mab_cp(mas, slot, mas->end + 1, b_node, ++b_end);
2125 	b_node->b_end--;
2126 	return;
2127 
2128 b_end:
2129 	b_node->b_end = b_end;
2130 }
2131 
2132 /*
2133  * mas_prev_sibling() - Find the previous node with the same parent.
2134  * @mas: the maple state
2135  *
2136  * Return: True if there is a previous sibling, false otherwise.
2137  */
2138 static inline bool mas_prev_sibling(struct ma_state *mas)
2139 {
2140 	unsigned int p_slot = mte_parent_slot(mas->node);
2141 
2142 	/* For root node, p_slot is set to 0 by mte_parent_slot(). */
2143 	if (!p_slot)
2144 		return false;
2145 
2146 	mas_ascend(mas);
2147 	mas->offset = p_slot - 1;
2148 	mas_descend(mas);
2149 	return true;
2150 }
2151 
2152 /*
2153  * mas_next_sibling() - Find the next node with the same parent.
2154  * @mas: the maple state
2155  *
2156  * Return: true if there is a next sibling, false otherwise.
2157  */
2158 static inline bool mas_next_sibling(struct ma_state *mas)
2159 {
2160 	MA_STATE(parent, mas->tree, mas->index, mas->last);
2161 
2162 	if (mte_is_root(mas->node))
2163 		return false;
2164 
2165 	parent = *mas;
2166 	mas_ascend(&parent);
2167 	parent.offset = mte_parent_slot(mas->node) + 1;
2168 	if (parent.offset > mas_data_end(&parent))
2169 		return false;
2170 
2171 	*mas = parent;
2172 	mas_descend(mas);
2173 	return true;
2174 }
2175 
2176 /*
2177  * mas_node_or_none() - Set the enode and state.
2178  * @mas: the maple state
2179  * @enode: The encoded maple node.
2180  *
2181  * Set the node to the enode and the status.
2182  */
2183 static inline void mas_node_or_none(struct ma_state *mas,
2184 		struct maple_enode *enode)
2185 {
2186 	if (enode) {
2187 		mas->node = enode;
2188 		mas->status = ma_active;
2189 	} else {
2190 		mas->node = NULL;
2191 		mas->status = ma_none;
2192 	}
2193 }
2194 
2195 /*
2196  * mas_wr_node_walk() - Find the correct offset for the index in the @mas.
2197  *                      If @mas->index cannot be found within the containing
2198  *                      node, we traverse to the last entry in the node.
2199  * @wr_mas: The maple write state
2200  *
2201  * Uses mas_slot_locked() and does not need to worry about dead nodes.
2202  */
2203 static inline void mas_wr_node_walk(struct ma_wr_state *wr_mas)
2204 {
2205 	struct ma_state *mas = wr_mas->mas;
2206 	unsigned char count, offset;
2207 
2208 	if (unlikely(ma_is_dense(wr_mas->type))) {
2209 		wr_mas->r_max = wr_mas->r_min = mas->index;
2210 		mas->offset = mas->index = mas->min;
2211 		return;
2212 	}
2213 
2214 	wr_mas->node = mas_mn(wr_mas->mas);
2215 	wr_mas->pivots = ma_pivots(wr_mas->node, wr_mas->type);
2216 	count = mas->end = ma_data_end(wr_mas->node, wr_mas->type,
2217 				       wr_mas->pivots, mas->max);
2218 	offset = mas->offset;
2219 
2220 	while (offset < count && mas->index > wr_mas->pivots[offset])
2221 		offset++;
2222 
2223 	wr_mas->r_max = offset < count ? wr_mas->pivots[offset] : mas->max;
2224 	wr_mas->r_min = mas_safe_min(mas, wr_mas->pivots, offset);
2225 	wr_mas->offset_end = mas->offset = offset;
2226 }
2227 
2228 /*
2229  * mast_rebalance_next() - Rebalance against the next node
2230  * @mast: The maple subtree state
2231  */
2232 static inline void mast_rebalance_next(struct maple_subtree_state *mast)
2233 {
2234 	unsigned char b_end = mast->bn->b_end;
2235 
2236 	mas_mab_cp(mast->orig_r, 0, mt_slot_count(mast->orig_r->node),
2237 		   mast->bn, b_end);
2238 	mast->orig_r->last = mast->orig_r->max;
2239 }
2240 
2241 /*
2242  * mast_rebalance_prev() - Rebalance against the previous node
2243  * @mast: The maple subtree state
2244  */
2245 static inline void mast_rebalance_prev(struct maple_subtree_state *mast)
2246 {
2247 	unsigned char end = mas_data_end(mast->orig_l) + 1;
2248 	unsigned char b_end = mast->bn->b_end;
2249 
2250 	mab_shift_right(mast->bn, end);
2251 	mas_mab_cp(mast->orig_l, 0, end - 1, mast->bn, 0);
2252 	mast->l->min = mast->orig_l->min;
2253 	mast->orig_l->index = mast->orig_l->min;
2254 	mast->bn->b_end = end + b_end;
2255 	mast->l->offset += end;
2256 }
2257 
2258 /*
2259  * mast_spanning_rebalance() - Rebalance nodes with nearest neighbour favouring
2260  * the node to the right.  Checking the nodes to the right then the left at each
2261  * level upwards until root is reached.
2262  * Data is copied into the @mast->bn.
2263  * @mast: The maple_subtree_state.
2264  */
2265 static inline
2266 bool mast_spanning_rebalance(struct maple_subtree_state *mast)
2267 {
2268 	struct ma_state r_tmp = *mast->orig_r;
2269 	struct ma_state l_tmp = *mast->orig_l;
2270 	unsigned char depth = 0;
2271 
2272 	do {
2273 		mas_ascend(mast->orig_r);
2274 		mas_ascend(mast->orig_l);
2275 		depth++;
2276 		if (mast->orig_r->offset < mas_data_end(mast->orig_r)) {
2277 			mast->orig_r->offset++;
2278 			do {
2279 				mas_descend(mast->orig_r);
2280 				mast->orig_r->offset = 0;
2281 			} while (--depth);
2282 
2283 			mast_rebalance_next(mast);
2284 			*mast->orig_l = l_tmp;
2285 			return true;
2286 		} else if (mast->orig_l->offset != 0) {
2287 			mast->orig_l->offset--;
2288 			do {
2289 				mas_descend(mast->orig_l);
2290 				mast->orig_l->offset =
2291 					mas_data_end(mast->orig_l);
2292 			} while (--depth);
2293 
2294 			mast_rebalance_prev(mast);
2295 			*mast->orig_r = r_tmp;
2296 			return true;
2297 		}
2298 	} while (!mte_is_root(mast->orig_r->node));
2299 
2300 	*mast->orig_r = r_tmp;
2301 	*mast->orig_l = l_tmp;
2302 	return false;
2303 }
2304 
2305 /*
2306  * mast_ascend() - Ascend the original left and right maple states.
2307  * @mast: the maple subtree state.
2308  *
2309  * Ascend the original left and right sides.  Set the offsets to point to the
2310  * data already in the new tree (@mast->l and @mast->r).
2311  */
2312 static inline void mast_ascend(struct maple_subtree_state *mast)
2313 {
2314 	MA_WR_STATE(wr_mas, mast->orig_r,  NULL);
2315 	mas_ascend(mast->orig_l);
2316 	mas_ascend(mast->orig_r);
2317 
2318 	mast->orig_r->offset = 0;
2319 	mast->orig_r->index = mast->r->max;
2320 	/* last should be larger than or equal to index */
2321 	if (mast->orig_r->last < mast->orig_r->index)
2322 		mast->orig_r->last = mast->orig_r->index;
2323 
2324 	wr_mas.type = mte_node_type(mast->orig_r->node);
2325 	mas_wr_node_walk(&wr_mas);
2326 	/* Set up the left side of things */
2327 	mast->orig_l->offset = 0;
2328 	mast->orig_l->index = mast->l->min;
2329 	wr_mas.mas = mast->orig_l;
2330 	wr_mas.type = mte_node_type(mast->orig_l->node);
2331 	mas_wr_node_walk(&wr_mas);
2332 
2333 	mast->bn->type = wr_mas.type;
2334 }
2335 
2336 /*
2337  * mas_new_ma_node() - Create and return a new maple node.  Helper function.
2338  * @mas: the maple state with the allocations.
2339  * @b_node: the maple_big_node with the type encoding.
2340  *
2341  * Use the node type from the maple_big_node to allocate a new node from the
2342  * ma_state.  This function exists mainly for code readability.
2343  *
2344  * Return: A new maple encoded node
2345  */
2346 static inline struct maple_enode
2347 *mas_new_ma_node(struct ma_state *mas, struct maple_big_node *b_node)
2348 {
2349 	return mt_mk_node(ma_mnode_ptr(mas_pop_node(mas)), b_node->type);
2350 }
2351 
2352 /*
2353  * mas_mab_to_node() - Set up right and middle nodes
2354  *
2355  * @mas: the maple state that contains the allocations.
2356  * @b_node: the node which contains the data.
2357  * @left: The pointer which will have the left node
2358  * @right: The pointer which may have the right node
2359  * @middle: the pointer which may have the middle node (rare)
2360  * @mid_split: the split location for the middle node
2361  *
2362  * Return: the split of left.
2363  */
2364 static inline unsigned char mas_mab_to_node(struct ma_state *mas,
2365 	struct maple_big_node *b_node, struct maple_enode **left,
2366 	struct maple_enode **right, struct maple_enode **middle,
2367 	unsigned char *mid_split)
2368 {
2369 	unsigned char split = 0;
2370 	unsigned char slot_count = mt_slots[b_node->type];
2371 
2372 	*left = mas_new_ma_node(mas, b_node);
2373 	*right = NULL;
2374 	*middle = NULL;
2375 	*mid_split = 0;
2376 
2377 	if (b_node->b_end < slot_count) {
2378 		split = b_node->b_end;
2379 	} else {
2380 		split = mab_calc_split(mas, b_node, mid_split);
2381 		*right = mas_new_ma_node(mas, b_node);
2382 	}
2383 
2384 	if (*mid_split)
2385 		*middle = mas_new_ma_node(mas, b_node);
2386 
2387 	return split;
2388 
2389 }
2390 
2391 /*
2392  * mab_set_b_end() - Add entry to b_node at b_node->b_end and increment the end
2393  * pointer.
2394  * @b_node: the big node to add the entry
2395  * @mas: the maple state to get the pivot (mas->max)
2396  * @entry: the entry to add, if NULL nothing happens.
2397  */
2398 static inline void mab_set_b_end(struct maple_big_node *b_node,
2399 				 struct ma_state *mas,
2400 				 void *entry)
2401 {
2402 	if (!entry)
2403 		return;
2404 
2405 	b_node->slot[b_node->b_end] = entry;
2406 	if (mt_is_alloc(mas->tree))
2407 		b_node->gap[b_node->b_end] = mas_max_gap(mas);
2408 	b_node->pivot[b_node->b_end++] = mas->max;
2409 }
2410 
2411 /*
2412  * mas_set_split_parent() - combine_then_separate helper function.  Sets the parent
2413  * of @mas->node to either @left or @right, depending on @slot and @split
2414  *
2415  * @mas: the maple state with the node that needs a parent
2416  * @left: possible parent 1
2417  * @right: possible parent 2
2418  * @slot: the slot the mas->node was placed
2419  * @split: the split location between @left and @right
2420  */
2421 static inline void mas_set_split_parent(struct ma_state *mas,
2422 					struct maple_enode *left,
2423 					struct maple_enode *right,
2424 					unsigned char *slot, unsigned char split)
2425 {
2426 	if (mas_is_none(mas))
2427 		return;
2428 
2429 	if ((*slot) <= split)
2430 		mas_set_parent(mas, mas->node, left, *slot);
2431 	else if (right)
2432 		mas_set_parent(mas, mas->node, right, (*slot) - split - 1);
2433 
2434 	(*slot)++;
2435 }
2436 
2437 /*
2438  * mte_mid_split_check() - Check if the next node passes the mid-split
2439  * @l: Pointer to left encoded maple node.
2440  * @m: Pointer to middle encoded maple node.
2441  * @r: Pointer to right encoded maple node.
2442  * @slot: The offset
2443  * @split: The split location.
2444  * @mid_split: The middle split.
2445  */
2446 static inline void mte_mid_split_check(struct maple_enode **l,
2447 				       struct maple_enode **r,
2448 				       struct maple_enode *right,
2449 				       unsigned char slot,
2450 				       unsigned char *split,
2451 				       unsigned char mid_split)
2452 {
2453 	if (*r == right)
2454 		return;
2455 
2456 	if (slot < mid_split)
2457 		return;
2458 
2459 	*l = *r;
2460 	*r = right;
2461 	*split = mid_split;
2462 }
2463 
2464 /*
2465  * mast_set_split_parents() - Helper function to set three nodes parents.  Slot
2466  * is taken from @mast->l.
2467  * @mast: the maple subtree state
2468  * @left: the left node
2469  * @right: the right node
2470  * @split: the split location.
2471  */
2472 static inline void mast_set_split_parents(struct maple_subtree_state *mast,
2473 					  struct maple_enode *left,
2474 					  struct maple_enode *middle,
2475 					  struct maple_enode *right,
2476 					  unsigned char split,
2477 					  unsigned char mid_split)
2478 {
2479 	unsigned char slot;
2480 	struct maple_enode *l = left;
2481 	struct maple_enode *r = right;
2482 
2483 	if (mas_is_none(mast->l))
2484 		return;
2485 
2486 	if (middle)
2487 		r = middle;
2488 
2489 	slot = mast->l->offset;
2490 
2491 	mte_mid_split_check(&l, &r, right, slot, &split, mid_split);
2492 	mas_set_split_parent(mast->l, l, r, &slot, split);
2493 
2494 	mte_mid_split_check(&l, &r, right, slot, &split, mid_split);
2495 	mas_set_split_parent(mast->m, l, r, &slot, split);
2496 
2497 	mte_mid_split_check(&l, &r, right, slot, &split, mid_split);
2498 	mas_set_split_parent(mast->r, l, r, &slot, split);
2499 }
2500 
2501 /*
2502  * mas_topiary_node() - Dispose of a single node
2503  * @mas: The maple state for pushing nodes
2504  * @in_rcu: If the tree is in rcu mode
2505  *
2506  * The node will either be RCU freed or pushed back on the maple state.
2507  */
2508 static inline void mas_topiary_node(struct ma_state *mas,
2509 		struct ma_state *tmp_mas, bool in_rcu)
2510 {
2511 	struct maple_node *tmp;
2512 	struct maple_enode *enode;
2513 
2514 	if (mas_is_none(tmp_mas))
2515 		return;
2516 
2517 	enode = tmp_mas->node;
2518 	tmp = mte_to_node(enode);
2519 	mte_set_node_dead(enode);
2520 	if (in_rcu)
2521 		ma_free_rcu(tmp);
2522 	else
2523 		mas_push_node(mas, tmp);
2524 }
2525 
2526 /*
2527  * mas_topiary_replace() - Replace the data with new data, then repair the
2528  * parent links within the new tree.  Iterate over the dead sub-tree and collect
2529  * the dead subtrees and topiary the nodes that are no longer of use.
2530  *
2531  * The new tree will have up to three children with the correct parent.  Keep
2532  * track of the new entries as they need to be followed to find the next level
2533  * of new entries.
2534  *
2535  * The old tree will have up to three children with the old parent.  Keep track
2536  * of the old entries as they may have more nodes below replaced.  Nodes within
2537  * [index, last] are dead subtrees, others need to be freed and followed.
2538  *
2539  * @mas: The maple state pointing at the new data
2540  * @old_enode: The maple encoded node being replaced
2541  * @new_height: The new height of the tree as a result of the operation
2542  *
2543  */
2544 static inline void mas_topiary_replace(struct ma_state *mas,
2545 		struct maple_enode *old_enode, unsigned char new_height)
2546 {
2547 	struct ma_state tmp[3], tmp_next[3];
2548 	MA_TOPIARY(subtrees, mas->tree);
2549 	bool in_rcu;
2550 	int i, n;
2551 
2552 	/* Place data in tree & then mark node as old */
2553 	mas_put_in_tree(mas, old_enode, new_height);
2554 
2555 	/* Update the parent pointers in the tree */
2556 	tmp[0] = *mas;
2557 	tmp[0].offset = 0;
2558 	tmp[1].status = ma_none;
2559 	tmp[2].status = ma_none;
2560 	while (!mte_is_leaf(tmp[0].node)) {
2561 		n = 0;
2562 		for (i = 0; i < 3; i++) {
2563 			if (mas_is_none(&tmp[i]))
2564 				continue;
2565 
2566 			while (n < 3) {
2567 				if (!mas_find_child(&tmp[i], &tmp_next[n]))
2568 					break;
2569 				n++;
2570 			}
2571 
2572 			mas_adopt_children(&tmp[i], tmp[i].node);
2573 		}
2574 
2575 		if (MAS_WARN_ON(mas, n == 0))
2576 			break;
2577 
2578 		while (n < 3)
2579 			tmp_next[n++].status = ma_none;
2580 
2581 		for (i = 0; i < 3; i++)
2582 			tmp[i] = tmp_next[i];
2583 	}
2584 
2585 	/* Collect the old nodes that need to be discarded */
2586 	if (mte_is_leaf(old_enode))
2587 		return mas_free(mas, old_enode);
2588 
2589 	tmp[0] = *mas;
2590 	tmp[0].offset = 0;
2591 	tmp[0].node = old_enode;
2592 	tmp[1].status = ma_none;
2593 	tmp[2].status = ma_none;
2594 	in_rcu = mt_in_rcu(mas->tree);
2595 	do {
2596 		n = 0;
2597 		for (i = 0; i < 3; i++) {
2598 			if (mas_is_none(&tmp[i]))
2599 				continue;
2600 
2601 			while (n < 3) {
2602 				if (!mas_find_child(&tmp[i], &tmp_next[n]))
2603 					break;
2604 
2605 				if ((tmp_next[n].min >= tmp_next->index) &&
2606 				    (tmp_next[n].max <= tmp_next->last)) {
2607 					mat_add(&subtrees, tmp_next[n].node);
2608 					tmp_next[n].status = ma_none;
2609 				} else {
2610 					n++;
2611 				}
2612 			}
2613 		}
2614 
2615 		if (MAS_WARN_ON(mas, n == 0))
2616 			break;
2617 
2618 		while (n < 3)
2619 			tmp_next[n++].status = ma_none;
2620 
2621 		for (i = 0; i < 3; i++) {
2622 			mas_topiary_node(mas, &tmp[i], in_rcu);
2623 			tmp[i] = tmp_next[i];
2624 		}
2625 	} while (!mte_is_leaf(tmp[0].node));
2626 
2627 	for (i = 0; i < 3; i++)
2628 		mas_topiary_node(mas, &tmp[i], in_rcu);
2629 
2630 	mas_mat_destroy(mas, &subtrees);
2631 }
2632 
2633 /*
2634  * mas_wmb_replace() - Write memory barrier and replace
2635  * @mas: The maple state
2636  * @old_enode: The old maple encoded node that is being replaced.
2637  * @new_height: The new height of the tree as a result of the operation
2638  *
2639  * Updates gap as necessary.
2640  */
2641 static inline void mas_wmb_replace(struct ma_state *mas,
2642 		struct maple_enode *old_enode, unsigned char new_height)
2643 {
2644 	/* Insert the new data in the tree */
2645 	mas_topiary_replace(mas, old_enode, new_height);
2646 
2647 	if (mte_is_leaf(mas->node))
2648 		return;
2649 
2650 	mas_update_gap(mas);
2651 }
2652 
2653 /*
2654  * mast_cp_to_nodes() - Copy data out to nodes.
2655  * @mast: The maple subtree state
2656  * @left: The left encoded maple node
2657  * @middle: The middle encoded maple node
2658  * @right: The right encoded maple node
2659  * @split: The location to split between left and (middle ? middle : right)
2660  * @mid_split: The location to split between middle and right.
2661  */
2662 static inline void mast_cp_to_nodes(struct maple_subtree_state *mast,
2663 	struct maple_enode *left, struct maple_enode *middle,
2664 	struct maple_enode *right, unsigned char split, unsigned char mid_split)
2665 {
2666 	bool new_lmax = true;
2667 
2668 	mas_node_or_none(mast->l, left);
2669 	mas_node_or_none(mast->m, middle);
2670 	mas_node_or_none(mast->r, right);
2671 
2672 	mast->l->min = mast->orig_l->min;
2673 	if (split == mast->bn->b_end) {
2674 		mast->l->max = mast->orig_r->max;
2675 		new_lmax = false;
2676 	}
2677 
2678 	mab_mas_cp(mast->bn, 0, split, mast->l, new_lmax);
2679 
2680 	if (middle) {
2681 		mab_mas_cp(mast->bn, 1 + split, mid_split, mast->m, true);
2682 		mast->m->min = mast->bn->pivot[split] + 1;
2683 		split = mid_split;
2684 	}
2685 
2686 	mast->r->max = mast->orig_r->max;
2687 	if (right) {
2688 		mab_mas_cp(mast->bn, 1 + split, mast->bn->b_end, mast->r, false);
2689 		mast->r->min = mast->bn->pivot[split] + 1;
2690 	}
2691 }
2692 
2693 /*
2694  * mast_combine_cp_left - Copy in the original left side of the tree into the
2695  * combined data set in the maple subtree state big node.
2696  * @mast: The maple subtree state
2697  */
2698 static inline void mast_combine_cp_left(struct maple_subtree_state *mast)
2699 {
2700 	unsigned char l_slot = mast->orig_l->offset;
2701 
2702 	if (!l_slot)
2703 		return;
2704 
2705 	mas_mab_cp(mast->orig_l, 0, l_slot - 1, mast->bn, 0);
2706 }
2707 
2708 /*
2709  * mast_combine_cp_right: Copy in the original right side of the tree into the
2710  * combined data set in the maple subtree state big node.
2711  * @mast: The maple subtree state
2712  */
2713 static inline void mast_combine_cp_right(struct maple_subtree_state *mast)
2714 {
2715 	if (mast->bn->pivot[mast->bn->b_end - 1] >= mast->orig_r->max)
2716 		return;
2717 
2718 	mas_mab_cp(mast->orig_r, mast->orig_r->offset + 1,
2719 		   mt_slot_count(mast->orig_r->node), mast->bn,
2720 		   mast->bn->b_end);
2721 	mast->orig_r->last = mast->orig_r->max;
2722 }
2723 
2724 /*
2725  * mast_sufficient: Check if the maple subtree state has enough data in the big
2726  * node to create at least one sufficient node
2727  * @mast: the maple subtree state
2728  */
2729 static inline bool mast_sufficient(struct maple_subtree_state *mast)
2730 {
2731 	if (mast->bn->b_end > mt_min_slot_count(mast->orig_l->node))
2732 		return true;
2733 
2734 	return false;
2735 }
2736 
2737 /*
2738  * mast_overflow: Check if there is too much data in the subtree state for a
2739  * single node.
2740  * @mast: The maple subtree state
2741  */
2742 static inline bool mast_overflow(struct maple_subtree_state *mast)
2743 {
2744 	if (mast->bn->b_end > mt_slot_count(mast->orig_l->node))
2745 		return true;
2746 
2747 	return false;
2748 }
2749 
2750 static inline void *mtree_range_walk(struct ma_state *mas)
2751 {
2752 	unsigned long *pivots;
2753 	unsigned char offset;
2754 	struct maple_node *node;
2755 	struct maple_enode *next, *last;
2756 	enum maple_type type;
2757 	void __rcu **slots;
2758 	unsigned char end;
2759 	unsigned long max, min;
2760 	unsigned long prev_max, prev_min;
2761 
2762 	next = mas->node;
2763 	min = mas->min;
2764 	max = mas->max;
2765 	do {
2766 		last = next;
2767 		node = mte_to_node(next);
2768 		type = mte_node_type(next);
2769 		pivots = ma_pivots(node, type);
2770 		end = ma_data_end(node, type, pivots, max);
2771 		prev_min = min;
2772 		prev_max = max;
2773 		if (pivots[0] >= mas->index) {
2774 			offset = 0;
2775 			max = pivots[0];
2776 			goto next;
2777 		}
2778 
2779 		offset = 1;
2780 		while (offset < end) {
2781 			if (pivots[offset] >= mas->index) {
2782 				max = pivots[offset];
2783 				break;
2784 			}
2785 			offset++;
2786 		}
2787 
2788 		min = pivots[offset - 1] + 1;
2789 next:
2790 		slots = ma_slots(node, type);
2791 		next = mt_slot(mas->tree, slots, offset);
2792 		if (unlikely(ma_dead_node(node)))
2793 			goto dead_node;
2794 	} while (!ma_is_leaf(type));
2795 
2796 	mas->end = end;
2797 	mas->offset = offset;
2798 	mas->index = min;
2799 	mas->last = max;
2800 	mas->min = prev_min;
2801 	mas->max = prev_max;
2802 	mas->node = last;
2803 	return (void *)next;
2804 
2805 dead_node:
2806 	mas_reset(mas);
2807 	return NULL;
2808 }
2809 
2810 /*
2811  * mas_spanning_rebalance() - Rebalance across two nodes which may not be peers.
2812  * @mas: The starting maple state
2813  * @mast: The maple_subtree_state, keeps track of 4 maple states.
2814  * @count: The estimated count of iterations needed.
2815  *
2816  * Follow the tree upwards from @l_mas and @r_mas for @count, or until the root
2817  * is hit.  First @b_node is split into two entries which are inserted into the
2818  * next iteration of the loop.  @b_node is returned populated with the final
2819  * iteration. @mas is used to obtain allocations.  orig_l_mas keeps track of the
2820  * nodes that will remain active by using orig_l_mas->index and orig_l_mas->last
2821  * to account of what has been copied into the new sub-tree.  The update of
2822  * orig_l_mas->last is used in mas_consume to find the slots that will need to
2823  * be either freed or destroyed.  orig_l_mas->depth keeps track of the height of
2824  * the new sub-tree in case the sub-tree becomes the full tree.
2825  */
2826 static void mas_spanning_rebalance(struct ma_state *mas,
2827 		struct maple_subtree_state *mast, unsigned char count)
2828 {
2829 	unsigned char split, mid_split;
2830 	unsigned char slot = 0;
2831 	unsigned char new_height = 0; /* used if node is a new root */
2832 	struct maple_enode *left = NULL, *middle = NULL, *right = NULL;
2833 	struct maple_enode *old_enode;
2834 
2835 	MA_STATE(l_mas, mas->tree, mas->index, mas->index);
2836 	MA_STATE(r_mas, mas->tree, mas->index, mas->last);
2837 	MA_STATE(m_mas, mas->tree, mas->index, mas->index);
2838 
2839 	/*
2840 	 * The tree needs to be rebalanced and leaves need to be kept at the same level.
2841 	 * Rebalancing is done by use of the ``struct maple_topiary``.
2842 	 */
2843 	mast->l = &l_mas;
2844 	mast->m = &m_mas;
2845 	mast->r = &r_mas;
2846 	l_mas.status = r_mas.status = m_mas.status = ma_none;
2847 
2848 	/* Check if this is not root and has sufficient data.  */
2849 	if (((mast->orig_l->min != 0) || (mast->orig_r->max != ULONG_MAX)) &&
2850 	    unlikely(mast->bn->b_end <= mt_min_slots[mast->bn->type]))
2851 		mast_spanning_rebalance(mast);
2852 
2853 	/*
2854 	 * Each level of the tree is examined and balanced, pushing data to the left or
2855 	 * right, or rebalancing against left or right nodes is employed to avoid
2856 	 * rippling up the tree to limit the amount of churn.  Once a new sub-section of
2857 	 * the tree is created, there may be a mix of new and old nodes.  The old nodes
2858 	 * will have the incorrect parent pointers and currently be in two trees: the
2859 	 * original tree and the partially new tree.  To remedy the parent pointers in
2860 	 * the old tree, the new data is swapped into the active tree and a walk down
2861 	 * the tree is performed and the parent pointers are updated.
2862 	 * See mas_topiary_replace() for more information.
2863 	 */
2864 	while (count--) {
2865 		mast->bn->b_end--;
2866 		mast->bn->type = mte_node_type(mast->orig_l->node);
2867 		split = mas_mab_to_node(mas, mast->bn, &left, &right, &middle,
2868 					&mid_split);
2869 		mast_set_split_parents(mast, left, middle, right, split,
2870 				       mid_split);
2871 		mast_cp_to_nodes(mast, left, middle, right, split, mid_split);
2872 		new_height++;
2873 
2874 		/*
2875 		 * Copy data from next level in the tree to mast->bn from next
2876 		 * iteration
2877 		 */
2878 		memset(mast->bn, 0, sizeof(struct maple_big_node));
2879 		mast->bn->type = mte_node_type(left);
2880 
2881 		/* Root already stored in l->node. */
2882 		if (mas_is_root_limits(mast->l))
2883 			goto new_root;
2884 
2885 		mast_ascend(mast);
2886 		mast_combine_cp_left(mast);
2887 		l_mas.offset = mast->bn->b_end;
2888 		mab_set_b_end(mast->bn, &l_mas, left);
2889 		mab_set_b_end(mast->bn, &m_mas, middle);
2890 		mab_set_b_end(mast->bn, &r_mas, right);
2891 
2892 		/* Copy anything necessary out of the right node. */
2893 		mast_combine_cp_right(mast);
2894 		mast->orig_l->last = mast->orig_l->max;
2895 
2896 		if (mast_sufficient(mast)) {
2897 			if (mast_overflow(mast))
2898 				continue;
2899 
2900 			if (mast->orig_l->node == mast->orig_r->node) {
2901 			       /*
2902 				* The data in b_node should be stored in one
2903 				* node and in the tree
2904 				*/
2905 				slot = mast->l->offset;
2906 				break;
2907 			}
2908 
2909 			continue;
2910 		}
2911 
2912 		/* May be a new root stored in mast->bn */
2913 		if (mas_is_root_limits(mast->orig_l))
2914 			break;
2915 
2916 		mast_spanning_rebalance(mast);
2917 
2918 		/* rebalancing from other nodes may require another loop. */
2919 		if (!count)
2920 			count++;
2921 	}
2922 
2923 	l_mas.node = mt_mk_node(ma_mnode_ptr(mas_pop_node(mas)),
2924 				mte_node_type(mast->orig_l->node));
2925 
2926 	mab_mas_cp(mast->bn, 0, mt_slots[mast->bn->type] - 1, &l_mas, true);
2927 	new_height++;
2928 	mas_set_parent(mas, left, l_mas.node, slot);
2929 	if (middle)
2930 		mas_set_parent(mas, middle, l_mas.node, ++slot);
2931 
2932 	if (right)
2933 		mas_set_parent(mas, right, l_mas.node, ++slot);
2934 
2935 	if (mas_is_root_limits(mast->l)) {
2936 new_root:
2937 		mas_mn(mast->l)->parent = ma_parent_ptr(mas_tree_parent(mas));
2938 		while (!mte_is_root(mast->orig_l->node))
2939 			mast_ascend(mast);
2940 	} else {
2941 		mas_mn(&l_mas)->parent = mas_mn(mast->orig_l)->parent;
2942 	}
2943 
2944 	old_enode = mast->orig_l->node;
2945 	mas->depth = l_mas.depth;
2946 	mas->node = l_mas.node;
2947 	mas->min = l_mas.min;
2948 	mas->max = l_mas.max;
2949 	mas->offset = l_mas.offset;
2950 	mas_wmb_replace(mas, old_enode, new_height);
2951 	mtree_range_walk(mas);
2952 	return;
2953 }
2954 
2955 /*
2956  * mas_rebalance() - Rebalance a given node.
2957  * @mas: The maple state
2958  * @b_node: The big maple node.
2959  *
2960  * Rebalance two nodes into a single node or two new nodes that are sufficient.
2961  * Continue upwards until tree is sufficient.
2962  */
2963 static inline void mas_rebalance(struct ma_state *mas,
2964 				struct maple_big_node *b_node)
2965 {
2966 	char empty_count = mas_mt_height(mas);
2967 	struct maple_subtree_state mast;
2968 	unsigned char shift, b_end = ++b_node->b_end;
2969 
2970 	MA_STATE(l_mas, mas->tree, mas->index, mas->last);
2971 	MA_STATE(r_mas, mas->tree, mas->index, mas->last);
2972 
2973 	trace_ma_op(__func__, mas);
2974 
2975 	/*
2976 	 * Rebalancing occurs if a node is insufficient.  Data is rebalanced
2977 	 * against the node to the right if it exists, otherwise the node to the
2978 	 * left of this node is rebalanced against this node.  If rebalancing
2979 	 * causes just one node to be produced instead of two, then the parent
2980 	 * is also examined and rebalanced if it is insufficient.  Every level
2981 	 * tries to combine the data in the same way.  If one node contains the
2982 	 * entire range of the tree, then that node is used as a new root node.
2983 	 */
2984 
2985 	mast.orig_l = &l_mas;
2986 	mast.orig_r = &r_mas;
2987 	mast.bn = b_node;
2988 	mast.bn->type = mte_node_type(mas->node);
2989 
2990 	l_mas = r_mas = *mas;
2991 
2992 	if (mas_next_sibling(&r_mas)) {
2993 		mas_mab_cp(&r_mas, 0, mt_slot_count(r_mas.node), b_node, b_end);
2994 		r_mas.last = r_mas.index = r_mas.max;
2995 	} else {
2996 		mas_prev_sibling(&l_mas);
2997 		shift = mas_data_end(&l_mas) + 1;
2998 		mab_shift_right(b_node, shift);
2999 		mas->offset += shift;
3000 		mas_mab_cp(&l_mas, 0, shift - 1, b_node, 0);
3001 		b_node->b_end = shift + b_end;
3002 		l_mas.index = l_mas.last = l_mas.min;
3003 	}
3004 
3005 	return mas_spanning_rebalance(mas, &mast, empty_count);
3006 }
3007 
3008 /*
3009  * mas_destroy_rebalance() - Rebalance left-most node while destroying the maple
3010  * state.
3011  * @mas: The maple state
3012  * @end: The end of the left-most node.
3013  *
3014  * During a mass-insert event (such as forking), it may be necessary to
3015  * rebalance the left-most node when it is not sufficient.
3016  */
3017 static inline void mas_destroy_rebalance(struct ma_state *mas, unsigned char end)
3018 {
3019 	enum maple_type mt = mte_node_type(mas->node);
3020 	struct maple_node reuse, *newnode, *parent, *new_left, *left, *node;
3021 	struct maple_enode *eparent, *old_eparent;
3022 	unsigned char offset, tmp, split = mt_slots[mt] / 2;
3023 	void __rcu **l_slots, **slots;
3024 	unsigned long *l_pivs, *pivs, gap;
3025 	bool in_rcu = mt_in_rcu(mas->tree);
3026 	unsigned char new_height = mas_mt_height(mas);
3027 
3028 	MA_STATE(l_mas, mas->tree, mas->index, mas->last);
3029 
3030 	l_mas = *mas;
3031 	mas_prev_sibling(&l_mas);
3032 
3033 	/* set up node. */
3034 	if (in_rcu) {
3035 		newnode = mas_pop_node(mas);
3036 	} else {
3037 		newnode = &reuse;
3038 	}
3039 
3040 	node = mas_mn(mas);
3041 	newnode->parent = node->parent;
3042 	slots = ma_slots(newnode, mt);
3043 	pivs = ma_pivots(newnode, mt);
3044 	left = mas_mn(&l_mas);
3045 	l_slots = ma_slots(left, mt);
3046 	l_pivs = ma_pivots(left, mt);
3047 	if (!l_slots[split])
3048 		split++;
3049 	tmp = mas_data_end(&l_mas) - split;
3050 
3051 	memcpy(slots, l_slots + split + 1, sizeof(void *) * tmp);
3052 	memcpy(pivs, l_pivs + split + 1, sizeof(unsigned long) * tmp);
3053 	pivs[tmp] = l_mas.max;
3054 	memcpy(slots + tmp, ma_slots(node, mt), sizeof(void *) * end);
3055 	memcpy(pivs + tmp, ma_pivots(node, mt), sizeof(unsigned long) * end);
3056 
3057 	l_mas.max = l_pivs[split];
3058 	mas->min = l_mas.max + 1;
3059 	old_eparent = mt_mk_node(mte_parent(l_mas.node),
3060 			     mas_parent_type(&l_mas, l_mas.node));
3061 	tmp += end;
3062 	if (!in_rcu) {
3063 		unsigned char max_p = mt_pivots[mt];
3064 		unsigned char max_s = mt_slots[mt];
3065 
3066 		if (tmp < max_p)
3067 			memset(pivs + tmp, 0,
3068 			       sizeof(unsigned long) * (max_p - tmp));
3069 
3070 		if (tmp < mt_slots[mt])
3071 			memset(slots + tmp, 0, sizeof(void *) * (max_s - tmp));
3072 
3073 		memcpy(node, newnode, sizeof(struct maple_node));
3074 		ma_set_meta(node, mt, 0, tmp - 1);
3075 		mte_set_pivot(old_eparent, mte_parent_slot(l_mas.node),
3076 			      l_pivs[split]);
3077 
3078 		/* Remove data from l_pivs. */
3079 		tmp = split + 1;
3080 		memset(l_pivs + tmp, 0, sizeof(unsigned long) * (max_p - tmp));
3081 		memset(l_slots + tmp, 0, sizeof(void *) * (max_s - tmp));
3082 		ma_set_meta(left, mt, 0, split);
3083 		eparent = old_eparent;
3084 
3085 		goto done;
3086 	}
3087 
3088 	/* RCU requires replacing both l_mas, mas, and parent. */
3089 	mas->node = mt_mk_node(newnode, mt);
3090 	ma_set_meta(newnode, mt, 0, tmp);
3091 
3092 	new_left = mas_pop_node(mas);
3093 	new_left->parent = left->parent;
3094 	mt = mte_node_type(l_mas.node);
3095 	slots = ma_slots(new_left, mt);
3096 	pivs = ma_pivots(new_left, mt);
3097 	memcpy(slots, l_slots, sizeof(void *) * split);
3098 	memcpy(pivs, l_pivs, sizeof(unsigned long) * split);
3099 	ma_set_meta(new_left, mt, 0, split);
3100 	l_mas.node = mt_mk_node(new_left, mt);
3101 
3102 	/* replace parent. */
3103 	offset = mte_parent_slot(mas->node);
3104 	mt = mas_parent_type(&l_mas, l_mas.node);
3105 	parent = mas_pop_node(mas);
3106 	slots = ma_slots(parent, mt);
3107 	pivs = ma_pivots(parent, mt);
3108 	memcpy(parent, mte_to_node(old_eparent), sizeof(struct maple_node));
3109 	rcu_assign_pointer(slots[offset], mas->node);
3110 	rcu_assign_pointer(slots[offset - 1], l_mas.node);
3111 	pivs[offset - 1] = l_mas.max;
3112 	eparent = mt_mk_node(parent, mt);
3113 done:
3114 	gap = mas_leaf_max_gap(mas);
3115 	mte_set_gap(eparent, mte_parent_slot(mas->node), gap);
3116 	gap = mas_leaf_max_gap(&l_mas);
3117 	mte_set_gap(eparent, mte_parent_slot(l_mas.node), gap);
3118 	mas_ascend(mas);
3119 
3120 	if (in_rcu) {
3121 		mas_replace_node(mas, old_eparent, new_height);
3122 		mas_adopt_children(mas, mas->node);
3123 	}
3124 
3125 	mas_update_gap(mas);
3126 }
3127 
3128 /*
3129  * mas_split_final_node() - Split the final node in a subtree operation.
3130  * @mast: the maple subtree state
3131  * @mas: The maple state
3132  */
3133 static inline void mas_split_final_node(struct maple_subtree_state *mast,
3134 					struct ma_state *mas)
3135 {
3136 	struct maple_enode *ancestor;
3137 
3138 	if (mte_is_root(mas->node)) {
3139 		if (mt_is_alloc(mas->tree))
3140 			mast->bn->type = maple_arange_64;
3141 		else
3142 			mast->bn->type = maple_range_64;
3143 	}
3144 	/*
3145 	 * Only a single node is used here, could be root.
3146 	 * The Big_node data should just fit in a single node.
3147 	 */
3148 	ancestor = mas_new_ma_node(mas, mast->bn);
3149 	mas_set_parent(mas, mast->l->node, ancestor, mast->l->offset);
3150 	mas_set_parent(mas, mast->r->node, ancestor, mast->r->offset);
3151 	mte_to_node(ancestor)->parent = mas_mn(mas)->parent;
3152 
3153 	mast->l->node = ancestor;
3154 	mab_mas_cp(mast->bn, 0, mt_slots[mast->bn->type] - 1, mast->l, true);
3155 	mas->offset = mast->bn->b_end - 1;
3156 }
3157 
3158 /*
3159  * mast_fill_bnode() - Copy data into the big node in the subtree state
3160  * @mast: The maple subtree state
3161  * @mas: the maple state
3162  * @skip: The number of entries to skip for new nodes insertion.
3163  */
3164 static inline void mast_fill_bnode(struct maple_subtree_state *mast,
3165 					 struct ma_state *mas,
3166 					 unsigned char skip)
3167 {
3168 	bool cp = true;
3169 	unsigned char split;
3170 
3171 	memset(mast->bn, 0, sizeof(struct maple_big_node));
3172 
3173 	if (mte_is_root(mas->node)) {
3174 		cp = false;
3175 	} else {
3176 		mas_ascend(mas);
3177 		mas->offset = mte_parent_slot(mas->node);
3178 	}
3179 
3180 	if (cp && mast->l->offset)
3181 		mas_mab_cp(mas, 0, mast->l->offset - 1, mast->bn, 0);
3182 
3183 	split = mast->bn->b_end;
3184 	mab_set_b_end(mast->bn, mast->l, mast->l->node);
3185 	mast->r->offset = mast->bn->b_end;
3186 	mab_set_b_end(mast->bn, mast->r, mast->r->node);
3187 	if (mast->bn->pivot[mast->bn->b_end - 1] == mas->max)
3188 		cp = false;
3189 
3190 	if (cp)
3191 		mas_mab_cp(mas, split + skip, mt_slot_count(mas->node) - 1,
3192 			   mast->bn, mast->bn->b_end);
3193 
3194 	mast->bn->b_end--;
3195 	mast->bn->type = mte_node_type(mas->node);
3196 }
3197 
3198 /*
3199  * mast_split_data() - Split the data in the subtree state big node into regular
3200  * nodes.
3201  * @mast: The maple subtree state
3202  * @mas: The maple state
3203  * @split: The location to split the big node
3204  */
3205 static inline void mast_split_data(struct maple_subtree_state *mast,
3206 	   struct ma_state *mas, unsigned char split)
3207 {
3208 	unsigned char p_slot;
3209 
3210 	mab_mas_cp(mast->bn, 0, split, mast->l, true);
3211 	mte_set_pivot(mast->r->node, 0, mast->r->max);
3212 	mab_mas_cp(mast->bn, split + 1, mast->bn->b_end, mast->r, false);
3213 	mast->l->offset = mte_parent_slot(mas->node);
3214 	mast->l->max = mast->bn->pivot[split];
3215 	mast->r->min = mast->l->max + 1;
3216 	if (mte_is_leaf(mas->node))
3217 		return;
3218 
3219 	p_slot = mast->orig_l->offset;
3220 	mas_set_split_parent(mast->orig_l, mast->l->node, mast->r->node,
3221 			     &p_slot, split);
3222 	mas_set_split_parent(mast->orig_r, mast->l->node, mast->r->node,
3223 			     &p_slot, split);
3224 }
3225 
3226 /*
3227  * mas_push_data() - Instead of splitting a node, it is beneficial to push the
3228  * data to the right or left node if there is room.
3229  * @mas: The maple state
3230  * @mast: The maple subtree state
3231  * @left: Push left or not.
3232  *
3233  * Keeping the height of the tree low means faster lookups.
3234  *
3235  * Return: True if pushed, false otherwise.
3236  */
3237 static inline bool mas_push_data(struct ma_state *mas,
3238 				struct maple_subtree_state *mast, bool left)
3239 {
3240 	unsigned char slot_total = mast->bn->b_end;
3241 	unsigned char end, space, split;
3242 
3243 	MA_STATE(tmp_mas, mas->tree, mas->index, mas->last);
3244 	tmp_mas = *mas;
3245 	tmp_mas.depth = mast->l->depth;
3246 
3247 	if (left && !mas_prev_sibling(&tmp_mas))
3248 		return false;
3249 	else if (!left && !mas_next_sibling(&tmp_mas))
3250 		return false;
3251 
3252 	end = mas_data_end(&tmp_mas);
3253 	slot_total += end;
3254 	space = 2 * mt_slot_count(mas->node) - 2;
3255 	/* -2 instead of -1 to ensure there isn't a triple split */
3256 	if (ma_is_leaf(mast->bn->type))
3257 		space--;
3258 
3259 	if (mas->max == ULONG_MAX)
3260 		space--;
3261 
3262 	if (slot_total >= space)
3263 		return false;
3264 
3265 	/* Get the data; Fill mast->bn */
3266 	mast->bn->b_end++;
3267 	if (left) {
3268 		mab_shift_right(mast->bn, end + 1);
3269 		mas_mab_cp(&tmp_mas, 0, end, mast->bn, 0);
3270 		mast->bn->b_end = slot_total + 1;
3271 	} else {
3272 		mas_mab_cp(&tmp_mas, 0, end, mast->bn, mast->bn->b_end);
3273 	}
3274 
3275 	/* Configure mast for splitting of mast->bn */
3276 	split = mt_slots[mast->bn->type] - 2;
3277 	if (left) {
3278 		/*  Switch mas to prev node  */
3279 		*mas = tmp_mas;
3280 		/* Start using mast->l for the left side. */
3281 		tmp_mas.node = mast->l->node;
3282 		*mast->l = tmp_mas;
3283 	} else {
3284 		tmp_mas.node = mast->r->node;
3285 		*mast->r = tmp_mas;
3286 		split = slot_total - split;
3287 	}
3288 	split = mab_no_null_split(mast->bn, split, mt_slots[mast->bn->type]);
3289 	/* Update parent slot for split calculation. */
3290 	if (left)
3291 		mast->orig_l->offset += end + 1;
3292 
3293 	mast_split_data(mast, mas, split);
3294 	mast_fill_bnode(mast, mas, 2);
3295 	mas_split_final_node(mast, mas);
3296 	return true;
3297 }
3298 
3299 /*
3300  * mas_split() - Split data that is too big for one node into two.
3301  * @mas: The maple state
3302  * @b_node: The maple big node
3303  */
3304 static void mas_split(struct ma_state *mas, struct maple_big_node *b_node)
3305 {
3306 	struct maple_subtree_state mast;
3307 	int height = 0;
3308 	unsigned int orig_height = mas_mt_height(mas);
3309 	unsigned char mid_split, split = 0;
3310 	struct maple_enode *old;
3311 
3312 	/*
3313 	 * Splitting is handled differently from any other B-tree; the Maple
3314 	 * Tree splits upwards.  Splitting up means that the split operation
3315 	 * occurs when the walk of the tree hits the leaves and not on the way
3316 	 * down.  The reason for splitting up is that it is impossible to know
3317 	 * how much space will be needed until the leaf is (or leaves are)
3318 	 * reached.  Since overwriting data is allowed and a range could
3319 	 * overwrite more than one range or result in changing one entry into 3
3320 	 * entries, it is impossible to know if a split is required until the
3321 	 * data is examined.
3322 	 *
3323 	 * Splitting is a balancing act between keeping allocations to a minimum
3324 	 * and avoiding a 'jitter' event where a tree is expanded to make room
3325 	 * for an entry followed by a contraction when the entry is removed.  To
3326 	 * accomplish the balance, there are empty slots remaining in both left
3327 	 * and right nodes after a split.
3328 	 */
3329 	MA_STATE(l_mas, mas->tree, mas->index, mas->last);
3330 	MA_STATE(r_mas, mas->tree, mas->index, mas->last);
3331 	MA_STATE(prev_l_mas, mas->tree, mas->index, mas->last);
3332 	MA_STATE(prev_r_mas, mas->tree, mas->index, mas->last);
3333 
3334 	trace_ma_op(__func__, mas);
3335 
3336 	mast.l = &l_mas;
3337 	mast.r = &r_mas;
3338 	mast.orig_l = &prev_l_mas;
3339 	mast.orig_r = &prev_r_mas;
3340 	mast.bn = b_node;
3341 
3342 	while (height++ <= orig_height) {
3343 		if (mt_slots[b_node->type] > b_node->b_end) {
3344 			mas_split_final_node(&mast, mas);
3345 			break;
3346 		}
3347 
3348 		l_mas = r_mas = *mas;
3349 		l_mas.node = mas_new_ma_node(mas, b_node);
3350 		r_mas.node = mas_new_ma_node(mas, b_node);
3351 		/*
3352 		 * Another way that 'jitter' is avoided is to terminate a split up early if the
3353 		 * left or right node has space to spare.  This is referred to as "pushing left"
3354 		 * or "pushing right" and is similar to the B* tree, except the nodes left or
3355 		 * right can rarely be reused due to RCU, but the ripple upwards is halted which
3356 		 * is a significant savings.
3357 		 */
3358 		/* Try to push left. */
3359 		if (mas_push_data(mas, &mast, true)) {
3360 			height++;
3361 			break;
3362 		}
3363 		/* Try to push right. */
3364 		if (mas_push_data(mas, &mast, false)) {
3365 			height++;
3366 			break;
3367 		}
3368 
3369 		split = mab_calc_split(mas, b_node, &mid_split);
3370 		mast_split_data(&mast, mas, split);
3371 		/*
3372 		 * Usually correct, mab_mas_cp in the above call overwrites
3373 		 * r->max.
3374 		 */
3375 		mast.r->max = mas->max;
3376 		mast_fill_bnode(&mast, mas, 1);
3377 		prev_l_mas = *mast.l;
3378 		prev_r_mas = *mast.r;
3379 	}
3380 
3381 	/* Set the original node as dead */
3382 	old = mas->node;
3383 	mas->node = l_mas.node;
3384 	mas_wmb_replace(mas, old, height);
3385 	mtree_range_walk(mas);
3386 	return;
3387 }
3388 
3389 /*
3390  * mas_commit_b_node() - Commit the big node into the tree.
3391  * @wr_mas: The maple write state
3392  * @b_node: The maple big node
3393  */
3394 static noinline_for_kasan void mas_commit_b_node(struct ma_wr_state *wr_mas,
3395 			    struct maple_big_node *b_node)
3396 {
3397 	enum store_type type = wr_mas->mas->store_type;
3398 
3399 	WARN_ON_ONCE(type != wr_rebalance && type != wr_split_store);
3400 
3401 	if (type == wr_rebalance)
3402 		return mas_rebalance(wr_mas->mas, b_node);
3403 
3404 	return mas_split(wr_mas->mas, b_node);
3405 }
3406 
3407 /*
3408  * mas_root_expand() - Expand a root to a node
3409  * @mas: The maple state
3410  * @entry: The entry to store into the tree
3411  */
3412 static inline void mas_root_expand(struct ma_state *mas, void *entry)
3413 {
3414 	void *contents = mas_root_locked(mas);
3415 	enum maple_type type = maple_leaf_64;
3416 	struct maple_node *node;
3417 	void __rcu **slots;
3418 	unsigned long *pivots;
3419 	int slot = 0;
3420 
3421 	node = mas_pop_node(mas);
3422 	pivots = ma_pivots(node, type);
3423 	slots = ma_slots(node, type);
3424 	node->parent = ma_parent_ptr(mas_tree_parent(mas));
3425 	mas->node = mt_mk_node(node, type);
3426 	mas->status = ma_active;
3427 
3428 	if (mas->index) {
3429 		if (contents) {
3430 			rcu_assign_pointer(slots[slot], contents);
3431 			if (likely(mas->index > 1))
3432 				slot++;
3433 		}
3434 		pivots[slot++] = mas->index - 1;
3435 	}
3436 
3437 	rcu_assign_pointer(slots[slot], entry);
3438 	mas->offset = slot;
3439 	pivots[slot] = mas->last;
3440 	if (mas->last != ULONG_MAX)
3441 		pivots[++slot] = ULONG_MAX;
3442 
3443 	mt_set_height(mas->tree, 1);
3444 	ma_set_meta(node, maple_leaf_64, 0, slot);
3445 	/* swap the new root into the tree */
3446 	rcu_assign_pointer(mas->tree->ma_root, mte_mk_root(mas->node));
3447 	return;
3448 }
3449 
3450 /*
3451  * mas_store_root() - Storing value into root.
3452  * @mas: The maple state
3453  * @entry: The entry to store.
3454  *
3455  * There is no root node now and we are storing a value into the root - this
3456  * function either assigns the pointer or expands into a node.
3457  */
3458 static inline void mas_store_root(struct ma_state *mas, void *entry)
3459 {
3460 	if (!entry) {
3461 		if (!mas->index)
3462 			rcu_assign_pointer(mas->tree->ma_root, NULL);
3463 	} else if (likely((mas->last != 0) || (mas->index != 0)))
3464 		mas_root_expand(mas, entry);
3465 	else if (((unsigned long) (entry) & 3) == 2)
3466 		mas_root_expand(mas, entry);
3467 	else {
3468 		rcu_assign_pointer(mas->tree->ma_root, entry);
3469 		mas->status = ma_start;
3470 	}
3471 }
3472 
3473 /*
3474  * mas_is_span_wr() - Check if the write needs to be treated as a write that
3475  * spans the node.
3476  * @wr_mas: The maple write state
3477  *
3478  * Spanning writes are writes that start in one node and end in another OR if
3479  * the write of a %NULL will cause the node to end with a %NULL.
3480  *
3481  * Return: True if this is a spanning write, false otherwise.
3482  */
3483 static bool mas_is_span_wr(struct ma_wr_state *wr_mas)
3484 {
3485 	unsigned long max = wr_mas->r_max;
3486 	unsigned long last = wr_mas->mas->last;
3487 	enum maple_type type = wr_mas->type;
3488 	void *entry = wr_mas->entry;
3489 
3490 	/* Contained in this pivot, fast path */
3491 	if (last < max)
3492 		return false;
3493 
3494 	if (ma_is_leaf(type)) {
3495 		max = wr_mas->mas->max;
3496 		if (last < max)
3497 			return false;
3498 	}
3499 
3500 	if (last == max) {
3501 		/*
3502 		 * The last entry of leaf node cannot be NULL unless it is the
3503 		 * rightmost node (writing ULONG_MAX), otherwise it spans slots.
3504 		 */
3505 		if (entry || last == ULONG_MAX)
3506 			return false;
3507 	}
3508 
3509 	trace_ma_write(__func__, wr_mas->mas, wr_mas->r_max, entry);
3510 	return true;
3511 }
3512 
3513 static inline void mas_wr_walk_descend(struct ma_wr_state *wr_mas)
3514 {
3515 	wr_mas->type = mte_node_type(wr_mas->mas->node);
3516 	mas_wr_node_walk(wr_mas);
3517 	wr_mas->slots = ma_slots(wr_mas->node, wr_mas->type);
3518 }
3519 
3520 static inline void mas_wr_walk_traverse(struct ma_wr_state *wr_mas)
3521 {
3522 	wr_mas->mas->max = wr_mas->r_max;
3523 	wr_mas->mas->min = wr_mas->r_min;
3524 	wr_mas->mas->node = wr_mas->content;
3525 	wr_mas->mas->offset = 0;
3526 	wr_mas->mas->depth++;
3527 }
3528 /*
3529  * mas_wr_walk() - Walk the tree for a write.
3530  * @wr_mas: The maple write state
3531  *
3532  * Uses mas_slot_locked() and does not need to worry about dead nodes.
3533  *
3534  * Return: True if it's contained in a node, false on spanning write.
3535  */
3536 static bool mas_wr_walk(struct ma_wr_state *wr_mas)
3537 {
3538 	struct ma_state *mas = wr_mas->mas;
3539 
3540 	while (true) {
3541 		mas_wr_walk_descend(wr_mas);
3542 		if (unlikely(mas_is_span_wr(wr_mas)))
3543 			return false;
3544 
3545 		wr_mas->content = mas_slot_locked(mas, wr_mas->slots,
3546 						  mas->offset);
3547 		if (ma_is_leaf(wr_mas->type))
3548 			return true;
3549 
3550 		if (mas->end < mt_slots[wr_mas->type] - 1)
3551 			wr_mas->vacant_height = mas->depth + 1;
3552 
3553 		if (ma_is_root(mas_mn(mas))) {
3554 			/* root needs more than 2 entries to be sufficient + 1 */
3555 			if (mas->end > 2)
3556 				wr_mas->sufficient_height = 1;
3557 		} else if (mas->end > mt_min_slots[wr_mas->type] + 1)
3558 			wr_mas->sufficient_height = mas->depth + 1;
3559 
3560 		mas_wr_walk_traverse(wr_mas);
3561 	}
3562 
3563 	return true;
3564 }
3565 
3566 static void mas_wr_walk_index(struct ma_wr_state *wr_mas)
3567 {
3568 	struct ma_state *mas = wr_mas->mas;
3569 
3570 	while (true) {
3571 		mas_wr_walk_descend(wr_mas);
3572 		wr_mas->content = mas_slot_locked(mas, wr_mas->slots,
3573 						  mas->offset);
3574 		if (ma_is_leaf(wr_mas->type))
3575 			return;
3576 		mas_wr_walk_traverse(wr_mas);
3577 	}
3578 }
3579 /*
3580  * mas_extend_spanning_null() - Extend a store of a %NULL to include surrounding %NULLs.
3581  * @l_wr_mas: The left maple write state
3582  * @r_wr_mas: The right maple write state
3583  */
3584 static inline void mas_extend_spanning_null(struct ma_wr_state *l_wr_mas,
3585 					    struct ma_wr_state *r_wr_mas)
3586 {
3587 	struct ma_state *r_mas = r_wr_mas->mas;
3588 	struct ma_state *l_mas = l_wr_mas->mas;
3589 	unsigned char l_slot;
3590 
3591 	l_slot = l_mas->offset;
3592 	if (!l_wr_mas->content)
3593 		l_mas->index = l_wr_mas->r_min;
3594 
3595 	if ((l_mas->index == l_wr_mas->r_min) &&
3596 		 (l_slot &&
3597 		  !mas_slot_locked(l_mas, l_wr_mas->slots, l_slot - 1))) {
3598 		if (l_slot > 1)
3599 			l_mas->index = l_wr_mas->pivots[l_slot - 2] + 1;
3600 		else
3601 			l_mas->index = l_mas->min;
3602 
3603 		l_mas->offset = l_slot - 1;
3604 	}
3605 
3606 	if (!r_wr_mas->content) {
3607 		if (r_mas->last < r_wr_mas->r_max)
3608 			r_mas->last = r_wr_mas->r_max;
3609 		r_mas->offset++;
3610 	} else if ((r_mas->last == r_wr_mas->r_max) &&
3611 	    (r_mas->last < r_mas->max) &&
3612 	    !mas_slot_locked(r_mas, r_wr_mas->slots, r_mas->offset + 1)) {
3613 		r_mas->last = mas_safe_pivot(r_mas, r_wr_mas->pivots,
3614 					     r_wr_mas->type, r_mas->offset + 1);
3615 		r_mas->offset++;
3616 	}
3617 }
3618 
3619 static inline void *mas_state_walk(struct ma_state *mas)
3620 {
3621 	void *entry;
3622 
3623 	entry = mas_start(mas);
3624 	if (mas_is_none(mas))
3625 		return NULL;
3626 
3627 	if (mas_is_ptr(mas))
3628 		return entry;
3629 
3630 	return mtree_range_walk(mas);
3631 }
3632 
3633 /*
3634  * mtree_lookup_walk() - Internal quick lookup that does not keep maple state up
3635  * to date.
3636  *
3637  * @mas: The maple state.
3638  *
3639  * Note: Leaves mas in undesirable state.
3640  * Return: The entry for @mas->index or %NULL on dead node.
3641  */
3642 static inline void *mtree_lookup_walk(struct ma_state *mas)
3643 {
3644 	unsigned long *pivots;
3645 	unsigned char offset;
3646 	struct maple_node *node;
3647 	struct maple_enode *next;
3648 	enum maple_type type;
3649 	void __rcu **slots;
3650 	unsigned char end;
3651 
3652 	next = mas->node;
3653 	do {
3654 		node = mte_to_node(next);
3655 		type = mte_node_type(next);
3656 		pivots = ma_pivots(node, type);
3657 		end = mt_pivots[type];
3658 		offset = 0;
3659 		do {
3660 			if (pivots[offset] >= mas->index)
3661 				break;
3662 		} while (++offset < end);
3663 
3664 		slots = ma_slots(node, type);
3665 		next = mt_slot(mas->tree, slots, offset);
3666 		if (unlikely(ma_dead_node(node)))
3667 			goto dead_node;
3668 	} while (!ma_is_leaf(type));
3669 
3670 	return (void *)next;
3671 
3672 dead_node:
3673 	mas_reset(mas);
3674 	return NULL;
3675 }
3676 
3677 static void mte_destroy_walk(struct maple_enode *, struct maple_tree *);
3678 /*
3679  * mas_new_root() - Create a new root node that only contains the entry passed
3680  * in.
3681  * @mas: The maple state
3682  * @entry: The entry to store.
3683  *
3684  * Only valid when the index == 0 and the last == ULONG_MAX
3685  */
3686 static inline void mas_new_root(struct ma_state *mas, void *entry)
3687 {
3688 	struct maple_enode *root = mas_root_locked(mas);
3689 	enum maple_type type = maple_leaf_64;
3690 	struct maple_node *node;
3691 	void __rcu **slots;
3692 	unsigned long *pivots;
3693 
3694 	WARN_ON_ONCE(mas->index || mas->last != ULONG_MAX);
3695 
3696 	if (!entry) {
3697 		mt_set_height(mas->tree, 0);
3698 		rcu_assign_pointer(mas->tree->ma_root, entry);
3699 		mas->status = ma_start;
3700 		goto done;
3701 	}
3702 
3703 	node = mas_pop_node(mas);
3704 	pivots = ma_pivots(node, type);
3705 	slots = ma_slots(node, type);
3706 	node->parent = ma_parent_ptr(mas_tree_parent(mas));
3707 	mas->node = mt_mk_node(node, type);
3708 	mas->status = ma_active;
3709 	rcu_assign_pointer(slots[0], entry);
3710 	pivots[0] = mas->last;
3711 	mt_set_height(mas->tree, 1);
3712 	rcu_assign_pointer(mas->tree->ma_root, mte_mk_root(mas->node));
3713 
3714 done:
3715 	if (xa_is_node(root))
3716 		mte_destroy_walk(root, mas->tree);
3717 
3718 	return;
3719 }
3720 /*
3721  * mas_wr_spanning_store() - Create a subtree with the store operation completed
3722  * and new nodes where necessary, then place the sub-tree in the actual tree.
3723  * Note that mas is expected to point to the node which caused the store to
3724  * span.
3725  * @wr_mas: The maple write state
3726  */
3727 static noinline void mas_wr_spanning_store(struct ma_wr_state *wr_mas)
3728 {
3729 	struct maple_subtree_state mast;
3730 	struct maple_big_node b_node;
3731 	struct ma_state *mas;
3732 	unsigned char height;
3733 
3734 	/* Left and Right side of spanning store */
3735 	MA_STATE(l_mas, NULL, 0, 0);
3736 	MA_STATE(r_mas, NULL, 0, 0);
3737 	MA_WR_STATE(r_wr_mas, &r_mas, wr_mas->entry);
3738 	MA_WR_STATE(l_wr_mas, &l_mas, wr_mas->entry);
3739 
3740 	/*
3741 	 * A store operation that spans multiple nodes is called a spanning
3742 	 * store and is handled early in the store call stack by the function
3743 	 * mas_is_span_wr().  When a spanning store is identified, the maple
3744 	 * state is duplicated.  The first maple state walks the left tree path
3745 	 * to ``index``, the duplicate walks the right tree path to ``last``.
3746 	 * The data in the two nodes are combined into a single node, two nodes,
3747 	 * or possibly three nodes (see the 3-way split above).  A ``NULL``
3748 	 * written to the last entry of a node is considered a spanning store as
3749 	 * a rebalance is required for the operation to complete and an overflow
3750 	 * of data may happen.
3751 	 */
3752 	mas = wr_mas->mas;
3753 	trace_ma_op(__func__, mas);
3754 
3755 	if (unlikely(!mas->index && mas->last == ULONG_MAX))
3756 		return mas_new_root(mas, wr_mas->entry);
3757 	/*
3758 	 * Node rebalancing may occur due to this store, so there may be three new
3759 	 * entries per level plus a new root.
3760 	 */
3761 	height = mas_mt_height(mas);
3762 
3763 	/*
3764 	 * Set up right side.  Need to get to the next offset after the spanning
3765 	 * store to ensure it's not NULL and to combine both the next node and
3766 	 * the node with the start together.
3767 	 */
3768 	r_mas = *mas;
3769 	/* Avoid overflow, walk to next slot in the tree. */
3770 	if (r_mas.last + 1)
3771 		r_mas.last++;
3772 
3773 	r_mas.index = r_mas.last;
3774 	mas_wr_walk_index(&r_wr_mas);
3775 	r_mas.last = r_mas.index = mas->last;
3776 
3777 	/* Set up left side. */
3778 	l_mas = *mas;
3779 	mas_wr_walk_index(&l_wr_mas);
3780 
3781 	if (!wr_mas->entry) {
3782 		mas_extend_spanning_null(&l_wr_mas, &r_wr_mas);
3783 		mas->offset = l_mas.offset;
3784 		mas->index = l_mas.index;
3785 		mas->last = l_mas.last = r_mas.last;
3786 	}
3787 
3788 	/* expanding NULLs may make this cover the entire range */
3789 	if (!l_mas.index && r_mas.last == ULONG_MAX) {
3790 		mas_set_range(mas, 0, ULONG_MAX);
3791 		return mas_new_root(mas, wr_mas->entry);
3792 	}
3793 
3794 	memset(&b_node, 0, sizeof(struct maple_big_node));
3795 	/* Copy l_mas and store the value in b_node. */
3796 	mas_store_b_node(&l_wr_mas, &b_node, l_mas.end);
3797 	/* Copy r_mas into b_node if there is anything to copy. */
3798 	if (r_mas.max > r_mas.last)
3799 		mas_mab_cp(&r_mas, r_mas.offset, r_mas.end,
3800 			   &b_node, b_node.b_end + 1);
3801 	else
3802 		b_node.b_end++;
3803 
3804 	/* Stop spanning searches by searching for just index. */
3805 	l_mas.index = l_mas.last = mas->index;
3806 
3807 	mast.bn = &b_node;
3808 	mast.orig_l = &l_mas;
3809 	mast.orig_r = &r_mas;
3810 	/* Combine l_mas and r_mas and split them up evenly again. */
3811 	return mas_spanning_rebalance(mas, &mast, height + 1);
3812 }
3813 
3814 /*
3815  * mas_wr_node_store() - Attempt to store the value in a node
3816  * @wr_mas: The maple write state
3817  *
3818  * Attempts to reuse the node, but may allocate.
3819  */
3820 static inline void mas_wr_node_store(struct ma_wr_state *wr_mas,
3821 				     unsigned char new_end)
3822 {
3823 	struct ma_state *mas = wr_mas->mas;
3824 	void __rcu **dst_slots;
3825 	unsigned long *dst_pivots;
3826 	unsigned char dst_offset, offset_end = wr_mas->offset_end;
3827 	struct maple_node reuse, *newnode;
3828 	unsigned char copy_size, node_pivots = mt_pivots[wr_mas->type];
3829 	bool in_rcu = mt_in_rcu(mas->tree);
3830 	unsigned char height = mas_mt_height(mas);
3831 
3832 	if (mas->last == wr_mas->end_piv)
3833 		offset_end++; /* don't copy this offset */
3834 	else if (unlikely(wr_mas->r_max == ULONG_MAX))
3835 		mas_bulk_rebalance(mas, mas->end, wr_mas->type);
3836 
3837 	/* set up node. */
3838 	if (in_rcu) {
3839 		newnode = mas_pop_node(mas);
3840 	} else {
3841 		memset(&reuse, 0, sizeof(struct maple_node));
3842 		newnode = &reuse;
3843 	}
3844 
3845 	newnode->parent = mas_mn(mas)->parent;
3846 	dst_pivots = ma_pivots(newnode, wr_mas->type);
3847 	dst_slots = ma_slots(newnode, wr_mas->type);
3848 	/* Copy from start to insert point */
3849 	memcpy(dst_pivots, wr_mas->pivots, sizeof(unsigned long) * mas->offset);
3850 	memcpy(dst_slots, wr_mas->slots, sizeof(void *) * mas->offset);
3851 
3852 	/* Handle insert of new range starting after old range */
3853 	if (wr_mas->r_min < mas->index) {
3854 		rcu_assign_pointer(dst_slots[mas->offset], wr_mas->content);
3855 		dst_pivots[mas->offset++] = mas->index - 1;
3856 	}
3857 
3858 	/* Store the new entry and range end. */
3859 	if (mas->offset < node_pivots)
3860 		dst_pivots[mas->offset] = mas->last;
3861 	rcu_assign_pointer(dst_slots[mas->offset], wr_mas->entry);
3862 
3863 	/*
3864 	 * this range wrote to the end of the node or it overwrote the rest of
3865 	 * the data
3866 	 */
3867 	if (offset_end > mas->end)
3868 		goto done;
3869 
3870 	dst_offset = mas->offset + 1;
3871 	/* Copy to the end of node if necessary. */
3872 	copy_size = mas->end - offset_end + 1;
3873 	memcpy(dst_slots + dst_offset, wr_mas->slots + offset_end,
3874 	       sizeof(void *) * copy_size);
3875 	memcpy(dst_pivots + dst_offset, wr_mas->pivots + offset_end,
3876 	       sizeof(unsigned long) * (copy_size - 1));
3877 
3878 	if (new_end < node_pivots)
3879 		dst_pivots[new_end] = mas->max;
3880 
3881 done:
3882 	mas_leaf_set_meta(newnode, maple_leaf_64, new_end);
3883 	if (in_rcu) {
3884 		struct maple_enode *old_enode = mas->node;
3885 
3886 		mas->node = mt_mk_node(newnode, wr_mas->type);
3887 		mas_replace_node(mas, old_enode, height);
3888 	} else {
3889 		memcpy(wr_mas->node, newnode, sizeof(struct maple_node));
3890 	}
3891 	trace_ma_write(__func__, mas, 0, wr_mas->entry);
3892 	mas_update_gap(mas);
3893 	mas->end = new_end;
3894 	return;
3895 }
3896 
3897 /*
3898  * mas_wr_slot_store: Attempt to store a value in a slot.
3899  * @wr_mas: the maple write state
3900  */
3901 static inline void mas_wr_slot_store(struct ma_wr_state *wr_mas)
3902 {
3903 	struct ma_state *mas = wr_mas->mas;
3904 	unsigned char offset = mas->offset;
3905 	void __rcu **slots = wr_mas->slots;
3906 	bool gap = false;
3907 
3908 	gap |= !mt_slot_locked(mas->tree, slots, offset);
3909 	gap |= !mt_slot_locked(mas->tree, slots, offset + 1);
3910 
3911 	if (wr_mas->offset_end - offset == 1) {
3912 		if (mas->index == wr_mas->r_min) {
3913 			/* Overwriting the range and a part of the next one */
3914 			rcu_assign_pointer(slots[offset], wr_mas->entry);
3915 			wr_mas->pivots[offset] = mas->last;
3916 		} else {
3917 			/* Overwriting a part of the range and the next one */
3918 			rcu_assign_pointer(slots[offset + 1], wr_mas->entry);
3919 			wr_mas->pivots[offset] = mas->index - 1;
3920 			mas->offset++; /* Keep mas accurate. */
3921 		}
3922 	} else {
3923 		WARN_ON_ONCE(mt_in_rcu(mas->tree));
3924 		/*
3925 		 * Expand the range, only partially overwriting the previous and
3926 		 * next ranges
3927 		 */
3928 		gap |= !mt_slot_locked(mas->tree, slots, offset + 2);
3929 		rcu_assign_pointer(slots[offset + 1], wr_mas->entry);
3930 		wr_mas->pivots[offset] = mas->index - 1;
3931 		wr_mas->pivots[offset + 1] = mas->last;
3932 		mas->offset++; /* Keep mas accurate. */
3933 	}
3934 
3935 	trace_ma_write(__func__, mas, 0, wr_mas->entry);
3936 	/*
3937 	 * Only update gap when the new entry is empty or there is an empty
3938 	 * entry in the original two ranges.
3939 	 */
3940 	if (!wr_mas->entry || gap)
3941 		mas_update_gap(mas);
3942 
3943 	return;
3944 }
3945 
3946 static inline void mas_wr_extend_null(struct ma_wr_state *wr_mas)
3947 {
3948 	struct ma_state *mas = wr_mas->mas;
3949 
3950 	if (!wr_mas->slots[wr_mas->offset_end]) {
3951 		/* If this one is null, the next and prev are not */
3952 		mas->last = wr_mas->end_piv;
3953 	} else {
3954 		/* Check next slot(s) if we are overwriting the end */
3955 		if ((mas->last == wr_mas->end_piv) &&
3956 		    (mas->end != wr_mas->offset_end) &&
3957 		    !wr_mas->slots[wr_mas->offset_end + 1]) {
3958 			wr_mas->offset_end++;
3959 			if (wr_mas->offset_end == mas->end)
3960 				mas->last = mas->max;
3961 			else
3962 				mas->last = wr_mas->pivots[wr_mas->offset_end];
3963 			wr_mas->end_piv = mas->last;
3964 		}
3965 	}
3966 
3967 	if (!wr_mas->content) {
3968 		/* If this one is null, the next and prev are not */
3969 		mas->index = wr_mas->r_min;
3970 	} else {
3971 		/* Check prev slot if we are overwriting the start */
3972 		if (mas->index == wr_mas->r_min && mas->offset &&
3973 		    !wr_mas->slots[mas->offset - 1]) {
3974 			mas->offset--;
3975 			wr_mas->r_min = mas->index =
3976 				mas_safe_min(mas, wr_mas->pivots, mas->offset);
3977 			wr_mas->r_max = wr_mas->pivots[mas->offset];
3978 		}
3979 	}
3980 }
3981 
3982 static inline void mas_wr_end_piv(struct ma_wr_state *wr_mas)
3983 {
3984 	while ((wr_mas->offset_end < wr_mas->mas->end) &&
3985 	       (wr_mas->mas->last > wr_mas->pivots[wr_mas->offset_end]))
3986 		wr_mas->offset_end++;
3987 
3988 	if (wr_mas->offset_end < wr_mas->mas->end)
3989 		wr_mas->end_piv = wr_mas->pivots[wr_mas->offset_end];
3990 	else
3991 		wr_mas->end_piv = wr_mas->mas->max;
3992 }
3993 
3994 static inline unsigned char mas_wr_new_end(struct ma_wr_state *wr_mas)
3995 {
3996 	struct ma_state *mas = wr_mas->mas;
3997 	unsigned char new_end = mas->end + 2;
3998 
3999 	new_end -= wr_mas->offset_end - mas->offset;
4000 	if (wr_mas->r_min == mas->index)
4001 		new_end--;
4002 
4003 	if (wr_mas->end_piv == mas->last)
4004 		new_end--;
4005 
4006 	return new_end;
4007 }
4008 
4009 /*
4010  * mas_wr_append: Attempt to append
4011  * @wr_mas: the maple write state
4012  * @new_end: The end of the node after the modification
4013  *
4014  * This is currently unsafe in rcu mode since the end of the node may be cached
4015  * by readers while the node contents may be updated which could result in
4016  * inaccurate information.
4017  */
4018 static inline void mas_wr_append(struct ma_wr_state *wr_mas,
4019 		unsigned char new_end)
4020 {
4021 	struct ma_state *mas = wr_mas->mas;
4022 	void __rcu **slots;
4023 	unsigned char end = mas->end;
4024 
4025 	if (new_end < mt_pivots[wr_mas->type]) {
4026 		wr_mas->pivots[new_end] = wr_mas->pivots[end];
4027 		ma_set_meta(wr_mas->node, wr_mas->type, 0, new_end);
4028 	}
4029 
4030 	slots = wr_mas->slots;
4031 	if (new_end == end + 1) {
4032 		if (mas->last == wr_mas->r_max) {
4033 			/* Append to end of range */
4034 			rcu_assign_pointer(slots[new_end], wr_mas->entry);
4035 			wr_mas->pivots[end] = mas->index - 1;
4036 			mas->offset = new_end;
4037 		} else {
4038 			/* Append to start of range */
4039 			rcu_assign_pointer(slots[new_end], wr_mas->content);
4040 			wr_mas->pivots[end] = mas->last;
4041 			rcu_assign_pointer(slots[end], wr_mas->entry);
4042 		}
4043 	} else {
4044 		/* Append to the range without touching any boundaries. */
4045 		rcu_assign_pointer(slots[new_end], wr_mas->content);
4046 		wr_mas->pivots[end + 1] = mas->last;
4047 		rcu_assign_pointer(slots[end + 1], wr_mas->entry);
4048 		wr_mas->pivots[end] = mas->index - 1;
4049 		mas->offset = end + 1;
4050 	}
4051 
4052 	if (!wr_mas->content || !wr_mas->entry)
4053 		mas_update_gap(mas);
4054 
4055 	mas->end = new_end;
4056 	trace_ma_write(__func__, mas, new_end, wr_mas->entry);
4057 	return;
4058 }
4059 
4060 /*
4061  * mas_wr_bnode() - Slow path for a modification.
4062  * @wr_mas: The write maple state
4063  *
4064  * This is where split, rebalance end up.
4065  */
4066 static void mas_wr_bnode(struct ma_wr_state *wr_mas)
4067 {
4068 	struct maple_big_node b_node;
4069 
4070 	trace_ma_write(__func__, wr_mas->mas, 0, wr_mas->entry);
4071 	memset(&b_node, 0, sizeof(struct maple_big_node));
4072 	mas_store_b_node(wr_mas, &b_node, wr_mas->offset_end);
4073 	mas_commit_b_node(wr_mas, &b_node);
4074 }
4075 
4076 /*
4077  * mas_wr_store_entry() - Internal call to store a value
4078  * @wr_mas: The maple write state
4079  */
4080 static inline void mas_wr_store_entry(struct ma_wr_state *wr_mas)
4081 {
4082 	struct ma_state *mas = wr_mas->mas;
4083 	unsigned char new_end = mas_wr_new_end(wr_mas);
4084 
4085 	switch (mas->store_type) {
4086 	case wr_exact_fit:
4087 		rcu_assign_pointer(wr_mas->slots[mas->offset], wr_mas->entry);
4088 		if (!!wr_mas->entry ^ !!wr_mas->content)
4089 			mas_update_gap(mas);
4090 		break;
4091 	case wr_append:
4092 		mas_wr_append(wr_mas, new_end);
4093 		break;
4094 	case wr_slot_store:
4095 		mas_wr_slot_store(wr_mas);
4096 		break;
4097 	case wr_node_store:
4098 		mas_wr_node_store(wr_mas, new_end);
4099 		break;
4100 	case wr_spanning_store:
4101 		mas_wr_spanning_store(wr_mas);
4102 		break;
4103 	case wr_split_store:
4104 	case wr_rebalance:
4105 		mas_wr_bnode(wr_mas);
4106 		break;
4107 	case wr_new_root:
4108 		mas_new_root(mas, wr_mas->entry);
4109 		break;
4110 	case wr_store_root:
4111 		mas_store_root(mas, wr_mas->entry);
4112 		break;
4113 	case wr_invalid:
4114 		MT_BUG_ON(mas->tree, 1);
4115 	}
4116 
4117 	return;
4118 }
4119 
4120 static inline void mas_wr_prealloc_setup(struct ma_wr_state *wr_mas)
4121 {
4122 	struct ma_state *mas = wr_mas->mas;
4123 
4124 	if (!mas_is_active(mas)) {
4125 		if (mas_is_start(mas))
4126 			goto set_content;
4127 
4128 		if (unlikely(mas_is_paused(mas)))
4129 			goto reset;
4130 
4131 		if (unlikely(mas_is_none(mas)))
4132 			goto reset;
4133 
4134 		if (unlikely(mas_is_overflow(mas)))
4135 			goto reset;
4136 
4137 		if (unlikely(mas_is_underflow(mas)))
4138 			goto reset;
4139 	}
4140 
4141 	/*
4142 	 * A less strict version of mas_is_span_wr() where we allow spanning
4143 	 * writes within this node.  This is to stop partial walks in
4144 	 * mas_prealloc() from being reset.
4145 	 */
4146 	if (mas->last > mas->max)
4147 		goto reset;
4148 
4149 	if (wr_mas->entry)
4150 		goto set_content;
4151 
4152 	if (mte_is_leaf(mas->node) && mas->last == mas->max)
4153 		goto reset;
4154 
4155 	goto set_content;
4156 
4157 reset:
4158 	mas_reset(mas);
4159 set_content:
4160 	wr_mas->content = mas_start(mas);
4161 }
4162 
4163 /**
4164  * mas_prealloc_calc() - Calculate number of nodes needed for a
4165  * given store oepration
4166  * @wr_mas: The maple write state
4167  * @entry: The entry to store into the tree
4168  *
4169  * Return: Number of nodes required for preallocation.
4170  */
4171 static inline int mas_prealloc_calc(struct ma_wr_state *wr_mas, void *entry)
4172 {
4173 	struct ma_state *mas = wr_mas->mas;
4174 	unsigned char height = mas_mt_height(mas);
4175 	int ret = height * 3 + 1;
4176 	unsigned char delta = height - wr_mas->vacant_height;
4177 
4178 	switch (mas->store_type) {
4179 	case wr_exact_fit:
4180 	case wr_append:
4181 	case wr_slot_store:
4182 		ret = 0;
4183 		break;
4184 	case wr_spanning_store:
4185 		if (wr_mas->sufficient_height < wr_mas->vacant_height)
4186 			ret = (height - wr_mas->sufficient_height) * 3 + 1;
4187 		else
4188 			ret = delta * 3 + 1;
4189 		break;
4190 	case wr_split_store:
4191 		ret = delta * 2 + 1;
4192 		break;
4193 	case wr_rebalance:
4194 		if (wr_mas->sufficient_height < wr_mas->vacant_height)
4195 			ret = (height - wr_mas->sufficient_height) * 2 + 1;
4196 		else
4197 			ret = delta * 2 + 1;
4198 		break;
4199 	case wr_node_store:
4200 		ret = mt_in_rcu(mas->tree) ? 1 : 0;
4201 		break;
4202 	case wr_new_root:
4203 		ret = 1;
4204 		break;
4205 	case wr_store_root:
4206 		if (likely((mas->last != 0) || (mas->index != 0)))
4207 			ret = 1;
4208 		else if (((unsigned long) (entry) & 3) == 2)
4209 			ret = 1;
4210 		else
4211 			ret = 0;
4212 		break;
4213 	case wr_invalid:
4214 		WARN_ON_ONCE(1);
4215 	}
4216 
4217 	return ret;
4218 }
4219 
4220 /*
4221  * mas_wr_store_type() - Determine the store type for a given
4222  * store operation.
4223  * @wr_mas: The maple write state
4224  *
4225  * Return: the type of store needed for the operation
4226  */
4227 static inline enum store_type mas_wr_store_type(struct ma_wr_state *wr_mas)
4228 {
4229 	struct ma_state *mas = wr_mas->mas;
4230 	unsigned char new_end;
4231 
4232 	if (unlikely(mas_is_none(mas) || mas_is_ptr(mas)))
4233 		return wr_store_root;
4234 
4235 	if (unlikely(!mas_wr_walk(wr_mas)))
4236 		return wr_spanning_store;
4237 
4238 	/* At this point, we are at the leaf node that needs to be altered. */
4239 	mas_wr_end_piv(wr_mas);
4240 	if (!wr_mas->entry)
4241 		mas_wr_extend_null(wr_mas);
4242 
4243 	if ((wr_mas->r_min == mas->index) && (wr_mas->r_max == mas->last))
4244 		return wr_exact_fit;
4245 
4246 	if (unlikely(!mas->index && mas->last == ULONG_MAX))
4247 		return wr_new_root;
4248 
4249 	new_end = mas_wr_new_end(wr_mas);
4250 	/* Potential spanning rebalance collapsing a node */
4251 	if (new_end < mt_min_slots[wr_mas->type]) {
4252 		if (!mte_is_root(mas->node) && !(mas->mas_flags & MA_STATE_BULK))
4253 			return  wr_rebalance;
4254 		return wr_node_store;
4255 	}
4256 
4257 	if (new_end >= mt_slots[wr_mas->type])
4258 		return wr_split_store;
4259 
4260 	if (!mt_in_rcu(mas->tree) && (mas->offset == mas->end))
4261 		return wr_append;
4262 
4263 	if ((new_end == mas->end) && (!mt_in_rcu(mas->tree) ||
4264 		(wr_mas->offset_end - mas->offset == 1)))
4265 		return wr_slot_store;
4266 
4267 	return wr_node_store;
4268 }
4269 
4270 /**
4271  * mas_wr_preallocate() - Preallocate enough nodes for a store operation
4272  * @wr_mas: The maple write state
4273  * @entry: The entry that will be stored
4274  *
4275  */
4276 static inline void mas_wr_preallocate(struct ma_wr_state *wr_mas, void *entry)
4277 {
4278 	int request;
4279 
4280 	mas_wr_prealloc_setup(wr_mas);
4281 	wr_mas->mas->store_type = mas_wr_store_type(wr_mas);
4282 	request = mas_prealloc_calc(wr_mas, entry);
4283 	if (!request)
4284 		return;
4285 
4286 	mas_node_count(wr_mas->mas, request);
4287 }
4288 
4289 /**
4290  * mas_insert() - Internal call to insert a value
4291  * @mas: The maple state
4292  * @entry: The entry to store
4293  *
4294  * Return: %NULL or the contents that already exists at the requested index
4295  * otherwise.  The maple state needs to be checked for error conditions.
4296  */
4297 static inline void *mas_insert(struct ma_state *mas, void *entry)
4298 {
4299 	MA_WR_STATE(wr_mas, mas, entry);
4300 
4301 	/*
4302 	 * Inserting a new range inserts either 0, 1, or 2 pivots within the
4303 	 * tree.  If the insert fits exactly into an existing gap with a value
4304 	 * of NULL, then the slot only needs to be written with the new value.
4305 	 * If the range being inserted is adjacent to another range, then only a
4306 	 * single pivot needs to be inserted (as well as writing the entry).  If
4307 	 * the new range is within a gap but does not touch any other ranges,
4308 	 * then two pivots need to be inserted: the start - 1, and the end.  As
4309 	 * usual, the entry must be written.  Most operations require a new node
4310 	 * to be allocated and replace an existing node to ensure RCU safety,
4311 	 * when in RCU mode.  The exception to requiring a newly allocated node
4312 	 * is when inserting at the end of a node (appending).  When done
4313 	 * carefully, appending can reuse the node in place.
4314 	 */
4315 	wr_mas.content = mas_start(mas);
4316 	if (wr_mas.content)
4317 		goto exists;
4318 
4319 	mas_wr_preallocate(&wr_mas, entry);
4320 	if (mas_is_err(mas))
4321 		return NULL;
4322 
4323 	/* spanning writes always overwrite something */
4324 	if (mas->store_type == wr_spanning_store)
4325 		goto exists;
4326 
4327 	/* At this point, we are at the leaf node that needs to be altered. */
4328 	if (mas->store_type != wr_new_root && mas->store_type != wr_store_root) {
4329 		wr_mas.offset_end = mas->offset;
4330 		wr_mas.end_piv = wr_mas.r_max;
4331 
4332 		if (wr_mas.content || (mas->last > wr_mas.r_max))
4333 			goto exists;
4334 	}
4335 
4336 	mas_wr_store_entry(&wr_mas);
4337 	return wr_mas.content;
4338 
4339 exists:
4340 	mas_set_err(mas, -EEXIST);
4341 	return wr_mas.content;
4342 
4343 }
4344 
4345 /**
4346  * mas_alloc_cyclic() - Internal call to find somewhere to store an entry
4347  * @mas: The maple state.
4348  * @startp: Pointer to ID.
4349  * @range_lo: Lower bound of range to search.
4350  * @range_hi: Upper bound of range to search.
4351  * @entry: The entry to store.
4352  * @next: Pointer to next ID to allocate.
4353  * @gfp: The GFP_FLAGS to use for allocations.
4354  *
4355  * Return: 0 if the allocation succeeded without wrapping, 1 if the
4356  * allocation succeeded after wrapping, or -EBUSY if there are no
4357  * free entries.
4358  */
4359 int mas_alloc_cyclic(struct ma_state *mas, unsigned long *startp,
4360 		void *entry, unsigned long range_lo, unsigned long range_hi,
4361 		unsigned long *next, gfp_t gfp)
4362 {
4363 	unsigned long min = range_lo;
4364 	int ret = 0;
4365 
4366 	range_lo = max(min, *next);
4367 	ret = mas_empty_area(mas, range_lo, range_hi, 1);
4368 	if ((mas->tree->ma_flags & MT_FLAGS_ALLOC_WRAPPED) && ret == 0) {
4369 		mas->tree->ma_flags &= ~MT_FLAGS_ALLOC_WRAPPED;
4370 		ret = 1;
4371 	}
4372 	if (ret < 0 && range_lo > min) {
4373 		mas_reset(mas);
4374 		ret = mas_empty_area(mas, min, range_hi, 1);
4375 		if (ret == 0)
4376 			ret = 1;
4377 	}
4378 	if (ret < 0)
4379 		return ret;
4380 
4381 	do {
4382 		mas_insert(mas, entry);
4383 	} while (mas_nomem(mas, gfp));
4384 	if (mas_is_err(mas))
4385 		return xa_err(mas->node);
4386 
4387 	*startp = mas->index;
4388 	*next = *startp + 1;
4389 	if (*next == 0)
4390 		mas->tree->ma_flags |= MT_FLAGS_ALLOC_WRAPPED;
4391 
4392 	mas_destroy(mas);
4393 	return ret;
4394 }
4395 EXPORT_SYMBOL(mas_alloc_cyclic);
4396 
4397 static __always_inline void mas_rewalk(struct ma_state *mas, unsigned long index)
4398 {
4399 retry:
4400 	mas_set(mas, index);
4401 	mas_state_walk(mas);
4402 	if (mas_is_start(mas))
4403 		goto retry;
4404 }
4405 
4406 static __always_inline bool mas_rewalk_if_dead(struct ma_state *mas,
4407 		struct maple_node *node, const unsigned long index)
4408 {
4409 	if (unlikely(ma_dead_node(node))) {
4410 		mas_rewalk(mas, index);
4411 		return true;
4412 	}
4413 	return false;
4414 }
4415 
4416 /*
4417  * mas_prev_node() - Find the prev non-null entry at the same level in the
4418  * tree.  The prev value will be mas->node[mas->offset] or the status will be
4419  * ma_none.
4420  * @mas: The maple state
4421  * @min: The lower limit to search
4422  *
4423  * The prev node value will be mas->node[mas->offset] or the status will be
4424  * ma_none.
4425  * Return: 1 if the node is dead, 0 otherwise.
4426  */
4427 static int mas_prev_node(struct ma_state *mas, unsigned long min)
4428 {
4429 	enum maple_type mt;
4430 	int offset, level;
4431 	void __rcu **slots;
4432 	struct maple_node *node;
4433 	unsigned long *pivots;
4434 	unsigned long max;
4435 
4436 	node = mas_mn(mas);
4437 	if (!mas->min)
4438 		goto no_entry;
4439 
4440 	max = mas->min - 1;
4441 	if (max < min)
4442 		goto no_entry;
4443 
4444 	level = 0;
4445 	do {
4446 		if (ma_is_root(node))
4447 			goto no_entry;
4448 
4449 		/* Walk up. */
4450 		if (unlikely(mas_ascend(mas)))
4451 			return 1;
4452 		offset = mas->offset;
4453 		level++;
4454 		node = mas_mn(mas);
4455 	} while (!offset);
4456 
4457 	offset--;
4458 	mt = mte_node_type(mas->node);
4459 	while (level > 1) {
4460 		level--;
4461 		slots = ma_slots(node, mt);
4462 		mas->node = mas_slot(mas, slots, offset);
4463 		if (unlikely(ma_dead_node(node)))
4464 			return 1;
4465 
4466 		mt = mte_node_type(mas->node);
4467 		node = mas_mn(mas);
4468 		pivots = ma_pivots(node, mt);
4469 		offset = ma_data_end(node, mt, pivots, max);
4470 		if (unlikely(ma_dead_node(node)))
4471 			return 1;
4472 	}
4473 
4474 	slots = ma_slots(node, mt);
4475 	mas->node = mas_slot(mas, slots, offset);
4476 	pivots = ma_pivots(node, mt);
4477 	if (unlikely(ma_dead_node(node)))
4478 		return 1;
4479 
4480 	if (likely(offset))
4481 		mas->min = pivots[offset - 1] + 1;
4482 	mas->max = max;
4483 	mas->offset = mas_data_end(mas);
4484 	if (unlikely(mte_dead_node(mas->node)))
4485 		return 1;
4486 
4487 	mas->end = mas->offset;
4488 	return 0;
4489 
4490 no_entry:
4491 	if (unlikely(ma_dead_node(node)))
4492 		return 1;
4493 
4494 	mas->status = ma_underflow;
4495 	return 0;
4496 }
4497 
4498 /*
4499  * mas_prev_slot() - Get the entry in the previous slot
4500  *
4501  * @mas: The maple state
4502  * @min: The minimum starting range
4503  * @empty: Can be empty
4504  *
4505  * Return: The entry in the previous slot which is possibly NULL
4506  */
4507 static void *mas_prev_slot(struct ma_state *mas, unsigned long min, bool empty)
4508 {
4509 	void *entry;
4510 	void __rcu **slots;
4511 	unsigned long pivot;
4512 	enum maple_type type;
4513 	unsigned long *pivots;
4514 	struct maple_node *node;
4515 	unsigned long save_point = mas->index;
4516 
4517 retry:
4518 	node = mas_mn(mas);
4519 	type = mte_node_type(mas->node);
4520 	pivots = ma_pivots(node, type);
4521 	if (unlikely(mas_rewalk_if_dead(mas, node, save_point)))
4522 		goto retry;
4523 
4524 	if (mas->min <= min) {
4525 		pivot = mas_safe_min(mas, pivots, mas->offset);
4526 
4527 		if (unlikely(mas_rewalk_if_dead(mas, node, save_point)))
4528 			goto retry;
4529 
4530 		if (pivot <= min)
4531 			goto underflow;
4532 	}
4533 
4534 again:
4535 	if (likely(mas->offset)) {
4536 		mas->offset--;
4537 		mas->last = mas->index - 1;
4538 		mas->index = mas_safe_min(mas, pivots, mas->offset);
4539 	} else  {
4540 		if (mas->index <= min)
4541 			goto underflow;
4542 
4543 		if (mas_prev_node(mas, min)) {
4544 			mas_rewalk(mas, save_point);
4545 			goto retry;
4546 		}
4547 
4548 		if (WARN_ON_ONCE(mas_is_underflow(mas)))
4549 			return NULL;
4550 
4551 		mas->last = mas->max;
4552 		node = mas_mn(mas);
4553 		type = mte_node_type(mas->node);
4554 		pivots = ma_pivots(node, type);
4555 		mas->index = pivots[mas->offset - 1] + 1;
4556 	}
4557 
4558 	slots = ma_slots(node, type);
4559 	entry = mas_slot(mas, slots, mas->offset);
4560 	if (unlikely(mas_rewalk_if_dead(mas, node, save_point)))
4561 		goto retry;
4562 
4563 	if (likely(entry))
4564 		return entry;
4565 
4566 	if (!empty) {
4567 		if (mas->index <= min)
4568 			goto underflow;
4569 
4570 		goto again;
4571 	}
4572 
4573 	return entry;
4574 
4575 underflow:
4576 	mas->status = ma_underflow;
4577 	return NULL;
4578 }
4579 
4580 /*
4581  * mas_next_node() - Get the next node at the same level in the tree.
4582  * @mas: The maple state
4583  * @node: The maple node
4584  * @max: The maximum pivot value to check.
4585  *
4586  * The next value will be mas->node[mas->offset] or the status will have
4587  * overflowed.
4588  * Return: 1 on dead node, 0 otherwise.
4589  */
4590 static int mas_next_node(struct ma_state *mas, struct maple_node *node,
4591 		unsigned long max)
4592 {
4593 	unsigned long min;
4594 	unsigned long *pivots;
4595 	struct maple_enode *enode;
4596 	struct maple_node *tmp;
4597 	int level = 0;
4598 	unsigned char node_end;
4599 	enum maple_type mt;
4600 	void __rcu **slots;
4601 
4602 	if (mas->max >= max)
4603 		goto overflow;
4604 
4605 	min = mas->max + 1;
4606 	level = 0;
4607 	do {
4608 		if (ma_is_root(node))
4609 			goto overflow;
4610 
4611 		/* Walk up. */
4612 		if (unlikely(mas_ascend(mas)))
4613 			return 1;
4614 
4615 		level++;
4616 		node = mas_mn(mas);
4617 		mt = mte_node_type(mas->node);
4618 		pivots = ma_pivots(node, mt);
4619 		node_end = ma_data_end(node, mt, pivots, mas->max);
4620 		if (unlikely(ma_dead_node(node)))
4621 			return 1;
4622 
4623 	} while (unlikely(mas->offset == node_end));
4624 
4625 	slots = ma_slots(node, mt);
4626 	mas->offset++;
4627 	enode = mas_slot(mas, slots, mas->offset);
4628 	if (unlikely(ma_dead_node(node)))
4629 		return 1;
4630 
4631 	if (level > 1)
4632 		mas->offset = 0;
4633 
4634 	while (unlikely(level > 1)) {
4635 		level--;
4636 		mas->node = enode;
4637 		node = mas_mn(mas);
4638 		mt = mte_node_type(mas->node);
4639 		slots = ma_slots(node, mt);
4640 		enode = mas_slot(mas, slots, 0);
4641 		if (unlikely(ma_dead_node(node)))
4642 			return 1;
4643 	}
4644 
4645 	if (!mas->offset)
4646 		pivots = ma_pivots(node, mt);
4647 
4648 	mas->max = mas_safe_pivot(mas, pivots, mas->offset, mt);
4649 	tmp = mte_to_node(enode);
4650 	mt = mte_node_type(enode);
4651 	pivots = ma_pivots(tmp, mt);
4652 	mas->end = ma_data_end(tmp, mt, pivots, mas->max);
4653 	if (unlikely(ma_dead_node(node)))
4654 		return 1;
4655 
4656 	mas->node = enode;
4657 	mas->min = min;
4658 	return 0;
4659 
4660 overflow:
4661 	if (unlikely(ma_dead_node(node)))
4662 		return 1;
4663 
4664 	mas->status = ma_overflow;
4665 	return 0;
4666 }
4667 
4668 /*
4669  * mas_next_slot() - Get the entry in the next slot
4670  *
4671  * @mas: The maple state
4672  * @max: The maximum starting range
4673  * @empty: Can be empty
4674  *
4675  * Return: The entry in the next slot which is possibly NULL
4676  */
4677 static void *mas_next_slot(struct ma_state *mas, unsigned long max, bool empty)
4678 {
4679 	void __rcu **slots;
4680 	unsigned long *pivots;
4681 	unsigned long pivot;
4682 	enum maple_type type;
4683 	struct maple_node *node;
4684 	unsigned long save_point = mas->last;
4685 	void *entry;
4686 
4687 retry:
4688 	node = mas_mn(mas);
4689 	type = mte_node_type(mas->node);
4690 	pivots = ma_pivots(node, type);
4691 	if (unlikely(mas_rewalk_if_dead(mas, node, save_point)))
4692 		goto retry;
4693 
4694 	if (mas->max >= max) {
4695 		if (likely(mas->offset < mas->end))
4696 			pivot = pivots[mas->offset];
4697 		else
4698 			pivot = mas->max;
4699 
4700 		if (unlikely(mas_rewalk_if_dead(mas, node, save_point)))
4701 			goto retry;
4702 
4703 		if (pivot >= max) { /* Was at the limit, next will extend beyond */
4704 			mas->status = ma_overflow;
4705 			return NULL;
4706 		}
4707 	}
4708 
4709 	if (likely(mas->offset < mas->end)) {
4710 		mas->index = pivots[mas->offset] + 1;
4711 again:
4712 		mas->offset++;
4713 		if (likely(mas->offset < mas->end))
4714 			mas->last = pivots[mas->offset];
4715 		else
4716 			mas->last = mas->max;
4717 	} else  {
4718 		if (mas->last >= max) {
4719 			mas->status = ma_overflow;
4720 			return NULL;
4721 		}
4722 
4723 		if (mas_next_node(mas, node, max)) {
4724 			mas_rewalk(mas, save_point);
4725 			goto retry;
4726 		}
4727 
4728 		if (WARN_ON_ONCE(mas_is_overflow(mas)))
4729 			return NULL;
4730 
4731 		mas->offset = 0;
4732 		mas->index = mas->min;
4733 		node = mas_mn(mas);
4734 		type = mte_node_type(mas->node);
4735 		pivots = ma_pivots(node, type);
4736 		mas->last = pivots[0];
4737 	}
4738 
4739 	slots = ma_slots(node, type);
4740 	entry = mt_slot(mas->tree, slots, mas->offset);
4741 	if (unlikely(mas_rewalk_if_dead(mas, node, save_point)))
4742 		goto retry;
4743 
4744 	if (entry)
4745 		return entry;
4746 
4747 
4748 	if (!empty) {
4749 		if (mas->last >= max) {
4750 			mas->status = ma_overflow;
4751 			return NULL;
4752 		}
4753 
4754 		mas->index = mas->last + 1;
4755 		goto again;
4756 	}
4757 
4758 	return entry;
4759 }
4760 
4761 /*
4762  * mas_rev_awalk() - Internal function.  Reverse allocation walk.  Find the
4763  * highest gap address of a given size in a given node and descend.
4764  * @mas: The maple state
4765  * @size: The needed size.
4766  *
4767  * Return: True if found in a leaf, false otherwise.
4768  *
4769  */
4770 static bool mas_rev_awalk(struct ma_state *mas, unsigned long size,
4771 		unsigned long *gap_min, unsigned long *gap_max)
4772 {
4773 	enum maple_type type = mte_node_type(mas->node);
4774 	struct maple_node *node = mas_mn(mas);
4775 	unsigned long *pivots, *gaps;
4776 	void __rcu **slots;
4777 	unsigned long gap = 0;
4778 	unsigned long max, min;
4779 	unsigned char offset;
4780 
4781 	if (unlikely(mas_is_err(mas)))
4782 		return true;
4783 
4784 	if (ma_is_dense(type)) {
4785 		/* dense nodes. */
4786 		mas->offset = (unsigned char)(mas->index - mas->min);
4787 		return true;
4788 	}
4789 
4790 	pivots = ma_pivots(node, type);
4791 	slots = ma_slots(node, type);
4792 	gaps = ma_gaps(node, type);
4793 	offset = mas->offset;
4794 	min = mas_safe_min(mas, pivots, offset);
4795 	/* Skip out of bounds. */
4796 	while (mas->last < min)
4797 		min = mas_safe_min(mas, pivots, --offset);
4798 
4799 	max = mas_safe_pivot(mas, pivots, offset, type);
4800 	while (mas->index <= max) {
4801 		gap = 0;
4802 		if (gaps)
4803 			gap = gaps[offset];
4804 		else if (!mas_slot(mas, slots, offset))
4805 			gap = max - min + 1;
4806 
4807 		if (gap) {
4808 			if ((size <= gap) && (size <= mas->last - min + 1))
4809 				break;
4810 
4811 			if (!gaps) {
4812 				/* Skip the next slot, it cannot be a gap. */
4813 				if (offset < 2)
4814 					goto ascend;
4815 
4816 				offset -= 2;
4817 				max = pivots[offset];
4818 				min = mas_safe_min(mas, pivots, offset);
4819 				continue;
4820 			}
4821 		}
4822 
4823 		if (!offset)
4824 			goto ascend;
4825 
4826 		offset--;
4827 		max = min - 1;
4828 		min = mas_safe_min(mas, pivots, offset);
4829 	}
4830 
4831 	if (unlikely((mas->index > max) || (size - 1 > max - mas->index)))
4832 		goto no_space;
4833 
4834 	if (unlikely(ma_is_leaf(type))) {
4835 		mas->offset = offset;
4836 		*gap_min = min;
4837 		*gap_max = min + gap - 1;
4838 		return true;
4839 	}
4840 
4841 	/* descend, only happens under lock. */
4842 	mas->node = mas_slot(mas, slots, offset);
4843 	mas->min = min;
4844 	mas->max = max;
4845 	mas->offset = mas_data_end(mas);
4846 	return false;
4847 
4848 ascend:
4849 	if (!mte_is_root(mas->node))
4850 		return false;
4851 
4852 no_space:
4853 	mas_set_err(mas, -EBUSY);
4854 	return false;
4855 }
4856 
4857 static inline bool mas_anode_descend(struct ma_state *mas, unsigned long size)
4858 {
4859 	enum maple_type type = mte_node_type(mas->node);
4860 	unsigned long pivot, min, gap = 0;
4861 	unsigned char offset, data_end;
4862 	unsigned long *gaps, *pivots;
4863 	void __rcu **slots;
4864 	struct maple_node *node;
4865 	bool found = false;
4866 
4867 	if (ma_is_dense(type)) {
4868 		mas->offset = (unsigned char)(mas->index - mas->min);
4869 		return true;
4870 	}
4871 
4872 	node = mas_mn(mas);
4873 	pivots = ma_pivots(node, type);
4874 	slots = ma_slots(node, type);
4875 	gaps = ma_gaps(node, type);
4876 	offset = mas->offset;
4877 	min = mas_safe_min(mas, pivots, offset);
4878 	data_end = ma_data_end(node, type, pivots, mas->max);
4879 	for (; offset <= data_end; offset++) {
4880 		pivot = mas_safe_pivot(mas, pivots, offset, type);
4881 
4882 		/* Not within lower bounds */
4883 		if (mas->index > pivot)
4884 			goto next_slot;
4885 
4886 		if (gaps)
4887 			gap = gaps[offset];
4888 		else if (!mas_slot(mas, slots, offset))
4889 			gap = min(pivot, mas->last) - max(mas->index, min) + 1;
4890 		else
4891 			goto next_slot;
4892 
4893 		if (gap >= size) {
4894 			if (ma_is_leaf(type)) {
4895 				found = true;
4896 				break;
4897 			}
4898 
4899 			mas->node = mas_slot(mas, slots, offset);
4900 			mas->min = min;
4901 			mas->max = pivot;
4902 			offset = 0;
4903 			break;
4904 		}
4905 next_slot:
4906 		min = pivot + 1;
4907 		if (mas->last <= pivot) {
4908 			mas_set_err(mas, -EBUSY);
4909 			return true;
4910 		}
4911 	}
4912 
4913 	mas->offset = offset;
4914 	return found;
4915 }
4916 
4917 /**
4918  * mas_walk() - Search for @mas->index in the tree.
4919  * @mas: The maple state.
4920  *
4921  * mas->index and mas->last will be set to the range if there is a value.  If
4922  * mas->status is ma_none, reset to ma_start
4923  *
4924  * Return: the entry at the location or %NULL.
4925  */
4926 void *mas_walk(struct ma_state *mas)
4927 {
4928 	void *entry;
4929 
4930 	if (!mas_is_active(mas) && !mas_is_start(mas))
4931 		mas->status = ma_start;
4932 retry:
4933 	entry = mas_state_walk(mas);
4934 	if (mas_is_start(mas)) {
4935 		goto retry;
4936 	} else if (mas_is_none(mas)) {
4937 		mas->index = 0;
4938 		mas->last = ULONG_MAX;
4939 	} else if (mas_is_ptr(mas)) {
4940 		if (!mas->index) {
4941 			mas->last = 0;
4942 			return entry;
4943 		}
4944 
4945 		mas->index = 1;
4946 		mas->last = ULONG_MAX;
4947 		mas->status = ma_none;
4948 		return NULL;
4949 	}
4950 
4951 	return entry;
4952 }
4953 EXPORT_SYMBOL_GPL(mas_walk);
4954 
4955 static inline bool mas_rewind_node(struct ma_state *mas)
4956 {
4957 	unsigned char slot;
4958 
4959 	do {
4960 		if (mte_is_root(mas->node)) {
4961 			slot = mas->offset;
4962 			if (!slot)
4963 				return false;
4964 		} else {
4965 			mas_ascend(mas);
4966 			slot = mas->offset;
4967 		}
4968 	} while (!slot);
4969 
4970 	mas->offset = --slot;
4971 	return true;
4972 }
4973 
4974 /*
4975  * mas_skip_node() - Internal function.  Skip over a node.
4976  * @mas: The maple state.
4977  *
4978  * Return: true if there is another node, false otherwise.
4979  */
4980 static inline bool mas_skip_node(struct ma_state *mas)
4981 {
4982 	if (mas_is_err(mas))
4983 		return false;
4984 
4985 	do {
4986 		if (mte_is_root(mas->node)) {
4987 			if (mas->offset >= mas_data_end(mas)) {
4988 				mas_set_err(mas, -EBUSY);
4989 				return false;
4990 			}
4991 		} else {
4992 			mas_ascend(mas);
4993 		}
4994 	} while (mas->offset >= mas_data_end(mas));
4995 
4996 	mas->offset++;
4997 	return true;
4998 }
4999 
5000 /*
5001  * mas_awalk() - Allocation walk.  Search from low address to high, for a gap of
5002  * @size
5003  * @mas: The maple state
5004  * @size: The size of the gap required
5005  *
5006  * Search between @mas->index and @mas->last for a gap of @size.
5007  */
5008 static inline void mas_awalk(struct ma_state *mas, unsigned long size)
5009 {
5010 	struct maple_enode *last = NULL;
5011 
5012 	/*
5013 	 * There are 4 options:
5014 	 * go to child (descend)
5015 	 * go back to parent (ascend)
5016 	 * no gap found. (return, error == -EBUSY)
5017 	 * found the gap. (return)
5018 	 */
5019 	while (!mas_is_err(mas) && !mas_anode_descend(mas, size)) {
5020 		if (last == mas->node)
5021 			mas_skip_node(mas);
5022 		else
5023 			last = mas->node;
5024 	}
5025 }
5026 
5027 /*
5028  * mas_sparse_area() - Internal function.  Return upper or lower limit when
5029  * searching for a gap in an empty tree.
5030  * @mas: The maple state
5031  * @min: the minimum range
5032  * @max: The maximum range
5033  * @size: The size of the gap
5034  * @fwd: Searching forward or back
5035  */
5036 static inline int mas_sparse_area(struct ma_state *mas, unsigned long min,
5037 				unsigned long max, unsigned long size, bool fwd)
5038 {
5039 	if (!unlikely(mas_is_none(mas)) && min == 0) {
5040 		min++;
5041 		/*
5042 		 * At this time, min is increased, we need to recheck whether
5043 		 * the size is satisfied.
5044 		 */
5045 		if (min > max || max - min + 1 < size)
5046 			return -EBUSY;
5047 	}
5048 	/* mas_is_ptr */
5049 
5050 	if (fwd) {
5051 		mas->index = min;
5052 		mas->last = min + size - 1;
5053 	} else {
5054 		mas->last = max;
5055 		mas->index = max - size + 1;
5056 	}
5057 	return 0;
5058 }
5059 
5060 /*
5061  * mas_empty_area() - Get the lowest address within the range that is
5062  * sufficient for the size requested.
5063  * @mas: The maple state
5064  * @min: The lowest value of the range
5065  * @max: The highest value of the range
5066  * @size: The size needed
5067  */
5068 int mas_empty_area(struct ma_state *mas, unsigned long min,
5069 		unsigned long max, unsigned long size)
5070 {
5071 	unsigned char offset;
5072 	unsigned long *pivots;
5073 	enum maple_type mt;
5074 	struct maple_node *node;
5075 
5076 	if (min > max)
5077 		return -EINVAL;
5078 
5079 	if (size == 0 || max - min < size - 1)
5080 		return -EINVAL;
5081 
5082 	if (mas_is_start(mas))
5083 		mas_start(mas);
5084 	else if (mas->offset >= 2)
5085 		mas->offset -= 2;
5086 	else if (!mas_skip_node(mas))
5087 		return -EBUSY;
5088 
5089 	/* Empty set */
5090 	if (mas_is_none(mas) || mas_is_ptr(mas))
5091 		return mas_sparse_area(mas, min, max, size, true);
5092 
5093 	/* The start of the window can only be within these values */
5094 	mas->index = min;
5095 	mas->last = max;
5096 	mas_awalk(mas, size);
5097 
5098 	if (unlikely(mas_is_err(mas)))
5099 		return xa_err(mas->node);
5100 
5101 	offset = mas->offset;
5102 	node = mas_mn(mas);
5103 	mt = mte_node_type(mas->node);
5104 	pivots = ma_pivots(node, mt);
5105 	min = mas_safe_min(mas, pivots, offset);
5106 	if (mas->index < min)
5107 		mas->index = min;
5108 	mas->last = mas->index + size - 1;
5109 	mas->end = ma_data_end(node, mt, pivots, mas->max);
5110 	return 0;
5111 }
5112 EXPORT_SYMBOL_GPL(mas_empty_area);
5113 
5114 /*
5115  * mas_empty_area_rev() - Get the highest address within the range that is
5116  * sufficient for the size requested.
5117  * @mas: The maple state
5118  * @min: The lowest value of the range
5119  * @max: The highest value of the range
5120  * @size: The size needed
5121  */
5122 int mas_empty_area_rev(struct ma_state *mas, unsigned long min,
5123 		unsigned long max, unsigned long size)
5124 {
5125 	struct maple_enode *last = mas->node;
5126 
5127 	if (min > max)
5128 		return -EINVAL;
5129 
5130 	if (size == 0 || max - min < size - 1)
5131 		return -EINVAL;
5132 
5133 	if (mas_is_start(mas))
5134 		mas_start(mas);
5135 	else if ((mas->offset < 2) && (!mas_rewind_node(mas)))
5136 		return -EBUSY;
5137 
5138 	if (unlikely(mas_is_none(mas) || mas_is_ptr(mas)))
5139 		return mas_sparse_area(mas, min, max, size, false);
5140 	else if (mas->offset >= 2)
5141 		mas->offset -= 2;
5142 	else
5143 		mas->offset = mas_data_end(mas);
5144 
5145 
5146 	/* The start of the window can only be within these values. */
5147 	mas->index = min;
5148 	mas->last = max;
5149 
5150 	while (!mas_rev_awalk(mas, size, &min, &max)) {
5151 		if (last == mas->node) {
5152 			if (!mas_rewind_node(mas))
5153 				return -EBUSY;
5154 		} else {
5155 			last = mas->node;
5156 		}
5157 	}
5158 
5159 	if (mas_is_err(mas))
5160 		return xa_err(mas->node);
5161 
5162 	if (unlikely(mas->offset == MAPLE_NODE_SLOTS))
5163 		return -EBUSY;
5164 
5165 	/* Trim the upper limit to the max. */
5166 	if (max < mas->last)
5167 		mas->last = max;
5168 
5169 	mas->index = mas->last - size + 1;
5170 	mas->end = mas_data_end(mas);
5171 	return 0;
5172 }
5173 EXPORT_SYMBOL_GPL(mas_empty_area_rev);
5174 
5175 /*
5176  * mte_dead_leaves() - Mark all leaves of a node as dead.
5177  * @enode: the encoded node
5178  * @mt: the maple tree
5179  * @slots: Pointer to the slot array
5180  *
5181  * Must hold the write lock.
5182  *
5183  * Return: The number of leaves marked as dead.
5184  */
5185 static inline
5186 unsigned char mte_dead_leaves(struct maple_enode *enode, struct maple_tree *mt,
5187 			      void __rcu **slots)
5188 {
5189 	struct maple_node *node;
5190 	enum maple_type type;
5191 	void *entry;
5192 	int offset;
5193 
5194 	for (offset = 0; offset < mt_slot_count(enode); offset++) {
5195 		entry = mt_slot(mt, slots, offset);
5196 		type = mte_node_type(entry);
5197 		node = mte_to_node(entry);
5198 		/* Use both node and type to catch LE & BE metadata */
5199 		if (!node || !type)
5200 			break;
5201 
5202 		mte_set_node_dead(entry);
5203 		node->type = type;
5204 		rcu_assign_pointer(slots[offset], node);
5205 	}
5206 
5207 	return offset;
5208 }
5209 
5210 /**
5211  * mte_dead_walk() - Walk down a dead tree to just before the leaves
5212  * @enode: The maple encoded node
5213  * @offset: The starting offset
5214  *
5215  * Note: This can only be used from the RCU callback context.
5216  */
5217 static void __rcu **mte_dead_walk(struct maple_enode **enode, unsigned char offset)
5218 {
5219 	struct maple_node *node, *next;
5220 	void __rcu **slots = NULL;
5221 
5222 	next = mte_to_node(*enode);
5223 	do {
5224 		*enode = ma_enode_ptr(next);
5225 		node = mte_to_node(*enode);
5226 		slots = ma_slots(node, node->type);
5227 		next = rcu_dereference_protected(slots[offset],
5228 					lock_is_held(&rcu_callback_map));
5229 		offset = 0;
5230 	} while (!ma_is_leaf(next->type));
5231 
5232 	return slots;
5233 }
5234 
5235 /**
5236  * mt_free_walk() - Walk & free a tree in the RCU callback context
5237  * @head: The RCU head that's within the node.
5238  *
5239  * Note: This can only be used from the RCU callback context.
5240  */
5241 static void mt_free_walk(struct rcu_head *head)
5242 {
5243 	void __rcu **slots;
5244 	struct maple_node *node, *start;
5245 	struct maple_enode *enode;
5246 	unsigned char offset;
5247 	enum maple_type type;
5248 
5249 	node = container_of(head, struct maple_node, rcu);
5250 
5251 	if (ma_is_leaf(node->type))
5252 		goto free_leaf;
5253 
5254 	start = node;
5255 	enode = mt_mk_node(node, node->type);
5256 	slots = mte_dead_walk(&enode, 0);
5257 	node = mte_to_node(enode);
5258 	do {
5259 		mt_free_bulk(node->slot_len, slots);
5260 		offset = node->parent_slot + 1;
5261 		enode = node->piv_parent;
5262 		if (mte_to_node(enode) == node)
5263 			goto free_leaf;
5264 
5265 		type = mte_node_type(enode);
5266 		slots = ma_slots(mte_to_node(enode), type);
5267 		if ((offset < mt_slots[type]) &&
5268 		    rcu_dereference_protected(slots[offset],
5269 					      lock_is_held(&rcu_callback_map)))
5270 			slots = mte_dead_walk(&enode, offset);
5271 		node = mte_to_node(enode);
5272 	} while ((node != start) || (node->slot_len < offset));
5273 
5274 	slots = ma_slots(node, node->type);
5275 	mt_free_bulk(node->slot_len, slots);
5276 
5277 free_leaf:
5278 	mt_free_rcu(&node->rcu);
5279 }
5280 
5281 static inline void __rcu **mte_destroy_descend(struct maple_enode **enode,
5282 	struct maple_tree *mt, struct maple_enode *prev, unsigned char offset)
5283 {
5284 	struct maple_node *node;
5285 	struct maple_enode *next = *enode;
5286 	void __rcu **slots = NULL;
5287 	enum maple_type type;
5288 	unsigned char next_offset = 0;
5289 
5290 	do {
5291 		*enode = next;
5292 		node = mte_to_node(*enode);
5293 		type = mte_node_type(*enode);
5294 		slots = ma_slots(node, type);
5295 		next = mt_slot_locked(mt, slots, next_offset);
5296 		if ((mte_dead_node(next)))
5297 			next = mt_slot_locked(mt, slots, ++next_offset);
5298 
5299 		mte_set_node_dead(*enode);
5300 		node->type = type;
5301 		node->piv_parent = prev;
5302 		node->parent_slot = offset;
5303 		offset = next_offset;
5304 		next_offset = 0;
5305 		prev = *enode;
5306 	} while (!mte_is_leaf(next));
5307 
5308 	return slots;
5309 }
5310 
5311 static void mt_destroy_walk(struct maple_enode *enode, struct maple_tree *mt,
5312 			    bool free)
5313 {
5314 	void __rcu **slots;
5315 	struct maple_node *node = mte_to_node(enode);
5316 	struct maple_enode *start;
5317 
5318 	if (mte_is_leaf(enode)) {
5319 		mte_set_node_dead(enode);
5320 		node->type = mte_node_type(enode);
5321 		goto free_leaf;
5322 	}
5323 
5324 	start = enode;
5325 	slots = mte_destroy_descend(&enode, mt, start, 0);
5326 	node = mte_to_node(enode); // Updated in the above call.
5327 	do {
5328 		enum maple_type type;
5329 		unsigned char offset;
5330 		struct maple_enode *parent, *tmp;
5331 
5332 		node->slot_len = mte_dead_leaves(enode, mt, slots);
5333 		if (free)
5334 			mt_free_bulk(node->slot_len, slots);
5335 		offset = node->parent_slot + 1;
5336 		enode = node->piv_parent;
5337 		if (mte_to_node(enode) == node)
5338 			goto free_leaf;
5339 
5340 		type = mte_node_type(enode);
5341 		slots = ma_slots(mte_to_node(enode), type);
5342 		if (offset >= mt_slots[type])
5343 			goto next;
5344 
5345 		tmp = mt_slot_locked(mt, slots, offset);
5346 		if (mte_node_type(tmp) && mte_to_node(tmp)) {
5347 			parent = enode;
5348 			enode = tmp;
5349 			slots = mte_destroy_descend(&enode, mt, parent, offset);
5350 		}
5351 next:
5352 		node = mte_to_node(enode);
5353 	} while (start != enode);
5354 
5355 	node = mte_to_node(enode);
5356 	node->slot_len = mte_dead_leaves(enode, mt, slots);
5357 	if (free)
5358 		mt_free_bulk(node->slot_len, slots);
5359 
5360 free_leaf:
5361 	if (free)
5362 		mt_free_rcu(&node->rcu);
5363 	else
5364 		mt_clear_meta(mt, node, node->type);
5365 }
5366 
5367 /*
5368  * mte_destroy_walk() - Free a tree or sub-tree.
5369  * @enode: the encoded maple node (maple_enode) to start
5370  * @mt: the tree to free - needed for node types.
5371  *
5372  * Must hold the write lock.
5373  */
5374 static inline void mte_destroy_walk(struct maple_enode *enode,
5375 				    struct maple_tree *mt)
5376 {
5377 	struct maple_node *node = mte_to_node(enode);
5378 
5379 	if (mt_in_rcu(mt)) {
5380 		mt_destroy_walk(enode, mt, false);
5381 		call_rcu(&node->rcu, mt_free_walk);
5382 	} else {
5383 		mt_destroy_walk(enode, mt, true);
5384 	}
5385 }
5386 /* Interface */
5387 
5388 /**
5389  * mas_store() - Store an @entry.
5390  * @mas: The maple state.
5391  * @entry: The entry to store.
5392  *
5393  * The @mas->index and @mas->last is used to set the range for the @entry.
5394  *
5395  * Return: the first entry between mas->index and mas->last or %NULL.
5396  */
5397 void *mas_store(struct ma_state *mas, void *entry)
5398 {
5399 	int request;
5400 	MA_WR_STATE(wr_mas, mas, entry);
5401 
5402 	trace_ma_write(__func__, mas, 0, entry);
5403 #ifdef CONFIG_DEBUG_MAPLE_TREE
5404 	if (MAS_WARN_ON(mas, mas->index > mas->last))
5405 		pr_err("Error %lX > %lX " PTR_FMT "\n", mas->index, mas->last,
5406 		       entry);
5407 
5408 	if (mas->index > mas->last) {
5409 		mas_set_err(mas, -EINVAL);
5410 		return NULL;
5411 	}
5412 
5413 #endif
5414 
5415 	/*
5416 	 * Storing is the same operation as insert with the added caveat that it
5417 	 * can overwrite entries.  Although this seems simple enough, one may
5418 	 * want to examine what happens if a single store operation was to
5419 	 * overwrite multiple entries within a self-balancing B-Tree.
5420 	 */
5421 	mas_wr_prealloc_setup(&wr_mas);
5422 	mas->store_type = mas_wr_store_type(&wr_mas);
5423 	if (mas->mas_flags & MA_STATE_PREALLOC) {
5424 		mas_wr_store_entry(&wr_mas);
5425 		MAS_WR_BUG_ON(&wr_mas, mas_is_err(mas));
5426 		return wr_mas.content;
5427 	}
5428 
5429 	request = mas_prealloc_calc(&wr_mas, entry);
5430 	if (!request)
5431 		goto store;
5432 
5433 	mas_node_count(mas, request);
5434 	if (mas_is_err(mas))
5435 		return NULL;
5436 
5437 store:
5438 	mas_wr_store_entry(&wr_mas);
5439 	mas_destroy(mas);
5440 	return wr_mas.content;
5441 }
5442 EXPORT_SYMBOL_GPL(mas_store);
5443 
5444 /**
5445  * mas_store_gfp() - Store a value into the tree.
5446  * @mas: The maple state
5447  * @entry: The entry to store
5448  * @gfp: The GFP_FLAGS to use for allocations if necessary.
5449  *
5450  * Return: 0 on success, -EINVAL on invalid request, -ENOMEM if memory could not
5451  * be allocated.
5452  */
5453 int mas_store_gfp(struct ma_state *mas, void *entry, gfp_t gfp)
5454 {
5455 	unsigned long index = mas->index;
5456 	unsigned long last = mas->last;
5457 	MA_WR_STATE(wr_mas, mas, entry);
5458 	int ret = 0;
5459 
5460 retry:
5461 	mas_wr_preallocate(&wr_mas, entry);
5462 	if (unlikely(mas_nomem(mas, gfp))) {
5463 		if (!entry)
5464 			__mas_set_range(mas, index, last);
5465 		goto retry;
5466 	}
5467 
5468 	if (mas_is_err(mas)) {
5469 		ret = xa_err(mas->node);
5470 		goto out;
5471 	}
5472 
5473 	mas_wr_store_entry(&wr_mas);
5474 out:
5475 	mas_destroy(mas);
5476 	return ret;
5477 }
5478 EXPORT_SYMBOL_GPL(mas_store_gfp);
5479 
5480 /**
5481  * mas_store_prealloc() - Store a value into the tree using memory
5482  * preallocated in the maple state.
5483  * @mas: The maple state
5484  * @entry: The entry to store.
5485  */
5486 void mas_store_prealloc(struct ma_state *mas, void *entry)
5487 {
5488 	MA_WR_STATE(wr_mas, mas, entry);
5489 
5490 	if (mas->store_type == wr_store_root) {
5491 		mas_wr_prealloc_setup(&wr_mas);
5492 		goto store;
5493 	}
5494 
5495 	mas_wr_walk_descend(&wr_mas);
5496 	if (mas->store_type != wr_spanning_store) {
5497 		/* set wr_mas->content to current slot */
5498 		wr_mas.content = mas_slot_locked(mas, wr_mas.slots, mas->offset);
5499 		mas_wr_end_piv(&wr_mas);
5500 	}
5501 
5502 store:
5503 	trace_ma_write(__func__, mas, 0, entry);
5504 	mas_wr_store_entry(&wr_mas);
5505 	MAS_WR_BUG_ON(&wr_mas, mas_is_err(mas));
5506 	mas_destroy(mas);
5507 }
5508 EXPORT_SYMBOL_GPL(mas_store_prealloc);
5509 
5510 /**
5511  * mas_preallocate() - Preallocate enough nodes for a store operation
5512  * @mas: The maple state
5513  * @entry: The entry that will be stored
5514  * @gfp: The GFP_FLAGS to use for allocations.
5515  *
5516  * Return: 0 on success, -ENOMEM if memory could not be allocated.
5517  */
5518 int mas_preallocate(struct ma_state *mas, void *entry, gfp_t gfp)
5519 {
5520 	MA_WR_STATE(wr_mas, mas, entry);
5521 	int ret = 0;
5522 	int request;
5523 
5524 	mas_wr_prealloc_setup(&wr_mas);
5525 	mas->store_type = mas_wr_store_type(&wr_mas);
5526 	request = mas_prealloc_calc(&wr_mas, entry);
5527 	if (!request)
5528 		goto set_flag;
5529 
5530 	mas->mas_flags &= ~MA_STATE_PREALLOC;
5531 	mas_node_count_gfp(mas, request, gfp);
5532 	if (mas_is_err(mas)) {
5533 		mas_set_alloc_req(mas, 0);
5534 		ret = xa_err(mas->node);
5535 		mas_destroy(mas);
5536 		mas_reset(mas);
5537 		return ret;
5538 	}
5539 
5540 set_flag:
5541 	mas->mas_flags |= MA_STATE_PREALLOC;
5542 	return ret;
5543 }
5544 EXPORT_SYMBOL_GPL(mas_preallocate);
5545 
5546 /*
5547  * mas_destroy() - destroy a maple state.
5548  * @mas: The maple state
5549  *
5550  * Upon completion, check the left-most node and rebalance against the node to
5551  * the right if necessary.  Frees any allocated nodes associated with this maple
5552  * state.
5553  */
5554 void mas_destroy(struct ma_state *mas)
5555 {
5556 	struct maple_alloc *node;
5557 	unsigned long total;
5558 
5559 	/*
5560 	 * When using mas_for_each() to insert an expected number of elements,
5561 	 * it is possible that the number inserted is less than the expected
5562 	 * number.  To fix an invalid final node, a check is performed here to
5563 	 * rebalance the previous node with the final node.
5564 	 */
5565 	if (mas->mas_flags & MA_STATE_REBALANCE) {
5566 		unsigned char end;
5567 		if (mas_is_err(mas))
5568 			mas_reset(mas);
5569 		mas_start(mas);
5570 		mtree_range_walk(mas);
5571 		end = mas->end + 1;
5572 		if (end < mt_min_slot_count(mas->node) - 1)
5573 			mas_destroy_rebalance(mas, end);
5574 
5575 		mas->mas_flags &= ~MA_STATE_REBALANCE;
5576 	}
5577 	mas->mas_flags &= ~(MA_STATE_BULK|MA_STATE_PREALLOC);
5578 
5579 	total = mas_allocated(mas);
5580 	while (total) {
5581 		node = mas->alloc;
5582 		mas->alloc = node->slot[0];
5583 		if (node->node_count > 1) {
5584 			size_t count = node->node_count - 1;
5585 
5586 			mt_free_bulk(count, (void __rcu **)&node->slot[1]);
5587 			total -= count;
5588 		}
5589 		mt_free_one(ma_mnode_ptr(node));
5590 		total--;
5591 	}
5592 
5593 	mas->alloc = NULL;
5594 }
5595 EXPORT_SYMBOL_GPL(mas_destroy);
5596 
5597 /*
5598  * mas_expected_entries() - Set the expected number of entries that will be inserted.
5599  * @mas: The maple state
5600  * @nr_entries: The number of expected entries.
5601  *
5602  * This will attempt to pre-allocate enough nodes to store the expected number
5603  * of entries.  The allocations will occur using the bulk allocator interface
5604  * for speed.  Please call mas_destroy() on the @mas after inserting the entries
5605  * to ensure any unused nodes are freed.
5606  *
5607  * Return: 0 on success, -ENOMEM if memory could not be allocated.
5608  */
5609 int mas_expected_entries(struct ma_state *mas, unsigned long nr_entries)
5610 {
5611 	int nonleaf_cap = MAPLE_ARANGE64_SLOTS - 2;
5612 	struct maple_enode *enode = mas->node;
5613 	int nr_nodes;
5614 	int ret;
5615 
5616 	/*
5617 	 * Sometimes it is necessary to duplicate a tree to a new tree, such as
5618 	 * forking a process and duplicating the VMAs from one tree to a new
5619 	 * tree.  When such a situation arises, it is known that the new tree is
5620 	 * not going to be used until the entire tree is populated.  For
5621 	 * performance reasons, it is best to use a bulk load with RCU disabled.
5622 	 * This allows for optimistic splitting that favours the left and reuse
5623 	 * of nodes during the operation.
5624 	 */
5625 
5626 	/* Optimize splitting for bulk insert in-order */
5627 	mas->mas_flags |= MA_STATE_BULK;
5628 
5629 	/*
5630 	 * Avoid overflow, assume a gap between each entry and a trailing null.
5631 	 * If this is wrong, it just means allocation can happen during
5632 	 * insertion of entries.
5633 	 */
5634 	nr_nodes = max(nr_entries, nr_entries * 2 + 1);
5635 	if (!mt_is_alloc(mas->tree))
5636 		nonleaf_cap = MAPLE_RANGE64_SLOTS - 2;
5637 
5638 	/* Leaves; reduce slots to keep space for expansion */
5639 	nr_nodes = DIV_ROUND_UP(nr_nodes, MAPLE_RANGE64_SLOTS - 2);
5640 	/* Internal nodes */
5641 	nr_nodes += DIV_ROUND_UP(nr_nodes, nonleaf_cap);
5642 	/* Add working room for split (2 nodes) + new parents */
5643 	mas_node_count_gfp(mas, nr_nodes + 3, GFP_KERNEL);
5644 
5645 	/* Detect if allocations run out */
5646 	mas->mas_flags |= MA_STATE_PREALLOC;
5647 
5648 	if (!mas_is_err(mas))
5649 		return 0;
5650 
5651 	ret = xa_err(mas->node);
5652 	mas->node = enode;
5653 	mas_destroy(mas);
5654 	return ret;
5655 
5656 }
5657 EXPORT_SYMBOL_GPL(mas_expected_entries);
5658 
5659 static void mas_may_activate(struct ma_state *mas)
5660 {
5661 	if (!mas->node) {
5662 		mas->status = ma_start;
5663 	} else if (mas->index > mas->max || mas->index < mas->min) {
5664 		mas->status = ma_start;
5665 	} else {
5666 		mas->status = ma_active;
5667 	}
5668 }
5669 
5670 static bool mas_next_setup(struct ma_state *mas, unsigned long max,
5671 		void **entry)
5672 {
5673 	bool was_none = mas_is_none(mas);
5674 
5675 	if (unlikely(mas->last >= max)) {
5676 		mas->status = ma_overflow;
5677 		return true;
5678 	}
5679 
5680 	switch (mas->status) {
5681 	case ma_active:
5682 		return false;
5683 	case ma_none:
5684 		fallthrough;
5685 	case ma_pause:
5686 		mas->status = ma_start;
5687 		fallthrough;
5688 	case ma_start:
5689 		mas_walk(mas); /* Retries on dead nodes handled by mas_walk */
5690 		break;
5691 	case ma_overflow:
5692 		/* Overflowed before, but the max changed */
5693 		mas_may_activate(mas);
5694 		break;
5695 	case ma_underflow:
5696 		/* The user expects the mas to be one before where it is */
5697 		mas_may_activate(mas);
5698 		*entry = mas_walk(mas);
5699 		if (*entry)
5700 			return true;
5701 		break;
5702 	case ma_root:
5703 		break;
5704 	case ma_error:
5705 		return true;
5706 	}
5707 
5708 	if (likely(mas_is_active(mas))) /* Fast path */
5709 		return false;
5710 
5711 	if (mas_is_ptr(mas)) {
5712 		*entry = NULL;
5713 		if (was_none && mas->index == 0) {
5714 			mas->index = mas->last = 0;
5715 			return true;
5716 		}
5717 		mas->index = 1;
5718 		mas->last = ULONG_MAX;
5719 		mas->status = ma_none;
5720 		return true;
5721 	}
5722 
5723 	if (mas_is_none(mas))
5724 		return true;
5725 
5726 	return false;
5727 }
5728 
5729 /**
5730  * mas_next() - Get the next entry.
5731  * @mas: The maple state
5732  * @max: The maximum index to check.
5733  *
5734  * Returns the next entry after @mas->index.
5735  * Must hold rcu_read_lock or the write lock.
5736  * Can return the zero entry.
5737  *
5738  * Return: The next entry or %NULL
5739  */
5740 void *mas_next(struct ma_state *mas, unsigned long max)
5741 {
5742 	void *entry = NULL;
5743 
5744 	if (mas_next_setup(mas, max, &entry))
5745 		return entry;
5746 
5747 	/* Retries on dead nodes handled by mas_next_slot */
5748 	return mas_next_slot(mas, max, false);
5749 }
5750 EXPORT_SYMBOL_GPL(mas_next);
5751 
5752 /**
5753  * mas_next_range() - Advance the maple state to the next range
5754  * @mas: The maple state
5755  * @max: The maximum index to check.
5756  *
5757  * Sets @mas->index and @mas->last to the range.
5758  * Must hold rcu_read_lock or the write lock.
5759  * Can return the zero entry.
5760  *
5761  * Return: The next entry or %NULL
5762  */
5763 void *mas_next_range(struct ma_state *mas, unsigned long max)
5764 {
5765 	void *entry = NULL;
5766 
5767 	if (mas_next_setup(mas, max, &entry))
5768 		return entry;
5769 
5770 	/* Retries on dead nodes handled by mas_next_slot */
5771 	return mas_next_slot(mas, max, true);
5772 }
5773 EXPORT_SYMBOL_GPL(mas_next_range);
5774 
5775 /**
5776  * mt_next() - get the next value in the maple tree
5777  * @mt: The maple tree
5778  * @index: The start index
5779  * @max: The maximum index to check
5780  *
5781  * Takes RCU read lock internally to protect the search, which does not
5782  * protect the returned pointer after dropping RCU read lock.
5783  * See also: Documentation/core-api/maple_tree.rst
5784  *
5785  * Return: The entry higher than @index or %NULL if nothing is found.
5786  */
5787 void *mt_next(struct maple_tree *mt, unsigned long index, unsigned long max)
5788 {
5789 	void *entry = NULL;
5790 	MA_STATE(mas, mt, index, index);
5791 
5792 	rcu_read_lock();
5793 	entry = mas_next(&mas, max);
5794 	rcu_read_unlock();
5795 	return entry;
5796 }
5797 EXPORT_SYMBOL_GPL(mt_next);
5798 
5799 static bool mas_prev_setup(struct ma_state *mas, unsigned long min, void **entry)
5800 {
5801 	if (unlikely(mas->index <= min)) {
5802 		mas->status = ma_underflow;
5803 		return true;
5804 	}
5805 
5806 	switch (mas->status) {
5807 	case ma_active:
5808 		return false;
5809 	case ma_start:
5810 		break;
5811 	case ma_none:
5812 		fallthrough;
5813 	case ma_pause:
5814 		mas->status = ma_start;
5815 		break;
5816 	case ma_underflow:
5817 		/* underflowed before but the min changed */
5818 		mas_may_activate(mas);
5819 		break;
5820 	case ma_overflow:
5821 		/* User expects mas to be one after where it is */
5822 		mas_may_activate(mas);
5823 		*entry = mas_walk(mas);
5824 		if (*entry)
5825 			return true;
5826 		break;
5827 	case ma_root:
5828 		break;
5829 	case ma_error:
5830 		return true;
5831 	}
5832 
5833 	if (mas_is_start(mas))
5834 		mas_walk(mas);
5835 
5836 	if (unlikely(mas_is_ptr(mas))) {
5837 		if (!mas->index) {
5838 			mas->status = ma_none;
5839 			return true;
5840 		}
5841 		mas->index = mas->last = 0;
5842 		*entry = mas_root(mas);
5843 		return true;
5844 	}
5845 
5846 	if (mas_is_none(mas)) {
5847 		if (mas->index) {
5848 			/* Walked to out-of-range pointer? */
5849 			mas->index = mas->last = 0;
5850 			mas->status = ma_root;
5851 			*entry = mas_root(mas);
5852 			return true;
5853 		}
5854 		return true;
5855 	}
5856 
5857 	return false;
5858 }
5859 
5860 /**
5861  * mas_prev() - Get the previous entry
5862  * @mas: The maple state
5863  * @min: The minimum value to check.
5864  *
5865  * Must hold rcu_read_lock or the write lock.
5866  * Will reset mas to ma_start if the status is ma_none.  Will stop on not
5867  * searchable nodes.
5868  *
5869  * Return: the previous value or %NULL.
5870  */
5871 void *mas_prev(struct ma_state *mas, unsigned long min)
5872 {
5873 	void *entry = NULL;
5874 
5875 	if (mas_prev_setup(mas, min, &entry))
5876 		return entry;
5877 
5878 	return mas_prev_slot(mas, min, false);
5879 }
5880 EXPORT_SYMBOL_GPL(mas_prev);
5881 
5882 /**
5883  * mas_prev_range() - Advance to the previous range
5884  * @mas: The maple state
5885  * @min: The minimum value to check.
5886  *
5887  * Sets @mas->index and @mas->last to the range.
5888  * Must hold rcu_read_lock or the write lock.
5889  * Will reset mas to ma_start if the node is ma_none.  Will stop on not
5890  * searchable nodes.
5891  *
5892  * Return: the previous value or %NULL.
5893  */
5894 void *mas_prev_range(struct ma_state *mas, unsigned long min)
5895 {
5896 	void *entry = NULL;
5897 
5898 	if (mas_prev_setup(mas, min, &entry))
5899 		return entry;
5900 
5901 	return mas_prev_slot(mas, min, true);
5902 }
5903 EXPORT_SYMBOL_GPL(mas_prev_range);
5904 
5905 /**
5906  * mt_prev() - get the previous value in the maple tree
5907  * @mt: The maple tree
5908  * @index: The start index
5909  * @min: The minimum index to check
5910  *
5911  * Takes RCU read lock internally to protect the search, which does not
5912  * protect the returned pointer after dropping RCU read lock.
5913  * See also: Documentation/core-api/maple_tree.rst
5914  *
5915  * Return: The entry before @index or %NULL if nothing is found.
5916  */
5917 void *mt_prev(struct maple_tree *mt, unsigned long index, unsigned long min)
5918 {
5919 	void *entry = NULL;
5920 	MA_STATE(mas, mt, index, index);
5921 
5922 	rcu_read_lock();
5923 	entry = mas_prev(&mas, min);
5924 	rcu_read_unlock();
5925 	return entry;
5926 }
5927 EXPORT_SYMBOL_GPL(mt_prev);
5928 
5929 /**
5930  * mas_pause() - Pause a mas_find/mas_for_each to drop the lock.
5931  * @mas: The maple state to pause
5932  *
5933  * Some users need to pause a walk and drop the lock they're holding in
5934  * order to yield to a higher priority thread or carry out an operation
5935  * on an entry.  Those users should call this function before they drop
5936  * the lock.  It resets the @mas to be suitable for the next iteration
5937  * of the loop after the user has reacquired the lock.  If most entries
5938  * found during a walk require you to call mas_pause(), the mt_for_each()
5939  * iterator may be more appropriate.
5940  *
5941  */
5942 void mas_pause(struct ma_state *mas)
5943 {
5944 	mas->status = ma_pause;
5945 	mas->node = NULL;
5946 }
5947 EXPORT_SYMBOL_GPL(mas_pause);
5948 
5949 /**
5950  * mas_find_setup() - Internal function to set up mas_find*().
5951  * @mas: The maple state
5952  * @max: The maximum index
5953  * @entry: Pointer to the entry
5954  *
5955  * Returns: True if entry is the answer, false otherwise.
5956  */
5957 static __always_inline bool mas_find_setup(struct ma_state *mas, unsigned long max, void **entry)
5958 {
5959 	switch (mas->status) {
5960 	case ma_active:
5961 		if (mas->last < max)
5962 			return false;
5963 		return true;
5964 	case ma_start:
5965 		break;
5966 	case ma_pause:
5967 		if (unlikely(mas->last >= max))
5968 			return true;
5969 
5970 		mas->index = ++mas->last;
5971 		mas->status = ma_start;
5972 		break;
5973 	case ma_none:
5974 		if (unlikely(mas->last >= max))
5975 			return true;
5976 
5977 		mas->index = mas->last;
5978 		mas->status = ma_start;
5979 		break;
5980 	case ma_underflow:
5981 		/* mas is pointing at entry before unable to go lower */
5982 		if (unlikely(mas->index >= max)) {
5983 			mas->status = ma_overflow;
5984 			return true;
5985 		}
5986 
5987 		mas_may_activate(mas);
5988 		*entry = mas_walk(mas);
5989 		if (*entry)
5990 			return true;
5991 		break;
5992 	case ma_overflow:
5993 		if (unlikely(mas->last >= max))
5994 			return true;
5995 
5996 		mas_may_activate(mas);
5997 		*entry = mas_walk(mas);
5998 		if (*entry)
5999 			return true;
6000 		break;
6001 	case ma_root:
6002 		break;
6003 	case ma_error:
6004 		return true;
6005 	}
6006 
6007 	if (mas_is_start(mas)) {
6008 		/* First run or continue */
6009 		if (mas->index > max)
6010 			return true;
6011 
6012 		*entry = mas_walk(mas);
6013 		if (*entry)
6014 			return true;
6015 
6016 	}
6017 
6018 	if (unlikely(mas_is_ptr(mas)))
6019 		goto ptr_out_of_range;
6020 
6021 	if (unlikely(mas_is_none(mas)))
6022 		return true;
6023 
6024 	if (mas->index == max)
6025 		return true;
6026 
6027 	return false;
6028 
6029 ptr_out_of_range:
6030 	mas->status = ma_none;
6031 	mas->index = 1;
6032 	mas->last = ULONG_MAX;
6033 	return true;
6034 }
6035 
6036 /**
6037  * mas_find() - On the first call, find the entry at or after mas->index up to
6038  * %max.  Otherwise, find the entry after mas->index.
6039  * @mas: The maple state
6040  * @max: The maximum value to check.
6041  *
6042  * Must hold rcu_read_lock or the write lock.
6043  * If an entry exists, last and index are updated accordingly.
6044  * May set @mas->status to ma_overflow.
6045  *
6046  * Return: The entry or %NULL.
6047  */
6048 void *mas_find(struct ma_state *mas, unsigned long max)
6049 {
6050 	void *entry = NULL;
6051 
6052 	if (mas_find_setup(mas, max, &entry))
6053 		return entry;
6054 
6055 	/* Retries on dead nodes handled by mas_next_slot */
6056 	entry = mas_next_slot(mas, max, false);
6057 	/* Ignore overflow */
6058 	mas->status = ma_active;
6059 	return entry;
6060 }
6061 EXPORT_SYMBOL_GPL(mas_find);
6062 
6063 /**
6064  * mas_find_range() - On the first call, find the entry at or after
6065  * mas->index up to %max.  Otherwise, advance to the next slot mas->index.
6066  * @mas: The maple state
6067  * @max: The maximum value to check.
6068  *
6069  * Must hold rcu_read_lock or the write lock.
6070  * If an entry exists, last and index are updated accordingly.
6071  * May set @mas->status to ma_overflow.
6072  *
6073  * Return: The entry or %NULL.
6074  */
6075 void *mas_find_range(struct ma_state *mas, unsigned long max)
6076 {
6077 	void *entry = NULL;
6078 
6079 	if (mas_find_setup(mas, max, &entry))
6080 		return entry;
6081 
6082 	/* Retries on dead nodes handled by mas_next_slot */
6083 	return mas_next_slot(mas, max, true);
6084 }
6085 EXPORT_SYMBOL_GPL(mas_find_range);
6086 
6087 /**
6088  * mas_find_rev_setup() - Internal function to set up mas_find_*_rev()
6089  * @mas: The maple state
6090  * @min: The minimum index
6091  * @entry: Pointer to the entry
6092  *
6093  * Returns: True if entry is the answer, false otherwise.
6094  */
6095 static bool mas_find_rev_setup(struct ma_state *mas, unsigned long min,
6096 		void **entry)
6097 {
6098 
6099 	switch (mas->status) {
6100 	case ma_active:
6101 		goto active;
6102 	case ma_start:
6103 		break;
6104 	case ma_pause:
6105 		if (unlikely(mas->index <= min)) {
6106 			mas->status = ma_underflow;
6107 			return true;
6108 		}
6109 		mas->last = --mas->index;
6110 		mas->status = ma_start;
6111 		break;
6112 	case ma_none:
6113 		if (mas->index <= min)
6114 			goto none;
6115 
6116 		mas->last = mas->index;
6117 		mas->status = ma_start;
6118 		break;
6119 	case ma_overflow: /* user expects the mas to be one after where it is */
6120 		if (unlikely(mas->index <= min)) {
6121 			mas->status = ma_underflow;
6122 			return true;
6123 		}
6124 
6125 		mas->status = ma_active;
6126 		break;
6127 	case ma_underflow: /* user expects the mas to be one before where it is */
6128 		if (unlikely(mas->index <= min))
6129 			return true;
6130 
6131 		mas->status = ma_active;
6132 		break;
6133 	case ma_root:
6134 		break;
6135 	case ma_error:
6136 		return true;
6137 	}
6138 
6139 	if (mas_is_start(mas)) {
6140 		/* First run or continue */
6141 		if (mas->index < min)
6142 			return true;
6143 
6144 		*entry = mas_walk(mas);
6145 		if (*entry)
6146 			return true;
6147 	}
6148 
6149 	if (unlikely(mas_is_ptr(mas)))
6150 		goto none;
6151 
6152 	if (unlikely(mas_is_none(mas))) {
6153 		/*
6154 		 * Walked to the location, and there was nothing so the previous
6155 		 * location is 0.
6156 		 */
6157 		mas->last = mas->index = 0;
6158 		mas->status = ma_root;
6159 		*entry = mas_root(mas);
6160 		return true;
6161 	}
6162 
6163 active:
6164 	if (mas->index < min)
6165 		return true;
6166 
6167 	return false;
6168 
6169 none:
6170 	mas->status = ma_none;
6171 	return true;
6172 }
6173 
6174 /**
6175  * mas_find_rev: On the first call, find the first non-null entry at or below
6176  * mas->index down to %min.  Otherwise find the first non-null entry below
6177  * mas->index down to %min.
6178  * @mas: The maple state
6179  * @min: The minimum value to check.
6180  *
6181  * Must hold rcu_read_lock or the write lock.
6182  * If an entry exists, last and index are updated accordingly.
6183  * May set @mas->status to ma_underflow.
6184  *
6185  * Return: The entry or %NULL.
6186  */
6187 void *mas_find_rev(struct ma_state *mas, unsigned long min)
6188 {
6189 	void *entry = NULL;
6190 
6191 	if (mas_find_rev_setup(mas, min, &entry))
6192 		return entry;
6193 
6194 	/* Retries on dead nodes handled by mas_prev_slot */
6195 	return mas_prev_slot(mas, min, false);
6196 
6197 }
6198 EXPORT_SYMBOL_GPL(mas_find_rev);
6199 
6200 /**
6201  * mas_find_range_rev: On the first call, find the first non-null entry at or
6202  * below mas->index down to %min.  Otherwise advance to the previous slot after
6203  * mas->index down to %min.
6204  * @mas: The maple state
6205  * @min: The minimum value to check.
6206  *
6207  * Must hold rcu_read_lock or the write lock.
6208  * If an entry exists, last and index are updated accordingly.
6209  * May set @mas->status to ma_underflow.
6210  *
6211  * Return: The entry or %NULL.
6212  */
6213 void *mas_find_range_rev(struct ma_state *mas, unsigned long min)
6214 {
6215 	void *entry = NULL;
6216 
6217 	if (mas_find_rev_setup(mas, min, &entry))
6218 		return entry;
6219 
6220 	/* Retries on dead nodes handled by mas_prev_slot */
6221 	return mas_prev_slot(mas, min, true);
6222 }
6223 EXPORT_SYMBOL_GPL(mas_find_range_rev);
6224 
6225 /**
6226  * mas_erase() - Find the range in which index resides and erase the entire
6227  * range.
6228  * @mas: The maple state
6229  *
6230  * Must hold the write lock.
6231  * Searches for @mas->index, sets @mas->index and @mas->last to the range and
6232  * erases that range.
6233  *
6234  * Return: the entry that was erased or %NULL, @mas->index and @mas->last are updated.
6235  */
6236 void *mas_erase(struct ma_state *mas)
6237 {
6238 	void *entry;
6239 	unsigned long index = mas->index;
6240 	MA_WR_STATE(wr_mas, mas, NULL);
6241 
6242 	if (!mas_is_active(mas) || !mas_is_start(mas))
6243 		mas->status = ma_start;
6244 
6245 write_retry:
6246 	entry = mas_state_walk(mas);
6247 	if (!entry)
6248 		return NULL;
6249 
6250 	/* Must reset to ensure spanning writes of last slot are detected */
6251 	mas_reset(mas);
6252 	mas_wr_preallocate(&wr_mas, NULL);
6253 	if (mas_nomem(mas, GFP_KERNEL)) {
6254 		/* in case the range of entry changed when unlocked */
6255 		mas->index = mas->last = index;
6256 		goto write_retry;
6257 	}
6258 
6259 	if (mas_is_err(mas))
6260 		goto out;
6261 
6262 	mas_wr_store_entry(&wr_mas);
6263 out:
6264 	mas_destroy(mas);
6265 	return entry;
6266 }
6267 EXPORT_SYMBOL_GPL(mas_erase);
6268 
6269 /**
6270  * mas_nomem() - Check if there was an error allocating and do the allocation
6271  * if necessary If there are allocations, then free them.
6272  * @mas: The maple state
6273  * @gfp: The GFP_FLAGS to use for allocations
6274  * Return: true on allocation, false otherwise.
6275  */
6276 bool mas_nomem(struct ma_state *mas, gfp_t gfp)
6277 	__must_hold(mas->tree->ma_lock)
6278 {
6279 	if (likely(mas->node != MA_ERROR(-ENOMEM)))
6280 		return false;
6281 
6282 	if (gfpflags_allow_blocking(gfp) && !mt_external_lock(mas->tree)) {
6283 		mtree_unlock(mas->tree);
6284 		mas_alloc_nodes(mas, gfp);
6285 		mtree_lock(mas->tree);
6286 	} else {
6287 		mas_alloc_nodes(mas, gfp);
6288 	}
6289 
6290 	if (!mas_allocated(mas))
6291 		return false;
6292 
6293 	mas->status = ma_start;
6294 	return true;
6295 }
6296 
6297 void __init maple_tree_init(void)
6298 {
6299 	maple_node_cache = kmem_cache_create("maple_node",
6300 			sizeof(struct maple_node), sizeof(struct maple_node),
6301 			SLAB_PANIC, NULL);
6302 }
6303 
6304 /**
6305  * mtree_load() - Load a value stored in a maple tree
6306  * @mt: The maple tree
6307  * @index: The index to load
6308  *
6309  * Return: the entry or %NULL
6310  */
6311 void *mtree_load(struct maple_tree *mt, unsigned long index)
6312 {
6313 	MA_STATE(mas, mt, index, index);
6314 	void *entry;
6315 
6316 	trace_ma_read(__func__, &mas);
6317 	rcu_read_lock();
6318 retry:
6319 	entry = mas_start(&mas);
6320 	if (unlikely(mas_is_none(&mas)))
6321 		goto unlock;
6322 
6323 	if (unlikely(mas_is_ptr(&mas))) {
6324 		if (index)
6325 			entry = NULL;
6326 
6327 		goto unlock;
6328 	}
6329 
6330 	entry = mtree_lookup_walk(&mas);
6331 	if (!entry && unlikely(mas_is_start(&mas)))
6332 		goto retry;
6333 unlock:
6334 	rcu_read_unlock();
6335 	if (xa_is_zero(entry))
6336 		return NULL;
6337 
6338 	return entry;
6339 }
6340 EXPORT_SYMBOL(mtree_load);
6341 
6342 /**
6343  * mtree_store_range() - Store an entry at a given range.
6344  * @mt: The maple tree
6345  * @index: The start of the range
6346  * @last: The end of the range
6347  * @entry: The entry to store
6348  * @gfp: The GFP_FLAGS to use for allocations
6349  *
6350  * Return: 0 on success, -EINVAL on invalid request, -ENOMEM if memory could not
6351  * be allocated.
6352  */
6353 int mtree_store_range(struct maple_tree *mt, unsigned long index,
6354 		unsigned long last, void *entry, gfp_t gfp)
6355 {
6356 	MA_STATE(mas, mt, index, last);
6357 	int ret = 0;
6358 
6359 	trace_ma_write(__func__, &mas, 0, entry);
6360 	if (WARN_ON_ONCE(xa_is_advanced(entry)))
6361 		return -EINVAL;
6362 
6363 	if (index > last)
6364 		return -EINVAL;
6365 
6366 	mtree_lock(mt);
6367 	ret = mas_store_gfp(&mas, entry, gfp);
6368 	mtree_unlock(mt);
6369 
6370 	return ret;
6371 }
6372 EXPORT_SYMBOL(mtree_store_range);
6373 
6374 /**
6375  * mtree_store() - Store an entry at a given index.
6376  * @mt: The maple tree
6377  * @index: The index to store the value
6378  * @entry: The entry to store
6379  * @gfp: The GFP_FLAGS to use for allocations
6380  *
6381  * Return: 0 on success, -EINVAL on invalid request, -ENOMEM if memory could not
6382  * be allocated.
6383  */
6384 int mtree_store(struct maple_tree *mt, unsigned long index, void *entry,
6385 		 gfp_t gfp)
6386 {
6387 	return mtree_store_range(mt, index, index, entry, gfp);
6388 }
6389 EXPORT_SYMBOL(mtree_store);
6390 
6391 /**
6392  * mtree_insert_range() - Insert an entry at a given range if there is no value.
6393  * @mt: The maple tree
6394  * @first: The start of the range
6395  * @last: The end of the range
6396  * @entry: The entry to store
6397  * @gfp: The GFP_FLAGS to use for allocations.
6398  *
6399  * Return: 0 on success, -EEXISTS if the range is occupied, -EINVAL on invalid
6400  * request, -ENOMEM if memory could not be allocated.
6401  */
6402 int mtree_insert_range(struct maple_tree *mt, unsigned long first,
6403 		unsigned long last, void *entry, gfp_t gfp)
6404 {
6405 	MA_STATE(ms, mt, first, last);
6406 	int ret = 0;
6407 
6408 	if (WARN_ON_ONCE(xa_is_advanced(entry)))
6409 		return -EINVAL;
6410 
6411 	if (first > last)
6412 		return -EINVAL;
6413 
6414 	mtree_lock(mt);
6415 retry:
6416 	mas_insert(&ms, entry);
6417 	if (mas_nomem(&ms, gfp))
6418 		goto retry;
6419 
6420 	mtree_unlock(mt);
6421 	if (mas_is_err(&ms))
6422 		ret = xa_err(ms.node);
6423 
6424 	mas_destroy(&ms);
6425 	return ret;
6426 }
6427 EXPORT_SYMBOL(mtree_insert_range);
6428 
6429 /**
6430  * mtree_insert() - Insert an entry at a given index if there is no value.
6431  * @mt: The maple tree
6432  * @index : The index to store the value
6433  * @entry: The entry to store
6434  * @gfp: The GFP_FLAGS to use for allocations.
6435  *
6436  * Return: 0 on success, -EEXISTS if the range is occupied, -EINVAL on invalid
6437  * request, -ENOMEM if memory could not be allocated.
6438  */
6439 int mtree_insert(struct maple_tree *mt, unsigned long index, void *entry,
6440 		 gfp_t gfp)
6441 {
6442 	return mtree_insert_range(mt, index, index, entry, gfp);
6443 }
6444 EXPORT_SYMBOL(mtree_insert);
6445 
6446 int mtree_alloc_range(struct maple_tree *mt, unsigned long *startp,
6447 		void *entry, unsigned long size, unsigned long min,
6448 		unsigned long max, gfp_t gfp)
6449 {
6450 	int ret = 0;
6451 
6452 	MA_STATE(mas, mt, 0, 0);
6453 	if (!mt_is_alloc(mt))
6454 		return -EINVAL;
6455 
6456 	if (WARN_ON_ONCE(mt_is_reserved(entry)))
6457 		return -EINVAL;
6458 
6459 	mtree_lock(mt);
6460 retry:
6461 	ret = mas_empty_area(&mas, min, max, size);
6462 	if (ret)
6463 		goto unlock;
6464 
6465 	mas_insert(&mas, entry);
6466 	/*
6467 	 * mas_nomem() may release the lock, causing the allocated area
6468 	 * to be unavailable, so try to allocate a free area again.
6469 	 */
6470 	if (mas_nomem(&mas, gfp))
6471 		goto retry;
6472 
6473 	if (mas_is_err(&mas))
6474 		ret = xa_err(mas.node);
6475 	else
6476 		*startp = mas.index;
6477 
6478 unlock:
6479 	mtree_unlock(mt);
6480 	mas_destroy(&mas);
6481 	return ret;
6482 }
6483 EXPORT_SYMBOL(mtree_alloc_range);
6484 
6485 /**
6486  * mtree_alloc_cyclic() - Find somewhere to store this entry in the tree.
6487  * @mt: The maple tree.
6488  * @startp: Pointer to ID.
6489  * @range_lo: Lower bound of range to search.
6490  * @range_hi: Upper bound of range to search.
6491  * @entry: The entry to store.
6492  * @next: Pointer to next ID to allocate.
6493  * @gfp: The GFP_FLAGS to use for allocations.
6494  *
6495  * Finds an empty entry in @mt after @next, stores the new index into
6496  * the @id pointer, stores the entry at that index, then updates @next.
6497  *
6498  * @mt must be initialized with the MT_FLAGS_ALLOC_RANGE flag.
6499  *
6500  * Context: Any context.  Takes and releases the mt.lock.  May sleep if
6501  * the @gfp flags permit.
6502  *
6503  * Return: 0 if the allocation succeeded without wrapping, 1 if the
6504  * allocation succeeded after wrapping, -ENOMEM if memory could not be
6505  * allocated, -EINVAL if @mt cannot be used, or -EBUSY if there are no
6506  * free entries.
6507  */
6508 int mtree_alloc_cyclic(struct maple_tree *mt, unsigned long *startp,
6509 		void *entry, unsigned long range_lo, unsigned long range_hi,
6510 		unsigned long *next, gfp_t gfp)
6511 {
6512 	int ret;
6513 
6514 	MA_STATE(mas, mt, 0, 0);
6515 
6516 	if (!mt_is_alloc(mt))
6517 		return -EINVAL;
6518 	if (WARN_ON_ONCE(mt_is_reserved(entry)))
6519 		return -EINVAL;
6520 	mtree_lock(mt);
6521 	ret = mas_alloc_cyclic(&mas, startp, entry, range_lo, range_hi,
6522 			       next, gfp);
6523 	mtree_unlock(mt);
6524 	return ret;
6525 }
6526 EXPORT_SYMBOL(mtree_alloc_cyclic);
6527 
6528 int mtree_alloc_rrange(struct maple_tree *mt, unsigned long *startp,
6529 		void *entry, unsigned long size, unsigned long min,
6530 		unsigned long max, gfp_t gfp)
6531 {
6532 	int ret = 0;
6533 
6534 	MA_STATE(mas, mt, 0, 0);
6535 	if (!mt_is_alloc(mt))
6536 		return -EINVAL;
6537 
6538 	if (WARN_ON_ONCE(mt_is_reserved(entry)))
6539 		return -EINVAL;
6540 
6541 	mtree_lock(mt);
6542 retry:
6543 	ret = mas_empty_area_rev(&mas, min, max, size);
6544 	if (ret)
6545 		goto unlock;
6546 
6547 	mas_insert(&mas, entry);
6548 	/*
6549 	 * mas_nomem() may release the lock, causing the allocated area
6550 	 * to be unavailable, so try to allocate a free area again.
6551 	 */
6552 	if (mas_nomem(&mas, gfp))
6553 		goto retry;
6554 
6555 	if (mas_is_err(&mas))
6556 		ret = xa_err(mas.node);
6557 	else
6558 		*startp = mas.index;
6559 
6560 unlock:
6561 	mtree_unlock(mt);
6562 	mas_destroy(&mas);
6563 	return ret;
6564 }
6565 EXPORT_SYMBOL(mtree_alloc_rrange);
6566 
6567 /**
6568  * mtree_erase() - Find an index and erase the entire range.
6569  * @mt: The maple tree
6570  * @index: The index to erase
6571  *
6572  * Erasing is the same as a walk to an entry then a store of a NULL to that
6573  * ENTIRE range.  In fact, it is implemented as such using the advanced API.
6574  *
6575  * Return: The entry stored at the @index or %NULL
6576  */
6577 void *mtree_erase(struct maple_tree *mt, unsigned long index)
6578 {
6579 	void *entry = NULL;
6580 
6581 	MA_STATE(mas, mt, index, index);
6582 	trace_ma_op(__func__, &mas);
6583 
6584 	mtree_lock(mt);
6585 	entry = mas_erase(&mas);
6586 	mtree_unlock(mt);
6587 
6588 	return entry;
6589 }
6590 EXPORT_SYMBOL(mtree_erase);
6591 
6592 /*
6593  * mas_dup_free() - Free an incomplete duplication of a tree.
6594  * @mas: The maple state of a incomplete tree.
6595  *
6596  * The parameter @mas->node passed in indicates that the allocation failed on
6597  * this node. This function frees all nodes starting from @mas->node in the
6598  * reverse order of mas_dup_build(). There is no need to hold the source tree
6599  * lock at this time.
6600  */
6601 static void mas_dup_free(struct ma_state *mas)
6602 {
6603 	struct maple_node *node;
6604 	enum maple_type type;
6605 	void __rcu **slots;
6606 	unsigned char count, i;
6607 
6608 	/* Maybe the first node allocation failed. */
6609 	if (mas_is_none(mas))
6610 		return;
6611 
6612 	while (!mte_is_root(mas->node)) {
6613 		mas_ascend(mas);
6614 		if (mas->offset) {
6615 			mas->offset--;
6616 			do {
6617 				mas_descend(mas);
6618 				mas->offset = mas_data_end(mas);
6619 			} while (!mte_is_leaf(mas->node));
6620 
6621 			mas_ascend(mas);
6622 		}
6623 
6624 		node = mte_to_node(mas->node);
6625 		type = mte_node_type(mas->node);
6626 		slots = ma_slots(node, type);
6627 		count = mas_data_end(mas) + 1;
6628 		for (i = 0; i < count; i++)
6629 			((unsigned long *)slots)[i] &= ~MAPLE_NODE_MASK;
6630 		mt_free_bulk(count, slots);
6631 	}
6632 
6633 	node = mte_to_node(mas->node);
6634 	mt_free_one(node);
6635 }
6636 
6637 /*
6638  * mas_copy_node() - Copy a maple node and replace the parent.
6639  * @mas: The maple state of source tree.
6640  * @new_mas: The maple state of new tree.
6641  * @parent: The parent of the new node.
6642  *
6643  * Copy @mas->node to @new_mas->node, set @parent to be the parent of
6644  * @new_mas->node. If memory allocation fails, @mas is set to -ENOMEM.
6645  */
6646 static inline void mas_copy_node(struct ma_state *mas, struct ma_state *new_mas,
6647 		struct maple_pnode *parent)
6648 {
6649 	struct maple_node *node = mte_to_node(mas->node);
6650 	struct maple_node *new_node = mte_to_node(new_mas->node);
6651 	unsigned long val;
6652 
6653 	/* Copy the node completely. */
6654 	memcpy(new_node, node, sizeof(struct maple_node));
6655 	/* Update the parent node pointer. */
6656 	val = (unsigned long)node->parent & MAPLE_NODE_MASK;
6657 	new_node->parent = ma_parent_ptr(val | (unsigned long)parent);
6658 }
6659 
6660 /*
6661  * mas_dup_alloc() - Allocate child nodes for a maple node.
6662  * @mas: The maple state of source tree.
6663  * @new_mas: The maple state of new tree.
6664  * @gfp: The GFP_FLAGS to use for allocations.
6665  *
6666  * This function allocates child nodes for @new_mas->node during the duplication
6667  * process. If memory allocation fails, @mas is set to -ENOMEM.
6668  */
6669 static inline void mas_dup_alloc(struct ma_state *mas, struct ma_state *new_mas,
6670 		gfp_t gfp)
6671 {
6672 	struct maple_node *node = mte_to_node(mas->node);
6673 	struct maple_node *new_node = mte_to_node(new_mas->node);
6674 	enum maple_type type;
6675 	unsigned char request, count, i;
6676 	void __rcu **slots;
6677 	void __rcu **new_slots;
6678 	unsigned long val;
6679 
6680 	/* Allocate memory for child nodes. */
6681 	type = mte_node_type(mas->node);
6682 	new_slots = ma_slots(new_node, type);
6683 	request = mas_data_end(mas) + 1;
6684 	count = mt_alloc_bulk(gfp, request, (void **)new_slots);
6685 	if (unlikely(count < request)) {
6686 		memset(new_slots, 0, request * sizeof(void *));
6687 		mas_set_err(mas, -ENOMEM);
6688 		return;
6689 	}
6690 
6691 	/* Restore node type information in slots. */
6692 	slots = ma_slots(node, type);
6693 	for (i = 0; i < count; i++) {
6694 		val = (unsigned long)mt_slot_locked(mas->tree, slots, i);
6695 		val &= MAPLE_NODE_MASK;
6696 		((unsigned long *)new_slots)[i] |= val;
6697 	}
6698 }
6699 
6700 /*
6701  * mas_dup_build() - Build a new maple tree from a source tree
6702  * @mas: The maple state of source tree, need to be in MAS_START state.
6703  * @new_mas: The maple state of new tree, need to be in MAS_START state.
6704  * @gfp: The GFP_FLAGS to use for allocations.
6705  *
6706  * This function builds a new tree in DFS preorder. If the memory allocation
6707  * fails, the error code -ENOMEM will be set in @mas, and @new_mas points to the
6708  * last node. mas_dup_free() will free the incomplete duplication of a tree.
6709  *
6710  * Note that the attributes of the two trees need to be exactly the same, and the
6711  * new tree needs to be empty, otherwise -EINVAL will be set in @mas.
6712  */
6713 static inline void mas_dup_build(struct ma_state *mas, struct ma_state *new_mas,
6714 		gfp_t gfp)
6715 {
6716 	struct maple_node *node;
6717 	struct maple_pnode *parent = NULL;
6718 	struct maple_enode *root;
6719 	enum maple_type type;
6720 
6721 	if (unlikely(mt_attr(mas->tree) != mt_attr(new_mas->tree)) ||
6722 	    unlikely(!mtree_empty(new_mas->tree))) {
6723 		mas_set_err(mas, -EINVAL);
6724 		return;
6725 	}
6726 
6727 	root = mas_start(mas);
6728 	if (mas_is_ptr(mas) || mas_is_none(mas))
6729 		goto set_new_tree;
6730 
6731 	node = mt_alloc_one(gfp);
6732 	if (!node) {
6733 		new_mas->status = ma_none;
6734 		mas_set_err(mas, -ENOMEM);
6735 		return;
6736 	}
6737 
6738 	type = mte_node_type(mas->node);
6739 	root = mt_mk_node(node, type);
6740 	new_mas->node = root;
6741 	new_mas->min = 0;
6742 	new_mas->max = ULONG_MAX;
6743 	root = mte_mk_root(root);
6744 	while (1) {
6745 		mas_copy_node(mas, new_mas, parent);
6746 		if (!mte_is_leaf(mas->node)) {
6747 			/* Only allocate child nodes for non-leaf nodes. */
6748 			mas_dup_alloc(mas, new_mas, gfp);
6749 			if (unlikely(mas_is_err(mas)))
6750 				return;
6751 		} else {
6752 			/*
6753 			 * This is the last leaf node and duplication is
6754 			 * completed.
6755 			 */
6756 			if (mas->max == ULONG_MAX)
6757 				goto done;
6758 
6759 			/* This is not the last leaf node and needs to go up. */
6760 			do {
6761 				mas_ascend(mas);
6762 				mas_ascend(new_mas);
6763 			} while (mas->offset == mas_data_end(mas));
6764 
6765 			/* Move to the next subtree. */
6766 			mas->offset++;
6767 			new_mas->offset++;
6768 		}
6769 
6770 		mas_descend(mas);
6771 		parent = ma_parent_ptr(mte_to_node(new_mas->node));
6772 		mas_descend(new_mas);
6773 		mas->offset = 0;
6774 		new_mas->offset = 0;
6775 	}
6776 done:
6777 	/* Specially handle the parent of the root node. */
6778 	mte_to_node(root)->parent = ma_parent_ptr(mas_tree_parent(new_mas));
6779 set_new_tree:
6780 	/* Make them the same height */
6781 	new_mas->tree->ma_flags = mas->tree->ma_flags;
6782 	rcu_assign_pointer(new_mas->tree->ma_root, root);
6783 }
6784 
6785 /**
6786  * __mt_dup(): Duplicate an entire maple tree
6787  * @mt: The source maple tree
6788  * @new: The new maple tree
6789  * @gfp: The GFP_FLAGS to use for allocations
6790  *
6791  * This function duplicates a maple tree in Depth-First Search (DFS) pre-order
6792  * traversal. It uses memcpy() to copy nodes in the source tree and allocate
6793  * new child nodes in non-leaf nodes. The new node is exactly the same as the
6794  * source node except for all the addresses stored in it. It will be faster than
6795  * traversing all elements in the source tree and inserting them one by one into
6796  * the new tree.
6797  * The user needs to ensure that the attributes of the source tree and the new
6798  * tree are the same, and the new tree needs to be an empty tree, otherwise
6799  * -EINVAL will be returned.
6800  * Note that the user needs to manually lock the source tree and the new tree.
6801  *
6802  * Return: 0 on success, -ENOMEM if memory could not be allocated, -EINVAL If
6803  * the attributes of the two trees are different or the new tree is not an empty
6804  * tree.
6805  */
6806 int __mt_dup(struct maple_tree *mt, struct maple_tree *new, gfp_t gfp)
6807 {
6808 	int ret = 0;
6809 	MA_STATE(mas, mt, 0, 0);
6810 	MA_STATE(new_mas, new, 0, 0);
6811 
6812 	mas_dup_build(&mas, &new_mas, gfp);
6813 	if (unlikely(mas_is_err(&mas))) {
6814 		ret = xa_err(mas.node);
6815 		if (ret == -ENOMEM)
6816 			mas_dup_free(&new_mas);
6817 	}
6818 
6819 	return ret;
6820 }
6821 EXPORT_SYMBOL(__mt_dup);
6822 
6823 /**
6824  * mtree_dup(): Duplicate an entire maple tree
6825  * @mt: The source maple tree
6826  * @new: The new maple tree
6827  * @gfp: The GFP_FLAGS to use for allocations
6828  *
6829  * This function duplicates a maple tree in Depth-First Search (DFS) pre-order
6830  * traversal. It uses memcpy() to copy nodes in the source tree and allocate
6831  * new child nodes in non-leaf nodes. The new node is exactly the same as the
6832  * source node except for all the addresses stored in it. It will be faster than
6833  * traversing all elements in the source tree and inserting them one by one into
6834  * the new tree.
6835  * The user needs to ensure that the attributes of the source tree and the new
6836  * tree are the same, and the new tree needs to be an empty tree, otherwise
6837  * -EINVAL will be returned.
6838  *
6839  * Return: 0 on success, -ENOMEM if memory could not be allocated, -EINVAL If
6840  * the attributes of the two trees are different or the new tree is not an empty
6841  * tree.
6842  */
6843 int mtree_dup(struct maple_tree *mt, struct maple_tree *new, gfp_t gfp)
6844 {
6845 	int ret = 0;
6846 	MA_STATE(mas, mt, 0, 0);
6847 	MA_STATE(new_mas, new, 0, 0);
6848 
6849 	mas_lock(&new_mas);
6850 	mas_lock_nested(&mas, SINGLE_DEPTH_NESTING);
6851 	mas_dup_build(&mas, &new_mas, gfp);
6852 	mas_unlock(&mas);
6853 	if (unlikely(mas_is_err(&mas))) {
6854 		ret = xa_err(mas.node);
6855 		if (ret == -ENOMEM)
6856 			mas_dup_free(&new_mas);
6857 	}
6858 
6859 	mas_unlock(&new_mas);
6860 	return ret;
6861 }
6862 EXPORT_SYMBOL(mtree_dup);
6863 
6864 /**
6865  * __mt_destroy() - Walk and free all nodes of a locked maple tree.
6866  * @mt: The maple tree
6867  *
6868  * Note: Does not handle locking.
6869  */
6870 void __mt_destroy(struct maple_tree *mt)
6871 {
6872 	void *root = mt_root_locked(mt);
6873 
6874 	rcu_assign_pointer(mt->ma_root, NULL);
6875 	if (xa_is_node(root))
6876 		mte_destroy_walk(root, mt);
6877 
6878 	mt->ma_flags = mt_attr(mt);
6879 }
6880 EXPORT_SYMBOL_GPL(__mt_destroy);
6881 
6882 /**
6883  * mtree_destroy() - Destroy a maple tree
6884  * @mt: The maple tree
6885  *
6886  * Frees all resources used by the tree.  Handles locking.
6887  */
6888 void mtree_destroy(struct maple_tree *mt)
6889 {
6890 	mtree_lock(mt);
6891 	__mt_destroy(mt);
6892 	mtree_unlock(mt);
6893 }
6894 EXPORT_SYMBOL(mtree_destroy);
6895 
6896 /**
6897  * mt_find() - Search from the start up until an entry is found.
6898  * @mt: The maple tree
6899  * @index: Pointer which contains the start location of the search
6900  * @max: The maximum value of the search range
6901  *
6902  * Takes RCU read lock internally to protect the search, which does not
6903  * protect the returned pointer after dropping RCU read lock.
6904  * See also: Documentation/core-api/maple_tree.rst
6905  *
6906  * In case that an entry is found @index is updated to point to the next
6907  * possible entry independent whether the found entry is occupying a
6908  * single index or a range if indices.
6909  *
6910  * Return: The entry at or after the @index or %NULL
6911  */
6912 void *mt_find(struct maple_tree *mt, unsigned long *index, unsigned long max)
6913 {
6914 	MA_STATE(mas, mt, *index, *index);
6915 	void *entry;
6916 #ifdef CONFIG_DEBUG_MAPLE_TREE
6917 	unsigned long copy = *index;
6918 #endif
6919 
6920 	trace_ma_read(__func__, &mas);
6921 
6922 	if ((*index) > max)
6923 		return NULL;
6924 
6925 	rcu_read_lock();
6926 retry:
6927 	entry = mas_state_walk(&mas);
6928 	if (mas_is_start(&mas))
6929 		goto retry;
6930 
6931 	if (unlikely(xa_is_zero(entry)))
6932 		entry = NULL;
6933 
6934 	if (entry)
6935 		goto unlock;
6936 
6937 	while (mas_is_active(&mas) && (mas.last < max)) {
6938 		entry = mas_next_slot(&mas, max, false);
6939 		if (likely(entry && !xa_is_zero(entry)))
6940 			break;
6941 	}
6942 
6943 	if (unlikely(xa_is_zero(entry)))
6944 		entry = NULL;
6945 unlock:
6946 	rcu_read_unlock();
6947 	if (likely(entry)) {
6948 		*index = mas.last + 1;
6949 #ifdef CONFIG_DEBUG_MAPLE_TREE
6950 		if (MT_WARN_ON(mt, (*index) && ((*index) <= copy)))
6951 			pr_err("index not increased! %lx <= %lx\n",
6952 			       *index, copy);
6953 #endif
6954 	}
6955 
6956 	return entry;
6957 }
6958 EXPORT_SYMBOL(mt_find);
6959 
6960 /**
6961  * mt_find_after() - Search from the start up until an entry is found.
6962  * @mt: The maple tree
6963  * @index: Pointer which contains the start location of the search
6964  * @max: The maximum value to check
6965  *
6966  * Same as mt_find() except that it checks @index for 0 before
6967  * searching. If @index == 0, the search is aborted. This covers a wrap
6968  * around of @index to 0 in an iterator loop.
6969  *
6970  * Return: The entry at or after the @index or %NULL
6971  */
6972 void *mt_find_after(struct maple_tree *mt, unsigned long *index,
6973 		    unsigned long max)
6974 {
6975 	if (!(*index))
6976 		return NULL;
6977 
6978 	return mt_find(mt, index, max);
6979 }
6980 EXPORT_SYMBOL(mt_find_after);
6981 
6982 #ifdef CONFIG_DEBUG_MAPLE_TREE
6983 atomic_t maple_tree_tests_run;
6984 EXPORT_SYMBOL_GPL(maple_tree_tests_run);
6985 atomic_t maple_tree_tests_passed;
6986 EXPORT_SYMBOL_GPL(maple_tree_tests_passed);
6987 
6988 #ifndef __KERNEL__
6989 extern void kmem_cache_set_non_kernel(struct kmem_cache *, unsigned int);
6990 void mt_set_non_kernel(unsigned int val)
6991 {
6992 	kmem_cache_set_non_kernel(maple_node_cache, val);
6993 }
6994 
6995 extern void kmem_cache_set_callback(struct kmem_cache *cachep,
6996 		void (*callback)(void *));
6997 void mt_set_callback(void (*callback)(void *))
6998 {
6999 	kmem_cache_set_callback(maple_node_cache, callback);
7000 }
7001 
7002 extern void kmem_cache_set_private(struct kmem_cache *cachep, void *private);
7003 void mt_set_private(void *private)
7004 {
7005 	kmem_cache_set_private(maple_node_cache, private);
7006 }
7007 
7008 extern unsigned long kmem_cache_get_alloc(struct kmem_cache *);
7009 unsigned long mt_get_alloc_size(void)
7010 {
7011 	return kmem_cache_get_alloc(maple_node_cache);
7012 }
7013 
7014 extern void kmem_cache_zero_nr_tallocated(struct kmem_cache *);
7015 void mt_zero_nr_tallocated(void)
7016 {
7017 	kmem_cache_zero_nr_tallocated(maple_node_cache);
7018 }
7019 
7020 extern unsigned int kmem_cache_nr_tallocated(struct kmem_cache *);
7021 unsigned int mt_nr_tallocated(void)
7022 {
7023 	return kmem_cache_nr_tallocated(maple_node_cache);
7024 }
7025 
7026 extern unsigned int kmem_cache_nr_allocated(struct kmem_cache *);
7027 unsigned int mt_nr_allocated(void)
7028 {
7029 	return kmem_cache_nr_allocated(maple_node_cache);
7030 }
7031 
7032 void mt_cache_shrink(void)
7033 {
7034 }
7035 #else
7036 /*
7037  * mt_cache_shrink() - For testing, don't use this.
7038  *
7039  * Certain testcases can trigger an OOM when combined with other memory
7040  * debugging configuration options.  This function is used to reduce the
7041  * possibility of an out of memory even due to kmem_cache objects remaining
7042  * around for longer than usual.
7043  */
7044 void mt_cache_shrink(void)
7045 {
7046 	kmem_cache_shrink(maple_node_cache);
7047 
7048 }
7049 EXPORT_SYMBOL_GPL(mt_cache_shrink);
7050 
7051 #endif /* not defined __KERNEL__ */
7052 /*
7053  * mas_get_slot() - Get the entry in the maple state node stored at @offset.
7054  * @mas: The maple state
7055  * @offset: The offset into the slot array to fetch.
7056  *
7057  * Return: The entry stored at @offset.
7058  */
7059 static inline struct maple_enode *mas_get_slot(struct ma_state *mas,
7060 		unsigned char offset)
7061 {
7062 	return mas_slot(mas, ma_slots(mas_mn(mas), mte_node_type(mas->node)),
7063 			offset);
7064 }
7065 
7066 /* Depth first search, post-order */
7067 static void mas_dfs_postorder(struct ma_state *mas, unsigned long max)
7068 {
7069 
7070 	struct maple_enode *p, *mn = mas->node;
7071 	unsigned long p_min, p_max;
7072 
7073 	mas_next_node(mas, mas_mn(mas), max);
7074 	if (!mas_is_overflow(mas))
7075 		return;
7076 
7077 	if (mte_is_root(mn))
7078 		return;
7079 
7080 	mas->node = mn;
7081 	mas_ascend(mas);
7082 	do {
7083 		p = mas->node;
7084 		p_min = mas->min;
7085 		p_max = mas->max;
7086 		mas_prev_node(mas, 0);
7087 	} while (!mas_is_underflow(mas));
7088 
7089 	mas->node = p;
7090 	mas->max = p_max;
7091 	mas->min = p_min;
7092 }
7093 
7094 /* Tree validations */
7095 static void mt_dump_node(const struct maple_tree *mt, void *entry,
7096 		unsigned long min, unsigned long max, unsigned int depth,
7097 		enum mt_dump_format format);
7098 static void mt_dump_range(unsigned long min, unsigned long max,
7099 			  unsigned int depth, enum mt_dump_format format)
7100 {
7101 	static const char spaces[] = "                                ";
7102 
7103 	switch(format) {
7104 	case mt_dump_hex:
7105 		if (min == max)
7106 			pr_info("%.*s%lx: ", depth * 2, spaces, min);
7107 		else
7108 			pr_info("%.*s%lx-%lx: ", depth * 2, spaces, min, max);
7109 		break;
7110 	case mt_dump_dec:
7111 		if (min == max)
7112 			pr_info("%.*s%lu: ", depth * 2, spaces, min);
7113 		else
7114 			pr_info("%.*s%lu-%lu: ", depth * 2, spaces, min, max);
7115 	}
7116 }
7117 
7118 static void mt_dump_entry(void *entry, unsigned long min, unsigned long max,
7119 			  unsigned int depth, enum mt_dump_format format)
7120 {
7121 	mt_dump_range(min, max, depth, format);
7122 
7123 	if (xa_is_value(entry))
7124 		pr_cont("value %ld (0x%lx) [" PTR_FMT "]\n", xa_to_value(entry),
7125 			xa_to_value(entry), entry);
7126 	else if (xa_is_zero(entry))
7127 		pr_cont("zero (%ld)\n", xa_to_internal(entry));
7128 	else if (mt_is_reserved(entry))
7129 		pr_cont("UNKNOWN ENTRY (" PTR_FMT ")\n", entry);
7130 	else
7131 		pr_cont(PTR_FMT "\n", entry);
7132 }
7133 
7134 static void mt_dump_range64(const struct maple_tree *mt, void *entry,
7135 		unsigned long min, unsigned long max, unsigned int depth,
7136 		enum mt_dump_format format)
7137 {
7138 	struct maple_range_64 *node = &mte_to_node(entry)->mr64;
7139 	bool leaf = mte_is_leaf(entry);
7140 	unsigned long first = min;
7141 	int i;
7142 
7143 	pr_cont(" contents: ");
7144 	for (i = 0; i < MAPLE_RANGE64_SLOTS - 1; i++) {
7145 		switch(format) {
7146 		case mt_dump_hex:
7147 			pr_cont(PTR_FMT " %lX ", node->slot[i], node->pivot[i]);
7148 			break;
7149 		case mt_dump_dec:
7150 			pr_cont(PTR_FMT " %lu ", node->slot[i], node->pivot[i]);
7151 		}
7152 	}
7153 	pr_cont(PTR_FMT "\n", node->slot[i]);
7154 	for (i = 0; i < MAPLE_RANGE64_SLOTS; i++) {
7155 		unsigned long last = max;
7156 
7157 		if (i < (MAPLE_RANGE64_SLOTS - 1))
7158 			last = node->pivot[i];
7159 		else if (!node->slot[i] && max != mt_node_max(entry))
7160 			break;
7161 		if (last == 0 && i > 0)
7162 			break;
7163 		if (leaf)
7164 			mt_dump_entry(mt_slot(mt, node->slot, i),
7165 					first, last, depth + 1, format);
7166 		else if (node->slot[i])
7167 			mt_dump_node(mt, mt_slot(mt, node->slot, i),
7168 					first, last, depth + 1, format);
7169 
7170 		if (last == max)
7171 			break;
7172 		if (last > max) {
7173 			switch(format) {
7174 			case mt_dump_hex:
7175 				pr_err("node " PTR_FMT " last (%lx) > max (%lx) at pivot %d!\n",
7176 					node, last, max, i);
7177 				break;
7178 			case mt_dump_dec:
7179 				pr_err("node " PTR_FMT " last (%lu) > max (%lu) at pivot %d!\n",
7180 					node, last, max, i);
7181 			}
7182 		}
7183 		first = last + 1;
7184 	}
7185 }
7186 
7187 static void mt_dump_arange64(const struct maple_tree *mt, void *entry,
7188 	unsigned long min, unsigned long max, unsigned int depth,
7189 	enum mt_dump_format format)
7190 {
7191 	struct maple_arange_64 *node = &mte_to_node(entry)->ma64;
7192 	unsigned long first = min;
7193 	int i;
7194 
7195 	pr_cont(" contents: ");
7196 	for (i = 0; i < MAPLE_ARANGE64_SLOTS; i++) {
7197 		switch (format) {
7198 		case mt_dump_hex:
7199 			pr_cont("%lx ", node->gap[i]);
7200 			break;
7201 		case mt_dump_dec:
7202 			pr_cont("%lu ", node->gap[i]);
7203 		}
7204 	}
7205 	pr_cont("| %02X %02X| ", node->meta.end, node->meta.gap);
7206 	for (i = 0; i < MAPLE_ARANGE64_SLOTS - 1; i++) {
7207 		switch (format) {
7208 		case mt_dump_hex:
7209 			pr_cont(PTR_FMT " %lX ", node->slot[i], node->pivot[i]);
7210 			break;
7211 		case mt_dump_dec:
7212 			pr_cont(PTR_FMT " %lu ", node->slot[i], node->pivot[i]);
7213 		}
7214 	}
7215 	pr_cont(PTR_FMT "\n", node->slot[i]);
7216 	for (i = 0; i < MAPLE_ARANGE64_SLOTS; i++) {
7217 		unsigned long last = max;
7218 
7219 		if (i < (MAPLE_ARANGE64_SLOTS - 1))
7220 			last = node->pivot[i];
7221 		else if (!node->slot[i])
7222 			break;
7223 		if (last == 0 && i > 0)
7224 			break;
7225 		if (node->slot[i])
7226 			mt_dump_node(mt, mt_slot(mt, node->slot, i),
7227 					first, last, depth + 1, format);
7228 
7229 		if (last == max)
7230 			break;
7231 		if (last > max) {
7232 			switch(format) {
7233 			case mt_dump_hex:
7234 				pr_err("node " PTR_FMT " last (%lx) > max (%lx) at pivot %d!\n",
7235 					node, last, max, i);
7236 				break;
7237 			case mt_dump_dec:
7238 				pr_err("node " PTR_FMT " last (%lu) > max (%lu) at pivot %d!\n",
7239 					node, last, max, i);
7240 			}
7241 		}
7242 		first = last + 1;
7243 	}
7244 }
7245 
7246 static void mt_dump_node(const struct maple_tree *mt, void *entry,
7247 		unsigned long min, unsigned long max, unsigned int depth,
7248 		enum mt_dump_format format)
7249 {
7250 	struct maple_node *node = mte_to_node(entry);
7251 	unsigned int type = mte_node_type(entry);
7252 	unsigned int i;
7253 
7254 	mt_dump_range(min, max, depth, format);
7255 
7256 	pr_cont("node " PTR_FMT " depth %d type %d parent " PTR_FMT, node,
7257 		depth, type, node ? node->parent : NULL);
7258 	switch (type) {
7259 	case maple_dense:
7260 		pr_cont("\n");
7261 		for (i = 0; i < MAPLE_NODE_SLOTS; i++) {
7262 			if (min + i > max)
7263 				pr_cont("OUT OF RANGE: ");
7264 			mt_dump_entry(mt_slot(mt, node->slot, i),
7265 					min + i, min + i, depth, format);
7266 		}
7267 		break;
7268 	case maple_leaf_64:
7269 	case maple_range_64:
7270 		mt_dump_range64(mt, entry, min, max, depth, format);
7271 		break;
7272 	case maple_arange_64:
7273 		mt_dump_arange64(mt, entry, min, max, depth, format);
7274 		break;
7275 
7276 	default:
7277 		pr_cont(" UNKNOWN TYPE\n");
7278 	}
7279 }
7280 
7281 void mt_dump(const struct maple_tree *mt, enum mt_dump_format format)
7282 {
7283 	void *entry = rcu_dereference_check(mt->ma_root, mt_locked(mt));
7284 
7285 	pr_info("maple_tree(" PTR_FMT ") flags %X, height %u root " PTR_FMT "\n",
7286 		 mt, mt->ma_flags, mt_height(mt), entry);
7287 	if (xa_is_node(entry))
7288 		mt_dump_node(mt, entry, 0, mt_node_max(entry), 0, format);
7289 	else if (entry)
7290 		mt_dump_entry(entry, 0, 0, 0, format);
7291 	else
7292 		pr_info("(empty)\n");
7293 }
7294 EXPORT_SYMBOL_GPL(mt_dump);
7295 
7296 /*
7297  * Calculate the maximum gap in a node and check if that's what is reported in
7298  * the parent (unless root).
7299  */
7300 static void mas_validate_gaps(struct ma_state *mas)
7301 {
7302 	struct maple_enode *mte = mas->node;
7303 	struct maple_node *p_mn, *node = mte_to_node(mte);
7304 	enum maple_type mt = mte_node_type(mas->node);
7305 	unsigned long gap = 0, max_gap = 0;
7306 	unsigned long p_end, p_start = mas->min;
7307 	unsigned char p_slot, offset;
7308 	unsigned long *gaps = NULL;
7309 	unsigned long *pivots = ma_pivots(node, mt);
7310 	unsigned int i;
7311 
7312 	if (ma_is_dense(mt)) {
7313 		for (i = 0; i < mt_slot_count(mte); i++) {
7314 			if (mas_get_slot(mas, i)) {
7315 				if (gap > max_gap)
7316 					max_gap = gap;
7317 				gap = 0;
7318 				continue;
7319 			}
7320 			gap++;
7321 		}
7322 		goto counted;
7323 	}
7324 
7325 	gaps = ma_gaps(node, mt);
7326 	for (i = 0; i < mt_slot_count(mte); i++) {
7327 		p_end = mas_safe_pivot(mas, pivots, i, mt);
7328 
7329 		if (!gaps) {
7330 			if (!mas_get_slot(mas, i))
7331 				gap = p_end - p_start + 1;
7332 		} else {
7333 			void *entry = mas_get_slot(mas, i);
7334 
7335 			gap = gaps[i];
7336 			MT_BUG_ON(mas->tree, !entry);
7337 
7338 			if (gap > p_end - p_start + 1) {
7339 				pr_err(PTR_FMT "[%u] %lu >= %lu - %lu + 1 (%lu)\n",
7340 				       mas_mn(mas), i, gap, p_end, p_start,
7341 				       p_end - p_start + 1);
7342 				MT_BUG_ON(mas->tree, gap > p_end - p_start + 1);
7343 			}
7344 		}
7345 
7346 		if (gap > max_gap)
7347 			max_gap = gap;
7348 
7349 		p_start = p_end + 1;
7350 		if (p_end >= mas->max)
7351 			break;
7352 	}
7353 
7354 counted:
7355 	if (mt == maple_arange_64) {
7356 		MT_BUG_ON(mas->tree, !gaps);
7357 		offset = ma_meta_gap(node);
7358 		if (offset > i) {
7359 			pr_err("gap offset " PTR_FMT "[%u] is invalid\n", node, offset);
7360 			MT_BUG_ON(mas->tree, 1);
7361 		}
7362 
7363 		if (gaps[offset] != max_gap) {
7364 			pr_err("gap " PTR_FMT "[%u] is not the largest gap %lu\n",
7365 			       node, offset, max_gap);
7366 			MT_BUG_ON(mas->tree, 1);
7367 		}
7368 
7369 		for (i++ ; i < mt_slot_count(mte); i++) {
7370 			if (gaps[i] != 0) {
7371 				pr_err("gap " PTR_FMT "[%u] beyond node limit != 0\n",
7372 				       node, i);
7373 				MT_BUG_ON(mas->tree, 1);
7374 			}
7375 		}
7376 	}
7377 
7378 	if (mte_is_root(mte))
7379 		return;
7380 
7381 	p_slot = mte_parent_slot(mas->node);
7382 	p_mn = mte_parent(mte);
7383 	MT_BUG_ON(mas->tree, max_gap > mas->max);
7384 	if (ma_gaps(p_mn, mas_parent_type(mas, mte))[p_slot] != max_gap) {
7385 		pr_err("gap " PTR_FMT "[%u] != %lu\n", p_mn, p_slot, max_gap);
7386 		mt_dump(mas->tree, mt_dump_hex);
7387 		MT_BUG_ON(mas->tree, 1);
7388 	}
7389 }
7390 
7391 static void mas_validate_parent_slot(struct ma_state *mas)
7392 {
7393 	struct maple_node *parent;
7394 	struct maple_enode *node;
7395 	enum maple_type p_type;
7396 	unsigned char p_slot;
7397 	void __rcu **slots;
7398 	int i;
7399 
7400 	if (mte_is_root(mas->node))
7401 		return;
7402 
7403 	p_slot = mte_parent_slot(mas->node);
7404 	p_type = mas_parent_type(mas, mas->node);
7405 	parent = mte_parent(mas->node);
7406 	slots = ma_slots(parent, p_type);
7407 	MT_BUG_ON(mas->tree, mas_mn(mas) == parent);
7408 
7409 	/* Check prev/next parent slot for duplicate node entry */
7410 
7411 	for (i = 0; i < mt_slots[p_type]; i++) {
7412 		node = mas_slot(mas, slots, i);
7413 		if (i == p_slot) {
7414 			if (node != mas->node)
7415 				pr_err("parent " PTR_FMT "[%u] does not have " PTR_FMT "\n",
7416 					parent, i, mas_mn(mas));
7417 			MT_BUG_ON(mas->tree, node != mas->node);
7418 		} else if (node == mas->node) {
7419 			pr_err("Invalid child " PTR_FMT " at parent " PTR_FMT "[%u] p_slot %u\n",
7420 			       mas_mn(mas), parent, i, p_slot);
7421 			MT_BUG_ON(mas->tree, node == mas->node);
7422 		}
7423 	}
7424 }
7425 
7426 static void mas_validate_child_slot(struct ma_state *mas)
7427 {
7428 	enum maple_type type = mte_node_type(mas->node);
7429 	void __rcu **slots = ma_slots(mte_to_node(mas->node), type);
7430 	unsigned long *pivots = ma_pivots(mte_to_node(mas->node), type);
7431 	struct maple_enode *child;
7432 	unsigned char i;
7433 
7434 	if (mte_is_leaf(mas->node))
7435 		return;
7436 
7437 	for (i = 0; i < mt_slots[type]; i++) {
7438 		child = mas_slot(mas, slots, i);
7439 
7440 		if (!child) {
7441 			pr_err("Non-leaf node lacks child at " PTR_FMT "[%u]\n",
7442 			       mas_mn(mas), i);
7443 			MT_BUG_ON(mas->tree, 1);
7444 		}
7445 
7446 		if (mte_parent_slot(child) != i) {
7447 			pr_err("Slot error at " PTR_FMT "[%u]: child " PTR_FMT " has pslot %u\n",
7448 			       mas_mn(mas), i, mte_to_node(child),
7449 			       mte_parent_slot(child));
7450 			MT_BUG_ON(mas->tree, 1);
7451 		}
7452 
7453 		if (mte_parent(child) != mte_to_node(mas->node)) {
7454 			pr_err("child " PTR_FMT " has parent " PTR_FMT " not " PTR_FMT "\n",
7455 			       mte_to_node(child), mte_parent(child),
7456 			       mte_to_node(mas->node));
7457 			MT_BUG_ON(mas->tree, 1);
7458 		}
7459 
7460 		if (i < mt_pivots[type] && pivots[i] == mas->max)
7461 			break;
7462 	}
7463 }
7464 
7465 /*
7466  * Validate all pivots are within mas->min and mas->max, check metadata ends
7467  * where the maximum ends and ensure there is no slots or pivots set outside of
7468  * the end of the data.
7469  */
7470 static void mas_validate_limits(struct ma_state *mas)
7471 {
7472 	int i;
7473 	unsigned long prev_piv = 0;
7474 	enum maple_type type = mte_node_type(mas->node);
7475 	void __rcu **slots = ma_slots(mte_to_node(mas->node), type);
7476 	unsigned long *pivots = ma_pivots(mas_mn(mas), type);
7477 
7478 	for (i = 0; i < mt_slots[type]; i++) {
7479 		unsigned long piv;
7480 
7481 		piv = mas_safe_pivot(mas, pivots, i, type);
7482 
7483 		if (!piv && (i != 0)) {
7484 			pr_err("Missing node limit pivot at " PTR_FMT "[%u]",
7485 			       mas_mn(mas), i);
7486 			MAS_WARN_ON(mas, 1);
7487 		}
7488 
7489 		if (prev_piv > piv) {
7490 			pr_err(PTR_FMT "[%u] piv %lu < prev_piv %lu\n",
7491 				mas_mn(mas), i, piv, prev_piv);
7492 			MAS_WARN_ON(mas, piv < prev_piv);
7493 		}
7494 
7495 		if (piv < mas->min) {
7496 			pr_err(PTR_FMT "[%u] %lu < %lu\n", mas_mn(mas), i,
7497 				piv, mas->min);
7498 			MAS_WARN_ON(mas, piv < mas->min);
7499 		}
7500 		if (piv > mas->max) {
7501 			pr_err(PTR_FMT "[%u] %lu > %lu\n", mas_mn(mas), i,
7502 				piv, mas->max);
7503 			MAS_WARN_ON(mas, piv > mas->max);
7504 		}
7505 		prev_piv = piv;
7506 		if (piv == mas->max)
7507 			break;
7508 	}
7509 
7510 	if (mas_data_end(mas) != i) {
7511 		pr_err("node" PTR_FMT ": data_end %u != the last slot offset %u\n",
7512 		       mas_mn(mas), mas_data_end(mas), i);
7513 		MT_BUG_ON(mas->tree, 1);
7514 	}
7515 
7516 	for (i += 1; i < mt_slots[type]; i++) {
7517 		void *entry = mas_slot(mas, slots, i);
7518 
7519 		if (entry && (i != mt_slots[type] - 1)) {
7520 			pr_err(PTR_FMT "[%u] should not have entry " PTR_FMT "\n",
7521 			       mas_mn(mas), i, entry);
7522 			MT_BUG_ON(mas->tree, entry != NULL);
7523 		}
7524 
7525 		if (i < mt_pivots[type]) {
7526 			unsigned long piv = pivots[i];
7527 
7528 			if (!piv)
7529 				continue;
7530 
7531 			pr_err(PTR_FMT "[%u] should not have piv %lu\n",
7532 			       mas_mn(mas), i, piv);
7533 			MAS_WARN_ON(mas, i < mt_pivots[type] - 1);
7534 		}
7535 	}
7536 }
7537 
7538 static void mt_validate_nulls(struct maple_tree *mt)
7539 {
7540 	void *entry, *last = (void *)1;
7541 	unsigned char offset = 0;
7542 	void __rcu **slots;
7543 	MA_STATE(mas, mt, 0, 0);
7544 
7545 	mas_start(&mas);
7546 	if (mas_is_none(&mas) || (mas_is_ptr(&mas)))
7547 		return;
7548 
7549 	while (!mte_is_leaf(mas.node))
7550 		mas_descend(&mas);
7551 
7552 	slots = ma_slots(mte_to_node(mas.node), mte_node_type(mas.node));
7553 	do {
7554 		entry = mas_slot(&mas, slots, offset);
7555 		if (!last && !entry) {
7556 			pr_err("Sequential nulls end at " PTR_FMT "[%u]\n",
7557 				mas_mn(&mas), offset);
7558 		}
7559 		MT_BUG_ON(mt, !last && !entry);
7560 		last = entry;
7561 		if (offset == mas_data_end(&mas)) {
7562 			mas_next_node(&mas, mas_mn(&mas), ULONG_MAX);
7563 			if (mas_is_overflow(&mas))
7564 				return;
7565 			offset = 0;
7566 			slots = ma_slots(mte_to_node(mas.node),
7567 					 mte_node_type(mas.node));
7568 		} else {
7569 			offset++;
7570 		}
7571 
7572 	} while (!mas_is_overflow(&mas));
7573 }
7574 
7575 /*
7576  * validate a maple tree by checking:
7577  * 1. The limits (pivots are within mas->min to mas->max)
7578  * 2. The gap is correctly set in the parents
7579  */
7580 void mt_validate(struct maple_tree *mt)
7581 	__must_hold(mas->tree->ma_lock)
7582 {
7583 	unsigned char end;
7584 
7585 	MA_STATE(mas, mt, 0, 0);
7586 	mas_start(&mas);
7587 	if (!mas_is_active(&mas))
7588 		return;
7589 
7590 	while (!mte_is_leaf(mas.node))
7591 		mas_descend(&mas);
7592 
7593 	while (!mas_is_overflow(&mas)) {
7594 		MAS_WARN_ON(&mas, mte_dead_node(mas.node));
7595 		end = mas_data_end(&mas);
7596 		if (MAS_WARN_ON(&mas, (end < mt_min_slot_count(mas.node)) &&
7597 				(!mte_is_root(mas.node)))) {
7598 			pr_err("Invalid size %u of " PTR_FMT "\n",
7599 			       end, mas_mn(&mas));
7600 		}
7601 
7602 		mas_validate_parent_slot(&mas);
7603 		mas_validate_limits(&mas);
7604 		mas_validate_child_slot(&mas);
7605 		if (mt_is_alloc(mt))
7606 			mas_validate_gaps(&mas);
7607 		mas_dfs_postorder(&mas, ULONG_MAX);
7608 	}
7609 	mt_validate_nulls(mt);
7610 }
7611 EXPORT_SYMBOL_GPL(mt_validate);
7612 
7613 void mas_dump(const struct ma_state *mas)
7614 {
7615 	pr_err("MAS: tree=" PTR_FMT " enode=" PTR_FMT " ",
7616 	       mas->tree, mas->node);
7617 	switch (mas->status) {
7618 	case ma_active:
7619 		pr_err("(ma_active)");
7620 		break;
7621 	case ma_none:
7622 		pr_err("(ma_none)");
7623 		break;
7624 	case ma_root:
7625 		pr_err("(ma_root)");
7626 		break;
7627 	case ma_start:
7628 		pr_err("(ma_start) ");
7629 		break;
7630 	case ma_pause:
7631 		pr_err("(ma_pause) ");
7632 		break;
7633 	case ma_overflow:
7634 		pr_err("(ma_overflow) ");
7635 		break;
7636 	case ma_underflow:
7637 		pr_err("(ma_underflow) ");
7638 		break;
7639 	case ma_error:
7640 		pr_err("(ma_error) ");
7641 		break;
7642 	}
7643 
7644 	pr_err("Store Type: ");
7645 	switch (mas->store_type) {
7646 	case wr_invalid:
7647 		pr_err("invalid store type\n");
7648 		break;
7649 	case wr_new_root:
7650 		pr_err("new_root\n");
7651 		break;
7652 	case wr_store_root:
7653 		pr_err("store_root\n");
7654 		break;
7655 	case wr_exact_fit:
7656 		pr_err("exact_fit\n");
7657 		break;
7658 	case wr_split_store:
7659 		pr_err("split_store\n");
7660 		break;
7661 	case wr_slot_store:
7662 		pr_err("slot_store\n");
7663 		break;
7664 	case wr_append:
7665 		pr_err("append\n");
7666 		break;
7667 	case wr_node_store:
7668 		pr_err("node_store\n");
7669 		break;
7670 	case wr_spanning_store:
7671 		pr_err("spanning_store\n");
7672 		break;
7673 	case wr_rebalance:
7674 		pr_err("rebalance\n");
7675 		break;
7676 	}
7677 
7678 	pr_err("[%u/%u] index=%lx last=%lx\n", mas->offset, mas->end,
7679 	       mas->index, mas->last);
7680 	pr_err("     min=%lx max=%lx alloc=" PTR_FMT ", depth=%u, flags=%x\n",
7681 	       mas->min, mas->max, mas->alloc, mas->depth, mas->mas_flags);
7682 	if (mas->index > mas->last)
7683 		pr_err("Check index & last\n");
7684 }
7685 EXPORT_SYMBOL_GPL(mas_dump);
7686 
7687 void mas_wr_dump(const struct ma_wr_state *wr_mas)
7688 {
7689 	pr_err("WR_MAS: node=" PTR_FMT " r_min=%lx r_max=%lx\n",
7690 	       wr_mas->node, wr_mas->r_min, wr_mas->r_max);
7691 	pr_err("        type=%u off_end=%u, node_end=%u, end_piv=%lx\n",
7692 	       wr_mas->type, wr_mas->offset_end, wr_mas->mas->end,
7693 	       wr_mas->end_piv);
7694 }
7695 EXPORT_SYMBOL_GPL(mas_wr_dump);
7696 
7697 #endif /* CONFIG_DEBUG_MAPLE_TREE */
7698