Re: [PATCH v8 11/18] iommu/vt-d: Restore IOMMU state and reclaimed domain ids

From: Baolu Lu

Date: Fri Dec 04 2026 - 21:47:26 EST


On 12/5/2026 8:03 AM, Samiullah Khawaja wrote:
diff --git a/drivers/iommu/intel/liveupdate.c b/drivers/iommu/intel/ liveupdate.c
index c0c121bc3ea5..d43947879e78 100644
--- a/drivers/iommu/intel/liveupdate.c
+++ b/drivers/iommu/intel/liveupdate.c
@@ -304,6 +304,145 @@ static int preserve_iommu_context_tables(struct device_domain_info *info)
     return 0;
 }
+static int restore_context_table(struct intel_iommu *iommu,
+                 struct iommu_hw_ser *ser,
+                 u8 bus, u8 devfn, u64 root_val)
+{
+    bool present = root_val & 1;
+
+    /*
+     * The root entries of unpreserved context tables are cleared during
+     * shutdown, so the preserved bitmap must match the present bits.
+     */
+    if (is_context_table_preserved(iommu, ser, bus, devfn) != present)
+        return -EINVAL;
+
+    if (present)
+        iommu_restore_pages(root_val & VTD_PAGE_MASK);
+
+    return 0;
+}
+
+static int restore_iommu_context(struct intel_iommu *iommu,
+                 struct iommu_hw_ser *ser)
+{
+    struct root_entry *root;
+    int ret;
+    int i;
+
+    for (i = 0; i < ROOT_ENTRY_NR; i++) {
+        root = &iommu->root_entry[i];
+
+        ret = restore_context_table(iommu, ser, i, 0, root->lo);
+        if (ret)
+            return ret;
+
+        if (!sm_supported(iommu))
+            continue;
+
+        ret = restore_context_table(iommu, ser, i, 0x80, root->hi);
+        if (ret)
+            return ret;
+    }
+
+    return 0;
+}
+
+static int _restore_used_domain_ids(struct iommu_device_ser *ser, void *arg)
+{
+    int id = ser->attachment.attachment_id;
+    struct iommu_hw_ser *iommu_hw_ser;
+    struct intel_iommu *iommu = arg;
+
+    if (WARN_ON(!ser->iommu_phys))
+        return 0;
+
+    iommu_hw_ser = phys_to_virt(ser->iommu_phys);
+    if (iommu_hw_ser->type != IOMMU_SER_TYPE_INTEL)
+        return 0;
+
+    /* Only allocate domain ID from associated IOMMU HW unit */
+    if (iommu_hw_ser->intel.phys_addr != iommu->reg_phys)
+        return 0;
+
+    /* An out of range domain ID means the preserved state is corrupt. */
+    if (id < 0 || id >= iommu->max_domain_id)
+        return -EINVAL;
+
+    return reserve_domain_id(iommu, id);
+}
+
+/**
+ * intel_iommu_liveupdate_restore_root_table() - Restore root table and reclaim domain IDs
+ * @iommu: Target IOMMU
+ * @iommu_ser: Serialized IOMMU hardware state from previous kernel
+ *
+ * Restores the preserved root table and context tables for the IOMMU hardware
+ * instance across Live Update, and reclaims all domain IDs previously allocated
+ * to preserved devices so they are not reused.
+ *
+ * The preserved devices keep doing DMA through the preserved translation
+ * tables, so a failure to restore them is fatal.
+ */
+void intel_iommu_liveupdate_restore_root_table(struct intel_iommu *iommu,
+                           struct iommu_hw_ser *iommu_ser)

The name is not accurate because this function also restores the context
tables and reclaims domain IDs.

How about intel_iommu_liveupdate_restore()?

As you suggested above and in my reply, I can move the sanity checks and
other initialization like reclaiming domain ids to a separate function
intel_iommu_liveupdate_restore(). And this function can continue to
restore only root table and context tables?

Or this function can be renamed and called earlier in the lookup loop,
but that means the root_entry will be set earlier and that is
inconsistent.

Yes, either way works for me.

Thanks,
baolu