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: 1211581 Return-Path: X-Original-To: patchwork-kvm@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork1.kernel.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by patchwork1.kernel.org (Postfix) with ESMTP id 7CF2940071 for ; Wed, 18 Jul 2012 15:08:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754815Ab2GRPIs (ORCPT ); Wed, 18 Jul 2012 11:08:48 -0400 Received: from e06smtp17.uk.ibm.com ([195.75.94.113]:38288 "EHLO e06smtp17.uk.ibm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754676Ab2GRPIh (ORCPT ); Wed, 18 Jul 2012 11:08:37 -0400 Received: from /spool/local by e06smtp17.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Wed, 18 Jul 2012 16:08:36 +0100 Received: from d06nrmr1806.portsmouth.uk.ibm.com (9.149.39.193) by e06smtp17.uk.ibm.com (192.168.101.147) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Wed, 18 Jul 2012 16:08:33 +0100 Received: from d06av11.portsmouth.uk.ibm.com (d06av11.portsmouth.uk.ibm.com [9.149.37.252]) by d06nrmr1806.portsmouth.uk.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id q6IF8WxJ2281616 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 q6IF8VEl008044 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: Cc: , Anthony Liguori , Kevin Wolf , Paolo Bonzini , "Michael S. Tsirkin" , Asias He , Khoa Huynh , Stefan Hajnoczi Subject: [RFC v9 09/27] virtio-blk: Add Linux AIO queue 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-0542-0000-0000-00000280013A Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.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);