Skip to contentSkip to conversation

blk-throttle: check for overflow in calculate_bytes_allowed

PATCH Applied Khazhismel Kumykov 3 participants 3 messages Oct 20, 2023 – Oct 21, 2023

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 blk-throttle: check for overflow in calculate_bytes_allowed

Inexact, we may reject some not-overflowing values incorrectly, but they'll be on the order of exabytes allowed anyways.

This fixes divide error crash on x86 if bps_limit is not configured or is set too high in the rare case that jiffy_elapsed is greater than HZ.

Fixes e8368b57c006 ("blk-throttle: use calculate_io/bytes_allowed() for throtl_trim_slice()")
Fixes 8d6bbaada2e0 ("blk-throttle: prevent overflow while calculating wait time")
---
 block/blk-throttle.c | 6 ++++++
 1 file changed, 6 insertions(+)
block/blk-throttle.c +6 -0
index 38a881cf97d0..13e4377a8b28 100644
@@ -723,6 +723,12 @@ static unsigned int calculate_io_allowed(u32 iops_limit,
 
 static u64 calculate_bytes_allowed(u64 bps_limit, unsigned long jiffy_elapsed)
 {
+	/*
+	 * Can result be wider than 64 bits?
+	 * We check against 62, not 64, due to ilog2 truncation.
+	 */
+	if (ilog2(bps_limit) + ilog2(jiffy_elapsed) - ilog2(HZ) > 62)
+		return U64_MAX;
 	return mul_u64_u64_div_u64(bps_limit, (u64)jiffy_elapsed, (u64)HZ);
 }
 
Signature · 1 line
2.42.0.655.g421f12c284-goog
maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 9 lines · Khazhismel Kumykov

Inexact, we may reject some not-overflowing values incorrectly, but they'll be on the order of exabytes allowed anyways.

This fixes divide error crash on x86 if bps_limit is not configured or is set too high in the rare case that jiffy_elapsed is greater than HZ.

Fixes e8368b57c006 ("blk-throttle: use calculate_io/bytes_allowed() for throtl_trim_slice()")
Fixes 8d6bbaada2e0 ("blk-throttle: prevent overflow while calculating wait time")

Thanks.

Signature · 1 line
tejun
maintainer Applied
#2 lore raw
In reply to Khazhismel Kumykov
Quoted 8 lines · Khazhismel Kumykov

Inexact, we may reject some not-overflowing values incorrectly, but they'll be on the order of exabytes allowed anyways.

This fixes divide error crash on x86 if bps_limit is not configured or is set too high in the rare case that jiffy_elapsed is greater than HZ.

[...]

Applied, thanks!

[1/1] blk-throttle: check for overflow in calculate_bytes_allowed

      commit: 2dd710d476f2f1f6eaca884f625f69ef4389ed40

Best regards,

Signature · 1 line
Jens Axboe