]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
btrfs: avoid used space computation when reserving space
authorFilipe Manana <fdmanana@suse.com>
Fri, 17 Oct 2025 15:10:04 +0000 (16:10 +0100)
committerDavid Sterba <dsterba@suse.com>
Mon, 24 Nov 2025 21:07:36 +0000 (22:07 +0100)
In __reserve_bytes() we have 3 repeated calls to btrfs_space_info_used(),
one early on as soon as take the space_info's spinlock, another one when
we call btrfs_can_overcommit(), which calls btrfs_space_info_used() again,
and a final one when we are reserving for a flush emergency.

During all these calls we are holding the space_info's spinlock, which is
heavily used by the space reservation and flushing code, so it's desirable
to make the critical sections as short as possible.

So make this more efficient by:

1) Instead of calling btrfs_can_overcommit() call the new variant
   can_overcommit() which takes the space_info's used space as an argument
   and pass the value we already computed and have in the 'used' variable;

2) Instead of calling btrfs_space_info_used() with its second argument as
   false when we are doing a flush emergency, decrement the space_info's
   bytes_may_use counter from the 'used' variable, as the difference
   between passing true or false as the second argument to
   btrfs_space_info_used() is whether or not to include the space_info's
   bytes_may_use counter in the computation.

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

index f5ff51680f41240dc10eb461f3464860243252e4..6c2769044b552576e96f42f049a0e16fd6186e5f 100644 (file)
@@ -1783,7 +1783,7 @@ static int __reserve_bytes(struct btrfs_space_info *space_info, u64 orig_bytes,
         */
        if (!pending_tickets &&
            ((used + orig_bytes <= space_info->total_bytes) ||
-            btrfs_can_overcommit(space_info, orig_bytes, flush))) {
+            can_overcommit(space_info, used, orig_bytes, flush))) {
                btrfs_space_info_update_bytes_may_use(space_info, orig_bytes);
                ret = 0;
        }
@@ -1794,7 +1794,7 @@ static int __reserve_bytes(struct btrfs_space_info *space_info, u64 orig_bytes,
         * left to allocate for the block.
         */
        if (ret && unlikely(flush == BTRFS_RESERVE_FLUSH_EMERGENCY)) {
-               used = btrfs_space_info_used(space_info, false);
+               used -= space_info->bytes_may_use;
                if (used + orig_bytes <= space_info->total_bytes) {
                        btrfs_space_info_update_bytes_may_use(space_info, orig_bytes);
                        ret = 0;