From: Fuad Tabba <fuad.tabba@linux.dev>
To: Marc Zyngier <maz@kernel.org>, Oliver Upton <oupton@kernel.org>,
kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org
Cc: Joey Gouly <joey.gouly@arm.com>,
Steffen Eiden <seiden@linux.ibm.com>,
Suzuki K Poulose <suzuki.poulose@arm.com>,
Zenghui Yu <yuzenghui@huawei.com>,
Catalin Marinas <catalin.marinas@arm.com>,
Will Deacon <will@kernel.org>,
Mark Rutland <mark.rutland@arm.com>,
Vincent Donnefort <vdonnefort@google.com>,
kernel-team@android.com, linux-kernel@vger.kernel.org,
Oculytic <oculytic@gmail.com>, Fuad Tabba <tabba@google.com>
Subject: [PATCH 3/3] KVM: arm64: Disable host PMU counters for protected guests
Date: Tue, 27 Oct 2026 08:11:47 +0000 [thread overview]
Message-ID: <20261027081147.498183-4-fuad.tabba@linux.dev> (raw)
In-Reply-To: <20261027081147.498183-1-fuad.tabba@linux.dev>
A protected guest has no PMU, and EL2 clears MDCR_EL2.HPME for it.
However, HPME has no effect, since __pkvm_vcpu_load() sets HPMN to cover
every event counter, and HPME never covers the cycle or instruction
counter. The counters the host has enabled therefore keep counting the
guest.
Stop them in the world switch while a protected guest runs, reading
PMCNTENSET_EL0 rather than trusting the host-controlled events mask.
Gate this on a new per-CPU HAS_PMUV3 flag, since system_supports_pmuv3()
is false when the CPUs implement different PMU versions.
Side channels are outside pKVM's threat model, so this is hardening
rather than a fix.
Reported-by: Oculytic <oculytic@gmail.com>
Signed-off-by: Fuad Tabba <fuad.tabba@linux.dev>
---
arch/arm64/include/asm/kvm_host.h | 1 +
arch/arm64/kvm/debug.c | 4 +++-
arch/arm64/kvm/hyp/nvhe/switch.c | 24 ++++++++++++++++++++++++
3 files changed, 28 insertions(+), 1 deletion(-)
diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h
index 1c6632216e327..7ecb55e5018eb 100644
--- a/arch/arm64/include/asm/kvm_host.h
+++ b/arch/arm64/include/asm/kvm_host.h
@@ -834,6 +834,7 @@ struct kvm_host_data {
#define KVM_HOST_DATA_FLAG_VCPU_IN_HYP_CONTEXT 4
#define KVM_HOST_DATA_FLAG_L1_VNCR_MAPPED 5
#define KVM_HOST_DATA_FLAG_HAS_BRBE 6
+#define KVM_HOST_DATA_FLAG_HAS_PMUV3 7
unsigned long flags;
struct kvm_cpu_context host_ctxt;
diff --git a/arch/arm64/kvm/debug.c b/arch/arm64/kvm/debug.c
index f4d7b12045e8f..3c74b076b5278 100644
--- a/arch/arm64/kvm/debug.c
+++ b/arch/arm64/kvm/debug.c
@@ -79,9 +79,11 @@ void kvm_init_host_debug_data(void)
unsigned int pmuver = cpuid_feature_extract_unsigned_field(dfr0,
ID_AA64DFR0_EL1_PMUVer_SHIFT);
- if (pmuv3_implemented(pmuver))
+ if (pmuv3_implemented(pmuver)) {
*host_data_ptr(nr_event_counters) = FIELD_GET(ARMV8_PMU_PMCR_N,
read_sysreg(pmcr_el0));
+ host_data_set_flag(HAS_PMUV3);
+ }
*host_data_ptr(debug_brps) = SYS_FIELD_GET(ID_AA64DFR0_EL1, BRPs, dfr0);
*host_data_ptr(debug_wrps) = SYS_FIELD_GET(ID_AA64DFR0_EL1, WRPs, dfr0);
diff --git a/arch/arm64/kvm/hyp/nvhe/switch.c b/arch/arm64/kvm/hyp/nvhe/switch.c
index 91e1edf40bcd2..79a6277de4fc1 100644
--- a/arch/arm64/kvm/hyp/nvhe/switch.c
+++ b/arch/arm64/kvm/hyp/nvhe/switch.c
@@ -175,6 +175,26 @@ static void __pmu_switch_to_host(struct kvm_vcpu *vcpu)
#define __pmu_switch_to_host(v) do {} while (0)
#endif
+static u64 __pmu_stop_host_counters(struct kvm_vcpu *vcpu)
+{
+ u64 pmcnten;
+
+ if (!vcpu_is_protected(vcpu) || !host_data_test_flag(HAS_PMUV3))
+ return 0;
+
+ pmcnten = read_sysreg(pmcntenset_el0);
+ if (pmcnten)
+ write_sysreg(pmcnten, pmcntenclr_el0);
+
+ return pmcnten;
+}
+
+static void __pmu_restore_host_counters(u64 pmcnten)
+{
+ if (pmcnten)
+ write_sysreg(pmcnten, pmcntenset_el0);
+}
+
/*
* Handler for protected VM MSR, MRS or System instruction execution in AArch64.
*
@@ -282,6 +302,7 @@ int __kvm_vcpu_run(struct kvm_vcpu *vcpu)
struct kvm_cpu_context *guest_ctxt;
struct kvm_s2_mmu *mmu;
bool pmu_switch_needed;
+ u64 host_pmcnten;
u64 exit_code;
/*
@@ -300,6 +321,7 @@ int __kvm_vcpu_run(struct kvm_vcpu *vcpu)
guest_ctxt = &vcpu->arch.ctxt;
pmu_switch_needed = __pmu_switch_to_guest(vcpu);
+ host_pmcnten = __pmu_stop_host_counters(vcpu);
__sysreg_save_state_nvhe(host_ctxt);
/*
@@ -384,6 +406,8 @@ int __kvm_vcpu_run(struct kvm_vcpu *vcpu)
if (pmu_switch_needed)
__pmu_switch_to_host(vcpu);
+ __pmu_restore_host_counters(host_pmcnten);
+
/* Returning to host will clear PSR.I, remask PMR if needed */
if (system_uses_irq_prio_masking())
gic_write_pmr(GIC_PRIO_IRQOFF);
--
2.47.3
next prev parent reply other threads:[~2026-10-27 8:12 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-27 8:11 [PATCH 0/3] KVM: arm64: Fix PMU handling in pKVM Fuad Tabba
2026-10-27 8:11 ` [PATCH 1/3] KVM: arm64: Synchronise kvm_host_data before host deprivileging Fuad Tabba
2026-10-27 8:11 ` [PATCH 2/3] KVM: arm64: Pass the PMU events to the hyp vCPU of non-protected guests Fuad Tabba
2026-10-27 11:05 ` Vincent Donnefort
2026-10-28 11:01 ` Fuad Tabba
2026-10-27 8:11 ` Fuad Tabba [this message]
2026-10-27 9:32 ` [PATCH 3/3] KVM: arm64: Disable host PMU counters for protected guests Marc Zyngier
2026-10-27 9:41 ` Fuad Tabba
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=20261027081147.498183-4-fuad.tabba@linux.dev \
--to=fuad.tabba@linux.dev \
--cc=catalin.marinas@arm.com \
--cc=joey.gouly@arm.com \
--cc=kernel-team@android.com \
--cc=kvmarm@lists.linux.dev \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mark.rutland@arm.com \
--cc=maz@kernel.org \
--cc=oculytic@gmail.com \
--cc=oupton@kernel.org \
--cc=seiden@linux.ibm.com \
--cc=suzuki.poulose@arm.com \
--cc=tabba@google.com \
--cc=vdonnefort@google.com \
--cc=will@kernel.org \
--cc=yuzenghui@huawei.com \
/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
all inboxes | Powered by JetHome®