From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=80.241.56.172 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796477568; b=StrjGroye1DVhCxdw3Rb0xmZkILR0Z54OecoCixhlLnu1c5Y3ez5yfInRHfwUOjbYN7D OsPChE9tnGq2hxdFict2g7AEFS2t2ZXuxkhW/S4fYCt3ZTkKix9FYTYXOJhc36uAS4S5A 6j6MKKZTLMWDMJQRIFZgOVrdGCQrS+sLRuunefUOFgjCwzlmr+ErAm3Htsv37Q19i+nQ4 xvmYwapi82LPnXuG4KSb3vM/lnaIKCEsjxPk6aGFASyO2+uXDYOrPelzjltUVuaRhEFwW HBT8tP9ls7lkiJDDRVfg6Lguphj/7DJU/U6XSBvPHwU2HE+MsTtbmqDahPas3UXlUZQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796477568; 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=UpWRYIVB1redy2w+3pZrSiqoXgyj7korNIv2f5lv808=; b=cnnIPuKFuiV6OKOKf2M7xjpa9kNoDm66QEMyoAsRM3ugc01s7xXZ3iwwtPfxb1k5NaZN EeAL6MUUliZt7QL9TexYpRXJ8e/TAgJHBCfSTLUA9MOF6QlgFKrZbuKitBfTEK0qLFUa2 eEMGHPDlSJSPISl1YN1xRYHvJLnXkwlLyvANIyXCW+r39N28J6NCYiGVCJ8j8mz+JP/DH 1wXI8V3kpS4GcJRjbPdGmoTPHB0VATVIy8bKC1Ebsw/J/+3eo4jLhp5B3U1FZX+WD8667 aCbi6TDrb845Q8v2HGD7vVqMpmKALvk7otXyBCg8qAAWpmck1WXG0zSIgMD96a4S+CQ== 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=bFOXIafk; arc=none smtp.remote-ip=80.241.56.172 Received: from mout-p-202.mailbox.org (mout-p-202.mailbox.org [80.241.56.172]) (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 CFAB2606A6A0; Sat, 5 Dec 2026 13:32:47 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org CFAB2606A6A0 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=bFOXIafk 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-202.mailbox.org (Postfix) with ESMTPS id 4jhTnS5261zMlvh; Sat, 05 Dec 2026 14:32:44 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mailbox.org; s=mail20150812; t=1796477564; 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=UpWRYIVB1redy2w+3pZrSiqoXgyj7korNIv2f5lv808=; b=bFOXIafkhyrnYe6XqefHeFrqYcvPyRNBMDUu7R50PltB3DQee7X0YxGQmVXlzeoKfxON/K pEP4yms98pPn9nF3CQ7018zl3vTW2fYzcNICKINfgYkt1k5RnKJ3vfintCtbh1hOZBdZ8K YvG9qBbPtL0Mz1nCLgt+/uUrtDP95CZUYunrdfJeSVnUxn+H9O69z7Bb0tHbSH99K75jLD PvKmRxox7Sv3KM7MkIFZ/pc7NYL40o43+OLLM3nThOQpOzJitPK63yCfkiXWNagQ4FMQcd U7XKq4wIQ4jDjwIOoHuyYaE5DeJdibEqfNceoN+0F9igJlzqJMnZwpHmvvFNsg== 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 8/8] perf/core: Stop an event which overflows while it is throttled Date: Sat, 5 Dec 2026 15:32:21 +0200 Message-ID: <20261205133221.3294-9-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: 1kxe875txuds63qjegrcroaadr3e1qd7 X-MBO-RS-ID: fec0b869dda0a41a65c 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 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