From: Thomas Gleixner <tglx@kernel.org>
To: "Thomas Weißschuh" <linux@weissschuh.net>,
"Petr Mladek" <pmladek@suse.com>
Cc: Steven Rostedt <rostedt@goodmis.org>,
John Ogness <john.ogness@linutronix.de>,
Sergey Senozhatsky <senozhatsky@chromium.org>,
Anna-Maria Behnsen <anna-maria@linutronix.de>,
Frederic Weisbecker <frederic@kernel.org>,
linux-kernel@vger.kernel.org
Subject: Re: [QUESTION] printk: time namespaces and printk timestamps
Date: Tue, 26 May 2026 22:08:21 +0200 [thread overview]
Message-ID: <874ijuszm2.ffs@tglx> (raw)
In-Reply-To: <9f0b1f85-1856-4e44-a419-5f14e13c0b82@t-8ch.de>
On Mon, May 25 2026 at 17:38, Thomas Weißschuh wrote:
> On 2026-05-25 15:29:01+0200, Petr Mladek wrote:
>
> Result:
> [ 2.998937] test (host console, non-namespaced)
> 0.873620599 (lsclocks --time monotonic in namespace)
> <12>[18446744071.708489] test (/dev/kmsg in namespace)
Of course :)
>> > > > > I see two potential problems:
>> > > > >
>> > > > > 1. printk() historically used a monotonic clock. I am not sure if any
>> > > > > usespace application depends on it but there is a risk of breaking
>> > > > > something.
>> > > >
>> > > > >From the perspective of the namespaced process currently the timestamps
>> > > > from printk may in fact not be CLOCK_MONOTONIC.
>> > >
>> > > The messages stored in the kernel ring buffer currently always have
>> > > monotonic timestamps.
>> >
>> > "monotonic" as in "never decreases" (obious) or "CLOCK_MONOTONIC"?
>> > That is not actually well-documented I think.
>> > dmesg(1) assumes CLOCK_MONOTONIC.
>>
>> Honestly, I do not know the details. printk() is calling local_clock()
>> which is a wrapper over sched_clock(). I think that it is based on
>> "CLOCK_MONOTONIC" but it is not precise because it is lockless.
>
> We have ktime_get_mono_fast_ns() which is also supposed to do this.
> The name would be clearer and it should be guaranteed to be
> CLOCK_MONOTONIC. But I don't know the details or history so this is just
> guesswork right now.
local_clock() is available slightly earlier than CLOCK_MONOTONIC and
contrary to CLOCK_MONOTONIC it is not NTP adjusted. So they drift apart,
which makes it actually annoying to correlate dmesg with actual time.
We had this discussion before:
https://lore.kernel.org/lkml/20171115182657.383713029@linutronix.de/
Tracing allows to select time, which _is_ very useful for correlation.
>> > As a real-world example, dmesg(1) uses clock_gettime(CLOCK_MONOTONIC) in
>> > its logic to format kmsg timestamps as wallcock/CLOCK_REALTIME ones:
That's absurd. There is no way dmesg(1) can map local_clock() to
REALTIME via MONOTONIC. Wishful thinking at best.
>> It should get fixed if we added the name space offset, similar
>> to commit 3ae700ecfae91331 ("fs/proc: apply the time namespace
>> offset to /proc/stat btime"). But we should use
>> timens_add_boottime_ns() which does not exist at the moment.
As long as printk uses local_clock() trying to apply a namespace offset is
completely moot.
local_clock() is basically CLOCK_MONOTONIC_RAW plus an offset, which is
something between 30-100ms. But it's actually not exact as the
calculation is slightly different and therefore drifts apart over time
slightly.
CLOCK_MONOTONIC on the other hand is diverging completely when the
system has NTP enabled. A very small frequency adjustment of 12ppm sums
up to ~one second per day.
Thanks,
tglx
prev parent reply other threads:[~2026-05-26 20:08 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-23 15:20 [QUESTION] printk: time namespaces and printk timestamps Thomas Weißschuh
2026-05-25 8:37 ` Petr Mladek
2026-05-25 8:47 ` Thomas Weißschuh
2026-05-25 9:30 ` Petr Mladek
2026-05-25 10:26 ` Thomas Weißschuh
2026-05-25 13:29 ` Petr Mladek
2026-05-25 15:38 ` Thomas Weißschuh
2026-05-26 20:08 ` Thomas Gleixner [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=874ijuszm2.ffs@tglx \
--to=tglx@kernel.org \
--cc=anna-maria@linutronix.de \
--cc=frederic@kernel.org \
--cc=john.ogness@linutronix.de \
--cc=linux-kernel@vger.kernel.org \
--cc=linux@weissschuh.net \
--cc=pmladek@suse.com \
--cc=rostedt@goodmis.org \
--cc=senozhatsky@chromium.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox