From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id E6371C4320A for ; Tue, 3 Aug 2021 07:59:27 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id CAB2F60EC0 for ; Tue, 3 Aug 2021 07:59:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234385AbhHCH7h (ORCPT ); Tue, 3 Aug 2021 03:59:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46048 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234232AbhHCH7f (ORCPT ); Tue, 3 Aug 2021 03:59:35 -0400 Received: from mail-qt1-x84a.google.com (mail-qt1-x84a.google.com [IPv6:2607:f8b0:4864:20::84a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F1D01C06175F for ; Tue, 3 Aug 2021 00:59:24 -0700 (PDT) Received: by mail-qt1-x84a.google.com with SMTP id m2-20020ac807c20000b0290269bd8044e1so12496866qth.10 for ; Tue, 03 Aug 2021 00:59:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:message-id:mime-version:subject:from:to:cc; bh=Fxd607RMBzP4aoVO0IekhXmHo+nn/xa9qFfPGrfYJnk=; b=YymPx+mWp1zUbGTLO5o8137nsca5qm9tyeMBp6mwVCeFmc2hPAVp6FRtc/ojTVbd0e bTJkElcc0C6ssNMxn8f1pPJx08ZJTZNOWD/QHci2jOnYQ4C9P5J7xnODtAwpF77xNzB0 TIdSAXiFgg300g+h+io9WAoNmjFXP4l1QYqCB/m42HWP5vCrPXgQNK44A+jsjNegQwFn 4CWG/kdVsURaNsXjonrIEsrPpzfl2ad18OFY+e7omyKmYqhWJ5HeVPhW68yaZszdfgDo Hjd9A6DbB51Z4zL+7X+KAJITtV5AeBVr74YqKV56/qV1Tr/RpFvnsxvLGKEV5ZUQ6aTL 8PEA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc; bh=Fxd607RMBzP4aoVO0IekhXmHo+nn/xa9qFfPGrfYJnk=; b=Y9LMwV181cjyaQrLDQzseaC3vuk5Zxleuk/G57k6+h13U0RhJtOVnVZ1qgEzfdhWCZ 0kDgC3QBlWXFnr2vHHJeffAhdP97NCW+CnvIr9EoOW5Su38eK5p9yv4joli3NG1NDVW3 BBcnM0WZ9C078RtWGAsuoa3m3GQ/wtHLwJ4lOE3gZXbGDFILtDRBtqz5B0hW8H8Y+hu4 O5PaFvb3YIeSuThQdjJHwIckIsvbFp/XBMcpyrmeDlIMl3HEiYkPT3psZH1pcTIXYb4b JLtzCj8zD9APKiOX+P5+wuNMpamLCsVUILqzWayIVGojMidr2DgcQkyNqE+GVcBVW4n1 bppQ== X-Gm-Message-State: AOAM532p34dy11VHvKBU8U3zB5UnilOWm8o5r3wBt/tPvVJGCIucVstl IwrKM8q6+aSdTPhomtmO6p/emIR/MWE9Sg== X-Google-Smtp-Source: ABdhPJysqvn+LjS2ao5Iec7fgO+cxbswRHEKmAV0xZsBvKypSr1bKig1Z1zbBeJ2hUtpYyWnPao0H7Tj6P3tAQ== X-Received: from suleiman1.tok.corp.google.com ([2401:fa00:8f:203:7e55:6c56:df0d:f664]) (user=suleiman job=sendgmr) by 2002:a05:6214:332:: with SMTP id j18mr20391348qvu.21.1627977564134; Tue, 03 Aug 2021 00:59:24 -0700 (PDT) Date: Tue, 3 Aug 2021 16:59:14 +0900 Message-Id: <20210803075914.3070477-1-suleiman@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.32.0.554.ge1b32706d8-goog Subject: [PATCH] kvm,x86: Use the refined tsc rate for the guest tsc. From: Suleiman Souhlal To: Paolo Bonzini Cc: ssouhlal@freebsd.org, hikalium@chromium.org, senozhatsky@chromium.org, Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , Thomas Gleixner , Ingo Molnar , Borislav Petkov , x86@kernel.org, "H. Peter Anvin" , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Suleiman Souhlal Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Prior to this change, the initial tsc rate used by kvm would be the unrefined rate, instead of the refined rate that is derived later at boot and used for timekeeping. This can cause time to advance at different rates between the host and the guest. Signed-off-by: Suleiman Souhlal --- arch/x86/kvm/x86.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 4116567f3d44..1e59bb326c10 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -2199,6 +2199,7 @@ static atomic_t kvm_guest_has_master_clock = ATOMIC_INIT(0); #endif static DEFINE_PER_CPU(unsigned long, cpu_tsc_khz); +static DEFINE_PER_CPU(bool, cpu_tsc_khz_changed); static unsigned long max_tsc_khz; static u32 adjust_tsc_khz(u32 khz, s32 ppm) @@ -2906,6 +2907,14 @@ static int kvm_guest_time_update(struct kvm_vcpu *v) kvm_make_request(KVM_REQ_CLOCK_UPDATE, v); return 1; } + /* + * Use the refined tsc_khz instead of the tsc_khz at boot (which was + * not refined yet when we got it), if the tsc frequency hasn't changed. + * If the frequency does change, it does not get refined any further, + * so it is safe to use the one gotten from the notifiers. + */ + if (!__this_cpu_read(cpu_tsc_khz_changed)) + tgt_tsc_khz = tsc_khz; if (!use_master_clock) { host_tsc = rdtsc(); kernel_ns = get_kvmclock_base_ns(); @@ -8086,6 +8095,8 @@ static void tsc_khz_changed(void *data) khz = freq->new; else if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) khz = cpufreq_quick_get(raw_smp_processor_id()); + if (khz) + __this_cpu_write(cpu_tsc_khz_changed, true); if (!khz) khz = tsc_khz; __this_cpu_write(cpu_tsc_khz, khz); -- 2.32.0.554.ge1b32706d8-goog