Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp397745pxj; Fri, 7 May 2021 11:00:02 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxZE2M7cNp6VPAQ3lsOROZO50Bht1hcvWDcHXhCV2d6JDV6OH8Bs1HwuajDOQuZ89Lh3unJ X-Received: by 2002:a63:2206:: with SMTP id i6mr11435413pgi.265.1620410401886; Fri, 07 May 2021 11:00:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620410401; cv=none; d=google.com; s=arc-20160816; b=GCgSP7e0sNiCZKYa5yM6RQwUyt8qvZm29548lbhW3d2CTm1ajvHE5Yyy07BvU+jmuy dtt5rlNUvFhtdxC4t0+hO+/C8jZwXJ6bAi/e4N8Woo9hKTMuVEKudBCYXDbsE2PAxUmc qIChwvFkczQ6hFtoAr41u+YA38jk4JZORHkrsM+5xJmHpTtwDTmuFU0jOMxrpO1bZOTV 3hENkV/NNr/UCy//HIdalK+3ASBzRAwM1YEnCN9J8cxxMv7yu9TnnlJpKBUjrg95AWbp ISllTqv+o2sydS/hglPE2em+Yc42VageCXV0hAPbeTWLHhy/7RMzD87ANVYUrA4qrC80 E9PA== 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=7b05HQwQFJbiC5hEzCCygGe/UudPuvMm6AH3d8lkYuo=; b=AtuuZAQ2JV/3JYD+S8e5qN6wPqY4clfEYeuPZDSgWiqHNI3V3Br7lvq/KOnu4tqwYE QmRQiFLsCEab5HPbAoXjRBoMGzkQv0jw4Cb/6KOD234bNTj5v/6Tj5PH25DbIfscatFy TAoVN82qbwB7CqdcVdahuJSU6P2oF5np7bmA0EeTmeZpBg/LpYMm+Gvv5N0kRk27nwD0 JIFipRazjH6/YXJ1uDVI1aUS8EgIupydL8UwqObCyMieNvEDDIbdYlfl3fcWaNXPydWJ rUF55KBaHbdvgX3eYRBFG0ReRkPWH5nTv4nE/qoEglb0CAW1kbJ2QRQOgCM9+RW0BZaE LwJg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=KYOZ2sJK; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id i9si7835777pjs.67.2021.05.07.10.59.49; Fri, 07 May 2021 11:00:01 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=KYOZ2sJK; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S229524AbhEGR7R (ORCPT + 99 others); Fri, 7 May 2021 13:59:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46490 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229476AbhEGR7R (ORCPT ); Fri, 7 May 2021 13:59:17 -0400 Received: from mail-pl1-x62d.google.com (mail-pl1-x62d.google.com [IPv6:2607:f8b0:4864:20::62d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DAA02C061574 for ; Fri, 7 May 2021 10:58:14 -0700 (PDT) Received: by mail-pl1-x62d.google.com with SMTP id p17so5498785plf.12 for ; Fri, 07 May 2021 10:58:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=7b05HQwQFJbiC5hEzCCygGe/UudPuvMm6AH3d8lkYuo=; b=KYOZ2sJKADhfuZ/uHMMrB7Yz4pjkf+WtoJEvVJeLQAI9RAcsyiddelzeYpgeq/WBRf aYbEM6iM2YzK07SC+oosCVwJBCI1UdqnphuM+V9hK8PeM5SJyF3N1k9UbJSaoW0Iqd+G SXr8hLPVtRDKqwh8waGJhlnzYj+7ZWnb45KzLXh4dDbIxRKVegBZXC9njBPFtj6qOHSF oHCkSGUDqH3uc4QH4u+yj95GJanLrZdcpWxl4Irl0QPl4rBsU48qszWvPraQRjub2pLL iMPaWawIKjMH7c9YyQocMUFO8/Po/Z4fmRlR+s9laHvYUbfUQkyUdvXfcqN/6AHFnBe5 xWdA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=7b05HQwQFJbiC5hEzCCygGe/UudPuvMm6AH3d8lkYuo=; b=F9iBpDkLYgbTtenuBs7yz8VnIaoDXKPX7x7Y+eroyC/TwbuYmf0VZE9glcEm83JIQb Pge5JInx3jRkIli4qsm29rozIjbc5+z+KZgy3IdLNEYQURIGd3fXoMRpUfC+HdGYb6S3 QlEybM2jO4rY4xxOGwMjl8IKQjzzHzYmdyI0zw3+7FQlD/KelxORggDTt3mQcZtTPWbq LuHqcqYsjUgl+L2M8hDTdg925L+NzZpBC0RIytLOs7v4VlgnOWdAAfW73RprrlZ0F1PX 1UDBDUyLMedMe7tZujdZ8rB7wefWuMaTyLpYIDYDrhC2N/MdeRQkUx3SdMmc826sy3g0 ID/w== X-Gm-Message-State: AOAM53106NzfWuUWb8RHJL3fj7kx6MD4PZ7+l6aESb4CVyP/kCGQxxe5 aShDBHgTnRI9VQWpX9NP7uvFWg== X-Received: by 2002:a17:902:b20a:b029:ef:463:365a with SMTP id t10-20020a170902b20ab02900ef0463365amr5403349plr.17.1620410294246; Fri, 07 May 2021 10:58:14 -0700 (PDT) Received: from google.com (240.111.247.35.bc.googleusercontent.com. [35.247.111.240]) by smtp.gmail.com with ESMTPSA id q7sm3220941pfq.172.2021.05.07.10.58.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 May 2021 10:58:13 -0700 (PDT) Date: Fri, 7 May 2021 17:58:09 +0000 From: Sean Christopherson To: Venkatesh Srinivas Cc: Jon Kohler , Paolo Bonzini , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , Thomas Gleixner , Ingo Molnar , Borislav Petkov , x86@kernel.org, "H. Peter Anvin" , kvm@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] KVM: x86: use X86_FEATURE_RSB_CTXSW for RSB stuffing in vmexit Message-ID: References: <20210507150636.94389-1-jon@nutanix.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, May 07, 2021, Venkatesh Srinivas wrote: > On Fri, May 7, 2021 at 8:08 AM Jon Kohler wrote: > > > > cpufeatures.h defines X86_FEATURE_RSB_CTXSW as "Fill RSB on context > > switches" which seems more accurate than using X86_FEATURE_RETPOLINE > > in the vmxexit path for RSB stuffing. > > > > X86_FEATURE_RSB_CTXSW is used for FILL_RETURN_BUFFER in > > arch/x86/entry/entry_{32|64}.S. This change makes KVM vmx and svm > > follow that same pattern. This pairs up nicely with the language in > > bugs.c, where this cpu_cap is enabled, which indicates that RSB > > stuffing should be unconditional with spectrev2 enabled. > > /* > > * If spectre v2 protection has been enabled, unconditionally fill > > * RSB during a context switch; this protects against two independent > > * issues: > > * > > * - RSB underflow (and switch to BTB) on Skylake+ > > * - SpectreRSB variant of spectre v2 on X86_BUG_SPECTRE_V2 CPUs > > */ > > setup_force_cpu_cap(X86_FEATURE_RSB_CTXSW); > > > > Furthermore, on X86_FEATURE_IBRS_ENHANCED CPUs && SPECTRE_V2_CMD_AUTO, > > we're bypassing setting X86_FEATURE_RETPOLINE, where as far as I could > > find, we should still be doing RSB stuffing no matter what when > > CONFIG_RETPOLINE is enabled and spectrev2 is set to auto. > > If I'm reading https://software.intel.com/security-software-guidance/deep-dives/deep-dive-indirect-branch-restricted-speculation > correctly, I don't think an RSB fill sequence is required on VMExit on > processors w/ Enhanced IBRS. Specifically: > """ > On processors with enhanced IBRS, an RSB overwrite sequence may not > suffice to prevent the predicted target of a near return from using an > RSB entry created in a less privileged predictor mode. Software can > prevent this by enabling SMEP (for transitions from user mode to > supervisor mode) and by having IA32_SPEC_CTRL.IBRS set during VM exits > """ > On Enhanced IBRS processors, it looks like SPEC_CTRL.IBRS is set > across all #VMExits via x86_virt_spec_ctrl in kvm. > > So is this patch needed? Venkatesh belatedly pointed out (off list) that KVM VMX stops intercepting MSR_IA32_SPEC_CTRL after the first (successful) write by the guest. But, I believe that's a non-issue for ENHANCED_IBRS because of this blurb in Intel's documentation[*]: Processors with enhanced IBRS still support the usage model where IBRS is set only in the OS/VMM for OSes that enable SMEP. To do this, such processors will ensure that guest behavior cannot control the RSB after a VM exit once IBRS is set, even if IBRS was not set at the time of the VM exit. The code and changelog for commit 706d51681d63 ("x86/speculation: Support Enhanced IBRS on future CPUs") is more than a little confusing: spectre_v2_select_mitigation(): if (boot_cpu_has(X86_FEATURE_IBRS_ENHANCED)) { mode = SPECTRE_V2_IBRS_ENHANCED; /* Force it so VMEXIT will restore correctly */ x86_spec_ctrl_base |= SPEC_CTRL_IBRS; wrmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base); goto specv2_set_mode; } changelog: Kernel also has to make sure that IBRS bit remains set after VMEXIT because the guest might have cleared the bit. This is already covered by the existing x86_spec_ctrl_set_guest() and x86_spec_ctrl_restore_host() speculation control functions. but I _think_ that is simply saying that MSR_IA32_SPEC_CTRL.IBRS needs to be restored in order to keep the mitigations active in the host. I don't think it contradicts the documentation that says VM-Exit is automagically mitigated if IBRS has _ever_ been set. [*] https://software.intel.com/security-software-guidance/deep-dives/deep-dive-indirect-branch-restricted-speculation