Skip to content

Commit add703f

Browse files
htejunaxboe
authored andcommitted
blk-mq: use percpu_ref for mq usage count
Currently, blk-mq uses a percpu_counter to keep track of how many usages are in flight. The percpu_counter is drained while freezing to ensure that no usage is left in-flight after freezing is complete. blk_mq_queue_enter/exit() and blk_mq_[un]freeze_queue() implement this per-cpu gating mechanism. This type of code has relatively high chance of subtle bugs which are extremely difficult to trigger and it's way too hairy to be open coded in blk-mq. percpu_ref can serve the same purpose after the recent changes. This patch replaces the open-coded per-cpu usage counting and draining mechanism with percpu_ref. blk_mq_queue_enter() performs tryget_live on the ref and exit() performs put. blk_mq_freeze_queue() kills the ref and waits until the reference count reaches zero. blk_mq_unfreeze_queue() revives the ref and wakes up the waiters. Signed-off-by: Tejun Heo <tj@kernel.org> Cc: Jens Axboe <axboe@kernel.dk> Cc: Nicholas A. Bellinger <nab@linux-iscsi.org> Cc: Kent Overstreet <kmo@daterainc.com> Signed-off-by: Jens Axboe <axboe@fb.com>
1 parent 72d6f02 commit add703f

2 files changed

Lines changed: 31 additions & 40 deletions

File tree

‎block/blk-mq.c‎

Lines changed: 29 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -78,34 +78,32 @@ static void blk_mq_hctx_clear_pending(struct blk_mq_hw_ctx *hctx,
7878

7979
static int blk_mq_queue_enter(struct request_queue *q)
8080
{
81-
int ret;
82-
83-
__percpu_counter_add(&q->mq_usage_counter, 1, 1000000);
84-
smp_mb();
85-
86-
/* we have problems freezing the queue if it's initializing */
87-
if (!q->mq_freeze_depth)
88-
return 0;
89-
90-
__percpu_counter_add(&q->mq_usage_counter, -1, 1000000);
81+
while (true) {
82+
int ret;
9183

92-
spin_lock_irq(q->queue_lock);
93-
ret = wait_event_interruptible_lock_irq(q->mq_freeze_wq,
94-
!q->mq_freeze_depth || blk_queue_dying(q),
95-
*q->queue_lock);
96-
/* inc usage with lock hold to avoid freeze_queue runs here */
97-
if (!ret && !blk_queue_dying(q))
98-
__percpu_counter_add(&q->mq_usage_counter, 1, 1000000);
99-
else if (blk_queue_dying(q))
100-
ret = -ENODEV;
101-
spin_unlock_irq(q->queue_lock);
84+
if (percpu_ref_tryget_live(&q->mq_usage_counter))
85+
return 0;
10286

103-
return ret;
87+
ret = wait_event_interruptible(q->mq_freeze_wq,
88+
!q->mq_freeze_depth || blk_queue_dying(q));
89+
if (blk_queue_dying(q))
90+
return -ENODEV;
91+
if (ret)
92+
return ret;
93+
}
10494
}
10595

10696
static void blk_mq_queue_exit(struct request_queue *q)
10797
{
108-
__percpu_counter_add(&q->mq_usage_counter, -1, 1000000);
98+
percpu_ref_put(&q->mq_usage_counter);
99+
}
100+
101+
static void blk_mq_usage_counter_release(struct percpu_ref *ref)
102+
{
103+
struct request_queue *q =
104+
container_of(ref, struct request_queue, mq_usage_counter);
105+
106+
wake_up_all(&q->mq_freeze_wq);
109107
}
110108

111109
/*
@@ -118,18 +116,9 @@ void blk_mq_freeze_queue(struct request_queue *q)
118116
q->mq_freeze_depth++;
119117
spin_unlock_irq(q->queue_lock);
120118

121-
while (true) {
122-
s64 count;
123-
124-
spin_lock_irq(q->queue_lock);
125-
count = percpu_counter_sum(&q->mq_usage_counter);
126-
spin_unlock_irq(q->queue_lock);
127-
128-
if (count == 0)
129-
break;
130-
blk_mq_start_hw_queues(q);
131-
msleep(10);
132-
}
119+
percpu_ref_kill(&q->mq_usage_counter);
120+
blk_mq_run_queues(q, false);
121+
wait_event(q->mq_freeze_wq, percpu_ref_is_zero(&q->mq_usage_counter));
133122
}
134123

135124
static void blk_mq_unfreeze_queue(struct request_queue *q)
@@ -140,8 +129,10 @@ static void blk_mq_unfreeze_queue(struct request_queue *q)
140129
wake = !--q->mq_freeze_depth;
141130
WARN_ON_ONCE(q->mq_freeze_depth < 0);
142131
spin_unlock_irq(q->queue_lock);
143-
if (wake)
132+
if (wake) {
133+
percpu_ref_reinit(&q->mq_usage_counter);
144134
wake_up_all(&q->mq_freeze_wq);
135+
}
145136
}
146137

147138
bool blk_mq_can_queue(struct blk_mq_hw_ctx *hctx)
@@ -1785,7 +1776,7 @@ struct request_queue *blk_mq_init_queue(struct blk_mq_tag_set *set)
17851776
if (!q)
17861777
goto err_hctxs;
17871778

1788-
if (percpu_counter_init(&q->mq_usage_counter, 0))
1779+
if (percpu_ref_init(&q->mq_usage_counter, blk_mq_usage_counter_release))
17891780
goto err_map;
17901781

17911782
setup_timer(&q->timeout, blk_mq_rq_timer, (unsigned long) q);
@@ -1878,7 +1869,7 @@ void blk_mq_free_queue(struct request_queue *q)
18781869
blk_mq_exit_hw_queues(q, set, set->nr_hw_queues);
18791870
blk_mq_free_hw_queues(q, set);
18801871

1881-
percpu_counter_destroy(&q->mq_usage_counter);
1872+
percpu_ref_exit(&q->mq_usage_counter);
18821873

18831874
free_percpu(q->queue_ctx);
18841875
kfree(q->queue_hw_ctx);
@@ -2037,8 +2028,7 @@ static int __init blk_mq_init(void)
20372028
{
20382029
blk_mq_cpu_init();
20392030

2040-
/* Must be called after percpu_counter_hotcpu_callback() */
2041-
hotcpu_notifier(blk_mq_queue_reinit_notify, -10);
2031+
hotcpu_notifier(blk_mq_queue_reinit_notify, 0);
20422032

20432033
return 0;
20442034
}

‎include/linux/blkdev.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <linux/bsg.h>
2222
#include <linux/smp.h>
2323
#include <linux/rcupdate.h>
24+
#include <linux/percpu-refcount.h>
2425

2526
#include <asm/scatterlist.h>
2627

@@ -484,7 +485,7 @@ struct request_queue {
484485
#endif
485486
struct rcu_head rcu_head;
486487
wait_queue_head_t mq_freeze_wq;
487-
struct percpu_counter mq_usage_counter;
488+
struct percpu_ref mq_usage_counter;
488489
struct list_head all_q_node;
489490

490491
struct blk_mq_tag_set *tag_set;

0 commit comments

Comments
 (0)