• Kan Liang's avatar
    x86/fpu: Use proper mask to replace full instruction mask · a063bf24
    Kan Liang authored
    When saving xstate to a kernel/user XSAVE area with the XSAVE family of
    instructions, the current code applies the 'full' instruction mask (-1),
    which tries to XSAVE all possible features. This method relies on
    hardware to trim 'all possible' down to what is enabled in the
    hardware. The code works well for now. However, there will be a
    problem, if some features are enabled in hardware, but are not suitable
    to be saved into all kernel XSAVE buffers, like task->fpu, due to
    performance consideration.
    
    One such example is the Last Branch Records (LBR) state. The LBR state
    only contains valuable information when LBR is explicitly enabled by
    the perf subsystem, and the size of an LBR state is large (808 bytes
    for now). To avoid both CPU overhead and space overhead at each context
    switch, the LBR state should not be saved into task->fpu like other
    state components. It should be saved/restored on demand when LBR is
    enabled in the perf subsystem. Current copy_xregs_to_* will trigger a
    buffer overflow for such cases.
    
    Three sites use the '-1' instruction mask which must be updated.
    
    Two are saving/restoring the xstate to/from a kernel-allocated XSAVE
    buffer and can use 'xfeatures_mask_all', which will save/restore all of
    the features present in a normal task FPU buffer.
    
    The last one saves the register state directly to a user buffer. It
    could
    also use 'xfeatures_mask_all'. Just as it was with the '-1' argument,
    any supervisor states in the mask will be filtered out by the hardware
    and not saved to the buffer.  But, to be more explicit about what is
    expected to be saved, use xfeatures_mask_user() for the instruction
    mask.
    
    KVM includes the header file fpu/internal.h. To avoid 'undefined
    xfeatures_mask_all' compiling issue, move copy_fpregs_to_fpstate() to
    fpu/core.c and export it, because:
    - The xfeatures_mask_all is indirectly used via copy_fpregs_to_fpstate()
      by KVM. The function which is directly used by other modules should be
      exported.
    - The copy_fpregs_to_fpstate() is a function, while xfeatures_mask_all
      is a variable for the "internal" FPU state. It's safer to export a
      function than a variable, which may be implicitly changed by others.
    - The copy_fpregs_to_fpstate() is a big function with many checks. The
      removal of the inline keyword should not impact the performance.
    Signed-off-by: default avatarKan Liang <kan.liang@linux.intel.com>
    Signed-off-by: default avatarPeter Zijlstra (Intel) <peterz@infradead.org>
    Reviewed-by: default avatarDave Hansen <dave.hansen@intel.com>
    Link: https://lkml.kernel.org/r/1593780569-62993-20-git-send-email-kan.liang@linux.intel.com
    a063bf24
core.c 11.9 KB