x86/fpu/kvm: Sanitize the FPU guest/user handling
v2 cover x86/fpu/kvm: Sanitize the FPU guest/user handling
Currently KVM allocates two FPU structs which are used for saving the user state of the vCPU thread and restoring the guest state when entering vcpu_run() and doing the reverse operation before leaving vcpu_run().
With the new fpstate mechanism this can be reduced to one extra buffer by swapping the fpstate pointer in current::thread::fpu. This makes also the upcoming support for AMX and XFD simpler because then fpstate information (features, sizes, xfd) are always consistent and it does not require any nasty workarounds.
The following series cleans that up and replaces the current scheme with a single guest state which is switched in when entering vcpu_run() and switched out before leaving it.
The rework is valuable even without AMX/XFD because it consumes less memory and when swapping the fpstates there is no memory copy required when TIF_NEED_LOAD_FPU is set on the going out fpstate.
The series is based on:
git://git.kernel.org/pub/scm/linux/kernel/git/tglx/devel.git x86/fpu-3
and available from git:
git://git.kernel.org/pub/scm/linux/kernel/git/tglx/devel.git x86/fpu-3-kvm
V1 can be found here:
https://lore.kernel.org/r/[email protected]
Changes vs. V1:
Drop the restore_mask argument as the result is constant anyway - Paolo
Thanks,
tglx
--- include/asm/fpu/api.h | 19 ++++++-- include/asm/fpu/types.h | 44 ++++++++++++++++++- include/asm/kvm_host.h | 7 --- kernel/fpu/core.c | 111 ++++++++++++++++++++++++++++++++---------------- kvm/svm/svm.c | 7 +-- kvm/x86.c | 88 ++++++++++---------------------------- 6 files changed, 164 insertions(+), 112 deletions(-)
PATCH v2 1/4 x86/fpu: Prepare for sanitizing KVM FPU code
For the upcoming AMX support it's necessary to do a proper integration with KVM. To avoid more nasty hackery in KVM which violate encapsulation extend struct fpu and fpstate so the fpstate switching can be consolidated and simplified.
Currently KVM allocates two FPU structs which are used for saving the user state of the vCPU thread and restoring the guest state when entering vcpu_run() and doing the reverse operation before leaving vcpu_run().
With the new fpstate mechanism this can be reduced to one extra buffer by swapping the fpstate pointer in current::thread::fpu. This makes the upcoming support for AMX and XFD simpler because then fpstate information (features, sizes, xfd) are always consistent and it does not require any nasty workarounds.
Add fpu::__task_fpstate to save the regular fpstate pointer while the task is inside vcpu_run(). Add some state fields to fpstate to indicate the nature of the state.
--- arch/x86/include/asm/fpu/types.h | 44 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-)
---
arch/x86/include/asm/fpu/types.h +43 -1
@@ -322,8 +322,32 @@ struct fpstate {/* @user_xfeatures: xfeatures valid in UABI buffers */ u64 user_xfeatures; +/* @is_valloc: Indicator for dynamically allocated state */ + unsigned int is_valloc : 1; + +/* @is_guest: Indicator for guest state (KVM) */ + unsigned int is_guest : 1; + +/* +* @is_confidential: Indicator for KVM confidential mode. +* The FPU registers are restored by the +* vmentry firmware from encrypted guest +* memory. On vmexit the FPU registers are +* saved by firmware to encrypted guest memory +* and the registers are scrubbed before +* returning to the host. So there is no +* content which is worth saving and restoring +* The fpstate has to be there so that +* preemption and softirq FPU usage works. +* without special casing. +*/ + unsigned int is_confidential : 1; + +/* @in_use: State is in use */ + unsigned int in_use : 1; +/* @regs: The register state union for all supported formats */ -union fpregs_state regs; +union fpregs_state regs;/* @regs is dynamically sized! Don't add anything after @regs! */ } __attribute__ ((aligned (64)));
@@ -364,6 +388,14 @@ struct fpu {struct fpstate *fpstate;/* +* @__task_fpstate: +* +* Pointer to an inactive struct fpstate. Initialized to NULL. Is +* used only for KVM support to swap out the regular task fpstate. +*/ +struct fpstate *__task_fpstate; + +/* * @__fpstate: * * Initial in-memory storage for FPU registers which are saved in
@@ -379,6 +411,16 @@ struct fpu { };/* +* Guest pseudo FPU container +*/ +struct fpu_guest { +/* +* @fpstate: Pointer to the allocated guest fpstate +*/ +struct fpstate *fpstate; +}; + +/* * FPU state configuration data. Initialized at boot time. Read only after init. */ struct fpu_state_config {
[tip: x86/fpu] x86/fpu: Prepare for sanitizing KVM FPU code
The following commit has been merged into the x86/fpu branch of tip:
The following commit has been merged into the x86/fpu branch of tip:
Commit-ID: 75c52dad5e327605f1025f399dafdf4aaf5dae9c Gitweb: https://git.kernel.org/tip/75c52dad5e327605f1025f399dafdf4aaf5dae9c Author: Thomas Gleixner <[email protected]> AuthorDate: Fri, 22 Oct 2021 20:55:49 +02:00 Committer: Borislav Petkov <[email protected]> CommitterDate: Sat, 23 Oct 2021 13:14:50 +02:00
x86/fpu: Prepare for sanitizing KVM FPU code
For the upcoming AMX support it's necessary to do a proper integration with KVM. To avoid more nasty hackery in KVM which violate encapsulation extend struct fpu and fpstate so the fpstate switching can be consolidated and simplified.
Currently KVM allocates two FPU structs which are used for saving the user state of the vCPU thread and restoring the guest state when entering vcpu_run() and doing the reverse operation before leaving vcpu_run().
With the new fpstate mechanism this can be reduced to one extra buffer by swapping the fpstate pointer in current::thread::fpu. This makes the upcoming support for AMX and XFD simpler because then fpstate information (features, sizes, xfd) are always consistent and it does not require any nasty workarounds.
Add fpu::__task_fpstate to save the regular fpstate pointer while the task is inside vcpu_run(). Add some state fields to fpstate to indicate the nature of the state.
--- arch/x86/include/asm/fpu/types.h | 44 ++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-)
arch/x86/include/asm/fpu/types.h +43 -1
@@ -322,8 +322,32 @@ struct fpstate {/* @user_xfeatures: xfeatures valid in UABI buffers */ u64 user_xfeatures; +/* @is_valloc: Indicator for dynamically allocated state */ + unsigned int is_valloc : 1; + +/* @is_guest: Indicator for guest state (KVM) */ + unsigned int is_guest : 1; + +/* +* @is_confidential: Indicator for KVM confidential mode. +* The FPU registers are restored by the +* vmentry firmware from encrypted guest +* memory. On vmexit the FPU registers are +* saved by firmware to encrypted guest memory +* and the registers are scrubbed before +* returning to the host. So there is no +* content which is worth saving and restoring. +* The fpstate has to be there so that +* preemption and softirq FPU usage works +* without special casing. +*/ + unsigned int is_confidential : 1; + +/* @in_use: State is in use */ + unsigned int in_use : 1; +/* @regs: The register state union for all supported formats */ -union fpregs_state regs; +union fpregs_state regs;/* @regs is dynamically sized! Don't add anything after @regs! */ }__aligned(64) ;
@@ -364,6 +388,14 @@ struct fpu {struct fpstate *fpstate;/* +* @__task_fpstate: +* +* Pointer to an inactive struct fpstate. Initialized to NULL. Is +* used only for KVM support to swap out the regular task fpstate. +*/ +struct fpstate *__task_fpstate; + +/* * @__fpstate: * * Initial in-memory storage for FPU registers which are saved in
@@ -379,6 +411,16 @@ struct fpu { };/* +* Guest pseudo FPU container +*/ +struct fpu_guest { +/* +* @fpstate: Pointer to the allocated guest fpstate +*/ +struct fpstate *fpstate; +}; + +/* * FPU state configuration data. Initialized at boot time. Read only after init. */ struct fpu_state_config {
PATCH v2 2/4 x86/fpu: Provide infrastructure for KVM FPU cleanup
In reply to Thomas GleixnerFor the upcoming AMX support it's necessary to do a proper integration with KVM. Currently KVM allocates two FPU structs which are used for saving the user state of the vCPU thread and restoring the guest state when entering vcpu_run() and doing the reverse operation before leaving vcpu_run().
With the new fpstate mechanism this can be reduced to one extra buffer by swapping the fpstate pointer in current::thread::fpu. This makes the upcoming support for AMX and XFD simpler because then fpstate information (features, sizes, xfd) are always consistent and it does not require any nasty workarounds.
Provide:
- An allocator which initializes the state properly
- A replacement for the existing FPU swap mechanim
Aside of the reduced memory foot print, this also makes state switching more efficient when TIF_FPU_NEED_LOAD is set. It does not require a memcpy as the state is already correct in the to be swapped out fpstate.
The existing interfaces will be removed once KVM is converted over.
---
V2: Remove the restore_mask argument as the result is constant anyway - Paolo
--- arch/x86/include/asm/fpu/api.h | 13 ++++++ arch/x86/kernel/fpu/core.c | 85 ++++++++++++++++++++++++++++++++++++++--- 2 files changed, 92 insertions(+), 6 deletions(-)
---
arch/x86/include/asm/fpu/api.h +13 -0
@@ -135,9 +135,22 @@ extern void fpu_init_fpstate_user(structextern void fpstate_clear_xstate_component (struct fpstate *fps,unsigned int xfeature);/* KVM specific functions */ +extern bool fpu_alloc_guest_fpstate (struct fpu_guest *gfpu); +extern void fpu_free_guest_fpstate (struct fpu_guest *gfpu); +extern int fpu_swap_kvm_fpstate (struct fpu_guest *gfpu,bool enter_guest);extern void fpu_swap_kvm_fpu (struct fpu *save,struct fpu *rstor,u64 restore_mask);extern int fpu_copy_kvm_uabi_to_fpstate (struct fpu *fpu,const void *buf,u64 xcr0,u32 *pkru);extern void fpu_copy_fpstate_to_kvm_uabi (struct fpu *fpu,void *buf,unsigned int size,u32 pkru); +static inline void fpstate_set_confidential (struct fpu_guest *gfpu) +{ + gfpu->fpstate->is_confidential = true; +} + +static inline bool fpstate_is_confidential (struct fpu_guest *gfpu) +{ +return gfpu->fpstate->is_confidential; +} +#endif /* _ASM_X86_FPU_API_H */
arch/x86/kernel/fpu/core.c +79 -6
101 lines not on this page · raw message
[tip: x86/fpu] x86/fpu: Provide infrastructure for KVM FPU cleanup
The following commit has been merged into the x86/fpu branch of tip:
The following commit has been merged into the x86/fpu branch of tip:
Commit-ID: 69f6ed1d14c6bcf712f4bb22a231c15eeab401e7 Gitweb: https://git.kernel.org/tip/69f6ed1d14c6bcf712f4bb22a231c15eeab401e7 Author: Thomas Gleixner <[email protected]> AuthorDate: Fri, 22 Oct 2021 20:55:51 +02:00 Committer: Borislav Petkov <[email protected]> CommitterDate: Sat, 23 Oct 2021 14:50:19 +02:00
x86/fpu: Provide infrastructure for KVM FPU cleanup
For the upcoming AMX support it's necessary to do a proper integration with KVM. Currently KVM allocates two FPU structs which are used for saving the user state of the vCPU thread and restoring the guest state when entering vcpu_run() and doing the reverse operation before leaving vcpu_run().
With the new fpstate mechanism this can be reduced to one extra buffer by swapping the fpstate pointer in current::thread::fpu. This makes the upcoming support for AMX and XFD simpler because then fpstate information (features, sizes, xfd) are always consistent and it does not require any nasty workarounds.
Provide:
- An allocator which initializes the state properly
- A replacement for the existing FPU swap mechanim
Aside of the reduced memory footprint, this also makes state switching more efficient when TIF_FPU_NEED_LOAD is set. It does not require a memcpy as the state is already correct in the to be swapped out fpstate.
The existing interfaces will be removed once KVM is converted over.
--- arch/x86/include/asm/fpu/api.h | 13 +++++- arch/x86/kernel/fpu/core.c | 85 ++++++++++++++++++++++++++++++--- 2 files changed, 92 insertions(+), 6 deletions(-)
arch/x86/include/asm/fpu/api.h +13 -0
@@ -135,9 +135,22 @@ extern void fpu_init_fpstate_user(struct fpu *fpu);extern void fpstate_clear_xstate_component (struct fpstate *fps,unsigned int xfeature);/* KVM specific functions */ +extern bool fpu_alloc_guest_fpstate (struct fpu_guest *gfpu); +extern void fpu_free_guest_fpstate (struct fpu_guest *gfpu); +extern int fpu_swap_kvm_fpstate (struct fpu_guest *gfpu,bool enter_guest);extern void fpu_swap_kvm_fpu (struct fpu *save,struct fpu *rstor,u64 restore_mask);extern int fpu_copy_kvm_uabi_to_fpstate (struct fpu *fpu,const void *buf,u64 xcr0,u32 *pkru);extern void fpu_copy_fpstate_to_kvm_uabi (struct fpu *fpu,void *buf,unsigned int size,u32 pkru); +static inline void fpstate_set_confidential (struct fpu_guest *gfpu) +{ + gfpu->fpstate->is_confidential = true; +} + +static inline bool fpstate_is_confidential (struct fpu_guest *gfpu) +{ +return gfpu->fpstate->is_confidential; +} +#endif /* _ASM_X86_FPU_API_H */
arch/x86/kernel/fpu/core.c +79 -6
101 lines not on this page · raw message
[tip: x86/fpu] x86/fpu: Provide infrastructure for KVM FPU cleanup
In reply to Thomas GleixnerThe following commit has been merged into the x86/fpu branch of tip:
The following commit has been merged into the x86/fpu branch of tip:
Commit-ID: b35633854ccb5cb0129e1cd160d55112f94cbdce Gitweb: https://git.kernel.org/tip/b35633854ccb5cb0129e1cd160d55112f94cbdce Author: Thomas Gleixner <[email protected]> AuthorDate: Fri, 22 Oct 2021 20:55:51 +02:00 Committer: Borislav Petkov <[email protected]> CommitterDate: Mon, 25 Oct 2021 10:22:07 +02:00
x86/fpu: Provide infrastructure for KVM FPU cleanup
For the upcoming AMX support it's necessary to do a proper integration with KVM. Currently KVM allocates two FPU structs which are used for saving the user state of the vCPU thread and restoring the guest state when entering vcpu_run() and doing the reverse operation before leaving vcpu_run().
With the new fpstate mechanism this can be reduced to one extra buffer by swapping the fpstate pointer in current::thread::fpu. This makes the upcoming support for AMX and XFD simpler because then fpstate information (features, sizes, xfd) are always consistent and it does not require any nasty workarounds.
Provide:
- An allocator which initializes the state properly
- A replacement for the existing FPU swap mechanim
Aside of the reduced memory footprint, this also makes state switching more efficient when TIF_FPU_NEED_LOAD is set. It does not require a memcpy as the state is already correct in the to be swapped out fpstate.
The existing interfaces will be removed once KVM is converted over.
[ bp: Include vmalloc.h explicitly to prevent build failures due to
include files cleanups, courtesy of Stephen Rothwell:
https://lore.kernel.org/r/[email protected] ]
--- arch/x86/include/asm/fpu/api.h | 13 +++++- arch/x86/kernel/fpu/core.c | 86 ++++++++++++++++++++++++++++++--- 2 files changed, 93 insertions(+), 6 deletions(-)
arch/x86/include/asm/fpu/api.h +13 -0
@@ -135,9 +135,22 @@ extern void fpu_init_fpstate_user(struct fpu *fpu);extern void fpstate_clear_xstate_component (struct fpstate *fps,unsigned int xfeature);/* KVM specific functions */ +extern bool fpu_alloc_guest_fpstate (struct fpu_guest *gfpu); +extern void fpu_free_guest_fpstate (struct fpu_guest *gfpu); +extern int fpu_swap_kvm_fpstate (struct fpu_guest *gfpu,bool enter_guest);extern void fpu_swap_kvm_fpu (struct fpu *save,struct fpu *rstor,u64 restore_mask);extern int fpu_copy_kvm_uabi_to_fpstate (struct fpu *fpu,const void *buf,u64 xcr0,u32 *pkru);extern void fpu_copy_fpstate_to_kvm_uabi (struct fpu *fpu,void *buf,unsigned int size,u32 pkru); +static inline void fpstate_set_confidential (struct fpu_guest *gfpu) +{ + gfpu->fpstate->is_confidential = true; +} + +static inline bool fpstate_is_confidential (struct fpu_guest *gfpu) +{ +return gfpu->fpstate->is_confidential; +} +#endif /* _ASM_X86_FPU_API_H */
arch/x86/kernel/fpu/core.c +80 -6
108 lines not on this page · raw message
PATCH v2 3/4 x86/kvm: Convert FPU handling to a single swap buffer
In reply to Thomas GleixnerFor the upcoming AMX support it's necessary to do a proper integration with KVM. Currently KVM allocates two FPU structs which are used for saving the user state of the vCPU thread and restoring the guest state when entering vcpu_run() and doing the reverse operation before leaving vcpu_run().
With the new fpstate mechanism this can be reduced to one extra buffer by swapping the fpstate pointer in current::thread::fpu. This makes the upcoming support for AMX and XFD simpler because then fpstate information (features, sizes, xfd) are always consistent and it does not require any nasty workarounds.
Convert the KVM FPU code over to this new scheme.
---
V2: Remove the restore_mask argument - Paolo
--- arch/x86/include/asm/fpu/api.h | 4 - arch/x86/include/asm/kvm_host.h | 7 --- arch/x86/kernel/fpu/core.c | 16 +++---- arch/x86/kvm/svm/svm.c | 7 +-- arch/x86/kvm/x86.c | 88 ++++++++++------------------------------ 5 files changed, 40 insertions(+), 82 deletions(-)
---
arch/x86/include/asm/fpu/api.h +2 -2
@@ -140,8 +140,8 @@ extern void fpu_free_guest_fpstate(strucextern int fpu_swap_kvm_fpstate (struct fpu_guest *gfpu,bool enter_guest);extern void fpu_swap_kvm_fpu (struct fpu *save,struct fpu *rstor,u64 restore_mask); -extern int fpu_copy_kvm_uabi_to_fpstate (struct fpu *fpu,const void *buf,u64 xcr0,u32 *pkru); -extern void fpu_copy_fpstate_to_kvm_uabi (struct fpu *fpu,void *buf,unsigned int size,u32 pkru); +extern void fpu_copy_guest_fpstate_to_uabi (struct fpu_guest *gfpu,void *buf,unsigned int size,u32 pkru); +extern int fpu_copy_uabi_to_guest_fpstate (struct fpu_guest *gfpu,const void *buf,u64 xcr0,u32 *vpkru);static inline void fpstate_set_confidential (struct fpu_guest *gfpu) {
arch/x86/include/asm/kvm_host.h +2 -5
@@ -691,11 +691,10 @@ struct kvm_vcpu_arch {* * Note that while the PKRU state lives inside the fpu registers, * it is switched out separately at VMENTER and VMEXIT time. The -* "guest_fpu" state here contains the guest FPU context, with the +* "guest_fpstate" state here contains the guest FPU context, with the * host PRKU bits. */ -struct fpu *user_fpu; -struct fpu *guest_fpu; +struct fpu_guest guest_fpu;u64 xcr0;u64 guest_supported_xcr0;
@@ -1685,8 +1684,6 @@ void kvm_vcpu_deliver_sipi_vector(structint kvm_task_switch (struct kvm_vcpu *vcpu,u16 tss_selector,int idt_index,int reason,bool has_error_code,u32 error_code); -void kvm_free_guest_fpu (struct kvm_vcpu *vcpu); -void kvm_post_set_cr0 (struct kvm_vcpu *vcpu,unsigned long old_cr0,unsigned long cr0);void kvm_post_set_cr4 (struct kvm_vcpu *vcpu,unsigned long old_cr4,unsigned long cr4);int kvm_set_cr0 (struct kvm_vcpu *vcpu,unsigned long cr0);
arch/x86/kernel/fpu/core.c +8 -8
@@ -268,10 +268,10 @@ void fpu_swap_kvm_fpu(struct fpu *save, }EXPORT_SYMBOL_GPL (fpu_swap_kvm_fpu); -void fpu_copy_fpstate_to_kvm_uabi (struct fpu *fpu,void *buf, -unsigned int size,u32 pkru) +void fpu_copy_guest_fpstate_to_uabi (struct fpu_guest *gfpu,void *buf, +unsigned int size,u32 pkru) { -struct fpstate *kstate = fpu->fpstate; +struct fpstate *kstate = gfpu->fpstate;union fpregs_state *ustate = buf;struct membuf mb = { .p = buf, .left = size };
@@ -284,12 +284,12 @@ void fpu_copy_fpstate_to_kvm_uabi(struct ustate->xsave.header.xfeatures =XFEATURE_MASK_FPSSE ; } } -EXPORT_SYMBOL_GPL (fpu_copy_fpstate_to_kvm_uabi); +EXPORT_SYMBOL_GPL (fpu_copy_guest_fpstate_to_uabi); -int fpu_copy_kvm_uabi_to_fpstate (struct fpu *fpu,const void *buf,u64 xcr0, -u32 *vpkru) +int fpu_copy_uabi_to_guest_fpstate (struct fpu_guest *gfpu,const void *buf, +u64 xcr0,u32 *vpkru) { -struct fpstate *kstate = fpu->fpstate; +struct fpstate *kstate = gfpu->fpstate;const union fpregs_state *ustate = buf;struct pkru_state *xpkru;int ret;
@@ -320,7 +320,7 @@ int fpu_copy_kvm_uabi_to_fpstate(structxstate_init_xcomp_bv (&kstate->regs.xsave, fpu_user_cfg.max_features); return 0; } -EXPORT_SYMBOL_GPL (fpu_copy_kvm_uabi_to_fpstate); +EXPORT_SYMBOL_GPL (fpu_copy_uabi_to_guest_fpstate);#endif /* CONFIG_KVM */ void kernel_fpu_begin_mask (unsigned int kfpu_mask)
arch/x86/kvm/svm/svm.c +4 -3
@@ -36,6 +36,7 @@#include <asm/spec-ctrl.h> #include <asm/cpu_device_id.h> #include <asm/traps.h> +#include <asm/fpu/api.h> #include <asm/virtext.h> #include "trace.h"
@@ -1346,10 +1347,10 @@ static int svm_create_vcpu(struct kvm_vc/* * SEV-ES guests maintain an encrypted version of their FPU * state which is restored and saved on VMRUN and VMEXIT. -* Free the fpu structure to prevent KVM from attempting to -* access the FPU state. +* Mark vcpu->arch.guest_fpu->fpstate as scratch so it won't +* do xsave/xrstor on it. */ -kvm_free_guest_fpu (vcpu); +fpstate_set_confidential (&vcpu->arch.guest_fpu); } err =avic_init_vcpu (svm);
arch/x86/kvm/x86.c +24 -64
197 lines not on this page · raw message
[tip: x86/fpu] x86/kvm: Convert FPU handling to a single swap buffer
The following commit has been merged into the x86/fpu branch of tip:
The following commit has been merged into the x86/fpu branch of tip:
Commit-ID: d69c1382e1b73a0496a70872a035ca2b22d074e5 Gitweb: https://git.kernel.org/tip/d69c1382e1b73a0496a70872a035ca2b22d074e5 Author: Thomas Gleixner <[email protected]> AuthorDate: Fri, 22 Oct 2021 20:55:53 +02:00 Committer: Borislav Petkov <[email protected]> CommitterDate: Sat, 23 Oct 2021 16:13:29 +02:00
x86/kvm: Convert FPU handling to a single swap buffer
For the upcoming AMX support it's necessary to do a proper integration with KVM. Currently KVM allocates two FPU structs which are used for saving the user state of the vCPU thread and restoring the guest state when entering vcpu_run() and doing the reverse operation before leaving vcpu_run().
With the new fpstate mechanism this can be reduced to one extra buffer by swapping the fpstate pointer in current::thread::fpu. This makes the upcoming support for AMX and XFD simpler because then fpstate information (features, sizes, xfd) are always consistent and it does not require any nasty workarounds.
Convert the KVM FPU code over to this new scheme.
--- arch/x86/include/asm/fpu/api.h | 4 +- arch/x86/include/asm/kvm_host.h | 7 +--- arch/x86/kernel/fpu/core.c | 16 +++--- arch/x86/kvm/svm/svm.c | 7 +-- arch/x86/kvm/x86.c | 88 ++++++++------------------------ 5 files changed, 40 insertions(+), 82 deletions(-)
arch/x86/include/asm/fpu/api.h +2 -2
@@ -140,8 +140,8 @@ extern void fpu_free_guest_fpstate(struct fpu_guest *gfpu);extern int fpu_swap_kvm_fpstate (struct fpu_guest *gfpu,bool enter_guest);extern void fpu_swap_kvm_fpu (struct fpu *save,struct fpu *rstor,u64 restore_mask); -extern int fpu_copy_kvm_uabi_to_fpstate (struct fpu *fpu,const void *buf,u64 xcr0,u32 *pkru); -extern void fpu_copy_fpstate_to_kvm_uabi (struct fpu *fpu,void *buf,unsigned int size,u32 pkru); +extern void fpu_copy_guest_fpstate_to_uabi (struct fpu_guest *gfpu,void *buf,unsigned int size,u32 pkru); +extern int fpu_copy_uabi_to_guest_fpstate (struct fpu_guest *gfpu,const void *buf,u64 xcr0,u32 *vpkru);static inline void fpstate_set_confidential (struct fpu_guest *gfpu) {
arch/x86/include/asm/kvm_host.h +2 -5
@@ -691,11 +691,10 @@ struct kvm_vcpu_arch {* * Note that while the PKRU state lives inside the fpu registers, * it is switched out separately at VMENTER and VMEXIT time. The -* "guest_fpu" state here contains the guest FPU context, with the +* "guest_fpstate" state here contains the guest FPU context, with the * host PRKU bits. */ -struct fpu *user_fpu; -struct fpu *guest_fpu; +struct fpu_guest guest_fpu;u64 xcr0;u64 guest_supported_xcr0;
@@ -1685,8 +1684,6 @@ void kvm_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector);int kvm_task_switch (struct kvm_vcpu *vcpu,u16 tss_selector,int idt_index,int reason,bool has_error_code,u32 error_code); -void kvm_free_guest_fpu (struct kvm_vcpu *vcpu); -void kvm_post_set_cr0 (struct kvm_vcpu *vcpu,unsigned long old_cr0,unsigned long cr0);void kvm_post_set_cr4 (struct kvm_vcpu *vcpu,unsigned long old_cr4,unsigned long cr4);int kvm_set_cr0 (struct kvm_vcpu *vcpu,unsigned long cr0);
arch/x86/kernel/fpu/core.c +8 -8
@@ -268,10 +268,10 @@ void fpu_swap_kvm_fpu(struct fpu *save, struct fpu *rstor, u64 restore_mask) }EXPORT_SYMBOL_GPL (fpu_swap_kvm_fpu); -void fpu_copy_fpstate_to_kvm_uabi (struct fpu *fpu,void *buf, -unsigned int size,u32 pkru) +void fpu_copy_guest_fpstate_to_uabi (struct fpu_guest *gfpu,void *buf, +unsigned int size,u32 pkru) { -struct fpstate *kstate = fpu->fpstate; +struct fpstate *kstate = gfpu->fpstate;union fpregs_state *ustate = buf;struct membuf mb = { .p = buf, .left = size };
@@ -284,12 +284,12 @@ void fpu_copy_fpstate_to_kvm_uabi(struct fpu *fpu, void *buf, ustate->xsave.header.xfeatures =XFEATURE_MASK_FPSSE ; } } -EXPORT_SYMBOL_GPL (fpu_copy_fpstate_to_kvm_uabi); +EXPORT_SYMBOL_GPL (fpu_copy_guest_fpstate_to_uabi); -int fpu_copy_kvm_uabi_to_fpstate (struct fpu *fpu,const void *buf,u64 xcr0, -u32 *vpkru) +int fpu_copy_uabi_to_guest_fpstate (struct fpu_guest *gfpu,const void *buf, +u64 xcr0,u32 *vpkru) { -struct fpstate *kstate = fpu->fpstate; +struct fpstate *kstate = gfpu->fpstate;const union fpregs_state *ustate = buf;struct pkru_state *xpkru;int ret;
@@ -320,7 +320,7 @@ int fpu_copy_kvm_uabi_to_fpstate(struct fpu *fpu, const void *buf, u64 xcr0,xstate_init_xcomp_bv (&kstate->regs.xsave, kstate->xfeatures); return 0; } -EXPORT_SYMBOL_GPL (fpu_copy_kvm_uabi_to_fpstate); +EXPORT_SYMBOL_GPL (fpu_copy_uabi_to_guest_fpstate);#endif /* CONFIG_KVM */ void kernel_fpu_begin_mask (unsigned int kfpu_mask)
arch/x86/kvm/svm/svm.c +4 -3
@@ -36,6 +36,7 @@#include <asm/spec-ctrl.h> #include <asm/cpu_device_id.h> #include <asm/traps.h> +#include <asm/fpu/api.h> #include <asm/virtext.h> #include "trace.h"
@@ -1346,10 +1347,10 @@ static int svm_create_vcpu(struct kvm_vcpu *vcpu)/* * SEV-ES guests maintain an encrypted version of their FPU * state which is restored and saved on VMRUN and VMEXIT. -* Free the fpu structure to prevent KVM from attempting to -* access the FPU state. +* Mark vcpu->arch.guest_fpu->fpstate as scratch so it won't +* do xsave/xrstor on it. */ -kvm_free_guest_fpu (vcpu); +fpstate_set_confidential (&vcpu->arch.guest_fpu); } err =avic_init_vcpu (svm);
arch/x86/kvm/x86.c +24 -64
197 lines not on this page · raw message
[tip: x86/fpu] x86/kvm: Convert FPU handling to a single swap buffer
In reply to Thomas GleixnerThe following commit has been merged into the x86/fpu branch of tip:
The following commit has been merged into the x86/fpu branch of tip:
Commit-ID: 61fb3a87598361283d96913bb1f0f3d0fe1310ef Gitweb: https://git.kernel.org/tip/61fb3a87598361283d96913bb1f0f3d0fe1310ef Author: Thomas Gleixner <[email protected]> AuthorDate: Fri, 22 Oct 2021 20:55:53 +02:00 Committer: Borislav Petkov <[email protected]> CommitterDate: Mon, 25 Oct 2021 10:24:16 +02:00
x86/kvm: Convert FPU handling to a single swap buffer
For the upcoming AMX support it's necessary to do a proper integration with KVM. Currently KVM allocates two FPU structs which are used for saving the user state of the vCPU thread and restoring the guest state when entering vcpu_run() and doing the reverse operation before leaving vcpu_run().
With the new fpstate mechanism this can be reduced to one extra buffer by swapping the fpstate pointer in current::thread::fpu. This makes the upcoming support for AMX and XFD simpler because then fpstate information (features, sizes, xfd) are always consistent and it does not require any nasty workarounds.
Convert the KVM FPU code over to this new scheme.
--- arch/x86/include/asm/fpu/api.h | 4 +- arch/x86/include/asm/kvm_host.h | 7 +--- arch/x86/kernel/fpu/core.c | 16 +++--- arch/x86/kvm/svm/svm.c | 7 +-- arch/x86/kvm/x86.c | 88 ++++++++------------------------ 5 files changed, 40 insertions(+), 82 deletions(-)
arch/x86/include/asm/fpu/api.h +2 -2
@@ -140,8 +140,8 @@ extern void fpu_free_guest_fpstate(struct fpu_guest *gfpu);extern int fpu_swap_kvm_fpstate (struct fpu_guest *gfpu,bool enter_guest);extern void fpu_swap_kvm_fpu (struct fpu *save,struct fpu *rstor,u64 restore_mask); -extern int fpu_copy_kvm_uabi_to_fpstate (struct fpu *fpu,const void *buf,u64 xcr0,u32 *pkru); -extern void fpu_copy_fpstate_to_kvm_uabi (struct fpu *fpu,void *buf,unsigned int size,u32 pkru); +extern void fpu_copy_guest_fpstate_to_uabi (struct fpu_guest *gfpu,void *buf,unsigned int size,u32 pkru); +extern int fpu_copy_uabi_to_guest_fpstate (struct fpu_guest *gfpu,const void *buf,u64 xcr0,u32 *vpkru);static inline void fpstate_set_confidential (struct fpu_guest *gfpu) {
arch/x86/include/asm/kvm_host.h +2 -5
@@ -691,11 +691,10 @@ struct kvm_vcpu_arch {* * Note that while the PKRU state lives inside the fpu registers, * it is switched out separately at VMENTER and VMEXIT time. The -* "guest_fpu" state here contains the guest FPU context, with the +* "guest_fpstate" state here contains the guest FPU context, with the * host PRKU bits. */ -struct fpu *user_fpu; -struct fpu *guest_fpu; +struct fpu_guest guest_fpu;u64 xcr0;u64 guest_supported_xcr0;
@@ -1685,8 +1684,6 @@ void kvm_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector);int kvm_task_switch (struct kvm_vcpu *vcpu,u16 tss_selector,int idt_index,int reason,bool has_error_code,u32 error_code); -void kvm_free_guest_fpu (struct kvm_vcpu *vcpu); -void kvm_post_set_cr0 (struct kvm_vcpu *vcpu,unsigned long old_cr0,unsigned long cr0);void kvm_post_set_cr4 (struct kvm_vcpu *vcpu,unsigned long old_cr4,unsigned long cr4);int kvm_set_cr0 (struct kvm_vcpu *vcpu,unsigned long cr0);
arch/x86/kernel/fpu/core.c +8 -8
@@ -269,10 +269,10 @@ void fpu_swap_kvm_fpu(struct fpu *save, struct fpu *rstor, u64 restore_mask) }EXPORT_SYMBOL_GPL (fpu_swap_kvm_fpu); -void fpu_copy_fpstate_to_kvm_uabi (struct fpu *fpu,void *buf, -unsigned int size,u32 pkru) +void fpu_copy_guest_fpstate_to_uabi (struct fpu_guest *gfpu,void *buf, +unsigned int size,u32 pkru) { -struct fpstate *kstate = fpu->fpstate; +struct fpstate *kstate = gfpu->fpstate;union fpregs_state *ustate = buf;struct membuf mb = { .p = buf, .left = size };
@@ -285,12 +285,12 @@ void fpu_copy_fpstate_to_kvm_uabi(struct fpu *fpu, void *buf, ustate->xsave.header.xfeatures =XFEATURE_MASK_FPSSE ; } } -EXPORT_SYMBOL_GPL (fpu_copy_fpstate_to_kvm_uabi); +EXPORT_SYMBOL_GPL (fpu_copy_guest_fpstate_to_uabi); -int fpu_copy_kvm_uabi_to_fpstate (struct fpu *fpu,const void *buf,u64 xcr0, -u32 *vpkru) +int fpu_copy_uabi_to_guest_fpstate (struct fpu_guest *gfpu,const void *buf, +u64 xcr0,u32 *vpkru) { -struct fpstate *kstate = fpu->fpstate; +struct fpstate *kstate = gfpu->fpstate;const union fpregs_state *ustate = buf;struct pkru_state *xpkru;int ret;
@@ -321,7 +321,7 @@ int fpu_copy_kvm_uabi_to_fpstate(struct fpu *fpu, const void *buf, u64 xcr0,xstate_init_xcomp_bv (&kstate->regs.xsave, kstate->xfeatures); return 0; } -EXPORT_SYMBOL_GPL (fpu_copy_kvm_uabi_to_fpstate); +EXPORT_SYMBOL_GPL (fpu_copy_uabi_to_guest_fpstate);#endif /* CONFIG_KVM */ void kernel_fpu_begin_mask (unsigned int kfpu_mask)
arch/x86/kvm/svm/svm.c +4 -3
@@ -36,6 +36,7 @@#include <asm/spec-ctrl.h> #include <asm/cpu_device_id.h> #include <asm/traps.h> +#include <asm/fpu/api.h> #include <asm/virtext.h> #include "trace.h"
@@ -1346,10 +1347,10 @@ static int svm_create_vcpu(struct kvm_vcpu *vcpu)/* * SEV-ES guests maintain an encrypted version of their FPU * state which is restored and saved on VMRUN and VMEXIT. -* Free the fpu structure to prevent KVM from attempting to -* access the FPU state. +* Mark vcpu->arch.guest_fpu->fpstate as scratch so it won't +* do xsave/xrstor on it. */ -kvm_free_guest_fpu (vcpu); +fpstate_set_confidential (&vcpu->arch.guest_fpu); } err =avic_init_vcpu (svm);
arch/x86/kvm/x86.c +24 -64
197 lines not on this page · raw message
PATCH v2 4/4 x86/fpu: Remove old KVM FPU interface
In reply to Thomas GleixnerNo more users.
--- arch/x86/include/asm/fpu/api.h | 2 -- arch/x86/kernel/fpu/core.c | 32 -------------------------------- 2 files changed, 34 deletions(-)
---
arch/x86/include/asm/fpu/api.h +0 -2
@@ -131,14 +131,12 @@ static inline void fpstate_init_soft(str DECLARE_PER_CPU(struct fpu *, fpu_fpregs_owner_ctx);/* fpstate-related functions which are exported to KVM */ -extern void fpu_init_fpstate_user (struct fpu *fpu);extern void fpstate_clear_xstate_component (struct fpstate *fps,unsigned int xfeature);/* KVM specific functions */ extern bool fpu_alloc_guest_fpstate (struct fpu_guest *gfpu);extern void fpu_free_guest_fpstate (struct fpu_guest *gfpu);extern int fpu_swap_kvm_fpstate (struct fpu_guest *gfpu,bool enter_guest); -extern void fpu_swap_kvm_fpu (struct fpu *save,struct fpu *rstor,u64 restore_mask);extern void fpu_copy_guest_fpstate_to_uabi (struct fpu_guest *gfpu,void *buf,unsigned int size,u32 pkru);extern int fpu_copy_uabi_to_guest_fpstate (struct fpu_guest *gfpu,const void *buf,u64 xcr0,u32 *vpkru);
arch/x86/kernel/fpu/core.c +0 -32
@@ -245,29 +245,6 @@ int fpu_swap_kvm_fpstate(struct fpu_gues }EXPORT_SYMBOL_GPL (fpu_swap_kvm_fpstate); -void fpu_swap_kvm_fpu (struct fpu *save,struct fpu *rstor,u64 restore_mask) -{ -fpregs_lock (); - -if (save) { -struct fpstate *fpcur = current->thread.fpu.fpstate; - -if (test_thread_flag (TIF_NEED_FPU_LOAD )) -memcpy (&save->fpstate->regs, &fpcur->regs, fpcur->size); -else -save_fpregs_to_fpstate (save); - } - -if (rstor) { - restore_mask &=XFEATURE_MASK_FPSTATE ; -restore_fpregs_from_fpstate (rstor->fpstate, restore_mask); - } - -fpregs_mark_activate (); -fpregs_unlock (); -} -EXPORT_SYMBOL_GPL (fpu_swap_kvm_fpu); -void fpu_copy_guest_fpstate_to_uabi (struct fpu_guest *gfpu,void *buf,unsigned int size,u32 pkru) {
@@ -437,15 +414,6 @@ void fpstate_reset(struct fpu *fpu)__fpstate_reset (fpu->fpstate); } -#if IS_ENABLED (CONFIG_KVM ) -void fpu_init_fpstate_user (struct fpu *fpu) -{ -fpstate_reset (fpu); -fpstate_init_user (fpu->fpstate); -} -EXPORT_SYMBOL_GPL (fpu_init_fpstate_user); -#endif -/* Clone current's FPU state on fork */ int fpu_clone (struct task_struct *dst) {
[tip: x86/fpu] x86/fpu: Remove old KVM FPU interface
The following commit has been merged into the x86/fpu branch of tip:
The following commit has been merged into the x86/fpu branch of tip:
Commit-ID: 582b01b6ab2714a0a4d554cea7f0d4efeaa2154d Gitweb: https://git.kernel.org/tip/582b01b6ab2714a0a4d554cea7f0d4efeaa2154d Author: Thomas Gleixner <[email protected]> AuthorDate: Fri, 22 Oct 2021 20:55:54 +02:00 Committer: Borislav Petkov <[email protected]> CommitterDate: Sat, 23 Oct 2021 17:05:19 +02:00
x86/fpu: Remove old KVM FPU interface
No more users.
--- arch/x86/include/asm/fpu/api.h | 2 -- arch/x86/kernel/fpu/core.c | 32 -------------------------------- 2 files changed, 34 deletions(-)
arch/x86/include/asm/fpu/api.h +0 -2
@@ -131,14 +131,12 @@ static inline void fpstate_init_soft(struct swregs_state *soft) {} DECLARE_PER_CPU(struct fpu *, fpu_fpregs_owner_ctx);/* fpstate-related functions which are exported to KVM */ -extern void fpu_init_fpstate_user (struct fpu *fpu);extern void fpstate_clear_xstate_component (struct fpstate *fps,unsigned int xfeature);/* KVM specific functions */ extern bool fpu_alloc_guest_fpstate (struct fpu_guest *gfpu);extern void fpu_free_guest_fpstate (struct fpu_guest *gfpu);extern int fpu_swap_kvm_fpstate (struct fpu_guest *gfpu,bool enter_guest); -extern void fpu_swap_kvm_fpu (struct fpu *save,struct fpu *rstor,u64 restore_mask);extern void fpu_copy_guest_fpstate_to_uabi (struct fpu_guest *gfpu,void *buf,unsigned int size,u32 pkru);extern int fpu_copy_uabi_to_guest_fpstate (struct fpu_guest *gfpu,const void *buf,u64 xcr0,u32 *vpkru);
arch/x86/kernel/fpu/core.c +0 -32
@@ -245,29 +245,6 @@ int fpu_swap_kvm_fpstate(struct fpu_guest *guest_fpu, bool enter_guest) }EXPORT_SYMBOL_GPL (fpu_swap_kvm_fpstate); -void fpu_swap_kvm_fpu (struct fpu *save,struct fpu *rstor,u64 restore_mask) -{ -fpregs_lock (); - -if (save) { -struct fpstate *fpcur = current->thread.fpu.fpstate; - -if (test_thread_flag (TIF_NEED_FPU_LOAD )) -memcpy (&save->fpstate->regs, &fpcur->regs, fpcur->size); -else -save_fpregs_to_fpstate (save); - } - -if (rstor) { - restore_mask &=XFEATURE_MASK_FPSTATE ; -restore_fpregs_from_fpstate (rstor->fpstate, restore_mask); - } - -fpregs_mark_activate (); -fpregs_unlock (); -} -EXPORT_SYMBOL_GPL (fpu_swap_kvm_fpu); -void fpu_copy_guest_fpstate_to_uabi (struct fpu_guest *gfpu,void *buf,unsigned int size,u32 pkru) {
@@ -437,15 +414,6 @@ void fpstate_reset(struct fpu *fpu)__fpstate_reset (fpu->fpstate); } -#if IS_ENABLED (CONFIG_KVM ) -void fpu_init_fpstate_user (struct fpu *fpu) -{ -fpstate_reset (fpu); -fpstate_init_user (fpu->fpstate); -} -EXPORT_SYMBOL_GPL (fpu_init_fpstate_user); -#endif -/* Clone current's FPU state on fork */ int fpu_clone (struct task_struct *dst) {
[tip: x86/fpu] x86/fpu: Remove old KVM FPU interface
In reply to Thomas GleixnerThe following commit has been merged into the x86/fpu branch of tip:
The following commit has been merged into the x86/fpu branch of tip:
Commit-ID: c341f1fe1543dfaf94916cef298aa60be545235f Gitweb: https://git.kernel.org/tip/c341f1fe1543dfaf94916cef298aa60be545235f Author: Thomas Gleixner <[email protected]> AuthorDate: Fri, 22 Oct 2021 20:55:54 +02:00 Committer: Borislav Petkov <[email protected]> CommitterDate: Mon, 25 Oct 2021 10:24:16 +02:00
x86/fpu: Remove old KVM FPU interface
No more users.
--- arch/x86/include/asm/fpu/api.h | 2 -- arch/x86/kernel/fpu/core.c | 32 -------------------------------- 2 files changed, 34 deletions(-)
arch/x86/include/asm/fpu/api.h +0 -2
@@ -131,14 +131,12 @@ static inline void fpstate_init_soft(struct swregs_state *soft) {} DECLARE_PER_CPU(struct fpu *, fpu_fpregs_owner_ctx);/* fpstate-related functions which are exported to KVM */ -extern void fpu_init_fpstate_user (struct fpu *fpu);extern void fpstate_clear_xstate_component (struct fpstate *fps,unsigned int xfeature);/* KVM specific functions */ extern bool fpu_alloc_guest_fpstate (struct fpu_guest *gfpu);extern void fpu_free_guest_fpstate (struct fpu_guest *gfpu);extern int fpu_swap_kvm_fpstate (struct fpu_guest *gfpu,bool enter_guest); -extern void fpu_swap_kvm_fpu (struct fpu *save,struct fpu *rstor,u64 restore_mask);extern void fpu_copy_guest_fpstate_to_uabi (struct fpu_guest *gfpu,void *buf,unsigned int size,u32 pkru);extern int fpu_copy_uabi_to_guest_fpstate (struct fpu_guest *gfpu,const void *buf,u64 xcr0,u32 *vpkru);
arch/x86/kernel/fpu/core.c +0 -32
@@ -246,29 +246,6 @@ int fpu_swap_kvm_fpstate(struct fpu_guest *guest_fpu, bool enter_guest) }EXPORT_SYMBOL_GPL (fpu_swap_kvm_fpstate); -void fpu_swap_kvm_fpu (struct fpu *save,struct fpu *rstor,u64 restore_mask) -{ -fpregs_lock (); - -if (save) { -struct fpstate *fpcur = current->thread.fpu.fpstate; - -if (test_thread_flag (TIF_NEED_FPU_LOAD )) -memcpy (&save->fpstate->regs, &fpcur->regs, fpcur->size); -else -save_fpregs_to_fpstate (save); - } - -if (rstor) { - restore_mask &=XFEATURE_MASK_FPSTATE ; -restore_fpregs_from_fpstate (rstor->fpstate, restore_mask); - } - -fpregs_mark_activate (); -fpregs_unlock (); -} -EXPORT_SYMBOL_GPL (fpu_swap_kvm_fpu); -void fpu_copy_guest_fpstate_to_uabi (struct fpu_guest *gfpu,void *buf,unsigned int size,u32 pkru) {
@@ -438,15 +415,6 @@ void fpstate_reset(struct fpu *fpu)__fpstate_reset (fpu->fpstate); } -#if IS_ENABLED (CONFIG_KVM ) -void fpu_init_fpstate_user (struct fpu *fpu) -{ -fpstate_reset (fpu); -fpstate_init_user (fpu->fpstate); -} -EXPORT_SYMBOL_GPL (fpu_init_fpstate_user); -#endif -/* Clone current's FPU state on fork */ int fpu_clone (struct task_struct *dst) {