hc
2023-12-11 d2ccde1c8e90d38cee87a1b0309ad2827f3fd30d
kernel/arch/x86/kernel/kvmclock.c
....@@ -1,19 +1,6 @@
1
+// SPDX-License-Identifier: GPL-2.0-or-later
12 /* KVM paravirtual clock driver. A clocksource implementation
23 Copyright (C) 2008 Glauber de Oliveira Costa, Red Hat Inc.
3
-
4
- This program is free software; you can redistribute it and/or modify
5
- it under the terms of the GNU General Public License as published by
6
- the Free Software Foundation; either version 2 of the License, or
7
- (at your option) any later version.
8
-
9
- This program is distributed in the hope that it will be useful,
10
- but WITHOUT ANY WARRANTY; without even the implied warranty of
11
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
- GNU General Public License for more details.
13
-
14
- You should have received a copy of the GNU General Public License
15
- along with this program; if not, write to the Free Software
16
- Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
174 */
185
196 #include <linux/clocksource.h>
....@@ -33,7 +20,6 @@
3320 #include <asm/hypervisor.h>
3421 #include <asm/mem_encrypt.h>
3522 #include <asm/x86_init.h>
36
-#include <asm/reboot.h>
3723 #include <asm/kvmclock.h>
3824
3925 static int kvmclock __initdata = 1;
....@@ -111,7 +97,7 @@
11197 if (!stable)
11298 clear_sched_clock_stable();
11399 kvm_sched_clock_offset = kvm_clock_read();
114
- pv_time_ops.sched_clock = kvm_sched_clock_read;
100
+ pv_ops.time.sched_clock = kvm_sched_clock_read;
115101
116102 pr_info("kvm-clock: using sched offset of %llu cycles",
117103 kvm_sched_clock_offset);
....@@ -163,12 +149,19 @@
163149 return ret;
164150 }
165151
152
+static int kvm_cs_enable(struct clocksource *cs)
153
+{
154
+ vclocks_set_used(VDSO_CLOCKMODE_PVCLOCK);
155
+ return 0;
156
+}
157
+
166158 struct clocksource kvm_clock = {
167159 .name = "kvm-clock",
168160 .read = kvm_clock_get_cycles,
169161 .rating = 400,
170162 .mask = CLOCKSOURCE_MASK(64),
171163 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
164
+ .enable = kvm_cs_enable,
172165 };
173166 EXPORT_SYMBOL_GPL(kvm_clock);
174167
....@@ -201,28 +194,9 @@
201194 }
202195 #endif
203196
204
-/*
205
- * After the clock is registered, the host will keep writing to the
206
- * registered memory location. If the guest happens to shutdown, this memory
207
- * won't be valid. In cases like kexec, in which you install a new kernel, this
208
- * means a random memory location will be kept being written. So before any
209
- * kind of shutdown from our side, we unregister the clock by writing anything
210
- * that does not have the 'enable' bit set in the msr
211
- */
212
-#ifdef CONFIG_KEXEC_CORE
213
-static void kvm_crash_shutdown(struct pt_regs *regs)
197
+void kvmclock_disable(void)
214198 {
215199 native_write_msr(msr_kvm_system_time, 0, 0);
216
- kvm_disable_steal_time();
217
- native_machine_crash_shutdown(regs);
218
-}
219
-#endif
220
-
221
-static void kvm_shutdown(void)
222
-{
223
- native_write_msr(msr_kvm_system_time, 0, 0);
224
- kvm_disable_steal_time();
225
- native_machine_shutdown();
226200 }
227201
228202 static void __init kvmclock_init_mem(void)
....@@ -266,20 +240,19 @@
266240
267241 static int __init kvm_setup_vsyscall_timeinfo(void)
268242 {
269
-#ifdef CONFIG_X86_64
270
- u8 flags;
271
-
272
- if (!per_cpu(hv_clock_per_cpu, 0) || !kvmclock_vsyscall)
273
- return 0;
274
-
275
- flags = pvclock_read_flags(&hv_clock_boot[0].pvti);
276
- if (!(flags & PVCLOCK_TSC_STABLE_BIT))
277
- return 0;
278
-
279
- kvm_clock.archdata.vclock_mode = VCLOCK_PVCLOCK;
280
-#endif
281
-
282243 kvmclock_init_mem();
244
+
245
+#ifdef CONFIG_X86_64
246
+ if (per_cpu(hv_clock_per_cpu, 0) && kvmclock_vsyscall) {
247
+ u8 flags;
248
+
249
+ flags = pvclock_read_flags(&hv_clock_boot[0].pvti);
250
+ if (!(flags & PVCLOCK_TSC_STABLE_BIT))
251
+ return 0;
252
+
253
+ kvm_clock.vdso_clock_mode = VDSO_CLOCKMODE_PVCLOCK;
254
+ }
255
+#endif
283256
284257 return 0;
285258 }
....@@ -350,11 +323,21 @@
350323 #endif
351324 x86_platform.save_sched_clock_state = kvm_save_sched_clock_state;
352325 x86_platform.restore_sched_clock_state = kvm_restore_sched_clock_state;
353
- machine_ops.shutdown = kvm_shutdown;
354
-#ifdef CONFIG_KEXEC_CORE
355
- machine_ops.crash_shutdown = kvm_crash_shutdown;
356
-#endif
357326 kvm_get_preset_lpj();
327
+
328
+ /*
329
+ * X86_FEATURE_NONSTOP_TSC is TSC runs at constant rate
330
+ * with P/T states and does not stop in deep C-states.
331
+ *
332
+ * Invariant TSC exposed by host means kvmclock is not necessary:
333
+ * can use TSC as clocksource.
334
+ *
335
+ */
336
+ if (boot_cpu_has(X86_FEATURE_CONSTANT_TSC) &&
337
+ boot_cpu_has(X86_FEATURE_NONSTOP_TSC) &&
338
+ !check_tsc_unstable())
339
+ kvm_clock.rating = 299;
340
+
358341 clocksource_register_hz(&kvm_clock, NSEC_PER_SEC);
359342 pv_info.name = "KVM";
360343 }