Kernel KVM virtualization development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Yosry Ahmed" <yosry@kernel.org>
Cc: kvm@vger.kernel.org
Subject: Re: [PATCH v7 11/17] KVM: x86/pmu: Track mediated PMU counters with mode-specific enables
Date: Thu, 28 May 2026 00:45:30 +0000	[thread overview]
Message-ID: <20260528004530.C3C8D1F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260527234711.4175166-12-yosry@kernel.org>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] Non-atomic modification of `pmu->global_status` in NMI context can cause lost updates.
--

commit 0456def2f622ab0d0efee8781cb983557b090999
Author: Yosry Ahmed <yosry@kernel.org>

KVM: x86/pmu: Track mediated PMU counters with mode-specific enables

This commit tracks mediated PMU counters with mode-specific enables by adding
a new bitmap to the PMU state. This avoids having to constantly check if a
counter needs to be disabled for mode-specific reasons, such as Host-Only or
Guest-Only bits in EVENTSEL on SVM.

> diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
> index 0f3cbf9fc1ae1..32a413182a751 100644
> --- a/arch/x86/include/asm/kvm_host.h
> +++ b/arch/x86/include/asm/kvm_host.h
> @@ -615,6 +615,8 @@ struct kvm_pmu {
>  	DECLARE_BITMAP(pmc_counting_instructions, X86_PMC_IDX_MAX);
>  	DECLARE_BITMAP(pmc_counting_branches, X86_PMC_IDX_MAX);
>  
> +	DECLARE_BITMAP(pmc_has_mode_specific_enables, X86_PMC_IDX_MAX);

[Severity: High]
This is a pre-existing issue, but can the non-atomic modification of
pmu->global_status in NMI context cause lost updates?

Looking at kvm_perf_overflow(), it runs in NMI context on the host when
a performance counter overflows. It calls __kvm_perf_overflow(), which
uses the non-atomic __set_bit() and __test_and_set_bit() to update
pmu->global_status.

Concurrently, the vCPU thread modifies pmu->global_status during MSR
emulation (e.g., handling MSR_CORE_PERF_GLOBAL_OVF_CTRL or
MSR_AMD64_PERF_CNTR_GLOBAL_STATUS_CLR) using non-atomic read-modify-write
operations like pmu->global_status &= ~data.

If an NMI fires between the vCPU thread's load and store of global_status:

vCPU thread:
    u64 status = pmu->global_status;

NMI fires here:
    __kvm_perf_overflow()
        __set_bit(..., (unsigned long *)&pmu->global_status);

vCPU thread resumes:
    pmu->global_status = status & ~data;

Will the vCPU thread completely overwrite the NMI's update? Does this cause
KVM to silently drop performance counter overflows, leading to lost PMIs
in the guest?

[ ... ]

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260527234711.4175166-1-yosry@kernel.org?part=11

  reply	other threads:[~2026-05-28  0:45 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-27 23:46 [PATCH v7 00/17] KVM: x86/pmu: Add support for AMD Host-Only/Guest-Only bits Yosry Ahmed
2026-05-27 23:46 ` [PATCH v7 01/17] KVM: nSVM: Stop leaking single-stepping on VMRUN into L2 Yosry Ahmed
2026-05-27 23:46 ` [PATCH v7 02/17] KVM: nSVM: Bail early out of VMRUN emulation if advancing RIP fails Yosry Ahmed
2026-05-27 23:46 ` [PATCH v7 03/17] KVM: nSVM: Unify RIP and PMU handling calls when emulating VMRUN Yosry Ahmed
2026-05-27 23:46 ` [PATCH v7 04/17] KVM: nSVM: Move VMRUN instruction retirement after entering guest mode Yosry Ahmed
2026-05-27 23:46 ` [PATCH v7 05/17] KVM: x86: Move enable_pmu/enable_mediated_pmu to pmu.h and pmu.c Yosry Ahmed
2026-05-27 23:47 ` [PATCH v7 06/17] KVM: x86/pmu: Rename reprogram_counters() to clarify usage Yosry Ahmed
2026-05-27 23:47 ` [PATCH v7 07/17] KVM: x86/pmu: Do a single atomic OR when reprogramming counters Yosry Ahmed
2026-05-27 23:47 ` [PATCH v7 08/17] KVM: x86/pmu: Check mediated PMU counter enablement before event filters Yosry Ahmed
2026-05-28  0:54   ` sashiko-bot
2026-05-27 23:47 ` [PATCH v7 09/17] KVM: x86/pmu: Add support for KVM_X86_PMU_OP_OPTIONAL_RET0 Yosry Ahmed
2026-05-27 23:47 ` [PATCH v7 10/17] KVM: x86/pmu: Disable counters based on Host-Only/Guest-Only bits in SVM Yosry Ahmed
2026-05-28  0:34   ` sashiko-bot
2026-05-28  1:43     ` Sean Christopherson
2026-05-27 23:47 ` [PATCH v7 11/17] KVM: x86/pmu: Track mediated PMU counters with mode-specific enables Yosry Ahmed
2026-05-28  0:45   ` sashiko-bot [this message]
2026-05-27 23:47 ` [PATCH v7 12/17] KVM: x86/pmu: Reprogram Host/Guest-Only counters on nested transitions Yosry Ahmed
2026-05-27 23:47 ` [PATCH v7 13/17] KVM: x86/pmu: Allow Host-Only/Guest-Only bits with nSVM and mediated PMU Yosry Ahmed
2026-05-27 23:47 ` [PATCH v7 14/17] KVM: selftests: Refactor allocating guest stack into a helper Yosry Ahmed
2026-05-27 23:47 ` [PATCH v7 15/17] KVM: selftests: Allocate a dedicated guest page for x86 L2 guest stack Yosry Ahmed
2026-05-28  2:56   ` Sean Christopherson
2026-05-28 17:58     ` Yosry Ahmed
2026-05-28 18:01       ` Sean Christopherson
2026-05-28 18:03         ` Yosry Ahmed
2026-05-27 23:47 ` [PATCH v7 16/17] KVM: selftests: Drop L1-provided stacks for L2 guests on x86 Yosry Ahmed
2026-05-27 23:47 ` [PATCH v7 17/17] KVM: selftests: Add svm_pmu_host_guest_test for Host-Only/Guest-Only bits Yosry Ahmed
2026-05-28  2:25   ` Sean Christopherson
2026-05-28 18:01     ` Yosry Ahmed
2026-05-28 18:04       ` Sean Christopherson
2026-05-28 18:15         ` Jim Mattson
2026-05-28  2:27 ` [PATCH v7 00/17] KVM: x86/pmu: Add support for AMD " Sean Christopherson
2026-05-28 18:02   ` Yosry Ahmed
2026-05-28 18:05     ` Sean Christopherson
2026-05-28  8:30 ` Mi, Dapeng
2026-05-28 18:01   ` Yosry Ahmed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260528004530.C3C8D1F000E9@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=kvm@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=yosry@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox