Received: by 2002:a05:7412:f589:b0:e2:908c:2ebd with SMTP id eh9csp178446rdb; Tue, 31 Oct 2023 04:42:43 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEwUtKopel6v5JDft40n6X3Ot7dsDpgQWZGYkpA44HSeRz2nwt7UZOOO4tRrplUvmPqY2o6 X-Received: by 2002:a05:6a20:2d2a:b0:171:48a1:a85a with SMTP id g42-20020a056a202d2a00b0017148a1a85amr12023743pzl.23.1698752562943; Tue, 31 Oct 2023 04:42:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698752562; cv=none; d=google.com; s=arc-20160816; b=lLK/9qe+0f97jOhmwwc46EgZvCkmGw8aQ9nJM8zkGUXQ/24cwv1GF/reu7a0x4HY+O CyIAVu+kKDvtRjjVxDK7eYd5aV0sCu6VlJ/jnAMxuuOZCr7iFGMMu4oOpyHGfVg2ipb4 5j0+y873X83uoqMHXkAYr7onOW9fNxQ8QXtHQvXdDElEIS/VzQ7JoyyyNG8Am3QyyjOt Rr1BJuYn359wFXW+mrvvzzKJuTjrdpYzx2ODkowvr5xDb/kEadcasJAji31C3mfy8qG7 EWNhXJ+KlOpwglIrQfxKrVMLIZB1qSm8qvc6S+gSIiaR5lOWp+3oc24cenFoTwlG9RcH dw7w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:references:in-reply-to:user-agent:subject:cc:to:from :date:dkim-signature; bh=VlN8p3RfYXpXh6bENRI9S68YgJgpcAwvfutYsLKoRF8=; fh=LKl7yMChGU72rnjExto6Zy1tjO05zsSQxt3+zdI2f9A=; b=He81nOsLvCKPUKZLLkzvgfRzr7f4TwQdbNwG3eZV8cmVgxAeKM+LXWorC6A5EDH64u z6cWvJNYYiw0SagYgEZYfhMgjoxYb8nmLWwqzZrBrxPWqAUd6m+cpzWLSMLAC4dXmTkk 1HGzIsNGdkke4k00G3qsA4nbPb3A1i0yPHO1hpW1kOEA6fM0V2nm/nij8/Y0Wt3yE4bV at2m10IS8H46RZEF9kawx2/s2YlPkm29+iUUMZjQxWJMJP9i5P3BgSXNEq1s6ae9zpgq Tq+Xoj+D59dkiE6FHJrXhhiJUhAliJoMpHWaTrvuf1IRHzc5KHnmLdais+ZsOniQ+t3v wr7Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=desiato.20200630 header.b=iGQM8KHo; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from fry.vger.email (fry.vger.email. [2620:137:e000::3:8]) by mx.google.com with ESMTPS id u19-20020a056a00159300b0069023e4bca8si902876pfk.214.2023.10.31.04.42.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 31 Oct 2023 04:42:42 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) client-ip=2620:137:e000::3:8; Authentication-Results: mx.google.com; dkim=pass header.i=@infradead.org header.s=desiato.20200630 header.b=iGQM8KHo; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id 3F8D68023924; Tue, 31 Oct 2023 04:42:40 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344026AbjJaLme (ORCPT + 99 others); Tue, 31 Oct 2023 07:42:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34284 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344003AbjJaLmc (ORCPT ); Tue, 31 Oct 2023 07:42:32 -0400 Received: from desiato.infradead.org (desiato.infradead.org [IPv6:2001:8b0:10b:1:d65d:64ff:fe57:4e05]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2E45691; Tue, 31 Oct 2023 04:42:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Content-Transfer-Encoding:Content-Type :MIME-Version:Message-ID:References:In-Reply-To:Subject:CC:To:From:Date: Sender:Reply-To:Content-ID:Content-Description; bh=VlN8p3RfYXpXh6bENRI9S68YgJgpcAwvfutYsLKoRF8=; b=iGQM8KHo53UcuP/wjvDnraWCAV +cK8IC6VobUa7A1LGj5o1rYNKBzU7e2CPUH3yazhrqfKrj0odV/auJ8v2ZG7AUJmdviuvjozHRzVX a+nGB63sCxSFYXUBhpu20H37NPhNMWgCDQYSbl1oQ3DnYk8zUa7sW4gqRqv3LJJW+Y3unaQCUwGfl OqRWJAE5/GLny+lqvaOERSTGUUfCYazW0nBf40A7ACDIC0PLuz41sZEo1BAAd0S1mnHYgN68Fxv2X z1/JTXkulD1nAwKvg7y2/m2SzivuGJJ3al8MwwTsH8OOrrZNJmOlI40kI9i4Jmu36D4vHx3xlGOEw 6ttE7z6g==; Received: from [46.18.216.58] (helo=[127.0.0.1]) by desiato.infradead.org with esmtpsa (Exim 4.96 #2 (Red Hat Linux)) id 1qxn8W-004mxB-1n; Tue, 31 Oct 2023 11:42:22 +0000 Date: Tue, 31 Oct 2023 11:42:19 +0000 From: David Woodhouse To: paul@xen.org, Paul Durrant , kvm@vger.kernel.org, linux-kernel@vger.kernel.org CC: Sean Christopherson , Paolo Bonzini , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" Subject: Re: [PATCH] KVM: x86/xen: improve accuracy of Xen timers User-Agent: K-9 Mail for Android In-Reply-To: <1a679274-bbff-4549-a1ea-c7ea9f1707cc@xen.org> References: <96da7273adfff2a346de9a4a27ce064f6fe0d0a1.camel@infradead.org> <1a679274-bbff-4549-a1ea-c7ea9f1707cc@xen.org> Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-SRS-Rewrite: SMTP reverse-path rewritten from by desiato.infradead.org. See http://www.infradead.org/rpr.html X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.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 (fry.vger.email [0.0.0.0]); Tue, 31 Oct 2023 04:42:40 -0700 (PDT) On 31 October 2023 10:42:42 GMT, Paul Durrant wrot= e: >There is no documented ordering requirement on setting KVM_XEN_VCPU_ATTR_= TYPE_TIMER versus KVM_XEN_VCPU_ATTR_TYPE_VCPU_INFO or KVM_XEN_ATTR_TYPE_SHA= RED_INFO but kvm_xen_start_timer() now needs the vCPU's pvclock to be valid= =2E Should actually starting the timer not be deferred until then? (Or simp= ly add a check here and have the attribute setting fail if the pvclock is n= ot valid)=2E There are no such dependencies and I don't want there to be=2E That would = be the *epitome* of what my "if it needs documenting, fix it first" mantra = is intended to correct=2E The fact that this broke on migration because the hv_clock isn't set up ye= t, as we saw in our overnight testing, is just a bug=2E In my tree I've fix= ed it thus: index 63531173dad1=2E=2Ee3d2d63eef34 100644 --- a/arch/x86/kvm/xen=2Ec +++ b/arch/x86/kvm/xen=2Ec @@ -182,7 +182,7 @@ static void kvm_xen_start_timer(st ruct kvm_vcpu *vcpu, u64 guest_abs, * the absolute CLOCK_MONOTONIC time at which the timer should * fire=2E */ - if (vcpu->kvm->arch=2Euse_master_clock && + if (vcpu->arch=2Ehv_clock=2Eversion && vcpu->kvm-> arch=2Euse_master_clock && static_cpu_has(X86_FEATURE_CONSTANT_TSC)) { uint64_t host_tsc, guest_tsc; @@ -206,9 +206,23 @@ static void kvm_xen_start_timer(s truct kvm_vcpu *vcpu, u64 guest_abs, /* Calculate the guest kvmclock as the guest would do it=2E */ guest_tsc =3D kvm_read_l1_tsc(vcpu, host _tsc); - guest_now =3D __pvclock_read_cycles(&vcp u->arch=2Ehv_clock, guest_tsc); + guest_now =3D __pvclock_read_cycles(&vcp u->arch=2Ehv_clock, + gues t_tsc); } else { - /* Without CONSTANT_TSC, get_kvmclock_ ns() is the only option */ + /* + * Without CONSTANT_TSC, get_kvmclock_ ns() is the only option=2E + * + * Also if the guest PV clock hasn't b een set up yet, as is + * likely to be the case during migrat ion when the vCPU has + * not been run yet=2E It would be possi ble to calculate the + * scaling factors properly in that ca se but there's not much + * point in doing so=2E The get_kvmclock _ns() drift accumulates + * over time, so it's OK to use it at startup=2E Besides, on + * migration there's going to be a lit tle bit of skew in the + * precise moment at which timers fire anyway=2E Often they'll + * be in the "past" by the time the VM is running again after + * migration=2E + */ guest_now =3D get_kvmclock_ns(vcpu->kvm) ; kernel_now =3D ktime_get(); } -- 2=2E41=2E0 We *could* reset the timer when the vCPU starts to run and handles the KVM= _REQ_CLOCK_UPDATE event, but I don't want to for two reasons=2E Firstly, we just don't need that complexity=2E This approach is OK, as the= newly-added comment says=2E And we do need to fix get_kvmclock_ns() anyway= , so it should work fine=2E Most of this patch will still be useful as it u= ses a single TSC read and we *do* need to do that part even after all the k= vmclock brokenness is fixed=2E But the complexity on KVM_REQ_CLOCK_UPDATE i= sn't needed in the long term=2E Secondly, it's also wrong thing to do in the general case=2E Let's say KVM= does its thing and snaps the kvmclock backwards in time on a KVM_REQ_CLOCK= _UPDATE=2E=2E=2E do we really want to reinterpret existing timers against t= he new kvmclock? They were best left alone, I think=2E=20 =00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00= =00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00= =00=00=00=00=00 =00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00= =00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00= =00=00=00=00=00 =00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00= =00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00= =00=00=00=00=00 =00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00= =00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00= =00=00=00=00=00 =00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00= =00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00= =00=00=00=00=00 =00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00= =00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00= =00=00=00=00=00 =00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00= =00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00= =00=00=00=00=00 =00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00= =00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00= =00=00=00=00=00 =00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00= =00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00= =00=00=00=00=00 =00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00= =00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00= =00=00=00=00=00 =00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00= =00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00= =00=00=00=00=00 =00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00= =00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00= =00=00=00=00=00 =00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00= =00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00= =00=00=00=00=00 =00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00= =00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00= =00=00=00=00=00 =00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00= =00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00= =00=00=00=00=00 =00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00= =00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00= =00=00=00=00=00 =00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00= =00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00= =00=00=00=00=00 =00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00= =00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00= =00=00=00=00=00 =00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00= =00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00=00= =00=00=00=00=00