All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 0/4] KVM: arm64: nv: Fix AT emulation for nested virtualization
@ 2026-11-19 13:25 Dan Carpenter
  2026-11-19 13:25 ` [PATCH v2 1/4] KVM: arm64: nv: Fix AT emulation for TCR2_EL1 Dan Carpenter
                   ` (3 more replies)
  0 siblings, 4 replies; 8+ messages in thread
From: Dan Carpenter @ 2026-11-19 13:25 UTC (permalink / raw)
  To: Marc Zyngier, Oliver Upton, Fuad Tabba, Joey Gouly, Steffen Eiden,
	Suzuki K Poulose, Zenghui Yu, Catalin Marinas, Will Deacon,
	Mark Rutland, Jintack Lim
  Cc: linux-arm-kernel, kvmarm, Dan Carpenter, Karthik Bhargavan,
	Clément Blaudeau, stable

This fixes two issues in handling AT emulation for nested virt.
* VMs were potentially using the TCR2 bits which weren't intended for
  them.
* It turned out that guests could write to the TCR2 bits even when
  those were supposed to be hidden.  That is patches 2 and 3.
* The VMID wasn't handled correctly.  Roll over could happen before
  or during the AT execution leading to invalid results.

Changes in v2:
1) Sashiko pointed out that we need to restore the original TCR2
   bits at the end of the AT command.
2) Clean up based on Marc's feed back.
3) Trap the writes to the TCR2 registers (normal and nested)

Signed-off-by: Dan Carpenter <dan@pereq.eu>
---
Dan Carpenter (4):
      KVM: arm64: nv: Fix AT emulation for TCR2_EL1
      KVM: arm64: Trap TCR2_EL1 writes when S1PIE or S1POE is hidden
      KVM: arm64: nv: Clear hidden TCR2 bits for nested guests
      KVM: arm64: nv: Update the shadow MMU's VMID for AT emulation

 arch/arm64/kvm/at.c                        | 54 +++++++++++++++++++++++++++---
 arch/arm64/kvm/config.c                    |  5 +++
 arch/arm64/kvm/hyp/include/hyp/sysreg-sr.h | 10 +++++-
 arch/arm64/kvm/sys_regs.c                  | 33 ++++++++++++++++--
 4 files changed, 95 insertions(+), 7 deletions(-)
---
base-commit: 5a7235e9adff42aa043ac9473dbeb0354dca0967
change-id: 20261119-nv-tcr2-59b13df5ecf6

Best regards,
--  
Dan Carpenter <dan@pereq.eu>


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

* [PATCH v2 1/4] KVM: arm64: nv: Fix AT emulation for TCR2_EL1
  2026-11-19 13:25 [PATCH v2 0/4] KVM: arm64: nv: Fix AT emulation for nested virtualization Dan Carpenter
@ 2026-11-19 13:25 ` Dan Carpenter
  2026-11-19 13:25 ` [PATCH v2 2/4] KVM: arm64: Trap TCR2_EL1 writes when S1PIE or S1POE is hidden Dan Carpenter
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 8+ messages in thread
From: Dan Carpenter @ 2026-11-19 13:25 UTC (permalink / raw)
  To: Marc Zyngier, Oliver Upton, Fuad Tabba, Joey Gouly, Steffen Eiden,
	Suzuki K Poulose, Zenghui Yu, Catalin Marinas, Will Deacon,
	Mark Rutland, Jintack Lim
  Cc: linux-arm-kernel, kvmarm, Dan Carpenter, Karthik Bhargavan,
	Clément Blaudeau, stable

Arm provides an AT instruction that translates a virtual address the way
the MMU would and returns the result.  There is a bug in the way this
instruction is emulated for nested virtualization because of how TCR2_EL1
is handled.  The bug is fairly minor.

The TCR2_EL1 register has bits which turn on permission indirection
(PIE) and permission overlays (POE).  When these bits are set, the AT
instruction reads the permissions stored in PIR_EL1 and POR_EL1.  If
you have the right features and permissions then in the return value from
the AT instruction we set one bit to either 1 or 0 depending on whether
the access would be allowed or whether it would fault.

However, the bug is that just because the host has TCR2 enabled, it
doesn't mean the VM has it.  In the current code KVM doesn't load these
registers for a VM which doesn't have the features, instead the AT runs
with the host's HCRX_EL2, which has TCR2En set.  So the AT uses the
TCR2_EL1, PIR_EL1 and POR_EL1 values another VM left on the CPU, or
possibly the VMM's POR_EL0.  Likewise, for a VM which has TCR2 but not
PIE or POE, the L2's TCR2_EL1 is loaded unmasked.

The impact of this bug is that we leak a single bit of data to the L1,
but in real life it's hard to imagine how it is useful to an attacker.

Fix this by clearing the PIE and POE bits in effective_tcr2() when the
VM doesn't have those features, and use it for the L2's TCR2_EL1.  When
the AT runs on the guest hypervisor's own registers, in __kvm_at_s1e2()
and in __kvm_at_s1e01_fast(), load the guest's TCR2_EL2 for the AT and
restore the live value afterwards.

Fixes: 477e89cabb14 ("KVM: arm64: nv: Add basic emulation of AT S1E{0,1}{R,W}")
Fixes: 23e7a34c8397 ("KVM: arm64: Add AT fast-path support for S1PIE")
Cc: stable@vger.kernel.org
Signed-off-by: Dan Carpenter <dan@pereq.eu>
---
v2: Restore original TCR2 values at the end of the AT command
    Cleanup the code based on Marc's review.
    Add some comments
---
 arch/arm64/kvm/at.c | 48 +++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 45 insertions(+), 3 deletions(-)

diff --git a/arch/arm64/kvm/at.c b/arch/arm64/kvm/at.c
index 30d8f062b2c2..9dfabd0586e2 100644
--- a/arch/arm64/kvm/at.c
+++ b/arch/arm64/kvm/at.c
@@ -91,11 +91,18 @@ static enum trans_regime compute_translation_regime(struct kvm_vcpu *vcpu, u32 o
 static u64 effective_tcr2(struct kvm_vcpu *vcpu, enum trans_regime regime)
 {
 	if (regime == TR_EL10) {
+		u64 val;
+
 		if (vcpu_has_nv(vcpu) &&
 		    !(__vcpu_sys_reg(vcpu, HCRX_EL2) & HCRX_EL2_TCR2En))
 			return 0;
 
-		return vcpu_read_sys_reg(vcpu, TCR2_EL1);
+		val = vcpu_read_sys_reg(vcpu, TCR2_EL1);
+		if (!kvm_has_s1pie(vcpu->kvm))
+			val &= ~TCR2_EL1_PIE;
+		if (!kvm_has_s1poe(vcpu->kvm))
+			val &= ~(TCR2_EL1_POE | TCR2_EL1_E0POE);
+		return val;
 	}
 
 	return vcpu_read_sys_reg(vcpu, TCR2_EL2);
@@ -1384,6 +1391,24 @@ static int handle_at_slow(struct kvm_vcpu *vcpu, u32 op, u64 vaddr, u64 *par)
 	return 0;
 }
 
+/*
+ * TCR2 features are always exposed to the guests.  Clear the ones
+ * which are not exposed on this VM.  Return the original TCR2 so we
+ * can restore it after running the AT command.
+ */
+static u64 load_guest_tcr2(struct kvm_vcpu *vcpu)
+{
+	u64 orig = read_sysreg_el1(SYS_TCR2);
+
+	/*
+	 * This relies on the reset value of TCR2_EL2 being 0, and not
+	 * being modified when FEAT_TCR2 is not exposed to the guest.
+	 */
+	write_sysreg_el1(vcpu_read_sys_reg(vcpu, TCR2_EL2), SYS_TCR2);
+
+	return orig;
+}
+
 /*
  * Return the PAR_EL1 value as the result of a valid translation.
  *
@@ -1397,7 +1422,9 @@ static u64 __kvm_at_s1e01_fast(struct kvm_vcpu *vcpu, u32 op, u64 vaddr)
 {
 	struct mmu_config config;
 	struct kvm_s2_mmu *mmu;
+	bool swap_tcr2 = false;
 	bool fail, mmu_cs;
+	u64 orig_tcr2;
 	u64 par;
 
 	par = SYS_PAR_EL1_F;
@@ -1438,7 +1465,7 @@ static u64 __kvm_at_s1e01_fast(struct kvm_vcpu *vcpu, u32 op, u64 vaddr)
 	write_sysreg_el1(vcpu_read_sys_reg(vcpu, TCR_EL1),	SYS_TCR);
 	write_sysreg_el1(vcpu_read_sys_reg(vcpu, MAIR_EL1),	SYS_MAIR);
 	if (kvm_has_tcr2(vcpu->kvm)) {
-		write_sysreg_el1(vcpu_read_sys_reg(vcpu, TCR2_EL1), SYS_TCR2);
+		write_sysreg_el1(effective_tcr2(vcpu, TR_EL10), SYS_TCR2);
 		if (kvm_has_s1pie(vcpu->kvm)) {
 			write_sysreg_el1(vcpu_read_sys_reg(vcpu, PIR_EL1), SYS_PIR);
 			write_sysreg_el1(vcpu_read_sys_reg(vcpu, PIRE0_EL1), SYS_PIRE0);
@@ -1452,6 +1479,12 @@ static u64 __kvm_at_s1e01_fast(struct kvm_vcpu *vcpu, u32 op, u64 vaddr)
 	__load_stage2(mmu);
 
 skip_mmu_switch:
+	if (!mmu_cs && vcpu_has_nv(vcpu) &&
+	    cpus_have_final_cap(ARM64_HAS_TCR2)) {
+		swap_tcr2 = true;
+		orig_tcr2 = load_guest_tcr2(vcpu);
+	}
+
 	/* Temporarily switch back to guest context */
 	write_sysreg_hcr(vcpu->arch.hcr_el2);
 	isb();
@@ -1485,6 +1518,9 @@ static u64 __kvm_at_s1e01_fast(struct kvm_vcpu *vcpu, u32 op, u64 vaddr)
 	if (!fail)
 		par = read_sysreg_par();
 
+	if (swap_tcr2)
+		write_sysreg_el1(orig_tcr2, SYS_TCR2);
+
 	write_sysreg_hcr(HCR_HOST_VHE_FLAGS);
 
 	if (mmu_cs)
@@ -1544,7 +1580,7 @@ int __kvm_at_s1e2(struct kvm_vcpu *vcpu, u32 op, u64 vaddr)
 	 * switching context behind everybody's back, disable interrupts...
 	 */
 	scoped_guard(write_lock_irqsave, &vcpu->kvm->mmu_lock) {
-		u64 val, hcr;
+		u64 val, hcr, orig_tcr2;
 		bool fail;
 
 		val = hcr = read_sysreg(hcr_el2);
@@ -1554,6 +1590,9 @@ int __kvm_at_s1e2(struct kvm_vcpu *vcpu, u32 op, u64 vaddr)
 		if (!vcpu_el2_e2h_is_set(vcpu))
 			val |= HCR_EL2_NV | HCR_EL2_NV1;
 
+		if (cpus_have_final_cap(ARM64_HAS_TCR2))
+			orig_tcr2 = load_guest_tcr2(vcpu);
+
 		write_sysreg_hcr(val);
 		isb();
 
@@ -1577,6 +1616,9 @@ int __kvm_at_s1e2(struct kvm_vcpu *vcpu, u32 op, u64 vaddr)
 		if (!fail)
 			par = read_sysreg_par();
 
+		if (cpus_have_final_cap(ARM64_HAS_TCR2))
+			write_sysreg_el1(orig_tcr2, SYS_TCR2);
+
 		write_sysreg_hcr(hcr);
 		isb();
 	}

-- 
2.53.0


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

* [PATCH v2 2/4] KVM: arm64: Trap TCR2_EL1 writes when S1PIE or S1POE is hidden
  2026-11-19 13:25 [PATCH v2 0/4] KVM: arm64: nv: Fix AT emulation for nested virtualization Dan Carpenter
  2026-11-19 13:25 ` [PATCH v2 1/4] KVM: arm64: nv: Fix AT emulation for TCR2_EL1 Dan Carpenter
@ 2026-11-19 13:25 ` Dan Carpenter
  2026-11-19 16:32   ` Marc Zyngier
  2026-11-19 13:25 ` [PATCH v2 3/4] KVM: arm64: nv: Clear hidden TCR2 bits for nested guests Dan Carpenter
  2026-11-19 13:25 ` [PATCH v2 4/4] KVM: arm64: nv: Update the shadow MMU's VMID for AT emulation Dan Carpenter
  3 siblings, 1 reply; 8+ messages in thread
From: Dan Carpenter @ 2026-11-19 13:25 UTC (permalink / raw)
  To: Marc Zyngier, Oliver Upton, Fuad Tabba, Joey Gouly, Steffen Eiden,
	Suzuki K Poulose, Zenghui Yu, Catalin Marinas, Will Deacon,
	Mark Rutland, Jintack Lim
  Cc: linux-arm-kernel, kvmarm, Dan Carpenter, Karthik Bhargavan,
	Clément Blaudeau, stable

A VMM can offer FEAT_TCR2 to a guest but hide FEAT_S1PIE or FEAT_S1POE,
for example this is done to keep the VM migratable.  KVM then stops
loading the PIR_EL1, PIRE0_EL1 and POR_EL1 registers for that guest.  But
the guest's writes to the TCR2_EL1 register don't trap, and the CPU still
acts on its PIE, POE and E0POE bits.

If a guest turned these features on, it would run with the permission
settings another VM left on the CPU.  Technically this is a kind of
information leak and a guest could figure out another guests permission
setting based on which accesses fault.

For these VMs, set the TCR_EL1 bit in HFGWTR_EL2 so that writes to
TCR_EL1 and TCR2_EL1 trap, and clear the hidden bits in a new
access_tcr2_el1() handler.

Fixes: 58627b722ee2 ("KVM: arm64: Make PIR{,E0}_EL1 UNDEF if S1PIE is not advertised to the guest")
Fixes: b86c9bea6349 ("KVM: arm64: Save/restore POE registers")
Cc: stable@vger.kernel.org
Signed-off-by: Dan Carpenter <dan@pereq.eu>
---
v2: This is a new patch

This is only a partial fix, I will update nested virtualization in the
next patch and the !vcpu_has_nv(vcpu) condition in __compute_hfgwtr() will
go away.
---
 arch/arm64/kvm/config.c   |  5 +++++
 arch/arm64/kvm/sys_regs.c | 14 +++++++++++++-
 2 files changed, 18 insertions(+), 1 deletion(-)

diff --git a/arch/arm64/kvm/config.c b/arch/arm64/kvm/config.c
index e295435a30b8..f756f7c4fdf8 100644
--- a/arch/arm64/kvm/config.c
+++ b/arch/arm64/kvm/config.c
@@ -1896,6 +1896,11 @@ static void __compute_hfgwtr(struct kvm_vcpu *vcpu)
 
 	if (cpus_have_final_cap(ARM64_WORKAROUND_AMPERE_AC03_CPU_38))
 		*vcpu_fgt(vcpu, HFGWTR_EL2) |= HFGWTR_EL2_TCR_EL1;
+
+	/* The CPU acts on hidden TCR2_EL1 bits, so trap writes to clear them */
+	if (!vcpu_has_nv(vcpu) && kvm_has_tcr2(vcpu->kvm) &&
+	    (!kvm_has_s1pie(vcpu->kvm) || !kvm_has_s1poe(vcpu->kvm)))
+		*vcpu_fgt(vcpu, HFGWTR_EL2) |= HFGWTR_EL2_TCR_EL1;
 }
 
 static void __compute_hdfgwtr(struct kvm_vcpu *vcpu)
diff --git a/arch/arm64/kvm/sys_regs.c b/arch/arm64/kvm/sys_regs.c
index afd7e76bedff..3899702d438b 100644
--- a/arch/arm64/kvm/sys_regs.c
+++ b/arch/arm64/kvm/sys_regs.c
@@ -615,6 +615,18 @@ static bool access_vm_reg(struct kvm_vcpu *vcpu,
 	return true;
 }
 
+static bool access_tcr2_el1(struct kvm_vcpu *vcpu,
+			    struct sys_reg_params *p,
+			    const struct sys_reg_desc *r)
+{
+	if (!kvm_has_s1pie(vcpu->kvm))
+		p->regval &= ~TCR2_EL1_PIE;
+	if (!kvm_has_s1poe(vcpu->kvm))
+		p->regval &= ~(TCR2_EL1_POE | TCR2_EL1_E0POE);
+
+	return access_vm_reg(vcpu, p, r);
+}
+
 static bool access_actlr(struct kvm_vcpu *vcpu,
 			 struct sys_reg_params *p,
 			 const struct sys_reg_desc *r)
@@ -3501,7 +3513,7 @@ static const struct sys_reg_desc sys_reg_descs[] = {
 	{ SYS_DESC(SYS_TTBR0_EL1), access_vm_reg, reset_unknown, TTBR0_EL1 },
 	{ SYS_DESC(SYS_TTBR1_EL1), access_vm_reg, reset_unknown, TTBR1_EL1 },
 	{ SYS_DESC(SYS_TCR_EL1), access_vm_reg, reset_val, TCR_EL1, 0 },
-	{ SYS_DESC(SYS_TCR2_EL1), access_vm_reg, reset_val, TCR2_EL1, 0,
+	{ SYS_DESC(SYS_TCR2_EL1), access_tcr2_el1, reset_val, TCR2_EL1, 0,
 	  .visibility = tcr2_visibility },
 
 	PTRAUTH_KEY(APIA),

-- 
2.53.0


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

* [PATCH v2 3/4] KVM: arm64: nv: Clear hidden TCR2 bits for nested guests
  2026-11-19 13:25 [PATCH v2 0/4] KVM: arm64: nv: Fix AT emulation for nested virtualization Dan Carpenter
  2026-11-19 13:25 ` [PATCH v2 1/4] KVM: arm64: nv: Fix AT emulation for TCR2_EL1 Dan Carpenter
  2026-11-19 13:25 ` [PATCH v2 2/4] KVM: arm64: Trap TCR2_EL1 writes when S1PIE or S1POE is hidden Dan Carpenter
@ 2026-11-19 13:25 ` Dan Carpenter
  2026-11-19 13:25 ` [PATCH v2 4/4] KVM: arm64: nv: Update the shadow MMU's VMID for AT emulation Dan Carpenter
  3 siblings, 0 replies; 8+ messages in thread
From: Dan Carpenter @ 2026-11-19 13:25 UTC (permalink / raw)
  To: Marc Zyngier, Oliver Upton, Fuad Tabba, Joey Gouly, Steffen Eiden,
	Suzuki K Poulose, Zenghui Yu, Catalin Marinas, Will Deacon,
	Mark Rutland, Jintack Lim
  Cc: linux-arm-kernel, kvmarm, Dan Carpenter, Karthik Bhargavan,
	Clément Blaudeau, stable

The previous patch added traps for TCR2_EL1 writes on VMs that have
FEAT_TCR2 but not FEAT_S1PIE or FEAT_S1POE.  This patch extends that
fix to cover nested virtualization.

The bug is that a KVM can hide the PIE and POE bits in TCR2_EL1 but a VM
can still write to them.  It obviously makes no sense to hide a register
from the VM but still allow writes. The implication of this bug is a
small information leak because a VM can determine the permission bits of
another VM's page tables.

The previous patch trapped writes to TCR2_EL1 but nested virtualization
is more complicated because we have to deal with TCR2_EL2 as well.  A
trapped TCR2_EL1 write has to go to either TCR2_EL2 or TCR2_EL1 depending
on who made it.  With nested virtualization the hidden bits can reach the
hardware in three ways:

- A guest hypervisor at vEL2 writes its TCR2_EL2.
- An L2 writes its own TCR2_EL1.
- An L1 writes its L2's TCR2_EL1 in the VNCR page, which KVM loads as is.

To fix this, remove the vcpu_has_nv() check in __compute_hfgwtr() so that
these writes are trapped too.  When the guest hypervisor is at vEL2,
update TCR2_EL2 and TCR_EL2 instead of L2's registers.  Also clear the
hidden bits in __sysreg_restore_el1_state() when it loads a guest's
TCR2_EL1.

Fixes: 58627b722ee2 ("KVM: arm64: Make PIR{,E0}_EL1 UNDEF if S1PIE is not advertised to the guest")
Fixes: b86c9bea6349 ("KVM: arm64: Save/restore POE registers")
Cc: stable@vger.kernel.org
Signed-off-by: Dan Carpenter <dan@pereq.eu>
---
v2: This is a new patch
---
 arch/arm64/kvm/config.c                    |  2 +-
 arch/arm64/kvm/hyp/include/hyp/sysreg-sr.h | 10 +++++++++-
 arch/arm64/kvm/sys_regs.c                  | 19 ++++++++++++++++++-
 3 files changed, 28 insertions(+), 3 deletions(-)

diff --git a/arch/arm64/kvm/config.c b/arch/arm64/kvm/config.c
index f756f7c4fdf8..8bde0a5eafb6 100644
--- a/arch/arm64/kvm/config.c
+++ b/arch/arm64/kvm/config.c
@@ -1898,7 +1898,7 @@ static void __compute_hfgwtr(struct kvm_vcpu *vcpu)
 		*vcpu_fgt(vcpu, HFGWTR_EL2) |= HFGWTR_EL2_TCR_EL1;
 
 	/* The CPU acts on hidden TCR2_EL1 bits, so trap writes to clear them */
-	if (!vcpu_has_nv(vcpu) && kvm_has_tcr2(vcpu->kvm) &&
+	if (kvm_has_tcr2(vcpu->kvm) &&
 	    (!kvm_has_s1pie(vcpu->kvm) || !kvm_has_s1poe(vcpu->kvm)))
 		*vcpu_fgt(vcpu, HFGWTR_EL2) |= HFGWTR_EL2_TCR_EL1;
 }
diff --git a/arch/arm64/kvm/hyp/include/hyp/sysreg-sr.h b/arch/arm64/kvm/hyp/include/hyp/sysreg-sr.h
index 4a008c8a696b..d6e8bab14ed3 100644
--- a/arch/arm64/kvm/hyp/include/hyp/sysreg-sr.h
+++ b/arch/arm64/kvm/hyp/include/hyp/sysreg-sr.h
@@ -261,7 +261,15 @@ static inline void __sysreg_restore_el1_state(struct kvm_cpu_context *ctxt,
 	write_sysreg_el1(ctxt_sys_reg(ctxt, TTBR0_EL1),	SYS_TTBR0);
 	write_sysreg_el1(ctxt_sys_reg(ctxt, TTBR1_EL1),	SYS_TTBR1);
 	if (ctxt_has_tcrx(ctxt)) {
-		write_sysreg_el1(ctxt_sys_reg(ctxt, TCR2_EL1),	SYS_TCR2);
+		u64 tcr2 = ctxt_sys_reg(ctxt, TCR2_EL1);
+
+		if (ctxt_is_guest(ctxt)) {
+			if (!ctxt_has_s1pie(ctxt))
+				tcr2 &= ~TCR2_EL1_PIE;
+			if (!ctxt_has_s1poe(ctxt))
+				tcr2 &= ~(TCR2_EL1_POE | TCR2_EL1_E0POE);
+		}
+		write_sysreg_el1(tcr2, SYS_TCR2);
 
 		if (ctxt_has_s1pie(ctxt)) {
 			write_sysreg_el1(ctxt_sys_reg(ctxt, PIR_EL1),	SYS_PIR);
diff --git a/arch/arm64/kvm/sys_regs.c b/arch/arm64/kvm/sys_regs.c
index 3899702d438b..e4a3beac6ec9 100644
--- a/arch/arm64/kvm/sys_regs.c
+++ b/arch/arm64/kvm/sys_regs.c
@@ -624,6 +624,23 @@ static bool access_tcr2_el1(struct kvm_vcpu *vcpu,
 	if (!kvm_has_s1poe(vcpu->kvm))
 		p->regval &= ~(TCR2_EL1_POE | TCR2_EL1_E0POE);
 
+	if (is_hyp_ctxt(vcpu) && vcpu_el2_e2h_is_set(vcpu)) {
+		vcpu_write_sys_reg(vcpu, p->regval, TCR2_EL2);
+		return true;
+	}
+
+	return access_vm_reg(vcpu, p, r);
+}
+
+static bool access_tcr_el1(struct kvm_vcpu *vcpu,
+			   struct sys_reg_params *p,
+			   const struct sys_reg_desc *r)
+{
+	if (is_hyp_ctxt(vcpu) && vcpu_el2_e2h_is_set(vcpu)) {
+		vcpu_write_sys_reg(vcpu, p->regval, TCR_EL2);
+		return true;
+	}
+
 	return access_vm_reg(vcpu, p, r);
 }
 
@@ -3512,7 +3529,7 @@ static const struct sys_reg_desc sys_reg_descs[] = {
 	{ SYS_DESC(SYS_SMCR_EL1), undef_access },
 	{ SYS_DESC(SYS_TTBR0_EL1), access_vm_reg, reset_unknown, TTBR0_EL1 },
 	{ SYS_DESC(SYS_TTBR1_EL1), access_vm_reg, reset_unknown, TTBR1_EL1 },
-	{ SYS_DESC(SYS_TCR_EL1), access_vm_reg, reset_val, TCR_EL1, 0 },
+	{ SYS_DESC(SYS_TCR_EL1), access_tcr_el1, reset_val, TCR_EL1, 0 },
 	{ SYS_DESC(SYS_TCR2_EL1), access_tcr2_el1, reset_val, TCR2_EL1, 0,
 	  .visibility = tcr2_visibility },
 

-- 
2.53.0


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

* [PATCH v2 4/4] KVM: arm64: nv: Update the shadow MMU's VMID for AT emulation
  2026-11-19 13:25 [PATCH v2 0/4] KVM: arm64: nv: Fix AT emulation for nested virtualization Dan Carpenter
                   ` (2 preceding siblings ...)
  2026-11-19 13:25 ` [PATCH v2 3/4] KVM: arm64: nv: Clear hidden TCR2 bits for nested guests Dan Carpenter
@ 2026-11-19 13:25 ` Dan Carpenter
  3 siblings, 0 replies; 8+ messages in thread
From: Dan Carpenter @ 2026-11-19 13:25 UTC (permalink / raw)
  To: Marc Zyngier, Oliver Upton, Fuad Tabba, Joey Gouly, Steffen Eiden,
	Suzuki K Poulose, Zenghui Yu, Catalin Marinas, Will Deacon,
	Mark Rutland, Jintack Lim
  Cc: linux-arm-kernel, kvmarm, Dan Carpenter, Karthik Bhargavan,
	Clément Blaudeau, stable

There is a missing kvm_arm_vmid_update() before the __load_stage2()
in __kvm_at_s1e01_fast().  The bug affects nested virtualization on
arm64.

In KVM a "stage 2" refers to the VM's page table mappings. The
__kvm_at_s1e01_fast() function emulates the AT instruction, which
translates a virtual address the way the MMU would and returns the
result.

Each stage 2 has a VMID, which the hardware uses to tag TLB and
walk-cache entries, so that one VM never uses another's entries.  There
are only 2^8 or 2^16 VMIDs, which is quite limited, so vmid.c hands
them out by generation.  When they run out, a rollover starts a new
generation and flushes the TLBs: the VMIDs active on a CPU at that
moment are kept, and every other VMID can be given to another VM.

The kvm_arm_vmid_update() function checks the VMID's generation, takes
a new VMID if it is from an old generation, and marks it as active on
this CPU.  Without this, the shadow MMU's VMID could be an old one that
now belongs to another VM, or a rollover on another CPU could hand it
to another VM while we are using it.  Either way the AT fills TLB entries
tagged with another VM's VMID.

To fix this, call kvm_arm_vmid_update() to update the VMID before loading
the shadow stage 2.  That marks it as active on the CPU.  Then at the end
of the function, undo that.  Restore the original VMID as active while
checking for and handling rollover.

Fixes: 477e89cabb14 ("KVM: arm64: nv: Add basic emulation of AT S1E{0,1}{R,W}")
Cc: stable@vger.kernel.org
Signed-off-by: Dan Carpenter <dan@pereq.eu>
---
v2: Unchanged
---
 arch/arm64/kvm/at.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/arch/arm64/kvm/at.c b/arch/arm64/kvm/at.c
index 9dfabd0586e2..5924cfa01bd4 100644
--- a/arch/arm64/kvm/at.c
+++ b/arch/arm64/kvm/at.c
@@ -1476,6 +1476,7 @@ static u64 __kvm_at_s1e01_fast(struct kvm_vcpu *vcpu, u32 op, u64 vaddr)
 		}
 	}
 	write_sysreg_el1(vcpu_read_sys_reg(vcpu, SCTLR_EL1),	SYS_SCTLR);
+	kvm_arm_vmid_update(&mmu->vmid);
 	__load_stage2(mmu);
 
 skip_mmu_switch:
@@ -1523,8 +1524,11 @@ static u64 __kvm_at_s1e01_fast(struct kvm_vcpu *vcpu, u32 op, u64 vaddr)
 
 	write_sysreg_hcr(HCR_HOST_VHE_FLAGS);
 
-	if (mmu_cs)
+	if (mmu_cs) {
+		kvm_arm_vmid_update(&vcpu->arch.hw_mmu->vmid);
+		config.vttbr = kvm_get_vttbr(vcpu->arch.hw_mmu);
 		__mmu_config_restore(&config);
+	}
 
 	return par;
 }

-- 
2.53.0


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

* Re: [PATCH v2 2/4] KVM: arm64: Trap TCR2_EL1 writes when S1PIE or S1POE is hidden
  2026-11-19 13:25 ` [PATCH v2 2/4] KVM: arm64: Trap TCR2_EL1 writes when S1PIE or S1POE is hidden Dan Carpenter
@ 2026-11-19 16:32   ` Marc Zyngier
  2026-11-20  5:33     ` Dan Carpenter
  0 siblings, 1 reply; 8+ messages in thread
From: Marc Zyngier @ 2026-11-19 16:32 UTC (permalink / raw)
  To: Dan Carpenter
  Cc: Oliver Upton, Fuad Tabba, Joey Gouly, Steffen Eiden,
	Suzuki K Poulose, Zenghui Yu, Catalin Marinas, Will Deacon,
	Mark Rutland, Jintack Lim, linux-arm-kernel, kvmarm,
	Karthik Bhargavan, Clément Blaudeau, stable

On Thu, 19 Nov 2026 13:25:35 +0000,
Dan Carpenter <dan@pereq.eu> wrote:
> 
> A VMM can offer FEAT_TCR2 to a guest but hide FEAT_S1PIE or FEAT_S1POE,
> for example this is done to keep the VM migratable.  KVM then stops
> loading the PIR_EL1, PIRE0_EL1 and POR_EL1 registers for that guest.  But
> the guest's writes to the TCR2_EL1 register don't trap, and the CPU still
> acts on its PIE, POE and E0POE bits.
> 
> If a guest turned these features on, it would run with the permission
> settings another VM left on the CPU.  Technically this is a kind of
> information leak and a guest could figure out another guests permission
> setting based on which accesses fault.
> 
> For these VMs, set the TCR_EL1 bit in HFGWTR_EL2 so that writes to
> TCR_EL1 and TCR2_EL1 trap, and clear the hidden bits in a new
> access_tcr2_el1() handler.
> 
> Fixes: 58627b722ee2 ("KVM: arm64: Make PIR{,E0}_EL1 UNDEF if S1PIE is not advertised to the guest")
> Fixes: b86c9bea6349 ("KVM: arm64: Save/restore POE registers")
> Cc: stable@vger.kernel.org
> Signed-off-by: Dan Carpenter <dan@pereq.eu>
> ---
> v2: This is a new patch
> 
> This is only a partial fix, I will update nested virtualization in the
> next patch and the !vcpu_has_nv(vcpu) condition in __compute_hfgwtr() will
> go away.

No, I really don't want to go down that road.

Trapping TCR_EL1 is an absolute performance sink, and at this point it
is not worth running VMs (it puts everyone at the level of AmpereOne).
It also puts the penalty on well behaved guests which should be able
to access TCR_EL1 as much as they want without penalties.

Yes, the architecture is terrible, and we have to work around it.  I
can see two ways to address this:

- load PIR/PIRE0/POR unconditionally so that they do not contain any
  state that logically belong to another guest, preferably with values
  that prevent any access (0 is a good candidate for the permissions).
  It means that if the guest plays with us, it will lose permission to
  its own memory accesses.

- Move TCR2_EL1 to the sanitised set, and make sure that these bits are
  never evaluated by KVM, and will be cleared on each entry (we
  already do this for TCR2_EL2).

Thanks,

	M.

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

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

* Re: [PATCH v2 2/4] KVM: arm64: Trap TCR2_EL1 writes when S1PIE or S1POE is hidden
  2026-11-19 16:32   ` Marc Zyngier
@ 2026-11-20  5:33     ` Dan Carpenter
  2026-11-20  9:23       ` Marc Zyngier
  0 siblings, 1 reply; 8+ messages in thread
From: Dan Carpenter @ 2026-11-20  5:33 UTC (permalink / raw)
  To: Marc Zyngier
  Cc: Oliver Upton, Fuad Tabba, Joey Gouly, Steffen Eiden,
	Suzuki K Poulose, Zenghui Yu, Catalin Marinas, Will Deacon,
	Mark Rutland, Jintack Lim, linux-arm-kernel, kvmarm,
	Karthik Bhargavan, Clément Blaudeau, stable

[-- Attachment #1: Type: text/plain, Size: 2003 bytes --]

On Thu, Nov 19, 2026 at 04:32:00PM +0000, Marc Zyngier wrote:
> On Thu, 19 Nov 2026 13:25:35 +0000,
> Dan Carpenter <dan@pereq.eu> wrote:
> > 
> > A VMM can offer FEAT_TCR2 to a guest but hide FEAT_S1PIE or FEAT_S1POE,
> > for example this is done to keep the VM migratable.  KVM then stops
> > loading the PIR_EL1, PIRE0_EL1 and POR_EL1 registers for that guest.  But
> > the guest's writes to the TCR2_EL1 register don't trap, and the CPU still
> > acts on its PIE, POE and E0POE bits.
> > 
> > If a guest turned these features on, it would run with the permission
> > settings another VM left on the CPU.  Technically this is a kind of
> > information leak and a guest could figure out another guests permission
> > setting based on which accesses fault.
> > 
> > For these VMs, set the TCR_EL1 bit in HFGWTR_EL2 so that writes to
> > TCR_EL1 and TCR2_EL1 trap, and clear the hidden bits in a new
> > access_tcr2_el1() handler.
> > 
> > Fixes: 58627b722ee2 ("KVM: arm64: Make PIR{,E0}_EL1 UNDEF if S1PIE is not advertised to the guest")
> > Fixes: b86c9bea6349 ("KVM: arm64: Save/restore POE registers")
> > Cc: stable@vger.kernel.org
> > Signed-off-by: Dan Carpenter <dan@pereq.eu>
> > ---
> > v2: This is a new patch
> > 
> > This is only a partial fix, I will update nested virtualization in the
> > next patch and the !vcpu_has_nv(vcpu) condition in __compute_hfgwtr() will
> > go away.
> 
> No, I really don't want to go down that road.
> 

Yeah.  Thanks, Marc.

I'm still a KVM newbie.  I'm embarrassed I sent that in retrospect.  I
had Claude create a checklist based on analysing your review comments so
I can hopefully get ahead of it a bit.  Attached for your amusement.
Apparently 10% of the time your review is "Why?"

Also Claude says that it could have found the Sashiko bugs by reviewing
patches to see if any different bugs get exposed by making a fix.  We
shall see.

Btw, I'm still hoping to get you that selftest but I need to send a few
more fixes first.

regards,
dan carpenter


[-- Attachment #2: marc-review-checklist.md --]
[-- Type: text/markdown, Size: 5346 bytes --]

# Predicting Marc Zyngier's review comments (KVM/arm64)

From 1000 of Marc's review emails (`~/progs/kernel/tmp/maz_reviews/sample/`), read in four parts, plus his
replies to our own patches in November 2026.  The counts are emails per 1000 (rough).  The checklist is the
part to use; the rest explains it.

## Pre-send checklist

**Rationale and correctness**
- [ ] The commit message says *why*, with the concrete sequence of events that goes wrong.  "Why?" / "What
      is the rationale?" is his most common question (~100).
- [ ] The behaviour matches the ARM ARM (or the GIC/PSCI spec), using its terms: UNDEF, RES0, CONSTRAINED
      UNPREDICTABLE.  Cite the section if it isn't obvious.  Not "what QEMU does" or "what x86 does" (~95).
- [ ] The root cause is fixed rather than papered over.  A WARN stays until the bug behind it is fixed.

**Cost**
- [ ] Nothing new runs on every entry or exit, and nothing new traps, unless it has to.  Prefer
      vcpu_load()/vcpu_put() or a static key.  Don't make VMs without the feature pay for it.  Under nested
      virtualization, ID-register reads and EL2 accesses trap, so count them.  Any performance claim needs
      numbers from an exit-heavy workload such as hackbench (~55).
      *Ours:* trapping TCR_EL1 to fix TCR2 got "an absolute performance sink"; the `loaded` checks in
      kvm_inject_nested() got "fast-path bloating".

**Minimal fixes**
- [ ] The smallest backportable change: no rework, no churn (renames, reordering, moving files,
      guard() conversions), no speculative fields or hooks, no Kconfig options ("battery included") (~40).
- [ ] Fixes: points at the real culprit, and only at something actually broken.  Cc: stable only if the
      backport is worth having (~30).

**Reuse**
- [ ] Use the existing infrastructure instead of a parallel mechanism: generated sysreg definitions,
      config.c feature maps, FGT/FGU, the trap tables, sanitised registers, vcpu requests, existing helpers.
      No magic numbers or duplicate definitions; use BIT()/GENMASK()/FIELD_PREP() (~55).

**Nested virtualization**
- [ ] Nested virtualization is VHE-only, and nVHE is effectively dead for new features.  Don't add code for
      combinations that can't exist.
- [ ] A new trap register comes with everything it traps: encoding_to_cgt[]/encoding_to_fgt[] entries, FGU,
      RES0/RES1 in kvm_init_nv_sysregs(), and VNCR where it applies.  No half-done features.
- [ ] Keep nested-only hacks local, so that someone who doesn't think about nested virtualization can't undo
      them by accident.

**Userspace ABI**
- [ ] Assume any VMM, in any order, and saving or restoring at any time; not only QEMU.  No KVM-internal state
      in the UAPI, and no new capabilities for KVM's own problems.  Tracepoints and stats are ABI.  Choose the
      errno with care.  Ask whether a WARN can be triggered from userspace: it panics under pKVM (~65).

**Locking**
- [ ] For each lock, READ_ONCE() or preempt_disable(), know what it protects against, and what stops the vcpu
      running or the value changing "behind your back" (~45).

**Code style** (~70, mostly "nit:")
- [ ] Clear names (not "Java-esque"); one helper per job, written once, no single-caller one-liners; no
      unneeded inline/__always_inline; no macro abuse.
- [ ] Comments only where the reason isn't obvious; no stale, wrong or obvious ones.
- [ ] Don't break lines just for 80 columns; he ignores checkpatch line length.

**Commit message**
- [ ] Short and technical: not a cover letter, not a recap of earlier versions, not marketing, no "No
      functional change intended" restating what was said.  It matches the code.  The subject says what the
      patch really does, with the right prefix ("KVM: arm64:", "KVM: arm64: nv:") (~40).

**Series and process**
- [ ] One logical change per patch, bisectable, documentation and UAPI in their own patches, the feature
      enabled last, each hunk in the patch that uses it (~45).
- [ ] A cover letter; a "Changes since vN" log for each version; Cc the maintainers and earlier reviewers;
      based on kvmarm/next or -rc1..-rc3, and only fixes after about -rc6; tested, and say how (QEMU as well
      as kvmtool, 64K pages, lockdep, a selftest for anything with a reproducer) (~60).
- [ ] When he replies with a diff ("completely untested", "How about this instead?"), take it.  If he writes a
      comment, paste it in verbatim.

## What makes him blunt

Ignored review comments, missing changelogs, maintainers left off Cc, untested code ("I stopped reading
here"), churn, hand-waving about performance without numbers, "go read the x86 patch", and wrong bot
findings.  He does credit correct bot findings ("Sashiko has spotted an interesting corner case") and
accepted a patch with "Assisted-by: Claude" without comment: what matters is whether the content is right.

## Our own patches, November 2026

| Patch | His comment | Checklist item |
|---|---|---|
| NULL deref v1, `loaded` checks | fast-path bloat; load the vcpu in the ioctl instead | Cost, minimal fixes |
| AT TCR2 v1 | asked for a comment explaining why it works; gave one | Paste his comment verbatim |
| TCR2 trap patches | trapping TCR_EL1 is a performance sink; load 0s instead | Cost |
| mmio_needed (Sashiko) | "multiple ways to commit" the exception state | Root cause, reuse |

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

* Re: [PATCH v2 2/4] KVM: arm64: Trap TCR2_EL1 writes when S1PIE or S1POE is hidden
  2026-11-20  5:33     ` Dan Carpenter
@ 2026-11-20  9:23       ` Marc Zyngier
  0 siblings, 0 replies; 8+ messages in thread
From: Marc Zyngier @ 2026-11-20  9:23 UTC (permalink / raw)
  To: Dan Carpenter
  Cc: Oliver Upton, Fuad Tabba, Joey Gouly, Steffen Eiden,
	Suzuki K Poulose, Zenghui Yu, Catalin Marinas, Will Deacon,
	Mark Rutland, Jintack Lim, linux-arm-kernel, kvmarm,
	Karthik Bhargavan, Clément Blaudeau, stable

On Fri, 20 Nov 2026 05:33:00 +0000,
Dan Carpenter <dan@pereq.eu> wrote:
> 
> [1  <text/plain; us-ascii (7bit)>]
> On Thu, Nov 19, 2026 at 04:32:00PM +0000, Marc Zyngier wrote:
> > On Thu, 19 Nov 2026 13:25:35 +0000,
> > Dan Carpenter <dan@pereq.eu> wrote:
> > > 
> > > A VMM can offer FEAT_TCR2 to a guest but hide FEAT_S1PIE or FEAT_S1POE,
> > > for example this is done to keep the VM migratable.  KVM then stops
> > > loading the PIR_EL1, PIRE0_EL1 and POR_EL1 registers for that guest.  But
> > > the guest's writes to the TCR2_EL1 register don't trap, and the CPU still
> > > acts on its PIE, POE and E0POE bits.
> > > 
> > > If a guest turned these features on, it would run with the permission
> > > settings another VM left on the CPU.  Technically this is a kind of
> > > information leak and a guest could figure out another guests permission
> > > setting based on which accesses fault.
> > > 
> > > For these VMs, set the TCR_EL1 bit in HFGWTR_EL2 so that writes to
> > > TCR_EL1 and TCR2_EL1 trap, and clear the hidden bits in a new
> > > access_tcr2_el1() handler.
> > > 
> > > Fixes: 58627b722ee2 ("KVM: arm64: Make PIR{,E0}_EL1 UNDEF if S1PIE is not advertised to the guest")
> > > Fixes: b86c9bea6349 ("KVM: arm64: Save/restore POE registers")
> > > Cc: stable@vger.kernel.org
> > > Signed-off-by: Dan Carpenter <dan@pereq.eu>
> > > ---
> > > v2: This is a new patch
> > > 
> > > This is only a partial fix, I will update nested virtualization in the
> > > next patch and the !vcpu_has_nv(vcpu) condition in __compute_hfgwtr() will
> > > go away.
> > 
> > No, I really don't want to go down that road.
> > 
> 
> Yeah.  Thanks, Marc.
> 
> I'm still a KVM newbie.  I'm embarrassed I sent that in retrospect.

Don't be embarrassed. There is nothing wrong with proposing a solution
and being told why it doesn't really work. If anything, you have
spotted an interesting problem that we had ignored so far, and forced
me into thinking about it. Seems like a net win to me.

> I had Claude create a checklist based on analysing your review
> comments so I can hopefully get ahead of it a bit.  Attached for
> your amusement.  Apparently 10% of the time your review is "Why?"

Oh man. This thing has summed me up, digested me and reduced me to a
markdown file. It's 9:15 and I feel like I need a drink...

On the other hand, some of this stuff could be used to add more
substance to Documentation/process/maintainer-kvm-arm64.rst (which is
only on our fixes branch for now, but on its way to Linus).

> Also Claude says that it could have found the Sashiko bugs by reviewing
> patches to see if any different bugs get exposed by making a fix.  We
> shall see.
> 
> Btw, I'm still hoping to get you that selftest but I need to send a few
> more fixes first.

Sure, no worries.

Thanks again,

	M.

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

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

end of thread, other threads:[~2026-11-20  9:23 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-19 13:25 [PATCH v2 0/4] KVM: arm64: nv: Fix AT emulation for nested virtualization Dan Carpenter
2026-11-19 13:25 ` [PATCH v2 1/4] KVM: arm64: nv: Fix AT emulation for TCR2_EL1 Dan Carpenter
2026-11-19 13:25 ` [PATCH v2 2/4] KVM: arm64: Trap TCR2_EL1 writes when S1PIE or S1POE is hidden Dan Carpenter
2026-11-19 16:32   ` Marc Zyngier
2026-11-20  5:33     ` Dan Carpenter
2026-11-20  9:23       ` Marc Zyngier
2026-11-19 13:25 ` [PATCH v2 3/4] KVM: arm64: nv: Clear hidden TCR2 bits for nested guests Dan Carpenter
2026-11-19 13:25 ` [PATCH v2 4/4] KVM: arm64: nv: Update the shadow MMU's VMID for AT emulation Dan Carpenter

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.