* [PATCH] perf/x86/amd/core: Handle overflows of counters stopped by a throttle
@ 2026-11-24 21:11 Oancea Catalin
2026-11-24 21:22 ` sashiko-bot
2026-12-01 10:56 ` Ravi Bangoria
0 siblings, 2 replies; 4+ messages in thread
From: Oancea Catalin @ 2026-11-24 21:11 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
amd_pmu_v2_handle_irq() reads PerfCntrGlobalStatus once, handles the
overflows of the counters which are set in cpuc->active_mask and warns
about every status bit which is left over:
New overflows for inactive PMCs: 2
WARNING: arch/x86/events/amd/core.c:1031 at amd_pmu_v2_handle_irq+0x86e/0x9a0
Call Trace:
perf_event_nmi_handler
nmi_handle
default_do_nmi
exc_nmi
Throttling stops the whole event group, so perf_event_overflow() can
stop counters which the loop has not reached yet. When both events of a
group overflow in the same NMI and the first one is throttled:
amd_pmu_v2_handle_irq()
status = 0x3
counter 0: x86_perf_event_set_period()
perf_event_overflow()
perf_event_throttle_group()
x86_pmu_stop(leader) clears active_mask bit 0
x86_pmu_stop(sibling) clears active_mask bit 1
counter 1: not in active_mask, skipped
status = 0x2 warning
The overflow of the sibling is not handled. Its period is not set up
again and its status bit stays set, because only the handled bits are
acknowledged.
An unprivileged task can trigger this with perf_event_paranoid at its
default of 2: open a group of two sampling events with short periods
(raw event 0x76, exclude_kernel, sample_period 20000 and 20011) on the
task itself and spin in user space. With six such tasks the warning
shows up within seconds.
Take a copy of active_mask before the loop and walk the counters which
were active on entry. For a counter which was stopped in the meantime,
update the count, clear the status bit and set up the next period as
before, but do not deliver a sample, as the event is throttled.
Observed on v7.4-rc4-162-g8d7fe9885c62 in a KVM guest (virtual
PerfMonV2 PMU, KASAN and lockdep enabled), not on bare metal. An MSR
trace of the guest shows the sequence above: status 0x3, a write to
counter 0, both event selects disabled, no write to counter 1. With
this change the trace shows counter 1 being written after the two
disables, and the warning did not trigger in six runs from fresh boots.
The problem was found with syzkaller. The reproducer, the analysis and
this patch were produced with an AI coding assistant (Claude Code,
model Claude Mythos 5.2).
Fixes: 9734e25fbf5a ("perf: Fix the throttle logic for a group")
Signed-off-by: Oancea Catalin <john3@mailbox.org>
Cc: stable@vger.kernel.org
---
Notes for reviewers, not for the changelog:
Testing was done only in a KVM guest (-cpu host on a Ryzen 9 7945HX host
running 6.12.107, so KVM's virtual PMU), with KASAN, lockdep and HZ=1000.
I have not run this on bare metal or on a non-debug kernel, where the NMI
is much faster and throttling harder to reach.
Without the patch the reproducer below triggered the warning on 4 of 4
fresh boots as root, 2 of 2 as uid 65534 and 1 of 1 as uid 65534 in a user
namespace. With the patch: 0 of 6 fresh boots (3 root, 2 uid 65534, 1 user
namespace), 90 seconds each, no other warning. In a 20 second MSR trace of
one CPU with the patch, the sequence "status 0x3, counter 0 written, both
event selects disabled, counter 1 written" occurred about 2100 times.
Not verified: that the counts and samples of the sibling are right after
it is unthrottled. The second counter of the guest counted unreliably even
without sampling, which I attribute to the host's counters being shared
with other VMs, so I could not measure it.
Not looked at: x86_pmu_handle_irq() and the Intel handler also skip
counters which are no longer in active_mask. They do not warn, but an
overflow of a sibling stopped by a group throttle would not have its
period set up again there either, if I read them correctly.
The Fixes: commit was checked by test: with the same config and
reproducer, 9734e25fbf5a warns (2 of 2 fresh boots) and its parent
ca559503b89c does not (0 of 3, 90 seconds each). 9734e25fbf5a is in v6.16
and later. I did not test the stable trees.
The kernel .config is the syzkaller one I fuzz with; I can send it.
---- 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 | 14 +++++++++++++-
1 file changed, 13 insertions(+), 1 deletion(-)
diff --git a/arch/x86/events/amd/core.c b/arch/x86/events/amd/core.c
index 49b6b8fce566..8d3781816418 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 consumed.
+ */
+ 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];
@@ -1014,6 +1022,10 @@ static int amd_pmu_v2_handle_irq(struct pt_regs *regs)
if (!x86_perf_event_set_period(event))
continue;
+ /* Stopped by a group throttle in an earlier iteration */
+ if (!test_bit(idx, cpuc->active_mask))
+ continue;
+
perf_sample_save_brstack(&data, event, &cpuc->lbr_stack, NULL);
perf_event_overflow(event, &data, regs);
base-commit: 8d7fe9885c62fc9a95bf55d9db244d9672a8aadc
--
2.56.1
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH] perf/x86/amd/core: Handle overflows of counters stopped by a throttle
2026-11-24 21:11 [PATCH] perf/x86/amd/core: Handle overflows of counters stopped by a throttle Oancea Catalin
@ 2026-11-24 21:22 ` sashiko-bot
2026-12-01 10:56 ` Ravi Bangoria
1 sibling, 0 replies; 4+ messages in thread
From: sashiko-bot @ 2026-11-24 21:22 UTC (permalink / raw)
To: Oancea Catalin; +Cc: linux-perf-users
> amd_pmu_v2_handle_irq() reads PerfCntrGlobalStatus once, handles the
> overflows of the counters which are set in cpuc->active_mask and warns
> about every status bit which is left over:
>
> New overflows for inactive PMCs: 2
> WARNING: arch/x86/events/amd/core.c:1031 at amd_pmu_v2_handle_irq+0x86e/0x9a0
> Call Trace:
> perf_event_nmi_handler
> nmi_handle
> default_do_nmi
> exc_nmi
>
> Throttling stops the whole event group, so perf_event_overflow() can
> stop counters which the loop has not reached yet. When both events of a
> group overflow in the same NMI and the first one is throttled:
> [ ... ]
> this patch were produced with an AI coding assistant (Claude Code,
> model Claude Mythos 5.2).
>
> Fixes: 9734e25fbf5a ("perf: Fix the throttle logic for a group")
> Signed-off-by: Oancea Catalin <john3@mailbox.org>
Sashiko has reviewed this patch and found no issues.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261124211146.61378-1-john3@mailbox.org?part=1
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] perf/x86/amd/core: Handle overflows of counters stopped by a throttle
2026-11-24 21:11 [PATCH] perf/x86/amd/core: Handle overflows of counters stopped by a throttle Oancea Catalin
2026-11-24 21:22 ` sashiko-bot
@ 2026-12-01 10:56 ` Ravi Bangoria
2026-12-01 16:28 ` Oancea Catalin
1 sibling, 1 reply; 4+ messages in thread
From: Ravi Bangoria @ 2026-12-01 10:56 UTC (permalink / raw)
To: Oancea Catalin
Cc: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim, 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
Hi Oancea,
> amd_pmu_v2_handle_irq() reads PerfCntrGlobalStatus once, handles the
> overflows of the counters which are set in cpuc->active_mask and warns
> about every status bit which is left over:
>
> New overflows for inactive PMCs: 2
> WARNING: arch/x86/events/amd/core.c:1031 at amd_pmu_v2_handle_irq+0x86e/0x9a0
> Call Trace:
> perf_event_nmi_handler
> nmi_handle
> default_do_nmi
> exc_nmi
>
> Throttling stops the whole event group, so perf_event_overflow() can
> stop counters which the loop has not reached yet. When both events of a
> group overflow in the same NMI and the first one is throttled:
>
> amd_pmu_v2_handle_irq()
> status = 0x3
> counter 0: x86_perf_event_set_period()
> perf_event_overflow()
> perf_event_throttle_group()
> x86_pmu_stop(leader) clears active_mask bit 0
> x86_pmu_stop(sibling) clears active_mask bit 1
> counter 1: not in active_mask, skipped
> status = 0x2 warning
>
> The overflow of the sibling is not handled. Its period is not set up
> again and its status bit stays set, because only the handled bits are
> acknowledged.
I assume Intel's PMI handler as well as generic x86_pmu_handle_irq()
are also affected?
> @@ -1014,6 +1022,10 @@ static int amd_pmu_v2_handle_irq(struct pt_regs *regs)
> if (!x86_perf_event_set_period(event))
> continue;
>
> + /* Stopped by a group throttle in an earlier iteration */
Nit: Can this comment be more explicit:
/*
* Group was throttled in an earlier iteration; re-arm the
* counter without delivering the sample.
*/
Thanks,
Ravi
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] perf/x86/amd/core: Handle overflows of counters stopped by a throttle
2026-12-01 10:56 ` Ravi Bangoria
@ 2026-12-01 16:28 ` Oancea Catalin
0 siblings, 0 replies; 4+ messages in thread
From: Oancea Catalin @ 2026-12-01 16:28 UTC (permalink / raw)
To: Ravi Bangoria
Cc: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim, 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
Hi Ravi,
thanks for the review.
On Tue, 1 Dec 2026 16:26:09 +0530, Ravi Bangoria wrote:
>> The overflow of the sibling is not handled. Its period is not set up
>> again and its status bit stays set, because only the handled bits are
>> acknowledged.
>
> I assume Intel's PMI handler as well as generic x86_pmu_handle_irq()
> are also affected?
Yes, both. I have tested them now.
For the generic handler I booted the same KVM guest with
clearcpuid=perfmon_v2, so that amd_pmu_handle_irq() is used, on
v7.4-rc5-25-g19cc08c1a6a6. It has no status register to compare with,
so nothing warns, but the overflow of the sibling is skipped in the
same way. In an MSR trace of 8 seconds there are 493 NMIs which reload
counter 0, disable both event selects and do not touch counter 1, and
the next read of counter 1, with the counter still disabled, shows that
it had overflowed. With a debug print in the handler I counted 3574 and
1977 such counters in two runs of 8 seconds.
About half of them are picked up by a later NMI after the group is
unthrottled, since that handler goes by the counter values and the
counter still reads as overflowed. That sample is late and covers more
than one period. The other half are still pending when the task is
scheduled out.
The Intel handler I tested on an i7-14700T (bare metal, P-cores), same
commit plus a debug print in handle_pmi_common(). It skips the stopped
sibling too, and there it is worse: the status has already been acked,
so no later PMI finds the counter again. The unthrottle restarts it
with its old value and it does not sample until the event is scheduled
out and in again.
On bare metal the two periods of my reproducer (20000 and 20011) hardly
ever overflow in the same PMI: 0 skips in about 11000 throttles. With
both periods at 20000 it was 6575 skips in 8 seconds. But a few are
enough. A single task with a 20000/20011 group, pinned to one P-core,
run as uid 65534, got 16 skips in 3 seconds, and after each of them the
sibling delivered no samples for 13 to 605 ms. In three such runs the
sibling delivered 23%, 42% and 55% of the samples of the leader. Opened
as two independent events, both deliver the same number. The counts
stay right in all cases, it is the samples which are missing.
This patch only changes amd_pmu_v2_handle_irq(), where the warning is.
I can send patches for x86_pmu_handle_irq() and handle_pmi_common()
with the same approach, if that is how you want it fixed.
>> + /* Stopped by a group throttle in an earlier iteration */
>
> Nit: Can this comment be more explicit:
>
> /*
> * Group was throttled in an earlier iteration; re-arm the
> * counter without delivering the sample.
> */
Sure, I will use that in v2.
Thanks,
Catalin
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-12-01 16:29 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-24 21:11 [PATCH] perf/x86/amd/core: Handle overflows of counters stopped by a throttle Oancea Catalin
2026-11-24 21:22 ` sashiko-bot
2026-12-01 10:56 ` Ravi Bangoria
2026-12-01 16:28 ` Oancea Catalin
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox