iommu/vt-d: Fixes for v5.16-rc3
Subsystems (2)
Lu Baolu
maintainercover iommu/vt-d: Fixes for v5.16-rc3
Hi Joerg,
Two small fixes are queued for v5.16. They aim to fix:
- Unbalanced rcu_read_lock/unlock() in cap_audit_static();
- Missed unmapped PTE entries in dma_pte_clear_level().
This series is also available on github:
https://github.com/LuBaolu/intel-iommu/commits/vtd-fix-for-v5.16-rc3
Please consider them for the iommu/fix branch.
Best regards,
Lu Baolu
Alex Williamson (1):
iommu/vt-d: Fix unmap_pages support
Christophe JAILLET (1):
iommu/vt-d: Fix an unbalanced rcu_read_lock/rcu_read_unlock()
drivers/iommu/intel/cap_audit.c | 5 +++-- drivers/iommu/intel/iommu.c | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-)
Signature · 6 lines
2.25.1 _______________________________________________ iommu mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/iommu
Lu Baolu
maintainerPATCH 1/2 iommu/vt-d: Fix an unbalanced rcu_read_lock/rcu_read_unlock()
From: Christophe JAILLET <[email protected]>
If we return -EOPNOTSUPP, the rcu lock remains lock. This is spurious. Go through the end of the function instead. This way, the missing 'rcu_read_unlock()' is called.
--- drivers/iommu/intel/cap_audit.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/iommu/intel/cap_audit.c +3 -2
@@ -144,6 +144,7 @@ static int cap_audit_static(struct intel_iommu *iommu, enum cap_audit_type type) {struct dmar_drhd_unit *d;struct intel_iommu *i; +int rc =0 ;rcu_read_lock ();if (list_empty (&dmar_drhd_units))
@@ -169,11 +170,11 @@ static int cap_audit_static(struct intel_iommu *iommu, enum cap_audit_type type)*/ if (intel_cap_smts_sanity () && !intel_cap_flts_sanity () && !intel_cap_slts_sanity ()) -return -EOPNOTSUPP ; + rc = -EOPNOTSUPP ; out:rcu_read_unlock (); -return 0 ; +return rc; }int intel_cap_audit (enum cap_audit_type type,struct intel_iommu *iommu)
Signature · 6 lines
2.25.1 _______________________________________________ iommu mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/iommu
Lu Baolu
maintainerPATCH 2/2 iommu/vt-d: Fix unmap_pages support
In reply to Lu BaoluFrom: Alex Williamson <[email protected]>
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.
--- drivers/iommu/intel/iommu.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iommu/intel/iommu.c +1 -1
@@ -1366,7 +1366,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)
Signature · 6 lines
2.25.1 _______________________________________________ iommu mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/iommu
Lu Baolu
maintainerRe: [PATCH 2/2] iommu/vt-d: Fix unmap_pages support
Quoted 36 lines · Lu Baolu
From: Alex Williamson<[email protected]>
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 providedpgsize_bitmap. VT-d previously used essentiallyPAGE_MASKfor 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, theunmap_pagesinterface 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 of0x7fe00, which is clearly greater than0x401ff, 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 fromlevel_pfnrather than our working pfn.
Quoted 7 lines
Fixes 3f34f1259776 ("iommu/vt-d: Implement map/unmap_pages() iommu_ops callback")Reported-by Ajay Garg<[email protected]>Signed-off-by Alex Williamson<[email protected]>Tested-by Giovanni Cabiddu<[email protected]>Signed-off-by Lu Baolu<[email protected]>
0-day build robot reported an error on this patch.
"drivers/iommu/intel/iommu.c:1344:7: warning: variable 'level_pfn' is used uninitialized whenever 'if' condition is true [-Wsometimes-uninitialized] "
I will send a v2 of this patch. Sorry for the inconvenience.
Best regards
baolu
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
Joerg Roedel
maintainerQuoted 5 lines · Lu Baolu
Alex Williamson (1):
iommu/vt-d: Fixunmap_pagessupportChristophe JAILLET (1):
iommu/vt-d: Fix an unbalanced rcu_read_lock/rcu_read_unlock()
Queued for v5.16, thanks.
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu