Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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: 059ab7b82eec ("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>

authored by

Jacky Bai and committed by
Daniel Lezcano
5b8843fc 471ef0b5

+4 -4
+4 -4
drivers/clocksource/timer-imx-tpm.c
··· 83 83 static int tpm_set_next_event(unsigned long delta, 84 84 struct clock_event_device *evt) 85 85 { 86 - unsigned long next, now; 86 + unsigned long next, prev, now; 87 87 88 - next = tpm_read_counter(); 89 - next += delta; 88 + prev = tpm_read_counter(); 89 + next = prev + delta; 90 90 writel(next, timer_base + TPM_C0V); 91 91 now = tpm_read_counter(); 92 92 ··· 96 96 * of writing CNT registers which may cause the min_delta event got 97 97 * missed, so we need add a ETIME check here in case it happened. 98 98 */ 99 - return (int)(next - now) <= 0 ? -ETIME : 0; 99 + return (now - prev) >= delta ? -ETIME : 0; 100 100 } 101 101 102 102 static int tpm_set_state_oneshot(struct clock_event_device *evt)