Re: [PATCH v6 08/18] iommu: Add APIs to get iommu and device preserved state

From: Baolu Lu

Date: Fri Oct 30 2026 - 02:03:25 EST


On 10/25/2026 10:19 AM, Samiullah Khawaja wrote:
The preserved state of the device and IOMMU needs to be fetched during
shutdown and boot in the next kernel. Add APIs that can be used to fetch
the preserved state of a device and IOMMU. The APIs will only be used
during shutdown and after liveupdate so no locking needed.

Reviewed-by: Pranjal Shrivastava <praan@xxxxxxxxxx>
Signed-off-by: Samiullah Khawaja <skhawaja@xxxxxxxxxx>
---
drivers/iommu/liveupdate.c | 88 ++++++++++++++++++++++++++++++++
include/linux/iommu-liveupdate.h | 70 +++++++++++++++++++++++++
2 files changed, 158 insertions(+)


[...]

+
+/**
+ * iommu_get_preserved_data() - Get preserved data for an IOMMU HW
+ * @token: Token used to preserve this IOMMU HW
+ * @type: IOMMU type in preserved state
+ *
+ * Gets the preserved state of an IOMMU HW using token and the IOMMU type.
+ *
+ * Return: struct iommu_hw_ser on success, NULL if no preserved state found.
+ */
+struct iommu_hw_ser *iommu_get_preserved_data(u64 token, enum iommu_type_ser type)
+{
+ struct iommu_hw_ser *iommu_ser;
+ struct iommu_flb_obj *flb_obj;
+ struct kho_block_set_it it;
+ int ret;
+
+ ret = iommu_liveupdate_flb_get_incoming(&flb_obj);

iommu_get_preserved_data() returns the incoming state, while
iommu_preserved_state() returns the outgoing state. The naming is
confusing.

How about using "preserved" for the old kernel before kexec and
"restored" for the new kernel after kexec? For example,
iommu_get_restored_state() would be consistent with
dev_iommu_restored_state() and iommu_domain_restored_state().

+ if (ret)
+ return NULL;
+
+ iommu_ser_set_for_each(&flb_obj->iommu_set, it, iommu_ser) {
+ if (iommu_ser->token == token && iommu_ser->type == type)
+ break;
+ }
+
+ liveupdate_flb_put_incoming(&iommu_flb);
+ return iommu_ser;
+}
+EXPORT_SYMBOL(iommu_get_preserved_data);

Why are different export macros used in the same file?

EXPORT_SYMBOL(iommu_get_preserved_data);
EXPORT_SYMBOL_GPL(iommu_preserve_domain);

Should these all be EXPORT_SYMBOL_GPL?

+
/**
* iommu_preserve_domain() - Preserve an IOMMU domain across live update
* @domain: Domain to preserve
diff --git a/include/linux/iommu-liveupdate.h b/include/linux/iommu-liveupdate.h
index cdf8a82b7f1d..10fcdd41c7a1 100644
--- a/include/linux/iommu-liveupdate.h
+++ b/include/linux/iommu-liveupdate.h
@@ -13,6 +13,16 @@
#include <linux/liveupdate.h>
#include <linux/kho/abi/iommu.h>
+/**
+ * iommu_preserved_device_iter_fn - Callback for iterating preserved devices
+ * @ser: Pointer to serialized device state
+ * @arg: User-defined context argument
+ *
+ * Return: 0 to continue iteration, or a negative error code to abort.
+ */
+typedef int (*iommu_preserved_device_iter_fn)(struct iommu_device_ser *ser,
+ void *arg);
+
#ifdef CONFIG_IOMMU_LIVEUPDATE
int iommu_liveupdate_register_flb(struct liveupdate_file_handler *handler);
void iommu_liveupdate_unregister_flb(struct liveupdate_file_handler *handler);
@@ -52,6 +62,46 @@ static inline void *dev_iommu_preserved_state(struct device *dev)
return NULL;
}
+/**
+ * iommu_domain_restored_state() - Get restored state of an IOMMU domain
+ * @domain: Pointer to struct iommu_domain to get restored state of.
+ *
+ * Return: Pointer to restored state on success, NULL on error.
+ */
+static inline void *iommu_domain_restored_state(struct iommu_domain *domain)
+{
+ struct iommu_domain_ser *ser;
+
+ ser = domain->domain_ser;
+ if (ser && (ser->hdr.flags & IOMMU_SER_FLAG_INCOMING))
+ return ser;
+
+ 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;
+}
+
+int iommu_for_each_preserved_device(iommu_preserved_device_iter_fn fn,
+ void *arg);
+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);
int iommu_preserve_device(struct iommu_domain *domain,
@@ -88,6 +138,26 @@ 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 void *iommu_domain_restored_state(struct iommu_domain *domain)
+{
+ return NULL;
+}
+
+static inline int iommu_for_each_preserved_device(iommu_preserved_device_iter_fn fn, void *arg)
+{
+ return -EOPNOTSUPP;
+}
+
+static inline struct iommu_hw_ser *iommu_get_preserved_data(u64 token, enum iommu_type_ser type)
+{
+ return NULL;
+}
+
static inline int iommu_preserve_domain(struct iommu_domain *domain, struct iommu_domain_ser **ser)
{
return -EOPNOTSUPP;

Thanks,
baolu