Linux kernel -stable discussions
 help / color / mirror / Atom feed
From: Marc Zyngier <maz@kernel.org>
To: Fuad Tabba <fuad.tabba@linux.dev>
Cc: Vincent Donnefort <vdonnefort@google.com>,
	oupton@kernel.org, kvmarm@lists.linux.dev,
	linux-arm-kernel@lists.infradead.org, joey.gouly@arm.com,
	seiden@linux.ibm.com, suzuki.poulose@arm.com,
	yuzenghui@huawei.com, catalin.marinas@arm.com, will@kernel.org,
	kernel-team@android.com, stable@vger.kernel.org
Subject: Re: [PATCH v1] KVM: arm64: Use CNTPCT_EL0 for hyp trace clock
Date: Sat, 28 Nov 2026 11:27:23 +0000	[thread overview]
Message-ID: <86qzg5xkkk.wl-maz@kernel.org> (raw)
In-Reply-To: <CA+EHjTy15GC3=c7MvQu7eKON1g-v8KFzYXruoTfWvoSdBOnMCA@mail.gmail.com>

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.

      reply	other threads:[~2026-11-28 11:27 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=86qzg5xkkk.wl-maz@kernel.org \
    --to=maz@kernel.org \
    --cc=catalin.marinas@arm.com \
    --cc=fuad.tabba@linux.dev \
    --cc=joey.gouly@arm.com \
    --cc=kernel-team@android.com \
    --cc=kvmarm@lists.linux.dev \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=oupton@kernel.org \
    --cc=seiden@linux.ibm.com \
    --cc=stable@vger.kernel.org \
    --cc=suzuki.poulose@arm.com \
    --cc=vdonnefort@google.com \
    --cc=will@kernel.org \
    --cc=yuzenghui@huawei.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox