From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=198.175.65.9 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796176386; b=t2u2OW3npD24pGIfglTlC5VsFo/B9uVz/s0YTH3Z8e9vg99uULk+QrLoxcmsBtVLVlft FsXKfWeoCcmRCqXuDjL+L1mwbwZGEeU55e+/69qKc4itkNCWR4IGCOIf/DZwLr1UMU1e6 91fEZLD2D11DMmf6QVfb+P60fnDcxY7gfb655TDblYxHSvTjwKF5Ahhy6e2l6flTjN9g1 y+4LSouUJgJqnXX8umjvjHsDV8CySUdsfGCjQxn/3V72dSmMev0fJMbvESq/2Hu3Y2ldv paGGvpuwWbYehd+gVYrZcUiK/4ex1tZWaZlBGzvd7ASzvfgrw/giA+VsXZPDphIEtqg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796176386; h=DMARC-Filter:DKIM-Signature:X-CSE-ConnectionGUID:X-CSE-MsgGUID: X-IronPort-AV:X-IronPort-AV:Received:X-CSE-ConnectionGUID: X-CSE-MsgGUID:X-ExtLoop1:X-IronPort-AV:Received:Message-ID:Date: MIME-Version:User-Agent:Subject:To:Cc:References:Content-Language: From:In-Reply-To:Content-Type:Content-Transfer-Encoding; bh=HovSXyhpCXHNQrRQasodPPXWKuYBeADfJUNITBB03qQ=; b=giGnlwnD/jOL3ibJgevQ8eyv4curLpm1+ti2hj5WJYt9ixKdVpqYfPbau+OXZCIetuC/ teK4obAG6qznhp4jLQrlOYm26G5MDj6+Zdrk8DW/nonZbs+MYIY0oj4JND1S/IFKcYbcC UwDQQcNJjK8hWdvIK0z/lF4b47EmD1WsC7vdeI0z8v2j89u9yOUd3U0/vBretWBuI1R+U GGmSpqs0D5V+gXayVdGuGlLhxUpXm8higQ9qNbW6KauOJFULDX5XuOtnhcYiwBSVPTgy2 QOJKMBsqH7tDNhYMXNKCebzJlDGcWy1cYEsHQOYruTcw5+eYlDm3YJKG/xAyixVH56w== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.intel.com; spf=pass smtp.mailfrom=linux.intel.com; dkim=pass header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=dINxgqr+; arc=none smtp.remote-ip=198.175.65.9 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.9]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0A27D606A31A; Wed, 2 Dec 2026 01:53:05 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 0A27D606A31A Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=dINxgqr+ DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1796176387; x=1827712387; h=message-id:date:mime-version:subject:to:cc:references: from:in-reply-to:content-transfer-encoding; bh=HovSXyhpCXHNQrRQasodPPXWKuYBeADfJUNITBB03qQ=; b=dINxgqr+GDF5htB9thpLO4F/MEopg2uhZV8W+MraCHCz+tFnErbLFzoP 2GwsQqBFaCadj0CWU9zACVr798bqxAeQKPciDappZbZ6/Bt9RsJGFMIkX pQ7Pmy6t01c0lEwY4ZnEp2O7HQJ8EiED+jI3zO75KjhNbRRGFQ266du6m 5Gf3V6suPelQFHerWgvRIEzqVfjwmaDbyFGnwfLcab0YX4EyBbDMn94GV 7ciaxWkyDeGJYcFnOzb1iMqBJKwtJfv/arX7hZYwGn5jfyGQ64doZ4lRV Lsr/dJVf+Qss3flzN4YwhM+lpGUKqnIyiiwbALgygcWUBfOQNau/zxjFL A==; X-CSE-ConnectionGUID: Uftk6Cp3S2eCY0N5gGU9AQ== X-CSE-MsgGUID: Nr+x3hDpRkqvtY9mTtCP6A== X-IronPort-AV: E=McAfee;i="6800,10657,11983"; a="4985201" X-IronPort-AV: E=Sophos;i="6.27,245,1787036400"; d="scan'208";a="4985201" Received: from orviesa010.jf.intel.com ([10.64.159.150]) by orvoesa101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Dec 2026 17:53:06 -0800 X-CSE-ConnectionGUID: oO11bRS7RWWj+/dV+e6tOg== X-CSE-MsgGUID: wSaj19JJTDG2uFbFim1jwg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.27,245,1787036400"; d="scan'208";a="4844641" Received: from dapengmi-mobl1.ccr.corp.intel.com (HELO [10.124.241.239]) ([10.124.241.239]) by smtpauth.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Dec 2026 17:53:00 -0800 Message-ID: <392a079e-3148-4154-973b-7b1f319c8fb5@linux.intel.com> Date: Wed, 2 Dec 2026 09:52:57 +0800 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 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v2] perf/x86/amd/core: Handle overflows of counters stopped by a throttle To: Oancea Catalin , 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 References: <20261201163432.44692-1-john3@mailbox.org> Content-Language: en-US From: "Mi, Dapeng" In-Reply-To: <20261201163432.44692-1-john3@mailbox.org> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On 12/2/2026 12:34 AM, Oancea Catalin wrote: > amd_pmu_v2_handle_irq() reads PerfCntrGlobalStatus once, handles the > overflows of the counters which are set in cpuc->active_mask and warns > about every status bit which is left over: > > New overflows for inactive PMCs: 2 > WARNING: arch/x86/events/amd/core.c:1031 at amd_pmu_v2_handle_irq+0x86e/0x9a0 > Call Trace: > perf_event_nmi_handler > nmi_handle > default_do_nmi > exc_nmi > > Throttling stops the whole event group, so perf_event_overflow() can > stop counters which the loop has not reached yet. When both events of a > group overflow in the same NMI and the first one is throttled: > > amd_pmu_v2_handle_irq() > status = 0x3 > counter 0: x86_perf_event_set_period() > perf_event_overflow() > perf_event_throttle_group() > x86_pmu_stop(leader) clears active_mask bit 0 > x86_pmu_stop(sibling) clears active_mask bit 1 > counter 1: not in active_mask, skipped > status = 0x2 warning > > The overflow of the sibling is not handled. Its period is not set up > again and its status bit stays set, because only the handled bits are > acknowledged. > > An unprivileged task can trigger this with perf_event_paranoid at its > default of 2: open a group of two sampling events with short periods > (raw event 0x76, exclude_kernel, sample_period 20000 and 20011) on the > task itself and spin in user space. With six such tasks the warning > shows up within seconds. > > Take a copy of active_mask before the loop and walk the counters which > were active on entry. For a counter which was stopped in the meantime, > update the count, clear the status bit and set up the next period as > before, but do not deliver a sample, as the event is throttled. > > x86_pmu_handle_irq() and the Intel handler skip such a counter in the > same way. They do not warn, and they are not changed here. Could you please fix the same issue on Intel platforms with a separate patch? We need to avoid the deviation on AMD and Intel platforms.  > > Observed on v7.4-rc5-25-g19cc08c1a6a6 in a KVM guest (virtual > PerfMonV2 PMU, KASAN and lockdep enabled), not on bare metal. An MSR > trace of the guest shows the sequence above: status 0x3, a write to > counter 0, both event selects disabled, no write to counter 1. With > this change the trace shows counter 1 being written in the same NMI > after the two disables, and the warning did not trigger in five runs > from fresh boots. > > 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 > --- > v2: > - Reworded the comment at the second active_mask test as suggested by > Ravi Bangoria. No change to the code. > - Changelog: say that x86_pmu_handle_irq() and the Intel handler have > the same gap and are not changed here (Ravi asked about them), and > name the commit and the runs of the new test. > - Tested again on v7.4-rc5-25-g19cc08c1a6a6. > > v1: https://lore.kernel.org/all/20261124211146.61378-1-john3@mailbox.org/ > > Notes for reviewers, not for the changelog: > > Testing was done only in a KVM guest (-cpu host on a Ryzen 9 7945HX host > running 6.12.107, so KVM's virtual PMU), with KASAN, lockdep and HZ=1000. > I have not run this on bare metal or on a non-debug kernel, where the NMI > is much faster and throttling harder to reach. > > On 19cc08c1a6a6 without the patch the reproducer below triggered the > warning in 3 of 3 runs from fresh boots (2 as root, 1 as uid 65534), 27 > seconds each. With the patch: 0 of 5 (3 as root, 2 as uid 65534), no > other warning. The last 5 seconds of every run were traced (MSR events). > In the patched runs a counter which was stopped while a lower one was > handled and which had an overflow pending was reloaded in the same NMI > each time, 25603 times in total, and never left for later. > > A caveat for anybody who tries this in a KVM guest: the highest guest > counter in use sometimes did not count at all here, also for a plain > counting event. A run in which that happens cannot warn. The numbers > above are from runs whose trace shows both counters overflowing. > > Not verified: that the counts of the sibling are exact after it is > unthrottled. I did not compare them with expected values. > > x86_pmu_handle_irq(): tested on the same commit with clearcpuid=perfmon_v2 > (so amd_pmu_handle_irq() is used). The sibling's overflow is skipped > there too, without a warning; a later NMI picks it up, or it is still > pending when the task is scheduled out. > > Intel handle_pmi_common(): tested on an i7-14700T (bare metal, P-cores) > with the same commit and a debug print. The stopped sibling is skipped > there too. Its status bit is already acked, so no later PMI handles it, > and it does not sample again until the event is rescheduled. Details of > both tests are in my reply in the v1 thread. > > This applies to mainline and to tip perf/urgent (67b34211a9eb). In tip > perf/core the first hunk's context differs: 86a52e8d105f ("perf/x86: Mark > perf_sample_data __uninitialized") changed the declaration next to the > one added here. > > The Fixes: commit was checked by test: with the same config and > reproducer, 9734e25fbf5a warns (2 of 2 fresh boots) and its parent > ca559503b89c does not (0 of 3, 90 seconds each). 9734e25fbf5a is in v6.16 > and later. I did not test the stable trees. > > The kernel .config is the syzkaller one I fuzz with; I can send it. > > ---- 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 | 17 ++++++++++++++++- > 1 file changed, 16 insertions(+), 1 deletion(-) > > diff --git a/arch/x86/events/amd/core.c b/arch/x86/events/amd/core.c > index 49b6b8fce566..3efcdff2f47b 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 consumed. > + */ > + 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]; > @@ -1014,6 +1022,13 @@ static int amd_pmu_v2_handle_irq(struct pt_regs *regs) > if (!x86_perf_event_set_period(event)) > continue; > > + /* > + * Group was throttled in an earlier iteration; re-arm the > + * counter without delivering the sample. > + */ > + if (!test_bit(idx, cpuc->active_mask)) > + continue; Did I miss something? Why to skip the sample delivery for the overflowed siblings?  Thanks. > + > perf_sample_save_brstack(&data, event, &cpuc->lbr_stack, NULL); > > perf_event_overflow(event, &data, regs); > > base-commit: 19cc08c1a6a650f8e54c0461ff198be6448f425d