From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pf1-f199.google.com (mail-pf1-f199.google.com [209.85.210.199]) (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 CE5EC4A8228 for ; Thu, 6 Aug 2026 23:37:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.199 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786059428; cv=none; b=SINo64XLkgIw0FbWlBKzh+jl/yKChBKhh5TbOd1OtpwMVwKCIj1TV38FqVEmxkrVx9p1r03RU6diA/L4sCqmyyUcviwdS1Zq32x4JbkJ4OYGMWDjOHQwNXEAjH3W/pIE2gmqdXsfFhw/GOUG3hE73nIEUiR+TY4LSHWo3zO/DNo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786059428; c=relaxed/simple; bh=Ca5NSaZv9qqo48HD9EV20yk4xb4Lp+1A39egJhJXjjk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=uREzCSTFKn9o9z6nyGZ3Yjn8tngmZXZ48iFluAfGMtmgdNfN6WgS/DrgwbVNckkwko9k1bACeDPtbYIguyVjkIZmxu04lYd5WJhgYe/qDX1dZSz0dMaIqWhcuPUewxf7IDY1Gt4YNA7vwOlcci0e7dW6rbsR1utuRtponRp8oro= 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=axYD9JPi; arc=none smtp.client-ip=209.85.210.199 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="axYD9JPi" Received: by mail-pf1-f199.google.com with SMTP id d2e1a72fcca58-8487ed7f7beso3066709b3a.0 for ; Thu, 06 Aug 2026 16:37:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1786059425; x=1786664225; 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=EB+XxHVCdw85PrkaMnB06pus7nsirHAVeqpBTRH+yt8=; b=axYD9JPiBN+vUhOUseQksVmSY6Eb5yoCdJaPO+h9BSbqX7pYsBYbCWiVxoCHG7o8I4 xdx6SBMMtI/tTVxRbSYgkO1WPR4p/rb6gDnIAeI7M7wjFJZaCR5UOibmdLej+ZzjXhfP sHxsAz1CqhgAc/bLPEAuORyFfjxtZFeAspOel9RzCx8Y+N6yOogOd33n77VQOZ8rpTK3 PPLeSJPhrCVt+aof0FzncLexDL8XmrsWFcZk4ra8rZTsGr7HobnQ9V8OsDVgJpZ033YR Sub5eAl7yC10ZW4JdCZmUh86iCfRINW4xYjFizjFbpaYbR2X1ZyeikhPmcp0G8TKK5/1 Fggw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1786059425; x=1786664225; 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=EB+XxHVCdw85PrkaMnB06pus7nsirHAVeqpBTRH+yt8=; b=lA1Ygg9UTrYsW98Fg55/d7XrCL+dwi9gG7EpUBK2R2KGbcN7njdNjwbud2FfarUT5B Z59ssvIgF0N0vglp6g78VDssfkMtvQx6Ccw2BEIkUBlRen7kIjsjuqMfasLB4+1yRmC+ HWjxo6fH5f96X3rElRGo2EWxGJWZ4UhjHiDtNsYrxtCrV/sPlJa6Md2D+9/1g1LzlfUW DMVLqxuv4hVUlJ1NQDamDSvX3VLrVsklz9uMkAHTyYPvN2kcwodZ8hNDQaRKenHEFA5Q qJYNzvfhlLJUdKgbDST8ImoTPJCl8mAKUgLfuFfBOSw7W/MkCQNXvyQ5Ow+OB3CBs7nQ fO+Q== X-Forwarded-Encrypted: i=1; AHgh+RrDyZnC+voQ6t/bbNpjEFUUTnN/u9qeX3IrNOdWY0Vml4FUiuHjfzagJgUOj3DjkvRNkrPHEShvv231ERk=@vger.kernel.org X-Gm-Message-State: AOJu0Yw29aym2UhahQJokZkjNwosbBXr4a5LvCO0CxGLNNygblK51NPP QJLQjhnn5QdZdDxGYbaq0wijge6+Zg4oTZQJPaVRVVQ08DWO+aMsWdRmlC+CqalA8si9t53fAlB u0JXzjg== X-Received: from pfbmc40.prod.google.com ([2002:a05:6a00:76a8:b0:84c:1460:ab72]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:2441:b0:848:5951:ff78 with SMTP id d2e1a72fcca58-84f2e1525d0mr19690745b3a.36.1786059424271; Thu, 06 Aug 2026 16:37:04 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 6 Aug 2026 16:35:49 -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-33-seanjc@google.com> Subject: [PATCH v6 32/51] x86/kvmclock: Move sched_clock save/restore helpers up in kvmclock.c 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" Move kvmclock's sched_clock save/restore helper "up" so that they can (eventually) be referenced by kvm_sched_clock_init(). No functional change intended. Reviewed-by: David Woodhouse Signed-off-by: Sean Christopherson --- arch/x86/kernel/kvmclock.c | 108 ++++++++++++++++++------------------- 1 file changed, 54 insertions(+), 54 deletions(-) diff --git a/arch/x86/kernel/kvmclock.c b/arch/x86/kernel/kvmclock.c index 4bc0495f1f9e..07e875738c39 100644 --- a/arch/x86/kernel/kvmclock.c +++ b/arch/x86/kernel/kvmclock.c @@ -71,6 +71,25 @@ static int kvm_set_wallclock(const struct timespec64 *now) return -ENODEV; } +static void kvm_register_clock(char *txt) +{ + struct pvclock_vsyscall_time_info *src = this_cpu_hvclock(); + u64 pa; + + if (!src) + return; + + pa = slow_virt_to_phys(&src->pvti) | 0x01ULL; + wrmsrq(msr_kvm_system_time, pa); + pr_debug("kvm-clock: cpu %d, msr %llx, %s", smp_processor_id(), pa, txt); +} + +static void kvmclock_disable(void) +{ + if (msr_kvm_system_time) + native_write_msr(msr_kvm_system_time, 0); +} + static u64 kvm_clock_read(void) { u64 ret; @@ -112,6 +131,30 @@ static noinstr u64 kvm_sched_clock_read(void) return pvclock_clocksource_read_nowd(this_cpu_pvti()) - kvm_sched_clock_offset; } +static void kvm_save_sched_clock_state(void) +{ + /* + * Stop host writes to kvmclock immediately prior to suspend/hibernate. + * If the system is hibernating, then kvmclock will likely reside at a + * different physical address when the system awakens, and host writes + * to the old address prior to reconfiguring kvmclock would clobber + * random memory. + */ + kvmclock_disable(); +} + +#ifdef CONFIG_SMP +static void kvm_setup_secondary_clock(void) +{ + kvm_register_clock("secondary cpu clock"); +} +#endif + +static void kvm_restore_sched_clock_state(void) +{ + kvm_register_clock("primary cpu clock, resume"); +} + static inline void kvm_sched_clock_init(bool stable) { kvm_sched_clock_offset = kvm_clock_read(); @@ -124,6 +167,17 @@ static inline void kvm_sched_clock_init(bool stable) sizeof(((struct pvclock_vcpu_time_info *)NULL)->system_time)); } +void kvmclock_cpu_action(enum kvm_guest_cpu_action action) +{ + /* + * Don't disable kvmclock on the BSP during suspend. If kvmclock is + * being used for sched_clock, then it needs to be kept alive until the + * last minute, and restored as quickly as possible after resume. + */ + if (action != KVM_GUEST_BSP_SUSPEND) + kvmclock_disable(); +} + /* * If we don't do that, there is the possibility that the guest * will calibrate under heavy load - thus, getting a lower lpj - @@ -183,60 +237,6 @@ static struct clocksource kvm_clock = { .enable = kvm_cs_enable, }; -static void kvm_register_clock(char *txt) -{ - struct pvclock_vsyscall_time_info *src = this_cpu_hvclock(); - u64 pa; - - if (!src) - return; - - pa = slow_virt_to_phys(&src->pvti) | 0x01ULL; - wrmsrq(msr_kvm_system_time, pa); - pr_debug("kvm-clock: cpu %d, msr %llx, %s", smp_processor_id(), pa, txt); -} - -static void kvmclock_disable(void) -{ - if (msr_kvm_system_time) - native_write_msr(msr_kvm_system_time, 0); -} - -static void kvm_save_sched_clock_state(void) -{ - /* - * Stop host writes to kvmclock immediately prior to suspend/hibernate. - * If the system is hibernating, then kvmclock will likely reside at a - * different physical address when the system awakens, and host writes - * to the old address prior to reconfiguring kvmclock would clobber - * random memory. - */ - kvmclock_disable(); -} - -static void kvm_restore_sched_clock_state(void) -{ - kvm_register_clock("primary cpu clock, resume"); -} - -void kvmclock_cpu_action(enum kvm_guest_cpu_action action) -{ - /* - * Don't disable kvmclock on the BSP during suspend. If kvmclock is - * being used for sched_clock, then it needs to be kept alive until the - * last minute, and restored as quickly as possible after resume. - */ - if (action != KVM_GUEST_BSP_SUSPEND) - kvmclock_disable(); -} - -#ifdef CONFIG_SMP -static void kvm_setup_secondary_clock(void) -{ - kvm_register_clock("secondary cpu clock"); -} -#endif - static void __init kvmclock_init_mem(void) { unsigned long ncpus; -- 2.55.0.679.g6767b8d81c-goog