[PATCH v1] KVM: arm64: Use CNTPCT_EL0 for hyp trace clock
Marc Zyngier
maz at kernel.org
Sat Nov 28 03:27:23 PST 2026
On Sat, 28 Nov 2026 10:23:20 +0000,
Fuad Tabba <fuad.tabba at linux.dev> wrote:
>
> Hi Marc,
>
> On Sat, 28 Nov 2026 09:34:01 +0000, Marc Zyngier <maz at kernel.org> wrote:
> > On Fri, 27 Nov 2026 21:27:43 +0000,
> > Vincent Donnefort <vdonnefort at google.com> wrote:
> > >
> > > trace_hyp_clock() reads the virtual counter (CNTVCT_EL0). Under nVHE
> > > (and pKVM without hVHE), loading a guest with a non-zero offset applies
> > > that offset to CNTVCT_EL0, corrupting the trace timestamps. Use
> > > CNTPCT_EL0 instead.
> >
> > pKVM mandates CNTVOFF_EL2 being 0. Has that changed?
>
> Not for non-protected VMs. flush_hyp_timer_state() zeroes CNTVOFF_EL2
> only for a protected vCPU, and a non-protected VM in pKVM uses the
> offset the host sets. Without hVHE, the hypervisor's CNTVCT_EL0 reads
> then include that offset. I saw the trace timestamps jump on QEMU with
> pKVM on a cortex-a57, running a non-protected guest.
I don't doubt you saw it. But frankly, it is high time we stop piling
more and more stuff on nVHE. It is *dead*. So making things measurably
worse for everyone else is not on.
If tracing on nVHE is that useful (which I find debatable), then
context switch CNTVOFF_EL2 appropriately in the tracing code.
Something like below.
M.
diff --git a/arch/arm64/kvm/hyp/nvhe/clock.c b/arch/arm64/kvm/hyp/nvhe/clock.c
index f3e2619db4e4..b339340cea40 100644
--- a/arch/arm64/kvm/hyp/nvhe/clock.c
+++ b/arch/arm64/kvm/hyp/nvhe/clock.c
@@ -47,6 +47,28 @@ void trace_hyp_clock_update(u32 mult, u32 shift, u64 epoch_ns, u64 epoch_cyc)
smp_store_release(&clock->cur, bank);
}
+static u64 get_counter(void)
+{
+ u64 cntvoff = 0, cyc;
+
+ if (!has_hvhe()) {
+ cntvoff = read_sysreg(cntvoff_el2);
+ if (cntvoff) {
+ write_sysreg(0, cntvoff_el2);
+ isb();
+ }
+ }
+
+ cyc = __arch_counter_get_cntvct();
+
+ if (!has_hvhe() && cntvoff) {
+ write_sysreg(cntvoff, cntvoff_el2);
+ isb();
+ }
+
+ return cyc;
+}
+
/* Use untrusted host data */
u64 trace_hyp_clock(void)
{
@@ -54,7 +76,7 @@ u64 trace_hyp_clock(void)
u64 bank = smp_load_acquire(&clock->cur);
u64 cyc, ns;
- cyc = __arch_counter_get_cntvct() - clock->data[bank].epoch_cyc;
+ cyc = get_counter() - clock->data[bank].epoch_cyc;
if (likely(cyc < clock->data[bank].cyc_overflow64)) {
ns = cyc * clock->data[bank].mult;
--
Without deviation from the norm, progress is not possible.
More information about the linux-arm-kernel
mailing list