Skip to contentSkip to conversation

mm/page-writeback: avoid overflow when reporting BDI byte limits

PATCH In review Hungyu Lin 2 participants 2 messages Nov 3 – Nov 5

Subsystems (4)
lore
Share & tools
lore

Hungyu Lin

author
#0 lore raw

PATCH 1/1 mm/page-writeback: avoid overflow when reporting BDI byte limits

bdi_get_bytes() multiplies unsigned long operands before assigning the result to u64. On 32-bit kernels, the intermediate product wraps and min_bytes and max_bytes report incorrect limits. For example, with vm.dirty_bytes set to 104857600 and max_ratio set to 100, max_bytes reports 268 instead of 104857600.

Convert the page count to bytes in u64 and use mul_u64_u32_div() to avoid overflow in the subsequent multiplication. This also handles large dirty limits on systems with larger pages. Combining the two divisions keeps the existing rounding behavior.

Fixes 00df7d51263b ("mm: add bdi_get_max_bytes() function")
---
 mm/page-writeback.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
mm/page-writeback.c +2 -1
index 499a35473e4f..ddebec79d8d9 100644
@@ -665,7 +665,8 @@ static u64 bdi_get_bytes(unsigned int ratio)
 	u64 bytes;
 
 	global_dirty_limits(&background_thresh, &dirty_thresh);
-	bytes = (dirty_thresh * PAGE_SIZE * ratio) / BDI_RATIO_SCALE / 100;
+	bytes = mul_u64_u32_div((u64)dirty_thresh << PAGE_SHIFT, ratio,
+				100 * BDI_RATIO_SCALE);
 
 	return bytes;
 }
Signature · 1 line
2.43.0
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 6 lines · Hungyu Lin

bdi_get_bytes() multiplies unsigned long operands before assigning the result to u64. On 32-bit kernels, the intermediate product wraps and min_bytes and max_bytes report incorrect limits. For example, with vm.dirty_bytes set to 104857600 and max_ratio set to 100, max_bytes reports 268 instead of 104857600.

Quoted 8 lines

Convert the page count to bytes in u64 and use mul_u64_u32_div() to avoid overflow in the subsequent multiplication. This also handles large dirty limits on systems with larger pages. Combining the two divisions keeps the existing rounding behavior.

Fixes 00df7d51263b ("mm: add bdi_get_max_bytes() function")

Looks good. Feel free to add:

Honza

Quoted 21 lines
---
 mm/page-writeback.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
mm/page-writeback.c +2 -1
index 499a35473e4f..ddebec79d8d9 100644
@@ -665,7 +665,8 @@ static u64 bdi_get_bytes(unsigned int ratio)
 	u64 bytes;
 
 	global_dirty_limits(&background_thresh, &dirty_thresh);
-	bytes = (dirty_thresh * PAGE_SIZE * ratio) / BDI_RATIO_SCALE / 100;
+	bytes = mul_u64_u32_div((u64)dirty_thresh << PAGE_SHIFT, ratio,
+				100 * BDI_RATIO_SCALE);
 
 	return bytes;
 }
Signature · 1 line
2.43.0
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR