Received: by 2002:ac0:bc90:0:0:0:0:0 with SMTP id a16csp1429620img; Tue, 19 Mar 2019 07:29:54 -0700 (PDT) X-Google-Smtp-Source: APXvYqztraL3kLMNiMjHqjgsDu01qnoyJqPFbLD79lhuku4vy+pTOAj2+mjEpYIEqvaUkHDBHdLV X-Received: by 2002:aa7:918b:: with SMTP id x11mr2333433pfa.228.1553005794005; Tue, 19 Mar 2019 07:29:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1553005793; cv=none; d=google.com; s=arc-20160816; b=pt3mrzv5NxGYnGMK2YKJQcDdIzl8M/bCDk9RKSxcUvRmXx0dBvA/dCOPkqBJ//WCa7 wA6z/tOZ1dFVcuKdfMawKbo/YlN0JEifo8AjCvcIK0ySJ1+jjZdyK7EkIyL4s8xYgte1 uHGTWHggFg/MuXQthWTikkMUWcwawSEa4YeX5ihCDuExue5Jop6CxQze34Y6Zfog4CYo Q0b0t5IhoXW+3+CVztXSEDJstMmqOtpmNeTui9IxwRhVJy2G26RvRDc8MYuAkEN7AjdO XMtVrTflJvNg74Q0g4DxXwl+SFQ18sRH85t7YviVWQtTNEsXSkY697ig3GiG0+VnaBU2 nbtw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=+TXdMKmZc1DadkuND7Yt6f6r+8/SIt/bPm+3MioFaPY=; b=DzGFwP06i1r98m0E9VYbmlCZnADpf3qK7Iprvrx90L4INqF1JtwkITfWHEzU7zQ6OA 3eq3vYHBVzmmAeFjFjaKlhxQaAL733/dWO8v/Y3ThxtRkJKUAp9LW0VHmb5ZJMXEwatF wMPf+FSJAgDQAoIjjpOhplsXtkEN680Rf1SGh1sf4LGIc5wLUvaPlITDh1OMPvy7yliL PvVyxbwm+qzTNPa53p/HXH2sk6AFE4jO/a377ySATVZE5DbsYLZCeLMWyO6DgMvzFig7 gIbQb+uSqBg0QG5rL3KMu5JtSj3dfsSMmOE3oiaYU7Bn9Drvmv1y6NRmyaDUFe6gBJiQ vbrQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 73si12578251pld.156.2019.03.19.07.29.38; Tue, 19 Mar 2019 07:29:53 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727664AbfCSO2v (ORCPT + 99 others); Tue, 19 Mar 2019 10:28:51 -0400 Received: from mga03.intel.com ([134.134.136.65]:45138 "EHLO mga03.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726464AbfCSO2u (ORCPT ); Tue, 19 Mar 2019 10:28:50 -0400 X-Amp-Result: UNSCANNABLE X-Amp-File-Uploaded: False Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by orsmga103.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 19 Mar 2019 07:28:49 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.58,498,1544515200"; d="scan'208";a="132883414" Received: from sjchrist-coffee.jf.intel.com (HELO linux.intel.com) ([10.54.74.181]) by fmsmga008.fm.intel.com with ESMTP; 19 Mar 2019 07:28:49 -0700 Date: Tue, 19 Mar 2019 07:28:49 -0700 From: Sean Christopherson To: Xiaoyao Li Cc: kvm@vger.kernel.org, Paolo Bonzini , Radim =?utf-8?B?S3LEjW3DocWZ?= , Thomas Gleixner , Ingo Molnar , Borislav Petkov , "H. Peter Anvin" , linux-kernel@vger.kernel.org, chao.gao@intel.com Subject: Re: [PATCH v2 2/2] kvm/vmx: Using hardware cpuid faulting to avoid emulation overhead Message-ID: <20190319142849.GB25575@linux.intel.com> References: <20190318114324.14198-1-xiaoyao.li@linux.intel.com> <20190318114324.14198-3-xiaoyao.li@linux.intel.com> <20190318163832.GB13528@linux.intel.com> <085cc389df4d734ee173bb4c199776591d197a21.camel@linux.intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <085cc389df4d734ee173bb4c199776591d197a21.camel@linux.intel.com> User-Agent: Mutt/1.5.24 (2015-08-30) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Mar 19, 2019 at 12:37:23PM +0800, Xiaoyao Li wrote: > On Mon, 2019-03-18 at 09:38 -0700, Sean Christopherson wrote: > > On Mon, Mar 18, 2019 at 07:43:24PM +0800, Xiaoyao Li wrote: > > > Current cpuid faulting of guest is purely emulated in kvm, which exploits > > > CPUID vm exit to inject #GP to guest. However, if host hardware cpu has > > > X86_FEATURE_CPUID_FAULT, we can just use the hardware cpuid faulting for > > > guest to avoid the vm exit overhead. > > > > Heh, I obviously didn't look at this patch before responding to patch 1/2. > > > > > Note: cpuid faulting takes higher priority over CPUID instruction vm > > > exit (Intel SDM vol3.25.1.1). > > > > > > Since cpuid faulting only exists on some Intel's cpu, just apply this > > > optimization to vmx. > > > > > > Signed-off-by: Xiaoyao Li > > > --- > > > arch/x86/include/asm/kvm_host.h | 2 ++ > > > arch/x86/kvm/vmx/vmx.c | 19 +++++++++++++++---- > > > arch/x86/kvm/x86.c | 15 ++++++++++++--- > > > 3 files changed, 29 insertions(+), 7 deletions(-) > > > > > > diff --git a/arch/x86/include/asm/kvm_host.h > > > b/arch/x86/include/asm/kvm_host.h > > > index ce79d7bfe1fd..14cad587b804 100644 > > > --- a/arch/x86/include/asm/kvm_host.h > > > +++ b/arch/x86/include/asm/kvm_host.h > > > @@ -1339,6 +1339,8 @@ void kvm_lmsw(struct kvm_vcpu *vcpu, unsigned long > > > msw); > > > void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l); > > > int kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr); > > > > > > +int kvm_supported_msr_misc_features_enables(struct kvm_vcpu *vcpu, u64 > > > data); > > > + > > > int kvm_get_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr); > > > int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr); > > > > > > diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c > > > index 2c59e0209e36..6b413e471dca 100644 > > > --- a/arch/x86/kvm/vmx/vmx.c > > > +++ b/arch/x86/kvm/vmx/vmx.c > > > @@ -1037,7 +1037,7 @@ static void pt_guest_exit(struct vcpu_vmx *vmx) > > > > > > static void vmx_save_host_cpuid_fault(struct vcpu_vmx *vmx) > > > { > > > - u64 host_val; > > > + u64 host_val, guest_val; > > > > > > if (!boot_cpu_has(X86_FEATURE_CPUID_FAULT)) > > > return; > > > @@ -1045,10 +1045,12 @@ static void vmx_save_host_cpuid_fault(struct > > > vcpu_vmx *vmx) > > > rdmsrl(MSR_MISC_FEATURES_ENABLES, host_val); > > > vmx->host_msr_misc_features_enables = host_val; > > > > > > - /* clear cpuid fault bit to avoid it leak to guest */ > > > - if (host_val & MSR_MISC_FEATURES_ENABLES_CPUID_FAULT) { > > > + guest_val = vmx->vcpu.arch.msr_misc_features_enables; > > > + > > > + /* we can use the hardware cpuid faulting to avoid emulation overhead */ > > > + if ((host_val ^ guest_val) & MSR_MISC_FEATURES_ENABLES_CPUID_FAULT) { > > > wrmsrl(MSR_MISC_FEATURES_ENABLES, > > > - host_val & ~MSR_MISC_FEATURES_ENABLES_CPUID_FAULT); > > > + host_val ^ MSR_MISC_FEATURES_ENABLES_CPUID_FAULT); > > > } > > > } > > > > > > @@ -2057,6 +2059,15 @@ static int vmx_set_msr(struct kvm_vcpu *vcpu, struct > > > msr_data *msr_info) > > > else > > > vmx->pt_desc.guest.addr_a[index / 2] = data; > > > break; > > > + case MSR_MISC_FEATURES_ENABLES: > > > + if (!kvm_supported_msr_misc_features_enables(vcpu, data)) > > > + return 1; > > > + if (boot_cpu_has(X86_FEATURE_CPUID_FAULT)) { > > > + if (vmx->loaded_cpu_state) > > > > No need for two separate if statements. And assuming we're checking the > > existing shadow value when loading guest/host state, the WRMSR should > > only be done if the host's value is non-zero. > > I'll combine these two if statements into one. > > I cannot understand why the WRMSR should only be done if the host's value is > non-zero. I think there is no depedency with host's value, if using the hardware > cpuid faulting. We just need to set the value to real hardware MSR. What I was trying to say in patch 1/2 regarding save/restore, is that I don't think it is worthwhile to voluntarily switch hardware's value. In other words, do the WRMSR if and only if it's absolutely necessary. And that means only installing the guest's value if the host's value is non-zero and host_val != guest_val. If the host's value is zero, then the guest's value is irrelevant as CPUID faulting behavior will naturally be taken care of when intercepting CPUID. And for obvious reasons the WRMSR can be skipped if host and guest want the same value. As it pertains to this code, we should not modify any hardware value that isn't saved/restored. If we go with the above approach, then we're only switching the hardware MSR when the host's value is non-zero. Ergo this code should not write the actual MSR if the host value is zero as the MSR will not be restored to the host's value when putting the vCPU. > > > + wrmsrl(MSR_MISC_FEATURES_ENABLES, data); > > > + } > > > + vcpu->arch.msr_misc_features_enables = data; > > > + break; > > > case MSR_TSC_AUX: > > > if (!msr_info->host_initiated && > > > !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP)) > > > diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c > > > index 434ec113cc79..33a8c95b2f2e 100644 > > > --- a/arch/x86/kvm/x86.c > > > +++ b/arch/x86/kvm/x86.c > > > @@ -2449,6 +2449,17 @@ static void record_steal_time(struct kvm_vcpu *vcpu) > > > &vcpu->arch.st.steal, sizeof(struct kvm_steal_time)); > > > } > > > > > > +int kvm_supported_msr_misc_features_enables(struct kvm_vcpu *vcpu, u64 > > > data) > > > +{ > > > + if (data & ~MSR_MISC_FEATURES_ENABLES_CPUID_FAULT || > > > + (data & MSR_MISC_FEATURES_ENABLES_CPUID_FAULT && > > > + !supports_cpuid_fault(vcpu))) > > > + return 0; > > > + else > > > + return 1; > > > +} > > > +EXPORT_SYMBOL_GPL(kvm_supported_msr_misc_features_enables); > > > + > > > int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info) > > > { > > > bool pr = false; > > > @@ -2669,9 +2680,7 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct > > > msr_data *msr_info) > > > vcpu->arch.msr_platform_info = data; > > > break; > > > case MSR_MISC_FEATURES_ENABLES: > > > - if (data & ~MSR_MISC_FEATURES_ENABLES_CPUID_FAULT || > > > - (data & MSR_MISC_FEATURES_ENABLES_CPUID_FAULT && > > > - !supports_cpuid_fault(vcpu))) > > > + if (!kvm_supported_msr_misc_features_enables(vcpu, data)) > > > return 1; > > > vcpu->arch.msr_misc_features_enables = data; > > > break; > > > -- > > > 2.19.1 > > > >