[PATCH v3 3/3] KVM: PPC: Book3s HV: Ensure reliable usage of latency counters

From: Gautam Menghani

Date: Fri Nov 06 2026 - 10:36:32 EST


The kvmppc_vcpu_stats tracepoint and the vpa-pmu driver can both enable
and disable the latency counters in the VPA. When both are used
together, they can step on each other and produce unreliable numbers.
Since the VPA element for enabling/disabling counters
(l2_counters_enable) continues counting with any value > 0, introduce a
spinlock to ensure that the tracepoint and vpa-pmu driver can work
correctly together.

Fixes: e1f288d2f9c6 ("KVM: PPC: Book3S HV nestedv2: Add support for reading VPA counters for pseries guests")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Gautam Menghani <gautam@xxxxxxxxxxxxx>
---
arch/powerpc/kvm/book3s_hv.c | 9 +++++++--
1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/arch/powerpc/kvm/book3s_hv.c b/arch/powerpc/kvm/book3s_hv.c
index 5fa6f93548ad..e1bde3c6b0c4 100644
--- a/arch/powerpc/kvm/book3s_hv.c
+++ b/arch/powerpc/kvm/book3s_hv.c
@@ -4142,14 +4142,19 @@ int kvmhv_get_l2_counters_status(void)
get_lppaca()->l2_counters_enable;
}

+static DEFINE_RAW_SPINLOCK(l2_counters_lock);
void kvmhv_set_l2_counters_status(int cpu, bool status)
{
+ unsigned long flags;
+
if (!firmware_has_feature(FW_FEATURE_LPAR))
return;
+ raw_spin_lock_irqsave(&l2_counters_lock, flags);
if (status)
- lppaca_of(cpu).l2_counters_enable = 1;
+ lppaca_of(cpu).l2_counters_enable++;
else
- lppaca_of(cpu).l2_counters_enable = 0;
+ lppaca_of(cpu).l2_counters_enable--;
+ raw_spin_unlock_irqrestore(&l2_counters_lock, flags);
}
EXPORT_SYMBOL(kvmhv_set_l2_counters_status);

--
2.56.0