* [PATCH v4 0/4] perf/x86: Handle overflows of counters stopped by a throttle
@ 2026-12-03 11:39 Oancea Catalin
2026-12-03 11:39 ` [PATCH v4 1/4] perf/core: Do not account overflows of a throttled event Oancea Catalin
` (3 more replies)
0 siblings, 4 replies; 6+ messages in thread
From: Oancea Catalin @ 2026-12-03 11:39 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, 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 2 to 4 make the three handlers walk the counters which were
active on entry, so that such an overflow is handled like any other.
Patch 1 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.
v4:
- Patch 1 returns 1 instead of 0 for an event which is already
throttled. With 0, perf_swevent_overflow() did not leave its loop
when an NMI throttled the group under it, as the Sashiko review of
v3 pointed out. It reproduces with v3: a tracepoint event delivered
over 100 million samples in one go, with interrupts off for more
than 3 seconds (KVM guest).
- Patch 1 has a new changelog, as Dapeng asked. The test for the
point above showed that mainline already loses unthrottles when a
software event is in the group, so the changelog starts from that.
The comment in the code says both cases now.
- Patches 3 and 4 have Dapeng's Reviewed-by. Patches 2 to 4 are
unchanged.
- Tested again, on d06ce6f1eb5e.
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, as uid 65534: without the series
the PerfMonV2 handler warns in 2 of 2 boots; with it there is no
warning, on either handler. An MSR trace of two grouped cycles
events shows the stopped counter being handled in the same NMI in
all 822 cases with each handler, and never without the series.
perf record of that group gives 332080/332077 samples without and
385857/385857 with the series. The group of patch 1 (as root,
leader period 8000) gets 289 THROTTLE and 264 UNTHROTTLE records in
5 seconds without the series, 1273 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 3 of
3 boots without the series and in 0 of 6 with it. An MSR trace shows
the stopped counter being handled in the same NMI in all 28706
cases, and in all 20671 with the generic handler (booted with
clearcpuid=perfmon_v2); without the series in none of 6040.
- 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 389471 / 333850 317204 / 317204
cycles, E-core 386871 / 6211 387302 / 387302
instructions (leader on a
fixed counter), P-core 166259 / 382270 386034 / 386034
Also tested: PEBS events in the group and frequency mode. "perf test"
gives the same failures with and without the series.
- The group of patch 1 (cycles leader, tracepoint sibling, as root),
5 seconds on the Intel machine: 150 THROTTLE and 76 UNTHROTTLE
records without the series, 1317 of each with it.
The series is against mainline and applies to tip perf/urgent. On tip
perf/core patches 2 to 4 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 (4):
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
arch/x86/events/amd/core.c | 10 +++++++++-
arch/x86/events/core.c | 10 +++++++++-
arch/x86/events/intel/core.c | 11 ++++++++++-
kernel/events/core.c | 9 +++++++++
4 files changed, 37 insertions(+), 3 deletions(-)
base-commit: d06ce6f1eb5eeffd4ebcfde345dc34a98131d106
--
2.56.1
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH v4 1/4] perf/core: Do not account overflows of a throttled event
2026-12-03 11:39 [PATCH v4 0/4] perf/x86: Handle overflows of counters stopped by a throttle Oancea Catalin
@ 2026-12-03 11:39 ` Oancea Catalin
2026-12-03 11:39 ` [PATCH v4 2/4] perf/x86/amd/core: Handle overflows of counters stopped by a throttle Oancea Catalin
` (2 subsequent siblings)
3 siblings, 0 replies; 6+ messages in thread
From: Oancea Catalin @ 2026-12-03 11:39 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, 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
before 108 7 43139
after 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; "after" 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 1317 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 2 to 4 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 4ab3919ac78e..4b47883e82af 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -10773,6 +10773,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 [flat|nested] 6+ messages in thread
* [PATCH v4 2/4] perf/x86/amd/core: Handle overflows of counters stopped by a throttle
2026-12-03 11:39 [PATCH v4 0/4] perf/x86: Handle overflows of counters stopped by a throttle Oancea Catalin
2026-12-03 11:39 ` [PATCH v4 1/4] perf/core: Do not account overflows of a throttled event Oancea Catalin
@ 2026-12-03 11:39 ` Oancea Catalin
2026-12-03 13:42 ` Ravi Bangoria
2026-12-03 11:39 ` [PATCH v4 3/4] perf/x86: " Oancea Catalin
2026-12-03 11:39 ` [PATCH v4 4/4] perf/x86/intel: " Oancea Catalin
3 siblings, 1 reply; 6+ messages in thread
From: Oancea Catalin @ 2026-12-03 11:39 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, 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>
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 [flat|nested] 6+ messages in thread
* [PATCH v4 3/4] perf/x86: Handle overflows of counters stopped by a throttle
2026-12-03 11:39 [PATCH v4 0/4] perf/x86: Handle overflows of counters stopped by a throttle Oancea Catalin
2026-12-03 11:39 ` [PATCH v4 1/4] perf/core: Do not account overflows of a throttled event Oancea Catalin
2026-12-03 11:39 ` [PATCH v4 2/4] perf/x86/amd/core: Handle overflows of counters stopped by a throttle Oancea Catalin
@ 2026-12-03 11:39 ` Oancea Catalin
2026-12-03 11:39 ` [PATCH v4 4/4] perf/x86/intel: " Oancea Catalin
3 siblings, 0 replies; 6+ messages in thread
From: Oancea Catalin @ 2026-12-03 11:39 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, 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 2 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 [flat|nested] 6+ messages in thread
* [PATCH v4 4/4] perf/x86/intel: Handle overflows of counters stopped by a throttle
2026-12-03 11:39 [PATCH v4 0/4] perf/x86: Handle overflows of counters stopped by a throttle Oancea Catalin
` (2 preceding siblings ...)
2026-12-03 11:39 ` [PATCH v4 3/4] perf/x86: " Oancea Catalin
@ 2026-12-03 11:39 ` Oancea Catalin
3 siblings, 0 replies; 6+ messages in thread
From: Oancea Catalin @ 2026-12-03 11:39 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, 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 the series with
that one change on 6.18.66 on the same machine (same tests, same
result) 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 [flat|nested] 6+ messages in thread
* Re: [PATCH v4 2/4] perf/x86/amd/core: Handle overflows of counters stopped by a throttle
2026-12-03 11:39 ` [PATCH v4 2/4] perf/x86/amd/core: Handle overflows of counters stopped by a throttle Oancea Catalin
@ 2026-12-03 13:42 ` Ravi Bangoria
0 siblings, 0 replies; 6+ messages in thread
From: Ravi Bangoria @ 2026-12-03 13:42 UTC (permalink / raw)
To: Oancea Catalin, 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, Dapeng Mi, Ravi Bangoria
> 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>
> Cc: stable@vger.kernel.org
Reviewed-by: Ravi Bangoria <ravi.bangoria@amd.com>
Thanks,
Ravi
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2026-12-03 13:43 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-03 11:39 [PATCH v4 0/4] perf/x86: Handle overflows of counters stopped by a throttle Oancea Catalin
2026-12-03 11:39 ` [PATCH v4 1/4] perf/core: Do not account overflows of a throttled event Oancea Catalin
2026-12-03 11:39 ` [PATCH v4 2/4] perf/x86/amd/core: Handle overflows of counters stopped by a throttle Oancea Catalin
2026-12-03 13:42 ` Ravi Bangoria
2026-12-03 11:39 ` [PATCH v4 3/4] perf/x86: " Oancea Catalin
2026-12-03 11:39 ` [PATCH v4 4/4] perf/x86/intel: " Oancea Catalin
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®