mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 1/2] clocksource: imx-tpm: fix return -ETIME when delta exceeds INT_MAX
@ 2024-07-25 19:33 Frank Li
  2024-07-25 19:33 ` [PATCH 2/2] clocksource: imx-tpm: fix next event not taking effect sometime Frank Li
                   ` (4 more replies)
  0 siblings, 5 replies; 9+ messages in thread
From: Frank Li @ 2024-07-25 19:33 UTC (permalink / raw)
  To: Daniel Lezcano, Thomas Gleixner, Shawn Guo, Sascha Hauer,
	Pengutronix Kernel Team, Fabio Estevam, Dong Aisheng,
	open list:CLOCKSOURCE, CLOCKEVENT DRIVERS,
	open list:ARM/FREESCALE IMX / MXC ARM ARCHITECTURE,
	moderated list:ARM/FREESCALE IMX / MXC ARM ARCHITECTURE
  Cc: imx

From: Jacky Bai <ping.bai@nxp.com>

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>
---
 drivers/clocksource/timer-imx-tpm.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/drivers/clocksource/timer-imx-tpm.c b/drivers/clocksource/timer-imx-tpm.c
index bd64a8a8427f3..cd23caf1e5999 100644
--- a/drivers/clocksource/timer-imx-tpm.c
+++ b/drivers/clocksource/timer-imx-tpm.c
@@ -83,10 +83,10 @@ static u64 notrace tpm_read_sched_clock(void)
 static int tpm_set_next_event(unsigned long delta,
 				struct clock_event_device *evt)
 {
-	unsigned long next, now;
+	unsigned long next, prev, now;
 
-	next = tpm_read_counter();
-	next += delta;
+	prev = tpm_read_counter();
+	next = prev + delta;
 	writel(next, timer_base + TPM_C0V);
 	now = tpm_read_counter();
 
@@ -96,7 +96,7 @@ static int tpm_set_next_event(unsigned long delta,
 	 * of writing CNT registers which may cause the min_delta event got
 	 * missed, so we need add a ETIME check here in case it happened.
 	 */
-	return (int)(next - now) <= 0 ? -ETIME : 0;
+	return (now - prev) >= delta ? -ETIME : 0;
 }
 
 static int tpm_set_state_oneshot(struct clock_event_device *evt)
-- 
2.34.1


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2024-09-02 10:02 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-07-25 19:33 [PATCH 1/2] clocksource: imx-tpm: fix return -ETIME when delta exceeds INT_MAX Frank Li
2024-07-25 19:33 ` [PATCH 2/2] clocksource: imx-tpm: fix next event not taking effect sometime Frank Li
2024-09-02 10:02   ` [tip: timers/urgent] clocksource/drivers/imx-tpm: Fix " tip-bot2 for Jacky Bai
2024-08-14 16:47 ` [PATCH 1/2] clocksource: imx-tpm: fix return -ETIME when delta exceeds INT_MAX Frank Li
2024-08-19 13:21 ` Daniel Lezcano
2024-08-19 18:43   ` Frank Li
2024-08-21  7:20     ` Jacky Bai (OSS)
2024-09-02  9:29 ` Daniel Lezcano
2024-09-02 10:02 ` [tip: timers/urgent] clocksource/drivers/imx-tpm: Fix " tip-bot2 for Jacky Bai

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®