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 7/8] perf/core: Throttle an event only once
Date: Sat, 5 Dec 2026 15:32:20 +0200 [thread overview]
Message-ID: <20261205133221.3294-8-john3@mailbox.org> (raw)
In-Reply-To: <20261205133221.3294-1-john3@mailbox.org>
A software event throttles its group with NMIs enabled. If the NMI of
a hardware event of the group arrives while
perf_event_throttle_group() is on its way through the group, and that
event is at its limit too, the NMI throttles the group a second time.
The leader gets two THROTTLE records, and an event whose stop was
interrupted is stopped again from inside it:
WARNING: arch/x86/events/core.c:1787 at x86_pmu_stop+0x266/0x280
perf_event_throttle_group
__perf_event_overflow
perf_swevent_event
perf_tp_event
perf_trace_sched_stat_runtime
Set the mark with xchg() and leave an event alone which has it
already. Each event is then stopped and logged by one throttle only.
Tested in a KVM guest on a Ryzen 9 7945HX with two kernel cycles
events and a sched_stat_runtime tracepoint event in a group and
kernel.perf_event_max_sample_rate=100, in 20 runs of 10 seconds:
without the fix 882 of 203286 throttles were logged twice and the
warning showed; with it, none of 202494 and no warning.
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. Found while testing patch 6 with groups of three events,
and it needs the field of patch 6. swloop3 is swloop.c from the notes
of patch 2 with a second kernel cycles event between the leader and
the tracepoint event:
fh = ev_open(&h, -1);
...
+ h.sample_period += 7;
+ h.disabled = 0;
+ fh2 = ev_open(&h, fh);
...
+ ioctl(fh2, PERF_EVENT_IOC_SET_OUTPUT, fh);
It needs root, or perf_event_paranoid at 1 or lower. With the default
limit the events have to reach it at the same moment, which happens
here with periods of 8000 to 12000: as uid 65534 with
perf_event_paranoid at 1, mainline warns in the first run of 10
seconds. With kernel.perf_event_max_sample_rate=100 every overflow
throttles.
"Without the fix" in the changelog is patches 1 to 6. Mainline
(d06ce6f1eb5e) gives the same warning in the same test, also on a
Core i7-14700T, and so does 6.18.66.
On bare metal (Ryzen 9 7945HX, three runs of 10 seconds, one sample
per tick) mainline logs 313 of 8094 throttles twice with the
PerfMonV2 handler and 204 of 7957 with the generic one, and warns
with both. With the series it is none of 7635 with each handler, and
no warning.
The same warning with the same call chain was reported in February,
for 6.19-rc7, and not explained then:
https://lore.kernel.org/all/PS1PPF7E1D7501F6302C4D63E37AB0D5FC5AB98A@PS1PPF7E1D7501F.apcprd02.prod.outlook.com/
I have not run that reproducer, so I can not say that it is this
case.
The tick disables the PMU for the unthrottle. That is not an option
for the throttle: its walk also runs inside the PMU's own interrupt
handler.
kernel/events/core.c | 8 +++++++-
1 file changed, 7 insertions(+), 1 deletion(-)
diff --git a/kernel/events/core.c b/kernel/events/core.c
index 59ea05f9594e..fdc7335c7321 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -2806,7 +2806,13 @@ static void perf_event_throttle(struct perf_event *event)
if (event->state != PERF_EVENT_STATE_ACTIVE)
return;
- event->hw.throttled = 1;
+ /*
+ * An NMI can throttle the group while a throttle from another context
+ * is half way through it. Each event is stopped by one of them only.
+ */
+ if (xchg(&event->hw.throttled, 1))
+ return;
+
event->pmu->stop(event, 0);
if (event == event->group_leader)
perf_log_throttle(event, 0);
--
2.56.1
next prev 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 ` Oancea Catalin [this message]
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-8-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