block, scsi, md: Improve suspend and resume
Subsystems (4)
v6 cover block, scsi, md: Improve suspend and resume
Hello Jens,
It is known that during the resume following a hibernate, especially when using an md RAID1 array created on top of SCSI devices, sometimes the system hangs instead of coming up properly. This patch series fixes this problem. This patch series is an alternative for Ming Lei's "block/scsi:
safe SCSI quiescing" patch series. The advantage of this patch series is that a fix for the md driver has been included.
These patches have been tested on top of the block layer for-next branch.
Please consider these changes for kernel v4.15.
Thanks,
Bart.
Changes between v5 and v6:
- Split an md patch into two patches to make it easier to review the changes.
- For the md patch that suspends I/O while the system is frozen, switched back to the freezer mechanism because this makes the code shorter and easier to review.
- Changed blk_set/clear_preempt_only() from EXPORT_SYMBOL() into EXPORT_SYMBOL_GPL().
- Made blk_set_preempt_only() behave as a test-and-set operation.
- Introduced blk_get_request_flags() and BLK_MQ_REQ_PREEMPT as requested by Christoph and reduced the number of arguments of blk_queue_enter() back from three to two.
- In scsi_device_quiesce(), moved the blk_mq_freeze_queue() call out of a critical section. Made the explanation of why the synchronize_rcu() call is necessary more detailed.
Changes between v4 and v5:
- Split blk_set_preempt_only() into two functions as requested by Christoph.
- Made blk_get_request() trigger WARN_ONCE() if it is attempted to allocate a request while the system is frozen. This is a big help to identify drivers that submit I/O while the system is frozen.
- Since kernel thread freezing is on its way out, reworked the approach for avoiding that the md driver submits I/O while the system is frozen such that the approach no longer depends on the kernel thread freeze mechanism.
- Fixed the (theoretical) deadlock in scsi_device_quiesce() that was identified by Ming.
Changes between v3 and v4:
- Made sure that this patch series not only works for scsi-mq but also for the legacy SCSI stack.
- Removed an smp_rmb()/smp_wmb() pair from the hot path and added a comment that explains why that is safe.
- Reordered the patches in this patch series.
Changes between v2 and v3:
- Made md kernel threads freezable.
- Changed the approach for quiescing SCSI devices again.
- Addressed Ming's review comments.
Changes compared to v1 of this patch series:
- Changed the approach and rewrote the patch series.
Bart Van Assche (8):
md: Rename md_notifier into md_reboot_notifier
md: Introduce md_stop_all_writes()
md: Neither resync nor reshape while the system is frozen
block: Introduce blk_get_request_flags()
block: Introduce BLK_MQ_REQ_PREEMPT
ide, scsi: Tell the block layer at request allocation time about
preempt requests
block: Add the QUEUE_FLAG_PREEMPT_ONLY request queue flag block, scsi: Make SCSI device suspend and resume work reliably
Ming Lei (1):
block: Make q_usage_counter also track legacy requests
block/blk-core.c | 127 ++++++++++++++++++++++++++++++++++++++++-------- block/blk-mq.c | 16 +++--- block/blk-timeout.c | 2 +- drivers/ide/ide-pm.c | 4 +- drivers/md/md.c | 45 ++++++++++++++--- drivers/scsi/scsi_lib.c | 33 ++++++++----- fs/block_dev.c | 4 +- include/linux/blk-mq.h | 1 + include/linux/blkdev.h | 11 ++++- 9 files changed, 189 insertions(+), 54 deletions(-)
Signature · 1 line
2.14.2
PATCH v6 1/9 md: Rename md_notifier into md_reboot_notifier
This avoids confusion with the pm notifier that will be added through a later patch.
--- drivers/md/md.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/md/md.c +3 -3
@@ -8966,7 +8966,7 @@ static int md_notify_reboot(struct notifier_block *this,return NOTIFY_DONE ; } -static struct notifier_block md_notifier = { +static struct notifier_block md_reboot_notifier = { .notifier_call = md_notify_reboot, .next =NULL , .priority =INT_MAX ,/* before any real devices */
@@ -9003,7 +9003,7 @@ static int __init md_init(void)blk_register_region (MKDEV (mdp_major,0 ),1UL <<MINORBITS ,THIS_MODULE , md_probe,NULL ,NULL ); -register_reboot_notifier (&md_notifier); +register_reboot_notifier (&md_reboot_notifier); raid_table_header =register_sysctl_table (raid_root_table);md_geninit ();
@@ -9243,7 +9243,7 @@ static __exit void md_exit(void)unregister_blkdev (MD_MAJOR ,"md" );unregister_blkdev (mdp_major,"mdp" ); -unregister_reboot_notifier (&md_notifier); +unregister_reboot_notifier (&md_reboot_notifier);unregister_sysctl_table (raid_table_header);/* We cannot unload the modules while some process is
Signature · 1 line
2.14.2
PATCH v6 2/9 md: Introduce md_stop_all_writes()
In reply to Bart Van AsscheIntroduce md_stop_all_writes() because the next patch will add a second caller for this function. This patch does not change any functionality.
--- drivers/md/md.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/md/md.c +7 -2
@@ -8937,8 +8937,7 @@ int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors, }EXPORT_SYMBOL_GPL (rdev_clear_badblocks); -static int md_notify_reboot (struct notifier_block *this, -unsigned long code,void *x) +static void md_stop_all_writes (void ) {struct list_head *tmp;struct mddev *mddev;
@@ -8962,6 +8961,12 @@ static int md_notify_reboot(struct notifier_block *this,*/ if (need_delay)mdelay (1000 *1 ); +} + +static int md_notify_reboot (struct notifier_block *this, +unsigned long code,void *x) +{ +md_stop_all_writes ();return NOTIFY_DONE ; }
Signature · 1 line
2.14.2
PATCH v6 3/9 md: Neither resync nor reshape while the system is frozen
In reply to Bart Van AsscheSome people use the md driver on laptops and use the suspend and resume functionality. Since it is essential that submitting of new I/O requests stops before a hibernation image is created, interrupt the md resync and reshape actions if the system is being frozen. Note: the resync and reshape will restart after the system is resumed and a message similar to the following will appear in the system log:
md: md0: data-check interrupted.
--- drivers/md/md.c | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-)
drivers/md/md.c +29 -1
@@ -66,6 +66,8 @@#include <linux/raid/md_u.h> #include <linux/slab.h> #include <linux/percpu-refcount.h> +#include <linux/freezer.h> +#include <linux/suspend.h> #include <trace/events/block.h> #include "md.h"
@@ -7439,6 +7441,7 @@ static int md_thread(void *arg)*/ allow_signal (SIGKILL ); +set_freezable ();while (!kthread_should_stop ()) {/* We need to wait INTERRUPTIBLE so that
@@ -7449,7 +7452,7 @@ static int md_thread(void *arg)if (signal_pending (current))flush_signals (current); - wait_event_interruptible_timeout + wait_event_freezable_timeout (thread->wqueue, test_bit(THREAD_WAKEUP, &thread->flags) || kthread_should_stop() || kthread_should_park(),
@@ -8963,6 +8966,29 @@ static void md_stop_all_writes(void)mdelay (1000 *1 ); } +/* +* Ensure that neither resyncing nor reshaping occurs while the system is +* frozen. +*/ +static int md_notify_pm (struct notifier_block *bl,unsigned long state, +void *unused) +{ +pr_debug ("%s: state = %ld\n" , __func__, state); + +switch (state) { +case PM_HIBERNATION_PREPARE : +case PM_SUSPEND_PREPARE : +case PM_RESTORE_PREPARE : +md_stop_all_writes (); +break ; + } +return NOTIFY_DONE ; +} + +static struct notifier_block md_pm_notifier = { + .notifier_call = md_notify_pm, +}; +static int md_notify_reboot (struct notifier_block *this,unsigned long code,void *x) {
@@ -9009,6 +9035,7 @@ static int __init md_init(void) md_probe, NULL, NULL);register_reboot_notifier (&md_reboot_notifier); +register_pm_notifier (&md_pm_notifier); raid_table_header =register_sysctl_table (raid_root_table);md_geninit ();
@@ -9248,6 +9275,7 @@ static __exit void md_exit(void)unregister_blkdev (MD_MAJOR ,"md" );unregister_blkdev (mdp_major,"mdp" ); +unregister_pm_notifier (&md_pm_notifier);unregister_reboot_notifier (&md_reboot_notifier);unregister_sysctl_table (raid_table_header);
Signature · 1 line
2.14.2
PATCH v6 4/9 block: Make q_usage_counter also track legacy requests
In reply to Bart Van AsscheFrom: Ming Lei <[email protected]>
This patch makes it possible to pause request allocation for the legacy block layer by calling blk_mq_freeze_queue() and blk_mq_unfreeze_queue().
[ bvanassche: Combined two patches into one, edited a comment and made sure
REQ_NOWAIT is handled properly in blk_old_get_request() ]
--- block/blk-core.c | 12 ++++++++++++ block/blk-mq.c | 10 ++-------- 2 files changed, 14 insertions(+), 8 deletions(-)
block/blk-core.c +12 -0
@@ -610,6 +610,9 @@ void blk_set_queue_dying(struct request_queue *q) }spin_unlock_irq (q->queue_lock); } + +/* Make blk_queue_enter() reexamine the DYING flag. */ +wake_up_all (&q->mq_freeze_wq); }EXPORT_SYMBOL_GPL (blk_set_queue_dying);
@@ -1395,16 +1398,22 @@ static struct request *blk_old_get_request(struct request_queue *q, unsigned int op, gfp_t gfp_mask) {struct request *rq; +int ret =0 ;WARN_ON_ONCE (q->mq_ops);/* create ioc upfront */ create_io_context (gfp_mask, q->node); + ret =blk_queue_enter (q, !(gfp_mask & __GFP_DIRECT_RECLAIM) || + (op &REQ_NOWAIT )); +if (ret) +return ERR_PTR (ret);spin_lock_irq (q->queue_lock); rq =get_request (q, op,NULL , gfp_mask);if (IS_ERR (rq)) {spin_unlock_irq (q->queue_lock); +blk_queue_exit (q);return rq; }
@@ -1576,6 +1585,7 @@ void __blk_put_request(struct request_queue *q, struct request *req)blk_free_request (rl, req);freed_request (rl, sync, rq_flags);blk_put_rl (rl); +blk_queue_exit (q); } }EXPORT_SYMBOL_GPL (__blk_put_request);
@@ -1857,8 +1867,10 @@ static blk_qc_t blk_queue_bio(struct request_queue *q, struct bio *bio)* Grab a free request. This is might sleep but can not fail. * Returns with the queue unlocked. */ +blk_queue_enter_live (q); req =get_request (q, bio->bi_opf, bio,GFP_NOIO );if (IS_ERR (req)) { +blk_queue_exit (q);__wbt_done (q->rq_wb, wb_acct);if (PTR_ERR (req) == -ENOMEM ) bio->bi_status =BLK_STS_RESOURCE ;
block/blk-mq.c +2 -8
@@ -125,7 +125,8 @@ void blk_freeze_queue_start(struct request_queue *q) freeze_depth =atomic_inc_return (&q->mq_freeze_depth);if (freeze_depth ==1 ) {percpu_ref_kill (&q->q_usage_counter); -blk_mq_run_hw_queues (q, false); +if (q->mq_ops) +blk_mq_run_hw_queues (q, false); } }EXPORT_SYMBOL_GPL (blk_freeze_queue_start);
@@ -255,13 +256,6 @@ void blk_mq_wake_waiters(struct request_queue *q)queue_for_each_hw_ctx (q, hctx, i)if (blk_mq_hw_queue_mapped (hctx))blk_mq_tag_wakeup_all (hctx->tags, true); - -/* -* If we are called because the queue has now been marked as -* dying, we need to ensure that processes currently waiting on -* the queue are notified as well. -*/ -wake_up_all (&q->mq_freeze_wq); }bool blk_mq_can_queue (struct blk_mq_hw_ctx *hctx)
Signature · 1 line
2.14.2
PATCH v6 5/9 block: Introduce blk_get_request_flags()
In reply to Bart Van AsscheA side effect of this patch is that the GFP mask that is passed to several allocation functions in the legacy block layer is changed from GFP_KERNEL into __GFP_DIRECT_RECLAIM.
--- block/blk-core.c | 50 +++++++++++++++++++++++++++++++++++--------------- include/linux/blkdev.h | 3 +++ 2 files changed, 38 insertions(+), 15 deletions(-)
block/blk-core.c +35 -15
@@ -1157,7 +1157,7 @@ int blk_update_nr_requests(struct request_queue *q, unsigned int nr)* @rl: request list to allocate from * @op: operation and flags * @bio: bio to allocate request for (can be %NULL) -* @gfp_mask: allocation mask +* @flags: BLQ_MQ_REQ_* flags * * Get a free request from @q. This function may fail under memory * pressure or if @q is dead.
@@ -1167,7 +1167,7 @@ int blk_update_nr_requests(struct request_queue *q, unsigned int nr)* Returns request pointer on success, with @q->queue_lock *not held*. */ static struct request *__get_request (struct request_list *rl,unsigned int op, -struct bio *bio,gfp_t gfp_mask) +struct bio *bio,unsigned int flags) {struct request_queue *q = rl->q;struct request *rq;
@@ -1176,6 +1176,8 @@ static struct request *__get_request(struct request_list *rl, unsigned int op,struct io_cq *icq =NULL ;const bool is_sync =op_is_sync (op);int may_queue; +gfp_t gfp_mask = flags &BLK_MQ_REQ_NOWAIT ?GFP_ATOMIC : + __GFP_DIRECT_RECLAIM;req_flags_t rq_flags =RQF_ALLOCED ;lockdep_assert_held (q->queue_lock);
@@ -1336,7 +1338,7 @@ static struct request *__get_request(struct request_list *rl, unsigned int op,* @q: request_queue to allocate request from * @op: operation and flags * @bio: bio to allocate request for (can be %NULL) -* @gfp_mask: allocation mask +* @flags: BLK_MQ_REQ_* flags. * * Get a free request from @q. If %__GFP_DIRECT_RECLAIM is set in @gfp_mask, * this function keeps retrying under memory pressure and fails iff @q is dead.
@@ -1346,7 +1348,7 @@ static struct request *__get_request(struct request_list *rl, unsigned int op,* Returns request pointer on success, with @q->queue_lock *not held*. */ static struct request *get_request (struct request_queue *q,unsigned int op, -struct bio *bio,gfp_t gfp_mask) +struct bio *bio,unsigned int flags) {const bool is_sync =op_is_sync (op);DEFINE_WAIT (wait);
@@ -1358,7 +1360,7 @@ static struct request *get_request(struct request_queue *q, unsigned int op, rl =blk_get_rl (q, bio);/* transferred to @rq on success */ retry: - rq =__get_request (rl, op, bio, gfp_mask); + rq =__get_request (rl, op, bio, flags);if (!IS_ERR (rq))return rq;
@@ -1367,7 +1369,7 @@ static struct request *get_request(struct request_queue *q, unsigned int op,return ERR_PTR (-EAGAIN ); } -if (!gfpflags_allow_blocking (gfp_mask) ||unlikely (blk_queue_dying (q))) { +if ((flags &BLK_MQ_REQ_NOWAIT ) ||unlikely (blk_queue_dying (q))) {blk_put_rl (rl);return rq; }
@@ -1394,10 +1396,13 @@ static struct request *get_request(struct request_queue *q, unsigned int op, goto retry; } +/* flags: BLK_MQ_REQ_PREEMPT and/or BLK_MQ_REQ_NOWAIT. */ static struct request *blk_old_get_request (struct request_queue *q, -unsigned int op,gfp_t gfp_mask) +unsigned int op,unsigned int flags) {struct request *rq; +gfp_t gfp_mask = flags &BLK_MQ_REQ_NOWAIT ?GFP_ATOMIC : + __GFP_DIRECT_RECLAIM;int ret =0 ;WARN_ON_ONCE (q->mq_ops);
@@ -1410,7 +1415,7 @@ static struct request *blk_old_get_request(struct request_queue *q,if (ret)return ERR_PTR (ret);spin_lock_irq (q->queue_lock); - rq =get_request (q, op,NULL , gfp_mask); + rq =get_request (q, op,NULL , flags);if (IS_ERR (rq)) {spin_unlock_irq (q->queue_lock);blk_queue_exit (q);
@@ -1424,25 +1429,40 @@ static struct request *blk_old_get_request(struct request_queue *q,return rq; } -struct request *blk_get_request (struct request_queue *q,unsigned int op, -gfp_t gfp_mask) +/** +* blk_get_request_flags - allocate a request +* @q: request queue to allocate a request for +* @op: operation (REQ_OP_*) and REQ_* flags, e.g. REQ_SYNC. +* @flags: BLK_MQ_REQ_* flags, e.g. BLK_MQ_REQ_NOWAIT. +*/ +struct request *blk_get_request_flags (struct request_queue *q,unsigned int op, +unsigned int flags) {struct request *req; +WARN_ON_ONCE (op &REQ_NOWAIT ); +WARN_ON_ONCE (flags & ~BLK_MQ_REQ_NOWAIT ); +if (q->mq_ops) { - req =blk_mq_alloc_request (q, op, - (gfp_mask & __GFP_DIRECT_RECLAIM) ? -0 :BLK_MQ_REQ_NOWAIT ); + req =blk_mq_alloc_request (q, op, flags);if (!IS_ERR (req) && q->mq_ops->initialize_rq_fn) q->mq_ops->initialize_rq_fn (req); }else { - req =blk_old_get_request (q, op, gfp_mask); + req =blk_old_get_request (q, op, flags);if (!IS_ERR (req) && q->initialize_rq_fn) q->initialize_rq_fn (req); }return req; } +EXPORT_SYMBOL (blk_get_request_flags); + +struct request *blk_get_request (struct request_queue *q,unsigned int op, +gfp_t gfp_mask) +{ +return blk_get_request_flags (q, op, gfp_mask & __GFP_DIRECT_RECLAIM ? +0 :BLK_MQ_REQ_NOWAIT ); +}EXPORT_SYMBOL (blk_get_request);/**
@@ -1868,7 +1888,7 @@ static blk_qc_t blk_queue_bio(struct request_queue *q, struct bio *bio)* Returns with the queue unlocked. */ blk_queue_enter_live (q); - req =get_request (q, bio->bi_opf, bio,GFP_NOIO ); + req =get_request (q, bio->bi_opf, bio,0 );if (IS_ERR (req)) {blk_queue_exit (q);__wbt_done (q->rq_wb, wb_acct);
include/linux/blkdev.h +3 -0
@@ -940,6 +940,9 @@ extern void blk_rq_init(struct request_queue *q, struct request *rq);extern void blk_init_request_from_bio (struct request *req,struct bio *bio);extern void blk_put_request (struct request *);extern void __blk_put_request (struct request_queue *,struct request *); +extern struct request *blk_get_request_flags (struct request_queue *, +unsigned int op, +unsigned int flags);extern struct request *blk_get_request (struct request_queue *,unsigned int op,gfp_t gfp_mask);extern void blk_requeue_request (struct request_queue *,struct request *);
Signature · 1 line
2.14.2
PATCH v6 6/9 block: Introduce BLK_MQ_REQ_PREEMPT
In reply to Bart Van AsscheSet RQF_PREEMPT if BLK_MQ_REQ_PREEMPT is passed to blk_get_request_flags().
--- block/blk-core.c | 4 +++- block/blk-mq.c | 2 ++ include/linux/blk-mq.h | 1 + 3 files changed, 6 insertions(+), 1 deletion(-)
block/blk-core.c +3 -1
@@ -1260,6 +1260,8 @@ static struct request *__get_request(struct request_list *rl, unsigned int op,blk_rq_set_rl (rq, rl); rq->cmd_flags = op; rq->rq_flags = rq_flags; +if (flags &BLK_MQ_REQ_PREEMPT ) + rq->rq_flags |=RQF_PREEMPT ;/* init elvpriv */ if (rq_flags &RQF_ELVPRIV ) {
@@ -1441,7 +1443,7 @@ struct request *blk_get_request_flags(struct request_queue *q, unsigned int op,struct request *req;WARN_ON_ONCE (op &REQ_NOWAIT ); -WARN_ON_ONCE (flags & ~BLK_MQ_REQ_NOWAIT ); +WARN_ON_ONCE (flags & ~(BLK_MQ_REQ_NOWAIT |BLK_MQ_REQ_PREEMPT ));if (q->mq_ops) { req =blk_mq_alloc_request (q, op, flags);
block/blk-mq.c +2 -0
@@ -290,6 +290,8 @@ static struct request *blk_mq_rq_ctx_init(struct blk_mq_alloc_data *data, rq->q = data->q; rq->mq_ctx = data->ctx; rq->cmd_flags = op; +if (data->flags &BLK_MQ_REQ_PREEMPT ) + rq->rq_flags |=RQF_PREEMPT ;if (blk_queue_io_stat (data->q)) rq->rq_flags |=RQF_IO_STAT ;/* do not touch atomic flags, it needs atomic ops against the timer */
include/linux/blk-mq.h +1 -0
@@ -200,6 +200,7 @@ enum {BLK_MQ_REQ_NOWAIT = (1 <<0 ),/* return when out of requests */ BLK_MQ_REQ_RESERVED = (1 <<1 ),/* allocate from reserved pool */ BLK_MQ_REQ_INTERNAL = (1 <<2 ),/* allocate internal/sched tag */ + BLK_MQ_REQ_PREEMPT = (1 << 3), /* set RQF_PREEMPT */ }; struct request *blk_mq_alloc_request(struct request_queue *q, unsigned int op,
Signature · 1 line
2.14.2
PATCH v6 7/9 ide, scsi: Tell the block layer at request allocation time about preempt requests
In reply to Bart Van AsscheConvert blk_get_request(q, op, __GFP_RECLAIM) into blk_get_request(q, op, BLK_MQ_PREEMPT). This patch does not change any functionality.
--- drivers/ide/ide-pm.c | 4 ++-- drivers/scsi/scsi_lib.c | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-)
drivers/ide/ide-pm.c +2 -2
@@ -89,9 +89,9 @@ int generic_ide_resume(struct device *dev) }memset (&rqpm,0 ,sizeof (rqpm)); - rq =blk_get_request (drive->queue,REQ_OP_DRV_IN , __GFP_RECLAIM); + rq =blk_get_request_flags (drive->queue,REQ_OP_DRV_IN , +BLK_MQ_REQ_PREEMPT );ide_req (rq)->type =ATA_PRIV_PM_RESUME ; - rq->rq_flags |=RQF_PREEMPT ; rq->special = &rqpm; rqpm.pm_step =IDE_PM_START_RESUME ; rqpm.pm_state =PM_EVENT_ON ;
drivers/scsi/scsi_lib.c +3 -3
@@ -252,9 +252,9 @@ int scsi_execute(struct scsi_device *sdev, const unsigned char *cmd,struct scsi_request *rq;int ret =DRIVER_ERROR <<24 ; - req =blk_get_request (sdev->request_queue, + req =blk_get_request_flags (sdev->request_queue, data_direction ==DMA_TO_DEVICE ? -REQ_OP_SCSI_OUT :REQ_OP_SCSI_IN , __GFP_RECLAIM); +REQ_OP_SCSI_OUT :REQ_OP_SCSI_IN ,BLK_MQ_REQ_PREEMPT );if (IS_ERR (req))return ret; rq =scsi_req (req);
@@ -268,7 +268,7 @@ int scsi_execute(struct scsi_device *sdev, const unsigned char *cmd, rq->retries = retries; req->timeout = timeout; req->cmd_flags |= flags; - req->rq_flags |= rq_flags |RQF_QUIET |RQF_PREEMPT ; + req->rq_flags |= rq_flags |RQF_QUIET ;/* * head injection *required* here otherwise quiesce won't work
Signature · 1 line
2.14.2
Re: [PATCH v6 7/9] ide, scsi: Tell the block layer at request allocation time about preempt requests
From: Bart Van Assche <[email protected]>
Date: Wed, 4 Oct 2017 17:01:08 -0700
Quoted 10 lines
Convert
blk_get_request(q, op, __GFP_RECLAIM)intoblk_get_request(q, op, BLK_MQ_PREEMPT). This patch does not change any functionality.Signed-off-by Bart Van Assche <[email protected]>Cc Martin K. Petersen <[email protected]>Cc David S. Miller <[email protected]>Cc Ming Lei <[email protected]>Cc Christoph Hellwig <[email protected]>Cc Hannes Reinecke <[email protected]>Cc Johannes Thumshirn <[email protected]>
For IDE:
PATCH v6 8/9 block: Add the QUEUE_FLAG_PREEMPT_ONLY request queue flag
In reply to Bart Van AsscheThis flag will be used in the next patch to let the block layer core know whether or not a SCSI request queue has been quiesced. A quiesced SCSI queue namely only processes RQF_PREEMPT requests.
--- block/blk-core.c | 29 +++++++++++++++++++++++++++++ include/linux/blkdev.h | 6 ++++++ 2 files changed, 35 insertions(+)
block/blk-core.c +29 -0
@@ -346,6 +346,35 @@ void blk_sync_queue(struct request_queue *q) }EXPORT_SYMBOL (blk_sync_queue); +/** +* blk_set_preempt_only - set QUEUE_FLAG_PREEMPT_ONLY. +* +* Returns the previous value of the PREEMPT_ONLY flag - 0 if the flag was not +* set and 1 if the flag was already set. +*/ +int blk_set_preempt_only (struct request_queue *q) +{ +unsigned long flags; +int res; + +spin_lock_irqsave (q->queue_lock, flags); + res =queue_flag_test_and_set (QUEUE_FLAG_PREEMPT_ONLY , q); +spin_unlock_irqrestore (q->queue_lock, flags); + +return res; +} +EXPORT_SYMBOL_GPL (blk_set_preempt_only); + +void blk_clear_preempt_only (struct request_queue *q) +{ +unsigned long flags; + +spin_lock_irqsave (q->queue_lock, flags); +queue_flag_clear (QUEUE_FLAG_PREEMPT_ONLY , q); +spin_unlock_irqrestore (q->queue_lock, flags); +} +EXPORT_SYMBOL_GPL (blk_clear_preempt_only); +/** * __blk_run_queue_uncond - run a queue whether or not it has been stopped * @q: The queue to run
include/linux/blkdev.h +6 -0
@@ -631,6 +631,7 @@ struct request_queue {#define QUEUE_FLAG_REGISTERED 26/* queue has been registered to a disk */ #define QUEUE_FLAG_SCSI_PASSTHROUGH 27/* queue supports SCSI commands */ #define QUEUE_FLAG_QUIESCED 28/* queue has been quiesced */ +#define QUEUE_FLAG_PREEMPT_ONLY 29/* only process REQ_PREEMPT requests */ #define QUEUE_FLAG_DEFAULT ((1 << QUEUE_FLAG_IO_STAT) | \ (1 << QUEUE_FLAG_STACKABLE) | \
@@ -735,6 +736,11 @@ static inline void queue_flag_clear(unsigned int flag, struct request_queue *q) ((rq)->cmd_flags & (REQ_FAILFAST_DEV |REQ_FAILFAST_TRANSPORT | \ REQ_FAILFAST_DRIVER))#define blk_queue_quiesced (q) test_bit(QUEUE_FLAG_QUIESCED, &(q)->queue_flags) +#define blk_queue_preempt_only (q) \ + test_bit(QUEUE_FLAG_PREEMPT_ONLY, &(q)->queue_flags) + +extern int blk_set_preempt_only (struct request_queue *q); +extern void blk_clear_preempt_only (struct request_queue *q);static inline bool blk_account_rq (struct request *rq) {
Signature · 1 line
2.14.2
PATCH v6 9/9 block, scsi: Make SCSI device suspend and resume work reliably
In reply to Bart Van AsscheIt is essential during suspend and resume that neither the filesystem state nor the filesystem metadata in RAM changes. This is why while the hibernation image is being written or restored that SCSI devices are quiesced. The SCSI core quiesces devices through scsi_device_quiesce() and scsi_device_resume(). In the SDEV_QUIESCE state execution of non-preempt requests is deferred. This is realized by returning BLKPREP_DEFER from inside scsi_prep_state_check() for quiesced SCSI devices. Avoid that a full queue prevents power management requests to be submitted by deferring allocation of non-preempt requests for devices in the quiesced state. This patch has been tested by running the following commands and by verifying that after resume the fio job is still running:
for d in /sys/class/block/sd*[a-z]; do
hcil=$(readlink "$d/device")
hcil=${hcil#../../../}
echo 4 > "$d/queue/nr_requests"
echo 1 > "/sys/class/scsi_device/$hcil/device/queue_depth"
done
bdev=$(readlink /dev/disk/by-uuid/5217d83f-213e-4b42-b86e-20013325ba6c) bdev=${bdev#../../}
hcil=$(readlink "/sys/block/$bdev/device")
hcil=${hcil#../../../}
fio --name="$bdev" --filename="/dev/$bdev" --buffered=0 --bs=512 --rw=randread \
--ioengine=libaio --numjobs=4 --iodepth=16 --iodepth_batch=1 --thread \
--loops=$((2**31)) &
pid=$!
sleep 1
systemctl hibernate
sleep 10
kill $pid
References: "I/O hangs after resuming from suspend-to-ram" (https://marc.info/?l=linux-block&m=150340235201348).
--- block/blk-core.c | 38 ++++++++++++++++++++++++++++++-------- block/blk-mq.c | 4 ++-- block/blk-timeout.c | 2 +- drivers/scsi/scsi_lib.c | 27 +++++++++++++++++++-------- fs/block_dev.c | 4 ++-- include/linux/blkdev.h | 2 +- 6 files changed, 55 insertions(+), 22 deletions(-)
block/blk-core.c +30 -8
@@ -371,6 +371,7 @@ void blk_clear_preempt_only(struct request_queue *q)spin_lock_irqsave (q->queue_lock, flags);queue_flag_clear (QUEUE_FLAG_PREEMPT_ONLY , q); +wake_up_all (&q->mq_freeze_wq);spin_unlock_irqrestore (q->queue_lock, flags); }EXPORT_SYMBOL_GPL (blk_clear_preempt_only);
@@ -792,15 +793,34 @@ struct request_queue *blk_alloc_queue(gfp_t gfp_mask) }EXPORT_SYMBOL (blk_alloc_queue); -int blk_queue_enter (struct request_queue *q,bool nowait) +/** +* blk_queue_enter() - try to increase q->q_usage_counter +* @q: request queue pointer +* @flags: BLK_MQ_REQ_NOWAIT and/or BLK_MQ_REQ_PREEMPT +*/ +int blk_queue_enter (struct request_queue *q,unsigned int flags) { +const bool preempt = flags &BLK_MQ_REQ_PREEMPT ; +while (true) {int ret; -if (percpu_ref_tryget_live (&q->q_usage_counter)) -return 0 ; +if (percpu_ref_tryget_live (&q->q_usage_counter)) { +/* +* The code that sets the PREEMPT_ONLY flag is +* responsible for ensuring that that flag is globally +* visible before the queue is unfrozen. +*/ +if (preempt || !blk_queue_preempt_only (q)) { +return 0 ; + }else { +percpu_ref_put (&q->q_usage_counter); +WARN_ONCE ("%s: Attempt to allocate non-preempt request in preempt-only mode.\n" , +kobject_name (q->kobj.parent)); + } + } -if (nowait) +if (flags &BLK_MQ_REQ_NOWAIT )return -EBUSY ;/*
@@ -813,7 +833,8 @@ int blk_queue_enter(struct request_queue *q, bool nowait)smp_rmb (); ret =wait_event_interruptible (q->mq_freeze_wq, - !atomic_read (&q->mq_freeze_depth) || + (atomic_read (&q->mq_freeze_depth) ==0 && + (preempt || !blk_queue_preempt_only (q))) ||blk_queue_dying (q));if (blk_queue_dying (q))return -ENODEV ;
@@ -1441,8 +1462,7 @@ static struct request *blk_old_get_request(struct request_queue *q,/* create ioc upfront */ create_io_context (gfp_mask, q->node); - ret =blk_queue_enter (q, !(gfp_mask & __GFP_DIRECT_RECLAIM) || - (op &REQ_NOWAIT )); + ret =blk_queue_enter (q, flags);if (ret)return ERR_PTR (ret);spin_lock_irq (q->queue_lock);
@@ -2263,8 +2283,10 @@ blk_qc_t generic_make_request(struct bio *bio) current->bio_list = bio_list_on_stack; do { struct request_queue *q = bio->bi_disk->queue; + unsigned int flags = bio->bi_opf & REQ_NOWAIT ? + BLK_MQ_REQ_NOWAIT : 0; - if (likely(blk_queue_enter(q, bio->bi_opf & REQ_NOWAIT) == 0)) { + if (likely(blk_queue_enter(q, flags) == 0)) { struct bio_list lower, same; /* Create a fresh bio_list for all subordinate requests */
block/blk-mq.c +2 -2
@@ -386,7 +386,7 @@ struct request *blk_mq_alloc_request(struct request_queue *q, unsigned int op,struct request *rq;int ret; - ret =blk_queue_enter (q, flags &BLK_MQ_REQ_NOWAIT ); + ret =blk_queue_enter (q, flags);if (ret)return ERR_PTR (ret);
@@ -425,7 +425,7 @@ struct request *blk_mq_alloc_request_hctx(struct request_queue *q,if (hctx_idx >= q->nr_hw_queues)return ERR_PTR (-EIO ); - ret =blk_queue_enter (q, true); + ret =blk_queue_enter (q, flags);if (ret)return ERR_PTR (ret);
block/blk-timeout.c +1 -1
@@ -134,7 +134,7 @@ void blk_timeout_work(struct work_struct *work)struct request *rq, *tmp;int next_set =0 ; -if (blk_queue_enter (q, true)) +if (blk_queue_enter (q,BLK_MQ_REQ_NOWAIT |BLK_MQ_REQ_PREEMPT ))return ;spin_lock_irqsave (q->queue_lock, flags);
drivers/scsi/scsi_lib.c +19 -8
@@ -2926,21 +2926,30 @@ static void scsi_wait_for_queuecommand(struct scsi_device *sdev)int scsi_device_quiesce (struct scsi_device *sdev) { +struct request_queue *q = sdev->request_queue;int err; +blk_mq_freeze_queue (q); +if (blk_set_preempt_only (q)) { +blk_mq_unfreeze_queue (q); +return -EINVAL ; + } +/* +* Ensure that the effect of blk_set_preempt_only() will be visible +* for percpu_ref_tryget() callers that occur after the queue +* unfreeze. See also https://lwn.net/Articles/573497/. +*/ +synchronize_rcu (); +blk_mq_unfreeze_queue (q); +mutex_lock (&sdev->state_mutex); err =scsi_device_set_state (sdev,SDEV_QUIESCE );mutex_unlock (&sdev->state_mutex);if (err) -return err; +blk_clear_preempt_only (q); -scsi_run_queue (sdev->request_queue); -while (atomic_read (&sdev->device_busy)) { -msleep_interruptible (200 ); -scsi_run_queue (sdev->request_queue); - } -return 0 ; +return err; }EXPORT_SYMBOL (scsi_device_quiesce);
@@ -2961,8 +2970,10 @@ void scsi_device_resume(struct scsi_device *sdev)*/ mutex_lock (&sdev->state_mutex);if (sdev->sdev_state ==SDEV_QUIESCE && -scsi_device_set_state (sdev,SDEV_RUNNING ) ==0 ) +scsi_device_set_state (sdev,SDEV_RUNNING ) ==0 ) { +blk_clear_preempt_only (sdev->request_queue);scsi_run_queue (sdev->request_queue); + }mutex_unlock (&sdev->state_mutex); }EXPORT_SYMBOL (scsi_device_resume);
fs/block_dev.c +2 -2
@@ -674,7 +674,7 @@ int bdev_read_page(struct block_device *bdev, sector_t sector,if (!ops->rw_page ||bdev_get_integrity (bdev))return result; - result =blk_queue_enter (bdev->bd_queue, false); + result =blk_queue_enter (bdev->bd_queue,0 );if (result)return result; result = ops->rw_page (bdev, sector +get_start_sect (bdev), page, false);
@@ -710,7 +710,7 @@ int bdev_write_page(struct block_device *bdev, sector_t sector,if (!ops->rw_page ||bdev_get_integrity (bdev))return -EOPNOTSUPP ; - result =blk_queue_enter (bdev->bd_queue, false); + result =blk_queue_enter (bdev->bd_queue,0 );if (result)return result;
include/linux/blkdev.h +1 -1
@@ -972,7 +972,7 @@ extern int scsi_cmd_ioctl(struct request_queue *, struct gendisk *, fmode_t,extern int sg_scsi_ioctl (struct request_queue *,struct gendisk *,fmode_t ,struct scsi_ioctl_command __user *); -extern int blk_queue_enter (struct request_queue *q,bool nowait); +extern int blk_queue_enter (struct request_queue *q,unsigned int flags);extern void blk_queue_exit (struct request_queue *q);extern void blk_start_queue (struct request_queue *q);extern void blk_start_queue_async (struct request_queue *q);
Signature · 1 line
2.14.2
Re: [PATCH v6 9/9] block, scsi: Make SCSI device suspend and resume work reliably
Quoted 3 lines · Bart Van Assche
It is essential during suspend and resume that neither the filesystem state nor the filesystem metadata in RAM changes. This is why while the hibernation image is being written or restored that SCSI devices
quiesce isn't used only for suspend and resume, And the issue isn't suspend/resume specific too. So please change the title/commit log as sort of 'make SCSI quiesce more reliable/safe'.
Quoted 74 lines
are quiesced. The SCSI core quiesces devices through
scsi_device_quiesce()andscsi_device_resume(). In theSDEV_QUIESCEstate execution of non-preempt requests is deferred. This is realized by returningBLKPREP_DEFERfrom insidescsi_prep_state_check()for quiesced SCSI devices. Avoid that a full queue prevents power management requests to be submitted by deferring allocation of non-preempt requests for devices in the quiesced state. This patch has been tested by running the following commands and by verifying that after resume the fio job is still running:for d in /sys/class/block/sd*[a-z]; do
hcil=$(readlink "$d/device")
hcil=${hcil#../../../}
echo 4 > "$d/queue/nr_requests"
echo 1 > "/sys/class/scsi_device/$hcil/device/queue_depth"
done
bdev=$(readlink/dev/disk/by-uuid/5217d83f-213e-4b42-b86e-20013325ba6c) bdev=${bdev#../../}
hcil=$(readlink "/sys/block/$bdev/device")
hcil=${hcil#../../../}
fio --name="$bdev" --filename="/dev/$bdev" --buffered=0 --bs=512 --rw=randread \
--ioengine=libaio --numjobs=4 --iodepth=16 --iodepth_batch=1 --thread \
--loops=$((2**31)) &
pid=$!
sleep 1
systemctl hibernate
sleep 10
kill $pidReported-by Oleksandr Natalenko <[email protected]>References: "I/O hangs after resuming from suspend-to-ram" (https://marc.info/?l=linux-block&m=150340235201348).
Signed-off-by Bart Van Assche <[email protected]>Cc Martin K. Petersen <[email protected]>Cc Ming Lei <[email protected]>Cc Christoph Hellwig <[email protected]>Cc Hannes Reinecke <[email protected]>Cc Johannes Thumshirn <[email protected]>--- block/blk-core.c | 38 ++++++++++++++++++++++++++++++-------- block/blk-mq.c | 4 ++-- block/blk-timeout.c | 2 +- drivers/scsi/scsi_lib.c | 27 +++++++++++++++++++-------- fs/block_dev.c | 4 ++-- include/linux/blkdev.h | 2 +- 6 files changed, 55 insertions(+), 22 deletions(-)block/blk-core.c +9
-1@@ -371,6 +371,7 @@ void blk_clear_preempt_only(struct request_queue *q)spin_lock_irqsave (q->queue_lock, flags);queue_flag_clear (QUEUE_FLAG_PREEMPT_ONLY , q); +wake_up_all (&q->mq_freeze_wq);spin_unlock_irqrestore (q->queue_lock, flags); }EXPORT_SYMBOL_GPL (blk_clear_preempt_only);@@ -792,15 +793,34 @@ struct request_queue *blk_alloc_queue(gfp_t gfp_mask) }EXPORT_SYMBOL (blk_alloc_queue); -int blk_queue_enter (struct request_queue *q,bool nowait) +/** +* blk_queue_enter() - try to increase q->q_usage_counter +* @q: request queue pointer +* @flags: BLK_MQ_REQ_NOWAIT and/or BLK_MQ_REQ_PREEMPT +*/ +int blk_queue_enter (struct request_queue *q,unsigned int flags) { +const bool preempt = flags &BLK_MQ_REQ_PREEMPT ; +while (true) {int ret;
block/blk-core.c struct request_queue *blk_alloc_queue(gfp_t gfp_mask) · line 814
Quoted 10 lines
block/blk-core.c +7
-2-if (percpu_ref_tryget_live (&q->q_usage_counter)) -return 0 ; +if (percpu_ref_tryget_live (&q->q_usage_counter)) { +/* +* The code that sets the PREEMPT_ONLY flag is +* responsible for ensuring that that flag is globally +* visible before the queue is unfrozen. +*/ +if (preempt || !blk_queue_preempt_only (q)) {
PREEMPT_ONLY flag is checked without RCU read lock held, so the synchronize_rcu() may just wait for completion of pre-exit percpu_ref_tryget_live(), which can be reordered with the reading on blk_queue_preempt_only().
Quoted 86 lines
block/blk-core.c +14
-5+return 0 ; + }else { +percpu_ref_put (&q->q_usage_counter); +WARN_ONCE ("%s: Attempt to allocate non-preempt request in preempt-only mode.\n" , +kobject_name (q->kobj.parent)); + } + } -if (nowait) +if (flags &BLK_MQ_REQ_NOWAIT )return -EBUSY ;/* @@ -813,7 +833,8 @@ int blk_queue_enter(struct request_queue *q, bool nowait)smp_rmb (); ret =wait_event_interruptible (q->mq_freeze_wq, - !atomic_read (&q->mq_freeze_depth) || + (atomic_read (&q->mq_freeze_depth) ==0 && + (preempt || !blk_queue_preempt_only (q))) ||blk_queue_dying (q));if (blk_queue_dying (q))return -ENODEV ;@@ -1441,8 +1462,7 @@ static struct request *blk_old_get_request(struct request_queue *q,/* create ioc upfront */ create_io_context (gfp_mask, q->node); - ret =blk_queue_enter (q, !(gfp_mask & __GFP_DIRECT_RECLAIM) || - (op &REQ_NOWAIT )); + ret =blk_queue_enter (q, flags);if (ret)return ERR_PTR (ret);spin_lock_irq (q->queue_lock);@@ -2263,8 +2283,10 @@ blk_qc_t generic_make_request(struct bio *bio) current->bio_list = bio_list_on_stack; do { struct request_queue *q = bio->bi_disk->queue; + unsigned int flags = bio->bi_opf & REQ_NOWAIT ? + BLK_MQ_REQ_NOWAIT : 0; - if (likely(blk_queue_enter(q, bio->bi_opf & REQ_NOWAIT) == 0)) { + if (likely(blk_queue_enter(q, flags) == 0)) { struct bio_list lower, same; /* Create a fresh bio_list for all subordinate requests */block/blk-mq.c +2
-2@@ -386,7 +386,7 @@ struct request *blk_mq_alloc_request(struct request_queue *q, unsigned int op,struct request *rq;int ret; - ret =blk_queue_enter (q, flags &BLK_MQ_REQ_NOWAIT ); + ret =blk_queue_enter (q, flags);if (ret)return ERR_PTR (ret);@@ -425,7 +425,7 @@ struct request *blk_mq_alloc_request_hctx(struct request_queue *q,if (hctx_idx >= q->nr_hw_queues)return ERR_PTR (-EIO ); - ret =blk_queue_enter (q, true); + ret =blk_queue_enter (q, flags);if (ret)return ERR_PTR (ret);block/blk-timeout.c +1
-1@@ -134,7 +134,7 @@ void blk_timeout_work(struct work_struct *work)struct request *rq, *tmp;int next_set =0 ; -if (blk_queue_enter (q, true)) +if (blk_queue_enter (q,BLK_MQ_REQ_NOWAIT |BLK_MQ_REQ_PREEMPT ))return ;spin_lock_irqsave (q->queue_lock, flags);drivers/scsi/scsi_lib.c +0
-0@@ -2926,21 +2926,30 @@ static void scsi_wait_for_queuecommand(struct scsi_device *sdev)int
drivers/scsi/scsi_lib.c static void scsi_wait_for_queuecommand(struct scsi_device *sdev) · line 2936
Quoted 10 lines
drivers/scsi/scsi_lib.c +6
-0scsi_device_quiesce(struct scsi_device *sdev) { +struct request_queue *q = sdev->request_queue;int err; +blk_mq_freeze_queue (q); +if (blk_set_preempt_only (q)) { +blk_mq_unfreeze_queue (q); +return -EINVAL ; + }
This way is wrong, if blk_set_preempt_only() returns true it means the queue has been in PREEMPT_ONLY already, and failing scsi_device_quiesce() can break suspend/resume or sending SCSI domain validation command.
The reasonable handling should be just going ahead if queue is in PREEMPT_ONLY already.
drivers/scsi/scsi_lib.c static void scsi_wait_for_queuecommand(struct scsi_device *sdev) · line 2942
Quoted 6 lines
drivers/scsi/scsi_lib.c +6
-0+/* +* Ensure that the effect of blk_set_preempt_only() will be visible +* for percpu_ref_tryget() callers that occur after the queue +* unfreeze. See also https://lwn.net/Articles/573497/. +*/ +synchronize_rcu ();
This synchronize_rcu may be saved if we set the PREEMPT_ONLY flag before freezing queue since blk_mq_freeze_queue() may implicate one synchronize_rcu().
Quoted 21 lines
drivers/scsi/scsi_lib.c +4
-7+blk_mq_unfreeze_queue (q); +mutex_lock (&sdev->state_mutex); err =scsi_device_set_state (sdev,SDEV_QUIESCE );mutex_unlock (&sdev->state_mutex);if (err) -return err; +blk_clear_preempt_only (q); -scsi_run_queue (sdev->request_queue); -while (atomic_read (&sdev->device_busy)) { -msleep_interruptible (200 ); -scsi_run_queue (sdev->request_queue); - } -return 0 ; +return err; }EXPORT_SYMBOL (scsi_device_quiesce);
drivers/scsi/scsi_lib.c void scsi_device_resume(struct scsi_device *sdev) · line 2977
Quoted 9 lines
drivers/scsi/scsi_lib.c +3
-1@@ -2961,8 +2970,10 @@ void scsi_device_resume(struct scsi_device *sdev)*/ mutex_lock (&sdev->state_mutex);if (sdev->sdev_state ==SDEV_QUIESCE && -scsi_device_set_state (sdev,SDEV_RUNNING ) ==0 ) +scsi_device_set_state (sdev,SDEV_RUNNING ) ==0 ) { +blk_clear_preempt_only (sdev->request_queue);scsi_run_queue (sdev->request_queue); + }mutex_unlock (&sdev->state_mutex);
scsi_run_queue() can be removed, and blk_clear_preempt_only() needn't to be run with holding sdev->state_mutex, just like in quiesce path.
Signature · 1 line
Ming
Re: [PATCH v6 9/9] block, scsi: Make SCSI device suspend and resume work reliably
T24gU2F0LCAyMDE3LTEwLTA3IGF0IDEyOjMzICswODAwLCBNaW5nIExlaSB3cm90ZToNCj4gT24g V2VkLCBPY3QgMDQsIDIwMTcgYXQgMDU6MDE6MTBQTSAtMDcwMCwgQmFydCBWYW4gQXNzY2hlIHdy b3RlOg0KPiA+IEl0IGlzIGVzc2VudGlhbCBkdXJpbmcgc3VzcGVuZCBhbmQgcmVzdW1lIHRoYXQg bmVpdGhlciB0aGUgZmlsZXN5c3RlbQ0KPiA+IHN0YXRlIG5vciB0aGUgZmlsZXN5c3RlbSBtZXRh ZGF0YSBpbiBSQU0gY2hhbmdlcy4gVGhpcyBpcyB3aHkgd2hpbGUNCj4gPiB0aGUgaGliZXJuYXRp b24gaW1hZ2UgaXMgYmVpbmcgd3JpdHRlbiBvciByZXN0b3JlZCB0aGF0IFNDU0kgZGV2aWNlcw0K PiANCj4gcXVpZXNjZSBpc24ndCB1c2VkIG9ubHkgZm9yIHN1c3BlbmQgYW5kIHJlc3VtZSwgQW5k IHRoZSBpc3N1ZSBpc24ndA0KPiBzdXNwZW5kL3Jlc3VtZSBzcGVjaWZpYyB0b28uIFNvIHBsZWFz ZSBjaGFuZ2UgdGhlIHRpdGxlL2NvbW1pdCBsb2cNCj4gYXMgc29ydCBvZiAnbWFrZSBTQ1NJIHF1 aWVzY2UgbW9yZSByZWxpYWJsZS9zYWZlJy4gDQoNCk9LLCBJIHdpbGw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Vycy4N Cg0KQSBjb3JyZWN0aW9uOiB0aGUgY3VycmVudCBiZWhhdmlvciB3aGVuIHF1aWVzY2luZyBhbiBh bHJlYWR5IHF1aWVzY2VkIGRldmljZSBpcw0KdGhhdCBzY3NpX2RldmljZV9xdWllc2NlKCkgcmV0 dXJucyAwIHdpdGhvdXQgY2hhbmdpbmcgYW55IHN0YXRlLiBBbnl3YXksIEkgcHJvcG9zZQ0KdG8g a2VlcCB0aGF0IGJlaGF2aW9yIGFuZCBub3QgdG8gYWRkIG1vcmUgY29tcGxleGl0eSBub3cuDQoN CkJhcnQu
Re: [PATCH v6 9/9] block, scsi: Make SCSI device suspend and resume work reliably
In reply to Bart Van AsscheQuoted 46 lines · Bart Van Assche
Quoted 8 lines · Ming Lei
Quoted 3 lines · Bart Van Assche
It is essential during suspend and resume that neither the filesystem state nor the filesystem metadata in RAM changes. This is why while the hibernation image is being written or restored that SCSI devices
quiesce isn't used only for suspend and resume, And the issue isn't suspend/resume specific too. So please change the title/commit log as sort of 'make SCSI quiesce more reliable/safe'.
OK, I will modify the patch title.
Quoted 6 lines
Quoted 6 lines
block/blk-core.c +4
-2-if (percpu_ref_tryget_live (&q->q_usage_counter)) -return 0 ; +if (percpu_ref_tryget_live (&q->q_usage_counter)) { +/* +* The code that sets the PREEMPT_ONLY flag is +* responsible for ensuring that that flag is globally Quoted 8 lines
block/blk-core.c struct request_queue *blk_alloc_queue(gfp_t gfp_mask) · line 814
Quoted 3 lines
block/blk-core.c +3
-0+* visible before the queue is unfrozen. +*/ +if (preempt || !blk_queue_preempt_only (q)) {
PREEMPT_ONLYflag is checked without RCU read lock held, so thesynchronize_rcu()may just wait for completion of pre-exitpercpu_ref_tryget_live(), which can be reordered with the reading onblk_queue_preempt_only().OK, I will add rcu_read_lock_sched/unlock_sched() calls around this code.
Quoted 6 lines
Quoted 6 lines
int
scsi_device_quiesce(struct scsi_device *sdev)
{drivers/scsi/scsi_lib.c +1
-0+struct request_queue *q = sdev->request_queue;int err;Quoted 10 lines
drivers/scsi/scsi_lib.c static void scsi_wait_for_queuecommand(struct scsi_device *sdev) · line 2936
Quoted 5 lines
drivers/scsi/scsi_lib.c +5
-0+blk_mq_freeze_queue (q); +if (blk_set_preempt_only (q)) { +blk_mq_unfreeze_queue (q); +return -EINVAL ; + }This way is wrong, if
blk_set_preempt_only()returns true it means the queue has been inPREEMPT_ONLYalready, and failingscsi_device_quiesce()can break suspend/resume or sending SCSI domain validation command.
Quoted 4 lines
That's an interesting comment but I propose that what you suggest is implemented through a separate patch. The above code preserves the existing behavior, namely that
scsi_device_quiesce()returns an error code if called when a SCSI device has already been quiesced. See alsoscsi_device_set_state(). BTW, I don't think that
Please see scsi_device_set_state():
if (state == oldstate)
return 0;And believe it or not, there is one real nested calling of scsi_device_quiesce() in transport_spi.
Quoted 5 lines
it is likely that this will occur. The only code other than the power management code I know of that sets the SCSI QUIESCE state is the SCSI sysfs state store method and the SCSI parallel code. I don't know any software that sets the SCSI QUIESCE state through sysfs. And I'm not sure the SCSI parallel code has a significant number of users.
As I know, one famous VM uses that, and the original report is that this VM may hang after running I/O for days by our customer. The revalidate path can trigger QUIESCE, and udev should cause that.
Quoted 28 lines
Quoted 11 lines
drivers/scsi/scsi_lib.c static void scsi_wait_for_queuecommand(struct scsi_device *sdev) · line 2942
Quoted 7 lines
drivers/scsi/scsi_lib.c +6
-0+/* +* Ensure that the effect of blk_set_preempt_only() will be visible +* for percpu_ref_tryget() callers that occur after the queue +* unfreeze. See also https://lwn.net/Articles/573497/. +*/ +synchronize_rcu ();This
synchronize_rcumay be saved if we set thePREEMPT_ONLYflag before freezing queue sinceblk_mq_freeze_queue()may implicate onesynchronize_rcu().That's an interesting comment. I will look further into this.
Quoted 5 lines
Quoted 5 lines
drivers/scsi/scsi_lib.c +0
-1@@ -2961,8 +2970,10 @@ void scsi_device_resume(struct scsi_device *sdev)*/ mutex_lock (&sdev->state_mutex); if (sdev->sdev_state == SDEV_QUIESCE && -scsi_device_set_state (sdev,SDEV_RUNNING ) ==0 )Quoted 8 lines
drivers/scsi/scsi_lib.c void scsi_device_resume(struct scsi_device *sdev) · line 2977
Quoted 5 lines
drivers/scsi/scsi_lib.c +3
-0+ scsi_device_set_state(sdev, SDEV_RUNNING) == 0) { +blk_clear_preempt_only (sdev->request_queue);scsi_run_queue (sdev->request_queue); + }mutex_unlock (&sdev->state_mutex);
scsi_run_queue()can be removed, andblk_clear_preempt_only()needn't to be run with holdingsdev->state_mutex, just like in quiesce path.
Quoted 4 lines
I will look into removing the
scsi_run_queue()call. But I prefer to keep theblk_clear_preempt_only()inside the critical section because that call doesn't sleep and because it changes state information that is related to the SCSI state.
The thing is that the same state change in blk_set_preempt_only() can't be protected by the lock, and this way may confuse people wrt. inconsistent lock usage on same state protection.
Signature · 1 line
Ming