Skip to contentSkip to conversation

KVM: arm64: Fixes to early stage-2 fault handling

PATCH Applied Will Deacon 2 participants 6 messages Jul 29, 2020 – Jul 30, 2020

Subsystems (2)
lore
Share & tools
lore

Will Deacon

author maintainer
#0 lore raw

v2 cover KVM: arm64: Fixes to early stage-2 fault handling

Hi all,

This is version two of the patches I posted last week:

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

I got my brain in a twist with exactly what is reported in HPFAR for a stage-2 abort on a stage-1 table walk, so I don't think any of these are serious any more.

With these changes, the early stage-2 fault handling follows:

S2 instruction abort:
* Not in memslot, or S2 fault on S1 walk for tables in R/O memslot:
=> inject external iabt to guest

S2 data abort:
* Not in memslot:

    - S2 fault on S1 walk:      inject external dabt to guest
    - Cache maintenance:        skip instr
    - Syndrome valid            EXIT_MMIO
    - Syndrome invalid          EXIT_NISV

* Write fault in R/O memslot:
- S2 fault on S1 walk: inject external dabt to guest
- Access is write:
- Syndrome valid EXIT_MMIO
- Syndrome invalid EXIT_NISV (includes cache maintenance)

Everything else gets handled by handle_access_fault()/user_mem_abort().

Will

Cc James Morse <[email protected]>
Cc Suzuki Poulose <[email protected]>

--->8

Will Deacon (4):
KVM: arm64: Rename kvm_vcpu_dabt_isextabt()
KVM: arm64: Handle data and instruction external aborts the same way
KVM: arm64: Don't skip cache maintenance for read-only memslots
KVM: arm64: Move S1PTW S2 fault logic out of io_mem_abort()

 arch/arm64/include/asm/kvm_emulate.h |  2 +-
 arch/arm64/kvm/hyp/switch.c          |  2 +-
 arch/arm64/kvm/mmio.c                |  6 ------
 arch/arm64/kvm/mmu.c                 | 26 +++++++++++++++++---------
 4 files changed, 19 insertions(+), 17 deletions(-)
Signature · 6 lines
2.28.0.rc0.142.g3c755180ce-goog

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
author maintainer
#1 lore raw

PATCH v2 1/4 KVM: arm64: Rename kvm_vcpu_dabt_isextabt()

kvm_vcpu_dabt_isextabt() is not specific to data aborts and, unlike kvm_vcpu_dabt_issext(), has nothing to do with sign extension.

Rename it to 'kvm_vcpu_abt_issea()'.

Cc Marc Zyngier <[email protected]>
Cc Quentin Perret <[email protected]>
---
 arch/arm64/include/asm/kvm_emulate.h | 2 +-
 arch/arm64/kvm/hyp/switch.c          | 2 +-
 arch/arm64/kvm/mmu.c                 | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)
arch/arm64/include/asm/kvm_emulate.h +1 -1
index 4d0f8ea600ba..26eabf987e71 100644
@@ -366,7 +366,7 @@ static __always_inline u8 kvm_vcpu_trap_get_fault_type(const struct kvm_vcpu *vc
 	return kvm_vcpu_get_hsr(vcpu) & ESR_ELx_FSC_TYPE;
 }
 
-static __always_inline bool kvm_vcpu_dabt_isextabt(const struct kvm_vcpu *vcpu)
+static __always_inline bool kvm_vcpu_abt_issea(const struct kvm_vcpu *vcpu)
 {
 	switch (kvm_vcpu_trap_get_fault(vcpu)) {
 	case FSC_SEA:
arch/arm64/kvm/hyp/switch.c +1 -1
index db1c4487d95d..72459d955799 100644
@@ -594,7 +594,7 @@ static bool __hyp_text fixup_guest_exit(struct kvm_vcpu *vcpu, u64 *exit_code)
 		valid = kvm_vcpu_trap_get_class(vcpu) == ESR_ELx_EC_DABT_LOW &&
 			kvm_vcpu_trap_get_fault_type(vcpu) == FSC_FAULT &&
 			kvm_vcpu_dabt_isvalid(vcpu) &&
-			!kvm_vcpu_dabt_isextabt(vcpu) &&
+			!kvm_vcpu_abt_issea(vcpu) &&
 			!kvm_vcpu_dabt_iss1tw(vcpu);
 
 		if (valid) {
arch/arm64/kvm/mmu.c +1 -1
index df2a8025ec8a..97394d56a5a9 100644
@@ -2074,7 +2074,7 @@ int kvm_handle_guest_abort(struct kvm_vcpu *vcpu, struct kvm_run *run)
 	is_iabt = kvm_vcpu_trap_is_iabt(vcpu);
 
 	/* Synchronous External Abort? */
-	if (kvm_vcpu_dabt_isextabt(vcpu)) {
+	if (kvm_vcpu_abt_issea(vcpu)) {
 		/*
 		 * For RAS the host kernel may handle this abort.
 		 * There is no need to pass the error into the guest.
Signature · 6 lines
2.28.0.rc0.142.g3c755180ce-goog

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
author maintainer
#2 lore raw

PATCH v2 2/4 KVM: arm64: Handle data and instruction external aborts the same way

In reply to Will Deacon

If the guest generates a synchronous external abort which is not handled by the host, we inject it back into the guest as a virtual SError, but only if the original fault was reported on the data side. Instruction faults are reported as "Unsupported FSC", causing the vCPU run loop to bail with -EFAULT.

Although synchronous external aborts from a guest are pretty unusual, treat them the same regardless of whether they are taken as data or instruction aborts by EL2.

Cc Marc Zyngier <[email protected]>
Cc Quentin Perret <[email protected]>
---
 arch/arm64/kvm/mmu.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
arch/arm64/kvm/mmu.c +3 -6
index 97394d56a5a9..96d995a1ef68 100644
@@ -2079,13 +2079,10 @@ int kvm_handle_guest_abort(struct kvm_vcpu *vcpu, struct kvm_run *run)
 		 * For RAS the host kernel may handle this abort.
 		 * There is no need to pass the error into the guest.
 		 */
-		if (!kvm_handle_guest_sea(fault_ipa, kvm_vcpu_get_hsr(vcpu)))
-			return 1;
-
-		if (unlikely(!is_iabt)) {
+		if (kvm_handle_guest_sea(fault_ipa, kvm_vcpu_get_hsr(vcpu)))
 			kvm_inject_vabt(vcpu);
-			return 1;
-		}
+
+		return 1;
 	}
 
 	trace_kvm_guest_fault(*vcpu_pc(vcpu), kvm_vcpu_get_hsr(vcpu),
Signature · 6 lines
2.28.0.rc0.142.g3c755180ce-goog

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
author maintainer
#3 lore raw

PATCH v2 3/4 KVM: arm64: Don't skip cache maintenance for read-only memslots

In reply to Will Deacon

If a guest performs cache maintenance on a read-only memslot, we should inform userspace rather than skip the instruction altogether.

Cc Marc Zyngier <[email protected]>
Cc Quentin Perret <[email protected]>
---
 arch/arm64/kvm/mmu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm64/kvm/mmu.c +1 -1
index 96d995a1ef68..4150bce3d0b6 100644
@@ -2121,7 +2121,7 @@ int kvm_handle_guest_abort(struct kvm_vcpu *vcpu, struct kvm_run *run)
 		 * So let's assume that the guest is just being
 		 * cautious, and skip the instruction.
 		 */
-		if (kvm_vcpu_dabt_is_cm(vcpu)) {
+		if (kvm_is_error_hva(hva) && kvm_vcpu_dabt_is_cm(vcpu)) {
 			kvm_skip_instr(vcpu, kvm_vcpu_trap_il_is32bit(vcpu));
 			ret = 1;
 			goto out_unlock;
Signature · 6 lines
2.28.0.rc0.142.g3c755180ce-goog

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
author maintainer
#4 lore raw

PATCH v2 4/4 KVM: arm64: Move S1PTW S2 fault logic out of io_mem_abort()

In reply to Will Deacon

To allow for re-injection of stage-2 faults on stage-1 page-table walks due to either a missing or read-only memslot, move the triage logic out of io_mem_abort() and into kvm_handle_guest_abort(), where these aborts can be handled before anything else.

Cc Marc Zyngier <[email protected]>
Cc Quentin Perret <[email protected]>
---
 arch/arm64/kvm/mmio.c |  6 ------
 arch/arm64/kvm/mmu.c  | 13 ++++++++++++-
 2 files changed, 12 insertions(+), 7 deletions(-)
arch/arm64/kvm/mmio.c +0 -6
index 4e0366759726..58de2ae4f6bb 100644
@@ -145,12 +145,6 @@ int io_mem_abort(struct kvm_vcpu *vcpu, struct kvm_run *run,
 		return -ENOSYS;
 	}
 
-	/* Page table accesses IO mem: tell guest to fix its TTBR */
-	if (kvm_vcpu_dabt_iss1tw(vcpu)) {
-		kvm_inject_dabt(vcpu, kvm_vcpu_get_hfar(vcpu));
-		return 1;
-	}
-
 	/*
 	 * Prepare MMIO operation. First decode the syndrome data we get
 	 * from the CPU. Then try if some in-kernel emulation feels
arch/arm64/kvm/mmu.c +12 -1
index 4150bce3d0b6..9f5fde1243d4 100644
@@ -2105,12 +2105,23 @@ int kvm_handle_guest_abort(struct kvm_vcpu *vcpu, struct kvm_run *run)
 	hva = gfn_to_hva_memslot_prot(memslot, gfn, &writable);
 	write_fault = kvm_is_write_fault(vcpu);
 	if (kvm_is_error_hva(hva) || (write_fault && !writable)) {
+		/*
+		 * The guest has put either its instructions or its page-tables
+		 * somewhere it shouldn't have. Userspace won't be able to do
+		 * anything about this (there's no syndrome for a start), so
+		 * re-inject the abort back into the guest.
+		 */
 		if (is_iabt) {
-			/* Prefetch Abort on I/O address */
 			ret = -ENOEXEC;
 			goto out;
 		}
 
+		if (kvm_vcpu_dabt_iss1tw(vcpu)) {
+			kvm_inject_dabt(vcpu, kvm_vcpu_get_hfar(vcpu));
+			ret = 1;
+			goto out_unlock;
+		}
+
 		/*
 		 * Check for a cache maintenance operation. Since we
 		 * ended-up here, we know it is outside of any memory
Signature · 6 lines
2.28.0.rc0.142.g3c755180ce-goog

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
maintainer Applied
#5 lore raw
In reply to Will Deacon
Quoted 9 lines · Will Deacon

This is version two of the patches I posted last week:

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

I got my brain in a twist with exactly what is reported in HPFAR for a stage-2 abort on a stage-1 table walk, so I don't think any of these are serious any more.

[...]

Applied to kvm-arm64/misc-5.9, thanks!

[1/4] KVM: arm64: Rename kvm_vcpu_dabt_isextabt()

      commit: c9a636f29b5f236441ff059cef0b2fe734c05afd
[2/4] KVM: arm64: Handle data and instruction external aborts the same way
      commit: 84b951a803a5464b0bff2fb1366e96f07f75b066
[3/4] KVM: arm64: Don't skip cache maintenance for read-only memslots
      commit: 54dc0d2404dd7aa0dd4e4f388a65622b68c6eaff
[4/4] KVM: arm64: Move S1PTW S2 fault logic out of io_mem_abort()
      commit: 022c8328dc8021248047b373b9f67790641b8f2d

Cheers,

	M.
Signature · 7 lines
Without deviation from the norm, progress is not possible.


_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm