All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] kvm: x86: Disable KVM_HC_CLOCK_PAIRING if tsc is in always catchup mode
@ 2022-02-15 20:01 Anton Romanov
  2022-02-16 14:14 ` Vitaly Kuznetsov
  0 siblings, 1 reply; 7+ messages in thread
From: Anton Romanov @ 2022-02-15 20:01 UTC (permalink / raw)
  To: kvm, pbonzini; +Cc: mtosatti, Anton Romanov

If vcpu has tsc_always_catchup set each request updates pvclock data.
KVM_HC_CLOCK_PAIRING consumers such as ptp_kvm_x86 rely on tsc read on
host's side and do hypercall inside pvclock_read_retry loop leading to
infinite loop in such situation.

v2:
    Added warn

Signed-off-by: Anton Romanov <romanton@google.com>
---
 arch/x86/kvm/x86.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 7131d735b1ef..aaafb46a6048 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -8945,6 +8945,15 @@ static int kvm_pv_clock_pairing(struct kvm_vcpu *vcpu, gpa_t paddr,
 	if (!kvm_get_walltime_and_clockread(&ts, &cycle))
 		return -KVM_EOPNOTSUPP;
 
+	/*
+	 * When tsc is in permanent catchup mode guests won't be able to use
+	 * pvclock_read_retry loop to get consistent view of pvclock
+	 */
+	if (vcpu->arch.tsc_always_catchup) {
+		pr_warn_ratelimited("KVM_HC_CLOCK_PAIRING not supported if vcpu is in tsc catchup mode\n");
+		return -KVM_EOPNOTSUPP;
+	}
+
 	clock_pairing.sec = ts.tv_sec;
 	clock_pairing.nsec = ts.tv_nsec;
 	clock_pairing.tsc = kvm_read_l1_tsc(vcpu, cycle);
-- 
2.35.1.265.g69c8d7142f-goog


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH v2] kvm: x86: Disable KVM_HC_CLOCK_PAIRING if tsc is in always catchup mode
  2022-02-15 20:01 [PATCH v2] kvm: x86: Disable KVM_HC_CLOCK_PAIRING if tsc is in always catchup mode Anton Romanov
@ 2022-02-16 14:14 ` Vitaly Kuznetsov
  2022-02-17 15:41   ` Sean Christopherson
  0 siblings, 1 reply; 7+ messages in thread
From: Vitaly Kuznetsov @ 2022-02-16 14:14 UTC (permalink / raw)
  To: Anton Romanov; +Cc: mtosatti, Anton Romanov, kvm, pbonzini

Anton Romanov <romanton@google.com> writes:

> If vcpu has tsc_always_catchup set each request updates pvclock data.
> KVM_HC_CLOCK_PAIRING consumers such as ptp_kvm_x86 rely on tsc read on
> host's side and do hypercall inside pvclock_read_retry loop leading to
> infinite loop in such situation.
>
> v2:
>     Added warn
>
> Signed-off-by: Anton Romanov <romanton@google.com>
> ---
>  arch/x86/kvm/x86.c | 9 +++++++++
>  1 file changed, 9 insertions(+)
>
> diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> index 7131d735b1ef..aaafb46a6048 100644
> --- a/arch/x86/kvm/x86.c
> +++ b/arch/x86/kvm/x86.c
> @@ -8945,6 +8945,15 @@ static int kvm_pv_clock_pairing(struct kvm_vcpu *vcpu, gpa_t paddr,
>  	if (!kvm_get_walltime_and_clockread(&ts, &cycle))
>  		return -KVM_EOPNOTSUPP;
>  
> +	/*
> +	 * When tsc is in permanent catchup mode guests won't be able to use
> +	 * pvclock_read_retry loop to get consistent view of pvclock
> +	 */
> +	if (vcpu->arch.tsc_always_catchup) {
> +		pr_warn_ratelimited("KVM_HC_CLOCK_PAIRING not supported if vcpu is in tsc catchup mode\n");
> +		return -KVM_EOPNOTSUPP;

I'm not sure this warn is a good idea. It is guest triggerable and
'tsc_always_catchup' is not a bug, it is a perfectly valid situation in
the configuration when TSC scaling is unavailable. Even ratelimited,
it's not nice when guests can pollute host's logs.

Also, EOPNOTSUPP makes it sound like the hypercall is unsupported, I'd
suggest changing this to KVM_EFAULT.

> +	}
> +
>  	clock_pairing.sec = ts.tv_sec;
>  	clock_pairing.nsec = ts.tv_nsec;
>  	clock_pairing.tsc = kvm_read_l1_tsc(vcpu, cycle);

-- 
Vitaly


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v2] kvm: x86: Disable KVM_HC_CLOCK_PAIRING if tsc is in always catchup mode
  2022-02-16 14:14 ` Vitaly Kuznetsov
@ 2022-02-17 15:41   ` Sean Christopherson
  2022-02-17 16:09     ` Vitaly Kuznetsov
  0 siblings, 1 reply; 7+ messages in thread
From: Sean Christopherson @ 2022-02-17 15:41 UTC (permalink / raw)
  To: Vitaly Kuznetsov; +Cc: Anton Romanov, mtosatti, kvm, pbonzini

On Wed, Feb 16, 2022, Vitaly Kuznetsov wrote:
> Anton Romanov <romanton@google.com> writes:
> 
> > If vcpu has tsc_always_catchup set each request updates pvclock data.
> > KVM_HC_CLOCK_PAIRING consumers such as ptp_kvm_x86 rely on tsc read on
> > host's side and do hypercall inside pvclock_read_retry loop leading to
> > infinite loop in such situation.
> >
> > v2:
> >     Added warn

Versioning info goes in the "ignored" section, not the changelog.

> > Signed-off-by: Anton Romanov <romanton@google.com>
> > ---

This part is ignored by git.  Versioning info, and/or any commentary that doesn't
belong in the changelog, for a patch goes here.

> >  arch/x86/kvm/x86.c | 9 +++++++++
> >  1 file changed, 9 insertions(+)
> >
> > diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> > index 7131d735b1ef..aaafb46a6048 100644
> > --- a/arch/x86/kvm/x86.c
> > +++ b/arch/x86/kvm/x86.c
> > @@ -8945,6 +8945,15 @@ static int kvm_pv_clock_pairing(struct kvm_vcpu *vcpu, gpa_t paddr,
> >  	if (!kvm_get_walltime_and_clockread(&ts, &cycle))
> >  		return -KVM_EOPNOTSUPP;
> >  
> > +	/*
> > +	 * When tsc is in permanent catchup mode guests won't be able to use
> > +	 * pvclock_read_retry loop to get consistent view of pvclock
> > +	 */
> > +	if (vcpu->arch.tsc_always_catchup) {
> > +		pr_warn_ratelimited("KVM_HC_CLOCK_PAIRING not supported if vcpu is in tsc catchup mode\n");
> > +		return -KVM_EOPNOTSUPP;
> 
> I'm not sure this warn is a good idea. It is guest triggerable and
> 'tsc_always_catchup' is not a bug, it is a perfectly valid situation in
> the configuration when TSC scaling is unavailable. Even ratelimited,
> it's not nice when guests can pollute host's logs.

Agreed.  And if we want to alert the user/admin, it'd probably be better do so on
tsc_always_catchup first being set.  Doubt it's worth it though, assuming my other
patch to prevent KVM from setting tsc_always_catchup=true on non-ancient hardware
without userspace interaction gets merged.

> Also, EOPNOTSUPP makes it sound like the hypercall is unsupported, I'd
> suggest changing this to KVM_EFAULT.

Eh, it's consistent with the above check though, where KVM returns KVM_EOPNOTSUPP
due to the vclock mode being incompatible.  This is more or less the same, it's
just a different "mode".  KVM_EFAULT suggests that the guest did something wrong
and/or that the guest can remedy the problem in someway, e.g. by providing a
different address.  This issue is purely in the host's domain.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v2] kvm: x86: Disable KVM_HC_CLOCK_PAIRING if tsc is in always catchup mode
  2022-02-17 15:41   ` Sean Christopherson
@ 2022-02-17 16:09     ` Vitaly Kuznetsov
  2022-02-17 17:14       ` Paolo Bonzini
  0 siblings, 1 reply; 7+ messages in thread
From: Vitaly Kuznetsov @ 2022-02-17 16:09 UTC (permalink / raw)
  To: Sean Christopherson; +Cc: Anton Romanov, mtosatti, kvm, pbonzini

Sean Christopherson <seanjc@google.com> writes:

> On Wed, Feb 16, 2022, Vitaly Kuznetsov wrote:

...
>
>> Also, EOPNOTSUPP makes it sound like the hypercall is unsupported, I'd
>> suggest changing this to KVM_EFAULT.
>
> Eh, it's consistent with the above check though, where KVM returns KVM_EOPNOTSUPP
> due to the vclock mode being incompatible.  This is more or less the same, it's
> just a different "mode".  KVM_EFAULT suggests that the guest did something wrong
> and/or that the guest can remedy the problem in someway, e.g. by providing a
> different address.  This issue is purely in the host's domain.

Ack, Paolo's already made the change back to KVM_EOPNOTSUPP upon commit
(but I still mildly dislike using 'EOPNOTSUPP' for a temporary condition
on the host).

-- 
Vitaly


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v2] kvm: x86: Disable KVM_HC_CLOCK_PAIRING if tsc is in always catchup mode
  2022-02-17 16:09     ` Vitaly Kuznetsov
@ 2022-02-17 17:14       ` Paolo Bonzini
  2022-02-17 17:16         ` Vitaly Kuznetsov
  0 siblings, 1 reply; 7+ messages in thread
From: Paolo Bonzini @ 2022-02-17 17:14 UTC (permalink / raw)
  To: Vitaly Kuznetsov, Sean Christopherson; +Cc: Anton Romanov, mtosatti, kvm

On 2/17/22 17:09, Vitaly Kuznetsov wrote:
> Sean Christopherson <seanjc@google.com> writes:
> 
>> On Wed, Feb 16, 2022, Vitaly Kuznetsov wrote:
> 
> ...
>>
>>> Also, EOPNOTSUPP makes it sound like the hypercall is unsupported, I'd
>>> suggest changing this to KVM_EFAULT.
>>
>> Eh, it's consistent with the above check though, where KVM returns KVM_EOPNOTSUPP
>> due to the vclock mode being incompatible.  This is more or less the same, it's
>> just a different "mode".  KVM_EFAULT suggests that the guest did something wrong
>> and/or that the guest can remedy the problem in someway, e.g. by providing a
>> different address.  This issue is purely in the host's domain.
> 
> Ack, Paolo's already made the change back to KVM_EOPNOTSUPP upon commit
> (but I still mildly dislike using 'EOPNOTSUPP' for a temporary condition
> on the host).

It's not temporary, always-catchup is set on KVM_SET_TSC_KHZ and never 
goes away.

Paolo


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v2] kvm: x86: Disable KVM_HC_CLOCK_PAIRING if tsc is in always catchup mode
  2022-02-17 17:14       ` Paolo Bonzini
@ 2022-02-17 17:16         ` Vitaly Kuznetsov
  2022-02-17 17:39           ` Paolo Bonzini
  0 siblings, 1 reply; 7+ messages in thread
From: Vitaly Kuznetsov @ 2022-02-17 17:16 UTC (permalink / raw)
  To: Paolo Bonzini; +Cc: Anton Romanov, mtosatti, kvm, Sean Christopherson

Paolo Bonzini <pbonzini@redhat.com> writes:

> On 2/17/22 17:09, Vitaly Kuznetsov wrote:
...
>> (but I still mildly dislike using 'EOPNOTSUPP' for a temporary condition
>> on the host).
>
> It's not temporary, always-catchup is set on KVM_SET_TSC_KHZ and never 
> goes away.

Even if the guest is migrated (back) to the host which supports TSC
scaling?

-- 
Vitaly


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v2] kvm: x86: Disable KVM_HC_CLOCK_PAIRING if tsc is in always catchup mode
  2022-02-17 17:16         ` Vitaly Kuznetsov
@ 2022-02-17 17:39           ` Paolo Bonzini
  0 siblings, 0 replies; 7+ messages in thread
From: Paolo Bonzini @ 2022-02-17 17:39 UTC (permalink / raw)
  To: Vitaly Kuznetsov; +Cc: Anton Romanov, mtosatti, kvm, Sean Christopherson

On 2/17/22 18:16, Vitaly Kuznetsov wrote:
> Paolo Bonzini <pbonzini@redhat.com> writes:
> 
>> On 2/17/22 17:09, Vitaly Kuznetsov wrote:
> ...
>>> (but I still mildly dislike using 'EOPNOTSUPP' for a temporary condition
>>> on the host).
>>
>> It's not temporary, always-catchup is set on KVM_SET_TSC_KHZ and never
>> goes away.
> 
> Even if the guest is migrated (back) to the host which supports TSC
> scaling?

Ah right, in that case no.  But this hypercall does not have a CPUID 
bit, so the supported status can change on migration anyway.

Paolo


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2022-02-17 17:39 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-02-15 20:01 [PATCH v2] kvm: x86: Disable KVM_HC_CLOCK_PAIRING if tsc is in always catchup mode Anton Romanov
2022-02-16 14:14 ` Vitaly Kuznetsov
2022-02-17 15:41   ` Sean Christopherson
2022-02-17 16:09     ` Vitaly Kuznetsov
2022-02-17 17:14       ` Paolo Bonzini
2022-02-17 17:16         ` Vitaly Kuznetsov
2022-02-17 17:39           ` Paolo Bonzini

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.