Skip to content

Commit 87aa69a

Browse files
Ming Leigregkh
authored andcommitted
block: return ELEVATOR_DISCARD_MERGE if possible
[ Upstream commit 866663b ] When merging one bio to request, if they are discard IO and the queue supports multi-range discard, we need to return ELEVATOR_DISCARD_MERGE because both block core and related drivers(nvme, virtio-blk) doesn't handle mixed discard io merge(traditional IO merge together with discard merge) well. Fix the issue by returning ELEVATOR_DISCARD_MERGE in this situation, so both blk-mq and drivers just need to handle multi-range discard. Reported-by: Oleksandr Natalenko <oleksandr@natalenko.name> Signed-off-by: Ming Lei <ming.lei@redhat.com> Tested-by: Oleksandr Natalenko <oleksandr@natalenko.name> Fixes: 2705dfb ("block: fix discard request merge") Link: https://lore.kernel.org/r/20210729034226.1591070-1-ming.lei@redhat.com Signed-off-by: Jens Axboe <axboe@kernel.dk> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 3868507 commit 87aa69a

5 files changed

Lines changed: 24 additions & 16 deletions

File tree

‎block/bfq-iosched.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2251,6 +2251,9 @@ static int bfq_request_merge(struct request_queue *q, struct request **req,
22512251
__rq = bfq_find_rq_fmerge(bfqd, bio, q);
22522252
if (__rq && elv_bio_merge_ok(__rq, bio)) {
22532253
*req = __rq;
2254+
2255+
if (blk_discard_mergable(__rq))
2256+
return ELEVATOR_DISCARD_MERGE;
22542257
return ELEVATOR_FRONT_MERGE;
22552258
}
22562259

‎block/blk-merge.c‎

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -702,22 +702,6 @@ static void blk_account_io_merge_request(struct request *req)
702702
}
703703
}
704704

705-
/*
706-
* Two cases of handling DISCARD merge:
707-
* If max_discard_segments > 1, the driver takes every bio
708-
* as a range and send them to controller together. The ranges
709-
* needn't to be contiguous.
710-
* Otherwise, the bios/requests will be handled as same as
711-
* others which should be contiguous.
712-
*/
713-
static inline bool blk_discard_mergable(struct request *req)
714-
{
715-
if (req_op(req) == REQ_OP_DISCARD &&
716-
queue_max_discard_segments(req->q) > 1)
717-
return true;
718-
return false;
719-
}
720-
721705
static enum elv_merge blk_try_req_merge(struct request *req,
722706
struct request *next)
723707
{

‎block/elevator.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,9 @@ enum elv_merge elv_merge(struct request_queue *q, struct request **req,
336336
__rq = elv_rqhash_find(q, bio->bi_iter.bi_sector);
337337
if (__rq && elv_bio_merge_ok(__rq, bio)) {
338338
*req = __rq;
339+
340+
if (blk_discard_mergable(__rq))
341+
return ELEVATOR_DISCARD_MERGE;
339342
return ELEVATOR_BACK_MERGE;
340343
}
341344

‎block/mq-deadline.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,8 @@ static int dd_request_merge(struct request_queue *q, struct request **rq,
454454

455455
if (elv_bio_merge_ok(__rq, bio)) {
456456
*rq = __rq;
457+
if (blk_discard_mergable(__rq))
458+
return ELEVATOR_DISCARD_MERGE;
457459
return ELEVATOR_FRONT_MERGE;
458460
}
459461
}

‎include/linux/blkdev.h‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1534,6 +1534,22 @@ static inline int queue_limit_discard_alignment(struct queue_limits *lim, sector
15341534
return offset << SECTOR_SHIFT;
15351535
}
15361536

1537+
/*
1538+
* Two cases of handling DISCARD merge:
1539+
* If max_discard_segments > 1, the driver takes every bio
1540+
* as a range and send them to controller together. The ranges
1541+
* needn't to be contiguous.
1542+
* Otherwise, the bios/requests will be handled as same as
1543+
* others which should be contiguous.
1544+
*/
1545+
static inline bool blk_discard_mergable(struct request *req)
1546+
{
1547+
if (req_op(req) == REQ_OP_DISCARD &&
1548+
queue_max_discard_segments(req->q) > 1)
1549+
return true;
1550+
return false;
1551+
}
1552+
15371553
static inline int bdev_discard_alignment(struct block_device *bdev)
15381554
{
15391555
struct request_queue *q = bdev_get_queue(bdev);

0 commit comments

Comments
 (0)