Received: by 2002:ab2:1689:0:b0:1f7:5705:b850 with SMTP id d9csp415096lqa; Sat, 27 Apr 2024 07:26:48 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUE3NmEvijq6yLnXN12G+Psr5VZWpHvl9WwJGHX02yuPA5U8rRqa4XJMNyAq4hK4giWdOy4qiaO7iZ+2SlNvibX80lnEQFZbj7pnSIyKw== X-Google-Smtp-Source: AGHT+IHUMURTFSXGgOoDHU7nxCnc8ZyxRa/CAo1sfeWKSYE+Nm3/kYRTgkWr2SbDlj8NjOYfklYO X-Received: by 2002:a50:8a89:0:b0:568:a30c:2db5 with SMTP id j9-20020a508a89000000b00568a30c2db5mr3014566edj.40.1714228008213; Sat, 27 Apr 2024 07:26:48 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714228008; cv=pass; d=google.com; s=arc-20160816; b=ipuxxRgFSkhZnGULfXaCDkvxDEgAlF7VFa226Ac2hcLnESyQZdAdt4ZQg6N/HI/CQO Jn/nDpPCFEBbmGMPIwgZW7gpgAxrYD78heMYMSomU9mev3vfu17f5nLsDbNCkIcxBOTM bfUJSoLCrBRYtRxI1P50J8GUKPm4Ob3qIy/8WYrHJtZWwrA1LDBc3WSoD+Q0Xg5RpOFw tWzyr/tjcTYI66UPDEgqaS5Qpax/jwjs6tjaQqOAp+RwmijtqLfz3iubPEmkH8Jfy7G8 HIzlqDX5IibRiF1CC1ffiuCh4cOoSRqqX5wV+31Qp5IXcVrf0ONbKXa1A3yzMUXpYLqL lmDw== 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=ApxpDJWImUR2W02lv4MOUaYvWzFyVZfupX+UUwfAxSw=; fh=GxGcIBasQq2Gc/176mTrpEex7eS1coPy2FlYCD7Qc50=; b=E4/wZIOEK2bFDFBxZprrR7R0nyB7f2fgZ2/RPqab1u/pXt/kuFIhz4Et/IkqIzyBSw bP8svW1WLvEliLv5zLQhY62ZPvoFGTfcOqqcFx9HPkaOynJhLym/xsKEAIXx9hVrGZkc o6ubsXHcBfaG213GsQXNVt8sonUkh6Uj+wEFdVZPnhzd0aB/2oObyeH3+6vWeQwIWtTQ zQ4XdOGumyZkzSuz8MAZNyGdoZYXeAvO7XDwIBEvYuii/L1/9z41VjCUqsT54V9czNvp YwbjPZUqaJhJOmotWknovJWW0u8zt60EyZNaZyU4nYT1Ml0EGWpGS4OGab+FCH+yP90Y P8Xg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=W610WcHS; arc=pass (i=1 dkim=pass dkdomain=infradead.org); spf=pass (google.com: domain of linux-kernel+bounces-161022-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-161022-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id g33-20020a056402322100b00572701284d7si581211eda.243.2024.04.27.07.26.47 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 27 Apr 2024 07:26:48 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-161022-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=W610WcHS; arc=pass (i=1 dkim=pass dkdomain=infradead.org); spf=pass (google.com: domain of linux-kernel+bounces-161022-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-161022-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 am.mirrors.kernel.org (Postfix) with ESMTPS id A59181F23B63 for ; Sat, 27 Apr 2024 11:21:33 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 2684B54792; Sat, 27 Apr 2024 11:19:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="W610WcHS" Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) (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 E2D604AECE; Sat, 27 Apr 2024 11:19:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=90.155.50.34 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714216788; cv=none; b=k7HkLjW19BJak7xGlBWJtqzibjXF2Q4SccA5DHJoddj7l346LHwwgiVFbCg3fSV8IkUWA2tS4jXvvm75yzShrFA8Iyt92roQ869TK4OpKLYv/UUt0oyxWg+G+LO1hBYvLKXZBhwxRTzlraJYsMxI22xtLCE2V46amTdVYtyE3G4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714216788; c=relaxed/simple; bh=oreP581RFFJ0udRz/Em+dh8lxr/hYKZ4qCRmSQDE6AM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ZA/Kam1f0SS1zhCVYGSYD8m76T9L+wBVZcLjxouDH20qN2qGnM7VhfshIseKFhTf2+I9zqT89SPbBmOgJZJCB/D0WqKKmLYESWBsVKtg9VTtNdMCHkcY+8g9yiwFgxmyZyQGRR65+R41xg+DdYiPmyPdhk3nvAWT7idHwfLxf10= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org; spf=none smtp.mailfrom=casper.srs.infradead.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b=W610WcHS; arc=none smtp.client-ip=90.155.50.34 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=casper.srs.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; 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=ApxpDJWImUR2W02lv4MOUaYvWzFyVZfupX+UUwfAxSw=; b=W610WcHSR5FZGmNJ7Qwdx6MtZ1 audraTB/PsYQ+q2LXRcLt4yxxCHZ4lXAyaUBBPsIrlgWtNDdJMPE0m1Mf9nbc75YgyPMKVbaJULx0 SHTwCz8FwB8f0Qx+cgthCrRCEKRcLnguVdx2di/2VO7lCT4mgcYlruTYLXZ9ZGcMFU4uDg7+g9BCs kT5qxmDBOsPeDKH/uL6MQJQ3FDU22BMFwDkqNemcIH1k4bY0yFi3UbqXVSPofbd6iNJiN6w4CN714 ClGerZlwsmB8YqMhCiU6kJsfy5EHTyQNQHwxuzp46Q1ZTys178pUnaUmijJGHIcMYfXNf392tlLFq Qz2TW98A==; Received: from [2001:8b0:10b:1::ebe] (helo=i7.infradead.org) by casper.infradead.org with esmtpsa (Exim 4.97.1 #2 (Red Hat Linux)) id 1s0g5g-00000007Jwk-2tcw; Sat, 27 Apr 2024 11:19:36 +0000 Received: from dwoodhou by i7.infradead.org with local (Exim 4.97.1 #2 (Red Hat Linux)) id 1s0g5g-000000002bR-14Az; 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 07/15] KVM: x86: Avoid NTP frequency skew for KVM clock on 32-bit host Date: Sat, 27 Apr 2024 12:05:04 +0100 Message-ID: <20240427111929.9600-8-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 casper.infradead.org. See http://www.infradead.org/rpr.html From: David Woodhouse Commit 53fafdbb8b21 ("KVM: x86: switch KVMCLOCK base to monotonic raw clock") did so only for 64-bit hosts, by capturing the boot offset from within the existing clocksource notifier update_pvclock_gtod(). That notifier was added in commit 16e8d74d2da9 ("KVM: x86: notifier for clocksource changes") but only on x86_64, because its original purpose was just to disable the "master clock" mode which is only supported on x86_64. Now that the notifier is used for more than disabling master clock mode, (well, OK, more than a decade later but clocks are hard), enable it for the 32-bit build too so that get_kvmclock_base_ns() can be unaffected by NTP sync on 32-bit too. Signed-off-by: David Woodhouse Reviewed-by: Paul Durrant --- arch/x86/kvm/x86.c | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 00a7c1188dec..44b3d2a0da5b 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -2245,7 +2245,6 @@ static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data) return kvm_set_msr_ignored_check(vcpu, index, *data, true); } -#ifdef CONFIG_X86_64 struct pvclock_clock { int vclock_mode; u64 cycle_last; @@ -2303,13 +2302,6 @@ static s64 get_kvmclock_base_ns(void) /* Count up from boot time, but with the frequency of the raw clock. */ return ktime_to_ns(ktime_add(ktime_get_raw(), pvclock_gtod_data.offs_boot)); } -#else -static s64 get_kvmclock_base_ns(void) -{ - /* Master clock not used, so we can just use CLOCK_BOOTTIME. */ - return ktime_get_boottime_ns(); -} -#endif static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock, int sec_hi_ofs) { @@ -9819,6 +9811,7 @@ static void pvclock_irq_work_fn(struct irq_work *w) } static DEFINE_IRQ_WORK(pvclock_irq_work, pvclock_irq_work_fn); +#endif /* * Notification about pvclock gtod data update. @@ -9826,26 +9819,26 @@ static DEFINE_IRQ_WORK(pvclock_irq_work, pvclock_irq_work_fn); static int pvclock_gtod_notify(struct notifier_block *nb, unsigned long unused, void *priv) { - struct pvclock_gtod_data *gtod = &pvclock_gtod_data; struct timekeeper *tk = priv; update_pvclock_gtod(tk); +#ifdef CONFIG_X86_64 /* * Disable master clock if host does not trust, or does not use, * TSC based clocksource. Delegate queue_work() to irq_work as * this is invoked with tk_core.seq write held. */ - if (!gtod_is_based_on_tsc(gtod->clock.vclock_mode) && + if (!gtod_is_based_on_tsc(pvclock_gtod_data.clock.vclock_mode) && atomic_read(&kvm_guest_has_master_clock) != 0) irq_work_queue(&pvclock_irq_work); +#endif return 0; } static struct notifier_block pvclock_gtod_notifier = { .notifier_call = pvclock_gtod_notify, }; -#endif static inline void kvm_ops_update(struct kvm_x86_init_ops *ops) { @@ -9984,9 +9977,10 @@ int kvm_x86_vendor_init(struct kvm_x86_init_ops *ops) if (pi_inject_timer == -1) pi_inject_timer = housekeeping_enabled(HK_TYPE_TIMER); -#ifdef CONFIG_X86_64 + pvclock_gtod_register_notifier(&pvclock_gtod_notifier); +#ifdef CONFIG_X86_64 if (hypervisor_is_type(X86_HYPER_MS_HYPERV)) set_hv_tscchange_cb(kvm_hyperv_tsc_notifier); #endif -- 2.44.0