pwm: ipq: prevent potential 32-bit integer overflow in hi_div calculation
PATCH 1/1 pwm: ipq: prevent potential 32-bit integer overflow in hi_div calculation
From: Surendra Singh Chouhan <[email protected]>
In ipq_pwm_get_state(), hi_div was calculated as:
hi_div = hi_dur * (pre_div + 1);
hi_dur and (pre_div + 1) are both unsigned int (32-bit) values. Evaluating their multiplication using 32-bit arithmetic before assigning to the 64-bit u64 hi_div variable can overflow 32-bit unsigned math.
While effective_div explicitly uses (u64)(pwm_div + 1) * (pre_div + 1) to prevent overflow, hi_div was missing the (u64) cast.
Fix this by casting hi_dur to (u64) before multiplication, matching the precision used for effective_div.
--- drivers/pwm/pwm-ipq.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/pwm/pwm-ipq.c +1 -1
@@ -186,7 +186,7 @@ static int ipq_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm, state->period =DIV64_U64_ROUND_UP (effective_div *NSEC_PER_SEC , ipq_chip->clk_rate); - hi_div = hi_dur * (pre_div +1 ); + hi_div = (u64 )hi_dur * (pre_div +1 ); state->duty_cycle =DIV64_U64_ROUND_UP (hi_div *NSEC_PER_SEC , ipq_chip->clk_rate);
Signature · 1 line
2.55.0
Uwe Kleine-König
maintainerHello,
Quoted 9 lines · On Fri, Jul 24, 2026 at 10:18:54AM +0530, [email protected] wrote:
From: Surendra Singh Chouhan <[email protected]>
In
ipq_pwm_get_state(),hi_divwas calculated as:
hi_div=hi_dur* (pre_div+ 1);
hi_durand (pre_div+ 1) are both unsigned int (32-bit) values. Evaluating their multiplication using 32-bit arithmetic before assigning to the 64-bit u64hi_divvariable can overflow 32-bit unsigned math.
Quoted 8 lines
While
effective_divexplicitly uses (u64)(pwm_div + 1) * (pre_div+ 1) to prevent overflow,hi_divwas missing the (u64) cast.Fix this by casting
hi_durto (u64) before multiplication, matching the precision used foreffective_div.Fixes c436e3e9c265 ("pwm: Driver for qualcomm ipq6018 pwm block")Signed-off-by Surendra Singh Chouhan <[email protected]>
Applied to
https://git.kernel.org/pub/scm/linux/kernel/git/ukleinek/linux.git pwm/for-next
. Did you find this issue by code review or does it trigger easily? If the latter you might be able to convince me to send this patch to Linus before 7.3.
Best regards
Uwe
Hi Uwe,
Thanks for applying the patch!
I noticed this during code review while comparing how effective_div was calculated vs hi_div in ipq_pwm_get_state().
It can trigger easily on hardware whenever long periods or low clock frequencies are configured. Specifically, when (hi_dur * (pre_div + 1)) exceeds UINT_MAX (0xffffffff), 32-bit multiplication overflows before assignment to the 64-bit hi_div variable, resulting in an incorrect/truncated duty_cycle being reported via pwm_get_state() to userspace.
Sending it for 7.2 / current cycle as a bug fix would be great.
Thanks,
Surendra Singh Chouhan
Quoted 32 lines · Uwe Kleine-König
Hello,
Quoted 9 lines · On Fri, Jul 24, 2026 at 10:18:54AM +0530, [email protected] wrote:
From: Surendra Singh Chouhan <[email protected]>
In
ipq_pwm_get_state(),hi_divwas calculated as:
hi_div=hi_dur* (pre_div+ 1);
hi_durand (pre_div+ 1) are both unsigned int (32-bit) values. Evaluating their multiplication using 32-bit arithmetic before assigning to the 64-bit u64hi_divvariable can overflow 32-bit unsigned math.Quoted 8 lines
While
effective_divexplicitly uses (u64)(pwm_div + 1) * (pre_div+ 1) to prevent overflow,hi_divwas missing the (u64) cast.Fix this by casting
hi_durto (u64) before multiplication, matching the precision used foreffective_div.Fixes c436e3e9c265 ("pwm: Driver for qualcomm ipq6018 pwm block")Signed-off-by Surendra Singh Chouhan <[email protected]>Applied to
https://git.kernel.org/pub/scm/linux/kernel/git/ukleinek/linux.git pwm/for-next
. Did you find this issue by code review or does it trigger easily? If the latter you might be able to convince me to send this patch to Linus before 7.3.
Best regards
Uwe