Skip to contentSkip to conversation

KVM: arm64: Synchronise speculative page table walks on translation regime change

PATCH Superseded Marc Zyngier 2 participants 12 messages Apr 8, 2023 – Apr 13, 2023

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Marc Zyngier

author maintainer
#0 lore raw

v2 cover KVM: arm64: Synchronise speculative page table walks on translation regime change

It recently became apparent that the way we switch our EL1&0 translation regime is not entirely fool proof.

On taking an exception from EL1&0 to EL2(&0), the page table walker is allowed to carry on with speculative walks started from EL1&0 while running at EL2 (see R_LFHQG). Given that the PTW may be actively using the EL1&0 system registers, the only safe way to deal with it is to issue a DSB before changing any of it.

We already did the right thing for SPE and TRBE, but ignored the PTW for unknown reasons (probably because the architecture wasn't crystal clear at the time).

This requires a bit of surgery in the nvhe code, though most of these patches are comments so that my future self can understand the purpose of these barriers. The VHE code is largely unaffected, thanks to the DSB in the context switch.

The last patch isn't directly related, but a superfluous ISB was spotted while working on this series.

- From v1 [1]

- Upgraded TLBIs' dsb(ishst) to dsb(ish) to cover the PTW's required barrier (thanks to Oliver for spotting the issue)

- Split the nVHE patch into 3 distinct patches for ease of reviewing.

- Brought the extra ISB patch into this series despite having been previously posted separately.

[1] https://lore.kernel.org/r/[email protected]

Marc Zyngier (5):
KVM: arm64: nvhe: Synchronise with page table walker on vcpu run
KVM: arm64: nvhe: Synchronise with page table walker on TLBI
KVM: arm64: pkvm: Document the side effects of
kvm_flush_dcache_to_poc()
KVM: arm64: vhe: Synchronise with page table walker on MMU update
KVM: arm64: vhe: Drop extra isb() on guest exit

 arch/arm64/kvm/hyp/nvhe/debug-sr.c    |  2 --
 arch/arm64/kvm/hyp/nvhe/mem_protect.c |  7 +++++++
 arch/arm64/kvm/hyp/nvhe/switch.c      | 18 ++++++++++++++++++
 arch/arm64/kvm/hyp/nvhe/tlb.c         | 24 +++++++++++++++++++-----
 arch/arm64/kvm/hyp/vhe/switch.c       |  7 +++----
 arch/arm64/kvm/hyp/vhe/sysreg-sr.c    | 12 ++++++++++++
 6 files changed, 59 insertions(+), 11 deletions(-)
Signature · 1 line
2.34.1
author maintainer
#1 lore raw

PATCH v2 1/5 KVM: arm64: nvhe: Synchronise with page table walker on vcpu run

When taking an exception between the EL1&0 translation regime and the EL2 translation regime, the page table walker is allowed to complete the walks started from EL0 or EL1 while running at EL2.

It means that altering the system registers that define the EL1&0 translation regime is fraught with danger *unless* we wait for the completion of such walk with a DSB (R_LFHQG and subsequent statements in the ARM ARM). We already did the right thing for other external agents (SPE, TRBE), but not the PTW.

Rework the existing SPE/TRBE synchronisation to include the PTW, and add the missing DSB on guest exit.

---
 arch/arm64/kvm/hyp/nvhe/debug-sr.c |  2 --
 arch/arm64/kvm/hyp/nvhe/switch.c   | 18 ++++++++++++++++++
 2 files changed, 18 insertions(+), 2 deletions(-)
arch/arm64/kvm/hyp/nvhe/debug-sr.c +0 -2
index 2673bde62fad..d756b939f296 100644
@@ -37,7 +37,6 @@ static void __debug_save_spe(u64 *pmscr_el1)
 
 	/* Now drain all buffered data to memory */
 	psb_csync();
-	dsb(nsh);
 }
 
 static void __debug_restore_spe(u64 pmscr_el1)
@@ -69,7 +68,6 @@ static void __debug_save_trace(u64 *trfcr_el1)
 	isb();
 	/* Drain the trace buffer to memory */
 	tsb_csync();
-	dsb(nsh);
 }
 
 static void __debug_restore_trace(u64 trfcr_el1)
arch/arm64/kvm/hyp/nvhe/switch.c +18 -0
index c2cb46ca4fb6..71fa16a0dc77 100644
@@ -272,6 +272,17 @@ int __kvm_vcpu_run(struct kvm_vcpu *vcpu)
 	 */
 	__debug_save_host_buffers_nvhe(vcpu);
 
+	/*
+	 * We're about to restore some new MMU state. Make sure
+	 * ongoing page-table walks that have started before we
+	 * trapped to EL2 have completed. This also synchronises the
+	 * above disabling of SPE and TRBE.
+	 *
+	 * See DDI0487I.a D8.1.5 "Out-of-context translation regimes",
+	 * rule R_LFHQG and subsequent information statements.
+	 */
+	dsb(nsh);
+
 	__kvm_adjust_pc(vcpu);
 
 	/*
@@ -306,6 +317,13 @@ int __kvm_vcpu_run(struct kvm_vcpu *vcpu)
 	__timer_disable_traps(vcpu);
 	__hyp_vgic_save_state(vcpu);
 
+	/*
+	 * Same thing as before the guest run: we're about to switch
+	 * the MMU context, so let's make sure we don't have any
+	 * ongoing EL1&0 translations.
+	 */
+	dsb(nsh);
+
 	__deactivate_traps(vcpu);
 	__load_host_stage2();
 
Signature · 1 line
2.34.1
Reviewed-by on v2 1/5
#2 lore raw

Re: [PATCH v2 1/5] KVM: arm64: nvhe: Synchronise with page table walker on vcpu run

Quoted 10 lines · Marc Zyngier

When taking an exception between the EL1&0 translation regime and the EL2 translation regime, the page table walker is allowed to complete the walks started from EL0 or EL1 while running at EL2.

It means that altering the system registers that define the EL1&0 translation regime is fraught with danger *unless* we wait for the completion of such walk with a DSB (R_LFHQG and subsequent statements in the ARM ARM). We already did the right thing for other external agents (SPE, TRBE), but not the PTW.

Quoted 4 lines

Rework the existing SPE/TRBE synchronisation to include the PTW, and add the missing DSB on guest exit.

Signature · 2 lines
Thanks,
Oliver
author maintainer
#3 lore raw

PATCH v2 2/5 KVM: arm64: nvhe: Synchronise with page table walker on TLBI

In reply to Marc Zyngier

A TLBI from EL2 impacting EL1 involves messing with the EL1&0 translation regime, and the page table walker may still be performing speculative walks.

Piggyback on the existing DSBs to always have a DSB ISH that will synchronise all load/store operations that the PTW may still have.

---
 arch/arm64/kvm/hyp/nvhe/tlb.c | 24 +++++++++++++++++++-----
 1 file changed, 19 insertions(+), 5 deletions(-)
arch/arm64/kvm/hyp/nvhe/tlb.c +19 -5
index d296d617f589..e86dd04d49ff 100644
@@ -17,6 +17,23 @@ struct tlb_inv_context {
 static void __tlb_switch_to_guest(struct kvm_s2_mmu *mmu,
 				  struct tlb_inv_context *cxt)
 {
+	/*
+	 * We have two requirements:
+	 *
+	 * - ensure that the page table updates are visible to all
+         *   CPUs, for which a dsb(ishst) is what we need
+	 *
+	 * - complete any speculative page table walk started before
+         *   we trapped to EL2 so that we can mess with the MM
+         *   registers out of context, for which dsb(nsh) is enough
+	 *
+	 * The composition of these two barriers is a dsb(ish). This
+	 * might be slightly over the top for non-shareable TLBIs, but
+	 * they are so vanishingly rare that it isn't worth the
+	 * complexity.
+	 */
+	dsb(ish);
+
 	if (cpus_have_final_cap(ARM64_WORKAROUND_SPECULATIVE_AT)) {
 		u64 val;
 
@@ -60,8 +77,6 @@ void __kvm_tlb_flush_vmid_ipa(struct kvm_s2_mmu *mmu,
 {
 	struct tlb_inv_context cxt;
 
-	dsb(ishst);
-
 	/* Switch to requested VMID */
 	__tlb_switch_to_guest(mmu, &cxt);
 
@@ -113,8 +128,6 @@ void __kvm_tlb_flush_vmid(struct kvm_s2_mmu *mmu)
 {
 	struct tlb_inv_context cxt;
 
-	dsb(ishst);
-
 	/* Switch to requested VMID */
 	__tlb_switch_to_guest(mmu, &cxt);
 
@@ -142,7 +155,8 @@ void __kvm_flush_cpu_context(struct kvm_s2_mmu *mmu)
 
 void __kvm_flush_vm_context(void)
 {
-	dsb(ishst);
+	/* Same remark as in __tblb_switch_to_guest() */
+	dsb(ish);
 	__tlbi(alle1is);
 
 	/*
Signature · 1 line
2.34.1
#4 lore raw

Re: [PATCH v2 2/5] KVM: arm64: nvhe: Synchronise with page table walker on TLBI

Hi Marc,

Quoted 28 lines · Marc Zyngier

A TLBI from EL2 impacting EL1 involves messing with the EL1&0 translation regime, and the page table walker may still be performing speculative walks.

Piggyback on the existing DSBs to always have a DSB ISH that will synchronise all load/store operations that the PTW may still have.

---
 arch/arm64/kvm/hyp/nvhe/tlb.c | 24 +++++++++++++++++++-----
 1 file changed, 19 insertions(+), 5 deletions(-)
arch/arm64/kvm/hyp/nvhe/tlb.c +7 -0
index d296d617f589..e86dd04d49ff 100644
@@ -17,6 +17,23 @@ struct tlb_inv_context {
 static void __tlb_switch_to_guest(struct kvm_s2_mmu *mmu,
 				  struct tlb_inv_context *cxt)
 {
+	/*
+	 * We have two requirements:
+	 *
+	 * - ensure that the page table updates are visible to all
+         *   CPUs, for which a dsb(ishst) is what we need
+	 *
+	 * - complete any speculative page table walk started before

arch/arm64/kvm/hyp/nvhe/tlb.c struct tlb_inv_context { · line 36

Quoted 10 lines
arch/arm64/kvm/hyp/nvhe/tlb.c +10 -0
+         *   we trapped to EL2 so that we can mess with the MM
+         *   registers out of context, for which dsb(nsh) is enough
+	 *
+	 * The composition of these two barriers is a dsb(ish). This
+	 * might be slightly over the top for non-shareable TLBIs, but
+	 * they are so vanishingly rare that it isn't worth the
+	 * complexity.
+	 */
+	dsb(ish);
+

Ricardo is carrying a patch for non-shareable TLBIs on permission relaxation [*], and he's found that it produces some rather desirable performance improvements. I appreciate the elegance of your approach, but given what's coming does it make sense to have the TLBI handlers continue to explicitly perform the appropriate DSB?

[*] https://lore.kernel.org/kvmarm/[email protected]/

Quoted 22 lines
arch/arm64/kvm/hyp/nvhe/tlb.c +0 -4
 	if (cpus_have_final_cap(ARM64_WORKAROUND_SPECULATIVE_AT)) {
 		u64 val;
 
@@ -60,8 +77,6 @@ void __kvm_tlb_flush_vmid_ipa(struct kvm_s2_mmu *mmu,
 {
 	struct tlb_inv_context cxt;
 
-	dsb(ishst);
-
 	/* Switch to requested VMID */
 	__tlb_switch_to_guest(mmu, &cxt);
 
@@ -113,8 +128,6 @@ void __kvm_tlb_flush_vmid(struct kvm_s2_mmu *mmu)
 {
 	struct tlb_inv_context cxt;
 
-	dsb(ishst);
-
 	/* Switch to requested VMID */
 	__tlb_switch_to_guest(mmu, &cxt);
 

arch/arm64/kvm/hyp/nvhe/tlb.c void __kvm_flush_cpu_context(struct kvm_s2_mmu *mmu) · line 158

Quoted 5 lines
arch/arm64/kvm/hyp/nvhe/tlb.c +1 -1
@@ -142,7 +155,8 @@ void __kvm_flush_cpu_context(struct kvm_s2_mmu *mmu)
 
 void __kvm_flush_vm_context(void)
 {
-	dsb(ishst);
+	/* Same remark as in __tblb_switch_to_guest() */

typo: __tlb_switch_to_guest()

Quoted 8 lines
arch/arm64/kvm/hyp/nvhe/tlb.c +1 -0
+	dsb(ish);
 	__tlbi(alle1is);
 
 	/*
Signature · 1 line
2.34.1
Signature · 2 lines
Thanks,
Oliver
author maintainer
#5 lore raw

Re: [PATCH v2 2/5] KVM: arm64: nvhe: Synchronise with page table walker on TLBI

Quoted 40 lines · Oliver Upton

Hi Marc,

Quoted 36 lines · Marc Zyngier

A TLBI from EL2 impacting EL1 involves messing with the EL1&0 translation regime, and the page table walker may still be performing speculative walks.

Piggyback on the existing DSBs to always have a DSB ISH that will synchronise all load/store operations that the PTW may still have.

---
 arch/arm64/kvm/hyp/nvhe/tlb.c | 24 +++++++++++++++++++-----
 1 file changed, 19 insertions(+), 5 deletions(-)
arch/arm64/kvm/hyp/nvhe/tlb.c +15 -0
index d296d617f589..e86dd04d49ff 100644
@@ -17,6 +17,23 @@ struct tlb_inv_context {
 static void __tlb_switch_to_guest(struct kvm_s2_mmu *mmu,
 				  struct tlb_inv_context *cxt)
 {
+	/*
+	 * We have two requirements:
+	 *
+	 * - ensure that the page table updates are visible to all
+         *   CPUs, for which a dsb(ishst) is what we need
+	 *
+	 * - complete any speculative page table walk started before
+         *   we trapped to EL2 so that we can mess with the MM
+         *   registers out of context, for which dsb(nsh) is enough
+	 *
+	 * The composition of these two barriers is a dsb(ish). This
+	 * might be slightly over the top for non-shareable TLBIs, but
+	 * they are so vanishingly rare that it isn't worth the
+	 * complexity.
+	 */
Quoted 8 lines

arch/arm64/kvm/hyp/nvhe/tlb.c struct tlb_inv_context { · line 36

Quoted 2 lines
arch/arm64/kvm/hyp/nvhe/tlb.c +2 -0
+	dsb(ish);
+

Ricardo is carrying a patch for non-shareable TLBIs on permission relaxation [*], and he's found that it produces some rather desirable performance improvements. I appreciate the elegance of your approach, but given what's coming does it make sense to have the TLBI handlers continue to explicitly perform the appropriate DSB?

Ah, I forgot about my own patch! :D

Right, let me see if I can do something clever here...

Thanks,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
author maintainer
#6 lore raw

PATCH v2 3/5 KVM: arm64: pkvm: Document the side effects of kvm_flush_dcache_to_poc()

In reply to Marc Zyngier

We rely on the presence of a DSB at the end of kvm_flush_dcache_to_poc() that, on top of ensuring completion of the cache clean, also covers the speculative page table walk started from EL1.

Document this dependency.

---
 arch/arm64/kvm/hyp/nvhe/mem_protect.c | 7 +++++++
 1 file changed, 7 insertions(+)
arch/arm64/kvm/hyp/nvhe/mem_protect.c +7 -0
index 552653fa18be..2e9ec4a2a4a3 100644
@@ -297,6 +297,13 @@ int __pkvm_prot_finalize(void)
 	params->vttbr = kvm_get_vttbr(mmu);
 	params->vtcr = host_mmu.arch.vtcr;
 	params->hcr_el2 |= HCR_VM;
+
+	/*
+	 * The CMO below not only cleans the updated params to the
+	 * PoC, but also provides the DSB that ensures ongoing
+	 * page-table walks that have started before we trapped to EL2
+	 * have completed.
+	 */
 	kvm_flush_dcache_to_poc(params, sizeof(*params));
 
 	write_sysreg(params->hcr_el2, hcr_el2);
Signature · 1 line
2.34.1
Reviewed-by on v2 3/5
#7 lore raw

Re: [PATCH v2 3/5] KVM: arm64: pkvm: Document the side effects of kvm_flush_dcache_to_poc()

Quoted 7 lines · Marc Zyngier

We rely on the presence of a DSB at the end of kvm_flush_dcache_to_poc() that, on top of ensuring completion of the cache clean, also covers the speculative page table walk started from EL1.

Document this dependency.

Signature · 2 lines
Thanks,
Oliver
author maintainer
#8 lore raw

PATCH v2 4/5 KVM: arm64: vhe: Synchronise with page table walker on MMU update

In reply to Marc Zyngier

Contrary to nVHE, VHE is a lot easier when it comes to dealing with speculative page table walks started at EL1. As we only change EL1&0 translation regime when context-switching, we already benefit from the effect of the DSB that sits in the context switch code.

We only need to take care of it in the NV case, where we can flip between between two EL1 contexts (one of them being the virtual EL2) without a context switch.

---
 arch/arm64/kvm/hyp/vhe/sysreg-sr.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
arch/arm64/kvm/hyp/vhe/sysreg-sr.c +12 -0
index 7b44f6b3b547..b35a178e7e0d 100644
@@ -13,6 +13,7 @@
 #include <asm/kvm_asm.h>
 #include <asm/kvm_emulate.h>
 #include <asm/kvm_hyp.h>
+#include <asm/kvm_nested.h>
 
 /*
  * VHE: Host and guest must save mdscr_el1 and sp_el0 (and the PC and
@@ -69,6 +70,17 @@ void kvm_vcpu_load_sysregs_vhe(struct kvm_vcpu *vcpu)
 	host_ctxt = &this_cpu_ptr(&kvm_host_data)->host_ctxt;
 	__sysreg_save_user_state(host_ctxt);
 
+	/*
+	 * When running a normal EL1 guest, we only load a new vcpu
+	 * after a context switch, which imvolves a DSB, so all
+	 * speculative EL1&0 walks will have already completed.
+	 * If running NV, the vcpu may transition between vEL1 and
+	 * vEL2 without a context switch, so make sure we complete
+	 * those walks before loading a new context.
+	 */
+	if (vcpu_has_nv(vcpu))
+		dsb(nsh);
+
 	/*
 	 * Load guest EL1 and user state
 	 *
Signature · 1 line
2.34.1
Reviewed-by on v2 4/5
#9 lore raw

Re: [PATCH v2 4/5] KVM: arm64: vhe: Synchronise with page table walker on MMU update

Quoted 10 lines · Marc Zyngier

Contrary to nVHE, VHE is a lot easier when it comes to dealing with speculative page table walks started at EL1. As we only change EL1&0 translation regime when context-switching, we already benefit from the effect of the DSB that sits in the context switch code.

We only need to take care of it in the NV case, where we can flip between between two EL1 contexts (one of them being the virtual EL2) without a context switch.

Signature · 2 lines
Thanks,
Oliver
author maintainer
#10 lore raw

PATCH v2 5/5 KVM: arm64: vhe: Drop extra isb() on guest exit

In reply to Marc Zyngier

__kvm_vcpu_run_vhe() end on VHE with an isb(). However, this function is only reachable via kvm_call_hyp_ret(), which already contains an isb() in order to mimick the behaviour of nVHE and provide a context synchronisation event.

We thus have two isb()s back to back, which is one too many. Drop the first one and solely rely on the one in the helper.

---
 arch/arm64/kvm/hyp/vhe/switch.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
arch/arm64/kvm/hyp/vhe/switch.c +3 -4
index cd3f3117bf16..3d868e84c7a0 100644
@@ -227,11 +227,10 @@ int __kvm_vcpu_run(struct kvm_vcpu *vcpu)
 
 	/*
 	 * When we exit from the guest we change a number of CPU configuration
-	 * parameters, such as traps.  Make sure these changes take effect
-	 * before running the host or additional guests.
+	 * parameters, such as traps.  We rely on the isb() in kvm_call_hyp*()
+	 * to make sure these changes take effect before running the host or
+	 * additional guests.
 	 */
-	isb();
-
 	return ret;
 }
 
Signature · 1 line
2.34.1
Reviewed-by on v2 5/5
#11 lore raw

Re: [PATCH v2 5/5] KVM: arm64: vhe: Drop extra isb() on guest exit

Quoted 9 lines · Marc Zyngier

__kvm_vcpu_run_vhe() end on VHE with an isb(). However, this function is only reachable via kvm_call_hyp_ret(), which already contains an isb() in order to mimick the behaviour of nVHE and provide a context synchronisation event.

We thus have two isb()s back to back, which is one too many. Drop the first one and solely rely on the one in the helper.

Signature · 2 lines
Thanks,
Oliver