From dcb882bd436e2124e37640671cfa773dfaed485c Mon Sep 17 00:00:00 2001 From: =?utf8?q?Uwe=20Kleine-K=C3=B6nig?= Date: Sat, 12 Apr 2025 14:21:24 +0200 Subject: [PATCH] pwm: loongson: Fix u32 overflow in waveform calculation MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 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. Reported-by: Dan Carpenter Link: https://lore.kernel.org/r/44f3c764-8b65-49a9-b3ad-797e9fbb96f5@stanley.mountain Fixes: 2b62c89448dd ("pwm: Add Loongson PWM controller support") Signed-off-by: Uwe Kleine-König Reviewed-by: Binbin Zhou Link: https://lore.kernel.org/r/20250412122124.1636152-2-u.kleine-koenig@baylibre.com Signed-off-by: Uwe Kleine-König --- drivers/pwm/pwm-loongson.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/pwm/pwm-loongson.c b/drivers/pwm/pwm-loongson.c index 412c67739ef9..6392c4e34136 100644 --- a/drivers/pwm/pwm-loongson.c +++ b/drivers/pwm/pwm-loongson.c @@ -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 */ -- 2.50.1