diff mbox

block: Fix front merge check

Message ID 1468387413-2754-1-git-send-email-damien.lemoal@hgst.com (mailing list archive)
State New, archived
Headers show

Commit Message

Damien Le Moal July 13, 2016, 5:23 a.m. UTC
For a front merge, the maximum number of sectors of the
request must be checked against the front merge BIO sector,
not the current sector of the request.

Signed-off-by: Damien Le Moal <damien.lemoal@hgst.com>
---
 block/blk-merge.c      | 6 +++---
 include/linux/blkdev.h | 5 +++--
 2 files changed, 6 insertions(+), 5 deletions(-)

Comments

Hannes Reinecke July 13, 2016, 5:42 a.m. UTC | #1
On 07/13/2016 07:23 AM, Damien Le Moal wrote:
> For a front merge, the maximum number of sectors of the
> request must be checked against the front merge BIO sector,
> not the current sector of the request.
> 
> Signed-off-by: Damien Le Moal <damien.lemoal@hgst.com>
> ---
>  block/blk-merge.c      | 6 +++---
>  include/linux/blkdev.h | 5 +++--
>  2 files changed, 6 insertions(+), 5 deletions(-)
> 
Reviewed-by: Hannes Reinecke <hare@suse.com>

Cheers,

Hannes
Jens Axboe July 13, 2016, 4:19 p.m. UTC | #2
On 07/12/2016 10:23 PM, Damien Le Moal wrote:
> For a front merge, the maximum number of sectors of the
> request must be checked against the front merge BIO sector,
> not the current sector of the request.

Why does this matter? The merging should only happen before we start the 
request, hence rq pos and first bio should be one and the same.
Damien Le Moal July 14, 2016, 1:23 a.m. UTC | #3
Jens,

On 7/14/16 01:19, Jens Axboe wrote:
> On 07/12/2016 10:23 PM, Damien Le Moal wrote:
>> For a front merge, the maximum number of sectors of the
>> request must be checked against the front merge BIO sector,
>> not the current sector of the request.
>
> Why does this matter? The merging should only happen before we start the
> request, hence rq pos and first bio should be one and the same.

The block device of SMR drives is set up as chunked with the chunk size 
equal to the drive zone size. Since write requests directed to 
sequential zones cannot cross zone boundaries, both the front merge code 
and the back merge code must ensure that requests resulting from a merge 
do not cross chunk boundaries.

The back merge code does this well, but the front merge code fails to 
prevent merging when the BIO is the last write in a zone and the request 
is the first write in the following (empty) zone. The check against the 
request LBA does not prevent the merge as the request+bio size fit in 
the empty zone. The check must be against the BIO LBA to detect the 
chunk boundary crossing.

Best regards.
Jens Axboe July 21, 2016, 3:38 a.m. UTC | #4
On 07/13/2016 07:23 PM, Damien Le Moal wrote:
>
> Jens,
>
> On 7/14/16 01:19, Jens Axboe wrote:
>> On 07/12/2016 10:23 PM, Damien Le Moal wrote:
>>> For a front merge, the maximum number of sectors of the
>>> request must be checked against the front merge BIO sector,
>>> not the current sector of the request.
>>
>> Why does this matter? The merging should only happen before we start the
>> request, hence rq pos and first bio should be one and the same.
>
> The block device of SMR drives is set up as chunked with the chunk size
> equal to the drive zone size. Since write requests directed to
> sequential zones cannot cross zone boundaries, both the front merge code
> and the back merge code must ensure that requests resulting from a merge
> do not cross chunk boundaries.
>
> The back merge code does this well, but the front merge code fails to
> prevent merging when the BIO is the last write in a zone and the request
> is the first write in the following (empty) zone. The check against the
> request LBA does not prevent the merge as the request+bio size fit in
> the empty zone. The check must be against the BIO LBA to detect the
> chunk boundary crossing.

Ah that makes sense, it's on adding the new bio, not against the 
existing rq->bio. Applied for 4.8.
diff mbox

Patch

diff --git a/block/blk-merge.c b/block/blk-merge.c
index 2613531..b736569 100644
--- a/block/blk-merge.c
+++ b/block/blk-merge.c
@@ -500,7 +500,7 @@  int ll_back_merge_fn(struct request_queue *q, struct request *req,
 	    integrity_req_gap_back_merge(req, bio))
 		return 0;
 	if (blk_rq_sectors(req) + bio_sectors(bio) >
-	    blk_rq_get_max_sectors(req)) {
+	    blk_rq_get_max_sectors(req, blk_rq_pos(req))) {
 		req->cmd_flags |= REQ_NOMERGE;
 		if (req == q->last_merge)
 			q->last_merge = NULL;
@@ -524,7 +524,7 @@  int ll_front_merge_fn(struct request_queue *q, struct request *req,
 	    integrity_req_gap_front_merge(req, bio))
 		return 0;
 	if (blk_rq_sectors(req) + bio_sectors(bio) >
-	    blk_rq_get_max_sectors(req)) {
+	    blk_rq_get_max_sectors(req, bio->bi_iter.bi_sector)) {
 		req->cmd_flags |= REQ_NOMERGE;
 		if (req == q->last_merge)
 			q->last_merge = NULL;
@@ -570,7 +570,7 @@  static int ll_merge_requests_fn(struct request_queue *q, struct request *req,
 	 * Will it become too large?
 	 */
 	if ((blk_rq_sectors(req) + blk_rq_sectors(next)) >
-	    blk_rq_get_max_sectors(req))
+	    blk_rq_get_max_sectors(req, blk_rq_pos(req)))
 		return 0;
 
 	total_phys_segments = req->nr_phys_segments + next->nr_phys_segments;
diff --git a/include/linux/blkdev.h b/include/linux/blkdev.h
index 3d9cf32..b99ef36 100644
--- a/include/linux/blkdev.h
+++ b/include/linux/blkdev.h
@@ -904,7 +904,8 @@  static inline unsigned int blk_max_size_offset(struct request_queue *q,
 			(offset & (q->limits.chunk_sectors - 1));
 }
 
-static inline unsigned int blk_rq_get_max_sectors(struct request *rq)
+static inline unsigned int blk_rq_get_max_sectors(struct request *rq,
+						  sector_t offset)
 {
 	struct request_queue *q = rq->q;
 
@@ -914,7 +915,7 @@  static inline unsigned int blk_rq_get_max_sectors(struct request *rq)
 	if (!q->limits.chunk_sectors || (rq->cmd_flags & REQ_DISCARD))
 		return blk_queue_get_max_sectors(q, rq->cmd_flags);
 
-	return min(blk_max_size_offset(q, blk_rq_pos(rq)),
+	return min(blk_max_size_offset(q, offset),
 			blk_queue_get_max_sectors(q, rq->cmd_flags));
 }