blk-mq: fix hang caused by freeze/unfreeze sequence
PATCH v4 1/1 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 attempting 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)
Quoted 1 line
percpu_ref_kill()
^^^^^^ freeze_depth can't guarantee the order
blk_mq_unfreeze_queue()
> percpu_ref_resurrect()
Quoted 1 line
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: [email protected]
[2] Message id: [email protected]
[3] https://patchwork.kernel.org/patch/9268199/
---
v4:
- Update commit log
v3:
- rebase to v5.1
v2:
- forgotten hunk from local repo
- minor tweaks in the commit message
--- block/blk-core.c | 3 ++- block/blk-mq.c | 19 ++++++++++--------- include/linux/blkdev.h | 7 ++++++- 3 files changed, 18 insertions(+), 11 deletions(-)
block/blk-core.c +2 -1
@@ -433,7 +433,7 @@ int blk_queue_enter(struct request_queue *q, blk_mq_req_flags_t flags)smp_rmb ();wait_event (q->mq_freeze_wq, - (atomic_read (&q->mq_freeze_depth) ==0 && + (!q->mq_freeze_depth && (pm || (blk_pm_request_resume (q), !blk_queue_pm_only (q)))) ||blk_queue_dying (q));
@@ -523,6 +523,7 @@ struct request_queue *blk_alloc_queue_node(gfp_t gfp_mask, int node_id)spin_lock_init (&q->queue_lock);init_waitqueue_head (&q->mq_freeze_wq); +mutex_init (&q->mq_freeze_lock);/* * Init percpu_ref in atomic mode so that it's faster to shutdown.
block/blk-mq.c +10 -9
@@ -143,13 +143,14 @@ void blk_mq_in_flight_rw(struct request_queue *q, struct hd_struct *part,void blk_freeze_queue_start (struct request_queue *q) { -int freeze_depth; - - freeze_depth =atomic_inc_return (&q->mq_freeze_depth); -if (freeze_depth ==1 ) { +mutex_lock (&q->mq_freeze_lock); +if (++q->mq_freeze_depth ==1 ) {percpu_ref_kill (&q->q_usage_counter); +mutex_unlock (&q->mq_freeze_lock);if (queue_is_mq (q))blk_mq_run_hw_queues (q, false); + }else { +mutex_unlock (&q->mq_freeze_lock); } }EXPORT_SYMBOL_GPL (blk_freeze_queue_start);
@@ -198,14 +199,14 @@ EXPORT_SYMBOL_GPL(blk_mq_freeze_queue);void blk_mq_unfreeze_queue (struct request_queue *q) { -int freeze_depth; - - freeze_depth =atomic_dec_return (&q->mq_freeze_depth); -WARN_ON_ONCE (freeze_depth <0 ); -if (!freeze_depth) { +mutex_lock (&q->mq_freeze_lock); + q->mq_freeze_depth--; +WARN_ON_ONCE (q->mq_freeze_depth <0 ); +if (!q->mq_freeze_depth) {percpu_ref_resurrect (&q->q_usage_counter);wake_up_all (&q->mq_freeze_wq); } +mutex_unlock (&q->mq_freeze_lock); }EXPORT_SYMBOL_GPL (blk_mq_unfreeze_queue);
include/linux/blkdev.h +6 -1
@@ -535,7 +535,7 @@ struct request_queue {struct mutex sysfs_lock; -atomic_t mq_freeze_depth; +int mq_freeze_depth;#if defined(CONFIG_BLK_DEV_BSG )struct bsg_class_device bsg_dev;
@@ -547,6 +547,11 @@ struct request_queue {#endif struct rcu_head rcu_head;wait_queue_head_t mq_freeze_wq; +/* +* Protect concurrent access to q_usage_counter by +* percpu_ref_kill() and percpu_ref_reinit(). +*/ +struct mutex mq_freeze_lock;struct percpu_ref q_usage_counter;struct list_head all_q_node;
--
2.9.5
Quoted 157 lines · Bob Liu
The following is a description of a hang in
blk_mq_freeze_queue_wait(). The hang happens on attempting to freeze a queue while another task does queue unfreeze.The root cause is an incorrect sequence of
percpu_ref_resurrect()andpercpu_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)Quoted 1 line
percpu_ref_kill()^^^^^^
freeze_depthcan't guarantee the order
blk_mq_unfreeze_queue()
>percpu_ref_resurrect()Quoted 1 line
blk_mq_freeze_queue_wait()^^^^^^ Hang here!!!!This wrong sequence raises kernel warning:
percpu_ref_kill_and_confirmcalled more than once onblk_queue_usage_counter_release!WARNING: CPU: 0 PID: 11854 at lib/percpu-refcount.c:336 percpu_ref_kill_and_confirm+0x99/0xb0But the most unpleasant effect is a hang of a
blk_mq_freeze_queue_wait(), which waits for a zero of aq_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: pythonScript.py0; taskset the corresponding process running on cpu0
- cpu1: pythonScript.py1; taskset the corresponding process running on cpu1
Script.py:
------
#!/usr/bin/python3import os
import syswhile 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: [email protected]
[2] Message id: [email protected]
[3] https://patchwork.kernel.org/patch/9268199/Signed-off-by Bob Liu <[email protected]>---
v4:
- Update commit log
v3:
- rebase to v5.1
v2:
- forgotten hunk from local repo
- minor tweaks in the commit message--- block/blk-core.c | 3 ++- block/blk-mq.c | 19 ++++++++++--------- include/linux/blkdev.h | 7 ++++++- 3 files changed, 18 insertions(+), 11 deletions(-)block/blk-core.c +2
-1@@ -433,7 +433,7 @@ int blk_queue_enter(struct request_queue *q, blk_mq_req_flags_t flags)smp_rmb ();wait_event (q->mq_freeze_wq, - (atomic_read (&q->mq_freeze_depth) ==0 && + (!q->mq_freeze_depth && (pm || (blk_pm_request_resume (q), !blk_queue_pm_only (q)))) ||blk_queue_dying (q));@@ -523,6 +523,7 @@ struct request_queue *blk_alloc_queue_node(gfp_t gfp_mask, int node_id)spin_lock_init (&q->queue_lock);init_waitqueue_head (&q->mq_freeze_wq); +mutex_init (&q->mq_freeze_lock);/* * Init percpu_ref in atomic mode so that it's faster to shutdown. block/blk-mq.c +10
-9@@ -143,13 +143,14 @@ void blk_mq_in_flight_rw(struct request_queue *q, struct hd_struct *part,void blk_freeze_queue_start (struct request_queue *q) { -int freeze_depth; - - freeze_depth =atomic_inc_return (&q->mq_freeze_depth); -if (freeze_depth ==1 ) { +mutex_lock (&q->mq_freeze_lock); +if (++q->mq_freeze_depth ==1 ) {percpu_ref_kill (&q->q_usage_counter); +mutex_unlock (&q->mq_freeze_lock);if (queue_is_mq (q))blk_mq_run_hw_queues (q, false); + }else { +mutex_unlock (&q->mq_freeze_lock); } }EXPORT_SYMBOL_GPL (blk_freeze_queue_start);@@ -198,14 +199,14 @@ EXPORT_SYMBOL_GPL(blk_mq_freeze_queue);void blk_mq_unfreeze_queue (struct request_queue *q) { -int freeze_depth; - - freeze_depth =atomic_dec_return (&q->mq_freeze_depth); -WARN_ON_ONCE (freeze_depth <0 ); -if (!freeze_depth) { +mutex_lock (&q->mq_freeze_lock); + q->mq_freeze_depth--; +WARN_ON_ONCE (q->mq_freeze_depth <0 ); +if (!q->mq_freeze_depth) {percpu_ref_resurrect (&q->q_usage_counter);wake_up_all (&q->mq_freeze_wq); } +mutex_unlock (&q->mq_freeze_lock); }EXPORT_SYMBOL_GPL (blk_mq_unfreeze_queue);include/linux/blkdev.h +1
-1@@ -535,7 +535,7 @@ struct request_queue {struct mutex sysfs_lock; -atomic_t mq_freeze_depth; +int mq_freeze_depth;#if defined(CONFIG_BLK_DEV_BSG )struct bsg_class_device bsg_dev;
include/linux/blkdev.h struct request_queue { · line 556
Quoted 10 lines
include/linux/blkdev.h +5
-0@@ -547,6 +547,11 @@ struct request_queue {#endif struct rcu_head rcu_head;wait_queue_head_t mq_freeze_wq; +/* +* Protect concurrent access to q_usage_counter by +* percpu_ref_kill() and percpu_ref_reinit(). +*/ +struct mutex mq_freeze_lock;struct percpu_ref q_usage_counter;struct list_head all_q_node;
Looks fine,
Thanks,
Ming Lei
Quoted 3 lines · Bob Liu
The following is a description of a hang in
blk_mq_freeze_queue_wait(). The hang happens on attempting to freeze a queue while another task does queue unfreeze.
Hey Jens,
Could you please queue this patch?
As Jinpu mentioned:
"We at 1 & 1 IONOS (former ProfitBricks) have been carried this patch for some years, it has been running in production for some years too"
Thanks,
Bob
Quoted 170 lines · Bob Liu
The following is a description of a hang in
blk_mq_freeze_queue_wait(). The hang happens on attempting to freeze a queue while another task does queue unfreeze.The root cause is an incorrect sequence of
percpu_ref_resurrect()andpercpu_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)Quoted 1 line
percpu_ref_kill()^^^^^^
freeze_depthcan't guarantee the order
blk_mq_unfreeze_queue()
>percpu_ref_resurrect()Quoted 1 line
blk_mq_freeze_queue_wait()^^^^^^ Hang here!!!!This wrong sequence raises kernel warning:
percpu_ref_kill_and_confirmcalled more than once onblk_queue_usage_counter_release!WARNING: CPU: 0 PID: 11854 at lib/percpu-refcount.c:336 percpu_ref_kill_and_confirm+0x99/0xb0But the most unpleasant effect is a hang of a
blk_mq_freeze_queue_wait(), which waits for a zero of aq_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: pythonScript.py0; taskset the corresponding process running on cpu0
- cpu1: pythonScript.py1; taskset the corresponding process running on cpu1
Script.py:
------
#!/usr/bin/python3import os
import syswhile 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: [email protected]
[2] Message id: [email protected]
[3] https://patchwork.kernel.org/patch/9268199/Signed-off-by Bob Liu <[email protected]>---
v4:
- Update commit log
v3:
- rebase to v5.1
v2:
- forgotten hunk from local repo
- minor tweaks in the commit message--- block/blk-core.c | 3 ++- block/blk-mq.c | 19 ++++++++++--------- include/linux/blkdev.h | 7 ++++++- 3 files changed, 18 insertions(+), 11 deletions(-)block/blk-core.c +2
-1@@ -433,7 +433,7 @@ int blk_queue_enter(struct request_queue *q, blk_mq_req_flags_t flags)smp_rmb ();wait_event (q->mq_freeze_wq, - (atomic_read (&q->mq_freeze_depth) ==0 && + (!q->mq_freeze_depth && (pm || (blk_pm_request_resume (q), !blk_queue_pm_only (q)))) ||blk_queue_dying (q));@@ -523,6 +523,7 @@ struct request_queue *blk_alloc_queue_node(gfp_t gfp_mask, int node_id)spin_lock_init (&q->queue_lock);init_waitqueue_head (&q->mq_freeze_wq); +mutex_init (&q->mq_freeze_lock);/* * Init percpu_ref in atomic mode so that it's faster to shutdown. block/blk-mq.c +10
-9@@ -143,13 +143,14 @@ void blk_mq_in_flight_rw(struct request_queue *q, struct hd_struct *part,void blk_freeze_queue_start (struct request_queue *q) { -int freeze_depth; - - freeze_depth =atomic_inc_return (&q->mq_freeze_depth); -if (freeze_depth ==1 ) { +mutex_lock (&q->mq_freeze_lock); +if (++q->mq_freeze_depth ==1 ) {percpu_ref_kill (&q->q_usage_counter); +mutex_unlock (&q->mq_freeze_lock);if (queue_is_mq (q))blk_mq_run_hw_queues (q, false); + }else { +mutex_unlock (&q->mq_freeze_lock); } }EXPORT_SYMBOL_GPL (blk_freeze_queue_start);@@ -198,14 +199,14 @@ EXPORT_SYMBOL_GPL(blk_mq_freeze_queue);void blk_mq_unfreeze_queue (struct request_queue *q) { -int freeze_depth; - - freeze_depth =atomic_dec_return (&q->mq_freeze_depth); -WARN_ON_ONCE (freeze_depth <0 ); -if (!freeze_depth) { +mutex_lock (&q->mq_freeze_lock); + q->mq_freeze_depth--; +WARN_ON_ONCE (q->mq_freeze_depth <0 ); +if (!q->mq_freeze_depth) {percpu_ref_resurrect (&q->q_usage_counter);wake_up_all (&q->mq_freeze_wq); } +mutex_unlock (&q->mq_freeze_lock); }EXPORT_SYMBOL_GPL (blk_mq_unfreeze_queue);include/linux/blkdev.h +6
-2@@ -535,7 +535,7 @@ struct request_queue {struct mutex sysfs_lock; -atomic_t mq_freeze_depth; +int mq_freeze_depth;#if defined(CONFIG_BLK_DEV_BSG )struct bsg_class_device bsg_dev;@@ -547,6 +547,11 @@ struct request_queue {#endif struct rcu_head rcu_head;wait_queue_head_t mq_freeze_wq; +/* +* Protect concurrent access to q_usage_counter by +* percpu_ref_kill() and percpu_ref_reinit(). +*/ +struct mutex mq_freeze_lock;struct percpu_ref q_usage_counter;struct list_head all_q_node; --2.9.5