Linux Perf Users
 help / color / mirror / Atom feed
From: Yosry Ahmed <yosry@kernel.org>
To: Peter Zijlstra <peterz@infradead.org>,
	Sean Christopherson <seanjc@google.com>
Cc: Ingo Molnar <mingo@redhat.com>,
	Arnaldo Carvalho de Melo <acme@kernel.org>,
	Namhyung Kim <namhyung@kernel.org>,
	Mark Rutland <mark.rutland@arm.com>,
	Alexander Shishkin <alexander.shishkin@linux.intel.com>,
	Jiri Olsa <jolsa@kernel.org>, Ian Rogers <irogers@google.com>,
	Adrian Hunter <adrian.hunter@intel.com>,
	James Clark <james.clark@linaro.org>,
	Paolo Bonzini <pbonzini@redhat.com>,
	x86@kernel.org, Dapeng Mi <dapeng1.mi@linux.intel.com>,
	Mingwei Zhang <mizhang@google.com>,
	linux-perf-users@vger.kernel.org, kvm@vger.kernel.org,
	linux-kernel@vger.kernel.org, Yosry Ahmed <yosry@kernel.org>,
	stable@vger.kernel.org
Subject: [PATCH 2/2] KVM: x86/pmu: Clear PERF_GLOBAL_CTRL after switching to guest LVTPC
Date: Tue,  1 Dec 2026 17:14:13 +0000	[thread overview]
Message-ID: <20261201171413.2289887-3-yosry@kernel.org> (raw)
In-Reply-To: <20261201171413.2289887-1-yosry@kernel.org>

When loading the guest PMU context, clear PERF_GLOBAL_CTRL after
switching to guest LVTPC. Otherwise, the host perf NMI handler may set
bits in PERF_GLOBAL_CTRL after KVM clears it (specifically on Intel,
through __intel_pmu_enable_all()). This results in PERF_GLOBAL_CTRL not
being cleared when the guest counters and event selectors are loaded, so
the guest counters could start counting in the host.

Switching LVTPC while PERF_GLOBAL_CTRL is non-zero should be safe as all
host events should be disabled at this point, and guest
counters/selectors are not loaded yet. Although it is worth noting that
switching LVTPC while PERF_GLOBAL_CTRL was cleared was intentional [1].

Alternatively, clearing PERF_GLOBAL_CTRL could be moved inside
perf_load_guest_lvtpc(), after setting guest_lvtpc_loaded but before
writing APIC_LVTPC. This would make sure PERF_GLOBAL_CTRL is cleared
after host NMIs can no longer set it, and that switching LVTPC happens
with PERF_GLOBAL_CTRL cleared. However, this would require more plumbing
on the perf side.

Fixes: 56bb27369750 ("KVM: x86/pmu: Load/put mediated PMU context when entering/exiting guest")
Cc: stable@vger.kernel.org
Link: https://lore.kernel.org/20250818143204.GH3289052@noisy.programming.kicks-ass.net # [1]
Signed-off-by: Yosry Ahmed <yosry@kernel.org>
---
 arch/x86/kvm/pmu.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/arch/x86/kvm/pmu.c b/arch/x86/kvm/pmu.c
index eef2f792dc9e8..1a41cd4c4ecb4 100644
--- a/arch/x86/kvm/pmu.c
+++ b/arch/x86/kvm/pmu.c
@@ -1355,6 +1355,8 @@ void kvm_mediated_pmu_load(struct kvm_vcpu *vcpu)
 
 	perf_load_guest_context();
 
+	perf_load_guest_lvtpc(kvm_lapic_get_reg(vcpu->arch.apic, APIC_LVTPC));
+
 	/*
 	 * Explicitly clear PERF_GLOBAL_CTRL, as "loading" the guest's context
 	 * disables all individual counters (if any were enabled), but doesn't
@@ -1367,11 +1369,13 @@ void kvm_mediated_pmu_load(struct kvm_vcpu *vcpu)
 	 * configuring all events to be GUEST_ONLY.  Clear PERF_GLOBAL_CONTROL
 	 * even for SVM to minimize the damage if a perf event is left enabled,
 	 * and to ensure a consistent starting state.
+	 *
+	 * Clear PERF_GLOBAL_CTRL after switching to the guest LVTPC, as the
+	 * host perf NMI handler may set bits in PERF_GLOBAL_CTRL even if events
+	 * are disabled.
 	 */
 	wrmsrq(kvm_pmu_ops.PERF_GLOBAL_CTRL, 0);
 
-	perf_load_guest_lvtpc(kvm_lapic_get_reg(vcpu->arch.apic, APIC_LVTPC));
-
 	kvm_pmu_load_guest_pmcs(vcpu);
 
 	kvm_pmu_call(mediated_load)(vcpu);
-- 
2.56.1.839.g22a8290db9-goog


  parent reply	other threads:[~2026-12-01 17:14 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01 17:14 [PATCH 0/2] perf/x86: Fix races when loading guest mediated PMU context Yosry Ahmed
2026-12-01 17:14 ` [PATCH 1/2] perf/x86: Prevent races between NMIs and switching LVTPC Yosry Ahmed
2026-12-01 17:14 ` Yosry Ahmed [this message]
2026-12-01 17:20 ` [PATCH 0/2] perf/x86: Fix races when loading guest mediated PMU context sashiko-bot

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=20261201171413.2289887-3-yosry@kernel.org \
    --to=yosry@kernel.org \
    --cc=acme@kernel.org \
    --cc=adrian.hunter@intel.com \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=dapeng1.mi@linux.intel.com \
    --cc=irogers@google.com \
    --cc=james.clark@linaro.org \
    --cc=jolsa@kernel.org \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=mingo@redhat.com \
    --cc=mizhang@google.com \
    --cc=namhyung@kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=peterz@infradead.org \
    --cc=seanjc@google.com \
    --cc=stable@vger.kernel.org \
    --cc=x86@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