Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp309742rwb; Wed, 5 Oct 2022 19:38:14 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4YSJwc4vN0ICSJhhNrNoZW6WXXRpRsTxzv2lGQMeBgnJO2QG+2JXqVnS3aOaVLceeuEBgE X-Received: by 2002:a17:902:7c0d:b0:178:a6ca:1dbc with SMTP id x13-20020a1709027c0d00b00178a6ca1dbcmr2552907pll.115.1665023894186; Wed, 05 Oct 2022 19:38:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1665023894; cv=none; d=google.com; s=arc-20160816; b=noLpbGpVb/oMy0fRFCKPwKkCXRiQvGi028JEzzO4GyzkEGQz+RAQjfS5y0A7FgpGWc LCAhYJTCnP92pGD2JuKsx90L/4efYfNHJ8UUtlxpXs2qli9ifYbITJ66j3wRBzkGs1gZ KguadoKW8E/LjvqmCkOR0SOUP6qtT4TbdO/UnLIStWkRcuskUhVfOfo+1F077yvPL6Ly YmOJ/Zuz1/Q1fHpkfvfz5bgntgT9v/N+Ng1YuxaJjon2XeYLG0vUGrIPG4brUeQ74dX+ p/MMsLJdEdwFgKeYY6pPtyN8hr5GkdY6MSoi1VAW4rD9DzlGWGDT9CyNvt9aWwRHSmMY vVYw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=pqXx66Ksh1NF1TcKZ27pwFtk7SoBoxPosiYEwFzKXW8=; b=H2uV8kdaGGcp6KgCmzeHOp7TS60Qi8fhggHuF7FKlqtaS2pfBv4vpyhvFYnwUxIntc hPTjcnCue3NriU5mIbrunjs9IF59i4Agie+JFZTm29SR/ADFEBf+lE+nzIFihdr+RWu4 E8aUJShbQzWwTqWQmCMddRocV9qyhL5hZ1EuBDhZj2Q8KVVAr6GrwSwJfXxYgSKVbmvX u6oo0E9rhPUhcc3W+qaGQfcpo4xdODs90nuUukUfxy/FmEznyKrc13QLpMeLtx2IVjhc 2CylVxiOj1A1gz8p74oxfnDrSFzvzAReh8BlQzJ9wM972S251nrj3ZItMyWUn6koWM1S UhHA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=Xc5Tc6UK; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id z1-20020a170902ee0100b0017e733a15b6si11309315plb.286.2022.10.05.19.37.50; Wed, 05 Oct 2022 19:38:14 -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=@google.com header.s=20210112 header.b=Xc5Tc6UK; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229888AbiJFB2Z (ORCPT + 99 others); Wed, 5 Oct 2022 21:28:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53324 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229720AbiJFB2X (ORCPT ); Wed, 5 Oct 2022 21:28:23 -0400 Received: from mail-oa1-x2c.google.com (mail-oa1-x2c.google.com [IPv6:2001:4860:4864:20::2c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D217A5809F for ; Wed, 5 Oct 2022 18:28:21 -0700 (PDT) Received: by mail-oa1-x2c.google.com with SMTP id 586e51a60fabf-132fb4fd495so618975fac.12 for ; Wed, 05 Oct 2022 18:28:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=pqXx66Ksh1NF1TcKZ27pwFtk7SoBoxPosiYEwFzKXW8=; b=Xc5Tc6UKAhhi67OFF1BPuuO5IO2gDSKBjJx/Z03c7KeQX2vaOUB+22Xe/LkpEpF1uu 71OnA+X2I5Ogl7dZtss1zKRD+L+W6Az8mzArG7/2F4HDna5EidmpsYCi7uJ8azlWLjOf S6ijA9l3oU6iLVGGRpdg03YWylfiX265APqpMDYEyxLT8RZLrkibNzDUP1AqWJdQHf9L X1VLhmCQ5YyXPWpUqeTgpZW/knqla5OUMxIwJAQZbcX10D5G944vzQMeQIl/v29OTbl1 /GIjdiZa38OX0Je1AwcFV3673VrtztS0b9g3PvFGoI0gN+E6jR1dn9RRIngODUtDVweM BFtQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=pqXx66Ksh1NF1TcKZ27pwFtk7SoBoxPosiYEwFzKXW8=; b=fnlfqHvb922XupQXir055htKlRSyViKWGH/XtZh6UC5c6Rnz7i2+ZcKkBKbaxv+DXa UZ7kOFnZwiQVXD4J6/lkB2cK7KMEkYLPqO5lpTHYThYny47cPyaGn9lo758eAJWjSUqY s+C/rYBjJ/0mwVrUt0L6DsbeasJIMmj1+7MgB43Oo5U+3Wouxd4W88d59HJwPbuMqdpq I+PsX+T9QQTbuONCbN3WVFyb+mIGDzPx2vPhZ+jxdf7tPpNLbtpi3xJtmglGZWYcVLZ2 /S2Bz9lC1H/39erH3vHVxqoVFzvNWJrwe8XGe/why43kUI6PSmIvv+G7rc3M3cObuFZ5 wPJA== X-Gm-Message-State: ACrzQf3o6cT9NCrdCf20zu6PngvwjChFn9Uo4CvTWSjPF1EsVwriqP9Y tGK7cN2mBJ0vc71tHp281ZqCbTu1+icZYhNFUVq3BQ== X-Received: by 2002:a05:6870:580c:b0:12a:f136:a8f5 with SMTP id r12-20020a056870580c00b0012af136a8f5mr3907624oap.269.1665019700821; Wed, 05 Oct 2022 18:28:20 -0700 (PDT) MIME-Version: 1.0 References: <20221005220227.1959-1-surajjs@amazon.com> <684c8ef6-bf69-e31e-fb3e-d3beca52fd15@linux.intel.com> In-Reply-To: <684c8ef6-bf69-e31e-fb3e-d3beca52fd15@linux.intel.com> From: Jim Mattson Date: Wed, 5 Oct 2022 18:28:09 -0700 Message-ID: Subject: Re: [PATCH] x86/speculation: Mitigate eIBRS PBRSB predictions with WRMSR To: Daniel Sneddon Cc: Suraj Jitindar Singh , kvm@vger.kernel.org, sjitindarsingh@gmail.com, linux-kernel@vger.kernel.org, x86@kernel.org, tglx@linutronix.de, mingo@redhat.com, bp@suse.de, dave.hansen@linux.intel.com, seanjc@google.com, pbonzini@redhat.com, peterz@infradead.org, jpoimboe@kernel.org, pawan.kumar.gupta@linux.intel.com, benh@kernel.crashing.org, stable@vger.kernel.org Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL autolearn=unavailable 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, Oct 5, 2022 at 5:26 PM Daniel Sneddon wrote: > > On 10/5/22 16:46, Jim Mattson wrote: > > On Wed, Oct 5, 2022 at 3:03 PM Suraj Jitindar Singh wrote: > >> > >> tl;dr: The existing mitigation for eIBRS PBRSB predictions uses an INT3 to > >> ensure a call instruction retires before a following unbalanced RET. Replace > >> this with a WRMSR serialising instruction which has a lower performance > >> penalty. > >> > >> == Background == > >> > >> eIBRS (enhanced indirect branch restricted speculation) is used to prevent > >> predictor addresses from one privilege domain from being used for prediction > >> in a higher privilege domain. > >> > >> == Problem == > >> > >> On processors with eIBRS protections there can be a case where upon VM exit > >> a guest address may be used as an RSB prediction for an unbalanced RET if a > >> CALL instruction hasn't yet been retired. This is termed PBRSB (Post-Barrier > >> Return Stack Buffer). > >> > >> A mitigation for this was introduced in: > >> (2b1299322016731d56807aa49254a5ea3080b6b3 x86/speculation: Add RSB VM Exit protections) > >> > >> This mitigation [1] has a ~1% performance impact on VM exit compared to without > >> it [2]. > >> > >> == Solution == > >> > >> The WRMSR instruction can be used as a speculation barrier and a serialising > >> instruction. Use this on the VM exit path instead to ensure that a CALL > >> instruction (in this case the call to vmx_spec_ctrl_restore_host) has retired > >> before the prediction of a following unbalanced RET. > >> > >> This mitigation [3] has a negligible performance impact. > >> > >> == Testing == > >> > >> Run the outl_to_kernel kvm-unit-tests test 200 times per configuration which > >> counts the cycles for an exit to kernel mode. > >> > >> [1] With existing mitigation: > >> Average: 2026 cycles > >> [2] With no mitigation: > >> Average: 2008 cycles > >> [3] With proposed mitigation: > >> Average: 2008 cycles > >> > >> Signed-off-by: Suraj Jitindar Singh > >> Cc: stable@vger.kernel.org > >> --- > >> arch/x86/include/asm/nospec-branch.h | 7 +++---- > >> arch/x86/kvm/vmx/vmenter.S | 3 +-- > >> arch/x86/kvm/vmx/vmx.c | 5 +++++ > >> 3 files changed, 9 insertions(+), 6 deletions(-) > >> > >> diff --git a/arch/x86/include/asm/nospec-branch.h b/arch/x86/include/asm/nospec-branch.h > >> index c936ce9f0c47..e5723e024b47 100644 > >> --- a/arch/x86/include/asm/nospec-branch.h > >> +++ b/arch/x86/include/asm/nospec-branch.h > >> @@ -159,10 +159,9 @@ > >> * 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=ALT_NOT(X86_FEATURE_ALWAYS) > >> - ALTERNATIVE_2 "jmp .Lskip_rsb_\@", \ > >> - __stringify(__FILL_RETURN_BUFFER(\reg,\nr)), \ftr, \ > >> - __stringify(__FILL_ONE_RETURN), \ftr2 > >> +.macro FILL_RETURN_BUFFER reg:req nr:req ftr:req > >> + ALTERNATIVE "jmp .Lskip_rsb_\@", \ > >> + __stringify(__FILL_RETURN_BUFFER(\reg,\nr)), \ftr > >> > >> .Lskip_rsb_\@: > >> .endm > >> diff --git a/arch/x86/kvm/vmx/vmenter.S b/arch/x86/kvm/vmx/vmenter.S > >> index 6de96b943804..eb82797bd7bf 100644 > >> --- a/arch/x86/kvm/vmx/vmenter.S > >> +++ b/arch/x86/kvm/vmx/vmenter.S > >> @@ -231,8 +231,7 @@ SYM_INNER_LABEL(vmx_vmexit, SYM_L_GLOBAL) > >> * single call to retire, before the first unbalanced RET. > >> */ > >> > >> - FILL_RETURN_BUFFER %_ASM_CX, RSB_CLEAR_LOOPS, X86_FEATURE_RSB_VMEXIT,\ > >> - X86_FEATURE_RSB_VMEXIT_LITE > >> + FILL_RETURN_BUFFER %_ASM_CX, RSB_CLEAR_LOOPS, X86_FEATURE_RSB_VMEXIT > >> > >> > >> pop %_ASM_ARG2 /* @flags */ > >> diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c > >> index c9b49a09e6b5..fdcd8e10c2ab 100644 > >> --- a/arch/x86/kvm/vmx/vmx.c > >> +++ b/arch/x86/kvm/vmx/vmx.c > >> @@ -7049,8 +7049,13 @@ void noinstr vmx_spec_ctrl_restore_host(struct vcpu_vmx *vmx, > >> * For legacy IBRS, the IBRS bit always needs to be written after > >> * transitioning from a less privileged predictor mode, regardless of > >> * whether the guest/host values differ. > >> + * > >> + * For eIBRS affected by Post Barrier RSB Predictions a serialising > >> + * instruction (wrmsr) must be executed to ensure a call instruction has > >> + * retired before the prediction of a following unbalanced ret. > >> */ > >> if (cpu_feature_enabled(X86_FEATURE_KERNEL_IBRS) || > >> + cpu_feature_enabled(X86_FEATURE_RSB_VMEXIT_LITE) || > >> vmx->spec_ctrl != hostval) > >> native_wrmsrl(MSR_IA32_SPEC_CTRL, hostval); > > > > Better, I think, would be to leave the condition alone and put an > > LFENCE on the 'else' path: > > > > if (cpu_feature_enabled(X86_FEATURE_KERNEL_IBRS) || > > vmx->spec_ctrl != hostval) > > native_wrmsrl(MSR_IA32_SPEC_CTRL, hostval); > > else > > rmb(); > > > > When the guest and host have different IA32_SPEC_CTRL values, you get > > the serialization from the WRMSR. Otherwise, you get it from the > > cheaper LFENCE. > In this case systems that don't suffer from PBRSB (i.e. don've have > X86_FEATURE_RSB_VMEXIT_LITE set) would be doing a barrier for no reason. We're > just trading performance on vulnerable systems for a performance hit on systems > that aren't vulnerable. The lfence could be buried in an ALTERNATIVE keyed to X86_FEATURE_RSB_VMEXIT_LITE. > > This is still more convoluted than having the mitigation in one place. > Agreed. For a mere 18 cycles, it doesn't really seem worth the obfuscation.