]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
btrfs: fix u32 to s64 type conversion in dirty_metadata_bytes accounting
authorDave Chen <davechen@synology.com>
Mon, 29 Jun 2026 07:08:43 +0000 (15:08 +0800)
committerDavid Sterba <dsterba@suse.com>
Tue, 14 Jul 2026 05:04:19 +0000 (07:04 +0200)
The percpu_counter dirty_metadata_bytes is updated by negating eb->len
and passing it to percpu_counter_add_batch(), whose amount parameter is
s64.  Since commit 84cda1a6087d ("btrfs: cache folio size and shift in
extent_buffer"), eb->len is u32.  The u32 result of -eb->len, when
widened to the s64 parameter, becomes a large positive value instead of
the intended negative value.  For eb->len == 16384 the counter adds
+4294950912 instead of subtracting 16384.

The counter therefore grows on every metadata writeback instead of
shrinking by the extent buffer size, permanently exceeding
BTRFS_DIRTY_METADATA_THRESH and causing __btrfs_btree_balance_dirty()
to trigger balance_dirty_pages_ratelimited() unconditionally, adding
unnecessary writeback pressure.

Cast eb->len to s64 before negation at both call sites so the
subtraction is performed in signed 64-bit arithmetic.

Reviewed-by: Filipe Manana <fdmanana@suse.com>
Fixes: 84cda1a6087d ("btrfs: cache folio size and shift in extent_buffer")
Signed-off-by: Dave Chen <davechen@synology.com>
Signed-off-by: Filipe Manana <fdmanana@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/extent_io.c

index 7d604524e83c33ed70d601038d987926d5448205..de5785117a47f09f227e3bfa04302e6a843826dc 100644 (file)
@@ -2004,7 +2004,7 @@ static noinline_for_stack bool lock_extent_buffer_for_io(struct extent_buffer *e
 
                btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
                percpu_counter_add_batch(&fs_info->dirty_metadata_bytes,
-                                        -eb->len,
+                                        -(s64)eb->len,
                                         fs_info->dirty_metadata_batch);
                ret = true;
        } else {
@@ -3774,7 +3774,7 @@ void btrfs_clear_buffer_dirty(struct btrfs_trans_handle *trans,
                return;
 
        buffer_tree_clear_mark(eb, PAGECACHE_TAG_DIRTY);
-       percpu_counter_add_batch(&fs_info->dirty_metadata_bytes, -eb->len,
+       percpu_counter_add_batch(&fs_info->dirty_metadata_bytes, -(s64)eb->len,
                                 fs_info->dirty_metadata_batch);
 
        for (int i = 0; i < num_extent_folios(eb); i++) {