All of lore.kernel.org
 help / color / mirror / Atom feed
From: Ard Biesheuvel <ardb+git@google.com>
To: x86@kernel.org
Cc: linux-kernel@vger.kernel.org, Ard Biesheuvel <ardb@kernel.org>,
	 Borislav Petkov <bp@alien8.de>,
	Tom Lendacky <thomas.lendacky@amd.com>,
	 Thomas Gleixner <tglx@kernel.org>,
	Peter Zijlstra <peterz@infradead.org>,
	 Josh Poimboeuf <jpoimboe@kernel.org>,
	Pawan Gupta <pawan.kumar.gupta@linux.intel.com>,
	 Atish Patra <atishp@meta.com>
Subject: [PATCH 2/2] x86/entry: Implement the SafeRET fixup check more compactly
Date: Wed,  2 Dec 2026 13:28:02 +0100	[thread overview]
Message-ID: <20261202122759.1436053-6-ardb+git@google.com> (raw)
In-Reply-To: <20261202122759.1436053-4-ardb+git@google.com>

From: Ard Biesheuvel <ardb@kernel.org>

The check in __FIXUP_INTR_SAFERET() decides whether the RIP of an
interrupted context points into the SafeRET sequence. It does so by
using back to back CMP+Jcc instruction pairs, comparing the RIP in
memory with two 32-bit immediates that represent the start and the end
of the 5 byte window.

On Zen 3/4, this is optimal, even though the 12-byte CMP can only be
decoded by the first of the four decode slots, and does not combine
into a single macro-op with the subsequent Jcc: the first branch is
virtually always taken, and therefore virtually always predicted
correctly, and so the second CMP+Jcc pair is rarely executed.

On Zen 1/2, however, whether the first branch is taken or not depends on
whether the interrupted context lives before or after the SafeRET
sequence in the kernel code (i.e., in core kernel text or in a module or
BPF program), and so it is prone to misprediction. When it is not taken,
the second 12-byte CMP cannot be decoded in the same cycle as the first
(unless served from the op cache), as only the first decode slot can
handle instructions longer than 8 bytes, and it does not combine with
the subsequent Jcc either.

Finally, the 32-byte sequence (26 bytes at the NMI site) is longer than
necessary, resulting in needless I-cache pollution on unaffected cores.

Write this more succinctly, by comparing 'end - RIP' with 'end - start',
which yields unsigned 'below or equal' if and only if the SafeRET
sequence was interrupted, and where 'equal' means that RIP points to the
LEA itself, which the fixup relies on.

This needs only a single CMP+Jcc, whose outcome is the same for any RIP
outside of the window, so that it is always predicted correctly, and
which can be fused into a single macro-op. The resulting sequence
therefore always takes 3 macro-ops to execute: one fewer than before on
Zen 1/2 for RIPs in core kernel text, and one more on Zen 3/4, where it
makes no measurable difference (-0.1% +/- 0.3% for an exception fixed up
via the exception table, on a Zen 4 KVM guest using safe-RET).

The resulting alternative is 7 bytes shorter at the exception entry
sites (25 bytes instead of 32), and 4 bytes shorter at the NMI site (22
bytes instead of 26).

Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
---
 arch/x86/include/asm/nospec-branch.h | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)

diff --git a/arch/x86/include/asm/nospec-branch.h b/arch/x86/include/asm/nospec-branch.h
index 5ef8f3f12b15..e56e9e8dd8a1 100644
--- a/arch/x86/include/asm/nospec-branch.h
+++ b/arch/x86/include/asm/nospec-branch.h
@@ -202,15 +202,17 @@
  *	ptr->RIP = tmp;		// Overwrite interrupt return IP
  *	ptr->RSP += 8;		// Emulate RET
  *
+ * The range check is a single unsigned compare of L2 - RIP against L2 - L1,
+ * which sets ZF when RIP == L1. The fixup handler relies on this.
+ *
  * This ensures that this interrupt returns to the original return IP without
  * resuming the interrupted safe-RET sequence.
  */
-#define __FIXUP_INTR_SAFERET(name, ip, fixup)			\
-	cmpq	$(name)+5, ip;					\
-	ja	2f;						\
-	cmpq	$(name), ip;					\
-	jae	fixup;						\
-	2:
+#define __FIXUP_INTR_SAFERET(name, ip, fixup, scratch)		\
+	leaq	(name)+5(%rip), scratch;			\
+	subq	ip, scratch;					\
+	cmpq	$5, scratch;					\
+	jbe	fixup
 
 
 #ifdef __ASSEMBLER__
@@ -333,8 +335,8 @@
 .macro __FIXUP_INTR_SAFERET_ALT rip:req, rsp:req, scratch:req
 #ifdef CONFIG_MITIGATION_SRSO
 	ALTERNATIVE_2 "", \
-	__stringify(__FIXUP_INTR_SAFERET(srso_safe_ret, \rip, .Lfixup\@)), X86_FEATURE_SRSO, \
-	__stringify(__FIXUP_INTR_SAFERET(srso_alias_safe_ret, \rip, .Lfixup\@)), X86_FEATURE_SRSO_ALIAS
+	__stringify(__FIXUP_INTR_SAFERET(srso_safe_ret, \rip, .Lfixup\@, \scratch)), X86_FEATURE_SRSO, \
+	__stringify(__FIXUP_INTR_SAFERET(srso_alias_safe_ret, \rip, .Lfixup\@, \scratch)), X86_FEATURE_SRSO_ALIAS
 	.subsection 1
 .Lfixup\@:
 	jne	1f
-- 
2.56.1.915.g69d5ec7298-goog


      parent reply	other threads:[~2026-12-02 12:28 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02 12:28 [PATCH 0/2] x86: Fix bloated SafeRET fixup alternatives Ard Biesheuvel
2026-12-02 12:28 ` [PATCH 1/2] x86/entry: Move __FIXUP_INTR_SAFERET fixup out of line Ard Biesheuvel
2026-12-02 12:28 ` Ard Biesheuvel [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261202122759.1436053-6-ardb+git@google.com \
    --to=ardb+git@google.com \
    --cc=ardb@kernel.org \
    --cc=atishp@meta.com \
    --cc=bp@alien8.de \
    --cc=jpoimboe@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pawan.kumar.gupta@linux.intel.com \
    --cc=peterz@infradead.org \
    --cc=tglx@kernel.org \
    --cc=thomas.lendacky@amd.com \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.