Re: [PATCH v4 1/1] printk: fix zero-valued printk timestamps in early boot

Thomas Gleixner
2026-04-14T22:38:33Z

On Fri, Apr 10 2026 at 14:37, Tim Bird wrote:
> +
> +#include <linux/timekeeping.h>
> +#ifdef CONFIG_ARM64
> +#include <asm/sysreg.h>
> +#endif
> +
> +#ifdef CONFIG_EARLY_CYCLES_KHZ
> +static inline u64 early_unsafe_cycles(void)
> +{
> +#if defined(CONFIG_X86_64)
> + /*
> + * This rdtsc may happen before secure TSC is initialized, and
> + * it is unordered. So please don't use this value for cryptography
> + * or after SMP is initialized.
> + */
> + return rdtsc();
> +#elif defined(CONFIG_ARM64)
> + return read_sysreg(cntvct_el0);
> +#elif defined(CONFIG_RISCV_TIMER)
> + u64 val;
> +
> + asm volatile("rdtime %0" : "=r"(val));
> + return val;
> +#else
> + return 0;
> +#endif
> +}

No. Generic code and generic headers have no business to implement any
architecture specific code and there is zero justification for
architecture specific #ifdefs in generic code.

Ask your favourite AI assistant for an opinion.

> +#define NS_PER_KHZ 1000000UL

The existing nomenclature for nanosecond related constants is NSEC_*

> +
> +/* returns a nanosecond value based on early cycles */
> +static inline u64 early_times_ns(void)
> +{
> + if (CONFIG_EARLY_CYCLES_KHZ)
> + /*
> + * Note: the multiply must precede the division to avoid
> + * truncation and loss of resolution
> + * Don't use fancier MULT/SHIFT math here. Since this is
> + * static, the compiler can optimize the math operations.
> + */
> + return (early_unsafe_cycles() * NS_PER_KHZ) / CONFIG_EARLY_CYCLES_KHZ;

This code will result in a division by zero warning from any reasonable
compiler because this is evaluated _before_ it is eliminated.

> @@ -2294,6 +2295,8 @@ int vprintk_store(int facility, int level,
> * timestamp with respect to the caller.
> */
> ts_nsec = local_clock();
> + if (unlikely(!ts_nsec))
> + ts_nsec = early_times_ns();

I explained to you how this wants to be implemented to be useful and you
declared that you are unwilling to put the effort in.

My NAK for this disgusting and tasteless hack still applies.

Either you are willing to work with the community and the relevant
maintainers or you can keep your hackery maintained out of tree for
those who care about it and are willing to ignore the fallout.

Thanks,

tglx