Skip to content

Commit 4ce385f

Browse files
jgross1hansendc
authored andcommitted
x86/mm: Fix _pgd_alloc() for Xen PV mode
Recently _pgd_alloc() was switched from using __get_free_pages() to pagetable_alloc_noprof(), which might return a compound page in case the allocation order is larger than 0. On x86 this will be the case if CONFIG_MITIGATION_PAGE_TABLE_ISOLATION is set, even if PTI has been disabled at runtime. When running as a Xen PV guest (this will always disable PTI), using a compound page for a PGD will result in VM_BUG_ON_PGFLAGS being triggered when the Xen code tries to pin the PGD. Fix the Xen issue together with the not needed 8k allocation for a PGD with PTI disabled by replacing PGD_ALLOCATION_ORDER with an inline helper returning the needed order for PGD allocations. Fixes: a9b3c35 ("asm-generic: pgalloc: provide generic __pgd_{alloc,free}") Reported-by: Petr Vaněk <arkamar@atlas.cz> Signed-off-by: Juergen Gross <jgross@suse.com> Signed-off-by: Dave Hansen <dave.hansen@linux.intel.com> Tested-by: Petr Vaněk <arkamar@atlas.cz> Cc:stable@vger.kernel.org Link: https://lore.kernel.org/all/20250422131717.25724-1-jgross%40suse.com
1 parent 83b2d34 commit 4ce385f

File tree

4 files changed

+17
-14
lines changed

4 files changed

+17
-14
lines changed

arch/x86/include/asm/pgalloc.h

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#include <linux/mm.h> /* for struct page */
77
#include <linux/pagemap.h>
88

9+
#include <asm/cpufeature.h>
10+
911
#define __HAVE_ARCH_PTE_ALLOC_ONE
1012
#define __HAVE_ARCH_PGD_FREE
1113
#include <asm-generic/pgalloc.h>
@@ -29,16 +31,17 @@ static inline void paravirt_release_pud(unsigned long pfn) {}
2931
static inline void paravirt_release_p4d(unsigned long pfn) {}
3032
#endif
3133

32-
#ifdef CONFIG_MITIGATION_PAGE_TABLE_ISOLATION
3334
/*
34-
* Instead of one PGD, we acquire two PGDs. Being order-1, it is
35-
* both 8k in size and 8k-aligned. That lets us just flip bit 12
36-
* in a pointer to swap between the two 4k halves.
35+
* In case of Page Table Isolation active, we acquire two PGDs instead of one.
36+
* Being order-1, it is both 8k in size and 8k-aligned. That lets us just
37+
* flip bit 12 in a pointer to swap between the two 4k halves.
3738
*/
38-
#define PGD_ALLOCATION_ORDER 1
39-
#else
40-
#define PGD_ALLOCATION_ORDER 0
41-
#endif
39+
static inline unsigned int pgd_allocation_order(void)
40+
{
41+
if (cpu_feature_enabled(X86_FEATURE_PTI))
42+
return 1;
43+
return 0;
44+
}
4245

4346
/*
4447
* Allocate and free page tables.

arch/x86/kernel/machine_kexec_32.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ static void load_segments(void)
4242

4343
static void machine_kexec_free_page_tables(struct kimage *image)
4444
{
45-
free_pages((unsigned long)image->arch.pgd, PGD_ALLOCATION_ORDER);
45+
free_pages((unsigned long)image->arch.pgd, pgd_allocation_order());
4646
image->arch.pgd = NULL;
4747
#ifdef CONFIG_X86_PAE
4848
free_page((unsigned long)image->arch.pmd0);
@@ -59,7 +59,7 @@ static void machine_kexec_free_page_tables(struct kimage *image)
5959
static int machine_kexec_alloc_page_tables(struct kimage *image)
6060
{
6161
image->arch.pgd = (pgd_t *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
62-
PGD_ALLOCATION_ORDER);
62+
pgd_allocation_order());
6363
#ifdef CONFIG_X86_PAE
6464
image->arch.pmd0 = (pmd_t *)get_zeroed_page(GFP_KERNEL);
6565
image->arch.pmd1 = (pmd_t *)get_zeroed_page(GFP_KERNEL);

arch/x86/mm/pgtable.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ static inline pgd_t *_pgd_alloc(struct mm_struct *mm)
360360
* We allocate one page for pgd.
361361
*/
362362
if (!SHARED_KERNEL_PMD)
363-
return __pgd_alloc(mm, PGD_ALLOCATION_ORDER);
363+
return __pgd_alloc(mm, pgd_allocation_order());
364364

365365
/*
366366
* Now PAE kernel is not running as a Xen domain. We can allocate
@@ -380,7 +380,7 @@ static inline void _pgd_free(struct mm_struct *mm, pgd_t *pgd)
380380

381381
static inline pgd_t *_pgd_alloc(struct mm_struct *mm)
382382
{
383-
return __pgd_alloc(mm, PGD_ALLOCATION_ORDER);
383+
return __pgd_alloc(mm, pgd_allocation_order());
384384
}
385385

386386
static inline void _pgd_free(struct mm_struct *mm, pgd_t *pgd)

arch/x86/platform/efi/efi_64.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ int __init efi_alloc_page_tables(void)
7373
gfp_t gfp_mask;
7474

7575
gfp_mask = GFP_KERNEL | __GFP_ZERO;
76-
efi_pgd = (pgd_t *)__get_free_pages(gfp_mask, PGD_ALLOCATION_ORDER);
76+
efi_pgd = (pgd_t *)__get_free_pages(gfp_mask, pgd_allocation_order());
7777
if (!efi_pgd)
7878
goto fail;
7979

@@ -96,7 +96,7 @@ int __init efi_alloc_page_tables(void)
9696
if (pgtable_l5_enabled())
9797
free_page((unsigned long)pgd_page_vaddr(*pgd));
9898
free_pgd:
99-
free_pages((unsigned long)efi_pgd, PGD_ALLOCATION_ORDER);
99+
free_pages((unsigned long)efi_pgd, pgd_allocation_order());
100100
fail:
101101
return -ENOMEM;
102102
}

0 commit comments

Comments
 (0)