[PATCH V6 5/5] blk-mq-sched: don't dequeue request until all in ->dispatch are flushed

From: Ming Lei
Date: Mon Oct 09 2017 - 07:25:08 EST


During dispatching, we moved all requests from hctx->dispatch to
one temporary list, then dispatch them one by one from this list.
Unfortunately during this period, run queue from other contexts
may think the queue is idle, then start to dequeue from sw/scheduler
queue and still try to dispatch because ->dispatch is empty. This way
hurts sequential I/O performance because requests are dequeued when
lld queue is busy.

This patch introduces the state of BLK_MQ_S_DISPATCH_BUSY to
make sure that request isn't dequeued until ->dispatch is
flushed.

Reviewed-by: Bart Van Assche <bart.vanassche@xxxxxxx>
Reviewed-by: Christoph Hellwig <hch@xxxxxx>
Signed-off-by: Ming Lei <ming.lei@xxxxxxxxxx>
---
block/blk-mq-debugfs.c | 1 +
block/blk-mq-sched.c | 38 ++++++++++++++++++++++++++++++++------
block/blk-mq.c | 5 +++++
include/linux/blk-mq.h | 1 +
4 files changed, 39 insertions(+), 6 deletions(-)

diff --git a/block/blk-mq-debugfs.c b/block/blk-mq-debugfs.c
index e5dccc9f6f1d..6c15487bc3ff 100644
--- a/block/blk-mq-debugfs.c
+++ b/block/blk-mq-debugfs.c
@@ -181,6 +181,7 @@ static const char *const hctx_state_name[] = {
HCTX_STATE_NAME(SCHED_RESTART),
HCTX_STATE_NAME(TAG_WAITING),
HCTX_STATE_NAME(START_ON_RUN),
+ HCTX_STATE_NAME(DISPATCH_BUSY),
};
#undef HCTX_STATE_NAME

diff --git a/block/blk-mq-sched.c b/block/blk-mq-sched.c
index 14b354f617e5..9f549711da84 100644
--- a/block/blk-mq-sched.c
+++ b/block/blk-mq-sched.c
@@ -95,6 +95,18 @@ static void blk_mq_do_dispatch_sched(struct blk_mq_hw_ctx *hctx)
struct elevator_queue *e = q->elevator;
LIST_HEAD(rq_list);

+ /*
+ * If DISPATCH_BUSY is set, that means hw queue is busy
+ * and requests in the list of hctx->dispatch need to
+ * be flushed first, so return early.
+ *
+ * Wherever DISPATCH_BUSY is set, blk_mq_run_hw_queue()
+ * will be run to try to make progress, so it is always
+ * safe to check the state here.
+ */
+ if (test_bit(BLK_MQ_S_DISPATCH_BUSY, &hctx->state))
+ return;
+
do {
struct request *rq = e->type->ops.mq.dispatch_request(hctx);

@@ -121,6 +133,10 @@ static void blk_mq_do_dispatch_ctx(struct blk_mq_hw_ctx *hctx)
LIST_HEAD(rq_list);
struct blk_mq_ctx *ctx = READ_ONCE(hctx->dispatch_from);

+ /* See same comment in blk_mq_do_dispatch_sched() */
+ if (test_bit(BLK_MQ_S_DISPATCH_BUSY, &hctx->state))
+ return;
+
do {
struct request *rq;

@@ -176,12 +192,22 @@ void blk_mq_sched_dispatch_requests(struct blk_mq_hw_ctx *hctx)
*/
if (!list_empty(&rq_list)) {
blk_mq_sched_mark_restart_hctx(hctx);
- if (blk_mq_dispatch_rq_list(q, &rq_list)) {
- if (has_sched_dispatch)
- blk_mq_do_dispatch_sched(hctx);
- else
- blk_mq_do_dispatch_ctx(hctx);
- }
+ blk_mq_dispatch_rq_list(q, &rq_list);
+
+ /*
+ * We may clear DISPATCH_BUSY just after it is set from
+ * another context, the only cost is that one request is
+ * dequeued a bit early, we can survive that. Given the
+ * window is small enough, no need to worry about performance
+ * effect.
+ */
+ if (list_empty_careful(&hctx->dispatch))
+ clear_bit(BLK_MQ_S_DISPATCH_BUSY, &hctx->state);
+
+ if (has_sched_dispatch)
+ blk_mq_do_dispatch_sched(hctx);
+ else
+ blk_mq_do_dispatch_ctx(hctx);
} else if (has_sched_dispatch) {
blk_mq_do_dispatch_sched(hctx);
} else if (q->queue_depth) {
diff --git a/block/blk-mq.c b/block/blk-mq.c
index 394cb75d66fa..06dda6182b7a 100644
--- a/block/blk-mq.c
+++ b/block/blk-mq.c
@@ -1172,6 +1172,11 @@ bool blk_mq_dispatch_rq_list(struct request_queue *q, struct list_head *list)

spin_lock(&hctx->lock);
list_splice_init(list, &hctx->dispatch);
+ /*
+ * DISPATCH_BUSY won't be cleared until all requests
+ * in hctx->dispatch are dispatched successfully
+ */
+ set_bit(BLK_MQ_S_DISPATCH_BUSY, &hctx->state);
spin_unlock(&hctx->lock);

/*
diff --git a/include/linux/blk-mq.h b/include/linux/blk-mq.h
index 7b7a366a97f3..13f6c25fa461 100644
--- a/include/linux/blk-mq.h
+++ b/include/linux/blk-mq.h
@@ -172,6 +172,7 @@ enum {
BLK_MQ_S_SCHED_RESTART = 2,
BLK_MQ_S_TAG_WAITING = 3,
BLK_MQ_S_START_ON_RUN = 4,
+ BLK_MQ_S_DISPATCH_BUSY = 5,

BLK_MQ_MAX_DEPTH = 10240,

--
2.9.5