Support page table check on PowerPC
Subsystems (7)
v15 cover Support page table check on PowerPC
Support page table check on all PowerPC platforms. This works by serialising assignments, reassignments and clears of page table entries at each level in order to ensure that anonymous mappings have at most one writable consumer, and likewise that file-backed mappings are not simultaneously also anonymous mappings.
In order to support this infrastructure, a number of stubs must be defined for all powerpc platforms. Additionally, separate set_pte_at() and set_pte_at_unchecked(), to allow for internal, uninstrumented mappings.
On some PowerPC platforms, implementing {pte,pmd,pud}_user_accessible_page() requires the address. We revert previous changes that removed the address parameter from various interfaces, and add it to some other interfaces, in order to allow this.
(This series was initially written by Rohan McLure, who has left IBM and is no longer working on powerpc.)
v15:
* Rebase on mainline, including commit 91e40668e70a ("mm/page_table_check: Batch-check pmds/puds just like ptes") and associated arm64 changes
* Clarify/fix some commit messages
* Fix handling of address in a loop in __page_table_check_ptes_set()
v14:
* Fix a call to page_table_check_pud_set() that was missed (akpm)
v13:
* Rebase on mainline
* Don't use set_pte_at_unchecked() for early boot purposes (Pasha)
v12:
* Rename commits that revert changes to instead reflect that we are reinstating old behaviour due to it providing more flexibility
* Add return line to pud_pfn() stub
* Instrument ptep_get_and_clear() for nohash
v11:
* The pud_pfn() stub, which previously had no legitimate users on any powerpc platform, now has users in Book3s64 with transparent pages. Include a stub of the same name for each platform that does not define their own.
* Drop patch that standardised use of p*d_leaf(), as already included upstream in v6.9.
* Provide fallback definitions of p{m,u}d_user_accessible_page() that do not reference p*d_leaf(), p*d_pte(), as they are defined after powerpc/mm headers by linux/mm headers.
* Ensure that set_pte_at_unchecked() has the same checks as set_pte_at().
v10:
* Revert patches that removed address and mm parameters from page table check routines, including consuming code from arm64, x86_64 and riscv.
* Implement *_user_accessible_page() routines in terms of pte_user() where available (64-bit, book3s) but otherwise by checking the address (on platforms where the pte does not imply whether the mapping is for user or kernel)
* Internal set_pte_at() calls replaced with set_pte_at_unchecked(), which is identical, but prevents double instrumentation.
v9:
* Adapt to using the set_ptes() API, using __set_pte_at() where we need must avoid instrumentation.
* Use the logic of *_access_permitted() for implementing *_user_accessible_page(), which are required routines for page table check.
* Even though we no longer need p{m,u,4}d_leaf(), still default implement these to assist in refactoring out extant p{m,u,4}_is_leaf().
* Add p{m,u}_pte() stubs where asm-generic does not provide them, as page table check wants all *user_accessible_page() variants, and we would like to default implement the variants in terms of pte_user_accessible_page().
* Avoid the ugly pmdp_collapse_flush() macro nonsense! Just instrument its constituent calls instead for radix and hash.
v8:
* Fix linux/page_table_check.h include in asm/pgtable.h breaking 32-bit.
v7:
* Remove use of extern in set_pte prototypes
* Clean up pmdp_collapse_flush macro
* Replace set_pte_at with static inline function
* Fix commit message for patch 7
v6:
* Support huge pages and p{m,u}d accounting.
* Remove instrumentation from set_pte from kernel internal pages.
* 64s: Implement pmdp_collapse_flush in terms of __pmdp_collapse_flush as access to the mm_struct * is required.
v5:
Andrew Donnellan (2):
arm64/mm: Add addr parameter to __set_{ptes_anysz,ptes,pmds,puds}() arm64/mm: Add addr parameter to __ptep_get_and_clear_anysz()
Rohan McLure (11):
mm/page_table_check: Reinstate address parameter in
[__]page_table_check_pud[s]_set()
mm/page_table_check: Reinstate address parameter in
[__]page_table_check_pmd[s]_set()
mm/page_table_check: Provide addr parameter to
[__]page_table_check_ptes_set()
mm/page_table_check: Reinstate address parameter in
[__]page_table_check_pud_clear()
mm/page_table_check: Reinstate address parameter in
[__]page_table_check_pmd_clear()
mm/page_table_check: Reinstate address parameter in
[__]page_table_check_pte_clear() mm: Provide address parameter to p{te,md,ud}_user_accessible_page()
powerpc: mm: Add pud_pfn() stub
powerpc: mm: Implement *_user_accessible_page() for ptes
powerpc: mm: Use set_pte_at_unchecked() for internal usages
powerpc: mm: Support page table check
arch/arm64/include/asm/pgtable.h | 46 ++++++------- arch/arm64/mm/hugetlbpage.c | 15 +++-- arch/powerpc/Kconfig | 1 + arch/powerpc/include/asm/book3s/32/pgtable.h | 12 +++- arch/powerpc/include/asm/book3s/64/pgtable.h | 62 +++++++++++++++--- arch/powerpc/include/asm/nohash/pgtable.h | 13 +++- arch/powerpc/include/asm/pgtable.h | 19 ++++++ arch/powerpc/mm/book3s64/hash_pgtable.c | 4 ++ arch/powerpc/mm/book3s64/pgtable.c | 17 +++-- arch/powerpc/mm/book3s64/radix_pgtable.c | 9 ++- arch/powerpc/mm/pgtable.c | 12 ++++ arch/riscv/include/asm/pgtable.h | 20 +++--- arch/x86/include/asm/pgtable.h | 22 +++---- include/linux/page_table_check.h | 69 ++++++++++++-------- include/linux/pgtable.h | 10 +-- mm/page_table_check.c | 41 ++++++------ 16 files changed, 247 insertions(+), 125 deletions(-)
Signature · 1 line
2.49.0
PATCH v15 1/13 arm64/mm: Add addr parameter to __set_{ptes_anysz,ptes,pmds,puds}()
To provide support for page table check on powerpc, we need to reinstate the address parameter in several functions, including page_table_check_{ptes,pmds,puds}_set().
In preparation for this, add the addr parameter to arm64's __set_ptes_anysz() and its callers, __set_ptes(), __set_pmds() and __set_puds(). While this parameter won't (at present) be used on arm64, this will keep the usage of the page table check interfaces consistent.
---
v15: new patch
--- arch/arm64/include/asm/pgtable.h | 19 ++++++++----------- arch/arm64/mm/hugetlbpage.c | 8 ++++---- 2 files changed, 12 insertions(+), 15 deletions(-)
arch/arm64/include/asm/pgtable.h +8 -11
@@ -712,8 +712,8 @@ static inline pgprot_t pud_pgprot(pud_t pud)return __pgprot (pud_val (pfn_pud (pfn,__pgprot (0 ))) ^pud_val (pud)); } -static inline void __set_ptes_anysz (struct mm_struct *mm,pte_t *ptep, -pte_t pte,unsigned int nr, +static inline void __set_ptes_anysz (struct mm_struct *mm,unsigned long addr, +pte_t *ptep,pte_t pte,unsigned int nr,unsigned long pgsize) {unsigned long stride = pgsize >>PAGE_SHIFT ;
@@ -748,26 +748,23 @@ static inline void __set_ptes_anysz(struct mm_struct *mm, pte_t *ptep,__set_pte_complete (pte); } -static inline void __set_ptes (struct mm_struct *mm, -unsigned long __always_unused addr, +static inline void __set_ptes (struct mm_struct *mm,unsigned long addr,pte_t *ptep,pte_t pte,unsigned int nr) { -__set_ptes_anysz (mm, ptep, pte, nr,PAGE_SIZE ); +__set_ptes_anysz (mm, addr, ptep, pte, nr,PAGE_SIZE ); } -static inline void __set_pmds (struct mm_struct *mm, -unsigned long __always_unused addr, +static inline void __set_pmds (struct mm_struct *mm,unsigned long addr,pmd_t *pmdp,pmd_t pmd,unsigned int nr) { -__set_ptes_anysz (mm, (pte_t *)pmdp,pmd_pte (pmd), nr,PMD_SIZE ); +__set_ptes_anysz (mm, addr, (pte_t *)pmdp,pmd_pte (pmd), nr,PMD_SIZE ); }#define set_pmd_at (mm, addr, pmdp, pmd) __set_pmds(mm, addr, pmdp, pmd, 1) -static inline void __set_puds (struct mm_struct *mm, -unsigned long __always_unused addr, +static inline void __set_puds (struct mm_struct *mm,unsigned long addr,pud_t *pudp,pud_t pud,unsigned int nr) { -__set_ptes_anysz (mm, (pte_t *)pudp,pud_pte (pud), nr,PUD_SIZE ); +__set_ptes_anysz (mm, addr, (pte_t *)pudp,pud_pte (pud), nr,PUD_SIZE ); }#define set_pud_at (mm, addr, pudp, pud) __set_puds(mm, addr, pudp, pud, 1)
arch/arm64/mm/hugetlbpage.c +4 -4
@@ -226,7 +226,7 @@ void set_huge_pte_at(struct mm_struct *mm, unsigned long addr,if (!pte_present (pte)) {for (i =0 ; i < ncontig; i++, ptep++, addr += pgsize) -__set_ptes_anysz (mm, ptep, pte,1 , pgsize); +__set_ptes_anysz (mm, addr, ptep, pte,1 , pgsize);return ; }
@@ -234,7 +234,7 @@ void set_huge_pte_at(struct mm_struct *mm, unsigned long addr,if (pte_cont (pte) &&pte_valid (__ptep_get (ptep)))clear_flush (mm, addr, ptep, pgsize, ncontig); -__set_ptes_anysz (mm, ptep, pte, ncontig, pgsize); +__set_ptes_anysz (mm, addr, ptep, pte, ncontig, pgsize); } pte_t *huge_pte_alloc(struct mm_struct *mm, struct vm_area_struct *vma,
@@ -449,7 +449,7 @@ int huge_ptep_set_access_flags(struct vm_area_struct *vma,if (pte_young (orig_pte)) pte =pte_mkyoung (pte); -__set_ptes_anysz (mm, ptep, pte, ncontig, pgsize); +__set_ptes_anysz (mm, addr, ptep, pte, ncontig, pgsize);return 1 ; }
@@ -473,7 +473,7 @@ void huge_ptep_set_wrprotect(struct mm_struct *mm, pte =get_clear_contig_flush (mm, addr, ptep, pgsize, ncontig); pte =pte_wrprotect (pte); -__set_ptes_anysz (mm, ptep, pte, ncontig, pgsize); +__set_ptes_anysz (mm, addr, ptep, pte, ncontig, pgsize); } pte_t huge_ptep_clear_flush(struct vm_area_struct *vma,
Signature · 1 line
2.49.0
PATCH v15 2/13 arm64/mm: Add addr parameter to __ptep_get_and_clear_anysz()
In reply to Andrew DonnellanTo provide support for page table check on powerpc, we need to reinstate the address parameter in several functions, including page_table_check_{pte,pmd,pud}_clear().
In preparation for this, add the addr parameter to arm64's __ptep_get_and_clear_anysz() and change its callsites accordingly. While this parameter won't (at present) be used on arm64, this will keep the usage of the page table check interfaces consistent.
---
v15: new patch
--- arch/arm64/include/asm/pgtable.h | 5 +++-- arch/arm64/mm/hugetlbpage.c | 7 ++++--- 2 files changed, 7 insertions(+), 5 deletions(-)
arch/arm64/include/asm/pgtable.h +3 -2
@@ -1381,6 +1381,7 @@ static inline int pmdp_test_and_clear_young(struct vm_area_struct *vma,#endif /* CONFIG_TRANSPARENT_HUGEPAGE || CONFIG_ARCH_HAS_NONLEAF_PMD_YOUNG */ static inline pte_t __ptep_get_and_clear_anysz (struct mm_struct *mm, +unsigned long address,pte_t *ptep,unsigned long pgsize) {
@@ -1408,7 +1409,7 @@ static inline pte_t __ptep_get_and_clear_anysz(struct mm_struct *mm,static inline pte_t __ptep_get_and_clear (struct mm_struct *mm,unsigned long address,pte_t *ptep) { -return __ptep_get_and_clear_anysz (mm, ptep,PAGE_SIZE ); +return __ptep_get_and_clear_anysz (mm, address, ptep,PAGE_SIZE ); } static inline void __clear_full_ptes(struct mm_struct *mm, unsigned long addr,
@@ -1447,7 +1448,7 @@ static inline pte_t __get_and_clear_full_ptes(struct mm_struct *mm,static inline pmd_t pmdp_huge_get_and_clear (struct mm_struct *mm,unsigned long address,pmd_t *pmdp) { -return pte_pmd (__ptep_get_and_clear_anysz (mm, (pte_t *)pmdp,PMD_SIZE )); +return pte_pmd (__ptep_get_and_clear_anysz (mm, address, (pte_t *)pmdp,PMD_SIZE )); }#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
arch/arm64/mm/hugetlbpage.c +4 -3
@@ -159,11 +159,12 @@ static pte_t get_clear_contig(struct mm_struct *mm,pte_t pte, tmp_pte;bool present; - pte =__ptep_get_and_clear_anysz (mm, ptep, pgsize); + pte =__ptep_get_and_clear_anysz (mm, addr, ptep, pgsize); present =pte_present (pte);while (--ncontig) { ptep++; - tmp_pte =__ptep_get_and_clear_anysz (mm, ptep, pgsize); + addr += pgsize; + tmp_pte =__ptep_get_and_clear_anysz (mm, addr, ptep, pgsize);if (present) {if (pte_dirty (tmp_pte)) pte =pte_mkdirty (pte);
@@ -207,7 +208,7 @@ static void clear_flush(struct mm_struct *mm,unsigned long i, saddr = addr;for (i =0 ; i < ncontig; i++, addr += pgsize, ptep++) -__ptep_get_and_clear_anysz (mm, ptep, pgsize); +__ptep_get_and_clear_anysz (mm, addr, ptep, pgsize);if (mm == &init_mm)flush_tlb_kernel_range (saddr, addr);
Signature · 1 line
2.49.0
PATCH v15 3/13 mm/page_table_check: Reinstate address parameter in [__]page_table_check_pud[s]_set()
In reply to Andrew DonnellanFrom: Rohan McLure <[email protected]>
This reverts commit 6d144436d954 ("mm/page_table_check: remove unused parameter in [__]page_table_check_pud_set").
Reinstate previously unused parameters for the purpose of supporting powerpc platforms, as many do not encode user/kernel ownership of the page in the pte, but instead in the address of the access.
Apply this to __page_table_check_puds_set(), page_table_check_puds_set() and the page_table_check_pud_set() wrapper macro.
---
v13: remove inaccurate comment on riscv in the commit message
v14: fix an x86 usage I missed (found by akpm)
v15: rebase, amend commit message
--- arch/arm64/include/asm/pgtable.h | 3 ++- arch/riscv/include/asm/pgtable.h | 4 ++-- arch/x86/include/asm/pgtable.h | 4 ++-- include/linux/page_table_check.h | 12 ++++++------ mm/page_table_check.c | 4 ++-- 5 files changed, 14 insertions(+), 13 deletions(-)
arch/arm64/include/asm/pgtable.h +2 -1
@@ -727,7 +727,8 @@ static inline void __set_ptes_anysz(struct mm_struct *mm, unsigned long addr,break ;#ifndef __PAGETABLE_PMD_FOLDEDcase PUD_SIZE : -page_table_check_puds_set (mm, (pud_t *)ptep,pte_pud (pte), nr); +page_table_check_puds_set (mm, addr, (pud_t *)ptep, +pte_pud (pte), nr);break ;#endif default :
arch/riscv/include/asm/pgtable.h +2 -2
@@ -829,7 +829,7 @@ static inline void set_pmd_at(struct mm_struct *mm, unsigned long addr,static inline void set_pud_at (struct mm_struct *mm,unsigned long addr,pud_t *pudp,pud_t pud) { -page_table_check_pud_set (mm, pudp, pud); +page_table_check_pud_set (mm, addr, pudp, pud);return __set_pte_at (mm, (pte_t *)pudp,pud_pte (pud)); }
@@ -980,7 +980,7 @@ static inline void update_mmu_cache_pud(struct vm_area_struct *vma,static inline pud_t pudp_establish (struct vm_area_struct *vma,unsigned long address,pud_t *pudp,pud_t pud) { -page_table_check_pud_set (vma->vm_mm, pudp, pud); +page_table_check_pud_set (vma->vm_mm, address, pudp, pud);return __pud (atomic_long_xchg ((atomic_long_t *)pudp,pud_val (pud))); }
arch/x86/include/asm/pgtable.h +2 -2
@@ -1267,7 +1267,7 @@ static inline void set_pmd_at(struct mm_struct *mm, unsigned long addr,static inline void set_pud_at (struct mm_struct *mm,unsigned long addr,pud_t *pudp,pud_t pud) { -page_table_check_pud_set (mm, pudp, pud); +page_table_check_pud_set (mm, addr, pudp, pud);native_set_pud (pudp, pud); }
@@ -1418,7 +1418,7 @@ static inline pmd_t pmdp_establish(struct vm_area_struct *vma,static inline pud_t pudp_establish (struct vm_area_struct *vma,unsigned long address,pud_t *pudp,pud_t pud) { -page_table_check_pud_set (vma->vm_mm, pudp, pud); +page_table_check_pud_set (vma->vm_mm, address, pudp, pud);if (IS_ENABLED (CONFIG_SMP )) {return xchg (pudp, pud); }else {
include/linux/page_table_check.h +6 -6
@@ -21,8 +21,8 @@ void __page_table_check_ptes_set(struct mm_struct *mm, pte_t *ptep, pte_t pte, unsigned int nr);void __page_table_check_pmds_set (struct mm_struct *mm,pmd_t *pmdp,pmd_t pmd,unsigned int nr); -void __page_table_check_puds_set (struct mm_struct *mm,pud_t *pudp,pud_t pud, -unsigned int nr); +void __page_table_check_puds_set (struct mm_struct *mm,unsigned long addr, +pud_t *pudp,pud_t pud,unsigned int nr);void __page_table_check_pte_clear_range (struct mm_struct *mm,unsigned long addr,pmd_t pmd);
@@ -86,12 +86,12 @@ static inline void page_table_check_pmds_set(struct mm_struct *mm, }static inline void page_table_check_puds_set (struct mm_struct *mm, -pud_t *pudp,pud_t pud,unsigned int nr) +unsigned long addr,pud_t *pudp,pud_t pud,unsigned int nr) {if (static_branch_likely (&page_table_check_disabled))return ; -__page_table_check_puds_set (mm, pudp, pud, nr); +__page_table_check_puds_set (mm, addr, pudp, pud, nr); } static inline void page_table_check_pte_clear_range(struct mm_struct *mm,
@@ -137,7 +137,7 @@ static inline void page_table_check_pmds_set(struct mm_struct *mm, }static inline void page_table_check_puds_set (struct mm_struct *mm, -pud_t *pudp,pud_t pud,unsigned int nr) +unsigned long addr,pud_t *pudp,pud_t pud,unsigned int nr) { }
@@ -150,6 +150,6 @@ static inline void page_table_check_pte_clear_range(struct mm_struct *mm,#endif /* CONFIG_PAGE_TABLE_CHECK */ #define page_table_check_pmd_set (mm, pmdp, pmd) page_table_check_pmds_set(mm, pmdp, pmd, 1) -#define page_table_check_pud_set (mm, pudp, pud) page_table_check_puds_set(mm, pudp, pud, 1) +#define page_table_check_pud_set (mm, addr, pudp, pud) page_table_check_puds_set(mm, addr, pudp, pud, 1)#endif /* __LINUX_PAGE_TABLE_CHECK_H */
mm/page_table_check.c +2 -2
@@ -236,8 +236,8 @@ void __page_table_check_pmds_set(struct mm_struct *mm, pmd_t *pmdp, pmd_t pmd, }EXPORT_SYMBOL (__page_table_check_pmds_set); -void __page_table_check_puds_set (struct mm_struct *mm,pud_t *pudp,pud_t pud, -unsigned int nr) +void __page_table_check_puds_set (struct mm_struct *mm,unsigned long addr, +pud_t *pudp,pud_t pud,unsigned int nr) {unsigned long stride =PUD_SIZE >>PAGE_SHIFT ;unsigned int i;
Signature · 1 line
2.49.0
PATCH v15 4/13 mm/page_table_check: Reinstate address parameter in [__]page_table_check_pmd[s]_set()
In reply to Andrew DonnellanFrom: Rohan McLure <[email protected]>
This reverts commit a3b837130b58 ("mm/page_table_check: remove unused parameter in [__]page_table_check_pmd_set").
Reinstate previously unused parameters for the purpose of supporting powerpc platforms, as many do not encode user/kernel ownership of the page in the pte, but instead in the address of the access.
Apply this to __page_table_check_pmds_set(), page_table_check_pmd_set(), and the page_table_check_pmd_set() wrapper macro.
---
v13: remove inaccurate comment on riscv in the commit message
v14: rebase
v15: rebase, amend commit message
--- arch/arm64/include/asm/pgtable.h | 5 +++-- arch/riscv/include/asm/pgtable.h | 4 ++-- arch/x86/include/asm/pgtable.h | 4 ++-- include/linux/page_table_check.h | 12 ++++++------ mm/page_table_check.c | 4 ++-- 5 files changed, 15 insertions(+), 14 deletions(-)
arch/arm64/include/asm/pgtable.h +3 -2
@@ -723,7 +723,8 @@ static inline void __set_ptes_anysz(struct mm_struct *mm, unsigned long addr,page_table_check_ptes_set (mm, ptep, pte, nr);break ;case PMD_SIZE : -page_table_check_pmds_set (mm, (pmd_t *)ptep,pte_pmd (pte), nr); +page_table_check_pmds_set (mm, addr, (pmd_t *)ptep, +pte_pmd (pte), nr);break ;#ifndef __PAGETABLE_PMD_FOLDEDcase PUD_SIZE :
@@ -1538,7 +1539,7 @@ static inline void pmdp_set_wrprotect(struct mm_struct *mm,static inline pmd_t pmdp_establish (struct vm_area_struct *vma,unsigned long address,pmd_t *pmdp,pmd_t pmd) { -page_table_check_pmd_set (vma->vm_mm, pmdp, pmd); +page_table_check_pmd_set (vma->vm_mm, address, pmdp, pmd);return __pmd (xchg_relaxed (&pmd_val (*pmdp),pmd_val (pmd))); }#endif
arch/riscv/include/asm/pgtable.h +2 -2
@@ -822,7 +822,7 @@ static inline pud_t pud_mkspecial(pud_t pud)static inline void set_pmd_at (struct mm_struct *mm,unsigned long addr,pmd_t *pmdp,pmd_t pmd) { -page_table_check_pmd_set (mm, pmdp, pmd); +page_table_check_pmd_set (mm, addr, pmdp, pmd);return __set_pte_at (mm, (pte_t *)pmdp,pmd_pte (pmd)); }
@@ -893,7 +893,7 @@ static inline void pmdp_set_wrprotect(struct mm_struct *mm,static inline pmd_t pmdp_establish (struct vm_area_struct *vma,unsigned long address,pmd_t *pmdp,pmd_t pmd) { -page_table_check_pmd_set (vma->vm_mm, pmdp, pmd); +page_table_check_pmd_set (vma->vm_mm, address, pmdp, pmd);return __pmd (atomic_long_xchg ((atomic_long_t *)pmdp,pmd_val (pmd))); }
arch/x86/include/asm/pgtable.h +2 -2
@@ -1260,7 +1260,7 @@ static inline pud_t native_local_pudp_get_and_clear(pud_t *pudp)static inline void set_pmd_at (struct mm_struct *mm,unsigned long addr,pmd_t *pmdp,pmd_t pmd) { -page_table_check_pmd_set (mm, pmdp, pmd); +page_table_check_pmd_set (mm, addr, pmdp, pmd);set_pmd (pmdp, pmd); }
@@ -1403,7 +1403,7 @@ static inline void pmdp_set_wrprotect(struct mm_struct *mm,static inline pmd_t pmdp_establish (struct vm_area_struct *vma,unsigned long address,pmd_t *pmdp,pmd_t pmd) { -page_table_check_pmd_set (vma->vm_mm, pmdp, pmd); +page_table_check_pmd_set (vma->vm_mm, address, pmdp, pmd);if (IS_ENABLED (CONFIG_SMP )) {return xchg (pmdp, pmd); }else {
include/linux/page_table_check.h +6 -6
@@ -19,8 +19,8 @@ void __page_table_check_pmd_clear(struct mm_struct *mm, pmd_t pmd);void __page_table_check_pud_clear (struct mm_struct *mm,pud_t pud);void __page_table_check_ptes_set (struct mm_struct *mm,pte_t *ptep,pte_t pte,unsigned int nr); -void __page_table_check_pmds_set (struct mm_struct *mm,pmd_t *pmdp,pmd_t pmd, -unsigned int nr); +void __page_table_check_pmds_set (struct mm_struct *mm,unsigned long addr, +pmd_t *pmdp,pmd_t pmd,unsigned int nr);void __page_table_check_puds_set (struct mm_struct *mm,unsigned long addr,pud_t *pudp,pud_t pud,unsigned int nr); void __page_table_check_pte_clear_range(struct mm_struct *mm,
@@ -77,12 +77,12 @@ static inline void page_table_check_ptes_set(struct mm_struct *mm, }static inline void page_table_check_pmds_set (struct mm_struct *mm, -pmd_t *pmdp,pmd_t pmd,unsigned int nr) +unsigned long addr,pmd_t *pmdp,pmd_t pmd,unsigned int nr) {if (static_branch_likely (&page_table_check_disabled))return ; -__page_table_check_pmds_set (mm, pmdp, pmd, nr); +__page_table_check_pmds_set (mm, addr, pmdp, pmd, nr); } static inline void page_table_check_puds_set(struct mm_struct *mm,
@@ -132,7 +132,7 @@ static inline void page_table_check_ptes_set(struct mm_struct *mm, }static inline void page_table_check_pmds_set (struct mm_struct *mm, -pmd_t *pmdp,pmd_t pmd,unsigned int nr) +unsigned long addr,pmd_t *pmdp,pmd_t pmd,unsigned int nr) { }
@@ -149,7 +149,7 @@ static inline void page_table_check_pte_clear_range(struct mm_struct *mm,#endif /* CONFIG_PAGE_TABLE_CHECK */ -#define page_table_check_pmd_set (mm, pmdp, pmd) page_table_check_pmds_set(mm, pmdp, pmd, 1) +#define page_table_check_pmd_set (mm, addr, pmdp, pmd) page_table_check_pmds_set(mm, addr, pmdp, pmd, 1)#define page_table_check_pud_set (mm, addr, pudp, pud) page_table_check_puds_set(mm, addr, pudp, pud, 1)#endif /* __LINUX_PAGE_TABLE_CHECK_H */
mm/page_table_check.c +2 -2
@@ -218,8 +218,8 @@ static inline void page_table_check_pmd_flags(pmd_t pmd)WARN_ON_ONCE (swap_cached_writable (pmd_to_swp_entry (pmd))); } -void __page_table_check_pmds_set (struct mm_struct *mm,pmd_t *pmdp,pmd_t pmd, -unsigned int nr) +void __page_table_check_pmds_set (struct mm_struct *mm,unsigned long addr, +pmd_t *pmdp,pmd_t pmd,unsigned int nr) {unsigned long stride =PMD_SIZE >>PAGE_SHIFT ;unsigned int i;
Signature · 1 line
2.49.0
PATCH v15 5/13 mm/page_table_check: Provide addr parameter to [__]page_table_check_ptes_set()
In reply to Andrew DonnellanFrom: Rohan McLure <[email protected]>
To provide support for powerpc platforms, provide an addr parameter to the __page_table_check_ptes_set() and page_table_check_ptes_set() routines. This parameter is needed on some powerpc platforms which do not encode whether a mapping is for user or kernel in the pte. On such platforms, this can be inferred from the addr parameter.
---
v15: rebase, amend commit message
--- arch/arm64/include/asm/pgtable.h | 2 +- arch/riscv/include/asm/pgtable.h | 2 +- include/linux/page_table_check.h | 12 +++++++----- include/linux/pgtable.h | 2 +- mm/page_table_check.c | 4 ++-- 5 files changed, 12 insertions(+), 10 deletions(-)
arch/arm64/include/asm/pgtable.h +1 -1
@@ -720,7 +720,7 @@ static inline void __set_ptes_anysz(struct mm_struct *mm, unsigned long addr,switch (pgsize) {case PAGE_SIZE : -page_table_check_ptes_set (mm, ptep, pte, nr); +page_table_check_ptes_set (mm, addr, ptep, pte, nr);break ;case PMD_SIZE : page_table_check_pmds_set(mm, addr, (pmd_t *)ptep,
arch/riscv/include/asm/pgtable.h +1 -1
@@ -572,7 +572,7 @@ static inline void __set_pte_at(struct mm_struct *mm, pte_t *ptep, pte_t pteval)static inline void set_ptes (struct mm_struct *mm,unsigned long addr,pte_t *ptep,pte_t pteval,unsigned int nr) { -page_table_check_ptes_set (mm, ptep, pteval, nr); +page_table_check_ptes_set (mm, addr, ptep, pteval, nr);for (;;) {__set_pte_at (mm, ptep, pteval);
include/linux/page_table_check.h +7 -5
@@ -17,8 +17,8 @@ void __page_table_check_zero(struct page *page, unsigned int order);void __page_table_check_pte_clear (struct mm_struct *mm,pte_t pte);void __page_table_check_pmd_clear (struct mm_struct *mm,pmd_t pmd);void __page_table_check_pud_clear (struct mm_struct *mm,pud_t pud); -void __page_table_check_ptes_set (struct mm_struct *mm,pte_t *ptep,pte_t pte, -unsigned int nr); +void __page_table_check_ptes_set (struct mm_struct *mm,unsigned long addr, +pte_t *ptep,pte_t pte,unsigned int nr);void __page_table_check_pmds_set (struct mm_struct *mm,unsigned long addr,pmd_t *pmdp,pmd_t pmd,unsigned int nr); void __page_table_check_puds_set(struct mm_struct *mm, unsigned long addr,
@@ -68,12 +68,13 @@ static inline void page_table_check_pud_clear(struct mm_struct *mm, pud_t pud) }static inline void page_table_check_ptes_set (struct mm_struct *mm, -pte_t *ptep,pte_t pte,unsigned int nr) +unsigned long addr,pte_t *ptep, +pte_t pte,unsigned int nr) {if (static_branch_likely (&page_table_check_disabled))return ; -__page_table_check_ptes_set (mm, ptep, pte, nr); +__page_table_check_ptes_set (mm, addr, ptep, pte, nr); } static inline void page_table_check_pmds_set(struct mm_struct *mm,
@@ -127,7 +128,8 @@ static inline void page_table_check_pud_clear(struct mm_struct *mm, pud_t pud) }static inline void page_table_check_ptes_set (struct mm_struct *mm, -pte_t *ptep,pte_t pte,unsigned int nr) +unsigned long addr,pte_t *ptep, +pte_t pte,unsigned int nr) { }
include/linux/pgtable.h +1 -1
@@ -289,7 +289,7 @@ static inline pte_t pte_advance_pfn(pte_t pte, unsigned long nr)static inline void set_ptes (struct mm_struct *mm,unsigned long addr,pte_t *ptep,pte_t pte,unsigned int nr) { -page_table_check_ptes_set (mm, ptep, pte, nr); +page_table_check_ptes_set (mm, addr, ptep, pte, nr);for (;;) {set_pte (ptep, pte);
mm/page_table_check.c +2 -2
@@ -193,8 +193,8 @@ static inline void page_table_check_pte_flags(pte_t pte)WARN_ON_ONCE (swap_cached_writable (pte_to_swp_entry (pte))); } -void __page_table_check_ptes_set (struct mm_struct *mm,pte_t *ptep,pte_t pte, -unsigned int nr) +void __page_table_check_ptes_set (struct mm_struct *mm,unsigned long addr, +pte_t *ptep,pte_t pte,unsigned int nr) {unsigned int i;
Signature · 1 line
2.49.0
PATCH v15 6/13 mm/page_table_check: Reinstate address parameter in [__]page_table_check_pud_clear()
In reply to Andrew DonnellanFrom: Rohan McLure <[email protected]>
This reverts commit 931c38e16499 ("mm/page_table_check: remove unused parameter in [__]page_table_check_pud_clear").
Reinstate previously unused parameters for the purpose of supporting powerpc platforms, as many do not encode user/kernel ownership of the page in the pte, but instead in the address of the access.
---
v15: rebase
--- arch/arm64/include/asm/pgtable.h | 2 +- arch/x86/include/asm/pgtable.h | 2 +- include/linux/page_table_check.h | 11 +++++++---- include/linux/pgtable.h | 2 +- mm/page_table_check.c | 5 +++-- 5 files changed, 13 insertions(+), 9 deletions(-)
arch/arm64/include/asm/pgtable.h +1 -1
@@ -1398,7 +1398,7 @@ static inline pte_t __ptep_get_and_clear_anysz(struct mm_struct *mm,break ;#ifndef __PAGETABLE_PMD_FOLDEDcase PUD_SIZE : -page_table_check_pud_clear (mm,pte_pud (pte)); +page_table_check_pud_clear (mm, address,pte_pud (pte));break ;#endif default :
arch/x86/include/asm/pgtable.h +1 -1
@@ -1376,7 +1376,7 @@ static inline pud_t pudp_huge_get_and_clear(struct mm_struct *mm, {pud_t pud =native_pudp_get_and_clear (pudp); -page_table_check_pud_clear (mm, pud); +page_table_check_pud_clear (mm, addr, pud);return pud; }
include/linux/page_table_check.h +7 -4
@@ -16,7 +16,8 @@ extern struct page_ext_operations page_table_check_ops;void __page_table_check_zero (struct page *page,unsigned int order);void __page_table_check_pte_clear (struct mm_struct *mm,pte_t pte);void __page_table_check_pmd_clear (struct mm_struct *mm,pmd_t pmd); -void __page_table_check_pud_clear (struct mm_struct *mm,pud_t pud); +void __page_table_check_pud_clear (struct mm_struct *mm,unsigned long addr, +pud_t pud);void __page_table_check_ptes_set (struct mm_struct *mm,unsigned long addr,pte_t *ptep,pte_t pte,unsigned int nr); void __page_table_check_pmds_set(struct mm_struct *mm, unsigned long addr,
@@ -59,12 +60,13 @@ static inline void page_table_check_pmd_clear(struct mm_struct *mm, pmd_t pmd)__page_table_check_pmd_clear (mm, pmd); } -static inline void page_table_check_pud_clear (struct mm_struct *mm,pud_t pud) +static inline void page_table_check_pud_clear (struct mm_struct *mm, +unsigned long addr,pud_t pud) {if (static_branch_likely (&page_table_check_disabled))return ; -__page_table_check_pud_clear (mm, pud); +__page_table_check_pud_clear (mm, addr, pud); } static inline void page_table_check_ptes_set(struct mm_struct *mm,
@@ -123,7 +125,8 @@ static inline void page_table_check_pmd_clear(struct mm_struct *mm, pmd_t pmd) { } -static inline void page_table_check_pud_clear (struct mm_struct *mm,pud_t pud) +static inline void page_table_check_pud_clear (struct mm_struct *mm, +unsigned long addr,pud_t pud) { }
include/linux/pgtable.h +1 -1
@@ -650,7 +650,7 @@ static inline pud_t pudp_huge_get_and_clear(struct mm_struct *mm,pud_t pud = *pudp;pud_clear (pudp); -page_table_check_pud_clear (mm, pud); +page_table_check_pud_clear (mm, address, pud);return pud; }
mm/page_table_check.c +3 -2
@@ -167,7 +167,8 @@ void __page_table_check_pmd_clear(struct mm_struct *mm, pmd_t pmd) }EXPORT_SYMBOL (__page_table_check_pmd_clear); -void __page_table_check_pud_clear (struct mm_struct *mm,pud_t pud) +void __page_table_check_pud_clear (struct mm_struct *mm,unsigned long addr, +pud_t pud) {if (&init_mm == mm)return ;
@@ -246,7 +247,7 @@ void __page_table_check_puds_set(struct mm_struct *mm, unsigned long addr,return ;for (i =0 ; i < nr; i++) -__page_table_check_pud_clear (mm, *(pudp + i)); +__page_table_check_pud_clear (mm, addr +PUD_SIZE * i, *(pudp + i));if (pud_user_accessible_page (pud))page_table_check_set (pud_pfn (pud), stride * nr,pud_write (pud)); }
Signature · 1 line
2.49.0
PATCH v15 7/13 mm/page_table_check: Reinstate address parameter in [__]page_table_check_pmd_clear()
In reply to Andrew DonnellanFrom: Rohan McLure <[email protected]>
This reverts commit 1831414cd729 ("mm/page_table_check: remove unused parameter in [__]page_table_check_pmd_clear").
Reinstate previously unused parameters for the purpose of supporting powerpc platforms, as many do not encode user/kernel ownership of the page in the pte, but instead in the address of the access.
---
v15: rebase
--- arch/arm64/include/asm/pgtable.h | 2 +- arch/riscv/include/asm/pgtable.h | 2 +- arch/x86/include/asm/pgtable.h | 2 +- include/linux/page_table_check.h | 11 +++++++---- include/linux/pgtable.h | 2 +- mm/page_table_check.c | 5 +++-- 6 files changed, 14 insertions(+), 10 deletions(-)
arch/arm64/include/asm/pgtable.h +1 -1
@@ -1394,7 +1394,7 @@ static inline pte_t __ptep_get_and_clear_anysz(struct mm_struct *mm,page_table_check_pte_clear (mm, pte);break ;case PMD_SIZE : -page_table_check_pmd_clear (mm,pte_pmd (pte)); +page_table_check_pmd_clear (mm, address,pte_pmd (pte));break ;#ifndef __PAGETABLE_PMD_FOLDEDcase PUD_SIZE :
arch/riscv/include/asm/pgtable.h +1 -1
@@ -877,7 +877,7 @@ static inline pmd_t pmdp_huge_get_and_clear(struct mm_struct *mm, {pmd_t pmd =__pmd (atomic_long_xchg ((atomic_long_t *)pmdp,0 )); -page_table_check_pmd_clear (mm, pmd); +page_table_check_pmd_clear (mm, address, pmd);return pmd; }
arch/x86/include/asm/pgtable.h +1 -1
@@ -1365,7 +1365,7 @@ static inline pmd_t pmdp_huge_get_and_clear(struct mm_struct *mm, unsigned long {pmd_t pmd =native_pmdp_get_and_clear (pmdp); -page_table_check_pmd_clear (mm, pmd); +page_table_check_pmd_clear (mm, addr, pmd);return pmd; }
include/linux/page_table_check.h +7 -4
@@ -15,7 +15,8 @@ extern struct page_ext_operations page_table_check_ops;void __page_table_check_zero (struct page *page,unsigned int order);void __page_table_check_pte_clear (struct mm_struct *mm,pte_t pte); -void __page_table_check_pmd_clear (struct mm_struct *mm,pmd_t pmd); +void __page_table_check_pmd_clear (struct mm_struct *mm,unsigned long addr, +pmd_t pmd);void __page_table_check_pud_clear (struct mm_struct *mm,unsigned long addr,pud_t pud); void __page_table_check_ptes_set(struct mm_struct *mm, unsigned long addr,
@@ -52,12 +53,13 @@ static inline void page_table_check_pte_clear(struct mm_struct *mm, pte_t pte)__page_table_check_pte_clear (mm, pte); } -static inline void page_table_check_pmd_clear (struct mm_struct *mm,pmd_t pmd) +static inline void page_table_check_pmd_clear (struct mm_struct *mm, +unsigned long addr,pmd_t pmd) {if (static_branch_likely (&page_table_check_disabled))return ; -__page_table_check_pmd_clear (mm, pmd); +__page_table_check_pmd_clear (mm, addr, pmd); } static inline void page_table_check_pud_clear(struct mm_struct *mm,
@@ -121,7 +123,8 @@ static inline void page_table_check_pte_clear(struct mm_struct *mm, pte_t pte) { } -static inline void page_table_check_pmd_clear (struct mm_struct *mm,pmd_t pmd) +static inline void page_table_check_pmd_clear (struct mm_struct *mm, +unsigned long addr,pmd_t pmd) { }
include/linux/pgtable.h +1 -1
@@ -637,7 +637,7 @@ static inline pmd_t pmdp_huge_get_and_clear(struct mm_struct *mm,pmd_t pmd = *pmdp;pmd_clear (pmdp); -page_table_check_pmd_clear (mm, pmd); +page_table_check_pmd_clear (mm, address, pmd);return pmd; }
mm/page_table_check.c +3 -2
@@ -156,7 +156,8 @@ void __page_table_check_pte_clear(struct mm_struct *mm, pte_t pte) }EXPORT_SYMBOL (__page_table_check_pte_clear); -void __page_table_check_pmd_clear (struct mm_struct *mm,pmd_t pmd) +void __page_table_check_pmd_clear (struct mm_struct *mm,unsigned long addr, +pmd_t pmd) {if (&init_mm == mm)return ;
@@ -231,7 +232,7 @@ void __page_table_check_pmds_set(struct mm_struct *mm, unsigned long addr,page_table_check_pmd_flags (pmd);for (i =0 ; i < nr; i++) -__page_table_check_pmd_clear (mm, *(pmdp + i)); +__page_table_check_pmd_clear (mm, addr +PMD_SIZE * i, *(pmdp + i));if (pmd_user_accessible_page (pmd))page_table_check_set (pmd_pfn (pmd), stride * nr,pmd_write (pmd)); }
Signature · 1 line
2.49.0
PATCH v15 8/13 mm/page_table_check: Reinstate address parameter in [__]page_table_check_pte_clear()
In reply to Andrew DonnellanFrom: Rohan McLure <[email protected]>
This reverts commit aa232204c468 ("mm/page_table_check: remove unused parameter in [__]page_table_check_pte_clear").
Reinstate previously unused parameters for the purpose of supporting powerpc platforms, as many do not encode user/kernel ownership of the page in the pte, but instead in the address of the access.
---
v13: fix an additional occurrence
v15: rebase, fix loop handling
--- arch/arm64/include/asm/pgtable.h | 2 +- arch/riscv/include/asm/pgtable.h | 2 +- arch/x86/include/asm/pgtable.h | 4 ++-- include/linux/page_table_check.h | 11 +++++++---- include/linux/pgtable.h | 4 ++-- mm/page_table_check.c | 7 ++++--- 6 files changed, 17 insertions(+), 13 deletions(-)
arch/arm64/include/asm/pgtable.h +1 -1
@@ -1391,7 +1391,7 @@ static inline pte_t __ptep_get_and_clear_anysz(struct mm_struct *mm,switch (pgsize) {case PAGE_SIZE : -page_table_check_pte_clear (mm, pte); +page_table_check_pte_clear (mm, address, pte);break ;case PMD_SIZE :page_table_check_pmd_clear (mm, address,pte_pmd (pte));
arch/riscv/include/asm/pgtable.h +1 -1
@@ -603,7 +603,7 @@ static inline pte_t ptep_get_and_clear(struct mm_struct *mm, {pte_t pte =__pte (atomic_long_xchg ((atomic_long_t *)ptep,0 )); -page_table_check_pte_clear (mm, pte); +page_table_check_pte_clear (mm, address, pte);return pte; }
arch/x86/include/asm/pgtable.h +2 -2
@@ -1298,7 +1298,7 @@ static inline pte_t ptep_get_and_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep) {pte_t pte =native_ptep_get_and_clear (ptep); -page_table_check_pte_clear (mm, pte); +page_table_check_pte_clear (mm, addr, pte);return pte; }
@@ -1314,7 +1314,7 @@ static inline pte_t ptep_get_and_clear_full(struct mm_struct *mm,* care about updates and native needs no locking */ pte =native_local_ptep_get_and_clear (ptep); -page_table_check_pte_clear (mm, pte); +page_table_check_pte_clear (mm, addr, pte); }else { pte =ptep_get_and_clear (mm, addr, ptep); }
include/linux/page_table_check.h +7 -4
@@ -14,7 +14,8 @@ extern struct static_key_true page_table_check_disabled;extern struct page_ext_operations page_table_check_ops;void __page_table_check_zero (struct page *page,unsigned int order); -void __page_table_check_pte_clear (struct mm_struct *mm,pte_t pte); +void __page_table_check_pte_clear (struct mm_struct *mm,unsigned long addr, +pte_t pte);void __page_table_check_pmd_clear (struct mm_struct *mm,unsigned long addr,pmd_t pmd); void __page_table_check_pud_clear(struct mm_struct *mm, unsigned long addr,
@@ -45,12 +46,13 @@ static inline void page_table_check_free(struct page *page, unsigned int order)__page_table_check_zero (page, order); } -static inline void page_table_check_pte_clear (struct mm_struct *mm,pte_t pte) +static inline void page_table_check_pte_clear (struct mm_struct *mm, +unsigned long addr,pte_t pte) {if (static_branch_likely (&page_table_check_disabled))return ; -__page_table_check_pte_clear (mm, pte); +__page_table_check_pte_clear (mm, addr, pte); } static inline void page_table_check_pmd_clear(struct mm_struct *mm,
@@ -119,7 +121,8 @@ static inline void page_table_check_free(struct page *page, unsigned int order) { } -static inline void page_table_check_pte_clear (struct mm_struct *mm,pte_t pte) +static inline void page_table_check_pte_clear (struct mm_struct *mm, +unsigned long addr,pte_t pte) { }
include/linux/pgtable.h +2 -2
@@ -483,7 +483,7 @@ static inline pte_t ptep_get_and_clear(struct mm_struct *mm, {pte_t pte =ptep_get (ptep);pte_clear (mm, address, ptep); -page_table_check_pte_clear (mm, pte); +page_table_check_pte_clear (mm, address, pte);return pte; }#endif
@@ -542,7 +542,7 @@ static inline void ptep_clear(struct mm_struct *mm, unsigned long addr,* No need for ptep_get_and_clear(): page table check doesn't care about * any bits that could have been set by HW concurrently. */ -page_table_check_pte_clear (mm, pte); +page_table_check_pte_clear (mm, addr, pte); }#ifdef CONFIG_GUP_GET_PXX_LOW_HIGH
mm/page_table_check.c +4 -3
@@ -145,7 +145,8 @@ void __page_table_check_zero(struct page *page, unsigned int order)rcu_read_unlock (); } -void __page_table_check_pte_clear (struct mm_struct *mm,pte_t pte) +void __page_table_check_pte_clear (struct mm_struct *mm,unsigned long addr, +pte_t pte) {if (&init_mm == mm)return ;
@@ -206,7 +207,7 @@ void __page_table_check_ptes_set(struct mm_struct *mm, unsigned long addr,page_table_check_pte_flags (pte);for (i =0 ; i < nr; i++) -__page_table_check_pte_clear (mm,ptep_get (ptep + i)); +__page_table_check_pte_clear (mm, addr +PAGE_SIZE * i,ptep_get (ptep + i));if (pte_user_accessible_page (pte))page_table_check_set (pte_pfn (pte), nr,pte_write (pte)); }
@@ -268,7 +269,7 @@ void __page_table_check_pte_clear_range(struct mm_struct *mm,if (WARN_ON (!ptep))return ;for (i =0 ; i <PTRS_PER_PTE ; i++) { -__page_table_check_pte_clear (mm,ptep_get (ptep)); +__page_table_check_pte_clear (mm, addr,ptep_get (ptep)); addr +=PAGE_SIZE ; ptep++; }
Signature · 1 line
2.49.0
PATCH v15 9/13 mm: Provide address parameter to p{te,md,ud}_user_accessible_page()
In reply to Andrew DonnellanFrom: Rohan McLure <[email protected]>
On several powerpc platforms, a page table entry may not imply whether the relevant mapping is for userspace or kernelspace. Instead, such platforms infer this by the address which is being accessed.
Add an additional address argument to each of these routines in order to provide support for page table check on powerpc.
---
v15: rebase
--- arch/arm64/include/asm/pgtable.h | 6 +++--- arch/riscv/include/asm/pgtable.h | 6 +++--- arch/x86/include/asm/pgtable.h | 6 +++--- mm/page_table_check.c | 12 ++++++------ 4 files changed, 15 insertions(+), 15 deletions(-)
arch/arm64/include/asm/pgtable.h +3 -3
@@ -1314,17 +1314,17 @@ static inline int pgd_devmap(pgd_t pgd)#endif #ifdef CONFIG_PAGE_TABLE_CHECK -static inline bool pte_user_accessible_page (pte_t pte) +static inline bool pte_user_accessible_page (pte_t pte,unsigned long addr) {return pte_valid (pte) && (pte_user (pte) ||pte_user_exec (pte)); } -static inline bool pmd_user_accessible_page (pmd_t pmd) +static inline bool pmd_user_accessible_page (pmd_t pmd,unsigned long addr) {return pmd_valid (pmd) && !pmd_table (pmd) && (pmd_user (pmd) ||pmd_user_exec (pmd)); } -static inline bool pud_user_accessible_page (pud_t pud) +static inline bool pud_user_accessible_page (pud_t pud,unsigned long addr) {return pud_valid (pud) && !pud_table (pud) && (pud_user (pud) ||pud_user_exec (pud)); }
arch/riscv/include/asm/pgtable.h +3 -3
@@ -834,17 +834,17 @@ static inline void set_pud_at(struct mm_struct *mm, unsigned long addr, }#ifdef CONFIG_PAGE_TABLE_CHECK -static inline bool pte_user_accessible_page (pte_t pte) +static inline bool pte_user_accessible_page (pte_t pte,unsigned long addr) {return pte_present (pte) &&pte_user (pte); } -static inline bool pmd_user_accessible_page (pmd_t pmd) +static inline bool pmd_user_accessible_page (pmd_t pmd,unsigned long addr) {return pmd_leaf (pmd) &&pmd_user (pmd); } -static inline bool pud_user_accessible_page (pud_t pud) +static inline bool pud_user_accessible_page (pud_t pud,unsigned long addr) {return pud_leaf (pud) &&pud_user (pud); }
arch/x86/include/asm/pgtable.h +3 -3
@@ -1726,17 +1726,17 @@ static inline bool arch_has_hw_nonleaf_pmd_young(void)#endif #ifdef CONFIG_PAGE_TABLE_CHECK -static inline bool pte_user_accessible_page (pte_t pte) +static inline bool pte_user_accessible_page (pte_t pte,unsigned long addr) {return (pte_val (pte) & _PAGE_PRESENT) && (pte_val (pte) & _PAGE_USER); } -static inline bool pmd_user_accessible_page (pmd_t pmd) +static inline bool pmd_user_accessible_page (pmd_t pmd,unsigned long addr) {return pmd_leaf (pmd) && (pmd_val (pmd) & _PAGE_PRESENT) && (pmd_val (pmd) & _PAGE_USER); } -static inline bool pud_user_accessible_page (pud_t pud) +static inline bool pud_user_accessible_page (pud_t pud,unsigned long addr) {return pud_leaf (pud) && (pud_val (pud) & _PAGE_PRESENT) && (pud_val (pud) & _PAGE_USER); }
mm/page_table_check.c +6 -6
@@ -151,7 +151,7 @@ void __page_table_check_pte_clear(struct mm_struct *mm, unsigned long addr,if (&init_mm == mm)return ; -if (pte_user_accessible_page (pte)) { +if (pte_user_accessible_page (pte, addr)) {page_table_check_clear (pte_pfn (pte),PAGE_SIZE >>PAGE_SHIFT ); } }
@@ -163,7 +163,7 @@ void __page_table_check_pmd_clear(struct mm_struct *mm, unsigned long addr,if (&init_mm == mm)return ; -if (pmd_user_accessible_page (pmd)) { +if (pmd_user_accessible_page (pmd, addr)) {page_table_check_clear (pmd_pfn (pmd),PMD_SIZE >>PAGE_SHIFT ); } }
@@ -175,7 +175,7 @@ void __page_table_check_pud_clear(struct mm_struct *mm, unsigned long addr,if (&init_mm == mm)return ; -if (pud_user_accessible_page (pud)) { +if (pud_user_accessible_page (pud, addr)) {page_table_check_clear (pud_pfn (pud),PUD_SIZE >>PAGE_SHIFT ); } }
@@ -208,7 +208,7 @@ void __page_table_check_ptes_set(struct mm_struct *mm, unsigned long addr,for (i =0 ; i < nr; i++)__page_table_check_pte_clear (mm, addr +PAGE_SIZE * i,ptep_get (ptep + i)); -if (pte_user_accessible_page (pte)) +if (pte_user_accessible_page (pte, addr))page_table_check_set (pte_pfn (pte), nr,pte_write (pte)); }EXPORT_SYMBOL (__page_table_check_ptes_set);
@@ -234,7 +234,7 @@ void __page_table_check_pmds_set(struct mm_struct *mm, unsigned long addr,for (i =0 ; i < nr; i++)__page_table_check_pmd_clear (mm, addr +PMD_SIZE * i, *(pmdp + i)); -if (pmd_user_accessible_page (pmd)) +if (pmd_user_accessible_page (pmd, addr))page_table_check_set (pmd_pfn (pmd), stride * nr,pmd_write (pmd)); }EXPORT_SYMBOL (__page_table_check_pmds_set);
@@ -250,7 +250,7 @@ void __page_table_check_puds_set(struct mm_struct *mm, unsigned long addr,for (i =0 ; i < nr; i++)__page_table_check_pud_clear (mm, addr +PUD_SIZE * i, *(pudp + i)); -if (pud_user_accessible_page (pud)) +if (pud_user_accessible_page (pud, addr))page_table_check_set (pud_pfn (pud), stride * nr,pud_write (pud)); }EXPORT_SYMBOL (__page_table_check_puds_set);
Signature · 1 line
2.49.0
PATCH v15 10/13 powerpc: mm: Add pud_pfn() stub
In reply to Andrew DonnellanFrom: Rohan McLure <[email protected]>
The page table check feature requires that pud_pfn() be defined on each consuming architecture.
On powerpc, only book3s64 platforms allow for hugepages at this upper level, and thus only book3s64 currently defines pud_pfn().
The page table check code that calls pud_pfn() is gated by a call to pud_user_accessible_page(), which will always return false on any platform other than book3s64.
Add a stub implementation of pud_pfn() for all platforms other than book3s64. As it will never actually be used and should always be compiled out, raise a BUILD_BUG() error.
---
v15: clarify commit message
--- arch/powerpc/include/asm/pgtable.h | 9 +++++++++ 1 file changed, 9 insertions(+)
arch/powerpc/include/asm/pgtable.h +9 -0
@@ -214,6 +214,15 @@ static inline bool arch_supports_memmap_on_memory(unsigned long vmemmap_size)#endif /* CONFIG_PPC64 */ +#ifndef pud_pfn +#define pud_pfn pud_pfn +static inline int pud_pfn (pud_t pud) +{ +BUILD_BUG (); +return 0 ; +} +#endif +#endif /* __ASSEMBLY__ */ #endif /* _ASM_POWERPC_PGTABLE_H */
Signature · 1 line
2.49.0
PATCH v15 11/13 powerpc: mm: Implement *_user_accessible_page() for ptes
In reply to Andrew DonnellanFrom: Rohan McLure <[email protected]>
Page table checking depends on architectures providing an implementation of p{te,md,ud}_user_accessible_page. With refactorisations made on powerpc/mm, the pte_access_permitted() and similar methods verify whether a userland page is accessible with the required permissions.
Since page table checking is the only user of p{te,md,ud}_user_accessible_page(), implement these for all platforms, using some of the same preliminary checks taken by pte_access_permitted() on that platform.
Since commit 8e9bd41e4ce1 ("powerpc/nohash: Replace pte_user() by pte_read()") pte_user() is no longer required to be present on all platforms as it may be equivalent to or implied by pte_read(). Hence implementations of pte_user_accessible_page() are specialised.
--- arch/powerpc/include/asm/book3s/32/pgtable.h | 5 +++++ arch/powerpc/include/asm/book3s/64/pgtable.h | 17 +++++++++++++++++ arch/powerpc/include/asm/nohash/pgtable.h | 5 +++++ arch/powerpc/include/asm/pgtable.h | 8 ++++++++ 4 files changed, 35 insertions(+)
arch/powerpc/include/asm/book3s/32/pgtable.h +5 -0
@@ -437,6 +437,11 @@ static inline bool pte_access_permitted(pte_t pte, bool write)return true; } +static inline bool pte_user_accessible_page (pte_t pte,unsigned long addr) +{ +return pte_present (pte) && !is_kernel_addr (addr); +} +/* Conversion functions: convert a page and protection to a page entry, * and a page entry and page directory to the page they refer to. *
arch/powerpc/include/asm/book3s/64/pgtable.h +17 -0
@@ -540,6 +540,11 @@ static inline bool pte_access_permitted(pte_t pte, bool write)return arch_pte_access_permitted (pte_val (pte), write,0 ); } +static inline bool pte_user_accessible_page (pte_t pte,unsigned long addr) +{ +return pte_present (pte) &&pte_user (pte); +} +/* * Conversion functions: convert a page and protection to a page entry, * and a page entry and page directory to the page they refer to.
@@ -1430,5 +1435,17 @@ static inline bool is_pte_rw_upgrade(unsigned long old_val, unsigned long new_vareturn false; } +#define pmd_user_accessible_page pmd_user_accessible_page +static inline bool pmd_user_accessible_page (pmd_t pmd,unsigned long addr) +{ +return pmd_leaf (pmd) &&pte_user_accessible_page (pmd_pte (pmd), addr); +} + +#define pud_user_accessible_page pud_user_accessible_page +static inline bool pud_user_accessible_page (pud_t pud,unsigned long addr) +{ +return pud_leaf (pud) &&pte_user_accessible_page (pud_pte (pud), addr); +} +#endif /* __ASSEMBLY__ */ #endif /* _ASM_POWERPC_BOOK3S_64_PGTABLE_H_ */
arch/powerpc/include/asm/nohash/pgtable.h +5 -0
@@ -243,6 +243,11 @@ static inline bool pte_access_permitted(pte_t pte, bool write)return true; } +static inline bool pte_user_accessible_page (pte_t pte,unsigned long addr) +{ +return pte_present (pte) && !is_kernel_addr (addr); +} +/* Conversion functions: convert a page and protection to a page entry, * and a page entry and page directory to the page they refer to. *
arch/powerpc/include/asm/pgtable.h +8 -0
@@ -223,6 +223,14 @@ static inline int pud_pfn(pud_t pud) }#endif +#ifndef pmd_user_accessible_page +#define pmd_user_accessible_page (pmd, addr) false +#endif + +#ifndef pud_user_accessible_page +#define pud_user_accessible_page (pud, addr) false +#endif +#endif /* __ASSEMBLY__ */ #endif /* _ASM_POWERPC_PGTABLE_H */
Signature · 1 line
2.49.0
PATCH v15 12/13 powerpc: mm: Use set_pte_at_unchecked() for internal usages
In reply to Andrew DonnellanFrom: Rohan McLure <[email protected]>
In the new set_ptes() API, set_pte_at() (a special case of set_ptes()) is intended to be instrumented by the page table check facility. There are however several other routines that constitute the API for setting page table entries, including set_pmd_at() among others. Such routines are themselves implemented in terms of set_ptes_at().
A future patch providing support for page table checking on powerpc must take care to avoid duplicate calls to page_table_check_p{te,md,ud}_set(). Allow for assignment of pte entries without instrumentation through the set_pte_at_unchecked() routine introduced in this patch.
Cause API-facing routines that call set_pte_at() to instead call set_pte_at_unchecked(), which will remain uninstrumented by page table check. set_ptes() is itself implemented by calls to __set_pte_at(), so this eliminates redundant code.
---
v13: don't use the unchecked version for early-boot kernel mappings (Pasha)
--- arch/powerpc/include/asm/pgtable.h | 2 ++ arch/powerpc/mm/book3s64/pgtable.c | 6 +++--- arch/powerpc/mm/book3s64/radix_pgtable.c | 6 +++--- arch/powerpc/mm/pgtable.c | 8 ++++++++ 4 files changed, 16 insertions(+), 6 deletions(-)
arch/powerpc/include/asm/pgtable.h +2 -0
@@ -46,6 +46,8 @@ struct mm_struct;void set_ptes (struct mm_struct *mm,unsigned long addr,pte_t *ptep,pte_t pte,unsigned int nr);#define set_ptes set_ptes +void set_pte_at_unchecked (struct mm_struct *mm,unsigned long addr, +pte_t *ptep,pte_t pte);#define update_mmu_cache (vma, addr, ptep) \ update_mmu_cache_range(NULL, vma, addr, ptep, 1)
arch/powerpc/mm/book3s64/pgtable.c +3 -3
@@ -128,7 +128,7 @@ void set_pmd_at(struct mm_struct *mm, unsigned long addr,WARN_ON (!(pmd_leaf (pmd)));#endif trace_hugepage_set_pmd (addr,pmd_val (pmd)); -return set_pte_at (mm, addr,pmdp_ptep (pmdp),pmd_pte (pmd)); +return set_pte_at_unchecked (mm, addr,pmdp_ptep (pmdp),pmd_pte (pmd)); } void set_pud_at(struct mm_struct *mm, unsigned long addr,
@@ -145,7 +145,7 @@ void set_pud_at(struct mm_struct *mm, unsigned long addr,WARN_ON (!(pud_leaf (pud)));#endif trace_hugepage_set_pud (addr,pud_val (pud)); -return set_pte_at (mm, addr,pudp_ptep (pudp),pud_pte (pud)); +return set_pte_at_unchecked (mm, addr,pudp_ptep (pudp),pud_pte (pud)); }static void do_serialize (void *arg)
@@ -551,7 +551,7 @@ void ptep_modify_prot_commit(struct vm_area_struct *vma, unsigned long addr,if (radix_enabled ())return radix__ptep_modify_prot_commit (vma, addr, ptep, old_pte, pte); -set_pte_at (vma->vm_mm, addr, ptep, pte); +set_pte_at_unchecked (vma->vm_mm, addr, ptep, pte); }#ifdef CONFIG_TRANSPARENT_HUGEPAGE
arch/powerpc/mm/book3s64/radix_pgtable.c +3 -3
@@ -1600,7 +1600,7 @@ void radix__ptep_modify_prot_commit(struct vm_area_struct *vma, (atomic_read(&mm->context.copros) > 0))radix__flush_tlb_page (vma, addr); -set_pte_at (mm, addr, ptep, pte); +set_pte_at_unchecked (mm, addr, ptep, pte); }int pud_set_huge (pud_t *pud,phys_addr_t addr,pgprot_t prot)
@@ -1611,7 +1611,7 @@ int pud_set_huge(pud_t *pud, phys_addr_t addr, pgprot_t prot)if (!radix_enabled ())return 0 ; -set_pte_at (&init_mm,0 /* radix unused */ , ptep, new_pud); +set_pte_at_unchecked (&init_mm,0 /* radix unused */ , ptep, new_pud);return 1 ; }
@@ -1658,7 +1658,7 @@ int pmd_set_huge(pmd_t *pmd, phys_addr_t addr, pgprot_t prot)if (!radix_enabled ())return 0 ; -set_pte_at (&init_mm,0 /* radix unused */ , ptep, new_pmd); +set_pte_at_unchecked (&init_mm,0 /* radix unused */ , ptep, new_pmd);return 1 ; }
arch/powerpc/mm/pgtable.c +8 -0
@@ -224,6 +224,14 @@ void set_ptes(struct mm_struct *mm, unsigned long addr, pte_t *ptep, } } +void set_pte_at_unchecked (struct mm_struct *mm,unsigned long addr, +pte_t *ptep,pte_t pte) +{ +VM_WARN_ON (pte_hw_valid (*ptep) && !pte_protnone (*ptep)); + pte =set_pte_filter (pte, addr); +__set_pte_at (mm, addr, ptep, pte,0 ); +} +void unmap_kernel_page (unsigned long va) {pmd_t *pmdp =pmd_off_k (va);
Signature · 1 line
2.49.0
PATCH v15 13/13 powerpc: mm: Support page table check
In reply to Andrew DonnellanFrom: Rohan McLure <[email protected]>
On creation and clearing of a page table mapping, instrument such calls by invoking page_table_check_pte_set and page_table_check_pte_clear respectively. These calls serve as a sanity check against illegal mappings.
Enable ARCH_SUPPORTS_PAGE_TABLE_CHECK for all platforms.
See also:
riscv support in commit 3fee229a8eb9 ("riscv/mm: enable ARCH_SUPPORTS_PAGE_TABLE_CHECK")
arm64 in commit 42b2547137f5 ("arm64/mm: enable ARCH_SUPPORTS_PAGE_TABLE_CHECK")x86_64 in commit d283d422c6c4 ("x86: mm: add x86_64 support for page table check")
--- arch/powerpc/Kconfig | 1 + arch/powerpc/include/asm/book3s/32/pgtable.h | 7 ++- arch/powerpc/include/asm/book3s/64/pgtable.h | 45 +++++++++++++++----- arch/powerpc/include/asm/nohash/pgtable.h | 8 +++- arch/powerpc/mm/book3s64/hash_pgtable.c | 4 ++ arch/powerpc/mm/book3s64/pgtable.c | 11 +++-- arch/powerpc/mm/book3s64/radix_pgtable.c | 3 ++ arch/powerpc/mm/pgtable.c | 4 ++ 8 files changed, 68 insertions(+), 15 deletions(-)
arch/powerpc/Kconfig +1 -0
@@ -173,6 +173,7 @@ config PPC select ARCH_STACKWALK select ARCH_SUPPORTS_ATOMIC_RMW select ARCH_SUPPORTS_DEBUG_PAGEALLOC if PPC_BOOK3S || PPC_8xx + select ARCH_SUPPORTS_PAGE_TABLE_CHECK select ARCH_USE_BUILTIN_BSWAP select ARCH_USE_CMPXCHG_LOCKREF if PPC64 select ARCH_USE_MEMTEST
arch/powerpc/include/asm/book3s/32/pgtable.h +6 -1
@@ -202,6 +202,7 @@ void unmap_kernel_page(unsigned long va);#ifndef __ASSEMBLY__#include <linux/sched.h> #include <linux/threads.h> +#include <linux/page_table_check.h> /* Bits to mask out from a PGD to get to the PUD page */ #define PGD_MASKED_BITS 0
@@ -315,7 +316,11 @@ static inline int __ptep_test_and_clear_young(struct mm_struct *mm,static inline pte_t ptep_get_and_clear (struct mm_struct *mm,unsigned long addr,pte_t *ptep) { -return __pte (pte_update (mm, addr, ptep, ~_PAGE_HASHPTE,0 ,0 )); +pte_t old_pte =__pte (pte_update (mm, addr, ptep, ~_PAGE_HASHPTE,0 ,0 )); + +page_table_check_pte_clear (mm, addr, old_pte); + +return old_pte; }#define __HAVE_ARCH_PTEP_SET_WRPROTECT
arch/powerpc/include/asm/book3s/64/pgtable.h +35 -10
79 lines not on this page · raw message
arch/powerpc/include/asm/nohash/pgtable.h +7 -1
@@ -29,6 +29,8 @@ static inline pte_basic_t pte_update(struct mm_struct *mm, unsigned long addr, p#ifndef __ASSEMBLY__ +#include <linux/page_table_check.h> +extern int icache_44x_need_flush;#ifndef pte_huge_size
@@ -122,7 +124,11 @@ static inline void ptep_set_wrprotect(struct mm_struct *mm, unsigned long addr,static inline pte_t ptep_get_and_clear (struct mm_struct *mm,unsigned long addr,pte_t *ptep) { -return __pte (pte_update (mm, addr, ptep, ~0UL ,0 ,0 )); +pte_t old_pte =__pte (pte_update (mm, addr, ptep, ~0UL ,0 ,0 )); + +page_table_check_pte_clear (mm, addr, old_pte); + +return old_pte; }#define __HAVE_ARCH_PTEP_GET_AND_CLEAR
arch/powerpc/mm/book3s64/hash_pgtable.c +4 -0
@@ -8,6 +8,7 @@#include <linux/sched.h> #include <linux/mm_types.h> #include <linux/mm.h> +#include <linux/page_table_check.h> #include <linux/stop_machine.h> #include <asm/sections.h>
@@ -231,6 +232,9 @@ pmd_t hash__pmdp_collapse_flush(struct vm_area_struct *vma, unsigned long addres pmd = *pmdp;pmd_clear (pmdp); + +page_table_check_pmd_clear (vma->vm_mm, address, pmd); +/* * Wait for all pending hash_page to finish. This is needed * in case of subpage collapse. When we collapse normal pages
arch/powerpc/mm/book3s64/pgtable.c +8 -3
@@ -10,6 +10,7 @@#include <linux/pkeys.h> #include <linux/debugfs.h> #include <linux/proc_fs.h> +#include <linux/page_table_check.h> #include <asm/pgalloc.h> #include <asm/tlb.h>
@@ -128,6 +129,7 @@ void set_pmd_at(struct mm_struct *mm, unsigned long addr,WARN_ON (!(pmd_leaf (pmd)));#endif trace_hugepage_set_pmd (addr,pmd_val (pmd)); +page_table_check_pmd_set (mm, addr, pmdp, pmd);return set_pte_at_unchecked (mm, addr,pmdp_ptep (pmdp),pmd_pte (pmd)); }
@@ -145,6 +147,7 @@ void set_pud_at(struct mm_struct *mm, unsigned long addr,WARN_ON (!(pud_leaf (pud)));#endif trace_hugepage_set_pud (addr,pud_val (pud)); +page_table_check_pud_set (mm, addr, pudp, pud);return set_pte_at_unchecked (mm, addr,pudp_ptep (pudp),pud_pte (pud)); }
@@ -180,12 +183,14 @@ void serialize_against_pte_lookup(struct mm_struct *mm)pmd_t pmdp_invalidate (struct vm_area_struct *vma,unsigned long address,pmd_t *pmdp) { -unsigned long old_pmd; +pmd_t old_pmd;VM_WARN_ON_ONCE (!pmd_present (*pmdp)); - old_pmd =pmd_hugepage_update (vma->vm_mm, address, pmdp, _PAGE_PRESENT, _PAGE_INVALID); + old_pmd =__pmd (pmd_hugepage_update (vma->vm_mm, address, pmdp, _PAGE_PRESENT, _PAGE_INVALID));flush_pmd_tlb_range (vma, address, address +HPAGE_PMD_SIZE ); -return __pmd (old_pmd); +page_table_check_pmd_clear (vma->vm_mm, address, old_pmd); + +return old_pmd; } pud_t pudp_invalidate(struct vm_area_struct *vma, unsigned long address,
arch/powerpc/mm/book3s64/radix_pgtable.c +3 -0
@@ -14,6 +14,7 @@#include <linux/of.h> #include <linux/of_fdt.h> #include <linux/mm.h> +#include <linux/page_table_check.h> #include <linux/hugetlb.h> #include <linux/string_helpers.h> #include <linux/memory.h>
@@ -1468,6 +1469,8 @@ pmd_t radix__pmdp_collapse_flush(struct vm_area_struct *vma, unsigned long addre pmd = *pmdp;pmd_clear (pmdp); +page_table_check_pmd_clear (vma->vm_mm, address, pmd); +radix__flush_tlb_collapsed_pmd (vma->vm_mm, address);return pmd;
arch/powerpc/mm/pgtable.c +4 -0
@@ -22,6 +22,7 @@#include <linux/mm.h> #include <linux/percpu.h> #include <linux/hardirq.h> +#include <linux/page_table_check.h> #include <linux/hugetlb.h> #include <asm/tlbflush.h> #include <asm/tlb.h>
@@ -206,6 +207,9 @@ void set_ptes(struct mm_struct *mm, unsigned long addr, pte_t *ptep,* and not hw_valid ptes. Hence there is no translation cache flush * involved that need to be batched. */ + +page_table_check_ptes_set (mm, addr, ptep, pte, nr); +for (;;) {/*
Signature · 1 line
2.49.0