Received: by 2002:ab2:1689:0:b0:1f7:5705:b850 with SMTP id d9csp355011lqa; Sat, 27 Apr 2024 05:23:04 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUjmd2NDF1/21zvGBHNqxURusouXCbKchJI7zeSwXqCuwVeikeCMc/Y3JU/aGxiq48YBXwVMFLkU2NHYcNs/qLiJK1Mao5GXLkLmUz3qA== X-Google-Smtp-Source: AGHT+IEfdbW+gS3AcESnE2qB01pqZkyoGynRsJf8RHxMgg1KiAgVGt7+3NrdSKckM84D8JkWTNBv X-Received: by 2002:a05:6a00:2d02:b0:6f3:f062:c09b with SMTP id fa2-20020a056a002d0200b006f3f062c09bmr1278848pfb.6.1714220584148; Sat, 27 Apr 2024 05:23:04 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714220584; cv=pass; d=google.com; s=arc-20160816; b=XuPv8Gz4Gd8h0nbatenJ5pZBsnuaxGHKNJHaUX2d978a2phtKR4agi998SBzGnATzp HqUW3rx9M1529du/8p6CLXunJgCvK6syscdUXhuRiXq1Evb2c4HnlBCKhx1E3KdjAaJj 7izy0hOdc8spsKWhjJ2TqJ5nVe7H/5TR8gNyofdH74MnvWB3udPR6uJBGtQIOguyynZN Pfwokx+8vd19bhdzXtiF+Cg1mt6ftLchj1eBUjGF7SXBXwKy6W0C4OwMw65V9TpZ+ylU gpdeup7M0q+a/OcR1LD0tIHOiFdi0zW92gC8vhCwEQeDe/z8v1YzBENlp/tUvwLghzS5 vNRQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=hPvcvGj1y+IOFqFEZQ+yWKdeGU+LYxE6KkMCutNBHnc=; fh=GxGcIBasQq2Gc/176mTrpEex7eS1coPy2FlYCD7Qc50=; b=ueVJqvhCV8Mt2f/XsUhUWvhsNH4pwQ82Di8hQx1BuH13978+yxjfYUFXlCUZvPr89U 0rYxFQfcA38htuxSxl5gawpAvUMDC/a329gXHX6wHRLulnxq1CVpnHr92SIPwc267wZ1 O7q1K488n6OUnOsFBJcO7zRyfA9UGBlJrjJHT51AD9uYCdPsiP7kYrgVv2UR/I6S1D/8 PzSwI665wv3I+2wK54dlA7JDwJbmTMaPu50Bg1RXpxiHkB9ZlAsANQTn4rUTUR+FzK9V 7pDLf/3/VD4kpFFORdOeb4I1pnwc0ERdsGJXrRcRoGFX309ZKvu6PufUqPLTcWTbKZTH zSYw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@infradead.org header.s=desiato.20200630 header.b=BWCD4+ZL; arc=pass (i=1 dkim=pass dkdomain=infradead.org); spf=pass (google.com: domain of linux-kernel+bounces-161026-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-161026-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id w1-20020a634901000000b005f073cdbc73si16551545pga.473.2024.04.27.05.23.03 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 27 Apr 2024 05:23:04 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-161026-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@infradead.org header.s=desiato.20200630 header.b=BWCD4+ZL; arc=pass (i=1 dkim=pass dkdomain=infradead.org); spf=pass (google.com: domain of linux-kernel+bounces-161026-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-161026-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id B0B1F28609C for ; Sat, 27 Apr 2024 11:22:59 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id BBB4A524AD; Sat, 27 Apr 2024 11:21:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="BWCD4+ZL" Received: from desiato.infradead.org (desiato.infradead.org [90.155.92.199]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 178734DA05; Sat, 27 Apr 2024 11:21:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=90.155.92.199 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714216888; cv=none; b=mP4eogpPEADROPbqqKe2QspclhxlAyPQu6BJABHU5OhvyVu3ut4EUEJn0b4ZFubES1w04O0eCQedyr6hHV8JNlXWtXGgC/GeEgnEh1x8muBe6DPWE+rWIwAZnmU4sY+fyxYwqlIVo1td47Uxk3tX5izcNHwWWv0ctadN3qowdbo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714216888; c=relaxed/simple; bh=ldxafWPK+uthBWvPqxKpJGFwoUJXBXL/6tlTeC52pLQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=rCd/io8obHVn9t/wQTWJa7Fza6U7+oqUruHQdvPWoHUPcn1JgWIf5A/TLk99OpJ15ZD95MWtEH5cbYOs0B3xcKmIBfpHcIj7FFmc+1yDGrMc0rldSGbfoAjwmRKwSgNhWqgjI3ICpto/AVxN4qAPnz6w1WcytPLcDLl4Qj8kEKY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org; spf=none smtp.mailfrom=desiato.srs.infradead.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b=BWCD4+ZL; arc=none smtp.client-ip=90.155.92.199 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=desiato.srs.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Sender:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description; bh=hPvcvGj1y+IOFqFEZQ+yWKdeGU+LYxE6KkMCutNBHnc=; b=BWCD4+ZLAlnVyvGB2nq9wPhDYe w1xCjHrsnTCmRIc9ev97eslzpHnc0glI9rxHSGHjV+OxRzqqK9+1WvK66V/wPcQFfLvHpA1VtHNh8 TKiGCo4T5dwxFyqJdhLAwJQEmtWR5xVSErBxzfJwu28sWri1m6qqyYhZVYEuNZxQbaIhVGlq4UpNH pgEMCgcOWdydAZcMTggWUZW9iPncXmLoKWCXA9EYAiXYAnBf69DwPy/ICqNR1J3IFYO/Nx7pAhoOB aEp6D0KoiSfrqntjXYBCZq8QdaFve7/N27+N7rGO1iFFMbpyPjd4Bfl8O1w0DxnRxGK9suVh4zZi+ QwKvkLfQ==; Received: from [2001:8b0:10b:1::ebe] (helo=i7.infradead.org) by desiato.infradead.org with esmtpsa (Exim 4.97.1 #2 (Red Hat Linux)) id 1s0g5i-0000000FeZe-0pZA; Sat, 27 Apr 2024 11:19:46 +0000 Received: from dwoodhou by i7.infradead.org with local (Exim 4.97.1 #2 (Red Hat Linux)) id 1s0g5f-000000002b6-3hKv; Sat, 27 Apr 2024 12:19:35 +0100 From: David Woodhouse To: kvm@vger.kernel.org Cc: Paolo Bonzini , Jonathan Corbet , Sean Christopherson , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Paul Durrant , Shuah Khan , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, Oliver Upton , Marcelo Tosatti , jalliste@amazon.co.uk, sveith@amazon.de, zide.chen@intel.com, Dongli Zhang Subject: [PATCH v2 02/15] KVM: x86: Improve accuracy of KVM clock when TSC scaling is in force Date: Sat, 27 Apr 2024 12:04:59 +0100 Message-ID: <20240427111929.9600-3-dwmw2@infradead.org> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240427111929.9600-1-dwmw2@infradead.org> References: <20240427111929.9600-1-dwmw2@infradead.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: David Woodhouse X-SRS-Rewrite: SMTP reverse-path rewritten from by desiato.infradead.org. See http://www.infradead.org/rpr.html 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 calcuates 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. Fixes: 78db6a503796 ("KVM: x86: rewrite handling of scaled TSC for kvmclock") Signed-off-by: David Woodhouse Reviewed-by: Paul Durrant --- arch/x86/include/asm/kvm_host.h | 2 +- arch/x86/kvm/x86.c | 17 +++++++++-------- arch/x86/kvm/xen.c | 2 +- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 01c69840647e..8440c4081727 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -887,7 +887,7 @@ struct kvm_vcpu_arch { gpa_t time; struct pvclock_vcpu_time_info hv_clock; - unsigned int hw_tsc_khz; + unsigned int hw_tsc_hz; struct gfn_to_pfn_cache pv_time; /* set guest stopped flag in pvclock flags field */ bool pvclock_set_guest_stopped_request; diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 2d2619d3eee4..23281c508c27 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -3215,7 +3215,8 @@ static void kvm_setup_guest_pvclock(struct kvm_vcpu *v, static int kvm_guest_time_update(struct kvm_vcpu *v) { - unsigned long flags, tgt_tsc_khz; + unsigned long flags; + uint64_t tgt_tsc_hz; unsigned seq; struct kvm_vcpu_arch *vcpu = &v->arch; struct kvm_arch *ka = &v->kvm->arch; @@ -3252,8 +3253,8 @@ static int kvm_guest_time_update(struct kvm_vcpu *v) /* Keep irq disabled to prevent changes to the clock */ local_irq_save(flags); - tgt_tsc_khz = get_cpu_tsc_khz(); - if (unlikely(tgt_tsc_khz == 0)) { + tgt_tsc_hz = get_cpu_tsc_khz() * 1000LL; + if (unlikely(tgt_tsc_hz == 0)) { local_irq_restore(flags); kvm_make_request(KVM_REQ_CLOCK_UPDATE, v); return 1; @@ -3288,14 +3289,14 @@ static int kvm_guest_time_update(struct kvm_vcpu *v) /* With all the info we got, fill in the values */ if (kvm_caps.has_tsc_control) - tgt_tsc_khz = kvm_scale_tsc(tgt_tsc_khz, - v->arch.l1_tsc_scaling_ratio); + tgt_tsc_hz = kvm_scale_tsc(tgt_tsc_hz, + v->arch.l1_tsc_scaling_ratio); - if (unlikely(vcpu->hw_tsc_khz != tgt_tsc_khz)) { - kvm_get_time_scale(NSEC_PER_SEC, tgt_tsc_khz * 1000LL, + if (unlikely(vcpu->hw_tsc_hz != tgt_tsc_hz)) { + kvm_get_time_scale(NSEC_PER_SEC, tgt_tsc_hz, &vcpu->hv_clock.tsc_shift, &vcpu->hv_clock.tsc_to_system_mul); - vcpu->hw_tsc_khz = tgt_tsc_khz; + vcpu->hw_tsc_hz = tgt_tsc_hz; kvm_xen_update_tsc_info(v); } diff --git a/arch/x86/kvm/xen.c b/arch/x86/kvm/xen.c index 5a83a8154b79..014048c22652 100644 --- a/arch/x86/kvm/xen.c +++ b/arch/x86/kvm/xen.c @@ -2273,7 +2273,7 @@ void kvm_xen_update_tsc_info(struct kvm_vcpu *vcpu) entry = kvm_find_cpuid_entry_index(vcpu, function, 2); if (entry) - entry->eax = vcpu->arch.hw_tsc_khz; + entry->eax = vcpu->arch.hw_tsc_hz / 1000; } void kvm_xen_init_vm(struct kvm *kvm) -- 2.44.0