Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932768AbdGKQJ2 (ORCPT ); Tue, 11 Jul 2017 12:09:28 -0400 Received: from mx1.redhat.com ([209.132.183.28]:37674 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932635AbdGKQJ1 (ORCPT ); Tue, 11 Jul 2017 12:09:27 -0400 DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 75C033D953 Authentication-Results: ext-mx06.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx06.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=pbonzini@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 75C033D953 Subject: Re: [PATCH] KVM: VMX: Fix invalid guest state detection after task-switch emulation To: =?UTF-8?B?UmFkaW0gS3LEjW3DocWZ?= , Wanpeng Li Cc: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Wanpeng Li , Nadav Amit References: <1499757180-20439-1-git-send-email-wanpeng.li@hotmail.com> <20170711155458.GB28875@potion> From: Paolo Bonzini Message-ID: <44ce44eb-85c3-4fdb-b2ee-602ac2f9e14f@redhat.com> Date: Tue, 11 Jul 2017 18:09:10 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.2.0 MIME-Version: 1.0 In-Reply-To: <20170711155458.GB28875@potion> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 8bit X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Tue, 11 Jul 2017 16:09:26 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 2162 Lines: 53 On 11/07/2017 17:54, Radim Krčmář wrote: > 2017-07-11 00:13-0700, Wanpeng Li: >> From: Wanpeng Li >> >> This can be reproduced by EPT=1, unrestricted_guest=N, emulate_invalid_state=Y >> or EPT=0, the trace of kvm-unit-tests/taskswitch2.flat is like below, it tries >> to emulate invalid guest state task-switch: >> >> kvm_exit: reason TASK_SWITCH rip 0x0 info 40000058 0 >> kvm_emulate_insn: 42000:0:0f 0b (0x2) >> kvm_emulate_insn: 42000:0:0f 0b (0x2) failed >> kvm_inj_exception: #UD (0x0) >> kvm_entry: vcpu 0 >> kvm_exit: reason TASK_SWITCH rip 0x0 info 40000058 0 >> kvm_emulate_insn: 42000:0:0f 0b (0x2) >> kvm_emulate_insn: 42000:0:0f 0b (0x2) failed >> kvm_inj_exception: #UD (0x0) >> ...................... >> >> It appears that the task-switch emulation updates rflags (and vm86 >> flag) only after the segments are loaded, causing vmx->emulation_required >> to be set, when in fact invalid guest state emulation is not needed. >> >> This patch fixes it by updating vmx->emulation_required after the >> rflags (and vm86 flag) is updated in task-switch emulation. >> >> Suggested-by: Nadav Amit >> Cc: Paolo Bonzini >> Cc: Radim Krčmář >> Cc: Nadav Amit >> Signed-off-by: Wanpeng Li >> --- >> arch/x86/kvm/vmx.c | 1 + >> 1 file changed, 1 insertion(+) >> >> diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c >> index f50cbfd..70270a2 100644 >> --- a/arch/x86/kvm/vmx.c >> +++ b/arch/x86/kvm/vmx.c >> @@ -6255,6 +6255,7 @@ static int handle_task_switch(struct kvm_vcpu *vcpu) >> * TODO: What about debug traps on tss switch? >> * Are we supposed to inject them and update dr6? >> */ >> + vmx->emulation_required = emulation_required(vcpu); > > Hm, so the problem happened because changes to rflags can flip the value > of emulation_required(). I would add this line to vmx_set_rflags() to > make sure that we fixed everything, thanks. Note that there is some extra complication, because emulation_required is expensive and you'll want to run it only when EFLAGS.VM changes. Paolo