Fix mirror segfault with unaligned size
cover Fix mirror segfault with unaligned size
Kevin Wolf (2):
mirror: Fix qiov size for short requests
block: Assert qiov length matches request length
block.c | 2 ++ block/mirror.c | 4 +++- block/raw-posix.c | 12 ++++++++---- tests/qemu-iotests/041 | 5 +++++ tests/qemu-iotests/041.out | 4 ++-- 5 files changed, 20 insertions(+), 7 deletions(-)
Signature · 1 line
1.8.3.1
PATCH 1/2 mirror: Fix qiov size for short requests
When mirroring an image of a size that is not a multiple of the mirror job granularity, the last request would have the right nb_sectors argument, but a qiov that is rounded up to the next multiple of the granularity. Don't do this.
This fixes a segfault that is caused by raw-posix being confused by this and allocating a buffer with request length, but operating on it with qiov length.
--- block/mirror.c | 4 +++- tests/qemu-iotests/041 | 5 +++++ tests/qemu-iotests/041.out | 4 ++-- 3 files changed, 10 insertions(+), 3 deletions(-)
block/mirror.c +3 -1
@@ -265,9 +265,11 @@ static uint64_t coroutine_fn mirror_iteration(MirrorBlockJob *s) next_sector = sector_num;while (nb_chunks-- >0 ) {MirrorBuffer *buf =QSIMPLEQ_FIRST (&s->buf_free); +size_t remaining = (nb_sectors *BDRV_SECTOR_SIZE ) - op->qiov.size; +QSIMPLEQ_REMOVE_HEAD (&s->buf_free, next); s->buf_free_count--; -qemu_iovec_add (&op->qiov, buf, s->granularity); +qemu_iovec_add (&op->qiov, buf,MIN (s->granularity, remaining));/* Advance the HBitmapIter in parallel, so that we do not examine * the same sector twice.
tests/qemu-iotests/041 +5 -0
@@ -219,6 +219,11 @@ class TestSingleDriveZeroLength(TestSingleDrive): test_small_buffer2 = None test_large_cluster = None +class TestSingleDriverUnalignedLength(TestSingleDrive): + image_len = 1025 * 1024 + test_small_buffer2 = None + test_large_cluster = None + class TestMirrorNoBacking(ImageMirroringTestCase): image_len = 2 * 1024 * 1024 # MB
tests/qemu-iotests/041.out +2 -2
@@ -1,5 +1,5 @@ -.............................................. +...................................................... ---------------------------------------------------------------------- -Ran 46 tests +Ran 54 tests OK
Signature · 1 line
1.8.3.1
Re: [Qemu-devel] [PATCH for-2.1 1/2] mirror: Fix qiov size for short requests
Quoted 20 lines · Kevin Wolf
When mirroring an image of a size that is not a multiple of the mirror job granularity, the last request would have the right
nb_sectorsargument, but a qiov that is rounded up to the next multiple of the granularity. Don't do this.This fixes a segfault that is caused by raw-posix being confused by this and allocating a buffer with request length, but operating on it with qiov length.
Reported-by Eric Blake <[email protected]>Signed-off-by Kevin Wolf <[email protected]>--- block/mirror.c | 4 +++- tests/qemu-iotests/041 | 5 +++++ tests/qemu-iotests/041.out | 4 ++-- 3 files changed, 10 insertions(+), 3 deletions(-)+++ b/tests/qemu-iotests/041
tests/qemu-iotests/041 class TestSingleDriveZeroLength(TestSingleDrive): · line 222
Quoted 4 lines
tests/qemu-iotests/041 +1
-0@@ -219,6 +219,11 @@ class TestSingleDriveZeroLength(TestSingleDrive): test_small_buffer2 = None test_large_cluster = None +class TestSingleDriverUnalignedLength(TestSingleDrive):
s/Driver/Drive/ for consistency in the class name?
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Re: [Qemu-devel] [PATCH for-2.1 1/2] mirror: Fix qiov size for short requests
Quoted 19 lines · Eric Blake
Quoted 17 lines · Kevin Wolf
When mirroring an image of a size that is not a multiple of the mirror job granularity, the last request would have the right
nb_sectorsargument, but a qiov that is rounded up to the next multiple of the granularity. Don't do this.This fixes a segfault that is caused by raw-posix being confused by this and allocating a buffer with request length, but operating on it with qiov length.
Reported-by Eric Blake <[email protected]>Signed-off-by Kevin Wolf <[email protected]>--- block/mirror.c | 4 +++- tests/qemu-iotests/041 | 5 +++++ tests/qemu-iotests/041.out | 4 ++-- 3 files changed, 10 insertions(+), 3 deletions(-)
Quoted 9 lines
tests/qemu-iotests/041 class TestSingleDriveZeroLength(TestSingleDrive): · line 222
Quoted 6 lines
+++ b/tests/qemu-iotests/041
tests/qemu-iotests/041 +1
-0@@ -219,6 +219,11 @@ class TestSingleDriveZeroLength(TestSingleDrive): test_small_buffer2 = None test_large_cluster = None +class TestSingleDriverUnalignedLength(TestSingleDrive):s/Driver/Drive/ for consistency in the class name?
Other than that:
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Re: [Qemu-devel] [PATCH for-2.1 1/2] mirror: Fix qiov size for short requests
In reply to Eric BlakeAm 01.07.2014 um 18:52 hat Eric Blake geschrieben:
Quoted 19 lines · Kevin Wolf
Quoted 17 lines
When mirroring an image of a size that is not a multiple of the mirror job granularity, the last request would have the right
nb_sectorsargument, but a qiov that is rounded up to the next multiple of the granularity. Don't do this.This fixes a segfault that is caused by raw-posix being confused by this and allocating a buffer with request length, but operating on it with qiov length.
Reported-by Eric Blake <[email protected]>Signed-off-by Kevin Wolf <[email protected]>--- block/mirror.c | 4 +++- tests/qemu-iotests/041 | 5 +++++ tests/qemu-iotests/041.out | 4 ++-- 3 files changed, 10 insertions(+), 3 deletions(-)
Quoted 8 lines
tests/qemu-iotests/041 class TestSingleDriveZeroLength(TestSingleDrive): · line 222
Quoted 6 lines
+++ b/tests/qemu-iotests/041
tests/qemu-iotests/041 +1
-0@@ -219,6 +219,11 @@ class TestSingleDriveZeroLength(TestSingleDrive): test_small_buffer2 = None test_large_cluster = None +class TestSingleDriverUnalignedLength(TestSingleDrive):s/Driver/Drive/ for consistency in the class name?
Yes, that was a typo. Thanks for catching it.
Kevin
Stefan Hajnoczi
AppliedRe: [Qemu-devel] [PATCH for-2.1 1/2] mirror: Fix qiov size for short requests
In reply to Kevin WolfQuoted 9 lines · Kevin Wolf
When mirroring an image of a size that is not a multiple of the mirror job granularity, the last request would have the right
nb_sectorsargument, but a qiov that is rounded up to the next multiple of the granularity. Don't do this.This fixes a segfault that is caused by raw-posix being confused by this and allocating a buffer with request length, but operating on it with qiov length.
Quoted 7 lines
Reported-by Eric Blake <[email protected]>Signed-off-by Kevin Wolf <[email protected]>--- block/mirror.c | 4 +++- tests/qemu-iotests/041 | 5 +++++ tests/qemu-iotests/041.out | 4 ++-- 3 files changed, 10 insertions(+), 3 deletions(-)
Applied Eric's suggestion.
Thanks, applied to my block tree:
https://github.com/stefanha/qemu/commits/block
Stefan
PATCH 2/2 block: Assert qiov length matches request length
In reply to Kevin WolfAt least raw-posix relies on this because it can allocate bounce buffers based on the request length, but access it using all of the qiov entries later.
--- block.c | 2 ++ block/raw-posix.c | 12 ++++++++---- 2 files changed, 10 insertions(+), 4 deletions(-)
block.c +2 -0
@@ -3021,6 +3021,7 @@ static int coroutine_fn bdrv_aligned_preadv(BlockDriverState *bs,assert ((offset & (BDRV_SECTOR_SIZE -1 )) ==0 );assert ((bytes & (BDRV_SECTOR_SIZE -1 )) ==0 ); +assert (bytes == qiov->size);/* Handle Copy on Read and associated serialisation */ if (flags &BDRV_REQ_COPY_ON_READ ) {
@@ -3278,6 +3279,7 @@ static int coroutine_fn bdrv_aligned_pwritev(BlockDriverState *bs,assert ((offset & (BDRV_SECTOR_SIZE -1 )) ==0 );assert ((bytes & (BDRV_SECTOR_SIZE -1 )) ==0 ); +assert (bytes == qiov->size); waited =wait_serialising_requests (req);assert (!waited || !req->serialising);
block/raw-posix.c +8 -4
@@ -990,12 +990,14 @@ static int paio_submit_co(BlockDriverState *bs, int fd, acb->aio_type = type; acb->aio_fildes = fd; + acb->aio_nbytes = nb_sectors *BDRV_SECTOR_SIZE ; + acb->aio_offset = sector_num *BDRV_SECTOR_SIZE ; +if (qiov) { acb->aio_iov = qiov->iov; acb->aio_niov = qiov->niov; +assert (qiov->size == acb->aio_nbytes); } - acb->aio_nbytes = nb_sectors *512 ; - acb->aio_offset = sector_num *512 ;trace_paio_submit_co (sector_num, nb_sectors, type); pool =aio_get_thread_pool (bdrv_get_aio_context (bs));
@@ -1013,12 +1015,14 @@ static BlockDriverAIOCB *paio_submit(BlockDriverState *bs, int fd, acb->aio_type = type; acb->aio_fildes = fd; + acb->aio_nbytes = nb_sectors *BDRV_SECTOR_SIZE ; + acb->aio_offset = sector_num *BDRV_SECTOR_SIZE ; +if (qiov) { acb->aio_iov = qiov->iov; acb->aio_niov = qiov->niov; +assert (qiov->size == acb->aio_nbytes); } - acb->aio_nbytes = nb_sectors *512 ; - acb->aio_offset = sector_num *512 ;trace_paio_submit (acb, opaque, sector_num, nb_sectors, type); pool =aio_get_thread_pool (bdrv_get_aio_context (bs));
Signature · 1 line
1.8.3.1
Re: [Qemu-devel] [PATCH for-2.1 2/2] block: Assert qiov length matches request length
Am 01.07.2014 um 16:52 hat Kevin Wolf geschrieben:
Quoted 5 lines
At least raw-posix relies on this because it can allocate bounce buffers based on the request length, but access it using all of the qiov entries later.
Signed-off-by Kevin Wolf <[email protected]>
Self-NACK, this breaks some test cases. Patch 1 is still valid.
Kevin