thebeanogamer / rpms / qemu-kvm

Forked from rpms/qemu-kvm 5 months ago
Clone

Blame SOURCES/kvm-block-temporarily-hold-the-new-AioContext-of-bs_top-.patch

ed5979
From b952c8f1da6f8597736c0e040565830139369359 Mon Sep 17 00:00:00 2001
ed5979
From: Stefano Garzarella <sgarzare@redhat.com>
ed5979
Date: Tue, 14 Feb 2023 18:16:21 +0100
ed5979
Subject: [PATCH] block: temporarily hold the new AioContext of bs_top in
ed5979
 bdrv_append()
ed5979
ed5979
RH-Author: Stefano Garzarella <sgarzare@redhat.com>
ed5979
RH-MergeRequest: 153: block: temporarily hold the new AioContext of bs_top in bdrv_append()
ed5979
RH-Bugzilla: 2168209
ed5979
RH-Acked-by: Emanuele Giuseppe Esposito <eesposit@redhat.com>
ed5979
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
ed5979
RH-Acked-by: Kevin Wolf <kwolf@redhat.com>
ed5979
RH-Commit: [1/1] 5b190426d996e8c9f7a781bd97aee8d25756dbd3 (sgarzarella/qemu-kvm-c-9-s)
ed5979
ed5979
bdrv_append() is called with bs_top AioContext held, but
ed5979
bdrv_attach_child_noperm() could change the AioContext of bs_top.
ed5979
ed5979
bdrv_replace_node_noperm() calls bdrv_drained_begin() starting from
ed5979
commit 2398747128 ("block: Don't poll in bdrv_replace_child_noperm()").
ed5979
bdrv_drained_begin() can call BDRV_POLL_WHILE that assumes the new lock
ed5979
is taken, so let's temporarily hold the new AioContext to prevent QEMU
ed5979
from failing in BDRV_POLL_WHILE when it tries to release the wrong
ed5979
AioContext.
ed5979
ed5979
Buglink: https://bugzilla.redhat.com/show_bug.cgi?id=2168209
ed5979
Reported-by: Aihua Liang <aliang@redhat.com>
ed5979
Signed-off-by: Stefano Garzarella <sgarzare@redhat.com>
ed5979
Message-Id: <20230214171621.11574-1-sgarzare@redhat.com>
ed5979
Reviewed-by: Kevin Wolf <kwolf@redhat.com>
ed5979
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
ed5979
(cherry picked from commit 60d90bf43c169b9d1dbcb17ed794b7b02c6862b1)
ed5979
Signed-off-by: Stefano Garzarella <sgarzare@redhat.com>
ed5979
---
ed5979
 block.c | 23 +++++++++++++++++++++++
ed5979
 1 file changed, 23 insertions(+)
ed5979
ed5979
diff --git a/block.c b/block.c
ed5979
index 0d78711416..9e1dcb9e47 100644
ed5979
--- a/block.c
ed5979
+++ b/block.c
ed5979
@@ -5275,6 +5275,8 @@ int bdrv_drop_filter(BlockDriverState *bs, Error **errp)
ed5979
  * child.
ed5979
  *
ed5979
  * This function does not create any image files.
ed5979
+ *
ed5979
+ * The caller must hold the AioContext lock for @bs_top.
ed5979
  */
ed5979
 int bdrv_append(BlockDriverState *bs_new, BlockDriverState *bs_top,
ed5979
                 Error **errp)
ed5979
@@ -5282,11 +5284,14 @@ int bdrv_append(BlockDriverState *bs_new, BlockDriverState *bs_top,
ed5979
     int ret;
ed5979
     BdrvChild *child;
ed5979
     Transaction *tran = tran_new();
ed5979
+    AioContext *old_context, *new_context = NULL;
ed5979
 
ed5979
     GLOBAL_STATE_CODE();
ed5979
 
ed5979
     assert(!bs_new->backing);
ed5979
 
ed5979
+    old_context = bdrv_get_aio_context(bs_top);
ed5979
+
ed5979
     child = bdrv_attach_child_noperm(bs_new, bs_top, "backing",
ed5979
                                      &child_of_bds, bdrv_backing_role(bs_new),
ed5979
                                      tran, errp);
ed5979
@@ -5295,6 +5300,19 @@ int bdrv_append(BlockDriverState *bs_new, BlockDriverState *bs_top,
ed5979
         goto out;
ed5979
     }
ed5979
 
ed5979
+    /*
ed5979
+     * bdrv_attach_child_noperm could change the AioContext of bs_top.
ed5979
+     * bdrv_replace_node_noperm calls bdrv_drained_begin, so let's temporarily
ed5979
+     * hold the new AioContext, since bdrv_drained_begin calls BDRV_POLL_WHILE
ed5979
+     * that assumes the new lock is taken.
ed5979
+     */
ed5979
+    new_context = bdrv_get_aio_context(bs_top);
ed5979
+
ed5979
+    if (old_context != new_context) {
ed5979
+        aio_context_release(old_context);
ed5979
+        aio_context_acquire(new_context);
ed5979
+    }
ed5979
+
ed5979
     ret = bdrv_replace_node_noperm(bs_top, bs_new, true, tran, errp);
ed5979
     if (ret < 0) {
ed5979
         goto out;
ed5979
@@ -5306,6 +5324,11 @@ out:
ed5979
 
ed5979
     bdrv_refresh_limits(bs_top, NULL, NULL);
ed5979
 
ed5979
+    if (new_context && old_context != new_context) {
ed5979
+        aio_context_release(new_context);
ed5979
+        aio_context_acquire(old_context);
ed5979
+    }
ed5979
+
ed5979
     return ret;
ed5979
 }
ed5979
 
ed5979
-- 
ed5979
2.31.1
ed5979