linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] pwm: Use div64_ul instead of do_div
@ 2021-11-17  2:04 cgel.zte
  2021-11-17 11:24 ` Uwe Kleine-König
  0 siblings, 1 reply; 8+ messages in thread
From: cgel.zte @ 2021-11-17  2:04 UTC (permalink / raw)
  To: thierry.reding
  Cc: u.kleine-koenig, lee.jones, nicolas.ferre, alexandre.belloni,
	ludovic.desroches, linux-pwm, linux-arm-kernel, linux-kernel,
	Changcheng Deng, Zeal Robot

From: Changcheng Deng <deng.changcheng@zte.com.cn>

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 <zealci@zte.com.cn>
Signed-off-by: Changcheng Deng <deng.changcheng@zte.com.cn>
---
 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


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH] pwm: Use div64_ul instead of do_div
  2021-11-17  2:04 [PATCH] pwm: Use div64_ul instead of do_div cgel.zte
@ 2021-11-17 11:24 ` Uwe Kleine-König
  2021-11-17 12:46   ` [PATCH V2] " cgel.zte
  2021-11-18 10:09   ` [PATCH] " Russell King (Oracle)
  0 siblings, 2 replies; 8+ messages in thread
From: Uwe Kleine-König @ 2021-11-17 11:24 UTC (permalink / raw)
  To: cgel.zte
  Cc: thierry.reding, lee.jones, nicolas.ferre, alexandre.belloni,
	ludovic.desroches, linux-pwm, linux-arm-kernel, linux-kernel,
	Changcheng Deng, Zeal Robot

[-- Attachment #1: Type: text/plain, Size: 1409 bytes --]

Hello,

On Wed, Nov 17, 2021 at 02:04:26AM +0000, cgel.zte@gmail.com wrote:
> From: Changcheng Deng <deng.changcheng@zte.com.cn>
> 
> 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/ |

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH V2] pwm: Use div64_ul instead of do_div
  2021-11-17 11:24 ` Uwe Kleine-König
@ 2021-11-17 12:46   ` cgel.zte
  2021-11-17 14:54     ` Uwe Kleine-König
  2021-11-18 10:09   ` [PATCH] " Russell King (Oracle)
  1 sibling, 1 reply; 8+ messages in thread
From: cgel.zte @ 2021-11-17 12:46 UTC (permalink / raw)
  To: u.kleine-koenig
  Cc: alexandre.belloni, cgel.zte, deng.changcheng, lee.jones,
	linux-arm-kernel, linux-kernel, linux-pwm, ludovic.desroches,
	nicolas.ferre, thierry.reding, zealci

From: Changcheng Deng <deng.changcheng@zte.com.cn>

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 <zealci@zte.com.cn>
Signed-off-by: Changcheng Deng <deng.changcheng@zte.com.cn>
---
 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


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH V2] pwm: Use div64_ul instead of do_div
  2021-11-17 12:46   ` [PATCH V2] " cgel.zte
@ 2021-11-17 14:54     ` Uwe Kleine-König
  2021-11-18  2:52       ` [PATCH V3] " cgel.zte
  0 siblings, 1 reply; 8+ messages in thread
From: Uwe Kleine-König @ 2021-11-17 14:54 UTC (permalink / raw)
  To: cgel.zte
  Cc: alexandre.belloni, deng.changcheng, lee.jones, linux-arm-kernel,
	linux-kernel, linux-pwm, ludovic.desroches, nicolas.ferre,
	thierry.reding, zealci

[-- Attachment #1: Type: text/plain, Size: 1540 bytes --]

On Wed, Nov 17, 2021 at 12:46:53PM +0000, cgel.zte@gmail.com wrote:
> From: Changcheng Deng <deng.changcheng@zte.com.cn>
> 
> 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 <zealci@zte.com.cn>
> Signed-off-by: Changcheng Deng <deng.changcheng@zte.com.cn>
> ---
>  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/ |

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH V3] pwm: Use div64_ul instead of do_div
  2021-11-17 14:54     ` Uwe Kleine-König
@ 2021-11-18  2:52       ` cgel.zte
  2021-11-18  9:54         ` Uwe Kleine-König
  0 siblings, 1 reply; 8+ messages in thread
From: cgel.zte @ 2021-11-18  2:52 UTC (permalink / raw)
  To: u.kleine-koenig
  Cc: alexandre.belloni, cgel.zte, deng.changcheng, lee.jones,
	linux-arm-kernel, linux-kernel, linux-pwm, ludovic.desroches,
	nicolas.ferre, thierry.reding, zealci

From: Changcheng Deng <deng.changcheng@zte.com.cn>

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 <zealci@zte.com.cn>
Signed-off-by: Changcheng Deng <deng.changcheng@zte.com.cn>
---
 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


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH V3] pwm: Use div64_ul instead of do_div
  2021-11-18  2:52       ` [PATCH V3] " cgel.zte
@ 2021-11-18  9:54         ` Uwe Kleine-König
  0 siblings, 0 replies; 8+ messages in thread
From: Uwe Kleine-König @ 2021-11-18  9:54 UTC (permalink / raw)
  To: cgel.zte
  Cc: alexandre.belloni, deng.changcheng, lee.jones, linux-arm-kernel,
	linux-kernel, linux-pwm, ludovic.desroches, nicolas.ferre,
	thierry.reding, zealci

[-- Attachment #1: Type: text/plain, Size: 660 bytes --]

On Thu, Nov 18, 2021 at 02:52:54AM +0000, cgel.zte@gmail.com wrote:
> From: Changcheng Deng <deng.changcheng@zte.com.cn>
> 
> 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 <zealci@zte.com.cn>
> Signed-off-by: Changcheng Deng <deng.changcheng@zte.com.cn>

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/ |

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] pwm: Use div64_ul instead of do_div
  2021-11-17 11:24 ` Uwe Kleine-König
  2021-11-17 12:46   ` [PATCH V2] " cgel.zte
@ 2021-11-18 10:09   ` Russell King (Oracle)
  2021-11-18 13:19     ` Nicolas Ferre
  1 sibling, 1 reply; 8+ messages in thread
From: Russell King (Oracle) @ 2021-11-18 10:09 UTC (permalink / raw)
  To: Uwe Kleine-König
  Cc: cgel.zte, linux-pwm, alexandre.belloni, Zeal Robot, linux-kernel,
	ludovic.desroches, thierry.reding, Changcheng Deng, lee.jones,
	linux-arm-kernel

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, cgel.zte@gmail.com wrote:
> > From: Changcheng Deng <deng.changcheng@zte.com.cn>
> > 
> > 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!

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] pwm: Use div64_ul instead of do_div
  2021-11-18 10:09   ` [PATCH] " Russell King (Oracle)
@ 2021-11-18 13:19     ` Nicolas Ferre
  0 siblings, 0 replies; 8+ messages in thread
From: Nicolas Ferre @ 2021-11-18 13:19 UTC (permalink / raw)
  To: Russell King (Oracle), Uwe Kleine-König
  Cc: cgel.zte, linux-pwm, alexandre.belloni, Zeal Robot, linux-kernel,
	ludovic.desroches, thierry.reding, Changcheng Deng, lee.jones,
	linux-arm-kernel

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, cgel.zte@gmail.com wrote:
>>> From: Changcheng Deng <deng.changcheng@zte.com.cn>
>>>
>>> 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

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2021-11-18 13:19 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-11-17  2:04 [PATCH] pwm: Use div64_ul instead of do_div cgel.zte
2021-11-17 11:24 ` Uwe Kleine-König
2021-11-17 12:46   ` [PATCH V2] " cgel.zte
2021-11-17 14:54     ` Uwe Kleine-König
2021-11-18  2:52       ` [PATCH V3] " cgel.zte
2021-11-18  9:54         ` Uwe Kleine-König
2021-11-18 10:09   ` [PATCH] " Russell King (Oracle)
2021-11-18 13:19     ` Nicolas Ferre

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).