block/scsi: safe SCSI quiescing
Subsystems (6)
v2 cover block/scsi: safe SCSI quiescing
Hi,
The current SCSI quiesce isn't safe and easy to trigger I/O deadlock.
Once SCSI device is put into QUIESCE, no new request except for RQF_PREEMPT can be dispatched to SCSI successfully, and scsi_device_quiesce() just simply waits for completion of I/Os dispatched to SCSI stack. It isn't enough at all.
Because new request still can be allocated, but all the allocated requests can't be dispatched successfully, so request pool can be consumed up easily.
Then request with RQF_PREEMPT can't be allocated, and system may hang forever, such as during system suspend or SCSI domain alidation.
Both IO hang inside system suspend[1] or SCSI domain validation were reported before.
This patch tries to solve the issue by freezing block queue during SCSI quiescing, and allowing to allocate request of RQF_PREEMPT when queue is frozen.
Both SCSI and SCSI_MQ have this IO deadlock issue, this patch fixes them all by introducing preempt version of blk_freeze_queue() and blk_unfreeze_queue().
V2:
- drop the 1st patch in V1 because percpu_ref_is_dying() is enough as pointed by Tejun
- introduce preempt version of blk_[freeze|unfreeze]_queue
- sync between preempt freeze and normal freeze
- fix warning from percpu-refcount as reported by Oleksandr
[1] https://marc.info/?t=150340250100013&r=3&w=2
Ming Lei (8):
blk-mq: rename blk_mq_unfreeze_queue as blk_unfreeze_queue
blk-mq: rename blk_mq_freeze_queue as blk_freeze_queue
blk-mq: only run hw queues for blk-mq
blk-mq: rename blk_mq_freeze_queue_wait as blk_freeze_queue_wait
block: tracking request allocation with q_usage_counter
block: allow to allocate req with REQF_PREEMPT when queue is frozen
block: introduce preempt version of blk_[freeze|unfreeze]_queue
SCSI: freeze block queue when SCSI device is put into quiesce
block/bfq-iosched.c | 2 +- block/blk-cgroup.c | 8 ++-- block/blk-core.c | 50 ++++++++++++++++---- block/blk-mq.c | 119 ++++++++++++++++++++++++++++++++++++----------- block/blk-mq.h | 1 - block/blk.h | 6 +++ block/elevator.c | 4 +- drivers/block/loop.c | 16 +++---- drivers/block/rbd.c | 2 +- drivers/nvme/host/core.c | 8 ++-- drivers/scsi/scsi_lib.c | 21 ++++++++- include/linux/blk-mq.h | 15 +++--- include/linux/blkdev.h | 20 +++++++- 13 files changed, 206 insertions(+), 66 deletions(-)
Signature · 1 line
2.9.5
v2 cover block/scsi: safe SCSI quiescing
Hi,
The current SCSI quiesce isn't safe and easy to trigger I/O deadlock.
Once SCSI device is put into QUIESCE, no new request except for RQF_PREEMPT can be dispatched to SCSI successfully, and scsi_device_quiesce() just simply waits for completion of I/Os dispatched to SCSI stack. It isn't enough at all.
Because new request still can be allocated, but all the allocated requests can't be dispatched successfully, so request pool can be consumed up easily.
Then request with RQF_PREEMPT can't be allocated, and system may hang forever, such as during system suspend or SCSI domain alidation.
Both IO hang inside system suspend[1] or SCSI domain validation were reported before.
This patch tries to solve the issue by freezing block queue during SCSI quiescing, and allowing to allocate request of RQF_PREEMPT when queue is frozen.
Both SCSI and SCSI_MQ have this IO deadlock issue, this patch fixes them all by introducing preempt version of blk_freeze_queue() and blk_unfreeze_queue().
V2:
- drop the 1st patch in V1 because percpu_ref_is_dying() is enough as pointed by Tejun
- introduce preempt version of blk_[freeze|unfreeze]_queue
- sync between preempt freeze and normal freeze
- fix warning from percpu-refcount as reported by Oleksandr
[1] https://marc.info/?t=150340250100013&r=3&w=2
Ming Lei (8):
blk-mq: rename blk_mq_unfreeze_queue as blk_unfreeze_queue
blk-mq: rename blk_mq_freeze_queue as blk_freeze_queue
blk-mq: only run hw queues for blk-mq
blk-mq: rename blk_mq_freeze_queue_wait as blk_freeze_queue_wait
block: tracking request allocation with q_usage_counter
block: allow to allocate req with REQF_PREEMPT when queue is frozen
block: introduce preempt version of blk_[freeze|unfreeze]_queue
SCSI: freeze block queue when SCSI device is put into quiesce
block/bfq-iosched.c | 2 +- block/blk-cgroup.c | 8 ++-- block/blk-core.c | 50 ++++++++++++++++---- block/blk-mq.c | 119 ++++++++++++++++++++++++++++++++++++----------- block/blk-mq.h | 1 - block/blk.h | 6 +++ block/elevator.c | 4 +- drivers/block/loop.c | 16 +++---- drivers/block/rbd.c | 2 +- drivers/nvme/host/core.c | 8 ++-- drivers/scsi/scsi_lib.c | 21 ++++++++- include/linux/blk-mq.h | 15 +++--- include/linux/blkdev.h | 20 +++++++- 13 files changed, 206 insertions(+), 66 deletions(-)
Signature · 1 line
2.9.5
PATCH v2 1/8 blk-mq: rename blk_mq_unfreeze_queue as blk_unfreeze_queue
In reply to Ming LeiWe will support to freeze queue on block legacy path too.
--- block/blk-cgroup.c | 4 ++-- block/blk-mq.c | 10 +++++----- block/elevator.c | 2 +- drivers/block/loop.c | 8 ++++---- drivers/nvme/host/core.c | 4 ++-- include/linux/blk-mq.h | 2 +- 6 files changed, 15 insertions(+), 15 deletions(-)
block/blk-cgroup.c +2 -2
@@ -1337,7 +1337,7 @@ int blkcg_activate_policy(struct request_queue *q,spin_unlock_irq (q->queue_lock); out_bypass_end:if (q->mq_ops) -blk_mq_unfreeze_queue (q); +blk_unfreeze_queue (q);else blk_queue_bypass_end (q);if (pd_prealloc)
@@ -1388,7 +1388,7 @@ void blkcg_deactivate_policy(struct request_queue *q,spin_unlock_irq (q->queue_lock);if (q->mq_ops) -blk_mq_unfreeze_queue (q); +blk_unfreeze_queue (q);else blk_queue_bypass_end (q); }
block/blk-mq.c +5 -5
@@ -172,7 +172,7 @@ void blk_mq_freeze_queue(struct request_queue *q) }EXPORT_SYMBOL_GPL (blk_mq_freeze_queue); -void blk_mq_unfreeze_queue (struct request_queue *q) +void blk_unfreeze_queue (struct request_queue *q) {int freeze_depth;
@@ -183,7 +183,7 @@ void blk_mq_unfreeze_queue(struct request_queue *q)wake_up_all (&q->mq_freeze_wq); } } -EXPORT_SYMBOL_GPL (blk_mq_unfreeze_queue); +EXPORT_SYMBOL_GPL (blk_unfreeze_queue);/* * FIXME: replace the scsi_internal_device_*block_nowait() calls in the
@@ -2250,7 +2250,7 @@ static void blk_mq_update_tag_set_depth(struct blk_mq_tag_set *set,list_for_each_entry (q, &set->tag_list, tag_set_list) {blk_mq_freeze_queue (q);queue_set_hctx_shared (q, shared); -blk_mq_unfreeze_queue (q); +blk_unfreeze_queue (q); } }
@@ -2708,7 +2708,7 @@ static int __blk_mq_update_nr_requests(struct request_queue *q,if (!ret) q->nr_requests = nr; -blk_mq_unfreeze_queue (q); +blk_unfreeze_queue (q);return ret; }
@@ -2757,7 +2757,7 @@ static void __blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set, }list_for_each_entry (q, &set->tag_list, tag_set_list) -blk_mq_unfreeze_queue (q); +blk_unfreeze_queue (q); }void blk_mq_update_nr_hw_queues (struct blk_mq_tag_set *set,int nr_hw_queues)
block/elevator.c +1 -1
@@ -994,7 +994,7 @@ static int elevator_switch_mq(struct request_queue *q,blk_add_trace_msg (q,"elv switch: none" ); out: -blk_mq_unfreeze_queue (q); +blk_unfreeze_queue (q);return ret; }
drivers/block/loop.c +4 -4
@@ -217,7 +217,7 @@ static void __loop_update_dio(struct loop_device *lo, bool dio) lo->lo_flags |=LO_FLAGS_DIRECT_IO ; else lo->lo_flags &= ~LO_FLAGS_DIRECT_IO; -blk_mq_unfreeze_queue (lo->lo_queue); +blk_unfreeze_queue (lo->lo_queue); } static int
@@ -605,7 +605,7 @@ static int loop_switch(struct loop_device *lo, struct file *file)do_loop_switch (lo, &w);/* unfreeze */ -blk_mq_unfreeze_queue (lo->lo_queue); +blk_unfreeze_queue (lo->lo_queue);return 0 ; }
@@ -1079,7 +1079,7 @@ static int loop_clr_fd(struct loop_device *lo) lo->lo_state = Lo_unbound;/* This is safe: open() is still holding a reference. */ module_put (THIS_MODULE ); -blk_mq_unfreeze_queue (lo->lo_queue); +blk_unfreeze_queue (lo->lo_queue);if (lo->lo_flags &LO_FLAGS_PARTSCAN && bdev)loop_reread_partitions (lo, bdev);
@@ -1191,7 +1191,7 @@ loop_set_status(struct loop_device *lo, const struct loop_info64 *info)__loop_update_dio (lo, lo->use_dio); exit: -blk_mq_unfreeze_queue (lo->lo_queue); +blk_unfreeze_queue (lo->lo_queue);if (!err && (info->lo_flags &LO_FLAGS_PARTSCAN ) && !(lo->lo_flags &LO_FLAGS_PARTSCAN )) {
drivers/nvme/host/core.c +2 -2
@@ -1226,7 +1226,7 @@ static void __nvme_revalidate_disk(struct gendisk *disk, struct nvme_id_ns *id)if (ctrl->oncs &NVME_CTRL_ONCS_DSM )nvme_config_discard (ns); -blk_mq_unfreeze_queue (disk->queue); +blk_unfreeze_queue (disk->queue); }static int nvme_revalidate_disk (struct gendisk *disk)
@@ -2753,7 +2753,7 @@ void nvme_unfreeze(struct nvme_ctrl *ctrl)mutex_lock (&ctrl->namespaces_mutex);list_for_each_entry (ns, &ctrl->namespaces, list) -blk_mq_unfreeze_queue (ns->queue); +blk_unfreeze_queue (ns->queue);mutex_unlock (&ctrl->namespaces_mutex); }EXPORT_SYMBOL_GPL (nvme_unfreeze);
include/linux/blk-mq.h +1 -1
@@ -257,7 +257,7 @@ void blk_mq_delay_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs);void blk_mq_tagset_busy_iter (struct blk_mq_tag_set *tagset,busy_tag_iter_fn *fn,void *priv);void blk_mq_freeze_queue (struct request_queue *q); -void blk_mq_unfreeze_queue (struct request_queue *q); +void blk_unfreeze_queue (struct request_queue *q);void blk_freeze_queue_start (struct request_queue *q);void blk_mq_freeze_queue_wait (struct request_queue *q); int blk_mq_freeze_queue_wait_timeout(struct request_queue *q,
Signature · 1 line
2.9.5
PATCH v2 2/8 blk-mq: rename blk_mq_freeze_queue as blk_freeze_queue
In reply to Ming LeiThis APIs will be used by legacy path too.
--- block/bfq-iosched.c | 2 +- block/blk-cgroup.c | 4 ++-- block/blk-mq.c | 17 ++++------------- block/blk-mq.h | 1 - block/elevator.c | 2 +- drivers/block/loop.c | 8 ++++---- drivers/block/rbd.c | 2 +- drivers/nvme/host/core.c | 2 +- include/linux/blk-mq.h | 2 +- 9 files changed, 15 insertions(+), 25 deletions(-)
block/bfq-iosched.c +1 -1
@@ -4757,7 +4757,7 @@ static int bfq_init_queue(struct request_queue *q, struct elevator_type *e)* The invocation of the next bfq_create_group_hierarchy * function is the head of a chain of function calls * (bfq_create_group_hierarchy->blkcg_activate_policy-> -* blk_mq_freeze_queue) that may lead to the invocation of the +* blk_freeze_queue) that may lead to the invocation of the * has_work hook function. For this reason, * bfq_create_group_hierarchy is invoked only after all * scheduler data has been initialized, apart from the fields
block/blk-cgroup.c +2 -2
@@ -1296,7 +1296,7 @@ int blkcg_activate_policy(struct request_queue *q,return 0 ;if (q->mq_ops) -blk_mq_freeze_queue (q); +blk_freeze_queue (q);else blk_queue_bypass_start (q); pd_prealloc:
@@ -1363,7 +1363,7 @@ void blkcg_deactivate_policy(struct request_queue *q,return ;if (q->mq_ops) -blk_mq_freeze_queue (q); +blk_freeze_queue (q);else blk_queue_bypass_start (q);
block/blk-mq.c +4 -13
@@ -161,16 +161,7 @@ void blk_freeze_queue(struct request_queue *q)blk_freeze_queue_start (q);blk_mq_freeze_queue_wait (q); } - -void blk_mq_freeze_queue (struct request_queue *q) -{ -/* -* ...just an alias to keep freeze and unfreeze actions balanced -* in the blk_mq_* namespace -*/ -blk_freeze_queue (q); -} -EXPORT_SYMBOL_GPL (blk_mq_freeze_queue); +EXPORT_SYMBOL_GPL (blk_freeze_queue);void blk_unfreeze_queue (struct request_queue *q) {
@@ -2248,7 +2239,7 @@ static void blk_mq_update_tag_set_depth(struct blk_mq_tag_set *set,lockdep_assert_held (&set->tag_list_lock);list_for_each_entry (q, &set->tag_list, tag_set_list) { -blk_mq_freeze_queue (q); +blk_freeze_queue (q);queue_set_hctx_shared (q, shared);blk_unfreeze_queue (q); }
@@ -2683,7 +2674,7 @@ static int __blk_mq_update_nr_requests(struct request_queue *q,if (!set)return -EINVAL ; -blk_mq_freeze_queue (q); +blk_freeze_queue (q); ret =0 ;queue_for_each_hw_ctx (q, hctx, i) {
@@ -2747,7 +2738,7 @@ static void __blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set,return ;list_for_each_entry (q, &set->tag_list, tag_set_list) -blk_mq_freeze_queue (q); +blk_freeze_queue (q); set->nr_hw_queues = nr_hw_queues;blk_mq_update_queue_map (set);
block/blk-mq.h +0 -1
@@ -30,7 +30,6 @@ struct blk_mq_ctx { }____cacheline_aligned_in_smp ;void blk_mq_run_hw_queue (struct blk_mq_hw_ctx *hctx,bool async); -void blk_mq_freeze_queue (struct request_queue *q);void blk_mq_free_queue (struct request_queue *q);int blk_mq_update_nr_requests (struct request_queue *q,unsigned int nr);void blk_mq_wake_waiters (struct request_queue *q);
block/elevator.c +1 -1
@@ -967,7 +967,7 @@ static int elevator_switch_mq(struct request_queue *q, {int ret; -blk_mq_freeze_queue (q); +blk_freeze_queue (q);if (q->elevator) {if (q->elevator->registered)
drivers/block/loop.c +4 -4
@@ -211,7 +211,7 @@ static void __loop_update_dio(struct loop_device *lo, bool dio)* LO_FLAGS_READ_ONLY, both are set from kernel, and losetup * will get updated by ioctl(LOOP_GET_STATUS) */ -blk_mq_freeze_queue (lo->lo_queue); +blk_freeze_queue (lo->lo_queue); lo->use_dio = use_dio;if (use_dio) lo->lo_flags |=LO_FLAGS_DIRECT_IO ;
@@ -599,7 +599,7 @@ static int loop_switch(struct loop_device *lo, struct file *file) w.file = file;/* freeze queue and wait for completion of scheduled requests */ -blk_mq_freeze_queue (lo->lo_queue); +blk_freeze_queue (lo->lo_queue);/* do the switch action */ do_loop_switch (lo, &w);
@@ -1046,7 +1046,7 @@ static int loop_clr_fd(struct loop_device *lo)return -EINVAL ;/* freeze request queue during the transition */ -blk_mq_freeze_queue (lo->lo_queue); +blk_freeze_queue (lo->lo_queue);spin_lock_irq (&lo->lo_lock); lo->lo_state = Lo_rundown;
@@ -1116,7 +1116,7 @@ loop_set_status(struct loop_device *lo, const struct loop_info64 *info)return -EINVAL ;/* I/O need to be drained during transfer transition */ -blk_mq_freeze_queue (lo->lo_queue); +blk_freeze_queue (lo->lo_queue); err =loop_release_xfer (lo);if (err)
drivers/block/rbd.c +1 -1
@@ -6347,7 +6347,7 @@ static ssize_t do_rbd_remove(struct bus_type *bus,* Prevent new IO from being queued and wait for existing * IO to complete/fail. */ -blk_mq_freeze_queue (rbd_dev->disk->queue); +blk_freeze_queue (rbd_dev->disk->queue);blk_set_queue_dying (rbd_dev->disk->queue); }
drivers/nvme/host/core.c +1 -1
@@ -1210,7 +1210,7 @@ static void __nvme_revalidate_disk(struct gendisk *disk, struct nvme_id_ns *id) bs =1 << ns->lba_shift; ns->noiob =le16_to_cpu (id->noiob); -blk_mq_freeze_queue (disk->queue); +blk_freeze_queue (disk->queue);if (ctrl->ops->flags &NVME_F_METADATA_SUPPORTED )nvme_prep_integrity (disk, id, bs);
include/linux/blk-mq.h +1 -1
@@ -256,7 +256,7 @@ void blk_mq_run_hw_queues(struct request_queue *q, bool async);void blk_mq_delay_queue (struct blk_mq_hw_ctx *hctx,unsigned long msecs);void blk_mq_tagset_busy_iter (struct blk_mq_tag_set *tagset,busy_tag_iter_fn *fn,void *priv); -void blk_mq_freeze_queue (struct request_queue *q); +void blk_freeze_queue (struct request_queue *q);void blk_unfreeze_queue (struct request_queue *q);void blk_freeze_queue_start (struct request_queue *q);void blk_mq_freeze_queue_wait (struct request_queue *q);
Signature · 1 line
2.9.5
PATCH v2 3/8 blk-mq: only run hw queues for blk-mq
In reply to Ming LeiThis patch just makes it explicitely.
--- block/blk-mq.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
block/blk-mq.c +2 -1
@@ -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);
Signature · 1 line
2.9.5
PATCH v2 4/8 blk-mq: rename blk_mq_freeze_queue_wait as blk_freeze_queue_wait
In reply to Ming LeiThe only change on legacy is that blk_drain_queue() is run from blk_freeze_queue(), which is called in blk_cleanup_queue().
So this patch removes the explicite __blk_drain_queue() in blk_cleanup_queue().
--- block/blk-core.c | 17 +++++++++++++++-- block/blk-mq.c | 8 +++++--- block/blk.h | 1 + drivers/nvme/host/core.c | 2 +- include/linux/blk-mq.h | 2 +- 5 files changed, 23 insertions(+), 7 deletions(-)
block/blk-core.c +15 -2
@@ -530,6 +530,21 @@ static void __blk_drain_queue(struct request_queue *q, bool drain_all) }/** +* blk_drain_queue - drain requests from request_queue +* @q: queue to drain +* +* Drain requests from @q. All pending requests are drained. +* The caller is responsible for ensuring that no new requests +* which need to be drained are queued. +*/ +void blk_drain_queue (struct request_queue *q) +{ +spin_lock_irq (q->queue_lock); +__blk_drain_queue (q, true); +spin_unlock_irq (q->queue_lock); +} + +/** * blk_queue_bypass_start - enter queue bypass mode * @q: queue of interest *
@@ -653,8 +668,6 @@ void blk_cleanup_queue(struct request_queue *q)*/ blk_freeze_queue (q);spin_lock_irq (lock); -if (!q->mq_ops) -__blk_drain_queue (q, true);queue_flag_set (QUEUE_FLAG_DEAD , q);spin_unlock_irq (lock);
block/blk-mq.c +5 -3
@@ -131,11 +131,13 @@ void blk_freeze_queue_start(struct request_queue *q) }EXPORT_SYMBOL_GPL (blk_freeze_queue_start); -void blk_mq_freeze_queue_wait (struct request_queue *q) +void blk_freeze_queue_wait (struct request_queue *q) { +if (!q->mq_ops) +blk_drain_queue (q);wait_event (q->mq_freeze_wq,percpu_ref_is_zero (&q->q_usage_counter)); } -EXPORT_SYMBOL_GPL (blk_mq_freeze_queue_wait); +EXPORT_SYMBOL_GPL (blk_freeze_queue_wait);int blk_mq_freeze_queue_wait_timeout (struct request_queue *q,unsigned long timeout)
@@ -160,7 +162,7 @@ void blk_freeze_queue(struct request_queue *q)* exported to drivers as the only user for unfreeze is blk_mq. */ blk_freeze_queue_start (q); -blk_mq_freeze_queue_wait (q); +blk_freeze_queue_wait (q); }EXPORT_SYMBOL_GPL (blk_freeze_queue);
block/blk.h +1 -0
@@ -64,6 +64,7 @@ void blk_rq_bio_prep(struct request_queue *q, struct request *rq, struct bio *bio);void blk_queue_bypass_start (struct request_queue *q);void blk_queue_bypass_end (struct request_queue *q); +void blk_drain_queue (struct request_queue *q);void blk_dequeue_request (struct request *rq);void __blk_queue_free_tags (struct request_queue *q);void blk_freeze_queue (struct request_queue *q);
drivers/nvme/host/core.c +1 -1
@@ -2778,7 +2778,7 @@ void nvme_wait_freeze(struct nvme_ctrl *ctrl)mutex_lock (&ctrl->namespaces_mutex);list_for_each_entry (ns, &ctrl->namespaces, list) -blk_mq_freeze_queue_wait (ns->queue); +blk_freeze_queue_wait (ns->queue);mutex_unlock (&ctrl->namespaces_mutex); }EXPORT_SYMBOL_GPL (nvme_wait_freeze);
include/linux/blk-mq.h +1 -1
@@ -259,7 +259,7 @@ void blk_mq_tagset_busy_iter(struct blk_mq_tag_set *tagset,void blk_freeze_queue (struct request_queue *q);void blk_unfreeze_queue (struct request_queue *q);void blk_freeze_queue_start (struct request_queue *q); -void blk_mq_freeze_queue_wait (struct request_queue *q); +void blk_freeze_queue_wait (struct request_queue *q);int blk_mq_freeze_queue_wait_timeout (struct request_queue *q,unsigned long timeout); int blk_mq_reinit_tagset(struct blk_mq_tag_set *set,
Signature · 1 line
2.9.5
PATCH v2 5/8 block: tracking request allocation with q_usage_counter
In reply to Ming LeiThis usage is basically same with blk-mq, so that we can support to freeze queue easily.
--- block/blk-core.c | 8 ++++++++ 1 file changed, 8 insertions(+)
block/blk-core.c +8 -0
@@ -1405,16 +1405,21 @@ 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)); +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; }
@@ -1586,6 +1591,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);
@@ -1867,8 +1873,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 ;
Signature · 1 line
2.9.5
PATCH v2 6/8 block: allow to allocate req with REQF_PREEMPT when queue is frozen
In reply to Ming LeiREQF_PREEMPT is a bit special because it is required to be dispatched to lld even when SCSI device is quiesced.
So this patch introduces __blk_get_request() to allow block layer to allocate request when queue is frozen, since we will freeze queue before quiescing SCSI device in the following patch for supporting safe SCSI quiescing.
--- block/blk-core.c | 25 +++++++++++++++++-------- block/blk-mq.c | 11 +++++++++-- block/blk.h | 5 +++++ include/linux/blk-mq.h | 7 ++++--- include/linux/blkdev.h | 17 +++++++++++++++-- 5 files changed, 50 insertions(+), 15 deletions(-)
block/blk-core.c +17 -8
@@ -1402,7 +1402,8 @@ static struct request *get_request(struct request_queue *q, unsigned int op, }static struct request *blk_old_get_request (struct request_queue *q, -unsigned int op,gfp_t gfp_mask) +unsigned int op,gfp_t gfp_mask, +unsigned int flags) {struct request *rq;int ret =0 ;
@@ -1412,9 +1413,17 @@ 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)); +/* +* When queue is frozen, we still need to allocate req for +* REQF_PREEMPT. +*/ +if ((flags &BLK_MQ_REQ_PREEMPT ) &&blk_queue_is_freezing (q)) +blk_queue_enter_live (q); +else + ret =blk_queue_enter (q, !(gfp_mask & __GFP_DIRECT_RECLAIM));if (ret)return ERR_PTR (ret); +spin_lock_irq (q->queue_lock); rq =get_request (q, op,NULL , gfp_mask);if (IS_ERR (rq)) {
@@ -1430,26 +1439,26 @@ 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) +struct request *__blk_get_request (struct request_queue *q,unsigned int op, +gfp_t gfp_mask,unsigned int flags) {struct request *req;if (q->mq_ops) { req =blk_mq_alloc_request (q, op, - (gfp_mask & __GFP_DIRECT_RECLAIM) ? -0 :BLK_MQ_REQ_NOWAIT ); + flags | ((gfp_mask & __GFP_DIRECT_RECLAIM) ? +0 :BLK_MQ_REQ_NOWAIT ));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, gfp_mask, flags);if (!IS_ERR (req) && q->initialize_rq_fn) q->initialize_rq_fn (req); }return req; } -EXPORT_SYMBOL (blk_get_request); +EXPORT_SYMBOL (__blk_get_request);/** * blk_requeue_request - put a request back on queue
block/blk-mq.c +9 -2
@@ -382,9 +382,16 @@ struct request *blk_mq_alloc_request(struct request_queue *q, unsigned int op, {struct blk_mq_alloc_data alloc_data = { .flags = flags };struct request *rq; -int ret; +int ret =0 ; - ret =blk_queue_enter (q, flags &BLK_MQ_REQ_NOWAIT ); +/* +* When queue is frozen, we still need to allocate req for +* REQF_PREEMPT. +*/ +if ((flags &BLK_MQ_REQ_PREEMPT ) &&blk_queue_is_freezing (q)) +blk_queue_enter_live (q); +else + ret =blk_queue_enter (q, flags &BLK_MQ_REQ_NOWAIT );if (ret)return ERR_PTR (ret);
block/blk.h +5 -0
@@ -80,6 +80,11 @@ static inline void blk_queue_enter_live(struct request_queue *q)percpu_ref_get (&q->q_usage_counter); } +static inline bool blk_queue_is_freezing (struct request_queue *q) +{ +return percpu_ref_is_dying (&q->q_usage_counter); +} +#ifdef CONFIG_BLK_DEV_INTEGRITY void blk_flush_integrity (void );bool __bio_integrity_endio (struct bio *);
include/linux/blk-mq.h +4 -3
@@ -200,9 +200,10 @@ void blk_mq_free_request(struct request *rq);bool blk_mq_can_queue (struct blk_mq_hw_ctx *);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 =BLK_REQ_PREEMPT ,/* allocate for RQF_PREEMPT */ +BLK_MQ_REQ_NOWAIT = (1 <<8 ),/* return when out of requests */ +BLK_MQ_REQ_RESERVED = (1 <<9 ),/* allocate from reserved pool */ +BLK_MQ_REQ_INTERNAL = (1 <<10 ),/* allocate internal/sched tag */ }; struct request *blk_mq_alloc_request(struct request_queue *q, unsigned int op,
include/linux/blkdev.h +15 -2
@@ -858,6 +858,11 @@ enum { BLKPREP_INVALID, /* invalid command, kill, return -EREMOTEIO */ }; +/* passed to __blk_get_request */ +enum { +BLK_REQ_PREEMPT = (1 <<0 ),/* allocate for RQF_PREEMPT */ +}; +extern unsigned long blk_max_low_pfn, blk_max_pfn;/*
@@ -940,8 +945,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 (struct request_queue *,unsigned int op, -gfp_t gfp_mask); +extern struct request *__blk_get_request (struct request_queue *, +unsigned int op,gfp_t gfp_mask, +unsigned int flags);extern void blk_requeue_request (struct request_queue *,struct request *);extern int blk_lld_busy (struct request_queue *q); extern int blk_rq_prep_clone(struct request *rq, struct request *rq_src,
@@ -992,6 +998,13 @@ blk_status_t errno_to_blk_status(int errno);bool blk_mq_poll (struct request_queue *q,blk_qc_t cookie); +static inline struct request *blk_get_request (struct request_queue *q, +unsigned int op, +gfp_t gfp_mask) +{ +return __blk_get_request (q, op, gfp_mask,0 ); +} +static inline struct request_queue *bdev_get_queue (struct block_device *bdev) {return bdev->bd_disk->queue;/* this is never NULL */
Signature · 1 line
2.9.5
Re: [PATCH V2 6/8] block: allow to allocate req with REQF_PREEMPT when queue is frozen
T24gU2F0LCAyMDE3LTA5LTAyIGF0IDAyOjQ5ICswODAwLCBNaW5nIExlaSB3cm90ZToNCj4gKwlp ZiAoKGZsYWdzICYgQkxLX01RX1JFUV9QUkVFTVBUKSAmJiBibGtfcXVldWVfaXNfZnJlZXppbmco cSkpDQo+ICsJCWJsa19xdWV1ZV9lbnRlcl9saXZlKHEpOw0KPiArCWVsc2UNCj4gKwkJcmV0ID0g YmxrX3F1ZXVlX2VudGVyKHEsICEoZ2ZwX21hc2sgJiBfX0dGUF9ESVJFQ1RfUkVDTEFJTSkpOw0K DQpXaHkgZGlkIHlvdSByZXBvc3QgdGhpcyBwYXRjaCBzZXJpZXMgd2l0aG91dCB0cnlpbmcgdG8g cmVhY2ggYW4gYWdyZWVtZW50DQphYm91dCB0aGUgYXBwcm9hY2g/DQoNCkFueXdheSwgYmVjYXVz ZSBvZiB0aGUgdW5zYWZlIGJsa19xdWV1ZV9lbnRlcl9saXZlKCkgY2FsbCBpbnRyb2R1Y2VkIGJ5 IHRoaXMNCnBhdGNoLCBwbGVhc2UgYWRkIHRoZSBmb2xsb3dpbmcgdG8gdGhlIGRlc2NyaXB0aW9u IG9mIHRoaXMgcGF0Y2ggd2hlbmV2ZXIgeW91DQpyZXBvc3QgaXQ6DQoNCk5BSy1lZC1ieTogQmFy dCBWYW4gQXNzY2hlIDxiYXJ0LnZhbmFzc2NoZUB3ZGMuY29tPg0KDQo=
Re: [PATCH V2 6/8] block: allow to allocate req with REQF_PREEMPT when queue is frozen
Quoted 9 lines · Bart Van Assche
block/blk-core.c static struct request *blk_old_get_request(struct request_queue *q, · line 1423
Quoted 4 lines · Ming Lei
block/blk-core.c +4
-0+if ((flags &BLK_MQ_REQ_PREEMPT ) &&blk_queue_is_freezing (q)) +blk_queue_enter_live (q); +else + ret =blk_queue_enter (q, !(gfp_mask & __GFP_DIRECT_RECLAIM));Why did you repost this patch series without trying to reach an agreement about the approach?
Quoted 5 lines
Anyway, because of the unsafe
blk_queue_enter_live()call introduced by this patch, please add the following to the description of this patch whenever you repost it:NAK-ed-by Bart Van Assche <[email protected]>
Did you take a look at the patch 7 or cover letter? In that patch, if preempt freezing is on-progressing, any other freeze can't be started at all, so that is definitely safe to use blk_queue_enter_live() here, isn't it?
Signature · 1 line
Ming
PATCH v2 7/8 block: introduce preempt version of blk_[freeze|unfreeze]_queue
In reply to Ming LeiThe two APIs are required to allow request allocation for RQF_PREEMPT when queue is frozen.
The following two points have to be guaranteed for one queue:
1) preempt freezing can be started only after all pending normal & preempt freezing are completed
2) normal freezing can't be started if there is pending preempt freezing.
Because for normal freezing, once blk_mq_freeze_queue_wait() is returned, we have to make sure no I/Os are pending.
rwsem should have been perfect for this kind of sync, but lockdep will complain in case of nested normal freeze.
So spin_lock with freezing status is used for the sync.
--- block/blk-core.c | 2 ++ block/blk-mq.c | 72 +++++++++++++++++++++++++++++++++++++++++++++++--- include/linux/blk-mq.h | 2 ++ include/linux/blkdev.h | 3 +++ 4 files changed, 76 insertions(+), 3 deletions(-)
block/blk-core.c +2 -0
@@ -899,6 +899,8 @@ struct request_queue *blk_alloc_queue_node(gfp_t gfp_mask, int node_id)if (blkcg_init_queue (q)) goto fail_ref; +spin_lock_init (&q->freeze_lock); +return q; fail_ref:
block/blk-mq.c +69 -3
105 lines not on this page · raw message
include/linux/blk-mq.h +2 -0
@@ -259,6 +259,8 @@ void blk_mq_tagset_busy_iter(struct blk_mq_tag_set *tagset, busy_tag_iter_fn *fn, void *priv);void blk_freeze_queue (struct request_queue *q);void blk_unfreeze_queue (struct request_queue *q); +void blk_freeze_queue_preempt (struct request_queue *q); +void blk_unfreeze_queue_preempt (struct request_queue *q);void blk_freeze_queue_start (struct request_queue *q);void blk_freeze_queue_wait (struct request_queue *q); int blk_mq_freeze_queue_wait_timeout(struct request_queue *q,
include/linux/blkdev.h +3 -0
@@ -565,6 +565,9 @@ struct request_queue {int bypass_depth;atomic_t mq_freeze_depth; +spinlock_t freeze_lock; + unsigned normal_freezing:1; + unsigned preempt_freezing:1;#if defined(CONFIG_BLK_DEV_BSG )bsg_job_fn *bsg_job_fn;
Signature · 1 line
2.9.5
PATCH v2 8/8 SCSI: freeze block queue when SCSI device is put into quiesce
In reply to Ming LeiSimply quiesing SCSI device and waiting for completeion of IO dispatched to SCSI queue isn't safe, it is easy to use up requests because all these allocated requests can't be dispatched when device is put in QIUESCE. Then no request can be allocated for RQF_PREEMPT, and system may hang somewhere, such as When sending commands of sync_cache or start_stop during system suspend path.
Before quiesing SCSI, this patch freezes block queue first, so no new request can enter queue any more, and all pending requests are drained too once blk_freeze_queue is returned.
This patch also uses __blk_get_request() for allocating request with RQF_PREEMPT, so that the allocation can succeed even though block queue is frozen.
--- drivers/scsi/scsi_lib.c | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-)
drivers/scsi/scsi_lib.c +19 -2
@@ -243,10 +243,12 @@ int scsi_execute(struct scsi_device *sdev, const unsigned char *cmd,struct request *req;struct scsi_request *rq;int ret =DRIVER_ERROR <<24 ; +unsigned flag = sdev->sdev_state ==SDEV_QUIESCE ?BLK_REQ_PREEMPT :0 ; - req =blk_get_request (sdev->request_queue, + req =__blk_get_request (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 , __GFP_RECLAIM, + flag);if (IS_ERR (req))return ret; rq =scsi_req (req);
@@ -2890,6 +2892,19 @@ scsi_device_quiesce(struct scsi_device *sdev) {int err; +/* +* Simply quiesing SCSI device isn't safe, it is easy +* to use up requests because all these allocated requests +* can't be dispatched when device is put in QIUESCE. +* Then no request can be allocated and we may hang +* somewhere, such as system suspend/resume. +* +* So we freeze block queue first, no new request can +* enter queue any more, and pending requests are drained +* once blk_freeze_queue is returned. +*/ +blk_freeze_queue_preempt (sdev->request_queue); +mutex_lock (&sdev->state_mutex); err =scsi_device_set_state (sdev,SDEV_QUIESCE );mutex_unlock (&sdev->state_mutex);
@@ -2926,6 +2941,8 @@ void scsi_device_resume(struct scsi_device *sdev) scsi_device_set_state(sdev, SDEV_RUNNING) == 0)scsi_run_queue (sdev->request_queue);mutex_unlock (&sdev->state_mutex); + +blk_unfreeze_queue_preempt (sdev->request_queue); }EXPORT_SYMBOL (scsi_device_resume);
Signature · 1 line
2.9.5
With regard to suspend/resume cycle:
Quoted 67 lines · On pátek 1. září 2017 20:49:49 CEST Ming Lei wrote:
Hi,
The current SCSI quiesce isn't safe and easy to trigger I/O deadlock.
Once SCSI device is put into QUIESCE, no new request except for
RQF_PREEMPTcan be dispatched to SCSI successfully, andscsi_device_quiesce()just simply waits for completion of I/Os dispatched to SCSI stack. It isn't enough at all.Because new request still can be allocated, but all the allocated requests can't be dispatched successfully, so request pool can be consumed up easily.
Then request with
RQF_PREEMPTcan't be allocated, and system may hang forever, such as during system suspend or SCSI domain alidation.Both IO hang inside system suspend[1] or SCSI domain validation were reported before.
This patch tries to solve the issue by freezing block queue during SCSI quiescing, and allowing to allocate request of
RQF_PREEMPTwhen queue is frozen.Both SCSI and
SCSI_MQhave this IO deadlock issue, this patch fixes them all by introducing preempt version ofblk_freeze_queue()andblk_unfreeze_queue().V2:
- drop the 1st patch in V1 becausepercpu_ref_is_dying()is enough as pointed by Tejun- introduce preempt version of blk_[freeze|unfreeze]_queue
- sync between preempt freeze and normal freeze
- fix warning from percpu-refcount as reported by Oleksandr
[1] https://marc.info/?t=150340250100013&r=3&w=2
Ming Lei (8):
blk-mq: renameblk_mq_unfreeze_queueasblk_unfreeze_queue
blk-mq: renameblk_mq_freeze_queueasblk_freeze_queue
blk-mq: only run hw queues for blk-mq
blk-mq: renameblk_mq_freeze_queue_waitasblk_freeze_queue_wait
block: tracking request allocation withq_usage_counter
block: allow to allocate req withREQF_PREEMPTwhen queue is frozen
block: introduce preempt version of blk_[freeze|unfreeze]_queue
SCSI: freeze block queue when SCSI device is put into quiesceblock/bfq-iosched.c | 2 +- block/blk-cgroup.c | 8 ++-- block/blk-core.c | 50 ++++++++++++++++---- block/blk-mq.c | 119 ++++++++++++++++++++++++++++++++++++----------- block/blk-mq.h | 1 - block/blk.h | 6 +++ block/elevator.c | 4 +- drivers/block/loop.c | 16 +++---- drivers/block/rbd.c | 2 +- drivers/nvme/host/core.c | 8 ++-- drivers/scsi/scsi_lib.c | 21 ++++++++- include/linux/blk-mq.h | 15 +++--- include/linux/blkdev.h | 20 +++++++- 13 files changed, 206 insertions(+), 66 deletions(-)