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 v5] perf stat: Refuse a group mixing tool and kernel events
Date: Tue, 24 Nov 2026 14:18:32 +0000	[thread overview]
Message-ID: <sashiko-outbox-231330@kernel.org> (raw)
In-Reply-To: <20261124135630.1027716-1-takano32@gmail.com>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] perf evsel: incomplete tool leader check in get_group_fd()
  causes NULL deref
- [Low] perf test: stat+tool_group.sh fails when cycles falls back to
  task-clock
--

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

perf stat: Refuse a group mixing tool and kernel events

This commit refuses event groups in perf stat that mix tool events and
kernel events, while leaving weak groups led by a tool event to be split
when member events fail to open.

Link: https://lore.kernel.org/all/sashiko-outbox-227849@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/20261111101625.2204836-1-takano32@gmail.com/
Link: https://lore.kernel.org/all/awTXdgY38s2bijW5@google.com/

> 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..0d630a70f1b58
> --- /dev/null
> +++ b/tools/perf/tests/shell/stat+tool_group.sh
> @@ -0,0 +1,212 @@

[ ... ]

> +# The events named after the group have to show up in the output.
> +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
> +    if ! grep -q -- "${name}" "${out}"; then
> +      echo "perf stat did not print ${name}  [Failed]"
> +      cat "${out}"
> +      err=1
> +    fi
> +  done

[ ... ]

> +# A group of tool events only is read by perf itself, a single event in
> +# braces is not a group, a tool event outside the braces is not a member,
> +# and a weak group led by a tool event is split by the weak group
> +# handling.
> +accepted '{user_time,system_time}' user_time system_time
> +accepted '{duration_time,user_time}' duration_time user_time
> +accepted '{user_time}' user_time
> +accepted '{cycles,instructions},duration_time' duration_time
> +accepted '{user_time,cycles}:W' user_time cycles
> +accepted '{system_time,cycles}:W' system_time cycles
> +accepted '{duration_time,cycles}:W' duration_time cycles

[Severity: Low]
Will these three weak-group cases fail on machines or virtual machines
without a hardware cycles PMU?

When opening hardware cycles fails with ENODEV, ENOENT, or ENXIO after the
weak group splits, evsel__fallback() falls back to task-clock and renames
the evsel to task-clock.  The initial probe sets kernel_events=0 when
cycles does not appear in the output, but accepted() still requires every
name passed in "$@" (including cycles) to match "${out}".

[ ... ]

> diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c
> index d060fca8090f6..ef811b1f1e6af 100644
> --- a/tools/perf/util/evsel.c
> +++ b/tools/perf/util/evsel.c
> @@ -2381,6 +2381,15 @@ static int get_group_fd(struct evsel *evsel, int cpu_map_idx, int thread)
>  		return -1;
>  
>  	fd = FD(leader, cpu_map_idx, thread);
> +	if (fd == -1 && evsel__is_tool(leader)) {
> +		/*
> +		 * A tool event has no perf_event_open() descriptor, so -1 is
> +		 * normal for it and the members cannot be in the kernel group.
> +		 * builtin-stat.c decides on the weak group split from errno.
> +		 */
> +		errno = EINVAL;
> +		return -2;
> +	}
>  	BUG_ON(fd == -1 && leader->supported);

[Severity: Medium]
Should get_group_fd() check evsel__is_tool(leader) regardless of whether
fd == -1?

While duration_time leaves fd == -1, user_time and system_time store an
open procfs descriptor (/proc/stat or /proc/<pid>/stat) in FD(leader, ...),
so get_group_fd() still returns that procfs fd as group_fd for weak groups
such as -e '{user_time,cycles:k}:W' that check_tool_groups() lets through.

In the kernel's perf_event_open syscall, permission checks such as
perf_allow_kernel() run before group_fd is validated with is_perf_file().
Under the default kernel.perf_event_paranoid=2, opening cycles:k in
perf stat -a -e '{user_time,cycles:k}:W' fails with EACCES instead of
EBADF or EINVAL.  Because __run_perf_stat() only splits a weak group on
EINVAL or EBADF, the failed member remains attached to the user_time
leader while perf_evsel__free_fd(&counter->core) frees its fd array to
NULL, and enabling or disabling the leader then triggers a NULL pointer
dereference in evsel__enable_cpu() -> perf_evsel__enable_cpu() ->
perf_evsel__run_ioctl().

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

      reply	other threads:[~2026-11-24 14:18 UTC|newest]

Thread overview: 12+ 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 [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-231330@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