]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
btrfs: optimize balance_level() path reference handling
authorSun YangKai <sunk67188@gmail.com>
Fri, 14 Nov 2025 07:24:46 +0000 (15:24 +0800)
committerDavid Sterba <dsterba@suse.com>
Tue, 25 Nov 2025 00:50:56 +0000 (01:50 +0100)
Instead of incrementing refcount on 'left' node when it's referenced by
path, simply transfer ownership to path and set left to NULL. This
eliminates:

- Unnecessary refcount increment/decrement operations
- Redundant conditional checks for left node cleanup

The path now consistently owns the left node reference when used.

Signed-off-by: Sun YangKai <sunk67188@gmail.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/ctree.c

index 3abddd2cdfd38463360e8cb88b1455c2671b0c9e..4df4c6cda620d8fb19adb16ecb7d50bc2c7ade45 100644 (file)
@@ -1125,11 +1125,12 @@ static noinline int balance_level(struct btrfs_trans_handle *trans,
        /* update the path */
        if (left) {
                if (btrfs_header_nritems(left) > orig_slot) {
-                       refcount_inc(&left->refs);
                        /* left was locked after cow */
                        path->nodes[level] = left;
                        path->slots[level + 1] -= 1;
                        path->slots[level] = orig_slot;
+                       /* Left is now owned by path. */
+                       left = NULL;
                        if (mid) {
                                btrfs_tree_unlock(mid);
                                free_extent_buffer(mid);
@@ -1149,8 +1150,7 @@ out:
                free_extent_buffer(right);
        }
        if (left) {
-               if (path->nodes[level] != left)
-                       btrfs_tree_unlock(left);
+               btrfs_tree_unlock(left);
                free_extent_buffer(left);
        }
        return ret;