2021-11-17 01:04:07

by CGEL

[permalink] [raw]
Subject: [PATCH] block: Use div64_ul instead of do_div

From: Changcheng Deng <[email protected]>

do_div() does a 64-by-32 division. If the divisor is unsigned long, using
div64_ul can avoid truncation to 32-bit.

Reported-by: Zeal Robot <[email protected]>
Signed-off-by: Changcheng Deng <[email protected]>
---
block/blk-throttle.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/block/blk-throttle.c b/block/blk-throttle.c
index 39bb6e68a9a2..2db635d66617 100644
--- a/block/blk-throttle.c
+++ b/block/blk-throttle.c
@@ -1903,7 +1903,7 @@ static void throtl_downgrade_check(struct throtl_grp *tg)

if (tg->bps[READ][LIMIT_LOW]) {
bps = tg->last_bytes_disp[READ] * HZ;
- do_div(bps, elapsed_time);
+ div64_ul(bps, elapsed_time);
if (bps >= tg->bps[READ][LIMIT_LOW])
tg->last_low_overflow_time[READ] = now;
}
--
2.25.1



2021-11-18 03:41:21

by CGEL

[permalink] [raw]
Subject: [PATCH v2] block: Use div64_ul instead of do_div

From: Changcheng Deng <[email protected]>

do_div() does a 64-by-32 division. Here the divisor is an unsigned long
which on some platforms is 64 bit wide. So use div64_ul instead of do_div
to avoid a possible truncation.

Reported-by: Zeal Robot <[email protected]>
Signed-off-by: Changcheng Deng <[email protected]>
---
block/blk-throttle.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/block/blk-throttle.c b/block/blk-throttle.c
index 39bb6e68a9a2..2db635d66617 100644
--- a/block/blk-throttle.c
+++ b/block/blk-throttle.c
@@ -1903,7 +1903,7 @@ static void throtl_downgrade_check(struct throtl_grp *tg)

if (tg->bps[READ][LIMIT_LOW]) {
bps = tg->last_bytes_disp[READ] * HZ;
- do_div(bps, elapsed_time);
+ bps = div64_ul(bps, elapsed_time);
if (bps >= tg->bps[READ][LIMIT_LOW])
tg->last_low_overflow_time[READ] = now;
}
--
2.25.1


2021-11-29 22:32:19

by Tejun Heo

[permalink] [raw]
Subject: Re: [PATCH v2] block: Use div64_ul instead of do_div

On Thu, Nov 18, 2021 at 03:40:33AM +0000, [email protected] wrote:
> From: Changcheng Deng <[email protected]>
>
> do_div() does a 64-by-32 division. Here the divisor is an unsigned long
> which on some platforms is 64 bit wide. So use div64_ul instead of do_div
> to avoid a possible truncation.
>
> Reported-by: Zeal Robot <[email protected]>
> Signed-off-by: Changcheng Deng <[email protected]>
> ---
> block/blk-throttle.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/block/blk-throttle.c b/block/blk-throttle.c
> index 39bb6e68a9a2..2db635d66617 100644
> --- a/block/blk-throttle.c
> +++ b/block/blk-throttle.c
> @@ -1903,7 +1903,7 @@ static void throtl_downgrade_check(struct throtl_grp *tg)
>
> if (tg->bps[READ][LIMIT_LOW]) {
> bps = tg->last_bytes_disp[READ] * HZ;
> - do_div(bps, elapsed_time);
> + bps = div64_ul(bps, elapsed_time);

Yeah, the divisor is ulong for whatever reason but the thing is in jiffies.
I have a hard time imagining it overflowing 32bit. I'm not sure how useful
this patch is.

--
tejun

2021-12-02 16:50:26

by Michal Koutný

[permalink] [raw]
Subject: Re: [PATCH v2] block: Use div64_ul instead of do_div

Hello.

On Thu, Nov 18, 2021 at 03:40:33AM +0000, [email protected] wrote:
> Reported-by: Zeal Robot <[email protected]>
> Signed-off-by: Changcheng Deng <[email protected]>

Just feedback to the bot or to you -- the same do_div() is in the same
function for writes too. I think the processing shouldn't stop at the
first occurence.

Michal


Attachments:
(No filename) (352.00 B)
signature.asc (228.00 B)
Digital signature
Download all attachments