Skip to contentSkip to conversation

tracing: Override trace_instance parameter

PATCH Superseded Masami Hiramatsu (Google) 2 participants 5 messages Oct 2

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Masami Hiramatsu (Google)

author maintainer
#0 lore raw

v2 cover tracing: Override trace_instance parameter

Hi,

Here is the 2nd version of the patches to change the "trace_instance=" kernel parameter to be overriden by latter parameters.
The previous version is here:

https://lore.kernel.org/all/178982869661.289221.2143320719673148321.stgit@devnote2/

In this version, I just rebased it on probes/core.

Currently if the user specifies the same name trace_instance at boot time, the later one is skipped.
However, this does not allow user to override the trace_instance setting via bootloader if user specifies the default trace_instance in the bootconfig, because the bootconfig parameter is prepend to the kernel cmdline. (IOW, the bootconfig expects kernel parameters are LAST_WIN policy.)

To fix this issue, the [1/2] splits the command line parsing loop and trace instance initialization loop. The parser checks the overlap of the trace instance name and override by the later one if those have the same name.

This series also have a test set as [2/2], which is based on ftrace boottime test patchset [1], which is currently on probes/core branch.

[1] https://lore.kernel.org/all/178649540853.438282.4271870161169100680.stgit@devnote2/

Thank you,

---
base-commit: b15df7805dc52555e7c867d33a496edf5be11289

Masami Hiramatsu (Google) (2):
tracing: Override the same name trace_instance= by latter one selftests/ftrace: Add test case for overriding trace_instance parameter

 Documentation/admin-guide/kernel-parameters.txt    |    7 +
 Documentation/trace/debugging.rst                  |   15 ++
 kernel/trace/trace.c                               |  130 ++++++++++++++------
 .../cmdline-07-trace-instance-override.bconf       |    2 
 .../cmdline-07-trace-instance-override.cmdline     |    1 
 .../tests/cmdline-07-trace-instance-override.sh    |   56 +++++++++
 6 files changed, 173 insertions(+), 38 deletions(-)
 create mode 100644 tools/testing/selftests/ftrace/boottime/bootconfigs/cmdline-07-trace-instance-override.bconf
 create mode 100644 tools/testing/selftests/ftrace/boottime/cmdlines/cmdline-07-trace-instance-override.cmdline
 create mode 100644 tools/testing/selftests/ftrace/boottime/tests/cmdline-07-trace-instance-override.sh

--
Masami Hiramatsu (Google) <[email protected]>

author maintainer
#1 lore raw

PATCH v2 1/2 tracing: Override the same name trace_instance= by latter one

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

Override the same name boot instance by the latter parameter. This allows user to override the trace instance in bootconfig by the cmdline from bootloader.

For example, set default trace_instance parameter in bootconfig e.g.:

  kernel {
     reserve_mem=12M:32M:trace
     trace_instance=boot_map@trace
  }

This just add a persistent trace instance.

And when you need to record events, you can override the boot_map trace instance from bootloader:

trace_instance=boot_map@trace,sched:*

Then the boot_map instance is start tracing sched:* events.

---
 Documentation/admin-guide/kernel-parameters.txt |    7 +
 Documentation/trace/debugging.rst               |   15 +++
 kernel/trace/trace.c                            |  130 ++++++++++++++++-------
 3 files changed, 114 insertions(+), 38 deletions(-)
Documentation/admin-guide/kernel-parameters.txt +7 -0
index e75344f4e0cd..0695ba0234c9 100644
@@ -7806,6 +7806,13 @@ Kernel parameters
 
 			NB: Both the mapped address and size must be page aligned for the architecture.
 
+			Multiple trace_instance= options can be specified to
+			create multiple instances. If the same instance name is
+			specified more than once, the latter one will override
+			any earlier definitions for that instance. This allows
+			overriding an instance configuration that was defined
+			earlier on the command line or via bootconfig.
+
 			See also Documentation/trace/debugging.rst
 
 
Documentation/trace/debugging.rst +15 -0
index bca1710d92bf..b66cc070ae79 100644
@@ -178,3 +178,18 @@ instance without stopping the trace.
 Note that this "backup" instance is readonly, and will be removed automatically
 if you clear the trace data or read out all trace data from the "trace_pipe"
 or the "trace_pipe_raw" files.
+
+Overriding trace instances
+--------------------------
+
+Multiple ``trace_instance=`` options can be specified on the kernel command
+line to create multiple instances. If the same instance name is specified
+more than once, the latter definition will override any previous definitions
+for that instance.
+
+This is useful when an instance configuration is defined in bootconfig, but
+needs to be overridden or modified from the bootloader command line (for
+example, to change the enabled events or add flags like ``traceoff``)::
+
+  trace_instance=boot_map^traceoff@trace
+
kernel/trace/trace.c +92 -38

235 lines not on this page · raw message

author maintainer
#4 lore raw

PATCH v2 2/2 selftests/ftrace: Add test case for overriding trace_instance parameter

In reply to Masami Hiramatsu (Google)

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

Add a boot-time tracing test case to verify that specifying multiple trace_instance= parameters with the same instance name overrides earlier definitions:

- Verifies that a kernel command-line trace_instance= parameter overrides an earlier trace instance defined via bootconfig (kernel.trace_instance).
- Verifies that a latter kernel command-line trace_instance= parameter overrides an earlier trace_instance= on the same command line.
- Confirms that the overridden instance has its previous events disabled, new events enabled, and flags (such as traceoff) properly applied.

Assisted-by Antigravity:gemini-3.8-flash
---
 .../cmdline-07-trace-instance-override.bconf       |    2 +
 .../cmdline-07-trace-instance-override.cmdline     |    1 
 .../tests/cmdline-07-trace-instance-override.sh    |   56 ++++++++++++++++++++
 3 files changed, 59 insertions(+)
 create mode 100644 tools/testing/selftests/ftrace/boottime/bootconfigs/cmdline-07-trace-instance-override.bconf
 create mode 100644 tools/testing/selftests/ftrace/boottime/cmdlines/cmdline-07-trace-instance-override.cmdline
 create mode 100644 tools/testing/selftests/ftrace/boottime/tests/cmdline-07-trace-instance-override.sh
tools/testing/selftests/ftrace/boottime/bootconfigs/cmdline-07-trace-instance-override.bconf new file +2 -0
new file mode 100644
index 000000000000..2551da39f722
@@ -0,0 +1,2 @@
+kernel.trace_instance = "foo,sched:sched_switch"
+
tools/testing/selftests/ftrace/boottime/cmdlines/cmdline-07-trace-instance-override.cmdline new file +1 -0
new file mode 100644
index 000000000000..1953eb5d6f80
@@ -0,0 +1 @@
+trace_instance=foo^traceoff,sched:sched_waking trace_instance=bar,sched:sched_switch trace_instance=bar^traceoff,irq:irq_handler_entry
tools/testing/selftests/ftrace/boottime/tests/cmdline-07-trace-instance-override.sh new file +56 -0

56 lines not on this page · raw message

#2 lore raw

Re: [PATCH v2 1/2] tracing: Override the same name trace_instance= by latter one

In reply to Masami Hiramatsu (Google)
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] tracing: heap out-of-bounds write in parse_boot_instance_info due to unescaped tabs - [Medium]…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] tracing: heap out-of-bounds write in parse_boot_instance_info due to unescaped tabs
- [Medium] tracing: duplicate trace array creation via implicit backup lookup causes tracefs init failure --

commit e7d9ac9646a2e104af11665c8c61543d20737c13
Author: Masami Hiramatsu (Google) <[email protected]>

tracing: Override the same name trace_instance= by latter one

Override the same name boot instance by the latter parameter. This allows user to override the trace instance in bootconfig by the cmdline from bootloader.

Quoted 12 lines
kernel/trace/trace.c +1 -0
index e4a490d3d08c8..4590d38969450 100644
@@ -308,6 +309,7 @@ static int __init boot_instance(char *str)
 
 	ret = snprintf(slot, left, "%s\t", str);
 	boot_instance_index += ret;
+	nr_boot_instances++;
 
 	return 1;
 }

[ ... ]

Quoted 2 lines

+__init static void parse_boot_instance_info(struct boot_instance *boot_instances)
{

[ ... ]

Quoted 3 lines
kernel/trace/trace.c +3 -0
+	while ((events = strsep(&str, "\t"))) {
+		if (!*events)
+			continue;

[ ... ]

kernel/trace/trace.c __init static void enable_instances(void) · line 9767

Quoted 11 lines
kernel/trace/trace.c +9 -0
+		for (i = 0; i < idx; i++) {
+			/* Override by the last defined instance */
+			if (!strcmp(name, boot_instances[i].name))
+				break;
 		}
 
+		boot_instances[i].name = name;
+		boot_instances[i].flag_delim = flag_delim;
+		boot_instances[i].addr_delim = addr_delim;
+		boot_instances[i].backup = backup;
+		boot_instances[i].events = events;

[Severity: High]
If a user provides a quoted parameter containing a literal tab (for example, trace_instance="foo\tbar"), won't strsep(&str, "\t") split it into more tokens than what was originally counted in nr_boot_instances?

This loop appears to write beyond the bounds of the boot_instances array when there are embedded tabs, which could lead to memory corruption and a kernel panic during early boot.

Quoted 28 lines
kernel/trace/trace.c +28 -0
+		if (i == idx)
+			idx++;
+	}
+
+	nr_boot_instances = idx;
+}
+
+__init static void enable_instances(void)
+{
+	struct boot_instance *boot_instances __free(kfree) = NULL;
+	struct boot_instance *bi;
+	struct trace_array *tr;
+	bool memmap_area = false;
+	char *tok;
+
+	if (!boot_instance_index)
+		return;
+
+	/* Parse the instance name to check the overlap */
+	boot_instances = kmalloc_array(nr_boot_instances,
+				       sizeof(*boot_instances), GFP_KERNEL);
+	if (!boot_instances)
+		return;
+
+	parse_boot_instance_info(boot_instances);
+
+	for (int i = 0; i < nr_boot_instances; i++) {
+		char *rname __free(kfree) = NULL;

[ ... ]

Quoted 11 lines
kernel/trace/trace.c +2 -7
-		if (trace_array_find(name)) {
-			pr_warn("Tracing: Instance %s already exists\n", name);
-			continue;
-		}
-
-		if (backup) {
-			if (backup_instance_area(backup, &addr, &size) < 0)
+		if (bi->backup) {
+			if (backup_instance_area(bi->backup, &addr, &size) < 0)
 				continue;
 		}

[ ... ]

kernel/trace/trace.c __init static void enable_instances(void) · line 9882

Quoted 2 lines
kernel/trace/trace.c +1 -1
-		tr = trace_array_create_systems(name, NULL, addr, size);
+		tr = trace_array_create_systems(bi->name, NULL, addr, size);

[Severity: Medium]
With the trace_array_find() check removed, does this unconditionally create a new trace array, even if one implicitly exists?

If a user specifies a trace_instance with a backup pointing to another instance defined later (e.g., trace_instance=foo=bar trace_instance=bar), backup_instance_area() will implicitly create "bar" via trace_array_get_by_name().

When the loop processes "bar" explicitly later on, it seems this code will call trace_array_create_systems("bar", ...) a second time, duplicating the trace array in ftrace_trace_arrays. This duplicate could cause tracefs_create_dir() to fail and abort initialization of subsequent trace instances.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/179091984881.537422.15308457677039592540.stgit@devnote2?part=1
author maintainer
#3 lore raw

Re: [PATCH v2 1/2] tracing: Override the same name trace_instance= by latter one

Quoted 60 lines · On Fri, 02 Oct 2026 09:14:31 +0000 [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] tracing: heap out-of-bounds write in parse_boot_instance_info due to unescaped tabs
- [Medium] tracing: duplicate trace array creation via implicit backup lookup causes tracefs init failure --

commit e7d9ac9646a2e104af11665c8c61543d20737c13
Author: Masami Hiramatsu (Google) <[email protected]>

tracing: Override the same name trace_instance= by latter one

Override the same name boot instance by the latter parameter. This allows user to override the trace instance in bootconfig by
the cmdline from bootloader.

Quoted 12 lines
kernel/trace/trace.c +1 -0
index e4a490d3d08c8..4590d38969450 100644
@@ -308,6 +309,7 @@ static int __init boot_instance(char *str)
 
 	ret = snprintf(slot, left, "%s\t", str);
 	boot_instance_index += ret;
+	nr_boot_instances++;
 
 	return 1;
 }  

[ ... ]

Quoted 2 lines

+__init static void parse_boot_instance_info(struct boot_instance *boot_instances) {

[ ... ]

Quoted 3 lines
kernel/trace/trace.c +3 -0
+	while ((events = strsep(&str, "\t"))) {
+		if (!*events)
+			continue;  

[ ... ]

kernel/trace/trace.c __init static void enable_instances(void) · line 9767

Quoted 11 lines
kernel/trace/trace.c +9 -0
+		for (i = 0; i < idx; i++) {
+			/* Override by the last defined instance */
+			if (!strcmp(name, boot_instances[i].name))
+				break;
 		}
 
+		boot_instances[i].name = name;
+		boot_instances[i].flag_delim = flag_delim;
+		boot_instances[i].addr_delim = addr_delim;
+		boot_instances[i].backup = backup;
+		boot_instances[i].events = events;  

[Severity: High]
If a user provides a quoted parameter containing a literal tab (for example, trace_instance="foo\tbar"), won't strsep(&str, "\t") split it into more tokens than what was originally counted in nr_boot_instances?

Quoted 3 lines

This loop appears to write beyond the bounds of the boot_instances array when there are embedded tabs, which could lead to memory corruption and a kernel panic during early boot.

Ah, OK. Such usage is not recommended but it can happen and causes array index overrun.

Quoted 49 lines
Quoted 30 lines
kernel/trace/trace.c +21 -0
+		if (i == idx)
+			idx++;
+	}
+
+	nr_boot_instances = idx;
+}
+
+__init static void enable_instances(void)
+{
+	struct boot_instance *boot_instances __free(kfree) = NULL;
+	struct boot_instance *bi;
+	struct trace_array *tr;
+	bool memmap_area = false;
+	char *tok;
+
+	if (!boot_instance_index)
+		return;
+
+	/* Parse the instance name to check the overlap */
+	boot_instances = kmalloc_array(nr_boot_instances,
+				       sizeof(*boot_instances),

GFP_KERNEL);

kernel/trace/trace.c +7 -0
+	if (!boot_instances)
+		return;
+
+	parse_boot_instance_info(boot_instances);
+
+	for (int i = 0; i < nr_boot_instances; i++) {
+		char *rname __free(kfree) = NULL;  

[ ... ]

Quoted 13 lines
kernel/trace/trace.c +0 -2
-		if (trace_array_find(name)) {
-			pr_warn("Tracing: Instance %s already

exists\n", name);

kernel/trace/trace.c +0 -5
-			continue;
-		}
-
-		if (backup) {
-			if (backup_instance_area(backup, &addr,

&size) < 0)

kernel/trace/trace.c +2 -0
+		if (bi->backup) {
+			if (backup_instance_area(bi->backup,

&addr, &size) < 0) continue;
}

[ ... ]

Quoted 8 lines
Quoted 4 lines
kernel/trace/trace.c +0 -1
-		tr = trace_array_create_systems(name, NULL, addr,

size);

kernel/trace/trace.c +1 -0
+		tr = trace_array_create_systems(bi->name, NULL,

addr, size);

[Severity: Medium]
With the trace_array_find() check removed, does this unconditionally create a new trace array, even if one implicitly exists?

parse_boot_instance_info() already checks the overlaps.

Quoted 5 lines

If a user specifies a trace_instance with a backup pointing to another instance defined later (e.g., trace_instance=foo=bar trace_instance=bar), backup_instance_area() will implicitly create "bar" via trace_array_get_by_name().

Ah, that's a good point! we should check it in
parse_boot_instance_info().

Quoted 7 lines

When the loop processes "bar" explicitly later on, it seems this code will call trace_array_create_systems("bar", ...) a second time, duplicating the trace array in ftrace_trace_arrays. This duplicate could cause tracefs_create_dir() to fail and abort initialization of subsequent trace instances.

Thank you,

Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>