From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pf1-f198.google.com (mail-pf1-f198.google.com [209.85.210.198]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E90EF3515EA for ; Wed, 12 Aug 2026 20:08:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.198 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786565327; cv=none; b=Bvg8oMCdoenBKt7OHIMbb290gT2GNkKK4aOAkTIMvmOPUwc8IghNphnPlNxlasCFXztDDkM45DaTXyA7scUTSnXq2Tgb7wqzJKphbDvO3NuDij+ZNuvO3rEd/pBO/Cu4eHXbJjFR+6/t++3cII0/OJuoQ9jLkFVA4PQ+6/e/uA8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786565327; c=relaxed/simple; bh=R4a9X20wgVE3GYY5k7tJ7PMK4SWWCo/b8w6Nu1+bL4c=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Content-Type; b=hj/raMyfC4o7e9fO1+Bb3oqw5GJWeAkgpP3XCjj1yM4QiaAYw/XRsIcl/Re7h+trr8MtRIlQGkyPpeCQI8p5zLlihNOVyDC/6HifH3sfvvh8CZvXh132vuSGKYwZLSj64IXQ2iQdp+bPEb/x9PWuuUSYVCggo9DVQnfTy/6hwe4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=D9GLC0+y; arc=none smtp.client-ip=209.85.210.198 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="D9GLC0+y" Received: by mail-pf1-f198.google.com with SMTP id d2e1a72fcca58-84e024d2129so2097310b3a.2 for ; Wed, 12 Aug 2026 13:08:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1786565325; x=1787170125; darn=vger.kernel.org; h=content-type:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=/e1iLgIlxCuChfXNbN4KEooe5yYz2uaYvUPHL6sMaos=; b=D9GLC0+yAU/4v6altYnSo6I+1VTt0RHma/GC8zMu2geUMjzLda3gZKjtqnOIC0iI60 jJWJxCquapNvmC5hjFZyZ3Wz3SlamzaqCB0bWtS6HuSZnV4Zp6YQAUWcSUR8Z9341vA5 D5sGXMt2RPaf4D8TZfMIWDwUm+kks+gNkswEdIj3+M6f6AsuowXhvAMavRppHKNnOXKc xgROW/6VdHMh+cjVFplaaHMlMGIA44gaCcxFRQCNjTXysXdMNWqzcOuaGTkW6p438jCU Zclrr/sR41U9XJdN5yNHOZZ1FLNoXqGS675oe8d4aa3kIilguMCsV2VlubJQ5B7gV2X+ RpWg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1786565325; x=1787170125; h=content-type: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:content-type; bh=/e1iLgIlxCuChfXNbN4KEooe5yYz2uaYvUPHL6sMaos=; b=UfcvaxAk9E/7LY5A6xnWtajlVNXyHqQOln9+EmzSfOvQkHi5u+WSVk1jwveLur1tSh 0GGjFITuKG2htlqs9NC+tvmQaGjKWSBrxrCsPZmwW/kIpX3vWcXOxjnaSAkGZg1WpZcH /mmraqRqcFywvgyUE5+iE4TFobKlgjeWYKq2JXWqdMPBF3VvlgKtCRoeVFzgen8gHG5L O7MZtgbY9LqfQxBeoSKubsi/1kYu1Q/Xyn6Y6NPdCQJQbr9IkbTKF+Z4saBUU0et304k IfqHnDiDIMdUZXVOeh75V4Mvh/nvKM3e+gHOM1eUXdRIF0/c7Vdhc3DKB5sWg+SfQtPj ipWg== X-Forwarded-Encrypted: i=1; AHgh+RqERQPE4ApkjeHkVq+DbZBO5utQgdlN/VsSRlAt9b6KOVVnX2yYS+n+1dMy5yp79G/ywj21LyrPbgD8Q7A=@vger.kernel.org X-Gm-Message-State: AOJu0Ywe7H2bfspb4ltTuMoqekYHFWAfLBn/f9MKJrTo1hvG/D7cDDUD NLUC4dmIdcVrtGsI6tu1RKqdJ80iQdKlxXfDNkljVXH8vOQPjapCaAVjFEFNl6PsfxS0ovl0wsU tp+kljg== X-Received: from pfrg3.prod.google.com ([2002:aa7:9f03:0:b0:84f:a7f2:3113]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:4207:b0:848:4faa:480b with SMTP id d2e1a72fcca58-84fc6cdd974mr554619b3a.12.1786565325119; Wed, 12 Aug 2026 13:08:45 -0700 (PDT) Date: Wed, 12 Aug 2026 13:08:44 -0700 In-Reply-To: <20260810225500.869288-3-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260810225500.869288-1-seanjc@google.com> <20260810225500.869288-3-seanjc@google.com> Message-ID: Subject: Re: [PATCH v9 02/21] KVM: x86: Improve accuracy of KVM clock when TSC scaling is in force From: Sean Christopherson To: Paolo Bonzini , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Paul Durrant , David Woodhouse , Dongli Zhang Content-Type: text/plain; charset="us-ascii" On Mon, Aug 10, 2026, Sean Christopherson wrote: > From: David Woodhouse > > The kvm_guest_time_update() function scales the host TSC frequency to > the guest's using kvm_scale_tsc() and the v->arch.l1_tsc_scaling_ratio > scaling ratio previously calculated for that vCPU. Then calculates the > scaling factors for the KVM clock itself based on that guest TSC > frequency. > > However, it uses kHz as the unit when scaling, and then multiplies by > 1000 only at the end. > > With a host TSC frequency of 3000MHz and a guest set to 2500MHz, the > result of kvm_scale_tsc() will actually come out at 2,499,999kHz. So > the KVM clock advertised to the guest is based on a frequency of > 2,499,999,000 Hz. > > By using Hz as the unit from the beginning, the KVM clock would be based > on a more accurate frequency of 2,499,999,999 Hz in this example. > > Use u64 for the hw_tsc_hz field since an unsigned int would overflow for > TSC frequencies above 4GHz. Use div_u64() for the Xen CPUID leaf to > play nice with 32-bit kernels. > > Fixes: 78db6a503796 ("KVM: x86: rewrite handling of scaled TSC for kvmclock") > Reviewed-by: Paul Durrant > Signed-off-by: David Woodhouse > Tested-by: Dongli Zhang > Signed-off-by: Sean Christopherson > --- > arch/x86/include/asm/kvm_host.h | 2 +- > arch/x86/kvm/cpuid.c | 1 + > arch/x86/kvm/x86.c | 18 ++++++++++-------- > 3 files changed, 12 insertions(+), 9 deletions(-) > > diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h > index 283847619ff8..6ddc988576d0 100644 > --- a/arch/x86/include/asm/kvm_host.h > +++ b/arch/x86/include/asm/kvm_host.h > @@ -864,7 +864,7 @@ struct kvm_vcpu_arch { > gpa_t time; > s8 pvclock_tsc_shift; > u32 pvclock_tsc_mul; > - unsigned int hw_tsc_khz; > + u64 hw_tsc_hz; >From https://sashiko.dev/#/patchset/20260810225500.869288-1-seanjc%40google.com: : > diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h : > index b517257a63153..099c3da20547d 100644 : > --- a/arch/x86/include/asm/kvm_host.h : > +++ b/arch/x86/include/asm/kvm_host.h : > @@ -1004,7 +1004,7 @@ struct kvm_vcpu_arch { : > gpa_t time; : > s8 pvclock_tsc_shift; : > u32 pvclock_tsc_mul; : > - unsigned int hw_tsc_khz; : > + u64 hw_tsc_hz; : > struct gfn_to_pfn_cache pv_time; : > /* set guest stopped flag in pvclock flags field */ : > bool pvclock_set_guest_stopped_request; : : The commit message mentions using div_u64() for the Xen CPUID leaf, but is : the actual update to kvm_cpuid() missing here? : : It appears kvm_cpuid() still references the old field name: : : arch/x86/kvm/cpuid.c:kvm_cpuid() { : ... : *eax = vcpu->arch.hw_tsc_khz; : ... : } : : Since the field was renamed to hw_tsc_hz in struct kvm_vcpu_arch, will this : cause a build failure when compiling for x86? The changelog is stale. This will be applied on top of commit 7d3bd21e457b ("KVM: x86: Remove runtime Xen TSC frequency CPUID update") that's sitting in kvm-x86/clocks. I'll drop the stale sentence from the changelog when applying.