[PATCH v3 2/2] blk-throttle: kill the no longer needed overflow check in calculate_bytes_allowed()

Oleg Nesterov posted 2 patches 1 month, 2 weeks ago
[PATCH v3 2/2] blk-throttle: kill the no longer needed overflow check in calculate_bytes_allowed()
Posted by Oleg Nesterov 1 month, 2 weeks ago
Now that mul_u64_u64_div_u64() can't crash there is no need to check for
possible overflow in calculate_bytes_allowed().

Signed-off-by: Oleg Nesterov <oleg@redhat.com>
---
 block/blk-throttle.c | 6 ------
 1 file changed, 6 deletions(-)

diff --git a/block/blk-throttle.c b/block/blk-throttle.c
index 397b6a410f9e..66339e22cc85 100644
--- a/block/blk-throttle.c
+++ b/block/blk-throttle.c
@@ -601,12 +601,6 @@ 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);
 }
 
-- 
2.25.1.362.g51ebf55
Re: [PATCH v3 2/2] blk-throttle: kill the no longer needed overflow check in calculate_bytes_allowed()
Posted by David Laight 1 month, 2 weeks ago
On Fri, 15 Aug 2025 18:41:02 +0200
Oleg Nesterov <oleg@redhat.com> wrote:

> Now that mul_u64_u64_div_u64() can't crash there is no need to check for
> possible overflow in calculate_bytes_allowed().
> 
> Signed-off-by: Oleg Nesterov <oleg@redhat.com>
> ---
>  block/blk-throttle.c | 6 ------
>  1 file changed, 6 deletions(-)
> 
> diff --git a/block/blk-throttle.c b/block/blk-throttle.c
> index 397b6a410f9e..66339e22cc85 100644
> --- a/block/blk-throttle.c
> +++ b/block/blk-throttle.c
> @@ -601,12 +601,6 @@ 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);

Not directly related, but the two (u64) casts are pointless and can be removed.

	David

>  }
>
Re: [PATCH v3 2/2] blk-throttle: kill the no longer needed overflow check in calculate_bytes_allowed()
Posted by Oleg Nesterov 1 month, 2 weeks ago
On 08/17, David Laight wrote:
>
> On Fri, 15 Aug 2025 18:41:02 +0200
> Oleg Nesterov <oleg@redhat.com> wrote:
>
> > Now that mul_u64_u64_div_u64() can't crash there is no need to check for
> > possible overflow in calculate_bytes_allowed().
> >
> > Signed-off-by: Oleg Nesterov <oleg@redhat.com>
> > ---
> >  block/blk-throttle.c | 6 ------
> >  1 file changed, 6 deletions(-)
> >
> > diff --git a/block/blk-throttle.c b/block/blk-throttle.c
> > index 397b6a410f9e..66339e22cc85 100644
> > --- a/block/blk-throttle.c
> > +++ b/block/blk-throttle.c
> > @@ -601,12 +601,6 @@ 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);
>
> Not directly related, but the two (u64) casts are pointless and can be removed.

Yeah... I only reverted 2dd710d476f2 ("blk-throttle: check for overflow in
calculate_bytes_allowed")

Oleg.
Re: [PATCH v3 2/2] blk-throttle: kill the no longer needed overflow check in calculate_bytes_allowed()
Posted by H. Peter Anvin 1 month, 2 weeks ago
On August 17, 2025 5:50:13 AM PDT, David Laight <david.laight.linux@gmail.com> wrote:
>On Fri, 15 Aug 2025 18:41:02 +0200
>Oleg Nesterov <oleg@redhat.com> wrote:
>
>> Now that mul_u64_u64_div_u64() can't crash there is no need to check for
>> possible overflow in calculate_bytes_allowed().
>> 
>> Signed-off-by: Oleg Nesterov <oleg@redhat.com>
>> ---
>>  block/blk-throttle.c | 6 ------
>>  1 file changed, 6 deletions(-)
>> 
>> diff --git a/block/blk-throttle.c b/block/blk-throttle.c
>> index 397b6a410f9e..66339e22cc85 100644
>> --- a/block/blk-throttle.c
>> +++ b/block/blk-throttle.c
>> @@ -601,12 +601,6 @@ 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);
>
>Not directly related, but the two (u64) casts are pointless and can be removed.
>
>	David
>
>>  }
>>  
>

It's also rather broken, because a division with a constant can be implemented as a multiply, and both gcc and clang knows how to do that.