tracing: Add reset to trace remotes

Allow to reset the trace remote buffer by writing to the Tracefs "trace"
file. This is similar to the regular Tracefs interface.

Link: https://patch.msgid.link/20260309162516.2623589-7-vdonnefort@google.com
Reviewed-by: Steven Rostedt (Google) <rostedt@goodmis.org>
Signed-off-by: Vincent Donnefort <vdonnefort@google.com>
Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
This commit is contained in:
Vincent Donnefort 2026-03-09 16:24:52 +00:00 committed by Steven Rostedt (Google)
parent 96e43537af
commit 9af4ab0e11
2 changed files with 48 additions and 0 deletions

View File

@ -17,6 +17,8 @@
* remote to allow writing.
* @swap_reader_page: Called when Tracefs consumes a new page from a
* ring-buffer. It is expected from the remote to isolate a
* @reset: Called on `echo 0 > trace`. It is expected from the
* remote to reset all ring-buffer pages.
* new reader-page from the @cpu ring-buffer.
*/
struct trace_remote_callbacks {
@ -24,6 +26,7 @@ struct trace_remote_callbacks {
void (*unload_trace_buffer)(struct trace_buffer_desc *desc, void *priv);
int (*enable_tracing)(bool enable, void *priv);
int (*swap_reader_page)(unsigned int cpu, void *priv);
int (*reset)(unsigned int cpu, void *priv);
};
int trace_remote_register(const char *name, struct trace_remote_callbacks *cbs, void *priv);

View File

@ -63,6 +63,7 @@ static int trace_remote_load(struct trace_remote *remote)
rb_remote->desc = desc;
rb_remote->swap_reader_page = remote->cbs->swap_reader_page;
rb_remote->priv = remote->priv;
rb_remote->reset = remote->cbs->reset;
remote->trace_buffer = ring_buffer_alloc_remote(rb_remote);
if (!remote->trace_buffer) {
remote->cbs->unload_trace_buffer(desc, remote->priv);
@ -138,6 +139,21 @@ static int trace_remote_disable_tracing(struct trace_remote *remote)
return 0;
}
static void trace_remote_reset(struct trace_remote *remote, int cpu)
{
lockdep_assert_held(&remote->lock);
if (!trace_remote_loaded(remote))
return;
if (cpu == RING_BUFFER_ALL_CPUS)
ring_buffer_reset(remote->trace_buffer);
else
ring_buffer_reset_cpu(remote->trace_buffer, cpu);
trace_remote_try_unload(remote);
}
static ssize_t
tracing_on_write(struct file *filp, const char __user *ubuf, size_t cnt, loff_t *ppos)
{
@ -414,6 +430,26 @@ static const struct file_operations trace_pipe_fops = {
.release = trace_pipe_release,
};
static ssize_t trace_write(struct file *filp, const char __user *ubuf, size_t cnt, loff_t *ppos)
{
struct inode *inode = file_inode(filp);
struct trace_remote *remote = inode->i_private;
int cpu = RING_BUFFER_ALL_CPUS;
if (inode->i_cdev)
cpu = (long)inode->i_cdev - 1;
guard(mutex)(&remote->lock);
trace_remote_reset(remote, cpu);
return cnt;
}
static const struct file_operations trace_fops = {
.write = trace_write,
};
static int trace_remote_init_tracefs(const char *name, struct trace_remote *remote)
{
struct dentry *remote_d, *percpu_d, *d;
@ -452,6 +488,10 @@ static int trace_remote_init_tracefs(const char *name, struct trace_remote *remo
if (!d)
goto err;
d = trace_create_file("trace", TRACEFS_MODE_WRITE, remote_d, remote, &trace_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);
@ -474,6 +514,11 @@ static int trace_remote_init_tracefs(const char *name, struct trace_remote *remo
&trace_pipe_fops);
if (!d)
goto err;
d = trace_create_cpu_file("trace", TRACEFS_MODE_WRITE, cpu_d, remote, cpu,
&trace_fops);
if (!d)
goto err;
}
return 0;