From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=80.241.56.171 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796477566; b=XWjRVvukprEjbBGhE1aBM/RCUWC4t0luzIEzMF7NBmWJzC2HK/nl5UWGWFuWqZZpatWQ 0eld8vYlAzixj95UYyvln6YGsiBwpSJdR7cdevNtUsYGqBrz0oWiVpn3b37JyISdxv4p+ o3m1bKBhjiqGYcfO5zNE/P29RB5qZemHL2L5cllMD9VXA4D0xnkuwurNSXPxU+l284VjT CPkrrK9rf1BXtn+4eWVZCtzqt+Z6adfhxkxF2QTtIoRo6bA8BCn+wSFJNSWjS+UlWblFp VNsSMUGsBlEVdjWnwjhYRaXHL+GGY38L/9A/ULo64heaKBeLq2PKi8hWuOSm7jZudng== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796477566; 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=IJpBjoMbiwt44vKPhcASDlWtHhODv6jPOvHtO5OfXnI=; b=DAiXV2w6RxXUKLJLGtai5Ldp6uWJ+OyTqu1Yk3ujvXfBlQ/MsVYkQHo6MqDzhMIkwhuJ hI9jmm+Hz3/oy14WY9l8XRUAoL7qknJJFs/0xd2jN6UgDz09jEOjXzCqecfn3xG8vY+uv PmtqXTXjmscx+mStGouyk9OqhlbhOLSA4sJDj7jJImB+vzt3FcaxQxmc0x6eGhv1DoHwM g85pYOX0Ji01qlJyZtTN1zuxv0M1RjoZHRCajRGMnne/sbeEJr5BhbZSud8nVCkHmwcAG kMOVjo1S0Cg14NtSVDYJfBocakv7oefZrDnDVRe9wBKS0ozmA7Cyy6h7W9fWTcv4BUw== 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=QPeoqjX0; arc=none smtp.remote-ip=80.241.56.171 Received: from mout-p-201.mailbox.org (mout-p-201.mailbox.org [80.241.56.171]) (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 F139F606A33E; Sat, 5 Dec 2026 13:32:45 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org F139F606A33E 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=QPeoqjX0 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-201.mailbox.org (Postfix) with ESMTPS id 4jhTnQ3R6NzMllT; Sat, 05 Dec 2026 14:32:42 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mailbox.org; s=mail20150812; t=1796477562; 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=IJpBjoMbiwt44vKPhcASDlWtHhODv6jPOvHtO5OfXnI=; b=QPeoqjX0fJ94yUR9kkhIVBUbCUa3zYlf/+qMQ342O9sa7+tc8Lh+Xi+4NOMaSuX5eyse9Z Vrb5rPgcRsFMYJoz9i+sw5rPeMjGxltLyJlBEqvmpaOXCmozzfoW9TNJOkTBvSv3jKFe0p +8J3X7WNVCgqoP4XiIWrA9wrdVGuQfEjPzqGi+cUhmRl8GPSVslGaV5hQuX/ctfz7/HhOX MKxzMBDv+hQcJPZ6+ylyL372uZpUtTFJADVpsH0QyY3XVeZqHMyFUxPR3S9bNBC2vb3zHB bdOmtS8qnltUzow4Z6IcIQsIJgy0zMouVKvXkuCEeWhnP5FBSbZjhEZYCdW6dw== 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 7/8] perf/core: Throttle an event only once Date: Sat, 5 Dec 2026 15:32:20 +0200 Message-ID: <20261205133221.3294-8-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: eimxed1kaatuzxbco8oebmempw5cp79p X-MBO-RS-ID: 8d89b81a2ddd040d110 A software event throttles its group with NMIs enabled. If the NMI of a hardware event of the group arrives while perf_event_throttle_group() is on its way through the group, and that event is at its limit too, the NMI throttles the group a second time. The leader gets two THROTTLE records, and an event whose stop was interrupted is stopped again from inside it: WARNING: arch/x86/events/core.c:1787 at x86_pmu_stop+0x266/0x280 perf_event_throttle_group __perf_event_overflow perf_swevent_event perf_tp_event perf_trace_sched_stat_runtime Set the mark with xchg() and leave an event alone which has it already. Each event is then stopped and logged by one throttle only. Tested in a KVM guest on a Ryzen 9 7945HX with two kernel cycles events and a sched_stat_runtime tracepoint event in a group and kernel.perf_event_max_sample_rate=100, in 20 runs of 10 seconds: without the fix 882 of 203286 throttles were logged twice and the warning showed; with it, none of 202494 and no warning. 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. Found while testing patch 6 with groups of three events, and it needs the field of patch 6. swloop3 is swloop.c from the notes of patch 2 with a second kernel cycles event between the leader and the tracepoint event: fh = ev_open(&h, -1); ... + h.sample_period += 7; + h.disabled = 0; + fh2 = ev_open(&h, fh); ... + ioctl(fh2, PERF_EVENT_IOC_SET_OUTPUT, fh); It needs root, or perf_event_paranoid at 1 or lower. With the default limit the events have to reach it at the same moment, which happens here with periods of 8000 to 12000: as uid 65534 with perf_event_paranoid at 1, mainline warns in the first run of 10 seconds. With kernel.perf_event_max_sample_rate=100 every overflow throttles. "Without the fix" in the changelog is patches 1 to 6. Mainline (d06ce6f1eb5e) gives the same warning in the same test, also on a Core i7-14700T, and so does 6.18.66. On bare metal (Ryzen 9 7945HX, three runs of 10 seconds, one sample per tick) mainline logs 313 of 8094 throttles twice with the PerfMonV2 handler and 204 of 7957 with the generic one, and warns with both. With the series it is none of 7635 with each handler, and no warning. The same warning with the same call chain was reported in February, for 6.19-rc7, and not explained then: https://lore.kernel.org/all/PS1PPF7E1D7501F6302C4D63E37AB0D5FC5AB98A@PS1PPF7E1D7501F.apcprd02.prod.outlook.com/ I have not run that reproducer, so I can not say that it is this case. The tick disables the PMU for the unthrottle. That is not an option for the throttle: its walk also runs inside the PMU's own interrupt handler. kernel/events/core.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/kernel/events/core.c b/kernel/events/core.c index 59ea05f9594e..fdc7335c7321 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -2806,7 +2806,13 @@ static void perf_event_throttle(struct perf_event *event) if (event->state != PERF_EVENT_STATE_ACTIVE) return; - event->hw.throttled = 1; + /* + * An NMI can throttle the group while a throttle from another context + * is half way through it. Each event is stopped by one of them only. + */ + if (xchg(&event->hw.throttled, 1)) + return; + event->pmu->stop(event, 0); if (event == event->group_leader) perf_log_throttle(event, 0); -- 2.56.1