[RFC PATCH v2 09/32] iommu: Implement IOMMU LU FLB preserve/unpreserve callbacks

From: Samiullah Khawaja
Date: Tue Dec 02 2025 - 18:05:14 EST


Use KHO preserve memory alloc/free helper functions to allocate memory
for the IOMMU LU FLB. The serialization structs for device, domain and
iommu are allocated.

Signed-off-by: Samiullah Khawaja <skhawaja@xxxxxxxxxx>
---
drivers/iommu/liveupdate.c | 71 +++++++++++++++++++++++++++++++++++++-
1 file changed, 70 insertions(+), 1 deletion(-)

diff --git a/drivers/iommu/liveupdate.c b/drivers/iommu/liveupdate.c
index 93e5691a8c1f..21ce7be9b87e 100644
--- a/drivers/iommu/liveupdate.c
+++ b/drivers/iommu/liveupdate.c
@@ -10,15 +10,84 @@
#include <linux/kexec_handover.h>
#include <linux/liveupdate.h>
#include <linux/iommu-lu.h>
+#include <linux/iommu.h>
#include <linux/errno.h>

+static void iommu_liveupdate_free_objs(u64 next, bool incoming)
+{
+ struct iommu_objs_ser *objs;
+
+ while (next) {
+ objs = __va(next);
+ next = objs->next_objs;
+
+ if (!incoming)
+ kho_unpreserve_free(objs);
+ else
+ folio_put(virt_to_folio(objs));
+ }
+}
+
static void iommu_liveupdate_flb_free(struct iommu_lu_flb_obj *obj)
{
+ if (obj->iommu_domains)
+ iommu_liveupdate_free_objs(obj->ser->iommu_domains_phys, false);
+
+ if (obj->devices)
+ iommu_liveupdate_free_objs(obj->ser->devices_phys, false);
+
+ if (obj->iommus)
+ iommu_liveupdate_free_objs(obj->ser->iommus_phys, false);
+
+ kho_unpreserve_free(obj->ser);
}

static int iommu_liveupdate_flb_preserve(struct liveupdate_flb_op_args *argp)
{
- return -EOPNOTSUPP;
+ struct iommu_lu_flb_obj *obj;
+ struct iommu_lu_flb_ser *ser;
+ void *mem;
+
+ obj = kzalloc(sizeof(*obj), GFP_KERNEL);
+ if (!obj)
+ return -ENOMEM;
+
+ mutex_init(&obj->lock);
+ mem = kho_alloc_preserve(sizeof(*ser));
+ if (IS_ERR(mem))
+ goto err_free;
+
+ ser = mem;
+ obj->ser = ser;
+
+ mem = kho_alloc_preserve(PAGE_SIZE);
+ if (IS_ERR(mem))
+ goto err_free;
+
+ obj->iommu_domains = mem;
+ ser->iommu_domains_phys = virt_to_phys(obj->iommu_domains);
+
+ mem = kho_alloc_preserve(PAGE_SIZE);
+ if (IS_ERR(mem))
+ goto err_free;
+
+ obj->devices = mem;
+ ser->devices_phys = virt_to_phys(obj->devices);
+
+ mem = kho_alloc_preserve(PAGE_SIZE);
+ if (IS_ERR(mem))
+ goto err_free;
+
+ obj->iommus = mem;
+ ser->iommus_phys = virt_to_phys(obj->iommus);
+
+ argp->obj = obj;
+ argp->data = virt_to_phys(ser);
+ return 0;
+
+err_free:
+ iommu_liveupdate_flb_free(obj);
+ return PTR_ERR(mem);
}

static void iommu_liveupdate_flb_unpreserve(struct liveupdate_flb_op_args *argp)
--
2.52.0.158.g65b55ccf14-goog