Blame SOURCES/kvm-virtio-scsi-fixed-virtio_scsi_ctx_check-failed-when-.patch

8b1478
From ed8f16b1786f28d3fb6c6cef79cd4f94deae8857 Mon Sep 17 00:00:00 2001
8b1478
From: Sergio Lopez Pascual <slp@redhat.com>
8b1478
Date: Thu, 28 Nov 2019 11:30:47 +0100
8b1478
Subject: [PATCH] virtio-scsi: fixed virtio_scsi_ctx_check failed when
8b1478
 detaching scsi disk
8b1478
MIME-Version: 1.0
8b1478
Content-Type: text/plain; charset=UTF-8
8b1478
Content-Transfer-Encoding: 8bit
8b1478
8b1478
RH-Author: Sergio Lopez Pascual <slp@redhat.com>
8b1478
Message-id: <20191128113047.253669-2-slp@redhat.com>
8b1478
Patchwork-id: 92747
8b1478
O-Subject: [RHEL-7.8 qemu-kvm-rhev PATCH v2 1/1] virtio-scsi: fixed virtio_scsi_ctx_check failed when detaching scsi disk
8b1478
Bugzilla: 1764120
8b1478
RH-Acked-by: Markus Armbruster <armbru@redhat.com>
8b1478
RH-Acked-by: Max Reitz <mreitz@redhat.com>
8b1478
RH-Acked-by: Maxim Levitsky <mlevitsk@redhat.com>
8b1478
8b1478
From: Zhengui li <lizhengui@huawei.com>
8b1478
8b1478
commit a6f230c move blockbackend back to main AioContext on unplug. It set the AioContext of
8b1478
SCSIDevice to the main AioContex, but s->ctx is still the iothread AioContex(if the scsi controller
8b1478
is configure with iothread). So if there are having in-flight requests during unplug, a failing assertion
8b1478
happend. The bt is below:
8b1478
(gdb) bt
8b1478
#0  0x0000ffff86aacbd0 in raise () from /lib64/libc.so.6
8b1478
#1  0x0000ffff86aadf7c in abort () from /lib64/libc.so.6
8b1478
#2  0x0000ffff86aa6124 in __assert_fail_base () from /lib64/libc.so.6
8b1478
#3  0x0000ffff86aa61a4 in __assert_fail () from /lib64/libc.so.6
8b1478
#4  0x0000000000529118 in virtio_scsi_ctx_check (d=<optimized out>, s=<optimized out>, s=<optimized out>) at /home/qemu-4.0.0/hw/scsi/virtio-scsi.c:246
8b1478
#5  0x0000000000529ec4 in virtio_scsi_handle_cmd_req_prepare (s=0x2779ec00, req=0xffff740397d0) at /home/qemu-4.0.0/hw/scsi/virtio-scsi.c:559
8b1478
#6  0x000000000052a228 in virtio_scsi_handle_cmd_vq (s=0x2779ec00, vq=0xffff7c6d7110) at /home/qemu-4.0.0/hw/scsi/virtio-scsi.c:603
8b1478
#7  0x000000000052afa8 in virtio_scsi_data_plane_handle_cmd (vdev=<optimized out>, vq=0xffff7c6d7110) at /home/qemu-4.0.0/hw/scsi/virtio-scsi-dataplane.c:59
8b1478
#8  0x000000000054d94c in virtio_queue_host_notifier_aio_poll (opaque=<optimized out>) at /home/qemu-4.0.0/hw/virtio/virtio.c:2452
8b1478
8b1478
assert(blk_get_aio_context(d->conf.blk) == s->ctx) failed.
8b1478
8b1478
To avoid assertion failed,  moving the "if" after qdev_simple_device_unplug_cb.
8b1478
8b1478
In addition, to avoid another qemu crash below, add aio_disable_external before
8b1478
qdev_simple_device_unplug_cb, which disable the further processing of external clients
8b1478
when doing qdev_simple_device_unplug_cb.
8b1478
(gdb) bt
8b1478
#0  scsi_req_unref (req=0xffff6802c6f0) at hw/scsi/scsi-bus.c:1283
8b1478
#1  0x00000000005294a4 in virtio_scsi_handle_cmd_req_submit (req=<optimized out>,
8b1478
    s=<optimized out>) at /home/qemu-4.0.0/hw/scsi/virtio-scsi.c:589
8b1478
#2  0x000000000052a2a8 in virtio_scsi_handle_cmd_vq (s=s@entry=0x9c90e90,
8b1478
    vq=vq@entry=0xffff7c05f110) at /home/qemu-4.0.0/hw/scsi/virtio-scsi.c:625
8b1478
#3  0x000000000052afd8 in virtio_scsi_data_plane_handle_cmd (vdev=<optimized out>,
8b1478
    vq=0xffff7c05f110) at /home/qemu-4.0.0/hw/scsi/virtio-scsi-dataplane.c:60
8b1478
#4  0x000000000054d97c in virtio_queue_host_notifier_aio_poll (opaque=<optimized out>)
8b1478
    at /home/qemu-4.0.0/hw/virtio/virtio.c:2447
8b1478
#5  0x00000000009b204c in run_poll_handlers_once (ctx=ctx@entry=0x6efea40,
8b1478
    timeout=timeout@entry=0xffff7d7f7308) at util/aio-posix.c:521
8b1478
#6  0x00000000009b2b64 in run_poll_handlers (ctx=ctx@entry=0x6efea40,
8b1478
    max_ns=max_ns@entry=4000, timeout=timeout@entry=0xffff7d7f7308) at util/aio-posix.c:559
8b1478
#7  0x00000000009b2ca0 in try_poll_mode (ctx=ctx@entry=0x6efea40, timeout=0xffff7d7f7308,
8b1478
    timeout@entry=0xffff7d7f7348) at util/aio-posix.c:594
8b1478
#8  0x00000000009b31b8 in aio_poll (ctx=0x6efea40, blocking=blocking@entry=true)
8b1478
    at util/aio-posix.c:636
8b1478
#9  0x00000000006973cc in iothread_run (opaque=0x6ebd800) at iothread.c:75
8b1478
#10 0x00000000009b592c in qemu_thread_start (args=0x6efef60) at util/qemu-thread-posix.c:502
8b1478
#11 0x0000ffff8057f8bc in start_thread () from /lib64/libpthread.so.0
8b1478
#12 0x0000ffff804e5f8c in thread_start () from /lib64/libc.so.6
8b1478
(gdb) p bus
8b1478
$1 = (SCSIBus *) 0x0
8b1478
8b1478
Signed-off-by: Zhengui li <lizhengui@huawei.com>
8b1478
Message-Id: <1563696502-7972-1-git-send-email-lizhengui@huawei.com>
8b1478
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
8b1478
Message-Id: <1563829520-17525-1-git-send-email-pbonzini@redhat.com>
8b1478
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
8b1478
(cherry picked from commit 9c5aad84da1c37429d06c193f23a8df6445ed29e)
8b1478
[slp: we need to get a reference to the underlying BDS to be able
8b1478
to switch the context after calling qdev_simple_device_unplug_cb(),
8b1478
as in 2.12 this causes the SCSIDevice to go away immediately]
8b1478
Signed-off-by: Sergio Lopez <slp@redhat.com>
8b1478
8b1478
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
8b1478
---
8b1478
 hw/scsi/virtio-scsi.c | 30 ++++++++++++++++++++++++++----
8b1478
 1 file changed, 26 insertions(+), 4 deletions(-)
8b1478
8b1478
diff --git a/hw/scsi/virtio-scsi.c b/hw/scsi/virtio-scsi.c
8b1478
index 391500b..2bd54b3 100644
8b1478
--- a/hw/scsi/virtio-scsi.c
8b1478
+++ b/hw/scsi/virtio-scsi.c
8b1478
@@ -20,6 +20,7 @@
8b1478
 #include "qemu/error-report.h"
8b1478
 #include "qemu/iov.h"
8b1478
 #include "sysemu/block-backend.h"
8b1478
+#include "sysemu/blockdev.h"
8b1478
 #include "hw/scsi/scsi.h"
8b1478
 #include "scsi/constants.h"
8b1478
 #include "hw/virtio/virtio-bus.h"
8b1478
@@ -839,6 +840,9 @@ static void virtio_scsi_hotunplug(HotplugHandler *hotplug_dev, DeviceState *dev,
8b1478
     VirtIODevice *vdev = VIRTIO_DEVICE(hotplug_dev);
8b1478
     VirtIOSCSI *s = VIRTIO_SCSI(vdev);
8b1478
     SCSIDevice *sd = SCSI_DEVICE(dev);
8b1478
+    AioContext *ctx = s->ctx ?: qemu_get_aio_context();
8b1478
+    BlockDriverState *bs;
8b1478
+    DriveInfo *dinfo;
8b1478
 
8b1478
     if (virtio_vdev_has_feature(vdev, VIRTIO_SCSI_F_HOTPLUG)) {
8b1478
         virtio_scsi_acquire(s);
8b1478
@@ -848,13 +852,31 @@ static void virtio_scsi_hotunplug(HotplugHandler *hotplug_dev, DeviceState *dev,
8b1478
         virtio_scsi_release(s);
8b1478
     }
8b1478
 
8b1478
-    if (s->ctx) {
8b1478
+    /*
8b1478
+     * This SCSIDevice goes away after calling qdev_simple_device_unplug_cb(),
8b1478
+     * so get a reference to the underlying BDS here to be able to switch
8b1478
+     * its AioContext afterwards.
8b1478
+     */
8b1478
+    bs = blk_bs(sd->conf.blk);
8b1478
+
8b1478
+    /*
8b1478
+     * Drives attached to a legacy device will get auto deleted while
8b1478
+     * unplugging the latter, so we don't need to switch their context.
8b1478
+     * Get a reference to dinfo here, which is only NULL for non-legacy
8b1478
+     * devices, and use it to avoid doing the switch for drives attached
8b1478
+     * to legacy devices.
8b1478
+     */
8b1478
+    dinfo = blk_legacy_dinfo(sd->conf.blk);
8b1478
+
8b1478
+    aio_disable_external(ctx);
8b1478
+    qdev_simple_device_unplug_cb(hotplug_dev, dev, errp);
8b1478
+    aio_enable_external(ctx);
8b1478
+
8b1478
+    if (s->ctx && bs && !dinfo) {
8b1478
         virtio_scsi_acquire(s);
8b1478
-        blk_set_aio_context(sd->conf.blk, qemu_get_aio_context());
8b1478
+        bdrv_set_aio_context(bs, qemu_get_aio_context());
8b1478
         virtio_scsi_release(s);
8b1478
     }
8b1478
-
8b1478
-    qdev_simple_device_unplug_cb(hotplug_dev, dev, errp);
8b1478
 }
8b1478
 
8b1478
 static struct SCSIBusInfo virtio_scsi_scsi_info = {
8b1478
-- 
8b1478
1.8.3.1
8b1478