Received: by 2002:a05:6358:c692:b0:131:369:b2a3 with SMTP id fe18csp4799342rwb; Mon, 31 Jul 2023 12:23:31 -0700 (PDT) X-Google-Smtp-Source: APBJJlHjqhoSXtSpR2WgYgKtLMX5Ea4av2dynhWsGoPPR0nJICNOjRdu+SkxkYE8GOU+yTTj6fpI X-Received: by 2002:aca:181a:0:b0:3a3:b39d:a8bf with SMTP id h26-20020aca181a000000b003a3b39da8bfmr10848953oih.45.1690831410976; Mon, 31 Jul 2023 12:23:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690831410; cv=none; d=google.com; s=arc-20160816; b=Nbp+IF+A0jb/BDm1Tvo30xCQ1wc0+4sd3nSLEaPFLicKD+wkIxLmpdKgTweEPQZpYx 4F34+68k1ElcLGiVpIbsDrK4jhnFL2gsyU2jA50pWeceOZ/kgXIbSmXvIFj46P2f/haG L0Pw7BQEGGda94+x1ly8gFQhs4RwNc/4XaJuTf6qkxFJeoLBonTLlohLa+Q+odL+bW9t MYpaRe3RTaeo/yCjBNgUlVVq7Faxthb038LVTCoIjjhUlsgLqPJs7J928sTilSxODNUj dsTiZSiAtGds2MmSfIP7WtJhqvefRvNHnVI3cUpppIy2yOSDDXG4RZF3TCZ4z32RAEBJ cZsw== 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=NeX+BTQhkz1hkGfxaYW5m3HOmH/qPCvTm2ZC5kk5axY=; fh=5eRfecjQiSQdPxAoCJX/AvzdzZP/QONHva93Q5zhdcw=; b=gatX7ILk1qHw725cG0NCD7WtSVY4TzkObKv2QnA5hb2iL7wMLWt3ibXKGQjB7UeM48 oXIGeZ3paR3mocUx73WoplZbR0NSoSDNv8Trkm7Q9SrSeUYqFkuF1UCSvCm724ouckrR uol9odxWy1E7Tfk4ehQZIFp67x3RYhMNAxvDkXoVhdyzBdpq4xgLKfr4zxAEWWV7lVGc EWjdDg9eoZcw286jRYWKol2jwkZMayZvCPjlhut9lef3V4lpwVEKFRGLTvJkchlZEEMn umYstkJdsYgboynLr2Hhb29KK00FSAc+w3PVRUyZA56yBJw+E/L3gYnHzfI75SIobVtn RpgQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=PXJ5ihB3; 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 a10-20020a65640a000000b005633498bbf3si7370037pgv.290.2023.07.31.12.23.19; Mon, 31 Jul 2023 12:23:30 -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=PXJ5ihB3; 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 S231854AbjGaSaC (ORCPT + 99 others); Mon, 31 Jul 2023 14:30:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56756 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230154AbjGaSaA (ORCPT ); Mon, 31 Jul 2023 14:30:00 -0400 Received: from out-72.mta1.migadu.com (out-72.mta1.migadu.com [95.215.58.72]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EA7EB1735 for ; Mon, 31 Jul 2023 11:29:59 -0700 (PDT) Date: Mon, 31 Jul 2023 18:29:54 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1690828198; 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=NeX+BTQhkz1hkGfxaYW5m3HOmH/qPCvTm2ZC5kk5axY=; b=PXJ5ihB3HjR3iQoLymxlbHr5/RsJ1YRGvzpt728eEEVabWE2GREe+S2Q/QDowGAi0Ipe+2 wZRo8XZKrJyJVcmb2D3dnRsmiuF2xDywb+C01K/C9DkVJ7nQ6SLL779lzEUs1VVshVQIkD 1QY+kFgAs1spyll4MAzb5yPL6UmopxQ= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Oliver Upton To: Like Xu Cc: Paolo Bonzini , Sean Christopherson , kvm@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v3] KVM: x86/tsc: Don't sync user changes to TSC with KVM-initiated change Message-ID: References: <20230731080758.29482-1-likexu@tencent.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230731080758.29482-1-likexu@tencent.com> 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,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE 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 Mon, Jul 31, 2023 at 04:07:58PM +0800, Like Xu wrote: > From: Like Xu > > Add kvm->arch.user_changed_tsc to avoid synchronizing user changes to > the TSC with the KVM-initiated change in kvm_arch_vcpu_postcreate() by > conditioning this mess on userspace having written the TSC at least > once already. > > Here lies UAPI baggage: user-initiated TSC write with a small delta > (1 second) of virtual cycle time against real time is interpreted as an > attempt to synchronize the CPU. In such a scenario, the vcpu's tsc_offset > is not configured as expected, resulting in significant guest service > response latency, which is observed in our production environment. The changelog reads really weird, because it is taken out of context when it isn't a comment over the affected code. Furthermore, 'our production environment' is a complete black box to the rest of the community, it would be helpful spelling out exactly what the use case is. Suggested changelog: KVM interprets writes to the TSC with values within 1 second of each other as an attempt to synchronize the TSC for all vCPUs in the VM, and uses a common offset for all vCPUs in a VM. For brevity's sake let's just ignore what happens on systems with an unstable TSC. While this may seem odd, it is imperative for VM save/restore, as VMMs such as QEMU have long resorted to saving the TSCs (by value) from all vCPUs in the VM at approximately the same time. Of course, it is impossible to synchronize all the vCPU ioctls to capture the exact instant in time, hence KVM fudges it a bit on the restore side. This has been useful for the 'typical' VM lifecycle, where in all likelihood the VM goes through save/restore a considerable amount of time after VM creation. Nonetheless, there are some use cases that need to restore a VM snapshot that was created very shortly after boot (<1 second). Unfortunately the TSC sync code makes no distinction between kernel and user-initiated writes, which leads to the target VM synchronizing on the TSC offset from creation instead of the user-intended value. Avoid synchronizing user-initiated changes to the guest TSC with the KVM initiated change in kvm_arch_vcpu_postcreate() by conditioning the logic on userspace having written the TSC at least once. I'll also note that the whole value-based TSC sync scheme is in desperate need of testing. -- Thanks, Oliver