Skip to content

Commit cc44954

Browse files
Andrei Duleajoergroedel
authored andcommitted
iommu/amd: Unmap all L7 PTEs when downgrading page-sizes
When replacing a large mapping created with page-mode 7 (i.e. non-default page size), tear down the entire series of replicated PTEs. Besides providing access to the old mapping, another thing that might go wrong with this issue is on the fetch_pte() code path that can return a PDE entry of the newly re-mapped range. While at it, make sure that we flush the TLB in case alloc_pte() fails and returns NULL at a lower level. Fixes: 6d568ef ("iommu/amd: Allow downgrading page-sizes in alloc_pte()") Signed-off-by: Andrei Dulea <[email protected]>
1 parent 7f1f168 commit cc44954

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

drivers/iommu/amd_iommu.c

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1512,10 +1512,32 @@ static u64 *alloc_pte(struct protection_domain *domain,
15121512
__pte = *pte;
15131513
pte_level = PM_PTE_LEVEL(__pte);
15141514

1515-
if (!IOMMU_PTE_PRESENT(__pte) ||
1516-
pte_level == PAGE_MODE_NONE ||
1515+
/*
1516+
* If we replace a series of large PTEs, we need
1517+
* to tear down all of them.
1518+
*/
1519+
if (IOMMU_PTE_PRESENT(__pte) &&
15171520
pte_level == PAGE_MODE_7_LEVEL) {
1521+
unsigned long count, i;
1522+
u64 *lpte;
1523+
1524+
lpte = first_pte_l7(pte, NULL, &count);
1525+
1526+
/*
1527+
* Unmap the replicated PTEs that still match the
1528+
* original large mapping
1529+
*/
1530+
for (i = 0; i < count; ++i)
1531+
cmpxchg64(&lpte[i], __pte, 0ULL);
1532+
1533+
domain->updated = true;
1534+
continue;
1535+
}
1536+
1537+
if (!IOMMU_PTE_PRESENT(__pte) ||
1538+
pte_level == PAGE_MODE_NONE) {
15181539
page = (u64 *)get_zeroed_page(gfp);
1540+
15191541
if (!page)
15201542
return NULL;
15211543

@@ -1646,8 +1668,10 @@ static int iommu_map_page(struct protection_domain *dom,
16461668
count = PAGE_SIZE_PTE_COUNT(page_size);
16471669
pte = alloc_pte(dom, bus_addr, page_size, NULL, gfp);
16481670

1649-
if (!pte)
1671+
if (!pte) {
1672+
update_domain(dom);
16501673
return -ENOMEM;
1674+
}
16511675

16521676
for (i = 0; i < count; ++i)
16531677
freelist = free_clear_pte(&pte[i], pte[i], freelist);

0 commit comments

Comments
 (0)