Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp21079965rwd; Thu, 29 Jun 2023 10:37:31 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7TDAnXbIB6NC6dF7uWGu5NubojhyNn9afc5bSIrqON7wLRv7F1VKZLE/G1sjO52IWKXimi X-Received: by 2002:a17:903:244e:b0:1b6:b445:36d0 with SMTP id l14-20020a170903244e00b001b6b44536d0mr7168203pls.26.1688060251187; Thu, 29 Jun 2023 10:37:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688060251; cv=none; d=google.com; s=arc-20160816; b=0ruAWRYKZx9Kf/KPqhJwVdWjWb1DnTEqgOAo86weA/9QAIA1DeabFcaiDHoUfGWMQP TJ9/wBAHeyiww07Ei+QG1BXeY1C2A66k73a0eM7WTl5zwAz5roWDGXMuektr+it04Xni 87wuUr8zTKcjBmhlpHKZ6dqNNPx/SQov5drnUExrNHz1x7f2q5dywBijItf1aHROP7jL Z7KyA+xv3gsduIS+QhfSwMt720BKndpe/mg0RDFy180tkZWkDEeS2EsPo7/XcgmUOcNk 1U2DNn6zfx5wk5qfn+W5GO5o8TqlP1B6KmKaPof8AgkjosyFa7PzEAcXY/IQ7NtwwECV 2AIA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=Vsug4q/7DHQPgeIlomXxa8wBczhcEdo4oTk+wDRMkzw=; fh=NC4ejBzWpfv5Can9xJ9E8IhWLg3rRVaR3yNSiK0cQrk=; b=L5A1/tHZBrJQNyzzKf4uayyzOFL2ifd99TRDlfNzV6iQ0CNU8Qq6TPj7UX7LKZJwI/ QFtxytgyNL7RA3hE2yzWWnO5bE38v9pBbkaEhYgJsCFfSI8Pl7YfStkJFptEHpLVQvoP 6N8xNB0/dMUUHp7DWrp7J0ytQXA1QojPTEkSLqK3X0xM16N85flx/u4QM0ZcXUTLExHB 9LJGC/I9vdWZrbIqGB3m3bk0WWkM6tSQaScOutbKqyM6DexP+wuVU7xoc9MgZJn/8NqK lTyZIIESqqm4DRFk40eW9KSHjmKI2l4InOxNawo3O0d8pxPkkMNsoVE+koYmyULN5caM //Eg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=5zbQ7t8Q; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id jw20-20020a170903279400b001b5afd23447si6124974plb.244.2023.06.29.10.37.16; Thu, 29 Jun 2023 10:37:31 -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=@google.com header.s=20221208 header.b=5zbQ7t8Q; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232197AbjF2RTn (ORCPT + 99 others); Thu, 29 Jun 2023 13:19:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53334 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232013AbjF2RTm (ORCPT ); Thu, 29 Jun 2023 13:19:42 -0400 Received: from mail-pj1-x1049.google.com (mail-pj1-x1049.google.com [IPv6:2607:f8b0:4864:20::1049]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 86C833596 for ; Thu, 29 Jun 2023 10:19:40 -0700 (PDT) Received: by mail-pj1-x1049.google.com with SMTP id 98e67ed59e1d1-262e619fbd0so565053a91.0 for ; Thu, 29 Jun 2023 10:19:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1688059180; x=1690651180; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=Vsug4q/7DHQPgeIlomXxa8wBczhcEdo4oTk+wDRMkzw=; b=5zbQ7t8Q36AniZGdwegMe7PO/pTQbPeeDK86t+ZeIdDrPDcUWUcUt/YNpBEMJR+5TK jnZ2C/e8CSnB2VDhLM2szRhInlOrnmLBJDiI6JdtlYPmKby1l5HQfEoHU3fogfIQGYwH ymGEHxHXSCUjT2cNzU5Ofp8A0oszFb6JXu5PVdmQwDDi6bqpHcj++MSw9wvVA31+u6sX LIArrHRzjRkwFYMFkZtYxYt6YOrCSg2myVakDf8anqVViD5ZY3MfUz/aoT2Ry+7h9YOO ojs9H+v31xaK2MzbN7oH015zTgEyjEPalWuBFhxrfmgyEaTznrRzrnJr9akCqJflI1FT /9iQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688059180; x=1690651180; h=cc: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; bh=Vsug4q/7DHQPgeIlomXxa8wBczhcEdo4oTk+wDRMkzw=; b=VXGcFYVyTJ+4ZdVruEtTl2shio6XU9R/J09YfaFqABuTJd4pP+Z0WT4QOigiC0pqAz R2RmawpCJ4PVPik727AgyBhV3xJ4tU0ZTZN7QQcIj9xmbDSFWvUgwOhQJmg1LfOOtf+w OjifIn9hiPKIUWxFioU4BU5VBmdrSbmy4ovKpO9t+yBdYk1xo1WhbfXn9m4xlMx4ahS9 3/WMkjpOazvDVsH9q2XOXjb1I+WRKt3lHPkqFr1yu0Tx5XVCa8CuqEDlHaD/xkF8EEAo 9oOA9+R7sfgF3wu+ik9o7G4BSmSwJ0glQP5NCG7be96d+EwX0WgqZu99viVPp46L/2ca yrJQ== X-Gm-Message-State: ABy/qLbUsqdLSKc0YxYupllCTp6bmTvG8ZNr28WlUjgVHs/14oHnsN9z LzeVhrpFUEy5QGEh/OrQOmni9Jt7gy4= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:90a:c7cb:b0:262:dc60:20b3 with SMTP id gf11-20020a17090ac7cb00b00262dc6020b3mr14686pjb.8.1688059179997; Thu, 29 Jun 2023 10:19:39 -0700 (PDT) Date: Thu, 29 Jun 2023 10:19:38 -0700 In-Reply-To: <20230629164838.66847-1-likexu@tencent.com> Mime-Version: 1.0 References: <20230629164838.66847-1-likexu@tencent.com> Message-ID: Subject: Re: [PATCH] KVM: x86/tsc: Update guest tsc_offset again before vcpu first runs From: Sean Christopherson To: Like Xu Cc: Paolo Bonzini , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Oliver Upton Content-Type: text/plain; charset="us-ascii" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL autolearn=unavailable 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 +Oliver On Fri, Jun 30, 2023, Like Xu wrote: > From: Like Xu > > When a new vcpu is created and subsequently restored by vcpu snapshot, > apply kvm_vcpu_write_tsc_offset() before vcpu runs for the first time. > > Before a vcpu runs for the first time, the user space (VMM) sets the guest > tsc as it wants, which may triggers the time synchronization mechanism with > other vcpus (if any). In a scenario where a vcpu snapshot is used to > restore, like the bugzilla report [*], the newly target guest tsc (e.g. > at the time of vcpu restoration) is synchronized with its the most > primitive guest timestamp initialized at the time of vcpu creation. > > Furthermore, the VMM can actually update the target guest tsc multiple > times before the vcpu actually gets running, which requires the tsc_offset > to be updated every time it is set. In this scenario, it can be considered > as unstable tsc (even this vcpu has not yet even started ticking to follow > the intended logic of KVM timer emulation). > > It is only necessary to delay this step until kvm_arch_vcpu_load() to > catch up with guest expectation with the help of kvm_vcpu_has_run(), > and the change is expected to not break any of the cumbersome existing > virt timer features. "expected to not break" and "does not break" are two different statements. > Reported-by: Yong He > Closes: https://bugzilla.kernel.org/show_bug.cgi?id=217423 [*] > Tested-by: Jinrong Liang > Signed-off-by: Like Xu > --- > 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 439312e04384..616940fc3791 100644 > --- a/arch/x86/kvm/x86.c > +++ b/arch/x86/kvm/x86.c > @@ -4818,7 +4818,7 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) > if (tsc_delta < 0) > mark_tsc_unstable("KVM discovered backwards TSC"); > > - if (kvm_check_tsc_unstable()) { > + if (kvm_check_tsc_unstable() || !kvm_vcpu_has_run(vcpu)) { > u64 offset = kvm_compute_l1_tsc_offset(vcpu, > vcpu->arch.last_guest_tsc); > kvm_vcpu_write_tsc_offset(vcpu, offset); Doing this on every vCPU load feels all kinds of wrong, e.g. it will override the value set by userspace via KVM_VCPU_TSC_OFFSET. One could argue the KVM is "helping" userspace by providing a more up-to-date offset for the guest, but "helping" userspace by silently overriding userspace rarely ends well. Can't we instead just fix the heuristic that tries to detect synchronization? --- arch/x86/kvm/x86.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index c30364152fe6..43d40f058a41 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -2721,14 +2721,14 @@ static void kvm_synchronize_tsc(struct kvm_vcpu *vcpu, u64 data) * kvm_clock stable after CPU hotplug */ synchronizing = true; - } else { + } else if (kvm_vcpu_has_run(vcpu)) { u64 tsc_exp = kvm->arch.last_tsc_write + nsec_to_cycles(vcpu, elapsed); u64 tsc_hz = vcpu->arch.virtual_tsc_khz * 1000LL; /* * Special case: TSC write with a small delta (1 second) - * of virtual cycle time against real time is - * interpreted as an attempt to synchronize the CPU. + * of virtual cycle time against real time on a running + * vCPU is interpreted as an attempt to synchronize. */ synchronizing = data < tsc_exp + tsc_hz && data + tsc_hz > tsc_exp; base-commit: 2d6f036579d4ef5a09b0b45f66e34406290dfa1e --