From: Mengmeng Zhao <mengmeng.zhao@linux.dev>
To: Jens Axboe <axboe@kernel.dk>, Dan Williams <djbw@kernel.org>,
Matthew Wilcox <willy@infradead.org>, Jan Kara <jack@suse.cz>,
Alexander Viro <viro@zeniv.linux.org.uk>,
Christian Brauner <brauner@kernel.org>,
tytso@mit.edu, Andreas Dilger <adilger.kernel@dilger.ca>,
Baokun Li <libaokun@linux.alibaba.com>,
Ojaswin Mujoo <ojaswin@linux.ibm.com>,
"Ritesh Harjani (IBM)" <ritesh.list@gmail.com>,
Zhang Yi <yi.zhang@huawei.com>,
"Darrick J. Wong" <djwong@kernel.org>,
Andrew Morton <akpm@linux-foundation.org>,
asml.silence@gmail.com, hch@lst.de,
Anuj Gupta <anuj1072538@gmail.com>
Cc: linux-block@vger.kernel.org, linux-kernel@vger.kernel.org,
linux-fsdevel@vger.kernel.org, nvdimm@lists.linux.dev,
linux-ext4@vger.kernel.org, linux-xfs@vger.kernel.org,
linux-mm@kvack.org, Li Wang <liwang@kylinos.cn>
Subject: [RFC 6/6] ext4: support dma-buf registered buffers for direct I/O
Date: Thu, 5 Nov 2026 14:58:47 +0800 [thread overview]
Message-ID: <20261105065847.196540-7-mengmeng.zhao@linux.dev> (raw)
In-Reply-To: <20261105065847.196540-1-mengmeng.zhao@linux.dev>
From: Mengmeng Zhao <zhaomengmeng@kylinos.cn>
Add the init_dma_buf_io_ctx fop for ext4 files. The data of every
regular file lives on the block device of the mount, so the buffer is
registered for that device and an ext4 file cannot move to another disk
after the registration.
A device mapping can only be read or written by a direct I/O bio.
Add a fast check at ext4_file_read_iter/ext4_file_write_iter entry.
Refuse the iterator for buffered I/O in the two dispatchers; the DAX
methods are covered by dax_iomap_rw() and direct I/O by the iomap and
block layer guards.
A direct I/O write whose tail still needs the page cache reports the
bytes the block layer already submitted instead of the error, as that
part cannot be undone and the remainder can never be copied.
Signed-off-by: Mengmeng Zhao <zhaomengmeng@kylinos.cn>
Signed-off-by: Li Wang <liwang@kylinos.cn>
---
fs/ext4/file.c | 33 +++++++++++++++++++++++++++++++++
1 file changed, 33 insertions(+)
diff --git a/fs/ext4/file.c b/fs/ext4/file.c
index 374b4bc25bd5..46629915fa84 100644
--- a/fs/ext4/file.c
+++ b/fs/ext4/file.c
@@ -139,6 +139,12 @@ static ssize_t ext4_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
if (!iov_iter_count(to))
return 0; /* skip atime */
+ /* a device mapping can only be read by a direct I/O bio */
+ if (iov_iter_is_dmabuf_map(to) &&
+ (!(iocb->ki_flags & IOCB_DIRECT) ||
+ !ext4_should_use_dio(iocb, to)))
+ return -EOPNOTSUPP;
+
#ifdef CONFIG_FS_DAX
if (IS_DAX(inode))
return ext4_dax_read_iter(iocb, to);
@@ -670,6 +676,13 @@ static ssize_t ext4_dio_write_iter(struct kiocb *iocb, struct iov_iter *from)
ssize_t err;
loff_t endbyte;
+ /*
+ * The page cache cannot copy from a device mapping. What
+ * direct I/O already submitted stands on its own.
+ */
+ if (iov_iter_is_dmabuf_map(from))
+ return ret > 0 ? ret : -EAGAIN;
+
/*
* There is no support for atomic writes on buffered-io yet,
* we should never fallback to buffered-io for DIO atomic
@@ -773,6 +786,12 @@ ext4_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
if (unlikely(ret))
return ret;
+ /* a device mapping can only be written by a direct I/O bio */
+ if (iov_iter_is_dmabuf_map(from) &&
+ (!(iocb->ki_flags & IOCB_DIRECT) ||
+ !ext4_should_use_dio(iocb, from)))
+ return -EOPNOTSUPP;
+
#ifdef CONFIG_FS_DAX
if (IS_DAX(inode))
return ext4_dax_write_iter(iocb, from);
@@ -1032,10 +1051,24 @@ loff_t ext4_llseek(struct file *file, loff_t offset, int whence)
return vfs_setpos(file, offset, maxbytes);
}
+/*
+ * The data of every regular file lives on the block device of the
+ * filesystem, so an ext4 file cannot move to another disk after the
+ * buffer has been registered.
+ */
+static int ext4_file_init_dma_buf_io_ctx(struct file *file,
+ struct dma_buf_io_ctx *ctx)
+{
+ return iomap_file_init_dma_buf_io_ctx(file,
+ 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,
.iopoll = iocb_bio_iopoll,
.unlocked_ioctl = ext4_ioctl,
#ifdef CONFIG_COMPAT
--
2.43.0
prev parent reply other threads:[~2026-11-05 7:00 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-05 6:58 [RFC 0/6] ext4: Add dma-buf read/write support Mengmeng Zhao
2026-11-05 6:58 ` [RFC 1/6] block: refuse a dma-buf bio that carries a crypto context Mengmeng Zhao
2026-11-05 6:58 ` [RFC 2/6] mm/filemap: refuse dmabuf iterators in the generic read/write copy paths Mengmeng Zhao
2026-11-05 6:58 ` [RFC 3/6] dax: refuse dmabuf iterators in dax_iomap_rw() Mengmeng Zhao
2026-11-05 6:58 ` [RFC 4/6] iomap: refuse dmabuf iterators in iomap_file_buffered_write() Mengmeng Zhao
2026-11-05 6:58 ` [RFC 5/6] iomap: refuse dmabuf iterators on contents-encrypted inodes Mengmeng Zhao
2026-11-05 6:58 ` Mengmeng Zhao [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261105065847.196540-7-mengmeng.zhao@linux.dev \
--to=mengmeng.zhao@linux.dev \
--cc=adilger.kernel@dilger.ca \
--cc=akpm@linux-foundation.org \
--cc=anuj1072538@gmail.com \
--cc=asml.silence@gmail.com \
--cc=axboe@kernel.dk \
--cc=brauner@kernel.org \
--cc=djbw@kernel.org \
--cc=djwong@kernel.org \
--cc=hch@lst.de \
--cc=jack@suse.cz \
--cc=libaokun@linux.alibaba.com \
--cc=linux-block@vger.kernel.org \
--cc=linux-ext4@vger.kernel.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=linux-xfs@vger.kernel.org \
--cc=liwang@kylinos.cn \
--cc=nvdimm@lists.linux.dev \
--cc=ojaswin@linux.ibm.com \
--cc=ritesh.list@gmail.com \
--cc=tytso@mit.edu \
--cc=viro@zeniv.linux.org.uk \
--cc=willy@infradead.org \
--cc=yi.zhang@huawei.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox