From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 03A874028E6 for ; Fri, 29 May 2026 14:45:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.202 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780065922; cv=none; b=uMJHB6SCTqSRMdKjuz8igkVKB0t57WFk94VDT7arbAslcnkKrpXmxkcXU5eXETMC5kbr0Ukm50Y/Bmr0mjmVHoQ4hPgsRZz6y9IAK3jpri/Z9FkyzqsWzeVWceSae4xjTdsB8Qra9Y4zYwz5E1vdtw90PCeBMB4h2csgHG9zHWU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780065922; c=relaxed/simple; bh=iG71vVceguXTLIhuEypwN50IDc3m1rOU1h5u0e/+Vxc=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Q+e5ZNrM0fa5FN5rlm4e8BLDNfFNygkmVh//8MclkkkiYPJSM9l8Z8qNfqJkdsJ5lQ29WbFbIqvuLN4Eq1DAxGsLvZ6XJO1WoKHgrD03UD1VHJ6pRNjANAzOdJgqQRP8hFloHEB2WbyeHn1u3JDwwGp5p6ti6Ow3lwcMiPk2RLQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=FoM4v5R3; arc=none smtp.client-ip=209.85.214.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="FoM4v5R3" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-2bd5b20aaa6so149193095ad.2 for ; Fri, 29 May 2026 07:45:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1780065918; x=1780670718; darn=vger.kernel.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=CjqUsxyZ/EUi7tEkmfz6ySaHUFaiYUv5qsrOzmWsVSE=; b=FoM4v5R3JZXTyLVCKgBa93vP38pxMx3lHP6DzffW3Z1+nKpgGLBojWqknktvXjBYUu trcIUePnU+bN+DfZSEOlmgfxfTgYunq29jHj873027hkcVxnOnHxDqFOapkfg5Hl2ur/ DqK6bmeFM9jA7p2fyjLpYFqjTVyFE0p9rubze1s76XlCDV5hmswLlmY3W2Q3A6VCsRuw SCAcX7pm6Fp2hIFJOCPC9+LwGoRieD0ceJ5c8AAbcvAHfxj+x8TKJW7GponNV5uacI5O WA0/OLYE1eCg2y/+psi0zB8Slb5rKuUltnUt6BwIdvLueIzl2ATeRJ4ODaQvENneQQvO ec1w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780065918; x=1780670718; 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=CjqUsxyZ/EUi7tEkmfz6ySaHUFaiYUv5qsrOzmWsVSE=; b=b2/ZQfB3866BOtEas0zSzoSATWRsPKMyKe1EPCepHb8VGa+MMh1/OKDaX+x0e9F43m WC7E7Ii1M7REvyhRsq/+J8EN2064f8vFZVG6NnbJAUx6H2q844sLZ9YWrZqCZ+r15b9w 7qceb3scxFEyg5+Q38MJYo5uQzsKASf5D/BTApLymSs6DoKbJUsn+lYJ2KXRxYG0mKzb 6qUHmjge/nCB30C9v5l8vhADy94Hwtxz6pr3Ge+A/tk5A7qCmTPJKxoGi0qFng98vFZd AjLTzK6mq9fN0voMT2aNGxqiMRPNuHE2Fp+gOp1wA9/w77zXP4eHtc6EbH9dAuc7NHV/ SQ5A== X-Forwarded-Encrypted: i=1; AFNElJ9yfYqkx8Gwa8zOLhCsV0IEgtDwNMzz+SKetM/V1jYHvF924hnW+MyvdW2WW+QzXvFAdHO40UZPmb67Zl8=@vger.kernel.org X-Gm-Message-State: AOJu0YxdltzmoR8mhm4RbVcdUQn9xr+/YSFPk1RL8ZKruXFNlPhwhFiN YqzyfuzKVHtbq4ibhhkURjRq63kQs/K+vYGSmPzwmTtOypiCPKhTVH3eXCv5fxz5ETD1S11yOqG 5Ls3h3g== X-Received: from plgy12.prod.google.com ([2002:a17:903:22cc:b0:2bf:21c9:7d27]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:1c6:b0:2bf:3309:ecce with SMTP id d9443c01a7336-2bf3686593amr1360175ad.28.1780065918022; Fri, 29 May 2026 07:45:18 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 29 May 2026 07:44:09 -0700 In-Reply-To: <20260529144435.704127-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-hyperv@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260529144435.704127-1-seanjc@google.com> X-Mailer: git-send-email 2.54.0.823.g6e5bcc1fc9-goog Message-ID: <20260529144435.704127-23-seanjc@google.com> Subject: [PATCH v4 22/47] clocksource: hyper-v: Register sched_clock save/restore iff it's necessary From: Sean Christopherson To: Paolo Bonzini , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, Kiryl Shutsemau , Sean Christopherson , "K. Y. Srinivasan" , Haiyang Zhang , Wei Liu , Dexuan Cui , Long Li , Ajay Kaher , Alexey Makhalov , Jan Kiszka , Andy Lutomirski , Peter Zijlstra , Juergen Gross , Daniel Lezcano , John Stultz Cc: "H. Peter Anvin" , Rick Edgecombe , Vitaly Kuznetsov , Broadcom internal kernel review list , Boris Ostrovsky , Stephen Boyd , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-coco@lists.linux.dev, linux-hyperv@vger.kernel.org, virtualization@lists.linux.dev, xen-devel@lists.xenproject.org, David Woodhouse , Tom Lendacky , Nikunj A Dadhania , David Woodhouse , Michael Kelley , Thomas Gleixner Content-Type: text/plain; charset="UTF-8" Register the Hyper-V reference counter (refcounter) callbacks for saving and restoring its PV sched_clock, if and only if the refcounter is actually being used for sched_clock. Currently, Hyper-V overrides the save/restore hooks if the reference TSC available, whereas the Hyper-V refcounter 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 refcounter 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. Reviewed-by: Michael Kelley Tested-by: Michael Kelley Acked-by: Wei Liu Signed-off-by: Sean Christopherson --- 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 f8653fc05a40..2403231fd4b0 100644 --- a/arch/x86/kernel/cpu/mshyperv.c +++ b/arch/x86/kernel/cpu/mshyperv.c @@ -275,63 +275,6 @@ static void hv_guest_crash_shutdown(struct pt_regs *regs) } #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 resume. - */ -static void save_hv_clock_tsc_state(void) -{ - hv_ref_counter_at_suspend = 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 = reference counter (time) at suspend - * - reference counter (time) now. - */ - hv_adj_sched_clock_offset(hv_ref_counter_at_suspend - hv_read_reference_counter()); -} - -/* - * Functions to override save_sched_clock_state and restore_sched_clock_state - * 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 = x86_platform.save_sched_clock_state; - x86_platform.save_sched_clock_state = hv_save_sched_clock_state; - - old_restore_sched_clock_state = x86_platform.restore_sched_clock_state; - x86_platform.restore_sched_clock_state = hv_restore_sched_clock_state; -} - #ifdef CONFIG_X86_64 DEFINE_STATIC_CALL(hv_hypercall, hv_std_hypercall); EXPORT_STATIC_CALL_TRAMP_GPL(hv_hypercall); @@ -739,7 +682,6 @@ static void __init ms_hyperv_init_platform(void) /* 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/hyperv_timer.c index e9f5034a1bc8..72b966340a46 100644 --- a/drivers/clocksource/hyperv_timer.c +++ b/drivers/clocksource/hyperv_timer.c @@ -537,10 +537,60 @@ static __always_inline void hv_setup_sched_clock(void *sched_clock) #elif defined CONFIG_PARAVIRT #include +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 resume. + */ +static void save_hv_clock_tsc_state(void) +{ + hv_ref_counter_at_suspend = 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 = reference counter (time) at suspend + * - reference counter (time) now. + */ + hv_adj_sched_clock_offset(hv_ref_counter_at_suspend - hv_read_reference_counter()); +} +/* + * Functions to override save_sched_clock_state and restore_sched_clock_state + * 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 = x86_platform.save_sched_clock_state; + x86_platform.save_sched_clock_state = hv_save_sched_clock_state; + + old_restore_sched_clock_state = x86_platform.restore_sched_clock_state; + x86_platform.restore_sched_clock_state = hv_restore_sched_clock_state; } #else /* !CONFIG_GENERIC_SCHED_CLOCK && !CONFIG_PARAVIRT */ static __always_inline void hv_setup_sched_clock(void *sched_clock) {} -- 2.54.0.823.g6e5bcc1fc9-goog