From 3a1ed0ed9dff3a7349053a529a082aa69cb96ad1 Mon Sep 17 00:00:00 2001 Message-Id: <3a1ed0ed9dff3a7349053a529a082aa69cb96ad1.1358333973.git.minovotn@redhat.com> In-Reply-To: References: From: Stefan Hajnoczi Date: Mon, 14 Jan 2013 15:00:15 +0100 Subject: [PATCH 3/3] dataplane: handle misaligned virtio-blk requests RH-Author: Stefan Hajnoczi Message-id: <1358175615-24929-4-git-send-email-stefanha@redhat.com> Patchwork-id: 45783 O-Subject: [RHEL-6.4 qemu-kvm PATCH 3/3] dataplane: handle misaligned virtio-blk requests Bugzilla: 895392 RH-Acked-by: Kevin Wolf RH-Acked-by: Laszlo Ersek RH-Acked-by: Paolo Bonzini O_DIRECT on Linux has alignment requirements on I/O buffers and misaligned requests result in -EINVAL. The Linux virtio_blk guest driver usually submits aligned requests so I forgot to handle misaligned requests. It turns out that virtio-win guest drivers submit misaligned requests. Handle them using a bounce buffer that meets alignment requirements. Signed-off-by: Stefan Hajnoczi Signed-off-by: Kevin Wolf (cherry picked from commit de0161c0d553f2aaf6118ca87f978a5e6b4a9732) Conflicts: hw/dataplane/virtio-blk.c: qemu_iovec_from_buf() -> qemu_iovec_from_buffer() qemu_iovec_to_buf() -> qemu_iovec_to_buffer() g_slice_alloc() -> qemu_malloc() g_slice_free() -> qemu_free() Signed-off-by: Stefan Hajnoczi --- hw/dataplane/virtio-blk.c | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) Signed-off-by: Michal Novotny --- hw/dataplane/virtio-blk.c | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/hw/dataplane/virtio-blk.c b/hw/dataplane/virtio-blk.c index d706b24..7703370 100644 --- a/hw/dataplane/virtio-blk.c +++ b/hw/dataplane/virtio-blk.c @@ -34,6 +34,8 @@ typedef struct { struct iocb iocb; /* Linux AIO control block */ QEMUIOVector *inhdr; /* iovecs for virtio_blk_inhdr */ unsigned int head; /* vring descriptor index */ + struct iovec *bounce_iov; /* used if guest buffers are unaligned */ + QEMUIOVector *read_qiov; /* for read completion /w bounce buffer */ } VirtIOBlockRequest; struct VirtIOBlockDataPlane { @@ -87,6 +89,18 @@ static void complete_request(struct iocb *iocb, ssize_t ret, void *opaque) trace_virtio_blk_data_plane_complete_request(s, req->head, ret); + if (req->read_qiov) { + assert(req->bounce_iov); + qemu_iovec_from_buffer(req->read_qiov, req->bounce_iov->iov_base, len); + qemu_iovec_destroy(req->read_qiov); + qemu_free(req->read_qiov); + } + + if (req->bounce_iov) { + qemu_vfree(req->bounce_iov->iov_base); + qemu_free(req->bounce_iov); + } + qemu_iovec_from_buffer(req->inhdr, &hdr, sizeof(hdr)); qemu_iovec_destroy(req->inhdr); qemu_free(req->inhdr); @@ -134,6 +148,30 @@ static int do_rdwr_cmd(VirtIOBlockDataPlane *s, bool read, QEMUIOVector *inhdr) { struct iocb *iocb; + QEMUIOVector qiov; + struct iovec *bounce_iov = NULL; + QEMUIOVector *read_qiov = NULL; + + qemu_iovec_init_external(&qiov, iov, iov_cnt); + if (!bdrv_qiov_is_aligned(s->blk->conf.bs, &qiov)) { + void *bounce_buffer = qemu_blockalign(s->blk->conf.bs, qiov.size); + + if (read) { + /* Need to copy back from bounce buffer on completion */ + read_qiov = qemu_malloc(sizeof(*read_qiov)); + qemu_iovec_init(read_qiov, iov_cnt); + qemu_iovec_concat_iov(read_qiov, iov, iov_cnt, 0, qiov.size); + } else { + qemu_iovec_to_buffer(&qiov, bounce_buffer); + } + + /* Redirect I/O to aligned bounce buffer */ + bounce_iov = qemu_malloc(sizeof(*bounce_iov)); + bounce_iov->iov_base = bounce_buffer; + bounce_iov->iov_len = qiov.size; + iov = bounce_iov; + iov_cnt = 1; + } iocb = ioq_rdwr(&s->ioqueue, read, iov, iov_cnt, offset); @@ -141,6 +179,8 @@ static int do_rdwr_cmd(VirtIOBlockDataPlane *s, bool read, VirtIOBlockRequest *req = container_of(iocb, VirtIOBlockRequest, iocb); req->head = head; req->inhdr = inhdr; + req->bounce_iov = bounce_iov; + req->read_qiov = read_qiov; return 0; } -- 1.7.11.7