From patchwork Wed Jul 18 15:07:36 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Hajnoczi X-Patchwork-Id: 171722 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id F3E2B2C00B3 for ; Thu, 19 Jul 2012 01:35:49 +1000 (EST) Received: from localhost ([::1]:36386 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SrVsc-0007GS-N8 for incoming@patchwork.ozlabs.org; Wed, 18 Jul 2012 11:09:34 -0400 Received: from eggs.gnu.org ([208.118.235.92]:52989) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SrVrq-0005le-Bf for qemu-devel@nongnu.org; Wed, 18 Jul 2012 11:08:47 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1SrVrf-0000iz-UF for qemu-devel@nongnu.org; Wed, 18 Jul 2012 11:08:46 -0400 Received: from e06smtp11.uk.ibm.com ([195.75.94.107]:34133) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SrVrf-0000ic-KY for qemu-devel@nongnu.org; Wed, 18 Jul 2012 11:08:35 -0400 Received: from /spool/local by e06smtp11.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Wed, 18 Jul 2012 16:08:35 +0100 Received: from d06nrmr1307.portsmouth.uk.ibm.com (9.149.38.129) by e06smtp11.uk.ibm.com (192.168.101.141) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Wed, 18 Jul 2012 16:08:32 +0100 Received: from d06av11.portsmouth.uk.ibm.com (d06av11.portsmouth.uk.ibm.com [9.149.37.252]) by d06nrmr1307.portsmouth.uk.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id q6IF8VRq2572308 for ; Wed, 18 Jul 2012 16:08:32 +0100 Received: from d06av11.portsmouth.uk.ibm.com (loopback [127.0.0.1]) by d06av11.portsmouth.uk.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id q6IF8VEf008044 for ; Wed, 18 Jul 2012 09:08:31 -0600 Received: from localhost (sig-9-145-185-169.de.ibm.com [9.145.185.169]) by d06av11.portsmouth.uk.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id q6IF8UBe008022; Wed, 18 Jul 2012 09:08:30 -0600 From: Stefan Hajnoczi To: Date: Wed, 18 Jul 2012 16:07:36 +0100 Message-Id: <1342624074-24650-10-git-send-email-stefanha@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: <1342624074-24650-1-git-send-email-stefanha@linux.vnet.ibm.com> References: <1342624074-24650-1-git-send-email-stefanha@linux.vnet.ibm.com> x-cbid: 12071815-5024-0000-0000-000003438A95 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 195.75.94.107 Cc: Kevin Wolf , Anthony Liguori , Stefan Hajnoczi , kvm@vger.kernel.org, "Michael S. Tsirkin" , Khoa Huynh , Paolo Bonzini , Asias He Subject: [Qemu-devel] [RFC v9 09/27] virtio-blk: Add Linux AIO queue 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+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Requests read from the vring will be placed in a queue where they can be merged as necessary. Once all requests have been read from the vring, the queue can be submitted. Signed-off-by: Stefan Hajnoczi --- hw/dataplane/ioq.h | 104 ++++++++++++++++++++++++++++++++++++++++++++++++++++ hw/virtio-blk.c | 33 ++++++++--------- 2 files changed, 120 insertions(+), 17 deletions(-) create mode 100644 hw/dataplane/ioq.h diff --git a/hw/dataplane/ioq.h b/hw/dataplane/ioq.h new file mode 100644 index 0000000..26ca307 --- /dev/null +++ b/hw/dataplane/ioq.h @@ -0,0 +1,104 @@ +#ifndef IO_QUEUE_H +#define IO_QUEUE_H + +typedef struct { + int fd; /* file descriptor */ + unsigned int maxreqs; /* max length of freelist and queue */ + + io_context_t io_ctx; /* Linux AIO context */ + EventNotifier notifier; /* Linux AIO eventfd */ + + /* Requests can complete in any order so a free list is necessary to manage + * available iocbs. + */ + struct iocb **freelist; /* free iocbs */ + unsigned int freelist_idx; + + /* Multiple requests are queued up before submitting them all in one go */ + struct iocb **queue; /* queued iocbs */ + unsigned int queue_idx; +} IOQueue; + +static void ioq_init(IOQueue *ioq, int fd, unsigned int maxreqs) +{ + ioq->fd = fd; + ioq->maxreqs = maxreqs; + + if (io_setup(maxreqs, &ioq->io_ctx) != 0) { + fprintf(stderr, "ioq io_setup failed\n"); + exit(1); + } + + if (event_notifier_init(&ioq->notifier, 0) != 0) { + fprintf(stderr, "ioq io event notifier creation failed\n"); + exit(1); + } + + ioq->freelist = g_malloc0(sizeof ioq->freelist[0] * maxreqs); + ioq->freelist_idx = 0; + + ioq->queue = g_malloc0(sizeof ioq->queue[0] * maxreqs); + ioq->queue_idx = 0; +} + +static void ioq_cleanup(IOQueue *ioq) +{ + g_free(ioq->freelist); + g_free(ioq->queue); + + event_notifier_cleanup(&ioq->notifier); + io_destroy(ioq->io_ctx); +} + +static EventNotifier *ioq_get_notifier(IOQueue *ioq) +{ + return &ioq->notifier; +} + +static struct iocb *ioq_get_iocb(IOQueue *ioq) +{ + if (unlikely(ioq->freelist_idx == 0)) { + fprintf(stderr, "ioq underflow\n"); + exit(1); + } + struct iocb *iocb = ioq->freelist[--ioq->freelist_idx]; + ioq->queue[ioq->queue_idx++] = iocb; +} + +static __attribute__((unused)) void ioq_put_iocb(IOQueue *ioq, struct iocb *iocb) +{ + if (unlikely(ioq->freelist_idx == ioq->maxreqs)) { + fprintf(stderr, "ioq overflow\n"); + exit(1); + } + ioq->freelist[ioq->freelist_idx++] = iocb; +} + +static __attribute__((unused)) void ioq_rdwr(IOQueue *ioq, bool read, struct iovec *iov, unsigned int count, long long offset) +{ + struct iocb *iocb = ioq_get_iocb(ioq); + + if (read) { + io_prep_preadv(iocb, ioq->fd, iov, count, offset); + } else { + io_prep_pwritev(iocb, ioq->fd, iov, count, offset); + } + io_set_eventfd(iocb, event_notifier_get_fd(&ioq->notifier)); +} + +static __attribute__((unused)) void ioq_fdsync(IOQueue *ioq) +{ + struct iocb *iocb = ioq_get_iocb(ioq); + + io_prep_fdsync(iocb, ioq->fd); + io_set_eventfd(iocb, event_notifier_get_fd(&ioq->notifier)); +} + +static __attribute__((unused)) int ioq_submit(IOQueue *ioq) +{ + int rc = io_submit(ioq->io_ctx, ioq->queue_idx, ioq->queue); + ioq->queue_idx = 0; /* reset */ + return rc; +} + +#endif /* IO_QUEUE_H */ diff --git a/hw/virtio-blk.c b/hw/virtio-blk.c index 91f1bab..5e1ed79 100644 --- a/hw/virtio-blk.c +++ b/hw/virtio-blk.c @@ -13,12 +13,14 @@ #include #include "qemu-common.h" +#include "block_int.h" #include "qemu-thread.h" #include "qemu-error.h" #include "blockdev.h" #include "virtio-blk.h" #include "hw/dataplane/event-poll.h" #include "hw/dataplane/vring.h" +#include "hw/dataplane/ioq.h" #include "kvm.h" enum { @@ -42,9 +44,9 @@ typedef struct VirtIOBlock Vring vring; /* virtqueue vring */ + IOQueue ioqueue; /* Linux AIO queue (should really be per dataplane thread) */ + EventPoll event_poll; /* event poller */ - io_context_t io_ctx; /* Linux AIO context */ - EventNotifier io_notifier; /* Linux AIO eventfd */ EventHandler io_handler; /* Linux AIO completion handler */ EventHandler notify_handler; /* virtqueue notify handler */ } VirtIOBlock; @@ -128,6 +130,14 @@ static void *data_plane_thread(void *opaque) return NULL; } +/* Normally the block driver passes down the fd, there's no way to get it from + * above. + */ +static int get_raw_posix_fd_hack(VirtIOBlock *s) +{ + return *(int*)s->bs->file->opaque; +} + static void data_plane_start(VirtIOBlock *s) { vring_setup(&s->vring, &s->vdev, 0); @@ -138,23 +148,13 @@ static void data_plane_start(VirtIOBlock *s) fprintf(stderr, "virtio-blk failed to set host notifier, ensure -enable-kvm is set\n"); exit(1); } - event_poll_add(&s->event_poll, &s->notify_handler, virtio_queue_get_host_notifier(s->vq), handle_notify); - /* Create aio context */ - if (io_setup(SEG_MAX, &s->io_ctx) != 0) { - fprintf(stderr, "virtio-blk io_setup failed\n"); - exit(1); - } - - if (event_notifier_init(&s->io_notifier, 0) != 0) { - fprintf(stderr, "virtio-blk io event notifier creation failed\n"); - exit(1); - } - - event_poll_add(&s->event_poll, &s->io_handler, &s->io_notifier, handle_io); + ioq_init(&s->ioqueue, get_raw_posix_fd_hack(s), REQ_MAX); + /* TODO populate ioqueue freelist */ + event_poll_add(&s->event_poll, &s->io_handler, ioq_get_notifier(&s->ioqueue), handle_io); qemu_thread_create(&s->data_plane_thread, data_plane_thread, s, QEMU_THREAD_JOINABLE); @@ -167,8 +167,7 @@ static void data_plane_stop(VirtIOBlock *s) /* TODO stop data plane thread */ - event_notifier_cleanup(&s->io_notifier); - io_destroy(s->io_ctx); + ioq_cleanup(&s->ioqueue); s->vdev.binding->set_host_notifier(s->vdev.binding_opaque, 0, false);