All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/2] x86: Fix bloated SafeRET fixup alternatives
@ 2026-12-02 12:28 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 ` [PATCH 2/2] x86/entry: Implement the SafeRET fixup check more compactly Ard Biesheuvel
  0 siblings, 2 replies; 3+ messages in thread
From: Ard Biesheuvel @ 2026-12-02 12:28 UTC (permalink / raw)
  To: x86
  Cc: linux-kernel, Ard Biesheuvel, Borislav Petkov, Tom Lendacky,
	Thomas Gleixner, Peter Zijlstra, Josh Poimboeuf, Pawan Gupta,
	Atish Patra

From: Ard Biesheuvel <ardb@kernel.org>

The SafeRET fixup helpers are expanded into the entry code using
alternatives patching, even though most of the code can happily be
placed out of line. The remaining logic can be expressed more
succinctly, resulting in less code bloat in the entry code itself.

This reduces the size of the alternative from 67 to 25 bytes (or 49 to
22 bytes for the NMI variety)

I spotted this while working on RIP-relative codegen for vmlinux.

Cc: Borislav Petkov <bp@alien8.de>
Cc: Tom Lendacky <thomas.lendacky@amd.com>
Cc: Thomas Gleixner <tglx@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Josh Poimboeuf <jpoimboe@kernel.org>
Cc: Pawan Gupta <pawan.kumar.gupta@linux.intel.com>
Cc: Atish Patra <atishp@meta.com>

Ard Biesheuvel (2):
  x86/entry: Move __FIXUP_INTR_SAFERET fixup out of line
  x86/entry: Implement the SafeRET fixup check more compactly

 arch/x86/entry/entry_64.S            |  6 +--
 arch/x86/include/asm/nospec-branch.h | 53 +++++++++++---------
 2 files changed, 33 insertions(+), 26 deletions(-)

-- 
2.56.1.915.g69d5ec7298-goog


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH 1/2] x86/entry: Move __FIXUP_INTR_SAFERET fixup out of line
  2026-12-02 12:28 [PATCH 0/2] x86: Fix bloated SafeRET fixup alternatives Ard Biesheuvel
@ 2026-12-02 12:28 ` Ard Biesheuvel
  2026-12-02 12:28 ` [PATCH 2/2] x86/entry: Implement the SafeRET fixup check more compactly Ard Biesheuvel
  1 sibling, 0 replies; 3+ messages in thread
From: Ard Biesheuvel @ 2026-12-02 12:28 UTC (permalink / raw)
  To: x86
  Cc: linux-kernel, Ard Biesheuvel, Borislav Petkov, Tom Lendacky,
	Thomas Gleixner, Peter Zijlstra, Josh Poimboeuf, Pawan Gupta,
	Atish Patra

From: Ard Biesheuvel <ardb@kernel.org>

The __FIXUP_INTR_SAFERET macro checks whether a RIP value recorded in an
exception or NMI frame points right before or after the LEA instruction
in the SRSO safe return helper, as the sequence needs to be emulated in
that case. This is a very rare occurrence, but the check is needed on
every exception or NMI taken from kernel mode.

However, the fixup itself is stone cold code, so place it out of line.
Doing so reduces the size of the alternative sequence from 67 to 32
bytes, reducing I-cache pollution on a hot path for all cores, not just
the ones that need this mitigation.

The out of line fixup is identical between the two SafeRET mitigation
variants, so deduplicate it, leaving only a single copy for each call
site.

While at it, make the implicit use of %RDX as a scratch register
explicit at the call sites.

Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
---
 arch/x86/entry/entry_64.S            |  6 +--
 arch/x86/include/asm/nospec-branch.h | 47 +++++++++++---------
 2 files changed, 29 insertions(+), 24 deletions(-)

diff --git a/arch/x86/entry/entry_64.S b/arch/x86/entry/entry_64.S
index 7cfc617750c5..4bfbfebdd832 100644
--- a/arch/x86/entry/entry_64.S
+++ b/arch/x86/entry/entry_64.S
@@ -937,7 +937,7 @@ SYM_CODE_START(paranoid_entry)
 	IBRS_ENTER save_reg=%r15
 	UNTRAIN_RET_FROM_CALL
 
-	FIXUP_INTR_SAFERET %rsp
+	FIXUP_INTR_SAFERET %rsp scratch=%rdx
 
 	RET
 SYM_CODE_END(paranoid_entry)
@@ -1044,7 +1044,7 @@ SYM_CODE_START(error_entry)
 
 	VALIDATE_UNRET_END
 
-	FIXUP_INTR_SAFERET %rsp
+	FIXUP_INTR_SAFERET %rsp scratch=%rdx
 
 	cmpq	$.Lgs_change, RIP+8(%rsp)
 	jne	.Lerror_entry_done_lfence
@@ -1206,7 +1206,7 @@ SYM_CODE_START(asm_exc_nmi)
 
 .Lnmi_from_kernel:
 	/* Repair the source frame before inspecting or copying it. */
-	FIXUP_NMI_INTR_SAFERET %rsp
+	FIXUP_NMI_INTR_SAFERET %rsp scratch=%rdx
 
 	/*
 	 * Here's what our stack frame will look like:
diff --git a/arch/x86/include/asm/nospec-branch.h b/arch/x86/include/asm/nospec-branch.h
index 64554434320d..5ef8f3f12b15 100644
--- a/arch/x86/include/asm/nospec-branch.h
+++ b/arch/x86/include/asm/nospec-branch.h
@@ -189,7 +189,8 @@
  * L1:	LEA	8(%RSP), %RSP
  * L2:	RET
  *
- * The C equivalent of the ASM sequence below is:
+ * The C equivalent of the ASM sequence in __FIXUP_INTR_SAFERET below (and
+ * __FIXUP_INTR_SAFERET_ALT further down) is:
  *
  *	if (ptr->RIP > L2)	// After RET?
  *		goto done;
@@ -204,18 +205,11 @@
  * This ensures that this interrupt returns to the original return IP without
  * resuming the interrupted safe-RET sequence.
  */
-#define __FIXUP_INTR_SAFERET(name, ptr, rip_offs, rsp_offs)	\
-	cmpq	$(name)+5, rip_offs(ptr);			\
+#define __FIXUP_INTR_SAFERET(name, ip, fixup)			\
+	cmpq	$(name)+5, ip;					\
 	ja	2f;						\
-	cmpq	$(name), rip_offs(ptr);				\
-	jb	2f;						\
-	jne	1f;						\
-	addq	$8, rsp_offs(ptr);				\
-	1:;							\
-	movq	rsp_offs(ptr), %rdx;				\
-	movq	(%rdx), %rdx;					\
-	movq	%rdx, rip_offs(ptr);				\
-	addq	$8, rsp_offs(ptr);				\
+	cmpq	$(name), ip;					\
+	jae	fixup;						\
 	2:
 
 
@@ -336,26 +330,37 @@
 #define UNTRAIN_RET_FROM_CALL \
 	__UNTRAIN_RET X86_FEATURE_ENTRY_IBPB, __stringify(RESET_CALL_DEPTH_FROM_CALL)
 
-.macro FIXUP_INTR_SAFERET pt_regs
+.macro __FIXUP_INTR_SAFERET_ALT rip:req, rsp:req, scratch:req
 #ifdef CONFIG_MITIGATION_SRSO
 	ALTERNATIVE_2 "", \
-	__stringify(__FIXUP_INTR_SAFERET(srso_safe_ret, \pt_regs, RIP+8, RSP+8)), X86_FEATURE_SRSO, \
-	__stringify(__FIXUP_INTR_SAFERET(srso_alias_safe_ret, \pt_regs, RIP+8, RSP+8)), X86_FEATURE_SRSO_ALIAS
+	__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
+	.subsection 1
+.Lfixup\@:
+	jne	1f
+	addq	$8, \rsp		/* Emulate LEA */
+1:	movq	\rsp, \scratch		/* Read original return IP */
+	movq	(\scratch), \scratch
+	movq	\scratch, \rip		/* Overwrite interrupt return IP */
+	addq	$8, \rsp		/* Emulate RET */
+	jmp	.Ldone\@
+	.previous
+.Ldone\@:
 #endif
 .endm
 
+.macro FIXUP_INTR_SAFERET pt_regs:req, scratch:req
+	__FIXUP_INTR_SAFERET_ALT (RIP+8)(\pt_regs), (RSP+8)(\pt_regs), \scratch
+.endm
+
 /*
  * Note the offsets encoding below:
  *
  * The IRET frame at call site in asm_exc_nmi() starts at 8(%rsp) (NMI handler
  * users %rdx as a temp var so it pushes it on the stack on entry).
  */
-.macro FIXUP_NMI_INTR_SAFERET fptr
-#ifdef CONFIG_MITIGATION_SRSO
-	ALTERNATIVE_2 "", \
-	__stringify(__FIXUP_INTR_SAFERET(srso_safe_ret, \fptr, RIP-RIP+8, RSP-RIP+8)), X86_FEATURE_SRSO, \
-	__stringify(__FIXUP_INTR_SAFERET(srso_alias_safe_ret, \fptr, RIP-RIP+8, RSP-RIP+8)), X86_FEATURE_SRSO_ALIAS
-#endif
+.macro FIXUP_NMI_INTR_SAFERET fptr:req, scratch:req
+	__FIXUP_INTR_SAFERET_ALT (RIP-RIP+8)(\fptr), (RSP-RIP+8)(\fptr), \scratch
 .endm
 
 .macro CALL_DEPTH_ACCOUNT
-- 
2.56.1.915.g69d5ec7298-goog


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH 2/2] x86/entry: Implement the SafeRET fixup check more compactly
  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
  1 sibling, 0 replies; 3+ messages in thread
From: Ard Biesheuvel @ 2026-12-02 12:28 UTC (permalink / raw)
  To: x86
  Cc: linux-kernel, Ard Biesheuvel, Borislav Petkov, Tom Lendacky,
	Thomas Gleixner, Peter Zijlstra, Josh Poimboeuf, Pawan Gupta,
	Atish Patra

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


^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-12-02 12:28 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 ` [PATCH 2/2] x86/entry: Implement the SafeRET fixup check more compactly Ard Biesheuvel

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.