Linux Perf Users
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "TAKANO Mitsuhiro" <takano32@gmail.com>,
	linux-perf-users@vger.kernel.org
Subject: Re: [PATCH v13 1/2] perf stat: Refuse a group mixing tool and kernel events
Date: Fri, 04 Dec 2026 04:36:35 +0000	[thread overview]
Message-ID: <sashiko-outbox-249097@kernel.org> (raw)
In-Reply-To: <20261204041444.1078407-1-takano32@gmail.com>

Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [Medium] perf: get_group_fd() EINVAL causes infinite retry loop in
  record__open()
- [Medium] perf test: hybrid_core_types() does not strip running
  percentages
- [Medium] perf test: stat+tool_group.sh -C picks CPU 0 without matching
  core PMU
- [Low] perf stat: quadratic evlist scan in check_unopened_in_group()
--

commit 5aa05b6d313e575c95aee1475611bfc35b8273a1
Author: TAKANO Mitsuhiro <takano32@gmail.com>

perf stat: Refuse a group mixing tool and kernel events

This commit rejects event groups in perf stat that mix kernel events with
events perf does not open via perf_event_open(), updates get_group_fd() and
evsel enable/disable paths to handle leaders and members without kernel
descriptors, and adds a shell test covering tool event groups.

Link: https://lore.kernel.org/all/sashiko-outbox-227849@kernel.org/
Link: https://lore.kernel.org/all/sashiko-outbox-231330@kernel.org/
Link: https://lore.kernel.org/all/sashiko-outbox-232396@kernel.org/
Link: https://lore.kernel.org/all/20261111-tool-pmu-leader-v2-1-3faa0fb98c51@gmail.com/
Link: https://lore.kernel.org/all/awC_aZXD5FBGu4mO@google.com/
Link: https://lore.kernel.org/all/awTXdgY38s2bijW5@google.com/
Link: https://lore.kernel.org/all/20261123071913.653315-7-irogers@google.com/

[ ... ]

> diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c
> index 37752d6bfef45..4fa3958c4cf33 100644
> --- a/tools/perf/builtin-stat.c
> +++ b/tools/perf/builtin-stat.c
> @@ -2469,6 +2469,77 @@ static int parse_tpebs_mode(const struct option *opt, const char *str,
>  }
>  #endif // HAVE_ARCH_X86_64_SUPPORT

[ ... ]

> +static int check_unopened_in_group(struct evlist *evlist)
> +{
> +	struct evsel *leader, *evsel;
> +
> +	evlist__for_each_entry(evlist, leader) {
> +		struct evsel *unopened = NULL;
> +		bool has_kernel = false, weak = false;
> +
> +		if (!evsel__is_group_leader(leader) || leader->core.nr_members < 2)
> +			continue;
> +
> +		/*
> +		 * The members of a group are not necessarily adjacent in the
> +		 * evlist: evlist__check_cpu_maps() makes a removed member a
> +		 * leader of its own without moving it.
> +		 */
> +		evlist__for_each_entry(evlist, evsel) {
> +			if (!evsel__has_leader(evsel, leader))
> +				continue;

[Severity: Low]
Can this nested evlist scan cause noticeable startup overhead on large
grouped workloads?

cmd_stat() calls check_unopened_in_group() after evlist__expand_cgroup(),
which multiplies both the evlist length and the number of group leaders by
the number of cgroups.  Scanning the full evlist for every multi-member
leader makes startup O(C^2) even when no tool, hwmon, or drm events are
present.

A linear pre-check for any evsel__is_non_perf_event_open_pmu() event in a
multi-member group (or stopping the inner loop once leader->core.nr_members
entries are visited) would avoid the quadratic scan in the common case.

[ ... ]

> diff --git a/tools/perf/tests/shell/stat+tool_group.sh b/tools/perf/tests/shell/stat+tool_group.sh
> new file mode 100755
> index 0000000000000..4a2aae62321c0
> --- /dev/null
> +++ b/tools/perf/tests/shell/stat+tool_group.sh

[ ... ]

> +hybrid_core_types() {
> +  awk 'function bare(name) {
> +         gsub(/\/$/, "", name)
> +         sub(/^[^\/]*\//, "", name)
> +         return name
> +       }
> +       FNR == NR {
> +         sub(/#.*/, "")
> +         if ($1 ~ /^[0-9][0-9,.]*$/)
> +           counted[bare($(NF))] = 1
> +         next
> +       }
> +       /<not counted>/ {
> +         sub(/#.*/, "")
> +         if (bare($(NF)) in counted)
> +           found = 1
> +       }
> +       END { exit !found }' "${out}" "${out}"
> +}

[Severity: Medium]
Does hybrid_core_types() fail to extract the event name when
print_running_std() appends a running percentage?

When run != ena, print_running_std() prints a trailing "(XX.XX%)" after the
event name, so $(NF) evaluates to "(0.00%)" on an unexecuted hybrid core
PMU line like "<not counted> cpu_atom/cycles/  (0.00%)" and accepted()
fails.

Follow-up commit "perf stat: Fix hybrid test failure and respect $PERF in
stat+tool_group.sh" strips the percentage in the /<not counted>/ block, but
omits the same strip from the FNR == NR block, where a multiplexed counted
event without a "#" metric comment still records counted["(XX.XX%)"].
Stripping the trailing percentage in both passes would handle both cases.

[ ... ]

> +if grep -qE '(^|,)0(,|$)' /sys/devices/system/cpu/online 2>/dev/null; then
> +  refused '{cycles,duration_time}' duration_time -C 0
> +else
> +  echo "CPU 0 is not online, cannot test -C  [Skipped]"
> +  skipped=$((skipped + 1))
> +fi

[Severity: Medium]
Will this -C 0 test silently skip on SMP systems and hang perf stat on
hybrid systems?

The regex '(^|,)0(,|$)' does not match standard cpulist range notation such
as "0-7", so multi-CPU systems skip the check.  Follow-up commit "perf
stat: Fix hybrid test failure and respect $PERF in stat+tool_group.sh"
fixes the range parsing, but still picks CPU 0 without checking which core
PMU covers it.

On a hybrid Intel machine where CPU 0 belongs to cpu_core and cpu_atom
(sorted first in core_pmus) covers higher CPUs, '{cycles,duration_time}'
makes cpu_atom/cycles/ the group leader.  __perf_evlist__propagate_maps()
then unlinks it via list_del_init() while duration_time still points to it.

evlist__check_cpu_maps() -> evsel__group_desc() -> for_each_group_evsel()
then loops forever on the self-linked leader, hanging perf stat.  Picking
an online CPU from a core PMU's cpus file and qualifying cycles with that
PMU would avoid this.

[ ... ]

> diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c
> index d060fca8090f6..ff8e8e3228588 100644
> --- a/tools/perf/util/evsel.c
> +++ b/tools/perf/util/evsel.c

[ ... ]

> @@ -2371,23 +2418,59 @@ static int get_group_fd(struct evsel *evsel, int cpu_map_idx, int thread)

[ ... ]

> +	if (fd == -1) {
> +		/*
> +		 * A weak group is split on EINVAL, but a strong group must
> +		 * keep the leader's errno: an unprivileged open that failed
> +		 * with EACCES has to keep reporting the perf_event_paranoid
> +		 * diagnostic rather than turn into "Invalid argument".
> +		 */
> +		if (evsel->weak_group)
> +			errno = EINVAL;
> +		return -2;
> +	}

[Severity: Medium]
Can overwriting errno with EINVAL here cause an infinite retry loop in
record__open()?

If an unprivileged user runs perf record -e '{slots,topdown-retiring}:W'
on an x86 Intel CPU under perf_event_paranoid restrictions, slots fails
evsel__open() with EACCES (leaving fd == -1) and is marked skippable.
Opening topdown-retiring then hits get_group_fd(), which now overwrites
errno to EINVAL and returns -2.

tools/perf/builtin-record.c:record__open() {
	...
			if ((errno == EINVAL || errno == EBADF) &&
			    pos->core.leader != &pos->core &&
			    pos->weak_group) {
				pos = evlist__reset_weak_group(evlist, pos, true);
				goto try_again;
			}
	...
}

Because arch_evsel__must_be_in_group() is true for topdown-retiring,
evsel__remove_from_group() refuses to ungroup it and leaves pos->weak_group
intact.  evlist__reset_weak_group() rewinds pos back to slots, so
record__open() retries indefinitely and floods stderr.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261204041444.1078407-1-takano32@gmail.com?part=1

      parent reply	other threads:[~2026-12-04  4:36 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-11  6:04 [PATCH v2] perf stat: Count kernel events in a group led by a tool event Hang Zhengyang via B4 Relay
2026-11-11  6:18 ` sashiko-bot
2026-11-11 11:06 ` [PATCH v3] " TAKANO Mitsuhiro
2026-11-11 11:33   ` sashiko-bot
2026-11-12  3:49     ` Hang Zhengyang
2026-11-19  8:06   ` Namhyung Kim
2026-11-19 10:08     ` TAKANO Mitsuhiro
2026-11-20 21:45       ` Namhyung Kim
2026-11-22  8:59   ` [PATCH v4] perf stat: Refuse a group mixing tool and kernel events TAKANO Mitsuhiro
2026-11-22  9:22     ` sashiko-bot
2026-11-24 13:56     ` [PATCH v5] " TAKANO Mitsuhiro
2026-11-24 14:18       ` sashiko-bot
2026-11-25  1:38       ` [PATCH v6] " TAKANO Mitsuhiro
2026-11-25  2:17         ` sashiko-bot
2026-11-25 13:38         ` [PATCH v7] " TAKANO Mitsuhiro
2026-11-25 13:56           ` sashiko-bot
2026-11-25 17:54           ` [PATCH v8] " TAKANO Mitsuhiro
2026-11-25 18:22             ` sashiko-bot
2026-11-25 22:27             ` [PATCH v9] " TAKANO Mitsuhiro
2026-11-25 22:59               ` sashiko-bot
2026-12-01  6:48               ` Ian Rogers
2026-12-01 14:08               ` [PATCH v10] " TAKANO Mitsuhiro
2026-12-01 14:27                 ` sashiko-bot
2026-12-01 23:55                 ` [PATCH v11] " TAKANO Mitsuhiro
2026-12-02  0:11                   ` sashiko-bot
2026-12-02  5:33                   ` [PATCH v12] " TAKANO Mitsuhiro
2026-12-02  5:48                     ` sashiko-bot
2026-12-03 21:14                     ` Ian Rogers
2026-12-04  4:14                     ` [PATCH v13 1/2] " TAKANO Mitsuhiro
2026-12-04  4:14                       ` [PATCH v13 2/2] perf stat: Fix hybrid test failure and respect $PERF in stat+tool_group.sh TAKANO Mitsuhiro
2026-12-04  4:28                         ` sashiko-bot
2026-12-04  4:36                       ` sashiko-bot [this message]

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-249097@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=takano32@gmail.com \
    /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