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=1796297975; b=jIP09lbRiice9oQP1uCPGsNxD6SABIFmaIr+nMAs84X/Q+kRraF4eczGfzWIhla0y+14 MQSx09YZvRXvMucJeJ8kyr1F/xtxGOEXf9xQuziAdrBwp/oM3RE+zzC+uBbNa7ihYTYjp fqRCRceRqmlu9teejj9v9Uekn+GK+fxnRQlO+TYEBpmoAgJKXsUeiKIjFJKGKYXQLkRru j0wV1UACD2hgfKmmLGQ5zZxeveM5Znwk2YVrY0oW+YtKw+N/xEdPgOoqP8JmHlJhX2sX7 RxbidOqTRukdulsDGABui0CS9/ogiQo7okTfQDrSiY6cswBDwlz3KaazNaO0ZKTFCEA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796297975; 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-ID:X-MBO-RS-META; bh=C8IdeXkckM/Ftp76RKi0yNK5DFo3QDUpFzjawLjsIqM=; b=0KryxSMTS4cod4KTI7EkYwdZPT472+Ot0z5o3hJNri5u7cdkeFtnVUFuBgdDYYsoT+WX DpUnkvQUcxMTkhcsnQWk4W8rVNajVqaXZT5ET5M9X8iDKVUkh8CZp1i3l+EyFXsMHZQTq cTo9NXwtbptWBpWK4KsoGr8GDHkmIvZK6oYt09FkNxNSy5LdeDDtEVG0/AoZnq+wj7HLO uNGIsQsMc4LsOFYFfNkbJnoGN1wENd8ELimkXBn1vwHLQmJL+SsGa0GAompkq96w8sHDO MpDFfcMVg7WPNEVZayhZKfFHG9owmfZzP43pxfxr+STAPed2ZxGDF6DB+1Y1g8pjzag== 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=L4n4uLh+; 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 6DFAA606A685; Thu, 3 Dec 2026 11:39:34 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 6DFAA606A685 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=L4n4uLh+ Received: from smtp202.mailbox.org (smtp202.mailbox.org [IPv6:2001:67c:2050:b231:465::202]) (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 4jgCMk4bwRzMllg; Thu, 03 Dec 2026 12:39:30 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mailbox.org; s=mail20150812; t=1796297970; 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=C8IdeXkckM/Ftp76RKi0yNK5DFo3QDUpFzjawLjsIqM=; b=L4n4uLh+psljQKqkn8st5exXltSnB45qs7IMJZHYuevaFHSxvD34R957wrRvs1jCE/y/4K 4Ddesz+NNkjlGCA03c0xYYKdYXGmNbtNftM0DXpUOBloTo4a2T6qX9Le1XHlWPJFUi3NlC Xb+Bgr1sanApmCRdH+0vq7qVlR59HjMfN3OlN0AAt+oTI/fNI4GOranVGXFqMoTGAwh0pe 0wLXpHWS4zZ5AzT/UrMbyJ1Nahxw/Jby6teZkCCdVNWCgRNXGMsfOAZtclCMbXwv/6UrmT OVxxf+sWeNCJsi0GfUfCRUkMJIVIAIOoVg0Ss4h4HDC7wO2LWjRqZ4pSh/SW9g== 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, Ravi Bangoria , Dapeng Mi Subject: [PATCH v4 2/4] perf/x86/amd/core: Handle overflows of counters stopped by a throttle Date: Thu, 3 Dec 2026 13:39:20 +0200 Message-ID: <20261203113922.77972-3-john3@mailbox.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261203113922.77972-1-john3@mailbox.org> References: <20261203113922.77972-1-john3@mailbox.org> X-Mailing-List: linux-kernel@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-ID: 7ae0b3318f3ebcaecee X-MBO-RS-META: x6p5pkk7957cmf5y4oippym1n99qcu3p Since commit 9734e25fbf5a ("perf: Fix the throttle logic for a group") a throttle stops the whole event group. perf_event_overflow() can therefore stop counters which the NMI handler has not reached yet. amd_pmu_v2_handle_irq() tests cpuc->active_mask while it walks the counters. It skips a sibling which was stopped that way, also when the sibling has overflowed in the same NMI. The overflow is not handled, the status bit stays set and the handler warns: New overflows for inactive PMCs: 2 WARNING: arch/x86/events/amd/core.c:1031 at amd_pmu_v2_handle_irq+0x86e/0x9a0 An unprivileged task can trigger this with perf_event_paranoid at its default of 2: a group of two sampling events with short periods on the task itself, which spins in user space. Take a copy of active_mask before the loop and walk the counters which were active on entry. The overflow of a counter which was stopped in the meantime is then handled like any other. The problem was found with syzkaller. An AI coding assistant (Claude Code) was used in the analysis and in writing the reproducer and the patch. Fixes: 9734e25fbf5a ("perf: Fix the throttle logic for a group") Signed-off-by: Oancea Catalin Cc: stable@vger.kernel.org --- The reproducer, reduced from a syzkaller program: ---- repro.c ---- // Reproducer for "WARNING in amd_pmu_v2_handle_irq" (New overflows for inactive PMCs). // Reduced from a program found by syzkaller. // // Each worker opens a group of two hardware sampling events on its own task, // both with a short fixed period, enables them and burns CPU in user space // for a moment, then exits. NPROC workers do this in a loop. // // Build: gcc -O2 -static -o repro repro.c Run: ./repro (stop with ^C) #define _GNU_SOURCE #include #include #include #include #include #include #define NPROC 6 static int ev(unsigned long long config, unsigned long long period, int group) { struct perf_event_attr a; memset(&a, 0, sizeof(a)); a.type = PERF_TYPE_RAW; /* CPU hardware counter */ a.size = sizeof(a); a.config = config; a.sample_period = period; /* short fixed period: many interrupts per tick */ a.sample_type = PERF_SAMPLE_IP; a.disabled = 1; a.exclude_kernel = 1; /* what an unprivileged user may ask for */ return syscall(SYS_perf_event_open, &a, 0 /* this task */, -1 /* any CPU */, group, 0); } static void once(void) { volatile unsigned long n; int leader = ev(0x76, 20000, -1); /* 0x76: CPU cycles */ ev(0x76, 20011, leader); prctl(PR_TASK_PERF_EVENTS_ENABLE, 0, 0, 0, 0); for (n = 0; n < 30000000; n++) ; } int main(void) { int i; for (i = 0; i < NPROC; i++) { if (fork()) continue; for (;;) { pid_t p = fork(); if (p == 0) { once(); _exit(0); } waitpid(p, NULL, 0); } } pause(); return 0; } ---- end of repro.c ---- arch/x86/events/amd/core.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/arch/x86/events/amd/core.c b/arch/x86/events/amd/core.c index 49b6b8fce566..c1e2d169210a 100644 --- a/arch/x86/events/amd/core.c +++ b/arch/x86/events/amd/core.c @@ -958,6 +958,7 @@ static int amd_pmu_v2_handle_irq(struct pt_regs *regs) struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); static atomic64_t status_warned = ATOMIC64_INIT(0); u64 reserved, status, mask, new_bits, prev_bits; + DECLARE_BITMAP(active, X86_PMC_IDX_MAX); struct perf_sample_data data; struct hw_perf_event *hwc; struct perf_event *event; @@ -994,8 +995,15 @@ static int amd_pmu_v2_handle_irq(struct pt_regs *regs) /* Clear any reserved bits set by buggy microcode */ status &= amd_pmu_global_cntr_mask; + /* + * perf_event_overflow() can throttle and thereby stop the whole group. + * Walk the counters which are active now, so that a pending overflow + * of a sibling stopped that way is still handled. + */ + bitmap_copy(active, cpuc->active_mask, X86_PMC_IDX_MAX); + for_each_set_bit(idx, x86_pmu.cntr_mask, X86_PMC_IDX_MAX) { - if (!test_bit(idx, cpuc->active_mask)) + if (!test_bit(idx, active)) continue; event = cpuc->events[idx]; -- 2.56.1