Skip to content

Commit a6bbf5d

Browse files
ardbiesheuvelctmarinas
authored andcommitted
arm64: mm: Add definitions to support 5 levels of paging
Add the required types and descriptor accessors to support 5 levels of paging in the common code. This is one of the prerequisites for supporting 52-bit virtual addressing with 4k pages. Note that this does not cover the code that handles kernel mappings or the fixmap. Signed-off-by: Ard Biesheuvel <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Catalin Marinas <[email protected]>
1 parent 925a0eb commit a6bbf5d

File tree

6 files changed

+188
-9
lines changed

6 files changed

+188
-9
lines changed

arch/arm64/include/asm/pgalloc.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,47 @@ static inline void __p4d_populate(p4d_t *p4dp, phys_addr_t pudp, p4dval_t prot)
6060
}
6161
#endif /* CONFIG_PGTABLE_LEVELS > 3 */
6262

63+
#if CONFIG_PGTABLE_LEVELS > 4
64+
65+
static inline void __pgd_populate(pgd_t *pgdp, phys_addr_t p4dp, pgdval_t prot)
66+
{
67+
if (pgtable_l5_enabled())
68+
set_pgd(pgdp, __pgd(__phys_to_pgd_val(p4dp) | prot));
69+
}
70+
71+
static inline void pgd_populate(struct mm_struct *mm, pgd_t *pgdp, p4d_t *p4dp)
72+
{
73+
pgdval_t pgdval = PGD_TYPE_TABLE;
74+
75+
pgdval |= (mm == &init_mm) ? PGD_TABLE_UXN : PGD_TABLE_PXN;
76+
__pgd_populate(pgdp, __pa(p4dp), pgdval);
77+
}
78+
79+
static inline p4d_t *p4d_alloc_one(struct mm_struct *mm, unsigned long addr)
80+
{
81+
gfp_t gfp = GFP_PGTABLE_USER;
82+
83+
if (mm == &init_mm)
84+
gfp = GFP_PGTABLE_KERNEL;
85+
return (p4d_t *)get_zeroed_page(gfp);
86+
}
87+
88+
static inline void p4d_free(struct mm_struct *mm, p4d_t *p4d)
89+
{
90+
if (!pgtable_l5_enabled())
91+
return;
92+
BUG_ON((unsigned long)p4d & (PAGE_SIZE-1));
93+
free_page((unsigned long)p4d);
94+
}
95+
96+
#define __p4d_free_tlb(tlb, p4d, addr) p4d_free((tlb)->mm, p4d)
97+
#else
98+
static inline void __pgd_populate(pgd_t *pgdp, phys_addr_t p4dp, pgdval_t prot)
99+
{
100+
BUILD_BUG();
101+
}
102+
#endif /* CONFIG_PGTABLE_LEVELS > 4 */
103+
63104
extern pgd_t *pgd_alloc(struct mm_struct *mm);
64105
extern void pgd_free(struct mm_struct *mm, pgd_t *pgdp);
65106

arch/arm64/include/asm/pgtable-hwdef.h

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@
2626
#define ARM64_HW_PGTABLE_LEVELS(va_bits) (((va_bits) - 4) / (PAGE_SHIFT - 3))
2727

2828
/*
29-
* Size mapped by an entry at level n ( 0 <= n <= 3)
29+
* Size mapped by an entry at level n ( -1 <= n <= 3)
3030
* We map (PAGE_SHIFT - 3) at all translation levels and PAGE_SHIFT bits
3131
* in the final page. The maximum number of translation levels supported by
32-
* the architecture is 4. Hence, starting at level n, we have further
32+
* the architecture is 5. Hence, starting at level n, we have further
3333
* ((4 - n) - 1) levels of translation excluding the offset within the page.
3434
* So, the total number of bits mapped by an entry at level n is :
3535
*
@@ -62,9 +62,16 @@
6262
#define PTRS_PER_PUD (1 << (PAGE_SHIFT - 3))
6363
#endif
6464

65+
#if CONFIG_PGTABLE_LEVELS > 4
66+
#define P4D_SHIFT ARM64_HW_PGTABLE_LEVEL_SHIFT(0)
67+
#define P4D_SIZE (_AC(1, UL) << P4D_SHIFT)
68+
#define P4D_MASK (~(P4D_SIZE-1))
69+
#define PTRS_PER_P4D (1 << (PAGE_SHIFT - 3))
70+
#endif
71+
6572
/*
6673
* PGDIR_SHIFT determines the size a top-level page table entry can map
67-
* (depending on the configuration, this level can be 0, 1 or 2).
74+
* (depending on the configuration, this level can be -1, 0, 1 or 2).
6875
*/
6976
#define PGDIR_SHIFT ARM64_HW_PGTABLE_LEVEL_SHIFT(4 - CONFIG_PGTABLE_LEVELS)
7077
#define PGDIR_SIZE (_AC(1, UL) << PGDIR_SHIFT)
@@ -87,6 +94,15 @@
8794
/*
8895
* Hardware page table definitions.
8996
*
97+
* Level -1 descriptor (PGD).
98+
*/
99+
#define PGD_TYPE_TABLE (_AT(pgdval_t, 3) << 0)
100+
#define PGD_TABLE_BIT (_AT(pgdval_t, 1) << 1)
101+
#define PGD_TYPE_MASK (_AT(pgdval_t, 3) << 0)
102+
#define PGD_TABLE_PXN (_AT(pgdval_t, 1) << 59)
103+
#define PGD_TABLE_UXN (_AT(pgdval_t, 1) << 60)
104+
105+
/*
90106
* Level 0 descriptor (P4D).
91107
*/
92108
#define P4D_TYPE_TABLE (_AT(p4dval_t, 3) << 0)

arch/arm64/include/asm/pgtable-types.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ typedef struct { pudval_t pud; } pud_t;
3636
#define __pud(x) ((pud_t) { (x) } )
3737
#endif
3838

39+
#if CONFIG_PGTABLE_LEVELS > 4
40+
typedef struct { p4dval_t p4d; } p4d_t;
41+
#define p4d_val(x) ((x).p4d)
42+
#define __p4d(x) ((p4d_t) { (x) } )
43+
#endif
44+
3945
typedef struct { pgdval_t pgd; } pgd_t;
4046
#define pgd_val(x) ((x).pgd)
4147
#define __pgd(x) ((pgd_t) { (x) } )

arch/arm64/include/asm/pgtable.h

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -808,7 +808,6 @@ static inline pud_t *p4d_pgtable(p4d_t p4d)
808808
#else
809809

810810
#define p4d_page_paddr(p4d) ({ BUILD_BUG(); 0;})
811-
#define pgd_page_paddr(pgd) ({ BUILD_BUG(); 0;})
812811

813812
/* Match pud_offset folding in <asm/generic/pgtable-nopud.h> */
814813
#define pud_set_fixmap(addr) NULL
@@ -819,6 +818,87 @@ static inline pud_t *p4d_pgtable(p4d_t p4d)
819818

820819
#endif /* CONFIG_PGTABLE_LEVELS > 3 */
821820

821+
#if CONFIG_PGTABLE_LEVELS > 4
822+
823+
static __always_inline bool pgtable_l5_enabled(void)
824+
{
825+
if (!alternative_has_cap_likely(ARM64_ALWAYS_BOOT))
826+
return vabits_actual == VA_BITS;
827+
return alternative_has_cap_unlikely(ARM64_HAS_VA52);
828+
}
829+
830+
static inline bool mm_p4d_folded(const struct mm_struct *mm)
831+
{
832+
return !pgtable_l5_enabled();
833+
}
834+
#define mm_p4d_folded mm_p4d_folded
835+
836+
#define p4d_ERROR(e) \
837+
pr_err("%s:%d: bad p4d %016llx.\n", __FILE__, __LINE__, p4d_val(e))
838+
839+
#define pgd_none(pgd) (pgtable_l5_enabled() && !pgd_val(pgd))
840+
#define pgd_bad(pgd) (pgtable_l5_enabled() && !(pgd_val(pgd) & 2))
841+
#define pgd_present(pgd) (!pgd_none(pgd))
842+
843+
static inline void set_pgd(pgd_t *pgdp, pgd_t pgd)
844+
{
845+
if (in_swapper_pgdir(pgdp)) {
846+
set_swapper_pgd(pgdp, __pgd(pgd_val(pgd)));
847+
return;
848+
}
849+
850+
WRITE_ONCE(*pgdp, pgd);
851+
dsb(ishst);
852+
isb();
853+
}
854+
855+
static inline void pgd_clear(pgd_t *pgdp)
856+
{
857+
if (pgtable_l5_enabled())
858+
set_pgd(pgdp, __pgd(0));
859+
}
860+
861+
static inline phys_addr_t pgd_page_paddr(pgd_t pgd)
862+
{
863+
return __pgd_to_phys(pgd);
864+
}
865+
866+
#define p4d_index(addr) (((addr) >> P4D_SHIFT) & (PTRS_PER_P4D - 1))
867+
868+
static inline p4d_t *pgd_to_folded_p4d(pgd_t *pgdp, unsigned long addr)
869+
{
870+
return (p4d_t *)PTR_ALIGN_DOWN(pgdp, PAGE_SIZE) + p4d_index(addr);
871+
}
872+
873+
static inline phys_addr_t p4d_offset_phys(pgd_t *pgdp, unsigned long addr)
874+
{
875+
BUG_ON(!pgtable_l5_enabled());
876+
877+
return pgd_page_paddr(READ_ONCE(*pgdp)) + p4d_index(addr) * sizeof(p4d_t);
878+
}
879+
880+
static inline
881+
p4d_t *p4d_offset_lockless(pgd_t *pgdp, pgd_t pgd, unsigned long addr)
882+
{
883+
if (!pgtable_l5_enabled())
884+
return pgd_to_folded_p4d(pgdp, addr);
885+
return (p4d_t *)__va(pgd_page_paddr(pgd)) + p4d_index(addr);
886+
}
887+
#define p4d_offset_lockless p4d_offset_lockless
888+
889+
static inline p4d_t *p4d_offset(pgd_t *pgdp, unsigned long addr)
890+
{
891+
return p4d_offset_lockless(pgdp, READ_ONCE(*pgdp), addr);
892+
}
893+
894+
#define pgd_page(pgd) pfn_to_page(__phys_to_pfn(__pgd_to_phys(pgd)))
895+
896+
#else
897+
898+
static inline bool pgtable_l5_enabled(void) { return false; }
899+
900+
#endif /* CONFIG_PGTABLE_LEVELS > 4 */
901+
822902
#define pgd_ERROR(e) \
823903
pr_err("%s:%d: bad pgd %016llx.\n", __FILE__, __LINE__, pgd_val(e))
824904

arch/arm64/mm/mmu.c

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,7 +1025,7 @@ static void free_empty_pud_table(p4d_t *p4dp, unsigned long addr,
10251025
if (CONFIG_PGTABLE_LEVELS <= 3)
10261026
return;
10271027

1028-
if (!pgtable_range_aligned(start, end, floor, ceiling, PGDIR_MASK))
1028+
if (!pgtable_range_aligned(start, end, floor, ceiling, P4D_MASK))
10291029
return;
10301030

10311031
/*
@@ -1048,8 +1048,8 @@ static void free_empty_p4d_table(pgd_t *pgdp, unsigned long addr,
10481048
unsigned long end, unsigned long floor,
10491049
unsigned long ceiling)
10501050
{
1051-
unsigned long next;
10521051
p4d_t *p4dp, p4d;
1052+
unsigned long i, next, start = addr;
10531053

10541054
do {
10551055
next = p4d_addr_end(addr, end);
@@ -1061,6 +1061,27 @@ static void free_empty_p4d_table(pgd_t *pgdp, unsigned long addr,
10611061
WARN_ON(!p4d_present(p4d));
10621062
free_empty_pud_table(p4dp, addr, next, floor, ceiling);
10631063
} while (addr = next, addr < end);
1064+
1065+
if (!pgtable_l5_enabled())
1066+
return;
1067+
1068+
if (!pgtable_range_aligned(start, end, floor, ceiling, PGDIR_MASK))
1069+
return;
1070+
1071+
/*
1072+
* Check whether we can free the p4d page if the rest of the
1073+
* entries are empty. Overlap with other regions have been
1074+
* handled by the floor/ceiling check.
1075+
*/
1076+
p4dp = p4d_offset(pgdp, 0UL);
1077+
for (i = 0; i < PTRS_PER_P4D; i++) {
1078+
if (!p4d_none(READ_ONCE(p4dp[i])))
1079+
return;
1080+
}
1081+
1082+
pgd_clear(pgdp);
1083+
__flush_tlb_kernel_pgtable(start);
1084+
free_hotplug_pgtable_page(virt_to_page(p4dp));
10641085
}
10651086

10661087
static void free_empty_tables(unsigned long addr, unsigned long end,
@@ -1145,6 +1166,12 @@ int pmd_set_huge(pmd_t *pmdp, phys_addr_t phys, pgprot_t prot)
11451166
return 1;
11461167
}
11471168

1169+
#ifndef __PAGETABLE_P4D_FOLDED
1170+
void p4d_clear_huge(p4d_t *p4dp)
1171+
{
1172+
}
1173+
#endif
1174+
11481175
int pud_clear_huge(pud_t *pudp)
11491176
{
11501177
if (!pud_sect(READ_ONCE(*pudp)))

arch/arm64/mm/pgd.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,36 @@
1717

1818
static struct kmem_cache *pgd_cache __ro_after_init;
1919

20+
static bool pgdir_is_page_size(void)
21+
{
22+
if (PGD_SIZE == PAGE_SIZE)
23+
return true;
24+
if (CONFIG_PGTABLE_LEVELS == 5)
25+
return !pgtable_l5_enabled();
26+
return false;
27+
}
28+
2029
pgd_t *pgd_alloc(struct mm_struct *mm)
2130
{
2231
gfp_t gfp = GFP_PGTABLE_USER;
2332

24-
if (PGD_SIZE == PAGE_SIZE)
33+
if (pgdir_is_page_size())
2534
return (pgd_t *)__get_free_page(gfp);
2635
else
2736
return kmem_cache_alloc(pgd_cache, gfp);
2837
}
2938

3039
void pgd_free(struct mm_struct *mm, pgd_t *pgd)
3140
{
32-
if (PGD_SIZE == PAGE_SIZE)
41+
if (pgdir_is_page_size())
3342
free_page((unsigned long)pgd);
3443
else
3544
kmem_cache_free(pgd_cache, pgd);
3645
}
3746

3847
void __init pgtable_cache_init(void)
3948
{
40-
if (PGD_SIZE == PAGE_SIZE)
49+
if (pgdir_is_page_size())
4150
return;
4251

4352
#ifdef CONFIG_ARM64_PA_BITS_52

0 commit comments

Comments
 (0)