Skip to contentSkip to conversation

block/parallels: Fix buffer-based write call

PATCH Applied Hanna Reitz 3 participants 8 messages Jul 14, 2022 – Jul 26, 2022 Block layer

lore
Share & tools
lore
#0 lore raw

cover block/parallels: Fix buffer-based write call

Hi,

While reviewing Stefan’s libblkio driver series, I’ve noticed that block/parallels.c contains a call to bdrv_co_pwritev() that doesn’t pass a QEMUIOVector object but a plain buffer instead. That seems wrong and also pretty dangerous, so change it to a bdrv_co_pwrite() call (as I assume it should be), and add a regression test demonstrating the problem.

Hanna Reitz (2):
block/parallels: Fix buffer-based write call
iotests/131: Add parallels regression test

 block/parallels.c          |  4 ++--
 tests/qemu-iotests/131     | 35 ++++++++++++++++++++++++++++++++++-
 tests/qemu-iotests/131.out | 13 +++++++++++++
 3 files changed, 49 insertions(+), 3 deletions(-)
Signature · 1 line
2.35.3
#1 lore raw

PATCH 1/2 block/parallels: Fix buffer-based write call

Commit a4072543ccdddbd241d5962d9237b8b41fd006bf has changed the I/O here from working on a local one-element I/O vector to just using the buffer directly (using the bdrv_co_pread()/bdrv_co_pwrite() helper functions introduced shortly before).

However, it only changed the bdrv_co_preadv() call to bdrv_co_pread() - the subsequent bdrv_co_pwritev() call stayed this way, and so still expects a QEMUIOVector pointer instead of a plain buffer. We must change that to be a bdrv_co_pwrite() call.

Fixes a4072543ccdddbd241d5962d ("block/parallels: use buffer-based io")
---
 block/parallels.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
block/parallels.c +2 -2
index 8b235b9505..a229c06f25 100644
@@ -241,8 +241,8 @@ static int64_t allocate_clusters(BlockDriverState *bs, int64_t sector_num,
             return ret;
         }
 
-        ret = bdrv_co_pwritev(bs->file, s->data_end * BDRV_SECTOR_SIZE,
-                              nb_cow_bytes, buf, 0);
+        ret = bdrv_co_pwrite(bs->file, s->data_end * BDRV_SECTOR_SIZE,
+                             nb_cow_bytes, buf, 0);
         qemu_vfree(buf);
         if (ret < 0) {
             return ret;
Signature · 1 line
2.35.3
Reviewed-by on v1 1/2
#2 lore raw
Quoted 21 lines · Hanna Reitz

Commit a4072543ccdddbd241d5962d9237b8b41fd006bf has changed the I/O here from working on a local one-element I/O vector to just using the buffer directly (using the bdrv_co_pread()/bdrv_co_pwrite() helper functions introduced shortly before).

However, it only changed the bdrv_co_preadv() call to bdrv_co_pread() - the subsequent bdrv_co_pwritev() call stayed this way, and so still expects a QEMUIOVector pointer instead of a plain buffer. We must change that to be a bdrv_co_pwrite() call.

Fixes a4072543ccdddbd241d5962d ("block/parallels: use buffer-based io")
---
  block/parallels.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
block/parallels.c +0 -0
index 8b235b9505..a229c06f25 100644

block/parallels.c static int64_t allocate_clusters(BlockDriverState *bs, int64_t sector_num, · line 248

Quoted 10 lines
block/parallels.c +2 -2
@@ -241,8 +241,8 @@ static int64_t allocate_clusters(BlockDriverState *bs, int64_t sector_num,
              return ret;
          }
 
-        ret = bdrv_co_pwritev(bs->file, s->data_end * BDRV_SECTOR_SIZE,
-                              nb_cow_bytes, buf, 0);
+        ret = bdrv_co_pwrite(bs->file, s->data_end * BDRV_SECTOR_SIZE,
+                             nb_cow_bytes, buf, 0);
          qemu_vfree(buf);
          if (ret < 0) {
              return ret;
Reviewed-by on v1 1/2
#3 lore raw
In reply to Hanna Reitz
Quoted 5 lines · Hanna Reitz

Commit a4072543ccdddbd241d5962d9237b8b41fd006bf has changed the I/O here from working on a local one-element I/O vector to just using the buffer directly (using the bdrv_co_pread()/bdrv_co_pwrite() helper functions introduced shortly before).

Quoted 7 lines

However, it only changed the bdrv_co_preadv() call to bdrv_co_pread() - the subsequent bdrv_co_pwritev() call stayed this way, and so still expects a QEMUIOVector pointer instead of a plain buffer. We must change that to be a bdrv_co_pwrite() call.

Fixes a4072543ccdddbd241d5962d ("block/parallels: use buffer-based io")
Signature · 2 lines
Best regards,
Vladimir
#4 lore raw

PATCH 2/2 iotests/131: Add parallels regression test

In reply to Hanna Reitz

Test an allocating write to a parallels image that has a backing node. Before HEAD^, doing so used to give me a failed assertion (when the backing node contains only 42 bytes; the results varies with the value chosen, for 0 bytes, for example, all I get is EIO).

---
 tests/qemu-iotests/131     | 35 ++++++++++++++++++++++++++++++++++-
 tests/qemu-iotests/131.out | 13 +++++++++++++
 2 files changed, 47 insertions(+), 1 deletion(-)
tests/qemu-iotests/131 +34 -1
index d7456cae5b..a847692b4c 100755
@@ -43,7 +43,7 @@ _supported_os Linux
 
 inuse_offset=$((0x2c))
 
-size=64M
+size=$((64 * 1024 * 1024))
 CLUSTER_SIZE=64k
 IMGFMT=parallels
 _make_test_img $size
@@ -70,6 +70,39 @@ _check_test_img
 _check_test_img -r all
 { $QEMU_IO -c "read -P 0x11 64k 64k" "$TEST_IMG"; } 2>&1 | _filter_qemu_io | _filter_testdir
 
+echo "== allocate with backing =="
+# Verify that allocating clusters works fine even when there is a backing image.
+# Regression test for a bug where we would pass a buffer read from the backing
+# node as a QEMUIOVector object, which could cause anything from I/O errors over
+# assertion failures to invalid reads from memory.
+
+# Clear image
+_make_test_img $size
+# Create base image
+TEST_IMG="$TEST_IMG.base" _make_test_img $size
+
+# Write some data to the base image (which would trigger an assertion failure if
+# interpreted as a QEMUIOVector)
+$QEMU_IO -c 'write -P 42 0 64k' "$TEST_IMG.base" | _filter_qemu_io
+
+# Parallels does not seem to support storing a backing filename in the image
+# itself, so we need to build our backing chain on the command line
+imgopts="driver=$IMGFMT,file.driver=$IMGPROTO,file.filename=$TEST_IMG"
+imgopts+=",backing.driver=$IMGFMT"
+imgopts+=",backing.file.driver=$IMGPROTO,backing.file.filename=$TEST_IMG.base"
+
+# Cause allocation in the top image
+QEMU_IO_OPTIONS=$QEMU_IO_OPTIONS_NO_FMT \
+    $QEMU_IO --image-opts "$imgopts" -c 'write -P 1 0 64' | _filter_qemu_io
+
+# Verify
+QEMU_IO_OPTIONS=$QEMU_IO_OPTIONS_NO_FMT \
+    $QEMU_IO --image-opts "$imgopts" \
+    -c 'read -P 1 0 64' \
+    -c "read -P 42 64 $((64 * 1024 - 64))" \
+    -c "read -P 0 64k $((size - 64 * 1024))" \
+    | _filter_qemu_io
+
 # success, all done
 echo "*** done"
 rm -f $seq.full
tests/qemu-iotests/131.out +13 -0
index 70da03dee5..de5ef7a8f5 100644
@@ -37,4 +37,17 @@ Double checking the fixed image now...
 No errors were found on the image.
 read 65536/65536 bytes at offset 65536
 64 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
+== allocate with backing ==
+Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=67108864
+Formatting 'TEST_DIR/t.IMGFMT.base', fmt=IMGFMT size=67108864
+wrote 65536/65536 bytes at offset 0
+64 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
+wrote 64/64 bytes at offset 0
+64 bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
+read 64/64 bytes at offset 0
+64 bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
+read 65472/65472 bytes at offset 64
+63.938 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
+read 67043328/67043328 bytes at offset 65536
+63.938 MiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
 *** done
Signature · 1 line
2.35.3
Reviewed-by on v1 2/2
#5 lore raw

Re: [PATCH 2/2] iotests/131: Add parallels regression test

Quoted 76 lines · Hanna Reitz

Test an allocating write to a parallels image that has a backing node. Before HEAD^, doing so used to give me a failed assertion (when the backing node contains only 42 bytes; the results varies with the value chosen, for 0 bytes, for example, all I get is EIO).

---
  tests/qemu-iotests/131     | 35 ++++++++++++++++++++++++++++++++++-
  tests/qemu-iotests/131.out | 13 +++++++++++++
  2 files changed, 47 insertions(+), 1 deletion(-)
tests/qemu-iotests/131 +34 -1
index d7456cae5b..a847692b4c 100755
@@ -43,7 +43,7 @@ _supported_os Linux
 
  inuse_offset=$((0x2c))
 
-size=64M
+size=$((64 * 1024 * 1024))
  CLUSTER_SIZE=64k
  IMGFMT=parallels
  _make_test_img $size
@@ -70,6 +70,39 @@ _check_test_img
  _check_test_img -r all
  { $QEMU_IO -c "read -P 0x11 64k 64k" "$TEST_IMG"; } 2>&1 | _filter_qemu_io | _filter_testdir
 
+echo "== allocate with backing =="
+# Verify that allocating clusters works fine even when there is a backing image.
+# Regression test for a bug where we would pass a buffer read from the backing
+# node as a QEMUIOVector object, which could cause anything from I/O errors over
+# assertion failures to invalid reads from memory.
+
+# Clear image
+_make_test_img $size
+# Create base image
+TEST_IMG="$TEST_IMG.base" _make_test_img $size
+
+# Write some data to the base image (which would trigger an assertion failure if
+# interpreted as a QEMUIOVector)
+$QEMU_IO -c 'write -P 42 0 64k' "$TEST_IMG.base" | _filter_qemu_io
+
+# Parallels does not seem to support storing a backing filename in the image
+# itself, so we need to build our backing chain on the command line
+imgopts="driver=$IMGFMT,file.driver=$IMGPROTO,file.filename=$TEST_IMG"
+imgopts+=",backing.driver=$IMGFMT"
+imgopts+=",backing.file.driver=$IMGPROTO,backing.file.filename=$TEST_IMG.base"
+
+# Cause allocation in the top image
+QEMU_IO_OPTIONS=$QEMU_IO_OPTIONS_NO_FMT \
+    $QEMU_IO --image-opts "$imgopts" -c 'write -P 1 0 64' | _filter_qemu_io
+
+# Verify
+QEMU_IO_OPTIONS=$QEMU_IO_OPTIONS_NO_FMT \
+    $QEMU_IO --image-opts "$imgopts" \
+    -c 'read -P 1 0 64' \
+    -c "read -P 42 64 $((64 * 1024 - 64))" \
+    -c "read -P 0 64k $((size - 64 * 1024))" \
+    | _filter_qemu_io
+
  # success, all done
  echo "*** done"
  rm -f $seq.full
tests/qemu-iotests/131.out +4 -0
index 70da03dee5..de5ef7a8f5 100644
@@ -37,4 +37,17 @@ Double checking the fixed image now...
  No errors were found on the image.
  read 65536/65536 bytes at offset 65536
  64 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
+== allocate with backing ==
+Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=67108864
+Formatting 'TEST_DIR/t.IMGFMT.base', fmt=IMGFMT size=67108864
+wrote 65536/65536 bytes at offset 0

tests/qemu-iotests/131.out Double checking the fixed image now... · line 53

Quoted 10 lines
tests/qemu-iotests/131.out +9 -0
+64 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
+wrote 64/64 bytes at offset 0
+64 bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
+read 64/64 bytes at offset 0
+64 bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
+read 65472/65472 bytes at offset 64
+63.938 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
+read 67043328/67043328 bytes at offset 65536
+63.938 MiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
  *** done
Reviewed-by on v1 2/2
#6 lore raw

Re: [PATCH 2/2] iotests/131: Add parallels regression test

In reply to Hanna Reitz
Quoted 6 lines · Hanna Reitz

Test an allocating write to a parallels image that has a backing node. Before HEAD^, doing so used to give me a failed assertion (when the backing node contains only 42 bytes; the results varies with the value chosen, for 0 bytes, for example, all I get is EIO).

Signature · 2 lines
Best regards,
Vladimir
#7 lore raw
In reply to Hanna Reitz
Quoted 10 lines · Hanna Reitz

Hi,

While reviewing Stefan’s libblkio driver series, I’ve noticed that block/parallels.c contains a call to bdrv_co_pwritev() that doesn’t pass a QEMUIOVector object but a plain buffer instead. That seems wrong and also pretty dangerous, so change it to a bdrv_co_pwrite() call (as I assume it should be), and add a regression test demonstrating the problem.

Quoted 9 lines

Hanna Reitz (2):
block/parallels: Fix buffer-based write call
iotests/131: Add parallels regression test

  block/parallels.c          |  4 ++--
  tests/qemu-iotests/131     | 35 ++++++++++++++++++++++++++++++++++-
  tests/qemu-iotests/131.out | 13 +++++++++++++
  3 files changed, 49 insertions(+), 3 deletions(-)

Thanks, applied to my block branch.

Signature · 2 lines
Best regards,
Vladimir