[v9,06/27] x86/fpu/xstate: Create guest fpstate with guest specific config

Message ID 20240124024200.102792-7-weijiang.yang@intel.com
State New
Headers
Series Enable CET Virtualization |

Commit Message

Yang, Weijiang Jan. 24, 2024, 2:41 a.m. UTC
  Use fpu_guest_cfg to calculate guest fpstate settings, open code for
__fpstate_reset() to avoid using kernel FPU config.

Below configuration steps are currently enforced to get guest fpstate:
1) Kernel sets up guest FPU settings in fpu__init_system_xstate().
2) User space sets vCPU thread group xstate permits via arch_prctl().
3) User space creates guest fpstate via __fpu_alloc_init_guest_fpstate()
   for vcpu thread.
4) User space enables guest dynamic xfeatures and re-allocate guest
   fpstate.

By adding kernel dynamic xfeatures in above #1 and #2, guest xstate area
size is expanded to hold (fpu_kernel_cfg.default_features | kernel dynamic
xfeatures | user dynamic xfeatures), then host xsaves/xrstors can operate
for all guest xfeatures.

The user_* fields remain unchanged for compatibility with KVM uAPIs.

Signed-off-by: Yang Weijiang <weijiang.yang@intel.com>
Reviewed-by: Maxim Levitsky <mlevitsk@redhat.com>
---
 arch/x86/kernel/fpu/core.c | 44 ++++++++++++++++++++++++++++++--------
 1 file changed, 35 insertions(+), 9 deletions(-)
  

Comments

Edgecombe, Rick P Jan. 30, 2024, 1:38 a.m. UTC | #1
On Tue, 2024-01-23 at 18:41 -0800, Yang Weijiang wrote:
> -bool fpu_alloc_guest_fpstate(struct fpu_guest *gfpu)
> +static struct fpstate *__fpu_alloc_init_guest_fpstate(struct
> fpu_guest *gfpu)
>  {
>         struct fpstate *fpstate;
>         unsigned int size;
>  
> -       size = fpu_user_cfg.default_size + ALIGN(offsetof(struct
> fpstate, regs), 64);
> +       /*
> +        * fpu_guest_cfg.default_size is initialized to hold all
> enabled
> +        * xfeatures except the user dynamic xfeatures. If the user
> dynamic
> +        * xfeatures are enabled, the guest fpstate will be re-
> allocated to
> +        * hold all guest enabled xfeatures, so omit user dynamic
> xfeatures
> +        * here.
> +        */
> +       size = fpu_guest_cfg.default_size +
> +              ALIGN(offsetof(struct fpstate, regs), 64);

Minor, I'm not sure the extra char warrants changing it to a wrapped
line, but that's just my personal opinion.

> +
>         fpstate = vzalloc(size);
>         if (!fpstate)
> -               return false;
> +               return NULL;
> +       /*
> +        * Initialize sizes and feature masks, use fpu_user_cfg.*
> +        * for user_* settings for compatibility of exiting uAPIs.
> +        */
> +       fpstate->size           = fpu_guest_cfg.default_size;
> +       fpstate->xfeatures      = fpu_guest_cfg.default_features;
> +       fpstate->user_size      = fpu_user_cfg.default_size;
> +       fpstate->user_xfeatures = fpu_user_cfg.default_features;
> +       fpstate->xfd            = 0;
>  
> -       /* Leave xfd to 0 (the reset value defined by spec) */
> -       __fpstate_reset(fpstate, 0);
>         fpstate_init_user(fpstate);
>         fpstate->is_valloc      = true;
>         fpstate->is_guest       = true;
>  
>         gfpu->fpstate           = fpstate;
> -       gfpu->xfeatures         = fpu_user_cfg.default_features;
> -       gfpu->perm              = fpu_user_cfg.default_features;
> +       gfpu->xfeatures         = fpu_guest_cfg.default_features;
> +       gfpu->perm              = fpu_guest_cfg.default_features;
> +
> +       return fpstate;
> +}
> +
> +bool fpu_alloc_guest_fpstate(struct fpu_guest *gfpu)
> +{
> +       struct fpstate *fpstate;
> +
> +       fpstate = __fpu_alloc_init_guest_fpstate(gfpu);

The above two statements could be just one line and still even fit
under 80 chars.

All the same,

Reviewed-by: Rick Edgecombe <rick.p.edgecombe@intel.com>

> +
> +       if (!fpstate)
> +               return false;
>
  
Yang, Weijiang Jan. 30, 2024, 2:54 p.m. UTC | #2
On 1/30/2024 9:38 AM, Edgecombe, Rick P wrote:
> On Tue, 2024-01-23 at 18:41 -0800, Yang Weijiang wrote:
>> -bool fpu_alloc_guest_fpstate(struct fpu_guest *gfpu)
>> +static struct fpstate *__fpu_alloc_init_guest_fpstate(struct
>> fpu_guest *gfpu)
>>   {
>>          struct fpstate *fpstate;
>>          unsigned int size;
>>   
>> -       size = fpu_user_cfg.default_size + ALIGN(offsetof(struct
>> fpstate, regs), 64);
>> +       /*
>> +        * fpu_guest_cfg.default_size is initialized to hold all
>> enabled
>> +        * xfeatures except the user dynamic xfeatures. If the user
>> dynamic
>> +        * xfeatures are enabled, the guest fpstate will be re-
>> allocated to
>> +        * hold all guest enabled xfeatures, so omit user dynamic
>> xfeatures
>> +        * here.
>> +        */
>> +       size = fpu_guest_cfg.default_size +
>> +              ALIGN(offsetof(struct fpstate, regs), 64);
> Minor, I'm not sure the extra char warrants changing it to a wrapped
> line, but that's just my personal opinion.
>
>> +
>>          fpstate = vzalloc(size);
>>          if (!fpstate)
>> -               return false;
>> +               return NULL;
>> +       /*
>> +        * Initialize sizes and feature masks, use fpu_user_cfg.*
>> +        * for user_* settings for compatibility of exiting uAPIs.
>> +        */
>> +       fpstate->size           = fpu_guest_cfg.default_size;
>> +       fpstate->xfeatures      = fpu_guest_cfg.default_features;
>> +       fpstate->user_size      = fpu_user_cfg.default_size;
>> +       fpstate->user_xfeatures = fpu_user_cfg.default_features;
>> +       fpstate->xfd            = 0;
>>   
>> -       /* Leave xfd to 0 (the reset value defined by spec) */
>> -       __fpstate_reset(fpstate, 0);
>>          fpstate_init_user(fpstate);
>>          fpstate->is_valloc      = true;
>>          fpstate->is_guest       = true;
>>   
>>          gfpu->fpstate           = fpstate;
>> -       gfpu->xfeatures         = fpu_user_cfg.default_features;
>> -       gfpu->perm              = fpu_user_cfg.default_features;
>> +       gfpu->xfeatures         = fpu_guest_cfg.default_features;
>> +       gfpu->perm              = fpu_guest_cfg.default_features;
>> +
>> +       return fpstate;
>> +}
>> +
>> +bool fpu_alloc_guest_fpstate(struct fpu_guest *gfpu)
>> +{
>> +       struct fpstate *fpstate;
>> +
>> +       fpstate = __fpu_alloc_init_guest_fpstate(gfpu);
> The above two statements could be just one line and still even fit
> under 80 chars.

Indeed, the variable is redundant, I'll remove it, thanks!

>
> All the same,
>
> Reviewed-by: Rick Edgecombe <rick.p.edgecombe@intel.com>
>
>> +
>> +       if (!fpstate)
>> +               return false;
>>
  

Patch

diff --git a/arch/x86/kernel/fpu/core.c b/arch/x86/kernel/fpu/core.c
index 516af626bf6a..6048352e1cc7 100644
--- a/arch/x86/kernel/fpu/core.c
+++ b/arch/x86/kernel/fpu/core.c
@@ -194,8 +194,6 @@  void fpu_reset_from_exception_fixup(void)
 }
 
 #if IS_ENABLED(CONFIG_KVM)
-static void __fpstate_reset(struct fpstate *fpstate, u64 xfd);
-
 static void fpu_init_guest_permissions(struct fpu_guest *gfpu)
 {
 	struct fpu_state_perm *fpuperm;
@@ -216,25 +214,53 @@  static void fpu_init_guest_permissions(struct fpu_guest *gfpu)
 	gfpu->perm = perm & ~FPU_GUEST_PERM_LOCKED;
 }
 
-bool fpu_alloc_guest_fpstate(struct fpu_guest *gfpu)
+static struct fpstate *__fpu_alloc_init_guest_fpstate(struct fpu_guest *gfpu)
 {
 	struct fpstate *fpstate;
 	unsigned int size;
 
-	size = fpu_user_cfg.default_size + ALIGN(offsetof(struct fpstate, regs), 64);
+	/*
+	 * fpu_guest_cfg.default_size is initialized to hold all enabled
+	 * xfeatures except the user dynamic xfeatures. If the user dynamic
+	 * xfeatures are enabled, the guest fpstate will be re-allocated to
+	 * hold all guest enabled xfeatures, so omit user dynamic xfeatures
+	 * here.
+	 */
+	size = fpu_guest_cfg.default_size +
+	       ALIGN(offsetof(struct fpstate, regs), 64);
+
 	fpstate = vzalloc(size);
 	if (!fpstate)
-		return false;
+		return NULL;
+	/*
+	 * Initialize sizes and feature masks, use fpu_user_cfg.*
+	 * for user_* settings for compatibility of exiting uAPIs.
+	 */
+	fpstate->size		= fpu_guest_cfg.default_size;
+	fpstate->xfeatures	= fpu_guest_cfg.default_features;
+	fpstate->user_size	= fpu_user_cfg.default_size;
+	fpstate->user_xfeatures	= fpu_user_cfg.default_features;
+	fpstate->xfd		= 0;
 
-	/* Leave xfd to 0 (the reset value defined by spec) */
-	__fpstate_reset(fpstate, 0);
 	fpstate_init_user(fpstate);
 	fpstate->is_valloc	= true;
 	fpstate->is_guest	= true;
 
 	gfpu->fpstate		= fpstate;
-	gfpu->xfeatures		= fpu_user_cfg.default_features;
-	gfpu->perm		= fpu_user_cfg.default_features;
+	gfpu->xfeatures		= fpu_guest_cfg.default_features;
+	gfpu->perm		= fpu_guest_cfg.default_features;
+
+	return fpstate;
+}
+
+bool fpu_alloc_guest_fpstate(struct fpu_guest *gfpu)
+{
+	struct fpstate *fpstate;
+
+	fpstate = __fpu_alloc_init_guest_fpstate(gfpu);
+
+	if (!fpstate)
+		return false;
 
 	/*
 	 * KVM sets the FP+SSE bits in the XSAVE header when copying FPU state