qcow2: encryption threads
v6 cover qcow2: encryption threads
v6:
- Rebase on master, so 02 changed to reflect changes in qcow2_compress(), keep r-b.
- Rewrite 06 to even simpler locking [Max], drop r-b
Drop following
"qcow2: qcow2_co_preadv: skip using hd_qiov when possible", I'll do it in my other coming soon series, and it is actually unrelated to current.
- Drop controversial perf test, hope I'll return to this topic, but not in context of these series.
- Add Max's r-b's to all here, except 06
Vladimir Sementsov-Ogievskiy (8):
qcow2.h: add missing include
qcow2: add separate file for threaded data processing functions
qcow2-threads: use thread_pool_submit_co
qcow2-threads: qcow2_co_do_compress: protect queuing by mutex
qcow2-threads: split out generic path
qcow2: qcow2_co_preadv: improve locking
qcow2: bdrv_co_pwritev: move encryption code out of the lock
qcow2: do encryption in threads
block/qcow2.h | 20 ++- block/qcow2-bitmap.c | 1 - block/qcow2-cache.c | 1 - block/qcow2-cluster.c | 8 +- block/qcow2-refcount.c | 1 - block/qcow2-snapshot.c | 1 - block/qcow2-threads.c | 268 +++++++++++++++++++++++++++++++++++++++++ block/qcow2.c | 241 +++++------------------------------- block/Makefile.objs | 2 +- 9 files changed, 321 insertions(+), 222 deletions(-) create mode 100644 block/qcow2-threads.c
Signature · 1 line
2.18.0
PATCH v6 1/8 qcow2.h: add missing include
qcow2.h depends on block_int.h. Compilation isn't broken currently only due to block_int.h always included before qcow2.h. Though, it seems better to directly include block_int.h in qcow2.h.
--- block/qcow2.h | 1 + block/qcow2-bitmap.c | 1 - block/qcow2-cache.c | 1 - block/qcow2-cluster.c | 1 - block/qcow2-refcount.c | 1 - block/qcow2-snapshot.c | 1 - block/qcow2.c | 1 - 7 files changed, 1 insertion(+), 6 deletions(-)
block/qcow2.h +1 -0
@@ -28,6 +28,7 @@#include "crypto/block.h" #include "qemu/coroutine.h" #include "qemu/units.h" +#include "block/block_int.h" //#define DEBUG_ALLOC //#define DEBUG_ALLOC2
block/qcow2-bitmap.c +0 -1
@@ -29,7 +29,6 @@#include "qapi/error.h" #include "qemu/cutils.h" -#include "block/block_int.h" #include "qcow2.h" /* NOTICE: BME here means Bitmaps Extension and used as a namespace for
block/qcow2-cache.c +0 -1
@@ -23,7 +23,6 @@*/ #include "qemu/osdep.h" -#include "block/block_int.h" #include "qemu-common.h" #include "qcow2.h" #include "trace.h"
block/qcow2-cluster.c +0 -1
@@ -27,7 +27,6 @@#include "qapi/error.h" #include "qemu-common.h" -#include "block/block_int.h" #include "qcow2.h" #include "qemu/bswap.h" #include "trace.h"
block/qcow2-refcount.c +0 -1
@@ -25,7 +25,6 @@#include "qemu/osdep.h" #include "qapi/error.h" #include "qemu-common.h" -#include "block/block_int.h" #include "qcow2.h" #include "qemu/range.h" #include "qemu/bswap.h"
block/qcow2-snapshot.c +0 -1
@@ -24,7 +24,6 @@#include "qemu/osdep.h" #include "qapi/error.h" -#include "block/block_int.h" #include "qcow2.h" #include "qemu/bswap.h" #include "qemu/error-report.h"
block/qcow2.c +0 -1
@@ -27,7 +27,6 @@#define ZLIB_CONST #include <zlib.h> -#include "block/block_int.h" #include "block/qdict.h" #include "sysemu/block-backend.h" #include "qemu/module.h"
Signature · 1 line
2.18.0
PATCH v6 2/8 qcow2: add separate file for threaded data processing functions
In reply to Vladimir Sementsov-OgievskiyMove compression-on-threads to separate file. Encryption will be in it too.
--- block/qcow2.h | 7 ++ block/qcow2-threads.c | 201 ++++++++++++++++++++++++++++++++++++++++++ block/qcow2.c | 169 ----------------------------------- block/Makefile.objs | 2 +- 4 files changed, 209 insertions(+), 170 deletions(-) create mode 100644 block/qcow2-threads.c
block/qcow2.h +7 -0
@@ -735,4 +735,11 @@ void qcow2_remove_persistent_dirty_bitmap(BlockDriverState *bs,const char *name, Error **errp); +ssize_t coroutine_fn +qcow2_co_compress (BlockDriverState *bs,void *dest,size_t dest_size, +const void *src,size_t src_size); +ssize_t coroutine_fn +qcow2_co_decompress (BlockDriverState *bs,void *dest,size_t dest_size, +const void *src,size_t src_size); +#endif
block/qcow2-threads.c new file +201 -0
201 lines not on this page · raw message
block/qcow2.c +0 -169
187 lines not on this page · raw message
block/Makefile.objs +1 -1
@@ -6,7 +6,7 @@ block-obj-$(CONFIG_BOCHS) += bochs.o block-obj-$(CONFIG_VVFAT) += vvfat.o block-obj-$(CONFIG_DMG) += dmg.o -block-obj-y += qcow2.o qcow2-refcount.o qcow2-cluster.o qcow2-snapshot.o qcow2-cache.o qcow2-bitmap.o +block-obj-y += qcow2.o qcow2-refcount.o qcow2-cluster.o qcow2-snapshot.o qcow2-cache.o qcow2-bitmap.o qcow2-threads.o block-obj-$(CONFIG_QED) += qed.o qed-l2-cache.o qed-table.o qed-cluster.o block-obj-$(CONFIG_QED) += qed-check.o block-obj-y += vhdx.o vhdx-endian.o vhdx-log.o
Signature · 1 line
2.18.0
PATCH v6 3/8 qcow2-threads: use thread_pool_submit_co
In reply to Vladimir Sementsov-OgievskiyUse thread_pool_submit_co, instead of reinventing it here. Note, that thread_pool_submit_aio() never returns NULL, so checking it was an extra thing.
--- block/qcow2-threads.c | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-)
block/qcow2-threads.c +2 -15
@@ -144,17 +144,11 @@ static int qcow2_compress_pool_func(void *opaque)return 0 ; } -static void qcow2_compress_complete (void *opaque,int ret) -{ -qemu_coroutine_enter (opaque); -} - static ssize_t coroutine_fnqcow2_co_do_compress (BlockDriverState *bs,void *dest,size_t dest_size,const void *src,size_t src_size,Qcow2CompressFunc func) {BDRVQcow2State *s = bs->opaque; -BlockAIOCB *acb;ThreadPool *pool =aio_get_thread_pool (bdrv_get_aio_context (bs));Qcow2CompressData arg = { .dest = dest,
@@ -169,16 +163,9 @@ qcow2_co_do_compress(BlockDriverState *bs, void *dest, size_t dest_size, } s->nb_compress_threads++; - acb =thread_pool_submit_aio (pool, qcow2_compress_pool_func, &arg, - qcow2_compress_complete, -qemu_coroutine_self ()); - -if (!acb) { - s->nb_compress_threads--; -return -EINVAL ; - } -qemu_coroutine_yield (); +thread_pool_submit_co (pool, qcow2_compress_pool_func, &arg); s->nb_compress_threads--; +qemu_co_queue_next (&s->compress_wait_queue);return arg.ret;
Signature · 1 line
2.18.0
PATCH v6 4/8 qcow2-threads: qcow2_co_do_compress: protect queuing by mutex
In reply to Vladimir Sementsov-OgievskiyDrop dependence on AioContext lock.
--- block/qcow2-threads.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)
block/qcow2-threads.c +7 -3
@@ -158,15 +158,19 @@ qcow2_co_do_compress(BlockDriverState *bs, void *dest, size_t dest_size, .func = func, }; +qemu_co_mutex_lock (&s->lock);while (s->nb_compress_threads >=MAX_COMPRESS_THREADS ) { -qemu_co_queue_wait (&s->compress_wait_queue,NULL ); +qemu_co_queue_wait (&s->compress_wait_queue, &s->lock); } - s->nb_compress_threads++; +qemu_co_mutex_unlock (&s->lock); +thread_pool_submit_co (pool, qcow2_compress_pool_func, &arg); - s->nb_compress_threads--; +qemu_co_mutex_lock (&s->lock); + s->nb_compress_threads--;qemu_co_queue_next (&s->compress_wait_queue); +qemu_co_mutex_unlock (&s->lock);return arg.ret; }
Signature · 1 line
2.18.0
PATCH v6 5/8 qcow2-threads: split out generic path
In reply to Vladimir Sementsov-OgievskiyMove generic part out of qcow2_co_do_compress, to reuse it for encryption and rename things that would be shared with encryption path.
--- block/qcow2.h | 4 ++-- block/qcow2-threads.c | 47 ++++++++++++++++++++++++++++--------------- block/qcow2.c | 2 +- 3 files changed, 34 insertions(+), 19 deletions(-)
block/qcow2.h +2 -2
@@ -347,8 +347,8 @@ typedef struct BDRVQcow2State {char *image_backing_format;char *image_data_file; -CoQueue compress_wait_queue; -int nb_compress_threads; +CoQueue thread_task_queue; +int nb_threads;BdrvChild *data_file; } BDRVQcow2State;
block/qcow2-threads.c +31 -16
65 lines not on this page · raw message
block/qcow2.c +1 -1
@@ -1694,7 +1694,7 @@ static int coroutine_fn qcow2_do_open(BlockDriverState *bs, QDict *options, }#endif -qemu_co_queue_init (&s->compress_wait_queue); +qemu_co_queue_init (&s->thread_task_queue);return ret;
Signature · 1 line
2.18.0
PATCH v6 6/8 qcow2: qcow2_co_preadv: improve locking
In reply to Vladimir Sementsov-OgievskiyBackground: decryption will be done in threads, to take benefit of it, we should move it out of the lock first.
But let's go further: it turns out, that only qcow2_get_cluster_offset() needs locking, so reduce locking to it.
--- block/qcow2.c | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-)
block/qcow2.c +2 -10
@@ -1970,8 +1970,6 @@ static coroutine_fn int qcow2_co_preadv(BlockDriverState *bs, uint64_t offset,qemu_iovec_init (&hd_qiov, qiov->niov); -qemu_co_mutex_lock (&s->lock); -while (bytes !=0 ) {/* prepare next request */
@@ -1981,7 +1979,9 @@ static coroutine_fn int qcow2_co_preadv(BlockDriverState *bs, uint64_t offset, QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size); } +qemu_co_mutex_lock (&s->lock); ret =qcow2_get_cluster_offset (bs, offset, &cur_bytes, &cluster_offset); +qemu_co_mutex_unlock (&s->lock);if (ret <0 ) { goto fail; }
@@ -1996,10 +1996,8 @@ static coroutine_fn int qcow2_co_preadv(BlockDriverState *bs, uint64_t offset,if (bs->backing) {BLKDBG_EVENT (bs->file,BLKDBG_READ_BACKING_AIO ); -qemu_co_mutex_unlock (&s->lock); ret =bdrv_co_preadv (bs->backing, offset, cur_bytes, &hd_qiov,0 ); -qemu_co_mutex_lock (&s->lock);if (ret <0 ) { goto fail; }
@@ -2015,11 +2013,9 @@ static coroutine_fn int qcow2_co_preadv(BlockDriverState *bs, uint64_t offset,break ;case QCOW2_CLUSTER_COMPRESSED : -qemu_co_mutex_unlock (&s->lock); ret =qcow2_co_preadv_compressed (bs, cluster_offset, offset, cur_bytes, &hd_qiov); -qemu_co_mutex_lock (&s->lock);if (ret <0 ) { goto fail; }
@@ -2056,11 +2052,9 @@ static coroutine_fn int qcow2_co_preadv(BlockDriverState *bs, uint64_t offset, }BLKDBG_EVENT (bs->file,BLKDBG_READ_AIO ); -qemu_co_mutex_unlock (&s->lock); ret =bdrv_co_preadv (s->data_file, cluster_offset + offset_in_cluster, cur_bytes, &hd_qiov,0 ); -qemu_co_mutex_lock (&s->lock);if (ret <0 ) { goto fail; }
@@ -2095,8 +2089,6 @@ static coroutine_fn int qcow2_co_preadv(BlockDriverState *bs, uint64_t offset, ret =0 ; fail: -qemu_co_mutex_unlock (&s->lock); -qemu_iovec_destroy (&hd_qiov);qemu_vfree (cluster_data);
Signature · 1 line
2.18.0
Re: [Qemu-devel] [PATCH v6 6/8] qcow2: qcow2_co_preadv: improve locking
Quoted 7 lines · Vladimir Sementsov-Ogievskiy
Background: decryption will be done in threads, to take benefit of it, we should move it out of the lock first.
But let's go further: it turns out, that only
qcow2_get_cluster_offset()needs locking, so reduce locking to it.Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>
Berto
PATCH v6 7/8 qcow2: bdrv_co_pwritev: move encryption code out of the lock
In reply to Vladimir Sementsov-OgievskiyEncryption will be done in threads, to take benefit of it, we should move it out of the lock first.
--- block/qcow2.c | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-)
block/qcow2.c +21 -14
@@ -2169,11 +2169,20 @@ static coroutine_fn int qcow2_co_pwritev(BlockDriverState *bs, uint64_t offset, ret =qcow2_alloc_cluster_offset (bs, offset, &cur_bytes, &cluster_offset, &l2meta);if (ret <0 ) { - goto fail; + goto out_locked; }assert ((cluster_offset &511 ) ==0 ); + ret =qcow2_pre_write_overlap_check (bs,0 , + cluster_offset + offset_in_cluster, + cur_bytes, true); +if (ret <0 ) { + goto out_locked; + } + +qemu_co_mutex_unlock (&s->lock); +qemu_iovec_reset (&hd_qiov);qemu_iovec_concat (&hd_qiov, qiov, bytes_done, cur_bytes);
@@ -2185,7 +2194,7 @@ static coroutine_fn int qcow2_co_pwritev(BlockDriverState *bs, uint64_t offset, * s->cluster_size);if (cluster_data ==NULL ) { ret = -ENOMEM ; - goto fail; + goto out_unlocked; } }
@@ -2200,40 +2209,34 @@ static coroutine_fn int qcow2_co_pwritev(BlockDriverState *bs, uint64_t offset, cluster_data, cur_bytes, NULL) < 0) { ret = -EIO ; - goto fail; + goto out_unlocked; }qemu_iovec_reset (&hd_qiov);qemu_iovec_add (&hd_qiov, cluster_data, cur_bytes); } - ret =qcow2_pre_write_overlap_check (bs,0 , - cluster_offset + offset_in_cluster, cur_bytes, true); -if (ret <0 ) { - goto fail; - } -/* If we need to do COW, check if it's possible to merge the * writing of the guest data together with that of the COW regions. * If it's not possible (or not necessary) then write the * guest data now. */ if (!merge_cow (offset, cur_bytes, &hd_qiov, l2meta)) { -qemu_co_mutex_unlock (&s->lock);BLKDBG_EVENT (bs->file,BLKDBG_WRITE_AIO );trace_qcow2_writev_data (qemu_coroutine_self (), cluster_offset + offset_in_cluster); ret =bdrv_co_pwritev (s->data_file, cluster_offset + offset_in_cluster, cur_bytes, &hd_qiov,0 ); -qemu_co_mutex_lock (&s->lock);if (ret <0 ) { - goto fail; + goto out_unlocked; } } +qemu_co_mutex_lock (&s->lock); + ret =qcow2_handle_l2meta (bs, &l2meta, true);if (ret) { - goto fail; + goto out_locked; } bytes -= cur_bytes;
@@ -2242,8 +2245,12 @@ static coroutine_fn int qcow2_co_pwritev(BlockDriverState *bs, uint64_t offset,trace_qcow2_writev_done_part (qemu_coroutine_self (), cur_bytes); } ret =0 ; + goto out_locked; -fail: +out_unlocked: +qemu_co_mutex_lock (&s->lock); + +out_locked:qcow2_handle_l2meta (bs, &l2meta, false);qemu_co_mutex_unlock (&s->lock);
Signature · 1 line
2.18.0
PATCH v6 8/8 qcow2: do encryption in threads
In reply to Vladimir Sementsov-OgievskiyDo encryption/decryption in threads, like it is already done for compression. This improves asynchronous encrypted io.
--- block/qcow2.h | 8 ++++++ block/qcow2-cluster.c | 7 ++--- block/qcow2-threads.c | 65 +++++++++++++++++++++++++++++++++++++++++-- block/qcow2.c | 22 +++++---------- 4 files changed, 81 insertions(+), 21 deletions(-)
block/qcow2.h +8 -0
@@ -264,6 +264,8 @@ typedef struct Qcow2BitmapHeaderExt {uint64_t bitmap_directory_offset; }QEMU_PACKED Qcow2BitmapHeaderExt; +#define QCOW2_MAX_THREADS 4 + typedef struct BDRVQcow2State { int cluster_bits; int cluster_size;
@@ -741,5 +743,11 @@ qcow2_co_compress(BlockDriverState *bs, void *dest, size_t dest_size, ssize_t coroutine_fnqcow2_co_decompress (BlockDriverState *bs,void *dest,size_t dest_size,const void *src,size_t src_size); +int coroutine_fn +qcow2_co_encrypt (BlockDriverState *bs,uint64_t file_cluster_offset, +uint64_t offset,void *buf,size_t len); +int coroutine_fn +qcow2_co_decrypt (BlockDriverState *bs,uint64_t file_cluster_offset, +uint64_t offset,void *buf,size_t len);#endif
block/qcow2-cluster.c +3 -4
@@ -471,13 +471,12 @@ static bool coroutine_fn do_perform_cow_encrypt(BlockDriverState *bs, {if (bytes && bs->encrypted) {BDRVQcow2State *s = bs->opaque; -int64_t offset = (s->crypt_physical_offset ? - (cluster_offset + offset_in_cluster) : - (src_cluster_offset + offset_in_cluster));assert ((offset_in_cluster & ~BDRV_SECTOR_MASK ) ==0 );assert ((bytes & ~BDRV_SECTOR_MASK ) ==0 );assert (s->crypto); -if (qcrypto_block_encrypt (s->crypto, offset, buffer, bytes,NULL ) <0 ) { +if (qcow2_co_encrypt (bs, cluster_offset, + src_cluster_offset + offset_in_cluster, + buffer, bytes) <0 ) {return false; } }
block/qcow2-threads.c +63 -2
74 lines not on this page · raw message
block/qcow2.c +7 -15
@@ -297,7 +297,7 @@ static int qcow2_read_extensions(BlockDriverState *bs, uint64_t start_offset, } s->crypto =qcrypto_block_open (s->crypto_opts,"encrypt." , qcow2_crypto_hdr_read_func, - bs, cflags,1 , errp); + bs, cflags,QCOW2_MAX_THREADS , errp);if (!s->crypto) {return -EINVAL ; }
@@ -1539,7 +1539,8 @@ static int coroutine_fn qcow2_do_open(BlockDriverState *bs, QDict *options, cflags |=QCRYPTO_BLOCK_OPEN_NO_IO ; } s->crypto =qcrypto_block_open (s->crypto_opts,"encrypt." , -NULL ,NULL , cflags,1 , errp); +NULL ,NULL , cflags, +QCOW2_MAX_THREADS , errp);if (!s->crypto) { ret = -EINVAL ; goto fail;
@@ -2062,13 +2063,8 @@ static coroutine_fn int qcow2_co_preadv(BlockDriverState *bs, uint64_t offset,assert (s->crypto);assert ((offset & (BDRV_SECTOR_SIZE -1 )) ==0 );assert ((cur_bytes & (BDRV_SECTOR_SIZE -1 )) ==0 ); -if (qcrypto_block_decrypt (s->crypto, - (s->crypt_physical_offset ? - cluster_offset + offset_in_cluster : - offset), - cluster_data, - cur_bytes, -NULL ) <0 ) { +if (qcow2_co_decrypt (bs, cluster_offset, offset, + cluster_data, cur_bytes) <0 ) { ret = -EIO ; goto fail; }
@@ -2202,12 +2198,8 @@ static coroutine_fn int qcow2_co_pwritev(BlockDriverState *bs, uint64_t offset, QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size);qemu_iovec_to_buf (&hd_qiov,0 , cluster_data, hd_qiov.size); -if (qcrypto_block_encrypt (s->crypto, - (s->crypt_physical_offset ? - cluster_offset + offset_in_cluster : - offset), - cluster_data, - cur_bytes,NULL ) <0 ) { +if (qcow2_co_encrypt (bs, cluster_offset, offset, + cluster_data, cur_bytes) <0 ) { ret = -EIO ; goto out_unlocked; }
Signature · 1 line
2.18.0
Max Reitz
AppliedQuoted 22 lines · Vladimir Sementsov-Ogievskiy
v6:
- Rebase on master, so 02 changed to reflect changes inqcow2_compress(), keep r-b.
- Rewrite 06 to even simpler locking [Max], drop r-b
Drop following
"qcow2:qcow2_co_preadv: skip usinghd_qiovwhen possible", I'll do it in my other coming soon series, and it is actually unrelated to current.
- Drop controversial perf test, hope I'll return to this topic, but not in context of these series.
- Add Max's r-b's to all here, except 06Vladimir Sementsov-Ogievskiy (8):
qcow2.h: add missing include
qcow2: add separate file for threaded data processing functions
qcow2-threads: usethread_pool_submit_co
qcow2-threads:qcow2_co_do_compress: protect queuing by mutex
qcow2-threads: split out generic path
qcow2:qcow2_co_preadv: improve locking
qcow2:bdrv_co_pwritev: move encryption code out of the lock
qcow2: do encryption in threads
Quoted 11 lines
block/qcow2.h | 20 ++- block/qcow2-bitmap.c | 1 - block/qcow2-cache.c | 1 - block/qcow2-cluster.c | 8 +- block/qcow2-refcount.c | 1 - block/qcow2-snapshot.c | 1 - block/qcow2-threads.c | 268 +++++++++++++++++++++++++++++++++++++++++ block/qcow2.c | 241 +++++------------------------------- block/Makefile.objs | 2 +- 9 files changed, 321 insertions(+), 222 deletions(-) create mode 100644 block/qcow2-threads.c
Thanks, applied to my block branch:
https://git.xanclic.moe/XanClic/qemu/commits/branch/block
Max
Quoted 35 lines · Max Reitz
Quoted 32 lines · Vladimir Sementsov-Ogievskiy
v6:
- Rebase on master, so 02 changed to reflect changes inqcow2_compress(), keep r-b.- Rewrite 06 to even simpler locking [Max], drop r-b
Drop following
"qcow2:qcow2_co_preadv: skip usinghd_qiovwhen possible", I'll do it in my other coming soon series, and it is actually unrelated to current.
- Drop controversial perf test, hope I'll return to this topic, but not in context of these series.
- Add Max's r-b's to all here, except 06Vladimir Sementsov-Ogievskiy (8):
qcow2.h: add missing include
qcow2: add separate file for threaded data processing functions
qcow2-threads: usethread_pool_submit_co
qcow2-threads:qcow2_co_do_compress: protect queuing by mutex
qcow2-threads: split out generic path
qcow2:qcow2_co_preadv: improve locking
qcow2:bdrv_co_pwritev: move encryption code out of the lock
qcow2: do encryption in threadsblock/qcow2.h | 20 ++- block/qcow2-bitmap.c | 1 - block/qcow2-cache.c | 1 - block/qcow2-cluster.c | 8 +- block/qcow2-refcount.c | 1 - block/qcow2-snapshot.c | 1 - block/qcow2-threads.c | 268 +++++++++++++++++++++++++++++++++++++++++ block/qcow2.c | 241 +++++------------------------------- block/Makefile.objs | 2 +- 9 files changed, 321 insertions(+), 222 deletions(-)Quoted 1 line
create mode 100644
block/qcow2-threads.c
Quoted 4 lines
Thanks, applied to my block branch:
Thank you!!
Signature · 2 lines
Best regards, Vladimir