]> www.infradead.org Git - users/dwmw2/linux.git/commitdiff
KVM: arm64: Drop FP_FOREIGN_STATE from the hypervisor code
authorMarc Zyngier <maz@kernel.org>
Sat, 28 May 2022 11:38:13 +0000 (12:38 +0100)
committerMarc Zyngier <maz@kernel.org>
Thu, 9 Jun 2022 11:01:51 +0000 (12:01 +0100)
The vcpu KVM_ARM64_FP_FOREIGN_FPSTATE flag tracks the thread's own
TIF_FOREIGN_FPSTATE so that we can evaluate just before running
the vcpu whether it the FP regs contain something that is owned
by the vcpu or not by updating the rest of the FP flags.

We do this in the hypervisor code in order to make sure we're
in a context where we are not interruptible. But we already
have a hook in the run loop to generate this flag. We may as
well update the FP flags directly and save the pointless flag
tracking.

Whilst we're at it, rename update_fp_enabled() to guest_owns_fp_regs()
to indicate what the leftover of this helper actually do.

Signed-off-by: Marc Zyngier <maz@kernel.org>
Reviewed-by: Reiji Watanabe <reijiw@google.com>
Reviewed-by: Mark Brown <broonie@kernel.org>
arch/arm64/include/asm/kvm_host.h
arch/arm64/kvm/fpsimd.c
arch/arm64/kvm/hyp/include/hyp/switch.h
arch/arm64/kvm/hyp/nvhe/switch.c
arch/arm64/kvm/hyp/vhe/switch.c

index 47a1e25e25bbc836e2c99cec064fd9dfd4ce7a09..63103cc1bdc4e54843a2ce676e99e0a2ca55fbaa 100644 (file)
@@ -462,7 +462,6 @@ struct kvm_vcpu_arch {
 
 #define KVM_ARM64_DEBUG_STATE_SAVE_SPE (1 << 12) /* Save SPE context if active  */
 #define KVM_ARM64_DEBUG_STATE_SAVE_TRBE        (1 << 13) /* Save TRBE context if active  */
-#define KVM_ARM64_FP_FOREIGN_FPSTATE   (1 << 14)
 #define KVM_ARM64_ON_UNSUPPORTED_CPU   (1 << 15) /* Physical CPU not in supported_cpus */
 #define KVM_ARM64_HOST_SME_ENABLED     (1 << 16) /* SME enabled for EL0 */
 #define KVM_ARM64_WFIT                 (1 << 17) /* WFIT instruction trapped */
index 6012b08ecb14e7f6464374cf780c9bf271af1cca..edbc0183c89b0e1adaaed1d4c16816e245de64f0 100644 (file)
@@ -107,16 +107,19 @@ void kvm_arch_vcpu_load_fp(struct kvm_vcpu *vcpu)
 }
 
 /*
- * Called just before entering the guest once we are no longer
- * preemptable. Syncs the host's TIF_FOREIGN_FPSTATE with the KVM
- * mirror of the flag used by the hypervisor.
+ * Called just before entering the guest once we are no longer preemptable
+ * and interrupts are disabled. If we have managed to run anything using
+ * FP while we were preemptible (such as off the back of an interrupt),
+ * then neither the host nor the guest own the FP hardware (and it was the
+ * responsibility of the code that used FP to save the existing state).
+ *
+ * Note that not supporting FP is basically the same thing as far as the
+ * hypervisor is concerned (nothing to save).
  */
 void kvm_arch_vcpu_ctxflush_fp(struct kvm_vcpu *vcpu)
 {
-       if (test_thread_flag(TIF_FOREIGN_FPSTATE))
-               vcpu->arch.flags |= KVM_ARM64_FP_FOREIGN_FPSTATE;
-       else
-               vcpu->arch.flags &= ~KVM_ARM64_FP_FOREIGN_FPSTATE;
+       if (!system_supports_fpsimd() || test_thread_flag(TIF_FOREIGN_FPSTATE))
+               vcpu->arch.flags &= ~(KVM_ARM64_FP_ENABLED | KVM_ARM64_FP_HOST);
 }
 
 /*
index 37d9f211c200c8c221137251f3147a3a96bb28c9..e543203849436a81d9995e3222878c53297e9446 100644 (file)
@@ -37,21 +37,9 @@ struct kvm_exception_table_entry {
 extern struct kvm_exception_table_entry __start___kvm_ex_table;
 extern struct kvm_exception_table_entry __stop___kvm_ex_table;
 
-/* Check whether the FP regs were dirtied while in the host-side run loop: */
-static inline bool update_fp_enabled(struct kvm_vcpu *vcpu)
+/* Check whether the FP regs are owned by the guest */
+static inline bool guest_owns_fp_regs(struct kvm_vcpu *vcpu)
 {
-       /*
-        * When the system doesn't support FP/SIMD, we cannot rely on
-        * the _TIF_FOREIGN_FPSTATE flag. However, we always inject an
-        * abort on the very first access to FP and thus we should never
-        * see KVM_ARM64_FP_ENABLED. For added safety, make sure we always
-        * trap the accesses.
-        */
-       if (!system_supports_fpsimd() ||
-           vcpu->arch.flags & KVM_ARM64_FP_FOREIGN_FPSTATE)
-               vcpu->arch.flags &= ~(KVM_ARM64_FP_ENABLED |
-                                     KVM_ARM64_FP_HOST);
-
        return !!(vcpu->arch.flags & KVM_ARM64_FP_ENABLED);
 }
 
index 6db801db8f271939ca7329f03ce300715e163dfd..a6b9f1186577a382ae6f9e873a6e46c5fc65f2e8 100644 (file)
@@ -43,7 +43,7 @@ static void __activate_traps(struct kvm_vcpu *vcpu)
 
        val = vcpu->arch.cptr_el2;
        val |= CPTR_EL2_TTA | CPTR_EL2_TAM;
-       if (!update_fp_enabled(vcpu)) {
+       if (!guest_owns_fp_regs(vcpu)) {
                val |= CPTR_EL2_TFP | CPTR_EL2_TZ;
                __activate_traps_fpsimd32(vcpu);
        }
index 969f20daf97aabb9b8dd67c7eec8e7272e1953d9..46f365254e9f12262b3d97bff20405836e6a5ab8 100644 (file)
@@ -55,7 +55,7 @@ static void __activate_traps(struct kvm_vcpu *vcpu)
 
        val |= CPTR_EL2_TAM;
 
-       if (update_fp_enabled(vcpu)) {
+       if (guest_owns_fp_regs(vcpu)) {
                if (vcpu_has_sve(vcpu))
                        val |= CPACR_EL1_ZEN_EL0EN | CPACR_EL1_ZEN_EL1EN;
        } else {