Linux Perf Users
 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 8/8] perf/core: Stop an event which overflows while it is throttled
Date: Sat,  5 Dec 2026 15:32:21 +0200	[thread overview]
Message-ID: <20261205133221.3294-9-john3@mailbox.org> (raw)
In-Reply-To: <20261205133221.3294-1-john3@mailbox.org>

The overflow of an event which has the throttle mark is not accounted.
An event which counts with the mark set is therefore never throttled:
it interrupts at its full rate until the tick unthrottles its group.

perf_event_unthrottle() clears the mark and then starts the event. An
NMI which throttles the group between the two marks the event again,
and it is started with the mark:

  perf_event_unthrottle(event)        NMI of another event of the group
    hw.throttled = 0
                                      perf_event_throttle_group()
                                        hw.throttled = 1
                                        pmu->stop(), not started yet
    pmu->start()

The callers disable the PMU, but that does not rule the NMI out. A KVM
guest which uses x86_pmu_handle_irq() gets such NMIs while its PMU is
disabled.

Stop an event which overflows with the mark set. Its group is
throttled then, and the tick starts the event again with the group.

Do not do that while perf_event_throttle_group() runs on this CPU. The
event can be one which that walk has marked and is stopping, and a
second stop from an NMI inside the first one gives the warning in
x86_pmu_stop() which the previous patch describes.

__perf_event_period() starts the event before it clears the mark, and
does not start it a second time. An overflow in between would leave
the event stopped. Unthrottle the group first there.

I could not hit the window with the series applied. Without the first
patch of the series the hardware events count while
__perf_event_period() unthrottles the group, and its test, with
periods of 1000 and 500 for the two hardware events, hangs a KVM guest
on a Ryzen 9 7945HX. With this patch the test completes, in two of
two boots.

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. Patch 2 relies on an event with the throttle mark being
stopped. Patch 1 closes the one path I found on which that does not
hold. This patch is for the case that one is left: whatever starts an
event with the mark, its first overflow stops it.

The NMI with the PMU disabled is from a debug print in the guest,
with the generic handler (clearcpuid=perfmon_v2) and patches 1 to 7:
32 throttles from an NMI inside the unthrottle walk of
__perf_event_period() in 18 runs of perloop (notes of patch 1), each
with the PMU of the context disabled. None with the PerfMonV2 handler.
I have no such print from bare metal.

The test of the changelog is perloop with both hardware events
overflowing, one sample per tick, four runs of 10 seconds with periods
from 100 to 5000, same guest, without patch 1:

  patches 2 to 7      the guest stops answering in the third run
                      (periods 1000 and 500), as with v4
  patches 2 to 8      runs, 2 of 2 boots

Both still give the warnings which patch 1 fixes. All eight patches:
no warning in that test.

With the whole series I forced the window with a test-only hook:
every 7th unthrottle of a hardware event throttles the group between
the two lines of the diagram. On a Core i7-14700T, bare metal, that
gave 8.5 million such starts in the tests of patches 1, 2 and 7. The
event was stopped at its overflow in 4.50 million of them, and all
tests complete. With the stop of this patch switched off, the KVM
guest stops answering in perloop (periods 5000 and 2000), with both
handlers; with the stop the same runs complete.

In the other 4.01 million the next unthrottle came before the
overflow and started an event which was running: the warning in
x86_pmu_start(). This patch does not change that. As far as I can
see mainline warns the same way if an NMI throttles a group there.

The change in __perf_event_period() is not from a test. The event
was started with the mark there, and the mark was cleared afterwards
without a start. I have no case in which the event overflows between
the two: its PMU is disabled and the counter has just been reloaded.
The order is now the one perf_event_unthrottle() has.

pmu->stop() on an event which is stopped already is what the tick and
__perf_event_period() do today for a throttled event, and the x86
handlers now report the overflow of a sibling which the throttle of
the same NMI has stopped, so this path runs in every test of the
series.

It is a patch of its own so that patches 2 to 5 stay what was posted
as v4.

 kernel/events/core.c | 20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)

diff --git a/kernel/events/core.c b/kernel/events/core.c
index fdc7335c7321..9706c2ea69f4 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -2827,13 +2827,18 @@ static void perf_event_unthrottle_group(struct perf_event *event, bool skip_star
 		perf_event_unthrottle(sibling, skip_start_event ? sibling != event : true);
 }
 
+/* Nonzero while perf_event_throttle_group() walks a group on this CPU. */
+static DEFINE_PER_CPU(int, perf_throttling);
+
 static void perf_event_throttle_group(struct perf_event *event)
 {
 	struct perf_event *sibling, *leader = event->group_leader;
 
+	__this_cpu_inc(perf_throttling);
 	perf_event_throttle(leader);
 	for_each_sibling_event(sibling, leader)
 		perf_event_throttle(sibling);
+	__this_cpu_dec(perf_throttling);
 }
 
 static int
@@ -6581,15 +6586,17 @@ static void __perf_event_period(struct perf_event *event,
 	local64_set(&event->hw.period_left, 0);
 
 	if (active) {
-		event->pmu->start(event, PERF_EF_RELOAD);
 		/*
 		 * Once the period is force-reset, the event starts immediately.
 		 * But the event/group could be throttled. Unthrottle the
 		 * event/group now to avoid the next tick trying to unthrottle
-		 * while we already re-started the event/group.
+		 * while we already re-started the event/group. Do that before
+		 * the start: an event which overflows while it is throttled is
+		 * stopped, and nothing would start it again.
 		 */
 		if (event->hw.throttled)
 			perf_event_unthrottle_group(event, true);
+		event->pmu->start(event, PERF_EF_RELOAD);
 		perf_pmu_enable(event->pmu_ctx->pmu);
 	}
 }
@@ -10788,9 +10795,16 @@ __perf_event_account_interrupt(struct perf_event *event, int throttle,
 	 * perf_event_throttle() has stopped it, or one that was pending when
 	 * it was stopped. Do not account it, that could throttle the group a
 	 * second time.
+	 *
+	 * Nothing would throttle an event which counts with the mark set, so
+	 * stop it here. Not inside a throttle on this CPU: that one stops the
+	 * events it has marked itself.
 	 */
-	if (unlikely(hwc->throttled))
+	if (unlikely(hwc->throttled)) {
+		if (!__this_cpu_read(perf_throttling))
+			event->pmu->stop(event, 0);
 		return 1;
+	}
 
 	seq = __this_cpu_read(perf_throttled_seq);
 	if (seq != hwc->interrupts_seq) {
-- 
2.56.1


  parent 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 ` [PATCH v5 1/8] perf/core: Disable the PMU of the context in __perf_event_period() Oancea Catalin
2026-12-05 13:58   ` 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 ` Oancea Catalin [this message]
2026-12-05 14:24   ` [PATCH v5 8/8] perf/core: Stop an event which overflows while it is throttled 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-9-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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox