[RFC PATCH 14/15] iommu/vt-d: sanitize restored root table and iommu contexts

Samiullah Khawaja
2025-09-28T19:08:02Z

The persisted root table will contain context entries from the previous
kernel. Sanitize the root table entries by setting them all to zero.

This is temporary, the context entries for the persisted devices need to
be kept intact.

Signed-off-by: Samiullah Khawaja <skhawaja@google.com>
---
drivers/iommu/intel/liveupdate.c | 22 ++++++++++++++++++++++
1 file changed, 22 insertions(+)

diff --git a/drivers/iommu/intel/liveupdate.c b/drivers/iommu/intel/liveupdate.c
index 755325a5225c..3783632cf634 100644
--- a/drivers/iommu/intel/liveupdate.c
+++ b/drivers/iommu/intel/liveupdate.c
@@ -306,6 +306,26 @@ static struct iommu_ser *get_liveupdate_state(void)
return ser;
}

+static void sanitize_iommu_context(struct intel_iommu *iommu)
+{
+ struct context_entry *context;
+ int i;
+
+ /* TODO: Keep the context entries for the preserved devices. */
+ for (i = 0; i < ROOT_ENTRY_NR; i++) {
+ context = iommu_context_addr(iommu, i, 0, 0);
+ if (context)
+ memset(context, 0, PAGE_SIZE);
+
+ if (!sm_supported(iommu))
+ continue;
+
+ context = iommu_context_addr(iommu, i, 0x80, 0);
+ if (context)
+ memset(context, 0, PAGE_SIZE);
+ }
+}
+
static int restore_iommu_context(struct intel_iommu *iommu)
{
struct context_entry *context;
@@ -324,6 +344,8 @@ static int restore_iommu_context(struct intel_iommu *iommu)
BUG_ON(!kho_restore_folio(virt_to_phys(context)));
}

+ sanitize_iommu_context(iommu);
+
return ret;
}

--
2.51.0.536.g15c5d4f767-goog