Skip to contentSkip to conversation

printk: Add new timestamps

PATCH Superseded Prarit Bhargava 4 participants 7 messages Aug 24, 2017 – Aug 25, 2017

Subsystems (4)

v8 of 12 Superseded Latest is v12 →

lore
Share & tools
lore
#0 lore raw

v8 cover printk: Add new timestamps

printk.time=1/CONFIG_PRINTK_TIME=1 adds a unmodified local hardware clock timestamp to printk messages. The local hardware clock loses time each day making it difficult to determine exactly when an issue has occurred in the kernel log, and making it difficult to determine how kernel and hardware issues relate to each other in real time.

Add monotonic, boottime, and real clock timestamps in addition to the existing local hardware clock timestamp.

Cc Mark Salyzyn <[email protected]>
Cc Jonathan Corbet <[email protected]>
Cc Petr Mladek <[email protected]>
Cc Sergey Senozhatsky <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc John Stultz <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Stephen Boyd <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc "Paul E. McKenney" <[email protected]>
Cc Christoffer Dall <[email protected]>
Cc Deepa Dinamani <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Joel Fernandes <[email protected]>
Cc Prarit Bhargava <[email protected]>
Cc Kees Cook <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Geert Uytterhoeven <[email protected]>
Cc "Luis R. Rodriguez" <[email protected]>
Cc Nicholas Piggin <[email protected]>
Cc "Jason A. Donenfeld" <[email protected]>
Cc Olof Johansson <[email protected]>
Cc Josh Poimboeuf <[email protected]>
[jstultz: reworked Kconfig settings to avoid defconfig noise]

Prarit Bhargava (2):
time: Make fast functions return 0 before timekeeping is initialized
printk: Add monotonic, boottime, and realtime timestamps

 Documentation/admin-guide/kernel-parameters.txt |   6 +-
 include/linux/timekeeping.h                     |   1 +
 kernel/printk/printk.c                          | 154 +++++++++++++++++++++++-
 kernel/time/timekeeping.c                       |  46 +++++--
 lib/Kconfig.debug                               |  65 +++++++++-
 5 files changed, 253 insertions(+), 19 deletions(-)
Signature · 1 line
1.8.5.5
#1 lore raw

PATCH v8 1/2 time: Make fast functions return 0 before timekeeping is initialized

printk timestamps will be extended to include mono and boot time by using the fast timekeeping functions ktime_get_mono|boot_fast_ns() functions. The functions can return garbage before timekeeping is initialized resulting in garbage timestamps.

The fast time functions must return 0 before timekeeping is initialized.

---
 kernel/time/timekeeping.c | 47 +++++++++++++++++++++++++++++++++--------------
 1 file changed, 33 insertions(+), 14 deletions(-)
kernel/time/timekeeping.c +33 -14
index cedafa008de5..d111039e0245 100644
@@ -60,8 +60,39 @@ struct tk_fast {
 	struct tk_read_base	base[2];
 };
 
-static struct tk_fast tk_fast_mono ____cacheline_aligned;
-static struct tk_fast tk_fast_raw  ____cacheline_aligned;
+/* Suspend-time cycles value for halted fast timekeeper. */
+static u64 cycles_at_suspend;
+
+static u64 dummy_clock_read(struct clocksource *cs)
+{
+	return cycles_at_suspend;
+}
+
+static struct clocksource dummy_clock = {
+	.read = dummy_clock_read,
+};
+
+static struct tk_fast tk_fast_mono ____cacheline_aligned = {
+	.base = {
+		(struct tk_read_base){
+			.clock = &dummy_clock,
+		},
+		(struct tk_read_base){
+			.clock = &dummy_clock,
+		},
+	},
+};
+
+static struct tk_fast tk_fast_raw  ____cacheline_aligned = {
+	.base = {
+		(struct tk_read_base){
+			.clock = &dummy_clock,
+		},
+		(struct tk_read_base){
+			.clock = &dummy_clock,
+		},
+	},
+};
 
 /* flag for if timekeeping is suspended */
 int __read_mostly timekeeping_suspended;
@@ -477,18 +508,6 @@ u64 notrace ktime_get_boot_fast_ns(void)
 }
 EXPORT_SYMBOL_GPL(ktime_get_boot_fast_ns);
 
-/* Suspend-time cycles value for halted fast timekeeper. */
-static u64 cycles_at_suspend;
-
-static u64 dummy_clock_read(struct clocksource *cs)
-{
-	return cycles_at_suspend;
-}
-
-static struct clocksource dummy_clock = {
-	.read = dummy_clock_read,
-};
-
 /**
  * halt_fast_timekeeper - Prevent fast timekeeper from accessing clocksource.
  * @tk: Timekeeper to snapshot.
Signature · 1 line
1.8.5.5
#2 lore raw

PATCH v8 2/2 printk: Add monotonic, boottime, and realtime timestamps

In reply to Prarit Bhargava

printk.time=1/CONFIG_PRINTK_TIME=1 adds a unmodified local hardware clock timestamp to printk messages. The local hardware clock loses time each day making it difficult to determine exactly when an issue has occurred in the kernel log, and making it difficult to determine how kernel and hardware issues relate to each other in real time.

Make printk output different timestamps by adding options for no timestamp, the local hardware clock, the monotonic clock, the boottime clock, and the real clock. Allow a user to pick one of the clocks by using the printk.time kernel parameter. Output the type of clock in /sys/module/printk/parameters/time so userspace programs can interpret the timestamp.

Real clock & 32-bit systems: Selecting the real clock printk timestamp may lead to unlikely situations where a timestamp is wrong because the real time without the protection of a sequence lock when printk_get_ts() is set printk_get_real_ns().

v2: Use peterz's suggested Kconfig options. Merge patchset together. Fix i386 !CONFIG_PRINTK builds.

v3: Fixed x86_64_defconfig. Added printk_time_type enum and printk_time_str for better output. Added BOOTTIME clock functionality.

v4: Fix messages, add additional printk.time options, and fix configs.

v5: Renaming of structures, and allow printk_time_set() to evaluate substrings of entries (eg: allow 'r', 'real', 'realtime'). From peterz, make fast functions return 0 until timekeeping is initialized (removes timekeeping_active & ktime_get_boot|real_log_ts() suggested by
tglx and adds ktime_get_real_offset()). Switch to a function pointer
for printk_get_ts() and reference fast functions. Make timestamp_sources enum match choice options for CONFIG_PRINTK_TIME (adds PRINTK_TIME_UNDEFINED).

v6: Define PRINTK_TIME_UNDEFINED for !CONFIG_PRINTK builds. Separate timekeeping changes into separate patch. Minor include file cleanup.

v7: Add default case to printk_set_timestamp() and add PRINTK_TIME_DEBUG for users that want to set timestamp to different values during runtime. Add jstultz' Kconfig to avoid defconfig churn.

v8: Add CONFIG_PRINTK_TIME_DEBUG to allow timestamp runtime switching. Rename PRINTK_TIME_DISABLE to PRINTK_TIME_DISABLED. Rename printk_set_timestamp() to printk_set_ts_func(). Separate printk_set_ts_func() and printk_get_first_ts() portions. Rename param functions. Adjust configs, enum, and timestamp_sources_str to be 0-4. Add mention realtime clock is UTC in Documentation.

Cc Mark Salyzyn <[email protected]>
Cc Jonathan Corbet <[email protected]>
Cc Petr Mladek <[email protected]>
Cc Sergey Senozhatsky <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc John Stultz <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Stephen Boyd <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc "Paul E. McKenney" <[email protected]>
Cc Christoffer Dall <[email protected]>
Cc Deepa Dinamani <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Joel Fernandes <[email protected]>
Cc Prarit Bhargava <[email protected]>
Cc Kees Cook <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Geert Uytterhoeven <[email protected]>
Cc "Luis R. Rodriguez" <[email protected]>
Cc Nicholas Piggin <[email protected]>
Cc "Jason A. Donenfeld" <[email protected]>
Cc Olof Johansson <[email protected]>
Cc Josh Poimboeuf <[email protected]>
[jstultz: reworked Kconfig settings to avoid defconfig noise]
---
 Documentation/admin-guide/kernel-parameters.txt |   6 +-
 include/linux/timekeeping.h                     |   1 +
 kernel/printk/printk.c                          | 154 +++++++++++++++++++++++-
 kernel/time/timekeeping.c                       |   5 +
 lib/Kconfig.debug                               |  65 +++++++++-
 5 files changed, 223 insertions(+), 8 deletions(-)
Documentation/admin-guide/kernel-parameters.txt +4 -2
index d9c171ce4190..8d6b194533af 100644
@@ -3188,8 +3188,10 @@
 			ratelimit - ratelimit the logging
 			Default: ratelimit
 
-	printk.time=	Show timing data prefixed to each printk message line
-			Format: <bool>  (1/Y/y=enable, 0/N/n=disable)
+	printk.time=	Show timestamp prefixed to each printk message line
+			Format: <string>
+				(0/N/n/disable, 1/Y/y/local,
+				 b/boot, m/monotonic, r/realtime (in UTC))
 
 	processor.max_cstate=	[HW,ACPI]
 			Limit processor to maximum C-state
include/linux/timekeeping.h +1 -0
index ddc229ff6d1e..80ca2fa22b6a 100644
@@ -239,6 +239,7 @@ static inline u64 ktime_get_raw_ns(void)
 extern u64 ktime_get_mono_fast_ns(void);
 extern u64 ktime_get_raw_fast_ns(void);
 extern u64 ktime_get_boot_fast_ns(void);
+extern u64 ktime_get_real_offset(void);
 
 /*
  * Timespec interfaces utilizing the ktime based ones
kernel/printk/printk.c +149 -5

195 lines not on this page · raw message

kernel/time/timekeeping.c +5 -0
index d111039e0245..de07bb5ffef5 100644
@@ -508,6 +508,11 @@ u64 notrace ktime_get_boot_fast_ns(void)
 }
 EXPORT_SYMBOL_GPL(ktime_get_boot_fast_ns);
 
+u64 ktime_get_real_offset(void)
+{
+	return ktime_to_ns(tk_core.timekeeper.offs_real);
+}
+
 /**
  * halt_fast_timekeeper - Prevent fast timekeeper from accessing clocksource.
  * @tk: Timekeeper to snapshot.
lib/Kconfig.debug +64 -1

82 lines not on this page · raw message

Signature · 1 line
1.8.5.5
#3 lore raw

Re: [PATCH 2/2 v8] printk: Add monotonic, boottime, and realtime timestamps

Quoted 14 lines · Prarit Bhargava
include/linux/timekeeping.h +1 -0
@@ -239,6 +239,7 @@ static inline u64 ktime_get_raw_ns(void)
 extern u64 ktime_get_mono_fast_ns(void);
 extern u64 ktime_get_raw_fast_ns(void);
 extern u64 ktime_get_boot_fast_ns(void);
+extern u64 ktime_get_real_offset(void);
 
 /*
  * Timespec interfaces utilizing the ktime based ones
kernel/printk/printk.c +0 -0
index fc47863f629c..dd972bc5c88b 100644

...

Quoted 10 lines

+ * printk_get_real_ns: - Return a realtime timestamp for printk messages
+ * On 32-bit systems selecting the real clock printk timestamp may lead to
+ * unlikely situations where a timestamp is wrong because the real time offset
+ * is read without the protection of a sequence lock.
+ */

kernel/printk/printk.c +5 -0
+static u64 printk_get_real_ns(void)
+{
+       return ktime_get_mono_fast_ns() + ktime_get_real_offset();
+}
+

...

kernel/time/timekeeping.c u64 notrace ktime_get_boot_fast_ns(void) · line 515

Quoted 13 lines
kernel/time/timekeeping.c +5 -0
index d111039e0245..de07bb5ffef5 100644
@@ -508,6 +508,11 @@ u64 notrace ktime_get_boot_fast_ns(void)
 }
 EXPORT_SYMBOL_GPL(ktime_get_boot_fast_ns);
 
+u64 ktime_get_real_offset(void)
+{
+       return ktime_to_ns(tk_core.timekeeper.offs_real);
+}
+

Apologies! One last nit here. So if we're going to export ktime_get_real_offset(), folks are going to use it, and there is no documentation about its very sketchy behavioral limits as an interface, except elsewhere in the printk code.

Instead of doing that, could you export a __ktime_get_real_fast_ns_unsafe() function, which has its limits (calculating the realtime w/o locks, thus may return completely bad values occasionally) clearly documented in the timekeeping code?

You can then use that directly in your printk code, and others who find it and think "Hey this would be great for my life-safety-critical-system driver" will be clearly warned.

thanks
-john

#4 lore raw

Re: [PATCH 2/2 v8] printk: Add monotonic, boottime, and realtime timestamps

Quoted 46 lines · John Stultz
Quoted 14 lines · Prarit Bhargava
include/linux/timekeeping.h +1 -0
@@ -239,6 +239,7 @@ static inline u64 ktime_get_raw_ns(void)
 extern u64 ktime_get_mono_fast_ns(void);
 extern u64 ktime_get_raw_fast_ns(void);
 extern u64 ktime_get_boot_fast_ns(void);
+extern u64 ktime_get_real_offset(void);
 
 /*
  * Timespec interfaces utilizing the ktime based ones
kernel/printk/printk.c +0 -0
index fc47863f629c..dd972bc5c88b 100644

...

Quoted 10 lines

+ * printk_get_real_ns: - Return a realtime timestamp for printk messages
+ * On 32-bit systems selecting the real clock printk timestamp may lead to
+ * unlikely situations where a timestamp is wrong because the real time offset
+ * is read without the protection of a sequence lock.
+ */

kernel/printk/printk.c +5 -0
+static u64 printk_get_real_ns(void)
+{
+       return ktime_get_mono_fast_ns() + ktime_get_real_offset();
+}
+

...

kernel/time/timekeeping.c u64 notrace ktime_get_boot_fast_ns(void) · line 515

Quoted 13 lines
kernel/time/timekeeping.c +5 -0
index d111039e0245..de07bb5ffef5 100644
@@ -508,6 +508,11 @@ u64 notrace ktime_get_boot_fast_ns(void)
 }
 EXPORT_SYMBOL_GPL(ktime_get_boot_fast_ns);
 
+u64 ktime_get_real_offset(void)
+{
+       return ktime_to_ns(tk_core.timekeeper.offs_real);
+}
+

Apologies! One last nit here. So if we're going to export ktime_get_real_offset(), folks are going to use it, and there is no documentation about its very sketchy behavioral limits as an interface, except elsewhere in the printk code.

Quoted 4 lines

Instead of doing that, could you export a __ktime_get_real_fast_ns_unsafe() function, which has its limits (calculating the realtime w/o locks, thus may return completely bad values occasionally) clearly documented in the timekeeping code?

np. I'm going to copy the code for

u64 notrace ktime_get_boot_fast_ns(void)

but I'm unsure why the function is marked "notrace", and if __ktime_get_real_fast_ns_unsafe() must be as well? I don't see anything in the git log that indicates why the function is notrace.

I've added Joel to this thread ...

P.

#5 lore raw

Re: [PATCH 2/2 v8] printk: Add monotonic, boottime, and realtime timestamps

Quoted 9 lines · On Fri, 25 Aug 2017 10:46:21 -0400 Prarit Bhargava <[email protected]> wrote:

np. I'm going to copy the code for

u64 notrace ktime_get_boot_fast_ns(void)

but I'm unsure why the function is marked "notrace", and if __ktime_get_real_fast_ns_unsafe() must be as well? I don't see anything in the git log that indicates why the function is notrace.

I've added Joel to this thread ...

The "notrace" is added because it is one of the trace clocks:

kernel/trace/trace.c: { ktime_get_boot_fast_ns, "boot", 1 },

And function tracer can use it. We don't want to be tracing the clock that the function tracer is using (otherwise it will get double traced). It use to bug, but I believe I finally got the recursion protection working to prevent that.

-- Steve

#6 lore raw

Re: [PATCH 2/2 v8] printk: Add monotonic, boottime, and realtime timestamps

In reply to Prarit Bhargava

On (08/24/17 09:42), Prarit Bhargava wrote:
[..]

lib/Kconfig.debug config CONSOLE_LOGLEVEL_DEFAULT · line 91

Quoted 13 lines
lib/Kconfig.debug +13 -0
+config PRINTK_TIME_DEBUG
+	bool "Allow runtime reselection of any timebase on printks"
+	depends on PRINTK
+	default N
+	help
+	  Selecting this option causes time stamps of the printk()
+	  messages to be changed freely at runtime on the output of
+	  the syslog() system call and at the console. Without this
+	  option, one can only enable or disable the configuration
+	  selected timebase.
+
+	  Runtime adjustment can be set via
+	  /sys/module/printk/paramters/time as follows with a string:

s/paramters/parameters/

lib/Kconfig.debug config CONSOLE_LOGLEVEL_DEFAULT · line 94

Quoted 3 lines
lib/Kconfig.debug +3 -0
+	  0/N/n/disable, 1/Y/y/local, b/boot, m/monotonic, r/realtime.
+	  eg: echo local >/sys/module/printk/parameters/time
+	      echo realtime >/sys/module/printk/parameters/time
	-ss