Skip to contentSkip to conversation

tracing: Show last module text symbols in the stacktrace

PATCH In review Masami Hiramatsu (Google) 3 participants 5 messages Mar 10, 2025 – Mar 11, 2025 Tracing

lore
Share & tools
lore

Masami Hiramatsu (Google)

author maintainer
#0 lore raw

PATCH v3 1/1 tracing: Show last module text symbols in the stacktrace

From: Masami Hiramatsu (Google) <[email protected]>

Since the previous boot trace buffer can include module text address in the stacktrace. As same as the kernel text address, convert the module text address using the module address information.

---
Changes in v3:
- Move module_delta to trace_scratch data structure.
- Remove LRU based removed module information.

---
 kernel/trace/trace.c        |   99 +++++++++++++++++++++++++++++++++++++++++--
 kernel/trace/trace.h        |    2 +
 kernel/trace/trace_output.c |    4 +-
 3 files changed, 98 insertions(+), 7 deletions(-)
kernel/trace/trace.c +94 -5
index c3c79908766e..0c1aa1750077 100644
@@ -49,6 +49,7 @@
 #include <linux/fsnotify.h>
 #include <linux/irq_work.h>
 #include <linux/workqueue.h>
+#include <linux/sort.h>
 
 #include <asm/setup.h> /* COMMAND_LINE_SIZE and kaslr_offset() */
 
@@ -5996,11 +5997,41 @@ struct trace_mod_entry {
 struct trace_scratch {
 	unsigned long		kaslr_addr;
 	unsigned long		nr_entries;
+	long			*module_delta;
 	struct trace_mod_entry	entries[];
 };
 
 static DEFINE_MUTEX(scratch_mutex);
 
+/**
+ * trace_adjust_address() - Adjust prev boot address to current address.
+ * @tr: Persistent ring buffer's trace_array.
+ * @addr: Address in @tr which is adjusted.
+ */
+unsigned long trace_adjust_address(struct trace_array *tr, unsigned long addr)
+{
+	struct trace_scratch *tscratch;
+	long *module_delta;
+	int i;
+
+	/* If we don't have last boot delta, return the address */
+	if (!(tr->flags & TRACE_ARRAY_FL_LAST_BOOT))
+		return addr;
+
+	tscratch = tr->scratch;
+	module_delta = READ_ONCE(tscratch->module_delta);
+	if (!tscratch || !tscratch->nr_entries || !module_delta ||
+	    tscratch->entries[0].mod_addr > addr)
+		return addr + tr->text_delta;
+
+	/* Note that entries must be sorted. */
+	for (i = 0; i < tscratch->nr_entries; i++)
+		if (addr < tscratch->entries[i].mod_addr)
+			break;
+
+	return addr + module_delta[i - 1];
+}
+
 static int save_mod(struct module *mod, void *data)
 {
 	struct trace_array *tr = data;
@@ -6029,6 +6060,7 @@ static int save_mod(struct module *mod, void *data)
 static void update_last_data(struct trace_array *tr)
 {
 	struct trace_scratch *tscratch;
+	long *module_delta;
 
 	if (!(tr->flags & TRACE_ARRAY_FL_BOOT))
 		return;
@@ -6063,6 +6095,8 @@ static void update_last_data(struct trace_array *tr)
 		return;
 
 	tscratch = tr->scratch;
+	module_delta = READ_ONCE(tscratch->module_delta);
+	WRITE_ONCE(tscratch->module_delta, NULL);
 
 	/* Set the persistent ring buffer meta data to this address */
 #ifdef CONFIG_RANDOMIZE_BASE
@@ -6071,6 +6105,8 @@ static void update_last_data(struct trace_array *tr)
 	tscratch->kaslr_addr = 0;
 #endif
 	tr->flags &= ~TRACE_ARRAY_FL_LAST_BOOT;
+
+	kfree(module_delta);
 }
 
 /**
@@ -9342,10 +9378,43 @@ static struct dentry *trace_instance_dir;
 static void
 init_tracer_tracefs(struct trace_array *tr, struct dentry *d_tracer);
 
+static int make_mod_delta(struct module *mod, void *data)
+{
+	struct trace_scratch *tscratch;
+	struct trace_mod_entry *entry;
+	struct trace_array *tr = data;
+	long *module_delta;
+	int i;
+
+	tscratch = tr->scratch;
+	module_delta = READ_ONCE(tscratch->module_delta);
+	for (i = 0; i < tscratch->nr_entries; i++) {
+		entry = &tscratch->entries[i];
+		if (!strcmp(mod->name, entry->mod_name)) {
+			if (mod->state == MODULE_STATE_GOING)
+				module_delta[i] = 0;
+			else
+				module_delta[i] = (unsigned long)mod->mem[MOD_TEXT].base
+						 - entry->mod_addr;
+			break;
+		}
+	}
+	return 0;
+}
+
+static int mod_addr_comp(const void *a, const void *b, const void *data)
+{
+	const struct trace_mod_entry *e1 = a;
+	const struct trace_mod_entry *e2 = b;
+
+	return e1->mod_addr > e2->mod_addr ? 1 : -1;
+}
+
 static void setup_trace_scratch(struct trace_array *tr,
 				struct trace_scratch *tscratch, unsigned int size)
 {
 	struct trace_mod_entry *entry;
+	int i, nr_entries;
 
 	if (!tscratch)
 		return;
@@ -9362,7 +9431,7 @@ static void setup_trace_scratch(struct trace_array *tr,
 		goto reset;
 
 	/* Check if each module name is a valid string */
-	for (int i = 0; i < tscratch->nr_entries; i++) {
+	for (i = 0; i < tscratch->nr_entries; i++) {
 		int n;
 
 		entry = &tscratch->entries[i];
@@ -9376,6 +9445,21 @@ static void setup_trace_scratch(struct trace_array *tr,
 		if (n == MODULE_NAME_LEN)
 			goto reset;
 	}
+
+	nr_entries = i;
+	tscratch->module_delta = kcalloc(nr_entries, sizeof(long), GFP_KERNEL);
+	if (!tscratch->module_delta) {
+		pr_info("module_delta allocation failed. Not able to decode module address.");
+		goto reset;
+	}
+
+	/* Sort the entries so that we can find appropriate module from address. */
+	sort_r(tscratch->entries, nr_entries, sizeof(struct trace_mod_entry),
+	       mod_addr_comp, NULL, NULL);
+
+	/* Scan modules to make text delta for modules. */
+	module_for_each_mod(make_mod_delta, tr);
+
 	return;
  reset:
 	/* Invalid trace modules */
@@ -10101,19 +10185,23 @@ static bool trace_array_active(struct trace_array *tr)
 	return trace_events_enabled(tr, NULL) > 1;
 }
 
-static void trace_module_record(struct module *mod)
+static void trace_module_record(struct module *mod, bool remove)
 {
 	struct trace_array *tr;
+	unsigned long flags;
 
 	list_for_each_entry(tr, &ftrace_trace_arrays, list) {
+		flags = tr->flags & (TRACE_ARRAY_FL_BOOT | TRACE_ARRAY_FL_LAST_BOOT);
 		/* Update any persistent trace array that has already been started */
-		if ((tr->flags & (TRACE_ARRAY_FL_BOOT | TRACE_ARRAY_FL_LAST_BOOT)) ==
-		    TRACE_ARRAY_FL_BOOT) {
+		if (flags == TRACE_ARRAY_FL_BOOT && !remove) {
 			/* Only update if the trace array is active */
 			if (trace_array_active(tr)) {
 				guard(mutex)(&scratch_mutex);
 				save_mod(mod, tr);
 			}
+		} else if (flags & TRACE_ARRAY_FL_LAST_BOOT) {
+			/* Update delta if the module loaded in previous boot */
+			make_mod_delta(mod, tr);
 		}
 	}
 }
@@ -10126,10 +10214,11 @@ static int trace_module_notify(struct notifier_block *self,
 	switch (val) {
 	case MODULE_STATE_COMING:
 		trace_module_add_evals(mod);
-		trace_module_record(mod);
+		trace_module_record(mod, false);
 		break;
 	case MODULE_STATE_GOING:
 		trace_module_remove_evals(mod);
+		trace_module_record(mod, true);
 		break;
 	}
 
kernel/trace/trace.h +2 -0
index 0d6efb8a1179..3375f8301de3 100644
@@ -466,6 +466,8 @@ extern int tracing_set_clock(struct trace_array *tr, const char *clockstr);
 
 extern bool trace_clock_in_ns(struct trace_array *tr);
 
+extern unsigned long trace_adjust_address(struct trace_array *tr, unsigned long addr);
+
 /*
  * The global tracer (top) should be the first trace array added,
  * but we check the flag anyway.
kernel/trace/trace_output.c +2 -2
index 03d56f711ad1..1ad54fcf25cb 100644
@@ -5,6 +5,7 @@
  * Copyright (C) 2008 Red Hat Inc, Steven Rostedt <[email protected]>
  *
  */
+#include "trace.h"
 #include <linux/module.h>
 #include <linux/mutex.h>
 #include <linux/ftrace.h>
@@ -1248,7 +1249,6 @@ static enum print_line_t trace_stack_print(struct trace_iterator *iter,
 	struct trace_seq *s = &iter->seq;
 	unsigned long *p;
 	unsigned long *end;
-	long delta = iter->tr->text_delta;
 
 	trace_assign_type(field, iter->ent);
 	end = (unsigned long *)((long)iter->ent + iter->ent_size);
@@ -1265,7 +1265,7 @@ static enum print_line_t trace_stack_print(struct trace_iterator *iter,
 			trace_seq_puts(s, "[FTRACE TRAMPOLINE]\n");
 			continue;
 		}
-		seq_print_ip_sym(s, (*p) + delta, flags);
+		seq_print_ip_sym(s, trace_adjust_address(iter->tr, *p), flags);
 		trace_seq_putc(s, '\n');
 	}
 
maintainer
#1 lore raw
Quoted 30 lines · On Mon, 10 Mar 2025 22:47:27 +0900 "Masami Hiramatsu (Google)"…

From: Masami Hiramatsu (Google) <[email protected]>

Since the previous boot trace buffer can include module text address in the stacktrace. As same as the kernel text address, convert the module text address using the module address information.

---
Changes in v3:
- Move module_delta to trace_scratch data structure.
- Remove LRU based removed module information.

---
 kernel/trace/trace.c        |   99 +++++++++++++++++++++++++++++++++++++++++--
 kernel/trace/trace.h        |    2 +
 kernel/trace/trace_output.c |    4 +-
 3 files changed, 98 insertions(+), 7 deletions(-)
kernel/trace/trace.c +1 -0
index c3c79908766e..0c1aa1750077 100644
@@ -49,6 +49,7 @@
 #include <linux/fsnotify.h>
 #include <linux/irq_work.h>
 #include <linux/workqueue.h>
+#include <linux/sort.h>
 
 #include <asm/setup.h> /* COMMAND_LINE_SIZE and kaslr_offset() */
 

kernel/trace/trace.c struct trace_mod_entry { · line 6000

Quoted 4 lines
kernel/trace/trace.c +1 -0
@@ -5996,11 +5997,41 @@ struct trace_mod_entry {
 struct trace_scratch {
 	unsigned long		kaslr_addr;
 	unsigned long		nr_entries;
+	long			*module_delta;

Why are we adding this pointer into the persistent memory when it is useless after a crash?

Quoted 13 lines
kernel/trace/trace.c +8 -0
 	struct trace_mod_entry	entries[];
 };
 
 static DEFINE_MUTEX(scratch_mutex);
 
+/**
+ * trace_adjust_address() - Adjust prev boot address to current address.
+ * @tr: Persistent ring buffer's trace_array.
+ * @addr: Address in @tr which is adjusted.
+ */
+unsigned long trace_adjust_address(struct trace_array *tr, unsigned long addr)
+{
+	struct trace_scratch *tscratch;

kernel/trace/trace.c struct trace_mod_entry { · line 6023

Quoted 10 lines
kernel/trace/trace.c +10 -0
+	long *module_delta;
+	int i;
+
+	/* If we don't have last boot delta, return the address */
+	if (!(tr->flags & TRACE_ARRAY_FL_LAST_BOOT))
+		return addr;
+
+	tscratch = tr->scratch;
+	module_delta = READ_ONCE(tscratch->module_delta);
+	if (!tscratch || !tscratch->nr_entries || !module_delta ||

You shouldn't have a !tscratch test just after dereferencing it:

		tscratch->module_delta

Perhaps have it be:

	module_delta = tscratch ? READ_ONCE(tscratch->module_delta) : 0;
	if (!module_delta || !tscratch->nr_entries || tscratch->entries[0].mod_addr > addr)

kernel/trace/trace.c struct trace_mod_entry { · line 6030

Quoted 7 lines
kernel/trace/trace.c +7 -0
+	    tscratch->entries[0].mod_addr > addr)
+		return addr + tr->text_delta;
+
+	/* Note that entries must be sorted. */
+	for (i = 0; i < tscratch->nr_entries; i++)
+		if (addr < tscratch->entries[i].mod_addr)
+			break;

If we are bother sorting it, why not do a binary search here?

Quoted 114 lines
kernel/trace/trace.c +59 -2
+
+	return addr + module_delta[i - 1];
+}
+
 static int save_mod(struct module *mod, void *data)
 {
 	struct trace_array *tr = data;
@@ -6029,6 +6060,7 @@ static int save_mod(struct module *mod, void *data)
 static void update_last_data(struct trace_array *tr)
 {
 	struct trace_scratch *tscratch;
+	long *module_delta;
 
 	if (!(tr->flags & TRACE_ARRAY_FL_BOOT))
 		return;
@@ -6063,6 +6095,8 @@ static void update_last_data(struct trace_array *tr)
 		return;
 
 	tscratch = tr->scratch;
+	module_delta = READ_ONCE(tscratch->module_delta);
+	WRITE_ONCE(tscratch->module_delta, NULL);
 
 	/* Set the persistent ring buffer meta data to this address */
 #ifdef CONFIG_RANDOMIZE_BASE
@@ -6071,6 +6105,8 @@ static void update_last_data(struct trace_array *tr)
 	tscratch->kaslr_addr = 0;
 #endif
 	tr->flags &= ~TRACE_ARRAY_FL_LAST_BOOT;
+
+	kfree(module_delta);
 }
 
 /**
@@ -9342,10 +9378,43 @@ static struct dentry *trace_instance_dir;
 static void
 init_tracer_tracefs(struct trace_array *tr, struct dentry *d_tracer);
 
+static int make_mod_delta(struct module *mod, void *data)
+{
+	struct trace_scratch *tscratch;
+	struct trace_mod_entry *entry;
+	struct trace_array *tr = data;
+	long *module_delta;
+	int i;
+
+	tscratch = tr->scratch;
+	module_delta = READ_ONCE(tscratch->module_delta);
+	for (i = 0; i < tscratch->nr_entries; i++) {
+		entry = &tscratch->entries[i];
+		if (!strcmp(mod->name, entry->mod_name)) {
+			if (mod->state == MODULE_STATE_GOING)
+				module_delta[i] = 0;
+			else
+				module_delta[i] = (unsigned long)mod->mem[MOD_TEXT].base
+						 - entry->mod_addr;
+			break;
+		}
+	}
+	return 0;
+}
+
+static int mod_addr_comp(const void *a, const void *b, const void *data)
+{
+	const struct trace_mod_entry *e1 = a;
+	const struct trace_mod_entry *e2 = b;
+
+	return e1->mod_addr > e2->mod_addr ? 1 : -1;
+}
+
 static void setup_trace_scratch(struct trace_array *tr,
 				struct trace_scratch *tscratch, unsigned int size)
 {
 	struct trace_mod_entry *entry;
+	int i, nr_entries;
 
 	if (!tscratch)
 		return;
@@ -9362,7 +9431,7 @@ static void setup_trace_scratch(struct trace_array *tr,
 		goto reset;
 
 	/* Check if each module name is a valid string */
-	for (int i = 0; i < tscratch->nr_entries; i++) {
+	for (i = 0; i < tscratch->nr_entries; i++) {
 		int n;
 
 		entry = &tscratch->entries[i];
@@ -9376,6 +9445,21 @@ static void setup_trace_scratch(struct trace_array *tr,
 		if (n == MODULE_NAME_LEN)
 			goto reset;
 	}
+
+	nr_entries = i;
+	tscratch->module_delta = kcalloc(nr_entries, sizeof(long), GFP_KERNEL);
+	if (!tscratch->module_delta) {
+		pr_info("module_delta allocation failed. Not able to decode module address.");
+		goto reset;
+	}
+
+	/* Sort the entries so that we can find appropriate module from address. */
+	sort_r(tscratch->entries, nr_entries, sizeof(struct trace_mod_entry),
+	       mod_addr_comp, NULL, NULL);
+
+	/* Scan modules to make text delta for modules. */
+	module_for_each_mod(make_mod_delta, tr);
+
 	return;
  reset:
 	/* Invalid trace modules */
@@ -10101,19 +10185,23 @@ static bool trace_array_active(struct trace_array *tr)
 	return trace_events_enabled(tr, NULL) > 1;
 }
 
-static void trace_module_record(struct module *mod)
+static void trace_module_record(struct module *mod, bool remove)

kernel/trace/trace.c static bool trace_array_active(struct trace_array *tr) · line 10196

Quoted 10 lines
kernel/trace/trace.c +3 -2
 {
 	struct trace_array *tr;
+	unsigned long flags;
 
 	list_for_each_entry(tr, &ftrace_trace_arrays, list) {
+		flags = tr->flags & (TRACE_ARRAY_FL_BOOT | TRACE_ARRAY_FL_LAST_BOOT);
 		/* Update any persistent trace array that has already been started */
-		if ((tr->flags & (TRACE_ARRAY_FL_BOOT | TRACE_ARRAY_FL_LAST_BOOT)) ==
-		    TRACE_ARRAY_FL_BOOT) {
+		if (flags == TRACE_ARRAY_FL_BOOT && !remove) {

Can you rename the parameter from "remove" to "add" so we don't have a double negative.

		if (flags == TRACE_ARRAY_FL_BOOT && add) {
Quoted 12 lines
kernel/trace/trace.c +3 -0
 			/* Only update if the trace array is active */
 			if (trace_array_active(tr)) {
 				guard(mutex)(&scratch_mutex);
 				save_mod(mod, tr);
 			}
+		} else if (flags & TRACE_ARRAY_FL_LAST_BOOT) {
+			/* Update delta if the module loaded in previous boot */
+			make_mod_delta(mod, tr);
 		}
 	}
 }

kernel/trace/trace.c static int trace_module_notify(struct notifier_block *self, · line 10217

Quoted 5 lines
kernel/trace/trace.c +1 -1
@@ -10126,10 +10214,11 @@ static int trace_module_notify(struct notifier_block *self,
 	switch (val) {
 	case MODULE_STATE_COMING:
 		trace_module_add_evals(mod);
-		trace_module_record(mod);
+		trace_module_record(mod, false);
		trace_module_record(mod, true);

kernel/trace/trace.c static int trace_module_notify(struct notifier_block *self, · line 10221

Quoted 4 lines
kernel/trace/trace.c +1 -0
 		break;
 	case MODULE_STATE_GOING:
 		trace_module_remove_evals(mod);
+		trace_module_record(mod, true);
		trace_module_record(mod, false);

kernel/trace/trace.c static int trace_module_notify(struct notifier_block *self, · line 10224

Quoted 4 lines
kernel/trace/trace.c +0 -0
 		break;
 	}
 

-- Steve

author maintainer
#2 lore raw
Quoted 32 lines · On Tue, 11 Mar 2025 10:08:32 -0400 Steven Rostedt <[email protected]> wrote:
Quoted 29 lines · On Mon, 10 Mar 2025 22:47:27 +0900 "Masami Hiramatsu (Google)"…

From: Masami Hiramatsu (Google) <[email protected]>

Since the previous boot trace buffer can include module text address in the stacktrace. As same as the kernel text address, convert the module text address using the module address information.

---
Changes in v3:
- Move module_delta to trace_scratch data structure.
- Remove LRU based removed module information.

---
 kernel/trace/trace.c        |   99 +++++++++++++++++++++++++++++++++++++++++--
 kernel/trace/trace.h        |    2 +
 kernel/trace/trace_output.c |    4 +-
 3 files changed, 98 insertions(+), 7 deletions(-)
kernel/trace/trace.c +1 -0
index c3c79908766e..0c1aa1750077 100644
@@ -49,6 +49,7 @@
 #include <linux/fsnotify.h>
 #include <linux/irq_work.h>
 #include <linux/workqueue.h>
+#include <linux/sort.h>
 
 #include <asm/setup.h> /* COMMAND_LINE_SIZE and kaslr_offset() */
 
Quoted 8 lines

kernel/trace/trace.c struct trace_mod_entry { · line 6000

Quoted 5 lines
kernel/trace/trace.c +1 -0
@@ -5996,11 +5997,41 @@ struct trace_mod_entry {
 struct trace_scratch {
 	unsigned long		kaslr_addr;
 	unsigned long		nr_entries;
+	long			*module_delta;

Why are we adding this pointer into the persistent memory when it is useless after a crash?

This pointer is only usable after reboot. Hmm, but yeah, it should be moved to trace_array again. I confused to copy the nr_entries in here and trace_array, there seems 2 same value saved in both side. I think we should use tscratch->nr_entries to tr->module_delta too.

Quoted 27 lines
Quoted 14 lines
kernel/trace/trace.c +8 -0
 	struct trace_mod_entry	entries[];
 };
 
 static DEFINE_MUTEX(scratch_mutex);
 
+/**
+ * trace_adjust_address() - Adjust prev boot address to current address.
+ * @tr: Persistent ring buffer's trace_array.
+ * @addr: Address in @tr which is adjusted.
+ */
+unsigned long trace_adjust_address(struct trace_array *tr, unsigned long addr)
+{
+	struct trace_scratch *tscratch;

kernel/trace/trace.c struct trace_mod_entry { · line 6023

Quoted 10 lines
kernel/trace/trace.c +10 -0
+	long *module_delta;
+	int i;
+
+	/* If we don't have last boot delta, return the address */
+	if (!(tr->flags & TRACE_ARRAY_FL_LAST_BOOT))
+		return addr;
+
+	tscratch = tr->scratch;
+	module_delta = READ_ONCE(tscratch->module_delta);
+	if (!tscratch || !tscratch->nr_entries || !module_delta ||

You shouldn't have a !tscratch test just after dereferencing it:

Quoted 6 lines
		tscratch->module_delta

Perhaps have it be:

	module_delta = tscratch ? READ_ONCE(tscratch->module_delta) : 0;
	if (!module_delta || !tscratch->nr_entries || tscratch->entries[0].mod_addr > addr)

Good catch!

Quoted 11 lines

kernel/trace/trace.c struct trace_mod_entry { · line 6030

Quoted 9 lines
kernel/trace/trace.c +7 -0
+	    tscratch->entries[0].mod_addr > addr)
+		return addr + tr->text_delta;
+
+	/* Note that entries must be sorted. */
+	for (i = 0; i < tscratch->nr_entries; i++)
+		if (addr < tscratch->entries[i].mod_addr)
+			break;

If we are bother sorting it, why not do a binary search here?

OK.

BTW, currently it does not care whether there are different modules are loaded on the same address (e.g. remove module and load another module), so it could find another module. Anyway, we don't know when the stacktrace is traced, so there is no way to find correct module if any module is unloaded.

I'll add this note somewhere.

Quoted 129 lines
Quoted 115 lines
kernel/trace/trace.c +59 -2
+
+	return addr + module_delta[i - 1];
+}
+
 static int save_mod(struct module *mod, void *data)
 {
 	struct trace_array *tr = data;
@@ -6029,6 +6060,7 @@ static int save_mod(struct module *mod, void *data)
 static void update_last_data(struct trace_array *tr)
 {
 	struct trace_scratch *tscratch;
+	long *module_delta;
 
 	if (!(tr->flags & TRACE_ARRAY_FL_BOOT))
 		return;
@@ -6063,6 +6095,8 @@ static void update_last_data(struct trace_array *tr)
 		return;
 
 	tscratch = tr->scratch;
+	module_delta = READ_ONCE(tscratch->module_delta);
+	WRITE_ONCE(tscratch->module_delta, NULL);
 
 	/* Set the persistent ring buffer meta data to this address */
 #ifdef CONFIG_RANDOMIZE_BASE
@@ -6071,6 +6105,8 @@ static void update_last_data(struct trace_array *tr)
 	tscratch->kaslr_addr = 0;
 #endif
 	tr->flags &= ~TRACE_ARRAY_FL_LAST_BOOT;
+
+	kfree(module_delta);
 }
 
 /**
@@ -9342,10 +9378,43 @@ static struct dentry *trace_instance_dir;
 static void
 init_tracer_tracefs(struct trace_array *tr, struct dentry *d_tracer);
 
+static int make_mod_delta(struct module *mod, void *data)
+{
+	struct trace_scratch *tscratch;
+	struct trace_mod_entry *entry;
+	struct trace_array *tr = data;
+	long *module_delta;
+	int i;
+
+	tscratch = tr->scratch;
+	module_delta = READ_ONCE(tscratch->module_delta);
+	for (i = 0; i < tscratch->nr_entries; i++) {
+		entry = &tscratch->entries[i];
+		if (!strcmp(mod->name, entry->mod_name)) {
+			if (mod->state == MODULE_STATE_GOING)
+				module_delta[i] = 0;
+			else
+				module_delta[i] = (unsigned long)mod->mem[MOD_TEXT].base
+						 - entry->mod_addr;
+			break;
+		}
+	}
+	return 0;
+}
+
+static int mod_addr_comp(const void *a, const void *b, const void *data)
+{
+	const struct trace_mod_entry *e1 = a;
+	const struct trace_mod_entry *e2 = b;
+
+	return e1->mod_addr > e2->mod_addr ? 1 : -1;
+}
+
 static void setup_trace_scratch(struct trace_array *tr,
 				struct trace_scratch *tscratch, unsigned int size)
 {
 	struct trace_mod_entry *entry;
+	int i, nr_entries;
 
 	if (!tscratch)
 		return;
@@ -9362,7 +9431,7 @@ static void setup_trace_scratch(struct trace_array *tr,
 		goto reset;
 
 	/* Check if each module name is a valid string */
-	for (int i = 0; i < tscratch->nr_entries; i++) {
+	for (i = 0; i < tscratch->nr_entries; i++) {
 		int n;
 
 		entry = &tscratch->entries[i];
@@ -9376,6 +9445,21 @@ static void setup_trace_scratch(struct trace_array *tr,
 		if (n == MODULE_NAME_LEN)
 			goto reset;
 	}
+
+	nr_entries = i;
+	tscratch->module_delta = kcalloc(nr_entries, sizeof(long), GFP_KERNEL);
+	if (!tscratch->module_delta) {
+		pr_info("module_delta allocation failed. Not able to decode module address.");
+		goto reset;
+	}
+
+	/* Sort the entries so that we can find appropriate module from address. */
+	sort_r(tscratch->entries, nr_entries, sizeof(struct trace_mod_entry),
+	       mod_addr_comp, NULL, NULL);
+
+	/* Scan modules to make text delta for modules. */
+	module_for_each_mod(make_mod_delta, tr);
+
 	return;
  reset:
 	/* Invalid trace modules */
@@ -10101,19 +10185,23 @@ static bool trace_array_active(struct trace_array *tr)
 	return trace_events_enabled(tr, NULL) > 1;
 }
 
-static void trace_module_record(struct module *mod)
+static void trace_module_record(struct module *mod, bool remove)

kernel/trace/trace.c static bool trace_array_active(struct trace_array *tr) · line 10196

Quoted 10 lines
kernel/trace/trace.c +3 -2
 {
 	struct trace_array *tr;
+	unsigned long flags;
 
 	list_for_each_entry(tr, &ftrace_trace_arrays, list) {
+		flags = tr->flags & (TRACE_ARRAY_FL_BOOT | TRACE_ARRAY_FL_LAST_BOOT);
 		/* Update any persistent trace array that has already been started */
-		if ((tr->flags & (TRACE_ARRAY_FL_BOOT | TRACE_ARRAY_FL_LAST_BOOT)) ==
-		    TRACE_ARRAY_FL_BOOT) {
+		if (flags == TRACE_ARRAY_FL_BOOT && !remove) {

Can you rename the parameter from "remove" to "add" so we don't have a double negative.

Quoted 1 line
		if (flags == TRACE_ARRAY_FL_BOOT && add) {

Ah, OK.

Quoted 22 lines
Quoted 14 lines
kernel/trace/trace.c +3 -0
 			/* Only update if the trace array is active */
 			if (trace_array_active(tr)) {
 				guard(mutex)(&scratch_mutex);
 				save_mod(mod, tr);
 			}
+		} else if (flags & TRACE_ARRAY_FL_LAST_BOOT) {
+			/* Update delta if the module loaded in previous boot */
+			make_mod_delta(mod, tr);
 		}
 	}
 }

kernel/trace/trace.c static int trace_module_notify(struct notifier_block *self, · line 10217

Quoted 5 lines
kernel/trace/trace.c +1 -1
@@ -10126,10 +10214,11 @@ static int trace_module_notify(struct notifier_block *self,
 	switch (val) {
 	case MODULE_STATE_COMING:
 		trace_module_add_evals(mod);
-		trace_module_record(mod);
+		trace_module_record(mod, false);
		trace_module_record(mod, true);
Quoted 6 lines

kernel/trace/trace.c static int trace_module_notify(struct notifier_block *self, · line 10221

Quoted 4 lines
kernel/trace/trace.c +1 -0
 		break;
 	case MODULE_STATE_GOING:
 		trace_module_remove_evals(mod);
+		trace_module_record(mod, true);
		trace_module_record(mod, false);

OK.

Quoted 8 lines

kernel/trace/trace.c static int trace_module_notify(struct notifier_block *self, · line 10224

Quoted 5 lines
kernel/trace/trace.c +0 -0
 		break;
 	}
 

-- Steve

Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
#3 lore raw
In reply to Masami Hiramatsu (Google)
kernel test robot noticed the following build errors:

Hi Masami,

kernel test robot noticed the following build errors:

[auto build test ERROR on trace/for-next]
[cannot apply to linus/master v6.14-rc6 next-20250311]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Masami-Hiramatsu-Google/tracing-Show-last-module-text-symbols-in-the-stacktrace/20250310-214849
base:   https://git.kernel.org/pub/scm/linux/kernel/git/trace/linux-trace for-next
patch link:    https://lore.kernel.org/r/174161444691.1063601.16690699136628689205.stgit%40devnote2
patch subject: [PATCH v3] tracing: Show last module text symbols in the stacktrace
config: i386-buildonly-randconfig-004-20250311 (https://download.01.org/0day-ci/archive/20250311/[email protected]/config)
compiler: clang version 19.1.7 (https://github.com/llvm/llvm-project cd708029e0b2869e80abe31ddb175f7c35361f90)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250311/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

kernel/trace/trace.c:6072:38: error: incomplete definition of type 'struct module'

    6072 |         entry->mod_addr = (unsigned long)mod->mem[MOD_TEXT].base;
         |                                          ~~~^
   include/linux/printk.h:394:8: note: forward declaration of 'struct module'
     394 | struct module;
         |        ^
   kernel/trace/trace.c:6072:44: error: use of undeclared identifier 'MOD_TEXT'
    6072 |         entry->mod_addr = (unsigned long)mod->mem[MOD_TEXT].base;
         |                                                   ^
   kernel/trace/trace.c:6073:30: error: incomplete definition of type 'struct module'
    6073 |         strscpy(entry->mod_name, mod->name);
         |                                  ~~~^
   include/linux/string.h:114:55: note: expanded from macro 'strscpy'
     114 |         CONCATENATE(__strscpy, COUNT_ARGS(__VA_ARGS__))(dst, src, __VA_ARGS__)
         |                                                              ^~~
   include/linux/string.h:80:21: note: expanded from macro '__strscpy0'
      80 |         sized_strscpy(dst, src, sizeof(dst) + __must_be_array(dst) +    \
         |                            ^~~
   include/linux/printk.h:394:8: note: forward declaration of 'struct module'
     394 | struct module;
         |        ^
   kernel/trace/trace.c:9411:18: error: incomplete definition of type 'struct module'
    9411 |                 if (!strcmp(mod->name, entry->mod_name)) {
         |                             ~~~^
   include/linux/printk.h:394:8: note: forward declaration of 'struct module'
     394 | struct module;
         |        ^
   kernel/trace/trace.c:9412:11: error: incomplete definition of type 'struct module'
    9412 |                         if (mod->state == MODULE_STATE_GOING)
         |                             ~~~^
   include/linux/printk.h:394:8: note: forward declaration of 'struct module'
     394 | struct module;
         |        ^
>> kernel/trace/trace.c:9412:22: error: use of undeclared identifier 'MODULE_STATE_GOING'
    9412 |                         if (mod->state == MODULE_STATE_GOING)
         |                                           ^
   kernel/trace/trace.c:9415:41: error: incomplete definition of type 'struct module'
    9415 |                                 module_delta[i] = (unsigned long)mod->mem[MOD_TEXT].base
         |                                                                  ~~~^
   include/linux/printk.h:394:8: note: forward declaration of 'struct module'
     394 | struct module;
         |        ^
   kernel/trace/trace.c:9415:47: error: use of undeclared identifier 'MOD_TEXT'
    9415 |                                 module_delta[i] = (unsigned long)mod->mem[MOD_TEXT].base
         |                                                                           ^

8 errors generated.

vim +/MODULE_STATE_GOING +9412 kernel/trace/trace.c

9398
9399 static int make_mod_delta(struct module *mod, void *data)
9400 {
9401 struct trace_scratch *tscratch;
9402 struct trace_mod_entry *entry;
9403 struct trace_array *tr = data;
9404 long *module_delta;
9405 int i;
9406
9407 tscratch = tr->scratch;
9408 module_delta = READ_ONCE(tscratch->module_delta);
9409 for (i = 0; i < tscratch->nr_entries; i++) {
9410 entry = &tscratch->entries[i];
9411 if (!strcmp(mod->name, entry->mod_name)) {

> 9412				if (mod->state == MODULE_STATE_GOING)

9413 module_delta[i] = 0;
9414 else
9415 module_delta[i] = (unsigned long)mod->mem[MOD_TEXT].base
9416 - entry->mod_addr;
9417 break;
9418 }
9419 }
9420 return 0;
9421 }
9422

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#4 lore raw
In reply to Masami Hiramatsu (Google)
kernel test robot noticed the following build errors:

Hi Masami,

kernel test robot noticed the following build errors:

[auto build test ERROR on trace/for-next]
[cannot apply to linus/master v6.14-rc6 next-20250311]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Masami-Hiramatsu-Google/tracing-Show-last-module-text-symbols-in-the-stacktrace/20250310-214849
base:   https://git.kernel.org/pub/scm/linux/kernel/git/trace/linux-trace for-next
patch link:    https://lore.kernel.org/r/174161444691.1063601.16690699136628689205.stgit%40devnote2
patch subject: [PATCH v3] tracing: Show last module text symbols in the stacktrace
config: i386-buildonly-randconfig-001-20250311 (https://download.01.org/0day-ci/archive/20250311/[email protected]/config)
compiler: gcc-12 (Debian 12.2.0-14) 12.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250311/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

kernel/trace/trace.c: In function 'save_mod':
kernel/trace/trace.c:6072:45: error: invalid use of undefined type 'struct module'

    6072 |         entry->mod_addr = (unsigned long)mod->mem[MOD_TEXT].base;
         |                                             ^~
   kernel/trace/trace.c:6072:51: error: 'MOD_TEXT' undeclared (first use in this function)
    6072 |         entry->mod_addr = (unsigned long)mod->mem[MOD_TEXT].base;
         |                                                   ^~~~~~~~
   kernel/trace/trace.c:6072:51: note: each undeclared identifier is reported only once for each function it appears in
   In file included from arch/x86/include/asm/page_32.h:18,
                    from arch/x86/include/asm/page.h:14,
                    from arch/x86/include/asm/thread_info.h:12,
                    from include/linux/thread_info.h:60,
                    from include/linux/spinlock.h:60,
                    from include/linux/mmzone.h:8,
                    from include/linux/gfp.h:7,
                    from include/linux/mm.h:7,
                    from include/linux/ring_buffer.h:5,
                    from kernel/trace/trace.c:15:
   kernel/trace/trace.c:6073:37: error: invalid use of undefined type 'struct module'
    6073 |         strscpy(entry->mod_name, mod->name);
         |                                     ^~
   include/linux/string.h:80:28: note: in definition of macro '__strscpy0'
      80 |         sized_strscpy(dst, src, sizeof(dst) + __must_be_array(dst) +    \
         |                            ^~~
   kernel/trace/trace.c:6073:9: note: in expansion of macro 'strscpy'
    6073 |         strscpy(entry->mod_name, mod->name);
         |         ^~~~~~~
   In file included from include/asm-generic/bug.h:5,
                    from arch/x86/include/asm/bug.h:99,
                    from include/linux/bug.h:5,
                    from include/linux/mmdebug.h:5,
                    from include/linux/mm.h:6:
   kernel/trace/trace.c:6073:37: error: invalid use of undefined type 'struct module'
    6073 |         strscpy(entry->mod_name, mod->name);
         |                                     ^~
   include/linux/compiler.h:197:79: note: in definition of macro '__BUILD_BUG_ON_ZERO_MSG'
     197 | #define __BUILD_BUG_ON_ZERO_MSG(e, msg) ((int)sizeof(struct {_Static_assert(!(e), msg);}))
         |                                                                               ^
   include/linux/compiler.h:211:33: note: in expansion of macro '__annotated'
     211 |         __BUILD_BUG_ON_ZERO_MSG(__annotated(p, nonstring), "must be cstr (NUL-terminated)")
         |                                 ^~~~~~~~~~~
   include/linux/string.h:81:55: note: in expansion of macro '__must_be_cstr'
      81 |                                 __must_be_cstr(dst) + __must_be_cstr(src))
         |                                                       ^~~~~~~~~~~~~~
   include/linux/args.h:25:24: note: in expansion of macro '__strscpy0'
      25 | #define __CONCAT(a, b) a ## b
         |                        ^
   kernel/trace/trace.c:6073:9: note: in expansion of macro 'strscpy'
    6073 |         strscpy(entry->mod_name, mod->name);
         |         ^~~~~~~
   kernel/trace/trace.c: In function 'make_mod_delta':
   kernel/trace/trace.c:9411:32: error: invalid use of undefined type 'struct module'
    9411 |                 if (!strcmp(mod->name, entry->mod_name)) {
         |                                ^~
   kernel/trace/trace.c:9412:32: error: invalid use of undefined type 'struct module'
    9412 |                         if (mod->state == MODULE_STATE_GOING)
         |                                ^~
>> kernel/trace/trace.c:9412:43: error: 'MODULE_STATE_GOING' undeclared (first use in this function)
    9412 |                         if (mod->state == MODULE_STATE_GOING)
         |                                           ^~~~~~~~~~~~~~~~~~
   kernel/trace/trace.c:9415:69: error: invalid use of undefined type 'struct module'
    9415 |                                 module_delta[i] = (unsigned long)mod->mem[MOD_TEXT].base
         |                                                                     ^~
   kernel/trace/trace.c:9415:75: error: 'MOD_TEXT' undeclared (first use in this function)
    9415 |                                 module_delta[i] = (unsigned long)mod->mem[MOD_TEXT].base
         |                                                                           ^~~~~~~~

vim +/MODULE_STATE_GOING +9412 kernel/trace/trace.c

9398
9399 static int make_mod_delta(struct module *mod, void *data)
9400 {
9401 struct trace_scratch *tscratch;
9402 struct trace_mod_entry *entry;
9403 struct trace_array *tr = data;
9404 long *module_delta;
9405 int i;
9406
9407 tscratch = tr->scratch;
9408 module_delta = READ_ONCE(tscratch->module_delta);
9409 for (i = 0; i < tscratch->nr_entries; i++) {
9410 entry = &tscratch->entries[i];

> 9411			if (!strcmp(mod->name, entry->mod_name)) {
> 9412				if (mod->state == MODULE_STATE_GOING)

9413 module_delta[i] = 0;
9414 else
9415 module_delta[i] = (unsigned long)mod->mem[MOD_TEXT].base
9416 - entry->mod_addr;
9417 break;
9418 }
9419 }
9420 return 0;
9421 }
9422

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki