Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp2523652pxu; Mon, 7 Dec 2020 08:41:11 -0800 (PST) X-Google-Smtp-Source: ABdhPJwu4T/PEL0RN+RVNumcFIZ8AtZnYbhRNCVSCiuyhe61ctjaozuMwQ4optIBwxeKUwPS6iVd X-Received: by 2002:a05:6402:c1c:: with SMTP id co28mr20788053edb.287.1607359271644; Mon, 07 Dec 2020 08:41:11 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1607359271; cv=none; d=google.com; s=arc-20160816; b=Ym35AL+owz3IOMozen6PXW1B8nDmwajvPHbvG2joKc/PIq7dQHL2ivOx2sb+Ayx7zN VbAM1kd7Wf9FDsIWKj6qcUrfJe4ShwVKI01x1XAwhIYelbuxxS2bwEhb301DSphc3IND jIbT0f5TSCIzUd8Z6uBLvjhSw9JzpATE/14ePJmkmLIe9xY+3Dq5vJBewwlUrhHbDLhf aErzXCA8DFUrwlv/BVirDbBlPjcfhx0cspUcpsmyUwYQRVS4+MIs6evsVgrou0wI6lf+ asgM0BlDFNn84b9OqDnxSMjAn/Kc3lFOruTZKGGdmBuRBu23yI0nF6oKBf3hM2zUPIjL byjQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:message-id:date:references :in-reply-to:subject:cc:to:dkim-signature:dkim-signature:from; bh=7l+UILr+JWHc8AnnfIEx57njnbogruA/YdmfJ48nOWQ=; b=KpRoaLmOHu86mU2np7yW5pTWk87VOeyJD/QkXzcUE4DfySyDvmlaAsyavkaYvgQNfg k8BDDqZWDb6WBMrXe3bHXwCbLpI4WAn3V3uilj21Nse94pGnxH/9fwM3Iu4v71m8Qm02 1A7O0oH/XCCxAGv+GeBU/YiZOBTplQTyoCtKZq/Xsa0/dxXIwNJeZHMk4uyMy7xIovLq p98IxVC5FunbXLlmYqeUkkO6T2MIWmUbQ8YLoAxFmBpoyPJ6KNGtmLCB9nSI9u85HAkg Nmg3b3Zsqd1V0D+zH8jHi/OweRsIVgkhwom6OHKAUDO9lLTMjrKiEH0ODrn5ys61CKyO 6JYw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=QEWCmfVg; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=Jl8yYrpC; 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=QUARANTINE dis=NONE) header.from=linutronix.de Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id hr22si6358230ejc.411.2020.12.07.08.40.38; Mon, 07 Dec 2020 08:41:11 -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=@linutronix.de header.s=2020 header.b=QEWCmfVg; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=Jl8yYrpC; 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=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726377AbgLGQjT (ORCPT + 99 others); Mon, 7 Dec 2020 11:39:19 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60410 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725781AbgLGQjT (ORCPT ); Mon, 7 Dec 2020 11:39:19 -0500 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DDBD2C061749; Mon, 7 Dec 2020 08:38:38 -0800 (PST) From: Thomas Gleixner DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1607359117; 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: in-reply-to:in-reply-to:references:references; bh=7l+UILr+JWHc8AnnfIEx57njnbogruA/YdmfJ48nOWQ=; b=QEWCmfVg9WQzZxZMjjUfHb5J5LLN4FF5IbRC778foF8tzrzme+wn/5z/gtWeGMRX3M/d0V 06fmWGoVGLJrU4SCAa2kQbzP6hoZvrOYOtVOTrch/w4572yosLo3+qcAmdLs/oFrdj9nX0 jvC0TiIEdN9b5Igp1mw/hR3D5TRAdcobKVaUHdK54ZCgFEDZrdUC4VHat/caLcAPxr4SKK Xt9scgAfUS3OTX9eRkTA+s8gB/tbxwxlzMtVTaDc+rs2hBI3G4kFp66JX0CaBioW8PxE5A 4jFVRqBZ4ohR1Mix2mfhqLCPPhtZ0qBiwbAoydPgyNmAIrhJFHysMKhY+1EgRA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1607359117; 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: in-reply-to:in-reply-to:references:references; bh=7l+UILr+JWHc8AnnfIEx57njnbogruA/YdmfJ48nOWQ=; b=Jl8yYrpCp9vwJobQHJttjXxAiqDTAdR0IBiu1qjmf4qFl/smdAMHpC8SOO7ort40g7LE1V YkLLDTF8x0m0JmCw== To: Maxim Levitsky , kvm@vger.kernel.org Cc: "H. Peter Anvin" , Paolo Bonzini , Jonathan Corbet , Jim Mattson , Wanpeng Li , "open list\:KERNEL SELFTEST FRAMEWORK" , Vitaly Kuznetsov , Marcelo Tosatti , 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 In-Reply-To: <1dbbeefc7c76c259b55582468ccd3aab35a6de60.camel@redhat.com> References: <20201203171118.372391-1-mlevitsk@redhat.com> <20201203171118.372391-2-mlevitsk@redhat.com> <87a6uq9abf.fsf@nanos.tec.linutronix.de> <1dbbeefc7c76c259b55582468ccd3aab35a6de60.camel@redhat.com> Date: Mon, 07 Dec 2020 17:38:36 +0100 Message-ID: <87a6up606r.fsf@nanos.tec.linutronix.de> MIME-Version: 1.0 Content-Type: text/plain Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. TSC guest CPU = TSC guest base + CPU TSC_ADJUST ==> TSC guest CPU = TSC host base + guest base offset + CPU TSC_ADJUST The normal and sane case is just TSC_ADJUST == 0. It's very cleanly decomposable. Thanks, tglx