block: Discard merging fixes
Subsystems (3)
v2 cover block: Discard merging fixes
This series fixes some incorrect accounting from merging discard requests.
The first patch fixes merging two discard requests by having a special case for merging these using the unique constraints discards have.
The second fixes merging a new discard bio into an existing request. Previously all the schedulers defaulted to a front merge, so the patch just checks merge type and adds a 'case' for handling discard in each of the callers.
I've only been able to test with mq-deadline on NVMe as that is the only driver currently supporting multiple discard ranges in a single command.
v1 -> v2:
Fixed patch 1 based on feedback from Jens, tested with recreate sequence.
Added 2nd patch for merging discard bios into existing requests.
Keith Busch (2):
block: Merge discard requests as a special case
block: Handle merging discards in IO schedulers
block/bfq-iosched.c | 2 +- block/blk-core.c | 4 ++++ block/blk-merge.c | 29 ++++++++++++++++++++++++++++- block/blk-mq-sched.c | 2 ++ block/cfq-iosched.c | 2 +- block/deadline-iosched.c | 4 +++- block/elevator.c | 2 +- block/mq-deadline.c | 2 +- 8 files changed, 41 insertions(+), 6 deletions(-)
Signature · 1 line
2.14.3
PATCH v2 1/2 block: Merge discard requests as a special case
Discard requests operate under different constraints than other operations and have different rules for merging. This patch will handle such requests as a special case, using the same criteria and segment accounting used for merging a discard bio into a reqseut.
--- block/blk-merge.c | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-)
block/blk-merge.c +28 -1
@@ -550,6 +550,25 @@ static bool req_no_special_merge(struct request *req)return !q->mq_ops && req->special; } +static int req_attempt_discard_merge (struct request_queue *q,struct request *req, +struct request *next) +{ +unsigned short segments =blk_rq_nr_discard_segments (req); + +if (segments >=queue_max_discard_segments (q)) + goto no_merge; +if (blk_rq_sectors (req) +bio_sectors (next->bio) > +blk_rq_get_max_sectors (req,blk_rq_pos (req))) + goto no_merge; + + req->nr_phys_segments = segments +blk_rq_nr_discard_segments (next); +return 1 ; + +no_merge: +req_set_nomerge (q, req); +return 0 ; +} +static int ll_merge_requests_fn (struct request_queue *q,struct request *req,struct request *next) {
@@ -564,6 +583,13 @@ static int ll_merge_requests_fn(struct request_queue *q, struct request *req,if (req_no_special_merge (req) ||req_no_special_merge (next))return 0 ; +/* +* Merging discard requests use different constraints than other +* operations. +*/ +if (req_op (req) ==REQ_OP_DISCARD ) +return req_attempt_discard_merge (q, req, next); +if (req_gap_back_merge (req, next->bio))return 0 ;
@@ -715,7 +741,8 @@ static struct request *attempt_merge(struct request_queue *q, req->__data_len +=blk_rq_bytes (next); -elv_merge_requests (q, req, next); +if (req_op (req) !=REQ_OP_DISCARD ) +elv_merge_requests (q, req, next);/* * 'next' is going away, so update stats accordingly
Signature · 1 line
2.14.3
Jens Axboe
maintainerRe: [PATCHv2 1/2] block: Merge discard requests as a special case
Quoted 4 lines · Keith Busch
Discard requests operate under different constraints than other operations and have different rules for merging. This patch will handle such requests as a special case, using the same criteria and segment accounting used for merging a discard bio into a reqseut.
I already fixed this one up.
Signature · 1 line
Jens Axboe
PATCH v2 2/2 block: Handle merging discards in IO schedulers
In reply to Keith BuschThis adds support for merging discard requests in all IO schedulers.
--- block/bfq-iosched.c | 2 +- block/blk-core.c | 4 ++++ block/blk-mq-sched.c | 2 ++ block/cfq-iosched.c | 2 +- block/deadline-iosched.c | 4 +++- block/elevator.c | 2 +- block/mq-deadline.c | 2 +- 7 files changed, 13 insertions(+), 5 deletions(-)
block/bfq-iosched.c +1 -1
@@ -1839,7 +1839,7 @@ static int bfq_request_merge(struct request_queue *q, struct request **req, __rq =bfq_find_rq_fmerge (bfqd, bio, q);if (__rq &&elv_bio_merge_ok (__rq, bio)) { *req = __rq; -return ELEVATOR_FRONT_MERGE ; +return blk_try_merge (__rq, bio); }return ELEVATOR_NO_MERGE ;
block/blk-core.c +4 -0
@@ -1952,6 +1952,10 @@ static blk_qc_t blk_queue_bio(struct request_queue *q, struct bio *bio)else elv_merged_request (q, req,ELEVATOR_FRONT_MERGE ); goto out_unlock; +case ELEVATOR_DISCARD_MERGE : +if (!bio_attempt_discard_merge (q, req, bio)) +break ; + goto out_unlock;default :break ; }
block/blk-mq-sched.c +2 -0
@@ -259,6 +259,8 @@ bool blk_mq_sched_try_merge(struct request_queue *q, struct bio *bio,if (!*merged_request)elv_merged_request (q, rq,ELEVATOR_FRONT_MERGE );return true; +case ELEVATOR_DISCARD_MERGE : +return bio_attempt_discard_merge (q, rq, bio);default :return false; }
block/cfq-iosched.c +1 -1
@@ -2518,7 +2518,7 @@ static enum elv_merge cfq_merge(struct request_queue *q, struct request **req, __rq =cfq_find_rq_fmerge (cfqd, bio);if (__rq &&elv_bio_merge_ok (__rq, bio)) { *req = __rq; -return ELEVATOR_FRONT_MERGE ; +return blk_try_merge (__rq, bio); }return ELEVATOR_NO_MERGE ;
block/deadline-iosched.c +3 -1
@@ -14,6 +14,8 @@#include <linux/compiler.h> #include <linux/rbtree.h> +#include "blk.h" +/* * See Documentation/block/deadline-iosched.txt */
@@ -142,7 +144,7 @@ deadline_merge(struct request_queue *q, struct request **req, struct bio *bio)if (elv_bio_merge_ok (__rq, bio)) { *req = __rq; -return ELEVATOR_FRONT_MERGE ; +return blk_try_merge (__rq, bio); } } }
block/elevator.c +1 -1
@@ -484,7 +484,7 @@ enum elv_merge elv_merge(struct request_queue *q, struct request **req, __rq =elv_rqhash_find (q, bio->bi_iter.bi_sector);if (__rq &&elv_bio_merge_ok (__rq, bio)) { *req = __rq; -return ELEVATOR_BACK_MERGE ; +return blk_try_merge (__rq, bio); }if (e->uses_mq && e->type->ops.mq.request_merge)
block/mq-deadline.c +1 -1
@@ -451,7 +451,7 @@ static int dd_request_merge(struct request_queue *q, struct request **rq,if (elv_bio_merge_ok (__rq, bio)) { *rq = __rq; -return ELEVATOR_FRONT_MERGE ; +return blk_try_merge (__rq, bio); } }
Signature · 1 line
2.14.3
Jens Axboe
maintainerRe: [PATCHv2 2/2] block: Handle merging discards in IO schedulers
Quoted 1 line · Keith Busch
This adds support for merging discard requests in all IO schedulers.
As per my last email, some of these aren't correct. Only the blk-mq-sched change is correct/useful, so we should just do that separately.
Signature · 1 line
Jens Axboe
Re: [PATCHv2 2/2] block: Handle merging discards in IO schedulers
Quoted 6 lines · Jens Axboe
Quoted 1 line · Keith Busch
This adds support for merging discard requests in all IO schedulers.
As per my last email, some of these aren't correct. Only the blk-mq-sched change is correct/useful, so we should just do that separately.
Sorry, I got ahead of myself with this one.