Commit e5c239cf authored by Marcelo Tosatti's avatar Marcelo Tosatti Committed by Avi Kivity

KVM: Fix kvm_vcpu_block() task state race

There's still a race in kvm_vcpu_block(), if a wake_up_interruptible()
call happens before the task state is set to TASK_INTERRUPTIBLE:

CPU0                            CPU1

kvm_vcpu_block

add_wait_queue

kvm_cpu_has_interrupt = 0
                                set interrupt
                                if (waitqueue_active())
                                        wake_up_interruptible()

kvm_cpu_has_pending_timer
kvm_arch_vcpu_runnable
signal_pending

set_current_state(TASK_INTERRUPTIBLE)
schedule()

Can be fixed by using prepare_to_wait() which sets the task state before
testing for the wait condition.
Signed-off-by: default avatarMarcelo Tosatti <mtosatti@redhat.com>
Signed-off-by: default avatarAvi Kivity <avi@qumranet.com>
parent bd25ed03
...@@ -758,25 +758,26 @@ void mark_page_dirty(struct kvm *kvm, gfn_t gfn) ...@@ -758,25 +758,26 @@ void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
*/ */
void kvm_vcpu_block(struct kvm_vcpu *vcpu) void kvm_vcpu_block(struct kvm_vcpu *vcpu)
{ {
DECLARE_WAITQUEUE(wait, current); DEFINE_WAIT(wait);
add_wait_queue(&vcpu->wq, &wait); for (;;) {
prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
/*
* We will block until either an interrupt or a signal wakes us up if (kvm_cpu_has_interrupt(vcpu))
*/ break;
while (!kvm_cpu_has_interrupt(vcpu) if (kvm_cpu_has_pending_timer(vcpu))
&& !kvm_cpu_has_pending_timer(vcpu) break;
&& !signal_pending(current) if (kvm_arch_vcpu_runnable(vcpu))
&& !kvm_arch_vcpu_runnable(vcpu)) { break;
set_current_state(TASK_INTERRUPTIBLE); if (signal_pending(current))
break;
vcpu_put(vcpu); vcpu_put(vcpu);
schedule(); schedule();
vcpu_load(vcpu); vcpu_load(vcpu);
} }
__set_current_state(TASK_RUNNING); finish_wait(&vcpu->wq, &wait);
remove_wait_queue(&vcpu->wq, &wait);
} }
void kvm_resched(struct kvm_vcpu *vcpu) void kvm_resched(struct kvm_vcpu *vcpu)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment