• Thomas Gleixner's avatar
    x86/fpu: Make init_fpstate correct with optimized XSAVE · f9dfb5e3
    Thomas Gleixner authored
    The XSAVE init code initializes all enabled and supported components with
    XRSTOR(S) to init state. Then it XSAVEs the state of the components back
    into init_fpstate which is used in several places to fill in the init state
    of components.
    
    This works correctly with XSAVE, but not with XSAVEOPT and XSAVES because
    those use the init optimization and skip writing state of components which
    are in init state. So init_fpstate.xsave still contains all zeroes after
    this operation.
    
    There are two ways to solve that:
    
       1) Use XSAVE unconditionally, but that requires to reshuffle the buffer when
          XSAVES is enabled because XSAVES uses compacted format.
    
       2) Save the components which are known to have a non-zero init state by other
          means.
    
    Looking deeper, #2 is the right thing to do because all components the
    kernel supports have all-zeroes init state except the legacy features (FP,
    SSE). Those cannot be hard coded because the states are not identical on all
    CPUs, but they can be saved with FXSAVE which avoids all conditionals.
    
    Use FXSAVE to save the legacy FP/SSE components in init_fpstate along with
    a BUILD_BUG_ON() which reminds developers to validate that a newly added
    component has all zeroes init state. As a bonus remove the now unused
    copy_xregs_to_kernel_booting() crutch.
    
    The XSAVE and reshuffle method can still be implemented in the unlikely
    case that components are added which have a non-zero init state and no
    other means to save them. For now, FXSAVE is just simple and good enough.
    
      [ bp: Fix a typo or two in the text. ]
    
    Fixes: 6bad06b7 ("x86, xsave: Use xsaveopt in context-switch path when supported")
    Signed-off-by: default avatarThomas Gleixner <tglx@linutronix.de>
    Signed-off-by: default avatarBorislav Petkov <bp@suse.de>
    Reviewed-by: default avatarBorislav Petkov <bp@suse.de>
    Cc: stable@vger.kernel.org
    Link: https://lkml.kernel.org/r/20210618143444.587311343@linutronix.de
    f9dfb5e3
xstate.c 38.6 KB