linux-stable/drivers/iommu/intel
Alex Williamson 724ee060d0 iommu/vt-d: Fix unmap_pages support
[ Upstream commit 86dc40c7ea ]

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: 3f34f12597 ("iommu/vt-d: Implement map/unmap_pages() iommu_ops callback")
Reported-by: Ajay Garg <ajaygargnsit@gmail.com>
Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
Tested-by: Giovanni Cabiddu <giovanni.cabiddu@intel.com>
Link: https://lore.kernel.org/all/20211002124012.18186-1-ajaygargnsit@gmail.com/
Link: https://lore.kernel.org/r/163659074748.1617923.12716161410774184024.stgit@omen
Signed-off-by: Lu Baolu <baolu.lu@linux.intel.com>
Link: https://lore.kernel.org/r/20211126135556.397932-3-baolu.lu@linux.intel.com
Signed-off-by: Joerg Roedel <jroedel@suse.de>
Signed-off-by: Sasha Levin <sashal@kernel.org>
2021-12-01 09:04:54 +01:00
..
cap_audit.c iommu/vt-d: Move capability check code to cap_audit files 2021-02-04 14:42:00 +01:00
cap_audit.h iommu/vt-d: Move capability check code to cap_audit files 2021-02-04 14:42:00 +01:00
debugfs.c iommu/vt-d: Expose latency monitor data through debugfs 2021-06-10 09:06:13 +02:00
dmar.c iommu/vt-d: Drop "0x" prefix from PCI bus & device addresses 2021-09-28 11:41:11 +02:00
iommu.c iommu/vt-d: Fix unmap_pages support 2021-12-01 09:04:54 +01:00
irq_remapping.c pci-v5.13-changes 2021-05-05 13:24:11 -07:00
Kconfig iommu/vt-d: Enable Intel IOMMU scalable mode by default 2021-08-19 10:41:08 +02:00
Makefile iommu/vt-d: Add common code for dmar latency performance monitors 2021-06-10 09:06:13 +02:00
pasid.c Merge branches 'apple/dart', 'arm/smmu', 'iommu/fixes', 'x86/amd', 'x86/vt-d' and 'core' into next 2021-08-20 17:14:35 +02:00
pasid.h Merge branches 'apple/dart', 'arm/smmu', 'iommu/fixes', 'x86/amd', 'x86/vt-d' and 'core' into next 2021-08-20 17:14:35 +02:00
perf.c iommu/vt-d: Drop the kernel doc annotation 2021-08-19 10:41:08 +02:00
perf.h iommu/vt-d: Add common code for dmar latency performance monitors 2021-06-10 09:06:13 +02:00
svm.c iommu/vt-d: Fix a deadlock in intel_svm_drain_prq() 2021-09-09 13:18:07 +02:00
trace.c