Received: by 2002:a05:7412:d8a:b0:e2:908c:2ebd with SMTP id b10csp2801375rdg; Mon, 16 Oct 2023 15:49:24 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEyIZWeIRXttN+YuxMj9oCcSCusz6mWC2gKuZB6LOOHMZtBcncLW234CIezTPNqxlIdEnaH X-Received: by 2002:a17:90a:198b:b0:27c:fa71:4765 with SMTP id 11-20020a17090a198b00b0027cfa714765mr463377pji.33.1697496564012; Mon, 16 Oct 2023 15:49:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697496563; cv=none; d=google.com; s=arc-20160816; b=Sajzs094F2r8ruBtpOko5kZKl+NNBnkxv1keB+hed/15/waiDUJH5U0PbpMwMDC5GM qWDrLeOAlwSOj40UyCdoASiLNOrXoVz+nVwKrO8Q0cQ/Co020aWE9CQdODaaM88mGMc/ dWaWNb2VR92mz+NnBcfURJNWFI5bNjzywMQxK4wuNzpPTPtzhQxfDbKQy60jkgH0xS0W RwKHHBpTeH+phMg2ELjqvDUf456JdeixhopxXgjRbFdrD0hdId+WlSiufc7p765opRRx wnnMjjccNlgsYBjD89SgA4X8f0syLScRAdQzw2ttgFgzGy9T8XF67Wd0cJQgmWz+6Mca H5Mg== 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=84aTo4Tf+aZUy0HcQ/Zqt9HQ4tRBchQvuwbCa4iyIjE=; fh=c1fI0N9uFB/iuBAyCuI/hsjqFGAV1YTI7CTYhwF978Q=; b=Bi+uTjV7lzOEaVregVvjBP3BvZph5031tqt9g2eq1yp+Zz6/6fgycNuJTum/0shVde rK4+byDExK/y1u3E4gBfxZnhr9+uMCfzFY5IYQBnEQrslgGfTwm/69h8c7RBbCXqxreZ r5y6vJqS/3I8Qf57B5WKZP0reTl8DNHNwk4iXtA8uZIt6C8Qq2wArsKZ1t153klhSdH+ Crc6cNzeW/8b2VVg5qRUv66I0EvzHjdwFUkD+eo3JMRKgSLMJl7+syR/aRVkKEZHuPB/ 9zh1s5Y8KJ1urp3uiBxNpLt4r9FHXAmIuCleeXb3a9+r+Am5VzQ05GZtVUIBnAYPbQ/n t7YA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="s/nWiyME"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 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 snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id lb17-20020a17090b4a5100b002700bcdbff1si7663143pjb.154.2023.10.16.15.49.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 Oct 2023 15:49:23 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="s/nWiyME"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 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 snail.vger.email (Postfix) with ESMTP id 1053E80267C0; Mon, 16 Oct 2023 15:49:23 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233787AbjJPWsc (ORCPT + 99 others); Mon, 16 Oct 2023 18:48:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45628 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233333AbjJPWsa (ORCPT ); Mon, 16 Oct 2023 18:48:30 -0400 Received: from mail-pf1-x44a.google.com (mail-pf1-x44a.google.com [IPv6:2607:f8b0:4864:20::44a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 425D2F9 for ; Mon, 16 Oct 2023 15:48:23 -0700 (PDT) Received: by mail-pf1-x44a.google.com with SMTP id d2e1a72fcca58-6b2018a11e2so3758386b3a.2 for ; Mon, 16 Oct 2023 15:48:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1697496502; x=1698101302; 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=84aTo4Tf+aZUy0HcQ/Zqt9HQ4tRBchQvuwbCa4iyIjE=; b=s/nWiyMENdy1hEDXEZO9QIdBcRB83YoiDaOJjVCPukUwiRsZ93UEmU/M18N246xNUC TxzzQQQ+5f50/G8G1sjra8hC217UR+/ZugynQQtAmwx5bzkdRmWRyiLL8gclO52W9ORA x+Y4jDXKpvqhJRSRYuq3GS8x39BxBefpwHRQMtfeROUX+zQoUAEanTiO8d3zvgUfoztQ rFUnOuG6fxdcM4dyhv9WJpkrvCG+XqGHBvXJloxnKNeO6/a7XHpG9HSVPRDepQubgaFw Q2GNJYD9UviNF7k82Lmc2lHlksAMoTw+W8dcTqLuI+jfYFRgcKME0RWtzK4GpvMmcFn+ oV5A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697496502; x=1698101302; 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=84aTo4Tf+aZUy0HcQ/Zqt9HQ4tRBchQvuwbCa4iyIjE=; b=Tb1oSsBDxnY9VTlimZrIdschr+ymjMbJ/c2sLrRIp8xkdBtPmi7RbmY6LU+m3I4VnL WH7HJ1g45bAKBoJyuCK0LofrnAAQnNJsRNDecjkXsnUJs1jdO0KosTSWPOExYhBHHn1C 7nktqMSpV16RauSWow77quyh1htApkQil0GY8mb8+R5JYzjZMqwQhn9vhaNf6N9UTeso DobLV1+6soEc0uRH2THky3HAcRJwZDS5IhEo2W+4dPiw3wJ+UPx9URON2ZIhCFTdRERL I0ulwlNMqxGDJsFWg6/DqxqRBX97en0u3/HniNNbpud+Rsoq3wYKuIyZh1Vw9fo9Qqdn jYAg== X-Gm-Message-State: AOJu0Yw+k6i87Tame1BxdI0PdpAS07GnRJ/8zAmr1Ad9SeWLE+WdLjp7 tecDfCdHH7tlPaFZ4AWfTgg1d+49SxM= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6a00:d5b:b0:68e:24fe:d9a with SMTP id n27-20020a056a000d5b00b0068e24fe0d9amr13672pfv.2.1697496502682; Mon, 16 Oct 2023 15:48:22 -0700 (PDT) Date: Mon, 16 Oct 2023 15:48:21 -0700 In-Reply-To: Mime-Version: 1.0 References: <8f3493ca4c0e726d5c3876bb7dd2cfc432d9deaa.camel@infradead.org> <993cc7f9-a134-8086-3410-b915fe5db7a5@oracle.com> <03afed7eb3c1e5f4b2b8ecfd8616ae5c6f1819e9.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=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,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 lindbergh.monkeyblade.net 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 (snail.vger.email [0.0.0.0]); Mon, 16 Oct 2023 15:49:23 -0700 (PDT) On Mon, Oct 16, 2023, Dongli Zhang wrote: > Hi Sean, > > On 10/16/23 11:49, Sean Christopherson wrote: > > Compile tested only, but the below should fix the vCPU hotplug case. Then > > someone (not me) just needs to figure out why kvm_xen_shared_info_init() forces > > a masterclock update. > > > > I still think we should clean up the periodic sync code, but I don't think we > > need to periodically sync the masterclock. > > This looks good to me. The core idea is to not update master clock for the > synchronized cases. > > > How about the negative value case? I see in the linux code it is still there? See below. > (It is out of the scope of my expectation as I do not need to run vCPUs in > different tsc freq as host) > > Thank you very much! > > Dongli Zhang > > > > > --- > > arch/x86/kvm/x86.c | 29 ++++++++++++++++------------- > > 1 file changed, 16 insertions(+), 13 deletions(-) > > > > diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c > > index c54e1133e0d3..f0a607b6fc31 100644 > > --- a/arch/x86/kvm/x86.c > > +++ b/arch/x86/kvm/x86.c > > @@ -2510,26 +2510,29 @@ static inline int gtod_is_based_on_tsc(int mode) > > } > > #endif > > > > -static void kvm_track_tsc_matching(struct kvm_vcpu *vcpu) > > +static void kvm_track_tsc_matching(struct kvm_vcpu *vcpu, bool new_generation) > > { > > #ifdef CONFIG_X86_64 > > - bool vcpus_matched; > > struct kvm_arch *ka = &vcpu->kvm->arch; > > struct pvclock_gtod_data *gtod = &pvclock_gtod_data; > > > > - vcpus_matched = (ka->nr_vcpus_matched_tsc + 1 == > > - atomic_read(&vcpu->kvm->online_vcpus)); > > + /* > > + * To use the masterclock, the host clocksource must be based on TSC > > + * and all vCPUs must have matching TSCs. Note, the count for matching > > + * vCPUs doesn't include the reference vCPU, hence "+1". > > + */ > > + bool use_master_clock = (ka->nr_vcpus_matched_tsc + 1 == > > + atomic_read(&vcpu->kvm->online_vcpus)) && > > + gtod_is_based_on_tsc(gtod->clock.vclock_mode); > > > > /* > > - * Once the masterclock is enabled, always perform request in > > - * order to update it. > > - * > > - * In order to enable masterclock, the host clocksource must be TSC > > - * and the vcpus need to have matched TSCs. When that happens, > > - * perform request to enable masterclock. > > + * Request a masterclock update if the masterclock needs to be toggled > > + * on/off, or when starting a new generation and the masterclock is > > + * enabled (compute_guest_tsc() requires the masterclock snaphot to be > > + * taken _after_ the new generation is created). > > */ > > - if (ka->use_master_clock || > > - (gtod_is_based_on_tsc(gtod->clock.vclock_mode) && vcpus_matched)) > > + if ((ka->use_master_clock && new_generation) || > > + (ka->use_master_clock != use_master_clock)) > > kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu); > > > > trace_kvm_track_tsc(vcpu->vcpu_id, ka->nr_vcpus_matched_tsc, > > @@ -2706,7 +2709,7 @@ static void __kvm_synchronize_tsc(struct kvm_vcpu *vcpu, u64 offset, u64 tsc, > > vcpu->arch.this_tsc_nsec = kvm->arch.cur_tsc_nsec; > > vcpu->arch.this_tsc_write = kvm->arch.cur_tsc_write; > > > > - kvm_track_tsc_matching(vcpu); > > + kvm_track_tsc_matching(vcpu, !matched); If my analysis of how the negative timestamp occurred is correct, the problematic scenario was if cur_tsc_nsec/cur_tsc_write were updated without a masterclock update. Passing !matched for @new_generation means that KVM will force a masterclock update if cur_tsc_nsec/cur_tsc_write are changed, i.e. prevent the negative timestamp bug.