mm: fix arithmetic for bdi min_ratio and
Subsystems (4)
v2 cover mm: fix arithmetic for bdi min_ratio and
changes since v1:
- split the previous v1 patch into two separate patches with corresponding "Fixes" tag (Andrew Morton)
- patch 2: remove "UL" suffix for the "100" constant
v1: https://lore.kernel.org/all/[email protected]/
Jingbo Xu (2):
mm: fix arithmetic for bdi min_ratio
mm: fix arithmetic for max_prop_frac when setting max_ratio
mm/page-writeback.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
Signature · 1 line
2.19.1.6.gb485710b
PATCH v2 1/2 mm: fix arithmetic for bdi min_ratio
Since now bdi->min_ratio is part per million, fix the wrong arithmetic. Otherwise it will fail with -EINVAL when setting a reasonable min_ratio, as it tries to set min_ratio to (min_ratio * BDI_RATIO_SCALE) in percentage unit, which exceeds 100% anyway.
# 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--- mm/page-writeback.c | 1 - 1 file changed, 1 deletion(-)
mm/page-writeback.c +0 -1
@@ -692,7 +692,6 @@ static int __bdi_set_min_ratio(struct backing_dev_info *bdi, unsigned int min_raif (min_ratio >100 *BDI_RATIO_SCALE )return -EINVAL ; - min_ratio *=BDI_RATIO_SCALE ;spin_lock_bh (&bdi_lock);if (min_ratio > bdi->max_ratio) {
Signature · 1 line
2.19.1.6.gb485710b
PATCH v2 2/2 mm: fix arithmetic for max_prop_frac when setting max_ratio
In reply to Jingbo XuSince now bdi->max_ratio is part per million, fix the wrong arithmetic for max_prop_frac when setting max_ratio. Otherwise the miscalculated max_prop_frac will affect the incrementing of writeout completion count when max_ratio is not 100%.
--- mm/page-writeback.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
mm/page-writeback.c +2 -1
@@ -728,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, +100 *BDI_RATIO_SCALE ); }spin_unlock_bh (&bdi_lock);
Signature · 1 line
2.19.1.6.gb485710b
Matthew Wilcox
maintainerRe: [PATCH v2 2/2] mm: fix arithmetic for max_prop_frac when setting max_ratio
mm/page-writeback.c static int __bdi_set_max_ratio(struct backing_dev_info *bdi, unsigned int max_ra · line 733
Quoted 6 lines · Jingbo Xu
mm/page-writeback.c +2
-1}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, +100 *BDI_RATIO_SCALE ); }
Why use div64_u64 here?
FPROP_FRAC_BASE is an unsigned long. max_ratio is an unsigned int, so the numerator is an unsigned long. BDI_RATIO_SCALE is 10,000, so the numerator is an unsigned int. There's no 64-bit arithmetic needed here.
Re: [PATCH v2 2/2] mm: fix arithmetic for max_prop_frac when setting max_ratio
Quoted 10 lines · Matthew Wilcox
mm/page-writeback.c static int __bdi_set_max_ratio(struct backing_dev_info *bdi, unsigned int max_ra · line 733
Quoted 6 lines · Jingbo Xu
mm/page-writeback.c +2
-1}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, +100 *BDI_RATIO_SCALE ); }Why use
div64_u64here?
Quoted 3 lines
FPROP_FRAC_BASEis an unsigned long.max_ratiois an unsigned int, so the numerator is an unsigned long.BDI_RATIO_SCALEis 10,000, so the numerator is an unsigned int. There's no 64-bit arithmetic needed here.
Yes, div64_u64() is actually not needed here. So it seems
bdi->max_prop_frac = FPROP_FRAC_BASE * max_ratio / 100 / BDI_RATIO_SCALE;
is adequate?
Signature · 2 lines
Thanks, Jingbo
Matthew Wilcox
maintainerRe: [PATCH v2 2/2] mm: fix arithmetic for max_prop_frac when setting max_ratio
Quoted 15 lines · Jingbo Xu
Quoted 10 lines · Matthew Wilcox
mm/page-writeback.c static int __bdi_set_max_ratio(struct backing_dev_info *bdi, unsigned int max_ra · line 733
Quoted 6 lines · Jingbo Xu
mm/page-writeback.c +2
-1}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, +100 *BDI_RATIO_SCALE ); }Why use
div64_u64here?Quoted 3 lines
FPROP_FRAC_BASEis an unsigned long.max_ratiois an unsigned int, so the numerator is an unsigned long.BDI_RATIO_SCALEis 10,000, so the numerator is an unsigned int. There's no 64-bit arithmetic needed here.
Quoted 5 lines
Yes,
div64_u64()is actually not needed here. So it seems
bdi->max_prop_frac=FPROP_FRAC_BASE*max_ratio/ 100 /BDI_RATIO_SCALE;is adequate?
I'd rather spell that as:
bdi->max_prop_frac = (FPROP_FRAC_BASE * max_ratio) / (100 * BDI_RATIO_SCALE);
It's closer to how you'd write it out mathematically and so it reads more easily. At least for me.
Re: [PATCH v2 2/2] mm: fix arithmetic for max_prop_frac when setting max_ratio
Quoted 22 lines · Matthew Wilcox
Quoted 15 lines · Jingbo Xu
Quoted 10 lines · Matthew Wilcox
mm/page-writeback.c static int __bdi_set_max_ratio(struct backing_dev_info *bdi, unsigned int max_ra · line 733
Quoted 6 lines · Jingbo Xu
mm/page-writeback.c +2
-1}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, +100 *BDI_RATIO_SCALE ); }Why use
div64_u64here?Quoted 3 lines
FPROP_FRAC_BASEis an unsigned long.max_ratiois an unsigned int, so the numerator is an unsigned long.BDI_RATIO_SCALEis 10,000, so the numerator is an unsigned int. There's no 64-bit arithmetic needed here.Quoted 5 lines
Yes,
div64_u64()is actually not needed here. So it seems
bdi->max_prop_frac=FPROP_FRAC_BASE*max_ratio/ 100 /BDI_RATIO_SCALE;is adequate?
Quoted 7 lines
I'd rather spell that as:
bdi->max_prop_frac = (FPROP_FRAC_BASE * max_ratio) / (100 * BDI_RATIO_SCALE);It's closer to how you'd write it out mathematically and so it reads more easily. At least for me.
Thanks, I would send v3 soon.
Signature · 2 lines
Thanks, Jingbo