[PATCH v5 11/18] iommu: Restore and reattach preserved domains to devices

From: Samiullah Khawaja

Date: Sun Sep 20 2026 - 20:55:04 EST


During default domain setup, restore the preserved domains by restoring
the page tables using restore() iommupt op. Associated the restored
domain with the iommu group of the preserved device, and reattach the
domain to the device.

Signed-off-by: Samiullah Khawaja <skhawaja@xxxxxxxxxx>
---
drivers/iommu/iommu.c | 116 +++++++++++++++++++++++++++++--
drivers/iommu/liveupdate.c | 116 +++++++++++++++++++++++++++++++
include/linux/iommu-liveupdate.h | 64 +++++++++++++++++
3 files changed, 290 insertions(+), 6 deletions(-)

diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c
index 9c1ad8de3ad7..cca1eb332a43 100644
--- a/drivers/iommu/iommu.c
+++ b/drivers/iommu/iommu.c
@@ -158,6 +158,7 @@ static void __iommu_group_set_domain_nofail(struct iommu_group *group,
WARN_ON(__iommu_group_set_domain_internal(
group, new_domain, IOMMU_SET_DOMAIN_MUST_SUCCEED));
}
+static int __iommu_group_alloc_blocking_domain(struct iommu_group *group);

static int iommu_setup_default_domain(struct iommu_group *group,
int target_type);
@@ -540,6 +541,10 @@ static int iommu_init_device(struct device *dev)
goto err_free;
}

+#ifdef CONFIG_IOMMU_LIVEUPDATE
+ iommu_init_device_preserved_data(dev);
+#endif
+
iommu_dev = ops->probe_device(dev);
if (IS_ERR(iommu_dev)) {
ret = PTR_ERR(iommu_dev);
@@ -604,7 +609,8 @@ static void iommu_deinit_device(struct device *dev)
* Regardless, if a delayed attach never occurred, then the release
* should still avoid touching any hardware configuration either.
*/
- if (!dev->iommu->attach_deferred && ops->release_domain) {
+ if (!dev->iommu->attach_deferred && ops->release_domain &&
+ !dev_iommu_restored_state(dev)) {
struct iommu_domain *release_domain = ops->release_domain;

/*
@@ -694,7 +700,8 @@ static int __iommu_probe_device(struct device *dev, struct list_head *group_list
}

for_each_group_device(group, gdev2) {
- if (dev_iommu_preserved_state(gdev2->dev)) {
+ if (dev_iommu_preserved_state(gdev2->dev) ||
+ dev_iommu_restored_state(gdev2->dev)) {
ret = -EBUSY;
goto err_free_gdev;
}
@@ -764,6 +771,27 @@ int iommu_probe_device(struct device *dev)
return 0;
}

+static void __iommu_group_remove_restored_device(struct iommu_group *group,
+ struct device *dev)
+{
+ struct iommu_device_ser *device_ser;
+
+ lockdep_assert_held(&group->mutex);
+ device_ser = dev_iommu_restored_state(dev);
+ if (!device_ser)
+ return;
+
+ if (!group->owner_cnt || group->owner != device_ser)
+ return;
+
+ if (group->owner_cnt > 1) {
+ group->owner_cnt--;
+ } else {
+ group->owner_cnt = 0;
+ group->owner = NULL;
+ }
+}
+
static void __iommu_group_free_device(struct iommu_group *group,
struct group_device *grp_dev)
{
@@ -775,13 +803,15 @@ static void __iommu_group_free_device(struct iommu_group *group,
trace_remove_device_from_group(group->id, dev);

/*
- * If the group has become empty then ownership must have been
- * released, and the current domain must be set back to NULL or
- * the default domain.
+ * If the group has become empty then ownership must have been released,
+ * and the current domain must be set back to NULL or the default
+ * domain. A restored domain remains attached to the restored device on
+ * removal.
*/
if (list_empty(&group->devices))
WARN_ON(group->owner_cnt ||
- group->domain != group->default_domain);
+ (group->domain != group->default_domain &&
+ !iommu_domain_restored_state(group->domain)));

kfree(grp_dev->name);
kfree(grp_dev);
@@ -794,6 +824,7 @@ static void __iommu_group_remove_device(struct device *dev)
struct group_device *device;

mutex_lock(&group->mutex);
+ __iommu_group_remove_restored_device(group, dev);
for_each_group_device(group, device) {
if (device->dev != dev)
continue;
@@ -2211,6 +2242,7 @@ static int __iommu_attach_device(struct iommu_domain *domain,
ret = domain->ops->attach_dev(domain, dev, old);
if (ret)
return ret;
+
dev->iommu->attach_deferred = 0;
trace_attach_device_to_domain(dev);
return 0;
@@ -3175,6 +3207,62 @@ int iommu_fwspec_add_ids(struct device *dev, const u32 *ids, int num_ids)
}
EXPORT_SYMBOL_GPL(iommu_fwspec_add_ids);

+static struct device *__iommu_group_restored_device(struct iommu_group *group)
+{
+ struct group_device *gdev;
+
+ lockdep_assert_held(&group->mutex);
+ for_each_group_device(group, gdev) {
+ if (!dev_is_pci(gdev->dev))
+ continue;
+
+ if (dev_iommu_restored_state(gdev->dev))
+ return gdev->dev;
+ }
+
+ return NULL;
+}
+
+static int __iommu_group_restore_domain(struct iommu_group *group)
+{
+ struct iommu_device_ser *device_ser;
+ struct iommu_domain *domain;
+ struct device *dev;
+ void *owner;
+ int ret;
+
+ lockdep_assert_held(&group->mutex);
+ if (group->domain)
+ return -EBUSY;
+
+ dev = __iommu_group_restored_device(group);
+ device_ser = dev_iommu_restored_state(dev);
+ if (!device_ser)
+ return -ENOENT;
+
+ ret = __iommu_group_alloc_blocking_domain(group);
+ if (ret)
+ return ret;
+
+ domain = iommu_restore_domain(dev, device_ser, &owner);
+ if (WARN_ON(IS_ERR(domain)))
+ return PTR_ERR(domain);
+
+ /* The restored domain is attached with the restored device. */
+ ret = __iommu_group_set_domain(group, domain);
+ if (ret)
+ return ret;
+
+ /*
+ * Ownership of groups with preserved devices is set during boot. These
+ * will be reclaimed later by the entity (iommufd) that preserved them.
+ */
+ WARN_ON(group->owner);
+ group->owner = owner;
+ group->owner_cnt = 1;
+ return ret;
+}
+
/**
* iommu_setup_default_domain - Set the default_domain for the group
* @group: Group to change
@@ -3233,6 +3321,16 @@ static int iommu_setup_default_domain(struct iommu_group *group,

/* We must set default_domain early for __iommu_device_set_domain */
group->default_domain = dom;
+
+ /* Preserved devices need to be attached to the restore domain */
+ if (__iommu_group_restored_device(group)) {
+ ret = __iommu_group_restore_domain(group);
+ if (ret)
+ goto err_restore_def_domain;
+
+ goto out_free_old;
+ }
+
if (!group->domain) {
/*
* Drivers are not allowed to fail the first domain attach.
@@ -4102,6 +4200,9 @@ int pci_dev_reset_iommu_prepare(struct pci_dev *pdev)
if (!pci_ats_supported(pdev) || !dev_has_iommu(&pdev->dev))
return 0;

+ if (dev_iommu_restored_state(&pdev->dev))
+ return 0;
+
guard(mutex)(&group->mutex);

gdev = __dev_to_gdev(&pdev->dev);
@@ -4213,6 +4314,9 @@ void pci_dev_reset_iommu_done(struct pci_dev *pdev)
if (!pci_ats_supported(pdev) || !dev_has_iommu(&pdev->dev))
return;

+ if (dev_iommu_restored_state(&pdev->dev))
+ return;
+
guard(mutex)(&group->mutex);

gdev = __dev_to_gdev(&pdev->dev);
diff --git a/drivers/iommu/liveupdate.c b/drivers/iommu/liveupdate.c
index ff76a23d9583..4fa12b4efea5 100644
--- a/drivers/iommu/liveupdate.c
+++ b/drivers/iommu/liveupdate.c
@@ -716,3 +716,119 @@ void iommu_unpreserve_device(struct iommu_domain *domain, struct device *dev)
liveupdate_flb_put_outgoing(&iommu_flb);
}
EXPORT_SYMBOL_GPL(iommu_unpreserve_device);
+
+static inline bool match_device_ser(struct iommu_device_ser *match,
+ struct pci_dev *pdev)
+{
+ return match->devid == pci_dev_id(pdev) && match->pci_domain_nr == pci_domain_nr(pdev->bus);
+}
+
+/**
+ * iommu_init_device_preserved_data() - Initialize preserved state for device
+ * @dev: Target device
+ *
+ * Looks up incoming Live Update state for @dev and attaches it to the device if
+ * found.
+ */
+void iommu_init_device_preserved_data(struct device *dev)
+{
+ struct iommu_device_ser *device_ser = NULL;
+ struct iommu_device_array_ser *array;
+ struct iommu_flb_obj *flb_obj;
+ int ret, idx;
+
+ if (!dev_is_pci(dev))
+ return;
+
+ ret = iommu_liveupdate_flb_get_incoming(&flb_obj);
+ if (ret)
+ return;
+
+ mutex_lock(&flb_obj->lock);
+ array = phys_to_virt(flb_obj->ser->device_array_phys);
+ iommu_liveupdate_for_each_arr(array) {
+ iommu_liveupdate_for_each_obj(array, device_ser, idx) {
+ if (match_device_ser(device_ser, to_pci_dev(dev))) {
+ device_ser->hdr.flags |= IOMMU_SER_FLAG_INCOMING;
+ goto out;
+ }
+ }
+ }
+
+ device_ser = NULL;
+out:
+ WRITE_ONCE(dev->iommu->device_ser, device_ser);
+ mutex_unlock(&flb_obj->lock);
+ liveupdate_flb_put_incoming(&iommu_flb);
+}
+EXPORT_SYMBOL(iommu_init_device_preserved_data);
+
+/**
+ * iommu_restore_domain() - Restore a preserved domain for a device
+ * @dev: Target device
+ * @ser: Serialized device state
+ * @owner: Pointer to store group owner handle
+ *
+ * Restores or reuses a restored preserved domain for @dev from serialized state
+ * @ser.
+ *
+ * Return: Restored iommu_domain pointer, or ERR_PTR.
+ */
+struct iommu_domain *iommu_restore_domain(struct device *dev,
+ struct iommu_device_ser *ser,
+ void **owner)
+{
+ struct iommu_domain_ser *domain_ser;
+ struct iommu_flb_obj *flb_obj;
+ struct iommu_domain *domain;
+ struct pt_iommu *pt;
+ int ret;
+
+ ret = iommu_liveupdate_flb_get_incoming(&flb_obj);
+ if (ret)
+ return ERR_PTR(ret);
+
+ mutex_lock(&flb_obj->lock);
+
+ /* Preserved device should have a preserved domain */
+ if (!ser->domain_iommu_ser.domain_phys) {
+ domain = ERR_PTR(-EINVAL);
+ goto out;
+ }
+
+ domain_ser = phys_to_virt(ser->domain_iommu_ser.domain_phys);
+ if (domain_ser->restored_domain) {
+ *owner = ser;
+ domain = domain_ser->restored_domain;
+ goto out;
+ }
+
+ domain_ser->hdr.flags |= IOMMU_SER_FLAG_INCOMING;
+ domain = iommu_paging_domain_alloc(dev);
+ if (IS_ERR(domain))
+ goto out;
+
+ pt = iommupt_from_domain(domain);
+ if (!pt) {
+ iommu_domain_free(domain);
+ domain = ERR_PTR(-EOPNOTSUPP);
+ goto out;
+ }
+
+ ret = pt->ops->restore(pt, domain_ser);
+ if (ret) {
+ iommu_domain_free(domain);
+ domain = ERR_PTR(ret);
+ goto out;
+ }
+
+ /* The device is owned by the preserved state. */
+ *owner = ser;
+ domain->preserved_state = domain_ser;
+ domain_ser->restored_domain = domain;
+
+out:
+ mutex_unlock(&flb_obj->lock);
+ liveupdate_flb_put_incoming(&iommu_flb);
+ return domain;
+}
diff --git a/include/linux/iommu-liveupdate.h b/include/linux/iommu-liveupdate.h
index 06f66cc8f526..3ff537ae081d 100644
--- a/include/linux/iommu-liveupdate.h
+++ b/include/linux/iommu-liveupdate.h
@@ -64,8 +64,51 @@ static inline void *iommu_domain_restored_state(struct iommu_domain *domain)
return NULL;
}

+/**
+ * dev_iommu_restored_state() - Get restored state of a device
+ * @dev: Target device
+ *
+ * Return: Restored state pointer or NULL.
+ */
+static inline void *dev_iommu_restored_state(struct device *dev)
+{
+ struct iommu_device_ser *ser;
+
+ if (!dev->iommu)
+ return NULL;
+
+ ser = READ_ONCE(dev->iommu->device_ser);
+ if (ser && (ser->hdr.flags & IOMMU_SER_FLAG_INCOMING))
+ return ser;
+
+ return NULL;
+}
+
+/**
+ * dev_iommu_restore_did() - Get restored domain ID for a device
+ * @dev: Target device
+ * @domain: Target domain
+ *
+ * Fetches the domain ID preserved for @dev and @domain across Live Update.
+ *
+ * Return: Domain ID or -1 on error.
+ */
+static inline int dev_iommu_restore_did(struct device *dev, struct iommu_domain *domain)
+{
+ struct iommu_device_ser *ser = dev_iommu_restored_state(dev);
+
+ if (ser && iommu_domain_restored_state(domain))
+ return ser->domain_iommu_ser.attachment_id;
+
+ return -1;
+}
+
+struct iommu_domain *iommu_restore_domain(struct device *dev,
+ struct iommu_device_ser *ser,
+ void **owner);
int iommu_for_each_preserved_device(iommu_preserved_device_iter_fn fn,
void *arg);
+void iommu_init_device_preserved_data(struct device *dev);
struct iommu_hw_ser *iommu_get_preserved_data(u64 token, enum iommu_type_ser type);
int iommu_preserve_domain(struct iommu_domain *domain, struct iommu_domain_ser **ser);
void iommu_unpreserve_domain(struct iommu_domain *domain);
@@ -98,16 +141,37 @@ static inline void *dev_iommu_preserved_state(struct device *dev)
return NULL;
}

+static inline void *dev_iommu_restored_state(struct device *dev)
+{
+ return NULL;
+}
+
+static inline int dev_iommu_restore_did(struct device *dev, struct iommu_domain *domain)
+{
+ return -1;
+}
+
static inline void *iommu_domain_restored_state(struct iommu_domain *domain)
{
return NULL;
}

+static inline struct iommu_domain *iommu_restore_domain(struct device *dev,
+ struct iommu_device_ser *ser,
+ void **owner)
+{
+ return NULL;
+}
+
static inline int iommu_for_each_preserved_device(iommu_preserved_device_iter_fn fn, void *arg)
{
return -EOPNOTSUPP;
}

+static inline void iommu_init_device_preserved_data(struct device *dev)
+{
+}
+
static inline struct iommu_hw_ser *iommu_get_preserved_data(u64 token, enum iommu_type_ser type)
{
return NULL;
--
2.55.0.1082.g2b9226bbc0-goog