Re: [PATCH 8/8] tracing: Update modules to persistent instances when loaded

From: Google
Date: Thu Feb 06 2025 - 11:53:41 EST


On Thu, 6 Feb 2025 10:36:12 -0500
Steven Rostedt <rostedt@xxxxxxxxxxx> wrote:

> On Thu, 6 Feb 2025 19:01:24 +0900
> Masami Hiramatsu (Google) <mhiramat@xxxxxxxxxx> wrote:
>
> > > +static void trace_module_record(struct module *mod)
> > > +{
> > > + struct trace_array *tr;
> > > +
> > > + list_for_each_entry(tr, &ftrace_trace_arrays, list) {
> > > + /* Update any persistent trace array that has already been started */
> > > + if ((tr->flags & (TRACE_ARRAY_FL_BOOT | TRACE_ARRAY_FL_LAST_BOOT)) ==
> > > + TRACE_ARRAY_FL_BOOT) {
> > > + /* Only update if the trace array is active */
> > > + if (trace_array_active(tr))
> >
> > Do we really need this check? It seems that we can just save_mod() if the
> > above condition is true.
>
> It gets a little more complicated if we need to add and remove modules.

Yeah, but for converting the module address, we don't want to see other
module information.

>
> >
> > > + save_mod(mod, tr);
> > > + }
> > > + }
> > > +}
> > > +
> > > static int trace_module_notify(struct notifier_block *self,
> > > unsigned long val, void *data)
> > > {
> > > @@ -10096,6 +10120,7 @@ static int trace_module_notify(struct notifier_block *self,
> > > switch (val) {
> > > case MODULE_STATE_COMING:
> > > trace_module_add_evals(mod);
> > > + trace_module_record(mod);
> > > break;
> > > case MODULE_STATE_GOING:
> > > trace_module_remove_evals(mod);
> >
> > Don't we need to remove the module entry when a module is removed?
> > (everytime we remove a module, trace data is cleared?)
>
> I do have a patch that that removes entries, but I decided we don't really
> want to do that.
>
> If we want to have events for modules that were removed. Note, the ring
> buffer is cleared if any module event was ever enabled and then the module
> is removed, as how to print it is removed too. But we could disable that
> for the persistent ring buffers as they should not be using the default
> trace event print format anyway.

Yeah, if the event is on the module the buffer is cleared.
But the module address can be in the stacktrace. In that case, the event
in the module is not enabled, but other events like sched_switch can
take stacktrace which can include the module address. In that case, the
buffer is also cleared when the module is removed?

> As for stack traces, we still want the module it was for when the stack
> trace happens. A common bug we see is when a module is removed, it can
> cause other bugs. We want to know about modules that were removed. Keeping
> that information about removed modules will allow us to see what functions
> were called by a stack trace for a module that was removed.

Hmm, but that should be covered by module load/unload events?

Anyway, this series does not cover the module text address in the stacktrace.
I just made a series of patches (which also not cover the module removal yet),
but it can show the basic idea.

My idea is to sort the previous module entries in the persistent buffer
when it is setup. We also make a "module_delta" array in the trace_array.
Then the print function can searche the appropriate module info from
the sorted table and find corresponding delta from "module_delta".

For example,

/sys/kernel/tracing/instances/boot_mapped # cat trace
<...>-1629 [006] ..... 202.860051: foo_bar_with_fn: foo Look at me 4
<...>-1629 [006] ..... 202.860059: <stack trace>
=> trace_event_raw_event_foo_bar_with_fn
=> simple_thread_fn
=> kthread
=> ret_from_fork
=> ret_from_fork_asm
/sys/kernel/tracing/instances/boot_mapped # cat last_boot_info
Offset: 0
ffffffffa0016000 trace_events_sample
ffffffffa0025000 trace_printk
/sys/kernel/tracing/instances/boot_mapped # lsmod

trace_events_sample 45056 0 - Live 0xffffffffa001c000
trace_printk 12288 0 - Live 0xffffffffa0016000

Let me share it.

Thank you,

--
Masami Hiramatsu (Google) <mhiramat@xxxxxxxxxx>