arm64/mm: Enable userfaultfd write-protect
v2 cover arm64/mm: Enable userfaultfd write-protect
Hi All,
This series adds uffd write-protect support for arm64.
Previous attempts to add uffd-wp (and soft-dirty) have failed because of a perceived lack of available PTE SW bits. However it actually turns out that there are 2 available but they are hidden. PTE_PROT_NONE was previously occupying a SW bit, but can be moved, freeing up the SW bit. Bit 63 is marked as "IGNORED" in the Arm ARM, but it does not currently indicate "reserved for SW use" like it does for the other SW bits. I've confirmed with the spec owner that this is an oversight; the bit is intended to be reserved for SW use and the spec will clarify this in a future update.
So now we have two spare bits; patch 3 enables uffd-wp on arm64, using the SW bit freed up by moving PTE_PROT_NONE. This leaves bit 63 spare for future use (e.g. soft-dirty - see RFC at [2] - or some other usage).
---
This applies on top of v6.9-rc5.
All mm selftests involving uffd-wp now run. However, this work exposed a bug in core-mm that was leading to some test uffd-wp failures in the pagemap_ioctl test. The fix for that is posted at [3]. With the fix applied, all the uffd-wp tests pass and no other selftest regressions are observed.
Changes since v1 [1]
====================
patch 1 & 2 (was patch 1):
- generalized PMD_PRESENT_INVALID into PTE_INVALID
- removed explicit PTE_PROT_NONE bit
patch 3 (was patch 2):
- collected R-b/A-b from Peter and Catalin - thanks!
[1] https://lore.kernel.org/linux-arm-kernel/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/
[3] https://lore.kernel.org/all/[email protected]/
Thanks,
Ryan
Ryan Roberts (3):
arm64/mm: Refactor PMD_PRESENT_INVALID and PTE_PROT_NONE bits arm64/mm: Move PTE_INVALID to overlay PTE_NS arm64/mm: Add uffd write-protect support
arch/arm64/Kconfig | 1 + arch/arm64/include/asm/pgtable-hwdef.h | 1 + arch/arm64/include/asm/pgtable-prot.h | 17 ++--- arch/arm64/include/asm/pgtable.h | 88 +++++++++++++++++++------- 4 files changed, 75 insertions(+), 32 deletions(-)
--
2.25.1
PATCH v2 1/3 arm64/mm: Refactor PMD_PRESENT_INVALID and PTE_PROT_NONE bits
Currently the PMD_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.
Note that while PMD_PRESENT_INVALID technically only applies to pmds, the swap pte layout is common to ptes and pmds so we are currently effectively reserving that bit at both levels.
Let's replace PMD_PRESENT_INVALID with a more generic PTE_INVALID bit, which occupies the same position (bit 59) but applies uniformly to page/block descriptors at any level. This bit is only interpretted when PTE_VALID is clear. 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.
With this in place, we can remove PTE_PROT_NONE entirely and instead represent PROT_NONE as a present but invalid pte (PTE_VALID=0, PTE_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 a swap pte bit (bit 58 in both cases).
--- arch/arm64/include/asm/pgtable-prot.h | 11 ++------ arch/arm64/include/asm/pgtable.h | 38 ++++++++++++--------------- 2 files changed, 19 insertions(+), 30 deletions(-)
arch/arm64/include/asm/pgtable-prot.h +2 -9
@@ -18,14 +18,7 @@#define PTE_DIRTY (_AT(pteval_t, 1) << 55)#define PTE_SPECIAL (_AT(pteval_t, 1) << 56)#define PTE_DEVMAP (_AT(pteval_t, 1) << 57) -#define PTE_PROT_NONE (_AT(pteval_t, 1) << 58)/* only when !PTE_VALID */ - -/* -* This bit indicates that the entry is present i.e. pmd_page() -* still points to a valid huge page in memory even if the pmd -* has been invalidated. -*/ -#define PMD_PRESENT_INVALID (_AT(pteval_t, 1) << 59)/* only when !PMD_SECT_VALID */ +#define PTE_INVALID (_AT(pteval_t, 1) << 59)/* only when !PTE_VALID */ #define _PROT_DEFAULT (PTE_TYPE_PAGE | PTE_AF | PTE_SHARED)#define _PROT_SECT_DEFAULT (PMD_TYPE_SECT | PMD_SECT_AF | PMD_SECT_S)
@@ -103,7 +96,7 @@ static inline bool __pure lpa2_is_enabled(void) __val; \ }) -#define PAGE_NONE __pgprot(((_PAGE_DEFAULT) & ~PTE_VALID) | PTE_PROT_NONE | PTE_RDONLY | PTE_NG | PTE_PXN | PTE_UXN) +#define PAGE_NONE __pgprot(((_PAGE_DEFAULT) & ~PTE_VALID) | PTE_INVALID | PTE_RDONLY | PTE_NG | PTE_PXN | PTE_UXN)/* shared+writable pages are clean by default, hence PTE_RDONLY|PTE_WRITE */ #define PAGE_SHARED __pgprot(_PAGE_SHARED)#define PAGE_SHARED_EXEC __pgprot(_PAGE_SHARED_EXEC)
arch/arm64/include/asm/pgtable.h +17 -21
@@ -105,7 +105,7 @@ static inline pteval_t __phys_to_pte_val(phys_addr_t phys)/* * The following only work if pte_present(). Undefined behaviour otherwise. */ -#define pte_present (pte) (!!(pte_val(pte) & (PTE_VALID | PTE_PROT_NONE))) +#define pte_present (pte) (pte_valid(pte) || pte_invalid(pte))#define pte_young (pte) (!!(pte_val(pte) & PTE_AF))#define pte_special (pte) (!!(pte_val(pte) & PTE_SPECIAL))#define pte_write (pte) (!!(pte_val(pte) & PTE_WRITE))
@@ -132,6 +132,7 @@ static inline pteval_t __phys_to_pte_val(phys_addr_t phys)#define pte_dirty (pte) (pte_sw_dirty(pte) || pte_hw_dirty(pte))#define pte_valid (pte) (!!(pte_val(pte) & PTE_VALID)) +#define pte_invalid (pte) ((pte_val(pte) & (PTE_VALID | PTE_INVALID)) == PTE_INVALID)/* * Execute-only user mappings do not have the PTE_USER bit set. All valid * kernel mappings have the PTE_UXN bit set.
@@ -261,6 +262,13 @@ static inline pte_t pte_mkpresent(pte_t pte)return set_pte_bit (pte,__pgprot (PTE_VALID )); } +static inline pte_t pte_mkinvalid (pte_t pte) +{ + pte =set_pte_bit (pte,__pgprot (PTE_INVALID )); + pte =clear_pte_bit (pte,__pgprot (PTE_VALID )); +return pte; +} +static inline pmd_t pmd_mkcont (pmd_t pmd) {return __pmd (pmd_val (pmd) |PMD_SECT_CONT );
@@ -469,7 +477,7 @@ static inline pte_t pte_swp_clear_exclusive(pte_t pte)*/ static inline int pte_protnone (pte_t pte) { -return (pte_val (pte) & (PTE_VALID |PTE_PROT_NONE )) ==PTE_PROT_NONE ; +return pte_invalid (pte) && !pte_user (pte) && !pte_user_exec (pte); }static inline int pmd_protnone (pmd_t pmd)
@@ -478,12 +486,7 @@ static inline int pmd_protnone(pmd_t pmd) }#endif -#define pmd_present_invalid (pmd) (!!(pmd_val(pmd) & PMD_PRESENT_INVALID)) - -static inline int pmd_present (pmd_t pmd) -{ -return pte_present (pmd_pte (pmd)) ||pmd_present_invalid (pmd); -} +#define pmd_present (pmd) pte_present(pmd_pte(pmd))/* * THP definitions.
@@ -508,14 +511,7 @@ static inline int pmd_trans_huge(pmd_t pmd)#define pmd_mkclean (pmd) pte_pmd(pte_mkclean(pmd_pte(pmd)))#define pmd_mkdirty (pmd) pte_pmd(pte_mkdirty(pmd_pte(pmd)))#define pmd_mkyoung (pmd) pte_pmd(pte_mkyoung(pmd_pte(pmd))) - -static inline pmd_t pmd_mkinvalid (pmd_t pmd) -{ - pmd =set_pmd_bit (pmd,__pgprot (PMD_PRESENT_INVALID )); - pmd =clear_pmd_bit (pmd,__pgprot (PMD_SECT_VALID )); - -return pmd; -} +#define pmd_mkinvalid (pmd) pte_pmd(pte_mkinvalid(pmd_pte(pmd)))#define pmd_thp_or_huge (pmd) (pmd_huge(pmd) || pmd_trans_huge(pmd))
@@ -1027,7 +1023,7 @@ static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)* in MAIR_EL1. The mask below has to include PTE_ATTRINDX_MASK. */ const pteval_t mask =PTE_USER |PTE_PXN |PTE_UXN |PTE_RDONLY | -PTE_PROT_NONE |PTE_VALID |PTE_WRITE |PTE_GP | +PTE_INVALID |PTE_VALID |PTE_WRITE |PTE_GP |PTE_ATTRINDX_MASK ;/* preserve the hardware dirty information */ if (pte_hw_dirty (pte))
@@ -1076,17 +1072,17 @@ static inline int pgd_devmap(pgd_t pgd)#ifdef CONFIG_PAGE_TABLE_CHECK static inline bool pte_user_accessible_page (pte_t pte) { -return pte_present (pte) && (pte_user (pte) ||pte_user_exec (pte)); +return pte_valid (pte) && (pte_user (pte) ||pte_user_exec (pte)); }static inline bool pmd_user_accessible_page (pmd_t pmd) { -return pmd_leaf (pmd) && !pmd_present_invalid (pmd) && (pmd_user (pmd) ||pmd_user_exec (pmd)); +return pmd_valid (pmd) && !pmd_table (pmd) && (pmd_user (pmd) ||pmd_user_exec (pmd)); }static inline bool pud_user_accessible_page (pud_t pud) { -return pud_leaf (pud) && (pud_user (pud) ||pud_user_exec (pud)); +return pud_valid (pud) && !pud_table (pud) && (pud_user (pud) ||pud_user_exec (pud)); }#endif
@@ -1250,7 +1246,7 @@ static inline pmd_t pmdp_establish(struct vm_area_struct *vma,* bits 2: remember PG_anon_exclusive * bits 3-7: swap type * bits 8-57: swap offset -* bit 58: PTE_PROT_NONE (must be zero) +* bit 59: PTE_INVALID (must be zero) */ #define __SWP_TYPE_SHIFT 3#define __SWP_TYPE_BITS 5
Signature · 1 line
2.25.1
Re: [PATCH v2 1/3] arm64/mm: Refactor PMD_PRESENT_INVALID and PTE_PROT_NONE bits
Quoted 10 lines · Ryan Roberts
Currently the
PMD_PRESENT_INVALIDandPTE_PROT_NONEfunctionality explicitly occupy 2 bits in the PTE when PTE_VALID/PMD_SECT_VALID is clear. This has 2 significant consequences:-
PTE_PROT_NONEconsumes 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.
Quoted 7 lines
Note that while
PMD_PRESENT_INVALIDtechnically only applies to pmds, the swap pte layout is common to ptes and pmds so we are currently effectively reserving that bit at both levels.Let's replace
PMD_PRESENT_INVALIDwith a more genericPTE_INVALIDbit, which occupies the same position (bit 59) but applies uniformly to page/block descriptors at any level. This bit is only interpretted when
s/interpretted/interpreted/
Quoted 10 lines
PTE_VALIDis clear. 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 callpte_pfn(), etc). But crucially, the HW treats the pte as invalid and will fault if it hits.With this in place, we can remove
PTE_PROT_NONEentirely and instead representPROT_NONEas a present but invalid pte (PTE_VALID=0, PTE_INVALID=1) with PTE_USER=0 and PTE_UXN=1. This is a unique combination that is not used anywhere else.
Quoted 5 lines
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 a swap pte bit (bit 58 in both cases).
Signed-off-by Ryan Roberts <[email protected]>
Not an expert on all the details, but nothing jumped at me.
Signature · 3 lines
Cheers, David / dhildenb
Re: [PATCH v2 1/3] arm64/mm: Refactor PMD_PRESENT_INVALID and PTE_PROT_NONE bits
In reply to Ryan RobertsQuoted 7 lines · Ryan Roberts
arch/arm64/include/asm/pgtable-prot.h +0
-0@@ -18,14 +18,7 @@#define PTE_DIRTY (_AT(pteval_t, 1) << 55)#define PTE_SPECIAL (_AT(pteval_t, 1) << 56)
arch/arm64/include/asm/pgtable-prot.h line 21
Quoted 10 lines
arch/arm64/include/asm/pgtable-prot.h +1
-8#define PTE_DEVMAP (_AT(pteval_t, 1) << 57) -#define PTE_PROT_NONE (_AT(pteval_t, 1) << 58)/* only when !PTE_VALID */ - -/* -* This bit indicates that the entry is present i.e. pmd_page() -* still points to a valid huge page in memory even if the pmd -* has been invalidated. -*/ -#define PMD_PRESENT_INVALID (_AT(pteval_t, 1) << 59)/* only when !PMD_SECT_VALID */ +#define PTE_INVALID (_AT(pteval_t, 1) << 59)/* only when !PTE_VALID */
Nitpick - I prefer the PTE_PRESENT_INVALID name as it makes it clearer it's a present pte. We already have PTE_VALID, calling it PTE_INVALID looks like a negation only.
Quoted 14 lines
arch/arm64/include/asm/pgtable.h +1
-1@@ -105,7 +105,7 @@ static inline pteval_t __phys_to_pte_val(phys_addr_t phys)/* * The following only work if pte_present(). Undefined behaviour otherwise. */ -#define pte_present (pte) (!!(pte_val(pte) & (PTE_VALID | PTE_PROT_NONE))) +#define pte_present (pte) (pte_valid(pte) || pte_invalid(pte))#define pte_young (pte) (!!(pte_val(pte) & PTE_AF))#define pte_special (pte) (!!(pte_val(pte) & PTE_SPECIAL))#define pte_write (pte) (!!(pte_val(pte) & PTE_WRITE))
arch/arm64/include/asm/pgtable.h static inline pteval_t __phys_to_pte_val(phys_addr_t phys) · line 135
Quoted 4 lines
arch/arm64/include/asm/pgtable.h +1
-0@@ -132,6 +132,7 @@ static inline pteval_t __phys_to_pte_val(phys_addr_t phys)#define pte_dirty (pte) (pte_sw_dirty(pte) || pte_hw_dirty(pte))#define pte_valid (pte) (!!(pte_val(pte) & PTE_VALID)) +#define pte_invalid (pte) ((pte_val(pte) & (PTE_VALID | PTE_INVALID)) == PTE_INVALID)
Same argument as above, pte_invalid() looks confusing to me, better (to me) as pte_present_invalid().
I think it's sufficient to check PTE_PRESENT_INVALID only. We'd never have both bits set, so no need for mask and compare.
arch/arm64/include/asm/pgtable.h static inline pte_t pte_mkpresent(pte_t pte) · line 270
Quoted 13 lines
arch/arm64/include/asm/pgtable.h +6
-0/* * Execute-only user mappings do not have the PTE_USER bit set. All valid * kernel mappings have the PTE_UXN bit set. @@ -261,6 +262,13 @@ static inline pte_t pte_mkpresent(pte_t pte)return set_pte_bit (pte,__pgprot (PTE_VALID )); } +static inline pte_t pte_mkinvalid (pte_t pte) +{ + pte =set_pte_bit (pte,__pgprot (PTE_INVALID )); + pte =clear_pte_bit (pte,__pgprot (PTE_VALID )); +return pte; +}
I wonder whether we need to define this. I guess it makes sense than having the pmd_mkinvalid() use the PTE_* definitions directly, though it won't be something we need to do on a pte.
Quoted 53 lines
arch/arm64/include/asm/pgtable.h +5
-16+static inline pmd_t pmd_mkcont (pmd_t pmd) {return __pmd (pmd_val (pmd) |PMD_SECT_CONT );@@ -469,7 +477,7 @@ static inline pte_t pte_swp_clear_exclusive(pte_t pte)*/ static inline int pte_protnone (pte_t pte) { -return (pte_val (pte) & (PTE_VALID |PTE_PROT_NONE )) ==PTE_PROT_NONE ; +return pte_invalid (pte) && !pte_user (pte) && !pte_user_exec (pte); }static inline int pmd_protnone (pmd_t pmd)@@ -478,12 +486,7 @@ static inline int pmd_protnone(pmd_t pmd) }#endif -#define pmd_present_invalid (pmd) (!!(pmd_val(pmd) & PMD_PRESENT_INVALID)) - -static inline int pmd_present (pmd_t pmd) -{ -return pte_present (pmd_pte (pmd)) ||pmd_present_invalid (pmd); -} +#define pmd_present (pmd) pte_present(pmd_pte(pmd))/* * THP definitions. @@ -508,14 +511,7 @@ static inline int pmd_trans_huge(pmd_t pmd)#define pmd_mkclean (pmd) pte_pmd(pte_mkclean(pmd_pte(pmd)))#define pmd_mkdirty (pmd) pte_pmd(pte_mkdirty(pmd_pte(pmd)))#define pmd_mkyoung (pmd) pte_pmd(pte_mkyoung(pmd_pte(pmd))) - -static inline pmd_t pmd_mkinvalid (pmd_t pmd) -{ - pmd =set_pmd_bit (pmd,__pgprot (PMD_PRESENT_INVALID )); - pmd =clear_pmd_bit (pmd,__pgprot (PMD_SECT_VALID )); - -return pmd; -} +#define pmd_mkinvalid (pmd) pte_pmd(pte_mkinvalid(pmd_pte(pmd)))#define pmd_thp_or_huge (pmd) (pmd_huge(pmd) || pmd_trans_huge(pmd))@@ -1027,7 +1023,7 @@ static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)* in MAIR_EL1. The mask below has to include PTE_ATTRINDX_MASK. */ const pteval_t mask =PTE_USER |PTE_PXN |PTE_UXN |PTE_RDONLY | -PTE_PROT_NONE |PTE_VALID |PTE_WRITE |PTE_GP | +PTE_INVALID |PTE_VALID |PTE_WRITE |PTE_GP |PTE_ATTRINDX_MASK ;/* preserve the hardware dirty information */ if (pte_hw_dirty (pte))
arch/arm64/include/asm/pgtable.h static inline int pgd_devmap(pgd_t pgd) · line 1076
Quoted 6 lines
arch/arm64/include/asm/pgtable.h +1
-1@@ -1076,17 +1072,17 @@ static inline int pgd_devmap(pgd_t pgd)#ifdef CONFIG_PAGE_TABLE_CHECK static inline bool pte_user_accessible_page (pte_t pte) { -return pte_present (pte) && (pte_user (pte) ||pte_user_exec (pte)); +return pte_valid (pte) && (pte_user (pte) ||pte_user_exec (pte)); }
This looks fine, it doesn't change the semantics since PROT_NONE never had pte_user() or pte_user_exec().
arch/arm64/include/asm/pgtable.h static inline int pgd_devmap(pgd_t pgd) · line 1081
Quoted 5 lines
arch/arm64/include/asm/pgtable.h +1
-1static inline bool pmd_user_accessible_page (pmd_t pmd) { -return pmd_leaf (pmd) && !pmd_present_invalid (pmd) && (pmd_user (pmd) ||pmd_user_exec (pmd)); +return pmd_valid (pmd) && !pmd_table (pmd) && (pmd_user (pmd) ||pmd_user_exec (pmd)); }
Maybe our pmd_leaf() should actually check valid && !table instead of present and no need to change these.
arch/arm64/include/asm/pgtable.h static inline int pgd_devmap(pgd_t pgd) · line 1087
Quoted 6 lines
arch/arm64/include/asm/pgtable.h +1
-1static inline bool pud_user_accessible_page (pud_t pud) { -return pud_leaf (pud) && (pud_user (pud) ||pud_user_exec (pud)); +return pud_valid (pud) && !pud_table (pud) && (pud_user (pud) ||pud_user_exec (pud)); }#endif
Same here.
Otherwise I'm happy with the patch. Feel free to add:
As for getting it into 6.10, I'd say it's pretty late. Are there other dependencies on core kernel fixes?
Signature · 1 line
Catalin
Re: [PATCH v2 1/3] arm64/mm: Refactor PMD_PRESENT_INVALID and PTE_PROT_NONE bits
Quoted 14 lines · Catalin Marinas
Quoted 13 lines · Ryan Roberts
arch/arm64/include/asm/pgtable-prot.h +0
-5@@ -18,14 +18,7 @@#define PTE_DIRTY (_AT(pteval_t, 1) << 55)#define PTE_SPECIAL (_AT(pteval_t, 1) << 56)#define PTE_DEVMAP (_AT(pteval_t, 1) << 57) -#define PTE_PROT_NONE (_AT(pteval_t, 1) << 58)/* only when !PTE_VALID */ - -/* -* This bit indicates that the entry is present i.e. pmd_page() -* still points to a valid huge page in memory even if the pmd
Quoted 8 lines
arch/arm64/include/asm/pgtable-prot.h line 21
Quoted 4 lines
arch/arm64/include/asm/pgtable-prot.h +1
-3-* has been invalidated. -*/ -#define PMD_PRESENT_INVALID (_AT(pteval_t, 1) << 59)/* only when !PMD_SECT_VALID */ +#define PTE_INVALID (_AT(pteval_t, 1) << 59)/* only when !PTE_VALID */ Nitpick - I prefer the
PTE_PRESENT_INVALIDname as it makes it clearer it's a present pte. We already havePTE_VALID, calling itPTE_INVALIDlooks like a negation only.
Meh, for me the pte can only be valid or invalid if it is present. So it's implicit. And if you have PTE_PRESENT_INVALID you should also have PTE_PRESENT_VALID.
We also have pte_mkinvalid(), which is core-mm-defined. In your scheme, surely it should be pte_mkpresent_invalid()?
But you're the boss, I'll change this to PTE_PRESENT_INVALID. :-(
Quoted 17 lines
Quoted 17 lines
arch/arm64/include/asm/pgtable.h +1
-1@@ -105,7 +105,7 @@ static inline pteval_t __phys_to_pte_val(phys_addr_t phys)/* * The following only work if pte_present(). Undefined behaviour otherwise. */ -#define pte_present (pte) (!!(pte_val(pte) & (PTE_VALID | PTE_PROT_NONE))) +#define pte_present (pte) (pte_valid(pte) || pte_invalid(pte))#define pte_young (pte) (!!(pte_val(pte) & PTE_AF))#define pte_special (pte) (!!(pte_val(pte) & PTE_SPECIAL))#define pte_write (pte) (!!(pte_val(pte) & PTE_WRITE))@@ -132,6 +132,7 @@ static inline pteval_t __phys_to_pte_val(phys_addr_t phys)#define pte_dirty (pte) (pte_sw_dirty(pte) || pte_hw_dirty(pte))
Quoted 5 lines
arch/arm64/include/asm/pgtable.h static inline pteval_t __phys_to_pte_val(phys_addr_t phys) · line 135
Quoted 2 lines
arch/arm64/include/asm/pgtable.h +1
-0#define pte_valid (pte) (!!(pte_val(pte) & PTE_VALID)) +#define pte_invalid (pte) ((pte_val(pte) & (PTE_VALID | PTE_INVALID)) == PTE_INVALID)Same argument as above,
pte_invalid()looks confusing to me, better (to me) aspte_present_invalid().
OK. Consider it done.
Quoted 3 lines
I think it's sufficient to check
PTE_PRESENT_INVALIDonly. We'd never have both bits set, so no need for mask and compare.
My rationale is that the INVALID bit may have some other HW meaning when PTE_VALID is set, so its not correct to interpret it as INVALID unless VALID is clear. Granted bit 59 is AttrIndex[3] or PBHA[0], neither of which we are using currently so it will always be 0 when PTE_VALID=1 (and same argument when its moved to NS in next patch). But it feels fragile to me. I'd rather leave it as is unless you insist.
Quoted 8 lines
Quoted 8 lines
arch/arm64/include/asm/pgtable.h +0
-0/* * Execute-only user mappings do not have the PTE_USER bit set. All valid * kernel mappings have the PTE_UXN bit set. @@ -261,6 +262,13 @@ static inline pte_t pte_mkpresent(pte_t pte)return set_pte_bit (pte,__pgprot (PTE_VALID )); }
Quoted 10 lines
arch/arm64/include/asm/pgtable.h static inline pte_t pte_mkpresent(pte_t pte) · line 270
Quoted 6 lines
arch/arm64/include/asm/pgtable.h +6
-0+static inline pte_t pte_mkinvalid (pte_t pte) +{ + pte =set_pte_bit (pte,__pgprot (PTE_INVALID )); + pte =clear_pte_bit (pte,__pgprot (PTE_VALID )); +return pte; +}I wonder whether we need to define this. I guess it makes sense than having the
pmd_mkinvalid()use the PTE_* definitions directly, though it won't be something we need to do on a pte.
For me its much cleaner to do it as is because it makes it clear that the format is the same across pte, pmd and pud. And we need pte_invalid() (or pte_present_invalid()) for PROT_NONE so isn't it better to match it with a setter?
Quoted 64 lines
Quoted 54 lines
arch/arm64/include/asm/pgtable.h +5
-16+static inline pmd_t pmd_mkcont (pmd_t pmd) {return __pmd (pmd_val (pmd) |PMD_SECT_CONT );@@ -469,7 +477,7 @@ static inline pte_t pte_swp_clear_exclusive(pte_t pte)*/ static inline int pte_protnone (pte_t pte) { -return (pte_val (pte) & (PTE_VALID |PTE_PROT_NONE )) ==PTE_PROT_NONE ; +return pte_invalid (pte) && !pte_user (pte) && !pte_user_exec (pte); }static inline int pmd_protnone (pmd_t pmd)@@ -478,12 +486,7 @@ static inline int pmd_protnone(pmd_t pmd) }#endif -#define pmd_present_invalid (pmd) (!!(pmd_val(pmd) & PMD_PRESENT_INVALID)) - -static inline int pmd_present (pmd_t pmd) -{ -return pte_present (pmd_pte (pmd)) ||pmd_present_invalid (pmd); -} +#define pmd_present (pmd) pte_present(pmd_pte(pmd))/* * THP definitions. @@ -508,14 +511,7 @@ static inline int pmd_trans_huge(pmd_t pmd)#define pmd_mkclean (pmd) pte_pmd(pte_mkclean(pmd_pte(pmd)))#define pmd_mkdirty (pmd) pte_pmd(pte_mkdirty(pmd_pte(pmd)))#define pmd_mkyoung (pmd) pte_pmd(pte_mkyoung(pmd_pte(pmd))) - -static inline pmd_t pmd_mkinvalid (pmd_t pmd) -{ - pmd =set_pmd_bit (pmd,__pgprot (PMD_PRESENT_INVALID )); - pmd =clear_pmd_bit (pmd,__pgprot (PMD_SECT_VALID )); - -return pmd; -} +#define pmd_mkinvalid (pmd) pte_pmd(pte_mkinvalid(pmd_pte(pmd)))#define pmd_thp_or_huge (pmd) (pmd_huge(pmd) || pmd_trans_huge(pmd))@@ -1027,7 +1023,7 @@ static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)* in MAIR_EL1. The mask below has to include PTE_ATTRINDX_MASK. */ const pteval_t mask =PTE_USER |PTE_PXN |PTE_UXN |PTE_RDONLY | -PTE_PROT_NONE |PTE_VALID |PTE_WRITE |PTE_GP | +PTE_INVALID |PTE_VALID |PTE_WRITE |PTE_GP |PTE_ATTRINDX_MASK ;/* preserve the hardware dirty information */ if (pte_hw_dirty (pte))arch/arm64/include/asm/pgtable.h static inline int pgd_devmap(pgd_t pgd) · line 1076
Quoted 6 lines
arch/arm64/include/asm/pgtable.h +1
-1@@ -1076,17 +1072,17 @@ static inline int pgd_devmap(pgd_t pgd)#ifdef CONFIG_PAGE_TABLE_CHECK static inline bool pte_user_accessible_page (pte_t pte) { -return pte_present (pte) && (pte_user (pte) ||pte_user_exec (pte)); +return pte_valid (pte) && (pte_user (pte) ||pte_user_exec (pte)); }This looks fine, it doesn't change the semantics since
PROT_NONEnever hadpte_user()orpte_user_exec().
Quoted 8 lines
arch/arm64/include/asm/pgtable.h static inline int pgd_devmap(pgd_t pgd) · line 1081
Quoted 5 lines
arch/arm64/include/asm/pgtable.h +1
-1static inline bool pmd_user_accessible_page (pmd_t pmd) { -return pmd_leaf (pmd) && !pmd_present_invalid (pmd) && (pmd_user (pmd) ||pmd_user_exec (pmd)); +return pmd_valid (pmd) && !pmd_table (pmd) && (pmd_user (pmd) ||pmd_user_exec (pmd)); }Maybe our
pmd_leaf()should actually check valid && !table instead of present and no need to change these.
I'm not sure that would be a great approach; pmd_leaf() is core-mm-defined. And I can't imagine core-mm would want pmd_leaf() to start returning false after calling pmd_mkinvalid(). You probably won't find anywhere where it actually matters right now, but it would be subtly broken and could be exposed in future.
Quoted 10 lines
arch/arm64/include/asm/pgtable.h static inline int pgd_devmap(pgd_t pgd) · line 1087
Quoted 7 lines
arch/arm64/include/asm/pgtable.h +1
-1static inline bool pud_user_accessible_page (pud_t pud) { -return pud_leaf (pud) && (pud_user (pud) ||pud_user_exec (pud)); +return pud_valid (pud) && !pud_table (pud) && (pud_user (pud) ||pud_user_exec (pud)); }#endif Same here.
Quoted 6 lines
Otherwise I'm happy with the patch. Feel free to add:
Reviewed-by Catalin Marinas <[email protected]>As for getting it into 6.10, I'd say it's pretty late. Are there other dependencies on core kernel fixes?
Yes one fix that this depends on - Andrew has just taken the fix into mm-hotfixes-unstable. So I think that will get into v6.9 all being well? I'm only pushing because I'd prefer to have it off my desk before the baby comes (14th May). Realistically it can wait.
Catalin Marinas
maintainerRe: [PATCH v2 1/3] arm64/mm: Refactor PMD_PRESENT_INVALID and PTE_PROT_NONE bits
Quoted 24 lines · Ryan Roberts
Quoted 14 lines · Catalin Marinas
Quoted 13 lines · Ryan Roberts
arch/arm64/include/asm/pgtable-prot.h +0
-5@@ -18,14 +18,7 @@#define PTE_DIRTY (_AT(pteval_t, 1) << 55)#define PTE_SPECIAL (_AT(pteval_t, 1) << 56)#define PTE_DEVMAP (_AT(pteval_t, 1) << 57) -#define PTE_PROT_NONE (_AT(pteval_t, 1) << 58)/* only when !PTE_VALID */ - -/* -* This bit indicates that the entry is present i.e. pmd_page() -* still points to a valid huge page in memory even if the pmd Quoted 8 lines
arch/arm64/include/asm/pgtable-prot.h line 21
Quoted 4 lines
arch/arm64/include/asm/pgtable-prot.h +1
-3-* has been invalidated. -*/ -#define PMD_PRESENT_INVALID (_AT(pteval_t, 1) << 59)/* only when !PMD_SECT_VALID */ +#define PTE_INVALID (_AT(pteval_t, 1) << 59)/* only when !PTE_VALID */ Nitpick - I prefer the
PTE_PRESENT_INVALIDname as it makes it clearer it's a present pte. We already havePTE_VALID, calling itPTE_INVALIDlooks like a negation only.
Quoted 8 lines
Meh, for me the pte can only be valid or invalid if it is present. So it's implicit. And if you have
PTE_PRESENT_INVALIDyou should also havePTE_PRESENT_VALID.We also have
pte_mkinvalid(), which is core-mm-defined. In your scheme, surely it should bepte_mkpresent_invalid()?But you're the boss, I'll change this to
PTE_PRESENT_INVALID. :-(
TBH, I don't have a strong opinion but best to avoid the bikeshedding. I'll leave the decision to you ;). It would match the pmd_mkinvalid() core code. But if you drop 'present' make sure you add a comment above that it's meant for present ptes.
Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
arch/arm64/include/asm/pgtable.h +2
-1@@ -105,7 +105,7 @@ static inline pteval_t __phys_to_pte_val(phys_addr_t phys)/* * The following only work if pte_present(). Undefined behaviour otherwise. */ -#define pte_present (pte) (!!(pte_val(pte) & (PTE_VALID | PTE_PROT_NONE))) +#define pte_present (pte) (pte_valid(pte) || pte_invalid(pte))#define pte_young (pte) (!!(pte_val(pte) & PTE_AF))#define pte_special (pte) (!!(pte_val(pte) & PTE_SPECIAL))#define pte_write (pte) (!!(pte_val(pte) & PTE_WRITE))@@ -132,6 +132,7 @@ static inline pteval_t __phys_to_pte_val(phys_addr_t phys)#define pte_dirty (pte) (pte_sw_dirty(pte) || pte_hw_dirty(pte))#define pte_valid (pte) (!!(pte_val(pte) & PTE_VALID)) +#define pte_invalid (pte) ((pte_val(pte) & (PTE_VALID | PTE_INVALID)) == PTE_INVALID)
[...]
Quoted 9 lines
Quoted 2 lines
I think it's sufficient to check
PTE_PRESENT_INVALIDonly. We'd never have both bits set, so no need for mask and compare.My rationale is that the INVALID bit may have some other HW meaning when
PTE_VALIDis set, so its not correct to interpret it as INVALID unless VALID is clear. Granted bit 59 is AttrIndex[3] or PBHA[0], neither of which we are using currently so it will always be 0 when PTE_VALID=1 (and same argument when its moved to NS in next patch). But it feels fragile to me. I'd rather leave it as is unless you insist.
You are right. It currently works but it may overlap with some hardware bit on valid ptes.
Quoted 18 lines
Quoted 7 lines
Quoted 7 lines
/*
* Execute-only user mappings do not have thePTE_USERbit set. All valid
* kernel mappings have thePTE_UXNbit set.arch/arm64/include/asm/pgtable.h +0
-0@@ -261,6 +262,13 @@ static inline pte_t pte_mkpresent(pte_t pte)return set_pte_bit (pte,__pgprot (PTE_VALID )); }Quoted 10 lines
arch/arm64/include/asm/pgtable.h static inline pte_t pte_mkpresent(pte_t pte) · line 270
Quoted 6 lines
arch/arm64/include/asm/pgtable.h +6
-0+static inline pte_t pte_mkinvalid (pte_t pte) +{ + pte =set_pte_bit (pte,__pgprot (PTE_INVALID )); + pte =clear_pte_bit (pte,__pgprot (PTE_VALID )); +return pte; +}I wonder whether we need to define this. I guess it makes sense than having the
pmd_mkinvalid()use the PTE_* definitions directly, though it won't be something we need to do on a pte.
Quoted 3 lines
For me its much cleaner to do it as is because it makes it clear that the format is the same across pte, pmd and pud. And we need
pte_invalid()(orpte_present_invalid()) forPROT_NONEso isn't it better to match it with a setter?
I agree. It was just a remark above.
Quoted 9 lines
Quoted 8 lines
arch/arm64/include/asm/pgtable.h static inline int pgd_devmap(pgd_t pgd) · line 1081
Quoted 5 lines
arch/arm64/include/asm/pgtable.h +1
-1static inline bool pmd_user_accessible_page (pmd_t pmd) { -return pmd_leaf (pmd) && !pmd_present_invalid (pmd) && (pmd_user (pmd) ||pmd_user_exec (pmd)); +return pmd_valid (pmd) && !pmd_table (pmd) && (pmd_user (pmd) ||pmd_user_exec (pmd)); }Maybe our
pmd_leaf()should actually check valid && !table instead of present and no need to change these.
Quoted 4 lines
I'm not sure that would be a great approach;
pmd_leaf()is core-mm-defined. And I can't imagine core-mm would wantpmd_leaf()to start returning false after callingpmd_mkinvalid(). You probably won't find anywhere where it actually matters right now, but it would be subtly broken and could be exposed in future.
True, I think it's fine currently but you never know. So after pmd_mkinvalid() on a leaf pmd, pmd_leaf() should still return true. It might be worth adding a test to pmd_thp_tests() in debug_vm_pgtable.c.
Quoted 9 lines
Quoted 9 lines
arch/arm64/include/asm/pgtable.h static inline int pgd_devmap(pgd_t pgd) · line 1087
Quoted 6 lines
static inline bool
pud_user_accessible_page(pud_t pud){arch/arm64/include/asm/pgtable.h +1
-1- return pud_leaf(pud) && (pud_user(pud) || pud_user_exec(pud)); + return pud_valid(pud) && !pud_table(pud) && (pud_user(pud) || pud_user_exec(pud)); }#endif Same here.
Quoted 3 lines
Quoted 3 lines
Otherwise I'm happy with the patch. Feel free to add:
Reviewed-by Catalin Marinas <[email protected]>
My reviewed-by tag still stands even if you leave the patch as is.
Thanks.
Signature · 1 line
Catalin
Re: [PATCH v2 1/3] arm64/mm: Refactor PMD_PRESENT_INVALID and PTE_PROT_NONE bits
Quoted 32 lines · Catalin Marinas
Quoted 31 lines · Ryan Roberts
Quoted 14 lines · Catalin Marinas
Quoted 13 lines · Ryan Roberts
arch/arm64/include/asm/pgtable-prot.h +0
-5@@ -18,14 +18,7 @@#define PTE_DIRTY (_AT(pteval_t, 1) << 55)#define PTE_SPECIAL (_AT(pteval_t, 1) << 56)#define PTE_DEVMAP (_AT(pteval_t, 1) << 57) -#define PTE_PROT_NONE (_AT(pteval_t, 1) << 58)/* only when !PTE_VALID */ - -/* -* This bit indicates that the entry is present i.e. pmd_page() -* still points to a valid huge page in memory even if the pmd Quoted 8 lines
arch/arm64/include/asm/pgtable-prot.h line 21
Quoted 4 lines
arch/arm64/include/asm/pgtable-prot.h +1
-3-* has been invalidated. -*/ -#define PMD_PRESENT_INVALID (_AT(pteval_t, 1) << 59)/* only when !PMD_SECT_VALID */ +#define PTE_INVALID (_AT(pteval_t, 1) << 59)/* only when !PTE_VALID */ Nitpick - I prefer the
PTE_PRESENT_INVALIDname as it makes it clearer it's a present pte. We already havePTE_VALID, calling itPTE_INVALIDlooks like a negation only.Meh, for me the pte can only be valid or invalid if it is present. So it's implicit. And if you have
PTE_PRESENT_INVALIDyou should also havePTE_PRESENT_VALID.We also have
pte_mkinvalid(), which is core-mm-defined. In your scheme, surely it should bepte_mkpresent_invalid()?
Quoted 6 lines
Quoted 1 line
But you're the boss, I'll change this to
PTE_PRESENT_INVALID. :-(TBH, I don't have a strong opinion but best to avoid the bikeshedding. I'll leave the decision to you ;). It would match the
pmd_mkinvalid()core code. But if you drop 'present' make sure you add a comment above that it's meant for present ptes.
OK, thanks - I'll add a comment and leave it as pte_invalid().
Quoted 30 lines
Quoted 19 lines
Quoted 19 lines
Quoted 19 lines
arch/arm64/include/asm/pgtable.h +2
-1@@ -105,7 +105,7 @@ static inline pteval_t __phys_to_pte_val(phys_addr_t phys)/* * The following only work if pte_present(). Undefined behaviour otherwise. */ -#define pte_present (pte) (!!(pte_val(pte) & (PTE_VALID | PTE_PROT_NONE))) +#define pte_present (pte) (pte_valid(pte) || pte_invalid(pte))#define pte_young (pte) (!!(pte_val(pte) & PTE_AF))#define pte_special (pte) (!!(pte_val(pte) & PTE_SPECIAL))#define pte_write (pte) (!!(pte_val(pte) & PTE_WRITE))@@ -132,6 +132,7 @@ static inline pteval_t __phys_to_pte_val(phys_addr_t phys)#define pte_dirty (pte) (pte_sw_dirty(pte) || pte_hw_dirty(pte))#define pte_valid (pte) (!!(pte_val(pte) & PTE_VALID)) +#define pte_invalid (pte) ((pte_val(pte) & (PTE_VALID | PTE_INVALID)) == PTE_INVALID)[...]
Quoted 9 lines
Quoted 2 lines
I think it's sufficient to check
PTE_PRESENT_INVALIDonly. We'd never have both bits set, so no need for mask and compare.My rationale is that the INVALID bit may have some other HW meaning when
PTE_VALIDis set, so its not correct to interpret it as INVALID unless VALID is clear. Granted bit 59 is AttrIndex[3] or PBHA[0], neither of which we are using currently so it will always be 0 when PTE_VALID=1 (and same argument when its moved to NS in next patch). But it feels fragile to me. I'd rather leave it as is unless you insist.
Quoted 2 lines
You are right. It currently works but it may overlap with some hardware bit on valid ptes.
OK, I'll leave it as is.
Quoted 19 lines
Quoted 19 lines
Quoted 18 lines
Quoted 5 lines
/*
* Execute-only user mappings do not have thePTE_USERbit set. All valid
* kernel mappings have thePTE_UXNbit set.arch/arm64/include/asm/pgtable.h static inline pte_t pte_mkpresent(pte_t pte) · line 270
Quoted 9 lines
arch/arm64/include/asm/pgtable.h +6
-0@@ -261,6 +262,13 @@ static inline pte_t pte_mkpresent(pte_t pte)return set_pte_bit (pte,__pgprot (PTE_VALID )); } +static inline pte_t pte_mkinvalid (pte_t pte) +{ + pte =set_pte_bit (pte,__pgprot (PTE_INVALID )); + pte =clear_pte_bit (pte,__pgprot (PTE_VALID )); +return pte; +}I wonder whether we need to define this. I guess it makes sense than having the
pmd_mkinvalid()use the PTE_* definitions directly, though it won't be something we need to do on a pte.
Quoted 5 lines
Quoted 3 lines
For me its much cleaner to do it as is because it makes it clear that the format is the same across pte, pmd and pud. And we need
pte_invalid()(orpte_present_invalid()) forPROT_NONEso isn't it better to match it with a setter?I agree. It was just a remark above.
ACK.
Quoted 10 lines
Quoted 10 lines
Quoted 9 lines
arch/arm64/include/asm/pgtable.h static inline int pgd_devmap(pgd_t pgd) · line 1081
Quoted 6 lines
arch/arm64/include/asm/pgtable.h +1
-1static inline bool pmd_user_accessible_page (pmd_t pmd) { -return pmd_leaf (pmd) && !pmd_present_invalid (pmd) && (pmd_user (pmd) ||pmd_user_exec (pmd)); +return pmd_valid (pmd) && !pmd_table (pmd) && (pmd_user (pmd) ||pmd_user_exec (pmd)); }Maybe our
pmd_leaf()should actually check valid && !table instead of present and no need to change these.
Quoted 8 lines
Quoted 4 lines
I'm not sure that would be a great approach;
pmd_leaf()is core-mm-defined. And I can't imagine core-mm would wantpmd_leaf()to start returning false after callingpmd_mkinvalid(). You probably won't find anywhere where it actually matters right now, but it would be subtly broken and could be exposed in future.True, I think it's fine currently but you never know. So after
pmd_mkinvalid()on a leaf pmd,pmd_leaf()should still return true. It might be worth adding a test topmd_thp_tests()indebug_vm_pgtable.c.
Good idea; I'll add tests for this.
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
arch/arm64/include/asm/pgtable.h static inline int pgd_devmap(pgd_t pgd) · line 1087
Quoted 7 lines
static inline bool
pud_user_accessible_page(pud_t pud){arch/arm64/include/asm/pgtable.h +1
-1- return pud_leaf(pud) && (pud_user(pud) || pud_user_exec(pud)); + return pud_valid(pud) && !pud_table(pud) && (pud_user(pud) || pud_user_exec(pud)); }#endif Same here.
Quoted 5 lines
Quoted 3 lines
Quoted 3 lines
Otherwise I'm happy with the patch. Feel free to add:
Reviewed-by Catalin Marinas <[email protected]>My reviewed-by tag still stands even if you leave the patch as is.
Thanks.
There is still one problem I need to resolve; During this work I discovered that core-mm can call pmd_mkinvalid() for swap pmds. On arm64 this will turn the swap pmd into a present pmd, and BadThings can happen in GUP-fast (and any other lockless SW table walkers). My original fix modified core-mm to only call pmd_mkinvalid() for present pmds. But discussion over there has shown that arm64 is the only arch that cannot handle this. So I've been convinced that it's probably more robust to make arm64 handle it gracefully and add tests to debug_vm_pgtable.c to check for this. Patch incoming shortly, but it will cause a conflict with this series. So I'll send a v2 of this once that fix is accepted.
Quoted 3 lines
Thanks.
Catalin Marinas
maintainerRe: [PATCH v2 1/3] arm64/mm: Refactor PMD_PRESENT_INVALID and PTE_PROT_NONE bits
Quoted 9 lines · Ryan Roberts
There is still one problem I need to resolve; During this work I discovered that core-mm can call
pmd_mkinvalid()for swap pmds. On arm64 this will turn the swap pmd into a present pmd, and BadThings can happen in GUP-fast (and any other lockless SW table walkers). My original fix modified core-mm to only callpmd_mkinvalid()for present pmds. But discussion over there has shown that arm64 is the only arch that cannot handle this. So I've been convinced that it's probably more robust to make arm64 handle it gracefully and add tests todebug_vm_pgtable.cto check for this. Patch incoming shortly, but it will cause a conflict with this series. So I'll send a v2 of this once that fix is accepted.
Sounds fine. I can queue the arm64 pmd_mkinvalid() fix for 6.9 and you can base this series on top. But I have a preference for this patchset to sit in -next for a bit anyway, so it might be 6.11 material.
Signature · 1 line
Catalin
Re: [PATCH v2 1/3] arm64/mm: Refactor PMD_PRESENT_INVALID and PTE_PROT_NONE bits
Quoted 11 lines · Catalin Marinas
Quoted 9 lines · Ryan Roberts
There is still one problem I need to resolve; During this work I discovered that core-mm can call
pmd_mkinvalid()for swap pmds. On arm64 this will turn the swap pmd into a present pmd, and BadThings can happen in GUP-fast (and any other lockless SW table walkers). My original fix modified core-mm to only callpmd_mkinvalid()for present pmds. But discussion over there has shown that arm64 is the only arch that cannot handle this. So I've been convinced that it's probably more robust to make arm64 handle it gracefully and add tests todebug_vm_pgtable.cto check for this. Patch incoming shortly, but it will cause a conflict with this series. So I'll send a v2 of this once that fix is accepted.
Quoted 3 lines
Sounds fine. I can queue the arm64
pmd_mkinvalid()fix for 6.9 and you can base this series on top. But I have a preference for this patchset to sit in -next for a bit anyway, so it might be 6.11 material.
Yeah that works for me. I just sent the pmd_mkinvalid() fix.
Re: [PATCH v2 1/3] arm64/mm: Refactor PMD_PRESENT_INVALID and PTE_PROT_NONE bits
In reply to Catalin MarinasQuoted 32 lines · Catalin Marinas
Quoted 31 lines · Ryan Roberts
Quoted 14 lines · Catalin Marinas
Quoted 13 lines · Ryan Roberts
arch/arm64/include/asm/pgtable-prot.h +0
-5@@ -18,14 +18,7 @@#define PTE_DIRTY (_AT(pteval_t, 1) << 55)#define PTE_SPECIAL (_AT(pteval_t, 1) << 56)#define PTE_DEVMAP (_AT(pteval_t, 1) << 57) -#define PTE_PROT_NONE (_AT(pteval_t, 1) << 58)/* only when !PTE_VALID */ - -/* -* This bit indicates that the entry is present i.e. pmd_page() -* still points to a valid huge page in memory even if the pmd Quoted 8 lines
arch/arm64/include/asm/pgtable-prot.h line 21
Quoted 4 lines
arch/arm64/include/asm/pgtable-prot.h +1
-3-* has been invalidated. -*/ -#define PMD_PRESENT_INVALID (_AT(pteval_t, 1) << 59)/* only when !PMD_SECT_VALID */ +#define PTE_INVALID (_AT(pteval_t, 1) << 59)/* only when !PTE_VALID */ Nitpick - I prefer the
PTE_PRESENT_INVALIDname as it makes it clearer it's a present pte. We already havePTE_VALID, calling itPTE_INVALIDlooks like a negation only.Meh, for me the pte can only be valid or invalid if it is present. So it's implicit. And if you have
PTE_PRESENT_INVALIDyou should also havePTE_PRESENT_VALID.We also have
pte_mkinvalid(), which is core-mm-defined. In your scheme, surely it should bepte_mkpresent_invalid()?
Quoted 6 lines
Quoted 1 line
But you're the boss, I'll change this to
PTE_PRESENT_INVALID. :-(TBH, I don't have a strong opinion but best to avoid the bikeshedding. I'll leave the decision to you ;). It would match the
pmd_mkinvalid()core code. But if you drop 'present' make sure you add a comment above that it's meant for present ptes.
FWIW, I was confused by
present = valid | invalid
Something like
present = present_valid | present_invalid
would be more obvious at least to me ;)
Signature · 3 lines
Cheers, David / dhildenb
Re: [PATCH v2 1/3] arm64/mm: Refactor PMD_PRESENT_INVALID and PTE_PROT_NONE bits
Quoted 39 lines · David Hildenbrand
Quoted 38 lines · Catalin Marinas
Quoted 36 lines · Ryan Roberts
Quoted 18 lines · Catalin Marinas
Quoted 17 lines · Ryan Roberts
arch/arm64/include/asm/pgtable-prot.h +0
-1@@ -18,14 +18,7 @@ #define PTE_DIRTY (_AT(pteval_t, 1) << 55) #define PTE_SPECIAL (_AT(pteval_t, 1) << 56) #define PTE_DEVMAP (_AT(pteval_t, 1) << 57) -#define PTE_PROT_NONE (_AT(pteval_t, 1) << 58)/* only when !PTE_VALID */
arch/arm64/include/asm/pgtable-prot.h +0
-6- -/* -* This bit indicates that the entry is present i.e. pmd_page() -* still points to a valid huge page in memory even if the pmd -* has been invalidated. -*/ Quoted 8 lines
Quoted 4 lines
-#define PMD_PRESENT_INVALID (
_AT(pteval_t, 1)<< 59) /* only when !PMD_SECT_VALID */
+#define PTE_INVALID (_AT(pteval_t, 1)<< 59) /* only when !PTE_VALID */Nitpick - I prefer the
PTE_PRESENT_INVALIDname as it makes it clearer it's a present pte. We already havePTE_VALID, calling itPTE_INVALIDlooks like a negation only.Meh, for me the pte can only be valid or invalid if it is present. So it's implicit. And if you have
PTE_PRESENT_INVALIDyou should also havePTE_PRESENT_VALID.We also have
pte_mkinvalid(), which is core-mm-defined. In your scheme, surely it should bepte_mkpresent_invalid()?But you're the boss, I'll change this to
PTE_PRESENT_INVALID. :-(
Quoted 8 lines
Quoted 4 lines
TBH, I don't have a strong opinion but best to avoid the bikeshedding. I'll leave the decision to you ;). It would match the
pmd_mkinvalid()core code. But if you drop 'present' make sure you add a comment above that it's meant for present ptes.FWIW, I was confused by
present = valid | invalid
OK fair enough.
Quoted 4 lines
Something like
present =
present_valid|present_invalid
I don't want to change pte_valid() to pte_present_valid(); that would also be a fair bit of churn.
I'll take Catalin's suggestion and make this PTE_PRESENT_INVALID and pte_present_invalid(). And obviously leave pmd_mkinvalid() as it is. (Conversation in the other thread has concluded that it's ok to invalidate a non-present pmd afterall).
Quoted 3 lines
would be more obvious at least to me ;)
Re: [PATCH v2 1/3] arm64/mm: Refactor PMD_PRESENT_INVALID and PTE_PROT_NONE bits
Quoted 52 lines · Ryan Roberts
Quoted 39 lines · David Hildenbrand
Quoted 38 lines · Catalin Marinas
Quoted 36 lines · Ryan Roberts
Quoted 18 lines · Catalin Marinas
Quoted 17 lines · Ryan Roberts
arch/arm64/include/asm/pgtable-prot.h +0
-1@@ -18,14 +18,7 @@ #define PTE_DIRTY (_AT(pteval_t, 1) << 55) #define PTE_SPECIAL (_AT(pteval_t, 1) << 56) #define PTE_DEVMAP (_AT(pteval_t, 1) << 57) -#define PTE_PROT_NONE (_AT(pteval_t, 1) << 58)/* only when !PTE_VALID */
arch/arm64/include/asm/pgtable-prot.h +0
-6- -/* -* This bit indicates that the entry is present i.e. pmd_page() -* still points to a valid huge page in memory even if the pmd -* has been invalidated. -*/ Quoted 8 lines
Quoted 4 lines
-#define PMD_PRESENT_INVALID (
_AT(pteval_t, 1)<< 59) /* only when !PMD_SECT_VALID */
+#define PTE_INVALID (_AT(pteval_t, 1)<< 59) /* only when !PTE_VALID */Nitpick - I prefer the
PTE_PRESENT_INVALIDname as it makes it clearer it's a present pte. We already havePTE_VALID, calling itPTE_INVALIDlooks like a negation only.Meh, for me the pte can only be valid or invalid if it is present. So it's implicit. And if you have
PTE_PRESENT_INVALIDyou should also havePTE_PRESENT_VALID.We also have
pte_mkinvalid(), which is core-mm-defined. In your scheme, surely it should bepte_mkpresent_invalid()?But you're the boss, I'll change this to
PTE_PRESENT_INVALID. :-(Quoted 8 lines
Quoted 4 lines
TBH, I don't have a strong opinion but best to avoid the bikeshedding. I'll leave the decision to you ;). It would match the
pmd_mkinvalid()core code. But if you drop 'present' make sure you add a comment above that it's meant for present ptes.FWIW, I was confused by
present = valid | invalid
OK fair enough.
Quoted 1 line
Quoted 6 lines
Quoted 3 lines
Something like
present =
present_valid|present_invalidI don't want to change
pte_valid()topte_present_valid(); that would also be a fair bit of churn.
Yes.
Quoted 5 lines
I'll take Catalin's suggestion and make this
PTE_PRESENT_INVALIDandpte_present_invalid(). And obviously leavepmd_mkinvalid()as it is. (Conversation in the other thread has concluded that it's ok to invalidate a non-present pmd afterall).
Works for me.
Signature · 3 lines
Cheers, David / dhildenb
Will Deacon
maintainerRe: [PATCH v2 1/3] arm64/mm: Refactor PMD_PRESENT_INVALID and PTE_PROT_NONE bits
In reply to Ryan RobertsHey Ryan,
Just a couple of comments on this:
Quoted 7 lines · Ryan Roberts
arch/arm64/include/asm/pgtable-prot.h +0
-0@@ -18,14 +18,7 @@#define PTE_DIRTY (_AT(pteval_t, 1) << 55)#define PTE_SPECIAL (_AT(pteval_t, 1) << 56)
arch/arm64/include/asm/pgtable-prot.h line 21
Quoted 10 lines
arch/arm64/include/asm/pgtable-prot.h +1
-8#define PTE_DEVMAP (_AT(pteval_t, 1) << 57) -#define PTE_PROT_NONE (_AT(pteval_t, 1) << 58)/* only when !PTE_VALID */ - -/* -* This bit indicates that the entry is present i.e. pmd_page() -* still points to a valid huge page in memory even if the pmd -* has been invalidated. -*/ -#define PMD_PRESENT_INVALID (_AT(pteval_t, 1) << 59)/* only when !PMD_SECT_VALID */ +#define PTE_INVALID (_AT(pteval_t, 1) << 59)/* only when !PTE_VALID */
So this now overlaps with AttrIndx[3] if FEAT_AIE is implemented. Although this shouldn't matter on the face of things because it's only used for invalid entries, we originally moved the PROT_NONE bit from 2 to 57 back in 3676f9ef5481 ("arm64: Move PTE_PROT_NONE higher up") because it was possible to change the memory type for PROT_NONE mappings via some drivers.
Moving the field to the NS bit (as you do later in the series) resolves this, but the architecture currently says that the NS bit is RES0. How can we guarantee that it won't be repurposed by hardware in future?
Quoted 47 lines
arch/arm64/include/asm/pgtable-prot.h +1
-1#define _PROT_DEFAULT (PTE_TYPE_PAGE | PTE_AF | PTE_SHARED)#define _PROT_SECT_DEFAULT (PMD_TYPE_SECT | PMD_SECT_AF | PMD_SECT_S)@@ -103,7 +96,7 @@ static inline bool __pure lpa2_is_enabled(void) __val; \ }) -#define PAGE_NONE __pgprot(((_PAGE_DEFAULT) & ~PTE_VALID) | PTE_PROT_NONE | PTE_RDONLY | PTE_NG | PTE_PXN | PTE_UXN) +#define PAGE_NONE __pgprot(((_PAGE_DEFAULT) & ~PTE_VALID) | PTE_INVALID | PTE_RDONLY | PTE_NG | PTE_PXN | PTE_UXN)/* shared+writable pages are clean by default, hence PTE_RDONLY|PTE_WRITE */ #define PAGE_SHARED __pgprot(_PAGE_SHARED)#define PAGE_SHARED_EXEC __pgprot(_PAGE_SHARED_EXEC)arch/arm64/include/asm/pgtable.h +9
-1@@ -105,7 +105,7 @@ static inline pteval_t __phys_to_pte_val(phys_addr_t phys)/* * The following only work if pte_present(). Undefined behaviour otherwise. */ -#define pte_present (pte) (!!(pte_val(pte) & (PTE_VALID | PTE_PROT_NONE))) +#define pte_present (pte) (pte_valid(pte) || pte_invalid(pte))#define pte_young (pte) (!!(pte_val(pte) & PTE_AF))#define pte_special (pte) (!!(pte_val(pte) & PTE_SPECIAL))#define pte_write (pte) (!!(pte_val(pte) & PTE_WRITE))@@ -132,6 +132,7 @@ static inline pteval_t __phys_to_pte_val(phys_addr_t phys)#define pte_dirty (pte) (pte_sw_dirty(pte) || pte_hw_dirty(pte))#define pte_valid (pte) (!!(pte_val(pte) & PTE_VALID)) +#define pte_invalid (pte) ((pte_val(pte) & (PTE_VALID | PTE_INVALID)) == PTE_INVALID)/* * Execute-only user mappings do not have the PTE_USER bit set. All valid * kernel mappings have the PTE_UXN bit set. @@ -261,6 +262,13 @@ static inline pte_t pte_mkpresent(pte_t pte)return set_pte_bit (pte,__pgprot (PTE_VALID )); } +static inline pte_t pte_mkinvalid (pte_t pte) +{ + pte =set_pte_bit (pte,__pgprot (PTE_INVALID )); + pte =clear_pte_bit (pte,__pgprot (PTE_VALID )); +return pte; +} +static inline pmd_t pmd_mkcont (pmd_t pmd) {return __pmd (pmd_val (pmd) |PMD_SECT_CONT );
arch/arm64/include/asm/pgtable.h static inline pte_t pte_swp_clear_exclusive(pte_t pte) · line 481
Quoted 6 lines
arch/arm64/include/asm/pgtable.h +1
-1@@ -469,7 +477,7 @@ static inline pte_t pte_swp_clear_exclusive(pte_t pte)*/ static inline int pte_protnone (pte_t pte) { -return (pte_val (pte) & (PTE_VALID |PTE_PROT_NONE )) ==PTE_PROT_NONE ; +return pte_invalid (pte) && !pte_user (pte) && !pte_user_exec (pte); }
Why do we need to check pte_user_*() here? Isn't PROT_NONE the only case in which a pte will have PTE_INVALID set?
Will
Re: [PATCH v2 1/3] arm64/mm: Refactor PMD_PRESENT_INVALID and PTE_PROT_NONE bits
Quoted 21 lines · Will Deacon
Hey Ryan,
Just a couple of comments on this:
Quoted 16 lines · Ryan Roberts
arch/arm64/include/asm/pgtable-prot.h +0
-8@@ -18,14 +18,7 @@#define PTE_DIRTY (_AT(pteval_t, 1) << 55)#define PTE_SPECIAL (_AT(pteval_t, 1) << 56)#define PTE_DEVMAP (_AT(pteval_t, 1) << 57) -#define PTE_PROT_NONE (_AT(pteval_t, 1) << 58)/* only when !PTE_VALID */ - -/* -* This bit indicates that the entry is present i.e. pmd_page() -* still points to a valid huge page in memory even if the pmd -* has been invalidated. -*/ -#define PMD_PRESENT_INVALID (_AT(pteval_t, 1) << 59)/* only when !PMD_SECT_VALID */
Quoted 8 lines
arch/arm64/include/asm/pgtable-prot.h line 21
Quoted 1 line
arch/arm64/include/asm/pgtable-prot.h +1
-0+#define PTE_INVALID (_AT(pteval_t, 1) << 59)/* only when !PTE_VALID */ So this now overlaps with AttrIndx[3] if
FEAT_AIEis implemented. Although this shouldn't matter on the face of things because it's only used for invalid entries, we originally moved thePROT_NONEbit from 2 to 57 back in3676f9ef5481("arm64: MovePTE_PROT_NONEhigher up") because it was possible to change the memory type forPROT_NONEmappings via some drivers.
I'm not sure I follow your argument.
1. We don't support FEAT_AIE (currently) so AttrIndx[3] is always going to be 0
for valid ptes. Drivers are only calling our helpers (e.g. pgprot_writecombine(), right?) and those only know how to set AttrIndx[2:0].
2. PMD_PRESENT_INVALID was already occupying bit 59. So wouldn't the same shape
of concern apply there too for PMDs that have been invalidated, where the driver then comes along and changes the memory type? (Perhaps because PMD_PRESENT_INVALID is only set while the PTL is held this can't happen).
3. I had this same vague concern about confusion due to overlapping bit 59,
which is why in the next patch, I'm moving it to the NS bit.
Experience tells me that when I'm arguing confidently with someone who is much more expert than me, then I'm using wrong... so what have I missed? :)
Quoted 4 lines
Moving the field to the NS bit (as you do later in the series) resolves this, but the architecture currently says that the NS bit is RES0. How can we guarantee that it won't be repurposed by hardware in future?
Well it remains free for use in valid entries of course. So I guess you are asking how to guarantee we won't also need to be able to modify it on the fly for PROT_NONE entries? I don't have a definite answer, but I've been working on the assumption that the architecture introducing a feature that is only needed in states where NS is not needed is unlikely (so using that bit for the feature is also unlikely). And then needing to manipulate that feature dyanically for PROT_NONE mappings is even less likely.
If all else fails we could move it to nG (bit 11) to free up bit 5. But that requires a bit more fiddling with the swap pte format.
Quoted 49 lines
Quoted 49 lines
arch/arm64/include/asm/pgtable-prot.h +1
-1#define _PROT_DEFAULT (PTE_TYPE_PAGE | PTE_AF | PTE_SHARED)#define _PROT_SECT_DEFAULT (PMD_TYPE_SECT | PMD_SECT_AF | PMD_SECT_S)@@ -103,7 +96,7 @@ static inline bool __pure lpa2_is_enabled(void) __val; \ }) -#define PAGE_NONE __pgprot(((_PAGE_DEFAULT) & ~PTE_VALID) | PTE_PROT_NONE | PTE_RDONLY | PTE_NG | PTE_PXN | PTE_UXN) +#define PAGE_NONE __pgprot(((_PAGE_DEFAULT) & ~PTE_VALID) | PTE_INVALID | PTE_RDONLY | PTE_NG | PTE_PXN | PTE_UXN)/* shared+writable pages are clean by default, hence PTE_RDONLY|PTE_WRITE */ #define PAGE_SHARED __pgprot(_PAGE_SHARED)#define PAGE_SHARED_EXEC __pgprot(_PAGE_SHARED_EXEC)arch/arm64/include/asm/pgtable.h +9
-1@@ -105,7 +105,7 @@ static inline pteval_t __phys_to_pte_val(phys_addr_t phys)/* * The following only work if pte_present(). Undefined behaviour otherwise. */ -#define pte_present (pte) (!!(pte_val(pte) & (PTE_VALID | PTE_PROT_NONE))) +#define pte_present (pte) (pte_valid(pte) || pte_invalid(pte))#define pte_young (pte) (!!(pte_val(pte) & PTE_AF))#define pte_special (pte) (!!(pte_val(pte) & PTE_SPECIAL))#define pte_write (pte) (!!(pte_val(pte) & PTE_WRITE))@@ -132,6 +132,7 @@ static inline pteval_t __phys_to_pte_val(phys_addr_t phys)#define pte_dirty (pte) (pte_sw_dirty(pte) || pte_hw_dirty(pte))#define pte_valid (pte) (!!(pte_val(pte) & PTE_VALID)) +#define pte_invalid (pte) ((pte_val(pte) & (PTE_VALID | PTE_INVALID)) == PTE_INVALID)/* * Execute-only user mappings do not have the PTE_USER bit set. All valid * kernel mappings have the PTE_UXN bit set. @@ -261,6 +262,13 @@ static inline pte_t pte_mkpresent(pte_t pte)return set_pte_bit (pte,__pgprot (PTE_VALID )); } +static inline pte_t pte_mkinvalid (pte_t pte) +{ + pte =set_pte_bit (pte,__pgprot (PTE_INVALID )); + pte =clear_pte_bit (pte,__pgprot (PTE_VALID )); +return pte; +} +static inline pmd_t pmd_mkcont (pmd_t pmd) {return __pmd (pmd_val (pmd) |PMD_SECT_CONT );@@ -469,7 +477,7 @@ static inline pte_t pte_swp_clear_exclusive(pte_t pte)*/
Quoted 8 lines
arch/arm64/include/asm/pgtable.h static inline pte_t pte_swp_clear_exclusive(pte_t pte) · line 481
Quoted 5 lines
arch/arm64/include/asm/pgtable.h +1
-1static inline int pte_protnone (pte_t pte) { -return (pte_val (pte) & (PTE_VALID |PTE_PROT_NONE )) ==PTE_PROT_NONE ; +return pte_invalid (pte) && !pte_user (pte) && !pte_user_exec (pte); }Why do we need to check pte_user_*() here? Isn't
PROT_NONEthe only case in which a pte will havePTE_INVALIDset?
I guess for *ptes* this is technically correct. But I was trying to make the format generic and reusable for *pmds* too. (pmd_protnone() wraps pte_protnone()). For pmds, PTE_INVALID also represents invalid-but-present PMDs (i.e. pmds on which pmd_mkinvalid() has been called).
The intention is that PTE_INVALID indicates "present but not valid in HW". And (!pte_user(pte) && !pte_user_exec(pte)) indicates the PROT_NONE permission.
Quoted 2 lines
Will
Will Deacon
maintainerRe: [PATCH v2 1/3] arm64/mm: Refactor PMD_PRESENT_INVALID and PTE_PROT_NONE bits
Quoted 27 lines · Ryan Roberts
Quoted 17 lines · Will Deacon
Quoted 16 lines · Ryan Roberts
arch/arm64/include/asm/pgtable-prot.h +0
-8@@ -18,14 +18,7 @@#define PTE_DIRTY (_AT(pteval_t, 1) << 55)#define PTE_SPECIAL (_AT(pteval_t, 1) << 56)#define PTE_DEVMAP (_AT(pteval_t, 1) << 57) -#define PTE_PROT_NONE (_AT(pteval_t, 1) << 58)/* only when !PTE_VALID */ - -/* -* This bit indicates that the entry is present i.e. pmd_page() -* still points to a valid huge page in memory even if the pmd -* has been invalidated. -*/ -#define PMD_PRESENT_INVALID (_AT(pteval_t, 1) << 59)/* only when !PMD_SECT_VALID */ Quoted 8 lines
arch/arm64/include/asm/pgtable-prot.h line 21
Quoted 1 line
arch/arm64/include/asm/pgtable-prot.h +1
-0+#define PTE_INVALID (_AT(pteval_t, 1) << 59)/* only when !PTE_VALID */ So this now overlaps with AttrIndx[3] if
FEAT_AIEis implemented. Although this shouldn't matter on the face of things because it's only used for invalid entries, we originally moved thePROT_NONEbit from 2 to 57 back in3676f9ef5481("arm64: MovePTE_PROT_NONEhigher up") because it was possible to change the memory type forPROT_NONEmappings via some drivers.
Quoted 5 lines
I'm not sure I follow your argument.
1. We don't support
FEAT_AIE(currently) so AttrIndx[3] is always going to be 0
for valid ptes. Drivers are only calling our helpers (e.g.pgprot_writecombine(), right?) and those only know how to set AttrIndx[2:0].
Sure, but we might want to use it in future and chucking that out for the sake of uffd doesn't seem like an obviously worthwhile trade-off to me.
Quoted 4 lines
2.
PMD_PRESENT_INVALIDwas already occupying bit 59. So wouldn't the same shape
of concern apply there too for PMDs that have been invalidated, where the driver then comes along and changes the memory type? (Perhaps becausePMD_PRESENT_INVALIDis only set while the PTL is held this can't happen).
I was mainly thinking of the PROT_NONE case, to be honest with you. I struggle to envisage how a driver could sensibly mess with the memory type for anonymous mappings, let alone huge pages! But perhaps I just lack imagination :)
Quoted 7 lines
3. I had this same vague concern about confusion due to overlapping bit 59,
which is why in the next patch, I'm moving it to the NS bit.Experience tells me that when I'm arguing confidently with someone who is much more expert than me, then I'm using wrong... so what have I missed? :)
Quoted 1 line
Quoted 5 lines
Quoted 3 lines
Moving the field to the NS bit (as you do later in the series) resolves this, but the architecture currently says that the NS bit is RES0. How can we guarantee that it won't be repurposed by hardware in future?
Well it remains free for use in valid entries of course.
I think that's what I'm actually questioning! RES0 doesn't mean that tomorrow's whizz-bang CPU extension isn't allowed to use it, but that's a guarantee that we need if we're going to use it for our own purposes.
Quoted 7 lines
So I guess you are asking how to guarantee we won't also need to be able to modify it on the fly for
PROT_NONEentries? I don't have a definite answer, but I've been working on the assumption that the architecture introducing a feature that is only needed in states where NS is not needed is unlikely (so using that bit for the feature is also unlikely). And then needing to manipulate that feature dyanically forPROT_NONEmappings is even less likely.
The architects are quite good at inventing unlikely features :) SVE blowing the sigcontext comes to mind. I think we should seek clarification that the NS bit won't be allocated in the future if we are going to use it for our own stuff.
Quoted 2 lines
If all else fails we could move it to nG (bit 11) to free up bit 5. But that requires a bit more fiddling with the swap pte format.
Oh, cunning, I hadn't thought of that. I think that's probably a better approach if the NS bit isn't guaranteed to be left alone by the architecture.
Quoted 11 lines
Quoted 10 lines
arch/arm64/include/asm/pgtable-prot.h static inline pte_t pte_swp_clear_exclusive(pte_t pte) · line 481
Quoted 7 lines
arch/arm64/include/asm/pgtable-prot.h +1
-1@@ -469,7 +477,7 @@ static inline pte_t pte_swp_clear_exclusive(pte_t pte)*/ static inline int pte_protnone (pte_t pte) { -return (pte_val (pte) & (PTE_VALID |PTE_PROT_NONE )) ==PTE_PROT_NONE ; +return pte_invalid (pte) && !pte_user (pte) && !pte_user_exec (pte); }Why do we need to check pte_user_*() here? Isn't
PROT_NONEthe only case in which a pte will havePTE_INVALIDset?
Quoted 7 lines
I guess for *ptes* this is technically correct. But I was trying to make the format generic and reusable for *pmds* too. (
pmd_protnone()wrapspte_protnone()). For pmds,PTE_INVALIDalso represents invalid-but-present PMDs (i.e. pmds on whichpmd_mkinvalid()has been called).The intention is that
PTE_INVALIDindicates "present but not valid in HW". And (!pte_user(pte) && !pte_user_exec(pte)) indicates thePROT_NONEpermission.
Ok, but it does mean the compiler can't emit a nice TBNZ instruction for the pte macro. Can you either seperate out the pmd/pte versions of the macro or just add a comment along the lines of what you said above, please?
Cheers,
Will
Re: [PATCH v2 1/3] arm64/mm: Refactor PMD_PRESENT_INVALID and PTE_PROT_NONE bits
Quoted 28 lines · Will Deacon
Quoted 27 lines · Ryan Roberts
Quoted 25 lines · Will Deacon
Quoted 7 lines · Ryan Roberts
arch/arm64/include/asm/pgtable-prot.h +0
-0@@ -18,14 +18,7 @@#define PTE_DIRTY (_AT(pteval_t, 1) << 55)#define PTE_SPECIAL (_AT(pteval_t, 1) << 56)arch/arm64/include/asm/pgtable-prot.h line 21
Quoted 10 lines
arch/arm64/include/asm/pgtable-prot.h +1
-8#define PTE_DEVMAP (_AT(pteval_t, 1) << 57) -#define PTE_PROT_NONE (_AT(pteval_t, 1) << 58)/* only when !PTE_VALID */ - -/* -* This bit indicates that the entry is present i.e. pmd_page() -* still points to a valid huge page in memory even if the pmd -* has been invalidated. -*/ -#define PMD_PRESENT_INVALID (_AT(pteval_t, 1) << 59)/* only when !PMD_SECT_VALID */ +#define PTE_INVALID (_AT(pteval_t, 1) << 59)/* only when !PTE_VALID */ So this now overlaps with AttrIndx[3] if
FEAT_AIEis implemented. Although this shouldn't matter on the face of things because it's only used for invalid entries, we originally moved thePROT_NONEbit from 2 to 57 back in3676f9ef5481("arm64: MovePTE_PROT_NONEhigher up") because it was possible to change the memory type forPROT_NONEmappings via some drivers.
Quoted 8 lines
Quoted 5 lines
I'm not sure I follow your argument.
1. We don't support
FEAT_AIE(currently) so AttrIndx[3] is always going to be 0
for valid ptes. Drivers are only calling our helpers (e.g.pgprot_writecombine(), right?) and those only know how to set AttrIndx[2:0].Sure, but we might want to use it in future and chucking that out for the sake of uffd doesn't seem like an obviously worthwhile trade-off to me.
Totally agree, which is why I move it in the next patch. I was just commenting that its not a problem for this intermediate state between patches because the kernel doesn't support FEAT_AIE today.
Quoted 6 lines
Quoted 5 lines
2.
PMD_PRESENT_INVALIDwas already occupying bit 59. So wouldn't the same shape
of concern apply there too for PMDs that have been invalidated, where the driver then comes along and changes the memory type? (Perhaps becausePMD_PRESENT_INVALIDis only set while the PTL is held this can't happen).
Quoted 10 lines
I was mainly thinking of the
PROT_NONEcase, to be honest with you. I struggle to envisage how a driver could sensibly mess with the memory type for anonymous mappings, let alone huge pages! But perhaps I just lack imagination :)Quoted 5 lines
3. I had this same vague concern about confusion due to overlapping bit 59,
which is why in the next patch, I'm moving it to the NS bit.Experience tells me that when I'm arguing confidently with someone who is much more expert than me, then I'm using wrong... so what have I missed? :)
Sorry that was meant to say "I'm *usually* wrong" in case it wasn't obvious.
Quoted 9 lines
Quoted 7 lines
Quoted 5 lines
Moving the field to the NS bit (as you do later in the series) resolves this, but the architecture currently says that the NS bit is RES0. How can we guarantee that it won't be repurposed by hardware in future?
Well it remains free for use in valid entries of course.
I think that's what I'm actually questioning!
Sorry I'm not sure if we are talking cross-purposes... PTE_INVALID is only overlayed on the NS bit when PTE_VALID=0. So the NS bit remains 0 for valid PTEs today, and in future, any new feature control within the bit could be set as required for valid ptes.
But I *think* you are concerned about the possibility that any future feature control that occupies that bit could also require persisting in pte/pmd even when its invalidated (i.e. pmd_mkinvalid(pmd) or pte_modify(pte, PAGE_NONE))?
Quoted 12 lines
RES0 doesn't mean that
tomorrow's whizz-bang CPU extension isn't allowed to use it, but that's a guarantee that we need if we're going to use it for our own purposes.Quoted 7 lines
So I guess you are asking how to guarantee we won't also need to be able to modify it on the fly for
PROT_NONEentries? I don't have a definite answer, but I've been working on the assumption that the architecture introducing a feature that is only needed in states where NS is not needed is unlikely (so using that bit for the feature is also unlikely). And then needing to manipulate that feature dyanically forPROT_NONEmappings is even less likely.
Quoted 4 lines
The architects are quite good at inventing unlikely features :) SVE blowing the sigcontext comes to mind. I think we should seek clarification that the NS bit won't be allocated in the future if we are going to use it for our own stuff.
OK, clarification sought; the architects are *not* willing to upgrade the res0 to "IGNORED"...
Quoted 7 lines
Quoted 3 lines
If all else fails we could move it to nG (bit 11) to free up bit 5. But that requires a bit more fiddling with the swap pte format.
Oh, cunning, I hadn't thought of that. I think that's probably a better approach if the NS bit isn't guaranteed to be left alone by the architecture.
...So I'll change patch 2 to move the bit to nG (bit 11). Does that work for you? I *think* an alternative could be the contig bit. But nG feels safest to me
- I'd have to think a lot harder about contig bit.
Quoted 17 lines
Quoted 17 lines
Quoted 11 lines
arch/arm64/include/asm/pgtable-prot.h static inline pte_t pte_swp_clear_exclusive(pte_t pte) · line 481
Quoted 8 lines
arch/arm64/include/asm/pgtable-prot.h +1
-1@@ -469,7 +477,7 @@ static inline pte_t pte_swp_clear_exclusive(pte_t pte)*/ static inline int pte_protnone (pte_t pte) { -return (pte_val (pte) & (PTE_VALID |PTE_PROT_NONE )) ==PTE_PROT_NONE ; +return pte_invalid (pte) && !pte_user (pte) && !pte_user_exec (pte); }Why do we need to check pte_user_*() here? Isn't
PROT_NONEthe only case in which a pte will havePTE_INVALIDset?I guess for *ptes* this is technically correct. But I was trying to make the format generic and reusable for *pmds* too. (
pmd_protnone()wrapspte_protnone()). For pmds,PTE_INVALIDalso represents invalid-but-present PMDs (i.e. pmds on whichpmd_mkinvalid()has been called).
Quoted 6 lines
Quoted 2 lines
The intention is that
PTE_INVALIDindicates "present but not valid in HW". And (!pte_user(pte) && !pte_user_exec(pte)) indicates thePROT_NONEpermission.Ok, but it does mean the compiler can't emit a nice TBNZ instruction for the pte macro. Can you either seperate out the pmd/pte versions of the macro or just add a comment along the lines of what you said above, please?
I'll add a comment; I'd rather not have the implementations diverge unless there is a clear performance advantage.
Thanks for the review!
Quoted 4 lines
Cheers,
Will
PATCH v2 2/3 arm64/mm: Move PTE_INVALID to overlay PTE_NS
In reply to Ryan RobertsPTE_INVALID was previously occupying bit 59, which when a PTE is valid can either be IGNORED, PBHA[0] or AttrIndex[3], depending on the HW configuration. In practice this is currently not a problem because PTE_INVALID can only be 1 when PTE_VALID=0 and upstream Linux always requires the bit set to 0 for a valid pte.
However, if in future Linux wants to use the field (e.g. AttrIndex[3]) then we could end up with confusion when PTE_INVALID comes along and corrupts the field - we would ideally want to preserve it even for an invalid (but present) pte.
The other problem with bit 59 is that it prevents the offset field of a swap entry within a swap pte from growing beyond 51 bits. By moving PTE_INVALID to a low bit we can lay the swap pte out so that the offset field could grow to 53 bits in future.
So let's move PTE_INVALID to overlay PTE_NS (bit 5). PTE_NS is res0 for SW outside of the secure state so Linux will never need to touch it.
These are both marginal benefits, but make things a bit tidier in my opinion.
--- arch/arm64/include/asm/pgtable-hwdef.h | 1 + arch/arm64/include/asm/pgtable-prot.h | 2 +- arch/arm64/include/asm/pgtable.h | 8 ++++---- 3 files changed, 6 insertions(+), 5 deletions(-)
arch/arm64/include/asm/pgtable-hwdef.h +1 -0
@@ -160,6 +160,7 @@#define PTE_TYPE_MASK (_AT(pteval_t, 3) << 0)#define PTE_TYPE_PAGE (_AT(pteval_t, 3) << 0)#define PTE_TABLE_BIT (_AT(pteval_t, 1) << 1) +#define PTE_NS (_AT(pteval_t, 1) << 5)/* NS */ #define PTE_USER (_AT(pteval_t, 1) << 6)/* AP[1] */ #define PTE_RDONLY (_AT(pteval_t, 1) << 7)/* AP[2] */ #define PTE_SHARED (_AT(pteval_t, 3) << 8)/* SH[1:0], inner shareable */
arch/arm64/include/asm/pgtable-prot.h +1 -1
@@ -18,7 +18,7 @@#define PTE_DIRTY (_AT(pteval_t, 1) << 55)#define PTE_SPECIAL (_AT(pteval_t, 1) << 56)#define PTE_DEVMAP (_AT(pteval_t, 1) << 57) -#define PTE_INVALID (_AT(pteval_t, 1) << 59)/* only when !PTE_VALID */ +#define PTE_INVALID (PTE_NS)/* only when !PTE_VALID */ #define _PROT_DEFAULT (PTE_TYPE_PAGE | PTE_AF | PTE_SHARED)#define _PROT_SECT_DEFAULT (PMD_TYPE_SECT | PMD_SECT_AF | PMD_SECT_S)
arch/arm64/include/asm/pgtable.h +4 -4
@@ -1244,11 +1244,11 @@ static inline pmd_t pmdp_establish(struct vm_area_struct *vma,* Encode and decode a swap entry: * bits 0-1: present (must be zero) * bits 2: remember PG_anon_exclusive -* bits 3-7: swap type -* bits 8-57: swap offset -* bit 59: PTE_INVALID (must be zero) +* bit 5: PTE_INVALID (must be zero) +* bits 6-10: swap type +* bits 11-60: swap offset */ -#define __SWP_TYPE_SHIFT 3 +#define __SWP_TYPE_SHIFT 6#define __SWP_TYPE_BITS 5#define __SWP_OFFSET_BITS 50#define __SWP_TYPE_MASK ((1 << __SWP_TYPE_BITS) - 1)
Signature · 1 line
2.25.1
Re: [PATCH v2 2/3] arm64/mm: Move PTE_INVALID to overlay PTE_NS
Quoted 16 lines · Ryan Roberts
PTE_INVALIDwas previously occupying bit 59, which when a PTE is valid can either be IGNORED, PBHA[0] or AttrIndex[3], depending on the HW configuration. In practice this is currently not a problem becausePTE_INVALIDcan only be 1 when PTE_VALID=0 and upstream Linux always requires the bit set to 0 for a valid pte.However, if in future Linux wants to use the field (e.g. AttrIndex[3]) then we could end up with confusion when
PTE_INVALIDcomes along and corrupts the field - we would ideally want to preserve it even for an invalid (but present) pte.The other problem with bit 59 is that it prevents the offset field of a swap entry within a swap pte from growing beyond 51 bits. By moving
PTE_INVALIDto a low bit we can lay the swap pte out so that the offset field could grow to 53 bits in future.
Quoted 7 lines
So let's move
PTE_INVALIDto overlayPTE_NS(bit 5).PTE_NSis res0 for SW outside of the secure state so Linux will never need to touch it.These are both marginal benefits, but make things a bit tidier in my opinion.
Signed-off-by Ryan Roberts <[email protected]>
(subject to renaming PTE_INVALID to PTE_PRESENT_INVALID)
PATCH v2 3/3 arm64/mm: Add uffd write-protect support
In reply to Ryan RobertsLet's use the newly-free PTE SW bit (58) to add support for uffd-wp.
The standard handlers are implemented for set/test/clear for both pte and pmd. Additionally we must also track the uffd-wp state as a pte swp bit, so use a free swap pte bit (3).
--- arch/arm64/Kconfig | 1 + arch/arm64/include/asm/pgtable-prot.h | 8 +++++ arch/arm64/include/asm/pgtable.h | 44 +++++++++++++++++++++++++++ 3 files changed, 53 insertions(+)
arch/arm64/Kconfig +1 -0
@@ -255,6 +255,7 @@ config ARM64 select SYSCTL_EXCEPTION_TRACE select THREAD_INFO_IN_TASK select HAVE_ARCH_USERFAULTFD_MINOR if USERFAULTFD + select HAVE_ARCH_USERFAULTFD_WP if USERFAULTFD select TRACE_IRQFLAGS_SUPPORT select TRACE_IRQFLAGS_NMI_SUPPORT select HAVE_SOFTIRQ_ON_OWN_STACK
arch/arm64/include/asm/pgtable-prot.h +8 -0
@@ -20,6 +20,14 @@#define PTE_DEVMAP (_AT(pteval_t, 1) << 57)#define PTE_INVALID (PTE_NS)/* only when !PTE_VALID */ +#ifdef CONFIG_HAVE_ARCH_USERFAULTFD_WP +#define PTE_UFFD_WP (_AT(pteval_t, 1) << 58)/* uffd-wp tracking */ +#define PTE_SWP_UFFD_WP (_AT(pteval_t, 1) << 3)/* only for swp ptes */ +#else +#define PTE_UFFD_WP (_AT(pteval_t, 0)) +#define PTE_SWP_UFFD_WP (_AT(pteval_t, 0)) +#endif /* CONFIG_HAVE_ARCH_USERFAULTFD_WP */ +#define _PROT_DEFAULT (PTE_TYPE_PAGE | PTE_AF | PTE_SHARED)#define _PROT_SECT_DEFAULT (PMD_TYPE_SECT | PMD_SECT_AF | PMD_SECT_S)
arch/arm64/include/asm/pgtable.h +44 -0
68 lines not on this page · raw message
Signature · 1 line
2.25.1
Re: [PATCH v2 3/3] arm64/mm: Add uffd write-protect support
Quoted 10 lines · Ryan Roberts
Let's use the newly-free PTE SW bit (58) to add support for uffd-wp.
The standard handlers are implemented for set/test/clear for both pte and pmd. Additionally we must also track the uffd-wp state as a pte swp bit, so use a free swap pte bit (3).
Acked-by Peter Xu <[email protected]>Reviewed-by Catalin Marinas <[email protected]>Signed-off-by Ryan Roberts <[email protected]>---
Signature · 3 lines
Cheers, David / dhildenb