From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.201]) (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 1B1DD423F99 for ; Wed, 1 Jul 2026 19:33:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782934394; cv=none; b=f3rZLYbe1bjg7Bvrt5vDn59xfCLPzwZpZnZV1lpdW99uu3tejNUM17FgGfhYJnvRcd3Qw29L1Y6vDbm+lFYdzmZBX5GUPWhYG6CwJiWFYBhfGXTXlIC+rHR05v1fiDIGuvZK27O85VnaFZKJb06GLoZK1RqJv0yK73gOkcGg4CQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782934394; c=relaxed/simple; bh=3u/3BcPYx5GLwibFsRZKXwskFyQsjX3xT9ahxKGNPqU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=GJkSX2+PtnIkkEkaqnenkENdcXod6VaMF4z4iEAgzGuoQI5aeIMaFfrTtHeJxbIpLW03eMcZUCBUHpqZzO7mG+5Hb7/FYGS+og3RSaWOOq/B0HQWKCluiBb4HKNQq0w5w3Eoab95dCyS5aZ89stFVBeQpQYHdD0wVi4ptcplZtI= 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=h9KwZzOQ; arc=none smtp.client-ip=209.85.210.201 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="h9KwZzOQ" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-845ea8fd3easo1687930b3a.0 for ; Wed, 01 Jul 2026 12:33:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1782934391; x=1783539191; 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=V+q3+pR5470wsF5QmCXBVWy55xlVZlxhghTF4iDuO4s=; b=h9KwZzOQiBbfPqW214plNu6AgzswVYC6EeBisyTt3x540JyIcoj6KRPcXGrmQEsll7 s/gbd6aCxwrVZrYPZqfKA+PDa3JmFmQyUPEUsDFNRD3SoahC7pA47TVPLIm73aq/wAWL buPQaoDPFRxP2R/pRnoTKoMc/8/mNTp0eqg784H1h2QqUZfJXQLlKv4EH8HpRSK/WOLE xda8CvZB9p0sa4VrSAsJ9jHOV01iVuaTJDKZQGEaw6gD4lkDb96icVoMJqoEH0nQHhSR dHNffh6RmYl5+erKa0Eo1hCBxZga085A+T0m6MwYYCpQtCWz5A/BdR18x0sYERT0CLST Y6ig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1782934391; x=1783539191; 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=V+q3+pR5470wsF5QmCXBVWy55xlVZlxhghTF4iDuO4s=; b=Z4OWPADvISkKIzYYh++/8xy0YA3nxS3yhNL08EwxPou2qYnKNHDdZIZRjaXbbOdkvb bA6cloEB4nOmtYyNtQKfxcB8f4L0xjnuDWuJylNnTogjlUfo++cqOtRruwsTwsnOQPHD blwwL1J/kypxurdVfB24M5Ohfdqdn2xUmLEVza8nik8CfaUve7cFDFyJdTGDTWDst96D MWH1cdvPiI/+VRzJcloq6Nxo2yTl4Ku7zldRSAVfnyUjrDtKoj2qI+XXI1XsQftlrDQw ZhhqTmiNXashvhFp6O6mjTYdQyPAfxw/+DA/SS8TSoae8bPr/wI9KBxGpaWiDO/e/4/m jNKw== X-Forwarded-Encrypted: i=1; AFNElJ/2/uHqLfo8FxPOolSCfL+GsqYMc0BzSvGrlcrxBelytk3kLgLlynCu/hmYEcgaWzfR3k52yaxYH+qccXI=@vger.kernel.org X-Gm-Message-State: AOJu0YwF7+6gYWsAnRzD9raMx83AquwkA8Mx70gDAow3b5T5OGAdxdET 8F1tMJI+7AH1Ghwq2qG+WcqLz6PewmJ7LJ7syd/kgOYPkUq98rpIjED20AoIaUFEBIPeSPrq5aO PEgq2Bw== X-Received: from pfblh19.prod.google.com ([2002:a05:6a00:7113:b0:847:926b:dc16]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:2d0e:b0:845:d284:9e11 with SMTP id d2e1a72fcca58-847c51d242emr1964528b3a.56.1782934390999; Wed, 01 Jul 2026 12:33:10 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 1 Jul 2026 12:31:57 -0700 In-Reply-To: <20260701193212.749551-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: <20260701193212.749551-1-seanjc@google.com> X-Mailer: git-send-email 2.55.0.rc0.799.gd6f94ed593-goog Message-ID: <20260701193212.749551-37-seanjc@google.com> Subject: [PATCH v5 36/51] x86/paravirt: Pass sched_clock save/restore helpers during registration From: Sean Christopherson To: Jonathan Corbet , Paolo Bonzini , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, Kiryl Shutsemau , Rick Edgecombe , 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: Shuah Khan , "H. Peter Anvin" , Vitaly Kuznetsov , Broadcom internal kernel review list , Boris Ostrovsky , Stephen Boyd , linux-doc@vger.kernel.org, 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, Tom Lendacky , Nikunj A Dadhania , David Woodhouse , David Woodhouse , Michael Kelley , Thomas Gleixner Content-Type: text/plain; charset="UTF-8" Pass in a PV clock's save/restore helpers when configuring sched_clock instead of relying on each PV clock to manually set the save/restore hooks. In addition to bringing sanity to the code, this will allow gracefully "rejecting" a PV sched_clock, e.g. when running as a CoCo guest that has access to a "secure" TSC. No functional change intended. Reviewed-by: David Woodhouse Signed-off-by: Sean Christopherson --- arch/x86/include/asm/timer.h | 9 ++++++--- arch/x86/kernel/cpu/vmware.c | 8 +++----- arch/x86/kernel/kvmclock.c | 6 +++--- arch/x86/kernel/tsc.c | 5 ++++- arch/x86/xen/time.c | 5 ++--- drivers/clocksource/hyperv_timer.c | 6 ++---- 6 files changed, 20 insertions(+), 19 deletions(-) diff --git a/arch/x86/include/asm/timer.h b/arch/x86/include/asm/timer.h index fe41d40a9ae6..e97cd1ae03d1 100644 --- a/arch/x86/include/asm/timer.h +++ b/arch/x86/include/asm/timer.h @@ -14,11 +14,14 @@ extern int no_timer_check; extern bool using_native_sched_clock(void); #ifdef CONFIG_PARAVIRT -void __paravirt_set_sched_clock(u64 (*func)(void), bool stable); +void __paravirt_set_sched_clock(u64 (*func)(void), bool stable, + void (*save)(void), void (*restore)(void)); -static inline void paravirt_set_sched_clock(u64 (*func)(void)) +static inline void paravirt_set_sched_clock(u64 (*func)(void), + void (*save)(void), + void (*restore)(void)) { - __paravirt_set_sched_clock(func, true); + __paravirt_set_sched_clock(func, true, save, restore); } #endif diff --git a/arch/x86/kernel/cpu/vmware.c b/arch/x86/kernel/cpu/vmware.c index 5c1ccaf4a25e..232255279a6e 100644 --- a/arch/x86/kernel/cpu/vmware.c +++ b/arch/x86/kernel/cpu/vmware.c @@ -347,11 +347,9 @@ static void __init vmware_paravirt_ops_setup(void) vmware_cyc2ns_setup(); - if (vmw_sched_clock) { - paravirt_set_sched_clock(vmware_sched_clock); - x86_platform.save_sched_clock_state = x86_init_noop; - x86_platform.restore_sched_clock_state = x86_init_noop; - } + if (vmw_sched_clock) + paravirt_set_sched_clock(vmware_sched_clock, + x86_init_noop, x86_init_noop); if (vmware_is_stealclock_available()) { has_steal_clock = true; diff --git a/arch/x86/kernel/kvmclock.c b/arch/x86/kernel/kvmclock.c index 07e875738c39..5b9955343199 100644 --- a/arch/x86/kernel/kvmclock.c +++ b/arch/x86/kernel/kvmclock.c @@ -158,7 +158,9 @@ static void kvm_restore_sched_clock_state(void) static inline void kvm_sched_clock_init(bool stable) { kvm_sched_clock_offset = kvm_clock_read(); - __paravirt_set_sched_clock(kvm_sched_clock_read, stable); + __paravirt_set_sched_clock(kvm_sched_clock_read, stable, + kvm_save_sched_clock_state, + kvm_restore_sched_clock_state); pr_info("kvm-clock: using sched offset of %llu cycles", kvm_sched_clock_offset); @@ -367,8 +369,6 @@ void __init kvmclock_init(bool prefer_tsc) #ifdef CONFIG_SMP x86_cpuinit.early_percpu_clock_init = kvm_setup_secondary_clock; #endif - x86_platform.save_sched_clock_state = kvm_save_sched_clock_state; - x86_platform.restore_sched_clock_state = kvm_restore_sched_clock_state; kvm_get_preset_lpj(); /* diff --git a/arch/x86/kernel/tsc.c b/arch/x86/kernel/tsc.c index 7473dcab4775..83353d643150 100644 --- a/arch/x86/kernel/tsc.c +++ b/arch/x86/kernel/tsc.c @@ -280,12 +280,15 @@ bool using_native_sched_clock(void) return static_call_query(pv_sched_clock) == native_sched_clock; } -void __paravirt_set_sched_clock(u64 (*func)(void), bool stable) +void __paravirt_set_sched_clock(u64 (*func)(void), bool stable, + void (*save)(void), void (*restore)(void)) { if (!stable) clear_sched_clock_stable(); static_call_update(pv_sched_clock, func); + x86_platform.save_sched_clock_state = save; + x86_platform.restore_sched_clock_state = restore; } #else u64 sched_clock_noinstr(void) __attribute__((alias("native_sched_clock"))); diff --git a/arch/x86/xen/time.c b/arch/x86/xen/time.c index 477441752f40..8cd8bfaf1320 100644 --- a/arch/x86/xen/time.c +++ b/arch/x86/xen/time.c @@ -566,13 +566,12 @@ static void __init xen_init_time_common(void) { xen_sched_clock_offset = xen_clocksource_read(); static_call_update(pv_steal_clock, xen_steal_clock); - paravirt_set_sched_clock(xen_sched_clock); + /* * Xen has paravirtualized suspend/resume and so doesn't use the common * x86 sched_clock save/restore hooks. */ - x86_platform.save_sched_clock_state = x86_init_noop; - x86_platform.restore_sched_clock_state = x86_init_noop; + paravirt_set_sched_clock(xen_sched_clock, x86_init_noop, x86_init_noop); x86_init.hyper.get_tsc_khz = xen_tsc_khz; x86_platform.get_wallclock = xen_get_wallclock; diff --git a/drivers/clocksource/hyperv_timer.c b/drivers/clocksource/hyperv_timer.c index 220668207d19..8ee7a9de0f4f 100644 --- a/drivers/clocksource/hyperv_timer.c +++ b/drivers/clocksource/hyperv_timer.c @@ -570,10 +570,8 @@ static void hv_restore_sched_clock_state(void) 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); - - x86_platform.save_sched_clock_state = hv_save_sched_clock_state; - x86_platform.restore_sched_clock_state = hv_restore_sched_clock_state; + paravirt_set_sched_clock(sched_clock, hv_save_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.55.0.rc0.799.gd6f94ed593-goog