Skip to contentSkip to conversation

block: Fix copy-on-read crash with partial final cluster

PATCH In review Kevin Wolf 2 participants 4 messages Jul 6, 2018 – Jul 20, 2018 Block layer

lore
Share & tools
lore

Kevin Wolf

author
#0 lore raw

PATCH 1/1 block: Fix copy-on-read crash with partial final cluster

If the virtual disk size isn't aligned to full clusters, bdrv_co_do_copy_on_readv() may get pnum == 0 before having the full cluster completed, which will let it run into an assertion failure:

qemu-io: block/io.c:1203: bdrv_co_do_copy_on_readv: Assertion `skip_bytes < pnum' failed.

Check for EOF, assert that we read at least as much as the read request originally wanted to have (which is true at EOF because otherwise bdrv_check_byte_request() would already have returned an error) and return success early even though we couldn't copy the full cluster.

---
 block/io.c                 | 6 ++++++
 tests/qemu-iotests/197     | 9 +++++++++
 tests/qemu-iotests/197.out | 8 ++++++++
 3 files changed, 23 insertions(+)
block/io.c +6 -0
index 038449f81f..4c0831149c 100644
@@ -1200,6 +1200,12 @@ static int coroutine_fn bdrv_co_do_copy_on_readv(BdrvChild *child,
             pnum = MIN(cluster_bytes, max_transfer);
         }
 
+        /* Stop at EOF if the image ends in the middle of the cluster */
+        if (ret == 0 && pnum == 0) {
+            assert(progress >= bytes);
+            break;
+        }
+
         assert(skip_bytes < pnum);
 
         if (ret <= 0) {
tests/qemu-iotests/197 +9 -0
index 3ae4975eec..0369aa5cff 100755
@@ -109,6 +109,15 @@ $QEMU_IO -f qcow2 -c map "$TEST_WRAP"
 _check_test_img
 $QEMU_IMG compare -f $IMGFMT -F qcow2 "$TEST_IMG" "$TEST_WRAP"
 
+echo
+echo '=== Partial final cluster ==='
+echo
+
+_make_test_img 1024
+$QEMU_IO -f $IMGFMT -C -c 'read 0 1024' "$TEST_IMG" | _filter_qemu_io
+$QEMU_IO -f $IMGFMT -c map "$TEST_IMG"
+_check_test_img
+
 # success, all done
 echo '*** done'
 status=0
tests/qemu-iotests/197.out +8 -0
index 52b4137d7b..8febda5dea 100644
@@ -23,4 +23,12 @@ can't open device TEST_DIR/t.wrap.qcow2: Can't use copy-on-read on read-only dev
 1023.938 MiB (0x3fff0000) bytes not allocated at offset 3 GiB (0xc0010000)
 No errors were found on the image.
 Images are identical.
+
+=== Partial final cluster ===
+
+Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=1024
+read 1024/1024 bytes at offset 0
+1 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
+1 KiB (0x400) bytes     allocated at offset 0 bytes (0x0)
+No errors were found on the image.
 *** done
Signature · 1 line
2.13.6
Reviewed-by on v1 1/1
#1 lore raw
Quoted 23 lines · Kevin Wolf

If the virtual disk size isn't aligned to full clusters, bdrv_co_do_copy_on_readv() may get pnum == 0 before having the full cluster completed, which will let it run into an assertion failure:

qemu-io: block/io.c:1203: bdrv_co_do_copy_on_readv: Assertion `skip_bytes < pnum' failed.

Check for EOF, assert that we read at least as much as the read request originally wanted to have (which is true at EOF because otherwise bdrv_check_byte_request() would already have returned an error) and return success early even though we couldn't copy the full cluster.

---
  block/io.c                 | 6 ++++++
  tests/qemu-iotests/197     | 9 +++++++++
  tests/qemu-iotests/197.out | 8 ++++++++
  3 files changed, 23 insertions(+)
block/io.c +0 -0
index 038449f81f..4c0831149c 100644

block/io.c static int coroutine_fn bdrv_co_do_copy_on_readv(BdrvChild *child, · line 1204

Quoted 5 lines
block/io.c +2 -0
@@ -1200,6 +1200,12 @@ static int coroutine_fn bdrv_co_do_copy_on_readv(BdrvChild *child,
              pnum = MIN(cluster_bytes, max_transfer);
          }
 
+        /* Stop at EOF if the image ends in the middle of the cluster */
+        if (ret == 0 && pnum == 0) {

Is it any smarter to check for 'ret & BDRV_BLOCK_EOF' instead of pnum == 0?

But as far as I can tell, right now those two conditions are synonymous.

block/io.c static int coroutine_fn bdrv_co_do_copy_on_readv(BdrvChild *child, · line 1210

Quoted 7 lines
block/io.c +6 -0
+echo
+echo '=== Partial final cluster ==='
+echo
+
+_make_test_img 1024
+$QEMU_IO -f $IMGFMT -C -c 'read 0 1024' "$TEST_IMG" | _filter_qemu_io

Here, $TEST_IMG has no backing file, and does not have the final cluster allocated; all we have to do is properly read all zeroes. Is it worth also explicitly testing reading of allocated data under COR, and/or the case of one image with another backing image (with same or differing partial cluster sizes), where COR actually has to write the partial cluster? However, the logic in the code appears to cover all of those, whether or not the testsuite does as well.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
author
#2 lore raw

Am 06.07.2018 um 23:20 hat Eric Blake geschrieben:

Quoted 31 lines · Kevin Wolf
Quoted 23 lines

If the virtual disk size isn't aligned to full clusters, bdrv_co_do_copy_on_readv() may get pnum == 0 before having the full cluster completed, which will let it run into an assertion failure:

qemu-io: block/io.c:1203: bdrv_co_do_copy_on_readv: Assertion `skip_bytes < pnum' failed.

Check for EOF, assert that we read at least as much as the read request originally wanted to have (which is true at EOF because otherwise bdrv_check_byte_request() would already have returned an error) and return success early even though we couldn't copy the full cluster.

---
  block/io.c                 | 6 ++++++
  tests/qemu-iotests/197     | 9 +++++++++
  tests/qemu-iotests/197.out | 8 ++++++++
  3 files changed, 23 insertions(+)
block/io.c +0 -0
index 038449f81f..4c0831149c 100644

block/io.c static int coroutine_fn bdrv_co_do_copy_on_readv(BdrvChild *child, · line 1203

Quoted 4 lines
block/io.c +2 -0
@@ -1200,6 +1200,12 @@ static int coroutine_fn bdrv_co_do_copy_on_readv(BdrvChild *child,
              pnum = MIN(cluster_bytes, max_transfer);
          }
+        /* Stop at EOF if the image ends in the middle of the cluster */
+        if (ret == 0 && pnum == 0) {

Is it any smarter to check for 'ret & BDRV_BLOCK_EOF' instead of pnum == 0?

Quoted 1 line

But as far as I can tell, right now those two conditions are synonymous.

You removed the context from your quote, which contains the assertion that the commit message mentions:

    assert(skip_bytes < pnum);

So I think it makes more sense to check pnum, because it's the short pnum and not the BDRV_BLOCK_EOF flag that would make the following code fail.

Quoted 9 lines

block/io.c static int coroutine_fn bdrv_co_do_copy_on_readv(BdrvChild *child, · line 1209

Quoted 6 lines
block/io.c +6 -0
+echo
+echo '=== Partial final cluster ==='
+echo
+
+_make_test_img 1024
+$QEMU_IO -f $IMGFMT -C -c 'read 0 1024' "$TEST_IMG" | _filter_qemu_io

Here, $TEST_IMG has no backing file, and does not have the final cluster allocated; all we have to do is properly read all zeroes.

And write them back, we test that it's allocated afterwards.

Quoted 2 lines

Is it worth also explicitly testing reading of allocated data under COR

I could add a second read of the same area, which would not only test reading of allocated data, but also test whether the allocating COR wrote the correct data (all zeros).

Do you think that would be a worthwhile addition?

Quoted 4 lines

and/or the case of one image with another backing image (with same or differing partial cluster sizes), where COR actually has to write the partial cluster? However, the logic in the code appears to cover all of those, whether or not the testsuite does as well.

Having a backing file is a different code path for non-zero data, but I think we already test normal write/write_zeroes extensively. For zero data, it doesn't make a difference whether it comes from the backing file or from not having a backing file (as far as the COR logic is concerned, anyway).

I didn't want to use a backing file because the test is 'generic', but it already uses qcow2 in other cases, so if we really think this is important to have, I guess I can have another case with qcow2 over $IMGFMT and non-zero data.

Kevin

#3 lore raw
Quoted 26 lines · Kevin Wolf

Am 06.07.2018 um 23:20 hat Eric Blake geschrieben:

Quoted 13 lines · Kevin Wolf
Quoted 11 lines

If the virtual disk size isn't aligned to full clusters, bdrv_co_do_copy_on_readv() may get pnum == 0 before having the full cluster completed, which will let it run into an assertion failure:

qemu-io: block/io.c:1203: bdrv_co_do_copy_on_readv: Assertion `skip_bytes < pnum' failed.

Check for EOF, assert that we read at least as much as the read request originally wanted to have (which is true at EOF because otherwise bdrv_check_byte_request() would already have returned an error) and return success early even though we couldn't copy the full cluster.

Quoted 9 lines

tests/qemu-iotests/197 $QEMU_IO -f qcow2 -c map "$TEST_WRAP" · line 117

Quoted 6 lines
tests/qemu-iotests/197 +6 -0
+echo
+echo '=== Partial final cluster ==='
+echo
+
+_make_test_img 1024
+$QEMU_IO -f $IMGFMT -C -c 'read 0 1024' "$TEST_IMG" | _filter_qemu_io

Here, $TEST_IMG has no backing file, and does not have the final cluster allocated; all we have to do is properly read all zeroes.

And write them back, we test that it's allocated afterwards.

Quoted 8 lines
Quoted 2 lines

Is it worth also explicitly testing reading of allocated data under COR

I could add a second read of the same area, which would not only test reading of allocated data, but also test whether the allocating COR wrote the correct data (all zeros).

Do you think that would be a worthwhile addition?

It can't hurt (we've already proven that our iotests coverage is incomplete, and anything we can do to enhance it improves chances of it catching unintentional regressions).

Quoted 12 lines
Quoted 5 lines

and/or the case of one image with another backing image (with same or differing partial cluster sizes), where COR actually has to write the partial cluster? However, the logic in the code appears to cover all of those, whether or not the testsuite does as well.

Having a backing file is a different code path for non-zero data, but I think we already test normal write/write_zeroes extensively. For zero data, it doesn't make a difference whether it comes from the backing file or from not having a backing file (as far as the COR logic is concerned, anyway).

Quoted 4 lines

I didn't want to use a backing file because the test is 'generic', but it already uses qcow2 in other cases, so if we really think this is important to have, I guess I can have another case with qcow2 over $IMGFMT and non-zero data.

At this point, I'm not volunteering to write such test enhancements, and I don't think it's a show-stopper if 3.0 uses the test as currently in git.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org