aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndrew Jones <drjones@redhat.com>2017-04-26 16:32:24 -0400
committerPaolo Bonzini <pbonzini@redhat.com>2017-04-27 08:16:17 -0400
commitcde9af6e79046e12cd08d161139b1d5e57e9fbac (patch)
tree2192140fe822f0805d3b766f3e277798e12445b0
parent6c6e8360b34d59d2f687a1649e61173742dbc891 (diff)
KVM: add explicit barrier to kvm_vcpu_kick
kvm_vcpu_kick() must issue a general memory barrier prior to reading vcpu->mode in order to ensure correctness of the mutual-exclusion memory barrier pattern used with vcpu->requests. While the cmpxchg called from kvm_vcpu_kick(): kvm_vcpu_kick kvm_arch_vcpu_should_kick kvm_vcpu_exiting_guest_mode cmpxchg implies general memory barriers before and after the operation, that implication is only valid when cmpxchg succeeds. We need an explicit barrier for when it fails, otherwise a VCPU thread on its entry path that reads zero for vcpu->requests does not exclude the possibility the requesting thread sees !IN_GUEST_MODE when it reads vcpu->mode. kvm_make_all_cpus_request already had a barrier, so we remove it, as now it would be redundant. Signed-off-by: Andrew Jones <drjones@redhat.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
-rw-r--r--arch/x86/kvm/x86.c2
-rw-r--r--include/linux/kvm_host.h6
-rw-r--r--virt/kvm/kvm_main.c3
3 files changed, 7 insertions, 4 deletions
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 0936c3e2e51c..69fcee26f4da 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -6853,7 +6853,7 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
6853 6853
6854 /* 6854 /*
6855 * 1) We should set ->mode before checking ->requests. Please see 6855 * 1) We should set ->mode before checking ->requests. Please see
6856 * the comment in kvm_make_all_cpus_request. 6856 * the comment in kvm_vcpu_exiting_guest_mode().
6857 * 6857 *
6858 * 2) For APICv, we should set ->mode before checking PIR.ON. This 6858 * 2) For APICv, we should set ->mode before checking PIR.ON. This
6859 * pairs with the memory barrier implicit in pi_test_and_set_on 6859 * pairs with the memory barrier implicit in pi_test_and_set_on
diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h
index a805ddcb7eb0..84c5396564f7 100644
--- a/include/linux/kvm_host.h
+++ b/include/linux/kvm_host.h
@@ -270,6 +270,12 @@ struct kvm_vcpu {
270 270
271static inline int kvm_vcpu_exiting_guest_mode(struct kvm_vcpu *vcpu) 271static inline int kvm_vcpu_exiting_guest_mode(struct kvm_vcpu *vcpu)
272{ 272{
273 /*
274 * The memory barrier ensures a previous write to vcpu->requests cannot
275 * be reordered with the read of vcpu->mode. It pairs with the general
276 * memory barrier following the write of vcpu->mode in VCPU RUN.
277 */
278 smp_mb__before_atomic();
273 return cmpxchg(&vcpu->mode, IN_GUEST_MODE, EXITING_GUEST_MODE); 279 return cmpxchg(&vcpu->mode, IN_GUEST_MODE, EXITING_GUEST_MODE);
274} 280}
275 281
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index 3772f7dcc72d..1efb07643035 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -183,9 +183,6 @@ bool kvm_make_all_cpus_request(struct kvm *kvm, unsigned int req)
183 kvm_make_request(req, vcpu); 183 kvm_make_request(req, vcpu);
184 cpu = vcpu->cpu; 184 cpu = vcpu->cpu;
185 185
186 /* Set ->requests bit before we read ->mode. */
187 smp_mb__after_atomic();
188
189 if (!(req & KVM_REQUEST_NO_WAKEUP)) 186 if (!(req & KVM_REQUEST_NO_WAKEUP))
190 kvm_vcpu_wake_up(vcpu); 187 kvm_vcpu_wake_up(vcpu);
191 188