blk-mq: provide a default .bio_merge
PATCH 1/1 blk-mq: provide a default .bio_merge
Before blk-mq is introduced, I/O is merged before putting into plug queue, but blk-mq changed the order and makes merging basically impossible until mq-deadline is introduced. Then it is observed that throughput of sequential I/O is degraded about 10%~20% on virtio-blk in the test[1] if IO schedluer isn't used.
This patch provides a default per-sw-queue bio merging if there isn't scheduler enabled or the scheduler hasn't implement .bio_merge(), and this way actually moves merging before plugging just like what blk_queue_bio() does, then the performance regression is fixed.
[1]. test script:
sudo fio --direct=1 --size=128G --bsrange=4k-4k --runtime=40 --numjobs=16 --ioengine=libaio --iodepth=64 --group_reporting=1 --filename=/dev/vdb --name=virtio_blk-test-$RW --rw=$RW --output-format=json
RW=read or write
--- block/blk-mq-sched.c | 61 +++++++++++++++++++++++++++++++++---- block/blk-mq-sched.h | 4 +-- block/blk-mq.c | 85 +++++++--------------------------------------------- 3 files changed, 66 insertions(+), 84 deletions(-)
block/blk-mq-sched.c +55 -6
@@ -221,19 +221,68 @@ bool blk_mq_sched_try_merge(struct request_queue *q, struct bio *bio, }EXPORT_SYMBOL_GPL (blk_mq_sched_try_merge); +/* +* Reverse check our software queue for entries that we could potentially +* merge with. Currently includes a hand-wavy stop count of 8, to not spend +* too much time checking for merges. +*/ +static bool blk_mq_attempt_merge (struct request_queue *q, +struct blk_mq_ctx *ctx,struct bio *bio) +{ +struct request *rq; +int checked =8 ; + +list_for_each_entry_reverse (rq, &ctx->rq_list, queuelist) { +bool merged = false; + +if (!checked--) +break ; + +if (!blk_rq_merge_ok (rq, bio)) +continue ; + +switch (blk_try_merge (rq, bio)) { +case ELEVATOR_BACK_MERGE : +if (blk_mq_sched_allow_merge (q, rq, bio)) + merged =bio_attempt_back_merge (q, rq, bio); +break ; +case ELEVATOR_FRONT_MERGE : +if (blk_mq_sched_allow_merge (q, rq, bio)) + merged =bio_attempt_front_merge (q, rq, bio); +break ; +case ELEVATOR_DISCARD_MERGE : + merged =bio_attempt_discard_merge (q, rq, bio); +break ; +default : +continue ; + } + +if (merged) + ctx->rq_merged++; +return merged; + } + +return false; +} +bool __blk_mq_sched_bio_merge (struct request_queue *q,struct bio *bio) {struct elevator_queue *e = q->elevator; +struct blk_mq_ctx *ctx =blk_mq_get_ctx (q); +struct blk_mq_hw_ctx *hctx =blk_mq_map_queue (q, ctx->cpu); +bool ret = false; -if (e->type->ops.mq.bio_merge) { -struct blk_mq_ctx *ctx =blk_mq_get_ctx (q); -struct blk_mq_hw_ctx *hctx =blk_mq_map_queue (q, ctx->cpu); - +if (e && e->type->ops.mq.bio_merge) {blk_mq_put_ctx (ctx);return e->type->ops.mq.bio_merge (hctx, bio); + }else if (hctx->flags &BLK_MQ_F_SHOULD_MERGE ) { +/* default per sw-queue merge */ +spin_lock (&ctx->lock); + ret =blk_mq_attempt_merge (q, ctx, bio); +spin_unlock (&ctx->lock); } - -return false; +blk_mq_put_ctx (ctx); +return ret; }bool blk_mq_sched_try_insert_merge (struct request_queue *q,struct request *rq)
block/blk-mq-sched.h +1 -3
@@ -38,9 +38,7 @@ int blk_mq_sched_init(struct request_queue *q);static inline bool blk_mq_sched_bio_merge (struct request_queue *q,struct bio *bio) { -struct elevator_queue *e = q->elevator; - -if (!e ||blk_queue_nomerges (q) || !bio_mergeable (bio)) +if (blk_queue_nomerges (q) || !bio_mergeable (bio))return false;return __blk_mq_sched_bio_merge (q, bio);
block/blk-mq.c +10 -75
107 lines not on this page · raw message
Signature · 1 line
2.9.3
Quoted 11 lines · Ming Lei
Before blk-mq is introduced, I/O is merged before putting into plug queue, but blk-mq changed the order and makes merging basically impossible until mq-deadline is introduced. Then it is observed that throughput of sequential I/O is degraded about 10%~20% on virtio-blk in the test[1] if IO schedluer isn't used.
This patch provides a default per-sw-queue bio merging if there isn't scheduler enabled or the scheduler hasn't implement .bio_merge(), and this way actually moves merging before plugging just like what
blk_queue_bio()does, then the performance regression is fixed.
This looks generally reasonable, but can you split the move of blk_mq_attempt_merge into a separate patch (or just skip it for now)? This clutters up the diff a lot and makes it much harder to read.
block/blk-mq-sched.c bool blk_mq_sched_try_merge(struct request_queue *q, struct bio *bio, · line 278
Quoted 11 lines
bool
__blk_mq_sched_bio_merge(struct request_queue *q, struct bio *bio){block/blk-mq-sched.c +5
-0struct elevator_queue *e = q->elevator; +struct blk_mq_ctx *ctx =blk_mq_get_ctx (q); +struct blk_mq_hw_ctx *hctx =blk_mq_map_queue (q, ctx->cpu); +bool ret = false; +if (e && e->type->ops.mq.bio_merge) {blk_mq_put_ctx (ctx);return e->type->ops.mq.bio_merge (hctx, bio); + }else if (hctx->flags &BLK_MQ_F_SHOULD_MERGE ) {
No need for the relse here given the return. Also both mq-deadline and cfq don't need the hctx at all and just the queue, so we could even skip it for that case.
if (e && e->type->ops.mq.bio_merge)
return e->type->ops.mq.bio_merge(q, bio);
ctx = blk_mq_get_ctx(q);
hctx = blk_mq_map_queue(q, ctx->cpu);
if (hctx->flags & BLK_MQ_F_SHOULD_MERGE) {
...
}(and we only need the hctx for the flags, sigh..)
Quoted 13 lines · Christoph Hellwig
Quoted 11 lines · Ming Lei
Before blk-mq is introduced, I/O is merged before putting into plug queue, but blk-mq changed the order and makes merging basically impossible until mq-deadline is introduced. Then it is observed that throughput of sequential I/O is degraded about 10%~20% on virtio-blk in the test[1] if IO schedluer isn't used.
This patch provides a default per-sw-queue bio merging if there isn't scheduler enabled or the scheduler hasn't implement .bio_merge(), and this way actually moves merging before plugging just like what
blk_queue_bio()does, then the performance regression is fixed.
Quoted 3 lines
This looks generally reasonable, but can you split the move of
blk_mq_attempt_mergeinto a separate patch (or just skip it for now)? This clutters up the diff a lot and makes it much harder to read.
OK, will do it in v2.
Quoted 17 lines
block/blk-mq-sched.c bool blk_mq_sched_try_merge(struct request_queue *q, struct bio *bio, · line 278
Quoted 12 lines
bool
__blk_mq_sched_bio_merge(struct request_queue *q, struct bio *bio){block/blk-mq-sched.c +5
-0struct elevator_queue *e = q->elevator; +struct blk_mq_ctx *ctx =blk_mq_get_ctx (q); +struct blk_mq_hw_ctx *hctx =blk_mq_map_queue (q, ctx->cpu); +bool ret = false; +if (e && e->type->ops.mq.bio_merge) {blk_mq_put_ctx (ctx);return e->type->ops.mq.bio_merge (hctx, bio); + }else if (hctx->flags &BLK_MQ_F_SHOULD_MERGE ) {No need for the relse here given the return. Also both mq-deadline and cfq don't need the hctx at all and just the queue, so we could even skip it for that case.
Quoted 2 lines
if (e && e->type->ops.mq.bio_merge) return e->type->ops.mq.bio_merge(q, bio);
That need to change current elevator interface(pass q instead of hctx), so I think better to not do that in this patch.
Quoted 8 lines
ctx = blk_mq_get_ctx(q); hctx = blk_mq_map_queue(q, ctx->cpu); if (hctx->flags & BLK_MQ_F_SHOULD_MERGE) { ... }(and we only need the hctx for the flags, sigh..)
The check may need to be removed in the future, since every blk-mq driver sets BLK_MQ_F_SHOULD_MERGE now.
Thanks,
Ming