Received: from eggs.gnu.org ([2001:4830:134:3::10]:42093) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VCAHG-0000kv-BA for qemu-devel@nongnu.org; Wed, 21 Aug 2013 11:25:02 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1VCAH5-0004aV-Vq for qemu-devel@nongnu.org; Wed, 21 Aug 2013 11:24:54 -0400 Received: from mail-wg0-x235.google.com ([2a00:1450:400c:c00::235]:43684) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VCAH5-0004aM-IG for qemu-devel@nongnu.org; Wed, 21 Aug 2013 11:24:43 -0400 Received: by mail-wg0-f53.google.com with SMTP id c11so522338wgh.32 for ; Wed, 21 Aug 2013 08:24:42 -0700 (PDT) Date: Wed, 21 Aug 2013 17:24:40 +0200 From: Stefan Hajnoczi Message-ID: <20130821152440.GB18303@stefanha-thinkpad.redhat.com> References: <1377050567-19122-1-git-send-email-asias@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1377050567-19122-1-git-send-email-asias@redhat.com> 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 On Wed, Aug 21, 2013 at 10:02:47AM +0800, Asias He wrote: > 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. I think we need to look more closely at the error code path: acb->ret = ret; retval = qemu_write_full(s->fds[GLUSTER_FD_WRITE], &acb, sizeof(acb)); if (retval != sizeof(acb)) { /* * Gluster AIO callback thread failed to notify the waiting * QEMU thread about IO completion. * * Complete this IO request and make the disk inaccessible for * subsequent reads and writes. */ error_report("Gluster failed to notify QEMU about IO completion"); qemu_mutex_lock_iothread(); /* We are in gluster thread context */ acb->common.cb(acb->common.opaque, -EIO); qemu_aio_release(acb); close(s->fds[GLUSTER_FD_READ]); close(s->fds[GLUSTER_FD_WRITE]); Is it safe to close the fds? There is a race here: 1. Another thread opens a new file descriptor and gets GLUSTER_FD_READ or GLUSTER_FD_WRITE's old fd value. 2. Another gluster thread invokes the callback and does qemu_write_full(s->fds[GLUSTER_FD_WRITE], ...). Since the mutex doesn't protect s->fds[] this is possible. Maybe a simpler solution for request completion is: 1. Linked list of completed acbs. 2. Mutex to protect the linked list. 3. EventNotifier to signal iothread. Then this function becomes: static void gluster_finish_aiocb(struct glfs_fd *fd, ssize_t ret, void *arg) { GlusterAIOCB *acb = arg; BlockDriverState *bs = acb->common.bs; BDRVGlusterState *s = bs->opaque; int retval; acb->ret = ret; qemu_mutex_lock(&s->finish_list_lock); QSIMPLEQ_INSERT_TAIL(&s->finish_list, acb, list); qemu_mutex_unlock(&s->finish_list_lock); event_notifier_set(&s->finish_list_notifier); } Stefan