]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
btrfs: shorten critical section in btrfs_preempt_reclaim_metadata_space()
authorFilipe Manana <fdmanana@suse.com>
Fri, 17 Oct 2025 16:07:22 +0000 (17:07 +0100)
committerDavid Sterba <dsterba@suse.com>
Mon, 24 Nov 2025 21:09:46 +0000 (22:09 +0100)
We are doing a lot of small calculations and assignments while holding the
space_info's spinlock, which is a heavily used lock for space reservation
and flushing. There's no point in holding the lock for so long when all we
want is to call need_preemptive_reclaim() and get a consistent value for a
couple of counters from the space_info. Instead, grab the counters into
local variables, release the lock and then use the local variables.

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/space-info.c

index 2dd9d4e5c2c22cb3f4ea1e369ffed26bf2bdab8c..9a072009eec8fe777c15243406d77d5664937588 100644 (file)
@@ -1263,7 +1263,10 @@ static void btrfs_preempt_reclaim_metadata_space(struct work_struct *work)
                u64 delalloc_size = 0;
                u64 to_reclaim, block_rsv_size;
                const u64 global_rsv_size = btrfs_block_rsv_reserved(global_rsv);
+               const u64 bytes_may_use = space_info->bytes_may_use;
+               const u64 bytes_pinned = space_info->bytes_pinned;
 
+               spin_unlock(&space_info->lock);
                /*
                 * We don't have a precise counter for the metadata being
                 * reserved for delalloc, so we'll approximate it by subtracting
@@ -1275,8 +1278,8 @@ static void btrfs_preempt_reclaim_metadata_space(struct work_struct *work)
                        btrfs_block_rsv_reserved(delayed_block_rsv) +
                        btrfs_block_rsv_reserved(delayed_refs_rsv) +
                        btrfs_block_rsv_reserved(trans_rsv);
-               if (block_rsv_size < space_info->bytes_may_use)
-                       delalloc_size = space_info->bytes_may_use - block_rsv_size;
+               if (block_rsv_size < bytes_may_use)
+                       delalloc_size = bytes_may_use - block_rsv_size;
 
                /*
                 * We don't want to include the global_rsv in our calculation,
@@ -1293,10 +1296,10 @@ static void btrfs_preempt_reclaim_metadata_space(struct work_struct *work)
                if (delalloc_size > block_rsv_size) {
                        to_reclaim = delalloc_size;
                        flush = FLUSH_DELALLOC;
-               } else if (space_info->bytes_pinned >
+               } else if (bytes_pinned >
                           (btrfs_block_rsv_reserved(delayed_block_rsv) +
                            btrfs_block_rsv_reserved(delayed_refs_rsv))) {
-                       to_reclaim = space_info->bytes_pinned;
+                       to_reclaim = bytes_pinned;
                        flush = COMMIT_TRANS;
                } else if (btrfs_block_rsv_reserved(delayed_block_rsv) >
                           btrfs_block_rsv_reserved(delayed_refs_rsv)) {
@@ -1307,8 +1310,6 @@ static void btrfs_preempt_reclaim_metadata_space(struct work_struct *work)
                        flush = FLUSH_DELAYED_REFS_NR;
                }
 
-               spin_unlock(&space_info->lock);
-
                loops++;
 
                /*