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=1796477556; b=BCJPQc4S/5YZTXG1V76xBHxQX5KT6WBDk41E4OvjOgBs1Hp2hZ+Rvx1fHD9Lw2yvdc2b dMw1H57bnR2T055sLZ+M51w+JzA7h0l8eELPygoLWLuVxpuQEbpsiKYOJhVWk+z/XNDSW zhKT5FqRIA4q9oZFwJA4/bZOx9yUhpzMlvAn5m8XJ0tjUr6khpYoxTRfnnKYBpPbInJWK tkRZ0SLJEnHHQdYwhyL7LL6ya3JKIxiCEqS0dwEHOTnq0Ogs1+it3zy0Exz+tfugLhkI2 06fw8vHN2gf/0FSYLqVqYf1btWKi6c1KyoY/uPIreCroDXbBr8CzF3DSAsBIY0j8FfA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796477556; h=DMARC-Filter:Received:DKIM-Signature:From:To:Cc:Subject:Date: Message-ID:X-Mailer:MIME-Version:Content-Transfer-Encoding: X-MBO-RS-META:X-MBO-RS-ID; bh=qo1B8dm9JL8J5d7peAXd49KeAQH1vQOrEwUAWk0Rbmc=; b=w8EiHgZy5y7eHwSgUaptMluS90MfTYBdmauAYj4phtv9YVX8TtiWKv+g6fqvJt8Tv0qz kc6xMlCiOnw277NbXppQDdBBtWaoQcfMaIso7tyiGMG4vSWVvpiFuR+aRN9xD0vqwy/iQ nmf/JWtKQiyVx1JoSBEHohHN0k9SzFjPUJWcx6A6bFyAvjtm+NNQj2q24WJthCiMvPkYL JvdITkL9RbmzeLWps69Y/nYf4TA8VFCpsnJ6dCYfLpZTzqePSfFy1t8YvmB9axcYeBaar hRxH/gYAg7jHpR0oJIqqzafLuHdIyZseaKPFaOxJeVnCjHZ4AxQ3DZaWsBWIwTqwhXg== 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=rI8ULma7; 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 3341A606A322; Sat, 5 Dec 2026 13:32:34 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 3341A606A322 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, unprotected) header.d=mailbox.org header.i=@mailbox.org header.a=rsa-sha256 header.s=mail20150812 header.b=rI8ULma7 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 4jhTn55zdpzKmZ5; Sat, 05 Dec 2026 14:32:25 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mailbox.org; s=mail20150812; t=1796477545; 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; bh=qo1B8dm9JL8J5d7peAXd49KeAQH1vQOrEwUAWk0Rbmc=; b=rI8ULma7JzSEwVKPeeDabapCDflXC9P1DRpRSeTQkJgA0PbpQlFkkuy1cIxwHxRa1W9Rhg yU62tIrpYrtfjv8M7iHEO4RznuWwSE83a7l0WFQLYlYBz/t5vJI0l9Z//bO6iBcvfyaog+ mfZ+fe4+r//0G72LzjSAgMKKlJlITTgXfNDpoJkpppUD3UkyqKyXUfsELHC5hL69Rye9q5 FF1tlg3fSsye8RSTQru1Ufn+Fq9WHV8WzPS9oTWx/xnsjhynN9dYqZQnbOtNQLKv8NhN9r Wy+QbQfSJ07GxpAa7IsJY5m8ijKpECu1dgeX5f/y91urR7YWA7lpCs4w+MwzCQ== 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 0/8] perf/x86: Handle overflows of counters stopped by a throttle Date: Sat, 5 Dec 2026 15:32:13 +0200 Message-ID: <20261205133221.3294-1-john3@mailbox.org> X-Mailer: git-send-email 2.56.1 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: ddznrkwwetqqaifckcioi9tq431qretw X-MBO-RS-ID: 89960125261ad982843 Since 9734e25fbf5a ("perf: Fix the throttle logic for a group") a throttle stops the whole group, from inside the PMU interrupt handler. The x86 handlers test cpuc->active_mask while they walk the counters, so they skip a group member which was just stopped, also when its overflow is pending in the same interrupt: - amd_pmu_v2_handle_irq() warns "New overflows for inactive PMCs"; - x86_pmu_handle_irq() notices the overflow in a later NMI, or never; - handle_pmi_common() does not set the period up again, and the event stops sampling until it is scheduled in again. Patches 3 to 5 make the three handlers walk the counters which were active on entry, so that such an overflow is handled like any other. Patch 2 is needed for that: the overflow of an event which is already throttled must not go through the throttle accounting again. It also fixes a case in which a group is not unthrottled today. Patch 2 relies on a throttled event being stopped. Patch 1 fixes the one place I found where that does not hold: PERF_EVENT_IOC_PERIOD on a software event can start a hardware event in a throttled group. Patch 6 closes what patch 2 leaves open. The throttle mark is a value of the overflow count, and other code which writes the count can still remove it. The mark gets a field of its own. Patch 7 uses that field to keep an NMI from throttling a group a second time while a software event is throttling it. Patch 8 stops an event which overflows while it has the mark. With patch 2 nothing else would throttle an event which was started with it. v5: - Patch 1 is new. v4 can hang without it: an event which __perf_event_period() starts in a throttled group is not throttled any more once patch 2 skips its accounting. Mainline only warns there. I found this while testing v5. Please do not apply v4. - Patch 6 is new. Sashiko's review of v4 patch 1 said that the check for the throttle mark and the count that follows are not atomic for a software event, and that the mark is not stored atomically on 32-bit. The first reproduces, on mainline and less often with v4 (software leader, hardware sibling). The NMI watchdog's handler and the BTS drain write the count too and remove the mark as well. - Patch 7 is new. A test of patch 6 with three events in a group gave a warning in x86_pmu_stop(), which mainline gives too. - Patch 8 is new. It makes patch 2 safe against an NMI which throttles a group while the group is being unthrottled. A KVM guest gets such NMIs with the generic handler. - Patch 3 has Ravi's Reviewed-by. - Patches 2 to 5 are v4 patches 1 to 4, unchanged. Only the table in the changelog of patch 2 has new labels, "without fix" and "with fix" (Dapeng). - Tested again, on d06ce6f1eb5e. v4: https://lore.kernel.org/all/20261203113922.77972-1-john3@mailbox.org/ v3: https://lore.kernel.org/all/20261203062703.15346-1-john3@mailbox.org/ v2: https://lore.kernel.org/all/20261201163432.44692-1-john3@mailbox.org/ v1: https://lore.kernel.org/all/20261124211146.61378-1-john3@mailbox.org/ Tested on d06ce6f1eb5e with perf_event_paranoid at 2: - AMD Ryzen 9 7945HX, bare metal, one boot per kernel and handler. Without the series the PerfMonV2 handler warns (uid 65534), and the tests of patches 1 and 7 (root) give the x86_pmu_start() and x86_pmu_stop() warnings with both handlers. With the series there is no kernel warning in either boot. An MSR trace of two grouped cycles events shows the stopped counter being handled in the same NMI in all 822 cases with the PerfMonV2 handler and in all 822 with the generic one, and never without the series. With the PerfMonV2 handler, perf record of that group gives 339563/339561 samples without and 309721/309721 with the series, and the group of patch 2 (as root, leader period 8000) gets 260 THROTTLE and 224 UNTHROTTLE records in 5 seconds without the series, 1278 of each with it. - The same CPU as a KVM host, guest with KASAN and lockdep, as root and as uid 65534. PerfMonV2 handler: the warning shows in 2 of 2 boots without the series and in 0 of 5 with it. An MSR trace shows the stopped counter being handled in the same NMI in all 28657 cases, and in all 21202 with the generic handler (booted with clearcpuid=perfmon_v2); without the series in none of 3487. - Intel, on a Core i7-14700T, as an unprivileged user. Samples of two grouped events with period 20000 in 4 seconds, without and with the series: cycles, P-core 376871 / 340116 301194 / 301194 cycles, E-core 388157 / 361422 387838 / 387838 instructions (leader on a fixed counter), P-core 342983 / 386591 388141 / 388141 Also tested: PEBS events in the group and frequency mode. - The group of patch 2 (cycles leader, tracepoint sibling, as root), 5 seconds on the Intel machine: 150 THROTTLE and 76 UNTHROTTLE records without the series, 1314 of each with it. - The numbers for patches 1, 6, 7 and 8 are in their changelogs. The series is against mainline and applies to tip perf/urgent. On tip perf/core patches 3 to 5 need their context adjusted (the declarations of the sample data, and the PEBS drain lines in handle_pmi_common()). knc_pmu_handle_irq(), p4_pmu_handle_irq() and zhaoxin_pmu_handle_irq() have the same loop. I have no such hardware and did not change them. Oancea Catalin (8): perf/core: Disable the PMU of the context in __perf_event_period() perf/core: Do not account overflows of a throttled event perf/x86/amd/core: Handle overflows of counters stopped by a throttle perf/x86: Handle overflows of counters stopped by a throttle perf/x86/intel: Handle overflows of counters stopped by a throttle perf/core: Keep the throttle mark out of hwc->interrupts perf/core: Throttle an event only once perf/core: Stop an event which overflows while it is throttled arch/x86/events/amd/core.c | 10 +++++- arch/x86/events/core.c | 10 +++++- arch/x86/events/intel/core.c | 11 ++++++- include/linux/perf_event.h | 7 +++++ kernel/events/core.c | 59 ++++++++++++++++++++++++++++-------- 5 files changed, 81 insertions(+), 16 deletions(-) base-commit: d06ce6f1eb5eeffd4ebcfde345dc34a98131d106 -- 2.56.1