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=1796477559; b=Rk1pJS/1wsTlLQFbVZiKXOK2PuNXDybNV8pl5nhN7fl41O3F6IYD617dnea++1d3oYc5 sXhnh7wRnkwoigeNXbeYZVTYRTSpQ4T4vUi+98uukIRurR/9Ytn5BqhceQpCvgBiMXfHZ AtFq8tR34XgYqBd4tWZ8IARtEgv9r8OeGHIQTvCR9GRNEllGi/MrkoMYcRa3bvw+x0q1P 50TjvT+/2UrsMN3zuURDfpdNFouuZz6zAkZXzuT0tYNHFTwdVhfZTpvjgSCB9mOA1smII AOJWIIJiO3twm2q0qoCzbEOfv2DLo8yRLQKZvD7UPKjwiZkVkcBX24OTFyUqQE/0fJw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796477559; 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=8Q9ahI7IHYVvt/gCcsZqFXV8+rHEupIn3mTsq74N9tk=; b=0eG4otO7Ep5tZhTKVPYLJpmrLlSU0nhgfMm9SgE6N9FLNdieMCofiitE711O9mV/aHQ6 0wayvVe5U+X2kj3Q3fMxI8s1/FiHrA288XEVl5ezCCu4uHXBt+t9R27hsY9crTsgVA5vM GGNSZmK9gTU04LDD5/EEQ9jZ4+mr2Xu1hLyrUzeYOPZunH+40Px4Jt7yO+Vv5NoZPZoEe +GPCcAE/7IqqE2auEvWweZUEXNSFEgSRjd6Yq1l7D1gTjYztc+clb/5FyTWx3jF9tUWUN KGlNrsalgLgbJYSE19yffHEacUr7A11SmfyRyB/4UgpfmTMCx9XR9grwFGEx+LpxqUQ== 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="uW41x/4z"; 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 191A1606A684; Sat, 5 Dec 2026 13:32:37 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 191A1606A684 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=uW41x/4z 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 4jhTn81jZSzMlcX; Sat, 05 Dec 2026 14:32:28 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mailbox.org; s=mail20150812; t=1796477548; 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=8Q9ahI7IHYVvt/gCcsZqFXV8+rHEupIn3mTsq74N9tk=; b=uW41x/4zTRH1jirnjLfoHkxjAyyQAMOSsS8wA2tDmcm9KD7xFXoZS3fjez/8uTltIg/BQq dvHU4VE79gBEpc7wVLIHHt0HC0NESkRpObWqknCZzxhxZKjBWzjLCkonApZPcQ7lInvNYh 7R/kexJ53QImbMrmJ34MHIjG4yc4qDKr3eHQLG6wUkQqwc/qnQYZyBpMnBGn4onZj3QC4U +ZcqSQpUs35/eMuuMRzbBOmJ/tdb1TToFW0ajHZ8MyKSMmawxF+E80RPoLiWdUsP5KKRQt ZGd39hzZnVcTrcw3edtFJXoQ7IYrGmYZDZCWnWGLTkltThG/v+S+tEesOAiCww== 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 1/8] perf/core: Disable the PMU of the context in __perf_event_period() Date: Sat, 5 Dec 2026 15:32:14 +0200 Message-ID: <20261205133221.3294-2-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: tbe9kho99pgrxghqir5f3f8byb5eqzcd X-MBO-RS-ID: 08223b44cfd8f595d6b __perf_event_period() unthrottles the group of the event when the event is throttled. It does so with event->pmu disabled. For a software event in a group with hardware events that is the software PMU, and the hardware events keep counting. A hardware event which has just been started can then overflow and throttle the group while perf_event_unthrottle_group() is still walking it. The rest of the walk starts the remaining events of a group which is throttled again, and the next unthrottle starts them a second time: WARNING: arch/x86/events/core.c:1698 at x86_pmu_start+0x191/0x1e0 perf_event_unthrottle_group perf_adjust_freq_unthr_events perf_adjust_freq_unthr_context perf_event_task_tick Until then such an event counts with its throttle mark set. The next patch relies on a marked event being stopped. Disable the PMU of the event's context instead, as the tick does. In a group with hardware events that is their PMU. Tested in a KVM guest on a Ryzen 9 7945HX with a page-faults leader, a kernel cycles event and an instructions event, the task setting the period of the leader in a loop, kernel.perf_event_max_sample_rate=100: without the fix the warning shows in the first second; with it there is none in 2.1 million calls. 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, and first for a reason: v4 without it can hang. perloop.c below is the test of the changelog: a page-faults event (period 2^40) leads a group with a kernel cycles event with a short period and an instructions event, and the task sets the period of the leader in a loop. It needs root, or perf_event_paranoid at 1 or lower. "Without the fix" is mainline (d06ce6f1eb5e), "with it" is mainline with this patch alone. 6.18.66 warns in the same way. With the default sample rate limit mainline warns with a period of 2 for the cycles event, also as uid 65534 with perf_event_paranoid at 1; with one sample per tick any short period does. With the last argument the instructions event has a short period too, and it is the one which is started in a throttled group. On mainline its next overflow removes the mark, and it is throttled like any other event. With v4 patch 1/4 (2/8 here) its overflows are not accounted while it is marked, so nothing throttles it. Four runs of 10 seconds with periods from 100 to 5000, same guest: mainline runs, with the warning above v4 the guest stops answering in the third run (periods 1000 and 500) this patch alone runs, no x86_pmu_start() warning all eight runs, no warning "This patch alone" still gives the warning which patch 3 fixes, as mainline does. So please do not apply v4. With the generic handler (guest booted with clearcpuid=perfmon_v2) the warning can still show in that second mode, also with the whole series. That is a different problem. Debug prints show x86_pmu_handle_irq() throttling the group from an NMI which KVM delivers while the PMU is disabled, for a counter which wrapped between its reload and its stop in the handler and was then started again without a reload. As far as I can see the tick's unthrottle can meet the same. The PerfMonV2 handler stops all counters while it runs and does not have it: no such start and no warning in 18 runs. On bare metal (Ryzen 9 7945HX) mainline gives the x86_pmu_start() warning with both handlers. The series gives none with either, in six runs of 10 seconds. With the generic handler such a wrapped counter also makes this test slow there: 177200 calls in 10 seconds with a cycles period of 200 or 800, over 3 million in the other four runs. amd_pmu_disable_all() waits up to 50 us for a counter which looks overflowed, and with this patch the ioctl disables that PMU. An MSR trace in the guest shows the 50 reads of the counter in every call. On mainline the counter is in the same state; only this ioctl does not wait for it there. The tick (perf_adjust_freq_unthr_context()) already disables pmu_ctx->pmu, and event_sched_in() clears the mark inside the PMU transaction of the group. ---- perloop.c ---- // SPDX-License-Identifier: GPL-2.0 /* * perloop: can the NMI of a hardware event throttle a group while * PERF_EVENT_IOC_PERIOD on a software event of the group unthrottles it? * __perf_event_period() disables the PMU of the event it is called for, here * a software event, so the hardware events of the group can interrupt the * walk of perf_event_unthrottle_group(). Needs root or perf_event_paranoid * at 1 or lower (kernel counting). With the default sample rate limit use a * period of 2; with kernel.perf_event_max_sample_rate=100 the first overflow * throttles and any short period does. * * One group on the calling task, pinned to one CPU: * S page-faults, sample_period 2^40 (leader, software) * H cycles, kernel only, sample_period (sibling, NMI) * H2 instructions, kernel only, sample_period (sibling) * The task sets the period of S in a loop. Each call unthrottles the group: * S, then H is started, then H2. If H overflows before H2 has been * unthrottled, its NMI throttles the group, and the walk then starts H2 in a * throttled group. The next unthrottle starts H2 a second time: * WARN_ON_ONCE() in x86_pmu_start(). The result is in the kernel log. * * Without H2 does not overflow (period 2^40). With a short one H2 * runs with its throttle mark set until it overflows. * * usage: perloop [h2period] */ #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include 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 s = { 0 }, h = { 0 }; uint64_t end, period = 1ull << 40, calls = 0, failed = 0; cpu_set_t set; int fs, fh, fh2, i; if (argc < 4) { fprintf(stderr, "usage: perloop [h2period]\n"); return 2; } CPU_ZERO(&set); CPU_SET(atoi(argv[1]), &set); if (sched_setaffinity(0, sizeof(set), &set)) { perror("sched_setaffinity"); return 1; } s.size = sizeof(s); s.type = PERF_TYPE_SOFTWARE; s.config = PERF_COUNT_SW_PAGE_FAULTS; s.sample_period = period; s.disabled = 1; fs = ev_open(&s, -1); if (fs < 0) { perror("open S"); 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.exclude_user = 1; h.exclude_hv = 1; fh = ev_open(&h, fs); if (fh < 0) { perror("open H"); return 1; } h.config = PERF_COUNT_HW_INSTRUCTIONS; h.sample_period = argc > 4 ? strtoull(argv[4], NULL, 0) : period; fh2 = ev_open(&h, fs); if (fh2 < 0) { perror("open H2"); return 1; } ioctl(fs, PERF_EVENT_IOC_ENABLE, PERF_IOC_FLAG_GROUP); end = now_ns() + strtoull(argv[2], NULL, 0) * 1000000000ull; do { for (i = 0; i < 100; i++) if (ioctl(fs, PERF_EVENT_IOC_PERIOD, &period)) failed++; calls += 100; } while (now_ns() < end); ioctl(fs, PERF_EVENT_IOC_DISABLE, PERF_IOC_FLAG_GROUP); printf("hperiod=%s h2period=%s period_calls=%llu failed=%llu\n", argv[3], argc > 4 ? argv[4] : "2^40", (unsigned long long)calls, (unsigned long long)failed); return 0; } ---- end of perloop.c ---- kernel/events/core.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/kernel/events/core.c b/kernel/events/core.c index 4ab3919ac78e..da4f217d633e 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -6564,7 +6564,12 @@ static void __perf_event_period(struct perf_event *event, active = (event->state == PERF_EVENT_STATE_ACTIVE); if (active) { - perf_pmu_disable(event->pmu); + /* + * Disable the PMU of the context, which for a software event in + * a group with hardware events is theirs: their NMI must not + * throttle the group while it is unthrottled below. + */ + perf_pmu_disable(event->pmu_ctx->pmu); event->pmu->stop(event, PERF_EF_UPDATE); } @@ -6580,7 +6585,7 @@ static void __perf_event_period(struct perf_event *event, */ if (event->hw.interrupts == MAX_INTERRUPTS) perf_event_unthrottle_group(event, true); - perf_pmu_enable(event->pmu); + perf_pmu_enable(event->pmu_ctx->pmu); } } -- 2.56.1