Received: by 2002:a05:6358:4e97:b0:b3:742d:4702 with SMTP id ce23csp1435953rwb; Fri, 19 Aug 2022 03:48:49 -0700 (PDT) X-Google-Smtp-Source: AA6agR7ow5Jm8BCbp3Jj6zbglFIk//syJgbePLE7eiOGYqeglusIW7ivfusPe+L0oXSVHSYyI1ZM X-Received: by 2002:a17:902:efca:b0:172:b9f9:31b5 with SMTP id ja10-20020a170902efca00b00172b9f931b5mr5084242plb.98.1660906129598; Fri, 19 Aug 2022 03:48:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660906129; cv=none; d=google.com; s=arc-20160816; b=NTGfR26okA9oxJZnB9ZI6Oh1HBrn52VBi/Kh/RpFXRhI+nWuACJI0x89Aev/b8pCn2 Xnv4W4zURJHH6jrPB2hOTAJ43YTlxU/OKuTC+uyFUbfZgElfNV57Cvojv5EtDRDhEYdM lK4WY1A9OaJOjmiPbm/u/6OYpIF7MDGg1QDvTYHv4FTkM9t99/9fGbmv4hy6oQXnpH6U +hu9OLz2lekr9eeARCSGclI6ifi3OQm39TcwYevRU7tisCm5L53aWChJIPhAK4JLeY2N jfIlfRCx7V4bJ/HqsWr28cIuW5W5KqBTcudy131C9MPsoxLaOp8kzJnG+O/NoPlSsYBl 47Mg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=dotXOKDiE1Y3p77G/61a0SdN8QdKHMNUsMOBOW8ypLA=; b=SjVhuPUlDyoJKt2YT6kkS/6pLHv4IqYWQEcpjnAS8JB4COpZpPnzxAP5RnhpJsKYAs uEli00kkxWS1oUeMAsPc/kn1/sOH4s2nP0fvHYlAz2Yrw5mIrnP9ZcTtoX88IMbaBlnr EHDw2ZmXyh7r+U4bCGNsQDxOyuJc8wqyAgKhjZVnTgJS0ZyQFuub6XkMxDxDOi/GclEH r+OFdoWxkixyjoGjp5rPy8tOuoTO0OE5NLD1V1WULyld64kpFNmKOCm5qDPfIFnSbvcM rDrG7sf2VxBlikPt1RLqhU3wb/xkjuOSVWHAoLojTbwZLHS5zUEHeANhy1twDwloPsgn ngZw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b="blckdN/W"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id k9-20020a056a00134900b0052e525cc159si4210854pfu.160.2022.08.19.03.48.32; Fri, 19 Aug 2022 03:48:49 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b="blckdN/W"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1348130AbiHSKdw (ORCPT + 99 others); Fri, 19 Aug 2022 06:33:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49868 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1347681AbiHSKdt (ORCPT ); Fri, 19 Aug 2022 06:33:49 -0400 Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9687E2C108; Fri, 19 Aug 2022 03:33:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=dotXOKDiE1Y3p77G/61a0SdN8QdKHMNUsMOBOW8ypLA=; b=blckdN/WSOPTEjHVqp8yk9NPT0 QIdwpSuyVgGGsN7RFjen+tZmn7irX96jkAlgWREvYD3NBXcNsZabMpHJRyxxcft0HguTNOtzuRVMY wUhxjGNmqIRMXtR4hI7b47Uz06VpsOE7u6lpU0tsXVrItxQrPz3O9pnM2Qsw79QeMKPOmDco+ct5T psua+uKaS26T+MITqkD2Vse88mEwa06rym4C7jN6SDs2tsR0XodwEKp9OZAMR55h6KBWib5vY9qjA 3jjLJSL14hWNyCj/0/dlZTIsJwaXPIwgy05dVRLM3LBVJ7wMQe2d87iEOrbySSWXICOtNM1DlPfFG 39gq9tkQ==; Received: from j130084.upc-j.chello.nl ([24.132.130.84] helo=worktop.programming.kicks-ass.net) by casper.infradead.org with esmtpsa (Exim 4.94.2 #2 (Red Hat Linux)) id 1oOzJn-00B1c4-Ui; Fri, 19 Aug 2022 10:33:36 +0000 Received: by worktop.programming.kicks-ass.net (Postfix, from userid 1000) id E61AF980163; Fri, 19 Aug 2022 12:33:34 +0200 (CEST) Date: Fri, 19 Aug 2022 12:33:34 +0200 From: Peter Zijlstra To: Daniel Sneddon Cc: Greg Kroah-Hartman , linux-kernel@vger.kernel.org, stable@vger.kernel.org, Pawan Gupta , Borislav Petkov , Andrew Cooper , Thomas Gleixner , x86@kernel.org, Josh Poimboeuf Subject: Re: [PATCH] x86/nospec: Unwreck the RSB stuffing Message-ID: References: <20220809175513.345597655@linuxfoundation.org> <20220809175513.979067723@linuxfoundation.org> <839e2877-bb16-dbb5-d4da-bc611733c7e1@linux.intel.com> <84f4b1ea-d837-9a53-a21c-4ac602ff8e75@linux.intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Aug 17, 2022 at 08:55:08AM +0200, Peter Zijlstra wrote: > On Tue, Aug 16, 2022 at 11:04:36AM -0700, Daniel Sneddon wrote: > > diff --git a/arch/x86/include/asm/cpufeatures.h b/arch/x86/include/asm/cpufeatures.h > > index 1a31ae6d758b..c5b55c9f2849 100644 > > --- a/arch/x86/include/asm/cpufeatures.h > > +++ b/arch/x86/include/asm/cpufeatures.h > > @@ -420,7 +420,7 @@ > > #define X86_FEATURE_V_TSC_AUX (19*32+ 9) /* "" Virtual TSC_AUX */ > > #define X86_FEATURE_SME_COHERENT (19*32+10) /* "" AMD hardware-enforced > > cache coherency */ > > > > -#define X86_FEATURE_NEVER (-1) /* "" Logical complement of ALWAYS */ > > +#define X86_FEATURE_NEVER (0x7FFF) /* "" Logical complement of > > ALWAYS */ > > > Bah, I initially spelled that: ALT_NOT(X86_FEATURE_ALWAYS), but Boris > made me do the -1 thing there. Oh well, Boris can fix that :-) Boris, how's this then? Will you cram it into x86/urgent ? --- Subject: x86/nospec: Unwreck the RSB stuffing From: Peter Zijlstra Date: Tue, 16 Aug 2022 14:28:36 +0200 Commit 2b1299322016 ("x86/speculation: Add RSB VM Exit protections") made a right mess of the RSB stuffing, rewrite the whole thing to not suck. Thanks to Andrew for the enlightening comment about Post-Barrier RSB things so we can make this code less magical. Cc: stable@vger.kernel.org Signed-off-by: Peter Zijlstra (Intel) --- arch/x86/include/asm/nospec-branch.h | 80 +++++++++++++++++------------------ 1 file changed, 39 insertions(+), 41 deletions(-) --- a/arch/x86/include/asm/nospec-branch.h +++ b/arch/x86/include/asm/nospec-branch.h @@ -35,33 +35,44 @@ #define RSB_CLEAR_LOOPS 32 /* To forcibly overwrite all entries */ /* + * Common helper for __FILL_RETURN_BUFFER and __FILL_ONE_RETURN. + */ +#define __FILL_RETURN_SLOT \ + ANNOTATE_INTRA_FUNCTION_CALL; \ + call 772f; \ + int3; \ +772: + +/* + * Stuff the entire RSB. + * * Google experimented with loop-unrolling and this turned out to be * the optimal version - two calls, each with their own speculation * trap should their return address end up getting used, in a loop. */ -#define __FILL_RETURN_BUFFER(reg, nr, sp) \ - mov $(nr/2), reg; \ -771: \ - ANNOTATE_INTRA_FUNCTION_CALL; \ - call 772f; \ -773: /* speculation trap */ \ - UNWIND_HINT_EMPTY; \ - pause; \ - lfence; \ - jmp 773b; \ -772: \ - ANNOTATE_INTRA_FUNCTION_CALL; \ - call 774f; \ -775: /* speculation trap */ \ - UNWIND_HINT_EMPTY; \ - pause; \ - lfence; \ - jmp 775b; \ -774: \ - add $(BITS_PER_LONG/8) * 2, sp; \ - dec reg; \ - jnz 771b; \ - /* barrier for jnz misprediction */ \ +#define __FILL_RETURN_BUFFER(reg, nr) \ + mov $(nr/2), reg; \ +771: \ + __FILL_RETURN_SLOT \ + __FILL_RETURN_SLOT \ + add $(BITS_PER_LONG/8) * 2, %_ASM_SP; \ + dec reg; \ + jnz 771b; \ + /* barrier for jnz misprediction */ \ + lfence; + +/* + * Stuff a single RSB slot. + * + * To mitigate Post-Barrier RSB speculation, one CALL instruction must be + * forced to retire before letting a RET instruction execute. + * + * On PBRSB-vulnerable CPUs, it is not safe for a RET to be executed + * before this point. + */ +#define __FILL_ONE_RETURN \ + __FILL_RETURN_SLOT \ + add $(BITS_PER_LONG/8), %_ASM_SP; \ lfence; #ifdef __ASSEMBLY__ @@ -132,28 +143,15 @@ #endif .endm -.macro ISSUE_UNBALANCED_RET_GUARD - ANNOTATE_INTRA_FUNCTION_CALL - call .Lunbalanced_ret_guard_\@ - int3 -.Lunbalanced_ret_guard_\@: - add $(BITS_PER_LONG/8), %_ASM_SP - lfence -.endm - /* * A simpler FILL_RETURN_BUFFER macro. Don't make people use the CPP * monstrosity above, manually. */ -.macro FILL_RETURN_BUFFER reg:req nr:req ftr:req ftr2 -.ifb \ftr2 - ALTERNATIVE "jmp .Lskip_rsb_\@", "", \ftr -.else - ALTERNATIVE_2 "jmp .Lskip_rsb_\@", "", \ftr, "jmp .Lunbalanced_\@", \ftr2 -.endif - __FILL_RETURN_BUFFER(\reg,\nr,%_ASM_SP) -.Lunbalanced_\@: - ISSUE_UNBALANCED_RET_GUARD +.macro FILL_RETURN_BUFFER reg:req nr:req ftr:req ftr2=ALT_NOT(X86_FEATURE_ALWAYS) + ALTERNATIVE_2 "jmp .Lskip_rsb_\@", \ + __stringify(__FILL_RETURN_BUFFER(\reg,\nr)), \ftr, \ + __stringify(__FILL_ONE_RETURN), \ftr2 + .Lskip_rsb_\@: .endm