Skip to contentSkip to conversation

block: Fix multiwrite error handling

PATCH Superseded Kevin Wolf 3 participants 7 messages Jul 1, 2010 – Jul 2, 2010

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Kevin Wolf

author
#0 lore raw

cover block: Fix multiwrite error handling

The bdrv_aio_multiwrite error handling has some bugs that lead to premature cleanup, causing use-after-free and double free problems.

Kevin Wolf (2):
block: Fix too early free in multiwrite
block: Handle multiwrite errors only when all requests have completed

 block.c |   11 +++--------
 1 files changed, 3 insertions(+), 8 deletions(-)
author
#4 lore raw

PATCH 2/2 block: Handle multiwrite errors only when all requests have completed

Don't try to be clever by freeing all temporary data and calling all callbacks when the return value (an error) is certain. Doing so has at least two important problems:

* The temporary data that is freed (qiov, possibly zero buffer) is still used by the requests that have not yet completed.
* Calling the callbacks for all requests in the multiwrite means for the caller that it may free buffers etc. which are still in use.

Just remember the error value and do the cleanup when all requests have completed.

---
 block.c |    5 +----
 1 files changed, 1 insertions(+), 4 deletions(-)
block.c +1 -4
index 9719649..bff7d5a 100644
@@ -2056,14 +2056,11 @@ static void multiwrite_cb(void *opaque, int ret)
 
     if (ret < 0 && !mcb->error) {
         mcb->error = ret;
-        multiwrite_user_cb(mcb);
     }
 
     mcb->num_requests--;
     if (mcb->num_requests == 0) {
-        if (mcb->error == 0) {
-            multiwrite_user_cb(mcb);
-        }
+        multiwrite_user_cb(mcb);
         qemu_free(mcb);
     }
 }
Signature · 1 line
1.6.6.1
author
#1 lore raw

PATCH 1/2 block: Fix too early free in multiwrite

In reply to Kevin Wolf

bdrv_aio_writev may call the callback immediately (and it will commonly do so in error cases). If num_requests doesn't have its final value yet, multiwrite_cb will falsely detect that all requests are completed and frees the mcb. However, the mcb is still used by other requests that are started only afterwards. When all requests are completed, it is freed for the second time.

Fix this by setting the right num_requests from the beginning.

---
 block.c |    6 ++----
 1 files changed, 2 insertions(+), 4 deletions(-)
block.c +2 -4
index c40dd2c..9719649 100644
@@ -2198,6 +2198,7 @@ int bdrv_aio_multiwrite(BlockDriverState *bs, BlockRequest *reqs, int num_reqs)
     num_reqs = multiwrite_merge(bs, reqs, num_reqs, mcb);
 
     // Run the aio requests
+    mcb->num_requests = num_reqs;
     for (i = 0; i < num_reqs; i++) {
         acb = bdrv_aio_writev(bs, reqs[i].sector, reqs[i].qiov,
             reqs[i].nb_sectors, multiwrite_cb, mcb);
@@ -2206,16 +2207,13 @@ int bdrv_aio_multiwrite(BlockDriverState *bs, BlockRequest *reqs, int num_reqs)
             // We can only fail the whole thing if no request has been
             // submitted yet. Otherwise we'll wait for the submitted AIOs to
             // complete and report the error in the callback.
-            if (mcb->num_requests == 0) {
+            if (i == 0) {
                 reqs[i].error = -EIO;
                 goto fail;
             } else {
-                mcb->num_requests++;
                 multiwrite_cb(mcb, -EIO);
                 break;
             }
-        } else {
-            mcb->num_requests++;
         }
     }
 
Signature · 1 line
1.6.6.1
#2 lore raw

Re: [Qemu-devel] [PATCH 1/2] block: Fix too early free in multiwrite

Quoted 7 lines · Kevin Wolf

bdrv_aio_writev may call the callback immediately (and it will commonly do so in error cases). If num_requests doesn't have its final value yet, multiwrite_cb will falsely detect that all requests are completed and frees the mcb. However, the mcb is still used by other requests that are started only afterwards. When all requests are completed, it is freed for the second time.

Fix this by setting the right num_requests from the beginning.

Looks good to me.

Quoted 37 lines

---
 block.c |    6 ++----

 1 files changed, 2 insertions(+), 4 deletions(-)
block.c +2 -5
index c40dd2c..9719649 100644
@@ -2198,6 +2198,7 @@ int bdrv_aio_multiwrite(BlockDriverState *bs, BlockRequest *reqs, int num_reqs)
     num_reqs = multiwrite_merge(bs, reqs, num_reqs, mcb);
 
     // Run the aio requests
+    mcb->num_requests = num_reqs;
     for (i = 0; i < num_reqs; i++) {
         acb = bdrv_aio_writev(bs, reqs[i].sector, reqs[i].qiov,
             reqs[i].nb_sectors, multiwrite_cb, mcb);
@@ -2206,16 +2207,13 @@ int bdrv_aio_multiwrite(BlockDriverState *bs, BlockRequest *reqs, int num_reqs)
             // We can only fail the whole thing if no request has been
             // submitted yet. Otherwise we'll wait for the submitted AIOs to
             // complete and report the error in the callback.
-            if (mcb->num_requests == 0) {
+            if (i == 0) {
                 reqs[i].error = -EIO;
                 goto fail;
             } else {
-                mcb->num_requests++;
                 multiwrite_cb(mcb, -EIO);
                 break;
             }
-        } else {
-            mcb->num_requests++;
         }
     }
 
--
Quoted 4 lines

1.6.6.1

Stefan

#5 lore raw

Re: [Qemu-devel] [PATCH 2/2] block: Handle multiwrite errors only when all requests have completed

In reply to Kevin Wolf
Quoted 24 lines · Kevin Wolf

Don't try to be clever by freeing all temporary data and calling all callbacks when the return value (an error) is certain. Doing so has at least two important problems:

* The temporary data that is freed (qiov, possibly zero buffer) is still used by the requests that have not yet completed.
* Calling the callbacks for all requests in the multiwrite means for the caller that it may free buffers etc. which are still in use.

Just remember the error value and do the cleanup when all requests have completed.

---
 block.c |    5 +----

 1 files changed, 1 insertions(+), 4 deletions(-)
block.c +0 -0
index 9719649..bff7d5a 100644
@@ -2056,14 +2056,11 @@ static void multiwrite_cb(void *opaque, int ret)
 
     if (ret < 0 && !mcb->error) {

block.c static void multiwrite_cb(void *opaque, int ret) · line 2063

Quoted 10 lines
block.c +1 -4
         mcb->error = ret;
-        multiwrite_user_cb(mcb);
     }
 
     mcb->num_requests--;
     if (mcb->num_requests == 0) {
-        if (mcb->error == 0) {
-            multiwrite_user_cb(mcb);
-        }
+        multiwrite_user_cb(mcb);

I am a little confused at this point. Here is the meat of bdrv_aio_multiwrite() from the kevin/devel branch:

// Run the aio requests
mcb->num_requests = num_reqs;

for (i = 0; i < num_reqs; i++) {
    acb = bdrv_aio_writev(bs, reqs[i].sector, reqs[i].qiov,
        reqs[i].nb_sectors, multiwrite_cb, mcb);

    if (acb == NULL) {
        // We can only fail the whole thing if no request has been
        // submitted yet. Otherwise we'll wait for the submitted AIOs to
        // complete and report the error in the callback.
        if (i == 0) {
            reqs[i].error = -EIO;
            goto fail;
        } else {
            multiwrite_cb(mcb, -EIO);
            break;
        }
    }
}

If bdrv_aio_write() fails on request 2 out of 3, then mcb->num_requests = 3 but only 2 requests were issued before breaking the loop.

Now the 2 issued requests complete and call multiwrite_cb(). Since was mcb->num_requests = 3, it never reaches zero and neither multiwrite_user_cb(mcb) nor qemu_free(mcb) are ever called.

Is it safe to adjust mcb->num_requests = i before calling multiwrite_cb() and breaking the loop in the failure case? That way the num->num_requests will reach zero.

I hesitate a little because previous requests could have completed, multiwrite_cb() was called, and mcb->num_requests was decremented? Then the value of i cannot be used for mcb->num_requests because previous requests it counts have already completed.

Stefan

Reviewed-by on v1 1/2
#3 lore raw

Re: [Qemu-devel] [PATCH 1/2] block: Fix too early free in multiwrite

In reply to Kevin Wolf
Quoted 7 lines · Kevin Wolf

bdrv_aio_writev may call the callback immediately (and it will commonly do so in error cases). If num_requests doesn't have its final value yet, multiwrite_cb will falsely detect that all requests are completed and frees the mcb. However, the mcb is still used by other requests that are started only afterwards. When all requests are completed, it is freed for the second time.

Fix this by setting the right num_requests from the beginning.

Looks good,

#6 lore raw

Re: [Qemu-devel] [PATCH 2/2] block: Handle multiwrite errors only when all requests have completed

In reply to Kevin Wolf
Quoted 11 lines · Kevin Wolf

Don't try to be clever by freeing all temporary data and calling all callbacks when the return value (an error) is certain. Doing so has at least two important problems:

* The temporary data that is freed (qiov, possibly zero buffer) is still used by the requests that have not yet completed.
* Calling the callbacks for all requests in the multiwrite means for the caller that it may free buffers etc. which are still in use.

Just remember the error value and do the cleanup when all requests have completed.

Looks good.