qcow2: Avoid bounce buffers
cover qcow2: Avoid bounce buffers
Kevin Wolf (3):
cutils: qemu_iovec_copy and qemu_iovec_memset
qcow2: Avoid bounce buffers for AIO read requests
qcow2: Avoid bounce buffers for AIO write requests
block/qcow2-cluster.c | 8 +++- block/qcow2.c | 115 ++++++++++++++++++++++++++++++------------------- block/qcow2.h | 4 +- cutils.c | 50 +++++++++++++++++---- qemu-common.h | 3 + 5 files changed, 124 insertions(+), 56 deletions(-)
Signature · 1 line
1.7.2.2
PATCH 1/3 cutils: qemu_iovec_copy and qemu_iovec_memset
This adds two functions that work on QEMUIOVectors and will be used by the next qcow2 patches.
--- cutils.c | 50 +++++++++++++++++++++++++++++++++++++++++--------- qemu-common.h | 3 +++ 2 files changed, 44 insertions(+), 9 deletions(-)
cutils.c +41 -9
@@ -168,30 +168,50 @@ void qemu_iovec_add(QEMUIOVector *qiov, void *base, size_t len) }/* -* Copies iovecs from src to the end dst until src is completely copied or the -* total size of the copied iovec reaches size. The size of the last copied -* iovec is changed in order to fit the specified total size if it isn't a -* perfect fit already. +* Copies iovecs from src to the end of dst. It starts copying after skipping +* the given number of bytes in src and copies until src is completely copied +* or the total size of the copied iovec reaches size.The size of the last +* copied iovec is changed in order to fit the specified total size if it isn't +* a perfect fit already. */ -void qemu_iovec_concat (QEMUIOVector *dst,QEMUIOVector *src,size_t size) +void qemu_iovec_copy (QEMUIOVector *dst,QEMUIOVector *src,uint64_t skip, +size_t size) {int i;size_t done; +void *iov_base; +uint64_t iov_len;assert (dst->nalloc !=-1 ); done =0 ;for (i =0 ; (i < src->niov) && (done != size); i++) { -if (done + src->iov[i].iov_len > size) { -qemu_iovec_add (dst, src->iov[i].iov_base, size - done); +if (skip >= src->iov[i].iov_len) { +/* Skip the whole iov */ + skip -= src->iov[i].iov_len; +continue ; + }else { +/* Skip only part (or nothing) of the iov */ + iov_base = (uint8_t *) src->iov[i].iov_base + skip; + iov_len = src->iov[i].iov_len - skip; + skip =0 ; + } + +if (done + iov_len > size) { +qemu_iovec_add (dst, iov_base, size - done);break ; }else { -qemu_iovec_add (dst, src->iov[i].iov_base, src->iov[i].iov_len); +qemu_iovec_add (dst, iov_base, iov_len); } - done += src->iov[i].iov_len; + done += iov_len; } } +void qemu_iovec_concat (QEMUIOVector *dst,QEMUIOVector *src,size_t size) +{ +qemu_iovec_copy (dst, src,0 , size); +} +void qemu_iovec_destroy (QEMUIOVector *qiov) {assert (qiov->nalloc !=-1 );
@@ -234,6 +254,18 @@ void qemu_iovec_from_buffer(QEMUIOVector *qiov, const void *buf, size_t count) } } +void qemu_iovec_memset (QEMUIOVector *qiov,int c,size_t count) +{ +size_t n; +int i; + +for (i =0 ; i < qiov->niov && count; ++i) { + n =MIN (count, qiov->iov[i].iov_len); +memset (qiov->iov[i].iov_base, c, n); + count -= n; + } +} +#ifndef _WIN32/* Sets a specific flag */ int fcntl_setfl (int fd,int flag)
qemu-common.h +3 -0
@@ -278,11 +278,14 @@ typedef struct QEMUIOVector {void qemu_iovec_init (QEMUIOVector *qiov,int alloc_hint);void qemu_iovec_init_external (QEMUIOVector *qiov,struct iovec *iov,int niov);void qemu_iovec_add (QEMUIOVector *qiov,void *base,size_t len); +void qemu_iovec_copy (QEMUIOVector *dst,QEMUIOVector *src,uint64_t skip, +size_t size);void qemu_iovec_concat (QEMUIOVector *dst,QEMUIOVector *src,size_t size);void qemu_iovec_destroy (QEMUIOVector *qiov);void qemu_iovec_reset (QEMUIOVector *qiov);void qemu_iovec_to_buffer (QEMUIOVector *qiov,void *buf);void qemu_iovec_from_buffer (QEMUIOVector *qiov,const void *buf,size_t count); +void qemu_iovec_memset (QEMUIOVector *qiov,int c,size_t count);struct Monitor ;typedef struct Monitor Monitor ;
Signature · 1 line
1.7.2.2
PATCH 2/3 qcow2: Avoid bounce buffers for AIO read requests
In reply to Kevin Wolfqcow2 used to use bounce buffers for any AIO requests. This does not only imply unnecessary copying, but also unbounded allocations which should be avoided.
This patch removes bounce buffers from the normal AIO read path, and constrains them to a constant size for encrypted images.
--- block/qcow2-cluster.c | 8 ++++- block/qcow2.c | 86 +++++++++++++++++++++++++++++++++--------------- block/qcow2.h | 4 +- 3 files changed, 68 insertions(+), 30 deletions(-)
block/qcow2-cluster.c +7 -1
@@ -349,6 +349,8 @@ static int qcow_read(BlockDriverState *bs, int64_t sector_num,BDRVQcowState *s = bs->opaque;int ret, index_in_cluster, n, n1;uint64_t cluster_offset; +struct iovec iov; +QEMUIOVector qiov;while (nb_sectors >0 ) { n = nb_sectors;
@@ -363,7 +365,11 @@ static int qcow_read(BlockDriverState *bs, int64_t sector_num,if (!cluster_offset) {if (bs->backing_hd) {/* read from the base image */ - n1 =qcow2_backing_read1 (bs->backing_hd, sector_num, buf, n); + iov.iov_base = buf; + iov.iov_len = n *512 ; +qemu_iovec_init_external (&qiov, &iov,1 ); + + n1 =qcow2_backing_read1 (bs->backing_hd, &qiov, sector_num, n);if (n1 >0 ) {BLKDBG_EVENT (bs->file,BLKDBG_READ_BACKING ); ret =bdrv_read (bs->backing_hd, sector_num, buf, n1);
block/qcow2.c +59 -27
164 lines not on this page · raw message
block/qcow2.h +2 -2
@@ -166,8 +166,8 @@ static inline int64_t align_offset(int64_t offset, int n)// FIXME Need qcow2_ prefix to global functions /* qcow2.c functions */ -int qcow2_backing_read1 (BlockDriverState *bs, -int64_t sector_num,uint8_t *buf,int nb_sectors); +int qcow2_backing_read1 (BlockDriverState *bs,QEMUIOVector *qiov, +int64_t sector_num,int nb_sectors);/* qcow2-refcount.c functions */ int qcow2_refcount_init (BlockDriverState *bs);
Signature · 1 line
1.7.2.2
PATCH 3/3 qcow2: Avoid bounce buffers for AIO write requests
In reply to Kevin Wolfqcow2 used to use bounce buffers for any AIO requests. This does not only imply unnecessary copying, but also unbounded allocations which should be avoided.
This patch removes bounce buffers from the normal AIO write path. Encrypted images continue to use a bounce buffer, however with constant size.
--- block/qcow2.c | 41 ++++++++++++++++++----------------------- 1 files changed, 18 insertions(+), 23 deletions(-)
block/qcow2.c +18 -23
@@ -332,15 +332,12 @@ typedef struct QCowAIOCB {BlockDriverAIOCB common;int64_t sector_num;QEMUIOVector *qiov; -uint8_t *buf; -void *orig_buf;int remaining_sectors;int cur_nr_sectors;/* number of sectors in current iteration */ uint64_t bytes_done;uint64_t cluster_offset;uint8_t *cluster_data;BlockDriverAIOCB *hd_aiocb; -struct iovec hd_iov;QEMUIOVector hd_qiov;QEMUBH *bh;QCowL2Meta l2meta;
@@ -531,14 +528,7 @@ static QCowAIOCB *qcow_aio_setup(BlockDriverState *bs, acb->sector_num = sector_num; acb->qiov = qiov; -if (!is_write) { -qemu_iovec_init (&acb->hd_qiov, qiov->niov); - }else if (qiov->niov ==1 ) { - acb->buf = (uint8_t *)qiov->iov->iov_base; - }else { - acb->buf = acb->orig_buf =qemu_blockalign (bs, qiov->size); -qemu_iovec_to_buffer (qiov, acb->buf); - } +qemu_iovec_init (&acb->hd_qiov, qiov->niov); acb->bytes_done =0 ; acb->remaining_sectors = nb_sectors;
@@ -590,7 +580,6 @@ static void qcow_aio_write_cb(void *opaque, int ret)BlockDriverState *bs = acb->common.bs;BDRVQcowState *s = bs->opaque;int index_in_cluster; -const uint8_t *src_buf;int n_end; acb->hd_aiocb =NULL ;
@@ -606,7 +595,7 @@ static void qcow_aio_write_cb(void *opaque, int ret) acb->remaining_sectors -= acb->cur_nr_sectors; acb->sector_num += acb->cur_nr_sectors; - acb->buf += acb->cur_nr_sectors *512 ; + acb->bytes_done += acb->cur_nr_sectors *512 ;if (acb->remaining_sectors ==0 ) {/* request completed */
@@ -637,20 +626,27 @@ static void qcow_aio_write_cb(void *opaque, int ret)assert ((acb->cluster_offset &511 ) ==0 ); +qemu_iovec_reset (&acb->hd_qiov); +qemu_iovec_copy (&acb->hd_qiov, acb->qiov, acb->bytes_done, + acb->cur_nr_sectors *512 ); +if (s->crypt_method) {if (!acb->cluster_data) { acb->cluster_data =qemu_mallocz (QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size); } -qcow2_encrypt_sectors (s, acb->sector_num, acb->cluster_data, acb->buf, - acb->cur_nr_sectors,1 , &s->aes_encrypt_key); - src_buf = acb->cluster_data; - }else { - src_buf = acb->buf; + +assert (acb->hd_qiov.size <=QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size); +qemu_iovec_to_buffer (&acb->hd_qiov, acb->cluster_data); + +qcow2_encrypt_sectors (s, acb->sector_num, acb->cluster_data, + acb->cluster_data, acb->cur_nr_sectors,1 , &s->aes_encrypt_key); + +qemu_iovec_reset (&acb->hd_qiov); +qemu_iovec_add (&acb->hd_qiov, acb->cluster_data, + acb->cur_nr_sectors *512 ); } - acb->hd_iov.iov_base = (void *)src_buf; - acb->hd_iov.iov_len = acb->cur_nr_sectors *512 ; -qemu_iovec_init_external (&acb->hd_qiov, &acb->hd_iov,1 ); +BLKDBG_EVENT (bs->file,BLKDBG_WRITE_AIO ); acb->hd_aiocb = bdrv_aio_writev(bs->file, (acb->cluster_offset >> 9) + index_in_cluster,
@@ -668,9 +664,8 @@ fail:QLIST_REMOVE (&acb->l2meta, next_in_flight); } done: -if (acb->qiov->niov >1 ) -qemu_vfree (acb->orig_buf); acb->common.cb (acb->common.opaque, ret); +qemu_iovec_destroy (&acb->hd_qiov);qemu_aio_release (acb); }
Signature · 1 line
1.7.2.2
Quoted 11 lines · Kevin Wolf
Kevin Wolf (3):
cutils:qemu_iovec_copyandqemu_iovec_memset
qcow2: Avoid bounce buffers for AIO read requests
qcow2: Avoid bounce buffers for AIO write requestsblock/qcow2-cluster.c | 8 +++-
block/qcow2.c | 115 ++++++++++++++++++++++++++++++-------------------block/qcow2.h| 4 +-
cutils.c | 50 +++++++++++++++++----
qemu-common.h | 3 +5 files changed, 124 insertions(+), 56 deletions(-)
Looks good. The common iovec functions promise to be useful too.
Stefan