Skip to contentSkip to conversation

block: fix get_max_io_size()

PATCH In review Keith Busch 3 participants 12 messages Aug 6, 2020 – Aug 18, 2020

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 block: fix get_max_io_size()

A previous commit aligning splits to physical block sizes inadvertently modified one return case such that that it now returns 0 length splits when the number of sectors doesn't exceed the physical offset. This later hits a BUG in bio_split(). Restore the previous working behavior.

Cc Bart Van Assche <[email protected]>
Fixes 9cc5169cd478b ("block: Improve physical block alignment of split bios")
---
 block/blk-merge.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
block/blk-merge.c +1 -1
index 5196dc145270..d7fef954d42f 100644
@@ -154,7 +154,7 @@ static inline unsigned get_max_io_size(struct request_queue *q,
 	if (max_sectors > start_offset)
 		return max_sectors - start_offset;
 
-	return sectors & (lbs - 1);
+	return sectors & ~(lbs - 1);
 }
 
 static inline unsigned get_max_segment_size(const struct request_queue *q,
Signature · 1 line
2.24.1
maintainer
#1 lore raw
Quoted 4 lines · Keith Busch

A previous commit aligning splits to physical block sizes inadvertently modified one return case such that that it now returns 0 length splits when the number of sectors doesn't exceed the physical offset. This later hits a BUG in bio_split(). Restore the previous working behavior.

Yikes! I wonder how that lived so long... Applied.

Signature · 1 line
Jens Axboe
#2 lore raw
In reply to Keith Busch
Quoted 19 lines · Keith Busch

A previous commit aligning splits to physical block sizes inadvertently modified one return case such that that it now returns 0 length splits when the number of sectors doesn't exceed the physical offset. This later hits a BUG in bio_split(). Restore the previous working behavior.

Cc Bart Van Assche <[email protected]>
Fixes 9cc5169cd478b ("block: Improve physical block alignment of split bios")
---
 block/blk-merge.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
block/blk-merge.c +0 -0
index 5196dc145270..d7fef954d42f 100644

block/blk-merge.c static inline unsigned get_max_io_size(struct request_queue *q, · line 158

Quoted 6 lines
block/blk-merge.c +1 -1
@@ -154,7 +154,7 @@ static inline unsigned get_max_io_size(struct request_queue *q,
 	if (max_sectors > start_offset)
 		return max_sectors - start_offset;
 
-	return sectors & (lbs - 1);
+	return sectors & ~(lbs - 1);
 }

I think we agree that get_max_io_size() should never return zero. However, the above change seems wrong to me because it will cause get_max_io_size() to return zero if the logical block size is larger than 512 bytes and if sectors < lbs. How about changing the return statement as follows (untested):

	return max(sectors & (lbs - 1), sectors);

Thanks,

Bart.

#3 lore raw
Quoted 23 lines · Bart Van Assche
Quoted 22 lines · Keith Busch

A previous commit aligning splits to physical block sizes inadvertently modified one return case such that that it now returns 0 length splits when the number of sectors doesn't exceed the physical offset. This later hits a BUG in bio_split(). Restore the previous working behavior.

Cc Bart Van Assche <[email protected]>
Fixes 9cc5169cd478b ("block: Improve physical block alignment of split bios")
---
 block/blk-merge.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
block/blk-merge.c +0 -0
index 5196dc145270..d7fef954d42f 100644
@@ -154,7 +154,7 @@ static inline unsigned get_max_io_size(struct request_queue *q,
 	if (max_sectors > start_offset)
 		return max_sectors - start_offset;
 
Quoted 8 lines

block/blk-merge.c static inline unsigned get_max_io_size(struct request_queue *q, · line 158

Quoted 3 lines
block/blk-merge.c +1 -1
-	return sectors & (lbs - 1);
+	return sectors & ~(lbs - 1);
 }

I think we agree that get_max_io_size() should never return zero. However, the above change seems wrong to me because it will cause get_max_io_size() to return zero if the logical block size is larger than 512 bytes and if sectors < lbs. How about changing the return statement as follows (untested):

This should work better than what was mentioned in my previous email:

- return sectors & (lbs - 1);
+ return sectors;

Thanks,

Bart.

#4 lore raw

Review

Quoted 4 lines · Bart Van Assche

This should work better than what was mentioned in my previous email:

(quoted diff) +1 -1
-	return sectors & (lbs - 1);
+	return sectors;

It used to be something like that. There were some situations where it didn't work, which brought d0e5fbb01a67e, but I think the real problem was from mismatched queue_limits, which I think I addressed with 5f009d3f8e668, so maybe this is okay now.

#5 lore raw
Quoted 10 lines · Keith Busch

Review

Quoted 4 lines · Bart Van Assche

This should work better than what was mentioned in my previous email:

(quoted diff) +1 -1
-	return sectors & (lbs - 1);
+	return sectors;

It used to be something like that. There were some situations where it didn't work, which brought d0e5fbb01a67e, but I think the real problem was from mismatched queue_limits, which I think I addressed with 5f009d3f8e668, so maybe this is okay now.

Hi Keith,

How about replacing your patch with the (untested) patch below?

Thanks,

Bart.

block/blk-merge.c +4 -6
index 5196dc145270..2d10fa3768a3 100644
@@ -135,10 +135,9 @@ static struct bio *blk_bio_write_same_split(struct request_queue *q,
 /*
  * Return the maximum number of sectors from the start of a bio that may be
  * submitted as a single request to a block device. If enough sectors remain,
- * align the end to the physical block size. Otherwise align the end to the
- * logical block size. This approach minimizes the number of non-aligned
- * requests that are submitted to a block device if the start of a bio is not
- * aligned to a physical block boundary.
+ * align the end to the physical block size. This approach minimizes the
+ * number of non-aligned requests that are submitted to a block device if the
+ * start of a bio is not aligned to a physical block boundary.
  */
 static inline unsigned get_max_io_size(struct request_queue *q,
 				       struct bio *bio)
@@ -146,7 +145,6 @@ static inline unsigned get_max_io_size(struct request_queue *q,
 	unsigned sectors = blk_max_size_offset(q, bio->bi_iter.bi_sector);
 	unsigned max_sectors = sectors;
 	unsigned pbs = queue_physical_block_size(q) >> SECTOR_SHIFT;
-	unsigned lbs = queue_logical_block_size(q) >> SECTOR_SHIFT;
 	unsigned start_offset = bio->bi_iter.bi_sector & (pbs - 1);
 
 	max_sectors += start_offset;
@@ -154,7 +152,7 @@ static inline unsigned get_max_io_size(struct request_queue *q,
 	if (max_sectors > start_offset)
 		return max_sectors - start_offset;
 
-	return sectors & (lbs - 1);
+	return sectors;
 }
 
 static inline unsigned get_max_segment_size(const struct request_queue *q,
#7 lore raw
Quoted 11 lines · Keith Busch
Quoted 3 lines · Bart Van Assche

Hi Keith,

How about replacing your patch with the (untested) patch below?

I believe that should be fine, but I broke the kernel last time I did something like that. I still think it was from incorrect queue_limits, but Linus disagreed.

* http://lkml.iu.edu/hypermail/linux/kernel/1601.2/03994.html

Hi Keith,

Thanks for the interesting link. Regarding Linus' comments about bio
splitting: if the last return statement in get_max_io_size() is reached then it is guaranteed that sectors < pbs (physical block size). So I think that Linus' comment applies to the previous return statement instead of to the last ("return max_sectors - start_offset;"). However, I think it is already guaranteed that that value is a multiple of the logical block size because start_offset is a multiple of the logical block size and because of the following statement: "max_sectors &= ~(pbs - 1);".

Bart.

#8 lore raw
Quoted 15 lines · Bart Van Assche
Quoted 11 lines · Keith Busch
Quoted 3 lines · Bart Van Assche

Hi Keith,

How about replacing your patch with the (untested) patch below?

I believe that should be fine, but I broke the kernel last time I did something like that. I still think it was from incorrect queue_limits, but Linus disagreed.

* http://lkml.iu.edu/hypermail/linux/kernel/1601.2/03994.html

Hi Keith,

Quoted 8 lines

Thanks for the interesting link. Regarding Linus' comments about bio
splitting: if the last return statement in get_max_io_size() is reached then it is guaranteed that sectors < pbs (physical block size). So I think that Linus' comment applies to the previous return statement instead of to the last ("return max_sectors - start_offset;"). However, I think it is already guaranteed that that value is a multiple of the logical block size because start_offset is a multiple of the logical block size and because of the following statement: "max_sectors &= ~(pbs - 1);".

This breaks if limits.max_sectors is not a multiple of the queue's logical block size and the physical block size is larger than max_sectors.

#9 lore raw
In reply to Bart Van Assche
Quoted 3 lines · Bart Van Assche

I think we agree that get_max_io_size() should never return zero. However, the above change seems wrong to me because it will cause get_max_io_size() to return zero if the logical block size is larger than 512 bytes and if sectors < lbs.

I'm pretty sure we have more problems if 'sectors' isn't a multiple of the logical block size.

#10 lore raw
In reply to Keith Busch

Hi Jens,

The proposed alternatives continue to break with allowable (however unlikely) queue limits, where this should be safe for any possible settings. I think this should be okay to go as-is.

Quoted 28 lines · Keith Busch

A previous commit aligning splits to physical block sizes inadvertently modified one return case such that that it now returns 0 length splits when the number of sectors doesn't exceed the physical offset. This later hits a BUG in bio_split(). Restore the previous working behavior.

Cc Bart Van Assche <[email protected]>
Fixes 9cc5169cd478b ("block: Improve physical block alignment of split bios")
---
 block/blk-merge.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
block/blk-merge.c +1 -1
index 5196dc145270..d7fef954d42f 100644
@@ -154,7 +154,7 @@ static inline unsigned get_max_io_size(struct request_queue *q,
 	if (max_sectors > start_offset)
 		return max_sectors - start_offset;
 
-	return sectors & (lbs - 1);
+	return sectors & ~(lbs - 1);
 }
 
 static inline unsigned get_max_segment_size(const struct request_queue *q,
maintainer
#11 lore raw
Quoted 5 lines · Keith Busch

Hi Jens,

The proposed alternatives continue to break with allowable (however unlikely) queue limits, where this should be safe for any possible settings. I think this should be okay to go as-is.

OK, let's try this again then. Queued up for 5.9.

Signature · 1 line
Jens Axboe