Skip to contentSkip to conversation

iommu/vt-d: Flush cache for PASID table before using it

PATCH Superseded Dmytro Maluka 2 participants 2 messages Dec 21, 2025

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iommu/vt-d: Flush cache for PASID table before using it

When writing the address of a freshly allocated zero-initialized PASID table to a PASID directory entry, do that after the CPU cache flush for this PASID table, not before it, to avoid the time window when this PASID table may be already used by non-coherent IOMMU hardware while its contents in RAM is still some random old data, not zero-initialized.

---
 drivers/iommu/intel/pasid.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/iommu/intel/pasid.c +4 -3
index 3e2255057079..77b9b147ab50 100644
@@ -153,6 +153,9 @@ static struct pasid_entry *intel_pasid_get_entry(struct device *dev, u32 pasid)
 		if (!entries)
 			return NULL;
 
+		if (!ecap_coherent(info->iommu->ecap))
+			clflush_cache_range(entries, VTD_PAGE_SIZE);
+
 		/*
 		 * The pasid directory table entry won't be freed after
 		 * allocation. No worry about the race with free and
@@ -165,10 +168,8 @@ static struct pasid_entry *intel_pasid_get_entry(struct device *dev, u32 pasid)
 			iommu_free_pages(entries);
 			goto retry;
 		}
-		if (!ecap_coherent(info->iommu->ecap)) {
-			clflush_cache_range(entries, VTD_PAGE_SIZE);
+		if (!ecap_coherent(info->iommu->ecap))
 			clflush_cache_range(&dir[dir_index].val, sizeof(*dir));
-		}
 	}
 
 	return &entries[index];
Signature · 1 line
2.47.2
maintainer
#1 lore raw
Quoted 6 lines · Dmytro Maluka

When writing the address of a freshly allocated zero-initialized PASID table to a PASID directory entry, do that after the CPU cache flush for this PASID table, not before it, to avoid the time window when this PASID table may be already used by non-coherent IOMMU hardware while its contents in RAM is still some random old data, not zero-initialized.

Fixes 194b3348bdbb ("iommu/vt-d: Fix PASID directory pointer coherency")
Quoted 21 lines
---
  drivers/iommu/intel/pasid.c | 7 ++++---
  1 file changed, 4 insertions(+), 3 deletions(-)
drivers/iommu/intel/pasid.c +3 -0
index 3e2255057079..77b9b147ab50 100644
@@ -153,6 +153,9 @@ static struct pasid_entry *intel_pasid_get_entry(struct device *dev, u32 pasid)
  		if (!entries)
  			return NULL;
 
+		if (!ecap_coherent(info->iommu->ecap))
+			clflush_cache_range(entries, VTD_PAGE_SIZE);
+
  		/*
  		 * The pasid directory table entry won't be freed after
  		 * allocation. No worry about the race with free and
@@ -165,10 +168,8 @@ static struct pasid_entry *intel_pasid_get_entry(struct device *dev, u32 pasid)
  			iommu_free_pages(entries);

drivers/iommu/intel/pasid.c static struct pasid_entry *intel_pasid_get_entry(struct device *dev, u32 pasid) · line 175

Quoted 10 lines
drivers/iommu/intel/pasid.c +1 -3
  			goto retry;
  		}
-		if (!ecap_coherent(info->iommu->ecap)) {
-			clflush_cache_range(entries, VTD_PAGE_SIZE);
+		if (!ecap_coherent(info->iommu->ecap))
  			clflush_cache_range(&dir[dir_index].val, sizeof(*dir));
-		}
  	}
 
  	return &entries[index];

Thanks,
baolu