Received: by 2002:a05:7412:d8a:b0:e2:908c:2ebd with SMTP id b10csp1270750rdg; Fri, 13 Oct 2023 16:27:55 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEajHRMV5TtogH2THCjW5BTITyzJ8+mKuh5R7kP2aapDNImvUA35jKbQ3doZw8Gy1ULfrVo X-Received: by 2002:a17:90a:345:b0:27d:2dde:597a with SMTP id 5-20020a17090a034500b0027d2dde597amr3951573pjf.10.1697239674700; Fri, 13 Oct 2023 16:27:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697239674; cv=none; d=google.com; s=arc-20160816; b=u8P+BH8TF9Qr9ox+WZLe8wqwYePkd229CjULZiKotsG5mOwL2YosK693lVn7v5+/jN iNbDBCwKfVcZp5c44vUSENBLNMIttdDjmfgjnTvizLp5uo4BS3xdLm5j6hj2kHaf7YSm g2BgoI7iR9AaWhTKGlLJe0/IZylu+k/0aCzeCw05T/JhtBQEzMF3Th4dCTS7Zz9NA3Kc gHYXOXTYErTM4oE/uE6ppWvTXH+DGsXu+19nGuN6DKXidTP7qsn1ABY3v3v6NgLcnIMO tz6xnaLPCVtKRfQtDoEBmV4374Xt4+UGxBP/EESpRxGkO94NrXURc8vICJm3j8EtYFKq b/9w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=ZxyOGXfEtIJtv+c8UKOk7f88Du3NTkDVIB8Ot0qjFrU=; fh=c1fI0N9uFB/iuBAyCuI/hsjqFGAV1YTI7CTYhwF978Q=; b=quqaKYom0CU5KzQjcSS4jLv9zJzpSHsOhwyfK7W0Z2nBew8Pwc0ndKmgoFdpYn/Sz3 ZYzc/Lgcmqw6f0a9Ljrr8eBihwgIUc4RW5wnPM9yM9aqG3DuKZrvDjnjOQsy0nz1MXS2 nQnSOD84qHHGJnSxM8cLJtufoUE0fxr7IgLQf7QegaI6yPu9j7T14w7qmRlQjOSL8jkv 9jXCNnDrTEhd6DMVttQ7th1fzF8CyzBXkKn41ELb1D8DJLtC0vDxTAsc684HjGYw9rTh 5qKrxUtoF690ByG92dcQK8brbVfE9VXsPl2nCxxosmKS2q2Sg+aKSK7C2ButOt2PfQWb hYmw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="dDPr/jcN"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id bz23-20020a056a02061700b0057748a05fcfsi5545887pgb.27.2023.10.13.16.27.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 Oct 2023 16:27:54 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="dDPr/jcN"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id 3F545806E3FD; Fri, 13 Oct 2023 16:27:03 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232374AbjJMX0v (ORCPT + 99 others); Fri, 13 Oct 2023 19:26:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49630 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232021AbjJMX0t (ORCPT ); Fri, 13 Oct 2023 19:26:49 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0534EC9 for ; Fri, 13 Oct 2023 16:26:48 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-d9a5a16fa94so2249927276.0 for ; Fri, 13 Oct 2023 16:26:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1697239607; x=1697844407; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=ZxyOGXfEtIJtv+c8UKOk7f88Du3NTkDVIB8Ot0qjFrU=; b=dDPr/jcNs5wnaiIeHyIdazDbfbBjJu6YU3t+MqEUQXdodtuWZhLpfUI3tF/4mL2zdG BKkYp83jM7dUXZQAfjDywOXqGAu4Zuopou8zaS4jfyULrrd5funZuUp/Vtw/67v3A7mh u9P8etvDvEcuO30Qz7Sj7mN/yjUleF44gD1UtPKbGTnHxPKw7H/4MQwq6ldJbLVn7+8E Hm7wVtd+yMEAK4/e100NfC/hJjJgRnWWxmxEXlIUUYtGEAqRzbtTpuvlcDaNb9ifQXny ZYn9685U8uGhNsZqPq2bQ3ca7q5vSRjew3kc1a8WvOn5a/WLsDwpM40F5n0L7gwDfDS0 gsmg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697239607; x=1697844407; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=ZxyOGXfEtIJtv+c8UKOk7f88Du3NTkDVIB8Ot0qjFrU=; b=PJBVlUupSjYhYfFKnHPlkhz1xSLrxLeJdzXCw7uNEZBnnbHl00vwpRdk5l9LtTQdkx UBLwXqOO8cL2HTTsi/2ivCMZQRUo6J5NFcWBpH0de3KwTOXwc3Wqt3U+AqV+pignuOL+ xPECiud1PMLQ7WzKPsYPhyXu8PswUA3zWt7gDlpPCaWC058q64y+LMoQEXyWKOwILm1G 4yDkKmWgUbHXarE43J70O8dApoWpmRCb38UwjWsr7Tc2SxqQq9Aca72NYp6/DSPKegzE rkM5ORu1oJP2Jqnxw9kcvQvvmwQCZahCLDErKQz2umBiBxgFj5ZelPy43laTJBbitSj1 q17g== X-Gm-Message-State: AOJu0Yw6FGYCdDIhNZ1mFq+No/pdori/mfNc9e/TFUSRwq2uTZUBDMJB GYbjg0F7khqug7FORqfMQaL0lpPUmIU= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6902:150b:b0:d9a:4400:d62c with SMTP id q11-20020a056902150b00b00d9a4400d62cmr64933ybu.4.1697239607269; Fri, 13 Oct 2023 16:26:47 -0700 (PDT) Date: Fri, 13 Oct 2023 16:26:45 -0700 In-Reply-To: Mime-Version: 1.0 References: <9975969725a64c2ba2b398244dba3437bff5154e.camel@infradead.org> <34057852-f6c0-d6d5-261f-bbb5fa056425@oracle.com> <8f3493ca4c0e726d5c3876bb7dd2cfc432d9deaa.camel@infradead.org> Message-ID: Subject: Re: [PATCH RFC 1/1] KVM: x86: add param to update master clock periodically From: Sean Christopherson To: Dongli Zhang Cc: David Woodhouse , Joe Jin , x86@kernel.org, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, pbonzini@redhat.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com Content-Type: text/plain; charset="us-ascii" X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Fri, 13 Oct 2023 16:27:03 -0700 (PDT) On Fri, Oct 13, 2023, Dongli Zhang wrote: > On 10/13/23 11:07, Sean Christopherson wrote: > > What if we add a module param to disable KVM's TSC synchronization craziness > > entirely? If we first clean up the peroidic sync mess, then it seems like it'd > > be relatively straightforward to let kill off all of the synchronization, including > > the synchronization of kvmclock to the host's TSC-based CLOCK_MONOTONIC_RAW. > > > > Not intended to be a functional patch... > > > > --- > > arch/x86/kvm/x86.c | 35 ++++++++++++++++++++++++++++++++--- > > 1 file changed, 32 insertions(+), 3 deletions(-) > > > > diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c > > index 5b2104bdd99f..75fc6cbaef0d 100644 > > --- a/arch/x86/kvm/x86.c > > +++ b/arch/x86/kvm/x86.c > > @@ -157,6 +157,9 @@ module_param(min_timer_period_us, uint, S_IRUGO | S_IWUSR); > > static bool __read_mostly kvmclock_periodic_sync = true; > > module_param(kvmclock_periodic_sync, bool, S_IRUGO); > > > > +static bool __read_mostly enable_tsc_sync = true; > > +module_param_named(tsc_synchronization, enable_tsc_sync, bool, 0444); > > + > > /* tsc tolerance in parts per million - default to 1/2 of the NTP threshold */ > > static u32 __read_mostly tsc_tolerance_ppm = 250; > > module_param(tsc_tolerance_ppm, uint, S_IRUGO | S_IWUSR); > > @@ -2722,6 +2725,12 @@ static void kvm_synchronize_tsc(struct kvm_vcpu *vcpu, u64 data) > > bool matched = false; > > bool synchronizing = false; > > > > + if (!enable_tsc_sync) { > > + offset = kvm_compute_l1_tsc_offset(vcpu, data); > > + kvm_vcpu_write_tsc_offset(vcpu, offset); > > + return; > > + } > > TBH, I do not like this idea for two reasons. > > 1. As a very primary part of my work is to resolve kernel issue, when debugging > any clock drift issue, it is really happy for me to see all vCPUs have the same > vcpu->arch.tsc_offset in the coredump or vCPU debugfs. > > This patch may lead to that different vCPUs added at different time have > different vcpu->arch.tsc_offset. The expectation is that *if* userspace disables TSC synchronization, then userespace would be responsible for setting the guest TSC offset directly. And disabling TSC synchronization would be fully opt-in, i.e. we'd fix the existing masterclock sync issues first. > 2. Suppose the KVM host has been running for long time, and the drift between > two domains would be accumulated to super large? (Even it may not introduce > anything bad immediately) That already happens today, e.g. unless the host does vCPU hotplug or is using XEN's shared info page, masterclock updates effectively never happen. And I'm not aware of a single bug report of someone complaining that kvmclock has drifted from the host clock. The only bug reports we have are when KVM triggers an update and causes time to jump from the guest's perspective. If the guest needs accurate timekeeping, it's all but guaranteed to be using NTP or an equivalent. I.e. the imprecision that's inherent in the pvclock ABI shouldn't be problematic for any guest that actually cares. > If the objective is to avoid masterclock updating, how about the below I copied > from my prior diagnostic kernel to help debug this issue. > > The idea is to never update master clock, if tsc is stable (and masterclock is > already used). That's another option, but if there are no masterclock updates, then it suffers the exact same (theoretical) problem as #2. And there are real downsides, e.g. defining when KVM would synchronize kvmclock with the host clock would be significantly harder, as we'd have to capture all the possible ways that KVM could (temporarily) disable the masterclock and start synchronizing.