Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1432033 > unrolled thread

[PATCH] KVM: vmx: fix underflow in TSC deadline calculation

Started byPaolo Bonzini <pbonzini@redhat.com>
First post2016-06-27 15:20 +0200
Last post2016-06-28 10:50 +0200
Articles 4 — 2 participants

Back to article view | Back to linux.kernel


Contents

  [PATCH] KVM: vmx: fix underflow in TSC deadline calculation Paolo Bonzini <pbonzini@redhat.com> - 2016-06-27 15:20 +0200
    Re: [PATCH] KVM: vmx: fix underflow in TSC deadline calculation Wanpeng Li <kernellwp@gmail.com> - 2016-06-28 08:20 +0200
      Re: [PATCH] KVM: vmx: fix underflow in TSC deadline calculation Wanpeng Li <kernellwp@gmail.com> - 2016-06-28 10:50 +0200
      Re: [PATCH] KVM: vmx: fix underflow in TSC deadline calculation Paolo Bonzini <pbonzini@redhat.com> - 2016-06-28 10:50 +0200

#1432033 — [PATCH] KVM: vmx: fix underflow in TSC deadline calculation

FromPaolo Bonzini <pbonzini@redhat.com>
Date2016-06-27 15:20 +0200
Subject[PATCH] KVM: vmx: fix underflow in TSC deadline calculation
Message-ID<rOECd-Ho-1@gated-at.bofh.it>
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 <wanpeng.li@hotmail.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 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 &&
-- 
1.8.3.1

[toc] | [next] | [standalone]


#1432508

FromWanpeng Li <kernellwp@gmail.com>
Date2016-06-28 08:20 +0200
Message-ID<rOUxj-35E-7@gated-at.bofh.it>
In reply to#1432033
2016-06-27 21:11 GMT+08:00 Paolo Bonzini <pbonzini@redhat.com>:
> 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 <wanpeng.li@hotmail.com>
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
> ---
>  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.

Regards,
Wanpeng Li

[toc] | [prev] | [next] | [standalone]


#1432644

FromWanpeng Li <kernellwp@gmail.com>
Date2016-06-28 10:50 +0200
Message-ID<rOWSt-4vW-19@gated-at.bofh.it>
In reply to#1432508
2016-06-28 16:43 GMT+08:00 Paolo Bonzini <pbonzini@redhat.com>:
>
>
> On 28/06/2016 08:15, Wanpeng Li wrote:
>> 2016-06-27 21:11 GMT+08:00 Paolo Bonzini <pbonzini@redhat.com>:
>>> 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 <wanpeng.li@hotmail.com>
>>> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
>>> ---
>>>  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?

Yeah, we can keep them separately. :)

Regards,
Wanpeng Li

[toc] | [prev] | [next] | [standalone]


#1432647

FromPaolo Bonzini <pbonzini@redhat.com>
Date2016-06-28 10:50 +0200
Message-ID<rOWSt-4vW-21@gated-at.bofh.it>
In reply to#1432508

On 28/06/2016 08:15, Wanpeng Li wrote:
> 2016-06-27 21:11 GMT+08:00 Paolo Bonzini <pbonzini@redhat.com>:
>> 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 <wanpeng.li@hotmail.com>
>> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
>> ---
>>  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

[toc] | [prev] | [standalone]


Back to top | Article view | linux.kernel


csiph-web