From: Oancea Catalin <john3@mailbox.org>
To: Peter Zijlstra <peterz@infradead.org>,
Ingo Molnar <mingo@redhat.com>,
Arnaldo Carvalho de Melo <acme@kernel.org>,
Namhyung Kim <namhyung@kernel.org>
Cc: Kan Liang <kan.liang@linux.intel.com>,
Mark Rutland <mark.rutland@arm.com>,
Alexander Shishkin <alexander.shishkin@linux.intel.com>,
Jiri Olsa <jolsa@kernel.org>, Ian Rogers <irogers@google.com>,
Adrian Hunter <adrian.hunter@intel.com>,
James Clark <james.clark@linaro.org>,
Thomas Gleixner <tglx@kernel.org>, Borislav Petkov <bp@alien8.de>,
Dave Hansen <dave.hansen@linux.intel.com>,
x86@kernel.org, "H. Peter Anvin" <hpa@zytor.com>,
linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org,
shicenci@gmail.com, Ravi Bangoria <ravi.bangoria@amd.com>,
Dapeng Mi <dapeng1.mi@linux.intel.com>
Subject: [PATCH v5 6/8] perf/core: Keep the throttle mark out of hwc->interrupts
Date: Sat, 5 Dec 2026 15:32:19 +0200 [thread overview]
Message-ID: <20261205133221.3294-7-john3@mailbox.org> (raw)
In-Reply-To: <20261205133221.3294-1-john3@mailbox.org>
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
next prev parent reply other threads:[~2026-12-05 13:32 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-05 13:32 [PATCH v5 0/8] perf/x86: Handle overflows of counters stopped by a throttle Oancea Catalin
2026-12-05 13:32 ` [PATCH v5 1/8] perf/core: Disable the PMU of the context in __perf_event_period() Oancea Catalin
2026-12-05 13:58 ` sashiko-bot
2026-12-05 13:32 ` [PATCH v5 2/8] perf/core: Do not account overflows of a throttled event Oancea Catalin
2026-12-05 13:32 ` [PATCH v5 3/8] perf/x86/amd/core: Handle overflows of counters stopped by a throttle Oancea Catalin
2026-12-05 13:32 ` [PATCH v5 4/8] perf/x86: " Oancea Catalin
2026-12-05 13:32 ` [PATCH v5 5/8] perf/x86/intel: " Oancea Catalin
2026-12-05 13:32 ` Oancea Catalin [this message]
2026-12-05 13:57 ` [PATCH v5 6/8] perf/core: Keep the throttle mark out of hwc->interrupts 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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261205133221.3294-7-john3@mailbox.org \
--to=john3@mailbox.org \
--cc=acme@kernel.org \
--cc=adrian.hunter@intel.com \
--cc=alexander.shishkin@linux.intel.com \
--cc=bp@alien8.de \
--cc=dapeng1.mi@linux.intel.com \
--cc=dave.hansen@linux.intel.com \
--cc=hpa@zytor.com \
--cc=irogers@google.com \
--cc=james.clark@linaro.org \
--cc=jolsa@kernel.org \
--cc=kan.liang@linux.intel.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-perf-users@vger.kernel.org \
--cc=mark.rutland@arm.com \
--cc=mingo@redhat.com \
--cc=namhyung@kernel.org \
--cc=peterz@infradead.org \
--cc=ravi.bangoria@amd.com \
--cc=shicenci@gmail.com \
--cc=tglx@kernel.org \
--cc=x86@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox