x86/fpu: Dont restore PKRU in fpregs_restore_userspace()
authorThomas Gleixner <tglx@linutronix.de>
Wed, 23 Jun 2021 12:02:17 +0000 (14:02 +0200)
committerBorislav Petkov <bp@suse.de>
Wed, 23 Jun 2021 17:33:32 +0000 (19:33 +0200)
switch_to() and flush_thread() write the task's PKRU value eagerly so
the PKRU value of current is always valid in the hardware.

That means there is no point in restoring PKRU on exit to user or when
reactivating the task's FPU registers in the signal frame setup path.

This allows to remove all the xstate buffer updates with PKRU values once
the PKRU state is stored in thread struct while a task is scheduled out.

Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Borislav Petkov <bp@suse.de>
Reviewed-by: Borislav Petkov <bp@suse.de>
Link: https://lkml.kernel.org/r/20210623121456.303919033@linutronix.de
arch/x86/include/asm/fpu/internal.h
arch/x86/include/asm/fpu/xstate.h
arch/x86/kernel/fpu/core.c

index bcfb6365a76bd9ddc0eed05f8c487037bbc8fe33..521774320e6aa3847c10ebe4c524893899502fa7 100644 (file)
@@ -457,7 +457,21 @@ static inline void fpregs_restore_userregs(void)
                return;
 
        if (!fpregs_state_valid(fpu, cpu)) {
-               restore_fpregs_from_fpstate(&fpu->state);
+               u64 mask;
+
+               /*
+                * This restores _all_ xstate which has not been
+                * established yet.
+                *
+                * If PKRU is enabled, then the PKRU value is already
+                * correct because it was either set in switch_to() or in
+                * flush_thread(). So it is excluded because it might be
+                * not up to date in current->thread.fpu.xsave state.
+                */
+               mask = xfeatures_mask_restore_user() |
+                       xfeatures_mask_supervisor();
+               __restore_fpregs_from_fpstate(&fpu->state, mask);
+
                fpregs_activate(fpu);
                fpu->last_cpu = cpu;
        }
index af9ea134bf5e6af5e3a5019d816d07ac74360211..6a0aaafb93eca1a40d87d46901c39cb982c37a12 100644 (file)
                                      XFEATURE_MASK_BNDREGS | \
                                      XFEATURE_MASK_BNDCSR)
 
+/*
+ * Features which are restored when returning to user space.
+ * PKRU is not restored on return to user space because PKRU
+ * is switched eagerly in switch_to() and flush_thread()
+ */
+#define XFEATURE_MASK_USER_RESTORE     \
+       (XFEATURE_MASK_USER_SUPPORTED & ~XFEATURE_MASK_PKRU)
+
 /* All currently supported supervisor features */
 #define XFEATURE_MASK_SUPERVISOR_SUPPORTED (XFEATURE_MASK_PASID)
 
@@ -92,6 +100,17 @@ static inline u64 xfeatures_mask_uabi(void)
        return xfeatures_mask_all & XFEATURE_MASK_USER_SUPPORTED;
 }
 
+/*
+ * The xfeatures which are restored by the kernel when returning to user
+ * mode. This is not necessarily the same as xfeatures_mask_uabi() as the
+ * kernel does not manage all XCR0 enabled features via xsave/xrstor as
+ * some of them have to be switched eagerly on context switch and exec().
+ */
+static inline u64 xfeatures_mask_restore_user(void)
+{
+       return xfeatures_mask_all & XFEATURE_MASK_USER_RESTORE;
+}
+
 static inline u64 xfeatures_mask_independent(void)
 {
        if (!boot_cpu_has(X86_FEATURE_ARCH_LBR))
index 12437383ff79fb51e93c4e63f9174730241e67be..470576ced90799b3e12acd09c77514b7f14a924f 100644 (file)
@@ -404,7 +404,7 @@ void fpu__clear_user_states(struct fpu *fpu)
        }
 
        /* Reset user states in registers. */
-       restore_fpregs_from_init_fpstate(xfeatures_mask_uabi());
+       restore_fpregs_from_init_fpstate(xfeatures_mask_restore_user());
 
        /*
         * Now all FPU registers have their desired values.  Inform the FPU