Skip to contentSkip to conversation

block: prevent merges of discard and write requests

PATCH In review Adrian Hunter 5 participants 8 messages Sep 25, 2010 – Sep 27, 2010 Block layer

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 block: prevent merges of discard and write requests

Add logic to prevent two I/O requests being merged if only one of them is a discard. Ditto secure discard.

Without this fix, it is possible for write requests to transform into discard requests. For example:

Submit bio 1 to discard 8 sectors from sector n Submit bio 2 to write 8 sectors from sector n + 16 Submit bio 3 to write 8 sectors from sector n + 8

Bio 1 becomes request 1. Bio 2 becomes request 2. Bio 3 is merged with request 2, and then subsequently request 2 is merged with request 1 resulting in just one I/O request which discards all 24 sectors.

---
 block/blk-merge.c |   12 ++++++++++++
 1 files changed, 12 insertions(+), 0 deletions(-)
block/blk-merge.c +12 -0
index 6a72546..3d88c1f 100644
@@ -375,6 +375,18 @@ static int attempt_merge(struct request_queue *q, struct request *req,
 	if (blk_rq_pos(req) + blk_rq_sectors(req) != blk_rq_pos(next))
 		return 0;
 
+	/*
+	 * Don't merge file system requests and discard requests
+	 */
+	if ((req->cmd_flags & REQ_DISCARD) != (next->cmd_flags & REQ_DISCARD))
+		return 0;
+
+	/*
+	 * Don't merge discard requests and secure discard requests
+	 */
+	if ((req->cmd_flags & REQ_SECURE) != (next->cmd_flags & REQ_SECURE))
+		return 0;
+
 	if (rq_data_dir(req) != rq_data_dir(next)
 	    || req->rq_disk != next->rq_disk
 	    || next->special)
Signature · 1 line
1.7.0.4
maintainer
#1 lore raw
Quoted 6 lines · Adrian Hunter

Add logic to prevent two I/O requests being merged if only one of them is a discard. Ditto secure discard.

Without this fix, it is possible for write requests to transform into discard requests. For example:

Quoted 8 lines

Submit bio 1 to discard 8 sectors from sector n Submit bio 2 to write 8 sectors from sector n + 16 Submit bio 3 to write 8 sectors from sector n + 8

Bio 1 becomes request 1. Bio 2 becomes request 2. Bio 3 is merged with request 2, and then subsequently request 2 is merged with request 1 resulting in just one I/O request which discards all 24 sectors.

Wow, that's a disaster. We can now have requests in the same direction and of the same type (fs), but not mergeable.

I would move the check up above the position calculations. I will apply this and upstream it right away. Thanks a lot!

Signature · 1 line
Jens Axboe
#2 lore raw
Quoted 16 lines · Jens Axboe
Quoted 6 lines · Adrian Hunter

Add logic to prevent two I/O requests being merged if only one of them is a discard. Ditto secure discard.

Without this fix, it is possible for write requests to transform into discard requests. For example:

Quoted 8 lines

Submit bio 1 to discard 8 sectors from sector n Submit bio 2 to write 8 sectors from sector n + 16 Submit bio 3 to write 8 sectors from sector n + 8

Bio 1 becomes request 1. Bio 2 becomes request 2. Bio 3 is merged with request 2, and then subsequently request 2 is merged with request 1 resulting in just one I/O request which discards all 24 sectors.

Quoted 6 lines

Wow, that's a disaster. We can now have requests in the same direction and of the same type (fs), but not mergeable.

I would move the check up above the position calculations. I will apply this and upstream it right away. Thanks a lot!

Jens, is this (the REQ_DISCARD hunk) required for stable as well? It appears there's not much change relating to merging requests between HEAD and v2.6.35, so I assume it is?

--Kyle

maintainer
#3 lore raw
Quoted 20 lines · Kyle McMartin
Quoted 19 lines · Jens Axboe
Quoted 6 lines · Adrian Hunter

Add logic to prevent two I/O requests being merged if only one of them is a discard. Ditto secure discard.

Without this fix, it is possible for write requests to transform into discard requests. For example:

Quoted 8 lines

Submit bio 1 to discard 8 sectors from sector n Submit bio 2 to write 8 sectors from sector n + 16 Submit bio 3 to write 8 sectors from sector n + 8

Bio 1 becomes request 1. Bio 2 becomes request 2. Bio 3 is merged with request 2, and then subsequently request 2 is merged with request 1 resulting in just one I/O request which discards all 24 sectors.

Wow, that's a disaster. We can now have requests in the same direction and of the same type (fs), but not mergeable.

Quoted 7 lines
Quoted 3 lines

I would move the check up above the position calculations. I will apply this and upstream it right away. Thanks a lot!

Jens, is this (the REQ_DISCARD hunk) required for stable as well? It appears there's not much change relating to merging requests between HEAD and v2.6.35, so I assume it is?

No, 2.6.35 and earlier is safe, it's only 2.6.36-rc that is affected by this bug.

Signature · 1 line
Jens Axboe
#4 lore raw
Quoted 7 lines · Jens Axboe
Quoted 3 lines

Jens, is this (the REQ_DISCARD hunk) required for stable as well? It appears there's not much change relating to merging requests between HEAD and v2.6.35, so I assume it is?

No, 2.6.35 and earlier is safe, it's only 2.6.36-rc that is affected by this bug.

Groovy! Thanks for the quick reply.

--Kyle

#5 lore raw
In reply to Jens Axboe
Quoted 25 lines · Jens Axboe
Quoted 24 lines · Kyle McMartin
Quoted 22 lines · Jens Axboe
Quoted 6 lines · Adrian Hunter

Add logic to prevent two I/O requests being merged if only one of them is a discard.  Ditto secure discard.

Without this fix, it is possible for write requests to transform into discard requests.  For example:

Quoted 8 lines

  Submit bio 1 to discard 8 sectors from sector n Submit bio 2 to write 8 sectors from sector n + 16 Submit bio 3 to write 8 sectors from sector n + 8

Bio 1 becomes request 1.  Bio 2 becomes request 2. Bio 3 is merged with request 2, and then subsequently request 2 is merged with request 1 resulting in just one I/O request which discards all 24 sectors.

Wow, that's a disaster. We can now have requests in the same direction and of the same type (fs), but not mergeable.

I would move the check up above the position calculations. I will apply this and upstream it right away. Thanks a lot!

Quoted 6 lines
Quoted 3 lines

Jens, is this (the REQ_DISCARD hunk) required for stable as well? It appears there's not much change relating to merging requests between HEAD and v2.6.35, so I assume it is?

No, 2.6.35 and earlier is safe, it's only 2.6.36-rc that is affected by this bug.

I'm not so sure... I think 2.6.35 is affected. Jens, what do you hold to be the regression point?

The initial discard merge implementation (commit e17fc0a1ccf88) always set REQ_SOFTBARRIER for discards. As such such non-ioctl blkdev_issue_discard() discards were always !rq_mergeable().

As commit e17fc0a1ccf88 shared: it is always possible to send a discard without the use of a barrier (but such callers were assumed to be safe, e.g. mkfs.* discarding entire device via ioctl?).

But things really broke once we started playing games with barrier flags associated with discards. The regression point (relative to discard merging) seems to have occurred when we got away from using REQ_SOFTBARRIER with commit: fbbf055692aeb "block: fix DISCARD_BARRIER requests". Which was still committed to v2.6.35...

So long story short: it seems stable's 2.6.35.y needs this fix too.

Of course I could easily be missing something.. if I got any of this wrong please correct me ;)

Thanks,
Mike

#6 lore raw
Quoted 35 lines · Mike Snitzer
Quoted 25 lines · Jens Axboe
Quoted 24 lines · Kyle McMartin
Quoted 22 lines · Jens Axboe
Quoted 6 lines · Adrian Hunter

Add logic to prevent two I/O requests being merged if only one of them is a discard.  Ditto secure discard.

Without this fix, it is possible for write requests to transform into discard requests.  For example:

Quoted 8 lines

  Submit bio 1 to discard 8 sectors from sector n Submit bio 2 to write 8 sectors from sector n + 16 Submit bio 3 to write 8 sectors from sector n + 8

Bio 1 becomes request 1.  Bio 2 becomes request 2. Bio 3 is merged with request 2, and then subsequently request 2 is merged with request 1 resulting in just one I/O request which discards all 24 sectors.

Wow, that's a disaster. We can now have requests in the same direction and of the same type (fs), but not mergeable.

I would move the check up above the position calculations. I will apply this and upstream it right away. Thanks a lot!

Quoted 6 lines
Quoted 3 lines

Jens, is this (the REQ_DISCARD hunk) required for stable as well? It appears there's not much change relating to merging requests between HEAD and v2.6.35, so I assume it is?

No, 2.6.35 and earlier is safe, it's only 2.6.36-rc that is affected by this bug.

I'm not so sure... I think 2.6.35 is affected.  Jens, what do you hold to be the regression point?

...

Quoted 5 lines

But things really broke once we started playing games with barrier flags associated with discards.  The regression point (relative to discard merging) seems to have occurred when we got away from using REQ_SOFTBARRIER with commit: fbbf055692aeb "block: fix DISCARD_BARRIER requests".  Which was still committed to v2.6.35...

OK I take that back, with commit fbbf055692aeb REQ_HARDBARRIER is used for discards.. which is equally applicable to !rq_mergeable().

Anyway, I'd still like to understand what you feel is the regression point.

Mike

maintainer
#7 lore raw
Quoted 43 lines · Mike Snitzer
Quoted 35 lines · Mike Snitzer
Quoted 25 lines · Jens Axboe
Quoted 24 lines · Kyle McMartin
Quoted 22 lines · Jens Axboe
Quoted 6 lines · Adrian Hunter

Add logic to prevent two I/O requests being merged if only one of them is a discard. Ditto secure discard.

Without this fix, it is possible for write requests to transform into discard requests. For example:

Quoted 8 lines

Submit bio 1 to discard 8 sectors from sector n Submit bio 2 to write 8 sectors from sector n + 16 Submit bio 3 to write 8 sectors from sector n + 8

Bio 1 becomes request 1. Bio 2 becomes request 2. Bio 3 is merged with request 2, and then subsequently request 2 is merged with request 1 resulting in just one I/O request which discards all 24 sectors.

Wow, that's a disaster. We can now have requests in the same direction and of the same type (fs), but not mergeable.

I would move the check up above the position calculations. I will apply this and upstream it right away. Thanks a lot!

Quoted 6 lines
Quoted 3 lines

Jens, is this (the REQ_DISCARD hunk) required for stable as well? It appears there's not much change relating to merging requests between HEAD and v2.6.35, so I assume it is?

No, 2.6.35 and earlier is safe, it's only 2.6.36-rc that is affected by this bug.

I'm not so sure... I think 2.6.35 is affected. Jens, what do you hold to be the regression point?

...

Quoted 5 lines

But things really broke once we started playing games with barrier flags associated with discards. The regression point (relative to discard merging) seems to have occurred when we got away from using REQ_SOFTBARRIER with commit: fbbf055692aeb "block: fix DISCARD_BARRIER requests". Which was still committed to v2.6.35...

Quoted 4 lines

OK I take that back, with commit fbbf055692aeb REQ_HARDBARRIER is used for discards.. which is equally applicable to !rq_mergeable().

Anyway, I'd still like to understand what you feel is the regression point.

Looking at the end result, 2.6.35 does look like it's affected for request-to-request merges, if those were submitted outside of blkdev_issue_discard() (all in-kernel users have BARRIER set). But we can't rule out of-of-tree drivers, so I suppose we should submit the patch to stable just to be on the safe side.

Thankfully it is a very rare condition. Request-to-request merges are fairly uncommon, and the chance of an unrelated merge (most users do waiting issues) is small. So it's affected in the same sense that .36-rc is, I initially thought the problem was worse there.

Signature · 1 line
Jens Axboe