All of lore.kernel.org
 help / color / mirror / Atom feed
* KVM: protect concurrent make_all_cpus_request
@ 2009-06-17 13:53 Marcelo Tosatti
  2009-06-17 16:52 ` Andrea Arcangeli
  2009-06-18  8:45 ` Avi Kivity
  0 siblings, 2 replies; 3+ messages in thread
From: Marcelo Tosatti @ 2009-06-17 13:53 UTC (permalink / raw)
  To: kvm, Avi Kivity; +Cc: Andrea Arcangeli



make_all_cpus_request contains a race condition which can 
trigger false request completed status, as follows:

CPU0                                              CPU1

if (test_and_set_bit(req,&vcpu->requests))
   ....                                        	   if (test_and_set_bit(req,&vcpu->requests))
   ..                                                  return
proceed to smp_call_function_many(wait=1)

Use a spinlock to serialize concurrent CPUs.

Signed-off-by: Andrea Arcangeli <aarcange@redhat.com>
Signed-off-by: Marcelo Tosatti <mtosatti@redhat.com>

diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h
index 1b48092..2451f48 100644
--- a/include/linux/kvm_host.h
+++ b/include/linux/kvm_host.h
@@ -124,6 +124,7 @@ struct kvm_kernel_irq_routing_entry {
 
 struct kvm {
 	spinlock_t mmu_lock;
+	spinlock_t requests_lock;
 	struct rw_semaphore slots_lock;
 	struct mm_struct *mm; /* userspace tied to this vm */
 	int nmemslots;
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index 8939ffa..6847bb7 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -739,6 +739,7 @@ static bool make_all_cpus_request(struct kvm *kvm, unsigned int req)
 		cpumask_clear(cpus);
 
 	me = get_cpu();
+	spin_lock(&kvm->requests_lock);
 	kvm_for_each_vcpu(i, vcpu, kvm) {
 		if (test_and_set_bit(req, &vcpu->requests))
 			continue;
@@ -752,6 +753,7 @@ static bool make_all_cpus_request(struct kvm *kvm, unsigned int req)
 		smp_call_function_many(cpus, ack_flush, NULL, 1);
 	else
 		called = false;
+	spin_unlock(&kvm->requests_lock);
 	put_cpu();
 	free_cpumask_var(cpus);
 	return called;
@@ -972,6 +974,7 @@ static struct kvm *kvm_create_vm(void)
 	kvm->mm = current->mm;
 	atomic_inc(&kvm->mm->mm_count);
 	spin_lock_init(&kvm->mmu_lock);
+	spin_lock_init(&kvm->requests_lock);
 	kvm_io_bus_init(&kvm->pio_bus);
 	kvm_irqfd_init(kvm);
 	mutex_init(&kvm->lock);

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: KVM: protect concurrent make_all_cpus_request
  2009-06-17 13:53 KVM: protect concurrent make_all_cpus_request Marcelo Tosatti
@ 2009-06-17 16:52 ` Andrea Arcangeli
  2009-06-18  8:45 ` Avi Kivity
  1 sibling, 0 replies; 3+ messages in thread
From: Andrea Arcangeli @ 2009-06-17 16:52 UTC (permalink / raw)
  To: Marcelo Tosatti; +Cc: kvm, Avi Kivity

On Wed, Jun 17, 2009 at 10:53:47AM -0300, Marcelo Tosatti wrote:
> 
> 
> make_all_cpus_request contains a race condition which can 
> trigger false request completed status, as follows:
> 
> CPU0                                              CPU1
> 
> if (test_and_set_bit(req,&vcpu->requests))
>    ....                                        	   if (test_and_set_bit(req,&vcpu->requests))
>    ..                                                  return
> proceed to smp_call_function_many(wait=1)
> 
> Use a spinlock to serialize concurrent CPUs.

Agreed, this is the safest...

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: KVM: protect concurrent make_all_cpus_request
  2009-06-17 13:53 KVM: protect concurrent make_all_cpus_request Marcelo Tosatti
  2009-06-17 16:52 ` Andrea Arcangeli
@ 2009-06-18  8:45 ` Avi Kivity
  1 sibling, 0 replies; 3+ messages in thread
From: Avi Kivity @ 2009-06-18  8:45 UTC (permalink / raw)
  To: Marcelo Tosatti; +Cc: kvm, Andrea Arcangeli

On 06/17/2009 04:53 PM, Marcelo Tosatti wrote:
> make_all_cpus_request contains a race condition which can
> trigger false request completed status, as follows:
>
> CPU0                                              CPU1
>
> if (test_and_set_bit(req,&vcpu->requests))
>     ....                                        	   if (test_and_set_bit(req,&vcpu->requests))
>     ..                                                  return
> proceed to smp_call_function_many(wait=1)
>
> Use a spinlock to serialize concurrent CPUs.
>
>    

Applied, thanks.

-- 
error compiling committee.c: too many arguments to function


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2009-06-18  8:45 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-06-17 13:53 KVM: protect concurrent make_all_cpus_request Marcelo Tosatti
2009-06-17 16:52 ` Andrea Arcangeli
2009-06-18  8:45 ` Avi Kivity

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.