x86/entry: Avoid redundant CR3 write on paranoid returns
authorLai Jiangshan <laijs@linux.alibaba.com>
Mon, 8 Jan 2024 11:39:50 +0000 (11:39 +0000)
committerThomas Gleixner <tglx@linutronix.de>
Wed, 24 Jan 2024 12:57:59 +0000 (13:57 +0100)
commitbb998361999e79bc87dae1ebe0f5bf317f632585
tree0b5ae02b0ac4007c2614f642a9fd5661e1885e19
parent6613476e225e090cc9aad49be7fa504e290dd33d
x86/entry: Avoid redundant CR3 write on paranoid returns

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 21e94459110252 ("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: Lai Jiangshan <laijs@linux.alibaba.com>
Signed-off-by: Brendan Jackman <jackmanb@google.com>
Signed-off-by: Thomas 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
arch/x86/entry/calling.h
arch/x86/entry/entry_64.S