From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.128.70 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796214525; b=LI2+OXwlUdweYR9s3chxsWDrpO14q/fVLAiEERjvQJ+/dGGwnqoHzX+6zv3umDmeYSx4 sG1100Eq1x7lBeHZ4MQuTW+K1br8aL8axRbNqSbRiiy0MI9hZGw6W2PGWQsitgUdeHJhp nFIuYYuJqVrhCQvfTJIcJ0WADKxdPObY7KMCzCjG2fzA2V1cjuyXoMI5z+Um4P2VXZrGF 5+LcvadncV7BGVqGIWi2ra9zVFZWtK+wXcK1AM+gnb7ms5zxxQf7GBWwpqCHcHMJAyouL 5q4Lycj6Zah7htw6W1pFz81fipjETXoNKa15z8IrKDzuylWNAl/iaOzflc2KKaJRB+g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796214525; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Received:Date:In-Reply-To:Mime-Version: References:X-Developer-Key:X-Developer-Signature:X-Mailer:Message-ID: Subject:From:To:Cc:Content-Type; bh=acEf2Gfg9RJHhLoabsO7UaYIcPGxjjiLkXDqLLHEMas=; b=wkFTqRc3i/lsnm+U5QG/6cWGVxsdsey3wezBAniP2FK0RqjxXSNBnAVqQQlGppxjGj45 w7Th8KS67xa2RpmM3WDS5DIlIYS1e5RsFPln/ELpOuiyoGKpSFpvo5x+cF0RIpOgFFXoA 6E6D8hai6QEwwe4p09jf9lmhbGNHLATDY42snLsDTi5iF3HOSiis9F0HdFXLPQ8gXTmYn 32AekFpzZcQ9aY0eSrRzd5gHr6TfDBc9pTxQiimUPoSQT3TIK3K00YrCWSI3w+8Ot8YXs ZXktVqFfOFEZwSZ0zzH2wTXeEABjqWJPW5jgYKFXLH0t4SWMJ9M/b5/uzUcVRhWKB8w== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=S9HiFQ69; arc=none smtp.remote-ip=209.85.128.70 Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DA09F606A323 for ; Wed, 2 Dec 2026 12:28:44 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org DA09F606A323 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=S9HiFQ69 Received: by mail-wm1-f70.google.com with SMTP id 5b1f17b1804b1-4bb66fc3aa6so109069645e9.2 for ; Wed, 02 Dec 2026 04:28:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796214523; x=1796819323; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=acEf2Gfg9RJHhLoabsO7UaYIcPGxjjiLkXDqLLHEMas=; b=S9HiFQ69Tarfjg1WpUWB26NSynnNmzRiKufhVDqJ6StYIW2BwQAPM2zN3arHIKcBjh /wg5fJ4Q5kHYzL17m+77t6zRd3I9V+IgjxAe7WyQexe0tCHiHWS0/eDunkOdxf7QlUZM AErNbagSw6qdse3BFhd1cDRcK4pR03VeF4N45ZmyD0M6YpoGmQYquDK2TGM5D8dd9xrQ b9FYfdXw3Q/RKBBHjaoWbIqSK9K4U3L4epts3u783UYe/uv4W89qaH2R0o4LMyIwz7rA 1Gm0FANKlksx0Tyf50wkDGgAj5wt8p7mLzBncGY+EB+qP4v1jkQVx+/1+mROiR/TNbtv 5fyQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796214523; x=1796819323; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=acEf2Gfg9RJHhLoabsO7UaYIcPGxjjiLkXDqLLHEMas=; b=OySOCgawZDDEWPo9GViQfTgjVNFxBWTyltjL3ONnJEnvc0P4SNoHZx3blg599Oam+K IQ6/0kNbHOmh2f293NVBptS0XqqQqj1X2R916J27Z7GoLz7wszJodEVu4Y0Dbv+dSnEy G4EFRrgUWbkonbrY9UvmmH++vPc0OiRtc7ymg25Qre6uWxFwBiSPje6diISG+uwBR+CT 8v+yl+djAUzHsYPkAdsjGeuvOG/m4U/xGcLKpy60h5alrOYPetCrf7P35lPR5oxy8t1W GsTuFT7/lG7Ouddk6TbyFKAvN1LGEzEucQU9ix17szCJ0QoTsoMXO5Mx5KbPLUHdMktr f/5w== X-Gm-Message-State: APuU4Z8JdABP6p69CRPBExnZfHUEL6inrHRMJxl5XKjbqwNy6soT/iio A8iFsjdiJflKtI65tzErAMmUW3m9gAdVPD5ItEJbV+aonHTnqVJd8Xpr6UtpDnTqv6XFaO5D5g= = X-Received: from wmbhb5.prod.google.com ([2002:a05:600c:8685:b0:4a2:df9b:ef74]) (user=ardb job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:4fd6:b0:4bb:aa4f:c027 with SMTP id 5b1f17b1804b1-4bbca9a47f9mr155319035e9.0.1796214523224; Wed, 02 Dec 2026 04:28:43 -0800 (PST) Date: Wed, 2 Dec 2026 13:28:01 +0100 In-Reply-To: <20261202122759.1436053-4-ardb+git@google.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20261202122759.1436053-4-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=5001; i=ardb@kernel.org; h=from:subject; bh=R644x07BBcMp4BbnnujABDGoX1sh1VDIO48Nl1qZtX8=; b=owGbwMvMwCVmkMcZplerG8N4Wi2JIVuA76Ly9qzj1QeKd/sbpQSt1F2WuURqwbnDr7U9Wt9nu DTGHHXsKGVhEONikBVTZBGY/ffdztMTpWqdZ8nCzGFlAhnCwMUpABNh+cTI0Be8/hQXN1fpFIns TwENFjevLpvw6zp7aAgvT1TUnjMNKowMf7xjNk258rPixnvHV9MY2ELysjnc4y5wWK9niL3LE8T ABwA= X-Mailer: git-send-email 2.56.1.915.g69d5ec7298-goog Message-ID: <20261202122759.1436053-5-ardb+git@google.com> Subject: [PATCH 1/2] x86/entry: Move __FIXUP_INTR_SAFERET fixup out of line From: Ard Biesheuvel To: x86@kernel.org Cc: linux-kernel@vger.kernel.org, Ard Biesheuvel , Borislav Petkov , Tom Lendacky , Thomas Gleixner , Peter Zijlstra , Josh Poimboeuf , Pawan Gupta , Atish Patra Content-Type: text/plain; charset="UTF-8" From: Ard Biesheuvel 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 --- 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