diff mbox series

mm: fix arithmetic for bdi min_ratio and max_ratio

Message ID 20231218031640.77983-1-jefflexu@linux.alibaba.com (mailing list archive)
State New
Headers show
Series mm: fix arithmetic for bdi min_ratio and max_ratio | expand

Commit Message

Jingbo Xu Dec. 18, 2023, 3:16 a.m. UTC
bdi->[min|max]_ratio are both part per million.  Fix the wrong
arithmetic when setting bdi's min_ratio and max_ratio.

Fixes: efc3e6ad53ea ("mm: split off __bdi_set_max_ratio() function")
Fixes: 8021fb3232f2 ("mm: split off __bdi_set_min_ratio() function")
Reported-by: Joseph Qi <joseph.qi@linux.alibaba.com>
Signed-off-by: Jingbo Xu <jefflexu@linux.alibaba.com>
---
 mm/page-writeback.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Comments

Jingbo Xu Dec. 18, 2023, 3:29 a.m. UTC | #1
+cc fsdevel

On 12/18/23 11:16 AM, Jingbo Xu wrote:
> bdi->[min|max]_ratio are both part per million.  Fix the wrong
> arithmetic when setting bdi's min_ratio and max_ratio.
> 
> Fixes: efc3e6ad53ea ("mm: split off __bdi_set_max_ratio() function")
> Fixes: 8021fb3232f2 ("mm: split off __bdi_set_min_ratio() function")
> Reported-by: Joseph Qi <joseph.qi@linux.alibaba.com>
> Signed-off-by: Jingbo Xu <jefflexu@linux.alibaba.com>
> ---
>  mm/page-writeback.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/mm/page-writeback.c b/mm/page-writeback.c
> index ee2fd6a6af40..b393b3281ce9 100644
> --- a/mm/page-writeback.c
> +++ b/mm/page-writeback.c
> @@ -692,7 +692,6 @@ static int __bdi_set_min_ratio(struct backing_dev_info *bdi, unsigned int min_ra
>  
>  	if (min_ratio > 100 * BDI_RATIO_SCALE)
>  		return -EINVAL;
> -	min_ratio *= BDI_RATIO_SCALE;
>  
>  	spin_lock_bh(&bdi_lock);
>  	if (min_ratio > bdi->max_ratio) {
> @@ -729,7 +728,8 @@ static int __bdi_set_max_ratio(struct backing_dev_info *bdi, unsigned int max_ra
>  		ret = -EINVAL;
>  	} else {
>  		bdi->max_ratio = max_ratio;
> -		bdi->max_prop_frac = (FPROP_FRAC_BASE * max_ratio) / 100;
> +		bdi->max_prop_frac = div64_u64(FPROP_FRAC_BASE * max_ratio,
> +					       100UL * BDI_RATIO_SCALE);
>  	}
>  	spin_unlock_bh(&bdi_lock);
>
Andrew Morton Dec. 18, 2023, 6:21 p.m. UTC | #2
On Mon, 18 Dec 2023 11:16:40 +0800 Jingbo Xu <jefflexu@linux.alibaba.com> wrote:

> bdi->[min|max]_ratio are both part per million.  Fix the wrong
> arithmetic when setting bdi's min_ratio and max_ratio.
> 
> Fixes: efc3e6ad53ea ("mm: split off __bdi_set_max_ratio() function")
> Fixes: 8021fb3232f2 ("mm: split off __bdi_set_min_ratio() function")

As we have two Fixes:, it would be better to have two patches please.

> --- a/mm/page-writeback.c
> +++ b/mm/page-writeback.c
> @@ -692,7 +692,6 @@ static int __bdi_set_min_ratio(struct backing_dev_info *bdi, unsigned int min_ra
>  
>  	if (min_ratio > 100 * BDI_RATIO_SCALE)
>  		return -EINVAL;
> -	min_ratio *= BDI_RATIO_SCALE;
>  
>  	spin_lock_bh(&bdi_lock);
>  	if (min_ratio > bdi->max_ratio) {
> @@ -729,7 +728,8 @@ static int __bdi_set_max_ratio(struct backing_dev_info *bdi, unsigned int max_ra
>  		ret = -EINVAL;
>  	} else {
>  		bdi->max_ratio = max_ratio;
> -		bdi->max_prop_frac = (FPROP_FRAC_BASE * max_ratio) / 100;
> +		bdi->max_prop_frac = div64_u64(FPROP_FRAC_BASE * max_ratio,
> +					       100UL * BDI_RATIO_SCALE);
>  	}
>  	spin_unlock_bh(&bdi_lock);

Does this change have any observable runtime effects?  If so, what are they?
Jingbo Xu Dec. 19, 2023, 2:03 a.m. UTC | #3
On 12/19/23 2:21 AM, Andrew Morton wrote:
> On Mon, 18 Dec 2023 11:16:40 +0800 Jingbo Xu <jefflexu@linux.alibaba.com> wrote:
> 
>> bdi->[min|max]_ratio are both part per million.  Fix the wrong
>> arithmetic when setting bdi's min_ratio and max_ratio.
>>
>> Fixes: efc3e6ad53ea ("mm: split off __bdi_set_max_ratio() function")
>> Fixes: 8021fb3232f2 ("mm: split off __bdi_set_min_ratio() function")
> 
> As we have two Fixes:, it would be better to have two patches please.

Sounds reasonable.  Thanks.

> 
>> --- a/mm/page-writeback.c
>> +++ b/mm/page-writeback.c
>> @@ -692,7 +692,6 @@ static int __bdi_set_min_ratio(struct backing_dev_info *bdi, unsigned int min_ra
>>  
>>  	if (min_ratio > 100 * BDI_RATIO_SCALE)
>>  		return -EINVAL;
>> -	min_ratio *= BDI_RATIO_SCALE;

This indeed has visible runtime effect as follows:

# cat /sys/class/bdi/253\:0/min_ratio
0
# cat /sys/class/bdi/253\:0/max_ratio
100
# echo 1 > /sys/class/bdi/253\:0/min_ratio
-bash: echo: write error: Invalid argument

Setting min_ratio will fail with -EINVAL, as the above code tries to set
min_ratio with (min_ratio * BDI_RATIO_SCALE)%, i.e. 10000% in the above
example.

>>  
>>  	spin_lock_bh(&bdi_lock);
>>  	if (min_ratio > bdi->max_ratio) {
>> @@ -729,7 +728,8 @@ static int __bdi_set_max_ratio(struct backing_dev_info *bdi, unsigned int max_ra
>>  		ret = -EINVAL;
>>  	} else {
>>  		bdi->max_ratio = max_ratio;
>> -		bdi->max_prop_frac = (FPROP_FRAC_BASE * max_ratio) / 100;
>> +		bdi->max_prop_frac = div64_u64(FPROP_FRAC_BASE * max_ratio,
>> +					       100UL * BDI_RATIO_SCALE);
>>  	}
>>  	spin_unlock_bh(&bdi_lock);

This one has no visible runtime effect, but I believe this would affect
the incrementing of writeout completion count when max_ratio is not 100%.
diff mbox series

Patch

diff --git a/mm/page-writeback.c b/mm/page-writeback.c
index ee2fd6a6af40..b393b3281ce9 100644
--- a/mm/page-writeback.c
+++ b/mm/page-writeback.c
@@ -692,7 +692,6 @@  static int __bdi_set_min_ratio(struct backing_dev_info *bdi, unsigned int min_ra
 
 	if (min_ratio > 100 * BDI_RATIO_SCALE)
 		return -EINVAL;
-	min_ratio *= BDI_RATIO_SCALE;
 
 	spin_lock_bh(&bdi_lock);
 	if (min_ratio > bdi->max_ratio) {
@@ -729,7 +728,8 @@  static int __bdi_set_max_ratio(struct backing_dev_info *bdi, unsigned int max_ra
 		ret = -EINVAL;
 	} else {
 		bdi->max_ratio = max_ratio;
-		bdi->max_prop_frac = (FPROP_FRAC_BASE * max_ratio) / 100;
+		bdi->max_prop_frac = div64_u64(FPROP_FRAC_BASE * max_ratio,
+					       100UL * BDI_RATIO_SCALE);
 	}
 	spin_unlock_bh(&bdi_lock);