Linux-ARM-Kernel Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v1] KVM: arm64: Use CNTPCT_EL0 for hyp trace clock
@ 2026-11-27 21:27 Vincent Donnefort
  2026-11-28  9:34 ` Marc Zyngier
  0 siblings, 1 reply; 4+ messages in thread
From: Vincent Donnefort @ 2026-11-27 21:27 UTC (permalink / raw)
  To: maz, oupton, kvmarm, linux-arm-kernel
  Cc: joey.gouly, seiden, suzuki.poulose, yuzenghui, catalin.marinas,
	will, kernel-team, fuad.tabba, Vincent Donnefort, stable

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.

Cc: stable@vger.kernel.org
Fixes: b22888917fa4 ("KVM: arm64: Sync boot clock with the nVHE/pKVM hyp")
Reported-by: Fuad Tabba <fuad.tabba@linux.dev>
Signed-off-by: Vincent Donnefort <vdonnefort@google.com>
---
 arch/arm64/kvm/hyp/nvhe/clock.c | 2 +-
 arch/arm64/kvm/hyp_trace.c      | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/arch/arm64/kvm/hyp/nvhe/clock.c b/arch/arm64/kvm/hyp/nvhe/clock.c
index f3e2619db4e4..ea4412e6737c 100644
--- a/arch/arm64/kvm/hyp/nvhe/clock.c
+++ b/arch/arm64/kvm/hyp/nvhe/clock.c
@@ -54,7 +54,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 = __arch_counter_get_cntpct() - clock->data[bank].epoch_cyc;
 
 	if (likely(cyc < clock->data[bank].cyc_overflow64)) {
 		ns = cyc * clock->data[bank].mult;
diff --git a/arch/arm64/kvm/hyp_trace.c b/arch/arm64/kvm/hyp_trace.c
index b9404496f977..06d6bd7aeac7 100644
--- a/arch/arm64/kvm/hyp_trace.c
+++ b/arch/arm64/kvm/hyp_trace.c
@@ -510,8 +510,8 @@ int __init kvm_hyp_trace_init(void)
 			per_cpu(timer_unstable_counter_workaround, cpu);
 
 		if (IS_ENABLED(CONFIG_ARM_ARCH_TIMER_OOL_WORKAROUND) &&
-		    wa && wa->read_cntvct_el0) {
-			pr_warn("hyp trace can't handle CNTVCT workaround '%s'\n", wa->desc);
+		    wa && wa->read_cntpct_el0) {
+			pr_warn("hyp trace can't handle CNTPCT workaround '%s'\n", wa->desc);
 			return -EOPNOTSUPP;
 		}
 	}

base-commit: b6a9fa34ce4187cb69d3c588a4bbffc31ce5db8f
-- 
2.56.1.839.g22a8290db9-goog



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v1] KVM: arm64: Use CNTPCT_EL0 for hyp trace clock
  2026-11-27 21:27 [PATCH v1] KVM: arm64: Use CNTPCT_EL0 for hyp trace clock Vincent Donnefort
@ 2026-11-28  9:34 ` Marc Zyngier
  2026-11-28 10:23   ` Fuad Tabba
  0 siblings, 1 reply; 4+ messages in thread
From: Marc Zyngier @ 2026-11-28  9:34 UTC (permalink / raw)
  To: Vincent Donnefort
  Cc: oupton, kvmarm, linux-arm-kernel, joey.gouly, seiden,
	suzuki.poulose, yuzenghui, catalin.marinas, will, kernel-team,
	fuad.tabba, stable

On Fri, 27 Nov 2026 21:27:43 +0000,
Vincent Donnefort <vdonnefort@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?

> 
> Cc: stable@vger.kernel.org
> Fixes: b22888917fa4 ("KVM: arm64: Sync boot clock with the nVHE/pKVM hyp")
> Reported-by: Fuad Tabba <fuad.tabba@linux.dev>
> Signed-off-by: Vincent Donnefort <vdonnefort@google.com>
> ---
>  arch/arm64/kvm/hyp/nvhe/clock.c | 2 +-
>  arch/arm64/kvm/hyp_trace.c      | 4 ++--
>  2 files changed, 3 insertions(+), 3 deletions(-)
> 
> diff --git a/arch/arm64/kvm/hyp/nvhe/clock.c b/arch/arm64/kvm/hyp/nvhe/clock.c
> index f3e2619db4e4..ea4412e6737c 100644
> --- a/arch/arm64/kvm/hyp/nvhe/clock.c
> +++ b/arch/arm64/kvm/hyp/nvhe/clock.c
> @@ -54,7 +54,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 = __arch_counter_get_cntpct() - clock->data[bank].epoch_cyc;
>  
>  	if (likely(cyc < clock->data[bank].cyc_overflow64)) {
>  		ns = cyc * clock->data[bank].mult;
> diff --git a/arch/arm64/kvm/hyp_trace.c b/arch/arm64/kvm/hyp_trace.c
> index b9404496f977..06d6bd7aeac7 100644
> --- a/arch/arm64/kvm/hyp_trace.c
> +++ b/arch/arm64/kvm/hyp_trace.c
> @@ -510,8 +510,8 @@ int __init kvm_hyp_trace_init(void)
>  			per_cpu(timer_unstable_counter_workaround, cpu);
>  
>  		if (IS_ENABLED(CONFIG_ARM_ARCH_TIMER_OOL_WORKAROUND) &&
> -		    wa && wa->read_cntvct_el0) {
> -			pr_warn("hyp trace can't handle CNTVCT workaround '%s'\n", wa->desc);
> +		    wa && wa->read_cntpct_el0) {
> +			pr_warn("hyp trace can't handle CNTPCT workaround '%s'\n", wa->desc);
>  			return -EOPNOTSUPP;
>  		}
>  	}
> 

This is a bad idea. Virtualising the physical counter is extremely
expensive, while the virtual counter is basically free. On top of
that, nVHE is a dried up turd we shouldn't invest any effort into.

I can see two options: disabling trace on nVHE altogether (my
favourite one, because let's face it, *nobody* is using pKVM on that),
or reset CNTVOFF_EL2 to 0 across the read of the counter when on nVHE.

	M.

-- 
Without deviation from the norm, progress is not possible.


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v1] KVM: arm64: Use CNTPCT_EL0 for hyp trace clock
  2026-11-28  9:34 ` Marc Zyngier
@ 2026-11-28 10:23   ` Fuad Tabba
  2026-11-28 11:27     ` Marc Zyngier
  0 siblings, 1 reply; 4+ messages in thread
From: Fuad Tabba @ 2026-11-28 10:23 UTC (permalink / raw)
  To: Marc Zyngier
  Cc: Vincent Donnefort, oupton, kvmarm, linux-arm-kernel, joey.gouly,
	seiden, suzuki.poulose, yuzenghui, catalin.marinas, will,
	kernel-team, stable

Hi Marc,

On Sat, 28 Nov 2026 09:34:01 +0000, Marc Zyngier <maz@kernel.org> wrote:
> On Fri, 27 Nov 2026 21:27:43 +0000,
> Vincent Donnefort <vdonnefort@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.

Cheers,
/fuad


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v1] KVM: arm64: Use CNTPCT_EL0 for hyp trace clock
  2026-11-28 10:23   ` Fuad Tabba
@ 2026-11-28 11:27     ` Marc Zyngier
  0 siblings, 0 replies; 4+ messages in thread
From: Marc Zyngier @ 2026-11-28 11:27 UTC (permalink / raw)
  To: Fuad Tabba
  Cc: Vincent Donnefort, oupton, kvmarm, linux-arm-kernel, joey.gouly,
	seiden, suzuki.poulose, yuzenghui, catalin.marinas, will,
	kernel-team, stable

On Sat, 28 Nov 2026 10:23:20 +0000,
Fuad Tabba <fuad.tabba@linux.dev> wrote:
> 
> Hi Marc,
> 
> On Sat, 28 Nov 2026 09:34:01 +0000, Marc Zyngier <maz@kernel.org> wrote:
> > On Fri, 27 Nov 2026 21:27:43 +0000,
> > Vincent Donnefort <vdonnefort@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.


^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-11-28 11:27 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-27 21:27 [PATCH v1] KVM: arm64: Use CNTPCT_EL0 for hyp trace clock Vincent Donnefort
2026-11-28  9:34 ` Marc Zyngier
2026-11-28 10:23   ` Fuad Tabba
2026-11-28 11:27     ` Marc Zyngier

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox