* [PATCH] sched_clock: Add option to use absolute time against hardware clock reset
@ 2026-09-02 8:21 Feng Tang
2026-09-02 15:32 ` Marc Zyngier
2026-09-05 20:39 ` Thomas Gleixner
0 siblings, 2 replies; 8+ messages in thread
From: Feng Tang @ 2026-09-02 8:21 UTC (permalink / raw)
To: Thomas Gleixner, John Stultz, Stephen Boyd, Miroslav Lichvar,
Daniel Lezcano, Peter Zijlstra
Cc: Marc Zyngier, Petr Mladek, linux-kernel, 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.
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.
Signed-off-by: Feng Tang <feng.tang@linux.alibaba.com>
---
kernel/time/sched_clock.c | 14 +++++++++++---
1 file changed, 11 insertions(+), 3 deletions(-)
diff --git a/kernel/time/sched_clock.c b/kernel/time/sched_clock.c
index f3aaef695b8c..321c580d6799 100644
--- a/kernel/time/sched_clock.c
+++ b/kernel/time/sched_clock.c
@@ -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;
--
2.39.5 (Apple Git-154)
^ permalink raw reply [flat|nested] 8+ messages in thread* Re: [PATCH] sched_clock: Add option to use absolute time against hardware clock reset
2026-09-02 8:21 [PATCH] sched_clock: Add option to use absolute time against hardware clock reset Feng Tang
@ 2026-09-02 15:32 ` Marc Zyngier
2026-09-03 6:51 ` Yao Yuan
2026-09-03 7:45 ` Feng Tang
2026-09-05 20:39 ` Thomas Gleixner
1 sibling, 2 replies; 8+ messages in thread
From: Marc Zyngier @ 2026-09-02 15:32 UTC (permalink / raw)
To: Feng Tang
Cc: Thomas Gleixner, John Stultz, Stephen Boyd, Miroslav Lichvar,
Daniel Lezcano, Peter Zijlstra, Petr Mladek, linux-kernel
On Wed, 02 Sep 2026 09:21:23 +0100,
Feng Tang <feng.tang@linux.alibaba.com> wrote:
>
> 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.
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.
--
Jazz isn't dead. It just smells funny.
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH] sched_clock: Add option to use absolute time against hardware clock reset
2026-09-02 15:32 ` Marc Zyngier
@ 2026-09-03 6:51 ` Yao Yuan
2026-09-03 7:38 ` Marc Zyngier
2026-09-03 7:45 ` Feng Tang
1 sibling, 1 reply; 8+ messages in thread
From: Yao Yuan @ 2026-09-03 6:51 UTC (permalink / raw)
To: Marc Zyngier
Cc: Feng Tang, Thomas Gleixner, John Stultz, Stephen Boyd,
Miroslav Lichvar, Daniel Lezcano, Peter Zijlstra, Petr Mladek,
linux-kernel
On Wed, Sep 02, 2026 at 04:32:25PM +0800, Marc Zyngier wrote:
> On Wed, 02 Sep 2026 09:21:23 +0100,
> Feng Tang <feng.tang@linux.alibaba.com> wrote:
> >
> > 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.
>
> 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,
> 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 ?
>
> 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.
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH] sched_clock: Add option to use absolute time against hardware clock reset
2026-09-03 6:51 ` Yao Yuan
@ 2026-09-03 7:38 ` Marc Zyngier
2026-09-03 8:07 ` Feng Tang
2026-09-03 10:20 ` Yao Yuan
0 siblings, 2 replies; 8+ messages in thread
From: Marc Zyngier @ 2026-09-03 7:38 UTC (permalink / raw)
To: Yao Yuan
Cc: Feng Tang, Thomas Gleixner, John Stultz, Stephen Boyd,
Miroslav Lichvar, Daniel Lezcano, Peter Zijlstra, Petr Mladek,
linux-kernel
On Thu, 03 Sep 2026 07:51:03 +0100,
Yao Yuan <yaoyuan@linux.alibaba.com> wrote:
>
> On Wed, Sep 02, 2026 at 04:32:25PM +0800, Marc Zyngier wrote:
> > On Wed, 02 Sep 2026 09:21:23 +0100,
> > Feng Tang <feng.tang@linux.alibaba.com> wrote:
> > >
> > > 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.
> >
> > 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,
>
> > 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.
--
Jazz isn't dead. It just smells funny.
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH] sched_clock: Add option to use absolute time against hardware clock reset
2026-09-03 7:38 ` Marc Zyngier
@ 2026-09-03 8:07 ` Feng Tang
2026-09-03 10:20 ` Yao Yuan
1 sibling, 0 replies; 8+ messages in thread
From: Feng Tang @ 2026-09-03 8:07 UTC (permalink / raw)
To: Marc Zyngier
Cc: Yao Yuan, Thomas Gleixner, John Stultz, Stephen Boyd,
Miroslav Lichvar, Daniel Lezcano, Peter Zijlstra, Petr Mladek,
linux-kernel
On Thu, Sep 03, 2026 at 08:38:57AM +0100, Marc Zyngier wrote:
[...]
> > Hi Marc,
> >
> > > 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.
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 :)
>
> 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
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH] sched_clock: Add option to use absolute time against hardware clock reset
2026-09-03 7:38 ` Marc Zyngier
2026-09-03 8:07 ` Feng Tang
@ 2026-09-03 10:20 ` Yao Yuan
1 sibling, 0 replies; 8+ messages in thread
From: Yao Yuan @ 2026-09-03 10:20 UTC (permalink / raw)
To: Marc Zyngier
Cc: Feng Tang, Thomas Gleixner, John Stultz, Stephen Boyd,
Miroslav Lichvar, Daniel Lezcano, Peter Zijlstra, Petr Mladek,
linux-kernel
On Thu, Sep 03, 2026 at 08:38:57AM +0800, Marc Zyngier wrote:
> On Thu, 03 Sep 2026 07:51:03 +0100,
> Yao Yuan <yaoyuan@linux.alibaba.com> wrote:
> >
> > On Wed, Sep 02, 2026 at 04:32:25PM +0800, Marc Zyngier wrote:
> > > On Wed, 02 Sep 2026 09:21:23 +0100,
> > > Feng Tang <feng.tang@linux.alibaba.com> wrote:
> > > >
> > > > 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.
> > >
> > > 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,
> >
> > > 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.
Thanks for the details !
I see, this yet like some hypervisor hide its handling time
from guest to me :-)
>
> 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!
>
> M.
>
> --
> Jazz isn't dead. It just smells funny.
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH] sched_clock: Add option to use absolute time against hardware clock reset
2026-09-02 15:32 ` Marc Zyngier
2026-09-03 6:51 ` Yao Yuan
@ 2026-09-03 7:45 ` Feng Tang
1 sibling, 0 replies; 8+ messages in thread
From: Feng Tang @ 2026-09-03 7:45 UTC (permalink / raw)
To: Marc Zyngier
Cc: Thomas Gleixner, John Stultz, Stephen Boyd, Miroslav Lichvar,
Daniel Lezcano, Peter Zijlstra, Petr Mladek, linux-kernel,
yaoyuan
On Wed, Sep 02, 2026 at 04:32:25PM +0100, Marc Zyngier wrote:
> On Wed, 02 Sep 2026 09:21:23 +0100,
> Feng Tang <feng.tang@linux.alibaba.com> wrote:
> >
> > 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.
Hi Marc,
Thanks for the great inputs!
I forgot to emphasized that this is only a debug option, mostly for bug
chasing.
> 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 :)
> 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.
> 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
>
> Thanks,
>
> M.
>
> --
> Jazz isn't dead. It just smells funny.
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH] sched_clock: Add option to use absolute time against hardware clock reset
2026-09-02 8:21 [PATCH] sched_clock: Add option to use absolute time against hardware clock reset Feng Tang
2026-09-02 15:32 ` Marc Zyngier
@ 2026-09-05 20:39 ` Thomas Gleixner
1 sibling, 0 replies; 8+ messages in thread
From: Thomas Gleixner @ 2026-09-05 20:39 UTC (permalink / raw)
To: Feng Tang, John Stultz, Stephen Boyd, Miroslav Lichvar,
Daniel Lezcano, Peter Zijlstra
Cc: Marc Zyngier, Petr Mladek, linux-kernel, Feng Tang
On Wed, Sep 02 2026 at 16:21, Feng Tang wrote:
> 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.
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2026-09-05 20:39 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-09-02 8:21 [PATCH] sched_clock: Add option to use absolute time against hardware clock reset Feng Tang
2026-09-02 15:32 ` Marc Zyngier
2026-09-03 6:51 ` Yao Yuan
2026-09-03 7:38 ` Marc Zyngier
2026-09-03 8:07 ` Feng Tang
2026-09-03 10:20 ` Yao Yuan
2026-09-03 7:45 ` Feng Tang
2026-09-05 20:39 ` Thomas Gleixner
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®