Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp2763371pxu; Mon, 7 Dec 2020 15:17:12 -0800 (PST) X-Google-Smtp-Source: ABdhPJwIfUm/KTh04c2luB6DWWaZryS6GGXXqDa/XdhVBoq29cwFJ8d0/4kKseZOWJWGE2gvO6KD X-Received: by 2002:a17:906:a0c3:: with SMTP id bh3mr21143221ejb.497.1607383032381; Mon, 07 Dec 2020 15:17:12 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1607383032; cv=none; d=google.com; s=arc-20160816; b=aLS+4WvDFVoFwUtN3ILWs5fQOTMPMGAZMjTwD6kZLtI33kYDgHUUGUMf4muDkPgmZR 3j3thxwlh5qJC/mqm8l3a/+RShpve2TpNzG69twfAZ87PDJRg7KA9UZRNfvYLtCvPLZ5 nahFJFP8y5ghHFvlX/c9215FU7wVdkQ1P/2oy4C+fGUsVpg2Ans+II4A37HA0subgik3 kLGAgE3CAqCv13uXzD+nOFrWht0m65T45wymXJeK0zsIv/62gyH6gmnSOzpJRJI4TWDX RfiE6gdSbfpjkduSHqSZjiGB2FVoUjKjM4D8euM27WB4nG1mg67T4WjHHdQuM2kYotXH dUMg== 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-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=Gbbh4ibxUFpi3OAKBtYtSpm5Z1JaNbGkY8hc3Ov1OeA=; b=noEZPZjTDJiAHkW3t/s+geYILyPti2OwBVT5PiLe+j78PVabK5aiCuf+LzQgWxldri c1xElq0In8vaTczx7nE+JSncz19wNCeqN1pjnYmGqpPbnIwEsmXWSc2resBRlkzMae3S 7aw9i/iIyJM/OLJcGR6jtVocGAKCgOQrOGTUUICYbt+3qUaXLSn/oOmgwXB5SXrAZcl1 +EKYuXf1dNFxmmqaTsUKLFACfEsDX0+jT1GfMVLuRIZDOMAEZDJrRGhIXR+7LSJTHbdL ab1h33RpVPnmtnWORetTCYQzua6BjsgUFGF+A2deojfpH1spl+xGsbjcwYUCckbUnqxn CVFw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=rQuROwyc; 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 dc28si9171387edb.219.2020.12.07.15.16.49; Mon, 07 Dec 2020 15:17:12 -0800 (PST) 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=rQuROwyc; 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 S1728155AbgLGXOE (ORCPT + 99 others); Mon, 7 Dec 2020 18:14:04 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37442 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725917AbgLGXOE (ORCPT ); Mon, 7 Dec 2020 18:14:04 -0500 Received: from mail-pg1-x541.google.com (mail-pg1-x541.google.com [IPv6:2607:f8b0:4864:20::541]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 75A94C061793 for ; Mon, 7 Dec 2020 15:13:23 -0800 (PST) Received: by mail-pg1-x541.google.com with SMTP id o5so10395219pgm.10 for ; Mon, 07 Dec 2020 15:13:23 -0800 (PST) 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:content-transfer-encoding:in-reply-to; bh=Gbbh4ibxUFpi3OAKBtYtSpm5Z1JaNbGkY8hc3Ov1OeA=; b=rQuROwycmhYuaF7TU7X0pJ/7xgAKppbha0BqNqI/sLgV4WGQtuvMyx/qz4/M6diqaM 4yf4lx3yMt5uuRfdfcWpaRMCphkexOi4D552m/ygiWSe6uLCLJxxpRtLSxWw0NXKf6MQ I54+XaVd9b0lekf+QJbUGZtHpZ82agD2LA9P+yl19No7qjTNsXYYXMfeSa+xaU8mMFEi uLrDoqpU20YTcJTGOaA1cj2Op/n7hQuB+LEPk1gLvyx8DoeLtoiy2GNrITZQoJeo5jUB IaDgnyi+LRWPnIpDRD7nUL3A1fFkN71X5vkdgCoq2/zAz32iDISvdGw7DTfMZxG1dzG1 IEKQ== 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:content-transfer-encoding :in-reply-to; bh=Gbbh4ibxUFpi3OAKBtYtSpm5Z1JaNbGkY8hc3Ov1OeA=; b=eIjzD+bQzbVhqQcUx/fpSxa0VQvryQyjwVsxIYIhoRXqzMDljCsqQ24oaIBbm2Vfte HwHiO+uhC21i9pXNbxMrzVCrbLYw1wlIDfty7PjUAL83UMhk7oDxVbr3b9gXIlcx62RI NXLpe+cNoBlGOPdztwPR5I2BtIPPsMAW9cuvu4TlFf8VgBvpYaFHSMVxBiXZJ59ZoZop Lmweh5SWep1p6cWcX/fpPCpS1MjIJHPXxkPaYLK5b9nK4adfDZWfOeJDeId7FMJoZRDh BN3Eh2512fx0yvrZX2mlSK94B3Z6rQCNQreMdiUJVz+C8tGNUOGw69OVkGphoaVUdnzC vf/w== X-Gm-Message-State: AOAM533FKg6w3YH8LVHeAV5DGZMqAa8I6JxlRTOInUHRppI5bV0d9Fub uTGMQpRn3gzcAobTPppzeRZ/CA== X-Received: by 2002:a17:902:6b45:b029:d6:c43e:ad13 with SMTP id g5-20020a1709026b45b02900d6c43ead13mr18461110plt.77.1607382802822; Mon, 07 Dec 2020 15:13:22 -0800 (PST) Received: from google.com ([2620:15c:f:10:1ea0:b8ff:fe73:50f5]) by smtp.gmail.com with ESMTPSA id c3sm13598807pgm.41.2020.12.07.15.13.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Dec 2020 15:13:22 -0800 (PST) Date: Mon, 7 Dec 2020 15:13:15 -0800 From: Sean Christopherson To: Babu Moger Cc: pbonzini@redhat.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, fenghua.yu@intel.com, tony.luck@intel.com, wanpengli@tencent.com, kvm@vger.kernel.org, thomas.lendacky@amd.com, peterz@infradead.org, joro@8bytes.org, x86@kernel.org, kyung.min.park@intel.com, linux-kernel@vger.kernel.org, krish.sadhukhan@oracle.com, hpa@zytor.com, mgross@linux.intel.com, vkuznets@redhat.com, kim.phillips@amd.com, wei.huang2@amd.com, jmattson@google.com Subject: Re: [PATCH 2/2] KVM: SVM: Add support for Virtual SPEC_CTRL Message-ID: References: <160738054169.28590.5171339079028237631.stgit@bmoger-ubuntu> <160738067970.28590.1275116532320186155.stgit@bmoger-ubuntu> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <160738067970.28590.1275116532320186155.stgit@bmoger-ubuntu> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Dec 07, 2020, Babu Moger wrote: > Newer AMD processors have a feature to virtualize the use of the > SPEC_CTRL MSR. When supported, the SPEC_CTRL MSR is automatically > virtualized and no longer requires hypervisor intervention. Hrm, is MSR_AMD64_VIRT_SPEC_CTRL only for SSBD? Should that MSR be renamed to avoid confusion with the new form of VIRT_SPEC_CTRL? > This feature is detected via CPUID function 0x8000000A_EDX[20]: > GuestSpecCtrl. > > Hypervisors are not required to enable this feature since it is > automatically enabled on processors that support it. > > When this feature is enabled, the hypervisor no longer has to > intercept the usage of the SPEC_CTRL MSR and no longer is required to > save and restore the guest SPEC_CTRL setting when switching > hypervisor/guest modes. Well, it's still required if the hypervisor wanted to allow the guest to turn off mitigations that are enabled in the host. I'd omit this entirely and focus on what hardware does and how Linux/KVM utilize the new feature. > The effective SPEC_CTRL setting is the guest SPEC_CTRL setting or'ed with the > hypervisor SPEC_CTRL setting. This line needs to be higher in the changelog, it's easily the most relevant info for understanding the mechanics. Please also explicitly state the context switching mechanics, e.g. is it tracked in the VMCB, loaded on VMRUN, saved on VM-Exit, etc... > This allows the hypervisor to ensure a minimum SPEC_CTRL if desired. > > This support also fixes an issue where a guest may sometimes see an > inconsistent value for the SPEC_CTRL MSR on processors that support > this feature. With the current SPEC_CTRL support, the first write to > SPEC_CTRL is intercepted and the virtualized version of the SPEC_CTRL > MSR is not updated. When the guest reads back the SPEC_CTRL MSR, it > will be 0x0, instead of the actual expected value. There isn’t a > security concern here, because the host SPEC_CTRL value is or’ed with > the Guest SPEC_CTRL value to generate the effective SPEC_CTRL value. > KVM writes with the guest's virtualized SPEC_CTRL value to SPEC_CTRL > MSR just before the VMRUN, so it will always have the actual value > even though it doesn’t appear that way in the guest. The guest will > only see the proper value for the SPEC_CTRL register if the guest was > to write to the SPEC_CTRL register again. With Virtual SPEC_CTRL > support, the MSR interception of SPEC_CTRL is disabled during > vmcb_init, so this will no longer be an issue. > > Signed-off-by: Babu Moger > --- > arch/x86/kvm/svm/svm.c | 17 ++++++++++++++--- > 1 file changed, 14 insertions(+), 3 deletions(-) > > diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c > index 79b3a564f1c9..3d73ec0cdb87 100644 > --- a/arch/x86/kvm/svm/svm.c > +++ b/arch/x86/kvm/svm/svm.c > @@ -1230,6 +1230,14 @@ static void init_vmcb(struct vcpu_svm *svm) > > svm_check_invpcid(svm); > > + /* > + * If the host supports V_SPEC_CTRL then disable the interception > + * of MSR_IA32_SPEC_CTRL. > + */ > + if (boot_cpu_has(X86_FEATURE_V_SPEC_CTRL)) > + set_msr_interception(&svm->vcpu, svm->msrpm, MSR_IA32_SPEC_CTRL, > + 1, 1); > + > if (kvm_vcpu_apicv_active(&svm->vcpu)) > avic_init_vmcb(svm); > > @@ -3590,7 +3598,8 @@ static __no_kcsan fastpath_t svm_vcpu_run(struct kvm_vcpu *vcpu) > * is no need to worry about the conditional branch over the wrmsr > * being speculatively taken. > */ > - x86_spec_ctrl_set_guest(svm->spec_ctrl, svm->virt_spec_ctrl); > + if (!static_cpu_has(X86_FEATURE_V_SPEC_CTRL)) > + x86_spec_ctrl_set_guest(svm->spec_ctrl, svm->virt_spec_ctrl); > > svm_vcpu_enter_exit(vcpu, svm); > > @@ -3609,12 +3618,14 @@ static __no_kcsan fastpath_t svm_vcpu_run(struct kvm_vcpu *vcpu) > * If the L02 MSR bitmap does not intercept the MSR, then we need to > * save it. > */ > - if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL))) > + if (!static_cpu_has(X86_FEATURE_V_SPEC_CTRL) && > + unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL))) This will break migration, or maybe just cause wierdness, as userspace will always see '0' when reading SPEC_CTRL and its writes will be ignored. Is there a VMCB field that holds the guest's value? If so, this read can be skipped, and instead the MSR set/get flows probably need to poke into the VMCB. > svm->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL); > > reload_tss(vcpu); > > - x86_spec_ctrl_restore_host(svm->spec_ctrl, svm->virt_spec_ctrl); > + if (!static_cpu_has(X86_FEATURE_V_SPEC_CTRL)) > + x86_spec_ctrl_restore_host(svm->spec_ctrl, svm->virt_spec_ctrl); > > vcpu->arch.cr2 = svm->vmcb->save.cr2; > vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax; >