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