Skip to contentSkip to conversation

KVM: arm64: Fix PMU handling in pKVM

PATCH Superseded Fuad Tabba 3 participants 8 messages Oct 27 – Oct 28

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Fuad Tabba

author reviewer
#0 lore raw

cover KVM: arm64: Fix PMU handling in pKVM

Hi folks,

Two fixes and a hardening patch for PMU handling in pKVM, as one series [1].

The first is Vincent's rework of his HPMN fix. It supersedes his earlier posting [2]. Rather than read nr_event_counters through a different accessor, the host now copies the hypervisor's kvm_host_data before it's deprivileged, so host_data_ptr() returns what was set up at init. Otherwise, HPMN is 0 and a non-protected guest's event counters don't count.

The second is the non-protected half of Oliver's patch from 2024 [3]. It passes the host's PMU events to the hyp vCPU of a non-protected guest, whose counters otherwise all read 0.

The third covers the protected half. It stops the host's counters while a protected guest runs, so the host can't count the guest's cycles and events. Side channels are outside pKVM's threat model, so it carries no
Fixes: tag.

Based on kvmarm/next (dd0bc00e3e920).

Cheers,
/fuad

[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/
[3] https://lore.kernel.org/all/[email protected]/

Fuad Tabba (1):
KVM: arm64: Disable host PMU counters for protected guests

Oliver Upton (1):
KVM: arm64: Pass the PMU events to the hyp vCPU of non-protected
guests

Vincent Donnefort (1):
KVM: arm64: Synchronise kvm_host_data before host deprivileging

 arch/arm64/include/asm/kvm_host.h  |  1 +
 arch/arm64/kvm/debug.c             |  4 +++-
 arch/arm64/kvm/hyp/nvhe/hyp-main.c | 11 +++++++++++
 arch/arm64/kvm/hyp/nvhe/switch.c   | 24 ++++++++++++++++++++++++
 arch/arm64/kvm/pkvm.c              |  6 ++++++
 5 files changed, 45 insertions(+), 1 deletion(-)

base-commit: dd0bc00e3e9201fd49b62ff9cc07890c89003afe

Signature · 1 line
2.47.3
author reviewer
#1 lore raw

PATCH 1/3 KVM: arm64: Synchronise kvm_host_data before host deprivileging

From: Vincent Donnefort <[email protected]>

When protected mode is enabled, the host is populating and reading the hypervisor copy of kvm_host_data until it is deprivileged. This is an issue for nr_event_counters which is only used by the host to populate mdcr_el2. After deprivileging, it is reading an uninitialised value and sets HPMN to 0, preventing programmable event counters from counting a non-protected guest.

Before the host is entirely deprivileged, copy the entire kvm_host_data content from the hypervisor copy to the host-accessible one.

Fixes 2417218f2f23 ("KVM: arm64: Get rid of __kvm_get_mdcr_el2() and related warts")
---
 arch/arm64/kvm/pkvm.c | 6 ++++++
 1 file changed, 6 insertions(+)
arch/arm64/kvm/pkvm.c +6 -0
index 380dfb1db0be4..58190ff6723d6 100644
@@ -316,6 +316,12 @@ static void __init _kvm_host_prot_finalize(void *arg)
 {
 	int *err = arg;
 
+	/*
+	 * After __pkvm_prot_finalize, access to the hypervisor copy of
+	 * kvm_host_data is lost. Save it into the host-accessible version.
+	 */
+	*this_cpu_ptr(&kvm_host_data) = *this_cpu_ptr_nvhe_sym(kvm_host_data);
+
 	if (WARN_ON(kvm_call_hyp_nvhe(__pkvm_prot_finalize)))
 		WRITE_ONCE(*err, -EINVAL);
 }
Signature · 1 line
2.47.3
author reviewer
#2 lore raw

PATCH 2/3 KVM: arm64: Pass the PMU events to the hyp vCPU of non-protected guests

In reply to Fuad Tabba

From: Oliver Upton <[email protected]>

Before every run, the host records in vcpu->arch.pmu.events which perf counters to disable and enable around guest entry, and __pmu_switch_to_guest() applies them at EL2. Since commit be66e67f1750 ("KVM: arm64: Use the pKVM hyp vCPU structure in handle___kvm_vcpu_run()"), protected mode runs the hyp vCPU instead, and flush_hyp_vcpu() never copies the events into it. So the counters backing a non-protected guest's PMU are never enabled, and every counter the guest reads stays at 0, the cycle counter included. The host's counters that exclude guests keep counting the guest as well.

Copy the events into the hyp vCPU of a non-protected guest on every run. The events only exist with CONFIG_HW_PERF_EVENTS, so the copy is built under it. Like mdcr_el2, they're host-controlled and never reach a protected guest.

Fixes be66e67f1750 ("KVM: arm64: Use the pKVM hyp vCPU structure in handle___kvm_vcpu_run()")

[Fuad: non-protected copy only, tested on the hyp vCPU, under
CONFIG_HW_PERF_EVENTS, new message]

---
 arch/arm64/kvm/hyp/nvhe/hyp-main.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
arch/arm64/kvm/hyp/nvhe/hyp-main.c +11 -0
index d715aa942577e..28a292deecb64 100644
@@ -690,6 +690,16 @@ static void sync_debug_state(struct pkvm_hyp_vcpu *hyp_vcpu)
 		host_vcpu->arch.external_debug_state = hyp_vcpu->vcpu.arch.external_debug_state;
 }
 
+#ifdef CONFIG_HW_PERF_EVENTS
+static void flush_pmu_state(struct pkvm_hyp_vcpu *hyp_vcpu)
+{
+	if (!pkvm_hyp_vcpu_is_protected(hyp_vcpu))
+		hyp_vcpu->vcpu.arch.pmu.events = hyp_vcpu->host_vcpu->arch.pmu.events;
+}
+#else
+static void flush_pmu_state(struct pkvm_hyp_vcpu *hyp_vcpu) {}
+#endif
+
 static void flush_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu)
 {
 	struct kvm_vcpu *host_vcpu = hyp_vcpu->host_vcpu;
@@ -698,6 +708,7 @@ static void flush_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu)
 
 	fpsimd_sve_flush();
 	flush_debug_state(hyp_vcpu);
+	flush_pmu_state(hyp_vcpu);
 
 	/*
 	 * If we deal with a non-protected guest and the state is potentially
Signature · 1 line
2.47.3
Reviewed-by Tested-by on v1 2/3
#3 lore raw

Re: [PATCH 2/3] KVM: arm64: Pass the PMU events to the hyp vCPU of non-protected guests

Quoted 35 lines · Fuad Tabba

From: Oliver Upton <[email protected]>

Before every run, the host records in vcpu->arch.pmu.events which perf counters to disable and enable around guest entry, and __pmu_switch_to_guest() applies them at EL2. Since commit be66e67f1750 ("KVM: arm64: Use the pKVM hyp vCPU structure in handle___kvm_vcpu_run()"), protected mode runs the hyp vCPU instead, and flush_hyp_vcpu() never copies the events into it. So the counters backing a non-protected guest's PMU are never enabled, and every counter the guest reads stays at 0, the cycle counter included. The host's counters that exclude guests keep counting the guest as well.

Copy the events into the hyp vCPU of a non-protected guest on every run. The events only exist with CONFIG_HW_PERF_EVENTS, so the copy is built under it. Like mdcr_el2, they're host-controlled and never reach a protected guest.

Fixes be66e67f1750 ("KVM: arm64: Use the pKVM hyp vCPU structure in handle___kvm_vcpu_run()")

[Fuad: non-protected copy only, tested on the hyp vCPU, under
CONFIG_HW_PERF_EVENTS, new message]

---
 arch/arm64/kvm/hyp/nvhe/hyp-main.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
arch/arm64/kvm/hyp/nvhe/hyp-main.c +0 -0
index d715aa942577e..28a292deecb64 100644
@@ -690,6 +690,16 @@ static void sync_debug_state(struct pkvm_hyp_vcpu *hyp_vcpu)
 		host_vcpu->arch.external_debug_state = hyp_vcpu->vcpu.arch.external_debug_state;
 }

arch/arm64/kvm/hyp/nvhe/hyp-main.c static void sync_debug_state(struct pkvm_hyp_vcpu *hyp_vcpu) · line 701

Quoted 10 lines
arch/arm64/kvm/hyp/nvhe/hyp-main.c +9 -0
 
+#ifdef CONFIG_HW_PERF_EVENTS
+static void flush_pmu_state(struct pkvm_hyp_vcpu *hyp_vcpu)
+{
+	if (!pkvm_hyp_vcpu_is_protected(hyp_vcpu))
+		hyp_vcpu->vcpu.arch.pmu.events = hyp_vcpu->host_vcpu->arch.pmu.events;
+}
+#else
+static void flush_pmu_state(struct pkvm_hyp_vcpu *hyp_vcpu) {}
+#endif

nit: Could save a couple of lines by just doing?

#ifdef CONFIG_HW_PERF_EVENTS
if (!pkvm_hyp_vcpu_is_protected(hyp_vcpu))

        hyp_vcpu->vcpu.arch.pmu.events = hyp_vcpu->host_vcpu->arch.pmu.events;

#endif

Quoted 15 lines
arch/arm64/kvm/hyp/nvhe/hyp-main.c +2 -0
+
 static void flush_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu)
 {
 	struct kvm_vcpu *host_vcpu = hyp_vcpu->host_vcpu;
@@ -698,6 +708,7 @@ static void flush_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu)
 
 	fpsimd_sve_flush();
 	flush_debug_state(hyp_vcpu);
+	flush_pmu_state(hyp_vcpu);
 
 	/*
 	 * If we deal with a non-protected guest and the state is potentially
Signature · 1 line
2.47.3
Signature · 1 line
Vincent
author reviewer
#4 lore raw

Re: [PATCH 2/3] KVM: arm64: Pass the PMU events to the hyp vCPU of non-protected guests

On Tue, 27 Oct 2026 at 11:05, Vincent Donnefort <[email protected]> wrote:
[...]

Quoted 11 lines

arch/arm64/kvm/hyp/nvhe/hyp-main.c static void sync_debug_state(struct pkvm_hyp_vcpu *hyp_vcpu) · line 701

Quoted 9 lines
arch/arm64/kvm/hyp/nvhe/hyp-main.c +9 -0
+#ifdef CONFIG_HW_PERF_EVENTS
+static void flush_pmu_state(struct pkvm_hyp_vcpu *hyp_vcpu)
+{
+     if (!pkvm_hyp_vcpu_is_protected(hyp_vcpu))
+             hyp_vcpu->vcpu.arch.pmu.events = hyp_vcpu->host_vcpu->arch.pmu.events;
+}
+#else
+static void flush_pmu_state(struct pkvm_hyp_vcpu *hyp_vcpu) {}
+#endif

nit: Could save a couple of lines by just doing?

The coding style says to "Prefer to compile out entire functions, rather than portions of functions", as do similar functions here.

[...]

Quoted 2 lines

Thanks!
/fuad

author reviewer
#5 lore raw

PATCH 3/3 KVM: arm64: Disable host PMU counters for protected guests

In reply to Fuad Tabba

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.

---
 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(-)
arch/arm64/include/asm/kvm_host.h +1 -0
index 1c6632216e327..7ecb55e5018eb 100644
@@ -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;
arch/arm64/kvm/debug.c +3 -1
index f4d7b12045e8f..3c74b076b5278 100644
@@ -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);
arch/arm64/kvm/hyp/nvhe/switch.c +24 -0
index 91e1edf40bcd2..79a6277de4fc1 100644
@@ -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);
Signature · 1 line
2.47.3
maintainer
#6 lore raw

Re: [PATCH 3/3] KVM: arm64: Disable host PMU counters for protected guests

Quoted 40 lines · Fuad Tabba

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.

---
 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(-)
arch/arm64/include/asm/kvm_host.h +1 -0
index 1c6632216e327..7ecb55e5018eb 100644
@@ -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;
arch/arm64/kvm/debug.c +0 -0
index f4d7b12045e8f..3c74b076b5278 100644

arch/arm64/kvm/debug.c void kvm_init_host_debug_data(void) · line 85

Quoted 8 lines
arch/arm64/kvm/debug.c +2 -1
@@ -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);

Honestly, I don't think we need to work out which CPUs have PMUv3 and which don't. I'd be perfectly happy to disable KVM on asymmetric systems, simply because they don't exist.

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
author reviewer
#7 lore raw

Re: [PATCH 3/3] KVM: arm64: Disable host PMU counters for protected guests

On Tue, 27 Oct 2026 at 09:32, Marc Zyngier <[email protected]> wrote:
[...]

Quoted 9 lines

arch/arm64/kvm/debug.c void kvm_init_host_debug_data(void) · line 85

Quoted 5 lines
arch/arm64/kvm/debug.c +2 -1
-     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);

Honestly, I don't think we need to work out which CPUs have PMUv3 and which don't. I'd be perfectly happy to disable KVM on asymmetric systems, simply because they don't exist.

Ack. I'll drop the per-CPU flag on the respin, gate on system_supports_pmuv3(), and refuse KVM on asymmetric systems.

/fuad