Skip to contentSkip to conversation

pwm: loongson: Fix u32 overflow in waveform calculation

PATCH In review Uwe Kleine-König 3 participants 3 messages Apr 12, 2025 – Apr 15, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

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.

Fixes 322fc380cea1 ("pwm: Add Loongson PWM controller support")

---
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
index 412c67739ef9..6392c4e34136 100644
@@ -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
#1 lore raw

Hello Binbin,

Quoted 10 lines · Uwe Kleine-König

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;
Quoted 6 lines

actually work as intended.

Fixes 322fc380cea1 ("pwm: Add Loongson PWM controller support")

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

maintainer Reviewed-by on v1 1/1
#2 lore raw
In reply to Uwe Kleine-König

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 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;
Quoted 6 lines

actually work as intended.

Fixes 322fc380cea1 ("pwm: Add Loongson PWM controller support")
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
Uwe

  drivers/pwm/pwm-loongson.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/pwm/pwm-loongson.c +1 -1
index 412c67739ef9..6392c4e34136 100644
@@ -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