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;
1363 root = mas_root(mas);
1364 /* Tree with nodes */
1365 if (likely(xa_is_node(root))) {
1367 mas->node = mte_safe_root(root);
1373 if (unlikely(!root)) {
1374 mas->node = MAS_NONE;
1375 mas->offset = MAPLE_NODE_SLOTS;
1379 /* Single entry tree */
1380 mas->node = MAS_ROOT;
1381 mas->offset = MAPLE_NODE_SLOTS;
1383 /* Single entry tree. */
1394 * ma_data_end() - Find the end of the data in a node.
1395 * @node: The maple node
1396 * @type: The maple node type
1397 * @pivots: The array of pivots in the node
1398 * @max: The maximum value in the node
1400 * Uses metadata to find the end of the data when possible.
1401 * Return: The zero indexed last slot with data (may be null).
1403 static inline unsigned char ma_data_end(struct maple_node *node,
1404 enum maple_type type,
1405 unsigned long *pivots,
1408 unsigned char offset;
1413 if (type == maple_arange_64)
1414 return ma_meta_end(node, type);
1416 offset = mt_pivots[type] - 1;
1417 if (likely(!pivots[offset]))
1418 return ma_meta_end(node, type);
1420 if (likely(pivots[offset] == max))
1423 return mt_pivots[type];
1427 * mas_data_end() - Find the end of the data (slot).
1428 * @mas: the maple state
1430 * This method is optimized to check the metadata of a node if the node type
1431 * supports data end metadata.
1433 * Return: The zero indexed last slot with data (may be null).
1435 static inline unsigned char mas_data_end(struct ma_state *mas)
1437 enum maple_type type;
1438 struct maple_node *node;
1439 unsigned char offset;
1440 unsigned long *pivots;
1442 type = mte_node_type(mas->node);
1444 if (type == maple_arange_64)
1445 return ma_meta_end(node, type);
1447 pivots = ma_pivots(node, type);
1448 if (unlikely(ma_dead_node(node)))
1451 offset = mt_pivots[type] - 1;
1452 if (likely(!pivots[offset]))
1453 return ma_meta_end(node, type);
1455 if (likely(pivots[offset] == mas->max))
1458 return mt_pivots[type];
1462 * mas_leaf_max_gap() - Returns the largest gap in a leaf node
1463 * @mas - the maple state
1465 * Return: The maximum gap in the leaf.
1467 static unsigned long mas_leaf_max_gap(struct ma_state *mas)
1470 unsigned long pstart, gap, max_gap;
1471 struct maple_node *mn;
1472 unsigned long *pivots;
1475 unsigned char max_piv;
1477 mt = mte_node_type(mas->node);
1479 slots = ma_slots(mn, mt);
1481 if (unlikely(ma_is_dense(mt))) {
1483 for (i = 0; i < mt_slots[mt]; i++) {
1498 * Check the first implied pivot optimizes the loop below and slot 1 may
1499 * be skipped if there is a gap in slot 0.
1501 pivots = ma_pivots(mn, mt);
1502 if (likely(!slots[0])) {
1503 max_gap = pivots[0] - mas->min + 1;
1509 /* reduce max_piv as the special case is checked before the loop */
1510 max_piv = ma_data_end(mn, mt, pivots, mas->max) - 1;
1512 * Check end implied pivot which can only be a gap on the right most
1515 if (unlikely(mas->max == ULONG_MAX) && !slots[max_piv + 1]) {
1516 gap = ULONG_MAX - pivots[max_piv];
1521 for (; i <= max_piv; i++) {
1522 /* data == no gap. */
1523 if (likely(slots[i]))
1526 pstart = pivots[i - 1];
1527 gap = pivots[i] - pstart;
1531 /* There cannot be two gaps in a row. */
1538 * ma_max_gap() - Get the maximum gap in a maple node (non-leaf)
1539 * @node: The maple node
1540 * @gaps: The pointer to the gaps
1541 * @mt: The maple node type
1542 * @*off: Pointer to store the offset location of the gap.
1544 * Uses the metadata data end to scan backwards across set gaps.
1546 * Return: The maximum gap value
1548 static inline unsigned long
1549 ma_max_gap(struct maple_node *node, unsigned long *gaps, enum maple_type mt,
1552 unsigned char offset, i;
1553 unsigned long max_gap = 0;
1555 i = offset = ma_meta_end(node, mt);
1557 if (gaps[i] > max_gap) {
1568 * mas_max_gap() - find the largest gap in a non-leaf node and set the slot.
1569 * @mas: The maple state.
1571 * If the metadata gap is set to MAPLE_ARANGE64_META_MAX, there is no gap.
1573 * Return: The gap value.
1575 static inline unsigned long mas_max_gap(struct ma_state *mas)
1577 unsigned long *gaps;
1578 unsigned char offset;
1580 struct maple_node *node;
1582 mt = mte_node_type(mas->node);
1584 return mas_leaf_max_gap(mas);
1587 offset = ma_meta_gap(node, mt);
1588 if (offset == MAPLE_ARANGE64_META_MAX)
1591 gaps = ma_gaps(node, mt);
1592 return gaps[offset];
1596 * mas_parent_gap() - Set the parent gap and any gaps above, as needed
1597 * @mas: The maple state
1598 * @offset: The gap offset in the parent to set
1599 * @new: The new gap value.
1601 * Set the parent gap then continue to set the gap upwards, using the metadata
1602 * of the parent to see if it is necessary to check the node above.
1604 static inline void mas_parent_gap(struct ma_state *mas, unsigned char offset,
1607 unsigned long meta_gap = 0;
1608 struct maple_node *pnode;
1609 struct maple_enode *penode;
1610 unsigned long *pgaps;
1611 unsigned char meta_offset;
1612 enum maple_type pmt;
1614 pnode = mte_parent(mas->node);
1615 pmt = mas_parent_enum(mas, mas->node);
1616 penode = mt_mk_node(pnode, pmt);
1617 pgaps = ma_gaps(pnode, pmt);
1620 meta_offset = ma_meta_gap(pnode, pmt);
1621 if (meta_offset == MAPLE_ARANGE64_META_MAX)
1624 meta_gap = pgaps[meta_offset];
1626 pgaps[offset] = new;
1628 if (meta_gap == new)
1631 if (offset != meta_offset) {
1635 ma_set_meta_gap(pnode, pmt, offset);
1636 } else if (new < meta_gap) {
1638 new = ma_max_gap(pnode, pgaps, pmt, &meta_offset);
1639 ma_set_meta_gap(pnode, pmt, meta_offset);
1642 if (ma_is_root(pnode))
1645 /* Go to the parent node. */
1646 pnode = mte_parent(penode);
1647 pmt = mas_parent_enum(mas, penode);
1648 pgaps = ma_gaps(pnode, pmt);
1649 offset = mte_parent_slot(penode);
1650 penode = mt_mk_node(pnode, pmt);
1655 * mas_update_gap() - Update a nodes gaps and propagate up if necessary.
1656 * @mas - the maple state.
1658 static inline void mas_update_gap(struct ma_state *mas)
1660 unsigned char pslot;
1661 unsigned long p_gap;
1662 unsigned long max_gap;
1664 if (!mt_is_alloc(mas->tree))
1667 if (mte_is_root(mas->node))
1670 max_gap = mas_max_gap(mas);
1672 pslot = mte_parent_slot(mas->node);
1673 p_gap = ma_gaps(mte_parent(mas->node),
1674 mas_parent_enum(mas, mas->node))[pslot];
1676 if (p_gap != max_gap)
1677 mas_parent_gap(mas, pslot, max_gap);
1681 * mas_adopt_children() - Set the parent pointer of all nodes in @parent to
1682 * @parent with the slot encoded.
1683 * @mas - the maple state (for the tree)
1684 * @parent - the maple encoded node containing the children.
1686 static inline void mas_adopt_children(struct ma_state *mas,
1687 struct maple_enode *parent)
1689 enum maple_type type = mte_node_type(parent);
1690 struct maple_node *node = mas_mn(mas);
1691 void __rcu **slots = ma_slots(node, type);
1692 unsigned long *pivots = ma_pivots(node, type);
1693 struct maple_enode *child;
1694 unsigned char offset;
1696 offset = ma_data_end(node, type, pivots, mas->max);
1698 child = mas_slot_locked(mas, slots, offset);
1699 mte_set_parent(child, parent, offset);
1704 * mas_replace() - Replace a maple node in the tree with mas->node. Uses the
1705 * parent encoding to locate the maple node in the tree.
1706 * @mas - the ma_state to use for operations.
1707 * @advanced - boolean to adopt the child nodes and free the old node (false) or
1708 * leave the node (true) and handle the adoption and free elsewhere.
1710 static inline void mas_replace(struct ma_state *mas, bool advanced)
1711 __must_hold(mas->tree->lock)
1713 struct maple_node *mn = mas_mn(mas);
1714 struct maple_enode *old_enode;
1715 unsigned char offset = 0;
1716 void __rcu **slots = NULL;
1718 if (ma_is_root(mn)) {
1719 old_enode = mas_root_locked(mas);
1721 offset = mte_parent_slot(mas->node);
1722 slots = ma_slots(mte_parent(mas->node),
1723 mas_parent_enum(mas, mas->node));
1724 old_enode = mas_slot_locked(mas, slots, offset);
1727 if (!advanced && !mte_is_leaf(mas->node))
1728 mas_adopt_children(mas, mas->node);
1730 if (mte_is_root(mas->node)) {
1731 mn->parent = ma_parent_ptr(
1732 ((unsigned long)mas->tree | MA_ROOT_PARENT));
1733 rcu_assign_pointer(mas->tree->ma_root, mte_mk_root(mas->node));
1734 mas_set_height(mas);
1736 rcu_assign_pointer(slots[offset], mas->node);
1740 mas_free(mas, old_enode);
1744 * mas_new_child() - Find the new child of a node.
1745 * @mas: the maple state
1746 * @child: the maple state to store the child.
1748 static inline bool mas_new_child(struct ma_state *mas, struct ma_state *child)
1749 __must_hold(mas->tree->lock)
1752 unsigned char offset;
1754 unsigned long *pivots;
1755 struct maple_enode *entry;
1756 struct maple_node *node;
1759 mt = mte_node_type(mas->node);
1761 slots = ma_slots(node, mt);
1762 pivots = ma_pivots(node, mt);
1763 end = ma_data_end(node, mt, pivots, mas->max);
1764 for (offset = mas->offset; offset <= end; offset++) {
1765 entry = mas_slot_locked(mas, slots, offset);
1766 if (mte_parent(entry) == node) {
1768 mas->offset = offset + 1;
1769 child->offset = offset;
1779 * mab_shift_right() - Shift the data in mab right. Note, does not clean out the
1780 * old data or set b_node->b_end.
1781 * @b_node: the maple_big_node
1782 * @shift: the shift count
1784 static inline void mab_shift_right(struct maple_big_node *b_node,
1785 unsigned char shift)
1787 unsigned long size = b_node->b_end * sizeof(unsigned long);
1789 memmove(b_node->pivot + shift, b_node->pivot, size);
1790 memmove(b_node->slot + shift, b_node->slot, size);
1791 if (b_node->type == maple_arange_64)
1792 memmove(b_node->gap + shift, b_node->gap, size);
1796 * mab_middle_node() - Check if a middle node is needed (unlikely)
1797 * @b_node: the maple_big_node that contains the data.
1798 * @size: the amount of data in the b_node
1799 * @split: the potential split location
1800 * @slot_count: the size that can be stored in a single node being considered.
1802 * Return: true if a middle node is required.
1804 static inline bool mab_middle_node(struct maple_big_node *b_node, int split,
1805 unsigned char slot_count)
1807 unsigned char size = b_node->b_end;
1809 if (size >= 2 * slot_count)
1812 if (!b_node->slot[split] && (size >= 2 * slot_count - 1))
1819 * mab_no_null_split() - ensure the split doesn't fall on a NULL
1820 * @b_node: the maple_big_node with the data
1821 * @split: the suggested split location
1822 * @slot_count: the number of slots in the node being considered.
1824 * Return: the split location.
1826 static inline int mab_no_null_split(struct maple_big_node *b_node,
1827 unsigned char split, unsigned char slot_count)
1829 if (!b_node->slot[split]) {
1831 * If the split is less than the max slot && the right side will
1832 * still be sufficient, then increment the split on NULL.
1834 if ((split < slot_count - 1) &&
1835 (b_node->b_end - split) > (mt_min_slots[b_node->type]))
1844 * mab_calc_split() - Calculate the split location and if there needs to be two
1846 * @bn: The maple_big_node with the data
1847 * @mid_split: The second split, if required. 0 otherwise.
1849 * Return: The first split location. The middle split is set in @mid_split.
1851 static inline int mab_calc_split(struct ma_state *mas,
1852 struct maple_big_node *bn, unsigned char *mid_split, unsigned long min)
1854 unsigned char b_end = bn->b_end;
1855 int split = b_end / 2; /* Assume equal split. */
1856 unsigned char slot_min, slot_count = mt_slots[bn->type];
1859 * To support gap tracking, all NULL entries are kept together and a node cannot
1860 * end on a NULL entry, with the exception of the left-most leaf. The
1861 * limitation means that the split of a node must be checked for this condition
1862 * and be able to put more data in one direction or the other.
1864 if (unlikely((mas->mas_flags & MA_STATE_BULK))) {
1866 split = b_end - mt_min_slots[bn->type];
1868 if (!ma_is_leaf(bn->type))
1871 mas->mas_flags |= MA_STATE_REBALANCE;
1872 if (!bn->slot[split])
1878 * Although extremely rare, it is possible to enter what is known as the 3-way
1879 * split scenario. The 3-way split comes about by means of a store of a range
1880 * that overwrites the end and beginning of two full nodes. The result is a set
1881 * of entries that cannot be stored in 2 nodes. Sometimes, these two nodes can
1882 * also be located in different parent nodes which are also full. This can
1883 * carry upwards all the way to the root in the worst case.
1885 if (unlikely(mab_middle_node(bn, split, slot_count))) {
1887 *mid_split = split * 2;
1889 slot_min = mt_min_slots[bn->type];
1893 * Avoid having a range less than the slot count unless it
1894 * causes one node to be deficient.
1895 * NOTE: mt_min_slots is 1 based, b_end and split are zero.
1897 while (((bn->pivot[split] - min) < slot_count - 1) &&
1898 (split < slot_count - 1) && (b_end - split > slot_min))
1902 /* Avoid ending a node on a NULL entry */
1903 split = mab_no_null_split(bn, split, slot_count);
1905 if (unlikely(*mid_split))
1906 *mid_split = mab_no_null_split(bn, *mid_split, slot_count);
1912 * mas_mab_cp() - Copy data from a maple state inclusively to a maple_big_node
1913 * and set @b_node->b_end to the next free slot.
1914 * @mas: The maple state
1915 * @mas_start: The starting slot to copy
1916 * @mas_end: The end slot to copy (inclusively)
1917 * @b_node: The maple_big_node to place the data
1918 * @mab_start: The starting location in maple_big_node to store the data.
1920 static inline void mas_mab_cp(struct ma_state *mas, unsigned char mas_start,
1921 unsigned char mas_end, struct maple_big_node *b_node,
1922 unsigned char mab_start)
1925 struct maple_node *node;
1927 unsigned long *pivots, *gaps;
1928 int i = mas_start, j = mab_start;
1929 unsigned char piv_end;
1932 mt = mte_node_type(mas->node);
1933 pivots = ma_pivots(node, mt);
1935 b_node->pivot[j] = pivots[i++];
1936 if (unlikely(i > mas_end))
1941 piv_end = min(mas_end, mt_pivots[mt]);
1942 for (; i < piv_end; i++, j++) {
1943 b_node->pivot[j] = pivots[i];
1944 if (unlikely(!b_node->pivot[j]))
1947 if (unlikely(mas->max == b_node->pivot[j]))
1951 if (likely(i <= mas_end))
1952 b_node->pivot[j] = mas_safe_pivot(mas, pivots, i, mt);
1955 b_node->b_end = ++j;
1957 slots = ma_slots(node, mt);
1958 memcpy(b_node->slot + mab_start, slots + mas_start, sizeof(void *) * j);
1959 if (!ma_is_leaf(mt) && mt_is_alloc(mas->tree)) {
1960 gaps = ma_gaps(node, mt);
1961 memcpy(b_node->gap + mab_start, gaps + mas_start,
1962 sizeof(unsigned long) * j);
1967 * mas_leaf_set_meta() - Set the metadata of a leaf if possible.
1968 * @mas: The maple state
1969 * @node: The maple node
1970 * @pivots: pointer to the maple node pivots
1971 * @mt: The maple type
1972 * @end: The assumed end
1974 * Note, end may be incremented within this function but not modified at the
1975 * source. This is fine since the metadata is the last thing to be stored in a
1976 * node during a write.
1978 static inline void mas_leaf_set_meta(struct ma_state *mas,
1979 struct maple_node *node, unsigned long *pivots,
1980 enum maple_type mt, unsigned char end)
1982 /* There is no room for metadata already */
1983 if (mt_pivots[mt] <= end)
1986 if (pivots[end] && pivots[end] < mas->max)
1989 if (end < mt_slots[mt] - 1)
1990 ma_set_meta(node, mt, 0, end);
1994 * mab_mas_cp() - Copy data from maple_big_node to a maple encoded node.
1995 * @b_node: the maple_big_node that has the data
1996 * @mab_start: the start location in @b_node.
1997 * @mab_end: The end location in @b_node (inclusively)
1998 * @mas: The maple state with the maple encoded node.
2000 static inline void mab_mas_cp(struct maple_big_node *b_node,
2001 unsigned char mab_start, unsigned char mab_end,
2002 struct ma_state *mas, bool new_max)
2005 enum maple_type mt = mte_node_type(mas->node);
2006 struct maple_node *node = mte_to_node(mas->node);
2007 void __rcu **slots = ma_slots(node, mt);
2008 unsigned long *pivots = ma_pivots(node, mt);
2009 unsigned long *gaps = NULL;
2012 if (mab_end - mab_start > mt_pivots[mt])
2015 if (!pivots[mt_pivots[mt] - 1])
2016 slots[mt_pivots[mt]] = NULL;
2020 pivots[j++] = b_node->pivot[i++];
2021 } while (i <= mab_end && likely(b_node->pivot[i]));
2023 memcpy(slots, b_node->slot + mab_start,
2024 sizeof(void *) * (i - mab_start));
2027 mas->max = b_node->pivot[i - 1];
2030 if (likely(!ma_is_leaf(mt) && mt_is_alloc(mas->tree))) {
2031 unsigned long max_gap = 0;
2032 unsigned char offset = 15;
2034 gaps = ma_gaps(node, mt);
2036 gaps[--j] = b_node->gap[--i];
2037 if (gaps[j] > max_gap) {
2043 ma_set_meta(node, mt, offset, end);
2045 mas_leaf_set_meta(mas, node, pivots, mt, end);
2050 * mas_descend_adopt() - Descend through a sub-tree and adopt children.
2051 * @mas: the maple state with the maple encoded node of the sub-tree.
2053 * Descend through a sub-tree and adopt children who do not have the correct
2054 * parents set. Follow the parents which have the correct parents as they are
2055 * the new entries which need to be followed to find other incorrectly set
2058 static inline void mas_descend_adopt(struct ma_state *mas)
2060 struct ma_state list[3], next[3];
2064 * At each level there may be up to 3 correct parent pointers which indicates
2065 * the new nodes which need to be walked to find any new nodes at a lower level.
2068 for (i = 0; i < 3; i++) {
2075 while (!mte_is_leaf(list[0].node)) {
2077 for (i = 0; i < 3; i++) {
2078 if (mas_is_none(&list[i]))
2081 if (i && list[i-1].node == list[i].node)
2084 while ((n < 3) && (mas_new_child(&list[i], &next[n])))
2087 mas_adopt_children(&list[i], list[i].node);
2091 next[n++].node = MAS_NONE;
2093 /* descend by setting the list to the children */
2094 for (i = 0; i < 3; i++)
2100 * mas_bulk_rebalance() - Rebalance the end of a tree after a bulk insert.
2101 * @mas: The maple state
2102 * @end: The maple node end
2103 * @mt: The maple node type
2105 static inline void mas_bulk_rebalance(struct ma_state *mas, unsigned char end,
2108 if (!(mas->mas_flags & MA_STATE_BULK))
2111 if (mte_is_root(mas->node))
2114 if (end > mt_min_slots[mt]) {
2115 mas->mas_flags &= ~MA_STATE_REBALANCE;
2121 * mas_store_b_node() - Store an @entry into the b_node while also copying the
2122 * data from a maple encoded node.
2123 * @wr_mas: the maple write state
2124 * @b_node: the maple_big_node to fill with data
2125 * @offset_end: the offset to end copying
2127 * Return: The actual end of the data stored in @b_node
2129 static noinline_for_kasan void mas_store_b_node(struct ma_wr_state *wr_mas,
2130 struct maple_big_node *b_node, unsigned char offset_end)
2133 unsigned char b_end;
2134 /* Possible underflow of piv will wrap back to 0 before use. */
2136 struct ma_state *mas = wr_mas->mas;
2138 b_node->type = wr_mas->type;
2142 /* Copy start data up to insert. */
2143 mas_mab_cp(mas, 0, slot - 1, b_node, 0);
2144 b_end = b_node->b_end;
2145 piv = b_node->pivot[b_end - 1];
2149 if (piv + 1 < mas->index) {
2150 /* Handle range starting after old range */
2151 b_node->slot[b_end] = wr_mas->content;
2152 if (!wr_mas->content)
2153 b_node->gap[b_end] = mas->index - 1 - piv;
2154 b_node->pivot[b_end++] = mas->index - 1;
2157 /* Store the new entry. */
2158 mas->offset = b_end;
2159 b_node->slot[b_end] = wr_mas->entry;
2160 b_node->pivot[b_end] = mas->last;
2163 if (mas->last >= mas->max)
2166 /* Handle new range ending before old range ends */
2167 piv = mas_logical_pivot(mas, wr_mas->pivots, offset_end, wr_mas->type);
2168 if (piv > mas->last) {
2169 if (piv == ULONG_MAX)
2170 mas_bulk_rebalance(mas, b_node->b_end, wr_mas->type);
2172 if (offset_end != slot)
2173 wr_mas->content = mas_slot_locked(mas, wr_mas->slots,
2176 b_node->slot[++b_end] = wr_mas->content;
2177 if (!wr_mas->content)
2178 b_node->gap[b_end] = piv - mas->last + 1;
2179 b_node->pivot[b_end] = piv;
2182 slot = offset_end + 1;
2183 if (slot > wr_mas->node_end)
2186 /* Copy end data to the end of the node. */
2187 mas_mab_cp(mas, slot, wr_mas->node_end + 1, b_node, ++b_end);
2192 b_node->b_end = b_end;
2196 * mas_prev_sibling() - Find the previous node with the same parent.
2197 * @mas: the maple state
2199 * Return: True if there is a previous sibling, false otherwise.
2201 static inline bool mas_prev_sibling(struct ma_state *mas)
2203 unsigned int p_slot = mte_parent_slot(mas->node);
2205 if (mte_is_root(mas->node))
2212 mas->offset = p_slot - 1;
2218 * mas_next_sibling() - Find the next node with the same parent.
2219 * @mas: the maple state
2221 * Return: true if there is a next sibling, false otherwise.
2223 static inline bool mas_next_sibling(struct ma_state *mas)
2225 MA_STATE(parent, mas->tree, mas->index, mas->last);
2227 if (mte_is_root(mas->node))
2231 mas_ascend(&parent);
2232 parent.offset = mte_parent_slot(mas->node) + 1;
2233 if (parent.offset > mas_data_end(&parent))
2242 * mte_node_or_node() - Return the encoded node or MAS_NONE.
2243 * @enode: The encoded maple node.
2245 * Shorthand to avoid setting %NULLs in the tree or maple_subtree_state.
2247 * Return: @enode or MAS_NONE
2249 static inline struct maple_enode *mte_node_or_none(struct maple_enode *enode)
2254 return ma_enode_ptr(MAS_NONE);
2258 * mas_wr_node_walk() - Find the correct offset for the index in the @mas.
2259 * @wr_mas: The maple write state
2261 * Uses mas_slot_locked() and does not need to worry about dead nodes.
2263 static inline void mas_wr_node_walk(struct ma_wr_state *wr_mas)
2265 struct ma_state *mas = wr_mas->mas;
2266 unsigned char count;
2267 unsigned char offset;
2268 unsigned long index, min, max;
2270 if (unlikely(ma_is_dense(wr_mas->type))) {
2271 wr_mas->r_max = wr_mas->r_min = mas->index;
2272 mas->offset = mas->index = mas->min;
2276 wr_mas->node = mas_mn(wr_mas->mas);
2277 wr_mas->pivots = ma_pivots(wr_mas->node, wr_mas->type);
2278 count = wr_mas->node_end = ma_data_end(wr_mas->node, wr_mas->type,
2279 wr_mas->pivots, mas->max);
2280 offset = mas->offset;
2281 min = mas_safe_min(mas, wr_mas->pivots, offset);
2282 if (unlikely(offset == count))
2285 max = wr_mas->pivots[offset];
2287 if (unlikely(index <= max))
2290 if (unlikely(!max && offset))
2294 while (++offset < count) {
2295 max = wr_mas->pivots[offset];
2298 else if (unlikely(!max))
2307 wr_mas->r_max = max;
2308 wr_mas->r_min = min;
2309 wr_mas->offset_end = mas->offset = offset;
2313 * mas_topiary_range() - Add a range of slots to the topiary.
2314 * @mas: The maple state
2315 * @destroy: The topiary to add the slots (usually destroy)
2316 * @start: The starting slot inclusively
2317 * @end: The end slot inclusively
2319 static inline void mas_topiary_range(struct ma_state *mas,
2320 struct ma_topiary *destroy, unsigned char start, unsigned char end)
2323 unsigned char offset;
2325 MT_BUG_ON(mas->tree, mte_is_leaf(mas->node));
2326 slots = ma_slots(mas_mn(mas), mte_node_type(mas->node));
2327 for (offset = start; offset <= end; offset++) {
2328 struct maple_enode *enode = mas_slot_locked(mas, slots, offset);
2330 if (mte_dead_node(enode))
2333 mat_add(destroy, enode);
2338 * mast_topiary() - Add the portions of the tree to the removal list; either to
2339 * be freed or discarded (destroy walk).
2340 * @mast: The maple_subtree_state.
2342 static inline void mast_topiary(struct maple_subtree_state *mast)
2344 MA_WR_STATE(wr_mas, mast->orig_l, NULL);
2345 unsigned char r_start, r_end;
2346 unsigned char l_start, l_end;
2347 void __rcu **l_slots, **r_slots;
2349 wr_mas.type = mte_node_type(mast->orig_l->node);
2350 mast->orig_l->index = mast->orig_l->last;
2351 mas_wr_node_walk(&wr_mas);
2352 l_start = mast->orig_l->offset + 1;
2353 l_end = mas_data_end(mast->orig_l);
2355 r_end = mast->orig_r->offset;
2360 l_slots = ma_slots(mas_mn(mast->orig_l),
2361 mte_node_type(mast->orig_l->node));
2363 r_slots = ma_slots(mas_mn(mast->orig_r),
2364 mte_node_type(mast->orig_r->node));
2366 if ((l_start < l_end) &&
2367 mte_dead_node(mas_slot_locked(mast->orig_l, l_slots, l_start))) {
2371 if (mte_dead_node(mas_slot_locked(mast->orig_r, r_slots, r_end))) {
2376 if ((l_start > r_end) && (mast->orig_l->node == mast->orig_r->node))
2379 /* At the node where left and right sides meet, add the parts between */
2380 if (mast->orig_l->node == mast->orig_r->node) {
2381 return mas_topiary_range(mast->orig_l, mast->destroy,
2385 /* mast->orig_r is different and consumed. */
2386 if (mte_is_leaf(mast->orig_r->node))
2389 if (mte_dead_node(mas_slot_locked(mast->orig_l, l_slots, l_end)))
2393 if (l_start <= l_end)
2394 mas_topiary_range(mast->orig_l, mast->destroy, l_start, l_end);
2396 if (mte_dead_node(mas_slot_locked(mast->orig_r, r_slots, r_start)))
2399 if (r_start <= r_end)
2400 mas_topiary_range(mast->orig_r, mast->destroy, 0, r_end);
2404 * mast_rebalance_next() - Rebalance against the next node
2405 * @mast: The maple subtree state
2406 * @old_r: The encoded maple node to the right (next node).
2408 static inline void mast_rebalance_next(struct maple_subtree_state *mast)
2410 unsigned char b_end = mast->bn->b_end;
2412 mas_mab_cp(mast->orig_r, 0, mt_slot_count(mast->orig_r->node),
2414 mast->orig_r->last = mast->orig_r->max;
2418 * mast_rebalance_prev() - Rebalance against the previous node
2419 * @mast: The maple subtree state
2420 * @old_l: The encoded maple node to the left (previous node)
2422 static inline void mast_rebalance_prev(struct maple_subtree_state *mast)
2424 unsigned char end = mas_data_end(mast->orig_l) + 1;
2425 unsigned char b_end = mast->bn->b_end;
2427 mab_shift_right(mast->bn, end);
2428 mas_mab_cp(mast->orig_l, 0, end - 1, mast->bn, 0);
2429 mast->l->min = mast->orig_l->min;
2430 mast->orig_l->index = mast->orig_l->min;
2431 mast->bn->b_end = end + b_end;
2432 mast->l->offset += end;
2436 * mast_spanning_rebalance() - Rebalance nodes with nearest neighbour favouring
2437 * the node to the right. Checking the nodes to the right then the left at each
2438 * level upwards until root is reached. Free and destroy as needed.
2439 * Data is copied into the @mast->bn.
2440 * @mast: The maple_subtree_state.
2443 bool mast_spanning_rebalance(struct maple_subtree_state *mast)
2445 struct ma_state r_tmp = *mast->orig_r;
2446 struct ma_state l_tmp = *mast->orig_l;
2447 struct maple_enode *ancestor = NULL;
2448 unsigned char start, end;
2449 unsigned char depth = 0;
2451 r_tmp = *mast->orig_r;
2452 l_tmp = *mast->orig_l;
2454 mas_ascend(mast->orig_r);
2455 mas_ascend(mast->orig_l);
2458 (mast->orig_r->node == mast->orig_l->node)) {
2459 ancestor = mast->orig_r->node;
2460 end = mast->orig_r->offset - 1;
2461 start = mast->orig_l->offset + 1;
2464 if (mast->orig_r->offset < mas_data_end(mast->orig_r)) {
2466 ancestor = mast->orig_r->node;
2470 mast->orig_r->offset++;
2472 mas_descend(mast->orig_r);
2473 mast->orig_r->offset = 0;
2477 mast_rebalance_next(mast);
2479 unsigned char l_off = 0;
2480 struct maple_enode *child = r_tmp.node;
2483 if (ancestor == r_tmp.node)
2489 if (l_off < r_tmp.offset)
2490 mas_topiary_range(&r_tmp, mast->destroy,
2491 l_off, r_tmp.offset);
2493 if (l_tmp.node != child)
2494 mat_add(mast->free, child);
2496 } while (r_tmp.node != ancestor);
2498 *mast->orig_l = l_tmp;
2501 } else if (mast->orig_l->offset != 0) {
2503 ancestor = mast->orig_l->node;
2504 end = mas_data_end(mast->orig_l);
2507 mast->orig_l->offset--;
2509 mas_descend(mast->orig_l);
2510 mast->orig_l->offset =
2511 mas_data_end(mast->orig_l);
2515 mast_rebalance_prev(mast);
2517 unsigned char r_off;
2518 struct maple_enode *child = l_tmp.node;
2521 if (ancestor == l_tmp.node)
2524 r_off = mas_data_end(&l_tmp);
2526 if (l_tmp.offset < r_off)
2529 if (l_tmp.offset < r_off)
2530 mas_topiary_range(&l_tmp, mast->destroy,
2531 l_tmp.offset, r_off);
2533 if (r_tmp.node != child)
2534 mat_add(mast->free, child);
2536 } while (l_tmp.node != ancestor);
2538 *mast->orig_r = r_tmp;
2541 } while (!mte_is_root(mast->orig_r->node));
2543 *mast->orig_r = r_tmp;
2544 *mast->orig_l = l_tmp;
2549 * mast_ascend_free() - Add current original maple state nodes to the free list
2551 * @mast: the maple subtree state.
2553 * Ascend the original left and right sides and add the previous nodes to the
2554 * free list. Set the slots to point to the correct location in the new nodes.
2557 mast_ascend_free(struct maple_subtree_state *mast)
2559 MA_WR_STATE(wr_mas, mast->orig_r, NULL);
2560 struct maple_enode *left = mast->orig_l->node;
2561 struct maple_enode *right = mast->orig_r->node;
2563 mas_ascend(mast->orig_l);
2564 mas_ascend(mast->orig_r);
2565 mat_add(mast->free, left);
2568 mat_add(mast->free, right);
2570 mast->orig_r->offset = 0;
2571 mast->orig_r->index = mast->r->max;
2572 /* last should be larger than or equal to index */
2573 if (mast->orig_r->last < mast->orig_r->index)
2574 mast->orig_r->last = mast->orig_r->index;
2576 * The node may not contain the value so set slot to ensure all
2577 * of the nodes contents are freed or destroyed.
2579 wr_mas.type = mte_node_type(mast->orig_r->node);
2580 mas_wr_node_walk(&wr_mas);
2581 /* Set up the left side of things */
2582 mast->orig_l->offset = 0;
2583 mast->orig_l->index = mast->l->min;
2584 wr_mas.mas = mast->orig_l;
2585 wr_mas.type = mte_node_type(mast->orig_l->node);
2586 mas_wr_node_walk(&wr_mas);
2588 mast->bn->type = wr_mas.type;
2592 * mas_new_ma_node() - Create and return a new maple node. Helper function.
2593 * @mas: the maple state with the allocations.
2594 * @b_node: the maple_big_node with the type encoding.
2596 * Use the node type from the maple_big_node to allocate a new node from the
2597 * ma_state. This function exists mainly for code readability.
2599 * Return: A new maple encoded node
2601 static inline struct maple_enode
2602 *mas_new_ma_node(struct ma_state *mas, struct maple_big_node *b_node)
2604 return mt_mk_node(ma_mnode_ptr(mas_pop_node(mas)), b_node->type);
2608 * mas_mab_to_node() - Set up right and middle nodes
2610 * @mas: the maple state that contains the allocations.
2611 * @b_node: the node which contains the data.
2612 * @left: The pointer which will have the left node
2613 * @right: The pointer which may have the right node
2614 * @middle: the pointer which may have the middle node (rare)
2615 * @mid_split: the split location for the middle node
2617 * Return: the split of left.
2619 static inline unsigned char mas_mab_to_node(struct ma_state *mas,
2620 struct maple_big_node *b_node, struct maple_enode **left,
2621 struct maple_enode **right, struct maple_enode **middle,
2622 unsigned char *mid_split, unsigned long min)
2624 unsigned char split = 0;
2625 unsigned char slot_count = mt_slots[b_node->type];
2627 *left = mas_new_ma_node(mas, b_node);
2632 if (b_node->b_end < slot_count) {
2633 split = b_node->b_end;
2635 split = mab_calc_split(mas, b_node, mid_split, min);
2636 *right = mas_new_ma_node(mas, b_node);
2640 *middle = mas_new_ma_node(mas, b_node);
2647 * mab_set_b_end() - Add entry to b_node at b_node->b_end and increment the end
2649 * @b_node - the big node to add the entry
2650 * @mas - the maple state to get the pivot (mas->max)
2651 * @entry - the entry to add, if NULL nothing happens.
2653 static inline void mab_set_b_end(struct maple_big_node *b_node,
2654 struct ma_state *mas,
2660 b_node->slot[b_node->b_end] = entry;
2661 if (mt_is_alloc(mas->tree))
2662 b_node->gap[b_node->b_end] = mas_max_gap(mas);
2663 b_node->pivot[b_node->b_end++] = mas->max;
2667 * mas_set_split_parent() - combine_then_separate helper function. Sets the parent
2668 * of @mas->node to either @left or @right, depending on @slot and @split
2670 * @mas - the maple state with the node that needs a parent
2671 * @left - possible parent 1
2672 * @right - possible parent 2
2673 * @slot - the slot the mas->node was placed
2674 * @split - the split location between @left and @right
2676 static inline void mas_set_split_parent(struct ma_state *mas,
2677 struct maple_enode *left,
2678 struct maple_enode *right,
2679 unsigned char *slot, unsigned char split)
2681 if (mas_is_none(mas))
2684 if ((*slot) <= split)
2685 mte_set_parent(mas->node, left, *slot);
2687 mte_set_parent(mas->node, right, (*slot) - split - 1);
2693 * mte_mid_split_check() - Check if the next node passes the mid-split
2694 * @**l: Pointer to left encoded maple node.
2695 * @**m: Pointer to middle encoded maple node.
2696 * @**r: Pointer to right encoded maple node.
2698 * @*split: The split location.
2699 * @mid_split: The middle split.
2701 static inline void mte_mid_split_check(struct maple_enode **l,
2702 struct maple_enode **r,
2703 struct maple_enode *right,
2705 unsigned char *split,
2706 unsigned char mid_split)
2711 if (slot < mid_split)
2720 * mast_set_split_parents() - Helper function to set three nodes parents. Slot
2721 * is taken from @mast->l.
2722 * @mast - the maple subtree state
2723 * @left - the left node
2724 * @right - the right node
2725 * @split - the split location.
2727 static inline void mast_set_split_parents(struct maple_subtree_state *mast,
2728 struct maple_enode *left,
2729 struct maple_enode *middle,
2730 struct maple_enode *right,
2731 unsigned char split,
2732 unsigned char mid_split)
2735 struct maple_enode *l = left;
2736 struct maple_enode *r = right;
2738 if (mas_is_none(mast->l))
2744 slot = mast->l->offset;
2746 mte_mid_split_check(&l, &r, right, slot, &split, mid_split);
2747 mas_set_split_parent(mast->l, l, r, &slot, split);
2749 mte_mid_split_check(&l, &r, right, slot, &split, mid_split);
2750 mas_set_split_parent(mast->m, l, r, &slot, split);
2752 mte_mid_split_check(&l, &r, right, slot, &split, mid_split);
2753 mas_set_split_parent(mast->r, l, r, &slot, split);
2757 * mas_wmb_replace() - Write memory barrier and replace
2758 * @mas: The maple state
2759 * @free: the maple topiary list of nodes to free
2760 * @destroy: The maple topiary list of nodes to destroy (walk and free)
2762 * Updates gap as necessary.
2764 static inline void mas_wmb_replace(struct ma_state *mas,
2765 struct ma_topiary *free,
2766 struct ma_topiary *destroy)
2768 /* All nodes must see old data as dead prior to replacing that data */
2769 smp_wmb(); /* Needed for RCU */
2771 /* Insert the new data in the tree */
2772 mas_replace(mas, true);
2774 if (!mte_is_leaf(mas->node))
2775 mas_descend_adopt(mas);
2777 mas_mat_free(mas, free);
2780 mas_mat_destroy(mas, destroy);
2782 if (mte_is_leaf(mas->node))
2785 mas_update_gap(mas);
2789 * mast_new_root() - Set a new tree root during subtree creation
2790 * @mast: The maple subtree state
2791 * @mas: The maple state
2793 static inline void mast_new_root(struct maple_subtree_state *mast,
2794 struct ma_state *mas)
2796 mas_mn(mast->l)->parent =
2797 ma_parent_ptr(((unsigned long)mas->tree | MA_ROOT_PARENT));
2798 if (!mte_dead_node(mast->orig_l->node) &&
2799 !mte_is_root(mast->orig_l->node)) {
2801 mast_ascend_free(mast);
2803 } while (!mte_is_root(mast->orig_l->node));
2805 if ((mast->orig_l->node != mas->node) &&
2806 (mast->l->depth > mas_mt_height(mas))) {
2807 mat_add(mast->free, mas->node);
2812 * mast_cp_to_nodes() - Copy data out to nodes.
2813 * @mast: The maple subtree state
2814 * @left: The left encoded maple node
2815 * @middle: The middle encoded maple node
2816 * @right: The right encoded maple node
2817 * @split: The location to split between left and (middle ? middle : right)
2818 * @mid_split: The location to split between middle and right.
2820 static inline void mast_cp_to_nodes(struct maple_subtree_state *mast,
2821 struct maple_enode *left, struct maple_enode *middle,
2822 struct maple_enode *right, unsigned char split, unsigned char mid_split)
2824 bool new_lmax = true;
2826 mast->l->node = mte_node_or_none(left);
2827 mast->m->node = mte_node_or_none(middle);
2828 mast->r->node = mte_node_or_none(right);
2830 mast->l->min = mast->orig_l->min;
2831 if (split == mast->bn->b_end) {
2832 mast->l->max = mast->orig_r->max;
2836 mab_mas_cp(mast->bn, 0, split, mast->l, new_lmax);
2839 mab_mas_cp(mast->bn, 1 + split, mid_split, mast->m, true);
2840 mast->m->min = mast->bn->pivot[split] + 1;
2844 mast->r->max = mast->orig_r->max;
2846 mab_mas_cp(mast->bn, 1 + split, mast->bn->b_end, mast->r, false);
2847 mast->r->min = mast->bn->pivot[split] + 1;
2852 * mast_combine_cp_left - Copy in the original left side of the tree into the
2853 * combined data set in the maple subtree state big node.
2854 * @mast: The maple subtree state
2856 static inline void mast_combine_cp_left(struct maple_subtree_state *mast)
2858 unsigned char l_slot = mast->orig_l->offset;
2863 mas_mab_cp(mast->orig_l, 0, l_slot - 1, mast->bn, 0);
2867 * mast_combine_cp_right: Copy in the original right side of the tree into the
2868 * combined data set in the maple subtree state big node.
2869 * @mast: The maple subtree state
2871 static inline void mast_combine_cp_right(struct maple_subtree_state *mast)
2873 if (mast->bn->pivot[mast->bn->b_end - 1] >= mast->orig_r->max)
2876 mas_mab_cp(mast->orig_r, mast->orig_r->offset + 1,
2877 mt_slot_count(mast->orig_r->node), mast->bn,
2879 mast->orig_r->last = mast->orig_r->max;
2883 * mast_sufficient: Check if the maple subtree state has enough data in the big
2884 * node to create at least one sufficient node
2885 * @mast: the maple subtree state
2887 static inline bool mast_sufficient(struct maple_subtree_state *mast)
2889 if (mast->bn->b_end > mt_min_slot_count(mast->orig_l->node))
2896 * mast_overflow: Check if there is too much data in the subtree state for a
2898 * @mast: The maple subtree state
2900 static inline bool mast_overflow(struct maple_subtree_state *mast)
2902 if (mast->bn->b_end >= mt_slot_count(mast->orig_l->node))
2908 static inline void *mtree_range_walk(struct ma_state *mas)
2910 unsigned long *pivots;
2911 unsigned char offset;
2912 struct maple_node *node;
2913 struct maple_enode *next, *last;
2914 enum maple_type type;
2917 unsigned long max, min;
2918 unsigned long prev_max, prev_min;
2926 node = mte_to_node(next);
2927 type = mte_node_type(next);
2928 pivots = ma_pivots(node, type);
2929 end = ma_data_end(node, type, pivots, max);
2930 if (unlikely(ma_dead_node(node)))
2933 if (pivots[offset] >= mas->index) {
2936 max = pivots[offset];
2942 } while ((offset < end) && (pivots[offset] < mas->index));
2945 min = pivots[offset - 1] + 1;
2947 if (likely(offset < end && pivots[offset]))
2948 max = pivots[offset];
2951 slots = ma_slots(node, type);
2952 next = mt_slot(mas->tree, slots, offset);
2953 if (unlikely(ma_dead_node(node)))
2955 } while (!ma_is_leaf(type));
2957 mas->offset = offset;
2960 mas->min = prev_min;
2961 mas->max = prev_max;
2963 return (void *)next;
2971 * mas_spanning_rebalance() - Rebalance across two nodes which may not be peers.
2972 * @mas: The starting maple state
2973 * @mast: The maple_subtree_state, keeps track of 4 maple states.
2974 * @count: The estimated count of iterations needed.
2976 * Follow the tree upwards from @l_mas and @r_mas for @count, or until the root
2977 * is hit. First @b_node is split into two entries which are inserted into the
2978 * next iteration of the loop. @b_node is returned populated with the final
2979 * iteration. @mas is used to obtain allocations. orig_l_mas keeps track of the
2980 * nodes that will remain active by using orig_l_mas->index and orig_l_mas->last
2981 * to account of what has been copied into the new sub-tree. The update of
2982 * orig_l_mas->last is used in mas_consume to find the slots that will need to
2983 * be either freed or destroyed. orig_l_mas->depth keeps track of the height of
2984 * the new sub-tree in case the sub-tree becomes the full tree.
2986 * Return: the number of elements in b_node during the last loop.
2988 static int mas_spanning_rebalance(struct ma_state *mas,
2989 struct maple_subtree_state *mast, unsigned char count)
2991 unsigned char split, mid_split;
2992 unsigned char slot = 0;
2993 struct maple_enode *left = NULL, *middle = NULL, *right = NULL;
2995 MA_STATE(l_mas, mas->tree, mas->index, mas->index);
2996 MA_STATE(r_mas, mas->tree, mas->index, mas->last);
2997 MA_STATE(m_mas, mas->tree, mas->index, mas->index);
2998 MA_TOPIARY(free, mas->tree);
2999 MA_TOPIARY(destroy, mas->tree);
3002 * The tree needs to be rebalanced and leaves need to be kept at the same level.
3003 * Rebalancing is done by use of the ``struct maple_topiary``.
3009 mast->destroy = &destroy;
3010 l_mas.node = r_mas.node = m_mas.node = MAS_NONE;
3012 /* Check if this is not root and has sufficient data. */
3013 if (((mast->orig_l->min != 0) || (mast->orig_r->max != ULONG_MAX)) &&
3014 unlikely(mast->bn->b_end <= mt_min_slots[mast->bn->type]))
3015 mast_spanning_rebalance(mast);
3017 mast->orig_l->depth = 0;
3020 * Each level of the tree is examined and balanced, pushing data to the left or
3021 * right, or rebalancing against left or right nodes is employed to avoid
3022 * rippling up the tree to limit the amount of churn. Once a new sub-section of
3023 * the tree is created, there may be a mix of new and old nodes. The old nodes
3024 * will have the incorrect parent pointers and currently be in two trees: the
3025 * original tree and the partially new tree. To remedy the parent pointers in
3026 * the old tree, the new data is swapped into the active tree and a walk down
3027 * the tree is performed and the parent pointers are updated.
3028 * See mas_descend_adopt() for more information..
3032 mast->bn->type = mte_node_type(mast->orig_l->node);
3033 split = mas_mab_to_node(mas, mast->bn, &left, &right, &middle,
3034 &mid_split, mast->orig_l->min);
3035 mast_set_split_parents(mast, left, middle, right, split,
3037 mast_cp_to_nodes(mast, left, middle, right, split, mid_split);
3040 * Copy data from next level in the tree to mast->bn from next
3043 memset(mast->bn, 0, sizeof(struct maple_big_node));
3044 mast->bn->type = mte_node_type(left);
3045 mast->orig_l->depth++;
3047 /* Root already stored in l->node. */
3048 if (mas_is_root_limits(mast->l))
3051 mast_ascend_free(mast);
3052 mast_combine_cp_left(mast);
3053 l_mas.offset = mast->bn->b_end;
3054 mab_set_b_end(mast->bn, &l_mas, left);
3055 mab_set_b_end(mast->bn, &m_mas, middle);
3056 mab_set_b_end(mast->bn, &r_mas, right);
3058 /* Copy anything necessary out of the right node. */
3059 mast_combine_cp_right(mast);
3061 mast->orig_l->last = mast->orig_l->max;
3063 if (mast_sufficient(mast))
3066 if (mast_overflow(mast))
3069 /* May be a new root stored in mast->bn */
3070 if (mas_is_root_limits(mast->orig_l))
3073 mast_spanning_rebalance(mast);
3075 /* rebalancing from other nodes may require another loop. */
3080 l_mas.node = mt_mk_node(ma_mnode_ptr(mas_pop_node(mas)),
3081 mte_node_type(mast->orig_l->node));
3082 mast->orig_l->depth++;
3083 mab_mas_cp(mast->bn, 0, mt_slots[mast->bn->type] - 1, &l_mas, true);
3084 mte_set_parent(left, l_mas.node, slot);
3086 mte_set_parent(middle, l_mas.node, ++slot);
3089 mte_set_parent(right, l_mas.node, ++slot);
3091 if (mas_is_root_limits(mast->l)) {
3093 mast_new_root(mast, mas);
3095 mas_mn(&l_mas)->parent = mas_mn(mast->orig_l)->parent;
3098 if (!mte_dead_node(mast->orig_l->node))
3099 mat_add(&free, mast->orig_l->node);
3101 mas->depth = mast->orig_l->depth;
3102 *mast->orig_l = l_mas;
3103 mte_set_node_dead(mas->node);
3105 /* Set up mas for insertion. */
3106 mast->orig_l->depth = mas->depth;
3107 mast->orig_l->alloc = mas->alloc;
3108 *mas = *mast->orig_l;
3109 mas_wmb_replace(mas, &free, &destroy);
3110 mtree_range_walk(mas);
3111 return mast->bn->b_end;
3115 * mas_rebalance() - Rebalance a given node.
3116 * @mas: The maple state
3117 * @b_node: The big maple node.
3119 * Rebalance two nodes into a single node or two new nodes that are sufficient.
3120 * Continue upwards until tree is sufficient.
3122 * Return: the number of elements in b_node during the last loop.
3124 static inline int mas_rebalance(struct ma_state *mas,
3125 struct maple_big_node *b_node)
3127 char empty_count = mas_mt_height(mas);
3128 struct maple_subtree_state mast;
3129 unsigned char shift, b_end = ++b_node->b_end;
3131 MA_STATE(l_mas, mas->tree, mas->index, mas->last);
3132 MA_STATE(r_mas, mas->tree, mas->index, mas->last);
3134 trace_ma_op(__func__, mas);
3137 * Rebalancing occurs if a node is insufficient. Data is rebalanced
3138 * against the node to the right if it exists, otherwise the node to the
3139 * left of this node is rebalanced against this node. If rebalancing
3140 * causes just one node to be produced instead of two, then the parent
3141 * is also examined and rebalanced if it is insufficient. Every level
3142 * tries to combine the data in the same way. If one node contains the
3143 * entire range of the tree, then that node is used as a new root node.
3145 mas_node_count(mas, 1 + empty_count * 3);
3146 if (mas_is_err(mas))
3149 mast.orig_l = &l_mas;
3150 mast.orig_r = &r_mas;
3152 mast.bn->type = mte_node_type(mas->node);
3154 l_mas = r_mas = *mas;
3156 if (mas_next_sibling(&r_mas)) {
3157 mas_mab_cp(&r_mas, 0, mt_slot_count(r_mas.node), b_node, b_end);
3158 r_mas.last = r_mas.index = r_mas.max;
3160 mas_prev_sibling(&l_mas);
3161 shift = mas_data_end(&l_mas) + 1;
3162 mab_shift_right(b_node, shift);
3163 mas->offset += shift;
3164 mas_mab_cp(&l_mas, 0, shift - 1, b_node, 0);
3165 b_node->b_end = shift + b_end;
3166 l_mas.index = l_mas.last = l_mas.min;
3169 return mas_spanning_rebalance(mas, &mast, empty_count);
3173 * mas_destroy_rebalance() - Rebalance left-most node while destroying the maple
3175 * @mas: The maple state
3176 * @end: The end of the left-most node.
3178 * During a mass-insert event (such as forking), it may be necessary to
3179 * rebalance the left-most node when it is not sufficient.
3181 static inline void mas_destroy_rebalance(struct ma_state *mas, unsigned char end)
3183 enum maple_type mt = mte_node_type(mas->node);
3184 struct maple_node reuse, *newnode, *parent, *new_left, *left, *node;
3185 struct maple_enode *eparent;
3186 unsigned char offset, tmp, split = mt_slots[mt] / 2;
3187 void __rcu **l_slots, **slots;
3188 unsigned long *l_pivs, *pivs, gap;
3189 bool in_rcu = mt_in_rcu(mas->tree);
3191 MA_STATE(l_mas, mas->tree, mas->index, mas->last);
3194 mas_prev_sibling(&l_mas);
3198 /* Allocate for both left and right as well as parent. */
3199 mas_node_count(mas, 3);
3200 if (mas_is_err(mas))
3203 newnode = mas_pop_node(mas);
3209 newnode->parent = node->parent;
3210 slots = ma_slots(newnode, mt);
3211 pivs = ma_pivots(newnode, mt);
3212 left = mas_mn(&l_mas);
3213 l_slots = ma_slots(left, mt);
3214 l_pivs = ma_pivots(left, mt);
3215 if (!l_slots[split])
3217 tmp = mas_data_end(&l_mas) - split;
3219 memcpy(slots, l_slots + split + 1, sizeof(void *) * tmp);
3220 memcpy(pivs, l_pivs + split + 1, sizeof(unsigned long) * tmp);
3221 pivs[tmp] = l_mas.max;
3222 memcpy(slots + tmp, ma_slots(node, mt), sizeof(void *) * end);
3223 memcpy(pivs + tmp, ma_pivots(node, mt), sizeof(unsigned long) * end);
3225 l_mas.max = l_pivs[split];
3226 mas->min = l_mas.max + 1;
3227 eparent = mt_mk_node(mte_parent(l_mas.node),
3228 mas_parent_enum(&l_mas, l_mas.node));
3231 unsigned char max_p = mt_pivots[mt];
3232 unsigned char max_s = mt_slots[mt];
3235 memset(pivs + tmp, 0,
3236 sizeof(unsigned long *) * (max_p - tmp));
3238 if (tmp < mt_slots[mt])
3239 memset(slots + tmp, 0, sizeof(void *) * (max_s - tmp));
3241 memcpy(node, newnode, sizeof(struct maple_node));
3242 ma_set_meta(node, mt, 0, tmp - 1);
3243 mte_set_pivot(eparent, mte_parent_slot(l_mas.node),
3246 /* Remove data from l_pivs. */
3248 memset(l_pivs + tmp, 0, sizeof(unsigned long) * (max_p - tmp));
3249 memset(l_slots + tmp, 0, sizeof(void *) * (max_s - tmp));
3250 ma_set_meta(left, mt, 0, split);
3255 /* RCU requires replacing both l_mas, mas, and parent. */
3256 mas->node = mt_mk_node(newnode, mt);
3257 ma_set_meta(newnode, mt, 0, tmp);
3259 new_left = mas_pop_node(mas);
3260 new_left->parent = left->parent;
3261 mt = mte_node_type(l_mas.node);
3262 slots = ma_slots(new_left, mt);
3263 pivs = ma_pivots(new_left, mt);
3264 memcpy(slots, l_slots, sizeof(void *) * split);
3265 memcpy(pivs, l_pivs, sizeof(unsigned long) * split);
3266 ma_set_meta(new_left, mt, 0, split);
3267 l_mas.node = mt_mk_node(new_left, mt);
3269 /* replace parent. */
3270 offset = mte_parent_slot(mas->node);
3271 mt = mas_parent_enum(&l_mas, l_mas.node);
3272 parent = mas_pop_node(mas);
3273 slots = ma_slots(parent, mt);
3274 pivs = ma_pivots(parent, mt);
3275 memcpy(parent, mte_to_node(eparent), sizeof(struct maple_node));
3276 rcu_assign_pointer(slots[offset], mas->node);
3277 rcu_assign_pointer(slots[offset - 1], l_mas.node);
3278 pivs[offset - 1] = l_mas.max;
3279 eparent = mt_mk_node(parent, mt);
3281 gap = mas_leaf_max_gap(mas);
3282 mte_set_gap(eparent, mte_parent_slot(mas->node), gap);
3283 gap = mas_leaf_max_gap(&l_mas);
3284 mte_set_gap(eparent, mte_parent_slot(l_mas.node), gap);
3288 mas_replace(mas, false);
3290 mas_update_gap(mas);
3294 * mas_split_final_node() - Split the final node in a subtree operation.
3295 * @mast: the maple subtree state
3296 * @mas: The maple state
3297 * @height: The height of the tree in case it's a new root.
3299 static inline bool mas_split_final_node(struct maple_subtree_state *mast,
3300 struct ma_state *mas, int height)
3302 struct maple_enode *ancestor;
3304 if (mte_is_root(mas->node)) {
3305 if (mt_is_alloc(mas->tree))
3306 mast->bn->type = maple_arange_64;
3308 mast->bn->type = maple_range_64;
3309 mas->depth = height;
3312 * Only a single node is used here, could be root.
3313 * The Big_node data should just fit in a single node.
3315 ancestor = mas_new_ma_node(mas, mast->bn);
3316 mte_set_parent(mast->l->node, ancestor, mast->l->offset);
3317 mte_set_parent(mast->r->node, ancestor, mast->r->offset);
3318 mte_to_node(ancestor)->parent = mas_mn(mas)->parent;
3320 mast->l->node = ancestor;
3321 mab_mas_cp(mast->bn, 0, mt_slots[mast->bn->type] - 1, mast->l, true);
3322 mas->offset = mast->bn->b_end - 1;
3327 * mast_fill_bnode() - Copy data into the big node in the subtree state
3328 * @mast: The maple subtree state
3329 * @mas: the maple state
3330 * @skip: The number of entries to skip for new nodes insertion.
3332 static inline void mast_fill_bnode(struct maple_subtree_state *mast,
3333 struct ma_state *mas,
3337 struct maple_enode *old = mas->node;
3338 unsigned char split;
3340 memset(mast->bn->gap, 0, sizeof(unsigned long) * ARRAY_SIZE(mast->bn->gap));
3341 memset(mast->bn->slot, 0, sizeof(unsigned long) * ARRAY_SIZE(mast->bn->slot));
3342 memset(mast->bn->pivot, 0, sizeof(unsigned long) * ARRAY_SIZE(mast->bn->pivot));
3343 mast->bn->b_end = 0;
3345 if (mte_is_root(mas->node)) {
3349 mat_add(mast->free, old);
3350 mas->offset = mte_parent_slot(mas->node);
3353 if (cp && mast->l->offset)
3354 mas_mab_cp(mas, 0, mast->l->offset - 1, mast->bn, 0);
3356 split = mast->bn->b_end;
3357 mab_set_b_end(mast->bn, mast->l, mast->l->node);
3358 mast->r->offset = mast->bn->b_end;
3359 mab_set_b_end(mast->bn, mast->r, mast->r->node);
3360 if (mast->bn->pivot[mast->bn->b_end - 1] == mas->max)
3364 mas_mab_cp(mas, split + skip, mt_slot_count(mas->node) - 1,
3365 mast->bn, mast->bn->b_end);
3368 mast->bn->type = mte_node_type(mas->node);
3372 * mast_split_data() - Split the data in the subtree state big node into regular
3374 * @mast: The maple subtree state
3375 * @mas: The maple state
3376 * @split: The location to split the big node
3378 static inline void mast_split_data(struct maple_subtree_state *mast,
3379 struct ma_state *mas, unsigned char split)
3381 unsigned char p_slot;
3383 mab_mas_cp(mast->bn, 0, split, mast->l, true);
3384 mte_set_pivot(mast->r->node, 0, mast->r->max);
3385 mab_mas_cp(mast->bn, split + 1, mast->bn->b_end, mast->r, false);
3386 mast->l->offset = mte_parent_slot(mas->node);
3387 mast->l->max = mast->bn->pivot[split];
3388 mast->r->min = mast->l->max + 1;
3389 if (mte_is_leaf(mas->node))
3392 p_slot = mast->orig_l->offset;
3393 mas_set_split_parent(mast->orig_l, mast->l->node, mast->r->node,
3395 mas_set_split_parent(mast->orig_r, mast->l->node, mast->r->node,
3400 * mas_push_data() - Instead of splitting a node, it is beneficial to push the
3401 * data to the right or left node if there is room.
3402 * @mas: The maple state
3403 * @height: The current height of the maple state
3404 * @mast: The maple subtree state
3405 * @left: Push left or not.
3407 * Keeping the height of the tree low means faster lookups.
3409 * Return: True if pushed, false otherwise.
3411 static inline bool mas_push_data(struct ma_state *mas, int height,
3412 struct maple_subtree_state *mast, bool left)
3414 unsigned char slot_total = mast->bn->b_end;
3415 unsigned char end, space, split;
3417 MA_STATE(tmp_mas, mas->tree, mas->index, mas->last);
3419 tmp_mas.depth = mast->l->depth;
3421 if (left && !mas_prev_sibling(&tmp_mas))
3423 else if (!left && !mas_next_sibling(&tmp_mas))
3426 end = mas_data_end(&tmp_mas);
3428 space = 2 * mt_slot_count(mas->node) - 2;
3429 /* -2 instead of -1 to ensure there isn't a triple split */
3430 if (ma_is_leaf(mast->bn->type))
3433 if (mas->max == ULONG_MAX)
3436 if (slot_total >= space)
3439 /* Get the data; Fill mast->bn */
3442 mab_shift_right(mast->bn, end + 1);
3443 mas_mab_cp(&tmp_mas, 0, end, mast->bn, 0);
3444 mast->bn->b_end = slot_total + 1;
3446 mas_mab_cp(&tmp_mas, 0, end, mast->bn, mast->bn->b_end);
3449 /* Configure mast for splitting of mast->bn */
3450 split = mt_slots[mast->bn->type] - 2;
3452 /* Switch mas to prev node */
3453 mat_add(mast->free, mas->node);
3455 /* Start using mast->l for the left side. */
3456 tmp_mas.node = mast->l->node;
3459 mat_add(mast->free, tmp_mas.node);
3460 tmp_mas.node = mast->r->node;
3462 split = slot_total - split;
3464 split = mab_no_null_split(mast->bn, split, mt_slots[mast->bn->type]);
3465 /* Update parent slot for split calculation. */
3467 mast->orig_l->offset += end + 1;
3469 mast_split_data(mast, mas, split);
3470 mast_fill_bnode(mast, mas, 2);
3471 mas_split_final_node(mast, mas, height + 1);
3476 * mas_split() - Split data that is too big for one node into two.
3477 * @mas: The maple state
3478 * @b_node: The maple big node
3479 * Return: 1 on success, 0 on failure.
3481 static int mas_split(struct ma_state *mas, struct maple_big_node *b_node)
3483 struct maple_subtree_state mast;
3485 unsigned char mid_split, split = 0;
3488 * Splitting is handled differently from any other B-tree; the Maple
3489 * Tree splits upwards. Splitting up means that the split operation
3490 * occurs when the walk of the tree hits the leaves and not on the way
3491 * down. The reason for splitting up is that it is impossible to know
3492 * how much space will be needed until the leaf is (or leaves are)
3493 * reached. Since overwriting data is allowed and a range could
3494 * overwrite more than one range or result in changing one entry into 3
3495 * entries, it is impossible to know if a split is required until the
3498 * Splitting is a balancing act between keeping allocations to a minimum
3499 * and avoiding a 'jitter' event where a tree is expanded to make room
3500 * for an entry followed by a contraction when the entry is removed. To
3501 * accomplish the balance, there are empty slots remaining in both left
3502 * and right nodes after a split.
3504 MA_STATE(l_mas, mas->tree, mas->index, mas->last);
3505 MA_STATE(r_mas, mas->tree, mas->index, mas->last);
3506 MA_STATE(prev_l_mas, mas->tree, mas->index, mas->last);
3507 MA_STATE(prev_r_mas, mas->tree, mas->index, mas->last);
3508 MA_TOPIARY(mat, mas->tree);
3510 trace_ma_op(__func__, mas);
3511 mas->depth = mas_mt_height(mas);
3512 /* Allocation failures will happen early. */
3513 mas_node_count(mas, 1 + mas->depth * 2);
3514 if (mas_is_err(mas))
3519 mast.orig_l = &prev_l_mas;
3520 mast.orig_r = &prev_r_mas;
3524 while (height++ <= mas->depth) {
3525 if (mt_slots[b_node->type] > b_node->b_end) {
3526 mas_split_final_node(&mast, mas, height);
3530 l_mas = r_mas = *mas;
3531 l_mas.node = mas_new_ma_node(mas, b_node);
3532 r_mas.node = mas_new_ma_node(mas, b_node);
3534 * Another way that 'jitter' is avoided is to terminate a split up early if the
3535 * left or right node has space to spare. This is referred to as "pushing left"
3536 * or "pushing right" and is similar to the B* tree, except the nodes left or
3537 * right can rarely be reused due to RCU, but the ripple upwards is halted which
3538 * is a significant savings.
3540 /* Try to push left. */
3541 if (mas_push_data(mas, height, &mast, true))
3544 /* Try to push right. */
3545 if (mas_push_data(mas, height, &mast, false))
3548 split = mab_calc_split(mas, b_node, &mid_split, prev_l_mas.min);
3549 mast_split_data(&mast, mas, split);
3551 * Usually correct, mab_mas_cp in the above call overwrites
3554 mast.r->max = mas->max;
3555 mast_fill_bnode(&mast, mas, 1);
3556 prev_l_mas = *mast.l;
3557 prev_r_mas = *mast.r;
3560 /* Set the original node as dead */
3561 mat_add(mast.free, mas->node);
3562 mas->node = l_mas.node;
3563 mas_wmb_replace(mas, mast.free, NULL);
3564 mtree_range_walk(mas);
3569 * mas_reuse_node() - Reuse the node to store the data.
3570 * @wr_mas: The maple write state
3571 * @bn: The maple big node
3572 * @end: The end of the data.
3574 * Will always return false in RCU mode.
3576 * Return: True if node was reused, false otherwise.
3578 static inline bool mas_reuse_node(struct ma_wr_state *wr_mas,
3579 struct maple_big_node *bn, unsigned char end)
3581 /* Need to be rcu safe. */
3582 if (mt_in_rcu(wr_mas->mas->tree))
3585 if (end > bn->b_end) {
3586 int clear = mt_slots[wr_mas->type] - bn->b_end;
3588 memset(wr_mas->slots + bn->b_end, 0, sizeof(void *) * clear--);
3589 memset(wr_mas->pivots + bn->b_end, 0, sizeof(void *) * clear);
3591 mab_mas_cp(bn, 0, bn->b_end, wr_mas->mas, false);
3596 * mas_commit_b_node() - Commit the big node into the tree.
3597 * @wr_mas: The maple write state
3598 * @b_node: The maple big node
3599 * @end: The end of the data.
3601 static noinline_for_kasan int mas_commit_b_node(struct ma_wr_state *wr_mas,
3602 struct maple_big_node *b_node, unsigned char end)
3604 struct maple_node *node;
3605 unsigned char b_end = b_node->b_end;
3606 enum maple_type b_type = b_node->type;
3608 if ((b_end < mt_min_slots[b_type]) &&
3609 (!mte_is_root(wr_mas->mas->node)) &&
3610 (mas_mt_height(wr_mas->mas) > 1))
3611 return mas_rebalance(wr_mas->mas, b_node);
3613 if (b_end >= mt_slots[b_type])
3614 return mas_split(wr_mas->mas, b_node);
3616 if (mas_reuse_node(wr_mas, b_node, end))
3619 mas_node_count(wr_mas->mas, 1);
3620 if (mas_is_err(wr_mas->mas))
3623 node = mas_pop_node(wr_mas->mas);
3624 node->parent = mas_mn(wr_mas->mas)->parent;
3625 wr_mas->mas->node = mt_mk_node(node, b_type);
3626 mab_mas_cp(b_node, 0, b_end, wr_mas->mas, false);
3627 mas_replace(wr_mas->mas, false);
3629 mas_update_gap(wr_mas->mas);
3634 * mas_root_expand() - Expand a root to a node
3635 * @mas: The maple state
3636 * @entry: The entry to store into the tree
3638 static inline int mas_root_expand(struct ma_state *mas, void *entry)
3640 void *contents = mas_root_locked(mas);
3641 enum maple_type type = maple_leaf_64;
3642 struct maple_node *node;
3644 unsigned long *pivots;
3647 mas_node_count(mas, 1);
3648 if (unlikely(mas_is_err(mas)))
3651 node = mas_pop_node(mas);
3652 pivots = ma_pivots(node, type);
3653 slots = ma_slots(node, type);
3654 node->parent = ma_parent_ptr(
3655 ((unsigned long)mas->tree | MA_ROOT_PARENT));
3656 mas->node = mt_mk_node(node, type);
3660 rcu_assign_pointer(slots[slot], contents);
3661 if (likely(mas->index > 1))
3664 pivots[slot++] = mas->index - 1;
3667 rcu_assign_pointer(slots[slot], entry);
3669 pivots[slot] = mas->last;
3670 if (mas->last != ULONG_MAX)
3673 mas_set_height(mas);
3675 /* swap the new root into the tree */
3676 rcu_assign_pointer(mas->tree->ma_root, mte_mk_root(mas->node));
3677 ma_set_meta(node, maple_leaf_64, 0, slot);
3681 static inline void mas_store_root(struct ma_state *mas, void *entry)
3683 if (likely((mas->last != 0) || (mas->index != 0)))
3684 mas_root_expand(mas, entry);
3685 else if (((unsigned long) (entry) & 3) == 2)
3686 mas_root_expand(mas, entry);
3688 rcu_assign_pointer(mas->tree->ma_root, entry);
3689 mas->node = MAS_START;
3694 * mas_is_span_wr() - Check if the write needs to be treated as a write that
3696 * @mas: The maple state
3697 * @piv: The pivot value being written
3698 * @type: The maple node type
3699 * @entry: The data to write
3701 * Spanning writes are writes that start in one node and end in another OR if
3702 * the write of a %NULL will cause the node to end with a %NULL.
3704 * Return: True if this is a spanning write, false otherwise.
3706 static bool mas_is_span_wr(struct ma_wr_state *wr_mas)
3709 unsigned long last = wr_mas->mas->last;
3710 unsigned long piv = wr_mas->r_max;
3711 enum maple_type type = wr_mas->type;
3712 void *entry = wr_mas->entry;
3714 /* Contained in this pivot */
3718 max = wr_mas->mas->max;
3719 if (unlikely(ma_is_leaf(type))) {
3720 /* Fits in the node, but may span slots. */
3724 /* Writes to the end of the node but not null. */
3725 if ((last == max) && entry)
3729 * Writing ULONG_MAX is not a spanning write regardless of the
3730 * value being written as long as the range fits in the node.
3732 if ((last == ULONG_MAX) && (last == max))
3734 } else if (piv == last) {
3738 /* Detect spanning store wr walk */
3739 if (last == ULONG_MAX)
3743 trace_ma_write(__func__, wr_mas->mas, piv, entry);
3748 static inline void mas_wr_walk_descend(struct ma_wr_state *wr_mas)
3750 wr_mas->type = mte_node_type(wr_mas->mas->node);
3751 mas_wr_node_walk(wr_mas);
3752 wr_mas->slots = ma_slots(wr_mas->node, wr_mas->type);
3755 static inline void mas_wr_walk_traverse(struct ma_wr_state *wr_mas)
3757 wr_mas->mas->max = wr_mas->r_max;
3758 wr_mas->mas->min = wr_mas->r_min;
3759 wr_mas->mas->node = wr_mas->content;
3760 wr_mas->mas->offset = 0;
3761 wr_mas->mas->depth++;
3764 * mas_wr_walk() - Walk the tree for a write.
3765 * @wr_mas: The maple write state
3767 * Uses mas_slot_locked() and does not need to worry about dead nodes.
3769 * Return: True if it's contained in a node, false on spanning write.
3771 static bool mas_wr_walk(struct ma_wr_state *wr_mas)
3773 struct ma_state *mas = wr_mas->mas;
3776 mas_wr_walk_descend(wr_mas);
3777 if (unlikely(mas_is_span_wr(wr_mas)))
3780 wr_mas->content = mas_slot_locked(mas, wr_mas->slots,
3782 if (ma_is_leaf(wr_mas->type))
3785 mas_wr_walk_traverse(wr_mas);
3791 static bool mas_wr_walk_index(struct ma_wr_state *wr_mas)
3793 struct ma_state *mas = wr_mas->mas;
3796 mas_wr_walk_descend(wr_mas);
3797 wr_mas->content = mas_slot_locked(mas, wr_mas->slots,
3799 if (ma_is_leaf(wr_mas->type))
3801 mas_wr_walk_traverse(wr_mas);
3807 * mas_extend_spanning_null() - Extend a store of a %NULL to include surrounding %NULLs.
3808 * @l_wr_mas: The left maple write state
3809 * @r_wr_mas: The right maple write state
3811 static inline void mas_extend_spanning_null(struct ma_wr_state *l_wr_mas,
3812 struct ma_wr_state *r_wr_mas)
3814 struct ma_state *r_mas = r_wr_mas->mas;
3815 struct ma_state *l_mas = l_wr_mas->mas;
3816 unsigned char l_slot;
3818 l_slot = l_mas->offset;
3819 if (!l_wr_mas->content)
3820 l_mas->index = l_wr_mas->r_min;
3822 if ((l_mas->index == l_wr_mas->r_min) &&
3824 !mas_slot_locked(l_mas, l_wr_mas->slots, l_slot - 1))) {
3826 l_mas->index = l_wr_mas->pivots[l_slot - 2] + 1;
3828 l_mas->index = l_mas->min;
3830 l_mas->offset = l_slot - 1;
3833 if (!r_wr_mas->content) {
3834 if (r_mas->last < r_wr_mas->r_max)
3835 r_mas->last = r_wr_mas->r_max;
3837 } else if ((r_mas->last == r_wr_mas->r_max) &&
3838 (r_mas->last < r_mas->max) &&
3839 !mas_slot_locked(r_mas, r_wr_mas->slots, r_mas->offset + 1)) {
3840 r_mas->last = mas_safe_pivot(r_mas, r_wr_mas->pivots,
3841 r_wr_mas->type, r_mas->offset + 1);
3846 static inline void *mas_state_walk(struct ma_state *mas)
3850 entry = mas_start(mas);
3851 if (mas_is_none(mas))
3854 if (mas_is_ptr(mas))
3857 return mtree_range_walk(mas);
3861 * mtree_lookup_walk() - Internal quick lookup that does not keep maple state up
3864 * @mas: The maple state.
3866 * Note: Leaves mas in undesirable state.
3867 * Return: The entry for @mas->index or %NULL on dead node.
3869 static inline void *mtree_lookup_walk(struct ma_state *mas)
3871 unsigned long *pivots;
3872 unsigned char offset;
3873 struct maple_node *node;
3874 struct maple_enode *next;
3875 enum maple_type type;
3884 node = mte_to_node(next);
3885 type = mte_node_type(next);
3886 pivots = ma_pivots(node, type);
3887 end = ma_data_end(node, type, pivots, max);
3888 if (unlikely(ma_dead_node(node)))
3891 if (pivots[offset] >= mas->index)
3896 } while ((offset < end) && (pivots[offset] < mas->index));
3898 if (likely(offset > end))
3899 max = pivots[offset];
3902 slots = ma_slots(node, type);
3903 next = mt_slot(mas->tree, slots, offset);
3904 if (unlikely(ma_dead_node(node)))
3906 } while (!ma_is_leaf(type));
3908 return (void *)next;
3916 * mas_new_root() - Create a new root node that only contains the entry passed
3918 * @mas: The maple state
3919 * @entry: The entry to store.
3921 * Only valid when the index == 0 and the last == ULONG_MAX
3923 * Return 0 on error, 1 on success.
3925 static inline int mas_new_root(struct ma_state *mas, void *entry)
3927 struct maple_enode *root = mas_root_locked(mas);
3928 enum maple_type type = maple_leaf_64;
3929 struct maple_node *node;
3931 unsigned long *pivots;
3933 if (!entry && !mas->index && mas->last == ULONG_MAX) {
3935 mas_set_height(mas);
3936 rcu_assign_pointer(mas->tree->ma_root, entry);
3937 mas->node = MAS_START;
3941 mas_node_count(mas, 1);
3942 if (mas_is_err(mas))
3945 node = mas_pop_node(mas);
3946 pivots = ma_pivots(node, type);
3947 slots = ma_slots(node, type);
3948 node->parent = ma_parent_ptr(
3949 ((unsigned long)mas->tree | MA_ROOT_PARENT));
3950 mas->node = mt_mk_node(node, type);
3951 rcu_assign_pointer(slots[0], entry);
3952 pivots[0] = mas->last;
3954 mas_set_height(mas);
3955 rcu_assign_pointer(mas->tree->ma_root, mte_mk_root(mas->node));
3958 if (xa_is_node(root))
3959 mte_destroy_walk(root, mas->tree);
3964 * mas_wr_spanning_store() - Create a subtree with the store operation completed
3965 * and new nodes where necessary, then place the sub-tree in the actual tree.
3966 * Note that mas is expected to point to the node which caused the store to
3968 * @wr_mas: The maple write state
3970 * Return: 0 on error, positive on success.
3972 static inline int mas_wr_spanning_store(struct ma_wr_state *wr_mas)
3974 struct maple_subtree_state mast;
3975 struct maple_big_node b_node;
3976 struct ma_state *mas;
3977 unsigned char height;
3979 /* Left and Right side of spanning store */
3980 MA_STATE(l_mas, NULL, 0, 0);
3981 MA_STATE(r_mas, NULL, 0, 0);
3983 MA_WR_STATE(r_wr_mas, &r_mas, wr_mas->entry);
3984 MA_WR_STATE(l_wr_mas, &l_mas, wr_mas->entry);
3987 * A store operation that spans multiple nodes is called a spanning
3988 * store and is handled early in the store call stack by the function
3989 * mas_is_span_wr(). When a spanning store is identified, the maple
3990 * state is duplicated. The first maple state walks the left tree path
3991 * to ``index``, the duplicate walks the right tree path to ``last``.
3992 * The data in the two nodes are combined into a single node, two nodes,
3993 * or possibly three nodes (see the 3-way split above). A ``NULL``
3994 * written to the last entry of a node is considered a spanning store as
3995 * a rebalance is required for the operation to complete and an overflow
3996 * of data may happen.
3999 trace_ma_op(__func__, mas);
4001 if (unlikely(!mas->index && mas->last == ULONG_MAX))
4002 return mas_new_root(mas, wr_mas->entry);
4004 * Node rebalancing may occur due to this store, so there may be three new
4005 * entries per level plus a new root.
4007 height = mas_mt_height(mas);
4008 mas_node_count(mas, 1 + height * 3);
4009 if (mas_is_err(mas))
4013 * Set up right side. Need to get to the next offset after the spanning
4014 * store to ensure it's not NULL and to combine both the next node and
4015 * the node with the start together.
4018 /* Avoid overflow, walk to next slot in the tree. */
4022 r_mas.index = r_mas.last;
4023 mas_wr_walk_index(&r_wr_mas);
4024 r_mas.last = r_mas.index = mas->last;
4026 /* Set up left side. */
4028 mas_wr_walk_index(&l_wr_mas);
4030 if (!wr_mas->entry) {
4031 mas_extend_spanning_null(&l_wr_mas, &r_wr_mas);
4032 mas->offset = l_mas.offset;
4033 mas->index = l_mas.index;
4034 mas->last = l_mas.last = r_mas.last;
4037 /* expanding NULLs may make this cover the entire range */
4038 if (!l_mas.index && r_mas.last == ULONG_MAX) {
4039 mas_set_range(mas, 0, ULONG_MAX);
4040 return mas_new_root(mas, wr_mas->entry);
4043 memset(&b_node, 0, sizeof(struct maple_big_node));
4044 /* Copy l_mas and store the value in b_node. */
4045 mas_store_b_node(&l_wr_mas, &b_node, l_wr_mas.node_end);
4046 /* Copy r_mas into b_node. */
4047 if (r_mas.offset <= r_wr_mas.node_end)
4048 mas_mab_cp(&r_mas, r_mas.offset, r_wr_mas.node_end,
4049 &b_node, b_node.b_end + 1);
4053 /* Stop spanning searches by searching for just index. */
4054 l_mas.index = l_mas.last = mas->index;
4057 mast.orig_l = &l_mas;
4058 mast.orig_r = &r_mas;
4059 /* Combine l_mas and r_mas and split them up evenly again. */
4060 return mas_spanning_rebalance(mas, &mast, height + 1);
4064 * mas_wr_node_store() - Attempt to store the value in a node
4065 * @wr_mas: The maple write state
4067 * Attempts to reuse the node, but may allocate.
4069 * Return: True if stored, false otherwise
4071 static inline bool mas_wr_node_store(struct ma_wr_state *wr_mas)
4073 struct ma_state *mas = wr_mas->mas;
4074 void __rcu **dst_slots;
4075 unsigned long *dst_pivots;
4076 unsigned char dst_offset;
4077 unsigned char new_end = wr_mas->node_end;
4078 unsigned char offset;
4079 unsigned char node_slots = mt_slots[wr_mas->type];
4080 struct maple_node reuse, *newnode;
4081 unsigned char copy_size, max_piv = mt_pivots[wr_mas->type];
4082 bool in_rcu = mt_in_rcu(mas->tree);
4084 offset = mas->offset;
4085 if (mas->last == wr_mas->r_max) {
4086 /* runs right to the end of the node */
4087 if (mas->last == mas->max)
4089 /* don't copy this offset */
4090 wr_mas->offset_end++;
4091 } else if (mas->last < wr_mas->r_max) {
4092 /* new range ends in this range */
4093 if (unlikely(wr_mas->r_max == ULONG_MAX))
4094 mas_bulk_rebalance(mas, wr_mas->node_end, wr_mas->type);
4098 if (wr_mas->end_piv == mas->last)
4099 wr_mas->offset_end++;
4101 new_end -= wr_mas->offset_end - offset - 1;
4104 /* new range starts within a range */
4105 if (wr_mas->r_min < mas->index)
4108 /* Not enough room */
4109 if (new_end >= node_slots)
4112 /* Not enough data. */
4113 if (!mte_is_root(mas->node) && (new_end <= mt_min_slots[wr_mas->type]) &&
4114 !(mas->mas_flags & MA_STATE_BULK))
4119 mas_node_count(mas, 1);
4120 if (mas_is_err(mas))
4123 newnode = mas_pop_node(mas);
4125 memset(&reuse, 0, sizeof(struct maple_node));
4129 newnode->parent = mas_mn(mas)->parent;
4130 dst_pivots = ma_pivots(newnode, wr_mas->type);
4131 dst_slots = ma_slots(newnode, wr_mas->type);
4132 /* Copy from start to insert point */
4133 memcpy(dst_pivots, wr_mas->pivots, sizeof(unsigned long) * (offset + 1));
4134 memcpy(dst_slots, wr_mas->slots, sizeof(void *) * (offset + 1));
4135 dst_offset = offset;
4137 /* Handle insert of new range starting after old range */
4138 if (wr_mas->r_min < mas->index) {
4140 rcu_assign_pointer(dst_slots[dst_offset], wr_mas->content);
4141 dst_pivots[dst_offset++] = mas->index - 1;
4144 /* Store the new entry and range end. */
4145 if (dst_offset < max_piv)
4146 dst_pivots[dst_offset] = mas->last;
4147 mas->offset = dst_offset;
4148 rcu_assign_pointer(dst_slots[dst_offset], wr_mas->entry);
4151 * this range wrote to the end of the node or it overwrote the rest of
4154 if (wr_mas->offset_end > wr_mas->node_end || mas->last >= mas->max) {
4155 new_end = dst_offset;
4160 /* Copy to the end of node if necessary. */
4161 copy_size = wr_mas->node_end - wr_mas->offset_end + 1;
4162 memcpy(dst_slots + dst_offset, wr_mas->slots + wr_mas->offset_end,
4163 sizeof(void *) * copy_size);
4164 if (dst_offset < max_piv) {
4165 if (copy_size > max_piv - dst_offset)
4166 copy_size = max_piv - dst_offset;
4168 memcpy(dst_pivots + dst_offset,
4169 wr_mas->pivots + wr_mas->offset_end,
4170 sizeof(unsigned long) * copy_size);
4173 if ((wr_mas->node_end == node_slots - 1) && (new_end < node_slots - 1))
4174 dst_pivots[new_end] = mas->max;
4177 mas_leaf_set_meta(mas, newnode, dst_pivots, maple_leaf_64, new_end);
4179 mas->node = mt_mk_node(newnode, wr_mas->type);
4180 mas_replace(mas, false);
4182 memcpy(wr_mas->node, newnode, sizeof(struct maple_node));
4184 trace_ma_write(__func__, mas, 0, wr_mas->entry);
4185 mas_update_gap(mas);
4190 * mas_wr_slot_store: Attempt to store a value in a slot.
4191 * @wr_mas: the maple write state
4193 * Return: True if stored, false otherwise
4195 static inline bool mas_wr_slot_store(struct ma_wr_state *wr_mas)
4197 struct ma_state *mas = wr_mas->mas;
4198 unsigned long lmax; /* Logical max. */
4199 unsigned char offset = mas->offset;
4201 if ((wr_mas->r_max > mas->last) && ((wr_mas->r_min != mas->index) ||
4202 (offset != wr_mas->node_end)))
4205 if (offset == wr_mas->node_end - 1)
4208 lmax = wr_mas->pivots[offset + 1];
4210 /* going to overwrite too many slots. */
4211 if (lmax < mas->last)
4214 if (wr_mas->r_min == mas->index) {
4215 /* overwriting two or more ranges with one. */
4216 if (lmax == mas->last)
4219 /* Overwriting all of offset and a portion of offset + 1. */
4220 rcu_assign_pointer(wr_mas->slots[offset], wr_mas->entry);
4221 wr_mas->pivots[offset] = mas->last;
4225 /* Doesn't end on the next range end. */
4226 if (lmax != mas->last)
4229 /* Overwriting a portion of offset and all of offset + 1 */
4230 if ((offset + 1 < mt_pivots[wr_mas->type]) &&
4231 (wr_mas->entry || wr_mas->pivots[offset + 1]))
4232 wr_mas->pivots[offset + 1] = mas->last;
4234 rcu_assign_pointer(wr_mas->slots[offset + 1], wr_mas->entry);
4235 wr_mas->pivots[offset] = mas->index - 1;
4236 mas->offset++; /* Keep mas accurate. */
4239 trace_ma_write(__func__, mas, 0, wr_mas->entry);
4240 mas_update_gap(mas);
4244 static inline void mas_wr_end_piv(struct ma_wr_state *wr_mas)
4246 while ((wr_mas->mas->last > wr_mas->end_piv) &&
4247 (wr_mas->offset_end < wr_mas->node_end))
4248 wr_mas->end_piv = wr_mas->pivots[++wr_mas->offset_end];
4250 if (wr_mas->mas->last > wr_mas->end_piv)
4251 wr_mas->end_piv = wr_mas->mas->max;
4254 static inline void mas_wr_extend_null(struct ma_wr_state *wr_mas)
4256 struct ma_state *mas = wr_mas->mas;
4258 if (mas->last < wr_mas->end_piv && !wr_mas->slots[wr_mas->offset_end])
4259 mas->last = wr_mas->end_piv;
4261 /* Check next slot(s) if we are overwriting the end */
4262 if ((mas->last == wr_mas->end_piv) &&
4263 (wr_mas->node_end != wr_mas->offset_end) &&
4264 !wr_mas->slots[wr_mas->offset_end + 1]) {
4265 wr_mas->offset_end++;
4266 if (wr_mas->offset_end == wr_mas->node_end)
4267 mas->last = mas->max;
4269 mas->last = wr_mas->pivots[wr_mas->offset_end];
4270 wr_mas->end_piv = mas->last;
4273 if (!wr_mas->content) {
4274 /* If this one is null, the next and prev are not */
4275 mas->index = wr_mas->r_min;
4277 /* Check prev slot if we are overwriting the start */
4278 if (mas->index == wr_mas->r_min && mas->offset &&
4279 !wr_mas->slots[mas->offset - 1]) {
4281 wr_mas->r_min = mas->index =
4282 mas_safe_min(mas, wr_mas->pivots, mas->offset);
4283 wr_mas->r_max = wr_mas->pivots[mas->offset];
4288 static inline bool mas_wr_append(struct ma_wr_state *wr_mas)
4290 unsigned char end = wr_mas->node_end;
4291 unsigned char new_end = end + 1;
4292 struct ma_state *mas = wr_mas->mas;
4293 unsigned char node_pivots = mt_pivots[wr_mas->type];
4295 if ((mas->index != wr_mas->r_min) && (mas->last == wr_mas->r_max)) {
4296 if (new_end < node_pivots)
4297 wr_mas->pivots[new_end] = wr_mas->pivots[end];
4299 if (new_end < node_pivots)
4300 ma_set_meta(wr_mas->node, maple_leaf_64, 0, new_end);
4302 rcu_assign_pointer(wr_mas->slots[new_end], wr_mas->entry);
4303 mas->offset = new_end;
4304 wr_mas->pivots[end] = mas->index - 1;
4309 if ((mas->index == wr_mas->r_min) && (mas->last < wr_mas->r_max)) {
4310 if (new_end < node_pivots)
4311 wr_mas->pivots[new_end] = wr_mas->pivots[end];
4313 rcu_assign_pointer(wr_mas->slots[new_end], wr_mas->content);
4314 if (new_end < node_pivots)
4315 ma_set_meta(wr_mas->node, maple_leaf_64, 0, new_end);
4317 wr_mas->pivots[end] = mas->last;
4318 rcu_assign_pointer(wr_mas->slots[end], wr_mas->entry);
4326 * mas_wr_bnode() - Slow path for a modification.
4327 * @wr_mas: The write maple state
4329 * This is where split, rebalance end up.
4331 static void mas_wr_bnode(struct ma_wr_state *wr_mas)
4333 struct maple_big_node b_node;
4335 trace_ma_write(__func__, wr_mas->mas, 0, wr_mas->entry);
4336 memset(&b_node, 0, sizeof(struct maple_big_node));
4337 mas_store_b_node(wr_mas, &b_node, wr_mas->offset_end);
4338 mas_commit_b_node(wr_mas, &b_node, wr_mas->node_end);
4341 static inline void mas_wr_modify(struct ma_wr_state *wr_mas)
4343 unsigned char node_slots;
4344 unsigned char node_size;
4345 struct ma_state *mas = wr_mas->mas;
4347 /* Direct replacement */
4348 if (wr_mas->r_min == mas->index && wr_mas->r_max == mas->last) {
4349 rcu_assign_pointer(wr_mas->slots[mas->offset], wr_mas->entry);
4350 if (!!wr_mas->entry ^ !!wr_mas->content)
4351 mas_update_gap(mas);
4355 /* Attempt to append */
4356 node_slots = mt_slots[wr_mas->type];
4357 node_size = wr_mas->node_end - wr_mas->offset_end + mas->offset + 2;
4358 if (mas->max == ULONG_MAX)
4361 /* slot and node store will not fit, go to the slow path */
4362 if (unlikely(node_size >= node_slots))
4365 if (wr_mas->entry && (wr_mas->node_end < node_slots - 1) &&
4366 (mas->offset == wr_mas->node_end) && mas_wr_append(wr_mas)) {
4367 if (!wr_mas->content || !wr_mas->entry)
4368 mas_update_gap(mas);
4372 if ((wr_mas->offset_end - mas->offset <= 1) && mas_wr_slot_store(wr_mas))
4374 else if (mas_wr_node_store(wr_mas))
4377 if (mas_is_err(mas))
4381 mas_wr_bnode(wr_mas);
4385 * mas_wr_store_entry() - Internal call to store a value
4386 * @mas: The maple state
4387 * @entry: The entry to store.
4389 * Return: The contents that was stored at the index.
4391 static inline void *mas_wr_store_entry(struct ma_wr_state *wr_mas)
4393 struct ma_state *mas = wr_mas->mas;
4395 wr_mas->content = mas_start(mas);
4396 if (mas_is_none(mas) || mas_is_ptr(mas)) {
4397 mas_store_root(mas, wr_mas->entry);
4398 return wr_mas->content;
4401 if (unlikely(!mas_wr_walk(wr_mas))) {
4402 mas_wr_spanning_store(wr_mas);
4403 return wr_mas->content;
4406 /* At this point, we are at the leaf node that needs to be altered. */
4407 wr_mas->end_piv = wr_mas->r_max;
4408 mas_wr_end_piv(wr_mas);
4411 mas_wr_extend_null(wr_mas);
4413 /* New root for a single pointer */
4414 if (unlikely(!mas->index && mas->last == ULONG_MAX)) {
4415 mas_new_root(mas, wr_mas->entry);
4416 return wr_mas->content;
4419 mas_wr_modify(wr_mas);
4420 return wr_mas->content;
4424 * mas_insert() - Internal call to insert a value
4425 * @mas: The maple state
4426 * @entry: The entry to store
4428 * Return: %NULL or the contents that already exists at the requested index
4429 * otherwise. The maple state needs to be checked for error conditions.
4431 static inline void *mas_insert(struct ma_state *mas, void *entry)
4433 MA_WR_STATE(wr_mas, mas, entry);
4436 * Inserting a new range inserts either 0, 1, or 2 pivots within the
4437 * tree. If the insert fits exactly into an existing gap with a value
4438 * of NULL, then the slot only needs to be written with the new value.
4439 * If the range being inserted is adjacent to another range, then only a
4440 * single pivot needs to be inserted (as well as writing the entry). If
4441 * the new range is within a gap but does not touch any other ranges,
4442 * then two pivots need to be inserted: the start - 1, and the end. As
4443 * usual, the entry must be written. Most operations require a new node
4444 * to be allocated and replace an existing node to ensure RCU safety,
4445 * when in RCU mode. The exception to requiring a newly allocated node
4446 * is when inserting at the end of a node (appending). When done
4447 * carefully, appending can reuse the node in place.
4449 wr_mas.content = mas_start(mas);
4453 if (mas_is_none(mas) || mas_is_ptr(mas)) {
4454 mas_store_root(mas, entry);
4458 /* spanning writes always overwrite something */
4459 if (!mas_wr_walk(&wr_mas))
4462 /* At this point, we are at the leaf node that needs to be altered. */
4463 wr_mas.offset_end = mas->offset;
4464 wr_mas.end_piv = wr_mas.r_max;
4466 if (wr_mas.content || (mas->last > wr_mas.r_max))
4472 mas_wr_modify(&wr_mas);
4473 return wr_mas.content;
4476 mas_set_err(mas, -EEXIST);
4477 return wr_mas.content;
4482 * mas_prev_node() - Find the prev non-null entry at the same level in the
4483 * tree. The prev value will be mas->node[mas->offset] or MAS_NONE.
4484 * @mas: The maple state
4485 * @min: The lower limit to search
4487 * The prev node value will be mas->node[mas->offset] or MAS_NONE.
4488 * Return: 1 if the node is dead, 0 otherwise.
4490 static inline int mas_prev_node(struct ma_state *mas, unsigned long min)
4495 struct maple_node *node;
4496 struct maple_enode *enode;
4497 unsigned long *pivots;
4499 if (mas_is_none(mas))
4505 if (ma_is_root(node))
4509 if (unlikely(mas_ascend(mas)))
4511 offset = mas->offset;
4516 mt = mte_node_type(mas->node);
4518 slots = ma_slots(node, mt);
4519 pivots = ma_pivots(node, mt);
4520 if (unlikely(ma_dead_node(node)))
4523 mas->max = pivots[offset];
4525 mas->min = pivots[offset - 1] + 1;
4526 if (unlikely(ma_dead_node(node)))
4534 enode = mas_slot(mas, slots, offset);
4535 if (unlikely(ma_dead_node(node)))
4539 mt = mte_node_type(mas->node);
4541 slots = ma_slots(node, mt);
4542 pivots = ma_pivots(node, mt);
4543 offset = ma_data_end(node, mt, pivots, mas->max);
4544 if (unlikely(ma_dead_node(node)))
4548 mas->min = pivots[offset - 1] + 1;
4550 if (offset < mt_pivots[mt])
4551 mas->max = pivots[offset];
4557 mas->node = mas_slot(mas, slots, offset);
4558 if (unlikely(ma_dead_node(node)))
4561 mas->offset = mas_data_end(mas);
4562 if (unlikely(mte_dead_node(mas->node)))
4568 mas->offset = offset;
4570 mas->min = pivots[offset - 1] + 1;
4572 if (unlikely(ma_dead_node(node)))
4575 mas->node = MAS_NONE;
4580 * mas_next_node() - Get the next node at the same level in the tree.
4581 * @mas: The maple state
4582 * @max: The maximum pivot value to check.
4584 * The next value will be mas->node[mas->offset] or MAS_NONE.
4585 * Return: 1 on dead node, 0 otherwise.
4587 static inline int mas_next_node(struct ma_state *mas, struct maple_node *node,
4590 unsigned long min, pivot;
4591 unsigned long *pivots;
4592 struct maple_enode *enode;
4594 unsigned char offset;
4595 unsigned char node_end;
4599 if (mas->max >= max)
4604 if (ma_is_root(node))
4611 if (unlikely(mas_ascend(mas)))
4614 offset = mas->offset;
4617 mt = mte_node_type(mas->node);
4618 pivots = ma_pivots(node, mt);
4619 node_end = ma_data_end(node, mt, pivots, mas->max);
4620 if (unlikely(ma_dead_node(node)))
4623 } while (unlikely(offset == node_end));
4625 slots = ma_slots(node, mt);
4626 pivot = mas_safe_pivot(mas, pivots, ++offset, mt);
4627 while (unlikely(level > 1)) {
4628 /* Descend, if necessary */
4629 enode = mas_slot(mas, slots, offset);
4630 if (unlikely(ma_dead_node(node)))
4636 mt = mte_node_type(mas->node);
4637 slots = ma_slots(node, mt);
4638 pivots = ma_pivots(node, mt);
4639 if (unlikely(ma_dead_node(node)))
4646 enode = mas_slot(mas, slots, offset);
4647 if (unlikely(ma_dead_node(node)))
4656 if (unlikely(ma_dead_node(node)))
4659 mas->node = MAS_NONE;
4664 * mas_next_nentry() - Get the next node entry
4665 * @mas: The maple state
4666 * @max: The maximum value to check
4667 * @*range_start: Pointer to store the start of the range.
4669 * Sets @mas->offset to the offset of the next node entry, @mas->last to the
4670 * pivot of the entry.
4672 * Return: The next entry, %NULL otherwise
4674 static inline void *mas_next_nentry(struct ma_state *mas,
4675 struct maple_node *node, unsigned long max, enum maple_type type)
4677 unsigned char count;
4678 unsigned long pivot;
4679 unsigned long *pivots;
4683 if (mas->last == mas->max) {
4684 mas->index = mas->max;
4688 slots = ma_slots(node, type);
4689 pivots = ma_pivots(node, type);
4690 count = ma_data_end(node, type, pivots, mas->max);
4691 if (unlikely(ma_dead_node(node)))
4694 mas->index = mas_safe_min(mas, pivots, mas->offset);
4695 if (unlikely(ma_dead_node(node)))
4698 if (mas->index > max)
4701 if (mas->offset > count)
4704 while (mas->offset < count) {
4705 pivot = pivots[mas->offset];
4706 entry = mas_slot(mas, slots, mas->offset);
4707 if (ma_dead_node(node))
4716 mas->index = pivot + 1;
4720 if (mas->index > mas->max) {
4721 mas->index = mas->last;
4725 pivot = mas_safe_pivot(mas, pivots, mas->offset, type);
4726 entry = mas_slot(mas, slots, mas->offset);
4727 if (ma_dead_node(node))
4741 static inline void mas_rewalk(struct ma_state *mas, unsigned long index)
4744 mas_set(mas, index);
4745 mas_state_walk(mas);
4746 if (mas_is_start(mas))
4751 * mas_next_entry() - Internal function to get the next entry.
4752 * @mas: The maple state
4753 * @limit: The maximum range start.
4755 * Set the @mas->node to the next entry and the range_start to
4756 * the beginning value for the entry. Does not check beyond @limit.
4757 * Sets @mas->index and @mas->last to the limit if it is hit.
4758 * Restarts on dead nodes.
4760 * Return: the next entry or %NULL.
4762 static inline void *mas_next_entry(struct ma_state *mas, unsigned long limit)
4765 struct maple_enode *prev_node;
4766 struct maple_node *node;
4767 unsigned char offset;
4771 if (mas->index > limit) {
4772 mas->index = mas->last = limit;
4778 offset = mas->offset;
4779 prev_node = mas->node;
4781 mt = mte_node_type(mas->node);
4783 if (unlikely(mas->offset >= mt_slots[mt])) {
4784 mas->offset = mt_slots[mt] - 1;
4788 while (!mas_is_none(mas)) {
4789 entry = mas_next_nentry(mas, node, limit, mt);
4790 if (unlikely(ma_dead_node(node))) {
4791 mas_rewalk(mas, last);
4798 if (unlikely((mas->index > limit)))
4802 prev_node = mas->node;
4803 offset = mas->offset;
4804 if (unlikely(mas_next_node(mas, node, limit))) {
4805 mas_rewalk(mas, last);
4810 mt = mte_node_type(mas->node);
4813 mas->index = mas->last = limit;
4814 mas->offset = offset;
4815 mas->node = prev_node;
4820 * mas_prev_nentry() - Get the previous node entry.
4821 * @mas: The maple state.
4822 * @limit: The lower limit to check for a value.
4824 * Return: the entry, %NULL otherwise.
4826 static inline void *mas_prev_nentry(struct ma_state *mas, unsigned long limit,
4827 unsigned long index)
4829 unsigned long pivot, min;
4830 unsigned char offset;
4831 struct maple_node *mn;
4833 unsigned long *pivots;
4842 mt = mte_node_type(mas->node);
4843 offset = mas->offset - 1;
4844 if (offset >= mt_slots[mt])
4845 offset = mt_slots[mt] - 1;
4847 slots = ma_slots(mn, mt);
4848 pivots = ma_pivots(mn, mt);
4849 if (unlikely(ma_dead_node(mn))) {
4850 mas_rewalk(mas, index);
4854 if (offset == mt_pivots[mt])
4857 pivot = pivots[offset];
4859 if (unlikely(ma_dead_node(mn))) {
4860 mas_rewalk(mas, index);
4864 while (offset && ((!mas_slot(mas, slots, offset) && pivot >= limit) ||
4866 pivot = pivots[--offset];
4868 min = mas_safe_min(mas, pivots, offset);
4869 entry = mas_slot(mas, slots, offset);
4870 if (unlikely(ma_dead_node(mn))) {
4871 mas_rewalk(mas, index);
4875 if (likely(entry)) {
4876 mas->offset = offset;
4883 static inline void *mas_prev_entry(struct ma_state *mas, unsigned long min)
4887 if (mas->index < min) {
4888 mas->index = mas->last = min;
4889 mas->node = MAS_NONE;
4893 while (likely(!mas_is_none(mas))) {
4894 entry = mas_prev_nentry(mas, min, mas->index);
4895 if (unlikely(mas->last < min))
4901 if (unlikely(mas_prev_node(mas, min))) {
4902 mas_rewalk(mas, mas->index);
4911 mas->index = mas->last = min;
4916 * mas_rev_awalk() - Internal function. Reverse allocation walk. Find the
4917 * highest gap address of a given size in a given node and descend.
4918 * @mas: The maple state
4919 * @size: The needed size.
4921 * Return: True if found in a leaf, false otherwise.
4924 static bool mas_rev_awalk(struct ma_state *mas, unsigned long size)
4926 enum maple_type type = mte_node_type(mas->node);
4927 struct maple_node *node = mas_mn(mas);
4928 unsigned long *pivots, *gaps;
4930 unsigned long gap = 0;
4931 unsigned long max, min;
4932 unsigned char offset;
4934 if (unlikely(mas_is_err(mas)))
4937 if (ma_is_dense(type)) {
4939 mas->offset = (unsigned char)(mas->index - mas->min);
4943 pivots = ma_pivots(node, type);
4944 slots = ma_slots(node, type);
4945 gaps = ma_gaps(node, type);
4946 offset = mas->offset;
4947 min = mas_safe_min(mas, pivots, offset);
4948 /* Skip out of bounds. */
4949 while (mas->last < min)
4950 min = mas_safe_min(mas, pivots, --offset);
4952 max = mas_safe_pivot(mas, pivots, offset, type);
4953 while (mas->index <= max) {
4957 else if (!mas_slot(mas, slots, offset))
4958 gap = max - min + 1;
4961 if ((size <= gap) && (size <= mas->last - min + 1))
4965 /* Skip the next slot, it cannot be a gap. */
4970 max = pivots[offset];
4971 min = mas_safe_min(mas, pivots, offset);
4981 min = mas_safe_min(mas, pivots, offset);
4984 if (unlikely((mas->index > max) || (size - 1 > max - mas->index)))
4987 if (unlikely(ma_is_leaf(type))) {
4988 mas->offset = offset;
4990 mas->max = min + gap - 1;
4994 /* descend, only happens under lock. */
4995 mas->node = mas_slot(mas, slots, offset);
4998 mas->offset = mas_data_end(mas);
5002 if (!mte_is_root(mas->node))
5006 mas_set_err(mas, -EBUSY);
5010 static inline bool mas_anode_descend(struct ma_state *mas, unsigned long size)
5012 enum maple_type type = mte_node_type(mas->node);
5013 unsigned long pivot, min, gap = 0;
5014 unsigned char offset;
5015 unsigned long *gaps;
5016 unsigned long *pivots = ma_pivots(mas_mn(mas), type);
5017 void __rcu **slots = ma_slots(mas_mn(mas), type);
5020 if (ma_is_dense(type)) {
5021 mas->offset = (unsigned char)(mas->index - mas->min);
5025 gaps = ma_gaps(mte_to_node(mas->node), type);
5026 offset = mas->offset;
5027 min = mas_safe_min(mas, pivots, offset);
5028 for (; offset < mt_slots[type]; offset++) {
5029 pivot = mas_safe_pivot(mas, pivots, offset, type);
5030 if (offset && !pivot)
5033 /* Not within lower bounds */
5034 if (mas->index > pivot)
5039 else if (!mas_slot(mas, slots, offset))
5040 gap = min(pivot, mas->last) - max(mas->index, min) + 1;
5045 if (ma_is_leaf(type)) {
5049 if (mas->index <= pivot) {
5050 mas->node = mas_slot(mas, slots, offset);
5059 if (mas->last <= pivot) {
5060 mas_set_err(mas, -EBUSY);
5065 if (mte_is_root(mas->node))
5068 mas->offset = offset;
5073 * mas_walk() - Search for @mas->index in the tree.
5074 * @mas: The maple state.
5076 * mas->index and mas->last will be set to the range if there is a value. If
5077 * mas->node is MAS_NONE, reset to MAS_START.
5079 * Return: the entry at the location or %NULL.
5081 void *mas_walk(struct ma_state *mas)
5086 entry = mas_state_walk(mas);
5087 if (mas_is_start(mas))
5090 if (mas_is_ptr(mas)) {
5095 mas->last = ULONG_MAX;
5100 if (mas_is_none(mas)) {
5102 mas->last = ULONG_MAX;
5107 EXPORT_SYMBOL_GPL(mas_walk);
5109 static inline bool mas_rewind_node(struct ma_state *mas)
5114 if (mte_is_root(mas->node)) {
5124 mas->offset = --slot;
5129 * mas_skip_node() - Internal function. Skip over a node.
5130 * @mas: The maple state.
5132 * Return: true if there is another node, false otherwise.
5134 static inline bool mas_skip_node(struct ma_state *mas)
5136 if (mas_is_err(mas))
5140 if (mte_is_root(mas->node)) {
5141 if (mas->offset >= mas_data_end(mas)) {
5142 mas_set_err(mas, -EBUSY);
5148 } while (mas->offset >= mas_data_end(mas));
5155 * mas_awalk() - Allocation walk. Search from low address to high, for a gap of
5157 * @mas: The maple state
5158 * @size: The size of the gap required
5160 * Search between @mas->index and @mas->last for a gap of @size.
5162 static inline void mas_awalk(struct ma_state *mas, unsigned long size)
5164 struct maple_enode *last = NULL;
5167 * There are 4 options:
5168 * go to child (descend)
5169 * go back to parent (ascend)
5170 * no gap found. (return, slot == MAPLE_NODE_SLOTS)
5171 * found the gap. (return, slot != MAPLE_NODE_SLOTS)
5173 while (!mas_is_err(mas) && !mas_anode_descend(mas, size)) {
5174 if (last == mas->node)
5182 * mas_fill_gap() - Fill a located gap with @entry.
5183 * @mas: The maple state
5184 * @entry: The value to store
5185 * @slot: The offset into the node to store the @entry
5186 * @size: The size of the entry
5187 * @index: The start location
5189 static inline void mas_fill_gap(struct ma_state *mas, void *entry,
5190 unsigned char slot, unsigned long size, unsigned long *index)
5192 MA_WR_STATE(wr_mas, mas, entry);
5193 unsigned char pslot = mte_parent_slot(mas->node);
5194 struct maple_enode *mn = mas->node;
5195 unsigned long *pivots;
5196 enum maple_type ptype;
5198 * mas->index is the start address for the search
5199 * which may no longer be needed.
5200 * mas->last is the end address for the search
5203 *index = mas->index;
5204 mas->last = mas->index + size - 1;
5207 * It is possible that using mas->max and mas->min to correctly
5208 * calculate the index and last will cause an issue in the gap
5209 * calculation, so fix the ma_state here
5212 ptype = mte_node_type(mas->node);
5213 pivots = ma_pivots(mas_mn(mas), ptype);
5214 mas->max = mas_safe_pivot(mas, pivots, pslot, ptype);
5215 mas->min = mas_safe_min(mas, pivots, pslot);
5218 mas_wr_store_entry(&wr_mas);
5222 * mas_sparse_area() - Internal function. Return upper or lower limit when
5223 * searching for a gap in an empty tree.
5224 * @mas: The maple state
5225 * @min: the minimum range
5226 * @max: The maximum range
5227 * @size: The size of the gap
5228 * @fwd: Searching forward or back
5230 static inline void mas_sparse_area(struct ma_state *mas, unsigned long min,
5231 unsigned long max, unsigned long size, bool fwd)
5233 unsigned long start = 0;
5235 if (!unlikely(mas_is_none(mas)))
5244 mas->last = start + size - 1;
5252 * mas_empty_area() - Get the lowest address within the range that is
5253 * sufficient for the size requested.
5254 * @mas: The maple state
5255 * @min: The lowest value of the range
5256 * @max: The highest value of the range
5257 * @size: The size needed
5259 int mas_empty_area(struct ma_state *mas, unsigned long min,
5260 unsigned long max, unsigned long size)
5262 unsigned char offset;
5263 unsigned long *pivots;
5266 if (mas_is_start(mas))
5268 else if (mas->offset >= 2)
5270 else if (!mas_skip_node(mas))
5274 if (mas_is_none(mas) || mas_is_ptr(mas)) {
5275 mas_sparse_area(mas, min, max, size, true);
5279 /* The start of the window can only be within these values */
5282 mas_awalk(mas, size);
5284 if (unlikely(mas_is_err(mas)))
5285 return xa_err(mas->node);
5287 offset = mas->offset;
5288 if (unlikely(offset == MAPLE_NODE_SLOTS))
5291 mt = mte_node_type(mas->node);
5292 pivots = ma_pivots(mas_mn(mas), mt);
5294 mas->min = pivots[offset - 1] + 1;
5296 if (offset < mt_pivots[mt])
5297 mas->max = pivots[offset];
5299 if (mas->index < mas->min)
5300 mas->index = mas->min;
5302 mas->last = mas->index + size - 1;
5305 EXPORT_SYMBOL_GPL(mas_empty_area);
5308 * mas_empty_area_rev() - Get the highest address within the range that is
5309 * sufficient for the size requested.
5310 * @mas: The maple state
5311 * @min: The lowest value of the range
5312 * @max: The highest value of the range
5313 * @size: The size needed
5315 int mas_empty_area_rev(struct ma_state *mas, unsigned long min,
5316 unsigned long max, unsigned long size)
5318 struct maple_enode *last = mas->node;
5320 if (mas_is_start(mas)) {
5322 mas->offset = mas_data_end(mas);
5323 } else if (mas->offset >= 2) {
5325 } else if (!mas_rewind_node(mas)) {
5330 if (mas_is_none(mas) || mas_is_ptr(mas)) {
5331 mas_sparse_area(mas, min, max, size, false);
5335 /* The start of the window can only be within these values. */
5339 while (!mas_rev_awalk(mas, size)) {
5340 if (last == mas->node) {
5341 if (!mas_rewind_node(mas))
5348 if (mas_is_err(mas))
5349 return xa_err(mas->node);
5351 if (unlikely(mas->offset == MAPLE_NODE_SLOTS))
5355 * mas_rev_awalk() has set mas->min and mas->max to the gap values. If
5356 * the maximum is outside the window we are searching, then use the last
5357 * location in the search.
5358 * mas->max and mas->min is the range of the gap.
5359 * mas->index and mas->last are currently set to the search range.
5362 /* Trim the upper limit to the max. */
5363 if (mas->max <= mas->last)
5364 mas->last = mas->max;
5366 mas->index = mas->last - size + 1;
5369 EXPORT_SYMBOL_GPL(mas_empty_area_rev);
5371 static inline int mas_alloc(struct ma_state *mas, void *entry,
5372 unsigned long size, unsigned long *index)
5377 if (mas_is_none(mas) || mas_is_ptr(mas)) {
5378 mas_root_expand(mas, entry);
5379 if (mas_is_err(mas))
5380 return xa_err(mas->node);
5383 return mte_pivot(mas->node, 0);
5384 return mte_pivot(mas->node, 1);
5387 /* Must be walking a tree. */
5388 mas_awalk(mas, size);
5389 if (mas_is_err(mas))
5390 return xa_err(mas->node);
5392 if (mas->offset == MAPLE_NODE_SLOTS)
5396 * At this point, mas->node points to the right node and we have an
5397 * offset that has a sufficient gap.
5401 min = mte_pivot(mas->node, mas->offset - 1) + 1;
5403 if (mas->index < min)
5406 mas_fill_gap(mas, entry, mas->offset, size, index);
5413 static inline int mas_rev_alloc(struct ma_state *mas, unsigned long min,
5414 unsigned long max, void *entry,
5415 unsigned long size, unsigned long *index)
5419 ret = mas_empty_area_rev(mas, min, max, size);
5423 if (mas_is_err(mas))
5424 return xa_err(mas->node);
5426 if (mas->offset == MAPLE_NODE_SLOTS)
5429 mas_fill_gap(mas, entry, mas->offset, size, index);
5437 * mas_dead_leaves() - Mark all leaves of a node as dead.
5438 * @mas: The maple state
5439 * @slots: Pointer to the slot array
5441 * Must hold the write lock.
5443 * Return: The number of leaves marked as dead.
5446 unsigned char mas_dead_leaves(struct ma_state *mas, void __rcu **slots)
5448 struct maple_node *node;
5449 enum maple_type type;
5453 for (offset = 0; offset < mt_slot_count(mas->node); offset++) {
5454 entry = mas_slot_locked(mas, slots, offset);
5455 type = mte_node_type(entry);
5456 node = mte_to_node(entry);
5457 /* Use both node and type to catch LE & BE metadata */
5461 mte_set_node_dead(entry);
5462 smp_wmb(); /* Needed for RCU */
5464 rcu_assign_pointer(slots[offset], node);
5470 static void __rcu **mas_dead_walk(struct ma_state *mas, unsigned char offset)
5472 struct maple_node *node, *next;
5473 void __rcu **slots = NULL;
5477 mas->node = ma_enode_ptr(next);
5479 slots = ma_slots(node, node->type);
5480 next = mas_slot_locked(mas, slots, offset);
5482 } while (!ma_is_leaf(next->type));
5487 static void mt_free_walk(struct rcu_head *head)
5490 struct maple_node *node, *start;
5491 struct maple_tree mt;
5492 unsigned char offset;
5493 enum maple_type type;
5494 MA_STATE(mas, &mt, 0, 0);
5496 node = container_of(head, struct maple_node, rcu);
5498 if (ma_is_leaf(node->type))
5501 mt_init_flags(&mt, node->ma_flags);
5504 mas.node = mt_mk_node(node, node->type);
5505 slots = mas_dead_walk(&mas, 0);
5506 node = mas_mn(&mas);
5508 mt_free_bulk(node->slot_len, slots);
5509 offset = node->parent_slot + 1;
5510 mas.node = node->piv_parent;
5511 if (mas_mn(&mas) == node)
5512 goto start_slots_free;
5514 type = mte_node_type(mas.node);
5515 slots = ma_slots(mte_to_node(mas.node), type);
5516 if ((offset < mt_slots[type]) && (slots[offset]))
5517 slots = mas_dead_walk(&mas, offset);
5519 node = mas_mn(&mas);
5520 } while ((node != start) || (node->slot_len < offset));
5522 slots = ma_slots(node, node->type);
5523 mt_free_bulk(node->slot_len, slots);
5528 mt_free_rcu(&node->rcu);
5531 static inline void __rcu **mas_destroy_descend(struct ma_state *mas,
5532 struct maple_enode *prev, unsigned char offset)
5534 struct maple_node *node;
5535 struct maple_enode *next = mas->node;
5536 void __rcu **slots = NULL;
5541 slots = ma_slots(node, mte_node_type(mas->node));
5542 next = mas_slot_locked(mas, slots, 0);
5543 if ((mte_dead_node(next)))
5544 next = mas_slot_locked(mas, slots, 1);
5546 mte_set_node_dead(mas->node);
5547 node->type = mte_node_type(mas->node);
5548 node->piv_parent = prev;
5549 node->parent_slot = offset;
5552 } while (!mte_is_leaf(next));
5557 static void mt_destroy_walk(struct maple_enode *enode, unsigned char ma_flags,
5561 struct maple_node *node = mte_to_node(enode);
5562 struct maple_enode *start;
5563 struct maple_tree mt;
5565 MA_STATE(mas, &mt, 0, 0);
5567 if (mte_is_leaf(enode))
5570 mt_init_flags(&mt, ma_flags);
5573 mas.node = start = enode;
5574 slots = mas_destroy_descend(&mas, start, 0);
5575 node = mas_mn(&mas);
5577 enum maple_type type;
5578 unsigned char offset;
5579 struct maple_enode *parent, *tmp;
5581 node->slot_len = mas_dead_leaves(&mas, slots);
5583 mt_free_bulk(node->slot_len, slots);
5584 offset = node->parent_slot + 1;
5585 mas.node = node->piv_parent;
5586 if (mas_mn(&mas) == node)
5587 goto start_slots_free;
5589 type = mte_node_type(mas.node);
5590 slots = ma_slots(mte_to_node(mas.node), type);
5591 if (offset >= mt_slots[type])
5594 tmp = mas_slot_locked(&mas, slots, offset);
5595 if (mte_node_type(tmp) && mte_to_node(tmp)) {
5598 slots = mas_destroy_descend(&mas, parent, offset);
5601 node = mas_mn(&mas);
5602 } while (start != mas.node);
5604 node = mas_mn(&mas);
5605 node->slot_len = mas_dead_leaves(&mas, slots);
5607 mt_free_bulk(node->slot_len, slots);
5614 mt_free_rcu(&node->rcu);
5618 * mte_destroy_walk() - Free a tree or sub-tree.
5619 * @enode: the encoded maple node (maple_enode) to start
5620 * @mt: the tree to free - needed for node types.
5622 * Must hold the write lock.
5624 static inline void mte_destroy_walk(struct maple_enode *enode,
5625 struct maple_tree *mt)
5627 struct maple_node *node = mte_to_node(enode);
5629 if (mt_in_rcu(mt)) {
5630 mt_destroy_walk(enode, mt->ma_flags, false);
5631 call_rcu(&node->rcu, mt_free_walk);
5633 mt_destroy_walk(enode, mt->ma_flags, true);
5637 static void mas_wr_store_setup(struct ma_wr_state *wr_mas)
5639 if (unlikely(mas_is_paused(wr_mas->mas)))
5640 mas_reset(wr_mas->mas);
5642 if (!mas_is_start(wr_mas->mas)) {
5643 if (mas_is_none(wr_mas->mas)) {
5644 mas_reset(wr_mas->mas);
5646 wr_mas->r_max = wr_mas->mas->max;
5647 wr_mas->type = mte_node_type(wr_mas->mas->node);
5648 if (mas_is_span_wr(wr_mas))
5649 mas_reset(wr_mas->mas);
5657 * mas_store() - Store an @entry.
5658 * @mas: The maple state.
5659 * @entry: The entry to store.
5661 * The @mas->index and @mas->last is used to set the range for the @entry.
5662 * Note: The @mas should have pre-allocated entries to ensure there is memory to
5663 * store the entry. Please see mas_expected_entries()/mas_destroy() for more details.
5665 * Return: the first entry between mas->index and mas->last or %NULL.
5667 void *mas_store(struct ma_state *mas, void *entry)
5669 MA_WR_STATE(wr_mas, mas, entry);
5671 trace_ma_write(__func__, mas, 0, entry);
5672 #ifdef CONFIG_DEBUG_MAPLE_TREE
5673 if (mas->index > mas->last)
5674 pr_err("Error %lu > %lu %p\n", mas->index, mas->last, entry);
5675 MT_BUG_ON(mas->tree, mas->index > mas->last);
5676 if (mas->index > mas->last) {
5677 mas_set_err(mas, -EINVAL);
5684 * Storing is the same operation as insert with the added caveat that it
5685 * can overwrite entries. Although this seems simple enough, one may
5686 * want to examine what happens if a single store operation was to
5687 * overwrite multiple entries within a self-balancing B-Tree.
5689 mas_wr_store_setup(&wr_mas);
5690 mas_wr_store_entry(&wr_mas);
5691 return wr_mas.content;
5693 EXPORT_SYMBOL_GPL(mas_store);
5696 * mas_store_gfp() - Store a value into the tree.
5697 * @mas: The maple state
5698 * @entry: The entry to store
5699 * @gfp: The GFP_FLAGS to use for allocations if necessary.
5701 * Return: 0 on success, -EINVAL on invalid request, -ENOMEM if memory could not
5704 int mas_store_gfp(struct ma_state *mas, void *entry, gfp_t gfp)
5706 MA_WR_STATE(wr_mas, mas, entry);
5708 mas_wr_store_setup(&wr_mas);
5709 trace_ma_write(__func__, mas, 0, entry);
5711 mas_wr_store_entry(&wr_mas);
5712 if (unlikely(mas_nomem(mas, gfp)))
5715 if (unlikely(mas_is_err(mas)))
5716 return xa_err(mas->node);
5720 EXPORT_SYMBOL_GPL(mas_store_gfp);
5723 * mas_store_prealloc() - Store a value into the tree using memory
5724 * preallocated in the maple state.
5725 * @mas: The maple state
5726 * @entry: The entry to store.
5728 void mas_store_prealloc(struct ma_state *mas, void *entry)
5730 MA_WR_STATE(wr_mas, mas, entry);
5732 mas_wr_store_setup(&wr_mas);
5733 trace_ma_write(__func__, mas, 0, entry);
5734 mas_wr_store_entry(&wr_mas);
5735 BUG_ON(mas_is_err(mas));
5738 EXPORT_SYMBOL_GPL(mas_store_prealloc);
5741 * mas_preallocate() - Preallocate enough nodes for a store operation
5742 * @mas: The maple state
5743 * @gfp: The GFP_FLAGS to use for allocations.
5745 * Return: 0 on success, -ENOMEM if memory could not be allocated.
5747 int mas_preallocate(struct ma_state *mas, gfp_t gfp)
5751 mas_node_count_gfp(mas, 1 + mas_mt_height(mas) * 3, gfp);
5752 mas->mas_flags |= MA_STATE_PREALLOC;
5753 if (likely(!mas_is_err(mas)))
5756 mas_set_alloc_req(mas, 0);
5757 ret = xa_err(mas->node);
5765 * mas_destroy() - destroy a maple state.
5766 * @mas: The maple state
5768 * Upon completion, check the left-most node and rebalance against the node to
5769 * the right if necessary. Frees any allocated nodes associated with this maple
5772 void mas_destroy(struct ma_state *mas)
5774 struct maple_alloc *node;
5775 unsigned long total;
5778 * When using mas_for_each() to insert an expected number of elements,
5779 * it is possible that the number inserted is less than the expected
5780 * number. To fix an invalid final node, a check is performed here to
5781 * rebalance the previous node with the final node.
5783 if (mas->mas_flags & MA_STATE_REBALANCE) {
5786 if (mas_is_start(mas))
5789 mtree_range_walk(mas);
5790 end = mas_data_end(mas) + 1;
5791 if (end < mt_min_slot_count(mas->node) - 1)
5792 mas_destroy_rebalance(mas, end);
5794 mas->mas_flags &= ~MA_STATE_REBALANCE;
5796 mas->mas_flags &= ~(MA_STATE_BULK|MA_STATE_PREALLOC);
5798 total = mas_allocated(mas);
5801 mas->alloc = node->slot[0];
5802 if (node->node_count > 1) {
5803 size_t count = node->node_count - 1;
5805 mt_free_bulk(count, (void __rcu **)&node->slot[1]);
5808 kmem_cache_free(maple_node_cache, node);
5814 EXPORT_SYMBOL_GPL(mas_destroy);
5817 * mas_expected_entries() - Set the expected number of entries that will be inserted.
5818 * @mas: The maple state
5819 * @nr_entries: The number of expected entries.
5821 * This will attempt to pre-allocate enough nodes to store the expected number
5822 * of entries. The allocations will occur using the bulk allocator interface
5823 * for speed. Please call mas_destroy() on the @mas after inserting the entries
5824 * to ensure any unused nodes are freed.
5826 * Return: 0 on success, -ENOMEM if memory could not be allocated.
5828 int mas_expected_entries(struct ma_state *mas, unsigned long nr_entries)
5830 int nonleaf_cap = MAPLE_ARANGE64_SLOTS - 2;
5831 struct maple_enode *enode = mas->node;
5836 * Sometimes it is necessary to duplicate a tree to a new tree, such as
5837 * forking a process and duplicating the VMAs from one tree to a new
5838 * tree. When such a situation arises, it is known that the new tree is
5839 * not going to be used until the entire tree is populated. For
5840 * performance reasons, it is best to use a bulk load with RCU disabled.
5841 * This allows for optimistic splitting that favours the left and reuse
5842 * of nodes during the operation.
5845 /* Optimize splitting for bulk insert in-order */
5846 mas->mas_flags |= MA_STATE_BULK;
5849 * Avoid overflow, assume a gap between each entry and a trailing null.
5850 * If this is wrong, it just means allocation can happen during
5851 * insertion of entries.
5853 nr_nodes = max(nr_entries, nr_entries * 2 + 1);
5854 if (!mt_is_alloc(mas->tree))
5855 nonleaf_cap = MAPLE_RANGE64_SLOTS - 2;
5857 /* Leaves; reduce slots to keep space for expansion */
5858 nr_nodes = DIV_ROUND_UP(nr_nodes, MAPLE_RANGE64_SLOTS - 2);
5859 /* Internal nodes */
5860 nr_nodes += DIV_ROUND_UP(nr_nodes, nonleaf_cap);
5861 /* Add working room for split (2 nodes) + new parents */
5862 mas_node_count(mas, nr_nodes + 3);
5864 /* Detect if allocations run out */
5865 mas->mas_flags |= MA_STATE_PREALLOC;
5867 if (!mas_is_err(mas))
5870 ret = xa_err(mas->node);
5876 EXPORT_SYMBOL_GPL(mas_expected_entries);
5879 * mas_next() - Get the next entry.
5880 * @mas: The maple state
5881 * @max: The maximum index to check.
5883 * Returns the next entry after @mas->index.
5884 * Must hold rcu_read_lock or the write lock.
5885 * Can return the zero entry.
5887 * Return: The next entry or %NULL
5889 void *mas_next(struct ma_state *mas, unsigned long max)
5891 if (mas_is_none(mas) || mas_is_paused(mas))
5892 mas->node = MAS_START;
5894 if (mas_is_start(mas))
5895 mas_walk(mas); /* Retries on dead nodes handled by mas_walk */
5897 if (mas_is_ptr(mas)) {
5900 mas->last = ULONG_MAX;
5905 if (mas->last == ULONG_MAX)
5908 /* Retries on dead nodes handled by mas_next_entry */
5909 return mas_next_entry(mas, max);
5911 EXPORT_SYMBOL_GPL(mas_next);
5914 * mt_next() - get the next value in the maple tree
5915 * @mt: The maple tree
5916 * @index: The start index
5917 * @max: The maximum index to check
5919 * Return: The entry at @index or higher, or %NULL if nothing is found.
5921 void *mt_next(struct maple_tree *mt, unsigned long index, unsigned long max)
5924 MA_STATE(mas, mt, index, index);
5927 entry = mas_next(&mas, max);
5931 EXPORT_SYMBOL_GPL(mt_next);
5934 * mas_prev() - Get the previous entry
5935 * @mas: The maple state
5936 * @min: The minimum value to check.
5938 * Must hold rcu_read_lock or the write lock.
5939 * Will reset mas to MAS_START if the node is MAS_NONE. Will stop on not
5942 * Return: the previous value or %NULL.
5944 void *mas_prev(struct ma_state *mas, unsigned long min)
5947 /* Nothing comes before 0 */
5949 mas->node = MAS_NONE;
5953 if (unlikely(mas_is_ptr(mas)))
5956 if (mas_is_none(mas) || mas_is_paused(mas))
5957 mas->node = MAS_START;
5959 if (mas_is_start(mas)) {
5965 if (mas_is_ptr(mas)) {
5971 mas->index = mas->last = 0;
5972 return mas_root_locked(mas);
5974 return mas_prev_entry(mas, min);
5976 EXPORT_SYMBOL_GPL(mas_prev);
5979 * mt_prev() - get the previous value in the maple tree
5980 * @mt: The maple tree
5981 * @index: The start index
5982 * @min: The minimum index to check
5984 * Return: The entry at @index or lower, or %NULL if nothing is found.
5986 void *mt_prev(struct maple_tree *mt, unsigned long index, unsigned long min)
5989 MA_STATE(mas, mt, index, index);
5992 entry = mas_prev(&mas, min);
5996 EXPORT_SYMBOL_GPL(mt_prev);
5999 * mas_pause() - Pause a mas_find/mas_for_each to drop the lock.
6000 * @mas: The maple state to pause
6002 * Some users need to pause a walk and drop the lock they're holding in
6003 * order to yield to a higher priority thread or carry out an operation
6004 * on an entry. Those users should call this function before they drop
6005 * the lock. It resets the @mas to be suitable for the next iteration
6006 * of the loop after the user has reacquired the lock. If most entries
6007 * found during a walk require you to call mas_pause(), the mt_for_each()
6008 * iterator may be more appropriate.
6011 void mas_pause(struct ma_state *mas)
6013 mas->node = MAS_PAUSE;
6015 EXPORT_SYMBOL_GPL(mas_pause);
6018 * mas_find() - On the first call, find the entry at or after mas->index up to
6019 * %max. Otherwise, find the entry after mas->index.
6020 * @mas: The maple state
6021 * @max: The maximum value to check.
6023 * Must hold rcu_read_lock or the write lock.
6024 * If an entry exists, last and index are updated accordingly.
6025 * May set @mas->node to MAS_NONE.
6027 * Return: The entry or %NULL.
6029 void *mas_find(struct ma_state *mas, unsigned long max)
6031 if (unlikely(mas_is_paused(mas))) {
6032 if (unlikely(mas->last == ULONG_MAX)) {
6033 mas->node = MAS_NONE;
6036 mas->node = MAS_START;
6037 mas->index = ++mas->last;
6040 if (unlikely(mas_is_none(mas)))
6041 mas->node = MAS_START;
6043 if (unlikely(mas_is_start(mas))) {
6044 /* First run or continue */
6047 if (mas->index > max)
6050 entry = mas_walk(mas);
6055 if (unlikely(!mas_searchable(mas)))
6058 /* Retries on dead nodes handled by mas_next_entry */
6059 return mas_next_entry(mas, max);
6061 EXPORT_SYMBOL_GPL(mas_find);
6064 * mas_find_rev: On the first call, find the first non-null entry at or below
6065 * mas->index down to %min. Otherwise find the first non-null entry below
6066 * mas->index down to %min.
6067 * @mas: The maple state
6068 * @min: The minimum value to check.
6070 * Must hold rcu_read_lock or the write lock.
6071 * If an entry exists, last and index are updated accordingly.
6072 * May set @mas->node to MAS_NONE.
6074 * Return: The entry or %NULL.
6076 void *mas_find_rev(struct ma_state *mas, unsigned long min)
6078 if (unlikely(mas_is_paused(mas))) {
6079 if (unlikely(mas->last == ULONG_MAX)) {
6080 mas->node = MAS_NONE;
6083 mas->node = MAS_START;
6084 mas->last = --mas->index;
6087 if (unlikely(mas_is_start(mas))) {
6088 /* First run or continue */
6091 if (mas->index < min)
6094 entry = mas_walk(mas);
6099 if (unlikely(!mas_searchable(mas)))
6102 if (mas->index < min)
6105 /* Retries on dead nodes handled by mas_prev_entry */
6106 return mas_prev_entry(mas, min);
6108 EXPORT_SYMBOL_GPL(mas_find_rev);
6111 * mas_erase() - Find the range in which index resides and erase the entire
6113 * @mas: The maple state
6115 * Must hold the write lock.
6116 * Searches for @mas->index, sets @mas->index and @mas->last to the range and
6117 * erases that range.
6119 * Return: the entry that was erased or %NULL, @mas->index and @mas->last are updated.
6121 void *mas_erase(struct ma_state *mas)
6124 MA_WR_STATE(wr_mas, mas, NULL);
6126 if (mas_is_none(mas) || mas_is_paused(mas))
6127 mas->node = MAS_START;
6129 /* Retry unnecessary when holding the write lock. */
6130 entry = mas_state_walk(mas);
6135 /* Must reset to ensure spanning writes of last slot are detected */
6137 mas_wr_store_setup(&wr_mas);
6138 mas_wr_store_entry(&wr_mas);
6139 if (mas_nomem(mas, GFP_KERNEL))
6144 EXPORT_SYMBOL_GPL(mas_erase);
6147 * mas_nomem() - Check if there was an error allocating and do the allocation
6148 * if necessary If there are allocations, then free them.
6149 * @mas: The maple state
6150 * @gfp: The GFP_FLAGS to use for allocations
6151 * Return: true on allocation, false otherwise.
6153 bool mas_nomem(struct ma_state *mas, gfp_t gfp)
6154 __must_hold(mas->tree->lock)
6156 if (likely(mas->node != MA_ERROR(-ENOMEM))) {
6161 if (gfpflags_allow_blocking(gfp) && !mt_external_lock(mas->tree)) {
6162 mtree_unlock(mas->tree);
6163 mas_alloc_nodes(mas, gfp);
6164 mtree_lock(mas->tree);
6166 mas_alloc_nodes(mas, gfp);
6169 if (!mas_allocated(mas))
6172 mas->node = MAS_START;
6176 void __init maple_tree_init(void)
6178 maple_node_cache = kmem_cache_create("maple_node",
6179 sizeof(struct maple_node), sizeof(struct maple_node),
6184 * mtree_load() - Load a value stored in a maple tree
6185 * @mt: The maple tree
6186 * @index: The index to load
6188 * Return: the entry or %NULL
6190 void *mtree_load(struct maple_tree *mt, unsigned long index)
6192 MA_STATE(mas, mt, index, index);
6195 trace_ma_read(__func__, &mas);
6198 entry = mas_start(&mas);
6199 if (unlikely(mas_is_none(&mas)))
6202 if (unlikely(mas_is_ptr(&mas))) {
6209 entry = mtree_lookup_walk(&mas);
6210 if (!entry && unlikely(mas_is_start(&mas)))
6214 if (xa_is_zero(entry))
6219 EXPORT_SYMBOL(mtree_load);
6222 * mtree_store_range() - Store an entry at a given range.
6223 * @mt: The maple tree
6224 * @index: The start of the range
6225 * @last: The end of the range
6226 * @entry: The entry to store
6227 * @gfp: The GFP_FLAGS to use for allocations
6229 * Return: 0 on success, -EINVAL on invalid request, -ENOMEM if memory could not
6232 int mtree_store_range(struct maple_tree *mt, unsigned long index,
6233 unsigned long last, void *entry, gfp_t gfp)
6235 MA_STATE(mas, mt, index, last);
6236 MA_WR_STATE(wr_mas, &mas, entry);
6238 trace_ma_write(__func__, &mas, 0, entry);
6239 if (WARN_ON_ONCE(xa_is_advanced(entry)))
6247 mas_wr_store_entry(&wr_mas);
6248 if (mas_nomem(&mas, gfp))
6252 if (mas_is_err(&mas))
6253 return xa_err(mas.node);
6257 EXPORT_SYMBOL(mtree_store_range);
6260 * mtree_store() - Store an entry at a given index.
6261 * @mt: The maple tree
6262 * @index: The index to store the value
6263 * @entry: The entry to store
6264 * @gfp: The GFP_FLAGS to use for allocations
6266 * Return: 0 on success, -EINVAL on invalid request, -ENOMEM if memory could not
6269 int mtree_store(struct maple_tree *mt, unsigned long index, void *entry,
6272 return mtree_store_range(mt, index, index, entry, gfp);
6274 EXPORT_SYMBOL(mtree_store);
6277 * mtree_insert_range() - Insert an entry at a give range if there is no value.
6278 * @mt: The maple tree
6279 * @first: The start of the range
6280 * @last: The end of the range
6281 * @entry: The entry to store
6282 * @gfp: The GFP_FLAGS to use for allocations.
6284 * Return: 0 on success, -EEXISTS if the range is occupied, -EINVAL on invalid
6285 * request, -ENOMEM if memory could not be allocated.
6287 int mtree_insert_range(struct maple_tree *mt, unsigned long first,
6288 unsigned long last, void *entry, gfp_t gfp)
6290 MA_STATE(ms, mt, first, last);
6292 if (WARN_ON_ONCE(xa_is_advanced(entry)))
6300 mas_insert(&ms, entry);
6301 if (mas_nomem(&ms, gfp))
6305 if (mas_is_err(&ms))
6306 return xa_err(ms.node);
6310 EXPORT_SYMBOL(mtree_insert_range);
6313 * mtree_insert() - Insert an entry at a give index if there is no value.
6314 * @mt: The maple tree
6315 * @index : The index to store the value
6316 * @entry: The entry to store
6317 * @gfp: The FGP_FLAGS to use for allocations.
6319 * Return: 0 on success, -EEXISTS if the range is occupied, -EINVAL on invalid
6320 * request, -ENOMEM if memory could not be allocated.
6322 int mtree_insert(struct maple_tree *mt, unsigned long index, void *entry,
6325 return mtree_insert_range(mt, index, index, entry, gfp);
6327 EXPORT_SYMBOL(mtree_insert);
6329 int mtree_alloc_range(struct maple_tree *mt, unsigned long *startp,
6330 void *entry, unsigned long size, unsigned long min,
6331 unsigned long max, gfp_t gfp)
6335 MA_STATE(mas, mt, min, max - size);
6336 if (!mt_is_alloc(mt))
6339 if (WARN_ON_ONCE(mt_is_reserved(entry)))
6355 mas.last = max - size;
6356 ret = mas_alloc(&mas, entry, size, startp);
6357 if (mas_nomem(&mas, gfp))
6363 EXPORT_SYMBOL(mtree_alloc_range);
6365 int mtree_alloc_rrange(struct maple_tree *mt, unsigned long *startp,
6366 void *entry, unsigned long size, unsigned long min,
6367 unsigned long max, gfp_t gfp)
6371 MA_STATE(mas, mt, min, max - size);
6372 if (!mt_is_alloc(mt))
6375 if (WARN_ON_ONCE(mt_is_reserved(entry)))
6389 ret = mas_rev_alloc(&mas, min, max, entry, size, startp);
6390 if (mas_nomem(&mas, gfp))
6396 EXPORT_SYMBOL(mtree_alloc_rrange);
6399 * mtree_erase() - Find an index and erase the entire range.
6400 * @mt: The maple tree
6401 * @index: The index to erase
6403 * Erasing is the same as a walk to an entry then a store of a NULL to that
6404 * ENTIRE range. In fact, it is implemented as such using the advanced API.
6406 * Return: The entry stored at the @index or %NULL
6408 void *mtree_erase(struct maple_tree *mt, unsigned long index)
6412 MA_STATE(mas, mt, index, index);
6413 trace_ma_op(__func__, &mas);
6416 entry = mas_erase(&mas);
6421 EXPORT_SYMBOL(mtree_erase);
6424 * __mt_destroy() - Walk and free all nodes of a locked maple tree.
6425 * @mt: The maple tree
6427 * Note: Does not handle locking.
6429 void __mt_destroy(struct maple_tree *mt)
6431 void *root = mt_root_locked(mt);
6433 rcu_assign_pointer(mt->ma_root, NULL);
6434 if (xa_is_node(root))
6435 mte_destroy_walk(root, mt);
6439 EXPORT_SYMBOL_GPL(__mt_destroy);
6442 * mtree_destroy() - Destroy a maple tree
6443 * @mt: The maple tree
6445 * Frees all resources used by the tree. Handles locking.
6447 void mtree_destroy(struct maple_tree *mt)
6453 EXPORT_SYMBOL(mtree_destroy);
6456 * mt_find() - Search from the start up until an entry is found.
6457 * @mt: The maple tree
6458 * @index: Pointer which contains the start location of the search
6459 * @max: The maximum value to check
6461 * Handles locking. @index will be incremented to one beyond the range.
6463 * Return: The entry at or after the @index or %NULL
6465 void *mt_find(struct maple_tree *mt, unsigned long *index, unsigned long max)
6467 MA_STATE(mas, mt, *index, *index);
6469 #ifdef CONFIG_DEBUG_MAPLE_TREE
6470 unsigned long copy = *index;
6473 trace_ma_read(__func__, &mas);
6480 entry = mas_state_walk(&mas);
6481 if (mas_is_start(&mas))
6484 if (unlikely(xa_is_zero(entry)))
6490 while (mas_searchable(&mas) && (mas.index < max)) {
6491 entry = mas_next_entry(&mas, max);
6492 if (likely(entry && !xa_is_zero(entry)))
6496 if (unlikely(xa_is_zero(entry)))
6500 if (likely(entry)) {
6501 *index = mas.last + 1;
6502 #ifdef CONFIG_DEBUG_MAPLE_TREE
6503 if ((*index) && (*index) <= copy)
6504 pr_err("index not increased! %lx <= %lx\n",
6506 MT_BUG_ON(mt, (*index) && ((*index) <= copy));
6512 EXPORT_SYMBOL(mt_find);
6515 * mt_find_after() - Search from the start up until an entry is found.
6516 * @mt: The maple tree
6517 * @index: Pointer which contains the start location of the search
6518 * @max: The maximum value to check
6520 * Handles locking, detects wrapping on index == 0
6522 * Return: The entry at or after the @index or %NULL
6524 void *mt_find_after(struct maple_tree *mt, unsigned long *index,
6530 return mt_find(mt, index, max);
6532 EXPORT_SYMBOL(mt_find_after);
6534 #ifdef CONFIG_DEBUG_MAPLE_TREE
6535 atomic_t maple_tree_tests_run;
6536 EXPORT_SYMBOL_GPL(maple_tree_tests_run);
6537 atomic_t maple_tree_tests_passed;
6538 EXPORT_SYMBOL_GPL(maple_tree_tests_passed);
6541 extern void kmem_cache_set_non_kernel(struct kmem_cache *, unsigned int);
6542 void mt_set_non_kernel(unsigned int val)
6544 kmem_cache_set_non_kernel(maple_node_cache, val);
6547 extern unsigned long kmem_cache_get_alloc(struct kmem_cache *);
6548 unsigned long mt_get_alloc_size(void)
6550 return kmem_cache_get_alloc(maple_node_cache);
6553 extern void kmem_cache_zero_nr_tallocated(struct kmem_cache *);
6554 void mt_zero_nr_tallocated(void)
6556 kmem_cache_zero_nr_tallocated(maple_node_cache);
6559 extern unsigned int kmem_cache_nr_tallocated(struct kmem_cache *);
6560 unsigned int mt_nr_tallocated(void)
6562 return kmem_cache_nr_tallocated(maple_node_cache);
6565 extern unsigned int kmem_cache_nr_allocated(struct kmem_cache *);
6566 unsigned int mt_nr_allocated(void)
6568 return kmem_cache_nr_allocated(maple_node_cache);
6572 * mas_dead_node() - Check if the maple state is pointing to a dead node.
6573 * @mas: The maple state
6574 * @index: The index to restore in @mas.
6576 * Used in test code.
6577 * Return: 1 if @mas has been reset to MAS_START, 0 otherwise.
6579 static inline int mas_dead_node(struct ma_state *mas, unsigned long index)
6581 if (unlikely(!mas_searchable(mas) || mas_is_start(mas)))
6584 if (likely(!mte_dead_node(mas->node)))
6587 mas_rewalk(mas, index);
6591 void mt_cache_shrink(void)
6596 * mt_cache_shrink() - For testing, don't use this.
6598 * Certain testcases can trigger an OOM when combined with other memory
6599 * debugging configuration options. This function is used to reduce the
6600 * possibility of an out of memory even due to kmem_cache objects remaining
6601 * around for longer than usual.
6603 void mt_cache_shrink(void)
6605 kmem_cache_shrink(maple_node_cache);
6608 EXPORT_SYMBOL_GPL(mt_cache_shrink);
6610 #endif /* not defined __KERNEL__ */
6612 * mas_get_slot() - Get the entry in the maple state node stored at @offset.
6613 * @mas: The maple state
6614 * @offset: The offset into the slot array to fetch.
6616 * Return: The entry stored at @offset.
6618 static inline struct maple_enode *mas_get_slot(struct ma_state *mas,
6619 unsigned char offset)
6621 return mas_slot(mas, ma_slots(mas_mn(mas), mte_node_type(mas->node)),
6627 * mas_first_entry() - Go the first leaf and find the first entry.
6628 * @mas: the maple state.
6629 * @limit: the maximum index to check.
6630 * @*r_start: Pointer to set to the range start.
6632 * Sets mas->offset to the offset of the entry, r_start to the range minimum.
6634 * Return: The first entry or MAS_NONE.
6636 static inline void *mas_first_entry(struct ma_state *mas, struct maple_node *mn,
6637 unsigned long limit, enum maple_type mt)
6641 unsigned long *pivots;
6645 mas->index = mas->min;
6646 if (mas->index > limit)
6651 while (likely(!ma_is_leaf(mt))) {
6652 MT_BUG_ON(mas->tree, mte_dead_node(mas->node));
6653 slots = ma_slots(mn, mt);
6654 entry = mas_slot(mas, slots, 0);
6655 pivots = ma_pivots(mn, mt);
6656 if (unlikely(ma_dead_node(mn)))
6661 mt = mte_node_type(mas->node);
6663 MT_BUG_ON(mas->tree, mte_dead_node(mas->node));
6666 slots = ma_slots(mn, mt);
6667 entry = mas_slot(mas, slots, 0);
6668 if (unlikely(ma_dead_node(mn)))
6671 /* Slot 0 or 1 must be set */
6672 if (mas->index > limit)
6679 entry = mas_slot(mas, slots, 1);
6680 pivots = ma_pivots(mn, mt);
6681 if (unlikely(ma_dead_node(mn)))
6684 mas->index = pivots[0] + 1;
6685 if (mas->index > limit)
6692 if (likely(!ma_dead_node(mn)))
6693 mas->node = MAS_NONE;
6697 /* Depth first search, post-order */
6698 static void mas_dfs_postorder(struct ma_state *mas, unsigned long max)
6701 struct maple_enode *p = MAS_NONE, *mn = mas->node;
6702 unsigned long p_min, p_max;
6704 mas_next_node(mas, mas_mn(mas), max);
6705 if (!mas_is_none(mas))
6708 if (mte_is_root(mn))
6713 while (mas->node != MAS_NONE) {
6717 mas_prev_node(mas, 0);
6728 /* Tree validations */
6729 static void mt_dump_node(const struct maple_tree *mt, void *entry,
6730 unsigned long min, unsigned long max, unsigned int depth);
6731 static void mt_dump_range(unsigned long min, unsigned long max,
6734 static const char spaces[] = " ";
6737 pr_info("%.*s%lu: ", depth * 2, spaces, min);
6739 pr_info("%.*s%lu-%lu: ", depth * 2, spaces, min, max);
6742 static void mt_dump_entry(void *entry, unsigned long min, unsigned long max,
6745 mt_dump_range(min, max, depth);
6747 if (xa_is_value(entry))
6748 pr_cont("value %ld (0x%lx) [%p]\n", xa_to_value(entry),
6749 xa_to_value(entry), entry);
6750 else if (xa_is_zero(entry))
6751 pr_cont("zero (%ld)\n", xa_to_internal(entry));
6752 else if (mt_is_reserved(entry))
6753 pr_cont("UNKNOWN ENTRY (%p)\n", entry);
6755 pr_cont("%p\n", entry);
6758 static void mt_dump_range64(const struct maple_tree *mt, void *entry,
6759 unsigned long min, unsigned long max, unsigned int depth)
6761 struct maple_range_64 *node = &mte_to_node(entry)->mr64;
6762 bool leaf = mte_is_leaf(entry);
6763 unsigned long first = min;
6766 pr_cont(" contents: ");
6767 for (i = 0; i < MAPLE_RANGE64_SLOTS - 1; i++)
6768 pr_cont("%p %lu ", node->slot[i], node->pivot[i]);
6769 pr_cont("%p\n", node->slot[i]);
6770 for (i = 0; i < MAPLE_RANGE64_SLOTS; i++) {
6771 unsigned long last = max;
6773 if (i < (MAPLE_RANGE64_SLOTS - 1))
6774 last = node->pivot[i];
6775 else if (!node->slot[i] && max != mt_node_max(entry))
6777 if (last == 0 && i > 0)
6780 mt_dump_entry(mt_slot(mt, node->slot, i),
6781 first, last, depth + 1);
6782 else if (node->slot[i])
6783 mt_dump_node(mt, mt_slot(mt, node->slot, i),
6784 first, last, depth + 1);
6789 pr_err("node %p last (%lu) > max (%lu) at pivot %d!\n",
6790 node, last, max, i);
6797 static void mt_dump_arange64(const struct maple_tree *mt, void *entry,
6798 unsigned long min, unsigned long max, unsigned int depth)
6800 struct maple_arange_64 *node = &mte_to_node(entry)->ma64;
6801 bool leaf = mte_is_leaf(entry);
6802 unsigned long first = min;
6805 pr_cont(" contents: ");
6806 for (i = 0; i < MAPLE_ARANGE64_SLOTS; i++)
6807 pr_cont("%lu ", node->gap[i]);
6808 pr_cont("| %02X %02X| ", node->meta.end, node->meta.gap);
6809 for (i = 0; i < MAPLE_ARANGE64_SLOTS - 1; i++)
6810 pr_cont("%p %lu ", node->slot[i], node->pivot[i]);
6811 pr_cont("%p\n", node->slot[i]);
6812 for (i = 0; i < MAPLE_ARANGE64_SLOTS; i++) {
6813 unsigned long last = max;
6815 if (i < (MAPLE_ARANGE64_SLOTS - 1))
6816 last = node->pivot[i];
6817 else if (!node->slot[i])
6819 if (last == 0 && i > 0)
6822 mt_dump_entry(mt_slot(mt, node->slot, i),
6823 first, last, depth + 1);
6824 else if (node->slot[i])
6825 mt_dump_node(mt, mt_slot(mt, node->slot, i),
6826 first, last, depth + 1);
6831 pr_err("node %p last (%lu) > max (%lu) at pivot %d!\n",
6832 node, last, max, i);
6839 static void mt_dump_node(const struct maple_tree *mt, void *entry,
6840 unsigned long min, unsigned long max, unsigned int depth)
6842 struct maple_node *node = mte_to_node(entry);
6843 unsigned int type = mte_node_type(entry);
6846 mt_dump_range(min, max, depth);
6848 pr_cont("node %p depth %d type %d parent %p", node, depth, type,
6849 node ? node->parent : NULL);
6853 for (i = 0; i < MAPLE_NODE_SLOTS; i++) {
6855 pr_cont("OUT OF RANGE: ");
6856 mt_dump_entry(mt_slot(mt, node->slot, i),
6857 min + i, min + i, depth);
6861 case maple_range_64:
6862 mt_dump_range64(mt, entry, min, max, depth);
6864 case maple_arange_64:
6865 mt_dump_arange64(mt, entry, min, max, depth);
6869 pr_cont(" UNKNOWN TYPE\n");
6873 void mt_dump(const struct maple_tree *mt)
6875 void *entry = rcu_dereference_check(mt->ma_root, mt_locked(mt));
6877 pr_info("maple_tree(%p) flags %X, height %u root %p\n",
6878 mt, mt->ma_flags, mt_height(mt), entry);
6879 if (!xa_is_node(entry))
6880 mt_dump_entry(entry, 0, 0, 0);
6882 mt_dump_node(mt, entry, 0, mt_node_max(entry), 0);
6884 EXPORT_SYMBOL_GPL(mt_dump);
6887 * Calculate the maximum gap in a node and check if that's what is reported in
6888 * the parent (unless root).
6890 static void mas_validate_gaps(struct ma_state *mas)
6892 struct maple_enode *mte = mas->node;
6893 struct maple_node *p_mn;
6894 unsigned long gap = 0, max_gap = 0;
6895 unsigned long p_end, p_start = mas->min;
6896 unsigned char p_slot;
6897 unsigned long *gaps = NULL;
6898 unsigned long *pivots = ma_pivots(mte_to_node(mte), mte_node_type(mte));
6901 if (ma_is_dense(mte_node_type(mte))) {
6902 for (i = 0; i < mt_slot_count(mte); i++) {
6903 if (mas_get_slot(mas, i)) {
6914 gaps = ma_gaps(mte_to_node(mte), mte_node_type(mte));
6915 for (i = 0; i < mt_slot_count(mte); i++) {
6916 p_end = mas_logical_pivot(mas, pivots, i, mte_node_type(mte));
6919 if (mas_get_slot(mas, i)) {
6924 gap += p_end - p_start + 1;
6926 void *entry = mas_get_slot(mas, i);
6930 if (gap != p_end - p_start + 1) {
6931 pr_err("%p[%u] -> %p %lu != %lu - %lu + 1\n",
6933 mas_get_slot(mas, i), gap,
6937 MT_BUG_ON(mas->tree,
6938 gap != p_end - p_start + 1);
6941 if (gap > p_end - p_start + 1) {
6942 pr_err("%p[%u] %lu >= %lu - %lu + 1 (%lu)\n",
6943 mas_mn(mas), i, gap, p_end, p_start,
6944 p_end - p_start + 1);
6945 MT_BUG_ON(mas->tree,
6946 gap > p_end - p_start + 1);
6954 p_start = p_end + 1;
6955 if (p_end >= mas->max)
6960 if (mte_is_root(mte))
6963 p_slot = mte_parent_slot(mas->node);
6964 p_mn = mte_parent(mte);
6965 MT_BUG_ON(mas->tree, max_gap > mas->max);
6966 if (ma_gaps(p_mn, mas_parent_enum(mas, mte))[p_slot] != max_gap) {
6967 pr_err("gap %p[%u] != %lu\n", p_mn, p_slot, max_gap);
6971 MT_BUG_ON(mas->tree,
6972 ma_gaps(p_mn, mas_parent_enum(mas, mte))[p_slot] != max_gap);
6975 static void mas_validate_parent_slot(struct ma_state *mas)
6977 struct maple_node *parent;
6978 struct maple_enode *node;
6979 enum maple_type p_type = mas_parent_enum(mas, mas->node);
6980 unsigned char p_slot = mte_parent_slot(mas->node);
6984 if (mte_is_root(mas->node))
6987 parent = mte_parent(mas->node);
6988 slots = ma_slots(parent, p_type);
6989 MT_BUG_ON(mas->tree, mas_mn(mas) == parent);
6991 /* Check prev/next parent slot for duplicate node entry */
6993 for (i = 0; i < mt_slots[p_type]; i++) {
6994 node = mas_slot(mas, slots, i);
6996 if (node != mas->node)
6997 pr_err("parent %p[%u] does not have %p\n",
6998 parent, i, mas_mn(mas));
6999 MT_BUG_ON(mas->tree, node != mas->node);
7000 } else if (node == mas->node) {
7001 pr_err("Invalid child %p at parent %p[%u] p_slot %u\n",
7002 mas_mn(mas), parent, i, p_slot);
7003 MT_BUG_ON(mas->tree, node == mas->node);
7008 static void mas_validate_child_slot(struct ma_state *mas)
7010 enum maple_type type = mte_node_type(mas->node);
7011 void __rcu **slots = ma_slots(mte_to_node(mas->node), type);
7012 unsigned long *pivots = ma_pivots(mte_to_node(mas->node), type);
7013 struct maple_enode *child;
7016 if (mte_is_leaf(mas->node))
7019 for (i = 0; i < mt_slots[type]; i++) {
7020 child = mas_slot(mas, slots, i);
7021 if (!pivots[i] || pivots[i] == mas->max)
7027 if (mte_parent_slot(child) != i) {
7028 pr_err("Slot error at %p[%u]: child %p has pslot %u\n",
7029 mas_mn(mas), i, mte_to_node(child),
7030 mte_parent_slot(child));
7031 MT_BUG_ON(mas->tree, 1);
7034 if (mte_parent(child) != mte_to_node(mas->node)) {
7035 pr_err("child %p has parent %p not %p\n",
7036 mte_to_node(child), mte_parent(child),
7037 mte_to_node(mas->node));
7038 MT_BUG_ON(mas->tree, 1);
7044 * Validate all pivots are within mas->min and mas->max.
7046 static void mas_validate_limits(struct ma_state *mas)
7049 unsigned long prev_piv = 0;
7050 enum maple_type type = mte_node_type(mas->node);
7051 void __rcu **slots = ma_slots(mte_to_node(mas->node), type);
7052 unsigned long *pivots = ma_pivots(mas_mn(mas), type);
7054 /* all limits are fine here. */
7055 if (mte_is_root(mas->node))
7058 for (i = 0; i < mt_slots[type]; i++) {
7061 piv = mas_safe_pivot(mas, pivots, i, type);
7063 if (!piv && (i != 0))
7066 if (!mte_is_leaf(mas->node)) {
7067 void *entry = mas_slot(mas, slots, i);
7070 pr_err("%p[%u] cannot be null\n",
7073 MT_BUG_ON(mas->tree, !entry);
7076 if (prev_piv > piv) {
7077 pr_err("%p[%u] piv %lu < prev_piv %lu\n",
7078 mas_mn(mas), i, piv, prev_piv);
7079 MT_BUG_ON(mas->tree, piv < prev_piv);
7082 if (piv < mas->min) {
7083 pr_err("%p[%u] %lu < %lu\n", mas_mn(mas), i,
7085 MT_BUG_ON(mas->tree, piv < mas->min);
7087 if (piv > mas->max) {
7088 pr_err("%p[%u] %lu > %lu\n", mas_mn(mas), i,
7090 MT_BUG_ON(mas->tree, piv > mas->max);
7093 if (piv == mas->max)
7096 for (i += 1; i < mt_slots[type]; i++) {
7097 void *entry = mas_slot(mas, slots, i);
7099 if (entry && (i != mt_slots[type] - 1)) {
7100 pr_err("%p[%u] should not have entry %p\n", mas_mn(mas),
7102 MT_BUG_ON(mas->tree, entry != NULL);
7105 if (i < mt_pivots[type]) {
7106 unsigned long piv = pivots[i];
7111 pr_err("%p[%u] should not have piv %lu\n",
7112 mas_mn(mas), i, piv);
7113 MT_BUG_ON(mas->tree, i < mt_pivots[type] - 1);
7118 static void mt_validate_nulls(struct maple_tree *mt)
7120 void *entry, *last = (void *)1;
7121 unsigned char offset = 0;
7123 MA_STATE(mas, mt, 0, 0);
7126 if (mas_is_none(&mas) || (mas.node == MAS_ROOT))
7129 while (!mte_is_leaf(mas.node))
7132 slots = ma_slots(mte_to_node(mas.node), mte_node_type(mas.node));
7134 entry = mas_slot(&mas, slots, offset);
7135 if (!last && !entry) {
7136 pr_err("Sequential nulls end at %p[%u]\n",
7137 mas_mn(&mas), offset);
7139 MT_BUG_ON(mt, !last && !entry);
7141 if (offset == mas_data_end(&mas)) {
7142 mas_next_node(&mas, mas_mn(&mas), ULONG_MAX);
7143 if (mas_is_none(&mas))
7146 slots = ma_slots(mte_to_node(mas.node),
7147 mte_node_type(mas.node));
7152 } while (!mas_is_none(&mas));
7156 * validate a maple tree by checking:
7157 * 1. The limits (pivots are within mas->min to mas->max)
7158 * 2. The gap is correctly set in the parents
7160 void mt_validate(struct maple_tree *mt)
7164 MA_STATE(mas, mt, 0, 0);
7167 if (!mas_searchable(&mas))
7170 mas_first_entry(&mas, mas_mn(&mas), ULONG_MAX, mte_node_type(mas.node));
7171 while (!mas_is_none(&mas)) {
7172 MT_BUG_ON(mas.tree, mte_dead_node(mas.node));
7173 if (!mte_is_root(mas.node)) {
7174 end = mas_data_end(&mas);
7175 if ((end < mt_min_slot_count(mas.node)) &&
7176 (mas.max != ULONG_MAX)) {
7177 pr_err("Invalid size %u of %p\n", end,
7179 MT_BUG_ON(mas.tree, 1);
7183 mas_validate_parent_slot(&mas);
7184 mas_validate_child_slot(&mas);
7185 mas_validate_limits(&mas);
7186 if (mt_is_alloc(mt))
7187 mas_validate_gaps(&mas);
7188 mas_dfs_postorder(&mas, ULONG_MAX);
7190 mt_validate_nulls(mt);
7195 EXPORT_SYMBOL_GPL(mt_validate);
7197 #endif /* CONFIG_DEBUG_MAPLE_TREE */