Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp857017yba; Wed, 24 Apr 2019 10:43:01 -0700 (PDT) X-Google-Smtp-Source: APXvYqzk6qTRntsn5oH19CQIpLkHqiw8TVTwPHHGTNPx2gUbMBzFF2xAD89qyRf6swjCWZgJ7KGr X-Received: by 2002:a63:d709:: with SMTP id d9mr32273975pgg.38.1556127780942; Wed, 24 Apr 2019 10:43:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1556127780; cv=none; d=google.com; s=arc-20160816; b=K4LkXRNfDy35GbVTKUvK8NpbjMMsra1kKhq1rZWoawLJfuMEbTXHi782m6oCsQwQKe FKwzCHcYIlSNCJesMqLgA4Qq4gmjH6XvNwqNviEs146A/2PfOCaZyLRvxTpEdYPOodFD FoUof0BCsbYhGTStB6ZbCTxrmMHx9kaZiHpJXTiTn2j5igPkfISDHP8iWZUtVPAr+eGJ Mg12hRlq0IF4dJ1LgW7q9yuRvSzqZ+wnrxHr+hLvwgwtrVNEhQiDIepPwRliBaAVxA6E pFy0JPSiGWxKaG/DF4N2yAtmOC8GtM/tisyaxutVXvmlS1jypPLTg28FbwYwvI0AXyV6 7o+g== 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:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=J1fz375E+IuBQIlyN0wByNBa8bjBlf6+COIy0hipobw=; b=ZLyCZSKpR3a77HFdx8HupsvEoLAPpv93uwwxz1djmmfpnj/do/46EJnNCIRV5Peukp pINyjWlGvKAeV0MpBLu+YqKPCprUnYv/IuIChgkPUYPAsVCEvaNfVRcapcS0AJbI/59y EFaUSq6/2caEOt9AnuKZSmUjHWisb0CplnsHZqzoF/nbyrGt7PdFytGPgR8SxjghvfTH e0Wzr3MEHhSN55GZ6PeGf9yWX2acLWmQiWl3TcWzjhBzACjAGUVakZjm5vF+nTA6nCa5 W1E/iZvOiPHfkdH8REilgvX5VZ6GBOU10Tnj0dZz8OfGBpeN8i/fyrsOvwiGJg1o2ACz 3yxA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=eM8MqFgg; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id c7si4027113pgn.277.2019.04.24.10.42.45; Wed, 24 Apr 2019 10:43:00 -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; dkim=pass header.i=@kernel.org header.s=default header.b=eM8MqFgg; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2392078AbfDXRg4 (ORCPT + 99 others); Wed, 24 Apr 2019 13:36:56 -0400 Received: from mail.kernel.org ([198.145.29.99]:35712 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2392176AbfDXRgw (ORCPT ); Wed, 24 Apr 2019 13:36:52 -0400 Received: from localhost (62-193-50-229.as16211.net [62.193.50.229]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 23ED221904; Wed, 24 Apr 2019 17:36:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1556127410; bh=G58GOjJY7ucINlevbt6attF/gldHjBvlfzQmDz1MXjA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=eM8MqFgg251diOpANbD7gpJ364ZsZfrGaduXAXS4oavDHEU5BaBsCbTyrHOaL+BD8 bhkelwIvLNr6jUdOjQ3LP7WwzOzkcxHCd/60nL1ja1MIHB9yAv7BZ2XaTSxSy2mzHl y4PHZyFEz8q7Ic35UZ0Lt1288yJ7DSdCOiTWw50s= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, WANG Chao , Paolo Bonzini Subject: [PATCH 5.0 082/115] x86/kvm: move kvm_load/put_guest_xcr0 into atomic context Date: Wed, 24 Apr 2019 19:10:18 +0200 Message-Id: <20190424170929.753343125@linuxfoundation.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190424170924.797924502@linuxfoundation.org> References: <20190424170924.797924502@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: WANG Chao commit 1811d979c71621aafc7b879477202d286f7e863b upstream. guest xcr0 could leak into host when MCE happens in guest mode. Because do_machine_check() could schedule out at a few places. For example: kvm_load_guest_xcr0 ... kvm_x86_ops->run(vcpu) { vmx_vcpu_run vmx_complete_atomic_exit kvm_machine_check do_machine_check do_memory_failure memory_failure lock_page In this case, host_xcr0 is 0x2ff, guest vcpu xcr0 is 0xff. After schedule out, host cpu has guest xcr0 loaded (0xff). In __switch_to { switch_fpu_finish copy_kernel_to_fpregs XRSTORS If any bit i in XSTATE_BV[i] == 1 and xcr0[i] == 0, XRSTORS will generate #GP (In this case, bit 9). Then ex_handler_fprestore kicks in and tries to reinitialize fpu by restoring init fpu state. Same story as last #GP, except we get DOUBLE FAULT this time. Cc: stable@vger.kernel.org Signed-off-by: WANG Chao Signed-off-by: Paolo Bonzini Signed-off-by: Greg Kroah-Hartman --- arch/x86/kvm/svm.c | 2 ++ arch/x86/kvm/vmx/vmx.c | 4 ++++ arch/x86/kvm/x86.c | 10 ++++------ arch/x86/kvm/x86.h | 2 ++ 4 files changed, 12 insertions(+), 6 deletions(-) --- a/arch/x86/kvm/svm.c +++ b/arch/x86/kvm/svm.c @@ -5634,6 +5634,7 @@ static void svm_vcpu_run(struct kvm_vcpu svm->vmcb->save.cr2 = vcpu->arch.cr2; clgi(); + kvm_load_guest_xcr0(vcpu); /* * If this vCPU has touched SPEC_CTRL, restore the guest's value if @@ -5779,6 +5780,7 @@ static void svm_vcpu_run(struct kvm_vcpu if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI)) kvm_before_interrupt(&svm->vcpu); + kvm_put_guest_xcr0(vcpu); stgi(); /* Any pending NMI will happen here */ --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -6548,6 +6548,8 @@ static void vmx_vcpu_run(struct kvm_vcpu if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) vmx_set_interrupt_shadow(vcpu, 0); + kvm_load_guest_xcr0(vcpu); + if (static_cpu_has(X86_FEATURE_PKU) && kvm_read_cr4_bits(vcpu, X86_CR4_PKE) && vcpu->arch.pkru != vmx->host_pkru) @@ -6635,6 +6637,8 @@ static void vmx_vcpu_run(struct kvm_vcpu __write_pkru(vmx->host_pkru); } + kvm_put_guest_xcr0(vcpu); + vmx->nested.nested_run_pending = 0; vmx->idt_vectoring_info = 0; --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -800,7 +800,7 @@ void kvm_lmsw(struct kvm_vcpu *vcpu, uns } EXPORT_SYMBOL_GPL(kvm_lmsw); -static void kvm_load_guest_xcr0(struct kvm_vcpu *vcpu) +void kvm_load_guest_xcr0(struct kvm_vcpu *vcpu) { if (kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE) && !vcpu->guest_xcr0_loaded) { @@ -810,8 +810,9 @@ static void kvm_load_guest_xcr0(struct k vcpu->guest_xcr0_loaded = 1; } } +EXPORT_SYMBOL_GPL(kvm_load_guest_xcr0); -static void kvm_put_guest_xcr0(struct kvm_vcpu *vcpu) +void kvm_put_guest_xcr0(struct kvm_vcpu *vcpu) { if (vcpu->guest_xcr0_loaded) { if (vcpu->arch.xcr0 != host_xcr0) @@ -819,6 +820,7 @@ static void kvm_put_guest_xcr0(struct kv vcpu->guest_xcr0_loaded = 0; } } +EXPORT_SYMBOL_GPL(kvm_put_guest_xcr0); static int __kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr) { @@ -7856,8 +7858,6 @@ static int vcpu_enter_guest(struct kvm_v goto cancel_injection; } - kvm_load_guest_xcr0(vcpu); - if (req_immediate_exit) { kvm_make_request(KVM_REQ_EVENT, vcpu); kvm_x86_ops->request_immediate_exit(vcpu); @@ -7910,8 +7910,6 @@ static int vcpu_enter_guest(struct kvm_v vcpu->mode = OUTSIDE_GUEST_MODE; smp_wmb(); - kvm_put_guest_xcr0(vcpu); - kvm_before_interrupt(vcpu); kvm_x86_ops->handle_external_intr(vcpu); kvm_after_interrupt(vcpu); --- a/arch/x86/kvm/x86.h +++ b/arch/x86/kvm/x86.h @@ -347,4 +347,6 @@ static inline void kvm_after_interrupt(s __this_cpu_write(current_vcpu, NULL); } +void kvm_load_guest_xcr0(struct kvm_vcpu *vcpu); +void kvm_put_guest_xcr0(struct kvm_vcpu *vcpu); #endif