Skip to contentSkip to conversation

iommu/vt-d: Fix unmap_pages support

PATCH In review Alex Williamson 5 participants 6 messages Nov 11, 2021 – Nov 19, 2021

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

PATCH 1/1 iommu/vt-d: Fix unmap_pages support

When supporting only the .map and .unmap callbacks of iommu_ops, the IOMMU driver can make assumptions about the size and alignment used for mappings based on the driver provided pgsize_bitmap. VT-d previously used essentially PAGE_MASK for this bitmap as any power of two mapping was acceptably filled by native page sizes.

However, with the .map_pages and .unmap_pages interface we're now getting page-size and count arguments. If we simply combine these as (page-size * count) and make use of the previous map/unmap functions internally, any size and alignment assumptions are very different.

As an example, a given vfio device assignment VM will often create a 4MB mapping at IOVA pfn [0x3fe00 - 0x401ff]. On a system that does not support IOMMU super pages, the unmap_pages interface will ask to unmap 1024 4KB pages at the base IOVA. dma_pte_clear_level() will recurse down to level 2 of the page table where the first half of the pfn range exactly matches the entire pte level. We clear the pte, increment the pfn by the level size, but (oops) the next pte is on a new page, so we exit the loop an pop back up a level. When we then update the pfn based on that higher level, we seem to assume that the previous pfn value was at the start of the level. In this case the level size is 256K pfns, which we add to the base pfn and get a results of 0x7fe00, which is clearly greater than 0x401ff, so we're done. Meanwhile we never cleared the ptes for the remainder of the range. When the VM remaps this range, we're overwriting valid ptes and the VT-d driver complains loudly, as reported by the user report linked below.

The fix for this seems relatively simple, if each iteration of the loop in dma_pte_clear_level() is assumed to clear to the end of the level pte page, then our next pfn should be calculated from level_pfn rather than our working pfn.

Fixes 3f34f1259776 ("iommu/vt-d: Implement map/unmap_pages() iommu_ops callback")
---
 drivers/iommu/intel/iommu.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iommu/intel/iommu.c +1 -1
index d75f59ae28e6..f6395f5425f0 100644
@@ -1249,7 +1249,7 @@ static struct page *dma_pte_clear_level(struct dmar_domain *domain, int level,
 						       freelist);
 		}
 next:
-		pfn += level_size(level);
+		pfn = level_pfn + level_size(level);
 	} while (!first_pte_in_page(++pte) && pfn <= last_pfn);
 
 	if (first_pte)

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

#1 lore raw

Thanks Alex, Baolu.
The patch fixes things at my end. No kernel-flooding is seen now (tested starting/stopping vm > 10 times).

Thanks and Regards,
Ajay

Quoted 50 lines · Alex Williamson

When supporting only the .map and .unmap callbacks of iommu_ops, the IOMMU driver can make assumptions about the size and alignment used for mappings based on the driver provided pgsize_bitmap. VT-d previously used essentially PAGE_MASK for this bitmap as any power of two mapping was acceptably filled by native page sizes.

However, with the .map_pages and .unmap_pages interface we're now getting page-size and count arguments. If we simply combine these as (page-size * count) and make use of the previous map/unmap functions internally, any size and alignment assumptions are very different.

As an example, a given vfio device assignment VM will often create a 4MB mapping at IOVA pfn [0x3fe00 - 0x401ff]. On a system that does not support IOMMU super pages, the unmap_pages interface will ask to unmap 1024 4KB pages at the base IOVA. dma_pte_clear_level() will recurse down to level 2 of the page table where the first half of the pfn range exactly matches the entire pte level. We clear the pte, increment the pfn by the level size, but (oops) the next pte is on a new page, so we exit the loop an pop back up a level. When we then update the pfn based on that higher level, we seem to assume that the previous pfn value was at the start of the level. In this case the level size is 256K pfns, which we add to the base pfn and get a results of 0x7fe00, which is clearly greater than 0x401ff, so we're done. Meanwhile we never cleared the ptes for the remainder of the range. When the VM remaps this range, we're overwriting valid ptes and the VT-d driver complains loudly, as reported by the user report linked below.

The fix for this seems relatively simple, if each iteration of the loop in dma_pte_clear_level() is assumed to clear to the end of the level pte page, then our next pfn should be calculated from level_pfn rather than our working pfn.

Fixes 3f34f1259776 ("iommu/vt-d: Implement map/unmap_pages() iommu_ops callback")
---
 drivers/iommu/intel/iommu.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iommu/intel/iommu.c +0 -0
index d75f59ae28e6..f6395f5425f0 100644

drivers/iommu/intel/iommu.c static struct page *dma_pte_clear_level(struct dmar_domain *domain, int level, · line 1255

Quoted 8 lines
drivers/iommu/intel/iommu.c +1 -1
@@ -1249,7 +1249,7 @@ static struct page *dma_pte_clear_level(struct dmar_domain *domain, int level,
                                                       freelist);
                }
 next:
-               pfn += level_size(level);
+               pfn = level_pfn + level_size(level);
        } while (!first_pte_in_page(++pte) && pfn <= last_pfn);
 
        if (first_pte)

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

Tested-by on v1 1/1
#2 lore raw
In reply to Alex Williamson

I was able to hit the issue that this patch is fixing using the memory allocator in QATlib (https://github.com/intel/qatlib/) which uses vfio to map and unmap memory.

This patch fixes the problem.

Quoted 34 lines · Alex Williamson

When supporting only the .map and .unmap callbacks of iommu_ops, the IOMMU driver can make assumptions about the size and alignment used for mappings based on the driver provided pgsize_bitmap. VT-d previously used essentially PAGE_MASK for this bitmap as any power of two mapping was acceptably filled by native page sizes.

However, with the .map_pages and .unmap_pages interface we're now getting page-size and count arguments. If we simply combine these as (page-size * count) and make use of the previous map/unmap functions internally, any size and alignment assumptions are very different.

As an example, a given vfio device assignment VM will often create a 4MB mapping at IOVA pfn [0x3fe00 - 0x401ff]. On a system that does not support IOMMU super pages, the unmap_pages interface will ask to unmap 1024 4KB pages at the base IOVA. dma_pte_clear_level() will recurse down to level 2 of the page table where the first half of the pfn range exactly matches the entire pte level. We clear the pte, increment the pfn by the level size, but (oops) the next pte is on a new page, so we exit the loop an pop back up a level. When we then update the pfn based on that higher level, we seem to assume that the previous pfn value was at the start of the level. In this case the level size is 256K pfns, which we add to the base pfn and get a results of 0x7fe00, which is clearly greater than 0x401ff, so we're done. Meanwhile we never cleared the ptes for the remainder of the range. When the VM remaps this range, we're overwriting valid ptes and the VT-d driver complains loudly, as reported by the user report linked below.

The fix for this seems relatively simple, if each iteration of the loop in dma_pte_clear_level() is assumed to clear to the end of the level pte page, then our next pfn should be calculated from level_pfn rather than our working pfn.

Quoted 4 lines
Fixes 3f34f1259776 ("iommu/vt-d: Implement map/unmap_pages() iommu_ops callback")

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

maintainer
#3 lore raw
In reply to Alex Williamson

Hi Alex,

Quoted 34 lines · Alex Williamson

When supporting only the .map and .unmap callbacks of iommu_ops, the IOMMU driver can make assumptions about the size and alignment used for mappings based on the driver provided pgsize_bitmap. VT-d previously used essentially PAGE_MASK for this bitmap as any power of two mapping was acceptably filled by native page sizes.

However, with the .map_pages and .unmap_pages interface we're now getting page-size and count arguments. If we simply combine these as (page-size * count) and make use of the previous map/unmap functions internally, any size and alignment assumptions are very different.

As an example, a given vfio device assignment VM will often create a 4MB mapping at IOVA pfn [0x3fe00 - 0x401ff]. On a system that does not support IOMMU super pages, the unmap_pages interface will ask to unmap 1024 4KB pages at the base IOVA. dma_pte_clear_level() will recurse down to level 2 of the page table where the first half of the pfn range exactly matches the entire pte level. We clear the pte, increment the pfn by the level size, but (oops) the next pte is on a new page, so we exit the loop an pop back up a level. When we then update the pfn based on that higher level, we seem to assume that the previous pfn value was at the start of the level. In this case the level size is 256K pfns, which we add to the base pfn and get a results of 0x7fe00, which is clearly greater than 0x401ff, so we're done. Meanwhile we never cleared the ptes for the remainder of the range. When the VM remaps this range, we're overwriting valid ptes and the VT-d driver complains loudly, as reported by the user report linked below.

The fix for this seems relatively simple, if each iteration of the loop in dma_pte_clear_level() is assumed to clear to the end of the level pte page, then our next pfn should be calculated from level_pfn rather than our working pfn.

Quoted 4 lines
Fixes 3f34f1259776 ("iommu/vt-d: Implement map/unmap_pages() iommu_ops callback")

Thank you for fixing this! I will queue it for v5.16.

Best regards,
baolu

Quoted 11 lines
---
  drivers/iommu/intel/iommu.c |    2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iommu/intel/iommu.c +0 -0
index d75f59ae28e6..f6395f5425f0 100644

drivers/iommu/intel/iommu.c static struct page *dma_pte_clear_level(struct dmar_domain *domain, int level, · line 1255

Quoted 8 lines
drivers/iommu/intel/iommu.c +1 -1
@@ -1249,7 +1249,7 @@ static struct page *dma_pte_clear_level(struct dmar_domain *domain, int level,
  						       freelist);
  		}
  next:
-		pfn += level_size(level);
+		pfn = level_pfn + level_size(level);
  	} while (!first_pte_in_page(++pte) && pfn <= last_pfn);
 
  	if (first_pte)

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

#4 lore raw
Quoted 50 lines · Lu Baolu

Hi Alex,

Quoted 40 lines · Alex Williamson

When supporting only the .map and .unmap callbacks of iommu_ops, the IOMMU driver can make assumptions about the size and alignment used for mappings based on the driver provided pgsize_bitmap.  VT-d previously used essentially PAGE_MASK for this bitmap as any power of two mapping was acceptably filled by native page sizes.

However, with the .map_pages and .unmap_pages interface we're now getting page-size and count arguments.  If we simply combine these as (page-size * count) and make use of the previous map/unmap functions internally, any size and alignment assumptions are very different.

As an example, a given vfio device assignment VM will often create a 4MB mapping at IOVA pfn [0x3fe00 - 0x401ff].  On a system that does not support IOMMU super pages, the unmap_pages interface will ask to unmap 1024 4KB pages at the base IOVA. dma_pte_clear_level()
will recurse down to level 2 of the page table where the first half of the pfn range exactly matches the entire pte level.  We clear the
pte, increment the pfn by the level size, but (oops) the next pte is
on a new page, so we exit the loop an pop back up a level.  When we then update the pfn based on that higher level, we seem to assume that the previous pfn value was at the start of the level.  In this case the level size is 256K pfns, which we add to the base pfn and get a results of 0x7fe00, which is clearly greater than 0x401ff, so we're done.  Meanwhile we never cleared the ptes for the remainder
of the range.  When the VM remaps this range, we're overwriting valid
ptes and the VT-d driver complains loudly, as reported by the user report linked below.

The fix for this seems relatively simple, if each iteration of the loop in dma_pte_clear_level() is assumed to clear to the end of the level pte page, then our next pfn should be calculated from level_pfn
rather than our working pfn.

Quoted 6 lines
Fixes 3f34f1259776 ("iommu/vt-d: Implement map/unmap_pages()

iommu_ops callback")

Link:
https://lore.kernel.org/all/[email protected]/

Quoted 5 lines

Thank you for fixing this! I will queue it for v5.16.

Best regards,
baolu

Hi Baolu,

Do you have an estimate of when this will be submitted?

Regards,
Jerry

Quoted 17 lines
Quoted 17 lines

---
  drivers/iommu/intel/iommu.c |    2 +-

  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iommu/intel/iommu.c +0 -0
index d75f59ae28e6..f6395f5425f0 100644
@@ -1249,7 +1249,7 @@ static struct page
 *dma_pte_clear_level(struct dmar_domain *domain, int level,
                                                        freelist);
                 }

  next:
-               pfn += level_size(level);
+               pfn = level_pfn + level_size(level); } while (!first_pte_in_page(++pte) && pfn <= last_pfn);

Quoted 8 lines
Quoted 4 lines


        if (first_pte)

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

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

maintainer
#5 lore raw

Hi Jerry,

Quoted 53 lines · Jerry Snitselaar
Quoted 52 lines · Lu Baolu

Hi Alex,

Quoted 40 lines · Alex Williamson

When supporting only the .map and .unmap callbacks of iommu_ops, the IOMMU driver can make assumptions about the size and alignment used for mappings based on the driver provided pgsize_bitmap.  VT-d previously used essentially PAGE_MASK for this bitmap as any power of two mapping was acceptably filled by native page sizes.

However, with the .map_pages and .unmap_pages interface we're now getting page-size and count arguments.  If we simply combine these as (page-size * count) and make use of the previous map/unmap functions internally, any size and alignment assumptions are very different.

As an example, a given vfio device assignment VM will often create a 4MB mapping at IOVA pfn [0x3fe00 - 0x401ff].  On a system that does not support IOMMU super pages, the unmap_pages interface will ask to unmap 1024 4KB pages at the base IOVA. dma_pte_clear_level()
will recurse down to level 2 of the page table where the first half of the pfn range exactly matches the entire pte level.  We clear the
pte, increment the pfn by the level size, but (oops) the next pte is
on a new page, so we exit the loop an pop back up a level.  When we then update the pfn based on that higher level, we seem to assume that the previous pfn value was at the start of the level.  In this case the level size is 256K pfns, which we add to the base pfn and get a results of 0x7fe00, which is clearly greater than 0x401ff, so we're done.  Meanwhile we never cleared the ptes for the remainder
of the range.  When the VM remaps this range, we're overwriting valid
ptes and the VT-d driver complains loudly, as reported by the user report linked below.

The fix for this seems relatively simple, if each iteration of the loop in dma_pte_clear_level() is assumed to clear to the end of the level pte page, then our next pfn should be calculated from level_pfn
rather than our working pfn.

Quoted 6 lines
Fixes 3f34f1259776 ("iommu/vt-d: Implement map/unmap_pages()

iommu_ops callback")

Link:
https://lore.kernel.org/all/[email protected]/

Thank you for fixing this! I will queue it for v5.16.

Quoted 7 lines
Quoted 3 lines

Best regards,
baolu

Hi Baolu,

Do you have an estimate of when this will be submitted?

I will submit all fix patches in my queue to Joerg early the next week.

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