[RFC PATCH 05/15] iommu: Introduce API to preserve iommu domain

Samiullah Khawaja
2025-09-28T19:07:49Z

Add an API that can be called by the iommu users to preserve iommu
domain. Currently it only marks the iommu_domain as preserved.

Signed-off-by: Samiullah Khawaja <skhawaja@google.com>
---
drivers/iommu/iommu.c | 20 ++++++++++++++++++++
include/linux/iommu.h | 10 ++++++++++
2 files changed, 30 insertions(+)

diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c
index bfa7c8653720..2e6e9c3f26ec 100644
--- a/drivers/iommu/iommu.c
+++ b/drivers/iommu/iommu.c
@@ -2002,6 +2002,10 @@ static void iommu_domain_init(struct iommu_domain *domain, unsigned int type,
domain->owner = ops;
if (!domain->ops)
domain->ops = ops->default_domain_ops;
+
+#ifdef CONFIG_LIVEUPDATE
+ atomic_set(&domain->preserved, 0);
+#endif
}

static struct iommu_domain *
@@ -2054,6 +2058,22 @@ EXPORT_SYMBOL_GPL(iommu_paging_domain_alloc_flags);

#ifdef CONFIG_LIVEUPDATE
DECLARE_RWSEM(liveupdate_state_rwsem);
+
+int iommu_domain_preserve(struct iommu_domain *domain)
+{
+ int ret;
+
+ lockdep_assert_held(&liveupdate_state_rwsem);
+ if (!domain->ops->preserve)
+ return -EOPNOTSUPP;
+
+ ret = domain->ops->preserve(domain);
+ if (!ret)
+ atomic_set(&domain->preserved, 1);
+
+ return ret;
+}
+EXPORT_SYMBOL_GPL(iommu_domain_preserve);
#endif

void iommu_domain_free(struct iommu_domain *domain)
diff --git a/include/linux/iommu.h b/include/linux/iommu.h
index 40801d8eac61..aafd06134f5c 100644
--- a/include/linux/iommu.h
+++ b/include/linux/iommu.h
@@ -14,6 +14,7 @@
#include <linux/err.h>
#include <linux/of.h>
#include <linux/iova_bitmap.h>
+#include <linux/atomic.h>
#include <uapi/linux/iommufd.h>

#define IOMMU_READ (1 << 0)
@@ -248,6 +249,10 @@ struct iommu_domain {
struct list_head next;
};
};
+
+#ifdef CONFIG_LIVEUPDATE
+ atomic_t preserved;
+#endif
};

static inline bool iommu_is_dma_domain(struct iommu_domain *domain)
@@ -915,6 +920,11 @@ static inline struct iommu_domain *iommu_paging_domain_alloc(struct device *dev)
{
return iommu_paging_domain_alloc_flags(dev, 0);
}
+
+#ifdef CONFIG_LIVEUPDATE
+int iommu_domain_preserve(struct iommu_domain *domain);
+#endif
+
extern void iommu_domain_free(struct iommu_domain *domain);
extern int iommu_attach_device(struct iommu_domain *domain,
struct device *dev);
--
2.51.0.536.g15c5d4f767-goog