Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp2383056pxj; Mon, 10 May 2021 01:19:53 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwSTCA+ICrBPKAW/Znl7Qbw1c6C3PXkKn6hSvyrabGoewZpuc3+ZSyeyaJAdBaAZBmYFw9b X-Received: by 2002:a17:906:8390:: with SMTP id p16mr23541623ejx.430.1620634793075; Mon, 10 May 2021 01:19:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620634793; cv=none; d=google.com; s=arc-20160816; b=j2geTjYME2giQ9O/L9DtbBartfwXCdAUfzzndyUu89RI05YqXoA6I69Qp9rL1jByxZ izsyWnRD2BmST41Y2O3slo1ul/FWmWjZH4QGHTf5PEHCTjBWV9bediy6yX0YpDB/N+Oa pYTY8S4xTnmPreercx27ChSGHZjgO+AUytFu295NjM76GPeDyFbNWEqWvBw4HCcCfRae py9VNDqFPbT0IkSVKRD+RtB1u8lRMHbizLrdkZ38Vp5cq1CX39E44mRBiXciyIARodml y+y2hUgBkY0RsMsNSZ/J4kXXAesfr2kw5MAVTc2g+NiscW0NDwKhdA4Wg9aXYVFLbjvU /spQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:date:cc:to:from:subject :message-id:dkim-signature; bh=LWHVecN3IcQ6TaVNoD4ymbrSPyoMCexrSWsjK9p+k/c=; b=DIX1Gzqm7m2IqhwMLGh87OVkgKmB+BqVa6+VVziUk2DC82FqD28Cl44yVnHktv4Vyn 1+8POnsR2/mZT7NoCWI3qmk+cY52hEbQXzQTP4pMqrDHJqTMF67g2IY5itnT/BU+RZxs AFmNWMcqqz1AgERadkqQWX3vw7vWY74WNYaGaM7v5p8WvMjpItinXl7YSMy4Ghi0YcD6 YL4hw3B9EXHq6uNCy1jhhykbzzUqH9hhlHeB0tJs9QziDHkqlJ3AChDJMhnRXhfzluIc Wx4bQ6Zqln0KKGSo6U6OortuLuyiP+pnDHlYtAkcaklwTqg3buouFFGo28W3TiVy/YEM 1O3w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=aAyfEni4; 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=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id r10si13582929ejy.100.2021.05.10.01.19.29; Mon, 10 May 2021 01:19:53 -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=@redhat.com header.s=mimecast20190719 header.b=aAyfEni4; 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=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230146AbhEJITo (ORCPT + 99 others); Mon, 10 May 2021 04:19:44 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:59490 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230135AbhEJITo (ORCPT ); Mon, 10 May 2021 04:19:44 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1620634719; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=LWHVecN3IcQ6TaVNoD4ymbrSPyoMCexrSWsjK9p+k/c=; b=aAyfEni4VXLTgpujDdf7srOHlnjKmWgxIS9xnz1ti1345iDaJGo0cDnT8FdAF05AzoXK4l Q//qxXz7PCYohaQCAFHcOGRO3qXZuDnwp2Fn73QUiB5w49I8yvoZo9bGyxHAIzVzJ93Jht alxt4+MNKNRLeTLTNBHHgpx9Mq+ys5E= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-565-bBW1ouWrNyKkzkyR6PIfuw-1; Mon, 10 May 2021 04:18:38 -0400 X-MC-Unique: bBW1ouWrNyKkzkyR6PIfuw-1 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 mimecast-mx01.redhat.com (Postfix) with ESMTPS id 12EF0801B13; Mon, 10 May 2021 08:18:36 +0000 (UTC) Received: from starship (unknown [10.40.194.86]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2021517A9B; Mon, 10 May 2021 08:18:32 +0000 (UTC) Message-ID: <17c3853354e59ad99afffc0481eb0796a5975003.camel@redhat.com> Subject: Re: [PATCH 05/15] KVM: VMX: Disable preemption when probing user return MSRs From: Maxim Levitsky To: Sean Christopherson , Paolo Bonzini Cc: Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Xiaoyao Li , Reiji Watanabe Date: Mon, 10 May 2021 11:18:31 +0300 In-Reply-To: <20210504171734.1434054-6-seanjc@google.com> References: <20210504171734.1434054-1-seanjc@google.com> <20210504171734.1434054-6-seanjc@google.com> Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.36.5 (3.36.5-2.fc32) MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, 2021-05-04 at 10:17 -0700, Sean Christopherson wrote: > Disable preemption when probing a user return MSR via RDSMR/WRMSR. If > the MSR holds a different value per logical CPU, the WRMSR could corrupt > the host's value if KVM is preempted between the RDMSR and WRMSR, and > then rescheduled on a different CPU. > > Opportunistically land the helper in common x86, SVM will use the helper > in a future commit. > > Fixes: 4be534102624 ("KVM: VMX: Initialize vmx->guest_msrs[] right after allocation") > Cc: stable@vger.kernel.org > Cc: Xiaoyao Li > Signed-off-by: Sean Christopherson > --- > arch/x86/include/asm/kvm_host.h | 1 + > arch/x86/kvm/vmx/vmx.c | 5 +---- > arch/x86/kvm/x86.c | 16 ++++++++++++++++ > 3 files changed, 18 insertions(+), 4 deletions(-) > > diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h > index 3e5fc80a35c8..a02c9bf3f7f1 100644 > --- a/arch/x86/include/asm/kvm_host.h > +++ b/arch/x86/include/asm/kvm_host.h > @@ -1778,6 +1778,7 @@ int kvm_pv_send_ipi(struct kvm *kvm, unsigned long ipi_bitmap_low, > unsigned long icr, int op_64_bit); > > void kvm_define_user_return_msr(unsigned index, u32 msr); > +int kvm_probe_user_return_msr(u32 msr); > int kvm_set_user_return_msr(unsigned index, u64 val, u64 mask); > > u64 kvm_scale_tsc(struct kvm_vcpu *vcpu, u64 tsc); > diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c > index 99591e523b47..990ee339a05f 100644 > --- a/arch/x86/kvm/vmx/vmx.c > +++ b/arch/x86/kvm/vmx/vmx.c > @@ -6914,12 +6914,9 @@ static int vmx_create_vcpu(struct kvm_vcpu *vcpu) > > for (i = 0; i < ARRAY_SIZE(vmx_uret_msrs_list); ++i) { > u32 index = vmx_uret_msrs_list[i]; > - u32 data_low, data_high; > int j = vmx->nr_uret_msrs; > > - if (rdmsr_safe(index, &data_low, &data_high) < 0) > - continue; > - if (wrmsr_safe(index, data_low, data_high) < 0) > + if (kvm_probe_user_return_msr(index)) > continue; > > vmx->guest_uret_msrs[j].slot = i; > diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c > index 3bf52ba5f2bb..e304447be42d 100644 > --- a/arch/x86/kvm/x86.c > +++ b/arch/x86/kvm/x86.c > @@ -339,6 +339,22 @@ static void kvm_on_user_return(struct user_return_notifier *urn) > } > } > > +int kvm_probe_user_return_msr(u32 msr) > +{ > + u64 val; > + int ret; > + > + preempt_disable(); > + ret = rdmsrl_safe(msr, &val); > + if (ret) > + goto out; > + ret = wrmsrl_safe(msr, val); > +out: > + preempt_enable(); > + return ret; > +} > +EXPORT_SYMBOL_GPL(kvm_probe_user_return_msr); > + > void kvm_define_user_return_msr(unsigned slot, u32 msr) > { > BUG_ON(slot >= KVM_MAX_NR_USER_RETURN_MSRS); Reviewed-by: Maxim Levitsky One note though: every time we probe for a MSR existance via checking for a #GP on write, we risk getting nonsense results if the L1 has ignore_msrs=1. Thus if possible to use the CPUID instead, that would be preferred. Best regards, Maxim Levitsky