[PATCH 01/12] block: move direct io alignment check to common

Keith Busch kbusch at fb.com
Thu Jun 30 13:42:01 PDT 2022


From: Keith Busch <kbusch at kernel.org>

All direct io has the same setup and alignment check, so just do it once
in common code.

Signed-off-by: Keith Busch <kbusch at kernel.org>
---
 block/fops.c           | 44 ++++++++++++++----------------------------
 include/linux/blkdev.h |  7 +++++++
 2 files changed, 22 insertions(+), 29 deletions(-)

diff --git a/block/fops.c b/block/fops.c
index 86d3cab9bf93..f37af5924cef 100644
--- a/block/fops.c
+++ b/block/fops.c
@@ -42,28 +42,17 @@ static unsigned int dio_bio_write_op(struct kiocb *iocb)
 	return op;
 }
 
-static bool blkdev_dio_unaligned(struct block_device *bdev, loff_t pos,
-			      struct iov_iter *iter)
-{
-	return pos & (bdev_logical_block_size(bdev) - 1) ||
-		!bdev_iter_is_aligned(bdev, iter);
-}
-
 #define DIO_INLINE_BIO_VECS 4
 
 static ssize_t __blkdev_direct_IO_simple(struct kiocb *iocb,
-		struct iov_iter *iter, unsigned int nr_pages)
+		struct iov_iter *iter, unsigned int nr_pages,
+		struct block_device *bdev, loff_t pos)
 {
-	struct block_device *bdev = iocb->ki_filp->private_data;
 	struct bio_vec inline_vecs[DIO_INLINE_BIO_VECS], *vecs;
-	loff_t pos = iocb->ki_pos;
 	bool should_dirty = false;
 	struct bio bio;
 	ssize_t ret;
 
-	if (blkdev_dio_unaligned(bdev, pos, iter))
-		return -EINVAL;
-
 	if (nr_pages <= DIO_INLINE_BIO_VECS)
 		vecs = inline_vecs;
 	else {
@@ -168,20 +157,15 @@ static void blkdev_bio_end_io(struct bio *bio)
 }
 
 static ssize_t __blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter,
-		unsigned int nr_pages)
+		unsigned int nr_pages, struct block_device *bdev, loff_t pos)
 {
-	struct block_device *bdev = iocb->ki_filp->private_data;
 	struct blk_plug plug;
 	struct blkdev_dio *dio;
 	struct bio *bio;
 	bool is_read = (iov_iter_rw(iter) == READ), is_sync;
 	unsigned int opf = is_read ? REQ_OP_READ : dio_bio_write_op(iocb);
-	loff_t pos = iocb->ki_pos;
 	int ret = 0;
 
-	if (blkdev_dio_unaligned(bdev, pos, iter))
-		return -EINVAL;
-
 	if (iocb->ki_flags & IOCB_ALLOC_CACHE)
 		opf |= REQ_ALLOC_CACHE;
 	bio = bio_alloc_bioset(bdev, nr_pages, opf, GFP_KERNEL,
@@ -292,20 +276,15 @@ static void blkdev_bio_end_io_async(struct bio *bio)
 }
 
 static ssize_t __blkdev_direct_IO_async(struct kiocb *iocb,
-					struct iov_iter *iter,
-					unsigned int nr_pages)
+		struct iov_iter *iter, unsigned int nr_pages,
+		struct block_device *bdev, loff_t pos)
 {
-	struct block_device *bdev = iocb->ki_filp->private_data;
 	bool is_read = iov_iter_rw(iter) == READ;
 	unsigned int opf = is_read ? REQ_OP_READ : dio_bio_write_op(iocb);
 	struct blkdev_dio *dio;
 	struct bio *bio;
-	loff_t pos = iocb->ki_pos;
 	int ret = 0;
 
-	if (blkdev_dio_unaligned(bdev, pos, iter))
-		return -EINVAL;
-
 	if (iocb->ki_flags & IOCB_ALLOC_CACHE)
 		opf |= REQ_ALLOC_CACHE;
 	bio = bio_alloc_bioset(bdev, nr_pages, opf, GFP_KERNEL,
@@ -357,18 +336,25 @@ static ssize_t __blkdev_direct_IO_async(struct kiocb *iocb,
 
 static ssize_t blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
 {
+	struct block_device *bdev = iocb->ki_filp->private_data;
+	loff_t pos = iocb->ki_pos;
 	unsigned int nr_pages;
 
 	if (!iov_iter_count(iter))
 		return 0;
+	if (blkdev_dio_unaligned(bdev, pos, iter))
+		return -EINVAL;
 
 	nr_pages = bio_iov_vecs_to_alloc(iter, BIO_MAX_VECS + 1);
 	if (likely(nr_pages <= BIO_MAX_VECS)) {
 		if (is_sync_kiocb(iocb))
-			return __blkdev_direct_IO_simple(iocb, iter, nr_pages);
-		return __blkdev_direct_IO_async(iocb, iter, nr_pages);
+			return __blkdev_direct_IO_simple(iocb, iter, nr_pages,
+							 bdev, pos);
+		return __blkdev_direct_IO_async(iocb, iter, nr_pages, bdev,
+						pos);
 	}
-	return __blkdev_direct_IO(iocb, iter, bio_max_segs(nr_pages));
+	return __blkdev_direct_IO(iocb, iter, bio_max_segs(nr_pages), bdev,
+				  pos);
 }
 
 static int blkdev_writepage(struct page *page, struct writeback_control *wbc)
diff --git a/include/linux/blkdev.h b/include/linux/blkdev.h
index 22b12531aeb7..9d676adfaaa1 100644
--- a/include/linux/blkdev.h
+++ b/include/linux/blkdev.h
@@ -1352,6 +1352,13 @@ static inline bool bdev_iter_is_aligned(struct block_device *bdev,
 				   bdev_logical_block_size(bdev) - 1);
 }
 
+static inline bool blkdev_dio_unaligned(struct block_device *bdev, loff_t p,
+			      struct iov_iter *iter)
+{
+	return p & (bdev_logical_block_size(bdev) - 1) ||
+		!bdev_iter_is_aligned(bdev, iter);
+}
+
 static inline int blk_rq_aligned(struct request_queue *q, unsigned long addr,
 				 unsigned int len)
 {
-- 
2.30.2




More information about the Linux-nvme mailing list