All of lore.kernel.org
 help / color / mirror / Atom feed
From: Oancea Catalin <john3@mailbox.org>
To: Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@redhat.com>,
	Arnaldo Carvalho de Melo <acme@kernel.org>,
	Namhyung Kim <namhyung@kernel.org>
Cc: Kan Liang <kan.liang@linux.intel.com>,
	Mark Rutland <mark.rutland@arm.com>,
	Alexander Shishkin <alexander.shishkin@linux.intel.com>,
	Jiri Olsa <jolsa@kernel.org>, Ian Rogers <irogers@google.com>,
	Adrian Hunter <adrian.hunter@intel.com>,
	James Clark <james.clark@linaro.org>,
	Thomas Gleixner <tglx@kernel.org>, Borislav Petkov <bp@alien8.de>,
	Dave Hansen <dave.hansen@linux.intel.com>,
	x86@kernel.org, "H. Peter Anvin" <hpa@zytor.com>,
	linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org,
	shicenci@gmail.com, Ravi Bangoria <ravi.bangoria@amd.com>,
	Dapeng Mi <dapeng1.mi@linux.intel.com>
Subject: [PATCH v5 1/8] perf/core: Disable the PMU of the context in __perf_event_period()
Date: Sat,  5 Dec 2026 15:32:14 +0200	[thread overview]
Message-ID: <20261205133221.3294-2-john3@mailbox.org> (raw)
In-Reply-To: <20261205133221.3294-1-john3@mailbox.org>

__perf_event_period() unthrottles the group of the event when the
event is throttled. It does so with event->pmu disabled. For a
software event in a group with hardware events that is the software
PMU, and the hardware events keep counting.

A hardware event which has just been started can then overflow and
throttle the group while perf_event_unthrottle_group() is still
walking it. The rest of the walk starts the remaining events of a
group which is throttled again, and the next unthrottle starts them a
second time:

  WARNING: arch/x86/events/core.c:1698 at x86_pmu_start+0x191/0x1e0
   perf_event_unthrottle_group
   perf_adjust_freq_unthr_events
   perf_adjust_freq_unthr_context
   perf_event_task_tick

Until then such an event counts with its throttle mark set. The next
patch relies on a marked event being stopped.

Disable the PMU of the event's context instead, as the tick does. In
a group with hardware events that is their PMU.

Tested in a KVM guest on a Ryzen 9 7945HX with a page-faults leader,
a kernel cycles event and an instructions event, the task setting the
period of the leader in a loop, kernel.perf_event_max_sample_rate=100:
without the fix the warning shows in the first second; with it there
is none in 2.1 million calls.

An AI coding assistant (Claude Code) was used in the analysis and in
writing the patch.

Fixes: 9734e25fbf5a ("perf: Fix the throttle logic for a group")
Signed-off-by: Oancea Catalin <john3@mailbox.org>
Cc: stable@vger.kernel.org
---
New in v5, and first for a reason: v4 without it can hang.

perloop.c below is the test of the changelog: a page-faults event
(period 2^40) leads a group with a kernel cycles event with a short
period and an instructions event, and the task sets the period of the
leader in a loop. It needs root, or perf_event_paranoid at 1 or lower.
"Without the fix" is mainline (d06ce6f1eb5e), "with it" is mainline
with this patch alone. 6.18.66 warns in the same way. With the default
sample rate limit mainline warns with a period of 2 for the cycles
event, also as uid 65534 with perf_event_paranoid at 1; with one
sample per tick any short period does.

With the last argument the instructions event has a short period
too, and it is the one which is started in a throttled group. On
mainline its next overflow removes the mark, and it is throttled like
any other event. With v4 patch 1/4 (2/8 here) its overflows are not
accounted while it is marked, so nothing throttles it. Four runs of
10 seconds with periods from 100 to 5000, same guest:

  mainline            runs, with the warning above
  v4                  the guest stops answering in the third run
                      (periods 1000 and 500)
  this patch alone    runs, no x86_pmu_start() warning
  all eight           runs, no warning

"This patch alone" still gives the warning which patch 3 fixes, as
mainline does.

So please do not apply v4.

With the generic handler (guest booted with clearcpuid=perfmon_v2)
the warning can still show in that second mode, also with the whole
series. That is a different problem. Debug prints show
x86_pmu_handle_irq() throttling the group from an NMI which KVM
delivers while the PMU is disabled, for a counter which wrapped
between its reload and its stop in the handler and was then started
again without a reload. As far as I can see the tick's unthrottle can
meet the same. The PerfMonV2 handler stops all counters while it runs
and does not have it: no such start and no warning in 18 runs.

On bare metal (Ryzen 9 7945HX) mainline gives the x86_pmu_start()
warning with both handlers. The series gives none with either, in six
runs of 10 seconds.

With the generic handler such a wrapped counter also makes this test
slow there: 177200 calls in 10 seconds with a cycles period of 200 or
800, over 3 million in the other four runs. amd_pmu_disable_all()
waits up to 50 us for a counter which looks overflowed, and with this
patch the ioctl disables that PMU. An MSR trace in the guest shows
the 50 reads of the counter in every call. On mainline the counter is
in the same state; only this ioctl does not wait for it there.

The tick (perf_adjust_freq_unthr_context()) already disables
pmu_ctx->pmu, and event_sched_in() clears the mark inside the PMU
transaction of the group.

---- perloop.c ----
// SPDX-License-Identifier: GPL-2.0
/*
 * perloop: can the NMI of a hardware event throttle a group while
 * PERF_EVENT_IOC_PERIOD on a software event of the group unthrottles it?
 * __perf_event_period() disables the PMU of the event it is called for, here
 * a software event, so the hardware events of the group can interrupt the
 * walk of perf_event_unthrottle_group().  Needs root or perf_event_paranoid
 * at 1 or lower (kernel counting).  With the default sample rate limit use a
 * period of 2; with kernel.perf_event_max_sample_rate=100 the first overflow
 * throttles and any short period does.
 *
 * One group on the calling task, pinned to one CPU:
 *   S   page-faults, sample_period 2^40                     (leader, software)
 *   H   cycles, kernel only, sample_period <hperiod>        (sibling, NMI)
 *   H2  instructions, kernel only, sample_period <h2period> (sibling)
 * The task sets the period of S in a loop.  Each call unthrottles the group:
 * S, then H is started, then H2.  If H overflows before H2 has been
 * unthrottled, its NMI throttles the group, and the walk then starts H2 in a
 * throttled group.  The next unthrottle starts H2 a second time:
 * WARN_ON_ONCE() in x86_pmu_start().  The result is in the kernel log.
 *
 * Without <h2period> H2 does not overflow (period 2^40).  With a short one H2
 * runs with its throttle mark set until it overflows.
 *
 * usage: perloop <cpu> <seconds> <hperiod> [h2period]
 */
#define _GNU_SOURCE
#include <linux/perf_event.h>
#include <sys/syscall.h>
#include <sys/ioctl.h>
#include <sched.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>

static uint64_t now_ns(void)
{
	struct timespec ts;

	clock_gettime(CLOCK_MONOTONIC, &ts);
	return ts.tv_sec * 1000000000ull + ts.tv_nsec;
}

static int ev_open(struct perf_event_attr *a, int group)
{
	return syscall(SYS_perf_event_open, a, 0, -1, group, 0);
}

int main(int argc, char **argv)
{
	struct perf_event_attr s = { 0 }, h = { 0 };
	uint64_t end, period = 1ull << 40, calls = 0, failed = 0;
	cpu_set_t set;
	int fs, fh, fh2, i;

	if (argc < 4) {
		fprintf(stderr, "usage: perloop <cpu> <seconds> <hperiod> [h2period]\n");
		return 2;
	}
	CPU_ZERO(&set);
	CPU_SET(atoi(argv[1]), &set);
	if (sched_setaffinity(0, sizeof(set), &set)) {
		perror("sched_setaffinity");
		return 1;
	}

	s.size = sizeof(s);
	s.type = PERF_TYPE_SOFTWARE;
	s.config = PERF_COUNT_SW_PAGE_FAULTS;
	s.sample_period = period;
	s.disabled = 1;
	fs = ev_open(&s, -1);
	if (fs < 0) {
		perror("open S");
		return 1;
	}

	h.size = sizeof(h);
	h.type = PERF_TYPE_HARDWARE;
	h.config = PERF_COUNT_HW_CPU_CYCLES;
	h.sample_period = strtoull(argv[3], NULL, 0);
	h.exclude_user = 1;
	h.exclude_hv = 1;
	fh = ev_open(&h, fs);
	if (fh < 0) {
		perror("open H");
		return 1;
	}
	h.config = PERF_COUNT_HW_INSTRUCTIONS;
	h.sample_period = argc > 4 ? strtoull(argv[4], NULL, 0) : period;
	fh2 = ev_open(&h, fs);
	if (fh2 < 0) {
		perror("open H2");
		return 1;
	}
	ioctl(fs, PERF_EVENT_IOC_ENABLE, PERF_IOC_FLAG_GROUP);

	end = now_ns() + strtoull(argv[2], NULL, 0) * 1000000000ull;
	do {
		for (i = 0; i < 100; i++)
			if (ioctl(fs, PERF_EVENT_IOC_PERIOD, &period))
				failed++;
		calls += 100;
	} while (now_ns() < end);
	ioctl(fs, PERF_EVENT_IOC_DISABLE, PERF_IOC_FLAG_GROUP);

	printf("hperiod=%s h2period=%s period_calls=%llu failed=%llu\n", argv[3],
	       argc > 4 ? argv[4] : "2^40",
	       (unsigned long long)calls, (unsigned long long)failed);
	return 0;
}
---- end of perloop.c ----

 kernel/events/core.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/kernel/events/core.c b/kernel/events/core.c
index 4ab3919ac78e..da4f217d633e 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -6564,7 +6564,12 @@ static void __perf_event_period(struct perf_event *event,
 
 	active = (event->state == PERF_EVENT_STATE_ACTIVE);
 	if (active) {
-		perf_pmu_disable(event->pmu);
+		/*
+		 * Disable the PMU of the context, which for a software event in
+		 * a group with hardware events is theirs: their NMI must not
+		 * throttle the group while it is unthrottled below.
+		 */
+		perf_pmu_disable(event->pmu_ctx->pmu);
 		event->pmu->stop(event, PERF_EF_UPDATE);
 	}
 
@@ -6580,7 +6585,7 @@ static void __perf_event_period(struct perf_event *event,
 		 */
 		if (event->hw.interrupts == MAX_INTERRUPTS)
 			perf_event_unthrottle_group(event, true);
-		perf_pmu_enable(event->pmu);
+		perf_pmu_enable(event->pmu_ctx->pmu);
 	}
 }
 
-- 
2.56.1


  reply	other threads:[~2026-12-05 13:32 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-05 13:32 [PATCH v5 0/8] perf/x86: Handle overflows of counters stopped by a throttle Oancea Catalin
2026-12-05 13:32 ` Oancea Catalin [this message]
2026-12-05 13:58   ` [PATCH v5 1/8] perf/core: Disable the PMU of the context in __perf_event_period() sashiko-bot
2026-12-05 13:32 ` [PATCH v5 2/8] perf/core: Do not account overflows of a throttled event Oancea Catalin
2026-12-05 13:32 ` [PATCH v5 3/8] perf/x86/amd/core: Handle overflows of counters stopped by a throttle Oancea Catalin
2026-12-05 13:32 ` [PATCH v5 4/8] perf/x86: " Oancea Catalin
2026-12-05 13:32 ` [PATCH v5 5/8] perf/x86/intel: " Oancea Catalin
2026-12-05 13:32 ` [PATCH v5 6/8] perf/core: Keep the throttle mark out of hwc->interrupts Oancea Catalin
2026-12-05 13:57   ` sashiko-bot
2026-12-05 13:32 ` [PATCH v5 7/8] perf/core: Throttle an event only once Oancea Catalin
2026-12-05 13:32 ` [PATCH v5 8/8] perf/core: Stop an event which overflows while it is throttled Oancea Catalin
2026-12-05 14:24   ` sashiko-bot
2026-12-05 14:24 ` [PATCH v5 0/8] perf/x86: Handle overflows of counters stopped by a throttle 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=20261205133221.3294-2-john3@mailbox.org \
    --to=john3@mailbox.org \
    --cc=acme@kernel.org \
    --cc=adrian.hunter@intel.com \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=bp@alien8.de \
    --cc=dapeng1.mi@linux.intel.com \
    --cc=dave.hansen@linux.intel.com \
    --cc=hpa@zytor.com \
    --cc=irogers@google.com \
    --cc=james.clark@linaro.org \
    --cc=jolsa@kernel.org \
    --cc=kan.liang@linux.intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=peterz@infradead.org \
    --cc=ravi.bangoria@amd.com \
    --cc=shicenci@gmail.com \
    --cc=tglx@kernel.org \
    --cc=x86@kernel.org \
    /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.