Skip to content

Commit 1c6d984

Browse files
kirylbonzini
authored andcommitted
x86/kvm: Do not try to disable kvmclock if it was not enabled
kvm_guest_cpu_offline() tries to disable kvmclock regardless if it is present in the VM. It leads to write to a MSR that doesn't exist on some configurations, namely in TDX guest: unchecked MSR access error: WRMSR to 0x12 (tried to write 0x0000000000000000) at rIP: 0xffffffff8110687c (kvmclock_disable+0x1c/0x30) kvmclock enabling is gated by CLOCKSOURCE and CLOCKSOURCE2 KVM paravirt features. Do not disable kvmclock if it was not enabled. Signed-off-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com> Fixes: c02027b ("x86/kvm: Disable kvmclock on all CPUs on shutdown") Reviewed-by: Sean Christopherson <seanjc@google.com> Reviewed-by: Vitaly Kuznetsov <vkuznets@redhat.com> Cc: Paolo Bonzini <pbonzini@redhat.com> Cc: Wanpeng Li <wanpengli@tencent.com> Cc: stable@vger.kernel.org Message-Id: <20231205004510.27164-6-kirill.shutemov@linux.intel.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent 7f26fea commit 1c6d984

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

arch/x86/kernel/kvmclock.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424

2525
static int kvmclock __initdata = 1;
2626
static int kvmclock_vsyscall __initdata = 1;
27-
static int msr_kvm_system_time __ro_after_init = MSR_KVM_SYSTEM_TIME;
28-
static int msr_kvm_wall_clock __ro_after_init = MSR_KVM_WALL_CLOCK;
27+
static int msr_kvm_system_time __ro_after_init;
28+
static int msr_kvm_wall_clock __ro_after_init;
2929
static u64 kvm_sched_clock_offset __ro_after_init;
3030

3131
static int __init parse_no_kvmclock(char *arg)
@@ -195,7 +195,8 @@ static void kvm_setup_secondary_clock(void)
195195

196196
void kvmclock_disable(void)
197197
{
198-
native_write_msr(msr_kvm_system_time, 0, 0);
198+
if (msr_kvm_system_time)
199+
native_write_msr(msr_kvm_system_time, 0, 0);
199200
}
200201

201202
static void __init kvmclock_init_mem(void)
@@ -294,7 +295,10 @@ void __init kvmclock_init(void)
294295
if (kvm_para_has_feature(KVM_FEATURE_CLOCKSOURCE2)) {
295296
msr_kvm_system_time = MSR_KVM_SYSTEM_TIME_NEW;
296297
msr_kvm_wall_clock = MSR_KVM_WALL_CLOCK_NEW;
297-
} else if (!kvm_para_has_feature(KVM_FEATURE_CLOCKSOURCE)) {
298+
} else if (kvm_para_has_feature(KVM_FEATURE_CLOCKSOURCE)) {
299+
msr_kvm_system_time = MSR_KVM_SYSTEM_TIME;
300+
msr_kvm_wall_clock = MSR_KVM_WALL_CLOCK;
301+
} else {
298302
return;
299303
}
300304

0 commit comments

Comments
 (0)