Linux Perf Users
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Ian Rogers" <irogers@google.com>
Cc: linux-perf-users@vger.kernel.org
Subject: Re: [PATCH v2 06/30] perf env: Add host_env() singleton
Date: Fri, 13 Nov 2026 20:47:01 +0000	[thread overview]
Message-ID: <sashiko-outbox-213677@kernel.org> (raw)
In-Reply-To: <20261113195335.2865725-7-irogers@google.com>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] perf header: write_cmdline() conditional host_env() fallback
  corrupts perf inject HEADER_CMDLINE
- [Low] perf trace: direct cmd_trace() call in main() skips host_env()
  setup/exit
--

commit 514f7c0886cdcd7ffafc72b5f2dce5946ffe0612
Author: Ian Rogers <irogers@google.com>

perf env: Add host_env() singleton

This commit introduces a statically initialized host_env() singleton to
represent the live host environment and drops the struct perf_env *
parameter from machine__new_host(), machine__new_kallsyms(),
machine__new_live(), and __perf_session__new().

> diff --git a/tools/perf/builtin-trace.c b/tools/perf/builtin-trace.c
> index 4094141461f91..fb02fa6dd4377 100644
> --- a/tools/perf/builtin-trace.c
> +++ b/tools/perf/builtin-trace.c
[ ... ]
> @@ -2121,20 +2120,14 @@ static char *trace__machine__resolve_kernel_addr(void *vmachine, unsigned long l
>  	return machine__resolve_kernel_addr(vmachine, addrp, modp);
>  }
>  
> -static int trace__symbols_init(struct trace *trace, int argc, const char **argv,
> -			       struct evlist *evlist)
> +static int trace__symbols_init(struct trace *trace, struct evlist *evlist)
>  {
>  	int err = symbol__init(NULL);
>  
>  	if (err)
>  		return err;
>  
> -	perf_env__init(&trace->host_env);
> -	err = perf_env__set_cmdline(&trace->host_env, argc, argv);
> -	if (err)
> -		goto out;
> -
> -	trace->host = machine__new_host(&trace->host_env);
> +	trace->host = machine__new_host();
>  	if (trace->host == NULL) {
>  		err = -ENOMEM;
>  		goto out;
> @@ -2155,10 +2148,8 @@ static int trace__symbols_init(struct trace *trace, int argc, const char **argv,
>  					    /*mmap_data=*/false,
>  					    /*nr_threads_synthesize=*/1);
>  out:
> -	if (err) {
> -		perf_env__exit(&trace->host_env);
> +	if (err)
>  		symbol__exit();
> -	}
>  	return err;
>  }
>  
> @@ -2167,7 +2158,6 @@ static void trace__symbols__exit(struct trace *trace)
>  	machine__delete(trace->host);
>  	trace->host = NULL;
>  
> -	perf_env__exit(&trace->host_env);
>  	symbol__exit();
>  }
[ ... ]
> diff --git a/tools/perf/perf.c b/tools/perf/perf.c
> index d60bc929c2572..bcbc12295a387 100644
> --- a/tools/perf/perf.c
> +++ b/tools/perf/perf.c
> @@ -380,12 +381,14 @@ static int run_builtin(const struct cmd_struct *p, int argc, const char **argv)
>  		use_pager = 1;
>  	commit_pager_choice();
>  
> +	perf_env__set_cmdline(host_env(), argc, argv);
>  	status = p->fn(argc, argv);
>  	perf_config__exit();
>  #ifdef HAVE_LIBTRACEEVENT
>  	trace_event__exit();
>  #endif
>  	exit_browser(status);
> +	perf_env__exit(host_env());
>  
>  	if (status)
>  		return status & 0xff;

[Severity: Low]
Does moving perf_env__set_cmdline() and perf_env__exit() from
trace__symbols_init() and trace__symbols__exit() into run_builtin() miss
the direct cmd_trace() invocation in main()?

tools/perf/perf.c:main() {
	...
	if (strstarts(cmd, "trace")) {
		setup_path();
		argv[0] = "trace";
		return cmd_trace(argc, argv);
	}
	...
}

When invoked via the trace alias, main() calls cmd_trace() directly
without going through run_builtin(), leaving host_env()->cmdline_argv
unpopulated for trace record and skipping perf_env__exit(host_env()) on
exit.

[ ... ]

> diff --git a/tools/perf/util/header.c b/tools/perf/util/header.c
> index 9eec6deee190f..b6f86f557cafe 100644
> --- a/tools/perf/util/header.c
> +++ b/tools/perf/util/header.c
> @@ -670,6 +670,9 @@ static int write_cmdline(struct feat_fd *ff,
>  	char pbuf[MAXPATHLEN], *buf;
>  	int i, ret, n;
>  
> +	if (!env->cmdline_argv)
> +		env = host_env();
> +
>  	/* actual path to perf binary */
>  	buf = perf_exe(pbuf, MAXPATHLEN);
>  

[Severity: Medium]
Can checking !env->cmdline_argv here corrupt HEADER_CMDLINE when perf
inject regenerates the output header?

When perf inject reads the input perf.data header, process_cmdline()
populates ff->ph->env.cmdline_argv from the input file, with the perf
binary path already stored at cmdline_argv[0].  Because keep_feat()
returns false for HEADER_CMDLINE, perf_session__inject_header() calls
write_cmdline() to update the feature section.  With env->cmdline_argv
already non-NULL, write_cmdline() will not switch to host_env(), so it
reuses the input file's command line instead of recording the perf inject
invocation and prepends perf_exe() a second time.  As noted above,
invoking trace directly via main() -> cmd_trace() also bypasses
perf_env__set_cmdline(host_env(), ...), leaving host_env()->cmdline_argv
NULL when write_cmdline() falls back to host_env().

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261113195335.2865725-1-irogers@google.com?part=6

  reply	other threads:[~2026-11-13 20:47 UTC|newest]

Thread overview: 58+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-13 19:53 [PATCH v2 00/30] perf: Per-env PMUs, metric event sharing, and recorded event metrics Ian Rogers
2026-11-13 19:53 ` [PATCH v2 01/30] perf pmus: Don't skip core PMUs as duplicates Ian Rogers
2026-11-13 20:04   ` sashiko-bot
2026-11-13 19:53 ` [PATCH v2 02/30] perf script: Fix metrics computed from samples Ian Rogers
2026-11-13 20:05   ` sashiko-bot
2026-11-13 19:53 ` [PATCH v2 03/30] perf script: Don't drop metric samples for CPUs missing from the evsel Ian Rogers
2026-11-13 20:02   ` sashiko-bot
2026-11-13 19:53 ` [PATCH v2 04/30] perf metricgroup: Don't prefer tool events for displaying a metric Ian Rogers
2026-11-13 19:59   ` sashiko-bot
2026-11-13 19:53 ` [PATCH v2 05/30] perf env: Make lazy perf_env initialization thread-safe Ian Rogers
2026-11-13 20:05   ` sashiko-bot
2026-11-13 19:53 ` [PATCH v2 06/30] perf env: Add host_env() singleton Ian Rogers
2026-11-13 20:47   ` sashiko-bot [this message]
2026-11-13 19:53 ` [PATCH v2 07/30] perf pmu: Add from_env parameter to perf_pmu__init() Ian Rogers
2026-11-13 20:10   ` sashiko-bot
2026-11-13 19:53 ` [PATCH v2 08/30] perf pmus: Introduce struct perf_pmus and perf_env__pmus() Ian Rogers
2026-11-13 20:36   ` sashiko-bot
2026-11-13 19:53 ` [PATCH v2 09/30] perf pmus: Pass struct perf_pmus to find helpers Ian Rogers
2026-11-13 20:11   ` sashiko-bot
2026-11-13 19:53 ` [PATCH v2 10/30] perf pmus: Pass struct perf_pmus to scan helpers Ian Rogers
2026-11-13 20:06   ` sashiko-bot
2026-11-13 19:53 ` [PATCH v2 11/30] perf pmu-events: Add e_machine to struct pmu_events_map Ian Rogers
2026-11-13 20:12   ` sashiko-bot
2026-11-13 19:53 ` [PATCH v2 12/30] perf pmu-events: Pass e_machine to find_core_*_table() Ian Rogers
2026-11-13 20:05   ` sashiko-bot
2026-11-13 19:53 ` [PATCH v2 13/30] perf header: Pass e_machine to strcmp_cpuid_str() Ian Rogers
2026-11-13 20:01   ` sashiko-bot
2026-11-13 19:53 ` [PATCH v2 14/30] perf header: Support CPUID matching across all architectures Ian Rogers
2026-11-13 20:05   ` sashiko-bot
2026-11-13 19:53 ` [PATCH v2 15/30] perf test: Make the CPUID matching test architecture independent Ian Rogers
2026-11-13 20:02   ` sashiko-bot
2026-11-13 19:53 ` [PATCH v2 16/30] perf test: Add CPUID matching tests for other architectures Ian Rogers
2026-11-13 20:03   ` sashiko-bot
2026-11-13 19:53 ` [PATCH v2 17/30] perf dwarf-regs: Add e_machine__same_arch() Ian Rogers
2026-11-13 20:00   ` sashiko-bot
2026-11-13 19:53 ` [PATCH v2 18/30] perf metricgroup: Pass struct perf_pmus to metricgroup__for_each_metric() Ian Rogers
2026-11-13 20:37   ` sashiko-bot
2026-11-13 19:53 ` [PATCH v2 19/30] perf metricgroup: Move matching of metrics to recorded events from perf script Ian Rogers
2026-11-13 19:53 ` [PATCH v2 20/30] perf parse-events: Decode metric-id terms when adding events Ian Rogers
2026-11-13 20:33   ` sashiko-bot
2026-11-13 19:53 ` [PATCH v2 21/30] perf evsel: Generalize metric_id to a set of metric_ids Ian Rogers
2026-11-13 20:31   ` sashiko-bot
2026-11-13 19:53 ` [PATCH v2 22/30] perf pmu: Add perf_pmu__for_each_name_from_config() Ian Rogers
2026-11-13 20:13   ` sashiko-bot
2026-11-13 19:53 ` [PATCH v2 23/30] perf metricgroup: Share metric events by their counter encoding Ian Rogers
2026-11-13 19:53 ` [PATCH v2 24/30] perf stat: Share events between metrics and command line events Ian Rogers
2026-11-13 20:14   ` sashiko-bot
2026-11-13 19:53 ` [PATCH v2 25/30] perf pmus: Add perf_pmus__read_from_env() Ian Rogers
2026-11-13 19:53 ` [PATCH v2 26/30] perf metricgroup: Improve matching of metrics to recorded events Ian Rogers
2026-11-13 20:37   ` sashiko-bot
2026-11-13 19:53 ` [PATCH v2 27/30] perf metricgroup: Add metricgroup__add_recorded_metrics() Ian Rogers
2026-11-13 20:42   ` sashiko-bot
2026-11-13 19:53 ` [PATCH v2 28/30] perf stat report: Display metrics for recorded events Ian Rogers
2026-11-13 20:44   ` sashiko-bot
2026-11-13 19:53 ` [PATCH v2 29/30] perf script: Display metrics for perf stat record data Ian Rogers
2026-11-13 20:47   ` sashiko-bot
2026-11-13 19:53 ` [PATCH v2 30/30] perf test: Add stat record metrics test Ian Rogers
2026-11-13 20:14   ` sashiko-bot

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=sashiko-outbox-213677@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=irogers@google.com \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /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