Skip to content

Commit 6984046

Browse files
Jianchao Wangaxboe
authored andcommitted
block: fix the DISCARD request merge
There are two cases when handle DISCARD merge. If max_discard_segments == 1, the bios/requests need to be contiguous to merge. If max_discard_segments > 1, it takes every bio as a range and different range needn't to be contiguous. But now, attempt_merge screws this up. It always consider contiguity for DISCARD for the case max_discard_segments > 1 and cannot merge contiguous DISCARD for the case max_discard_segments == 1, because rq_attempt_discard_merge always returns false in this case. This patch fixes both of the two cases above. Reviewed-by: Christoph Hellwig <hch@lst.de> Reviewed-by: Ming Lei <ming.lei@redhat.com> Signed-off-by: Jianchao Wang <jianchao.w.wang@oracle.com> Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent a435ab4 commit 6984046

File tree

1 file changed

+36
-10
lines changed

1 file changed

+36
-10
lines changed

block/blk-merge.c

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,31 @@ static void blk_account_io_merge(struct request *req)
714714
part_stat_unlock();
715715
}
716716
}
717+
/*
718+
* Two cases of handling DISCARD merge:
719+
* If max_discard_segments > 1, the driver takes every bio
720+
* as a range and send them to controller together. The ranges
721+
* needn't to be contiguous.
722+
* Otherwise, the bios/requests will be handled as same as
723+
* others which should be contiguous.
724+
*/
725+
static inline bool blk_discard_mergable(struct request *req)
726+
{
727+
if (req_op(req) == REQ_OP_DISCARD &&
728+
queue_max_discard_segments(req->q) > 1)
729+
return true;
730+
return false;
731+
}
732+
733+
enum elv_merge blk_try_req_merge(struct request *req, struct request *next)
734+
{
735+
if (blk_discard_mergable(req))
736+
return ELEVATOR_DISCARD_MERGE;
737+
else if (blk_rq_pos(req) + blk_rq_sectors(req) == blk_rq_pos(next))
738+
return ELEVATOR_BACK_MERGE;
739+
740+
return ELEVATOR_NO_MERGE;
741+
}
717742

718743
/*
719744
* For non-mq, this has to be called with the request spinlock acquired.
@@ -731,12 +756,6 @@ static struct request *attempt_merge(struct request_queue *q,
731756
if (req_op(req) != req_op(next))
732757
return NULL;
733758

734-
/*
735-
* not contiguous
736-
*/
737-
if (blk_rq_pos(req) + blk_rq_sectors(req) != blk_rq_pos(next))
738-
return NULL;
739-
740759
if (rq_data_dir(req) != rq_data_dir(next)
741760
|| req->rq_disk != next->rq_disk
742761
|| req_no_special_merge(next))
@@ -760,11 +779,19 @@ static struct request *attempt_merge(struct request_queue *q,
760779
* counts here. Handle DISCARDs separately, as they
761780
* have separate settings.
762781
*/
763-
if (req_op(req) == REQ_OP_DISCARD) {
782+
783+
switch (blk_try_req_merge(req, next)) {
784+
case ELEVATOR_DISCARD_MERGE:
764785
if (!req_attempt_discard_merge(q, req, next))
765786
return NULL;
766-
} else if (!ll_merge_requests_fn(q, req, next))
787+
break;
788+
case ELEVATOR_BACK_MERGE:
789+
if (!ll_merge_requests_fn(q, req, next))
790+
return NULL;
791+
break;
792+
default:
767793
return NULL;
794+
}
768795

769796
/*
770797
* If failfast settings disagree or any of the two is already
@@ -888,8 +915,7 @@ bool blk_rq_merge_ok(struct request *rq, struct bio *bio)
888915

889916
enum elv_merge blk_try_merge(struct request *rq, struct bio *bio)
890917
{
891-
if (req_op(rq) == REQ_OP_DISCARD &&
892-
queue_max_discard_segments(rq->q) > 1)
918+
if (blk_discard_mergable(rq))
893919
return ELEVATOR_DISCARD_MERGE;
894920
else if (blk_rq_pos(rq) + blk_rq_sectors(rq) == bio->bi_iter.bi_sector)
895921
return ELEVATOR_BACK_MERGE;

0 commit comments

Comments
 (0)