Re: [PATCH v2 15/16] vfio/pci: Preserve the iommufd state of the vfio cdev
From: Samiullah Khawaja
Date: Wed May 20 2026 - 18:40:53 EST
On Wed, May 20, 2026 at 12:57:40AM +0000, Pranjal Shrivastava wrote:
On Mon, Apr 27, 2026 at 05:56:32PM +0000, Samiullah Khawaja wrote:
If the vfio cdev is attached to an iommufd, preserve the state of the
attached iommufd also. Basically preserve the iommu specific state of
the device and also the attach iommu HW unit.
Once the device and its iommufd attachment is preserved, it cannot be
detached or attached to another IOAS until it is unpreserved.
Signed-off-by: Samiullah Khawaja <skhawaja@xxxxxxxxxx>
---
drivers/vfio/device_cdev.c | 10 ++++++++
drivers/vfio/pci/vfio_pci_liveupdate.c | 33 +++++++++++++++++++++++++-
2 files changed, 42 insertions(+), 1 deletion(-)
[...]
diff --git a/drivers/vfio/pci/vfio_pci_liveupdate.c b/drivers/vfio/pci/vfio_pci_liveupdate.c
index 976ef17e6103..b56d80379ffc 100644
--- a/drivers/vfio/pci/vfio_pci_liveupdate.c
+++ b/drivers/vfio/pci/vfio_pci_liveupdate.c
@@ -108,10 +108,13 @@
#include <linux/kho/abi/vfio_pci.h>
#include <linux/liveupdate.h>
#include <linux/errno.h>
+#include <linux/iommufd.h>
#include <linux/vfio.h>
#include "vfio_pci_priv.h"
+MODULE_IMPORT_NS("IOMMUFD");
+
static bool vfio_pci_liveupdate_can_preserve(struct liveupdate_file_handler *handler,
struct file *file)
{
@@ -153,9 +156,26 @@ static int vfio_pci_liveupdate_preserve(struct liveupdate_file_op_args *args)
vdev = container_of(device, struct vfio_pci_core_device, vdev);
pdev = vdev->pdev;
+#ifdef CONFIG_IOMMU_LIVEUPDATE
+ /* If iommufd is attached, preserve the underlying domain */
+ mutex_lock(&device->dev_set->lock);
+ if (device->iommufd_attached) {
+ u64 token, preserved_state;
+
+ ret = iommufd_device_preserve(args->session,
+ device->iommufd_device,
+ &token, &preserved_state);
+ if (ret) {
+ mutex_unlock(&device->dev_set->lock);
+ return ret;
+ }
+ }
+ mutex_unlock(&device->dev_set->lock);
+#endif
+
ret = pci_liveupdate_preserve(pdev);
if (ret)
- return ret;
+ goto err_iommufd_unpreserve;
Nit: Would we hit the WARN in patch 14 if (device->iommufd_attached) was
false here? Should we check if (device->iommufd_attached) below before
calling unpreserve?
Agreed. I will update this.
ser = kho_alloc_preserve(sizeof(*ser));
if (IS_ERR(ser)) {
@@ -170,6 +190,9 @@ static int vfio_pci_liveupdate_preserve(struct liveupdate_file_op_args *args)
args->serialized_data = virt_to_phys(ser);
return 0;
+err_iommufd_unpreserve:
+ iommufd_device_unpreserve(args->session, device->iommufd_device);
+
err_unpreserve:
pci_liveupdate_unpreserve(pdev);
return ret;
With that nit:
Reviewed-by: Pranjal Shrivastava <praan@xxxxxxxxxx>
Thanks,
Praan
Thanks,
Sami