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 Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id DDD57EB64D7 for ; Fri, 30 Jun 2023 17:24:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233171AbjF3RYi (ORCPT ); Fri, 30 Jun 2023 13:24:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41844 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233082AbjF3RY2 (ORCPT ); Fri, 30 Jun 2023 13:24:28 -0400 Received: from out-8.mta1.migadu.com (out-8.mta1.migadu.com [IPv6:2001:41d0:203:375::8]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 88ACF1713 for ; Fri, 30 Jun 2023 10:24:24 -0700 (PDT) Date: Fri, 30 Jun 2023 17:24:19 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1688145862; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=JEgiQFbtqMAhuryJj77jXTyIRFEwAksjrjxqtSP6Jpk=; b=PnIumV092qsEvd5Z3GRY627vjHr/QAzlEmygzkJfzxtT+vT/kCNuema38wnD0QczkJBNs9 GS9RlV8VLRCR/dnEJCkvfJyLgvrUIlVIFA9VMoBbvQbsUA5Yf834XiQR8u8MknJRZtpeR7 AVqDiG/EagK+xnoAZVRzYivTGYgMAaE= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Oliver Upton To: Sean Christopherson Cc: Like Xu , Paolo Bonzini , kvm@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] KVM: x86/tsc: Update guest tsc_offset again before vcpu first runs Message-ID: References: <20230629164838.66847-1-likexu@tencent.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Migadu-Flow: FLOW_OUT Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org On Fri, Jun 30, 2023 at 05:21:41PM +0000, Oliver Upton wrote: > > 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; > > This would break existing save/restore patterns for the TSC. QEMU relies > on KVM synchronizing the TSCs when restoring a VM, since it cannot > snapshot the TSC values of all the vCPUs in a single instant. It instead > tries to save the TSCs at roughly the same time [*], which KVM detects > on the target and gets everything back in sync. Can't wait to see when > this heuristic actually breaks :) Of course, forgot to actually include the link. [*] https://gitlab.com/qemu-project/qemu/-/blob/408015a97dbe48a9dde8c0d2526c9312691952e7/target/i386/kvm/kvm.c#L249 -- Thanks, Oliver