Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp3601554pxu; Tue, 8 Dec 2020 17:01:00 -0800 (PST) X-Google-Smtp-Source: ABdhPJyV0zLd1F68Xd6CPlDkoIFigzHoDlBProOZiyBvtOxGnTuqIS4KdZQprqaEcXFPQdEufAwa X-Received: by 2002:a17:906:60c8:: with SMTP id f8mr49394ejk.14.1607475659765; Tue, 08 Dec 2020 17:00:59 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1607475659; cv=none; d=google.com; s=arc-20160816; b=tyoD5WjZG4je/3CZBpTlrjlYYZjWUjxyYlQi+CeQCWUGV/mHXcdRhk+dm/P6aq+93d vt/4JCd9k10xZaqjdzKKuv8FLwjdj28VBiUT3FkKe75ekdg4/XKOW2XQ6gdeZke92ni1 +qRhEFYPh9DAvJL6tu4yQjwSPkYLavZuZwFLBTuy2+6XdGe9MCsQfonv+DLdMeMyDy+5 HKVwbSLbSKad5O5Sd/+X+K647Ip372dtXFIMXShhEgjDjFj05Obvx4BbhglfCCo7uPMu iVGjnUrU01CgsRMDHff3vRziYafHWbvbGdc6gRIjtZjyaBKCnztf1/44ZR41fzno0isk lLGQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=1ocH5W3W6/BNUy9ovonqCXcdd/rBC0yAl/SfS8e31LM=; b=EcwV8s85lF5cWPOsFLst4eldh1H7wY0pkQfL+rxetJSdQ7FI1A4GxKYlXnXJHGrhfK 8fVEWpJnqf01D+vFv6C03lcSjbUviqEBBSWV4nvJCSUgvnsB1Yjl3e7rHT/t3d+Pg2oA PHcYbPXS+BtgB/0htT6p09TKnelHUGLh3xFahvXBunQzJlzFhNGZxRmh2xnwKaW/DDIH eDL77PhDuD3zRVCHokVWy2fYIRuqaK96NVRS6sVo6IlYvevVgROJC5SA/Upo8976r6Fg Z05xmVIFMWW9Hu3SW/aXR/ZyfwxgSAFu1ClPMa5PUOmTA7EOLbo8IihJm0Qc340q91u0 tp1A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=DkDAjNbi; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id g7si81763ejh.224.2020.12.08.17.00.35; Tue, 08 Dec 2020 17:00:59 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=DkDAjNbi; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729849AbgLHOZQ (ORCPT + 99 others); Tue, 8 Dec 2020 09:25:16 -0500 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:27206 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729759AbgLHOZQ (ORCPT ); Tue, 8 Dec 2020 09:25:16 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1607437429; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=1ocH5W3W6/BNUy9ovonqCXcdd/rBC0yAl/SfS8e31LM=; b=DkDAjNbiKX4z5iMTdN5El/2EmR+r38/bt+2145csPjOn/PjCO0PjuK1ExN62cn0adHZGIG 3ZnFN4OL2davyTeUpXIHSfYUlywJEQ4f3VyLAVKoWbAlU8ZddfW0tz0wGo88hyzXvNygcS I5hUzI/YprPv5B2Dq09miKmU9ylSX7A= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-217-aK6QfRXaO3m8dgajqCgvOA-1; Tue, 08 Dec 2020 09:23:45 -0500 X-MC-Unique: aK6QfRXaO3m8dgajqCgvOA-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 28E2E107ACE4; Tue, 8 Dec 2020 14:23:43 +0000 (UTC) Received: from fuller.cnet (ovpn-112-8.gru2.redhat.com [10.97.112.8]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 53D7760877; Tue, 8 Dec 2020 14:23:42 +0000 (UTC) Received: by fuller.cnet (Postfix, from userid 1000) id 95B74416CD79; Mon, 7 Dec 2020 20:11:27 -0300 (-03) Date: Mon, 7 Dec 2020 20:11:27 -0300 From: Marcelo Tosatti To: Andy Lutomirski Cc: Maxim Levitsky , Thomas Gleixner , kvm@vger.kernel.org, "H. Peter Anvin" , Paolo Bonzini , Jonathan Corbet , Jim Mattson , Wanpeng Li , "open list:KERNEL SELFTEST FRAMEWORK" , Vitaly Kuznetsov , Sean Christopherson , open list , Ingo Molnar , "maintainer:X86 ARCHITECTURE (32-BIT AND 64-BIT)" , Joerg Roedel , Borislav Petkov , Shuah Khan , Andrew Jones , Oliver Upton , "open list:DOCUMENTATION" Subject: Re: [PATCH v2 1/3] KVM: x86: implement KVM_{GET|SET}_TSC_STATE Message-ID: <20201207231127.GB27492@fuller.cnet> References: <636fecc20b0143128b484f159ff795ff65d05b82.camel@redhat.com> <885C1725-B479-47F6-B08D-A7181637A80A@amacapital.net> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <885C1725-B479-47F6-B08D-A7181637A80A@amacapital.net> User-Agent: Mutt/1.10.1 (2018-07-13) X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Dec 07, 2020 at 10:04:45AM -0800, Andy Lutomirski wrote: > > > On Dec 7, 2020, at 9:00 AM, Maxim Levitsky wrote: > > > > On Mon, 2020-12-07 at 08:53 -0800, Andy Lutomirski wrote: > >>>> On Dec 7, 2020, at 8:38 AM, Thomas Gleixner wrote: > >>> > >>> On Mon, Dec 07 2020 at 14:16, Maxim Levitsky wrote: > >>>>> On Sun, 2020-12-06 at 17:19 +0100, Thomas Gleixner wrote: > >>>>> From a timekeeping POV and the guests expectation of TSC this is > >>>>> fundamentally wrong: > >>>>> > >>>>> tscguest = scaled(hosttsc) + offset > >>>>> > >>>>> The TSC has to be viewed systemwide and not per CPU. It's systemwide > >>>>> used for timekeeping and for that to work it has to be synchronized. > >>>>> > >>>>> Why would this be different on virt? Just because it's virt or what? > >>>>> > >>>>> Migration is a guest wide thing and you're not migrating single vCPUs. > >>>>> > >>>>> This hackery just papers over he underlying design fail that KVM looks > >>>>> at the TSC per vCPU which is the root cause and that needs to be fixed. > >>>> > >>>> I don't disagree with you. > >>>> As far as I know the main reasons that kvm tracks TSC per guest are > >>>> > >>>> 1. cases when host tsc is not stable > >>>> (hopefully rare now, and I don't mind making > >>>> the new API just refuse to work when this is detected, and revert to old way > >>>> of doing things). > >>> > >>> That's a trainwreck to begin with and I really would just not support it > >>> for anything new which aims to be more precise and correct. TSC has > >>> become pretty reliable over the years. > >>> > >>>> 2. (theoretical) ability of the guest to introduce per core tsc offfset > >>>> by either using TSC_ADJUST (for which I got recently an idea to stop > >>>> advertising this feature to the guest), or writing TSC directly which > >>>> is allowed by Intel's PRM: > >>> > >>> For anything halfways modern the write to TSC is reflected in TSC_ADJUST > >>> which means you get the precise offset. > >>> > >>> The general principle still applies from a system POV. > >>> > >>> TSC base (systemwide view) - The sane case > >>> > >>> TSC CPU = TSC base + TSC_ADJUST > >>> > >>> The guest TSC base is a per guest constant offset to the host TSC. > >>> > >>> TSC guest base = TSC host base + guest base offset > >>> > >>> If the guest want's this different per vCPU by writing to the MSR or to > >>> TSC_ADJUST then you still can have a per vCPU offset in TSC_ADJUST which > >>> is the offset to the TSC base of the guest. > >> > >> How about, if the guest wants to write TSC_ADJUST, it can turn off all paravirt features and keep both pieces? > >> > > > > This is one of the things I had in mind recently. > > > > Even better, we can stop advertising TSC_ADJUST in CPUID to the guest > > and forbid it from writing it at all. > > Seems reasonable to me. > > It also seems okay for some MSRs to stop working after the guest enabled new PV timekeeping. > > I do have a feature request, though: IMO it would be quite nifty if the new kvmclock structure could also expose NTP corrections. In other words, if you could expose enough info to calculate CLOCK_MONOTONIC_RAW, CLOCK_MONOTONIC, and CLOCK_REALTIME, then we could have paravirt NTP. Hi Andy, Any reason why drivers/ptp/ptp_kvm.c does not work for you? > Bonus points if whatever you do for CLOCK_REALTIME also exposes leap seconds in a race free way :). But I suppose that just exposing TAI and letting the guest deal with the TAI - UTC offset itself would get the job done just fine.