From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:58594) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VC3b5-0007dN-Aq for qemu-devel@nongnu.org; Wed, 21 Aug 2013 04:17:03 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1VC3aw-0006Hq-FP for qemu-devel@nongnu.org; Wed, 21 Aug 2013 04:16:55 -0400 Received: from mail-ea0-x22c.google.com ([2a00:1450:4013:c01::22c]:57660) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VC3aw-0006Hh-83 for qemu-devel@nongnu.org; Wed, 21 Aug 2013 04:16:46 -0400 Received: by mail-ea0-f172.google.com with SMTP id r16so61902ead.31 for ; Wed, 21 Aug 2013 01:16:45 -0700 (PDT) Sender: Paolo Bonzini Message-ID: <52147742.2090401@redhat.com> Date: Wed, 21 Aug 2013 10:16:02 +0200 From: Paolo Bonzini MIME-Version: 1.0 References: <1377050567-19122-1-git-send-email-asias@redhat.com> In-Reply-To: <1377050567-19122-1-git-send-email-asias@redhat.com> Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Subject: Re: [Qemu-devel] [PATCH] block: Fix race in gluster_finish_aiocb List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Asias He Cc: Kevin Wolf , Vijay Bellur , qemu-devel@nongnu.org, Stefan Hajnoczi , Bharata B Rao , MORITA Kazutaka Il 21/08/2013 04:02, Asias He ha scritto: > In block/gluster.c, we have > > gluster_finish_aiocb > { > if (retval != sizeof(acb)) { > qemu_mutex_lock_iothread(); /* We are in gluster thread context */ > ... > qemu_mutex_unlock_iothread(); > } > } > > qemu tools, e.g. qemu-img, might race here because > qemu_mutex_{lock,unlock}_iothread are a nop operation and > gluster_finish_aiocb is in the gluster thread context. > > To fix, we introduce our own mutex for qemu tools. > > Signed-off-by: Asias He > --- > stubs/iothread-lock.c | 11 +++++++++++ > 1 file changed, 11 insertions(+) > > diff --git a/stubs/iothread-lock.c b/stubs/iothread-lock.c > index 5d8aca1..d5c6dec 100644 > --- a/stubs/iothread-lock.c > +++ b/stubs/iothread-lock.c > @@ -1,10 +1,21 @@ > #include "qemu-common.h" > #include "qemu/main-loop.h" > > +static QemuMutex qemu_tools_mutex; > +static pthread_once_t qemu_tools_once = PTHREAD_ONCE_INIT; Doesn't work on Windows, but you can just add __attribute__((__constructor__)) to qemu_tools_mutex_init. Paolo > +static void qemu_tools_mutex_init(void) > +{ > + qemu_mutex_init(&qemu_tools_mutex); > +} > + > void qemu_mutex_lock_iothread(void) > { > + pthread_once(&qemu_tools_once, qemu_tools_mutex_init); > + qemu_mutex_lock(&qemu_tools_mutex); > } > > void qemu_mutex_unlock_iothread(void) > { > + qemu_mutex_unlock(&qemu_tools_mutex); > } >