xref: /linux/lib/maple_tree.c (revision c06944560a562828d507166b4f87c01c367cc9c1)
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 */
mt_alloc_one(gfp_t gfp)174 static inline struct maple_node *mt_alloc_one(gfp_t gfp)
175 {
176 	return kmem_cache_alloc(maple_node_cache, gfp);
177 }
178 
mt_alloc_bulk(gfp_t gfp,size_t size,void ** nodes)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 
mt_free_one(struct maple_node * node)184 static inline void mt_free_one(struct maple_node *node)
185 {
186 	kmem_cache_free(maple_node_cache, node);
187 }
188 
mt_free_bulk(size_t size,void __rcu ** nodes)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 
mt_free_rcu(struct rcu_head * head)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  */
ma_free_rcu(struct maple_node * node)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 
mt_set_height(struct maple_tree * mt,unsigned char height)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 
mas_mt_height(struct ma_state * mas)224 static unsigned int mas_mt_height(struct ma_state *mas)
225 {
226 	return mt_height(mas->tree);
227 }
228 
mt_attr(struct maple_tree * mt)229 static inline unsigned int mt_attr(struct maple_tree *mt)
230 {
231 	return mt->ma_flags & ~MT_FLAGS_HEIGHT_MASK;
232 }
233 
mte_node_type(const struct maple_enode * entry)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 
ma_is_dense(const enum maple_type type)241 static __always_inline bool ma_is_dense(const enum maple_type type)
242 {
243 	return type < maple_leaf_64;
244 }
245 
ma_is_leaf(const enum maple_type type)246 static __always_inline bool ma_is_leaf(const enum maple_type type)
247 {
248 	return type < maple_range_64;
249 }
250 
mte_is_leaf(const struct maple_enode * entry)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  */
mt_is_reserved(const void * entry)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 
mas_set_err(struct ma_state * mas,long err)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 
mas_is_ptr(const struct ma_state * mas)272 static __always_inline bool mas_is_ptr(const struct ma_state *mas)
273 {
274 	return mas->status == ma_root;
275 }
276 
mas_is_start(const struct ma_state * mas)277 static __always_inline bool mas_is_start(const struct ma_state *mas)
278 {
279 	return mas->status == ma_start;
280 }
281 
mas_is_none(const struct ma_state * mas)282 static __always_inline bool mas_is_none(const struct ma_state *mas)
283 {
284 	return mas->status == ma_none;
285 }
286 
mas_is_paused(const struct ma_state * mas)287 static __always_inline bool mas_is_paused(const struct ma_state *mas)
288 {
289 	return mas->status == ma_pause;
290 }
291 
mas_is_overflow(struct ma_state * mas)292 static __always_inline bool mas_is_overflow(struct ma_state *mas)
293 {
294 	return mas->status == ma_overflow;
295 }
296 
mas_is_underflow(struct ma_state * mas)297 static inline bool mas_is_underflow(struct ma_state *mas)
298 {
299 	return mas->status == ma_underflow;
300 }
301 
mte_to_node(const struct maple_enode * entry)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  */
mte_to_mat(const struct maple_enode * entry)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  */
mas_mn(const struct ma_state * mas)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  */
mte_set_node_dead(struct maple_enode * mn)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 
mt_mk_node(const struct maple_node * node,enum maple_type type)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 
mte_mk_root(const struct maple_enode * node)355 static inline void *mte_mk_root(const struct maple_enode *node)
356 {
357 	return (void *)((unsigned long)node | MAPLE_ROOT_NODE);
358 }
359 
mte_safe_root(const struct maple_enode * node)360 static inline void *mte_safe_root(const struct maple_enode *node)
361 {
362 	return (void *)((unsigned long)node & ~MAPLE_ROOT_NODE);
363 }
364 
mte_set_full(const struct maple_enode * node)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 
mte_clear_full(const struct maple_enode * node)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 
mte_has_null(const struct maple_enode * node)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 
ma_is_root(struct maple_node * node)380 static __always_inline bool ma_is_root(struct maple_node *node)
381 {
382 	return ((unsigned long)node->parent & MA_ROOT_PARENT);
383 }
384 
mte_is_root(const struct maple_enode * node)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 
mas_is_root_limits(const struct ma_state * mas)390 static inline bool mas_is_root_limits(const struct ma_state *mas)
391 {
392 	return !mas->min && mas->max == ULONG_MAX;
393 }
394 
mt_is_alloc(struct maple_tree * mt)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  */
mte_parent_shift(unsigned long parent)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  */
mte_parent_slot_mask(unsigned long parent)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
mas_parent_type(struct ma_state * mas,struct maple_enode * enode)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
mas_set_parent(struct ma_state * mas,struct maple_enode * enode,const struct maple_enode * parent,unsigned char slot)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
mte_parent_slot(const struct maple_enode * enode)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
mte_parent(const struct maple_enode * enode)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  */
ma_dead_node(const struct maple_node * node)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  */
mte_dead_node(const struct maple_enode * enode)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  */
mas_allocated(const struct ma_state * mas)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  */
mas_set_alloc_req(struct ma_state * mas,unsigned long count)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  */
mas_alloc_req(const struct ma_state * mas)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  */
ma_pivots(struct maple_node * node,enum maple_type type)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  */
ma_gaps(struct maple_node * node,enum maple_type type)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
mas_safe_pivot(const struct ma_state * mas,unsigned long * pivots,unsigned char piv,enum maple_type type)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
mas_safe_min(struct ma_state * mas,unsigned long * pivots,unsigned char offset)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  */
mte_set_pivot(struct maple_enode * mn,unsigned char piv,unsigned long val)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  */
ma_slots(struct maple_node * mn,enum maple_type mt)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 
mt_write_locked(const struct maple_tree * mt)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 
mt_locked(const struct maple_tree * mt)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 
mt_slot(const struct maple_tree * mt,void __rcu ** slots,unsigned char offset)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 
mt_slot_locked(struct maple_tree * mt,void __rcu ** slots,unsigned char offset)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  */
mas_slot_locked(struct ma_state * mas,void __rcu ** slots,unsigned char offset)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  */
mas_slot(struct ma_state * mas,void __rcu ** slots,unsigned char offset)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  */
mas_root(struct ma_state * mas)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 
mt_root_locked(struct maple_tree * mt)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  */
mas_root_locked(struct ma_state * mas)858 static inline void *mas_root_locked(struct ma_state *mas)
859 {
860 	return mt_root_locked(mas->tree);
861 }
862 
ma_meta(struct maple_node * mn,enum maple_type mt)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  */
ma_set_meta(struct maple_node * mn,enum maple_type mt,unsigned char offset,unsigned char end)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  */
mt_clear_meta(struct maple_tree * mt,struct maple_node * mn,enum maple_type type)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  */
ma_meta_end(struct maple_node * mn,enum maple_type mt)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  */
ma_meta_gap(struct maple_node * mn)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  */
ma_set_meta_gap(struct maple_node * mn,enum maple_type mt,unsigned char offset)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  */
mat_add(struct ma_topiary * mat,struct maple_enode * dead_enode)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  */
mas_mat_destroy(struct ma_state * mas,struct ma_topiary * mat)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  */
mas_descend(struct ma_state * mas)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  */
mte_set_gap(const struct maple_enode * mn,unsigned char gap,unsigned long val)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  */
mas_ascend(struct ma_state * mas)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  */
mas_pop_node(struct ma_state * mas)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  */
mas_push_node(struct ma_state * mas,struct maple_node * used)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  */
mas_alloc_nodes(struct ma_state * mas,gfp_t gfp)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  */
mas_free(struct ma_state * mas,struct maple_enode * used)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  */
mas_node_count_gfp(struct ma_state * mas,int count,gfp_t gfp)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  */
mas_node_count(struct ma_state * mas,int count)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  */
mas_start(struct ma_state * mas)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  */
ma_data_end(struct maple_node * node,enum maple_type type,unsigned long * pivots,unsigned long max)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  */
mas_data_end(struct ma_state * mas)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  */
mas_leaf_max_gap(struct ma_state * mas)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
ma_max_gap(struct maple_node * node,unsigned long * gaps,enum maple_type mt,unsigned char * off)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  */
mas_max_gap(struct ma_state * mas)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  */
mas_parent_gap(struct ma_state * mas,unsigned char offset,unsigned long new)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  */
mas_update_gap(struct ma_state * mas)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  */
mas_adopt_children(struct ma_state * mas,struct maple_enode * parent)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  */
mas_put_in_tree(struct ma_state * mas,struct maple_enode * old_enode,char new_height)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  */
mas_replace_node(struct ma_state * mas,struct maple_enode * old_enode,unsigned char new_height)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  */
mas_find_child(struct ma_state * mas,struct ma_state * child)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  */
mab_shift_right(struct maple_big_node * b_node,unsigned char shift)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  */
mab_middle_node(struct maple_big_node * b_node,int split,unsigned char slot_count)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  */
mab_no_null_split(struct maple_big_node * b_node,unsigned char split,unsigned char slot_count)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  */
mab_calc_split(struct ma_state * mas,struct maple_big_node * bn,unsigned char * mid_split)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  */
mas_mab_cp(struct ma_state * mas,unsigned char mas_start,unsigned char mas_end,struct maple_big_node * b_node,unsigned char mab_start)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  */
mas_leaf_set_meta(struct maple_node * node,enum maple_type mt,unsigned char end)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  */
mab_mas_cp(struct maple_big_node * b_node,unsigned char mab_start,unsigned char mab_end,struct ma_state * mas,bool new_max)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  */
mas_bulk_rebalance(struct ma_state * mas,unsigned char end,enum maple_type mt)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  */
mas_store_b_node(struct ma_wr_state * wr_mas,struct maple_big_node * b_node,unsigned char offset_end)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  */
mas_prev_sibling(struct ma_state * mas)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  */
mas_next_sibling(struct ma_state * mas)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  */
mas_node_or_none(struct ma_state * mas,struct maple_enode * enode)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  */
mas_wr_node_walk(struct ma_wr_state * wr_mas)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  */
mast_rebalance_next(struct maple_subtree_state * mast)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  */
mast_rebalance_prev(struct maple_subtree_state * mast)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
mast_spanning_rebalance(struct maple_subtree_state * mast)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  */
mast_ascend(struct maple_subtree_state * mast)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
mas_new_ma_node(struct ma_state * mas,struct maple_big_node * b_node)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  */
mas_mab_to_node(struct ma_state * mas,struct maple_big_node * b_node,struct maple_enode ** left,struct maple_enode ** right,struct maple_enode ** middle,unsigned char * mid_split)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  */
mab_set_b_end(struct maple_big_node * b_node,struct ma_state * mas,void * entry)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  */
mas_set_split_parent(struct ma_state * mas,struct maple_enode * left,struct maple_enode * right,unsigned char * slot,unsigned char split)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  */
mte_mid_split_check(struct maple_enode ** l,struct maple_enode ** r,struct maple_enode * right,unsigned char slot,unsigned char * split,unsigned char mid_split)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  */
mast_set_split_parents(struct maple_subtree_state * mast,struct maple_enode * left,struct maple_enode * middle,struct maple_enode * right,unsigned char split,unsigned char mid_split)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  */
mas_topiary_node(struct ma_state * mas,struct ma_state * tmp_mas,bool in_rcu)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  */
mas_topiary_replace(struct ma_state * mas,struct maple_enode * old_enode,unsigned char new_height)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  */
mas_wmb_replace(struct ma_state * mas,struct maple_enode * old_enode,unsigned char new_height)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  */
mast_cp_to_nodes(struct maple_subtree_state * mast,struct maple_enode * left,struct maple_enode * middle,struct maple_enode * right,unsigned char split,unsigned char mid_split)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  */
mast_combine_cp_left(struct maple_subtree_state * mast)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  */
mast_combine_cp_right(struct maple_subtree_state * mast)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  */
mast_sufficient(struct maple_subtree_state * mast)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  */
mast_overflow(struct maple_subtree_state * mast)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 
mtree_range_walk(struct ma_state * mas)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  */
mas_spanning_rebalance(struct ma_state * mas,struct maple_subtree_state * mast,unsigned char count)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  */
mas_rebalance(struct ma_state * mas,struct maple_big_node * b_node)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  */
mas_destroy_rebalance(struct ma_state * mas,unsigned char end)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  */
mas_split_final_node(struct maple_subtree_state * mast,struct ma_state * mas)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  */
mast_fill_bnode(struct maple_subtree_state * mast,struct ma_state * mas,unsigned char skip)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  */
mast_split_data(struct maple_subtree_state * mast,struct ma_state * mas,unsigned char split)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  */
mas_push_data(struct ma_state * mas,struct maple_subtree_state * mast,bool left)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  */
mas_split(struct ma_state * mas,struct maple_big_node * b_node)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  */
mas_commit_b_node(struct ma_wr_state * wr_mas,struct maple_big_node * b_node)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  */
mas_root_expand(struct ma_state * mas,void * entry)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  */
mas_store_root(struct ma_state * mas,void * entry)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  */
mas_is_span_wr(struct ma_wr_state * wr_mas)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 
mas_wr_walk_descend(struct ma_wr_state * wr_mas)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 
mas_wr_walk_traverse(struct ma_wr_state * wr_mas)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  */
mas_wr_walk(struct ma_wr_state * wr_mas)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 
mas_wr_walk_index(struct ma_wr_state * wr_mas)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  */
mas_extend_spanning_null(struct ma_wr_state * l_wr_mas,struct ma_wr_state * r_wr_mas)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 
mas_state_walk(struct ma_state * mas)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  */
mtree_lookup_walk(struct ma_state * mas)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  */
mas_new_root(struct ma_state * mas,void * entry)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  */
mas_wr_spanning_store(struct ma_wr_state * wr_mas)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  */
mas_wr_node_store(struct ma_wr_state * wr_mas,unsigned char new_end)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  */
mas_wr_slot_store(struct ma_wr_state * wr_mas)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 
mas_wr_extend_null(struct ma_wr_state * wr_mas)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 
mas_wr_end_piv(struct ma_wr_state * wr_mas)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 
mas_wr_new_end(struct ma_wr_state * wr_mas)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  */
mas_wr_append(struct ma_wr_state * wr_mas,unsigned char new_end)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  */
mas_wr_bnode(struct ma_wr_state * wr_mas)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  */
mas_wr_store_entry(struct ma_wr_state * wr_mas)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 
mas_wr_prealloc_setup(struct ma_wr_state * wr_mas)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  */
mas_prealloc_calc(struct ma_wr_state * wr_mas,void * entry)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  */
mas_wr_store_type(struct ma_wr_state * wr_mas)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  */
mas_wr_preallocate(struct ma_wr_state * wr_mas,void * entry)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  */
mas_insert(struct ma_state * mas,void * entry)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  */
mas_alloc_cyclic(struct ma_state * mas,unsigned long * startp,void * entry,unsigned long range_lo,unsigned long range_hi,unsigned long * next,gfp_t gfp)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 
mas_rewalk(struct ma_state * mas,unsigned long index)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 
mas_rewalk_if_dead(struct ma_state * mas,struct maple_node * node,const unsigned long index)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  */
mas_prev_node(struct ma_state * mas,unsigned long min)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  */
mas_prev_slot(struct ma_state * mas,unsigned long min,bool empty)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 
4564 	if (likely(entry))
4565 		return entry;
4566 
4567 	if (!empty) {
4568 		if (mas->index <= min) {
4569 			mas->status = ma_underflow;
4570 			return NULL;
4571 		}
4572 
4573 		goto again;
4574 	}
4575 
4576 	return entry;
4577 
4578 underflow:
4579 	mas->status = ma_underflow;
4580 	return NULL;
4581 }
4582 
4583 /*
4584  * mas_next_node() - Get the next node at the same level in the tree.
4585  * @mas: The maple state
4586  * @node: The maple node
4587  * @max: The maximum pivot value to check.
4588  *
4589  * The next value will be mas->node[mas->offset] or the status will have
4590  * overflowed.
4591  * Return: 1 on dead node, 0 otherwise.
4592  */
mas_next_node(struct ma_state * mas,struct maple_node * node,unsigned long max)4593 static int mas_next_node(struct ma_state *mas, struct maple_node *node,
4594 		unsigned long max)
4595 {
4596 	unsigned long min;
4597 	unsigned long *pivots;
4598 	struct maple_enode *enode;
4599 	struct maple_node *tmp;
4600 	int level = 0;
4601 	unsigned char node_end;
4602 	enum maple_type mt;
4603 	void __rcu **slots;
4604 
4605 	if (mas->max >= max)
4606 		goto overflow;
4607 
4608 	min = mas->max + 1;
4609 	level = 0;
4610 	do {
4611 		if (ma_is_root(node))
4612 			goto overflow;
4613 
4614 		/* Walk up. */
4615 		if (unlikely(mas_ascend(mas)))
4616 			return 1;
4617 
4618 		level++;
4619 		node = mas_mn(mas);
4620 		mt = mte_node_type(mas->node);
4621 		pivots = ma_pivots(node, mt);
4622 		node_end = ma_data_end(node, mt, pivots, mas->max);
4623 		if (unlikely(ma_dead_node(node)))
4624 			return 1;
4625 
4626 	} while (unlikely(mas->offset == node_end));
4627 
4628 	slots = ma_slots(node, mt);
4629 	mas->offset++;
4630 	enode = mas_slot(mas, slots, mas->offset);
4631 	if (unlikely(ma_dead_node(node)))
4632 		return 1;
4633 
4634 	if (level > 1)
4635 		mas->offset = 0;
4636 
4637 	while (unlikely(level > 1)) {
4638 		level--;
4639 		mas->node = enode;
4640 		node = mas_mn(mas);
4641 		mt = mte_node_type(mas->node);
4642 		slots = ma_slots(node, mt);
4643 		enode = mas_slot(mas, slots, 0);
4644 		if (unlikely(ma_dead_node(node)))
4645 			return 1;
4646 	}
4647 
4648 	if (!mas->offset)
4649 		pivots = ma_pivots(node, mt);
4650 
4651 	mas->max = mas_safe_pivot(mas, pivots, mas->offset, mt);
4652 	tmp = mte_to_node(enode);
4653 	mt = mte_node_type(enode);
4654 	pivots = ma_pivots(tmp, mt);
4655 	mas->end = ma_data_end(tmp, mt, pivots, mas->max);
4656 	if (unlikely(ma_dead_node(node)))
4657 		return 1;
4658 
4659 	mas->node = enode;
4660 	mas->min = min;
4661 	return 0;
4662 
4663 overflow:
4664 	if (unlikely(ma_dead_node(node)))
4665 		return 1;
4666 
4667 	mas->status = ma_overflow;
4668 	return 0;
4669 }
4670 
4671 /*
4672  * mas_next_slot() - Get the entry in the next slot
4673  *
4674  * @mas: The maple state
4675  * @max: The maximum starting range
4676  * @empty: Can be empty
4677  *
4678  * Return: The entry in the next slot which is possibly NULL
4679  */
mas_next_slot(struct ma_state * mas,unsigned long max,bool empty)4680 static void *mas_next_slot(struct ma_state *mas, unsigned long max, bool empty)
4681 {
4682 	void __rcu **slots;
4683 	unsigned long *pivots;
4684 	unsigned long pivot;
4685 	enum maple_type type;
4686 	struct maple_node *node;
4687 	unsigned long save_point = mas->last;
4688 	void *entry;
4689 
4690 retry:
4691 	node = mas_mn(mas);
4692 	type = mte_node_type(mas->node);
4693 	pivots = ma_pivots(node, type);
4694 	if (unlikely(mas_rewalk_if_dead(mas, node, save_point)))
4695 		goto retry;
4696 
4697 	if (mas->max >= max) {
4698 		if (likely(mas->offset < mas->end))
4699 			pivot = pivots[mas->offset];
4700 		else
4701 			pivot = mas->max;
4702 
4703 		if (unlikely(mas_rewalk_if_dead(mas, node, save_point)))
4704 			goto retry;
4705 
4706 		if (pivot >= max) { /* Was at the limit, next will extend beyond */
4707 			mas->status = ma_overflow;
4708 			return NULL;
4709 		}
4710 	}
4711 
4712 	if (likely(mas->offset < mas->end)) {
4713 		mas->index = pivots[mas->offset] + 1;
4714 again:
4715 		mas->offset++;
4716 		if (likely(mas->offset < mas->end))
4717 			mas->last = pivots[mas->offset];
4718 		else
4719 			mas->last = mas->max;
4720 	} else  {
4721 		if (mas->last >= max) {
4722 			mas->status = ma_overflow;
4723 			return NULL;
4724 		}
4725 
4726 		if (mas_next_node(mas, node, max)) {
4727 			mas_rewalk(mas, save_point);
4728 			goto retry;
4729 		}
4730 
4731 		if (WARN_ON_ONCE(mas_is_overflow(mas)))
4732 			return NULL;
4733 
4734 		mas->offset = 0;
4735 		mas->index = mas->min;
4736 		node = mas_mn(mas);
4737 		type = mte_node_type(mas->node);
4738 		pivots = ma_pivots(node, type);
4739 		mas->last = pivots[0];
4740 	}
4741 
4742 	slots = ma_slots(node, type);
4743 	entry = mt_slot(mas->tree, slots, mas->offset);
4744 	if (unlikely(mas_rewalk_if_dead(mas, node, save_point)))
4745 		goto retry;
4746 
4747 	if (entry)
4748 		return entry;
4749 
4750 
4751 	if (!empty) {
4752 		if (mas->last >= max) {
4753 			mas->status = ma_overflow;
4754 			return NULL;
4755 		}
4756 
4757 		mas->index = mas->last + 1;
4758 		goto again;
4759 	}
4760 
4761 	return entry;
4762 }
4763 
4764 /*
4765  * mas_rev_awalk() - Internal function.  Reverse allocation walk.  Find the
4766  * highest gap address of a given size in a given node and descend.
4767  * @mas: The maple state
4768  * @size: The needed size.
4769  *
4770  * Return: True if found in a leaf, false otherwise.
4771  *
4772  */
mas_rev_awalk(struct ma_state * mas,unsigned long size,unsigned long * gap_min,unsigned long * gap_max)4773 static bool mas_rev_awalk(struct ma_state *mas, unsigned long size,
4774 		unsigned long *gap_min, unsigned long *gap_max)
4775 {
4776 	enum maple_type type = mte_node_type(mas->node);
4777 	struct maple_node *node = mas_mn(mas);
4778 	unsigned long *pivots, *gaps;
4779 	void __rcu **slots;
4780 	unsigned long gap = 0;
4781 	unsigned long max, min;
4782 	unsigned char offset;
4783 
4784 	if (unlikely(mas_is_err(mas)))
4785 		return true;
4786 
4787 	if (ma_is_dense(type)) {
4788 		/* dense nodes. */
4789 		mas->offset = (unsigned char)(mas->index - mas->min);
4790 		return true;
4791 	}
4792 
4793 	pivots = ma_pivots(node, type);
4794 	slots = ma_slots(node, type);
4795 	gaps = ma_gaps(node, type);
4796 	offset = mas->offset;
4797 	min = mas_safe_min(mas, pivots, offset);
4798 	/* Skip out of bounds. */
4799 	while (mas->last < min)
4800 		min = mas_safe_min(mas, pivots, --offset);
4801 
4802 	max = mas_safe_pivot(mas, pivots, offset, type);
4803 	while (mas->index <= max) {
4804 		gap = 0;
4805 		if (gaps)
4806 			gap = gaps[offset];
4807 		else if (!mas_slot(mas, slots, offset))
4808 			gap = max - min + 1;
4809 
4810 		if (gap) {
4811 			if ((size <= gap) && (size <= mas->last - min + 1))
4812 				break;
4813 
4814 			if (!gaps) {
4815 				/* Skip the next slot, it cannot be a gap. */
4816 				if (offset < 2)
4817 					goto ascend;
4818 
4819 				offset -= 2;
4820 				max = pivots[offset];
4821 				min = mas_safe_min(mas, pivots, offset);
4822 				continue;
4823 			}
4824 		}
4825 
4826 		if (!offset)
4827 			goto ascend;
4828 
4829 		offset--;
4830 		max = min - 1;
4831 		min = mas_safe_min(mas, pivots, offset);
4832 	}
4833 
4834 	if (unlikely((mas->index > max) || (size - 1 > max - mas->index)))
4835 		goto no_space;
4836 
4837 	if (unlikely(ma_is_leaf(type))) {
4838 		mas->offset = offset;
4839 		*gap_min = min;
4840 		*gap_max = min + gap - 1;
4841 		return true;
4842 	}
4843 
4844 	/* descend, only happens under lock. */
4845 	mas->node = mas_slot(mas, slots, offset);
4846 	mas->min = min;
4847 	mas->max = max;
4848 	mas->offset = mas_data_end(mas);
4849 	return false;
4850 
4851 ascend:
4852 	if (!mte_is_root(mas->node))
4853 		return false;
4854 
4855 no_space:
4856 	mas_set_err(mas, -EBUSY);
4857 	return false;
4858 }
4859 
mas_anode_descend(struct ma_state * mas,unsigned long size)4860 static inline bool mas_anode_descend(struct ma_state *mas, unsigned long size)
4861 {
4862 	enum maple_type type = mte_node_type(mas->node);
4863 	unsigned long pivot, min, gap = 0;
4864 	unsigned char offset, data_end;
4865 	unsigned long *gaps, *pivots;
4866 	void __rcu **slots;
4867 	struct maple_node *node;
4868 	bool found = false;
4869 
4870 	if (ma_is_dense(type)) {
4871 		mas->offset = (unsigned char)(mas->index - mas->min);
4872 		return true;
4873 	}
4874 
4875 	node = mas_mn(mas);
4876 	pivots = ma_pivots(node, type);
4877 	slots = ma_slots(node, type);
4878 	gaps = ma_gaps(node, type);
4879 	offset = mas->offset;
4880 	min = mas_safe_min(mas, pivots, offset);
4881 	data_end = ma_data_end(node, type, pivots, mas->max);
4882 	for (; offset <= data_end; offset++) {
4883 		pivot = mas_safe_pivot(mas, pivots, offset, type);
4884 
4885 		/* Not within lower bounds */
4886 		if (mas->index > pivot)
4887 			goto next_slot;
4888 
4889 		if (gaps)
4890 			gap = gaps[offset];
4891 		else if (!mas_slot(mas, slots, offset))
4892 			gap = min(pivot, mas->last) - max(mas->index, min) + 1;
4893 		else
4894 			goto next_slot;
4895 
4896 		if (gap >= size) {
4897 			if (ma_is_leaf(type)) {
4898 				found = true;
4899 				break;
4900 			}
4901 
4902 			mas->node = mas_slot(mas, slots, offset);
4903 			mas->min = min;
4904 			mas->max = pivot;
4905 			offset = 0;
4906 			break;
4907 		}
4908 next_slot:
4909 		min = pivot + 1;
4910 		if (mas->last <= pivot) {
4911 			mas_set_err(mas, -EBUSY);
4912 			return true;
4913 		}
4914 	}
4915 
4916 	mas->offset = offset;
4917 	return found;
4918 }
4919 
4920 /**
4921  * mas_walk() - Search for @mas->index in the tree.
4922  * @mas: The maple state.
4923  *
4924  * mas->index and mas->last will be set to the range if there is a value.  If
4925  * mas->status is ma_none, reset to ma_start
4926  *
4927  * Return: the entry at the location or %NULL.
4928  */
mas_walk(struct ma_state * mas)4929 void *mas_walk(struct ma_state *mas)
4930 {
4931 	void *entry;
4932 
4933 	if (!mas_is_active(mas) || !mas_is_start(mas))
4934 		mas->status = ma_start;
4935 retry:
4936 	entry = mas_state_walk(mas);
4937 	if (mas_is_start(mas)) {
4938 		goto retry;
4939 	} else if (mas_is_none(mas)) {
4940 		mas->index = 0;
4941 		mas->last = ULONG_MAX;
4942 	} else if (mas_is_ptr(mas)) {
4943 		if (!mas->index) {
4944 			mas->last = 0;
4945 			return entry;
4946 		}
4947 
4948 		mas->index = 1;
4949 		mas->last = ULONG_MAX;
4950 		mas->status = ma_none;
4951 		return NULL;
4952 	}
4953 
4954 	return entry;
4955 }
4956 EXPORT_SYMBOL_GPL(mas_walk);
4957 
mas_rewind_node(struct ma_state * mas)4958 static inline bool mas_rewind_node(struct ma_state *mas)
4959 {
4960 	unsigned char slot;
4961 
4962 	do {
4963 		if (mte_is_root(mas->node)) {
4964 			slot = mas->offset;
4965 			if (!slot)
4966 				return false;
4967 		} else {
4968 			mas_ascend(mas);
4969 			slot = mas->offset;
4970 		}
4971 	} while (!slot);
4972 
4973 	mas->offset = --slot;
4974 	return true;
4975 }
4976 
4977 /*
4978  * mas_skip_node() - Internal function.  Skip over a node.
4979  * @mas: The maple state.
4980  *
4981  * Return: true if there is another node, false otherwise.
4982  */
mas_skip_node(struct ma_state * mas)4983 static inline bool mas_skip_node(struct ma_state *mas)
4984 {
4985 	if (mas_is_err(mas))
4986 		return false;
4987 
4988 	do {
4989 		if (mte_is_root(mas->node)) {
4990 			if (mas->offset >= mas_data_end(mas)) {
4991 				mas_set_err(mas, -EBUSY);
4992 				return false;
4993 			}
4994 		} else {
4995 			mas_ascend(mas);
4996 		}
4997 	} while (mas->offset >= mas_data_end(mas));
4998 
4999 	mas->offset++;
5000 	return true;
5001 }
5002 
5003 /*
5004  * mas_awalk() - Allocation walk.  Search from low address to high, for a gap of
5005  * @size
5006  * @mas: The maple state
5007  * @size: The size of the gap required
5008  *
5009  * Search between @mas->index and @mas->last for a gap of @size.
5010  */
mas_awalk(struct ma_state * mas,unsigned long size)5011 static inline void mas_awalk(struct ma_state *mas, unsigned long size)
5012 {
5013 	struct maple_enode *last = NULL;
5014 
5015 	/*
5016 	 * There are 4 options:
5017 	 * go to child (descend)
5018 	 * go back to parent (ascend)
5019 	 * no gap found. (return, error == -EBUSY)
5020 	 * found the gap. (return)
5021 	 */
5022 	while (!mas_is_err(mas) && !mas_anode_descend(mas, size)) {
5023 		if (last == mas->node)
5024 			mas_skip_node(mas);
5025 		else
5026 			last = mas->node;
5027 	}
5028 }
5029 
5030 /*
5031  * mas_sparse_area() - Internal function.  Return upper or lower limit when
5032  * searching for a gap in an empty tree.
5033  * @mas: The maple state
5034  * @min: the minimum range
5035  * @max: The maximum range
5036  * @size: The size of the gap
5037  * @fwd: Searching forward or back
5038  */
mas_sparse_area(struct ma_state * mas,unsigned long min,unsigned long max,unsigned long size,bool fwd)5039 static inline int mas_sparse_area(struct ma_state *mas, unsigned long min,
5040 				unsigned long max, unsigned long size, bool fwd)
5041 {
5042 	if (!unlikely(mas_is_none(mas)) && min == 0) {
5043 		min++;
5044 		/*
5045 		 * At this time, min is increased, we need to recheck whether
5046 		 * the size is satisfied.
5047 		 */
5048 		if (min > max || max - min + 1 < size)
5049 			return -EBUSY;
5050 	}
5051 	/* mas_is_ptr */
5052 
5053 	if (fwd) {
5054 		mas->index = min;
5055 		mas->last = min + size - 1;
5056 	} else {
5057 		mas->last = max;
5058 		mas->index = max - size + 1;
5059 	}
5060 	return 0;
5061 }
5062 
5063 /*
5064  * mas_empty_area() - Get the lowest address within the range that is
5065  * sufficient for the size requested.
5066  * @mas: The maple state
5067  * @min: The lowest value of the range
5068  * @max: The highest value of the range
5069  * @size: The size needed
5070  */
mas_empty_area(struct ma_state * mas,unsigned long min,unsigned long max,unsigned long size)5071 int mas_empty_area(struct ma_state *mas, unsigned long min,
5072 		unsigned long max, unsigned long size)
5073 {
5074 	unsigned char offset;
5075 	unsigned long *pivots;
5076 	enum maple_type mt;
5077 	struct maple_node *node;
5078 
5079 	if (min > max)
5080 		return -EINVAL;
5081 
5082 	if (size == 0 || max - min < size - 1)
5083 		return -EINVAL;
5084 
5085 	if (mas_is_start(mas))
5086 		mas_start(mas);
5087 	else if (mas->offset >= 2)
5088 		mas->offset -= 2;
5089 	else if (!mas_skip_node(mas))
5090 		return -EBUSY;
5091 
5092 	/* Empty set */
5093 	if (mas_is_none(mas) || mas_is_ptr(mas))
5094 		return mas_sparse_area(mas, min, max, size, true);
5095 
5096 	/* The start of the window can only be within these values */
5097 	mas->index = min;
5098 	mas->last = max;
5099 	mas_awalk(mas, size);
5100 
5101 	if (unlikely(mas_is_err(mas)))
5102 		return xa_err(mas->node);
5103 
5104 	offset = mas->offset;
5105 	node = mas_mn(mas);
5106 	mt = mte_node_type(mas->node);
5107 	pivots = ma_pivots(node, mt);
5108 	min = mas_safe_min(mas, pivots, offset);
5109 	if (mas->index < min)
5110 		mas->index = min;
5111 	mas->last = mas->index + size - 1;
5112 	mas->end = ma_data_end(node, mt, pivots, mas->max);
5113 	return 0;
5114 }
5115 EXPORT_SYMBOL_GPL(mas_empty_area);
5116 
5117 /*
5118  * mas_empty_area_rev() - Get the highest address within the range that is
5119  * sufficient for the size requested.
5120  * @mas: The maple state
5121  * @min: The lowest value of the range
5122  * @max: The highest value of the range
5123  * @size: The size needed
5124  */
mas_empty_area_rev(struct ma_state * mas,unsigned long min,unsigned long max,unsigned long size)5125 int mas_empty_area_rev(struct ma_state *mas, unsigned long min,
5126 		unsigned long max, unsigned long size)
5127 {
5128 	struct maple_enode *last = mas->node;
5129 
5130 	if (min > max)
5131 		return -EINVAL;
5132 
5133 	if (size == 0 || max - min < size - 1)
5134 		return -EINVAL;
5135 
5136 	if (mas_is_start(mas))
5137 		mas_start(mas);
5138 	else if ((mas->offset < 2) && (!mas_rewind_node(mas)))
5139 		return -EBUSY;
5140 
5141 	if (unlikely(mas_is_none(mas) || mas_is_ptr(mas)))
5142 		return mas_sparse_area(mas, min, max, size, false);
5143 	else if (mas->offset >= 2)
5144 		mas->offset -= 2;
5145 	else
5146 		mas->offset = mas_data_end(mas);
5147 
5148 
5149 	/* The start of the window can only be within these values. */
5150 	mas->index = min;
5151 	mas->last = max;
5152 
5153 	while (!mas_rev_awalk(mas, size, &min, &max)) {
5154 		if (last == mas->node) {
5155 			if (!mas_rewind_node(mas))
5156 				return -EBUSY;
5157 		} else {
5158 			last = mas->node;
5159 		}
5160 	}
5161 
5162 	if (mas_is_err(mas))
5163 		return xa_err(mas->node);
5164 
5165 	if (unlikely(mas->offset == MAPLE_NODE_SLOTS))
5166 		return -EBUSY;
5167 
5168 	/* Trim the upper limit to the max. */
5169 	if (max < mas->last)
5170 		mas->last = max;
5171 
5172 	mas->index = mas->last - size + 1;
5173 	mas->end = mas_data_end(mas);
5174 	return 0;
5175 }
5176 EXPORT_SYMBOL_GPL(mas_empty_area_rev);
5177 
5178 /*
5179  * mte_dead_leaves() - Mark all leaves of a node as dead.
5180  * @enode: the encoded node
5181  * @mt: the maple tree
5182  * @slots: Pointer to the slot array
5183  *
5184  * Must hold the write lock.
5185  *
5186  * Return: The number of leaves marked as dead.
5187  */
5188 static inline
mte_dead_leaves(struct maple_enode * enode,struct maple_tree * mt,void __rcu ** slots)5189 unsigned char mte_dead_leaves(struct maple_enode *enode, struct maple_tree *mt,
5190 			      void __rcu **slots)
5191 {
5192 	struct maple_node *node;
5193 	enum maple_type type;
5194 	void *entry;
5195 	int offset;
5196 
5197 	for (offset = 0; offset < mt_slot_count(enode); offset++) {
5198 		entry = mt_slot(mt, slots, offset);
5199 		type = mte_node_type(entry);
5200 		node = mte_to_node(entry);
5201 		/* Use both node and type to catch LE & BE metadata */
5202 		if (!node || !type)
5203 			break;
5204 
5205 		mte_set_node_dead(entry);
5206 		node->type = type;
5207 		rcu_assign_pointer(slots[offset], node);
5208 	}
5209 
5210 	return offset;
5211 }
5212 
5213 /**
5214  * mte_dead_walk() - Walk down a dead tree to just before the leaves
5215  * @enode: The maple encoded node
5216  * @offset: The starting offset
5217  *
5218  * Note: This can only be used from the RCU callback context.
5219  */
mte_dead_walk(struct maple_enode ** enode,unsigned char offset)5220 static void __rcu **mte_dead_walk(struct maple_enode **enode, unsigned char offset)
5221 {
5222 	struct maple_node *node, *next;
5223 	void __rcu **slots = NULL;
5224 
5225 	next = mte_to_node(*enode);
5226 	do {
5227 		*enode = ma_enode_ptr(next);
5228 		node = mte_to_node(*enode);
5229 		slots = ma_slots(node, node->type);
5230 		next = rcu_dereference_protected(slots[offset],
5231 					lock_is_held(&rcu_callback_map));
5232 		offset = 0;
5233 	} while (!ma_is_leaf(next->type));
5234 
5235 	return slots;
5236 }
5237 
5238 /**
5239  * mt_free_walk() - Walk & free a tree in the RCU callback context
5240  * @head: The RCU head that's within the node.
5241  *
5242  * Note: This can only be used from the RCU callback context.
5243  */
mt_free_walk(struct rcu_head * head)5244 static void mt_free_walk(struct rcu_head *head)
5245 {
5246 	void __rcu **slots;
5247 	struct maple_node *node, *start;
5248 	struct maple_enode *enode;
5249 	unsigned char offset;
5250 	enum maple_type type;
5251 
5252 	node = container_of(head, struct maple_node, rcu);
5253 
5254 	if (ma_is_leaf(node->type))
5255 		goto free_leaf;
5256 
5257 	start = node;
5258 	enode = mt_mk_node(node, node->type);
5259 	slots = mte_dead_walk(&enode, 0);
5260 	node = mte_to_node(enode);
5261 	do {
5262 		mt_free_bulk(node->slot_len, slots);
5263 		offset = node->parent_slot + 1;
5264 		enode = node->piv_parent;
5265 		if (mte_to_node(enode) == node)
5266 			goto free_leaf;
5267 
5268 		type = mte_node_type(enode);
5269 		slots = ma_slots(mte_to_node(enode), type);
5270 		if ((offset < mt_slots[type]) &&
5271 		    rcu_dereference_protected(slots[offset],
5272 					      lock_is_held(&rcu_callback_map)))
5273 			slots = mte_dead_walk(&enode, offset);
5274 		node = mte_to_node(enode);
5275 	} while ((node != start) || (node->slot_len < offset));
5276 
5277 	slots = ma_slots(node, node->type);
5278 	mt_free_bulk(node->slot_len, slots);
5279 
5280 free_leaf:
5281 	mt_free_rcu(&node->rcu);
5282 }
5283 
mte_destroy_descend(struct maple_enode ** enode,struct maple_tree * mt,struct maple_enode * prev,unsigned char offset)5284 static inline void __rcu **mte_destroy_descend(struct maple_enode **enode,
5285 	struct maple_tree *mt, struct maple_enode *prev, unsigned char offset)
5286 {
5287 	struct maple_node *node;
5288 	struct maple_enode *next = *enode;
5289 	void __rcu **slots = NULL;
5290 	enum maple_type type;
5291 	unsigned char next_offset = 0;
5292 
5293 	do {
5294 		*enode = next;
5295 		node = mte_to_node(*enode);
5296 		type = mte_node_type(*enode);
5297 		slots = ma_slots(node, type);
5298 		next = mt_slot_locked(mt, slots, next_offset);
5299 		if ((mte_dead_node(next)))
5300 			next = mt_slot_locked(mt, slots, ++next_offset);
5301 
5302 		mte_set_node_dead(*enode);
5303 		node->type = type;
5304 		node->piv_parent = prev;
5305 		node->parent_slot = offset;
5306 		offset = next_offset;
5307 		next_offset = 0;
5308 		prev = *enode;
5309 	} while (!mte_is_leaf(next));
5310 
5311 	return slots;
5312 }
5313 
mt_destroy_walk(struct maple_enode * enode,struct maple_tree * mt,bool free)5314 static void mt_destroy_walk(struct maple_enode *enode, struct maple_tree *mt,
5315 			    bool free)
5316 {
5317 	void __rcu **slots;
5318 	struct maple_node *node = mte_to_node(enode);
5319 	struct maple_enode *start;
5320 
5321 	if (mte_is_leaf(enode)) {
5322 		node->type = mte_node_type(enode);
5323 		goto free_leaf;
5324 	}
5325 
5326 	start = enode;
5327 	slots = mte_destroy_descend(&enode, mt, start, 0);
5328 	node = mte_to_node(enode); // Updated in the above call.
5329 	do {
5330 		enum maple_type type;
5331 		unsigned char offset;
5332 		struct maple_enode *parent, *tmp;
5333 
5334 		node->slot_len = mte_dead_leaves(enode, mt, slots);
5335 		if (free)
5336 			mt_free_bulk(node->slot_len, slots);
5337 		offset = node->parent_slot + 1;
5338 		enode = node->piv_parent;
5339 		if (mte_to_node(enode) == node)
5340 			goto free_leaf;
5341 
5342 		type = mte_node_type(enode);
5343 		slots = ma_slots(mte_to_node(enode), type);
5344 		if (offset >= mt_slots[type])
5345 			goto next;
5346 
5347 		tmp = mt_slot_locked(mt, slots, offset);
5348 		if (mte_node_type(tmp) && mte_to_node(tmp)) {
5349 			parent = enode;
5350 			enode = tmp;
5351 			slots = mte_destroy_descend(&enode, mt, parent, offset);
5352 		}
5353 next:
5354 		node = mte_to_node(enode);
5355 	} while (start != enode);
5356 
5357 	node = mte_to_node(enode);
5358 	node->slot_len = mte_dead_leaves(enode, mt, slots);
5359 	if (free)
5360 		mt_free_bulk(node->slot_len, slots);
5361 
5362 free_leaf:
5363 	if (free)
5364 		mt_free_rcu(&node->rcu);
5365 	else
5366 		mt_clear_meta(mt, node, node->type);
5367 }
5368 
5369 /*
5370  * mte_destroy_walk() - Free a tree or sub-tree.
5371  * @enode: the encoded maple node (maple_enode) to start
5372  * @mt: the tree to free - needed for node types.
5373  *
5374  * Must hold the write lock.
5375  */
mte_destroy_walk(struct maple_enode * enode,struct maple_tree * mt)5376 static inline void mte_destroy_walk(struct maple_enode *enode,
5377 				    struct maple_tree *mt)
5378 {
5379 	struct maple_node *node = mte_to_node(enode);
5380 
5381 	if (mt_in_rcu(mt)) {
5382 		mt_destroy_walk(enode, mt, false);
5383 		call_rcu(&node->rcu, mt_free_walk);
5384 	} else {
5385 		mt_destroy_walk(enode, mt, true);
5386 	}
5387 }
5388 /* Interface */
5389 
5390 /**
5391  * mas_store() - Store an @entry.
5392  * @mas: The maple state.
5393  * @entry: The entry to store.
5394  *
5395  * The @mas->index and @mas->last is used to set the range for the @entry.
5396  *
5397  * Return: the first entry between mas->index and mas->last or %NULL.
5398  */
mas_store(struct ma_state * mas,void * entry)5399 void *mas_store(struct ma_state *mas, void *entry)
5400 {
5401 	int request;
5402 	MA_WR_STATE(wr_mas, mas, entry);
5403 
5404 	trace_ma_write(__func__, mas, 0, entry);
5405 #ifdef CONFIG_DEBUG_MAPLE_TREE
5406 	if (MAS_WARN_ON(mas, mas->index > mas->last))
5407 		pr_err("Error %lX > %lX " PTR_FMT "\n", mas->index, mas->last,
5408 		       entry);
5409 
5410 	if (mas->index > mas->last) {
5411 		mas_set_err(mas, -EINVAL);
5412 		return NULL;
5413 	}
5414 
5415 #endif
5416 
5417 	/*
5418 	 * Storing is the same operation as insert with the added caveat that it
5419 	 * can overwrite entries.  Although this seems simple enough, one may
5420 	 * want to examine what happens if a single store operation was to
5421 	 * overwrite multiple entries within a self-balancing B-Tree.
5422 	 */
5423 	mas_wr_prealloc_setup(&wr_mas);
5424 	mas->store_type = mas_wr_store_type(&wr_mas);
5425 	if (mas->mas_flags & MA_STATE_PREALLOC) {
5426 		mas_wr_store_entry(&wr_mas);
5427 		MAS_WR_BUG_ON(&wr_mas, mas_is_err(mas));
5428 		return wr_mas.content;
5429 	}
5430 
5431 	request = mas_prealloc_calc(&wr_mas, entry);
5432 	if (!request)
5433 		goto store;
5434 
5435 	mas_node_count(mas, request);
5436 	if (mas_is_err(mas))
5437 		return NULL;
5438 
5439 store:
5440 	mas_wr_store_entry(&wr_mas);
5441 	mas_destroy(mas);
5442 	return wr_mas.content;
5443 }
5444 EXPORT_SYMBOL_GPL(mas_store);
5445 
5446 /**
5447  * mas_store_gfp() - Store a value into the tree.
5448  * @mas: The maple state
5449  * @entry: The entry to store
5450  * @gfp: The GFP_FLAGS to use for allocations if necessary.
5451  *
5452  * Return: 0 on success, -EINVAL on invalid request, -ENOMEM if memory could not
5453  * be allocated.
5454  */
mas_store_gfp(struct ma_state * mas,void * entry,gfp_t gfp)5455 int mas_store_gfp(struct ma_state *mas, void *entry, gfp_t gfp)
5456 {
5457 	unsigned long index = mas->index;
5458 	unsigned long last = mas->last;
5459 	MA_WR_STATE(wr_mas, mas, entry);
5460 	int ret = 0;
5461 
5462 retry:
5463 	mas_wr_preallocate(&wr_mas, entry);
5464 	if (unlikely(mas_nomem(mas, gfp))) {
5465 		if (!entry)
5466 			__mas_set_range(mas, index, last);
5467 		goto retry;
5468 	}
5469 
5470 	if (mas_is_err(mas)) {
5471 		ret = xa_err(mas->node);
5472 		goto out;
5473 	}
5474 
5475 	mas_wr_store_entry(&wr_mas);
5476 out:
5477 	mas_destroy(mas);
5478 	return ret;
5479 }
5480 EXPORT_SYMBOL_GPL(mas_store_gfp);
5481 
5482 /**
5483  * mas_store_prealloc() - Store a value into the tree using memory
5484  * preallocated in the maple state.
5485  * @mas: The maple state
5486  * @entry: The entry to store.
5487  */
mas_store_prealloc(struct ma_state * mas,void * entry)5488 void mas_store_prealloc(struct ma_state *mas, void *entry)
5489 {
5490 	MA_WR_STATE(wr_mas, mas, entry);
5491 
5492 	if (mas->store_type == wr_store_root) {
5493 		mas_wr_prealloc_setup(&wr_mas);
5494 		goto store;
5495 	}
5496 
5497 	mas_wr_walk_descend(&wr_mas);
5498 	if (mas->store_type != wr_spanning_store) {
5499 		/* set wr_mas->content to current slot */
5500 		wr_mas.content = mas_slot_locked(mas, wr_mas.slots, mas->offset);
5501 		mas_wr_end_piv(&wr_mas);
5502 	}
5503 
5504 store:
5505 	trace_ma_write(__func__, mas, 0, entry);
5506 	mas_wr_store_entry(&wr_mas);
5507 	MAS_WR_BUG_ON(&wr_mas, mas_is_err(mas));
5508 	mas_destroy(mas);
5509 }
5510 EXPORT_SYMBOL_GPL(mas_store_prealloc);
5511 
5512 /**
5513  * mas_preallocate() - Preallocate enough nodes for a store operation
5514  * @mas: The maple state
5515  * @entry: The entry that will be stored
5516  * @gfp: The GFP_FLAGS to use for allocations.
5517  *
5518  * Return: 0 on success, -ENOMEM if memory could not be allocated.
5519  */
mas_preallocate(struct ma_state * mas,void * entry,gfp_t gfp)5520 int mas_preallocate(struct ma_state *mas, void *entry, gfp_t gfp)
5521 {
5522 	MA_WR_STATE(wr_mas, mas, entry);
5523 	int ret = 0;
5524 	int request;
5525 
5526 	mas_wr_prealloc_setup(&wr_mas);
5527 	mas->store_type = mas_wr_store_type(&wr_mas);
5528 	request = mas_prealloc_calc(&wr_mas, entry);
5529 	if (!request)
5530 		goto set_flag;
5531 
5532 	mas->mas_flags &= ~MA_STATE_PREALLOC;
5533 	mas_node_count_gfp(mas, request, gfp);
5534 	if (mas_is_err(mas)) {
5535 		mas_set_alloc_req(mas, 0);
5536 		ret = xa_err(mas->node);
5537 		mas_destroy(mas);
5538 		mas_reset(mas);
5539 		return ret;
5540 	}
5541 
5542 set_flag:
5543 	mas->mas_flags |= MA_STATE_PREALLOC;
5544 	return ret;
5545 }
5546 EXPORT_SYMBOL_GPL(mas_preallocate);
5547 
5548 /*
5549  * mas_destroy() - destroy a maple state.
5550  * @mas: The maple state
5551  *
5552  * Upon completion, check the left-most node and rebalance against the node to
5553  * the right if necessary.  Frees any allocated nodes associated with this maple
5554  * state.
5555  */
mas_destroy(struct ma_state * mas)5556 void mas_destroy(struct ma_state *mas)
5557 {
5558 	struct maple_alloc *node;
5559 	unsigned long total;
5560 
5561 	/*
5562 	 * When using mas_for_each() to insert an expected number of elements,
5563 	 * it is possible that the number inserted is less than the expected
5564 	 * number.  To fix an invalid final node, a check is performed here to
5565 	 * rebalance the previous node with the final node.
5566 	 */
5567 	if (mas->mas_flags & MA_STATE_REBALANCE) {
5568 		unsigned char end;
5569 		if (mas_is_err(mas))
5570 			mas_reset(mas);
5571 		mas_start(mas);
5572 		mtree_range_walk(mas);
5573 		end = mas->end + 1;
5574 		if (end < mt_min_slot_count(mas->node) - 1)
5575 			mas_destroy_rebalance(mas, end);
5576 
5577 		mas->mas_flags &= ~MA_STATE_REBALANCE;
5578 	}
5579 	mas->mas_flags &= ~(MA_STATE_BULK|MA_STATE_PREALLOC);
5580 
5581 	total = mas_allocated(mas);
5582 	while (total) {
5583 		node = mas->alloc;
5584 		mas->alloc = node->slot[0];
5585 		if (node->node_count > 1) {
5586 			size_t count = node->node_count - 1;
5587 
5588 			mt_free_bulk(count, (void __rcu **)&node->slot[1]);
5589 			total -= count;
5590 		}
5591 		mt_free_one(ma_mnode_ptr(node));
5592 		total--;
5593 	}
5594 
5595 	mas->alloc = NULL;
5596 }
5597 EXPORT_SYMBOL_GPL(mas_destroy);
5598 
5599 /*
5600  * mas_expected_entries() - Set the expected number of entries that will be inserted.
5601  * @mas: The maple state
5602  * @nr_entries: The number of expected entries.
5603  *
5604  * This will attempt to pre-allocate enough nodes to store the expected number
5605  * of entries.  The allocations will occur using the bulk allocator interface
5606  * for speed.  Please call mas_destroy() on the @mas after inserting the entries
5607  * to ensure any unused nodes are freed.
5608  *
5609  * Return: 0 on success, -ENOMEM if memory could not be allocated.
5610  */
mas_expected_entries(struct ma_state * mas,unsigned long nr_entries)5611 int mas_expected_entries(struct ma_state *mas, unsigned long nr_entries)
5612 {
5613 	int nonleaf_cap = MAPLE_ARANGE64_SLOTS - 2;
5614 	struct maple_enode *enode = mas->node;
5615 	int nr_nodes;
5616 	int ret;
5617 
5618 	/*
5619 	 * Sometimes it is necessary to duplicate a tree to a new tree, such as
5620 	 * forking a process and duplicating the VMAs from one tree to a new
5621 	 * tree.  When such a situation arises, it is known that the new tree is
5622 	 * not going to be used until the entire tree is populated.  For
5623 	 * performance reasons, it is best to use a bulk load with RCU disabled.
5624 	 * This allows for optimistic splitting that favours the left and reuse
5625 	 * of nodes during the operation.
5626 	 */
5627 
5628 	/* Optimize splitting for bulk insert in-order */
5629 	mas->mas_flags |= MA_STATE_BULK;
5630 
5631 	/*
5632 	 * Avoid overflow, assume a gap between each entry and a trailing null.
5633 	 * If this is wrong, it just means allocation can happen during
5634 	 * insertion of entries.
5635 	 */
5636 	nr_nodes = max(nr_entries, nr_entries * 2 + 1);
5637 	if (!mt_is_alloc(mas->tree))
5638 		nonleaf_cap = MAPLE_RANGE64_SLOTS - 2;
5639 
5640 	/* Leaves; reduce slots to keep space for expansion */
5641 	nr_nodes = DIV_ROUND_UP(nr_nodes, MAPLE_RANGE64_SLOTS - 2);
5642 	/* Internal nodes */
5643 	nr_nodes += DIV_ROUND_UP(nr_nodes, nonleaf_cap);
5644 	/* Add working room for split (2 nodes) + new parents */
5645 	mas_node_count_gfp(mas, nr_nodes + 3, GFP_KERNEL);
5646 
5647 	/* Detect if allocations run out */
5648 	mas->mas_flags |= MA_STATE_PREALLOC;
5649 
5650 	if (!mas_is_err(mas))
5651 		return 0;
5652 
5653 	ret = xa_err(mas->node);
5654 	mas->node = enode;
5655 	mas_destroy(mas);
5656 	return ret;
5657 
5658 }
5659 EXPORT_SYMBOL_GPL(mas_expected_entries);
5660 
mas_next_setup(struct ma_state * mas,unsigned long max,void ** entry)5661 static bool mas_next_setup(struct ma_state *mas, unsigned long max,
5662 		void **entry)
5663 {
5664 	bool was_none = mas_is_none(mas);
5665 
5666 	if (unlikely(mas->last >= max)) {
5667 		mas->status = ma_overflow;
5668 		return true;
5669 	}
5670 
5671 	switch (mas->status) {
5672 	case ma_active:
5673 		return false;
5674 	case ma_none:
5675 		fallthrough;
5676 	case ma_pause:
5677 		mas->status = ma_start;
5678 		fallthrough;
5679 	case ma_start:
5680 		mas_walk(mas); /* Retries on dead nodes handled by mas_walk */
5681 		break;
5682 	case ma_overflow:
5683 		/* Overflowed before, but the max changed */
5684 		mas->status = ma_active;
5685 		break;
5686 	case ma_underflow:
5687 		/* The user expects the mas to be one before where it is */
5688 		mas->status = ma_active;
5689 		*entry = mas_walk(mas);
5690 		if (*entry)
5691 			return true;
5692 		break;
5693 	case ma_root:
5694 		break;
5695 	case ma_error:
5696 		return true;
5697 	}
5698 
5699 	if (likely(mas_is_active(mas))) /* Fast path */
5700 		return false;
5701 
5702 	if (mas_is_ptr(mas)) {
5703 		*entry = NULL;
5704 		if (was_none && mas->index == 0) {
5705 			mas->index = mas->last = 0;
5706 			return true;
5707 		}
5708 		mas->index = 1;
5709 		mas->last = ULONG_MAX;
5710 		mas->status = ma_none;
5711 		return true;
5712 	}
5713 
5714 	if (mas_is_none(mas))
5715 		return true;
5716 
5717 	return false;
5718 }
5719 
5720 /**
5721  * mas_next() - Get the next entry.
5722  * @mas: The maple state
5723  * @max: The maximum index to check.
5724  *
5725  * Returns the next entry after @mas->index.
5726  * Must hold rcu_read_lock or the write lock.
5727  * Can return the zero entry.
5728  *
5729  * Return: The next entry or %NULL
5730  */
mas_next(struct ma_state * mas,unsigned long max)5731 void *mas_next(struct ma_state *mas, unsigned long max)
5732 {
5733 	void *entry = NULL;
5734 
5735 	if (mas_next_setup(mas, max, &entry))
5736 		return entry;
5737 
5738 	/* Retries on dead nodes handled by mas_next_slot */
5739 	return mas_next_slot(mas, max, false);
5740 }
5741 EXPORT_SYMBOL_GPL(mas_next);
5742 
5743 /**
5744  * mas_next_range() - Advance the maple state to the next range
5745  * @mas: The maple state
5746  * @max: The maximum index to check.
5747  *
5748  * Sets @mas->index and @mas->last to the range.
5749  * Must hold rcu_read_lock or the write lock.
5750  * Can return the zero entry.
5751  *
5752  * Return: The next entry or %NULL
5753  */
mas_next_range(struct ma_state * mas,unsigned long max)5754 void *mas_next_range(struct ma_state *mas, unsigned long max)
5755 {
5756 	void *entry = NULL;
5757 
5758 	if (mas_next_setup(mas, max, &entry))
5759 		return entry;
5760 
5761 	/* Retries on dead nodes handled by mas_next_slot */
5762 	return mas_next_slot(mas, max, true);
5763 }
5764 EXPORT_SYMBOL_GPL(mas_next_range);
5765 
5766 /**
5767  * mt_next() - get the next value in the maple tree
5768  * @mt: The maple tree
5769  * @index: The start index
5770  * @max: The maximum index to check
5771  *
5772  * Takes RCU read lock internally to protect the search, which does not
5773  * protect the returned pointer after dropping RCU read lock.
5774  * See also: Documentation/core-api/maple_tree.rst
5775  *
5776  * Return: The entry higher than @index or %NULL if nothing is found.
5777  */
mt_next(struct maple_tree * mt,unsigned long index,unsigned long max)5778 void *mt_next(struct maple_tree *mt, unsigned long index, unsigned long max)
5779 {
5780 	void *entry = NULL;
5781 	MA_STATE(mas, mt, index, index);
5782 
5783 	rcu_read_lock();
5784 	entry = mas_next(&mas, max);
5785 	rcu_read_unlock();
5786 	return entry;
5787 }
5788 EXPORT_SYMBOL_GPL(mt_next);
5789 
mas_prev_setup(struct ma_state * mas,unsigned long min,void ** entry)5790 static bool mas_prev_setup(struct ma_state *mas, unsigned long min, void **entry)
5791 {
5792 	if (unlikely(mas->index <= min)) {
5793 		mas->status = ma_underflow;
5794 		return true;
5795 	}
5796 
5797 	switch (mas->status) {
5798 	case ma_active:
5799 		return false;
5800 	case ma_start:
5801 		break;
5802 	case ma_none:
5803 		fallthrough;
5804 	case ma_pause:
5805 		mas->status = ma_start;
5806 		break;
5807 	case ma_underflow:
5808 		/* underflowed before but the min changed */
5809 		mas->status = ma_active;
5810 		break;
5811 	case ma_overflow:
5812 		/* User expects mas to be one after where it is */
5813 		mas->status = ma_active;
5814 		*entry = mas_walk(mas);
5815 		if (*entry)
5816 			return true;
5817 		break;
5818 	case ma_root:
5819 		break;
5820 	case ma_error:
5821 		return true;
5822 	}
5823 
5824 	if (mas_is_start(mas))
5825 		mas_walk(mas);
5826 
5827 	if (unlikely(mas_is_ptr(mas))) {
5828 		if (!mas->index) {
5829 			mas->status = ma_none;
5830 			return true;
5831 		}
5832 		mas->index = mas->last = 0;
5833 		*entry = mas_root(mas);
5834 		return true;
5835 	}
5836 
5837 	if (mas_is_none(mas)) {
5838 		if (mas->index) {
5839 			/* Walked to out-of-range pointer? */
5840 			mas->index = mas->last = 0;
5841 			mas->status = ma_root;
5842 			*entry = mas_root(mas);
5843 			return true;
5844 		}
5845 		return true;
5846 	}
5847 
5848 	return false;
5849 }
5850 
5851 /**
5852  * mas_prev() - Get the previous entry
5853  * @mas: The maple state
5854  * @min: The minimum value to check.
5855  *
5856  * Must hold rcu_read_lock or the write lock.
5857  * Will reset mas to ma_start if the status is ma_none.  Will stop on not
5858  * searchable nodes.
5859  *
5860  * Return: the previous value or %NULL.
5861  */
mas_prev(struct ma_state * mas,unsigned long min)5862 void *mas_prev(struct ma_state *mas, unsigned long min)
5863 {
5864 	void *entry = NULL;
5865 
5866 	if (mas_prev_setup(mas, min, &entry))
5867 		return entry;
5868 
5869 	return mas_prev_slot(mas, min, false);
5870 }
5871 EXPORT_SYMBOL_GPL(mas_prev);
5872 
5873 /**
5874  * mas_prev_range() - Advance to the previous range
5875  * @mas: The maple state
5876  * @min: The minimum value to check.
5877  *
5878  * Sets @mas->index and @mas->last to the range.
5879  * Must hold rcu_read_lock or the write lock.
5880  * Will reset mas to ma_start if the node is ma_none.  Will stop on not
5881  * searchable nodes.
5882  *
5883  * Return: the previous value or %NULL.
5884  */
mas_prev_range(struct ma_state * mas,unsigned long min)5885 void *mas_prev_range(struct ma_state *mas, unsigned long min)
5886 {
5887 	void *entry = NULL;
5888 
5889 	if (mas_prev_setup(mas, min, &entry))
5890 		return entry;
5891 
5892 	return mas_prev_slot(mas, min, true);
5893 }
5894 EXPORT_SYMBOL_GPL(mas_prev_range);
5895 
5896 /**
5897  * mt_prev() - get the previous value in the maple tree
5898  * @mt: The maple tree
5899  * @index: The start index
5900  * @min: The minimum index to check
5901  *
5902  * Takes RCU read lock internally to protect the search, which does not
5903  * protect the returned pointer after dropping RCU read lock.
5904  * See also: Documentation/core-api/maple_tree.rst
5905  *
5906  * Return: The entry before @index or %NULL if nothing is found.
5907  */
mt_prev(struct maple_tree * mt,unsigned long index,unsigned long min)5908 void *mt_prev(struct maple_tree *mt, unsigned long index, unsigned long min)
5909 {
5910 	void *entry = NULL;
5911 	MA_STATE(mas, mt, index, index);
5912 
5913 	rcu_read_lock();
5914 	entry = mas_prev(&mas, min);
5915 	rcu_read_unlock();
5916 	return entry;
5917 }
5918 EXPORT_SYMBOL_GPL(mt_prev);
5919 
5920 /**
5921  * mas_pause() - Pause a mas_find/mas_for_each to drop the lock.
5922  * @mas: The maple state to pause
5923  *
5924  * Some users need to pause a walk and drop the lock they're holding in
5925  * order to yield to a higher priority thread or carry out an operation
5926  * on an entry.  Those users should call this function before they drop
5927  * the lock.  It resets the @mas to be suitable for the next iteration
5928  * of the loop after the user has reacquired the lock.  If most entries
5929  * found during a walk require you to call mas_pause(), the mt_for_each()
5930  * iterator may be more appropriate.
5931  *
5932  */
mas_pause(struct ma_state * mas)5933 void mas_pause(struct ma_state *mas)
5934 {
5935 	mas->status = ma_pause;
5936 	mas->node = NULL;
5937 }
5938 EXPORT_SYMBOL_GPL(mas_pause);
5939 
5940 /**
5941  * mas_find_setup() - Internal function to set up mas_find*().
5942  * @mas: The maple state
5943  * @max: The maximum index
5944  * @entry: Pointer to the entry
5945  *
5946  * Returns: True if entry is the answer, false otherwise.
5947  */
mas_find_setup(struct ma_state * mas,unsigned long max,void ** entry)5948 static __always_inline bool mas_find_setup(struct ma_state *mas, unsigned long max, void **entry)
5949 {
5950 	switch (mas->status) {
5951 	case ma_active:
5952 		if (mas->last < max)
5953 			return false;
5954 		return true;
5955 	case ma_start:
5956 		break;
5957 	case ma_pause:
5958 		if (unlikely(mas->last >= max))
5959 			return true;
5960 
5961 		mas->index = ++mas->last;
5962 		mas->status = ma_start;
5963 		break;
5964 	case ma_none:
5965 		if (unlikely(mas->last >= max))
5966 			return true;
5967 
5968 		mas->index = mas->last;
5969 		mas->status = ma_start;
5970 		break;
5971 	case ma_underflow:
5972 		/* mas is pointing at entry before unable to go lower */
5973 		if (unlikely(mas->index >= max)) {
5974 			mas->status = ma_overflow;
5975 			return true;
5976 		}
5977 
5978 		mas->status = ma_active;
5979 		*entry = mas_walk(mas);
5980 		if (*entry)
5981 			return true;
5982 		break;
5983 	case ma_overflow:
5984 		if (unlikely(mas->last >= max))
5985 			return true;
5986 
5987 		mas->status = ma_active;
5988 		*entry = mas_walk(mas);
5989 		if (*entry)
5990 			return true;
5991 		break;
5992 	case ma_root:
5993 		break;
5994 	case ma_error:
5995 		return true;
5996 	}
5997 
5998 	if (mas_is_start(mas)) {
5999 		/* First run or continue */
6000 		if (mas->index > max)
6001 			return true;
6002 
6003 		*entry = mas_walk(mas);
6004 		if (*entry)
6005 			return true;
6006 
6007 	}
6008 
6009 	if (unlikely(mas_is_ptr(mas)))
6010 		goto ptr_out_of_range;
6011 
6012 	if (unlikely(mas_is_none(mas)))
6013 		return true;
6014 
6015 	if (mas->index == max)
6016 		return true;
6017 
6018 	return false;
6019 
6020 ptr_out_of_range:
6021 	mas->status = ma_none;
6022 	mas->index = 1;
6023 	mas->last = ULONG_MAX;
6024 	return true;
6025 }
6026 
6027 /**
6028  * mas_find() - On the first call, find the entry at or after mas->index up to
6029  * %max.  Otherwise, find the entry after mas->index.
6030  * @mas: The maple state
6031  * @max: The maximum value to check.
6032  *
6033  * Must hold rcu_read_lock or the write lock.
6034  * If an entry exists, last and index are updated accordingly.
6035  * May set @mas->status to ma_overflow.
6036  *
6037  * Return: The entry or %NULL.
6038  */
mas_find(struct ma_state * mas,unsigned long max)6039 void *mas_find(struct ma_state *mas, unsigned long max)
6040 {
6041 	void *entry = NULL;
6042 
6043 	if (mas_find_setup(mas, max, &entry))
6044 		return entry;
6045 
6046 	/* Retries on dead nodes handled by mas_next_slot */
6047 	entry = mas_next_slot(mas, max, false);
6048 	/* Ignore overflow */
6049 	mas->status = ma_active;
6050 	return entry;
6051 }
6052 EXPORT_SYMBOL_GPL(mas_find);
6053 
6054 /**
6055  * mas_find_range() - On the first call, find the entry at or after
6056  * mas->index up to %max.  Otherwise, advance to the next slot mas->index.
6057  * @mas: The maple state
6058  * @max: The maximum value to check.
6059  *
6060  * Must hold rcu_read_lock or the write lock.
6061  * If an entry exists, last and index are updated accordingly.
6062  * May set @mas->status to ma_overflow.
6063  *
6064  * Return: The entry or %NULL.
6065  */
mas_find_range(struct ma_state * mas,unsigned long max)6066 void *mas_find_range(struct ma_state *mas, unsigned long max)
6067 {
6068 	void *entry = NULL;
6069 
6070 	if (mas_find_setup(mas, max, &entry))
6071 		return entry;
6072 
6073 	/* Retries on dead nodes handled by mas_next_slot */
6074 	return mas_next_slot(mas, max, true);
6075 }
6076 EXPORT_SYMBOL_GPL(mas_find_range);
6077 
6078 /**
6079  * mas_find_rev_setup() - Internal function to set up mas_find_*_rev()
6080  * @mas: The maple state
6081  * @min: The minimum index
6082  * @entry: Pointer to the entry
6083  *
6084  * Returns: True if entry is the answer, false otherwise.
6085  */
mas_find_rev_setup(struct ma_state * mas,unsigned long min,void ** entry)6086 static bool mas_find_rev_setup(struct ma_state *mas, unsigned long min,
6087 		void **entry)
6088 {
6089 
6090 	switch (mas->status) {
6091 	case ma_active:
6092 		goto active;
6093 	case ma_start:
6094 		break;
6095 	case ma_pause:
6096 		if (unlikely(mas->index <= min)) {
6097 			mas->status = ma_underflow;
6098 			return true;
6099 		}
6100 		mas->last = --mas->index;
6101 		mas->status = ma_start;
6102 		break;
6103 	case ma_none:
6104 		if (mas->index <= min)
6105 			goto none;
6106 
6107 		mas->last = mas->index;
6108 		mas->status = ma_start;
6109 		break;
6110 	case ma_overflow: /* user expects the mas to be one after where it is */
6111 		if (unlikely(mas->index <= min)) {
6112 			mas->status = ma_underflow;
6113 			return true;
6114 		}
6115 
6116 		mas->status = ma_active;
6117 		break;
6118 	case ma_underflow: /* user expects the mas to be one before where it is */
6119 		if (unlikely(mas->index <= min))
6120 			return true;
6121 
6122 		mas->status = ma_active;
6123 		break;
6124 	case ma_root:
6125 		break;
6126 	case ma_error:
6127 		return true;
6128 	}
6129 
6130 	if (mas_is_start(mas)) {
6131 		/* First run or continue */
6132 		if (mas->index < min)
6133 			return true;
6134 
6135 		*entry = mas_walk(mas);
6136 		if (*entry)
6137 			return true;
6138 	}
6139 
6140 	if (unlikely(mas_is_ptr(mas)))
6141 		goto none;
6142 
6143 	if (unlikely(mas_is_none(mas))) {
6144 		/*
6145 		 * Walked to the location, and there was nothing so the previous
6146 		 * location is 0.
6147 		 */
6148 		mas->last = mas->index = 0;
6149 		mas->status = ma_root;
6150 		*entry = mas_root(mas);
6151 		return true;
6152 	}
6153 
6154 active:
6155 	if (mas->index < min)
6156 		return true;
6157 
6158 	return false;
6159 
6160 none:
6161 	mas->status = ma_none;
6162 	return true;
6163 }
6164 
6165 /**
6166  * mas_find_rev: On the first call, find the first non-null entry at or below
6167  * mas->index down to %min.  Otherwise find the first non-null entry below
6168  * mas->index down to %min.
6169  * @mas: The maple state
6170  * @min: The minimum value to check.
6171  *
6172  * Must hold rcu_read_lock or the write lock.
6173  * If an entry exists, last and index are updated accordingly.
6174  * May set @mas->status to ma_underflow.
6175  *
6176  * Return: The entry or %NULL.
6177  */
mas_find_rev(struct ma_state * mas,unsigned long min)6178 void *mas_find_rev(struct ma_state *mas, unsigned long min)
6179 {
6180 	void *entry = NULL;
6181 
6182 	if (mas_find_rev_setup(mas, min, &entry))
6183 		return entry;
6184 
6185 	/* Retries on dead nodes handled by mas_prev_slot */
6186 	return mas_prev_slot(mas, min, false);
6187 
6188 }
6189 EXPORT_SYMBOL_GPL(mas_find_rev);
6190 
6191 /**
6192  * mas_find_range_rev: On the first call, find the first non-null entry at or
6193  * below mas->index down to %min.  Otherwise advance to the previous slot after
6194  * mas->index down to %min.
6195  * @mas: The maple state
6196  * @min: The minimum value to check.
6197  *
6198  * Must hold rcu_read_lock or the write lock.
6199  * If an entry exists, last and index are updated accordingly.
6200  * May set @mas->status to ma_underflow.
6201  *
6202  * Return: The entry or %NULL.
6203  */
mas_find_range_rev(struct ma_state * mas,unsigned long min)6204 void *mas_find_range_rev(struct ma_state *mas, unsigned long min)
6205 {
6206 	void *entry = NULL;
6207 
6208 	if (mas_find_rev_setup(mas, min, &entry))
6209 		return entry;
6210 
6211 	/* Retries on dead nodes handled by mas_prev_slot */
6212 	return mas_prev_slot(mas, min, true);
6213 }
6214 EXPORT_SYMBOL_GPL(mas_find_range_rev);
6215 
6216 /**
6217  * mas_erase() - Find the range in which index resides and erase the entire
6218  * range.
6219  * @mas: The maple state
6220  *
6221  * Must hold the write lock.
6222  * Searches for @mas->index, sets @mas->index and @mas->last to the range and
6223  * erases that range.
6224  *
6225  * Return: the entry that was erased or %NULL, @mas->index and @mas->last are updated.
6226  */
mas_erase(struct ma_state * mas)6227 void *mas_erase(struct ma_state *mas)
6228 {
6229 	void *entry;
6230 	unsigned long index = mas->index;
6231 	MA_WR_STATE(wr_mas, mas, NULL);
6232 
6233 	if (!mas_is_active(mas) || !mas_is_start(mas))
6234 		mas->status = ma_start;
6235 
6236 write_retry:
6237 	entry = mas_state_walk(mas);
6238 	if (!entry)
6239 		return NULL;
6240 
6241 	/* Must reset to ensure spanning writes of last slot are detected */
6242 	mas_reset(mas);
6243 	mas_wr_preallocate(&wr_mas, NULL);
6244 	if (mas_nomem(mas, GFP_KERNEL)) {
6245 		/* in case the range of entry changed when unlocked */
6246 		mas->index = mas->last = index;
6247 		goto write_retry;
6248 	}
6249 
6250 	if (mas_is_err(mas))
6251 		goto out;
6252 
6253 	mas_wr_store_entry(&wr_mas);
6254 out:
6255 	mas_destroy(mas);
6256 	return entry;
6257 }
6258 EXPORT_SYMBOL_GPL(mas_erase);
6259 
6260 /**
6261  * mas_nomem() - Check if there was an error allocating and do the allocation
6262  * if necessary If there are allocations, then free them.
6263  * @mas: The maple state
6264  * @gfp: The GFP_FLAGS to use for allocations
6265  * Return: true on allocation, false otherwise.
6266  */
mas_nomem(struct ma_state * mas,gfp_t gfp)6267 bool mas_nomem(struct ma_state *mas, gfp_t gfp)
6268 	__must_hold(mas->tree->ma_lock)
6269 {
6270 	if (likely(mas->node != MA_ERROR(-ENOMEM)))
6271 		return false;
6272 
6273 	if (gfpflags_allow_blocking(gfp) && !mt_external_lock(mas->tree)) {
6274 		mtree_unlock(mas->tree);
6275 		mas_alloc_nodes(mas, gfp);
6276 		mtree_lock(mas->tree);
6277 	} else {
6278 		mas_alloc_nodes(mas, gfp);
6279 	}
6280 
6281 	if (!mas_allocated(mas))
6282 		return false;
6283 
6284 	mas->status = ma_start;
6285 	return true;
6286 }
6287 
maple_tree_init(void)6288 void __init maple_tree_init(void)
6289 {
6290 	maple_node_cache = kmem_cache_create("maple_node",
6291 			sizeof(struct maple_node), sizeof(struct maple_node),
6292 			SLAB_PANIC, NULL);
6293 }
6294 
6295 /**
6296  * mtree_load() - Load a value stored in a maple tree
6297  * @mt: The maple tree
6298  * @index: The index to load
6299  *
6300  * Return: the entry or %NULL
6301  */
mtree_load(struct maple_tree * mt,unsigned long index)6302 void *mtree_load(struct maple_tree *mt, unsigned long index)
6303 {
6304 	MA_STATE(mas, mt, index, index);
6305 	void *entry;
6306 
6307 	trace_ma_read(__func__, &mas);
6308 	rcu_read_lock();
6309 retry:
6310 	entry = mas_start(&mas);
6311 	if (unlikely(mas_is_none(&mas)))
6312 		goto unlock;
6313 
6314 	if (unlikely(mas_is_ptr(&mas))) {
6315 		if (index)
6316 			entry = NULL;
6317 
6318 		goto unlock;
6319 	}
6320 
6321 	entry = mtree_lookup_walk(&mas);
6322 	if (!entry && unlikely(mas_is_start(&mas)))
6323 		goto retry;
6324 unlock:
6325 	rcu_read_unlock();
6326 	if (xa_is_zero(entry))
6327 		return NULL;
6328 
6329 	return entry;
6330 }
6331 EXPORT_SYMBOL(mtree_load);
6332 
6333 /**
6334  * mtree_store_range() - Store an entry at a given range.
6335  * @mt: The maple tree
6336  * @index: The start of the range
6337  * @last: The end of the range
6338  * @entry: The entry to store
6339  * @gfp: The GFP_FLAGS to use for allocations
6340  *
6341  * Return: 0 on success, -EINVAL on invalid request, -ENOMEM if memory could not
6342  * be allocated.
6343  */
mtree_store_range(struct maple_tree * mt,unsigned long index,unsigned long last,void * entry,gfp_t gfp)6344 int mtree_store_range(struct maple_tree *mt, unsigned long index,
6345 		unsigned long last, void *entry, gfp_t gfp)
6346 {
6347 	MA_STATE(mas, mt, index, last);
6348 	int ret = 0;
6349 
6350 	trace_ma_write(__func__, &mas, 0, entry);
6351 	if (WARN_ON_ONCE(xa_is_advanced(entry)))
6352 		return -EINVAL;
6353 
6354 	if (index > last)
6355 		return -EINVAL;
6356 
6357 	mtree_lock(mt);
6358 	ret = mas_store_gfp(&mas, entry, gfp);
6359 	mtree_unlock(mt);
6360 
6361 	return ret;
6362 }
6363 EXPORT_SYMBOL(mtree_store_range);
6364 
6365 /**
6366  * mtree_store() - Store an entry at a given index.
6367  * @mt: The maple tree
6368  * @index: The index to store the value
6369  * @entry: The entry to store
6370  * @gfp: The GFP_FLAGS to use for allocations
6371  *
6372  * Return: 0 on success, -EINVAL on invalid request, -ENOMEM if memory could not
6373  * be allocated.
6374  */
mtree_store(struct maple_tree * mt,unsigned long index,void * entry,gfp_t gfp)6375 int mtree_store(struct maple_tree *mt, unsigned long index, void *entry,
6376 		 gfp_t gfp)
6377 {
6378 	return mtree_store_range(mt, index, index, entry, gfp);
6379 }
6380 EXPORT_SYMBOL(mtree_store);
6381 
6382 /**
6383  * mtree_insert_range() - Insert an entry at a given range if there is no value.
6384  * @mt: The maple tree
6385  * @first: The start of the range
6386  * @last: The end of the range
6387  * @entry: The entry to store
6388  * @gfp: The GFP_FLAGS to use for allocations.
6389  *
6390  * Return: 0 on success, -EEXISTS if the range is occupied, -EINVAL on invalid
6391  * request, -ENOMEM if memory could not be allocated.
6392  */
mtree_insert_range(struct maple_tree * mt,unsigned long first,unsigned long last,void * entry,gfp_t gfp)6393 int mtree_insert_range(struct maple_tree *mt, unsigned long first,
6394 		unsigned long last, void *entry, gfp_t gfp)
6395 {
6396 	MA_STATE(ms, mt, first, last);
6397 	int ret = 0;
6398 
6399 	if (WARN_ON_ONCE(xa_is_advanced(entry)))
6400 		return -EINVAL;
6401 
6402 	if (first > last)
6403 		return -EINVAL;
6404 
6405 	mtree_lock(mt);
6406 retry:
6407 	mas_insert(&ms, entry);
6408 	if (mas_nomem(&ms, gfp))
6409 		goto retry;
6410 
6411 	mtree_unlock(mt);
6412 	if (mas_is_err(&ms))
6413 		ret = xa_err(ms.node);
6414 
6415 	mas_destroy(&ms);
6416 	return ret;
6417 }
6418 EXPORT_SYMBOL(mtree_insert_range);
6419 
6420 /**
6421  * mtree_insert() - Insert an entry at a given index if there is no value.
6422  * @mt: The maple tree
6423  * @index : The index to store the value
6424  * @entry: The entry to store
6425  * @gfp: The GFP_FLAGS to use for allocations.
6426  *
6427  * Return: 0 on success, -EEXISTS if the range is occupied, -EINVAL on invalid
6428  * request, -ENOMEM if memory could not be allocated.
6429  */
mtree_insert(struct maple_tree * mt,unsigned long index,void * entry,gfp_t gfp)6430 int mtree_insert(struct maple_tree *mt, unsigned long index, void *entry,
6431 		 gfp_t gfp)
6432 {
6433 	return mtree_insert_range(mt, index, index, entry, gfp);
6434 }
6435 EXPORT_SYMBOL(mtree_insert);
6436 
mtree_alloc_range(struct maple_tree * mt,unsigned long * startp,void * entry,unsigned long size,unsigned long min,unsigned long max,gfp_t gfp)6437 int mtree_alloc_range(struct maple_tree *mt, unsigned long *startp,
6438 		void *entry, unsigned long size, unsigned long min,
6439 		unsigned long max, gfp_t gfp)
6440 {
6441 	int ret = 0;
6442 
6443 	MA_STATE(mas, mt, 0, 0);
6444 	if (!mt_is_alloc(mt))
6445 		return -EINVAL;
6446 
6447 	if (WARN_ON_ONCE(mt_is_reserved(entry)))
6448 		return -EINVAL;
6449 
6450 	mtree_lock(mt);
6451 retry:
6452 	ret = mas_empty_area(&mas, min, max, size);
6453 	if (ret)
6454 		goto unlock;
6455 
6456 	mas_insert(&mas, entry);
6457 	/*
6458 	 * mas_nomem() may release the lock, causing the allocated area
6459 	 * to be unavailable, so try to allocate a free area again.
6460 	 */
6461 	if (mas_nomem(&mas, gfp))
6462 		goto retry;
6463 
6464 	if (mas_is_err(&mas))
6465 		ret = xa_err(mas.node);
6466 	else
6467 		*startp = mas.index;
6468 
6469 unlock:
6470 	mtree_unlock(mt);
6471 	mas_destroy(&mas);
6472 	return ret;
6473 }
6474 EXPORT_SYMBOL(mtree_alloc_range);
6475 
6476 /**
6477  * mtree_alloc_cyclic() - Find somewhere to store this entry in the tree.
6478  * @mt: The maple tree.
6479  * @startp: Pointer to ID.
6480  * @range_lo: Lower bound of range to search.
6481  * @range_hi: Upper bound of range to search.
6482  * @entry: The entry to store.
6483  * @next: Pointer to next ID to allocate.
6484  * @gfp: The GFP_FLAGS to use for allocations.
6485  *
6486  * Finds an empty entry in @mt after @next, stores the new index into
6487  * the @id pointer, stores the entry at that index, then updates @next.
6488  *
6489  * @mt must be initialized with the MT_FLAGS_ALLOC_RANGE flag.
6490  *
6491  * Context: Any context.  Takes and releases the mt.lock.  May sleep if
6492  * the @gfp flags permit.
6493  *
6494  * Return: 0 if the allocation succeeded without wrapping, 1 if the
6495  * allocation succeeded after wrapping, -ENOMEM if memory could not be
6496  * allocated, -EINVAL if @mt cannot be used, or -EBUSY if there are no
6497  * free entries.
6498  */
mtree_alloc_cyclic(struct maple_tree * mt,unsigned long * startp,void * entry,unsigned long range_lo,unsigned long range_hi,unsigned long * next,gfp_t gfp)6499 int mtree_alloc_cyclic(struct maple_tree *mt, unsigned long *startp,
6500 		void *entry, unsigned long range_lo, unsigned long range_hi,
6501 		unsigned long *next, gfp_t gfp)
6502 {
6503 	int ret;
6504 
6505 	MA_STATE(mas, mt, 0, 0);
6506 
6507 	if (!mt_is_alloc(mt))
6508 		return -EINVAL;
6509 	if (WARN_ON_ONCE(mt_is_reserved(entry)))
6510 		return -EINVAL;
6511 	mtree_lock(mt);
6512 	ret = mas_alloc_cyclic(&mas, startp, entry, range_lo, range_hi,
6513 			       next, gfp);
6514 	mtree_unlock(mt);
6515 	return ret;
6516 }
6517 EXPORT_SYMBOL(mtree_alloc_cyclic);
6518 
mtree_alloc_rrange(struct maple_tree * mt,unsigned long * startp,void * entry,unsigned long size,unsigned long min,unsigned long max,gfp_t gfp)6519 int mtree_alloc_rrange(struct maple_tree *mt, unsigned long *startp,
6520 		void *entry, unsigned long size, unsigned long min,
6521 		unsigned long max, gfp_t gfp)
6522 {
6523 	int ret = 0;
6524 
6525 	MA_STATE(mas, mt, 0, 0);
6526 	if (!mt_is_alloc(mt))
6527 		return -EINVAL;
6528 
6529 	if (WARN_ON_ONCE(mt_is_reserved(entry)))
6530 		return -EINVAL;
6531 
6532 	mtree_lock(mt);
6533 retry:
6534 	ret = mas_empty_area_rev(&mas, min, max, size);
6535 	if (ret)
6536 		goto unlock;
6537 
6538 	mas_insert(&mas, entry);
6539 	/*
6540 	 * mas_nomem() may release the lock, causing the allocated area
6541 	 * to be unavailable, so try to allocate a free area again.
6542 	 */
6543 	if (mas_nomem(&mas, gfp))
6544 		goto retry;
6545 
6546 	if (mas_is_err(&mas))
6547 		ret = xa_err(mas.node);
6548 	else
6549 		*startp = mas.index;
6550 
6551 unlock:
6552 	mtree_unlock(mt);
6553 	mas_destroy(&mas);
6554 	return ret;
6555 }
6556 EXPORT_SYMBOL(mtree_alloc_rrange);
6557 
6558 /**
6559  * mtree_erase() - Find an index and erase the entire range.
6560  * @mt: The maple tree
6561  * @index: The index to erase
6562  *
6563  * Erasing is the same as a walk to an entry then a store of a NULL to that
6564  * ENTIRE range.  In fact, it is implemented as such using the advanced API.
6565  *
6566  * Return: The entry stored at the @index or %NULL
6567  */
mtree_erase(struct maple_tree * mt,unsigned long index)6568 void *mtree_erase(struct maple_tree *mt, unsigned long index)
6569 {
6570 	void *entry = NULL;
6571 
6572 	MA_STATE(mas, mt, index, index);
6573 	trace_ma_op(__func__, &mas);
6574 
6575 	mtree_lock(mt);
6576 	entry = mas_erase(&mas);
6577 	mtree_unlock(mt);
6578 
6579 	return entry;
6580 }
6581 EXPORT_SYMBOL(mtree_erase);
6582 
6583 /*
6584  * mas_dup_free() - Free an incomplete duplication of a tree.
6585  * @mas: The maple state of a incomplete tree.
6586  *
6587  * The parameter @mas->node passed in indicates that the allocation failed on
6588  * this node. This function frees all nodes starting from @mas->node in the
6589  * reverse order of mas_dup_build(). There is no need to hold the source tree
6590  * lock at this time.
6591  */
mas_dup_free(struct ma_state * mas)6592 static void mas_dup_free(struct ma_state *mas)
6593 {
6594 	struct maple_node *node;
6595 	enum maple_type type;
6596 	void __rcu **slots;
6597 	unsigned char count, i;
6598 
6599 	/* Maybe the first node allocation failed. */
6600 	if (mas_is_none(mas))
6601 		return;
6602 
6603 	while (!mte_is_root(mas->node)) {
6604 		mas_ascend(mas);
6605 		if (mas->offset) {
6606 			mas->offset--;
6607 			do {
6608 				mas_descend(mas);
6609 				mas->offset = mas_data_end(mas);
6610 			} while (!mte_is_leaf(mas->node));
6611 
6612 			mas_ascend(mas);
6613 		}
6614 
6615 		node = mte_to_node(mas->node);
6616 		type = mte_node_type(mas->node);
6617 		slots = ma_slots(node, type);
6618 		count = mas_data_end(mas) + 1;
6619 		for (i = 0; i < count; i++)
6620 			((unsigned long *)slots)[i] &= ~MAPLE_NODE_MASK;
6621 		mt_free_bulk(count, slots);
6622 	}
6623 
6624 	node = mte_to_node(mas->node);
6625 	mt_free_one(node);
6626 }
6627 
6628 /*
6629  * mas_copy_node() - Copy a maple node and replace the parent.
6630  * @mas: The maple state of source tree.
6631  * @new_mas: The maple state of new tree.
6632  * @parent: The parent of the new node.
6633  *
6634  * Copy @mas->node to @new_mas->node, set @parent to be the parent of
6635  * @new_mas->node. If memory allocation fails, @mas is set to -ENOMEM.
6636  */
mas_copy_node(struct ma_state * mas,struct ma_state * new_mas,struct maple_pnode * parent)6637 static inline void mas_copy_node(struct ma_state *mas, struct ma_state *new_mas,
6638 		struct maple_pnode *parent)
6639 {
6640 	struct maple_node *node = mte_to_node(mas->node);
6641 	struct maple_node *new_node = mte_to_node(new_mas->node);
6642 	unsigned long val;
6643 
6644 	/* Copy the node completely. */
6645 	memcpy(new_node, node, sizeof(struct maple_node));
6646 	/* Update the parent node pointer. */
6647 	val = (unsigned long)node->parent & MAPLE_NODE_MASK;
6648 	new_node->parent = ma_parent_ptr(val | (unsigned long)parent);
6649 }
6650 
6651 /*
6652  * mas_dup_alloc() - Allocate child nodes for a maple node.
6653  * @mas: The maple state of source tree.
6654  * @new_mas: The maple state of new tree.
6655  * @gfp: The GFP_FLAGS to use for allocations.
6656  *
6657  * This function allocates child nodes for @new_mas->node during the duplication
6658  * process. If memory allocation fails, @mas is set to -ENOMEM.
6659  */
mas_dup_alloc(struct ma_state * mas,struct ma_state * new_mas,gfp_t gfp)6660 static inline void mas_dup_alloc(struct ma_state *mas, struct ma_state *new_mas,
6661 		gfp_t gfp)
6662 {
6663 	struct maple_node *node = mte_to_node(mas->node);
6664 	struct maple_node *new_node = mte_to_node(new_mas->node);
6665 	enum maple_type type;
6666 	unsigned char request, count, i;
6667 	void __rcu **slots;
6668 	void __rcu **new_slots;
6669 	unsigned long val;
6670 
6671 	/* Allocate memory for child nodes. */
6672 	type = mte_node_type(mas->node);
6673 	new_slots = ma_slots(new_node, type);
6674 	request = mas_data_end(mas) + 1;
6675 	count = mt_alloc_bulk(gfp, request, (void **)new_slots);
6676 	if (unlikely(count < request)) {
6677 		memset(new_slots, 0, request * sizeof(void *));
6678 		mas_set_err(mas, -ENOMEM);
6679 		return;
6680 	}
6681 
6682 	/* Restore node type information in slots. */
6683 	slots = ma_slots(node, type);
6684 	for (i = 0; i < count; i++) {
6685 		val = (unsigned long)mt_slot_locked(mas->tree, slots, i);
6686 		val &= MAPLE_NODE_MASK;
6687 		((unsigned long *)new_slots)[i] |= val;
6688 	}
6689 }
6690 
6691 /*
6692  * mas_dup_build() - Build a new maple tree from a source tree
6693  * @mas: The maple state of source tree, need to be in MAS_START state.
6694  * @new_mas: The maple state of new tree, need to be in MAS_START state.
6695  * @gfp: The GFP_FLAGS to use for allocations.
6696  *
6697  * This function builds a new tree in DFS preorder. If the memory allocation
6698  * fails, the error code -ENOMEM will be set in @mas, and @new_mas points to the
6699  * last node. mas_dup_free() will free the incomplete duplication of a tree.
6700  *
6701  * Note that the attributes of the two trees need to be exactly the same, and the
6702  * new tree needs to be empty, otherwise -EINVAL will be set in @mas.
6703  */
mas_dup_build(struct ma_state * mas,struct ma_state * new_mas,gfp_t gfp)6704 static inline void mas_dup_build(struct ma_state *mas, struct ma_state *new_mas,
6705 		gfp_t gfp)
6706 {
6707 	struct maple_node *node;
6708 	struct maple_pnode *parent = NULL;
6709 	struct maple_enode *root;
6710 	enum maple_type type;
6711 
6712 	if (unlikely(mt_attr(mas->tree) != mt_attr(new_mas->tree)) ||
6713 	    unlikely(!mtree_empty(new_mas->tree))) {
6714 		mas_set_err(mas, -EINVAL);
6715 		return;
6716 	}
6717 
6718 	root = mas_start(mas);
6719 	if (mas_is_ptr(mas) || mas_is_none(mas))
6720 		goto set_new_tree;
6721 
6722 	node = mt_alloc_one(gfp);
6723 	if (!node) {
6724 		new_mas->status = ma_none;
6725 		mas_set_err(mas, -ENOMEM);
6726 		return;
6727 	}
6728 
6729 	type = mte_node_type(mas->node);
6730 	root = mt_mk_node(node, type);
6731 	new_mas->node = root;
6732 	new_mas->min = 0;
6733 	new_mas->max = ULONG_MAX;
6734 	root = mte_mk_root(root);
6735 	while (1) {
6736 		mas_copy_node(mas, new_mas, parent);
6737 		if (!mte_is_leaf(mas->node)) {
6738 			/* Only allocate child nodes for non-leaf nodes. */
6739 			mas_dup_alloc(mas, new_mas, gfp);
6740 			if (unlikely(mas_is_err(mas)))
6741 				return;
6742 		} else {
6743 			/*
6744 			 * This is the last leaf node and duplication is
6745 			 * completed.
6746 			 */
6747 			if (mas->max == ULONG_MAX)
6748 				goto done;
6749 
6750 			/* This is not the last leaf node and needs to go up. */
6751 			do {
6752 				mas_ascend(mas);
6753 				mas_ascend(new_mas);
6754 			} while (mas->offset == mas_data_end(mas));
6755 
6756 			/* Move to the next subtree. */
6757 			mas->offset++;
6758 			new_mas->offset++;
6759 		}
6760 
6761 		mas_descend(mas);
6762 		parent = ma_parent_ptr(mte_to_node(new_mas->node));
6763 		mas_descend(new_mas);
6764 		mas->offset = 0;
6765 		new_mas->offset = 0;
6766 	}
6767 done:
6768 	/* Specially handle the parent of the root node. */
6769 	mte_to_node(root)->parent = ma_parent_ptr(mas_tree_parent(new_mas));
6770 set_new_tree:
6771 	/* Make them the same height */
6772 	new_mas->tree->ma_flags = mas->tree->ma_flags;
6773 	rcu_assign_pointer(new_mas->tree->ma_root, root);
6774 }
6775 
6776 /**
6777  * __mt_dup(): Duplicate an entire maple tree
6778  * @mt: The source maple tree
6779  * @new: The new maple tree
6780  * @gfp: The GFP_FLAGS to use for allocations
6781  *
6782  * This function duplicates a maple tree in Depth-First Search (DFS) pre-order
6783  * traversal. It uses memcpy() to copy nodes in the source tree and allocate
6784  * new child nodes in non-leaf nodes. The new node is exactly the same as the
6785  * source node except for all the addresses stored in it. It will be faster than
6786  * traversing all elements in the source tree and inserting them one by one into
6787  * the new tree.
6788  * The user needs to ensure that the attributes of the source tree and the new
6789  * tree are the same, and the new tree needs to be an empty tree, otherwise
6790  * -EINVAL will be returned.
6791  * Note that the user needs to manually lock the source tree and the new tree.
6792  *
6793  * Return: 0 on success, -ENOMEM if memory could not be allocated, -EINVAL If
6794  * the attributes of the two trees are different or the new tree is not an empty
6795  * tree.
6796  */
__mt_dup(struct maple_tree * mt,struct maple_tree * new,gfp_t gfp)6797 int __mt_dup(struct maple_tree *mt, struct maple_tree *new, gfp_t gfp)
6798 {
6799 	int ret = 0;
6800 	MA_STATE(mas, mt, 0, 0);
6801 	MA_STATE(new_mas, new, 0, 0);
6802 
6803 	mas_dup_build(&mas, &new_mas, gfp);
6804 	if (unlikely(mas_is_err(&mas))) {
6805 		ret = xa_err(mas.node);
6806 		if (ret == -ENOMEM)
6807 			mas_dup_free(&new_mas);
6808 	}
6809 
6810 	return ret;
6811 }
6812 EXPORT_SYMBOL(__mt_dup);
6813 
6814 /**
6815  * mtree_dup(): Duplicate an entire maple tree
6816  * @mt: The source maple tree
6817  * @new: The new maple tree
6818  * @gfp: The GFP_FLAGS to use for allocations
6819  *
6820  * This function duplicates a maple tree in Depth-First Search (DFS) pre-order
6821  * traversal. It uses memcpy() to copy nodes in the source tree and allocate
6822  * new child nodes in non-leaf nodes. The new node is exactly the same as the
6823  * source node except for all the addresses stored in it. It will be faster than
6824  * traversing all elements in the source tree and inserting them one by one into
6825  * the new tree.
6826  * The user needs to ensure that the attributes of the source tree and the new
6827  * tree are the same, and the new tree needs to be an empty tree, otherwise
6828  * -EINVAL will be returned.
6829  *
6830  * Return: 0 on success, -ENOMEM if memory could not be allocated, -EINVAL If
6831  * the attributes of the two trees are different or the new tree is not an empty
6832  * tree.
6833  */
mtree_dup(struct maple_tree * mt,struct maple_tree * new,gfp_t gfp)6834 int mtree_dup(struct maple_tree *mt, struct maple_tree *new, gfp_t gfp)
6835 {
6836 	int ret = 0;
6837 	MA_STATE(mas, mt, 0, 0);
6838 	MA_STATE(new_mas, new, 0, 0);
6839 
6840 	mas_lock(&new_mas);
6841 	mas_lock_nested(&mas, SINGLE_DEPTH_NESTING);
6842 	mas_dup_build(&mas, &new_mas, gfp);
6843 	mas_unlock(&mas);
6844 	if (unlikely(mas_is_err(&mas))) {
6845 		ret = xa_err(mas.node);
6846 		if (ret == -ENOMEM)
6847 			mas_dup_free(&new_mas);
6848 	}
6849 
6850 	mas_unlock(&new_mas);
6851 	return ret;
6852 }
6853 EXPORT_SYMBOL(mtree_dup);
6854 
6855 /**
6856  * __mt_destroy() - Walk and free all nodes of a locked maple tree.
6857  * @mt: The maple tree
6858  *
6859  * Note: Does not handle locking.
6860  */
__mt_destroy(struct maple_tree * mt)6861 void __mt_destroy(struct maple_tree *mt)
6862 {
6863 	void *root = mt_root_locked(mt);
6864 
6865 	rcu_assign_pointer(mt->ma_root, NULL);
6866 	if (xa_is_node(root))
6867 		mte_destroy_walk(root, mt);
6868 
6869 	mt->ma_flags = mt_attr(mt);
6870 }
6871 EXPORT_SYMBOL_GPL(__mt_destroy);
6872 
6873 /**
6874  * mtree_destroy() - Destroy a maple tree
6875  * @mt: The maple tree
6876  *
6877  * Frees all resources used by the tree.  Handles locking.
6878  */
mtree_destroy(struct maple_tree * mt)6879 void mtree_destroy(struct maple_tree *mt)
6880 {
6881 	mtree_lock(mt);
6882 	__mt_destroy(mt);
6883 	mtree_unlock(mt);
6884 }
6885 EXPORT_SYMBOL(mtree_destroy);
6886 
6887 /**
6888  * mt_find() - Search from the start up until an entry is found.
6889  * @mt: The maple tree
6890  * @index: Pointer which contains the start location of the search
6891  * @max: The maximum value of the search range
6892  *
6893  * Takes RCU read lock internally to protect the search, which does not
6894  * protect the returned pointer after dropping RCU read lock.
6895  * See also: Documentation/core-api/maple_tree.rst
6896  *
6897  * In case that an entry is found @index is updated to point to the next
6898  * possible entry independent whether the found entry is occupying a
6899  * single index or a range if indices.
6900  *
6901  * Return: The entry at or after the @index or %NULL
6902  */
mt_find(struct maple_tree * mt,unsigned long * index,unsigned long max)6903 void *mt_find(struct maple_tree *mt, unsigned long *index, unsigned long max)
6904 {
6905 	MA_STATE(mas, mt, *index, *index);
6906 	void *entry;
6907 #ifdef CONFIG_DEBUG_MAPLE_TREE
6908 	unsigned long copy = *index;
6909 #endif
6910 
6911 	trace_ma_read(__func__, &mas);
6912 
6913 	if ((*index) > max)
6914 		return NULL;
6915 
6916 	rcu_read_lock();
6917 retry:
6918 	entry = mas_state_walk(&mas);
6919 	if (mas_is_start(&mas))
6920 		goto retry;
6921 
6922 	if (unlikely(xa_is_zero(entry)))
6923 		entry = NULL;
6924 
6925 	if (entry)
6926 		goto unlock;
6927 
6928 	while (mas_is_active(&mas) && (mas.last < max)) {
6929 		entry = mas_next_slot(&mas, max, false);
6930 		if (likely(entry && !xa_is_zero(entry)))
6931 			break;
6932 	}
6933 
6934 	if (unlikely(xa_is_zero(entry)))
6935 		entry = NULL;
6936 unlock:
6937 	rcu_read_unlock();
6938 	if (likely(entry)) {
6939 		*index = mas.last + 1;
6940 #ifdef CONFIG_DEBUG_MAPLE_TREE
6941 		if (MT_WARN_ON(mt, (*index) && ((*index) <= copy)))
6942 			pr_err("index not increased! %lx <= %lx\n",
6943 			       *index, copy);
6944 #endif
6945 	}
6946 
6947 	return entry;
6948 }
6949 EXPORT_SYMBOL(mt_find);
6950 
6951 /**
6952  * mt_find_after() - Search from the start up until an entry is found.
6953  * @mt: The maple tree
6954  * @index: Pointer which contains the start location of the search
6955  * @max: The maximum value to check
6956  *
6957  * Same as mt_find() except that it checks @index for 0 before
6958  * searching. If @index == 0, the search is aborted. This covers a wrap
6959  * around of @index to 0 in an iterator loop.
6960  *
6961  * Return: The entry at or after the @index or %NULL
6962  */
mt_find_after(struct maple_tree * mt,unsigned long * index,unsigned long max)6963 void *mt_find_after(struct maple_tree *mt, unsigned long *index,
6964 		    unsigned long max)
6965 {
6966 	if (!(*index))
6967 		return NULL;
6968 
6969 	return mt_find(mt, index, max);
6970 }
6971 EXPORT_SYMBOL(mt_find_after);
6972 
6973 #ifdef CONFIG_DEBUG_MAPLE_TREE
6974 atomic_t maple_tree_tests_run;
6975 EXPORT_SYMBOL_GPL(maple_tree_tests_run);
6976 atomic_t maple_tree_tests_passed;
6977 EXPORT_SYMBOL_GPL(maple_tree_tests_passed);
6978 
6979 #ifndef __KERNEL__
6980 extern void kmem_cache_set_non_kernel(struct kmem_cache *, unsigned int);
mt_set_non_kernel(unsigned int val)6981 void mt_set_non_kernel(unsigned int val)
6982 {
6983 	kmem_cache_set_non_kernel(maple_node_cache, val);
6984 }
6985 
6986 extern void kmem_cache_set_callback(struct kmem_cache *cachep,
6987 		void (*callback)(void *));
mt_set_callback(void (* callback)(void *))6988 void mt_set_callback(void (*callback)(void *))
6989 {
6990 	kmem_cache_set_callback(maple_node_cache, callback);
6991 }
6992 
6993 extern void kmem_cache_set_private(struct kmem_cache *cachep, void *private);
mt_set_private(void * private)6994 void mt_set_private(void *private)
6995 {
6996 	kmem_cache_set_private(maple_node_cache, private);
6997 }
6998 
6999 extern unsigned long kmem_cache_get_alloc(struct kmem_cache *);
mt_get_alloc_size(void)7000 unsigned long mt_get_alloc_size(void)
7001 {
7002 	return kmem_cache_get_alloc(maple_node_cache);
7003 }
7004 
7005 extern void kmem_cache_zero_nr_tallocated(struct kmem_cache *);
mt_zero_nr_tallocated(void)7006 void mt_zero_nr_tallocated(void)
7007 {
7008 	kmem_cache_zero_nr_tallocated(maple_node_cache);
7009 }
7010 
7011 extern unsigned int kmem_cache_nr_tallocated(struct kmem_cache *);
mt_nr_tallocated(void)7012 unsigned int mt_nr_tallocated(void)
7013 {
7014 	return kmem_cache_nr_tallocated(maple_node_cache);
7015 }
7016 
7017 extern unsigned int kmem_cache_nr_allocated(struct kmem_cache *);
mt_nr_allocated(void)7018 unsigned int mt_nr_allocated(void)
7019 {
7020 	return kmem_cache_nr_allocated(maple_node_cache);
7021 }
7022 
mt_cache_shrink(void)7023 void mt_cache_shrink(void)
7024 {
7025 }
7026 #else
7027 /*
7028  * mt_cache_shrink() - For testing, don't use this.
7029  *
7030  * Certain testcases can trigger an OOM when combined with other memory
7031  * debugging configuration options.  This function is used to reduce the
7032  * possibility of an out of memory even due to kmem_cache objects remaining
7033  * around for longer than usual.
7034  */
mt_cache_shrink(void)7035 void mt_cache_shrink(void)
7036 {
7037 	kmem_cache_shrink(maple_node_cache);
7038 
7039 }
7040 EXPORT_SYMBOL_GPL(mt_cache_shrink);
7041 
7042 #endif /* not defined __KERNEL__ */
7043 /*
7044  * mas_get_slot() - Get the entry in the maple state node stored at @offset.
7045  * @mas: The maple state
7046  * @offset: The offset into the slot array to fetch.
7047  *
7048  * Return: The entry stored at @offset.
7049  */
mas_get_slot(struct ma_state * mas,unsigned char offset)7050 static inline struct maple_enode *mas_get_slot(struct ma_state *mas,
7051 		unsigned char offset)
7052 {
7053 	return mas_slot(mas, ma_slots(mas_mn(mas), mte_node_type(mas->node)),
7054 			offset);
7055 }
7056 
7057 /* Depth first search, post-order */
mas_dfs_postorder(struct ma_state * mas,unsigned long max)7058 static void mas_dfs_postorder(struct ma_state *mas, unsigned long max)
7059 {
7060 
7061 	struct maple_enode *p, *mn = mas->node;
7062 	unsigned long p_min, p_max;
7063 
7064 	mas_next_node(mas, mas_mn(mas), max);
7065 	if (!mas_is_overflow(mas))
7066 		return;
7067 
7068 	if (mte_is_root(mn))
7069 		return;
7070 
7071 	mas->node = mn;
7072 	mas_ascend(mas);
7073 	do {
7074 		p = mas->node;
7075 		p_min = mas->min;
7076 		p_max = mas->max;
7077 		mas_prev_node(mas, 0);
7078 	} while (!mas_is_underflow(mas));
7079 
7080 	mas->node = p;
7081 	mas->max = p_max;
7082 	mas->min = p_min;
7083 }
7084 
7085 /* Tree validations */
7086 static void mt_dump_node(const struct maple_tree *mt, void *entry,
7087 		unsigned long min, unsigned long max, unsigned int depth,
7088 		enum mt_dump_format format);
mt_dump_range(unsigned long min,unsigned long max,unsigned int depth,enum mt_dump_format format)7089 static void mt_dump_range(unsigned long min, unsigned long max,
7090 			  unsigned int depth, enum mt_dump_format format)
7091 {
7092 	static const char spaces[] = "                                ";
7093 
7094 	switch(format) {
7095 	case mt_dump_hex:
7096 		if (min == max)
7097 			pr_info("%.*s%lx: ", depth * 2, spaces, min);
7098 		else
7099 			pr_info("%.*s%lx-%lx: ", depth * 2, spaces, min, max);
7100 		break;
7101 	case mt_dump_dec:
7102 		if (min == max)
7103 			pr_info("%.*s%lu: ", depth * 2, spaces, min);
7104 		else
7105 			pr_info("%.*s%lu-%lu: ", depth * 2, spaces, min, max);
7106 	}
7107 }
7108 
mt_dump_entry(void * entry,unsigned long min,unsigned long max,unsigned int depth,enum mt_dump_format format)7109 static void mt_dump_entry(void *entry, unsigned long min, unsigned long max,
7110 			  unsigned int depth, enum mt_dump_format format)
7111 {
7112 	mt_dump_range(min, max, depth, format);
7113 
7114 	if (xa_is_value(entry))
7115 		pr_cont("value %ld (0x%lx) [" PTR_FMT "]\n", xa_to_value(entry),
7116 			xa_to_value(entry), entry);
7117 	else if (xa_is_zero(entry))
7118 		pr_cont("zero (%ld)\n", xa_to_internal(entry));
7119 	else if (mt_is_reserved(entry))
7120 		pr_cont("UNKNOWN ENTRY (" PTR_FMT ")\n", entry);
7121 	else
7122 		pr_cont(PTR_FMT "\n", entry);
7123 }
7124 
mt_dump_range64(const struct maple_tree * mt,void * entry,unsigned long min,unsigned long max,unsigned int depth,enum mt_dump_format format)7125 static void mt_dump_range64(const struct maple_tree *mt, void *entry,
7126 		unsigned long min, unsigned long max, unsigned int depth,
7127 		enum mt_dump_format format)
7128 {
7129 	struct maple_range_64 *node = &mte_to_node(entry)->mr64;
7130 	bool leaf = mte_is_leaf(entry);
7131 	unsigned long first = min;
7132 	int i;
7133 
7134 	pr_cont(" contents: ");
7135 	for (i = 0; i < MAPLE_RANGE64_SLOTS - 1; i++) {
7136 		switch(format) {
7137 		case mt_dump_hex:
7138 			pr_cont(PTR_FMT " %lX ", node->slot[i], node->pivot[i]);
7139 			break;
7140 		case mt_dump_dec:
7141 			pr_cont(PTR_FMT " %lu ", node->slot[i], node->pivot[i]);
7142 		}
7143 	}
7144 	pr_cont(PTR_FMT "\n", node->slot[i]);
7145 	for (i = 0; i < MAPLE_RANGE64_SLOTS; i++) {
7146 		unsigned long last = max;
7147 
7148 		if (i < (MAPLE_RANGE64_SLOTS - 1))
7149 			last = node->pivot[i];
7150 		else if (!node->slot[i] && max != mt_node_max(entry))
7151 			break;
7152 		if (last == 0 && i > 0)
7153 			break;
7154 		if (leaf)
7155 			mt_dump_entry(mt_slot(mt, node->slot, i),
7156 					first, last, depth + 1, format);
7157 		else if (node->slot[i])
7158 			mt_dump_node(mt, mt_slot(mt, node->slot, i),
7159 					first, last, depth + 1, format);
7160 
7161 		if (last == max)
7162 			break;
7163 		if (last > max) {
7164 			switch(format) {
7165 			case mt_dump_hex:
7166 				pr_err("node " PTR_FMT " last (%lx) > max (%lx) at pivot %d!\n",
7167 					node, last, max, i);
7168 				break;
7169 			case mt_dump_dec:
7170 				pr_err("node " PTR_FMT " last (%lu) > max (%lu) at pivot %d!\n",
7171 					node, last, max, i);
7172 			}
7173 		}
7174 		first = last + 1;
7175 	}
7176 }
7177 
mt_dump_arange64(const struct maple_tree * mt,void * entry,unsigned long min,unsigned long max,unsigned int depth,enum mt_dump_format format)7178 static void mt_dump_arange64(const struct maple_tree *mt, void *entry,
7179 	unsigned long min, unsigned long max, unsigned int depth,
7180 	enum mt_dump_format format)
7181 {
7182 	struct maple_arange_64 *node = &mte_to_node(entry)->ma64;
7183 	unsigned long first = min;
7184 	int i;
7185 
7186 	pr_cont(" contents: ");
7187 	for (i = 0; i < MAPLE_ARANGE64_SLOTS; i++) {
7188 		switch (format) {
7189 		case mt_dump_hex:
7190 			pr_cont("%lx ", node->gap[i]);
7191 			break;
7192 		case mt_dump_dec:
7193 			pr_cont("%lu ", node->gap[i]);
7194 		}
7195 	}
7196 	pr_cont("| %02X %02X| ", node->meta.end, node->meta.gap);
7197 	for (i = 0; i < MAPLE_ARANGE64_SLOTS - 1; i++) {
7198 		switch (format) {
7199 		case mt_dump_hex:
7200 			pr_cont(PTR_FMT " %lX ", node->slot[i], node->pivot[i]);
7201 			break;
7202 		case mt_dump_dec:
7203 			pr_cont(PTR_FMT " %lu ", node->slot[i], node->pivot[i]);
7204 		}
7205 	}
7206 	pr_cont(PTR_FMT "\n", node->slot[i]);
7207 	for (i = 0; i < MAPLE_ARANGE64_SLOTS; i++) {
7208 		unsigned long last = max;
7209 
7210 		if (i < (MAPLE_ARANGE64_SLOTS - 1))
7211 			last = node->pivot[i];
7212 		else if (!node->slot[i])
7213 			break;
7214 		if (last == 0 && i > 0)
7215 			break;
7216 		if (node->slot[i])
7217 			mt_dump_node(mt, mt_slot(mt, node->slot, i),
7218 					first, last, depth + 1, format);
7219 
7220 		if (last == max)
7221 			break;
7222 		if (last > max) {
7223 			switch(format) {
7224 			case mt_dump_hex:
7225 				pr_err("node " PTR_FMT " last (%lx) > max (%lx) at pivot %d!\n",
7226 					node, last, max, i);
7227 				break;
7228 			case mt_dump_dec:
7229 				pr_err("node " PTR_FMT " last (%lu) > max (%lu) at pivot %d!\n",
7230 					node, last, max, i);
7231 			}
7232 		}
7233 		first = last + 1;
7234 	}
7235 }
7236 
mt_dump_node(const struct maple_tree * mt,void * entry,unsigned long min,unsigned long max,unsigned int depth,enum mt_dump_format format)7237 static void mt_dump_node(const struct maple_tree *mt, void *entry,
7238 		unsigned long min, unsigned long max, unsigned int depth,
7239 		enum mt_dump_format format)
7240 {
7241 	struct maple_node *node = mte_to_node(entry);
7242 	unsigned int type = mte_node_type(entry);
7243 	unsigned int i;
7244 
7245 	mt_dump_range(min, max, depth, format);
7246 
7247 	pr_cont("node " PTR_FMT " depth %d type %d parent " PTR_FMT, node,
7248 		depth, type, node ? node->parent : NULL);
7249 	switch (type) {
7250 	case maple_dense:
7251 		pr_cont("\n");
7252 		for (i = 0; i < MAPLE_NODE_SLOTS; i++) {
7253 			if (min + i > max)
7254 				pr_cont("OUT OF RANGE: ");
7255 			mt_dump_entry(mt_slot(mt, node->slot, i),
7256 					min + i, min + i, depth, format);
7257 		}
7258 		break;
7259 	case maple_leaf_64:
7260 	case maple_range_64:
7261 		mt_dump_range64(mt, entry, min, max, depth, format);
7262 		break;
7263 	case maple_arange_64:
7264 		mt_dump_arange64(mt, entry, min, max, depth, format);
7265 		break;
7266 
7267 	default:
7268 		pr_cont(" UNKNOWN TYPE\n");
7269 	}
7270 }
7271 
mt_dump(const struct maple_tree * mt,enum mt_dump_format format)7272 void mt_dump(const struct maple_tree *mt, enum mt_dump_format format)
7273 {
7274 	void *entry = rcu_dereference_check(mt->ma_root, mt_locked(mt));
7275 
7276 	pr_info("maple_tree(" PTR_FMT ") flags %X, height %u root " PTR_FMT "\n",
7277 		 mt, mt->ma_flags, mt_height(mt), entry);
7278 	if (xa_is_node(entry))
7279 		mt_dump_node(mt, entry, 0, mt_node_max(entry), 0, format);
7280 	else if (entry)
7281 		mt_dump_entry(entry, 0, 0, 0, format);
7282 	else
7283 		pr_info("(empty)\n");
7284 }
7285 EXPORT_SYMBOL_GPL(mt_dump);
7286 
7287 /*
7288  * Calculate the maximum gap in a node and check if that's what is reported in
7289  * the parent (unless root).
7290  */
mas_validate_gaps(struct ma_state * mas)7291 static void mas_validate_gaps(struct ma_state *mas)
7292 {
7293 	struct maple_enode *mte = mas->node;
7294 	struct maple_node *p_mn, *node = mte_to_node(mte);
7295 	enum maple_type mt = mte_node_type(mas->node);
7296 	unsigned long gap = 0, max_gap = 0;
7297 	unsigned long p_end, p_start = mas->min;
7298 	unsigned char p_slot, offset;
7299 	unsigned long *gaps = NULL;
7300 	unsigned long *pivots = ma_pivots(node, mt);
7301 	unsigned int i;
7302 
7303 	if (ma_is_dense(mt)) {
7304 		for (i = 0; i < mt_slot_count(mte); i++) {
7305 			if (mas_get_slot(mas, i)) {
7306 				if (gap > max_gap)
7307 					max_gap = gap;
7308 				gap = 0;
7309 				continue;
7310 			}
7311 			gap++;
7312 		}
7313 		goto counted;
7314 	}
7315 
7316 	gaps = ma_gaps(node, mt);
7317 	for (i = 0; i < mt_slot_count(mte); i++) {
7318 		p_end = mas_safe_pivot(mas, pivots, i, mt);
7319 
7320 		if (!gaps) {
7321 			if (!mas_get_slot(mas, i))
7322 				gap = p_end - p_start + 1;
7323 		} else {
7324 			void *entry = mas_get_slot(mas, i);
7325 
7326 			gap = gaps[i];
7327 			MT_BUG_ON(mas->tree, !entry);
7328 
7329 			if (gap > p_end - p_start + 1) {
7330 				pr_err(PTR_FMT "[%u] %lu >= %lu - %lu + 1 (%lu)\n",
7331 				       mas_mn(mas), i, gap, p_end, p_start,
7332 				       p_end - p_start + 1);
7333 				MT_BUG_ON(mas->tree, gap > p_end - p_start + 1);
7334 			}
7335 		}
7336 
7337 		if (gap > max_gap)
7338 			max_gap = gap;
7339 
7340 		p_start = p_end + 1;
7341 		if (p_end >= mas->max)
7342 			break;
7343 	}
7344 
7345 counted:
7346 	if (mt == maple_arange_64) {
7347 		MT_BUG_ON(mas->tree, !gaps);
7348 		offset = ma_meta_gap(node);
7349 		if (offset > i) {
7350 			pr_err("gap offset " PTR_FMT "[%u] is invalid\n", node, offset);
7351 			MT_BUG_ON(mas->tree, 1);
7352 		}
7353 
7354 		if (gaps[offset] != max_gap) {
7355 			pr_err("gap " PTR_FMT "[%u] is not the largest gap %lu\n",
7356 			       node, offset, max_gap);
7357 			MT_BUG_ON(mas->tree, 1);
7358 		}
7359 
7360 		for (i++ ; i < mt_slot_count(mte); i++) {
7361 			if (gaps[i] != 0) {
7362 				pr_err("gap " PTR_FMT "[%u] beyond node limit != 0\n",
7363 				       node, i);
7364 				MT_BUG_ON(mas->tree, 1);
7365 			}
7366 		}
7367 	}
7368 
7369 	if (mte_is_root(mte))
7370 		return;
7371 
7372 	p_slot = mte_parent_slot(mas->node);
7373 	p_mn = mte_parent(mte);
7374 	MT_BUG_ON(mas->tree, max_gap > mas->max);
7375 	if (ma_gaps(p_mn, mas_parent_type(mas, mte))[p_slot] != max_gap) {
7376 		pr_err("gap " PTR_FMT "[%u] != %lu\n", p_mn, p_slot, max_gap);
7377 		mt_dump(mas->tree, mt_dump_hex);
7378 		MT_BUG_ON(mas->tree, 1);
7379 	}
7380 }
7381 
mas_validate_parent_slot(struct ma_state * mas)7382 static void mas_validate_parent_slot(struct ma_state *mas)
7383 {
7384 	struct maple_node *parent;
7385 	struct maple_enode *node;
7386 	enum maple_type p_type;
7387 	unsigned char p_slot;
7388 	void __rcu **slots;
7389 	int i;
7390 
7391 	if (mte_is_root(mas->node))
7392 		return;
7393 
7394 	p_slot = mte_parent_slot(mas->node);
7395 	p_type = mas_parent_type(mas, mas->node);
7396 	parent = mte_parent(mas->node);
7397 	slots = ma_slots(parent, p_type);
7398 	MT_BUG_ON(mas->tree, mas_mn(mas) == parent);
7399 
7400 	/* Check prev/next parent slot for duplicate node entry */
7401 
7402 	for (i = 0; i < mt_slots[p_type]; i++) {
7403 		node = mas_slot(mas, slots, i);
7404 		if (i == p_slot) {
7405 			if (node != mas->node)
7406 				pr_err("parent " PTR_FMT "[%u] does not have " PTR_FMT "\n",
7407 					parent, i, mas_mn(mas));
7408 			MT_BUG_ON(mas->tree, node != mas->node);
7409 		} else if (node == mas->node) {
7410 			pr_err("Invalid child " PTR_FMT " at parent " PTR_FMT "[%u] p_slot %u\n",
7411 			       mas_mn(mas), parent, i, p_slot);
7412 			MT_BUG_ON(mas->tree, node == mas->node);
7413 		}
7414 	}
7415 }
7416 
mas_validate_child_slot(struct ma_state * mas)7417 static void mas_validate_child_slot(struct ma_state *mas)
7418 {
7419 	enum maple_type type = mte_node_type(mas->node);
7420 	void __rcu **slots = ma_slots(mte_to_node(mas->node), type);
7421 	unsigned long *pivots = ma_pivots(mte_to_node(mas->node), type);
7422 	struct maple_enode *child;
7423 	unsigned char i;
7424 
7425 	if (mte_is_leaf(mas->node))
7426 		return;
7427 
7428 	for (i = 0; i < mt_slots[type]; i++) {
7429 		child = mas_slot(mas, slots, i);
7430 
7431 		if (!child) {
7432 			pr_err("Non-leaf node lacks child at " PTR_FMT "[%u]\n",
7433 			       mas_mn(mas), i);
7434 			MT_BUG_ON(mas->tree, 1);
7435 		}
7436 
7437 		if (mte_parent_slot(child) != i) {
7438 			pr_err("Slot error at " PTR_FMT "[%u]: child " PTR_FMT " has pslot %u\n",
7439 			       mas_mn(mas), i, mte_to_node(child),
7440 			       mte_parent_slot(child));
7441 			MT_BUG_ON(mas->tree, 1);
7442 		}
7443 
7444 		if (mte_parent(child) != mte_to_node(mas->node)) {
7445 			pr_err("child " PTR_FMT " has parent " PTR_FMT " not " PTR_FMT "\n",
7446 			       mte_to_node(child), mte_parent(child),
7447 			       mte_to_node(mas->node));
7448 			MT_BUG_ON(mas->tree, 1);
7449 		}
7450 
7451 		if (i < mt_pivots[type] && pivots[i] == mas->max)
7452 			break;
7453 	}
7454 }
7455 
7456 /*
7457  * Validate all pivots are within mas->min and mas->max, check metadata ends
7458  * where the maximum ends and ensure there is no slots or pivots set outside of
7459  * the end of the data.
7460  */
mas_validate_limits(struct ma_state * mas)7461 static void mas_validate_limits(struct ma_state *mas)
7462 {
7463 	int i;
7464 	unsigned long prev_piv = 0;
7465 	enum maple_type type = mte_node_type(mas->node);
7466 	void __rcu **slots = ma_slots(mte_to_node(mas->node), type);
7467 	unsigned long *pivots = ma_pivots(mas_mn(mas), type);
7468 
7469 	for (i = 0; i < mt_slots[type]; i++) {
7470 		unsigned long piv;
7471 
7472 		piv = mas_safe_pivot(mas, pivots, i, type);
7473 
7474 		if (!piv && (i != 0)) {
7475 			pr_err("Missing node limit pivot at " PTR_FMT "[%u]",
7476 			       mas_mn(mas), i);
7477 			MAS_WARN_ON(mas, 1);
7478 		}
7479 
7480 		if (prev_piv > piv) {
7481 			pr_err(PTR_FMT "[%u] piv %lu < prev_piv %lu\n",
7482 				mas_mn(mas), i, piv, prev_piv);
7483 			MAS_WARN_ON(mas, piv < prev_piv);
7484 		}
7485 
7486 		if (piv < mas->min) {
7487 			pr_err(PTR_FMT "[%u] %lu < %lu\n", mas_mn(mas), i,
7488 				piv, mas->min);
7489 			MAS_WARN_ON(mas, piv < mas->min);
7490 		}
7491 		if (piv > mas->max) {
7492 			pr_err(PTR_FMT "[%u] %lu > %lu\n", mas_mn(mas), i,
7493 				piv, mas->max);
7494 			MAS_WARN_ON(mas, piv > mas->max);
7495 		}
7496 		prev_piv = piv;
7497 		if (piv == mas->max)
7498 			break;
7499 	}
7500 
7501 	if (mas_data_end(mas) != i) {
7502 		pr_err("node" PTR_FMT ": data_end %u != the last slot offset %u\n",
7503 		       mas_mn(mas), mas_data_end(mas), i);
7504 		MT_BUG_ON(mas->tree, 1);
7505 	}
7506 
7507 	for (i += 1; i < mt_slots[type]; i++) {
7508 		void *entry = mas_slot(mas, slots, i);
7509 
7510 		if (entry && (i != mt_slots[type] - 1)) {
7511 			pr_err(PTR_FMT "[%u] should not have entry " PTR_FMT "\n",
7512 			       mas_mn(mas), i, entry);
7513 			MT_BUG_ON(mas->tree, entry != NULL);
7514 		}
7515 
7516 		if (i < mt_pivots[type]) {
7517 			unsigned long piv = pivots[i];
7518 
7519 			if (!piv)
7520 				continue;
7521 
7522 			pr_err(PTR_FMT "[%u] should not have piv %lu\n",
7523 			       mas_mn(mas), i, piv);
7524 			MAS_WARN_ON(mas, i < mt_pivots[type] - 1);
7525 		}
7526 	}
7527 }
7528 
mt_validate_nulls(struct maple_tree * mt)7529 static void mt_validate_nulls(struct maple_tree *mt)
7530 {
7531 	void *entry, *last = (void *)1;
7532 	unsigned char offset = 0;
7533 	void __rcu **slots;
7534 	MA_STATE(mas, mt, 0, 0);
7535 
7536 	mas_start(&mas);
7537 	if (mas_is_none(&mas) || (mas_is_ptr(&mas)))
7538 		return;
7539 
7540 	while (!mte_is_leaf(mas.node))
7541 		mas_descend(&mas);
7542 
7543 	slots = ma_slots(mte_to_node(mas.node), mte_node_type(mas.node));
7544 	do {
7545 		entry = mas_slot(&mas, slots, offset);
7546 		if (!last && !entry) {
7547 			pr_err("Sequential nulls end at " PTR_FMT "[%u]\n",
7548 				mas_mn(&mas), offset);
7549 		}
7550 		MT_BUG_ON(mt, !last && !entry);
7551 		last = entry;
7552 		if (offset == mas_data_end(&mas)) {
7553 			mas_next_node(&mas, mas_mn(&mas), ULONG_MAX);
7554 			if (mas_is_overflow(&mas))
7555 				return;
7556 			offset = 0;
7557 			slots = ma_slots(mte_to_node(mas.node),
7558 					 mte_node_type(mas.node));
7559 		} else {
7560 			offset++;
7561 		}
7562 
7563 	} while (!mas_is_overflow(&mas));
7564 }
7565 
7566 /*
7567  * validate a maple tree by checking:
7568  * 1. The limits (pivots are within mas->min to mas->max)
7569  * 2. The gap is correctly set in the parents
7570  */
mt_validate(struct maple_tree * mt)7571 void mt_validate(struct maple_tree *mt)
7572 	__must_hold(mas->tree->ma_lock)
7573 {
7574 	unsigned char end;
7575 
7576 	MA_STATE(mas, mt, 0, 0);
7577 	mas_start(&mas);
7578 	if (!mas_is_active(&mas))
7579 		return;
7580 
7581 	while (!mte_is_leaf(mas.node))
7582 		mas_descend(&mas);
7583 
7584 	while (!mas_is_overflow(&mas)) {
7585 		MAS_WARN_ON(&mas, mte_dead_node(mas.node));
7586 		end = mas_data_end(&mas);
7587 		if (MAS_WARN_ON(&mas, (end < mt_min_slot_count(mas.node)) &&
7588 				(!mte_is_root(mas.node)))) {
7589 			pr_err("Invalid size %u of " PTR_FMT "\n",
7590 			       end, mas_mn(&mas));
7591 		}
7592 
7593 		mas_validate_parent_slot(&mas);
7594 		mas_validate_limits(&mas);
7595 		mas_validate_child_slot(&mas);
7596 		if (mt_is_alloc(mt))
7597 			mas_validate_gaps(&mas);
7598 		mas_dfs_postorder(&mas, ULONG_MAX);
7599 	}
7600 	mt_validate_nulls(mt);
7601 }
7602 EXPORT_SYMBOL_GPL(mt_validate);
7603 
mas_dump(const struct ma_state * mas)7604 void mas_dump(const struct ma_state *mas)
7605 {
7606 	pr_err("MAS: tree=" PTR_FMT " enode=" PTR_FMT " ",
7607 	       mas->tree, mas->node);
7608 	switch (mas->status) {
7609 	case ma_active:
7610 		pr_err("(ma_active)");
7611 		break;
7612 	case ma_none:
7613 		pr_err("(ma_none)");
7614 		break;
7615 	case ma_root:
7616 		pr_err("(ma_root)");
7617 		break;
7618 	case ma_start:
7619 		pr_err("(ma_start) ");
7620 		break;
7621 	case ma_pause:
7622 		pr_err("(ma_pause) ");
7623 		break;
7624 	case ma_overflow:
7625 		pr_err("(ma_overflow) ");
7626 		break;
7627 	case ma_underflow:
7628 		pr_err("(ma_underflow) ");
7629 		break;
7630 	case ma_error:
7631 		pr_err("(ma_error) ");
7632 		break;
7633 	}
7634 
7635 	pr_err("Store Type: ");
7636 	switch (mas->store_type) {
7637 	case wr_invalid:
7638 		pr_err("invalid store type\n");
7639 		break;
7640 	case wr_new_root:
7641 		pr_err("new_root\n");
7642 		break;
7643 	case wr_store_root:
7644 		pr_err("store_root\n");
7645 		break;
7646 	case wr_exact_fit:
7647 		pr_err("exact_fit\n");
7648 		break;
7649 	case wr_split_store:
7650 		pr_err("split_store\n");
7651 		break;
7652 	case wr_slot_store:
7653 		pr_err("slot_store\n");
7654 		break;
7655 	case wr_append:
7656 		pr_err("append\n");
7657 		break;
7658 	case wr_node_store:
7659 		pr_err("node_store\n");
7660 		break;
7661 	case wr_spanning_store:
7662 		pr_err("spanning_store\n");
7663 		break;
7664 	case wr_rebalance:
7665 		pr_err("rebalance\n");
7666 		break;
7667 	}
7668 
7669 	pr_err("[%u/%u] index=%lx last=%lx\n", mas->offset, mas->end,
7670 	       mas->index, mas->last);
7671 	pr_err("     min=%lx max=%lx alloc=" PTR_FMT ", depth=%u, flags=%x\n",
7672 	       mas->min, mas->max, mas->alloc, mas->depth, mas->mas_flags);
7673 	if (mas->index > mas->last)
7674 		pr_err("Check index & last\n");
7675 }
7676 EXPORT_SYMBOL_GPL(mas_dump);
7677 
mas_wr_dump(const struct ma_wr_state * wr_mas)7678 void mas_wr_dump(const struct ma_wr_state *wr_mas)
7679 {
7680 	pr_err("WR_MAS: node=" PTR_FMT " r_min=%lx r_max=%lx\n",
7681 	       wr_mas->node, wr_mas->r_min, wr_mas->r_max);
7682 	pr_err("        type=%u off_end=%u, node_end=%u, end_piv=%lx\n",
7683 	       wr_mas->type, wr_mas->offset_end, wr_mas->mas->end,
7684 	       wr_mas->end_piv);
7685 }
7686 EXPORT_SYMBOL_GPL(mas_wr_dump);
7687 
7688 #endif /* CONFIG_DEBUG_MAPLE_TREE */
7689