From patchwork Wed Mar 16 10:10:18 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fam Zheng X-Patchwork-Id: 8597251 Return-Path: X-Original-To: patchwork-qemu-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 134E9C0553 for ; Wed, 16 Mar 2016 10:13:33 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 590EA20225 for ; Wed, 16 Mar 2016 10:13:32 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 85A2E201CE for ; Wed, 16 Mar 2016 10:13:31 +0000 (UTC) Received: from localhost ([::1]:54326 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ag8SI-0003MH-T5 for patchwork-qemu-devel@patchwork.kernel.org; Wed, 16 Mar 2016 06:13:30 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:47137) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ag8Pc-0007Xb-VN for qemu-devel@nongnu.org; Wed, 16 Mar 2016 06:10:50 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ag8Pb-0005hl-Nm for qemu-devel@nongnu.org; Wed, 16 Mar 2016 06:10:44 -0400 Received: from mx1.redhat.com ([209.132.183.28]:37921) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ag8PU-0005fo-Qt; Wed, 16 Mar 2016 06:10:36 -0400 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) by mx1.redhat.com (Postfix) with ESMTPS id 81350C003592; Wed, 16 Mar 2016 10:10:36 +0000 (UTC) Received: from fam-t430.redhat.com (vpn1-6-163.pek2.redhat.com [10.72.6.163]) by int-mx11.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u2GAAIo7010795; Wed, 16 Mar 2016 06:10:33 -0400 From: Fam Zheng To: qemu-devel@nongnu.org Date: Wed, 16 Mar 2016 18:10:18 +0800 Message-Id: <1458123018-18651-5-git-send-email-famz@redhat.com> In-Reply-To: <1458123018-18651-1-git-send-email-famz@redhat.com> References: <1458123018-18651-1-git-send-email-famz@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.24 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: Kevin Wolf , qemu-block@nongnu.org, "Michael S. Tsirkin" , tubo@linux.vnet.ibm.com, Stefan Hajnoczi , cornelia.huck@de.ibm.com, pbonzini@redhat.com, borntraeger@de.ibm.com Subject: [Qemu-devel] [PATCH 4/4] virtio-blk: Clean up start/stop with mutex and BH X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP This is to make the dataplane start logic simpler to understand. Start/stop take the mutex so we don't need the starting flag. The bottom half is scheduled in the iothread to actually hook up request handlers with vq. Suggested-by: Paolo Bonzini Signed-off-by: Fam Zheng --- hw/block/dataplane/virtio-blk.c | 58 +++++++++++++++++++++++++++++++---------- 1 file changed, 44 insertions(+), 14 deletions(-) diff --git a/hw/block/dataplane/virtio-blk.c b/hw/block/dataplane/virtio-blk.c index 36f3d2b..9e5c543 100644 --- a/hw/block/dataplane/virtio-blk.c +++ b/hw/block/dataplane/virtio-blk.c @@ -26,7 +26,6 @@ #include "qom/object_interfaces.h" struct VirtIOBlockDataPlane { - bool starting; bool stopping; bool disabled; @@ -49,6 +48,8 @@ struct VirtIOBlockDataPlane { /* Operation blocker on BDS */ Error *blocker; + + QemuMutex start_stop_lock; }; /* Raise an interrupt to signal guest, if necessary */ @@ -150,6 +151,7 @@ void virtio_blk_data_plane_create(VirtIODevice *vdev, VirtIOBlkConf *conf, s = g_new0(VirtIOBlockDataPlane, 1); s->vdev = vdev; s->conf = conf; + qemu_mutex_init(&s->start_stop_lock); if (conf->iothread) { s->iothread = conf->iothread; @@ -184,19 +186,47 @@ void virtio_blk_data_plane_destroy(VirtIOBlockDataPlane *s) g_free(s); } +typedef struct { + VirtIOBlock *vblk; + QEMUBH *bh; +} VirtIOBlockStartData; + +static void virtio_blk_data_plane_start_bh_cb(void *opaque) +{ + VirtIOBlockStartData *data = opaque; + VirtIOBlockDataPlane *s = data->vblk->dataplane; + + qemu_mutex_lock(&s->start_stop_lock); + if (!data->vblk->dataplane_started) { + goto out; + } + /* Kick right away to begin processing requests already in vring */ + event_notifier_set(virtio_queue_get_host_notifier(s->vq)); + + /* Get this show started by hooking up our callbacks */ + virtio_queue_aio_set_host_notifier_handler(s->vq, s->ctx, true, true); + +out: + qemu_bh_delete(data->bh); + g_free(data); + qemu_mutex_unlock(&s->start_stop_lock); +} + /* Context: QEMU global mutex held */ void virtio_blk_data_plane_start(VirtIOBlockDataPlane *s) { BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(s->vdev))); VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); VirtIOBlock *vblk = VIRTIO_BLK(s->vdev); + VirtIOBlockStartData *data; int r; - if (vblk->dataplane_started || s->starting) { + qemu_mutex_lock(&s->start_stop_lock); + if (vblk->dataplane_started) { + qemu_mutex_unlock(&s->start_stop_lock); return; } - s->starting = true; s->vq = virtio_get_queue(s->vdev, 0); /* Set up guest notifier (irq) */ @@ -215,27 +245,24 @@ void virtio_blk_data_plane_start(VirtIOBlockDataPlane *s) goto fail_host_notifier; } - s->starting = false; vblk->dataplane_started = true; trace_virtio_blk_data_plane_start(s); blk_set_aio_context(s->conf->conf.blk, s->ctx); - /* Kick right away to begin processing requests already in vring */ - event_notifier_set(virtio_queue_get_host_notifier(s->vq)); - - /* Get this show started by hooking up our callbacks */ - aio_context_acquire(s->ctx); - virtio_queue_aio_set_host_notifier_handler(s->vq, s->ctx, true, true); - aio_context_release(s->ctx); + data = g_new(VirtIOBlockStartData, 1); + data->vblk = vblk; + data->bh = aio_bh_new(s->ctx, virtio_blk_data_plane_start_bh_cb, data); + qemu_bh_schedule(data->bh); + qemu_mutex_unlock(&s->start_stop_lock); return; fail_host_notifier: k->set_guest_notifiers(qbus->parent, 1, false); fail_guest_notifiers: s->disabled = true; - s->starting = false; vblk->dataplane_started = true; + qemu_mutex_unlock(&s->start_stop_lock); } /* Context: QEMU global mutex held */ @@ -245,15 +272,16 @@ void virtio_blk_data_plane_stop(VirtIOBlockDataPlane *s) VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); VirtIOBlock *vblk = VIRTIO_BLK(s->vdev); + qemu_mutex_lock(&s->start_stop_lock); if (!vblk->dataplane_started || s->stopping) { - return; + goto out; } /* Better luck next time. */ if (s->disabled) { s->disabled = false; vblk->dataplane_started = false; - return; + goto out; } s->stopping = true; trace_virtio_blk_data_plane_stop(s); @@ -275,4 +303,6 @@ void virtio_blk_data_plane_stop(VirtIOBlockDataPlane *s) vblk->dataplane_started = false; s->stopping = false; +out: + qemu_mutex_unlock(&s->start_stop_lock); }