All of lore.kernel.org
 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 v6] perf stat: Refuse a group mixing tool and kernel events
Date: Wed, 25 Nov 2026 02:17:26 +0000	[thread overview]
Message-ID: <sashiko-outbox-232396@kernel.org> (raw)
In-Reply-To: <20261125013826.1495729-1-takano32@gmail.com>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] perf tests: per-task <not counted> failure on hybrid PMUs in
  accepted()
- [Medium] perf stat: NULL deref in evsel__enable_cpu() on unsplit weak
  tool group
--

commit 102728bd68653615df5786d4a884d15a2f55a157
Author: TAKANO Mitsuhiro <takano32@gmail.com>

perf stat: Refuse a group mixing tool and kernel events

This commit rejects event groups that mix tool PMU events with kernel events
in perf stat, while leaving weak groups led by a tool event to the open-loop
weak group splitting logic and returning -2 from get_group_fd() for any tool
leader.

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/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..21cf17d3dcca8 100644
> --- a/tools/perf/builtin-stat.c
> +++ b/tools/perf/builtin-stat.c
> @@ -2469,6 +2469,70 @@ static int parse_tpebs_mode(const struct option *opt, const char *str,
>  }
>  #endif // HAVE_ARCH_X86_64_SUPPORT
>  
> +/*
> + * A tool event like user_time has no event in the kernel: perf reads it
> + * from a file such as /proc/stat, or computes it in perf itself.  A group
> + * that mixes one with an event the kernel opens cannot be counted.
> + * get_group_fd() has no group fd to hand to the members when the leader is
> + * a tool event, so the members cannot be in a kernel group with it; with
> + * a kernel event as the leader the members do open, but the group read is
> + * rejected as the kernel knows only the members it opened.  perf stat used
> + * to leave the rest of the group uncounted in both cases, when the CPU maps
> + * had not split the group first; refuse the group instead.  A group with no
> + * event the kernel opens is left alone, as perf reads each event in it.  A
> + * weak group led by a tool event is left to the weak group handling in the
> + * open loop, which splits it when a member fails to open; any other weak
> + * group is refused like a strong one.
> + */
> +static int check_tool_groups(struct evlist *evlist)
> +{
> +	struct evsel *leader, *evsel;
> +
> +	evlist__for_each_entry(evlist, leader) {
> +		struct evsel *tool = 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;
> +			if (evsel__is_tool(evsel)) {
> +				if (!tool)
> +					tool = evsel;
> +			} else if (!evsel__is_non_perf_event_open_pmu(evsel)) {
> +				has_kernel = true;
> +			}
> +			weak |= evsel->weak_group;
> +		}
> +
> +		if (!tool || !has_kernel)
> +			continue;
> +
> +		/*
> +		 * A weak group led by a tool event is left to the weak group
> +		 * handling in the open loop: get_group_fd() returns -2 for a
> +		 * tool leader, the open fails with EINVAL, and that is what
> +		 * splits the group.  Any other weak group does not get that
> +		 * far, so it is refused like a strong group.
> +		 */
> +		if (weak && evsel__is_tool(leader))
> +			continue;

[Severity: Medium]
Can a weak group led by a tool event still reach enable_counters() with an
unsplit member whose core.fd is NULL, causing a NULL pointer dereference in
evsel__enable_cpu()?

With a retire-latency member (for example,
perf stat -a -e '{user_time,cycles:R}:W' -- true), evsel__open_cpu() returns
early after evsel__tpebs_open() before allocating core.fd or calling
get_group_fd(), so the weak group is never split.  Similarly, with an
architecture force-grouped member and :WX (for example,
perf stat -a -e '{user_time,slots}:WX' -- true on x86),
evsel__remove_from_group() refuses to detach a member where
arch_evsel__must_be_in_group() is true, so the second pass fails again in
get_group_fd() and perf_evsel__free_fd() sets member->core.fd to NULL while
leaving it attached to the tool leader:

tools/perf/util/evsel.c:evsel__remove_from_group() {
	if (!arch_evsel__must_be_in_group(evsel) && evsel != leader) {
		evsel__set_leader(evsel, evsel);
		evsel->core.nr_members = 0;
		leader->core.nr_members--;
	}
}

When !target__enable_on_exec(&target), __evlist__enable() checks
pos->core.fd only on the tool leader and calls evsel__enable_cpu(leader),
which loops over remaining group members and calls
perf_evsel__enable_cpu(&member->core) -> perf_evsel__run_ioctl() ->
xyarray__max_y(NULL).

> 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..ff71429779792
> --- /dev/null
> +++ b/tools/perf/tests/shell/stat+tool_group.sh

[ ... ]

> +accepted() {
> +  local group="$1" name rc=0
> +
> +  shift
> +  echo "Testing that perf stat accepts ${group}"
> +  perf stat -e "${group}" -o "${out}" -- true 2> "${stderr}" || rc=$?
> +
> +  if [ "${rc}" != 0 ]; then
> +    cat "${stderr}"
> +    echo "perf stat failed for ${group} (${rc})  [Failed]"
> +    err=1
> +    return
> +  fi
> +  if grep -q "cannot be grouped with kernel events" "${stderr}"; then
> +    cat "${stderr}"
> +    echo "perf stat refused ${group}  [Failed]"
> +    err=1
> +    return
> +  fi
> +  for name in "$@"; do
> +    # When no hardware cycles event can be counted, cycles falls back to
> +    # task-clock and the evsel is renamed, so it need not appear.
> +    case "${name}" in
> +      cycles|cycles:*)
> +        [ "${kernel_events}" = 0 ] && continue ;;
> +    esac
> +    if ! grep -q -- "${name}" "${out}"; then
> +      echo "perf stat did not print ${name}  [Failed]"
> +      cat "${out}"
> +      err=1
> +    fi
> +  done
> +  if grep -q "<not counted>" "${out}"; then
> +    echo "perf stat did not count an event in ${group}  [Failed]"
> +    cat "${out}"
> +    err=1

[Severity: Medium]
Will this "<not counted>" check in accepted() fail on hybrid systems when
running the per-task test cases such as '{user_time,cycles}:W' and
'{cycles,instructions},duration_time'?

On hybrid machines (such as Intel Alder Lake or ARM big.LITTLE), unqualified
cycles and instructions expand into one evsel per core PMU (for example,
cpu_core/cycles/ and cpu_atom/cycles/).  Because the initial kernel_events
probe only checks for "<not supported>", kernel_events remains 1, and a
short-lived per-task run of "true" without --hybrid-merge executes on only
one core type while printing "<not counted>" for the unused core PMU.

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

  reply	other threads:[~2026-11-25  2:17 UTC|newest]

Thread overview: 26+ 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:04 ` Hang Zhengyang
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 [this message]
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

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-232396@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.