A dma-buf I/O context can only be used with the file it was created on. File systems need to share it between files on the same device. Add a dma_buf_io_tag() file operation. It returns a tag for the device a file does I/O to, or 0 if the file can't do dma-buf I/O. The context stores the tag of the creating file and can be used with any file with the same tag. A file's tag can change after open, e.g. when O_DIRECT is cleared, so compare it on every I/O. Based on a patch by Pavel Begunkov: https://lore.kernel.org/all/7165ab453cc680276b88f47f9ae89bc4967543cd.1793751001.git.asml.silence@gmail.com/ Signed-off-by: Anuj Gupta Signed-off-by: Nitesh Shetty --- include/linux/dma-buf-io.h | 19 +++++++++++++++++++ include/linux/fs.h | 1 + 2 files changed, 20 insertions(+) diff --git a/include/linux/dma-buf-io.h b/include/linux/dma-buf-io.h index d9f27650a851..0f5d391706cf 100644 --- a/include/linux/dma-buf-io.h +++ b/include/linux/dma-buf-io.h @@ -48,10 +48,29 @@ struct dma_buf_io_ctx { enum dma_data_direction dir; bool is_dead; + /* set by the importer, matched against ->dma_buf_io_tag() */ + u64 importer_tag; + void *dev_priv; const struct dma_buf_io_ops *dev_ops; }; +/* The tag can change after open, so check it for every I/O. */ +static inline u64 dma_buf_io_file_tag(struct file *file) +{ + if (!file->f_op->dma_buf_io_tag || !(file->f_flags & O_DIRECT)) + return 0; + return file->f_op->dma_buf_io_tag(file); +} + +static inline bool dma_buf_io_file_compatible(const struct dma_buf_io_ctx *ctx, + struct file *file) +{ + u64 tag = dma_buf_io_file_tag(file); + + return tag && tag == ctx->importer_tag; +} + int dma_buf_io_ctx_create(struct file *file, struct dma_buf *dmabuf, enum dma_data_direction dir, diff --git a/include/linux/fs.h b/include/linux/fs.h index 5b3a100aba77..48c1a2c95ba8 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -1965,6 +1965,7 @@ struct file_operations { unsigned int poll_flags); int (*mmap_prepare)(struct vm_area_desc *); int (*init_dma_buf_io_ctx)(struct file *, struct dma_buf_io_ctx *); + u64 (*dma_buf_io_tag)(struct file *file); } __randomize_layout; /* Supports async buffered reads */ -- 2.47.3