Skip to contentSkip to conversation

KVM: arm64: Fix PMU counter overflows for non-protected guests in pKVM

PATCH In review Fuad Tabba 2 participants 5 messages Dec 4

Subsystems (4)
lore
Share & tools
lore

Fuad Tabba

author reviewer
#0 lore raw

cover KVM: arm64: Fix PMU counter overflows for non-protected guests in pKVM

Hi folks,

A non-protected guest in pKVM loses PMU counter overflows. When a counter overflows, the host sets the counter's bit in its copy of PMOVSSET_EL0, and the next lazy state sync overwrites that copy with the hyp vCPU's stale one. The guest then takes no overflow interrupt, or takes it with the overflow bit clear. Patch 1 leaves the PMU registers out of the sync.

I went through the other writes the host makes without first syncing the guest's state, and they only touch the timer registers, which the sync already skips.

Patch 2 adds a test case for counter overflow to vpmu_counter_access, which fails in pKVM without the fix and passes with it in pKVM, nVHE, VHE and NV. On a host with more than one PMU type, the test needs James's fix [1].

Based on Linux 7.4-rc5 (fe6ff8d5d675c; also applies to kvmarm/fixes 012e4bc01e339).

Cheers,
/fuad

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

Fuad Tabba (2):
KVM: arm64: Don't copy the PMU registers between the host and hyp vCPU
KVM: arm64: selftests: Test PMU counter overflow in
vpmu_counter_access

 arch/arm64/kvm/hyp/nvhe/hyp-main.c            |  12 +-
 .../selftests/kvm/arm64/vpmu_counter_access.c | 214 +++++++++++++++++-
 2 files changed, 224 insertions(+), 2 deletions(-)

base-commit: fe6ff8d5d675c01793b9f051d16055dd4aa75244

Signature · 1 line
2.47.3
author reviewer
#1 lore raw

PATCH 1/2 KVM: arm64: Don't copy the PMU registers between the host and hyp vCPU

The host emulates the PMU of a non-protected guest in pKVM, and EL2 doesn't access the guest's PMU registers in the hyp vCPU. Lazy state sync copies them anyway.

Since commit 8ee90ca40b10c ("KVM: arm64: Pass the PMU events to the hyp vCPU of non-protected guests") the guest's counters count, and the perf overflow handler sets an overflowing counter's bit in the host's copy of PMOVSSET_EL0 before the host has synced the guest's state. The next sync overwrites it with the hyp vCPU's stale copy, so the guest takes no overflow interrupt, or takes it with the overflow bit clear.

Leave the PMU registers out of the copy, like we do for the timer registers.

Fixes 734dc8c01c838 ("KVM: arm64: Implement lazy vCPU state sync for non-protected guests")
---
 arch/arm64/kvm/hyp/nvhe/hyp-main.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
arch/arm64/kvm/hyp/nvhe/hyp-main.c +11 -1
index 0c023ee0cf42a..2d0401a94246b 100644
@@ -627,7 +627,7 @@ static void __copy_vcpu_state(const struct kvm_vcpu *from_vcpu,
 	to_vcpu->arch.ctxt.fp_regs	= from_vcpu->arch.ctxt.fp_regs;
 
 	/*
-	 * Copy the sysregs, but don't mess with the timer state which
+	 * Copy the sysregs, but don't mess with the timer and PMU state which
 	 * is directly handled by EL1 and is expected to be preserved.
 	 * enum vcpu_sysreg is sparse: VNCR-mapped registers take values
 	 * derived from their VNCR page offset, so the timer registers do
@@ -640,6 +640,16 @@ static void __copy_vcpu_state(const struct kvm_vcpu *from_vcpu,
 		case CNTV_CTL_EL0:
 		case CNTP_CVAL_EL0:
 		case CNTP_CTL_EL0:
+		case PMCR_EL0:
+		case PMSELR_EL0:
+		case PMEVCNTR0_EL0 ... PMEVCNTR30_EL0:
+		case PMCCNTR_EL0:
+		case PMEVTYPER0_EL0 ... PMEVTYPER30_EL0:
+		case PMCCFILTR_EL0:
+		case PMCNTENSET_EL0:
+		case PMINTENSET_EL1:
+		case PMOVSSET_EL0:
+		case PMUSERENR_EL0:
 			continue;
 		}
 		to_vcpu->arch.ctxt.sys_regs[i] = from_vcpu->arch.ctxt.sys_regs[i];
Signature · 1 line
2.47.3
author reviewer
#2 lore raw

PATCH 2/2 KVM: arm64: selftests: Test PMU counter overflow in vpmu_counter_access

In reply to Fuad Tabba

The vpmu_counter_access test accesses the guest's PMU registers, but doesn't check what happens when a counter overflows.

Add a case where the guest lets its cycle counter overflow, and check that it takes the PMU interrupt with the counter's bit set in PMOVSSET_EL0, whether it waits without trapping or traps in a loop. Do the same for a chained pair of event counters, where the high counter must also go up by one. With the cycle counter's interrupt disabled, check that the guest reads the bit as set and doesn't take the interrupt.

Add a define for the PMU's interrupt number now that the guest uses it.

---
 .../selftests/kvm/arm64/vpmu_counter_access.c | 214 +++++++++++++++++-
 1 file changed, 213 insertions(+), 1 deletion(-)
tools/testing/selftests/kvm/arm64/vpmu_counter_access.c +213 -1
index 22223395969e0..57e2f39863767 100644
@@ -10,14 +10,18 @@
  * other counters.
  * It also checks if the userspace accesses to the PMU regsisters honor the
  * PMCR.N value that's set for the guest.
+ * Finally, it checks the overflow status and interrupt on a counter overflow.
  * This test runs only when KVM_CAP_ARM_PMU_V3 is supported on the host.
  */
+#include <arch_timer.h>
+#include <gic.h>
 #include <kvm_util.h>
 #include <processor.h>
 #include <test_util.h>
 #include <vgic.h>
 #include <perf/arm_pmuv3.h>
 #include <linux/bitfield.h>
+#include <linux/psci.h>
 
 /* The max number of the PMU event counters (excluding the cycle counter) */
 #define ARMV8_PMU_MAX_GENERAL_COUNTERS	(ARMV8_PMU_MAX_COUNTERS - 1)
@@ -25,6 +29,9 @@
 /* The cycle counter bit position that's common among the PMU registers */
 #define ARMV8_PMU_CYCLE_IDX		31
 
+/* The PPI that the PMU interrupt is delivered on */
+#define PMU_IRQ				23
+
 struct vpmu_vm {
 	struct kvm_vm *vm;
 	struct kvm_vcpu *vcpu;
@@ -403,7 +410,7 @@ static void create_vpmu_vm(void *guest_code)
 {
 	struct kvm_vcpu_init init;
 	u8 pmuver, ec;
-	u64 dfr0, irq = 23;
+	u64 dfr0, irq = PMU_IRQ;
 	struct kvm_device_attr irq_attr = {
 		.group = KVM_ARM_VCPU_PMU_V3_CTRL,
 		.attr = KVM_ARM_VCPU_PMU_V3_IRQ,
@@ -596,6 +603,209 @@ static void run_error_test(u64 pmcr_n)
 	destroy_vpmu_vm();
 }
 
+#define CYCLE_COUNTER		BIT(ARMV8_PMU_CYCLE_IDX)
+#define CHAIN_LOW		BIT(0)
+#define CHAIN_HIGH		BIT(1)
+
+#define NR_OVERFLOWS		8
+#define CYCLES_TO_OVERFLOW	100000
+#define OVERFLOW_TIMEOUT_MS	2000
+
+enum overflow_wait {
+	/* The guest doesn't trap, so it exits on the overflow interrupt. */
+	WAIT_QUIET,
+	WAIT_TRAPPING,
+};
+
+static u64 irq_count, irq_ovs;
+
+static void guest_irq_handler(struct ex_regs *regs)
+{
+	unsigned int intid = gic_get_and_ack_irq();
+
+	if (intid == PMU_IRQ) {
+		WRITE_ONCE(irq_ovs, read_sysreg(pmovsclr_el0));
+		WRITE_ONCE(irq_count, READ_ONCE(irq_count) + 1);
+
+		write_sysreg(CYCLE_COUNTER | CHAIN_LOW | CHAIN_HIGH, pmovsclr_el0);
+		isb();
+	}
+
+	gic_set_eoi(intid);
+}
+
+static void overflow_stop(u64 counters)
+{
+	write_sysreg(counters, pmcntenclr_el0);
+	write_sysreg(counters, pmintenclr_el1);
+	write_sysreg(counters, pmovsclr_el0);
+	isb();
+}
+
+static void overflow_start(u64 counters, u64 irq_counter)
+{
+	WRITE_ONCE(irq_count, 0);
+	WRITE_ONCE(irq_ovs, 0);
+
+	write_sysreg(irq_counter, pmintenset_el1);
+	write_sysreg(counters, pmcntenset_el0);
+	isb();
+}
+
+static void wait_for_overflow(enum overflow_wait wait, u64 counter)
+{
+	u64 deadline = timer_get_cntct(VIRTUAL) + msec_to_cycles(OVERFLOW_TIMEOUT_MS);
+	struct arm_smccc_res res;
+
+	while (!READ_ONCE(irq_count) && timer_get_cntct(VIRTUAL) < deadline) {
+		if (wait == WAIT_TRAPPING)
+			do_smccc(PSCI_0_2_FN_PSCI_VERSION, 0, 0, 0, 0, 0, 0, 0, &res);
+	}
+
+	__GUEST_ASSERT(READ_ONCE(irq_count),
+		       "No interrupt for the overflow of counter 0x%lx", counter);
+	__GUEST_ASSERT(READ_ONCE(irq_ovs) & counter,
+		       "Overflow interrupt for counter 0x%lx with PMOVSCLR_EL0 = 0x%lx",
+		       counter, READ_ONCE(irq_ovs));
+}
+
+static void test_cycle_counter_overflow(enum overflow_wait wait)
+{
+	int i;
+
+	for (i = 0; i < NR_OVERFLOWS; i++) {
+		overflow_stop(CYCLE_COUNTER);
+		write_sysreg(-CYCLES_TO_OVERFLOW, pmccntr_el0);
+		overflow_start(CYCLE_COUNTER, CYCLE_COUNTER);
+
+		wait_for_overflow(wait, CYCLE_COUNTER);
+	}
+
+	overflow_stop(CYCLE_COUNTER);
+}
+
+static void test_overflow_irq_disabled(void)
+{
+	u64 deadline, ovs;
+	int i;
+
+	for (i = 0; i < NR_OVERFLOWS; i++) {
+		overflow_stop(CYCLE_COUNTER);
+		write_sysreg(-CYCLES_TO_OVERFLOW, pmccntr_el0);
+		overflow_start(CYCLE_COUNTER, 0);
+
+		deadline = timer_get_cntct(VIRTUAL) + msec_to_cycles(OVERFLOW_TIMEOUT_MS);
+		do {
+			ovs = read_sysreg(pmovsclr_el0);
+		} while (!(ovs & CYCLE_COUNTER) && timer_get_cntct(VIRTUAL) < deadline);
+
+		/* The handler clears the status bit, so check for an interrupt first. */
+		__GUEST_ASSERT(!READ_ONCE(irq_count),
+			       "Overflow interrupt for the cycle counter with its interrupt disabled");
+		__GUEST_ASSERT(ovs & CYCLE_COUNTER,
+			       "No overflow status for the cycle counter with its interrupt disabled");
+	}
+
+	overflow_stop(CYCLE_COUNTER);
+}
+
+static void test_chained_counter_overflow(enum overflow_wait wait)
+{
+	u64 high;
+	int i;
+
+	write_pmevtypern(0, ARMV8_PMU_INCLUDE_EL2 | ARMV8_PMUV3_PERFCTR_CPU_CYCLES);
+	write_pmevtypern(1, ARMV8_PMUV3_PERFCTR_CHAIN);
+
+	for (i = 0; i < NR_OVERFLOWS; i++) {
+		overflow_stop(CHAIN_LOW | CHAIN_HIGH);
+		write_pmevcntrn(0, (u32)-CYCLES_TO_OVERFLOW);
+		write_pmevcntrn(1, 0);
+		overflow_start(CHAIN_LOW | CHAIN_HIGH, CHAIN_LOW);
+
+		wait_for_overflow(wait, CHAIN_LOW);
+
+		write_sysreg(CHAIN_LOW | CHAIN_HIGH, pmcntenclr_el0);
+		isb();
+		high = read_pmevcntrn(1);
+		__GUEST_ASSERT(high == 1,
+			       "Chained counter at %lu after one overflow of the counter below it",
+			       high);
+	}
+
+	overflow_stop(CHAIN_LOW | CHAIN_HIGH);
+}
+
+static void guest_overflow_code(void)
+{
+	local_irq_disable();
+	gic_init(GIC_V3, 1);
+	gic_irq_enable(PMU_IRQ);
+
+	/* The bits for implemented counters are reset to UNKNOWN. */
+	write_sysreg(~0ul, pmcntenclr_el0);
+	write_sysreg(~0ul, pmintenclr_el1);
+	write_sysreg(~0ul, pmovsclr_el0);
+
+	/* On a host with NV, the guest runs at EL2, where a counter doesn't count by default. */
+	write_sysreg(ARMV8_PMU_INCLUDE_EL2, pmccfiltr_el0);
+
+	/* Leave PMCR_EL0.LP clear, since a counter chains only if it overflows at 32 bits. */
+	write_sysreg(ARMV8_PMU_PMCR_E | ARMV8_PMU_PMCR_LC, pmcr_el0);
+	isb();
+	local_irq_enable();
+
+	test_cycle_counter_overflow(WAIT_QUIET);
+	test_cycle_counter_overflow(WAIT_TRAPPING);
+	test_overflow_irq_disabled();
+
+	if (get_pmcr_n(read_sysreg(pmcr_el0)) >= 2 &&
+	    (read_sysreg(pmceid0_el0) & BIT(ARMV8_PMUV3_PERFCTR_CPU_CYCLES))) {
+		test_chained_counter_overflow(WAIT_QUIET);
+		test_chained_counter_overflow(WAIT_TRAPPING);
+	} else {
+		/* Tell the host that the chained counters weren't tested. */
+		GUEST_SYNC(0);
+	}
+
+	GUEST_DONE();
+}
+
+/*
+ * Create a guest with one vCPU, and check that it sees the overflow status for
+ * each counter overflow, and takes the PMU interrupt only if it's enabled.
+ */
+static void run_overflow_test(void)
+{
+	struct kvm_vcpu *vcpu;
+	struct ucall uc;
+
+	create_vpmu_vm(guest_overflow_code);
+	vcpu = vpmu_vm.vcpu;
+	vm_install_exception_handler(vpmu_vm.vm, VECTOR_IRQ_CURRENT, guest_irq_handler);
+	vcpu_device_attr_set(vcpu, KVM_ARM_VCPU_PMU_V3_CTRL, KVM_ARM_VCPU_PMU_V3_INIT, NULL);
+
+	do {
+		vcpu_run(vcpu);
+
+		switch (get_ucall(vcpu, &uc)) {
+		case UCALL_ABORT:
+			REPORT_GUEST_ASSERT(uc);
+			break;
+		case UCALL_SYNC:
+			pr_info("Chained counter overflow not tested: needs two event counters and CPU_CYCLES\n");
+			break;
+		case UCALL_DONE:
+			break;
+		default:
+			TEST_FAIL("Unknown ucall %lu", uc.cmd);
+			break;
+		}
+	} while (uc.cmd != UCALL_DONE);
+
+	destroy_vpmu_vm();
+}
+
 /*
  * Return the default number of implemented PMU event counters excluding
  * the cycle counter (i.e. PMCR_EL0.N value) for the guest.
@@ -639,5 +849,7 @@ int main(void)
 	for (i = pmcr_n + 1; i < ARMV8_PMU_MAX_COUNTERS; i++)
 		run_error_test(i);
 
+	run_overflow_test();
+
 	return 0;
 }
Signature · 1 line
2.47.3
#3 lore raw

Re: [PATCH 2/2] KVM: arm64: selftests: Test PMU counter overflow in vpmu_counter_access

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] kvm/arm64: missing CPU pinning in run_overflow_test() breaks big.LITTLE - [Low] KVM: arm64…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] kvm/arm64: missing CPU pinning in run_overflow_test() breaks big.LITTLE
- [Low] KVM: arm64: selftests: missing ARMV8_PMU_INCLUDE_EL2 on CHAIN counter
--

commit edb981028c9723f221e3d9e0fdc640b53ad0edc4
Author: Fuad Tabba <[email protected]>

KVM: arm64: selftests: Test PMU counter overflow in vpmu_counter_access

This commit extends vpmu_counter_access to test PMU cycle counter and chained event counter overflows, checking overflow status and interrupt delivery both when the guest waits quietly and when it traps in a loop.

Quoted 4 lines
tools/testing/selftests/kvm/arm64/vpmu_counter_access.c +0 -0
index 22223395969e0..57e2f39863767 100644

[ ... ]

Quoted 1 line
tools/testing/selftests/kvm/arm64/vpmu_counter_access.c +0 -0
@@ -596,6 +603,209 @@ static void run_error_test(u64 pmcr_n)

[ ... ]

tools/testing/selftests/kvm/arm64/vpmu_counter_access.c static void run_error_test(u64 pmcr_n) · line 718

Quoted 7 lines
tools/testing/selftests/kvm/arm64/vpmu_counter_access.c +7 -0
+static void test_chained_counter_overflow(enum overflow_wait wait)
+{
+	u64 high;
+	int i;
+
+	write_pmevtypern(0, ARMV8_PMU_INCLUDE_EL2 | ARMV8_PMUV3_PERFCTR_CPU_CYCLES);
+	write_pmevtypern(1, ARMV8_PMUV3_PERFCTR_CHAIN);

[Severity: Low]
Should PMEVTYPER1_EL0 also include ARMV8_PMU_INCLUDE_EL2 for the CHAIN event?

On NV-capable hosts the guest runs at EL2, where ARMv8 PMUv3 applies the PMEVTYPER<n>_EL0 filtering bits (including NSH) to chained odd counters too. drivers/perf/arm_pmuv3.c:armv8pmu_write_event_type() sets ARMV8_PMUV3_PERFCTR_CHAIN | ARMV8_PMU_INCLUDE_EL2 for this reason.

This only passes today because kvm_pmu_counter_increment() skips EL filtering on CHAIN events, so the test will fail if KVM enforces architectural filtering or backs the counter directly in hardware.

[ ... ]

tools/testing/selftests/kvm/arm64/vpmu_counter_access.c static void run_error_test(u64 pmcr_n) · line 786

Quoted 9 lines
tools/testing/selftests/kvm/arm64/vpmu_counter_access.c +9 -0
+static void run_overflow_test(void)
+{
+	struct kvm_vcpu *vcpu;
+	struct ucall uc;
+
+	create_vpmu_vm(guest_overflow_code);
+	vcpu = vpmu_vm.vcpu;
+	vm_install_exception_handler(vpmu_vm.vm, VECTOR_IRQ_CURRENT, guest_irq_handler);
+	vcpu_device_attr_set(vcpu, KVM_ARM_VCPU_PMU_V3_CTRL, KVM_ARM_VCPU_PMU_V3_INIT, NULL);

[Severity: Medium]
Can run_overflow_test() fail with a timeout on heterogeneous (big.LITTLE) arm64 hosts when the vCPU thread migrates across PMU clusters?

create_vpmu_vm() relies on kvm_arm_set_default_pmu() -> kvm_pmu_probe_armpmu(), which binds the VM to whichever arm_pmu covers the current CPU at vCPU init time without restricting kvm->arch.supported_cpus.

If the scheduler later migrates the spinning vCPU thread to a CPU outside that PMU's supported_cpus mask, armpmu_filter() and armpmu_add() stop scheduling the guest's perf event, so the counter stops advancing and wait_for_overflow() times out.

Pinning the thread with pin_self_to_any_cpu() before create_vpmu_vm() (or configuring KVM_ARM_VCPU_PMU_V3_SET_PMU with matching CPU affinity) keeps the vCPU on the bound PMU's cluster.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
author reviewer
#4 lore raw

Re: [PATCH 2/2] KVM: arm64: selftests: Test PMU counter overflow in vpmu_counter_access

Quoted 4 lines · On Fri, 04 Dec 2026 11:06:30 +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] kvm/arm64: missing CPU pinning in run_overflow_test() breaks big.LITTLE

Known issue, James's patch [1] fixes it.

Quoted 2 lines

- [Low] KVM: arm64: selftests: missing ARMV8_PMU_INCLUDE_EL2 on CHAIN counter

The count is UNPREDICTABLE without it. The fix below (tested) applies on top of this patch. I'll include it if I respin, or feel free to fold it in when applying (whichever is easier).

tools/testing/selftests/kvm/arm64/vpmu_counter_access.c +1 -1
index 57e2f39863767..7d1cd4acdcb9b 100644
@@ -715,7 +715,7 @@ static void test_chained_counter_overflow(enum overflow_wait wait)
 	int i;
 
 	write_pmevtypern(0, ARMV8_PMU_INCLUDE_EL2 | ARMV8_PMUV3_PERFCTR_CPU_CYCLES);
-	write_pmevtypern(1, ARMV8_PMUV3_PERFCTR_CHAIN);
+	write_pmevtypern(1, ARMV8_PMU_INCLUDE_EL2 | ARMV8_PMUV3_PERFCTR_CHAIN);
 
 	for (i = 0; i < NR_OVERFLOWS; i++) {
 		overflow_stop(CHAIN_LOW | CHAIN_HIGH);

Cheers,
/fuad

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