Skip to contentSkip to conversation

scsi: virtio_scsi: bound EH timer resets to avoid unkillable hang

RFC PATCH In review Nguyen Ngoc Thang 6 participants 14 messages Sep 11 – Nov 9

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 scsi: virtio_scsi: bound EH timer resets to avoid unkillable hang

("INFO: task hung in __rq_qos_throttle (2)", still open upstream, no fix, bisection failed, 97 crashes since 2026-08-08)

virtscsi_eh_timed_out() always returns SCSI_EH_RESET_TIMER, trusting the host to eventually answer every command. A privileged raw write to /sys/bus/pci/devices/*/config that clears PCI_COMMAND_MASTER on the disk breaks that assumption: the device can no longer DMA, so no completion -- real or error -- ever arrives, and the reset loop runs forever. Any submitter that then hits wbt's inflight limit blocks uninterruptibly in wbt_wait() with no way to recover.

The syzbot C reproducer does exactly this: it opens the disk's PCI config sysfs file and pwrite()s the two bytes 02 00 at offset 4 (PCI_COMMAND), clearing PCI_COMMAND_MASTER while keeping PCI_COMMAND_MEMORY set. The hung task is a writeback worker stuck in wbt_wait() <- __rq_qos_throttle() <- blk_mq_get_new_requests(), on a disk named sdaN -- i.e. a SCSI-model disk, consistent with virtio_scsi rather than virtio-blk.

Bound the resets: after VIRTSCSI_EH_RESET_LIMIT tries (~150s), let real SCSI EH run (abort -> device reset -> offline), which fails the command and unblocks rq_qos waiters. A host that is merely slow for longer than that now gets its commands aborted/offlined instead of waited on indefinitely -- a deliberate tradeoff.

virtscsi_tmf(), used by both abort and device-reset handlers, waited on the same broken ctrl virtqueue unboundedly, which would just move the hang into the EH thread. Bound it too. On timeout the command is left queued on the ctrl vq (a device that answers late must still be able to find it), so it is not freed. Clearing cmd->comp so a late completion doesn't write through the now-invalid on-stack completion has to be serialized against virtscsi_complete_free(), which reads cmd->comp and calls complete() on it under ctrl_vq.vq_lock -- taking that same lock around the check-and-clear (and re-checking completion_done() inside it) closes the race instead of just shrinking it. The leak is one virtio_scsi_cmd per timed-out TMF, bounded by queue depth, on a path where the device is being offlined anyway.

Verified with a QEMU virtio-scsi repro that reproduces the syzbot mechanism above (pwrite of 02 00 at PCI config offset 4 on the sda backing device, mid-writeback, via /sys/bus/pci/devices/*/config):
unpatched, a request sits stuck past its own 30s timeout with zero EH activity even after 100+s; patched, eh_resets increments deterministically, SCSI EH aborts/resets/offlines the device at the expected ~150-190s mark, and both previously-stuck writers come back with -EIO instead of hanging. Full dmesg from the patched run was checked for WARN/BUG/lockdep output around the new ctrl_vq.vq_lock critical section in virtscsi_tmf(); none appeared.

---
 drivers/scsi/virtio_scsi.c | 51 ++++++++++++++++++++++++++++++++++----
 1 file changed, 46 insertions(+), 5 deletions(-)
drivers/scsi/virtio_scsi.c +46 -5
index 35731b18c519..b4f20c487718 100644
@@ -37,6 +37,11 @@
 #define VIRTIO_SCSI_EVENT_LEN 8
 #define VIRTIO_SCSI_VQ_BASE 2
 
+/* Max timer resets in virtscsi_eh_timed_out() before letting real EH run. */
+#define VIRTSCSI_EH_RESET_LIMIT 5
+/* How long to let the host answer an abort/reset TMF before giving up. */
+#define VIRTSCSI_TMF_TIMEOUT (10 * HZ)
+
 static unsigned int virtscsi_poll_queues;
 module_param(virtscsi_poll_queues, uint, 0644);
 MODULE_PARM_DESC(virtscsi_poll_queues,
@@ -46,6 +51,7 @@ MODULE_PARM_DESC(virtscsi_poll_queues,
 struct virtio_scsi_cmd {
 	struct scsi_cmnd *sc;
 	struct completion *comp;
+	unsigned int eh_resets;
 	union {
 		struct virtio_scsi_cmd_req       cmd;
 		struct virtio_scsi_cmd_req_pi    cmd_pi;
@@ -586,6 +592,7 @@ static enum scsi_qc_status virtscsi_queuecommand(struct Scsi_Host *shost,
 		"cmd %p CDB: %#02x\n", sc, sc->cmnd[0]);
 
 	cmd->sc = sc;
+	cmd->eh_resets = 0;
 
 	BUG_ON(sc->cmd_len > VIRTIO_SCSI_CDB_SIZE);
 
@@ -625,7 +632,35 @@ static int virtscsi_tmf(struct virtio_scsi *vscsi, struct virtio_scsi_cmd *cmd)
 			      sizeof cmd->req.tmf, sizeof cmd->resp.tmf, true) < 0)
 		goto out;
 
-	wait_for_completion(&comp);
+	if (!wait_for_completion_timeout(&comp, VIRTSCSI_TMF_TIMEOUT)) {
+		unsigned long flags;
+		bool completed;
+
+		/*
+		 * No answer within the timeout. virtscsi_complete_free()
+		 * reads cmd->comp and calls complete() on it under
+		 * ctrl_vq.vq_lock, so take the same lock to decide, atomically
+		 * with that path, whether the completion already happened.
+		 *
+		 * If it hasn't: clear cmd->comp so a completion that arrives
+		 * after we drop the lock finds NULL and leaves this
+		 * soon-to-be-invalid stack frame alone. cmd stays queued on
+		 * the ctrl vq (a device that answers late must still be able
+		 * to find it), so it is not freed here.
+		 *
+		 * If it has: the response landed (and complete() already ran)
+		 * right as we timed out, so fall through and read it as if
+		 * wait_for_completion_timeout() had succeeded.
+		 */
+		spin_lock_irqsave(&vscsi->ctrl_vq.vq_lock, flags);
+		completed = completion_done(&comp);
+		if (!completed)
+			cmd->comp = NULL;
+		spin_unlock_irqrestore(&vscsi->ctrl_vq.vq_lock, flags);
+
+		if (!completed)
+			return FAILED;
+	}
 	if (cmd->resp.tmf.response == VIRTIO_SCSI_S_OK ||
 	    cmd->resp.tmf.response == VIRTIO_SCSI_S_FUNCTION_SUCCEEDED)
 		ret = SUCCESS;
@@ -783,13 +818,19 @@ static void virtscsi_commit_rqs(struct Scsi_Host *shost, u16 hwq)
 }
 
 /*
- * The host guarantees to respond to each command, although I/O
- * latencies might be higher than on bare metal.  Reset the timer
- * unconditionally to give the host a chance to perform EH.
+ * The host normally answers every command, so reset the timer and keep
+ * waiting. But if the transport is broken (e.g. bus mastering was turned
+ * off), no completion can ever arrive: give up after a few resets so SCSI
+ * EH fails the command instead of blocking its submitter forever.
  */
 static enum scsi_timeout_action virtscsi_eh_timed_out(struct scsi_cmnd *scmnd)
 {
-	return SCSI_EH_RESET_TIMER;
+	struct virtio_scsi_cmd *cmd = scsi_cmd_priv(scmnd);
+
+	if (++cmd->eh_resets < VIRTSCSI_EH_RESET_LIMIT)
+		return SCSI_EH_RESET_TIMER;
+
+	return SCSI_EH_NOT_HANDLED;
 }
 
 static const struct scsi_host_template virtscsi_host_template = {
Signature · 1 line
2.43.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Critical] Missing eh_host_reset_handler causes SCSI EH to free commands while the host is active…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Critical] Missing eh_host_reset_handler causes SCSI EH to free commands while the host is active, resulting in DMA memory corruption and Use-After-Free. --

commit c1b61825f071ba8c8020eead463039fdb142c7b0
Author: Nguyen Ngoc Thang <[email protected]>

scsi: virtio_scsi: bound EH timer resets to avoid unkillable hang

This commit limits the number of times virtscsi_eh_timed_out() can reset the command timer to 5, returning SCSI_EH_NOT_HANDLED afterward. It also bounds the wait time in virtscsi_tmf() when handling aborts and resets to prevent infinite hangs if the transport breaks.

Quoted 4 lines
drivers/scsi/virtio_scsi.c +0 -0
index 35731b18c519e..b4f20c4877182 100644

[ ... ]

Quoted 11 lines
drivers/scsi/virtio_scsi.c +4 -3
@@ -783,13 +818,19 @@ static void virtscsi_commit_rqs(struct Scsi_Host *shost, u16 hwq)
 }
 
 /*
- * The host guarantees to respond to each command, although I/O
- * latencies might be higher than on bare metal.  Reset the timer
- * unconditionally to give the host a chance to perform EH.
+ * The host normally answers every command, so reset the timer and keep
+ * waiting. But if the transport is broken (e.g. bus mastering was turned
+ * off), no completion can ever arrive: give up after a few resets so SCSI
+ * EH fails the command instead of blocking its submitter forever.

drivers/scsi/virtio_scsi.c static void virtscsi_commit_rqs(struct Scsi_Host *shost, u16 hwq) · line 833

Quoted 10 lines
drivers/scsi/virtio_scsi.c +6 -1
  */
 static enum scsi_timeout_action virtscsi_eh_timed_out(struct scsi_cmnd *scmnd)
 {
-	return SCSI_EH_RESET_TIMER;
+	struct virtio_scsi_cmd *cmd = scsi_cmd_priv(scmnd);
+
+	if (++cmd->eh_resets < VIRTSCSI_EH_RESET_LIMIT)
+		return SCSI_EH_RESET_TIMER;
+
+	return SCSI_EH_NOT_HANDLED;

[Severity: Critical]
Does returning SCSI_EH_NOT_HANDLED here allow SCSI EH to run and eventually free the command without ensuring the host has dropped its DMA references?

If the virtio host experiences a severe delay and eventually completes the timed-out commands, could it DMA the response directly into memory that has already been freed by SCSI EH and potentially reallocated to new requests?

If this happens, wouldn't it result in a use-after-free and DMA memory corruption when virtscsi_complete_cmd() runs?

For example:

virtscsi_complete_cmd() {
	struct virtio_scsi_cmd *cmd = buf;
	struct scsi_cmnd *sc = cmd->sc;
	struct virtio_scsi_cmd_resp *resp = &cmd->resp.cmd;
	...
	scsi_done(sc);
}

Here the dangling pointer from the virtqueue would be dereferenced after the command has been freed by SCSI EH, potentially followed by a kernel panic when scsi_done() is called on the corrupted scsi_cmnd pointer.

drivers/scsi/virtio_scsi.c static void virtscsi_commit_rqs(struct Scsi_Host *shost, u16 hwq) · line 836

Quoted 3 lines
drivers/scsi/virtio_scsi.c +0 -0
 }
 
 static const struct scsi_host_template virtscsi_host_template = {

[Severity: Critical]
Does the host template lack an eh_host_reset_handler?

Without this, SCSI EH cannot safely stop the virtio device before freeing the timed-out commands. If SCSI EH attempts aborts and device resets which also time out, it will give up, offline the device, and flush (free) the commands. When the host later resumes and processes the virtqueue, could it overwrite the freed memory?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw

Thank you for the review -- the eh_host_reset_handler finding is correct, and the underlying mechanism is worse than a stale read.

Once virtscsi_eh_timed_out() lets SCSI EH run to completion on an unresponsive host, scsi_eh_bus_device_reset() leaves the command in work_q (device reset fails the same way abort does, via the same bounded virtscsi_tmf()), and since virtio_scsi implements neither eh_target_reset_handler, eh_bus_reset_handler nor eh_host_reset_handler, scsi_eh_target_reset()/scsi_eh_bus_reset()/scsi_eh_host_reset() all fail immediately (scsi_try_*_reset() return FAILED when the handler pointer is NULL) and the command falls through to scsi_eh_offline_sdevs(), which calls scsi_eh_finish_cmd() and frees the tag back to the block layer.

The struct virtio_scsi_cmd for that command lives in scsi_cmd_priv(), i.e. inside the now-freed request. If a new command is queued on the same tag, it gets the same address. If the host was only very slow -- not actually dead -- and eventually completes the *original* descriptor, virtscsi_complete_cmd() will read cmd->sc from that shared address and call scsi_done() on whatever command currently occupies it, which by then is the live, unrelated command still in flight on that tag. That's a double completion on a request the driver itself has not finished, not merely a read of stale memory.

The original virtscsi_eh_timed_out() comment ("The host guarantees to respond to each command... Reset the timer unconditionally") reads, in this light, less like an optimistic assumption and more like the invariant that kept this reachable at all: as long as EH could never progress past abort/device-reset into offline+free, the tag-reuse race had no opening. My patch removes that invariant to fix the wbt hang, but doesn't supply a replacement, which is the hole you're pointing at.

What I looked at as a replacement and why I'm not sending it as a v2 yet:

Adding eh_host_reset_handler that mirrors virtscsi_freeze()/ virtscsi_restore() (virtscsi_remove_vqs() + virtscsi_init()) was my first instinct. virtio_reset_device() does call virtio_break_device() + virtio_synchronize_cbs() before dev->config->reset(), which is meant to guarantee no vq callback is still in flight when del_vqs() runs -- but that pairing is compiled in only under CONFIG_VIRTIO_HARDEN_NOTIFICATION (drivers/virtio/virtio.c:255-264), so the safety property isn't universal. Separately, a host reset triggered by one wedged command on one LUN would tear down and reinitialize every virtqueue on the host, silently abandoning any genuinely in-flight, unrelated I/O on other LUNs/targets that scsi_error_handler() never queued for recovery in the first place -- freeze/restore can rely on PM having fully quiesced the block queues first; EH's SHOST_RECOVERY only blocks new submissions, it doesn't drain what's already dispatched. I don't want to trade the tag-reuse race for a config-dependent fix plus silent collateral I/O loss without your input on whether that tradeoff is acceptable or whether there's a narrower primitive intended for this.

Is virtio_break_device() (independent of host reset, and independent of CONFIG_VIRTIO_HARDEN_NOTIFICATION) the intended way to make a single wedged command's descriptor permanently safe to let go of, without resetting the whole device? It looks close -- virtqueue_get_buf() checks vq->broken before touching the used ring at all -- but it's one-way (no unbreak short of recreating the vq), so I'm not certain it's meant to be used outside device teardown. Has this class of problem (bound a SCSI EH timeout on a transport that can't cheaply prove a command is gone) come up for virtio_scsi or virtio_blk before, and is there a pattern I should be following instead of inventing one?

Happy to do the legwork on whichever direction you point at -- I have a QEMU virtio-scsi repro harness already wired up for the original hang (clearing PCI_COMMAND_MASTER mid-write) that I can extend to exercise the recovery path too.

#3 lore raw
Quoted 13 lines · Nguyen Ngoc Thang

Thank you for the review -- the eh_host_reset_handler finding is correct, and the underlying mechanism is worse than a stale read.

Once virtscsi_eh_timed_out() lets SCSI EH run to completion on an unresponsive host, scsi_eh_bus_device_reset() leaves the command in work_q (device reset fails the same way abort does, via the same bounded virtscsi_tmf()), and since virtio_scsi implements neither eh_target_reset_handler, eh_bus_reset_handler nor eh_host_reset_handler, scsi_eh_target_reset()/scsi_eh_bus_reset()/scsi_eh_host_reset() all fail immediately (scsi_try_*_reset() return FAILED when the handler pointer is NULL) and the command falls through to scsi_eh_offline_sdevs(), which calls scsi_eh_finish_cmd() and frees the tag back to the block layer.

[ .. ]

Quoted 6 lines

Happy to do the legwork on whichever direction you point at -- I have a QEMU virtio-scsi repro harness already wired up for the original hang (clearing PCI_COMMAND_MASTER mid-write) that I can extend to exercise the recovery path too.

In short: you can't.
The linux kernel lacks the ability to abort an outstanding I/O from userspace; userspace (and that includes qemu) _has_ to wait for that I/O to return.
Otherwise there is no guarantee that the DMA regions of that command
are ever freed, and the kernel can happily scribble over memory which
userspace already assumed to be free. As these DMA regions are mapped
into the qemu guest you essentially allow the kernel to overwrite guest
memory at any time.
Not a good idea.

In general: SCSI host_reset _is_ the bug hammer. If that doesn't work (for whatever reason) you system is hosed as you can _never_ get the memory and DMA regions of outstanding commands back to the system. So the better fix would be to implement a host_reset() callback for virtio-scsi which would reset the virtio setup, terminating all outstanding commands and reset the queues.

Cheers,

Hannes

Signature · 4 lines
Dr. Hannes Reinecke                  Kernel Storage Architect
[email protected]                                +49 911 74053 688
SUSE Software Solutions GmbH, Frankenstr. 146, 90461 Nürnberg
HRB 36809 (AG Nürnberg), GF: I. Totev, A. McDonald, W. Knoblich
#4 lore raw

PATCH v2 1/1 scsi: virtio_scsi: implement eh_host_reset_handler

Once virtscsi_eh_timed_out() lets SCSI EH run on an unresponsive host, abort and device reset both fail the same way (same dead ctrl vq), and since virtio_scsi implements neither target nor bus reset, EH falls through to scsi_eh_offline_sdevs(), which frees the stuck commands' DMA buffers without any guarantee the device has actually stopped touching them. If bus mastering (or whatever broke the transport) comes back later, the device can still write into memory the kernel has already reused.

Add a real eh_host_reset_handler instead. virtio_reset_device() is the guarantee scsi_eh_offline_sdevs() was missing: once it returns, the device will not touch guest memory again, so it is safe to hand outstanding commands' buffers back. Reuse virtscsi_remove_vqs() + virtscsi_init() -- the exact sequence already used across suspend/resume -- to tear the virtqueues down and rebuild them.

Verified with a QEMU virtio-scsi repro (PCI_COMMAND_MASTER cleared mid-write): EH now runs abort -> device reset -> host reset -> abort -> device reset -> host reset -> offline, then dd's stuck fsync returns EIO and D-state drains to 0, in ~244s total. No task remains uninterruptibly blocked.

---
v1 -> v2: implemented eh_host_reset_handler as suggested by Hannes Reinecke in review of the original RFC, instead of relying solely on the EH loop falling through to offline.

---
 drivers/scsi/virtio_scsi.c | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)
drivers/scsi/virtio_scsi.c +24 -0
index b4f20c487718..9698f4f91cc6 100644
@@ -833,6 +833,8 @@ static enum scsi_timeout_action virtscsi_eh_timed_out(struct scsi_cmnd *scmnd)
 	return SCSI_EH_NOT_HANDLED;
 }
 
+static int virtscsi_host_reset(struct scsi_cmnd *sc);
+
 static const struct scsi_host_template virtscsi_host_template = {
 	.module = THIS_MODULE,
 	.name = "Virtio SCSI HBA",
@@ -846,6 +848,7 @@ static const struct scsi_host_template virtscsi_host_template = {
 	.eh_abort_handler = virtscsi_abort,
 	.eh_device_reset_handler = virtscsi_device_reset,
 	.eh_timed_out = virtscsi_eh_timed_out,
+	.eh_host_reset_handler = virtscsi_host_reset,
 	.sdev_init = virtscsi_device_alloc,
 
 	.dma_boundary = UINT_MAX,
@@ -947,6 +950,27 @@ static int virtscsi_init(struct virtio_device *vdev,
 	return err;
 }
 
+/*
+ * No bus/target reset in virtio-scsi, so EH lands here after device
+ * reset also times out. A virtio reset guarantees the device won't
+ * touch guest memory again, so stuck commands can be freed safely.
+ * Same teardown/rebuild already used for suspend/resume.
+ */
+static int virtscsi_host_reset(struct scsi_cmnd *sc)
+{
+	struct virtio_scsi *vscsi = shost_priv(sc->device->host);
+	struct virtio_device *vdev = vscsi->vdev;
+
+	scmd_printk(KERN_INFO, sc, "host reset\n");
+
+	virtscsi_remove_vqs(vdev);
+	if (virtscsi_init(vdev, vscsi))
+		return FAILED;
+
+	virtio_device_ready(vdev);
+	return SUCCESS;
+}
+
 static int virtscsi_probe(struct virtio_device *vdev)
 {
 	struct Scsi_Host *shost;
Signature · 1 line
2.43.0
#5 lore raw

Re: [PATCH v2] scsi: virtio_scsi: implement eh_host_reset_handler

Quoted 16 lines · Nguyen Ngoc Thang

Once virtscsi_eh_timed_out() lets SCSI EH run on an unresponsive host, abort and device reset both fail the same way (same dead ctrl vq), and since virtio_scsi implements neither target nor bus reset, EH falls through to scsi_eh_offline_sdevs(), which frees the stuck commands' DMA buffers without any guarantee the device has actually stopped touching them. If bus mastering (or whatever broke the transport) comes back later, the device can still write into memory the kernel has already reused.

Add a real eh_host_reset_handler instead. virtio_reset_device() is the guarantee scsi_eh_offline_sdevs() was missing: once it returns, the device will not touch guest memory again, so it is safe to hand outstanding commands' buffers back. Reuse virtscsi_remove_vqs() + virtscsi_init() -- the exact sequence already used across suspend/resume -- to tear the virtqueues down and rebuild them.

Quoted 8 lines

Verified with a QEMU virtio-scsi repro (PCI_COMMAND_MASTER cleared mid-write): EH now runs abort -> device reset -> host reset -> abort -> device reset -> host reset -> offline, then dd's stuck fsync returns EIO and D-state drains to 0, in ~244s total. No task remains uninterruptibly blocked.

This is not posted correctly:

- start a nbew thread do not reply
- against some tree which is not upstream

I papered over it

Quoted 13 lines

---
v1 -> v2: implemented eh_host_reset_handler as suggested by Hannes Reinecke in review of the original RFC, instead of relying solely on the EH loop falling through to offline.

---
 drivers/scsi/virtio_scsi.c | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)
drivers/scsi/virtio_scsi.c +0 -0
index b4f20c487718..9698f4f91cc6 100644

drivers/scsi/virtio_scsi.c static enum scsi_timeout_action virtscsi_eh_timed_out(struct scsi_cmnd *scmnd) · line 837

Quoted 5 lines
drivers/scsi/virtio_scsi.c +2 -0
@@ -833,6 +833,8 @@ static enum scsi_timeout_action virtscsi_eh_timed_out(struct scsi_cmnd *scmnd)
 	return SCSI_EH_NOT_HANDLED;
 }
 
+static int virtscsi_host_reset(struct scsi_cmnd *sc);
+

so move the function here?

Quoted 42 lines
drivers/scsi/virtio_scsi.c +22 -0
 static const struct scsi_host_template virtscsi_host_template = {
 	.module = THIS_MODULE,
 	.name = "Virtio SCSI HBA",
@@ -846,6 +848,7 @@ static const struct scsi_host_template virtscsi_host_template = {
 	.eh_abort_handler = virtscsi_abort,
 	.eh_device_reset_handler = virtscsi_device_reset,
 	.eh_timed_out = virtscsi_eh_timed_out,
+	.eh_host_reset_handler = virtscsi_host_reset,
 	.sdev_init = virtscsi_device_alloc,
 
 	.dma_boundary = UINT_MAX,
@@ -947,6 +950,27 @@ static int virtscsi_init(struct virtio_device *vdev,
 	return err;
 }
 
+/*
+ * No bus/target reset in virtio-scsi, so EH lands here after device
+ * reset also times out. A virtio reset guarantees the device won't
+ * touch guest memory again, so stuck commands can be freed safely.
+ * Same teardown/rebuild already used for suspend/resume.
+ */
+static int virtscsi_host_reset(struct scsi_cmnd *sc)
+{
+	struct virtio_scsi *vscsi = shost_priv(sc->device->host);
+	struct virtio_device *vdev = vscsi->vdev;
+
+	scmd_printk(KERN_INFO, sc, "host reset\n");
+
+	virtscsi_remove_vqs(vdev);
+	if (virtscsi_init(vdev, vscsi))
+		return FAILED;
+
+	virtio_device_ready(vdev);
+	return SUCCESS;
+}
+
 static int virtscsi_probe(struct virtio_device *vdev)
 {
 	struct Scsi_Host *shost;
Signature · 1 line
2.43.0
#6 lore raw

Re: [PATCH v2] scsi: virtio_scsi: implement eh_host_reset_handler

In reply to Nguyen Ngoc Thang
Quoted 16 lines · Nguyen Ngoc Thang

Once virtscsi_eh_timed_out() lets SCSI EH run on an unresponsive host, abort and device reset both fail the same way (same dead ctrl vq), and since virtio_scsi implements neither target nor bus reset, EH falls through to scsi_eh_offline_sdevs(), which frees the stuck commands' DMA buffers without any guarantee the device has actually stopped touching them. If bus mastering (or whatever broke the transport) comes back later, the device can still write into memory the kernel has already reused.

Add a real eh_host_reset_handler instead. virtio_reset_device() is the guarantee scsi_eh_offline_sdevs() was missing: once it returns, the device will not touch guest memory again, so it is safe to hand outstanding commands' buffers back. Reuse virtscsi_remove_vqs() + virtscsi_init() -- the exact sequence already used across suspend/resume -- to tear the virtqueues down and rebuild them.

Quoted 5 lines

Verified with a QEMU virtio-scsi repro (PCI_COMMAND_MASTER cleared mid-write): EH now runs abort -> device reset -> host reset -> abort -> device reset -> host reset -> offline, then dd's stuck fsync returns EIO and D-state drains to 0, in ~244s total. No task remains uninterruptibly blocked.

By the way, there is a known limitation in the QEMU virtio-scsi implementation where some reset-related operations are implemented synchronously (can block vCPUs and QEMU activity while waiting for I/O to drain in some situations) instead of asynchronously. Recently there has been work in QEMU to make VIRTIO device reset asynchronous, for example, but it needs to be implemented on a per-device basis and virtio-scsi hasn't taken advantage of it yet.

Please let me know if you'd like to discuss or contribute. It's something that is on my radar but not until 2027.

Quoted 33 lines

---
v1 -> v2: implemented eh_host_reset_handler as suggested by Hannes Reinecke in review of the original RFC, instead of relying solely on the EH loop falling through to offline.

---
 drivers/scsi/virtio_scsi.c | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)
drivers/scsi/virtio_scsi.c +3 -0
index b4f20c487718..9698f4f91cc6 100644
@@ -833,6 +833,8 @@ static enum scsi_timeout_action virtscsi_eh_timed_out(struct scsi_cmnd *scmnd)
 	return SCSI_EH_NOT_HANDLED;
 }
 
+static int virtscsi_host_reset(struct scsi_cmnd *sc);
+
 static const struct scsi_host_template virtscsi_host_template = {
 	.module = THIS_MODULE,
 	.name = "Virtio SCSI HBA",
@@ -846,6 +848,7 @@ static const struct scsi_host_template virtscsi_host_template = {
 	.eh_abort_handler = virtscsi_abort,
 	.eh_device_reset_handler = virtscsi_device_reset,
 	.eh_timed_out = virtscsi_eh_timed_out,
+	.eh_host_reset_handler = virtscsi_host_reset,
 	.sdev_init = virtscsi_device_alloc,
 
 	.dma_boundary = UINT_MAX,

drivers/scsi/virtio_scsi.c static int virtscsi_init(struct virtio_device *vdev, · line 954

Quoted 5 lines
drivers/scsi/virtio_scsi.c +2 -0
@@ -947,6 +950,27 @@ static int virtscsi_init(struct virtio_device *vdev,
 	return err;
 }
 
+/*
+ * No bus/target reset in virtio-scsi, so EH lands here after device

The choice to use the VIRTIO device reset in virtscsi_host_reset() looks good to me, but I would like clarification on this comment. virtio-scsi does have I_T_NEXUS_RESET and I believe this is the modern equivalent to the pre-SCSI-6 TARGET RESET. Is it really the case that virtio-scsi doesn't have target reset? And do you want to go ahead and implement .eh_target_reset_handler() using I_T_NEXUS_RESET?

drivers/scsi/virtio_scsi.c static int virtscsi_init(struct virtio_device *vdev, · line 966

Quoted 12 lines
drivers/scsi/virtio_scsi.c +12 -0
+ * reset also times out. A virtio reset guarantees the device won't
+ * touch guest memory again, so stuck commands can be freed safely.
+ * Same teardown/rebuild already used for suspend/resume.
+ */
+static int virtscsi_host_reset(struct scsi_cmnd *sc)
+{
+	struct virtio_scsi *vscsi = shost_priv(sc->device->host);
+	struct virtio_device *vdev = vscsi->vdev;
+
+	scmd_printk(KERN_INFO, sc, "host reset\n");
+
+	virtscsi_remove_vqs(vdev);

This is synchronous and may take a long time. I noticed that some SCSI drivers have a timeout that returns FAILED when the low-level host reset operation takes too long. This can be done here too but would require adding a timeout to virtio_reset_device() (i.e. see the virtio-pci implementation that loops until the device register indicates reset is complete).

drivers/scsi/virtio_scsi.c static int virtscsi_init(struct virtio_device *vdev, · line 970

Quoted 4 lines
drivers/scsi/virtio_scsi.c +4 -0
+	if (virtscsi_init(vdev, vscsi))
+		return FAILED;
+
+	virtio_device_ready(vdev);

Missing virtscsi_kick_event_all() like in virtscsi_restore()?

Quoted 9 lines
drivers/scsi/virtio_scsi.c +3 -0
+	return SUCCESS;
+}
+
 static int virtscsi_probe(struct virtio_device *vdev)
 {
 	struct Scsi_Host *shost;
Signature · 1 line
2.43.0
#7 lore raw

Re: [PATCH v2] scsi: virtio_scsi: implement eh_host_reset_handler

Quoted 70 lines · Stefan Hajnoczi
Quoted 16 lines · Nguyen Ngoc Thang

Once virtscsi_eh_timed_out() lets SCSI EH run on an unresponsive host, abort and device reset both fail the same way (same dead ctrl vq), and since virtio_scsi implements neither target nor bus reset, EH falls through to scsi_eh_offline_sdevs(), which frees the stuck commands' DMA buffers without any guarantee the device has actually stopped touching them. If bus mastering (or whatever broke the transport) comes back later, the device can still write into memory the kernel has already reused.

Add a real eh_host_reset_handler instead. virtio_reset_device() is the guarantee scsi_eh_offline_sdevs() was missing: once it returns, the device will not touch guest memory again, so it is safe to hand outstanding commands' buffers back. Reuse virtscsi_remove_vqs() + virtscsi_init() -- the exact sequence already used across suspend/resume -- to tear the virtqueues down and rebuild them.

Quoted 5 lines

Verified with a QEMU virtio-scsi repro (PCI_COMMAND_MASTER cleared mid-write): EH now runs abort -> device reset -> host reset -> abort -> device reset -> host reset -> offline, then dd's stuck fsync returns EIO and D-state drains to 0, in ~244s total. No task remains uninterruptibly blocked.

By the way, there is a known limitation in the QEMU virtio-scsi implementation where some reset-related operations are implemented synchronously (can block vCPUs and QEMU activity while waiting for I/O to drain in some situations) instead of asynchronously. Recently there has been work in QEMU to make VIRTIO device reset asynchronous, for example, but it needs to be implemented on a per-device basis and virtio-scsi hasn't taken advantage of it yet.

Please let me know if you'd like to discuss or contribute. It's something that is on my radar but not until 2027.

Quoted 36 lines

---
v1 -> v2: implemented eh_host_reset_handler as suggested by Hannes Reinecke in review of the original RFC, instead of relying solely on the EH loop falling through to offline.

---
  drivers/scsi/virtio_scsi.c | 24 ++++++++++++++++++++++++
  1 file changed, 24 insertions(+)
drivers/scsi/virtio_scsi.c +3 -0
index b4f20c487718..9698f4f91cc6 100644
@@ -833,6 +833,8 @@ static enum scsi_timeout_action virtscsi_eh_timed_out(struct scsi_cmnd *scmnd)
  	return SCSI_EH_NOT_HANDLED;
  }
 
+static int virtscsi_host_reset(struct scsi_cmnd *sc);
+
  static const struct scsi_host_template virtscsi_host_template = {
  	.module = THIS_MODULE,
  	.name = "Virtio SCSI HBA",
@@ -846,6 +848,7 @@ static const struct scsi_host_template virtscsi_host_template = {
  	.eh_abort_handler = virtscsi_abort,
  	.eh_device_reset_handler = virtscsi_device_reset,
  	.eh_timed_out = virtscsi_eh_timed_out,
+	.eh_host_reset_handler = virtscsi_host_reset,
  	.sdev_init = virtscsi_device_alloc,
 
  	.dma_boundary = UINT_MAX,
@@ -947,6 +950,27 @@ static int virtscsi_init(struct virtio_device *vdev,
  	return err;
  }
 
Quoted 10 lines

drivers/scsi/virtio_scsi.c static int virtscsi_init(struct virtio_device *vdev, · line 954

Quoted 2 lines
drivers/scsi/virtio_scsi.c +2 -0
+/*
+ * No bus/target reset in virtio-scsi, so EH lands here after device

The choice to use the VIRTIO device reset in virtscsi_host_reset() looks good to me, but I would like clarification on this comment. virtio-scsi does have I_T_NEXUS_RESET and I believe this is the modern equivalent to the pre-SCSI-6 TARGET RESET. Is it really the case that virtio-scsi doesn't have target reset? And do you want to go ahead and implement .eh_target_reset_handler() using I_T_NEXUS_RESET?

Would probably not a bad idea to use I_T_NEXUS_RESET here. Not sure what it does internally, though; _is_ there a nexus to reset?

Quoted 11 lines
Quoted 11 lines
drivers/scsi/virtio_scsi.c +11 -0
+ * reset also times out. A virtio reset guarantees the device won't
+ * touch guest memory again, so stuck commands can be freed safely.
+ * Same teardown/rebuild already used for suspend/resume.
+ */
+static int virtscsi_host_reset(struct scsi_cmnd *sc)
+{
+	struct virtio_scsi *vscsi = shost_priv(sc->device->host);
+	struct virtio_device *vdev = vscsi->vdev;
+
+	scmd_printk(KERN_INFO, sc, "host reset\n");
+
Quoted 9 lines

drivers/scsi/virtio_scsi.c static int virtscsi_init(struct virtio_device *vdev, · line 966

Quoted 1 line
drivers/scsi/virtio_scsi.c +1 -0
+	virtscsi_remove_vqs(vdev);

This is synchronous and may take a long time. I noticed that some SCSI drivers have a timeout that returns FAILED when the low-level host reset operation takes too long. This can be done here too but would require adding a timeout to virtio_reset_device() (i.e. see the virtio-pci implementation that loops until the device register indicates reset is complete).

Never, ever, return FAILED from a host reset.
That will induce a non-recoverable error, and you have to reset the OS to get the virtio devices back.
Not something you'd want to do I guess.

Yes, I know that host reset can take a long time. But normally host reset would be the last resort, so really when you have to call it your system has exhausted all other options.

Cheers,

Hannes

Signature · 4 lines
Dr. Hannes Reinecke                  Kernel Storage Architect
[email protected]                                +49 911 74053 688
SUSE Software Solutions GmbH, Frankenstr. 146, 90461 Nürnberg
HRB 36809 (AG Nürnberg), GF: I. Totev, A. McDonald, W. Knoblich
#8 lore raw

Re: [PATCH v2] scsi: virtio_scsi: implement eh_host_reset_handler

Quoted 78 lines · Hannes Reinecke
Quoted 68 lines · Stefan Hajnoczi
Quoted 16 lines · Nguyen Ngoc Thang

Once virtscsi_eh_timed_out() lets SCSI EH run on an unresponsive host, abort and device reset both fail the same way (same dead ctrl vq), and since virtio_scsi implements neither target nor bus reset, EH falls through to scsi_eh_offline_sdevs(), which frees the stuck commands' DMA buffers without any guarantee the device has actually stopped touching them. If bus mastering (or whatever broke the transport) comes back later, the device can still write into memory the kernel has already reused.

Add a real eh_host_reset_handler instead. virtio_reset_device() is the guarantee scsi_eh_offline_sdevs() was missing: once it returns, the device will not touch guest memory again, so it is safe to hand outstanding commands' buffers back. Reuse virtscsi_remove_vqs() + virtscsi_init() -- the exact sequence already used across suspend/resume -- to tear the virtqueues down and rebuild them.

Quoted 5 lines

Verified with a QEMU virtio-scsi repro (PCI_COMMAND_MASTER cleared mid-write): EH now runs abort -> device reset -> host reset -> abort -> device reset -> host reset -> offline, then dd's stuck fsync returns EIO and D-state drains to 0, in ~244s total. No task remains uninterruptibly blocked.

By the way, there is a known limitation in the QEMU virtio-scsi implementation where some reset-related operations are implemented synchronously (can block vCPUs and QEMU activity while waiting for I/O to drain in some situations) instead of asynchronously. Recently there has been work in QEMU to make VIRTIO device reset asynchronous, for example, but it needs to be implemented on a per-device basis and virtio-scsi hasn't taken advantage of it yet.

Please let me know if you'd like to discuss or contribute. It's something that is on my radar but not until 2027.

Quoted 34 lines

---
v1 -> v2: implemented eh_host_reset_handler as suggested by Hannes Reinecke in review of the original RFC, instead of relying solely on the EH loop falling through to offline.

---
  drivers/scsi/virtio_scsi.c | 24 ++++++++++++++++++++++++
  1 file changed, 24 insertions(+)
drivers/scsi/virtio_scsi.c +4 -0
index b4f20c487718..9698f4f91cc6 100644
@@ -833,6 +833,8 @@ static enum scsi_timeout_action virtscsi_eh_timed_out(struct scsi_cmnd *scmnd)
  	return SCSI_EH_NOT_HANDLED;
  }
+static int virtscsi_host_reset(struct scsi_cmnd *sc);
+
  static const struct scsi_host_template virtscsi_host_template = {
  	.module = THIS_MODULE,
  	.name = "Virtio SCSI HBA",
@@ -846,6 +848,7 @@ static const struct scsi_host_template virtscsi_host_template = {
  	.eh_abort_handler = virtscsi_abort,
  	.eh_device_reset_handler = virtscsi_device_reset,
  	.eh_timed_out = virtscsi_eh_timed_out,
+	.eh_host_reset_handler = virtscsi_host_reset,
  	.sdev_init = virtscsi_device_alloc,
  	.dma_boundary = UINT_MAX,
@@ -947,6 +950,27 @@ static int virtscsi_init(struct virtio_device *vdev,
  	return err;
  }
+/*
Quoted 9 lines

drivers/scsi/virtio_scsi.c static int virtscsi_init(struct virtio_device *vdev, · line 954

Quoted 1 line
drivers/scsi/virtio_scsi.c +1 -0
+ * No bus/target reset in virtio-scsi, so EH lands here after device

The choice to use the VIRTIO device reset in virtscsi_host_reset() looks good to me, but I would like clarification on this comment. virtio-scsi does have I_T_NEXUS_RESET and I believe this is the modern equivalent to the pre-SCSI-6 TARGET RESET. Is it really the case that virtio-scsi doesn't have target reset? And do you want to go ahead and implement .eh_target_reset_handler() using I_T_NEXUS_RESET?

Quoted 3 lines

Would probably not a bad idea to use I_T_NEXUS_RESET here. Not sure what it does internally, though; _is_ there a nexus to reset?

Whether there is any benefit to I_T_NEXUS_RESET may depend on the device implementation. In QEMU:
- LOGICAL_UNIT_RESET resets just 1 LUN
- I_T_NEXUS_RESET resets all LUNs
- VIRTIO device reset also resets the HBA itself

In practice QEMU's I_T_NEXUS_RESET is unlikely to fix things if LOGICAL_UNIT_RESET wasn't sufficient. Maybe it will help on other device implementations though.

I think the Linux driver mapping should be:
- .eh_device_reset_handler() -> LOGICAL_UNIT_RESET
- .eh_target_reset_handler() -> I_T_NEXUS_RESET
- .eh_host_reset_handler() -> VIRTIO device reset

Quoted 20 lines
Quoted 11 lines
Quoted 11 lines
drivers/scsi/virtio_scsi.c +11 -0
+ * reset also times out. A virtio reset guarantees the device won't
+ * touch guest memory again, so stuck commands can be freed safely.
+ * Same teardown/rebuild already used for suspend/resume.
+ */
+static int virtscsi_host_reset(struct scsi_cmnd *sc)
+{
+	struct virtio_scsi *vscsi = shost_priv(sc->device->host);
+	struct virtio_device *vdev = vscsi->vdev;
+
+	scmd_printk(KERN_INFO, sc, "host reset\n");
+
Quoted 9 lines

drivers/scsi/virtio_scsi.c static int virtscsi_init(struct virtio_device *vdev, · line 966

Quoted 1 line
drivers/scsi/virtio_scsi.c +1 -0
+	virtscsi_remove_vqs(vdev);

This is synchronous and may take a long time. I noticed that some SCSI drivers have a timeout that returns FAILED when the low-level host reset operation takes too long. This can be done here too but would require adding a timeout to virtio_reset_device() (i.e. see the virtio-pci implementation that loops until the device register indicates reset is complete).

Quoted 8 lines

Never, ever, return FAILED from a host reset.
That will induce a non-recoverable error, and you have to reset the OS to get the virtio devices back.
Not something you'd want to do I guess.

Yes, I know that host reset can take a long time. But normally host reset would be the last resort, so really when you have to call it your system has exhausted all other options.

Okay.

Stefan

#9 lore raw

Re: [PATCH v2] scsi: virtio_scsi: implement eh_host_reset_handler

Quoted 89 lines · Stefan Hajnoczi
Quoted 78 lines · Hannes Reinecke
Quoted 68 lines · Stefan Hajnoczi
Quoted 16 lines · Nguyen Ngoc Thang

Once virtscsi_eh_timed_out() lets SCSI EH run on an unresponsive host, abort and device reset both fail the same way (same dead ctrl vq), and since virtio_scsi implements neither target nor bus reset, EH falls through to scsi_eh_offline_sdevs(), which frees the stuck commands' DMA buffers without any guarantee the device has actually stopped touching them. If bus mastering (or whatever broke the transport) comes back later, the device can still write into memory the kernel has already reused.

Add a real eh_host_reset_handler instead. virtio_reset_device() is the guarantee scsi_eh_offline_sdevs() was missing: once it returns, the device will not touch guest memory again, so it is safe to hand outstanding commands' buffers back. Reuse virtscsi_remove_vqs() + virtscsi_init() -- the exact sequence already used across suspend/resume -- to tear the virtqueues down and rebuild them.

Quoted 5 lines

Verified with a QEMU virtio-scsi repro (PCI_COMMAND_MASTER cleared mid-write): EH now runs abort -> device reset -> host reset -> abort -> device reset -> host reset -> offline, then dd's stuck fsync returns EIO and D-state drains to 0, in ~244s total. No task remains uninterruptibly blocked.

By the way, there is a known limitation in the QEMU virtio-scsi implementation where some reset-related operations are implemented synchronously (can block vCPUs and QEMU activity while waiting for I/O to drain in some situations) instead of asynchronously. Recently there has been work in QEMU to make VIRTIO device reset asynchronous, for example, but it needs to be implemented on a per-device basis and virtio-scsi hasn't taken advantage of it yet.

Please let me know if you'd like to discuss or contribute. It's something that is on my radar but not until 2027.

Quoted 34 lines

---
v1 -> v2: implemented eh_host_reset_handler as suggested by Hannes Reinecke in review of the original RFC, instead of relying solely on the EH loop falling through to offline.

---
   drivers/scsi/virtio_scsi.c | 24 ++++++++++++++++++++++++
   1 file changed, 24 insertions(+)
drivers/scsi/virtio_scsi.c +4 -0
index b4f20c487718..9698f4f91cc6 100644
@@ -833,6 +833,8 @@ static enum scsi_timeout_action virtscsi_eh_timed_out(struct scsi_cmnd *scmnd)
   	return SCSI_EH_NOT_HANDLED;
   }
+static int virtscsi_host_reset(struct scsi_cmnd *sc);
+
   static const struct scsi_host_template virtscsi_host_template = {
   	.module = THIS_MODULE,
   	.name = "Virtio SCSI HBA",
@@ -846,6 +848,7 @@ static const struct scsi_host_template virtscsi_host_template = {
   	.eh_abort_handler = virtscsi_abort,
   	.eh_device_reset_handler = virtscsi_device_reset,
   	.eh_timed_out = virtscsi_eh_timed_out,
+	.eh_host_reset_handler = virtscsi_host_reset,
   	.sdev_init = virtscsi_device_alloc,
   	.dma_boundary = UINT_MAX,
@@ -947,6 +950,27 @@ static int virtscsi_init(struct virtio_device *vdev,
   	return err;
   }
+/*
Quoted 9 lines

drivers/scsi/virtio_scsi.c static int virtscsi_init(struct virtio_device *vdev, · line 954

Quoted 1 line
drivers/scsi/virtio_scsi.c +1 -0
+ * No bus/target reset in virtio-scsi, so EH lands here after device

The choice to use the VIRTIO device reset in virtscsi_host_reset() looks good to me, but I would like clarification on this comment. virtio-scsi does have I_T_NEXUS_RESET and I believe this is the modern equivalent to the pre-SCSI-6 TARGET RESET. Is it really the case that virtio-scsi doesn't have target reset? And do you want to go ahead and implement .eh_target_reset_handler() using I_T_NEXUS_RESET?

Quoted 3 lines

Would probably not a bad idea to use I_T_NEXUS_RESET here. Not sure what it does internally, though; _is_ there a nexus to reset?

Whether there is any benefit to I_T_NEXUS_RESET may depend on the device implementation. In QEMU:
- LOGICAL_UNIT_RESET resets just 1 LUN
- I_T_NEXUS_RESET resets all LUNs
- VIRTIO device reset also resets the HBA itself

Quoted 9 lines

In practice QEMU's I_T_NEXUS_RESET is unlikely to fix things if LOGICAL_UNIT_RESET wasn't sufficient. Maybe it will help on other device implementations though.

I think the Linux driver mapping should be:
- .eh_device_reset_handler() -> LOGICAL_UNIT_RESET
- .eh_target_reset_handler() -> I_T_NEXUS_RESET
- .eh_host_reset_handler() -> VIRTIO device reset

I guess 'target_reset' is pretty much pointless for modern setups. If lun reset doesn't work it's pretty much guaranteed that target_reset won't help, either.
(Unless you are on SAS, then it might, as I_T nexus reset actually resets the SAS link and such does something useful).
Problem is that in most cases LUN reset fails as the _communication_ to the LUN is interrupted, and hence sending another TMF across the same communication channel won't help, either.
For virtio that would mean that we'd have to reset the virtio queues; can we even do that in a meaningful fashion?

Cheers,

Hannes

Signature · 4 lines
Dr. Hannes Reinecke                  Kernel Storage Architect
[email protected]                                +49 911 74053 688
SUSE Software Solutions GmbH, Frankenstr. 146, 90461 Nürnberg
HRB 36809 (AG Nürnberg), GF: I. Totev, A. McDonald, W. Knoblich
#10 lore raw

Re: [PATCH v2] scsi: virtio_scsi: implement eh_host_reset_handler

Quoted 99 lines · Hannes Reinecke
Quoted 89 lines · Stefan Hajnoczi
Quoted 78 lines · Hannes Reinecke
Quoted 68 lines · Stefan Hajnoczi
Quoted 16 lines · Nguyen Ngoc Thang

Once virtscsi_eh_timed_out() lets SCSI EH run on an unresponsive host, abort and device reset both fail the same way (same dead ctrl vq), and since virtio_scsi implements neither target nor bus reset, EH falls through to scsi_eh_offline_sdevs(), which frees the stuck commands' DMA buffers without any guarantee the device has actually stopped touching them. If bus mastering (or whatever broke the transport) comes back later, the device can still write into memory the kernel has already reused.

Add a real eh_host_reset_handler instead. virtio_reset_device() is the guarantee scsi_eh_offline_sdevs() was missing: once it returns, the device will not touch guest memory again, so it is safe to hand outstanding commands' buffers back. Reuse virtscsi_remove_vqs() + virtscsi_init() -- the exact sequence already used across suspend/resume -- to tear the virtqueues down and rebuild them.

Quoted 5 lines

Verified with a QEMU virtio-scsi repro (PCI_COMMAND_MASTER cleared mid-write): EH now runs abort -> device reset -> host reset -> abort -> device reset -> host reset -> offline, then dd's stuck fsync returns EIO and D-state drains to 0, in ~244s total. No task remains uninterruptibly blocked.

By the way, there is a known limitation in the QEMU virtio-scsi implementation where some reset-related operations are implemented synchronously (can block vCPUs and QEMU activity while waiting for I/O to drain in some situations) instead of asynchronously. Recently there has been work in QEMU to make VIRTIO device reset asynchronous, for example, but it needs to be implemented on a per-device basis and virtio-scsi hasn't taken advantage of it yet.

Please let me know if you'd like to discuss or contribute. It's something that is on my radar but not until 2027.

Quoted 34 lines

---
v1 -> v2: implemented eh_host_reset_handler as suggested by Hannes Reinecke in review of the original RFC, instead of relying solely on the EH loop falling through to offline.

---
   drivers/scsi/virtio_scsi.c | 24 ++++++++++++++++++++++++
   1 file changed, 24 insertions(+)
drivers/scsi/virtio_scsi.c +4 -0
index b4f20c487718..9698f4f91cc6 100644
@@ -833,6 +833,8 @@ static enum scsi_timeout_action virtscsi_eh_timed_out(struct scsi_cmnd *scmnd)
   	return SCSI_EH_NOT_HANDLED;
   }
+static int virtscsi_host_reset(struct scsi_cmnd *sc);
+
   static const struct scsi_host_template virtscsi_host_template = {
   	.module = THIS_MODULE,
   	.name = "Virtio SCSI HBA",
@@ -846,6 +848,7 @@ static const struct scsi_host_template virtscsi_host_template = {
   	.eh_abort_handler = virtscsi_abort,
   	.eh_device_reset_handler = virtscsi_device_reset,
   	.eh_timed_out = virtscsi_eh_timed_out,
+	.eh_host_reset_handler = virtscsi_host_reset,
   	.sdev_init = virtscsi_device_alloc,
   	.dma_boundary = UINT_MAX,
@@ -947,6 +950,27 @@ static int virtscsi_init(struct virtio_device *vdev,
   	return err;
   }
+/*
Quoted 9 lines

drivers/scsi/virtio_scsi.c static int virtscsi_init(struct virtio_device *vdev, · line 954

Quoted 1 line
drivers/scsi/virtio_scsi.c +1 -0
+ * No bus/target reset in virtio-scsi, so EH lands here after device

The choice to use the VIRTIO device reset in virtscsi_host_reset() looks good to me, but I would like clarification on this comment. virtio-scsi does have I_T_NEXUS_RESET and I believe this is the modern equivalent to the pre-SCSI-6 TARGET RESET. Is it really the case that virtio-scsi doesn't have target reset? And do you want to go ahead and implement .eh_target_reset_handler() using I_T_NEXUS_RESET?

Quoted 3 lines

Would probably not a bad idea to use I_T_NEXUS_RESET here. Not sure what it does internally, though; _is_ there a nexus to reset?

Whether there is any benefit to I_T_NEXUS_RESET may depend on the device implementation. In QEMU:
- LOGICAL_UNIT_RESET resets just 1 LUN
- I_T_NEXUS_RESET resets all LUNs
- VIRTIO device reset also resets the HBA itself

Quoted 9 lines

In practice QEMU's I_T_NEXUS_RESET is unlikely to fix things if LOGICAL_UNIT_RESET wasn't sufficient. Maybe it will help on other device implementations though.

I think the Linux driver mapping should be:
- .eh_device_reset_handler() -> LOGICAL_UNIT_RESET
- .eh_target_reset_handler() -> I_T_NEXUS_RESET
- .eh_host_reset_handler() -> VIRTIO device reset

Quoted 10 lines

I guess 'target_reset' is pretty much pointless for modern setups. If lun reset doesn't work it's pretty much guaranteed that target_reset won't help, either.
(Unless you are on SAS, then it might, as I_T nexus reset actually resets the SAS link and such does something useful).
Problem is that in most cases LUN reset fails as the _communication_ to the LUN is interrupted, and hence sending another TMF across the same communication channel won't help, either.
For virtio that would mean that we'd have to reset the virtio queues; can we even do that in a meaningful fashion?

The virtqueues can only really be reset by VIRTIO device reset (.eh_host_reset_handler()).

Stefan

#11 lore raw

Re: [PATCH v2] scsi: virtio_scsi: implement eh_host_reset_handler

In reply to Stefan Hajnoczi
Quoted 85 lines · Stefan Hajnoczi
Quoted 75 lines · Hannes Reinecke
Quoted 65 lines · Stefan Hajnoczi
Quoted 16 lines · Nguyen Ngoc Thang

Once virtscsi_eh_timed_out() lets SCSI EH run on an unresponsive host, abort and device reset both fail the same way (same dead ctrl vq), and since virtio_scsi implements neither target nor bus reset, EH falls through to scsi_eh_offline_sdevs(), which frees the stuck commands' DMA buffers without any guarantee the device has actually stopped touching them. If bus mastering (or whatever broke the transport) comes back later, the device can still write into memory the kernel has already reused.

Add a real eh_host_reset_handler instead. virtio_reset_device() is the guarantee scsi_eh_offline_sdevs() was missing: once it returns, the device will not touch guest memory again, so it is safe to hand outstanding commands' buffers back. Reuse virtscsi_remove_vqs() + virtscsi_init() -- the exact sequence already used across suspend/resume -- to tear the virtqueues down and rebuild them.

Quoted 5 lines

Verified with a QEMU virtio-scsi repro (PCI_COMMAND_MASTER cleared mid-write): EH now runs abort -> device reset -> host reset -> abort -> device reset -> host reset -> offline, then dd's stuck fsync returns EIO and D-state drains to 0, in ~244s total. No task remains uninterruptibly blocked.

By the way, there is a known limitation in the QEMU virtio-scsi implementation where some reset-related operations are implemented synchronously (can block vCPUs and QEMU activity while waiting for I/O to drain in some situations) instead of asynchronously. Recently there has been work in QEMU to make VIRTIO device reset asynchronous, for example, but it needs to be implemented on a per-device basis and virtio-scsi hasn't taken advantage of it yet.

Please let me know if you'd like to discuss or contribute. It's something that is on my radar but not until 2027.

Quoted 32 lines

---
v1 -> v2: implemented eh_host_reset_handler as suggested by Hannes Reinecke in review of the original RFC, instead of relying solely on the EH loop falling through to offline.

---
  drivers/scsi/virtio_scsi.c | 24 ++++++++++++++++++++++++
  1 file changed, 24 insertions(+)
drivers/scsi/virtio_scsi.c +3 -0
index b4f20c487718..9698f4f91cc6 100644
@@ -833,6 +833,8 @@ static enum scsi_timeout_action virtscsi_eh_timed_out(struct scsi_cmnd *scmnd)
  	return SCSI_EH_NOT_HANDLED;
  }
+static int virtscsi_host_reset(struct scsi_cmnd *sc);
+
  static const struct scsi_host_template virtscsi_host_template = {
  	.module = THIS_MODULE,
  	.name = "Virtio SCSI HBA",
@@ -846,6 +848,7 @@ static const struct scsi_host_template virtscsi_host_template = {
  	.eh_abort_handler = virtscsi_abort,
  	.eh_device_reset_handler = virtscsi_device_reset,
  	.eh_timed_out = virtscsi_eh_timed_out,
+	.eh_host_reset_handler = virtscsi_host_reset,
  	.sdev_init = virtscsi_device_alloc,
  	.dma_boundary = UINT_MAX,
@@ -947,6 +950,27 @@ static int virtscsi_init(struct virtio_device *vdev,
  	return err;
  }
Quoted 9 lines

drivers/scsi/virtio_scsi.c static int virtscsi_init(struct virtio_device *vdev, · line 954

Quoted 2 lines
drivers/scsi/virtio_scsi.c +2 -0
+/*
+ * No bus/target reset in virtio-scsi, so EH lands here after device

The choice to use the VIRTIO device reset in virtscsi_host_reset() looks good to me, but I would like clarification on this comment. virtio-scsi does have I_T_NEXUS_RESET and I believe this is the modern equivalent to the pre-SCSI-6 TARGET RESET. Is it really the case that virtio-scsi doesn't have target reset? And do you want to go ahead and implement .eh_target_reset_handler() using I_T_NEXUS_RESET?

Quoted 3 lines

Would probably not a bad idea to use I_T_NEXUS_RESET here. Not sure what it does internally, though; _is_ there a nexus to reset?

Whether there is any benefit to I_T_NEXUS_RESET may depend on the device implementation. In QEMU:
- LOGICAL_UNIT_RESET resets just 1 LUN
- I_T_NEXUS_RESET resets all LUNs
- VIRTIO device reset also resets the HBA itself

Quoted 8 lines

In practice QEMU's I_T_NEXUS_RESET is unlikely to fix things if LOGICAL_UNIT_RESET wasn't sufficient. Maybe it will help on other device implementations though.

I think the Linux driver mapping should be:
- .eh_device_reset_handler() -> LOGICAL_UNIT_RESET
- .eh_target_reset_handler() -> I_T_NEXUS_RESET
- .eh_host_reset_handler() -> VIRTIO device reset

How are vhost drivers notified of the device reset? For qemu virtio-scsi this would call virtio_scsi_reset right?

For vhost-scsi though, do we need to add a similar reset function?

#12 lore raw

Re: [PATCH v2] scsi: virtio_scsi: implement eh_host_reset_handler

Quoted 95 lines · Mike Christie
Quoted 85 lines · Stefan Hajnoczi
Quoted 75 lines · Hannes Reinecke
Quoted 65 lines · Stefan Hajnoczi
Quoted 16 lines · Nguyen Ngoc Thang

Once virtscsi_eh_timed_out() lets SCSI EH run on an unresponsive host, abort and device reset both fail the same way (same dead ctrl vq), and since virtio_scsi implements neither target nor bus reset, EH falls through to scsi_eh_offline_sdevs(), which frees the stuck commands' DMA buffers without any guarantee the device has actually stopped touching them. If bus mastering (or whatever broke the transport) comes back later, the device can still write into memory the kernel has already reused.

Add a real eh_host_reset_handler instead. virtio_reset_device() is the guarantee scsi_eh_offline_sdevs() was missing: once it returns, the device will not touch guest memory again, so it is safe to hand outstanding commands' buffers back. Reuse virtscsi_remove_vqs() + virtscsi_init() -- the exact sequence already used across suspend/resume -- to tear the virtqueues down and rebuild them.

Quoted 5 lines

Verified with a QEMU virtio-scsi repro (PCI_COMMAND_MASTER cleared mid-write): EH now runs abort -> device reset -> host reset -> abort -> device reset -> host reset -> offline, then dd's stuck fsync returns EIO and D-state drains to 0, in ~244s total. No task remains uninterruptibly blocked.

By the way, there is a known limitation in the QEMU virtio-scsi implementation where some reset-related operations are implemented synchronously (can block vCPUs and QEMU activity while waiting for I/O to drain in some situations) instead of asynchronously. Recently there has been work in QEMU to make VIRTIO device reset asynchronous, for example, but it needs to be implemented on a per-device basis and virtio-scsi hasn't taken advantage of it yet.

Please let me know if you'd like to discuss or contribute. It's something that is on my radar but not until 2027.

Quoted 32 lines

---
v1 -> v2: implemented eh_host_reset_handler as suggested by Hannes Reinecke in review of the original RFC, instead of relying solely on the EH loop falling through to offline.

---
  drivers/scsi/virtio_scsi.c | 24 ++++++++++++++++++++++++
  1 file changed, 24 insertions(+)
drivers/scsi/virtio_scsi.c +3 -0
index b4f20c487718..9698f4f91cc6 100644
@@ -833,6 +833,8 @@ static enum scsi_timeout_action virtscsi_eh_timed_out(struct scsi_cmnd *scmnd)
  	return SCSI_EH_NOT_HANDLED;
  }
+static int virtscsi_host_reset(struct scsi_cmnd *sc);
+
  static const struct scsi_host_template virtscsi_host_template = {
  	.module = THIS_MODULE,
  	.name = "Virtio SCSI HBA",
@@ -846,6 +848,7 @@ static const struct scsi_host_template virtscsi_host_template = {
  	.eh_abort_handler = virtscsi_abort,
  	.eh_device_reset_handler = virtscsi_device_reset,
  	.eh_timed_out = virtscsi_eh_timed_out,
+	.eh_host_reset_handler = virtscsi_host_reset,
  	.sdev_init = virtscsi_device_alloc,
  	.dma_boundary = UINT_MAX,
@@ -947,6 +950,27 @@ static int virtscsi_init(struct virtio_device *vdev,
  	return err;
  }
Quoted 9 lines

drivers/scsi/virtio_scsi.c static int virtscsi_init(struct virtio_device *vdev, · line 954

Quoted 2 lines
drivers/scsi/virtio_scsi.c +2 -0
+/*
+ * No bus/target reset in virtio-scsi, so EH lands here after device

The choice to use the VIRTIO device reset in virtscsi_host_reset() looks good to me, but I would like clarification on this comment. virtio-scsi does have I_T_NEXUS_RESET and I believe this is the modern equivalent to the pre-SCSI-6 TARGET RESET. Is it really the case that virtio-scsi doesn't have target reset? And do you want to go ahead and implement .eh_target_reset_handler() using I_T_NEXUS_RESET?

Quoted 3 lines

Would probably not a bad idea to use I_T_NEXUS_RESET here. Not sure what it does internally, though; _is_ there a nexus to reset?

Whether there is any benefit to I_T_NEXUS_RESET may depend on the device implementation. In QEMU:
- LOGICAL_UNIT_RESET resets just 1 LUN
- I_T_NEXUS_RESET resets all LUNs
- VIRTIO device reset also resets the HBA itself

Quoted 8 lines

In practice QEMU's I_T_NEXUS_RESET is unlikely to fix things if LOGICAL_UNIT_RESET wasn't sufficient. Maybe it will help on other device implementations though.

I think the Linux driver mapping should be:
- .eh_device_reset_handler() -> LOGICAL_UNIT_RESET
- .eh_target_reset_handler() -> I_T_NEXUS_RESET
- .eh_host_reset_handler() -> VIRTIO device reset

Quoted 4 lines

How are vhost drivers notified of the device reset? For qemu virtio-scsi this would call virtio_scsi_reset right?

For vhost-scsi though, do we need to add a similar reset function?

VIRTIO device reset is handled by vhost_scsi's VHOST_SCSI_CLEAR_ENDPOINT and VHOST_SCSI_SET_ENDPOINT ioctls called from QEMU's hw/scsi/vhost-scsi.c:vhost_scsi_set_status(). The Linux vhost_scsi code flushes requests and resets the device, so I don't think code changes are needed.

Stefan

#13 lore raw
In reply to Hannes Reinecke
Quoted 3 lines · Hannes Reinecke

So the better fix would be to implement a host_reset() callback for virtio-scsi which would reset the virtio setup, terminating all outstanding commands and reset the queues.

Thanks for the suggestion, Hannes -- that's exactly what the patch below does (also posted separately as [PATCH v2], Message-ID <[email protected]>; linking it back into this thread since the subject changed and a couple of mail clients won't merge the two threads on their own).

It adds eh_host_reset_handler backed by virtio_reset_device(): once virtio_reset_device() returns, the device is guaranteed to have stopped touching guest memory, so it's safe to let scsi_eh_offline_sdevs() free the stuck commands' DMA buffers afterwards -- the guarantee that was missing before (falling through to offline with neither target, bus, nor host reset implemented). Reuses virtscsi_remove_vqs() + virtscsi_init(), the same teardown/rebuild sequence already used across suspend/resume.

Verified against the same QEMU virtio-scsi repro (PCI_COMMAND_MASTER cleared mid-write): EH now runs abort -> device reset -> host reset -> abort -> device reset -> host reset -> offline, dd's stuck fsync returns EIO, D-state drains to 0, ~244s total.

---
 drivers/scsi/virtio_scsi.c | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)
drivers/scsi/virtio_scsi.c +24 -0
index b4f20c487718..9698f4f91cc6 100644
@@ -833,6 +833,8 @@ static enum scsi_timeout_action virtscsi_eh_timed_out(struct scsi_cmnd *scmnd)
 	return SCSI_EH_NOT_HANDLED;
 }
 
+static int virtscsi_host_reset(struct scsi_cmnd *sc);
+
 static const struct scsi_host_template virtscsi_host_template = {
 	.module = THIS_MODULE,
 	.name = "Virtio SCSI HBA",
@@ -846,6 +848,7 @@ static const struct scsi_host_template virtscsi_host_template = {
 	.eh_abort_handler = virtscsi_abort,
 	.eh_device_reset_handler = virtscsi_device_reset,
 	.eh_timed_out = virtscsi_eh_timed_out,
+	.eh_host_reset_handler = virtscsi_host_reset,
 	.sdev_init = virtscsi_device_alloc,
 
 	.dma_boundary = UINT_MAX,
@@ -947,6 +950,27 @@ static int virtscsi_init(struct virtio_device *vdev,
 	return err;
 }
 
+/*
+ * No bus/target reset in virtio-scsi, so EH lands here after device
+ * reset also times out. A virtio reset guarantees the device won't
+ * touch guest memory again, so stuck commands can be freed safely.
+ * Same teardown/rebuild already used for suspend/resume.
+ */
+static int virtscsi_host_reset(struct scsi_cmnd *sc)
+{
+	struct virtio_scsi *vscsi = shost_priv(sc->device->host);
+	struct virtio_device *vdev = vscsi->vdev;
+
+	scmd_printk(KERN_INFO, sc, "host reset\n");
+
+	virtscsi_remove_vqs(vdev);
+	if (virtscsi_init(vdev, vscsi))
+		return FAILED;
+
+	virtio_device_ready(vdev);
+	return SUCCESS;
+}
+
 static int virtscsi_probe(struct virtio_device *vdev)
 {
 	struct Scsi_Host *shost;
Signature · 4 lines
2.43.0

Thanks,
Thang