From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.218.70 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796214526; b=xUlfeYzY2V0wV1rS7zE+CnjPkP16XXThumNAqs7/Wjd52cVRXnaa2AdYsIpMD3jukwF+ V/VPna1g+oTkrxDVlTwF2RAZ/KSpSxexY9Z/pZPITUAcYX5SPynvILGC16q0n/h6xOFrF X473gJb6e9bnoGRDi3vi4V+yb/ynIonhkA08MV2A8ZeIPlaR+GI0t+mTGmLs3HOuJ+vnZ QAn9hpFoXXW3rmQp5Z176yhO3mXDoU+gCpieFSrDeawFwKszBCtNLikDUKpGz303nZ+bS u9wvBQ0pvFurU/ym5aU5hPpk4eZkWRZViRN3AW2MO3C3dWB4nAam530PO8qTa0+sk1w== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796214526; 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=CBTef0XXOsULBfKLh2MWyBKjT7C3/iSbpLv/qNvSfNg=; b=po5/tW+r2nJLVDzbm/mBCbjmgzSV8MWOTEK1w7iafw8bcP9V91bqlENMMWYFB1F8MRYa oMdxiMxSHW2f+ujdZd2VjR+2y4rrklivtr1xN7qUd8EAhTJsw5OpPm7RtTv+WLKZq8gDv cpbAy8G+UObGt+tF4dhhhvGJEjYPcjIYrOHy8bzfw8vfgPMS9GZqMYGbtatc5v/abDNb1 D7wIpOBL4blG356yTu3fV7+KwSMxPJhJ8za3/rImVfcWRYwgb79UjvmAX76vQ/J6W3u51 Kt0YfUdZxjB1GUEML99E0kafK2UaK5x9YrPYFgA4CzLedI8xYopTO2xawepKlfjwY9w== 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=qQbdSxBa; arc=none smtp.remote-ip=209.85.218.70 Received: from mail-ej1-f70.google.com (mail-ej1-f70.google.com [209.85.218.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 37BD8606A337 for ; Wed, 2 Dec 2026 12:28:46 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 37BD8606A337 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=qQbdSxBa Received: by mail-ej1-f70.google.com with SMTP id a640c23a62f3a-c5101fd59e3so309877566b.0 for ; Wed, 02 Dec 2026 04:28:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796214525; x=1796819325; 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=CBTef0XXOsULBfKLh2MWyBKjT7C3/iSbpLv/qNvSfNg=; b=qQbdSxBaxQeYqMPOTSRmB8dkOB3eGxry//kZBwRWGKYXysXI5zgBiDVcVaHzkxXiDx 5lP66QeVs/bj5ih4Xz3Vp08LL+lHnGxorSJ0vT9Vv+jGGLsIVOu5BZgZV6yUNWVZca9G ZITVbjPOCEHdokiNMhCqZW5y8pZNv4eihAvBxhXAHdBCR4eOLP35jyMdtkqs6rEthLeA k+xSGO5kL4d7nmblNCHcgvIOkR6v3LRdE19+H3liF3O1dbokII73TamZDqX/i9pA4n3U uEjKxe5yU1SEGVvnzlr1Ed8LP4eoAnH47QUzup4mvjCT0yh+3NMsxnr1SVuVif3Zjlpu yc/w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796214525; x=1796819325; 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=CBTef0XXOsULBfKLh2MWyBKjT7C3/iSbpLv/qNvSfNg=; b=maZT3jPDwENrofyAjvFsnGYvOEEgE9qDkPnP5eC1RTbRn4bkdYS/E5mg58UpErvndP ov+gm2Qxdh9du1KdODxyAKqHfI+1I04LzGxKvPFGXX2Ag7TxMAS/hqBIZNURF/XIy57h K8H/2kYseJasomZFuq2fp1ZgxngqgSPPsUUW+mmLd8WwPRgPiabRsnc2foTRBpybt6xe nRBfFNblaQQCybLvhrGEPW+8Iy4y8va++Qaqpg1ESGjsyxLUWyWXzfaN7DNIrKnOSwAi md/x7WccYq0ovcbtfBX8vA26zezhAyqgPQxtrFRH0K/CnO2BU2Eeo9JHxmC4B7Mefto9 /Xrw== X-Gm-Message-State: APuU4Z/i1/WcLpvWL7h4GlkHj2C6U6CAzyT5NzJTOeEVgbc74sa+vZEp JC4umC1WioTzIOONy083mHiRnnZ3EnbF6pFSdDp0GnRN7CQvZVN0Fp5sdfFoHMayfynVe4Z3Xw= = X-Received: from ejex11.prod.google.com ([2002:a17:906:298b:b0:c50:7d13:f19a]) (user=ardb job=prod-delivery.src-stubby-dispatcher) by 2002:a17:906:369b:b0:c46:ba33:1f0a with SMTP id a640c23a62f3a-c513ab2d54amr2476677666b.31.1796214524467; Wed, 02 Dec 2026 04:28:44 -0800 (PST) Date: Wed, 2 Dec 2026 13:28:02 +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=3994; i=ardb@kernel.org; h=from:subject; bh=kfNbiKaPf/Sz6Gvpst/dL34PqXRnCGaCyqMY+ugw6iA=; b=owGbwMvMwCVmkMcZplerG8N4Wi2JIVuA73LnEzExztXJylmcZ7cdvWF7YvVZi5TInfqqbVlLW Tf36J3rKGVhEONikBVTZBGY/ffdztMTpWqdZ8nCzGFlAhnCwMUpABNp4mRk2PfwXvgur2Py6jLf vj5//L53TVKZkf5Dzg7B5Lt7zJ5z5TIy3NkvliThu3vvh8nnOPps0+WkGR7+OTlNXPVde9o6pX8 c7AA= X-Mailer: git-send-email 2.56.1.915.g69d5ec7298-goog Message-ID: <20261202122759.1436053-6-ardb+git@google.com> Subject: [PATCH 2/2] x86/entry: Implement the SafeRET fixup check more compactly 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 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 --- 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