From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 24CDF3A9D9C for ; Tue, 26 May 2026 20:08:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779826107; cv=none; b=Fj7+ckenoVnSS2rkb7C/WRHy607g2oEuwQ4EIOpADgpfjRb+XCRjQJI/SQcGWzyTSKFefEEBQkR+/OkYDosk0cXcWRN8eSHmzVPbDVHYIX6AZzt/+zRcQMohxigDljVquQa9Bai4xMwrhRB5KO8uR8cpm55Q/bkqL/uZ6shb/pk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779826107; c=relaxed/simple; bh=O5rGknUjFwWVYSACendv0UgxUr6Y+55GumJrHm8bRXs=; h=From:To:Cc:Subject:In-Reply-To:References:Date:Message-ID: MIME-Version:Content-Type; b=ofolFFJb2nDuU7JUJINhZjrAQ57iasiksKs5kEwgu2JshCWI3hNayk+jWduDTeoVqydMyS7enBZs7s0ROny2ngKOyR3j+CTiTeHZSLET08oXR2zOwmIIjrDro7REBi1GyifWkDCLgbRbkzR1PU6986WA/qqt2Bwj7t3E3Nd/ZM0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Sq56yVvO; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Sq56yVvO" Received: by smtp.kernel.org (Postfix) with UTF8SMTPSA id 15C981F000E9; Tue, 26 May 2026 20:08:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1779826105; bh=EEpkq7kshFQGF6aGbYd8p6+CLNF3GfTAOBVD/nRI+NM=; h=From:To:Cc:Subject:In-Reply-To:References:Date; b=Sq56yVvO0bgiju7kApzfFbIX0KrjO4AyUpVkigBx/2vnpZME9z1tCID4suWBsRISJ 3a96NgyEDB6qilpjug71eqsHpQ7a9z408Fa18Y8orgYCCLJHAHhwVsZgBvT0lRSJKj gbzHqsjb11ALIMAOa0h7wPjgvrLF44ORZJl36t+3ZbWZ++fLG6lWSWJ0nlIiJIcX6b 3Zp0Yn/V+qZBLioEWX4ENcyyu5osmqRz2h0vjqu6kO6L7QrKbi4UmkEJ49OYeT8kER q/Eh+CsOuxCAng147xMIvc70RwVeIt3O8EXMIuO+LHaYXilCUPe253zwyoYslzm4Wg 7X4t1Fe8E3mDQ== From: Thomas Gleixner To: Thomas =?utf-8?Q?Wei=C3=9Fschuh?= , Petr Mladek Cc: Steven Rostedt , John Ogness , Sergey Senozhatsky , Anna-Maria Behnsen , Frederic Weisbecker , linux-kernel@vger.kernel.org Subject: Re: [QUESTION] printk: time namespaces and printk timestamps In-Reply-To: <9f0b1f85-1856-4e44-a419-5f14e13c0b82@t-8ch.de> References: <9f0b1f85-1856-4e44-a419-5f14e13c0b82@t-8ch.de> Date: Tue, 26 May 2026 22:08:21 +0200 Message-ID: <874ijuszm2.ffs@tglx> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable On Mon, May 25 2026 at 17:38, Thomas Wei=C3=9Fschuh 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: >> > > > >=20 >> > > > > 1. printk() historically used a monotonic clock. I am not sure i= f any >> > > > > usespace application depends on it but there is a risk of bre= aking >> > > > > something. >> > > >=20 >> > > > >From the perspective of the namespaced process currently the time= stamps >> > > > from printk may in fact not be CLOCK_MONOTONIC. >> > >=20 >> > > The messages stored in the kernel ring buffer currently always have >> > > monotonic timestamps. >> >=20 >> > "monotonic" as in "never decreases" (obious) or "CLOCK_MONOTONIC"? >> > That is not actually well-documented I think. >> > dmesg(1) assumes CLOCK_MONOTONIC. >>=20 >> 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