From nobody Mon Feb 9 06:19:05 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 F263347AF60; Thu, 8 Jan 2026 14:23:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767882187; cv=none; b=UZ2x0uyucuMsmQ5SxCP2t7vU4TyHup6CzrPcAabONI1Ee+pPkeCp+/Vipc5KsQu9RcGCIuvv7SbymIKNw0BB+QYAoPk7Z6NMa0EWgWbMENxiv9bTwOZcN6dGikltsyl+v2WhwV9d0hfG8fxC2I2vh1yGlMBOdU3li/l9+iQYl30= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767882187; c=relaxed/simple; bh=JU48qQ4ep2c1+AkSPWBP9SDUbBp2sjZBhbd4s82yNaA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Bwbw/64uxUF5e2KQ52Jr3IAKh/IO3UTNLKMMP4tRtFnjGfJEe+0htl2KZdZy3GoRnxnfDw2ei7hL3OGCWXqVCAjrp620j/dBWbfN2tCoiy+TfFb59sI0aVjVUDpzveDExPndDrbd15Mx1wlrr+aTesibdKek7PS7YTJR/bDhFlY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=sjSs+rlc; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="sjSs+rlc" Received: by smtp.kernel.org (Postfix) with ESMTPSA id A6EC7C116C6; Thu, 8 Jan 2026 14:23:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1767882185; bh=JU48qQ4ep2c1+AkSPWBP9SDUbBp2sjZBhbd4s82yNaA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=sjSs+rlcguzE92nRwUcdKcPLyi4fgLocYz8FuFL/M3sxSNJmp1k6cq0ZFHpff7jKs 0V4CarGf4cFz/ngKCd1xhW5ddFiskFY9x+4e1oVKhreBWfOjIVES5GXiyz7Dch4hmc G8wi0TtAP5pOQ4q2DRpgnSwmHS1/kOea8Sq/mhFk2QgEZOKxfWaa/qYD+Co9y6DDFT oDf5eGpuzQr6bTAD3SA3Uw2hZGMx5fw9pudDbmCjPWoCMamwygKTWO/jjGQI0BjY49 JG6ehUPiz/wyW/jUGewzjyrs0R2D9nCHxKiXmHQ48VHR+jWSXZmpGB9vbTv9Yos3nI GHd1xyGVglZUA== From: "Masami Hiramatsu (Google)" To: Steven Rostedt Cc: Masami Hiramatsu , Mathieu Desnoyers , linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org Subject: [PATCH v2 1/2] tracing: Make the backup instance readonly Date: Thu, 8 Jan 2026 23:23:03 +0900 Message-ID: <176788218294.1398317.13118277798168167277.stgit@mhiramat.tok.corp.google.com> X-Mailer: git-send-email 2.52.0.351.gbe84eed79e-goog In-Reply-To: <176788217131.1398317.11144318616426272901.stgit@mhiramat.tok.corp.google.com> References: <176788217131.1398317.11144318616426272901.stgit@mhiramat.tok.corp.google.com> 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) Since there is no reason to reuse the backup instance, make it readonly. Note that only backup instances are readonly, because other trace instances will be empty unless it is writable. Only backup instances have copy entries from the original. With this change, most of the trace control files are removed from the backup instance, including eventfs enable/filter etc. # find /sys/kernel/tracing/instances/backup/events/ | wc -l 4093 # find /sys/kernel/tracing/instances/boot_map/events/ | wc -l 9573 Signed-off-by: Masami Hiramatsu (Google) --- Changes in v2: - Use readonly file_operations to prohibit writing instead of checking flags in write() callbacks. - Remove writable files from eventfs. --- kernel/trace/trace.c | 163 ++++++++++++++++++++++++++++++++-------= ---- kernel/trace/trace.h | 14 +++- kernel/trace/trace_boot.c | 5 + kernel/trace/trace_events.c | 75 ++++++++++++++------ 4 files changed, 192 insertions(+), 65 deletions(-) diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c index 38f7a7a55c23..1b87595413fe 100644 --- a/kernel/trace/trace.c +++ b/kernel/trace/trace.c @@ -4888,6 +4888,9 @@ static int tracing_open(struct inode *inode, struct f= ile *file) int cpu =3D tracing_get_cpu(inode); struct array_buffer *trace_buf =3D &tr->array_buffer; =20 + if (trace_array_is_readonly(tr)) + return -EPERM; + #ifdef CONFIG_TRACER_MAX_TRACE if (tr->current_trace->print_max) trace_buf =3D &tr->max_buffer; @@ -5055,6 +5058,15 @@ static const struct file_operations tracing_fops =3D= { .release =3D tracing_release, }; =20 +static const struct file_operations tracing_ro_fops =3D { + .open =3D tracing_open, + .read =3D seq_read, + .read_iter =3D seq_read_iter, + .splice_read =3D copy_splice_read, + .llseek =3D tracing_lseek, + .release =3D tracing_release, +}; + static const struct file_operations show_traces_fops =3D { .open =3D show_traces_open, .read =3D seq_read, @@ -5162,6 +5174,13 @@ static const struct file_operations tracing_cpumask_= fops =3D { .llseek =3D generic_file_llseek, }; =20 +static const struct file_operations tracing_cpumask_ro_fops =3D { + .open =3D tracing_open_generic_tr, + .read =3D tracing_cpumask_read, + .release =3D tracing_release_generic_tr, + .llseek =3D generic_file_llseek, +}; + static int tracing_trace_options_show(struct seq_file *m, void *v) { struct tracer_opt *trace_opts; @@ -8106,6 +8125,13 @@ static const struct file_operations set_tracer_fops = =3D { .release =3D tracing_release_generic_tr, }; =20 +static const struct file_operations set_tracer_ro_fops =3D { + .open =3D tracing_open_generic_tr, + .read =3D tracing_set_trace_read, + .llseek =3D generic_file_llseek, + .release =3D tracing_release_generic_tr, +}; + static const struct file_operations tracing_pipe_fops =3D { .open =3D tracing_open_pipe, .poll =3D tracing_poll_pipe, @@ -8122,6 +8148,13 @@ static const struct file_operations tracing_entries_= fops =3D { .release =3D tracing_release_generic_tr, }; =20 +static const struct file_operations tracing_entries_ro_fops =3D { + .open =3D tracing_open_generic_tr, + .read =3D tracing_entries_read, + .llseek =3D generic_file_llseek, + .release =3D tracing_release_generic_tr, +}; + static const struct file_operations tracing_syscall_buf_fops =3D { .open =3D tracing_open_generic_tr, .read =3D tracing_syscall_buf_read, @@ -8170,6 +8203,13 @@ static const struct file_operations trace_clock_fops= =3D { .write =3D tracing_clock_write, }; =20 +static const struct file_operations trace_clock_ro_fops =3D { + .open =3D tracing_clock_open, + .read =3D seq_read, + .llseek =3D seq_lseek, + .release =3D tracing_single_release_tr, +}; + static const struct file_operations trace_time_stamp_mode_fops =3D { .open =3D tracing_time_stamp_mode_open, .read =3D seq_read, @@ -9353,12 +9393,16 @@ static void tracing_init_tracefs_percpu(struct trace_array *tr, long cpu) { struct dentry *d_percpu =3D tracing_dentry_percpu(tr, cpu); + umode_t writable_mode =3D TRACE_MODE_WRITE; struct dentry *d_cpu; char cpu_dir[30]; /* 30 characters should be more than enough */ =20 if (!d_percpu) return; =20 + if (trace_array_is_readonly(tr)) + writable_mode =3D TRACE_MODE_READ; + snprintf(cpu_dir, 30, "cpu%ld", cpu); d_cpu =3D tracefs_create_dir(cpu_dir, d_percpu); if (!d_cpu) { @@ -9371,7 +9415,7 @@ tracing_init_tracefs_percpu(struct trace_array *tr, l= ong cpu) tr, cpu, &tracing_pipe_fops); =20 /* per cpu trace */ - trace_create_cpu_file("trace", TRACE_MODE_WRITE, d_cpu, + trace_create_cpu_file("trace", writable_mode, d_cpu, tr, cpu, &tracing_fops); =20 trace_create_cpu_file("trace_pipe_raw", TRACE_MODE_READ, d_cpu, @@ -9566,21 +9610,31 @@ static const struct file_operations trace_options_c= ore_fops =3D { .llseek =3D generic_file_llseek, }; =20 -struct dentry *trace_create_file(const char *name, - umode_t mode, - struct dentry *parent, - void *data, - const struct file_operations *fops) +struct dentry *__trace_create_file(const char *name, + umode_t mode, + struct dentry *parent, + void *data, + const struct file_operations *fops, + const struct file_operations *ro_fops) { + bool readonly =3D !!(mode & TRACE_MODE_WRITE_MASK); struct dentry *ret; =20 - ret =3D tracefs_create_file(name, mode, parent, data, fops); + ret =3D tracefs_create_file(name, mode, parent, data, readonly ? ro_fops = : fops); if (!ret) pr_warn("Could not create tracefs '%s' entry\n", name); =20 return ret; } =20 +struct dentry *trace_create_file(const char *name, + umode_t mode, + struct dentry *parent, + void *data, + const struct file_operations *fops) +{ + return __trace_create_file(name, mode, parent, data, fops, fops); +} =20 static struct dentry *trace_options_init_dentry(struct trace_array *tr) { @@ -9811,6 +9865,9 @@ rb_simple_write(struct file *filp, const char __user = *ubuf, unsigned long val; int ret; =20 + if (trace_array_is_readonly(tr)) + return -EPERM; + ret =3D kstrtoul_from_user(ubuf, cnt, 10, &val); if (ret) return ret; @@ -9845,6 +9902,13 @@ static const struct file_operations rb_simple_fops = =3D { .llseek =3D default_llseek, }; =20 +static const struct file_operations rb_simple_ro_fops =3D { + .open =3D tracing_open_generic_tr, + .read =3D rb_simple_read, + .release =3D tracing_release_generic_tr, + .llseek =3D default_llseek, +}; + static ssize_t buffer_percent_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos) @@ -9986,6 +10050,13 @@ static const struct file_operations buffer_subbuf_s= ize_fops =3D { .llseek =3D default_llseek, }; =20 +static const struct file_operations buffer_subbuf_size_ro_fops =3D { + .open =3D tracing_open_generic_tr, + .read =3D buffer_subbuf_size_read, + .release =3D tracing_release_generic_tr, + .llseek =3D default_llseek, +}; + static struct dentry *trace_instance_dir; =20 static void @@ -10597,89 +10668,101 @@ static __init void create_trace_instances(struct= dentry *d_tracer) static void init_tracer_tracefs(struct trace_array *tr, struct dentry *d_tracer) { + umode_t writable_mode =3D TRACE_MODE_WRITE; + bool readonly =3D trace_array_is_readonly(tr); int cpu; =20 + if (readonly) + writable_mode =3D TRACE_MODE_READ; + trace_create_file("available_tracers", TRACE_MODE_READ, d_tracer, tr, &show_traces_fops); =20 - trace_create_file("current_tracer", TRACE_MODE_WRITE, d_tracer, - tr, &set_tracer_fops); + __trace_create_file("current_tracer", writable_mode, d_tracer, + tr, &set_tracer_fops, &set_tracer_ro_fops); =20 - trace_create_file("tracing_cpumask", TRACE_MODE_WRITE, d_tracer, - tr, &tracing_cpumask_fops); + __trace_create_file("tracing_cpumask", writable_mode, d_tracer, + tr, &tracing_cpumask_fops, &tracing_cpumask_ro_fops); =20 + /* Options are used for changing print-format even for readonly instance.= */ trace_create_file("trace_options", TRACE_MODE_WRITE, d_tracer, tr, &tracing_iter_fops); =20 - trace_create_file("trace", TRACE_MODE_WRITE, d_tracer, - tr, &tracing_fops); + __trace_create_file("trace", writable_mode, d_tracer, + tr, &tracing_fops, &tracing_ro_fops); =20 trace_create_file("trace_pipe", TRACE_MODE_READ, d_tracer, tr, &tracing_pipe_fops); =20 - trace_create_file("buffer_size_kb", TRACE_MODE_WRITE, d_tracer, - tr, &tracing_entries_fops); + __trace_create_file("buffer_size_kb", writable_mode, d_tracer, + tr, &tracing_entries_fops, &tracing_entries_ro_fops); =20 trace_create_file("buffer_total_size_kb", TRACE_MODE_READ, d_tracer, tr, &tracing_total_entries_fops); =20 - trace_create_file("free_buffer", 0200, d_tracer, - tr, &tracing_free_buffer_fops); + if (!readonly) { + trace_create_file("free_buffer", 0200, d_tracer, + tr, &tracing_free_buffer_fops); + + trace_create_file("trace_marker", 0220, d_tracer, + tr, &tracing_mark_fops); + + tr->trace_marker_file =3D __find_event_file(tr, "ftrace", "print"); =20 - trace_create_file("trace_marker", 0220, d_tracer, - tr, &tracing_mark_fops); + trace_create_file("trace_marker_raw", 0220, d_tracer, + tr, &tracing_mark_raw_fops); =20 - tr->trace_marker_file =3D __find_event_file(tr, "ftrace", "print"); + trace_create_file("buffer_percent", TRACE_MODE_WRITE, d_tracer, + tr, &buffer_percent_fops); =20 - trace_create_file("trace_marker_raw", 0220, d_tracer, - tr, &tracing_mark_raw_fops); + trace_create_file("syscall_user_buf_size", TRACE_MODE_WRITE, d_tracer, + tr, &tracing_syscall_buf_fops); + } =20 - trace_create_file("trace_clock", TRACE_MODE_WRITE, d_tracer, tr, - &trace_clock_fops); + __trace_create_file("trace_clock", writable_mode, d_tracer, tr, + &trace_clock_fops, &trace_clock_ro_fops); =20 - trace_create_file("tracing_on", TRACE_MODE_WRITE, d_tracer, - tr, &rb_simple_fops); + __trace_create_file("tracing_on", writable_mode, d_tracer, + tr, &rb_simple_fops, &rb_simple_ro_fops); =20 trace_create_file("timestamp_mode", TRACE_MODE_READ, d_tracer, tr, &trace_time_stamp_mode_fops); =20 tr->buffer_percent =3D 50; =20 - trace_create_file("buffer_percent", TRACE_MODE_WRITE, d_tracer, - tr, &buffer_percent_fops); - - trace_create_file("buffer_subbuf_size_kb", TRACE_MODE_WRITE, d_tracer, - tr, &buffer_subbuf_size_fops); - - trace_create_file("syscall_user_buf_size", TRACE_MODE_WRITE, d_tracer, - tr, &tracing_syscall_buf_fops); + __trace_create_file("buffer_subbuf_size_kb", writable_mode, d_tracer, + tr, &buffer_subbuf_size_fops, + &buffer_subbuf_size_ro_fops); =20 create_trace_options_dir(tr); =20 #ifdef CONFIG_TRACER_MAX_TRACE - trace_create_maxlat_file(tr, d_tracer); + if (!readonly) + trace_create_maxlat_file(tr, d_tracer); #endif =20 - if (ftrace_create_function_files(tr, d_tracer)) + if (!readonly && ftrace_create_function_files(tr, d_tracer)) MEM_FAIL(1, "Could not allocate function filter files"); =20 if (tr->range_addr_start) { trace_create_file("last_boot_info", TRACE_MODE_READ, d_tracer, tr, &last_boot_fops); #ifdef CONFIG_TRACER_SNAPSHOT - } else { + } else if (!readonly) { trace_create_file("snapshot", TRACE_MODE_WRITE, d_tracer, tr, &snapshot_fops); #endif } =20 - trace_create_file("error_log", TRACE_MODE_WRITE, d_tracer, - tr, &tracing_err_log_fops); + if (!readonly) + trace_create_file("error_log", TRACE_MODE_WRITE, d_tracer, + tr, &tracing_err_log_fops); =20 for_each_tracing_cpu(cpu) tracing_init_tracefs_percpu(tr, cpu); =20 - ftrace_init_tracefs(tr, d_tracer); + if (!readonly) + ftrace_init_tracefs(tr, d_tracer); } =20 #ifdef CONFIG_TRACEFS_AUTOMOUNT_DEPRECATED diff --git a/kernel/trace/trace.h b/kernel/trace/trace.h index b6d42fe06115..4fae5cf1182c 100644 --- a/kernel/trace/trace.h +++ b/kernel/trace/trace.h @@ -33,6 +33,7 @@ =20 #define TRACE_MODE_WRITE 0640 #define TRACE_MODE_READ 0440 +#define TRACE_MODE_WRITE_MASK (TRACE_MODE_WRITE & ~TRACE_MODE_READ) =20 enum trace_type { __TRACE_FIRST_TYPE =3D 0, @@ -483,6 +484,12 @@ extern bool trace_clock_in_ns(struct trace_array *tr); =20 extern unsigned long trace_adjust_address(struct trace_array *tr, unsigned= long addr); =20 +static inline bool trace_array_is_readonly(struct trace_array *tr) +{ + /* backup instance is read only. */ + return tr->flags & TRACE_ARRAY_FL_VMALLOC; +} + /* * The global tracer (top) should be the first trace array added, * but we check the flag anyway. @@ -680,7 +687,12 @@ struct dentry *trace_create_file(const char *name, struct dentry *parent, void *data, const struct file_operations *fops); - +struct dentry *__trace_create_file(const char *name, + umode_t mode, + struct dentry *parent, + void *data, + const struct file_operations *fops, + const struct file_operations *ro_fops); =20 /** * tracer_tracing_is_on_cpu - show real state of ring buffer enabled on fo= r a cpu diff --git a/kernel/trace/trace_boot.c b/kernel/trace/trace_boot.c index dbe29b4c6a7a..2ca2541c8a58 100644 --- a/kernel/trace/trace_boot.c +++ b/kernel/trace/trace_boot.c @@ -61,7 +61,8 @@ trace_boot_set_instance_options(struct trace_array *tr, s= truct xbc_node *node) v =3D memparse(p, NULL); if (v < PAGE_SIZE) pr_err("Buffer size is too small: %s\n", p); - if (tracing_resize_ring_buffer(tr, v, RING_BUFFER_ALL_CPUS) < 0) + if (trace_array_is_readonly(tr) || + tracing_resize_ring_buffer(tr, v, RING_BUFFER_ALL_CPUS) < 0) pr_err("Failed to resize trace buffer to %s\n", p); } =20 @@ -597,7 +598,7 @@ trace_boot_enable_tracer(struct trace_array *tr, struct= xbc_node *node) =20 p =3D xbc_node_find_value(node, "tracer", NULL); if (p && *p !=3D '\0') { - if (tracing_set_tracer(tr, p) < 0) + if (trace_array_is_readonly(tr) || tracing_set_tracer(tr, p) < 0) pr_err("Failed to set given tracer: %s\n", p); } =20 diff --git a/kernel/trace/trace_events.c b/kernel/trace/trace_events.c index 9b07ad9eb284..741b16b54d90 100644 --- a/kernel/trace/trace_events.c +++ b/kernel/trace/trace_events.c @@ -1379,6 +1379,9 @@ static int __ftrace_set_clr_event(struct trace_array = *tr, const char *match, { int ret; =20 + if (trace_array_is_readonly(tr)) + return -EPERM; + mutex_lock(&event_mutex); ret =3D __ftrace_set_clr_event_nolock(tr, match, sub, event, set, mod); mutex_unlock(&event_mutex); @@ -2817,8 +2820,8 @@ event_subsystem_dir(struct trace_array *tr, const cha= r *name, } else __get_system(system); =20 - /* ftrace only has directories no files */ - if (strcmp(name, "ftrace") =3D=3D 0) + /* ftrace only has directories no files, readonly instance too. */ + if (strcmp(name, "ftrace") =3D=3D 0 || trace_array_is_readonly(tr)) nr_entries =3D 0; else nr_entries =3D ARRAY_SIZE(system_entries); @@ -2979,7 +2982,6 @@ event_create_dir(struct eventfs_inode *parent, struct= trace_event_file *file) struct eventfs_inode *e_events; struct eventfs_inode *ei; const char *name; - int nr_entries; int ret; static struct eventfs_entry event_entries[] =3D { { @@ -3024,6 +3026,18 @@ event_create_dir(struct eventfs_inode *parent, struc= t trace_event_file *file) }, #endif }; + static struct eventfs_entry event_ro_entries[] =3D { + { + .name =3D "format", + .callback =3D event_callback, + }, +#ifdef CONFIG_PERF_EVENTS + { + .name =3D "id", + .callback =3D event_callback, + }, +#endif + }; =20 /* * If the trace point header did not define TRACE_SYSTEM @@ -3037,10 +3051,14 @@ event_create_dir(struct eventfs_inode *parent, stru= ct trace_event_file *file) if (!e_events) return -ENOMEM; =20 - nr_entries =3D ARRAY_SIZE(event_entries); - name =3D trace_event_name(call); - ei =3D eventfs_create_dir(name, e_events, event_entries, nr_entries, file= ); + + if (trace_array_is_readonly(tr)) + ei =3D eventfs_create_dir(name, e_events, event_ro_entries, + ARRAY_SIZE(event_ro_entries), file); + else + ei =3D eventfs_create_dir(name, e_events, event_entries, + ARRAY_SIZE(event_entries), file); if (IS_ERR(ei)) { pr_warn("Could not create tracefs '%s' directory\n", name); return -1; @@ -4378,7 +4396,6 @@ create_event_toplevel_files(struct dentry *parent, st= ruct trace_array *tr) { struct eventfs_inode *e_events; struct dentry *entry; - int nr_entries; static struct eventfs_entry events_entries[] =3D { { .name =3D "enable", @@ -4393,30 +4410,44 @@ create_event_toplevel_files(struct dentry *parent, = struct trace_array *tr) .callback =3D events_callback, }, }; + static struct eventfs_entry events_ro_entries[] =3D { + { + .name =3D "header_page", + .callback =3D events_callback, + }, + { + .name =3D "header_event", + .callback =3D events_callback, + }, + }; =20 - entry =3D trace_create_file("set_event", TRACE_MODE_WRITE, parent, - tr, &ftrace_set_event_fops); - if (!entry) - return -ENOMEM; - - nr_entries =3D ARRAY_SIZE(events_entries); - - e_events =3D eventfs_create_events_dir("events", parent, events_entries, - nr_entries, tr); + if (trace_array_is_readonly(tr)) + e_events =3D eventfs_create_events_dir("events", parent, events_ro_entri= es, + ARRAY_SIZE(events_ro_entries), tr); + else + e_events =3D eventfs_create_events_dir("events", parent, events_entries, + ARRAY_SIZE(events_entries), tr); if (IS_ERR(e_events)) { pr_warn("Could not create tracefs 'events' directory\n"); return -ENOMEM; } =20 - /* There are not as crucial, just warn if they are not created */ + if (!trace_array_is_readonly(tr)) { + + entry =3D trace_create_file("set_event", TRACE_MODE_WRITE, parent, + tr, &ftrace_set_event_fops); + if (!entry) + return -ENOMEM; =20 - trace_create_file("set_event_pid", TRACE_MODE_WRITE, parent, - tr, &ftrace_set_event_pid_fops); + /* There are not as crucial, just warn if they are not created */ =20 - trace_create_file("set_event_notrace_pid", - TRACE_MODE_WRITE, parent, tr, - &ftrace_set_event_notrace_pid_fops); + trace_create_file("set_event_pid", TRACE_MODE_WRITE, parent, + tr, &ftrace_set_event_pid_fops); =20 + trace_create_file("set_event_notrace_pid", + TRACE_MODE_WRITE, parent, tr, + &ftrace_set_event_notrace_pid_fops); + } tr->event_dir =3D e_events; =20 return 0; From nobody Mon Feb 9 06:19:05 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 9716047AF60; Thu, 8 Jan 2026 14:23:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767882193; cv=none; b=DgMjvFtak066jXtfCRH8n1Hzil5bANXUkdVZzbCeO680j9v6LavgA469nhAzsUPb/NSSRSgfzUG0NHQCm6f4PJn4joEAmQHL9MxPi5R9S9hLQ8TPohELRU/JH9ZB1wgZ1b2gzxXnc656d/dyeINuECNBEzflpX8gRlL2HcC+ocg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767882193; c=relaxed/simple; bh=J97sQnaVFXB8iaJTRSYhgCw2Ny3W6xvVeT9aZMdEBtw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=D32pOK4sFM4rWkecYC5P9kKwDVdCo+/oONiBX1/jxNvoDrxjx01g7YqTGwVGfwbDJIRUc3QXOE3Z46KRqMgeL+a4M1qzmAddRcAQDPT7kV8kpKcK0lylL0obYipsGx+Hp4icJxEV2FwS39GenVd2ZC/j4UGwR+RlnhaTtJr+Prw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=jkfguMAT; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="jkfguMAT" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6CE45C116C6; Thu, 8 Jan 2026 14:23:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1767882193; bh=J97sQnaVFXB8iaJTRSYhgCw2Ny3W6xvVeT9aZMdEBtw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=jkfguMATACMHmD69NVs0+tRL9yRT1Iq3Plmj3O22LGGwfSy+eLschTM1FNB9x1KHh wR8f2xUtsj+Ea41sofwpMotjXNrJNAvvJZkCpHiQOPCGDR3amUVpm+Ek0a7/0nPEgB C/Zeb4rPpsltQtDmVTux+b3eLjnkJ6EVFednZH9xq2ayrLP4qCsgGeVls/+JK74rLU HciimZBpmbd1aOsteVmNe5RVFGFZM0ZZ31nya24WVWiiTQpQNOaVJOpTkLC+X5cAx+ o0Tx7nWIaqIhyfmgrU5Fr9794n64ev8+kvDIbR8BWNJW4oSjWyG9lWaqIihD6WCMEL 55fOqO2U87QJw== From: "Masami Hiramatsu (Google)" To: Steven Rostedt Cc: Masami Hiramatsu , Mathieu Desnoyers , linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org Subject: [PATCH v2 2/2] tracing: Add autoremove feature to the backup instance Date: Thu, 8 Jan 2026 23:23:10 +0900 Message-ID: <176788219069.1398317.13803610975411308454.stgit@mhiramat.tok.corp.google.com> X-Mailer: git-send-email 2.52.0.351.gbe84eed79e-goog In-Reply-To: <176788217131.1398317.11144318616426272901.stgit@mhiramat.tok.corp.google.com> References: <176788217131.1398317.11144318616426272901.stgit@mhiramat.tok.corp.google.com> 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) Since the backup instance is readonly, after reading all data via pipe, no data is left on the instance. Thus it can be removed safely after closing all files. Signed-off-by: Masami Hiramatsu (Google) --- kernel/trace/trace.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++= +++- kernel/trace/trace.h | 6 +++++ 2 files changed, 69 insertions(+), 1 deletion(-) diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c index 1b87595413fe..c0b05a560203 100644 --- a/kernel/trace/trace.c +++ b/kernel/trace/trace.c @@ -590,6 +590,55 @@ void trace_set_ring_buffer_expanded(struct trace_array= *tr) tr->ring_buffer_expanded =3D true; } =20 +static int __remove_instance(struct trace_array *tr); + +static void trace_array_autoremove(struct work_struct *work) +{ + struct trace_array *tr =3D container_of(work, struct trace_array, autorem= ove_work); + + guard(mutex)(&event_mutex); + guard(mutex)(&trace_types_lock); + + /* + * This can be fail if someone gets @tr before starting this + * function, but in that case, this will be kicked again when + * putting it. So we don't care the result. + */ + __remove_instance(tr); +} + +static struct workqueue_struct *autoremove_wq; + +static void trace_array_init_autoremove(struct trace_array *tr) +{ + INIT_WORK(&tr->autoremove_work, trace_array_autoremove); +} + +static void trace_array_kick_autoremove(struct trace_array *tr) +{ + if (!work_pending(&tr->autoremove_work) && autoremove_wq) + queue_work(autoremove_wq, &tr->autoremove_work); +} + +static void trace_array_cancel_autoremove(struct trace_array *tr) +{ + if (work_pending(&tr->autoremove_work)) + cancel_work(&tr->autoremove_work); +} + +__init static int trace_array_init_autoremove_wq(void) +{ + autoremove_wq =3D alloc_workqueue("tr_autoremove_wq", + WQ_UNBOUND | WQ_HIGHPRI, 0); + if (!autoremove_wq) { + pr_err("Unable to allocate tr_autoremove_wq\n"); + return -ENOMEM; + } + return 0; +} + +late_initcall_sync(trace_array_init_autoremove_wq); + LIST_HEAD(ftrace_trace_arrays); =20 int trace_array_get(struct trace_array *this_tr) @@ -598,7 +647,7 @@ int trace_array_get(struct trace_array *this_tr) =20 guard(mutex)(&trace_types_lock); list_for_each_entry(tr, &ftrace_trace_arrays, list) { - if (tr =3D=3D this_tr) { + if (tr =3D=3D this_tr && !tr->free_on_close) { tr->ref++; return 0; } @@ -611,6 +660,12 @@ static void __trace_array_put(struct trace_array *this= _tr) { WARN_ON(!this_tr->ref); this_tr->ref--; + /* + * When free_on_close is set, prepare removing the array + * when the last reference is released. + */ + if (this_tr->ref =3D=3D 1 && this_tr->free_on_close) + trace_array_kick_autoremove(this_tr); } =20 /** @@ -6225,6 +6280,10 @@ static void update_last_data(struct trace_array *tr) /* Only if the buffer has previous boot data clear and update it. */ tr->flags &=3D ~TRACE_ARRAY_FL_LAST_BOOT; =20 + /* If this is a backup instance, mark it for autoremove. */ + if (tr->flags & TRACE_ARRAY_FL_VMALLOC) + tr->free_on_close =3D true; + /* Reset the module list and reload them */ if (tr->scratch) { struct trace_scratch *tscratch =3D tr->scratch; @@ -10428,6 +10487,8 @@ trace_array_create_systems(const char *name, const = char *systems, if (ftrace_allocate_ftrace_ops(tr) < 0) goto out_free_tr; =20 + trace_array_init_autoremove(tr); + ftrace_init_trace_array(tr); =20 init_trace_flags_index(tr); @@ -10576,6 +10637,7 @@ static int __remove_instance(struct trace_array *tr) if (update_marker_trace(tr, 0)) synchronize_rcu(); =20 + trace_array_cancel_autoremove(tr); tracing_set_nop(tr); clear_ftrace_function_probes(tr); event_trace_del_tracer(tr); diff --git a/kernel/trace/trace.h b/kernel/trace/trace.h index 4fae5cf1182c..b6b56e790b13 100644 --- a/kernel/trace/trace.h +++ b/kernel/trace/trace.h @@ -447,6 +447,12 @@ struct trace_array { * we do not waste memory on systems that are not using tracing. */ bool ring_buffer_expanded; + /* + * If the ring buffer is a read only backup instance, it will be + * removed after dumping all data via pipe, because no readable data. + */ + bool free_on_close; + struct work_struct autoremove_work; }; =20 enum {