move pagetable_*_dtor() to __tlb_remove_table()
Subsystems (26)
cover move pagetable_*_dtor() to __tlb_remove_table()
Hi all,
As proposed [1] by Peter Zijlstra below, this patch series aims to move pagetable_*_dtor() into __tlb_remove_table(). This will cleanup pagetable_*_dtor() a bit and more gracefully fix the UAF issue [2] reported by syzbot.
``` Notably: - s390 pud isn't calling the existing pagetable_pud_[cd]tor() - none of the p4d things have pagetable_p4d_[cd]tor() (x86,arm64,s390,riscv) and they have inconsistent accounting - while much of the _ctor calls are in generic code, many of the _dtor calls are in arch code for hysterial raisins, this could easily be fixed - if we fix ptlock_free() to handle NULL, then all the _dtor() functions can use it, and we can observe they're all identical and can be folded after all that cleanup, you can move the _dtor from *_free_tlb() into tlb_remove_table() -- which for the above case, would then have it called from __tlb_remove_table_free(). ```
And hi Andrew, I developed the code based on the latest linux-next, so I reverted the "mm: pgtable: make ptlock be freed by RCU" first. Once the review of this patch series is completed, the "mm: pgtable: make ptlock be freed by RCU" can be dropped directly from mm tree, and this revert patch will not be needed.
This series is based on next-20241213. And I tested this patch series on x86 and only cross-compiled it on arm[64], powerpc, riscv, s390 and sparc.
Comments and suggestions are welcome!
Thanks,
Qi
[1]. https://lore.kernel.org/all/[email protected]/
[2]. https://lore.kernel.org/all/[email protected]/
Qi Zheng (12):
Revert "mm: pgtable: make ptlock be freed by RCU"
mm: pgtable: introduce generic p4d_alloc_one() and p4d_free()
arm64: pgtable: use mmu gather to free p4d level page table
s390: pgtable: add statistics for PUD and P4D level page table
mm: pgtable: introduce pagetable_dtor()
arm: pgtable: move pagetable_dtor() to __tlb_remove_table()
arm64: pgtable: move pagetable_dtor() to __tlb_remove_table()
riscv: pgtable: move pagetable_dtor() to __tlb_remove_table()
x86: pgtable: move pagetable_dtor() to __tlb_remove_table()
s390: pgtable: also move pagetable_dtor() of PxD to
__tlb_remove_table()
mm: pgtable: introduce generic __tlb_remove_table()
mm: pgtable: move __tlb_remove_table_one() in x86 to generic file
Documentation/mm/split_page_table_lock.rst | 4 +- arch/arm/include/asm/tlb.h | 10 ---- arch/arm64/include/asm/pgalloc.h | 17 +++--- arch/arm64/include/asm/tlb.h | 21 +++++--- arch/csky/include/asm/pgalloc.h | 2 +- arch/hexagon/include/asm/pgalloc.h | 2 +- arch/loongarch/include/asm/pgalloc.h | 2 +- arch/m68k/include/asm/mcf_pgalloc.h | 4 +- arch/m68k/include/asm/sun3_pgalloc.h | 2 +- arch/m68k/mm/motorola.c | 2 +- arch/mips/include/asm/pgalloc.h | 2 +- arch/nios2/include/asm/pgalloc.h | 2 +- arch/openrisc/include/asm/pgalloc.h | 2 +- arch/powerpc/include/asm/tlb.h | 1 + arch/powerpc/mm/book3s64/mmu_context.c | 2 +- arch/powerpc/mm/book3s64/pgtable.c | 2 +- arch/powerpc/mm/pgtable-frag.c | 4 +- arch/riscv/include/asm/pgalloc.h | 51 ++++++++---------- arch/riscv/include/asm/tlb.h | 18 ------- arch/riscv/mm/init.c | 4 +- arch/s390/include/asm/pgalloc.h | 31 +++++++---- arch/s390/include/asm/tlb.h | 37 ++++++------- arch/s390/mm/pgalloc.c | 21 ++------ arch/sh/include/asm/pgalloc.h | 2 +- arch/sparc/include/asm/tlb_32.h | 1 + arch/sparc/include/asm/tlb_64.h | 1 + arch/sparc/mm/init_64.c | 2 +- arch/sparc/mm/srmmu.c | 2 +- arch/um/include/asm/pgalloc.h | 6 +-- arch/x86/include/asm/pgalloc.h | 16 +++--- arch/x86/include/asm/tlb.h | 33 ------------ arch/x86/kernel/paravirt.c | 1 + arch/x86/mm/pgtable.c | 13 ++--- include/asm-generic/pgalloc.h | 61 ++++++++++++++++++++-- include/asm-generic/tlb.h | 15 +++++- include/linux/mm.h | 44 +++++----------- include/linux/mm_types.h | 9 +--- mm/memory.c | 23 +++----- mm/mmu_gather.c | 20 ++++++- 39 files changed, 233 insertions(+), 259 deletions(-)
Signature · 1 line
2.20.1
PATCH 1/12 Revert "mm: pgtable: make ptlock be freed by RCU"
This reverts commit 2f3443770437e49abc39af26962d293851cbab6d.
--- include/linux/mm.h | 2 +- include/linux/mm_types.h | 9 +-------- mm/memory.c | 22 ++++++---------------- 3 files changed, 8 insertions(+), 25 deletions(-)
include/linux/mm.h +1 -1
@@ -2988,7 +2988,7 @@ void ptlock_free(struct ptdesc *ptdesc);static inline spinlock_t *ptlock_ptr (struct ptdesc *ptdesc) { -return &(ptdesc->ptl->ptl); +return ptdesc->ptl; }#else /* ALLOC_SPLIT_PTLOCKS */ static inline void ptlock_cache_init (void )
include/linux/mm_types.h +1 -8
@@ -434,13 +434,6 @@ FOLIO_MATCH(flags, _flags_2a);FOLIO_MATCH (compound_head, _head_2a);#undef FOLIO_MATCH -#if ALLOC_SPLIT_PTLOCKS -struct pt_lock { -spinlock_t ptl; -struct rcu_head rcu; -}; -#endif -/** * struct ptdesc - Memory descriptor for page tables. * @__page_flags: Same as page flags. Powerpc only.
@@ -485,7 +478,7 @@ struct ptdesc {union {unsigned long _pt_pad_2;#if ALLOC_SPLIT_PTLOCKS -struct pt_lock *ptl; +spinlock_t *ptl;#else spinlock_t ptl;#endif
mm/memory.c +6 -16
@@ -7041,34 +7041,24 @@ static struct kmem_cache *page_ptl_cachep;void __init ptlock_cache_init (void ) { - page_ptl_cachep =kmem_cache_create ("page->ptl" ,sizeof (struct pt_lock ),0 , + page_ptl_cachep =kmem_cache_create ("page->ptl" ,sizeof (spinlock_t),0 ,SLAB_PANIC ,NULL ); }bool ptlock_alloc (struct ptdesc *ptdesc) { -struct pt_lock *pt_lock; +spinlock_t *ptl; - pt_lock =kmem_cache_alloc (page_ptl_cachep,GFP_KERNEL ); -if (!pt_lock) + ptl =kmem_cache_alloc (page_ptl_cachep,GFP_KERNEL ); +if (!ptl)return false; - ptdesc->ptl = pt_lock; + ptdesc->ptl = ptl;return true; } -static void ptlock_free_rcu (struct rcu_head *head) -{ -struct pt_lock *pt_lock; - - pt_lock = container_of(head, struct pt_lock, rcu); -kmem_cache_free (page_ptl_cachep, pt_lock); -} -void ptlock_free (struct ptdesc *ptdesc) { -struct pt_lock *pt_lock = ptdesc->ptl; - -call_rcu (&pt_lock->rcu, ptlock_free_rcu); +kmem_cache_free (page_ptl_cachep, ptdesc->ptl); }#endif
Signature · 1 line
2.20.1
PATCH 2/12 mm: pgtable: introduce generic p4d_alloc_one() and p4d_free()
In reply to Qi ZhengSeveral architectures (arm64, riscv, x86) define p4d_alloc_one() as a wrapper for get_zeroed_page() and p4d_free() as a wrapper for free_page().
For these architectures, provide a generic implementation in asm-generic/pgalloc.h and convert them to use it. And like other levels of page tables, add statistics for P4D level page table.
For s390, it also defines p4d_alloc_one() and p4d_free(), but it uses its own logic, so skip it.
--- arch/arm64/include/asm/pgalloc.h | 15 ++++----- arch/riscv/include/asm/pgalloc.h | 25 ++++++--------- arch/x86/include/asm/pgalloc.h | 16 ++++------ arch/x86/mm/pgtable.c | 3 ++ include/asm-generic/pgalloc.h | 55 ++++++++++++++++++++++++++++++++ include/linux/mm.h | 16 ++++++++++ 6 files changed, 98 insertions(+), 32 deletions(-)
arch/arm64/include/asm/pgalloc.h +7 -8
@@ -15,6 +15,8 @@#define __HAVE_ARCH_PGD_FREE#define __HAVE_ARCH_PUD_FREE +#define __HAVE_ARCH_P4D_ALLOC_ONE +#define __HAVE_ARCH_P4D_FREE#include <asm-generic/pgalloc.h> #define PGD_SIZE (PTRS_PER_PGD * sizeof(pgd_t))
@@ -87,19 +89,16 @@ static inline void pgd_populate(struct mm_struct *mm, pgd_t *pgdp, p4d_t *p4dp)static inline p4d_t *p4d_alloc_one (struct mm_struct *mm,unsigned long addr) { -gfp_t gfp =GFP_PGTABLE_USER ; +if (!pgtable_l5_enabled ()) +return NULL ; -if (mm == &init_mm) - gfp =GFP_PGTABLE_KERNEL ; -return (p4d_t *)get_zeroed_page (gfp); +return __p4d_alloc_one (mm, addr); }static inline void p4d_free (struct mm_struct *mm,p4d_t *p4d) { -if (!pgtable_l5_enabled ()) -return ; -BUG_ON ((unsigned long )p4d & (PAGE_SIZE -1 )); -free_page ((unsigned long )p4d); +if (pgtable_l5_enabled ()) +__p4d_free (mm, p4d); }#define __p4d_free_tlb (tlb, p4d, addr) p4d_free((tlb)->mm, p4d)
arch/riscv/include/asm/pgalloc.h +10 -15
@@ -14,6 +14,8 @@#ifdef CONFIG_MMU #define __HAVE_ARCH_PUD_ALLOC_ONE#define __HAVE_ARCH_PUD_FREE +#define __HAVE_ARCH_P4D_ALLOC_ONE +#define __HAVE_ARCH_P4D_FREE#include <asm-generic/pgalloc.h> static inline void riscv_tlb_remove_ptdesc (struct mmu_gather *tlb,void *pt)
@@ -118,21 +120,10 @@ static inline void __pud_free_tlb(struct mmu_gather *tlb, pud_t *pud,#define p4d_alloc_one p4d_alloc_onestatic inline p4d_t *p4d_alloc_one (struct mm_struct *mm,unsigned long addr) { -if (pgtable_l5_enabled) { -gfp_t gfp =GFP_PGTABLE_USER ; - -if (mm == &init_mm) - gfp =GFP_PGTABLE_KERNEL ; -return (p4d_t *)get_zeroed_page (gfp); - } +if (!pgtable_l5_enabled) +return NULL ; -return NULL ; -} - -static inline void __p4d_free (struct mm_struct *mm,p4d_t *p4d) -{ -BUG_ON ((unsigned long )p4d & (PAGE_SIZE -1 )); -free_page ((unsigned long )p4d); +return __p4d_alloc_one (mm, addr); }#define p4d_free p4d_free
@@ -145,8 +136,12 @@ static inline void p4d_free(struct mm_struct *mm, p4d_t *p4d)static inline void __p4d_free_tlb (struct mmu_gather *tlb,p4d_t *p4d,unsigned long addr) { -if (pgtable_l5_enabled) +if (pgtable_l5_enabled) { +struct ptdesc *ptdesc =virt_to_ptdesc (p4d); + +pagetable_p4d_dtor (ptdesc);riscv_tlb_remove_ptdesc (tlb,virt_to_ptdesc (p4d)); + } }#endif /* __PAGETABLE_PMD_FOLDED */
arch/x86/include/asm/pgalloc.h +7 -9
@@ -8,6 +8,8 @@#define __HAVE_ARCH_PTE_ALLOC_ONE#define __HAVE_ARCH_PGD_FREE +#define __HAVE_ARCH_P4D_ALLOC_ONE +#define __HAVE_ARCH_P4D_FREE#include <asm-generic/pgalloc.h> static inline int __paravirt_pgd_alloc (struct mm_struct *mm) {return 0 ; }
@@ -149,20 +151,16 @@ static inline void pgd_populate_safe(struct mm_struct *mm, pgd_t *pgd, p4d_t *p4static inline p4d_t *p4d_alloc_one (struct mm_struct *mm,unsigned long addr) { -gfp_t gfp =GFP_KERNEL_ACCOUNT ; +if (!pgtable_l5_enabled ()) +return NULL ; -if (mm == &init_mm) - gfp &= ~__GFP_ACCOUNT; -return (p4d_t *)get_zeroed_page (gfp); +return __p4d_alloc_one (mm, addr); }static inline void p4d_free (struct mm_struct *mm,p4d_t *p4d) { -if (!pgtable_l5_enabled ()) -return ; - -BUG_ON ((unsigned long )p4d & (PAGE_SIZE -1 )); -free_page ((unsigned long )p4d); +if (pgtable_l5_enabled ()) +return __p4d_free (mm, p4d); }extern void ___p4d_free_tlb (struct mmu_gather *tlb,p4d_t *p4d);
arch/x86/mm/pgtable.c +3 -0
@@ -94,6 +94,9 @@ void ___pud_free_tlb(struct mmu_gather *tlb, pud_t *pud)#if CONFIG_PGTABLE_LEVELS >4 void ___p4d_free_tlb (struct mmu_gather *tlb,p4d_t *p4d) { +struct ptdesc *ptdesc =virt_to_ptdesc (p4d); + +pagetable_p4d_dtor (ptdesc);paravirt_release_p4d (__pa (p4d) >>PAGE_SHIFT );paravirt_tlb_remove_table (tlb,virt_to_page (p4d)); }
include/asm-generic/pgalloc.h +55 -0
61 lines not on this page · raw message
include/linux/mm.h +16 -0
@@ -3237,6 +3237,22 @@ static inline void pagetable_pud_dtor(struct ptdesc *ptdesc)lruvec_stat_sub_folio (folio,NR_PAGETABLE ); } +static inline void pagetable_p4d_ctor (struct ptdesc *ptdesc) +{ +struct folio *folio =ptdesc_folio (ptdesc); + +__folio_set_pgtable (folio); +lruvec_stat_add_folio (folio,NR_PAGETABLE ); +} + +static inline void pagetable_p4d_dtor (struct ptdesc *ptdesc) +{ +struct folio *folio =ptdesc_folio (ptdesc); + +__folio_clear_pgtable (folio); +lruvec_stat_sub_folio (folio,NR_PAGETABLE ); +} +extern void __init pagecache_init (void );extern void free_initmem (void );
Signature · 1 line
2.20.1
PATCH 3/12 arm64: pgtable: use mmu gather to free p4d level page table
In reply to Qi ZhengLike other levels of page tables, also use mmu gather mechanism to free p4d level page table.
--- arch/arm64/include/asm/pgalloc.h | 2 -- arch/arm64/include/asm/tlb.h | 14 ++++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-)
arch/arm64/include/asm/pgalloc.h +0 -2
@@ -100,8 +100,6 @@ static inline void p4d_free(struct mm_struct *mm, p4d_t *p4d)if (pgtable_l5_enabled ()) __p4d_free(mm, p4d); } - -#define __p4d_free_tlb (tlb, p4d, addr) p4d_free((tlb)->mm, p4d)#else static inline void __pgd_populate (pgd_t *pgdp,phys_addr_t p4dp,pgdval_t prot) {
arch/arm64/include/asm/tlb.h +14 -0
@@ -111,4 +111,18 @@ static inline void __pud_free_tlb(struct mmu_gather *tlb, pud_t *pudp, }#endif +#if CONFIG_PGTABLE_LEVELS >4 +static inline void __p4d_free_tlb (struct mmu_gather *tlb,p4d_t *p4dp, +unsigned long addr) +{ +struct ptdesc *ptdesc =virt_to_ptdesc (p4dp); + +if (!pgtable_l5_enabled ()) +return ; + +pagetable_p4d_dtor (ptdesc); +tlb_remove_ptdesc (tlb, ptdesc); +} +#endif +#endif
Signature · 1 line
2.20.1
PATCH 4/12 s390: pgtable: add statistics for PUD and P4D level page table
In reply to Qi ZhengLike PMD and PTE level page table, also add statistics for PUD and P4D page table.
--- arch/s390/include/asm/pgalloc.h | 29 +++++++++++++++++++------- arch/s390/include/asm/tlb.h | 37 +++++++++++++++++---------------- 2 files changed, 40 insertions(+), 26 deletions(-)
arch/s390/include/asm/pgalloc.h +21 -8
@@ -53,29 +53,42 @@ static inline p4d_t *p4d_alloc_one(struct mm_struct *mm, unsigned long address) {unsigned long *table =crst_table_alloc (mm); -if (table) -crst_table_init (table, _REGION2_ENTRY_EMPTY); +if (!table) +return NULL ; +crst_table_init (table, _REGION2_ENTRY_EMPTY); +pagetable_p4d_ctor (virt_to_ptdesc (table)); +return (p4d_t *) table; }static inline void p4d_free (struct mm_struct *mm,p4d_t *p4d) { -if (!mm_p4d_folded (mm)) -crst_table_free (mm, (unsigned long *) p4d); +if (mm_p4d_folded (mm)) +return ; + +pagetable_p4d_dtor (virt_to_ptdesc (p4d)); +crst_table_free (mm, (unsigned long *) p4d); }static inline pud_t *pud_alloc_one (struct mm_struct *mm,unsigned long address) {unsigned long *table =crst_table_alloc (mm); -if (table) -crst_table_init (table, _REGION3_ENTRY_EMPTY); + +if (!table) +return NULL ; +crst_table_init (table, _REGION3_ENTRY_EMPTY); +pagetable_pud_ctor (virt_to_ptdesc (table)); +return (pud_t *) table; }static inline void pud_free (struct mm_struct *mm,pud_t *pud) { -if (!mm_pud_folded (mm)) -crst_table_free (mm, (unsigned long *) pud); +if (mm_pud_folded (mm)) +return ; + +pagetable_pud_dtor (virt_to_ptdesc (pud)); +crst_table_free (mm, (unsigned long *) pud); }static inline pmd_t *pmd_alloc_one (struct mm_struct *mm,unsigned long vmaddr)
arch/s390/include/asm/tlb.h +19 -18
@@ -110,24 +110,6 @@ static inline void pmd_free_tlb(struct mmu_gather *tlb, pmd_t *pmd,tlb_remove_ptdesc (tlb, pmd); } -/* -* p4d_free_tlb frees a pud table and clears the CRSTE for the -* region second table entry from the tlb. -* If the mm uses a four level page table the single p4d is freed -* as the pgd. p4d_free_tlb checks the asce_limit against 8PB -* to avoid the double free of the p4d in this case. -*/ -static inline void p4d_free_tlb (struct mmu_gather *tlb,p4d_t *p4d, -unsigned long address) -{ -if (mm_p4d_folded (tlb->mm)) -return ; -__tlb_adjust_range (tlb, address,PAGE_SIZE ); - tlb->mm->context.flush_mm =1 ; - tlb->freed_tables =1 ; -tlb_remove_ptdesc (tlb, p4d); -} -/* * pud_free_tlb frees a pud table and clears the CRSTE for the * region third table entry from the tlb.
@@ -140,11 +122,30 @@ static inline void pud_free_tlb(struct mmu_gather *tlb, pud_t *pud, {if (mm_pud_folded (tlb->mm))return ; +pagetable_pud_dtor (virt_to_ptdesc (pud)); tlb->mm->context.flush_mm =1 ; tlb->freed_tables =1 ; tlb->cleared_p4ds =1 ;tlb_remove_ptdesc (tlb, pud); } +/* +* p4d_free_tlb frees a p4d table and clears the CRSTE for the +* region second table entry from the tlb. +* If the mm uses a four level page table the single p4d is freed +* as the pgd. p4d_free_tlb checks the asce_limit against 8PB +* to avoid the double free of the p4d in this case. +*/ +static inline void p4d_free_tlb (struct mmu_gather *tlb,p4d_t *p4d, +unsigned long address) +{ +if (mm_p4d_folded (tlb->mm)) +return ; +pagetable_p4d_dtor (virt_to_ptdesc (p4d)); +__tlb_adjust_range (tlb, address,PAGE_SIZE ); + tlb->mm->context.flush_mm =1 ; + tlb->freed_tables =1 ; +tlb_remove_ptdesc (tlb, p4d); +}#endif /* _S390_TLB_H */
Signature · 1 line
2.20.1
PATCH 5/12 mm: pgtable: introduce pagetable_dtor()
In reply to Qi ZhengThe pagetable_p*_dtor() are exactly the same except for the handling of ptlock. If we make ptlock_free() handle the case where ptdesc->ptl is NULL and remove VM_BUG_ON_PAGE() from pmd_ptlock_free(), we can unify pagetable_p*_dtor() into one function. Let's introduce pagetable_dtor() to do this.
Later, pagetable_dtor() will be moved to tlb_remove_ptdesc(), so that ptlock and page table pages can be freed together (regardless of whether RCU is used). This prevents the use-after-free problem where the ptlock is freed immediately but the page table pages is freed later via RCU.
--- Documentation/mm/split_page_table_lock.rst | 4 +- arch/arm/include/asm/tlb.h | 4 +- arch/arm64/include/asm/tlb.h | 8 ++-- arch/csky/include/asm/pgalloc.h | 2 +- arch/hexagon/include/asm/pgalloc.h | 2 +- arch/loongarch/include/asm/pgalloc.h | 2 +- arch/m68k/include/asm/mcf_pgalloc.h | 4 +- arch/m68k/include/asm/sun3_pgalloc.h | 2 +- arch/m68k/mm/motorola.c | 2 +- arch/mips/include/asm/pgalloc.h | 2 +- arch/nios2/include/asm/pgalloc.h | 2 +- arch/openrisc/include/asm/pgalloc.h | 2 +- arch/powerpc/mm/book3s64/mmu_context.c | 2 +- arch/powerpc/mm/book3s64/pgtable.c | 2 +- arch/powerpc/mm/pgtable-frag.c | 4 +- arch/riscv/include/asm/pgalloc.h | 8 ++-- arch/riscv/mm/init.c | 4 +- arch/s390/include/asm/pgalloc.h | 6 +-- arch/s390/include/asm/tlb.h | 6 +-- arch/s390/mm/pgalloc.c | 2 +- arch/sh/include/asm/pgalloc.h | 2 +- arch/sparc/mm/init_64.c | 2 +- arch/sparc/mm/srmmu.c | 2 +- arch/um/include/asm/pgalloc.h | 6 +-- arch/x86/mm/pgtable.c | 12 ++--- include/asm-generic/pgalloc.h | 8 ++-- include/linux/mm.h | 52 ++++------------------ mm/memory.c | 3 +- 28 files changed, 62 insertions(+), 95 deletions(-)
Documentation/mm/split_page_table_lock.rst +2 -2
@@ -62,7 +62,7 @@ Support of split page table lock by an architecture =================================================== There's no need in special enabling of PTE split page table lock: everything -required is done by pagetable_pte_ctor() and pagetable_pte_dtor(), which +required is done by pagetable_pte_ctor() and pagetable_dtor(), which must be called on PTE table allocation / freeing. Make sure the architecture doesn't use slab allocator for page table
@@ -73,7 +73,7 @@ PMD split lock only makes sense if you have more than two page table levels. PMD split lock enabling requires pagetable_pmd_ctor() call on PMD table -allocation and pagetable_pmd_dtor() on freeing. +allocation and pagetable_dtor() on freeing. Allocation usually happens in pmd_alloc_one(), freeing in pmd_free() and pmd_free_tlb(), but make sure you cover all PMD table allocation / freeing
arch/arm/include/asm/tlb.h +2 -2
@@ -41,7 +41,7 @@ __pte_free_tlb(struct mmu_gather *tlb, pgtable_t pte, unsigned long addr) {struct ptdesc *ptdesc =page_ptdesc (pte); -pagetable_pte_dtor (ptdesc); +pagetable_dtor (ptdesc);#ifndef CONFIG_ARM_LPAE /*
@@ -61,7 +61,7 @@ __pmd_free_tlb(struct mmu_gather *tlb, pmd_t *pmdp, unsigned long addr)#ifdef CONFIG_ARM_LPAE struct ptdesc *ptdesc =virt_to_ptdesc (pmdp); -pagetable_pmd_dtor (ptdesc); +pagetable_dtor (ptdesc);tlb_remove_ptdesc (tlb, ptdesc);#endif }
arch/arm64/include/asm/tlb.h +4 -4
@@ -82,7 +82,7 @@ static inline void __pte_free_tlb(struct mmu_gather *tlb, pgtable_t pte, {struct ptdesc *ptdesc =page_ptdesc (pte); -pagetable_pte_dtor (ptdesc); +pagetable_dtor (ptdesc);tlb_remove_ptdesc (tlb, ptdesc); }
@@ -92,7 +92,7 @@ static inline void __pmd_free_tlb(struct mmu_gather *tlb, pmd_t *pmdp, {struct ptdesc *ptdesc =virt_to_ptdesc (pmdp); -pagetable_pmd_dtor (ptdesc); +pagetable_dtor (ptdesc);tlb_remove_ptdesc (tlb, ptdesc); }#endif
@@ -106,7 +106,7 @@ static inline void __pud_free_tlb(struct mmu_gather *tlb, pud_t *pudp,if (!pgtable_l4_enabled ())return ; -pagetable_pud_dtor (ptdesc); +pagetable_dtor (ptdesc); tlb_remove_ptdesc(tlb, ptdesc); }#endif
@@ -120,7 +120,7 @@ static inline void __p4d_free_tlb(struct mmu_gather *tlb, p4d_t *p4dp,if (!pgtable_l5_enabled ())return ; -pagetable_p4d_dtor (ptdesc); +pagetable_dtor (ptdesc); tlb_remove_ptdesc(tlb, ptdesc); }#endif
arch/csky/include/asm/pgalloc.h +1 -1
@@ -63,7 +63,7 @@ static inline pgd_t *pgd_alloc(struct mm_struct *mm)#define __pte_free_tlb (tlb, pte, address) \ do { \ - pagetable_pte_dtor(page_ptdesc(pte)); \ + pagetable_dtor(page_ptdesc(pte)); \ tlb_remove_page_ptdesc(tlb, page_ptdesc(pte)); \ } while (0)
arch/hexagon/include/asm/pgalloc.h +1 -1
@@ -89,7 +89,7 @@ static inline void pmd_populate_kernel(struct mm_struct *mm, pmd_t *pmd,#define __pte_free_tlb (tlb, pte, addr) \ do { \ - pagetable_pte_dtor((page_ptdesc(pte))); \ + pagetable_dtor((page_ptdesc(pte))); \ tlb_remove_page_ptdesc((tlb), (page_ptdesc(pte))); \ } while (0)
arch/loongarch/include/asm/pgalloc.h +1 -1
@@ -57,7 +57,7 @@ static inline pte_t *pte_alloc_one_kernel(struct mm_struct *mm)#define __pte_free_tlb (tlb, pte, address) \ do { \ - pagetable_pte_dtor(page_ptdesc(pte)); \ + pagetable_dtor(page_ptdesc(pte)); \ tlb_remove_page_ptdesc((tlb), page_ptdesc(pte)); \ } while (0)
arch/m68k/include/asm/mcf_pgalloc.h +2 -2
@@ -37,7 +37,7 @@ static inline void __pte_free_tlb(struct mmu_gather *tlb, pgtable_t pgtable, {struct ptdesc *ptdesc =virt_to_ptdesc (pgtable); -pagetable_pte_dtor (ptdesc); +pagetable_dtor (ptdesc);pagetable_free (ptdesc); }
@@ -61,7 +61,7 @@ static inline void pte_free(struct mm_struct *mm, pgtable_t pgtable) {struct ptdesc *ptdesc =virt_to_ptdesc (pgtable); -pagetable_pte_dtor (ptdesc); +pagetable_dtor (ptdesc);pagetable_free (ptdesc); }
arch/m68k/include/asm/sun3_pgalloc.h +1 -1
@@ -19,7 +19,7 @@ extern const char bad_pmd_string[];#define __pte_free_tlb (tlb, pte, addr) \ do { \ - pagetable_pte_dtor(page_ptdesc(pte)); \ + pagetable_dtor(page_ptdesc(pte)); \ tlb_remove_page_ptdesc((tlb), page_ptdesc(pte)); \ } while (0)
arch/m68k/mm/motorola.c +1 -1
@@ -201,7 +201,7 @@ int free_pointer_table(void *table, int type)list_del (dp);mmu_page_dtor ((void *)page);if (type ==TABLE_PTE ) -pagetable_pte_dtor (virt_to_ptdesc ((void *)page)); +pagetable_dtor (virt_to_ptdesc ((void *)page));free_page (page);return 1 ; }else if (ptable_list[type].next != dp) {
arch/mips/include/asm/pgalloc.h +1 -1
@@ -56,7 +56,7 @@ static inline void pgd_free(struct mm_struct *mm, pgd_t *pgd)#define __pte_free_tlb (tlb, pte, address) \ do { \ - pagetable_pte_dtor(page_ptdesc(pte)); \ + pagetable_dtor(page_ptdesc(pte)); \ tlb_remove_page_ptdesc((tlb), page_ptdesc(pte)); \ } while (0)
arch/nios2/include/asm/pgalloc.h +1 -1
@@ -30,7 +30,7 @@ extern pgd_t *pgd_alloc(struct mm_struct *mm);#define __pte_free_tlb (tlb, pte, addr) \ do { \ - pagetable_pte_dtor(page_ptdesc(pte)); \ + pagetable_dtor(page_ptdesc(pte)); \ tlb_remove_page_ptdesc((tlb), (page_ptdesc(pte))); \ } while (0)
arch/openrisc/include/asm/pgalloc.h +1 -1
@@ -68,7 +68,7 @@ extern pte_t *pte_alloc_one_kernel(struct mm_struct *mm);#define __pte_free_tlb (tlb, pte, addr) \ do { \ - pagetable_pte_dtor(page_ptdesc(pte)); \ + pagetable_dtor(page_ptdesc(pte)); \ tlb_remove_page_ptdesc((tlb), (page_ptdesc(pte))); \ } while (0)
arch/powerpc/mm/book3s64/mmu_context.c +1 -1
@@ -253,7 +253,7 @@ static void pmd_frag_destroy(void *pmd_frag) count = ((unsigned long )pmd_frag & ~PAGE_MASK ) >>PMD_FRAG_SIZE_SHIFT ;/* We allow PTE_FRAG_NR fragments from a PTE page */ if (atomic_sub_and_test (PMD_FRAG_NR - count, &ptdesc->pt_frag_refcount)) { -pagetable_pmd_dtor (ptdesc); +pagetable_dtor (ptdesc);pagetable_free (ptdesc); } }
arch/powerpc/mm/book3s64/pgtable.c +1 -1
@@ -477,7 +477,7 @@ void pmd_fragment_free(unsigned long *pmd)BUG_ON (atomic_read (&ptdesc->pt_frag_refcount) <=0 );if (atomic_dec_and_test (&ptdesc->pt_frag_refcount)) { -pagetable_pmd_dtor (ptdesc); +pagetable_dtor (ptdesc);pagetable_free (ptdesc); } }
arch/powerpc/mm/pgtable-frag.c +2 -2
@@ -25,7 +25,7 @@ void pte_frag_destroy(void *pte_frag) count = ((unsigned long )pte_frag & ~PAGE_MASK ) >>PTE_FRAG_SIZE_SHIFT ;/* We allow PTE_FRAG_NR fragments from a PTE page */ if (atomic_sub_and_test (PTE_FRAG_NR - count, &ptdesc->pt_frag_refcount)) { -pagetable_pte_dtor (ptdesc); +pagetable_dtor (ptdesc);pagetable_free (ptdesc); } }
@@ -111,7 +111,7 @@ static void pte_free_now(struct rcu_head *head)struct ptdesc *ptdesc; ptdesc = container_of(head, struct ptdesc, pt_rcu_head); -pagetable_pte_dtor (ptdesc); +pagetable_dtor (ptdesc);pagetable_free (ptdesc); }
arch/riscv/include/asm/pgalloc.h +4 -4
@@ -112,7 +112,7 @@ static inline void __pud_free_tlb(struct mmu_gather *tlb, pud_t *pud,if (pgtable_l4_enabled) {struct ptdesc *ptdesc =virt_to_ptdesc (pud); -pagetable_pud_dtor (ptdesc); +pagetable_dtor (ptdesc);riscv_tlb_remove_ptdesc (tlb, ptdesc); } }
@@ -139,7 +139,7 @@ static inline void __p4d_free_tlb(struct mmu_gather *tlb, p4d_t *p4d,if (pgtable_l5_enabled) {struct ptdesc *ptdesc =virt_to_ptdesc (p4d); -pagetable_p4d_dtor (ptdesc); +pagetable_dtor (ptdesc);riscv_tlb_remove_ptdesc (tlb,virt_to_ptdesc (p4d)); } }
@@ -172,7 +172,7 @@ static inline void __pmd_free_tlb(struct mmu_gather *tlb, pmd_t *pmd, {struct ptdesc *ptdesc =virt_to_ptdesc (pmd); -pagetable_pmd_dtor (ptdesc); +pagetable_dtor (ptdesc);riscv_tlb_remove_ptdesc (tlb, ptdesc); }
@@ -183,7 +183,7 @@ static inline void __pte_free_tlb(struct mmu_gather *tlb, pgtable_t pte, {struct ptdesc *ptdesc =page_ptdesc (pte); -pagetable_pte_dtor (ptdesc); +pagetable_dtor (ptdesc);riscv_tlb_remove_ptdesc (tlb, ptdesc); }#endif /* CONFIG_MMU */
arch/riscv/mm/init.c +2 -2
@@ -1558,7 +1558,7 @@ static void __meminit free_pte_table(pte_t *pte_start, pmd_t *pmd)return ; } -pagetable_pte_dtor (ptdesc); +pagetable_dtor (ptdesc);if (PageReserved (page))free_reserved_page (page);else
@@ -1580,7 +1580,7 @@ static void __meminit free_pmd_table(pmd_t *pmd_start, pud_t *pud, bool is_vmemm }if (!is_vmemmap) -pagetable_pmd_dtor (ptdesc); +pagetable_dtor (ptdesc);if (PageReserved (page))free_reserved_page (page);else
arch/s390/include/asm/pgalloc.h +3 -3
@@ -66,7 +66,7 @@ static inline void p4d_free(struct mm_struct *mm, p4d_t *p4d)if (mm_p4d_folded (mm))return ; -pagetable_p4d_dtor (virt_to_ptdesc (p4d)); +pagetable_dtor (virt_to_ptdesc (p4d));crst_table_free (mm, (unsigned long *) p4d); }
@@ -87,7 +87,7 @@ static inline void pud_free(struct mm_struct *mm, pud_t *pud)if (mm_pud_folded (mm))return ; -pagetable_pud_dtor (virt_to_ptdesc (pud)); +pagetable_dtor (virt_to_ptdesc (pud));crst_table_free (mm, (unsigned long *) pud); }
@@ -109,7 +109,7 @@ static inline void pmd_free(struct mm_struct *mm, pmd_t *pmd) {if (mm_pmd_folded (mm))return ; -pagetable_pmd_dtor (virt_to_ptdesc (pmd)); +pagetable_dtor (virt_to_ptdesc (pmd));crst_table_free (mm, (unsigned long *) pmd); }
arch/s390/include/asm/tlb.h +3 -3
@@ -102,7 +102,7 @@ static inline void pmd_free_tlb(struct mmu_gather *tlb, pmd_t *pmd, {if (mm_pmd_folded (tlb->mm))return ; -pagetable_pmd_dtor (virt_to_ptdesc (pmd)); +pagetable_dtor (virt_to_ptdesc (pmd));__tlb_adjust_range (tlb, address,PAGE_SIZE ); tlb->mm->context.flush_mm =1 ; tlb->freed_tables =1 ;
@@ -122,7 +122,7 @@ static inline void pud_free_tlb(struct mmu_gather *tlb, pud_t *pud, {if (mm_pud_folded (tlb->mm))return ; -pagetable_pud_dtor (virt_to_ptdesc (pud)); +pagetable_dtor (virt_to_ptdesc (pud)); tlb->mm->context.flush_mm =1 ; tlb->freed_tables =1 ; tlb->cleared_p4ds =1 ;
@@ -141,7 +141,7 @@ static inline void p4d_free_tlb(struct mmu_gather *tlb, p4d_t *p4d, {if (mm_p4d_folded (tlb->mm))return ; -pagetable_p4d_dtor (virt_to_ptdesc (p4d)); +pagetable_dtor (virt_to_ptdesc (p4d));__tlb_adjust_range (tlb, address,PAGE_SIZE ); tlb->mm->context.flush_mm =1 ; tlb->freed_tables =1 ;
arch/s390/mm/pgalloc.c +1 -1
@@ -182,7 +182,7 @@ unsigned long *page_table_alloc(struct mm_struct *mm)static void pagetable_pte_dtor_free (struct ptdesc *ptdesc) { -pagetable_pte_dtor (ptdesc); +pagetable_dtor (ptdesc);pagetable_free (ptdesc); }
arch/sh/include/asm/pgalloc.h +1 -1
@@ -34,7 +34,7 @@ static inline void pmd_populate(struct mm_struct *mm, pmd_t *pmd,#define __pte_free_tlb (tlb, pte, addr) \ do { \ - pagetable_pte_dtor(page_ptdesc(pte)); \ + pagetable_dtor(page_ptdesc(pte)); \ tlb_remove_page_ptdesc((tlb), (page_ptdesc(pte))); \ } while (0)
arch/sparc/mm/init_64.c +1 -1
@@ -2915,7 +2915,7 @@ static void __pte_free(pgtable_t pte) {struct ptdesc *ptdesc =virt_to_ptdesc (pte); -pagetable_pte_dtor (ptdesc); +pagetable_dtor (ptdesc);pagetable_free (ptdesc); }
arch/sparc/mm/srmmu.c +1 -1
@@ -372,7 +372,7 @@ void pte_free(struct mm_struct *mm, pgtable_t ptep) page =pfn_to_page (__nocache_pa ((unsigned long )ptep) >>PAGE_SHIFT );spin_lock (&mm->page_table_lock);if (page_ref_dec_return (page) ==1 ) -pagetable_pte_dtor (page_ptdesc (page)); +pagetable_dtor (page_ptdesc (page));spin_unlock (&mm->page_table_lock);srmmu_free_nocache (ptep,SRMMU_PTE_TABLE_SIZE );
arch/um/include/asm/pgalloc.h +3 -3
@@ -27,7 +27,7 @@ extern pgd_t *pgd_alloc(struct mm_struct *);#define __pte_free_tlb (tlb, pte, address) \ do { \ - pagetable_pte_dtor(page_ptdesc(pte)); \ + pagetable_dtor(page_ptdesc(pte)); \ tlb_remove_page_ptdesc((tlb), (page_ptdesc(pte))); \ } while (0)
@@ -35,7 +35,7 @@ do { \#define __pmd_free_tlb (tlb, pmd, address) \ do { \ - pagetable_pmd_dtor(virt_to_ptdesc(pmd)); \ + pagetable_dtor(virt_to_ptdesc(pmd)); \ tlb_remove_page_ptdesc((tlb), virt_to_ptdesc(pmd)); \ } while (0)
@@ -43,7 +43,7 @@ do { \#define __pud_free_tlb (tlb, pud, address) \ do { \ - pagetable_pud_dtor(virt_to_ptdesc(pud)); \ + pagetable_dtor(virt_to_ptdesc(pud)); \ tlb_remove_page_ptdesc((tlb), virt_to_ptdesc(pud)); \ } while (0)
arch/x86/mm/pgtable.c +6 -6
@@ -60,7 +60,7 @@ early_param("userpte", setup_userpte);void ___pte_free_tlb (struct mmu_gather *tlb,struct page *pte) { -pagetable_pte_dtor (page_ptdesc (pte)); +pagetable_dtor (page_ptdesc (pte));paravirt_release_pte (page_to_pfn (pte));paravirt_tlb_remove_table (tlb, pte); }
@@ -77,7 +77,7 @@ void ___pmd_free_tlb(struct mmu_gather *tlb, pmd_t *pmd)#ifdef CONFIG_X86_PAE tlb->need_flush_all =1 ;#endif -pagetable_pmd_dtor (ptdesc); +pagetable_dtor (ptdesc);paravirt_tlb_remove_table (tlb,ptdesc_page (ptdesc)); }
@@ -86,7 +86,7 @@ void ___pud_free_tlb(struct mmu_gather *tlb, pud_t *pud) {struct ptdesc *ptdesc =virt_to_ptdesc (pud); -pagetable_pud_dtor (ptdesc); +pagetable_dtor (ptdesc);paravirt_release_pud (__pa (pud) >>PAGE_SHIFT );paravirt_tlb_remove_table (tlb,virt_to_page (pud)); }
@@ -96,7 +96,7 @@ void ___p4d_free_tlb(struct mmu_gather *tlb, p4d_t *p4d) {struct ptdesc *ptdesc =virt_to_ptdesc (p4d); -pagetable_p4d_dtor (ptdesc); +pagetable_dtor (ptdesc);paravirt_release_p4d (__pa (p4d) >>PAGE_SHIFT );paravirt_tlb_remove_table (tlb,virt_to_page (p4d)); }
@@ -233,7 +233,7 @@ static void free_pmds(struct mm_struct *mm, pmd_t *pmds[], int count)if (pmds[i]) { ptdesc =virt_to_ptdesc (pmds[i]); -pagetable_pmd_dtor (ptdesc); +pagetable_dtor (ptdesc);pagetable_free (ptdesc);mm_dec_nr_pmds (mm); }
@@ -867,7 +867,7 @@ int pud_free_pmd_page(pud_t *pud, unsigned long addr)free_page ((unsigned long )pmd_sv); -pagetable_pmd_dtor (virt_to_ptdesc (pmd)); +pagetable_dtor (virt_to_ptdesc (pmd));free_page ((unsigned long )pmd);return 1 ;
include/asm-generic/pgalloc.h +4 -4
@@ -109,7 +109,7 @@ static inline void pte_free(struct mm_struct *mm, struct page *pte_page) {struct ptdesc *ptdesc =page_ptdesc (pte_page); -pagetable_pte_dtor (ptdesc); +pagetable_dtor (ptdesc);pagetable_free (ptdesc); }
@@ -153,7 +153,7 @@ static inline void pmd_free(struct mm_struct *mm, pmd_t *pmd)struct ptdesc *ptdesc =virt_to_ptdesc (pmd);BUG_ON ((unsigned long )pmd & (PAGE_SIZE -1 )); -pagetable_pmd_dtor (ptdesc); +pagetable_dtor (ptdesc); pagetable_free(ptdesc); }#endif
@@ -202,7 +202,7 @@ static inline void __pud_free(struct mm_struct *mm, pud_t *pud)struct ptdesc *ptdesc =virt_to_ptdesc (pud);BUG_ON ((unsigned long )pud & (PAGE_SIZE -1 )); -pagetable_pud_dtor (ptdesc); +pagetable_dtor (ptdesc);pagetable_free (ptdesc); }
@@ -257,7 +257,7 @@ static inline void __p4d_free(struct mm_struct *mm, p4d_t *p4d)struct ptdesc *ptdesc =virt_to_ptdesc (p4d);BUG_ON ((unsigned long )p4d & (PAGE_SIZE -1 )); -pagetable_p4d_dtor (ptdesc); +pagetable_dtor (ptdesc);pagetable_free (ptdesc); }
include/linux/mm.h +9 -43
94 lines not on this page · raw message
mm/memory.c +2 -1
@@ -7058,7 +7058,8 @@ bool ptlock_alloc(struct ptdesc *ptdesc)void ptlock_free (struct ptdesc *ptdesc) { -kmem_cache_free (page_ptl_cachep, ptdesc->ptl); +if (ptdesc->ptl) +kmem_cache_free (page_ptl_cachep, ptdesc->ptl); }#endif
Signature · 1 line
2.20.1
PATCH 6/12 arm: pgtable: move pagetable_dtor() to __tlb_remove_table()
In reply to Qi ZhengMove pagetable_dtor() to __tlb_remove_table(), so that ptlock and page table pages can be freed together (regardless of whether RCU is used). This prevents the use-after-free problem where the ptlock is freed immediately but the page table pages is freed later via RCU.
Page tables shouldn't have swap cache, so use pagetable_free() instead of free_page_and_swap_cache() to free page table pages.
--- arch/arm/include/asm/tlb.h | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-)
arch/arm/include/asm/tlb.h +4 -5
@@ -26,12 +26,14 @@#else /* !CONFIG_MMU */ -#include <linux/swap.h> #include <asm/tlbflush.h> static inline void __tlb_remove_table (void *_table) { -free_page_and_swap_cache ((struct page *)_table); +struct ptdesc *ptdesc = (struct ptdesc *)_table; + +pagetable_dtor (ptdesc); +pagetable_free (ptdesc); }#include <asm-generic/tlb.h>
@@ -41,8 +43,6 @@ __pte_free_tlb(struct mmu_gather *tlb, pgtable_t pte, unsigned long addr) {struct ptdesc *ptdesc =page_ptdesc (pte); -pagetable_dtor (ptdesc); -#ifndef CONFIG_ARM_LPAE /* * With the classic ARM MMU, a pte page has two corresponding pmd
@@ -61,7 +61,6 @@ __pmd_free_tlb(struct mmu_gather *tlb, pmd_t *pmdp, unsigned long addr)#ifdef CONFIG_ARM_LPAE struct ptdesc *ptdesc =virt_to_ptdesc (pmdp); -pagetable_dtor (ptdesc);tlb_remove_ptdesc (tlb, ptdesc);#endif }
Signature · 1 line
2.20.1
PATCH 7/12 arm64: pgtable: move pagetable_dtor() to __tlb_remove_table()
In reply to Qi ZhengMove pagetable_dtor() to __tlb_remove_table(), so that ptlock and page table pages can be freed together (regardless of whether RCU is used). This prevents the use-after-free problem where the ptlock is freed immediately but the page table pages is freed later via RCU.
Page tables shouldn't have swap cache, so use pagetable_free() instead of free_page_and_swap_cache() to free page table pages.
--- arch/arm64/include/asm/tlb.h | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-)
arch/arm64/include/asm/tlb.h +4 -6
@@ -9,11 +9,13 @@#define __ASM_TLB_H#include <linux/pagemap.h> -#include <linux/swap.h> static inline void __tlb_remove_table (void *_table) { -free_page_and_swap_cache ((struct page *)_table); +struct ptdesc *ptdesc = (struct ptdesc *)_table; + +pagetable_dtor (ptdesc); +pagetable_free (ptdesc); }#define tlb_flush tlb_flush
@@ -82,7 +84,6 @@ static inline void __pte_free_tlb(struct mmu_gather *tlb, pgtable_t pte, {struct ptdesc *ptdesc =page_ptdesc (pte); -pagetable_dtor (ptdesc);tlb_remove_ptdesc (tlb, ptdesc); }
@@ -92,7 +93,6 @@ static inline void __pmd_free_tlb(struct mmu_gather *tlb, pmd_t *pmdp, {struct ptdesc *ptdesc =virt_to_ptdesc (pmdp); -pagetable_dtor (ptdesc);tlb_remove_ptdesc (tlb, ptdesc); }#endif
@@ -106,7 +106,6 @@ static inline void __pud_free_tlb(struct mmu_gather *tlb, pud_t *pudp,if (!pgtable_l4_enabled ())return ; -pagetable_dtor (ptdesc); tlb_remove_ptdesc(tlb, ptdesc); }#endif
@@ -120,7 +119,6 @@ static inline void __p4d_free_tlb(struct mmu_gather *tlb, p4d_t *p4dp,if (!pgtable_l5_enabled ())return ; -pagetable_dtor (ptdesc); tlb_remove_ptdesc(tlb, ptdesc); }#endif
Signature · 1 line
2.20.1
PATCH 8/12 riscv: pgtable: move pagetable_dtor() to __tlb_remove_table()
In reply to Qi ZhengMove pagetable_dtor() to __tlb_remove_table(), so that ptlock and page table pages can be freed together (regardless of whether RCU is used). This prevents the use-after-free problem where the ptlock is freed immediately but the page table pages is freed later via RCU.
Page tables shouldn't have swap cache, so use pagetable_free() instead of free_page_and_swap_cache() to free page table pages.
By the way, move the comment above __tlb_remove_table() to riscv_tlb_remove_ptdesc(), it will be more appropriate.
--- arch/riscv/include/asm/pgalloc.h | 38 ++++++++++++++------------------ arch/riscv/include/asm/tlb.h | 14 ++++-------- 2 files changed, 21 insertions(+), 31 deletions(-)
arch/riscv/include/asm/pgalloc.h +17 -21
@@ -18,12 +18,22 @@#define __HAVE_ARCH_P4D_FREE#include <asm-generic/pgalloc.h> +/* +* While riscv platforms with riscv_ipi_for_rfence as true require an IPI to +* perform TLB shootdown, some platforms with riscv_ipi_for_rfence as false use +* SBI to perform TLB shootdown. To keep software pagetable walkers safe in this +* case we switch to RCU based table free (MMU_GATHER_RCU_TABLE_FREE). See the +* comment below 'ifdef CONFIG_MMU_GATHER_RCU_TABLE_FREE' in include/asm-generic/tlb.h +* for more details. +*/ static inline void riscv_tlb_remove_ptdesc (struct mmu_gather *tlb,void *pt) { -if (riscv_use_sbi_for_rfence ()) +if (riscv_use_sbi_for_rfence ()) {tlb_remove_ptdesc (tlb, pt); -else + }else { +pagetable_dtor (pt);tlb_remove_page_ptdesc (tlb, pt); + } } static inline void pmd_populate_kernel(struct mm_struct *mm,
@@ -109,12 +119,8 @@ static inline void pud_free(struct mm_struct *mm, pud_t *pud)static inline void __pud_free_tlb (struct mmu_gather *tlb,pud_t *pud,unsigned long addr) { -if (pgtable_l4_enabled) { -struct ptdesc *ptdesc =virt_to_ptdesc (pud); - -pagetable_dtor (ptdesc); -riscv_tlb_remove_ptdesc (tlb, ptdesc); - } +if (pgtable_l4_enabled) +riscv_tlb_remove_ptdesc (tlb,virt_to_ptdesc (pud)); }#define p4d_alloc_one p4d_alloc_one
@@ -136,12 +142,8 @@ static inline void p4d_free(struct mm_struct *mm, p4d_t *p4d)static inline void __p4d_free_tlb (struct mmu_gather *tlb,p4d_t *p4d,unsigned long addr) { -if (pgtable_l5_enabled) { -struct ptdesc *ptdesc =virt_to_ptdesc (p4d); - -pagetable_dtor (ptdesc); +if (pgtable_l5_enabled)riscv_tlb_remove_ptdesc (tlb,virt_to_ptdesc (p4d)); - } }#endif /* __PAGETABLE_PMD_FOLDED */
@@ -170,10 +172,7 @@ static inline pgd_t *pgd_alloc(struct mm_struct *mm)static inline void __pmd_free_tlb (struct mmu_gather *tlb,pmd_t *pmd,unsigned long addr) { -struct ptdesc *ptdesc =virt_to_ptdesc (pmd); - -pagetable_dtor (ptdesc); -riscv_tlb_remove_ptdesc (tlb, ptdesc); +riscv_tlb_remove_ptdesc (tlb,virt_to_ptdesc (pmd)); }#endif /* __PAGETABLE_PMD_FOLDED */
@@ -181,10 +180,7 @@ static inline void __pmd_free_tlb(struct mmu_gather *tlb, pmd_t *pmd,static inline void __pte_free_tlb (struct mmu_gather *tlb,pgtable_t pte,unsigned long addr) { -struct ptdesc *ptdesc =page_ptdesc (pte); - -pagetable_dtor (ptdesc); -riscv_tlb_remove_ptdesc (tlb, ptdesc); +riscv_tlb_remove_ptdesc (tlb,page_ptdesc (pte)); }#endif /* CONFIG_MMU */
arch/riscv/include/asm/tlb.h +4 -10
@@ -11,19 +11,13 @@ struct mmu_gather;static void tlb_flush (struct mmu_gather *tlb);#ifdef CONFIG_MMU -#include <linux/swap.h> -/* -* While riscv platforms with riscv_ipi_for_rfence as true require an IPI to -* perform TLB shootdown, some platforms with riscv_ipi_for_rfence as false use -* SBI to perform TLB shootdown. To keep software pagetable walkers safe in this -* case we switch to RCU based table free (MMU_GATHER_RCU_TABLE_FREE). See the -* comment below 'ifdef CONFIG_MMU_GATHER_RCU_TABLE_FREE' in include/asm-generic/tlb.h -* for more details. -*/ static inline void __tlb_remove_table (void *table) { -free_page_and_swap_cache (table); +struct ptdesc *ptdesc = (struct ptdesc *)table; + +pagetable_dtor (ptdesc); +pagetable_free (ptdesc); }#endif /* CONFIG_MMU */
Signature · 1 line
2.20.1
PATCH 9/12 x86: pgtable: move pagetable_dtor() to __tlb_remove_table()
In reply to Qi ZhengMove pagetable_dtor() to __tlb_remove_table(), so that ptlock and page table pages can be freed together (regardless of whether RCU is used). This prevents the use-after-free problem where the ptlock is freed immediately but the page table pages is freed later via RCU.
Page tables shouldn't have swap cache, so use pagetable_free() instead of free_page_and_swap_cache() to free page table pages.
--- arch/x86/include/asm/tlb.h | 17 ++++++++++------- arch/x86/kernel/paravirt.c | 1 + arch/x86/mm/pgtable.c | 12 ++---------- 3 files changed, 13 insertions(+), 17 deletions(-)
arch/x86/include/asm/tlb.h +10 -7
@@ -31,24 +31,27 @@ static inline void tlb_flush(struct mmu_gather *tlb)*/ static inline void __tlb_remove_table (void *table) { -free_page_and_swap_cache (table); +struct ptdesc *ptdesc = (struct ptdesc *)table; + +pagetable_dtor (ptdesc); +pagetable_free (ptdesc); }#ifdef CONFIG_PT_RECLAIM static inline void __tlb_remove_table_one_rcu (struct rcu_head *head) { -struct page *page; +struct ptdesc *ptdesc; - page = container_of(head, struct page, rcu_head); -put_page (page); + ptdesc = container_of(head, struct ptdesc, pt_rcu_head); +__tlb_remove_table (ptdesc); }static inline void __tlb_remove_table_one (void *table) { -struct page *page; +struct ptdesc *ptdesc; - page = table; -call_rcu (&page->rcu_head, __tlb_remove_table_one_rcu); + ptdesc = table; +call_rcu (&ptdesc->pt_rcu_head, __tlb_remove_table_one_rcu); }#define __tlb_remove_table_one __tlb_remove_table_one#endif /* CONFIG_PT_RECLAIM */
arch/x86/kernel/paravirt.c +1 -0
@@ -62,6 +62,7 @@ void __init native_pv_lock_init(void)#ifndef CONFIG_PT_RECLAIM static void native_tlb_remove_table (struct mmu_gather *tlb,void *table) { +pagetable_dtor (table);tlb_remove_page (tlb, table); }#else
arch/x86/mm/pgtable.c +2 -10
@@ -23,6 +23,7 @@ EXPORT_SYMBOL(physical_mask);static inline void paravirt_tlb_remove_table (struct mmu_gather *tlb,void *table) { +pagetable_dtor (table);tlb_remove_page (tlb, table); }#else
@@ -60,7 +61,6 @@ early_param("userpte", setup_userpte);void ___pte_free_tlb (struct mmu_gather *tlb,struct page *pte) { -pagetable_dtor (page_ptdesc (pte));paravirt_release_pte (page_to_pfn (pte));paravirt_tlb_remove_table (tlb, pte); }
@@ -68,7 +68,6 @@ void ___pte_free_tlb(struct mmu_gather *tlb, struct page *pte)#if CONFIG_PGTABLE_LEVELS >2 void ___pmd_free_tlb (struct mmu_gather *tlb,pmd_t *pmd) { -struct ptdesc *ptdesc =virt_to_ptdesc (pmd);paravirt_release_pmd (__pa (pmd) >>PAGE_SHIFT );/* * NOTE! For PAE, any changes to the top page-directory-pointer-table
@@ -77,16 +76,12 @@ void ___pmd_free_tlb(struct mmu_gather *tlb, pmd_t *pmd)#ifdef CONFIG_X86_PAE tlb->need_flush_all =1 ;#endif -pagetable_dtor (ptdesc); -paravirt_tlb_remove_table (tlb,ptdesc_page (ptdesc)); +paravirt_tlb_remove_table (tlb,virt_to_page (pmd)); }#if CONFIG_PGTABLE_LEVELS >3 void ___pud_free_tlb (struct mmu_gather *tlb,pud_t *pud) { -struct ptdesc *ptdesc =virt_to_ptdesc (pud); - -pagetable_dtor (ptdesc);paravirt_release_pud (__pa (pud) >>PAGE_SHIFT );paravirt_tlb_remove_table (tlb,virt_to_page (pud)); }
@@ -94,9 +89,6 @@ void ___pud_free_tlb(struct mmu_gather *tlb, pud_t *pud)#if CONFIG_PGTABLE_LEVELS >4 void ___p4d_free_tlb (struct mmu_gather *tlb,p4d_t *p4d) { -struct ptdesc *ptdesc =virt_to_ptdesc (p4d); - -pagetable_dtor (ptdesc);paravirt_release_p4d (__pa (p4d) >>PAGE_SHIFT );paravirt_tlb_remove_table (tlb,virt_to_page (p4d)); }
Signature · 1 line
2.20.1
PATCH 10/12 s390: pgtable: also move pagetable_dtor() of PxD to __tlb_remove_table()
In reply to Qi ZhengIn s390, the pagetable_dtor() of PTE has long been moved to __tlb_remove_table(). So similarly, also move the pagetable_dtor() of PMD|PUD|P4D to __tlb_remove_table(). This prevents the use-after-free problem where the ptlock is freed immediately but the page table pages is freed later via RCU.
By the way, rename pagetable_pte_dtor_free() to pagetable_dtor_free().
--- arch/s390/include/asm/tlb.h | 3 --- arch/s390/mm/pgalloc.c | 14 ++++---------- 2 files changed, 4 insertions(+), 13 deletions(-)
arch/s390/include/asm/tlb.h +0 -3
@@ -102,7 +102,6 @@ static inline void pmd_free_tlb(struct mmu_gather *tlb, pmd_t *pmd, {if (mm_pmd_folded (tlb->mm))return ; -pagetable_dtor (virt_to_ptdesc (pmd));__tlb_adjust_range (tlb, address,PAGE_SIZE ); tlb->mm->context.flush_mm =1 ; tlb->freed_tables =1 ;
@@ -122,7 +121,6 @@ static inline void pud_free_tlb(struct mmu_gather *tlb, pud_t *pud, {if (mm_pud_folded (tlb->mm))return ; -pagetable_dtor (virt_to_ptdesc (pud)); tlb->mm->context.flush_mm =1 ; tlb->freed_tables =1 ; tlb->cleared_p4ds =1 ;
@@ -141,7 +139,6 @@ static inline void p4d_free_tlb(struct mmu_gather *tlb, p4d_t *p4d, {if (mm_p4d_folded (tlb->mm))return ; -pagetable_dtor (virt_to_ptdesc (p4d));__tlb_adjust_range (tlb, address,PAGE_SIZE ); tlb->mm->context.flush_mm =1 ; tlb->freed_tables =1 ;
arch/s390/mm/pgalloc.c +4 -10
@@ -180,7 +180,7 @@ unsigned long *page_table_alloc(struct mm_struct *mm)return table; } -static void pagetable_pte_dtor_free (struct ptdesc *ptdesc) +static void pagetable_dtor_free (struct ptdesc *ptdesc) {pagetable_dtor (ptdesc);pagetable_free (ptdesc);
@@ -190,20 +190,14 @@ void page_table_free(struct mm_struct *mm, unsigned long *table) {struct ptdesc *ptdesc =virt_to_ptdesc (table); -pagetable_pte_dtor_free (ptdesc); +pagetable_dtor_free (ptdesc); }void __tlb_remove_table (void *table) {struct ptdesc *ptdesc =virt_to_ptdesc (table); -struct page *page =ptdesc_page (ptdesc); -if (compound_order (page) ==CRST_ALLOC_ORDER ) { -/* pmd, pud, or p4d */ -pagetable_free (ptdesc); -return ; - } -pagetable_pte_dtor_free (ptdesc); +pagetable_dtor_free (ptdesc); }#ifdef CONFIG_TRANSPARENT_HUGEPAGE
@@ -211,7 +205,7 @@ static void pte_free_now(struct rcu_head *head) { struct ptdesc *ptdesc = container_of(head, struct ptdesc, pt_rcu_head); -pagetable_pte_dtor_free (ptdesc); +pagetable_dtor_free (ptdesc); }void pte_free_defer (struct mm_struct *mm,pgtable_t pgtable)
Signature · 1 line
2.20.1
PATCH 11/12 mm: pgtable: introduce generic __tlb_remove_table()
In reply to Qi ZhengSeveral architectures (arm, arm64, riscv, s390 and x86) define exactly the same __tlb_remove_table(), just introduce generic __tlb_remove_table() to eliminate these duplications.
--- arch/arm/include/asm/tlb.h | 9 --------- arch/arm64/include/asm/tlb.h | 7 ------- arch/powerpc/include/asm/tlb.h | 1 + arch/riscv/include/asm/tlb.h | 12 ------------ arch/s390/include/asm/tlb.h | 1 - arch/s390/mm/pgalloc.c | 7 ------- arch/sparc/include/asm/tlb_32.h | 1 + arch/sparc/include/asm/tlb_64.h | 1 + arch/x86/include/asm/tlb.h | 17 ----------------- include/asm-generic/tlb.h | 15 +++++++++++++-- 10 files changed, 16 insertions(+), 55 deletions(-)
arch/arm/include/asm/tlb.h +0 -9
@@ -27,15 +27,6 @@#else /* !CONFIG_MMU */ #include <asm/tlbflush.h> - -static inline void __tlb_remove_table (void *_table) -{ -struct ptdesc *ptdesc = (struct ptdesc *)_table; - -pagetable_dtor (ptdesc); -pagetable_free (ptdesc); -} -#include <asm-generic/tlb.h> static inline void
arch/arm64/include/asm/tlb.h +0 -7
@@ -10,13 +10,6 @@#include <linux/pagemap.h> -static inline void __tlb_remove_table (void *_table) -{ -struct ptdesc *ptdesc = (struct ptdesc *)_table; - -pagetable_dtor (ptdesc); -pagetable_free (ptdesc); -}#define tlb_flush tlb_flushstatic void tlb_flush (struct mmu_gather *tlb);
arch/powerpc/include/asm/tlb.h +1 -0
@@ -37,6 +37,7 @@ extern void tlb_flush(struct mmu_gather *tlb);*/ #define tlb_needs_table_invalidate () radix_enabled() +#define __HAVE_ARCH_TLB_REMOVE_TABLE/* Get the generic bits... */ #include <asm-generic/tlb.h>
arch/riscv/include/asm/tlb.h +0 -12
@@ -10,18 +10,6 @@ struct mmu_gather;static void tlb_flush (struct mmu_gather *tlb); -#ifdef CONFIG_MMU - -static inline void __tlb_remove_table (void *table) -{ -struct ptdesc *ptdesc = (struct ptdesc *)table; - -pagetable_dtor (ptdesc); -pagetable_free (ptdesc); -} - -#endif /* CONFIG_MMU */ -#define tlb_flush tlb_flush#include <asm-generic/tlb.h>
arch/s390/include/asm/tlb.h +0 -1
@@ -22,7 +22,6 @@* Pages used for the page tables is a different story. FIXME: more */ -void __tlb_remove_table (void *_table);static inline void tlb_flush (struct mmu_gather *tlb);static inline bool __tlb_remove_page_size (struct mmu_gather *tlb,struct page *page,bool delay_rmap,int page_size);
arch/s390/mm/pgalloc.c +0 -7
@@ -193,13 +193,6 @@ void page_table_free(struct mm_struct *mm, unsigned long *table)pagetable_dtor_free (ptdesc); } -void __tlb_remove_table (void *table) -{ -struct ptdesc *ptdesc =virt_to_ptdesc (table); - -pagetable_dtor_free (ptdesc); -} -#ifdef CONFIG_TRANSPARENT_HUGEPAGE static void pte_free_now (struct rcu_head *head) {
arch/sparc/include/asm/tlb_32.h +1 -0
@@ -2,6 +2,7 @@#ifndef _SPARC_TLB_H#define _SPARC_TLB_H +#define __HAVE_ARCH_TLB_REMOVE_TABLE#include <asm-generic/tlb.h> #endif /* _SPARC_TLB_H */
arch/sparc/include/asm/tlb_64.h +1 -0
@@ -33,6 +33,7 @@ void flush_tlb_pending(void);#define tlb_needs_table_invalidate () (false)#endif +#define __HAVE_ARCH_TLB_REMOVE_TABLE#include <asm-generic/tlb.h> #endif /* _SPARC64_TLB_H */
arch/x86/include/asm/tlb.h +0 -17
@@ -20,23 +20,6 @@ static inline void tlb_flush(struct mmu_gather *tlb)flush_tlb_mm_range (tlb->mm, start, end, stride_shift, tlb->freed_tables); } -/* -* While x86 architecture in general requires an IPI to perform TLB -* shootdown, enablement code for several hypervisors overrides -* .flush_tlb_others hook in pv_mmu_ops and implements it by issuing -* a hypercall. To keep software pagetable walkers safe in this case we -* switch to RCU based table free (MMU_GATHER_RCU_TABLE_FREE). See the comment -* below 'ifdef CONFIG_MMU_GATHER_RCU_TABLE_FREE' in include/asm-generic/tlb.h -* for more details. -*/ -static inline void __tlb_remove_table (void *table) -{ -struct ptdesc *ptdesc = (struct ptdesc *)table; - -pagetable_dtor (ptdesc); -pagetable_free (ptdesc); -} -#ifdef CONFIG_PT_RECLAIM static inline void __tlb_remove_table_one_rcu (struct rcu_head *head) {
include/asm-generic/tlb.h +13 -2
@@ -153,8 +153,9 @@* * Useful if your architecture has non-page page directories. * -* When used, an architecture is expected to provide __tlb_remove_table() -* which does the actual freeing of these pages. +* When used, an architecture is expected to provide __tlb_remove_table() or +* use the generic __tlb_remove_table(), which does the actual freeing of these +* pages. * * MMU_GATHER_RCU_TABLE_FREE *
@@ -207,6 +208,16 @@ struct mmu_table_batch {#define MAX_TABLE_BATCH \ ((PAGE_SIZE - sizeof(struct mmu_table_batch)) / sizeof(void *)) +#ifndef __HAVE_ARCH_TLB_REMOVE_TABLE +static inline void __tlb_remove_table (void *_table) +{ +struct ptdesc *ptdesc = (struct ptdesc *)_table; + +pagetable_dtor (ptdesc); +pagetable_free (ptdesc); +} +#endif +extern void tlb_remove_table (struct mmu_gather *tlb,void *table);#else /* !CONFIG_MMU_GATHER_HAVE_TABLE_FREE */
Signature · 1 line
2.20.1
PATCH 12/12 mm: pgtable: move __tlb_remove_table_one() in x86 to generic file
In reply to Qi ZhengThe __tlb_remove_table_one() in x86 does not contain architecture-specific content, so move it to the generic file.
--- arch/x86/include/asm/tlb.h | 19 ------------------- mm/mmu_gather.c | 20 ++++++++++++++++++-- 2 files changed, 18 insertions(+), 21 deletions(-)
arch/x86/include/asm/tlb.h +0 -19
@@ -20,25 +20,6 @@ static inline void tlb_flush(struct mmu_gather *tlb)flush_tlb_mm_range (tlb->mm, start, end, stride_shift, tlb->freed_tables); } -#ifdef CONFIG_PT_RECLAIM -static inline void __tlb_remove_table_one_rcu (struct rcu_head *head) -{ -struct ptdesc *ptdesc; - - ptdesc = container_of(head, struct ptdesc, pt_rcu_head); -__tlb_remove_table (ptdesc); -} - -static inline void __tlb_remove_table_one (void *table) -{ -struct ptdesc *ptdesc; - - ptdesc = table; -call_rcu (&ptdesc->pt_rcu_head, __tlb_remove_table_one_rcu); -} -#define __tlb_remove_table_one __tlb_remove_table_one -#endif /* CONFIG_PT_RECLAIM */ -static inline void invlpg (unsigned long addr) { asmvolatile ("invlpg (%0)" ::"r" (addr) :"memory" );
mm/mmu_gather.c +18 -2
@@ -311,13 +311,29 @@ static inline void tlb_table_invalidate(struct mmu_gather *tlb) } } -#ifndef __tlb_remove_table_one +#ifdef CONFIG_PT_RECLAIM +static inline void __tlb_remove_table_one_rcu (struct rcu_head *head) +{ +struct ptdesc *ptdesc; + + ptdesc = container_of(head, struct ptdesc, pt_rcu_head); +__tlb_remove_table (ptdesc); +} + +static inline void __tlb_remove_table_one (void *table) +{ +struct ptdesc *ptdesc; + + ptdesc = table; +call_rcu (&ptdesc->pt_rcu_head, __tlb_remove_table_one_rcu); +} +#else static inline void __tlb_remove_table_one (void *table) {tlb_remove_table_sync_one ();__tlb_remove_table (table); } -#endif +#endif /* CONFIG_PT_RECLAIM */ static void tlb_remove_table_one (void *table) {
Signature · 1 line
2.20.1
Re: [PATCH 01/12] Revert "mm: pgtable: make ptlock be freed by RCU"
In reply to Qi ZhengQuoted 4 lines · Qi Zheng
This reverts commit
2f3443770437e49abc39af26962d293851cbab6d.Signed-off-by Qi Zheng <[email protected]>
Bloating struct pt_lock is unnecessary. Glad to see it's reverted.
Quoted 94 lines
--- include/linux/mm.h | 2 +- include/linux/mm_types.h | 9 +-------- mm/memory.c | 22 ++++++---------------- 3 files changed, 8 insertions(+), 25 deletions(-)include/linux/mm.h +1
-1@@ -2988,7 +2988,7 @@ void ptlock_free(struct ptdesc *ptdesc);static inline spinlock_t *ptlock_ptr (struct ptdesc *ptdesc) { -return &(ptdesc->ptl->ptl); +return ptdesc->ptl; }#else /* ALLOC_SPLIT_PTLOCKS */ static inline void ptlock_cache_init (void )include/linux/mm_types.h +1
-8@@ -434,13 +434,6 @@ FOLIO_MATCH(flags, _flags_2a);FOLIO_MATCH (compound_head, _head_2a);#undef FOLIO_MATCH -#if ALLOC_SPLIT_PTLOCKS -struct pt_lock { -spinlock_t ptl; -struct rcu_head rcu; -}; -#endif -/** * struct ptdesc - Memory descriptor for page tables. * @__page_flags: Same as page flags. Powerpc only. @@ -485,7 +478,7 @@ struct ptdesc {union {unsigned long _pt_pad_2;#if ALLOC_SPLIT_PTLOCKS -struct pt_lock *ptl; +spinlock_t *ptl;#else spinlock_t ptl;#endif mm/memory.c +6
-17@@ -7041,34 +7041,24 @@ static struct kmem_cache *page_ptl_cachep;void __init ptlock_cache_init (void ) { - page_ptl_cachep =kmem_cache_create ("page->ptl" ,sizeof (struct pt_lock ),0 , + page_ptl_cachep =kmem_cache_create ("page->ptl" ,sizeof (spinlock_t),0 ,SLAB_PANIC ,NULL ); }bool ptlock_alloc (struct ptdesc *ptdesc) { -struct pt_lock *pt_lock; +spinlock_t *ptl; - pt_lock =kmem_cache_alloc (page_ptl_cachep,GFP_KERNEL ); -if (!pt_lock) + ptl =kmem_cache_alloc (page_ptl_cachep,GFP_KERNEL ); +if (!ptl)return false; - ptdesc->ptl = pt_lock; + ptdesc->ptl = ptl;return true; } -static void ptlock_free_rcu (struct rcu_head *head) -{ -struct pt_lock *pt_lock; - - pt_lock = container_of(head, struct pt_lock, rcu); -kmem_cache_free (page_ptl_cachep, pt_lock); -} -void ptlock_free (struct ptdesc *ptdesc) { -struct pt_lock *pt_lock = ptdesc->ptl; - -call_rcu (&pt_lock->rcu, ptlock_free_rcu); +kmem_cache_free (page_ptl_cachep, ptdesc->ptl); }#endif --2.20.1
Peter Zijlstra
maintainerQuoted 3 lines · Qi Zheng
As proposed [1] by Peter Zijlstra below, this patch series aims to move pagetable_*_dtor() into
__tlb_remove_table(). This will cleanup pagetable_*_dtor() a bit and more gracefully fix the UAF issue [2] reported by syzbot.
I'll go over the patches in more detail on Monday, but at least the first few patches should probably carry something like:
Since you extracted it from my half-baked patch, the rest could probably do with:
Anyway, thanks for doing this, and so quicky! As said, I'll read more on Monday.
Quoted 10 lines · Peter Zijlstra
Quoted 3 lines · Qi Zheng
As proposed [1] by Peter Zijlstra below, this patch series aims to move pagetable_*_dtor() into
__tlb_remove_table(). This will cleanup pagetable_*_dtor() a bit and more gracefully fix the UAF issue [2] reported by syzbot.I'll go over the patches in more detail on Monday, but at least the first few patches should probably carry something like:
Originally-by Peter Zijlstra (Intel) <[email protected]>
Right! Will add this for [PATCH 02/12], [PATCH 03/12] and [PATCH 05/12],
Quoted 5 lines
Since you extracted it from my half-baked patch, the rest could probably do with:
Suggested-by Peter Zijlstra (Intel) <[email protected]>
and add this for [PATCH 04/12], [PATCH 06|07|08|09|10/12].
Quoted 3 lines
Anyway, thanks for doing this, and so quicky! As said, I'll read more on Monday.
Thanks!
Re: [PATCH 01/12] Revert "mm: pgtable: make ptlock be freed by RCU"
In reply to Yu ZhaoQuoted 7 lines · Yu Zhao
Quoted 4 lines · Qi Zheng
This reverts commit
2f3443770437e49abc39af26962d293851cbab6d.Signed-off-by Qi Zheng <[email protected]>Bloating struct
pt_lockis unnecessary. Glad to see it's reverted.
Indeed!
Quoted 2 lines
Acked-by Yu Zhao <[email protected]>
Thanks! Once the review of this patch series is completed, we can simply drop "mm: pgtable: make ptlock be freed by RCU" from mm tree.
Quoted 96 lines
Quoted 96 lines
--- include/linux/mm.h | 2 +- include/linux/mm_types.h | 9 +-------- mm/memory.c | 22 ++++++---------------- 3 files changed, 8 insertions(+), 25 deletions(-)include/linux/mm.h +1
-1@@ -2988,7 +2988,7 @@ void ptlock_free(struct ptdesc *ptdesc);static inline spinlock_t *ptlock_ptr (struct ptdesc *ptdesc) { -return &(ptdesc->ptl->ptl); +return ptdesc->ptl; }#else /* ALLOC_SPLIT_PTLOCKS */ static inline void ptlock_cache_init (void )include/linux/mm_types.h +1
-8@@ -434,13 +434,6 @@ FOLIO_MATCH(flags, _flags_2a);FOLIO_MATCH (compound_head, _head_2a);#undef FOLIO_MATCH -#if ALLOC_SPLIT_PTLOCKS -struct pt_lock { -spinlock_t ptl; -struct rcu_head rcu; -}; -#endif -/** * struct ptdesc - Memory descriptor for page tables. * @__page_flags: Same as page flags. Powerpc only. @@ -485,7 +478,7 @@ struct ptdesc {union {unsigned long _pt_pad_2;#if ALLOC_SPLIT_PTLOCKS -struct pt_lock *ptl; +spinlock_t *ptl;#else spinlock_t ptl;#endif mm/memory.c +6
-17@@ -7041,34 +7041,24 @@ static struct kmem_cache *page_ptl_cachep;void __init ptlock_cache_init (void ) { - page_ptl_cachep =kmem_cache_create ("page->ptl" ,sizeof (struct pt_lock ),0 , + page_ptl_cachep =kmem_cache_create ("page->ptl" ,sizeof (spinlock_t),0 ,SLAB_PANIC ,NULL ); }bool ptlock_alloc (struct ptdesc *ptdesc) { -struct pt_lock *pt_lock; +spinlock_t *ptl; - pt_lock =kmem_cache_alloc (page_ptl_cachep,GFP_KERNEL ); -if (!pt_lock) + ptl =kmem_cache_alloc (page_ptl_cachep,GFP_KERNEL ); +if (!ptl)return false; - ptdesc->ptl = pt_lock; + ptdesc->ptl = ptl;return true; } -static void ptlock_free_rcu (struct rcu_head *head) -{ -struct pt_lock *pt_lock; - - pt_lock = container_of(head, struct pt_lock, rcu); -kmem_cache_free (page_ptl_cachep, pt_lock); -} -void ptlock_free (struct ptdesc *ptdesc) { -struct pt_lock *pt_lock = ptdesc->ptl; - -call_rcu (&pt_lock->rcu, ptlock_free_rcu); +kmem_cache_free (page_ptl_cachep, ptdesc->ptl); }#endif --2.20.1
Andrew Morton
maintainerRe: [PATCH 01/12] Revert "mm: pgtable: make ptlock be freed by RCU"
Quoted 5 lines · On Sun, 15 Dec 2024 14:29:38 +0800 Qi Zheng <[email protected]> wrote:
Quoted 2 lines
Acked-by Yu Zhao <[email protected]>Thanks! Once the review of this patch series is completed, we can simply drop "mm: pgtable: make ptlock be freed by RCU" from mm tree.
Can we drop it now and does the remainder of the series "synchronously scan and reclaim empty user PTE pages v4" remain valid and useful?
Re: [PATCH 01/12] Revert "mm: pgtable: make ptlock be freed by RCU"
Hi Andrew,
Quoted 10 lines · Andrew Morton
Quoted 5 lines · On Sun, 15 Dec 2024 14:29:38 +0800 Qi Zheng <[email protected]> wrote:
Quoted 2 lines
Acked-by Yu Zhao <[email protected]>Thanks! Once the review of this patch series is completed, we can simply drop "mm: pgtable: make ptlock be freed by RCU" from mm tree.
Can we drop it now and does the remainder of the series "synchronously scan and reclaim empty user PTE pages v4" remain valid and useful?
The "mm: pgtable: make ptlock be freed by RCU" fixes the UAF issue [1] reported by syzbot. If it is dropped now and this patch series is not merged, the UAF issue will reappear.
[1]. https://lore.kernel.org/lkml/[email protected]/
Thanks,
Qi
Quoted 1 line
Andrew Morton
maintainerRe: [PATCH 01/12] Revert "mm: pgtable: make ptlock be freed by RCU"
Quoted 14 lines · On Mon, 16 Dec 2024 14:15:35 +0800 Qi Zheng <[email protected]> wrote:
Hi Andrew,
Quoted 10 lines · Andrew Morton
Quoted 5 lines · On Sun, 15 Dec 2024 14:29:38 +0800 Qi Zheng <[email protected]> wrote:
Quoted 2 lines
Acked-by Yu Zhao <[email protected]>Thanks! Once the review of this patch series is completed, we can simply drop "mm: pgtable: make ptlock be freed by RCU" from mm tree.
Can we drop it now and does the remainder of the series "synchronously scan and reclaim empty user PTE pages v4" remain valid and useful?
Quoted 6 lines
The "mm: pgtable: make ptlock be freed by RCU" fixes the UAF issue [1] reported by syzbot. If it is dropped now and this patch series is not merged, the UAF issue will reappear.
OK, so as I understand it,
- the series "synchronously scan and reclaim empty user PTE pages v4" exposes a use-after-free bug, and fixes that bug with the patch "mm:
pgtable: make ptlock be freed by RCU".
- The series "move pagetable_*_dtor() to __tlb_remove_table()" fixes that bug in a more desirable way.
- So when the series "move pagetable_*_dtor() to __tlb_remove_table()" is merged into mm-unstable, I drop the patch "mm: pgtable: make ptlock be freed by RCU".
Correct?
Re: [PATCH 01/12] Revert "mm: pgtable: make ptlock be freed by RCU"
Quoted 29 lines · Andrew Morton
Quoted 14 lines · On Mon, 16 Dec 2024 14:15:35 +0800 Qi Zheng <[email protected]> wrote:
Hi Andrew,
Quoted 10 lines · Andrew Morton
Quoted 5 lines · On Sun, 15 Dec 2024 14:29:38 +0800 Qi Zheng <[email protected]> wrote:
Quoted 2 lines
Acked-by Yu Zhao <[email protected]>Thanks! Once the review of this patch series is completed, we can simply drop "mm: pgtable: make ptlock be freed by RCU" from mm tree.
Can we drop it now and does the remainder of the series "synchronously scan and reclaim empty user PTE pages v4" remain valid and useful?
Quoted 6 lines
The "mm: pgtable: make ptlock be freed by RCU" fixes the UAF issue [1] reported by syzbot. If it is dropped now and this patch series is not merged, the UAF issue will reappear.
OK, so as I understand it,
- the series "synchronously scan and reclaim empty user PTE pages v4" exposes a use-after-free bug, and fixes that bug with the patch "mm:
pgtable: make ptlock be freed by RCU".
Quoted 8 lines
- The series "move pagetable_*_dtor() to
__tlb_remove_table()" fixes that bug in a more desirable way.- So when the series "move pagetable_*_dtor() to
__tlb_remove_table()" is merged into mm-unstable, I drop the patch "mm: pgtable: make ptlock be freed by RCU".Correct?
Right!
Peter Zijlstra
maintainerRe: [PATCH 11/12] mm: pgtable: introduce generic __tlb_remove_table()
In reply to Qi ZhengQuoted 26 lines · Qi Zheng
arch/s390/mm/pgalloc.c +0
-7@@ -193,13 +193,6 @@ void page_table_free(struct mm_struct *mm, unsigned long *table)pagetable_dtor_free (ptdesc); } -void __tlb_remove_table (void *table) -{ -struct ptdesc *ptdesc =virt_to_ptdesc (table); - -pagetable_dtor_free (ptdesc); -} -#ifdef CONFIG_TRANSPARENT_HUGEPAGE static void pte_free_now (struct rcu_head *head) {include/asm-generic/tlb.h +0
-0#define MAX_TABLE_BATCH \ ((PAGE_SIZE - sizeof(struct mmu_table_batch)) / sizeof(void *))
include/asm-generic/tlb.h
Quoted 10 lines
include/asm-generic/tlb.h +9
-0+#ifndef __HAVE_ARCH_TLB_REMOVE_TABLE +static inline void __tlb_remove_table (void *_table) +{ +struct ptdesc *ptdesc = (struct ptdesc *)_table; + +pagetable_dtor (ptdesc); +pagetable_free (ptdesc); +} +#endif
Spot the fail...
That said, all this ptdesc stuff is another giant trainwreck. Let me clean that up for you.
---
arch/arm/include/asm/tlb.h +2 -6
@@ -32,8 +32,6 @@static inline void __pte_free_tlb (struct mmu_gather *tlb,pgtable_t pte,unsigned long addr) { -struct ptdesc *ptdesc =page_ptdesc (pte); -#ifndef CONFIG_ARM_LPAE /* * With the classic ARM MMU, a pte page has two corresponding pmd
@@ -43,16 +41,14 @@ __pte_free_tlb(struct mmu_gather *tlb, pgtable_t pte, unsigned long addr)__tlb_adjust_range (tlb, addr -PAGE_SIZE ,2 *PAGE_SIZE );#endif -tlb_remove_ptdesc (tlb, ptdesc); +tlb_remove_table (tlb, pte); }static inline void __pmd_free_tlb (struct mmu_gather *tlb,pmd_t *pmdp,unsigned long addr) {#ifdef CONFIG_ARM_LPAE -struct ptdesc *ptdesc =virt_to_ptdesc (pmdp); - -tlb_remove_ptdesc (tlb, ptdesc); +tlb_remove_table (tlb,virt_to_page (pmdp));#endif }
arch/arm64/include/asm/tlb.h +4 -12
@@ -75,18 +75,14 @@ static inline void tlb_flush(struct mmu_gather *tlb)static inline void __pte_free_tlb (struct mmu_gather *tlb,pgtable_t pte,unsigned long addr) { -struct ptdesc *ptdesc =page_ptdesc (pte); - -tlb_remove_ptdesc (tlb, ptdesc); +tlb_remove_table (tlb, pte); }#if CONFIG_PGTABLE_LEVELS >2 static inline void __pmd_free_tlb (struct mmu_gather *tlb,pmd_t *pmdp,unsigned long addr) { -struct ptdesc *ptdesc =virt_to_ptdesc (pmdp); - -tlb_remove_ptdesc (tlb, ptdesc); +tlb_remove_table (tlb,virt_to_page (pmdp)); }#endif
@@ -94,12 +90,10 @@ static inline void __pmd_free_tlb(struct mmu_gather *tlb, pmd_t *pmdp,static inline void __pud_free_tlb (struct mmu_gather *tlb,pud_t *pudp,unsigned long addr) { -struct ptdesc *ptdesc =virt_to_ptdesc (pudp); -if (!pgtable_l4_enabled ())return ; -tlb_remove_ptdesc (tlb, ptdesc); +tlb_remove_table (tlb,virt_to_page (pudp)); }#endif
@@ -107,12 +101,10 @@ static inline void __pud_free_tlb(struct mmu_gather *tlb, pud_t *pudp,static inline void __p4d_free_tlb (struct mmu_gather *tlb,p4d_t *p4dp,unsigned long addr) { -struct ptdesc *ptdesc =virt_to_ptdesc (p4dp); -if (!pgtable_l5_enabled ())return ; -tlb_remove_ptdesc (tlb, ptdesc); +tlb_remove_table (tlb,virt_to_page (p4dp)); }#endif
arch/csky/include/asm/pgalloc.h +1 -1
@@ -64,7 +64,7 @@ static inline pgd_t *pgd_alloc(struct mm_struct *mm)#define __pte_free_tlb (tlb, pte, address) \ do { \ pagetable_dtor(page_ptdesc(pte)); \ - tlb_remove_page_ptdesc(tlb, page_ptdesc(pte)); \ + tlb_remove_page(tlb, pte); \ } while (0)extern void pagetable_init (void );
arch/hexagon/include/asm/pgalloc.h +1 -1
@@ -90,7 +90,7 @@ static inline void pmd_populate_kernel(struct mm_struct *mm, pmd_t *pmd,#define __pte_free_tlb (tlb, pte, addr) \ do { \ pagetable_dtor((page_ptdesc(pte))); \ - tlb_remove_page_ptdesc((tlb), (page_ptdesc(pte))); \ + tlb_remove_page((tlb), (pte)); \ } while (0)#endif
arch/loongarch/include/asm/pgalloc.h +1 -1
@@ -58,7 +58,7 @@ static inline pte_t *pte_alloc_one_kernel(struct mm_struct *mm)#define __pte_free_tlb (tlb, pte, address) \ do { \ pagetable_dtor(page_ptdesc(pte)); \ - tlb_remove_page_ptdesc((tlb), page_ptdesc(pte)); \ + tlb_remove_page((tlb), (pte)); \ } while (0)#ifndef __PAGETABLE_PMD_FOLDED
arch/m68k/include/asm/sun3_pgalloc.h +1 -1
@@ -20,7 +20,7 @@ extern const char bad_pmd_string[];#define __pte_free_tlb (tlb, pte, addr) \ do { \ pagetable_dtor(page_ptdesc(pte)); \ - tlb_remove_page_ptdesc((tlb), page_ptdesc(pte)); \ + tlb_remove_page((tlb), (pte)); \ } while (0)static inline void pmd_populate_kernel (struct mm_struct *mm,pmd_t *pmd,pte_t *pte)
arch/mips/include/asm/pgalloc.h +1 -1
@@ -57,7 +57,7 @@ static inline void pgd_free(struct mm_struct *mm, pgd_t *pgd)#define __pte_free_tlb (tlb, pte, address) \ do { \ pagetable_dtor(page_ptdesc(pte)); \ - tlb_remove_page_ptdesc((tlb), page_ptdesc(pte)); \ + tlb_remove_page((tlb), (pte)); \ } while (0)#ifndef __PAGETABLE_PMD_FOLDED
arch/nios2/include/asm/pgalloc.h +1 -1
@@ -31,7 +31,7 @@ extern pgd_t *pgd_alloc(struct mm_struct *mm);#define __pte_free_tlb (tlb, pte, addr) \ do { \ pagetable_dtor(page_ptdesc(pte)); \ - tlb_remove_page_ptdesc((tlb), (page_ptdesc(pte))); \ + tlb_remove_page((tlb), (pte)); \ } while (0)#endif /* _ASM_NIOS2_PGALLOC_H */
arch/openrisc/include/asm/pgalloc.h +1 -1
@@ -69,7 +69,7 @@ extern pte_t *pte_alloc_one_kernel(struct mm_struct *mm);#define __pte_free_tlb (tlb, pte, addr) \ do { \ pagetable_dtor(page_ptdesc(pte)); \ - tlb_remove_page_ptdesc((tlb), (page_ptdesc(pte))); \ + tlb_remove_page((tlb), (pte)); \ } while (0)#endif
arch/riscv/include/asm/pgalloc.h +7 -7
@@ -26,13 +26,13 @@* comment below 'ifdef CONFIG_MMU_GATHER_RCU_TABLE_FREE' in include/asm-generic/tlb.h * for more details. */ -static inline void riscv_tlb_remove_ptdesc (struct mmu_gather *tlb,void *pt) +static inline void riscv_tlb_remove_table (struct mmu_gather *tlb,void *pt) {if (riscv_use_sbi_for_rfence ()) { -tlb_remove_ptdesc (tlb, pt); +tlb_remove_table (tlb, pt); }else {pagetable_dtor (pt); -tlb_remove_page_ptdesc (tlb, pt); +tlb_remove_page (tlb, pt); } }
@@ -120,7 +120,7 @@ static inline void __pud_free_tlb(struct mmu_gather *tlb, pud_t *pud, unsigned long addr) {if (pgtable_l4_enabled) -riscv_tlb_remove_ptdesc (tlb,virt_to_ptdesc (pud)); +riscv_tlb_remove_table (tlb,virt_to_page (pud)); }#define p4d_alloc_one p4d_alloc_one
@@ -143,7 +143,7 @@ static inline void __p4d_free_tlb(struct mmu_gather *tlb, p4d_t *p4d, unsigned long addr) {if (pgtable_l5_enabled) -riscv_tlb_remove_ptdesc (tlb,virt_to_ptdesc (p4d)); +riscv_tlb_remove_table (tlb,virt_to_page (p4d)); }#endif /* __PAGETABLE_PMD_FOLDED */
@@ -172,7 +172,7 @@ static inline pgd_t *pgd_alloc(struct mm_struct *mm)static inline void __pmd_free_tlb (struct mmu_gather *tlb,pmd_t *pmd,unsigned long addr) { -riscv_tlb_remove_ptdesc (tlb,virt_to_ptdesc (pmd)); +riscv_tlb_remove_table (tlb,virt_to_page (pmd)); }#endif /* __PAGETABLE_PMD_FOLDED */
@@ -180,7 +180,7 @@ static inline void __pmd_free_tlb(struct mmu_gather *tlb, pmd_t *pmd,static inline void __pte_free_tlb (struct mmu_gather *tlb,pgtable_t pte,unsigned long addr) { -riscv_tlb_remove_ptdesc (tlb,page_ptdesc (pte)); +riscv_tlb_remove_table (tlb, pte); }#endif /* CONFIG_MMU */
arch/s390/include/asm/tlb.h +4 -4
@@ -86,7 +86,7 @@ static inline void pte_free_tlb(struct mmu_gather *tlb, pgtable_t pte, tlb->cleared_pmds =1 ;if (mm_alloc_pgste (tlb->mm))gmap_unlink (tlb->mm, (unsigned long *)pte, address); -tlb_remove_ptdesc (tlb, pte); +tlb_remove_table (tlb, pte); }/*
@@ -105,7 +105,7 @@ static inline void pmd_free_tlb(struct mmu_gather *tlb, pmd_t *pmd, tlb->mm->context.flush_mm =1 ; tlb->freed_tables =1 ; tlb->cleared_puds =1 ; -tlb_remove_ptdesc (tlb, pmd); +tlb_remove_table (tlb, pmd); }/*
@@ -123,7 +123,7 @@ static inline void pud_free_tlb(struct mmu_gather *tlb, pud_t *pud, tlb->mm->context.flush_mm =1 ; tlb->freed_tables =1 ; tlb->cleared_p4ds =1 ; -tlb_remove_ptdesc (tlb, pud); +tlb_remove_table (tlb, pud); }/*
@@ -141,7 +141,7 @@ static inline void p4d_free_tlb(struct mmu_gather *tlb, p4d_t *p4d,__tlb_adjust_range (tlb, address,PAGE_SIZE ); tlb->mm->context.flush_mm =1 ; tlb->freed_tables =1 ; - tlb_remove_ptdesc(tlb, p4d); + tlb_remove_table(tlb, p4d); }#endif /* _S390_TLB_H */
arch/sh/include/asm/pgalloc.h +1 -1
@@ -35,7 +35,7 @@ static inline void pmd_populate(struct mm_struct *mm, pmd_t *pmd,#define __pte_free_tlb (tlb, pte, addr) \ do { \ pagetable_dtor(page_ptdesc(pte)); \ - tlb_remove_page_ptdesc((tlb), (page_ptdesc(pte))); \ + tlb_remove_page((tlb), (pte)); \ } while (0)#endif /* __ASM_SH_PGALLOC_H */
arch/um/include/asm/pgalloc.h +4 -4
@@ -28,7 +28,7 @@ extern pgd_t *pgd_alloc(struct mm_struct *);#define __pte_free_tlb (tlb, pte, address) \ do { \ pagetable_dtor(page_ptdesc(pte)); \ - tlb_remove_page_ptdesc((tlb), (page_ptdesc(pte))); \ + tlb_remove_page((tlb), (pte)); \ } while (0)#if CONFIG_PGTABLE_LEVELS >2
@@ -36,15 +36,15 @@ do { \#define __pmd_free_tlb (tlb, pmd, address) \ do { \ pagetable_dtor(virt_to_ptdesc(pmd)); \ - tlb_remove_page_ptdesc((tlb), virt_to_ptdesc(pmd)); \ + tlb_remove_page((tlb), virt_page(pmd)); \ } while (0)#if CONFIG_PGTABLE_LEVELS >3 #define __pud_free_tlb (tlb, pud, address) \ do { \ - pagetable_dtor(virt_to_ptdesc(pud)); \ - tlb_remove_page_ptdesc((tlb), virt_to_ptdesc(pud)); \ + pagetable_dtor(virt_to_ptdesc(pud)); \ + tlb_remove_page((tlb), virt_to_page(pud)); \ } while (0)#endif
include/asm-generic/tlb.h +2 -13
@@ -209,9 +209,9 @@ struct mmu_table_batch { ((PAGE_SIZE - sizeof(struct mmu_table_batch)) / sizeof(void *))#ifndef __HAVE_ARCH_TLB_REMOVE_TABLE -static inline void __tlb_remove_table (void *_table) +static inline void __tlb_remove_table (void *table) { -struct ptdesc *ptdesc = (struct ptdesc *)_table; +struct ptdesc *ptdesc =page_to_ptdesc (table);pagetable_dtor (ptdesc);pagetable_free (ptdesc);
@@ -499,17 +499,6 @@ static inline void tlb_remove_page(struct mmu_gather *tlb, struct page *page)return tlb_remove_page_size (tlb, page,PAGE_SIZE ); } -static inline void tlb_remove_ptdesc (struct mmu_gather *tlb,void *pt) -{ -tlb_remove_table (tlb, pt); -} - -/* Like tlb_remove_ptdesc, but for page-like page directories. */ -static inline void tlb_remove_page_ptdesc (struct mmu_gather *tlb,struct ptdesc *pt) -{ -tlb_remove_page (tlb,ptdesc_page (pt)); -} -static inline void tlb_change_page_size (struct mmu_gather *tlb,unsigned int page_size) {
Peter Zijlstra
maintainerRe: [PATCH 11/12] mm: pgtable: introduce generic __tlb_remove_table()
Quoted 47 lines · Peter Zijlstra
Quoted 26 lines · Qi Zheng
arch/s390/mm/pgalloc.c +0
-7@@ -193,13 +193,6 @@ void page_table_free(struct mm_struct *mm, unsigned long *table)pagetable_dtor_free (ptdesc); } -void __tlb_remove_table (void *table) -{ -struct ptdesc *ptdesc =virt_to_ptdesc (table); - -pagetable_dtor_free (ptdesc); -} -#ifdef CONFIG_TRANSPARENT_HUGEPAGE static void pte_free_now (struct rcu_head *head) {include/asm-generic/tlb.h +0
-0#define MAX_TABLE_BATCH \ ((PAGE_SIZE - sizeof(struct mmu_table_batch)) / sizeof(void *))include/asm-generic/tlb.h
Quoted 10 lines
include/asm-generic/tlb.h +9
-0+#ifndef __HAVE_ARCH_TLB_REMOVE_TABLE +static inline void __tlb_remove_table (void *_table) +{ +struct ptdesc *ptdesc = (struct ptdesc *)_table; + +pagetable_dtor (ptdesc); +pagetable_free (ptdesc); +} +#endif Spot the fail...
That said, all this ptdesc stuff is another giant trainwreck. Let me clean that up for you.
---
include/asm-generic/tlb.h struct mmu_table_batch { · line 217
Quoted 8 lines
include/asm-generic/tlb.h +2
-2-static inline void __tlb_remove_table (void *_table) +static inline void __tlb_remove_table (void *table) { -struct ptdesc *ptdesc = (struct ptdesc *)_table; +struct ptdesc *ptdesc =page_to_ptdesc (table);pagetable_dtor (ptdesc);pagetable_free (ptdesc);
And now observe that __tlb_remove_table() is identical to asm-generic/pgalloc.h pte_free(), pmd_free(), __pud_free() and __p4d_free().
And I'm sure we don't need 5 copies of this :-), wdyt?
Peter Zijlstra
maintainerRe: [PATCH 11/12] mm: pgtable: introduce generic __tlb_remove_table()
Quoted 54 lines · Peter Zijlstra
Quoted 53 lines · Peter Zijlstra
Quoted 26 lines · Qi Zheng
arch/s390/mm/pgalloc.c +0
-7@@ -193,13 +193,6 @@ void page_table_free(struct mm_struct *mm, unsigned long *table)pagetable_dtor_free (ptdesc); } -void __tlb_remove_table (void *table) -{ -struct ptdesc *ptdesc =virt_to_ptdesc (table); - -pagetable_dtor_free (ptdesc); -} -#ifdef CONFIG_TRANSPARENT_HUGEPAGE static void pte_free_now (struct rcu_head *head) {include/asm-generic/tlb.h +0
-0#define MAX_TABLE_BATCH \ ((PAGE_SIZE - sizeof(struct mmu_table_batch)) / sizeof(void *))include/asm-generic/tlb.h
Quoted 10 lines
include/asm-generic/tlb.h +9
-0+#ifndef __HAVE_ARCH_TLB_REMOVE_TABLE +static inline void __tlb_remove_table (void *_table) +{ +struct ptdesc *ptdesc = (struct ptdesc *)_table; + +pagetable_dtor (ptdesc); +pagetable_free (ptdesc); +} +#endif Spot the fail...
That said, all this ptdesc stuff is another giant trainwreck. Let me clean that up for you.
---
include/asm-generic/tlb.h +2
-2-static inline void __tlb_remove_table (void *_table) +static inline void __tlb_remove_table (void *table) { -struct ptdesc *ptdesc = (struct ptdesc *)_table; +struct ptdesc *ptdesc =page_to_ptdesc (table);
Quoted 8 lines
include/asm-generic/tlb.h struct mmu_table_batch { · line 217
Quoted 2 lines
include/asm-generic/tlb.h +0
-0pagetable_dtor (ptdesc);pagetable_free (ptdesc);And now observe that
__tlb_remove_table()is identical toasm-generic/pgalloc.hpte_free(),pmd_free(),__pud_free()and__p4d_free().And I'm sure we don't need 5 copies of this :-), wdyt?
Argh, nearly, it has the whole page vs virt nonsense still going on. Bah.
Re: [PATCH 11/12] mm: pgtable: introduce generic __tlb_remove_table()
In reply to Peter ZijlstraQuoted 1 line · Peter Zijlstra
On Sat, Dec 14, 2024 at 05:02:57PM +0800, Qi Zheng wrote:
[...]
Quoted 14 lines
Review
Quoted 10 lines
(quoted diff) +9
-0+#ifndef __HAVE_ARCH_TLB_REMOVE_TABLE +static inline void __tlb_remove_table(void *_table) +{ + struct ptdesc *ptdesc = (struct ptdesc *)_table; + + pagetable_dtor(ptdesc); + pagetable_free(ptdesc); +} +#endifSpot the fail...
Quoted 2 lines
That said, all this ptdesc stuff is another giant trainwreck. Let me clean that up for you.
It looks like you want to revert what was done in this patch series:
https://lore.kernel.org/all/[email protected]/
But why? It seems that splitting ptdesc from struct page is a good thing?
Quoted 354 lines
---
arch/arm/include/asm/tlb.h +2
-6@@ -32,8 +32,6 @@static inline void __pte_free_tlb (struct mmu_gather *tlb,pgtable_t pte,unsigned long addr) { -struct ptdesc *ptdesc =page_ptdesc (pte); -#ifndef CONFIG_ARM_LPAE /* * With the classic ARM MMU, a pte page has two corresponding pmd @@ -43,16 +41,14 @@ __pte_free_tlb(struct mmu_gather *tlb, pgtable_t pte, unsigned long addr)__tlb_adjust_range (tlb, addr -PAGE_SIZE ,2 *PAGE_SIZE );#endif -tlb_remove_ptdesc (tlb, ptdesc); +tlb_remove_table (tlb, pte); }static inline void __pmd_free_tlb (struct mmu_gather *tlb,pmd_t *pmdp,unsigned long addr) {#ifdef CONFIG_ARM_LPAE -struct ptdesc *ptdesc =virt_to_ptdesc (pmdp); - -tlb_remove_ptdesc (tlb, ptdesc); +tlb_remove_table (tlb,virt_to_page (pmdp));#endif }arch/arm64/include/asm/tlb.h +4
-12@@ -75,18 +75,14 @@ static inline void tlb_flush(struct mmu_gather *tlb)static inline void __pte_free_tlb (struct mmu_gather *tlb,pgtable_t pte,unsigned long addr) { -struct ptdesc *ptdesc =page_ptdesc (pte); - -tlb_remove_ptdesc (tlb, ptdesc); +tlb_remove_table (tlb, pte); }#if CONFIG_PGTABLE_LEVELS >2 static inline void __pmd_free_tlb (struct mmu_gather *tlb,pmd_t *pmdp,unsigned long addr) { -struct ptdesc *ptdesc =virt_to_ptdesc (pmdp); - -tlb_remove_ptdesc (tlb, ptdesc); +tlb_remove_table (tlb,virt_to_page (pmdp)); }#endif @@ -94,12 +90,10 @@ static inline void __pmd_free_tlb(struct mmu_gather *tlb, pmd_t *pmdp,static inline void __pud_free_tlb (struct mmu_gather *tlb,pud_t *pudp,unsigned long addr) { -struct ptdesc *ptdesc =virt_to_ptdesc (pudp); -if (!pgtable_l4_enabled ())return ; -tlb_remove_ptdesc (tlb, ptdesc); +tlb_remove_table (tlb,virt_to_page (pudp)); }#endif @@ -107,12 +101,10 @@ static inline void __pud_free_tlb(struct mmu_gather *tlb, pud_t *pudp,static inline void __p4d_free_tlb (struct mmu_gather *tlb,p4d_t *p4dp,unsigned long addr) { -struct ptdesc *ptdesc =virt_to_ptdesc (p4dp); -if (!pgtable_l5_enabled ())return ; -tlb_remove_ptdesc (tlb, ptdesc); +tlb_remove_table (tlb,virt_to_page (p4dp)); }#endif arch/csky/include/asm/pgalloc.h +1
-1@@ -64,7 +64,7 @@ static inline pgd_t *pgd_alloc(struct mm_struct *mm)#define __pte_free_tlb (tlb, pte, address) \ do { \ pagetable_dtor(page_ptdesc(pte)); \ - tlb_remove_page_ptdesc(tlb, page_ptdesc(pte)); \ + tlb_remove_page(tlb, pte); \ } while (0)extern void pagetable_init (void );arch/hexagon/include/asm/pgalloc.h +1
-1@@ -90,7 +90,7 @@ static inline void pmd_populate_kernel(struct mm_struct *mm, pmd_t *pmd,#define __pte_free_tlb (tlb, pte, addr) \ do { \ pagetable_dtor((page_ptdesc(pte))); \ - tlb_remove_page_ptdesc((tlb), (page_ptdesc(pte))); \ + tlb_remove_page((tlb), (pte)); \ } while (0)#endif arch/loongarch/include/asm/pgalloc.h +1
-1@@ -58,7 +58,7 @@ static inline pte_t *pte_alloc_one_kernel(struct mm_struct *mm)#define __pte_free_tlb (tlb, pte, address) \ do { \ pagetable_dtor(page_ptdesc(pte)); \ - tlb_remove_page_ptdesc((tlb), page_ptdesc(pte)); \ + tlb_remove_page((tlb), (pte)); \ } while (0)#ifndef __PAGETABLE_PMD_FOLDEDarch/m68k/include/asm/sun3_pgalloc.h +1
-1@@ -20,7 +20,7 @@ extern const char bad_pmd_string[];#define __pte_free_tlb (tlb, pte, addr) \ do { \ pagetable_dtor(page_ptdesc(pte)); \ - tlb_remove_page_ptdesc((tlb), page_ptdesc(pte)); \ + tlb_remove_page((tlb), (pte)); \ } while (0)static inline void pmd_populate_kernel (struct mm_struct *mm,pmd_t *pmd,pte_t *pte)arch/mips/include/asm/pgalloc.h +1
-1@@ -57,7 +57,7 @@ static inline void pgd_free(struct mm_struct *mm, pgd_t *pgd)#define __pte_free_tlb (tlb, pte, address) \ do { \ pagetable_dtor(page_ptdesc(pte)); \ - tlb_remove_page_ptdesc((tlb), page_ptdesc(pte)); \ + tlb_remove_page((tlb), (pte)); \ } while (0)#ifndef __PAGETABLE_PMD_FOLDEDarch/nios2/include/asm/pgalloc.h +1
-1@@ -31,7 +31,7 @@ extern pgd_t *pgd_alloc(struct mm_struct *mm);#define __pte_free_tlb (tlb, pte, addr) \ do { \ pagetable_dtor(page_ptdesc(pte)); \ - tlb_remove_page_ptdesc((tlb), (page_ptdesc(pte))); \ + tlb_remove_page((tlb), (pte)); \ } while (0)#endif /* _ASM_NIOS2_PGALLOC_H */ arch/openrisc/include/asm/pgalloc.h +1
-1@@ -69,7 +69,7 @@ extern pte_t *pte_alloc_one_kernel(struct mm_struct *mm);#define __pte_free_tlb (tlb, pte, addr) \ do { \ pagetable_dtor(page_ptdesc(pte)); \ - tlb_remove_page_ptdesc((tlb), (page_ptdesc(pte))); \ + tlb_remove_page((tlb), (pte)); \ } while (0)#endif arch/riscv/include/asm/pgalloc.h +7
-7@@ -26,13 +26,13 @@* comment below 'ifdef CONFIG_MMU_GATHER_RCU_TABLE_FREE' in include/asm-generic/tlb.h * for more details. */ -static inline void riscv_tlb_remove_ptdesc (struct mmu_gather *tlb,void *pt) +static inline void riscv_tlb_remove_table (struct mmu_gather *tlb,void *pt) {if (riscv_use_sbi_for_rfence ()) { -tlb_remove_ptdesc (tlb, pt); +tlb_remove_table (tlb, pt); }else {pagetable_dtor (pt); -tlb_remove_page_ptdesc (tlb, pt); +tlb_remove_page (tlb, pt); } }@@ -120,7 +120,7 @@ static inline void __pud_free_tlb(struct mmu_gather *tlb, pud_t *pud, unsigned long addr) {if (pgtable_l4_enabled) -riscv_tlb_remove_ptdesc (tlb,virt_to_ptdesc (pud)); +riscv_tlb_remove_table (tlb,virt_to_page (pud)); }#define p4d_alloc_one p4d_alloc_one@@ -143,7 +143,7 @@ static inline void __p4d_free_tlb(struct mmu_gather *tlb, p4d_t *p4d, unsigned long addr) {if (pgtable_l5_enabled) -riscv_tlb_remove_ptdesc (tlb,virt_to_ptdesc (p4d)); +riscv_tlb_remove_table (tlb,virt_to_page (p4d)); }#endif /* __PAGETABLE_PMD_FOLDED */ @@ -172,7 +172,7 @@ static inline pgd_t *pgd_alloc(struct mm_struct *mm)static inline void __pmd_free_tlb (struct mmu_gather *tlb,pmd_t *pmd,unsigned long addr) { -riscv_tlb_remove_ptdesc (tlb,virt_to_ptdesc (pmd)); +riscv_tlb_remove_table (tlb,virt_to_page (pmd)); }#endif /* __PAGETABLE_PMD_FOLDED */ @@ -180,7 +180,7 @@ static inline void __pmd_free_tlb(struct mmu_gather *tlb, pmd_t *pmd,static inline void __pte_free_tlb (struct mmu_gather *tlb,pgtable_t pte,unsigned long addr) { -riscv_tlb_remove_ptdesc (tlb,page_ptdesc (pte)); +riscv_tlb_remove_table (tlb, pte); }#endif /* CONFIG_MMU */ arch/s390/include/asm/tlb.h +4
-4@@ -86,7 +86,7 @@ static inline void pte_free_tlb(struct mmu_gather *tlb, pgtable_t pte, tlb->cleared_pmds =1 ;if (mm_alloc_pgste (tlb->mm))gmap_unlink (tlb->mm, (unsigned long *)pte, address); -tlb_remove_ptdesc (tlb, pte); +tlb_remove_table (tlb, pte); }/* @@ -105,7 +105,7 @@ static inline void pmd_free_tlb(struct mmu_gather *tlb, pmd_t *pmd, tlb->mm->context.flush_mm =1 ; tlb->freed_tables =1 ; tlb->cleared_puds =1 ; -tlb_remove_ptdesc (tlb, pmd); +tlb_remove_table (tlb, pmd); }/* @@ -123,7 +123,7 @@ static inline void pud_free_tlb(struct mmu_gather *tlb, pud_t *pud, tlb->mm->context.flush_mm =1 ; tlb->freed_tables =1 ; tlb->cleared_p4ds =1 ; -tlb_remove_ptdesc (tlb, pud); +tlb_remove_table (tlb, pud); }/* @@ -141,7 +141,7 @@ static inline void p4d_free_tlb(struct mmu_gather *tlb, p4d_t *p4d,__tlb_adjust_range (tlb, address,PAGE_SIZE ); tlb->mm->context.flush_mm =1 ; tlb->freed_tables =1 ; - tlb_remove_ptdesc(tlb, p4d); + tlb_remove_table(tlb, p4d); }#endif /* _S390_TLB_H */ arch/sh/include/asm/pgalloc.h +1
-1@@ -35,7 +35,7 @@ static inline void pmd_populate(struct mm_struct *mm, pmd_t *pmd,#define __pte_free_tlb (tlb, pte, addr) \ do { \ pagetable_dtor(page_ptdesc(pte)); \ - tlb_remove_page_ptdesc((tlb), (page_ptdesc(pte))); \ + tlb_remove_page((tlb), (pte)); \ } while (0)#endif /* __ASM_SH_PGALLOC_H */ arch/um/include/asm/pgalloc.h +4
-4@@ -28,7 +28,7 @@ extern pgd_t *pgd_alloc(struct mm_struct *);#define __pte_free_tlb (tlb, pte, address) \ do { \ pagetable_dtor(page_ptdesc(pte)); \ - tlb_remove_page_ptdesc((tlb), (page_ptdesc(pte))); \ + tlb_remove_page((tlb), (pte)); \ } while (0)#if CONFIG_PGTABLE_LEVELS >2 @@ -36,15 +36,15 @@ do { \#define __pmd_free_tlb (tlb, pmd, address) \ do { \ pagetable_dtor(virt_to_ptdesc(pmd)); \ - tlb_remove_page_ptdesc((tlb), virt_to_ptdesc(pmd)); \ + tlb_remove_page((tlb), virt_page(pmd)); \ } while (0)#if CONFIG_PGTABLE_LEVELS >3 #define __pud_free_tlb (tlb, pud, address) \ do { \ - pagetable_dtor(virt_to_ptdesc(pud)); \ - tlb_remove_page_ptdesc((tlb), virt_to_ptdesc(pud)); \ + pagetable_dtor(virt_to_ptdesc(pud)); \ + tlb_remove_page((tlb), virt_to_page(pud)); \ } while (0)#endif include/asm-generic/tlb.h +2
-13@@ -209,9 +209,9 @@ struct mmu_table_batch { ((PAGE_SIZE - sizeof(struct mmu_table_batch)) / sizeof(void *))#ifndef __HAVE_ARCH_TLB_REMOVE_TABLE -static inline void __tlb_remove_table (void *_table) +static inline void __tlb_remove_table (void *table) { -struct ptdesc *ptdesc = (struct ptdesc *)_table; +struct ptdesc *ptdesc =page_to_ptdesc (table);pagetable_dtor (ptdesc);pagetable_free (ptdesc);@@ -499,17 +499,6 @@ static inline void tlb_remove_page(struct mmu_gather *tlb, struct page *page)return tlb_remove_page_size (tlb, page,PAGE_SIZE ); } -static inline void tlb_remove_ptdesc (struct mmu_gather *tlb,void *pt) -{ -tlb_remove_table (tlb, pt); -} - -/* Like tlb_remove_ptdesc, but for page-like page directories. */ -static inline void tlb_remove_page_ptdesc (struct mmu_gather *tlb,struct ptdesc *pt) -{ -tlb_remove_page (tlb,ptdesc_page (pt)); -} -static inline void tlb_change_page_size (struct mmu_gather *tlb,unsigned int page_size) {
Re: [PATCH 11/12] mm: pgtable: introduce generic __tlb_remove_table()
In reply to Peter ZijlstraQuoted 62 lines · Peter Zijlstra
Quoted 61 lines · Peter Zijlstra
Quoted 47 lines · Peter Zijlstra
Quoted 26 lines · Qi Zheng
arch/s390/mm/pgalloc.c +0
-7@@ -193,13 +193,6 @@ void page_table_free(struct mm_struct *mm, unsigned long *table)pagetable_dtor_free (ptdesc); } -void __tlb_remove_table (void *table) -{ -struct ptdesc *ptdesc =virt_to_ptdesc (table); - -pagetable_dtor_free (ptdesc); -} -#ifdef CONFIG_TRANSPARENT_HUGEPAGE static void pte_free_now (struct rcu_head *head) {include/asm-generic/tlb.h +0
-0#define MAX_TABLE_BATCH \ ((PAGE_SIZE - sizeof(struct mmu_table_batch)) / sizeof(void *))include/asm-generic/tlb.h
Quoted 10 lines
include/asm-generic/tlb.h +9
-0+#ifndef __HAVE_ARCH_TLB_REMOVE_TABLE +static inline void __tlb_remove_table (void *_table) +{ +struct ptdesc *ptdesc = (struct ptdesc *)_table; + +pagetable_dtor (ptdesc); +pagetable_free (ptdesc); +} +#endif Spot the fail...
That said, all this ptdesc stuff is another giant trainwreck. Let me clean that up for you.
---
include/asm-generic/tlb.h struct mmu_table_batch { · line 217
Quoted 8 lines
include/asm-generic/tlb.h +2
-2-static inline void __tlb_remove_table (void *_table) +static inline void __tlb_remove_table (void *table) { -struct ptdesc *ptdesc = (struct ptdesc *)_table; +struct ptdesc *ptdesc =page_to_ptdesc (table);pagetable_dtor (ptdesc);pagetable_free (ptdesc);And now observe that
__tlb_remove_table()is identical toasm-generic/pgalloc.hpte_free(),pmd_free(),__pud_free()and__p4d_free().
Quoted 4 lines
Quoted 1 line
And I'm sure we don't need 5 copies of this :-), wdyt?
Argh, nearly, it has the whole page vs virt nonsense still going on. Bah.
Yes, maybe it can be unified into struct page parameter, which seems to be more convenient for conversion:
static inline void pagtable_dtor_free(struct mm_struct *mm, void *table)
{
struct ptdesc *ptdesc = page_to_ptdesc(table);
pagetable_dtor(ptdesc);
pagetable_free(ptdesc);
}Peter Zijlstra
maintainerRe: [PATCH 11/12] mm: pgtable: introduce generic __tlb_remove_table()
In reply to Qi ZhengQuoted 23 lines · Qi Zheng
Quoted 1 line · Peter Zijlstra
On Sat, Dec 14, 2024 at 05:02:57PM +0800, Qi Zheng wrote:
[...]
Quoted 13 lines
Review
Quoted 9 lines
(quoted diff) +9
-0+#ifndef __HAVE_ARCH_TLB_REMOVE_TABLE +static inline void __tlb_remove_table(void *_table) +{ + struct ptdesc *ptdesc = (struct ptdesc *)_table; + + pagetable_dtor(ptdesc); + pagetable_free(ptdesc); +} +#endifSpot the fail...
Quoted 2 lines
That said, all this ptdesc stuff is another giant trainwreck. Let me clean that up for you.
Quoted 6 lines
It looks like you want to revert what was done in this patch series:
https://lore.kernel.org/all/[email protected]/
But why? It seems that splitting ptdesc from struct page is a good thing?
Because we're explicitly allocating pages for the page-tables, and also, code like:
tlb_remove_page_ptdesc((tlb), (page_ptdesc(pte)));
static inline void tlb_remove_page_ptdesc(struct mmu_gather *tlb, struct ptdesc *pt)
{
tlb_remove_page(tlb, ptdesc_page(pt));
}Just makes me upset.
Just bloody write tlb_remove_page() and call it a day.
All that nonsense is just obfuscation at this point.
Re: [PATCH 11/12] mm: pgtable: introduce generic __tlb_remove_table()
Quoted 34 lines · Peter Zijlstra
Quoted 23 lines · Qi Zheng
Quoted 1 line · Peter Zijlstra
On Sat, Dec 14, 2024 at 05:02:57PM +0800, Qi Zheng wrote:
[...]
Quoted 13 lines
Review
Quoted 9 lines
(quoted diff) +9
-0+#ifndef __HAVE_ARCH_TLB_REMOVE_TABLE +static inline void __tlb_remove_table(void *_table) +{ + struct ptdesc *ptdesc = (struct ptdesc *)_table; + + pagetable_dtor(ptdesc); + pagetable_free(ptdesc); +} +#endifSpot the fail...
Quoted 2 lines
That said, all this ptdesc stuff is another giant trainwreck. Let me clean that up for you.
Quoted 6 lines
It looks like you want to revert what was done in this patch series:
https://lore.kernel.org/all/[email protected]/
But why? It seems that splitting ptdesc from struct page is a good thing?
Because we're explicitly allocating pages for the page-tables, and also, code like:
Quoted 8 lines
tlb_remove_page_ptdesc((tlb), (page_ptdesc(pte)));static inline void
tlb_remove_page_ptdesc(struct mmu_gather *tlb, struct ptdesc *pt){ tlb_remove_page(tlb, ptdesc_page(pt)); }Just makes me upset.
Aha, this strikes me as odd too.
Also +CC Vishal Moola, the author of ptdesc, who may be able to provide more background information. If Vishal has no objection, I will try to remove tlb_remove_ptdesc() and tlb_remove_page_ptdesc() as Peter suggested.
Quoted 4 lines
Just bloody write
tlb_remove_page()and call it a day.All that nonsense is just obfuscation at this point.
In addition, will remove the duplicates of __tlb_remove_table, asm-generic/pgalloc.h pte_free(), pmd_free(), __pud_free() and __p4d_free() as follows:
include/asm-generic/pgalloc.h +4 -19
@@ -107,10 +107,7 @@ static inline pgtable_t pte_alloc_one_noprof(struct mm_struct *mm)*/ static inline void pte_free (struct mm_struct *mm,struct page *pte_page) { -struct ptdesc *ptdesc =page_ptdesc (pte_page); - -pagetable_dtor (ptdesc); -pagetable_free (ptdesc); +pagetable_dtor_free (pte_page); }
@@ -150,11 +147,7 @@ static inline pmd_t *pmd_alloc_one_noprof(struct mm_struct *mm, unsigned long ad#ifndef __HAVE_ARCH_PMD_FREEstatic inline void pmd_free (struct mm_struct *mm,pmd_t *pmd) { -struct ptdesc *ptdesc =virt_to_ptdesc (pmd); - -BUG_ON ((unsigned long )pmd & (PAGE_SIZE -1 )); -pagetable_dtor (ptdesc); -pagetable_free (ptdesc); +pagetable_dtor_free (virt_to_page (pmd)); }#endif
@@ -199,11 +192,7 @@ static inline pud_t *pud_alloc_one_noprof(struct mm_struct *mm, unsigned long adstatic inline void __pud_free (struct mm_struct *mm,pud_t *pud) { -struct ptdesc *ptdesc =virt_to_ptdesc (pud); - -BUG_ON ((unsigned long )pud & (PAGE_SIZE -1 )); -pagetable_dtor (ptdesc); -pagetable_free (ptdesc); +pagetable_dtor_free (virt_to_page (pud)); }#ifndef __HAVE_ARCH_PUD_FREE
@@ -254,11 +243,7 @@ static inline p4d_t *p4d_alloc_one_noprof(struct mm_struct *mm, unsigned long adstatic inline void __p4d_free (struct mm_struct *mm,p4d_t *p4d) { -struct ptdesc *ptdesc =virt_to_ptdesc (p4d); - -BUG_ON ((unsigned long )p4d & (PAGE_SIZE -1 )); -pagetable_dtor (ptdesc); -pagetable_free (ptdesc); +pagetable_dtor_free (virt_to_page (p4d)); }#ifndef __HAVE_ARCH_P4D_FREE
include/asm-generic/tlb.h +1 -4
@@ -211,10 +211,7 @@ struct mmu_table_batch {#ifndef __HAVE_ARCH_TLB_REMOVE_TABLEstatic inline void __tlb_remove_table (void *_table) { -struct ptdesc *ptdesc = (struct ptdesc *)_table; - -pagetable_dtor (ptdesc); -pagetable_free (ptdesc); +pagetable_dtor_free (_table); }#endif
include/linux/mm.h +8 -0
@@ -3064,6 +3064,14 @@ static inline void pagetable_dtor(struct ptdesc *ptdesc)lruvec_stat_sub_folio (folio,NR_PAGETABLE ); } +static inline void pagetable_dtor_free (void *table) +{ +struct ptdesc *ptdesc =page_ptdesc ((struct page *)table); + +pagetable_dtor (ptdesc); +pagetable_dtor (ptdesc); +} +static inline bool pagetable_pte_ctor (struct ptdesc *ptdesc) {struct folio *folio =ptdesc_folio (ptdesc);
Thanks!
Peter Zijlstra
maintainerRe: [PATCH 11/12] mm: pgtable: introduce generic __tlb_remove_table()
Quoted 6 lines · Qi Zheng
include/linux/mm.h +0
-0@@ -3064,6 +3064,14 @@ static inline void pagetable_dtor(struct ptdesc *ptdesc)
include/linux/mm.h static inline void pagetable_dtor(struct ptdesc · line 3074
Quoted 10 lines
include/linux/mm.h +7
-0lruvec_stat_sub_folio (folio,NR_PAGETABLE ); } +static inline void pagetable_dtor_free (void *table) +{ +struct ptdesc *ptdesc =page_ptdesc ((struct page *)table); + +pagetable_dtor (ptdesc); +pagetable_dtor (ptdesc); +}
Right, that works, except you have whitespace issues and I think you'll find it'll work better if you don't call _dtor twice but instead replace that last one with _free() :-)
Re: [PATCH 11/12] mm: pgtable: introduce generic __tlb_remove_table()
Quoted 10 lines · Peter Zijlstra
Quoted 9 lines · Qi Zheng
include/linux/mm.h +0
-0@@ -3064,6 +3064,14 @@ static inline void pagetable_dtor(struct ptdesc *ptdesc)lruvec_stat_sub_folio (folio,NR_PAGETABLE ); }
Quoted 11 lines
include/linux/mm.h static inline void pagetable_dtor(struct ptdesc *ptdesc) · line 3073
Quoted 7 lines
include/linux/mm.h +7
-0+static inline void pagetable_dtor_free (void *table) +{ +struct ptdesc *ptdesc =page_ptdesc ((struct page *)table); + +pagetable_dtor (ptdesc); +pagetable_dtor (ptdesc); +}Right, that works, except you have whitespace issues and I think you'll find it'll work better if you don't call
_dtortwice but instead replace that last one with_free():-)
Ah, stupid thing I did, please ignore it. ;)
Will add this to v2.
Thanks!
Re: [PATCH 02/12] mm: pgtable: introduce generic p4d_alloc_one() and p4d_free()
In reply to Qi ZhengHi,
Quoted 211 lines · Qi Zheng
Several architectures (arm64, riscv, x86) define
p4d_alloc_one()as a wrapper forget_zeroed_page()andp4d_free()as a wrapper forfree_page().For these architectures, provide a generic implementation in
asm-generic/pgalloc.hand convert them to use it. And like other levels of page tables, add statistics for P4D level page table.For s390, it also defines
p4d_alloc_one()andp4d_free(), but it uses its own logic, so skip it.Signed-off-by Qi Zheng <[email protected]>--- arch/arm64/include/asm/pgalloc.h | 15 ++++----- arch/riscv/include/asm/pgalloc.h | 25 ++++++--------- arch/x86/include/asm/pgalloc.h | 16 ++++------ arch/x86/mm/pgtable.c | 3 ++ include/asm-generic/pgalloc.h | 55 ++++++++++++++++++++++++++++++++ include/linux/mm.h | 16 ++++++++++ 6 files changed, 98 insertions(+), 32 deletions(-)arch/arm64/include/asm/pgalloc.h +7
-8@@ -15,6 +15,8 @@#define __HAVE_ARCH_PGD_FREE#define __HAVE_ARCH_PUD_FREE +#define __HAVE_ARCH_P4D_ALLOC_ONE +#define __HAVE_ARCH_P4D_FREE#include <asm-generic/pgalloc.h> #define PGD_SIZE (PTRS_PER_PGD * sizeof(pgd_t))@@ -87,19 +89,16 @@ static inline void pgd_populate(struct mm_struct *mm, pgd_t *pgdp, p4d_t *p4dp)static inline p4d_t *p4d_alloc_one (struct mm_struct *mm,unsigned long addr) { -gfp_t gfp =GFP_PGTABLE_USER ; +if (!pgtable_l5_enabled ()) +return NULL ; -if (mm == &init_mm) - gfp =GFP_PGTABLE_KERNEL ; -return (p4d_t *)get_zeroed_page (gfp); +return __p4d_alloc_one (mm, addr); }static inline void p4d_free (struct mm_struct *mm,p4d_t *p4d) { -if (!pgtable_l5_enabled ()) -return ; -BUG_ON ((unsigned long )p4d & (PAGE_SIZE -1 )); -free_page ((unsigned long )p4d); +if (pgtable_l5_enabled ()) +__p4d_free (mm, p4d); }#define __p4d_free_tlb (tlb, p4d, addr) p4d_free((tlb)->mm, p4d)arch/riscv/include/asm/pgalloc.h +10
-15@@ -14,6 +14,8 @@#ifdef CONFIG_MMU #define __HAVE_ARCH_PUD_ALLOC_ONE#define __HAVE_ARCH_PUD_FREE +#define __HAVE_ARCH_P4D_ALLOC_ONE +#define __HAVE_ARCH_P4D_FREE#include <asm-generic/pgalloc.h> static inline void riscv_tlb_remove_ptdesc (struct mmu_gather *tlb,void *pt)@@ -118,21 +120,10 @@ static inline void __pud_free_tlb(struct mmu_gather *tlb, pud_t *pud,#define p4d_alloc_one p4d_alloc_onestatic inline p4d_t *p4d_alloc_one (struct mm_struct *mm,unsigned long addr) { -if (pgtable_l5_enabled) { -gfp_t gfp =GFP_PGTABLE_USER ; - -if (mm == &init_mm) - gfp =GFP_PGTABLE_KERNEL ; -return (p4d_t *)get_zeroed_page (gfp); - } +if (!pgtable_l5_enabled) +return NULL ; -return NULL ; -} - -static inline void __p4d_free (struct mm_struct *mm,p4d_t *p4d) -{ -BUG_ON ((unsigned long )p4d & (PAGE_SIZE -1 )); -free_page ((unsigned long )p4d); +return __p4d_alloc_one (mm, addr); }#define p4d_free p4d_free@@ -145,8 +136,12 @@ static inline void p4d_free(struct mm_struct *mm, p4d_t *p4d)static inline void __p4d_free_tlb (struct mmu_gather *tlb,p4d_t *p4d,unsigned long addr) { -if (pgtable_l5_enabled) +if (pgtable_l5_enabled) { +struct ptdesc *ptdesc =virt_to_ptdesc (p4d); + +pagetable_p4d_dtor (ptdesc);riscv_tlb_remove_ptdesc (tlb,virt_to_ptdesc (p4d)); + } }#endif /* __PAGETABLE_PMD_FOLDED */ arch/x86/include/asm/pgalloc.h +7
-9@@ -8,6 +8,8 @@#define __HAVE_ARCH_PTE_ALLOC_ONE#define __HAVE_ARCH_PGD_FREE +#define __HAVE_ARCH_P4D_ALLOC_ONE +#define __HAVE_ARCH_P4D_FREE#include <asm-generic/pgalloc.h> static inline int __paravirt_pgd_alloc (struct mm_struct *mm) {return 0 ; }@@ -149,20 +151,16 @@ static inline void pgd_populate_safe(struct mm_struct *mm, pgd_t *pgd, p4d_t *p4static inline p4d_t *p4d_alloc_one (struct mm_struct *mm,unsigned long addr) { -gfp_t gfp =GFP_KERNEL_ACCOUNT ; +if (!pgtable_l5_enabled ()) +return NULL ; -if (mm == &init_mm) - gfp &= ~__GFP_ACCOUNT; -return (p4d_t *)get_zeroed_page (gfp); +return __p4d_alloc_one (mm, addr); }static inline void p4d_free (struct mm_struct *mm,p4d_t *p4d) { -if (!pgtable_l5_enabled ()) -return ; - -BUG_ON ((unsigned long )p4d & (PAGE_SIZE -1 )); -free_page ((unsigned long )p4d); +if (pgtable_l5_enabled ()) +return __p4d_free (mm, p4d); }extern void ___p4d_free_tlb (struct mmu_gather *tlb,p4d_t *p4d);arch/x86/mm/pgtable.c +3
-0@@ -94,6 +94,9 @@ void ___pud_free_tlb(struct mmu_gather *tlb, pud_t *pud)#if CONFIG_PGTABLE_LEVELS >4 void ___p4d_free_tlb (struct mmu_gather *tlb,p4d_t *p4d) { +struct ptdesc *ptdesc =virt_to_ptdesc (p4d); + +pagetable_p4d_dtor (ptdesc);paravirt_release_p4d (__pa (p4d) >>PAGE_SHIFT );paravirt_tlb_remove_table (tlb,virt_to_page (p4d)); }include/asm-generic/pgalloc.h +40
-0@@ -215,6 +215,61 @@ static inline void pud_free(struct mm_struct *mm, pud_t *pud)#endif /* CONFIG_PGTABLE_LEVELS > 3 */ +#if CONFIG_PGTABLE_LEVELS >4 + +static inline p4d_t *__p4d_alloc_one_noprof (struct mm_struct *mm,unsigned long addr) +{ +gfp_t gfp =GFP_PGTABLE_USER ; +struct ptdesc *ptdesc; + +if (mm == &init_mm) + gfp =GFP_PGTABLE_KERNEL ; + gfp &= ~__GFP_HIGHMEM; + + ptdesc =pagetable_alloc_noprof (gfp,0 ); +if (!ptdesc) +return NULL ; + +pagetable_p4d_ctor (ptdesc); +return ptdesc_address (ptdesc); +} +#define __p4d_alloc_one (...) alloc_hooks(__p4d_alloc_one_noprof(__VA_ARGS__)) + +#ifndef __HAVE_ARCH_P4D_ALLOC_ONE +/** +* p4d_alloc_one - allocate memory for a P4D-level page table +* @mm: the mm_struct of the current context +* +* Allocate memory for a page table using %GFP_PGTABLE_USER for user context +* and %GFP_PGTABLE_KERNEL for kernel context. +* +* Return: pointer to the allocated memory or %NULL on error +*/ +static inline p4d_t *p4d_alloc_one_noprof (struct mm_struct *mm,unsigned long addr) +{ +return __p4d_alloc_one_noprof (mm, addr); +} +#define p4d_alloc_one (...) alloc_hooks(p4d_alloc_one_noprof(__VA_ARGS__)) +#endif + +static inline void __p4d_free (struct mm_struct *mm,p4d_t *p4d) +{ +struct ptdesc *ptdesc =virt_to_ptdesc (p4d);
include/asm-generic/pgalloc.h static inline void pud_free(struct mm_struct *mm, pud_t *pud) · line 267
Quoted 10 lines
include/asm-generic/pgalloc.h +8
-0+ +BUG_ON ((unsigned long )p4d & (PAGE_SIZE -1 )); +pagetable_p4d_dtor (ptdesc); +pagetable_free (ptdesc); +} + +#ifndef __HAVE_ARCH_P4D_FREE +static inline void p4d_free (struct mm_struct *mm,pud_t *p4d)
Should this perhaps be p4d_t *p4d rather than pud_t *p4d?
Otherwise I get this build error:
In file included from /home/klara/git/linux/arch/riscv/include/asm/kfence.h:8,
from /home/klara/git/linux/mm/kfence/core.c:34:
/home/klara/git/linux/include/asm-generic/pgalloc.h: In function ‘p4d_free’:
/home/klara/git/linux/include/asm-generic/pgalloc.h:252:24: error: passing argument 2 of ‘__p4d_free’ from incompatible pointer type [-Wincompatible-pointer-types]
252 | __p4d_free(mm, p4d);
| ^~~
| |
| pud_t */home/klara/git/linux/include/asm-generic/pgalloc.h:244:60: note: expected ‘p4d_t *’ but argument is of type ‘pud_t *’
244 | static inline void __p4d_free(struct mm_struct *mm, p4d_t *p4d)
| ~~~~~~~^~~
In file included from /home/klara/git/linux/arch/riscv/include/asm/kfence.h:8,
from /home/klara/git/linux/mm/kfence/report.c:22:/home/klara/git/linux/include/asm-generic/pgalloc.h: In function ‘p4d_free’:/home/klara/git/linux/include/asm-generic/pgalloc.h:252:24: error: passing argument 2 of ‘__p4d_free’ from incompatible pointer type [-Wincompatible-pointer-types]
252 | __p4d_free(mm, p4d);
| ^~~
| |
| pud_t */home/klara/git/linux/include/asm-generic/pgalloc.h:244:60: note: expected ‘p4d_t *’ but argument is of type ‘pud_t *’
244 | static inline void __p4d_free(struct mm_struct *mm, p4d_t *p4d)
| ~~~~~~~^~~
Regards,
Klara Modin
Quoted 37 lines
include/asm-generic/pgalloc.h +7
-0+{ +__p4d_free (mm, p4d); +} +#endif + +#endif +#ifndef __HAVE_ARCH_PGD_FREEstatic inline void pgd_free(struct mm_struct *mm, pgd_t *pgd) {include/linux/mm.h +16
-0@@ -3237,6 +3237,22 @@ static inline void pagetable_pud_dtor(struct ptdesc *ptdesc)lruvec_stat_sub_folio (folio,NR_PAGETABLE ); } +static inline void pagetable_p4d_ctor (struct ptdesc *ptdesc) +{ +struct folio *folio =ptdesc_folio (ptdesc); + +__folio_set_pgtable (folio); +lruvec_stat_add_folio (folio,NR_PAGETABLE ); +} + +static inline void pagetable_p4d_dtor (struct ptdesc *ptdesc) +{ +struct folio *folio =ptdesc_folio (ptdesc); + +__folio_clear_pgtable (folio); +lruvec_stat_sub_folio (folio,NR_PAGETABLE ); +} +extern void __init pagecache_init (void );extern void free_initmem (void );
Re: [PATCH 02/12] mm: pgtable: introduce generic p4d_alloc_one() and p4d_free()
Quoted 215 lines · Klara Modin
Hi,
Quoted 210 lines · Qi Zheng
Several architectures (arm64, riscv, x86) define
p4d_alloc_one()as a wrapper forget_zeroed_page()andp4d_free()as a wrapper forfree_page().For these architectures, provide a generic implementation in
asm-generic/pgalloc.hand convert them to use it. And like other levels of page tables, add statistics for P4D level page table.For s390, it also defines
p4d_alloc_one()andp4d_free(), but it uses its own logic, so skip it.Signed-off-by Qi Zheng <[email protected]>---
arch/arm64/include/asm/pgalloc.h| 15 ++++-----arch/riscv/include/asm/pgalloc.h| 25 ++++++--------- arch/x86/include/asm/pgalloc.h | 16 ++++------ arch/x86/mm/pgtable.c | 3 ++ include/asm-generic/pgalloc.h | 55 ++++++++++++++++++++++++++++++++ include/linux/mm.h | 16 ++++++++++6 files changed, 98 insertions(+), 32 deletions(-)arch/arm64/include/asm/pgalloc.h +7
-8@@ -15,6 +15,8 @@ #define __HAVE_ARCH_PGD_FREE #define __HAVE_ARCH_PUD_FREE +#define __HAVE_ARCH_P4D_ALLOC_ONE +#define __HAVE_ARCH_P4D_FREE #include <asm-generic/pgalloc.h> #define PGD_SIZE (PTRS_PER_PGD * sizeof(pgd_t))@@ -87,19 +89,16 @@ static inline void pgd_populate(struct mm_struct *mm, pgd_t *pgdp, p4d_t *p4dp) static inline p4d_t *p4d_alloc_one(struct mm_struct *mm, unsigned long addr) { - gfp_t gfp = GFP_PGTABLE_USER; + if (!pgtable_l5_enabled()) + return NULL; - if (mm == &init_mm) - gfp = GFP_PGTABLE_KERNEL; - return (p4d_t *)get_zeroed_page(gfp); + return __p4d_alloc_one(mm, addr); } static inline void p4d_free(struct mm_struct *mm, p4d_t *p4d) { - if (!pgtable_l5_enabled()) - return; - BUG_ON((unsigned long)p4d & (PAGE_SIZE-1)); - free_page((unsigned long)p4d); + if (pgtable_l5_enabled()) + __p4d_free(mm, p4d); } #define __p4d_free_tlb(tlb, p4d, addr) p4d_free((tlb)->mm, p4d)arch/riscv/include/asm/pgalloc.h +10
-15@@ -14,6 +14,8 @@ #ifdef CONFIG_MMU #define __HAVE_ARCH_PUD_ALLOC_ONE #define __HAVE_ARCH_PUD_FREE +#define __HAVE_ARCH_P4D_ALLOC_ONE +#define __HAVE_ARCH_P4D_FREE #include <asm-generic/pgalloc.h> static inline void riscv_tlb_remove_ptdesc(struct mmu_gather *tlb, void *pt)@@ -118,21 +120,10 @@ static inline void __pud_free_tlb(struct mmu_gather *tlb, pud_t *pud, #define p4d_alloc_one p4d_alloc_one static inline p4d_t *p4d_alloc_one(struct mm_struct *mm, unsigned long addr) { - if (pgtable_l5_enabled) { - gfp_t gfp = GFP_PGTABLE_USER; - - if (mm == &init_mm) - gfp = GFP_PGTABLE_KERNEL; - return (p4d_t *)get_zeroed_page(gfp); - } + if (!pgtable_l5_enabled) + return NULL; - return NULL; -} - -static inline void __p4d_free (struct mm_struct *mm,p4d_t *p4d) -{ - BUG_ON((unsigned long)p4d & (PAGE_SIZE-1)); - free_page((unsigned long)p4d); + return __p4d_alloc_one(mm, addr); } #define p4d_free p4d_free@@ -145,8 +136,12 @@ static inline void p4d_free(struct mm_struct *mm, p4d_t *p4d) static inline void __p4d_free_tlb(struct mmu_gather *tlb, p4d_t *p4d, unsigned long addr) { - if (pgtable_l5_enabled) + if (pgtable_l5_enabled) { + struct ptdesc *ptdesc = virt_to_ptdesc(p4d); + + pagetable_p4d_dtor(ptdesc); riscv_tlb_remove_ptdesc(tlb, virt_to_ptdesc(p4d)); + } } #endif /* __PAGETABLE_PMD_FOLDED */arch/x86/include/asm/pgalloc.h +2
-0@@ -8,6 +8,8 @@ #define __HAVE_ARCH_PTE_ALLOC_ONE #define __HAVE_ARCH_PGD_FREE +#define __HAVE_ARCH_P4D_ALLOC_ONE +#define __HAVE_ARCH_P4D_FREE #include <asm-generic/pgalloc.h> static inline int __paravirt_pgd_alloc(struct mm_struct *mm) { return 0; }@@ -149,20 +151,16 @@ static inline void pgd_populate_safe(struct
mm_struct*mm,pgd_t*pgd,p4d_t*p4
static inlinep4d_t*p4d_alloc_one(structmm_struct*mm, unsigned long addr) {
-gfp_tgfp =GFP_KERNEL_ACCOUNT;
+ if (!pgtable_l5_enabled())
+ return NULL;
- if (mm ==&init_mm)
- gfp &= ~__GFP_ACCOUNT;
- return (p4d_t*)get_zeroed_page(gfp);
+ return__p4d_alloc_one(mm, addr);
}
static inline voidp4d_free(struct mm_struct *mm, p4d_t *p4d)
{
- if (!pgtable_l5_enabled())
- return;
-
-BUG_ON((unsigned long)p4d & (PAGE_SIZE-1));
-free_page((unsigned long)p4d);
+ if (pgtable_l5_enabled())
+ return__p4d_free(mm, p4d);
}
extern void___p4d_free_tlb(struct mmu_gather *tlb, p4d_t *p4d);arch/x86/mm/pgtable.c +3
-0@@ -94,6 +94,9 @@ void ___pud_free_tlb(struct mmu_gather *tlb, pud_t *pud) #if CONFIG_PGTABLE_LEVELS > 4 void ___p4d_free_tlb(struct mmu_gather *tlb, p4d_t *p4d) { + struct ptdesc *ptdesc = virt_to_ptdesc(p4d); + + pagetable_p4d_dtor(ptdesc); paravirt_release_p4d(__pa(p4d) >> PAGE_SHIFT); paravirt_tlb_remove_table(tlb, virt_to_page(p4d));}
include/asm-generic/pgalloc.h +26
-0@@ -215,6 +215,61 @@ static inline void pud_free(struct mm_struct *mm, pud_t *pud) #endif /* CONFIG_PGTABLE_LEVELS > 3 */ +#if CONFIG_PGTABLE_LEVELS >4 + +static inline p4d_t *__p4d_alloc_one_noprof (struct mm_struct *mm,unsigned long addr) +{ + gfp_t gfp = GFP_PGTABLE_USER; + struct ptdesc *ptdesc; + + if (mm == &init_mm) + gfp = GFP_PGTABLE_KERNEL; + gfp &= ~__GFP_HIGHMEM; + + ptdesc = pagetable_alloc_noprof(gfp, 0); + if (!ptdesc) + return NULL; + + pagetable_p4d_ctor(ptdesc); + return ptdesc_address(ptdesc); +} +#define __p4d_alloc_one (...) alloc_hooks(__p4d_alloc_one_noprof(__VA_ARGS__)) + +#ifndef __HAVE_ARCH_P4D_ALLOC_ONE +/** +* p4d_alloc_one - allocate memory for a P4D-level page table +* @mm: the mm_struct of the current context +* +* Allocate memory for a page table using %GFP_PGTABLE_USER for user context
+ * and %GFP_PGTABLE_KERNEL for kernel context.
+ *
+ * Return: pointer to the allocated memory or %NULL on error
+ */
+static inlinep4d_t*p4d_alloc_one_noprof(structmm_struct*mm, unsigned long addr)include/asm-generic/pgalloc.h +4
-0+{ + return __p4d_alloc_one_noprof(mm, addr); +} +#define p4d_alloc_one (...)alloc_hooks(p4d_alloc_one_noprof(__VA_ARGS__))
include/asm-generic/pgalloc.h +11
-0+#endif + +static inline void __p4d_free (struct mm_struct *mm,p4d_t *p4d) +{ + struct ptdesc *ptdesc = virt_to_ptdesc(p4d); + + BUG_ON((unsigned long)p4d & (PAGE_SIZE-1)); + pagetable_p4d_dtor(ptdesc); + pagetable_free(ptdesc); +} +
Quoted 4 lines
include/asm-generic/pgalloc.h static inline void pud_free(struct mm_struct *mm, pud_t *pud) · line 265
Quoted 2 lines
include/asm-generic/pgalloc.h +2
-0+#ifndef __HAVE_ARCH_P4D_FREE +static inline void p4d_free (struct mm_struct *mm,pud_t *p4d)Should this perhaps be
p4d_t*p4d rather thanpud_t*p4d?
Yes, my bad. Will fix it.
Thanks!
Quoted 66 lines
Otherwise I get this build error:
In file included from
/home/klara/git/linux/arch/riscv/include/asm/kfence.h:8,
from/home/klara/git/linux/mm/kfence/core.c:34:/home/klara/git/linux/include/asm-generic/pgalloc.h: In function ‘p4d_free’:/home/klara/git/linux/include/asm-generic/pgalloc.h:252:24: error: passing argument 2 of ‘__p4d_free’ from incompatible pointer type [-Wincompatible-pointer-types]
252 |__p4d_free(mm, p4d);| ^~~ | | | pud_t *
/home/klara/git/linux/include/asm-generic/pgalloc.h:244:60: note: expected ‘p4d_t *’ but argument is of type ‘pud_t *’
244 | static inline void__p4d_free(struct mm_struct *mm, p4d_t *p4d)
| ~~~~~~~^~~
In file included from/home/klara/git/linux/arch/riscv/include/asm/kfence.h:8,
from/home/klara/git/linux/mm/kfence/report.c:22:/home/klara/git/linux/include/asm-generic/pgalloc.h: In function ‘p4d_free’:/home/klara/git/linux/include/asm-generic/pgalloc.h:252:24: error: passing argument 2 of ‘__p4d_free’ from incompatible pointer type [-Wincompatible-pointer-types]
252 |__p4d_free(mm, p4d);| ^~~ | | | pud_t *
/home/klara/git/linux/include/asm-generic/pgalloc.h:244:60: note: expected ‘p4d_t *’ but argument is of type ‘pud_t *’
244 | static inline void__p4d_free(struct mm_struct *mm, p4d_t *p4d)
| ~~~~~~~^~~Regards,
Klara ModinQuoted 36 lines
include/asm-generic/pgalloc.h +7
-0+{ + __p4d_free(mm, p4d); +} +#endif + +#endif +#ifndef
__HAVE_ARCH_PGD_FREE
static inline voidpgd_free(struct mm_struct *mm, pgd_t *pgd){include/linux/mm.h +16
-0@@ -3237,6 +3237,22 @@ static inline void pagetable_pud_dtor(struct ptdesc *ptdesc) lruvec_stat_sub_folio(folio, NR_PAGETABLE); } +static inline void pagetable_p4d_ctor (struct ptdesc *ptdesc) +{ + struct folio *folio = ptdesc_folio(ptdesc); + + __folio_set_pgtable(folio); + lruvec_stat_add_folio(folio, NR_PAGETABLE); +} + +static inline void pagetable_p4d_dtor (struct ptdesc *ptdesc) +{ + struct folio *folio = ptdesc_folio(ptdesc); + + __folio_clear_pgtable(folio); + lruvec_stat_sub_folio(folio, NR_PAGETABLE); +} + extern void __init pagecache_init(void); extern void free_initmem(void);