From patchwork Wed Jun 17 13:53:47 2009 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marcelo Tosatti X-Patchwork-Id: 30882 Received: from vger.kernel.org (vger.kernel.org [209.132.176.167]) by demeter.kernel.org (8.14.2/8.14.2) with ESMTP id n5HDtTbB006473 for ; Wed, 17 Jun 2009 13:55:29 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1765845AbZFQNyQ (ORCPT ); Wed, 17 Jun 2009 09:54:16 -0400 Received: (majordomo@vger.kernel.org) by vger.kernel.org id S1762689AbZFQNyP (ORCPT ); Wed, 17 Jun 2009 09:54:15 -0400 Received: from mx2.redhat.com ([66.187.237.31]:50054 "EHLO mx2.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755505AbZFQNyP (ORCPT ); Wed, 17 Jun 2009 09:54:15 -0400 Received: from int-mx2.corp.redhat.com (int-mx2.corp.redhat.com [172.16.27.26]) by mx2.redhat.com (8.13.8/8.13.8) with ESMTP id n5HDsIdZ003059 for ; Wed, 17 Jun 2009 09:54:18 -0400 Received: from ns3.rdu.redhat.com (ns3.rdu.redhat.com [10.11.255.199]) by int-mx2.corp.redhat.com (8.13.1/8.13.1) with ESMTP id n5HDsHMG017450; Wed, 17 Jun 2009 09:54:17 -0400 Received: from amt.cnet (vpn-51-11.sfbay.redhat.com [10.14.51.11]) by ns3.rdu.redhat.com (8.13.8/8.13.8) with ESMTP id n5HDsFBg018535; Wed, 17 Jun 2009 09:54:16 -0400 Received: from amt.cnet (amt.cnet [127.0.0.1]) by amt.cnet (Postfix) with ESMTP id DE1E8680138; Wed, 17 Jun 2009 10:53:48 -0300 (BRT) Received: (from marcelo@localhost) by amt.cnet (8.14.3/8.14.3/Submit) id n5HDrlsP014944; Wed, 17 Jun 2009 10:53:47 -0300 Date: Wed, 17 Jun 2009 10:53:47 -0300 From: Marcelo Tosatti To: kvm@vger.kernel.org, Avi Kivity Cc: Andrea Arcangeli Subject: KVM: protect concurrent make_all_cpus_request Message-ID: <20090617135347.GC14583@amt.cnet> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.19 (2009-01-05) X-Scanned-By: MIMEDefang 2.58 on 172.16.27.26 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org 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 Signed-off-by: Marcelo Tosatti --- To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html 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);