qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [Qemu-devel] [PATCH 0/2] block: Fix unaligned zero write
@ 2015-03-23  4:46 Fam Zheng
  2015-03-23  4:46 ` [Qemu-devel] [PATCH 1/2] " Fam Zheng
  2015-03-23  4:46 ` [Qemu-devel] [PATCH 2/2] qemu-iotests: Test unaligned 4k " Fam Zheng
  0 siblings, 2 replies; 6+ messages in thread
From: Fam Zheng @ 2015-03-23  4:46 UTC (permalink / raw)
  To: qemu-devel; +Cc: Kevin Wolf, qemu-block, qemu-stable, Stefan Hajnoczi

This fixes a segfault when doing unaligned zero write to a image that is 4k
aligned.

Reproducer:

    $ (echo "open -o file.align=4k blkdebug::img"; echo "write -z 512 1024") | qemu-io



Fam Zheng (2):
  block: Fix unaligned zero write
  qemu-iotests: Test unaligned 4k zero write

 block.c                    | 51 ++++++++++++++++++++++++++++++++++++++--------
 tests/qemu-iotests/033     | 47 ++++++++++++++++++++++++++----------------
 tests/qemu-iotests/033.out | 26 +++++++++++++++++++++++
 3 files changed, 98 insertions(+), 26 deletions(-)

-- 
2.1.0

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Qemu-devel] [PATCH 1/2] block: Fix unaligned zero write
  2015-03-23  4:46 [Qemu-devel] [PATCH 0/2] block: Fix unaligned zero write Fam Zheng
@ 2015-03-23  4:46 ` Fam Zheng
  2015-03-23 14:14   ` Stefan Hajnoczi
  2015-03-23  4:46 ` [Qemu-devel] [PATCH 2/2] qemu-iotests: Test unaligned 4k " Fam Zheng
  1 sibling, 1 reply; 6+ messages in thread
From: Fam Zheng @ 2015-03-23  4:46 UTC (permalink / raw)
  To: qemu-devel; +Cc: Kevin Wolf, qemu-block, qemu-stable, Stefan Hajnoczi

If the zero write is not aligned, bdrv_co_do_pwritev will segfault
because of accessing to the NULL qiov passed in by bdrv_co_write_zeroes.
Fix this by allocating a local qiov in bdrv_co_do_pwritev if the request
is not aligned. (In this case the padding iovs are necessary anyway, so
it doesn't hurt.)

Also add a check at the end of bdrv_co_do_pwritev to clear the zero flag
if padding is involved.

Signed-off-by: Fam Zheng <famz@redhat.com>
---
 block.c | 51 ++++++++++++++++++++++++++++++++++++++++++---------
 1 file changed, 42 insertions(+), 9 deletions(-)

diff --git a/block.c b/block.c
index 210fd5f..469b509 100644
--- a/block.c
+++ b/block.c
@@ -3086,6 +3086,19 @@ out:
     return ret;
 }
 
+static inline uint64_t bdrv_get_align(BlockDriverState *bs)
+{
+    /* TODO Lift BDRV_SECTOR_SIZE restriction in BlockDriver interface */
+    return MAX(BDRV_SECTOR_SIZE, bs->request_alignment);
+}
+
+static inline bool bdrv_req_is_aligned(BlockDriverState *bs,
+                                       int64_t offset, size_t bytes)
+{
+    int64_t align = bdrv_get_align(bs);
+    return !(offset & (align - 1) || (bytes & (align - 1)));
+}
+
 /*
  * Handle a read request in coroutine context
  */
@@ -3096,8 +3109,7 @@ static int coroutine_fn bdrv_co_do_preadv(BlockDriverState *bs,
     BlockDriver *drv = bs->drv;
     BdrvTrackedRequest req;
 
-    /* TODO Lift BDRV_SECTOR_SIZE restriction in BlockDriver interface */
-    uint64_t align = MAX(BDRV_SECTOR_SIZE, bs->request_alignment);
+    uint64_t align = bdrv_get_align(bs);
     uint8_t *head_buf = NULL;
     uint8_t *tail_buf = NULL;
     QEMUIOVector local_qiov;
@@ -3337,8 +3349,7 @@ static int coroutine_fn bdrv_co_do_pwritev(BlockDriverState *bs,
     BdrvRequestFlags flags)
 {
     BdrvTrackedRequest req;
-    /* TODO Lift BDRV_SECTOR_SIZE restriction in BlockDriver interface */
-    uint64_t align = MAX(BDRV_SECTOR_SIZE, bs->request_alignment);
+    uint64_t align = bdrv_get_align(bs);
     uint8_t *head_buf = NULL;
     uint8_t *tail_buf = NULL;
     QEMUIOVector local_qiov;
@@ -3435,6 +3446,10 @@ static int coroutine_fn bdrv_co_do_pwritev(BlockDriverState *bs,
         bytes = ROUND_UP(bytes, align);
     }
 
+    if (use_local_qiov) {
+        /* Local buffer may have non-zero data. */
+        flags &= ~BDRV_REQ_ZERO_WRITE;
+    }
     ret = bdrv_aligned_pwritev(bs, &req, offset, bytes,
                                use_local_qiov ? &local_qiov : qiov,
                                flags);
@@ -3475,14 +3490,32 @@ int coroutine_fn bdrv_co_write_zeroes(BlockDriverState *bs,
                                       int64_t sector_num, int nb_sectors,
                                       BdrvRequestFlags flags)
 {
+    int ret;
+
     trace_bdrv_co_write_zeroes(bs, sector_num, nb_sectors, flags);
 
-    if (!(bs->open_flags & BDRV_O_UNMAP)) {
-        flags &= ~BDRV_REQ_MAY_UNMAP;
-    }
+    if (bdrv_req_is_aligned(bs, sector_num << BDRV_SECTOR_BITS,
+                            nb_sectors << BDRV_SECTOR_BITS)) {
+        ret = bdrv_co_do_writev(bs, sector_num, nb_sectors, NULL,
+                                BDRV_REQ_ZERO_WRITE | flags);
+    } else {
+        uint8_t *buf;
+        QEMUIOVector local_qiov;
+        size_t bytes = nb_sectors << BDRV_SECTOR_BITS;
+
+        buf = qemu_memalign(bdrv_opt_mem_align(bs), bytes);
+        memset(buf, 0, bytes);
+        qemu_iovec_init(&local_qiov, 1);
+        qemu_iovec_add(&local_qiov, buf, bytes);
+        if (!(bs->open_flags & BDRV_O_UNMAP)) {
+            flags &= ~BDRV_REQ_MAY_UNMAP;
+        }
 
-    return bdrv_co_do_writev(bs, sector_num, nb_sectors, NULL,
-                             BDRV_REQ_ZERO_WRITE | flags);
+        ret = bdrv_co_do_writev(bs, sector_num, nb_sectors, &local_qiov,
+                                BDRV_REQ_ZERO_WRITE | flags);
+        qemu_vfree(buf);
+    }
+    return ret;
 }
 
 /**
-- 
2.1.0

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [Qemu-devel] [PATCH 2/2] qemu-iotests: Test unaligned 4k zero write
  2015-03-23  4:46 [Qemu-devel] [PATCH 0/2] block: Fix unaligned zero write Fam Zheng
  2015-03-23  4:46 ` [Qemu-devel] [PATCH 1/2] " Fam Zheng
@ 2015-03-23  4:46 ` Fam Zheng
  1 sibling, 0 replies; 6+ messages in thread
From: Fam Zheng @ 2015-03-23  4:46 UTC (permalink / raw)
  To: qemu-devel; +Cc: Kevin Wolf, qemu-block, qemu-stable, Stefan Hajnoczi

Signed-off-by: Fam Zheng <famz@redhat.com>
---
 tests/qemu-iotests/033     | 47 +++++++++++++++++++++++++++++-----------------
 tests/qemu-iotests/033.out | 26 +++++++++++++++++++++++++
 2 files changed, 56 insertions(+), 17 deletions(-)

diff --git a/tests/qemu-iotests/033 b/tests/qemu-iotests/033
index ea3351c..4008f10 100755
--- a/tests/qemu-iotests/033
+++ b/tests/qemu-iotests/033
@@ -46,26 +46,39 @@ _supported_os Linux
 size=128M
 _make_test_img $size
 
-echo
-echo "== preparing image =="
-$QEMU_IO -c "write -P 0xa 0x200 0x400" "$TEST_IMG" | _filter_qemu_io
-$QEMU_IO -c "write -P 0xa 0x20000 0x600" "$TEST_IMG" | _filter_qemu_io
-$QEMU_IO -c "write -z 0x400 0x20000" "$TEST_IMG" | _filter_qemu_io
+do_test()
+{
+	local align=$1
+	local iocmd=$2
+	local img=$3
+	{
+		echo "open -o driver=$IMGFMT,file.align=$align blkdebug::$img"
+		echo $iocmd
+	} | $QEMU_IO
+}
+
+for align in 512 4k; do
+	echo
+	echo "== preparing image =="
+	do_test $align "write -P 0xa 0x200 0x400" "$TEST_IMG" | _filter_qemu_io
+	do_test $align "write -P 0xa 0x20000 0x600" "$TEST_IMG" | _filter_qemu_io
+	do_test $align "write -z 0x400 0x20000" "$TEST_IMG" | _filter_qemu_io
 
-echo
-echo "== verifying patterns (1) =="
-$QEMU_IO -c "read -P 0xa 0x200 0x200" "$TEST_IMG" | _filter_qemu_io
-$QEMU_IO -c "read -P 0x0 0x400 0x20000" "$TEST_IMG" | _filter_qemu_io
-$QEMU_IO -c "read -P 0xa 0x20400 0x200" "$TEST_IMG" | _filter_qemu_io
+	echo
+	echo "== verifying patterns (1) =="
+	do_test $align "read -P 0xa 0x200 0x200" "$TEST_IMG" | _filter_qemu_io
+	do_test $align "read -P 0x0 0x400 0x20000" "$TEST_IMG" | _filter_qemu_io
+	do_test $align "read -P 0xa 0x20400 0x200" "$TEST_IMG" | _filter_qemu_io
 
-echo
-echo "== rewriting zeroes =="
-$QEMU_IO -c "write -P 0xb 0x10000 0x10000" "$TEST_IMG" | _filter_qemu_io
-$QEMU_IO -c "write -z 0x10000 0x10000" "$TEST_IMG" | _filter_qemu_io
+	echo
+	echo "== rewriting zeroes =="
+	do_test $align "write -P 0xb 0x10000 0x10000" "$TEST_IMG" | _filter_qemu_io
+	do_test $align "write -z 0x10000 0x10000" "$TEST_IMG" | _filter_qemu_io
 
-echo
-echo "== verifying patterns (2) =="
-$QEMU_IO -c "read -P 0x0 0x400 0x20000" "$TEST_IMG" | _filter_qemu_io
+	echo
+	echo "== verifying patterns (2) =="
+	do_test $align "read -P 0x0 0x400 0x20000" "$TEST_IMG" | _filter_qemu_io
+done
 
 # success, all done
 echo "*** done"
diff --git a/tests/qemu-iotests/033.out b/tests/qemu-iotests/033.out
index 41475ee..305949f 100644
--- a/tests/qemu-iotests/033.out
+++ b/tests/qemu-iotests/033.out
@@ -26,4 +26,30 @@ wrote 65536/65536 bytes at offset 65536
 == verifying patterns (2) ==
 read 131072/131072 bytes at offset 1024
 128 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
+
+== preparing image ==
+wrote 1024/1024 bytes at offset 512
+1 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
+wrote 1536/1536 bytes at offset 131072
+1.500 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
+wrote 131072/131072 bytes at offset 1024
+128 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
+
+== verifying patterns (1) ==
+read 512/512 bytes at offset 512
+512 bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
+read 131072/131072 bytes at offset 1024
+128 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
+read 512/512 bytes at offset 132096
+512 bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
+
+== rewriting zeroes ==
+wrote 65536/65536 bytes at offset 65536
+64 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
+wrote 65536/65536 bytes at offset 65536
+64 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
+
+== verifying patterns (2) ==
+read 131072/131072 bytes at offset 1024
+128 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
 *** done
-- 
2.1.0

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [Qemu-devel] [PATCH 1/2] block: Fix unaligned zero write
  2015-03-23  4:46 ` [Qemu-devel] [PATCH 1/2] " Fam Zheng
@ 2015-03-23 14:14   ` Stefan Hajnoczi
  2015-03-23 14:35     ` [Qemu-devel] [Qemu-stable] " Peter Lieven
  2015-03-24  1:19     ` [Qemu-devel] " Fam Zheng
  0 siblings, 2 replies; 6+ messages in thread
From: Stefan Hajnoczi @ 2015-03-23 14:14 UTC (permalink / raw)
  To: Fam Zheng
  Cc: Kevin Wolf, Stefan Hajnoczi, qemu-devel, qemu-block, qemu-stable

[-- Attachment #1: Type: text/plain, Size: 1308 bytes --]

On Mon, Mar 23, 2015 at 12:46:09PM +0800, Fam Zheng wrote:
> @@ -3435,6 +3446,10 @@ static int coroutine_fn bdrv_co_do_pwritev(BlockDriverState *bs,
>          bytes = ROUND_UP(bytes, align);
>      }
>  
> +    if (use_local_qiov) {
> +        /* Local buffer may have non-zero data. */
> +        flags &= ~BDRV_REQ_ZERO_WRITE;
> +    }
>      ret = bdrv_aligned_pwritev(bs, &req, offset, bytes,
>                                 use_local_qiov ? &local_qiov : qiov,
>                                 flags);
> @@ -3475,14 +3490,32 @@ int coroutine_fn bdrv_co_write_zeroes(BlockDriverState *bs,
>                                        int64_t sector_num, int nb_sectors,
>                                        BdrvRequestFlags flags)
>  {
> +    int ret;
> +
>      trace_bdrv_co_write_zeroes(bs, sector_num, nb_sectors, flags);
>  
> -    if (!(bs->open_flags & BDRV_O_UNMAP)) {
> -        flags &= ~BDRV_REQ_MAY_UNMAP;
> -    }

Why is it okay to drop this when the request is aligned?

> +    if (bdrv_req_is_aligned(bs, sector_num << BDRV_SECTOR_BITS,
> +                            nb_sectors << BDRV_SECTOR_BITS)) {
> +        ret = bdrv_co_do_writev(bs, sector_num, nb_sectors, NULL,
> +                                BDRV_REQ_ZERO_WRITE | flags);
> +    } else {

[-- Attachment #2: Type: application/pgp-signature, Size: 473 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Qemu-devel] [Qemu-stable] [PATCH 1/2] block: Fix unaligned zero write
  2015-03-23 14:14   ` Stefan Hajnoczi
@ 2015-03-23 14:35     ` Peter Lieven
  2015-03-24  1:19     ` [Qemu-devel] " Fam Zheng
  1 sibling, 0 replies; 6+ messages in thread
From: Peter Lieven @ 2015-03-23 14:35 UTC (permalink / raw)
  To: Stefan Hajnoczi, Fam Zheng
  Cc: Kevin Wolf, qemu-block, qemu-devel, Stefan Hajnoczi, qemu-stable

Am 23.03.2015 um 15:14 schrieb Stefan Hajnoczi:
> On Mon, Mar 23, 2015 at 12:46:09PM +0800, Fam Zheng wrote:
>> @@ -3435,6 +3446,10 @@ static int coroutine_fn bdrv_co_do_pwritev(BlockDriverState *bs,
>>           bytes = ROUND_UP(bytes, align);
>>       }
>>   
>> +    if (use_local_qiov) {
>> +        /* Local buffer may have non-zero data. */
>> +        flags &= ~BDRV_REQ_ZERO_WRITE;
>> +    }
>>       ret = bdrv_aligned_pwritev(bs, &req, offset, bytes,
>>                                  use_local_qiov ? &local_qiov : qiov,
>>                                  flags);
>> @@ -3475,14 +3490,32 @@ int coroutine_fn bdrv_co_write_zeroes(BlockDriverState *bs,
>>                                         int64_t sector_num, int nb_sectors,
>>                                         BdrvRequestFlags flags)
>>   {
>> +    int ret;
>> +
>>       trace_bdrv_co_write_zeroes(bs, sector_num, nb_sectors, flags);
>>   
>> -    if (!(bs->open_flags & BDRV_O_UNMAP)) {
>> -        flags &= ~BDRV_REQ_MAY_UNMAP;
>> -    }
> Why is it okay to drop this when the request is aligned?

I also think it is not. This is the only point in the code that clears the
MAY_UNMAP flag when we do not open with discard=on

Peter

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Qemu-devel] [PATCH 1/2] block: Fix unaligned zero write
  2015-03-23 14:14   ` Stefan Hajnoczi
  2015-03-23 14:35     ` [Qemu-devel] [Qemu-stable] " Peter Lieven
@ 2015-03-24  1:19     ` Fam Zheng
  1 sibling, 0 replies; 6+ messages in thread
From: Fam Zheng @ 2015-03-24  1:19 UTC (permalink / raw)
  To: Stefan Hajnoczi
  Cc: Kevin Wolf, Stefan Hajnoczi, qemu-devel, qemu-block, qemu-stable

On Mon, 03/23 14:14, Stefan Hajnoczi wrote:
> On Mon, Mar 23, 2015 at 12:46:09PM +0800, Fam Zheng wrote:
> > @@ -3435,6 +3446,10 @@ static int coroutine_fn bdrv_co_do_pwritev(BlockDriverState *bs,
> >          bytes = ROUND_UP(bytes, align);
> >      }
> >  
> > +    if (use_local_qiov) {
> > +        /* Local buffer may have non-zero data. */
> > +        flags &= ~BDRV_REQ_ZERO_WRITE;
> > +    }
> >      ret = bdrv_aligned_pwritev(bs, &req, offset, bytes,
> >                                 use_local_qiov ? &local_qiov : qiov,
> >                                 flags);
> > @@ -3475,14 +3490,32 @@ int coroutine_fn bdrv_co_write_zeroes(BlockDriverState *bs,
> >                                        int64_t sector_num, int nb_sectors,
> >                                        BdrvRequestFlags flags)
> >  {
> > +    int ret;
> > +
> >      trace_bdrv_co_write_zeroes(bs, sector_num, nb_sectors, flags);
> >  
> > -    if (!(bs->open_flags & BDRV_O_UNMAP)) {
> > -        flags &= ~BDRV_REQ_MAY_UNMAP;
> > -    }
> 
> Why is it okay to drop this when the request is aligned?

It's not. I will restore it.

> 
> > +    if (bdrv_req_is_aligned(bs, sector_num << BDRV_SECTOR_BITS,
> > +                            nb_sectors << BDRV_SECTOR_BITS)) {
> > +        ret = bdrv_co_do_writev(bs, sector_num, nb_sectors, NULL,
> > +                                BDRV_REQ_ZERO_WRITE | flags);
> > +    } else {

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2015-03-24  1:19 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-03-23  4:46 [Qemu-devel] [PATCH 0/2] block: Fix unaligned zero write Fam Zheng
2015-03-23  4:46 ` [Qemu-devel] [PATCH 1/2] " Fam Zheng
2015-03-23 14:14   ` Stefan Hajnoczi
2015-03-23 14:35     ` [Qemu-devel] [Qemu-stable] " Peter Lieven
2015-03-24  1:19     ` [Qemu-devel] " Fam Zheng
2015-03-23  4:46 ` [Qemu-devel] [PATCH 2/2] qemu-iotests: Test unaligned 4k " Fam Zheng

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).