Re: [PATCH v2 2/3] tracing: initialize trace_seq buffers

From: Steven Rostedt
Date: Thu Jan 25 2024 - 15:44:42 EST


On Thu, 25 Jan 2024 17:16:21 -0300
"Ricardo B. Marliere" <ricardo@xxxxxxxxxxxx> wrote:

> Now that trace_seq_reset have been created, correct the places where the
> buffers need to be initialized.

This patch would need to come first. You don't ever want to intentionally
create a broken kernel.

Also, the change log should be:

In order to extend trace_seq into being dynamic, the struct trace_seq
will no longer be valid if simply set to zero. Call trace_seq_init() for
all trace_seq when they are first created.

>
> Suggested-by: Steven Rostedt <rostedt@xxxxxxxxxxx>
> Signed-off-by: Ricardo B. Marliere <ricardo@xxxxxxxxxxxx>
> ---
> kernel/trace/trace.c | 14 ++++++++++++++
> 1 file changed, 14 insertions(+)
>

You also need to initialize iter.seq in ftrace_dump()

-- Steve

> diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c
> index d4c55d3e21c2..9827700d0164 100644
> --- a/kernel/trace/trace.c
> +++ b/kernel/trace/trace.c
> @@ -4889,6 +4889,9 @@ __tracing_open(struct inode *inode, struct file *file, bool snapshot)
>
> mutex_unlock(&trace_types_lock);
>
> + trace_seq_init(&iter->seq);
> + trace_seq_init(&iter->tmp_seq);
> +
> return iter;
>
> fail:
> @@ -6770,6 +6773,7 @@ static int tracing_open_pipe(struct inode *inode, struct file *filp)
> }
>
> trace_seq_init(&iter->seq);
> + trace_seq_init(&iter->tmp_seq);
> iter->trace = tr->current_trace;
>
> if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
> @@ -6947,6 +6951,7 @@ tracing_read_pipe(struct file *filp, char __user *ubuf,
> trace_iterator_reset(iter);
> cpumask_clear(iter->started);
> trace_seq_init(&iter->seq);
> + trace_seq_init(&iter->tmp_seq);
>
> trace_event_read_lock();
> trace_access_lock(iter->cpu_file);
> @@ -8277,6 +8282,9 @@ static int tracing_buffers_open(struct inode *inode, struct file *filp)
> if (ret < 0)
> trace_array_put(tr);
>
> + trace_seq_init(&info->iter.seq);
> + trace_seq_init(&info->iter.tmp_seq);
> +
> return ret;
> }
>
> @@ -10300,6 +10308,9 @@ void trace_init_global_iter(struct trace_iterator *iter)
> iter->temp_size = STATIC_TEMP_BUF_SIZE;
> iter->fmt = static_fmt_buf;
> iter->fmt_size = STATIC_FMT_BUF_SIZE;
> +
> + trace_seq_init(&iter->seq);
> + trace_seq_init(&iter->tmp_seq);
> }
>
> void ftrace_dump(enum ftrace_dump_mode oops_dump_mode)
> @@ -10712,6 +10723,9 @@ void __init early_trace_init(void)
> tracepoint_printk = 0;
> else
> static_key_enable(&tracepoint_printk_key.key);
> +
> + trace_seq_init(&tracepoint_print_iter->seq);
> + trace_seq_init(&tracepoint_print_iter->tmp_seq);
> }
> tracer_alloc_buffers();
>