* [PATCH] [ Upstream commit 085bb18a0b91007f04ddca13af30277c78040157 ]
@ 2026-11-23 9:45 Yi Xu
2026-11-23 12:16 ` Greg KH
0 siblings, 1 reply; 4+ messages 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] 4+ messages in thread* Re: [PATCH] [ Upstream commit 085bb18a0b91007f04ddca13af30277c78040157 ]
2026-11-23 9:45 [PATCH] [ Upstream commit 085bb18a0b91007f04ddca13af30277c78040157 ] Yi Xu
@ 2026-11-23 12:16 ` Greg KH
2026-11-24 3:27 ` [PATCH 5.10.y] scsi: sd: Mark the disk dead if runtime resume fails in sd_remove() Yi Xu
0 siblings, 1 reply; 4+ messages in thread
From: Greg KH @ 2026-11-23 12:16 UTC (permalink / raw)
To: Yi Xu; +Cc: stable, sashal, bvanassche, hulianqin
On Mon, Nov 23, 2026 at 05:45:02PM +0800, Yi Xu wrote:
> scsi: sd: Mark the queue dying if runtime resume fails in sd_remove()
This is a very odd backport. Your subject is off, and this line is
wrong?
Please look at the thousands of examples of how to do this on the stable
mailing list so that we can accept this.
thanks,
greg k-h
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH 5.10.y] scsi: sd: Mark the disk dead if runtime resume fails in sd_remove()
2026-11-23 12:16 ` Greg KH
@ 2026-11-24 3:27 ` Yi Xu
2026-11-24 14:50 ` Sasha Levin
0 siblings, 1 reply; 4+ messages in thread
From: Yi Xu @ 2026-11-24 3:27 UTC (permalink / raw)
To: stable; +Cc: gregkh, sashal, bvanassche, hulianqin
[ Upstream commit 085bb18a0b91007f04ddca13af30277c78040157 ]
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.
[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,
Per Greg's feedback, this resend fixes the message layout: the subject
line now matches the upstream title, and the upstream anchor line is in
the expected format and position. The code change and the equivalence
argument are unchanged from the previous submission.
Manual backport of upstream commit 085bb18 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 bracketed 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] 4+ messages in thread* Re: [PATCH 5.10.y] scsi: sd: Mark the disk dead if runtime resume fails in sd_remove()
2026-11-24 3:27 ` [PATCH 5.10.y] scsi: sd: Mark the disk dead if runtime resume fails in sd_remove() Yi Xu
@ 2026-11-24 14:50 ` Sasha Levin
0 siblings, 0 replies; 4+ messages in thread
From: Sasha Levin @ 2026-11-24 14:50 UTC (permalink / raw)
To: stable; +Cc: Sasha Levin, gregkh, bvanassche, hulianqin, Yi Xu
> [ Upstream commit 085bb18a0b91007f04ddca13af30277c78040157 ]
This is only in 7.3 so far. 6.18, 6.12, 6.6, 6.1 and 5.15 need it
before 5.10 can take it, so please send backports for those trees too.
On 6.18 through 6.1 it is only a context adjustment in sd_remove(),
which still has the old "static int sd_remove(struct device *dev)" form.
5.15 also needs 7e9a46004b47 ("block: set QUEUE_FLAG_DYING unconditionally in
blk_mark_disk_dead()") first. Without it, blk_mark_disk_dead() there does not
set QUEUE_FLAG_DYING and del_gendisk() leaves the queue frozen, so
sd_shutdown() can block forever in blk_queue_enter() when the autopm get fails
on a still-active device.
> [The upstream fix calls blk_mark_disk_dead(), introduced by
> commit 6f8191fdf41d ("block: simplify disk shutdown"). That
blk_mark_disk_dead() was added by 7a5428dcb790 ("block: fix surprise
removal for drivers calling blk_set_queue_dying"), not 6f8191fdf41d.
> 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
On 5.10 del_gendisk() does not mark the queue dying through
blk_unregister_queue(). That happens in blk_cleanup_queue() from
__scsi_remove_device(), after sd_remove() returns.
> The fix builds cleanly on v5.10.274.
Did you reproduce the hang on 5.10 and test the fix there, or only
build it?
Please send a v3.
--
Thanks,
Sasha
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-11-24 14:51 UTC | newest]
Thread overview: 4+ messages (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
2026-11-23 12:16 ` Greg KH
2026-11-24 3:27 ` [PATCH 5.10.y] scsi: sd: Mark the disk dead if runtime resume fails in sd_remove() Yi Xu
2026-11-24 14:50 ` Sasha Levin
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox