[PATCH 2/5] pwm: airoha: Fix 32 bit overflow when converting duty ticks to ns

From: shao.mingyin

Date: Fri Nov 06 2026 - 11:07:47 EST


From: Shao Mingyin <shao.mingyin@xxxxxxxxxx>

airoha_pwm_get_duty_ns_from_ticks() computes "duty_tick * period_ns"
where both operands are u32. The result is passed to DIV_U64_ROUND_UP()
as a u64, but the multiplication itself is performed in 32 bit and wraps
modulo 2^32. period_ns can be as large as AIROHA_PWM_PERIOD_MAX_NS (1 s)
and duty_tick up to AIROHA_PWM_DUTY_FULL (255), so the product exceeds
u32 and the reported duty cycle is wrong.

Cast duty_tick to u64 before multiplying.

Fixes: b55bbc2872eb ("pwm: airoha: Add support for EN7581 SoC")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Shao Mingyin <shao.mingyin@xxxxxxxxxx>
---

diff --git a/drivers/pwm/pwm-airoha.c b/drivers/pwm/pwm-airoha.c
index 0cf6b011520a..702c9cfc0e37 100644
--- a/drivers/pwm/pwm-airoha.c
+++ b/drivers/pwm/pwm-airoha.c
@@ -152,9 +152,9 @@ static u32 airoha_pwm_get_duty_ns_from_ticks(u32 period_tick, u32 duty_tick)
/*
- * Overflow can't occur in multiplication as duty_tick is just 8 bit
- * and period_ns is clamped to AIROHA_PWM_PERIOD_MAX_NS and fit in a
- * u64.
+ * period_ns is at most AIROHA_PWM_PERIOD_MAX_NS (1 s), which fits in
+ * a u32, but duty_tick * period_ns can exceed 32 bits, so promote
+ * duty_tick to u64 before multiplying.
*/
- return DIV_U64_ROUND_UP(duty_tick * period_ns, AIROHA_PWM_DUTY_FULL);
+ return DIV_U64_ROUND_UP((u64)duty_tick * period_ns, AIROHA_PWM_DUTY_FULL);
}

static int airoha_pwm_get_bucket(struct airoha_pwm *pc, int bucket,
--
2.47.0