From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=80.241.56.152 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796279233; b=qMchN6JaGHnkKocUs8lVGnsG8uL1OEIc+LpS+9M6odYrTbDgfTzJGE2Um2geqwdYu/zx qP1TD40BQ88YACtGExEDmKS1skzWEKvl/t3CGyi43i+mHwVSy4LksjO0geXRHcok2YCvK Buq/v4HF5ow2ggIXDx3e+FJFBo6mSDP/l9FLVMqLbk+1I6t/SNRqkW4fd3KfLOkdqlC4L 5k3x9vOScfKLNGIlz1NQKUFJMi4lApQwqI6fiYhRly59GxBDiQMKZ2BGts2Cadw4tX5Tz dZEEd10ylYEVZl84srU8uJlfXk4NFT6kn6ez720kqBW8HslLJbMizFVguFkPcyAb4JQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796279233; 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=k8Mm3LBPZ47mKZZK8QuFzeZLC8HnDaW/cxyjfxx3ZHQCfhVSSsg2oVuiZvIqXRqDrl9h 3rsVyTqORFqU10c96LV4FgCPyjG2qAsMYbatIdbNRL5/965heWNAqPyTrvgwZVLBP9hiQ yYb5pF7l0TjdjSsh82Yc5mdOkoe6OtI5TZF7nJYK8zVHQqYA12EQg+wedpGBrD1mbCQNH Ub1FpvDKkHKdLq7wmqMZMx2e0kDSIJk+V2GUrQ3TNMLcYrDcWuNg7zuNER4wG09amfHCw gt4EWxIUFg2LjnAxDInuAcrB9Z7Sshf7fss7VXJ+3PcOVE+ZRYYE6ff4voPjye8LsCw== 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=C6aG+7Q2; arc=none smtp.remote-ip=80.241.56.152 Received: from mout-p-102.mailbox.org (mout-p-102.mailbox.org [80.241.56.152]) (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 78570606A33A; Thu, 3 Dec 2026 06:27:12 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 78570606A33A 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=C6aG+7Q2 Received: from smtp1.mailbox.org (smtp1.mailbox.org [IPv6:2001:67c:2050:b231:465::1]) (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-102.mailbox.org (Postfix) with ESMTPS id 4jg4RK1pGlzKrsW; Thu, 03 Dec 2026 07:27:09 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mailbox.org; s=mail20150812; t=1796279229; 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=C6aG+7Q2/kle7PirWI7oxDTScRIB0V4v3SsRLAnFYWuOhylDh3M7xHd0Ht6QEN1NzP4+Ua 7DC4mTTur5Fz4nhQ86PGl/SD6/B1s/oTJRuHH6adRGXDPc9auOy3Au9R3SERm6zgAxe4Dp /sUW04vbxAUjiP1H+z9M+MNbpvCwu7ujPsqr3vL8Qvg+7/115yvFTMkyW1jrNwQxCe/1lZ Sc8jmGwcsLvl04FSSbh5PEHTIngvYC2RBrhBKwckLCP/94wKQlqDl1NhVVmm4RiUj0Geay Vtdp4F8umJfryJl5SYS0ieJyw7t157Tgc6PNztvy7q6mPqRAo3lTGsQW5N+Qvg== 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 v3 2/4] perf/x86/amd/core: Handle overflows of counters stopped by a throttle Date: Thu, 3 Dec 2026 08:27:01 +0200 Message-ID: <20261203062703.15346-3-john3@mailbox.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261203062703.15346-1-john3@mailbox.org> References: <20261203062703.15346-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-ID: 06b090252e2ffcda986 X-MBO-RS-META: j8d6atxya5fkrmu8xm546tnm7rmbx3nj 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