Skip to contentSkip to conversation

KVM: arm64: Fix PMU handling in pKVM

PATCH Superseded Fuad Tabba 2 participants 6 messages Oct 28

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Fuad Tabba

author reviewer
#0 lore raw

v2 cover KVM: arm64: Fix PMU handling in pKVM

Hi folks,

Changes since v1 [1]:
- Gate __pmu_stop_host_counters() on system_supports_pmuv3() rather than v1's per-CPU HAS_PMUV3 flag. (Marc)
- Collected Vincent's Reviewed-by and Tested-by on patch 2. (thanks!)

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

The first is Vincent's rework of his HPMN fix. It supersedes his earlier posting [3]. Rather than read nr_event_counters through a different accessor, the host 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 [4]. 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]/
[4] 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/kvm/hyp/nvhe/hyp-main.c | 11 +++++++++++
 arch/arm64/kvm/hyp/nvhe/switch.c   | 24 ++++++++++++++++++++++++
 arch/arm64/kvm/pkvm.c              |  6 ++++++
 3 files changed, 41 insertions(+)

base-commit: dd0bc00e3e9201fd49b62ff9cc07890c89003afe

Signature · 1 line
2.47.3
author reviewer
#1 lore raw

PATCH v2 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 v2 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
author reviewer
#3 lore raw

PATCH v2 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 system_supports_pmuv3(), as __activate_traps_common() does for PMUSERENR_EL0.

Side channels are outside pKVM's threat model, so this is hardening rather than a fix.

---
 arch/arm64/kvm/hyp/nvhe/switch.c | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)
arch/arm64/kvm/hyp/nvhe/switch.c +24 -0
index 91e1edf40bcd2..f602154ab49b5 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) || !system_supports_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
#4 lore raw

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

Quoted 29 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 system_supports_pmuv3(), as __activate_traps_common() does for PMUSERENR_EL0.

Side channels are outside pKVM's threat model, so this is hardening rather than a fix.

---
 arch/arm64/kvm/hyp/nvhe/switch.c | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)
arch/arm64/kvm/hyp/nvhe/switch.c +0 -0
index 91e1edf40bcd2..f602154ab49b5 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
 

arch/arm64/kvm/hyp/nvhe/switch.c static void __pmu_switch_to_host(struct kvm_vcpu *vcpu) · line 187

Quoted 10 lines
arch/arm64/kvm/hyp/nvhe/switch.c +10 -0
+static u64 __pmu_stop_host_counters(struct kvm_vcpu *vcpu)
+{
+	u64 pmcnten;
+
+	if (!vcpu_is_protected(vcpu) || !system_supports_pmuv3())
+		return 0;
+
+	pmcnten = read_sysreg(pmcntenset_el0);
+	if (pmcnten)
+		write_sysreg(pmcnten, pmcntenclr_el0);

It feels a bit odd to change the individual counter enables, rather than just use PMCR_EL0.E, which is designed for this purpose.

It probably doesn't change much, but it'd certainly make the intent a bit more obvious. Or am I missing something?

Thanks,

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

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

Quoted 3 lines · Marc Zyngier

On Wed, 28 Oct 2026 11:07:39 +0000,
Fuad Tabba <[email protected]> wrote:

[...]

Quoted 14 lines

arch/arm64/kvm/hyp/nvhe/switch.c static void __pmu_switch_to_host(struct kvm_vcpu *vcpu) · line 187

Quoted 10 lines
arch/arm64/kvm/hyp/nvhe/switch.c +10 -0
+static u64 __pmu_stop_host_counters(struct kvm_vcpu *vcpu)
+{
+     u64 pmcnten;
+
+     if (!vcpu_is_protected(vcpu) || !system_supports_pmuv3())
+             return 0;
+
+     pmcnten = read_sysreg(pmcntenset_el0);
+     if (pmcnten)
+             write_sysreg(pmcnten, pmcntenclr_el0);

It feels a bit odd to change the individual counter enables, rather than just use PMCR_EL0.E, which is designed for this purpose.

Quoted 2 lines

It probably doesn't change much, but it'd certainly make the intent a bit more obvious. Or am I missing something?

Nope, my miss. I'll move to PMCR_EL0.E in v3.

Thanks,
/fuad