Skip to contentSkip to conversation

iommu/vt-d: check alignment before using psi

PATCH In review David Stevens 3 participants 3 messages Mar 16, 2022 – Mar 18, 2022

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iommu/vt-d: check alignment before using psi

From: David Stevens <[email protected]>

Fall back to domain selective flush if the target address is not aligned to the mask being used for invalidation. This is necessary because page selective invalidation masks out the lower order bits of the target address based on the mask value, so if a non-aligned address is targeted for psi, then mappings at the end of [pfn, pfn+pages) may not properly be flushed from the iotlb.

This is not normally an issue because iova.c always allocates iovas that are aligned to their size. However, iovas which come from other sources (e.g. userspace via VFIO) may not be aligned.

---
 drivers/iommu/intel/iommu.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/iommu/intel/iommu.c +2 -1
index 5b196cfe9ed2..c122686e0a5c 100644
@@ -1735,7 +1735,8 @@ static void iommu_flush_iotlb_psi(struct intel_iommu *iommu,
 		 * and the base address is naturally aligned to the size.
 		 */
 		if (!cap_pgsel_inv(iommu->cap) ||
-		    mask > cap_max_amask_val(iommu->cap))
+		    mask > cap_max_amask_val(iommu->cap) ||
+		    unlikely(((1 << mask) - 1) & pfn))
 			iommu->flush.flush_iotlb(iommu, did, 0, 0,
 							DMA_TLB_DSI_FLUSH);
 		else
Signature · 6 lines
2.35.1.723.g4982287a31-goog

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
#1 lore raw
Quoted 19 lines

-----Original Message-----
From: iommu <[email protected]> On Behalf Of David Stevens
Sent: Wednesday, March 16, 2022 1:07 PM
To: Lu Baolu <[email protected]>
Cc: [email protected]; [email protected]; David Stevens <[email protected]>
Subject: [PATCH] iommu/vt-d: check alignment before using psi

From: David Stevens <[email protected]>

Fall back to domain selective flush if the target address is not aligned to the mask being used for invalidation. This is necessary because page selective invalidation masks out the lower order bits of the target address based on the mask value, so if a non-aligned address is targeted for psi, then mappings at the end of [pfn, pfn+pages) may not properly be flushed from the iotlb.

Quoted 3 lines

This is not normally an issue because iova.c always allocates iovas that are aligned to their size. However, iovas which come from other sources (e.g. userspace via VFIO) may not be aligned.

Indeed, the dma_map/unmap always use alloca_iova(..., true) to allocate iova, which means size_aligned. But the iova used by userspace might not be size aligned. And it seems we cannot expect userspace can always use size_aligned iova base address.

BR,
Tina

Quoted 21 lines
---
 drivers/iommu/intel/iommu.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/iommu/intel/iommu.c +2 -1
index 5b196cfe9ed2..c122686e0a5c 100644
@@ -1735,7 +1735,8 @@ static void iommu_flush_iotlb_psi(struct
 intel_iommu *iommu,
 		 * and the base address is naturally aligned to the size.
 		 */
 		if (!cap_pgsel_inv(iommu->cap) ||
-		    mask > cap_max_amask_val(iommu->cap))
+		    mask > cap_max_amask_val(iommu->cap) ||
+		    unlikely(((1 << mask) - 1) & pfn))
 			iommu->flush.flush_iotlb(iommu, did, 0, 0,
 
	DMA_TLB_DSI_FLUSH);
Quoted 8 lines

else
--
2.35.1.723.g4982287a31-goog

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

#2 lore raw
In reply to David Stevens
Quoted 7 lines

From: David Stevens <[email protected]>
Sent: Wednesday, March 16, 2022 1:07 PM

From: David Stevens <[email protected]>

Fall back to domain selective flush if the target address is not aligned to the mask being used for invalidation. This is necessary because page

using domain selective flush is a bit conservative. What about trying mask+1 first?

Quoted 29 lines

selective invalidation masks out the lower order bits of the target address based on the mask value, so if a non-aligned address is targeted for psi, then mappings at the end of [pfn, pfn+pages) may not properly be flushed from the iotlb.

This is not normally an issue because iova.c always allocates iovas that are aligned to their size. However, iovas which come from other sources (e.g. userspace via VFIO) may not be aligned.

---
 drivers/iommu/intel/iommu.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/iommu/intel/iommu.c +2 -1
index 5b196cfe9ed2..c122686e0a5c 100644
@@ -1735,7 +1735,8 @@ static void iommu_flush_iotlb_psi(struct
 intel_iommu *iommu,
 		 * and the base address is naturally aligned to the size.
 		 */
 		if (!cap_pgsel_inv(iommu->cap) ||
-		    mask > cap_max_amask_val(iommu->cap))
+		    mask > cap_max_amask_val(iommu->cap) ||
+		    unlikely(((1 << mask) - 1) & pfn))
 			iommu->flush.flush_iotlb(iommu, did, 0, 0,
 
	DMA_TLB_DSI_FLUSH);
Quoted 3 lines

else
--
2.35.1.723.g4982287a31-goog

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu