Skip to content

Commit 93ae6b0

Browse files
brooniewilldeacon
authored andcommitted
KVM: arm64: Discard any SVE state when entering KVM guests
Since 8383741 (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 <[email protected]> Reviewed-by: Catalin Marinas <[email protected]> Reviewed-by: Marc Zyngier <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Will Deacon <[email protected]>
1 parent f0c4d9f commit 93ae6b0

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

arch/arm64/include/asm/fpsimd.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ extern void fpsimd_signal_preserve_current_state(void);
5656
extern void fpsimd_preserve_current_state(void);
5757
extern void fpsimd_restore_current_state(void);
5858
extern void fpsimd_update_current_state(struct user_fpsimd_state const *state);
59+
extern void fpsimd_kvm_prepare(void);
5960

6061
extern void fpsimd_bind_state_to_cpu(struct user_fpsimd_state *state,
6162
void *sve_state, unsigned int sve_vl,

arch/arm64/kernel/fpsimd.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1627,6 +1627,29 @@ void fpsimd_signal_preserve_current_state(void)
16271627
sve_to_fpsimd(current);
16281628
}
16291629

1630+
/*
1631+
* Called by KVM when entering the guest.
1632+
*/
1633+
void fpsimd_kvm_prepare(void)
1634+
{
1635+
if (!system_supports_sve())
1636+
return;
1637+
1638+
/*
1639+
* KVM does not save host SVE state since we can only enter
1640+
* the guest from a syscall so the ABI means that only the
1641+
* non-saved SVE state needs to be saved. If we have left
1642+
* SVE enabled for performance reasons then update the task
1643+
* state to be FPSIMD only.
1644+
*/
1645+
get_cpu_fpsimd_context();
1646+
1647+
if (test_and_clear_thread_flag(TIF_SVE))
1648+
sve_to_fpsimd(current);
1649+
1650+
put_cpu_fpsimd_context();
1651+
}
1652+
16301653
/*
16311654
* Associate current's FPSIMD context with this cpu
16321655
* The caller must have ownership of the cpu FPSIMD context before calling

arch/arm64/kvm/fpsimd.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,12 @@ int kvm_arch_vcpu_run_map_fp(struct kvm_vcpu *vcpu)
7575
void kvm_arch_vcpu_load_fp(struct kvm_vcpu *vcpu)
7676
{
7777
BUG_ON(!current->mm);
78-
BUG_ON(test_thread_flag(TIF_SVE));
7978

8079
if (!system_supports_fpsimd())
8180
return;
8281

82+
fpsimd_kvm_prepare();
83+
8384
vcpu->arch.fp_state = FP_STATE_HOST_OWNED;
8485

8586
vcpu_clear_flag(vcpu, HOST_SVE_ENABLED);

0 commit comments

Comments
 (0)