From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f200.google.com (mail-pl1-f200.google.com [209.85.214.200]) (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 6574F4A970C for ; Thu, 6 Aug 2026 23:37:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.200 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786059433; cv=none; b=lNsEzLb6V8TCk5G4ODVi1c0H1AxUagI1nb5CvT8QzFuI9qHkxuGWfQo4QcunbcsZtgTVh7NH42db85H9ZAkH2QZFh69b0kV7p0FxPiH1ChnjjR1bNc43kzj3j69S65RBRNOp0N+IdAohVZsd0dSUzDNqGDXDZXHhWuL+vLYy/Ps= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786059433; c=relaxed/simple; bh=YgMzpUMx8xHWU9hl5Ir/Bh8NISf16vz/qFAMq5wPe60=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=ev5V8A9KWzTMQK8Ft9XU807UqR8ChJLv5uOQVjArxgF0eInwGtJMBunyBWeDsIRC21J4gnjMG0wteD8aQxYRFw/9PoOuFOOBZRS0Sk2vlKPASrotfVZplJMxIXtk6KewvnQ66iqO4wb7pOTng1GerT7FjA+A9EuVe+X/KmcImjE= 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=ClQQo/ZZ; arc=none smtp.client-ip=209.85.214.200 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="ClQQo/ZZ" Received: by mail-pl1-f200.google.com with SMTP id d9443c01a7336-2cc86a9ef97so45941315ad.3 for ; Thu, 06 Aug 2026 16:37:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1786059429; x=1786664229; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:reply-to:from:to:cc:subject:date:message-id :reply-to:content-type; bh=bZd2/vTLw6kP6aan4oVP2ByAHPA3GmOMxUTn1c9gmLc=; b=ClQQo/ZZt0ERn7GAwCFBzrNJxLi2sBqvPKX/+QRZQF5Whlo/HnkkquvNzb4Xf2+Tq6 q4RQydWGuo9nPwpNDgRbZV6LshrqBLHDvU+HRliyf2978/2xq7/spP+ZxU1Z7nUDF2kI 8KTOy7roXJywFl20szT6sVv5BFR6Db13w33BZuWpgiZDRNj27Egua8gWcMOvFj61t6wW vFWsuh2krOYA4hWX9HZdhYdl0kvSojI74gNXCdr0xAxnvjYIBjjlPwKOHuTD6Le+5iTh Wz7w2VIg2Xm3OrZTY3VDa2GPgleLxTNdY3gjodJE8M3sk6mYENgMqiX1pLe8g9nroL75 TX/g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1786059429; x=1786664229; h=content-type: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:content-type; bh=bZd2/vTLw6kP6aan4oVP2ByAHPA3GmOMxUTn1c9gmLc=; b=SINtlosaJZBMM2iTq+0S5q9Ujak0dOOI2saE0JDy1sbC0pEBjiVan7+UMLokbVyLVh 30u7hWuamz1OxSS5owJPI460PLdj4dvwKY/NFMD0Ry7T4vsFPSUgeIg5ldmmLkrCmQMy 29xYC8s9usmek/9lF0aSW+YNoH42Wb3Afcf2lvBxY5BmgMTmnFDugySmPBQqHFYqsf3a 9usfKJ05D8MYTCdPE2FCBtLM1bqDcflQEVNR8zP2pZ8j08N3PJHFWwT9vtelJJi6FuIW VYHMETOw/xKcWXxd4box/oiufH/ohXoapF9XJDEqKMOSp/0ehAtCq8duRGBIdG0kxBUr JJAg== X-Forwarded-Encrypted: i=1; AHgh+RrbvUa+IgyqLM7dF7UmJ2ehJ0gI29oKsisJNN+v50mBiFWG5uOWOUeB/35SOYvP8+8vEutOttI0rvcNZys=@vger.kernel.org X-Gm-Message-State: AOJu0YynR+zz4+zYGQBKhQYTCTSyYnaGBb5GpEt9ETbntOyT+aU9F7UR udrWPAMMRCDgkC6o/u8kdvHyi04eCO/+Z+soKaRLT/GYPCb+m4ZsWX1X45HztBGf1YhWAM078Zg Z06Gn8g== X-Received: from pjtz20.prod.google.com ([2002:a17:90a:cb14:b0:36b:7f07:6fcd]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:1c84:b0:38f:1e1a:515e with SMTP id 98e67ed59e1d1-3903c5b1688mr16494071a91.14.1786059429092; Thu, 06 Aug 2026 16:37:09 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 6 Aug 2026 16:35:53 -0700 In-Reply-To: <20260806233609.212337-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: <20260806233609.212337-1-seanjc@google.com> X-Mailer: git-send-email 2.55.0.679.g6767b8d81c-goog Message-ID: <20260806233609.212337-37-seanjc@google.com> Subject: [PATCH v6 36/51] x86/paravirt: Pass sched_clock save/restore helpers during registration From: Sean Christopherson To: Kiryl Shutsemau , Rick Edgecombe , Sean Christopherson , Paolo Bonzini , "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: Vitaly Kuznetsov , Broadcom internal kernel review list , Boris Ostrovsky , Stephen Boyd , Miroslav Lichvar , 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 , David Woodhouse , David Woodhouse , 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 Reviewed-by: Michael Kelley 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 94fec990a02c..3598de5297f8 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 60fcc3020792..d4f7cb127235 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.679.g6767b8d81c-goog