* [PATCH] [ Upstream commit 085bb18a0b91007f04ddca13af30277c78040157 ]
@ 2026-11-23 9:45 Yi Xu
0 siblings, 0 replies; only message in thread
From: Yi Xu @ 2026-11-23 9:45 UTC (permalink / raw)
To: stable; +Cc: gregkh, sashal, bvanassche, hulianqin
scsi: sd: Mark the queue dying if runtime resume fails in sd_remove()
If a SCSI disk is unplugged while runtime suspended, its request queue
is still in the pm-only state when sd_remove() is called. The runtime
resume attempted by scsi_autopm_get_device() fails because the parent
device, e.g. a USB device in the USB_STATE_NOTATTACHED state, cannot be
resumed. Hence, sdev_runtime_resume() is not called and the pm-only
state is not cleared. Next, del_gendisk() calls invalidate_partition(),
which calls fsync_bdev() and thus sync_filesystem(). Any bio submitted
by the filesystem is queued through submit_bio() and waits forever in
blk_queue_enter() because the queue will never leave the pm-only state
and because the queue is not yet marked dying:
blk_queue_enter (wait_event on mq_freeze_wq -- HANGS HERE)
bio_queue_enter
__submit_bio_noacct_mq
submit_bio_noacct
submit_bio
submit_bio_wait
blkdev_issue_flush
ext4_sync_fs (sb->s_op->sync_fs)
sync_filesystem
fsync_bdev
invalidate_partition
del_gendisk
Fix this hang by calling blk_set_queue_dying() if runtime resume fails.
The hang point is the wait_event() in blk_queue_enter(), whose exit
condition includes blk_queue_dying(q) (i.e. QUEUE_FLAG_DYING set on
the queue). blk_set_queue_dying() sets that flag, freezes the queue
and wakes up all waiters on mq_freeze_wq, so the wait_event exits and
the pending sync bio fails immediately with -ENODEV; the removal then
completes instead of hanging. Syncing the filesystem is not possible
anyway if runtime resume failed, so no data is lost that could
otherwise have been written. If runtime resume succeeds, behavior is
not changed. Make this change in sd_remove() instead of in a SCSI LLD
such that it works for all SCSI transports.
Please note that this patch converts temporary runtime resume failures
into permanent failures. Before this patch, if runtime resume fails
temporarily, a filesystem sync would be postponed until a later resume
succeeds.
[Deviation from upstream]
The upstream fix calls blk_mark_disk_dead(), introduced by
commit 6f8191fdf41d ("block: simplify disk shutdown"). That
helper and the GD_DEAD flag do not exist in this series. The
v5.10.y equivalent is blk_set_queue_dying(): in this series
blk_queue_enter() (the counterpart of the upstream
__bio_queue_enter()) releases waiters on QUEUE_FLAG_DYING, and
del_gendisk() itself marks the queue dying later via
blk_unregister_queue(); this fix only moves that point ahead of
the hanging invalidate_partition()/fsync_bdev() sync. Function
name mapping vs. the upstream message: sync_filesystem() call in
del_gendisk() -> invalidate_partition()/fsync_bdev();
__bio_queue_enter() -> blk_queue_enter(); blk_mark_disk_dead() ->
blk_set_queue_dying(). The trigger condition
(scsi_autopm_get_device() < 0) and the "temporary failures become
permanent" trade-off are identical to upstream. The normal removal
path is unchanged.
Reported-by: Lianqin Hu <hulianqin@vivo.com>
Suggested-by: Bart Van Assche <bvanassche@acm.org>
Cc: stable@vger.kernel.org
Signed-off-by: Yi Xu <xuyi0562@gmail.com>
---
Hi stable team,
Manual backport of upstream commit 085bb18 ("scsi: sd: Mark the disk
dead if runtime resume fails in sd_remove()") to v5.10.y.
The automated cherry-pick does not apply to this series: the upstream
fix calls blk_mark_disk_dead(), which does not exist here, and the
sd_remove() context differs. This patch assembles the identical effect
from primitives available in 5.10.y -- see the Deviation section in
the commit message for the equivalence argument and function-name
mapping. The fix builds cleanly on v5.10.274.
Background: the problem was reported against USB storage surprise
removal on the linux-scsi list
(https://lore.kernel.org/all/2c0c76d70f0fe197932836b9bcf8496095135d1b.1792446921.git.bvanassche@acm.org/);
Cc'ing the reporter and the author for visibility.
base-commit: 3a323bc22b881a5bdc1646080f93f5ecb22850bf
Thanks,
Yi Xu
drivers/scsi/sd.c | 10 +++++++++-
1 file changed, 9 insertions(+), 1 deletion(-)
diff --git a/drivers/scsi/sd.c b/drivers/scsi/sd.c
index da6df9809b0c..6cf56b936f14 100644
--- a/drivers/scsi/sd.c
+++ b/drivers/scsi/sd.c
@@ -3545,7 +3545,15 @@ static int sd_remove(struct device *dev)
sdkp = dev_get_drvdata(dev);
devt = disk_devt(sdkp->disk);
- scsi_autopm_get_device(sdkp->device);
+ /*
+ * If runtime resume fails, e.g. because the device was unplugged
+ * while runtime suspended, the request queue stays in the pm-only
+ * state and any I/O submitted by del_gendisk(), e.g. from
+ * invalidate_partition(), would wait forever in blk_queue_enter().
+ * Prevent this by marking the queue dying.
+ */
+ if (scsi_autopm_get_device(sdkp->device) < 0)
+ blk_set_queue_dying(sdkp->disk->queue);
async_synchronize_full_domain(&scsi_sd_pm_domain);
device_del(&sdkp->dev);
--
2.34.1
^ permalink raw reply related [flat|nested] only message in thread
only message in thread, other threads:[~2026-11-23 9:45 UTC | newest]
Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-23 9:45 [PATCH] [ Upstream commit 085bb18a0b91007f04ddca13af30277c78040157 ] Yi Xu
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox