Skip to content

Commit 0bb8aee

Browse files
suryasaimadhuMatt Fleming
authored andcommitted
x86/mm/pageattr: Add a PUD error unwinding path
In case we encounter an error during the mapping of a region, we want to unwind what we've established so far exactly the way we did the mapping. This is the PUD part kept deliberately small for easier review. Signed-off-by: Borislav Petkov <[email protected]> Signed-off-by: Matt Fleming <[email protected]>
1 parent c6b6f36 commit 0bb8aee

File tree

1 file changed

+58
-2
lines changed

1 file changed

+58
-2
lines changed

arch/x86/mm/pageattr.c

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,51 @@ static int split_large_page(pte_t *kpte, unsigned long address)
666666
return 0;
667667
}
668668

669+
#define unmap_pmd_range(pud, start, pre_end) do {} while (0)
670+
671+
static void unmap_pud_range(pgd_t *pgd, unsigned long start, unsigned long end)
672+
{
673+
pud_t *pud = pud_offset(pgd, start);
674+
675+
/*
676+
* Not on a GB page boundary?
677+
*/
678+
if (start & (PUD_SIZE - 1)) {
679+
unsigned long next_page = (start + PUD_SIZE) & PUD_MASK;
680+
unsigned long pre_end = min_t(unsigned long, end, next_page);
681+
682+
unmap_pmd_range(pud, start, pre_end);
683+
684+
start = pre_end;
685+
pud++;
686+
}
687+
688+
/*
689+
* Try to unmap in 1G chunks?
690+
*/
691+
while (end - start >= PUD_SIZE) {
692+
693+
if (pud_large(*pud))
694+
pud_clear(pud);
695+
else
696+
unmap_pmd_range(pud, start, start + PUD_SIZE);
697+
698+
start += PUD_SIZE;
699+
pud++;
700+
}
701+
702+
/*
703+
* 2M leftovers?
704+
*/
705+
if (start < end)
706+
unmap_pmd_range(pud, start, end);
707+
708+
/*
709+
* No need to try to free the PUD page because we'll free it in
710+
* populate_pgd's error path
711+
*/
712+
}
713+
669714
static int alloc_pte_page(pmd_t *pmd)
670715
{
671716
pte_t *pte = (pte_t *)get_zeroed_page(GFP_KERNEL | __GFP_NOTRACK);
@@ -883,9 +928,20 @@ static int populate_pgd(struct cpa_data *cpa, unsigned long addr)
883928
pgprot_val(pgprot) |= pgprot_val(cpa->mask_set);
884929

885930
ret = populate_pud(cpa, addr, pgd_entry, pgprot);
886-
if (ret < 0)
887-
return ret;
931+
if (ret < 0) {
932+
unmap_pud_range(pgd_entry, addr,
933+
addr + (cpa->numpages << PAGE_SHIFT));
888934

935+
if (allocd_pgd) {
936+
/*
937+
* If I allocated this PUD page, I can just as well
938+
* free it in this error path.
939+
*/
940+
pgd_clear(pgd_entry);
941+
free_page((unsigned long)pud);
942+
}
943+
return ret;
944+
}
889945
cpa->numpages = ret;
890946
return 0;
891947
}

0 commit comments

Comments
 (0)