Message ID | 20230724073516.45394-1-likexu@tencent.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9010:0:b0:3e4:2afc:c1 with SMTP id l16csp1682463vqg; Mon, 24 Jul 2023 02:39:44 -0700 (PDT) X-Google-Smtp-Source: APBJJlFgbB9U4QWpyr8mHORqckDz/1kaISdJkHus+GGZQ+z0C0QYYMM3DEha4VAb8Nd0H4Tdr7SZ X-Received: by 2002:ac2:4f0b:0:b0:4fd:c785:58b with SMTP id k11-20020ac24f0b000000b004fdc785058bmr5177155lfr.4.1690191583459; Mon, 24 Jul 2023 02:39:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690191583; cv=none; d=google.com; s=arc-20160816; b=qslYVy6r6YNeP2Oyy8ZBEzACSxxE7hsbuIUf01K9bo8BYSGgYMd88jjWIb3nx137UU Xw3ghdSvywaN1w0cS/CBe65rtw4zCWrjrfydJwj3dkI5Mv/IVwKkJ9esPjy/eyLXcJKH NM7Wifp5CVZfTSt7BTBPTmO5xe6/2hjB/Qr6u7SIMxW3b59LOz+1qK5NChvnpS+eibzV TRoG7MyK9lyUx/4Bym+CiOFQ68BuzD/dVscPMWMd/h9dIO+LvVK9h06HNuhBG8jNPbGd KEPUKcYzSuyG19tJduiniODkMK+NU0ZKe8zgboFfz/xs76WO61RHLqJW2SX/LkEtDrNU i8Bw== 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id l1-20020aa7c3c1000000b0051ddbdeb0c7si6218624edr.350.2023.07.24.02.39.19; Mon, 24 Jul 2023 02:39:43 -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 S229809AbjGXHfm (ORCPT <rfc822;kautuk.consul.80@gmail.com> + 99 others); Mon, 24 Jul 2023 03:35:42 -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 <rfc822;linux-kernel@vger.kernel.org>); 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 <like.xu.linux@gmail.com> X-Google-Original-From: Like Xu <likexu@tencent.com> To: Sean Christopherson <seanjc@google.com> Cc: Oliver Upton <oliver.upton@linux.dev>, Paolo Bonzini <pbonzini@redhat.com>, 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: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1772294329719652486 X-GMAIL-MSGID: 1772294329719652486 |
Series |
[v2] KVM: x86/tsc: Use calculated tsc_offset before matching the fist vcpu's tsc
|
|
Commit Message
Like Xu
July 24, 2023, 7:35 a.m. UTC
From: Like Xu <likexu@tencent.com> 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 <alexyonghe@tencent.com> Closes: https://bugzilla.kernel.org/show_bug.cgi?id=217423 Suggested-by: Sean Christopherson <seanjc@google.com> Suggested-by: Oliver Upton <oliver.upton@linux.dev> Signed-off-by: Like Xu <likexu@tencent.com> --- 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(-) base-commit: fdf0eaf11452d72945af31804e2a1048ee1b574c
Comments
On 7/24/23 09:35, Like Xu wrote: > From: Like Xu <likexu@tencent.com> > > 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 <alexyonghe@tencent.com> > Closes: https://bugzilla.kernel.org/show_bug.cgi?id=217423 > Suggested-by: Sean Christopherson <seanjc@google.com> > Suggested-by: Oliver Upton <oliver.upton@linux.dev> > Signed-off-by: Like Xu <likexu@tencent.com> > --- > 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; > I am not sure, when will kvm->arch.nr_vcpus_matched_tsc ever be nonzero again once a new generation starts? If this patch ever causes synchronizing to be false, matched will also be false when calling __kvm_synchronize_tsc. What was wrong with Oliver's proposed patch? Paolo
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;