From nobody Thu Sep 24 21:46:49 2026 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 D1769323416; Sat, 19 Sep 2026 14:38:31 +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=1789828713; cv=none; b=igf0x/atJP57gsGSO+4IVFZ+Jj3/nFzO1iL7KzcHHNwKvHyKVu9zVmVC8g75Y5PBsrerKt+QYPD6ffHsT0VmbRr4k3G2LTQu/a4ROrgBI9noZttEZdB3Hx8efq0uj/b/7koHNmb1gQwNWDX9p/3ZsIzQ0lo1ABsTrIv7Ac9MvhM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789828713; c=relaxed/simple; bh=WkFwwEGwXunA6CRjKdvA161yL92eNohakoz6vokz+dE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=eIKqOLFvueDuIm0Y68oGp5saA+6zYO+ADctY71lzgcB1aIPuQvqx3/X/KZT3D6uYrRnfeqcP9Y4EVoySazqIn7J4nfZ3Hb2FJkvLbeO/nHXkHzm2UcWmMVArlCxoOWflRNRGeIgcoDrQ+tujV9YEKu1QRpRqqWSbPB4aPT8xoxU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=hi3vrJxu; 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="hi3vrJxu" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 29FEF1F000FF; Sat, 19 Sep 2026 14:38:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1789828711; bh=wLd9E546h4iUqMUIOwgazII1NOQSfP8ntwUQaQbvMes=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=hi3vrJxuXqZqJ8ARW7D/+Ng99C8uJRx4h4qL1dL0OqTj9VvpzOfd8019PF5EvS0O8 /F7tZh+tjgW4KY49O4S2IzvoQylrYPCUZ6R92yJ4jGBbgSZRKU6VIX4mFj2vx1fvTn +Qyk/UXsIP9kREt99y9AxALrUISQfJEOZCKW5cNSL4PJrhKOXb1oGur4NMHxO/oF/W kQ71ON1Nj3M/47twOOAvVIOxd4bvOygNr005dKe6XHiSJ89Swf57NeI3NRp8g63Kmk 30gSvSfoq9Qik1noGesk2reLGHYPOhEG5wuMJE1IZ7Vu5RcfwvQKwbqnMGpVVPsLkJ yTEKolPv63uiw== From: "Masami Hiramatsu (Google)" To: Jonathan Corbet , Steven Rostedt , Masami Hiramatsu , Shuah Khan Cc: Shuah Khan , Mathieu Desnoyers , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH 1/2] tracing: Override the same name trace_instance= by latter one Date: Sat, 19 Sep 2026 23:38:26 +0900 Message-ID: <178982870684.289221.3913908968761883887.stgit@devnote2> X-Mailer: git-send-email 2.43.0 In-Reply-To: <178982869661.289221.2143320719673148321.stgit@devnote2> References: <178982869661.289221.2143320719673148321.stgit@devnote2> User-Agent: StGit/0.19 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 From: Masami Hiramatsu (Google) 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=3D12M:32M:trace trace_instance=3Dboot_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=3Dboot_map@trace,sched:* Then the boot_map instance is start tracing sched:* events. Signed-off-by: Masami Hiramatsu (Google) --- Documentation/admin-guide/kernel-parameters.txt | 7 + Documentation/trace/debugging.rst | 15 +++ kernel/trace/trace.c | 127 +++++++++++++++++--= ---- 3 files changed, 115 insertions(+), 34 deletions(-) diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentatio= n/admin-guide/kernel-parameters.txt index b5493a7f8f22..232edcd9f2c9 100644 --- a/Documentation/admin-guide/kernel-parameters.txt +++ b/Documentation/admin-guide/kernel-parameters.txt @@ -7768,6 +7768,13 @@ Kernel parameters =20 NB: Both the mapped address and size must be page aligned for the archi= tecture. =20 + Multiple trace_instance=3D 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 =20 =20 diff --git a/Documentation/trace/debugging.rst b/Documentation/trace/debugg= ing.rst index bca1710d92bf..b66cc070ae79 100644 --- a/Documentation/trace/debugging.rst +++ b/Documentation/trace/debugging.rst @@ -178,3 +178,18 @@ instance without stopping the trace. Note that this "backup" instance is readonly, and will be removed automati= cally if you clear the trace data or read out all trace data from the "trace_pip= e" or the "trace_pipe_raw" files. + +Overriding trace instances +-------------------------- + +Multiple ``trace_instance=3D`` options can be specified on the kernel comm= and +line to create multiple instances. If the same instance name is specified +more than once, the latter definition will override any previous definitio= ns +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=3Dboot_map^traceoff@trace + diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c index c9e182d40059..39a6eeb52d28 100644 --- a/kernel/trace/trace.c +++ b/kernel/trace/trace.c @@ -220,6 +220,7 @@ static char *default_bootup_tracer; =20 static char boot_instance_info[COMMAND_LINE_SIZE] __initdata; static int boot_instance_index; +static int nr_boot_instances; =20 /* * Repeated boot parameters, including Bootconfig array expansions, need @@ -308,6 +309,7 @@ static int __init boot_instance(char *str) =20 ret =3D snprintf(slot, left, "%s\t", str); boot_instance_index +=3D ret; + nr_boot_instances++; =20 return 1; } @@ -9611,33 +9613,42 @@ __init static int backup_instance_area(const char *= backup, return 0; } =20 -__init static void enable_instances(void) +struct boot_instance { + char *name; + char *flag_delim; + char *addr_delim; + char *backup; + char *events; +}; + +__init static void parse_boot_instance_info(struct boot_instance *boot_ins= tances) { - struct trace_array *tr; - bool memmap_area =3D false; - char *curr_str; + char *flag_delim; + char *addr_delim; + char *backup; + char *events; char *name; - char *str; char *tok; + char *str; + int idx =3D 0; + int i; =20 /* A tab is always appended */ boot_instance_info[boot_instance_index - 1] =3D '\0'; str =3D boot_instance_info; =20 - while ((curr_str =3D strsep(&str, "\t"))) { - phys_addr_t start =3D 0; - phys_addr_t size =3D 0; - unsigned long addr =3D 0; - bool traceprintk =3D false; - bool traceoff =3D false; - char *flag_delim; - char *addr_delim; - char *rname __free(kfree) =3D NULL; - char *backup; + while ((events =3D strsep(&str, "\t"))) { + if (!*events) + continue; =20 - tok =3D strsep(&curr_str, ","); + tok =3D strsep(&events, ","); =20 name =3D strsep(&tok, "=3D"); + if (!*name) { + pr_warn("Tracing: Empty boot instance name\n"); + continue; + } + backup =3D tok; =20 flag_delim =3D strchr(name, '^'); @@ -9649,15 +9660,62 @@ __init static void enable_instances(void) if (flag_delim) *flag_delim++ =3D '\0'; =20 - if (backup) { - if (backup_instance_area(backup, &addr, &size) < 0) + for (i =3D 0; i < idx; i++) { + /* Override by the last defined instance */ + if (!strcmp(name, boot_instances[i].name)) + break; + } + + boot_instances[i].name =3D name; + boot_instances[i].flag_delim =3D flag_delim; + boot_instances[i].addr_delim =3D addr_delim; + boot_instances[i].backup =3D backup; + boot_instances[i].events =3D events; + if (i =3D=3D idx) + idx++; + } + + nr_boot_instances =3D idx; +} + +__init static void enable_instances(void) +{ + struct boot_instance *boot_instances __free(kfree) =3D NULL; + struct boot_instance *bi; + struct trace_array *tr; + bool memmap_area =3D false; + char *tok; + + if (!boot_instance_index) + return; + + /* Parse the instance name to check the overlap */ + boot_instances =3D kmalloc_array(nr_boot_instances, + sizeof(*boot_instances), GFP_KERNEL); + if (!boot_instances) + return; + + parse_boot_instance_info(boot_instances); + + for (int i =3D 0; i < nr_boot_instances; i++) { + char *rname __free(kfree) =3D NULL; + phys_addr_t start =3D 0; + phys_addr_t size =3D 0; + unsigned long addr =3D 0; + bool traceprintk =3D false; + bool traceoff =3D false; + + bi =3D &boot_instances[i]; + + if (bi->backup) { + if (backup_instance_area(bi->backup, &addr, &size) < 0) continue; } =20 - if (flag_delim) { + if (bi->flag_delim) { char *flag; =20 - while ((flag =3D strsep(&flag_delim, "^"))) { + while ((flag =3D strsep(&bi->flag_delim, "^"))) { if (strcmp(flag, "traceoff") =3D=3D 0) { traceoff =3D true; } else if ((strcmp(flag, "printk") =3D=3D 0) || @@ -9666,35 +9724,35 @@ __init static void enable_instances(void) traceprintk =3D true; } else { pr_info("Tracing: Invalid instance flag '%s' for %s\n", - flag, name); + flag, bi->name); } } } =20 - tok =3D addr_delim; + tok =3D bi->addr_delim; if (tok && isdigit(*tok)) { start =3D memparse(tok, &tok); if (!start) { pr_warn("Tracing: Invalid boot instance address for %s\n", - name); + bi->name); continue; } if (*tok !=3D ':') { - pr_warn("Tracing: No size specified for instance %s\n", name); + pr_warn("Tracing: No size specified for instance %s\n", bi->name); continue; } tok++; size =3D memparse(tok, &tok); if (!size) { pr_warn("Tracing: Invalid boot instance size for %s\n", - name); + bi->name); continue; } memmap_area =3D true; } else if (tok) { if (!reserve_mem_find_by_name(tok, &start, &size)) { start =3D 0; - pr_warn("Failed to map boot instance %s to %s\n", name, tok); + pr_warn("Failed to map boot instance %s to %s\n", bi->name, tok); continue; } rname =3D kstrdup(tok, GFP_KERNEL); @@ -9717,19 +9775,19 @@ __init static void enable_instances(void) addr =3D (unsigned long)phys_to_virt(start); if (addr) { pr_info("Tracing: mapped boot instance %s at physical memory %pa of si= ze 0x%lx\n", - name, &start, (unsigned long)size); + bi->name, &start, (unsigned long)size); } else { - pr_warn("Tracing: Failed to map boot instance %s\n", name); + pr_warn("Tracing: Failed to map boot instance %s\n", bi->name); continue; } } else { /* Only non mapped buffers have snapshot buffers */ - do_allocate_snapshot(name); + do_allocate_snapshot(bi->name); } =20 - tr =3D trace_array_create_systems(name, NULL, addr, size); + tr =3D trace_array_create_systems(bi->name, NULL, addr, size); if (IS_ERR(tr)) { - pr_warn("Tracing: Failed to create instance buffer %s\n", curr_str); + pr_warn("Tracing: Failed to create instance buffer %s\n", bi->name); continue; } =20 @@ -9750,12 +9808,12 @@ __init static void enable_instances(void) /* * Backup buffers can be freed but need vfree(). */ - if (backup) { + if (bi->backup) { tr->flags |=3D TRACE_ARRAY_FL_VMALLOC | TRACE_ARRAY_FL_RDONLY; trace_array_start_autoremove(); } =20 - if (start || backup) { + if (start || bi->backup) { tr->flags |=3D TRACE_ARRAY_FL_BOOT | TRACE_ARRAY_FL_LAST_BOOT; tr->range_name =3D no_free_ptr(rname); } @@ -9764,11 +9822,12 @@ __init static void enable_instances(void) * Save the events to start and enabled them after all boot instances * have been created. */ - tr->boot_events =3D curr_str; + tr->boot_events =3D bi->events; } =20 /* Enable the events after all boot instances have been created */ list_for_each_entry(tr, &ftrace_trace_arrays, list) { + char *curr_str; =20 if (!tr->boot_events || !(*tr->boot_events)) { tr->boot_events =3D NULL; From nobody Thu Sep 24 21:46:49 2026 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 B75F3344DB7; Sat, 19 Sep 2026 14:38:41 +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=1789828723; cv=none; b=rYjc4OR/BlzC952CCuoShru45zk235ta3U1DVbZUavEGbVa83UuyRQReGUkFfcKf86lGYwTq4kzCwTFA4OKxRVte9cJfemRyHDmvGTvlH+E3SCQ8GyCocfX0Nw22W/gjrDBjR2QW02PDeCpt3xUJwErwmEjWeaZV1KIW6U2M2Dg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789828723; c=relaxed/simple; bh=0sTUHdTBguf9D+ODJqdIL5b6eL2ZzSqEDwWf8Ns29CY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=QzXqhW5tS2qTsJ8VDrHKgs8JuFc4zkALSiHXnjwPaO345rd6xCDGVl3+CouNtXoe1Zvojjg0rUSKQyq9GUDQB6Ts5HpkcszTUVGhtyspFNDemO+n4FPfwGk4b7ZaaVJgd4g0H71a9tffF48+0fkOFEUMD+gVxgUqV3G2q+QnjUk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=eiVEdh/z; 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="eiVEdh/z" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 327E11F000FF; Sat, 19 Sep 2026 14:38:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1789828721; bh=b7dMkdKwVdOPUbRXbSR/ZpGWRKdnGU2o7/s4+tCrt7E=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=eiVEdh/z+DIpaYbhrF/Sf96pO066THwbfh3exO7y6wf9CvboZaTpuRsJcQoAsZje+ EwscWzfRg0/jrBO4Ey2G8SmLpEaaFw/MKP04Tjeoo8M5+nSAZhYffTv5LgKktvr1WH wLQb0fF+BLMCQAcI+7b0b7gdk4QE6hxoLxcoZbEFrWuU5YFrphzrQBfm8nYfHUvumv eqXneLzfMmbOBz25nd/9Gt18iHULw7xN2tj3rHFK2gz6oInmBWfo4iasoQSbsmKNXx ukyjSuie9mpbrVxXHdquQ2h0ZiHisy4wtr1+gihTMidmTxGyNowjs9JYlZSfVaJBkU XMrp3lwlD/o9Q== From: "Masami Hiramatsu (Google)" To: Jonathan Corbet , Steven Rostedt , Masami Hiramatsu , Shuah Khan Cc: Shuah Khan , Mathieu Desnoyers , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH 2/2] selftests/ftrace: Add test case for overriding trace_instance parameter Date: Sat, 19 Sep 2026 23:38:36 +0900 Message-ID: <178982871684.289221.11881705926508561494.stgit@devnote2> X-Mailer: git-send-email 2.43.0 In-Reply-To: <178982869661.289221.2143320719673148321.stgit@devnote2> References: <178982869661.289221.2143320719673148321.stgit@devnote2> User-Agent: StGit/0.19 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 From: Masami Hiramatsu (Google) Add a boot-time tracing test case to verify that specifying multiple trace_instance=3D parameters with the same instance name overrides earlier definitions: - Verifies that a kernel command-line trace_instance=3D parameter overrides an earlier trace instance defined via bootconfig (kernel.trace_instance). - Verifies that a latter kernel command-line trace_instance=3D parameter overrides an earlier trace_instance=3D 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 Signed-off-by: Masami Hiramatsu (Google) --- .../cmdline-07-trace-instance-override.bconf | 2 + .../cmdline-07-trace-instance-override.cmdline | 1=20 .../tests/cmdline-07-trace-instance-override.sh | 56 ++++++++++++++++= ++++ 3 files changed, 59 insertions(+) create mode 100644 tools/testing/selftests/ftrace/boottime/bootconfigs/cmd= line-07-trace-instance-override.bconf create mode 100644 tools/testing/selftests/ftrace/boottime/cmdlines/cmdlin= e-07-trace-instance-override.cmdline create mode 100644 tools/testing/selftests/ftrace/boottime/tests/cmdline-0= 7-trace-instance-override.sh diff --git a/tools/testing/selftests/ftrace/boottime/bootconfigs/cmdline-07= -trace-instance-override.bconf b/tools/testing/selftests/ftrace/boottime/bo= otconfigs/cmdline-07-trace-instance-override.bconf new file mode 100644 index 000000000000..2551da39f722 --- /dev/null +++ b/tools/testing/selftests/ftrace/boottime/bootconfigs/cmdline-07-trace-= instance-override.bconf @@ -0,0 +1,2 @@ +kernel.trace_instance =3D "foo,sched:sched_switch" + diff --git a/tools/testing/selftests/ftrace/boottime/cmdlines/cmdline-07-tr= ace-instance-override.cmdline b/tools/testing/selftests/ftrace/boottime/cmd= lines/cmdline-07-trace-instance-override.cmdline new file mode 100644 index 000000000000..1953eb5d6f80 --- /dev/null +++ b/tools/testing/selftests/ftrace/boottime/cmdlines/cmdline-07-trace-ins= tance-override.cmdline @@ -0,0 +1 @@ +trace_instance=3Dfoo^traceoff,sched:sched_waking trace_instance=3Dbar,sche= d:sched_switch trace_instance=3Dbar^traceoff,irq:irq_handler_entry diff --git a/tools/testing/selftests/ftrace/boottime/tests/cmdline-07-trace= -instance-override.sh b/tools/testing/selftests/ftrace/boottime/tests/cmdli= ne-07-trace-instance-override.sh new file mode 100644 index 000000000000..a13b7deb6735 --- /dev/null +++ b/tools/testing/selftests/ftrace/boottime/tests/cmdline-07-trace-instan= ce-override.sh @@ -0,0 +1,56 @@ +#!/bin/sh +# SPDX-License-Identifier: GPL-2.0 +# Copyright (C) 2026, Google LLC. +# Check trace_instance=3D override with bootconfig and kernel command-line +TRACEDIR=3D"/sys/kernel/tracing" + +if [ -f /proc/bootconfig ] && grep -q "dump_bconf" /proc/cmdline 2>/dev/nu= ll; then + echo "=3D=3D=3D /proc/bootconfig =3D=3D=3D" + cat /proc/bootconfig + echo "=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D" +fi + +check_instance() { + instance=3D"$1" + old_event=3D"$2" + new_event=3D"$3" + + if [ ! -d "$TRACEDIR/instances/$instance" ]; then + echo "FAIL: trace instance $instance does not exist" + exit 1 + fi + + if [ -f "$TRACEDIR/instances/$instance/events/$old_event/enable" ]; then + ENABLE=3D$(cat "$TRACEDIR/instances/$instance/events/$old_event/enable") + if [ "$ENABLE" =3D "1" ]; then + echo "FAIL: overridden event $old_event is still enabled in instance $i= nstance" + exit 1 + fi + fi + + if [ ! -d "$TRACEDIR/instances/$instance/events/$new_event" ]; then + echo "FAIL: event $new_event does not exist in instance $instance" + exit 1 + fi + + ENABLE=3D$(cat "$TRACEDIR/instances/$instance/events/$new_event/enable") + if [ "$ENABLE" !=3D "1" ]; then + echo "FAIL: event $new_event is not enabled in instance $instance ($ENAB= LE)" + exit 1 + fi + + TRACING_ON=3D$(cat "$TRACEDIR/instances/$instance/tracing_on") + if [ "$TRACING_ON" !=3D "0" ]; then + echo "FAIL: tracing_on is not 0 in instance $instance ($TRACING_ON)" + exit 1 + fi +} + +# Test 1: bootconfig trace_instance overridden by cmdline parameter +check_instance "foo" "sched/sched_switch" "sched/sched_waking" + +# Test 2: cmdline trace_instance overridden by subsequent cmdline parameter +check_instance "bar" "sched/sched_switch" "irq/irq_handler_entry" + +echo "PASS: cmdline-07-trace-instance-override" +exit 0