iommu/vt-d: Force to flush iotlb before creating superpage
Subsystems (3)
PATCH 1/1 iommu/vt-d: Force to flush iotlb before creating superpage
The translation caches may preserve obsolete data when the mapping size is changed, suppose the following sequence which can reveal the problem with high probability.
1.mmap(4GB,MAP_HUGETLB)
2.
while (1) {
(a) DMA MAP 0,0xa0000 (b) DMA UNMAP 0,0xa0000 (c) DMA MAP 0,0xc0000000
* DMA read IOVA 0 may failure here (Not present)
* if the problem occurs.
(d) DMA UNMAP 0,0xc0000000
}
The page table(only focus on IOVA 0) after (a) is:
PML4: 0x19db5c1003 entry:0xffff899bdcd2f000
PDPE: 0x1a1cacb003 entry:0xffff89b35b5c1000
PDE: 0x1a30a72003 entry:0xffff89b39cacb000
PTE: 0x21d200803 entry:0xffff89b3b0a72000The page table after (b) is:
PML4: 0x19db5c1003 entry:0xffff899bdcd2f000
PDPE: 0x1a1cacb003 entry:0xffff89b35b5c1000
PDE: 0x1a30a72003 entry:0xffff89b39cacb000
PTE: 0x0 entry:0xffff89b3b0a72000The page table after (c) is:
PML4: 0x19db5c1003 entry:0xffff899bdcd2f000 PDPE: 0x1a1cacb003 entry:0xffff89b35b5c1000 PDE: 0x21d200883 entry:0xffff89b39cacb000 (*)
Because the PDE entry after (b) is present, it won't be flushed even if the iommu driver flush cache when unmap, so the obsolete data may be preserved in cache, which would cause the wrong translation at end.
However, we can see the PDE entry is finally switch to 2M-superpage mapping, but it does not transform to 0x21d200883 directly:
1. PDE: 0x1a30a72003
2. __domain_mapping
dma_pte_free_pagetable
Set the PDE entry to ZERO
Set the PDE entry to 0x21d200883
So we must flush the cache after the entry switch to ZERO to avoid the obsolete info be preserved.
--- drivers/iommu/intel/iommu.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-)
drivers/iommu/intel/iommu.c +13 -2
@@ -2342,9 +2342,20 @@ static inline int hardware_largepage_caps(struct dmar_domain *domain,* removed to make room for superpage(s). * We're adding new large pages, so make sure * we don't remove their parent tables. +* +* We also need to flush the iotlb before creating +* superpage to ensure it does not perserves any +* obsolete info. */ -dma_pte_free_pagetable (domain, iov_pfn, end_pfn, - largepage_lvl +1 ); +if (dma_pte_present (pte)) { +int i; + +dma_pte_free_pagetable (domain, iov_pfn, end_pfn, + largepage_lvl +1 ); +for_each_domain_iommu (i, domain) +iommu_flush_iotlb_psi (g_iommus[i], domain, + iov_pfn, nr_pages,0 ,0 ); + } }else { pteval &= ~(uint64_t )DMA_PTE_LARGE_PAGE ; }
Signature · 6 lines
1.8.3.1 _______________________________________________ iommu mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/iommu
Lu Baolu
maintainerHi Longpeng,
Quoted 79 lines · Longpeng(Mike)
The translation caches may preserve obsolete data when the mapping size is changed, suppose the following sequence which can reveal the problem with high probability.
1.mmap(4GB,MAP_HUGETLB)
2.while (1) { (a) DMA MAP 0,0xa0000 (b) DMA UNMAP 0,0xa0000 (c) DMA MAP 0,0xc0000000 * DMA read IOVA 0 may failure here (Not present) * if the problem occurs. (d) DMA UNMAP 0,0xc0000000}
The page table(only focus on IOVA 0) after (a) is:
PML4: 0x19db5c1003 entry:0xffff899bdcd2f000 PDPE: 0x1a1cacb003 entry:0xffff89b35b5c1000 PDE: 0x1a30a72003 entry:0xffff89b39cacb000 PTE: 0x21d200803 entry:0xffff89b3b0a72000The page table after (b) is:
PML4: 0x19db5c1003 entry:0xffff899bdcd2f000 PDPE: 0x1a1cacb003 entry:0xffff89b35b5c1000 PDE: 0x1a30a72003 entry:0xffff89b39cacb000 PTE: 0x0 entry:0xffff89b3b0a72000The page table after (c) is:
PML4: 0x19db5c1003 entry:0xffff899bdcd2f000 PDPE: 0x1a1cacb003 entry:0xffff89b35b5c1000 PDE: 0x21d200883 entry:0xffff89b39cacb000 (*)Because the PDE entry after (b) is present, it won't be flushed even if the iommu driver flush cache when unmap, so the obsolete data may be preserved in cache, which would cause the wrong translation at end.
However, we can see the PDE entry is finally switch to 2M-superpage mapping, but it does not transform to
0x21d200883directly:1. PDE:
0x1a30a72003
2.__domain_mapping
dma_pte_free_pagetable
Set the PDE entry to ZERO
Set the PDE entry to0x21d200883So we must flush the cache after the entry switch to ZERO to avoid the obsolete info be preserved.
Cc David Woodhouse <[email protected]>Cc Lu Baolu <[email protected]>Cc Nadav Amit <[email protected]>Cc Alex Williamson <[email protected]>Cc Kevin Tian <[email protected]>Cc Gonglei (Arei) <[email protected]>Fixes 6491d4d02893 ("intel-iommu: Free old page tables before creating superpage")Cc <[email protected]> # v3.0+Suggested-by Lu Baolu <[email protected]>Signed-off-by Longpeng(Mike) <[email protected]>--- drivers/iommu/intel/iommu.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-)drivers/iommu/intel/iommu.c +4
-0@@ -2342,9 +2342,20 @@ static inline int hardware_largepage_caps(struct dmar_domain *domain,* removed to make room for superpage(s). * We're adding new large pages, so make sure * we don't remove their parent tables. +* +* We also need to flush the iotlb before creating +* superpage to ensure it does not perserves any +* obsolete info. */
drivers/iommu/intel/iommu.c static inline int hardware_largepage_caps(struct dmar_domain *domain, · line 2357
Quoted 10 lines
drivers/iommu/intel/iommu.c +8
-2-dma_pte_free_pagetable (domain, iov_pfn, end_pfn, - largepage_lvl +1 ); +if (dma_pte_present (pte)) { +int i; + +dma_pte_free_pagetable (domain, iov_pfn, end_pfn, + largepage_lvl +1 ); +for_each_domain_iommu (i, domain) +iommu_flush_iotlb_psi (g_iommus[i], domain, + iov_pfn, nr_pages,0 ,0 );
Thanks for patch!
How about making the flushed page size accurate? For example,
@@ -2365,8 +2365,8 @@ __domain_mapping(struct dmar_domain *domain, unsigned long iov_pfn,
dma_pte_free_pagetable(domain,iov_pfn, end_pfn,
largepage_lvl + 1);
for_each_domain_iommu(i, domain)
- iommu_flush_iotlb_psi(g_iommus[i], domain,
- iov_pfn, nr_pages, 0, 0);
+ iommu_flush_iotlb_psi(g_iommus[i], domain, iov_pfn,
+ ALIGN_DOWN(nr_pages, lvl_pages), 0, 0);
drivers/iommu/intel/iommu.c static inline int hardware_largepage_caps(struct dmar_domain *domain, · line 2361
Quoted 5 lines
drivers/iommu/intel/iommu.c +1
-0+ } }else { pteval &= ~(uint64_t )DMA_PTE_LARGE_PAGE ; }
Best regards,
baolu
Hi Baolu,
Quoted 103 lines · Lu Baolu
Hi Longpeng,
Quoted 83 lines · Longpeng(Mike)
The translation caches may preserve obsolete data when the mapping size is changed, suppose the following sequence which can reveal the problem with high probability.
1.mmap(4GB,MAP_HUGETLB)
2.
while (1) {
(a) DMA MAP 0,0xa0000
(b) DMA UNMAP 0,0xa0000
(c) DMA MAP 0,0xc0000000
* DMA read IOVA 0 may failure here (Not present)
* if the problem occurs.
(d) DMA UNMAP 0,0xc0000000
}The page table(only focus on IOVA 0) after (a) is:
PML4: 0x19db5c1003 entry:0xffff899bdcd2f000
PDPE: 0x1a1cacb003 entry:0xffff89b35b5c1000
PDE: 0x1a30a72003 entry:0xffff89b39cacb000
PTE: 0x21d200803 entry:0xffff89b3b0a72000The page table after (b) is:
PML4: 0x19db5c1003 entry:0xffff899bdcd2f000
PDPE: 0x1a1cacb003 entry:0xffff89b35b5c1000
PDE: 0x1a30a72003 entry:0xffff89b39cacb000
PTE: 0x0 entry:0xffff89b3b0a72000The page table after (c) is:
PML4: 0x19db5c1003 entry:0xffff899bdcd2f000
PDPE: 0x1a1cacb003 entry:0xffff89b35b5c1000
PDE: 0x21d200883 entry:0xffff89b39cacb000 (*)Because the PDE entry after (b) is present, it won't be flushed even if the iommu driver flush cache when unmap, so the obsolete data may be preserved in cache, which would cause the wrong translation at end.
However, we can see the PDE entry is finally switch to 2M-superpage mapping, but it does not transform to
0x21d200883directly:1. PDE:
0x1a30a72003
2.__domain_mapping
dma_pte_free_pagetable
Set the PDE entry to ZERO
Set the PDE entry to0x21d200883So we must flush the cache after the entry switch to ZERO to avoid the obsolete info be preserved.
Cc David Woodhouse <[email protected]>Cc Lu Baolu <[email protected]>Cc Nadav Amit <[email protected]>Cc Alex Williamson <[email protected]>Cc Kevin Tian <[email protected]>Cc Gonglei (Arei) <[email protected]>Fixes 6491d4d02893 ("intel-iommu: Free old page tables before creatingsuperpage")
Cc: <[email protected]> # v3.0+
Link:
https://lore.kernel.org/linux-iommu/[email protected]/Suggested-by Lu Baolu <[email protected]>Signed-off-by Longpeng(Mike) <[email protected]>---
drivers/iommu/intel/iommu.c| 15 +++++++++++++--1 file changed, 13 insertions(+), 2 deletions(-)drivers/iommu/intel/iommu.c +4
-0@@ -2342,9 +2342,20 @@ static inline int hardware_largepage_caps(structdmar_domain *domain, * removed to make room for superpage(s). * We're adding new large pages, so make sure * we don't remove their parent tables. +* +* We also need to flush the iotlb before creating +* superpage to ensure it does not perserves any +* obsolete info. */ drivers/iommu/intel/iommu.c static inline int hardware_largepage_caps(struct · line 2358
Quoted 10 lines
drivers/iommu/intel/iommu.c +8
-2- dma_pte_free_pagetable(domain, iov_pfn, end_pfn, - largepage_lvl + 1); + if (dma_pte_present(pte)) { + int i; + + dma_pte_free_pagetable(domain, iov_pfn, end_pfn, + largepage_lvl + 1); + for_each_domain_iommu(i, domain) + iommu_flush_iotlb_psi(g_iommus[i], domain, + iov_pfn, nr_pages, 0, 0);Thanks for patch!
How about making the flushed page size accurate? For example,
Review __domain_mapping(struct dmar_domain *domain, unsigned long · line 2372
Quoted 10 lines
(quoted diff) +2
-2@@ -2365,8 +2365,8 @@ __domain_mapping(struct dmar_domain *domain, unsigned long iov_pfn, dma_pte_free_pagetable(domain, iov_pfn, end_pfn, largepage_lvl + 1); for_each_domain_iommu(i, domain) - iommu_flush_iotlb_psi(g_iommus[i], domain, - iov_pfn, nr_pages, 0, 0); + iommu_flush_iotlb_psi(g_iommus[i], domain, iov_pfn, + ALIGN_DOWN(nr_pages, lvl_pages), 0, 0);
Yes, make sense.
Maybe another alternative is 'end_pfn - iova_pfn + 1', it's readable because we free pagetable with (iova_pfn, end_pfn) above. Which one do you prefer?
Quoted 10 lines
Quoted 6 lines
drivers/iommu/intel/iommu.c +1
-0+ } } else { pteval &= ~(uint64_t)DMA_PTE_LARGE_PAGE;}
Best regards,
baolu
.
Lu Baolu
maintainerQuoted 118 lines · Cloud Infrastructure Service Product Dept.)
Hi Baolu,
Quoted 103 lines · Lu Baolu
Hi Longpeng,
Quoted 83 lines · Longpeng(Mike)
The translation caches may preserve obsolete data when the mapping size is changed, suppose the following sequence which can reveal the problem with high probability.
1.mmap(4GB,MAP_HUGETLB)
2.
while (1) {
(a) DMA MAP 0,0xa0000
(b) DMA UNMAP 0,0xa0000
(c) DMA MAP 0,0xc0000000
* DMA read IOVA 0 may failure here (Not present)
* if the problem occurs.
(d) DMA UNMAP 0,0xc0000000
}The page table(only focus on IOVA 0) after (a) is:
PML4: 0x19db5c1003 entry:0xffff899bdcd2f000
PDPE: 0x1a1cacb003 entry:0xffff89b35b5c1000
PDE: 0x1a30a72003 entry:0xffff89b39cacb000
PTE: 0x21d200803 entry:0xffff89b3b0a72000The page table after (b) is:
PML4: 0x19db5c1003 entry:0xffff899bdcd2f000
PDPE: 0x1a1cacb003 entry:0xffff89b35b5c1000
PDE: 0x1a30a72003 entry:0xffff89b39cacb000
PTE: 0x0 entry:0xffff89b3b0a72000The page table after (c) is:
PML4: 0x19db5c1003 entry:0xffff899bdcd2f000
PDPE: 0x1a1cacb003 entry:0xffff89b35b5c1000
PDE: 0x21d200883 entry:0xffff89b39cacb000 (*)Because the PDE entry after (b) is present, it won't be flushed even if the iommu driver flush cache when unmap, so the obsolete data may be preserved in cache, which would cause the wrong translation at end.
However, we can see the PDE entry is finally switch to 2M-superpage mapping, but it does not transform to
0x21d200883directly:1. PDE:
0x1a30a72003
2.__domain_mapping
dma_pte_free_pagetable
Set the PDE entry to ZERO
Set the PDE entry to0x21d200883So we must flush the cache after the entry switch to ZERO to avoid the obsolete info be preserved.
Cc David Woodhouse <[email protected]>Cc Lu Baolu <[email protected]>Cc Nadav Amit <[email protected]>Cc Alex Williamson <[email protected]>Cc Kevin Tian <[email protected]>Cc Gonglei (Arei) <[email protected]>Fixes 6491d4d02893 ("intel-iommu: Free old page tables before creatingsuperpage")
Cc: <[email protected]> # v3.0+
Link:
https://lore.kernel.org/linux-iommu/[email protected]/Suggested-by Lu Baolu <[email protected]>Signed-off-by Longpeng(Mike) <[email protected]>--- drivers/iommu/intel/iommu.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-)drivers/iommu/intel/iommu.c +4
-0@@ -2342,9 +2342,20 @@ static inline int hardware_largepage_caps(structdmar_domain *domain, * removed to make room for superpage(s). * We're adding new large pages, so make sure * we don't remove their parent tables. +* +* We also need to flush the iotlb before creating +* superpage to ensure it does not perserves any +* obsolete info. */ drivers/iommu/intel/iommu.c static inline int hardware_largepage_caps(struct · line 2358
Quoted 10 lines
drivers/iommu/intel/iommu.c +8
-2- dma_pte_free_pagetable(domain, iov_pfn, end_pfn, - largepage_lvl + 1); + if (dma_pte_present(pte)) { + int i; + + dma_pte_free_pagetable(domain, iov_pfn, end_pfn, + largepage_lvl + 1); + for_each_domain_iommu(i, domain) + iommu_flush_iotlb_psi(g_iommus[i], domain, + iov_pfn, nr_pages, 0, 0);Thanks for patch!
How about making the flushed page size accurate? For example,
Review __domain_mapping(struct dmar_domain *domain, unsigned long · line 2372
Quoted 10 lines
(quoted diff) +2
-2@@ -2365,8 +2365,8 @@ __domain_mapping(struct dmar_domain *domain, unsigned long iov_pfn, dma_pte_free_pagetable(domain, iov_pfn, end_pfn, largepage_lvl + 1); for_each_domain_iommu(i, domain) - iommu_flush_iotlb_psi(g_iommus[i], domain, - iov_pfn, nr_pages, 0, 0); + iommu_flush_iotlb_psi(g_iommus[i], domain, iov_pfn, + ALIGN_DOWN(nr_pages, lvl_pages), 0, 0);Yes, make sense.
Quoted 2 lines
Maybe another alternative is '
end_pfn-iova_pfn+ 1', it's readable because we free pagetable with (iova_pfn,end_pfn) above. Which one do you prefer?
Yours looks better.
By the way, if you are willing to prepare a v2, please make sure to add Joerg (IOMMU subsystem maintainer) to the list.
Best regards,
baolu
Lu Baolu
maintainerHi Longpeng,
Quoted 6 lines · Longpeng(Mike)
drivers/iommu/intel/iommu.c +0
-0@@ -2342,9 +2342,20 @@ static inline int hardware_largepage_caps(struct dmar_domain *domain,* removed to make room for superpage(s).
drivers/iommu/intel/iommu.c static inline int hardware_largepage_caps(struct dmar_domain *domain, · line 2350
Quoted 10 lines
drivers/iommu/intel/iommu.c +5
-2* We're adding new large pages, so make sure * we don't remove their parent tables. +* +* We also need to flush the iotlb before creating +* superpage to ensure it does not perserves any +* obsolete info. */ -dma_pte_free_pagetable (domain, iov_pfn, end_pfn, - largepage_lvl +1 ); +if (dma_pte_present (pte)) {
The dma_pte_free_pagetable() clears a batch of PTEs. So checking current PTE is insufficient. How about removing this check and always performing cache invalidation?
drivers/iommu/intel/iommu.c static inline int hardware_largepage_caps(struct dmar_domain *domain,
Quoted 8 lines
drivers/iommu/intel/iommu.c +8
-0+int i; + +dma_pte_free_pagetable (domain, iov_pfn, end_pfn, + largepage_lvl +1 ); +for_each_domain_iommu (i, domain) +iommu_flush_iotlb_psi (g_iommus[i], domain, + iov_pfn, nr_pages,0 ,0 ); +
Best regards,
baolu
Hi Baolu,
Quoted 21 lines
-----Original Message-----
From: Lu Baolu [mailto:[email protected]]
Sent: Friday, April 2, 2021 12:44 PM
To: Longpeng (Mike, Cloud Infrastructure Service Product Dept.) <[email protected]>; [email protected]; [email protected]
Cc: [email protected]; David Woodhouse <[email protected]>; Nadav Amit <[email protected]>; Alex Williamson <[email protected]>; Kevin Tian <[email protected]>; Gonglei (Arei) <[email protected]>; [email protected]
Subject: Re: [PATCH] iommu/vt-d: Force to flush iotlb before creating superpageHi Longpeng,
drivers/iommu/intel/iommu.c static inline int hardware_largepage_caps(struct
Quoted 5 lines · Longpeng(Mike)
drivers/iommu/intel/iommu.c +0
-0@@ -2342,9 +2342,20 @@ static inline int hardware_largepage_caps(struct
dmar_domain*domain,
Quoted 16 lines
drivers/iommu/intel/iommu.c static inline int hardware_largepage_caps(struct dmar_domain *domain, · line 2350
Quoted 11 lines
drivers/iommu/intel/iommu.c +5
-2* removed to make room for superpage(s). * We're adding new large pages, so make sure * we don't remove their parent tables. +* +* We also need to flush the iotlb before creating +* superpage to ensure it does not perserves any +* obsolete info. */ -dma_pte_free_pagetable (domain, iov_pfn, end_pfn, - largepage_lvl +1 ); +if (dma_pte_present (pte)) {The
dma_pte_free_pagetable()clears a batch of PTEs. So checking current PTE is insufficient. How about removing this check and always performing cache invalidation?
Um...the PTE here may be present( e.g. 4K mapping --> superpage mapping ) or NOT-present ( e.g. create a totally new superpage mapping ), but we only need to call free_pagetable and flush_iotlb in the former case, right ?
Quoted 11 lines
drivers/iommu/intel/iommu.c static inline int hardware_largepage_caps(struct dmar_domain *domain,
Quoted 8 lines
drivers/iommu/intel/iommu.c +8
-0+int i; + +dma_pte_free_pagetable (domain, iov_pfn, end_pfn, + largepage_lvl +1 ); +for_each_domain_iommu (i, domain) +iommu_flush_iotlb_psi (g_iommus[i], domain, + iov_pfn, nr_pages,0 ,0 ); +Best regards,
baolu
Lu Baolu
maintainerHi Longpeng,
Quoted 40 lines · Cloud Infrastructure Service Product Dept.)
Hi Baolu,
Quoted 21 lines
-----Original Message-----
From: Lu Baolu [mailto:[email protected]]
Sent: Friday, April 2, 2021 12:44 PM
To: Longpeng (Mike, Cloud Infrastructure Service Product Dept.) <[email protected]>; [email protected]; [email protected]
Cc: [email protected]; David Woodhouse <[email protected]>; Nadav Amit <[email protected]>; Alex Williamson <[email protected]>; Kevin Tian <[email protected]>; Gonglei (Arei) <[email protected]>; [email protected]
Subject: Re: [PATCH] iommu/vt-d: Force to flush iotlb before creating superpageHi Longpeng,
drivers/iommu/intel/iommu.c static inline int hardware_largepage_caps(struct
Quoted 5 lines · Longpeng(Mike)
drivers/iommu/intel/iommu.c +0
-0@@ -2342,9 +2342,20 @@ static inline int hardware_largepage_caps(struct
dmar_domain*domain,Quoted 16 lines
drivers/iommu/intel/iommu.c static inline int hardware_largepage_caps(struct · line 2350
Quoted 11 lines
drivers/iommu/intel/iommu.c +5
-2* removed to make room for superpage(s). * We're adding new large pages, so make sure * we don't remove their parent tables. +* +* We also need to flush the iotlb before creating +* superpage to ensure it does not perserves any +* obsolete info. */ -dma_pte_free_pagetable (domain, iov_pfn, end_pfn, - largepage_lvl +1 ); +if (dma_pte_present (pte)) {The
dma_pte_free_pagetable()clears a batch of PTEs. So checking current PTE is insufficient. How about removing this check and always performing cache invalidation?
Quoted 1 line
Um...the PTE here may be present( e.g. 4K mapping --> superpage mapping ) orNOT-present ( e.g. create a totally new superpage mapping ), but we only need to call
free_pagetableandflush_iotlbin the former case, right ?
But this code covers multiple PTEs and perhaps crosses the page boundary.
How about moving this code into a separated function and check PTE presence there. A sample code could look like below: [compiled but not tested!]
drivers/iommu/intel/iommu.c +38 -14
@@ -2300,6 +2300,41 @@ static inline int hardware_largepage_caps(struct dmar_domain *domain,return level; } +/* +* Ensure that old small page tables are removed to make room for superpage(s). +* We're going to add new large pages, so make sure we don't remove their parent +* tables. The IOTLB/devTLBs should be flushed if any PDE/PTEs are cleared. +*/ +static void switch_to_super_page (struct dmar_domain *domain, +unsigned long start_pfn, +unsigned long end_pfn,int level) +{ +unsigned long lvl_pages =lvl_to_nr_pages (level); +struct dma_pte *pte =NULL ; +int i; + +while (start_pfn <= end_pfn) { +if (!pte) + pte =pfn_to_dma_pte (domain, start_pfn, &level); + +if (dma_pte_present (pte)) { +dma_pte_free_pagetable (domain, start_pfn, + start_pfn + lvl_pages -1 , + level +1 ); + +for_each_domain_iommu (i, domain) +iommu_flush_iotlb_psi (g_iommus[i], domain, + start_pfn, lvl_pages, +0 ,0 ); + } + + pte++; + start_pfn += lvl_pages; +if (first_pte_in_page (pte)) + pte =NULL ; + } +} +static int __domain_mapping (struct dmar_domain *domain,unsigned long iov_pfn,unsigned long phys_pfn,unsigned long nr_pages,int prot)
@@ -2341,22 +2376,11 @@ __domain_mapping(struct dmar_domain *domain, unsigned long iov_pfn,return -ENOMEM ;/* It is large page*/ if (largepage_lvl >1 ) { -unsigned long nr_superpages, end_pfn; +unsigned long end_pfn; pteval |=DMA_PTE_LARGE_PAGE ; - lvl_pages =lvl_to_nr_pages (largepage_lvl); - - nr_superpages = nr_pages / lvl_pages; - end_pfn = iov_pfn + nr_superpages * lvl_pages -1 ; - -/* -* Ensure that old small page tables are -* removed to make room for superpage(s). -* We're adding new large pages, so make sure -* we don't remove their parent tables. -*/ -dma_pte_free_pagetable (domain, iov_pfn, end_pfn, - largepage_lvl +1 ); + end_pfn = ((iov_pfn + nr_pages) &level_mask (largepage_lvl)) -1 ; +switch_to_super_page (domain, iov_pfn, end_pfn, largepage_lvl); }else { pteval &= ~(uint64_t )DMA_PTE_LARGE_PAGE ; }
I will send you the diff patch off list. Any thoughts?
Best regards,
baolu
Quoted 12 lines
Quoted 12 lines
drivers/iommu/intel/iommu.c static inline int hardware_largepage_caps(struct · line 2358
Quoted 9 lines
drivers/iommu/intel/iommu.c +8
-0+int i; + +dma_pte_free_pagetable (domain, iov_pfn, end_pfn, + largepage_lvl +1 ); +for_each_domain_iommu (i, domain) +iommu_flush_iotlb_psi (g_iommus[i], domain, + iov_pfn, nr_pages,0 ,0 ); +Best regards,
baolu
Hi Baolu,
Quoted 83 lines
-----Original Message-----
From: Lu Baolu [mailto:[email protected]]
Sent: Thursday, April 8, 2021 12:32 PM
To: Longpeng (Mike, Cloud Infrastructure Service Product Dept.) <[email protected]>; [email protected]; [email protected]
Cc: [email protected]; David Woodhouse <[email protected]>; Nadav Amit <[email protected]>; Alex Williamson <[email protected]>; Kevin Tian <[email protected]>; Gonglei (Arei) <[email protected]>; [email protected]
Subject: Re: [PATCH] iommu/vt-d: Force to flush iotlb before creating superpageHi Longpeng,
Quoted 42 lines · Cloud Infrastructure Service Product Dept.)
Hi Baolu,
Quoted 23 lines
-----Original Message-----
From: Lu Baolu [mailto:[email protected]]
Sent: Friday, April 2, 2021 12:44 PM
To: Longpeng (Mike, Cloud Infrastructure Service Product Dept.) <[email protected]>; [email protected]; [email protected]
Cc: [email protected]; David Woodhouse <[email protected]>; Nadav Amit <[email protected]>; Alex Williamson <[email protected]>; Kevin Tian <[email protected]>; Gonglei (Arei) <[email protected]>; [email protected]
Subject: Re: [PATCH] iommu/vt-d: Force to flush iotlb before creating superpageHi Longpeng,
Quoted 6 lines · Longpeng(Mike)
(quoted diff) +0
-0
b/drivers/iommu/intel/iommu.cindex ee09323..cbcb434 100644drivers/iommu/intel/iommu.c +0
-0@@ -2342,9 +2342,20 @@ static inline inthardware_largepage_caps(struct
dmar_domain*domain,Quoted 16 lines
drivers/iommu/intel/iommu.c
Quoted 11 lines
* removed to make room for superpage(s).
* We're adding new large pages, so make sure
* we don't remove their parent tables.drivers/iommu/intel/iommu.c +5
-2+* +* We also need to flush the iotlb before creating +* superpage to ensure it does not perserves any +* obsolete info. */ -dma_pte_free_pagetable (domain, iov_pfn, end_pfn, - largepage_lvl +1 ); +if (dma_pte_present (pte)) {The
dma_pte_free_pagetable()clears a batch of PTEs. So checking current PTE is insufficient. How about removing this check and always performing cache invalidation?Quoted 1 line
Um...the PTE here may be present( e.g. 4K mapping --> superpage mapping )
orNOT-present ( e.g. create a totally new superpage mapping ), but we only need to call
free_pagetableandflush_iotlbin the former case, right ?But this code covers multiple PTEs and perhaps crosses the page boundary.
How about moving this code into a separated function and check PTE presence there. A sample code could look like below: [compiled but not tested!]
drivers/iommu/intel/iommu.c index renamed from drivers/iommu/intel/iommu.c +0
-0
d334f5b4e382..0e04d450c38a100644drivers/iommu/intel/iommu.c +2
-0@@ -2300,6 +2300,41 @@ static inline int hardware_largepage_caps(structdmar_domain *domain, return level; } +/* +* Ensure that old small page tables are removed to make room for superpage(s).
+ * We're going to add new large pages, so make sure we don't remove their parent
+ * tables. The IOTLB/devTLBs should be flushed if any PDE/PTEs are cleared.
+ */
drivers/iommu/intel/iommu.c static inline int hardware_largepage_caps(struct dmar_domain *domain,
Quoted 3 lines
drivers/iommu/intel/iommu.c +3
-0+static void switch_to_super_page (struct dmar_domain *domain, +unsigned long start_pfn, +unsigned long end_pfn,int level) {
Maybe "swith_to" will lead people to think "remove old and then setup new", so how about something like "remove_room_for_super_page" or "prepare_for_super_page" ?
drivers/iommu/intel/iommu.c static inline int hardware_largepage_caps(struct dmar_domain *domain, · line 2316
Quoted 5 lines
drivers/iommu/intel/iommu.c +5
-0+unsigned long lvl_pages =lvl_to_nr_pages (level); +struct dma_pte *pte =NULL ; +int i; + +while (start_pfn <= end_pfn) {
start_pfn < end_pfn ?
Quoted 55 lines
drivers/iommu/intel/iommu.c +10
-0+if (!pte) + pte =pfn_to_dma_pte (domain, start_pfn, &level); + +if (dma_pte_present (pte)) { +dma_pte_free_pagetable (domain, start_pfn, + start_pfn + lvl_pages -1 , + level +1 ); + +for_each_domain_iommu (i, domain) + iommu_flush_iotlb_psi(g_iommus[i],domain,
+start_pfn,lvl_pages,drivers/iommu/intel/iommu.c +11
-10+ 0, 0); + } + + pte++; + start_pfn += lvl_pages; +if (first_pte_in_page (pte)) + pte =NULL ; + } +} +static int __domain_mapping (struct dmar_domain *domain,unsigned long iov_pfn,unsigned long phys_pfn,unsigned long nr_pages,int prot)@@ -2341,22 +2376,11 @@ __domain_mapping(struct dmar_domain *domain,unsigned long iov_pfn, return -ENOMEM;/* It is large page*/ if (largepage_lvl >1 ) { -unsigned long nr_superpages, end_pfn; +unsigned long end_pfn; pteval |=DMA_PTE_LARGE_PAGE ; - lvl_pages =lvl_to_nr_pages (largepage_lvl); - - nr_superpages = nr_pages / lvl_pages; - end_pfn = iov_pfn + nr_superpages * lvl_pages -1 ; - -/* -* Ensure that old small page tables are -* removed to make room for superpage(s). -* We're adding new large pages, so make sure
drivers/iommu/intel/iommu.c +0
-3-* we don't remove their parent tables. -*/ - dma_pte_free_pagetable(domain, iov_pfn,
end_pfn,
-largepage_lvl+
1);
+end_pfn= ((iov_pfn+nr_pages) &level_mask(largepage_lvl)) - 1;
+ switch_to_super_page(domain,iov_pfn,
Quoted 8 lines
end_pfn, largepage_lvl); } else { pteval &= ~(uint64_t)DMA_PTE_LARGE_PAGE; }I will send you the diff patch off list. Any thoughts?
The solution looks good to me.
It's free for you to send this patch if you want, I'll send V2 if you have no plan to send it :)
Quoted 15 lines
Best regards,
baoluQuoted 12 lines
Quoted 12 lines
drivers/iommu/intel/iommu.c static inline int hardware_largepage_caps(struct dmar_domain *domain, · line 2330
Quoted 9 lines
drivers/iommu/intel/iommu.c +8
-0+int i; + +dma_pte_free_pagetable (domain, iov_pfn, end_pfn, + largepage_lvl +1 ); +for_each_domain_iommu (i, domain) +iommu_flush_iotlb_psi (g_iommus[i], domain, + iov_pfn, nr_pages,0 ,0 ); +Best regards,
baolu
Lu Baolu
maintainerHi Longpeng,
Quoted 78 lines · Cloud Infrastructure Service Product Dept.)
Hi Baolu,
Quoted 76 lines
-----Original Message-----
From: Lu Baolu [mailto:[email protected]]
Sent: Thursday, April 8, 2021 12:32 PM
To: Longpeng (Mike, Cloud Infrastructure Service Product Dept.) <[email protected]>; [email protected]; [email protected]
Cc: [email protected]; David Woodhouse <[email protected]>; Nadav Amit <[email protected]>; Alex Williamson <[email protected]>; Kevin Tian <[email protected]>; Gonglei (Arei) <[email protected]>; [email protected]
Subject: Re: [PATCH] iommu/vt-d: Force to flush iotlb before creating superpageHi Longpeng,
Quoted 42 lines · Cloud Infrastructure Service Product Dept.)
Hi Baolu,
Quoted 23 lines
-----Original Message-----
From: Lu Baolu [mailto:[email protected]]
Sent: Friday, April 2, 2021 12:44 PM
To: Longpeng (Mike, Cloud Infrastructure Service Product Dept.) <[email protected]>; [email protected]; [email protected]
Cc: [email protected]; David Woodhouse <[email protected]>; Nadav Amit <[email protected]>; Alex Williamson <[email protected]>; Kevin Tian <[email protected]>; Gonglei (Arei) <[email protected]>; [email protected]
Subject: Re: [PATCH] iommu/vt-d: Force to flush iotlb before creating superpageHi Longpeng,
Quoted 6 lines · Longpeng(Mike)
(quoted diff) +0
-0
b/drivers/iommu/intel/iommu.cindex ee09323..cbcb434 100644drivers/iommu/intel/iommu.c +0
-0@@ -2342,9 +2342,20 @@ static inline inthardware_largepage_caps(struct
dmar_domain*domain,Quoted 16 lines
drivers/iommu/intel/iommu.c
Quoted 11 lines
* removed to make room for superpage(s).
* We're adding new large pages, so make sure
* we don't remove their parent tables.drivers/iommu/intel/iommu.c +5
-2+* +* We also need to flush the iotlb before creating +* superpage to ensure it does not perserves any +* obsolete info. */ -dma_pte_free_pagetable (domain, iov_pfn, end_pfn, - largepage_lvl +1 ); +if (dma_pte_present (pte)) {The
dma_pte_free_pagetable()clears a batch of PTEs. So checking current PTE is insufficient. How about removing this check and always performing cache invalidation?Quoted 1 line
Um...the PTE here may be present( e.g. 4K mapping --> superpage mapping )
orNOT-present ( e.g. create a totally new superpage mapping ), but we only need to call
free_pagetableandflush_iotlbin the former case, right ?But this code covers multiple PTEs and perhaps crosses the page boundary.
How about moving this code into a separated function and check PTE presence there. A sample code could look like below: [compiled but not tested!]
drivers/iommu/intel/iommu.c index renamed from drivers/iommu/intel/iommu.c +0
-0
d334f5b4e382..0e04d450c38a100644drivers/iommu/intel/iommu.c +0
-0@@ -2300,6 +2300,41 @@ static inline int hardware_largepage_caps(structdmar_domain *domain, return level; }
Quoted 12 lines
drivers/iommu/intel/iommu.c static inline int hardware_largepage_caps(struct dmar_domain *domain,
Quoted 10 lines
drivers/iommu/intel/iommu.c +2
-0+/* +* Ensure that old small page tables are removed to make room for superpage(s).
+ * We're going to add new large pages, so make sure we don't remove their parent
+ * tables. The IOTLB/devTLBs should be flushed if any PDE/PTEs are cleared.
+ */drivers/iommu/intel/iommu.c +3
-0+static void switch_to_super_page (struct dmar_domain *domain, +unsigned long start_pfn, +unsigned long end_pfn,int level) {Maybe "
swith_to" will lead people to think "remove old and then setup new", so how about something like "remove_room_for_super_page" or "prepare_for_super_page" ?
I named it like this because we also want to have a opposite operation split_from_super_page() which switch a PDE or PDPE from super page setting up to small pages, which is needed to optimize dirty bit tracking during VM live migration.
Quoted 8 lines
drivers/iommu/intel/iommu.c static inline int hardware_largepage_caps(struct dmar_domain *domain, · line 2316
Quoted 6 lines
drivers/iommu/intel/iommu.c +5
-0+unsigned long lvl_pages =lvl_to_nr_pages (level); +struct dma_pte *pte =NULL ; +int i; + +while (start_pfn <= end_pfn) {
start_pfn<end_pfn?
end_pfn is inclusive.
Quoted 62 lines
Quoted 62 lines
drivers/iommu/intel/iommu.c +10
-0+if (!pte) + pte =pfn_to_dma_pte (domain, start_pfn, &level); + +if (dma_pte_present (pte)) { +dma_pte_free_pagetable (domain, start_pfn, + start_pfn + lvl_pages -1 , + level +1 ); + +for_each_domain_iommu (i, domain) + iommu_flush_iotlb_psi(g_iommus[i],domain,
+start_pfn,lvl_pages,drivers/iommu/intel/iommu.c +11
-10+ 0, 0); + } + + pte++; + start_pfn += lvl_pages; +if (first_pte_in_page (pte)) + pte =NULL ; + } +} +static int __domain_mapping (struct dmar_domain *domain,unsigned long iov_pfn,unsigned long phys_pfn,unsigned long nr_pages,int prot)@@ -2341,22 +2376,11 @@ __domain_mapping(struct dmar_domain *domain,unsigned long iov_pfn, return -ENOMEM;/* It is large page*/ if (largepage_lvl >1 ) { -unsigned long nr_superpages, end_pfn; +unsigned long end_pfn; pteval |=DMA_PTE_LARGE_PAGE ; - lvl_pages =lvl_to_nr_pages (largepage_lvl); - - nr_superpages = nr_pages / lvl_pages; - end_pfn = iov_pfn + nr_superpages * lvl_pages -1 ; - -/* -* Ensure that old small page tables are -* removed to make room for superpage(s). -* We're adding new large pages, so make sure
drivers/iommu/intel/iommu.c +0
-3-* we don't remove their parent tables. -*/ - dma_pte_free_pagetable(domain, iov_pfn,
end_pfn,
-largepage_lvl+
1);
+end_pfn= ((iov_pfn+nr_pages) &level_mask(largepage_lvl)) - 1;
+ switch_to_super_page(domain,iov_pfn,end_pfn, largepage_lvl); } else { pteval &= ~(uint64_t)DMA_PTE_LARGE_PAGE; }
Quoted 6 lines
Quoted 2 lines
I will send you the diff patch off list. Any thoughts?
The solution looks good to me.
It's free for you to send this patch if you want, I'll send V2 if you have no plan to send it :)
Please go ahead with a new version. Thank you for catching this and managing to fix it.
Best regards,
baolu
Quoted 5 lines
Quoted 5 lines
Best regards,
baoluQuoted 1 line
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
drivers/iommu/intel/iommu.c static inline int hardware_largepage_caps(struct dmar_domain *domain, · line 2330
Quoted 8 lines
drivers/iommu/intel/iommu.c +8
-0+int i; + +dma_pte_free_pagetable (domain, iov_pfn, end_pfn, + largepage_lvl +1 ); +for_each_domain_iommu (i, domain) +iommu_flush_iotlb_psi (g_iommus[i], domain, + iov_pfn, nr_pages,0 ,0 ); +Best regards,
baolu
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu