KVM: arm64: Discard any SVE state when entering KVM guests
authorMark Brown <broonie@kernel.org>
Tue, 15 Nov 2022 09:46:33 +0000 (09:46 +0000)
committerWill Deacon <will@kernel.org>
Tue, 29 Nov 2022 15:01:56 +0000 (15:01 +0000)
Since 8383741ab2e773a99 (KVM: arm64: Get rid of host SVE tracking/saving)
KVM has not tracked the host SVE state, relying on the fact that we
currently disable SVE whenever we perform a syscall. This may not be true
in future since performance optimisation may result in us keeping SVE
enabled in order to avoid needing to take access traps to reenable it.
Handle this by clearing TIF_SVE and converting the stored task state to
FPSIMD format when preparing to run the guest.  This is done with a new
call fpsimd_kvm_prepare() to keep the direct state manipulation
functions internal to fpsimd.c.

Signed-off-by: Mark Brown <broonie@kernel.org>
Reviewed-by: Catalin Marinas <catalin.marinas@arm.com>
Reviewed-by: Marc Zyngier <maz@kernel.org>
Link: https://lore.kernel.org/r/20221115094640.112848-2-broonie@kernel.org
Signed-off-by: Will Deacon <will@kernel.org>
arch/arm64/include/asm/fpsimd.h
arch/arm64/kernel/fpsimd.c
arch/arm64/kvm/fpsimd.c

index 6f86b7ab6c28f00d38573336c9e0425df75d80c1..c07e4abaca3d61a7ee8231890f3a9c5518e2db5e 100644 (file)
@@ -56,6 +56,7 @@ extern void fpsimd_signal_preserve_current_state(void);
 extern void fpsimd_preserve_current_state(void);
 extern void fpsimd_restore_current_state(void);
 extern void fpsimd_update_current_state(struct user_fpsimd_state const *state);
+extern void fpsimd_kvm_prepare(void);
 
 extern void fpsimd_bind_state_to_cpu(struct user_fpsimd_state *state,
                                     void *sve_state, unsigned int sve_vl,
index 23834d96d1e781d16e34e760f5a7e72af7422525..549e11645e0ffe0c983de0addfcd469992bd0556 100644 (file)
@@ -1627,6 +1627,29 @@ void fpsimd_signal_preserve_current_state(void)
                sve_to_fpsimd(current);
 }
 
+/*
+ * Called by KVM when entering the guest.
+ */
+void fpsimd_kvm_prepare(void)
+{
+       if (!system_supports_sve())
+               return;
+
+       /*
+        * KVM does not save host SVE state since we can only enter
+        * the guest from a syscall so the ABI means that only the
+        * non-saved SVE state needs to be saved.  If we have left
+        * SVE enabled for performance reasons then update the task
+        * state to be FPSIMD only.
+        */
+       get_cpu_fpsimd_context();
+
+       if (test_and_clear_thread_flag(TIF_SVE))
+               sve_to_fpsimd(current);
+
+       put_cpu_fpsimd_context();
+}
+
 /*
  * Associate current's FPSIMD context with this cpu
  * The caller must have ownership of the cpu FPSIMD context before calling
index ec8e4494873d412382a795691220fe55d229858e..51ca78b31b95241bb8186a473d1bf5ccd50a16f0 100644 (file)
@@ -75,11 +75,12 @@ int kvm_arch_vcpu_run_map_fp(struct kvm_vcpu *vcpu)
 void kvm_arch_vcpu_load_fp(struct kvm_vcpu *vcpu)
 {
        BUG_ON(!current->mm);
-       BUG_ON(test_thread_flag(TIF_SVE));
 
        if (!system_supports_fpsimd())
                return;
 
+       fpsimd_kvm_prepare();
+
        vcpu->arch.fp_state = FP_STATE_HOST_OWNED;
 
        vcpu_clear_flag(vcpu, HOST_SVE_ENABLED);