From: Mengmeng Zhao Implement init_dma_buf_io_ctx and dma_buf_io_tag for ext4, using s_bdev. Inodes that can't do direct I/O without the page cache get no tag, namely DAX, encryption, verity, inline data and data=journal. Refuse dma-buf iterators in the buffered and DAX paths. A short direct write is returned as is instead of falling back to buffered I/O. Signed-off-by: Mengmeng Zhao Signed-off-by: Li Wang Signed-off-by: Anuj Gupta Signed-off-by: Nitesh Shetty --- fs/ext4/ext4.h | 10 ++++++++++ fs/ext4/file.c | 45 ++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 52 insertions(+), 3 deletions(-) diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h index dc2c181337a1..5e622175df97 100644 --- a/fs/ext4/ext4.h +++ b/fs/ext4/ext4.h @@ -4402,6 +4402,16 @@ static inline bool ext4_inode_can_atomic_write(struct inode *inode) ext4_dio_alignment(inode) != 0; } +/* + * Encrypted inodes can do direct I/O, but it may go through the blk-crypto + * fallback, which copies the data through the CPU. + */ +static inline bool ext4_inode_can_dmabuf_io(struct inode *inode) +{ + return !IS_DAX(inode) && !IS_ENCRYPTED(inode) && + ext4_dio_alignment(inode) != 0; +} + extern int ext4_block_write_begin(handle_t *handle, struct folio *folio, loff_t pos, unsigned len, get_block_t *get_block); diff --git a/fs/ext4/file.c b/fs/ext4/file.c index cfb22d0d5728..c35ce9eecd39 100644 --- a/fs/ext4/file.c +++ b/fs/ext4/file.c @@ -30,6 +30,7 @@ #include #include #include +#include #include "ext4.h" #include "ext4_jbd2.h" #include "xattr.h" @@ -66,6 +67,14 @@ static bool ext4_should_use_dio(struct kiocb *iocb, struct iov_iter *iter) return IS_ALIGNED(iocb->ki_pos | iov_iter_alignment(iter), dio_align); } +static ssize_t ext4_buffered_read_iter(struct kiocb *iocb, struct iov_iter *to) +{ + if (iov_iter_is_dmabuf_map(to)) + return -EOPNOTSUPP; + + return generic_file_read_iter(iocb, to); +} + static ssize_t ext4_dio_read_iter(struct kiocb *iocb, struct iov_iter *to) { ssize_t ret; @@ -88,7 +97,7 @@ static ssize_t ext4_dio_read_iter(struct kiocb *iocb, struct iov_iter *to) * taken. */ iocb->ki_flags &= ~IOCB_DIRECT; - return generic_file_read_iter(iocb, to); + return ext4_buffered_read_iter(iocb, to); } ret = iomap_dio_read_simple(iocb, to, ext4_iomap_begin); @@ -119,7 +128,7 @@ static ssize_t ext4_dax_read_iter(struct kiocb *iocb, struct iov_iter *to) if (!IS_DAX(inode)) { inode_unlock_shared(inode); /* Fallback to buffered IO in case we cannot support DAX */ - return generic_file_read_iter(iocb, to); + return ext4_buffered_read_iter(iocb, to); } ret = dax_iomap_rw(iocb, to, ext4_iomap_next); inode_unlock_shared(inode); @@ -146,7 +155,7 @@ static ssize_t ext4_file_read_iter(struct kiocb *iocb, struct iov_iter *to) if (iocb->ki_flags & IOCB_DIRECT) return ext4_dio_read_iter(iocb, to); - return generic_file_read_iter(iocb, to); + return ext4_buffered_read_iter(iocb, to); } static ssize_t ext4_file_splice_read(struct file *in, loff_t *ppos, @@ -362,6 +371,9 @@ static ssize_t ext4_buffered_write_iter(struct kiocb *iocb, if (iocb->ki_flags & IOCB_ATOMIC) return -EOPNOTSUPP; + if (iov_iter_is_dmabuf_map(from)) + return -EOPNOTSUPP; + inode_lock(inode); /* @@ -696,6 +708,13 @@ static ssize_t ext4_dio_write_iter(struct kiocb *iocb, struct iov_iter *from) ssize_t err; loff_t endbyte; + /* + * A dma-buf iterator can't go through the page cache, so return + * the short write instead of falling back to buffered I/O. + */ + if (iov_iter_is_dmabuf_map(from)) + return ret; + /* * Buffered I/O provides no torn-write protection, so never * finish an atomic write through the page cache. iomap must @@ -740,6 +759,9 @@ ext4_dax_write_iter(struct kiocb *iocb, struct iov_iter *from) bool extend = false; struct inode *inode = file_inode(iocb->ki_filp); + if (iov_iter_is_dmabuf_map(from)) + return -EOPNOTSUPP; + if (iocb->ki_flags & IOCB_NOWAIT) { if (!inode_trylock(inode)) return -EAGAIN; @@ -1059,10 +1081,27 @@ loff_t ext4_llseek(struct file *file, loff_t offset, int whence) return vfs_setpos(file, offset, maxbytes); } +static u64 ext4_file_dma_buf_io_tag(struct file *file) +{ + struct inode *inode = file_inode(file); + + if (!ext4_inode_can_dmabuf_io(inode)) + return 0; + return bdev_dma_buf_io_tag(inode->i_sb->s_bdev); +} + +static int ext4_file_init_dma_buf_io_ctx(struct file *file, + struct dma_buf_io_ctx *ctx) +{ + return bdev_init_dma_buf_io_ctx(file_inode(file)->i_sb->s_bdev, ctx); +} + const struct file_operations ext4_file_operations = { .llseek = ext4_llseek, .read_iter = ext4_file_read_iter, .write_iter = ext4_file_write_iter, + .init_dma_buf_io_ctx = ext4_file_init_dma_buf_io_ctx, + .dma_buf_io_tag = ext4_file_dma_buf_io_tag, .iopoll = iocb_bio_iopoll, .unlocked_ioctl = ext4_ioctl, #ifdef CONFIG_COMPAT -- 2.47.3