Received: by 2002:ac0:950c:0:0:0:0:0 with SMTP id f12csp3746634imc; Thu, 14 Mar 2019 04:29:35 -0700 (PDT) X-Google-Smtp-Source: APXvYqwu7TisMz2UaMcaZfrBrG85RPRTlLddHq0Qdl8mntva9wEcRCB8M1D6I2bUeuC1l0lKMZY6 X-Received: by 2002:a63:4e1d:: with SMTP id c29mr45292376pgb.433.1552562974909; Thu, 14 Mar 2019 04:29:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1552562974; cv=none; d=google.com; s=arc-20160816; b=K60Lfo1MRUKRSONOeHR0snrRbzzEm2jIujENlvVzdGRyA7UL9Cx3IBcrQ7pulQIbcE kQJch5IYNsGofsJGNrZBb1Tj0KuBQhbIjHPtHaIPCprr87vY364kBEjwxTNExcIb8xXy pg/JCd4VmBcIdLaAKB0yHUi5qAtjQeAG52cvhftv5r9QRMqCX8VpkC6DzoiC+Kh+mBCL J08TWrhniEhrxqVhWEK4t9uM6Rn4GIShhwo7YCySrnspjkQBSzV4ziy2vs8Pz7bEH/gZ aL6gItSYKNnNd96JCcoAJYJBP82GCYOCmRoVVuWTlMkf3u/VcIotRHEDqlJROn98HMZl 5TEg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:autocrypt:openpgp:from:references:cc:to:subject; bh=LonAxATBCU8Ez3OZH+bUED8E7Ccs5gOEKlHasAsv0Qo=; b=zJOyyc+NoCYnNomJOon//BZw+YpbpkxhsZVoMgLqv8rmXdyJY97nHGdjKXzHlPEFwL RjKgQMlCz6LBW4BTKnQVWRyXhiRMApbP0+tp4MlphNg4MRQ+gYc8GiZbdJJYrcqh+9Hd Em9FFJvhWH7AbHB1VBGTngYoCZQjpSLaPhOQ9SiMkIetiM+8Ruya+c5eikBoO8uPSxFn Q0SfLVHmF1dukXQ7zeEIAD0h/cFzD857iZnOWB6n6h1d/fmAOuBFESB/+3nKfDYnylrl ajj6p8sdJFtntgHaWRUrY7uSCHRQe/j+gF2z+TwPiSQdNCTF3m9ea4gRwX1DKmYAMxFG 9x2g== 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=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id bj1si1922393plb.15.2019.03.14.04.29.20; Thu, 14 Mar 2019 04:29:34 -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=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727496AbfCNL23 (ORCPT + 99 others); Thu, 14 Mar 2019 07:28:29 -0400 Received: from mx1.redhat.com ([209.132.183.28]:37512 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727286AbfCNL22 (ORCPT ); Thu, 14 Mar 2019 07:28:28 -0400 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id F111B3EDB6; Thu, 14 Mar 2019 11:28:27 +0000 (UTC) Received: from [10.36.112.69] (ovpn-112-69.ams2.redhat.com [10.36.112.69]) by smtp.corp.redhat.com (Postfix) with ESMTPS id AFA48282E3; Thu, 14 Mar 2019 11:28:24 +0000 (UTC) Subject: Re: [PATCH] kvm/x86/vmx: switch MSR_MISC_FEATURES_ENABLES between host and guest To: Xiaoyao Li Cc: Kyle Huey , Chao Gao , =?UTF-8?B?UmFkaW0gS3LEjW3DocWZ?= , Thomas Gleixner , Ingo Molnar , Borislav Petkov , "H. Peter Anvin" , x86@kernel.org, kvm@vger.kernel.org, linux-kernel@vger.kernel.org References: <20190314063858.18292-1-xiaoyao.li@linux.intel.com> From: Paolo Bonzini Openpgp: preference=signencrypt Autocrypt: addr=pbonzini@redhat.com; prefer-encrypt=mutual; keydata= mQHhBFRCcBIBDqDGsz4K0zZun3jh+U6Z9wNGLKQ0kSFyjN38gMqU1SfP+TUNQepFHb/Gc0E2 CxXPkIBTvYY+ZPkoTh5xF9oS1jqI8iRLzouzF8yXs3QjQIZ2SfuCxSVwlV65jotcjD2FTN04 hVopm9llFijNZpVIOGUTqzM4U55sdsCcZUluWM6x4HSOdw5F5Utxfp1wOjD/v92Lrax0hjiX DResHSt48q+8FrZzY+AUbkUS+Jm34qjswdrgsC5uxeVcLkBgWLmov2kMaMROT0YmFY6A3m1S P/kXmHDXxhe23gKb3dgwxUTpENDBGcfEzrzilWueOeUWiOcWuFOed/C3SyijBx3Av/lbCsHU Vx6pMycNTdzU1BuAroB+Y3mNEuW56Yd44jlInzG2UOwt9XjjdKkJZ1g0P9dwptwLEgTEd3Fo UdhAQyRXGYO8oROiuh+RZ1lXp6AQ4ZjoyH8WLfTLf5g1EKCTc4C1sy1vQSdzIRu3rBIjAvnC tGZADei1IExLqB3uzXKzZ1BZ+Z8hnt2og9hb7H0y8diYfEk2w3R7wEr+Ehk5NQsT2MPI2QBd wEv1/Aj1DgUHZAHzG1QN9S8wNWQ6K9DqHZTBnI1hUlkp22zCSHK/6FwUCuYp1zcAEQEAAbQj UGFvbG8gQm9uemluaSA8cGJvbnppbmlAcmVkaGF0LmNvbT6JAg0EEwECACMFAlRCcBICGwMH CwkIBwMCAQYVCAIJCgsEFgIDAQIeAQIXgAAKCRB+FRAMzTZpsbceDp9IIN6BIA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bx2CRwkMHtOmzQiQ2tSLjKh/c HeyFH68xjeLcinR7jXMrHQK+UCEw6jqi1oeZzGvfmxarUmS0uRuffAb589AJW50kkQK9VD/9 QC2FJISSUDnRC0PawGSZDXhmvITJMdD4TjYrePYhSY4uuIV02v028TVAaYbIhxvDY0hUQE4r 8ZbGRLn52bEzaIPgl1p/adKfeOUeMReg/CkyzQpmyB1TSk8lDMxQzCYHXAzwnGi8WU9iuE1P 0wARAQABiQHzBBgBAgAJBQJUQnHuAhsMAAoJEH4VEAzNNmmxp1EOoJy0uZggJm7gZKeJ7iUp eX4eqUtqelUw6gU2daz2hE/jsxsTbC/w5piHmk1H1VWDKEM4bQBTuiJ0bfo55SWsUNN+c9hh IX+Y8LEe22izK3w7mRpvGcg+/ZRG4DEMHLP6JVsv5GMpoYwYOmHnplOzCXHvmdlW0i6SrMsB Dl9rw4AtIa6bRwWLim1lQ6EM3PWifPrWSUPrPcw4OLSwFk0CPqC4HYv/7ZnASVkR5EERFF3+ 6iaaVi5OgBd81F1TCvCX2BEyIDRZLJNvX3TOd5FEN+lIrl26xecz876SvcOb5SL5SKg9/rCB ufdPSjojkGFWGziHiFaYhbuI2E+NfWLJtd+ZvWAAV+O0d8vFFSvriy9enJ8kxJwhC0ECbSKF Y+W1eTIhMD3aeAKY90drozWEyHhENf4l/V+Ja5vOnW+gCDQkGt2Y1lJAPPSIqZKvHzGShdh8 DduC0U3xYkfbGAUvbxeepjgzp0uEnBXfPTy09JGpgWbg0w91GyfT/ujKaGd4vxG2Ei+MMNDm S1SMx7wu0evvQ5kT9NPzyq8R2GIhVSiAd2jioGuTjX6AZCFv3ToO53DliFMkVTecLptsXaes uUHgL9dKIfvpm+rNXRn9wAwGjk0X/A== Message-ID: Date: Thu, 14 Mar 2019 12:28:21 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.4.0 MIME-Version: 1.0 In-Reply-To: <20190314063858.18292-1-xiaoyao.li@linux.intel.com> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.39]); Thu, 14 Mar 2019 11:28:28 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 14/03/19 07:38, Xiaoyao Li wrote: > CPUID Faulting is a feature about CPUID instruction. When CPUID Faulting is > enabled, all execution of the CPUID instruction outside system-management > mode (SMM) cause a general-protection (#GP) if the CPL > 0. > > About this feature, detailed information can be found at > https://www.intel.com/content/dam/www/public/us/en/documents/application-notes/virtualization-technology-flexmigration-application-note.pdf > > There is an issue that current kvm doesn't switch the value of > MSR_MISC_FEATURES_ENABLES between host and guest. If MSR_MISC_FEATURES_ENABLES > exists on the hardware cpu, and host enables CPUID faulting (setting the bit 0 > of MSR_MISC_FEATURES_ENABLES), it will impact the guest's behavior because > cpuid faulting is enabled by host and passed to guest. > > From my tests, when host enables cpuid faulting, it causes guest boot failure > when guest uses *modprobe* to load modules. Below is the error log: > > [ 1.233556] traps: modprobe[71] general protection fault ip:7f0077f6495c sp:7ffda148d808 error:0 in ld-2.17.so[7f0077f4d000+22000] > [ 1.237780] traps: modprobe[73] general protection fault ip:7fad5aba095c sp:7ffd36067378 error:0 in ld-2.17.so[7fad5ab89000+22000] > [ 1.241930] traps: modprobe[75] general protection fault ip:7f3edb89495c sp:7fffa1a81308 error:0 in ld-2.17.so[7f3edb87d000+22000] > [ 1.245998] traps: modprobe[77] general protection fault ip:7f91d670895c sp:7ffc25fa7f38 error:0 in ld-2.17.so[7f91d66f1000+22000] > [ 1.250016] traps: modprobe[79] general protection fault ip:7f0ddbbdc95c sp:7ffe9c34f8d8 error:0 in ld-2.17.so[7f0ddbbc5000+22000] > > *modprobe* calls CPUID instruction thus causing cpuid faulting in guest. > At the end, because guest cannot *modprobe* modules, it boots failure. > > This patch switches MSR_MISC_FEATURES_ENABLES between host and guest when > hardware has this MSR. > > This patch doesn't confict with the commit db2336a80489 ("KVM: x86: virtualize > cpuid faulting"), which provides a software emulation of cpuid faulting for > x86 arch. Below analysing how cpuid faulting will work after applying this patch: > > 1. If host cpu is AMD. It doesn't have MSR_MISC_FEATURES_ENABLES, so we can just > use the software emulation of cpuid faulting. > > 2. If host cpu is Intel and it doesn't have MSR_MISC_FEATURES_ENABLES. The same > as case 1, we can just use the software emulation of cpuid faulting. > > 3. If host cpu is Intel and it has MSR_MISC_FEATURES_ENABLES. With this patch, > it will write guest's value into MSR_MISC_FEATURES_ENABLES when vm entry. > If guest enables cpuid faulting and when guest calls CPUID instruction with > CPL > 0, it will cause #GP exception in guest instead of VM exit because of > CPUID, thus it doesn't go to the kvm emualtion path but ues the hardware > feature. Also it's a benefit that we needn't use VM exit to inject #GP to > emulate cpuid faulting feature. > > Intel SDM vol3.25.1.1 specifies the priority between cpuid faulting > and CPUID instruction. > > Signed-off-by: Xiaoyao Li > --- > arch/x86/kvm/vmx/vmx.c | 19 +++++++++++++++++++ > 1 file changed, 19 insertions(+) > > diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c > index 30a6bcd735ec..90707fae688e 100644 > --- a/arch/x86/kvm/vmx/vmx.c > +++ b/arch/x86/kvm/vmx/vmx.c > @@ -6321,6 +6321,23 @@ static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx) > msrs[i].host, false); > } > > +static void atomic_switch_msr_misc_features_enables(struct kvm_vcpu *vcpu) > +{ > + u64 host_msr; > + struct vcpu_vmx *vmx = to_vmx(vcpu); > + > + /* if MSR MISC_FEATURES_ENABLES doesn't exist on the hardware, do nothing*/ > + if (rdmsrl_safe(MSR_MISC_FEATURES_ENABLES, &host_msr)) > + return; > + > + if (host_msr == vcpu->arch.msr_misc_features_enables) > + clear_atomic_switch_msr(vmx, MSR_MISC_FEATURES_ENABLES); > + else > + add_atomic_switch_msr(vmx, MSR_MISC_FEATURES_ENABLES, > + vcpu->arch.msr_misc_features_enables, > + host_msr, false); > +} > + > static void vmx_arm_hv_timer(struct vcpu_vmx *vmx, u32 val) > { > vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, val); > @@ -6562,6 +6579,8 @@ static void vmx_vcpu_run(struct kvm_vcpu *vcpu) > > atomic_switch_perf_msrs(vmx); > > + atomic_switch_msr_misc_features_enables(vcpu); > + > vmx_update_hv_timer(vcpu); > > /* > Adding a RDMSR for this to each vmentry is too heavy. Since we emulate MSR_MISC_FEATURES_ENABLES, you can just clear the MSR on vcpu_load and restore it on vcpu_put. Also, this needs a test in tools/testing/selftests/kvm. Paolo