1 // SPDX-License-Identifier: GPL-2.0+
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>
10 * DOC: Interesting implementation details of the Maple Tree
12 * Each node type has a number of slots for entries and a number of slots for
13 * pivots. In the case of dense nodes, the pivots are implied by the position
14 * and are simply the slot index + the minimum of the node.
16 * In regular B-Tree terms, pivots are called keys. The term pivot is used to
17 * indicate that the tree is specifying ranges, Pivots may appear in the
18 * subtree with an entry attached to the value where as keys are unique to a
19 * specific position of a B-tree. Pivot values are inclusive of the slot with
23 * The following illustrates the layout of a range64 nodes slots and pivots.
26 * Slots -> | 0 | 1 | 2 | ... | 12 | 13 | 14 | 15 |
28 * │ │ │ │ │ │ │ │ └─ Implied maximum
29 * │ │ │ │ │ │ │ └─ Pivot 14
30 * │ │ │ │ │ │ └─ Pivot 13
31 * │ │ │ │ │ └─ Pivot 12
39 * Internal (non-leaf) nodes contain pointers to other nodes.
40 * Leaf nodes contain entries.
42 * The location of interest is often referred to as an offset. All offsets have
43 * a slot, but the last offset has an implied pivot from the node above (or
44 * UINT_MAX for the root node.
46 * Ranges complicate certain write activities. When modifying any of
47 * the B-tree variants, it is known that one entry will either be added or
48 * deleted. When modifying the Maple Tree, one store operation may overwrite
49 * the entire data set, or one half of the tree, or the middle half of the tree.
54 #include <linux/maple_tree.h>
55 #include <linux/xarray.h>
56 #include <linux/types.h>
57 #include <linux/export.h>
58 #include <linux/slab.h>
59 #include <linux/limits.h>
60 #include <asm/barrier.h>
62 #define CREATE_TRACE_POINTS
63 #include <trace/events/maple_tree.h>
65 #define MA_ROOT_PARENT 1
69 * * MA_STATE_BULK - Bulk insert mode
70 * * MA_STATE_REBALANCE - Indicate a rebalance during bulk insert
71 * * MA_STATE_PREALLOC - Preallocated nodes, WARN_ON allocation
73 #define MA_STATE_BULK 1
74 #define MA_STATE_REBALANCE 2
75 #define MA_STATE_PREALLOC 4
77 #define ma_parent_ptr(x) ((struct maple_pnode *)(x))
78 #define ma_mnode_ptr(x) ((struct maple_node *)(x))
79 #define ma_enode_ptr(x) ((struct maple_enode *)(x))
80 static struct kmem_cache *maple_node_cache;
82 #ifdef CONFIG_DEBUG_MAPLE_TREE
83 static const unsigned long mt_max[] = {
84 [maple_dense] = MAPLE_NODE_SLOTS,
85 [maple_leaf_64] = ULONG_MAX,
86 [maple_range_64] = ULONG_MAX,
87 [maple_arange_64] = ULONG_MAX,
89 #define mt_node_max(x) mt_max[mte_node_type(x)]
92 static const unsigned char mt_slots[] = {
93 [maple_dense] = MAPLE_NODE_SLOTS,
94 [maple_leaf_64] = MAPLE_RANGE64_SLOTS,
95 [maple_range_64] = MAPLE_RANGE64_SLOTS,
96 [maple_arange_64] = MAPLE_ARANGE64_SLOTS,
98 #define mt_slot_count(x) mt_slots[mte_node_type(x)]
100 static const unsigned char mt_pivots[] = {
102 [maple_leaf_64] = MAPLE_RANGE64_SLOTS - 1,
103 [maple_range_64] = MAPLE_RANGE64_SLOTS - 1,
104 [maple_arange_64] = MAPLE_ARANGE64_SLOTS - 1,
106 #define mt_pivot_count(x) mt_pivots[mte_node_type(x)]
108 static const unsigned char mt_min_slots[] = {
109 [maple_dense] = MAPLE_NODE_SLOTS / 2,
110 [maple_leaf_64] = (MAPLE_RANGE64_SLOTS / 2) - 2,
111 [maple_range_64] = (MAPLE_RANGE64_SLOTS / 2) - 2,
112 [maple_arange_64] = (MAPLE_ARANGE64_SLOTS / 2) - 1,
114 #define mt_min_slot_count(x) mt_min_slots[mte_node_type(x)]
116 #define MAPLE_BIG_NODE_SLOTS (MAPLE_RANGE64_SLOTS * 2 + 2)
117 #define MAPLE_BIG_NODE_GAPS (MAPLE_ARANGE64_SLOTS * 2 + 1)
119 struct maple_big_node {
120 struct maple_pnode *parent;
121 unsigned long pivot[MAPLE_BIG_NODE_SLOTS - 1];
123 struct maple_enode *slot[MAPLE_BIG_NODE_SLOTS];
125 unsigned long padding[MAPLE_BIG_NODE_GAPS];
126 unsigned long gap[MAPLE_BIG_NODE_GAPS];
130 enum maple_type type;
134 * The maple_subtree_state is used to build a tree to replace a segment of an
135 * existing tree in a more atomic way. Any walkers of the older tree will hit a
136 * dead node and restart on updates.
138 struct maple_subtree_state {
139 struct ma_state *orig_l; /* Original left side of subtree */
140 struct ma_state *orig_r; /* Original right side of subtree */
141 struct ma_state *l; /* New left side of subtree */
142 struct ma_state *m; /* New middle of subtree (rare) */
143 struct ma_state *r; /* New right side of subtree */
144 struct ma_topiary *free; /* nodes to be freed */
145 struct ma_topiary *destroy; /* Nodes to be destroyed (walked and freed) */
146 struct maple_big_node *bn;
149 #ifdef CONFIG_KASAN_STACK
150 /* Prevent mas_wr_bnode() from exceeding the stack frame limit */
151 #define noinline_for_kasan noinline_for_stack
153 #define noinline_for_kasan inline
157 static inline struct maple_node *mt_alloc_one(gfp_t gfp)
159 return kmem_cache_alloc(maple_node_cache, gfp);
162 static inline int mt_alloc_bulk(gfp_t gfp, size_t size, void **nodes)
164 return kmem_cache_alloc_bulk(maple_node_cache, gfp, size, nodes);
167 static inline void mt_free_bulk(size_t size, void __rcu **nodes)
169 kmem_cache_free_bulk(maple_node_cache, size, (void **)nodes);
172 static void mt_free_rcu(struct rcu_head *head)
174 struct maple_node *node = container_of(head, struct maple_node, rcu);
176 kmem_cache_free(maple_node_cache, node);
180 * ma_free_rcu() - Use rcu callback to free a maple node
181 * @node: The node to free
183 * The maple tree uses the parent pointer to indicate this node is no longer in
184 * use and will be freed.
186 static void ma_free_rcu(struct maple_node *node)
188 node->parent = ma_parent_ptr(node);
189 call_rcu(&node->rcu, mt_free_rcu);
192 static void mas_set_height(struct ma_state *mas)
194 unsigned int new_flags = mas->tree->ma_flags;
196 new_flags &= ~MT_FLAGS_HEIGHT_MASK;
197 BUG_ON(mas->depth > MAPLE_HEIGHT_MAX);
198 new_flags |= mas->depth << MT_FLAGS_HEIGHT_OFFSET;
199 mas->tree->ma_flags = new_flags;
202 static unsigned int mas_mt_height(struct ma_state *mas)
204 return mt_height(mas->tree);
207 static inline enum maple_type mte_node_type(const struct maple_enode *entry)
209 return ((unsigned long)entry >> MAPLE_NODE_TYPE_SHIFT) &
210 MAPLE_NODE_TYPE_MASK;
213 static inline bool ma_is_dense(const enum maple_type type)
215 return type < maple_leaf_64;
218 static inline bool ma_is_leaf(const enum maple_type type)
220 return type < maple_range_64;
223 static inline bool mte_is_leaf(const struct maple_enode *entry)
225 return ma_is_leaf(mte_node_type(entry));
229 * We also reserve values with the bottom two bits set to '10' which are
232 static inline bool mt_is_reserved(const void *entry)
234 return ((unsigned long)entry < MAPLE_RESERVED_RANGE) &&
235 xa_is_internal(entry);
238 static inline void mas_set_err(struct ma_state *mas, long err)
240 mas->node = MA_ERROR(err);
243 static inline bool mas_is_ptr(struct ma_state *mas)
245 return mas->node == MAS_ROOT;
248 static inline bool mas_is_start(struct ma_state *mas)
250 return mas->node == MAS_START;
253 bool mas_is_err(struct ma_state *mas)
255 return xa_is_err(mas->node);
258 static inline bool mas_searchable(struct ma_state *mas)
260 if (mas_is_none(mas))
269 static inline struct maple_node *mte_to_node(const struct maple_enode *entry)
271 return (struct maple_node *)((unsigned long)entry & ~MAPLE_NODE_MASK);
275 * mte_to_mat() - Convert a maple encoded node to a maple topiary node.
276 * @entry: The maple encoded node
278 * Return: a maple topiary pointer
280 static inline struct maple_topiary *mte_to_mat(const struct maple_enode *entry)
282 return (struct maple_topiary *)
283 ((unsigned long)entry & ~MAPLE_NODE_MASK);
287 * mas_mn() - Get the maple state node.
288 * @mas: The maple state
290 * Return: the maple node (not encoded - bare pointer).
292 static inline struct maple_node *mas_mn(const struct ma_state *mas)
294 return mte_to_node(mas->node);
298 * mte_set_node_dead() - Set a maple encoded node as dead.
299 * @mn: The maple encoded node.
301 static inline void mte_set_node_dead(struct maple_enode *mn)
303 mte_to_node(mn)->parent = ma_parent_ptr(mte_to_node(mn));
304 smp_wmb(); /* Needed for RCU */
307 /* Bit 1 indicates the root is a node */
308 #define MAPLE_ROOT_NODE 0x02
309 /* maple_type stored bit 3-6 */
310 #define MAPLE_ENODE_TYPE_SHIFT 0x03
311 /* Bit 2 means a NULL somewhere below */
312 #define MAPLE_ENODE_NULL 0x04
314 static inline struct maple_enode *mt_mk_node(const struct maple_node *node,
315 enum maple_type type)
317 return (void *)((unsigned long)node |
318 (type << MAPLE_ENODE_TYPE_SHIFT) | MAPLE_ENODE_NULL);
321 static inline void *mte_mk_root(const struct maple_enode *node)
323 return (void *)((unsigned long)node | MAPLE_ROOT_NODE);
326 static inline void *mte_safe_root(const struct maple_enode *node)
328 return (void *)((unsigned long)node & ~MAPLE_ROOT_NODE);
331 static inline void *mte_set_full(const struct maple_enode *node)
333 return (void *)((unsigned long)node & ~MAPLE_ENODE_NULL);
336 static inline void *mte_clear_full(const struct maple_enode *node)
338 return (void *)((unsigned long)node | MAPLE_ENODE_NULL);
341 static inline bool mte_has_null(const struct maple_enode *node)
343 return (unsigned long)node & MAPLE_ENODE_NULL;
346 static inline bool ma_is_root(struct maple_node *node)
348 return ((unsigned long)node->parent & MA_ROOT_PARENT);
351 static inline bool mte_is_root(const struct maple_enode *node)
353 return ma_is_root(mte_to_node(node));
356 static inline bool mas_is_root_limits(const struct ma_state *mas)
358 return !mas->min && mas->max == ULONG_MAX;
361 static inline bool mt_is_alloc(struct maple_tree *mt)
363 return (mt->ma_flags & MT_FLAGS_ALLOC_RANGE);
368 * Excluding root, the parent pointer is 256B aligned like all other tree nodes.
369 * When storing a 32 or 64 bit values, the offset can fit into 5 bits. The 16
370 * bit values need an extra bit to store the offset. This extra bit comes from
371 * a reuse of the last bit in the node type. This is possible by using bit 1 to
372 * indicate if bit 2 is part of the type or the slot.
376 * 0x?00 = 16 bit nodes
377 * 0x010 = 32 bit nodes
378 * 0x110 = 64 bit nodes
380 * Slot size and alignment
382 * 0b?00 : 16 bit values, type in 0-1, slot in 2-7
383 * 0b010 : 32 bit values, type in 0-2, slot in 3-7
384 * 0b110 : 64 bit values, type in 0-2, slot in 3-7
387 #define MAPLE_PARENT_ROOT 0x01
389 #define MAPLE_PARENT_SLOT_SHIFT 0x03
390 #define MAPLE_PARENT_SLOT_MASK 0xF8
392 #define MAPLE_PARENT_16B_SLOT_SHIFT 0x02
393 #define MAPLE_PARENT_16B_SLOT_MASK 0xFC
395 #define MAPLE_PARENT_RANGE64 0x06
396 #define MAPLE_PARENT_RANGE32 0x04
397 #define MAPLE_PARENT_NOT_RANGE16 0x02
400 * mte_parent_shift() - Get the parent shift for the slot storage.
401 * @parent: The parent pointer cast as an unsigned long
402 * Return: The shift into that pointer to the star to of the slot
404 static inline unsigned long mte_parent_shift(unsigned long parent)
406 /* Note bit 1 == 0 means 16B */
407 if (likely(parent & MAPLE_PARENT_NOT_RANGE16))
408 return MAPLE_PARENT_SLOT_SHIFT;
410 return MAPLE_PARENT_16B_SLOT_SHIFT;
414 * mte_parent_slot_mask() - Get the slot mask for the parent.
415 * @parent: The parent pointer cast as an unsigned long.
416 * Return: The slot mask for that parent.
418 static inline unsigned long mte_parent_slot_mask(unsigned long parent)
420 /* Note bit 1 == 0 means 16B */
421 if (likely(parent & MAPLE_PARENT_NOT_RANGE16))
422 return MAPLE_PARENT_SLOT_MASK;
424 return MAPLE_PARENT_16B_SLOT_MASK;
428 * mas_parent_enum() - Return the maple_type of the parent from the stored
430 * @mas: The maple state
431 * @node: The maple_enode to extract the parent's enum
432 * Return: The node->parent maple_type
435 enum maple_type mte_parent_enum(struct maple_enode *p_enode,
436 struct maple_tree *mt)
438 unsigned long p_type;
440 p_type = (unsigned long)p_enode;
441 if (p_type & MAPLE_PARENT_ROOT)
442 return 0; /* Validated in the caller. */
444 p_type &= MAPLE_NODE_MASK;
445 p_type = p_type & ~(MAPLE_PARENT_ROOT | mte_parent_slot_mask(p_type));
448 case MAPLE_PARENT_RANGE64: /* or MAPLE_PARENT_ARANGE64 */
450 return maple_arange_64;
451 return maple_range_64;
458 enum maple_type mas_parent_enum(struct ma_state *mas, struct maple_enode *enode)
460 return mte_parent_enum(ma_enode_ptr(mte_to_node(enode)->parent), mas->tree);
464 * mte_set_parent() - Set the parent node and encode the slot
465 * @enode: The encoded maple node.
466 * @parent: The encoded maple node that is the parent of @enode.
467 * @slot: The slot that @enode resides in @parent.
469 * Slot number is encoded in the enode->parent bit 3-6 or 2-6, depending on the
473 void mte_set_parent(struct maple_enode *enode, const struct maple_enode *parent,
476 unsigned long val = (unsigned long)parent;
479 enum maple_type p_type = mte_node_type(parent);
481 BUG_ON(p_type == maple_dense);
482 BUG_ON(p_type == maple_leaf_64);
486 case maple_arange_64:
487 shift = MAPLE_PARENT_SLOT_SHIFT;
488 type = MAPLE_PARENT_RANGE64;
497 val &= ~MAPLE_NODE_MASK; /* Clear all node metadata in parent */
498 val |= (slot << shift) | type;
499 mte_to_node(enode)->parent = ma_parent_ptr(val);
503 * mte_parent_slot() - get the parent slot of @enode.
504 * @enode: The encoded maple node.
506 * Return: The slot in the parent node where @enode resides.
508 static inline unsigned int mte_parent_slot(const struct maple_enode *enode)
510 unsigned long val = (unsigned long)mte_to_node(enode)->parent;
512 if (val & MA_ROOT_PARENT)
516 * Okay to use MAPLE_PARENT_16B_SLOT_MASK as the last bit will be lost
517 * by shift if the parent shift is MAPLE_PARENT_SLOT_SHIFT
519 return (val & MAPLE_PARENT_16B_SLOT_MASK) >> mte_parent_shift(val);
523 * mte_parent() - Get the parent of @node.
524 * @node: The encoded maple node.
526 * Return: The parent maple node.
528 static inline struct maple_node *mte_parent(const struct maple_enode *enode)
530 return (void *)((unsigned long)
531 (mte_to_node(enode)->parent) & ~MAPLE_NODE_MASK);
535 * ma_dead_node() - check if the @enode is dead.
536 * @enode: The encoded maple node
538 * Return: true if dead, false otherwise.
540 static inline bool ma_dead_node(const struct maple_node *node)
542 struct maple_node *parent = (void *)((unsigned long)
543 node->parent & ~MAPLE_NODE_MASK);
545 return (parent == node);
549 * mte_dead_node() - check if the @enode is dead.
550 * @enode: The encoded maple node
552 * Return: true if dead, false otherwise.
554 static inline bool mte_dead_node(const struct maple_enode *enode)
556 struct maple_node *parent, *node;
558 node = mte_to_node(enode);
559 parent = mte_parent(enode);
560 return (parent == node);
564 * mas_allocated() - Get the number of nodes allocated in a maple state.
565 * @mas: The maple state
567 * The ma_state alloc member is overloaded to hold a pointer to the first
568 * allocated node or to the number of requested nodes to allocate. If bit 0 is
569 * set, then the alloc contains the number of requested nodes. If there is an
570 * allocated node, then the total allocated nodes is in that node.
572 * Return: The total number of nodes allocated
574 static inline unsigned long mas_allocated(const struct ma_state *mas)
576 if (!mas->alloc || ((unsigned long)mas->alloc & 0x1))
579 return mas->alloc->total;
583 * mas_set_alloc_req() - Set the requested number of allocations.
584 * @mas: the maple state
585 * @count: the number of allocations.
587 * The requested number of allocations is either in the first allocated node,
588 * located in @mas->alloc->request_count, or directly in @mas->alloc if there is
589 * no allocated node. Set the request either in the node or do the necessary
590 * encoding to store in @mas->alloc directly.
592 static inline void mas_set_alloc_req(struct ma_state *mas, unsigned long count)
594 if (!mas->alloc || ((unsigned long)mas->alloc & 0x1)) {
598 mas->alloc = (struct maple_alloc *)(((count) << 1U) | 1U);
602 mas->alloc->request_count = count;
606 * mas_alloc_req() - get the requested number of allocations.
607 * @mas: The maple state
609 * The alloc count is either stored directly in @mas, or in
610 * @mas->alloc->request_count if there is at least one node allocated. Decode
611 * the request count if it's stored directly in @mas->alloc.
613 * Return: The allocation request count.
615 static inline unsigned int mas_alloc_req(const struct ma_state *mas)
617 if ((unsigned long)mas->alloc & 0x1)
618 return (unsigned long)(mas->alloc) >> 1;
620 return mas->alloc->request_count;
625 * ma_pivots() - Get a pointer to the maple node pivots.
626 * @node - the maple node
627 * @type - the node type
629 * In the event of a dead node, this array may be %NULL
631 * Return: A pointer to the maple node pivots
633 static inline unsigned long *ma_pivots(struct maple_node *node,
634 enum maple_type type)
637 case maple_arange_64:
638 return node->ma64.pivot;
641 return node->mr64.pivot;
649 * ma_gaps() - Get a pointer to the maple node gaps.
650 * @node - the maple node
651 * @type - the node type
653 * Return: A pointer to the maple node gaps
655 static inline unsigned long *ma_gaps(struct maple_node *node,
656 enum maple_type type)
659 case maple_arange_64:
660 return node->ma64.gap;
670 * mte_pivot() - Get the pivot at @piv of the maple encoded node.
671 * @mn: The maple encoded node.
674 * Return: the pivot at @piv of @mn.
676 static inline unsigned long mte_pivot(const struct maple_enode *mn,
679 struct maple_node *node = mte_to_node(mn);
680 enum maple_type type = mte_node_type(mn);
682 if (piv >= mt_pivots[type]) {
687 case maple_arange_64:
688 return node->ma64.pivot[piv];
691 return node->mr64.pivot[piv];
699 * mas_safe_pivot() - get the pivot at @piv or mas->max.
700 * @mas: The maple state
701 * @pivots: The pointer to the maple node pivots
702 * @piv: The pivot to fetch
703 * @type: The maple node type
705 * Return: The pivot at @piv within the limit of the @pivots array, @mas->max
708 static inline unsigned long
709 mas_safe_pivot(const struct ma_state *mas, unsigned long *pivots,
710 unsigned char piv, enum maple_type type)
712 if (piv >= mt_pivots[type])
719 * mas_safe_min() - Return the minimum for a given offset.
720 * @mas: The maple state
721 * @pivots: The pointer to the maple node pivots
722 * @offset: The offset into the pivot array
724 * Return: The minimum range value that is contained in @offset.
726 static inline unsigned long
727 mas_safe_min(struct ma_state *mas, unsigned long *pivots, unsigned char offset)
730 return pivots[offset - 1] + 1;
736 * mas_logical_pivot() - Get the logical pivot of a given offset.
737 * @mas: The maple state
738 * @pivots: The pointer to the maple node pivots
739 * @offset: The offset into the pivot array
740 * @type: The maple node type
742 * When there is no value at a pivot (beyond the end of the data), then the
743 * pivot is actually @mas->max.
745 * Return: the logical pivot of a given @offset.
747 static inline unsigned long
748 mas_logical_pivot(struct ma_state *mas, unsigned long *pivots,
749 unsigned char offset, enum maple_type type)
751 unsigned long lpiv = mas_safe_pivot(mas, pivots, offset, type);
763 * mte_set_pivot() - Set a pivot to a value in an encoded maple node.
764 * @mn: The encoded maple node
765 * @piv: The pivot offset
766 * @val: The value of the pivot
768 static inline void mte_set_pivot(struct maple_enode *mn, unsigned char piv,
771 struct maple_node *node = mte_to_node(mn);
772 enum maple_type type = mte_node_type(mn);
774 BUG_ON(piv >= mt_pivots[type]);
779 node->mr64.pivot[piv] = val;
781 case maple_arange_64:
782 node->ma64.pivot[piv] = val;
791 * ma_slots() - Get a pointer to the maple node slots.
792 * @mn: The maple node
793 * @mt: The maple node type
795 * Return: A pointer to the maple node slots
797 static inline void __rcu **ma_slots(struct maple_node *mn, enum maple_type mt)
801 case maple_arange_64:
802 return mn->ma64.slot;
805 return mn->mr64.slot;
811 static inline bool mt_locked(const struct maple_tree *mt)
813 return mt_external_lock(mt) ? mt_lock_is_held(mt) :
814 lockdep_is_held(&mt->ma_lock);
817 static inline void *mt_slot(const struct maple_tree *mt,
818 void __rcu **slots, unsigned char offset)
820 return rcu_dereference_check(slots[offset], mt_locked(mt));
824 * mas_slot_locked() - Get the slot value when holding the maple tree lock.
825 * @mas: The maple state
826 * @slots: The pointer to the slots
827 * @offset: The offset into the slots array to fetch
829 * Return: The entry stored in @slots at the @offset.
831 static inline void *mas_slot_locked(struct ma_state *mas, void __rcu **slots,
832 unsigned char offset)
834 return rcu_dereference_protected(slots[offset], mt_locked(mas->tree));
838 * mas_slot() - Get the slot value when not holding the maple tree lock.
839 * @mas: The maple state
840 * @slots: The pointer to the slots
841 * @offset: The offset into the slots array to fetch
843 * Return: The entry stored in @slots at the @offset
845 static inline void *mas_slot(struct ma_state *mas, void __rcu **slots,
846 unsigned char offset)
848 return mt_slot(mas->tree, slots, offset);
852 * mas_root() - Get the maple tree root.
853 * @mas: The maple state.
855 * Return: The pointer to the root of the tree
857 static inline void *mas_root(struct ma_state *mas)
859 return rcu_dereference_check(mas->tree->ma_root, mt_locked(mas->tree));
862 static inline void *mt_root_locked(struct maple_tree *mt)
864 return rcu_dereference_protected(mt->ma_root, mt_locked(mt));
868 * mas_root_locked() - Get the maple tree root when holding the maple tree lock.
869 * @mas: The maple state.
871 * Return: The pointer to the root of the tree
873 static inline void *mas_root_locked(struct ma_state *mas)
875 return mt_root_locked(mas->tree);
878 static inline struct maple_metadata *ma_meta(struct maple_node *mn,
882 case maple_arange_64:
883 return &mn->ma64.meta;
885 return &mn->mr64.meta;
890 * ma_set_meta() - Set the metadata information of a node.
891 * @mn: The maple node
892 * @mt: The maple node type
893 * @offset: The offset of the highest sub-gap in this node.
894 * @end: The end of the data in this node.
896 static inline void ma_set_meta(struct maple_node *mn, enum maple_type mt,
897 unsigned char offset, unsigned char end)
899 struct maple_metadata *meta = ma_meta(mn, mt);
906 * ma_meta_end() - Get the data end of a node from the metadata
907 * @mn: The maple node
908 * @mt: The maple node type
910 static inline unsigned char ma_meta_end(struct maple_node *mn,
913 struct maple_metadata *meta = ma_meta(mn, mt);
919 * ma_meta_gap() - Get the largest gap location of a node from the metadata
920 * @mn: The maple node
921 * @mt: The maple node type
923 static inline unsigned char ma_meta_gap(struct maple_node *mn,
926 BUG_ON(mt != maple_arange_64);
928 return mn->ma64.meta.gap;
932 * ma_set_meta_gap() - Set the largest gap location in a nodes metadata
933 * @mn: The maple node
934 * @mn: The maple node type
935 * @offset: The location of the largest gap.
937 static inline void ma_set_meta_gap(struct maple_node *mn, enum maple_type mt,
938 unsigned char offset)
941 struct maple_metadata *meta = ma_meta(mn, mt);
947 * mat_add() - Add a @dead_enode to the ma_topiary of a list of dead nodes.
948 * @mat - the ma_topiary, a linked list of dead nodes.
949 * @dead_enode - the node to be marked as dead and added to the tail of the list
951 * Add the @dead_enode to the linked list in @mat.
953 static inline void mat_add(struct ma_topiary *mat,
954 struct maple_enode *dead_enode)
956 mte_set_node_dead(dead_enode);
957 mte_to_mat(dead_enode)->next = NULL;
959 mat->tail = mat->head = dead_enode;
963 mte_to_mat(mat->tail)->next = dead_enode;
964 mat->tail = dead_enode;
967 static void mte_destroy_walk(struct maple_enode *, struct maple_tree *);
968 static inline void mas_free(struct ma_state *mas, struct maple_enode *used);
971 * mas_mat_free() - Free all nodes in a dead list.
972 * @mas - the maple state
973 * @mat - the ma_topiary linked list of dead nodes to free.
975 * Free walk a dead list.
977 static void mas_mat_free(struct ma_state *mas, struct ma_topiary *mat)
979 struct maple_enode *next;
982 next = mte_to_mat(mat->head)->next;
983 mas_free(mas, mat->head);
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.
993 * Destroy walk a dead list.
995 static void mas_mat_destroy(struct ma_state *mas, struct ma_topiary *mat)
997 struct maple_enode *next;
1000 next = mte_to_mat(mat->head)->next;
1001 mte_destroy_walk(mat->head, mat->mtree);
1006 * mas_descend() - Descend into the slot stored in the ma_state.
1007 * @mas - the maple state.
1009 * Note: Not RCU safe, only use in write side or debug code.
1011 static inline void mas_descend(struct ma_state *mas)
1013 enum maple_type type;
1014 unsigned long *pivots;
1015 struct maple_node *node;
1019 type = mte_node_type(mas->node);
1020 pivots = ma_pivots(node, type);
1021 slots = ma_slots(node, type);
1024 mas->min = pivots[mas->offset - 1] + 1;
1025 mas->max = mas_safe_pivot(mas, pivots, mas->offset, type);
1026 mas->node = mas_slot(mas, slots, mas->offset);
1030 * mte_set_gap() - Set a maple node gap.
1031 * @mn: The encoded maple node
1032 * @gap: The offset of the gap to set
1033 * @val: The gap value
1035 static inline void mte_set_gap(const struct maple_enode *mn,
1036 unsigned char gap, unsigned long val)
1038 switch (mte_node_type(mn)) {
1041 case maple_arange_64:
1042 mte_to_node(mn)->ma64.gap[gap] = val;
1048 * mas_ascend() - Walk up a level of the tree.
1049 * @mas: The maple state
1051 * Sets the @mas->max and @mas->min to the correct values when walking up. This
1052 * may cause several levels of walking up to find the correct min and max.
1053 * May find a dead node which will cause a premature return.
1054 * Return: 1 on dead node, 0 otherwise
1056 static int mas_ascend(struct ma_state *mas)
1058 struct maple_enode *p_enode; /* parent enode. */
1059 struct maple_enode *a_enode; /* ancestor enode. */
1060 struct maple_node *a_node; /* ancestor node. */
1061 struct maple_node *p_node; /* parent node. */
1062 unsigned char a_slot;
1063 enum maple_type a_type;
1064 unsigned long min, max;
1065 unsigned long *pivots;
1066 unsigned char offset;
1067 bool set_max = false, set_min = false;
1069 a_node = mas_mn(mas);
1070 if (ma_is_root(a_node)) {
1075 p_node = mte_parent(mas->node);
1076 if (unlikely(a_node == p_node))
1078 a_type = mas_parent_enum(mas, mas->node);
1079 offset = mte_parent_slot(mas->node);
1080 a_enode = mt_mk_node(p_node, a_type);
1082 /* Check to make sure all parent information is still accurate */
1083 if (p_node != mte_parent(mas->node))
1086 mas->node = a_enode;
1087 mas->offset = offset;
1089 if (mte_is_root(a_enode)) {
1090 mas->max = ULONG_MAX;
1099 a_type = mas_parent_enum(mas, p_enode);
1100 a_node = mte_parent(p_enode);
1101 a_slot = mte_parent_slot(p_enode);
1102 a_enode = mt_mk_node(a_node, a_type);
1103 pivots = ma_pivots(a_node, a_type);
1105 if (unlikely(ma_dead_node(a_node)))
1108 if (!set_min && a_slot) {
1110 min = pivots[a_slot - 1] + 1;
1113 if (!set_max && a_slot < mt_pivots[a_type]) {
1115 max = pivots[a_slot];
1118 if (unlikely(ma_dead_node(a_node)))
1121 if (unlikely(ma_is_root(a_node)))
1124 } while (!set_min || !set_max);
1132 * mas_pop_node() - Get a previously allocated maple node from the maple state.
1133 * @mas: The maple state
1135 * Return: A pointer to a maple node.
1137 static inline struct maple_node *mas_pop_node(struct ma_state *mas)
1139 struct maple_alloc *ret, *node = mas->alloc;
1140 unsigned long total = mas_allocated(mas);
1141 unsigned int req = mas_alloc_req(mas);
1143 /* nothing or a request pending. */
1144 if (WARN_ON(!total))
1148 /* single allocation in this ma_state */
1154 if (node->node_count == 1) {
1155 /* Single allocation in this node. */
1156 mas->alloc = node->slot[0];
1157 mas->alloc->total = node->total - 1;
1162 ret = node->slot[--node->node_count];
1163 node->slot[node->node_count] = NULL;
1169 mas_set_alloc_req(mas, req);
1172 memset(ret, 0, sizeof(*ret));
1173 return (struct maple_node *)ret;
1177 * mas_push_node() - Push a node back on the maple state allocation.
1178 * @mas: The maple state
1179 * @used: The used maple node
1181 * Stores the maple node back into @mas->alloc for reuse. Updates allocated and
1182 * requested node count as necessary.
1184 static inline void mas_push_node(struct ma_state *mas, struct maple_node *used)
1186 struct maple_alloc *reuse = (struct maple_alloc *)used;
1187 struct maple_alloc *head = mas->alloc;
1188 unsigned long count;
1189 unsigned int requested = mas_alloc_req(mas);
1191 count = mas_allocated(mas);
1193 reuse->request_count = 0;
1194 reuse->node_count = 0;
1195 if (count && (head->node_count < MAPLE_ALLOC_SLOTS)) {
1196 head->slot[head->node_count++] = reuse;
1202 if ((head) && !((unsigned long)head & 0x1)) {
1203 reuse->slot[0] = head;
1204 reuse->node_count = 1;
1205 reuse->total += head->total;
1211 mas_set_alloc_req(mas, requested - 1);
1215 * mas_alloc_nodes() - Allocate nodes into a maple state
1216 * @mas: The maple state
1217 * @gfp: The GFP Flags
1219 static inline void mas_alloc_nodes(struct ma_state *mas, gfp_t gfp)
1221 struct maple_alloc *node;
1222 unsigned long allocated = mas_allocated(mas);
1223 unsigned int requested = mas_alloc_req(mas);
1225 void **slots = NULL;
1226 unsigned int max_req = 0;
1231 mas_set_alloc_req(mas, 0);
1232 if (mas->mas_flags & MA_STATE_PREALLOC) {
1235 WARN_ON(!allocated);
1238 if (!allocated || mas->alloc->node_count == MAPLE_ALLOC_SLOTS) {
1239 node = (struct maple_alloc *)mt_alloc_one(gfp);
1244 node->slot[0] = mas->alloc;
1245 node->node_count = 1;
1247 node->node_count = 0;
1251 node->total = ++allocated;
1256 node->request_count = 0;
1258 max_req = MAPLE_ALLOC_SLOTS;
1259 if (node->node_count) {
1260 unsigned int offset = node->node_count;
1262 slots = (void **)&node->slot[offset];
1265 slots = (void **)&node->slot;
1268 max_req = min(requested, max_req);
1269 count = mt_alloc_bulk(gfp, max_req, slots);
1273 node->node_count += count;
1275 node = node->slot[0];
1276 node->node_count = 0;
1277 node->request_count = 0;
1280 mas->alloc->total = allocated;
1284 /* Clean up potential freed allocations on bulk failure */
1285 memset(slots, 0, max_req * sizeof(unsigned long));
1287 mas_set_alloc_req(mas, requested);
1288 if (mas->alloc && !(((unsigned long)mas->alloc & 0x1)))
1289 mas->alloc->total = allocated;
1290 mas_set_err(mas, -ENOMEM);
1294 * mas_free() - Free an encoded maple node
1295 * @mas: The maple state
1296 * @used: The encoded maple node to free.
1298 * Uses rcu free if necessary, pushes @used back on the maple state allocations
1301 static inline void mas_free(struct ma_state *mas, struct maple_enode *used)
1303 struct maple_node *tmp = mte_to_node(used);
1305 if (mt_in_rcu(mas->tree))
1308 mas_push_node(mas, tmp);
1312 * mas_node_count() - Check if enough nodes are allocated and request more if
1313 * there is not enough nodes.
1314 * @mas: The maple state
1315 * @count: The number of nodes needed
1316 * @gfp: the gfp flags
1318 static void mas_node_count_gfp(struct ma_state *mas, int count, gfp_t gfp)
1320 unsigned long allocated = mas_allocated(mas);
1322 if (allocated < count) {
1323 mas_set_alloc_req(mas, count - allocated);
1324 mas_alloc_nodes(mas, gfp);
1329 * mas_node_count() - Check if enough nodes are allocated and request more if
1330 * there is not enough nodes.
1331 * @mas: The maple state
1332 * @count: The number of nodes needed
1334 * Note: Uses GFP_NOWAIT | __GFP_NOWARN for gfp flags.
1336 static void mas_node_count(struct ma_state *mas, int count)
1338 return mas_node_count_gfp(mas, count, GFP_NOWAIT | __GFP_NOWARN);
1342 * mas_start() - Sets up maple state for operations.
1343 * @mas: The maple state.
1345 * If mas->node == MAS_START, then set the min, max and depth to
1349 * - If mas->node is an error or not MAS_START, return NULL.
1350 * - If it's an empty tree: NULL & mas->node == MAS_NONE
1351 * - If it's a single entry: The entry & mas->node == MAS_ROOT
1352 * - If it's a tree: NULL & mas->node == safe root node.
1354 static inline struct maple_enode *mas_start(struct ma_state *mas)
1356 if (likely(mas_is_start(mas))) {
1357 struct maple_enode *root;
1360 mas->max = ULONG_MAX;
1364 root = mas_root(mas);
1365 /* Tree with nodes */
1366 if (likely(xa_is_node(root))) {
1368 mas->node = mte_safe_root(root);
1370 if (mte_dead_node(mas->node))
1377 if (unlikely(!root)) {
1378 mas->node = MAS_NONE;
1379 mas->offset = MAPLE_NODE_SLOTS;
1383 /* Single entry tree */
1384 mas->node = MAS_ROOT;
1385 mas->offset = MAPLE_NODE_SLOTS;
1387 /* Single entry tree. */
1398 * ma_data_end() - Find the end of the data in a node.
1399 * @node: The maple node
1400 * @type: The maple node type
1401 * @pivots: The array of pivots in the node
1402 * @max: The maximum value in the node
1404 * Uses metadata to find the end of the data when possible.
1405 * Return: The zero indexed last slot with data (may be null).
1407 static inline unsigned char ma_data_end(struct maple_node *node,
1408 enum maple_type type,
1409 unsigned long *pivots,
1412 unsigned char offset;
1417 if (type == maple_arange_64)
1418 return ma_meta_end(node, type);
1420 offset = mt_pivots[type] - 1;
1421 if (likely(!pivots[offset]))
1422 return ma_meta_end(node, type);
1424 if (likely(pivots[offset] == max))
1427 return mt_pivots[type];
1431 * mas_data_end() - Find the end of the data (slot).
1432 * @mas: the maple state
1434 * This method is optimized to check the metadata of a node if the node type
1435 * supports data end metadata.
1437 * Return: The zero indexed last slot with data (may be null).
1439 static inline unsigned char mas_data_end(struct ma_state *mas)
1441 enum maple_type type;
1442 struct maple_node *node;
1443 unsigned char offset;
1444 unsigned long *pivots;
1446 type = mte_node_type(mas->node);
1448 if (type == maple_arange_64)
1449 return ma_meta_end(node, type);
1451 pivots = ma_pivots(node, type);
1452 if (unlikely(ma_dead_node(node)))
1455 offset = mt_pivots[type] - 1;
1456 if (likely(!pivots[offset]))
1457 return ma_meta_end(node, type);
1459 if (likely(pivots[offset] == mas->max))
1462 return mt_pivots[type];
1466 * mas_leaf_max_gap() - Returns the largest gap in a leaf node
1467 * @mas - the maple state
1469 * Return: The maximum gap in the leaf.
1471 static unsigned long mas_leaf_max_gap(struct ma_state *mas)
1474 unsigned long pstart, gap, max_gap;
1475 struct maple_node *mn;
1476 unsigned long *pivots;
1479 unsigned char max_piv;
1481 mt = mte_node_type(mas->node);
1483 slots = ma_slots(mn, mt);
1485 if (unlikely(ma_is_dense(mt))) {
1487 for (i = 0; i < mt_slots[mt]; i++) {
1502 * Check the first implied pivot optimizes the loop below and slot 1 may
1503 * be skipped if there is a gap in slot 0.
1505 pivots = ma_pivots(mn, mt);
1506 if (likely(!slots[0])) {
1507 max_gap = pivots[0] - mas->min + 1;
1513 /* reduce max_piv as the special case is checked before the loop */
1514 max_piv = ma_data_end(mn, mt, pivots, mas->max) - 1;
1516 * Check end implied pivot which can only be a gap on the right most
1519 if (unlikely(mas->max == ULONG_MAX) && !slots[max_piv + 1]) {
1520 gap = ULONG_MAX - pivots[max_piv];
1525 for (; i <= max_piv; i++) {
1526 /* data == no gap. */
1527 if (likely(slots[i]))
1530 pstart = pivots[i - 1];
1531 gap = pivots[i] - pstart;
1535 /* There cannot be two gaps in a row. */
1542 * ma_max_gap() - Get the maximum gap in a maple node (non-leaf)
1543 * @node: The maple node
1544 * @gaps: The pointer to the gaps
1545 * @mt: The maple node type
1546 * @*off: Pointer to store the offset location of the gap.
1548 * Uses the metadata data end to scan backwards across set gaps.
1550 * Return: The maximum gap value
1552 static inline unsigned long
1553 ma_max_gap(struct maple_node *node, unsigned long *gaps, enum maple_type mt,
1556 unsigned char offset, i;
1557 unsigned long max_gap = 0;
1559 i = offset = ma_meta_end(node, mt);
1561 if (gaps[i] > max_gap) {
1572 * mas_max_gap() - find the largest gap in a non-leaf node and set the slot.
1573 * @mas: The maple state.
1575 * If the metadata gap is set to MAPLE_ARANGE64_META_MAX, there is no gap.
1577 * Return: The gap value.
1579 static inline unsigned long mas_max_gap(struct ma_state *mas)
1581 unsigned long *gaps;
1582 unsigned char offset;
1584 struct maple_node *node;
1586 mt = mte_node_type(mas->node);
1588 return mas_leaf_max_gap(mas);
1591 offset = ma_meta_gap(node, mt);
1592 if (offset == MAPLE_ARANGE64_META_MAX)
1595 gaps = ma_gaps(node, mt);
1596 return gaps[offset];
1600 * mas_parent_gap() - Set the parent gap and any gaps above, as needed
1601 * @mas: The maple state
1602 * @offset: The gap offset in the parent to set
1603 * @new: The new gap value.
1605 * Set the parent gap then continue to set the gap upwards, using the metadata
1606 * of the parent to see if it is necessary to check the node above.
1608 static inline void mas_parent_gap(struct ma_state *mas, unsigned char offset,
1611 unsigned long meta_gap = 0;
1612 struct maple_node *pnode;
1613 struct maple_enode *penode;
1614 unsigned long *pgaps;
1615 unsigned char meta_offset;
1616 enum maple_type pmt;
1618 pnode = mte_parent(mas->node);
1619 pmt = mas_parent_enum(mas, mas->node);
1620 penode = mt_mk_node(pnode, pmt);
1621 pgaps = ma_gaps(pnode, pmt);
1624 meta_offset = ma_meta_gap(pnode, pmt);
1625 if (meta_offset == MAPLE_ARANGE64_META_MAX)
1628 meta_gap = pgaps[meta_offset];
1630 pgaps[offset] = new;
1632 if (meta_gap == new)
1635 if (offset != meta_offset) {
1639 ma_set_meta_gap(pnode, pmt, offset);
1640 } else if (new < meta_gap) {
1642 new = ma_max_gap(pnode, pgaps, pmt, &meta_offset);
1643 ma_set_meta_gap(pnode, pmt, meta_offset);
1646 if (ma_is_root(pnode))
1649 /* Go to the parent node. */
1650 pnode = mte_parent(penode);
1651 pmt = mas_parent_enum(mas, penode);
1652 pgaps = ma_gaps(pnode, pmt);
1653 offset = mte_parent_slot(penode);
1654 penode = mt_mk_node(pnode, pmt);
1659 * mas_update_gap() - Update a nodes gaps and propagate up if necessary.
1660 * @mas - the maple state.
1662 static inline void mas_update_gap(struct ma_state *mas)
1664 unsigned char pslot;
1665 unsigned long p_gap;
1666 unsigned long max_gap;
1668 if (!mt_is_alloc(mas->tree))
1671 if (mte_is_root(mas->node))
1674 max_gap = mas_max_gap(mas);
1676 pslot = mte_parent_slot(mas->node);
1677 p_gap = ma_gaps(mte_parent(mas->node),
1678 mas_parent_enum(mas, mas->node))[pslot];
1680 if (p_gap != max_gap)
1681 mas_parent_gap(mas, pslot, max_gap);
1685 * mas_adopt_children() - Set the parent pointer of all nodes in @parent to
1686 * @parent with the slot encoded.
1687 * @mas - the maple state (for the tree)
1688 * @parent - the maple encoded node containing the children.
1690 static inline void mas_adopt_children(struct ma_state *mas,
1691 struct maple_enode *parent)
1693 enum maple_type type = mte_node_type(parent);
1694 struct maple_node *node = mas_mn(mas);
1695 void __rcu **slots = ma_slots(node, type);
1696 unsigned long *pivots = ma_pivots(node, type);
1697 struct maple_enode *child;
1698 unsigned char offset;
1700 offset = ma_data_end(node, type, pivots, mas->max);
1702 child = mas_slot_locked(mas, slots, offset);
1703 mte_set_parent(child, parent, offset);
1708 * mas_replace() - Replace a maple node in the tree with mas->node. Uses the
1709 * parent encoding to locate the maple node in the tree.
1710 * @mas - the ma_state to use for operations.
1711 * @advanced - boolean to adopt the child nodes and free the old node (false) or
1712 * leave the node (true) and handle the adoption and free elsewhere.
1714 static inline void mas_replace(struct ma_state *mas, bool advanced)
1715 __must_hold(mas->tree->lock)
1717 struct maple_node *mn = mas_mn(mas);
1718 struct maple_enode *old_enode;
1719 unsigned char offset = 0;
1720 void __rcu **slots = NULL;
1722 if (ma_is_root(mn)) {
1723 old_enode = mas_root_locked(mas);
1725 offset = mte_parent_slot(mas->node);
1726 slots = ma_slots(mte_parent(mas->node),
1727 mas_parent_enum(mas, mas->node));
1728 old_enode = mas_slot_locked(mas, slots, offset);
1731 if (!advanced && !mte_is_leaf(mas->node))
1732 mas_adopt_children(mas, mas->node);
1734 if (mte_is_root(mas->node)) {
1735 mn->parent = ma_parent_ptr(
1736 ((unsigned long)mas->tree | MA_ROOT_PARENT));
1737 rcu_assign_pointer(mas->tree->ma_root, mte_mk_root(mas->node));
1738 mas_set_height(mas);
1740 rcu_assign_pointer(slots[offset], mas->node);
1744 mas_free(mas, old_enode);
1748 * mas_new_child() - Find the new child of a node.
1749 * @mas: the maple state
1750 * @child: the maple state to store the child.
1752 static inline bool mas_new_child(struct ma_state *mas, struct ma_state *child)
1753 __must_hold(mas->tree->lock)
1756 unsigned char offset;
1758 unsigned long *pivots;
1759 struct maple_enode *entry;
1760 struct maple_node *node;
1763 mt = mte_node_type(mas->node);
1765 slots = ma_slots(node, mt);
1766 pivots = ma_pivots(node, mt);
1767 end = ma_data_end(node, mt, pivots, mas->max);
1768 for (offset = mas->offset; offset <= end; offset++) {
1769 entry = mas_slot_locked(mas, slots, offset);
1770 if (mte_parent(entry) == node) {
1772 mas->offset = offset + 1;
1773 child->offset = offset;
1783 * mab_shift_right() - Shift the data in mab right. Note, does not clean out the
1784 * old data or set b_node->b_end.
1785 * @b_node: the maple_big_node
1786 * @shift: the shift count
1788 static inline void mab_shift_right(struct maple_big_node *b_node,
1789 unsigned char shift)
1791 unsigned long size = b_node->b_end * sizeof(unsigned long);
1793 memmove(b_node->pivot + shift, b_node->pivot, size);
1794 memmove(b_node->slot + shift, b_node->slot, size);
1795 if (b_node->type == maple_arange_64)
1796 memmove(b_node->gap + shift, b_node->gap, size);
1800 * mab_middle_node() - Check if a middle node is needed (unlikely)
1801 * @b_node: the maple_big_node that contains the data.
1802 * @size: the amount of data in the b_node
1803 * @split: the potential split location
1804 * @slot_count: the size that can be stored in a single node being considered.
1806 * Return: true if a middle node is required.
1808 static inline bool mab_middle_node(struct maple_big_node *b_node, int split,
1809 unsigned char slot_count)
1811 unsigned char size = b_node->b_end;
1813 if (size >= 2 * slot_count)
1816 if (!b_node->slot[split] && (size >= 2 * slot_count - 1))
1823 * mab_no_null_split() - ensure the split doesn't fall on a NULL
1824 * @b_node: the maple_big_node with the data
1825 * @split: the suggested split location
1826 * @slot_count: the number of slots in the node being considered.
1828 * Return: the split location.
1830 static inline int mab_no_null_split(struct maple_big_node *b_node,
1831 unsigned char split, unsigned char slot_count)
1833 if (!b_node->slot[split]) {
1835 * If the split is less than the max slot && the right side will
1836 * still be sufficient, then increment the split on NULL.
1838 if ((split < slot_count - 1) &&
1839 (b_node->b_end - split) > (mt_min_slots[b_node->type]))
1848 * mab_calc_split() - Calculate the split location and if there needs to be two
1850 * @bn: The maple_big_node with the data
1851 * @mid_split: The second split, if required. 0 otherwise.
1853 * Return: The first split location. The middle split is set in @mid_split.
1855 static inline int mab_calc_split(struct ma_state *mas,
1856 struct maple_big_node *bn, unsigned char *mid_split, unsigned long min)
1858 unsigned char b_end = bn->b_end;
1859 int split = b_end / 2; /* Assume equal split. */
1860 unsigned char slot_min, slot_count = mt_slots[bn->type];
1863 * To support gap tracking, all NULL entries are kept together and a node cannot
1864 * end on a NULL entry, with the exception of the left-most leaf. The
1865 * limitation means that the split of a node must be checked for this condition
1866 * and be able to put more data in one direction or the other.
1868 if (unlikely((mas->mas_flags & MA_STATE_BULK))) {
1870 split = b_end - mt_min_slots[bn->type];
1872 if (!ma_is_leaf(bn->type))
1875 mas->mas_flags |= MA_STATE_REBALANCE;
1876 if (!bn->slot[split])
1882 * Although extremely rare, it is possible to enter what is known as the 3-way
1883 * split scenario. The 3-way split comes about by means of a store of a range
1884 * that overwrites the end and beginning of two full nodes. The result is a set
1885 * of entries that cannot be stored in 2 nodes. Sometimes, these two nodes can
1886 * also be located in different parent nodes which are also full. This can
1887 * carry upwards all the way to the root in the worst case.
1889 if (unlikely(mab_middle_node(bn, split, slot_count))) {
1891 *mid_split = split * 2;
1893 slot_min = mt_min_slots[bn->type];
1897 * Avoid having a range less than the slot count unless it
1898 * causes one node to be deficient.
1899 * NOTE: mt_min_slots is 1 based, b_end and split are zero.
1901 while (((bn->pivot[split] - min) < slot_count - 1) &&
1902 (split < slot_count - 1) && (b_end - split > slot_min))
1906 /* Avoid ending a node on a NULL entry */
1907 split = mab_no_null_split(bn, split, slot_count);
1909 if (unlikely(*mid_split))
1910 *mid_split = mab_no_null_split(bn, *mid_split, slot_count);
1916 * mas_mab_cp() - Copy data from a maple state inclusively to a maple_big_node
1917 * and set @b_node->b_end to the next free slot.
1918 * @mas: The maple state
1919 * @mas_start: The starting slot to copy
1920 * @mas_end: The end slot to copy (inclusively)
1921 * @b_node: The maple_big_node to place the data
1922 * @mab_start: The starting location in maple_big_node to store the data.
1924 static inline void mas_mab_cp(struct ma_state *mas, unsigned char mas_start,
1925 unsigned char mas_end, struct maple_big_node *b_node,
1926 unsigned char mab_start)
1929 struct maple_node *node;
1931 unsigned long *pivots, *gaps;
1932 int i = mas_start, j = mab_start;
1933 unsigned char piv_end;
1936 mt = mte_node_type(mas->node);
1937 pivots = ma_pivots(node, mt);
1939 b_node->pivot[j] = pivots[i++];
1940 if (unlikely(i > mas_end))
1945 piv_end = min(mas_end, mt_pivots[mt]);
1946 for (; i < piv_end; i++, j++) {
1947 b_node->pivot[j] = pivots[i];
1948 if (unlikely(!b_node->pivot[j]))
1951 if (unlikely(mas->max == b_node->pivot[j]))
1955 if (likely(i <= mas_end))
1956 b_node->pivot[j] = mas_safe_pivot(mas, pivots, i, mt);
1959 b_node->b_end = ++j;
1961 slots = ma_slots(node, mt);
1962 memcpy(b_node->slot + mab_start, slots + mas_start, sizeof(void *) * j);
1963 if (!ma_is_leaf(mt) && mt_is_alloc(mas->tree)) {
1964 gaps = ma_gaps(node, mt);
1965 memcpy(b_node->gap + mab_start, gaps + mas_start,
1966 sizeof(unsigned long) * j);
1971 * mas_leaf_set_meta() - Set the metadata of a leaf if possible.
1972 * @mas: The maple state
1973 * @node: The maple node
1974 * @pivots: pointer to the maple node pivots
1975 * @mt: The maple type
1976 * @end: The assumed end
1978 * Note, end may be incremented within this function but not modified at the
1979 * source. This is fine since the metadata is the last thing to be stored in a
1980 * node during a write.
1982 static inline void mas_leaf_set_meta(struct ma_state *mas,
1983 struct maple_node *node, unsigned long *pivots,
1984 enum maple_type mt, unsigned char end)
1986 /* There is no room for metadata already */
1987 if (mt_pivots[mt] <= end)
1990 if (pivots[end] && pivots[end] < mas->max)
1993 if (end < mt_slots[mt] - 1)
1994 ma_set_meta(node, mt, 0, end);
1998 * mab_mas_cp() - Copy data from maple_big_node to a maple encoded node.
1999 * @b_node: the maple_big_node that has the data
2000 * @mab_start: the start location in @b_node.
2001 * @mab_end: The end location in @b_node (inclusively)
2002 * @mas: The maple state with the maple encoded node.
2004 static inline void mab_mas_cp(struct maple_big_node *b_node,
2005 unsigned char mab_start, unsigned char mab_end,
2006 struct ma_state *mas, bool new_max)
2009 enum maple_type mt = mte_node_type(mas->node);
2010 struct maple_node *node = mte_to_node(mas->node);
2011 void __rcu **slots = ma_slots(node, mt);
2012 unsigned long *pivots = ma_pivots(node, mt);
2013 unsigned long *gaps = NULL;
2016 if (mab_end - mab_start > mt_pivots[mt])
2019 if (!pivots[mt_pivots[mt] - 1])
2020 slots[mt_pivots[mt]] = NULL;
2024 pivots[j++] = b_node->pivot[i++];
2025 } while (i <= mab_end && likely(b_node->pivot[i]));
2027 memcpy(slots, b_node->slot + mab_start,
2028 sizeof(void *) * (i - mab_start));
2031 mas->max = b_node->pivot[i - 1];
2034 if (likely(!ma_is_leaf(mt) && mt_is_alloc(mas->tree))) {
2035 unsigned long max_gap = 0;
2036 unsigned char offset = 15;
2038 gaps = ma_gaps(node, mt);
2040 gaps[--j] = b_node->gap[--i];
2041 if (gaps[j] > max_gap) {
2047 ma_set_meta(node, mt, offset, end);
2049 mas_leaf_set_meta(mas, node, pivots, mt, end);
2054 * mas_descend_adopt() - Descend through a sub-tree and adopt children.
2055 * @mas: the maple state with the maple encoded node of the sub-tree.
2057 * Descend through a sub-tree and adopt children who do not have the correct
2058 * parents set. Follow the parents which have the correct parents as they are
2059 * the new entries which need to be followed to find other incorrectly set
2062 static inline void mas_descend_adopt(struct ma_state *mas)
2064 struct ma_state list[3], next[3];
2068 * At each level there may be up to 3 correct parent pointers which indicates
2069 * the new nodes which need to be walked to find any new nodes at a lower level.
2072 for (i = 0; i < 3; i++) {
2079 while (!mte_is_leaf(list[0].node)) {
2081 for (i = 0; i < 3; i++) {
2082 if (mas_is_none(&list[i]))
2085 if (i && list[i-1].node == list[i].node)
2088 while ((n < 3) && (mas_new_child(&list[i], &next[n])))
2091 mas_adopt_children(&list[i], list[i].node);
2095 next[n++].node = MAS_NONE;
2097 /* descend by setting the list to the children */
2098 for (i = 0; i < 3; i++)
2104 * mas_bulk_rebalance() - Rebalance the end of a tree after a bulk insert.
2105 * @mas: The maple state
2106 * @end: The maple node end
2107 * @mt: The maple node type
2109 static inline void mas_bulk_rebalance(struct ma_state *mas, unsigned char end,
2112 if (!(mas->mas_flags & MA_STATE_BULK))
2115 if (mte_is_root(mas->node))
2118 if (end > mt_min_slots[mt]) {
2119 mas->mas_flags &= ~MA_STATE_REBALANCE;
2125 * mas_store_b_node() - Store an @entry into the b_node while also copying the
2126 * data from a maple encoded node.
2127 * @wr_mas: the maple write state
2128 * @b_node: the maple_big_node to fill with data
2129 * @offset_end: the offset to end copying
2131 * Return: The actual end of the data stored in @b_node
2133 static noinline_for_kasan void mas_store_b_node(struct ma_wr_state *wr_mas,
2134 struct maple_big_node *b_node, unsigned char offset_end)
2137 unsigned char b_end;
2138 /* Possible underflow of piv will wrap back to 0 before use. */
2140 struct ma_state *mas = wr_mas->mas;
2142 b_node->type = wr_mas->type;
2146 /* Copy start data up to insert. */
2147 mas_mab_cp(mas, 0, slot - 1, b_node, 0);
2148 b_end = b_node->b_end;
2149 piv = b_node->pivot[b_end - 1];
2153 if (piv + 1 < mas->index) {
2154 /* Handle range starting after old range */
2155 b_node->slot[b_end] = wr_mas->content;
2156 if (!wr_mas->content)
2157 b_node->gap[b_end] = mas->index - 1 - piv;
2158 b_node->pivot[b_end++] = mas->index - 1;
2161 /* Store the new entry. */
2162 mas->offset = b_end;
2163 b_node->slot[b_end] = wr_mas->entry;
2164 b_node->pivot[b_end] = mas->last;
2167 if (mas->last >= mas->max)
2170 /* Handle new range ending before old range ends */
2171 piv = mas_logical_pivot(mas, wr_mas->pivots, offset_end, wr_mas->type);
2172 if (piv > mas->last) {
2173 if (piv == ULONG_MAX)
2174 mas_bulk_rebalance(mas, b_node->b_end, wr_mas->type);
2176 if (offset_end != slot)
2177 wr_mas->content = mas_slot_locked(mas, wr_mas->slots,
2180 b_node->slot[++b_end] = wr_mas->content;
2181 if (!wr_mas->content)
2182 b_node->gap[b_end] = piv - mas->last + 1;
2183 b_node->pivot[b_end] = piv;
2186 slot = offset_end + 1;
2187 if (slot > wr_mas->node_end)
2190 /* Copy end data to the end of the node. */
2191 mas_mab_cp(mas, slot, wr_mas->node_end + 1, b_node, ++b_end);
2196 b_node->b_end = b_end;
2200 * mas_prev_sibling() - Find the previous node with the same parent.
2201 * @mas: the maple state
2203 * Return: True if there is a previous sibling, false otherwise.
2205 static inline bool mas_prev_sibling(struct ma_state *mas)
2207 unsigned int p_slot = mte_parent_slot(mas->node);
2209 if (mte_is_root(mas->node))
2216 mas->offset = p_slot - 1;
2222 * mas_next_sibling() - Find the next node with the same parent.
2223 * @mas: the maple state
2225 * Return: true if there is a next sibling, false otherwise.
2227 static inline bool mas_next_sibling(struct ma_state *mas)
2229 MA_STATE(parent, mas->tree, mas->index, mas->last);
2231 if (mte_is_root(mas->node))
2235 mas_ascend(&parent);
2236 parent.offset = mte_parent_slot(mas->node) + 1;
2237 if (parent.offset > mas_data_end(&parent))
2246 * mte_node_or_node() - Return the encoded node or MAS_NONE.
2247 * @enode: The encoded maple node.
2249 * Shorthand to avoid setting %NULLs in the tree or maple_subtree_state.
2251 * Return: @enode or MAS_NONE
2253 static inline struct maple_enode *mte_node_or_none(struct maple_enode *enode)
2258 return ma_enode_ptr(MAS_NONE);
2262 * mas_wr_node_walk() - Find the correct offset for the index in the @mas.
2263 * @wr_mas: The maple write state
2265 * Uses mas_slot_locked() and does not need to worry about dead nodes.
2267 static inline void mas_wr_node_walk(struct ma_wr_state *wr_mas)
2269 struct ma_state *mas = wr_mas->mas;
2270 unsigned char count;
2271 unsigned char offset;
2272 unsigned long index, min, max;
2274 if (unlikely(ma_is_dense(wr_mas->type))) {
2275 wr_mas->r_max = wr_mas->r_min = mas->index;
2276 mas->offset = mas->index = mas->min;
2280 wr_mas->node = mas_mn(wr_mas->mas);
2281 wr_mas->pivots = ma_pivots(wr_mas->node, wr_mas->type);
2282 count = wr_mas->node_end = ma_data_end(wr_mas->node, wr_mas->type,
2283 wr_mas->pivots, mas->max);
2284 offset = mas->offset;
2285 min = mas_safe_min(mas, wr_mas->pivots, offset);
2286 if (unlikely(offset == count))
2289 max = wr_mas->pivots[offset];
2291 if (unlikely(index <= max))
2294 if (unlikely(!max && offset))
2298 while (++offset < count) {
2299 max = wr_mas->pivots[offset];
2302 else if (unlikely(!max))
2311 wr_mas->r_max = max;
2312 wr_mas->r_min = min;
2313 wr_mas->offset_end = mas->offset = offset;
2317 * mas_topiary_range() - Add a range of slots to the topiary.
2318 * @mas: The maple state
2319 * @destroy: The topiary to add the slots (usually destroy)
2320 * @start: The starting slot inclusively
2321 * @end: The end slot inclusively
2323 static inline void mas_topiary_range(struct ma_state *mas,
2324 struct ma_topiary *destroy, unsigned char start, unsigned char end)
2327 unsigned char offset;
2329 MT_BUG_ON(mas->tree, mte_is_leaf(mas->node));
2330 slots = ma_slots(mas_mn(mas), mte_node_type(mas->node));
2331 for (offset = start; offset <= end; offset++) {
2332 struct maple_enode *enode = mas_slot_locked(mas, slots, offset);
2334 if (mte_dead_node(enode))
2337 mat_add(destroy, enode);
2342 * mast_topiary() - Add the portions of the tree to the removal list; either to
2343 * be freed or discarded (destroy walk).
2344 * @mast: The maple_subtree_state.
2346 static inline void mast_topiary(struct maple_subtree_state *mast)
2348 MA_WR_STATE(wr_mas, mast->orig_l, NULL);
2349 unsigned char r_start, r_end;
2350 unsigned char l_start, l_end;
2351 void __rcu **l_slots, **r_slots;
2353 wr_mas.type = mte_node_type(mast->orig_l->node);
2354 mast->orig_l->index = mast->orig_l->last;
2355 mas_wr_node_walk(&wr_mas);
2356 l_start = mast->orig_l->offset + 1;
2357 l_end = mas_data_end(mast->orig_l);
2359 r_end = mast->orig_r->offset;
2364 l_slots = ma_slots(mas_mn(mast->orig_l),
2365 mte_node_type(mast->orig_l->node));
2367 r_slots = ma_slots(mas_mn(mast->orig_r),
2368 mte_node_type(mast->orig_r->node));
2370 if ((l_start < l_end) &&
2371 mte_dead_node(mas_slot_locked(mast->orig_l, l_slots, l_start))) {
2375 if (mte_dead_node(mas_slot_locked(mast->orig_r, r_slots, r_end))) {
2380 if ((l_start > r_end) && (mast->orig_l->node == mast->orig_r->node))
2383 /* At the node where left and right sides meet, add the parts between */
2384 if (mast->orig_l->node == mast->orig_r->node) {
2385 return mas_topiary_range(mast->orig_l, mast->destroy,
2389 /* mast->orig_r is different and consumed. */
2390 if (mte_is_leaf(mast->orig_r->node))
2393 if (mte_dead_node(mas_slot_locked(mast->orig_l, l_slots, l_end)))
2397 if (l_start <= l_end)
2398 mas_topiary_range(mast->orig_l, mast->destroy, l_start, l_end);
2400 if (mte_dead_node(mas_slot_locked(mast->orig_r, r_slots, r_start)))
2403 if (r_start <= r_end)
2404 mas_topiary_range(mast->orig_r, mast->destroy, 0, r_end);
2408 * mast_rebalance_next() - Rebalance against the next node
2409 * @mast: The maple subtree state
2410 * @old_r: The encoded maple node to the right (next node).
2412 static inline void mast_rebalance_next(struct maple_subtree_state *mast)
2414 unsigned char b_end = mast->bn->b_end;
2416 mas_mab_cp(mast->orig_r, 0, mt_slot_count(mast->orig_r->node),
2418 mast->orig_r->last = mast->orig_r->max;
2422 * mast_rebalance_prev() - Rebalance against the previous node
2423 * @mast: The maple subtree state
2424 * @old_l: The encoded maple node to the left (previous node)
2426 static inline void mast_rebalance_prev(struct maple_subtree_state *mast)
2428 unsigned char end = mas_data_end(mast->orig_l) + 1;
2429 unsigned char b_end = mast->bn->b_end;
2431 mab_shift_right(mast->bn, end);
2432 mas_mab_cp(mast->orig_l, 0, end - 1, mast->bn, 0);
2433 mast->l->min = mast->orig_l->min;
2434 mast->orig_l->index = mast->orig_l->min;
2435 mast->bn->b_end = end + b_end;
2436 mast->l->offset += end;
2440 * mast_spanning_rebalance() - Rebalance nodes with nearest neighbour favouring
2441 * the node to the right. Checking the nodes to the right then the left at each
2442 * level upwards until root is reached. Free and destroy as needed.
2443 * Data is copied into the @mast->bn.
2444 * @mast: The maple_subtree_state.
2447 bool mast_spanning_rebalance(struct maple_subtree_state *mast)
2449 struct ma_state r_tmp = *mast->orig_r;
2450 struct ma_state l_tmp = *mast->orig_l;
2451 struct maple_enode *ancestor = NULL;
2452 unsigned char start, end;
2453 unsigned char depth = 0;
2455 r_tmp = *mast->orig_r;
2456 l_tmp = *mast->orig_l;
2458 mas_ascend(mast->orig_r);
2459 mas_ascend(mast->orig_l);
2462 (mast->orig_r->node == mast->orig_l->node)) {
2463 ancestor = mast->orig_r->node;
2464 end = mast->orig_r->offset - 1;
2465 start = mast->orig_l->offset + 1;
2468 if (mast->orig_r->offset < mas_data_end(mast->orig_r)) {
2470 ancestor = mast->orig_r->node;
2474 mast->orig_r->offset++;
2476 mas_descend(mast->orig_r);
2477 mast->orig_r->offset = 0;
2481 mast_rebalance_next(mast);
2483 unsigned char l_off = 0;
2484 struct maple_enode *child = r_tmp.node;
2487 if (ancestor == r_tmp.node)
2493 if (l_off < r_tmp.offset)
2494 mas_topiary_range(&r_tmp, mast->destroy,
2495 l_off, r_tmp.offset);
2497 if (l_tmp.node != child)
2498 mat_add(mast->free, child);
2500 } while (r_tmp.node != ancestor);
2502 *mast->orig_l = l_tmp;
2505 } else if (mast->orig_l->offset != 0) {
2507 ancestor = mast->orig_l->node;
2508 end = mas_data_end(mast->orig_l);
2511 mast->orig_l->offset--;
2513 mas_descend(mast->orig_l);
2514 mast->orig_l->offset =
2515 mas_data_end(mast->orig_l);
2519 mast_rebalance_prev(mast);
2521 unsigned char r_off;
2522 struct maple_enode *child = l_tmp.node;
2525 if (ancestor == l_tmp.node)
2528 r_off = mas_data_end(&l_tmp);
2530 if (l_tmp.offset < r_off)
2533 if (l_tmp.offset < r_off)
2534 mas_topiary_range(&l_tmp, mast->destroy,
2535 l_tmp.offset, r_off);
2537 if (r_tmp.node != child)
2538 mat_add(mast->free, child);
2540 } while (l_tmp.node != ancestor);
2542 *mast->orig_r = r_tmp;
2545 } while (!mte_is_root(mast->orig_r->node));
2547 *mast->orig_r = r_tmp;
2548 *mast->orig_l = l_tmp;
2553 * mast_ascend_free() - Add current original maple state nodes to the free list
2555 * @mast: the maple subtree state.
2557 * Ascend the original left and right sides and add the previous nodes to the
2558 * free list. Set the slots to point to the correct location in the new nodes.
2561 mast_ascend_free(struct maple_subtree_state *mast)
2563 MA_WR_STATE(wr_mas, mast->orig_r, NULL);
2564 struct maple_enode *left = mast->orig_l->node;
2565 struct maple_enode *right = mast->orig_r->node;
2567 mas_ascend(mast->orig_l);
2568 mas_ascend(mast->orig_r);
2569 mat_add(mast->free, left);
2572 mat_add(mast->free, right);
2574 mast->orig_r->offset = 0;
2575 mast->orig_r->index = mast->r->max;
2576 /* last should be larger than or equal to index */
2577 if (mast->orig_r->last < mast->orig_r->index)
2578 mast->orig_r->last = mast->orig_r->index;
2580 * The node may not contain the value so set slot to ensure all
2581 * of the nodes contents are freed or destroyed.
2583 wr_mas.type = mte_node_type(mast->orig_r->node);
2584 mas_wr_node_walk(&wr_mas);
2585 /* Set up the left side of things */
2586 mast->orig_l->offset = 0;
2587 mast->orig_l->index = mast->l->min;
2588 wr_mas.mas = mast->orig_l;
2589 wr_mas.type = mte_node_type(mast->orig_l->node);
2590 mas_wr_node_walk(&wr_mas);
2592 mast->bn->type = wr_mas.type;
2596 * mas_new_ma_node() - Create and return a new maple node. Helper function.
2597 * @mas: the maple state with the allocations.
2598 * @b_node: the maple_big_node with the type encoding.
2600 * Use the node type from the maple_big_node to allocate a new node from the
2601 * ma_state. This function exists mainly for code readability.
2603 * Return: A new maple encoded node
2605 static inline struct maple_enode
2606 *mas_new_ma_node(struct ma_state *mas, struct maple_big_node *b_node)
2608 return mt_mk_node(ma_mnode_ptr(mas_pop_node(mas)), b_node->type);
2612 * mas_mab_to_node() - Set up right and middle nodes
2614 * @mas: the maple state that contains the allocations.
2615 * @b_node: the node which contains the data.
2616 * @left: The pointer which will have the left node
2617 * @right: The pointer which may have the right node
2618 * @middle: the pointer which may have the middle node (rare)
2619 * @mid_split: the split location for the middle node
2621 * Return: the split of left.
2623 static inline unsigned char mas_mab_to_node(struct ma_state *mas,
2624 struct maple_big_node *b_node, struct maple_enode **left,
2625 struct maple_enode **right, struct maple_enode **middle,
2626 unsigned char *mid_split, unsigned long min)
2628 unsigned char split = 0;
2629 unsigned char slot_count = mt_slots[b_node->type];
2631 *left = mas_new_ma_node(mas, b_node);
2636 if (b_node->b_end < slot_count) {
2637 split = b_node->b_end;
2639 split = mab_calc_split(mas, b_node, mid_split, min);
2640 *right = mas_new_ma_node(mas, b_node);
2644 *middle = mas_new_ma_node(mas, b_node);
2651 * mab_set_b_end() - Add entry to b_node at b_node->b_end and increment the end
2653 * @b_node - the big node to add the entry
2654 * @mas - the maple state to get the pivot (mas->max)
2655 * @entry - the entry to add, if NULL nothing happens.
2657 static inline void mab_set_b_end(struct maple_big_node *b_node,
2658 struct ma_state *mas,
2664 b_node->slot[b_node->b_end] = entry;
2665 if (mt_is_alloc(mas->tree))
2666 b_node->gap[b_node->b_end] = mas_max_gap(mas);
2667 b_node->pivot[b_node->b_end++] = mas->max;
2671 * mas_set_split_parent() - combine_then_separate helper function. Sets the parent
2672 * of @mas->node to either @left or @right, depending on @slot and @split
2674 * @mas - the maple state with the node that needs a parent
2675 * @left - possible parent 1
2676 * @right - possible parent 2
2677 * @slot - the slot the mas->node was placed
2678 * @split - the split location between @left and @right
2680 static inline void mas_set_split_parent(struct ma_state *mas,
2681 struct maple_enode *left,
2682 struct maple_enode *right,
2683 unsigned char *slot, unsigned char split)
2685 if (mas_is_none(mas))
2688 if ((*slot) <= split)
2689 mte_set_parent(mas->node, left, *slot);
2691 mte_set_parent(mas->node, right, (*slot) - split - 1);
2697 * mte_mid_split_check() - Check if the next node passes the mid-split
2698 * @**l: Pointer to left encoded maple node.
2699 * @**m: Pointer to middle encoded maple node.
2700 * @**r: Pointer to right encoded maple node.
2702 * @*split: The split location.
2703 * @mid_split: The middle split.
2705 static inline void mte_mid_split_check(struct maple_enode **l,
2706 struct maple_enode **r,
2707 struct maple_enode *right,
2709 unsigned char *split,
2710 unsigned char mid_split)
2715 if (slot < mid_split)
2724 * mast_set_split_parents() - Helper function to set three nodes parents. Slot
2725 * is taken from @mast->l.
2726 * @mast - the maple subtree state
2727 * @left - the left node
2728 * @right - the right node
2729 * @split - the split location.
2731 static inline void mast_set_split_parents(struct maple_subtree_state *mast,
2732 struct maple_enode *left,
2733 struct maple_enode *middle,
2734 struct maple_enode *right,
2735 unsigned char split,
2736 unsigned char mid_split)
2739 struct maple_enode *l = left;
2740 struct maple_enode *r = right;
2742 if (mas_is_none(mast->l))
2748 slot = mast->l->offset;
2750 mte_mid_split_check(&l, &r, right, slot, &split, mid_split);
2751 mas_set_split_parent(mast->l, l, r, &slot, split);
2753 mte_mid_split_check(&l, &r, right, slot, &split, mid_split);
2754 mas_set_split_parent(mast->m, l, r, &slot, split);
2756 mte_mid_split_check(&l, &r, right, slot, &split, mid_split);
2757 mas_set_split_parent(mast->r, l, r, &slot, split);
2761 * mas_wmb_replace() - Write memory barrier and replace
2762 * @mas: The maple state
2763 * @free: the maple topiary list of nodes to free
2764 * @destroy: The maple topiary list of nodes to destroy (walk and free)
2766 * Updates gap as necessary.
2768 static inline void mas_wmb_replace(struct ma_state *mas,
2769 struct ma_topiary *free,
2770 struct ma_topiary *destroy)
2772 /* All nodes must see old data as dead prior to replacing that data */
2773 smp_wmb(); /* Needed for RCU */
2775 /* Insert the new data in the tree */
2776 mas_replace(mas, true);
2778 if (!mte_is_leaf(mas->node))
2779 mas_descend_adopt(mas);
2781 mas_mat_free(mas, free);
2784 mas_mat_destroy(mas, destroy);
2786 if (mte_is_leaf(mas->node))
2789 mas_update_gap(mas);
2793 * mast_new_root() - Set a new tree root during subtree creation
2794 * @mast: The maple subtree state
2795 * @mas: The maple state
2797 static inline void mast_new_root(struct maple_subtree_state *mast,
2798 struct ma_state *mas)
2800 mas_mn(mast->l)->parent =
2801 ma_parent_ptr(((unsigned long)mas->tree | MA_ROOT_PARENT));
2802 if (!mte_dead_node(mast->orig_l->node) &&
2803 !mte_is_root(mast->orig_l->node)) {
2805 mast_ascend_free(mast);
2807 } while (!mte_is_root(mast->orig_l->node));
2809 if ((mast->orig_l->node != mas->node) &&
2810 (mast->l->depth > mas_mt_height(mas))) {
2811 mat_add(mast->free, mas->node);
2816 * mast_cp_to_nodes() - Copy data out to nodes.
2817 * @mast: The maple subtree state
2818 * @left: The left encoded maple node
2819 * @middle: The middle encoded maple node
2820 * @right: The right encoded maple node
2821 * @split: The location to split between left and (middle ? middle : right)
2822 * @mid_split: The location to split between middle and right.
2824 static inline void mast_cp_to_nodes(struct maple_subtree_state *mast,
2825 struct maple_enode *left, struct maple_enode *middle,
2826 struct maple_enode *right, unsigned char split, unsigned char mid_split)
2828 bool new_lmax = true;
2830 mast->l->node = mte_node_or_none(left);
2831 mast->m->node = mte_node_or_none(middle);
2832 mast->r->node = mte_node_or_none(right);
2834 mast->l->min = mast->orig_l->min;
2835 if (split == mast->bn->b_end) {
2836 mast->l->max = mast->orig_r->max;
2840 mab_mas_cp(mast->bn, 0, split, mast->l, new_lmax);
2843 mab_mas_cp(mast->bn, 1 + split, mid_split, mast->m, true);
2844 mast->m->min = mast->bn->pivot[split] + 1;
2848 mast->r->max = mast->orig_r->max;
2850 mab_mas_cp(mast->bn, 1 + split, mast->bn->b_end, mast->r, false);
2851 mast->r->min = mast->bn->pivot[split] + 1;
2856 * mast_combine_cp_left - Copy in the original left side of the tree into the
2857 * combined data set in the maple subtree state big node.
2858 * @mast: The maple subtree state
2860 static inline void mast_combine_cp_left(struct maple_subtree_state *mast)
2862 unsigned char l_slot = mast->orig_l->offset;
2867 mas_mab_cp(mast->orig_l, 0, l_slot - 1, mast->bn, 0);
2871 * mast_combine_cp_right: Copy in the original right side of the tree into the
2872 * combined data set in the maple subtree state big node.
2873 * @mast: The maple subtree state
2875 static inline void mast_combine_cp_right(struct maple_subtree_state *mast)
2877 if (mast->bn->pivot[mast->bn->b_end - 1] >= mast->orig_r->max)
2880 mas_mab_cp(mast->orig_r, mast->orig_r->offset + 1,
2881 mt_slot_count(mast->orig_r->node), mast->bn,
2883 mast->orig_r->last = mast->orig_r->max;
2887 * mast_sufficient: Check if the maple subtree state has enough data in the big
2888 * node to create at least one sufficient node
2889 * @mast: the maple subtree state
2891 static inline bool mast_sufficient(struct maple_subtree_state *mast)
2893 if (mast->bn->b_end > mt_min_slot_count(mast->orig_l->node))
2900 * mast_overflow: Check if there is too much data in the subtree state for a
2902 * @mast: The maple subtree state
2904 static inline bool mast_overflow(struct maple_subtree_state *mast)
2906 if (mast->bn->b_end >= mt_slot_count(mast->orig_l->node))
2912 static inline void *mtree_range_walk(struct ma_state *mas)
2914 unsigned long *pivots;
2915 unsigned char offset;
2916 struct maple_node *node;
2917 struct maple_enode *next, *last;
2918 enum maple_type type;
2921 unsigned long max, min;
2922 unsigned long prev_max, prev_min;
2930 node = mte_to_node(next);
2931 type = mte_node_type(next);
2932 pivots = ma_pivots(node, type);
2933 end = ma_data_end(node, type, pivots, max);
2934 if (unlikely(ma_dead_node(node)))
2937 if (pivots[offset] >= mas->index) {
2940 max = pivots[offset];
2946 } while ((offset < end) && (pivots[offset] < mas->index));
2949 min = pivots[offset - 1] + 1;
2951 if (likely(offset < end && pivots[offset]))
2952 max = pivots[offset];
2955 slots = ma_slots(node, type);
2956 next = mt_slot(mas->tree, slots, offset);
2957 if (unlikely(ma_dead_node(node)))
2959 } while (!ma_is_leaf(type));
2961 mas->offset = offset;
2964 mas->min = prev_min;
2965 mas->max = prev_max;
2967 return (void *)next;
2975 * mas_spanning_rebalance() - Rebalance across two nodes which may not be peers.
2976 * @mas: The starting maple state
2977 * @mast: The maple_subtree_state, keeps track of 4 maple states.
2978 * @count: The estimated count of iterations needed.
2980 * Follow the tree upwards from @l_mas and @r_mas for @count, or until the root
2981 * is hit. First @b_node is split into two entries which are inserted into the
2982 * next iteration of the loop. @b_node is returned populated with the final
2983 * iteration. @mas is used to obtain allocations. orig_l_mas keeps track of the
2984 * nodes that will remain active by using orig_l_mas->index and orig_l_mas->last
2985 * to account of what has been copied into the new sub-tree. The update of
2986 * orig_l_mas->last is used in mas_consume to find the slots that will need to
2987 * be either freed or destroyed. orig_l_mas->depth keeps track of the height of
2988 * the new sub-tree in case the sub-tree becomes the full tree.
2990 * Return: the number of elements in b_node during the last loop.
2992 static int mas_spanning_rebalance(struct ma_state *mas,
2993 struct maple_subtree_state *mast, unsigned char count)
2995 unsigned char split, mid_split;
2996 unsigned char slot = 0;
2997 struct maple_enode *left = NULL, *middle = NULL, *right = NULL;
2999 MA_STATE(l_mas, mas->tree, mas->index, mas->index);
3000 MA_STATE(r_mas, mas->tree, mas->index, mas->last);
3001 MA_STATE(m_mas, mas->tree, mas->index, mas->index);
3002 MA_TOPIARY(free, mas->tree);
3003 MA_TOPIARY(destroy, mas->tree);
3006 * The tree needs to be rebalanced and leaves need to be kept at the same level.
3007 * Rebalancing is done by use of the ``struct maple_topiary``.
3013 mast->destroy = &destroy;
3014 l_mas.node = r_mas.node = m_mas.node = MAS_NONE;
3016 /* Check if this is not root and has sufficient data. */
3017 if (((mast->orig_l->min != 0) || (mast->orig_r->max != ULONG_MAX)) &&
3018 unlikely(mast->bn->b_end <= mt_min_slots[mast->bn->type]))
3019 mast_spanning_rebalance(mast);
3021 mast->orig_l->depth = 0;
3024 * Each level of the tree is examined and balanced, pushing data to the left or
3025 * right, or rebalancing against left or right nodes is employed to avoid
3026 * rippling up the tree to limit the amount of churn. Once a new sub-section of
3027 * the tree is created, there may be a mix of new and old nodes. The old nodes
3028 * will have the incorrect parent pointers and currently be in two trees: the
3029 * original tree and the partially new tree. To remedy the parent pointers in
3030 * the old tree, the new data is swapped into the active tree and a walk down
3031 * the tree is performed and the parent pointers are updated.
3032 * See mas_descend_adopt() for more information..
3036 mast->bn->type = mte_node_type(mast->orig_l->node);
3037 split = mas_mab_to_node(mas, mast->bn, &left, &right, &middle,
3038 &mid_split, mast->orig_l->min);
3039 mast_set_split_parents(mast, left, middle, right, split,
3041 mast_cp_to_nodes(mast, left, middle, right, split, mid_split);
3044 * Copy data from next level in the tree to mast->bn from next
3047 memset(mast->bn, 0, sizeof(struct maple_big_node));
3048 mast->bn->type = mte_node_type(left);
3049 mast->orig_l->depth++;
3051 /* Root already stored in l->node. */
3052 if (mas_is_root_limits(mast->l))
3055 mast_ascend_free(mast);
3056 mast_combine_cp_left(mast);
3057 l_mas.offset = mast->bn->b_end;
3058 mab_set_b_end(mast->bn, &l_mas, left);
3059 mab_set_b_end(mast->bn, &m_mas, middle);
3060 mab_set_b_end(mast->bn, &r_mas, right);
3062 /* Copy anything necessary out of the right node. */
3063 mast_combine_cp_right(mast);
3065 mast->orig_l->last = mast->orig_l->max;
3067 if (mast_sufficient(mast))
3070 if (mast_overflow(mast))
3073 /* May be a new root stored in mast->bn */
3074 if (mas_is_root_limits(mast->orig_l))
3077 mast_spanning_rebalance(mast);
3079 /* rebalancing from other nodes may require another loop. */
3084 l_mas.node = mt_mk_node(ma_mnode_ptr(mas_pop_node(mas)),
3085 mte_node_type(mast->orig_l->node));
3086 mast->orig_l->depth++;
3087 mab_mas_cp(mast->bn, 0, mt_slots[mast->bn->type] - 1, &l_mas, true);
3088 mte_set_parent(left, l_mas.node, slot);
3090 mte_set_parent(middle, l_mas.node, ++slot);
3093 mte_set_parent(right, l_mas.node, ++slot);
3095 if (mas_is_root_limits(mast->l)) {
3097 mast_new_root(mast, mas);
3099 mas_mn(&l_mas)->parent = mas_mn(mast->orig_l)->parent;
3102 if (!mte_dead_node(mast->orig_l->node))
3103 mat_add(&free, mast->orig_l->node);
3105 mas->depth = mast->orig_l->depth;
3106 *mast->orig_l = l_mas;
3107 mte_set_node_dead(mas->node);
3109 /* Set up mas for insertion. */
3110 mast->orig_l->depth = mas->depth;
3111 mast->orig_l->alloc = mas->alloc;
3112 *mas = *mast->orig_l;
3113 mas_wmb_replace(mas, &free, &destroy);
3114 mtree_range_walk(mas);
3115 return mast->bn->b_end;
3119 * mas_rebalance() - Rebalance a given node.
3120 * @mas: The maple state
3121 * @b_node: The big maple node.
3123 * Rebalance two nodes into a single node or two new nodes that are sufficient.
3124 * Continue upwards until tree is sufficient.
3126 * Return: the number of elements in b_node during the last loop.
3128 static inline int mas_rebalance(struct ma_state *mas,
3129 struct maple_big_node *b_node)
3131 char empty_count = mas_mt_height(mas);
3132 struct maple_subtree_state mast;
3133 unsigned char shift, b_end = ++b_node->b_end;
3135 MA_STATE(l_mas, mas->tree, mas->index, mas->last);
3136 MA_STATE(r_mas, mas->tree, mas->index, mas->last);
3138 trace_ma_op(__func__, mas);
3141 * Rebalancing occurs if a node is insufficient. Data is rebalanced
3142 * against the node to the right if it exists, otherwise the node to the
3143 * left of this node is rebalanced against this node. If rebalancing
3144 * causes just one node to be produced instead of two, then the parent
3145 * is also examined and rebalanced if it is insufficient. Every level
3146 * tries to combine the data in the same way. If one node contains the
3147 * entire range of the tree, then that node is used as a new root node.
3149 mas_node_count(mas, 1 + empty_count * 3);
3150 if (mas_is_err(mas))
3153 mast.orig_l = &l_mas;
3154 mast.orig_r = &r_mas;
3156 mast.bn->type = mte_node_type(mas->node);
3158 l_mas = r_mas = *mas;
3160 if (mas_next_sibling(&r_mas)) {
3161 mas_mab_cp(&r_mas, 0, mt_slot_count(r_mas.node), b_node, b_end);
3162 r_mas.last = r_mas.index = r_mas.max;
3164 mas_prev_sibling(&l_mas);
3165 shift = mas_data_end(&l_mas) + 1;
3166 mab_shift_right(b_node, shift);
3167 mas->offset += shift;
3168 mas_mab_cp(&l_mas, 0, shift - 1, b_node, 0);
3169 b_node->b_end = shift + b_end;
3170 l_mas.index = l_mas.last = l_mas.min;
3173 return mas_spanning_rebalance(mas, &mast, empty_count);
3177 * mas_destroy_rebalance() - Rebalance left-most node while destroying the maple
3179 * @mas: The maple state
3180 * @end: The end of the left-most node.
3182 * During a mass-insert event (such as forking), it may be necessary to
3183 * rebalance the left-most node when it is not sufficient.
3185 static inline void mas_destroy_rebalance(struct ma_state *mas, unsigned char end)
3187 enum maple_type mt = mte_node_type(mas->node);
3188 struct maple_node reuse, *newnode, *parent, *new_left, *left, *node;
3189 struct maple_enode *eparent;
3190 unsigned char offset, tmp, split = mt_slots[mt] / 2;
3191 void __rcu **l_slots, **slots;
3192 unsigned long *l_pivs, *pivs, gap;
3193 bool in_rcu = mt_in_rcu(mas->tree);
3195 MA_STATE(l_mas, mas->tree, mas->index, mas->last);
3198 mas_prev_sibling(&l_mas);
3202 /* Allocate for both left and right as well as parent. */
3203 mas_node_count(mas, 3);
3204 if (mas_is_err(mas))
3207 newnode = mas_pop_node(mas);
3213 newnode->parent = node->parent;
3214 slots = ma_slots(newnode, mt);
3215 pivs = ma_pivots(newnode, mt);
3216 left = mas_mn(&l_mas);
3217 l_slots = ma_slots(left, mt);
3218 l_pivs = ma_pivots(left, mt);
3219 if (!l_slots[split])
3221 tmp = mas_data_end(&l_mas) - split;
3223 memcpy(slots, l_slots + split + 1, sizeof(void *) * tmp);
3224 memcpy(pivs, l_pivs + split + 1, sizeof(unsigned long) * tmp);
3225 pivs[tmp] = l_mas.max;
3226 memcpy(slots + tmp, ma_slots(node, mt), sizeof(void *) * end);
3227 memcpy(pivs + tmp, ma_pivots(node, mt), sizeof(unsigned long) * end);
3229 l_mas.max = l_pivs[split];
3230 mas->min = l_mas.max + 1;
3231 eparent = mt_mk_node(mte_parent(l_mas.node),
3232 mas_parent_enum(&l_mas, l_mas.node));
3235 unsigned char max_p = mt_pivots[mt];
3236 unsigned char max_s = mt_slots[mt];
3239 memset(pivs + tmp, 0,
3240 sizeof(unsigned long *) * (max_p - tmp));
3242 if (tmp < mt_slots[mt])
3243 memset(slots + tmp, 0, sizeof(void *) * (max_s - tmp));
3245 memcpy(node, newnode, sizeof(struct maple_node));
3246 ma_set_meta(node, mt, 0, tmp - 1);
3247 mte_set_pivot(eparent, mte_parent_slot(l_mas.node),
3250 /* Remove data from l_pivs. */
3252 memset(l_pivs + tmp, 0, sizeof(unsigned long) * (max_p - tmp));
3253 memset(l_slots + tmp, 0, sizeof(void *) * (max_s - tmp));
3254 ma_set_meta(left, mt, 0, split);
3259 /* RCU requires replacing both l_mas, mas, and parent. */
3260 mas->node = mt_mk_node(newnode, mt);
3261 ma_set_meta(newnode, mt, 0, tmp);
3263 new_left = mas_pop_node(mas);
3264 new_left->parent = left->parent;
3265 mt = mte_node_type(l_mas.node);
3266 slots = ma_slots(new_left, mt);
3267 pivs = ma_pivots(new_left, mt);
3268 memcpy(slots, l_slots, sizeof(void *) * split);
3269 memcpy(pivs, l_pivs, sizeof(unsigned long) * split);
3270 ma_set_meta(new_left, mt, 0, split);
3271 l_mas.node = mt_mk_node(new_left, mt);
3273 /* replace parent. */
3274 offset = mte_parent_slot(mas->node);
3275 mt = mas_parent_enum(&l_mas, l_mas.node);
3276 parent = mas_pop_node(mas);
3277 slots = ma_slots(parent, mt);
3278 pivs = ma_pivots(parent, mt);
3279 memcpy(parent, mte_to_node(eparent), sizeof(struct maple_node));
3280 rcu_assign_pointer(slots[offset], mas->node);
3281 rcu_assign_pointer(slots[offset - 1], l_mas.node);
3282 pivs[offset - 1] = l_mas.max;
3283 eparent = mt_mk_node(parent, mt);
3285 gap = mas_leaf_max_gap(mas);
3286 mte_set_gap(eparent, mte_parent_slot(mas->node), gap);
3287 gap = mas_leaf_max_gap(&l_mas);
3288 mte_set_gap(eparent, mte_parent_slot(l_mas.node), gap);
3292 mas_replace(mas, false);
3294 mas_update_gap(mas);
3298 * mas_split_final_node() - Split the final node in a subtree operation.
3299 * @mast: the maple subtree state
3300 * @mas: The maple state
3301 * @height: The height of the tree in case it's a new root.
3303 static inline bool mas_split_final_node(struct maple_subtree_state *mast,
3304 struct ma_state *mas, int height)
3306 struct maple_enode *ancestor;
3308 if (mte_is_root(mas->node)) {
3309 if (mt_is_alloc(mas->tree))
3310 mast->bn->type = maple_arange_64;
3312 mast->bn->type = maple_range_64;
3313 mas->depth = height;
3316 * Only a single node is used here, could be root.
3317 * The Big_node data should just fit in a single node.
3319 ancestor = mas_new_ma_node(mas, mast->bn);
3320 mte_set_parent(mast->l->node, ancestor, mast->l->offset);
3321 mte_set_parent(mast->r->node, ancestor, mast->r->offset);
3322 mte_to_node(ancestor)->parent = mas_mn(mas)->parent;
3324 mast->l->node = ancestor;
3325 mab_mas_cp(mast->bn, 0, mt_slots[mast->bn->type] - 1, mast->l, true);
3326 mas->offset = mast->bn->b_end - 1;
3331 * mast_fill_bnode() - Copy data into the big node in the subtree state
3332 * @mast: The maple subtree state
3333 * @mas: the maple state
3334 * @skip: The number of entries to skip for new nodes insertion.
3336 static inline void mast_fill_bnode(struct maple_subtree_state *mast,
3337 struct ma_state *mas,
3341 struct maple_enode *old = mas->node;
3342 unsigned char split;
3344 memset(mast->bn->gap, 0, sizeof(unsigned long) * ARRAY_SIZE(mast->bn->gap));
3345 memset(mast->bn->slot, 0, sizeof(unsigned long) * ARRAY_SIZE(mast->bn->slot));
3346 memset(mast->bn->pivot, 0, sizeof(unsigned long) * ARRAY_SIZE(mast->bn->pivot));
3347 mast->bn->b_end = 0;
3349 if (mte_is_root(mas->node)) {
3353 mat_add(mast->free, old);
3354 mas->offset = mte_parent_slot(mas->node);
3357 if (cp && mast->l->offset)
3358 mas_mab_cp(mas, 0, mast->l->offset - 1, mast->bn, 0);
3360 split = mast->bn->b_end;
3361 mab_set_b_end(mast->bn, mast->l, mast->l->node);
3362 mast->r->offset = mast->bn->b_end;
3363 mab_set_b_end(mast->bn, mast->r, mast->r->node);
3364 if (mast->bn->pivot[mast->bn->b_end - 1] == mas->max)
3368 mas_mab_cp(mas, split + skip, mt_slot_count(mas->node) - 1,
3369 mast->bn, mast->bn->b_end);
3372 mast->bn->type = mte_node_type(mas->node);
3376 * mast_split_data() - Split the data in the subtree state big node into regular
3378 * @mast: The maple subtree state
3379 * @mas: The maple state
3380 * @split: The location to split the big node
3382 static inline void mast_split_data(struct maple_subtree_state *mast,
3383 struct ma_state *mas, unsigned char split)
3385 unsigned char p_slot;
3387 mab_mas_cp(mast->bn, 0, split, mast->l, true);
3388 mte_set_pivot(mast->r->node, 0, mast->r->max);
3389 mab_mas_cp(mast->bn, split + 1, mast->bn->b_end, mast->r, false);
3390 mast->l->offset = mte_parent_slot(mas->node);
3391 mast->l->max = mast->bn->pivot[split];
3392 mast->r->min = mast->l->max + 1;
3393 if (mte_is_leaf(mas->node))
3396 p_slot = mast->orig_l->offset;
3397 mas_set_split_parent(mast->orig_l, mast->l->node, mast->r->node,
3399 mas_set_split_parent(mast->orig_r, mast->l->node, mast->r->node,
3404 * mas_push_data() - Instead of splitting a node, it is beneficial to push the
3405 * data to the right or left node if there is room.
3406 * @mas: The maple state
3407 * @height: The current height of the maple state
3408 * @mast: The maple subtree state
3409 * @left: Push left or not.
3411 * Keeping the height of the tree low means faster lookups.
3413 * Return: True if pushed, false otherwise.
3415 static inline bool mas_push_data(struct ma_state *mas, int height,
3416 struct maple_subtree_state *mast, bool left)
3418 unsigned char slot_total = mast->bn->b_end;
3419 unsigned char end, space, split;
3421 MA_STATE(tmp_mas, mas->tree, mas->index, mas->last);
3423 tmp_mas.depth = mast->l->depth;
3425 if (left && !mas_prev_sibling(&tmp_mas))
3427 else if (!left && !mas_next_sibling(&tmp_mas))
3430 end = mas_data_end(&tmp_mas);
3432 space = 2 * mt_slot_count(mas->node) - 2;
3433 /* -2 instead of -1 to ensure there isn't a triple split */
3434 if (ma_is_leaf(mast->bn->type))
3437 if (mas->max == ULONG_MAX)
3440 if (slot_total >= space)
3443 /* Get the data; Fill mast->bn */
3446 mab_shift_right(mast->bn, end + 1);
3447 mas_mab_cp(&tmp_mas, 0, end, mast->bn, 0);
3448 mast->bn->b_end = slot_total + 1;
3450 mas_mab_cp(&tmp_mas, 0, end, mast->bn, mast->bn->b_end);
3453 /* Configure mast for splitting of mast->bn */
3454 split = mt_slots[mast->bn->type] - 2;
3456 /* Switch mas to prev node */
3457 mat_add(mast->free, mas->node);
3459 /* Start using mast->l for the left side. */
3460 tmp_mas.node = mast->l->node;
3463 mat_add(mast->free, tmp_mas.node);
3464 tmp_mas.node = mast->r->node;
3466 split = slot_total - split;
3468 split = mab_no_null_split(mast->bn, split, mt_slots[mast->bn->type]);
3469 /* Update parent slot for split calculation. */
3471 mast->orig_l->offset += end + 1;
3473 mast_split_data(mast, mas, split);
3474 mast_fill_bnode(mast, mas, 2);
3475 mas_split_final_node(mast, mas, height + 1);
3480 * mas_split() - Split data that is too big for one node into two.
3481 * @mas: The maple state
3482 * @b_node: The maple big node
3483 * Return: 1 on success, 0 on failure.
3485 static int mas_split(struct ma_state *mas, struct maple_big_node *b_node)
3487 struct maple_subtree_state mast;
3489 unsigned char mid_split, split = 0;
3492 * Splitting is handled differently from any other B-tree; the Maple
3493 * Tree splits upwards. Splitting up means that the split operation
3494 * occurs when the walk of the tree hits the leaves and not on the way
3495 * down. The reason for splitting up is that it is impossible to know
3496 * how much space will be needed until the leaf is (or leaves are)
3497 * reached. Since overwriting data is allowed and a range could
3498 * overwrite more than one range or result in changing one entry into 3
3499 * entries, it is impossible to know if a split is required until the
3502 * Splitting is a balancing act between keeping allocations to a minimum
3503 * and avoiding a 'jitter' event where a tree is expanded to make room
3504 * for an entry followed by a contraction when the entry is removed. To
3505 * accomplish the balance, there are empty slots remaining in both left
3506 * and right nodes after a split.
3508 MA_STATE(l_mas, mas->tree, mas->index, mas->last);
3509 MA_STATE(r_mas, mas->tree, mas->index, mas->last);
3510 MA_STATE(prev_l_mas, mas->tree, mas->index, mas->last);
3511 MA_STATE(prev_r_mas, mas->tree, mas->index, mas->last);
3512 MA_TOPIARY(mat, mas->tree);
3514 trace_ma_op(__func__, mas);
3515 mas->depth = mas_mt_height(mas);
3516 /* Allocation failures will happen early. */
3517 mas_node_count(mas, 1 + mas->depth * 2);
3518 if (mas_is_err(mas))
3523 mast.orig_l = &prev_l_mas;
3524 mast.orig_r = &prev_r_mas;
3528 while (height++ <= mas->depth) {
3529 if (mt_slots[b_node->type] > b_node->b_end) {
3530 mas_split_final_node(&mast, mas, height);
3534 l_mas = r_mas = *mas;
3535 l_mas.node = mas_new_ma_node(mas, b_node);
3536 r_mas.node = mas_new_ma_node(mas, b_node);
3538 * Another way that 'jitter' is avoided is to terminate a split up early if the
3539 * left or right node has space to spare. This is referred to as "pushing left"
3540 * or "pushing right" and is similar to the B* tree, except the nodes left or
3541 * right can rarely be reused due to RCU, but the ripple upwards is halted which
3542 * is a significant savings.
3544 /* Try to push left. */
3545 if (mas_push_data(mas, height, &mast, true))
3548 /* Try to push right. */
3549 if (mas_push_data(mas, height, &mast, false))
3552 split = mab_calc_split(mas, b_node, &mid_split, prev_l_mas.min);
3553 mast_split_data(&mast, mas, split);
3555 * Usually correct, mab_mas_cp in the above call overwrites
3558 mast.r->max = mas->max;
3559 mast_fill_bnode(&mast, mas, 1);
3560 prev_l_mas = *mast.l;
3561 prev_r_mas = *mast.r;
3564 /* Set the original node as dead */
3565 mat_add(mast.free, mas->node);
3566 mas->node = l_mas.node;
3567 mas_wmb_replace(mas, mast.free, NULL);
3568 mtree_range_walk(mas);
3573 * mas_reuse_node() - Reuse the node to store the data.
3574 * @wr_mas: The maple write state
3575 * @bn: The maple big node
3576 * @end: The end of the data.
3578 * Will always return false in RCU mode.
3580 * Return: True if node was reused, false otherwise.
3582 static inline bool mas_reuse_node(struct ma_wr_state *wr_mas,
3583 struct maple_big_node *bn, unsigned char end)
3585 /* Need to be rcu safe. */
3586 if (mt_in_rcu(wr_mas->mas->tree))
3589 if (end > bn->b_end) {
3590 int clear = mt_slots[wr_mas->type] - bn->b_end;
3592 memset(wr_mas->slots + bn->b_end, 0, sizeof(void *) * clear--);
3593 memset(wr_mas->pivots + bn->b_end, 0, sizeof(void *) * clear);
3595 mab_mas_cp(bn, 0, bn->b_end, wr_mas->mas, false);
3600 * mas_commit_b_node() - Commit the big node into the tree.
3601 * @wr_mas: The maple write state
3602 * @b_node: The maple big node
3603 * @end: The end of the data.
3605 static noinline_for_kasan int mas_commit_b_node(struct ma_wr_state *wr_mas,
3606 struct maple_big_node *b_node, unsigned char end)
3608 struct maple_node *node;
3609 unsigned char b_end = b_node->b_end;
3610 enum maple_type b_type = b_node->type;
3612 if ((b_end < mt_min_slots[b_type]) &&
3613 (!mte_is_root(wr_mas->mas->node)) &&
3614 (mas_mt_height(wr_mas->mas) > 1))
3615 return mas_rebalance(wr_mas->mas, b_node);
3617 if (b_end >= mt_slots[b_type])
3618 return mas_split(wr_mas->mas, b_node);
3620 if (mas_reuse_node(wr_mas, b_node, end))
3623 mas_node_count(wr_mas->mas, 1);
3624 if (mas_is_err(wr_mas->mas))
3627 node = mas_pop_node(wr_mas->mas);
3628 node->parent = mas_mn(wr_mas->mas)->parent;
3629 wr_mas->mas->node = mt_mk_node(node, b_type);
3630 mab_mas_cp(b_node, 0, b_end, wr_mas->mas, false);
3631 mas_replace(wr_mas->mas, false);
3633 mas_update_gap(wr_mas->mas);
3638 * mas_root_expand() - Expand a root to a node
3639 * @mas: The maple state
3640 * @entry: The entry to store into the tree
3642 static inline int mas_root_expand(struct ma_state *mas, void *entry)
3644 void *contents = mas_root_locked(mas);
3645 enum maple_type type = maple_leaf_64;
3646 struct maple_node *node;
3648 unsigned long *pivots;
3651 mas_node_count(mas, 1);
3652 if (unlikely(mas_is_err(mas)))
3655 node = mas_pop_node(mas);
3656 pivots = ma_pivots(node, type);
3657 slots = ma_slots(node, type);
3658 node->parent = ma_parent_ptr(
3659 ((unsigned long)mas->tree | MA_ROOT_PARENT));
3660 mas->node = mt_mk_node(node, type);
3664 rcu_assign_pointer(slots[slot], contents);
3665 if (likely(mas->index > 1))
3668 pivots[slot++] = mas->index - 1;
3671 rcu_assign_pointer(slots[slot], entry);
3673 pivots[slot] = mas->last;
3674 if (mas->last != ULONG_MAX)
3677 mas_set_height(mas);
3679 /* swap the new root into the tree */
3680 rcu_assign_pointer(mas->tree->ma_root, mte_mk_root(mas->node));
3681 ma_set_meta(node, maple_leaf_64, 0, slot);
3685 static inline void mas_store_root(struct ma_state *mas, void *entry)
3687 if (likely((mas->last != 0) || (mas->index != 0)))
3688 mas_root_expand(mas, entry);
3689 else if (((unsigned long) (entry) & 3) == 2)
3690 mas_root_expand(mas, entry);
3692 rcu_assign_pointer(mas->tree->ma_root, entry);
3693 mas->node = MAS_START;
3698 * mas_is_span_wr() - Check if the write needs to be treated as a write that
3700 * @mas: The maple state
3701 * @piv: The pivot value being written
3702 * @type: The maple node type
3703 * @entry: The data to write
3705 * Spanning writes are writes that start in one node and end in another OR if
3706 * the write of a %NULL will cause the node to end with a %NULL.
3708 * Return: True if this is a spanning write, false otherwise.
3710 static bool mas_is_span_wr(struct ma_wr_state *wr_mas)
3713 unsigned long last = wr_mas->mas->last;
3714 unsigned long piv = wr_mas->r_max;
3715 enum maple_type type = wr_mas->type;
3716 void *entry = wr_mas->entry;
3718 /* Contained in this pivot */
3722 max = wr_mas->mas->max;
3723 if (unlikely(ma_is_leaf(type))) {
3724 /* Fits in the node, but may span slots. */
3728 /* Writes to the end of the node but not null. */
3729 if ((last == max) && entry)
3733 * Writing ULONG_MAX is not a spanning write regardless of the
3734 * value being written as long as the range fits in the node.
3736 if ((last == ULONG_MAX) && (last == max))
3738 } else if (piv == last) {
3742 /* Detect spanning store wr walk */
3743 if (last == ULONG_MAX)
3747 trace_ma_write(__func__, wr_mas->mas, piv, entry);
3752 static inline void mas_wr_walk_descend(struct ma_wr_state *wr_mas)
3754 wr_mas->type = mte_node_type(wr_mas->mas->node);
3755 mas_wr_node_walk(wr_mas);
3756 wr_mas->slots = ma_slots(wr_mas->node, wr_mas->type);
3759 static inline void mas_wr_walk_traverse(struct ma_wr_state *wr_mas)
3761 wr_mas->mas->max = wr_mas->r_max;
3762 wr_mas->mas->min = wr_mas->r_min;
3763 wr_mas->mas->node = wr_mas->content;
3764 wr_mas->mas->offset = 0;
3765 wr_mas->mas->depth++;
3768 * mas_wr_walk() - Walk the tree for a write.
3769 * @wr_mas: The maple write state
3771 * Uses mas_slot_locked() and does not need to worry about dead nodes.
3773 * Return: True if it's contained in a node, false on spanning write.
3775 static bool mas_wr_walk(struct ma_wr_state *wr_mas)
3777 struct ma_state *mas = wr_mas->mas;
3780 mas_wr_walk_descend(wr_mas);
3781 if (unlikely(mas_is_span_wr(wr_mas)))
3784 wr_mas->content = mas_slot_locked(mas, wr_mas->slots,
3786 if (ma_is_leaf(wr_mas->type))
3789 mas_wr_walk_traverse(wr_mas);
3795 static bool mas_wr_walk_index(struct ma_wr_state *wr_mas)
3797 struct ma_state *mas = wr_mas->mas;
3800 mas_wr_walk_descend(wr_mas);
3801 wr_mas->content = mas_slot_locked(mas, wr_mas->slots,
3803 if (ma_is_leaf(wr_mas->type))
3805 mas_wr_walk_traverse(wr_mas);
3811 * mas_extend_spanning_null() - Extend a store of a %NULL to include surrounding %NULLs.
3812 * @l_wr_mas: The left maple write state
3813 * @r_wr_mas: The right maple write state
3815 static inline void mas_extend_spanning_null(struct ma_wr_state *l_wr_mas,
3816 struct ma_wr_state *r_wr_mas)
3818 struct ma_state *r_mas = r_wr_mas->mas;
3819 struct ma_state *l_mas = l_wr_mas->mas;
3820 unsigned char l_slot;
3822 l_slot = l_mas->offset;
3823 if (!l_wr_mas->content)
3824 l_mas->index = l_wr_mas->r_min;
3826 if ((l_mas->index == l_wr_mas->r_min) &&
3828 !mas_slot_locked(l_mas, l_wr_mas->slots, l_slot - 1))) {
3830 l_mas->index = l_wr_mas->pivots[l_slot - 2] + 1;
3832 l_mas->index = l_mas->min;
3834 l_mas->offset = l_slot - 1;
3837 if (!r_wr_mas->content) {
3838 if (r_mas->last < r_wr_mas->r_max)
3839 r_mas->last = r_wr_mas->r_max;
3841 } else if ((r_mas->last == r_wr_mas->r_max) &&
3842 (r_mas->last < r_mas->max) &&
3843 !mas_slot_locked(r_mas, r_wr_mas->slots, r_mas->offset + 1)) {
3844 r_mas->last = mas_safe_pivot(r_mas, r_wr_mas->pivots,
3845 r_wr_mas->type, r_mas->offset + 1);
3850 static inline void *mas_state_walk(struct ma_state *mas)
3854 entry = mas_start(mas);
3855 if (mas_is_none(mas))
3858 if (mas_is_ptr(mas))
3861 return mtree_range_walk(mas);
3865 * mtree_lookup_walk() - Internal quick lookup that does not keep maple state up
3868 * @mas: The maple state.
3870 * Note: Leaves mas in undesirable state.
3871 * Return: The entry for @mas->index or %NULL on dead node.
3873 static inline void *mtree_lookup_walk(struct ma_state *mas)
3875 unsigned long *pivots;
3876 unsigned char offset;
3877 struct maple_node *node;
3878 struct maple_enode *next;
3879 enum maple_type type;
3888 node = mte_to_node(next);
3889 type = mte_node_type(next);
3890 pivots = ma_pivots(node, type);
3891 end = ma_data_end(node, type, pivots, max);
3892 if (unlikely(ma_dead_node(node)))
3895 if (pivots[offset] >= mas->index)
3900 } while ((offset < end) && (pivots[offset] < mas->index));
3902 if (likely(offset > end))
3903 max = pivots[offset];
3906 slots = ma_slots(node, type);
3907 next = mt_slot(mas->tree, slots, offset);
3908 if (unlikely(ma_dead_node(node)))
3910 } while (!ma_is_leaf(type));
3912 return (void *)next;
3920 * mas_new_root() - Create a new root node that only contains the entry passed
3922 * @mas: The maple state
3923 * @entry: The entry to store.
3925 * Only valid when the index == 0 and the last == ULONG_MAX
3927 * Return 0 on error, 1 on success.
3929 static inline int mas_new_root(struct ma_state *mas, void *entry)
3931 struct maple_enode *root = mas_root_locked(mas);
3932 enum maple_type type = maple_leaf_64;
3933 struct maple_node *node;
3935 unsigned long *pivots;
3937 if (!entry && !mas->index && mas->last == ULONG_MAX) {
3939 mas_set_height(mas);
3940 rcu_assign_pointer(mas->tree->ma_root, entry);
3941 mas->node = MAS_START;
3945 mas_node_count(mas, 1);
3946 if (mas_is_err(mas))
3949 node = mas_pop_node(mas);
3950 pivots = ma_pivots(node, type);
3951 slots = ma_slots(node, type);
3952 node->parent = ma_parent_ptr(
3953 ((unsigned long)mas->tree | MA_ROOT_PARENT));
3954 mas->node = mt_mk_node(node, type);
3955 rcu_assign_pointer(slots[0], entry);
3956 pivots[0] = mas->last;
3958 mas_set_height(mas);
3959 rcu_assign_pointer(mas->tree->ma_root, mte_mk_root(mas->node));
3962 if (xa_is_node(root))
3963 mte_destroy_walk(root, mas->tree);
3968 * mas_wr_spanning_store() - Create a subtree with the store operation completed
3969 * and new nodes where necessary, then place the sub-tree in the actual tree.
3970 * Note that mas is expected to point to the node which caused the store to
3972 * @wr_mas: The maple write state
3974 * Return: 0 on error, positive on success.
3976 static inline int mas_wr_spanning_store(struct ma_wr_state *wr_mas)
3978 struct maple_subtree_state mast;
3979 struct maple_big_node b_node;
3980 struct ma_state *mas;
3981 unsigned char height;
3983 /* Left and Right side of spanning store */
3984 MA_STATE(l_mas, NULL, 0, 0);
3985 MA_STATE(r_mas, NULL, 0, 0);
3987 MA_WR_STATE(r_wr_mas, &r_mas, wr_mas->entry);
3988 MA_WR_STATE(l_wr_mas, &l_mas, wr_mas->entry);
3991 * A store operation that spans multiple nodes is called a spanning
3992 * store and is handled early in the store call stack by the function
3993 * mas_is_span_wr(). When a spanning store is identified, the maple
3994 * state is duplicated. The first maple state walks the left tree path
3995 * to ``index``, the duplicate walks the right tree path to ``last``.
3996 * The data in the two nodes are combined into a single node, two nodes,
3997 * or possibly three nodes (see the 3-way split above). A ``NULL``
3998 * written to the last entry of a node is considered a spanning store as
3999 * a rebalance is required for the operation to complete and an overflow
4000 * of data may happen.
4003 trace_ma_op(__func__, mas);
4005 if (unlikely(!mas->index && mas->last == ULONG_MAX))
4006 return mas_new_root(mas, wr_mas->entry);
4008 * Node rebalancing may occur due to this store, so there may be three new
4009 * entries per level plus a new root.
4011 height = mas_mt_height(mas);
4012 mas_node_count(mas, 1 + height * 3);
4013 if (mas_is_err(mas))
4017 * Set up right side. Need to get to the next offset after the spanning
4018 * store to ensure it's not NULL and to combine both the next node and
4019 * the node with the start together.
4022 /* Avoid overflow, walk to next slot in the tree. */
4026 r_mas.index = r_mas.last;
4027 mas_wr_walk_index(&r_wr_mas);
4028 r_mas.last = r_mas.index = mas->last;
4030 /* Set up left side. */
4032 mas_wr_walk_index(&l_wr_mas);
4034 if (!wr_mas->entry) {
4035 mas_extend_spanning_null(&l_wr_mas, &r_wr_mas);
4036 mas->offset = l_mas.offset;
4037 mas->index = l_mas.index;
4038 mas->last = l_mas.last = r_mas.last;
4041 /* expanding NULLs may make this cover the entire range */
4042 if (!l_mas.index && r_mas.last == ULONG_MAX) {
4043 mas_set_range(mas, 0, ULONG_MAX);
4044 return mas_new_root(mas, wr_mas->entry);
4047 memset(&b_node, 0, sizeof(struct maple_big_node));
4048 /* Copy l_mas and store the value in b_node. */
4049 mas_store_b_node(&l_wr_mas, &b_node, l_wr_mas.node_end);
4050 /* Copy r_mas into b_node. */
4051 if (r_mas.offset <= r_wr_mas.node_end)
4052 mas_mab_cp(&r_mas, r_mas.offset, r_wr_mas.node_end,
4053 &b_node, b_node.b_end + 1);
4057 /* Stop spanning searches by searching for just index. */
4058 l_mas.index = l_mas.last = mas->index;
4061 mast.orig_l = &l_mas;
4062 mast.orig_r = &r_mas;
4063 /* Combine l_mas and r_mas and split them up evenly again. */
4064 return mas_spanning_rebalance(mas, &mast, height + 1);
4068 * mas_wr_node_store() - Attempt to store the value in a node
4069 * @wr_mas: The maple write state
4071 * Attempts to reuse the node, but may allocate.
4073 * Return: True if stored, false otherwise
4075 static inline bool mas_wr_node_store(struct ma_wr_state *wr_mas)
4077 struct ma_state *mas = wr_mas->mas;
4078 void __rcu **dst_slots;
4079 unsigned long *dst_pivots;
4080 unsigned char dst_offset;
4081 unsigned char new_end = wr_mas->node_end;
4082 unsigned char offset;
4083 unsigned char node_slots = mt_slots[wr_mas->type];
4084 struct maple_node reuse, *newnode;
4085 unsigned char copy_size, max_piv = mt_pivots[wr_mas->type];
4086 bool in_rcu = mt_in_rcu(mas->tree);
4088 offset = mas->offset;
4089 if (mas->last == wr_mas->r_max) {
4090 /* runs right to the end of the node */
4091 if (mas->last == mas->max)
4093 /* don't copy this offset */
4094 wr_mas->offset_end++;
4095 } else if (mas->last < wr_mas->r_max) {
4096 /* new range ends in this range */
4097 if (unlikely(wr_mas->r_max == ULONG_MAX))
4098 mas_bulk_rebalance(mas, wr_mas->node_end, wr_mas->type);
4102 if (wr_mas->end_piv == mas->last)
4103 wr_mas->offset_end++;
4105 new_end -= wr_mas->offset_end - offset - 1;
4108 /* new range starts within a range */
4109 if (wr_mas->r_min < mas->index)
4112 /* Not enough room */
4113 if (new_end >= node_slots)
4116 /* Not enough data. */
4117 if (!mte_is_root(mas->node) && (new_end <= mt_min_slots[wr_mas->type]) &&
4118 !(mas->mas_flags & MA_STATE_BULK))
4123 mas_node_count(mas, 1);
4124 if (mas_is_err(mas))
4127 newnode = mas_pop_node(mas);
4129 memset(&reuse, 0, sizeof(struct maple_node));
4133 newnode->parent = mas_mn(mas)->parent;
4134 dst_pivots = ma_pivots(newnode, wr_mas->type);
4135 dst_slots = ma_slots(newnode, wr_mas->type);
4136 /* Copy from start to insert point */
4137 memcpy(dst_pivots, wr_mas->pivots, sizeof(unsigned long) * (offset + 1));
4138 memcpy(dst_slots, wr_mas->slots, sizeof(void *) * (offset + 1));
4139 dst_offset = offset;
4141 /* Handle insert of new range starting after old range */
4142 if (wr_mas->r_min < mas->index) {
4144 rcu_assign_pointer(dst_slots[dst_offset], wr_mas->content);
4145 dst_pivots[dst_offset++] = mas->index - 1;
4148 /* Store the new entry and range end. */
4149 if (dst_offset < max_piv)
4150 dst_pivots[dst_offset] = mas->last;
4151 mas->offset = dst_offset;
4152 rcu_assign_pointer(dst_slots[dst_offset], wr_mas->entry);
4155 * this range wrote to the end of the node or it overwrote the rest of
4158 if (wr_mas->offset_end > wr_mas->node_end || mas->last >= mas->max) {
4159 new_end = dst_offset;
4164 /* Copy to the end of node if necessary. */
4165 copy_size = wr_mas->node_end - wr_mas->offset_end + 1;
4166 memcpy(dst_slots + dst_offset, wr_mas->slots + wr_mas->offset_end,
4167 sizeof(void *) * copy_size);
4168 if (dst_offset < max_piv) {
4169 if (copy_size > max_piv - dst_offset)
4170 copy_size = max_piv - dst_offset;
4172 memcpy(dst_pivots + dst_offset,
4173 wr_mas->pivots + wr_mas->offset_end,
4174 sizeof(unsigned long) * copy_size);
4177 if ((wr_mas->node_end == node_slots - 1) && (new_end < node_slots - 1))
4178 dst_pivots[new_end] = mas->max;
4181 mas_leaf_set_meta(mas, newnode, dst_pivots, maple_leaf_64, new_end);
4183 mas->node = mt_mk_node(newnode, wr_mas->type);
4184 mas_replace(mas, false);
4186 memcpy(wr_mas->node, newnode, sizeof(struct maple_node));
4188 trace_ma_write(__func__, mas, 0, wr_mas->entry);
4189 mas_update_gap(mas);
4194 * mas_wr_slot_store: Attempt to store a value in a slot.
4195 * @wr_mas: the maple write state
4197 * Return: True if stored, false otherwise
4199 static inline bool mas_wr_slot_store(struct ma_wr_state *wr_mas)
4201 struct ma_state *mas = wr_mas->mas;
4202 unsigned long lmax; /* Logical max. */
4203 unsigned char offset = mas->offset;
4205 if ((wr_mas->r_max > mas->last) && ((wr_mas->r_min != mas->index) ||
4206 (offset != wr_mas->node_end)))
4209 if (offset == wr_mas->node_end - 1)
4212 lmax = wr_mas->pivots[offset + 1];
4214 /* going to overwrite too many slots. */
4215 if (lmax < mas->last)
4218 if (wr_mas->r_min == mas->index) {
4219 /* overwriting two or more ranges with one. */
4220 if (lmax == mas->last)
4223 /* Overwriting all of offset and a portion of offset + 1. */
4224 rcu_assign_pointer(wr_mas->slots[offset], wr_mas->entry);
4225 wr_mas->pivots[offset] = mas->last;
4229 /* Doesn't end on the next range end. */
4230 if (lmax != mas->last)
4233 /* Overwriting a portion of offset and all of offset + 1 */
4234 if ((offset + 1 < mt_pivots[wr_mas->type]) &&
4235 (wr_mas->entry || wr_mas->pivots[offset + 1]))
4236 wr_mas->pivots[offset + 1] = mas->last;
4238 rcu_assign_pointer(wr_mas->slots[offset + 1], wr_mas->entry);
4239 wr_mas->pivots[offset] = mas->index - 1;
4240 mas->offset++; /* Keep mas accurate. */
4243 trace_ma_write(__func__, mas, 0, wr_mas->entry);
4244 mas_update_gap(mas);
4248 static inline void mas_wr_end_piv(struct ma_wr_state *wr_mas)
4250 while ((wr_mas->mas->last > wr_mas->end_piv) &&
4251 (wr_mas->offset_end < wr_mas->node_end))
4252 wr_mas->end_piv = wr_mas->pivots[++wr_mas->offset_end];
4254 if (wr_mas->mas->last > wr_mas->end_piv)
4255 wr_mas->end_piv = wr_mas->mas->max;
4258 static inline void mas_wr_extend_null(struct ma_wr_state *wr_mas)
4260 struct ma_state *mas = wr_mas->mas;
4262 if (mas->last < wr_mas->end_piv && !wr_mas->slots[wr_mas->offset_end])
4263 mas->last = wr_mas->end_piv;
4265 /* Check next slot(s) if we are overwriting the end */
4266 if ((mas->last == wr_mas->end_piv) &&
4267 (wr_mas->node_end != wr_mas->offset_end) &&
4268 !wr_mas->slots[wr_mas->offset_end + 1]) {
4269 wr_mas->offset_end++;
4270 if (wr_mas->offset_end == wr_mas->node_end)
4271 mas->last = mas->max;
4273 mas->last = wr_mas->pivots[wr_mas->offset_end];
4274 wr_mas->end_piv = mas->last;
4277 if (!wr_mas->content) {
4278 /* If this one is null, the next and prev are not */
4279 mas->index = wr_mas->r_min;
4281 /* Check prev slot if we are overwriting the start */
4282 if (mas->index == wr_mas->r_min && mas->offset &&
4283 !wr_mas->slots[mas->offset - 1]) {
4285 wr_mas->r_min = mas->index =
4286 mas_safe_min(mas, wr_mas->pivots, mas->offset);
4287 wr_mas->r_max = wr_mas->pivots[mas->offset];
4292 static inline bool mas_wr_append(struct ma_wr_state *wr_mas)
4294 unsigned char end = wr_mas->node_end;
4295 unsigned char new_end = end + 1;
4296 struct ma_state *mas = wr_mas->mas;
4297 unsigned char node_pivots = mt_pivots[wr_mas->type];
4299 if ((mas->index != wr_mas->r_min) && (mas->last == wr_mas->r_max)) {
4300 if (new_end < node_pivots)
4301 wr_mas->pivots[new_end] = wr_mas->pivots[end];
4303 if (new_end < node_pivots)
4304 ma_set_meta(wr_mas->node, maple_leaf_64, 0, new_end);
4306 rcu_assign_pointer(wr_mas->slots[new_end], wr_mas->entry);
4307 mas->offset = new_end;
4308 wr_mas->pivots[end] = mas->index - 1;
4313 if ((mas->index == wr_mas->r_min) && (mas->last < wr_mas->r_max)) {
4314 if (new_end < node_pivots)
4315 wr_mas->pivots[new_end] = wr_mas->pivots[end];
4317 rcu_assign_pointer(wr_mas->slots[new_end], wr_mas->content);
4318 if (new_end < node_pivots)
4319 ma_set_meta(wr_mas->node, maple_leaf_64, 0, new_end);
4321 wr_mas->pivots[end] = mas->last;
4322 rcu_assign_pointer(wr_mas->slots[end], wr_mas->entry);
4330 * mas_wr_bnode() - Slow path for a modification.
4331 * @wr_mas: The write maple state
4333 * This is where split, rebalance end up.
4335 static void mas_wr_bnode(struct ma_wr_state *wr_mas)
4337 struct maple_big_node b_node;
4339 trace_ma_write(__func__, wr_mas->mas, 0, wr_mas->entry);
4340 memset(&b_node, 0, sizeof(struct maple_big_node));
4341 mas_store_b_node(wr_mas, &b_node, wr_mas->offset_end);
4342 mas_commit_b_node(wr_mas, &b_node, wr_mas->node_end);
4345 static inline void mas_wr_modify(struct ma_wr_state *wr_mas)
4347 unsigned char node_slots;
4348 unsigned char node_size;
4349 struct ma_state *mas = wr_mas->mas;
4351 /* Direct replacement */
4352 if (wr_mas->r_min == mas->index && wr_mas->r_max == mas->last) {
4353 rcu_assign_pointer(wr_mas->slots[mas->offset], wr_mas->entry);
4354 if (!!wr_mas->entry ^ !!wr_mas->content)
4355 mas_update_gap(mas);
4359 /* Attempt to append */
4360 node_slots = mt_slots[wr_mas->type];
4361 node_size = wr_mas->node_end - wr_mas->offset_end + mas->offset + 2;
4362 if (mas->max == ULONG_MAX)
4365 /* slot and node store will not fit, go to the slow path */
4366 if (unlikely(node_size >= node_slots))
4369 if (wr_mas->entry && (wr_mas->node_end < node_slots - 1) &&
4370 (mas->offset == wr_mas->node_end) && mas_wr_append(wr_mas)) {
4371 if (!wr_mas->content || !wr_mas->entry)
4372 mas_update_gap(mas);
4376 if ((wr_mas->offset_end - mas->offset <= 1) && mas_wr_slot_store(wr_mas))
4378 else if (mas_wr_node_store(wr_mas))
4381 if (mas_is_err(mas))
4385 mas_wr_bnode(wr_mas);
4389 * mas_wr_store_entry() - Internal call to store a value
4390 * @mas: The maple state
4391 * @entry: The entry to store.
4393 * Return: The contents that was stored at the index.
4395 static inline void *mas_wr_store_entry(struct ma_wr_state *wr_mas)
4397 struct ma_state *mas = wr_mas->mas;
4399 wr_mas->content = mas_start(mas);
4400 if (mas_is_none(mas) || mas_is_ptr(mas)) {
4401 mas_store_root(mas, wr_mas->entry);
4402 return wr_mas->content;
4405 if (unlikely(!mas_wr_walk(wr_mas))) {
4406 mas_wr_spanning_store(wr_mas);
4407 return wr_mas->content;
4410 /* At this point, we are at the leaf node that needs to be altered. */
4411 wr_mas->end_piv = wr_mas->r_max;
4412 mas_wr_end_piv(wr_mas);
4415 mas_wr_extend_null(wr_mas);
4417 /* New root for a single pointer */
4418 if (unlikely(!mas->index && mas->last == ULONG_MAX)) {
4419 mas_new_root(mas, wr_mas->entry);
4420 return wr_mas->content;
4423 mas_wr_modify(wr_mas);
4424 return wr_mas->content;
4428 * mas_insert() - Internal call to insert a value
4429 * @mas: The maple state
4430 * @entry: The entry to store
4432 * Return: %NULL or the contents that already exists at the requested index
4433 * otherwise. The maple state needs to be checked for error conditions.
4435 static inline void *mas_insert(struct ma_state *mas, void *entry)
4437 MA_WR_STATE(wr_mas, mas, entry);
4440 * Inserting a new range inserts either 0, 1, or 2 pivots within the
4441 * tree. If the insert fits exactly into an existing gap with a value
4442 * of NULL, then the slot only needs to be written with the new value.
4443 * If the range being inserted is adjacent to another range, then only a
4444 * single pivot needs to be inserted (as well as writing the entry). If
4445 * the new range is within a gap but does not touch any other ranges,
4446 * then two pivots need to be inserted: the start - 1, and the end. As
4447 * usual, the entry must be written. Most operations require a new node
4448 * to be allocated and replace an existing node to ensure RCU safety,
4449 * when in RCU mode. The exception to requiring a newly allocated node
4450 * is when inserting at the end of a node (appending). When done
4451 * carefully, appending can reuse the node in place.
4453 wr_mas.content = mas_start(mas);
4457 if (mas_is_none(mas) || mas_is_ptr(mas)) {
4458 mas_store_root(mas, entry);
4462 /* spanning writes always overwrite something */
4463 if (!mas_wr_walk(&wr_mas))
4466 /* At this point, we are at the leaf node that needs to be altered. */
4467 wr_mas.offset_end = mas->offset;
4468 wr_mas.end_piv = wr_mas.r_max;
4470 if (wr_mas.content || (mas->last > wr_mas.r_max))
4476 mas_wr_modify(&wr_mas);
4477 return wr_mas.content;
4480 mas_set_err(mas, -EEXIST);
4481 return wr_mas.content;
4486 * mas_prev_node() - Find the prev non-null entry at the same level in the
4487 * tree. The prev value will be mas->node[mas->offset] or MAS_NONE.
4488 * @mas: The maple state
4489 * @min: The lower limit to search
4491 * The prev node value will be mas->node[mas->offset] or MAS_NONE.
4492 * Return: 1 if the node is dead, 0 otherwise.
4494 static inline int mas_prev_node(struct ma_state *mas, unsigned long min)
4499 struct maple_node *node;
4500 struct maple_enode *enode;
4501 unsigned long *pivots;
4503 if (mas_is_none(mas))
4509 if (ma_is_root(node))
4513 if (unlikely(mas_ascend(mas)))
4515 offset = mas->offset;
4520 mt = mte_node_type(mas->node);
4522 slots = ma_slots(node, mt);
4523 pivots = ma_pivots(node, mt);
4524 if (unlikely(ma_dead_node(node)))
4527 mas->max = pivots[offset];
4529 mas->min = pivots[offset - 1] + 1;
4530 if (unlikely(ma_dead_node(node)))
4538 enode = mas_slot(mas, slots, offset);
4539 if (unlikely(ma_dead_node(node)))
4543 mt = mte_node_type(mas->node);
4545 slots = ma_slots(node, mt);
4546 pivots = ma_pivots(node, mt);
4547 offset = ma_data_end(node, mt, pivots, mas->max);
4548 if (unlikely(ma_dead_node(node)))
4552 mas->min = pivots[offset - 1] + 1;
4554 if (offset < mt_pivots[mt])
4555 mas->max = pivots[offset];
4561 mas->node = mas_slot(mas, slots, offset);
4562 if (unlikely(ma_dead_node(node)))
4565 mas->offset = mas_data_end(mas);
4566 if (unlikely(mte_dead_node(mas->node)))
4572 mas->offset = offset;
4574 mas->min = pivots[offset - 1] + 1;
4576 if (unlikely(ma_dead_node(node)))
4579 mas->node = MAS_NONE;
4584 * mas_next_node() - Get the next node at the same level in the tree.
4585 * @mas: The maple state
4586 * @max: The maximum pivot value to check.
4588 * The next value will be mas->node[mas->offset] or MAS_NONE.
4589 * Return: 1 on dead node, 0 otherwise.
4591 static inline int mas_next_node(struct ma_state *mas, struct maple_node *node,
4594 unsigned long min, pivot;
4595 unsigned long *pivots;
4596 struct maple_enode *enode;
4598 unsigned char offset;
4599 unsigned char node_end;
4603 if (mas->max >= max)
4608 if (ma_is_root(node))
4615 if (unlikely(mas_ascend(mas)))
4618 offset = mas->offset;
4621 mt = mte_node_type(mas->node);
4622 pivots = ma_pivots(node, mt);
4623 node_end = ma_data_end(node, mt, pivots, mas->max);
4624 if (unlikely(ma_dead_node(node)))
4627 } while (unlikely(offset == node_end));
4629 slots = ma_slots(node, mt);
4630 pivot = mas_safe_pivot(mas, pivots, ++offset, mt);
4631 while (unlikely(level > 1)) {
4632 /* Descend, if necessary */
4633 enode = mas_slot(mas, slots, offset);
4634 if (unlikely(ma_dead_node(node)))
4640 mt = mte_node_type(mas->node);
4641 slots = ma_slots(node, mt);
4642 pivots = ma_pivots(node, mt);
4643 if (unlikely(ma_dead_node(node)))
4650 enode = mas_slot(mas, slots, offset);
4651 if (unlikely(ma_dead_node(node)))
4660 if (unlikely(ma_dead_node(node)))
4663 mas->node = MAS_NONE;
4668 * mas_next_nentry() - Get the next node entry
4669 * @mas: The maple state
4670 * @max: The maximum value to check
4671 * @*range_start: Pointer to store the start of the range.
4673 * Sets @mas->offset to the offset of the next node entry, @mas->last to the
4674 * pivot of the entry.
4676 * Return: The next entry, %NULL otherwise
4678 static inline void *mas_next_nentry(struct ma_state *mas,
4679 struct maple_node *node, unsigned long max, enum maple_type type)
4681 unsigned char count;
4682 unsigned long pivot;
4683 unsigned long *pivots;
4687 if (mas->last == mas->max) {
4688 mas->index = mas->max;
4692 slots = ma_slots(node, type);
4693 pivots = ma_pivots(node, type);
4694 count = ma_data_end(node, type, pivots, mas->max);
4695 if (unlikely(ma_dead_node(node)))
4698 mas->index = mas_safe_min(mas, pivots, mas->offset);
4699 if (unlikely(ma_dead_node(node)))
4702 if (mas->index > max)
4705 if (mas->offset > count)
4708 while (mas->offset < count) {
4709 pivot = pivots[mas->offset];
4710 entry = mas_slot(mas, slots, mas->offset);
4711 if (ma_dead_node(node))
4720 mas->index = pivot + 1;
4724 if (mas->index > mas->max) {
4725 mas->index = mas->last;
4729 pivot = mas_safe_pivot(mas, pivots, mas->offset, type);
4730 entry = mas_slot(mas, slots, mas->offset);
4731 if (ma_dead_node(node))
4745 static inline void mas_rewalk(struct ma_state *mas, unsigned long index)
4748 mas_set(mas, index);
4749 mas_state_walk(mas);
4750 if (mas_is_start(mas))
4755 * mas_next_entry() - Internal function to get the next entry.
4756 * @mas: The maple state
4757 * @limit: The maximum range start.
4759 * Set the @mas->node to the next entry and the range_start to
4760 * the beginning value for the entry. Does not check beyond @limit.
4761 * Sets @mas->index and @mas->last to the limit if it is hit.
4762 * Restarts on dead nodes.
4764 * Return: the next entry or %NULL.
4766 static inline void *mas_next_entry(struct ma_state *mas, unsigned long limit)
4769 struct maple_enode *prev_node;
4770 struct maple_node *node;
4771 unsigned char offset;
4775 if (mas->index > limit) {
4776 mas->index = mas->last = limit;
4782 offset = mas->offset;
4783 prev_node = mas->node;
4785 mt = mte_node_type(mas->node);
4787 if (unlikely(mas->offset >= mt_slots[mt])) {
4788 mas->offset = mt_slots[mt] - 1;
4792 while (!mas_is_none(mas)) {
4793 entry = mas_next_nentry(mas, node, limit, mt);
4794 if (unlikely(ma_dead_node(node))) {
4795 mas_rewalk(mas, last);
4802 if (unlikely((mas->index > limit)))
4806 prev_node = mas->node;
4807 offset = mas->offset;
4808 if (unlikely(mas_next_node(mas, node, limit))) {
4809 mas_rewalk(mas, last);
4814 mt = mte_node_type(mas->node);
4817 mas->index = mas->last = limit;
4818 mas->offset = offset;
4819 mas->node = prev_node;
4824 * mas_prev_nentry() - Get the previous node entry.
4825 * @mas: The maple state.
4826 * @limit: The lower limit to check for a value.
4828 * Return: the entry, %NULL otherwise.
4830 static inline void *mas_prev_nentry(struct ma_state *mas, unsigned long limit,
4831 unsigned long index)
4833 unsigned long pivot, min;
4834 unsigned char offset;
4835 struct maple_node *mn;
4837 unsigned long *pivots;
4846 mt = mte_node_type(mas->node);
4847 offset = mas->offset - 1;
4848 if (offset >= mt_slots[mt])
4849 offset = mt_slots[mt] - 1;
4851 slots = ma_slots(mn, mt);
4852 pivots = ma_pivots(mn, mt);
4853 if (unlikely(ma_dead_node(mn))) {
4854 mas_rewalk(mas, index);
4858 if (offset == mt_pivots[mt])
4861 pivot = pivots[offset];
4863 if (unlikely(ma_dead_node(mn))) {
4864 mas_rewalk(mas, index);
4868 while (offset && ((!mas_slot(mas, slots, offset) && pivot >= limit) ||
4870 pivot = pivots[--offset];
4872 min = mas_safe_min(mas, pivots, offset);
4873 entry = mas_slot(mas, slots, offset);
4874 if (unlikely(ma_dead_node(mn))) {
4875 mas_rewalk(mas, index);
4879 if (likely(entry)) {
4880 mas->offset = offset;
4887 static inline void *mas_prev_entry(struct ma_state *mas, unsigned long min)
4891 if (mas->index < min) {
4892 mas->index = mas->last = min;
4893 mas->node = MAS_NONE;
4897 while (likely(!mas_is_none(mas))) {
4898 entry = mas_prev_nentry(mas, min, mas->index);
4899 if (unlikely(mas->last < min))
4905 if (unlikely(mas_prev_node(mas, min))) {
4906 mas_rewalk(mas, mas->index);
4915 mas->index = mas->last = min;
4920 * mas_rev_awalk() - Internal function. Reverse allocation walk. Find the
4921 * highest gap address of a given size in a given node and descend.
4922 * @mas: The maple state
4923 * @size: The needed size.
4925 * Return: True if found in a leaf, false otherwise.
4928 static bool mas_rev_awalk(struct ma_state *mas, unsigned long size)
4930 enum maple_type type = mte_node_type(mas->node);
4931 struct maple_node *node = mas_mn(mas);
4932 unsigned long *pivots, *gaps;
4934 unsigned long gap = 0;
4935 unsigned long max, min;
4936 unsigned char offset;
4938 if (unlikely(mas_is_err(mas)))
4941 if (ma_is_dense(type)) {
4943 mas->offset = (unsigned char)(mas->index - mas->min);
4947 pivots = ma_pivots(node, type);
4948 slots = ma_slots(node, type);
4949 gaps = ma_gaps(node, type);
4950 offset = mas->offset;
4951 min = mas_safe_min(mas, pivots, offset);
4952 /* Skip out of bounds. */
4953 while (mas->last < min)
4954 min = mas_safe_min(mas, pivots, --offset);
4956 max = mas_safe_pivot(mas, pivots, offset, type);
4957 while (mas->index <= max) {
4961 else if (!mas_slot(mas, slots, offset))
4962 gap = max - min + 1;
4965 if ((size <= gap) && (size <= mas->last - min + 1))
4969 /* Skip the next slot, it cannot be a gap. */
4974 max = pivots[offset];
4975 min = mas_safe_min(mas, pivots, offset);
4985 min = mas_safe_min(mas, pivots, offset);
4988 if (unlikely((mas->index > max) || (size - 1 > max - mas->index)))
4991 if (unlikely(ma_is_leaf(type))) {
4992 mas->offset = offset;
4994 mas->max = min + gap - 1;
4998 /* descend, only happens under lock. */
4999 mas->node = mas_slot(mas, slots, offset);
5002 mas->offset = mas_data_end(mas);
5006 if (!mte_is_root(mas->node))
5010 mas_set_err(mas, -EBUSY);
5014 static inline bool mas_anode_descend(struct ma_state *mas, unsigned long size)
5016 enum maple_type type = mte_node_type(mas->node);
5017 unsigned long pivot, min, gap = 0;
5018 unsigned char offset;
5019 unsigned long *gaps;
5020 unsigned long *pivots = ma_pivots(mas_mn(mas), type);
5021 void __rcu **slots = ma_slots(mas_mn(mas), type);
5024 if (ma_is_dense(type)) {
5025 mas->offset = (unsigned char)(mas->index - mas->min);
5029 gaps = ma_gaps(mte_to_node(mas->node), type);
5030 offset = mas->offset;
5031 min = mas_safe_min(mas, pivots, offset);
5032 for (; offset < mt_slots[type]; offset++) {
5033 pivot = mas_safe_pivot(mas, pivots, offset, type);
5034 if (offset && !pivot)
5037 /* Not within lower bounds */
5038 if (mas->index > pivot)
5043 else if (!mas_slot(mas, slots, offset))
5044 gap = min(pivot, mas->last) - max(mas->index, min) + 1;
5049 if (ma_is_leaf(type)) {
5053 if (mas->index <= pivot) {
5054 mas->node = mas_slot(mas, slots, offset);
5063 if (mas->last <= pivot) {
5064 mas_set_err(mas, -EBUSY);
5069 if (mte_is_root(mas->node))
5072 mas->offset = offset;
5077 * mas_walk() - Search for @mas->index in the tree.
5078 * @mas: The maple state.
5080 * mas->index and mas->last will be set to the range if there is a value. If
5081 * mas->node is MAS_NONE, reset to MAS_START.
5083 * Return: the entry at the location or %NULL.
5085 void *mas_walk(struct ma_state *mas)
5090 entry = mas_state_walk(mas);
5091 if (mas_is_start(mas))
5094 if (mas_is_ptr(mas)) {
5099 mas->last = ULONG_MAX;
5104 if (mas_is_none(mas)) {
5106 mas->last = ULONG_MAX;
5111 EXPORT_SYMBOL_GPL(mas_walk);
5113 static inline bool mas_rewind_node(struct ma_state *mas)
5118 if (mte_is_root(mas->node)) {
5128 mas->offset = --slot;
5133 * mas_skip_node() - Internal function. Skip over a node.
5134 * @mas: The maple state.
5136 * Return: true if there is another node, false otherwise.
5138 static inline bool mas_skip_node(struct ma_state *mas)
5140 if (mas_is_err(mas))
5144 if (mte_is_root(mas->node)) {
5145 if (mas->offset >= mas_data_end(mas)) {
5146 mas_set_err(mas, -EBUSY);
5152 } while (mas->offset >= mas_data_end(mas));
5159 * mas_awalk() - Allocation walk. Search from low address to high, for a gap of
5161 * @mas: The maple state
5162 * @size: The size of the gap required
5164 * Search between @mas->index and @mas->last for a gap of @size.
5166 static inline void mas_awalk(struct ma_state *mas, unsigned long size)
5168 struct maple_enode *last = NULL;
5171 * There are 4 options:
5172 * go to child (descend)
5173 * go back to parent (ascend)
5174 * no gap found. (return, slot == MAPLE_NODE_SLOTS)
5175 * found the gap. (return, slot != MAPLE_NODE_SLOTS)
5177 while (!mas_is_err(mas) && !mas_anode_descend(mas, size)) {
5178 if (last == mas->node)
5186 * mas_fill_gap() - Fill a located gap with @entry.
5187 * @mas: The maple state
5188 * @entry: The value to store
5189 * @slot: The offset into the node to store the @entry
5190 * @size: The size of the entry
5191 * @index: The start location
5193 static inline void mas_fill_gap(struct ma_state *mas, void *entry,
5194 unsigned char slot, unsigned long size, unsigned long *index)
5196 MA_WR_STATE(wr_mas, mas, entry);
5197 unsigned char pslot = mte_parent_slot(mas->node);
5198 struct maple_enode *mn = mas->node;
5199 unsigned long *pivots;
5200 enum maple_type ptype;
5202 * mas->index is the start address for the search
5203 * which may no longer be needed.
5204 * mas->last is the end address for the search
5207 *index = mas->index;
5208 mas->last = mas->index + size - 1;
5211 * It is possible that using mas->max and mas->min to correctly
5212 * calculate the index and last will cause an issue in the gap
5213 * calculation, so fix the ma_state here
5216 ptype = mte_node_type(mas->node);
5217 pivots = ma_pivots(mas_mn(mas), ptype);
5218 mas->max = mas_safe_pivot(mas, pivots, pslot, ptype);
5219 mas->min = mas_safe_min(mas, pivots, pslot);
5222 mas_wr_store_entry(&wr_mas);
5226 * mas_sparse_area() - Internal function. Return upper or lower limit when
5227 * searching for a gap in an empty tree.
5228 * @mas: The maple state
5229 * @min: the minimum range
5230 * @max: The maximum range
5231 * @size: The size of the gap
5232 * @fwd: Searching forward or back
5234 static inline void mas_sparse_area(struct ma_state *mas, unsigned long min,
5235 unsigned long max, unsigned long size, bool fwd)
5237 unsigned long start = 0;
5239 if (!unlikely(mas_is_none(mas)))
5248 mas->last = start + size - 1;
5256 * mas_empty_area() - Get the lowest address within the range that is
5257 * sufficient for the size requested.
5258 * @mas: The maple state
5259 * @min: The lowest value of the range
5260 * @max: The highest value of the range
5261 * @size: The size needed
5263 int mas_empty_area(struct ma_state *mas, unsigned long min,
5264 unsigned long max, unsigned long size)
5266 unsigned char offset;
5267 unsigned long *pivots;
5270 if (mas_is_start(mas))
5272 else if (mas->offset >= 2)
5274 else if (!mas_skip_node(mas))
5278 if (mas_is_none(mas) || mas_is_ptr(mas)) {
5279 mas_sparse_area(mas, min, max, size, true);
5283 /* The start of the window can only be within these values */
5286 mas_awalk(mas, size);
5288 if (unlikely(mas_is_err(mas)))
5289 return xa_err(mas->node);
5291 offset = mas->offset;
5292 if (unlikely(offset == MAPLE_NODE_SLOTS))
5295 mt = mte_node_type(mas->node);
5296 pivots = ma_pivots(mas_mn(mas), mt);
5298 mas->min = pivots[offset - 1] + 1;
5300 if (offset < mt_pivots[mt])
5301 mas->max = pivots[offset];
5303 if (mas->index < mas->min)
5304 mas->index = mas->min;
5306 mas->last = mas->index + size - 1;
5309 EXPORT_SYMBOL_GPL(mas_empty_area);
5312 * mas_empty_area_rev() - Get the highest address within the range that is
5313 * sufficient for the size requested.
5314 * @mas: The maple state
5315 * @min: The lowest value of the range
5316 * @max: The highest value of the range
5317 * @size: The size needed
5319 int mas_empty_area_rev(struct ma_state *mas, unsigned long min,
5320 unsigned long max, unsigned long size)
5322 struct maple_enode *last = mas->node;
5324 if (mas_is_start(mas)) {
5326 mas->offset = mas_data_end(mas);
5327 } else if (mas->offset >= 2) {
5329 } else if (!mas_rewind_node(mas)) {
5334 if (mas_is_none(mas) || mas_is_ptr(mas)) {
5335 mas_sparse_area(mas, min, max, size, false);
5339 /* The start of the window can only be within these values. */
5343 while (!mas_rev_awalk(mas, size)) {
5344 if (last == mas->node) {
5345 if (!mas_rewind_node(mas))
5352 if (mas_is_err(mas))
5353 return xa_err(mas->node);
5355 if (unlikely(mas->offset == MAPLE_NODE_SLOTS))
5359 * mas_rev_awalk() has set mas->min and mas->max to the gap values. If
5360 * the maximum is outside the window we are searching, then use the last
5361 * location in the search.
5362 * mas->max and mas->min is the range of the gap.
5363 * mas->index and mas->last are currently set to the search range.
5366 /* Trim the upper limit to the max. */
5367 if (mas->max <= mas->last)
5368 mas->last = mas->max;
5370 mas->index = mas->last - size + 1;
5373 EXPORT_SYMBOL_GPL(mas_empty_area_rev);
5375 static inline int mas_alloc(struct ma_state *mas, void *entry,
5376 unsigned long size, unsigned long *index)
5381 if (mas_is_none(mas) || mas_is_ptr(mas)) {
5382 mas_root_expand(mas, entry);
5383 if (mas_is_err(mas))
5384 return xa_err(mas->node);
5387 return mte_pivot(mas->node, 0);
5388 return mte_pivot(mas->node, 1);
5391 /* Must be walking a tree. */
5392 mas_awalk(mas, size);
5393 if (mas_is_err(mas))
5394 return xa_err(mas->node);
5396 if (mas->offset == MAPLE_NODE_SLOTS)
5400 * At this point, mas->node points to the right node and we have an
5401 * offset that has a sufficient gap.
5405 min = mte_pivot(mas->node, mas->offset - 1) + 1;
5407 if (mas->index < min)
5410 mas_fill_gap(mas, entry, mas->offset, size, index);
5417 static inline int mas_rev_alloc(struct ma_state *mas, unsigned long min,
5418 unsigned long max, void *entry,
5419 unsigned long size, unsigned long *index)
5423 ret = mas_empty_area_rev(mas, min, max, size);
5427 if (mas_is_err(mas))
5428 return xa_err(mas->node);
5430 if (mas->offset == MAPLE_NODE_SLOTS)
5433 mas_fill_gap(mas, entry, mas->offset, size, index);
5441 * mas_dead_leaves() - Mark all leaves of a node as dead.
5442 * @mas: The maple state
5443 * @slots: Pointer to the slot array
5445 * Must hold the write lock.
5447 * Return: The number of leaves marked as dead.
5450 unsigned char mas_dead_leaves(struct ma_state *mas, void __rcu **slots)
5452 struct maple_node *node;
5453 enum maple_type type;
5457 for (offset = 0; offset < mt_slot_count(mas->node); offset++) {
5458 entry = mas_slot_locked(mas, slots, offset);
5459 type = mte_node_type(entry);
5460 node = mte_to_node(entry);
5461 /* Use both node and type to catch LE & BE metadata */
5465 mte_set_node_dead(entry);
5466 smp_wmb(); /* Needed for RCU */
5468 rcu_assign_pointer(slots[offset], node);
5474 static void __rcu **mas_dead_walk(struct ma_state *mas, unsigned char offset)
5476 struct maple_node *node, *next;
5477 void __rcu **slots = NULL;
5481 mas->node = ma_enode_ptr(next);
5483 slots = ma_slots(node, node->type);
5484 next = mas_slot_locked(mas, slots, offset);
5486 } while (!ma_is_leaf(next->type));
5491 static void mt_free_walk(struct rcu_head *head)
5494 struct maple_node *node, *start;
5495 struct maple_tree mt;
5496 unsigned char offset;
5497 enum maple_type type;
5498 MA_STATE(mas, &mt, 0, 0);
5500 node = container_of(head, struct maple_node, rcu);
5502 if (ma_is_leaf(node->type))
5505 mt_init_flags(&mt, node->ma_flags);
5508 mas.node = mt_mk_node(node, node->type);
5509 slots = mas_dead_walk(&mas, 0);
5510 node = mas_mn(&mas);
5512 mt_free_bulk(node->slot_len, slots);
5513 offset = node->parent_slot + 1;
5514 mas.node = node->piv_parent;
5515 if (mas_mn(&mas) == node)
5516 goto start_slots_free;
5518 type = mte_node_type(mas.node);
5519 slots = ma_slots(mte_to_node(mas.node), type);
5520 if ((offset < mt_slots[type]) && (slots[offset]))
5521 slots = mas_dead_walk(&mas, offset);
5523 node = mas_mn(&mas);
5524 } while ((node != start) || (node->slot_len < offset));
5526 slots = ma_slots(node, node->type);
5527 mt_free_bulk(node->slot_len, slots);
5532 mt_free_rcu(&node->rcu);
5535 static inline void __rcu **mas_destroy_descend(struct ma_state *mas,
5536 struct maple_enode *prev, unsigned char offset)
5538 struct maple_node *node;
5539 struct maple_enode *next = mas->node;
5540 void __rcu **slots = NULL;
5545 slots = ma_slots(node, mte_node_type(mas->node));
5546 next = mas_slot_locked(mas, slots, 0);
5547 if ((mte_dead_node(next)))
5548 next = mas_slot_locked(mas, slots, 1);
5550 mte_set_node_dead(mas->node);
5551 node->type = mte_node_type(mas->node);
5552 node->piv_parent = prev;
5553 node->parent_slot = offset;
5556 } while (!mte_is_leaf(next));
5561 static void mt_destroy_walk(struct maple_enode *enode, unsigned char ma_flags,
5565 struct maple_node *node = mte_to_node(enode);
5566 struct maple_enode *start;
5567 struct maple_tree mt;
5569 MA_STATE(mas, &mt, 0, 0);
5571 if (mte_is_leaf(enode))
5574 mt_init_flags(&mt, ma_flags);
5577 mas.node = start = enode;
5578 slots = mas_destroy_descend(&mas, start, 0);
5579 node = mas_mn(&mas);
5581 enum maple_type type;
5582 unsigned char offset;
5583 struct maple_enode *parent, *tmp;
5585 node->slot_len = mas_dead_leaves(&mas, slots);
5587 mt_free_bulk(node->slot_len, slots);
5588 offset = node->parent_slot + 1;
5589 mas.node = node->piv_parent;
5590 if (mas_mn(&mas) == node)
5591 goto start_slots_free;
5593 type = mte_node_type(mas.node);
5594 slots = ma_slots(mte_to_node(mas.node), type);
5595 if (offset >= mt_slots[type])
5598 tmp = mas_slot_locked(&mas, slots, offset);
5599 if (mte_node_type(tmp) && mte_to_node(tmp)) {
5602 slots = mas_destroy_descend(&mas, parent, offset);
5605 node = mas_mn(&mas);
5606 } while (start != mas.node);
5608 node = mas_mn(&mas);
5609 node->slot_len = mas_dead_leaves(&mas, slots);
5611 mt_free_bulk(node->slot_len, slots);
5618 mt_free_rcu(&node->rcu);
5622 * mte_destroy_walk() - Free a tree or sub-tree.
5623 * @enode: the encoded maple node (maple_enode) to start
5624 * @mt: the tree to free - needed for node types.
5626 * Must hold the write lock.
5628 static inline void mte_destroy_walk(struct maple_enode *enode,
5629 struct maple_tree *mt)
5631 struct maple_node *node = mte_to_node(enode);
5633 if (mt_in_rcu(mt)) {
5634 mt_destroy_walk(enode, mt->ma_flags, false);
5635 call_rcu(&node->rcu, mt_free_walk);
5637 mt_destroy_walk(enode, mt->ma_flags, true);
5641 static void mas_wr_store_setup(struct ma_wr_state *wr_mas)
5643 if (unlikely(mas_is_paused(wr_mas->mas)))
5644 mas_reset(wr_mas->mas);
5646 if (!mas_is_start(wr_mas->mas)) {
5647 if (mas_is_none(wr_mas->mas)) {
5648 mas_reset(wr_mas->mas);
5650 wr_mas->r_max = wr_mas->mas->max;
5651 wr_mas->type = mte_node_type(wr_mas->mas->node);
5652 if (mas_is_span_wr(wr_mas))
5653 mas_reset(wr_mas->mas);
5661 * mas_store() - Store an @entry.
5662 * @mas: The maple state.
5663 * @entry: The entry to store.
5665 * The @mas->index and @mas->last is used to set the range for the @entry.
5666 * Note: The @mas should have pre-allocated entries to ensure there is memory to
5667 * store the entry. Please see mas_expected_entries()/mas_destroy() for more details.
5669 * Return: the first entry between mas->index and mas->last or %NULL.
5671 void *mas_store(struct ma_state *mas, void *entry)
5673 MA_WR_STATE(wr_mas, mas, entry);
5675 trace_ma_write(__func__, mas, 0, entry);
5676 #ifdef CONFIG_DEBUG_MAPLE_TREE
5677 if (mas->index > mas->last)
5678 pr_err("Error %lu > %lu %p\n", mas->index, mas->last, entry);
5679 MT_BUG_ON(mas->tree, mas->index > mas->last);
5680 if (mas->index > mas->last) {
5681 mas_set_err(mas, -EINVAL);
5688 * Storing is the same operation as insert with the added caveat that it
5689 * can overwrite entries. Although this seems simple enough, one may
5690 * want to examine what happens if a single store operation was to
5691 * overwrite multiple entries within a self-balancing B-Tree.
5693 mas_wr_store_setup(&wr_mas);
5694 mas_wr_store_entry(&wr_mas);
5695 return wr_mas.content;
5697 EXPORT_SYMBOL_GPL(mas_store);
5700 * mas_store_gfp() - Store a value into the tree.
5701 * @mas: The maple state
5702 * @entry: The entry to store
5703 * @gfp: The GFP_FLAGS to use for allocations if necessary.
5705 * Return: 0 on success, -EINVAL on invalid request, -ENOMEM if memory could not
5708 int mas_store_gfp(struct ma_state *mas, void *entry, gfp_t gfp)
5710 MA_WR_STATE(wr_mas, mas, entry);
5712 mas_wr_store_setup(&wr_mas);
5713 trace_ma_write(__func__, mas, 0, entry);
5715 mas_wr_store_entry(&wr_mas);
5716 if (unlikely(mas_nomem(mas, gfp)))
5719 if (unlikely(mas_is_err(mas)))
5720 return xa_err(mas->node);
5724 EXPORT_SYMBOL_GPL(mas_store_gfp);
5727 * mas_store_prealloc() - Store a value into the tree using memory
5728 * preallocated in the maple state.
5729 * @mas: The maple state
5730 * @entry: The entry to store.
5732 void mas_store_prealloc(struct ma_state *mas, void *entry)
5734 MA_WR_STATE(wr_mas, mas, entry);
5736 mas_wr_store_setup(&wr_mas);
5737 trace_ma_write(__func__, mas, 0, entry);
5738 mas_wr_store_entry(&wr_mas);
5739 BUG_ON(mas_is_err(mas));
5742 EXPORT_SYMBOL_GPL(mas_store_prealloc);
5745 * mas_preallocate() - Preallocate enough nodes for a store operation
5746 * @mas: The maple state
5747 * @gfp: The GFP_FLAGS to use for allocations.
5749 * Return: 0 on success, -ENOMEM if memory could not be allocated.
5751 int mas_preallocate(struct ma_state *mas, gfp_t gfp)
5755 mas_node_count_gfp(mas, 1 + mas_mt_height(mas) * 3, gfp);
5756 mas->mas_flags |= MA_STATE_PREALLOC;
5757 if (likely(!mas_is_err(mas)))
5760 mas_set_alloc_req(mas, 0);
5761 ret = xa_err(mas->node);
5769 * mas_destroy() - destroy a maple state.
5770 * @mas: The maple state
5772 * Upon completion, check the left-most node and rebalance against the node to
5773 * the right if necessary. Frees any allocated nodes associated with this maple
5776 void mas_destroy(struct ma_state *mas)
5778 struct maple_alloc *node;
5779 unsigned long total;
5782 * When using mas_for_each() to insert an expected number of elements,
5783 * it is possible that the number inserted is less than the expected
5784 * number. To fix an invalid final node, a check is performed here to
5785 * rebalance the previous node with the final node.
5787 if (mas->mas_flags & MA_STATE_REBALANCE) {
5790 if (mas_is_start(mas))
5793 mtree_range_walk(mas);
5794 end = mas_data_end(mas) + 1;
5795 if (end < mt_min_slot_count(mas->node) - 1)
5796 mas_destroy_rebalance(mas, end);
5798 mas->mas_flags &= ~MA_STATE_REBALANCE;
5800 mas->mas_flags &= ~(MA_STATE_BULK|MA_STATE_PREALLOC);
5802 total = mas_allocated(mas);
5805 mas->alloc = node->slot[0];
5806 if (node->node_count > 1) {
5807 size_t count = node->node_count - 1;
5809 mt_free_bulk(count, (void __rcu **)&node->slot[1]);
5812 kmem_cache_free(maple_node_cache, node);
5818 EXPORT_SYMBOL_GPL(mas_destroy);
5821 * mas_expected_entries() - Set the expected number of entries that will be inserted.
5822 * @mas: The maple state
5823 * @nr_entries: The number of expected entries.
5825 * This will attempt to pre-allocate enough nodes to store the expected number
5826 * of entries. The allocations will occur using the bulk allocator interface
5827 * for speed. Please call mas_destroy() on the @mas after inserting the entries
5828 * to ensure any unused nodes are freed.
5830 * Return: 0 on success, -ENOMEM if memory could not be allocated.
5832 int mas_expected_entries(struct ma_state *mas, unsigned long nr_entries)
5834 int nonleaf_cap = MAPLE_ARANGE64_SLOTS - 2;
5835 struct maple_enode *enode = mas->node;
5840 * Sometimes it is necessary to duplicate a tree to a new tree, such as
5841 * forking a process and duplicating the VMAs from one tree to a new
5842 * tree. When such a situation arises, it is known that the new tree is
5843 * not going to be used until the entire tree is populated. For
5844 * performance reasons, it is best to use a bulk load with RCU disabled.
5845 * This allows for optimistic splitting that favours the left and reuse
5846 * of nodes during the operation.
5849 /* Optimize splitting for bulk insert in-order */
5850 mas->mas_flags |= MA_STATE_BULK;
5853 * Avoid overflow, assume a gap between each entry and a trailing null.
5854 * If this is wrong, it just means allocation can happen during
5855 * insertion of entries.
5857 nr_nodes = max(nr_entries, nr_entries * 2 + 1);
5858 if (!mt_is_alloc(mas->tree))
5859 nonleaf_cap = MAPLE_RANGE64_SLOTS - 2;
5861 /* Leaves; reduce slots to keep space for expansion */
5862 nr_nodes = DIV_ROUND_UP(nr_nodes, MAPLE_RANGE64_SLOTS - 2);
5863 /* Internal nodes */
5864 nr_nodes += DIV_ROUND_UP(nr_nodes, nonleaf_cap);
5865 /* Add working room for split (2 nodes) + new parents */
5866 mas_node_count(mas, nr_nodes + 3);
5868 /* Detect if allocations run out */
5869 mas->mas_flags |= MA_STATE_PREALLOC;
5871 if (!mas_is_err(mas))
5874 ret = xa_err(mas->node);
5880 EXPORT_SYMBOL_GPL(mas_expected_entries);
5883 * mas_next() - Get the next entry.
5884 * @mas: The maple state
5885 * @max: The maximum index to check.
5887 * Returns the next entry after @mas->index.
5888 * Must hold rcu_read_lock or the write lock.
5889 * Can return the zero entry.
5891 * Return: The next entry or %NULL
5893 void *mas_next(struct ma_state *mas, unsigned long max)
5895 if (mas_is_none(mas) || mas_is_paused(mas))
5896 mas->node = MAS_START;
5898 if (mas_is_start(mas))
5899 mas_walk(mas); /* Retries on dead nodes handled by mas_walk */
5901 if (mas_is_ptr(mas)) {
5904 mas->last = ULONG_MAX;
5909 if (mas->last == ULONG_MAX)
5912 /* Retries on dead nodes handled by mas_next_entry */
5913 return mas_next_entry(mas, max);
5915 EXPORT_SYMBOL_GPL(mas_next);
5918 * mt_next() - get the next value in the maple tree
5919 * @mt: The maple tree
5920 * @index: The start index
5921 * @max: The maximum index to check
5923 * Return: The entry at @index or higher, or %NULL if nothing is found.
5925 void *mt_next(struct maple_tree *mt, unsigned long index, unsigned long max)
5928 MA_STATE(mas, mt, index, index);
5931 entry = mas_next(&mas, max);
5935 EXPORT_SYMBOL_GPL(mt_next);
5938 * mas_prev() - Get the previous entry
5939 * @mas: The maple state
5940 * @min: The minimum value to check.
5942 * Must hold rcu_read_lock or the write lock.
5943 * Will reset mas to MAS_START if the node is MAS_NONE. Will stop on not
5946 * Return: the previous value or %NULL.
5948 void *mas_prev(struct ma_state *mas, unsigned long min)
5951 /* Nothing comes before 0 */
5953 mas->node = MAS_NONE;
5957 if (unlikely(mas_is_ptr(mas)))
5960 if (mas_is_none(mas) || mas_is_paused(mas))
5961 mas->node = MAS_START;
5963 if (mas_is_start(mas)) {
5969 if (mas_is_ptr(mas)) {
5975 mas->index = mas->last = 0;
5976 return mas_root_locked(mas);
5978 return mas_prev_entry(mas, min);
5980 EXPORT_SYMBOL_GPL(mas_prev);
5983 * mt_prev() - get the previous value in the maple tree
5984 * @mt: The maple tree
5985 * @index: The start index
5986 * @min: The minimum index to check
5988 * Return: The entry at @index or lower, or %NULL if nothing is found.
5990 void *mt_prev(struct maple_tree *mt, unsigned long index, unsigned long min)
5993 MA_STATE(mas, mt, index, index);
5996 entry = mas_prev(&mas, min);
6000 EXPORT_SYMBOL_GPL(mt_prev);
6003 * mas_pause() - Pause a mas_find/mas_for_each to drop the lock.
6004 * @mas: The maple state to pause
6006 * Some users need to pause a walk and drop the lock they're holding in
6007 * order to yield to a higher priority thread or carry out an operation
6008 * on an entry. Those users should call this function before they drop
6009 * the lock. It resets the @mas to be suitable for the next iteration
6010 * of the loop after the user has reacquired the lock. If most entries
6011 * found during a walk require you to call mas_pause(), the mt_for_each()
6012 * iterator may be more appropriate.
6015 void mas_pause(struct ma_state *mas)
6017 mas->node = MAS_PAUSE;
6019 EXPORT_SYMBOL_GPL(mas_pause);
6022 * mas_find() - On the first call, find the entry at or after mas->index up to
6023 * %max. Otherwise, find the entry after mas->index.
6024 * @mas: The maple state
6025 * @max: The maximum value to check.
6027 * Must hold rcu_read_lock or the write lock.
6028 * If an entry exists, last and index are updated accordingly.
6029 * May set @mas->node to MAS_NONE.
6031 * Return: The entry or %NULL.
6033 void *mas_find(struct ma_state *mas, unsigned long max)
6035 if (unlikely(mas_is_paused(mas))) {
6036 if (unlikely(mas->last == ULONG_MAX)) {
6037 mas->node = MAS_NONE;
6040 mas->node = MAS_START;
6041 mas->index = ++mas->last;
6044 if (unlikely(mas_is_none(mas)))
6045 mas->node = MAS_START;
6047 if (unlikely(mas_is_start(mas))) {
6048 /* First run or continue */
6051 if (mas->index > max)
6054 entry = mas_walk(mas);
6059 if (unlikely(!mas_searchable(mas)))
6062 /* Retries on dead nodes handled by mas_next_entry */
6063 return mas_next_entry(mas, max);
6065 EXPORT_SYMBOL_GPL(mas_find);
6068 * mas_find_rev: On the first call, find the first non-null entry at or below
6069 * mas->index down to %min. Otherwise find the first non-null entry below
6070 * mas->index down to %min.
6071 * @mas: The maple state
6072 * @min: The minimum value to check.
6074 * Must hold rcu_read_lock or the write lock.
6075 * If an entry exists, last and index are updated accordingly.
6076 * May set @mas->node to MAS_NONE.
6078 * Return: The entry or %NULL.
6080 void *mas_find_rev(struct ma_state *mas, unsigned long min)
6082 if (unlikely(mas_is_paused(mas))) {
6083 if (unlikely(mas->last == ULONG_MAX)) {
6084 mas->node = MAS_NONE;
6087 mas->node = MAS_START;
6088 mas->last = --mas->index;
6091 if (unlikely(mas_is_start(mas))) {
6092 /* First run or continue */
6095 if (mas->index < min)
6098 entry = mas_walk(mas);
6103 if (unlikely(!mas_searchable(mas)))
6106 if (mas->index < min)
6109 /* Retries on dead nodes handled by mas_prev_entry */
6110 return mas_prev_entry(mas, min);
6112 EXPORT_SYMBOL_GPL(mas_find_rev);
6115 * mas_erase() - Find the range in which index resides and erase the entire
6117 * @mas: The maple state
6119 * Must hold the write lock.
6120 * Searches for @mas->index, sets @mas->index and @mas->last to the range and
6121 * erases that range.
6123 * Return: the entry that was erased or %NULL, @mas->index and @mas->last are updated.
6125 void *mas_erase(struct ma_state *mas)
6128 MA_WR_STATE(wr_mas, mas, NULL);
6130 if (mas_is_none(mas) || mas_is_paused(mas))
6131 mas->node = MAS_START;
6133 /* Retry unnecessary when holding the write lock. */
6134 entry = mas_state_walk(mas);
6139 /* Must reset to ensure spanning writes of last slot are detected */
6141 mas_wr_store_setup(&wr_mas);
6142 mas_wr_store_entry(&wr_mas);
6143 if (mas_nomem(mas, GFP_KERNEL))
6148 EXPORT_SYMBOL_GPL(mas_erase);
6151 * mas_nomem() - Check if there was an error allocating and do the allocation
6152 * if necessary If there are allocations, then free them.
6153 * @mas: The maple state
6154 * @gfp: The GFP_FLAGS to use for allocations
6155 * Return: true on allocation, false otherwise.
6157 bool mas_nomem(struct ma_state *mas, gfp_t gfp)
6158 __must_hold(mas->tree->lock)
6160 if (likely(mas->node != MA_ERROR(-ENOMEM))) {
6165 if (gfpflags_allow_blocking(gfp) && !mt_external_lock(mas->tree)) {
6166 mtree_unlock(mas->tree);
6167 mas_alloc_nodes(mas, gfp);
6168 mtree_lock(mas->tree);
6170 mas_alloc_nodes(mas, gfp);
6173 if (!mas_allocated(mas))
6176 mas->node = MAS_START;
6180 void __init maple_tree_init(void)
6182 maple_node_cache = kmem_cache_create("maple_node",
6183 sizeof(struct maple_node), sizeof(struct maple_node),
6188 * mtree_load() - Load a value stored in a maple tree
6189 * @mt: The maple tree
6190 * @index: The index to load
6192 * Return: the entry or %NULL
6194 void *mtree_load(struct maple_tree *mt, unsigned long index)
6196 MA_STATE(mas, mt, index, index);
6199 trace_ma_read(__func__, &mas);
6202 entry = mas_start(&mas);
6203 if (unlikely(mas_is_none(&mas)))
6206 if (unlikely(mas_is_ptr(&mas))) {
6213 entry = mtree_lookup_walk(&mas);
6214 if (!entry && unlikely(mas_is_start(&mas)))
6218 if (xa_is_zero(entry))
6223 EXPORT_SYMBOL(mtree_load);
6226 * mtree_store_range() - Store an entry at a given range.
6227 * @mt: The maple tree
6228 * @index: The start of the range
6229 * @last: The end of the range
6230 * @entry: The entry to store
6231 * @gfp: The GFP_FLAGS to use for allocations
6233 * Return: 0 on success, -EINVAL on invalid request, -ENOMEM if memory could not
6236 int mtree_store_range(struct maple_tree *mt, unsigned long index,
6237 unsigned long last, void *entry, gfp_t gfp)
6239 MA_STATE(mas, mt, index, last);
6240 MA_WR_STATE(wr_mas, &mas, entry);
6242 trace_ma_write(__func__, &mas, 0, entry);
6243 if (WARN_ON_ONCE(xa_is_advanced(entry)))
6251 mas_wr_store_entry(&wr_mas);
6252 if (mas_nomem(&mas, gfp))
6256 if (mas_is_err(&mas))
6257 return xa_err(mas.node);
6261 EXPORT_SYMBOL(mtree_store_range);
6264 * mtree_store() - Store an entry at a given index.
6265 * @mt: The maple tree
6266 * @index: The index to store the value
6267 * @entry: The entry to store
6268 * @gfp: The GFP_FLAGS to use for allocations
6270 * Return: 0 on success, -EINVAL on invalid request, -ENOMEM if memory could not
6273 int mtree_store(struct maple_tree *mt, unsigned long index, void *entry,
6276 return mtree_store_range(mt, index, index, entry, gfp);
6278 EXPORT_SYMBOL(mtree_store);
6281 * mtree_insert_range() - Insert an entry at a give range if there is no value.
6282 * @mt: The maple tree
6283 * @first: The start of the range
6284 * @last: The end of the range
6285 * @entry: The entry to store
6286 * @gfp: The GFP_FLAGS to use for allocations.
6288 * Return: 0 on success, -EEXISTS if the range is occupied, -EINVAL on invalid
6289 * request, -ENOMEM if memory could not be allocated.
6291 int mtree_insert_range(struct maple_tree *mt, unsigned long first,
6292 unsigned long last, void *entry, gfp_t gfp)
6294 MA_STATE(ms, mt, first, last);
6296 if (WARN_ON_ONCE(xa_is_advanced(entry)))
6304 mas_insert(&ms, entry);
6305 if (mas_nomem(&ms, gfp))
6309 if (mas_is_err(&ms))
6310 return xa_err(ms.node);
6314 EXPORT_SYMBOL(mtree_insert_range);
6317 * mtree_insert() - Insert an entry at a give index if there is no value.
6318 * @mt: The maple tree
6319 * @index : The index to store the value
6320 * @entry: The entry to store
6321 * @gfp: The FGP_FLAGS to use for allocations.
6323 * Return: 0 on success, -EEXISTS if the range is occupied, -EINVAL on invalid
6324 * request, -ENOMEM if memory could not be allocated.
6326 int mtree_insert(struct maple_tree *mt, unsigned long index, void *entry,
6329 return mtree_insert_range(mt, index, index, entry, gfp);
6331 EXPORT_SYMBOL(mtree_insert);
6333 int mtree_alloc_range(struct maple_tree *mt, unsigned long *startp,
6334 void *entry, unsigned long size, unsigned long min,
6335 unsigned long max, gfp_t gfp)
6339 MA_STATE(mas, mt, min, max - size);
6340 if (!mt_is_alloc(mt))
6343 if (WARN_ON_ONCE(mt_is_reserved(entry)))
6359 mas.last = max - size;
6360 ret = mas_alloc(&mas, entry, size, startp);
6361 if (mas_nomem(&mas, gfp))
6367 EXPORT_SYMBOL(mtree_alloc_range);
6369 int mtree_alloc_rrange(struct maple_tree *mt, unsigned long *startp,
6370 void *entry, unsigned long size, unsigned long min,
6371 unsigned long max, gfp_t gfp)
6375 MA_STATE(mas, mt, min, max - size);
6376 if (!mt_is_alloc(mt))
6379 if (WARN_ON_ONCE(mt_is_reserved(entry)))
6393 ret = mas_rev_alloc(&mas, min, max, entry, size, startp);
6394 if (mas_nomem(&mas, gfp))
6400 EXPORT_SYMBOL(mtree_alloc_rrange);
6403 * mtree_erase() - Find an index and erase the entire range.
6404 * @mt: The maple tree
6405 * @index: The index to erase
6407 * Erasing is the same as a walk to an entry then a store of a NULL to that
6408 * ENTIRE range. In fact, it is implemented as such using the advanced API.
6410 * Return: The entry stored at the @index or %NULL
6412 void *mtree_erase(struct maple_tree *mt, unsigned long index)
6416 MA_STATE(mas, mt, index, index);
6417 trace_ma_op(__func__, &mas);
6420 entry = mas_erase(&mas);
6425 EXPORT_SYMBOL(mtree_erase);
6428 * __mt_destroy() - Walk and free all nodes of a locked maple tree.
6429 * @mt: The maple tree
6431 * Note: Does not handle locking.
6433 void __mt_destroy(struct maple_tree *mt)
6435 void *root = mt_root_locked(mt);
6437 rcu_assign_pointer(mt->ma_root, NULL);
6438 if (xa_is_node(root))
6439 mte_destroy_walk(root, mt);
6443 EXPORT_SYMBOL_GPL(__mt_destroy);
6446 * mtree_destroy() - Destroy a maple tree
6447 * @mt: The maple tree
6449 * Frees all resources used by the tree. Handles locking.
6451 void mtree_destroy(struct maple_tree *mt)
6457 EXPORT_SYMBOL(mtree_destroy);
6460 * mt_find() - Search from the start up until an entry is found.
6461 * @mt: The maple tree
6462 * @index: Pointer which contains the start location of the search
6463 * @max: The maximum value to check
6465 * Handles locking. @index will be incremented to one beyond the range.
6467 * Return: The entry at or after the @index or %NULL
6469 void *mt_find(struct maple_tree *mt, unsigned long *index, unsigned long max)
6471 MA_STATE(mas, mt, *index, *index);
6473 #ifdef CONFIG_DEBUG_MAPLE_TREE
6474 unsigned long copy = *index;
6477 trace_ma_read(__func__, &mas);
6484 entry = mas_state_walk(&mas);
6485 if (mas_is_start(&mas))
6488 if (unlikely(xa_is_zero(entry)))
6494 while (mas_searchable(&mas) && (mas.index < max)) {
6495 entry = mas_next_entry(&mas, max);
6496 if (likely(entry && !xa_is_zero(entry)))
6500 if (unlikely(xa_is_zero(entry)))
6504 if (likely(entry)) {
6505 *index = mas.last + 1;
6506 #ifdef CONFIG_DEBUG_MAPLE_TREE
6507 if ((*index) && (*index) <= copy)
6508 pr_err("index not increased! %lx <= %lx\n",
6510 MT_BUG_ON(mt, (*index) && ((*index) <= copy));
6516 EXPORT_SYMBOL(mt_find);
6519 * mt_find_after() - Search from the start up until an entry is found.
6520 * @mt: The maple tree
6521 * @index: Pointer which contains the start location of the search
6522 * @max: The maximum value to check
6524 * Handles locking, detects wrapping on index == 0
6526 * Return: The entry at or after the @index or %NULL
6528 void *mt_find_after(struct maple_tree *mt, unsigned long *index,
6534 return mt_find(mt, index, max);
6536 EXPORT_SYMBOL(mt_find_after);
6538 #ifdef CONFIG_DEBUG_MAPLE_TREE
6539 atomic_t maple_tree_tests_run;
6540 EXPORT_SYMBOL_GPL(maple_tree_tests_run);
6541 atomic_t maple_tree_tests_passed;
6542 EXPORT_SYMBOL_GPL(maple_tree_tests_passed);
6545 extern void kmem_cache_set_non_kernel(struct kmem_cache *, unsigned int);
6546 void mt_set_non_kernel(unsigned int val)
6548 kmem_cache_set_non_kernel(maple_node_cache, val);
6551 extern unsigned long kmem_cache_get_alloc(struct kmem_cache *);
6552 unsigned long mt_get_alloc_size(void)
6554 return kmem_cache_get_alloc(maple_node_cache);
6557 extern void kmem_cache_zero_nr_tallocated(struct kmem_cache *);
6558 void mt_zero_nr_tallocated(void)
6560 kmem_cache_zero_nr_tallocated(maple_node_cache);
6563 extern unsigned int kmem_cache_nr_tallocated(struct kmem_cache *);
6564 unsigned int mt_nr_tallocated(void)
6566 return kmem_cache_nr_tallocated(maple_node_cache);
6569 extern unsigned int kmem_cache_nr_allocated(struct kmem_cache *);
6570 unsigned int mt_nr_allocated(void)
6572 return kmem_cache_nr_allocated(maple_node_cache);
6576 * mas_dead_node() - Check if the maple state is pointing to a dead node.
6577 * @mas: The maple state
6578 * @index: The index to restore in @mas.
6580 * Used in test code.
6581 * Return: 1 if @mas has been reset to MAS_START, 0 otherwise.
6583 static inline int mas_dead_node(struct ma_state *mas, unsigned long index)
6585 if (unlikely(!mas_searchable(mas) || mas_is_start(mas)))
6588 if (likely(!mte_dead_node(mas->node)))
6591 mas_rewalk(mas, index);
6595 void mt_cache_shrink(void)
6600 * mt_cache_shrink() - For testing, don't use this.
6602 * Certain testcases can trigger an OOM when combined with other memory
6603 * debugging configuration options. This function is used to reduce the
6604 * possibility of an out of memory even due to kmem_cache objects remaining
6605 * around for longer than usual.
6607 void mt_cache_shrink(void)
6609 kmem_cache_shrink(maple_node_cache);
6612 EXPORT_SYMBOL_GPL(mt_cache_shrink);
6614 #endif /* not defined __KERNEL__ */
6616 * mas_get_slot() - Get the entry in the maple state node stored at @offset.
6617 * @mas: The maple state
6618 * @offset: The offset into the slot array to fetch.
6620 * Return: The entry stored at @offset.
6622 static inline struct maple_enode *mas_get_slot(struct ma_state *mas,
6623 unsigned char offset)
6625 return mas_slot(mas, ma_slots(mas_mn(mas), mte_node_type(mas->node)),
6631 * mas_first_entry() - Go the first leaf and find the first entry.
6632 * @mas: the maple state.
6633 * @limit: the maximum index to check.
6634 * @*r_start: Pointer to set to the range start.
6636 * Sets mas->offset to the offset of the entry, r_start to the range minimum.
6638 * Return: The first entry or MAS_NONE.
6640 static inline void *mas_first_entry(struct ma_state *mas, struct maple_node *mn,
6641 unsigned long limit, enum maple_type mt)
6645 unsigned long *pivots;
6649 mas->index = mas->min;
6650 if (mas->index > limit)
6655 while (likely(!ma_is_leaf(mt))) {
6656 MT_BUG_ON(mas->tree, mte_dead_node(mas->node));
6657 slots = ma_slots(mn, mt);
6658 entry = mas_slot(mas, slots, 0);
6659 pivots = ma_pivots(mn, mt);
6660 if (unlikely(ma_dead_node(mn)))
6665 mt = mte_node_type(mas->node);
6667 MT_BUG_ON(mas->tree, mte_dead_node(mas->node));
6670 slots = ma_slots(mn, mt);
6671 entry = mas_slot(mas, slots, 0);
6672 if (unlikely(ma_dead_node(mn)))
6675 /* Slot 0 or 1 must be set */
6676 if (mas->index > limit)
6683 entry = mas_slot(mas, slots, 1);
6684 pivots = ma_pivots(mn, mt);
6685 if (unlikely(ma_dead_node(mn)))
6688 mas->index = pivots[0] + 1;
6689 if (mas->index > limit)
6696 if (likely(!ma_dead_node(mn)))
6697 mas->node = MAS_NONE;
6701 /* Depth first search, post-order */
6702 static void mas_dfs_postorder(struct ma_state *mas, unsigned long max)
6705 struct maple_enode *p = MAS_NONE, *mn = mas->node;
6706 unsigned long p_min, p_max;
6708 mas_next_node(mas, mas_mn(mas), max);
6709 if (!mas_is_none(mas))
6712 if (mte_is_root(mn))
6717 while (mas->node != MAS_NONE) {
6721 mas_prev_node(mas, 0);
6732 /* Tree validations */
6733 static void mt_dump_node(const struct maple_tree *mt, void *entry,
6734 unsigned long min, unsigned long max, unsigned int depth);
6735 static void mt_dump_range(unsigned long min, unsigned long max,
6738 static const char spaces[] = " ";
6741 pr_info("%.*s%lu: ", depth * 2, spaces, min);
6743 pr_info("%.*s%lu-%lu: ", depth * 2, spaces, min, max);
6746 static void mt_dump_entry(void *entry, unsigned long min, unsigned long max,
6749 mt_dump_range(min, max, depth);
6751 if (xa_is_value(entry))
6752 pr_cont("value %ld (0x%lx) [%p]\n", xa_to_value(entry),
6753 xa_to_value(entry), entry);
6754 else if (xa_is_zero(entry))
6755 pr_cont("zero (%ld)\n", xa_to_internal(entry));
6756 else if (mt_is_reserved(entry))
6757 pr_cont("UNKNOWN ENTRY (%p)\n", entry);
6759 pr_cont("%p\n", entry);
6762 static void mt_dump_range64(const struct maple_tree *mt, void *entry,
6763 unsigned long min, unsigned long max, unsigned int depth)
6765 struct maple_range_64 *node = &mte_to_node(entry)->mr64;
6766 bool leaf = mte_is_leaf(entry);
6767 unsigned long first = min;
6770 pr_cont(" contents: ");
6771 for (i = 0; i < MAPLE_RANGE64_SLOTS - 1; i++)
6772 pr_cont("%p %lu ", node->slot[i], node->pivot[i]);
6773 pr_cont("%p\n", node->slot[i]);
6774 for (i = 0; i < MAPLE_RANGE64_SLOTS; i++) {
6775 unsigned long last = max;
6777 if (i < (MAPLE_RANGE64_SLOTS - 1))
6778 last = node->pivot[i];
6779 else if (!node->slot[i] && max != mt_node_max(entry))
6781 if (last == 0 && i > 0)
6784 mt_dump_entry(mt_slot(mt, node->slot, i),
6785 first, last, depth + 1);
6786 else if (node->slot[i])
6787 mt_dump_node(mt, mt_slot(mt, node->slot, i),
6788 first, last, depth + 1);
6793 pr_err("node %p last (%lu) > max (%lu) at pivot %d!\n",
6794 node, last, max, i);
6801 static void mt_dump_arange64(const struct maple_tree *mt, void *entry,
6802 unsigned long min, unsigned long max, unsigned int depth)
6804 struct maple_arange_64 *node = &mte_to_node(entry)->ma64;
6805 bool leaf = mte_is_leaf(entry);
6806 unsigned long first = min;
6809 pr_cont(" contents: ");
6810 for (i = 0; i < MAPLE_ARANGE64_SLOTS; i++)
6811 pr_cont("%lu ", node->gap[i]);
6812 pr_cont("| %02X %02X| ", node->meta.end, node->meta.gap);
6813 for (i = 0; i < MAPLE_ARANGE64_SLOTS - 1; i++)
6814 pr_cont("%p %lu ", node->slot[i], node->pivot[i]);
6815 pr_cont("%p\n", node->slot[i]);
6816 for (i = 0; i < MAPLE_ARANGE64_SLOTS; i++) {
6817 unsigned long last = max;
6819 if (i < (MAPLE_ARANGE64_SLOTS - 1))
6820 last = node->pivot[i];
6821 else if (!node->slot[i])
6823 if (last == 0 && i > 0)
6826 mt_dump_entry(mt_slot(mt, node->slot, i),
6827 first, last, depth + 1);
6828 else if (node->slot[i])
6829 mt_dump_node(mt, mt_slot(mt, node->slot, i),
6830 first, last, depth + 1);
6835 pr_err("node %p last (%lu) > max (%lu) at pivot %d!\n",
6836 node, last, max, i);
6843 static void mt_dump_node(const struct maple_tree *mt, void *entry,
6844 unsigned long min, unsigned long max, unsigned int depth)
6846 struct maple_node *node = mte_to_node(entry);
6847 unsigned int type = mte_node_type(entry);
6850 mt_dump_range(min, max, depth);
6852 pr_cont("node %p depth %d type %d parent %p", node, depth, type,
6853 node ? node->parent : NULL);
6857 for (i = 0; i < MAPLE_NODE_SLOTS; i++) {
6859 pr_cont("OUT OF RANGE: ");
6860 mt_dump_entry(mt_slot(mt, node->slot, i),
6861 min + i, min + i, depth);
6865 case maple_range_64:
6866 mt_dump_range64(mt, entry, min, max, depth);
6868 case maple_arange_64:
6869 mt_dump_arange64(mt, entry, min, max, depth);
6873 pr_cont(" UNKNOWN TYPE\n");
6877 void mt_dump(const struct maple_tree *mt)
6879 void *entry = rcu_dereference_check(mt->ma_root, mt_locked(mt));
6881 pr_info("maple_tree(%p) flags %X, height %u root %p\n",
6882 mt, mt->ma_flags, mt_height(mt), entry);
6883 if (!xa_is_node(entry))
6884 mt_dump_entry(entry, 0, 0, 0);
6886 mt_dump_node(mt, entry, 0, mt_node_max(entry), 0);
6888 EXPORT_SYMBOL_GPL(mt_dump);
6891 * Calculate the maximum gap in a node and check if that's what is reported in
6892 * the parent (unless root).
6894 static void mas_validate_gaps(struct ma_state *mas)
6896 struct maple_enode *mte = mas->node;
6897 struct maple_node *p_mn;
6898 unsigned long gap = 0, max_gap = 0;
6899 unsigned long p_end, p_start = mas->min;
6900 unsigned char p_slot;
6901 unsigned long *gaps = NULL;
6902 unsigned long *pivots = ma_pivots(mte_to_node(mte), mte_node_type(mte));
6905 if (ma_is_dense(mte_node_type(mte))) {
6906 for (i = 0; i < mt_slot_count(mte); i++) {
6907 if (mas_get_slot(mas, i)) {
6918 gaps = ma_gaps(mte_to_node(mte), mte_node_type(mte));
6919 for (i = 0; i < mt_slot_count(mte); i++) {
6920 p_end = mas_logical_pivot(mas, pivots, i, mte_node_type(mte));
6923 if (mas_get_slot(mas, i)) {
6928 gap += p_end - p_start + 1;
6930 void *entry = mas_get_slot(mas, i);
6934 if (gap != p_end - p_start + 1) {
6935 pr_err("%p[%u] -> %p %lu != %lu - %lu + 1\n",
6937 mas_get_slot(mas, i), gap,
6941 MT_BUG_ON(mas->tree,
6942 gap != p_end - p_start + 1);
6945 if (gap > p_end - p_start + 1) {
6946 pr_err("%p[%u] %lu >= %lu - %lu + 1 (%lu)\n",
6947 mas_mn(mas), i, gap, p_end, p_start,
6948 p_end - p_start + 1);
6949 MT_BUG_ON(mas->tree,
6950 gap > p_end - p_start + 1);
6958 p_start = p_end + 1;
6959 if (p_end >= mas->max)
6964 if (mte_is_root(mte))
6967 p_slot = mte_parent_slot(mas->node);
6968 p_mn = mte_parent(mte);
6969 MT_BUG_ON(mas->tree, max_gap > mas->max);
6970 if (ma_gaps(p_mn, mas_parent_enum(mas, mte))[p_slot] != max_gap) {
6971 pr_err("gap %p[%u] != %lu\n", p_mn, p_slot, max_gap);
6975 MT_BUG_ON(mas->tree,
6976 ma_gaps(p_mn, mas_parent_enum(mas, mte))[p_slot] != max_gap);
6979 static void mas_validate_parent_slot(struct ma_state *mas)
6981 struct maple_node *parent;
6982 struct maple_enode *node;
6983 enum maple_type p_type = mas_parent_enum(mas, mas->node);
6984 unsigned char p_slot = mte_parent_slot(mas->node);
6988 if (mte_is_root(mas->node))
6991 parent = mte_parent(mas->node);
6992 slots = ma_slots(parent, p_type);
6993 MT_BUG_ON(mas->tree, mas_mn(mas) == parent);
6995 /* Check prev/next parent slot for duplicate node entry */
6997 for (i = 0; i < mt_slots[p_type]; i++) {
6998 node = mas_slot(mas, slots, i);
7000 if (node != mas->node)
7001 pr_err("parent %p[%u] does not have %p\n",
7002 parent, i, mas_mn(mas));
7003 MT_BUG_ON(mas->tree, node != mas->node);
7004 } else if (node == mas->node) {
7005 pr_err("Invalid child %p at parent %p[%u] p_slot %u\n",
7006 mas_mn(mas), parent, i, p_slot);
7007 MT_BUG_ON(mas->tree, node == mas->node);
7012 static void mas_validate_child_slot(struct ma_state *mas)
7014 enum maple_type type = mte_node_type(mas->node);
7015 void __rcu **slots = ma_slots(mte_to_node(mas->node), type);
7016 unsigned long *pivots = ma_pivots(mte_to_node(mas->node), type);
7017 struct maple_enode *child;
7020 if (mte_is_leaf(mas->node))
7023 for (i = 0; i < mt_slots[type]; i++) {
7024 child = mas_slot(mas, slots, i);
7025 if (!pivots[i] || pivots[i] == mas->max)
7031 if (mte_parent_slot(child) != i) {
7032 pr_err("Slot error at %p[%u]: child %p has pslot %u\n",
7033 mas_mn(mas), i, mte_to_node(child),
7034 mte_parent_slot(child));
7035 MT_BUG_ON(mas->tree, 1);
7038 if (mte_parent(child) != mte_to_node(mas->node)) {
7039 pr_err("child %p has parent %p not %p\n",
7040 mte_to_node(child), mte_parent(child),
7041 mte_to_node(mas->node));
7042 MT_BUG_ON(mas->tree, 1);
7048 * Validate all pivots are within mas->min and mas->max.
7050 static void mas_validate_limits(struct ma_state *mas)
7053 unsigned long prev_piv = 0;
7054 enum maple_type type = mte_node_type(mas->node);
7055 void __rcu **slots = ma_slots(mte_to_node(mas->node), type);
7056 unsigned long *pivots = ma_pivots(mas_mn(mas), type);
7058 /* all limits are fine here. */
7059 if (mte_is_root(mas->node))
7062 for (i = 0; i < mt_slots[type]; i++) {
7065 piv = mas_safe_pivot(mas, pivots, i, type);
7067 if (!piv && (i != 0))
7070 if (!mte_is_leaf(mas->node)) {
7071 void *entry = mas_slot(mas, slots, i);
7074 pr_err("%p[%u] cannot be null\n",
7077 MT_BUG_ON(mas->tree, !entry);
7080 if (prev_piv > piv) {
7081 pr_err("%p[%u] piv %lu < prev_piv %lu\n",
7082 mas_mn(mas), i, piv, prev_piv);
7083 MT_BUG_ON(mas->tree, piv < prev_piv);
7086 if (piv < mas->min) {
7087 pr_err("%p[%u] %lu < %lu\n", mas_mn(mas), i,
7089 MT_BUG_ON(mas->tree, piv < mas->min);
7091 if (piv > mas->max) {
7092 pr_err("%p[%u] %lu > %lu\n", mas_mn(mas), i,
7094 MT_BUG_ON(mas->tree, piv > mas->max);
7097 if (piv == mas->max)
7100 for (i += 1; i < mt_slots[type]; i++) {
7101 void *entry = mas_slot(mas, slots, i);
7103 if (entry && (i != mt_slots[type] - 1)) {
7104 pr_err("%p[%u] should not have entry %p\n", mas_mn(mas),
7106 MT_BUG_ON(mas->tree, entry != NULL);
7109 if (i < mt_pivots[type]) {
7110 unsigned long piv = pivots[i];
7115 pr_err("%p[%u] should not have piv %lu\n",
7116 mas_mn(mas), i, piv);
7117 MT_BUG_ON(mas->tree, i < mt_pivots[type] - 1);
7122 static void mt_validate_nulls(struct maple_tree *mt)
7124 void *entry, *last = (void *)1;
7125 unsigned char offset = 0;
7127 MA_STATE(mas, mt, 0, 0);
7130 if (mas_is_none(&mas) || (mas.node == MAS_ROOT))
7133 while (!mte_is_leaf(mas.node))
7136 slots = ma_slots(mte_to_node(mas.node), mte_node_type(mas.node));
7138 entry = mas_slot(&mas, slots, offset);
7139 if (!last && !entry) {
7140 pr_err("Sequential nulls end at %p[%u]\n",
7141 mas_mn(&mas), offset);
7143 MT_BUG_ON(mt, !last && !entry);
7145 if (offset == mas_data_end(&mas)) {
7146 mas_next_node(&mas, mas_mn(&mas), ULONG_MAX);
7147 if (mas_is_none(&mas))
7150 slots = ma_slots(mte_to_node(mas.node),
7151 mte_node_type(mas.node));
7156 } while (!mas_is_none(&mas));
7160 * validate a maple tree by checking:
7161 * 1. The limits (pivots are within mas->min to mas->max)
7162 * 2. The gap is correctly set in the parents
7164 void mt_validate(struct maple_tree *mt)
7168 MA_STATE(mas, mt, 0, 0);
7171 if (!mas_searchable(&mas))
7174 mas_first_entry(&mas, mas_mn(&mas), ULONG_MAX, mte_node_type(mas.node));
7175 while (!mas_is_none(&mas)) {
7176 MT_BUG_ON(mas.tree, mte_dead_node(mas.node));
7177 if (!mte_is_root(mas.node)) {
7178 end = mas_data_end(&mas);
7179 if ((end < mt_min_slot_count(mas.node)) &&
7180 (mas.max != ULONG_MAX)) {
7181 pr_err("Invalid size %u of %p\n", end,
7183 MT_BUG_ON(mas.tree, 1);
7187 mas_validate_parent_slot(&mas);
7188 mas_validate_child_slot(&mas);
7189 mas_validate_limits(&mas);
7190 if (mt_is_alloc(mt))
7191 mas_validate_gaps(&mas);
7192 mas_dfs_postorder(&mas, ULONG_MAX);
7194 mt_validate_nulls(mt);
7199 EXPORT_SYMBOL_GPL(mt_validate);
7201 #endif /* CONFIG_DEBUG_MAPLE_TREE */