2021-11-17 02:04:35

by CGEL

[permalink] [raw]
Subject: [PATCH] pwm: 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]>
---
drivers/pwm/pwm-atmel-hlcdc.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/pwm/pwm-atmel-hlcdc.c b/drivers/pwm/pwm-atmel-hlcdc.c
index a43b2babc809..1ae3d73b9832 100644
--- a/drivers/pwm/pwm-atmel-hlcdc.c
+++ b/drivers/pwm/pwm-atmel-hlcdc.c
@@ -60,7 +60,7 @@ static int atmel_hlcdc_pwm_apply(struct pwm_chip *c, struct pwm_device *pwm,
return -EINVAL;

clk_period_ns = (u64)NSEC_PER_SEC * 256;
- do_div(clk_period_ns, clk_freq);
+ div64_ul(clk_period_ns, clk_freq);
}

/* Errata: cannot use slow clk on some IP revisions */
@@ -72,7 +72,7 @@ static int atmel_hlcdc_pwm_apply(struct pwm_chip *c, struct pwm_device *pwm,
return -EINVAL;

clk_period_ns = (u64)NSEC_PER_SEC * 256;
- do_div(clk_period_ns, clk_freq);
+ div64_ul(clk_period_ns, clk_freq);
}

for (pres = 0; pres <= ATMEL_HLCDC_PWMPS_MAX; pres++) {
--
2.25.1



2021-11-17 11:24:42

by Uwe Kleine-König

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

Hello,

On Wed, Nov 17, 2021 at 02:04:26AM +0000, [email protected] wrote:
> 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.

After some research I understood your commit log. I'd write:

do_div() does a 64-by-32 division. Here the divsor 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.

The priority of this patch seems to be low, as the device seems to exist
only on (32bit) arm.

> diff --git a/drivers/pwm/pwm-atmel-hlcdc.c b/drivers/pwm/pwm-atmel-hlcdc.c
> index a43b2babc809..1ae3d73b9832 100644
> --- a/drivers/pwm/pwm-atmel-hlcdc.c
> +++ b/drivers/pwm/pwm-atmel-hlcdc.c
> @@ -60,7 +60,7 @@ static int atmel_hlcdc_pwm_apply(struct pwm_chip *c, struct pwm_device *pwm,
> return -EINVAL;
>
> clk_period_ns = (u64)NSEC_PER_SEC * 256;
> - do_div(clk_period_ns, clk_freq);
> + div64_ul(clk_period_ns, clk_freq);

This must be

clk_period_ns = div64_ul(clk_period_ns, clk_freq);

as div64_ul has a different calling convention than do_div. Same problem
in the next hunk.

Best regards
Uwe

--
Pengutronix e.K. | Uwe Kleine-K?nig |
Industrial Linux Solutions | https://www.pengutronix.de/ |


Attachments:
(No filename) (1.34 kB)
signature.asc (488.00 B)
Download all attachments

2021-11-17 12:50:24

by CGEL

[permalink] [raw]
Subject: [PATCH V2] pwm: 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]>
---
drivers/pwm/pwm-atmel-hlcdc.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/pwm/pwm-atmel-hlcdc.c b/drivers/pwm/pwm-atmel-hlcdc.c
index a43b2babc809..1ae3d73b9832 100644
--- a/drivers/pwm/pwm-atmel-hlcdc.c
+++ b/drivers/pwm/pwm-atmel-hlcdc.c
@@ -60,7 +60,7 @@ static int atmel_hlcdc_pwm_apply(struct pwm_chip *c, struct pwm_device *pwm,
return -EINVAL;

clk_period_ns = (u64)NSEC_PER_SEC * 256;
- do_div(clk_period_ns, clk_freq);
+ clk_period_ns = div64_ul(clk_period_ns, clk_freq);
}

/* Errata: cannot use slow clk on some IP revisions */
@@ -72,7 +72,7 @@ static int atmel_hlcdc_pwm_apply(struct pwm_chip *c, struct pwm_device *pwm,
return -EINVAL;

clk_period_ns = (u64)NSEC_PER_SEC * 256;
- do_div(clk_period_ns, clk_freq);
+ clk_period_ns = div64_ul(clk_period_ns, clk_freq);
}

for (pres = 0; pres <= ATMEL_HLCDC_PWMPS_MAX; pres++) {
--
2.25.1


2021-11-17 14:54:33

by Uwe Kleine-König

[permalink] [raw]
Subject: Re: [PATCH V2] pwm: Use div64_ul instead of do_div

On Wed, Nov 17, 2021 at 12:46:53PM +0000, [email protected] wrote:
> 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]>
> ---
> drivers/pwm/pwm-atmel-hlcdc.c | 4 ++--
> 1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/pwm/pwm-atmel-hlcdc.c b/drivers/pwm/pwm-atmel-hlcdc.c
> index a43b2babc809..1ae3d73b9832 100644
> --- a/drivers/pwm/pwm-atmel-hlcdc.c
> +++ b/drivers/pwm/pwm-atmel-hlcdc.c
> @@ -60,7 +60,7 @@ static int atmel_hlcdc_pwm_apply(struct pwm_chip *c, struct pwm_device *pwm,
> return -EINVAL;
>
> clk_period_ns = (u64)NSEC_PER_SEC * 256;
> - do_div(clk_period_ns, clk_freq);
> + clk_period_ns = div64_ul(clk_period_ns, clk_freq);
> }
>
> /* Errata: cannot use slow clk on some IP revisions */
> @@ -72,7 +72,7 @@ static int atmel_hlcdc_pwm_apply(struct pwm_chip *c, struct pwm_device *pwm,
> return -EINVAL;
>
> clk_period_ns = (u64)NSEC_PER_SEC * 256;
> - do_div(clk_period_ns, clk_freq);
> + clk_period_ns = div64_ul(clk_period_ns, clk_freq);

The code change is good now, the commit log is as confusing as in v1.

Best regards
Uwe

--
Pengutronix e.K. | Uwe Kleine-K?nig |
Industrial Linux Solutions | https://www.pengutronix.de/ |


Attachments:
(No filename) (1.46 kB)
signature.asc (488.00 B)
Download all attachments

2021-11-18 02:53:29

by CGEL

[permalink] [raw]
Subject: [PATCH V3] pwm: 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]>
---
drivers/pwm/pwm-atmel-hlcdc.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/pwm/pwm-atmel-hlcdc.c b/drivers/pwm/pwm-atmel-hlcdc.c
index a43b2babc809..1ae3d73b9832 100644
--- a/drivers/pwm/pwm-atmel-hlcdc.c
+++ b/drivers/pwm/pwm-atmel-hlcdc.c
@@ -60,7 +60,7 @@ static int atmel_hlcdc_pwm_apply(struct pwm_chip *c, struct pwm_device *pwm,
return -EINVAL;

clk_period_ns = (u64)NSEC_PER_SEC * 256;
- do_div(clk_period_ns, clk_freq);
+ clk_period_ns = div64_ul(clk_period_ns, clk_freq);
}

/* Errata: cannot use slow clk on some IP revisions */
@@ -72,7 +72,7 @@ static int atmel_hlcdc_pwm_apply(struct pwm_chip *c, struct pwm_device *pwm,
return -EINVAL;

clk_period_ns = (u64)NSEC_PER_SEC * 256;
- do_div(clk_period_ns, clk_freq);
+ clk_period_ns = div64_ul(clk_period_ns, clk_freq);
}

for (pres = 0; pres <= ATMEL_HLCDC_PWMPS_MAX; pres++) {
--
2.25.1


2021-11-18 09:55:53

by Uwe Kleine-König

[permalink] [raw]
Subject: Re: [PATCH V3] pwm: Use div64_ul instead of do_div

On Thu, Nov 18, 2021 at 02:52:54AM +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]>

There is no S-o-b line matching the mail sender.

Best regards
Uwe

--
Pengutronix e.K. | Uwe Kleine-K?nig |
Industrial Linux Solutions | https://www.pengutronix.de/ |


Attachments:
(No filename) (642.00 B)
signature.asc (488.00 B)
Download all attachments

2021-11-18 10:11:43

by Russell King (Oracle)

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

On Wed, Nov 17, 2021 at 12:24:00PM +0100, Uwe Kleine-K?nig wrote:
> Hello,
>
> On Wed, Nov 17, 2021 at 02:04:26AM +0000, [email protected] wrote:
> > 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.
>
> After some research I understood your commit log. I'd write:
>
> do_div() does a 64-by-32 division. Here the divsor 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.
>
> The priority of this patch seems to be low, as the device seems to exist
> only on (32bit) arm.

... where unsigned long is 32-bit.

In any case, for this to overflow, we would need to have a clock in
excess of 2^32-1 Hz, or around 4GHz - and if we had such a situation
on 32-bit devices, we need to change the type for holding the frequency
in the clk API, and probably a lot of code in the CCF as well.

Unless there is a real reason for this change, I would suggest leaving
the code as is - there is absolutely no point in making these divisions
more expensive unless there is a real reason.

--
RMK's Patch system: https://www.armlinux.org.uk/developer/patches/
FTTP is here! 40Mbps down 10Mbps up. Decent connectivity at last!

2021-11-18 13:19:45

by Nicolas Ferre

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

On 18/11/2021 at 11:09, Russell King (Oracle) wrote:
> EXTERNAL EMAIL: Do not click links or open attachments unless you know the content is safe
>
> On Wed, Nov 17, 2021 at 12:24:00PM +0100, Uwe Kleine-K?nig wrote:
>> Hello,
>>
>> On Wed, Nov 17, 2021 at 02:04:26AM +0000, [email protected] wrote:
>>> 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.
>>
>> After some research I understood your commit log. I'd write:
>>
>> do_div() does a 64-by-32 division. Here the divsor 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.
>>
>> The priority of this patch seems to be low, as the device seems to exist
>> only on (32bit) arm.
>
> ... where unsigned long is 32-bit.
>
> In any case, for this to overflow, we would need to have a clock in
> excess of 2^32-1 Hz, or around 4GHz - and if we had such a situation
> on 32-bit devices, we need to change the type for holding the frequency
> in the clk API, and probably a lot of code in the CCF as well.
>
> Unless there is a real reason for this change, I would suggest leaving
> the code as is - there is absolutely no point in making these divisions
> more expensive unless there is a real reason.

Thanks for the technical demonstration Russell. With this in mind:
NACK to the patch, sorry Changcheng Deng.

Best regards,
Nicolas


--
Nicolas Ferre