linux-block.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] block: fail unaligned bio from submit_bio_noacct()
@ 2024-03-21 13:16 Ming Lei
  2024-03-21 15:14 ` Bart Van Assche
                   ` (3 more replies)
  0 siblings, 4 replies; 16+ messages in thread
From: Ming Lei @ 2024-03-21 13:16 UTC (permalink / raw)
  To: Jens Axboe, linux-block; +Cc: Ming Lei, Mikulas Patocka, Mike Snitzer

For any bio with data, its start sector and size have to be aligned with
the queue's logical block size.

This rule is obvious, but there is still user which may send unaligned
bio to block layer, and it is observed that dm-integrity can do that,
and cause double free of driver's dma meta buffer.

So failfast unaligned bio from submit_bio_noacct() for avoiding more
troubles.

Cc: Mikulas Patocka <mpatocka@redhat.com>
Cc: Mike Snitzer <snitzer@kernel.org>
Signed-off-by: Ming Lei <ming.lei@redhat.com>
---
 block/blk-core.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

diff --git a/block/blk-core.c b/block/blk-core.c
index a16b5abdbbf5..b1a10187ef74 100644
--- a/block/blk-core.c
+++ b/block/blk-core.c
@@ -729,6 +729,20 @@ void submit_bio_noacct_nocheck(struct bio *bio)
 		__submit_bio_noacct(bio);
 }
 
+static bool bio_check_alignment(struct bio *bio, struct request_queue *q)
+{
+	unsigned int bs = q->limits.logical_block_size;
+	unsigned int size = bio->bi_iter.bi_size;
+
+	if (size & (bs - 1))
+		return false;
+
+	if (size && ((bio->bi_iter.bi_sector << SECTOR_SHIFT) & (bs - 1)))
+		return false;
+
+	return true;
+}
+
 /**
  * submit_bio_noacct - re-submit a bio to the block device layer for I/O
  * @bio:  The bio describing the location in memory and on the device.
@@ -780,6 +794,9 @@ void submit_bio_noacct(struct bio *bio)
 		}
 	}
 
+	if (WARN_ON_ONCE(!bio_check_alignment(bio, q)))
+		goto end_io;
+
 	if (!test_bit(QUEUE_FLAG_POLL, &q->queue_flags))
 		bio_clear_polled(bio);
 
-- 
2.41.0


^ permalink raw reply related	[flat|nested] 16+ messages in thread

end of thread, other threads:[~2024-03-24  8:03 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-03-21 13:16 [PATCH] block: fail unaligned bio from submit_bio_noacct() Ming Lei
2024-03-21 15:14 ` Bart Van Assche
2024-03-21 15:18   ` Ming Lei
2024-03-21 15:43 ` Mike Snitzer
2024-03-21 17:01   ` Mikulas Patocka
2024-03-21 22:07     ` Christoph Hellwig
2024-03-22  2:08     ` Ming Lei
2024-03-22  2:39       ` Keith Busch
2024-03-24  8:02         ` Ming Lei
2024-03-22 10:16       ` Mikulas Patocka
2024-03-21 17:09 ` [PATCH] " Jens Axboe
2024-03-21 22:09   ` Christoph Hellwig
2024-03-21 22:50     ` Jens Axboe
2024-03-22  0:31       ` Christoph Hellwig
2024-03-22  1:21   ` Ming Lei
2024-03-21 22:06 ` Christoph Hellwig

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).