From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pf1-f202.google.com (mail-pf1-f202.google.com [209.85.210.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 C40E041212E for ; Fri, 15 May 2026 19:21:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.202 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778872884; cv=none; b=P98LKVQVKpR/9qlajdNFRh+oyPOBf/4UbXkOMSVb7kJjVOgLteSYpbko9LKMWHwwCaX6K/E1PY5m7g+tuiUcMG2KUZo2T8+oBp2PcUOLir1g96oDJp9YKyZaV3yT6R9vWuNFJiwpoSVXlrYhnUvYG4c0GLs98ybUwmlP/NoXxrw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778872884; c=relaxed/simple; bh=+wUr561K82TnIIpq5POLLP9qm+OKlIkzSL6qT55+ohk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=p0zECwBorVi7Ri8GXAe5/EwFFd0MkK9IU5b6G6oXulZJ52aG8Cc1EdOQwMGyYZFmq7IUr58hns+m4OpLAIu804htcLg/4TzU/JKPWdc4jMKStEh+7xrGVAIUkmFnUcm8oeAqaOgL4HDzdx43qKE73KpRuBGunsyG64CIZ63Xtuk= 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=rzfmGsNG; arc=none smtp.client-ip=209.85.210.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="rzfmGsNG" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-82fd55bf6cdso177972b3a.3 for ; Fri, 15 May 2026 12:21:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1778872881; x=1779477681; darn=lists.linux.dev; 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=rGCW9kF5gS8wHa+YnUvDY3DL62KvWmhRkotZ7eduTfw=; b=rzfmGsNG/8VYb3P6qGnuXBo2m/dlV1K1CMAY9Mv5rXcKkm0Lprt8+Luio6P9I+f6Kk dHR29rO0aXDn8kmr3Ig47UGSPRexFUGP/F5RpZEjWoO6n92+mZ18hUsPg85CUnNe6hX8 VstX7YWWXUHGWXKABvAIYwLv0D5K2Q/lz/6ZlQeejIzUzH/Ztvl0SbVG8SMhptUMviou icUzyI7iQLKRKz/d8Ws2jGOCH79Y6X2QSHnL5DXahrkh44CMSV9x3g45n88W0f1zQFGe EMhjOanVbDQAINhHzbbvahIOEYkkn99KQeBSwBqTLkotcPzXtbUhZbl0aHa4HJt7lfSM hkJA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778872881; x=1779477681; 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=rGCW9kF5gS8wHa+YnUvDY3DL62KvWmhRkotZ7eduTfw=; b=I+mBcE2bnNvkzX0l6qALyQaR844RExH2448mvra4l1IBAkZcZvLob9tyuuQeP3dv6u VkXCXXhHG5YRfZo4s/mbAY7ogYw38PzI4tjOHC+bQpW6IZJdj31fkfo22oNruplPvwG3 jIj5gkTh9LTvVgVus7UXIhm8YkjH0Yw6i/QVRzEanXzAKYo7Sm+nPYgQaTGP4sXv/M/X PBblREE6Bz2Gdk9DUmteRazzmdd9JEaUbgSH8EFvkkFsiU8ZDtmKpEo9k+pum3UHKYbP UEofNJuEpzzq1H1NcjbDszreP2ymaImesJb3mjjdzAieIV7EwQIhqkPJhG3QE997ZsGO A6yw== X-Forwarded-Encrypted: i=1; AFNElJ9J2kS7jdZdifufrkJEKG+BPvkM+y19d8SbvjruifUUWvQ25dzbt+PKqMSQaP92BujIXomNbR1jTfGo5Dt4fQ==@lists.linux.dev X-Gm-Message-State: AOJu0Yw4J6acYAvEa3Ml+HSrZw1f/njBARfXJYv+kQsImReY+z279++a zD3Jx3t5ULfXM74kfM81sC7sZo2Kq/+huXD+1ybbMtn3zLFU86WKxC4JYYelWJwflACJi7lu70v hjVnNbQ== X-Received: from pfkq14.prod.google.com ([2002:a05:6a00:84e:b0:839:4a33:c354]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:12e4:b0:835:405a:7e68 with SMTP id d2e1a72fcca58-83f33d9dd83mr5938852b3a.32.1778872880815; Fri, 15 May 2026 12:21:20 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 15 May 2026 12:19:39 -0700 In-Reply-To: <20260515191942.1892718-1-seanjc@google.com> Precedence: bulk X-Mailing-List: virtualization@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260515191942.1892718-1-seanjc@google.com> X-Mailer: git-send-email 2.54.0.563.g4f69b47b94-goog Message-ID: <20260515191942.1892718-39-seanjc@google.com> Subject: [PATCH v3 38/41] x86/paravirt: kvmclock: Setup kvmclock early iff it's sched_clock From: Sean Christopherson To: Kiryl Shutsemau , Paolo Bonzini , Sean Christopherson , "K. Y. Srinivasan" , Haiyang Zhang , Wei Liu , Dexuan Cui , Long Li , Ajay Kaher , Alexey Makhalov , Jan Kiszka , Dave Hansen , Andy Lutomirski , Peter Zijlstra , Juergen Gross , Daniel Lezcano , Thomas Gleixner , John Stultz Cc: Rick Edgecombe , Vitaly Kuznetsov , Broadcom internal kernel review list , Boris Ostrovsky , Stephen Boyd , x86@kernel.org, linux-coco@lists.linux.dev, kvm@vger.kernel.org, linux-hyperv@vger.kernel.org, virtualization@lists.linux.dev, linux-kernel@vger.kernel.org, xen-devel@lists.xenproject.org, Michael Kelley , Tom Lendacky , Nikunj A Dadhania , Thomas Gleixner , David Woodhouse Content-Type: text/plain; charset="UTF-8" Rework the seemingly generic x86_cpuinit_ops.early_percpu_clock_init hook into a dedicated PV sched_clock hook, as the only reason the hook exists is to allow kvmclock to enable its PV clock on secondary CPUs before the kernel tries to reference sched_clock, e.g. when grabbing a timestamp for printk. Rearranging the hook doesn't exactly reduce complexity; arguably it does the opposite. But as-is, it's practically impossible to understand *why* kvmclock needs to do early configuration. Signed-off-by: Sean Christopherson --- arch/x86/include/asm/timer.h | 8 ++++++-- arch/x86/include/asm/x86_init.h | 2 -- arch/x86/kernel/kvmclock.c | 13 ++++++------- arch/x86/kernel/smpboot.c | 3 ++- arch/x86/kernel/tsc.c | 16 +++++++++++++++- arch/x86/kernel/x86_init.c | 1 - 6 files changed, 29 insertions(+), 14 deletions(-) diff --git a/arch/x86/include/asm/timer.h b/arch/x86/include/asm/timer.h index ca5c95d48c03..ab1271bd9c3b 100644 --- a/arch/x86/include/asm/timer.h +++ b/arch/x86/include/asm/timer.h @@ -15,14 +15,18 @@ extern bool using_native_sched_clock(void); #ifdef CONFIG_PARAVIRT int __init __paravirt_set_sched_clock(u64 (*func)(void), bool stable, - void (*save)(void), void (*restore)(void)); + void (*save)(void), void (*restore)(void), + void (*start_secondary)); static __always_inline void paravirt_set_sched_clock(u64 (*func)(void), void (*save)(void), void (*restore)(void)) { - (void)__paravirt_set_sched_clock(func, true, save, restore); + (void)__paravirt_set_sched_clock(func, true, save, restore, NULL); } +void paravirt_sched_clock_start_secondary(void); +#else +static inline void paravirt_sched_clock_start_secondary(void) { } #endif /* diff --git a/arch/x86/include/asm/x86_init.h b/arch/x86/include/asm/x86_init.h index 6c8a6ead84f6..d1b3f18ea41f 100644 --- a/arch/x86/include/asm/x86_init.h +++ b/arch/x86/include/asm/x86_init.h @@ -187,13 +187,11 @@ struct x86_init_ops { /** * struct x86_cpuinit_ops - platform specific cpu hotplug setups * @setup_percpu_clockev: set up the per cpu clock event device - * @early_percpu_clock_init: early init of the per cpu clock event device * @fixup_cpu_id: fixup function for cpuinfo_x86::topo.pkg_id * @parallel_bringup: Parallel bringup control */ struct x86_cpuinit_ops { void (*setup_percpu_clockev)(void); - void (*early_percpu_clock_init)(void); void (*fixup_cpu_id)(struct cpuinfo_x86 *c, int node); bool parallel_bringup; }; diff --git a/arch/x86/kernel/kvmclock.c b/arch/x86/kernel/kvmclock.c index 0578bc448b1b..62c8ea2e6769 100644 --- a/arch/x86/kernel/kvmclock.c +++ b/arch/x86/kernel/kvmclock.c @@ -127,12 +127,13 @@ static void kvm_save_sched_clock_state(void) kvmclock_disable(); } -#ifdef CONFIG_SMP -static void kvm_setup_secondary_clock(void) +static void kvm_setup_secondary_sched_clock(void) { + if (WARN_ON_ONCE(!IS_ENABLED(CONFIG_SMP))) + return; + kvm_register_clock("secondary cpu, sched_clock setup"); } -#endif static void kvm_restore_sched_clock_state(void) { @@ -352,7 +353,8 @@ static __init void kvm_sched_clock_init(bool stable) { if (__paravirt_set_sched_clock(kvm_sched_clock_read, stable, kvm_save_sched_clock_state, - kvm_restore_sched_clock_state)) + kvm_restore_sched_clock_state, + kvm_setup_secondary_sched_clock)) return; kvm_sched_clock_offset = kvm_clock_read(); @@ -437,9 +439,6 @@ void __init kvmclock_init(void) x86_platform.get_wallclock = kvm_get_wallclock; x86_platform.set_wallclock = kvm_set_wallclock; -#ifdef CONFIG_SMP - x86_cpuinit.early_percpu_clock_init = kvm_setup_secondary_clock; -#endif kvm_get_preset_lpj(); clocksource_register_hz(&kvm_clock, NSEC_PER_SEC); diff --git a/arch/x86/kernel/smpboot.c b/arch/x86/kernel/smpboot.c index 294a8ea60298..318ae70e5e7b 100644 --- a/arch/x86/kernel/smpboot.c +++ b/arch/x86/kernel/smpboot.c @@ -78,6 +78,7 @@ #include #include #include +#include #include #include #include @@ -275,7 +276,7 @@ static void notrace __noendbr start_secondary(void *unused) cpu_init(); fpu__init_cpu(); rcutree_report_cpu_starting(raw_smp_processor_id()); - x86_cpuinit.early_percpu_clock_init(); + paravirt_sched_clock_start_secondary(); ap_starting(); diff --git a/arch/x86/kernel/tsc.c b/arch/x86/kernel/tsc.c index 7a261214fa3e..f78e86494dec 100644 --- a/arch/x86/kernel/tsc.c +++ b/arch/x86/kernel/tsc.c @@ -280,8 +280,19 @@ bool using_native_sched_clock(void) return static_call_query(pv_sched_clock) == native_sched_clock; } +#ifdef CONFIG_SMP +static void (*pv_sched_clock_start_secondary)(void) __ro_after_init; + +void paravirt_sched_clock_start_secondary(void) +{ + if (pv_sched_clock_start_secondary) + pv_sched_clock_start_secondary(); +} +#endif + int __init __paravirt_set_sched_clock(u64 (*func)(void), bool stable, - void (*save)(void), void (*restore)(void)) + void (*save)(void), void (*restore)(void), + void (*start_secondary)) { /* * Don't replace TSC with a PV clock when running as a CoCo guest and @@ -298,6 +309,9 @@ int __init __paravirt_set_sched_clock(u64 (*func)(void), bool stable, static_call_update(pv_sched_clock, func); x86_platform.save_sched_clock_state = save; x86_platform.restore_sched_clock_state = restore; +#ifdef CONFIG_SMP + pv_sched_clock_start_secondary = start_secondary; +#endif return 0; } #else diff --git a/arch/x86/kernel/x86_init.c b/arch/x86/kernel/x86_init.c index ebefb77c37bb..cbb5ee613ed5 100644 --- a/arch/x86/kernel/x86_init.c +++ b/arch/x86/kernel/x86_init.c @@ -128,7 +128,6 @@ struct x86_init_ops x86_init __initdata = { }; struct x86_cpuinit_ops x86_cpuinit = { - .early_percpu_clock_init = x86_init_noop, .setup_percpu_clockev = setup_secondary_APIC_clock, .parallel_bringup = true, }; -- 2.54.0.563.g4f69b47b94-goog