* [PATCH 1/1] virtio-scsi: fix hot-plug event racing device realize
2026-10-23 16:19 [PATCH 0/1] virtio-scsi: fix hot-plug event racing device realize Denis V. Lunev
@ 2026-10-23 16:19 ` Denis V. Lunev
2026-11-02 23:51 ` [PATCH 0/1] " Denis V. Lunev
2026-11-17 19:53 ` Denis V. Lunev
2 siblings, 0 replies; 5+ messages in thread
From: Denis V. Lunev @ 2026-10-23 16:19 UTC (permalink / raw)
To: qemu-devel
Cc: den, qemu-stable, Ilya Rudakov, Paolo Bonzini, Fam Zheng,
Michael S. Tsirkin
From: Denis V. Lunev <den@openvz.org>
virtio_scsi_hotplug() is the HotplugHandler plug() callback, which
device_set_realized() calls before it sets dev->realized. The callback
pushes VIRTIO_SCSI_T_TRANSPORT_RESET/RESCAN to the guest at once, but
until realized is set the request path ignores the new device:
virtio_scsi_device_get() -> scsi_device_get() skips unrealized devices,
and so does REPORT LUNS. A guest that reacts to the event quickly, on a
vCPU while the request queue is served by an iothread, scans the target
and gets VIRTIO_SCSI_S_BAD_TARGET.
Neither Linux nor Windows retries that scan, so the hot-plugged disk or
CD-ROM stays invisible until somebody rescans the bus by hand. Linux
calls scsi_scan_target() once from the event handler. Windows storport
clears its rescan flag before enumerating the bus and never sets it
again for a failed target.
Push the event, and the REPORTED LUNS CHANGED unit attention that goes
with it, from a main-loop bottom half instead. It runs under the BQL,
which device_add holds until device_set_realized() returns, so the guest
is only told about a device it can reach. The iothread context would not
do: its bottom half could run before realized is set, and the realized
check and scsi_bus_set_ua() rely on the BQL as plug() did. The bottom
half is only scheduled when the guest negotiated VIRTIO_SCSI_F_HOTPLUG,
as before, so cold-plugged devices do not get one. It still checks that
the device is realized: nothing orders it against a device_del that
follows device_add, and an unplugged device has no parent bus.
Fixes: 8ddf958e8d62 ("scsi/scsi-bus: scsi_device_find: don't return unrealized devices")
Cc: qemu-stable@nongnu.org
Reported-by: Ilya Rudakov <irudakov@virtuozzo.com>
Signed-off-by: Denis V. Lunev <den@openvz.org>
Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: Fam Zheng <fam@euphon.net>
Cc: "Michael S. Tsirkin" <mst@redhat.com>
---
hw/scsi/virtio-scsi.c | 38 +++++++++++++++++++++++++++-----------
1 file changed, 27 insertions(+), 11 deletions(-)
diff --git a/hw/scsi/virtio-scsi.c b/hw/scsi/virtio-scsi.c
index d7e2662aa7..9ea1acb84c 100644
--- a/hw/scsi/virtio-scsi.c
+++ b/hw/scsi/virtio-scsi.c
@@ -1151,6 +1151,29 @@ static void virtio_scsi_pre_hotplug(const HotplugHandler *hotplug_dev,
sd->hba_supports_iothread = true;
}
+static void virtio_scsi_hotplug_notify_bh(void *opaque)
+{
+ SCSIDevice *sd = opaque;
+ DeviceState *dev = DEVICE(sd);
+
+ /* Nothing orders us against a following device_del */
+ if (qdev_is_realized(dev)) {
+ VirtIOSCSI *s = VIRTIO_SCSI(dev->parent_bus->parent);
+ VirtIOSCSIEventInfo info = {
+ .event = VIRTIO_SCSI_T_TRANSPORT_RESET,
+ .reason = VIRTIO_SCSI_EVT_RESET_RESCAN,
+ .address = {
+ .id = sd->id,
+ .lun = sd->lun,
+ },
+ };
+
+ virtio_scsi_push_event(s, &info);
+ scsi_bus_set_ua(&s->bus, SENSE_CODE(REPORTED_LUNS_CHANGED));
+ }
+ object_unref(OBJECT(sd));
+}
+
static void virtio_scsi_hotplug(const HotplugHandler *hotplug_dev, DeviceState *dev,
Error **errp)
{
@@ -1169,17 +1192,10 @@ static void virtio_scsi_hotplug(const HotplugHandler *hotplug_dev, DeviceState *
}
if (virtio_vdev_has_feature(vdev, VIRTIO_SCSI_F_HOTPLUG)) {
- VirtIOSCSIEventInfo info = {
- .event = VIRTIO_SCSI_T_TRANSPORT_RESET,
- .reason = VIRTIO_SCSI_EVT_RESET_RESCAN,
- .address = {
- .id = sd->id,
- .lun = sd->lun,
- },
- };
-
- virtio_scsi_push_event(s, &info);
- scsi_bus_set_ua(&s->bus, SENSE_CODE(REPORTED_LUNS_CHANGED));
+ /* The request path ignores @sd until it is realized, so notify after */
+ object_ref(OBJECT(sd));
+ aio_bh_schedule_oneshot(qemu_get_aio_context(),
+ virtio_scsi_hotplug_notify_bh, sd);
}
}
base-commit: b6c4b7e86dec12d07f5cac65265b986d38ee9002
--
2.53.0
^ permalink raw reply related [flat|nested] 5+ messages in thread* Re: [PATCH 0/1] virtio-scsi: fix hot-plug event racing device realize
2026-10-23 16:19 [PATCH 0/1] virtio-scsi: fix hot-plug event racing device realize Denis V. Lunev
2026-10-23 16:19 ` [PATCH 1/1] " Denis V. Lunev
@ 2026-11-02 23:51 ` Denis V. Lunev
2026-11-17 19:53 ` Denis V. Lunev
2 siblings, 0 replies; 5+ messages in thread
From: Denis V. Lunev @ 2026-11-02 23:51 UTC (permalink / raw)
To: Denis V. Lunev, qemu-devel; +Cc: Paolo Bonzini, Fam Zheng, Michael S. Tsirkin
On 10/23/26 18:19, Denis V. Lunev wrote:
> This email originated from an IP that might not be authorized by the domain it was sent from.
> Do not click links or open attachments unless it is an email you expected to receive.
> A disk or CD-ROM hot-plugged into virtio-scsi sometimes never shows up
> in the guest. QEMU has the device, the guest has consumed the RESCAN
> event, and a manual bus rescan finds the LUN at once. We see this in CI
> with Windows Server guests (vioscsi) and with Linux guests, always with
> the controller on an iothread.
>
> The event is pushed from the plug() callback, before the device is
> realized, and the iothread request path does not see unrealized devices
> since 8ddf958e8d62. A guest that scans right away gets BAD_TARGET and
> does not retry. The patch moves the event to a main-loop bottom half.
> The alternative, letting the request path see a plugged but unrealized
> device, would undo what 8ddf958e8d62 protects against.
>
> There is no test in the patch. The window lies inside a single
> device_add under the BQL, and a qtest or iotests client is served by the
> same main loop, so it cannot send a request in between. To reproduce, I
> booted a Linux guest with virtio-scsi on an iothread, ran QEMU under gdb
> in non-stop mode and stalled the main thread for 3 s in
> scsi_bus_set_ua(), right after the event is pushed, then hot-plugged a
> scsi-cd:
>
> break scsi_bus_set_ua
> commands
> call (int)usleep(3000000)
> continue
> end
>
> Without the patch the guest never enumerated the LUN (3 of 3 runs).
> With the patch the same stall falls after realize and the LUN appeared
> every time (3 of 3). Without the stall both builds pass, which is why
> this only shows up on loaded hosts. tests/qtest virtio-scsi passes.
>
> Every release since 5.2 has the bug, hence qemu-stable.
>
> Signed-off-by: Denis V. Lunev <den@openvz.org>
> Cc: Paolo Bonzini <pbonzini@redhat.com>
> Cc: Fam Zheng <fam@euphon.net>
> Cc: "Michael S. Tsirkin" <mst@redhat.com>
>
> Denis V. Lunev (1):
> virtio-scsi: fix hot-plug event racing device realize
>
> hw/scsi/virtio-scsi.c | 38 +++++++++++++++++++++++++++-----------
> 1 file changed, 27 insertions(+), 11 deletions(-)
>
>
> base-commit: b6c4b7e86dec12d07f5cac65265b986d38ee9002
ping
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH 0/1] virtio-scsi: fix hot-plug event racing device realize
2026-10-23 16:19 [PATCH 0/1] virtio-scsi: fix hot-plug event racing device realize Denis V. Lunev
2026-10-23 16:19 ` [PATCH 1/1] " Denis V. Lunev
2026-11-02 23:51 ` [PATCH 0/1] " Denis V. Lunev
@ 2026-11-17 19:53 ` Denis V. Lunev
2026-11-19 7:57 ` Michael S. Tsirkin
2 siblings, 1 reply; 5+ messages in thread
From: Denis V. Lunev @ 2026-11-17 19:53 UTC (permalink / raw)
To: Denis V. Lunev, qemu-devel; +Cc: Paolo Bonzini, Fam Zheng, Michael S. Tsirkin
On 10/23/26 18:19, Denis V. Lunev wrote:
> This email originated from an IP that might not be authorized by the domain it was sent from.
> Do not click links or open attachments unless it is an email you expected to receive.
> A disk or CD-ROM hot-plugged into virtio-scsi sometimes never shows up
> in the guest. QEMU has the device, the guest has consumed the RESCAN
> event, and a manual bus rescan finds the LUN at once. We see this in CI
> with Windows Server guests (vioscsi) and with Linux guests, always with
> the controller on an iothread.
>
> The event is pushed from the plug() callback, before the device is
> realized, and the iothread request path does not see unrealized devices
> since 8ddf958e8d62. A guest that scans right away gets BAD_TARGET and
> does not retry. The patch moves the event to a main-loop bottom half.
> The alternative, letting the request path see a plugged but unrealized
> device, would undo what 8ddf958e8d62 protects against.
>
> There is no test in the patch. The window lies inside a single
> device_add under the BQL, and a qtest or iotests client is served by the
> same main loop, so it cannot send a request in between. To reproduce, I
> booted a Linux guest with virtio-scsi on an iothread, ran QEMU under gdb
> in non-stop mode and stalled the main thread for 3 s in
> scsi_bus_set_ua(), right after the event is pushed, then hot-plugged a
> scsi-cd:
>
> break scsi_bus_set_ua
> commands
> call (int)usleep(3000000)
> continue
> end
>
> Without the patch the guest never enumerated the LUN (3 of 3 runs).
> With the patch the same stall falls after realize and the LUN appeared
> every time (3 of 3). Without the stall both builds pass, which is why
> this only shows up on loaded hosts. tests/qtest virtio-scsi passes.
>
> Every release since 5.2 has the bug, hence qemu-stable.
>
> Signed-off-by: Denis V. Lunev <den@openvz.org>
> Cc: Paolo Bonzini <pbonzini@redhat.com>
> Cc: Fam Zheng <fam@euphon.net>
> Cc: "Michael S. Tsirkin" <mst@redhat.com>
>
> Denis V. Lunev (1):
> virtio-scsi: fix hot-plug event racing device realize
>
> hw/scsi/virtio-scsi.c | 38 +++++++++++++++++++++++++++-----------
> 1 file changed, 27 insertions(+), 11 deletions(-)
>
>
> base-commit: b6c4b7e86dec12d07f5cac65265b986d38ee9002
ping
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH 0/1] virtio-scsi: fix hot-plug event racing device realize
2026-11-17 19:53 ` Denis V. Lunev
@ 2026-11-19 7:57 ` Michael S. Tsirkin
0 siblings, 0 replies; 5+ messages in thread
From: Michael S. Tsirkin @ 2026-11-19 7:57 UTC (permalink / raw)
To: Denis V. Lunev; +Cc: Denis V. Lunev, qemu-devel, Paolo Bonzini, Fam Zheng
On Tue, Nov 17, 2026 at 08:53:44PM +0100, Denis V. Lunev wrote:
> On 10/23/26 18:19, Denis V. Lunev wrote:
> > This email originated from an IP that might not be authorized by the domain it was sent from.
> > Do not click links or open attachments unless it is an email you expected to receive.
> > A disk or CD-ROM hot-plugged into virtio-scsi sometimes never shows up
> > in the guest. QEMU has the device, the guest has consumed the RESCAN
> > event, and a manual bus rescan finds the LUN at once. We see this in CI
> > with Windows Server guests (vioscsi) and with Linux guests, always with
> > the controller on an iothread.
> >
> > The event is pushed from the plug() callback, before the device is
> > realized, and the iothread request path does not see unrealized devices
> > since 8ddf958e8d62. A guest that scans right away gets BAD_TARGET and
> > does not retry. The patch moves the event to a main-loop bottom half.
> > The alternative, letting the request path see a plugged but unrealized
> > device, would undo what 8ddf958e8d62 protects against.
> >
> > There is no test in the patch. The window lies inside a single
> > device_add under the BQL, and a qtest or iotests client is served by the
> > same main loop, so it cannot send a request in between. To reproduce, I
> > booted a Linux guest with virtio-scsi on an iothread, ran QEMU under gdb
> > in non-stop mode and stalled the main thread for 3 s in
> > scsi_bus_set_ua(), right after the event is pushed, then hot-plugged a
> > scsi-cd:
> >
> > break scsi_bus_set_ua
> > commands
> > call (int)usleep(3000000)
> > continue
> > end
> >
> > Without the patch the guest never enumerated the LUN (3 of 3 runs).
> > With the patch the same stall falls after realize and the LUN appeared
> > every time (3 of 3). Without the stall both builds pass, which is why
> > this only shows up on loaded hosts. tests/qtest virtio-scsi passes.
> >
> > Every release since 5.2 has the bug, hence qemu-stable.
> >
> > Signed-off-by: Denis V. Lunev <den@openvz.org>
> > Cc: Paolo Bonzini <pbonzini@redhat.com>
> > Cc: Fam Zheng <fam@euphon.net>
> > Cc: "Michael S. Tsirkin" <mst@redhat.com>
> >
> > Denis V. Lunev (1):
> > virtio-scsi: fix hot-plug event racing device realize
> >
> > hw/scsi/virtio-scsi.c | 38 +++++++++++++++++++++++++++-----------
> > 1 file changed, 27 insertions(+), 11 deletions(-)
> >
> >
> > base-commit: b6c4b7e86dec12d07f5cac65265b986d38ee9002
> ping
pong. I'm not applying patches I'm not CC'd on. Paolo, wanna take
a look?
^ permalink raw reply [flat|nested] 5+ messages in thread