aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBob Liu <bob.liu@oracle.com>2019-05-20 23:25:55 -0400
committerJens Axboe <axboe@kernel.dk>2019-05-23 12:25:26 -0400
commit7996a8b5511a72465b0b286763c2d8f412b8874a (patch)
tree8924f9a161678ae643b08134a7e12e77adb55cce
parent6869875fbc04042ad01654591da60862706e86e3 (diff)
blk-mq: fix hang caused by freeze/unfreeze sequence
The following is a description of a hang in blk_mq_freeze_queue_wait(). The hang happens on attempt to freeze a queue while another task does queue unfreeze. The root cause is an incorrect sequence of percpu_ref_resurrect() and percpu_ref_kill() and as a result those two can be swapped: CPU#0 CPU#1 ---------------- ----------------- q1 = blk_mq_init_queue(shared_tags) q2 = blk_mq_init_queue(shared_tags): blk_mq_add_queue_tag_set(shared_tags): blk_mq_update_tag_set_depth(shared_tags): list_for_each_entry() blk_mq_freeze_queue(q1) > percpu_ref_kill() > blk_mq_freeze_queue_wait() blk_cleanup_queue(q1) blk_mq_freeze_queue(q1) > percpu_ref_kill() ^^^^^^ freeze_depth can't guarantee the order blk_mq_unfreeze_queue() > percpu_ref_resurrect() > blk_mq_freeze_queue_wait() ^^^^^^ Hang here!!!! This wrong sequence raises kernel warning: percpu_ref_kill_and_confirm called more than once on blk_queue_usage_counter_release! WARNING: CPU: 0 PID: 11854 at lib/percpu-refcount.c:336 percpu_ref_kill_and_confirm+0x99/0xb0 But the most unpleasant effect is a hang of a blk_mq_freeze_queue_wait(), which waits for a zero of a q_usage_counter, which never happens because percpu-ref was reinited (instead of being killed) and stays in PERCPU state forever. How to reproduce: - "insmod null_blk.ko shared_tags=1 nr_devices=0 queue_mode=2" - cpu0: python Script.py 0; taskset the corresponding process running on cpu0 - cpu1: python Script.py 1; taskset the corresponding process running on cpu1 Script.py: ------ #!/usr/bin/python3 import os import sys while True: on = "echo 1 > /sys/kernel/config/nullb/%s/power" % sys.argv[1] off = "echo 0 > /sys/kernel/config/nullb/%s/power" % sys.argv[1] os.system(on) os.system(off) ------ This bug was first reported and fixed by Roman, previous discussion: [1] Message id: 1443287365-4244-7-git-send-email-akinobu.mita@gmail.com [2] Message id: 1443563240-29306-6-git-send-email-tj@kernel.org [3] https://patchwork.kernel.org/patch/9268199/ Reviewed-by: Hannes Reinecke <hare@suse.com> Reviewed-by: Ming Lei <ming.lei@redhat.com> Reviewed-by: Bart Van Assche <bvanassche@acm.org> Reviewed-by: Christoph Hellwig <hch@lst.de> Signed-off-by: Roman Pen <roman.penyaev@profitbricks.com> Signed-off-by: Bob Liu <bob.liu@oracle.com> Signed-off-by: Jens Axboe <axboe@kernel.dk>
-rw-r--r--block/blk-core.c3
-rw-r--r--block/blk-mq.c19
-rw-r--r--include/linux/blkdev.h7
3 files changed, 18 insertions, 11 deletions
diff --git a/block/blk-core.c b/block/blk-core.c
index 419d600e6637..1bf83a0df0f6 100644
--- a/block/blk-core.c
+++ b/block/blk-core.c
@@ -413,7 +413,7 @@ int blk_queue_enter(struct request_queue *q, blk_mq_req_flags_t flags)
413 smp_rmb(); 413 smp_rmb();
414 414
415 wait_event(q->mq_freeze_wq, 415 wait_event(q->mq_freeze_wq,
416 (atomic_read(&q->mq_freeze_depth) == 0 && 416 (!q->mq_freeze_depth &&
417 (pm || (blk_pm_request_resume(q), 417 (pm || (blk_pm_request_resume(q),
418 !blk_queue_pm_only(q)))) || 418 !blk_queue_pm_only(q)))) ||
419 blk_queue_dying(q)); 419 blk_queue_dying(q));
@@ -503,6 +503,7 @@ struct request_queue *blk_alloc_queue_node(gfp_t gfp_mask, int node_id)
503 spin_lock_init(&q->queue_lock); 503 spin_lock_init(&q->queue_lock);
504 504
505 init_waitqueue_head(&q->mq_freeze_wq); 505 init_waitqueue_head(&q->mq_freeze_wq);
506 mutex_init(&q->mq_freeze_lock);
506 507
507 /* 508 /*
508 * Init percpu_ref in atomic mode so that it's faster to shutdown. 509 * Init percpu_ref in atomic mode so that it's faster to shutdown.
diff --git a/block/blk-mq.c b/block/blk-mq.c
index 08a6248d8536..32b8ad3d341b 100644
--- a/block/blk-mq.c
+++ b/block/blk-mq.c
@@ -144,13 +144,14 @@ void blk_mq_in_flight_rw(struct request_queue *q, struct hd_struct *part,
144 144
145void blk_freeze_queue_start(struct request_queue *q) 145void blk_freeze_queue_start(struct request_queue *q)
146{ 146{
147 int freeze_depth; 147 mutex_lock(&q->mq_freeze_lock);
148 148 if (++q->mq_freeze_depth == 1) {
149 freeze_depth = atomic_inc_return(&q->mq_freeze_depth);
150 if (freeze_depth == 1) {
151 percpu_ref_kill(&q->q_usage_counter); 149 percpu_ref_kill(&q->q_usage_counter);
150 mutex_unlock(&q->mq_freeze_lock);
152 if (queue_is_mq(q)) 151 if (queue_is_mq(q))
153 blk_mq_run_hw_queues(q, false); 152 blk_mq_run_hw_queues(q, false);
153 } else {
154 mutex_unlock(&q->mq_freeze_lock);
154 } 155 }
155} 156}
156EXPORT_SYMBOL_GPL(blk_freeze_queue_start); 157EXPORT_SYMBOL_GPL(blk_freeze_queue_start);
@@ -199,14 +200,14 @@ EXPORT_SYMBOL_GPL(blk_mq_freeze_queue);
199 200
200void blk_mq_unfreeze_queue(struct request_queue *q) 201void blk_mq_unfreeze_queue(struct request_queue *q)
201{ 202{
202 int freeze_depth; 203 mutex_lock(&q->mq_freeze_lock);
203 204 q->mq_freeze_depth--;
204 freeze_depth = atomic_dec_return(&q->mq_freeze_depth); 205 WARN_ON_ONCE(q->mq_freeze_depth < 0);
205 WARN_ON_ONCE(freeze_depth < 0); 206 if (!q->mq_freeze_depth) {
206 if (!freeze_depth) {
207 percpu_ref_resurrect(&q->q_usage_counter); 207 percpu_ref_resurrect(&q->q_usage_counter);
208 wake_up_all(&q->mq_freeze_wq); 208 wake_up_all(&q->mq_freeze_wq);
209 } 209 }
210 mutex_unlock(&q->mq_freeze_lock);
210} 211}
211EXPORT_SYMBOL_GPL(blk_mq_unfreeze_queue); 212EXPORT_SYMBOL_GPL(blk_mq_unfreeze_queue);
212 213
diff --git a/include/linux/blkdev.h b/include/linux/blkdev.h
index 1aafeb923e7b..592669bcc536 100644
--- a/include/linux/blkdev.h
+++ b/include/linux/blkdev.h
@@ -542,7 +542,7 @@ struct request_queue {
542 struct list_head unused_hctx_list; 542 struct list_head unused_hctx_list;
543 spinlock_t unused_hctx_lock; 543 spinlock_t unused_hctx_lock;
544 544
545 atomic_t mq_freeze_depth; 545 int mq_freeze_depth;
546 546
547#if defined(CONFIG_BLK_DEV_BSG) 547#if defined(CONFIG_BLK_DEV_BSG)
548 struct bsg_class_device bsg_dev; 548 struct bsg_class_device bsg_dev;
@@ -554,6 +554,11 @@ struct request_queue {
554#endif 554#endif
555 struct rcu_head rcu_head; 555 struct rcu_head rcu_head;
556 wait_queue_head_t mq_freeze_wq; 556 wait_queue_head_t mq_freeze_wq;
557 /*
558 * Protect concurrent access to q_usage_counter by
559 * percpu_ref_kill() and percpu_ref_reinit().
560 */
561 struct mutex mq_freeze_lock;
557 struct percpu_ref q_usage_counter; 562 struct percpu_ref q_usage_counter;
558 563
559 struct blk_mq_tag_set *tag_set; 564 struct blk_mq_tag_set *tag_set;