Use the disk sequence number as the tag of block device files.
It is never reused, so a new disk can't match an old registration.

Based on a patch by Pavel Begunkov:
https://lore.kernel.org/all/7165ab453cc680276b88f47f9ae89bc4967543cd.1793751001.git.asml.sile...@gmail.com/

Signed-off-by: Anuj Gupta <[email protected]>
Signed-off-by: Nitesh Shetty <[email protected]>
---
 block/bdev.c           |  6 +++++-
 block/fops.c           |  6 ++++++
 include/linux/blkdev.h | 15 +++++++++++++++
 3 files changed, 26 insertions(+), 1 deletion(-)

diff --git a/block/bdev.c b/block/bdev.c
index a949b3d0d97e..d5ea32a6217c 100644
--- a/block/bdev.c
+++ b/block/bdev.c
@@ -75,12 +75,16 @@ int bdev_init_dma_buf_io_ctx(struct block_device *bdev,
                             struct dma_buf_io_ctx *ctx)
 {
        struct gendisk *disk = bdev->bd_disk;
+       int ret;
 
        if (ctx->dmabuf->size > UINT_MAX)
                return -EINVAL;
        if (!disk->fops->init_dma_buf_io_ctx)
                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->importer_tag = bdev_dma_buf_io_tag(bdev);
+       return ret;
 }
 EXPORT_SYMBOL_GPL(bdev_init_dma_buf_io_ctx);
 
diff --git a/block/fops.c b/block/fops.c
index f017bd1e01b2..f723220194a4 100644
--- a/block/fops.c
+++ b/block/fops.c
@@ -1009,6 +1009,11 @@ static int blkdev_init_dma_buf_io_ctx(struct file *file,
        return bdev_init_dma_buf_io_ctx(file_bdev(file), ctx);
 }
 
+static u64 blkdev_dma_buf_io_tag(struct file *file)
+{
+       return bdev_dma_buf_io_tag(file_bdev(file));
+}
+
 const struct file_operations def_blk_fops = {
        .open           = blkdev_open,
        .release        = blkdev_release,
@@ -1028,6 +1033,7 @@ const struct file_operations def_blk_fops = {
        .uring_cmd      = blkdev_uring_cmd,
        .fop_flags      = FOP_BUFFER_RASYNC | FOP_DONTCACHE,
        .init_dma_buf_io_ctx = blkdev_init_dma_buf_io_ctx,
+       .dma_buf_io_tag = blkdev_dma_buf_io_tag,
 };
 
 static __init int blkdev_init(void)
diff --git a/include/linux/blkdev.h b/include/linux/blkdev.h
index 8b69a66b67df..a9a3bc618266 100644
--- a/include/linux/blkdev.h
+++ b/include/linux/blkdev.h
@@ -1677,6 +1677,21 @@ struct block_device_operations {
 int bdev_init_dma_buf_io_ctx(struct block_device *bdev,
                             struct dma_buf_io_ctx *ctx);
 
+/**
+ * bdev_dma_buf_io_tag - get the dma-buf I/O tag of a block device
+ * @bdev: block device to query
+ *
+ * The tag is the disk sequence number, which is never reused.
+ *
+ * Return: the tag, or 0 if the disk doesn't support dma-buf I/O.
+ */
+static inline u64 bdev_dma_buf_io_tag(struct block_device *bdev)
+{
+       if (!bdev->bd_disk->fops->init_dma_buf_io_ctx)
+               return 0;
+       return bdev->bd_disk->diskseq;
+}
+
 #ifdef CONFIG_COMPAT
 extern int blkdev_compat_ptr_ioctl(struct block_device *, blk_mode_t,
                                      unsigned int, unsigned long);
-- 
2.47.3

Reply via email to