Skip to contentSkip to conversation

iommu: Add a check to avoid invalid iotlb sync

PATCH Superseded chenxiang 3 participants 6 messages Mar 27, 2021 – Mar 30, 2021 IOMMU

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

chenxiang

author
#0 lore raw

PATCH 1/1 iommu: Add a check to avoid invalid iotlb sync

From: Xiang Chen <[email protected]>

Currently it will send a iotlb sync at end of iommu unmap even if iotlb_gather is not valid (iotlb_gather->pgsize = 0). Actually it is not necessary, so add a check to avoid invalid iotlb sync.

---
 include/linux/iommu.h | 3 +++
 1 file changed, 3 insertions(+)
include/linux/iommu.h +3 -0
index 9ca6e6b..6afa61b 100644
@@ -529,6 +529,9 @@ static inline void iommu_flush_iotlb_all(struct iommu_domain *domain)
 static inline void iommu_iotlb_sync(struct iommu_domain *domain,
 				  struct iommu_iotlb_gather *iotlb_gather)
 {
+	if (!iotlb_gather->pgsize)
+		return;
+
 	if (domain->ops->iotlb_sync)
 		domain->ops->iotlb_sync(domain, iotlb_gather);
 
Signature · 6 lines
2.8.1

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

From: Xiang Chen <[email protected]>

Currently it will send a iotlb sync at end of iommu unmap even if iotlb_gather is not valid (iotlb_gather->pgsize = 0). Actually it is not necessary, so add a check to avoid invalid iotlb sync.

---
 include/linux/iommu.h | 3 +++
 1 file changed, 3 insertions(+)
include/linux/iommu.h +0 -0
index 9ca6e6b..6afa61b 100644

include/linux/iommu.h static inline void iommu_flush_iotlb_all(struct iommu_domain *domain) · line 533

Quoted 5 lines
include/linux/iommu.h +2 -0
@@ -529,6 +529,9 @@ static inline void iommu_flush_iotlb_all(struct iommu_domain *domain)
 static inline void iommu_iotlb_sync(struct iommu_domain *domain,
 				  struct iommu_iotlb_gather *iotlb_gather)
 {
+	if (!iotlb_gather->pgsize)
+		return;

In which circumstances does this occur?

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

#2 lore raw

Hi Will,

Quoted 24 lines · Will Deacon
Quoted 16 lines · chenxiang

From: Xiang Chen <[email protected]>

Currently it will send a iotlb sync at end of iommu unmap even if iotlb_gather is not valid (iotlb_gather->pgsize = 0). Actually it is not necessary, so add a check to avoid invalid iotlb sync.

---
  include/linux/iommu.h | 3 +++
  1 file changed, 3 insertions(+)
include/linux/iommu.h +0 -0
index 9ca6e6b..6afa61b 100644

include/linux/iommu.h static inline void iommu_flush_iotlb_all(struct iommu_domain *domain) · line 533

Quoted 5 lines
include/linux/iommu.h +2 -0
@@ -529,6 +529,9 @@ static inline void iommu_flush_iotlb_all(struct iommu_domain *domain)
  static inline void iommu_iotlb_sync(struct iommu_domain *domain,
  				  struct iommu_iotlb_gather *iotlb_gather)
  {
+	if (!iotlb_gather->pgsize)
+		return;

In which circumstances does this occur?

Quoted 1 line

Will

When iommu_unmap, it initializes iotlb_gather (then iotlb_gather->pgsize = 0).
If the unmap size = ARM_LPAE_BLOCK_SIZE(lvl) && iopte_leaf(), it will fill the iotlb_gather (set iotlb_gather->pgsize = granule); but if the unmap size = ARM_LPAE_BLOCK_SIZE(lvl) && !iopte_leaf() (tlb flush walk situation), iotlb_gather is not filled (iotlb_gather->pgsize = 0), it will still send iommu_iotlb_sync(domain, &iotlb_gather) which is actually invalid iotlb sync at last.

Quoted 3 lines

.

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

reviewer
#3 lore raw
Quoted 29 lines · chenxiang (M)

Hi Will,

Quoted 25 lines · Will Deacon
Quoted 16 lines · chenxiang

From: Xiang Chen <[email protected]>

Currently it will send a iotlb sync at end of iommu unmap even if iotlb_gather is not valid (iotlb_gather->pgsize = 0). Actually it is not necessary, so add a check to avoid invalid iotlb sync.

---
  include/linux/iommu.h | 3 +++

  1 file changed, 3 insertions(+)
include/linux/iommu.h +0 -0
index 9ca6e6b..6afa61b 100644

include/linux/iommu.h static inline void iommu_flush_iotlb_all(struct · line 534

Quoted 6 lines
include/linux/iommu.h +2 -0
@@ -529,6 +529,9 @@ static inline void iommu_flush_iotlb_all(struct 
 iommu_domain *domain)
   static inline void iommu_iotlb_sync(struct iommu_domain *domain,
                     struct iommu_iotlb_gather *iotlb_gather)
   {
+    if (!iotlb_gather->pgsize)
+        return;

In which circumstances does this occur?

Quoted 6 lines
Quoted 1 line

Will

When iommu_unmap, it initializes iotlb_gather (then iotlb_gather->pgsize = 0).
If the unmap size = ARM_LPAE_BLOCK_SIZE(lvl) && iopte_leaf(), it will fill the iotlb_gather (set iotlb_gather->pgsize = granule); but if the unmap size = ARM_LPAE_BLOCK_SIZE(lvl) && !iopte_leaf() (tlb flush walk situation), iotlb_gather is not filled (iotlb_gather->pgsize = 0), it will still send iommu_iotlb_sync(domain, &iotlb_gather) which is actually invalid iotlb sync at last.

I guess this can happen in DMA API usage if we've previously mapped a block's worth of scatterlist pages into a block-=sized IOVA region, but this is not the place to do anything about it. The main thing this patch will do is break all the other drivers that implement .iotlb_sync but do not use iotlb_gather.

By all means optimise SMMUv3's sync behaviour, but the only valid place to do that is in SMMUv3's own sync callback. These particular details are beyond what the IOMMU core knows about.

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

maintainer
#4 lore raw
Quoted 48 lines · Robin Murphy
Quoted 31 lines · chenxiang (M)

Hi Will,

Quoted 25 lines · Will Deacon
Quoted 16 lines · chenxiang

From: Xiang Chen <[email protected]>

Currently it will send a iotlb sync at end of iommu unmap even if iotlb_gather is not valid (iotlb_gather->pgsize = 0). Actually it is not necessary, so add a check to avoid invalid iotlb sync.

---
  include/linux/iommu.h | 3 +++

  1 file changed, 3 insertions(+)
include/linux/iommu.h +0 -0
index 9ca6e6b..6afa61b 100644

include/linux/iommu.h static inline void · line 534

Quoted 6 lines
include/linux/iommu.h +2 -0
@@ -529,6 +529,9 @@ static inline void
 iommu_flush_iotlb_all(struct iommu_domain *domain)
   static inline void iommu_iotlb_sync(struct iommu_domain *domain,
                     struct iommu_iotlb_gather *iotlb_gather)
   {
+    if (!iotlb_gather->pgsize)
+        return;

In which circumstances does this occur?

Quoted 1 line

Will

Quoted 9 lines

When iommu_unmap, it initializes iotlb_gather (then iotlb_gather->pgsize = 0).
If the unmap size = ARM_LPAE_BLOCK_SIZE(lvl) && iopte_leaf(), it will fill the iotlb_gather (set iotlb_gather->pgsize = granule); but if the unmap size = ARM_LPAE_BLOCK_SIZE(lvl) && !iopte_leaf() (tlb flush walk situation), iotlb_gather is not filled (iotlb_gather->pgsize = 0),
it will still send iommu_iotlb_sync(domain, &iotlb_gather) which is actually invalid iotlb sync at last.

I guess this can happen in DMA API usage if we've previously mapped a block's worth of scatterlist pages into a block-=sized IOVA region, but this is not the place to do anything about it. The main thing this patch will do is break all the other drivers that implement .iotlb_sync but do not use iotlb_gather.

Quoted 3 lines

By all means optimise SMMUv3's sync behaviour, but the only valid place to do that is in SMMUv3's own sync callback. These particular details are beyond what the IOMMU core knows about.

Yes, that's where I was heading. Chenxiang, please could you send a v2 with the check inside arm_smmu_iotlb_sync() instead?

Thanks,

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

#5 lore raw
Quoted 47 lines · Will Deacon
Quoted 46 lines · Robin Murphy
Quoted 30 lines · chenxiang (M)

Hi Will,

Quoted 25 lines · Will Deacon
Quoted 16 lines · chenxiang

From: Xiang Chen <[email protected]>

Currently it will send a iotlb sync at end of iommu unmap even if iotlb_gather is not valid (iotlb_gather->pgsize = 0). Actually it is not necessary, so add a check to avoid invalid iotlb sync.

---
   include/linux/iommu.h | 3 +++
   1 file changed, 3 insertions(+)
include/linux/iommu.h +0 -0
index 9ca6e6b..6afa61b 100644

include/linux/iommu.h static inline void · line 534

Quoted 6 lines
include/linux/iommu.h +2 -0
@@ -529,6 +529,9 @@ static inline void
 iommu_flush_iotlb_all(struct iommu_domain *domain)
   static inline void iommu_iotlb_sync(struct iommu_domain *domain,
                     struct iommu_iotlb_gather *iotlb_gather)
   {
+    if (!iotlb_gather->pgsize)
+        return;

In which circumstances does this occur?

Quoted 1 line

Will

Quoted 9 lines

When iommu_unmap, it initializes iotlb_gather (then iotlb_gather->pgsize = 0).
If the unmap size = ARM_LPAE_BLOCK_SIZE(lvl) && iopte_leaf(), it will fill the iotlb_gather (set iotlb_gather->pgsize = granule); but if the unmap size = ARM_LPAE_BLOCK_SIZE(lvl) && !iopte_leaf() (tlb flush walk situation), iotlb_gather is not filled (iotlb_gather->pgsize = 0),
it will still send iommu_iotlb_sync(domain, &iotlb_gather) which is actually invalid iotlb sync at last.

I guess this can happen in DMA API usage if we've previously mapped a block's worth of scatterlist pages into a block-=sized IOVA region, but this is not the place to do anything about it. The main thing this patch will do is break all the other drivers that implement .iotlb_sync but do not use iotlb_gather.

Quoted 5 lines
Quoted 3 lines

By all means optimise SMMUv3's sync behaviour, but the only valid place to do that is in SMMUv3's own sync callback. These particular details are beyond what the IOMMU core knows about.

Yes, that's where I was heading. Chenxiang, please could you send a v2 with the check inside arm_smmu_iotlb_sync() instead?

Ok, thanks, i will send a v2 later.

Quoted 7 lines

Thanks,

Will

.

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