diff options
author | Wanpeng Li <wanpeng.li@hotmail.com> | 2017-05-11 21:12:05 -0400 |
---|---|---|
committer | Greg Kroah-Hartman <gregkh@linuxfoundation.org> | 2017-05-25 09:44:34 -0400 |
commit | c996ad7568c0ed3be135a5369078a4db30398a6d (patch) | |
tree | 2a07fd0d839b5917c87b5f2fd3f9270d72282a29 | |
parent | b64ecb25b1d5e13f02a5ec8ce3dc031e53bcfdaf (diff) |
KVM: x86: Fix potential preemption when get the current kvmclock timestamp
commit e2c2206a18993bc9f62393d49c7b2066c3845b25 upstream.
BUG: using __this_cpu_read() in preemptible [00000000] code: qemu-system-x86/2809
caller is __this_cpu_preempt_check+0x13/0x20
CPU: 2 PID: 2809 Comm: qemu-system-x86 Not tainted 4.11.0+ #13
Call Trace:
dump_stack+0x99/0xce
check_preemption_disabled+0xf5/0x100
__this_cpu_preempt_check+0x13/0x20
get_kvmclock_ns+0x6f/0x110 [kvm]
get_time_ref_counter+0x5d/0x80 [kvm]
kvm_hv_process_stimers+0x2a1/0x8a0 [kvm]
? kvm_hv_process_stimers+0x2a1/0x8a0 [kvm]
? kvm_arch_vcpu_ioctl_run+0xac9/0x1ce0 [kvm]
kvm_arch_vcpu_ioctl_run+0x5bf/0x1ce0 [kvm]
kvm_vcpu_ioctl+0x384/0x7b0 [kvm]
? kvm_vcpu_ioctl+0x384/0x7b0 [kvm]
? __fget+0xf3/0x210
do_vfs_ioctl+0xa4/0x700
? __fget+0x114/0x210
SyS_ioctl+0x79/0x90
entry_SYSCALL_64_fastpath+0x23/0xc2
RIP: 0033:0x7f9d164ed357
? __this_cpu_preempt_check+0x13/0x20
This can be reproduced by run kvm-unit-tests/hyperv_stimer.flat w/
CONFIG_PREEMPT and CONFIG_DEBUG_PREEMPT enabled.
Safe access to per-CPU data requires a couple of constraints, though: the
thread working with the data cannot be preempted and it cannot be migrated
while it manipulates per-CPU variables. If the thread is preempted, the
thread that replaces it could try to work with the same variables; migration
to another CPU could also cause confusion. However there is no preemption
disable when reads host per-CPU tsc rate to calculate the current kvmclock
timestamp.
This patch fixes it by utilizing get_cpu/put_cpu pair to guarantee both
__this_cpu_read() and rdtsc() are not preempted.
Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: Radim Krčmář <rkrcmar@redhat.com>
Signed-off-by: Wanpeng Li <wanpeng.li@hotmail.com>
Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Radim Krčmář <rkrcmar@redhat.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
-rw-r--r-- | arch/x86/kvm/x86.c | 10 |
1 files changed, 9 insertions, 1 deletions
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 2d683f8b9287..42065164d38a 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c | |||
@@ -1735,6 +1735,7 @@ static u64 __get_kvmclock_ns(struct kvm *kvm) | |||
1735 | { | 1735 | { |
1736 | struct kvm_arch *ka = &kvm->arch; | 1736 | struct kvm_arch *ka = &kvm->arch; |
1737 | struct pvclock_vcpu_time_info hv_clock; | 1737 | struct pvclock_vcpu_time_info hv_clock; |
1738 | u64 ret; | ||
1738 | 1739 | ||
1739 | spin_lock(&ka->pvclock_gtod_sync_lock); | 1740 | spin_lock(&ka->pvclock_gtod_sync_lock); |
1740 | if (!ka->use_master_clock) { | 1741 | if (!ka->use_master_clock) { |
@@ -1746,10 +1747,17 @@ static u64 __get_kvmclock_ns(struct kvm *kvm) | |||
1746 | hv_clock.system_time = ka->master_kernel_ns + ka->kvmclock_offset; | 1747 | hv_clock.system_time = ka->master_kernel_ns + ka->kvmclock_offset; |
1747 | spin_unlock(&ka->pvclock_gtod_sync_lock); | 1748 | spin_unlock(&ka->pvclock_gtod_sync_lock); |
1748 | 1749 | ||
1750 | /* both __this_cpu_read() and rdtsc() should be on the same cpu */ | ||
1751 | get_cpu(); | ||
1752 | |||
1749 | kvm_get_time_scale(NSEC_PER_SEC, __this_cpu_read(cpu_tsc_khz) * 1000LL, | 1753 | kvm_get_time_scale(NSEC_PER_SEC, __this_cpu_read(cpu_tsc_khz) * 1000LL, |
1750 | &hv_clock.tsc_shift, | 1754 | &hv_clock.tsc_shift, |
1751 | &hv_clock.tsc_to_system_mul); | 1755 | &hv_clock.tsc_to_system_mul); |
1752 | return __pvclock_read_cycles(&hv_clock, rdtsc()); | 1756 | ret = __pvclock_read_cycles(&hv_clock, rdtsc()); |
1757 | |||
1758 | put_cpu(); | ||
1759 | |||
1760 | return ret; | ||
1753 | } | 1761 | } |
1754 | 1762 | ||
1755 | u64 get_kvmclock_ns(struct kvm *kvm) | 1763 | u64 get_kvmclock_ns(struct kvm *kvm) |