Skip to contentSkip to conversation

clocksource: imx-tpm: fix return -ETIME when delta exceeds INT_MAX

PATCH Applied Frank Li 4 participants 9 messages Jul 25, 2024 – Sep 2, 2024

Subsystems (3)
lore
Share & tools
lore

Frank Li

author maintainer
#0 lore raw

PATCH 1/2 clocksource: imx-tpm: fix return -ETIME when delta exceeds INT_MAX

From: Jacky Bai <[email protected]>

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.

Fixes 059ab7b82eec ("clocksource/drivers/imx-tpm: Add imx tpm timer support")
---
 drivers/clocksource/timer-imx-tpm.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/clocksource/timer-imx-tpm.c +4 -4
index bd64a8a8427f3..cd23caf1e5999 100644
@@ -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)
Signature · 1 line
2.34.1
author maintainer
#1 lore raw

PATCH 2/2 clocksource: imx-tpm: fix next event not taking effect sometime

From: Jacky Bai <[email protected]>

The value written into the TPM CnV can only be updated into the hardware when the counter increases. Additional writes to the CnV write buffer are ignored until the register has been updated. Therefore, we need to check if the CnV has been updated before continuing. This may require waiting for 1 counter cycle in the worst case.

Fixes 059ab7b82eec ("clocksource/drivers/imx-tpm: Add imx tpm timer support")
---
 drivers/clocksource/timer-imx-tpm.c | 8 ++++++++
 1 file changed, 8 insertions(+)
drivers/clocksource/timer-imx-tpm.c +8 -0
index cd23caf1e5999..92c025b70eb62 100644
@@ -90,6 +90,14 @@ static int tpm_set_next_event(unsigned long delta,
 	writel(next, timer_base + TPM_C0V);
 	now = tpm_read_counter();
 
+	/*
+	 * Need to wait CNT increase at least 1 cycle to make sure
+	 * the C0V has been updated into HW.
+	 */
+	if ((next & 0xffffffff) != readl(timer_base + TPM_C0V))
+		while (now == tpm_read_counter())
+			;
+
 	/*
 	 * NOTE: We observed in a very small probability, the bus fabric
 	 * contention between GPU and A7 may results a few cycles delay
Signature · 1 line
2.34.1
merge bot Applied
#2 lore raw

[tip: timers/urgent] clocksource/drivers/imx-tpm: Fix next event not taking effect sometime

The following commit has been merged into the timers/urgent branch of tip:

The following commit has been merged into the timers/urgent branch of tip:

Commit-ID:     3d5c2f8e75a55cfb11a85086c71996af0354a1fb
Gitweb:        https://git.kernel.org/tip/3d5c2f8e75a55cfb11a85086c71996af0354a1fb
Author:        Jacky Bai <[email protected]>
AuthorDate:    Thu, 25 Jul 2024 15:33:55 -04:00
Committer:     Daniel Lezcano <[email protected]>
CommitterDate: Mon, 02 Sep 2024 10:04:15 +02:00

clocksource/drivers/imx-tpm: Fix next event not taking effect sometime

The value written into the TPM CnV can only be updated into the hardware when the counter increases. Additional writes to the CnV write buffer are ignored until the register has been updated. Therefore, we need to check if the CnV has been updated before continuing. This may require waiting for 1 counter cycle in the worst case.

---
 drivers/clocksource/timer-imx-tpm.c | 8 ++++++++
 1 file changed, 8 insertions(+)
drivers/clocksource/timer-imx-tpm.c +8 -0
index cd23caf..92c025b 100644
@@ -91,6 +91,14 @@ static int tpm_set_next_event(unsigned long delta,
 	now = tpm_read_counter();
 
 	/*
+	 * Need to wait CNT increase at least 1 cycle to make sure
+	 * the C0V has been updated into HW.
+	 */
+	if ((next & 0xffffffff) != readl(timer_base + TPM_C0V))
+		while (now == tpm_read_counter())
+			;
+
+	/*
 	 * NOTE: We observed in a very small probability, the bus fabric
 	 * contention between GPU and A7 may results a few cycles delay
 	 * of writing CNT registers which may cause the min_delta event got
author maintainer
#3 lore raw
In reply to Frank Li
Quoted 24 lines · Frank Li

From: Jacky Bai <[email protected]>

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.

Quoted 8 lines
Fixes 059ab7b82eec ("clocksource/drivers/imx-tpm: Add imx tpm timer support")

---

Ping?

Quoted 33 lines
 drivers/clocksource/timer-imx-tpm.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/clocksource/timer-imx-tpm.c +4 -5
index bd64a8a8427f3..cd23caf1e5999 100644
@@ -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

#4 lore raw
In reply to Frank Li
Quoted 4 lines · Frank Li

From: Jacky Bai <[email protected]>

In tpm_set_next_event(delta), return -ETIME by wrong cast to int when delta is larger than INT_MAX.

Is it something you observed or from your understanding of the code ?

Quoted 57 lines

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.

Fixes 059ab7b82eec ("clocksource/drivers/imx-tpm: Add imx tpm timer support")
---
  drivers/clocksource/timer-imx-tpm.c | 8 ++++----
  1 file changed, 4 insertions(+), 4 deletions(-)
drivers/clocksource/timer-imx-tpm.c +4 -4
index bd64a8a8427f3..cd23caf1e5999 100644
@@ -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)
Signature · 5 lines
<http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs

Follow Linaro:  <http://www.facebook.com/pages/Linaro> Facebook |
<http://twitter.com/#!/linaroorg> Twitter |
<http://www.linaro.org/linaro-blog/> Blog
author maintainer
#5 lore raw
Quoted 7 lines · Daniel Lezcano
Quoted 4 lines · Frank Li

From: Jacky Bai <[email protected]>

In tpm_set_next_event(delta), return -ETIME by wrong cast to int when delta is larger than INT_MAX.

Is it something you observed or from your understanding of the code ?

System use this timer to wakeup from lower idle, which core and local timer turn off. -ETIME may prevent system enter lower idle, or failure wakeup from lower idle utill counter run overflow. I am not sure how to trigger this problem.

Assume run at 24Mhz, 1/24us * 0x8000_0000 is around 89s. Problem trigger only next event bigger 89s, which relative quite big number.

Anyways, the original code is wrong, it claim max delta is 32bit.

Jack bai: any additonal supplement for this?

Frank

Quoted 63 lines
Quoted 48 lines

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.

Fixes 059ab7b82eec ("clocksource/drivers/imx-tpm: Add imx tpm timer support")
---
  drivers/clocksource/timer-imx-tpm.c | 8 ++++----
  1 file changed, 4 insertions(+), 4 deletions(-)
drivers/clocksource/timer-imx-tpm.c +3 -3
index bd64a8a8427f3..cd23caf1e5999 100644
@@ -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();

drivers/clocksource/timer-imx-tpm.c static int tpm_set_next_event(unsigned long delta, · line 101

Quoted 7 lines
drivers/clocksource/timer-imx-tpm.c +1 -1
@@ -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)

--
<http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs

Follow Linaro: <http://www.facebook.com/pages/Linaro> Facebook |
<http://twitter.com/#!/linaroorg> Twitter |
<http://www.linaro.org/linaro-blog/> Blog

#6 lore raw
Quoted 16 lines

Subject: Re: [PATCH 1/2] clocksource: imx-tpm: fix return -ETIME when delta exceeds INT_MAX

Quoted 7 lines · Daniel Lezcano
Quoted 4 lines · Frank Li

From: Jacky Bai <[email protected]>

In tpm_set_next_event(delta), return -ETIME by wrong cast to int when delta is larger than INT_MAX.

Is it something you observed or from your understanding of the code ?

System use this timer to wakeup from lower idle, which core and local timer turn off. -ETIME may prevent system enter lower idle, or failure wakeup from lower idle utill counter run overflow. I am not sure how to trigger this problem.

Quoted 7 lines

Assume run at 24Mhz, 1/24us * 0x8000_0000 is around 89s. Problem trigger only next event bigger 89s, which relative quite big number.

Anyways, the original code is wrong, it claim max delta is 32bit.

Jack bai: any additonal supplement for this?

The issue can be triggered in below function, it is possible max_delta will be assigned to delta, So the original int case logic will report wrong '-ETIME' error, then 'clockevents_program_min_delta' Will be called to reprogram the timer, due to the tpm limitation mentioned in patch 2/2, this min_delta program will not take effect.

int clockevents_program_event(struct clock_event_device *dev, ktime_t expires,

                        bool force) {
	...
	delta = min(delta, (int64_t) dev->max_delta_ns);
	delta = max(delta, (int64_t) dev->min_delta_ns);

	clc = ((unsigned long long) delta * dev->mult) >> dev->shift;
	rc = dev->set_next_event((unsigned long) clc, dev);

	return (rc && force) ? clockevents_program_min_delta(dev) : rc;
}

BR

Quoted 68 lines

Frank

Quoted 10 lines
Quoted 10 lines

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
         ...
Quoted 3 lines
Quoted 3 lines
         return (int)(next - now) <= 0 ? -ETIME : 0;
                      ^^^^^^^^^^
                      0x1_0000_000e - 0x10 = 0xffff_fffe, which is -2

when

Quoted 52 lines
Quoted 36 lines

cast to int. So return -ETIME.
}

To fix this, introduce a 'prev' variable and check if 'now - prev' is larger than delta.

Fixes 059ab7b82eec ("clocksource/drivers/imx-tpm: Add imx tpm timer

support")

---
  drivers/clocksource/timer-imx-tpm.c | 8 ++++----
  1 file changed, 4 insertions(+), 4 deletions(-)
drivers/clocksource/timer-imx-tpm.c +3 -3
index bd64a8a8427f3..cd23caf1e5999 100644
@@ -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();

drivers/clocksource/timer-imx-tpm.c static int tpm_set_next_event(unsigned long delta, · line 101

Quoted 7 lines
drivers/clocksource/timer-imx-tpm.c +1 -1
@@ -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)

--
<http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs

Follow Linaro: <http://www.facebook.com/pages/Linaro> Facebook |
<http://twitter.com/#!/linaroorg> Twitter |
<http://www.linaro.org/linaro-blog/> Blog

#7 lore raw
In reply to Frank Li
Quoted 24 lines · Frank Li

From: Jacky Bai <[email protected]>

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.

Quoted 8 lines
Fixes 059ab7b82eec ("clocksource/drivers/imx-tpm: Add imx tpm timer support")

---

Applied, thanks

Signature · 5 lines
<http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs

Follow Linaro:  <http://www.facebook.com/pages/Linaro> Facebook |
<http://twitter.com/#!/linaroorg> Twitter |
<http://www.linaro.org/linaro-blog/> Blog
merge bot Applied
#8 lore raw

[tip: timers/urgent] clocksource/drivers/imx-tpm: Fix return -ETIME when delta exceeds INT_MAX

In reply to Frank Li
The following commit has been merged into the timers/urgent branch of tip:

The following commit has been merged into the timers/urgent branch of tip:

Commit-ID:     5b8843fcd49827813da80c0f590a17ae4ce93c5d
Gitweb:        https://git.kernel.org/tip/5b8843fcd49827813da80c0f590a17ae4ce93c5d
Author:        Jacky Bai <[email protected]>
AuthorDate:    Thu, 25 Jul 2024 15:33:54 -04:00
Committer:     Daniel Lezcano <[email protected]>
CommitterDate: Mon, 02 Sep 2024 10:04:15 +02:00

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.

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