ext4: Add dma-buf read/write support
Subsystems (8)
cover ext4: Add dma-buf read/write support
From: Mengmeng Zhao <[email protected]>
This series add dma-buf read/write support for ext4. The dma-buf direct I/O infrastructure allows dma-buf to be handed to file I/O as an iov_iter, currently it only supports raw device. To better serve the GPU-NVME direct access through normal filesystem, prior work for XFS has been discussed at [1]. We also are interested in this field and working on it[2]. This patchset is based on Anuj Gupta's patchset as there are common parts at block/iomap. The base tree is:
https://github.com/anuj7781/linux-kernel/tree/anuj/fs-dmabuf-rfc-v11-desc-v2
Patches 1-5 add dma-buf guards at several common paths at block, mm/filemap, dax and iomap. Patches 6 add dma-buf support for ext4.
[1] https://lore.kernel.org/linux-xfs/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/
Mengmeng Zhao (6):
block: refuse a dma-buf bio that carries a crypto context mm/filemap: refuse dmabuf iterators in the generic read/write copy
paths
dax: refuse dmabuf iterators in dax_iomap_rw()
iomap: refuse dmabuf iterators in iomap_file_buffered_write()
iomap: refuse dmabuf iterators on contents-encrypted inodes
ext4: support dma-buf registered buffers for direct I/O
block/blk-mq.c | 4 +++- fs/dax.c | 4 ++++ fs/ext4/file.c | 33 +++++++++++++++++++++++++++++++++ fs/iomap/buffered-io.c | 7 +++++++ fs/iomap/direct-io.c | 10 ++++++++++ mm/filemap.c | 22 +++++++++++++++++++++- 6 files changed, 78 insertions(+), 2 deletions(-)
Signature · 1 line
2.43.0
RFC 1/6 block: refuse a dma-buf bio that carries a crypto context
From: Mengmeng Zhao <[email protected]>
A dma-buf bio has no pages for the crypto layer to walk: the software fallback would read the map as bio_vecs. Refuse it in blk_mq_submit_bio() before it can reach that path.
--- block/blk-mq.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
block/blk-mq.c +3 -1
@@ -3138,8 +3138,10 @@ void blk_mq_submit_bio(struct bio *bio) goto queue_exit; } +/* a dma-buf bio has no pages for integrity work or contents crypto */ if (unlikely (op_is_dmabuf (bio->bi_opf) && -bdev_has_integrity_csum (bio->bi_bdev))) { + (bdev_has_integrity_csum (bio->bi_bdev) || +bio_has_crypt_ctx (bio)))) {bio_endio_status (bio,BLK_STS_NOTSUPP ); goto queue_exit; }
Signature · 1 line
2.43.0
Re: [RFC 1/6] block: refuse a dma-buf bio that carries a crypto context
block/blk-mq.c void blk_mq_submit_bio(struct bio *bio) · line 3144
Quoted 5 lines · Mengmeng Zhao
block/blk-mq.c +3
-1+/* a dma-buf bio has no pages for integrity work or contents crypto */ if (unlikely (op_is_dmabuf (bio->bi_opf) && -bdev_has_integrity_csum (bio->bi_bdev))) { + (bdev_has_integrity_csum (bio->bi_bdev) || +bio_has_crypt_ctx (bio)))) {
This could really use a factored helper explaining all the refusals. And maybe we should use the chance to sit back and think what else would blow up and add it there as well.
Re: [RFC 1/6] block: refuse a dma-buf bio that carries a crypto context
Quoted 11 lines · Christoph Hellwig
block/blk-mq.c void blk_mq_submit_bio(struct bio *bio) · line 3144
Quoted 5 lines · Mengmeng Zhao
block/blk-mq.c +3
-1+/* a dma-buf bio has no pages for integrity work or contents crypto */ if (unlikely (op_is_dmabuf (bio->bi_opf) && -bdev_has_integrity_csum (bio->bi_bdev))) { + (bdev_has_integrity_csum (bio->bi_bdev) || +bio_has_crypt_ctx (bio)))) {This could really use a factored helper explaining all the refusals. And maybe we should use the chance to sit back and think what else would blow up and add it there as well.
Hi Christoph, thanks for the review. I will extract a helper in V2 soon. There are several buffered io fallbacks, it's really necessary to discuss how to avoid these. Glad to join it.
RFC 2/6 mm/filemap: refuse dmabuf iterators in the generic read/write copy paths
In reply to Mengmeng ZhaoFrom: Mengmeng Zhao <[email protected]>
The generic copy helpers cannot touch device memory. Refuse a dmabuf iterator in generic_perform_write() and the filemap_read() fallback while keeping any direct-I/O progress, and report a busy folio from generic_file_direct_write() as -EAGAIN instead of asking for the buffered fallback such an iterator cannot take.
--- mm/filemap.c | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-)
mm/filemap.c +21 -1
@@ -3013,6 +3013,14 @@ generic_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)return retval; } +/* +* Buffered I/O read is not supported for dmabuf; a short direct +* read before this point stands on its own, as in +* direct_write_fallback(). +*/ +if (iov_iter_is_dmabuf_map (iter)) +return retval >0 ? retval : -EOPNOTSUPP ; +return filemap_read (iocb, iter, retval); }EXPORT_SYMBOL (generic_file_read_iter);
@@ -4300,8 +4308,14 @@ generic_file_direct_write(struct kiocb *iocb, struct iov_iter *from)*/ written =kiocb_invalidate_pages (iocb, write_len);if (written) { -if (written == -EBUSY ) +if (written == -EBUSY ) { +/* +* Avoid fallback to buffered write for dmabuf. +*/ +if (iov_iter_is_dmabuf_map (from)) +return -EAGAIN ;return 0 ; + }return written; }
@@ -4353,6 +4367,12 @@ ssize_t generic_perform_write(struct kiocb *iocb, struct iov_iter *i)long status =0 ;ssize_t written =0 ; +/* +* Buffered write is not supported for dmabuf. +*/ +if (iov_iter_is_dmabuf_map (i)) +return -EOPNOTSUPP ; + do { struct folio *folio; size_t offset; /* Offset into folio */
Signature · 1 line
2.43.0
Re: [RFC 2/6] mm/filemap: refuse dmabuf iterators in the generic read/write copy paths
Quoted 3 lines · Mengmeng Zhao
From: Mengmeng Zhao <[email protected]>
The generic copy helpers cannot touch device memory.
Which generic copy helpers? This doesn't seem to touch copy_file_range at all.
Quoted 5 lines
Refuse a
dmabuf iterator ingeneric_perform_write()and thefilemap_read()fallback while keeping any direct-I/O progress, and report a busy folio fromgeneric_file_direct_write()as-EAGAINinstead of asking for the buffered fallback such an iterator cannot take.
I suspect the fallback code in the callers might be a better stop for this.
Re: [RFC 2/6] mm/filemap: refuse dmabuf iterators in the generic read/write copy paths
Quoted 8 lines · Christoph Hellwig
Quoted 3 lines · Mengmeng Zhao
From: Mengmeng Zhao <[email protected]>
The generic copy helpers cannot touch device memory.
Which generic copy helpers? This doesn't seem to touch
copy_file_rangeat all.
Sorry for the inaccuracy, it refers to the buffered io patch in generic_file_read_iter() and __generic_file_write_uter().
Quoted 9 lines
Quoted 5 lines
Refuse a
dmabuf iterator ingeneric_perform_write()and thefilemap_read()fallback while keeping any direct-I/O progress, and report a busy folio fromgeneric_file_direct_write()as-EAGAINinstead of asking for the buffered fallback such an iterator cannot take.I suspect the fallback code in the callers might be a better stop for this.
Ok, I will put it in the callers side.
RFC 3/6 dax: refuse dmabuf iterators in dax_iomap_rw()
In reply to Mengmeng ZhaoFrom: Mengmeng Zhao <[email protected]>
A dax access copies through the CPU linear address, which a device mapping cannot provide. A dmabuf iterator reaching dax_iomap_rw() splats a WARN_ON_ONCE from iterate_and_advance2() and fails the I/O with EFAULT. Refuse it up front with -EOPNOTSUPP, as the generic buffered copy paths already do.
--- fs/dax.c | 4 ++++ 1 file changed, 4 insertions(+)
fs/dax.c +4 -0
@@ -1759,6 +1759,10 @@ dax_iomap_rw(struct kiocb *iocb, struct iov_iter *iter,if (!iomi.len)return 0 ; +/* dax copies through the CPU, which a device mapping bypasses */ +if (iov_iter_is_dmabuf_map (iter)) +return -EOPNOTSUPP ; +if (iov_iter_rw (iter) ==WRITE ) {lockdep_assert_held_write (&iomi.inode->i_rwsem); iomi.flags |=IOMAP_WRITE ;
Signature · 1 line
2.43.0
RFC 4/6 iomap: refuse dmabuf iterators in iomap_file_buffered_write()
In reply to Mengmeng ZhaoFrom: Mengmeng Zhao <[email protected]>
A buffered write copies through the CPU linear address, which a device mapping cannot provide. fault_in_iov_iter_readable() treats a dmabuf iterator as always readable, so the copy fails with a WARN_ON_ONCE from iterate_and_advance2() and iomap_write_iter() retries the unchanged remainder forever.
Refuse the iterator at the entry with -EOPNOTSUPP, as generic_perform_write() and the generic read paths already do, so that every generic buffered copy helper turns the iterator away before touching the page cache.
--- fs/iomap/buffered-io.c | 7 +++++++ 1 file changed, 7 insertions(+)
fs/iomap/buffered-io.c +7 -0
@@ -1304,6 +1304,13 @@ iomap_file_buffered_write(struct kiocb *iocb, struct iov_iter *i, };ssize_t ret; +/* +* A buffered write copies through the CPU linear address, which a +* device mapping cannot provide. +*/ +if (iov_iter_is_dmabuf_map (i)) +return -EOPNOTSUPP ; +if (iocb->ki_flags &IOCB_NOWAIT ) iter.flags |=IOMAP_NOWAIT ;if (iocb->ki_flags &IOCB_DONTCACHE )
Signature · 1 line
2.43.0
RFC 5/6 iomap: refuse dmabuf iterators on contents-encrypted inodes
In reply to Mengmeng ZhaoFrom: Mengmeng Zhao <[email protected]>
Contents encryption needs the crypto layer to reach the data pages of the bio: blk-crypto_fallback kmaps and memcpys them, which a device mapping does not allow. Refuse the iterator in __iomap_dio_rw() before any bio is submitted instead of depending on each filesystem to keep dmabuf away from encrypted inodes, and in iomap_file_init_dma_buf_io_ctx() so that registration on an encrypted file fails up front.
--- fs/iomap/direct-io.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
fs/iomap/direct-io.c +10 -0
@@ -728,6 +728,14 @@ __iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,if (iov_iter_is_dmabuf_map (iter) && (dio_flags &IOMAP_DIO_BOUNCE ))return ERR_PTR (-EOPNOTSUPP ); +/* +* Contents encryption walks the bio pages in the crypto layer, which +* a dmabuf bio has none of. Reject before any bio is submitted. +*/ +if (iov_iter_is_dmabuf_map (iter) && +fscrypt_needs_contents_encryption (inode)) +return ERR_PTR (-EOPNOTSUPP ); + dio =kmalloc_obj (*dio);if (!dio)return ERR_PTR (-ENOMEM );
@@ -940,6 +948,8 @@ int iomap_file_init_dma_buf_io_ctx(struct file *file, struct block_device *bdev,if (IS_DAX (inode))return -EOPNOTSUPP ; +if (fscrypt_needs_contents_encryption (inode)) +return -EOPNOTSUPP ;return bdev_init_dma_buf_io_ctx (file, bdev, ctx); }EXPORT_SYMBOL_GPL (iomap_file_init_dma_buf_io_ctx);
Signature · 1 line
2.43.0
Re: [RFC 5/6] iomap: refuse dmabuf iterators on contents-encrypted inodes
I think contents-encrypted should be content-encrypted.
But we really need to find a way to make these checks more systematic instead of sprinkling them everywhere. E.g. have a flag in file->f_mode to allow it, which then only leaves the direct I/O to buffered I/O fallback as a special case.
Re: [RFC 5/6] iomap: refuse dmabuf iterators on contents-encrypted inodes
Quoted 7 lines · Christoph Hellwig
I think contents-encrypted should be content-encrypted.
But we really need to find a way to make these checks more systematic instead of sprinkling them everywhere. E.g. have a flag in
file->f_modeto allow it, which then only leaves the direct I/O to buffered I/O fallback as a special case.
Agreed, adding the check to every file system is quite tedious. We'll look into redesigning the implementation based on your suggestion. Thanks for your time!
RFC 6/6 ext4: support dma-buf registered buffers for direct I/O
In reply to Mengmeng ZhaoFrom: Mengmeng Zhao <[email protected]>
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.
--- fs/ext4/file.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+)
fs/ext4/file.c +33 -0
@@ -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
Signature · 1 line
2.43.0