Skip to contentSkip to conversation

sched_clock: Add option to use absolute time against hardware clock reset

PATCH In review Feng Tang 4 participants 11 messages Sep 2 – Sep 7

lore
Share & tools
lore

Feng Tang

author
#0 lore raw

PATCH 1/1 sched_clock: Add option to use absolute time against hardware clock reset

Currently sched_clock shows the relative time to the boot starting of kernel, while there could be long firmware start time before it and after the hardware reset.

On modern server platforms, there could be several software running in parallel. Like for arm64, it could have SCP (System Control Processor) firmware running on SCP processor, and ATF (Arm Trusted Firmware) and Linux OS on the main processor.

Debugging some nasty issues on these platform may need to cross-check the logs from these firmwares and Linux kernel for specific events, where a unified reference timeline is critical. All these software can read the hardware timer, which is also the base of sched_clock for Linux kernel. Using the absolute counter since hardware timer reset makes it possible for all kinds of software to have a same time base.

Add 'abs_sched_clock' parameter to provide an option for using absolute counter, and users should make sure their sched_clock (hardware timer) is capable of supporting absolute counter before enabling the option.

Locally, it did help on chasing some RAS issues which needed cooperation between kernel, SCP firmware and ATF, by mapping the actions from each players into one timeline based on the timestamps in their logs.

---
 kernel/time/sched_clock.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
kernel/time/sched_clock.c +11 -3
index f3aaef695b8c..321c580d6799 100644
@@ -49,6 +49,10 @@ static int irqtime = -1;
 
 core_param(irqtime, irqtime, int, 0400);
 
+/* Whether to use the absolute counter since the clock hardware reset */
+static bool abs_sched_clock;
+core_param(abs_sched_clock, abs_sched_clock, bool, 0400);
+
 static u64 notrace jiffy_sched_clock_read(void)
 {
 	/*
@@ -200,10 +204,14 @@ void sched_clock_register(u64 (*read)(void), int bits, unsigned long rate)
 
 	rd = cd.read_data[0];
 
-	/* Update epoch for new counter and update 'epoch_ns' from old counter*/
+	/* Update epoch for new counter and update 'epoch_ns' */
 	new_epoch = read();
-	cyc = cd.actual_read_sched_clock();
-	ns = rd.epoch_ns + cyc_to_ns((cyc - rd.epoch_cyc) & rd.sched_clock_mask, rd.mult, rd.shift);
+	if (abs_sched_clock) {
+		ns = cyc_to_ns(new_epoch & new_mask, new_mult, new_shift);
+	} else {
+		cyc = cd.actual_read_sched_clock();
+		ns = rd.epoch_ns + cyc_to_ns((cyc - rd.epoch_cyc) & rd.sched_clock_mask, rd.mult, rd.shift);
+	}
 	cd.actual_read_sched_clock = read;
 
 	rd.read_sched_clock	= read;
Signature · 1 line
2.39.5 (Apple Git-154)
#1 lore raw
Quoted 17 lines · Feng Tang

Currently sched_clock shows the relative time to the boot starting of kernel, while there could be long firmware start time before it and after the hardware reset.

On modern server platforms, there could be several software running in parallel. Like for arm64, it could have SCP (System Control Processor) firmware running on SCP processor, and ATF (Arm Trusted Firmware) and Linux OS on the main processor.

Debugging some nasty issues on these platform may need to cross-check the logs from these firmwares and Linux kernel for specific events, where a unified reference timeline is critical. All these software can read the hardware timer, which is also the base of sched_clock for Linux kernel. Using the absolute counter since hardware timer reset makes it possible for all kinds of software to have a same time base.

Quoted 7 lines

Add 'abs_sched_clock' parameter to provide an option for using absolute counter, and users should make sure their sched_clock (hardware timer) is capable of supporting absolute counter before enabling the option.

Locally, it did help on chasing some RAS issues which needed cooperation between kernel, SCP firmware and ATF, by mapping the actions from each players into one timeline based on the timestamps in their logs.

I really have to ask: why isn't this just a one-off sampling of the counter, kept in some user accessible location (debugfs or something else), and ultimately post-processed to align your logs? People have been doing this... forever, and that has been "good enough" so far.

The other thing is that your "absolute" clock isn't absolute at all. This doesn't consider SW running at EL2 that could happily offset thing by an arbitrary value.

I appreciate this is not what your case, but I'm somewhat reluctant to burden the kernel with something that is, by definition, unreliable.

Thanks,

	M.
Signature · 1 line
Jazz isn't dead. It just smells funny.
#2 lore raw
Quoted 23 lines · Marc Zyngier
Quoted 21 lines · Feng Tang

Currently sched_clock shows the relative time to the boot starting of kernel, while there could be long firmware start time before it and after the hardware reset.

On modern server platforms, there could be several software running in parallel. Like for arm64, it could have SCP (System Control Processor) firmware running on SCP processor, and ATF (Arm Trusted Firmware) and Linux OS on the main processor.

Debugging some nasty issues on these platform may need to cross-check the logs from these firmwares and Linux kernel for specific events, where a unified reference timeline is critical. All these software can read the hardware timer, which is also the base of sched_clock for Linux kernel. Using the absolute counter since hardware timer reset makes it possible for all kinds of software to have a same time base.

Add 'abs_sched_clock' parameter to provide an option for using absolute counter, and users should make sure their sched_clock (hardware timer) is capable of supporting absolute counter before enabling the option.

Quoted 9 lines
Quoted 3 lines

Locally, it did help on chasing some RAS issues which needed cooperation between kernel, SCP firmware and ATF, by mapping the actions from each players into one timeline based on the timestamps in their logs.

I really have to ask: why isn't this just a one-off sampling of the counter, kept in some user accessible location (debugfs or something else), and ultimately post-processed to align your logs? People have been doing this... forever, and that has been "good enough" so far.

Hi Marc,

Quoted 3 lines

The other thing is that your "absolute" clock isn't absolute at all. This doesn't consider SW running at EL2 that could happily offset thing by an arbitrary value.

Do you mean the VM case that VM's vcounter can be changed by hypervisor in EL2, Thus it's not that absolute in such scenario ?

Quoted 10 lines

I appreciate this is not what your case, but I'm somewhat reluctant to burden the kernel with something that is, by definition, unreliable.

Thanks,

	M.

--
Jazz isn't dead. It just smells funny.

#3 lore raw
Quoted 35 lines · Yao Yuan
Quoted 23 lines · Marc Zyngier
Quoted 21 lines · Feng Tang

Currently sched_clock shows the relative time to the boot starting of kernel, while there could be long firmware start time before it and after the hardware reset.

On modern server platforms, there could be several software running in parallel. Like for arm64, it could have SCP (System Control Processor) firmware running on SCP processor, and ATF (Arm Trusted Firmware) and Linux OS on the main processor.

Debugging some nasty issues on these platform may need to cross-check the logs from these firmwares and Linux kernel for specific events, where a unified reference timeline is critical. All these software can read the hardware timer, which is also the base of sched_clock for Linux kernel. Using the absolute counter since hardware timer reset makes it possible for all kinds of software to have a same time base.

Add 'abs_sched_clock' parameter to provide an option for using absolute counter, and users should make sure their sched_clock (hardware timer) is capable of supporting absolute counter before enabling the option.

Quoted 9 lines
Quoted 3 lines

Locally, it did help on chasing some RAS issues which needed cooperation between kernel, SCP firmware and ATF, by mapping the actions from each players into one timeline based on the timestamps in their logs.

I really have to ask: why isn't this just a one-off sampling of the counter, kept in some user accessible location (debugfs or something else), and ultimately post-processed to align your logs? People have been doing this... forever, and that has been "good enough" so far.

Quoted 8 lines

Hi Marc,

Quoted 3 lines

The other thing is that your "absolute" clock isn't absolute at all. This doesn't consider SW running at EL2 that could happily offset thing by an arbitrary value.

Do you mean the VM case that VM's vcounter can be changed by hypervisor in EL2, Thus it's not that absolute in such scenario ?

That's indeed one of the possibilities. EL2 controls both virtual and physical offsets, and therefore provides the kernel with a different view of time.

This doesn't even have to be a VM. There is a lot of non-hypervisor SW out there that just hogs EL2 for more or less nefarious purposes (such as "protecting" the kernel), and offsetting the counter values is one of thing they could do to hide what they are doing.

The other thing is that this change seems to break the sched_clock() handover, since the new clock doesn't start where the old one ends. This doesn't affect arm64, which can only have one true source of time, but other archs would probably suffer from this.

	M.
Signature · 1 line
Jazz isn't dead. It just smells funny.
author
#4 lore raw

On Thu, Sep 03, 2026 at 08:38:57AM +0100, Marc Zyngier wrote:
[...]

Quoted 9 lines
Quoted 8 lines

Hi Marc,

Quoted 3 lines

The other thing is that your "absolute" clock isn't absolute at all. This doesn't consider SW running at EL2 that could happily offset thing by an arbitrary value.

Do you mean the VM case that VM's vcounter can be changed by hypervisor in EL2, Thus it's not that absolute in such scenario ?

Quoted 8 lines

That's indeed one of the possibilities. EL2 controls both virtual and physical offsets, and therefore provides the kernel with a different view of time.

This doesn't even have to be a VM. There is a lot of non-hypervisor SW out there that just hogs EL2 for more or less nefarious purposes (such as "protecting" the kernel), and offsetting the counter values is one of thing they could do to hide what they are doing.

Thanks for the info.

Manipulating time counter(sched_clock) runtimely doesn't sound like a good thing. There used to be similar things happened for TSC on x86 platforms, which Thomas has mentioned several times :)

Quoted 5 lines

The other thing is that this change seems to break the sched_clock() handover, since the new clock doesn't start where the old one ends. This doesn't affect arm64, which can only have one true source of time, but other archs would probably suffer from this.

Yes, I has similar concern. And for sched_clock, it usually should be done by a very low read cost, high precison, and better per-cpu timer. I only have some knowledge about x86 and arm64, and don't know whether there is other architecture that really switches sched_clock .

Thanks,
Feng

#5 lore raw
In reply to Marc Zyngier
Quoted 46 lines · Marc Zyngier
Quoted 35 lines · Yao Yuan
Quoted 23 lines · Marc Zyngier
Quoted 21 lines · Feng Tang

Currently sched_clock shows the relative time to the boot starting of kernel, while there could be long firmware start time before it and after the hardware reset.

On modern server platforms, there could be several software running in parallel. Like for arm64, it could have SCP (System Control Processor) firmware running on SCP processor, and ATF (Arm Trusted Firmware) and Linux OS on the main processor.

Debugging some nasty issues on these platform may need to cross-check the logs from these firmwares and Linux kernel for specific events, where a unified reference timeline is critical. All these software can read the hardware timer, which is also the base of sched_clock for Linux kernel. Using the absolute counter since hardware timer reset makes it possible for all kinds of software to have a same time base.

Add 'abs_sched_clock' parameter to provide an option for using absolute counter, and users should make sure their sched_clock (hardware timer) is capable of supporting absolute counter before enabling the option.

Quoted 9 lines
Quoted 3 lines

Locally, it did help on chasing some RAS issues which needed cooperation between kernel, SCP firmware and ATF, by mapping the actions from each players into one timeline based on the timestamps in their logs.

I really have to ask: why isn't this just a one-off sampling of the counter, kept in some user accessible location (debugfs or something else), and ultimately post-processed to align your logs? People have been doing this... forever, and that has been "good enough" so far.

Quoted 8 lines

Hi Marc,

Quoted 3 lines

The other thing is that your "absolute" clock isn't absolute at all. This doesn't consider SW running at EL2 that could happily offset thing by an arbitrary value.

Do you mean the VM case that VM's vcounter can be changed by hypervisor in EL2, Thus it's not that absolute in such scenario ?

Quoted 8 lines

That's indeed one of the possibilities. EL2 controls both virtual and physical offsets, and therefore provides the kernel with a different view of time.

This doesn't even have to be a VM. There is a lot of non-hypervisor SW out there that just hogs EL2 for more or less nefarious purposes (such as "protecting" the kernel), and offsetting the counter values is one of thing they could do to hide what they are doing.

Thanks for the details !
I see, this yet like some hypervisor hide its handling time from guest to me :-)

Quoted 5 lines

The other thing is that this change seems to break the sched_clock() handover, since the new clock doesn't start where the old one ends. This doesn't affect arm64, which can only have one true source of time, but other archs would probably suffer from this.

Yes, we can discuss this more in Feng's reply, thanks!

Quoted 5 lines
	M.

--
Jazz isn't dead. It just smells funny.

author
#6 lore raw
In reply to Marc Zyngier
Quoted 19 lines · Marc Zyngier
Quoted 17 lines · Feng Tang

Currently sched_clock shows the relative time to the boot starting of kernel, while there could be long firmware start time before it and after the hardware reset.

On modern server platforms, there could be several software running in parallel. Like for arm64, it could have SCP (System Control Processor) firmware running on SCP processor, and ATF (Arm Trusted Firmware) and Linux OS on the main processor.

Debugging some nasty issues on these platform may need to cross-check the logs from these firmwares and Linux kernel for specific events, where a unified reference timeline is critical. All these software can read the hardware timer, which is also the base of sched_clock for Linux kernel. Using the absolute counter since hardware timer reset makes it possible for all kinds of software to have a same time base.

Quoted 7 lines
Quoted 7 lines

Add 'abs_sched_clock' parameter to provide an option for using absolute counter, and users should make sure their sched_clock (hardware timer) is capable of supporting absolute counter before enabling the option.

Locally, it did help on chasing some RAS issues which needed cooperation between kernel, SCP firmware and ATF, by mapping the actions from each players into one timeline based on the timestamps in their logs.

Hi Marc,

Thanks for the great inputs!

I forgot to emphasized that this is only a debug option, mostly for bug chasing.

Quoted 4 lines

I really have to ask: why isn't this just a one-off sampling of the counter, kept in some user accessible location (debugfs or something else), and ultimately post-processed to align your logs? People have been doing this... forever, and that has been "good enough" so far.

This option is for chasing nasty bugs (involving SCP/ATF), which could be panic during boot, so debugfs may not work. But yes, we can achieve this by printing this out in dmesg.

I agree the post-processing logs works, while this option could be more convenient, for kernel developers, and firmware developers too :)

Quoted 3 lines

The other thing is that your "absolute" clock isn't absolute at all. This doesn't consider SW running at EL2 that could happily offset thing by an arbitrary value.

Good point! It may not work for guest environment. And the main purpose is to debug baremetal issues, which need to cross-check logs from SCP, ATF and kernel.

Quoted 2 lines

I appreciate this is not what your case, but I'm somewhat reluctant to burden the kernel with something that is, by definition, unreliable.

You are right! There are all kinds of hardware timers out there, many of which have various issues, and 'unreliable' for this usage.

The needed features I can think of for a HW timer to use absolute counter are:
* always running, won't stop on entering cpuidle or system suspend
* won't change frequency on cpufreq change
* the wrap period is big enough

I thought about using CLOCK_SOURCE_SUSPEND_NONSTOP to do a capability check, but it's not available in sched_clock.c.

So I mentioned in commit log that users enabling this option should make sure the HW counter is capable.

btw, fwiw, Sashiko also gave some good comments, like I shouldn't use cyc_to_ns() for calculating the epoch_ns, which should be solved by using mul_u64_u64_div_u64()

Thanks,
Feng

Quoted 7 lines

Thanks,

	M.
Signature · 1 line
Jazz isn't dead. It just smells funny.
#7 lore raw
In reply to Feng Tang
Quoted 3 lines · Feng Tang

Locally, it did help on chasing some RAS issues which needed cooperation between kernel, SCP firmware and ATF, by mapping the actions from each players into one timeline based on the timestamps in their logs.

I told you before that we don't care about your bug chasing war stories at all. Educate your firmware people and stop pestering us with your firmware debug hacks.

author
#8 lore raw
Quoted 8 lines · Thomas Gleixner
Quoted 3 lines · Feng Tang

Locally, it did help on chasing some RAS issues which needed cooperation between kernel, SCP firmware and ATF, by mapping the actions from each players into one timeline based on the timestamps in their logs.

I told you before that we don't care about your bug chasing war stories at all. Educate your firmware people and stop pestering us with your firmware debug hacks.

Hi Thomas,

Yes, this patch is hacky.

From the reply of Marc in this thread:

"
I really have to ask: why isn't this just a one-off sampling of the counter, kept in some user accessible location (debugfs or something else), and ultimately post-processed to align your logs? People have been doing this... forever, and that has been "good enough" so far. "

IIUC, getting an absolute timestamp for logs is still practical in some cases. How about adding the offset-since-reset info into the message like Marc suggested:

---

kernel/time/sched_clock.c +6 -3
index f3aaef695b8c..d5a54276b49d 100644
@@ -176,7 +176,7 @@ static enum hrtimer_restart sched_clock_poll(struct hrtimer *hrt)
 
 void sched_clock_register(u64 (*read)(void), int bits, unsigned long rate)
 {
-	u64 res, wrap, new_mask, new_epoch, cyc, ns;
+	u64 res, wrap, new_mask, new_epoch, cyc, ns, reset_ns;
 	u32 new_mult, new_shift;
 	unsigned long r, flags;
 	char r_unit;
@@ -235,8 +235,11 @@ void sched_clock_register(u64 (*read)(void), int bits, unsigned long rate)
 	/* Calculate the ns resolution of this counter */
 	res = cyc_to_ns(1ULL, new_mult, new_shift);
 
-	pr_info("sched_clock: %u bits at %lu%cHz, resolution %lluns, wraps every %lluns\n",
-		bits, r, r_unit, res, wrap);
+	/* Calculate the time since last counter resetting to 0 */
+	reset_ns = mul_u64_u64_div_u64(new_epoch, NSEC_PER_SEC, rate);
+
+	pr_info("sched_clock: %u bits at %lu%cHz, resolution %lluns, wraps every %lluns, has run %lluns since counter reset\n",
+		bits, r, r_unit, res, wrap, reset_ns);
 
 	/* Enable IRQ time accounting if we have a fast enough sched_clock() */
 	if (irqtime > 0 || (irqtime == -1 && rate >= 1000000))

Thanks,
Feng

#9 lore raw
Quoted 47 lines · Feng Tang
Quoted 8 lines · Thomas Gleixner
Quoted 3 lines · Feng Tang

Locally, it did help on chasing some RAS issues which needed cooperation between kernel, SCP firmware and ATF, by mapping the actions from each players into one timeline based on the timestamps in their logs.

I told you before that we don't care about your bug chasing war stories at all. Educate your firmware people and stop pestering us with your firmware debug hacks.

Hi Thomas,

Yes, this patch is hacky.

From the reply of Marc in this thread:

"
I really have to ask: why isn't this just a one-off sampling of the counter, kept in some user accessible location (debugfs or something else), and ultimately post-processed to align your logs? People have been doing this... forever, and that has been "good enough" so far. "

IIUC, getting an absolute timestamp for logs is still practical in some cases. How about adding the offset-since-reset info into the message like Marc suggested:

---

kernel/time/sched_clock.c +1 -1
index f3aaef695b8c..d5a54276b49d 100644
@@ -176,7 +176,7 @@ static enum hrtimer_restart sched_clock_poll(struct hrtimer *hrt)
 
 void sched_clock_register(u64 (*read)(void), int bits, unsigned long rate)
 {
-	u64 res, wrap, new_mask, new_epoch, cyc, ns;
+	u64 res, wrap, new_mask, new_epoch, cyc, ns, reset_ns;
 	u32 new_mult, new_shift;
 	unsigned long r, flags;
 	char r_unit;
@@ -235,8 +235,11 @@ void sched_clock_register(u64 (*read)(void), int bits, unsigned long rate)
 	/* Calculate the ns resolution of this counter */
 	res = cyc_to_ns(1ULL, new_mult, new_shift);
 

kernel/time/sched_clock.c void sched_clock_register(u64 (*read)(void), int bits, unsigned long rate) · line 245

Quoted 10 lines
kernel/time/sched_clock.c +5 -2
-	pr_info("sched_clock: %u bits at %lu%cHz, resolution %lluns, wraps every %lluns\n",
-		bits, r, r_unit, res, wrap);
+	/* Calculate the time since last counter resetting to 0 */
+	reset_ns = mul_u64_u64_div_u64(new_epoch, NSEC_PER_SEC, rate);
+
+	pr_info("sched_clock: %u bits at %lu%cHz, resolution %lluns, wraps every %lluns, has run %lluns since counter reset\n",
+		bits, r, r_unit, res, wrap, reset_ns);
 
 	/* Enable IRQ time accounting if we have a fast enough sched_clock() */
 	if (irqtime > 0 || (irqtime == -1 && rate >= 1000000))

Which is what people have done locally since the beginning of times. And the only time this is actually useful is when bringing up new HW/FW that is broken.

So why the need to put that in an upstream kernel, instead of being as part of your debug toolbox? I don't think the kernel shouldn't be the recipient of this sort of stuff.

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
author
#10 lore raw

On Mon, Sep 07, 2026 at 10:37:03AM +0100, Marc Zyngier wrote:
[...]

Quoted 16 lines

kernel/time/sched_clock.c void sched_clock_register(u64 (*read)(void), int bits, unsigned long rate) · line 245

Quoted 11 lines
kernel/time/sched_clock.c +5 -2
-	pr_info("sched_clock: %u bits at %lu%cHz, resolution %lluns, wraps every %lluns\n",
-		bits, r, r_unit, res, wrap);
+	/* Calculate the time since last counter resetting to 0 */
+	reset_ns = mul_u64_u64_div_u64(new_epoch, NSEC_PER_SEC, rate);
+
+	pr_info("sched_clock: %u bits at %lu%cHz, resolution %lluns, wraps every %lluns, has run %lluns since counter reset\n",
+		bits, r, r_unit, res, wrap, reset_ns);
 
 	/* Enable IRQ time accounting if we have a fast enough sched_clock() */
 	if (irqtime > 0 || (irqtime == -1 && rate >= 1000000))

Which is what people have done locally since the beginning of times. And the only time this is actually useful is when bringing up new HW/FW that is broken.

Quoted 3 lines

So why the need to put that in an upstream kernel, instead of being as part of your debug toolbox? I don't think the kernel shouldn't be the recipient of this sort of stuff.

I see your point now, thanks!

- Feng

Quoted 5 lines
	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.