]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
btrfs: reduce block group critical section in do_trimming()
authorFilipe Manana <fdmanana@suse.com>
Mon, 20 Oct 2025 12:17:23 +0000 (13:17 +0100)
committerDavid Sterba <dsterba@suse.com>
Mon, 24 Nov 2025 21:15:09 +0000 (22:15 +0100)
There's no need to update the bytes_reserved and bytes_readonly fields of
the space_info while holding the block group's spinlock. We are only
making the critical section longer than necessary. So move the space_info
updates outside of the block group's critical section.

Reviewed-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
Signed-off-by: Filipe Manana <fdmanana@suse.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/free-space-cache.c

index ab873bd6719209c4978ec534e41f6c4820900b3d..6ccb492eae8e31716e7f3b7955f3d5c620d4352e 100644 (file)
@@ -3656,7 +3656,7 @@ static int do_trimming(struct btrfs_block_group *block_group,
        struct btrfs_fs_info *fs_info = block_group->fs_info;
        struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
        int ret;
-       int update = 0;
+       bool bg_ro;
        const u64 end = start + bytes;
        const u64 reserved_end = reserved_start + reserved_bytes;
        enum btrfs_trim_state trim_state = BTRFS_TRIM_STATE_UNTRIMMED;
@@ -3664,12 +3664,14 @@ static int do_trimming(struct btrfs_block_group *block_group,
 
        spin_lock(&space_info->lock);
        spin_lock(&block_group->lock);
-       if (!block_group->ro) {
+       bg_ro = block_group->ro;
+       if (!bg_ro) {
                block_group->reserved += reserved_bytes;
+               spin_unlock(&block_group->lock);
                space_info->bytes_reserved += reserved_bytes;
-               update = 1;
+       } else {
+               spin_unlock(&block_group->lock);
        }
-       spin_unlock(&block_group->lock);
        spin_unlock(&space_info->lock);
 
        ret = btrfs_discard_extent(fs_info, start, bytes, &trimmed);
@@ -3690,14 +3692,16 @@ static int do_trimming(struct btrfs_block_group *block_group,
        list_del(&trim_entry->list);
        mutex_unlock(&ctl->cache_writeout_mutex);
 
-       if (update) {
+       if (!bg_ro) {
                spin_lock(&space_info->lock);
                spin_lock(&block_group->lock);
-               if (block_group->ro)
-                       space_info->bytes_readonly += reserved_bytes;
+               bg_ro = block_group->ro;
                block_group->reserved -= reserved_bytes;
-               space_info->bytes_reserved -= reserved_bytes;
                spin_unlock(&block_group->lock);
+
+               space_info->bytes_reserved -= reserved_bytes;
+               if (bg_ro)
+                       space_info->bytes_readonly += reserved_bytes;
                spin_unlock(&space_info->lock);
        }