Skip to content

Commit 04ced15

Browse files
committed
blk-mq: move hctx lock/unlock into a helper
Move the RCU vs SRCU logic into lock/unlock helpers, which makes the actual functional bits within the locked region much easier to read. tj: Reordered in front of timeout revamp patches and added the missing blk_mq_run_hw_queue() conversion. Signed-off-by: Jens Axboe <axboe@kernel.dk> Signed-off-by: Tejun Heo <tj@kernel.org> Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent 0d52af5 commit 04ced15

File tree

1 file changed

+32
-34
lines changed

1 file changed

+32
-34
lines changed

block/blk-mq.c

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,22 @@ static void __blk_mq_complete_request(struct request *rq)
557557
put_cpu();
558558
}
559559

560+
static void hctx_unlock(struct blk_mq_hw_ctx *hctx, int srcu_idx)
561+
{
562+
if (!(hctx->flags & BLK_MQ_F_BLOCKING))
563+
rcu_read_unlock();
564+
else
565+
srcu_read_unlock(hctx->queue_rq_srcu, srcu_idx);
566+
}
567+
568+
static void hctx_lock(struct blk_mq_hw_ctx *hctx, int *srcu_idx)
569+
{
570+
if (!(hctx->flags & BLK_MQ_F_BLOCKING))
571+
rcu_read_lock();
572+
else
573+
*srcu_idx = srcu_read_lock(hctx->queue_rq_srcu);
574+
}
575+
560576
/**
561577
* blk_mq_complete_request - end I/O on a request
562578
* @rq: the request being processed
@@ -1214,17 +1230,11 @@ static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx)
12141230
*/
12151231
WARN_ON_ONCE(in_interrupt());
12161232

1217-
if (!(hctx->flags & BLK_MQ_F_BLOCKING)) {
1218-
rcu_read_lock();
1219-
blk_mq_sched_dispatch_requests(hctx);
1220-
rcu_read_unlock();
1221-
} else {
1222-
might_sleep();
1233+
might_sleep_if(hctx->flags & BLK_MQ_F_BLOCKING);
12231234

1224-
srcu_idx = srcu_read_lock(hctx->queue_rq_srcu);
1225-
blk_mq_sched_dispatch_requests(hctx);
1226-
srcu_read_unlock(hctx->queue_rq_srcu, srcu_idx);
1227-
}
1235+
hctx_lock(hctx, &srcu_idx);
1236+
blk_mq_sched_dispatch_requests(hctx);
1237+
hctx_unlock(hctx, srcu_idx);
12281238
}
12291239

12301240
/*
@@ -1296,17 +1306,10 @@ bool blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
12961306
* And queue will be rerun in blk_mq_unquiesce_queue() if it is
12971307
* quiesced.
12981308
*/
1299-
if (!(hctx->flags & BLK_MQ_F_BLOCKING)) {
1300-
rcu_read_lock();
1301-
need_run = !blk_queue_quiesced(hctx->queue) &&
1302-
blk_mq_hctx_has_pending(hctx);
1303-
rcu_read_unlock();
1304-
} else {
1305-
srcu_idx = srcu_read_lock(hctx->queue_rq_srcu);
1306-
need_run = !blk_queue_quiesced(hctx->queue) &&
1307-
blk_mq_hctx_has_pending(hctx);
1308-
srcu_read_unlock(hctx->queue_rq_srcu, srcu_idx);
1309-
}
1309+
hctx_lock(hctx, &srcu_idx);
1310+
need_run = !blk_queue_quiesced(hctx->queue) &&
1311+
blk_mq_hctx_has_pending(hctx);
1312+
hctx_unlock(hctx, srcu_idx);
13101313

13111314
if (need_run) {
13121315
__blk_mq_delay_run_hw_queue(hctx, async, 0);
@@ -1618,7 +1621,7 @@ static blk_qc_t request_to_qc_t(struct blk_mq_hw_ctx *hctx, struct request *rq)
16181621

16191622
static void __blk_mq_try_issue_directly(struct blk_mq_hw_ctx *hctx,
16201623
struct request *rq,
1621-
blk_qc_t *cookie, bool may_sleep)
1624+
blk_qc_t *cookie)
16221625
{
16231626
struct request_queue *q = rq->q;
16241627
struct blk_mq_queue_data bd = {
@@ -1668,25 +1671,20 @@ static void __blk_mq_try_issue_directly(struct blk_mq_hw_ctx *hctx,
16681671
}
16691672

16701673
insert:
1671-
blk_mq_sched_insert_request(rq, false, run_queue, false, may_sleep);
1674+
blk_mq_sched_insert_request(rq, false, run_queue, false,
1675+
hctx->flags & BLK_MQ_F_BLOCKING);
16721676
}
16731677

16741678
static void blk_mq_try_issue_directly(struct blk_mq_hw_ctx *hctx,
16751679
struct request *rq, blk_qc_t *cookie)
16761680
{
1677-
if (!(hctx->flags & BLK_MQ_F_BLOCKING)) {
1678-
rcu_read_lock();
1679-
__blk_mq_try_issue_directly(hctx, rq, cookie, false);
1680-
rcu_read_unlock();
1681-
} else {
1682-
unsigned int srcu_idx;
1681+
int srcu_idx;
16831682

1684-
might_sleep();
1683+
might_sleep_if(hctx->flags & BLK_MQ_F_BLOCKING);
16851684

1686-
srcu_idx = srcu_read_lock(hctx->queue_rq_srcu);
1687-
__blk_mq_try_issue_directly(hctx, rq, cookie, true);
1688-
srcu_read_unlock(hctx->queue_rq_srcu, srcu_idx);
1689-
}
1685+
hctx_lock(hctx, &srcu_idx);
1686+
__blk_mq_try_issue_directly(hctx, rq, cookie);
1687+
hctx_unlock(hctx, srcu_idx);
16901688
}
16911689

16921690
static blk_qc_t blk_mq_make_request(struct request_queue *q, struct bio *bio)

0 commit comments

Comments
 (0)