tracing/remotes: Add printk, dump_on_panic and boot parameters
Subsystems (4)
Vincent Donnefort
maintainerv5 cover tracing/remotes: Add printk, dump_on_panic and boot parameters
This series extends the recently introduced trace remotes infrastructure, bringing useful features for developers:
* dump_on_panic: Dump the trace remote buffer on system panic.
* dmesg: Redirect remote events to dmesg.
* trace_remote=: Configure a trace_remote from the commandline.
* poll_ms: Modify the polling period.
It also brings a couple of optimisations:
* In-header compressed length support for small events.
* Single work thread for remote polling.
And some misc improvements:
* Use kstrtobool where possible
* Free resources on remote registration failure
Changelog:
v5:
* Use seq_buf_str() for safe, properly terminated trace_seq extraction. (Steven)
* Remove destructive dmesg -c from selftests and rely on timestamp matching instead. (Steven)
v4 (https://lore.kernel.org/all/[email protected]/):
* Rebase on 7.2-rc6
* Use trace_append_boot_param() to parse cmdline options.
* Fix trace_remote_iterator cleanup order for non-consuming iterators
(Sashiko).
v3 (https://lore.kernel.org/all/[email protected]/):
* Make sure dmesg and panic handlers are using null-terminated trace_seq.
* Make struct trace_remote more compact
* Fix dump_on_panic test unloaded case (Sashiko)
* Rebase on 7.2-rc4
v2 (https://lore.kernel.org/all/[email protected]/):
Sashiko had a few spot-on comments on the v1 which forced me to heavily re-arrange the series, so I thought it'd be alright to send a v2 already including those changes.
* dump_on_oops -> dump_on_panic
* printk -> dmesg
* Allow to configure poll_ms
* Make the ring_buffer_iter functions panic-friendly
* Gate tracefs .open on remote registration (Sashiko)
* Use irqsave for reader lock (Sashiko)
* Fix iter locking for TRI_DMESG (Sashiko)
* Yield in the dmesg work (Sashiko)
v1 (https://lore.kernel.org/all/[email protected]/)
Vincent Donnefort (18):
tracing/remotes: Gate tracefs files opening on trace remote
registration
tracing/remotes: Release tracefs,eventfs on registration failure tracing/remotes: Use kstrtobool for boolean tracefs files tracing/remotes: Use a single per-remote polling work tracing/simple_ring_buffer: Add support for compressed length tracing/remotes: Add dmesg tracefs file
tracing/remotes: selftests: Add a test for the dmesg tracefs file tracing/remotes: selftests: Prefix hypervisor folder
ring-buffer: Use irqsave for the reader lock in
ring_buffer_poll_remote
ring-buffer: Use panic-friendly locking in ring_buffer_iter interface
ring-buffer: Add ring_buffer_read_remote_meta_page()
ring-buffer: Add kerneldoc for ring_buffer_poll_remote tracing/remotes: Add dump_on_panic tracefs file tracing/remotes: selftests: Add a test for the dump_on_panic tracefs
file
tracing/remotes: Add poll_ms tracefs file
tracing/remotes: Add trace_remote cmdline options
Documentation: tracing/remotes: Add detailed tracefs layout Documentation/kernel-parameters: Add trace_remote
.../admin-guide/kernel-parameters.txt | 19 +
Documentation/trace/remotes.rst | 66 +-
include/linux/ring_buffer.h | 1 +
kernel/trace/ring_buffer.c | 88 +-
kernel/trace/simple_ring_buffer.c | 25 +-
kernel/trace/trace_remote.c | 817 +++++++++++++++---
.../buffer_size.tc | 0
.../test.d/remotes/00hypervisor/dmesg.tc | 11 +
.../remotes/00hypervisor/dump_on_panic.tc | 11 +
.../{hypervisor => 00hypervisor}/hotplug.tc | 0
.../{hypervisor => 00hypervisor}/reset.tc | 0
.../{hypervisor => 00hypervisor}/trace.tc | 0
.../trace_pipe.tc | 0
.../{hypervisor => 00hypervisor}/unloading.tc | 0
.../selftests/ftrace/test.d/remotes/dmesg.tc | 81 ++
.../ftrace/test.d/remotes/dump_on_panic.tc | 51 ++
.../selftests/ftrace/test.d/remotes/functions | 2 +
17 files changed, 1032 insertions(+), 140 deletions(-)
rename tools/testing/selftests/ftrace/test.d/remotes/{hypervisor => 00hypervisor}/buffer_size.tc (100%)
create mode 100644 tools/testing/selftests/ftrace/test.d/remotes/00hypervisor/dmesg.tc
create mode 100644 tools/testing/selftests/ftrace/test.d/remotes/00hypervisor/dump_on_panic.tc
rename tools/testing/selftests/ftrace/test.d/remotes/{hypervisor => 00hypervisor}/hotplug.tc (100%)
rename tools/testing/selftests/ftrace/test.d/remotes/{hypervisor => 00hypervisor}/reset.tc (100%)
rename tools/testing/selftests/ftrace/test.d/remotes/{hypervisor => 00hypervisor}/trace.tc (100%)
rename tools/testing/selftests/ftrace/test.d/remotes/{hypervisor => 00hypervisor}/trace_pipe.tc (100%)
rename tools/testing/selftests/ftrace/test.d/remotes/{hypervisor => 00hypervisor}/unloading.tc (100%)
create mode 100644 tools/testing/selftests/ftrace/test.d/remotes/dmesg.tc
create mode 100644 tools/testing/selftests/ftrace/test.d/remotes/dump_on_panic.tcbase-commit: db2ddb87143519e20a95aa36c60b36107b736a58
Signature · 1 line
2.55.0.691.gc56d675ccc-goog
Vincent Donnefort
maintainerPATCH v5 1/18 tracing/remotes: Gate tracefs files opening on trace remote registration
Currently, if a remote fails to register, its resources will leak and will not be properly torn down.
To prevent a user accessing a remote tracefs that is about to be destroyed, keep track of the registered remotes in a global list, similarly to trace instances. Gate the tracefs open function based on the presence of the remote in that list.
kernel/trace/trace_remote.c +87 -13
@@ -39,6 +39,7 @@ struct trace_remote_iterator { };struct trace_remote { +struct list_head node;struct trace_remote_callbacks *cbs;void *priv;struct trace_buffer *trace_buffer;
@@ -57,6 +58,9 @@ struct trace_remote {bool tracing_on; }; +static DEFINE_MUTEX (trace_remotes_lock); +static LIST_HEAD (trace_remotes); +static bool trace_remote_loaded (struct trace_remote *remote) {return !!remote->trace_buffer;
@@ -170,6 +174,60 @@ static void trace_remote_reset(struct trace_remote *remote, int cpu)trace_remote_try_unload (remote); } +static int trace_remote_tracefs_open (struct inode *inode,struct file *filp) +{ +void *i_private = inode->i_private; +struct trace_remote *r; + +if (!i_private) +return -ENODEV ; + +guard (mutex)(&trace_remotes_lock); + +/* i_private is either a struct trace_remote or a struct remote_event */ +list_for_each_entry (r, &trace_remotes, node) { +if (r == i_private) +return 0 ; +if (!r->events) +continue ; +if (i_private >= (void *)r->events && + i_private < (void *)(r->events + r->nr_events)) +return 0 ; + } + +return -ENODEV ; +} + +#define DEFINE_TRACE_REMOTE_ATTRIBUTE_FUNCS (__name) \ +static int __name ## _open(struct inode *inode, struct file *file) \ +{ \ + int ret = trace_remote_tracefs_open(inode, file); \ + \ + if (ret) \ + return ret; \ + \ + return single_open(file, __name ## _show, inode->i_private); \ +} + +#define DEFINE_TRACE_REMOTE_ATTRIBUTE (__name) \ +DEFINE_TRACE_REMOTE_ATTRIBUTE_FUNCS(__name) \ +static const struct file_operations __name ## _fops = { \ + .open = __name ## _open, \ + .read = seq_read, \ + .write = __name ## _write, \ + .llseek = seq_lseek, \ + .release = single_release, \ +} + +#define DEFINE_TRACE_REMOTE_SHOW_ATTRIBUTE (__name) \ +DEFINE_TRACE_REMOTE_ATTRIBUTE_FUNCS(__name) \ +static const struct file_operations __name ## _fops = { \ + .open = __name ## _open, \ + .read = seq_read, \ + .llseek = seq_lseek, \ + .release = single_release, \ +} +static ssize_t tracing_on_write (struct file *filp,const char __user *ubuf,size_t cnt,loff_t *ppos) {
@@ -198,7 +256,7 @@ static int tracing_on_show(struct seq_file *s, void *unused)return 0 ; } -DEFINE_SHOW_STORE_ATTRIBUTE (tracing_on); +DEFINE_TRACE_REMOTE_ATTRIBUTE (tracing_on);static ssize_t buffer_size_kb_write (struct file *filp,const char __user *ubuf,size_t cnt,loff_t *ppos)
@@ -235,7 +293,7 @@ static int buffer_size_kb_show(struct seq_file *s, void *unused)return 0 ; } -DEFINE_SHOW_STORE_ATTRIBUTE (buffer_size_kb); +DEFINE_TRACE_REMOTE_ATTRIBUTE (buffer_size_kb);static int trace_remote_get (struct trace_remote *remote,int cpu) {
@@ -593,6 +651,11 @@ static int trace_pipe_open(struct inode *inode, struct file *filp)struct trace_remote *remote = inode->i_private;struct trace_remote_iterator *iter;int cpu =tracing_get_cpu (inode); +int ret; + + ret =trace_remote_tracefs_open (inode, filp); +if (ret) +return ret;guard (mutex)(&remote->lock);
@@ -734,22 +797,26 @@ static int trace_open(struct inode *inode, struct file *filp)int cpu =tracing_get_cpu (inode);int ret; + ret =trace_remote_tracefs_open (inode, filp); +if (ret) +return ret; +if (!(filp->f_mode &FMODE_READ ))return 0 ; + ret =seq_open (filp, &trace_sops); +if (ret) +return ret; +guard (mutex)(&remote->lock); iter =trace_remote_iter (remote, cpu,TRI_NONCONSUMING ); -if (IS_ERR (iter)) +if (IS_ERR (iter)) { +seq_release (inode, filp);return PTR_ERR (iter); - - ret =seq_open (filp, &trace_sops); -if (ret) { -trace_remote_iter_free (iter); -return ret; } - ((struct seq_file *)filp->private_data)->private = (void *)iter; + ((struct seq_file *)filp->private_data)->private = iter;return 0 ; }
@@ -932,8 +999,12 @@ int trace_remote_register(const char *name, struct trace_remote_callbacks *cbs, } ret = cbs->init ? cbs->init (remote->dentry, priv) :0 ; -if (ret) +if (ret) {pr_err ("Init failed for trace remote '%s' (%d)\n" , name, ret); + }else { +guard (mutex)(&trace_remotes_lock); +list_add (&remote->node, &trace_remotes); + }return ret; }
@@ -1069,7 +1140,7 @@ static ssize_t remote_event_enable_write(struct file *filp, const char __user *ureturn count; } -DEFINE_SHOW_STORE_ATTRIBUTE (remote_event_enable); +DEFINE_TRACE_REMOTE_ATTRIBUTE (remote_event_enable);static int remote_event_id_show (struct seq_file *s,void *unused) {
@@ -1079,7 +1150,7 @@ static int remote_event_id_show(struct seq_file *s, void *unused)return 0 ; } -DEFINE_SHOW_ATTRIBUTE (remote_event_id); +DEFINE_TRACE_REMOTE_SHOW_ATTRIBUTE (remote_event_id);static int remote_event_format_show (struct seq_file *s,void *unused) {
@@ -1108,7 +1179,7 @@ static int remote_event_format_show(struct seq_file *s, void *unused)return 0 ; } -DEFINE_SHOW_ATTRIBUTE (remote_event_format); +DEFINE_TRACE_REMOTE_SHOW_ATTRIBUTE (remote_event_format);static int remote_event_callback (const char *name,umode_t *mode,void **data,const struct file_operations **fops)
@@ -1194,6 +1265,7 @@ static ssize_t remote_events_dir_enable_read(struct file *filp, char __user *ubu }static const struct file_operations remote_events_dir_enable_fops = { + .open = trace_remote_tracefs_open, .write = remote_events_dir_enable_write, .read = remote_events_dir_enable_read, };
@@ -1218,6 +1290,7 @@ remote_events_dir_header_page_read(struct file *filp, char __user *ubuf, size_t }static const struct file_operations remote_events_dir_header_page_fops = { + .open = trace_remote_tracefs_open, .read = remote_events_dir_header_page_read, };
@@ -1241,6 +1314,7 @@ remote_events_dir_header_event_read(struct file *filp, char __user *ubuf, size_t }static const struct file_operations remote_events_dir_header_event_fops = { + .open = trace_remote_tracefs_open, .read = remote_events_dir_header_event_read, };
Signature · 1 line
2.55.0.691.gc56d675ccc-goog
[email protected]
AI reviewRe: [PATCH v5 01/18] tracing/remotes: Gate tracefs files opening on trace remote registration
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Incomplete tracefs gating: Caller-created tracefs files bypass the newly introduced…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Incomplete tracefs gating: Caller-created tracefs files bypass the newly introduced trace_remote_tracefs_open gating, leaving them fully accessible if registration fails. --
--- Patch [1]: [PATCH v5 01/18] tracing/remotes: Gate tracefs files opening on trace remote registration --- commit 63ef30c302e1341f240b4f5b82d4e65d421da4cf
Author: Vincent Donnefort <[email protected]>
tracing/remotes: Gate tracefs files opening on trace remote registration
This commit adds a global list of registered trace remotes and a gating function to prevent access to tracefs files for remotes that are being destroyed or failed to register. Standard trace remote file operations are wrapped to check this list before allowing open().
Quoted 2 lines
kernel/trace/trace_remote.c +0
-0
[ ... ]
Quoted 4 lines
kernel/trace/trace_remote.c +0
-0@@ -932,8 +999,12 @@ int trace_remote_register(const char *name, struct trace_remote_callbacks *cbs, } ret = cbs->init ? cbs->init (remote->dentry, priv) :0 ;
kernel/trace/trace_remote.c int trace_remote_register(const char *name, struct trace_remote_callbacks *cbs, · line 1010
Quoted 10 lines
kernel/trace/trace_remote.c +5
-1-if (ret) +if (ret) {pr_err ("Init failed for trace remote '%s' (%d)\n" , name, ret); + }else { +guard (mutex)(&trace_remotes_lock); +list_add (&remote->node, &trace_remotes); + }return ret; }
[Severity: High]
Does this leave caller-created tracefs files exposed if cbs->init() fails?
If a remote's initialization fails halfway through, any custom tracefs files already created by cbs->init() will leak and remain accessible. Because trace_remote_tracefs_open() is static to trace_remote.c, the caller cannot wrap their custom files' open() callbacks with this new gating mechanism.
For example, if KVM's hyp_trace_init_tracefs() successfully creates a 'write_event' file but fails later, trace_remote_register() returns an error and the remote is not added to the trace_remotes list.
Userspace could then open the leaked 'write_event' file, bypassing the gating entirely, and trigger kvm_call_hyp_nvhe() against the uninitialized hypervisor stub.
Should trace_remote_tracefs_open() be exported so that subsystems can gate their custom files, or should the leaked files be cleaned up when trace_remote_register() returns an error?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Vincent Donnefort
maintainerRe: [PATCH v5 01/18] tracing/remotes: Gate tracefs files opening on trace remote registration
Quoted 46 lines · On Wed, Aug 12, 2026 at 04:31:10PM +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Incomplete tracefs gating: Caller-created tracefs files bypass the newly introducedtrace_remote_tracefs_opengating, leaving them fully accessible if registration fails. ----- Patch [1]: [PATCH v5 01/18] tracing/remotes: Gate tracefs files opening on trace remote registration --- commit
63ef30c302e1341f240b4f5b82d4e65d421da4cf
Author: Vincent Donnefort <[email protected]>
tracing/remotes: Gate tracefs files opening on trace remote registrationThis commit adds a global list of registered trace remotes and a gating function to prevent access to tracefs files for remotes that are being destroyed or failed to register. Standard trace remote file operations are wrapped to check this list before allowing
open().Quoted 2 lines
kernel/trace/trace_remote.c +0
-0[ ... ]
Quoted 4 lines
kernel/trace/trace_remote.c +0
-0@@ -932,8 +999,12 @@ int trace_remote_register(const char *name, struct trace_remote_callbacks *cbs, } ret = cbs->init ? cbs->init (remote->dentry, priv) :0 ;kernel/trace/trace_remote.c int trace_remote_register(const char *name, struct trace_remote_callbacks *cbs, · line 1010
Quoted 10 lines
kernel/trace/trace_remote.c +5
-1-if (ret) +if (ret) {pr_err ("Init failed for trace remote '%s' (%d)\n" , name, ret); + }else { +guard (mutex)(&trace_remotes_lock); +list_add (&remote->node, &trace_remotes); + }return ret; }[Severity: High]
Does this leave caller-created tracefs files exposed ifcbs->init()fails?If a remote's initialization fails halfway through, any custom tracefs files already created by
cbs->init()will leak and remain accessible. Becausetrace_remote_tracefs_open()is static totrace_remote.c, the caller cannot wrap their custom files'open()callbacks with this new gating mechanism.For example, if KVM's
hyp_trace_init_tracefs()successfully creates a 'write_event' file but fails later,trace_remote_register()returns an error and the remote is not added to thetrace_remoteslist.
Quoted 10 lines
Userspace could then open the leaked '
write_event' file, bypassing the gating entirely, and triggerkvm_call_hyp_nvhe()against the uninitialized hypervisor stub.Should
trace_remote_tracefs_open()be exported so that subsystems can gate their custom files, or should the leaked files be cleaned up whentrace_remote_register()returns an error?Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
In the following patch, the tracefs folder is entirely removed on init failure.
Signature · 1 line
Vincent
Vincent Donnefort
maintainerPATCH v5 2/18 tracing/remotes: Release tracefs,eventfs on registration failure
In reply to Vincent DonnefortIn trace_remote_register(), if registration of events or the init callback fails, the created tracefs and eventfs directories are leaked.
Release the entire eventfs and tracefs hierarchy on trace_remote registration failure.
kernel/trace/trace_remote.c +93 -44
@@ -45,7 +45,8 @@ struct trace_remote {struct trace_buffer *trace_buffer;struct trace_buffer_desc *trace_buffer_desc;struct dentry *dentry; -struct eventfs_inode *eventfs; +struct eventfs_inode *eventfs_root; +struct eventfs_inode *eventfs_subdir;struct remote_event *events;unsigned long nr_events;unsigned long trace_buffer_size;
@@ -60,6 +61,7 @@ struct trace_remote {static DEFINE_MUTEX (trace_remotes_lock);static LIST_HEAD (trace_remotes); +static struct dentry *trace_remotes_root;static bool trace_remote_loaded (struct trace_remote *remote) {
@@ -865,23 +867,21 @@ static const struct file_operations trace_fops = {static int trace_remote_init_tracefs (const char *name,struct trace_remote *remote) {struct dentry *remote_d, *percpu_d, *d; -static struct dentry *root; -static DEFINE_MUTEX (lock);bool root_inited = false;int cpu; -guard (mutex)(&lock); +lockdep_assert_held (&trace_remotes_lock); -if (!root) { - root =tracefs_create_dir (TRACEFS_DIR ,NULL ); -if (!root) { +if (!trace_remotes_root) { + trace_remotes_root =tracefs_create_dir (TRACEFS_DIR ,NULL ); +if (!trace_remotes_root) {pr_err ("Failed to create tracefs dir " TRACEFS_DIR "\n" );return -ENOMEM ; } root_inited = true; } - remote_d =tracefs_create_dir (name, root); + remote_d =tracefs_create_dir (name, trace_remotes_root);if (!remote_d) {pr_err ("Failed to create tracefs dir " TRACEFS_DIR "%s/\n" , name); goto err;
@@ -939,8 +939,8 @@ static int trace_remote_init_tracefs(const char *name, struct trace_remote *remo err:if (root_inited) { -tracefs_remove (root); - root =NULL ; +tracefs_remove (trace_remotes_root); + trace_remotes_root =NULL ; }else {tracefs_remove (remote_d); }
@@ -948,8 +948,26 @@ static int trace_remote_init_tracefs(const char *name, struct trace_remote *remoreturn -ENOMEM ; } +static void trace_remote_remove_tracefs (struct trace_remote *remote) +{ +lockdep_assert_held (&trace_remotes_lock); + +if (!remote->dentry) +return ; + +tracefs_remove (remote->dentry); + remote->dentry =NULL ; + +if (!list_empty (&trace_remotes)) +return ; + +tracefs_remove (trace_remotes_root); + trace_remotes_root =NULL ; +} +static int trace_remote_register_events (const char *remote_name,struct trace_remote *remote,struct remote_event *events,size_t nr_events); +static void trace_remote_unregister_events (struct trace_remote *remote);/** * trace_remote_register() - Register a Tracefs remote
@@ -972,10 +990,9 @@ static int trace_remote_register_events(const char *remote_name, struct trace_reint trace_remote_register (const char *name,struct trace_remote_callbacks *cbs,void *priv,struct remote_event *events,size_t nr_events) { -struct trace_remote *remote; + struct trace_remote *remote __free(kfree) = kzalloc_obj(*remote);int ret; - remote =kzalloc_obj (*remote);if (!remote)return -ENOMEM ;
@@ -986,13 +1003,15 @@ int trace_remote_register(const char *name, struct trace_remote_callbacks *cbs,mutex_init (&remote->lock);init_rwsem (&remote->reader_lock); -if (trace_remote_init_tracefs (name, remote)) { -kfree (remote); -return -ENOMEM ; - } +guard (mutex)(&trace_remotes_lock); + + ret =trace_remote_init_tracefs (name, remote); +if (ret) +return ret; ret =trace_remote_register_events (name, remote, events, nr_events);if (ret) { +trace_remote_remove_tracefs (remote);pr_err ("Failed to register events for trace remote '%s' (%d)\n" , name, ret);return ret;
@@ -1000,13 +1019,16 @@ int trace_remote_register(const char *name, struct trace_remote_callbacks *cbs, ret = cbs->init ? cbs->init (remote->dentry, priv) :0 ;if (ret) { +trace_remote_unregister_events (remote); +trace_remote_remove_tracefs (remote);pr_err ("Init failed for trace remote '%s' (%d)\n" , name, ret); - }else { -guard (mutex)(&trace_remotes_lock); -list_add (&remote->node, &trace_remotes); +return ret; } -return ret; +list_add (&remote->node, &trace_remotes); +retain_and_null_ptr (remote); + +return 0 ; }EXPORT_SYMBOL_GPL (trace_remote_register);
@@ -1345,7 +1367,6 @@ static int remote_events_dir_callback(const char *name, umode_t *mode, void **dastatic int trace_remote_init_eventfs (const char *remote_name,struct trace_remote *remote,struct remote_event *evt) { -struct eventfs_inode *eventfs = remote->eventfs;static struct eventfs_entry dir_entries[] = { { .name ="enable" ,
@@ -1370,35 +1391,37 @@ static int trace_remote_init_eventfs(const char *remote_name, struct trace_remot .callback = remote_event_callback, } }; -bool eventfs_create = false; +struct eventfs_inode *eventfs_root, *eventfs_subdir, *e; -if (!eventfs) { - eventfs =eventfs_create_events_dir ("events" , remote->dentry, dir_entries, -ARRAY_SIZE (dir_entries), remote); -if (IS_ERR (eventfs)) -return PTR_ERR (eventfs); + eventfs_root = remote->eventfs_root; + eventfs_subdir = remote->eventfs_subdir; +if (!eventfs_root) { + eventfs_root =eventfs_create_events_dir ("events" , remote->dentry, dir_entries, +ARRAY_SIZE (dir_entries), remote); +if (IS_ERR (eventfs_root)) +return PTR_ERR (eventfs_root);/* * Create similar hierarchy as local events even if a single system is supported at * the moment */ - eventfs =eventfs_create_dir (remote_name, eventfs,NULL ,0 ,NULL ); -if (IS_ERR (eventfs)) -return PTR_ERR (eventfs); - - remote->eventfs = eventfs; - eventfs_create = true; - } - - eventfs =eventfs_create_dir (evt->name, eventfs, entries,ARRAY_SIZE (entries), evt); -if (IS_ERR (eventfs)) { -if (eventfs_create) { -eventfs_remove_events_dir (remote->eventfs); - remote->eventfs =NULL ; + eventfs_subdir =eventfs_create_dir (remote_name, eventfs_root,NULL ,0 ,NULL ); +if (IS_ERR (eventfs_subdir)) { +eventfs_remove_events_dir (eventfs_root); +return PTR_ERR (eventfs_subdir); } -return PTR_ERR (eventfs); } + e =eventfs_create_dir (evt->name, eventfs_subdir, entries,ARRAY_SIZE (entries), evt); +if (IS_ERR (e)) { +if (!remote->eventfs_root) +eventfs_remove_events_dir (eventfs_root); +return PTR_ERR (e); + } + + remote->eventfs_root = eventfs_root; + remote->eventfs_subdir = eventfs_subdir; +return 0 ; }
@@ -1413,11 +1436,11 @@ static int trace_remote_attach_events(struct trace_remote *remote, struct remoteif (evt->remote)return -EEXIST ; - evt->remote = remote; -/* We need events to be sorted for efficient lookup */ if (i && evt->id <= events[i -1 ].id)return -EINVAL ; + + evt->remote = remote; } remote->events = events;
@@ -1426,14 +1449,33 @@ static int trace_remote_attach_events(struct trace_remote *remote, struct remotereturn 0 ; } +static void trace_remote_detach_events (struct trace_remote *remote,struct remote_event *events, +size_t nr_events) +{ +int i; + +for (i =0 ; i < nr_events; i++) { +struct remote_event *evt = &events[i]; + +if (evt->remote == remote) + evt->remote =NULL ; + } + + remote->events =NULL ; + remote->nr_events =0 ; +} +static int trace_remote_register_events (const char *remote_name,struct trace_remote *remote,struct remote_event *events,size_t nr_events) {int i, ret; ret =trace_remote_attach_events (remote, events, nr_events); -if (ret) +if (ret) { +/* It is safe to call detach on a half-registered array */ +trace_remote_detach_events (remote, events, nr_events);return ret; + }for (i =0 ; i < nr_events; i++) {struct remote_event *evt = &events[i];
@@ -1447,6 +1489,13 @@ static int trace_remote_register_events(const char *remote_name, struct trace_rereturn 0 ; } +static void trace_remote_unregister_events (struct trace_remote *remote) +{ +trace_remote_detach_events (remote, remote->events, remote->nr_events); +if (remote->eventfs_root) +eventfs_remove_events_dir (remote->eventfs_root); +} +static int __cmp_events (const void *key,const void *data) {const struct remote_event *evt = data;
Signature · 1 line
2.55.0.691.gc56d675ccc-goog
[email protected]
AI reviewRe: [PATCH v5 02/18] tracing/remotes: Release tracefs,eventfs on registration failure
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
New issues:
- [High] Expanding the global trace_remotes_lock over the external driver callback cbs->init() introduces a severe risk of deadlocks and priority inversion.
Pre-existing issues:
- [Critical] A Time-Of-Check to Time-Of-Use race condition between trace_remote_tracefs_open() and concurrent unregistration leads to a Use-After-Free. --
--- Patch [2]: [PATCH v5 02/18] tracing/remotes: Release tracefs,eventfs on registration failure --- commit 70ddc6ce63b80a301a72ec3efb7683b32447865f
Author: Vincent Donnefort <[email protected]>
tracing/remotes: Release tracefs,eventfs on registration failure
In trace_remote_register(), if registration of events or the init callback fails, the created tracefs and eventfs directories are leaked. Release the entire eventfs and tracefs hierarchy on trace_remote registration failure.
Quoted 4 lines
kernel/trace/trace_remote.c +0
-0
[ ... ]
Quoted 33 lines
kernel/trace/trace_remote.c +7
-6@@ -972,10 +990,9 @@ static int trace_remote_register_events(const char *remote_name, struct trace_reint trace_remote_register (const char *name,struct trace_remote_callbacks *cbs,void *priv,struct remote_event *events,size_t nr_events) { -struct trace_remote *remote; + struct trace_remote *remote __free(kfree) = kzalloc_obj(*remote);int ret; - remote =kzalloc_obj (*remote);if (!remote)return -ENOMEM ;@@ -986,13 +1003,15 @@ int trace_remote_register(const char *name, struct trace_remote_callbacks *cbs,mutex_init (&remote->lock);init_rwsem (&remote->reader_lock); -if (trace_remote_init_tracefs (name, remote)) { -kfree (remote); -return -ENOMEM ; - } +guard (mutex)(&trace_remotes_lock); + + ret =trace_remote_init_tracefs (name, remote); +if (ret) +return ret; ret =trace_remote_register_events (name, remote, events, nr_events);if (ret) { +trace_remote_remove_tracefs (remote);pr_err ("Failed to register events for trace remote '%s' (%d)\n" , name, ret);return ret;
kernel/trace/trace_remote.c int trace_remote_register(const char *name, struct trace_remote_callbacks *cbs, · line 1020
Quoted 2 lines
kernel/trace/trace_remote.c +0
-0@@ -1000,13 +1019,16 @@ int trace_remote_register(const char *name, struct trace_remote_callbacks *cbs, ret = cbs->init ? cbs->init (remote->dentry, priv) :0 ;
[Severity: High]
Does holding the global trace_remotes_lock while calling the external init() callback introduce a risk of deadlocks?
Because trace_remotes_lock was acquired via guard(mutex) earlier in this function, calling an opaque external callback with this global subsystem lock held establishes an inverted lock dependency.
If the teardown path of the external driver takes its own locks before unregistering the trace remote, could this result in an ABBA deadlock?
Quoted 5 lines
kernel/trace/trace_remote.c +2
-1if (ret) { +trace_remote_unregister_events (remote); +trace_remote_remove_tracefs (remote);pr_err ("Init failed for trace remote '%s' (%d)\n" , name, ret); - }else {
kernel/trace/trace_remote.c int trace_remote_register(const char *name, struct trace_remote_callbacks *cbs, · line 1032
Quoted 10 lines
kernel/trace/trace_remote.c +4
-3-guard (mutex)(&trace_remotes_lock); -list_add (&remote->node, &trace_remotes); } -return ret; +list_add (&remote->node, &trace_remotes); +retain_and_null_ptr (remote); + +return 0 ; }
[Severity: Critical]
This is a pre-existing issue, but is there a race condition between trace_remote_tracefs_open() and concurrent unregistration?
In trace_remote_tracefs_open(), the code checks if the remote object exists in the global trace_remotes list:
kernel/trace/trace_remote.c:trace_remote_tracefs_open() {
...
guard(mutex)(&trace_remotes_lock);
/* i_private is either a struct trace_remote or a struct remote_event */
list_for_each_entry(r, &trace_remotes, node) {
if (r == i_private)
return 0;
...
}However, the lock is dropped on return without incrementing the object's reference count.
Then in trace_open(), the unprotected remote pointer is dereferenced:
kernel/trace/trace_remote.c:trace_open() {
...
ret = trace_remote_tracefs_open(inode, filp);
if (ret)
return ret;
if (!(filp->f_mode & FMODE_READ))
return 0;
...
guard(mutex)(&remote->lock);
...
}If a concurrent unregistration acquires trace_remotes_lock, removes the remote, and frees it after trace_remote_tracefs_open() returns but before trace_open() acquires remote->lock, will this result in a use-after-free regression?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Vincent Donnefort
maintainerRe: [PATCH v5 02/18] tracing/remotes: Release tracefs,eventfs on registration failure
Quoted 67 lines · On Wed, Aug 12, 2026 at 04:25:03PM +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
New issues:
- [High] Expanding the globaltrace_remotes_lockover the external driver callbackcbs->init()introduces a severe risk of deadlocks and priority inversion.Pre-existing issues:
- [Critical] A Time-Of-Check to Time-Of-Use race condition betweentrace_remote_tracefs_open()and concurrent unregistration leads to a Use-After-Free. ----- Patch [2]: [PATCH v5 02/18] tracing/remotes: Release tracefs,eventfs on registration failure --- commit
70ddc6ce63b80a301a72ec3efb7683b32447865f
Author: Vincent Donnefort <[email protected]>tracing/remotes: Release tracefs,eventfs on registration failure
In
trace_remote_register(), if registration of events or the init callback fails, the created tracefs and eventfs directories are leaked. Release the entire eventfs and tracefs hierarchy ontrace_remoteregistration failure.Quoted 4 lines
kernel/trace/trace_remote.c +0
-0[ ... ]
Quoted 33 lines
kernel/trace/trace_remote.c +7
-6@@ -972,10 +990,9 @@ static int trace_remote_register_events(const char *remote_name, struct trace_reint trace_remote_register (const char *name,struct trace_remote_callbacks *cbs,void *priv,struct remote_event *events,size_t nr_events) { -struct trace_remote *remote; + struct trace_remote *remote __free(kfree) = kzalloc_obj(*remote);int ret; - remote =kzalloc_obj (*remote);if (!remote)return -ENOMEM ;@@ -986,13 +1003,15 @@ int trace_remote_register(const char *name, struct trace_remote_callbacks *cbs,mutex_init (&remote->lock);init_rwsem (&remote->reader_lock); -if (trace_remote_init_tracefs (name, remote)) { -kfree (remote); -return -ENOMEM ; - } +guard (mutex)(&trace_remotes_lock); + + ret =trace_remote_init_tracefs (name, remote); +if (ret) +return ret; ret =trace_remote_register_events (name, remote, events, nr_events);if (ret) { +trace_remote_remove_tracefs (remote);pr_err ("Failed to register events for trace remote '%s' (%d)\n" , name, ret);return ret;kernel/trace/trace_remote.c int trace_remote_register(const char *name, struct trace_remote_callbacks *cbs, · line 1020
Quoted 2 lines
kernel/trace/trace_remote.c +0
-0@@ -1000,13 +1019,16 @@ int trace_remote_register(const char *name, struct trace_remote_callbacks *cbs, ret = cbs->init ? cbs->init (remote->dentry, priv) :0 ;[Severity: High]
Does holding the globaltrace_remotes_lockwhile calling the externalinit()callback introduce a risk of deadlocks?
Quoted 6 lines
Because
trace_remotes_lockwas acquired via guard(mutex) earlier in this function, calling an opaque external callback with this global subsystem lock held establishes an inverted lock dependency.If the teardown path of the external driver takes its own locks before unregistering the trace remote, could this result in an ABBA deadlock?
Current users are just fine. I do not think that is something worth considering at the moment.
Quoted 53 lines
Quoted 6 lines
kernel/trace/trace_remote.c +2
-1if (ret) { +trace_remote_unregister_events (remote); +trace_remote_remove_tracefs (remote);pr_err ("Init failed for trace remote '%s' (%d)\n" , name, ret); - }else {kernel/trace/trace_remote.c int trace_remote_register(const char *name, struct trace_remote_callbacks *cbs, · line 1032
Quoted 10 lines
kernel/trace/trace_remote.c +4
-3-guard (mutex)(&trace_remotes_lock); -list_add (&remote->node, &trace_remotes); } -return ret; +list_add (&remote->node, &trace_remotes); +retain_and_null_ptr (remote); + +return 0 ; }[Severity: Critical]
This is a pre-existing issue, but is there a race condition betweentrace_remote_tracefs_open()and concurrent unregistration?In
trace_remote_tracefs_open(), the code checks if the remote object exists in the globaltrace_remoteslist:kernel/trace/trace_remote.c:trace_remote_tracefs_open() { ... guard(mutex)(&trace_remotes_lock); /* i_private is either a struct trace_remote or a struct remote_event */ list_for_each_entry(r, &trace_remotes, node) { if (r == i_private) return 0; ... }However, the lock is dropped on return without incrementing the object's reference count.
Then in
trace_open(), the unprotected remote pointer is dereferenced:kernel/trace/trace_remote.c:trace_open() { ... ret = trace_remote_tracefs_open(inode, filp); if (ret) return ret; if (!(filp->f_mode & FMODE_READ)) return 0; ... guard(mutex)(&remote->lock); ... }
Quoted 4 lines
If a concurrent unregistration acquires
trace_remotes_lock, removes the remote, and frees it aftertrace_remote_tracefs_open()returns but beforetrace_open()acquiresremote->lock, will this result in a use-after-free regression?
trace_remote_tracefs_open() will block until the remote is properly registered, that is, added to the list trace_remotes.
After the remote is added to the list, there is no unregistration possible at the moment.
I do not see any concurrent unregistration possible.
Signature · 1 line
Vincent
Quoted 3 lines
Signature · 2 lines
-- Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Vincent Donnefort
maintainerPATCH v5 3/18 tracing/remotes: Use kstrtobool for boolean tracefs files
In reply to Vincent DonnefortUse kstrtobool in trace_remote.c where possible. This is more user-friendly as it allows a better variety of input strings.
kernel/trace/trace_remote.c +6 -6
@@ -235,10 +235,10 @@ tracing_on_write(struct file *filp, const char __user *ubuf, size_t cnt, loff_t {struct seq_file *seq = filp->private_data;struct trace_remote *remote = seq->private; -unsigned long val; +bool val;int ret; - ret =kstrtoul_from_user (ubuf, cnt,10 , &val); + ret =kstrtobool_from_user (ubuf, cnt, &val);if (ret)return ret;
@@ -1147,10 +1147,10 @@ static ssize_t remote_event_enable_write(struct file *filp, const char __user *ustruct seq_file *seq = filp->private_data;struct remote_event *evt = seq->private;struct trace_remote *remote = evt->remote; -u8 enable; +bool enable;int ret; - ret =kstrtou8_from_user (ubuf, count,10 , &enable); + ret =kstrtobool_from_user (ubuf, count, &enable);if (ret)return ret;
@@ -1231,10 +1231,10 @@ static ssize_t remote_events_dir_enable_write(struct file *filp, const char __us size_t count, loff_t *ppos) { struct trace_remote *remote = file_inode(filp)->i_private; +bool enable;int i, ret; -u8 enable; - ret =kstrtou8_from_user (ubuf, count,10 , &enable); + ret =kstrtobool_from_user (ubuf, count, &enable);if (ret)return ret;
Signature · 1 line
2.55.0.691.gc56d675ccc-goog
Vincent Donnefort
maintainerPATCH v5 4/18 tracing/remotes: Use a single per-remote polling work
In reply to Vincent DonnefortHaving a per-iterator polling work is wasteful when logging several trace_remote per_cpu/trace_pipe files in parallel. This results in one work running per-CPU, where only one would suffice.
Transition to a single per-remote polling work, scheduled on the first consumer creation and stopped when the last consuming iterator is freed.
This blanket polls all CPUs, regardless of which ones are actually being read. This is acceptable because the poll consists of reading the meta-page, which is a fast operation. Also, it is more common to log all CPUs in the system than only one, so this use-case should be favoured.
kernel/trace/trace_remote.c +35 -16
@@ -26,7 +26,6 @@ enum tri_type {struct trace_remote_iterator {struct trace_remote *remote;struct trace_seq seq; -struct delayed_work poll_work;unsigned long lost_events;u64 ts;struct ring_buffer_iter *rb_iter;
@@ -55,8 +54,10 @@ struct trace_remote {struct rw_semaphore reader_lock;struct rw_semaphore *pcpu_reader_locks;unsigned int nr_readers; +unsigned int poll_cnt;unsigned int poll_ms;bool tracing_on; +struct delayed_work poll_work; };static DEFINE_MUTEX (trace_remotes_lock);
@@ -350,17 +351,6 @@ static bool trace_remote_has_cpu(struct trace_remote *remote, int cpu)return ring_buffer_poll_remote (remote->trace_buffer, cpu) ==0 ; } -static void __poll_remote (struct work_struct *work) -{ -struct delayed_work *dwork =to_delayed_work (work); -struct trace_remote_iterator *iter; - - iter = container_of(dwork, struct trace_remote_iterator, poll_work); -ring_buffer_poll_remote (iter->remote->trace_buffer, iter->cpu); -schedule_delayed_work ((struct delayed_work *)work, -msecs_to_jiffies (iter->remote->poll_ms)); -} -static void __free_ring_buffer_iter (struct trace_remote_iterator *iter,int cpu) {if (cpu !=RING_BUFFER_ALL_CPUS ) {
@@ -404,6 +394,36 @@ static int __alloc_ring_buffer_iter(struct trace_remote_iterator *iter, int cpu)return 0 ; } +static void trace_remote_do_poll (struct trace_remote *remote) +{ +ring_buffer_poll_remote (remote->trace_buffer,RING_BUFFER_ALL_CPUS ); +schedule_delayed_work (&remote->poll_work,msecs_to_jiffies (remote->poll_ms)); +} + +static void __poll_remote (struct work_struct *work) +{ +struct delayed_work *dwork =to_delayed_work (work); + +trace_remote_do_poll (container_of(dwork ,struct trace_remote ,poll_work )); +} + +static void trace_remote_inc_poll (struct trace_remote *remote) +{ +/* poll_cnt <= nr_readers, inherits its overflow protection */ +if (!remote->poll_cnt++) +trace_remote_do_poll (remote); +} + +static void trace_remote_dec_poll (struct trace_remote *remote) +{ +if (WARN_ON_ONCE (!remote->poll_cnt)) +return ; + + remote->poll_cnt--; +if (!remote->poll_cnt) +cancel_delayed_work_sync (&remote->poll_work); +} +static struct trace_remote_iterator *trace_remote_iter (struct trace_remote *remote,int cpu,enum tri_type type) {
@@ -433,9 +453,7 @@ static struct trace_remote_iteratorswitch (type) {case TRI_CONSUMING : -ring_buffer_poll_remote (remote->trace_buffer, cpu); -INIT_DELAYED_WORK (&iter->poll_work, __poll_remote); -schedule_delayed_work (&iter->poll_work,msecs_to_jiffies (remote->poll_ms)); +trace_remote_inc_poll (remote);break ;case TRI_NONCONSUMING : ret =__alloc_ring_buffer_iter (iter, cpu);
@@ -469,7 +487,7 @@ static void trace_remote_iter_free(struct trace_remote_iterator *iter)switch (iter->type) {case TRI_CONSUMING : -cancel_delayed_work_sync (&iter->poll_work); +trace_remote_dec_poll (remote);break ;case TRI_NONCONSUMING :__free_ring_buffer_iter (iter, iter->cpu);
@@ -1002,6 +1020,7 @@ int trace_remote_register(const char *name, struct trace_remote_callbacks *cbs, remote->poll_ms =100 ;mutex_init (&remote->lock);init_rwsem (&remote->reader_lock); +INIT_DELAYED_WORK (&remote->poll_work, __poll_remote);guard (mutex)(&trace_remotes_lock);
Signature · 1 line
2.55.0.691.gc56d675ccc-goog
Vincent Donnefort
maintainerPATCH v5 5/18 tracing/simple_ring_buffer: Add support for compressed length
In reply to Vincent DonnefortThe array length is the total size in bytes of the data for the current event. It is possible to compress this value into the event header type, which has 28 unused types, saving 32 bits for sufficiently small events.
The compressed length is expressed as a multiple of the ring-buffer alignment, 4-bytes by default. Enforce this alignment.
kernel/trace/simple_ring_buffer.c +20 -5
@@ -207,7 +207,15 @@ static unsigned long rb_event_size(unsigned long length) {struct ring_buffer_event *event; -return length +RB_EVNT_HDR_SIZE +sizeof (event->array[0 ]); +if (!length) + length++; + + length =ALIGN (length,RB_ALIGNMENT ); + +if (length >RB_MAX_SMALL_DATA ) + length +=sizeof (event->array[0 ]); + +return length +RB_EVNT_HDR_SIZE ; } static struct ring_buffer_event *
@@ -223,12 +231,15 @@ rb_event_add_ts_extend(struct ring_buffer_event *event, u64 delta)static struct ring_buffer_event *simple_rb_reserve_next (struct simple_rb_per_cpu *cpu_buffer,unsigned long length,u64 timestamp) { -unsigned long ts_ext_size =0 , event_size =rb_event_size (length);struct simple_buffer_page *tail = cpu_buffer->tail_page; +unsigned long event_size, array_size, ts_ext_size =0 ;struct ring_buffer_event *event;u32 write, prev_write;u64 time_delta; + event_size =rb_event_size (length); + array_size = event_size -RB_EVNT_HDR_SIZE ; + time_delta = timestamp - cpu_buffer->write_stamp;if (test_time_stamp (time_delta))
@@ -259,9 +270,13 @@ simple_rb_reserve_next(struct simple_rb_per_cpu *cpu_buffer, unsigned long lengt time_delta =0 ; } - event->type_len =0 ; +if (length >RB_MAX_SMALL_DATA ) { + event->type_len =0 ; + event->array[0 ] = array_size; + }else { + event->type_len =DIV_ROUND_UP (array_size,RB_ALIGNMENT ); + } event->time_delta = time_delta; - event->array[0 ] = event_size -RB_EVNT_HDR_SIZE ;return event; }
@@ -284,7 +299,7 @@ void *simple_ring_buffer_reserve(struct simple_rb_per_cpu *cpu_buffer, unsigned rb_event =simple_rb_reserve_next (cpu_buffer, length, timestamp); -return &rb_event->array[1 ]; +return rb_event->type_len ? &rb_event->array[0 ] : &rb_event->array[1 ]; }EXPORT_SYMBOL_GPL (simple_ring_buffer_reserve);
Signature · 1 line
2.55.0.691.gc56d675ccc-goog
[email protected]
AI reviewRe: [PATCH v5 05/18] tracing/simple_ring_buffer: Add support for compressed length
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
New issues:
- [High] The patch unconditionally introduces type_len compression for small events and hardcodes 4-byte alignment, ignoring RB_FORCE_8BYTE_ALIGNMENT and RB_ARCH_ALIGNMENT. This causes payload truncation and unaligned accesses on architectures requiring 8-byte alignment.
Pre-existing issues:
- [High] simple_ring_buffer_reserve lacks a bounds check to prevent event_size from exceeding the maximum page capacity (PAGE_SIZE - BUF_PAGE_HDR_SIZE). --
--- Patch [5]: [PATCH v5 05/18] tracing/simple_ring_buffer: Add support for compressed length --- commit 661f4dfe638155c632fb481b814114210260a54c
Author: Vincent Donnefort <[email protected]>
tracing/simple_ring_buffer: Add support for compressed length
This patch adds support for compressing the event length into the event header type for small events in the simple ring buffer, saving 32 bits per event. It enforces a 4-byte alignment for the compressed length.
kernel/trace/simple_ring_buffer.c static unsigned long rb_event_size(unsigned long length) · line 213
Quoted 13 lines
kernel/trace/simple_ring_buffer.c +4
-1@@ -207,7 +207,15 @@ static unsigned long rb_event_size(unsigned long length) {struct ring_buffer_event *event; -return length +RB_EVNT_HDR_SIZE +sizeof (event->array[0 ]); +if (!length) + length++; + + length =ALIGN (length,RB_ALIGNMENT );
[Severity: High]
Does this hardcoded 4-byte alignment cause unaligned access exceptions on architectures that require 8-byte alignment (where RB_FORCE_8BYTE_ALIGNMENT is 1)?
It looks like using RB_ARCH_ALIGNMENT instead of RB_ALIGNMENT would prevent these exceptions.
Quoted 6 lines
kernel/trace/simple_ring_buffer.c +5
-0+ +if (length >RB_MAX_SMALL_DATA ) + length +=sizeof (event->array[0 ]); + +return length +RB_EVNT_HDR_SIZE ; }
[ ... ]
kernel/trace/simple_ring_buffer.c simple_rb_reserve_next(struct simple_rb_per_cpu *cpu_buffer, unsigned long lengt · line 278
Quoted 11 lines
kernel/trace/simple_ring_buffer.c +6
-1@@ -259,9 +270,13 @@ simple_rb_reserve_next(struct simple_rb_per_cpu *cpu_buffer, unsigned long lengt time_delta =0 ; } - event->type_len =0 ; +if (length >RB_MAX_SMALL_DATA ) { + event->type_len =0 ; + event->array[0 ] = array_size; + }else { + event->type_len =DIV_ROUND_UP (array_size,RB_ALIGNMENT ); + }
[Severity: High]
Can this unconditional compression cause payload truncation on architectures requiring 8-byte alignment?
If RB_FORCE_8BYTE_ALIGNMENT is set, it seems the core ring buffer reader (ring_buffer_event_length) might subsequently subtract 4 bytes erroneously, leading to data loss and potential out-of-bounds reads when parsing the event.
kernel/trace/simple_ring_buffer.c void *simple_ring_buffer_reserve(struct simple_rb_per_cpu *cpu_buffer, unsigned · line 302
Quoted 11 lines
kernel/trace/simple_ring_buffer.c +1
-2event->time_delta = time_delta; - event->array[0 ] = event_size -RB_EVNT_HDR_SIZE ;return event; }@@ -284,7 +299,7 @@ void *simple_ring_buffer_reserve(struct simple_rb_per_cpu *cpu_buffer, unsigned rb_event =simple_rb_reserve_next (cpu_buffer, length, timestamp); -return &rb_event->array[1 ]; +return rb_event->type_len ? &rb_event->array[0 ] : &rb_event->array[1 ];
[Severity: High]
This is a pre-existing issue, but does simple_ring_buffer_reserve() lack a bounds check for events larger than the maximum page capacity?
If a caller attempts to reserve an event with a length exceeding PAGE_SIZE - BUF_PAGE_HDR_SIZE, simple_rb_reserve_next() will move to a new page and set the write index beyond the page boundary. This would return a pointer that allows the caller to write out of bounds.
Would it be safer to validate that length is smaller than the maximum buffer page data size before proceeding with the reservation?
Quoted 2 lines
kernel/trace/simple_ring_buffer.c +0
-0}EXPORT_SYMBOL_GPL (simple_ring_buffer_reserve);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
Vincent Donnefort
maintainerRe: [PATCH v5 05/18] tracing/simple_ring_buffer: Add support for compressed length
Quoted 38 lines · On Wed, Aug 12, 2026 at 04:42:41PM +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
New issues:
- [High] The patch unconditionally introducestype_lencompression for small events and hardcodes 4-byte alignment, ignoringRB_FORCE_8BYTE_ALIGNMENTandRB_ARCH_ALIGNMENT. This causes payload truncation and unaligned accesses on architectures requiring 8-byte alignment.Pre-existing issues:
- [High]simple_ring_buffer_reservelacks a bounds check to preventevent_sizefrom exceeding the maximum page capacity (PAGE_SIZE - BUF_PAGE_HDR_SIZE). ----- Patch [5]: [PATCH v5 05/18] tracing/simple_ring_buffer: Add support for compressed length --- commit
661f4dfe638155c632fb481b814114210260a54c
Author: Vincent Donnefort <[email protected]>tracing/simple_ring_buffer: Add support for compressed length
This patch adds support for compressing the event length into the event header type for small events in the simple ring buffer, saving 32 bits per event. It enforces a 4-byte alignment for the compressed length.
kernel/trace/simple_ring_buffer.c static unsigned long rb_event_size(unsigned long length) · line 213
Quoted 13 lines
kernel/trace/simple_ring_buffer.c +4
-1@@ -207,7 +207,15 @@ static unsigned long rb_event_size(unsigned long length) {struct ring_buffer_event *event; -return length +RB_EVNT_HDR_SIZE +sizeof (event->array[0 ]); +if (!length) + length++; + + length =ALIGN (length,RB_ALIGNMENT );[Severity: High]
Does this hardcoded 4-byte alignment cause unaligned access exceptions on architectures that require 8-byte alignment (whereRB_FORCE_8BYTE_ALIGNMENTis 1)?
Quoted 2 lines
It looks like using
RB_ARCH_ALIGNMENTinstead ofRB_ALIGNMENTwould prevent these exceptions.
I will add support for RB_FORCE_8BYTE_ALIGNMENT in v6.
Quoted 46 lines
Quoted 7 lines
kernel/trace/simple_ring_buffer.c +5
-0+ +if (length >RB_MAX_SMALL_DATA ) + length +=sizeof (event->array[0 ]); + +return length +RB_EVNT_HDR_SIZE ; }[ ... ]
kernel/trace/simple_ring_buffer.c simple_rb_reserve_next(struct simple_rb_per_cpu *cpu_buffer, unsigned long lengt · line 278
Quoted 11 lines
kernel/trace/simple_ring_buffer.c +6
-1@@ -259,9 +270,13 @@ simple_rb_reserve_next(struct simple_rb_per_cpu *cpu_buffer, unsigned long lengt time_delta =0 ; } - event->type_len =0 ; +if (length >RB_MAX_SMALL_DATA ) { + event->type_len =0 ; + event->array[0 ] = array_size; + }else { + event->type_len =DIV_ROUND_UP (array_size,RB_ALIGNMENT ); + }[Severity: High]
Can this unconditional compression cause payload truncation on architectures requiring 8-byte alignment?If
RB_FORCE_8BYTE_ALIGNMENTis set, it seems the core ring buffer reader (ring_buffer_event_length) might subsequently subtract 4 bytes erroneously, leading to data loss and potential out-of-bounds reads when parsing the event.kernel/trace/simple_ring_buffer.c void *simple_ring_buffer_reserve(struct simple_rb_per_cpu *cpu_buffer, unsigned · line 302
Quoted 11 lines
kernel/trace/simple_ring_buffer.c +1
-2event->time_delta = time_delta; - event->array[0 ] = event_size -RB_EVNT_HDR_SIZE ;return event; }@@ -284,7 +299,7 @@ void *simple_ring_buffer_reserve(struct simple_rb_per_cpu *cpu_buffer, unsigned rb_event =simple_rb_reserve_next (cpu_buffer, length, timestamp); -return &rb_event->array[1 ]; +return rb_event->type_len ? &rb_event->array[0 ] : &rb_event->array[1 ];[Severity: High]
This is a pre-existing issue, but doessimple_ring_buffer_reserve()lack a bounds check for events larger than the maximum page capacity?
Quoted 7 lines
If a caller attempts to reserve an event with a length exceeding
PAGE_SIZE-BUF_PAGE_HDR_SIZE,simple_rb_reserve_next()will move to a new page and set the write index beyond the page boundary. This would return a pointer that allows the caller to write out of bounds.Would it be safer to validate that length is smaller than the maximum buffer page data size before proceeding with the reservation?
Right now we just do not consider such huge events as possible.
Signature · 1 line
Vincent
Quoted 6 lines
Quoted 3 lines
kernel/trace/simple_ring_buffer.c +0
-0}EXPORT_SYMBOL_GPL (simple_ring_buffer_reserve);Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
Vincent Donnefort
maintainerPATCH v5 6/18 tracing/remotes: Add dmesg tracefs file
In reply to Vincent DonnefortWhen enabled, the dmesg tracefs file enables the redirection of all events to dmesg. This is similar to tp_printk.
kernel/trace/trace_remote.c +125 -14
@@ -21,6 +21,7 @@enum tri_type {TRI_CONSUMING ,TRI_NONCONSUMING , +TRI_DMESG , };struct trace_remote_iterator {
@@ -43,6 +44,7 @@ struct trace_remote {void *priv;struct trace_buffer *trace_buffer;struct trace_buffer_desc *trace_buffer_desc; +struct trace_remote_iterator *dmesg;struct dentry *dentry;struct eventfs_inode *eventfs_root;struct eventfs_inode *eventfs_subdir;
@@ -394,10 +396,15 @@ static int __alloc_ring_buffer_iter(struct trace_remote_iterator *iter, int cpu)return 0 ; } +static bool trace_remote_do_dmesg (struct trace_remote *remote); +static void trace_remote_do_poll (struct trace_remote *remote) { +bool yield; +ring_buffer_poll_remote (remote->trace_buffer,RING_BUFFER_ALL_CPUS ); -schedule_delayed_work (&remote->poll_work,msecs_to_jiffies (remote->poll_ms)); + yield =trace_remote_do_dmesg (remote); +schedule_delayed_work (&remote->poll_work, yield ?0 :msecs_to_jiffies (remote->poll_ms)); }static void __poll_remote (struct work_struct *work)
@@ -452,6 +459,14 @@ static struct trace_remote_iteratortrace_seq_init (&iter->seq);switch (type) { +case TRI_DMESG : +/* only one dmesg iter allowed */ +if (WARN_ON_ONCE (remote->dmesg)) { + ret = -EBUSY ; +break ; + } +smp_store_release (&remote->dmesg, iter); + fallthrough;case TRI_CONSUMING :trace_remote_inc_poll (remote);break ;
@@ -486,6 +501,11 @@ static void trace_remote_iter_free(struct trace_remote_iterator *iter)lockdep_assert_held (&remote->lock);switch (iter->type) { +case TRI_DMESG : +WARN_ON_ONCE (remote->dmesg != iter); +smp_store_release (&remote->dmesg,NULL ); +flush_delayed_work (&remote->poll_work); + fallthrough;case TRI_CONSUMING :trace_remote_dec_poll (remote);break ;
@@ -498,13 +518,24 @@ static void trace_remote_iter_free(struct trace_remote_iterator *iter)trace_remote_put (remote); } +static bool trace_remote_iter_is_consuming (struct trace_remote_iterator *iter) +{ +switch (iter->type) { +case TRI_CONSUMING : +case TRI_DMESG : +return true; +default : +return false; + } +} +static void trace_remote_iter_read_start (struct trace_remote_iterator *iter) {struct trace_remote *remote = iter->remote;int cpu = iter->cpu;/* Acquire global reader lock */ -if (cpu ==RING_BUFFER_ALL_CPUS && iter->type ==TRI_CONSUMING ) +if (cpu ==RING_BUFFER_ALL_CPUS &&trace_remote_iter_is_consuming (iter))down_write (&remote->reader_lock);else down_read (&remote->reader_lock);
@@ -521,7 +552,7 @@ static void trace_remote_iter_read_start(struct trace_remote_iterator *iter)if (WARN_ON_ONCE (!remote->pcpu_reader_locks))return ; -if (iter->type ==TRI_CONSUMING ) +if (trace_remote_iter_is_consuming (iter))down_write (&remote->pcpu_reader_locks[cpu]);else down_read (&remote->pcpu_reader_locks[cpu]);
@@ -538,14 +569,14 @@ static void trace_remote_iter_read_finished(struct trace_remote_iterator *iter)* No need for the remote lock here, iter holds a reference on * remote->nr_readers */ -if (iter->type ==TRI_CONSUMING ) +if (trace_remote_iter_is_consuming (iter))up_write (&remote->pcpu_reader_locks[cpu]);else up_read (&remote->pcpu_reader_locks[cpu]); }/* Release global reader lock */ -if (cpu ==RING_BUFFER_ALL_CPUS && iter->type ==TRI_CONSUMING ) +if (cpu ==RING_BUFFER_ALL_CPUS &&trace_remote_iter_is_consuming (iter))up_write (&remote->reader_lock);else up_read (&remote->reader_lock);
@@ -562,10 +593,9 @@ __peek_event(struct trace_remote_iterator *iter, int cpu, u64 *ts, unsigned longstruct ring_buffer_event *rb_evt;struct ring_buffer_iter *rb_iter; -switch (iter->type) { -case TRI_CONSUMING : +if (trace_remote_iter_is_consuming (iter)) {return ring_buffer_peek (iter->remote->trace_buffer, cpu, ts, lost_events); -case TRI_NONCONSUMING : + }else { rb_iter =__get_rb_iter (iter, cpu);if (!rb_iter)return NULL ;
@@ -629,14 +659,10 @@ static void trace_remote_iter_move(struct trace_remote_iterator *iter) {struct trace_buffer *trace_buffer = iter->remote->trace_buffer; -switch (iter->type) { -case TRI_CONSUMING : +if (trace_remote_iter_is_consuming (iter))ring_buffer_consume (trace_buffer, iter->evt_cpu,NULL ,NULL ); -break ; -case TRI_NONCONSUMING : +else ring_buffer_iter_advance (__get_rb_iter (iter, iter->evt_cpu)); -break ; - } }static struct remote_event *trace_remote_find_event (struct trace_remote *remote,unsigned short id);
@@ -882,6 +908,87 @@ static const struct file_operations trace_fops = { .release = trace_release, }; +static bool trace_remote_do_dmesg (struct trace_remote *remote) +{ +struct trace_remote_iterator *iter =smp_load_acquire (&remote->dmesg); +unsigned int max_events =1000 ; + +if (!iter) +return false; + +trace_remote_iter_read_start (iter); + +while (trace_remote_iter_read_event (iter)) { +trace_seq_init (&iter->seq); + +trace_remote_iter_print_event (iter); +trace_seq_putc (&iter->seq,0 ); +if (!pr_info ("%s" ,seq_buf_str (&iter->seq.seq))) +break ; + +trace_remote_iter_move (iter); + +if (!(--max_events)) +break ; + } + +trace_remote_iter_read_finished (iter); + +return !max_events; +} + +static int trace_remote_enable_dmesg (struct trace_remote *remote,bool enable) +{ +struct trace_remote_iterator *iter = remote->dmesg; + +lockdep_assert_held (&remote->lock); + +if (enable == !!iter) +return 0 ; + +if (enable) { + iter =trace_remote_iter (remote,RING_BUFFER_ALL_CPUS ,TRI_DMESG ); +if (IS_ERR (iter)) +return PTR_ERR (iter); + }else { +trace_remote_iter_free (remote->dmesg); +/* trace_remote_iter_free has reset remote->dmesg */ + } + +return 0 ; +} + +static ssize_t +dmesg_write (struct file *filp,const char __user *ubuf,size_t cnt,loff_t *ppos) +{ +struct seq_file *seq = filp->private_data; +struct trace_remote *remote = seq->private; +bool val; +int ret; + + ret =kstrtobool_from_user (ubuf, cnt, &val); +if (ret) +return ret; + +guard (mutex)(&remote->lock); + + ret =trace_remote_enable_dmesg (remote, val); +if (ret) +return ret; + +return cnt; +} + +static int dmesg_show (struct seq_file *s,void *unused) +{ +struct trace_remote *remote = s->private; + +seq_printf (s,"%d\n" , !!remote->dmesg); + +return 0 ; +} +DEFINE_TRACE_REMOTE_ATTRIBUTE (dmesg); +static int trace_remote_init_tracefs (const char *name,struct trace_remote *remote) {struct dentry *remote_d, *percpu_d, *d;
@@ -922,6 +1029,10 @@ static int trace_remote_init_tracefs(const char *name, struct trace_remote *remoif (!d) goto err; + d =trace_create_file ("dmesg" ,TRACEFS_MODE_WRITE , remote_d, remote, &dmesg_fops); +if (!d) + goto err; + percpu_d =tracefs_create_dir ("per_cpu" , remote_d);if (!percpu_d) {pr_err ("Failed to create tracefs dir " TRACEFS_DIR "%s/per_cpu/\n" , name);
Signature · 1 line
2.55.0.691.gc56d675ccc-goog
[email protected]
AI reviewRe: [PATCH v5 06/18] tracing/remotes: Add dmesg tracefs file
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] When CONFIG_PRINTK is disabled, dmesg redirection stalls the event iterator perpetually because…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] When CONFIG_PRINTK is disabled, dmesg redirection stalls the event iterator perpetually because pr_info returns 0. --
--- Patch [6]: [PATCH v5 06/18] tracing/remotes: Add dmesg tracefs file --- commit b41c280df86084ff7600dad9b61a6fa3dbbdf1dd
Author: Vincent Donnefort <[email protected]>
tracing/remotes: Add dmesg tracefs file
When enabled, the dmesg tracefs file enables the redirection of all events to dmesg. This is similar to tp_printk.
Quoted 4 lines
kernel/trace/trace_remote.c +0
-0
[ ... ]
Quoted 11 lines
kernel/trace/trace_remote.c +7
-0@@ -882,6 +908,87 @@ static const struct file_operations trace_fops = { .release = trace_release, }; +static bool trace_remote_do_dmesg (struct trace_remote *remote) +{ +struct trace_remote_iterator *iter =smp_load_acquire (&remote->dmesg); +unsigned int max_events =1000 ; + +if (!iter) +return false;
kernel/trace/trace_remote.c static const struct file_operations trace_fops = { · line 927
Quoted 10 lines
kernel/trace/trace_remote.c +10
-0+ +trace_remote_iter_read_start (iter); + +while (trace_remote_iter_read_event (iter)) { +trace_seq_init (&iter->seq); + +trace_remote_iter_print_event (iter); +trace_seq_putc (&iter->seq,0 ); +if (!pr_info ("%s" ,seq_buf_str (&iter->seq.seq))) +break ;
[Severity: Medium]
When CONFIG_PRINTK is disabled, pr_info() is defined as no_printk, which unconditionally returns 0. Does this cause the loop to always break early and skip the call to trace_remote_iter_move() below?
If so, it appears the background polling work would continually fail to drain the trace remote buffer and run periodically without doing any work, as the iterator never advances.
Quoted 11 lines
kernel/trace/trace_remote.c +11
-0+ +trace_remote_iter_move (iter); + +if (!(--max_events)) +break ; + } + +trace_remote_iter_read_finished (iter); + +return !max_events; +}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
Vincent Donnefort
maintainerRe: [PATCH v5 06/18] tracing/remotes: Add dmesg tracefs file
Quoted 51 lines · On Wed, Aug 12, 2026 at 04:42:57PM +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] WhenCONFIG_PRINTKis disabled, dmesg redirection stalls the event iterator perpetually becausepr_inforeturns 0. ----- Patch [6]: [PATCH v5 06/18] tracing/remotes: Add dmesg tracefs file --- commit
b41c280df86084ff7600dad9b61a6fa3dbbdf1dd
Author: Vincent Donnefort <[email protected]>tracing/remotes: Add dmesg tracefs file
When enabled, the dmesg tracefs file enables the redirection of all events to dmesg. This is similar to
tp_printk.Quoted 4 lines
kernel/trace/trace_remote.c +0
-0[ ... ]
Quoted 11 lines
kernel/trace/trace_remote.c +7
-0@@ -882,6 +908,87 @@ static const struct file_operations trace_fops = { .release = trace_release, }; +static bool trace_remote_do_dmesg (struct trace_remote *remote) +{ +struct trace_remote_iterator *iter =smp_load_acquire (&remote->dmesg); +unsigned int max_events =1000 ; + +if (!iter) +return false;kernel/trace/trace_remote.c static const struct file_operations trace_fops = { · line 927
Quoted 10 lines
kernel/trace/trace_remote.c +10
-0+ +trace_remote_iter_read_start (iter); + +while (trace_remote_iter_read_event (iter)) { +trace_seq_init (&iter->seq); + +trace_remote_iter_print_event (iter); +trace_seq_putc (&iter->seq,0 ); +if (!pr_info ("%s" ,seq_buf_str (&iter->seq.seq))) +break ;[Severity: Medium]
WhenCONFIG_PRINTKis disabled,pr_info()is defined asno_printk, which unconditionally returns 0. Does this cause the loop to always break early and skip the call totrace_remote_iter_move()below?If so, it appears the background polling work would continually fail to drain the trace remote buffer and run periodically without doing any work, as the iterator never advances.
Quoted 14 lines
Quoted 11 lines
kernel/trace/trace_remote.c +11
-0+ +trace_remote_iter_move (iter); + +if (!(--max_events)) +break ; + } + +trace_remote_iter_read_finished (iter); + +return !max_events; +}Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
Do we really want to support !CONFIG_PRINTK?
In that case dmesg/dump_on_panic should just fail to enable... but is it really worth it?
Signature · 1 line
Vincent
Vincent Donnefort
maintainerPATCH v5 7/18 tracing/remotes: selftests: Add a test for the dmesg tracefs file
In reply to Vincent DonnefortExercise the newly introduced dmesg tracefs file that turns on and off the dmesg redirection.
tools/testing/selftests/ftrace/test.d/remotes/dmesg.tc new file +81 -0
@@ -0,0 +1,81 @@ +#!/bin/sh +# SPDX-License-Identifier: GPL-2.0 +# description: Test trace remote dmesg redirection +# requires: remotes/test + +. $TEST_DIR/remotes/functions + +test_dmesg() +{ + echo 0 > tracing_on + assert_unloaded + + # + # Test dmesg on/off when tracing is disabled + # + echo 1 > dmesg + test $(cat dmesg) -eq 1 + assert_loaded + + echo 0 > dmesg + test $(cat dmesg) -eq 0 + assert_unloaded + + # + # Test events are logged to dmesg + # + + echo 1 > tracing_on + assert_loaded + echo 1 > dmesg + test $(cat dmesg) -eq 1 + + # Timestamp format is '[ XXXX.YYYY]' + last_ts=$(dmesg | tail -1 | sed -n 's/^\[ *\([0-9]*.[0-9]*\)\].*/\1/p') + if [ -z "$last_ts" ]; then + last_ts=0 + fi + + nr_events=128 + for i in $(seq 1 $nr_events); do + echo $i > write_event + done + + sleep 1 + output=$(mktemp /tmp/remote_test.XXXXXX) + dmesg | awk -v ts=$last_ts ' + /^[ *[0-9]+\.[0-9]+\]/ { + t=$0; sub(/^[[ \t]*/, "", t); sub(/].*/, "", t); + if (t+0 > ts+0) print $0 + }' | grep "selftest id=" | sed 's/^[^]]*] //'> $output + + check_trace 1 $nr_events $output + + rm $output + + # + # Disable dmesg and verify events were not consumed by dmesg + # + echo 0 > dmesg + test $(cat dmesg) -eq 0 + + start_id=$(($nr_events + 1)) + end_id=$(($start_id + $nr_events)) + + for i in $(seq $start_id $end_id); do + echo $i > write_event + done + + sleep 1 + + output=$(dump_trace_pipe) + check_trace $start_id $end_id $output + rm $output +} + +if [ -z "$SOURCE_REMOTE_TEST" ]; then + set -e + + setup_remote_test + test_dmesg +fi
tools/testing/selftests/ftrace/test.d/remotes/functions +1 -0
@@ -8,6 +8,7 @@ setup_remote() cd remotes/$name/ echo 0 > tracing_on + echo 0 > dmesg clear_trace echo 7 > buffer_size_kb echo 0 > events/enable
tools/testing/selftests/ftrace/test.d/remotes/hypervisor/dmesg.tc new file +11 -0
@@ -0,0 +1,11 @@ +#!/bin/sh +# SPDX-License-Identifier: GPL-2.0 +# description: Test hypervisor trace dmesg redirection +# requires: remotes/hypervisor/write_event + +SOURCE_REMOTE_TEST=1 +. $TEST_DIR/remotes/dmesg.tc + +set -e +setup_remote "hypervisor" +test_dmesg
Signature · 1 line
2.55.0.691.gc56d675ccc-goog
[email protected]
AI reviewRe: [PATCH v5 07/18] tracing/remotes: selftests: Add a test for the dmesg tracefs file
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider: - [Medium] The test script leaks a temporary file in `/tmp/` if the test aborts due to a failure (e.g.…
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [Medium] The test script leaks a temporary file in /tmp/ if the test aborts due to a failure (e.g., when a command fails under set -e).
- [Medium] Unconditional write to the new dmesg tracefs file breaks the entire remote trace test suite on older kernels.
- [Medium] The new dmesg.tc script lacks the necessary # requires: header for the dmesg tracefs file, causing it to fail instead of being skipped on older kernels.
- [Low] The awk regular expression for matching dmesg timestamps contains an unescaped bracket, unintentionally creating a character class. --
--- Patch [7]: [PATCH v5 07/18] tracing/remotes: selftests: Add a test for the dmesg tracefs file --- commit bd86167985384496571cc5a8f03ca97dd77d76fd
Author: Vincent Donnefort <[email protected]>
tracing/remotes: selftests: Add a test for the dmesg tracefs file
Exercise the newly introduced dmesg tracefs file that turns on and off the dmesg redirection.
tools/testing/selftests/ftrace/test.d/remotes/dmesg.tc line 4
Quoted 10 lines
tools/testing/selftests/ftrace/test.d/remotes/dmesg.tc new file +4
-0@@ -0,0 +1,81 @@ +#!/bin/sh +# SPDX-License-Identifier: GPL-2.0 +# description: Test trace remote dmesg redirection +# requires: remotes/test
[Severity: Medium]
Does this script also need a "# requires: remotes/test/dmesg" declaration?
Without this explicit dependency, running the test on an older kernel that lacks the dmesg tracefs file will cause it to fail abruptly due to set -e, rather than gracefully skipping it as unsupported.
[ ... ]
tools/testing/selftests/ftrace/test.d/remotes/dmesg.tc line 45
Quoted 7 lines
tools/testing/selftests/ftrace/test.d/remotes/dmesg.tc new file +7
-0+ nr_events=128 + for i in $(seq 1 $nr_events); do + echo $i > write_event + done + + sleep 1 + output=$(mktemp /tmp/remote_test.XXXXXX)
[Severity: Medium]
Will this leak a temporary file in /tmp/ if the test aborts prematurely?
The script is running with set -e, so if a validation step like check_trace fails, it will immediately exit before reaching the rm $output command.
Should this use $TMPDIR instead, since the ftracetest framework cleans it up automatically?
tools/testing/selftests/ftrace/test.d/remotes/dmesg.tc line 47
Quoted 2 lines
tools/testing/selftests/ftrace/test.d/remotes/dmesg.tc new file +2
-0+ dmesg | awk -v ts=$last_ts ' + /^[ *[0-9]+\.[0-9]+\]/ {
[Severity: Low]
Does this regular expression match the bracket as intended?
By failing to escape the first bracket (^[), this creates a character class that matches spaces, asterisks, brackets, or digits instead of a literal opening bracket.
Could this be properly escaped as /^\[ *[0-9]+\.[0-9]+\]/ ?
Quoted 3 lines
tools/testing/selftests/ftrace/test.d/remotes/dmesg.tc new file +3
-0+ t=$0; sub(/^[[ \t]*/, "", t); sub(/].*/, "", t); + if (t+0 > ts+0) print $0 + }' | grep "selftest id=" | sed 's/^[^]]*] //'> $output
[ ... ]
tools/testing/selftests/ftrace/test.d/remotes/functions setup_remote() · line 11
Quoted 9 lines
tools/testing/selftests/ftrace/test.d/remotes/functions +1
-0@@ -8,6 +8,7 @@ setup_remote() cd remotes/$name/ echo 0 > tracing_on + echo 0 > dmesg
[Severity: Medium]
Does this unconditional write break the test suite on older kernels?
This setup function is used by all remote selftests. If the dmesg file does not exist, this echo will fail. Since the test scripts operate with set -e, this failure will cause the entire suite to crash on unsupported targets.
Could we check for the file's existence before writing to it?
Quoted 3 lines
tools/testing/selftests/ftrace/test.d/remotes/functions +0
-0clear_trace echo 7 > buffer_size_kb echo 0 > events/enable
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
Vincent Donnefort
maintainerRe: [PATCH v5 07/18] tracing/remotes: selftests: Add a test for the dmesg tracefs file
Quoted 44 lines · On Wed, Aug 12, 2026 at 04:40:39PM +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [Medium] The test script leaks a temporary file in/tmp/if the test aborts due to a failure (e.g., when a command fails underset -e).
- [Medium] Unconditional write to the newdmesgtracefs file breaks the entire remote trace test suite on older kernels.
- [Medium] The newdmesg.tcscript lacks the necessary# requires:header for thedmesgtracefs file, causing it to fail instead of being skipped on older kernels.
- [Low] The awk regular expression for matching dmesg timestamps contains an unescaped bracket, unintentionally creating a character class. ----- Patch [7]: [PATCH v5 07/18] tracing/remotes: selftests: Add a test for the dmesg tracefs file --- commit
bd86167985384496571cc5a8f03ca97dd77d76fd
Author: Vincent Donnefort <[email protected]>tracing/remotes: selftests: Add a test for the dmesg tracefs file
Exercise the newly introduced dmesg tracefs file that turns on and off the dmesg redirection.
tools/testing/selftests/ftrace/test.d/remotes/dmesg.tc line 4
Quoted 10 lines
tools/testing/selftests/ftrace/test.d/remotes/dmesg.tc new file +4
-0@@ -0,0 +1,81 @@ +#!/bin/sh +# SPDX-License-Identifier: GPL-2.0 +# description: Test trace remote dmesg redirection +# requires: remotes/test[Severity: Medium]
Does this script also need a "# requires: remotes/test/dmesg" declaration?Without this explicit dependency, running the test on an older kernel that lacks the dmesg tracefs file will cause it to fail abruptly due to set -e, rather than gracefully skipping it as unsupported.
[ ... ]
tools/testing/selftests/ftrace/test.d/remotes/dmesg.tc line 45
Quoted 7 lines
tools/testing/selftests/ftrace/test.d/remotes/dmesg.tc new file +7
-0+ nr_events=128 + for i in $(seq 1 $nr_events); do + echo $i > write_event + done + + sleep 1 + output=$(mktemp /tmp/remote_test.XXXXXX)
Quoted 8 lines
[Severity: Medium]
Will this leak a temporary file in/tmp/if the test aborts prematurely?The script is running with set -e, so if a validation step like
check_tracefails, it will immediately exit before reaching the rm $output command.Should this use $TMPDIR instead, since the ftracetest framework cleans it up automatically?
sigh... I have s/TMPDIR/tmp/ temporarily (pun unintended) to debug the script...
I'll fix that!
Quoted 4 lines
tools/testing/selftests/ftrace/test.d/remotes/dmesg.tc line 47
Quoted 3 lines
tools/testing/selftests/ftrace/test.d/remotes/dmesg.tc new file +2
-0+ dmesg | awk -v ts=$last_ts ' + /^[ *[0-9]+\.[0-9]+\]/ {
Quoted 8 lines
[Severity: Low]
Does this regular expression match the bracket as intended?By failing to escape the first bracket (^[), this creates a character class that matches spaces, asterisks, brackets, or digits instead of a literal opening bracket.
Could this be properly escaped as /^\[ *[0-9]+\.[0-9]+\]/ ?
Sure, I can escape "["
Signature · 1 line
Vincent
Quoted 32 lines
Quoted 4 lines
tools/testing/selftests/ftrace/test.d/remotes/dmesg.tc new file +3
-0+ t=$0; sub(/^[[ \t]*/, "", t); sub(/].*/, "", t); + if (t+0 > ts+0) print $0 + }' | grep "selftest id=" | sed 's/^[^]]*] //'> $output[ ... ]
tools/testing/selftests/ftrace/test.d/remotes/functions setup_remote() · line 11
Quoted 9 lines
tools/testing/selftests/ftrace/test.d/remotes/functions +1
-0@@ -8,6 +8,7 @@ setup_remote() cd remotes/$name/ echo 0 > tracing_on + echo 0 > dmesg[Severity: Medium]
Does this unconditional write break the test suite on older kernels?This setup function is used by all remote selftests. If the dmesg file does not exist, this echo will fail. Since the test scripts operate with set -e, this failure will cause the entire suite to crash on unsupported targets.
Could we check for the file's existence before writing to it?
Quoted 3 lines
tools/testing/selftests/ftrace/test.d/remotes/functions +0
-0clear_trace echo 7 > buffer_size_kb echo 0 > events/enableSignature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
Vincent Donnefort
maintainerPATCH v5 8/18 tracing/remotes: selftests: Prefix hypervisor folder
In reply to Vincent DonnefortRename the hypervisor tests directory to "00hypervisor" to ensure they run first. This prevents the hypervisor tests from being interleaved with other remote tests which is confusing.
tools/testing/selftests/ftrace/test.d/remotes/00hypervisor/buffer_size.tc renamed from tools/testing/selftests/ftrace/test.d/remotes/hypervisor/buffer_size.tc +0 -0
tools/testing/selftests/ftrace/test.d/remotes/00hypervisor/dmesg.tc renamed from tools/testing/selftests/ftrace/test.d/remotes/hypervisor/dmesg.tc +0 -0
tools/testing/selftests/ftrace/test.d/remotes/00hypervisor/hotplug.tc renamed from tools/testing/selftests/ftrace/test.d/remotes/hypervisor/hotplug.tc +0 -0
tools/testing/selftests/ftrace/test.d/remotes/00hypervisor/reset.tc renamed from tools/testing/selftests/ftrace/test.d/remotes/hypervisor/reset.tc +0 -0
tools/testing/selftests/ftrace/test.d/remotes/00hypervisor/trace.tc renamed from tools/testing/selftests/ftrace/test.d/remotes/hypervisor/trace.tc +0 -0
tools/testing/selftests/ftrace/test.d/remotes/00hypervisor/trace_pipe.tc renamed from tools/testing/selftests/ftrace/test.d/remotes/hypervisor/trace_pipe.tc +0 -0
tools/testing/selftests/ftrace/test.d/remotes/00hypervisor/unloading.tc renamed from tools/testing/selftests/ftrace/test.d/remotes/hypervisor/unloading.tc +0 -0
Signature · 1 line
2.55.0.691.gc56d675ccc-goog
[email protected]
AI reviewRe: [PATCH v5 08/18] tracing/remotes: selftests: Prefix hypervisor folder
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] The dmesg forwarder will stall indefinitely if pr_info() returns 0. --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The dmesg forwarder will stall indefinitely if pr_info() returns 0. --
--- Patch [8]: [PATCH v5 08/18] tracing/remotes: selftests: Prefix hypervisor folder --- commit 72aa88bcfffb90718532218e7344002c71fad7d0
Author: Vincent Donnefort <[email protected]>
tracing/remotes: selftests: Prefix hypervisor folder
Rename the hypervisor tests directory to "00hypervisor" to ensure they run first. This prevents the hypervisor tests from being interleaved with other remote tests which is confusing.
Quoted 4 lines
kernel/trace/trace_remote.c +0
-0
[ ... ]
Quoted 13 lines
kernel/trace/trace_remote.c +9
-0@@ -795,26 +908,105 @@ static const struct file_operations trace_fops = { .release = trace_release, }; +static bool trace_remote_do_dmesg (struct trace_remote *remote) +{ +struct trace_remote_iterator *iter =smp_load_acquire (&remote->dmesg); +unsigned int max_events =1000 ; + +if (!iter) +return false; + +trace_remote_iter_read_start (iter);
kernel/trace/trace_remote.c static const struct file_operations trace_fops = { · line 929
Quoted 10 lines
kernel/trace/trace_remote.c +10
-0+ +while (trace_remote_iter_read_event (iter)) { +trace_seq_init (&iter->seq); + +trace_remote_iter_print_event (iter); +trace_seq_putc (&iter->seq,0 ); +if (!pr_info ("%s" ,seq_buf_str (&iter->seq.seq))) +break ; + +trace_remote_iter_move (iter);
[Severity: Medium]
Will this loop stall indefinitely if CONFIG_PRINTK is disabled?
When CONFIG_PRINTK is disabled, the pr_info() macro expands to an inline function that unconditionally returns 0.
If that happens, the loop will break before calling trace_remote_iter_move().
Because the iterator is never advanced, the next time the polling work runs, won't it read the exact same event and break again, creating an infinite polling loop without making any progress?
Quoted 9 lines
kernel/trace/trace_remote.c +9
-0+ +if (!(--max_events)) +break ; + } + +trace_remote_iter_read_finished (iter); + +return !max_events; +}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
Vincent Donnefort
maintainerPATCH v5 9/18 ring-buffer: Use irqsave for the reader lock in ring_buffer_poll_remote
In reply to Vincent DonnefortCalling rb_wakeups with the reader lock held but interrupts enabled can lead to a deadlock: the irq_work might run on the same CPU, but will block when acquiring that very same reader spinlock.
First, rb_wakeups does not even need to be called under the reader lock. Move the function outside of the reader lock scope.
Second, the reader lock must be acquired with IRQs disabled anyway. Use the irqsave variant of the spinlock.
kernel/trace/ring_buffer.c +7 -3
@@ -6803,13 +6803,17 @@ int ring_buffer_poll_remote(struct trace_buffer *buffer, int cpu)struct ring_buffer_per_cpu *cpu_buffer;if (cpu !=RING_BUFFER_ALL_CPUS ) { +bool wakeup; +if (!cpumask_test_cpu (cpu, buffer->cpumask))return -EINVAL ; cpu_buffer = buffer->buffers[cpu]; -guard (raw_spinlock)(&cpu_buffer->reader_lock); -if (rb_read_remote_meta_page (cpu_buffer)) +scoped_guard (raw_spinlock_irqsave, &cpu_buffer->reader_lock) + wakeup = rb_read_remote_meta_page(cpu_buffer); + +if (wakeup)rb_wakeups (buffer, cpu_buffer);return 0 ;
@@ -6824,7 +6828,7 @@ int ring_buffer_poll_remote(struct trace_buffer *buffer, int cpu)for_each_buffer_cpu (buffer, cpu) { cpu_buffer = buffer->buffers[cpu]; -guard (raw_spinlock)(&cpu_buffer->reader_lock); +guard (raw_spinlock_irqsave)(&cpu_buffer->reader_lock);rb_read_remote_meta_page (cpu_buffer); }
Signature · 1 line
2.55.0.691.gc56d675ccc-goog
Vincent Donnefort
maintainerPATCH v5 10/18 ring-buffer: Use panic-friendly locking in ring_buffer_iter interface
In reply to Vincent DonnefortIn preparation for allowing trace_remote to dump the buffer on panic, make the non-consuming iterator functions panic-friendly.
kernel/trace/ring_buffer.c +18 -6
@@ -5605,6 +5605,9 @@ static void rb_iter_reset(struct ring_buffer_iter *iter) } } +static inline bool rb_reader_lock (struct ring_buffer_per_cpu *cpu_buffer); +static inline void rb_reader_unlock (struct ring_buffer_per_cpu *cpu_buffer,bool locked); +/** * ring_buffer_iter_reset - reset an iterator * @iter: The iterator to reset
@@ -5616,15 +5619,18 @@ void ring_buffer_iter_reset(struct ring_buffer_iter *iter) {struct ring_buffer_per_cpu *cpu_buffer;unsigned long flags; +bool dolock;if (!iter)return ; cpu_buffer = iter->cpu_buffer; -raw_spin_lock_irqsave (&cpu_buffer->reader_lock, flags); +local_irq_save (flags); + dolock =rb_reader_lock (cpu_buffer);rb_iter_reset (iter); -raw_spin_unlock_irqrestore (&cpu_buffer->reader_lock, flags); +rb_reader_unlock (cpu_buffer, dolock); +local_irq_restore (flags); }EXPORT_SYMBOL_GPL (ring_buffer_iter_reset);
@@ -6302,11 +6308,14 @@ ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts)struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;struct ring_buffer_event *event;unsigned long flags; +bool dolock; again: -raw_spin_lock_irqsave (&cpu_buffer->reader_lock, flags); +local_irq_save (flags); + dolock =rb_reader_lock (cpu_buffer); event =rb_iter_peek (iter, ts); -raw_spin_unlock_irqrestore (&cpu_buffer->reader_lock, flags); +rb_reader_unlock (cpu_buffer, dolock); +local_irq_restore (flags);if (event && event->type_len ==RINGBUF_TYPE_PADDING ) goto again;
@@ -6444,12 +6453,15 @@ void ring_buffer_iter_advance(struct ring_buffer_iter *iter) {struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer;unsigned long flags; +bool dolock; -raw_spin_lock_irqsave (&cpu_buffer->reader_lock, flags); +local_irq_save (flags); + dolock =rb_reader_lock (cpu_buffer); iter->missed_events =0 ;rb_advance_iter (iter); -raw_spin_unlock_irqrestore (&cpu_buffer->reader_lock, flags); +rb_reader_unlock (cpu_buffer, dolock); +local_irq_restore (flags); }EXPORT_SYMBOL_GPL (ring_buffer_iter_advance);
Signature · 1 line
2.55.0.691.gc56d675ccc-goog
[email protected]
AI reviewRe: [PATCH v5 10/18] ring-buffer: Use panic-friendly locking in ring_buffer_iter interface
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] rb_reader_lock() lacks an oops_in_progress check, leading to deadlocks during non-NMI panic dumps. --
--- Patch [10]: [PATCH v5 10/18] ring-buffer: Use panic-friendly locking in ring_buffer_iter interface --- commit 15fbd37892d8549b59334542a9c228e531aa61b5
Author: Vincent Donnefort <[email protected]>
ring-buffer: Use panic-friendly locking in ring_buffer_iter interface
This patch replaces raw_spin_lock_irqsave() with rb_reader_lock() in the iterator functions. The goal is to make the iterators panic-friendly so trace_remote can dump the buffer on panic without deadlocking.
Quoted 4 lines
kernel/trace/ring_buffer.c +0
-0
[ ... ]
Quoted 4 lines
kernel/trace/ring_buffer.c +0
-0@@ -5616,15 +5619,18 @@ void ring_buffer_iter_reset(struct ring_buffer_iter *iter) {struct ring_buffer_per_cpu *cpu_buffer;unsigned long flags;
kernel/trace/ring_buffer.c void ring_buffer_iter_reset(struct ring_buffer_iter *iter) · line 5630
Quoted 10 lines
kernel/trace/ring_buffer.c +3
-1+bool dolock;if (!iter)return ; cpu_buffer = iter->cpu_buffer; -raw_spin_lock_irqsave (&cpu_buffer->reader_lock, flags); +local_irq_save (flags); + dolock =rb_reader_lock (cpu_buffer);
[Severity: High]
This is a pre-existing issue, but does rb_reader_lock() lack an oops_in_progress check?
If a panic occurs in a normal (non-NMI) context, in_nmi() evaluates to false in rb_reader_lock():
static inline bool rb_reader_lock(struct ring_buffer_per_cpu *cpu_buffer)
{
if (likely(!in_nmi())) {
raw_spin_lock(&cpu_buffer->reader_lock);
return true;
}During a panic, smp_send_stop() will forcefully halt other CPUs. If one of those stopped CPUs was holding cpu_buffer->reader_lock, the panicking CPU will spin forever in raw_spin_lock().
Since the goal here is to make these iterators panic-friendly, does rb_reader_lock() also need to bypass the lock (e.g. check oops_in_progress) to avoid deadlocks during non-NMI panics?
Quoted 5 lines
kernel/trace/ring_buffer.c +2
-1rb_iter_reset (iter); -raw_spin_unlock_irqrestore (&cpu_buffer->reader_lock, flags); +rb_reader_unlock (cpu_buffer, dolock); +local_irq_restore (flags); }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=10
Vincent Donnefort
maintainerPATCH v5 11/18 ring-buffer: Add ring_buffer_read_remote_meta_page()
In reply to Vincent DonnefortIn preparation for the introduction of a panic handler for trace remotes, add ring_buffer_read_remote_meta_page(). This is similar to ring_buffer_poll_remote, but it doesn't try to wake up readers and, in the !RING_BUFFER_ALL_CPUS case, uses panic-friendly locks.
While at it, update trace_remote_has_cpu() to use this new function instead of ring_buffer_poll_remote(), avoiding unnecessary wakeups when verifying if a CPU buffer is active.
include/linux/ring_buffer.h +1 -0
@@ -299,6 +299,7 @@ struct ring_buffer_remote {void *priv; }; +int ring_buffer_read_remote_meta_page (struct trace_buffer *buffer,int cpu);int ring_buffer_poll_remote (struct trace_buffer *buffer,int cpu); struct trace_buffer *
kernel/trace/ring_buffer.c +41 -0
47 lines not on this page · raw message
kernel/trace/trace_remote.c +1 -1
@@ -350,7 +350,7 @@ static bool trace_remote_has_cpu(struct trace_remote *remote, int cpu)if (cpu ==RING_BUFFER_ALL_CPUS )return true; -return ring_buffer_poll_remote (remote->trace_buffer, cpu) ==0 ; +return ring_buffer_read_remote_meta_page (remote->trace_buffer, cpu) ==0 ; }static void __free_ring_buffer_iter (struct trace_remote_iterator *iter,int cpu)
Signature · 1 line
2.55.0.691.gc56d675ccc-goog
Vincent Donnefort
maintainerPATCH v5 12/18 ring-buffer: Add kerneldoc for ring_buffer_poll_remote
In reply to Vincent DonnefortDocument ring_buffer_poll_remote().
kernel/trace/ring_buffer.c +11 -0
@@ -6851,6 +6851,17 @@ int ring_buffer_read_remote_meta_page(struct trace_buffer *buffer, int cpu)return 0 ; } +/** +* ring_buffer_poll_remote - poll a remote ring buffer for new data +* @buffer: The ring buffer +* @cpu: The CPU buffer to poll (or RING_BUFFER_ALL_CPUS) +* +* This function polls the specified remote CPU buffer (or all of them) +* by reading its meta page to update the local reader's view. If new +* entries are detected, it triggers wakeups for any waiting readers. +* Returns: +* 0 on success, or -EINVAL if the CPU is not in the buffer's cpumask. +*/ int ring_buffer_poll_remote (struct trace_buffer *buffer,int cpu) {struct ring_buffer_per_cpu *cpu_buffer;
Signature · 1 line
2.55.0.691.gc56d675ccc-goog
Vincent Donnefort
maintainerPATCH v5 13/18 tracing/remotes: Add dump_on_panic tracefs file
In reply to Vincent DonnefortWhen enabled, dump_on_panic will dump the content of the trace remote buffer if the system panics.
kernel/trace/trace_remote.c +191 -40
@@ -7,6 +7,7 @@#include <linux/kstrtox.h> #include <linux/lockdep.h> #include <linux/mutex.h> +#include <linux/panic_notifier.h> #include <linux/tracefs.h> #include <linux/trace_remote.h> #include <linux/trace_seq.h>
@@ -22,6 +23,7 @@ enum tri_type {TRI_CONSUMING ,TRI_NONCONSUMING ,TRI_DMESG , + TRI_PANIC, }; struct trace_remote_iterator {
@@ -59,6 +61,9 @@ struct trace_remote {unsigned int poll_cnt;unsigned int poll_ms;bool tracing_on; +bool panic_on; +struct notifier_block panic_notifier; +struct trace_remote_iterator *panic_iter;struct delayed_work poll_work; };
@@ -71,10 +76,15 @@ static bool trace_remote_loaded(struct trace_remote *remote)return !!remote->trace_buffer; } +static void trace_remote_unload (struct trace_remote *remote); +static int trace_remote_panic_load (struct trace_remote *remote); +static void trace_remote_panic_unload (struct trace_remote *remote); +static int trace_remote_load (struct trace_remote *remote) {struct ring_buffer_remote *rb_remote = &remote->rb_remote;struct trace_buffer_desc *desc; +int ret =0 ;lockdep_assert_held (&remote->lock);
@@ -89,15 +99,28 @@ static int trace_remote_load(struct trace_remote *remote) rb_remote->swap_reader_page = remote->cbs->swap_reader_page; rb_remote->priv = remote->priv; rb_remote->reset = remote->cbs->reset; + remote->trace_buffer_desc = desc; remote->trace_buffer =ring_buffer_alloc_remote (rb_remote);if (!remote->trace_buffer) { remote->cbs->unload_trace_buffer (desc, remote->priv);return -ENOMEM ; } - remote->trace_buffer_desc = desc; +if (remote->panic_on) { + ret =trace_remote_panic_load (remote); +if (ret) +trace_remote_unload (remote); + } -return 0 ; +return ret; +} + +static void trace_remote_unload (struct trace_remote *remote) +{ +trace_remote_panic_unload (remote); +ring_buffer_free (remote->trace_buffer); + remote->trace_buffer =NULL ; + remote->cbs->unload_trace_buffer (remote->trace_buffer_desc, remote->priv); }static void trace_remote_try_unload (struct trace_remote *remote)
@@ -115,9 +138,7 @@ static void trace_remote_try_unload(struct trace_remote *remote)if (!ring_buffer_empty (remote->trace_buffer))return ; -ring_buffer_free (remote->trace_buffer); - remote->trace_buffer =NULL ; - remote->cbs->unload_trace_buffer (remote->trace_buffer_desc, remote->priv); +trace_remote_unload (remote); }static int trace_remote_enable_tracing (struct trace_remote *remote)
@@ -434,58 +455,68 @@ static void trace_remote_dec_poll(struct trace_remote *remote)static struct trace_remote_iterator *trace_remote_iter (struct trace_remote *remote,int cpu,enum tri_type type) { -struct trace_remote_iterator *iter =NULL ; + struct trace_remote_iterator *iter __free(kfree) = kzalloc_obj(*iter);int ret;lockdep_assert_held (&remote->lock); -if (type ==TRI_NONCONSUMING && !trace_remote_loaded (remote)) -return NULL ; +if (!iter) +return ERR_PTR (-ENOMEM ); - ret =trace_remote_get (remote, cpu); -if (ret) -return ERR_PTR (ret); +switch (type) { +case TRI_NONCONSUMING : +if (!trace_remote_loaded (remote)) +return NULL ; + fallthrough; +case TRI_CONSUMING : +case TRI_DMESG : + ret =trace_remote_get (remote, cpu); +if (ret) +return ERR_PTR (ret); +break ; +case TRI_PANIC : +break ; + }if (!trace_remote_has_cpu (remote, cpu)) { ret = -ENODEV ; goto err; } - iter =kzalloc_obj (*iter); -if (iter) { - iter->remote = remote; - iter->cpu = cpu; - iter->type = type; -trace_seq_init (&iter->seq); + iter->remote = remote; + iter->cpu = cpu; + iter->type = type; +trace_seq_init (&iter->seq); -switch (type) { -case TRI_DMESG : -/* only one dmesg iter allowed */ -if (WARN_ON_ONCE (remote->dmesg)) { - ret = -EBUSY ; -break ; - } -smp_store_release (&remote->dmesg, iter); - fallthrough; -case TRI_CONSUMING : -trace_remote_inc_poll (remote); -break ; -case TRI_NONCONSUMING : - ret =__alloc_ring_buffer_iter (iter, cpu); -break ; +switch (type) { +case TRI_DMESG : +/* only one dmesg iter allowed */ +if (WARN_ON_ONCE (remote->dmesg)) { + ret = -EBUSY ; + goto err; } - +smp_store_release (&remote->dmesg, iter); + fallthrough; +case TRI_CONSUMING : +trace_remote_inc_poll (remote); +break ; +case TRI_PANIC : +case TRI_NONCONSUMING : + ret =__alloc_ring_buffer_iter (iter, cpu);if (ret) goto err; - -return iter; +break ; } - ret = -ENOMEM ; + +return no_free_ptr (iter); err: -kfree (iter); -trace_remote_put (remote); - +switch (type) { +case TRI_PANIC : +break ; +default : +trace_remote_put (remote); + }return ERR_PTR (ret); }
@@ -508,14 +539,18 @@ static void trace_remote_iter_free(struct trace_remote_iterator *iter) fallthrough;case TRI_CONSUMING :trace_remote_dec_poll (remote); +trace_remote_put (remote);break ;case TRI_NONCONSUMING : +__free_ring_buffer_iter (iter, iter->cpu); +trace_remote_put (remote); +break ; +case TRI_PANIC :__free_ring_buffer_iter (iter, iter->cpu);break ; }kfree (iter); -trace_remote_put (remote); }static bool trace_remote_iter_is_consuming (struct trace_remote_iterator *iter)
@@ -989,6 +1024,117 @@ static int dmesg_show(struct seq_file *s, void *unused) }DEFINE_TRACE_REMOTE_ATTRIBUTE (dmesg); +static int trace_remote_panic_handler (struct notifier_block *self,unsigned long ev,void *v) +{ + struct trace_remote *remote = container_of(self, struct trace_remote, panic_notifier); +struct trace_remote_iterator *iter =smp_load_acquire (&remote->panic_iter); +int cpu; + +if (!iter) { +pr_warn ("Unexpected error: no panic iterator for the trace remote\n" ); +return NOTIFY_DONE ; + } + +for_each_possible_cpu (cpu) { +if (iter->rb_iters[cpu]) { +/* No RING_BUFFER_ALL_CPUS to avoid taking cpu_read_lock() */ +ring_buffer_read_remote_meta_page (remote->trace_buffer, cpu); +ring_buffer_iter_reset (iter->rb_iters[cpu]); + } + } + +while (trace_remote_iter_read_event (iter)) { +trace_seq_init (&iter->seq); + +trace_remote_iter_print_event (iter); +trace_seq_putc (&iter->seq,0 ); +pr_emerg ("%s" ,seq_buf_str (&iter->seq.seq)); + +trace_remote_iter_move (iter); + } + +return NOTIFY_DONE ; +} + +static int trace_remote_panic_load (struct trace_remote *remote) +{ +struct notifier_block *notifier = &remote->panic_notifier; +struct trace_remote_iterator *iter; + +lockdep_assert_held (&remote->lock); + +if (remote->panic_iter) +return 0 ; + + iter =trace_remote_iter (remote,RING_BUFFER_ALL_CPUS ,TRI_PANIC ); +if (IS_ERR (iter)) +return PTR_ERR (iter); + +smp_store_release (&remote->panic_iter, iter); + + notifier->notifier_call = trace_remote_panic_handler; + notifier->priority =INT_MAX -1 ; +atomic_notifier_chain_register (&panic_notifier_list, notifier); + +return 0 ; +} + +static void trace_remote_panic_unload (struct trace_remote *remote) +{ +struct trace_remote_iterator *iter = remote->panic_iter; + +lockdep_assert_held (&remote->lock); + +if (!iter) +return ; + +atomic_notifier_chain_unregister (&panic_notifier_list, &remote->panic_notifier); +smp_store_release (&remote->panic_iter,NULL ); +trace_remote_iter_free (iter); +} + +static ssize_t dump_on_panic_write (struct file *filp,const char __user *ubuf, +size_t cnt,loff_t *ppos) +{ +struct seq_file *seq = filp->private_data; +struct trace_remote *remote = seq->private; +bool enable; +int ret; + + ret =kstrtobool_from_user (ubuf, cnt, &enable); +if (ret) +return ret; + +guard (mutex)(&remote->lock); + +if (enable == remote->panic_on) +return cnt; + +if (trace_remote_loaded (remote)) { +if (enable) { + ret =trace_remote_panic_load (remote); +if (ret) +return ret; + }else { +trace_remote_panic_unload (remote); + } + } + + remote->panic_on = enable; + +return cnt; +} + +static int dump_on_panic_show (struct seq_file *s,void *unused) +{ +struct trace_remote *remote = s->private; + +seq_printf (s,"%d\n" , remote->panic_on); + +return 0 ; +} +DEFINE_TRACE_REMOTE_ATTRIBUTE (dump_on_panic); +static int trace_remote_init_tracefs (const char *name,struct trace_remote *remote) {struct dentry *remote_d, *percpu_d, *d;
@@ -1016,6 +1162,11 @@ static int trace_remote_init_tracefs(const char *name, struct trace_remote *remoif (!d) goto err; + d =trace_create_file ("dump_on_panic" ,TRACEFS_MODE_WRITE , remote_d, remote, + &dump_on_panic_fops); +if (!d) + goto err; + d =trace_create_file ("buffer_size_kb" ,TRACEFS_MODE_WRITE , remote_d, remote, &buffer_size_kb_fops);if (!d)
Signature · 1 line
2.55.0.691.gc56d675ccc-goog
[email protected]
AI reviewRe: [PATCH v5 13/18] tracing/remotes: Add dump_on_panic tracefs file
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] Deadlock in panic handler due to unconditionally acquiring raw spinlocks that may be held by…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] Deadlock in panic handler due to unconditionally acquiring raw spinlocks that may be held by stopped CPUs.
- [Low] Mixing goto-based error handling with scope-based __free() cleanup.
--
--- Patch [13]: [PATCH v5 13/18] tracing/remotes: Add dump_on_panic tracefs file --- commit 0e10ff19e3851c2a033a99ee96a39ed51f4786e7
Author: Vincent Donnefort <[email protected]>
tracing/remotes: Add dump_on_panic tracefs file
When enabled, dump_on_panic will dump the content of the trace remote buffer if the system panics.
Quoted 4 lines
kernel/trace/trace_remote.c +0
-0
[ ... ]
Quoted 14 lines
kernel/trace/trace_remote.c +3
-3@@ -434,58 +455,68 @@ static void trace_remote_dec_poll(struct trace_remote *remote)static struct trace_remote_iterator *trace_remote_iter (struct trace_remote *remote,int cpu,enum tri_type type) { -struct trace_remote_iterator *iter =NULL ; + struct trace_remote_iterator *iter __free(kfree) = kzalloc_obj(*iter);int ret;lockdep_assert_held (&remote->lock); -if (type ==TRI_NONCONSUMING && !trace_remote_loaded (remote)) -return NULL ; +if (!iter) +return ERR_PTR (-ENOMEM );
[ ... ]
kernel/trace/trace_remote.c static void trace_remote_dec_poll(struct trace_remote *remote) · line 483
Quoted 3 lines
kernel/trace/trace_remote.c +0
-0if (!trace_remote_has_cpu (remote, cpu)) { ret = -ENODEV ; goto err;
^^^^^^^^
[Severity: Low]
Does this mix goto-based error handling with scope-based __free() cleanup?
The iter variable was declared with __free(kfree) at the top of the function, but this goto jumps to an error label. Mixing goto labels and cleanup scope decorators in the same function can create confusing ownership semantics and violates cleanup guidelines.
Could this be converted entirely to scope-based cleanup, or avoid using __free() here?
[ ... ]
Quoted 10 lines
kernel/trace/trace_remote.c +6
-0@@ -989,6 +1024,117 @@ static int dmesg_show(struct seq_file *s, void *unused) }DEFINE_TRACE_REMOTE_ATTRIBUTE (dmesg); +static int trace_remote_panic_handler (struct notifier_block *self,unsigned long ev,void *v) +{ + struct trace_remote *remote = container_of(self, struct trace_remote, panic_notifier); +struct trace_remote_iterator *iter =smp_load_acquire (&remote->panic_iter); +int cpu; +
kernel/trace/trace_remote.c static int dmesg_show(struct seq_file *s, void *unused) · line 1042
Quoted 10 lines
kernel/trace/trace_remote.c +10
-0+if (!iter) { +pr_warn ("Unexpected error: no panic iterator for the trace remote\n" ); +return NOTIFY_DONE ; + } + +for_each_possible_cpu (cpu) { +if (iter->rb_iters[cpu]) { +/* No RING_BUFFER_ALL_CPUS to avoid taking cpu_read_lock() */ +ring_buffer_read_remote_meta_page (remote->trace_buffer, cpu); +ring_buffer_iter_reset (iter->rb_iters[cpu]);
[Severity: High]
Can these functions deadlock the panic handler?
During a panic, smp_send_stop() halts all other CPUs. If a remote CPU was reading the trace buffer and holding the reader_lock when it was halted, won't this spin forever?
ring_buffer_read_remote_meta_page() and ring_buffer_iter_reset() both call rb_reader_lock(), which unconditionally takes a raw_spin_lock() when not in NMI context:
kernel/trace/ring_buffer.c:rb_reader_lock() {
...
if (likely(!in_nmi())) {
raw_spin_lock(&cpu_buffer->reader_lock);
return true;
}
...
}Because standard panics do not execute in NMI context, in_nmi() returns false here, leading to a hang that prevents kdump or reboot.
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=13
Vincent Donnefort
maintainerPATCH v5 14/18 tracing/remotes: selftests: Add a test for the dump_on_panic tracefs file
In reply to Vincent DonnefortExercise the newly introduced dump_on_panic tracefs file that turns on or off the trace remote buffer dump on system panic.
tools/testing/selftests/ftrace/test.d/remotes/00hypervisor/dump_on_panic.tc new file +11 -0
@@ -0,0 +1,11 @@ +#!/bin/sh +# SPDX-License-Identifier: GPL-2.0 +# description: Test hypervisor trace dump_on_panic +# requires: remotes/hypervisor/write_event + +SOURCE_REMOTE_TEST=1 +. $TEST_DIR/remotes/dump_on_panic.tc + +set -e +setup_remote "hypervisor" +test_dump_on_panic
tools/testing/selftests/ftrace/test.d/remotes/dump_on_panic.tc new file +51 -0
51 lines not on this page · raw message
tools/testing/selftests/ftrace/test.d/remotes/functions +1 -0
@@ -9,6 +9,7 @@ setup_remote() cd remotes/$name/ echo 0 > tracing_on echo 0 > dmesg + echo 0 > dump_on_panic clear_trace echo 7 > buffer_size_kb echo 0 > events/enable
Signature · 1 line
2.55.0.691.gc56d675ccc-goog
[email protected]
AI reviewRe: [PATCH v5 14/18] tracing/remotes: selftests: Add a test for the dump_on_panic tracefs file
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [Medium] The test contains an unconditional early return, leaving dead code intended to manually trigger…
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [Medium] The test contains an unconditional early return, leaving dead code intended to manually trigger a kernel panic, which fails to validate the feature and creates a dangerous booby trap.
- [Medium] Unconditional write to dump_on_panic in the shared setup_remote() function will break all ftrace remote selftests on older kernels.
- [Medium] The new test fails instead of gracefully skipping on kernels that do not support the dump_on_panic feature.
--
--- Patch [14]: [PATCH v5 14/18] tracing/remotes: selftests: Add a test for the dump_on_panic tracefs file --- commit 77956bd02c91415a58b7ae353d502fbc7538fb12
Author: Vincent Donnefort <[email protected]>
tracing/remotes: selftests: Add a test for the dump_on_panic tracefs file
This patch adds a selftest for the dump_on_panic tracefs file. It creates a new test script and updates the remote setup functions to configure the parameter.
Quoted 9 lines
tools/testing/selftests/ftrace/test.d/remotes/dump_on_panic.tc new file +3
-0@@ -0,0 +1,51 @@ +#!/bin/sh +# SPDX-License-Identifier: GPL-2.0 +# description: Test trace remote dump_on_panic
tools/testing/selftests/ftrace/test.d/remotes/dump_on_panic.tc line 13
Quoted 10 lines
tools/testing/selftests/ftrace/test.d/remotes/dump_on_panic.tc new file +10
-0+# requires: remotes/test + +. $TEST_DIR/remotes/functions + +test_dump_on_panic() +{ + # + # Toggle when the buffer is loaded + # + assert_loaded
[Severity: Medium]
Since this test specifically targets the dump_on_panic file, should there be an existence check here (such as [ -f dump_on_panic ] || exit_unsupported) to prevent false negatives?
Without this check, the test fails instead of gracefully skipping on kernels that do not support the dump_on_panic feature.
Quoted 2 lines
tools/testing/selftests/ftrace/test.d/remotes/dump_on_panic.tc new file +2
-0+ echo 1 > dump_on_panic + echo 0 > dump_on_panic
[ ... ]
Quoted 5 lines
tools/testing/selftests/ftrace/test.d/remotes/dump_on_panic.tc new file +5
-0+ echo 1 > dump_on_panic + echo 1 > tracing_on + echo 0 > tracing_on + + # REMOVE ME FOR A PROPER OOPS TEST
tools/testing/selftests/ftrace/test.d/remotes/dump_on_panic.tc line 45
Quoted 10 lines
tools/testing/selftests/ftrace/test.d/remotes/dump_on_panic.tc new file +10
-0+ return + + echo 1 > tracing_on + + for i in $(seq 1 32); do + echo $i > write_event + done + + echo c > /proc/sysrq-trigger +}
[Severity: Medium]
Is this early return intentional?
This skips the actual panic-triggering code, leaving the dump_on_panic feature untested. Furthermore, leaving the sysrq-trigger command behind an early return could be dangerous if someone removes the return without realizing it will crash the host machine during a CI run.
Quoted 11 lines
tools/testing/selftests/ftrace/test.d/remotes/dump_on_panic.tc new file +6
-0+ +if [ -z "$SOURCE_REMOTE_TEST" ]; then + set -e + setup_remote_test + test_dump_on_panic +fitools/testing/selftests/ftrace/test.d/remotes/functions +0
-0
tools/testing/selftests/ftrace/test.d/remotes/functions setup_remote() · line 12
Quoted 4 lines
tools/testing/selftests/ftrace/test.d/remotes/functions +1
-0@@ -9,6 +9,7 @@ setup_remote() cd remotes/$name/ echo 0 > tracing_on echo 0 > dmesg + echo 0 > dump_on_panic
[Severity: Medium]
Could this unconditional write cause failures on older kernels?
Because setup_remote() is a shared setup function, and test scripts execute with set -e (errexit), if this is run against an older kernel lacking the dump_on_panic file, the shell will immediately abort. This would break all ftrace remote selftests on those kernels.
Quoted 3 lines
tools/testing/selftests/ftrace/test.d/remotes/functions +0
-0clear_trace echo 7 > buffer_size_kb echo 0 > events/enable
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=14
Vincent Donnefort
maintainerPATCH v5 15/18 tracing/remotes: Add poll_ms tracefs file
In reply to Vincent DonnefortAdd a tracefs file to configure the trace remote polling period. Keep the default value to 100ms.
kernel/trace/trace_remote.c +38 -0
@@ -1135,6 +1135,40 @@ static int dump_on_panic_show(struct seq_file *s, void *unused) }DEFINE_TRACE_REMOTE_ATTRIBUTE (dump_on_panic); +static ssize_t poll_ms_write (struct file *filp,const char __user *ubuf,size_t cnt,loff_t *ppos) +{ +struct seq_file *seq = filp->private_data; +struct trace_remote *remote = seq->private; +unsigned int val; +int ret; + + ret =kstrtouint_from_user (ubuf, cnt,10 , &val); +if (ret) +return ret; + +if (!val) +return -EINVAL ; + +guard (mutex)(&remote->lock); + +if (val < remote->poll_ms && remote->poll_cnt) +mod_delayed_work (system_percpu_wq, &remote->poll_work,msecs_to_jiffies (val)); + + remote->poll_ms = val; + +return cnt; +} + +static int poll_ms_show (struct seq_file *s,void *unused) +{ +struct trace_remote *remote = s->private; + +seq_printf (s,"%u\n" , remote->poll_ms); + +return 0 ; +} +DEFINE_TRACE_REMOTE_ATTRIBUTE (poll_ms); +static int trace_remote_init_tracefs (const char *name,struct trace_remote *remote) {struct dentry *remote_d, *percpu_d, *d;
@@ -1167,6 +1201,10 @@ static int trace_remote_init_tracefs(const char *name, struct trace_remote *remoif (!d) goto err; + d =trace_create_file ("poll_ms" ,TRACEFS_MODE_WRITE , remote_d, remote, &poll_ms_fops); +if (!d) + goto err; + d =trace_create_file ("buffer_size_kb" ,TRACEFS_MODE_WRITE , remote_d, remote, &buffer_size_kb_fops);if (!d)
Signature · 1 line
2.55.0.691.gc56d675ccc-goog
[email protected]
AI reviewRe: [PATCH v5 15/18] tracing/remotes: Add poll_ms tracefs file
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Low] Data race on `remote->poll_ms` due to lockless reads. - [Low] Improper mixing of goto-based error…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Low] Data race on remote->poll_ms due to lockless reads.
- [Low] Improper mixing of goto-based error handling and scope-based cleanup (__free) in trace_remote_iter. --
--- Patch [15]: [PATCH v5 15/18] tracing/remotes: Add poll_ms tracefs file --- commit a661ee191c6d806ed0a716f67237aca890febe25
Author: Vincent Donnefort <[email protected]>
tracing/remotes: Add poll_ms tracefs file
Add a tracefs file to configure the trace remote polling period. Keep the default value to 100ms.
Quoted 27 lines
kernel/trace/trace_remote.c +19
-0@@ -1135,6 +1135,40 @@ static int dump_on_panic_show(struct seq_file *s, void *unused) }DEFINE_TRACE_REMOTE_ATTRIBUTE (dump_on_panic); +static ssize_t poll_ms_write (struct file *filp,const char __user *ubuf,size_t cnt,loff_t *ppos) +{ +struct seq_file *seq = filp->private_data; +struct trace_remote *remote = seq->private; +unsigned int val; +int ret; + + ret =kstrtouint_from_user (ubuf, cnt,10 , &val); +if (ret) +return ret; + +if (!val) +return -EINVAL ; + +guard (mutex)(&remote->lock); + +if (val < remote->poll_ms && remote->poll_cnt) +mod_delayed_work (system_percpu_wq, &remote->poll_work,msecs_to_jiffies (val)); +
kernel/trace/trace_remote.c static int dump_on_panic_show(struct seq_file *s, void *unused) · line 1166
Quoted 10 lines
kernel/trace/trace_remote.c +10
-0+ remote->poll_ms = val; + +return cnt; +} + +static int poll_ms_show (struct seq_file *s,void *unused) +{ +struct trace_remote *remote = s->private; + +seq_printf (s,"%u\n" , remote->poll_ms);
[Severity: Low]
Does this code introduce a data race on remote->poll_ms?
In poll_ms_write(), remote->poll_ms is modified while holding remote->lock. However, in poll_ms_show(), remote->poll_ms is read without holding remote->lock.
It appears this could also cause a data race with lockless reads in trace_remote_do_poll():
kernel/trace/trace_remote.c:trace_remote_do_poll() {
...
schedule_delayed_work(&remote->poll_work, yield ? 0 : msecs_to_jiffies(remote->poll_ms));
}Could this concurrent read and write cause a data race, potentially leading to torn reads or violating kernel concurrency expectations?
[ ... ]
Also, looking at trace_remote_iter() in kernel/trace/trace_remote.c:
kernel/trace/trace_remote.c:trace_remote_iter() {
struct trace_remote_iterator *iter __free(kfree) = kzalloc_obj(*iter);
...
if (WARN_ON_ONCE(remote->dmesg)) {
ret = -EBUSY;
goto err;
}
...
err:
switch (type) {
case TRI_PANIC:
break;
default:
trace_remote_put(remote);
}
return ERR_PTR(ret);
}[Severity: Low]
Does this code incorrectly mix goto-based error handling and scope-based cleanup (__free)?
The cleanup subsystem guidelines prohibit mixing goto-based error paths with __free() variable declarations in the same function. While it does not appear to cause a concrete resource leak or double-free in this specific instance, could this create confusing ownership semantics and risk future bugs during refactoring?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=15
Vincent Donnefort
maintainerRe: [PATCH v5 15/18] tracing/remotes: Add poll_ms tracefs file
Quoted 62 lines · On Wed, Aug 12, 2026 at 05:13:04PM +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Low] Data race onremote->poll_msdue to lockless reads.
- [Low] Improper mixing of goto-based error handling and scope-based cleanup (__free) intrace_remote_iter. ----- Patch [15]: [PATCH v5 15/18] tracing/remotes: Add
poll_mstracefs file --- commita661ee191c6d806ed0a716f67237aca890febe25
Author: Vincent Donnefort <[email protected]>tracing/remotes: Add
poll_mstracefs fileAdd a tracefs file to configure the trace remote polling period. Keep the default value to 100ms.
Quoted 27 lines
kernel/trace/trace_remote.c +19
-0@@ -1135,6 +1135,40 @@ static int dump_on_panic_show(struct seq_file *s, void *unused) }DEFINE_TRACE_REMOTE_ATTRIBUTE (dump_on_panic); +static ssize_t poll_ms_write (struct file *filp,const char __user *ubuf,size_t cnt,loff_t *ppos) +{ +struct seq_file *seq = filp->private_data; +struct trace_remote *remote = seq->private; +unsigned int val; +int ret; + + ret =kstrtouint_from_user (ubuf, cnt,10 , &val); +if (ret) +return ret; + +if (!val) +return -EINVAL ; + +guard (mutex)(&remote->lock); + +if (val < remote->poll_ms && remote->poll_cnt) +mod_delayed_work (system_percpu_wq, &remote->poll_work,msecs_to_jiffies (val)); +kernel/trace/trace_remote.c static int dump_on_panic_show(struct seq_file *s, void *unused) · line 1166
Quoted 10 lines
kernel/trace/trace_remote.c +10
-0+ remote->poll_ms = val; + +return cnt; +} + +static int poll_ms_show (struct seq_file *s,void *unused) +{ +struct trace_remote *remote = s->private; + +seq_printf (s,"%u\n" , remote->poll_ms);[Severity: Low]
Does this code introduce a data race onremote->poll_ms?In
poll_ms_write(),remote->poll_msis modified while holdingremote->lock. However, inpoll_ms_show(),remote->poll_msis read without holdingremote->lock.It appears this could also cause a data race with lockless reads in
trace_remote_do_poll():
Quoted 7 lines
kernel/trace/trace_remote.c:trace_remote_do_poll() { ... schedule_delayed_work(&remote->poll_work, yield ? 0 : msecs_to_jiffies(remote->poll_ms)); }Could this concurrent read and write cause a data race, potentially leading to torn reads or violating kernel concurrency expectations?
Fair enough... I'll WRITE_ONCE/READ_ONCE.
Signature · 1 line
Vincent
Quoted 35 lines
[ ... ]
Also, looking at
trace_remote_iter()inkernel/trace/trace_remote.c:kernel/trace/trace_remote.c:trace_remote_iter() { struct trace_remote_iterator *iter __free(kfree) = kzalloc_obj(*iter); ... if (WARN_ON_ONCE(remote->dmesg)) { ret = -EBUSY; goto err; } ... err: switch (type) { case TRI_PANIC: break; default: trace_remote_put(remote); } return ERR_PTR(ret); }[Severity: Low]
Does this code incorrectly mix goto-based error handling and scope-based cleanup (__free)?The cleanup subsystem guidelines prohibit mixing goto-based error paths with
__free()variable declarations in the same function. While it does not appear to cause a concrete resource leak or double-free in this specific instance, could this create confusing ownership semantics and risk future bugs during refactoring?Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=15
Vincent Donnefort
maintainerPATCH v5 16/18 tracing/remotes: Add trace_remote cmdline options
In reply to Vincent DonnefortFollowing the same format as trace_instance, add a cmdline to configure a trace remote on registration:
trace_remote=<remote>^<opt1>^<opt2>,<evt1>,<evt2>
Enabling events automatically turns on tracing.
kernel/trace/trace_remote.c +133 -0
@@ -13,6 +13,8 @@#include <linux/trace_seq.h> #include <linux/types.h> +#include <asm/setup.h> +#include "trace.h" #define TRACEFS_DIR "remotes"
@@ -1169,6 +1171,124 @@ static int poll_ms_show(struct seq_file *s, void *unused) }DEFINE_TRACE_REMOTE_ATTRIBUTE (poll_ms); +static char trace_remote_cmdline[COMMAND_LINE_SIZE ]; + +static int __init set_trace_remote_cmdline (char *str) +{ +trace_append_boot_param (trace_remote_cmdline, str,'\t' ,COMMAND_LINE_SIZE ); +return 1 ; +} +__setup ("trace_remote=" , set_trace_remote_cmdline); + +static void trace_remote_apply_cmdline_opts (struct trace_remote *remote,char *cmdline) +{ +bool dmesg_on = false; +char *opt; +int ret; + +while ((opt =strsep (&cmdline,"^" ))) { +if (!*opt) +continue ; + +if (!strcmp (opt,"dump_on_panic" )) { + remote->panic_on = true; + }else if (!strcmp (opt,"dmesg" )) { + dmesg_on = true; + }else if (!strncmp (opt,"buf_size=" ,9 )) { +/* buf_size can only be applied if the buffer is unloaded */ +if (!WARN_ON (trace_remote_loaded (remote))) + remote->trace_buffer_size =memparse (opt +9 ,NULL ); + }else if (!strncmp (opt,"poll=" ,5 )) { +unsigned int poll_ms; + +if (!kstrtouint (opt +5 ,10 , &poll_ms) && poll_ms >0 ) + remote->poll_ms = poll_ms; +else +pr_warn ("Invalid trace remote poll '%s'\n" , opt); + }else { +pr_warn ("Unknown trace remote option '%s'\n" , opt); + } + } + +if (dmesg_on) { + ret =trace_remote_enable_dmesg (remote, true); +if (ret) +pr_warn ("Failed to enable trace remote dmesg (%d)\n" , ret); + } +} + +static struct remote_event * +trace_remote_find_event_by_name (struct trace_remote *remote,const char *name); + +static int +trace_remote_enable_event (struct trace_remote *remote,struct remote_event *evt,bool enable); + +static void trace_remote_apply_cmdline_events (struct trace_remote *remote,char *cmdline) +{ +bool tracing_on = false; +char *token; +int ret; + +while ((token =strsep (&cmdline,"," ))) { +struct remote_event *evt; + +if (!*token) +continue ; + + evt =trace_remote_find_event_by_name (remote, token); +if (!evt) { +pr_warn ("trace remote event '%s' not found\n" , token); +continue ; + } + + ret =trace_remote_enable_event (remote, evt, true); +if (ret) +pr_warn ("Failed to enable trace remote event '%s' (%d)\n" , token, ret); +else + tracing_on = true; + } + +if (tracing_on) { + ret =trace_remote_enable_tracing (remote); +if (ret) +pr_warn ("Failed to enable trace remote tracing (%d)\n" , ret); + } +} + +static void trace_remote_apply_cmdline (const char *name,struct trace_remote *remote) +{ + char *cmdline __free(kfree) = NULL; +char *events_cmdline =NULL ; +char *opts_cmdline =NULL ; +char *curr, *next; + +if (!trace_remote_cmdline[0 ]) +return ; + + cmdline =kstrdup (trace_remote_cmdline,GFP_KERNEL ); +if (!cmdline) +return ; + + next = cmdline; +while ((curr =strsep (&next,"\t" ))) { +char *token =strsep (&curr,"," ); +char *rname =strsep (&token,"^" ); + +if (strcmp (rname, name)) +continue ; + + opts_cmdline = token; + events_cmdline = curr; + +guard (mutex)(&remote->lock); + +if (opts_cmdline) +trace_remote_apply_cmdline_opts (remote, opts_cmdline); +if (events_cmdline) +trace_remote_apply_cmdline_events (remote, events_cmdline); + } +} +static int trace_remote_init_tracefs (const char *name,struct trace_remote *remote) {struct dentry *remote_d, *percpu_d, *d;
@@ -1345,6 +1465,7 @@ int trace_remote_register(const char *name, struct trace_remote_callbacks *cbs, }list_add (&remote->node, &trace_remotes); +trace_remote_apply_cmdline (name, remote);retain_and_null_ptr (remote);return 0 ;
@@ -1828,3 +1949,15 @@ static struct remote_event *trace_remote_find_event(struct trace_remote *remote,return bsearch ((const void *)(unsigned long )id, remote->events, remote->nr_events,sizeof (*remote->events), __cmp_events); } + +static struct remote_event * +trace_remote_find_event_by_name (struct trace_remote *remote,const char *name) +{ +int i; + +for (i =0 ; i < remote->nr_events; i++) { +if (!strcmp (remote->events[i].name, name)) +return &remote->events[i]; + } +return NULL ; +}
Signature · 1 line
2.55.0.691.gc56d675ccc-goog
[email protected]
AI reviewRe: [PATCH v5 16/18] tracing/remotes: Add trace_remote cmdline options
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] User-controllable command-line arguments inappropriately trigger a WARN_ON(). --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] User-controllable command-line arguments inappropriately trigger a WARN_ON(). --
--- Patch [16]: [PATCH v5 16/18] tracing/remotes: Add trace_remote cmdline options --- commit 473129b2ec6ced6d4d23093ce86b83497f7202d2
Author: Vincent Donnefort <[email protected]>
tracing/remotes: Add trace_remote cmdline options
This commit adds a trace_remote= boot command-line option to configure a trace remote upon registration. It enables configuring various options like buffer size, polling interval, and activating specific tracing events.
Quoted 4 lines
kernel/trace/trace_remote.c +0
-0
[ ... ]
Quoted 1 line
kernel/trace/trace_remote.c +0
-0@@ -1169,6 +1171,124 @@ static int poll_ms_show(struct seq_file *s, void *unused)
[ ... ]
Quoted 2 lines
kernel/trace/trace_remote.c +2
-0+static void trace_remote_apply_cmdline_opts (struct trace_remote *remote,char *cmdline) +{
[ ... ]
kernel/trace/trace_remote.c static int poll_ms_show(struct seq_file *s, void *unused) · line 1200
Quoted 4 lines
kernel/trace/trace_remote.c +4
-0+ }else if (!strncmp (opt,"buf_size=" ,9 )) { +/* buf_size can only be applied if the buffer is unloaded */ +if (!WARN_ON (trace_remote_loaded (remote))) + remote->trace_buffer_size =memparse (opt +9 ,NULL );
[Severity: Medium]
Should this be a pr_warn() instead of a WARN_ON()?
If the bootloader passes multiple trace_remote= parameters, and an earlier parameter enables events (which loads the buffer) before a later parameter tries to set buf_size= for the same remote, this check will fail and emit a warning.
Because WARN_ON() is used here, a simple typo or misordered boot command line will trigger a splat and cause a kernel panic during boot on any system configured with panic_on_warn.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=16
Vincent Donnefort
maintainerRe: [PATCH v5 16/18] tracing/remotes: Add trace_remote cmdline options
Quoted 38 lines · On Wed, Aug 12, 2026 at 05:11:48PM +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] User-controllable command-line arguments inappropriately trigger aWARN_ON(). ----- Patch [16]: [PATCH v5 16/18] tracing/remotes: Add
trace_remotecmdline options --- commit473129b2ec6ced6d4d23093ce86b83497f7202d2
Author: Vincent Donnefort <[email protected]>tracing/remotes: Add
trace_remotecmdline optionsThis commit adds a trace_remote= boot command-line option to configure a trace remote upon registration. It enables configuring various options like buffer size, polling interval, and activating specific tracing events.
Quoted 4 lines
kernel/trace/trace_remote.c +0
-0[ ... ]
Quoted 1 line
kernel/trace/trace_remote.c +0
-0@@ -1169,6 +1171,124 @@ static int poll_ms_show(struct seq_file *s, void *unused)[ ... ]
Quoted 2 lines
kernel/trace/trace_remote.c +2
-0+static void trace_remote_apply_cmdline_opts (struct trace_remote *remote,char *cmdline) +{[ ... ]
kernel/trace/trace_remote.c static int poll_ms_show(struct seq_file *s, void *unused) · line 1200
Quoted 4 lines
kernel/trace/trace_remote.c +4
-0+ }else if (!strncmp (opt,"buf_size=" ,9 )) { +/* buf_size can only be applied if the buffer is unloaded */ +if (!WARN_ON (trace_remote_loaded (remote))) + remote->trace_buffer_size =memparse (opt +9 ,NULL );[Severity: Medium]
Should this be apr_warn()instead of aWARN_ON()?
Quoted 11 lines
If the bootloader passes multiple trace_remote= parameters, and an earlier parameter enables events (which loads the buffer) before a later parameter tries to set buf_size= for the same remote, this check will fail and emit a warning.
Because
WARN_ON()is used here, a simple typo or misordered boot command line will trigger a splat and cause a kernel panic during boot on any system configured withpanic_on_warn.Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=16
pr_warn() sounds good
Signature · 1 line
Vincent
Vincent Donnefort
maintainerPATCH v5 17/18 Documentation: tracing/remotes: Add detailed tracefs layout
In reply to Vincent DonnefortAdd a description for each tracefs file available in a trace remote instance.
Documentation/trace/remotes.rst +62 -4
@@ -19,8 +19,8 @@ for which the host kernel can see and expose to user space. Register a remote ================= -A remote must provide a set of callbacks `struct trace_remote_callbacks` whom -description can be found below. Those callbacks allows Tracefs to enable and +A remote must provide a set of callbacks `struct trace_remote_callbacks` whose +description can be found below. Those callbacks allow Tracefs to enable and disable tracing and events, to load and unload a tracing buffer (a set of ring-buffers) and to swap a reader page with the head page, which enables consuming reading.
@@ -28,8 +28,66 @@ consuming reading. .. kernel-doc:: include/linux/trace_remote.h Once registered, an instance will appear for this remote in the Tracefs -directory **remotes/**. Buffers can then be read using the usual Tracefs files -**trace_pipe** and **trace**. +directory **remotes/**. The files within this directory allow configuring +and reading the remote buffer (see `The File System` below). + +The File System +=============== +A remote tracing instance is represented by a directory in Tracefs under +**remotes/**. The layout and files within it are very similar to standard ftrace +instances. Inside the remote directory, the following files and directories are +available: + + tracing_on + This file allows enabling or disabling the remote tracing. + + buffer_size_kb + This file displays and allows changing the size of the per-CPU ring + buffers used by the remote. It also shows if the buffer is **loaded** or + **unloaded**. To change the size, the remote buffers must be unloaded + first. Remote buffers are automatically unloaded when **tracing_on** is + off, no one is reading the buffer (either by accessing **trace_pipe** or + when **dmesg** is on) and no events remain in the buffer. + + trace + Display the human-readable content of the remote buffers. Reading this + file is non-consuming. Writing to this file clears the ring buffers. + + trace_pipe + Similar to **trace** but reading it consumes the events from the ring + buffers (consuming read). It blocks if there are no new events. + + dmesg + When enabled, all events from the remote are redirected to the kernel + dmesg. This is similar to the **tp_printk** option for in-kernel events. + It counts as a reader of the remote buffers and prevents unloading. + + dump_on_panic + When enabled, the remote tracing buffer is dumped to the console when a + kernel panic occurs. + + poll_ms + Modifies the polling interval for the trace_remote. + + per_cpu/ + This directory contains subdirectories for each possible CPU (e.g., + **cpu0/**, **cpu1/** ...) + + per_cpu/cpuX/trace + This is similar to the **trace** file, but it will only display the data + specific for the CPU. If written to, it only clears the specific CPU + buffer. + + per_cpu/cpuX/trace_pipe + This is similar to the **trace_pipe** file, and is a consuming read, but + it will only display (and consume) the data specific to the CPU. + + events/ + This directory contains remote events that can be enabled or disabled. + + events/enable + Allows enabling or disabling all the remote events. + Declare a remote event ======================
Signature · 1 line
2.55.0.691.gc56d675ccc-goog
Vincent Donnefort
maintainerPATCH v5 18/18 Documentation/kernel-parameters: Add trace_remote
In reply to Vincent DonnefortThe trace_remote parameter allows configuring a trace remote on registration. The syntax is similar to trace_instance.
Documentation/admin-guide/kernel-parameters.txt +19 -0
@@ -7787,6 +7787,25 @@ Kernel parameters See also Documentation/trace/ftrace.rst "trace options" section. + trace_remote=[remote-info] + [FTRACE] Configure a trace remote instance at boot. + Format: <name>[^option1[^option2...]][,event1[,event2...]] + + Supported options: + + dump_on_panic - Enable dumping the trace buffer on + panic. + dmesg - Redirect tracing output to dmesg. + buf_size=<size> - Set the trace buffer size (e.g. 2M). + poll=<ms> - Set the trace remote polling interval + in milliseconds. + + Events are a comma-separated list of events to enable. + If events are specified, tracing is automatically enabled. + + Multiple remotes can be configured by specifying this + parameter multiple times. + trace_trigger=[trigger-list] [FTRACE] Add an event trigger on specific events. Set a trigger on top of a specific event, with an optional
Signature · 1 line
2.55.0.691.gc56d675ccc-goog