• Lai Jiangshan's avatar
    x86/entry: Avoid redundant CR3 write on paranoid returns · bb998361
    Lai Jiangshan authored
    The CR3 restore happens in:
    
      1. #NMI return.
      2. paranoid_exit() (i.e. #MCE, #VC, #DB and #DF return)
    
    Contrary to the implication in commit 21e94459 ("x86/mm: Optimize
    RESTORE_CR3"), the kernel never modifies CR3 in any of these exceptions,
    except for switching from user to kernel pagetables under PTI. That
    means that most of the time when returning from an exception that
    interrupted the kernel no CR3 restore is necessary. Writing CR3 is
    expensive on some machines.
    
    Most of the time because the interrupt might have come during kernel entry
    before the user to kernel CR3 switch or the during exit after the kernel to
    user switch. In the former case skipping the restore would be correct, but
    definitely not for the latter.
    
    So check the saved CR3 value and restore it only, if it is a user CR3.
    
    Give the macro a new name to clarify its usage, and remove a comment that
    was describing the original behaviour along with the not longer needed jump
    label.
    Signed-off-by: default avatarLai Jiangshan <laijs@linux.alibaba.com>
    Signed-off-by: default avatarBrendan Jackman <jackmanb@google.com>
    Signed-off-by: default avatarThomas Gleixner <tglx@linutronix.de>
    Link: https://lore.kernel.org/r/20240108113950.360438-1-jackmanb@google.com
    
    [Rewrote commit message; responded to review comments]
    Change-Id: I6e56978c4753fb943a7897ff101f519514fa0827
    bb998361
entry_64.S 41.3 KB