KVM: arm64: Clarify rationale for ZCR_EL1 value restored on guest exit
authorFuad Tabba <tabba@google.com>
Tue, 23 Apr 2024 15:05:28 +0000 (16:05 +0100)
committerMarc Zyngier <maz@kernel.org>
Wed, 1 May 2024 15:48:14 +0000 (16:48 +0100)
Expand comment clarifying why the host value representing SVE
vector length being restored for ZCR_EL1 on guest exit isn't the
same as it was on guest entry.

Signed-off-by: Fuad Tabba <tabba@google.com>
Reviewed-by: Mark Brown <broonie@kernel.org>
Acked-by: Oliver Upton <oliver.upton@linux.dev>
Link: https://lore.kernel.org/r/20240423150538.2103045-21-tabba@google.com
Signed-off-by: Marc Zyngier <maz@kernel.org>
arch/arm64/kvm/fpsimd.c

index 63a6f82934a68d8bb393d0f74b649b31a846abb3..1807d3a79a8af8dbc22a5c34f4e289d3359b90f2 100644 (file)
@@ -175,12 +175,34 @@ void kvm_arch_vcpu_put_fp(struct kvm_vcpu *vcpu)
                if (vcpu_has_sve(vcpu)) {
                        __vcpu_sys_reg(vcpu, ZCR_EL1) = read_sysreg_el1(SYS_ZCR);
 
-                       /* Restore the VL that was saved when bound to the CPU */
+                       /*
+                        * Restore the VL that was saved when bound to the CPU,
+                        * which is the maximum VL for the guest. Because the
+                        * layout of the data when saving the sve state depends
+                        * on the VL, we need to use a consistent (i.e., the
+                        * maximum) VL.
+                        * Note that this means that at guest exit ZCR_EL1 is
+                        * not necessarily the same as on guest entry.
+                        *
+                        * Restoring the VL isn't needed in VHE mode since
+                        * ZCR_EL2 (accessed via ZCR_EL1) would fulfill the same
+                        * role when doing the save from EL2.
+                        */
                        if (!has_vhe())
                                sve_cond_update_zcr_vq(vcpu_sve_max_vq(vcpu) - 1,
                                                       SYS_ZCR_EL1);
                }
 
+               /*
+                * Flush (save and invalidate) the fpsimd/sve state so that if
+                * the host tries to use fpsimd/sve, it's not using stale data
+                * from the guest.
+                *
+                * Flushing the state sets the TIF_FOREIGN_FPSTATE bit for the
+                * context unconditionally, in both nVHE and VHE. This allows
+                * the kernel to restore the fpsimd/sve state, including ZCR_EL1
+                * when needed.
+                */
                fpsimd_save_and_flush_cpu_state();
        } else if (has_vhe() && system_supports_sve()) {
                /*