From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752347AbcF1InW (ORCPT ); Tue, 28 Jun 2016 04:43:22 -0400 Received: from mail-wm0-f68.google.com ([74.125.82.68]:35035 "EHLO mail-wm0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751419AbcF1InS (ORCPT ); Tue, 28 Jun 2016 04:43:18 -0400 Subject: Re: [PATCH] KVM: vmx: fix underflow in TSC deadline calculation To: Wanpeng Li References: <1467033073-3746-1-git-send-email-pbonzini@redhat.com> Cc: "linux-kernel@vger.kernel.org" , kvm , Wanpeng Li , Yunhong Jiang From: Paolo Bonzini Message-ID: <87394b34-800a-d981-48b8-c6f02e39caed@redhat.com> Date: Tue, 28 Jun 2016 10:43:13 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.1.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 28/06/2016 08:15, Wanpeng Li wrote: > 2016-06-27 21:11 GMT+08:00 Paolo Bonzini : >> If the TSC deadline timer is programmed really close to the deadline or >> even in the past, the computation in vmx_set_hv_timer can underflow and >> cause delta_tsc to be set to a huge value. This generally results >> in vmx_set_hv_timer returning -ERANGE, but we can fix it by limiting >> delta_tsc to be positive or zero. >> >> Reported-by: Wanpeng Li >> Signed-off-by: Paolo Bonzini >> --- >> arch/x86/kvm/vmx.c | 6 +++--- >> 1 file changed, 3 insertions(+), 3 deletions(-) >> >> diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c >> index c1d655c10fd2..85e2f0a882ca 100644 >> --- a/arch/x86/kvm/vmx.c >> +++ b/arch/x86/kvm/vmx.c >> @@ -10829,9 +10829,9 @@ static inline int u64_shl_div_u64(u64 a, unsigned int shift, >> static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc) >> { >> struct vcpu_vmx *vmx = to_vmx(vcpu); >> - u64 tscl = rdtsc(), delta_tsc; >> - >> - delta_tsc = guest_deadline_tsc - kvm_read_l1_tsc(vcpu, tscl); >> + u64 tscl = rdtsc(); >> + u64 guest_tscl = kvm_read_l1_tsc(vcpu, tscl); >> + u64 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl; >> >> /* Convert to host delta tsc if tsc scaling is enabled */ >> if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio && > > This patch still can't fix the bug after my testing. I have a patch on > hand and will send out soon. Nice! Do you think we need both patches? Thanks, Paolo