Skip to content

Commit f0f5863

Browse files
Ryan Robertswilldeacon
authored andcommitted
arm64/mm: Remove PTE_PROT_NONE bit
Currently the PTE_PRESENT_INVALID and PTE_PROT_NONE functionality explicitly occupy 2 bits in the PTE when PTE_VALID/PMD_SECT_VALID is clear. This has 2 significant consequences: - PTE_PROT_NONE consumes a precious SW PTE bit that could be used for other things. - The swap pte layout must reserve those same 2 bits and ensure they are both always zero for a swap pte. It would be nice to reclaim at least one of those bits. But PTE_PRESENT_INVALID, which since the previous patch, applies uniformly to page/block descriptors at any level when PTE_VALID is clear, can already give us most of what PTE_PROT_NONE requires: If it is set, then the pte is still considered present; pte_present() returns true and all the fields in the pte follow the HW interpretation (e.g. SW can safely call pte_pfn(), etc). But crucially, the HW treats the pte as invalid and will fault if it hits. So let's remove PTE_PROT_NONE entirely and instead represent PROT_NONE as a present but invalid pte (PTE_VALID=0, PTE_PRESENT_INVALID=1) with PTE_USER=0 and PTE_UXN=1. This is a unique combination that is not used anywhere else. The net result is a clearer, simpler, more generic encoding scheme that applies uniformly to all levels. Additionally we free up a PTE SW bit and a swap pte bit (bit 58 in both cases). Reviewed-by: Catalin Marinas <[email protected]> Signed-off-by: Ryan Roberts <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Will Deacon <[email protected]>
1 parent b28c74e commit f0f5863

File tree

2 files changed

+18
-16
lines changed

2 files changed

+18
-16
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
#define PTE_DIRTY (_AT(pteval_t, 1) << 55)
1919
#define PTE_SPECIAL (_AT(pteval_t, 1) << 56)
2020
#define PTE_DEVMAP (_AT(pteval_t, 1) << 57)
21-
#define PTE_PROT_NONE (_AT(pteval_t, 1) << 58) /* only when !PTE_VALID */
2221

2322
/*
2423
* PTE_PRESENT_INVALID=1 & PTE_VALID=0 indicates that the pte's fields should be
@@ -103,7 +102,7 @@ static inline bool __pure lpa2_is_enabled(void)
103102
__val; \
104103
})
105104

106-
#define PAGE_NONE __pgprot(((_PAGE_DEFAULT) & ~PTE_VALID) | PTE_PROT_NONE | PTE_RDONLY | PTE_NG | PTE_PXN | PTE_UXN)
105+
#define PAGE_NONE __pgprot(((_PAGE_DEFAULT) & ~PTE_VALID) | PTE_PRESENT_INVALID | PTE_RDONLY | PTE_NG | PTE_PXN | PTE_UXN)
107106
/* shared+writable pages are clean by default, hence PTE_RDONLY|PTE_WRITE */
108107
#define PAGE_SHARED __pgprot(_PAGE_SHARED)
109108
#define PAGE_SHARED_EXEC __pgprot(_PAGE_SHARED_EXEC)

arch/arm64/include/asm/pgtable.h

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ static inline pteval_t __phys_to_pte_val(phys_addr_t phys)
105105
/*
106106
* The following only work if pte_present(). Undefined behaviour otherwise.
107107
*/
108-
#define pte_present(pte) (!!(pte_val(pte) & (PTE_VALID | PTE_PROT_NONE)))
108+
#define pte_present(pte) (pte_valid(pte) || pte_present_invalid(pte))
109109
#define pte_young(pte) (!!(pte_val(pte) & PTE_AF))
110110
#define pte_special(pte) (!!(pte_val(pte) & PTE_SPECIAL))
111111
#define pte_write(pte) (!!(pte_val(pte) & PTE_WRITE))
@@ -483,7 +483,16 @@ static inline pte_t pte_swp_clear_exclusive(pte_t pte)
483483
*/
484484
static inline int pte_protnone(pte_t pte)
485485
{
486-
return (pte_val(pte) & (PTE_VALID | PTE_PROT_NONE)) == PTE_PROT_NONE;
486+
/*
487+
* pte_present_invalid() tells us that the pte is invalid from HW
488+
* perspective but present from SW perspective, so the fields are to be
489+
* interpretted as per the HW layout. The second 2 checks are the unique
490+
* encoding that we use for PROT_NONE. It is insufficient to only use
491+
* the first check because we share the same encoding scheme with pmds
492+
* which support pmd_mkinvalid(), so can be present-invalid without
493+
* being PROT_NONE.
494+
*/
495+
return pte_present_invalid(pte) && !pte_user(pte) && !pte_user_exec(pte);
487496
}
488497

489498
static inline int pmd_protnone(pmd_t pmd)
@@ -492,12 +501,7 @@ static inline int pmd_protnone(pmd_t pmd)
492501
}
493502
#endif
494503

495-
#define pmd_present_invalid(pmd) pte_present_invalid(pmd_pte(pmd))
496-
497-
static inline int pmd_present(pmd_t pmd)
498-
{
499-
return pte_present(pmd_pte(pmd)) || pmd_present_invalid(pmd);
500-
}
504+
#define pmd_present(pmd) pte_present(pmd_pte(pmd))
501505

502506
/*
503507
* THP definitions.
@@ -1036,8 +1040,8 @@ static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
10361040
* in MAIR_EL1. The mask below has to include PTE_ATTRINDX_MASK.
10371041
*/
10381042
const pteval_t mask = PTE_USER | PTE_PXN | PTE_UXN | PTE_RDONLY |
1039-
PTE_PROT_NONE | PTE_VALID | PTE_WRITE | PTE_GP |
1040-
PTE_ATTRINDX_MASK;
1043+
PTE_PRESENT_INVALID | PTE_VALID | PTE_WRITE |
1044+
PTE_GP | PTE_ATTRINDX_MASK;
10411045
/* preserve the hardware dirty information */
10421046
if (pte_hw_dirty(pte))
10431047
pte = set_pte_bit(pte, __pgprot(PTE_DIRTY));
@@ -1085,17 +1089,17 @@ static inline int pgd_devmap(pgd_t pgd)
10851089
#ifdef CONFIG_PAGE_TABLE_CHECK
10861090
static inline bool pte_user_accessible_page(pte_t pte)
10871091
{
1088-
return pte_present(pte) && (pte_user(pte) || pte_user_exec(pte));
1092+
return pte_valid(pte) && (pte_user(pte) || pte_user_exec(pte));
10891093
}
10901094

10911095
static inline bool pmd_user_accessible_page(pmd_t pmd)
10921096
{
1093-
return pmd_leaf(pmd) && !pmd_present_invalid(pmd) && (pmd_user(pmd) || pmd_user_exec(pmd));
1097+
return pmd_valid(pmd) && !pmd_table(pmd) && (pmd_user(pmd) || pmd_user_exec(pmd));
10941098
}
10951099

10961100
static inline bool pud_user_accessible_page(pud_t pud)
10971101
{
1098-
return pud_leaf(pud) && (pud_user(pud) || pud_user_exec(pud));
1102+
return pud_valid(pud) && !pud_table(pud) && (pud_user(pud) || pud_user_exec(pud));
10991103
}
11001104
#endif
11011105

@@ -1259,7 +1263,6 @@ static inline pmd_t pmdp_establish(struct vm_area_struct *vma,
12591263
* bits 2: remember PG_anon_exclusive
12601264
* bits 3-7: swap type
12611265
* bits 8-57: swap offset
1262-
* bit 58: PTE_PROT_NONE (must be zero)
12631266
* bit 59: PTE_PRESENT_INVALID (must be zero)
12641267
*/
12651268
#define __SWP_TYPE_SHIFT 3

0 commit comments

Comments
 (0)