Skip to contentSkip to conversation

virtio-blk: Suppress error action on r/w beyond end

PATCH Superseded Markus Armbruster 5 participants 18 messages Jun 5, 2014 – Jul 1, 2014

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

cover virtio-blk: Suppress error action on r/w beyond end

When a device model's I/O operation fails, we execute the error action. This lets layers above QEMU implement thin provisioning, or attempt to correct errors before they reach the guest. But when the I/O operation fails because its invalid, reporting the error to the guest is the only sensible action.

This short series does exactly that for virtio-blk. I intend to do the same for IDE and SCSI.

Markus Armbruster (3):
virtio-blk: Factor common checks out of virtio_blk_handle_read/write()
virtio-blk: Bypass error action and I/O accounting on invalid r/w
virtio-blk: Treat read/write beyond end as invalid

 hw/block/virtio-blk.c | 45 +++++++++++++++++++++++++++++----------------
 1 file changed, 29 insertions(+), 16 deletions(-)
Signature · 1 line
1.9.3
#1 lore raw

PATCH 1/3 virtio-blk: Factor common checks out of virtio_blk_handle_read/write()

---
 hw/block/virtio-blk.c | 24 ++++++++++++++----------
 1 file changed, 14 insertions(+), 10 deletions(-)
hw/block/virtio-blk.c +14 -10
index 8a568e5..f2b4dca 100644
@@ -281,6 +281,18 @@ static void virtio_blk_handle_flush(VirtIOBlockReq *req, MultiReqBuffer *mrb)
     bdrv_aio_flush(req->dev->bs, virtio_blk_flush_complete, req);
 }
 
+static bool virtio_blk_sect_range_ok(VirtIOBlock *dev,
+                                     uint64_t sector, size_t size)
+{
+    if (sector & dev->sector_mask) {
+        return false;
+    }
+    if (size % dev->conf->logical_block_size) {
+        return false;
+    }
+    return true;
+}
+
 static void virtio_blk_handle_write(VirtIOBlockReq *req, MultiReqBuffer *mrb)
 {
     BlockRequest *blkreq;
@@ -292,11 +304,7 @@ static void virtio_blk_handle_write(VirtIOBlockReq *req, MultiReqBuffer *mrb)
 
     trace_virtio_blk_handle_write(req, sector, req->qiov.size / 512);
 
-    if (sector & req->dev->sector_mask) {
-        virtio_blk_rw_complete(req, -EIO);
-        return;
-    }
-    if (req->qiov.size % req->dev->conf->logical_block_size) {
+    if (!virtio_blk_sect_range_ok(req->dev, sector, req->qiov.size)) {
         virtio_blk_rw_complete(req, -EIO);
         return;
     }
@@ -326,11 +334,7 @@ static void virtio_blk_handle_read(VirtIOBlockReq *req)
 
     trace_virtio_blk_handle_read(req, sector, req->qiov.size / 512);
 
-    if (sector & req->dev->sector_mask) {
-        virtio_blk_rw_complete(req, -EIO);
-        return;
-    }
-    if (req->qiov.size % req->dev->conf->logical_block_size) {
+    if (!virtio_blk_sect_range_ok(req->dev, sector, req->qiov.size)) {
         virtio_blk_rw_complete(req, -EIO);
         return;
     }
Signature · 1 line
1.9.3
#3 lore raw

PATCH 2/3 virtio-blk: Bypass error action and I/O accounting on invalid r/w

In reply to Markus Armbruster

When a device model's I/O operation fails, we execute the error action. This lets layers above QEMU implement thin provisioning, or attempt to correct errors before they reach the guest. But when the I/O operation fails because its invalid, reporting the error to the guest is the only sensible action.

If the guest's read or write asks for an invalid sector range, fail the request right away, without considering the error action. No change with error action BDRV_ACTION_REPORT.

Furthermore, bypass I/O accounting, because we want to track only I/O that actually reaches the block layer.

The next commit will extend "invalid sector range" to cover attempts to read/write beyond the end of the medium.

---
 hw/block/virtio-blk.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
hw/block/virtio-blk.c +8 -6
index f2b4dca..2c68d0d 100644
@@ -300,15 +300,16 @@ static void virtio_blk_handle_write(VirtIOBlockReq *req, MultiReqBuffer *mrb)
 
     sector = ldq_p(&req->out->sector);
 
-    bdrv_acct_start(req->dev->bs, &req->acct, req->qiov.size, BDRV_ACCT_WRITE);
-
     trace_virtio_blk_handle_write(req, sector, req->qiov.size / 512);
 
     if (!virtio_blk_sect_range_ok(req->dev, sector, req->qiov.size)) {
-        virtio_blk_rw_complete(req, -EIO);
+        virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR);
+        g_free(req);
         return;
     }
 
+    bdrv_acct_start(req->dev->bs, &req->acct, req->qiov.size, BDRV_ACCT_WRITE);
+
     if (mrb->num_writes == 32) {
         virtio_submit_multiwrite(req->dev->bs, mrb);
     }
@@ -330,14 +331,15 @@ static void virtio_blk_handle_read(VirtIOBlockReq *req)
 
     sector = ldq_p(&req->out->sector);
 
-    bdrv_acct_start(req->dev->bs, &req->acct, req->qiov.size, BDRV_ACCT_READ);
-
     trace_virtio_blk_handle_read(req, sector, req->qiov.size / 512);
 
     if (!virtio_blk_sect_range_ok(req->dev, sector, req->qiov.size)) {
-        virtio_blk_rw_complete(req, -EIO);
+        virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR);
+        g_free(req);
         return;
     }
+
+    bdrv_acct_start(req->dev->bs, &req->acct, req->qiov.size, BDRV_ACCT_READ);
     bdrv_aio_readv(req->dev->bs, sector, &req->qiov,
                    req->qiov.size / BDRV_SECTOR_SIZE,
                    virtio_blk_rw_complete, req);
Signature · 1 line
1.9.3
#7 lore raw

PATCH 3/3 virtio-blk: Treat read/write beyond end as invalid

In reply to Markus Armbruster

The block layer fails such reads and writes just fine. However, they then get treated like valid operations that fail: the error action gets executed. Unwanted; reporting the error to the guest is the only sensible action.

Reject them before passing them to the block layer. This bypasses the error action and I/O accounting.

---
 hw/block/virtio-blk.c | 7 +++++++
 1 file changed, 7 insertions(+)
hw/block/virtio-blk.c +7 -0
index 2c68d0d..0b38049 100644
@@ -284,12 +284,19 @@ static void virtio_blk_handle_flush(VirtIOBlockReq *req, MultiReqBuffer *mrb)
 static bool virtio_blk_sect_range_ok(VirtIOBlock *dev,
                                      uint64_t sector, size_t size)
 {
+    uint64_t nb_sectors = size >> BDRV_SECTOR_BITS;
+    uint64_t total_sectors;
+
     if (sector & dev->sector_mask) {
         return false;
     }
     if (size % dev->conf->logical_block_size) {
         return false;
     }
+    bdrv_get_geometry(dev->bs, &total_sectors);
+    if (sector > total_sectors || nb_sectors > total_sectors - sector) {
+        return false;
+    }
     return true;
 }
 
Signature · 1 line
1.9.3
Reviewed-by on v1 1/3
#2 lore raw

Re: [Qemu-devel] [PATCH 1/3] virtio-blk: Factor common checks out of virtio_blk_handle_read/write()

In reply to Markus Armbruster
Quoted 58 lines · Markus Armbruster
---
 hw/block/virtio-blk.c | 24 ++++++++++++++----------
 1 file changed, 14 insertions(+), 10 deletions(-)
hw/block/virtio-blk.c +14 -10
index 8a568e5..f2b4dca 100644
@@ -281,6 +281,18 @@ static void virtio_blk_handle_flush(VirtIOBlockReq *req, MultiReqBuffer *mrb)
     bdrv_aio_flush(req->dev->bs, virtio_blk_flush_complete, req);
 }
 
+static bool virtio_blk_sect_range_ok(VirtIOBlock *dev,
+                                     uint64_t sector, size_t size)
+{
+    if (sector & dev->sector_mask) {
+        return false;
+    }
+    if (size % dev->conf->logical_block_size) {
+        return false;
+    }
+    return true;
+}
+
 static void virtio_blk_handle_write(VirtIOBlockReq *req, MultiReqBuffer *mrb)
 {
     BlockRequest *blkreq;
@@ -292,11 +304,7 @@ static void virtio_blk_handle_write(VirtIOBlockReq *req, MultiReqBuffer *mrb)
 
     trace_virtio_blk_handle_write(req, sector, req->qiov.size / 512);
 
-    if (sector & req->dev->sector_mask) {
-        virtio_blk_rw_complete(req, -EIO);
-        return;
-    }
-    if (req->qiov.size % req->dev->conf->logical_block_size) {
+    if (!virtio_blk_sect_range_ok(req->dev, sector, req->qiov.size)) {
         virtio_blk_rw_complete(req, -EIO);
         return;
     }
@@ -326,11 +334,7 @@ static void virtio_blk_handle_read(VirtIOBlockReq *req)
 
     trace_virtio_blk_handle_read(req, sector, req->qiov.size / 512);
 
-    if (sector & req->dev->sector_mask) {
-        virtio_blk_rw_complete(req, -EIO);
-        return;
-    }
-    if (req->qiov.size % req->dev->conf->logical_block_size) {
+    if (!virtio_blk_sect_range_ok(req->dev, sector, req->qiov.size)) {
         virtio_blk_rw_complete(req, -EIO);
         return;
     }
Signature · 1 line
1.9.3
Reviewed-by on v1 2/3
#4 lore raw

Re: [Qemu-devel] [PATCH 2/3] virtio-blk: Bypass error action and I/O accounting on invalid r/w

In reply to Markus Armbruster
Quoted 4 lines · Markus Armbruster

When a device model's I/O operation fails, we execute the error action. This lets layers above QEMU implement thin provisioning, or attempt to correct errors before they reach the guest. But when the I/O operation fails because its invalid, reporting the error to the

s/its/it's/ ?

Anyway,

Quoted 64 lines

guest is the only sensible action.

If the guest's read or write asks for an invalid sector range, fail the request right away, without considering the error action. No change with error action BDRV_ACTION_REPORT.

Furthermore, bypass I/O accounting, because we want to track only I/O that actually reaches the block layer.

The next commit will extend "invalid sector range" to cover attempts to read/write beyond the end of the medium.

---
 hw/block/virtio-blk.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
hw/block/virtio-blk.c +8 -6
index f2b4dca..2c68d0d 100644
@@ -300,15 +300,16 @@ static void virtio_blk_handle_write(VirtIOBlockReq *req, MultiReqBuffer *mrb)
 
     sector = ldq_p(&req->out->sector);
 
-    bdrv_acct_start(req->dev->bs, &req->acct, req->qiov.size, BDRV_ACCT_WRITE);
-
     trace_virtio_blk_handle_write(req, sector, req->qiov.size / 512);
 
     if (!virtio_blk_sect_range_ok(req->dev, sector, req->qiov.size)) {
-        virtio_blk_rw_complete(req, -EIO);
+        virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR);
+        g_free(req);
         return;
     }
 
+    bdrv_acct_start(req->dev->bs, &req->acct, req->qiov.size, BDRV_ACCT_WRITE);
+
     if (mrb->num_writes == 32) {
         virtio_submit_multiwrite(req->dev->bs, mrb);
     }
@@ -330,14 +331,15 @@ static void virtio_blk_handle_read(VirtIOBlockReq *req)
 
     sector = ldq_p(&req->out->sector);
 
-    bdrv_acct_start(req->dev->bs, &req->acct, req->qiov.size, BDRV_ACCT_READ);
-
     trace_virtio_blk_handle_read(req, sector, req->qiov.size / 512);
 
     if (!virtio_blk_sect_range_ok(req->dev, sector, req->qiov.size)) {
-        virtio_blk_rw_complete(req, -EIO);
+        virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR);
+        g_free(req);
         return;
     }
+
+    bdrv_acct_start(req->dev->bs, &req->acct, req->qiov.size, BDRV_ACCT_READ);
     bdrv_aio_readv(req->dev->bs, sector, &req->qiov,
                    req->qiov.size / BDRV_SECTOR_SIZE,
                    virtio_blk_rw_complete, req);
Signature · 1 line
1.9.3
Reviewed-by on v1 3/3
#8 lore raw

Re: [Qemu-devel] [PATCH 3/3] virtio-blk: Treat read/write beyond end as invalid

In reply to Markus Armbruster
Quoted 41 lines · Markus Armbruster

The block layer fails such reads and writes just fine. However, they then get treated like valid operations that fail: the error action gets executed. Unwanted; reporting the error to the guest is the only sensible action.

Reject them before passing them to the block layer. This bypasses the error action and I/O accounting.

---
 hw/block/virtio-blk.c | 7 +++++++
 1 file changed, 7 insertions(+)
hw/block/virtio-blk.c +7 -0
index 2c68d0d..0b38049 100644
@@ -284,12 +284,19 @@ static void virtio_blk_handle_flush(VirtIOBlockReq *req, MultiReqBuffer *mrb)
 static bool virtio_blk_sect_range_ok(VirtIOBlock *dev,
                                      uint64_t sector, size_t size)
 {
+    uint64_t nb_sectors = size >> BDRV_SECTOR_BITS;
+    uint64_t total_sectors;
+
     if (sector & dev->sector_mask) {
         return false;
     }
     if (size % dev->conf->logical_block_size) {
         return false;
     }
+    bdrv_get_geometry(dev->bs, &total_sectors);
+    if (sector > total_sectors || nb_sectors > total_sectors - sector) {
+        return false;
+    }
     return true;
 }
 
Signature · 1 line
1.9.3
#5 lore raw

Re: [Qemu-devel] [PATCH 2/3] virtio-blk: Bypass error action and I/O accounting on invalid r/w

In reply to Fam Zheng
Quoted 7 lines · Fam Zheng
Quoted 4 lines · Markus Armbruster

When a device model's I/O operation fails, we execute the error action. This lets layers above QEMU implement thin provisioning, or attempt to correct errors before they reach the guest. But when the I/O operation fails because its invalid, reporting the error to the

s/its/it's/ ?

Yes, this is a case where you are contracting "it is" into "it's"

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#6 lore raw

Re: [Qemu-devel] [PATCH 2/3] virtio-blk: Bypass error action and I/O accounting on invalid r/w

In reply to Fam Zheng
Quoted 7 lines · Fam Zheng
Quoted 4 lines · Markus Armbruster

When a device model's I/O operation fails, we execute the error action. This lets layers above QEMU implement thin provisioning, or attempt to correct errors before they reach the guest. But when the I/O operation fails because its invalid, reporting the error to the

s/its/it's/ ?

Of course. Perhaps it can be fixed on commit.

Quoted 3 lines

Anyway,

Thanks!

#13 lore raw
In reply to Markus Armbruster

Il 05/06/2014 14:15, Markus Armbruster ha scritto:

Quoted 8 lines

When a device model's I/O operation fails, we execute the error action. This lets layers above QEMU implement thin provisioning, or attempt to correct errors before they reach the guest. But when the I/O operation fails because its invalid, reporting the error to the guest is the only sensible action.

This short series does exactly that for virtio-blk. I intend to do the same for IDE and SCSI.

Actually SCSI already does it (see check_lba_range in hw/scsi/scsi-disk.c). Thanks for thinking about it though!

Paolo

#14 lore raw
Quoted 7 lines · Paolo Bonzini

Il 05/06/2014 14:15, Markus Armbruster ha scritto:

Quoted 6 lines

When a device model's I/O operation fails, we execute the error action. This lets layers above QEMU implement thin provisioning, or attempt to correct errors before they reach the guest. But when the I/O operation fails because its invalid, reporting the error to the guest is the only sensible action.

Quoted 5 lines
Quoted 2 lines

This short series does exactly that for virtio-blk. I intend to do the same for IDE and SCSI.

Actually SCSI already does it (see check_lba_range in hw/scsi/scsi-disk.c). Thanks for thinking about it though!

Your hint saved me some digging. Thanks!

#9 lore raw

Re: [Qemu-devel] [PATCH 3/3] virtio-blk: Treat read/write beyond end as invalid

In reply to Markus Armbruster

hw/block/virtio-blk.c static void virtio_blk_handle_flush(VirtIOBlockReq *req, MultiReqBuffer *mrb) · line 299

Quoted 3 lines · Markus Armbruster
hw/block/virtio-blk.c +3 -0
+    if (sector > total_sectors || nb_sectors > total_sectors - sector) {
+        return false;
+    }

if (sector >= total_sectors || ...) {

#10 lore raw

Re: [Qemu-devel] [PATCH 3/3] virtio-blk: Treat read/write beyond end as invalid

Quoted 6 lines · Stefan Hajnoczi

hw/block/virtio-blk.c static void virtio_blk_handle_flush(VirtIOBlockReq *req, MultiReqBuffer *mrb) · line 299

Quoted 3 lines · Markus Armbruster
hw/block/virtio-blk.c +3 -0
+    if (sector > total_sectors || nb_sectors > total_sectors - sector) {
+        return false;
+    }

if (sector >= total_sectors || ...) {

I suspect reading bdrv_check_byte_request() put the '>' in my brain:

    if ((offset > len) || (len - offset < size))
        return -EIO;

Don't we need offset >= len here?

#11 lore raw

Re: [Qemu-devel] [PATCH 3/3] virtio-blk: Treat read/write beyond end as invalid

Quoted 7 lines · Markus Armbruster
Quoted 5 lines · Stefan Hajnoczi
Quoted 3 lines · Markus Armbruster
hw/block/virtio-blk.c +3 -0
+    if (sector > total_sectors || nb_sectors > total_sectors - sector) {
+        return false;
+    }
Quoted 8 lines
Quoted 1 line

if (sector >= total_sectors || ...) {

I suspect reading bdrv_check_byte_request() put the '>' in my brain:

    if ((offset > len) || (len - offset < size))
        return -EIO;

Don't we need offset >= len here?

Just remembered: we don't, because we allow I/O at offset len provided size is zero.

Same reasoning applies to my patch.

#15 lore raw
In reply to Markus Armbruster

Ping?

Quoted 16 lines · Markus Armbruster

When a device model's I/O operation fails, we execute the error action. This lets layers above QEMU implement thin provisioning, or attempt to correct errors before they reach the guest. But when the I/O operation fails because its invalid, reporting the error to the guest is the only sensible action.

This short series does exactly that for virtio-blk. I intend to do the same for IDE and SCSI.

Markus Armbruster (3):
virtio-blk: Factor common checks out of virtio_blk_handle_read/write()
virtio-blk: Bypass error action and I/O accounting on invalid r/w
virtio-blk: Treat read/write beyond end as invalid

 hw/block/virtio-blk.c | 45 +++++++++++++++++++++++++++++----------------
 1 file changed, 29 insertions(+), 16 deletions(-)
#12 lore raw

Re: [Qemu-devel] [PATCH 3/3] virtio-blk: Treat read/write beyond end as invalid

In reply to Markus Armbruster
Quoted 16 lines · Markus Armbruster
Quoted 14 lines · Markus Armbruster
Quoted 6 lines · Stefan Hajnoczi

hw/block/virtio-blk.c static void virtio_blk_handle_flush(VirtIOBlockReq *req, MultiReqBuffer *mrb) · line 299

Quoted 3 lines · Markus Armbruster
hw/block/virtio-blk.c +3 -0
+    if (sector > total_sectors || nb_sectors > total_sectors - sector) {
+        return false;
+    }

if (sector >= total_sectors || ...) {

I suspect reading bdrv_check_byte_request() put the '>' in my brain:

    if ((offset > len) || (len - offset < size))
        return -EIO;
Quoted 6 lines
Quoted 1 line

Don't we need offset >= len here?

Just remembered: we don't, because we allow I/O at offset len provided size is zero.

Same reasoning applies to my patch.

Okay. I didn't remember the offset=eof length=0 thing.

Stefan

Reviewed-by on the v1 series
#16 lore raw
In reply to Markus Armbruster
Quoted 9 lines · Markus Armbruster

When a device model's I/O operation fails, we execute the error action. This lets layers above QEMU implement thin provisioning, or attempt to correct errors before they reach the guest. But when the I/O operation fails because its invalid, reporting the error to the guest is the only sensible action.

This short series does exactly that for virtio-blk. I intend to do the same for IDE and SCSI.

Quoted 11 lines

Markus Armbruster (3):
virtio-blk: Factor common checks out of virtio_blk_handle_read/write()
virtio-blk: Bypass error action and I/O accounting on invalid r/w
virtio-blk: Treat read/write beyond end as invalid

 hw/block/virtio-blk.c | 45 +++++++++++++++++++++++++++++----------------
 1 file changed, 29 insertions(+), 16 deletions(-)
Signature · 1 line
1.9.3
#17 lore raw
In reply to Markus Armbruster

Please consider for 2.1. It needs git-am -3 by now; if you need a respin, let me know.

Quoted 16 lines · Markus Armbruster

When a device model's I/O operation fails, we execute the error action. This lets layers above QEMU implement thin provisioning, or attempt to correct errors before they reach the guest. But when the I/O operation fails because its invalid, reporting the error to the guest is the only sensible action.

This short series does exactly that for virtio-blk. I intend to do the same for IDE and SCSI.

Markus Armbruster (3):
virtio-blk: Factor common checks out of virtio_blk_handle_read/write()
virtio-blk: Bypass error action and I/O accounting on invalid r/w
virtio-blk: Treat read/write beyond end as invalid

 hw/block/virtio-blk.c | 45 +++++++++++++++++++++++++++++----------------
 1 file changed, 29 insertions(+), 16 deletions(-)