Skip to content

Commit fbc7e61

Browse files
mrutland-armMarc Zyngier
authored andcommitted
KVM: arm64: Unconditionally save+flush host FPSIMD/SVE/SME state
There are several problems with the way hyp code lazily saves the host's FPSIMD/SVE state, including: * Host SVE being discarded unexpectedly due to inconsistent configuration of TIF_SVE and CPACR_ELx.ZEN. This has been seen to result in QEMU crashes where SVE is used by memmove(), as reported by Eric Auger: https://issues.redhat.com/browse/RHEL-68997 * Host SVE state is discarded *after* modification by ptrace, which was an unintentional ptrace ABI change introduced with lazy discarding of SVE state. * The host FPMR value can be discarded when running a non-protected VM, where FPMR support is not exposed to a VM, and that VM uses FPSIMD/SVE. In these cases the hyp code does not save the host's FPMR before unbinding the host's FPSIMD/SVE/SME state, leaving a stale value in memory. Avoid these by eagerly saving and "flushing" the host's FPSIMD/SVE/SME state when loading a vCPU such that KVM does not need to save any of the host's FPSIMD/SVE/SME state. For clarity, fpsimd_kvm_prepare() is removed and the necessary call to fpsimd_save_and_flush_cpu_state() is placed in kvm_arch_vcpu_load_fp(). As 'fpsimd_state' and 'fpmr_ptr' should not be used, they are set to NULL; all uses of these will be removed in subsequent patches. Historical problems go back at least as far as v5.17, e.g. erroneous assumptions about TIF_SVE being clear in commit: 8383741 ("KVM: arm64: Get rid of host SVE tracking/saving") ... and so this eager save+flush probably needs to be backported to ALL stable trees. Fixes: 93ae6b0 ("KVM: arm64: Discard any SVE state when entering KVM guests") Fixes: 8c845e2 ("arm64/sve: Leave SVE enabled on syscall if we don't context switch") Fixes: ef3be86 ("KVM: arm64: Add save/restore support for FPMR") Reported-by: Eric Auger <[email protected]> Reported-by: Wilco Dijkstra <[email protected]> Reviewed-by: Mark Brown <[email protected]> Tested-by: Mark Brown <[email protected]> Tested-by: Eric Auger <[email protected]> Acked-by: Will Deacon <[email protected]> Cc: Catalin Marinas <[email protected]> Cc: Florian Weimer <[email protected]> Cc: Fuad Tabba <[email protected]> Cc: Jeremy Linton <[email protected]> Cc: Marc Zyngier <[email protected]> Cc: Oliver Upton <[email protected]> Cc: Paolo Bonzini <[email protected]> Signed-off-by: Mark Rutland <[email protected]> Reviewed-by: Oliver Upton <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Marc Zyngier <[email protected]>
1 parent 8dbccaf commit fbc7e61

File tree

2 files changed

+10
-50
lines changed

2 files changed

+10
-50
lines changed

arch/arm64/kernel/fpsimd.c

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1694,31 +1694,6 @@ void fpsimd_signal_preserve_current_state(void)
16941694
sve_to_fpsimd(current);
16951695
}
16961696

1697-
/*
1698-
* Called by KVM when entering the guest.
1699-
*/
1700-
void fpsimd_kvm_prepare(void)
1701-
{
1702-
if (!system_supports_sve())
1703-
return;
1704-
1705-
/*
1706-
* KVM does not save host SVE state since we can only enter
1707-
* the guest from a syscall so the ABI means that only the
1708-
* non-saved SVE state needs to be saved. If we have left
1709-
* SVE enabled for performance reasons then update the task
1710-
* state to be FPSIMD only.
1711-
*/
1712-
get_cpu_fpsimd_context();
1713-
1714-
if (test_and_clear_thread_flag(TIF_SVE)) {
1715-
sve_to_fpsimd(current);
1716-
current->thread.fp_type = FP_STATE_FPSIMD;
1717-
}
1718-
1719-
put_cpu_fpsimd_context();
1720-
}
1721-
17221697
/*
17231698
* Associate current's FPSIMD context with this cpu
17241699
* The caller must have ownership of the cpu FPSIMD context before calling

arch/arm64/kvm/fpsimd.c

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,18 @@ void kvm_arch_vcpu_load_fp(struct kvm_vcpu *vcpu)
5454
if (!system_supports_fpsimd())
5555
return;
5656

57-
fpsimd_kvm_prepare();
58-
5957
/*
60-
* We will check TIF_FOREIGN_FPSTATE just before entering the
61-
* guest in kvm_arch_vcpu_ctxflush_fp() and override this to
62-
* FP_STATE_FREE if the flag set.
58+
* Ensure that any host FPSIMD/SVE/SME state is saved and unbound such
59+
* that the host kernel is responsible for restoring this state upon
60+
* return to userspace, and the hyp code doesn't need to save anything.
61+
*
62+
* When the host may use SME, fpsimd_save_and_flush_cpu_state() ensures
63+
* that PSTATE.{SM,ZA} == {0,0}.
6364
*/
64-
*host_data_ptr(fp_owner) = FP_STATE_HOST_OWNED;
65-
*host_data_ptr(fpsimd_state) = kern_hyp_va(&current->thread.uw.fpsimd_state);
66-
*host_data_ptr(fpmr_ptr) = kern_hyp_va(&current->thread.uw.fpmr);
65+
fpsimd_save_and_flush_cpu_state();
66+
*host_data_ptr(fp_owner) = FP_STATE_FREE;
67+
*host_data_ptr(fpsimd_state) = NULL;
68+
*host_data_ptr(fpmr_ptr) = NULL;
6769

6870
host_data_clear_flag(HOST_SVE_ENABLED);
6971
if (read_sysreg(cpacr_el1) & CPACR_EL1_ZEN_EL0EN)
@@ -73,23 +75,6 @@ void kvm_arch_vcpu_load_fp(struct kvm_vcpu *vcpu)
7375
host_data_clear_flag(HOST_SME_ENABLED);
7476
if (read_sysreg(cpacr_el1) & CPACR_EL1_SMEN_EL0EN)
7577
host_data_set_flag(HOST_SME_ENABLED);
76-
77-
/*
78-
* If PSTATE.SM is enabled then save any pending FP
79-
* state and disable PSTATE.SM. If we leave PSTATE.SM
80-
* enabled and the guest does not enable SME via
81-
* CPACR_EL1.SMEN then operations that should be valid
82-
* may generate SME traps from EL1 to EL1 which we
83-
* can't intercept and which would confuse the guest.
84-
*
85-
* Do the same for PSTATE.ZA in the case where there
86-
* is state in the registers which has not already
87-
* been saved, this is very unlikely to happen.
88-
*/
89-
if (read_sysreg_s(SYS_SVCR) & (SVCR_SM_MASK | SVCR_ZA_MASK)) {
90-
*host_data_ptr(fp_owner) = FP_STATE_FREE;
91-
fpsimd_save_and_flush_cpu_state();
92-
}
9378
}
9479

9580
/*

0 commit comments

Comments
 (0)