Skip to contentSkip to conversation

clocksource/drivers/nxp-stm: Register sched_clock once

PATCH In review Juhee Kang 2 participants 3 messages Oct 15 – Nov 11

Subsystems (3)
lore
Share & tools
lore

Juhee Kang

author
#0 lore raw

PATCH v2 1/1 clocksource/drivers/nxp-stm: Register sched_clock once

Each probe updates stm_sched_clock before calling sched_clock_register() again. The call then subtracts the counter value saved during the previous registration from the new STM's counter. If the new counter is smaller, the subtraction wraps and the timestamp jumps forward. (e.g. from 0.4 to 32.9 seconds below)

  [    0.403653] sched_clock: 32 bits at 132MHz, resolution 7ns, wraps every 16243448316ns
  [    0.404056] clocksource: timer@40120000: mask: 0xffffffff max_cycles: 0xffffffff, max_idle_ns: 14456668980 ns
  [   32.890509] sched_clock: 32 bits at 132MHz, resolution 7ns, wraps every 16243448316ns
  [   32.890882] clocksource: timer@40124000: mask: 0xffffffff max_cycles: 0xffffffff, max_idle_ns: 14456668980 ns

To fix this, register sched_clock once at the end of the first successful probe. Clocksource and per-CPU clockevent registration remain unchanged.

Fixes cec32ac75827 ("clocksource/drivers/nxp-timer: Add the System Timer Module for the s32gx platforms")

---
Changes in v2:
- add the comment

---
 drivers/clocksource/timer-nxp-stm.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
drivers/clocksource/timer-nxp-stm.c +9 -4
index 6fe098a4a33f..23b2d44e4b78 100644
@@ -211,10 +211,6 @@ static int nxp_stm_clocksource_init(struct device *dev, struct stm_timer *stm_ti
 	if (ret)
 		return ret;
 
-	stm_sched_clock = stm_timer;
-
-	sched_clock_register(nxp_stm_read_sched_clock, 32, stm_timer->rate);
-
 	dev_dbg(dev, "Registered clocksource %s\n", name);
 
 	return 0;
@@ -473,6 +469,15 @@ static int nxp_stm_timer_probe(struct platform_device *pdev)
 			return ret;
 	}
 
+	/*
+	 * STM counters run independently at the same rate. Use the first
+	 * probed instance for sched_clock to avoid timestamp jumps.
+	 */
+	if (!stm_sched_clock) {
+		stm_sched_clock = stm_timer;
+		sched_clock_register(nxp_stm_read_sched_clock, 32, stm_timer->rate);
+	}
+
 	return 0;
 }
 
Signature · 1 line
2.55.0
Tested-by on v2 1/1
#1 lore raw
Quoted 60 lines · Juhee Kang

[You don't often get email from [email protected]. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]

Each probe updates stm_sched_clock before calling sched_clock_register() again. The call then subtracts the counter value saved during the previous registration from the new STM's counter. If the new counter is smaller, the subtraction wraps and the timestamp jumps forward. (e.g. from 0.4 to 32.9 seconds below)

  [    0.403653] sched_clock: 32 bits at 132MHz, resolution 7ns, wraps every 16243448316ns
  [    0.404056] clocksource: timer@40120000: mask: 0xffffffff max_cycles: 0xffffffff, max_idle_ns: 14456668980 ns
  [   32.890509] sched_clock: 32 bits at 132MHz, resolution 7ns, wraps every 16243448316ns
  [   32.890882] clocksource: timer@40124000: mask: 0xffffffff max_cycles: 0xffffffff, max_idle_ns: 14456668980 ns

To fix this, register sched_clock once at the end of the first successful probe. Clocksource and per-CPU clockevent registration remain unchanged.

Fixes cec32ac75827 ("clocksource/drivers/nxp-timer: Add the System Timer Module for the s32gx platforms")

---
Changes in v2:
- add the comment

---
 drivers/clocksource/timer-nxp-stm.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
drivers/clocksource/timer-nxp-stm.c +9 -5
index 6fe098a4a33f..23b2d44e4b78 100644
@@ -211,10 +211,6 @@ static int nxp_stm_clocksource_init(struct device *dev, struct stm_timer *stm_ti
        if (ret)
                return ret;
 
-       stm_sched_clock = stm_timer;
-
-       sched_clock_register(nxp_stm_read_sched_clock, 32, stm_timer->rate);
-
        dev_dbg(dev, "Registered clocksource %s\n", name);
 
        return 0;
@@ -473,6 +469,15 @@ static int nxp_stm_timer_probe(struct platform_device *pdev)
                        return ret;
        }
 
+       /*
+        * STM counters run independently at the same rate. Use the first
+        * probed instance for sched_clock to avoid timestamp jumps.
+        */
+       if (!stm_sched_clock) {
+               stm_sched_clock = stm_timer;
+               sched_clock_register(nxp_stm_read_sched_clock, 32, stm_timer->rate);
+       }
+
        return 0;
 }
 
--
Quoted 1 line

2.55.0

Thank you for addressing this issue!

Signature · 2 lines
Regards,
Ghennadi
author
#2 lore raw

Hi Daniel,

Just following up on this v2. Ghennadi reviewed and tested the patch. Could you take a look when you have a chance and let me know if anything else is needed?

Thanks.

Quoted 72 lines · Ghennadi Procopciuc
Quoted 60 lines · Juhee Kang

[You don't often get email from [email protected]. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]

Each probe updates stm_sched_clock before calling sched_clock_register() again. The call then subtracts the counter value saved during the previous registration from the new STM's counter. If the new counter is smaller, the subtraction wraps and the timestamp jumps forward. (e.g. from 0.4 to 32.9 seconds below)

  [    0.403653] sched_clock: 32 bits at 132MHz, resolution 7ns, wraps every 16243448316ns
  [    0.404056] clocksource: timer@40120000: mask: 0xffffffff max_cycles: 0xffffffff, max_idle_ns: 14456668980 ns
  [   32.890509] sched_clock: 32 bits at 132MHz, resolution 7ns, wraps every 16243448316ns
  [   32.890882] clocksource: timer@40124000: mask: 0xffffffff max_cycles: 0xffffffff, max_idle_ns: 14456668980 ns

To fix this, register sched_clock once at the end of the first successful probe. Clocksource and per-CPU clockevent registration remain unchanged.

Fixes cec32ac75827 ("clocksource/drivers/nxp-timer: Add the System Timer Module for the s32gx platforms")

---
Changes in v2:
- add the comment

---
 drivers/clocksource/timer-nxp-stm.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
drivers/clocksource/timer-nxp-stm.c +9 -5
index 6fe098a4a33f..23b2d44e4b78 100644
@@ -211,10 +211,6 @@ static int nxp_stm_clocksource_init(struct device *dev, struct stm_timer *stm_ti
        if (ret)
                return ret;
 
-       stm_sched_clock = stm_timer;
-
-       sched_clock_register(nxp_stm_read_sched_clock, 32, stm_timer->rate);
-
        dev_dbg(dev, "Registered clocksource %s\n", name);
 
        return 0;
@@ -473,6 +469,15 @@ static int nxp_stm_timer_probe(struct platform_device *pdev)
                        return ret;
        }
 
+       /*
+        * STM counters run independently at the same rate. Use the first
+        * probed instance for sched_clock to avoid timestamp jumps.
+        */
+       if (!stm_sched_clock) {
+               stm_sched_clock = stm_timer;
+               sched_clock_register(nxp_stm_read_sched_clock, 32, stm_timer->rate);
+       }
+
        return 0;
 }
 
--
Quoted 1 line

2.55.0

Thank you for addressing this issue!

--
Regards,
Ghennadi