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 v4] perf stat: Refuse a group mixing tool and kernel events
Date: Sun, 22 Nov 2026 09:22:48 +0000	[thread overview]
Message-ID: <sashiko-outbox-227849@kernel.org> (raw)
In-Reply-To: <20261122085947.3107661-1-takano32@gmail.com>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] perf stat: weak group exemption in check_tool_groups() causes
  NULL deref
- [Medium] perf stat: evlist__check_cpu_maps() splits hybrid tool groups
  before check
--

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

perf stat: Refuse a group mixing tool and kernel events

This commit adds check_tool_groups() to refuse event groups that mix tool
events with kernel events, together with documentation and a shell test.

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/

> diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c
> index 37752d6bfef45..7396cd92346af 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
>  
> +/*
> + * A tool event like user_time has no event in the kernel: it reads
> + * /proc/stat, or keeps its value in perf itself.  A group that mixes one
> + * with an event the kernel opens cannot be counted.
> + * get_group_fd() passes the leader's descriptor as the group fd, and the
> + * tool event has a procfs descriptor or none at all, so the kernel rejects
> + * the members with EBADF or perf gives up with "broken group leader"; 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 user_time or system_time is left to the weak group
> + * handling in the open loop, which splits it when a member fails with
> + * EBADF; 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, and so does a weak
> +		 * group split in the open loop.
> +		 */
> +		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 user_time or system_time is split by the
> +		 * weak group handling in the open loop, as the kernel refuses
> +		 * the members with EBADF, and that is what :W asks for.  Any
> +		 * other weak group does not get that far, so it is refused
> +		 * like a strong group.
> +		 */
> +		if (weak && evsel__is_tool(leader)) {
> +			enum tool_pmu_event ev = evsel__tool_event(leader);
> +
> +			if (ev == TOOL_PMU__EVENT_USER_TIME ||
> +			    ev == TOOL_PMU__EVENT_SYSTEM_TIME)
> +				continue;
> +		}

[Severity: Medium]
Can a weak group led by user_time or system_time still bypass the EBADF
split in __run_perf_stat() and trigger a NULL pointer dereference of
evsel->fd?

In the kernel, sys_perf_event_open() checks security_perf_event_open() and
perf_allow_kernel() before looking up group_fd with is_perf_file(group).
If a kernel member fails there with EACCES (for example, running
perf stat -a -e '{user_time,cycles:k}:W' -- true unprivileged under the
default kernel.perf_event_paranoid=2, where evsel__fallback() cannot fall
back because exclude_user is set), __run_perf_stat() does not call
evlist__reset_weak_group() because it only splits on EINVAL or EBADF.

__run_perf_stat() then calls perf_evsel__free_fd(&counter->core) on the
unsupported member while leaving it attached to the user_time leader, and
evlist__enable() -> evsel__enable_cpu() -> perf_evsel__run_ioctl()
dereferences NULL evsel->fd in xyarray__max_y(evsel->fd).

[ ... ]

> @@ -2947,6 +3018,11 @@ int cmd_stat(int argc, const char **argv)
>  
>  	evlist__check_cpu_maps(evsel_list);
>  
> +	if (check_tool_groups(evsel_list)) {
> +		status = -EINVAL;
> +		goto out;
> +	}

[Severity: Medium]
On hybrid CPU systems, doesn't running evlist__check_cpu_maps() before
check_tool_groups() split all tool events out of a core PMU group under -a
before check_tool_groups() can refuse it?

On a hybrid machine (such as Intel cpu_core and cpu_atom), core PMU events
like cycles have core.cpus restricted to that PMU's CPU subset, while
duration_time has 1 CPU and user_time has all online CPUs.  Under -a,
perf_cpu_map__equal(leader->core.cpus, evsel->core.cpus) is false for both
tool events, so evlist__check_cpu_maps() removes both from the group and
reduces leader->core.nr_members to 1.  check_tool_groups() then sees no
group left to refuse, and the new test case in stat+tool_group.sh:

  refused '{cycles,duration_time,user_time}' 'user_time|duration_time' -a

exits 0 and fails on hybrid machines.

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

  reply	other threads:[~2026-11-22  9:22 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 [this message]
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

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-227849@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.