Skip to contentSkip to conversation

virtio-blk: Fix double completion for werror=stop

PATCH Superseded Fam Zheng 3 participants 4 messages Nov 13, 2015 – Nov 16, 2015

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Fam Zheng

author
#0 lore raw

PATCH 1/1 virtio-blk: Fix double completion for werror=stop

When a request R is absorbed by request M, it is appended to the "mr_next" queue led by M, and is completed together with the completion of M, in virtio_blk_rw_complete.

With error policy equals stop, if M has an I/O error, now R also gets prepended to the per device DMA restart queue, which will be retried when VM resumes. It leads to a double completion (in symptoms of memory corruption or use after free).

Adding R to the queue is superfluous, only M needs to be in the queue.

Fix this by marking request R as "merged" and skipping it in virtio_blk_handle_rw_error.

---
 hw/block/virtio-blk.c          | 9 +++++++--
 include/hw/virtio/virtio-blk.h | 1 +
 2 files changed, 8 insertions(+), 2 deletions(-)
hw/block/virtio-blk.c +7 -2
index e70fccf..4871f2a 100644
@@ -36,6 +36,7 @@ VirtIOBlockReq *virtio_blk_alloc_request(VirtIOBlock *s)
     req->in_len = 0;
     req->next = NULL;
     req->mr_next = NULL;
+    req->merged = false;
     return req;
 }
 
@@ -72,8 +73,11 @@ static int virtio_blk_handle_rw_error(VirtIOBlockReq *req, int error,
     VirtIOBlock *s = req->dev;
 
     if (action == BLOCK_ERROR_ACTION_STOP) {
-        req->next = s->rq;
-        s->rq = req;
+        /* Merged requests don't need to be restarted. */
+        if (!req->merged) {
+            req->next = s->rq;
+            s->rq = req;
+        }
     } else if (action == BLOCK_ERROR_ACTION_REPORT) {
         virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR);
         block_acct_failed(blk_get_stats(s->blk), &req->acct);
@@ -344,6 +348,7 @@ static inline void submit_requests(BlockBackend *blk, MultiReqBuffer *mrb,
         for (i = start + 1; i < start + num_reqs; i++) {
             qemu_iovec_concat(qiov, &mrb->reqs[i]->qiov, 0,
                               mrb->reqs[i]->qiov.size);
+            mrb->reqs[i]->merged = true;
             mrb->reqs[i - 1]->mr_next = mrb->reqs[i];
             nb_sectors += mrb->reqs[i]->qiov.size / BDRV_SECTOR_SIZE;
         }
include/hw/virtio/virtio-blk.h +1 -0
index 6bf5905..db4adf4 100644
@@ -70,6 +70,7 @@ typedef struct VirtIOBlockReq {
     size_t in_len;
     struct VirtIOBlockReq *next;
     struct VirtIOBlockReq *mr_next;
+    bool merged;
     BlockAcctCookie acct;
 } VirtIOBlockReq;
 
Signature · 1 line
2.4.3
Tested-by on v1 1/1
#1 lore raw
Quoted 61 lines · Fam Zheng

When a request R is absorbed by request M, it is appended to the "mr_next" queue led by M, and is completed together with the completion of M, in virtio_blk_rw_complete.

With error policy equals stop, if M has an I/O error, now R also gets prepended to the per device DMA restart queue, which will be retried when VM resumes. It leads to a double completion (in symptoms of memory corruption or use after free).

Adding R to the queue is superfluous, only M needs to be in the queue.

Fix this by marking request R as "merged" and skipping it in virtio_blk_handle_rw_error.

---
 hw/block/virtio-blk.c          | 9 +++++++--
 include/hw/virtio/virtio-blk.h | 1 +
 2 files changed, 8 insertions(+), 2 deletions(-)
hw/block/virtio-blk.c +7 -2
index e70fccf..4871f2a 100644
@@ -36,6 +36,7 @@ VirtIOBlockReq *virtio_blk_alloc_request(VirtIOBlock *s)
     req->in_len = 0;
     req->next = NULL;
     req->mr_next = NULL;
+    req->merged = false;
     return req;
 }
 
@@ -72,8 +73,11 @@ static int virtio_blk_handle_rw_error(VirtIOBlockReq *req, int error,
     VirtIOBlock *s = req->dev;
 
     if (action == BLOCK_ERROR_ACTION_STOP) {
-        req->next = s->rq;
-        s->rq = req;
+        /* Merged requests don't need to be restarted. */
+        if (!req->merged) {
+            req->next = s->rq;
+            s->rq = req;
+        }
     } else if (action == BLOCK_ERROR_ACTION_REPORT) {
         virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR);
         block_acct_failed(blk_get_stats(s->blk), &req->acct);
@@ -344,6 +348,7 @@ static inline void submit_requests(BlockBackend *blk, MultiReqBuffer *mrb,
         for (i = start + 1; i < start + num_reqs; i++) {
             qemu_iovec_concat(qiov, &mrb->reqs[i]->qiov, 0,
                               mrb->reqs[i]->qiov.size);
+            mrb->reqs[i]->merged = true;
             mrb->reqs[i - 1]->mr_next = mrb->reqs[i];
             nb_sectors += mrb->reqs[i]->qiov.size / BDRV_SECTOR_SIZE;
         }
include/hw/virtio/virtio-blk.h +0 -0
index 6bf5905..db4adf4 100644

include/hw/virtio/virtio-blk.h typedef struct VirtIOBlockReq { · line 76

Quoted 7 lines
include/hw/virtio/virtio-blk.h +1 -0
@@ -70,6 +70,7 @@ typedef struct VirtIOBlockReq {
     size_t in_len;
     struct VirtIOBlockReq *next;
     struct VirtIOBlockReq *mr_next;
+    bool merged;
     BlockAcctCookie acct;
 } VirtIOBlockReq;
 

In my case, this patch fixes a memory corruption when the VM is stopped on io-error (no space) and restarted.

#2 lore raw
In reply to Fam Zheng
Quoted 34 lines · Fam Zheng

When a request R is absorbed by request M, it is appended to the "mr_next" queue led by M, and is completed together with the completion of M, in virtio_blk_rw_complete.

With error policy equals stop, if M has an I/O error, now R also gets prepended to the per device DMA restart queue, which will be retried when VM resumes. It leads to a double completion (in symptoms of memory corruption or use after free).

Adding R to the queue is superfluous, only M needs to be in the queue.

Fix this by marking request R as "merged" and skipping it in virtio_blk_handle_rw_error.

---
 hw/block/virtio-blk.c          | 9 +++++++--
 include/hw/virtio/virtio-blk.h | 1 +
 2 files changed, 8 insertions(+), 2 deletions(-)
hw/block/virtio-blk.c +1 -0
index e70fccf..4871f2a 100644
@@ -36,6 +36,7 @@ VirtIOBlockReq *virtio_blk_alloc_request(VirtIOBlock *s)
     req->in_len = 0;
     req->next = NULL;
     req->mr_next = NULL;
+    req->merged = false;
     return req;
 }
 

hw/block/virtio-blk.c static int virtio_blk_handle_rw_error(VirtIOBlockReq *req, int error, · line 80

Quoted 10 lines
hw/block/virtio-blk.c +5 -2
@@ -72,8 +73,11 @@ static int virtio_blk_handle_rw_error(VirtIOBlockReq *req, int error,
     VirtIOBlock *s = req->dev;
 
     if (action == BLOCK_ERROR_ACTION_STOP) {
-        req->next = s->rq;
-        s->rq = req;
+        /* Merged requests don't need to be restarted. */
+        if (!req->merged) {
+            req->next = s->rq;
+            s->rq = req;
+        }

I think it still needs to be queued, so that the request is migrated correctly. Should req->merged be checked in virtio_blk_dma_restart_bh instead?

Paolo

Quoted 24 lines
hw/block/virtio-blk.c +1 -0
     } else if (action == BLOCK_ERROR_ACTION_REPORT) {
         virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR);
         block_acct_failed(blk_get_stats(s->blk), &req->acct);
@@ -344,6 +348,7 @@ static inline void submit_requests(BlockBackend *blk, MultiReqBuffer *mrb,
         for (i = start + 1; i < start + num_reqs; i++) {
             qemu_iovec_concat(qiov, &mrb->reqs[i]->qiov, 0,
                               mrb->reqs[i]->qiov.size);
+            mrb->reqs[i]->merged = true;
             mrb->reqs[i - 1]->mr_next = mrb->reqs[i];
             nb_sectors += mrb->reqs[i]->qiov.size / BDRV_SECTOR_SIZE;
         }
include/hw/virtio/virtio-blk.h +1 -0
index 6bf5905..db4adf4 100644
@@ -70,6 +70,7 @@ typedef struct VirtIOBlockReq {
     size_t in_len;
     struct VirtIOBlockReq *next;
     struct VirtIOBlockReq *mr_next;
+    bool merged;
     BlockAcctCookie acct;
 } VirtIOBlockReq;
 
author
#3 lore raw
Quoted 39 lines · Paolo Bonzini
Quoted 36 lines · Fam Zheng

When a request R is absorbed by request M, it is appended to the "mr_next" queue led by M, and is completed together with the completion of M, in virtio_blk_rw_complete.

With error policy equals stop, if M has an I/O error, now R also gets prepended to the per device DMA restart queue, which will be retried when VM resumes. It leads to a double completion (in symptoms of memory corruption or use after free).

Adding R to the queue is superfluous, only M needs to be in the queue.

Fix this by marking request R as "merged" and skipping it in virtio_blk_handle_rw_error.

---
 hw/block/virtio-blk.c          | 9 +++++++--
 include/hw/virtio/virtio-blk.h | 1 +
 2 files changed, 8 insertions(+), 2 deletions(-)
hw/block/virtio-blk.c +1 -0
index e70fccf..4871f2a 100644
@@ -36,6 +36,7 @@ VirtIOBlockReq *virtio_blk_alloc_request(VirtIOBlock *s)
     req->in_len = 0;
     req->next = NULL;
     req->mr_next = NULL;
+    req->merged = false;
     return req;
 }
 
@@ -72,8 +73,11 @@ static int virtio_blk_handle_rw_error(VirtIOBlockReq *req, int error,
     VirtIOBlock *s = req->dev;
 
Quoted 12 lines

hw/block/virtio-blk.c static int virtio_blk_handle_rw_error(VirtIOBlockReq *req, int error, · line 80

Quoted 8 lines
hw/block/virtio-blk.c +5 -2
     if (action == BLOCK_ERROR_ACTION_STOP) {
-        req->next = s->rq;
-        s->rq = req;
+        /* Merged requests don't need to be restarted. */
+        if (!req->merged) {
+            req->next = s->rq;
+            s->rq = req;
+        }

I think it still needs to be queued, so that the request is migrated correctly. Should req->merged be checked in virtio_blk_dma_restart_bh instead?

Yes you're right.

Thanks,
Fam