Skip to contentSkip to conversation

Fix mirror segfault with unaligned size

PATCH Applied Kevin Wolf 3 participants 8 messages Jul 1, 2014 – Jul 2, 2014 Block layer

lore
Share & tools
lore

Kevin Wolf

author
#0 lore raw

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
author
#1 lore raw

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
index 6c3ee70..c7a655f 100644
@@ -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
index 2b1e8a0..3013a26 100755
@@ -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
index 42147c0..24093bc 100644
@@ -1,5 +1,5 @@
-..............................................
+......................................................
 ----------------------------------------------------------------------
-Ran 46 tests
+Ran 54 tests
 
 OK
Signature · 1 line
1.8.3.1
#2 lore raw

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_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(-)

+++ 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
Tested-by Reviewed-by
#3 lore raw

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_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(-)
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
author
#4 lore raw

Re: [Qemu-devel] [PATCH for-2.1 1/2] mirror: Fix qiov size for short requests

In reply to Eric Blake

Am 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_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(-)
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

#5 lore raw

Re: [Qemu-devel] [PATCH for-2.1 1/2] mirror: Fix qiov size for short requests

In reply to Kevin Wolf
Quoted 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_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.

Quoted 7 lines
---
 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

author
#6 lore raw

PATCH 2/2 block: Assert qiov length matches request length

In reply to Kevin Wolf

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.

---
 block.c           |  2 ++
 block/raw-posix.c | 12 ++++++++----
 2 files changed, 10 insertions(+), 4 deletions(-)
block.c +2 -0
index 362c85c..4358ed6 100644
@@ -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
index dacf4fb..924fe69 100644
@@ -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
author
#7 lore raw

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.

Self-NACK, this breaks some test cases. Patch 1 is still valid.

Kevin