Skip to content

Commit 59bf355

Browse files
David Stevensjoergroedel
authored andcommitted
iommu/vt-d: Calculate mask for non-aligned flushes
Calculate the appropriate mask for non-size-aligned page selective invalidation. Since psi uses the mask value to mask out the lower order bits of the target address, properly flushing the iotlb requires using a mask value such that [pfn, pfn+pages) all lie within the flushed size-aligned region. This is not normally an issue because iova.c always allocates iovas that are aligned to their size. However, iovas which come from other sources (e.g. userspace via VFIO) may not be aligned. To properly flush the IOTLB, both the start and end pfns need to be equal after applying the mask. That means that the most efficient mask to use is the index of the lowest bit that is equal where all higher bits are also equal. For example, if pfn=0x17f and pages=3, then end_pfn=0x181, so the smallest mask we can use is 8. Any differences above the highest bit of pages are due to carrying, so by xnor'ing pfn and end_pfn and then masking out the lower order bits based on pages, we get 0xffffff00, where the first set bit is the mask we want to use. Fixes: 6fe1010 ("vfio/type1: DMA unmap chunking") Cc: [email protected] Signed-off-by: David Stevens <[email protected]> Reviewed-by: Kevin Tian <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Lu Baolu <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Joerg Roedel <[email protected]>
1 parent af2d861 commit 59bf355

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

drivers/iommu/intel/iommu.c

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1588,7 +1588,8 @@ static void iommu_flush_iotlb_psi(struct intel_iommu *iommu,
15881588
unsigned long pfn, unsigned int pages,
15891589
int ih, int map)
15901590
{
1591-
unsigned int mask = ilog2(__roundup_pow_of_two(pages));
1591+
unsigned int aligned_pages = __roundup_pow_of_two(pages);
1592+
unsigned int mask = ilog2(aligned_pages);
15921593
uint64_t addr = (uint64_t)pfn << VTD_PAGE_SHIFT;
15931594
u16 did = domain->iommu_did[iommu->seq_id];
15941595

@@ -1600,10 +1601,30 @@ static void iommu_flush_iotlb_psi(struct intel_iommu *iommu,
16001601
if (domain_use_first_level(domain)) {
16011602
qi_flush_piotlb(iommu, did, PASID_RID2PASID, addr, pages, ih);
16021603
} else {
1604+
unsigned long bitmask = aligned_pages - 1;
1605+
1606+
/*
1607+
* PSI masks the low order bits of the base address. If the
1608+
* address isn't aligned to the mask, then compute a mask value
1609+
* needed to ensure the target range is flushed.
1610+
*/
1611+
if (unlikely(bitmask & pfn)) {
1612+
unsigned long end_pfn = pfn + pages - 1, shared_bits;
1613+
1614+
/*
1615+
* Since end_pfn <= pfn + bitmask, the only way bits
1616+
* higher than bitmask can differ in pfn and end_pfn is
1617+
* by carrying. This means after masking out bitmask,
1618+
* high bits starting with the first set bit in
1619+
* shared_bits are all equal in both pfn and end_pfn.
1620+
*/
1621+
shared_bits = ~(pfn ^ end_pfn) & ~bitmask;
1622+
mask = shared_bits ? __ffs(shared_bits) : BITS_PER_LONG;
1623+
}
1624+
16031625
/*
16041626
* Fallback to domain selective flush if no PSI support or
1605-
* the size is too big. PSI requires page size to be 2 ^ x,
1606-
* and the base address is naturally aligned to the size.
1627+
* the size is too big.
16071628
*/
16081629
if (!cap_pgsel_inv(iommu->cap) ||
16091630
mask > cap_max_amask_val(iommu->cap))

0 commit comments

Comments
 (0)