]> www.infradead.org Git - users/jedix/linux-maple.git/commitdiff
maple_tree: Move maple state by using parent in split and rebalance
authorLiam R. Howlett <Liam.Howlett@oracle.com>
Wed, 8 Oct 2025 23:57:27 +0000 (19:57 -0400)
committerLiam R. Howlett <Liam.Howlett@oracle.com>
Thu, 9 Oct 2025 17:35:30 +0000 (13:35 -0400)
Instead of copying the parent into the wr_mas->mas, set necessary values
in the maple state to what exists in the parent.

Signed-off-by: Liam R. Howlett <Liam.Howlett@oracle.com>
lib/maple_tree.c

index a5fb25cf3bf5038c76ffb8bf8c93bbc6dd2cb71b..4adad4e949029dc236150603f3d8f9cfd8f5d10e 100644 (file)
@@ -2899,6 +2899,17 @@ static bool spanning_ascend(struct maple_copy *cp, struct ma_state *mas,
        return true;
 }
 
+static inline
+void copy_tree_location(const struct ma_state *src, struct ma_state *dst)
+{
+       dst->node = src->node;
+       dst->offset = src->offset;
+       dst->min = src->min;
+       dst->max = src->max;
+       dst->end = src->end;
+       dst->depth = src->depth;
+}
+
 /*
  * rebalance_ascend() - Ascend the tree and set up for the next loop - if
  * necessary
@@ -2911,22 +2922,24 @@ static inline bool rebalance_ascend(struct maple_copy *cp,
                struct ma_state *parent)
 {
        struct ma_state *mas;
-       unsigned long min;
-       struct ma_state *r;
+       unsigned long min, max;
 
        mas = wr_mas->mas;
        if (!sib->end) {
                min = mas->min;
-               r = mas;
+               max = mas->max;
        } else if (sib->min > mas->max) { /* Move right succeeded */
                min = mas->min;
-               r = sib;
+               max = sib->max;
+               wr_mas->offset_end = parent->offset + 1;
        } else {
                min = sib->min;
-               r = mas;
+               max = mas->max;
+               wr_mas->offset_end = parent->offset;
+               parent->offset--;
        }
 
-       cp_dst_to_slots(cp, min, r->max, mas);
+       cp_dst_to_slots(cp, min, max, mas);
        if (cp_is_new_root(cp, mas))
                return false;
 
@@ -2934,20 +2947,11 @@ static inline bool rebalance_ascend(struct maple_copy *cp,
                return false;
 
        cp->height++;
-       parent->alloc = mas->alloc;
-       *mas = *parent;
+       copy_tree_location(parent, mas);
        wr_mas_setup(wr_mas, mas);
-       wr_mas->offset_end = mas->offset;
-       if (r == sib)
-               wr_mas->offset_end++;
-       else
-               wr_mas->mas->offset--;
-
        return true;
 }
 
-
-
 /*
  * mas_destroy_rebalance() - Rebalance left-most node while destroying the maple
  * state.
@@ -3739,11 +3743,15 @@ static inline bool split_ascend(struct maple_copy *cp,
        mas = wr_mas->mas;
        min = mas->min; /* push right, or normal split */
        max = mas->max;
+       wr_mas->offset_end = parent->offset;
        if (sib->end) {
-               if (sib->max < mas->min)
+               if (sib->max < mas->min) {
                        min = sib->min; /* push left */
-               else
+                       parent->offset--;
+               } else {
                        max = sib->max; /* push right */
+                       wr_mas->offset_end++;
+               }
        }
 
        cp_dst_to_slots(cp, min, max, mas);
@@ -3755,17 +3763,8 @@ static inline bool split_ascend(struct maple_copy *cp,
 
        cp->height++;
        BUG_ON(mas->node == parent->node);
-       parent->alloc = mas->alloc;
-       *mas = *parent;
+       copy_tree_location(parent, mas);
        wr_mas_setup(wr_mas, mas);
-       wr_mas->offset_end = mas->offset;
-       if (sib->end) {
-               if (sib->min == min)
-                       wr_mas->mas->offset--;
-               else
-                       wr_mas->offset_end++;
-       }
-
        return true;
 }
 
@@ -3802,14 +3801,14 @@ static inline void split_data(struct maple_copy *cp,
  */
 static void mas_wr_split(struct ma_wr_state *wr_mas)
 {
+       struct ma_state parent;
        struct ma_state *mas;
        struct maple_copy cp;
        struct ma_state sib;
-       struct ma_state parent;
 
        mas = wr_mas->mas;
-       parent = *mas;
        trace_ma_op(__func__, mas);
+       parent = *mas;
        cp_leaf_init(&cp, mas, wr_mas, wr_mas);
        do {
                if (!mte_is_root(parent.node)) {
@@ -3836,10 +3835,10 @@ static void mas_wr_split(struct ma_wr_state *wr_mas)
  */
 static void mas_wr_rebalance(struct ma_wr_state *wr_mas)
 {
+       struct ma_state parent;
        struct ma_state *mas;
        struct maple_copy cp;
        struct ma_state sib;
-       struct ma_state parent;
 
        /*
         * Rebalancing occurs if a node is insufficient.  Data is rebalanced