cover-letter: Allow MMIO regions to be exported through dmabuf
Subsystems (4)
cover cover-letter: Allow MMIO regions to be exported through dmabuf
From: Wei Lin Guay <[email protected]>
This is another attempt to revive the patches posted by Jason Gunthorpe and Vivek Kasireddy, at
https://patchwork.kernel.org/project/linux-media/cover/[email protected]/
https://lwn.net/Articles/970751/
In addition to the initial proposal by Jason, another promising application is exposing memory from an AI accelerator (bound to VFIO) to an RDMA device. This would allow the RDMA device to directly access the accelerator's memory, thereby facilitating direct data transactions between the RDMA device and the accelerator.
Below is from the text/motivation from the orginal cover letter.
dma-buf has become a way to safely acquire a handle to non-struct page memory that can still have lifetime controlled by the exporter. Notably RDMA can now import dma-buf FDs and build them into MRs which allows for PCI P2P operations. Extend this to allow vfio-pci to export MMIO memory from PCI device BARs.
This series supports a use case for SPDK where a NVMe device will be owned by SPDK through VFIO but interacting with a RDMA device. The RDMA device may directly access the NVMe CMB or directly manipulate the NVMe device's doorbell using PCI P2P.
However, as a general mechanism, it can support many other scenarios with VFIO. I imagine this dmabuf approach to be usable by iommufd as well for generic and safe P2P mappings.
This series goes after the "Break up ioctl dispatch functions to one function per ioctl" series.
v2:
- Name the new file dma_buf.c
- Restore orig_nents before freeing
- Fix reversed logic around priv->revoked
- Set priv->index
- Rebased on v2 "Break up ioctl dispatch functions"
v1: https://lore.kernel.org/r/[email protected]
Jason Gunthorpe (3):
vfio: Add vfio_device_get()
dma-buf: Add dma_buf_try_get()
vfio/pci: Allow MMIO regions to be exported through dma-buf
Wei Lin Guay (1):
vfio/pci: Allow export dmabuf without move_notify from importer
drivers/vfio/pci/Makefile | 1 + drivers/vfio/pci/dma_buf.c | 291 +++++++++++++++++++++++++++++ drivers/vfio/pci/vfio_pci_config.c | 8 +- drivers/vfio/pci/vfio_pci_core.c | 44 ++++- drivers/vfio/pci/vfio_pci_priv.h | 30 +++ drivers/vfio/vfio_main.c | 1 + include/linux/dma-buf.h | 13 ++ include/linux/vfio.h | 6 + include/linux/vfio_pci_core.h | 1 + include/uapi/linux/vfio.h | 18 ++ 10 files changed, 405 insertions(+), 8 deletions(-) create mode 100644 drivers/vfio/pci/dma_buf.c
--
2.43.5
PATCH 1/4 vfio: Add vfio_device_get()
From: Jason Gunthorpe <[email protected]>
Summary:
To increment a reference the caller already holds. Export vfio_device_put() to pair with it.
--- drivers/vfio/vfio_main.c | 1 + include/linux/vfio.h | 6 ++++++ 2 files changed, 7 insertions(+)
drivers/vfio/vfio_main.c +1 -0
@@ -171,6 +171,7 @@ void vfio_device_put_registration(struct vfio_device *device)if (refcount_dec_and_test (&device->refcount))complete (&device->comp); } +EXPORT_SYMBOL_GPL (vfio_device_put_registration);bool vfio_device_try_get_registration (struct vfio_device *device) {
include/linux/vfio.h +6 -0
@@ -279,6 +279,12 @@ static inline void vfio_put_device(struct vfio_device *device)int vfio_register_group_dev (struct vfio_device *device);int vfio_register_emulated_iommu_dev (struct vfio_device *device);void vfio_unregister_group_dev (struct vfio_device *device); +void vfio_device_put_registration (struct vfio_device *device); + +static inline void vfio_device_get (struct vfio_device *device) +{ +refcount_inc (&device->refcount); +}int vfio_assign_device_set (struct vfio_device *device,void *set_id);unsigned int vfio_device_set_open_count (struct vfio_device_set *dev_set);
--
2.43.5
PATCH 2/4 dma-buf: Add dma_buf_try_get()
In reply to Wei Lin GuayFrom: Jason Gunthorpe <[email protected]>
Summary:
Used to increment the refcount of the dma buf's struct file, only if the refcount is not zero. Useful to allow the struct file's lifetime to control the lifetime of the dmabuf while still letting the driver to keep track of created dmabufs.
--- include/linux/dma-buf.h | 13 +++++++++++++ 1 file changed, 13 insertions(+)
include/linux/dma-buf.h +13 -0
@@ -614,6 +614,19 @@ int dma_buf_fd(struct dma_buf *dmabuf, int flags);struct dma_buf *dma_buf_get (int fd);void dma_buf_put (struct dma_buf *dmabuf); +/** +* dma_buf_try_get - try to get a reference on a dmabuf +* @dmabuf - the dmabuf to get +* +* Returns true if a reference was successfully obtained. The caller must +* interlock with the dmabuf's release function in some way, such as RCU, to +* ensure that this is not called on freed memory. +*/ +static inline bool dma_buf_try_get (struct dma_buf *dmabuf) +{ +return get_file_rcu (&dmabuf->file); +} +struct sg_table *dma_buf_map_attachment (struct dma_buf_attachment *,enum dma_data_direction ); void dma_buf_unmap_attachment(struct dma_buf_attachment *, struct sg_table *,
--
2.43.5
PATCH 3/4 vfio/pci: Allow MMIO regions to be exported through dma-buf
In reply to Wei Lin GuayFrom: Jason Gunthorpe <[email protected]>
Summary:
dma-buf has become a way to safely acquire a handle to non-struct page memory that can still have lifetime controlled by the exporter. Notably RDMA can now import dma-buf FDs and build them into MRs which allows for PCI P2P operations. Extend this to allow vfio-pci to export MMIO memory from PCI device BARs.
The patch design loosely follows the pattern in commit db1a8dd916aa ("habanalabs: add support for dma-buf exporter") except this does not support pinning.
Instead, this implements what, in the past, we've called a revocable attachment using move. In normal situations the attachment is pinned, as a BAR does not change physical address. However when the VFIO device is closed, or a PCI reset is issued, access to the MMIO memory is revoked.
Revoked means that move occurs, but an attempt to immediately re-map the memory will fail. In the reset case a future move will be triggered when MMIO access returns. As both close and reset are under userspace control it is expected that userspace will suspend use of the dma-buf before doing these operations, the revoke is purely for kernel self-defense against a hostile userspace.
--- drivers/vfio/pci/Makefile | 1 + drivers/vfio/pci/dma_buf.c | 269 +++++++++++++++++++++++++++++ drivers/vfio/pci/vfio_pci_config.c | 8 +- drivers/vfio/pci/vfio_pci_core.c | 28 ++- drivers/vfio/pci/vfio_pci_priv.h | 23 +++ include/linux/vfio_pci_core.h | 1 + include/uapi/linux/vfio.h | 18 ++ 7 files changed, 340 insertions(+), 8 deletions(-) create mode 100644 drivers/vfio/pci/dma_buf.c
drivers/vfio/pci/Makefile +1 -0
@@ -2,6 +2,7 @@ vfio-pci-core-y := vfio_pci_core.o vfio_pci_intrs.o vfio_pci_rdwr.o vfio_pci_config.o vfio-pci-core-$(CONFIG_VFIO_PCI_ZDEV_KVM) += vfio_pci_zdev.o +vfio-pci-core-$(CONFIG_DMA_SHARED_BUFFER) += dma_buf.o obj-$(CONFIG_VFIO_PCI_CORE) += vfio-pci-core.o vfio-pci-y := vfio_pci.o
drivers/vfio/pci/dma_buf.c new file +269 -0
269 lines not on this page · raw message
drivers/vfio/pci/vfio_pci_config.c +6 -2
@@ -585,10 +585,12 @@ static int vfio_basic_config_write(struct vfio_pci_core_device *vdev, int pos, virt_mem = !!(le16_to_cpu (*virt_cmd) &PCI_COMMAND_MEMORY ); new_mem = !!(new_cmd &PCI_COMMAND_MEMORY ); -if (!new_mem) +if (!new_mem) {vfio_pci_zap_and_down_write_memory_lock (vdev); -else +vfio_pci_dma_buf_move (vdev, true); + }else {down_write (&vdev->memory_lock); + }/* * If the user is writing mem/io enable (new_mem/io) and we
@@ -623,6 +625,8 @@ static int vfio_basic_config_write(struct vfio_pci_core_device *vdev, int pos, *virt_cmd &=cpu_to_le16 (~mask); *virt_cmd |=cpu_to_le16 (new_cmd & mask); +if (__vfio_pci_memory_enabled (vdev)) +vfio_pci_dma_buf_move (vdev, false);up_write (&vdev->memory_lock); }
drivers/vfio/pci/vfio_pci_core.c +22 -6
@@ -700,6 +700,8 @@ void vfio_pci_core_close_device(struct vfio_device *core_vdev)#endif vfio_pci_core_disable (vdev); +vfio_pci_dma_buf_cleanup (vdev); +mutex_lock (&vdev->igate);if (vdev->err_trigger) {eventfd_ctx_put (vdev->err_trigger);
@@ -1244,7 +1246,10 @@ static int vfio_pci_ioctl_reset(struct vfio_pci_core_device *vdev,*/ vfio_pci_set_power_state (vdev,PCI_D0 ); +vfio_pci_dma_buf_move (vdev, true); ret =pci_try_reset_function (vdev->pdev); +if (__vfio_pci_memory_enabled (vdev)) +vfio_pci_dma_buf_move (vdev, false);up_write (&vdev->memory_lock);return ret;
@@ -1490,11 +1495,10 @@ long vfio_pci_core_ioctl(struct vfio_device *core_vdev, unsigned int cmd, }EXPORT_SYMBOL_GPL (vfio_pci_core_ioctl); -static int vfio_pci_core_feature_token (struct vfio_device *device,u32 flags, -uuid_t __user *arg,size_t argsz) +static int vfio_pci_core_feature_token (struct vfio_pci_core_device *vdev, +u32 flags,uuid_t __user *arg, +size_t argsz) { -struct vfio_pci_core_device *vdev = - container_of(device, struct vfio_pci_core_device, vdev);uuid_t uuid;int ret;
@@ -1521,6 +1525,9 @@ static int vfio_pci_core_feature_token(struct vfio_device *device, u32 flags,int vfio_pci_core_ioctl_feature (struct vfio_device *device,u32 flags,void __user *arg,size_t argsz) { +struct vfio_pci_core_device *vdev = + container_of(device, struct vfio_pci_core_device, vdev); +switch (flags &VFIO_DEVICE_FEATURE_MASK ) {case VFIO_DEVICE_FEATURE_LOW_POWER_ENTRY :return vfio_pci_core_pm_entry (device, flags, arg, argsz);
@@ -1530,7 +1537,9 @@ int vfio_pci_core_ioctl_feature(struct vfio_device *device, u32 flags,case VFIO_DEVICE_FEATURE_LOW_POWER_EXIT :return vfio_pci_core_pm_exit (device, flags, arg, argsz);case VFIO_DEVICE_FEATURE_PCI_VF_TOKEN : -return vfio_pci_core_feature_token (device, flags, arg, argsz); +return vfio_pci_core_feature_token (vdev, flags, arg, argsz); +case VFIO_DEVICE_FEATURE_DMA_BUF : +return vfio_pci_core_feature_dma_buf (vdev, flags, arg, argsz);default :return -ENOTTY ; }
@@ -2083,6 +2092,7 @@ int vfio_pci_core_init_dev(struct vfio_device *core_vdev)INIT_LIST_HEAD (&vdev->sriov_pfs_item);init_rwsem (&vdev->memory_lock);xa_init (&vdev->ctx); +INIT_LIST_HEAD (&vdev->dmabufs);return 0 ; }
@@ -2463,11 +2473,17 @@ static int vfio_pci_dev_set_hot_reset(struct vfio_device_set *dev_set,* cause the PCI config space reset without restoring the original * state (saved locally in 'vdev->pm_save'). */ -list_for_each_entry (vdev, &dev_set->device_list, vdev.dev_set_list) +list_for_each_entry (vdev, &dev_set->device_list, vdev.dev_set_list) { +vfio_pci_dma_buf_move (vdev, true);vfio_pci_set_power_state (vdev,PCI_D0 ); + } ret =pci_reset_bus (pdev); +list_for_each_entry (vdev, &dev_set->device_list, vdev.dev_set_list) +if (__vfio_pci_memory_enabled (vdev)) +vfio_pci_dma_buf_move (vdev, false); + vdev =list_last_entry (&dev_set->device_list, struct vfio_pci_core_device, vdev.dev_set_list);
drivers/vfio/pci/vfio_pci_priv.h +23 -0
@@ -101,4 +101,27 @@ static inline bool vfio_pci_is_vga(struct pci_dev *pdev) return (pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA; } +#ifdef CONFIG_DMA_SHARED_BUFFER +int vfio_pci_core_feature_dma_buf (struct vfio_pci_core_device *vdev,u32 flags, +struct vfio_device_feature_dma_buf __user *arg, +size_t argsz); +void vfio_pci_dma_buf_cleanup (struct vfio_pci_core_device *vdev); +void vfio_pci_dma_buf_move (struct vfio_pci_core_device *vdev,bool revoked); +#else +static int +vfio_pci_core_feature_dma_buf (struct vfio_pci_core_device *vdev,u32 flags, +struct vfio_device_feature_dma_buf __user *arg, +size_t argsz) +{ +return -ENOTTY ; +} +static inline void vfio_pci_dma_buf_cleanup (struct vfio_pci_core_device *vdev) +{ +} +static inline void vfio_pci_dma_buf_move (struct vfio_pci_core_device *vdev, +bool revoked) +{ +} +#endif +#endif
include/linux/vfio_pci_core.h +1 -0
@@ -94,6 +94,7 @@ struct vfio_pci_core_device {struct vfio_pci_core_device *sriov_pf_core_dev;struct notifier_block nb;struct rw_semaphore memory_lock; +struct list_head dmabufs; };/* Will be exported for vfio pci drivers usage */
include/uapi/linux/vfio.h +18 -0
@@ -1458,6 +1458,24 @@ struct vfio_device_feature_bus_master { };#define VFIO_DEVICE_FEATURE_BUS_MASTER 10 +/** +* Upon VFIO_DEVICE_FEATURE_GET create a dma_buf fd for the +* region selected. +* +* open_flags are the typical flags passed to open(2), eg O_RDWR, O_CLOEXEC, +* etc. offset/length specify a slice of the region to create the dmabuf from. +* If both are 0 then the whole region is used. +* +* Return: The fd number on success, -1 and errno is set on failure. +*/ +struct vfio_device_feature_dma_buf { +__u32 region_index; +__u32 open_flags; +__u32 offset; +__u64 length; +}; +#define VFIO_DEVICE_FEATURE_DMA_BUF 11 +/* -------- API for Type1 VFIO IOMMU -------- */ /**
--
2.43.5
PATCH 4/4 vfio/pci: Allow export dmabuf without move_notify from importer
In reply to Wei Lin GuayFrom: Wei Lin Guay <[email protected]>
Summary:
Support vfio to export dmabuf to importer such as RDMA NIC that does not support move_notify callback, since not all RDMA driver support on-demand-paging (ODP).
There are some use-cases such as bind accelerator that always pinned the device memory via vfio and export it to RDMA NIC such as EFA, BNXT_RE or IRDMA that does not support ODP.
--- drivers/vfio/pci/dma_buf.c | 32 +++++++++++++++++++++++++++----- drivers/vfio/pci/vfio_pci_core.c | 16 ++++++++++++++++ drivers/vfio/pci/vfio_pci_priv.h | 7 +++++++ 3 files changed, 50 insertions(+), 5 deletions(-)
drivers/vfio/pci/dma_buf.c +27 -5
@@ -17,6 +17,7 @@ struct vfio_pci_dma_buf {unsigned int orig_nents;size_t offset;bool revoked; +bool pinned; }; static int vfio_pci_dma_buf_attach(struct dma_buf *dmabuf,
@@ -32,17 +33,38 @@ static int vfio_pci_dma_buf_attach(struct dma_buf *dmabuf,return 0 ; } +bool vfio_pci_dma_buf_pinned (struct vfio_pci_core_device *vdev) +{ +struct vfio_pci_dma_buf *priv; +struct vfio_pci_dma_buf *tmp; +bool pinned = false; + +down_write (&vdev->memory_lock); +list_for_each_entry_safe (priv, tmp, &vdev->dmabufs, dmabufs_elm) { +if (!dma_buf_try_get (priv->dmabuf)) +continue ; +if (priv->pinned) { + pinned = true; +break ; + } + } +up_write (&vdev->memory_lock); +return pinned; +} +static void vfio_pci_dma_buf_unpin (struct dma_buf_attachment *attachment) { +struct vfio_pci_dma_buf *priv = attachment->dmabuf->priv; + + priv->pinned = false; }static int vfio_pci_dma_buf_pin (struct dma_buf_attachment *attachment) { -/* -* Uses the dynamic interface but must always allow for -* dma_buf_move_notify() to do revoke -*/ -return -EINVAL ; +struct vfio_pci_dma_buf *priv = attachment->dmabuf->priv; + + priv->pinned = true; +return 0 ; } static struct sg_table *
drivers/vfio/pci/vfio_pci_core.c +16 -0
@@ -1246,6 +1246,13 @@ static int vfio_pci_ioctl_reset(struct vfio_pci_core_device *vdev,*/ vfio_pci_set_power_state (vdev,PCI_D0 ); +/* +* prevent reset if dma_buf is pinned to avoid stale pinned +* expose to the dmabuf exporter. +*/ +if (vfio_pci_dma_buf_pinned (vdev)) +return -EINVAL ; +vfio_pci_dma_buf_move (vdev, true); ret =pci_try_reset_function (vdev->pdev);if (__vfio_pci_memory_enabled (vdev))
@@ -2444,6 +2451,15 @@ static int vfio_pci_dev_set_hot_reset(struct vfio_device_set *dev_set,break ; } +/* +* prevent reset if dma_buf is pinned to avoid stale pinned +* expose to the dmabuf exporter. +*/ +if (vfio_pci_dma_buf_pinned (vdev)) { + ret = -EINVAL ; +break ; + } +/* * Take the memory write lock for each device and zap BAR * mappings to prevent the user accessing the device while in
drivers/vfio/pci/vfio_pci_priv.h +7 -0
@@ -107,6 +107,7 @@ int vfio_pci_core_feature_dma_buf(struct vfio_pci_core_device *vdev, u32 flags, size_t argsz);void vfio_pci_dma_buf_cleanup (struct vfio_pci_core_device *vdev);void vfio_pci_dma_buf_move (struct vfio_pci_core_device *vdev,bool revoked); +bool vfio_pci_dma_buf_pinned (struct vfio_pci_core_device *vdev);#else static int vfio_pci_core_feature_dma_buf(struct vfio_pci_core_device *vdev, u32 flags,
@@ -115,6 +116,12 @@ vfio_pci_core_feature_dma_buf(struct vfio_pci_core_device *vdev, u32 flags, {return -ENOTTY ; } + +static inline bool vfio_pci_dma_buf_pinned (struct vfio_pci_core_device *vdev) +{ +return false; +} +static inline void vfio_pci_dma_buf_cleanup (struct vfio_pci_core_device *vdev) { }
--
2.43.5
Christian König
maintainerAm 16.12.24 um 10:54 schrieb Wei Lin Guay:
Quoted 26 lines
From: Wei Lin Guay <[email protected]>
This is another attempt to revive the patches posted by Jason Gunthorpe and Vivek Kasireddy, at
https://patchwork.kernel.org/project/linux-media/cover/[email protected]/
https://lwn.net/Articles/970751/In addition to the initial proposal by Jason, another promising application is exposing memory from an AI accelerator (bound to VFIO) to an RDMA device. This would allow the RDMA device to directly access the accelerator's memory, thereby facilitating direct data transactions between the RDMA device and the accelerator.
Below is from the text/motivation from the orginal cover letter.
dma-buf has become a way to safely acquire a handle to non-struct page memory that can still have lifetime controlled by the exporter. Notably RDMA can now import dma-buf FDs and build them into MRs which allows for PCI P2P operations. Extend this to allow vfio-pci to export MMIO memory from PCI device BARs.
This series supports a use case for SPDK where a NVMe device will be owned by SPDK through VFIO but interacting with a RDMA device. The RDMA device may directly access the NVMe CMB or directly manipulate the NVMe device's doorbell using PCI P2P.
Quoted 6 lines
However, as a general mechanism, it can support many other scenarios with VFIO. I imagine this dmabuf approach to be usable by iommufd as well for generic and safe P2P mappings.
This series goes after the "Break up ioctl dispatch functions to one function per ioctl" series.
Yeah that sounds like it should work.
But where is the rest of the series, I only see the cover letter?
Quoted 17 lines
v2:
- Name the new filedma_buf.c
- Restoreorig_nentsbefore freeing
- Fix reversed logic aroundpriv->revoked
- Setpriv->index
- Rebased on v2 "Break up ioctl dispatch functions"
v1: https://lore.kernel.org/r/[email protected]Cc Oded Gabbay <[email protected]>Cc Christian König <[email protected]>Cc Daniel Vetter <[email protected]>Cc Leon Romanovsky <[email protected]>Cc Maor Gottlieb <[email protected]>Signed-off-by Jason Gunthorpe <[email protected]>
Quoted 3 lines
Jason Gunthorpe (3):
vfio: Addvfio_device_get()
dma-buf: Adddma_buf_try_get()
That is usually a no-go. We have rejected adding dma_buf_try_get() multiple times.
Please explain *exactly* what you need that for and how you protect against races with tearndown.
Regards,
Christian.
Quoted 20 lines
vfio/pci: Allow MMIO regions to be exported through dma-buf
Wei Lin Guay (1):
vfio/pci: Allow export dmabuf withoutmove_notifyfrom importerdrivers/vfio/pci/Makefile | 1 + drivers/vfio/pci/dma_buf.c | 291 +++++++++++++++++++++++++++++ drivers/vfio/pci/vfio_pci_config.c | 8 +- drivers/vfio/pci/vfio_pci_core.c | 44 ++++- drivers/vfio/pci/vfio_pci_priv.h | 30 +++ drivers/vfio/vfio_main.c | 1 + include/linux/dma-buf.h | 13 ++ include/linux/vfio.h | 6 + include/linux/vfio_pci_core.h | 1 + include/uapi/linux/vfio.h | 18 ++ 10 files changed, 405 insertions(+), 8 deletions(-)create mode 100644
drivers/vfio/pci/dma_buf.c--
2.43.5
Quoted 6 lines · Christian König
Am 16.12.24 um 10:54 schrieb Wei Lin Guay:
Quoted 5 lines
From: Wei Lin Guay <[email protected]>
However, as a general mechanism, it can support many other scenarios with VFIO. I imagine this dmabuf approach to be usable by iommufd as well for generic and safe P2P mappings.
Quoted 6 lines
Quoted 2 lines
This series goes after the "Break up ioctl dispatch functions to one function per ioctl" series.
Yeah that sounds like it should work.
But where is the rest of the series, I only see the cover letter?
Should be here:
Christian König
maintainerAm 16.12.24 um 17:54 schrieb Keith Busch:
Quoted 7 lines · Christian König
Quoted 6 lines
Am 16.12.24 um 10:54 schrieb Wei Lin Guay:
Quoted 5 lines
From: Wei Lin Guay <[email protected]>
However, as a general mechanism, it can support many other scenarios with VFIO. I imagine this dmabuf approach to be usable by iommufd as well for generic and safe P2P mappings.
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
This series goes after the "Break up ioctl dispatch functions to one function per ioctl" series.
Yeah that sounds like it should work.
But where is the rest of the series, I only see the cover letter?
Should be here:
https://lore.kernel.org/linux-rdma/[email protected]/T/#u
Please send that out once more with me on explicit CC.
Apart from that I have to reject the adding of dma_buf_try_get(), that is clearly not something we should do.
Thanks,
Christian.
Hi Christian,
Thanks again for your prompt response/review.
Quoted 13 lines · Christian König
Quoted 2 lines
Am 16.12.24 um 17:54 schrieb Keith Busch:
Quoted 9 lines · Christian König
Quoted 8 lines
Am 16.12.24 um 10:54 schrieb Wei Lin Guay:
Quoted 7 lines
From: Wei Lin Guay <[email protected]>
However, as a general mechanism, it can support many other scenarios with VFIO. I imagine this dmabuf approach to be usable by iommufd as well for generic and safe P2P mappings.This series goes after the "Break up ioctl dispatch functions to one function per ioctl" series.
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
Yeah that sounds like it should work.
But where is the rest of the series, I only see the cover letter?
Should be here:
Please send that out once more with me on explicit CC.
I will resend the patch series. I was experiencing issues with my email client, which inadvertently split the series into two separate emails.
Quoted 2 lines
Apart from that I have to reject the adding of
dma_buf_try_get(), that is clearly not something we should do.
Understood. It appears that Vivek has confirmed that his v2 has resolved the issue. I will follow up with him to determine if he plans to resume his patch, and if so, I will apply my last patch on top of his updated patch series
Thanks,
Wei Lin
Quoted 3 lines
Thanks,
Christian.
Christian König
maintainerAm 17.12.24 um 12:06 schrieb Wei Lin Guay:
Quoted 3 lines
[SNIP]
Quoted 1 line
Please send that out once more with me on explicit CC.
I will resend the patch series. I was experiencing issues with my email client, which inadvertently split the series into two separate emails.
Alternatively I can also copy the code from the list archive and explain why that doesn't work:
+void vfio_pci_dma_buf_move(struct vfio_pci_core_device *vdev, bool revoked) +{
+ struct vfio_pci_dma_buf *priv;
+ struct vfio_pci_dma_buf *tmp;
+
+ lockdep_assert_held_write(&vdev->memory_lock);
This makes sure that the caller is holding vdev->memory_lock.
+
+ list_for_each_entry_safe(priv, tmp, &vdev->dmabufs, dmabufs_elm) { + if (!dma_buf_try_get(priv->dmabuf))
This here only works because vfio_pci_dma_buf_release() also grabs vdev->memory_lock and so we are protected against concurrent releases.
+ continue;
+ if (priv->revoked != revoked) {
+ dma_resv_lock(priv->dmabuf->resv, NULL); + priv->revoked = revoked;
+ dma_buf_move_notify(priv->dmabuf); + dma_resv_unlock(priv->dmabuf->resv); + }
+ dma_buf_put(priv->dmabuf);
The problem is now that this here might drop the last reference which in turn calls vfio_pci_dma_buf_release() which also tries to grab vdev->memory_lock and so results in a deadlock.
+ }
+}
This pattern was suggested multiple times and I had to rejected it every time because the whole idea is just fundamentally broken.
It's really astonishing how people always come up with the same broken pattern.
Regards,
Christian.
Quoted 10 lines
Quoted 2 lines
Apart from that I have to reject the adding of
dma_buf_try_get(), that is clearly not something we should do.Understood. It appears that Vivek has confirmed that his v2 has resolved the issue. I will follow up with him to determine if he plans to resume his patch, and if so, I will apply my last patch on top of his updated patch series
Thanks,
Wei LinQuoted 2 lines
Thanks,
Christian.
Hi Christian,
Quoted 36 lines
Subject: Re: [PATCH 0/4] cover-letter: Allow MMIO regions to be exported through dmabuf
Quoted 2 lines
I will resend the patch series. I was experiencing issues with my email
client, which inadvertently split the series into two separate emails.Alternatively I can also copy the code from the list archive and explain why that doesn't work:
(quoted diff) +6
-0+void vfio_pci_dma_buf_move(struct vfio_pci_core_device *vdev, bool +revoked) { + struct vfio_pci_dma_buf *priv; + struct vfio_pci_dma_buf *tmp; + + lockdep_assert_held_write(&vdev->memory_lock);This makes sure that the caller is holding
vdev->memory_lock.(quoted diff) +3
-0+ + list_for_each_entry_safe(priv, tmp, &vdev->dmabufs, dmabufs_elm) { + if (!dma_buf_try_get(priv->dmabuf))This here only works because
vfio_pci_dma_buf_release()also grabs vdev-Quoted 1 line
memory_lockand so we are protected against concurrent releases.(quoted diff) +8
-0+ continue; + if (priv->revoked != revoked) { + dma_resv_lock(priv->dmabuf->resv, NULL); + priv->revoked = revoked; + dma_buf_move_notify(priv->dmabuf); + dma_resv_unlock(priv->dmabuf->resv); + } + dma_buf_put(priv->dmabuf);
Quoted 3 lines
The problem is now that this here might drop the last reference which in turn calls
vfio_pci_dma_buf_release()which also tries to grab vdev-Quoted 1 line
memory_lockand so results in a deadlock.
AFAICS, vfio_pci_dma_buf_release() would not be called synchronously after the last reference is dropped by dma_buf_put(). This is because fput(), which is called by dma_buf_put() triggers f_op->release() asynchronously; therefore, a deadlock is unlikely to occur in this scenario, unless I am overlooking something.
Thanks,
Vivek
Quoted 39 lines
(quoted diff) +2
-0+ } +}This pattern was suggested multiple times and I had to rejected it every time because the whole idea is just fundamentally broken.
It's really astonishing how people always come up with the same broken pattern.
Regards,
Christian.Apart from that I have to reject the adding of
dma_buf_try_get(), that is clearly not something we should do.Understood. It appears that Vivek has confirmed that his v2 has
resolved the issue. I will follow up with him to determine if he plans to resume his patch, and if so, I will apply my last patch on top of his updated patch seriesThanks,
Wei LinThanks,
Christian.
Christian König
maintainerAm 18.12.24 um 07:16 schrieb Kasireddy, Vivek:
Quoted 36 lines
Hi Christian,
Quoted 34 lines
Subject: Re: [PATCH 0/4] cover-letter: Allow MMIO regions to be exported through dmabuf
Quoted 2 lines
I will resend the patch series. I was experiencing issues with my email
client, which inadvertently split the series into two separate emails.Alternatively I can also copy the code from the list archive and explain why that doesn't work:
(quoted diff) +6
-0+void vfio_pci_dma_buf_move(struct vfio_pci_core_device *vdev, bool +revoked) { + struct vfio_pci_dma_buf *priv; + struct vfio_pci_dma_buf *tmp; + + lockdep_assert_held_write(&vdev->memory_lock);This makes sure that the caller is holding
vdev->memory_lock.(quoted diff) +3
-0+ + list_for_each_entry_safe(priv, tmp, &vdev->dmabufs, dmabufs_elm) { + if (!dma_buf_try_get(priv->dmabuf))This here only works because
vfio_pci_dma_buf_release()also grabs vdev-Quoted 1 line
memory_lockand so we are protected against concurrent releases.(quoted diff) +8
-0+ continue; + if (priv->revoked != revoked) { + dma_resv_lock(priv->dmabuf->resv, NULL); + priv->revoked = revoked; + dma_buf_move_notify(priv->dmabuf); + dma_resv_unlock(priv->dmabuf->resv); + } + dma_buf_put(priv->dmabuf);
Quoted 7 lines
Quoted 3 lines
The problem is now that this here might drop the last reference which in turn calls
vfio_pci_dma_buf_release()which also tries to grab vdev-Quoted 1 line
memory_lockand so results in a deadlock.AFAICS,
vfio_pci_dma_buf_release()would not be called synchronously after the last reference is dropped bydma_buf_put(). This is becausefput(), which is called bydma_buf_put()triggersf_op->release()asynchronously; therefore, a deadlock is unlikely to occur in this scenario, unless I am overlooking something.
My recollection is that the f_op->release handler is only called asynchronously if fput() was issued in interrupt context.
But could be that this information is outdated.
Regards,
Christian.
Quoted 42 lines
Thanks,
VivekQuoted 38 lines
(quoted diff) +2
-0+ } +}This pattern was suggested multiple times and I had to rejected it every time because the whole idea is just fundamentally broken.
It's really astonishing how people always come up with the same broken pattern.
Regards,
Christian.Apart from that I have to reject the adding of
dma_buf_try_get(), that is clearly not something we should do.Understood. It appears that Vivek has confirmed that his v2 has
resolved the issue. I will follow up with him to determine if he plans to resume his patch, and if so, I will apply my last patch on top of his updated patch seriesThanks,
Wei LinThanks,
Christian.
Hi Christian,
Quoted 41 lines
Subject: Re: [PATCH 0/4] cover-letter: Allow MMIO regions to be exported through dmabuf
Quoted 14 lines
Quoted 14 lines
Quoted 3 lines
I will resend the patch series. I was experiencing issues with my email
client, which inadvertently split the series into two separate emails.Alternatively I can also copy the code from the list archive and explain why that doesn't work:
(quoted diff) +6
-0+void vfio_pci_dma_buf_move(struct vfio_pci_core_device *vdev, bool +revoked) { + struct vfio_pci_dma_buf *priv; + struct vfio_pci_dma_buf *tmp; + + lockdep_assert_held_write(&vdev->memory_lock);Quoted 7 lines
Quoted 7 lines
This makes sure that the caller is holding
vdev->memory_lock.(quoted diff) +3
-0+ + list_for_each_entry_safe(priv, tmp, &vdev->dmabufs, dmabufs_elm) { + if (!dma_buf_try_get(priv->dmabuf))This here only works because
vfio_pci_dma_buf_release()also grabsvdev-
Quoted 11 lines
Quoted 11 lines
Quoted 1 line
memory_lockand so we are protected against concurrent releases.(quoted diff) +8
-0+ continue; + if (priv->revoked != revoked) { + dma_resv_lock(priv->dmabuf->resv, NULL); + priv->revoked = revoked; + dma_buf_move_notify(priv->dmabuf); + dma_resv_unlock(priv->dmabuf->resv); + } + dma_buf_put(priv->dmabuf);The problem is now that this here might drop the last reference which in
turn
Quoted 3 lines
Quoted 2 lines
calls
vfio_pci_dma_buf_release()which also tries to grab vdev-Quoted 1 line
memory_lockand so results in a deadlock.AFAICS,
vfio_pci_dma_buf_release()would not be called synchronouslyafter the
Quoted 8 lines
Quoted 1 line
last reference is dropped by
dma_buf_put(). This is becausefput(), which iscalled
Quoted 1 line
by
dma_buf_put()triggersf_op->release()asynchronously; therefore, adeadlock
Quoted 1 line
is unlikely to occur in this scenario, unless I am overlooking something.
My recollection is that the
f_op->releasehandler is only called asynchronously iffput()was issued in interrupt context.
Here is the code of fput() from the current master:
void fput(struct file *file)
{
if (file_ref_put(&file->f_ref)) {
struct task_struct *task = current;
if (unlikely(!(file->f_mode & (FMODE_BACKING | FMODE_OPENED)))) {
file_free(file);
return;
}
if (likely(!in_interrupt() && !(task->flags & PF_KTHREAD))) {
init_task_work(&file->f_task_work, ____fput);
if (!task_work_add(task, &file->f_task_work, TWA_RESUME))
return;
/*
* After this task has run exit_task_work(),
* task_work_add() will fail. Fall through to delayed
* fput to avoid leaking *file.
*/
}
if (llist_add(&file->f_llist, &delayed_fput_list))
schedule_delayed_work(&delayed_fput_work, 1);
}
}IIUC, based on the above code, it looks like there are two ways in which the f_op->release() handler is triggered from fput():
- via delayed_fput() for kernel threads and code in interrupt context
- via task_work_run() just before the task/process returns to the user-mode
The first scenario above is definitely asynchronous as the release() handler is called from a worker thread. But I think the second case (which is the most common and relevant for our use-case) can also be considered asynchronous, because the execution of the system call or ioctl that led to the context in which dma_buf_put() was called is completed.
Here is a trace from my light testing with the udmabuf driver, where you can see the release() handler being called by syscall_exit_to_user_mode() :
[ 158.464203] Call Trace: [ 158.466681] <TASK> [ 158.468815] dump_stack_lvl+0x60/0x80 [ 158.472507] dump_stack+0x14/0x16 [ 158.475853] release_udmabuf+0x2f/0x9f [ 158.479631] dma_buf_release+0x3c/0x90 [ 158.483408] __dentry_kill+0x8f/0x180 [ 158.487098] dput+0xe7/0x1a0 [ 158.490013] __fput+0x131/0x2b0 [ 158.493178] ____fput+0x19/0x20 [ 158.496352] task_work_run+0x61/0x90 [ 158.499959] syscall_exit_to_user_mode+0x1a4/0x1b0 [ 158.504769] do_syscall_64+0x5b/0x110 [ 158.508458] entry_SYSCALL_64_after_hwframe+0x4b/0x53
And, here is the relevant syscall code (from arch/x86/entry/common.c):__visible noinstr bool do_syscall_64(struct pt_regs *regs, int nr)
{
add_random_kstack_offset();
nr = syscall_enter_from_user_mode(regs, nr);
instrumentation_begin();
if (!do_syscall_x64(regs, nr) && !do_syscall_x32(regs, nr) && nr != -1) {
/* Invalid system call, but still a system call. */
regs->ax = __x64_sys_ni_syscall(regs);
}
instrumentation_end();
syscall_exit_to_user_mode(regs);I also confirmed that the release() handler is indeed called after dma_buf_put() (and not by dma_buf_put()) by adding debug prints before and after dma_buf_put() and one in the release() handler. Furthermore, I also found that calling close() on the dmabuf fd in the user-space is one scenario in which fput() is called synchronously. Here is the relevant trace:
[ 302.770910] Call Trace: [ 302.773389] <TASK> [ 302.775516] dump_stack_lvl+0x60/0x80 [ 302.779209] dump_stack+0x14/0x16 [ 302.782549] release_udmabuf+0x2f/0x9f [ 302.786329] dma_buf_release+0x3c/0x90 [ 302.790105] __dentry_kill+0x8f/0x180 [ 302.793789] dput+0xe7/0x1a0 [ 302.796703] __fput+0x131/0x2b0 [ 302.799866] __fput_sync+0x53/0x70 [ 302.803299] __x64_sys_close+0x58/0xc0 [ 302.807076] x64_sys_call+0x126a/0x17d0 [ 302.810938] do_syscall_64+0x4f/0x110 [ 302.814622] entry_SYSCALL_64_after_hwframe+0x4b/0x53
As you can see above, there is indeed a synchronous version of fput() defined just below fput():
/*
* synchronous analog of fput(); for kernel threads that might be needed
* in some umount() (and thus can't use flush_delayed_fput() without
* risking deadlocks), need to wait for completion of __fput() and know
* for this specific struct file it won't involve anything that would
* need them. Use only if you really need it - at the very least,
* don't blindly convert fput() by kernel thread to that.
*/
void __fput_sync(struct file *file)
{
if (file_ref_put(&file->f_ref))
__fput(file);
}Based on all the above, I think we can conclude that since dma_buf_put() does not directly (or synchronously) call the f_op->release() handler, a deadlock is unlikely to occur in the scenario you described.
Thanks,
Vivek
Quoted 50 lines
But could be that this information is outdated.
Regards,
Christian.Quoted 8 lines
Thanks,
VivekQuoted 4 lines
(quoted diff) +2
-0+ } +}This pattern was suggested multiple times and I had to rejected it every
time
Quoted 14 lines
Quoted 14 lines
because the whole idea is just fundamentally broken.
It's really astonishing how people always come up with the same broken pattern.
Regards,
Christian.Quoted 8 lines
Quoted 8 lines
Apart from that I have to reject the adding of
dma_buf_try_get(), that is clearly not something we should do.Understood. It appears that Vivek has confirmed that his v2 has
resolved the issue. I will follow up with him to determine if he plans to resume his patch, and if so, I will apply my last patch on top of hisupdated
Quoted 12 lines
Quoted 12 lines
patch series
Thanks,
Wei LinThanks,
Christian.
Christian König
maintainerAm 19.12.24 um 08:02 schrieb Kasireddy, Vivek:
Quoted 149 lines
Hi Christian,
Quoted 38 lines
Subject: Re: [PATCH 0/4] cover-letter: Allow MMIO regions to be exported through dmabuf
Quoted 12 lines
Quoted 12 lines
Quoted 2 lines
I will resend the patch series. I was experiencing issues with my email
client, which inadvertently split the series into two separate emails.Alternatively I can also copy the code from the list archive and explain why that doesn't work:
(quoted diff) +6
-0+void vfio_pci_dma_buf_move(struct vfio_pci_core_device *vdev, bool +revoked) { + struct vfio_pci_dma_buf *priv; + struct vfio_pci_dma_buf *tmp; + + lockdep_assert_held_write(&vdev->memory_lock);Quoted 7 lines
Quoted 7 lines
This makes sure that the caller is holding
vdev->memory_lock.(quoted diff) +3
-0+ + list_for_each_entry_safe(priv, tmp, &vdev->dmabufs, dmabufs_elm) { + if (!dma_buf_try_get(priv->dmabuf))This here only works because
vfio_pci_dma_buf_release()also grabsvdev-
Quoted 11 lines
Quoted 11 lines
Quoted 1 line
memory_lockand so we are protected against concurrent releases.(quoted diff) +8
-0+ continue; + if (priv->revoked != revoked) { + dma_resv_lock(priv->dmabuf->resv, NULL); + priv->revoked = revoked; + dma_buf_move_notify(priv->dmabuf); + dma_resv_unlock(priv->dmabuf->resv); + } + dma_buf_put(priv->dmabuf);The problem is now that this here might drop the last reference which in
turn
Quoted 3 lines
Quoted 2 lines
calls
vfio_pci_dma_buf_release()which also tries to grab vdev-Quoted 1 line
memory_lockand so results in a deadlock.AFAICS,
vfio_pci_dma_buf_release()would not be called synchronouslyQuoted 8 lines
after the
Quoted 1 line
last reference is dropped by
dma_buf_put(). This is becausefput(), which iscalled
Quoted 1 line
by
dma_buf_put()triggersf_op->release()asynchronously; therefore, adeadlock
Quoted 1 line
is unlikely to occur in this scenario, unless I am overlooking something.
My recollection is that the
f_op->releasehandler is only called asynchronously iffput()was issued in interrupt context.Here is the code of
fput()from the current master:
voidfput(struct file *file){ if (file_ref_put(&file->f_ref)) { struct task_struct *task = current; if (unlikely(!(file->f_mode & (FMODE_BACKING | FMODE_OPENED)))) { file_free(file); return; } if (likely(!in_interrupt() && !(task->flags & PF_KTHREAD))) { init_task_work(&file->f_task_work, ____fput); if (!task_work_add(task, &file->f_task_work, TWA_RESUME)) return; /* * After this task has run exit_task_work(), * task_work_add() will fail. Fall through to delayed * fput to avoid leaking *file. */ } if (llist_add(&file->f_llist, &delayed_fput_list)) schedule_delayed_work(&delayed_fput_work, 1); } }IIUC, based on the above code, it looks like there are two ways in which the
f_op->release()handler is triggered fromfput():
- viadelayed_fput()for kernel threads and code in interrupt context
- viatask_work_run()just before the task/process returns to the user-modeThe first scenario above is definitely asynchronous as the
release()handler is called from a worker thread. But I think the second case (which is the most common and relevant for our use-case) can also be considered asynchronous, because the execution of the system call or ioctl that led to the context in whichdma_buf_put()was called is completed.Here is a trace from my light testing with the udmabuf driver, where you can see the
release()handler being called bysyscall_exit_to_user_mode():[ 158.464203] Call Trace: [ 158.466681] <TASK> [ 158.468815] dump_stack_lvl+0x60/0x80 [ 158.472507] dump_stack+0x14/0x16 [ 158.475853] release_udmabuf+0x2f/0x9f [ 158.479631] dma_buf_release+0x3c/0x90 [ 158.483408] __dentry_kill+0x8f/0x180 [ 158.487098] dput+0xe7/0x1a0 [ 158.490013] __fput+0x131/0x2b0 [ 158.493178] ____fput+0x19/0x20 [ 158.496352] task_work_run+0x61/0x90 [ 158.499959] syscall_exit_to_user_mode+0x1a4/0x1b0 [ 158.504769] do_syscall_64+0x5b/0x110 [ 158.508458] entry_SYSCALL_64_after_hwframe+0x4b/0x53And, here is the relevant syscall code (from
arch/x86/entry/common.c):__visiblenoinstr booldo_syscall_64(struct pt_regs *regs, int nr){ add_random_kstack_offset(); nr = syscall_enter_from_user_mode(regs, nr); instrumentation_begin(); if (!do_syscall_x64(regs, nr) && !do_syscall_x32(regs, nr) && nr != -1) { /* Invalid system call, but still a system call. */ regs->ax = __x64_sys_ni_syscall(regs); } instrumentation_end(); syscall_exit_to_user_mode(regs);I also confirmed that the
release()handler is indeed called afterdma_buf_put()(and not bydma_buf_put()) by adding debug prints before and afterdma_buf_put()and one in therelease()handler. Furthermore, I also found that callingclose()on the dmabuf fd in the user-space is one scenario in whichfput()is called synchronously. Here is the relevant trace:[ 302.770910] Call Trace: [ 302.773389] <TASK> [ 302.775516] dump_stack_lvl+0x60/0x80 [ 302.779209] dump_stack+0x14/0x16 [ 302.782549] release_udmabuf+0x2f/0x9f [ 302.786329] dma_buf_release+0x3c/0x90 [ 302.790105] __dentry_kill+0x8f/0x180 [ 302.793789] dput+0xe7/0x1a0 [ 302.796703] __fput+0x131/0x2b0 [ 302.799866] __fput_sync+0x53/0x70 [ 302.803299] __x64_sys_close+0x58/0xc0 [ 302.807076] x64_sys_call+0x126a/0x17d0 [ 302.810938] do_syscall_64+0x4f/0x110 [ 302.814622] entry_SYSCALL_64_after_hwframe+0x4b/0x53As you can see above, there is indeed a synchronous version of
fput()defined just belowfput():
/*
* synchronous analog offput(); for kernel threads that might be needed
* in someumount()(and thus can't useflush_delayed_fput()without
* risking deadlocks), need to wait for completion of__fput()and know
* for this specific struct file it won't involve anything that would
* need them. Use only if you really need it - at the very least,
* don't blindly convertfput()by kernel thread to that.
*/
void__fput_sync(struct file *file)
Quoted 8 lines
{ if (file_ref_put(&file->f_ref)) __fput(file); }Based on all the above, I think we can conclude that since
dma_buf_put()does not directly (or synchronously) call thef_op->release()handler, a deadlock is unlikely to occur in the scenario you described.
Yeah, I agree.
Interesting to know, I wasn't aware that the task_work functionality exists for that use case.
Thanks,
Christian.
Quoted 52 lines
Thanks,
VivekQuoted 48 lines
But could be that this information is outdated.
Regards,
Christian.Quoted 7 lines
Thanks,
VivekQuoted 4 lines
(quoted diff) +2
-0+ } +}This pattern was suggested multiple times and I had to rejected it every
time
Quoted 14 lines
Quoted 14 lines
because the whole idea is just fundamentally broken.
It's really astonishing how people always come up with the same broken pattern.
Regards,
Christian.Quoted 8 lines
Quoted 8 lines
Apart from that I have to reject the adding of
dma_buf_try_get(), that is clearly not something we should do.Understood. It appears that Vivek has confirmed that his v2 has
resolved the issue. I will follow up with him to determine if he plans to resume his patch, and if so, I will apply my last patch on top of hisupdated
Quoted 12 lines
Quoted 12 lines
patch series
Thanks,
Wei LinThanks,
Christian.
Jason Gunthorpe
maintainerQuoted 3 lines · Christian König
Quoted 3 lines
Based on all the above, I think we can conclude that since
dma_buf_put()does not directly (or synchronously) call thef_op->release()handler, a deadlock is unlikely to occur in the scenario you described.
Right, there is no deadlock here, and there is nothing inhernetly wrong with using try_get like this. The locking here is ugly ugly ugly, I forget why, but this was the best I came up with to untangle it without deadlocks or races.
Quoted 4 lines
Yeah, I agree.
Interesting to know, I wasn't aware that the
task_workfunctionality exists for that use case.
IIRC it was changed a while back because call chains were getting kind of crazy long with the file release functions and stack overflow was a problem in some cases.
Jason
Quoted 10 lines · Jason Gunthorpe
Quoted 3 lines · Christian König
Quoted 3 lines
Based on all the above, I think we can conclude that since
dma_buf_put()does not directly (or synchronously) call thef_op->release()handler, a deadlock is unlikely to occur in the scenario you described.Right, there is no deadlock here, and there is nothing inhernetly wrong with using
try_getlike this. The locking here is ugly ugly ugly, I forget why, but this was the best I came up with to untangle it without deadlocks or races.
Yeah, imo try_get is perfectly fine pattern. With that sorted my only request is to make the try_get specific to the dma_ops, because it only works if both ->release and the calling context of try_get follow the same rules, which by necessity are exporter specific.
In full generality as a dma_buf.c interface it's just busted and there's no way to make it work, unless we inflict that locking ugliness on absolutely every exporter.
Quoted 8 lines
Quoted 4 lines
Yeah, I agree.
Interesting to know, I wasn't aware that the
task_workfunctionality exists for that use case.IIRC it was changed a while back because call chains were getting kind of crazy long with the file release functions and stack overflow was a problem in some cases.
Hm I thought it was also a "oh fuck deadlocks" moment, because usually most of the very deep fput calls are for temporary reference and not the final one. Unless userspace is sneaky and drops its own fd reference in a separate thread with close(), then everything goes boom. And untangling all these was impossible.
-Sima
Signature · 3 lines
Simona Vetter Software Engineer, Intel Corporation http://blog.ffwll.ch
Jason Gunthorpe
maintainerQuoted 12 lines · Simona Vetter
Quoted 10 lines · Jason Gunthorpe
Quoted 3 lines · Christian König
Quoted 3 lines
Based on all the above, I think we can conclude that since
dma_buf_put()does not directly (or synchronously) call thef_op->release()handler, a deadlock is unlikely to occur in the scenario you described.Right, there is no deadlock here, and there is nothing inhernetly wrong with using
try_getlike this. The locking here is ugly ugly ugly, I forget why, but this was the best I came up with to untangle it without deadlocks or races.
Quoted 4 lines
Yeah, imo
try_getis perfectly fine pattern. With that sorted my only request is to make thetry_getspecific to thedma_ops, because it only works if both ->release and the calling context oftry_getfollow the same rules, which by necessity are exporter specific.
We already know the fd is a dma_ops one because it is on an internal list and it could not get there otherwise.
The pointer cannot become invalid and freed back to the type safe RCU while on the list, meaning the try_get is safe as is.
I think Christian's original advice to just open code it is the best option.
Quoted 3 lines
In full generality as a
dma_buf.cinterface it's just busted and there's no way to make it work, unless we inflict that locking ugliness on absolutely every exporter.
I'm not sure about that, the struct file code has special logic to accommodate the type safe RCU trick. I didn't try to digest it fully, but I expect there are ways to use it safely without the locking on release.
Quoted 7 lines
Quoted 3 lines
IIRC it was changed a while back because call chains were getting kind of crazy long with the file release functions and stack overflow was a problem in some cases.
Hm I thought it was also a "oh fuck deadlocks" moment, because usually most of the very deep fput calls are for temporary reference and not the final one.
That sounds motivating too, but I've also seen cases of being careful to unlock before fputting things..
Jason
Quoted 18 lines · Jason Gunthorpe
Quoted 12 lines · Simona Vetter
Quoted 10 lines · Jason Gunthorpe
Quoted 3 lines · Christian König
Quoted 3 lines
Based on all the above, I think we can conclude that since
dma_buf_put()does not directly (or synchronously) call thef_op->release()handler, a deadlock is unlikely to occur in the scenario you described.Right, there is no deadlock here, and there is nothing inhernetly wrong with using
try_getlike this. The locking here is ugly ugly ugly, I forget why, but this was the best I came up with to untangle it without deadlocks or races.Quoted 4 lines
Yeah, imo
try_getis perfectly fine pattern. With that sorted my only request is to make thetry_getspecific to thedma_ops, because it only works if both ->release and the calling context oftry_getfollow the same rules, which by necessity are exporter specific.
Quoted 8 lines
We already know the fd is a
dma_opsone because it is on an internal list and it could not get there otherwise.The pointer cannot become invalid and freed back to the type safe RCU while on the list, meaning the
try_getis safe as is.I think Christian's original advice to just open code it is the best option.
Yeah open coding in vfio is imo best, agreed on that.
Quoted 8 lines
Quoted 3 lines
In full generality as a
dma_buf.cinterface it's just busted and there's no way to make it work, unless we inflict that locking ugliness on absolutely every exporter.I'm not sure about that, the struct file code has special logic to accommodate the type safe RCU trick. I didn't try to digest it fully, but I expect there are ways to use it safely without the locking on release.
Hm yes, if you use a write barrier when set your file pointer and clear it in release, then you can use get_file_rcu with just rcu_read_lock on the read side. But you have to directly use your own struct file * pointer since it needs to reload that in a loop, you can't use dma_buf->file.
At that point you're already massively peeking behind the dma_buf abstraction that just directly using get_file_rcu is imo better.
And for anything else, whether it's rcu or plain locks, it's all subsystem specific anyway that I think a dma_buf.c wrapper
Not entirely sure, but for the dma_buf_try_get wrapper you have to put a full lock acquisition or rcu_sychnronize into your ->release callback, otherwise I don't think it's safe to use.
Quoted 10 lines
Quoted 7 lines
Quoted 3 lines
IIRC it was changed a while back because call chains were getting kind of crazy long with the file release functions and stack overflow was a problem in some cases.
Hm I thought it was also a "oh fuck deadlocks" moment, because usually most of the very deep fput calls are for temporary reference and not the final one.
That sounds motivating too, but I've also seen cases of being careful to unlock before fputting things..
Yeah I think knowledge about this issue was very uneven in different subsystems.
-Sima
Signature · 3 lines
Simona Vetter Software Engineer, Intel Corporation http://blog.ffwll.ch
Quoted 13 lines · Christian König
Am 16.12.24 um 17:54 schrieb Keith Busch:
Quoted 12 lines · Christian König
Quoted 11 lines
Am 16.12.24 um 10:54 schrieb Wei Lin Guay:
Quoted 7 lines
From: Wei Lin Guay <[email protected]>
However, as a general mechanism, it can support many other scenarios with VFIO. I imagine this dmabuf approach to be usable by iommufd as well for generic and safe P2P mappings.This series goes after the "Break up ioctl dispatch functions to one function per ioctl" series.
Yeah that sounds like it should work.
But where is the rest of the series, I only see the cover letter?
Quoted 8 lines
Quoted 3 lines
Should be here:
https://lore.kernel.org/linux-rdma/[email protected]/T/#uPlease send that out once more with me on explicit CC.
Apart from that I have to reject the adding of
dma_buf_try_get(), that is clearly not something we should do.
Yeah if we do try_get it would need to be at least on a specific dma_buf type (so checking for dma_buf->ops), since in general this does not work (unless we add the general code in dma_buf.c somehow to make it work). Aside from any other design concerns.
-Sima
Signature · 3 lines
Simona Vetter Software Engineer, Intel Corporation http://blog.ffwll.ch