Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp177047pxj; Tue, 18 May 2021 00:22:52 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx8rKVGpqs4dUuTfwVDa95HGnCXuPIJkIOMCd3TGkELXo4zKiau9ueVo2Rj4KlXxrebAxY4 X-Received: by 2002:a05:6402:40d0:: with SMTP id z16mr5449849edb.104.1621322572018; Tue, 18 May 2021 00:22:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1621322572; cv=none; d=google.com; s=arc-20160816; b=lhwWDMOjazgyUQH2YS6GuIhREy4M0KAGPOLpSi9vBonTp+ca1RI2BR6dKYzlls/jfy s+qiVvdOgvtudE+gKR/b5hcPDCTmY8JyCcS1iwnn7FUtRo9zsbNCygNk/WZJnqEWrKyq /5/FRv8jqrT5bBDNO7ugz07Gqdv51YBOM+X63BGjWJfQOPu+Warlx9L4gTrtjqh/6wUm rNao29TZkoH7nvRznwU/BHS3BEKveC3RFp95wyLSHkLtrkU25xut74olB01/WaTITRsY 9MMTEYzSda4sUy381Mm93if9t7p7YP/7qqRBTmv4z2GXiNsugJIv2GUeNIswF2JoHFiM i5LQ== 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:message-id:date:subject:cc:to :from:dkim-signature; bh=p/BIa3wW4+WrteiS8+0+rWyVWFDdd9xkXVhx5mEejdw=; b=sIt6PGaUbMAwbPyr90Wwif6s8NfgdRBaGo+fxK96VFRienn5TBLxWzteV7XatbksWh C/O4KLtLheRBtT1eCYOfbr3fnBkLkioMVwhjqwIuNFCBHQ3cTZafUj7fN/oJrriIK7wn DnqoCm5Mcj3Pop1e/+mNfDjEzMm810gEt13q99wyGyb6cYp0PMFSFzuNCTHH4OUcZoqR qKwkC/MDYRMcpTVWwSDmpeViwFbB1hpOFP/HpMcibjTjR9cmt5dma//N8RQcgJcYt3vL POUSvdTO3dHyaD7H4jMjqFRDEMfsDhdaRt2yesMvTAPXGJuoHfR+3NO+MHMlS36qNeqY NqwA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=tKpwbPJE; 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=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id pk25si16858191ejb.700.2021.05.18.00.22.27; Tue, 18 May 2021 00:22:52 -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=@linuxfoundation.org header.s=korg header.b=tKpwbPJE; 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=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240855AbhEQOz7 (ORCPT + 99 others); Mon, 17 May 2021 10:55:59 -0400 Received: from mail.kernel.org ([198.145.29.99]:37902 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240509AbhEQOr7 (ORCPT ); Mon, 17 May 2021 10:47:59 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id CD5B06196C; Mon, 17 May 2021 14:22:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1621261325; bh=bhkuDGn9CSoZ0F+cT7Z7/si1jIFFiEWhKkLCK9J1VPg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=tKpwbPJEp+GtquGN+NsxDBOmKmUBF4YSdAAR2zeFhcddwsDVRYurTlxQSd9tDTnY6 8YORp7AnVFNbVbbPRSqQQtU2w6EOgGss5x4WB03eXjDJfJVZn/4ZSiG3AXgnGzB03w vknAMszHZK0BmpVZo5Mq9QVyTs87Kk8+AvlwuS1M= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Xiaoyao Li , Sean Christopherson , Jim Mattson , Paolo Bonzini Subject: [PATCH 5.12 334/363] KVM: VMX: Disable preemption when probing user return MSRs Date: Mon, 17 May 2021 16:03:20 +0200 Message-Id: <20210517140313.896477790@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210517140302.508966430@linuxfoundation.org> References: <20210517140302.508966430@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Sean Christopherson commit 5104d7ffcf24749939bea7fdb5378d186473f890 upstream. 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 Message-Id: <20210504171734.1434054-6-seanjc@google.com> Reviewed-by: Jim Mattson Signed-off-by: Paolo Bonzini Signed-off-by: Greg Kroah-Hartman --- 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(-) --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1753,6 +1753,7 @@ int kvm_pv_send_ipi(struct kvm *kvm, uns 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); --- 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_vc 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; --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -335,6 +335,22 @@ static void kvm_on_user_return(struct us } } +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);