From patchwork Thu Feb 27 02:18:34 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13993563 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (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 EBEA32135AB for ; Thu, 27 Feb 2025 02:19:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740622776; cv=none; b=Dh8GKVKorjnjxP2SObwso2sytyFVkCSZ4hj8KZZrQxPrkzIQYpEtguGPTwc82Rjkj3kUv/nzIoLhYeizFYXf2576g2mX7Mu3JuiqT50rVwnQMC/FK31B2VJOs03sYt/i8KT4Jni09nIgR2MDS5psx7B9XLPrc5gOIVsfn2GNXYU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740622776; c=relaxed/simple; bh=MgbzTWQ5O3po+uvgZbmYvyZGhklKbSd0kRHueynvX0o=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=QBzKf67yifHeQYhjCTpx6szeGPoTNI75QiEkYQPoIPp0dj+/zA3ZmtQXGTUyWWetwbBJ6XzFJaph5oO696IwLVYy87WXGORSERZvvgBqhO1AKzrdN6Nc/77grA99RoaLczvjXEPcehDucKBuq55rkGYk1hqThPBXvFiccZIw4EI= 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=esGgKbEa; arc=none smtp.client-ip=209.85.216.74 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="esGgKbEa" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-2f2a9f056a8so981929a91.2 for ; Wed, 26 Feb 2025 18:19:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1740622774; x=1741227574; 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=+aPHxZSdvUlWxMuV2uSxypL8fcuaMUyvD+y52Dh+Bv8=; b=esGgKbEaExoCrTB4Y86lgDk+ECdQPl7Ki75ynOfIZdcx2Ni9dwYsEbKiljf5bhFpl8 yxy80BTNOVVSpjHjGxMtsviev/P+8XyqfMVX/8ajoH034qfASBFndqjFShDgrgsctm2q ULqHEFDLmK5own51FQQocur8XQ3q9XYQ7s0XytrLc+W28jICaVHjTUp11fGAVY+Trafh ar7hquqniruLHzG9HxKOqjS9cbx0OGSw5ne2HpQwEhjjwkBZ/WIe85cFhB8+eRNTs21Q WuhasTlOgzlep/Uy4bTrxpXYU5bfbnzMWjdDzZNKkKrSIhlA5SYUiDTaY1AogzQT2IX2 tjvw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740622774; x=1741227574; 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=+aPHxZSdvUlWxMuV2uSxypL8fcuaMUyvD+y52Dh+Bv8=; b=UHh3o8CTev/mDX7JukOCioIE22GsrQ+vq0fAOxKTLLbfpW1FS8qRF76qxIKCPVMf2g Ee3MBgZIWqW1uHXBIp2PdnO8o2xUTNZYjipOuvHStF+hYUHSYMR4vZ2/+d3cMSGxEI06 uQgo5iTig+ziBS5zKv7TZ+9tthxQD0ElwhipoMOKfK5BAfU5dm+4Tj6Uqh4DueKG7PWU l8Z7JgAqw/D8A7W2YCfCg4m21682oOG6MzYDKnzD6V3N7/Nt1VRRnHg3foh/PgnS/GEd Rjgv1fit/6srzkjhWyhWLIAVcjZxjFLvYRgsAtUmtO0koANq0p36/2GMZmy1p4STxvcU 04Ow== X-Forwarded-Encrypted: i=1; AJvYcCV8B0rF6FDqycz8PzU0WVP8c3elnGJBjHisakCnvIyaM1FPpPjL3uFwAJ058X35SxQTg9E=@vger.kernel.org X-Gm-Message-State: AOJu0Yx2nIdGVZRTVPAh3fpliKOkTs/8fR6TttmKad2wDeBZew0t+7+1 RTaCACG0IAc8zz+4PDs02Rtjs0uHrAmczQqW0bp9jyrb+FSi7DSia2Bf1YswRgMGrjdHtvq+jKM qgA== X-Google-Smtp-Source: AGHT+IGtJhSaBP2AlM0Njvk3g8X/xMhIC1fqVJrfdIV8pKqmQJT6ad+58HkgFke1VLmpWg41h8WiiOdBO04= X-Received: from pjbta3.prod.google.com ([2002:a17:90b:4ec3:b0:2ea:448a:8cd1]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:5483:b0:2ee:f076:20f1 with SMTP id 98e67ed59e1d1-2fe7e218ab9mr10711632a91.0.1740622774461; Wed, 26 Feb 2025 18:19:34 -0800 (PST) Reply-To: Sean Christopherson Date: Wed, 26 Feb 2025 18:18:34 -0800 In-Reply-To: <20250227021855.3257188-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250227021855.3257188-1-seanjc@google.com> X-Mailer: git-send-email 2.48.1.711.g2feabab25a-goog Message-ID: <20250227021855.3257188-19-seanjc@google.com> Subject: [PATCH v2 18/38] x86/paravirt: Pass sched_clock save/restore helpers during registration From: Sean Christopherson To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "Kirill A. Shutemov" , Paolo Bonzini , Sean Christopherson , Juergen Gross , "K. Y. Srinivasan" , Haiyang Zhang , Wei Liu , Dexuan Cui , Ajay Kaher , Jan Kiszka , Andy Lutomirski , Peter Zijlstra , Daniel Lezcano , John Stultz Cc: linux-kernel@vger.kernel.org, linux-coco@lists.linux.dev, kvm@vger.kernel.org, virtualization@lists.linux.dev, linux-hyperv@vger.kernel.org, xen-devel@lists.xenproject.org, Tom Lendacky , Nikunj A Dadhania 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. Signed-off-by: Sean Christopherson --- arch/x86/include/asm/paravirt.h | 8 +++++--- arch/x86/kernel/cpu/vmware.c | 7 ++----- arch/x86/kernel/kvmclock.c | 5 ++--- arch/x86/kernel/paravirt.c | 5 ++++- arch/x86/xen/time.c | 5 ++--- drivers/clocksource/hyperv_timer.c | 6 ++---- 6 files changed, 17 insertions(+), 19 deletions(-) diff --git a/arch/x86/include/asm/paravirt.h b/arch/x86/include/asm/paravirt.h index cfceabd5f7e1..dc26a3c26527 100644 --- a/arch/x86/include/asm/paravirt.h +++ b/arch/x86/include/asm/paravirt.h @@ -28,11 +28,13 @@ u64 dummy_sched_clock(void); DECLARE_STATIC_CALL(pv_steal_clock, dummy_steal_clock); DECLARE_STATIC_CALL(pv_sched_clock, dummy_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)); -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); } static __always_inline u64 paravirt_sched_clock(void) diff --git a/arch/x86/kernel/cpu/vmware.c b/arch/x86/kernel/cpu/vmware.c index d6eadb5b37fd..399cf3286a60 100644 --- a/arch/x86/kernel/cpu/vmware.c +++ b/arch/x86/kernel/cpu/vmware.c @@ -344,11 +344,8 @@ 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 = NULL; - x86_platform.restore_sched_clock_state = NULL; - } + if (vmw_sched_clock) + paravirt_set_sched_clock(vmware_sched_clock, NULL, NULL); if (vmware_is_stealclock_available()) { has_steal_clock = true; diff --git a/arch/x86/kernel/kvmclock.c b/arch/x86/kernel/kvmclock.c index c78db52ae399..1ad3878cc1d9 100644 --- a/arch/x86/kernel/kvmclock.c +++ b/arch/x86/kernel/kvmclock.c @@ -136,7 +136,8 @@ 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); @@ -343,8 +344,6 @@ void __init kvmclock_init(void) #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/paravirt.c b/arch/x86/kernel/paravirt.c index 55c819673a9d..9673cd3a3f0a 100644 --- a/arch/x86/kernel/paravirt.c +++ b/arch/x86/kernel/paravirt.c @@ -86,12 +86,15 @@ static u64 native_steal_clock(int cpu) DEFINE_STATIC_CALL(pv_steal_clock, native_steal_clock); DEFINE_STATIC_CALL(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; } /* These are in entry.S */ diff --git a/arch/x86/xen/time.c b/arch/x86/xen/time.c index 51eba986cd18..3179f850352d 100644 --- a/arch/x86/xen/time.c +++ b/arch/x86/xen/time.c @@ -564,13 +564,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 = NULL; - x86_platform.restore_sched_clock_state = NULL; + paravirt_set_sched_clock(xen_sched_clock, NULL, NULL); tsc_register_calibration_routines(xen_tsc_khz, NULL); x86_platform.get_wallclock = xen_get_wallclock; diff --git a/drivers/clocksource/hyperv_timer.c b/drivers/clocksource/hyperv_timer.c index 4a21874e91b9..1c4ed9995cb2 100644 --- a/drivers/clocksource/hyperv_timer.c +++ b/drivers/clocksource/hyperv_timer.c @@ -550,10 +550,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) {}