Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933675AbeAHTqg (ORCPT + 1 other); Mon, 8 Jan 2018 14:46:36 -0500 Received: from userp2120.oracle.com ([156.151.31.85]:47472 "EHLO userp2120.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932750AbeAHTqe (ORCPT ); Mon, 8 Jan 2018 14:46:34 -0500 Message-ID: <5A53C95C.5000602@ORACLE.COM> Date: Mon, 08 Jan 2018 21:41:16 +0200 From: Liran Alon User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.5.0 MIME-Version: 1.0 To: Paolo Bonzini , linux-kernel@vger.kernel.org, kvm@vger.kernel.org CC: jmattson@google.com, aliguori@amazon.com, thomas.lendacky@amd.com, dwmw@amazon.co.uk, bp@alien8.de Subject: Re: [PATCH 5/7] kvm: svm: pass MSR_IA32_SPEC_CTRL and MSR_IA32_PRED_CMD down to guest References: <1515434925-10250-1-git-send-email-pbonzini@redhat.com> <1515434925-10250-6-git-send-email-pbonzini@redhat.com> In-Reply-To: <1515434925-10250-6-git-send-email-pbonzini@redhat.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Proofpoint-Virus-Version: vendor=nai engine=5900 definitions=8768 signatures=668652 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=0 malwarescore=0 phishscore=0 bulkscore=0 spamscore=0 mlxscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1711220000 definitions=main-1801080279 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Return-Path: On 08/01/18 20:08, Paolo Bonzini wrote: > Direct access to MSR_IA32_SPEC_CTRL and MSR_IA32_PRED_CMD is important > for performance. Allow load/store of MSR_IA32_SPEC_CTRL, restore guest > IBRS on VM entry and set it to 0 on VM exit (because Linux does not use > it yet). > > Signed-off-by: Paolo Bonzini > --- > arch/x86/kvm/svm.c | 34 +++++++++++++++++++++++++++++++++- > 1 file changed, 33 insertions(+), 1 deletion(-) > > diff --git a/arch/x86/kvm/svm.c b/arch/x86/kvm/svm.c > index 55dde3896898..779981a00d01 100644 > --- a/arch/x86/kvm/svm.c > +++ b/arch/x86/kvm/svm.c > @@ -183,6 +183,8 @@ struct vcpu_svm { > u64 gs_base; > } host; > > + u64 spec_ctrl; > + > u32 *msrpm; > > ulong nmi_iret_rip; > @@ -236,7 +238,7 @@ struct amd_svm_iommu_ir { > > static const struct svm_direct_access_msrs { > u32 index; /* Index of the MSR */ > - bool always; /* True if intercept is always on */ > + bool always; /* True if intercept is always off */ By examining svm_vcpu_init_msrpm() it seems you are correct as if always==true then read/write bits are cleared from msrpm (which pass-through the MSR to the guest). However, I do think this comment change should be done in a separate commit as it is unrelated. > } direct_access_msrs[] = { > { .index = MSR_STAR, .always = true }, > { .index = MSR_IA32_SYSENTER_CS, .always = true }, > @@ -248,6 +250,8 @@ struct amd_svm_iommu_ir { > { .index = MSR_CSTAR, .always = true }, > { .index = MSR_SYSCALL_MASK, .always = true }, > #endif > + { .index = MSR_IA32_SPEC_CTRL, .always = true }, > + { .index = MSR_IA32_PRED_CMD, .always = true }, > { .index = MSR_IA32_LASTBRANCHFROMIP, .always = false }, > { .index = MSR_IA32_LASTBRANCHTOIP, .always = false }, > { .index = MSR_IA32_LASTINTFROMIP, .always = false }, > @@ -283,6 +287,8 @@ struct amd_svm_iommu_ir { > /* enable/disable Virtual GIF */ > static int vgif = true; > module_param(vgif, int, 0444); > + > +static bool __read_mostly have_spec_ctrl; > > static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0); > static void svm_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa); > @@ -1135,6 +1141,17 @@ static __init int svm_hardware_setup(void) > pr_info("Virtual GIF supported\n"); > } > > + /* > + * FIXME: this is only needed until SPEC_CTRL is supported > + * by upstream Linux in cpufeatures, then it can be replaced > + * with static_cpu_has. > + */ > + have_spec_ctrl = cpu_has_spec_ctrl(); > + if (have_spec_ctrl) > + pr_info("kvm: SPEC_CTRL available\n"); > + else > + pr_info("kvm: SPEC_CTRL not available\n"); > + > return 0; > > err: > @@ -3599,6 +3616,9 @@ static int svm_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) > case MSR_VM_CR: > msr_info->data = svm->nested.vm_cr_msr; > break; > + case MSR_IA32_SPEC_CTRL: > + msr_info->data = svm->spec_ctrl; > + break; > case MSR_IA32_UCODE_REV: > msr_info->data = 0x01000065; > break; > @@ -3754,6 +3774,9 @@ static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr) > case MSR_VM_IGNNE: > vcpu_unimpl(vcpu, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx, data); > break; > + case MSR_IA32_SPEC_CTRL: > + svm->spec_ctrl = data; > + break; > case MSR_IA32_APICBASE: > if (kvm_vcpu_apicv_active(vcpu)) > avic_update_vapic_bar(to_svm(vcpu), data); > @@ -4942,6 +4965,9 @@ static void svm_vcpu_run(struct kvm_vcpu *vcpu) > > local_irq_enable(); > > + if (have_spec_ctrl && svm->spec_ctrl != 0) > + wrmsrl(MSR_IA32_SPEC_CTRL, svm->spec_ctrl); > + > asm volatile ( > "push %%" _ASM_BP "; \n\t" > "mov %c[rbx](%[svm]), %%" _ASM_BX " \n\t" > @@ -5015,6 +5041,12 @@ static void svm_vcpu_run(struct kvm_vcpu *vcpu) > #endif > ); > > + if (have_spec_ctrl) { > + rdmsrl(MSR_IA32_SPEC_CTRL, svm->spec_ctrl); > + if (svm->spec_ctrl != 0) Very small stuff but I hate inconsistencies: Either you put here (svm->spec_ctrl) like in arch/x86/kvm/vmx.c or you will use there (vmx->spec_ctrl != 0). > + wrmsrl(MSR_IA32_SPEC_CTRL, 0); > + } > + > #ifdef CONFIG_X86_64 > wrmsrl(MSR_GS_BASE, svm->host.gs_base); > #else >