virtio-blk: Suppress error action on r/w beyond end
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
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
@@ -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
PATCH 2/3 virtio-blk: Bypass error action and I/O accounting on invalid r/w
In reply to Markus ArmbrusterWhen 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
@@ -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
PATCH 3/3 virtio-blk: Treat read/write beyond end as invalid
In reply to Markus ArmbrusterThe 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
@@ -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
Re: [Qemu-devel] [PATCH 1/3] virtio-blk: Factor common checks out of virtio_blk_handle_read/write()
In reply to Markus ArmbrusterQuoted 58 lines · Markus Armbruster
Signed-off-by Markus Armbruster <[email protected]>--- hw/block/virtio-blk.c | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-)hw/block/virtio-blk.c +14
-10@@ -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
Re: [Qemu-devel] [PATCH 2/3] virtio-blk: Bypass error action and I/O accounting on invalid r/w
In reply to Markus ArmbrusterQuoted 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.
Signed-off-by Markus Armbruster <[email protected]>--- hw/block/virtio-blk.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-)hw/block/virtio-blk.c +8
-6@@ -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
Re: [Qemu-devel] [PATCH 3/3] virtio-blk: Treat read/write beyond end as invalid
In reply to Markus ArmbrusterQuoted 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.
Signed-off-by Markus Armbruster <[email protected]>--- hw/block/virtio-blk.c | 7 +++++++ 1 file changed, 7 insertions(+)hw/block/virtio-blk.c +7
-0@@ -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
Re: [Qemu-devel] [PATCH 2/3] virtio-blk: Bypass error action and I/O accounting on invalid r/w
In reply to Fam ZhengQuoted 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
Re: [Qemu-devel] [PATCH 2/3] virtio-blk: Bypass error action and I/O accounting on invalid r/w
In reply to Fam ZhengQuoted 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,
Reviewed-by Fam Zheng <[email protected]>
Thanks!
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
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_rangeinhw/scsi/scsi-disk.c). Thanks for thinking about it though!
Your hint saved me some digging. Thanks!
Re: [Qemu-devel] [PATCH 3/3] virtio-blk: Treat read/write beyond end as invalid
In reply to Markus Armbrusterhw/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 || ...) {
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?
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.
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 invalidhw/block/virtio-blk.c | 45 +++++++++++++++++++++++++++++---------------- 1 file changed, 29 insertions(+), 16 deletions(-)
Re: [Qemu-devel] [PATCH 3/3] virtio-blk: Treat read/write beyond end as invalid
In reply to Markus ArmbrusterQuoted 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
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 invalidhw/block/virtio-blk.c | 45 +++++++++++++++++++++++++++++---------------- 1 file changed, 29 insertions(+), 16 deletions(-)Signature · 1 line
1.9.3
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 invalidhw/block/virtio-blk.c | 45 +++++++++++++++++++++++++++++---------------- 1 file changed, 29 insertions(+), 16 deletions(-)