Received: by 2002:ab2:60d1:0:b0:1f7:5705:b850 with SMTP id i17csp637426lqm; Wed, 1 May 2024 10:56:11 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUD+BdtZTPynEt2g6sMnzvDPceZ1MEKqu4E42AwNT/Om9Nr84lwGfkepZO3G+WW65Mki9x+mT46vOuv4xWoTNjnDRFh+ldZkfCvNf1nAQ== X-Google-Smtp-Source: AGHT+IH++xvmZTghgtP/0k/Xuv4CXXLuNli2LjwTDw25+i/H8U+MzlNW1V9J9eLYe9RkOSuo6Dcy X-Received: by 2002:a05:620a:4d7:b0:790:98ba:67c5 with SMTP id 23-20020a05620a04d700b0079098ba67c5mr3220677qks.33.1714586171442; Wed, 01 May 2024 10:56:11 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714586171; cv=pass; d=google.com; s=arc-20160816; b=ypG1CNEeZNnUCsBolJGnw0giFGgaCBUqPXhBwGubPBSUvztyXfjEE13pJMzSkzkFOy tSXkXPWOM35MIMHkBz3DIMj2jAkBtteR3bxaNOJ0insbnWb8QuMHbinCli27rRD5Y4gv 33M5PmjN0I8scMC17og1QlY4CWkZ2wFRwuryJyk4bYBKuCp3Wg/rwbJ825sX0KTUHvoQ RLT+hRMs9HFS+ctqsZ/lfoFbx6Mj0ahI36t3CAfi9MHRZ8go6NC3wb4GJ2b028mWxFYv miVXyd1VKT5wGkrYWEX7cnkAiCPGom55ERqo384urEkhC3KXkAjn9Io4W+iTEQE+VmRO 6Q8A== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id:dkim-signature; bh=Zhr2FAzmJKIqSMbLDig4WSUw3SjMYlWZtlg8e04TZs8=; fh=6J61XiHDeynt2jdN1MC3Ae+QaeJEqR93HkRA5d0Cy/0=; b=fQBmGGAfF3GZlfvwJEQ4AmlxyI3EbE4Wrw49+m2Xlc5182iyp3qG+O66ksRKd3uihc ZKnby1cwXQxobqBch9pTyYYVOrihza3nRwZJwZ2ZvfqFRT4B6j+itvXiKp13WhvhsRVY 5589Tj4PMn16VaXEbzsXDnqqWTgdQw6ouze10Fr00h12eNRV/Jx2T5Qm7kuHXIrFAirh KRdpeHyiL7dgmQTdG8gvhE7LQBbhgTVoALmd6Ir7+/fLOe9+rvitLWA+/P8h/0S6OfZI 340uymGzyHTjiX49JtbeQwZSwCIHiExtSexK6EHb/YEnyf4W+KHSnuZOMj7IdGwnx2PU Gbtw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=oCCD4AZV; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-165668-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-165668-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id l8-20020a37f508000000b0078d63795738si30779775qkk.277.2024.05.01.10.56.11 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 May 2024 10:56:11 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-165668-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=oCCD4AZV; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-165668-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-165668-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 2F9D81C20BA4 for ; Wed, 1 May 2024 17:56:11 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8EE981474AD; Wed, 1 May 2024 17:56:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="oCCD4AZV" Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.7]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1416C748D; Wed, 1 May 2024 17:55:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.7 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714586159; cv=none; b=GQ5arNIIaO5/Zqtcogc1N4ru8WuPo5DIfrVVyqo1NjSLm1cUNOS7aLiZ/eBaY4FRXqpCqQe51tLu40CRqo5EU0fSijY4QJOmDTRBQ1Q5UxWKh9lwlwu+o4ANQo2WX8bt1QAVNlYaByuWGK3VfJQmFoCgyv5RtX0hEUZikp7u5N0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714586159; c=relaxed/simple; bh=ImsoO37LmIyxcjfq9GfK3cKoACG+YHaB+w+naaUo6Rc=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=GcdYb3w1ufVHzaz+XoW7JkksLHYkfADravCF0VITV15A0HCmtr6lB4pAENIOJinl4bFp7+fAGULnXEi/YDiWdBYCgPdFG7ZQ5rxBQ48hbJee+/ZH3oAU6rOHI/W5kU9nXrQDKli6KvzLGRRwpatRW3P92d6NhCDR3HDi08lhd4o= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=oCCD4AZV; arc=none smtp.client-ip=192.198.163.7 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1714586158; x=1746122158; h=message-id:date:mime-version:subject:to:cc:references: from:in-reply-to:content-transfer-encoding; bh=ImsoO37LmIyxcjfq9GfK3cKoACG+YHaB+w+naaUo6Rc=; b=oCCD4AZVluDNgtK8Q06y6mGOUX82+UpJWc8Wls3ttzpAAQPz7LXqInbf wvh+cCuVBplvrZhH3ptZOYaNmn9aUh0g3uTY+ArXInmKv5PEBRT68kSFJ MiJrAkrXru0JiHxuYfdC3vW3XKp0ck0Ip64Is4nuwWWxHrMc0prY9AvDo 2T7Oh30vCVLPr1WhMth9Z9YKLf7+fV3kcmgx0OTksEN8ZxAb+x1bo0XhZ NFzFped9EblFeyPlYZbZC/24M45FB4TY5BW8BCcymA95aFDwq4ETH4Ofb pkYMUUPJOmRt7ui2ux1a2UWNuBevyOFwaF+o02EoxivUKa+aIObcuMKTG w==; X-CSE-ConnectionGUID: jA7c0FJbTJaUZLK8G5Y/0w== X-CSE-MsgGUID: xnm3Mn8XRWe1GWyq1ggQ4w== X-IronPort-AV: E=McAfee;i="6600,9927,11061"; a="35714918" X-IronPort-AV: E=Sophos;i="6.07,246,1708416000"; d="scan'208";a="35714918" Received: from orviesa005.jf.intel.com ([10.64.159.145]) by fmvoesa101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 May 2024 10:55:45 -0700 X-CSE-ConnectionGUID: fA5cYtbySBK+URiVZ2UYqQ== X-CSE-MsgGUID: sl7aHpoRT0mXP/c6i7u+8w== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,246,1708416000"; d="scan'208";a="31663564" Received: from soc-cp83kr3.jf.intel.com (HELO [10.24.10.57]) ([10.24.10.57]) by orviesa005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 May 2024 10:55:44 -0700 Message-ID: Date: Wed, 1 May 2024 10:55:46 -0700 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v2 15/15] KVM: x86: Factor out kvm_use_master_clock() To: David Woodhouse , kvm@vger.kernel.org Cc: Paolo Bonzini , Jonathan Corbet , Sean Christopherson , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Paul Durrant , Shuah Khan , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, Oliver Upton , Marcelo Tosatti , jalliste@amazon.co.uk, sveith@amazon.de, Dongli Zhang References: <20240427111929.9600-1-dwmw2@infradead.org> <20240427111929.9600-16-dwmw2@infradead.org> Content-Language: en-US From: "Chen, Zide" In-Reply-To: <20240427111929.9600-16-dwmw2@infradead.org> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit On 4/27/2024 4:05 AM, David Woodhouse wrote: > From: David Woodhouse > > Both kvm_track_tsc_matching() and pvclock_update_vm_gtod_copy() make a > decision about whether the KVM clock should be in master clock mode. > > They use *different* criteria for the decision though. This isn't really > a problem; it only has the potential to cause unnecessary invocations of > KVM_REQ_MASTERCLOCK_UPDATE if the masterclock was disabled due to TSC > going backwards, or the guest using the old MSR. But it isn't pretty. > > Factor the decision out to a single function. And document the historical > reason why it's disabled for guests that use the old MSR_KVM_SYSTEM_TIME. > > Signed-off-by: David Woodhouse > --- > arch/x86/kvm/x86.c | 27 +++++++++++++++++++++++---- > 1 file changed, 23 insertions(+), 4 deletions(-) > > diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c > index d6e4469f531a..680b39f17851 100644 > --- a/arch/x86/kvm/x86.c > +++ b/arch/x86/kvm/x86.c > @@ -2518,6 +2518,27 @@ static inline bool gtod_is_based_on_tsc(int mode) > } > #endif > > +static bool kvm_use_master_clock(strut kvm *kvm) typo: 'strut' -> 'struct' > +{ > + struct kvm_arch *ka = &kvm->arch; > + > + /* > + * The 'old kvmclock' check is a workaround (from 2015) for a > + * SUSE 2.6.16 kernel that didn't boot if the system_time in > + * its kvmclock was too far behind the current time. So the > + * mode of just setting the reference point and allowing time > + * to proceed linearly from there makes it fail to boot. > + * Despite that being kind of the *point* of the way the clock > + * is exposed to the guest. By coincidence, the offending > + * kernels used the old MSR_KVM_SYSTEM_TIME, which was moved > + * only because it resided in the wrong number range. So the > + * workaround is activated for *all* guests using the old MSR. > + */ > + return ka->all_vcpus_matched_tsc && > + !ka->backwards_tsc_observed && > + !ka->boot_vcpu_runs_old_kvmclock; > +} > + > static void kvm_track_tsc_matching(struct kvm_vcpu *vcpu) > { > #ifdef CONFIG_X86_64 > @@ -2550,7 +2571,7 @@ static void kvm_track_tsc_matching(struct kvm_vcpu *vcpu) > * To use the masterclock, the host clocksource must be based on TSC > * and all vCPUs must have matching TSC frequencies. > */ > - bool use_master_clock = ka->all_vcpus_matched_tsc && > + bool use_master_clock = kvm_use_master_clock(kvm) && 'kvm' should be `vcpu->kvm` > gtod_is_based_on_tsc(gtod->clock.vclock_mode);