From nobody Mon Feb 9 06:28:15 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=reject dis=none) header.from=google.com ARC-Seal: i=1; a=rsa-sha256; t=1740622812; cv=none; d=zohomail.com; s=zohoarc; b=W4eBaQsuKB8aTZLl5XjiZq4djlZjr5wAJCl+v3HadrG0BMZm4rzh2URZW0QKHa9j8JRLIK9KepPp7kXoshW7yb2W1yinHccTBxmgR3OtNKP5q16Hbc9P4W6CadOEA9d+yF6SqFYVDkQnGqoZAIs5rfYLJUdT099GbpK3La2hOEY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1740622812; h=Content-Type:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Sender:Subject:Subject:To:To:Message-Id; bh=xfyweVUhHjn5qLEM1jkmIOD/E67k0zFXQwB6BCCQHG8=; b=i6oZxA9Menzrj2+RIUZhe/a9VKO9ZJAb8ZnSa2SlPkqampHpV6sEj9ZPTxuCD0pz7d7q7DsQWmFIoGnpYkIhlceJ0tSnVyLYE+f/vzAQH9CuBtHrk6ERVaYobB8K/U1bxG8BlSRu6+QkSjaonG4IOZKS0HfCwYI+5f9SsaYFhUM= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1740622812728404.97788858062574; Wed, 26 Feb 2025 18:20:12 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.897375.1306100 (Exim 4.92) (envelope-from ) id 1tnTUe-0007xO-SN; Thu, 27 Feb 2025 02:19:20 +0000 Received: by outflank-mailman (output) from mailman id 897375.1306100; Thu, 27 Feb 2025 02:19:20 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1tnTUe-0007wr-MH; Thu, 27 Feb 2025 02:19:20 +0000 Received: by outflank-mailman (input) for mailman id 897375; Thu, 27 Feb 2025 02:19:19 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1tnTUd-0005qU-A1 for xen-devel@lists.xenproject.org; Thu, 27 Feb 2025 02:19:19 +0000 Received: from mail-pj1-x1049.google.com (mail-pj1-x1049.google.com [2607:f8b0:4864:20::1049]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 3f2266a6-f4b1-11ef-9aaf-95dc52dad729; Thu, 27 Feb 2025 03:19:18 +0100 (CET) Received: by mail-pj1-x1049.google.com with SMTP id 98e67ed59e1d1-2fe8fdfdd94so1107722a91.0 for ; Wed, 26 Feb 2025 18:19:18 -0800 (PST) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 3f2266a6-f4b1-11ef-9aaf-95dc52dad729 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1740622757; x=1741227557; darn=lists.xenproject.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=xfyweVUhHjn5qLEM1jkmIOD/E67k0zFXQwB6BCCQHG8=; b=vH6LX4pc8jDNnYlm0y6WFbo5ENE3fg7aY4WY3XnGsbseK8A8a7Ki/9jVqGPL2NVGil mzTm9mcTazopeT1lUDrgR00ynR7FtMGm9Ohxf0VPSDr2HLXYVagvipqESwPO3QWc7n26 fIllSZ73iRDc4sFXFrsfWDEsWVssI+iEPJ8EaCRk6Zz+uhM0iIhl+91IuJMnh5PCBGk0 O8p7YCSpX0eupP8TIp0HPbBiNlGQ9dNnAQq0viE3mp7YQLzJ9dhwoSuaMXtowRFczsN7 YNY/ad7Kx/8DjYhBxzYD48n5I7jcB8/fqGLGyOgA+vG9kn51oFw+Dx0tukiNloTwaci4 2LJQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740622757; x=1741227557; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=xfyweVUhHjn5qLEM1jkmIOD/E67k0zFXQwB6BCCQHG8=; b=maiteFCWC33pDdQ+TkHhG2d7nrDVPRlPt4+OoGD2+ykX9FaO6divq7cimmMBiTo5G/ Z4MyIHcI5OvpIVg82Caw/w5oZ8Zk88D+F3FkBf/byx7Tkb6NJEIwE+mF9dsykQwvHUhA QTIA/GssSHNrNOHpLSyavurigHKUQlyPS/CoBDaaem2F34GslwualWd+p9X3eXhJ6Voz 6RnfrOLcoZBenT+7tQ7lVrCO6lD0JkH7tWK4q5Z1nsCcouEbnKsOCUMAae9hz7fSEnLA ax3x7mQe7vVcjfBRvyV4X0OOA8kX5lciuGJfjz1UeBGaZ67InrNVpPEraV/FEN+oWUa7 pNKg== X-Forwarded-Encrypted: i=1; AJvYcCVyg6WrBMC+zHchBmFc8qFz3OvdUd/pcejIquQaZZs9g8xDyZyViVms1RJqD6PIqvJ69un0RkJxeb8=@lists.xenproject.org X-Gm-Message-State: AOJu0YzPfWdsyJMOgeeqwKi0h17aqgOHOMzCy2uOa2jVuR72z6wUf0bg 82EBDlVgSRTKdKOrESvUKc9J1M3qV/ra84/jMum6UHduy2M8WEERFqg6LEGaVYEfBijzNP5r3Xr PBA== X-Google-Smtp-Source: AGHT+IGK7T2uVtHCkzgg9idpaqI5CgjOHQ8lJUTz55dy8O8u0q+xXKgUvujnuayeBIrgMdHIgLk5Hrx+hJs= X-Received: from pjbta8.prod.google.com ([2002:a17:90b:4ec8:b0:2fa:1771:e276]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:2d48:b0:2ee:d7d3:3019 with SMTP id 98e67ed59e1d1-2fe7e31f7c1mr10280973a91.12.1740622757149; Wed, 26 Feb 2025 18:19:17 -0800 (PST) Reply-To: Sean Christopherson Date: Wed, 26 Feb 2025 18:18:24 -0800 In-Reply-To: <20250227021855.3257188-1-seanjc@google.com> Mime-Version: 1.0 References: <20250227021855.3257188-1-seanjc@google.com> X-Mailer: git-send-email 2.48.1.711.g2feabab25a-goog Message-ID: <20250227021855.3257188-9-seanjc@google.com> Subject: [PATCH v2 08/38] clocksource: hyper-v: Register sched_clock save/restore iff it's necessary From: Sean Christopherson To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "Kirill A. Shutemov" , Paolo Bonzini , Sean Christopherson , Juergen Gross , "K. Y. Srinivasan" , Haiyang Zhang , Wei Liu , Dexuan Cui , Ajay Kaher , Jan Kiszka , Andy Lutomirski , Peter Zijlstra , Daniel Lezcano , John Stultz Cc: linux-kernel@vger.kernel.org, linux-coco@lists.linux.dev, kvm@vger.kernel.org, virtualization@lists.linux.dev, linux-hyperv@vger.kernel.org, xen-devel@lists.xenproject.org, Tom Lendacky , Nikunj A Dadhania X-ZohoMail-DKIM: pass (identity @google.com) X-ZM-MESSAGEID: 1740622813699019000 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Register the Hyper-V timer callbacks or saving/restoring its PV sched_clock if and only if the timer is actually being used for sched_clock. Currently, Hyper-V overrides the save/restore hooks if the reference TSC available, whereas the Hyper-V timer code only overrides sched_clock if the reference TSC is available *and* it's not invariant. The flaw is effectively papered over by invoking the "old" save/restore callbacks as part of save/restore, but that's unnecessary and fragile. To avoid introducing more complexity, and to allow for additional cleanups of the PV sched_clock code, move the save/restore hooks and logic into hyperv_timer.c and simply wire up the hooks when overriding sched_clock itself. Note, while the Hyper-V timer code is intended to be architecture neutral, CONFIG_PARAVIRT is firmly x86-only, i.e. adding a small amount of x86 specific code (which will be reduced in future cleanups) doesn't meaningfully pollute generic code. Signed-off-by: Sean Christopherson Reviewed-by: Michael Kelley Tested-by: Michael Kelley --- arch/x86/kernel/cpu/mshyperv.c | 58 ------------------------------ drivers/clocksource/hyperv_timer.c | 50 ++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 58 deletions(-) diff --git a/arch/x86/kernel/cpu/mshyperv.c b/arch/x86/kernel/cpu/mshyperv.c index aa60491bf738..174f6a71c899 100644 --- a/arch/x86/kernel/cpu/mshyperv.c +++ b/arch/x86/kernel/cpu/mshyperv.c @@ -223,63 +223,6 @@ static void hv_machine_crash_shutdown(struct pt_regs *= regs) hyperv_cleanup(); } #endif /* CONFIG_CRASH_DUMP */ - -static u64 hv_ref_counter_at_suspend; -static void (*old_save_sched_clock_state)(void); -static void (*old_restore_sched_clock_state)(void); - -/* - * Hyper-V clock counter resets during hibernation. Save and restore clock - * offset during suspend/resume, while also considering the time passed - * before suspend. This is to make sure that sched_clock using hv tsc page - * based clocksource, proceeds from where it left off during suspend and - * it shows correct time for the timestamps of kernel messages after resum= e. - */ -static void save_hv_clock_tsc_state(void) -{ - hv_ref_counter_at_suspend =3D hv_read_reference_counter(); -} - -static void restore_hv_clock_tsc_state(void) -{ - /* - * Adjust the offsets used by hv tsc clocksource to - * account for the time spent before hibernation. - * adjusted value =3D reference counter (time) at suspend - * - reference counter (time) now. - */ - hv_adj_sched_clock_offset(hv_ref_counter_at_suspend - hv_read_reference_c= ounter()); -} - -/* - * Functions to override save_sched_clock_state and restore_sched_clock_st= ate - * functions of x86_platform. The Hyper-V clock counter is reset during - * suspend-resume and the offset used to measure time needs to be - * corrected, post resume. - */ -static void hv_save_sched_clock_state(void) -{ - old_save_sched_clock_state(); - save_hv_clock_tsc_state(); -} - -static void hv_restore_sched_clock_state(void) -{ - restore_hv_clock_tsc_state(); - old_restore_sched_clock_state(); -} - -static void __init x86_setup_ops_for_tsc_pg_clock(void) -{ - if (!(ms_hyperv.features & HV_MSR_REFERENCE_TSC_AVAILABLE)) - return; - - old_save_sched_clock_state =3D x86_platform.save_sched_clock_state; - x86_platform.save_sched_clock_state =3D hv_save_sched_clock_state; - - old_restore_sched_clock_state =3D x86_platform.restore_sched_clock_state; - x86_platform.restore_sched_clock_state =3D hv_restore_sched_clock_state; -} #endif /* CONFIG_HYPERV */ =20 static uint32_t __init ms_hyperv_platform(void) @@ -635,7 +578,6 @@ static void __init ms_hyperv_init_platform(void) =20 /* Register Hyper-V specific clocksource */ hv_init_clocksource(); - x86_setup_ops_for_tsc_pg_clock(); hv_vtl_init_platform(); #endif /* diff --git a/drivers/clocksource/hyperv_timer.c b/drivers/clocksource/hyper= v_timer.c index f00019b078a7..86a55167bf5d 100644 --- a/drivers/clocksource/hyperv_timer.c +++ b/drivers/clocksource/hyperv_timer.c @@ -534,10 +534,60 @@ static __always_inline void hv_setup_sched_clock(void= *sched_clock) sched_clock_register(sched_clock, 64, NSEC_PER_SEC); } #elif defined CONFIG_PARAVIRT +static u64 hv_ref_counter_at_suspend; +static void (*old_save_sched_clock_state)(void); +static void (*old_restore_sched_clock_state)(void); + +/* + * Hyper-V clock counter resets during hibernation. Save and restore clock + * offset during suspend/resume, while also considering the time passed + * before suspend. This is to make sure that sched_clock using hv tsc page + * based clocksource, proceeds from where it left off during suspend and + * it shows correct time for the timestamps of kernel messages after resum= e. + */ +static void save_hv_clock_tsc_state(void) +{ + hv_ref_counter_at_suspend =3D hv_read_reference_counter(); +} + +static void restore_hv_clock_tsc_state(void) +{ + /* + * Adjust the offsets used by hv tsc clocksource to + * account for the time spent before hibernation. + * adjusted value =3D reference counter (time) at suspend + * - reference counter (time) now. + */ + hv_adj_sched_clock_offset(hv_ref_counter_at_suspend - hv_read_reference_c= ounter()); +} +/* + * Functions to override save_sched_clock_state and restore_sched_clock_st= ate + * functions of x86_platform. The Hyper-V clock counter is reset during + * suspend-resume and the offset used to measure time needs to be + * corrected, post resume. + */ +static void hv_save_sched_clock_state(void) +{ + old_save_sched_clock_state(); + save_hv_clock_tsc_state(); +} + +static void hv_restore_sched_clock_state(void) +{ + restore_hv_clock_tsc_state(); + old_restore_sched_clock_state(); +} + static __always_inline void hv_setup_sched_clock(void *sched_clock) { /* We're on x86/x64 *and* using PV ops */ paravirt_set_sched_clock(sched_clock); + + old_save_sched_clock_state =3D x86_platform.save_sched_clock_state; + x86_platform.save_sched_clock_state =3D hv_save_sched_clock_state; + + old_restore_sched_clock_state =3D x86_platform.restore_sched_clock_state; + x86_platform.restore_sched_clock_state =3D hv_restore_sched_clock_state; } #else /* !CONFIG_GENERIC_SCHED_CLOCK && !CONFIG_PARAVIRT */ static __always_inline void hv_setup_sched_clock(void *sched_clock) {} --=20 2.48.1.711.g2feabab25a-goog