[PATCH] blk-mq: rationalize plug

From: Shaohua Li
Date: Sun Nov 30 2014 - 18:17:25 EST


plug is still helpful for workload with IO merge, but it can be harmful
otherwise especially with multiple hardware queues, as there is (supposed) no
lock contention in this case and plug can introduce latency.

For single queue, we always do plug. Reducing lock contention is still a win.
For multiple queues, we do a limited plug, eg plug only if there is merge. If a
request doesn't have merge with following request, the requet will be
dispatched immediately.

Signed-off-by: Shaohua Li <shli@xxxxxx>
---
block/blk-mq.c | 82 ++++++++++++++++++++++++++++++++++++++++++++--------------
1 file changed, 63 insertions(+), 19 deletions(-)

diff --git a/block/blk-mq.c b/block/blk-mq.c
index d5b4643..6c90354 100644
--- a/block/blk-mq.c
+++ b/block/blk-mq.c
@@ -1133,6 +1133,33 @@ static struct request *blk_mq_map_request(struct request_queue *q,
return rq;
}

+static int blk_mq_direct_issue_request(struct request *rq)
+{
+ int ret;
+ struct request_queue *q = rq->q;
+ struct blk_mq_hw_ctx *hctx = q->mq_ops->map_queue(q,
+ rq->mq_ctx->cpu);
+
+ /*
+ * For OK queue, we are done. For error, kill it. Any other
+ * error (busy), just add it to our list as we previously
+ * would have done
+ */
+ ret = q->mq_ops->queue_rq(hctx, rq, true);
+ if (ret == BLK_MQ_RQ_QUEUE_OK)
+ return 0;
+ else {
+ __blk_mq_requeue_request(rq);
+
+ if (ret == BLK_MQ_RQ_QUEUE_ERROR) {
+ rq->errors = -EIO;
+ blk_mq_end_request(rq, rq->errors);
+ return 0;
+ }
+ return -1;
+ }
+}
+
/*
* Multiple hardware queue variant. This will not use per-process plugs,
* but will attempt to bypass the hctx queueing if we can go straight to
@@ -1142,8 +1169,12 @@ static void blk_mq_make_request(struct request_queue *q, struct bio *bio)
{
const int is_sync = rw_is_sync(bio->bi_rw);
const int is_flush_fua = bio->bi_rw & (REQ_FLUSH | REQ_FUA);
+ unsigned int use_plug, request_count = 0;
struct blk_map_ctx data;
struct request *rq;
+ struct blk_plug *plug;
+
+ use_plug = !is_flush_fua;

blk_queue_bounce(q, &bio);

@@ -1152,6 +1183,10 @@ static void blk_mq_make_request(struct request_queue *q, struct bio *bio)
return;
}

+ if (use_plug && !blk_queue_nomerges(q) &&
+ blk_attempt_plug_merge(q, bio, &request_count))
+ return;
+
rq = blk_mq_map_request(q, bio, &data);
if (unlikely(!rq))
return;
@@ -1162,28 +1197,37 @@ static void blk_mq_make_request(struct request_queue *q, struct bio *bio)
goto run_queue;
}

- if (is_sync) {
- int ret;
+ /*
+ * we do limited pluging. If bio can be merged, do merge. Otherwise the
+ * existing request in the plug list will be issued. So the plug list
+ * will have one request at most
+ */
+ plug = current->plug;
+ if (use_plug && plug) {
+ struct request *old_rq = NULL;

blk_mq_bio_to_request(rq, bio);
+ if (!list_empty(&plug->mq_list)) {
+ old_rq = list_first_entry(&plug->mq_list,
+ struct request, queuelist);
+ list_del_init(&old_rq->queuelist);
+ }
+ list_add_tail(&rq->queuelist, &plug->mq_list);
+ blk_mq_put_ctx(data.ctx);

- /*
- * For OK queue, we are done. For error, kill it. Any other
- * error (busy), just add it to our list as we previously
- * would have done
- */
- ret = q->mq_ops->queue_rq(data.hctx, rq, true);
- if (ret == BLK_MQ_RQ_QUEUE_OK)
- goto done;
- else {
- __blk_mq_requeue_request(rq);
+ if (!old_rq)
+ return;

- if (ret == BLK_MQ_RQ_QUEUE_ERROR) {
- rq->errors = -EIO;
- blk_mq_end_request(rq, rq->errors);
- goto done;
- }
- }
+ if (!blk_mq_direct_issue_request(old_rq))
+ return;
+ blk_mq_insert_request(old_rq, false, true, true);
+ return;
+ }
+
+ if (is_sync) {
+ blk_mq_bio_to_request(rq, bio);
+ if (!blk_mq_direct_issue_request(rq))
+ goto done;
}

if (!blk_mq_merge_queue_io(data.hctx, data.ctx, rq, bio)) {
@@ -1216,7 +1260,7 @@ static void blk_sq_make_request(struct request_queue *q, struct bio *bio)
* If we have multiple hardware queues, just go directly to
* one of those for sync IO.
*/
- use_plug = !is_flush_fua && !is_sync;
+ use_plug = !is_flush_fua;

blk_queue_bounce(q, &bio);

--
1.8.3.2

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/