printk: add early_counter_ns routine for printk blind spot
Subsystems (4)
PATCH 1/1 printk: add early_counter_ns routine for printk blind spot
From: Tim Bird <[email protected]>
During early boot, printk timestamps are reported as zero, which creates a blind spot in early boot timings. This blind spot hinders timing and optimization efforts for code that executes before time_init(), which is when local_clock() is initialized sufficiently to start returning non-zero timestamps. This period is about 400 milliseconds for many current desktop and embedded machines running Linux.
Add an early_counter_ns function that returns nanosecond timestamps based on get_cycles(). get_cycles() is operational on arm64 and x86_64 from kernel start. Add some calibration printks to allow setting configuration variables that are used to convert cycle counts to nanoseconds (which are then used in early printks). Add CONFIG_EARLY_COUNTER_NS, as well as some associated conversion variables, as new kernel config variables.
After proper configuration, this yields non-zero timestamps for printks from the very start of kernel execution. The timestamps are relative to the start of the architecture-specific counter used in get_cycles (e.g. the TSC on x86_64 and cntvct_el0 on arm64). This means that the time reported reflects time-from-power-on for most embedded products. This is also a useful data point for boot-time optimization work.
Note that there is a discontinuity in the timestamp sequencing when standard clocks are finally initialized in time_init(). The printk timestamps are thus not monotonically increasing through the entire boot.
--- init/Kconfig | 47 ++++++++++++++++++++++++++++++++++++++++++ init/main.c | 25 ++++++++++++++++++++++ kernel/printk/printk.c | 15 ++++++++++++++ 3 files changed, 87 insertions(+)
init/Kconfig +47 -0
@@ -770,6 +770,53 @@ config IKHEADERS or similar programs. If you build the headers as a module, a module called kheaders.ko is built which can be loaded on-demand to get access to headers. +config EARLY_COUNTER_NS + bool "Use counter for early printk timestamps" + default y + depends on PRINTK + help + Use a cycle-counter to provide printk timestamps during early + boot. This allows seeing timing information that would + otherwise be displayed with 0-valued timestamps. + + In order for this to work, you need to specify values for + EARLY_COUNTER_MULT and EARLY_COUNTER_SHIFT, used to convert + from the cycle count to nanoseconds. + +config EARLY_COUNTER_MULT + int "Multiplier for early cycle counter" + depends on PRINTK && EARLY_COUNTER_NS + default 1 + help + This value specifies a multiplier to be used when converting + cycle counts to nanoseconds. The formula used is: + ns = (cycles * mult) >> shift + + Use a multiplier that will bring the value of (cycles * mult) + to near a power of two, that is greater than 1000. The + nanoseconds returned by this conversion are divided by 1000 + to be used as the printk timestamp counter (with resolution + of microseconds). + + As an example, for a cycle-counter with a frequency of 200 Mhz, + the multiplier would be: 10485760, and the shift would be 21. + +config EARLY_COUNTER_SHIFT + int "Shift value for early cycle counter" + range 0 63 + depends on PRINTK && EARLY_COUNTER_NS + default 0 + help + This value specifies a shift value to be used when converting + cycle counts to nanoseconds. The formula used is: + ns = (cycles * mult) >> shift + + Use a shift that will bring the result to a value + in nanoseconds. + + As an example, for a cycle-counter with a frequency of 200 Mhz, + the multiplier would be: 10485760, and the shift would be 21. + config LOG_BUF_SHIFT int "Kernel log buffer size (16 => 64KB, 17 => 128KB)" range 12 25
init/main.c +25 -0
@@ -105,6 +105,8 @@#include <linux/ptdump.h> #include <linux/time_namespace.h> #include <net/net_namespace.h> +#include <linux/timex.h> +#include <linux/sched/clock.h> #include <asm/io.h> #include <asm/setup.h>
@@ -906,6 +908,8 @@ static void __init early_numa_node_init(void)#endif } +static u64 start_cycles, start_ns; + asmlinkage __visible __init __no_sanitize_address __noreturn __no_stack_protectorvoid start_kernel (void ) {
@@ -1023,6 +1027,10 @@ void start_kernel(void)timekeeping_init ();time_init (); +/* used to calibrate early_counter_ns */ + start_cycles =get_cycles (); + start_ns =local_clock (); +/* This must be after timekeeping is initialized */ random_init ();
@@ -1474,6 +1482,8 @@ void __weak free_initmem(void)static int __ref kernel_init (void *unused) {int ret; +u64 end_cycles, end_ns; +u32 early_mult, early_shift;/* * Wait until kthreadd is all set-up.
@@ -1505,6 +1515,21 @@ static int __ref kernel_init(void *unused)do_sysctl_args (); +/* show calibration data for early_counter_ns */ + end_cycles =get_cycles (); + end_ns =local_clock (); +clocks_calc_mult_shift (&early_mult, &early_shift, + ((end_cycles - start_cycles) *NSEC_PER_SEC )/(end_ns - start_ns), +NSEC_PER_SEC ,50 ); + +#ifdef CONFIG_EARLY_COUNTER_NS +pr_info ("Early Counter: start_cycles=%llu, end_cycles=%llu, cycles=%llu\n" , + start_cycles, end_cycles, (end_cycles - start_cycles)); +pr_info ("Early Counter: start_ns=%llu, end_ns=%llu, ns=%llu\n" , + start_ns, end_ns, (end_ns - start_ns)); +pr_info ("Early Counter: MULT=%u, SHIFT=%u\n" , early_mult, early_shift); +#endif +if (ramdisk_execute_command) { ret =run_init_process (ramdisk_execute_command);if (!ret)
kernel/printk/printk.c +15 -0
@@ -2210,6 +2210,19 @@ static u16 printk_sprint(char *text, u16 size, int facility,return text_len; } +#ifdef CONFIG_EARLY_COUNTER_NS +static inline u64 early_counter_ns (void ) +{ +return ((u64 )get_cycles () *CONFIG_EARLY_COUNTER_MULT ) + >>CONFIG_EARLY_COUNTER_SHIFT ; +} +#else +static inline u64 early_counter_ns (void ) +{ +return 0 ; +} +#endif + __printf(4, 0) int vprintk_store(int facility, int level, const struct dev_printk_info *dev_info,
@@ -2239,6 +2252,8 @@ int vprintk_store(int facility, int level,* timestamp with respect to the caller. */ ts_nsec =local_clock (); +if (!ts_nsec) + ts_nsec =early_counter_ns (); caller_id =printk_caller_id ();
Signature · 1 line
2.43.0
kernel test robot
build botkernel test robot noticed the following build errors:
Hi Tim,
kernel test robot noticed the following build errors:
[auto build test ERROR on linus/master]
[also build test ERROR on v6.18-rc7]
[cannot apply to akpm-mm/mm-everything next-20251124]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Tim-Bird/printk-add-early_counter_ns-routine-for-printk-blind-spot/20251125-133242 base: linus/master patch link: https://lore.kernel.org/r/39b09edb-8998-4ebd-a564-7d594434a981%40bird.org patch subject: [PATCH] printk: add early_counter_ns routine for printk blind spot config: powerpc-allnoconfig (https://download.01.org/0day-ci/archive/20251125/[email protected]/config) compiler: powerpc-linux-gcc (GCC) 15.1.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251125/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
powerpc-linux-ld: init/main.o: in function `kernel_init':
>> main.c:(.ref.text+0x144): undefined reference to `__udivdi3'
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
Hi Tim,
I tested this on my i.MX93 FRDM (arm64) board and after a bit of fiddling with the MULT/SHIFT values I got it working. It can be a very valuable addition.
Some comments follow.
Quoted 27 lines · Tim Bird
From: Tim Bird <[email protected]>
During early boot, printk timestamps are reported as zero, which creates a blind spot in early boot timings. This blind spot hinders timing and optimization efforts for code that executes before
time_init(), which is whenlocal_clock()is initialized sufficiently to start returning non-zero timestamps. This period is about 400 milliseconds for many current desktop and embedded machines running Linux.Add an
early_counter_nsfunction that returns nanosecond timestamps based onget_cycles().get_cycles()is operational on arm64 andx86_64from kernel start. Add some calibration printks to allow setting configuration variables that are used to convert cycle counts to nanoseconds (which are then used in early printks). AddCONFIG_EARLY_COUNTER_NS, as well as some associated conversion variables, as new kernel config variables.After proper configuration, this yields non-zero timestamps for printks from the very start of kernel execution. The timestamps are relative to the start of the architecture-specific counter used in
get_cycles(e.g. the TSC onx86_64andcntvct_el0on arm64). This means that the time reported reflects time-from-power-on for most embedded products. This is also a useful data point for boot-time optimization work.
Quoted 4 lines
Note that there is a discontinuity in the timestamp sequencing when standard clocks are finally initialized in
time_init(). The printk timestamps are thus not monotonically increasing through the entire boot.
This is... not going to work, IMO, and might lead to breakages in userspace tools (are printk timings a userspace API?).
I actually have a counter-proposal: the time obtained through cycle evaluation is used as an offset to be added to the printk time after time_init() is called. A (working, but maybe sub-optimal) patch to obtain this is attached at the end.
Quoted 37 lines
Signed-off-by Tim Bird <[email protected]>--- init/Kconfig | 47 ++++++++++++++++++++++++++++++++++++++++++ init/main.c | 25 ++++++++++++++++++++++ kernel/printk/printk.c | 15 ++++++++++++++ 3 files changed, 87 insertions(+)init/Kconfig +21
-0@@ -770,6 +770,53 @@ config IKHEADERS or similar programs. If you build the headers as a module, a module called kheaders.ko is built which can be loaded on-demand to get access to headers. +config EARLY_COUNTER_NS + bool "Use counter for early printk timestamps" + default y + depends on PRINTK + help + Use a cycle-counter to provide printk timestamps during early + boot. This allows seeing timing information that would + otherwise be displayed with 0-valued timestamps. + + In order for this to work, you need to specify values for + EARLY_COUNTER_MULT and EARLY_COUNTER_SHIFT, used to convert + from the cycle count to nanoseconds. + +config EARLY_COUNTER_MULT + int "Multiplier for early cycle counter" + depends on PRINTK && EARLY_COUNTER_NS + default 1 + help + This value specifies a multiplier to be used when converting + cycle counts to nanoseconds. The formula used is: + ns = (cycles * mult) >> shift
init/Kconfig config IKHEADERS · line 803
Quoted 10 lines
init/Kconfig +10
-0+ + Use a multiplier that will bring the value of (cycles * mult) + to near a power of two, that is greater than 1000. The + nanoseconds returned by this conversion are divided by 1000 + to be used as the printk timestamp counter (with resolution + of microseconds). + + As an example, for a cycle-counter with a frequency of 200 Mhz, + the multiplier would be: 10485760, and the shift would be 21. +
If I got this correclty:
EARLY_COUNTER_MULT = (10^9 / freq) << EARLY_COUNTER_SHIFT
where EARLY_COUNTER_SHIFT can be chosen at will, provided it is big enough to survice the ns->us conversion but small enough not to overflow the u64 container.
Quoted 70 lines
init/Kconfig +16
-0+config EARLY_COUNTER_SHIFT + int "Shift value for early cycle counter" + range 0 63 + depends on PRINTK && EARLY_COUNTER_NS + default 0 + help + This value specifies a shift value to be used when converting + cycle counts to nanoseconds. The formula used is: + ns = (cycles * mult) >> shift + + Use a shift that will bring the result to a value + in nanoseconds. + + As an example, for a cycle-counter with a frequency of 200 Mhz, + the multiplier would be: 10485760, and the shift would be 21. + config LOG_BUF_SHIFT int "Kernel log buffer size (16 => 64KB, 17 => 128KB)" range 12 25init/main.c +15
-0@@ -105,6 +105,8 @@#include <linux/ptdump.h> #include <linux/time_namespace.h> #include <net/net_namespace.h> +#include <linux/timex.h> +#include <linux/sched/clock.h> #include <asm/io.h> #include <asm/setup.h> @@ -906,6 +908,8 @@ static void __init early_numa_node_init(void)#endif } +static u64 start_cycles, start_ns; + asmlinkage __visible __init __no_sanitize_address __noreturn __no_stack_protectorvoid start_kernel (void ) {@@ -1023,6 +1027,10 @@ void start_kernel(void)timekeeping_init ();time_init (); +/* used to calibrate early_counter_ns */ + start_cycles =get_cycles (); + start_ns =local_clock (); +/* This must be after timekeeping is initialized */ random_init ();@@ -1474,6 +1482,8 @@ void __weak free_initmem(void)static int __ref kernel_init (void *unused) {int ret; +u64 end_cycles, end_ns; +u32 early_mult, early_shift;/* * Wait until kthreadd is all set-up. @@ -1505,6 +1515,21 @@ static int __ref kernel_init(void *unused)do_sysctl_args (); +/* show calibration data for early_counter_ns */ + end_cycles =get_cycles (); + end_ns =local_clock (); + clocks_calc_mult_shift(&early_mult, &early_shift, + ((end_cycles - start_cycles) * NSEC_PER_SEC)/(end_ns - start_ns),
init/main.c static int __ref kernel_init(void *unused) · line 1532
Quoted 10 lines
init/main.c +10
-0+ NSEC_PER_SEC, 50); + +#ifdef CONFIG_EARLY_COUNTER_NS +pr_info ("Early Counter: start_cycles=%llu, end_cycles=%llu, cycles=%llu\n" , + start_cycles, end_cycles, (end_cycles - start_cycles)); +pr_info ("Early Counter: start_ns=%llu, end_ns=%llu, ns=%llu\n" , + start_ns, end_ns, (end_ns - start_ns)); +pr_info ("Early Counter: MULT=%u, SHIFT=%u\n" , early_mult, early_shift); +#endif +
I don't get the need to have these here - should they be an help for the integrator to calibrate and choose EARLY_COUNTER_MULT and EARLY_COUNTER_SHIFT? The ns values printed here have some meaning only if these two parameters are already set correctly in the first place - what's the foreseen calibration procedure?
Moreover, if they are only required for calibration, maybe pr_debugi() would be a better choice?
Quoted 40 lines
init/main.c +0
-0if (ramdisk_execute_command) { ret =run_init_process (ramdisk_execute_command);if (!ret)kernel/printk/printk.c +15
-0@@ -2210,6 +2210,19 @@ static u16 printk_sprint(char *text, u16 size, int facility,return text_len; } +#ifdef CONFIG_EARLY_COUNTER_NS +static inline u64 early_counter_ns (void ) +{ +return ((u64 )get_cycles () *CONFIG_EARLY_COUNTER_MULT ) + >>CONFIG_EARLY_COUNTER_SHIFT ; +} +#else +static inline u64 early_counter_ns (void ) +{ +return 0 ; +} +#endif + __printf(4, 0) int vprintk_store(int facility, int level, const struct dev_printk_info *dev_info,@@ -2239,6 +2252,8 @@ int vprintk_store(int facility, int level,* timestamp with respect to the caller. */ ts_nsec =local_clock (); +if (!ts_nsec) + ts_nsec =early_counter_ns (); caller_id =printk_caller_id ();Signature · 1 line
2.43.0
Best regards,
Francesco
---
kernel/printk/printk.c +17 -0
@@ -2216,11 +2216,26 @@ static inline u64 early_counter_ns(void)return ((u64 )get_cycles () *CONFIG_EARLY_COUNTER_MULT ) >> CONFIG_EARLY_COUNTER_SHIFT; } + +static u64 early_counter_ns_offset (void ) +{ +static u64 early_counter_ns_start =0 ; + +if (!early_counter_ns_start) + early_counter_ns_start =early_counter_ns (); + +return early_counter_ns_start; +}#else static inline u64 early_counter_ns (void ) {return 0 ; } + +static inline u64 early_counter_ns_offset (void ) +{ +return 0 ; +}#endif __printf (4 ,0 )
@@ -2254,6 +2269,8 @@ int vprintk_store(int facility, int level, ts_nsec =local_clock ();if (!ts_nsec) ts_nsec =early_counter_ns (); +else + ts_nsec +=early_counter_ns_offset (); caller_id =printk_caller_id ();
Hi all,
Quoted 28 lines · Francesco Valla
Quoted 27 lines · Tim Bird
From: Tim Bird <[email protected]>
During early boot, printk timestamps are reported as zero, which creates a blind spot in early boot timings. This blind spot hinders timing and optimization efforts for code that executes before
time_init(), which is whenlocal_clock()is initialized sufficiently to start returning non-zero timestamps. This period is about 400 milliseconds for many current desktop and embedded machines running Linux.Add an
early_counter_nsfunction that returns nanosecond timestamps based onget_cycles().get_cycles()is operational on arm64 andx86_64from kernel start. Add some calibration printks to allow setting configuration variables that are used to convert cycle counts to nanoseconds (which are then used in early printks). AddCONFIG_EARLY_COUNTER_NS, as well as some associated conversion variables, as new kernel config variables.After proper configuration, this yields non-zero timestamps for printks from the very start of kernel execution. The timestamps are relative to the start of the architecture-specific counter used in
get_cycles(e.g. the TSC onx86_64andcntvct_el0on arm64). This means that the time reported reflects time-from-power-on for most embedded products. This is also a useful data point for boot-time optimization work.
Quoted 7 lines
Quoted 4 lines
Note that there is a discontinuity in the timestamp sequencing when standard clocks are finally initialized in
time_init(). The printk timestamps are thus not monotonically increasing through the entire boot.This is... not going to work, IMO, and might lead to breakages in userspace tools (are printk timings a userspace API?).
I think they are.
Another approach would be to defer the calibration/conversion to userspace, and make sure the early part stands out.
I.e. when real timekeeping is available, kernel messages are prefixed by "[%5lu.%06lu]". Early messages could be prefixed by a plain integer "[%12u]", containing the raw cycle counter value.
The presence of the decimal point would make the difference obvious.
Quoted 4 lines
I actually have a counter-proposal: the time obtained through cycle evaluation is used as an offset to be added to the printk time after
time_init()is called. A (working, but maybe sub-optimal) patch to obtain this is attached at the end.
Oh, that's a nice idea, too!
Gr{oetje,eeting}s,
Geert
Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected] In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that. -- Linus Torvalds
Quoted 43 lines
-----Original Message-----
From: Geert Uytterhoeven <[email protected]>
Hi all,Quoted 28 lines · Francesco Valla
Quoted 27 lines · Tim Bird
From: Tim Bird <[email protected]>
During early boot, printk timestamps are reported as zero, which creates a blind spot in early boot timings. This blind spot hinders timing and optimization efforts for code that executes before
time_init(), which is whenlocal_clock()is initialized sufficiently to start returning non-zero timestamps. This period is about 400 milliseconds for many current desktop and embedded machines running Linux.Add an
early_counter_nsfunction that returns nanosecond timestamps based onget_cycles().get_cycles()is operational on arm64 andx86_64from kernel start. Add some calibration printks to allow setting configuration variables that are used to convert cycle counts to nanoseconds (which are then used in early printks). AddCONFIG_EARLY_COUNTER_NS, as well as some associated conversion variables, as new kernel config variables.After proper configuration, this yields non-zero timestamps for printks from the very start of kernel execution. The timestamps are relative to the start of the architecture-specific counter used in
get_cycles(e.g. the TSC onx86_64andcntvct_el0on arm64). This means that the time reported reflects time-from-power-on for most embedded products. This is also a useful data point for boot-time optimization work.Quoted 7 lines
Quoted 4 lines
Note that there is a discontinuity in the timestamp sequencing when standard clocks are finally initialized in
time_init(). The printk timestamps are thus not monotonically increasing through the entire boot.This is... not going to work, IMO, and might lead to breakages in userspace tools (are printk timings a userspace API?).
Quoted 8 lines
I think they are.
Another approach would be to defer the calibration/conversion to userspace, and make sure the early part stands out.
I.e. when real timekeeping is available, kernel messages are prefixed by "[%5lu.%06lu]". Early messages could be prefixed by a plain integer "[%12u]", containing the raw cycle counter value.
The presence of the decimal point would make the difference obvious.
I thought about this while I was creating this.
It wouldn't require the extra configuration for MULT and SHIFT (which would be nice), and it would be, as you say, very obvious that this was not a regular timestamp. This means it could be enabled on a generic kernel (making more likely it could be enabled by default). And really only boot-time optimizers would care enough to decode the data, so the onus would be on them to run the tool. Everyone else could ignore them.
I'm not sure if it would break existing printk-processing tools. I suspect it would.
Also, I find that post-processing tools often get overlooked.
I asked at ELC this year how many people are using show_delta, which has been upstream for years, and can do a few neat things with printk timestamps, and not a single person had even heard of it.
In this scenario, you would still need to have the calibration printks in the code so that the tool could pull them out to then convert the cycle-valued printks into printks with regular timestamps.
I could see doing this if people object to the non-genericity of the current solution.
Quoted 7 lines
Quoted 5 lines
I actually have a counter-proposal: the time obtained through cycle evaluation is used as an offset to be added to the printk time after
time_init()is called. A (working, but maybe sub-optimal) patch to obtain this is attached at the end.Oh, that's a nice idea, too!
Thanks for the feedback!
-- Tim
Petr Mladek
maintainerQuoted 60 lines · Tim
Quoted 43 lines
-----Original Message-----
From: Geert Uytterhoeven <[email protected]>
Hi all,Quoted 28 lines · Francesco Valla
Quoted 27 lines · Tim Bird
From: Tim Bird <[email protected]>
During early boot, printk timestamps are reported as zero, which creates a blind spot in early boot timings. This blind spot hinders timing and optimization efforts for code that executes before
time_init(), which is whenlocal_clock()is initialized sufficiently to start returning non-zero timestamps. This period is about 400 milliseconds for many current desktop and embedded machines running Linux.Add an
early_counter_nsfunction that returns nanosecond timestamps based onget_cycles().get_cycles()is operational on arm64 andx86_64from kernel start. Add some calibration printks to allow setting configuration variables that are used to convert cycle counts to nanoseconds (which are then used in early printks). AddCONFIG_EARLY_COUNTER_NS, as well as some associated conversion variables, as new kernel config variables.After proper configuration, this yields non-zero timestamps for printks from the very start of kernel execution. The timestamps are relative to the start of the architecture-specific counter used in
get_cycles(e.g. the TSC onx86_64andcntvct_el0on arm64). This means that the time reported reflects time-from-power-on for most embedded products. This is also a useful data point for boot-time optimization work.Quoted 7 lines
Quoted 4 lines
Note that there is a discontinuity in the timestamp sequencing when standard clocks are finally initialized in
time_init(). The printk timestamps are thus not monotonically increasing through the entire boot.This is... not going to work, IMO, and might lead to breakages in userspace tools (are printk timings a userspace API?).
Quoted 8 lines
I think they are.
Another approach would be to defer the calibration/conversion to userspace, and make sure the early part stands out.
I.e. when real timekeeping is available, kernel messages are prefixed by "[%5lu.%06lu]". Early messages could be prefixed by a plain integer "[%12u]", containing the raw cycle counter value.
The presence of the decimal point would make the difference obvious.I thought about this while I was creating this.
It wouldn't require the extra configuration for MULT and SHIFT (which would be nice), and it would be, as you say, very obvious that this was not a regular timestamp. This means it could be enabled on a generic kernel (making more likely it could be enabled by default). And really only boot-time optimizers would care enough to decode the data, so the onus would be on them to run the tool. Everyone else could ignore them.
Quoted 1 line
I'm not sure if it would break existing printk-processing tools. I suspect it would.
I guess that it might break even basic tools, like dmesg, journalctl, or crash.
A solution might be to pass it as an extra information to the official timestamp, for example:
+ on console:
<level>[timestamp][callerid][cl cycles] message
<6>[ 0.000000][ T0][cl 345678] BIOS-provided physical RAM map:
<6>[ 0.000000][ T0][cl 1036890] BIOS-e820: [mem 0x0000000000000000-0x000000000009fbff] usable
<6>[ 0.000000][ T0][cl 1129452] BIOS-e820: [mem 0x000000000009fc00-0x000000000009ffff] reserved + via /dev/kmsg
<level>,<sequnum>,<timestamp>,<contflag>[,additional_values, ... ];<message text>
6,2,0,-,caller=T0,cycle=345678;BIOS-provided physical RAM map:
6,3,0,-,caller=T0,cycle=1036890;BIOS-e820: [mem 0x0000000000000000-0x000000000009fbff] usable
6,4,0,-,caller=T0,cycle=1129452;BIOS-e820: [mem 0x000000000009fc00-0x000000000009ffff] reservedThe extra field would disappear after time_init().
The value might be stored in struct printk_info in the same field .ts_nsec. It might be distinguished from a real timestamp using a flag in enum printk_info_flags. The official timestamp would be zero when this flag is set.
It will not require the two CONFIG_ values for calibrating the computation.
The output on the console is a bit messy. But I guess that this feature is rather for tuning and it won't be enabled on production systems. So it might be acceptable.
time_init() might even print a message with the cycle value and the official timestamp on the same line. It can be used for post-processing and translating cycles back to ns.
Quoted 4 lines
Also, I find that post-processing tools often get overlooked.
I asked at ELC this year how many people are usingshow_delta, which has been upstream for years, and can do a few neat things with printk timestamps, and not a single person had even heard of it.
Yeah. We need to make sure that the post processing tool won't get mad, for example, crash or show garbage.
Best Regards,
Petr
Petr Mladek
maintainerQuoted 36 lines · Francesco Valla
Hi Tim,
I tested this on my i.MX93 FRDM (arm64) board and after a bit of fiddling with the MULT/SHIFT values I got it working. It can be a very valuable addition.
Some comments follow.
Quoted 27 lines · Tim Bird
From: Tim Bird <[email protected]>
During early boot, printk timestamps are reported as zero, which creates a blind spot in early boot timings. This blind spot hinders timing and optimization efforts for code that executes before
time_init(), which is whenlocal_clock()is initialized sufficiently to start returning non-zero timestamps. This period is about 400 milliseconds for many current desktop and embedded machines running Linux.Add an
early_counter_nsfunction that returns nanosecond timestamps based onget_cycles().get_cycles()is operational on arm64 andx86_64from kernel start. Add some calibration printks to allow setting configuration variables that are used to convert cycle counts to nanoseconds (which are then used in early printks). AddCONFIG_EARLY_COUNTER_NS, as well as some associated conversion variables, as new kernel config variables.After proper configuration, this yields non-zero timestamps for printks from the very start of kernel execution. The timestamps are relative to the start of the architecture-specific counter used in
get_cycles(e.g. the TSC onx86_64andcntvct_el0on arm64). This means that the time reported reflects time-from-power-on for most embedded products. This is also a useful data point for boot-time optimization work.
Quoted 7 lines
Quoted 4 lines
Note that there is a discontinuity in the timestamp sequencing when standard clocks are finally initialized in
time_init(). The printk timestamps are thus not monotonically increasing through the entire boot.This is... not going to work, IMO, and might lead to breakages in userspace tools (are printk timings a userspace API?).
Honestly, I am not sure if it would break anything. The fact is that printk() always used monotonic timers. And it is possible that some userspace depends on it.
I personally thing that non-monotonic time stamps might be confusing but they should not cause any serious breakage. But I might be wrong. People are creative...
Quoted 4 lines
I actually have a counter-proposal: the time obtained through cycle evaluation is used as an offset to be added to the printk time after
time_init()is called. A (working, but maybe sub-optimal) patch to obtain this is attached at the end.
I am not sure if this is a good idea. The offset would cause that all post-timer-init printk timestamps differ from values provided by the timer API. And it might cause confusion, for example, when they are printed as part of the message, or when analyzing a crash dump.
On the other hand, there are various clock sources in the kernel which are not comparable anyway. So maybe I am too cautious.
Quoted 57 lines
Quoted 27 lines
init/Kconfig +21
-0@@ -770,6 +770,53 @@ config IKHEADERS or similar programs. If you build the headers as a module, a module called kheaders.ko is built which can be loaded on-demand to get access to headers. +config EARLY_COUNTER_NS + bool "Use counter for early printk timestamps" + default y + depends on PRINTK + help + Use a cycle-counter to provide printk timestamps during early + boot. This allows seeing timing information that would + otherwise be displayed with 0-valued timestamps. + + In order for this to work, you need to specify values for + EARLY_COUNTER_MULT and EARLY_COUNTER_SHIFT, used to convert + from the cycle count to nanoseconds. + +config EARLY_COUNTER_MULT + int "Multiplier for early cycle counter" + depends on PRINTK && EARLY_COUNTER_NS + default 1 + help + This value specifies a multiplier to be used when converting + cycle counts to nanoseconds. The formula used is: + ns = (cycles * mult) >> shiftinit/Kconfig config IKHEADERS · line 803
Quoted 10 lines
init/Kconfig +10
-0+ + Use a multiplier that will bring the value of (cycles * mult) + to near a power of two, that is greater than 1000. The + nanoseconds returned by this conversion are divided by 1000 + to be used as the printk timestamp counter (with resolution + of microseconds). + + As an example, for a cycle-counter with a frequency of 200 Mhz, + the multiplier would be: 10485760, and the shift would be 21. +If I got this correclty:
EARLY_COUNTER_MULT= (10^9 / freq) <<EARLY_COUNTER_SHIFTwhere
EARLY_COUNTER_SHIFTcan be chosen at will, provided it is big enough to survice thens->usconversion but small enough not to overflow the u64 container.Quoted 11 lines
init/Kconfig +11
-0+config EARLY_COUNTER_SHIFT + int "Shift value for early cycle counter" + range 0 63 + depends on PRINTK && EARLY_COUNTER_NS + default 0 + help + This value specifies a shift value to be used when converting + cycle counts to nanoseconds. The formula used is: + ns = (cycles * mult) >> shift + + Use a shift that will bring the result to a value
Quoted 8 lines
init/Kconfig config IKHEADERS · line 822
Quoted 8 lines
init/Kconfig +5
-0+ in nanoseconds. + + As an example, for a cycle-counter with a frequency of 200 Mhz, + the multiplier would be: 10485760, and the shift would be 21. + config LOG_BUF_SHIFT int "Kernel log buffer size (16 => 64KB, 17 => 128KB)" range 12 25
So, it is usable only for a particular HW. It is not usable for a generic kernel which is supposed to run on misc HW.
I guess that there is no way to detect the CPU frequency at runtime?
Best Regards,
Petr
Quoted 47 lines
-----Original Message-----
From: Petr Mladek <[email protected]>Quoted 36 lines · Francesco Valla
Hi Tim,
I tested this on my i.MX93 FRDM (arm64) board and after a bit of fiddling with the MULT/SHIFT values I got it working. It can be a very valuable addition.
Some comments follow.
Quoted 27 lines · Tim Bird
From: Tim Bird <[email protected]>
During early boot, printk timestamps are reported as zero, which creates a blind spot in early boot timings. This blind spot hinders timing and optimization efforts for code that executes before
time_init(), which is whenlocal_clock()is initialized sufficiently to start returning non-zero timestamps. This period is about 400 milliseconds for many current desktop and embedded machines running Linux.Add an
early_counter_nsfunction that returns nanosecond timestamps based onget_cycles().get_cycles()is operational on arm64 andx86_64from kernel start. Add some calibration printks to allow setting configuration variables that are used to convert cycle counts to nanoseconds (which are then used in early printks). AddCONFIG_EARLY_COUNTER_NS, as well as some associated conversion variables, as new kernel config variables.After proper configuration, this yields non-zero timestamps for printks from the very start of kernel execution. The timestamps are relative to the start of the architecture-specific counter used in
get_cycles(e.g. the TSC onx86_64andcntvct_el0on arm64). This means that the time reported reflects time-from-power-on for most embedded products. This is also a useful data point for boot-time optimization work.Quoted 7 lines
Quoted 4 lines
Note that there is a discontinuity in the timestamp sequencing when standard clocks are finally initialized in
time_init(). The printk timestamps are thus not monotonically increasing through the entire boot.This is... not going to work, IMO, and might lead to breakages in userspace tools (are printk timings a userspace API?).
Quoted 7 lines
Honestly, I am not sure if it would break anything. The fact is that
printk()always used monotonic timers. And it is possible that some userspace depends on it.I personally thing that non-monotonic time stamps might be confusing but they should not cause any serious breakage. But I might be wrong. People are creative...
I worried about this, but I'm skeptical it's a big deal. Humans might be a little confused, but it's not difficult to see what's going on just by looking
at the timestamps. If a tool breaks, especially something that's used
in automation, e.g. it's used to report results, or is in some sort of CI loop where the break will cascade into a test failure, then that's a bigger issue. But right now I'm not aware of any boot-time tests where that would be the case.
I'll comment more on different fixes for this below.
Quoted 6 lines
Quoted 5 lines
I actually have a counter-proposal: the time obtained through cycle evaluation is used as an offset to be added to the printk time after
time_init()is called. A (working, but maybe sub-optimal) patch to obtain this is attached at the end.
Quoted 8 lines
I am not sure if this is a good idea. The offset would cause that all post-timer-init printk timestamps differ from values provided by the timer API. And it might cause confusion, for example, when they are printed as part of the message, or when analyzing a crash dump.
On the other hand, there are various clock sources in the kernel which are not comparable anyway. So maybe I am too cautious.
I thought of adding an offset, but I didn't want to disturb anything past the time_init() call. As it is now, the early_counter_ns feature only changes the zero-valued timestamps. So anything relying on the absolute value of an existing timestamp later in the boot would not be affected. I thought that if people suddenly saw the timestamps jump by 10 to 30 seconds (since they are now relative to machine start instead of to kernel clock start (time_init()), it would be very jarring. I suppose they would get used to it, though, and all relative timings should stay the same.
I also didn't want to add additional overhead (even a single add) in the case where CONFIG_EARLY_COUNTER_NS was disabled. But, realistically, I don't think an additional add in the printk path is not going to be noticeable, and I can probably structure it so that there's absolutely no overhead when CONFIG_EARLY_COUNTER_NS is disabled.
I considered embedding an early counter offset into local_clock(), and thus not modifying the printk code at all. This would have the benefit
of keeping the printk timestamps consistent with other uses of local_clock() data (such as crash dumps or inside other messages). But then that would embed the early_counter overhead into every user of local_clock() (whether humans saw the timestamp values or not). And some of those users might be more performance sensitive than printk is.
Finally, I considered adding another config option to control adding the offset, but at 3 configs for this fairly niche functionality, I thought I was already pushing my luck. But a new config would be easy to add.
My plan is to add an offset for the early_counter_ns value to printk, without a config, in the next patch version, and see what people think.
Quoted 41 lines
Quoted 41 lines
Quoted 28 lines
init/Kconfig +21
-0@@ -770,6 +770,53 @@ config IKHEADERS or similar programs. If you build the headers as a module, a module called kheaders.ko is built which can be loaded on-demand to get access to headers. +config EARLY_COUNTER_NS + bool "Use counter for early printk timestamps" + default y + depends on PRINTK + help + Use a cycle-counter to provide printk timestamps during early + boot. This allows seeing timing information that would + otherwise be displayed with 0-valued timestamps. + + In order for this to work, you need to specify values for + EARLY_COUNTER_MULT and EARLY_COUNTER_SHIFT, used to convert + from the cycle count to nanoseconds. + +config EARLY_COUNTER_MULT + int "Multiplier for early cycle counter" + depends on PRINTK && EARLY_COUNTER_NS + default 1 + help + This value specifies a multiplier to be used when converting + cycle counts to nanoseconds. The formula used is: + ns = (cycles * mult) >> shiftinit/Kconfig config IKHEADERS · line 803
Quoted 10 lines
init/Kconfig +10
-0+ + Use a multiplier that will bring the value of (cycles * mult) + to near a power of two, that is greater than 1000. The + nanoseconds returned by this conversion are divided by 1000 + to be used as the printk timestamp counter (with resolution + of microseconds). + + As an example, for a cycle-counter with a frequency of 200 Mhz, + the multiplier would be: 10485760, and the shift would be 21. +If I got this correclty:
Quoted 5 lines
Quoted 5 lines
EARLY_COUNTER_MULT= (10^9 / freq) <<EARLY_COUNTER_SHIFTwhere
EARLY_COUNTER_SHIFTcan be chosen at will, provided it is big enough to survice thens->usconversion but small enough not to overflow the u64 container.
Yeah, I was worried about these Kconfig explanations. I think it will be easier to just explain how I recommend this should be configured, which is something like:
Turn on CONFIG_EARLY_COUNTER_NS, run the kernel once, get the values for MULT and SHIFT (printed by the calibration code), enter them in the appropriate configs, and then build and run the kernel again. This only needs to be done once per platform, and could even be put into the defconfig (or a config fragment) for a platform. (More on having hardcoded config for this below). It was not my intent to have kernel developers doing weird (shift) math to enable this feature. Note that I can't get the values from devicetree or the kernel command line, as this is used before either of those is initialized or parsed.
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
init/Kconfig +14
-0+config EARLY_COUNTER_SHIFT + int "Shift value for early cycle counter" + range 0 63 + depends on PRINTK && EARLY_COUNTER_NS + default 0 + help + This value specifies a shift value to be used when converting + cycle counts to nanoseconds. The formula used is: + ns = (cycles * mult) >> shift + + Use a shift that will bring the result to a value + in nanoseconds. + + As an example, for a cycle-counter with a frequency of 200 Mhz,
Quoted 8 lines
Quoted 5 lines
init/Kconfig config IKHEADERS · line 822
Quoted 5 lines
init/Kconfig +2
-0+ the multiplier would be: 10485760, and the shift would be 21. + config LOG_BUF_SHIFT int "Kernel log buffer size (16 => 64KB, 17 => 128KB)" range 12 25So, it is usable only for a particular HW. It is not usable for a generic kernel which is supposed to run on misc HW.
That's correct. It is mostly targeted at embedded products, where shaving off 10 to 200 milliseconds in the pre-clock-initialization region of boot would be valuable. For people doing aggressive boot time optimization, they will have a custom kernel anyway (and probably a custom bootloader, device tree, initramfs, udev rules, SE linux rules, module load ordering, systemd config, etc.)
Basically, if you're optimizing the code in this kernel boot time blind spot, you are very likely not using a generic kernel. (That's not to say that the optimizations made won't ultimately be valuable to people using generic kernels).
Quoted 2 lines
I guess that there is no way to detect the CPU frequency at runtime?
This "feature" is used before clock initialization, which is what would be used to calibrate the CPU frequency at runtime. This runs so early that doing the calibration inline doesn't work. Not enough kernel services are available (Actually, zero services are available, as this can be used in the very first printk in start_kernel.)
Plan from here...
I got a compile error from 0-day on powerpc, so I need to re-spin to fix that. I'll address the other issues raised and submit a new version when I can. I'm off to Japan this week, and between business travel and the holidays, and being away from my lab where I can do hardware testing, it will probably be some time in January before I send the next version.
Thanks very much for the feedback!
-- Tim
Petr Mladek
maintainerAdding some people from the time subsystem into Cc. Please, keep them in the loop in the eventual next version of the patch.
For the new people, please note the discussion has already started, see
https://lore.kernel.org/r/[email protected]
Best Regards,
Petr
Quoted 196 lines · Tim Bird
From: Tim Bird <[email protected]>
During early boot, printk timestamps are reported as zero, which creates a blind spot in early boot timings. This blind spot hinders timing and optimization efforts for code that executes before
time_init(), which is whenlocal_clock()is initialized sufficiently to start returning non-zero timestamps. This period is about 400 milliseconds for many current desktop and embedded machines running Linux.Add an
early_counter_nsfunction that returns nanosecond timestamps based onget_cycles().get_cycles()is operational on arm64 andx86_64from kernel start. Add some calibration printks to allow setting configuration variables that are used to convert cycle counts to nanoseconds (which are then used in early printks). AddCONFIG_EARLY_COUNTER_NS, as well as some associated conversion variables, as new kernel config variables.After proper configuration, this yields non-zero timestamps for printks from the very start of kernel execution. The timestamps are relative to the start of the architecture-specific counter used in
get_cycles(e.g. the TSC onx86_64andcntvct_el0on arm64). This means that the time reported reflects time-from-power-on for most embedded products. This is also a useful data point for boot-time optimization work.Note that there is a discontinuity in the timestamp sequencing when standard clocks are finally initialized in
time_init(). The printk timestamps are thus not monotonically increasing through the entire boot.Signed-off-by Tim Bird <[email protected]>--- init/Kconfig | 47 ++++++++++++++++++++++++++++++++++++++++++ init/main.c | 25 ++++++++++++++++++++++ kernel/printk/printk.c | 15 ++++++++++++++ 3 files changed, 87 insertions(+)init/Kconfig +47
-0@@ -770,6 +770,53 @@ config IKHEADERS or similar programs. If you build the headers as a module, a module called kheaders.ko is built which can be loaded on-demand to get access to headers. +config EARLY_COUNTER_NS + bool "Use counter for early printk timestamps" + default y + depends on PRINTK + help + Use a cycle-counter to provide printk timestamps during early + boot. This allows seeing timing information that would + otherwise be displayed with 0-valued timestamps. + + In order for this to work, you need to specify values for + EARLY_COUNTER_MULT and EARLY_COUNTER_SHIFT, used to convert + from the cycle count to nanoseconds. + +config EARLY_COUNTER_MULT + int "Multiplier for early cycle counter" + depends on PRINTK && EARLY_COUNTER_NS + default 1 + help + This value specifies a multiplier to be used when converting + cycle counts to nanoseconds. The formula used is: + ns = (cycles * mult) >> shift + + Use a multiplier that will bring the value of (cycles * mult) + to near a power of two, that is greater than 1000. The + nanoseconds returned by this conversion are divided by 1000 + to be used as the printk timestamp counter (with resolution + of microseconds). + + As an example, for a cycle-counter with a frequency of 200 Mhz, + the multiplier would be: 10485760, and the shift would be 21. + +config EARLY_COUNTER_SHIFT + int "Shift value for early cycle counter" + range 0 63 + depends on PRINTK && EARLY_COUNTER_NS + default 0 + help + This value specifies a shift value to be used when converting + cycle counts to nanoseconds. The formula used is: + ns = (cycles * mult) >> shift + + Use a shift that will bring the result to a value + in nanoseconds. + + As an example, for a cycle-counter with a frequency of 200 Mhz, + the multiplier would be: 10485760, and the shift would be 21. + config LOG_BUF_SHIFT int "Kernel log buffer size (16 => 64KB, 17 => 128KB)" range 12 25init/main.c +25
-0@@ -105,6 +105,8 @@#include <linux/ptdump.h> #include <linux/time_namespace.h> #include <net/net_namespace.h> +#include <linux/timex.h> +#include <linux/sched/clock.h> #include <asm/io.h> #include <asm/setup.h> @@ -906,6 +908,8 @@ static void __init early_numa_node_init(void)#endif } +static u64 start_cycles, start_ns; + asmlinkage __visible __init __no_sanitize_address __noreturn __no_stack_protectorvoid start_kernel (void ) {@@ -1023,6 +1027,10 @@ void start_kernel(void)timekeeping_init ();time_init (); +/* used to calibrate early_counter_ns */ + start_cycles =get_cycles (); + start_ns =local_clock (); +/* This must be after timekeeping is initialized */ random_init ();@@ -1474,6 +1482,8 @@ void __weak free_initmem(void)static int __ref kernel_init (void *unused) {int ret; +u64 end_cycles, end_ns; +u32 early_mult, early_shift;/* * Wait until kthreadd is all set-up. @@ -1505,6 +1515,21 @@ static int __ref kernel_init(void *unused)do_sysctl_args (); +/* show calibration data for early_counter_ns */ + end_cycles =get_cycles (); + end_ns =local_clock (); +clocks_calc_mult_shift (&early_mult, &early_shift, + ((end_cycles - start_cycles) *NSEC_PER_SEC )/(end_ns - start_ns), +NSEC_PER_SEC ,50 ); + +#ifdef CONFIG_EARLY_COUNTER_NS +pr_info ("Early Counter: start_cycles=%llu, end_cycles=%llu, cycles=%llu\n" , + start_cycles, end_cycles, (end_cycles - start_cycles)); +pr_info ("Early Counter: start_ns=%llu, end_ns=%llu, ns=%llu\n" , + start_ns, end_ns, (end_ns - start_ns)); +pr_info ("Early Counter: MULT=%u, SHIFT=%u\n" , early_mult, early_shift); +#endif +if (ramdisk_execute_command) { ret =run_init_process (ramdisk_execute_command);if (!ret)kernel/printk/printk.c +15
-0@@ -2210,6 +2210,19 @@ static u16 printk_sprint(char *text, u16 size, int facility,return text_len; } +#ifdef CONFIG_EARLY_COUNTER_NS +static inline u64 early_counter_ns (void ) +{ +return ((u64 )get_cycles () *CONFIG_EARLY_COUNTER_MULT ) + >>CONFIG_EARLY_COUNTER_SHIFT ; +} +#else +static inline u64 early_counter_ns (void ) +{ +return 0 ; +} +#endif + __printf(4, 0) int vprintk_store(int facility, int level, const struct dev_printk_info *dev_info,@@ -2239,6 +2252,8 @@ int vprintk_store(int facility, int level,* timestamp with respect to the caller. */ ts_nsec =local_clock (); +if (!ts_nsec) + ts_nsec =early_counter_ns (); caller_id =printk_caller_id ();Signature · 1 line
2.43.0
kernel test robot
build botkernel test robot noticed the following build errors:
Hi Tim,
kernel test robot noticed the following build errors:
[auto build test ERROR on linus/master]
[also build test ERROR on v6.18-rc7]
[cannot apply to akpm-mm/mm-everything next-20251127]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Tim-Bird/printk-add-early_counter_ns-routine-for-printk-blind-spot/20251125-133242 base: linus/master patch link: https://lore.kernel.org/r/39b09edb-8998-4ebd-a564-7d594434a981%40bird.org patch subject: [PATCH] printk: add early_counter_ns routine for printk blind spot config: i386-randconfig-2006-20250825 (https://download.01.org/0day-ci/archive/20251127/[email protected]/config) compiler: gcc-14 (Debian 14.2.0-19) 14.2.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251127/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
ld: init/main.o: in function `kernel_init':
>> init/main.c:1522:(.ref.text+0x201): undefined reference to `__udivdi3'
vim +1522 init/main.c
1481
1482 static int __ref kernel_init(void *unused)
1483 {
1484 int ret;
1485 u64 end_cycles, end_ns;
1486 u32 early_mult, early_shift;
1487
1488 /*
1489 * Wait until kthreadd is all set-up.
1490 */
1491 wait_for_completion(&kthreadd_done);
1492
1493 kernel_init_freeable();
1494 /* need to finish all async __init code before freeing the memory */
1495 async_synchronize_full();
1496
1497 system_state = SYSTEM_FREEING_INITMEM;
1498 kprobe_free_init_mem();
1499 ftrace_free_init_mem();
1500 kgdb_free_init_mem();
1501 exit_boot_config();
1502 free_initmem();
1503 mark_readonly();
1504
1505 /*
1506 * Kernel mappings are now finalized - update the userspace page-table
1507 * to finalize PTI.
1508 */
1509 pti_finalize();
1510
1511 system_state = SYSTEM_RUNNING;
1512 numa_default_policy();
1513
1514 rcu_end_inkernel_boot();
1515
1516 do_sysctl_args();
1517
1518 /* show calibration data for early_counter_ns */
1519 end_cycles = get_cycles();
1520 end_ns = local_clock();
1521 clocks_calc_mult_shift(&early_mult, &early_shift,
> 1522 ((end_cycles - start_cycles) * NSEC_PER_SEC)/(end_ns - start_ns),
1523 NSEC_PER_SEC, 50);
1524
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
PATCH v2 1/1 printk: fix zero-valued printk timestamps in early boot
In reply to Tim BirdDuring early boot, printk timestamps are reported as zero before kernel timekeeping starts (e.g. before time_init()). This hinders boot-time optimization efforts. This period is about 400 milliseconds for many current desktop and embedded machines running Linux.
Add support to save cycles during early boot, and output correct timestamp values after timekeeping is initialized. get_cycles() is operational on arm64 and x86_64 from kernel start. Add code and variables to save calibration values used to later convert cycle counts to time values in the early printks. Add a config to control the feature.
This yields non-zero timestamps for printks from the very start of kernel execution. The timestamps are relative to the start of the architecture-specified counter used in get_cycles (e.g. the TSC on x86_64 and cntvct_el0 on arm64).
All timestamps reflect time from power-on instead of time from the kernel's timekeeping initialization.
---
V1 -> V2
Remove calibration CONFIG vars
Add 'depends on' to restrict arches (to handle ppc bug) Add early_ts_offset to avoid discontinuity Save cycles in ts_nsec, and convert on output Move conditional code to include file (early_times.h)
--- include/linux/early_times.h | 48 +++++++++++++++++++++++++++++++++++++ init/Kconfig | 12 ++++++++++ init/main.c | 26 ++++++++++++++++++++ kernel/printk/printk.c | 16 +++++++++++-- 4 files changed, 100 insertions(+), 2 deletions(-) create mode 100644 include/linux/early_times.h
include/linux/early_times.h new file +48 -0
@@ -0,0 +1,48 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#ifndef _KERNEL_PRINTK_EARLY_TIMES_H +#define _KERNEL_PRINTK_EARLY_TIMES_H + +#include <linux/timex.h> + +#if defined(CONFIG_EARLY_PRINTK_TIMES ) +extern u32 early_mult, early_shift; +extern u64 early_ts_offset; + +static inline u64 early_cycles (void ) +{ +return ((u64 )get_cycles () | (1ULL <<63 )); +} + +static inline u64 adjust_early_ts (u64 ts) +{ +/* High bit means ts is a cycle count */ +if (unlikely (ts & (1ULL <<63 ))) +/* +* mask high bit and convert to ns +* Note that early_mult may be 0, but that's OK because +* we'll just multiply by 0 and return 0. This will +* only occur if we're outputting a printk message +* before the calibration of the early timestamp. +* Any output after user space start (eg. from dmesg or +* journalctl) will show correct values. +*/ +return (((ts & ~(1ULL <<63 )) * early_mult) >> early_shift); + +/* If timestamp is already in ns, just add offset */ +return ts + early_ts_offset; +} +#else +static inline u64 early_cycles (void ) +{ +return 0 ; +} + +static inline u64 adjust_early_ts (u64 ts) +{ +return ts; +} +#endif /* CONFIG_EARLY_PRINTK_TIMES */ + +#endif /* _KERNEL_PRINTK_EARLY_TIMES_H */ +
init/Kconfig +12 -0
@@ -777,6 +777,18 @@ config IKHEADERS or similar programs. If you build the headers as a module, a module called kheaders.ko is built which can be loaded on-demand to get access to headers. +config EARLY_PRINTK_TIMES + bool "Show non-zero printk timestamps early in boot" + default y + depends on PRINTK + depends on ARM64 || X86_64 + help + Use a cycle-counter to provide printk timestamps during + early boot. This allows seeing timestamps for printks that + would otherwise show as 0. Note that this will shift the + printk timestamps to be relative to machine power on, instead + of relative to the start of kernel timekeeping. + config LOG_BUF_SHIFT int "Kernel log buffer size (16 => 64KB, 17 => 128KB)" range 12 25
init/main.c +26 -0
@@ -104,6 +104,9 @@#include <linux/pidfs.h> #include <linux/ptdump.h> #include <linux/time_namespace.h> +#include <linux/timex.h> +#include <linux/sched/clock.h> +#include <linux/early_times.h> #include <net/net_namespace.h> #include <asm/io.h>
@@ -160,6 +163,10 @@ static size_t initargs_offs;# define initargs_offs 0#endif +#ifdef CONFIG_EARLY_PRINTK_TIMES +static u64 start_cycles, start_ns; +#endif +static char *execute_command;static char *ramdisk_execute_command ="/init" ;
@@ -1118,6 +1125,11 @@ void start_kernel(void)timekeeping_init ();time_init (); +#ifdef CONFIG_EARLY_PRINTK_TIMES + start_cycles =get_cycles (); + start_ns =local_clock (); +#endif +/* This must be after timekeeping is initialized */ random_init ();
@@ -1600,6 +1612,20 @@ static int __ref kernel_init(void *unused)do_sysctl_args (); +#ifdef CONFIG_EARLY_PRINTK_TIMES +u64 end_cycles, end_ns; + +/* set calibration data for early_printk_times */ + end_cycles =get_cycles (); + end_ns =local_clock (); +clocks_calc_mult_shift (&early_mult, &early_shift, + ((end_cycles - start_cycles) *NSEC_PER_SEC )/(end_ns - start_ns), +NSEC_PER_SEC ,50 ); + early_ts_offset = ((start_cycles * early_mult) >> early_shift) - start_ns; +pr_debug ("Early printk times: mult=%u, shift=%u, offset=%llu ns\n" , + early_mult, early_shift, early_ts_offset); +#endif +if (ramdisk_execute_command) { ret =run_init_process (ramdisk_execute_command);if (!ret)
kernel/printk/printk.c +14 -2
@@ -46,6 +46,7 @@#include <linux/ctype.h> #include <linux/uio.h> #include <linux/sched/clock.h> +#include <linux/early_times.h> #include <linux/sched/debug.h> #include <linux/sched/task_stack.h> #include <linux/panic.h>
@@ -75,6 +76,11 @@ EXPORT_SYMBOL(ignore_console_lock_warning);EXPORT_TRACEPOINT_SYMBOL_GPL (console); +#ifdef CONFIG_EARLY_PRINTK_TIMES +u32 early_mult, early_shift; +u64 early_ts_offset; +#endif +/* * Low level drivers may need that to know if they can schedule in * their unblank() callback or not. So let's export it.
@@ -639,7 +645,7 @@ static void append_char(char **pp, char *e, char c)static ssize_t info_print_ext_header (char *buf,size_t size,struct printk_info *info) { -u64 ts_usec = info->ts_nsec; +u64 ts_usec =adjust_early_ts (info->ts_nsec);char caller[20 ];#ifdef CONFIG_PRINTK_CALLER u32 id = info->caller_id;
@@ -1352,7 +1358,11 @@ static size_t print_syslog(unsigned int level, char *buf)static size_t print_time (u64 ts,char *buf) { -unsigned long rem_nsec =do_div (ts,1000000000 ); +unsigned long rem_nsec; + + ts =adjust_early_ts (ts); + + rem_nsec =do_div (ts,1000000000 );return sprintf (buf,"[%5lu.%06lu]" , (unsigned long )ts, rem_nsec /1000 );
@@ -2242,6 +2252,8 @@ int vprintk_store(int facility, int level,* timestamp with respect to the caller. */ ts_nsec =local_clock (); +if (!ts_nsec) + ts_nsec =early_cycles (); caller_id =printk_caller_id ();
Signature · 1 line
2.43.0
Re: [PATCH v2] printk: fix zero-valued printk timestamps in early boot
Hi Tim,
I tested this both on X86_64 QEMU and on a i.MX93 (ARM64) and can confirm it is working as expected. Auto-calc of calibration data is far better than the configuration parameters in v1.
It is slightly confusing to see a time value printed to serial output and another one inside kmsg, but that's a human thing and should not confuse any tool.
Some notes follow.
Quoted 96 lines · Tim Bird
During early boot, printk timestamps are reported as zero before kernel timekeeping starts (e.g. before
time_init()). This hinders boot-time optimization efforts. This period is about 400 milliseconds for many current desktop and embedded machines running Linux.Add support to save cycles during early boot, and output correct timestamp values after timekeeping is initialized.
get_cycles()is operational on arm64 andx86_64from kernel start. Add code and variables to save calibration values used to later convert cycle counts to time values in the early printks. Add a config to control the feature.This yields non-zero timestamps for printks from the very start of kernel execution. The timestamps are relative to the start of the architecture-specified counter used in
get_cycles(e.g. the TSC onx86_64andcntvct_el0on arm64).All timestamps reflect time from power-on instead of time from the kernel's timekeeping initialization.
Signed-off-by Tim Bird <[email protected]>---
V1 -> V2
Remove calibration CONFIG vars
Add 'depends on' to restrict arches (to handle ppc bug) Addearly_ts_offsetto avoid discontinuity Save cycles ints_nsec, and convert on output Move conditional code to include file (early_times.h)--- include/linux/early_times.h | 48 +++++++++++++++++++++++++++++++++++++ init/Kconfig | 12 ++++++++++ init/main.c | 26 ++++++++++++++++++++ kernel/printk/printk.c | 16 +++++++++++-- 4 files changed, 100 insertions(+), 2 deletions(-) create mode 100644 include/linux/early_times.hinclude/linux/early_times.h new file +48
-0@@ -0,0 +1,48 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#ifndef _KERNEL_PRINTK_EARLY_TIMES_H +#define _KERNEL_PRINTK_EARLY_TIMES_H + +#include <linux/timex.h> + +#if defined(CONFIG_EARLY_PRINTK_TIMES ) +extern u32 early_mult, early_shift; +extern u64 early_ts_offset; + +static inline u64 early_cycles (void ) +{ +return ((u64 )get_cycles () | (1ULL <<63 )); +} + +static inline u64 adjust_early_ts (u64 ts) +{ +/* High bit means ts is a cycle count */ +if (unlikely (ts & (1ULL <<63 ))) +/* +* mask high bit and convert to ns +* Note that early_mult may be 0, but that's OK because +* we'll just multiply by 0 and return 0. This will +* only occur if we're outputting a printk message +* before the calibration of the early timestamp. +* Any output after user space start (eg. from dmesg or +* journalctl) will show correct values. +*/ +return (((ts & ~(1ULL <<63 )) * early_mult) >> early_shift); + +/* If timestamp is already in ns, just add offset */ +return ts + early_ts_offset; +} +#else +static inline u64 early_cycles (void ) +{ +return 0 ; +} + +static inline u64 adjust_early_ts (u64 ts) +{ +return ts; +} +#endif /* CONFIG_EARLY_PRINTK_TIMES */ + +#endif /* _KERNEL_PRINTK_EARLY_TIMES_H */ +init/Kconfig +0
-0
init/Kconfig config IKHEADERS · line 782
Quoted 6 lines
init/Kconfig +3
-0@@ -777,6 +777,18 @@ config IKHEADERS or similar programs. If you build the headers as a module, a module called kheaders.ko is built which can be loaded on-demand to get access to headers. +config EARLY_PRINTK_TIMES + bool "Show non-zero printk timestamps early in boot" + default y
Considering that this might have a significant impact on monitoring mechanisms already in place (that e.g. expect a specific dmesg print to have a maximum associated time value), please consider a N default here.
init/Kconfig config IKHEADERS · line 791
Quoted 9 lines
init/Kconfig +9
-0+ depends on PRINTK + depends on ARM64 || X86_64 + help + Use a cycle-counter to provide printk timestamps during + early boot. This allows seeing timestamps for printks that + would otherwise show as 0. Note that this will shift the + printk timestamps to be relative to machine power on, instead + of relative to the start of kernel timekeeping. +
To be precise, the timestamps will be relative to processor power on; the machine might have some other processors that run before the Linux one (this is the case for example of i.MX9 or AM62 SoCs) and will be unaccounted for even by this mechanism.
Quoted 29 lines
init/Kconfig +0
-0config LOG_BUF_SHIFT int "Kernel log buffer size (16 => 64KB, 17 => 128KB)" range 12 25init/main.c +7
-0@@ -104,6 +104,9 @@#include <linux/pidfs.h> #include <linux/ptdump.h> #include <linux/time_namespace.h> +#include <linux/timex.h> +#include <linux/sched/clock.h> +#include <linux/early_times.h> #include <net/net_namespace.h> #include <asm/io.h> @@ -160,6 +163,10 @@ static size_t initargs_offs;# define initargs_offs 0#endif +#ifdef CONFIG_EARLY_PRINTK_TIMES +static u64 start_cycles, start_ns; +#endif +static char *execute_command;static char *ramdisk_execute_command ="/init" ;
init/main.c void start_kernel(void) · line 1132
Quoted 8 lines
init/main.c +5
-0@@ -1118,6 +1125,11 @@ void start_kernel(void)timekeeping_init ();time_init (); +#ifdef CONFIG_EARLY_PRINTK_TIMES + start_cycles =get_cycles (); + start_ns =local_clock (); +#endif +
I was wondering it it wouldn't make more sense to move this logic to its own file, and have a plain call e.g. to early_times_init() here (continue...)
Quoted 11 lines
init/main.c +4
-0/* This must be after timekeeping is initialized */ random_init ();@@ -1600,6 +1612,20 @@ static int __ref kernel_init(void *unused)do_sysctl_args (); +#ifdef CONFIG_EARLY_PRINTK_TIMES +u64 end_cycles, end_ns; + +/* set calibration data for early_printk_times */
init/main.c static int __ref kernel_init(void *unused) · line 1628
Quoted 10 lines
init/main.c +10
-0+ end_cycles =get_cycles (); + end_ns =local_clock (); +clocks_calc_mult_shift (&early_mult, &early_shift, + ((end_cycles - start_cycles) *NSEC_PER_SEC )/(end_ns - start_ns), +NSEC_PER_SEC ,50 ); + early_ts_offset = ((start_cycles * early_mult) >> early_shift) - start_ns; +pr_debug ("Early printk times: mult=%u, shift=%u, offset=%llu ns\n" , + early_mult, early_shift, early_ts_offset); +#endif +
(...continue) and to early_times_calc() or something like that here.
In this way, all related variables (i.e.: start_cycles, start_ns from this file, but also early_mult, early_shift, and early_ts_offset from kernel/printk/printk.c) can be confined to their own file and not add noise here.
Quoted 61 lines
init/main.c +0
-0if (ramdisk_execute_command) { ret =run_init_process (ramdisk_execute_command);if (!ret)kernel/printk/printk.c +14
-2@@ -46,6 +46,7 @@#include <linux/ctype.h> #include <linux/uio.h> #include <linux/sched/clock.h> +#include <linux/early_times.h> #include <linux/sched/debug.h> #include <linux/sched/task_stack.h> #include <linux/panic.h> @@ -75,6 +76,11 @@ EXPORT_SYMBOL(ignore_console_lock_warning);EXPORT_TRACEPOINT_SYMBOL_GPL (console); +#ifdef CONFIG_EARLY_PRINTK_TIMES +u32 early_mult, early_shift; +u64 early_ts_offset; +#endif +/* * Low level drivers may need that to know if they can schedule in * their unblank() callback or not. So let's export it. @@ -639,7 +645,7 @@ static void append_char(char **pp, char *e, char c)static ssize_t info_print_ext_header (char *buf,size_t size,struct printk_info *info) { -u64 ts_usec = info->ts_nsec; +u64 ts_usec =adjust_early_ts (info->ts_nsec);char caller[20 ];#ifdef CONFIG_PRINTK_CALLER u32 id = info->caller_id;@@ -1352,7 +1358,11 @@ static size_t print_syslog(unsigned int level, char *buf)static size_t print_time (u64 ts,char *buf) { -unsigned long rem_nsec =do_div (ts,1000000000 ); +unsigned long rem_nsec; + + ts =adjust_early_ts (ts); + + rem_nsec =do_div (ts,1000000000 );return sprintf (buf,"[%5lu.%06lu]" , (unsigned long )ts, rem_nsec /1000 );@@ -2242,6 +2252,8 @@ int vprintk_store(int facility, int level,* timestamp with respect to the caller. */ ts_nsec =local_clock (); +if (!ts_nsec) + ts_nsec =early_cycles (); caller_id =printk_caller_id ();Signature · 1 line
2.43.0
Regards,
Francesco
RE: [PATCH v2] printk: fix zero-valued printk timestamps in early boot
Quoted 7 lines
-----Original Message-----
From: Francesco Valla <[email protected]>Hi Tim,
Quoted 7 lines
I tested this both on
X86_64QEMU and on a i.MX93 (ARM64) and can confirm it is working as expected. Auto-calc of calibration data is far better than the configuration parameters in v1.It is slightly confusing to see a time value printed to serial output and another one inside kmsg, but that's a human thing and should not confuse any tool.
Agreed. I wasn't too worried about it, because most serious developers working on boot-time will not be watching early messages over serial console. (Usually they use 'quiet' or some lower log level). But on qemu, it does look strange to see 0s on the first output sequence, and then non-zeroes when using dmesg later in the same boot.
I just realized though, that I should go back and see if there's a discontinuity on the output via serial (before and after calibration), and possibly put a note about that in the config description.
I'll think about what I can do here to reduce the confusion.
Quoted 103 lines
Some notes follow.
Quoted 99 lines · Tim Bird
During early boot, printk timestamps are reported as zero before kernel timekeeping starts (e.g. before
time_init()). This hinders boot-time optimization efforts. This period is about 400 milliseconds for many current desktop and embedded machines running Linux.Add support to save cycles during early boot, and output correct timestamp values after timekeeping is initialized.
get_cycles()is operational on arm64 andx86_64from kernel start. Add code and variables to save calibration values used to later convert cycle counts to time values in the early printks. Add a config to control the feature.This yields non-zero timestamps for printks from the very start of kernel execution. The timestamps are relative to the start of the architecture-specified counter used in
get_cycles(e.g. the TSC onx86_64andcntvct_el0on arm64).All timestamps reflect time from power-on instead of time from the kernel's timekeeping initialization.
Signed-off-by Tim Bird <[email protected]>---
V1 -> V2
Remove calibration CONFIG vars
Add 'depends on' to restrict arches (to handle ppc bug) Addearly_ts_offsetto avoid discontinuity Save cycles ints_nsec, and convert on output Move conditional code to include file (early_times.h)--- include/linux/early_times.h | 48 +++++++++++++++++++++++++++++++++++++ init/Kconfig | 12 ++++++++++ init/main.c | 26 ++++++++++++++++++++ kernel/printk/printk.c | 16 +++++++++++-- 4 files changed, 100 insertions(+), 2 deletions(-) create mode 100644 include/linux/early_times.hinclude/linux/early_times.h new file +48
-0@@ -0,0 +1,48 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#ifndef _KERNEL_PRINTK_EARLY_TIMES_H +#define _KERNEL_PRINTK_EARLY_TIMES_H + +#include <linux/timex.h> + +#if defined(CONFIG_EARLY_PRINTK_TIMES ) +extern u32 early_mult, early_shift; +extern u64 early_ts_offset; + +static inline u64 early_cycles (void ) +{ +return ((u64 )get_cycles () | (1ULL <<63 )); +} + +static inline u64 adjust_early_ts (u64 ts) +{ +/* High bit means ts is a cycle count */ +if (unlikely (ts & (1ULL <<63 ))) +/* +* mask high bit and convert to ns +* Note that early_mult may be 0, but that's OK because +* we'll just multiply by 0 and return 0. This will +* only occur if we're outputting a printk message +* before the calibration of the early timestamp. +* Any output after user space start (eg. from dmesg or +* journalctl) will show correct values. +*/ +return (((ts & ~(1ULL <<63 )) * early_mult) >> early_shift); + +/* If timestamp is already in ns, just add offset */ +return ts + early_ts_offset; +} +#else +static inline u64 early_cycles (void ) +{ +return 0 ; +} + +static inline u64 adjust_early_ts (u64 ts) +{ +return ts; +} +#endif /* CONFIG_EARLY_PRINTK_TIMES */ + +#endif /* _KERNEL_PRINTK_EARLY_TIMES_H */ +init/Kconfig +0
-0@@ -777,6 +777,18 @@ config IKHEADERS or similar programs. If you build the headers as a module, a module called kheaders.ko is built which can be loaded on-demand to get access to headers.
Quoted 7 lines
init/Kconfig config IKHEADERS · line 782
Quoted 3 lines
init/Kconfig +3
-0+config EARLY_PRINTK_TIMES + bool "Show non-zero printk timestamps early in boot" + default yConsidering that this might have a significant impact on monitoring mechanisms already in place (that e.g. expect a specific dmesg print to have a maximum associated time value), please consider a N default here.
Oops! Sorry, that was supposed to be 'default n'. You're right. I know I had this as default N, and I think I switched it temporarily for testing, and forgot to switch it back (and never caught it the numerous times I reviewed the patch before sending it out again, ugh). Thanks for catching this.
If people like this, and we don't see any problems with tooling or virtualization, I could see it switching to default Y in the future. But for now this should definitely be 'default n'.
Quoted 15 lines
init/Kconfig config IKHEADERS · line 791
Quoted 10 lines
init/Kconfig +9
-0+ depends on PRINTK + depends on ARM64 || X86_64 + help + Use a cycle-counter to provide printk timestamps during + early boot. This allows seeing timestamps for printks that + would otherwise show as 0. Note that this will shift the + printk timestamps to be relative to machine power on, instead + of relative to the start of kernel timekeeping. +To be precise, the timestamps will be relative to processor power on; the machine might have some other processors that run before the Linux one (this is the case for example of i.MX9 or AM62 SoCs) and will be unaccounted for even by this mechanism.
Good point. Even more precisely, it will be relative to
cycle-counter value initialization or reset, which often (but not always) corresponds to processor power on.
I'll adjust the wording.
I'm still a bit unsure what happens in the virtualization case. qemu seems to initialize the TSC at qemu start, but I'm not sure what happens for e.g. client VMs on cloud servers.
Quoted 67 lines
Quoted 30 lines
init/Kconfig +0
-0config LOG_BUF_SHIFT int "Kernel log buffer size (16 => 64KB, 17 => 128KB)" range 12 25init/main.c +7
-0@@ -104,6 +104,9 @@#include <linux/pidfs.h> #include <linux/ptdump.h> #include <linux/time_namespace.h> +#include <linux/timex.h> +#include <linux/sched/clock.h> +#include <linux/early_times.h> #include <net/net_namespace.h> #include <asm/io.h> @@ -160,6 +163,10 @@ static size_t initargs_offs;# define initargs_offs 0#endif +#ifdef CONFIG_EARLY_PRINTK_TIMES +static u64 start_cycles, start_ns; +#endif +static char *execute_command;static char *ramdisk_execute_command ="/init" ;init/main.c void start_kernel(void) · line 1132
Quoted 8 lines
init/main.c +5
-0@@ -1118,6 +1125,11 @@ void start_kernel(void)timekeeping_init ();time_init (); +#ifdef CONFIG_EARLY_PRINTK_TIMES + start_cycles =get_cycles (); + start_ns =local_clock (); +#endif +I was wondering it it wouldn't make more sense to move this logic to its own file, and have a plain call e.g. to
early_times_init()here (continue...)Quoted 11 lines
init/main.c +4
-0/* This must be after timekeeping is initialized */ random_init ();@@ -1600,6 +1612,20 @@ static int __ref kernel_init(void *unused)do_sysctl_args (); +#ifdef CONFIG_EARLY_PRINTK_TIMES +u64 end_cycles, end_ns; + +/* set calibration data for early_printk_times */ init/main.c static int __ref kernel_init(void *unused) · line 1628
Quoted 10 lines
init/main.c +10
-0+ end_cycles =get_cycles (); + end_ns =local_clock (); +clocks_calc_mult_shift (&early_mult, &early_shift, + ((end_cycles - start_cycles) *NSEC_PER_SEC )/(end_ns - start_ns), +NSEC_PER_SEC ,50 ); + early_ts_offset = ((start_cycles * early_mult) >> early_shift) - start_ns; +pr_debug ("Early printk times: mult=%u, shift=%u, offset=%llu ns\n" , + early_mult, early_shift, early_ts_offset); +#endif +(...continue) and to
early_times_calc()or something like that here.
Quoted 4 lines
In this way, all related variables (i.e.:
start_cycles,start_nsfrom this file, but alsoearly_mult,early_shift, andearly_ts_offsetfromkernel/printk/printk.c) can be confined to their own file and not add noise here.
I thought a lot about this, and wasn't sure whether to use that approach or not. I would need two routines: early_times_start_calibration, and early_times_finish_calibration(). I agree with the idea of keeping all variables in the printk module, so this is more self-contained. It also reduces the number of visible #ifdefs.
I think I'll go ahead and do this, and see what the code looks like. I suspect that what you recommend is the better way to go.
Quoted 57 lines
Quoted 57 lines
init/main.c +0
-0if (ramdisk_execute_command) { ret =run_init_process (ramdisk_execute_command);if (!ret)kernel/printk/printk.c +14
-2@@ -46,6 +46,7 @@#include <linux/ctype.h> #include <linux/uio.h> #include <linux/sched/clock.h> +#include <linux/early_times.h> #include <linux/sched/debug.h> #include <linux/sched/task_stack.h> #include <linux/panic.h> @@ -75,6 +76,11 @@ EXPORT_SYMBOL(ignore_console_lock_warning);EXPORT_TRACEPOINT_SYMBOL_GPL (console); +#ifdef CONFIG_EARLY_PRINTK_TIMES +u32 early_mult, early_shift; +u64 early_ts_offset; +#endif +/* * Low level drivers may need that to know if they can schedule in * their unblank() callback or not. So let's export it. @@ -639,7 +645,7 @@ static void append_char(char **pp, char *e, char c)static ssize_t info_print_ext_header (char *buf,size_t size,struct printk_info *info) { -u64 ts_usec = info->ts_nsec; +u64 ts_usec =adjust_early_ts (info->ts_nsec);char caller[20 ];#ifdef CONFIG_PRINTK_CALLER u32 id = info->caller_id;@@ -1352,7 +1358,11 @@ static size_t print_syslog(unsigned int level, char *buf)static size_t print_time (u64 ts,char *buf) { -unsigned long rem_nsec =do_div (ts,1000000000 ); +unsigned long rem_nsec; + + ts =adjust_early_ts (ts); + + rem_nsec =do_div (ts,1000000000 );return sprintf (buf,"[%5lu.%06lu]" , (unsigned long )ts, rem_nsec /1000 );@@ -2242,6 +2252,8 @@ int vprintk_store(int facility, int level,* timestamp with respect to the caller. */ ts_nsec =local_clock (); +if (!ts_nsec) + ts_nsec =early_cycles ();
Quoted 8 lines
Quoted 5 lines
kernel/printk/printk.c +0
-1caller_id =printk_caller_id (); --2.43.0
Regards,
Francesco
Thanks very much for the review and test!!
-- Tim
Petr Mladek
maintainerRe: [PATCH v2] printk: fix zero-valued printk timestamps in early boot
Quoted 14 lines · Tim
Quoted 7 lines
-----Original Message-----
From: Francesco Valla <[email protected]>Hi Tim,
Quoted 7 lines
I tested this both on
X86_64QEMU and on a i.MX93 (ARM64) and can confirm it is working as expected. Auto-calc of calibration data is far better than the configuration parameters in v1.It is slightly confusing to see a time value printed to serial output and another one inside kmsg, but that's a human thing and should not confuse any tool.
Quoted 8 lines
Agreed. I wasn't too worried about it, because most serious developers working on boot-time will not be watching early messages over serial console. (Usually they use 'quiet' or some lower log level). But on qemu, it does look strange to see 0s on the first output sequence, and then non-zeroes when using dmesg later in the same boot.
I just realized though, that I should go back and see if there's a discontinuity on the output via serial (before and after calibration), and possibly put a note about that in the config description.
I see the following in the serial console output:
[ 3.288049][ T1] Write protecting the kernel read-only data: 36864k [ 3.298554][ T1] Freeing unused kernel image (text/rodata gap) memory: 1656K [ 3.318942][ T1] Freeing unused kernel image (rodata/data gap) memory: 1540K [ 12.230014][ T1] Early printk times: mult=38775352, shift=27, offset=8891950261 ns [ 12.246008][ T1] Run /init as init process [ 12.254944][ T1] with arguments: [ 12.264341][ T1] /init [ 12.272184][ T1] nosplash
And this is from dmesg -S
[ 12.179999] [ T1] Write protecting the kernel read-only data: 36864k [ 12.190505] [ T1] Freeing unused kernel image (text/rodata gap) memory: 1656K [ 12.210893] [ T1] Freeing unused kernel image (rodata/data gap) memory: 1540K [ 12.230014] [ T1] Early printk times: mult=38775352, shift=27, offset=8891950261 ns [ 12.246008] [ T1] Run /init as init process [ 12.254944] [ T1] with arguments: [ 12.264341] [ T1] /init [ 12.272184] [ T1] nosplash
Quoted 1 line
I'll think about what I can do here to reduce the confusion.
I though about showing the non-adjusted timestamp with '?', Something like:
[ 3.288049?][ T1] Write protecting the kernel read-only data: 36864k [ 3.298554?][ T1] Freeing unused kernel image (text/rodata gap) memory: 1656K [ 3.318942?][ T1] Freeing unused kernel image (rodata/data gap) memory: 1540K [ 12.230014][ T1] Early printk times: mult=38775352, shift=27, offset=8891950261 ns [ 12.246008][ T1] Run /init as init process [ 12.254944][ T1] with arguments: [ 12.264341][ T1] /init [ 12.272184][ T1] nosplash
But I am afraid that it might break some monitoring tools.
Well, it might be acceptable when this feature is not enabled in production systems.
Quoted 14 lines
Quoted 6 lines
Quoted 6 lines
init/Kconfig +0
-0@@ -777,6 +777,18 @@ config IKHEADERS or similar programs. If you build the headers as a module, a module called kheaders.ko is built which can be loaded on-demand to get access to headers.Quoted 7 lines
init/Kconfig config IKHEADERS · line 782
Quoted 3 lines
init/Kconfig +3
-0+config EARLY_PRINTK_TIMES + bool "Show non-zero printk timestamps early in boot" + default yConsidering that this might have a significant impact on monitoring mechanisms already in place (that e.g. expect a specific dmesg print to have a maximum associated time value), please consider a N default here.
Quoted 8 lines
Oops! Sorry, that was supposed to be 'default n'. You're right. I know I had this as default N, and I think I switched it temporarily for testing, and forgot to switch it back (and never caught it the numerous times I reviewed the patch before sending it out again, ugh). Thanks for catching this.
If people like this, and we don't see any problems with tooling or virtualization, I could see it switching to default Y in the future. But for now this should definitely be 'default n'.
We need to be careful. The different output on console and via dmesg might confuse people. The extra '?' might help poeple but it might confuse tools.
Quoted 16 lines
Quoted 15 lines
init/Kconfig config IKHEADERS · line 791
Quoted 10 lines
init/Kconfig +9
-0+ depends on PRINTK + depends on ARM64 || X86_64 + help + Use a cycle-counter to provide printk timestamps during + early boot. This allows seeing timestamps for printks that + would otherwise show as 0. Note that this will shift the + printk timestamps to be relative to machine power on, instead + of relative to the start of kernel timekeeping. +To be precise, the timestamps will be relative to processor power on; the machine might have some other processors that run before the Linux one (this is the case for example of i.MX9 or AM62 SoCs) and will be unaccounted for even by this mechanism.
Quoted 8 lines
Good point. Even more precisely, it will be relative to
cycle-counter value initialization or reset, which often (but not always) corresponds to processor power on.I'll adjust the wording.
I'm still a bit unsure what happens in the virtualization case. qemu seems to initialize the TSC at qemu start, but I'm not sure what happens for e.g. client VMs on cloud servers.
I see the following via QEMU (from dmesg):
[ 8.853613] Linux version 6.19.0-rc7-default+ (pmladek@pathway) (gcc (SUSE Linux) 15.2.1 20251006, GNU ld (GNU Binutils; openSUSE Tumbleweed) 2.45.0.20251103-2) #521 SMP PREEMPT_DYNAMIC Mon Feb 2 16:36:53 CET 2026 [ 8.853617] Command line: BOOT_IMAGE=/boot/vmlinuz-6.19.0-rc7-default+ root=UUID=587ae802-e330-4059-9b48-d5b845e1075a resume=/dev/disk/by-uuid/369c7453-3d16-409d-88b2-5de027891a12 mitigations=auto nosplash earlycon=uart8250,io,0x3f8,115200 console=ttyS0,115200 console=ttynull console=tty0 debug_non_panic_cpus=1 panic=10 ignore_loglevel log_buf_len=1M [ 8.865086] BIOS-provided physical RAM map: [ 8.865087] BIOS-e820: [mem 0x0000000000000000-0x000000000009fbff] usable [ 8.865089] BIOS-e820: [mem 0x000000000009fc00-0x000000000009ffff] reserved [ 8.865090] BIOS-e820: [mem 0x00000000000f0000-0x00000000000fffff] reserved [ 8.865090] BIOS-e820: [mem 0x0000000000100000-0x000000007ffdbfff] usable [ 8.865091] BIOS-e820: [mem 0x000000007ffdc000-0x000000007fffffff] reserved [ 8.865092] BIOS-e820: [mem 0x00000000b0000000-0x00000000bfffffff] reserved [ 8.865092] BIOS-e820: [mem 0x00000000fed1c000-0x00000000fed1ffff] reserved [ 8.865093] BIOS-e820: [mem 0x00000000feffc000-0x00000000feffffff] reserved [ 8.865093] BIOS-e820: [mem 0x00000000fffc0000-0x00000000ffffffff] reserved [ 8.865094] BIOS-e820: [mem 0x0000000100000000-0x000000017fffffff] usable [ 8.865094] BIOS-e820: [mem 0x000000fd00000000-0x000000ffffffffff] reserved [ 8.865171] earlycon: uart8250 at I/O port 0x3f8 (options '115200') [ 8.865176] printk: legacy bootconsole [uart8250] enabled [ 8.892181] printk: allow messages from non-panic CPUs in panic() [ 8.893327] printk: debug: ignoring loglevel setting. [...] [ 12.162011] Freeing unused decrypted memory: 2036K [ 12.171970] Freeing unused kernel image (initmem) memory: 7120K [ 12.179999] Write protecting the kernel read-only data: 36864k [ 12.190505] Freeing unused kernel image (text/rodata gap) memory: 1656K [ 12.210893] Freeing unused kernel image (rodata/data gap) memory: 1540K [ 12.230014] Early printk times: mult=38775352, shift=27, offset=8891950261 ns [ 12.246008] Run /init as init process [ 12.254944] with arguments: [ 12.264341] /init [ 12.272184] nosplash [ 12.280738] with environment: [ 12.288728] HOME=/ [ 12.296319] TERM=linux
Best Regards,
Petr
Re: [PATCH v2] printk: fix zero-valued printk timestamps in early boot
In reply to Tim BirdHi Tim,
Quoted 21 lines · Tim Bird
During early boot, printk timestamps are reported as zero before kernel timekeeping starts (e.g. before
time_init()). This hinders boot-time optimization efforts. This period is about 400 milliseconds for many current desktop and embedded machines running Linux.Add support to save cycles during early boot, and output correct timestamp values after timekeeping is initialized.
get_cycles()is operational on arm64 andx86_64from kernel start. Add code and variables to save calibration values used to later convert cycle counts to time values in the early printks. Add a config to control the feature.This yields non-zero timestamps for printks from the very start of kernel execution. The timestamps are relative to the start of the architecture-specified counter used in
get_cycles(e.g. the TSC onx86_64andcntvct_el0on arm64).All timestamps reflect time from power-on instead of time from the kernel's timekeeping initialization.
Quoted 8 lines
Signed-off-by Tim Bird <[email protected]>---
V1 -> V2
Remove calibration CONFIG vars
Add 'depends on' to restrict arches (to handle ppc bug) Addearly_ts_offsetto avoid discontinuity Save cycles ints_nsec, and convert on output Move conditional code to include file (early_times.h)
Thanks for the update!
Quoted 7 lines
include/linux/early_times.h new file +4
-0@@ -0,0 +1,48 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#ifndef _KERNEL_PRINTK_EARLY_TIMES_H +#define _KERNEL_PRINTK_EARLY_TIMES_H
include/linux/early_times.h line 14
Quoted 10 lines
include/linux/early_times.h new file +10
-0+ +#include <linux/timex.h> + +#if defined(CONFIG_EARLY_PRINTK_TIMES ) +extern u32 early_mult, early_shift; +extern u64 early_ts_offset; + +static inline u64 early_cycles (void ) +{ +return ((u64 )get_cycles () | (1ULL <<63 ));
No need to cast to u64, as the second operand of the OR is u64 anyway. BIT_ULL(63)
I think it would be good to have a #define for this at the top.
Quoted 6 lines
include/linux/early_times.h new file +6
-0+} + +static inline u64 adjust_early_ts (u64 ts) +{ +/* High bit means ts is a cycle count */ +if (unlikely (ts & (1ULL <<63 )))
include/linux/early_times.h line 30
Quoted 10 lines
include/linux/early_times.h new file +10
-0+/* +* mask high bit and convert to ns +* Note that early_mult may be 0, but that's OK because +* we'll just multiply by 0 and return 0. This will +* only occur if we're outputting a printk message +* before the calibration of the early timestamp. +* Any output after user space start (eg. from dmesg or +* journalctl) will show correct values. +*/ +return (((ts & ~(1ULL <<63 )) * early_mult) >> early_shift);
Please use the mul_u64_u32_shr() helper.
Please wrap this block in curly braces.
Alternatively, you can invert the logic:
if (likely(!(ts & DEFINITION_FOR_1ULL_LSH63)))
return ts + early_ts_offset;Quoted 23 lines
include/linux/early_times.h new file +18
-0+ +/* If timestamp is already in ns, just add offset */ +return ts + early_ts_offset; +} +#else +static inline u64 early_cycles (void ) +{ +return 0 ; +} + +static inline u64 adjust_early_ts (u64 ts) +{ +return ts; +} +#endif /* CONFIG_EARLY_PRINTK_TIMES */ + +#endif /* _KERNEL_PRINTK_EARLY_TIMES_H */ +init/Kconfig +0
-0
init/Kconfig config IKHEADERS · line 784
Quoted 8 lines
init/Kconfig +5
-0@@ -777,6 +777,18 @@ config IKHEADERS or similar programs. If you build the headers as a module, a module called kheaders.ko is built which can be loaded on-demand to get access to headers. +config EARLY_PRINTK_TIMES + bool "Show non-zero printk timestamps early in boot" + default y + depends on PRINTK + depends on ARM64 || X86_64
So for now this is limited to (a few) 64-bit platforms...
Quoted 16 lines
init/Kconfig +7
-0+ help + Use a cycle-counter to provide printk timestamps during + early boot. This allows seeing timestamps for printks that + would otherwise show as 0. Note that this will shift the + printk timestamps to be relative to machine power on, instead + of relative to the start of kernel timekeeping. + config LOG_BUF_SHIFT int "Kernel log buffer size (16 => 64KB, 17 => 128KB)" range 12 25init/main.c +0
-0
init/main.c static size_t initargs_offs; · line 167
Quoted 5 lines
init/main.c +2
-0@@ -160,6 +163,10 @@ static size_t initargs_offs;# define initargs_offs 0#endif +#ifdef CONFIG_EARLY_PRINTK_TIMES +static u64 start_cycles, start_ns;
cycles_t start_cycles;
(cycles_t is unsigned long, i.e. either 32- or 64-bit).
Quoted 18 lines
init/main.c +7
-0+#endif +static char *execute_command;static char *ramdisk_execute_command ="/init" ;@@ -1118,6 +1125,11 @@ void start_kernel(void)timekeeping_init ();time_init (); +#ifdef CONFIG_EARLY_PRINTK_TIMES + start_cycles =get_cycles (); + start_ns =local_clock (); +#endif +/* This must be after timekeeping is initialized */ random_init ();
init/main.c static int __ref kernel_init(void *unused) · line 1616
Quoted 5 lines
init/main.c +2
-0@@ -1600,6 +1612,20 @@ static int __ref kernel_init(void *unused)do_sysctl_args (); +#ifdef CONFIG_EARLY_PRINTK_TIMES +u64 end_cycles, end_ns;
cycles_t end_cycles;
init/main.c static int __ref kernel_init(void *unused) · line 1623
Quoted 7 lines
init/main.c +7
-0+ +/* set calibration data for early_printk_times */ + end_cycles =get_cycles (); + end_ns =local_clock (); +clocks_calc_mult_shift (&early_mult, &early_shift, + ((end_cycles - start_cycles) *NSEC_PER_SEC )/(end_ns - start_ns), +NSEC_PER_SEC ,50 );
mul_u64_u64_div_u64() is probably the best helper that is available (there is no mul_ulong_u32_div_u64()).
init/main.c static int __ref kernel_init(void *unused) · line 1631
Quoted 8 lines
init/main.c +5
-0+ early_ts_offset = ((start_cycles * early_mult) >> early_shift) - start_ns; +pr_debug ("Early printk times: mult=%u, shift=%u, offset=%llu ns\n" , + early_mult, early_shift, early_ts_offset); +#endif +if (ramdisk_execute_command) { ret =run_init_process (ramdisk_execute_command);if (!ret)
Gr{oetje,eeting}s,
Geert
Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected] In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that. -- Linus Torvalds
RE: [PATCH v2] printk: fix zero-valued printk timestamps in early boot
Quoted 60 lines
-----Original Message-----
From: Geert Uytterhoeven <[email protected]>
Hi Tim,Quoted 21 lines · Tim Bird
During early boot, printk timestamps are reported as zero before kernel timekeeping starts (e.g. before
time_init()). This hinders boot-time optimization efforts. This period is about 400 milliseconds for many current desktop and embedded machines running Linux.Add support to save cycles during early boot, and output correct timestamp values after timekeeping is initialized.
get_cycles()is operational on arm64 andx86_64from kernel start. Add code and variables to save calibration values used to later convert cycle counts to time values in the early printks. Add a config to control the feature.This yields non-zero timestamps for printks from the very start of kernel execution. The timestamps are relative to the start of the architecture-specified counter used in
get_cycles(e.g. the TSC onx86_64andcntvct_el0on arm64).All timestamps reflect time from power-on instead of time from the kernel's timekeeping initialization.
Quoted 8 lines
Signed-off-by Tim Bird <[email protected]>---
V1 -> V2
Remove calibration CONFIG vars
Add 'depends on' to restrict arches (to handle ppc bug) Addearly_ts_offsetto avoid discontinuity Save cycles ints_nsec, and convert on output Move conditional code to include file (early_times.h)Thanks for the update!
Quoted 7 lines
include/linux/early_times.h new file +4
-0@@ -0,0 +1,48 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#ifndef _KERNEL_PRINTK_EARLY_TIMES_H +#define _KERNEL_PRINTK_EARLY_TIMES_Hinclude/linux/early_times.h line 14
Quoted 10 lines
include/linux/early_times.h new file +10
-0+ +#include <linux/timex.h> + +#if defined(CONFIG_EARLY_PRINTK_TIMES ) +extern u32 early_mult, early_shift; +extern u64 early_ts_offset; + +static inline u64 early_cycles (void ) +{ +return ((u64 )get_cycles () | (1ULL <<63 ));No need to cast to u64, as the second operand of the OR is u64 anyway.
BIT_ULL(63)
Quoted 1 line
I think it would be good to have a #define for this at the top.
I'll look at this. Is BIT_ULL(63) preferred over (1ULL << 63)?
Do you think something like "HIGH_BIT63" would be good enough?
Quoted 11 lines
Quoted 11 lines
include/linux/early_times.h new file +10
-0+} + +static inline u64 adjust_early_ts (u64 ts) +{ +/* High bit means ts is a cycle count */ +if (unlikely (ts & (1ULL <<63 ))) +/* +* mask high bit and convert to ns +* Note that early_mult may be 0, but that's OK because +* we'll just multiply by 0 and return 0. This will
Quoted 8 lines
include/linux/early_times.h line 30
Quoted 6 lines
include/linux/early_times.h new file +6
-0+* only occur if we're outputting a printk message +* before the calibration of the early timestamp. +* Any output after user space start (eg. from dmesg or +* journalctl) will show correct values. +*/ +return (((ts & ~(1ULL <<63 )) * early_mult) >> early_shift);Please use the
mul_u64_u32_shr()helper.
OK. I did not know about that.
I can check, but do you know offhand if timestamps from local_clock() on 32-bit systems are always 64-bit nanoseconds? I assume so looking at the printk code and making some assumptions. (But that's dangerous.)
Quoted 6 lines
Please wrap this block in curly braces.
Alternatively, you can invert the logic:if (likely(!(ts & DEFINITION_FOR_1ULL_LSH63))) return ts + early_ts_offset;
This is probably a better structure anyway. Will do.
Quoted 27 lines
Quoted 27 lines
include/linux/early_times.h new file +18
-0+ +/* If timestamp is already in ns, just add offset */ +return ts + early_ts_offset; +} +#else +static inline u64 early_cycles (void ) +{ +return 0 ; +} + +static inline u64 adjust_early_ts (u64 ts) +{ +return ts; +} +#endif /* CONFIG_EARLY_PRINTK_TIMES */ + +#endif /* _KERNEL_PRINTK_EARLY_TIMES_H */ +init/Kconfig +0
-0@@ -777,6 +777,18 @@ config IKHEADERS or similar programs. If you build the headers as a module, a module called kheaders.ko is built which can be loaded on-demand to get access to headers.
Quoted 7 lines
init/Kconfig config IKHEADERS · line 784
Quoted 5 lines
init/Kconfig +5
-0+config EARLY_PRINTK_TIMES + bool "Show non-zero printk timestamps early in boot" + default y + depends on PRINTK + depends on ARM64 || X86_64So for now this is limited to (a few) 64-bit platforms...
Yes, but it really shouldn't be. I got spooked when 0-day told me that the code wouldn't link on powerpc, so I restricted it to just machines I was actually testing. But this should work on some ARM32 and most x8632 platforms. Actually, it should work on anything that has a cycle-counter from kernel start (some RISC machine might qualify as well). However, I've seen a few cases where some platforms require kernel initialization of their cycle-counter, and I wanted to play it safe.
If such platforms are well-behaved and return 0 before they are initialized, it should still be safe to turn this on - it just won't have any effect.
I plan to do some more investigation of the powerpc error. It was
powerpc-linux-ld: init/main.o: in function `kernel_init':
main.c:(.ref.text+0x144): undefined reference to `__udivdi3'
from the definition of clocks_calc_mult_shift(), which seems to indicate a bug in the powerpc code. In the long run, I should try to track down that bug rather than exclude a bunch of other (likely-working) arches. But I was playing it conservative for now.
Quoted 20 lines
Quoted 20 lines
init/Kconfig +7
-0+ help + Use a cycle-counter to provide printk timestamps during + early boot. This allows seeing timestamps for printks that + would otherwise show as 0. Note that this will shift the + printk timestamps to be relative to machine power on, instead + of relative to the start of kernel timekeeping. + config LOG_BUF_SHIFT int "Kernel log buffer size (16 => 64KB, 17 => 128KB)" range 12 25init/main.c +0
-0@@ -160,6 +163,10 @@ static size_t initargs_offs;# define initargs_offs 0#endif
Quoted 6 lines
init/main.c static size_t initargs_offs; · line 167
Quoted 2 lines
init/main.c +2
-0+#ifdef CONFIG_EARLY_PRINTK_TIMES +static u64 start_cycles, start_ns;
cycles_tstart_cycles;(
cycles_tis unsigned long, i.e. either 32- or 64-bit).
OK, I’ll use this type.
Quoted 18 lines
Quoted 18 lines
init/main.c +7
-0+#endif +static char *execute_command;static char *ramdisk_execute_command ="/init" ;@@ -1118,6 +1125,11 @@ void start_kernel(void)timekeeping_init ();time_init (); +#ifdef CONFIG_EARLY_PRINTK_TIMES + start_cycles =get_cycles (); + start_ns =local_clock (); +#endif +/* This must be after timekeeping is initialized */ random_init ();
Quoted 8 lines
init/main.c static int __ref kernel_init(void *unused) · line 1616
Quoted 6 lines
init/main.c +2
-0@@ -1600,6 +1612,20 @@ static int __ref kernel_init(void *unused)do_sysctl_args (); +#ifdef CONFIG_EARLY_PRINTK_TIMES +u64 end_cycles, end_ns;
cycles_tend_cycles;
ack.
Quoted 11 lines
init/main.c static int __ref kernel_init(void *unused) · line 1623
Quoted 8 lines
init/main.c +7
-0+ +/* set calibration data for early_printk_times */ + end_cycles =get_cycles (); + end_ns =local_clock (); +clocks_calc_mult_shift (&early_mult, &early_shift, + ((end_cycles - start_cycles) *NSEC_PER_SEC )/(end_ns - start_ns), +NSEC_PER_SEC ,50 );
mul_u64_u64_div_u64()is probably the best helper that is available (there is nomul_ulong_u32_div_u64()).
What would the mul_u64_u64_div_u64 do if cycles_t is u32? Should it sill all work, just not optimized?
Quoted 12 lines
init/main.c static int __ref kernel_init(void *unused) · line 1631
Quoted 9 lines
init/main.c +5
-0+ early_ts_offset = ((start_cycles * early_mult) >> early_shift) - start_ns; +pr_debug ("Early printk times: mult=%u, shift=%u, offset=%llu ns\n" , + early_mult, early_shift, early_ts_offset); +#endif +if (ramdisk_execute_command) { ret =run_init_process (ramdisk_execute_command);if (!ret)Gr{oetje,eeting}s,
Quoted 2 lines
Geert
Thanks very much for the review and suggestions. I'm away from my lab for the next week, but I'd really like to test this on a 32-bit platform that has an early cycle-counter available. I'll have to do some research. I have some 32-bit platforms in my lab, but I'm not sure which ones
have early-cycle-counter support. Do any of the 32-bit platforms you're familiar with support
early cycle-counters (that is, cycle-counters that are running when the kernel starts, and don't need any kernel initialization at all)?
-- Tim
Re: [PATCH v2] printk: fix zero-valued printk timestamps in early boot
Hi Tim,
Quoted 55 lines · Tim
Quoted 55 lines
From: Geert Uytterhoeven <[email protected]>
Quoted 21 lines · Tim Bird
During early boot, printk timestamps are reported as zero before kernel timekeeping starts (e.g. before
time_init()). This hinders boot-time optimization efforts. This period is about 400 milliseconds for many current desktop and embedded machines running Linux.Add support to save cycles during early boot, and output correct timestamp values after timekeeping is initialized.
get_cycles()is operational on arm64 andx86_64from kernel start. Add code and variables to save calibration values used to later convert cycle counts to time values in the early printks. Add a config to control the feature.This yields non-zero timestamps for printks from the very start of kernel execution. The timestamps are relative to the start of the architecture-specified counter used in
get_cycles(e.g. the TSC onx86_64andcntvct_el0on arm64).All timestamps reflect time from power-on instead of time from the kernel's timekeeping initialization.
Quoted 8 lines
Signed-off-by Tim Bird <[email protected]>---
V1 -> V2
Remove calibration CONFIG vars
Add 'depends on' to restrict arches (to handle ppc bug) Addearly_ts_offsetto avoid discontinuity Save cycles ints_nsec, and convert on output Move conditional code to include file (early_times.h)Thanks for the update!
Quoted 7 lines
include/linux/early_times.h new file +4
-0@@ -0,0 +1,48 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#ifndef _KERNEL_PRINTK_EARLY_TIMES_H +#define _KERNEL_PRINTK_EARLY_TIMES_Hinclude/linux/early_times.h line 14
Quoted 10 lines
include/linux/early_times.h new file +10
-0+ +#include <linux/timex.h> + +#if defined(CONFIG_EARLY_PRINTK_TIMES ) +extern u32 early_mult, early_shift; +extern u64 early_ts_offset; + +static inline u64 early_cycles (void ) +{ +return ((u64 )get_cycles () | (1ULL <<63 ));No need to cast to u64, as the second operand of the OR is u64 anyway.
BIT_ULL(63)
Quoted 3 lines
Quoted 1 line
I think it would be good to have a #define for this at the top.
I'll look at this. Is
BIT_ULL(63)preferred over (1ULL << 63)?
When you refer to the bit value, yes: BIT() for unsigned long, BIT_ULL() for unsigned long long; recently we got BIT_U{8,16,32,64}(), too).
Quoted 1 line
Do you think something like "
HIGH_BIT63" would be good enough?
I'd name it for what it means, not what it does, e.g. EARLY_TS_FLAG?
Quoted 18 lines
Quoted 10 lines
Quoted 10 lines
include/linux/early_times.h new file +10
-0+} + +static inline u64 adjust_early_ts (u64 ts) +{ +/* High bit means ts is a cycle count */ +if (unlikely (ts & (1ULL <<63 ))) +/* +* mask high bit and convert to ns +* Note that early_mult may be 0, but that's OK because +* we'll just multiply by 0 and return 0. This will Quoted 8 lines
include/linux/early_times.h line 30
Quoted 6 lines
include/linux/early_times.h new file +6
-0+* only occur if we're outputting a printk message +* before the calibration of the early timestamp. +* Any output after user space start (eg. from dmesg or +* journalctl) will show correct values. +*/ +return (((ts & ~(1ULL <<63 )) * early_mult) >> early_shift);Please use the
mul_u64_u32_shr()helper.
Quoted 5 lines
OK. I did not know about that.
I can check, but do you know offhand if timestamps from
local_clock()on 32-bit systems are always 64-bit nanoseconds? I assume so looking at the printk code and making some assumptions. (But that's dangerous.)
I am not 100% sure, but I think they are. Note that on systems without high-resolution timers, they may increment in large steps, e.g. by 10000000 if HZ=100.
Quoted 21 lines
Quoted 6 lines
Quoted 6 lines
init/Kconfig +0
-0@@ -777,6 +777,18 @@ config IKHEADERS or similar programs. If you build the headers as a module, a module called kheaders.ko is built which can be loaded on-demand to get access to headers.Quoted 7 lines
init/Kconfig config IKHEADERS · line 784
Quoted 5 lines
init/Kconfig +5
-0+config EARLY_PRINTK_TIMES + bool "Show non-zero printk timestamps early in boot" + default y + depends on PRINTK + depends on ARM64 || X86_64So for now this is limited to (a few) 64-bit platforms...
Yes, but it really shouldn't be. I got spooked when 0-day told me that the code wouldn't link on powerpc, so I restricted it to just machines I was actually testing. But this should work on some ARM32 and most x8632 platforms. Actually, it should work on anything that has a cycle-counter from kernel start (some RISC machine might qualify as well). However, I've seen a few cases where some platforms require kernel initialization of their cycle-counter, and I wanted to play it safe.
Quoted 2 lines
If such platforms are well-behaved and return 0 before they are initialized, it should still be safe to turn this on - it just won't have any effect.
At least on m68k, get_cycles() always returns zero ;-)
Quoted 7 lines
I plan to do some more investigation of the powerpc error. It was
powerpc-linux-ld:init/main.o: in function `kernel_init':
main.c:(.ref.text+0x144): undefined reference to `__udivdi3'from the definition of
clocks_calc_mult_shift(), which seems to indicate a bug in the powerpc code. In the long run, I should try to track down that bug rather than exclude a bunch of other (likely-working) arches. But I was playing it conservative for now.
An undefined reference to __udivdi3 means you are using a 64-by-32 division, for which you should always use the helpers from <linux/math64.h>. Even on 64-bit, the helpers may generate better code.
Quoted 11 lines
Quoted 11 lines
Quoted 10 lines
init/main.c +7
-0+ +/* set calibration data for early_printk_times */ + end_cycles =get_cycles (); + end_ns =local_clock (); +clocks_calc_mult_shift (&early_mult, &early_shift, + ((end_cycles - start_cycles) *NSEC_PER_SEC )/(end_ns - start_ns), +NSEC_PER_SEC ,50 );
Quoted 5 lines
Quoted 2 lines
mul_u64_u64_div_u64()is probably the best helper that is available (there is nomul_ulong_u32_div_u64()).What would the
mul_u64_u64_div_u64do ifcycles_tis u32? Should it sill all work, just not optimized?
It should still work.arch/x86/include/asm/div64.h uses divq, so it is up to Intel or AMD. Everything else uses lib/math/div64.c.
If need, you can add an optimized version for e.g. arm64, too.
Gr{oetje,eeting}s,
Geert
Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected] In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that. -- Linus Torvalds
PATCH v3 1/1 printk: fix zero-valued printk timestamps in early boot
In reply to Tim BirdDuring early boot, printk timestamps are reported as zero before kernel timekeeping starts (e.g. before time_init()). This hinders boot-time optimization efforts. This period is about 400 milliseconds for many current desktop and embedded machines running Linux.
Add support to save cycles during early boot, and output correct timestamp values after timekeeping is initialized. get_cycles() is operational on arm64 and x86_64 from kernel start. Add code and variables to save calibration values used to later convert cycle counts to time values in the early printks. Add a config to control the feature.
This yields non-zero timestamps for printks from the very start of kernel execution. The timestamps are relative to the start of the architecture-specified counter used in get_cycles (e.g. the TSC on x86_64 and cntvct_el0 on arm64).
All timestamps reflect time from processor power-on instead of time from the kernel's timekeeping initialization.
---V2->V3
Default CONFIG option to 'n'
Move more code from into early_times.h
(reducing ifdefs in init/main.c)
Use math64 helper routines
Use cycles_t instead of u64 type
Add #defines for EARLY_CYCLES_BIT and MASK Invert if logic in adjust_early_ts() (note: no change to 'depends on' in Kconfig entry)
V1->V2
Remove calibration CONFIG vars
Add 'depends on' to restrict arches (to handle ppc bug) Add early_ts_offset to avoid discontinuity Save cycles in ts_nsec, and convert on output Move conditional code to include file (early_times.h)
include/linux/early_times.h | 85 +++++++++++++++++++++++++++++++++++++ init/Kconfig | 14 ++++++ init/main.c | 6 +++ kernel/printk/printk.c | 18 +++++++- 4 files changed, 121 insertions(+), 2 deletions(-) create mode 100644 include/linux/early_times.h
include/linux/early_times.h new file +85 -0
@@ -0,0 +1,85 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#ifndef _KERNEL_PRINTK_EARLY_TIMES_H +#define _KERNEL_PRINTK_EARLY_TIMES_H + +#include <linux/timex.h> +#include <linux/clocksource.h> + +/* use high bit of a u64 to indicate cycles instead of a timestamp */ +#define EARLY_CYCLES_BIT BIT_ULL(63) +#define EARLY_CYCLES_MASK ~(BIT_ULL(63)) + +#if defined(CONFIG_EARLY_PRINTK_TIMES ) +extern cycles_t start_cycles; +extern u64 start_ns; +extern u32 early_mult, early_shift; +extern u64 early_ts_offset; + +static inline void early_times_start_calibration (void ) +{ + start_cycles =get_cycles (); + start_ns =local_clock (); +} + +static inline void early_times_finish_calibration (void ) +{ +cycles_t end_cycles; +u64 end_ns; + +/* set calibration data for early_printk_times */ + end_cycles =get_cycles (); + end_ns =local_clock (); +clocks_calc_mult_shift (&early_mult, &early_shift, +mul_u64_u64_div_u64 (end_cycles - start_cycles, +NSEC_PER_SEC , end_ns - start_ns), +NSEC_PER_SEC ,100 ); + early_ts_offset =mul_u64_u32_shr (start_cycles, early_mult, early_shift) - start_ns; + +pr_debug ("Early printk times: mult=%u, shift=%u, offset=%llu ns\n" , + early_mult, early_shift, early_ts_offset); +} + +static inline u64 early_cycles (void ) +{ +return (get_cycles () |EARLY_CYCLES_BIT ); +} + +/* +* adjust_early_ts detects whether ts in is cycles or nanoseconds +* and converts it or adjusts it, taking into account the offset +* from cycle-counter start. +* +* Note that early_mult may be 0, but that's OK because +* we'll just multiply by 0 and return 0. This will +* only occur if we're outputting a printk message +* before the calibration of the early timestamp. +* Any output after user space start (eg. from dmesg or +* journalctl) will show correct values. +*/ +static inline u64 adjust_early_ts (u64 ts) +{ +if (likely (!(ts &EARLY_CYCLES_BIT ))) +/* if timestamp is not in cycles, just add offset */ +return ts + early_ts_offset; + +/* mask high bit and convert to nanoseconds */ +return mul_u64_u32_shr (ts &EARLY_CYCLES_MASK , early_mult, early_shift); +} + +#else +# define early_times_start_calibration () do { } while (0) +# define early_times_finish_calibration () do { } while (0) + +static inline u64 early_cycles (void ) +{ +return 0 ; +} + +static inline u64 adjust_early_ts (u64 ts) +{ +return ts; +} +#endif /* CONFIG_EARLY_PRINTK_TIMES */ + +#endif /* _KERNEL_PRINTK_EARLY_TIMES_H */
init/Kconfig +14 -0
@@ -777,6 +777,20 @@ config IKHEADERS or similar programs. If you build the headers as a module, a module called kheaders.ko is built which can be loaded on-demand to get access to headers. +config EARLY_PRINTK_TIMES + bool "Show non-zero printk timestamps early in boot" + default n + depends on PRINTK + depends on ARM64 || X86_64 + help + Use a cycle-counter to provide printk timestamps during + early boot. This allows seeing timestamps for printks that + would otherwise show as 0. Note that this will shift the + printk timestamps to be relative to processor power on, instead + of relative to the start of kernel timekeeping. This should be + closer to machine power on, giving a better indication of + overall boot time. + config LOG_BUF_SHIFT int "Kernel log buffer size (16 => 64KB, 17 => 128KB)" range 12 25
init/main.c +6 -0
@@ -104,6 +104,7 @@#include <linux/pidfs.h> #include <linux/ptdump.h> #include <linux/time_namespace.h> +#include <linux/early_times.h> #include <net/net_namespace.h> #include <asm/io.h>
@@ -1118,6 +1119,9 @@ void start_kernel(void)timekeeping_init ();time_init (); +/* This must be after timekeeping is initialized */ +early_times_start_calibration (); +/* This must be after timekeeping is initialized */ random_init ();
@@ -1600,6 +1604,8 @@ static int __ref kernel_init(void *unused)do_sysctl_args (); +early_times_finish_calibration (); +if (ramdisk_execute_command) { ret =run_init_process (ramdisk_execute_command);if (!ret)
kernel/printk/printk.c +16 -2
@@ -46,6 +46,7 @@#include <linux/ctype.h> #include <linux/uio.h> #include <linux/sched/clock.h> +#include <linux/early_times.h> #include <linux/sched/debug.h> #include <linux/sched/task_stack.h> #include <linux/panic.h>
@@ -75,6 +76,13 @@ EXPORT_SYMBOL(ignore_console_lock_warning);EXPORT_TRACEPOINT_SYMBOL_GPL (console); +#ifdef CONFIG_EARLY_PRINTK_TIMES +cycles_t start_cycles; +u64 start_ns; +u32 early_mult, early_shift; +u64 early_ts_offset; +#endif +/* * Low level drivers may need that to know if they can schedule in * their unblank() callback or not. So let's export it.
@@ -639,7 +647,7 @@ static void append_char(char **pp, char *e, char c)static ssize_t info_print_ext_header (char *buf,size_t size,struct printk_info *info) { -u64 ts_usec = info->ts_nsec; +u64 ts_usec =adjust_early_ts (info->ts_nsec);char caller[20 ];#ifdef CONFIG_PRINTK_CALLER u32 id = info->caller_id;
@@ -1352,7 +1360,11 @@ static size_t print_syslog(unsigned int level, char *buf)static size_t print_time (u64 ts,char *buf) { -unsigned long rem_nsec =do_div (ts,1000000000 ); +unsigned long rem_nsec; + + ts =adjust_early_ts (ts); + + rem_nsec =do_div (ts,1000000000 );return sprintf (buf,"[%5lu.%06lu]" , (unsigned long )ts, rem_nsec /1000 );
@@ -2242,6 +2254,8 @@ int vprintk_store(int facility, int level,* timestamp with respect to the caller. */ ts_nsec =local_clock (); +if (!ts_nsec) + ts_nsec =early_cycles (); caller_id =printk_caller_id ();
Signature · 1 line
2.43.0
Re: [PATCH v3] printk: fix zero-valued printk timestamps in early boot
Quoted 18 lines · Tim Bird
During early boot, printk timestamps are reported as zero before kernel timekeeping starts (e.g. before
time_init()). This hinders boot-time optimization efforts. This period is about 400 milliseconds for many current desktop and embedded machines running Linux.Add support to save cycles during early boot, and output correct timestamp values after timekeeping is initialized.
get_cycles()is operational on arm64 andx86_64from kernel start. Add code and variables to save calibration values used to later convert cycle counts to time values in the early printks. Add a config to control the feature.This yields non-zero timestamps for printks from the very start of kernel execution. The timestamps are relative to the start of the architecture-specified counter used in
get_cycles(e.g. the TSC onx86_64andcntvct_el0on arm64).
Quoted 4 lines
All timestamps reflect time from processor power-on instead of time from the kernel's timekeeping initialization.
Signed-off-by Tim Bird <[email protected]>
It looks good to me and seems to work fine. Feel free to use:
See a note below.
init/main.c line 107
Quoted 7 lines
init/main.c +1
-0@@ -104,6 +104,7 @@#include <linux/pidfs.h> #include <linux/ptdump.h> #include <linux/time_namespace.h> +#include <linux/early_times.h>
JFYI, I have tried this patch on top of the current Linus' tree (v7.0-rc2+) and it conflicted with the commit 499f86de4f8c34e19 ("init/main: read bootconfig header with get_unaligned_le32()") which added here:
#include <linux/unaligned.h>
init/main.c line 110
Quoted 3 lines
init/main.c +0
-0#include <net/net_namespace.h> #include <asm/io.h>
Best Regards,
Petr
Re: [PATCH v3] printk: fix zero-valued printk timestamps in early boot
In reply to Tim BirdHi Tim,
...on top of rc3 on an AMD Ryzen 7 4800H laptop. This patch conflicts with these commits with trivial fixes:
032a730268a3 init/main.c: wrap long kernel cmdline when printing to logs 60325c27d3cf printk: Add execution context (task name/CPU) to printk_info 499f86de4f8c init/main: read bootconfig header with get_unaligned_le32()
Comment below.
Quoted 1 line · Tim Bird
During early boot, printk timestamps are reported as zero before
<snip>
Quoted 49 lines
kernel/printk/printk.c +14
-2@@ -46,6 +46,7 @@#include <linux/ctype.h> #include <linux/uio.h> #include <linux/sched/clock.h> +#include <linux/early_times.h> #include <linux/sched/debug.h> #include <linux/sched/task_stack.h> #include <linux/panic.h> @@ -75,6 +76,13 @@ EXPORT_SYMBOL(ignore_console_lock_warning);EXPORT_TRACEPOINT_SYMBOL_GPL (console); +#ifdef CONFIG_EARLY_PRINTK_TIMES +cycles_t start_cycles; +u64 start_ns; +u32 early_mult, early_shift; +u64 early_ts_offset; +#endif +/* * Low level drivers may need that to know if they can schedule in * their unblank() callback or not. So let's export it. @@ -639,7 +647,7 @@ static void append_char(char **pp, char *e, char c)static ssize_t info_print_ext_header (char *buf,size_t size,struct printk_info *info) { -u64 ts_usec = info->ts_nsec; +u64 ts_usec =adjust_early_ts (info->ts_nsec);char caller[20 ];#ifdef CONFIG_PRINTK_CALLER u32 id = info->caller_id;@@ -1352,7 +1360,11 @@ static size_t print_syslog(unsigned int level, char *buf)static size_t print_time (u64 ts,char *buf) { -unsigned long rem_nsec =do_div (ts,1000000000 ); +unsigned long rem_nsec; + + ts =adjust_early_ts (ts); + + rem_nsec =do_div (ts,1000000000 );return sprintf (buf,"[%5lu.%06lu]" , (unsigned long )ts, rem_nsec /1000 );
kernel/printk/printk.c int vprintk_store(int facility, int level, · line 2258
Quoted 5 lines
kernel/printk/printk.c +2
-0@@ -2242,6 +2254,8 @@ int vprintk_store(int facility, int level,* timestamp with respect to the caller. */ ts_nsec =local_clock (); +if (!ts_nsec) + ts_nsec =early_cycles ();
ts_nsec goes on to be stored in a struct printk_info's ts_nsec which is documented to be "timestamp in nanoseconds":
/*
* Meta information about each stored message.
*
* All fields are set by the printk code except for @seq, which is
* set by the ringbuffer code.
*/
struct printk_info {
u64 seq; /* sequence number */
u64 ts_nsec; /* timestamp in nanoseconds */
u16 text_len; /* length of text message */
u8 facility; /* syslog facility */
u8 flags:5; /* internal record flags */
u8 level:3; /* syslog level */
u32 caller_id; /* thread id or processor id */
#ifdef CONFIG_PRINTK_EXECUTION_CTX
u32 caller_id2; /* caller_id complement */
/* name of the task that generated the message */
char comm[TASK_COMM_LEN];
#endif
struct dev_printk_info dev_info;
};Since with this patch, ts_nsec can either be a timestamp in ns or a cycle count, the comment should be updated. Ideally, I'd like the member name to be changed as well to reflect the new semantic. I'm thinking ts_raw or ts_ns_or_cyc... naming is hard :)
Thanks,
Shashank
Petr Mladek
maintainerRe: [PATCH v3] printk: fix zero-valued printk timestamps in early boot
Quoted 10 lines · Shashank Balaji
Hi Tim,
Tested-by Shashank Balaji <[email protected]>...on top of rc3 on an AMD Ryzen 7 4800H laptop. This patch conflicts with these commits with trivial fixes:
032a730268a3init/main.c: wrap long kernel cmdline when printing to logs 60325c27d3cfq printk: Add execution context (task name/CPU) toprintk_info499f86de4f8cinit/main: read bootconfig header withget_unaligned_le32()
Good to know.
Quoted 40 lines · Tim Bird
Quoted 1 line
During early boot, printk timestamps are reported as zero before
<snip>kernel/printk/printk.c int vprintk_store(int facility, int level, · line 2258
Quoted 10 lines
kernel/printk/printk.c +2
-0@@ -2242,6 +2254,8 @@ int vprintk_store(int facility, int level,* timestamp with respect to the caller. */ ts_nsec =local_clock (); +if (!ts_nsec) + ts_nsec =early_cycles ();
ts_nsecgoes on to be stored in a struct printk_info'sts_nsecwhich is documented to be "timestamp in nanoseconds":/* * Meta information about each stored message. * * All fields are set by the printk code except for @seq, which is * set by the ringbuffer code. */ struct printk_info { u64 seq; /* sequence number */ u64 ts_nsec; /* timestamp in nanoseconds */ u16 text_len; /* length of text message */ u8 facility; /* syslog facility */ u8 flags:5; /* internal record flags */ u8 level:3; /* syslog level */ u32 caller_id; /* thread id or processor id */ #ifdef CONFIG_PRINTK_EXECUTION_CTX u32 caller_id2; /* caller_id complement */ /* name of the task that generated the message */ char comm[TASK_COMM_LEN]; #endif struct dev_printk_info dev_info; };
Quoted 2 lines
Since with this patch,
ts_nseccan either be a timestamp in ns or a cycle count, the comment should be updated.
Yup, great catch!
Quoted 3 lines
Ideally, I'd like the member
name to be changed as well to reflect the new semantic. I'm thinkingts_raworts_ns_or_cyc... naming is hard :)
Hmm, we could not change it easily because it would break user space tools for reading kernel crash dump.
Alternative solution would be usign an union.
union {
u64 ts_nsec;
u64 ts_cycles;
};Best Regards,
Petr
RE: [PATCH v3] printk: fix zero-valued printk timestamps in early boot
In reply to Shashank BalajiQuoted 7 lines
-----Original Message-----
From: Shashank Balaji <[email protected]> Hi Tim,Tested-by Shashank Balaji <[email protected]>
Thanks for the testing!!
Quoted 94 lines
...on top of rc3 on an AMD Ryzen 7 4800H laptop. This patch conflicts with these commits with trivial fixes:
032a730268a3init/main.c: wrap long kernel cmdline when printing to logs60325c27d3cfprintk: Add execution context (task name/CPU) toprintk_info499f86de4f8cinit/main: read bootconfig header withget_unaligned_le32()Comment below.
Quoted 1 line · Tim Bird
During early boot, printk timestamps are reported as zero before
<snip>Quoted 49 lines
kernel/printk/printk.c +14
-2@@ -46,6 +46,7 @@#include <linux/ctype.h> #include <linux/uio.h> #include <linux/sched/clock.h> +#include <linux/early_times.h> #include <linux/sched/debug.h> #include <linux/sched/task_stack.h> #include <linux/panic.h> @@ -75,6 +76,13 @@ EXPORT_SYMBOL(ignore_console_lock_warning);EXPORT_TRACEPOINT_SYMBOL_GPL (console); +#ifdef CONFIG_EARLY_PRINTK_TIMES +cycles_t start_cycles; +u64 start_ns; +u32 early_mult, early_shift; +u64 early_ts_offset; +#endif +/* * Low level drivers may need that to know if they can schedule in * their unblank() callback or not. So let's export it. @@ -639,7 +647,7 @@ static void append_char(char **pp, char *e, char c)static ssize_t info_print_ext_header (char *buf,size_t size,struct printk_info *info) { -u64 ts_usec = info->ts_nsec; +u64 ts_usec =adjust_early_ts (info->ts_nsec);char caller[20 ];#ifdef CONFIG_PRINTK_CALLER u32 id = info->caller_id;@@ -1352,7 +1360,11 @@ static size_t print_syslog(unsigned int level, char *buf)static size_t print_time (u64 ts,char *buf) { -unsigned long rem_nsec =do_div (ts,1000000000 ); +unsigned long rem_nsec; + + ts =adjust_early_ts (ts); + + rem_nsec =do_div (ts,1000000000 );return sprintf (buf,"[%5lu.%06lu]" , (unsigned long )ts, rem_nsec /1000 );kernel/printk/printk.c int vprintk_store(int facility, int level, · line 2258
Quoted 5 lines
kernel/printk/printk.c +2
-0@@ -2242,6 +2254,8 @@ int vprintk_store(int facility, int level,* timestamp with respect to the caller. */ ts_nsec =local_clock (); +if (!ts_nsec) + ts_nsec =early_cycles ();
ts_nsecgoes on to be stored in a struct printk_info'sts_nsecwhich is documented to be "timestamp in nanoseconds":/* * Meta information about each stored message. * * All fields are set by the printk code except for @seq, which is * set by the ringbuffer code. */ struct printk_info { u64 seq; /* sequence number */ u64 ts_nsec; /* timestamp in nanoseconds */ u16 text_len; /* length of text message */ u8 facility; /* syslog facility */ u8 flags:5; /* internal record flags */ u8 level:3; /* syslog level */ u32 caller_id; /* thread id or processor id */ #ifdef CONFIG_PRINTK_EXECUTION_CTX u32 caller_id2; /* caller_id complement */ /* name of the task that generated the message */ char comm[TASK_COMM_LEN]; #endif struct dev_printk_info dev_info; };
Quoted 4 lines
Since with this patch,
ts_nseccan either be a timestamp in ns or a cycle count, the comment should be updated. Ideally, I'd like the member name to be changed as well to reflect the new semantic. I'm thinkingts_raworts_ns_or_cyc... naming is hard :)
Nice catch! I'm considering either changing the comment, or using a union here (or maybe both).
Thanks,
-- Tim
Re: [PATCH v3] printk: fix zero-valued printk timestamps in early boot
In reply to Tim BirdHi again,
Quoted 18 lines · Tim Bird
During early boot, printk timestamps are reported as zero before kernel timekeeping starts (e.g. before
time_init()). This hinders boot-time optimization efforts. This period is about 400 milliseconds for many current desktop and embedded machines running Linux.Add support to save cycles during early boot, and output correct timestamp values after timekeeping is initialized.
get_cycles()is operational on arm64 andx86_64from kernel start. Add code and variables to save calibration values used to later convert cycle counts to time values in the early printks. Add a config to control the feature.This yields non-zero timestamps for printks from the very start of kernel execution. The timestamps are relative to the start of the architecture-specified counter used in
get_cycles(e.g. the TSC onx86_64andcntvct_el0on arm64).
Quoted 4 lines
All timestamps reflect time from processor power-on instead of time from the kernel's timekeeping initialization.
Signed-off-by Tim Bird <[email protected]>
So if a console is read before the cycles -> timestamp conversion can happen, then they'll see 0. But reading from userspace will give the right timestamps.
Based on the previous discussions, to address this possible confusion, if changing the timestamp format, like adding '?', is a no-go because of concerns of breaking existing monitoring tools, what about appending something to the printk string after the timestamp? Hmm, no, that'll affect grep'ability _and_ may break monitoring tools. Or what about a pr_warn() early in boot to warn about the possible timestamp difference?
At the very least the possibility of this difference should be documented in the Kconfig description.
Continuing below...
Quoted 199 lines
---
V2->V3
Default CONFIG option to 'n'
Move more code from intoearly_times.h
(reducing ifdefs ininit/main.c)
Use math64 helper routines
Usecycles_tinstead of u64 type
Add #defines forEARLY_CYCLES_BITand MASK Invert if logic inadjust_early_ts()(note: no change to 'depends on' inKconfigentry)
V1->V2
Remove calibration CONFIG vars
Add 'depends on' to restrict arches (to handle ppc bug) Addearly_ts_offsetto avoid discontinuity Save cycles ints_nsec, and convert on output Move conditional code to include file (early_times.h)include/linux/early_times.h | 85 +++++++++++++++++++++++++++++++++++++ init/Kconfig | 14 ++++++ init/main.c | 6 +++ kernel/printk/printk.c | 18 +++++++- 4 files changed, 121 insertions(+), 2 deletions(-) create mode 100644 include/linux/early_times.hinclude/linux/early_times.h new file +85
-0@@ -0,0 +1,85 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#ifndef _KERNEL_PRINTK_EARLY_TIMES_H +#define _KERNEL_PRINTK_EARLY_TIMES_H + +#include <linux/timex.h> +#include <linux/clocksource.h> + +/* use high bit of a u64 to indicate cycles instead of a timestamp */ +#define EARLY_CYCLES_BIT BIT_ULL(63) +#define EARLY_CYCLES_MASK ~(BIT_ULL(63)) + +#if defined(CONFIG_EARLY_PRINTK_TIMES ) +extern cycles_t start_cycles; +extern u64 start_ns; +extern u32 early_mult, early_shift; +extern u64 early_ts_offset; + +static inline void early_times_start_calibration (void ) +{ + start_cycles =get_cycles (); + start_ns =local_clock (); +} + +static inline void early_times_finish_calibration (void ) +{ +cycles_t end_cycles; +u64 end_ns; + +/* set calibration data for early_printk_times */ + end_cycles =get_cycles (); + end_ns =local_clock (); +clocks_calc_mult_shift (&early_mult, &early_shift, +mul_u64_u64_div_u64 (end_cycles - start_cycles, +NSEC_PER_SEC , end_ns - start_ns), +NSEC_PER_SEC ,100 ); + early_ts_offset =mul_u64_u32_shr (start_cycles, early_mult, early_shift) - start_ns; + +pr_debug ("Early printk times: mult=%u, shift=%u, offset=%llu ns\n" , + early_mult, early_shift, early_ts_offset); +} + +static inline u64 early_cycles (void ) +{ +return (get_cycles () |EARLY_CYCLES_BIT ); +} + +/* +* adjust_early_ts detects whether ts in is cycles or nanoseconds +* and converts it or adjusts it, taking into account the offset +* from cycle-counter start. +* +* Note that early_mult may be 0, but that's OK because +* we'll just multiply by 0 and return 0. This will +* only occur if we're outputting a printk message +* before the calibration of the early timestamp. +* Any output after user space start (eg. from dmesg or +* journalctl) will show correct values. +*/ +static inline u64 adjust_early_ts (u64 ts) +{ +if (likely (!(ts &EARLY_CYCLES_BIT ))) +/* if timestamp is not in cycles, just add offset */ +return ts + early_ts_offset; + +/* mask high bit and convert to nanoseconds */ +return mul_u64_u32_shr (ts &EARLY_CYCLES_MASK , early_mult, early_shift); +} + +#else +# define early_times_start_calibration () do { } while (0) +# define early_times_finish_calibration () do { } while (0) + +static inline u64 early_cycles (void ) +{ +return 0 ; +} + +static inline u64 adjust_early_ts (u64 ts) +{ +return ts; +} +#endif /* CONFIG_EARLY_PRINTK_TIMES */ + +#endiV init/Kconfig +14
-0@@ -777,6 +777,20 @@ config IKHEADERS or similar programs. If you build the headers as a module, a module called kheaders.ko is built which can be loaded on-demand to get access to headers. +config EARLY_PRINTK_TIMES + bool "Show non-zero printk timestamps early in boot" + default n + depends on PRINTK + depends on ARM64 || X86_64 + help + Use a cycle-counter to provide printk timestamps during + early boot. This allows seeing timestamps for printks that + would otherwise show as 0. Note that this will shift the + printk timestamps to be relative to processor power on, instead + of relative to the start of kernel timekeeping. This should be + closer to machine power on, giving a better indication of + overall boot time. + config LOG_BUF_SHIFT int "Kernel log buffer size (16 => 64KB, 17 => 128KB)" range 12 25init/main.c +6
-0@@ -104,6 +104,7 @@#include <linux/pidfs.h> #include <linux/ptdump.h> #include <linux/time_namespace.h> +#include <linux/early_times.h> #include <net/net_namespace.h> #include <asm/io.h> @@ -1118,6 +1119,9 @@ void start_kernel(void)timekeeping_init ();time_init (); +/* This must be after timekeeping is initialized */ +early_times_start_calibration (); +/* This must be after timekeeping is initialized */ random_init ();@@ -1600,6 +1604,8 @@ static int __ref kernel_init(void *unused)do_sysctl_args (); +early_times_finish_calibration (); +if (ramdisk_execute_command) { ret =run_init_process (ramdisk_execute_command);if (!ret)kernel/printk/printk.c +8
-0@@ -46,6 +46,7 @@#include <linux/ctype.h> #include <linux/uio.h> #include <linux/sched/clock.h> +#include <linux/early_times.h> #include <linux/sched/debug.h> #include <linux/sched/task_stack.h> #include <linux/panic.h> @@ -75,6 +76,13 @@ EXPORT_SYMBOL(ignore_console_lock_warning);EXPORT_TRACEPOINT_SYMBOL_GPL (console); +#ifdef CONFIG_EARLY_PRINTK_TIMES +cycles_t start_cycles; +u64 start_ns; +u32 early_mult, early_shift; +u64 early_ts_offset; +#endif +/* * Low level drivers may need that to know if they can schedule in * their unblank() callback or not. So let's export it.
kernel/printk/printk.c static void append_char(char **pp, char *e, char c) · line 650
Quoted 5 lines
kernel/printk/printk.c +1
-1@@ -639,7 +647,7 @@ static void append_char(char **pp, char *e, char c)static ssize_t info_print_ext_header (char *buf,size_t size,struct printk_info *info) { -u64 ts_usec = info->ts_nsec; +u64 ts_usec =adjust_early_ts (info->ts_nsec);
printk_get_next_message() calls info_print_ext_header() for an extended console (/dev/kmsg and netcon_ext use this), whereas for non-extended consoles, record_print_text() -> info_print_prefix() -> print_time() is called. So, this adjustment should be made in print_time() too, otherwise non-extended console users are gonna be spooked with insane timestamps. This may explain the non-zero early timestamps Petr saw in his serial console output [1].
An accessor can be implemented for (struct printk_info).ts_nsec, say get_timestamp(), which can be called from both the places.
[1] https://lore.kernel.org/all/aYDPn2EJgJIWGDhM@pathway/
Thanks,
Shashank
Petr Mladek
maintainerRe: [PATCH v3] printk: fix zero-valued printk timestamps in early boot
Quoted 30 lines · Shashank Balaji
Hi again,
Quoted 18 lines · Tim Bird
During early boot, printk timestamps are reported as zero before kernel timekeeping starts (e.g. before
time_init()). This hinders boot-time optimization efforts. This period is about 400 milliseconds for many current desktop and embedded machines running Linux.Add support to save cycles during early boot, and output correct timestamp values after timekeeping is initialized.
get_cycles()is operational on arm64 andx86_64from kernel start. Add code and variables to save calibration values used to later convert cycle counts to time values in the early printks. Add a config to control the feature.This yields non-zero timestamps for printks from the very start of kernel execution. The timestamps are relative to the start of the architecture-specified counter used in
get_cycles(e.g. the TSC onx86_64andcntvct_el0on arm64).Quoted 4 lines
All timestamps reflect time from processor power-on instead of time from the kernel's timekeeping initialization.
Signed-off-by Tim Bird <[email protected]>So if a console is read before the cycles -> timestamp conversion can happen, then they'll see 0. But reading from userspace will give the right timestamps.
Quoted 6 lines
Based on the previous discussions, to address this possible confusion, if changing the timestamp format, like adding '?', is a no-go because of concerns of breaking existing monitoring tools, what about appending something to the printk string after the timestamp? Hmm, no, that'll affect grep'ability _and_ may break monitoring tools. Or what about a
pr_warn()early in boot to warn about the possible timestamp difference?
Or we could make it more obvious from the message in early_times_finish_calibration(), see below.
Quoted 2 lines
At the very least the possibility of this difference should be documented in the
Kconfigdescription.
Yeah, it would be nice to mention this in the Kconfig description.
Quoted 11 lines
Quoted 11 lines
include/linux/early_times.h new file +8
-0@@ -0,0 +1,85 @@ +static inline void early_times_finish_calibration (void ) +{ +cycles_t end_cycles; +u64 end_ns; + +/* set calibration data for early_printk_times */ + end_cycles =get_cycles (); + end_ns =local_clock ();
Quoted 8 lines
include/linux/early_times.h line 40
Quoted 8 lines
include/linux/early_times.h new file +8
-0+clocks_calc_mult_shift (&early_mult, &early_shift, +mul_u64_u64_div_u64 (end_cycles - start_cycles, +NSEC_PER_SEC , end_ns - start_ns), +NSEC_PER_SEC ,100 ); + early_ts_offset =mul_u64_u32_shr (start_cycles, early_mult, early_shift) - start_ns; + +pr_debug ("Early printk times: mult=%u, shift=%u, offset=%llu ns\n" , + early_mult, early_shift, early_ts_offset);
We might make it more obvious that an offset will get added to the existing timestamp since this point.
Also it has a "surprising" user visible effect so that it should be pr_info() instead of pr_debug(). Note pr_debug() messages might be hidden.
A minimalist change would be:
pr_info("Calibrated offset for early printk times: mult=%u, shift=%u, offset=%llu ns\n",
early_mult, early_shift, early_ts_offset);And/Or we might add one more line:
pr_info("The time offset is added for existing and newly added printk messages since now!");Quoted 9 lines
Quoted 9 lines
include/linux/early_times.h new file +2
-0+} +kernel/printk/printk.c +0
-1@@ -639,7 +647,7 @@ static void append_char(char **pp, char *e, char c)static ssize_t info_print_ext_header (char *buf,size_t size,struct printk_info *info) { -u64 ts_usec = info->ts_nsec;
Quoted 8 lines
kernel/printk/printk.c static void append_char(char **pp, char *e, char c) · line 650
Quoted 1 line
kernel/printk/printk.c +1
-0+u64 ts_usec =adjust_early_ts (info->ts_nsec);
printk_get_next_message()callsinfo_print_ext_header()for an extended console (/dev/kmsgandnetcon_extuse this), whereas for non-extended consoles,record_print_text()->info_print_prefix()->print_time()is called. So, this adjustment should be made inprint_time()too, otherwise non-extended console users are gonna be spooked with insane timestamps.
The v3 patch already modifies print_time().
Quoted 2 lines
This may explain the non-zero early timestamps Petr saw in his serial console output [1].
I am a bit confused now. There are three stages:
1. Early messages where the cycles are stored.
The serial console shows zero time stamp because
it reads the messages _before the calibration_, e.g.
[ 0.000000] Linux version 6.19.0-rc7-default+ (pmladek@pathway) (gcc (SUSE Linux) 15.2.1 20251006, GNU ld (GNU Binutils; openSUSE Tumbleweed) 2.45.0.20251103-2) #521 SMP PREEMPT_DYNAMIC Mon Feb 2 16:36:53 CET 2026
[ 0.000000] Command line: BOOT_IMAGE=/boot/vmlinuz-6.19.0-rc7-default+ root=UUID=587ae802-e330-4059-9b48-d5b845e1075a resume=/dev/disk/by-uuid/369c7453-3d16-409d-88b2-5de027891a12 mitigations=auto nosplash earlycon=uart8250,io,0x3f8,115200 console=ttyS0,115200 console=ttynull console=tty0 debug_non_panic_cpus=1 panic=10 ignore_loglevel log_buf_len=1M
[ 0.000000] BIOS-provided physical RAM map:
[ 0.000000] BIOS-e820: [mem 0x0000000000000000-0x000000000009fbff] usable
[ 0.000000] BIOS-e820: [mem 0x000000000009fc00-0x000000000009ffff] reserved
But "dmesg" shows some numbers because it reads the messages
_after the calibration_:
[ 8.853613] Linux version 6.19.0-rc7-default+ (pmladek@pathway) (gcc (SUSE Linux) 15.2.1 20251006, GNU ld (GNU Binutils; openSUSE Tumbleweed) 2.45.0.20251103-2) #521 SMP PREEMPT_DYNAMIC Mon Feb 2 16:36:53 CET 2026
[ 8.853617] Command line: BOOT_IMAGE=/boot/vmlinuz-6.19.0-rc7-default+ root=UUID=587ae802-e330-4059-9b48-d5b845e1075a resume=/dev/disk/by-uuid/369c7453-3d16-409d-88b2-5de027891a12 mitigations=auto nosplash earlycon=uart8250,io,0x3f8,115200 console=ttyS0,115200 console=ttynull console=tty0 debug_non_panic_cpus=1 panic=10 ignore_loglevel log_buf_len=1M
[ 8.865086] BIOS-provided physical RAM map:
[ 8.865087] BIOS-e820: [mem 0x0000000000000000-0x000000000009fbff] usable
[ 8.865089] BIOS-e820: [mem 0x000000000009fc00-0x000000000009ffff] reserved 2. Early messages added _after the timekeeping_ is initialized
but _before the early cycles calibration_.
They serial console prints them _without the offset_ because
it reads them _before the calibration_, e.g.
[ 3.288049][ T1] Write protecting the kernel read-only data: 36864k
[ 3.298554][ T1] Freeing unused kernel image (text/rodata gap) memory: 1656K
[ 3.318942][ T1] Freeing unused kernel image (rodata/data gap) memory: 1540K
But "dmesg" prints them _with the offset_ because it reads them
_after the calibration_, e.g.
[ 12.179999] [ T1] Write protecting the kernel read-only data: 36864k
[ 12.190505] [ T1] Freeing unused kernel image (text/rodata gap) memory: 1656K
[ 12.210893] [ T1] Freeing unused kernel image (rodata/data gap) memory: 1540K3. Messages added after the calibration of the early cycles.
They are printed with the offset by both serial console and
dmesg, e.g.
[ 12.230014][ T1] Early printk times: mult=38775352, shift=27, offset=8891950261 ns
[ 12.246008][ T1] Run /init as init process
[ 12.254944][ T1] with arguments:
[ 12.264341][ T1] /initQuoted 2 lines
An accessor can be implemented for (struct printk_info).ts_nsec, say
get_timestamp(), which can be called from both the places.
Yeah, a helper function for reading the timestamp might be a cleaner solution.
Quoted 1 line
Everything seems to be as expected there. The non-zero timestamps on the serial console are from messages added after the timekeeping was initialized.
Best Regards,
Petr
RE: [PATCH v3] printk: fix zero-valued printk timestamps in early boot
Thank you both Petr and Shashank for the review and feedback.
See my responses inline below.
Quoted 43 lines
-----Original Message-----
From: Petr Mladek <[email protected]>On Tue 2026-03-10 04: 25: 33, Shashank Balaji wrote: > Hi again, > > On Tue, Feb 10, 2026 at 04: 47: 41PM -0700, Tim Bird wrote: > > During early boot, printk timestamps are reported as zero before > > kernel timekeeping starts
Quoted 30 lines · Shashank Balaji
Hi again,
Quoted 18 lines · Tim Bird
During early boot, printk timestamps are reported as zero before kernel timekeeping starts (e.g. before
time_init()). This hinders boot-time optimization efforts. This period is about 400 milliseconds for many current desktop and embedded machines running Linux.Add support to save cycles during early boot, and output correct timestamp values after timekeeping is initialized.
get_cycles()is operational on arm64 andx86_64from kernel start. Add code and variables to save calibration values used to later convert cycle counts to time values in the early printks. Add a config to control the feature.This yields non-zero timestamps for printks from the very start of kernel execution. The timestamps are relative to the start of the architecture-specified counter used in
get_cycles(e.g. the TSC onx86_64andcntvct_el0on arm64).Quoted 4 lines
All timestamps reflect time from processor power-on instead of time from the kernel's timekeeping initialization.
Signed-off-by Tim Bird <[email protected]>So if a console is read before the cycles -> timestamp conversion can happen, then they'll see 0. But reading from userspace will give the right timestamps.
Quoted 6 lines
Based on the previous discussions, to address this possible confusion, if changing the timestamp format, like adding '?', is a no-go because of concerns of breaking existing monitoring tools, what about appending something to the printk string after the timestamp? Hmm, no, that'll affect grep'ability _and_ may break monitoring tools. Or what about a
pr_warn()early in boot to warn about the possible timestamp difference?
Quoted 7 lines
Or we could make it more obvious from the message in
early_times_finish_calibration(), see below.Quoted 2 lines
At the very least the possibility of this difference should be documented in the
Kconfigdescription.Yeah, it would be nice to mention this in the
Kconfigdescription.
I'll work on a v4 version of the patch, and add some text to mention this.
Quoted 24 lines
Quoted 12 lines
Quoted 12 lines
include/linux/early_times.h new file +8
-0@@ -0,0 +1,85 @@ +static inline void early_times_finish_calibration (void ) +{ +cycles_t end_cycles; +u64 end_ns; + +/* set calibration data for early_printk_times */ + end_cycles =get_cycles (); + end_ns =local_clock ();Quoted 8 lines
include/linux/early_times.h line 40
Quoted 8 lines
include/linux/early_times.h new file +8
-0+clocks_calc_mult_shift (&early_mult, &early_shift, +mul_u64_u64_div_u64 (end_cycles - start_cycles, +NSEC_PER_SEC , end_ns - start_ns), +NSEC_PER_SEC ,100 ); + early_ts_offset =mul_u64_u32_shr (start_cycles, early_mult, early_shift) - start_ns; + +pr_debug ("Early printk times: mult=%u, shift=%u, offset=%llu ns\n" , + early_mult, early_shift, early_ts_offset);We might make it more obvious that an offset will get added to the existing timestamp since this point.
Quoted 7 lines
Also it has a "surprising" user visible effect so that it should be
pr_info()instead ofpr_debug(). Notepr_debug()messages might be hidden.A minimalist change would be:
pr_info("Calibrated offset for early printk times: mult=%u, shift=%u, offset=%llu ns\n", early_mult, early_shift, early_ts_offset);
This sounds like a good idea. Will do.
Quoted 3 lines
And/Or we might add one more line:
pr_info("The time offset is added for existing and newly added printk messages since now!");
I think adding a message is a possibility, to explain the offset.
Let me consider the wordings for this. Using 'now' is a bit ambiguous, because the offset is always used in post-boot output, such as from dmesg or journalctl, even for messages earlier in the log.
Quoted 75 lines
Quoted 10 lines
Quoted 10 lines
include/linux/early_times.h new file +2
-0+} +kernel/printk/printk.c +0
-1@@ -639,7 +647,7 @@ static void append_char(char **pp, char *e, char c)static ssize_t info_print_ext_header (char *buf,size_t size,struct printk_info *info) { -u64 ts_usec = info->ts_nsec;Quoted 8 lines
kernel/printk/printk.c static void append_char(char **pp, char *e, char c) · line 650
Quoted 1 line
kernel/printk/printk.c +1
-0+u64 ts_usec =adjust_early_ts (info->ts_nsec);
printk_get_next_message()callsinfo_print_ext_header()for an extended console (/dev/kmsgandnetcon_extuse this), whereas for non-extended consoles,record_print_text()->info_print_prefix()->print_time()is called. So, this adjustment should be made inprint_time()too, otherwise non-extended console users are gonna be spooked with insane timestamps.The v3 patch already modifies
print_time().Quoted 2 lines
This may explain the non-zero early timestamps Petr saw in his serial console output [1].
I am a bit confused now. There are three stages:
1. Early messages where the cycles are stored.
The serial console shows zero time stamp because it reads the messages
_beforethecalibration_, e.g.[ 0.000000] Linux version 6.19.0-rc7-default+ (pmladek@pathway) (gcc (SUSE Linux) 15.2.1 20251006, GNU ld (GNU Binutils;
openSUSE Tumbleweed) 2.45.0.20251103-2) #521 SMPPREEMPT_DYNAMICMon Feb 2 16:36:53 CET 2026[ 0.000000] Command line: BOOT_IMAGE=/boot/vmlinuz-6.19.0-rc7-default+ root=UUID=587ae802-e330-4059-9b48- d5b845e1075a resume=/dev/disk/by-uuid/369c7453-3d16-409d-88b2-5de027891a12 mitigations=auto nosplash earlycon=uart8250,io,0x3f8,115200 console=ttyS0,115200 console=ttynull console=tty0 debug_non_panic_cpus=1 panic=10 ignore_loglevel log_buf_len=1M [ 0.000000] BIOS-provided physical RAM map: [ 0.000000] BIOS-e820: [mem 0x0000000000000000-0x000000000009fbff] usable [ 0.000000] BIOS-e820: [mem 0x000000000009fc00-0x000000000009ffff] reservedBut "dmesg" shows some numbers because it reads the messages
_afterthecalibration_:[ 8.853613] Linux version 6.19.0-rc7-default+ (pmladek@pathway) (gcc (SUSE Linux) 15.2.1 20251006, GNU ld (GNU Binutils;
openSUSE Tumbleweed) 2.45.0.20251103-2) #521 SMPPREEMPT_DYNAMICMon Feb 2 16:36:53 CET 2026[ 8.853617] Command line: BOOT_IMAGE=/boot/vmlinuz-6.19.0-rc7-default+ root=UUID=587ae802-e330-4059-9b48- d5b845e1075a resume=/dev/disk/by-uuid/369c7453-3d16-409d-88b2-5de027891a12 mitigations=auto nosplash earlycon=uart8250,io,0x3f8,115200 console=ttyS0,115200 console=ttynull console=tty0 debug_non_panic_cpus=1 panic=10 ignore_loglevel log_buf_len=1M [ 8.865086] BIOS-provided physical RAM map: [ 8.865087] BIOS-e820: [mem 0x0000000000000000-0x000000000009fbff] usable [ 8.865089] BIOS-e820: [mem 0x000000000009fc00-0x000000000009ffff] reserved2. Early messages added _after the timekeeping_ is initialized but _before the early cycles calibration_. They serial console prints them _without the offset_ because it reads them _before the calibration_, e.g. [ 3.288049][ T1] Write protecting the kernel read-only data: 36864k [ 3.298554][ T1] Freeing unused kernel image (text/rodata gap) memory: 1656K [ 3.318942][ T1] Freeing unused kernel image (rodata/data gap) memory: 1540K But "dmesg" prints them _with the offset_ because it reads them _after the calibration_, e.g. [ 12.179999] [ T1] Write protecting the kernel read-only data: 36864k [ 12.190505] [ T1] Freeing unused kernel image (text/rodata gap) memory: 1656K [ 12.210893] [ T1] Freeing unused kernel image (rodata/data gap) memory: 1540K 3. Messages added after the calibration of the early cycles.
Quoted 7 lines
They are printed with the offset by both serial console and
dmesg, e.g.
[ 12.230014][ T1] Early printk times: mult=38775352, shift=27, offset=8891950261 ns
[ 12.246008][ T1] Run /init as init process
[ 12.254944][ T1] with arguments:
[ 12.264341][ T1] /initThis is correct. I don't want to overwhelm users of this, but there are three time-gathering periods, and roughly 2 output times (before calibration and after calibration).
early boot = before time init, before cycles calibration and offset calculation mid boot = after time init, before cycles calibration and offset calculation late boot = after time init, after cycles calibration and offset calculation All of these are before the start of user space processes.
time of printk output time timestamp type stored timestamp output
------------------ ---------------- ----------------------------- ------------------------
early boot early boot cycles 0
mid boot mid-boot nanosecs seconds, with offset from time_init
late boot late-boot nanosecs seconds with offset from cycle counter start
-----
early boot post-boot (cycles already stored) seconds with offset from cycle counter start
mid boot post-boot (ns already stored) seconds with offset from cycle counter start
late boot post-boot (ns already stored) seconds with offset from cycle counter start
all others post-boot nanosecs seconds with offset from cycle counter start
The confusing thing is messages that are output to the console before calibration and offset calculation. Reports from all user space tools (ie from dmesg or journalctl) should be correct and consistent.
Quoted 6 lines
Quoted 3 lines
An accessor can be implemented for (struct printk_info).ts_nsec, say
get_timestamp(), which can be called from both the places.Yeah, a helper function for reading the timestamp might be a cleaner solution.
I consider adjust_early_ts() to be such an accessor function. It's supposed to hide the details of the type of the timestamp (cycles or ns) and the offset. Maybe this could be renamed to something better, like: get_adjusted_ts()? Let me know what you think.
Quoted 5 lines
Quoted 1 line
Everything seems to be as expected there. The non-zero timestamps on the serial console are from messages added after the timekeeping was initialized.
I'll rebase this patch to resolve the #include conflicts, and address this feedback, and hopefully get a new version out this week.
Thanks again for the discussion and ideas!
-- Tim
Petr Mladek
maintainerRe: [PATCH v3] printk: fix zero-valued printk timestamps in early boot
Quoted 68 lines · Tim
Quoted 52 lines
From: Petr Mladek <[email protected]>
There are three stages:1. Early messages where the cycles are stored.
The serial console shows zero time stamp because it reads the messages
_beforethecalibration_, e.g.[ 0.000000] Linux version 6.19.0-rc7-default+ (pmladek@pathway) (gcc (SUSE Linux) 15.2.1 20251006, GNU ld (GNU Binutils;
openSUSE Tumbleweed) 2.45.0.20251103-2) #521 SMPPREEMPT_DYNAMICMon Feb 2 16:36:53 CET 2026[ 0.000000] Command line: BOOT_IMAGE=/boot/vmlinuz-6.19.0-rc7-default+ root=UUID=587ae802-e330-4059-9b48- d5b845e1075a resume=/dev/disk/by-uuid/369c7453-3d16-409d-88b2-5de027891a12 mitigations=auto nosplash earlycon=uart8250,io,0x3f8,115200 console=ttyS0,115200 console=ttynull console=tty0 debug_non_panic_cpus=1 panic=10 ignore_loglevel log_buf_len=1M [ 0.000000] BIOS-provided physical RAM map: [ 0.000000] BIOS-e820: [mem 0x0000000000000000-0x000000000009fbff] usable [ 0.000000] BIOS-e820: [mem 0x000000000009fc00-0x000000000009ffff] reservedBut "dmesg" shows some numbers because it reads the messages
_afterthecalibration_:[ 8.853613] Linux version 6.19.0-rc7-default+ (pmladek@pathway) (gcc (SUSE Linux) 15.2.1 20251006, GNU ld (GNU Binutils;
openSUSE Tumbleweed) 2.45.0.20251103-2) #521 SMPPREEMPT_DYNAMICMon Feb 2 16:36:53 CET 2026[ 8.853617] Command line: BOOT_IMAGE=/boot/vmlinuz-6.19.0-rc7-default+ root=UUID=587ae802-e330-4059-9b48- d5b845e1075a resume=/dev/disk/by-uuid/369c7453-3d16-409d-88b2-5de027891a12 mitigations=auto nosplash earlycon=uart8250,io,0x3f8,115200 console=ttyS0,115200 console=ttynull console=tty0 debug_non_panic_cpus=1 panic=10 ignore_loglevel log_buf_len=1M [ 8.865086] BIOS-provided physical RAM map: [ 8.865087] BIOS-e820: [mem 0x0000000000000000-0x000000000009fbff] usable [ 8.865089] BIOS-e820: [mem 0x000000000009fc00-0x000000000009ffff] reserved2. Early messages added _after the timekeeping_ is initialized but _before the early cycles calibration_. They serial console prints them _without the offset_ because it reads them _before the calibration_, e.g. [ 3.288049][ T1] Write protecting the kernel read-only data: 36864k [ 3.298554][ T1] Freeing unused kernel image (text/rodata gap) memory: 1656K [ 3.318942][ T1] Freeing unused kernel image (rodata/data gap) memory: 1540K But "dmesg" prints them _with the offset_ because it reads them _after the calibration_, e.g. [ 12.179999] [ T1] Write protecting the kernel read-only data: 36864k [ 12.190505] [ T1] Freeing unused kernel image (text/rodata gap) memory: 1656K [ 12.210893] [ T1] Freeing unused kernel image (rodata/data gap) memory: 1540K 3. Messages added after the calibration of the early cycles.Quoted 7 lines
They are printed with the offset by both serial console and dmesg, e.g. [ 12.230014][ T1] Early printk times: mult=38775352, shift=27, offset=8891950261 ns [ 12.246008][ T1] Run /init as init process [ 12.254944][ T1] with arguments: [ 12.264341][ T1] /initThis is correct. I don't want to overwhelm users of this, but there are three time-gathering periods, and roughly 2 output times (before calibration and after calibration).
early boot = before time init, before cycles calibration and offset calculation mid boot = after time init, before cycles calibration and offset calculation late boot = after time init, after cycles calibration and offset calculation All of these are before the start of user space processes.
Quoted 5 lines
time of printk output time timestamp type stored timestamp output
------------------ ---------------- ----------------------------- ------------------------
early boot early boot cycles 0
mid boot mid-boot nanosecs seconds, with offset fromtime_init
late boot late-boot nanosecs seconds with offset from cycle counter start
This is a bit confusing. I looks like the offset from time_init() is not longer added.
Quoted 8 lines
-----
early boot post-boot (cycles already stored) seconds with offset from cycle counter start
mid boot post-boot (ns already stored) seconds with offset from cycle counter start
late boot post-boot (ns already stored) seconds with offset from cycle counter start
all others post-boot nanosecs seconds with offset from cycle counter startThe confusing thing is messages that are output to the console before calibration and offset calculation. Reports from all user space tools (ie from dmesg or journalctl) should be correct and consistent.
Yup.
I like that table. I just wonder how to better distinguish the offset from timekeeping and calibrated cycles. My variant:
<proposal>
The printk timestamps are stored and interpretted differently in the following periods:
- early boot: before timekeeping init, before cycles calibration
- mid boot: after timekeeping init, before cycles calibration
- late boot: after timekeeping init, after cycles calibration
Console output (immediately):
printk() time stored value immediate output(sec)
-----------------------------------------------------------------------------
early boot cycles 0
mid boot get_time() get_time()
late boot get_time() get_time + calibrated(cycles offset)
User space tools and late registered consoles:
printk() time stored value output after calibration (sec)
-----------------------------------------------------------------------------
early boot cycles calibrated(cycles)
mid boot get_time() get_time() + calibrated(cycles offset)
late boot get_time() get_time() + calibrated(cycles offset)
I am not sure where to put this. One place might be Documentation/core-api/printk-basics.rst. But it might be better add a separate file either under core-api/ or under admin-guide/.
Quoted 10 lines
Quoted 6 lines
Quoted 3 lines
An accessor can be implemented for (struct printk_info).ts_nsec, say
get_timestamp(), which can be called from both the places.Yeah, a helper function for reading the timestamp might be a cleaner solution.
I consider
adjust_early_ts()to be such an accessor function. It's supposed to hide the details of the type of the timestamp (cycles or ns) and the offset. Maybe this could be renamed to something better,
like:get_adjusted_ts()? Let me know what you think.
This a bike shedding area ;-)
I personally find
u64 ts = get_printk_info_ts(info);
a bit cleaner than
u64 ts = adjust_early_ts(info->ts_nsec);
because you might add a comment into struct printk_info definition that nobody should read the timestamp directly. They should use the helper intead.
The helper would do something like:
/*
* The number of early cycles is stored before the timekeeping gets initialized.
* The local_clock() value is stored later.
*
* Note that early_ts_offset, early_mult, and early_shift are 0
* before the cycles get calibrated against the official time keeping. *
* Any output after user space start (eg. from dmesg or journalctl)
* will show consistent values with calibrated cycles and offset. */
static inline u64 get_printk_info_ts(const struct printk_info *info)
{
if (likely(!(info->ts_cycles & EARLY_CYCLES_BIT)))
/* if timestamp is not in cycles, just add offset */
return info->ts_nsec + early_ts_offset;
/* mask high bit and convert to nanoseconds */
return mul_u64_u32_shr(info->ts_cycles & EARLY_CYCLES_MASK,
early_mult, early_shift);
}Note that I have already used two names (ts_cycles and ts_nsec) which would point to the same data via an union.
Quoted 2 lines
I'll rebase this patch to resolve the #include conflicts, and address this feedback, and hopefully get a new version out this week.
Thanks a lot. v3 looked good enough to me. But v4 will be even better after the feedback.
Take your time ;-)
Best Regards,
Petr
RE: [PATCH v3] printk: fix zero-valued printk timestamps in early boot
In reply to Tim BirdFrom: Tim Bird <[email protected]> Sent: Tuesday, February 10, 2026 3:48 PM
Quoted 14 lines
During early boot, printk timestamps are reported as zero before kernel timekeeping starts (e.g. before
time_init()). This hinders boot-time optimization efforts. This period is about 400 milliseconds for many current desktop and embedded machines running Linux.Add support to save cycles during early boot, and output correct timestamp values after timekeeping is initialized.
get_cycles()is operational on arm64 andx86_64from kernel start. Add code and variables to save calibration values used to later convert cycle counts to time values in the early printks. Add a config to control the feature.
Quoted 7 lines
This yields non-zero timestamps for printks from the very start of kernel execution. The timestamps are relative to the start of the architecture-specified counter used in
get_cycles(e.g. the TSC onx86_64andcntvct_el0on arm64).All timestamps reflect time from processor power-on instead of time from the kernel's timekeeping initialization.
I tried this patch in linux-next20260302 kernel running as a guest VM on a Hyper-V host. Two things:
1) In the dmesg output, I'm seeing a place where the timestamps briefly go backwards -- i.e., they are not monotonically increasing. Here's a snippet, where there's a smaller timestamp immediately after the tsc is detected:
[ 27.994891] SMBIOS 3.1.0 present. [ 27.994893] DMI: Microsoft Corporation Virtual Machine/Virtual Machine, BIOS Hyper-V UEFI Release v4.1 09/25/2025 [ 27.994898] DMI: Memory slots populated: 2/2 [ 27.995202] Hypervisor detected: Microsoft Hyper-V [ 27.995205] Hyper-V: privilege flags low 0xae7f, high 0x3b8030, ext 0x62, hints 0xa0e24, misc 0xe0bed7b2 [ 27.995208] Hyper-V: Nested features: 0x0 [ 27.995209] Hyper-V: LAPIC Timer Frequency: 0xc3500 [ 27.995210] Hyper-V: Using hypercall for remote TLB flush [ 27.995216] clocksource: hyperv_clocksource_tsc_page: mask: 0xffffffffffffffff max_cycles: 0x24e6a1710, max_idle_ns: 440795202120 ns [ 27.995218] clocksource: hyperv_clocksource_msr: mask: 0xffffffffffffffff max_cycles: 0x24e6a1710, max_idle_ns: 440795202120 ns [ 27.995220] tsc: Detected 2918.401 MHz processor [ 27.991060] e820: update [mem 0x00000000-0x00000fff] System RAM ==> device reserved [ 27.991062] e820: remove [mem 0x000a0000-0x000fffff] System RAM [ 27.991064] last_pfn = 0x210000 max_arch_pfn = 0x400000000 [ 27.991065] x86/PAT: PAT support disabled because CONFIG_X86_PAT is disabled in the kernel. [ 27.991066] x86/PAT: Configuration [0-7]: WB WT UC- UC WB WT UC- UC
Hyper-V provides a synthetic clocksource (two actually), and perhaps they are the cause of the problem, though I haven't spent any time debugging.
2) A Linux VM running in the Azure cloud is also running on Hyper-V. Such a VM typically uses cloud-init to set everything up at boot time, and cloud-init is outputting lines to the serial console with a timestamp that looks like the printk() timestamp, but apparently is not adjusted for the early timestamping that this patch adds. Again, I haven't debugged what's going on -- I'm not immediately sure of the mechanism that cloud-init uses to do output to the serial console. The use of the Hyper-V synthetic clock source might the cause of the problem here as well. Here's an output snippet from the serial console:
[ 20.330414] systemd[1]: Condition check resulted in OpenVSwitch configuration for cleanup being skipped. [ 20.332911] systemd[1]: Condition check resulted in Set Up Additional Binary Formats being skipped. [ 20.333257] pstore: Registered efi_pstore as persistent store backend [ 20.334360] systemd[1]: Condition check resulted in File System Check on Root Device being skipped. [ 20.338319] systemd[1]: Starting Load Kernel Modules... [ 20.341094] systemd[1]: Starting Remount Root and Kernel File Systems... [ 20.350993] systemd[1]: Starting udev Coldplug all Devices... [ 20.356255] systemd[1]: Starting Uncomplicated firewall... [ 20.361536] systemd[1]: Started Journal Service. [ 20.386902] EXT4-fs (sda1): re-mounted c02dce0c-0c40-4e6e-88af-c5a0987b0adb r/w. [ 22.532033] /dev/sr0: Can't lookup blockdev [ 7.955973] cloud-init[783]: Cloud-init v. 24.3.1-0ubuntu0~20.04.1 running 'init-local' at Wed, 11 Mar 2026 15:27:06 +0000. Up 7.48 seconds. [ 9.933120] cloud-init[822]: Cloud-init v. 24.3.1-0ubuntu0~20.04.1 running 'init' at Wed, 11 Mar 2026 15:27:08 +0000. Up 9.82 seconds. [ 9.935483] cloud-init[822]: ci-info: ++++++++++++++++++++++++++++++++++++++Net device info+++++++++++++++++++++++++++++++++++++++ [ 9.937726] cloud-init[822]: ci-info: +--------+------+-----------------------------+---------------+--------+-------------------+ [ 9.939905] cloud-init[822]: ci-info: | Device | Up | Address | Mask | Scope | Hw-Address | [ 9.942059] cloud-init[822]: ci-info: +--------+------+-----------------------------+---------------+--------+-------------------+
The cloud-init lines don't show up in dmesg, so there's no problem there.
I will look into both issues further, but probably not today.
Michael
Quoted 235 lines
Signed-off-by Tim Bird <[email protected]>---
V2->V3
Default CONFIG option to 'n'
Move more code from intoearly_times.h
(reducing ifdefs ininit/main.c)
Use math64 helper routines
Usecycles_tinstead of u64 type
Add #defines forEARLY_CYCLES_BITand MASK Invert if logic inadjust_early_ts()(note: no change to 'depends on' inKconfigentry)
V1->V2
Remove calibration CONFIG vars
Add 'depends on' to restrict arches (to handle ppc bug) Addearly_ts_offsetto avoid discontinuity Save cycles ints_nsec, and convert on output Move conditional code to include file (early_times.h)include/linux/early_times.h | 85 +++++++++++++++++++++++++++++++++++++ init/Kconfig | 14 ++++++ init/main.c | 6 +++ kernel/printk/printk.c | 18 +++++++- 4 files changed, 121 insertions(+), 2 deletions(-) create mode 100644 include/linux/early_times.hinclude/linux/early_times.h new file +37
-0@@ -0,0 +1,85 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#ifndef _KERNEL_PRINTK_EARLY_TIMES_H +#define _KERNEL_PRINTK_EARLY_TIMES_H + +#include <linux/timex.h> +#include <linux/clocksource.h> + +/* use high bit of a u64 to indicate cycles instead of a timestamp */ +#define EARLY_CYCLES_BIT BIT_ULL(63) +#define EARLY_CYCLES_MASK ~(BIT_ULL(63)) + +#if defined(CONFIG_EARLY_PRINTK_TIMES) +extern cycles_t start_cycles; +extern u64 start_ns; +extern u32 early_mult, early_shift; +extern u64 early_ts_offset; + +static inline void early_times_start_calibration(void) +{ + start_cycles = get_cycles(); + start_ns = local_clock(); +} + +static inline void early_times_finish_calibration(void) +{ + cycles_t end_cycles; + u64 end_ns; + + /* set calibration data for early_printk_times */ + end_cycles = get_cycles(); + end_ns = local_clock(); + clocks_calc_mult_shift(&early_mult, &early_shift, + mul_u64_u64_div_u64(end_cycles - start_cycles, + NSEC_PER_SEC, end_ns - start_ns), + NSEC_PER_SEC, 100); + early_ts_offset = mul_u64_u32_shr(start_cycles, early_mult, early_shift) -
start_ns;include/linux/early_times.h new file +48
-0+ +pr_debug ("Early printk times: mult=%u, shift=%u, offset=%llu ns\n" , + early_mult, early_shift, early_ts_offset); +} + +static inline u64 early_cycles (void ) +{ +return (get_cycles () |EARLY_CYCLES_BIT ); +} + +/* +* adjust_early_ts detects whether ts in is cycles or nanoseconds +* and converts it or adjusts it, taking into account the offset +* from cycle-counter start. +* +* Note that early_mult may be 0, but that's OK because +* we'll just multiply by 0 and return 0. This will +* only occur if we're outputting a printk message +* before the calibration of the early timestamp. +* Any output after user space start (eg. from dmesg or +* journalctl) will show correct values. +*/ +static inline u64 adjust_early_ts (u64 ts) +{ +if (likely (!(ts &EARLY_CYCLES_BIT ))) +/* if timestamp is not in cycles, just add offset */ +return ts + early_ts_offset; + +/* mask high bit and convert to nanoseconds */ +return mul_u64_u32_shr (ts &EARLY_CYCLES_MASK , early_mult, early_shift); +} + +#else +# define early_times_start_calibration () do { } while (0) +# define early_times_finish_calibration () do { } while (0) + +static inline u64 early_cycles (void ) +{ +return 0 ; +} + +static inline u64 adjust_early_ts (u64 ts) +{ +return ts; +} +#endif /* CONFIG_EARLY_PRINTK_TIMES */ + +#endif /* _KERNEL_PRINTK_EARLY_TIMES_H */ init/Kconfig +14
-0@@ -777,6 +777,20 @@ config IKHEADERS or similar programs. If you build the headers as a module, a module called kheaders.ko is built which can be loaded on-demand to get access to headers. +config EARLY_PRINTK_TIMES + bool "Show non-zero printk timestamps early in boot" + default n + depends on PRINTK + depends on ARM64 || X86_64 + help + Use a cycle-counter to provide printk timestamps during + early boot. This allows seeing timestamps for printks that + would otherwise show as 0. Note that this will shift the + printk timestamps to be relative to processor power on, instead + of relative to the start of kernel timekeeping. This should be + closer to machine power on, giving a better indication of + overall boot time. + config LOG_BUF_SHIFT int "Kernel log buffer size (16 => 64KB, 17 => 128KB)" range 12 25init/main.c +6
-0@@ -104,6 +104,7 @@#include <linux/pidfs.h> #include <linux/ptdump.h> #include <linux/time_namespace.h> +#include <linux/early_times.h> #include <net/net_namespace.h> #include <asm/io.h> @@ -1118,6 +1119,9 @@ void start_kernel(void)timekeeping_init ();time_init (); +/* This must be after timekeeping is initialized */ +early_times_start_calibration (); +/* This must be after timekeeping is initialized */ random_init ();@@ -1600,6 +1604,8 @@ static int __ref kernel_init(void *unused)do_sysctl_args (); +early_times_finish_calibration (); +if (ramdisk_execute_command) { ret =run_init_process (ramdisk_execute_command);if (!ret)kernel/printk/printk.c +16
-3@@ -46,6 +46,7 @@#include <linux/ctype.h> #include <linux/uio.h> #include <linux/sched/clock.h> +#include <linux/early_times.h> #include <linux/sched/debug.h> #include <linux/sched/task_stack.h> #include <linux/panic.h> @@ -75,6 +76,13 @@ EXPORT_SYMBOL(ignore_console_lock_warning);EXPORT_TRACEPOINT_SYMBOL_GPL (console); +#ifdef CONFIG_EARLY_PRINTK_TIMES +cycles_t start_cycles; +u64 start_ns; +u32 early_mult, early_shift; +u64 early_ts_offset; +#endif +/* * Low level drivers may need that to know if they can schedule in * their unblank() callback or not. So let's export it. @@ -639,7 +647,7 @@ static void append_char(char **pp, char *e, char c)static ssize_t info_print_ext_header (char *buf,size_t size,struct printk_info *info) { -u64 ts_usec = info->ts_nsec; +u64 ts_usec =adjust_early_ts (info->ts_nsec);char caller[20 ];#ifdef CONFIG_PRINTK_CALLER u32 id = info->caller_id;@@ -1352,7 +1360,11 @@ static size_t print_syslog(unsigned int level, char *buf)static size_t print_time (u64 ts,char *buf) { -unsigned long rem_nsec =do_div (ts,1000000000 ); +unsigned long rem_nsec; + + ts =adjust_early_ts (ts); + + rem_nsec =do_div (ts,1000000000 );return sprintf (buf,"[%5lu.%06lu]" , (unsigned long )ts, rem_nsec /1000 );@@ -2242,6 +2254,8 @@ int vprintk_store(int facility, int level,* timestamp with respect to the caller. */ ts_nsec =local_clock (); +if (!ts_nsec) + ts_nsec =early_cycles (); caller_id =printk_caller_id (); --2.43.0
RE: [PATCH v3] printk: fix zero-valued printk timestamps in early boot
Hey Micheal,
This report is very interesting.
Thanks very much for trying it out!
Quoted 33 lines
-----Original Message-----
From: Michael Kelley <[email protected]>
Sent: Wednesday, March 11, 2026 9:47 AM
From: Tim Bird <[email protected]> Sent: Tuesday, February 10, 2026 3:48 PMQuoted 14 lines
During early boot, printk timestamps are reported as zero before kernel timekeeping starts (e.g. before
time_init()). This hinders boot-time optimization efforts. This period is about 400 milliseconds for many current desktop and embedded machines running Linux.Add support to save cycles during early boot, and output correct timestamp values after timekeeping is initialized.
get_cycles()is operational on arm64 andx86_64from kernel start. Add code and variables to save calibration values used to later convert cycle counts to time values in the early printks. Add a config to control the feature.Quoted 7 lines
This yields non-zero timestamps for printks from the very start of kernel execution. The timestamps are relative to the start of the architecture-specified counter used in
get_cycles(e.g. the TSC onx86_64andcntvct_el0on arm64).All timestamps reflect time from processor power-on instead of time from the kernel's timekeeping initialization.
I tried this patch in linux-next20260302 kernel running as a guest VM on a Hyper-V host. Two things:
1) In the dmesg output, I'm seeing a place where the timestamps briefly go backwards -- i.e., they are not monotonically increasing. Here's a snippet, where there's a smaller timestamp immediately after the tsc is detected:
Quoted 11 lines
[ 27.994891] SMBIOS 3.1.0 present. [ 27.994893] DMI: Microsoft Corporation Virtual Machine/Virtual Machine, BIOS Hyper-V UEFI Release v4.1 09/25/2025 [ 27.994898] DMI: Memory slots populated: 2/2 [ 27.995202] Hypervisor detected: Microsoft Hyper-V [ 27.995205] Hyper-V: privilege flags low 0xae7f, high 0x3b8030, ext 0x62, hints 0xa0e24, misc 0xe0bed7b2 [ 27.995208] Hyper-V: Nested features: 0x0 [ 27.995209] Hyper-V: LAPIC Timer Frequency: 0xc3500 [ 27.995210] Hyper-V: Using hypercall for remote TLB flush [ 27.995216] clocksource: hyperv_clocksource_tsc_page: mask: 0xffffffffffffffff max_cycles: 0x24e6a1710, max_idle_ns: 440795202120 ns [ 27.995218] clocksource: hyperv_clocksource_msr: mask: 0xffffffffffffffff max_cycles: 0x24e6a1710, max_idle_ns: 440795202120 ns [ 27.995220] tsc: Detected 2918.401 MHz processor
I wonder if the tsc is getting fiddled with or virtualized somewhere in here, as part of clocksource initialization. I believe each clocksource in the kernel maintains it's own internal offset, and maybe the offset that is being used ends up being slightly different from the cycle-counter offset that the early_times feature uses. I'm just throwing out guesses. It's about a 4ms delta, which is pretty big.
Quoted 38 lines
[ 27.991060] e820: update [mem 0x00000000-0x00000fff] System RAM ==> device reserved [ 27.991062] e820: remove [mem 0x000a0000-0x000fffff] System RAM [ 27.991064] last_pfn = 0x210000 max_arch_pfn = 0x400000000 [ 27.991065] x86/PAT: PAT support disabled because CONFIG_X86_PAT is disabled in the kernel. [ 27.991066] x86/PAT: Configuration [0-7]: WB WT UC- UC WB WT UC- UCHyper-V provides a synthetic clocksource (two actually), and perhaps they are the cause of the problem, though I haven't spent any time debugging.
2) A Linux VM running in the Azure cloud is also running on Hyper-V. Such a VM typically uses cloud-init to set everything up at boot time, and cloud-init is outputting lines to the serial console with a timestamp that looks like the
printk()timestamp, but apparently is not adjusted for the early timestamping that this patch adds. Again, I haven't debugged what's going on -- I'm not immediately sure of the mechanism that cloud-init uses to do output to the serial console. The use of the Hyper-V synthetic clock source might the cause of the problem here as well. Here's an output snippet from the serial console:[ 20.330414] systemd[1]: Condition check resulted in OpenVSwitch configuration for cleanup being skipped. [ 20.332911] systemd[1]: Condition check resulted in Set Up Additional Binary Formats being skipped. [ 20.333257] pstore: Registered efi_pstore as persistent store backend [ 20.334360] systemd[1]: Condition check resulted in File System Check on Root Device being skipped. [ 20.338319] systemd[1]: Starting Load Kernel Modules... [ 20.341094] systemd[1]: Starting Remount Root and Kernel File Systems... [ 20.350993] systemd[1]: Starting udev Coldplug all Devices... [ 20.356255] systemd[1]: Starting Uncomplicated firewall... [ 20.361536] systemd[1]: Started Journal Service. [ 20.386902] EXT4-fs (sda1): re-mounted c02dce0c-0c40-4e6e-88af-c5a0987b0adb r/w. [ 22.532033] /dev/sr0: Can't lookup blockdev [ 7.955973] cloud-init[783]: Cloud-init v. 24.3.1-0ubuntu0~20.04.1 running 'init-local' at Wed, 11 Mar 2026 15:27:06 +0000. Up 7.48 seconds. [ 9.933120] cloud-init[822]: Cloud-init v. 24.3.1-0ubuntu0~20.04.1 running 'init' at Wed, 11 Mar 2026 15:27:08 +0000. Up 9.82 seconds. [ 9.935483] cloud-init[822]: ci-info: ++++++++++++++++++++++++++++++++++++++Net device info+++++++++++++++++++++++++++++++++++++++ [ 9.937726] cloud-init[822]: ci-info: +--------+------+-----------------------------+---------------+--------+-------------------+ [ 9.939905] cloud-init[822]: ci-info: | Device | Up | Address | Mask | Scope | Hw-Address | [ 9.942059] cloud-init[822]: ci-info: +--------+------+-----------------------------+---------------+--------+-------------------+
Quoted 1 line
The cloud-init lines don't show up in dmesg, so there's no problem there.
Are the timestamp values for cloud-init usually consistent with the ones from the kernel? That is, without the early_times patch, do they usually match kernel's printk timestamps?
It sounds like these cloud-init messages are emitted directly to the serial console, and do not go through the printk system. Is that right? How do they avoid having the printk messages and cloud-init messages intermingle? I assume cloud-init is a systemd service?
Can you share the calibration lines from this boot? I'm curious if the timestamps just relative to local_clock().
Quoted 1 line
I will look into both issues further, but probably not today.
Thanks for trying the code out. Let me know if you find out anything.
-- Tim
Quoted 237 lines
Michael
Quoted 235 lines
Signed-off-by Tim Bird <[email protected]>---
V2->V3
Default CONFIG option to 'n'
Move more code from intoearly_times.h
(reducing ifdefs ininit/main.c)
Use math64 helper routines
Usecycles_tinstead of u64 type
Add #defines forEARLY_CYCLES_BITand MASK Invert if logic inadjust_early_ts()(note: no change to 'depends on' inKconfigentry)
V1->V2
Remove calibration CONFIG vars
Add 'depends on' to restrict arches (to handle ppc bug) Addearly_ts_offsetto avoid discontinuity Save cycles ints_nsec, and convert on output Move conditional code to include file (early_times.h)include/linux/early_times.h | 85 +++++++++++++++++++++++++++++++++++++ init/Kconfig | 14 ++++++ init/main.c | 6 +++ kernel/printk/printk.c | 18 +++++++- 4 files changed, 121 insertions(+), 2 deletions(-) create mode 100644 include/linux/early_times.hinclude/linux/early_times.h new file +37
-0@@ -0,0 +1,85 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#ifndef _KERNEL_PRINTK_EARLY_TIMES_H +#define _KERNEL_PRINTK_EARLY_TIMES_H + +#include <linux/timex.h> +#include <linux/clocksource.h> + +/* use high bit of a u64 to indicate cycles instead of a timestamp */ +#define EARLY_CYCLES_BIT BIT_ULL(63) +#define EARLY_CYCLES_MASK ~(BIT_ULL(63)) + +#if defined(CONFIG_EARLY_PRINTK_TIMES) +extern cycles_t start_cycles; +extern u64 start_ns; +extern u32 early_mult, early_shift; +extern u64 early_ts_offset; + +static inline void early_times_start_calibration(void) +{ + start_cycles = get_cycles(); + start_ns = local_clock(); +} + +static inline void early_times_finish_calibration(void) +{ + cycles_t end_cycles; + u64 end_ns; + + /* set calibration data for early_printk_times */ + end_cycles = get_cycles(); + end_ns = local_clock(); + clocks_calc_mult_shift(&early_mult, &early_shift, + mul_u64_u64_div_u64(end_cycles - start_cycles, + NSEC_PER_SEC, end_ns - start_ns), + NSEC_PER_SEC, 100); + early_ts_offset = mul_u64_u32_shr(start_cycles, early_mult, early_shift) -
start_ns;include/linux/early_times.h new file +48
-0+ +pr_debug ("Early printk times: mult=%u, shift=%u, offset=%llu ns\n" , + early_mult, early_shift, early_ts_offset); +} + +static inline u64 early_cycles (void ) +{ +return (get_cycles () |EARLY_CYCLES_BIT ); +} + +/* +* adjust_early_ts detects whether ts in is cycles or nanoseconds +* and converts it or adjusts it, taking into account the offset +* from cycle-counter start. +* +* Note that early_mult may be 0, but that's OK because +* we'll just multiply by 0 and return 0. This will +* only occur if we're outputting a printk message +* before the calibration of the early timestamp. +* Any output after user space start (eg. from dmesg or +* journalctl) will show correct values. +*/ +static inline u64 adjust_early_ts (u64 ts) +{ +if (likely (!(ts &EARLY_CYCLES_BIT ))) +/* if timestamp is not in cycles, just add offset */ +return ts + early_ts_offset; + +/* mask high bit and convert to nanoseconds */ +return mul_u64_u32_shr (ts &EARLY_CYCLES_MASK , early_mult, early_shift); +} + +#else +# define early_times_start_calibration () do { } while (0) +# define early_times_finish_calibration () do { } while (0) + +static inline u64 early_cycles (void ) +{ +return 0 ; +} + +static inline u64 adjust_early_ts (u64 ts) +{ +return ts; +} +#endif /* CONFIG_EARLY_PRINTK_TIMES */ + +#endif /* _KERNEL_PRINTK_EARLY_TIMES_H */ init/Kconfig +14
-0@@ -777,6 +777,20 @@ config IKHEADERS or similar programs. If you build the headers as a module, a module called kheaders.ko is built which can be loaded on-demand to get access to headers. +config EARLY_PRINTK_TIMES + bool "Show non-zero printk timestamps early in boot" + default n + depends on PRINTK + depends on ARM64 || X86_64 + help + Use a cycle-counter to provide printk timestamps during + early boot. This allows seeing timestamps for printks that + would otherwise show as 0. Note that this will shift the + printk timestamps to be relative to processor power on, instead + of relative to the start of kernel timekeeping. This should be + closer to machine power on, giving a better indication of + overall boot time. + config LOG_BUF_SHIFT int "Kernel log buffer size (16 => 64KB, 17 => 128KB)" range 12 25init/main.c +6
-0@@ -104,6 +104,7 @@#include <linux/pidfs.h> #include <linux/ptdump.h> #include <linux/time_namespace.h> +#include <linux/early_times.h> #include <net/net_namespace.h> #include <asm/io.h> @@ -1118,6 +1119,9 @@ void start_kernel(void)timekeeping_init ();time_init (); +/* This must be after timekeeping is initialized */ +early_times_start_calibration (); +/* This must be after timekeeping is initialized */ random_init ();@@ -1600,6 +1604,8 @@ static int __ref kernel_init(void *unused)do_sysctl_args (); +early_times_finish_calibration (); +if (ramdisk_execute_command) { ret =run_init_process (ramdisk_execute_command);if (!ret)kernel/printk/printk.c +16
-3@@ -46,6 +46,7 @@#include <linux/ctype.h> #include <linux/uio.h> #include <linux/sched/clock.h> +#include <linux/early_times.h> #include <linux/sched/debug.h> #include <linux/sched/task_stack.h> #include <linux/panic.h> @@ -75,6 +76,13 @@ EXPORT_SYMBOL(ignore_console_lock_warning);EXPORT_TRACEPOINT_SYMBOL_GPL (console); +#ifdef CONFIG_EARLY_PRINTK_TIMES +cycles_t start_cycles; +u64 start_ns; +u32 early_mult, early_shift; +u64 early_ts_offset; +#endif +/* * Low level drivers may need that to know if they can schedule in * their unblank() callback or not. So let's export it. @@ -639,7 +647,7 @@ static void append_char(char **pp, char *e, char c)static ssize_t info_print_ext_header (char *buf,size_t size,struct printk_info *info) { -u64 ts_usec = info->ts_nsec; +u64 ts_usec =adjust_early_ts (info->ts_nsec);char caller[20 ];#ifdef CONFIG_PRINTK_CALLER u32 id = info->caller_id;@@ -1352,7 +1360,11 @@ static size_t print_syslog(unsigned int level, char *buf)static size_t print_time (u64 ts,char *buf) { -unsigned long rem_nsec =do_div (ts,1000000000 ); +unsigned long rem_nsec; + + ts =adjust_early_ts (ts); + + rem_nsec =do_div (ts,1000000000 );return sprintf (buf,"[%5lu.%06lu]" , (unsigned long )ts, rem_nsec /1000 );@@ -2242,6 +2254,8 @@ int vprintk_store(int facility, int level,* timestamp with respect to the caller. */ ts_nsec =local_clock (); +if (!ts_nsec) + ts_nsec =early_cycles (); caller_id =printk_caller_id (); --2.43.0
Petr Mladek
maintainerRe: [PATCH v3] printk: fix zero-valued printk timestamps in early boot
Finally added timekeeping maintainers and John into Cc. We should have added time since v1.
Anyway, you might see the entire history at
https://lore.kernel.org/all/[email protected]/
Quoted 51 lines · Tim
Hey Micheal,
This report is very interesting.
Thanks very much for trying it out!
Quoted 33 lines
-----Original Message-----
From: Michael Kelley <[email protected]>
Sent: Wednesday, March 11, 2026 9:47 AM
From: Tim Bird <[email protected]> Sent: Tuesday, February 10, 2026 3:48 PMQuoted 14 lines
During early boot, printk timestamps are reported as zero before kernel timekeeping starts (e.g. before
time_init()). This hinders boot-time optimization efforts. This period is about 400 milliseconds for many current desktop and embedded machines running Linux.Add support to save cycles during early boot, and output correct timestamp values after timekeeping is initialized.
get_cycles()is operational on arm64 andx86_64from kernel start. Add code and variables to save calibration values used to later convert cycle counts to time values in the early printks. Add a config to control the feature.Quoted 7 lines
This yields non-zero timestamps for printks from the very start of kernel execution. The timestamps are relative to the start of the architecture-specified counter used in
get_cycles(e.g. the TSC onx86_64andcntvct_el0on arm64).All timestamps reflect time from processor power-on instead of time from the kernel's timekeeping initialization.
I tried this patch in linux-next20260302 kernel running as a guest VM on a Hyper-V host. Two things:
1) In the dmesg output, I'm seeing a place where the timestamps briefly go backwards -- i.e., they are not monotonically increasing. Here's a snippet, where there's a smaller timestamp immediately after the tsc is detected:
Quoted 11 lines
[ 27.994891] SMBIOS 3.1.0 present. [ 27.994893] DMI: Microsoft Corporation Virtual Machine/Virtual Machine, BIOS Hyper-V UEFI Release v4.1 09/25/2025 [ 27.994898] DMI: Memory slots populated: 2/2 [ 27.995202] Hypervisor detected: Microsoft Hyper-V [ 27.995205] Hyper-V: privilege flags low 0xae7f, high 0x3b8030, ext 0x62, hints 0xa0e24, misc 0xe0bed7b2 [ 27.995208] Hyper-V: Nested features: 0x0 [ 27.995209] Hyper-V: LAPIC Timer Frequency: 0xc3500 [ 27.995210] Hyper-V: Using hypercall for remote TLB flush [ 27.995216] clocksource: hyperv_clocksource_tsc_page: mask: 0xffffffffffffffff max_cycles: 0x24e6a1710, max_idle_ns: 440795202120 ns [ 27.995218] clocksource: hyperv_clocksource_msr: mask: 0xffffffffffffffff max_cycles: 0x24e6a1710, max_idle_ns: 440795202120 ns [ 27.995220] tsc: Detected 2918.401 MHz processor
Quoted 10 lines
I wonder if the tsc is getting fiddled with or virtualized somewhere in here, as part of clocksource initialization. I believe each clocksource in the kernel maintains it's own internal offset, and maybe the offset that is being used ends up being slightly different from the cycle-counter offset that the
early_timesfeature uses. I'm just throwing out guesses. It's about a 4ms delta, which is pretty big.Quoted 5 lines
[ 27.991060] e820: update [mem 0x00000000-0x00000fff] System RAM ==> device reserved [ 27.991062] e820: remove [mem 0x000a0000-0x000fffff] System RAM [ 27.991064] last_pfn = 0x210000 max_arch_pfn = 0x400000000 [ 27.991065] x86/PAT: PAT support disabled because CONFIG_X86_PAT is disabled in the kernel. [ 27.991066] x86/PAT: Configuration [0-7]: WB WT UC- UC WB WT UC- UC
I wonder how the calibration of the cycles is precise. I wonder if the problem might be that cycles were faster right after boot than later during the calibration.
I added the following debug output on top of this patch:
include/linux/early_times.h +1 -0
@@ -20,6 +20,7 @@ static inline void early_times_start_calibration(void) { start_cycles =get_cycles (); start_ns =local_clock (); +pr_info ("Early printk times: started callibration: %llu ns\n" , start_ns); }static inline void early_times_finish_calibration (void )
kernel/printk/printk.c +2 -0
@@ -2308,6 +2308,8 @@ int vprintk_store(int facility, int level, ts_nsec =local_clock ();if (!ts_nsec) ts_nsec =early_cycles (); +else +pr_info_once ("local_clock() returned non-zero timestamp: %llu nsec\n" , ts_nsec); caller_id =printk_caller_id ();
And it produced in my kvm:
Let's say that start of the cycle counter is
Start of stage A
[ 8.684438] Linux version 7.0.0-rc2-default+ (pmladek@pathway) (gcc (SUSE Linux) 15.2.1 20260202, GNU ld (GNU Binutils; openSUSE Tumbleweed) 2.45.0.20251103-2) #571 SMP PREEMPT_DYNAMIC Fri Mar 13 10:23:54 CET 2026 [ 8.684442] Command line: BOOT_IMAGE=/boot/vmlinuz-7.0.0-rc2-default+ root=/dev/vda2 resume=/dev/disk/by-uuid/369c7453-3d16-409d-88b2-5de027891a12 mitigations=auto nosplash earlycon=uart8250,io,0x3f8,115200 console=ttyS0,115200 console=tty0 ignore_loglevel log_buf_len=1M crashkernel=512M,high crashkernel=72M,low [...] [ 8.696633] earlycon: uart8250 at I/O port 0x3f8 (options '115200') [ 8.696639] printk: legacy bootconsole [uart8250] enabled [ 8.731303] printk: debug: ignoring loglevel setting. [ 8.732349] NX (Execute Disable) protection: active [ 8.733447] APIC: Static calls initialized [ 8.734667] SMBIOS 2.8 present. [ 8.735358] DMI: QEMU Standard PC (Q35 + ICH9, 2009), BIOS rel-1.17.0-2-g4f253b9b-prebuilt.qemu.org 04/01/2014 [ 8.737285] DMI: Memory slots populated: 1/1 [ 8.738380] Hypervisor detected: KVM [ 8.739151] last_pfn = 0x7ffdc max_arch_pfn = 0x400000000 [ 8.740254] kvm-clock: Using msrs 4b564d01 and 4b564d00 [ 8.732971] printk: local_clock() returned non-zero timestamp: 3486 nsec
End of stage A
This is the point where printk() started storing the values from local_clock() instead of cycles.
Start of stage B
[ 8.732971] kvm-clock: using sched offset of 252367014082295 cycles [ 8.735471] clocksource: kvm-clock: mask: 0xffffffffffffffff max_cycles: 0x1cd42e4dffb, max_idle_ns: 881590591483 ns [ 8.738880] tsc: Detected 3293.776 MHz processor [ 8.740474] e820: update [mem 0x00000000-0x00000fff] System RAM ==> device reserved [...] [ 8.932671] rcu: srcu_init: Setting srcu_struct sizes based on contention. [ 8.934047] Early printk times: started callibration: 201079507 ns
End of stage B
This is where we started calibration of early cycles.
Start of stage C
[ 8.935571] Console: colour dummy device 80x25 [...] [ 9.289673] thermal_sys: Registered thermal governor 'fair_share' [ 9.290077] thermal_sys: Registered thermal governor 'bang_bang' [ 9.292290] thermal_sys: Registered thermal governor 'step_wise' [ 9.293530] thermal_sys: Registered thermal governor 'user_space' [ 9.294856] cpuidle: using governor ladder [ 9.296302] cpuidle: using governor menu
Here the thermal governors are registered. I guess that they might reduce speed of some HW.
[...]
[ 11.974147] clk: Disabling unused clocks
Some unused clocks are disabled. I wonder if this might affect counting the cycles.
[ 12.330852] Freeing unused kernel image (rodata/data gap) memory: 1500K [ 12.351191] Early printk times: mult=19634245, shift=26, offset=8732967929 ns
End of stage C
Here is the end on calibration.
Now, if the frequence of the cycles was:
+ was higher in the stage A when only cycles were stored
+ was lower in stage C when it was calibrated against local_clock()
Then it might result in higher (calibrated) timestamps in stage A and step back in stage B.
Or something like this. It is possible that even local_clock() does not have a stable frequence during the early boot.
Idea: A solution might be to start calibration when printk()
gets first non-zero time from local_clock.
Something like:
include/linux/early_times.h +5 -2
@@ -16,10 +16,13 @@ extern u64 start_ns;extern u32 early_mult, early_shift;extern u64 early_ts_offset; -static inline void early_times_start_calibration (void ) +static inline void early_times_may_start_calibration (u64 ts_ns) { +if (start_ns) +return ; + + start_ns = ts_ns; start_cycles =get_cycles (); - start_ns =local_clock (); }static inline void early_times_finish_calibration (void )
init/main.c +0 -3
@@ -1123,9 +1123,6 @@ void start_kernel(void)timekeeping_init ();time_init (); -/* This must be after timekeeping is initialized */ -early_times_start_calibration (); -/* This must be after timekeeping is initialized */ random_init ();
kernel/printk/printk.c +3 -1
@@ -2306,7 +2306,9 @@ int vprintk_store(int facility, int level,* timestamp with respect to the caller. */ ts_nsec =local_clock (); -if (!ts_nsec) +if (ts_nsec) +early_times_may_start_calibration (ts_nsec); +else ts_nsec =early_cycles (); caller_id =printk_caller_id ();
Quoted 21 lines
Quoted 21 lines
2) A Linux VM running in the Azure cloud is also running on Hyper-V. Such a VM typically uses cloud-init to set everything up at boot time, and cloud-init is outputting lines to the serial console with a timestamp that looks like the
printk()timestamp, but apparently is not adjusted for the early timestamping that this patch adds. Again, I haven't debugged what's going on -- I'm not immediately sure of the mechanism that cloud-init uses to do output to the serial console. The use of the Hyper-V synthetic clock source might the cause of the problem here as well. Here's an output snippet from the serial console:[ 20.330414] systemd[1]: Condition check resulted in OpenVSwitch configuration for cleanup being skipped. [ 20.332911] systemd[1]: Condition check resulted in Set Up Additional Binary Formats being skipped. [ 20.333257] pstore: Registered efi_pstore as persistent store backend [ 20.334360] systemd[1]: Condition check resulted in File System Check on Root Device being skipped. [ 20.338319] systemd[1]: Starting Load Kernel Modules... [ 20.341094] systemd[1]: Starting Remount Root and Kernel File Systems... [ 20.350993] systemd[1]: Starting udev Coldplug all Devices... [ 20.356255] systemd[1]: Starting Uncomplicated firewall... [ 20.361536] systemd[1]: Started Journal Service. [ 20.386902] EXT4-fs (sda1): re-mounted c02dce0c-0c40-4e6e-88af-c5a0987b0adb r/w. [ 22.532033] /dev/sr0: Can't lookup blockdev
Quoted 8 lines
Quoted 8 lines
[ 7.955973] cloud-init[783]: Cloud-init v. 24.3.1-0ubuntu0~20.04.1 running 'init-local' at Wed, 11 Mar 2026 15:27:06 +0000. Up 7.48 seconds. [ 9.933120] cloud-init[822]: Cloud-init v. 24.3.1-0ubuntu0~20.04.1 running 'init' at Wed, 11 Mar 2026 15:27:08 +0000. Up 9.82 seconds. [ 9.935483] cloud-init[822]: ci-info: ++++++++++++++++++++++++++++++++++++++Net device info+++++++++++++++++++++++++++++++++++++++ [ 9.937726] cloud-init[822]: ci-info: +--------+------+-----------------------------+---------------+--------+-------------------+ [ 9.939905] cloud-init[822]: ci-info: | Device | Up | Address | Mask | Scope | Hw-Address | [ 9.942059] cloud-init[822]: ci-info: +--------+------+-----------------------------+---------------+--------+-------------------+
This is more complicated. I wonder if the timestamps from cloud-init() are somehow synchronized with local_clock().
We might need to synchronize local_clock() with the cycles as well. But there is the chicken&egg problem. We need:
+ to know the offset caused by cycles when local_clock() gets initialized.
+ local_clock() running for some time to calibrate cycles.
Hmm, I see that time-management people are not in Cc. We should have added them since v1.
I add them now. Better late than never.
Best Regards,
Petr
Re: [PATCH v3] printk: fix zero-valued printk timestamps in early boot
Hi Tim, Petr,
Quoted 267 lines · Petr Mladek
Finally added timekeeping maintainers and John into Cc. We should have added time since v1.
Anyway, you might see the entire history at
https://lore.kernel.org/all/[email protected]/Quoted 51 lines · Tim
Hey Micheal,
This report is very interesting.
Thanks very much for trying it out!
Quoted 33 lines
-----Original Message-----
From: Michael Kelley <[email protected]>
Sent: Wednesday, March 11, 2026 9:47 AM
From: Tim Bird <[email protected]> Sent: Tuesday, February 10, 2026 3:48 PMQuoted 14 lines
During early boot, printk timestamps are reported as zero before kernel timekeeping starts (e.g. before
time_init()). This hinders boot-time optimization efforts. This period is about 400 milliseconds for many current desktop and embedded machines running Linux.Add support to save cycles during early boot, and output correct timestamp values after timekeeping is initialized.
get_cycles()is operational on arm64 andx86_64from kernel start. Add code and variables to save calibration values used to later convert cycle counts to time values in the early printks. Add a config to control the feature.Quoted 7 lines
This yields non-zero timestamps for printks from the very start of kernel execution. The timestamps are relative to the start of the architecture-specified counter used in
get_cycles(e.g. the TSC onx86_64andcntvct_el0on arm64).All timestamps reflect time from processor power-on instead of time from the kernel's timekeeping initialization.
I tried this patch in linux-next20260302 kernel running as a guest VM on a Hyper-V host. Two things:
1) In the dmesg output, I'm seeing a place where the timestamps briefly go backwards -- i.e., they are not monotonically increasing. Here's a snippet, where there's a smaller timestamp immediately after the tsc is detected:
Quoted 11 lines
[ 27.994891] SMBIOS 3.1.0 present. [ 27.994893] DMI: Microsoft Corporation Virtual Machine/Virtual Machine, BIOS Hyper-V UEFI Release v4.1 09/25/2025 [ 27.994898] DMI: Memory slots populated: 2/2 [ 27.995202] Hypervisor detected: Microsoft Hyper-V [ 27.995205] Hyper-V: privilege flags low 0xae7f, high 0x3b8030, ext 0x62, hints 0xa0e24, misc 0xe0bed7b2 [ 27.995208] Hyper-V: Nested features: 0x0 [ 27.995209] Hyper-V: LAPIC Timer Frequency: 0xc3500 [ 27.995210] Hyper-V: Using hypercall for remote TLB flush [ 27.995216] clocksource: hyperv_clocksource_tsc_page: mask: 0xffffffffffffffff max_cycles: 0x24e6a1710, max_idle_ns: 440795202120 ns [ 27.995218] clocksource: hyperv_clocksource_msr: mask: 0xffffffffffffffff max_cycles: 0x24e6a1710, max_idle_ns: 440795202120 ns [ 27.995220] tsc: Detected 2918.401 MHz processorQuoted 10 lines
I wonder if the tsc is getting fiddled with or virtualized somewhere in here, as part of clocksource initialization. I believe each clocksource in the kernel maintains it's own internal offset, and maybe the offset that is being used ends up being slightly different from the cycle-counter offset that the
early_timesfeature uses. I'm just throwing out guesses. It's about a 4ms delta, which is pretty big.Quoted 5 lines
[ 27.991060] e820: update [mem 0x00000000-0x00000fff] System RAM ==> device reserved [ 27.991062] e820: remove [mem 0x000a0000-0x000fffff] System RAM [ 27.991064] last_pfn = 0x210000 max_arch_pfn = 0x400000000 [ 27.991065] x86/PAT: PAT support disabled because CONFIG_X86_PAT is disabled in the kernel. [ 27.991066] x86/PAT: Configuration [0-7]: WB WT UC- UC WB WT UC- UCI wonder how the calibration of the cycles is precise. I wonder if the problem might be that cycles were faster right after boot than later during the calibration.
I added the following debug output on top of this patch:
include/linux/early_times.h +1
-0@@ -20,6 +20,7 @@ static inline void early_times_start_calibration(void) { start_cycles =get_cycles (); start_ns =local_clock (); +pr_info ("Early printk times: started callibration: %llu ns\n" , start_ns); }static inline void early_times_finish_calibration (void )kernel/printk/printk.c +2
-0@@ -2308,6 +2308,8 @@ int vprintk_store(int facility, int level, ts_nsec =local_clock ();if (!ts_nsec) ts_nsec =early_cycles (); +else +pr_info_once ("local_clock() returned non-zero timestamp: %llu nsec\n" , ts_nsec); caller_id =printk_caller_id ();And it produced in my kvm:
Let's say that start of the cycle counter is
Start of stage A
[ 8.684438] Linux version 7.0.0-rc2-default+ (pmladek@pathway) (gcc (SUSE Linux) 15.2.1 20260202, GNU ld (GNU Binutils; openSUSE Tumbleweed) 2.45.0.20251103-2) #571 SMP PREEMPT_DYNAMIC Fri Mar 13 10:23:54 CET 2026 [ 8.684442] Command line: BOOT_IMAGE=/boot/vmlinuz-7.0.0-rc2-default+ root=/dev/vda2 resume=/dev/disk/by-uuid/369c7453-3d16-409d-88b2-5de027891a12 mitigations=auto nosplash earlycon=uart8250,io,0x3f8,115200 console=ttyS0,115200 console=tty0 ignore_loglevel log_buf_len=1M crashkernel=512M,high crashkernel=72M,low [...] [ 8.696633] earlycon: uart8250 at I/O port 0x3f8 (options '115200') [ 8.696639] printk: legacy bootconsole [uart8250] enabled [ 8.731303] printk: debug: ignoring loglevel setting. [ 8.732349] NX (Execute Disable) protection: active [ 8.733447] APIC: Static calls initialized [ 8.734667] SMBIOS 2.8 present. [ 8.735358] DMI: QEMU Standard PC (Q35 + ICH9, 2009), BIOS rel-1.17.0-2-g4f253b9b-prebuilt.qemu.org 04/01/2014 [ 8.737285] DMI: Memory slots populated: 1/1 [ 8.738380] Hypervisor detected: KVM [ 8.739151] last_pfn = 0x7ffdc max_arch_pfn = 0x400000000 [ 8.740254] kvm-clock: Using msrs 4b564d01 and 4b564d00 [ 8.732971] printk: local_clock() returned non-zero timestamp: 3486 nsecEnd of stage A
This is the point where
printk()started storing the values fromlocal_clock()instead of cycles.Start of stage B
[ 8.732971] kvm-clock: using sched offset of 252367014082295 cycles [ 8.735471] clocksource: kvm-clock: mask: 0xffffffffffffffff max_cycles: 0x1cd42e4dffb, max_idle_ns: 881590591483 ns [ 8.738880] tsc: Detected 3293.776 MHz processor [ 8.740474] e820: update [mem 0x00000000-0x00000fff] System RAM ==> device reserved [...] [ 8.932671] rcu: srcu_init: Setting srcu_struct sizes based on contention. [ 8.934047] Early printk times: started callibration: 201079507 nsEnd of stage B
This is where we started calibration of early cycles.
Start of stage C
[ 8.935571] Console: colour dummy device 80x25 [...] [ 9.289673] thermal_sys: Registered thermal governor 'fair_share' [ 9.290077] thermal_sys: Registered thermal governor 'bang_bang' [ 9.292290] thermal_sys: Registered thermal governor 'step_wise' [ 9.293530] thermal_sys: Registered thermal governor 'user_space' [ 9.294856] cpuidle: using governor ladder [ 9.296302] cpuidle: using governor menuHere the thermal governors are registered. I guess that they might reduce speed of some HW.
[...]
[ 11.974147] clk: Disabling unused clocksSome unused clocks are disabled. I wonder if this might affect counting the cycles.
[ 12.330852] Freeing unused kernel image (rodata/data gap) memory: 1500K [ 12.351191] Early printk times: mult=19634245, shift=26, offset=8732967929 nsEnd of stage C
Here is the end on calibration.
Now, if the frequence of the cycles was:
+ was higher in the stage A when only cycles were stored
+ was lower in stage C when it was calibrated againstlocal_clock()Then it might result in higher (calibrated) timestamps in stage A and step back in stage B.
Or something like this. It is possible that even
local_clock()does not have a stable frequence during the early boot.Idea: A solution might be to start calibration when
printk()
gets first non-zero time fromlocal_clock.Something like:
include/linux/early_times.h +5
-2@@ -16,10 +16,13 @@ extern u64 start_ns;extern u32 early_mult, early_shift;extern u64 early_ts_offset; -static inline void early_times_start_calibration (void ) +static inline void early_times_may_start_calibration (u64 ts_ns) { +if (start_ns) +return ; + + start_ns = ts_ns; start_cycles =get_cycles (); - start_ns =local_clock (); }static inline void early_times_finish_calibration (void )init/main.c +0
-3@@ -1123,9 +1123,6 @@ void start_kernel(void)timekeeping_init ();time_init (); -/* This must be after timekeeping is initialized */ -early_times_start_calibration (); -/* This must be after timekeeping is initialized */ random_init ();kernel/printk/printk.c +3
-1@@ -2306,7 +2306,9 @@ int vprintk_store(int facility, int level,* timestamp with respect to the caller. */ ts_nsec =local_clock (); -if (!ts_nsec) +if (ts_nsec) +early_times_may_start_calibration (ts_nsec); +else ts_nsec =early_cycles (); caller_id =printk_caller_id ();Quoted 23 lines
Quoted 23 lines
2) A Linux VM running in the Azure cloud is also running on Hyper-V. Such a VM typically uses cloud-init to set everything up at boot time, and cloud-init is outputting lines to the serial console with a timestamp that looks like the
printk()timestamp, but apparently is not adjusted for the early timestamping that this patch adds. Again, I haven't debugged what's going on -- I'm not immediately sure of the mechanism that cloud-init uses to do output to the serial console. The use of the Hyper-V synthetic clock source might the cause of the problem here as well. Here's an output snippet from the serial console:[ 20.330414] systemd[1]: Condition check resulted in OpenVSwitch configuration for cleanup being skipped. [ 20.332911] systemd[1]: Condition check resulted in Set Up Additional Binary Formats being skipped. [ 20.333257] pstore: Registered efi_pstore as persistent store backend [ 20.334360] systemd[1]: Condition check resulted in File System Check on Root Device being skipped. [ 20.338319] systemd[1]: Starting Load Kernel Modules... [ 20.341094] systemd[1]: Starting Remount Root and Kernel File Systems... [ 20.350993] systemd[1]: Starting udev Coldplug all Devices... [ 20.356255] systemd[1]: Starting Uncomplicated firewall... [ 20.361536] systemd[1]: Started Journal Service. [ 20.386902] EXT4-fs (sda1): re-mounted c02dce0c-0c40-4e6e-88af-c5a0987b0adb r/w. [ 22.532033] /dev/sr0: Can't lookup blockdevQuoted 8 lines
Quoted 8 lines
[ 7.955973] cloud-init[783]: Cloud-init v. 24.3.1-0ubuntu0~20.04.1 running 'init-local' at Wed, 11 Mar 2026 15:27:06 +0000. Up 7.48 seconds. [ 9.933120] cloud-init[822]: Cloud-init v. 24.3.1-0ubuntu0~20.04.1 running 'init' at Wed, 11 Mar 2026 15:27:08 +0000. Up 9.82 seconds. [ 9.935483] cloud-init[822]: ci-info: ++++++++++++++++++++++++++++++++++++++Net device info+++++++++++++++++++++++++++++++++++++++ [ 9.937726] cloud-init[822]: ci-info: +--------+------+-----------------------------+---------------+--------+-------------------+ [ 9.939905] cloud-init[822]: ci-info: | Device | Up | Address | Mask | Scope | Hw-Address | [ 9.942059] cloud-init[822]: ci-info: +--------+------+-----------------------------+---------------+--------+-------------------+
Quoted 9 lines
This is more complicated. I wonder if the timestamps from cloud-init() are somehow synchronized with
local_clock().We might need to synchronize
local_clock()with the cycles as well. But there is the chicken&egg problem. We need:+ to know the offset caused by cycles when
local_clock()gets initialized.
+local_clock()running for some time to calibrate cycles.
Based on the discussions thus far, I see three problems currently:
1. Three phases of timestamps on consoles printing synchronously is awkward
2. Possible userspace breakage:
I don't know how big of a concern this is, but if the timestamps were to not start at zero, any tools expecting them to, would be shocked. printk timestamp comparison with CLOCK_BOOTTIME wouldn't work anymore.
3. Counter frequency constancy requirement:
For the early time calibration to be accurate, the counter frequency should be constant from processor power on to the end of the calibration. We don't know what the firmware or the bootloader may do in-between. If the counter frequency depends on cpu frequency or is just unstable, then we should err on the side of caution and bail out of early times. On the other hand, if constant counter frequency is architecturally guaranteed or if the cpu advertises so, then we should be good. Like, x86 has one only if boot_cpu_has(X86_FEATURE_CONSTANT_TSC), rest are dubious.
Idea: Making the early timestamps start at 0 would partially address these problems:
1. No awkward three phases of timestamps:
The only difference would be that serial would have a number of timestamps exactly at 0, while userspace wouldn't. The early time calibration data would only be used for userspace to convert the counter values to timestamps with a zero origin.
2. Userspace breakage:
CLOCK_BOOTTIME's origin is timekeeping init. So relative to that, the origin of early timestamps would be closer, but still different.
Making the early timestamps start at 0 would also lower counter frequency constancy requirement. Now the constancy requirement would only be between the period of kernel start to calibration end, a phase in which the kernel is in full control. Maybe even if !boot_cpu_has(X86_FEATURE_CONSTANT_TSC), the calibration data would be fine to use? This is debatable.
A disadvantage of this method is that visibility into per-kernel duration is lost. But that can be made up for by adding something like:
pr_info("Pre-kernel duration: %llu ms\n", pre_kernel_time);But this information should be taken with a grain of salt if constant counter frequency is not guaranteed.
Another point, since constancy of counter frequency is being relied upon anyway, conversion of counter value to timestamp can be done later once counter frequency calibration is complete, instead of doing our own calibration. An arch hook like arch_counter_freq() could be provided from which we can get this info. On x86, it would be wired up to tsc_khz. If arch_counter_freq() returns 0, that means constant frequency is not guaranteed, so we'd bail out of the counter value to timestamp conversion.
Thanks,
Shashank
RE: [PATCH v3] printk: fix zero-valued printk timestamps in early boot
Hey Shashank,
Thanks for looking into this, and providing some ideas. See my responses below.
Quoted 13 lines
-----Original Message-----
From: Shashank Balaji <[email protected]>Hi Tim, Petr,
Quoted 7 lines · Petr Mladek
Finally added timekeeping maintainers and John into Cc. We should have added time since v1.
Anyway, you might see the entire history at
https://lore.kernel.org/all/[email protected]/
...
Quoted 4 lines
Based on the discussions thus far, I see three problems currently:
1. Three phases of timestamps on consoles printing synchronously is awkward
Agreed.
Quoted 6 lines
2. Possible userspace breakage:
I don't know how big of a concern this is, but if the timestamps were to not start at zero, any tools expecting them to, would be shocked. printk timestamp comparison with
CLOCK_BOOTTIMEwouldn't work anymore.
Quoted 8 lines
3. Counter frequency constancy requirement:
For the early time calibration to be accurate, the counter frequency should be constant from processor power on to the end of the calibration. We don't know what the firmware or the bootloader may do in-between. If the counter frequency depends on cpu frequency or is just unstable, then we should err on the side of caution and bail out of early times. On the other hand, if constant counter frequency is architecturally guaranteed or if the cpu advertises so, then we should be good. Like, x86 has one only if
boot_cpu_has(X86_FEATURE_CONSTANT_TSC), rest are dubious.
I'll need to check when the data for boot_cpu_has() is initialized. I believe it's not available from the very first kernel instruction, but I may be misremembering. I think almost all x86_64 processors have invariant TSCs since about 2008, so it might be better to just document this issue in the config. I don't think a lot of people are optimizing this 100ms to 400ms region of early boot (that this patch is targeted at) on older processors.
Quoted 24 lines
Idea: Making the early timestamps start at 0 would partially address these problems:
1. No awkward three phases of timestamps:
The only difference would be that serial would have a number of timestamps exactly at 0, while userspace wouldn't. The early time calibration data would only be used for userspace to convert the counter values to timestamps with a zero origin.
2. Userspace breakage:
CLOCK_BOOTTIME's origin is timekeeping init. So relative to that, the origin of early timestamps would be closer, but still different.
Making the early timestamps start at 0 would also lower counter frequency constancy requirement. Now the constancy requirement would only be between the period of kernel start to calibration end, a phase in which the kernel is in full control. Maybe even if !boot_cpu_has(X86_FEATURE_CONSTANT_TSC), the calibration data would be fine to use? This is debatable.
A disadvantage of this method is that visibility into per-kernel duration is lost. But that can be made up for by adding something like:
pr_info("Pre-kernel duration: %llu ms\n", pre_kernel_time);
Quoted 7 lines
But this information should be taken with a grain of salt if constant counter frequency is not guaranteed.
Another point, since constancy of counter frequency is being relied upon anyway, conversion of counter value to timestamp can be done later once counter frequency calibration is complete, instead of doing our own calibration. An arch hook like
arch_counter_freq()could be provided from which we can get this info. On x86, it would be wired up totsc_khz. Ifarch_counter_freq()returns 0, that means constant frequency is not guaranteed, so we'd bail out of the counter value to timestamp conversion.
Interesting. If I can validate that local_clock() is using the same clock source (TSC) as my early_times calibration, maybe I *can* just steal the calibration already done when initializing that clock. I'm using the same routine 'clocks_calc_mult_shift', and maybe it would be better to just grab the numbers that result from the TSC clock initialization, rather than calling it again (with different numbers) myself. I'll look into this.
Thanks very much for the feedback and ideas.
-- Tim
RE: [PATCH v3] printk: fix zero-valued printk timestamps in early boot
In reply to Petr MladekFrom: Petr Mladek <[email protected]> Sent: Friday, March 13, 2026 3:45 AM
Quoted 65 lines
Finally added timekeeping maintainers and John into Cc. We should have added time since v1.
Anyway, you might see the entire history at
https://lore.kernel.org/all/[email protected]/Quoted 57 lines · Tim
Hey Micheal,
This report is very interesting.
Thanks very much for trying it out!
Quoted 33 lines
-----Original Message-----
From: Michael Kelley <[email protected]>
Sent: Wednesday, March 11, 2026 9:47 AM
From: Tim Bird <[email protected]> Sent: Tuesday, February 10, 2026 3:48 PMQuoted 14 lines
During early boot, printk timestamps are reported as zero before kernel timekeeping starts (e.g. before
time_init()). This hinders boot-time optimization efforts. This period is about 400 milliseconds for many current desktop and embedded machines running Linux.Add support to save cycles during early boot, and output correct timestamp values after timekeeping is initialized.
get_cycles()is operational on arm64 andx86_64from kernel start. Add code and variables to save calibration values used to later convert cycle counts to time values in the early printks. Add a config to control the feature.Quoted 7 lines
This yields non-zero timestamps for printks from the very start of kernel execution. The timestamps are relative to the start of the architecture-specified counter used in
get_cycles(e.g. the TSC onx86_64andcntvct_el0on arm64).All timestamps reflect time from processor power-on instead of time from the kernel's timekeeping initialization.
I tried this patch in linux-next20260302 kernel running as a guest VM on a Hyper-V host. Two things:
1) In the dmesg output, I'm seeing a place where the timestamps briefly go backwards -- i.e., they are not monotonically increasing. Here's a snippet, where there's a smaller timestamp immediately after the tsc is detected:
Quoted 11 lines
[ 27.994891] SMBIOS 3.1.0 present. [ 27.994893] DMI: Microsoft Corporation Virtual Machine/Virtual Machine, BIOS Hyper-V UEFI Release v4.1 09/25/2025 [ 27.994898] DMI: Memory slots populated: 2/2 [ 27.995202] Hypervisor detected: Microsoft Hyper-V [ 27.995205] Hyper-V: privilege flags low 0xae7f, high 0x3b8030, ext 0x62, hints 0xa0e24, misc 0xe0bed7b2 [ 27.995208] Hyper-V: Nested features: 0x0 [ 27.995209] Hyper-V: LAPIC Timer Frequency: 0xc3500 [ 27.995210] Hyper-V: Using hypercall for remote TLB flush [ 27.995216] clocksource: hyperv_clocksource_tsc_page: mask: 0xffffffffffffffff max_cycles: 0x24e6a1710, max_idle_ns: 440795202120 ns [ 27.995218] clocksource: hyperv_clocksource_msr: mask: 0xffffffffffffffff max_cycles: 0x24e6a1710, max_idle_ns: 440795202120 ns [ 27.995220] tsc: Detected 2918.401 MHz processorI wonder if the tsc is getting fiddled with or virtualized somewhere in here, as part of clocksource initialization. I believe each clocksource in the kernel maintains it's own internal offset, and maybe the offset that is being used ends up being slightly different from the cycle-counter offset that the
early_timesfeature uses. I'm just throwing out guesses. It's about a 4ms delta, which is pretty big.
I'm fairly certain the TSC frequency is not being fiddled with. In a guest VM on Hyper-V, the x86 instruction to read the TSC executes directly in hardware and is not virtualized. There *is* per-VM scaling of the TSC value to handle live migrations across virtualization hosts with different TSC frequencies, but that's not in play during my experiments.
Quoted 115 lines
Quoted 6 lines
Quoted 6 lines
[ 27.991060] e820: update [mem 0x00000000-0x00000fff] System RAM ==> device reserved [ 27.991062] e820: remove [mem 0x000a0000-0x000fffff] System RAM [ 27.991064] last_pfn = 0x210000 max_arch_pfn = 0x400000000 [ 27.991065] x86/PAT: PAT support disabled because CONFIG_X86_PAT is disabled in the kernel. [ 27.991066] x86/PAT: Configuration [0-7]: WB WT UC- UC WB WT UC- UCI wonder how the calibration of the cycles is precise. I wonder if the problem might be that cycles were faster right after boot than later during the calibration.
I added the following debug output on top of this patch:
include/linux/early_times.h +1
-0@@ -20,6 +20,7 @@ static inline void early_times_start_calibration(void) { start_cycles =get_cycles (); start_ns =local_clock (); +pr_info ("Early printk times: started callibration: %llu ns\n" , start_ns); }static inline void early_times_finish_calibration (void )kernel/printk/printk.c +2
-0@@ -2308,6 +2308,8 @@ int vprintk_store(int facility, int level, ts_nsec =local_clock ();if (!ts_nsec) ts_nsec =early_cycles (); +else +pr_info_once ("local_clock() returned non-zero timestamp: %llu nsec\n" , ts_nsec); caller_id =printk_caller_id ();And it produced in my kvm:
Let's say that start of the cycle counter is
Start of stage A
[ 8.684438] Linux version 7.0.0-rc2-default+ (pmladek@pathway) (gcc (SUSE Linux) 15.2.1 20260202, GNU ld (GNU Binutils; openSUSE Tumbleweed) 2.45.0.20251103-2) #571 SMP PREEMPT_DYNAMIC Fri Mar 13 10:23:54 CET 2026 [ 8.684442] Command line: BOOT_IMAGE=/boot/vmlinuz-7.0.0-rc2-default+root=/dev/vda2 resume=/dev/disk/by-uuid/369c7453-3d16-409d-88b2-
5de027891a12mitigations=auto nosplash earlycon=uart8250,io,0x3f8,115200 console=ttyS0,115200 console=tty0ignore_loglevellog_buf_len=1M crashkernel=512M,high crashkernel=72M,low
[...][ 8.696633] earlycon: uart8250 at I/O port 0x3f8 (options '115200') [ 8.696639] printk: legacy bootconsole [uart8250] enabled [ 8.731303] printk: debug: ignoring loglevel setting. [ 8.732349] NX (Execute Disable) protection: active [ 8.733447] APIC: Static calls initialized [ 8.734667] SMBIOS 2.8 present. [ 8.735358] DMI: QEMU Standard PC (Q35 + ICH9, 2009), BIOS rel-1.17.0-2-g4f253b9b-prebuilt.qemu.org 04/01/2014 [ 8.737285] DMI: Memory slots populated: 1/1 [ 8.738380] Hypervisor detected: KVM [ 8.739151] last_pfn = 0x7ffdc max_arch_pfn = 0x400000000 [ 8.740254] kvm-clock: Using msrs 4b564d01 and 4b564d00 [ 8.732971] printk: local_clock() returned non-zero timestamp: 3486 nsecEnd of stage A
This is the point where
printk()started storing the values fromlocal_clock()instead of cycles.Start of stage B
[ 8.732971] kvm-clock: using sched offset of 252367014082295 cycles [ 8.735471] clocksource: kvm-clock: mask: 0xffffffffffffffff max_cycles: 0x1cd42e4dffb, max_idle_ns: 881590591483 ns [ 8.738880] tsc: Detected 3293.776 MHz processor [ 8.740474] e820: update [mem 0x00000000-0x00000fff] System RAM ==> device reserved [...] [ 8.932671] rcu: srcu_init: Setting srcu_struct sizes based on contention. [ 8.934047] Early printk times: started callibration: 201079507 nsEnd of stage B
This is where we started calibration of early cycles.
Start of stage C
[ 8.935571] Console: colour dummy device 80x25 [...] [ 9.289673] thermal_sys: Registered thermal governor 'fair_share' [ 9.290077] thermal_sys: Registered thermal governor 'bang_bang' [ 9.292290] thermal_sys: Registered thermal governor 'step_wise' [ 9.293530] thermal_sys: Registered thermal governor 'user_space' [ 9.294856] cpuidle: using governor ladder [ 9.296302] cpuidle: using governor menuHere the thermal governors are registered. I guess that they might reduce speed of some HW.
[...]
[ 11.974147] clk: Disabling unused clocksSome unused clocks are disabled. I wonder if this might affect counting the cycles.
[ 12.330852] Freeing unused kernel image (rodata/data gap) memory: 1500K [ 12.351191] Early printk times: mult=19634245, shift=26, offset=8732967929 nsEnd of stage C
Here is the end on calibration.
Now, if the frequence of the cycles was:
Quoted 8 lines
+ was higher in the stage A when only cycles were stored
+ was lower in stage C when it was calibrated againstlocal_clock()Then it might result in higher (calibrated) timestamps in stage A and step back in stage B.
Or something like this. It is possible that even
local_clock()does not have a stable frequence during the early boot.
In my VM on Hyper-V, I do see a problem with the results of your calibration code. Over the calibration interval, you calculate the delta number of nanoseconds from local_clock() and the delta number of TSC cycles. The delta TSC cycles divided by the delta nanoseconds should yield the TSC frequency. But the result of your calibration code is about 3.05 cycles/nsec, when the actual TSC frequency is 2.918 cycles/nsec for the hardware I'm running on.
In a Linux VM where CONFIG_PARAVIRT=y, local_clock() eventually comes down to native_sched_clock(), which just reads the TSC and then converts to nanoseconds based on the kernel's understanding of the TSC frequency. So I don't think the local_clock() frequency is varying. But I'm thinking there are some adjustments being made to the value returned by local_clock() during early initialization, and I didn't try to track those down.
Hyper-V provides guest VMs with a synthetic clock (that is based on the TSC). As an experiment, I used that clock in the early time calibration, and everything worked properly. The calibration code produced delta nanoseconds and delta cycles that were exactly 2.918 cycles/nsec, and the transition from Stage A to Stage B was correct -- no cases of a smaller timestamp following a larger timestamp. So my conclusion is that the calibration is indeed problematic, though I haven't identified why the nanoseconds delta from local_clock() is smaller than it should be.
Quoted 76 lines
Idea: A solution might be to start calibration when
printk()
gets first non-zero time fromlocal_clock.Something like:
include/linux/early_times.h +5
-2@@ -16,10 +16,13 @@ extern u64 start_ns;extern u32 early_mult, early_shift;extern u64 early_ts_offset; -static inline void early_times_start_calibration (void ) +static inline void early_times_may_start_calibration (u64 ts_ns) { +if (start_ns) +return ; + + start_ns = ts_ns; start_cycles =get_cycles (); - start_ns =local_clock (); }static inline void early_times_finish_calibration (void )init/main.c +0
-3@@ -1123,9 +1123,6 @@ void start_kernel(void)timekeeping_init ();time_init (); -/* This must be after timekeeping is initialized */ -early_times_start_calibration (); -/* This must be after timekeeping is initialized */ random_init ();kernel/printk/printk.c +3
-1@@ -2306,7 +2306,9 @@ int vprintk_store(int facility, int level,* timestamp with respect to the caller. */ ts_nsec =local_clock (); -if (!ts_nsec) +if (ts_nsec) +early_times_may_start_calibration (ts_nsec); +else ts_nsec =early_cycles (); caller_id =printk_caller_id ();Quoted 21 lines
Quoted 21 lines
2) A Linux VM running in the Azure cloud is also running on Hyper-V. Such a VM typically uses cloud-init to set everything up at boot time, and cloud-init is outputting lines to the serial console with a timestamp that looks like the
printk()timestamp, but apparently is not adjusted for the early timestamping that this patch adds. Again, I haven't debugged what's going on -- I'm not immediately sure of the mechanism that cloud-init uses to do output to the serial console. The use of the Hyper-V synthetic clock source might the cause of the problem here as well. Here's an output snippet from the serial console:[ 20.330414] systemd[1]: Condition check resulted in OpenVSwitch configuration for cleanup being skipped. [ 20.332911] systemd[1]: Condition check resulted in Set Up Additional Binary Formats being skipped. [ 20.333257] pstore: Registered efi_pstore as persistent store backend [ 20.334360] systemd[1]: Condition check resulted in File System Check on Root Device being skipped. [ 20.338319] systemd[1]: Starting Load Kernel Modules... [ 20.341094] systemd[1]: Starting Remount Root and Kernel File Systems... [ 20.350993] systemd[1]: Starting udev Coldplug all Devices... [ 20.356255] systemd[1]: Starting Uncomplicated firewall... [ 20.361536] systemd[1]: Started Journal Service.
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
[ 20.386902] EXT4-fs (sda1): re-mounted c02dce0c-0c40-4e6e-88af-c5a0987b0adb r/w. [ 22.532033] /dev/sr0: Can't lookup blockdev [ 7.955973] cloud-init[783]: Cloud-init v. 24.3.1-0ubuntu0~20.04.1 running 'init-local' at Wed, 11 Mar 2026 15:27:06 +0000. Up 7.48 seconds. [ 9.933120] cloud-init[822]: Cloud-init v. 24.3.1-0ubuntu0~20.04.1 running 'init' at Wed, 11 Mar 2026 15:27:08 +0000. Up 9.82 seconds. [ 9.935483] cloud-init[822]: ci-info: ++++++++++++++++++++++++++++++++++++++Net device info+++++++++++++++++++++++++++++++++++++++ [ 9.937726] cloud-init[822]: ci-info: +--------+------+-----------------------------+---------------+--------+-------------------+ [ 9.939905] cloud-init[822]: ci-info: | Device | Up | Address | Mask | Scope | Hw-Address | [ 9.942059] cloud-init[822]: ci-info: +--------+------+-----------------------------+---------------+--------+-------------------+
The cloud-init output to the serial console is coming from syslog, which is presumably writing directly to /dev/console. This output is also recorded in the syslog log files (/var/log/syslog in my Ubuntu system), though with timestamps in a text format like
2026-03-14T08:44:24.781241-07:00
The timestamps recorded in /var/log/syslog, and as shown with the 'journalctl' command, are monontonic using that full date/time format. But 'journalctl' with the "-o short-monotonic" option shows the seconds-since-boot format, and in that case, the cloud-init timestamps are discontinuous with the kernel messages, like in the serial console output. I don't know exactly where journalctl gets its knowledge of the boot time, but among the possibilities are:
/proc/uptime/proc/stat (the "btime" field)
These are not adjusted for using early boot times. And it's not clear whether they should be -- I don't know what the big picture implications would be. And there are probably other places the boot time is available to user space. If using early boot times is intended to be only for occasional diagnostic use, then maybe living with the discontinuity is OK. I see that Shashank Balaji has also commented about userspace issues, which covers this syslog case.
I think all this gives a first-level explanation of what I'm seeing in a Hyper-V guest. I don't think any of it is specific to Hyper-V guests or the Hyper-V synthetic clock sources. The issues are more generic. Sorry. :-(
Michael
Quoted 17 lines
This is more complicated. I wonder if the timestamps from cloud-init() are somehow synchronized with
local_clock().We might need to synchronize
local_clock()with the cycles as well. But there is the chicken&egg problem. We need:+ to know the offset caused by cycles when
local_clock()gets initialized.
+local_clock()running for some time to calibrate cycles.Hmm, I see that time-management people are not in Cc. We should have added them since v1.
I add them now. Better late than never.
Best Regards,
Petr
RE: [PATCH v3] printk: fix zero-valued printk timestamps in early boot
Sorry for the slow response. See my response and plan inline below.
Quoted 74 lines
-----Original Message-----
From: Michael Kelley <[email protected]>From: Petr Mladek <pmladek@ suse. com> Sent: Friday, March 13, 2026 3: 45 AM > > Finally added timekeeping maintainers and John into Cc.
Quoted 1 line
We should have added time since v1. > > Anyway, you might see the entire history at
From: Petr Mladek <[email protected]> Sent: Friday, March 13, 2026 3:45 AM
Quoted 56 lines
Finally added timekeeping maintainers and John into Cc. We should have added time since v1.
Anyway, you might see the entire history at
https://lore.kernel.org/all/[email protected]/Quoted 48 lines · Tim
Hey Micheal,
This report is very interesting.
Thanks very much for trying it out!
Quoted 42 lines
-----Original Message-----
From: Michael Kelley <[email protected]>
Sent: Wednesday, March 11, 2026 9:47 AM
From: Tim Bird <[email protected]> Sent: Tuesday, February 10, 2026 3:48 PMQuoted 14 lines
During early boot, printk timestamps are reported as zero before kernel timekeeping starts (e.g. before
time_init()). This hinders boot-time optimization efforts. This period is about 400 milliseconds for many current desktop and embedded machines running Linux.Add support to save cycles during early boot, and output correct timestamp values after timekeeping is initialized.
get_cycles()is operational on arm64 andx86_64from kernel start. Add code and variables to save calibration values used to later convert cycle counts to time values in the early printks. Add a config to control the feature.Quoted 7 lines
This yields non-zero timestamps for printks from the very start of kernel execution. The timestamps are relative to the start of the architecture-specified counter used in
get_cycles(e.g. the TSC onx86_64andcntvct_el0on arm64).All timestamps reflect time from processor power-on instead of time from the kernel's timekeeping initialization.
I tried this patch in linux-next20260302 kernel running as a guest VM on a Hyper-V host. Two things:
1) In the dmesg output, I'm seeing a place where the timestamps briefly go backwards -- i.e., they are not monotonically increasing. Here's a snippet, where there's a smaller timestamp immediately after the tsc is detected:
[ 27.994891] SMBIOS 3.1.0 present. [ 27.994893] DMI: Microsoft Corporation Virtual Machine/Virtual Machine, BIOS Hyper-V UEFI Release v4.1 09/25/2025 [ 27.994898] DMI: Memory slots populated: 2/2 [ 27.995202] Hypervisor detected: Microsoft Hyper-V [ 27.995205] Hyper-V: privilege flags low 0xae7f, high 0x3b8030, ext 0x62, hints 0xa0e24, misc 0xe0bed7b2 [ 27.995208] Hyper-V: Nested features: 0x0 [ 27.995209] Hyper-V: LAPIC Timer Frequency: 0xc3500 [ 27.995210] Hyper-V: Using hypercall for remote TLB flush [ 27.995216] clocksource: hyperv_clocksource_tsc_page: mask: 0xffffffffffffffff max_cycles: 0x24e6a1710, max_idle_ns: 440795202120ns
Quoted 9 lines
Quoted 9 lines
Quoted 2 lines
[ 27.995218] clocksource: hyperv_clocksource_msr: mask: 0xffffffffffffffff max_cycles: 0x24e6a1710, max_idle_ns: 440795202120 ns [ 27.995220] tsc: Detected 2918.401 MHz processorI wonder if the tsc is getting fiddled with or virtualized somewhere in here, as part of clocksource initialization. I believe each clocksource in the kernel maintains it's own internal offset, and maybe the offset that is being used ends up being slightly different from the cycle-counter offset that the
early_timesfeature uses. I'm just throwing out guesses. It's about a 4ms delta, which is pretty big.
Quoted 5 lines
I'm fairly certain the TSC frequency is not being fiddled with. In a guest VM on Hyper-V, the x86 instruction to read the TSC executes directly in hardware and is not virtualized. There *is* per-VM scaling of the TSC value to handle live migrations across virtualization hosts with different TSC frequencies, but that's not in play during my experiments.
OK - thanks. I suspected this was the case.
Quoted 133 lines
Quoted 116 lines
Quoted 7 lines
Quoted 7 lines
[ 27.991060] e820: update [mem 0x00000000-0x00000fff] System RAM ==> device reserved [ 27.991062] e820: remove [mem 0x000a0000-0x000fffff] System RAM [ 27.991064] last_pfn = 0x210000 max_arch_pfn = 0x400000000 [ 27.991065] x86/PAT: PAT support disabled because CONFIG_X86_PAT is disabled in the kernel. [ 27.991066] x86/PAT: Configuration [0-7]: WB WT UC- UC WB WT UC- UCI wonder how the calibration of the cycles is precise. I wonder if the problem might be that cycles were faster right after boot than later during the calibration.
I added the following debug output on top of this patch:
include/linux/early_times.h +1
-0@@ -20,6 +20,7 @@ static inline void early_times_start_calibration(void) { start_cycles =get_cycles (); start_ns =local_clock (); +pr_info ("Early printk times: started callibration: %llu ns\n" , start_ns); }static inline void early_times_finish_calibration (void )kernel/printk/printk.c +2
-0@@ -2308,6 +2308,8 @@ int vprintk_store(int facility, int level, ts_nsec =local_clock ();if (!ts_nsec) ts_nsec =early_cycles (); +else +pr_info_once ("local_clock() returned non-zero timestamp: %llu nsec\n" , ts_nsec); caller_id =printk_caller_id ();And it produced in my kvm:
Let's say that start of the cycle counter is
Start of stage A
[ 8.684438] Linux version 7.0.0-rc2-default+ (pmladek@pathway) (gcc (SUSE Linux) 15.2.1 20260202, GNU ld (GNU Binutils; openSUSE Tumbleweed) 2.45.0.20251103-2) #571 SMP PREEMPT_DYNAMIC Fri Mar 13 10:23:54 CET 2026 [ 8.684442] Command line: BOOT_IMAGE=/boot/vmlinuz-7.0.0-rc2-default+root=/dev/vda2 resume=/dev/disk/by-uuid/369c7453-3d16-409d-88b2-
5de027891a12mitigations=auto nosplash earlycon=uart8250,io,0x3f8,115200 console=ttyS0,115200 console=tty0ignore_loglevellog_buf_len=1M crashkernel=512M,high crashkernel=72M,low
[...][ 8.696633] earlycon: uart8250 at I/O port 0x3f8 (options '115200') [ 8.696639] printk: legacy bootconsole [uart8250] enabled [ 8.731303] printk: debug: ignoring loglevel setting. [ 8.732349] NX (Execute Disable) protection: active [ 8.733447] APIC: Static calls initialized [ 8.734667] SMBIOS 2.8 present. [ 8.735358] DMI: QEMU Standard PC (Q35 + ICH9, 2009), BIOS rel-1.17.0-2-g4f253b9b-prebuilt.qemu.org 04/01/2014 [ 8.737285] DMI: Memory slots populated: 1/1 [ 8.738380] Hypervisor detected: KVM [ 8.739151] last_pfn = 0x7ffdc max_arch_pfn = 0x400000000 [ 8.740254] kvm-clock: Using msrs 4b564d01 and 4b564d00 [ 8.732971] printk: local_clock() returned non-zero timestamp: 3486 nsecEnd of stage A
This is the point where
printk()started storing the values fromlocal_clock()instead of cycles.Start of stage B
[ 8.732971] kvm-clock: using sched offset of 252367014082295 cycles [ 8.735471] clocksource: kvm-clock: mask: 0xffffffffffffffff max_cycles: 0x1cd42e4dffb, max_idle_ns: 881590591483 ns [ 8.738880] tsc: Detected 3293.776 MHz processor [ 8.740474] e820: update [mem 0x00000000-0x00000fff] System RAM ==> device reserved [...] [ 8.932671] rcu: srcu_init: Setting srcu_struct sizes based on contention. [ 8.934047] Early printk times: started callibration: 201079507 nsEnd of stage B
This is where we started calibration of early cycles.
Start of stage C
[ 8.935571] Console: colour dummy device 80x25 [...] [ 9.289673] thermal_sys: Registered thermal governor 'fair_share' [ 9.290077] thermal_sys: Registered thermal governor 'bang_bang' [ 9.292290] thermal_sys: Registered thermal governor 'step_wise' [ 9.293530] thermal_sys: Registered thermal governor 'user_space' [ 9.294856] cpuidle: using governor ladder [ 9.296302] cpuidle: using governor menuHere the thermal governors are registered. I guess that they might reduce speed of some HW.
[...]
[ 11.974147] clk: Disabling unused clocksSome unused clocks are disabled. I wonder if this might affect counting the cycles.
[ 12.330852] Freeing unused kernel image (rodata/data gap) memory: 1500K [ 12.351191] Early printk times: mult=19634245, shift=26, offset=8732967929 nsEnd of stage C
Here is the end on calibration.
Now, if the frequence of the cycles was:
Quoted 8 lines
+ was higher in the stage A when only cycles were stored
+ was lower in stage C when it was calibrated againstlocal_clock()Then it might result in higher (calibrated) timestamps in stage A and step back in stage B.
Or something like this. It is possible that even
local_clock()does not have a stable frequence during the early boot.In my VM on Hyper-V, I do see a problem with the results of your calibration code. Over the calibration interval, you calculate the delta number of nanoseconds from
local_clock()and the delta number of TSC cycles. The delta TSC cycles divided by the delta nanoseconds should yield the TSC frequency. But the result of your calibration code is about 3.05 cycles/nsec, when the actual TSC frequency is 2.918 cycles/nsec for the hardware I'm running on.
Quoted 7 lines
In a Linux VM where
CONFIG_PARAVIRT=y,local_clock()eventually comes down tonative_sched_clock(), which just reads the TSC and then converts to nanoseconds based on the kernel's understanding of the TSC frequency. So I don't think thelocal_clock()frequency is varying. But I'm thinking there are some adjustments being made to the value returned bylocal_clock()during early initialization, and I didn't try to track those down.
Thanks very much for this data point and information!
Quoted 10 lines
Hyper-V provides guest VMs with a synthetic clock (that is based on the TSC). As an experiment, I used that clock in the early time calibration, and everything worked properly. The calibration code produced delta nanoseconds and delta cycles that were exactly 2.918 cycles/nsec, and the transition from Stage A to Stage B was correct -- no cases of a smaller timestamp following a larger timestamp. So my conclusion is that the calibration is indeed problematic, though I haven't identified why the nanoseconds delta from
local_clock()is smaller than it should be.
I'll do a deep dive on my calibration math and see if I can figure out the problem. One guess I have is that it could
be an error in precision due to not enough time accumulating in local_clock before the start of calibration. I plan to move the calibration start around, as well as compare the math from my calibration with the one done by local_clock() to try to figure out the discrepancy.
Quoted 112 lines
Quoted 67 lines
Idea: A solution might be to start calibration when
printk()
gets first non-zero time fromlocal_clock.Something like:
include/linux/early_times.h +5
-2@@ -16,10 +16,13 @@ extern u64 start_ns;extern u32 early_mult, early_shift;extern u64 early_ts_offset; -static inline void early_times_start_calibration (void ) +static inline void early_times_may_start_calibration (u64 ts_ns) { +if (start_ns) +return ; + + start_ns = ts_ns; start_cycles =get_cycles (); - start_ns =local_clock (); }static inline void early_times_finish_calibration (void )init/main.c +0
-3@@ -1123,9 +1123,6 @@ void start_kernel(void)timekeeping_init ();time_init (); -/* This must be after timekeeping is initialized */ -early_times_start_calibration (); -/* This must be after timekeeping is initialized */ random_init ();kernel/printk/printk.c +3
-1@@ -2306,7 +2306,9 @@ int vprintk_store(int facility, int level,* timestamp with respect to the caller. */ ts_nsec =local_clock (); -if (!ts_nsec) +if (ts_nsec) +early_times_may_start_calibration (ts_nsec); +else ts_nsec =early_cycles (); caller_id =printk_caller_id ();Quoted 12 lines
Quoted 12 lines
2) A Linux VM running in the Azure cloud is also running on Hyper-V. Such a VM typically uses cloud-init to set everything up at boot time, and cloud-init is outputting lines to the serial console with a timestamp that looks like the
printk()timestamp, but apparently is not adjusted for the early timestamping that this patch adds. Again, I haven't debugged what's going on -- I'm not immediately sure of the mechanism that cloud-init uses to do output to the serial console. The use of the Hyper-V synthetic clock source might the cause of the problem here as well. Here's an output snippet from the serial console:Quoted 12 lines
Quoted 4 lines
Quoted 4 lines
[ 20.330414] systemd[1]: Condition check resulted in OpenVSwitch configuration for cleanup being skipped. [ 20.332911] systemd[1]: Condition check resulted in Set Up Additional Binary Formats being skipped. [ 20.333257] pstore: Registered efi_pstore as persistent store backend [ 20.334360] systemd[1]: Condition check resulted in File System Check on Root Device being skipped.Quoted 8 lines
Quoted 8 lines
[ 20.338319] systemd[1]: Starting Load Kernel Modules... [ 20.341094] systemd[1]: Starting Remount Root and Kernel File Systems... [ 20.350993] systemd[1]: Starting udev Coldplug all Devices... [ 20.356255] systemd[1]: Starting Uncomplicated firewall... [ 20.361536] systemd[1]: Started Journal Service. [ 20.386902] EXT4-fs (sda1): re-mounted c02dce0c-0c40-4e6e-88af-c5a0987b0adb r/w. [ 22.532033] /dev/sr0: Can't lookup blockdev [ 7.955973] cloud-init[783]: Cloud-init v. 24.3.1-0ubuntu0~20.04.1 running 'init-local' at Wed, 11 Mar 2026 15:27:06 +0000. Up 7.48seconds.
Quoted 1 line
Quoted 1 line
Quoted 1 line
[ 9.933120] cloud-init[822]: Cloud-init v. 24.3.1-0ubuntu0~20.04.1 running 'init' at Wed, 11 Mar 2026 15:27:08 +0000. Up 9.82seconds.
Quoted 1 line
Quoted 1 line
Quoted 1 line
[ 9.935483] cloud-init[822]: ci-info: ++++++++++++++++++++++++++++++++++++++Net deviceinfo+++++++++++++++++++++++++++++++++++++++
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
[ 9.937726] cloud-init[822]: ci-info: +--------+------+-----------------------------+---------------+--------+-------------------+ [ 9.939905] cloud-init[822]: ci-info: | Device | Up | Address | Mask | Scope | Hw-Address | [ 9.942059] cloud-init[822]: ci-info: +--------+------+-----------------------------+---------------+--------+-------------------+The cloud-init output to the serial console is coming from syslog, which is presumably writing directly to
/dev/console. This output is also recorded in the syslog log files (/var/log/syslogin my Ubuntu system), though with timestamps in a text format like2026-03-14T08:44:24.781241-07:00The timestamps recorded in
/var/log/syslog, and as shown with the 'journalctl' command, are monontonic using that full date/time format. But 'journalctl' with the "-o short-monotonic" option shows the seconds-since-boot format, and in that case, the cloud-init timestamps are discontinuous with the kernel messages, like in the serial console output. I don't know exactly where journalctl gets its knowledge of the boot time, but among the possibilities are:
/proc/uptime/proc/stat(the "btime" field)These are not adjusted for using early boot times. And it's not clear whether they should be -- I don't know what the big picture implications would be. And there are probably other places the boot time is available to user space. If using early boot times is intended to be only for occasional diagnostic use, then maybe living with the discontinuity is OK. I see that Shashank Balaji has also commented about userspace issues, which covers this syslog case.
Quoted 4 lines
I think all this gives a first-level explanation of what I'm seeing in a Hyper-V guest. I don't think any of it is specific to Hyper-V guests or the Hyper-V synthetic clock sources. The issues are more generic. Sorry. :-(
Yeah - I was worried about other timestamps not being in synchronization with my adjusted printk timestamps. Looks like that worry was justified.
At this point, there are two avenues:
1) double-down and embed the offset (from power-on rather than from time_init) all the way into local_clock() and/or whatever is providing CLOCK_MONOTONIC and CLOCK_BOOTTIME, or
2) back off, and abandon adding the offset to local_clock()-based printk timestamps. This would leave a discontinuity when EARLY_PRINTK_TIMES was enabled, between the (now) non-zero early printk timestamps and the ones following time_init(). This has the benefit of changing less code, and only affecting the early printk timestamps (and none of the rest of the system). And it has the downside of leaving the possibly confusing time discontinuity early in the kernel log. So far, I haven't seen any tools confused by this, and I can put a message before time_init() to inform humans about the switch.
The purpose of this patch is really focused on that early period of boot, where all other timing and tracing mechanisms are unavailable, and limiting the impact to just those early (currently zero) timestamps seems like the best course.
Of course I still need to get the calibration correct, and I'll work on that before sending another update.
-- Tim
John Ogness
maintainerRE: [PATCH v3] printk: fix zero-valued printk timestamps in early boot
Hi Tim,
Quoted 18 lines · Tim
Yeah - I was worried about other timestamps not being in synchronization with my adjusted printk timestamps. Looks like that worry was justified.
At this point, there are two avenues:
1) double-down and embed the offset (from power-on rather than from
time_init) all the way intolocal_clock()and/or whatever is providingCLOCK_MONOTONICandCLOCK_BOOTTIME, or2) back off, and abandon adding the offset to local_clock()-based printk timestamps. This would leave a discontinuity when
EARLY_PRINTK_TIMESwas enabled, between the (now) non-zero early printk timestamps and the ones followingtime_init(). This has the benefit of changing less code, and only affecting the early printk timestamps (and none of the rest of the system). And it has the downside of leaving the possibly confusing time discontinuity early in the kernel log. So far, I haven't seen any tools confused by this, and I can put a message beforetime_init()to inform humans about the switch.
Quoted 3 lines
The purpose of this patch is really focused on that early period of boot, where all other timing and tracing mechanisms are unavailable, and limiting the impact to just those early (currently zero) timestamps seems like the best course.
I have always felt that printk timestamping should be using a userspace-accessible clock, such as CLOCK_MONOTONIC, rather than the CPU local clock. This simplifies applications coordinating their own logs with raw kernel logs.
I was wondering if your pre-boot timing could be used as the init values for CLOCK_MONOTONIC, so that CLOCK_MONOTONIC is a clean continuation of your pre-boot clocking.
And then we could use this opportunity to switch printk to CLOCK_MONOTONIC.
This might also make sense if initializing CLOCK_MONOTONIC is somehow more straight forward that tracking an extra CPU local clock diff.
John Ogness
RE: [PATCH v3] printk: fix zero-valued printk timestamps in early boot
Quoted 27 lines
-----Original Message-----
From: John Ogness <[email protected]>
Hi Tim,Quoted 18 lines · Tim
Yeah - I was worried about other timestamps not being in synchronization with my adjusted printk timestamps. Looks like that worry was justified.
At this point, there are two avenues:
1) double-down and embed the offset (from power-on rather than from
time_init) all the way intolocal_clock()and/or whatever is providingCLOCK_MONOTONICandCLOCK_BOOTTIME, or2) back off, and abandon adding the offset to local_clock()-based printk timestamps. This would leave a discontinuity when
EARLY_PRINTK_TIMESwas enabled, between the (now) non-zero early printk timestamps and the ones followingtime_init(). This has the benefit of changing less code, and only affecting the early printk timestamps (and none of the rest of the system). And it has the downside of leaving the possibly confusing time discontinuity early in the kernel log. So far, I haven't seen any tools confused by this, and I can put a message beforetime_init()to inform humans about the switch.Quoted 3 lines
The purpose of this patch is really focused on that early period of boot, where all other timing and tracing mechanisms are unavailable, and limiting the impact to just those early (currently zero) timestamps seems like the best course.
Quoted 8 lines
I have always felt that printk timestamping should be using a userspace-accessible clock, such as
CLOCK_MONOTONIC, rather than the CPU local clock. This simplifies applications coordinating their own logs with raw kernel logs.I was wondering if your pre-boot timing could be used as the init values for
CLOCK_MONOTONIC, so thatCLOCK_MONOTONICis a clean continuation of your pre-boot clocking.
That's interesting. It does sound like it would solve the discontinuity problem. However, this does fall in the 'double-down' category of changes,
(ie embedding the offset from power-on provided by early cycles into CLOCK_MONOTONIC, and making additional changes to printk to use that clock.)
I would have to do some more research on the clock system for this, and it's getting a bit afield of my little patch to support getting timing info on 20 to 60 early boot printks.
Quoted 5 lines
And then we could use this opportunity to switch printk to
CLOCK_MONOTONIC.This might also make sense if initializing
CLOCK_MONOTONICis somehow more straight forward than tracking an extra CPU local clock diff.
I'd have to figure out what clock is providing CLOCK_MONOTONIC, and see how the initialization works. In terms of complications, I'd guess it's about equal. The code in my patch to add the early_times offset (early_ts_offset) should resolve down to a single inline add (inside adjust_early_ts, inside print_time).
This add only happens for printk timestamps, so the overall effect on the system is quite limited. If we put the offset add into CLOCK_MONOTONIC, it will affect all users (which, of course, is the point in order to harmonize timestamps between printk and user-space, and possibly other timestamp users in the kernel). But it makes the effect more general. Since the purpose of the patch was just to allow me to get timestamps for a small number of pre-time_init() printks, I really wanted to minimize any long-term runtime impacts.
Having said that, I think it's likely there's already some offset calculation or scaling that this might fold into for CLOCK_MONOTIC, where adjusting the offset to be from power-on rather than from initializing of timekeeping would generate no actual increase in the overhead of getting the CLOCK_MONOTONIC value.
I'll do some research and see if what I can find out, to see if that makes sense. Overall, I think it would be a net positive if:
- printk and user-space timestamps were harmonized
- the time base for the system (0-time) was from power-on rather than from kernel timekeeping initialization.
Thanks for the ideas and feedback.
-- Tim
RE: [PATCH v3] printk: fix zero-valued printk timestamps in early boot
In reply to Petr MladekQuoted 6 lines
-----Original Message-----
From: Petr Mladek <[email protected]>
Finally added timekeeping maintainers and John into Cc. We should have added time since v1.
Thanks for catching that. I'll try to remember to add those (and use John Ogness' correct email) going forward.
Quoted 69 lines
Anyway, you might see the entire history at
https://lore.kernel.org/all/[email protected]/Quoted 48 lines · Tim
Hey Micheal,
This report is very interesting.
Thanks very much for trying it out!
Quoted 42 lines
-----Original Message-----
From: Michael Kelley <[email protected]>
Sent: Wednesday, March 11, 2026 9:47 AM
From: Tim Bird <[email protected]> Sent: Tuesday, February 10, 2026 3:48 PMQuoted 14 lines
During early boot, printk timestamps are reported as zero before kernel timekeeping starts (e.g. before
time_init()). This hinders boot-time optimization efforts. This period is about 400 milliseconds for many current desktop and embedded machines running Linux.Add support to save cycles during early boot, and output correct timestamp values after timekeeping is initialized.
get_cycles()is operational on arm64 andx86_64from kernel start. Add code and variables to save calibration values used to later convert cycle counts to time values in the early printks. Add a config to control the feature.Quoted 7 lines
This yields non-zero timestamps for printks from the very start of kernel execution. The timestamps are relative to the start of the architecture-specified counter used in
get_cycles(e.g. the TSC onx86_64andcntvct_el0on arm64).All timestamps reflect time from processor power-on instead of time from the kernel's timekeeping initialization.
I tried this patch in linux-next20260302 kernel running as a guest VM on a Hyper-V host. Two things:
1) In the dmesg output, I'm seeing a place where the timestamps briefly go backwards -- i.e., they are not monotonically increasing. Here's a snippet, where there's a smaller timestamp immediately after the tsc is detected:
[ 27.994891] SMBIOS 3.1.0 present. [ 27.994893] DMI: Microsoft Corporation Virtual Machine/Virtual Machine, BIOS Hyper-V UEFI Release v4.1 09/25/2025 [ 27.994898] DMI: Memory slots populated: 2/2 [ 27.995202] Hypervisor detected: Microsoft Hyper-V [ 27.995205] Hyper-V: privilege flags low 0xae7f, high 0x3b8030, ext 0x62, hints 0xa0e24, misc 0xe0bed7b2 [ 27.995208] Hyper-V: Nested features: 0x0 [ 27.995209] Hyper-V: LAPIC Timer Frequency: 0xc3500 [ 27.995210] Hyper-V: Using hypercall for remote TLB flush [ 27.995216] clocksource: hyperv_clocksource_tsc_page: mask: 0xffffffffffffffff max_cycles: 0x24e6a1710, max_idle_ns: 440795202120ns
Quoted 13 lines
Quoted 2 lines
[ 27.995218] clocksource: hyperv_clocksource_msr: mask: 0xffffffffffffffff max_cycles: 0x24e6a1710, max_idle_ns: 440795202120 ns [ 27.995220] tsc: Detected 2918.401 MHz processorI wonder if the tsc is getting fiddled with or virtualized somewhere in here, as part of clocksource initialization. I believe each clocksource in the kernel maintains it's own internal offset, and maybe the offset that is being used ends up being slightly different from the cycle-counter offset that the
early_timesfeature uses. I'm just throwing out guesses. It's about a 4ms delta, which is pretty big.Quoted 5 lines
[ 27.991060] e820: update [mem 0x00000000-0x00000fff] System RAM ==> device reserved [ 27.991062] e820: remove [mem 0x000a0000-0x000fffff] System RAM [ 27.991064] last_pfn = 0x210000 max_arch_pfn = 0x400000000 [ 27.991065] x86/PAT: PAT support disabled because CONFIG_X86_PAT is disabled in the kernel. [ 27.991066] x86/PAT: Configuration [0-7]: WB WT UC- UC WB WT UC- UC
Quoted 3 lines
I wonder how the calibration of the cycles is precise. I wonder if the problem might be that cycles were faster right after boot than later during the calibration.
On x86_64 TSC has been stable on all processors produced since about 2008, when invariant TSC were introduced. They shouldn't be changing speeds or doing weird things.
Quoted 54 lines
I added the following debug output on top of this patch:
include/linux/early_times.h +1
-0@@ -20,6 +20,7 @@ static inline void early_times_start_calibration(void) { start_cycles =get_cycles (); start_ns =local_clock (); +pr_info ("Early printk times: started callibration: %llu ns\n" , start_ns); }static inline void early_times_finish_calibration (void )kernel/printk/printk.c +2
-0@@ -2308,6 +2308,8 @@ int vprintk_store(int facility, int level, ts_nsec =local_clock ();if (!ts_nsec) ts_nsec =early_cycles (); +else +pr_info_once ("local_clock() returned non-zero timestamp: %llu nsec\n" , ts_nsec); caller_id =printk_caller_id ();And it produced in my kvm:
Let's say that start of the cycle counter is
Start of stage A
[ 8.684438] Linux version 7.0.0-rc2-default+ (pmladek@pathway) (gcc (SUSE Linux) 15.2.1 20260202, GNU ld (GNU Binutils; openSUSE Tumbleweed) 2.45.0.20251103-2) #571 SMP PREEMPT_DYNAMIC Fri Mar 13 10:23:54 CET 2026 [ 8.684442] Command line: BOOT_IMAGE=/boot/vmlinuz-7.0.0-rc2-default+ root=/dev/vda2 resume=/dev/disk/by-uuid/369c7453-3d16- 409d-88b2-5de027891a12 mitigations=auto nosplash earlycon=uart8250,io,0x3f8,115200 console=ttyS0,115200 console=tty0 ignore_loglevel log_buf_len=1M crashkernel=512M,high crashkernel=72M,low [...] [ 8.696633] earlycon: uart8250 at I/O port 0x3f8 (options '115200') [ 8.696639] printk: legacy bootconsole [uart8250] enabled [ 8.731303] printk: debug: ignoring loglevel setting. [ 8.732349] NX (Execute Disable) protection: active [ 8.733447] APIC: Static calls initialized [ 8.734667] SMBIOS 2.8 present. [ 8.735358] DMI: QEMU Standard PC (Q35 + ICH9, 2009), BIOS rel-1.17.0-2-g4f253b9b-prebuilt.qemu.org 04/01/2014 [ 8.737285] DMI: Memory slots populated: 1/1 [ 8.738380] Hypervisor detected: KVM [ 8.739151] last_pfn = 0x7ffdc max_arch_pfn = 0x400000000 [ 8.740254] kvm-clock: Using msrs 4b564d01 and 4b564d00 [ 8.732971] printk: local_clock() returned non-zero timestamp: 3486 nsec
Quoted 4 lines
End of stage A
This is the point where
printk()started storing the values fromlocal_clock()instead of cycles.
Thanks for measuring this.
That's suspiciously close to the approximate 4ms different that Michael saw. It may be that my offset calculation is off. Michael also mentioned that the math seemed to be off on the calibration. I'll review the math and run some tests and see if I can figure out where the problem is. Another option
is to just eliminate the early_ts_offset, and live with the discontinuity going from cycle-based timestamps to local-clock()-based timestamps.
Quoted 44 lines
Start of stage B
[ 8.732971] kvm-clock: using sched offset of 252367014082295 cycles [ 8.735471] clocksource: kvm-clock: mask: 0xffffffffffffffff max_cycles: 0x1cd42e4dffb, max_idle_ns: 881590591483 ns [ 8.738880] tsc: Detected 3293.776 MHz processor [ 8.740474] e820: update [mem 0x00000000-0x00000fff] System RAM ==> device reserved [...] [ 8.932671] rcu: srcu_init: Setting srcu_struct sizes based on contention. [ 8.934047] Early printk times: started callibration: 201079507 nsEnd of stage B
This is where we started calibration of early cycles.
Start of stage C
[ 8.935571] Console: colour dummy device 80x25 [...] [ 9.289673] thermal_sys: Registered thermal governor 'fair_share' [ 9.290077] thermal_sys: Registered thermal governor 'bang_bang' [ 9.292290] thermal_sys: Registered thermal governor 'step_wise' [ 9.293530] thermal_sys: Registered thermal governor 'user_space' [ 9.294856] cpuidle: using governor ladder [ 9.296302] cpuidle: using governor menuHere the thermal governors are registered. I guess that they might reduce speed of some HW.
[...]
[ 11.974147] clk: Disabling unused clocksSome unused clocks are disabled. I wonder if this might affect counting the cycles.
[ 12.330852] Freeing unused kernel image (rodata/data gap) memory: 1500K [ 12.351191] Early printk times: mult=19634245, shift=26, offset=8732967929 nsEnd of stage C
Here is the end on calibration.
Now, if the frequence of the cycles was:
Quoted 8 lines
+ was higher in the stage A when only cycles were stored
+ was lower in stage C when it was calibrated againstlocal_clock()Then it might result in higher (calibrated) timestamps in stage A and step back in stage B.
Or something like this. It is possible that even
local_clock()does not have a stable frequence during the early boot.
Most x86_64 platforms are using TSC as the basis for local_clock(), and it should be stable (have invariant frequency) on all modern processors.
In the vast majority of cases, I expect that the cycles being used forearly_cycles is coming from the exact same base hardware (the TSC) as local_clock(), so there shouldn't be discrepancies.
(Someone with more clock expertise than me, please correct me if I'm wrong.
Even on virtualized systems, the underlying hardware for the TSC instruction is still just the real TSC hardware on the physical platform).
I suspect that I just got the math wrong somewhere, or there's an unaccounted-for delay somewhere I didn't take into account when calculating the offset. I'll review it and let you know what I find.
Quoted 3 lines
Idea: A solution might be to start calibration when
printk()
gets first non-zero time fromlocal_clock.
Actually, I want the values used in the calibration to have accumulated enough time for some accuracy to set in. If I grab local_clock() too close to zero, then the math is less accurate.
Quoted 84 lines
Something like:
include/linux/early_times.h +5
-2@@ -16,10 +16,13 @@ extern u64 start_ns;extern u32 early_mult, early_shift;extern u64 early_ts_offset; -static inline void early_times_start_calibration (void ) +static inline void early_times_may_start_calibration (u64 ts_ns) { +if (start_ns) +return ; + + start_ns = ts_ns; start_cycles =get_cycles (); - start_ns =local_clock (); }static inline void early_times_finish_calibration (void )init/main.c +0
-3@@ -1123,9 +1123,6 @@ void start_kernel(void)timekeeping_init ();time_init (); -/* This must be after timekeeping is initialized */ -early_times_start_calibration (); -/* This must be after timekeeping is initialized */ random_init ();kernel/printk/printk.c +3
-1@@ -2306,7 +2306,9 @@ int vprintk_store(int facility, int level,* timestamp with respect to the caller. */ ts_nsec =local_clock (); -if (!ts_nsec) +if (ts_nsec) +early_times_may_start_calibration (ts_nsec); +else ts_nsec =early_cycles (); caller_id =printk_caller_id ();Quoted 23 lines
Quoted 23 lines
2) A Linux VM running in the Azure cloud is also running on Hyper-V. Such a VM typically uses cloud-init to set everything up at boot time, and cloud-init is outputting lines to the serial console with a timestamp that looks like the
printk()timestamp, but apparently is not adjusted for the early timestamping that this patch adds. Again, I haven't debugged what's going on -- I'm not immediately sure of the mechanism that cloud-init uses to do output to the serial console. The use of the Hyper-V synthetic clock source might the cause of the problem here as well. Here's an output snippet from the serial console:[ 20.330414] systemd[1]: Condition check resulted in OpenVSwitch configuration for cleanup being skipped. [ 20.332911] systemd[1]: Condition check resulted in Set Up Additional Binary Formats being skipped. [ 20.333257] pstore: Registered efi_pstore as persistent store backend [ 20.334360] systemd[1]: Condition check resulted in File System Check on Root Device being skipped. [ 20.338319] systemd[1]: Starting Load Kernel Modules... [ 20.341094] systemd[1]: Starting Remount Root and Kernel File Systems... [ 20.350993] systemd[1]: Starting udev Coldplug all Devices... [ 20.356255] systemd[1]: Starting Uncomplicated firewall... [ 20.361536] systemd[1]: Started Journal Service. [ 20.386902] EXT4-fs (sda1): re-mounted c02dce0c-0c40-4e6e-88af-c5a0987b0adb r/w. [ 22.532033] /dev/sr0: Can't lookup blockdevQuoted 8 lines
Quoted 8 lines
[ 7.955973] cloud-init[783]: Cloud-init v. 24.3.1-0ubuntu0~20.04.1 running 'init-local' at Wed, 11 Mar 2026 15:27:06 +0000. Up 7.48 seconds. [ 9.933120] cloud-init[822]: Cloud-init v. 24.3.1-0ubuntu0~20.04.1 running 'init' at Wed, 11 Mar 2026 15:27:08 +0000. Up 9.82 seconds. [ 9.935483] cloud-init[822]: ci-info: ++++++++++++++++++++++++++++++++++++++Net device info+++++++++++++++++++++++++++++++++++++++ [ 9.937726] cloud-init[822]: ci-info: +--------+------+-----------------------------+---------------+--------+-------------------+ [ 9.939905] cloud-init[822]: ci-info: | Device | Up | Address | Mask | Scope | Hw-Address | [ 9.942059] cloud-init[822]: ci-info: +--------+------+-----------------------------+---------------+--------+-------------------+
Quoted 2 lines
This is more complicated. I wonder if the timestamps from cloud-init() are somehow synchronized with
local_clock().
This raises an important issue, which is that anyone using local_clock() will get timestamps relative to time_init(), whereas with the early times patch applied, printk is using timestamps relative to power on (well, TSC init which is almost always the same thing). I'm still a bit confused with how cloud-init is getting it's messages intermingled with the printk messages? Is this a feature of systemd?
Quoted 2 lines
We might need to synchronize
local_clock()with the cycles as well.
I thought about modifying local_clock to use early_ts_offset, so that all users of local_clock() would get timestamps with an offset adjusted so they were relative to power on, but that was a much more intrusive patch.
Quoted 9 lines
But there is the chicken&egg problem. We need:
+ to know the offset caused by cycles when
local_clock()gets initialized.
+local_clock()running for some time to calibrate cycles.Hmm, I see that time-management people are not in Cc. We should have added them since v1.
I add them now. Better late than never.
Thanks for the feedback and testing! I really appreciate it.
-- Tim
Thomas Gleixner
maintainerRe: [PATCH v3] printk: fix zero-valued printk timestamps in early boot
In reply to Petr MladekQuoted 4 lines · Petr Mladek
[ 9.296302] cpuidle: using governor menuHere the thermal governors are registered. I guess that they might reduce speed of some HW.
No. The frequency of the TSC and KVM clock are stable. For TSC that's only true for CPUs which have X86_FEATURE_CONSTANT_TSC set, which is the case for every x86 CPU shipped in the last 20 years.
Thanks,
tglx
Thomas Gleixner
maintainerRe: [PATCH v3] printk: fix zero-valued printk timestamps in early boot
In reply to Tim BirdQuoted 13 lines · Tim Bird
During early boot, printk timestamps are reported as zero before kernel timekeeping starts (e.g. before
time_init()). This hinders boot-time optimization efforts. This period is about 400 milliseconds for many current desktop and embedded machines running Linux.Add support to save cycles during early boot, and output correct timestamp values after timekeeping is initialized.
get_cycles()is operational on arm64 andx86_64from kernel start. Add code and variables to save calibration values used to later convert cycle counts to time values in the early printks. Add a config to control the feature.
Quoted 7 lines
This yields non-zero timestamps for printks from the very start of kernel execution. The timestamps are relative to the start of the architecture-specified counter used in
get_cycles(e.g. the TSC onx86_64andcntvct_el0on arm64).All timestamps reflect time from processor power-on instead of time from the kernel's timekeeping initialization.
Can we pretty please _not_ introduce yet another side channel to generate time stamps?
printk()
time_ns = local_clock();
local_clock()
local_clock_noinstr()
// After boot
if (static_branch_likely(&__sched_clock_stable)) return sched_clock_noinstr() + __sched_clock_offset; // Before sched_clock_init() if (!static_branch_likely(&sched_clock_running)) return sched_clock_noinstr(); clock = sched_clock_local(this_scd());
On x86:sched_clock_noinstr()
// bare metal
native_sched_clock()
// After TSC calibration
if (static_branch_likely(&__use_tsc)) {
...
}// Jiffies fallback.
So the obvious solution is to expand the fallback with:
if (tsc_available())
return tsc_early_uncalibrated();
return jiffies ....;As this needs to be supported by the architecture/platform in any case there is close to zero benefit from creating complicated generic infrastructure for this.
Thanks,
tglx
RE: [PATCH v3] printk: fix zero-valued printk timestamps in early boot
Hey Thomas,
Thanks for the review...
Quoted 21 lines
-----Original Message-----
From: Thomas Gleixner <[email protected]>Quoted 18 lines · Tim Bird
During early boot, printk timestamps are reported as zero before kernel timekeeping starts (e.g. before
time_init()). This hinders boot-time optimization efforts. This period is about 400 milliseconds for many current desktop and embedded machines running Linux.Add support to save cycles during early boot, and output correct timestamp values after timekeeping is initialized.
get_cycles()is operational on arm64 andx86_64from kernel start. Add code and variables to save calibration values used to later convert cycle counts to time values in the early printks. Add a config to control the feature.This yields non-zero timestamps for printks from the very start of kernel execution. The timestamps are relative to the start of the architecture-specified counter used in
get_cycles(e.g. the TSC onx86_64andcntvct_el0on arm64).
Quoted 5 lines
Quoted 2 lines
All timestamps reflect time from processor power-on instead of time from the kernel's timekeeping initialization.
Can we pretty please _not_ introduce yet another side channel to generate time stamps?
Well, this is using get_cycles(), which already exists on most architectures. This patch just adds a funky way to use cycles (which are available from power on, rather than from the start of kernel timekeeping) to allow saving timing data for some early printks (usually about 20 to 60 printks).
Also, my current plan is to back off of adjusting the offset of unrelated (non-pre-time_init()) printks, and limit the effect in the system to just those first early (pre-time_init()) printks. The complication to add an offset to all following printks was just to avoid a discontinuity in printk timestamps, once time_init() was called and "real" timestamps started producing non-zeros. Given how confusing this seems to have made things, I'm thinking of backing
off of that approach.
Quoted 4 lines
printk()time_ns = local_clock();
that's ts_nsec = local_clock()
Quoted 26 lines
local_clock()
local_clock_noinstr()
// After bootif (static_branch_likely(&__sched_clock_stable)) return sched_clock_noinstr() + __sched_clock_offset; // Before sched_clock_init() if (!static_branch_likely(&sched_clock_running)) return sched_clock_noinstr(); clock = sched_clock_local(this_scd());On x86:
sched_clock_noinstr()
// bare metal
native_sched_clock()// After TSC calibration if (static_branch_likely(&__use_tsc)) { ... }// Jiffies fallback.
So the obvious solution is to expand the fallback with:
Quoted 8 lines
if (tsc_available()) return tsc_early_uncalibrated(); return jiffies ....;As this needs to be supported by the architecture/platform in any case there is close to zero benefit from creating complicated generic infrastructure for this.
The problem with this is that tsc_early_uncalibrated() can't return nanoseconds until after calibration.
I don't think it's a good idea to returns cycles sometimes and nanoseconds at other times, from a deep-seated timing function like this.
Also tsc_available() might itself depend on initialization that hasn't happened yet (in early boot).
My approach of saving cycles in ts_nsec for the early printks works because there's a limited number of places (only 2) inside the printk code where ts_nsec is accessed, meaning that the code to detect a cycles value instead of a nanoseconds value can be constrained to just those two places. Basically, I'm doing the conversion from cycles to nanoseconds at printk presentation time, rather than at the time of printk message submission.
The approach that I originally started with
(see https://lore.kernel.org/linux-embedded/[email protected]/
was to use hardcoded multiplier and shift values for converting from cycles
to nanoseconds. These multiplier and shift values would be set at kernel configuration time (ie, using CONFIG values). This approach allows generating nanosecond values from TSC cycles from the very first kernel instruction, and hence removes the need for a calibration step at all (but of course, it requires boot-time-optimizing developers to build a custom kernel for their machine). This might be a reasonable approach, as the number of people working on reducing the boot-time of this section of the kernel (pre-time_init()), is pretty small, IMHO. Also, the patch for that was much simpler than this one.
There are other approaches, but none really work early enough in the kernel boot
to not be a pain. The goal is to provide timing info before: timekeeping init,
jiffies startup, and even CPU features determination, and to keep the effect narrow -- limited only to printks, and the first few pre-time_init() printk messages, at that.
I'm now researching a suggestion from Shashank Balaji to use the existing calibration data from tsc initialization, which might simplify the current patch even further. I'll make sure to CC you on the next version of the patch.
Thanks for your review and feedback of this feature.
-- Tim
Thomas Gleixner
maintainerRE: [PATCH v3] printk: fix zero-valued printk timestamps in early boot
Tim!
Quoted 1 line · Tim Bird
Well, this is using
get_cycles(), which already exists on most architectures.
The fact that get_cycles() exists does not make it a good choice. There is a reason why anything which deals with even remotely reliable time requirements stopped using it. It's still there as a low level architecture specific interface and most other usage is purely historical or wrong to begin with and should be removed completely.
A lot of people spent a significant amount of time to get rid of this ill defined mechanism and it's just sad that they did not manage to eliminate it completely.
Quoted 4 lines
This patch just adds a funky way to use cycles (which are available from power on, rather than from the start of kernel timekeeping) to allow saving timing data for some early printks (usually about 20 to 60 printks).
I can see that, but I'm not accepting yet another ill defined glued on mechanism which relies on a historical ill defined mistake.
Quoted 8 lines
Also, my current plan is to back off of adjusting the offset of unrelated (non-pre-time_init()) printks, and limit the effect in the system to just those first early (pre-time_init()) printks. The complication to add an offset to all following printks was just to avoid a discontinuity in printk timestamps, once
time_init()was called and "real" timestamps started producing non-zeros. Given how confusing this seems to have made things, I'm thinking of backing off of that approach.
This discontinuity results from the fact that you glued it into the printk code and sched_clock() does not know about it.
Quoted 4 lines
Quoted 3 lines
printk()time_ns = local_clock();that's
ts_nsec=local_clock()
That obviously changes the illustrative nature of my narrative significantly. Thanks for pointing it out.
Quoted 6 lines
Quoted 3 lines
As this needs to be supported by the architecture/platform in any case there is close to zero benefit from creating complicated generic infrastructure for this.
The problem with this is that
tsc_early_uncalibrated()can't return nanoseconds until after calibration.
In theory it could for most modern x86 CPUs as they advertise the nominal TSC frequency in CPUID. Other architectures have well known clocksource frequencies, e.g. S390 has a known nominal frequency of 1GHz (IIRC).
But that does not solve any of the other problems. See below.
Quoted 4 lines
I don't think it's a good idea to returns cycles sometimes and nanoseconds at other times, from a deep-seated timing function like this.
Alsotsc_available()might itself depend on initialization that hasn't happened yet (in early boot).
Access to the TSC requires the X86_FEATURE_TSC bit being set, which happens in early_cpu_init(). Before that get_cycles() returns firmly 0.
Quoted 7 lines
My approach of saving cycles in
ts_nsecfor the early printks works because there's a limited number of places (only 2) inside the printk code wherets_nsecis accessed, meaning that the code to detect a cycles value instead of a nanoseconds value can be constrained to just those two places. Basically, I'm doing the conversion from cycles to nanoseconds at printk presentation time, rather than at the time of printk message submission.
I know, but that again requires to add more ill defined infrastructure.
We are not aiming to add more, we want to get rid of it completely to the extent possible.
Quoted 5 lines
The approach that I originally started with
(see https://lore.kernel.org/linux-embedded/[email protected]/
was to use hardcoded multiplier and shift values for converting from cycles to nanoseconds. These multiplier and shift values would be set at kernel configuration time (ie, using CONFIG values).
Which makes it unusable for distro kernels and therefore a non-starter.
Quoted 4 lines
There are other approaches, but none really work early enough in the kernel boot to not be a pain. The goal is to provide timing info
before: timekeeping init, jiffies startup, and even CPU features determination,
As I pointed out before that's wishful thinking:
You _cannot_ access a resource before it has been determined to be available.
Period.
It does not matter at all if _you_ know for sure that it is the case in _your_ personal setup.
Quoted 2 lines
and to keep the effect narrow -- limited only to printks, and the first few pre-time_init() printk messages, at that.
Either it is solved in a generic way or we have to agree that it's not solvable at all. Your narrow effect argument is bogus and you know that very well.
Quoted 4 lines
I'm now researching a suggestion from Shashank Balaji to use the existing calibration data from tsc initialization, which might simplify the current patch even further. I'll make sure to CC you on the next version of the patch.
If you want to use the calibration data from tsc_early_init() then you achieve exactly _nothing_ because tsc_early_init() also enables the early sched clock on bare metal. On a VM with KVM clock available the KVM clock setup enables the early sched clock even before that via init_hypervisor_platform().
The early TSC init happens in setup_arch() via tsc_early_init() and it's completely unclear whether you can always access the TSC safely before that unconditionally due to SNP, which requires to enable the secure TSC first. There is a reason why all of this is ordered the way it is.
While reading TSC way before that might work on bare metal and in most VMs, it's not guaranteed to be safe unconditionally unless someone sits down and provides proof to the contrary. As always I'm happy to be proven wrong.
When tsc_early_init() was introduced for the very same reason you are looking into that, quite some people spent a lot of time to come up with a solution which was deemed safe enough to be used unconditionally.
Please consult the LKML archive for the full history. The commit links will give you a proper starting point.
That said, I completely understand the itch you are trying to scratch and I'm the least person to prevent an architecturally sound solution, but I'm also the first person to NAK any attempt which is based on uninformed claims and 'works for me' arguments.
The only clean way to solve this cleanly is moving the sched clock initialization to the earliest point possible and accepting that due to hardware, enumeration and virtualization constraints this point might be suboptimal. Everything else is just an attempt to defy reality.
Thanks,
tglx
Thomas Gleixner
maintainerRE: [PATCH v3] printk: fix zero-valued printk timestamps in early boot
Tim!
Quoted 4 lines · Thomas Gleixner
The only clean way to solve this cleanly is moving the sched clock initialization to the earliest point possible and accepting that due to hardware, enumeration and virtualization constraints this point might be suboptimal. Everything else is just an attempt to defy reality.
As the wheather was lousy, I sat down for a couple of hours and implemented the obviously incomplete PoC below for illustration.
As the FIXME comment in tsc_early_boot_setup() tells clearly, this is far from complete and needs way more thoughts and scrutiny, but it's good enough to prove the point:
[ 0.000019] tsc: Boot sched clock enabled
As early as possible, but with at least the minimal safe guards in place.
[ 0.000041] Linux version 7.0.0-rc5+ (tglx@tglx-pladt) (gcc (Debian 14.2.0-19) 14.2.0, GNU ld (GNU Binutils for Debian) 2.44) #893 SMP PREEMPT_DYNAMIC Sun Mar 29 21:38:08 CEST 2026 [ 0.000043] Command line: BOOT_IMAGE=/boot/vmlinuz-7.0.0-rc5+ root=UUID=fa44d501-1716-402a-8602-6315c3166f7b ro console=tty0 console=ttyS0,115200n8 ftrace_dump_on_oops earlyprintk=ttyS0,115200n8 tsc_early_khz=2095070 no-kvmclock
1) tsc_early_khz is required because KVM does not provide the frequency leaf.
2) no-kvmclock is required because KVM thinks it is more important than everything else.
Both issues are completely unrelated to the problem at hand.
[ 0.009415] BIOS-provided physical RAM map: [ 0.009416] BIOS-e820: [mem 0x0000000000000000-0x000000000009fbff] System RAM ... [ 0.009473] printk: legacy bootconsole [earlyser0] enabled ... [ 0.021037] Hypervisor detected: KVM
Without 'no-kvmclock' KVM would take over sched clock and reset sched clock time to 0 because KVM... Trivial problem to be solved and left as an exercise for the reader.
[ 0.021256] last_pfn = 0x7ffdb max_arch_pfn = 0x400000000 [ 0.051429] tsc: Fast TSC calibration using PIT [ 0.051699] tsc: Detected 2094.808 MHz processor [ 0.051990] tsc: Detected 2095.070 MHz TSC
That's in tsc_early_init() which enables the sched clock static key.
[ 0.052871] e820: update [mem 0x00000000-0x00000fff] System RAM ==> device reserved
Unsurprisingly time continues without a gap, jumping backwards or whatever.
If you want to have early timestamps then you have to go through the low level code on every architecture and do the proper sanity checks, enumerations, etc. no matter what. There is no guarantee that you can use any clock unconditionally during very early boot.
As you have to do that anyway, there is _zero_ justification for an extra facility side stepping sched clock, which would just create another pile of technical debt.
Quite the contrary, I'm going to get rid of technical debt:
The get_cycles() related changes in tsc.h are going to end up in a obviously revised formal patch tomorrow as there is exactly _zero_ requirement to provide this as a functional interface.
1) The default implementation in asm-generic returns 0
Ergo any code depending on a functional implementation is broken by definition.
2) The ops/cycles metrics are as bogus as the infamous BogoMIPS metrics
The "cycle" counter runs on almost all contemporary CPUs at a fixed frequency, which is completely unrelated to the actual CPU frequency and therefore to the actual CPU cycles.
The only realistic metric is ops/sec and nothing else and that can be trivially achieved by using the generic time getter interfaces.
Those might end up resulting in bogus benchmark results too if the underlying clocksource is jiffies, but that's again a matter of accepting reality.
If people really mandate that ops/bogo_cycles is required for the very wrong reasons, then I'm happy to bring it back with a global name change from get_cycles() to get_bogo_cycles() which excludes it from any serious usage including printk.
Thanks,
tglx
--- arch/x86/include/asm/tsc.h | 11 ++-- arch/x86/kernel/platform-quirks.c | 2 arch/x86/kernel/tsc.c | 96 ++++++++++++++++++++++++++++++-------- 3 files changed, 85 insertions(+), 24 deletions(-)
arch/x86/include/asm/tsc.h +7 -4
@@ -76,10 +76,7 @@ extern void disable_TSC(void);static inline cycles_t get_cycles (void ) { -if (!IS_ENABLED (CONFIG_X86_TSC ) && - !cpu_feature_enabled (X86_FEATURE_TSC )) -return 0 ; -return rdtsc (); +return 0 ; }#define get_cycles get_cycles
@@ -114,6 +111,12 @@ static inline void tsc_verify_tsc_adjuststatic inline void check_tsc_sync_target (void ) { }#endif +#if defined(CONFIG_X86_TSC ) && defined(CONFIG_X86_64 ) +void tsc_early_boot_setup (void ); +#else +static inline void tsc_early_boot_setup (void ) { } +#endif +extern int notsc_setup (char *);extern void tsc_save_sched_clock_state (void );extern void tsc_restore_sched_clock_state (void );
arch/x86/kernel/platform-quirks.c +2 -0
@@ -32,6 +32,8 @@ void __init x86_early_init_platform_quirif (x86_platform.set_legacy_features) x86_platform.set_legacy_features (); + +tsc_early_boot_setup (); }bool __init x86_pnpbios_disabled (void )
arch/x86/kernel/tsc.c +76 -20
184 lines not on this page · raw message
Re: [PATCH v3] printk: fix zero-valued printk timestamps in early boot
Quoted 42 lines · Thomas Gleixner
Quite the contrary, I'm going to get rid of technical debt:
The
get_cycles()related changes intsc.hare going to end up in a obviously revised formal patch tomorrow as there is exactly _zero_ requirement to provide this as a functional interface.1) The default implementation in asm-generic returns 0
Ergo any code depending on a functional implementation is broken by definition.
2) The ops/cycles metrics are as bogus as the infamous BogoMIPS metrics
The "cycle" counter runs on almost all contemporary CPUs at a fixed frequency, which is completely unrelated to the actual CPU frequency and therefore to the actual CPU cycles.
The only realistic metric is ops/sec and nothing else and that can be trivially achieved by using the generic time getter interfaces.
Those might end up resulting in bogus benchmark results too if the underlying clocksource is jiffies, but that's again a matter of accepting reality.
If people really mandate that ops/bogo_cycles is required for the very wrong reasons, then I'm happy to bring it back with a global name change from
get_cycles()toget_bogo_cycles()which excludes it from any serious usage including printk.Thanks,
tglx
--- arch/x86/include/asm/tsc.h | 11 ++-- arch/x86/kernel/platform-quirks.c | 2 arch/x86/kernel/tsc.c | 96 ++++++++++++++++++++++++++++++-------- 3 files changed, 85 insertions(+), 24 deletions(-)arch/x86/include/asm/tsc.h +0
-0@@ -76,10 +76,7 @@ extern void disable_TSC(void);
arch/x86/include/asm/tsc.h extern void disable_TSC(void); · line 82
Quoted 10 lines
arch/x86/include/asm/tsc.h +1
-4static inline cycles_t get_cycles (void ) { -if (!IS_ENABLED (CONFIG_X86_TSC ) && - !cpu_feature_enabled (X86_FEATURE_TSC )) -return 0 ; -return rdtsc (); +return 0 ; }#define get_cycles get_cycles
So this is probably going to break a bunch of generic stuff. There is a fair amount of 'get_cycles' usage out in the tree.
Mostly it appears to be various test code.
But I do agree that get_cycles() is terrible and should go away.
Re: [PATCH v3] printk: fix zero-valued printk timestamps in early boot
In reply to Thomas GleixnerOn Mon, 30 Mar 2026 00:42:59 +0200
Thomas Gleixner <[email protected]> wrote:
...
Quoted 20 lines
If you want to have early timestamps then you have to go through the low level code on every architecture and do the proper sanity checks, enumerations, etc. no matter what. There is no guarantee that you can use any clock unconditionally during very early boot.
As you have to do that anyway, there is _zero_ justification for an extra facility side stepping sched clock, which would just create another pile of technical debt.
Quite the contrary, I'm going to get rid of technical debt:
The
get_cycles()related changes intsc.hare going to end up in a obviously revised formal patch tomorrow as there is exactly _zero_ requirement to provide this as a functional interface.1) The default implementation in asm-generic returns 0
Ergo any code depending on a functional implementation is broken by definition.
Quoted 8 lines
2) The ops/cycles metrics are as bogus as the infamous BogoMIPS metrics
The "cycle" counter runs on almost all contemporary CPUs at a fixed frequency, which is completely unrelated to the actual CPU frequency and therefore to the actual CPU cycles.
The only realistic metric is ops/sec and nothing else and that can be trivially achieved by using the generic time getter interfaces.
ops/sec is also horrid because you get all all the interrupt time getting in the way.
Additionally it is only any use for relative comparisons done on exactly the same hardware, and is entirely useless is you are trying to show that a short code loop is running at/near the theoretical rate.
The only thing I've found that works is the PERF_COUNT_HW_CPU_CYCLES counter (low 32 bits).
In userspace you do have to directly read the register (rather than using the library function - too much overhead), and ignore any silly values generated by interrupts and process switches.
But it is accurate enough to time single function calls and see the effect of things like mispredicted branches and the data-dependency of divide instructions.
But I do agree that the TSC is entirely useless for measuring what it was originally intended to measure.
David
Quoted 13 lines
Those might end up resulting in bogus benchmark results too if the underlying clocksource is jiffies, but that's again a matter of accepting reality.
If people really mandate that ops/bogo_cycles is required for the very wrong reasons, then I'm happy to bring it back with a global name change from
get_cycles()toget_bogo_cycles()which excludes it from any serious usage including printk.Thanks,
tglx
RE: Earlier tsc init patch (was RE: [PATCH v3] printk: fix zero-valued printk timestamps in early boot)
In reply to Thomas GleixnerThomas,
Thanks for posting this PoC patch. I found the code interesting and informative. Please see my comments below.
Quoted 26 lines
-----Original Message-----
From: Thomas Gleixner <[email protected]>
Tim!Quoted 4 lines · Thomas Gleixner
The only clean way to solve this cleanly is moving the sched clock initialization to the earliest point possible and accepting that due to hardware, enumeration and virtualization constraints this point might be suboptimal. Everything else is just an attempt to defy reality.
As the wheather was lousy, I sat down for a couple of hours and implemented the obviously incomplete PoC below for illustration.
As the FIXME comment in
tsc_early_boot_setup()tells clearly, this is far from complete and needs way more thoughts and scrutiny, but it's good enough to prove the point:[ 0.000019] tsc: Boot sched clock enabledAs early as possible, but with at least the minimal safe guards in place.
[ 0.000041] Linux version 7.0.0-rc5+ (tglx@tglx-pladt) (gcc (Debian 14.2.0-19) 14.2.0, GNU ld (GNU Binutils for Debian) 2.44) #893 SMP PREEMPT_DYNAMIC Sun Mar 29 21:38:08 CEST 2026 [ 0.000043] Command line: BOOT_IMAGE=/boot/vmlinuz-7.0.0-rc5+ root=UUID=fa44d501-1716-402a-8602-6315c3166f7b ro console=tty0console=ttyS0,115200n8
ftrace_dump_on_oopsearlyprintk=ttyS0,115200n8 tsc_early_khz=2095070 no-kvmclock
Quoted 7 lines
1)
tsc_early_khzis required because KVM does not provide the frequency leaf.2) no-kvmclock is required because KVM thinks it is more important than everything else.
Both issues are completely unrelated to the problem at hand.
I don't really want to add more complexity to the boot code, or complicate any other timing mechanisms in the kernel, just to instrument a few early printks.
Adding additional code, command line variables, etc. is antithetical to the goal of reducing boot time and reducing technical debt.
The instrumentation that I want to add is for printks that are currently showing as zero, while also allowing to get useful timing data on temporary printks in that early region of boot.
Quoted 17 lines
[ 0.009415] BIOS-provided physical RAM map: [ 0.009416] BIOS-e820: [mem 0x0000000000000000-0x000000000009fbff] System RAM ... [ 0.009473] printk: legacy bootconsole [earlyser0] enabled ... [ 0.021037] Hypervisor detected: KVMWithout 'no-kvmclock' KVM would take over sched clock and reset sched clock time to 0 because KVM... Trivial problem to be solved and left as an exercise for the reader.
[ 0.021256] last_pfn = 0x7ffdb max_arch_pfn = 0x400000000 [ 0.051429] tsc: Fast TSC calibration using PIT [ 0.051699] tsc: Detected 2094.808 MHz processor [ 0.051990] tsc: Detected 2095.070 MHz TSC
Quoted 5 lines
That's in
tsc_early_init()which enables the sched clock static key.[ 0.052871] e820: update [mem 0x00000000-0x00000fff] System RAM ==> device reservedUnsurprisingly time continues without a gap, jumping backwards or whatever.
That's not a problem I care about.
Quoted 5 lines
If you want to have early timestamps then you have to go through the low level code on every architecture and do the proper sanity checks, enumerations, etc. no matter what. There is no guarantee that you can use any clock unconditionally during very early boot.
For the TSC on x86_64 and cntvct_el0 on arm64, I think you are incorrect. In those architectures I believe you are guaranteed that the instruction to get the cycle counter will work to retrieve a cycles value before any initialization code in the kernel, even under emulation or virtualization. But even if not, I do not believe it matters (see below).
Now, in order to get the clock frequency (and thus convert
to a units/sec timestamp value), additional work may be required (as shown in your POC) OR you need some mechanism to provide the frequency to the kernel
(such as a command line option or kernel config parameter).
However, all this is moot.
I believe if a platform developer can't guarantee that the clock they decide to use for this will be functional when the kernel starts (because either the hardware or the pre-kernel firmware doesn't start it automatically before the kernel loads), then the developer should not use my proposed early times feature.
I don't think this feature has any value for regular production kernels, and it is only intended to be useful for boot-time researchers and developers. Indeed, it's probably better if it is left turned off for production or distro kernels.
Quoted 4 lines
As you have to do that anyway, there is _zero_ justification for an extra facility side stepping sched clock, which would just create another pile of technical debt.
I think this POC adds much more complexity and technical debt than any version of my early-times patch.
Since you want to get rid of technical debt on the use of get_cycles(), I'll re-work the patch to eliminate use of that function, and revert to something similar to my earlier version of the patch that was less complicated.
Quoted 42 lines
Quite the contrary, I'm going to get rid of technical debt:
The
get_cycles()related changes intsc.hare going to end up in a obviously revised formal patch tomorrow as there is exactly _zero_ requirement to provide this as a functional interface.1) The default implementation in asm-generic returns 0
Ergo any code depending on a functional implementation is broken by definition.
2) The ops/cycles metrics are as bogus as the infamous BogoMIPS metrics
The "cycle" counter runs on almost all contemporary CPUs at a fixed frequency, which is completely unrelated to the actual CPU frequency and therefore to the actual CPU cycles.
The only realistic metric is ops/sec and nothing else and that can be trivially achieved by using the generic time getter interfaces.
Those might end up resulting in bogus benchmark results too if the underlying clocksource is jiffies, but that's again a matter of accepting reality.
If people really mandate that ops/bogo_cycles is required for the very wrong reasons, then I'm happy to bring it back with a global name change from
get_cycles()toget_bogo_cycles()which excludes it from any serious usage including printk.Thanks,
tglx
--- arch/x86/include/asm/tsc.h | 11 ++-- arch/x86/kernel/platform-quirks.c | 2 arch/x86/kernel/tsc.c | 96 ++++++++++++++++++++++++++++++-------- 3 files changed, 85 insertions(+), 24 deletions(-)
arch/x86/include/asm/tsc.h extern void disable_TSC(void); · line 79
Quoted 8 lines
arch/x86/include/asm/tsc.h +1
-4@@ -76,10 +76,7 @@ extern void disable_TSC(void);static inline cycles_t get_cycles (void ) { -if (!IS_ENABLED (CONFIG_X86_TSC ) && - !cpu_feature_enabled (X86_FEATURE_TSC )) -return 0 ; -return rdtsc (); +return 0 ;
Eliminating get_cycles() completely seems like an over-reaction to my patch. But it sounds like something you've wanted to eliminate for a while now, and I wouldn't stand in your way, or contribute to the technical debt associated with it by using it in my proposals. By my count there are about 80 current users of get_cycles() outside of arch-specific code, in the 7.0-rc6 kernel.
Quoted 213 lines
arch/x86/include/asm/tsc.h +6
-0}#define get_cycles get_cycles@@ -114,6 +111,12 @@ static inline void tsc_verify_tsc_adjuststatic inline void check_tsc_sync_target (void ) { }#endif +#if defined(CONFIG_X86_TSC ) && defined(CONFIG_X86_64 ) +void tsc_early_boot_setup (void ); +#else +static inline void tsc_early_boot_setup (void ) { } +#endif +extern int notsc_setup (char *);extern void tsc_save_sched_clock_state (void );extern void tsc_restore_sched_clock_state (void );arch/x86/kernel/platform-quirks.c +2
-0@@ -32,6 +32,8 @@ void __init x86_early_init_platform_quirif (x86_platform.set_legacy_features) x86_platform.set_legacy_features (); + +tsc_early_boot_setup (); }bool __init x86_pnpbios_disabled (void )arch/x86/kernel/tsc.c +69
-20@@ -18,6 +18,7 @@#include <linux/static_call.h> #include <asm/cpuid/api.h> +#include <asm/cmdline.h> #include <asm/hpet.h> #include <asm/timer.h> #include <asm/vgtod.h> @@ -48,6 +49,7 @@ EXPORT_SYMBOL(tsc_khz);*/ static int __read_mostly tsc_unstable;static unsigned int __initdata tsc_early_khz; +static bool tsc_early_boot_enabled__ro_after_init ;static DEFINE_STATIC_KEY_FALSE_RO (__use_tsc);@@ -202,12 +204,12 @@ static void set_cyc2ns_scale(unsigned lo/* * Initialize cyc2ns for boot cpu */ -static void __init cyc2ns_init_boot_cpu (void ) +static void __init cyc2ns_init_boot_cpu (unsigned long khz) {struct cyc2ns *c2n =this_cpu_ptr (&cyc2ns);seqcount_latch_init (&c2n->seq); -__set_cyc2ns_scale (tsc_khz,smp_processor_id (),rdtsc ()); +__set_cyc2ns_scale (khz,smp_processor_id (),rdtsc ()); }/* @@ -231,17 +233,24 @@ static void __init cyc2ns_init_secondary } } +static __always_inline u64 __native_sched_clock(void) +{ +u64 tsc_now =rdtsc (); + +/* return the value in ns */ +return __cycles_2_ns (tsc_now); +} +/* * Scheduler clock - returns current time in nanosec units. */ noinstr u64 native_sched_clock(void) { -if (static_branch_likely (&__use_tsc)) { -u64 tsc_now =rdtsc (); +if (static_branch_likely (&__use_tsc)) +return __native_sched_clock (); -/* return the value in ns */ -return __cycles_2_ns (tsc_now); - } +if (tsc_early_boot_enabled) +return __native_sched_clock ();/* * Fall back to jiffies if there's no TSC available: @@ -371,12 +380,12 @@ static u64 tsc_read_refs(u64 *p, int hpeint i;for (i =0 ; i <MAX_RETRIES ; i++) { - t1 =get_cycles (); + t1 =rdtsc ();if (hpet) *p =hpet_readl (HPET_COUNTER ) &0xFFFFFFFF ;else *p =acpi_pm_read_early (); - t2 =get_cycles (); + t2 =rdtsc ();if ((t2 - t1) < thresh)return t2; }@@ -468,13 +477,13 @@ static unsigned long pit_calibrate_tsc(uoutb (latch &0xff ,0x42 );outb (latch >>8 ,0x42 ); - tsc = t1 = t2 =get_cycles (); + tsc = t1 = t2 =rdtsc (); pitcnt =0 ; tscmax =0 ; tscmin =ULONG_MAX ;while ((inb (0x61 ) &0x20 ) ==0 ) { - t2 =get_cycles (); + t2 =rdtsc (); delta = t2 - tsc; tsc = t2;if ((unsigned long ) delta < tscmin)@@ -553,9 +562,9 @@ static inline int pit_expect_msb(unsigneif (!pit_verify_msb (val))break ; prev_tsc = tsc; - tsc =get_cycles (); + tsc =rdtsc (); } - *deltap =get_cycles () - prev_tsc; + *deltap =rdtsc () - prev_tsc; *tscp = tsc;/* @@ -745,19 +754,23 @@ unsigned long native_calibrate_tsc(void)static unsigned long cpu_khz_from_cpuid (void ) { -unsigned int eax_base_mhz, ebx_max_mhz, ecx_bus_mhz, edx; +struct cpuid_regs regs; -if (boot_cpu_data.x86_vendor !=X86_VENDOR_INTEL ) +/* Do it manually as this can be called _before_ boot_cpu_data is initialized */ +cpuid_leaf (0 , ®s); +/* Intel only */ +if (regs.ebx !=0x756e6547 && regs.ecx !=0x6c65746e && regs.edx !=0x49656e69 )return 0 ; -if (boot_cpu_data.cpuid_level <CPUID_LEAF_FREQ ) +if (regs.eax <CPUID_LEAF_FREQ )return 0 ; - eax_base_mhz = ebx_max_mhz = ecx_bus_mhz = edx =0 ; +memset (®s,0 ,sizeof (®s)); -cpuid (CPUID_LEAF_FREQ , &eax_base_mhz, &ebx_max_mhz, &ecx_bus_mhz, &edx); +cpuid_leaf (CPUID_LEAF_FREQ , ®s); -return eax_base_mhz *1000 ; +/* EAX contains base MHz */ +return regs.eax *1000 ; }/* @@ -1512,8 +1525,9 @@ static void __init tsc_enable_sched_cloc/* Sanitize TSC ADJUST before cyc2ns gets initialized */ tsc_store_and_check_tsc_adjust (true); -cyc2ns_init_boot_cpu (); +cyc2ns_init_boot_cpu (tsc_khz);static_branch_enable (&__use_tsc); + tsc_early_boot_enabled = false; }void __init tsc_early_init (void )@@ -1576,6 +1590,48 @@ void __init tsc_init(void)detect_art (); } +/* Only 64bit guarantees that CPUID is available */ +#ifdef CONFIG_X86_64 +void __init tsc_early_boot_setup (void ) +{ +struct cpuid_regs regs; +unsigned long khz =0 ; +char optstr[64 ]; + +if (cmdline_find_option_bool (boot_command_line,"notsc" )) +return ; + +// FIXME: This needs way more sanity checks vs. virt etc. +if (cc_platform_has (CC_ATTR_GUEST_SNP_SECURE_TSC )) +return ; + +// Why don't we have proper leaf structs yet? +cpuid_leaf (0 , ®s); +if (regs.eax <1 ) +return ; + +/* TSC enabled in CPUID? */ +cpuid_leaf (1 , ®s); +if (!(regs.edx &BIT (4 ))) +return ; + +if (cmdline_find_option (boot_command_line,"tsc_early_khz" , optstr,sizeof (optstr)) >0 ) { +// Shut up must check by doing a pointless zeroing +if (kstrtoul (optstr,10 , &khz) <0 ) + khz =0 ; + } + +if (!khz) + khz =cpu_khz_from_cpuid (); +if (!khz) +return ;
arch/x86/kernel/tsc.c void __init tsc_init(void) · line 1637
Quoted 10 lines
arch/x86/kernel/tsc.c +7
-0+ +cyc2ns_init_boot_cpu (khz); + tsc_early_boot_enabled = true; + pr_info("Boot sched clock enabled\n"); +} +#endif +#ifdef CONFIG_SMP /* * Check whether existing calibration data can be reused.
Overall, this code achieves the result of initializing clocks on some x86_64 platforms earlier (and moreso when properly configured via the command line). This achieves a lot of the value I was looking for.
Compared to my solution, it puts pre-timekeeping timestamps into the same time sequence space as subsequent "regular" ones. However, I don't think the discontinuity is actually a big problem.
This PoC does an admirable job of addressing the problem space I am interested in, but at the expense of adding more complexity to the boot sequence.
At the same time, it still leaves a blind spot in the kernel bootup sequence. The blind spot is much shorter, but still exists. And the PoCpatch is unfinished, and supports only one architecture. I wouldn't ask someone else to finish it for me, nor am I willing to commit to extend it (e.g. to finalize support for AMD processors) or maintain it going forward.
The discontinuity in timestamps is a tradeoff I'm willing to make in order keep the code simple and to cover more of the blind sport.
I prefer my original approach. I'll send an updated patch for consideration soon.
Thanks,
-- Tim
RE: [PATCH v3] printk: fix zero-valued printk timestamps in early boot
In reply to Thomas GleixnerThomas,
Your response is both very helpful and a bit frustrating. See below.
Quoted 13 lines
-----Original Message-----
From: Thomas Gleixner <[email protected]>
Tim!Quoted 1 line · Tim Bird
Well, this is using
get_cycles(), which already exists on most architectures.The fact that
get_cycles()exists does not make it a good choice. There is a reason why anything which deals with even remotely reliable time requirements stopped using it. It's still there as a low level architecture specific interface and most other usage is purely historical or wrong to begin with and should be removed completely.
Quoted 4 lines
A lot of people spent a significant amount of time to get rid of this ill defined mechanism and it's just sad that they did not manage to eliminate it completely.
That's good to know. Thanks. For what it's worth, I wouldn't describe my desired usage as being for remotely reliable time requirements.
Quoted 17 lines
Quoted 4 lines
This patch just adds a funky way to use cycles (which are available from power on, rather than from the start of kernel timekeeping) to allow saving timing data for some early printks (usually about 20 to 60 printks).
I can see that, but I'm not accepting yet another ill defined glued on mechanism which relies on a historical ill defined mistake.
Quoted 8 lines
Also, my current plan is to back off of adjusting the offset of unrelated (non-pre-time_init()) printks, and limit the effect in the system to just those first early (pre-time_init()) printks. The complication to add an offset to all following printks was just to avoid a discontinuity in printk timestamps, once
time_init()was called and "real" timestamps started producing non-zeros. Given how confusing this seems to have made things, I'm thinking of backing off of that approach.
Quoted 2 lines
This discontinuity results from the fact that you glued it into the printk code and
sched_clock()does not know about it.
Yes. Of course. That was intentional.
Quoted 8 lines
Quoted 5 lines
Quoted 4 lines
printk()time_ns = local_clock();that's
ts_nsec=local_clock()That obviously changes the illustrative nature of my narrative significantly. Thanks for pointing it out.
I was confused by the variable in your narrative, since I hadn't seen it. I grepped and reexamined the code. I clarified what I believed to be the variable you were referencing. I could have worded this better, but I don't believe you needed to respond with sarcasm.
Quoted 15 lines
Quoted 7 lines
Quoted 4 lines
As this needs to be supported by the architecture/platform in any case there is close to zero benefit from creating complicated generic infrastructure for this.
The problem with this is that
tsc_early_uncalibrated()can't return nanoseconds until after calibration.In theory it could for most modern x86 CPUs as they advertise the nominal TSC frequency in CPUID. Other architectures have well known clocksource frequencies, e.g. S390 has a known nominal frequency of 1GHz (IIRC).
But that does not solve any of the other problems. See below.
Quoted 8 lines
Quoted 4 lines
I don't think it's a good idea to returns cycles sometimes and nanoseconds at other times, from a deep-seated timing function like this.
Alsotsc_available()might itself depend on initialization that hasn't happened yet (in early boot).Access to the TSC requires the
X86_FEATURE_TSCbit being set, which happens inearly_cpu_init(). Before thatget_cycles()returns firmly 0.
Yes.
Quoted 9 lines
Quoted 7 lines
My approach of saving cycles in
ts_nsecfor the early printks works because there's a limited number of places (only 2) inside the printk code wherets_nsecis accessed, meaning that the code to detect a cycles value instead of a nanoseconds value can be constrained to just those two places. Basically, I'm doing the conversion from cycles to nanoseconds at printk presentation time, rather than at the time of printk message submission.I know, but that again requires to add more ill defined infrastructure.
Quite possibly you are right. Are you saying that the general concept of saving cycles to be converted later to nanoseconds is the ill-defined infrastructure, or are you saying using get_cycles() is not safe or accurate enough?
From what you say below, I believe you are saying the latter.
If you don't like the deferred cycles conversion, that's fine. I like the approach you demonstrated in your subsequent PoC patch to show super-early
tsc calibration (using a kernel param). I can certainly live with 14 microseconds of missed optimization information, if that's all it is.
Quoted 3 lines
We are not aiming to add more, we want to get rid of it completely to the extent possible.
OK.
Quoted 8 lines
Quoted 6 lines
The approach that I originally started with
(see https://lore.kernel.org/linux-embedded/[email protected]/
was to use hardcoded multiplier and shift values for converting from cycles to nanoseconds. These multiplier and shift values would be set at kernel configuration time (ie, using CONFIG values).Which makes it unusable for distro kernels and therefore a non-starter.
Can you elaborate on this? Indeed distro kernels would not be able to pre-set TCS calibration values, and would not turn on this feature (in that version of the patch) for production release kernels. But it sounds like you are saying that anything that requires a configuration that is non-general (or indeed used temporarily during development) is not acceptable upstream. Is that your position?
This "feature" is intended as a tool for developers who are optimizing Linux kernel boot time. (I'm not sure who else would be interested in getting timing data for these (currently zero-timestamped) printks during the first 100-400 milliseconds of kernel boot). This would be, I believe, developers who can change their configs and compile their kernels. The v1 version of the patch included calculations and printks to help developers set the calibration values for their hardware, so it was not targeted at my machine only.
This patch is part of a larger effort on my part to help automate boot-time tuning of the kernel. Many other parts of that effort rely on reconfiguration and recompilation of the kernel, which makes the whole thing a development-time effort, not so much a run-time, end-user, or production-level feature. And very much not a thing that can be accomplished with distro-only configs.
Quoted 8 lines
Quoted 5 lines
There are other approaches, but none really work early enough in the kernel boot to not be a pain. The goal is to provide timing info
before: timekeeping init, jiffies startup, and even CPU features determination,As I pointed out before that's wishful thinking:
Quoted 7 lines
You _cannot_ access a resource before it has been determined to be available.
Period.
It does not matter at all if _you_ know for sure that it is the case in _your_ personal setup.
I used get_cycles(), which has a check for availability in it, so the patch didn't access a resource before it was determined to be available.
It sounds like you're responding to my wording above and not the patch itself.
It is not my intent to handle only my personal setup.
Your help to make sure that this feature is as general as possible is much appreciated.
Quoted 6 lines
Quoted 3 lines
and to keep the effect narrow -- limited only to printks, and the first few pre-time_init() printk messages, at that.
Either it is solved in a generic way or we have to agree that it's not solvable at all.
I disagree that solving this limited problem (zero-valued timestamps in early boot) has to be solved in a generic way.
I already limited the solution space to only processors that I believed had reliable, pre-kernel-initialized cycle generators.
I think it's fair to have a specialized solution to a specialized problem, if it can be made to have very limited effect on other code.
I tried to avoid affecting any other timekeeping mechanism (ie re-engineering local_clock), specifically to avoid unwanted side effects.
Quoted 2 lines
Your narrow effect argument is bogus and you know that very well.
This sounds like you believe I am arguing in bad faith. I don't believe that I am.
Quoted 11 lines
Quoted 5 lines
I'm now researching a suggestion from Shashank Balaji to use the existing calibration data from tsc initialization, which might simplify the current patch even further. I'll make sure to CC you on the next version of the patch.
If you want to use the calibration data from
tsc_early_init()then you achieve exactly _nothing_ becausetsc_early_init()also enables the early sched clock on bare metal. On a VM with KVM clock available the KVM clock setup enables the early sched clock even before that viainit_hypervisor_platform().
In the v3 patch, the timing of sched_clock activation is independent of the timing of the use of the tsc calibration values. I don't understand how this comment is relevant to my patch.
Quoted 5 lines
The early TSC init happens in
setup_arch()viatsc_early_init()and it's completely unclear whether you can always access the TSC safely before that unconditionally due to SNP, which requires to enable the secure TSC first. There is a reason why all of this is ordered the way it is.
OK. Thanks for that info. I'll take a look at that.
What happens if you try a rdtsc() before tsc_early_init? If it returns zero, I can live with that. If it faults or returns random data that's a problem.
Quoted 6 lines
While reading TSC way before that might work on bare metal and in most VMs, it's not guaranteed to be safe unconditionally unless someone sits down and provides proof to the contrary. As always I'm happy to be proven wrong.
Quoted 6 lines
When
tsc_early_init()was introduced for the very same reason you are looking into that, quite some people spent a lot of time to come up with a solution which was deemed safe enough to be used unconditionally.Please consult the LKML archive for the full history. The commit links will give you a proper starting point.
OK - thanks for the information and the pointers. get_cycles() does have a CPU features check, so my access to the tsc was not completely unconditional. I'll do some more research here and try to make the ultimate solution as safe as possible.
Quoted 4 lines
That said, I completely understand the itch you are trying to scratch and I'm the least person to prevent an architecturally sound solution, but I'm also the first person to NAK any attempt which is based on uninformed claims and 'works for me' arguments.
Well, one of the purposes of posting patches is to get feedback
on different approaches. I'm honestly not trying to create a solution that only works on my hardware. But neither am I trying to boil the ocean here.
It's not clear to me that there's much harm in having
a single discontinuity early in the printk timestamps. No one has told me yet that printk timestamps absolutely MUST be monotonically increasing, everywhere.
Trying to solve that issue led to a more convoluted solution than my first approach (deferred cycles conversion versus compile-time calibration data).
I also don't think there's much harm if the data for these few printks is unavailable or wrong on some hardware. Maybe that's an area where we differ in opinion.
Quoted 5 lines
The only clean way to solve this cleanly is moving the sched clock initialization to the earliest point possible and accepting that due to hardware, enumeration and virtualization constraints this point might be suboptimal. Everything else is just an attempt to defy reality.
I think we're willing to accept different areas of sub-optimality. I can live with having to configure the feature, and statically configure the TSC clock calibration, at the expense of non-monotonic printk timestamps for the early printks.
Personally, I think that altering sched_clock or any other kernel timekeeping is overkill for this.
I do like the solution proposed in your patch. I considered a solution using an early-parsed kernel command line arg, but didn't know how early I could do that. I'll review your patch. Thanks!
And I appreciate the time you took to educate me about some of the other issues involved (especially secure TSC and possible TSC unavailability, and VM timekeeping issues, which complicate this.)
Regards,
-- Tim
Re: [PATCH v3] printk: fix zero-valued printk timestamps in early boot
Hi TIm,
Quoted 11 lines · Tim
Quoted 11 lines
From: Thomas Gleixner <[email protected]>
Quoted 4 lines
There are other approaches, but none really work early enough in the kernel boot to not be a pain. The goal is to provide timing info
before: timekeeping init, jiffies startup, and even CPU features determination,As I pointed out before that's wishful thinking:
You _cannot_ access a resource before it has been determined to be available.
Quoted 7 lines
Quoted 4 lines
Period.
It does not matter at all if _you_ know for sure that it is the case in _your_ personal setup.
I used
get_cycles(), which has a check for availability in it, so the patch didn't access a resource before it was determined to be available.
Note that such a check is only available in the MIPS and x86 versions.
Gr{oetje,eeting}s,
Geert
Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected] In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that. -- Linus Torvalds
Thomas Gleixner
maintainerRE: [PATCH v3] printk: fix zero-valued printk timestamps in early boot
In reply to Bird, TimTim!
Quoted 7 lines · Tim Bird
Quoted 7 lines
From: Thomas Gleixner <[email protected]>
Quoted 5 lines
The approach that I originally started with
(see https://lore.kernel.org/linux-embedded/[email protected]/
was to use hardcoded multiplier and shift values for converting from cycles to nanoseconds. These multiplier and shift values would be set at kernel configuration time (ie, using CONFIG values).
Quoted 7 lines
Quoted 1 line
Which makes it unusable for distro kernels and therefore a non-starter.
Can you elaborate on this? Indeed distro kernels would not be able to pre-set TCS calibration values, and would not turn on this feature (in that version of the patch) for production release kernels. But it sounds like you are saying that anything that requires a configuration that is non-general (or indeed used temporarily during development) is not acceptable upstream. Is that your position?
The point is that we really want to provide general available functionality as much as we can. The problem with all these special features is that they add to the overall maintainence burden. We do them when there is no other way.
Quoted 4 lines
This "feature" is intended as a tool for developers who are optimizing Linux kernel boot time. (I'm not sure who else would be interested in getting timing data for these (currently zero-timestamped) printks during the first 100-400 milliseconds of kernel boot). This would be, I believe, developers
The existing early TSC enablement on x86 starts providing printk timestamps at about 30ms after boot out of the box when I remove 'earlyprintk' from the command line on the same VM/host combo I tested the PoC. That too uses sched clock and not some special mechanism.
The real question is whether this boot time optimization really needs to have earlier time stamps and whether the hacks required for that are actually worth it.
Quoted 6 lines
This patch is part of a larger effort on my part to help automate boot-time tuning of the kernel. Many other parts of that effort rely on reconfiguration and recompilation of the kernel, which makes the whole thing a development-time effort, not so much a run-time, end-user, or production-level feature. And very much not a thing that can be accomplished with distro-only configs.
I understand that, but when you can achieve it by utilizing what's there already just by providing access to the TSC at the earliest possible time, then adding new infrastructure is obviously not the right thing to do.
Quoted 11 lines
Quoted 11 lines
Quoted 5 lines
There are other approaches, but none really work early enough in the kernel boot to not be a pain. The goal is to provide timing info
before: timekeeping init, jiffies startup, and even CPU features determination,As I pointed out before that's wishful thinking:
You _cannot_ access a resource before it has been determined to be available.
Quoted 8 lines
Quoted 4 lines
Period.
It does not matter at all if _you_ know for sure that it is the case in _your_ personal setup.
I used
get_cycles(), which has a check for availability in it, so the patch didn't access a resource before it was determined to be available.
It sounds like you're responding to my wording above and not the patch itself.
Correct: '... and even CPU feature determination'
Quoted 10 lines
Quoted 2 lines
Either it is solved in a generic way or we have to agree that it's not solvable at all.
I disagree that solving this limited problem (zero-valued timestamps in early boot) has to be solved in a generic way.
I already limited the solution space to only processors that I believed had reliable, pre-kernel-initialized cycle generators.I think it's fair to have a specialized solution to a specialized problem, if it can be made to have very limited effect on other code.
This 'my problem is special' mindset is exactly what frustrates me to be honest. It causes technical debt and I've spent several decades of work to mop up the technical debt caused by it.
Quoted 3 lines
I tried to avoid affecting any other timekeeping mechanism (ie re-engineering
local_clock), specifically to avoid unwanted side effects.
As I demonstrated there is no need to even touch printk or local clock at all.
Quoted 8 lines
Quoted 4 lines
The early TSC init happens in
setup_arch()viatsc_early_init()and it's completely unclear whether you can always access the TSC safely before that unconditionally due to SNP, which requires to enable the secure TSC first. There is a reason why all of this is ordered the way it is.OK. Thanks for that info. I'll take a look at that.
What happens if you try ardtsc()beforetsc_early_init? If it returns zero, I can live with that. If it faults or returns random data that's a problem.
As I said it's unclear.
Quoted 5 lines
Quoted 4 lines
The only clean way to solve this cleanly is moving the sched clock initialization to the earliest point possible and accepting that due to hardware, enumeration and virtualization constraints this point might be suboptimal. Everything else is just an attempt to defy reality.
Quoted 7 lines
I think we're willing to accept different areas of sub-optimality. I can live with having to configure the feature, and statically configure the TSC clock calibration, at the expense of non-monotonic printk timestamps for the early printks.
Personally, I think that altering
sched_clockor any other kernel timekeeping is overkill for this.
I'm not asking to change any of that. I clearly pointed out to you in my first reply that the existing local clock path is where you want to add your 'even earlier' access.
That said, let me come back to the question I asked earlier.
The existing early sched clock enablement, which happens either in the hypervisor detection for VMs and/or tsc_early_init() is around 30ms into the boot process.
The initialization which happens before that is pretty much the bare minimum to get so far. The optimization potential of that stage is very close to zero.
So the real good question is whether the extra information of how long that earliest init takes is really relevant to the goal of optimizing boot time. The expensive part of the boot process definitely comes after that.
Thanks,
tglx
Thomas Gleixner
maintainerRE: [PATCH v3] printk: fix zero-valued printk timestamps in early boot
Tim!
Quoted 4 lines · Thomas Gleixner
So the real good question is whether the extra information of how long that earliest init takes is really relevant to the goal of optimizing boot time. The expensive part of the boot process definitely comes after that.
That actually made me curious and so I hacked up the kernel with the patch below to compensate for the difference between:
x86_64_start_reservations()
i.e. the C entry point of the kernel and the actual earliest point (ASM entry code aside) where the kernel can take a timestamp, which is modulo the sanity checks in the PoC the same thing, right?
and
tsc_early_init()
where the upstream kernel enables TSC sched clock today with all sanity checks and enumeration in place.
Here is the result on a bare metal 256 CPU machine:
[ 0.000000] Linux version 7.0.0-rc3-dirty ...
....
[ 0.000000] tsc: Detected 2100.000 MHz processor [ 0.012482] e820: update [mem 0x00000000-0x00000fff] System RAM ==> device reserved
That's ~12ms of time which is not accounted for in the overall boot time until the machine reaches the init process:
[ 12.289141] Run /init as init process
That means we are talking about ~0.1% of overall boot time in this case.
Starting a 4 CPU guest with the same kernel image on the same physical machine and additionally 'no-kvmclock' on the command line to make the hack work:
[ 0.000000] Linux version 7.0.0-rc3-dirty ...
...
[ 0.000000] tsc: Detected 2094.965 MHz processor [ 0.015122] last_pfn = 0x280000 max_arch_pfn = 0x400000000
Unsurpringly it takes a bit longer because during that phase the guest takes a gazillion of vmexits.
[ 0.995082] Run /init as init process
Now in this 4 CPU case we are talking about 1.5% of the overall boot time.
With the same setup and 32 CPUs in the VM:
[ 0.015150] e820: remove [mem 0x000a0000-0x000fffff] System RAM
The initial phase takes 30us more than with 4 CPUs, which is in the noise and the machine ends up in init at:
[ 3.329398] Run /init as init process
which means in total we are up to 0.45% of the overall boot time now.
I'm honestly confused. May I politely ask which problem you are trying to solve?
Thanks,
tglx
---
arch/x86/kernel/head64.c +4 -0
@@ -291,6 +291,8 @@ asmlinkage __visible void __init __noretx86_64_start_reservations (real_mode_data); } +extern u64 start_ts; +void __init __noreturn x86_64_start_reservations (char *real_mode_data) {/* version is always not zero if it is copied */
@@ -307,6 +309,8 @@ void __init __noreturn x86_64_start_resebreak ; } + start_ts =rdtsc (); +start_kernel (); }
arch/x86/kernel/tsc.c +4 -1
@@ -53,6 +53,9 @@ static DEFINE_STATIC_KEY_FALSE_RO(__use_int tsc_clocksource_reliable; +extern u64 start_ts; +u64 start_ts; +static int __read_mostly tsc_force_recalibrate;static struct clocksource_base art_base_clk = {
@@ -207,7 +210,7 @@ static void __init cyc2ns_init_boot_cpu(struct cyc2ns *c2n =this_cpu_ptr (&cyc2ns);seqcount_latch_init (&c2n->seq); -__set_cyc2ns_scale (tsc_khz,smp_processor_id (),rdtsc ()); +__set_cyc2ns_scale (tsc_khz,smp_processor_id (), start_ts); }/*
Steven Rostedt
reviewerRe: [PATCH v3] printk: fix zero-valued printk timestamps in early boot
Quoted 29 lines · On Wed, 01 Apr 2026 01:36:26 +0200 Thomas Gleixner <[email protected]> wrote:
Quoted 4 lines · Thomas Gleixner
So the real good question is whether the extra information of how long that earliest init takes is really relevant to the goal of optimizing boot time. The expensive part of the boot process definitely comes after that.
That actually made me curious and so I hacked up the kernel with the patch below to compensate for the difference between:
x86_64_start_reservations()i.e. the C entry point of the kernel and the actual earliest point (ASM entry code aside) where the kernel can take a timestamp, which is modulo the sanity checks in the PoC the same thing, right?
and
tsc_early_init()where the upstream kernel enables TSC sched clock today with all sanity checks and enumeration in place.
Here is the result on a bare metal 256 CPU machine:
[ 0.000000] Linux version 7.0.0-rc3-dirty .......
Quoted 7 lines
[ 0.000000] tsc: Detected 2100.000 MHz processor [ 0.012482] e820: update [mem 0x00000000-0x00000fff] System RAM ==> device reservedThat's ~12ms of time which is not accounted for in the overall boot time until the machine reaches the init process:
[ 12.289141] Run /init as init process
12 seconds! Holly crap, that would fail every Chromebook requirement we have. In most cases, we shoot for a 3 second boot up and 8 second max. That's from hitting the power button to login screen. FYI, 30ms is considered a really long time.
We spend a lot of time looking at every thing that slows down the boot process, and yes we do a lot of tricks in user space as well (see ureadahead[1]).
I'm sure there's various devices that have issues between the kernel C starting point and where timing begins. When I was looking into boot times, I did find that the biggest issue with boot up was the initcalls. They are all serial. If one sleeps, it causes everything to back up. I would love to have initcalls have more parallelism, but there's a lot of code that depends on things running in serial :-(
Point being, I'm sure Tim is worried about devices that require fast boot up times. Not machines with 256 CPUs.
I haven't needed to look at what is happening before time stamps are running, as we track that time via the firmware timestamps. I'm just pointing out that the machine you used in this example isn't the target of the work that is looking into fast bootup times.
-- Steve
Re: [PATCH v3] printk: fix zero-valued printk timestamps in early boot
Quoted 6 lines · Steven Rostedt
Quoted 1 line · On Wed, 01 Apr 2026 01:36:26 +0200 Thomas Gleixner <[email protected]> wrote:
[ 12.289141] Run /init as init process12 seconds! Holly crap, that would fail every Chromebook requirement we have. In most cases, we shoot for a 3 second boot up and 8 second max.
So that's why my Sony HD Handycam never boots Linux, but instead still resumes the system Tim suspended 18 years ago ;-)
Looks like the old CE Linux Forum Boot Time Working Group needs more blood...
Gr{oetje,eeting}s,
Geert
Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected] In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that. -- Linus Torvalds
Thomas Gleixner
maintainerRe: [PATCH v3] printk: fix zero-valued printk timestamps in early boot
In reply to Steven RostedtSteven!
Quoted 11 lines · Steven Rostedt
Quoted 4 lines · On Wed, 01 Apr 2026 01:36:26 +0200 Thomas Gleixner <[email protected]> wrote:
That's ~12ms of time which is not accounted for in the overall boot time until the machine reaches the init process:
[ 12.289141] Run /init as init process12 seconds! Holly crap, that would fail every Chromebook requirement we have. In most cases, we shoot for a 3 second boot up and 8 second max. That's from hitting the power button to login screen. FYI, 30ms is considered a really long time.
Which 30ms are you talking about?
The first timestamp is 12ms into the boot on bare metal as you can see from the quoted text above. On a 4 CPU VM running on that very same host it's 15ms due to the VM exit costs.
Which means with your 3 seconds aim, that's ~0.4% of the overall time. Even with your 30ms it's still only 1% according to my old school understanding of math.
Quoted 2 lines
Point being, I'm sure Tim is worried about devices that require fast boot up times. Not machines with 256 CPUs.
You're clearly missing the point.
As I've demonstrated the time until the first time stamp is available has nothing to do with the number of CPUs and is more or less constant:
VM with 4 CPUs:
[ 0.015122] last_pfn = 0x280000 max_arch_pfn = 0x400000000
VM with 32 CPUs:
[ 0.015150] e820: remove [mem 0x000a0000-0x000fffff] System RAM
The costs of the number of CPUs comes later in the boot process:
VM with 4 CPUs:
[ 0.995082] Run /init as init process
VM with 32 CPUs:
[ 3.329398] Run /init as init process
No?
Thanks,
tglx
Steven Rostedt
reviewerRe: [PATCH v3] printk: fix zero-valued printk timestamps in early boot
Quoted 8 lines · On Wed, 01 Apr 2026 10:33:26 +0200 Thomas Gleixner <[email protected]> wrote:
Steven!
Quoted 5 lines · Steven Rostedt
Quoted 3 lines · On Wed, 01 Apr 2026 01:36:26 +0200 Thomas Gleixner <[email protected]> wrote:
That's ~12ms of time which is not accounted for in the overall boot time until the machine reaches the init process:
Quoted 8 lines
Quoted 6 lines
Quoted 1 line
[ 12.289141] Run /init as init process12 seconds! Holly crap, that would fail every Chromebook requirement we have. In most cases, we shoot for a 3 second boot up and 8 second max. That's from hitting the power button to login screen. FYI, 30ms is considered a really long time.
Which 30ms are you talking about?
Sorry, that was from your previous email:
The existing early TSC enablement on x86 starts providing printk timestamps at about 30ms after boot out of the box when I remove 'earlyprintk' from the command line on the same VM/host combo I tested the PoC. That too uses sched clock and not some special mechanism.
We actually try to account for pretty much every 10ms and if there's a 30ms gap somewhere, we look to see what it was doing.
Quoted 3 lines
As I've demonstrated the time until the first time stamp is available has nothing to do with the number of CPUs and is more or less constant:
So my question is, if something changes in this time frame, where it goes from 30ms to 300ms, how would we find out?
Again, for Chromebooks, we get these timestamps from the firmware and it's very useful.
-- Steve
Thomas Gleixner
maintainerRe: [PATCH v3] printk: fix zero-valued printk timestamps in early boot
Quoted 10 lines · Steven Rostedt
Quoted 2 lines · On Wed, 01 Apr 2026 10:33:26 +0200 Thomas Gleixner <[email protected]> wrote:
As I've demonstrated the time until the first time stamp is available has nothing to do with the number of CPUs and is more or less constant:
So my question is, if something changes in this time frame, where it goes from 30ms to 300ms, how would we find out?
Again, for Chromebooks, we get these timestamps from the firmware and it's very useful.
I understand all of that and I'm not opposed to make that happen if it's integrated properly into sched clock as I demonstrated with the PoC hack.
Thanks,
tglx
Re: [PATCH v3] printk: fix zero-valued printk timestamps in early boot
In reply to Thomas GleixnerHi Thomas,
Quoted 65 lines · Thomas Gleixner
Quoted 4 lines · Thomas Gleixner
So the real good question is whether the extra information of how long that earliest init takes is really relevant to the goal of optimizing boot time. The expensive part of the boot process definitely comes after that.
That actually made me curious and so I hacked up the kernel with the patch below to compensate for the difference between:
x86_64_start_reservations()i.e. the C entry point of the kernel and the actual earliest point (ASM entry code aside) where the kernel can take a timestamp, which is modulo the sanity checks in the PoC the same thing, right?
and
tsc_early_init()where the upstream kernel enables TSC sched clock today with all sanity checks and enumeration in place.
Here is the result on a bare metal 256 CPU machine:
[ 0.000000] Linux version 7.0.0-rc3-dirty .......
[ 0.000000] tsc: Detected 2100.000 MHz processor [ 0.012482] e820: update [mem 0x00000000-0x00000fff] System RAM ==> device reservedThat's ~12ms of time which is not accounted for in the overall boot time until the machine reaches the init process:
[ 12.289141] Run /init as init processThat means we are talking about ~0.1% of overall boot time in this case.
Starting a 4 CPU guest with the same kernel image on the same physical machine and additionally 'no-kvmclock' on the command line to make the hack work:
[ 0.000000] Linux version 7.0.0-rc3-dirty ......
[ 0.000000] tsc: Detected 2094.965 MHz processor [ 0.015122] last_pfn = 0x280000 max_arch_pfn = 0x400000000Unsurpringly it takes a bit longer because during that phase the guest takes a gazillion of vmexits.
[ 0.995082] Run /init as init processNow in this 4 CPU case we are talking about 1.5% of the overall boot time.
With the same setup and 32 CPUs in the VM:
[ 0.015150] e820: remove [mem 0x000a0000-0x000fffff] System RAMThe initial phase takes 30us more than with 4 CPUs, which is in the noise and the machine ends up in init at:
Quoted 6 lines
[ 3.329398] Run /init as init processwhich means in total we are up to 0.45% of the overall boot time now.
I'm honestly confused. May I politely ask which problem you are trying to solve?
A recent example of where this was a problem was in the creation of the arm64 linear map:
https://lore.kernel.org/all/[email protected]/
The boot time was all reported as 0, however we could tell there was a boot delay based on the timing of the firmware / other linux logs in the serial console. Eric Chanudet @ Red Hat (CCed) used the cntvct arch counters on arm64 to track down this unreported time to the linear map creation.
With this patch set, on a 32GB RAM arm64 system we have the linear map creation time went from ~350ms to 25ms. Again, the boot time was all reported as 0 in dmesg.
What Tim is trying to do is to identify if we have points like this on other systems, or if boot regressions are introduced in the future.
Brian
Thomas Gleixner
maintainerRe: [PATCH v3] printk: fix zero-valued printk timestamps in early boot
Brian!
Quoted 3 lines · Brian Masney
With this patch set, on a 32GB RAM arm64 system we have the linear map creation time went from ~350ms to 25ms. Again, the boot time was all reported as 0 in dmesg.
Impressive!
Quoted 2 lines
What Tim is trying to do is to identify if we have points like this on other systems, or if boot regressions are introduced in the future.
I completely understand that and as I clearly said from the very beginning I'm not trying to prevent that at all.
What I'm arguing against is the approach of creating an ad hoc wart in printk as that's creating yet another long term maintenance burden.
Thanks,
tglx