fs: support dma-buf backed direct I/O
Subsystems (6)
cover fs: support dma-buf backed direct I/O
This series lets an application register a dma-buf against a file, so reads and writes go directly to the device as dma-buf backed bios with no per-command DMA mapping.
This series builds on Pavel Begunkov's dma-buf block support v11,
https://lore.kernel.org/all/[email protected]/
which is queued in Jens Axboe's tree:
https://git.kernel.org/pub/scm/linux/kernel/git/axboe/linux.git/log/?h=for-7.4/io_uring-dmabuf
A separate kernel tree is present here:
https://github.com/anuj7781/linux-kernel/tree/anuj/fs-dmabuf-rfc-v11-desc-v2
Nitesh Shetty (4):
block: factor out bdev_init_dma_buf_io_ctx()
block: refuse a dma-buf bio for a disk the buffer was not registered
on
iomap: add dma-buf direct I/O guards and registration helper
xfs: support dma-buf registered buffers for direct I/O
block/bio.c | 15 +++++++++++++++ block/fops.c | 19 +++++++++++++++---- fs/iomap/direct-io.c | 31 ++++++++++++++++++++++++++++++- fs/xfs/xfs_file.c | 30 ++++++++++++++++++++++++++++++ include/linux/blkdev.h | 3 +++ include/linux/dma-buf-io.h | 2 ++ include/linux/iomap.h | 3 +++ 7 files changed, 98 insertions(+), 5 deletions(-)
Signature · 1 line
2.43.0
RFC 1/4 block: factor out bdev_init_dma_buf_io_ctx()
From: Nitesh Shetty <[email protected]>
Factor the checks and driver setup into an exported helper that takes an explicit block device, so filesystems can initialize a dma-buf I/O context for the device backing a regular file. The block-device file operation becomes a wrapper using file_bdev(), so its behavior is unchanged.
--- block/fops.c | 12 +++++++++--- include/linux/blkdev.h | 3 +++ 2 files changed, 12 insertions(+), 3 deletions(-)
block/fops.c +9 -3
@@ -934,10 +934,9 @@ static int blkdev_mmap_prepare(struct vm_area_desc *desc)return generic_file_mmap_prepare (desc); } -static int blkdev_init_dma_buf_io_ctx (struct file *file, -struct dma_buf_io_ctx *ctx) +int bdev_init_dma_buf_io_ctx (struct file *file,struct block_device *bdev, +struct dma_buf_io_ctx *ctx) { -struct block_device *bdev =file_bdev (file);struct gendisk *disk = bdev->bd_disk;if (!(file->f_flags &O_DIRECT ))
@@ -948,6 +947,13 @@ static int blkdev_init_dma_buf_io_ctx(struct file *file,return -EOPNOTSUPP ;return disk->fops->init_dma_buf_io_ctx (bdev, ctx); } +EXPORT_SYMBOL_GPL (bdev_init_dma_buf_io_ctx); + +static int blkdev_init_dma_buf_io_ctx (struct file *file, +struct dma_buf_io_ctx *ctx) +{ +return bdev_init_dma_buf_io_ctx (file,file_bdev (file), ctx); +}const struct file_operations def_blk_fops = { .open = blkdev_open,
include/linux/blkdev.h +3 -0
@@ -1609,6 +1609,9 @@ struct block_device_operations {int (*alternative_gpt_sector)(struct gendisk *disk,sector_t *sector); }; +int bdev_init_dma_buf_io_ctx (struct file *file,struct block_device *bdev, +struct dma_buf_io_ctx *ctx); +#ifdef CONFIG_COMPAT extern int blkdev_compat_ptr_ioctl (struct block_device *,blk_mode_t ,unsigned int ,unsigned long );
Signature · 1 line
2.43.0
Re: [RFC 1/4] block: factor out bdev_init_dma_buf_io_ctx()
Quoted 7 lines · Anuj Gupta
From: Nitesh Shetty <[email protected]>
Factor the checks and driver setup into an exported helper that takes an explicit block device, so filesystems can initialize a dma-buf I/O context for the device backing a regular file. The block-device file operation becomes a wrapper using
file_bdev(), so its behavior is unchanged.
Please move hte helper out of block/fops.c, as that only is for the block device file operations.
RFC 2/4 block: refuse a dma-buf bio for a disk the buffer was not registered on
In reply to Anuj GuptaFrom: Nitesh Shetty <[email protected]>
A registered dma-buf map belongs to the device it was mapped for. The device backing a file can change after registration. Record the registered disk's dev_t at init and refuse a bio that targets a different one.
--- block/bio.c | 15 +++++++++++++++ block/fops.c | 7 ++++++- include/linux/dma-buf-io.h | 2 ++ 3 files changed, 23 insertions(+), 1 deletion(-)
block/bio.c +15 -0
@@ -19,6 +19,7 @@#include <linux/blk-crypto.h> #include <linux/xarray.h> #include <linux/kmemleak.h> +#include <linux/dma-buf-io.h> #include <trace/events/block.h> #include "blk.h"
@@ -1269,6 +1270,17 @@ static inline bool bio_iov_bvec_aligned(const struct bio *bio, }#endif +/* +* A dma-buf map is only valid for the disk it was registered on, but +* the device backing a file can change after the registration. +*/ +static bool bio_dmabuf_map_valid (const struct bio *bio, +const struct iov_iter *iter) +{ +return iter->dmabuf_map->ctx->registered_devt == +disk_devt (bio->bi_bdev->bd_disk); +} +/** * bio_iov_iter_get_pages - add user or kernel pages to a bio * @bio: bio to add pages to
@@ -1300,6 +1312,9 @@ int bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter,if (WARN_ON_ONCE (bio_flagged (bio,BIO_CLONED )))return -EIO ; +if (iov_iter_is_dmabuf_map (iter) && !bio_dmabuf_map_valid (bio, iter)) +return -EXDEV ; +if (bio_iov_iter_set (bio, iter)) {if (iov_iter_is_bvec (iter) && !bio_iov_bvec_aligned (bio, mem_align_mask))
block/fops.c +6 -1
@@ -19,6 +19,7 @@#include <linux/iomap.h> #include <linux/module.h> #include <linux/io_uring/cmd.h> +#include <linux/dma-buf-io.h> #include "blk.h" static inline struct inode *bdev_file_inode (struct file *file)
@@ -938,6 +939,7 @@ int bdev_init_dma_buf_io_ctx(struct file *file, struct block_device *bdev, struct dma_buf_io_ctx *ctx) {struct gendisk *disk = bdev->bd_disk; +int ret;if (!(file->f_flags &O_DIRECT ))return -EINVAL ;
@@ -945,7 +947,10 @@ int bdev_init_dma_buf_io_ctx(struct file *file, struct block_device *bdev,return -EOPNOTSUPP ;if (bdev_has_integrity_csum (bdev))return -EOPNOTSUPP ; -return disk->fops->init_dma_buf_io_ctx (bdev, ctx); + ret = disk->fops->init_dma_buf_io_ctx (bdev, ctx); +if (!ret) + ctx->registered_devt =disk_devt (disk); +return ret; }EXPORT_SYMBOL_GPL (bdev_init_dma_buf_io_ctx);
include/linux/dma-buf-io.h +2 -0
@@ -50,6 +50,8 @@ struct dma_buf_io_ctx {void *dev_priv;const struct dma_buf_io_ops *dev_ops; + +dev_t registered_devt; }; int dma_buf_io_ctx_create(struct file *file,
Signature · 1 line
2.43.0
Re: [RFC 2/4] block: refuse a dma-buf bio for a disk the buffer was not registered on
Quoted 6 lines · Anuj Gupta
From: Nitesh Shetty <[email protected]>
A registered dma-buf map belongs to the device it was mapped for. The device backing a file can change after registration. Record the registered disk's
dev_tat init and refuse a bio that targets a different one.
Can you explain a bit better what this is trying to solve?
Re: [RFC 2/4] block: refuse a dma-buf bio for a disk the buffer was not registered on
In reply to Anuj GuptaQuoted 1 line · Anuj Gupta
From: Nitesh Shetty <[email protected]>
...> +static bool bio_dmabuf_map_valid(const struct bio *bio,
Quoted 56 lines
block/bio.c +9
-0+ const struct iov_iter *iter) +{ +return iter->dmabuf_map->ctx->registered_devt == +disk_devt (bio->bi_bdev->bd_disk); +} +/** * bio_iov_iter_get_pages - add user or kernel pages to a bio * @bio: bio to add pages to @@ -1300,6 +1312,9 @@ int bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter,if (WARN_ON_ONCE (bio_flagged (bio,BIO_CLONED )))return -EIO ; +if (iov_iter_is_dmabuf_map (iter) && !bio_dmabuf_map_valid (bio, iter)) +return -EXDEV ; +if (bio_iov_iter_set (bio, iter)) {if (iov_iter_is_bvec (iter) && !bio_iov_bvec_aligned (bio, mem_align_mask))block/fops.c +6
-1@@ -19,6 +19,7 @@#include <linux/iomap.h> #include <linux/module.h> #include <linux/io_uring/cmd.h> +#include <linux/dma-buf-io.h> #include "blk.h" static inline struct inode *bdev_file_inode (struct file *file)@@ -938,6 +939,7 @@ int bdev_init_dma_buf_io_ctx(struct file *file, struct block_device *bdev, struct dma_buf_io_ctx *ctx) {struct gendisk *disk = bdev->bd_disk; +int ret;if (!(file->f_flags &O_DIRECT ))return -EINVAL ;@@ -945,7 +947,10 @@ int bdev_init_dma_buf_io_ctx(struct file *file, struct block_device *bdev,return -EOPNOTSUPP ;if (bdev_has_integrity_csum (bdev))return -EOPNOTSUPP ; -return disk->fops->init_dma_buf_io_ctx (bdev, ctx); + ret = disk->fops->init_dma_buf_io_ctx (bdev, ctx); +if (!ret) + ctx->registered_devt =disk_devt (disk); +return ret; }EXPORT_SYMBOL_GPL (bdev_init_dma_buf_io_ctx);include/linux/dma-buf-io.h +0
-0
include/linux/dma-buf-io.h struct dma_buf_io_ctx { · line 54
Quoted 5 lines
include/linux/dma-buf-io.h +2
-0@@ -50,6 +50,8 @@ struct dma_buf_io_ctx {void *dev_priv;const struct dma_buf_io_ops *dev_ops; + +dev_t registered_devt;
That wouldn't work well with multiple devices. Maybe reject it in xfs / iomap for now?
FWIW, a generic solution is for xfs to own the ctx passed on registration instead of forwarding it to block, allocate an array of new ctxs for each device and register them separately. This should even be able to support dynamic device registration.
Signature · 1 line
Pavel Begunkov
Re: [RFC 2/4] block: refuse a dma-buf bio for a disk the buffer was not registered on
Quoted 63 lines · Pavel Begunkov
Quoted 1 line · Anuj Gupta
From: Nitesh Shetty <[email protected]>
...> +static bool bio_dmabuf_map_valid(const struct bio *bio,
Quoted 52 lines
block/bio.c +9
-0+ const struct iov_iter *iter) +{ +return iter->dmabuf_map->ctx->registered_devt == +disk_devt (bio->bi_bdev->bd_disk); +} +/** * bio_iov_iter_get_pages - add user or kernel pages to a bio * @bio: bio to add pages to @@ -1300,6 +1312,9 @@ int bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter,if (WARN_ON_ONCE (bio_flagged (bio,BIO_CLONED )))return -EIO ; +if (iov_iter_is_dmabuf_map (iter) && !bio_dmabuf_map_valid (bio, iter)) +return -EXDEV ; +if (bio_iov_iter_set (bio, iter)) {if (iov_iter_is_bvec (iter) && !bio_iov_bvec_aligned (bio, mem_align_mask))block/fops.c +6
-1@@ -19,6 +19,7 @@#include <linux/iomap.h> #include <linux/module.h> #include <linux/io_uring/cmd.h> +#include <linux/dma-buf-io.h> #include "blk.h" static inline struct inode *bdev_file_inode (struct file *file)@@ -938,6 +939,7 @@ int bdev_init_dma_buf_io_ctx(struct file *file, struct block_device *bdev, struct dma_buf_io_ctx *ctx) {struct gendisk *disk = bdev->bd_disk; +int ret;if (!(file->f_flags &O_DIRECT ))return -EINVAL ;@@ -945,7 +947,10 @@ int bdev_init_dma_buf_io_ctx(struct file *file, struct block_device *bdev,return -EOPNOTSUPP ;if (bdev_has_integrity_csum (bdev))return -EOPNOTSUPP ; -return disk->fops->init_dma_buf_io_ctx (bdev, ctx); + ret = disk->fops->init_dma_buf_io_ctx (bdev, ctx); +if (!ret) + ctx->registered_devt =disk_devt (disk); +return ret; }EXPORT_SYMBOL_GPL (bdev_init_dma_buf_io_ctx);include/linux/dma-buf-io.h +0
-0include/linux/dma-buf-io.h struct dma_buf_io_ctx { · line 53
Quoted 4 lines
include/linux/dma-buf-io.h +2
-0@@ -50,6 +50,8 @@ struct dma_buf_io_ctx {void *dev_priv;const struct dma_buf_io_ops *dev_ops; + +dev_t registered_devt;That wouldn't work well with multiple devices. Maybe reject it in xfs / iomap for now?
Quoted 5 lines
FWIW, a generic solution is for xfs to own the ctx passed on registration instead of forwarding it to block, allocate an array of new ctxs for each device and register them separately. This should even be able to support dynamic device registration.
Agree, that works, keeps block layer clean, will update in next version.
--Nitesh
RFC 3/4 iomap: add dma-buf direct I/O guards and registration helper
In reply to Anuj GuptaFrom: Nitesh Shetty <[email protected]>
Direct I/O copies data from the CPU in three places, zero-filling holes, inline data, and bounce pages. A dma-buf iterator has no CPU-visible pages, so refuse it.
Reads that encounter a hole or unwritten extent therefore return -EOPNOTSUPP for the entire operation, even if I/O for an earlier mapped extent was already submitted.
Also add iomap_file_init_dma_buf_io_ctx(), so filesystems can register without open-coding the DAX check. The filesystem picks the device, iomap does the rest.
--- fs/iomap/direct-io.c | 31 ++++++++++++++++++++++++++++++- include/linux/iomap.h | 3 +++ 2 files changed, 33 insertions(+), 1 deletion(-)
fs/iomap/direct-io.c +30 -1
@@ -427,6 +427,10 @@ static int iomap_dio_bio_iter(struct iomap_iter *iter, struct iomap_dio *dio)unsigned int alignment;ssize_t ret =0 ; +if (iov_iter_is_dmabuf_map (dio->submit.iter) && + (iomap->flags &IOMAP_F_INTEGRITY )) +return -EOPNOTSUPP ; +/* * File systems that write out of place and always allocate new blocks * need each bio to be block aligned as that's the unit of allocation.
@@ -595,7 +599,13 @@ static int iomap_dio_bio_iter(struct iomap_iter *iter, struct iomap_dio *dio)static int iomap_dio_hole_iter (struct iomap_iter *iter,struct iomap_dio *dio) { -loff_t length =iov_iter_zero (iomap_length (iter), dio->submit.iter); +loff_t length; + +/* a device mapping cannot be zeroed from the CPU */ +if (iov_iter_is_dmabuf_map (dio->submit.iter)) +return -EOPNOTSUPP ; + + length =iov_iter_zero (iomap_length (iter), dio->submit.iter); dio->size += length;if (!length)
@@ -612,6 +622,10 @@ static int iomap_dio_inline_iter(struct iomap_iter *iomi, struct iomap_dio *dio)loff_t pos = iomi->pos;u64 copied; +/* inline data is copied from the CPU, which a device mapping bypasses */ +if (iov_iter_is_dmabuf_map (iter)) +return -EOPNOTSUPP ; +if (WARN_ON_ONCE (!inline_data))return -EIO ;
@@ -710,6 +724,10 @@ __iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,if (!iomi.len)return NULL ; +/* bouncing copies the data from the CPU, which a device mapping bypasses */ +if (iov_iter_is_dmabuf_map (iter) && (dio_flags &IOMAP_DIO_BOUNCE )) +return ERR_PTR (-EOPNOTSUPP ); + dio =kmalloc_obj (*dio);if (!dio)return ERR_PTR (-ENOMEM );
@@ -915,6 +933,17 @@ iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter, }EXPORT_SYMBOL_GPL (iomap_dio_rw); +int iomap_file_init_dma_buf_io_ctx (struct file *file,struct block_device *bdev, +struct dma_buf_io_ctx *ctx) +{ +struct inode *inode =file_inode (file); + +if (IS_DAX (inode)) +return -EOPNOTSUPP ; +return bdev_init_dma_buf_io_ctx (file, bdev, ctx); +} +EXPORT_SYMBOL_GPL (iomap_file_init_dma_buf_io_ctx); +struct iomap_dio_simple {struct kiocb *iocb;size_t size;
include/linux/iomap.h +3 -0
@@ -682,6 +682,9 @@ struct iomap_dio *__iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,ssize_t iomap_dio_complete (struct iomap_dio *dio);void iomap_dio_bio_end_io (struct bio *bio); +int iomap_file_init_dma_buf_io_ctx (struct file *file,struct block_device *bdev, +struct dma_buf_io_ctx *ctx); +/* * Fast path for small, block-aligned direct I/Os that map to a single * contiguous on-disk extent.
Signature · 1 line
2.43.0
Re: [RFC 3/4] iomap: add dma-buf direct I/O guards and registration helper
Quoted 52 lines · Anuj Gupta
From: Nitesh Shetty <[email protected]>
Direct I/O copies data from the CPU in three places, zero-filling holes, inline data, and bounce pages. A dma-buf iterator has no CPU-visible pages, so refuse it.
Reads that encounter a hole or unwritten extent therefore return
-EOPNOTSUPPfor the entire operation, even if I/O for an earlier mapped extent was already submitted.Also add
iomap_file_init_dma_buf_io_ctx(), so filesystems can register without open-coding the DAX check. The filesystem picks the device, iomap does the rest.Signed-off-by Nitesh Shetty <[email protected]>Signed-off-by Anuj Gupta <[email protected]>--- fs/iomap/direct-io.c | 31 ++++++++++++++++++++++++++++++- include/linux/iomap.h | 3 +++ 2 files changed, 33 insertions(+), 1 deletion(-)fs/iomap/direct-io.c +11
-1@@ -427,6 +427,10 @@ static int iomap_dio_bio_iter(struct iomap_iter *iter, struct iomap_dio *dio)unsigned int alignment;ssize_t ret =0 ; +if (iov_iter_is_dmabuf_map (dio->submit.iter) && + (iomap->flags &IOMAP_F_INTEGRITY )) +return -EOPNOTSUPP ; +/* * File systems that write out of place and always allocate new blocks * need each bio to be block aligned as that's the unit of allocation. @@ -595,7 +599,13 @@ static int iomap_dio_bio_iter(struct iomap_iter *iter, struct iomap_dio *dio)static int iomap_dio_hole_iter (struct iomap_iter *iter,struct iomap_dio *dio) { -loff_t length =iov_iter_zero (iomap_length (iter), dio->submit.iter); +loff_t length; + +/* a device mapping cannot be zeroed from the CPU */ +if (iov_iter_is_dmabuf_map (dio->submit.iter)) +return -EOPNOTSUPP ; + + length =iov_iter_zero (iomap_length (iter), dio->submit.iter); dio->size += length;if (!length)
fs/iomap/direct-io.c static int iomap_dio_inline_iter(struct iomap_iter *iomi, struct iomap_dio *dio) · line 625
Quoted 4 lines
fs/iomap/direct-io.c +1
-0@@ -612,6 +622,10 @@ static int iomap_dio_inline_iter(struct iomap_iter *iomi, struct iomap_dio *dio)loff_t pos = iomi->pos;u64 copied; +/* inline data is copied from the CPU, which a device mapping bypasses */
Overly lone line here.
fs/iomap/direct-io.c __iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter, · line 727
Quoted 1 line
fs/iomap/direct-io.c +1
-0+/* bouncing copies the data from the CPU, which a device mapping bypasses */
.. and here. Note that could still copy to/from the device memory in either case, but that's probably rather pointless at least for actual device dmabufs. It might be useful for udmabuf or other kernel allocations, though.
RFC 4/4 xfs: support dma-buf registered buffers for direct I/O
In reply to Anuj GuptaFrom: Nitesh Shetty <[email protected]>
Add the init_dma_buf_io_ctx fop for XFS files. XFS picks the device and calls the iomap layer with the relevant device.
Refuse the iterator from every CPU-copy path: DAX, buffered fallback from a -ENOTBLK direct write, and read/writes without O_DIRECT.
If a file is switched between the data and realtime devices after registration, the block layer cross-device check refuses the I/O.
--- fs/xfs/xfs_file.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+)
fs/xfs/xfs_file.c +30 -0
@@ -340,6 +340,11 @@ xfs_file_read_iter(if (xfs_is_shutdown (mp))return -EIO ; +/* a device mapping can only be read by a direct I/O bio */ +if (iov_iter_is_dmabuf_map (to) && + (IS_DAX (inode) || !(iocb->ki_flags &IOCB_DIRECT ))) +return -EOPNOTSUPP ; +if (IS_DAX (inode)) ret =xfs_file_dax_read (iocb, to);else if (iocb->ki_flags &IOCB_DIRECT )
@@ -1196,6 +1201,11 @@ xfs_file_write_iter(return ret; } +/* a device mapping can only be written by a direct I/O bio */ +if (iov_iter_is_dmabuf_map (from) && + (IS_DAX (inode) || !(iocb->ki_flags &IOCB_DIRECT ))) +return -EOPNOTSUPP ; +if (IS_DAX (inode))return xfs_file_dax_write (iocb, from);
@@ -1209,6 +1219,9 @@ xfs_file_write_iter( ret =xfs_file_dio_write (iocb, from);if (ret != -ENOTBLK )return ret; +/* the page cache cannot copy from a device mapping */ +if (iov_iter_is_dmabuf_map (from)) +return -EOPNOTSUPP ; }if (xfs_is_zoned_inode (ip))
@@ -2173,10 +2186,27 @@ xfs_file_mmap_prepare(return 0 ; } +/* +* A realtime file's data lives on the realtime device, anything else on the +* data device. If a file is switched between them after registration, the +* block layer cross-device check refuses the I/O. +*/ +static int +xfs_file_init_dma_buf_io_ctx ( +struct file *file, +struct dma_buf_io_ctx *ctx) +{ +struct xfs_inode *ip =XFS_I (file_inode (file)); + +return iomap_file_init_dma_buf_io_ctx (file, +xfs_inode_buftarg (ip)->bt_bdev, ctx); +} +const struct file_operations xfs_file_operations = { .llseek = xfs_file_llseek, .read_iter = xfs_file_read_iter, .write_iter = xfs_file_write_iter, + .init_dma_buf_io_ctx = xfs_file_init_dma_buf_io_ctx, .splice_read = xfs_file_splice_read, .splice_write = iter_file_splice_write, .iopoll = iocb_bio_iopoll,
Signature · 1 line
2.43.0
Re: [RFC 4/4] xfs: support dma-buf registered buffers for direct I/O
Quoted 24 lines · Anuj Gupta
From: Nitesh Shetty <[email protected]>
Add the
init_dma_buf_io_ctxfop for XFS files. XFS picks the device and calls the iomap layer with the relevant device.Refuse the iterator from every CPU-copy path: DAX, buffered fallback from a
-ENOTBLKdirect write, and read/writes withoutO_DIRECT.If a file is switched between the data and realtime devices after registration, the block layer cross-device check refuses the I/O.
Signed-off-by Nitesh Shetty <[email protected]>--- fs/xfs/xfs_file.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+)fs/xfs/xfs_file.c +0
-0
fs/xfs/xfs_file.c xfs_file_read_iter( · line 346
Quoted 7 lines
fs/xfs/xfs_file.c +4
-0@@ -340,6 +340,11 @@ xfs_file_read_iter(if (xfs_is_shutdown (mp))return -EIO ; +/* a device mapping can only be read by a direct I/O bio */ +if (iov_iter_is_dmabuf_map (to) && + (IS_DAX (inode) || !(iocb->ki_flags &IOCB_DIRECT ))) +return -EOPNOTSUPP ;
Maybe move this into the actual methods for dax/buffered instead to avoid extra conditionals? Same for the write side.
fs/xfs/xfs_file.c xfs_file_write_iter( · line 1224
Quoted 8 lines
fs/xfs/xfs_file.c +3
-0@@ -1209,6 +1219,9 @@ xfs_file_write_iter( ret =xfs_file_dio_write (iocb, from);if (ret != -ENOTBLK )return ret; +/* the page cache cannot copy from a device mapping */ +if (iov_iter_is_dmabuf_map (from)) +return -EOPNOTSUPP ;
I suspect these random fallbacks are going to cause trouble, but I'm not really sure what to do about it either.
Quoted 5 lines
fs/xfs/xfs_file.c +5
-0+/* +* A realtime file's data lives on the realtime device, anything else on the +* data device. If a file is switched between them after registration, the +* block layer cross-device check refuses the I/O. +*/
fs/xfs/xfs_file.c xfs_file_mmap_prepare( · line 2203
Quoted 10 lines
fs/xfs/xfs_file.c +10
-0+static int +xfs_file_init_dma_buf_io_ctx ( +struct file *file, +struct dma_buf_io_ctx *ctx) +{ +struct xfs_inode *ip =XFS_I (file_inode (file)); + +return iomap_file_init_dma_buf_io_ctx (file, +xfs_inode_buftarg (ip)->bt_bdev, ctx); +}
Ah, this is where the previous cross check comes from. What about simply disallowing toggling the RT flag with outstanding reservations instead?
Re: [RFC 4/4] xfs: support dma-buf registered buffers for direct I/O
Quoted 10 lines · Christoph Hellwig
Quoted 10 lines
fs/xfs/xfs_file.c +10
-0+/* +* A realtime file's data lives on the realtime device, anything else on the +* data device. If a file is switched between them after registration, the +* block layer cross-device check refuses the I/O. +*/ +static int +xfs_file_init_dma_buf_io_ctx ( +struct file *file, +struct dma_buf_io_ctx *ctx) +{
Quoted 8 lines
fs/xfs/xfs_file.c xfs_file_mmap_prepare( · line 2203
Quoted 5 lines
fs/xfs/xfs_file.c +5
-0+struct xfs_inode *ip =XFS_I (file_inode (file)); + +return iomap_file_init_dma_buf_io_ctx (file, +xfs_inode_buftarg (ip)->bt_bdev, ctx); +}Ah, this is where the previous cross check comes from. What about simply disallowing toggling the RT flag with outstanding reservations instead?
So, instead of patch 2, this is about setting a new in-core inode flag here iff the registration was successful. And checking this new flag to refuse toggling RT?
Re: [RFC 4/4] xfs: support dma-buf registered buffers for direct I/O
In reply to Christoph HellwigQuoted 37 lines · Christoph Hellwig
Quoted 24 lines · Anuj Gupta
From: Nitesh Shetty <[email protected]>
Add the
init_dma_buf_io_ctxfop for XFS files. XFS picks the device and calls the iomap layer with the relevant device.Refuse the iterator from every CPU-copy path: DAX, buffered fallback from a
-ENOTBLKdirect write, and read/writes withoutO_DIRECT.If a file is switched between the data and realtime devices after registration, the block layer cross-device check refuses the I/O.
Signed-off-by Nitesh Shetty <[email protected]>--- fs/xfs/xfs_file.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+)fs/xfs/xfs_file.c +0
-0fs/xfs/xfs_file.c xfs_file_read_iter( · line 346
Quoted 7 lines
fs/xfs/xfs_file.c +4
-0@@ -340,6 +340,11 @@ xfs_file_read_iter(if (xfs_is_shutdown (mp))return -EIO ; +/* a device mapping can only be read by a direct I/O bio */ +if (iov_iter_is_dmabuf_map (to) && + (IS_DAX (inode) || !(iocb->ki_flags &IOCB_DIRECT ))) +return -EOPNOTSUPP ;Maybe move this into the actual methods for dax/buffered instead to avoid extra conditionals? Same for the write side.
Quoted 1 line
Quoted 10 lines
fs/xfs/xfs_file.c xfs_file_write_iter( · line 1224
Quoted 7 lines
fs/xfs/xfs_file.c +3
-0@@ -1209,6 +1219,9 @@ xfs_file_write_iter( ret =xfs_file_dio_write (iocb, from);if (ret != -ENOTBLK )return ret; +/* the page cache cannot copy from a device mapping */ +if (iov_iter_is_dmabuf_map (from)) +return -EOPNOTSUPP ;I suspect these random fallbacks are going to cause trouble, but I'm not really sure what to do about it either.
Folks did mention it to me before. For a slow path we can try do something like amdxdna_cbuf_clear() does:
dma_buf_vmap(dbuf, &vmap);
if (!vmap.vaddr)
return -EFAULT;
memset(vmap.vaddr, 0, dbuf->size);dma_buf_vunmap(dbuf, &vmap);
But I can already imagine it triggering some interesting behaviour like buffer migration, which will be too penalising for things like clear on a hole. And we'll need to be careful about deadlocking here.
Signature · 1 line
Pavel Begunkov
Re: [RFC 4/4] xfs: support dma-buf registered buffers for direct I/O
In reply to Christoph HellwigQuoted 11 lines
Quoted 11 lines
fs/xfs/xfs_file.c +11
-0+/* +* A realtime file's data lives on the realtime device, anything else on the +* data device. If a file is switched between them after registration, the +* block layer cross-device check refuses the I/O. +*/ +static int +xfs_file_init_dma_buf_io_ctx ( +struct file *file, +struct dma_buf_io_ctx *ctx) +{ +struct xfs_inode *ip =XFS_I (file_inode (file));
Quoted 8 lines
fs/xfs/xfs_file.c xfs_file_mmap_prepare( · line 2203
Quoted 4 lines
fs/xfs/xfs_file.c +4
-0+ +return iomap_file_init_dma_buf_io_ctx (file, +xfs_inode_buftarg (ip)->bt_bdev, ctx); +}Ah, this is where the previous cross check comes from. What about simply disallowing toggling the RT flag with outstanding reservations instead?
That works for the current exact-file scheme, but not with Pavel's shared-context proposal[1], since other files can use the buffer without registering it. There we can validate the context tag against the inode's current backing device per I/O.
Dear patch submitter,
Blktests CI has tested the following submission:
Status: FAILURE Name: [RFC,0/4] fs: support dma-buf backed direct I/O Patchwork: https://patchwork.kernel.org/series/1187147/&state=* Run record: https://github.com/linux-blktests/linux-block/actions/runs/38999893251
Failed test cases: md/001
Quoted 10 lines · On Oct 17, 2026 / 15:54, [email protected] wrote:
Dear patch submitter,
Blktests CI has tested the following submission:
Status: FAILURE Name: [RFC,0/4] fs: support dma-buf backed direct I/O Patchwork: https://patchwork.kernel.org/series/1187147/&state=* Run record: https://github.com/linux-blktests/linux-block/actions/runs/38999893251Failed test cases: md/001
Please ignore this failure report. I believe the failure cause of the md/001 failure is in a different patch for nvme-tcp driver [*].
[*] https://lore.kernel.org/linux-nvme/atSzF3URmcpP2_i2@shinmob/
Quoted 3 lines · Anuj Gupta
This series lets an application register a dma-buf against a file, so reads and writes go directly to the device as dma-buf backed bios with no per-command DMA mapping.
Great to see work done on the filesystem side!
For filesystems to be useful, we'll first need to make a registered buffer work with all files of the fs and not only the one we used for the registration. I think about something like below. I'll clean it up and send out later.
commit 11e25bf0dbce227f87b81cb9436ad347de0deb06
Author: Pavel Begunkov <[email protected]> Date: Sun Oct 18 23:09:44 2026 +0100
dma-buf-io: allow compatible file to reuse the context
Currently, we limit the use of a dma-buf I/O context to the file it was created from. It might be fine for block device files but won't work well with file systems as we need to support multiple files. Let the importing subsystem to set an opaque handle to file and indirectly to the dma-buf I/O context, and use it to allow only compatible files.
block/fops.c +1 -0
@@ -685,6 +685,7 @@ static int blkdev_open(struct inode *inode, struct file *filp) ret =bdev_open (bdev, mode, filp->private_data,NULL , filp);if (ret)blkdev_put_no_open (bdev); + filp->f_dma_io_tag = (unsigned long )bdev;return ret; }
drivers/dma-buf/dma-buf-io.c +3 -0
@@ -166,12 +166,15 @@ int dma_buf_io_ctx_create(struct file *file,if (!file->f_op->init_dma_buf_io_ctx)return -EOPNOTSUPP ; +if (WARN_ON (!file->f_dma_io_tag)) +return -EFAULT ; ctx =kzalloc_obj (*ctx);if (!ctx)return -ENOMEM ; ctx->dir = dir; ctx->dmabuf = dmabuf; + ctx->importer_tag = file->f_dma_io_tag;get_dma_buf (dmabuf); ret = file->f_op->init_dma_buf_io_ctx (file, ctx);
include/linux/dma-buf-io.h +9 -0
@@ -48,6 +48,15 @@ struct dma_buf_io_ctx {enum dma_data_direction dir;bool is_dead; +/* +* A tag uniqiue to the importing subsystem, which used for filtering +* out files incompatible with the dma-buf I/O context by matching it +* against struct file::f_dma_io_tag. It encodes both the set of +* devices it has been registered for and the subsystem specific +* map / context structures representation. +*/ +unsigned long importer_tag; +void *dev_priv;const struct dma_buf_io_ops *dev_ops; };
include/linux/fs.h +6 -0
@@ -1292,6 +1292,12 @@ struct file { };file_ref_t f_ref;/* --- cacheline 3 boundary (192 bytes) --- */ + +/* +* A tag representing the underlying subsystem for dma-buf I/O. +* See struct dma_buf_io_ctx::importer_tag. +*/ +unsigned long f_dma_io_tag; }__randomize_layout __attribute__((aligned(4))); /* lest something weird decides that 2 is OK */
io_uring/dma-buf.c +1 -2
@@ -4,7 +4,6 @@ static inline void io_release_dmabuf(void *priv) {struct io_buf_dma *bd = priv; -fput (bd->target_file);dma_buf_io_ctx_release (bd->ctx);kfree (bd); }
@@ -52,13 +51,13 @@ int __io_register_dmabuf(struct io_ring_ctx *ctx,if (ret) goto err; - bd->target_file = target_file; imu->len = dmabuf->size; imu->release = io_release_dmabuf; imu->priv = bd; imu->flags =IO_REGBUF_F_DMABUF ; imu->dir =IO_BUF_DEST |IO_BUF_SOURCE ;dma_buf_put (dmabuf); +fput (target_file);return 0 ; err:kfree (bd);
io_uring/dma-buf.h +1 -2
@@ -9,7 +9,6 @@struct io_buf_dma {struct dma_buf_io_ctx *ctx; -struct file *target_file; }; int __io_register_dmabuf(struct io_ring_ctx *ctx,
@@ -57,7 +56,7 @@ static inline int io_import_dmabuf_map(struct io_kiocb *req,if (!IS_ENABLED (CONFIG_DMA_SHARED_BUFFER ))return -EOPNOTSUPP ; -if (req->file != bd->target_file) +if (req->file->f_dma_io_tag != bd->ctx->importer_tag)return -EBADF ; req->flags |=REQ_F_DMABUF ;iov_iter_dmabuf_map (iter, ddir,NULL , offset, len);
Signature · 1 line
Pavel Begunkov
include/linux/fs.h struct file { · line 1301
Quoted 9 lines · Pavel Begunkov
include/linux/fs.h +6
-0file_ref_t f_ref;/* --- cacheline 3 boundary (192 bytes) --- */ + +/* +* A tag representing the underlying subsystem for dma-buf I/O. +* See struct dma_buf_io_ctx::importer_tag. +*/ +unsigned long f_dma_io_tag; } __randomize_layout
Growing strut file into a new cacheline just for dmabufs is a no-go..
Also note that a file can be placed on multiple block devices when using RAID or just btrfs placements (not sure about f2fs).
Quoted 12 lines · Christoph Hellwig
include/linux/fs.h struct file { · line 1301
Quoted 9 lines · Pavel Begunkov
file_ref_tf_ref;
/* --- cacheline 3 boundary (192 bytes) --- */include/linux/fs.h +6
-0+ +/* +* A tag representing the underlying subsystem for dma-buf I/O. +* See struct dma_buf_io_ctx::importer_tag. +*/ +unsigned long f_dma_io_tag; } __randomize_layoutGrowing strut file into a new cacheline just for dmabufs is a no-go..
I didn't check last time but there is space to spare. Maybe I don't know about all production enabled options growing mutexes.
struct file {
spinlock_t f_lock __attribute__((__aligned__(4))); /* 0 4 */
fmode_t f_mode; /* 4 4 */
const struct file_operations * f_op; /* 8 8 */
struct address_space * f_mapping; /* 16 8 */
void * private_data; /* 24 8 */
struct inode * f_inode; /* 32 8 */
unsigned int f_flags; /* 40 4 */
unsigned int f_iocb_flags; /* 44 4 */
const struct cred * f_cred; /* 48 8 */
struct fown_struct * f_owner; /* 56 8 */
/* --- cacheline 1 boundary (64 bytes) --- */ union {
const struct path f_path; /* 64 16 */
struct path __f_path; /* 64 16 */
}; /* 64 16 */
union {
struct mutex f_pos_lock __attribute__((__aligned__(8))); /* 80 24 */
u64 f_pipe; /* 80 8 */
} __attribute__((__aligned__(8))); /* 80 24 */
loff_t f_pos; /* 104 8 */
void * f_security; /* 112 8 */
errseq_t f_wb_err; /* 120 4 */
errseq_t f_sb_err; /* 124 4 */
/* --- cacheline 2 boundary (128 bytes) --- */
struct hlist_head * f_ep; /* 128 8 */
union {
struct callback_head f_task_work __attribute__((__aligned__(8))); /* 136 16 */
struct llist_node f_llist; /* 136 8 */
struct file_ra_state f_ra; /* 136 32 */
freeptr_t f_freeptr; /* 136 8 */
} __attribute__((__aligned__(8))); /* 136 32 */
file_ref_t f_ref; /* 168 8 */
/* size: 176, cachelines: 3, members: 19 */ /* forced alignments: 3 */
/* last cacheline: 48 bytes */
} __attribute__((__aligned__(8)));
Quoted 2 lines
Also note that a file can be placed on multiple block devices when using RAID or just btrfs placements (not sure about f2fs).
Filesystems should set it to a super block or something similar. It's "unsigned long" for that exact reason. Shouldn't be a problem for md either.
Signature · 1 line
Pavel Begunkov
Christian Brauner
maintainerQuoted 2 lines · Pavel Begunkov
I didn't check last time but there is space to spare. Maybe I don't know about all production enabled options growing mutexes.
Meh, I agree with Christoph. We have rejected similar proposal from other users in the past that just needed a little bit of that spare space.
The bytes that are left in the last cacheline are for things that are useful for every file and not for a rather nieche feature.
Quoted 3 lines
Filesystems should set it to a super block or something similar. It's "unsigned long" for that exact reason. Shouldn't be a problem for md either.
I don't think any of this needs to be in struct file. There are creative ways to work around this. We draw a hard line when bloating struct file. Sorry.