Received: by 2002:a05:6358:701b:b0:131:369:b2a3 with SMTP id 27csp2788732rwo; Mon, 24 Jul 2023 00:55:13 -0700 (PDT) X-Google-Smtp-Source: APBJJlH+TwvDyxTM9ngIj7lOhMsO1QGCtDS/Kj0bF31WwAR1JGLRL5dC4DdO7DwoBxPiLSX3U137 X-Received: by 2002:a05:6402:1a3c:b0:51e:2282:e1fc with SMTP id be28-20020a0564021a3c00b0051e2282e1fcmr6778556edb.6.1690185313338; Mon, 24 Jul 2023 00:55:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690185313; cv=none; d=google.com; s=arc-20160816; b=hLKuwbrpwe+gb//45YB1mFpx94ojnxShjHZAiYmnt1TtpGm91LEGvorOJ9ahOQBJqn C7O/I2wNmjgwh0tH2h8VpxfSZT10pRSwVSdOlJioT+UvVim0zSb/gCu8+Bw9ZWKh76py IRIwFdAKiqkR6xlZYN37TKrjBCT6a5Szi8WXqqQjDClPH0T9tX7B0zpMho7vb9EpL4Bw B+fSLveuLB3Cp37KRwTdUKIUZ5qmNRBuT1jtN1azeB/OtD25+ePbcw//wtPVr+9+sr/e Sqc8vgZymOv9/zUkgAoM+vfz5DYNXRmDNag4P4MHTezL4oIiliUIDxIQo022i5Avd3CO SnbA== 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:date:subject:cc:to:from:dkim-signature; bh=EDTSsXyhM0omkYQyX53gEEvKIjkVRX9tpymTOGcpwOQ=; fh=VMNKKuqpvFTTS/LM42GSv6RBx83h416+B+7NJG/XDEA=; b=oik9coZWu0/B02jK1YUmsRKQKKN9mnLUs9w5PvJD4hDXWk1W7r1LAWmFjLg0wZvVRI TmCJhG02SxXqjEHhwlXDgiBc08yrgHn1MKvAqtE2Ij95qz+532Npe6mC7xq5cUilQBJN rf/Yujw9eifizlhPe2WvBIT3k7SR2I9KlDUwJOhZ9/MuBsgxs8QCH7ilYM4rJWIMLt7g m7JtJ2ttseH1vo7DmAUrwycxZ3Q5rbmsB36M/n844PQoqP6F7hNnroZOhmQR6EgKwMA5 9NZlzuTHFJ9jn/3qsacSWx+Gj2Rmaxw2upajwV4+CgB/lEOgLi684NFJiOcijkYCBQeL nFgQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=eDtyE95H; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id w7-20020a056402128700b005221e49e4c6si2723025edv.299.2023.07.24.00.54.48; Mon, 24 Jul 2023 00:55:13 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=eDtyE95H; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231196AbjGXHfl (ORCPT + 99 others); Mon, 24 Jul 2023 03:35:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32884 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229499AbjGXHfi (ORCPT ); Mon, 24 Jul 2023 03:35:38 -0400 Received: from mail-qk1-x730.google.com (mail-qk1-x730.google.com [IPv6:2607:f8b0:4864:20::730]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2206DB9; Mon, 24 Jul 2023 00:35:37 -0700 (PDT) Received: by mail-qk1-x730.google.com with SMTP id af79cd13be357-76754b9eac0so373317485a.0; Mon, 24 Jul 2023 00:35:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1690184136; x=1690788936; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=EDTSsXyhM0omkYQyX53gEEvKIjkVRX9tpymTOGcpwOQ=; b=eDtyE95HreJTGndVUo26mno6lxe14TQaKM9C6h1Sh1yzgD96924Q4BFeW8fBEwx/KC wLdRECEVUywZKJ9MODAYrjPNxxmRiHk0jXj2Eid4m6Nt5hrBF+0gqKcl0bFy8QNMAHCt xPAD1JJrKy5izVH0Me4jqnz6OWFY4rj8LvqFwrz7F/LGXioJveDX5k9AkhyLYC+VQvB4 u+kbUbeadvb7DBdJatgZTTp0vEgg1goaZ8BesJTRYQJ9iqGemKOVkPTYSxKPmnZRpBNx PCUJ79MkOGFfWawtfAjfKRk/oqhyI1V07Ske9KDcalzvq8o3StHskuujBWLVeyKTNrnp bu3g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690184136; x=1690788936; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=EDTSsXyhM0omkYQyX53gEEvKIjkVRX9tpymTOGcpwOQ=; b=J5cAHLhLqxrwBnCCSihPps3EzrU59e6dMPpghrKvZrlcMXptAi6VercqaHIWLLcNk2 SK+S5RcCLqJhEhU3zceOgIGIwdPvEcdXMJLXsOMXXQZREM4Clse0Eug0T3ydEAfDNfmH mPOgOD8ZFdgr2REasyhHxodrfpIb+8fxpK8Mhh28KIzIjWSkwNBejpkyXa0OyOK38Wfr yNqDH8JRMWY0RAgMeqNYtlM8eywDQjfxGLPi6NDM9a0V2egO/YKSzl2L1z13zYfW68pQ iQJGWaHMa+romMhpAh2fvIgYrugOUjKJcgfdX8VmtSiu7hBl8/hg5Otr38YIh7SgtGTU DM9Q== X-Gm-Message-State: ABy/qLbNFqyDir0Aafq9aUpKW54XN9LRe6F/ZJAISj5YPQPJogEhxeXX 4EI9Qeabj8G79T+oawB+paU= X-Received: by 2002:a05:620a:4484:b0:767:81e5:566 with SMTP id x4-20020a05620a448400b0076781e50566mr8778382qkp.58.1690184136196; Mon, 24 Jul 2023 00:35:36 -0700 (PDT) Received: from localhost.localdomain ([103.7.29.32]) by smtp.gmail.com with ESMTPSA id w1-20020a17090a6b8100b00265cdfa3628sm8283147pjj.6.2023.07.24.00.35.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 24 Jul 2023 00:35:35 -0700 (PDT) From: Like Xu X-Google-Original-From: Like Xu To: Sean Christopherson Cc: Oliver Upton , Paolo Bonzini , kvm@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2] KVM: x86/tsc: Use calculated tsc_offset before matching the fist vcpu's tsc Date: Mon, 24 Jul 2023 15:35:16 +0800 Message-ID: <20230724073516.45394-1-likexu@tencent.com> X-Mailer: git-send-email 2.41.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham 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 From: Like Xu Avoid using kvm->arch.cur_tsc_offset until tsc sync is actually needed. When the vcpu is created for the first time, its tsc is 0, and KVM calculates its tsc_offset according to kvm_compute_l1_tsc_offset(). The userspace will then set the first non-zero tsc expected value for the first guest vcpu, at this time there is no need to play the tsc synchronize mechanism, the KVM should continue to write tsc_offset based on previously used kvm_compute_l1_tsc_offset(). If the tsc synchronization mechanism is incorrectly applied at this point, KVM will use the rewritten offset of the kvm->arch.cur_tsc_offset (on tsc_stable machines) to write tsc_offset, which is not in line with the expected tsc of the user space. Based on the current code, the vcpu's tsc_offset is not configured as expected, resulting in significant guest service response latency, which is observed in our production environment. Applying the tsc synchronization logic after the vcpu's tsc_generation and KVM's cur_tsc_generation have completed their first match and started keeping tracked helps to fix this issue, which also does not break any existing guest tsc test cases. Reported-by: Yong He Closes: https://bugzilla.kernel.org/show_bug.cgi?id=217423 Suggested-by: Sean Christopherson Suggested-by: Oliver Upton Signed-off-by: Like Xu --- V1 -> V2 Changelog: - Test the 'kvm_vcpu_has_run(vcpu)' proposal; (Sean) - Test the kvm->arch.user_changed_tsc proposal; (Oliver) V1: https://lore.kernel.org/kvm/20230629164838.66847-1-likexu@tencent.com/ arch/x86/kvm/x86.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index a6b9bea62fb8..4724dacea2df 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -2721,7 +2721,7 @@ static void kvm_synchronize_tsc(struct kvm_vcpu *vcpu, u64 data) * kvm_clock stable after CPU hotplug */ synchronizing = true; - } else { + } else if (kvm->arch.nr_vcpus_matched_tsc) { u64 tsc_exp = kvm->arch.last_tsc_write + nsec_to_cycles(vcpu, elapsed); u64 tsc_hz = vcpu->arch.virtual_tsc_khz * 1000LL; base-commit: fdf0eaf11452d72945af31804e2a1048ee1b574c -- 2.41.0