* [PATCH v5 0/8] perf/x86: Handle overflows of counters stopped by a throttle
@ 2026-12-05 13:32 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
` (8 more replies)
0 siblings, 9 replies; 13+ messages in thread
From: Oancea Catalin @ 2026-12-05 13:32 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim
Cc: Kan Liang, Mark Rutland, Alexander Shishkin, Jiri Olsa,
Ian Rogers, Adrian Hunter, James Clark, Thomas Gleixner,
Borislav Petkov, Dave Hansen, x86, H. Peter Anvin,
linux-perf-users, linux-kernel, shicenci, Ravi Bangoria,
Dapeng Mi
Since 9734e25fbf5a ("perf: Fix the throttle logic for a group") a
throttle stops the whole group, from inside the PMU interrupt handler.
The x86 handlers test cpuc->active_mask while they walk the counters,
so they skip a group member which was just stopped, also when its
overflow is pending in the same interrupt:
- amd_pmu_v2_handle_irq() warns "New overflows for inactive PMCs";
- x86_pmu_handle_irq() notices the overflow in a later NMI, or never;
- handle_pmi_common() does not set the period up again, and the event
stops sampling until it is scheduled in again.
Patches 3 to 5 make the three handlers walk the counters which were
active on entry, so that such an overflow is handled like any other.
Patch 2 is needed for that: the overflow of an event which is already
throttled must not go through the throttle accounting again. It also
fixes a case in which a group is not unthrottled today.
Patch 2 relies on a throttled event being stopped. Patch 1 fixes the
one place I found where that does not hold: PERF_EVENT_IOC_PERIOD on
a software event can start a hardware event in a throttled group.
Patch 6 closes what patch 2 leaves open. The throttle mark is a value
of the overflow count, and other code which writes the count can
still remove it. The mark gets a field of its own.
Patch 7 uses that field to keep an NMI from throttling a group a
second time while a software event is throttling it.
Patch 8 stops an event which overflows while it has the mark. With
patch 2 nothing else would throttle an event which was started with
it.
v5:
- Patch 1 is new. v4 can hang without it: an event which
__perf_event_period() starts in a throttled group is not throttled
any more once patch 2 skips its accounting. Mainline only warns
there. I found this while testing v5. Please do not apply v4.
- Patch 6 is new. Sashiko's review of v4 patch 1 said that the check
for the throttle mark and the count that follows are not atomic
for a software event, and that the mark is not stored atomically
on 32-bit. The first reproduces, on mainline and less often with
v4 (software leader, hardware sibling). The NMI watchdog's handler
and the BTS drain write the count too and remove the mark as well.
- Patch 7 is new. A test of patch 6 with three events in a group
gave a warning in x86_pmu_stop(), which mainline gives too.
- Patch 8 is new. It makes patch 2 safe against an NMI which
throttles a group while the group is being unthrottled. A KVM
guest gets such NMIs with the generic handler.
- Patch 3 has Ravi's Reviewed-by.
- Patches 2 to 5 are v4 patches 1 to 4, unchanged. Only the table
in the changelog of patch 2 has new labels, "without fix" and
"with fix" (Dapeng).
- Tested again, on d06ce6f1eb5e.
v4: https://lore.kernel.org/all/20261203113922.77972-1-john3@mailbox.org/
v3: https://lore.kernel.org/all/20261203062703.15346-1-john3@mailbox.org/
v2: https://lore.kernel.org/all/20261201163432.44692-1-john3@mailbox.org/
v1: https://lore.kernel.org/all/20261124211146.61378-1-john3@mailbox.org/
Tested on d06ce6f1eb5e with perf_event_paranoid at 2:
- AMD Ryzen 9 7945HX, bare metal, one boot per kernel and handler.
Without the series the PerfMonV2 handler warns (uid 65534), and
the tests of patches 1 and 7 (root) give the x86_pmu_start() and
x86_pmu_stop() warnings with both handlers. With the series there
is no kernel warning in either boot. An MSR trace of two grouped
cycles events shows the stopped counter being handled in the same
NMI in all 822 cases with the PerfMonV2 handler and in all 822 with
the generic one, and never without the series. With the PerfMonV2
handler, perf record of that group gives 339563/339561 samples
without and 309721/309721 with the series, and the group of patch 2
(as root, leader period 8000) gets 260 THROTTLE and 224 UNTHROTTLE
records in 5 seconds without the series, 1278 of each with it.
- The same CPU as a KVM host, guest with KASAN and lockdep, as
root and as uid 65534. PerfMonV2 handler: the warning shows in 2 of
2 boots without the series and in 0 of 5 with it. An MSR trace shows
the stopped counter being handled in the same NMI in all 28657
cases, and in all 21202 with the generic handler (booted with
clearcpuid=perfmon_v2); without the series in none of 3487.
- Intel, on a Core i7-14700T, as an unprivileged user. Samples of two
grouped events with period 20000 in 4 seconds, without and with the
series:
cycles, P-core 376871 / 340116 301194 / 301194
cycles, E-core 388157 / 361422 387838 / 387838
instructions (leader on a
fixed counter), P-core 342983 / 386591 388141 / 388141
Also tested: PEBS events in the group and frequency mode.
- The group of patch 2 (cycles leader, tracepoint sibling, as root),
5 seconds on the Intel machine: 150 THROTTLE and 76 UNTHROTTLE
records without the series, 1314 of each with it.
- The numbers for patches 1, 6, 7 and 8 are in their changelogs.
The series is against mainline and applies to tip perf/urgent. On tip
perf/core patches 3 to 5 need their context adjusted (the declarations
of the sample data, and the PEBS drain lines in handle_pmi_common()).
knc_pmu_handle_irq(), p4_pmu_handle_irq() and zhaoxin_pmu_handle_irq()
have the same loop. I have no such hardware and did not change them.
Oancea Catalin (8):
perf/core: Disable the PMU of the context in __perf_event_period()
perf/core: Do not account overflows of a throttled event
perf/x86/amd/core: Handle overflows of counters stopped by a throttle
perf/x86: Handle overflows of counters stopped by a throttle
perf/x86/intel: Handle overflows of counters stopped by a throttle
perf/core: Keep the throttle mark out of hwc->interrupts
perf/core: Throttle an event only once
perf/core: Stop an event which overflows while it is throttled
arch/x86/events/amd/core.c | 10 +++++-
arch/x86/events/core.c | 10 +++++-
arch/x86/events/intel/core.c | 11 ++++++-
include/linux/perf_event.h | 7 +++++
kernel/events/core.c | 59 ++++++++++++++++++++++++++++--------
5 files changed, 81 insertions(+), 16 deletions(-)
base-commit: d06ce6f1eb5eeffd4ebcfde345dc34a98131d106
--
2.56.1
^ permalink raw reply [flat|nested] 13+ messages in thread
* [PATCH v5 1/8] perf/core: Disable the PMU of the context in __perf_event_period()
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
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
` (7 subsequent siblings)
8 siblings, 1 reply; 13+ messages in thread
From: Oancea Catalin @ 2026-12-05 13:32 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim
Cc: Kan Liang, Mark Rutland, Alexander Shishkin, Jiri Olsa,
Ian Rogers, Adrian Hunter, James Clark, Thomas Gleixner,
Borislav Petkov, Dave Hansen, x86, H. Peter Anvin,
linux-perf-users, linux-kernel, shicenci, Ravi Bangoria,
Dapeng Mi
__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
^ permalink raw reply related [flat|nested] 13+ messages in thread
* [PATCH v5 2/8] perf/core: Do not account overflows of a throttled event
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:32 ` 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
` (6 subsequent siblings)
8 siblings, 0 replies; 13+ messages in thread
From: Oancea Catalin @ 2026-12-05 13:32 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim
Cc: Kan Liang, Mark Rutland, Alexander Shishkin, Jiri Olsa,
Ian Rogers, Adrian Hunter, James Clark, Thomas Gleixner,
Borislav Petkov, Dave Hansen, x86, H. Peter Anvin,
linux-perf-users, linux-kernel, shicenci, Ravi Bangoria,
Dapeng Mi
A throttled group can stay stopped when the tick should start it
again. Its events then deliver nothing until the task is scheduled in
again.
A throttle marks every event of the group with hw.interrupts ==
MAX_INTERRUPTS and stops it. The tick unthrottles a group when it
finds that mark on the group leader. __perf_event_account_interrupt()
counts the overflow of an event which is already marked like any
other, and that overwrites the mark.
This happens when a software event throttles the group of a hardware
leader. perf_event_throttle() marks the leader and then stops it, and
an NMI of the leader in between accounts one more overflow:
software sibling hardware leader, NMI
perf_event_throttle_group()
perf_event_throttle(leader)
hw.interrupts = MAX_INTERRUPTS
perf_event_overflow()
hw.interrupts++, now 0
pmu->stop()
tick: the leader is not marked, no unthrottle
A cycles event (kernel only, period 2000) leading a group with a
sched:sched_stat_runtime tracepoint event (period 1), 5 seconds on a
pinned task in a KVM guest:
THROTTLE UNTHROTTLE tracepoint samples
without fix 108 7 43139
with fix 508 507 202554
It also happens when an interrupt handler reports the overflow of a
counter which a throttle has just stopped. The following patches make
the x86 handlers do that. Take two instructions events in one group on
an Intel CPU, the leader on fixed counter 0 and the sibling on a
general purpose counter, which overflow in the same PMI. The sibling
is handled first and throttles the group. Then the leader's overflow
is accounted and its mark wraps to 0. Without this change that group
got 29 THROTTLE records and no UNTHROTTLE record in 4 seconds.
Return early for an event which is already throttled. Return 1, as the
throttle itself does, so that perf_swevent_overflow() and the other
callers which stop on that value still do. The sample is delivered as
before.
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
---
Sashiko's review of v3 asked for 1 instead of 0 here, because of the
loop in perf_swevent_overflow():
https://lore.kernel.org/all/sashiko-outbox-247257@kernel.org/
The test below (swloop.c) is for that. It needs root, or
perf_event_paranoid at 1 or lower. With v3 the tracepoint event
delivered up to 105 million samples in one loop, with interrupts off
for up to 3.6 seconds. With 1 the longest run is 399, and 401 without
the patch (the limit was 400 per tick).
The same test gave the table in the changelog; "with fix" is this
patch alone. Debug prints (not part of the patch) show the sequence of the
diagram: in 5 seconds the leader was accounted 105 times while it was
marked, each time from NMI, inside the perf_event_throttle_group() of
the tracepoint event and before the leader was stopped. In that run
the group was running again 104 times without an UNTHROTTLE record.
On a Core i7-14700T the same group gets 150 THROTTLE and 76 UNTHROTTLE
records in 5 seconds without the series, and 1314 of each with it.
6.18.66 shows the same, in the guest and on that machine.
The 29 THROTTLE records of the two instructions events are from a test
kernel with patches 3 to 5 but without this one, on the Core i7-14700T.
Both events delivered 11928 samples in the 4 seconds, instead of about
400000.
perf_event_account_interrupt() has other callers (Intel PEBS, AMD IBS,
powerpc). For an event which is already throttled they now get the
value they get when it is throttled.
---- swloop.c ----
// SPDX-License-Identifier: GPL-2.0
/*
* swloop: does a throttle from outside end the overflow loop of a software
* event? (Question raised in review of v3 patch 1/4.) It counts kernel
* cycles, so it needs root or perf_event_paranoid at 1 or lower. The id of
* sched:sched_stat_runtime is in its tracefs "id" file, which root can read.
*
* One group on the calling task, pinned to one CPU:
* H cycles, kernel only, sample_period <hperiod> (leader, hardware, NMI)
* E tracepoint sched:sched_stat_runtime, sample_period 1
* The tracepoint adds the run time in ns to E, so every hit asks
* perf_swevent_overflow() for about a million overflows; the loop there ends
* when the event is throttled. If H's NMI throttles the group while that loop
* runs, E is marked throttled from outside.
*
* Output per run: samples of H and E, lost records, throttle records, the
* longest row of E samples (lost ones included) between two other records,
* and the longest time the task did not get to run in user mode.
* A THROTTLE record that follows another one without an UNTHROTTLE record in
* between is counted as "repeated_throttle" if H gave at most one sample in
* between (the group was throttled twice), and as "ran_without_unthrottle"
* if H gave more: H is stopped by a throttle, so the group was restarted
* without having been unthrottled.
*
* usage: swloop <cpu> <seconds> <hperiod> <tracepoint id> [buffer pages, power of 2]
*/
#define _GNU_SOURCE
#include <linux/perf_event.h>
#include <sys/syscall.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sched.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.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 h = { 0 }, e = { 0 };
struct perf_event_mmap_page *mp;
uint64_t idh, ide, nh = 0, ne = 0, lost = 0, thr = 0, unthr = 0;
uint64_t run = 0, maxrun = 0, gap, maxgap = 0, t, last, end, big = 0;
uint64_t head, tail, mask, hs = 0, repeated = 0, ranwo = 0;
int throttled = 0;
size_t pages, len;
cpu_set_t set;
char *data;
int fh, fe;
if (argc < 5) {
fprintf(stderr, "usage: swloop <cpu> <seconds> <hperiod> <tracepoint id> [pages]\n");
return 2;
}
pages = argc > 5 ? strtoul(argv[5], NULL, 0) : 8192;
CPU_ZERO(&set);
CPU_SET(atoi(argv[1]), &set);
if (sched_setaffinity(0, sizeof(set), &set)) {
perror("sched_setaffinity");
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.sample_type = PERF_SAMPLE_IDENTIFIER;
h.exclude_user = 1;
h.exclude_hv = 1;
h.disabled = 1;
fh = ev_open(&h, -1);
if (fh < 0) {
perror("open H");
return 1;
}
e.size = sizeof(e);
e.type = PERF_TYPE_TRACEPOINT;
e.config = strtoull(argv[4], NULL, 0);
e.sample_period = 1;
e.sample_type = PERF_SAMPLE_IDENTIFIER;
fe = ev_open(&e, fh);
if (fe < 0) {
perror("open E");
return 1;
}
len = (pages + 1) * sysconf(_SC_PAGESIZE);
mp = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fh, 0);
if (mp == MAP_FAILED) {
perror("mmap");
return 1;
}
data = (char *)mp + sysconf(_SC_PAGESIZE);
mask = pages * sysconf(_SC_PAGESIZE) - 1;
if (ioctl(fe, PERF_EVENT_IOC_SET_OUTPUT, fh) ||
ioctl(fh, PERF_EVENT_IOC_ID, &idh) || ioctl(fe, PERF_EVENT_IOC_ID, &ide)) {
perror("ioctl");
return 1;
}
ioctl(fh, PERF_EVENT_IOC_ENABLE, PERF_IOC_FLAG_GROUP);
last = now_ns();
end = last + strtoull(argv[2], NULL, 0) * 1000000000ull;
tail = 0;
for (;;) {
t = now_ns();
gap = t - last;
last = t;
if (gap > maxgap)
maxgap = gap;
if (gap > 20000000)
big++;
if (t > end)
break;
head = __atomic_load_n(&mp->data_head, __ATOMIC_ACQUIRE);
while (tail < head) {
struct perf_event_header hd;
uint64_t v[3] = { 0 };
size_t i, n;
for (i = 0; i < sizeof(hd); i++)
((char *)&hd)[i] = data[(tail + i) & mask];
n = hd.size - sizeof(hd);
if (n > sizeof(v))
n = sizeof(v);
for (i = 0; i < n; i++)
((char *)v)[i] = data[(tail + sizeof(hd) + i) & mask];
tail += hd.size;
if (hd.type == PERF_RECORD_SAMPLE && v[0] == ide) {
ne++;
run++;
} else if (hd.type == PERF_RECORD_LOST) {
/* only E can fill the buffer; count them with its row */
lost += v[1];
run += v[1];
} else {
if (hd.type == PERF_RECORD_SAMPLE && v[0] == idh) {
nh++;
hs += throttled;
} else if (hd.type == PERF_RECORD_THROTTLE) {
thr++;
if (throttled && hs > 1)
ranwo++;
else if (throttled)
repeated++;
throttled = 1;
hs = 0;
} else if (hd.type == PERF_RECORD_UNTHROTTLE) {
unthr++;
throttled = 0;
}
if (run > maxrun)
maxrun = run;
run = 0;
}
}
if (run > maxrun)
maxrun = run;
__atomic_store_n(&mp->data_tail, tail, __ATOMIC_RELEASE);
last = now_ns(); /* do not count the time spent reading */
}
ioctl(fh, PERF_EVENT_IOC_DISABLE, PERF_IOC_FLAG_GROUP);
printf("hperiod=%llu H=%llu E=%llu lost=%llu throttle=%llu unthrottle=%llu longest_E_row=%llu longest_gap_ms=%.1f gaps_over_20ms=%llu repeated_throttle=%llu ran_without_unthrottle=%llu\n",
(unsigned long long)h.sample_period, (unsigned long long)nh,
(unsigned long long)ne, (unsigned long long)lost,
(unsigned long long)thr, (unsigned long long)unthr,
(unsigned long long)maxrun, maxgap / 1e6, (unsigned long long)big,
(unsigned long long)repeated, (unsigned long long)ranwo);
return 0;
}
---- end of swloop.c ----
kernel/events/core.c | 9 +++++++++
1 file changed, 9 insertions(+)
diff --git a/kernel/events/core.c b/kernel/events/core.c
index da4f217d633e..bfeabcd906f2 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -10778,6 +10778,15 @@ __perf_event_account_interrupt(struct perf_event *event, int throttle,
int ret = 0;
u64 seq;
+ /*
+ * A throttled event can still report an overflow: from an NMI before
+ * perf_event_throttle() has stopped it, or one that was pending when
+ * it was stopped. Do not account it, that would overwrite the mark
+ * which the tick needs on the group leader to unthrottle the group.
+ */
+ if (unlikely(hwc->interrupts == MAX_INTERRUPTS))
+ return 1;
+
seq = __this_cpu_read(perf_throttled_seq);
if (seq != hwc->interrupts_seq) {
hwc->interrupts_seq = seq;
--
2.56.1
^ permalink raw reply related [flat|nested] 13+ messages in thread
* [PATCH v5 3/8] perf/x86/amd/core: Handle overflows of counters stopped by a throttle
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:32 ` [PATCH v5 2/8] perf/core: Do not account overflows of a throttled event Oancea Catalin
@ 2026-12-05 13:32 ` Oancea Catalin
2026-12-05 13:32 ` [PATCH v5 4/8] perf/x86: " Oancea Catalin
` (5 subsequent siblings)
8 siblings, 0 replies; 13+ messages in thread
From: Oancea Catalin @ 2026-12-05 13:32 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim
Cc: Kan Liang, Mark Rutland, Alexander Shishkin, Jiri Olsa,
Ian Rogers, Adrian Hunter, James Clark, Thomas Gleixner,
Borislav Petkov, Dave Hansen, x86, H. Peter Anvin,
linux-perf-users, linux-kernel, shicenci, Ravi Bangoria,
Dapeng Mi
Since commit 9734e25fbf5a ("perf: Fix the throttle logic for a group")
a throttle stops the whole event group. perf_event_overflow() can
therefore stop counters which the NMI handler has not reached yet.
amd_pmu_v2_handle_irq() tests cpuc->active_mask while it walks the
counters. It skips a sibling which was stopped that way, also when the
sibling has overflowed in the same NMI. The overflow is not handled,
the status bit stays set and the handler warns:
New overflows for inactive PMCs: 2
WARNING: arch/x86/events/amd/core.c:1031 at amd_pmu_v2_handle_irq+0x86e/0x9a0
An unprivileged task can trigger this with perf_event_paranoid at its
default of 2: a group of two sampling events with short periods on the
task itself, which spins in user space.
Take a copy of active_mask before the loop and walk the counters which
were active on entry. The overflow of a counter which was stopped in
the meantime is then handled like any other.
The problem was found with syzkaller. An AI coding assistant (Claude
Code) was used in the analysis and in writing the reproducer and the
patch.
Fixes: 9734e25fbf5a ("perf: Fix the throttle logic for a group")
Signed-off-by: Oancea Catalin <john3@mailbox.org>
Reviewed-by: Ravi Bangoria <ravi.bangoria@amd.com>
Cc: stable@vger.kernel.org
---
The reproducer, reduced from a syzkaller program:
---- repro.c ----
// Reproducer for "WARNING in amd_pmu_v2_handle_irq" (New overflows for inactive PMCs).
// Reduced from a program found by syzkaller.
//
// Each worker opens a group of two hardware sampling events on its own task,
// both with a short fixed period, enables them and burns CPU in user space
// for a moment, then exits. NPROC workers do this in a loop.
//
// Build: gcc -O2 -static -o repro repro.c Run: ./repro (stop with ^C)
#define _GNU_SOURCE
#include <linux/perf_event.h>
#include <string.h>
#include <sys/prctl.h>
#include <sys/syscall.h>
#include <sys/wait.h>
#include <unistd.h>
#define NPROC 6
static int ev(unsigned long long config, unsigned long long period, int group)
{
struct perf_event_attr a;
memset(&a, 0, sizeof(a));
a.type = PERF_TYPE_RAW; /* CPU hardware counter */
a.size = sizeof(a);
a.config = config;
a.sample_period = period; /* short fixed period: many interrupts per tick */
a.sample_type = PERF_SAMPLE_IP;
a.disabled = 1;
a.exclude_kernel = 1; /* what an unprivileged user may ask for */
return syscall(SYS_perf_event_open, &a, 0 /* this task */, -1 /* any CPU */, group, 0);
}
static void once(void)
{
volatile unsigned long n;
int leader = ev(0x76, 20000, -1); /* 0x76: CPU cycles */
ev(0x76, 20011, leader);
prctl(PR_TASK_PERF_EVENTS_ENABLE, 0, 0, 0, 0);
for (n = 0; n < 30000000; n++)
;
}
int main(void)
{
int i;
for (i = 0; i < NPROC; i++) {
if (fork())
continue;
for (;;) {
pid_t p = fork();
if (p == 0) {
once();
_exit(0);
}
waitpid(p, NULL, 0);
}
}
pause();
return 0;
}
---- end of repro.c ----
arch/x86/events/amd/core.c | 10 +++++++++-
1 file changed, 9 insertions(+), 1 deletion(-)
diff --git a/arch/x86/events/amd/core.c b/arch/x86/events/amd/core.c
index 49b6b8fce566..c1e2d169210a 100644
--- a/arch/x86/events/amd/core.c
+++ b/arch/x86/events/amd/core.c
@@ -958,6 +958,7 @@ static int amd_pmu_v2_handle_irq(struct pt_regs *regs)
struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
static atomic64_t status_warned = ATOMIC64_INIT(0);
u64 reserved, status, mask, new_bits, prev_bits;
+ DECLARE_BITMAP(active, X86_PMC_IDX_MAX);
struct perf_sample_data data;
struct hw_perf_event *hwc;
struct perf_event *event;
@@ -994,8 +995,15 @@ static int amd_pmu_v2_handle_irq(struct pt_regs *regs)
/* Clear any reserved bits set by buggy microcode */
status &= amd_pmu_global_cntr_mask;
+ /*
+ * perf_event_overflow() can throttle and thereby stop the whole group.
+ * Walk the counters which are active now, so that a pending overflow
+ * of a sibling stopped that way is still handled.
+ */
+ bitmap_copy(active, cpuc->active_mask, X86_PMC_IDX_MAX);
+
for_each_set_bit(idx, x86_pmu.cntr_mask, X86_PMC_IDX_MAX) {
- if (!test_bit(idx, cpuc->active_mask))
+ if (!test_bit(idx, active))
continue;
event = cpuc->events[idx];
--
2.56.1
^ permalink raw reply related [flat|nested] 13+ messages in thread
* [PATCH v5 4/8] perf/x86: Handle overflows of counters stopped by a throttle
2026-12-05 13:32 [PATCH v5 0/8] perf/x86: Handle overflows of counters stopped by a throttle Oancea Catalin
` (2 preceding siblings ...)
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 ` Oancea Catalin
2026-12-05 13:32 ` [PATCH v5 5/8] perf/x86/intel: " Oancea Catalin
` (4 subsequent siblings)
8 siblings, 0 replies; 13+ messages in thread
From: Oancea Catalin @ 2026-12-05 13:32 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim
Cc: Kan Liang, Mark Rutland, Alexander Shishkin, Jiri Olsa,
Ian Rogers, Adrian Hunter, James Clark, Thomas Gleixner,
Borislav Petkov, Dave Hansen, x86, H. Peter Anvin,
linux-perf-users, linux-kernel, shicenci, Ravi Bangoria,
Dapeng Mi
x86_pmu_handle_irq() tests cpuc->active_mask while it walks the
counters. When perf_event_overflow() throttles a group, the other
events of the group are stopped and the loop skips them, also when
they have overflowed in the same NMI.
The period of such a counter is not set up again. The counter is
restarted as it is when the group is unthrottled, so the overflow is
noticed by a later NMI of another counter, or not at all when the
event is scheduled out first.
Take a copy of active_mask before the loop and walk the counters which
were active on entry, as in amd_pmu_v2_handle_irq().
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>
Reviewed-by: Dapeng Mi <dapeng1.mi@linux.intel.com>
Cc: stable@vger.kernel.org
---
Tested with the reproducer of patch 3 on the same AMD machine and in
the guest, booted with clearcpuid=perfmon_v2 so that
amd_pmu_handle_irq() is used. I have no other hardware which uses
this handler.
arch/x86/events/core.c | 10 +++++++++-
1 file changed, 9 insertions(+), 1 deletion(-)
diff --git a/arch/x86/events/core.c b/arch/x86/events/core.c
index 4e344fb1ae3e..b4f956c5ca1c 100644
--- a/arch/x86/events/core.c
+++ b/arch/x86/events/core.c
@@ -2157,6 +2157,7 @@ void x86_pmu_update_perf_regs(struct perf_event *event,
int x86_pmu_handle_irq(struct pt_regs *regs)
{
+ DECLARE_BITMAP(active, X86_PMC_IDX_MAX);
struct perf_sample_data data;
struct cpu_hw_events *cpuc;
struct perf_event *event;
@@ -2176,8 +2177,15 @@ int x86_pmu_handle_irq(struct pt_regs *regs)
*/
apic_write(APIC_LVTPC, APIC_DM_NMI);
+ /*
+ * perf_event_overflow() can throttle and thereby stop the whole group.
+ * Walk the counters which are active now, so that a pending overflow
+ * of a sibling stopped that way is still handled.
+ */
+ bitmap_copy(active, cpuc->active_mask, X86_PMC_IDX_MAX);
+
for_each_set_bit(idx, x86_pmu.cntr_mask, X86_PMC_IDX_MAX) {
- if (!test_bit(idx, cpuc->active_mask))
+ if (!test_bit(idx, active))
continue;
event = cpuc->events[idx];
--
2.56.1
^ permalink raw reply related [flat|nested] 13+ messages in thread
* [PATCH v5 5/8] perf/x86/intel: Handle overflows of counters stopped by a throttle
2026-12-05 13:32 [PATCH v5 0/8] perf/x86: Handle overflows of counters stopped by a throttle Oancea Catalin
` (3 preceding siblings ...)
2026-12-05 13:32 ` [PATCH v5 4/8] perf/x86: " Oancea Catalin
@ 2026-12-05 13:32 ` Oancea Catalin
2026-12-05 13:32 ` [PATCH v5 6/8] perf/core: Keep the throttle mark out of hwc->interrupts Oancea Catalin
` (3 subsequent siblings)
8 siblings, 0 replies; 13+ messages in thread
From: Oancea Catalin @ 2026-12-05 13:32 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim
Cc: Kan Liang, Mark Rutland, Alexander Shishkin, Jiri Olsa,
Ian Rogers, Adrian Hunter, James Clark, Thomas Gleixner,
Borislav Petkov, Dave Hansen, x86, H. Peter Anvin,
linux-perf-users, linux-kernel, shicenci, Ravi Bangoria,
Dapeng Mi
handle_pmi_common() tests cpuc->active_mask while it walks the status
bits. When perf_event_overflow() throttles a group, in the loop or in
the PEBS drain before it, the other events of the group are stopped
and the loop skips them, also when they have overflowed in the same
PMI.
The status bit of such a counter is acked, but its period is not set
up again, and the counter is restarted as it is when the group is
unthrottled. The event then delivers no samples until it is scheduled
in again. Two identical events in one group, on a Core i7-14700T:
$ perf record -c 20000 -e '{cpu_core/cycles/u,cpu_core/cycles/u}' \
-- taskset -c 4 ./spin
cpu_core/cycles/u 389471 samples
cpu_core/cycles/u 333850 samples
Take a copy of active_mask on entry, before the PEBS drain, and walk
the counters which were active then, as in the AMD and the generic
handler.
With the change both events deliver 317204 samples.
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>
Reviewed-by: Dapeng Mi <dapeng1.mi@linux.intel.com>
Cc: stable@vger.kernel.org # needs adjustments for <= 7.1
---
./spin is a loop in user space. Debug prints in the handler (not part
of this patch) show what happens to a skipped counter without the
change: it is started again without a reload at every unthrottle and
only gets a new period when the event is scheduled in again, up to 0.9
seconds later.
The PEBS case: group of cycles (period 20000) and a PEBS instructions
event which interrupts for every record. The PEBS drain throttles the
group and the cycles event's pending overflow is skipped. It delivered
11370 to 22454 samples per second without the change and 25449 to
26074 with it, which is what its counter says.
On the Fixes: tag: the skip needs the group stop which 9734e25fbf5a
added. The sample numbers were not better before it, though. On its
parent the sibling delivers about 2000 samples per second in the same
test, as far as I can read because a throttled sibling was only
unthrottled when it was scheduled in.
On stable: 7.2 and later take the series as it is. In 6.18.y the first
hunk of this patch has a different context line. I ran v4 with that
one change on 6.18.66 on the same machine (same tests, same result)
and v5 in a KVM guest on the AMD machine, and will send the backport
once this is in mainline.
arch/x86/events/intel/core.c | 11 ++++++++++-
1 file changed, 10 insertions(+), 1 deletion(-)
diff --git a/arch/x86/events/intel/core.c b/arch/x86/events/intel/core.c
index 88a60d3e4eeb..ea109b262a21 100644
--- a/arch/x86/events/intel/core.c
+++ b/arch/x86/events/intel/core.c
@@ -3821,11 +3821,20 @@ static int handle_pmi_common(struct pt_regs *regs, u64 status)
{
struct perf_sample_data data;
struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
+ DECLARE_BITMAP(active, X86_PMC_IDX_MAX);
int bit;
int handled = 0;
inc_perf_irq_stat();
+ /*
+ * perf_event_overflow() can throttle and thereby stop the whole group,
+ * from the PEBS drain as well as from the loop below. Remember which
+ * counters are active now, so that a pending overflow of a sibling
+ * stopped that way is still handled.
+ */
+ bitmap_copy(active, cpuc->active_mask, X86_PMC_IDX_MAX);
+
/*
* Ignore a range of extra bits in status that do not indicate
* overflow by themselves.
@@ -3936,7 +3945,7 @@ static int handle_pmi_common(struct pt_regs *regs, u64 status)
handled++;
- if (!test_bit(bit, cpuc->active_mask))
+ if (!test_bit(bit, active))
continue;
/* Event may have already been cleared: */
if (!event)
--
2.56.1
^ permalink raw reply related [flat|nested] 13+ messages in thread
* [PATCH v5 6/8] perf/core: Keep the throttle mark out of hwc->interrupts
2026-12-05 13:32 [PATCH v5 0/8] perf/x86: Handle overflows of counters stopped by a throttle Oancea Catalin
` (4 preceding siblings ...)
2026-12-05 13:32 ` [PATCH v5 5/8] perf/x86/intel: " Oancea Catalin
@ 2026-12-05 13:32 ` 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
` (2 subsequent siblings)
8 siblings, 1 reply; 13+ messages in thread
From: Oancea Catalin @ 2026-12-05 13:32 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim
Cc: Kan Liang, Mark Rutland, Alexander Shishkin, Jiri Olsa,
Ian Rogers, Adrian Hunter, James Clark, Thomas Gleixner,
Borislav Petkov, Dave Hansen, x86, H. Peter Anvin,
linux-perf-users, linux-kernel, shicenci, Ravi Bangoria,
Dapeng Mi
perf_event_throttle() marks an event by setting hwc->interrupts to
MAX_INTERRUPTS, and the tick unthrottles a group when it finds that
value on the group leader. The same field counts the overflows of the
event, and code which writes the count can remove the mark. The group
then stays stopped until the task is scheduled in again:
- __perf_event_account_interrupt() runs with NMIs enabled for a
software event. When the NMI of a hardware sibling throttles the
group after the check for the mark, the increment turns
MAX_INTERRUPTS into 0.
- watchdog_overflow_callback() sets the field to 0. With a limit of
one sample per tick the NMI watchdog's event is throttled at its
first overflow, and it never fires again.
- intel_pmu_drain_bts_buffer() increments the count when a throttle
stops a BTS event. A BTS leader with a cpu-clock sibling is never
unthrottled.
- On 32-bit the mark is not stored atomically.
Keep the mark in a field of its own, hwc->throttled, which only
perf_event_throttle() and perf_event_unthrottle() write.
Tested on a Core i7-14700T with a tracepoint or cpu-clock leader and
a kernel cycles sibling, in 11 runs of 20 seconds: without the fix 33
of 42687 throttles were not followed by an unthrottle and 8 groups
stayed stopped; with it, none of 42402. With
kernel.perf_event_max_sample_rate=100 the NMI watchdog fired once
more and then stopped; with the fix the tick starts it again.
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
---
This answers Sashiko's review of v4 patch 1:
https://lore.kernel.org/all/sashiko-outbox-247691@kernel.org/
Its first point reproduces. swlead.c below opens a tracepoint
(raw_syscalls:sys_enter) or cpu-clock event as the leader of a group
with a kernel cycles event and makes system calls in a loop. It needs
root, or perf_event_paranoid at 1 or lower. The program counts a group
as stuck when it got a THROTTLE record and then nothing for 100 ms
while the task runs.
"Without the fix" in the changelog is v4, which is patches 2 to 5
here, and "with it" is patches 1 to 7. On mainline (d06ce6f1eb5e) it
was 154 of 39025, with 81 groups stopped. In a KVM guest on a Ryzen 9
7945HX, 8 runs: 35 of 20592 on mainline, 6 of 18839 with v4, none of
22023 with this patch.
The watchdog on the Intel machine: 36 NMIs per CPU before the limit
was set to one sample per tick, 37 after 20 seconds with it, on
mainline. With this patch 97.
The BTS case, a BTS leader with a cpu-clock sibling, same machine,
8 runs of 10 seconds as root and as an unprivileged user: 1611
THROTTLE records and no UNTHROTTLE record on mainline.
The second point I could only check in the object code: an i386 build
stores the mark with two instructions. With this patch it is one.
I did not make the count and the mark one atomic value, because the
watchdog and the BTS code write the count as well. They can now do so.
The NMI watchdog is still throttled like any other event when the
limit is one sample per tick. With this patch the tick starts it
again. Without a tick, in a hard lockup, nothing does, so at that
limit it still cannot report one. Whether it should be throttled at
all is a different question and not part of this series.
A software event which passed the check before the NMI can still
throttle the group once more afterwards. The mark is not lost then,
but the leader gets a second THROTTLE record. Patch 7 is for that.
---- swlead.c ----
// SPDX-License-Identifier: GPL-2.0
/*
* swlead: is the throttle mark of a software group leader lost when the NMI
* of a hardware sibling throttles the group while the leader accounts an
* overflow of its own? (Question raised in review of v4 patch 1/4.) It
* counts kernel cycles, so it needs root or perf_event_paranoid at 1 or
* lower. The id of raw_syscalls:sys_enter is in its tracefs "id" file.
*
* One group on the calling task, pinned to one CPU:
* L tracepoint raw_syscalls:sys_enter, sample_period 1 (leader, software)
* or, with tracepoint id 0, cpu-clock with sample_period 10000 ns
* H cycles, kernel only, sample_period <hperiod> (sibling, NMI)
* The task makes getppid() calls in a loop, so L accounts one overflow per
* call with interrupts off and NMIs on. With the tracepoint, L never
* throttles the group itself (a single software overflow is accounted without
* the throttle check), so every throttle comes from H's NMI.
*
* The tick unthrottles a group when its leader has the throttle mark. The
* program reports a group as stuck when it saw a THROTTLE record and then
* 100 ms pass (100 ticks at HZ=1000) with no record at all, although the task
* runs. It then disables and enables the group, which restarts it, and goes
* on. A group that lost its mark is also restarted, without an UNTHROTTLE
* record, when the task is scheduled in again; that is counted as
* "ran_without_unthrottle": a THROTTLE record that follows another one with
* more than two samples and no UNTHROTTLE record in between. With two or
* fewer samples in between it is "repeated_throttle" (the group was throttled
* twice; a stopped event may still deliver the overflow it had pending).
*
* usage: swlead <cpu> <seconds> <hperiod> <tracepoint id or 0> [buffer pages, power of 2]
*/
#define _GNU_SOURCE
#include <linux/perf_event.h>
#include <sys/syscall.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sched.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#define STUCK_NS 100000000ull
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 h = { 0 }, l = { 0 };
struct perf_event_mmap_page *mp;
uint64_t idh, idl, nh = 0, nl = 0, lost = 0, thr = 0, unthr = 0;
uint64_t t, start, end, t_thr = 0, first_stuck = 0, stuck = 0, calls = 0;
uint64_t head, tail, mask, tp, since = 0, repeated = 0, ranwo = 0;
int throttled = 0, i;
size_t pages, len;
cpu_set_t set;
char *data;
int fh, fl;
if (argc < 5) {
fprintf(stderr, "usage: swlead <cpu> <seconds> <hperiod> <tracepoint id or 0> [pages]\n");
return 2;
}
pages = argc > 5 ? strtoul(argv[5], NULL, 0) : 8192;
CPU_ZERO(&set);
CPU_SET(atoi(argv[1]), &set);
if (sched_setaffinity(0, sizeof(set), &set)) {
perror("sched_setaffinity");
return 1;
}
tp = strtoull(argv[4], NULL, 0);
l.size = sizeof(l);
if (tp) {
l.type = PERF_TYPE_TRACEPOINT;
l.config = tp;
l.sample_period = 1;
} else {
l.type = PERF_TYPE_SOFTWARE;
l.config = PERF_COUNT_SW_CPU_CLOCK;
l.sample_period = 10000;
}
l.sample_type = PERF_SAMPLE_IDENTIFIER;
l.disabled = 1;
fl = ev_open(&l, -1);
if (fl < 0) {
perror("open L");
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.sample_type = PERF_SAMPLE_IDENTIFIER;
h.exclude_user = 1;
h.exclude_hv = 1;
fh = ev_open(&h, fl);
if (fh < 0) {
perror("open H");
return 1;
}
len = (pages + 1) * sysconf(_SC_PAGESIZE);
mp = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fl, 0);
if (mp == MAP_FAILED) {
perror("mmap");
return 1;
}
data = (char *)mp + sysconf(_SC_PAGESIZE);
mask = pages * sysconf(_SC_PAGESIZE) - 1;
if (ioctl(fh, PERF_EVENT_IOC_SET_OUTPUT, fl) ||
ioctl(fh, PERF_EVENT_IOC_ID, &idh) || ioctl(fl, PERF_EVENT_IOC_ID, &idl)) {
perror("ioctl");
return 1;
}
ioctl(fl, PERF_EVENT_IOC_ENABLE, PERF_IOC_FLAG_GROUP);
start = now_ns();
end = start + strtoull(argv[2], NULL, 0) * 1000000000ull;
tail = 0;
for (;;) {
for (i = 0; i < 200; i++)
syscall(SYS_getppid);
calls += 200;
t = now_ns();
if (t > end)
break;
head = __atomic_load_n(&mp->data_head, __ATOMIC_ACQUIRE);
if (tail == head && throttled && t - t_thr > STUCK_NS) {
stuck++;
if (!first_stuck)
first_stuck = t - start;
ioctl(fl, PERF_EVENT_IOC_DISABLE, PERF_IOC_FLAG_GROUP);
ioctl(fl, PERF_EVENT_IOC_ENABLE, PERF_IOC_FLAG_GROUP);
throttled = 0;
continue;
}
while (tail < head) {
struct perf_event_header hd;
uint64_t v[3] = { 0 };
size_t k, n;
for (k = 0; k < sizeof(hd); k++)
((char *)&hd)[k] = data[(tail + k) & mask];
n = hd.size - sizeof(hd);
if (n > sizeof(v))
n = sizeof(v);
for (k = 0; k < n; k++)
((char *)v)[k] = data[(tail + sizeof(hd) + k) & mask];
tail += hd.size;
if (hd.type == PERF_RECORD_SAMPLE && v[0] == idl) {
nl++;
since += throttled;
} else if (hd.type == PERF_RECORD_SAMPLE && v[0] == idh) {
nh++;
since += throttled;
} else if (hd.type == PERF_RECORD_LOST) {
lost += v[1];
} else if (hd.type == PERF_RECORD_THROTTLE) {
thr++;
if (throttled && since > 2)
ranwo++;
else if (throttled)
repeated++;
throttled = 1;
since = 0;
} else if (hd.type == PERF_RECORD_UNTHROTTLE) {
unthr++;
throttled = 0;
}
t_thr = t;
}
__atomic_store_n(&mp->data_tail, tail, __ATOMIC_RELEASE);
}
ioctl(fl, PERF_EVENT_IOC_DISABLE, PERF_IOC_FLAG_GROUP);
printf("leader=%s hperiod=%llu calls=%llu L=%llu H=%llu lost=%llu throttle=%llu unthrottle=%llu repeated_throttle=%llu ran_without_unthrottle=%llu stuck=%llu first_stuck_ms=%.0f\n",
tp ? "tracepoint" : "cpu-clock",
(unsigned long long)h.sample_period, (unsigned long long)calls,
(unsigned long long)nl, (unsigned long long)nh,
(unsigned long long)lost, (unsigned long long)thr,
(unsigned long long)unthr, (unsigned long long)repeated,
(unsigned long long)ranwo, (unsigned long long)stuck,
first_stuck / 1e6);
return 0;
}
---- end of swlead.c ----
include/linux/perf_event.h | 7 +++++++
kernel/events/core.c | 23 +++++++++++------------
2 files changed, 18 insertions(+), 12 deletions(-)
diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h
index b52348eca6ef..f2da27067a5f 100644
--- a/include/linux/perf_event.h
+++ b/include/linux/perf_event.h
@@ -269,6 +269,13 @@ struct hw_perf_event {
u64 interrupts_seq;
u64 interrupts;
+ /*
+ * Set while the event is throttled. It is not kept in 'interrupts',
+ * which an overflow handler updates even if an NMI has throttled the
+ * event in the meantime.
+ */
+ unsigned int throttled;
+
/*
* State for freq target events, see __perf_event_overflow() and
* perf_adjust_freq_unthr_context().
diff --git a/kernel/events/core.c b/kernel/events/core.c
index bfeabcd906f2..59ea05f9594e 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -2785,8 +2785,6 @@ void perf_event_disable_inatomic(struct perf_event *event)
irq_work_queue(&event->pending_disable_irq);
}
-#define MAX_INTERRUPTS (~0ULL)
-
static void perf_log_throttle(struct perf_event *event, int enable);
static void perf_log_itrace_start(struct perf_event *event);
@@ -2796,6 +2794,7 @@ static void perf_event_unthrottle(struct perf_event *event, bool start)
return;
event->hw.interrupts = 0;
+ event->hw.throttled = 0;
if (start)
event->pmu->start(event, 0);
if (event == event->group_leader)
@@ -2807,7 +2806,7 @@ static void perf_event_throttle(struct perf_event *event)
if (event->state != PERF_EVENT_STATE_ACTIVE)
return;
- event->hw.interrupts = MAX_INTERRUPTS;
+ event->hw.throttled = 1;
event->pmu->stop(event, 0);
if (event == event->group_leader)
perf_log_throttle(event, 0);
@@ -2859,7 +2858,7 @@ event_sched_in(struct perf_event *event, struct perf_event_context *ctx)
* ticks already, also for a heavily scheduling task there is little
* guarantee it'll get a tick in a timely manner.
*/
- if (unlikely(event->hw.interrupts == MAX_INTERRUPTS))
+ if (unlikely(event->hw.throttled))
perf_event_unthrottle(event, false);
perf_pmu_disable(event->pmu);
@@ -4489,7 +4488,7 @@ static void perf_adjust_freq_unthr_events(struct list_head *event_list)
hwc = &event->hw;
- if (hwc->interrupts == MAX_INTERRUPTS)
+ if (hwc->throttled)
perf_event_unthrottle_group(event, is_event_in_freq_mode(event));
if (!is_event_in_freq_mode(event))
@@ -6583,7 +6582,7 @@ static void __perf_event_period(struct perf_event *event,
* event/group now to avoid the next tick trying to unthrottle
* while we already re-started the event/group.
*/
- if (event->hw.interrupts == MAX_INTERRUPTS)
+ if (event->hw.throttled)
perf_event_unthrottle_group(event, true);
perf_pmu_enable(event->pmu_ctx->pmu);
}
@@ -10781,10 +10780,10 @@ __perf_event_account_interrupt(struct perf_event *event, int throttle,
/*
* A throttled event can still report an overflow: from an NMI before
* perf_event_throttle() has stopped it, or one that was pending when
- * it was stopped. Do not account it, that would overwrite the mark
- * which the tick needs on the group leader to unthrottle the group.
+ * it was stopped. Do not account it, that could throttle the group a
+ * second time.
*/
- if (unlikely(hwc->interrupts == MAX_INTERRUPTS))
+ if (unlikely(hwc->throttled))
return 1;
seq = __this_cpu_read(perf_throttled_seq);
@@ -11097,7 +11096,7 @@ static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
if (!overflow)
overflow = perf_swevent_set_period(event);
- if (hwc->interrupts == MAX_INTERRUPTS)
+ if (hwc->throttled)
return;
for (; overflow; overflow--) {
@@ -11105,7 +11104,7 @@ static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
data, regs)) {
/*
* We inhibit the overflow from happening when
- * hwc->interrupts == MAX_INTERRUPTS.
+ * the event is throttled.
*/
break;
}
@@ -12511,7 +12510,7 @@ static void perf_swevent_cancel_hrtimer(struct perf_event *event)
* which guarantees that perf_swevent_hrtimer() will stop the
* hrtimer once it sees the PERF_HES_STOPPED flag.
*/
- if (is_sampling_event(event) && (hwc->interrupts != MAX_INTERRUPTS)) {
+ if (is_sampling_event(event) && !hwc->throttled) {
ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
local64_set(&hwc->period_left, ktime_to_ns(remaining));
--
2.56.1
^ permalink raw reply related [flat|nested] 13+ messages in thread
* [PATCH v5 7/8] perf/core: Throttle an event only once
2026-12-05 13:32 [PATCH v5 0/8] perf/x86: Handle overflows of counters stopped by a throttle Oancea Catalin
` (5 preceding siblings ...)
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:32 ` 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 ` [PATCH v5 0/8] perf/x86: Handle overflows of counters stopped by a throttle sashiko-bot
8 siblings, 0 replies; 13+ messages in thread
From: Oancea Catalin @ 2026-12-05 13:32 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim
Cc: Kan Liang, Mark Rutland, Alexander Shishkin, Jiri Olsa,
Ian Rogers, Adrian Hunter, James Clark, Thomas Gleixner,
Borislav Petkov, Dave Hansen, x86, H. Peter Anvin,
linux-perf-users, linux-kernel, shicenci, Ravi Bangoria,
Dapeng Mi
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
^ permalink raw reply related [flat|nested] 13+ messages in thread
* [PATCH v5 8/8] perf/core: Stop an event which overflows while it is throttled
2026-12-05 13:32 [PATCH v5 0/8] perf/x86: Handle overflows of counters stopped by a throttle Oancea Catalin
` (6 preceding siblings ...)
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
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
8 siblings, 1 reply; 13+ messages in thread
From: Oancea Catalin @ 2026-12-05 13:32 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim
Cc: Kan Liang, Mark Rutland, Alexander Shishkin, Jiri Olsa,
Ian Rogers, Adrian Hunter, James Clark, Thomas Gleixner,
Borislav Petkov, Dave Hansen, x86, H. Peter Anvin,
linux-perf-users, linux-kernel, shicenci, Ravi Bangoria,
Dapeng Mi
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
^ permalink raw reply related [flat|nested] 13+ messages in thread
* Re: [PATCH v5 6/8] perf/core: Keep the throttle mark out of hwc->interrupts
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
0 siblings, 0 replies; 13+ messages in thread
From: sashiko-bot @ 2026-12-05 13:57 UTC (permalink / raw)
To: Oancea Catalin; +Cc: linux-perf-users
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] perf/core: missing READ_ONCE()/WRITE_ONCE() on NMI-shared
hw.throttled
--
commit 670a6e0b5b041b454dc0340976a55a8311e04937
Author: Oancea Catalin <john3@mailbox.org>
perf/core: Keep the throttle mark out of hwc->interrupts
This commit moves the perf event throttle mark out of hwc->interrupts into a
dedicated hw.throttled field in struct hw_perf_event, updating
perf_event_throttle(), perf_event_unthrottle(), and all throttle checks in
kernel/events/core.c to use it.
> diff --git a/kernel/events/core.c b/kernel/events/core.c
> index bfeabcd906f2e..59ea05f9594e8 100644
> --- a/kernel/events/core.c
> +++ b/kernel/events/core.c
[ ... ]
> @@ -2796,6 +2794,7 @@ static void perf_event_unthrottle(struct perf_event *event, bool start)
> return;
>
> event->hw.interrupts = 0;
> + event->hw.throttled = 0;
> if (start)
> event->pmu->start(event, 0);
> if (event == event->group_leader)
> @@ -2807,7 +2806,7 @@ static void perf_event_throttle(struct perf_event *event)
> if (event->state != PERF_EVENT_STATE_ACTIVE)
> return;
>
> - event->hw.interrupts = MAX_INTERRUPTS;
> + event->hw.throttled = 1;
> event->pmu->stop(event, 0);
> if (event == event->group_leader)
> perf_log_throttle(event, 0);
[Severity: Low]
Should the accesses to event->hw.throttled use READ_ONCE() and WRITE_ONCE()?
hw.throttled is shared locklessly between NMI context in perf_event_throttle()
and non-NMI contexts in perf_event_unthrottle() and the read sites. When a
hardware sibling PMI throttles a mixed group during a software overflow, the
plain load racing with the NMI store triggers a KCSAN data-race report.
Unmarked accesses also allow the compiler to CSE the load across
perf_swevent_overflow() and __perf_event_account_interrupt(), or reorder
hw.interrupts = 0 and hw.throttled = 0 in perf_event_unthrottle().
Commit "perf/core: Throttle an event only once" converts perf_event_throttle()
to xchg(), but perf_event_unthrottle() and the read sites remain unmarked.
Using READ_ONCE() and WRITE_ONCE() matches other NMI-shared flags in
kernel/events/core.c such as rb->aux_in_sampling.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261205133221.3294-1-john3@mailbox.org?part=6
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [PATCH v5 1/8] perf/core: Disable the PMU of the context in __perf_event_period()
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
0 siblings, 0 replies; 13+ messages in thread
From: sashiko-bot @ 2026-12-05 13:58 UTC (permalink / raw)
To: Oancea Catalin; +Cc: linux-perf-users
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] perf/core: __perf_event_period() skips disabling event->pmu
in mixed groups
- [Medium] perf/core: __perf_event_period() stops disabling event->pmu
in mixed groups
--
commit 54df77548d2b0f131559f285443e4651cf45fd41
Author: Oancea Catalin <john3@mailbox.org>
perf/core: Disable the PMU of the context in __perf_event_period()
This commit changes __perf_event_period() to disable and re-enable
event->pmu_ctx->pmu instead of event->pmu while updating the period and
unthrottling the event's group.
> diff --git a/kernel/events/core.c b/kernel/events/core.c
> index 4ab3919ac78e9..da4f217d633e1 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);
> }
[Severity: Medium]
Should __perf_event_period() still disable event->pmu in addition to
event->pmu_ctx->pmu?
When a perf_sw_context event is grouped with a hardware PMU, event->pmu
differs from event->pmu_ctx->pmu. On s390, cf_diag uses perf_sw_context
and can be grouped with cpumf_sampling, while cpumf_pmu_stop() and
cpumf_pmu_start() rely on cpumf_pmu_disable()/enable() to execute lcctl().
Skipping perf_pmu_disable(event->pmu) and perf_pmu_enable(event->pmu)
causes cfdiag_getctr() to read live un-paused counters and can leave
hardware counter sets stopped while the event believes it is active.
Because perf_tp_event_match() gates on perf_pmu_enabled(event->pmu), a
grouped tracepoint such as msr:write_msr (from x86_pmu_start()) can also
fire during perf_event_unthrottle_group() and throttle the group mid-walk,
hitting WARN_ON_ONCE(!(event->hw.state & PERF_HES_STOPPED)).
Commit "perf/core: Stop an event which overflows while it is throttled"
moves event->pmu->start() after perf_event_unthrottle_group(), mitigating
re-entry on event itself, but still leaves event->pmu enabled and does not
fix the s390 cf_diag regression.
Nesting perf_pmu_disable(event->pmu) and perf_pmu_enable(event->pmu) inside
the event->pmu_ctx->pmu calls, as event_sched_in() and event_sched_out()
do, avoids both regressions.
> @@ -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);
> }
> }
[Severity: Medium]
Can leaving event->pmu enabled here let a perf_kprobe sibling throttle the
group during perf_event_unthrottle_group()?
In a hardware group containing two perf_kprobe siblings and two hardware
siblings, where kprobe2 is attached to perf_event_update_userpage(),
calling PERF_EVENT_IOC_PERIOD on throttled kprobe1 no longer disables
&perf_kprobe, so perf_tp_event_match() still sees event->pmu as enabled:
__perf_event_period(kprobe1)
perf_pmu_disable(hw_pmu)
perf_event_unthrottle_group(kprobe1, true)
perf_event_unthrottle(kprobe2, true)
perf_event_unthrottle(hw_sibling1, true)
x86_pmu_start(hw_sibling1, 0)
perf_event_update_userpage()
<kprobe2 fires synchronously>
... -> perf_swevent_overflow()
perf_event_throttle_group() <-- re-throttles group
perf_event_unthrottle(hw_sibling2, true) <-- starts while throttled
Returning from the kprobe lets perf_event_unthrottle_group() start
hw_sibling2 in a throttled group, so the next unthrottle hits
WARN_ON_ONCE(!(event->hw.state & PERF_HES_STOPPED)) in x86_pmu_start().
Even after commit "perf/core: Stop an event which overflows while it is
throttled", this path still re-throttles the group mid-walk and also lets
__perf_event_period() start kprobe1 with hw.throttled set.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261205133221.3294-1-john3@mailbox.org?part=1
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [PATCH v5 8/8] perf/core: Stop an event which overflows while it is throttled
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
0 siblings, 0 replies; 13+ messages in thread
From: sashiko-bot @ 2026-12-05 14:24 UTC (permalink / raw)
To: Oancea Catalin; +Cc: linux-perf-users
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] perf/core: perf_throttling guard misses non-throttle
x86_pmu_stop() callers
- [Medium] perf/core: unthrottle-vs-NMI race still starts events with
hw.throttled set
--
commit 4a662d074649b317ca9a70b8242cd52b4a4419b2
Author: Oancea Catalin <john3@mailbox.org>
perf/core: Stop an event which overflows while it is throttled
This commit stops a throttled event in __perf_event_account_interrupt()
when it overflows outside perf_event_throttle_group(), using a per-CPU
perf_throttling counter to avoid nesting stops during a group throttle
walk. It also moves perf_event_unthrottle_group() before
event->pmu->start() in __perf_event_period().
> diff --git a/kernel/events/core.c b/kernel/events/core.c
> index fdc7335c7321b..9706c2ea69f4f 100644
> --- a/kernel/events/core.c
> +++ b/kernel/events/core.c
[ ... ]
> @@ -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);
> }
> }
[Severity: Medium]
Does moving perf_event_unthrottle_group() before event->pmu->start() open
the same unthrottle-vs-NMI window here, while stopping reactively on
overflow still leaves events running with hw.throttled set?
An NMI that throttles the group between clearing hw.throttled and calling
pmu->start() (in perf_event_unthrottle() or __perf_event_period()) starts
the event with hw.throttled set. Non-sampling siblings return early at
!is_sampling_event(event) in __perf_event_overflow(), so they never reach
__perf_event_account_interrupt() and keep counting across the throttled
interval, skewing group counts.
Whenever the next tick or __perf_event_period() unthrottles the group
before an overflow (or for any non-sampling sibling),
perf_event_unthrottle() calls pmu->start() on an already-running event and
hits WARN_ON_ONCE(!(event->hw.state & PERF_HES_STOPPED)) in
x86_pmu_start().
Closing the window at the unthrottle and start sites instead -- for
example, WRITE_ONCE(event->hw.throttled, 0) and barrier() before
pmu->start(), followed by re-checking READ_ONCE(event->hw.throttled) and
calling pmu->stop(event, 0) while perf_pmu_disable() is held -- would
avoid leaving events running while throttled.
[ ... ]
> @@ -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;
> + }
[Severity: Medium]
Can this re-enter x86_pmu_stop() from an NMI when a non-throttle caller is
already stopping the event?
perf_throttling is only incremented in perf_event_throttle_group(),
leaving callers such as __perf_event_period() and event_sched_out()
unguarded. In x86_pmu_stop(), cpuc->active_mask remains set across
x86_pmu_disable() before PERF_HES_STOPPED is set, and on AMD
amd_pmu_disable_event() explicitly waits for a pending overflow NMI there
via amd_pmu_wait_on_overflow():
__perf_event_period()
x86_pmu_stop(event, PERF_EF_UPDATE)
test_bit(hwc->idx, cpuc->active_mask)
x86_pmu_disable(event)
<NMI>
perf_event_overflow()
__perf_event_account_interrupt()
/* hwc->throttled != 0, perf_throttling == 0 */
x86_pmu_stop(event, 0)
x86_pmu_disable(event)
__clear_bit(hwc->idx, cpuc->active_mask)
hwc->state |= PERF_HES_STOPPED
__clear_bit(hwc->idx, cpuc->active_mask)
WARN_ON_ONCE(hwc->state & PERF_HES_STOPPED) <-- fires
On return from the NMI, the outer x86_pmu_stop() triggers
WARN_ON_ONCE(hwc->state & PERF_HES_STOPPED). On Intel PEBS and BTS
events, the nested intel_pmu_disable_event() also re-enters non-reentrant
disable and drain paths.
Separately, because perf_throttling is a single per-CPU counter rather
than scoped to the group, throttling one group from a software event
suppresses this stop for an unrelated throttled group whose NMI arrives on
the same CPU during that walk.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261205133221.3294-1-john3@mailbox.org?part=8
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [PATCH v5 0/8] perf/x86: Handle overflows of counters stopped by a throttle
2026-12-05 13:32 [PATCH v5 0/8] perf/x86: Handle overflows of counters stopped by a throttle Oancea Catalin
` (7 preceding siblings ...)
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
8 siblings, 0 replies; 13+ messages in thread
From: sashiko-bot @ 2026-12-05 14:24 UTC (permalink / raw)
To: Oancea Catalin; +Cc: linux-perf-users
Sashiko has finished reviewing this 8-patch series:
- Patches 2-5 and 7: no issues found.
- Patch 1 ("perf/core: Disable the PMU of the context in __perf_..."): 2
potential issues (2 Medium)
- Patch 6 ("perf/core: Keep the throttle mark out of hwc->interr..."): 1
potential issue (1 Low)
- Patch 8 ("perf/core: Stop an event which overflows while it is..."): 2
potential issues (2 Medium)
Detailed reviews have been sent as separate replies to patches 1, 6, and
8.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261205133221.3294-1-john3@mailbox.org
^ permalink raw reply [flat|nested] 13+ messages in thread
end of thread, other threads:[~2026-12-05 14:24 UTC | newest]
Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 ` [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
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox