[PATCH v2] tracing: Record trace_clock and recover when reboot

From: Masami Hiramatsu (Google)
Date: Thu Apr 17 2025 - 01:17:41 EST


From: Masami Hiramatsu (Google) <mhiramat@xxxxxxxxxx>

Record trace_clock information in the trace_scratch area and recover
the trace_clock when boot, so that reader can docode the timestamp
correctly.
Note that since most trace_clocks records the timestamp in nano-
seconds, this is not a bug. But some trace_clock, like counter and
tsc will record the counter value. Only for those trace_clock user
needs this information.

Signed-off-by: Masami Hiramatsu (Google) <mhiramat@xxxxxxxxxx>
---
Changes in v2:
- instead of exposing it via last_boot_info, set the current
trace_clock as the same clock we used in the last boot.
---
include/linux/trace_clock.h | 2 ++
kernel/trace/trace.c | 17 +++++++++++++++++
2 files changed, 19 insertions(+)

diff --git a/include/linux/trace_clock.h b/include/linux/trace_clock.h
index 00e8f98c940f..2245848270d2 100644
--- a/include/linux/trace_clock.h
+++ b/include/linux/trace_clock.h
@@ -15,6 +15,8 @@

#include <asm/trace_clock.h>

+#define TRACE_CLOCK_NAME_MAX 16
+
extern u64 notrace trace_clock_local(void);
extern u64 notrace trace_clock(void);
extern u64 notrace trace_clock_jiffies(void);
diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c
index 8ddf6b17215c..6ce5d05ec64d 100644
--- a/kernel/trace/trace.c
+++ b/kernel/trace/trace.c
@@ -6004,6 +6004,7 @@ struct trace_mod_entry {
};

struct trace_scratch {
+ char clock[TRACE_CLOCK_NAME_MAX];
unsigned long text_addr;
unsigned long nr_entries;
struct trace_mod_entry entries[];
@@ -6114,6 +6115,8 @@ static void update_last_data(struct trace_array *tr)
if (tr->scratch) {
struct trace_scratch *tscratch = tr->scratch;

+ strscpy(tscratch->clock, trace_clocks[tr->clock_id].name,
+ TRACE_CLOCK_NAME_MAX);
memset(tscratch->entries, 0,
flex_array_size(tscratch, entries, tscratch->nr_entries));
tscratch->nr_entries = 0;
@@ -7289,6 +7292,12 @@ int tracing_set_clock(struct trace_array *tr, const char *clockstr)
tracing_reset_online_cpus(&tr->max_buffer);
#endif

+ if (tr->scratch && !(tr->flags & TRACE_ARRAY_FL_LAST_BOOT)) {
+ struct trace_scratch *tscratch = tr->scratch;
+
+ strscpy(tscratch->clock, trace_clocks[i].name, TRACE_CLOCK_NAME_MAX);
+ }
+
mutex_unlock(&trace_types_lock);

return 0;
@@ -9514,6 +9523,14 @@ static void setup_trace_scratch(struct trace_array *tr,

/* Scan modules to make text delta for modules. */
module_for_each_mod(make_mod_delta, tr);
+
+ /* Set trace_clock as the same of the previous boot. */
+ if (tscratch->clock[0]) {
+ if (tracing_set_clock(tr, tscratch->clock) < 0) {
+ pr_info("the previous trace_clock info is not valid.");
+ goto reset;
+ }
+ }
return;
reset:
/* Invalid trace modules */