From: Masami Hiramatsu (Google) <mhiramat@kernel.org>
To: Steven Rostedt <rostedt@goodmis.org>
Cc: linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org,
Masami Hiramatsu <mhiramat@kernel.org>,
Mark Rutland <mark.rutland@arm.com>,
Mathieu Desnoyers <mathieu.desnoyers@efficios.com>,
Andrew Morton <akpm@linux-foundation.org>
Subject: Re: [PATCH 6/8] tracing: Have persistent trace instances save module addresses
Date: Thu, 6 Feb 2025 17:26:42 +0900 [thread overview]
Message-ID: <20250206172642.391c16d75dbce00518ef9688@kernel.org> (raw)
In-Reply-To: <20250205225103.930895467@goodmis.org>
On Wed, 05 Feb 2025 17:50:37 -0500
Steven Rostedt <rostedt@goodmis.org> wrote:
> From: Steven Rostedt <rostedt@goodmis.org>
>
> For trace instances that are mapped to persistent memory, have them use
> the scratch area to save the currently loaded modules. This will allow
> where the modules have been loaded on the next boot so that their
> addresses can be deciphered by using where they were loaded previously.
>
> Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
> ---
> kernel/trace/trace.c | 99 ++++++++++++++++++++++++++++++++++++++------
> 1 file changed, 87 insertions(+), 12 deletions(-)
>
> diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c
> index cb9f8e6878a0..a8e5f7ac2193 100644
> --- a/kernel/trace/trace.c
> +++ b/kernel/trace/trace.c
> @@ -5994,14 +5994,60 @@ ssize_t tracing_resize_ring_buffer(struct trace_array *tr,
> return ret;
> }
>
> +struct trace_mod_entry {
> + unsigned long mod_addr;
> + char mod_name[MODULE_NAME_LEN];
> +};
> +
> struct trace_scratch {
> unsigned long kaslr_addr;
> + unsigned long nr_entries;
> + struct trace_mod_entry entries[];
> };
>
> +static int save_mod(struct module *mod, void *data)
> +{
> + struct trace_array *tr = data;
> + struct trace_scratch *tscratch;
> + struct trace_mod_entry *entry;
> + unsigned int size;
> +
> + tscratch = tr->scratch;
> + if (!tscratch)
> + return -1;
> + size = tr->scratch_size;
> +
> + if (struct_size(tscratch, entries, tscratch->nr_entries + 1) > size)
> + return -1;
> +
> + entry = &tscratch->entries[tscratch->nr_entries];
> +
> + tscratch->nr_entries++;
> +
> + entry->mod_addr = (unsigned long)mod->mem[MOD_TEXT].base;
> + strscpy(entry->mod_name, mod->name);
> +
> + return 0;
> +}
> +
> static void update_last_data(struct trace_array *tr)
> {
> struct trace_scratch *tscratch;
>
> + if (!(tr->flags & TRACE_ARRAY_FL_BOOT))
> + return;
> +
> + /* Reset the module list and reload them */
> + if (tr->scratch) {
> + struct trace_scratch *tscratch = tr->scratch;
> +
> + memset(tscratch->entries, 0,
> + flex_array_size(tscratch, entries, tscratch->nr_entries));
> + tscratch->nr_entries = 0;
> +
> + module_for_each_mod(save_mod, tr);
> + }
This maybe too frequently scan the module because the update_last_data() is
called when;
- change tracer (this maybe OK)
- update "set_event"
- write 1 to "enable" under events
- change pid filter
- etc.
Once it is scanned, it should not scan again, but update by module
callback, because usually events are enabled individually (thus
write 1 to "event" can happen multiple time online).
I think we can move this after TRACE_ARRAY_FL_LAST_BOOT check,
if TRACE_ARRAY_FL_LAST_BOOT flag is set, that flag should be cleared
with updating the tscratch, and the flag is not set, we can skip updating
the scratch.
Thank you,
> +
> if (!(tr->flags & TRACE_ARRAY_FL_LAST_BOOT))
> return;
>
> @@ -9226,6 +9272,46 @@ static struct dentry *trace_instance_dir;
> static void
> init_tracer_tracefs(struct trace_array *tr, struct dentry *d_tracer);
>
> +static void setup_trace_scratch(struct trace_array *tr, void *scratch, unsigned int size)
> +{
> + struct trace_scratch *tscratch = scratch;
> + struct trace_mod_entry *entry;
> +
> + if (!scratch)
> + return;
> +
> + tr->scratch = scratch;
> + tr->scratch_size = size;
> +
> +#ifdef CONFIG_RANDOMIZE_BASE
> + if (tscratch->kaslr_addr)
> + tr->text_delta = kaslr_offset() - tscratch->kaslr_addr;
> +#endif
> +
> + if (struct_size(tscratch, entries, tscratch->nr_entries) > size)
> + goto reset;
> +
> + /* Check if each module name is a valid string */
> + for (int i = 0; i < tscratch->nr_entries; i++) {
> + int n;
> +
> + entry = &tscratch->entries[i];
> +
> + for (n = 0; n < MODULE_NAME_LEN; n++) {
> + if (entry->mod_name[n] == '\0')
> + break;
> + if (!isprint(entry->mod_name[n]))
> + goto reset;
> + }
> + if (n == MODULE_NAME_LEN)
> + goto reset;
> + }
> + return;
> + reset:
> + /* Invalid trace modules */
> + memset(scratch, 0, size);
> +}
> +
> static int
> allocate_trace_buffer(struct trace_array *tr, struct array_buffer *buf, int size)
> {
> @@ -9243,19 +9329,8 @@ allocate_trace_buffer(struct trace_array *tr, struct array_buffer *buf, int size
> tr->range_addr_size);
>
> scratch = ring_buffer_meta_scratch(buf->buffer, &scratch_size);
> - if (scratch) {
> - tr->scratch = scratch;
> - tr->scratch_size = scratch_size;
> + setup_trace_scratch(tr, scratch, scratch_size);
>
> -#ifdef CONFIG_RANDOMIZE_BASE
> - {
> - struct trace_scratch *tscratch = tr->scratch;
> -
> - if (tscratch->kaslr_addr)
> - tr->text_delta = kaslr_offset() - tscratch->kaslr_addr;
> - }
> -#endif
> - }
> /*
> * This is basically the same as a mapped buffer,
> * with the same restrictions.
> --
> 2.45.2
>
>
--
Masami Hiramatsu (Google) <mhiramat@kernel.org>
next prev parent reply other threads:[~2025-02-06 8:26 UTC|newest]
Thread overview: 45+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-02-05 22:50 [PATCH 0/8] ring-buffer/tracing: Save module information in persistent memory Steven Rostedt
2025-02-05 22:50 ` [PATCH 1/8] ring-buffer: Use kaslr address instead of text delta Steven Rostedt
2025-02-06 0:32 ` Masami Hiramatsu
2025-02-05 22:50 ` [PATCH 2/8] ring-buffer: Add buffer meta data for persistent ring buffer Steven Rostedt
2025-02-06 5:10 ` Masami Hiramatsu
2025-02-06 15:19 ` Steven Rostedt
2025-02-05 22:50 ` [PATCH 3/8] ring-buffer: Add ring_buffer_meta_scratch() Steven Rostedt
2025-02-06 5:13 ` Masami Hiramatsu
2025-02-05 22:50 ` [PATCH 4/8] tracing: Have persistent trace instances save KASLR offset Steven Rostedt
2025-02-06 5:22 ` Masami Hiramatsu
2025-02-06 15:24 ` Steven Rostedt
2025-02-07 0:58 ` Masami Hiramatsu
2025-02-07 1:03 ` Steven Rostedt
2025-02-07 2:15 ` Masami Hiramatsu
2025-02-05 22:50 ` [PATCH 5/8] module: Add module_for_each_mod() function Steven Rostedt
2025-02-06 5:28 ` Masami Hiramatsu
2025-02-06 15:27 ` Steven Rostedt
2025-02-10 13:04 ` Petr Pavlu
2025-02-10 14:08 ` Sebastian Andrzej Siewior
2025-02-14 22:30 ` Steven Rostedt
2025-02-18 21:21 ` Luis Chamberlain
2025-02-18 21:29 ` Steven Rostedt
2025-02-19 0:24 ` Steven Rostedt
2025-02-19 16:02 ` Luis Chamberlain
2025-02-05 22:50 ` [PATCH 6/8] tracing: Have persistent trace instances save module addresses Steven Rostedt
2025-02-06 8:26 ` Masami Hiramatsu [this message]
2025-02-06 15:29 ` Steven Rostedt
2025-02-06 16:53 ` Masami Hiramatsu
2025-02-05 22:50 ` [PATCH 7/8] tracing: Show module names and addresses of last boot Steven Rostedt
2025-02-07 1:51 ` Masami Hiramatsu
2025-02-07 2:02 ` Steven Rostedt
2025-02-07 2:25 ` Masami Hiramatsu
2025-02-05 22:50 ` [PATCH 8/8] tracing: Update modules to persistent instances when loaded Steven Rostedt
2025-02-06 10:01 ` Masami Hiramatsu
2025-02-06 15:36 ` Steven Rostedt
2025-02-06 16:53 ` Masami Hiramatsu
2025-02-06 16:58 ` [PATCH 1/3] tracing: Skip update_last_data() if it is already updated Masami Hiramatsu (Google)
2025-02-06 16:58 ` [PATCH 2/3] tracing: Remove checking the activity when module map is updating Masami Hiramatsu (Google)
2025-03-07 15:21 ` Steven Rostedt
2025-03-11 0:40 ` Masami Hiramatsu
2025-02-06 16:59 ` [PATCH 3/3] tracing: Show last module text symbols in the stacktrace Masami Hiramatsu (Google)
2025-02-06 17:46 ` Steven Rostedt
2025-02-07 1:50 ` Masami Hiramatsu
2025-02-06 17:18 ` [PATCH 8/8] tracing: Update modules to persistent instances when loaded Steven Rostedt
2025-02-07 0:47 ` Masami Hiramatsu
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250206172642.391c16d75dbce00518ef9688@kernel.org \
--to=mhiramat@kernel.org \
--cc=akpm@linux-foundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-trace-kernel@vger.kernel.org \
--cc=mark.rutland@arm.com \
--cc=mathieu.desnoyers@efficios.com \
--cc=rostedt@goodmis.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).