Commit adfd3b65 authored by Khazhismel Kumykov's avatar Khazhismel Kumykov Committed by Li Lingfeng
Browse files

blk-throttle: check for overflow in calculate_bytes_allowed

mainline inclusion
from mainline-v6.7-rc1
commit 2dd710d4
category: bugfix
bugzilla: https://gitee.com/openeuler/kernel/issues/IA8QFN

Reference: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=2dd710d476f2f1f6eaca884f625f69ef4389ed40



--------------------------------

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: e8368b57 ("blk-throttle: use calculate_io/bytes_allowed() for throtl_trim_slice()")
Fixes: 8d6bbaad ("blk-throttle: prevent overflow while calculating wait time")
Signed-off-by: default avatarKhazhismel Kumykov <khazhy@google.com>
Acked-by: default avatarTejun Heo <tj@kernel.org>
Link: https://lore.kernel.org/r/20231020223617.2739774-1-khazhy@google.com


Signed-off-by: default avatarJens Axboe <axboe@kernel.dk>
Signed-off-by: default avatarLi Lingfeng <lilingfeng3@huawei.com>
parent 378efac3
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -879,6 +879,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);
}