Skip to content

Commit 5b8843f

Browse files
JackyBaidlezcano
authored andcommitted
clocksource/drivers/imx-tpm: Fix return -ETIME when delta exceeds INT_MAX
In tpm_set_next_event(delta), return -ETIME by wrong cast to int when delta is larger than INT_MAX. For example: tpm_set_next_event(delta = 0xffff_fffe) { ... next = tpm_read_counter(); // assume next is 0x10 next += delta; // next will 0xffff_fffe + 0x10 = 0x1_0000_000e now = tpm_read_counter(); // now is 0x10 ... return (int)(next - now) <= 0 ? -ETIME : 0; ^^^^^^^^^^ 0x1_0000_000e - 0x10 = 0xffff_fffe, which is -2 when cast to int. So return -ETIME. } To fix this, introduce a 'prev' variable and check if 'now - prev' is larger than delta. Cc: stable@vger.kernel.org Fixes: 059ab7b ("clocksource/drivers/imx-tpm: Add imx tpm timer support") Signed-off-by: Jacky Bai <ping.bai@nxp.com> Reviewed-by: Peng Fan <peng.fan@nxp.com> Reviewed-by: Ye Li <ye.li@nxp.com> Reviewed-by: Jason Liu <jason.hui.liu@nxp.com> Signed-off-by: Frank Li <Frank.Li@nxp.com> Link: https://lore.kernel.org/r/20240725193355.1436005-1-Frank.Li@nxp.com Signed-off-by: Daniel Lezcano <daniel.lezcano@linaro.org>
1 parent 471ef0b commit 5b8843f

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

drivers/clocksource/timer-imx-tpm.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,10 @@ static u64 notrace tpm_read_sched_clock(void)
8383
static int tpm_set_next_event(unsigned long delta,
8484
struct clock_event_device *evt)
8585
{
86-
unsigned long next, now;
86+
unsigned long next, prev, now;
8787

88-
next = tpm_read_counter();
89-
next += delta;
88+
prev = tpm_read_counter();
89+
next = prev + delta;
9090
writel(next, timer_base + TPM_C0V);
9191
now = tpm_read_counter();
9292

@@ -96,7 +96,7 @@ static int tpm_set_next_event(unsigned long delta,
9696
* of writing CNT registers which may cause the min_delta event got
9797
* missed, so we need add a ETIME check here in case it happened.
9898
*/
99-
return (int)(next - now) <= 0 ? -ETIME : 0;
99+
return (now - prev) >= delta ? -ETIME : 0;
100100
}
101101

102102
static int tpm_set_state_oneshot(struct clock_event_device *evt)

0 commit comments

Comments
 (0)