Received: by 2002:ab2:1689:0:b0:1f7:5705:b850 with SMTP id d9csp415813lqa; Sat, 27 Apr 2024 07:28:19 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCW95zVN1Od5uhjP/2hoeyiIAwSKkkEZ6lEAWleyjmUlvonbY8Jl+EFbg3gEZh5GIWNxPJhdb8D1GuKznnUhrotPH/YV5TwFVZYlg/tmqQ== X-Google-Smtp-Source: AGHT+IGfkhHLQ+N5b1uMjK71LTxmtn/lB9F4UOQcnyZCNxghzNHp5H8ATvnFkIWUiZT1sVJLdqBo X-Received: by 2002:a05:6a20:2d07:b0:1a7:5780:1f4b with SMTP id g7-20020a056a202d0700b001a757801f4bmr9138883pzl.24.1714228099401; Sat, 27 Apr 2024 07:28:19 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714228099; cv=pass; d=google.com; s=arc-20160816; b=JI8/gdBCyrOGov9b4jH5p+ZS11T0f1euGD84VyoDPv4IXXrS0X3TBcUUNNdp+dvfq/ GTtI9M4pOwUPx4xkoD8Z0C2VSiISwEkqFpz0DBI/Hh9pzNuikizQ9qCKt8AaARW9AZqU za+ttKQB7GRDk/d+hGYvyM02TqoA2UxBQIilPdrGDIwUVNYwUlt4SMs7wUWMlJ4ywCnv 6QtCQkjLI/3CYDUibOBrcLfOhbSzGlO8zN9LozHo9IxWKlemqLyHddUwxI+EDs4fgPE8 VbUG8gAffj5YIbqm/qYyWYcSGaBQEpRKlci4xxK96EkwLe9OZb8Ub4svqDbXAY3eR/Bj 9CjQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=5S2tW55GgDiFyBbw9lOzLrSN36Zzgu35eCfjV6r8WLE=; fh=GxGcIBasQq2Gc/176mTrpEex7eS1coPy2FlYCD7Qc50=; b=xRtCGVyBPo3YPRNKtkqDnIGRDu9n+eqFzS/QU36H4s/syrYSlGIHqVRZes87klH++7 iALSMr3Tz7h1WDEWanUAapWsU19CiVR/dN6+2iWqHWs8p2MTPwrQMkRptqe6nZ0B9qUR LnSQUU9eeELPDhBfI4R9XHy3be3IuYFSWcLhDixZgyx3xlymkeujQ3SGMBqqbpUlH/ew p8J1SmrY3sVa6eRKRoOeWD/D1AaTQzt02VWsHOlYygWTdZv0oJnn1wPpmB0YDHikFgRT APxCW7U6kNN/nBCy+Rxm78HpUkBBLNA7BOe9K5ZvpdXAK3CPhg9pF/fNoiDtaEH9YLMe phAg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@infradead.org header.s=desiato.20200630 header.b="C/3S/7g2"; arc=pass (i=1 dkim=pass dkdomain=infradead.org); spf=pass (google.com: domain of linux-kernel+bounces-161029-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-161029-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id l189-20020a6388c6000000b005f7af029769si15536322pgd.21.2024.04.27.07.28.19 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 27 Apr 2024 07:28:19 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-161029-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@infradead.org header.s=desiato.20200630 header.b="C/3S/7g2"; arc=pass (i=1 dkim=pass dkdomain=infradead.org); spf=pass (google.com: domain of linux-kernel+bounces-161029-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-161029-linux.lists.archive=gmail.com@vger.kernel.org" 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 sv.mirrors.kernel.org (Postfix) with ESMTPS id DE16C28627D for ; Sat, 27 Apr 2024 11:23:13 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 289EF53387; Sat, 27 Apr 2024 11:21:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="C/3S/7g2" Received: from desiato.infradead.org (desiato.infradead.org [90.155.92.199]) (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 177F54D9F9; Sat, 27 Apr 2024 11:21:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=90.155.92.199 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714216889; cv=none; b=hfhi39zbpOnaNSvTXLSs83uyrsNDGXeN+ETMzmXerOgwE5iZUehRO0nuy7/gAoX1mi6hZlwfH+ZP1o3YJKiayItWCfs+wrBkiAATJRsOI+EO8UYp88BCypQkYSKQWvQtnYhLnccMmSEN0yqhwd3NdXzR+hmloCTUDXaDYDxzZXI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714216889; c=relaxed/simple; bh=zr1wUci8Xcm3SBmkDautJw+akuAPWZdTnl8cPxx2GoY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=BFxhoiU6CXo+zp2/hn9HqES5PpkMogX7TwgH9rEQloZ5Pipwy5RMx+RaDC+13DPtj/WHAKAs0P7mDnyrJrGBWLJzq/4pQIMkGuzyJnoMZC3K2CvQKxCtDMrUMaPR9S+Eopr4sDALn2grDF/jz5w2oFzdRDxSuz+3IH6kbAJV5ik= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org; spf=none smtp.mailfrom=desiato.srs.infradead.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b=C/3S/7g2; arc=none smtp.client-ip=90.155.92.199 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=desiato.srs.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Sender:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description; bh=5S2tW55GgDiFyBbw9lOzLrSN36Zzgu35eCfjV6r8WLE=; b=C/3S/7g2ZpEyQRx09JvWZeaGRy MstfydVMSKmDzDiNWQsVmHHabx9OQfnIVGuuynHfWhe7zBkqZrWPjQes73h15FdkhnTrE6PNu9LBi b14a6T+iiBXVFXYMoIITDFCnXOq/qlapJsNYPQ6D5yagLsv8JVtf0F1dfuAFc8dvnkGYjNQq558Pj BG4m/Ka0KD//ict8yEwYLWcCwuAqOybLY5jafWitwsG68L44u03utA67Qq97DkEKxNombNr+q3Vo7 RdniFSrdGS2PzHBGd0tvRjFWa3WUuvJ++CudOpdwkUgDJ0giXjFwnSRrz/nUKPm/lyjRzdmhy4hhM dyxiJD2w==; Received: from [2001:8b0:10b:1::ebe] (helo=i7.infradead.org) by desiato.infradead.org with esmtpsa (Exim 4.97.1 #2 (Red Hat Linux)) id 1s0g5i-0000000FeZk-0pka; Sat, 27 Apr 2024 11:19:46 +0000 Received: from dwoodhou by i7.infradead.org with local (Exim 4.97.1 #2 (Red Hat Linux)) id 1s0g5g-000000002bx-2nxs; Sat, 27 Apr 2024 12:19:36 +0100 From: David Woodhouse To: 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, zide.chen@intel.com, Dongli Zhang Subject: [PATCH v2 15/15] KVM: x86: Factor out kvm_use_master_clock() Date: Sat, 27 Apr 2024 12:05:12 +0100 Message-ID: <20240427111929.9600-16-dwmw2@infradead.org> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240427111929.9600-1-dwmw2@infradead.org> References: <20240427111929.9600-1-dwmw2@infradead.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: David Woodhouse X-SRS-Rewrite: SMTP reverse-path rewritten from by desiato.infradead.org. See http://www.infradead.org/rpr.html 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) +{ + 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) && gtod_is_based_on_tsc(gtod->clock.vclock_mode); /* @@ -3089,9 +3110,7 @@ static void pvclock_update_vm_gtod_copy(struct kvm *kvm) &ka->master_cycle_now); ka->use_master_clock = host_tsc_clocksource - && ka->all_vcpus_matched_tsc - && !ka->backwards_tsc_observed - && !ka->boot_vcpu_runs_old_kvmclock; + && kvm_use_master_clock(kvm); /* * When TSC scaling is in use (which can thankfully only happen -- 2.44.0