Skip to contentSkip to conversation

virtio-blk: Fix double completion for werror=stop

PATCH Applied Fam Zheng 4 participants 5 messages Nov 17, 2015 – Nov 23, 2015

lore
Share & tools
lore

Fam Zheng

author
#0 lore raw

PATCH v3 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.

During DMA restart in virtio_blk_dma_restart_bh, requests in s->rq are parsed and submitted again, possibly with a stale req->mr_next. It could be a problem if the request merging in virtio_blk_handle_request hasn't refreshed every mr_next pointer, in which case, virtio_blk_rw_complete could walk through unexpected requests following the stale pointers.

Fix this by unsetting the pointer in virtio_blk_rw_complete. It is safe because this req is either completed and freed right away, or it will be restarted and parsed from scratch out of the vq later.

---

v3: Fix as Stefan suggested.

---
 hw/block/virtio-blk.c | 4 ++++
 1 file changed, 4 insertions(+)
hw/block/virtio-blk.c +4 -0
index e70fccf..848f3fe 100644
@@ -112,6 +112,10 @@ static void virtio_blk_rw_complete(void *opaque, int ret)
              * happen on the other side of the migration).
              */
             if (virtio_blk_handle_rw_error(req, -ret, is_read)) {
+                /* Break the link in case the next request is added to the
+                 * restart queue and is going to be parsed from the ring again.
+                 */
+                req->mr_next = NULL;
                 continue;
             }
         }
Signature · 1 line
2.4.3
#1 lore raw
Quoted 14 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.

During DMA restart in virtio_blk_dma_restart_bh, requests in s->rq are parsed and submitted again, possibly with a stale req->mr_next. It could be a problem if the request merging in virtio_blk_handle_request hasn't refreshed every mr_next pointer, in which case, virtio_blk_rw_complete could walk through unexpected requests following the stale pointers.

Fix this by unsetting the pointer in virtio_blk_rw_complete. It is safe because this req is either completed and freed right away, or it will be restarted and parsed from scratch out of the vq later.

Quoted 8 lines

---

v3: Fix as Stefan suggested.

---
 hw/block/virtio-blk.c | 4 ++++
 1 file changed, 4 insertions(+)

Thanks, applied to my block tree:
https://github.com/stefanha/qemu/commits/block

Stefan

Tested-by
#2 lore raw
Quoted 19 lines · Stefan Hajnoczi
Quoted 18 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.

During DMA restart in virtio_blk_dma_restart_bh, requests in s->rq are parsed and submitted again, possibly with a stale req->mr_next. It could be a problem if the request merging in virtio_blk_handle_request hasn't refreshed every mr_next pointer, in which case, virtio_blk_rw_complete could walk through unexpected requests following the stale pointers.

Fix this by unsetting the pointer in virtio_blk_rw_complete. It is safe because this req is either completed and freed right away, or it will be restarted and parsed from scratch out of the vq later.

---

Quoted 7 lines
Quoted 4 lines

v3: Fix as Stefan suggested.

---
 hw/block/virtio-blk.c | 4 ++++
 1 file changed, 4 insertions(+)

Thanks, applied to my block tree:
https://github.com/stefanha/qemu/commits/block

I have also tested this one and it works fine.

Laurent

#3 lore raw
In reply to Fam Zheng
Quoted 29 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.

During DMA restart in virtio_blk_dma_restart_bh, requests in s->rq are parsed and submitted again, possibly with a stale req->mr_next. It could be a problem if the request merging in virtio_blk_handle_request hasn't refreshed every mr_next pointer, in which case, virtio_blk_rw_complete could walk through unexpected requests following the stale pointers.

Fix this by unsetting the pointer in virtio_blk_rw_complete. It is safe because this req is either completed and freed right away, or it will be restarted and parsed from scratch out of the vq later.

---

v3: Fix as Stefan suggested.

---
 hw/block/virtio-blk.c | 4 ++++
 1 file changed, 4 insertions(+)
hw/block/virtio-blk.c +0 -0
index e70fccf..848f3fe 100644

hw/block/virtio-blk.c static void virtio_blk_rw_complete(void *opaque, int ret) · line 121

Quoted 10 lines
hw/block/virtio-blk.c +4 -0
@@ -112,6 +112,10 @@ static void virtio_blk_rw_complete(void *opaque, int ret)
              * happen on the other side of the migration).
              */
             if (virtio_blk_handle_rw_error(req, -ret, is_read)) {
+                /* Break the link in case the next request is added to the
+                 * restart queue and is going to be parsed from the ring again.
+                 */
+                req->mr_next = NULL;
                 continue;
             }
         }

This is now a write-after-free for rerror/werror=stop. The right place to set req->mr_next is inside virtio_blk_handle_rw_error, I think.

Paolo

author
#4 lore raw
Quoted 11 lines · Paolo Bonzini
Quoted 11 lines
hw/block/virtio-blk.c +3 -0
index e70fccf..848f3fe 100644
@@ -112,6 +112,10 @@ static void virtio_blk_rw_complete(void *opaque, int ret)
              * happen on the other side of the migration).
              */
             if (virtio_blk_handle_rw_error(req, -ret, is_read)) {
+                /* Break the link in case the next request is added to the
+                 * restart queue and is going to be parsed from the ring again.
+                 */
Quoted 9 lines

hw/block/virtio-blk.c static void virtio_blk_rw_complete(void *opaque, int ret) · line 121

Quoted 5 lines
hw/block/virtio-blk.c +1 -0
+                req->mr_next = NULL;
                 continue;
             }
         }

This is now a write-after-free for rerror/werror=stop. The right place to set req->mr_next is inside virtio_blk_handle_rw_error, I think.

Oh yes :( Sending another patch.

Fam