Skip to contentSkip to conversation

[PATCH] [ Upstream commit 085bb18a0b91007f04ddca13af30277c78040157 ]

PATCH In review Yi Xu 2 participants 3 messages Nov 23 – Nov 24

Subsystems (2)
lore
Share & tools
lore

Yi Xu

author
#0 lore raw

PATCH 1/1 [PATCH] [ Upstream commit 085bb18a0b91007f04ddca13af30277c78040157 ]

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.

---
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(-)
drivers/scsi/sd.c +9 -1
index da6df9809b0c..6cf56b936f14 100644
@@ -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);
Signature · 1 line
2.34.1
maintainer
#1 lore raw
Quoted 1 line · Yi Xu

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

author
#2 lore raw

PATCH 1/1 scsi: sd: Mark the disk dead if runtime resume fails in sd_remove()

[ 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.]

---
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(-)
drivers/scsi/sd.c +9 -1
index da6df9809b0c..6cf56b936f14 100644
@@ -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);
Signature · 1 line
2.34.1