Skip to contentSkip to conversation

btrfs: fix u32 to s64 type conversion in dirty_metadata_bytes accounting

PATCH In review Dave Chen 3 participants 4 messages Jun 29 – Jun 30 Btrfs file system

lore
Share & tools
lore

Dave Chen

author
#0 lore raw

PATCH 1/1 btrfs: fix u32 to s64 type conversion in dirty_metadata_bytes accounting

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.

---
 fs/btrfs/extent_io.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
fs/btrfs/extent_io.c +2 -2
index 7d604524e83c..de5785117a47 100644
@@ -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++) {
Signature · 4 lines
2.43.0


Disclaimer: The contents of this e-mail message and any attachments are confidential and are intended solely for addressee. The information may also be legally privileged. This transmission is sent in trust, for the sole purpose of delivery to the intended recipient. If you have received this transmission in error, any use, reproduction or dissemination of this transmission is strictly prohibited. If you are not the intended recipient, please immediately notify the sender by reply e-mail or phone and delete this message and its attachments, if any.
Reviewed-by on v1 1/1
#1 lore raw
Quoted 15 lines · Dave Chen

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.

Quoted 4 lines

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

Curious, do you have any performance data to give an idea of the impact?

I'm asking not only for clarity but also to determine if adding a Fixes tag for stable backports makes sense.
The commit that caused this issue is 84cda1a6087d ("btrfs: cache folio size and shift in extent_buffer"), which changed struct extent_buffer::len from unsigned long (64-bit on 64 bits platforms) to u32.

Thanks.

Quoted 32 lines
---
 fs/btrfs/extent_io.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
fs/btrfs/extent_io.c +2 -3
index 7d604524e83c..de5785117a47 100644
@@ -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++) {
--

2.43.0

Disclaimer: The contents of this e-mail message and any attachments are confidential and are intended solely for addressee. The information may also be legally privileged. This transmission is sent in trust, for the sole purpose of delivery to the intended recipient. If you have received this transmission in error, any use, reproduction or dissemination of this transmission is strictly prohibited. If you are not the intended recipient, please immediately notify the sender by reply e-mail or phone and delete this message and its attachments, if any.

#2 lore raw
Quoted 21 lines · Filipe Manana
Quoted 15 lines · Dave Chen

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.

Quoted 4 lines

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

Quoted 8 lines

Curious, do you have any performance data to give an idea of the impact?

I'm asking not only for clarity but also to determine if adding a Fixes tag for stable backports makes sense.
The commit that caused this issue is 84cda1a6087d ("btrfs: cache folio size and shift in extent_buffer"), which changed struct extent_buffer::len from unsigned long (64-bit on 64 bits platforms) to u32.

Oh my that's very well hidden signedness bug. I'd rather not rely on the correct implicit conversions and add a helper like percpu_counter_sub_batch() that takes the positive value to subtract and correctly casts it to s64 inside.

We have more places with negative unsigned value passed to percpu_counter_add_batch(), e.g. in btrfs_remove_ordered_extent(). The entry->num_bytes is u64 so it's correct.

In the rest of kernel the negative values are not used frequently, in xfs, networking, bpf.

This fix can go in as-is as it's minimal, the API update can follow so we can be sure gems like

lib/flex_proportions.c:fprop_reflect_period_percpu()

		percpu_counter_add_batch(&pl->events,
                       -val + (val >> (period-pl->period)), PROP_BATCH);

can be made more understandable.

author
#3 lore raw
In reply to Filipe Manana
Quoted 4 lines · Filipe Manana

Curious, do you have any performance data to give an idea of the impact?

I'm asking not only for clarity but also to determine if adding a Fixes tag for stable backports makes sense.

We don't have upstream performance benchmark data for this. We discovered the issue while debugging an unrelated internal bug — dirty_metadata_bytes had grown to petabyte-range values on a filesystem with a fraction of that in actual dirty metadata, which led us to trace it back to the u32 negation.

The writeback impact described in the commit message (balance_dirty_pages_ratelimited() being called unconditionally) is based on code analysis, not measured.

I'll leave the Fixes tag decision to you and David.

Thanks for the review.

Disclaimer: The contents of this e-mail message and any attachments are confidential and are intended solely for addressee. The information may also be legally privileged. This transmission is sent in trust, for the sole purpose of delivery to the intended recipient. If you have received this transmission in error, any use, reproduction or dissemination of this transmission is strictly prohibited. If you are not the intended recipient, please immediately notify the sender by reply e-mail or phone and delete this message and its attachments, if any.