pwm: loongson: Fix u32 overflow in waveform calculation
Subsystems (2)
PATCH 1/1 pwm: loongson: Fix u32 overflow in waveform calculation
mul_u64_u64_div_u64() returns an u64 that might be bigger than U32_MAX. To properly handle this case it must not be directly assigned to an u32 value.
Use a wider type for duty and period to make the idiom:
duty = mul_u64_u64_div_u64(...) if (duty > U32_MAX) duty = U32_MAX;
actually work as intended.
---
Hello,
this is the approach that Dan suggested in reply to my suggested fix. This has
the advantage to not involve a very long line. I didn't check but I'd expect that the compiler produces comparable results for both approaches and so they are more or less equivalent after compilation.
Best regards
Uwe
drivers/pwm/pwm-loongson.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/pwm/pwm-loongson.c +1 -1
@@ -118,7 +118,7 @@ static int pwm_loongson_enable(struct pwm_chip *chip, struct pwm_device *pwm)static int pwm_loongson_config (struct pwm_chip *chip,struct pwm_device *pwm,u64 duty_ns,u64 period_ns) { -u32 duty, period; +u64 duty, period;struct pwm_loongson_ddata *ddata =to_pwm_loongson_ddata (chip);/* duty = duty_ns * ddata->clk_rate / NSEC_PER_SEC */
base-commit: 957062f2ba4790c495de606ecf8bc7398c0c710f
Signature · 1 line
2.47.2
Uwe Kleine-König
maintainerHello Binbin,
Quoted 10 lines · Uwe Kleine-König
mul_u64_u64_div_u64()returns an u64 that might be bigger thanU32_MAX. To properly handle this case it must not be directly assigned to an u32 value.Use a wider type for duty and period to make the idiom:
duty = mul_u64_u64_div_u64(...) if (duty > U32_MAX) duty = U32_MAX;
Quoted 6 lines
actually work as intended.
Reported-by Dan Carpenter <[email protected]>Fixes 322fc380cea1 ("pwm: Add Loongson PWM controller support")Signed-off-by Uwe Kleine-König <[email protected]>
I would have expected some feedback on such a bug report and patch from someone who was just added to the MAINTAINERS file claiming to maintain this driver.
I applied this patch now to
https://git.kernel.org/pub/scm/linux/kernel/git/ukleinek/linux.git pwm/for-next and still accept review tags for it.
Best regards
Uwe
Hi Uwe:
I'm very sorry for the late reply, I've been delayed for the past few days due to personal reasons. I will be more aware of this in the future.
Also, heartfelt thanks to you and Dan for the patch.
Quoted 10 lines · Uwe Kleine-König
mul_u64_u64_div_u64()returns an u64 that might be bigger thanU32_MAX. To properly handle this case it must not be directly assigned to an u32 value.Use a wider type for duty and period to make the idiom:
duty = mul_u64_u64_div_u64(...) if (duty > U32_MAX) duty = U32_MAX;
Quoted 6 lines
actually work as intended.
Reported-by Dan Carpenter <[email protected]>Fixes 322fc380cea1 ("pwm: Add Loongson PWM controller support")Signed-off-by Uwe Kleine-König <[email protected]>
Quoted 26 lines
---
Hello,this is the approach that Dan suggested in reply to my suggested fix. This has
the advantage to not involve a very long line. I didn't check but I'd expect that the compiler produces comparable results for both approaches and so they are more or less equivalent after compilation.Best regards
Uwedrivers/pwm/pwm-loongson.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/pwm/pwm-loongson.c +1
-1@@ -118,7 +118,7 @@ static int pwm_loongson_enable(struct pwm_chip *chip, struct pwm_device *pwm)static int pwm_loongson_config (struct pwm_chip *chip,struct pwm_device *pwm,u64 duty_ns,u64 period_ns) { -u32 duty, period; +u64 duty, period;struct pwm_loongson_ddata *ddata =to_pwm_loongson_ddata (chip);/* duty = duty_ns * ddata->clk_rate / NSEC_PER_SEC */
Quoted 1 line
base-commit:
957062f2ba4790c495de606ecf8bc7398c0c710f
Thanks.
Binbin