Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp22640746rwd; Fri, 30 Jun 2023 10:30:38 -0700 (PDT) X-Google-Smtp-Source: APBJJlFXOBak1mvjtbI4HSoOnTnGhvoAcb+OgNH1GsCAJzkucZcNfFCvllRe5NpVTcs2M6bdhASW X-Received: by 2002:a17:902:d486:b0:1b8:64e9:e4b3 with SMTP id c6-20020a170902d48600b001b864e9e4b3mr2643958plg.39.1688146237930; Fri, 30 Jun 2023 10:30:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688146237; cv=none; d=google.com; s=arc-20160816; b=jR7AK0+nDjGJzc9UucrhstyhIGz0yU9mGUPTilTjR9KKeg+ey1NVjrhV0LA0LD8vLB rT3Te7KmR5pxL0RgmjG73Xx8rPov06NRfxtcAF7ne5Cj96TcAWEVq9LkY2NZUo3D6vK3 46I2V8tbEGwPT3QzIiuxpQd+ueiaMgqOTQuH0/qZdzpx0KDet0nPNLHJeute0wHFX+xV sE4GCTeWkpF+yJs66e3emWD7l96Dw2aODicUcwxXSzT0uy82vwd0TT8r/ZN7ggP5Rkuq SwT+zkoRgTrt67AH/mODXzLQqfwowhz1mntWAmNRX0hNQFDLsvJ9vPEXrTT7r4YCeBDn v09A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:dkim-signature:date; bh=JEgiQFbtqMAhuryJj77jXTyIRFEwAksjrjxqtSP6Jpk=; fh=NGIdE9aHhz5ZojZ6E5DXRDEwh9FfPwfhp4PcR1ol9b4=; b=UDJRcoNPQzrfuQiPwKYCkMx5y6em72cRzCpI15Mku6jGMidG63TSF/QlnZ5FwbMuLb u5sPfkXIfRASppZKUdV4Ami3tPSV1w3QS0GgVf4PNVP6pjTD/wEL26dGnMtrlslTbZ8B DpS1JEcnO4vOfvzIYkYMr7Bp4I24kqgDTwRA29dMhfCmSX5GHb2xcieLPPAIE+I7j6b6 UwNJnupxqFMIVNRlHtqR/XKjf6wdgfIOgy4lyHfPfyvDY7sb9mfhGKOaj07HJXYriB5G awOz3WTQzl9Wzl22r3HZ8xra0o4l7EvUh8RlWFK0ahuO4cbDTY15H3BK7lfqWN63hduc 7OCA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=PnIumV09; 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=NONE dis=NONE) header.from=linux.dev Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id q5-20020a170902c74500b001b678455766si5589784plq.650.2023.06.30.10.30.22; Fri, 30 Jun 2023 10:30:37 -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=@linux.dev header.s=key1 header.b=PnIumV09; 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=NONE dis=NONE) header.from=linux.dev Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233172AbjF3RYi (ORCPT + 99 others); Fri, 30 Jun 2023 13:24:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42024 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233114AbjF3RY2 (ORCPT ); Fri, 30 Jun 2023 13:24:28 -0400 Received: from out-39.mta1.migadu.com (out-39.mta1.migadu.com [IPv6:2001:41d0:203:375::27]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 88B86199B 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 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED 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 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