From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=80.241.56.161 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796477564; b=jdwhQqFdxCoWPZ82DbSdARyN9U7LJqHTwEsFgYt25kOfm0J7lvcPhpSjqm6956EzSty2 JpBAlah4mLaNxSNLOujIZ/4IlM+XAmuw744CmsyvUdkRYGsZMpRbgVkID3gEM0rahnTTC IfdHZvJTEPW2ebr0pmDUMZBhWLTAEz+EIT38+oUUnES5sObkNC+RdoLIFuccPB26xzJ0C Jq+K7bsShkFpFIoq3+jexVvyZvBaEOYEOmhYI/ofI1VAvc38AZ5WWjRht2ZDwyZPb8vTj zD+/nd7GmcOFbu/KwAsDyS1lugPeh6kln9dxxqc+5OVuVfewsNgT7vtmhjQ3CObSiVQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796477564; h=DMARC-Filter:Received:DKIM-Signature:From:To:Cc:Subject:Date: Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding:X-MBO-RS-META:X-MBO-RS-ID; bh=534OMIVekV4hU9AisP0IfcimngkVaxv6yIDBddLLrNA=; b=HkO4HhhBVoEFg/gB8CwfYWF40aTPsE9OygSqIadz3qhpnPIyzmnkY0VOJGyrZEd5B86V qlWi1T4WAgS9nipT+pSSG2dwEydZeYHL5e/kUpfC0IvX2XYDmQ5TuLUYCyaJKIpzsmieG H3qYBDnawbpswrRwYL0E+ob2F5pfHoWhr2jL83GdkMKeQLkyBofJdUzllVP2AhglZsXGn s3lmoUfgh3FPxzUnsFmRKqkRC1egIsZN3g7r5h3YoHwTBn2tmRXCEVt0ftU150P4aKAwQ 1mbwaEH7S2aExy/VtbLCHUXCo3N00O9pqqBRiz2HX/b/Qmh2SOvo9wpjMh4kGhdEB3g== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=mailbox.org; spf=pass smtp.mailfrom=mailbox.org; dkim=pass header.d=mailbox.org header.i=@mailbox.org header.a=rsa-sha256 header.s=mail20150812 header.b=J+T4sE8R; arc=none smtp.remote-ip=80.241.56.161 Received: from mout-p-103.mailbox.org (mout-p-103.mailbox.org [80.241.56.161]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C17E0606A332; Sat, 5 Dec 2026 13:32:43 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org C17E0606A332 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=mailbox.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=mailbox.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=mailbox.org header.i=@mailbox.org header.a=rsa-sha256 header.s=mail20150812 header.b=J+T4sE8R Received: from smtp2.mailbox.org (smtp2.mailbox.org [IPv6:2001:67c:2050:b231:465::2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519MLKEM768 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-103.mailbox.org (Postfix) with ESMTPS id 4jhTnN1CSczKnSR; Sat, 05 Dec 2026 14:32:40 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mailbox.org; s=mail20150812; t=1796477560; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=534OMIVekV4hU9AisP0IfcimngkVaxv6yIDBddLLrNA=; b=J+T4sE8RReOV/bRjiz7xmzGaFg5A5bJsRvy0CII8r7fwyIat0/OUfTzZkM1v4WOHJ9dWMX mcYZXYjYof8xqVUJZ/xHG5Jzfo0kHQRhxj0Qv/m80YfR4JA+g+cglRiSd5Y/0DfNE0f7TW gV63p613GPv1fwonHJUNx0Io0FfbPsPmYez0uyzXu5NUBZj+YbPDixJJ3IAfKWcPRdMvQB E5sQT4n6dmRHNZg/Bqws8j3JDR78bkhpvEm/73T1R2qrprXFg0LpOdRWAyUoptWxw0AH5U ie8Q8Ii3gu/izWaix7de5lbIO+Jq2wjuGht7vr94vYJumElQAFt69ZCO7X/pgw== From: Oancea Catalin 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@kernel.org, "H. Peter Anvin" , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, shicenci@gmail.com, Ravi Bangoria , Dapeng Mi Subject: [PATCH v5 6/8] perf/core: Keep the throttle mark out of hwc->interrupts Date: Sat, 5 Dec 2026 15:32:19 +0200 Message-ID: <20261205133221.3294-7-john3@mailbox.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261205133221.3294-1-john3@mailbox.org> References: <20261205133221.3294-1-john3@mailbox.org> X-Mailing-List: linux-perf-users@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-MBO-RS-META: kmyntr1xaec6uxkpgyhh64fte3a66o1k X-MBO-RS-ID: 9be0861a1713809c165 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 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 (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 [buffer pages, power of 2] */ #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include #include #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 [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