mm: Fix page-table teardown during memory hot-remove
Subsystems (5)
v3 cover mm: Fix page-table teardown during memory hot-remove
Memory hot-remove can free page-table pages allocated through paths with different constructor state. Some paths run the page-table constructor, while generic sparse-vmemmap population may not. The x86, RISC-V, and arm64 teardown paths did not handle that distinction consistently, leaving constructor/destructor accounting unbalanced.
Make pagetable_free() handle any remaining constructor state before freeing a page-table page. Callers that already ran pagetable_dtor() have cleared that state, so the destructor is not run twice. The RISC-V and arm64 fixes then use this common helper for their memory-hot-remove paths.
The architecture fixes depend on the common MM change in patch 1, so this remains a single series. I expect it to go through the MM tree, with review or acks from the relevant architecture maintainers.
v3:
- Explain why pagetable_free() does not run the destructor twice.
- Add the x86 stable prerequisites noted by Jose A. Perez de Azpillaga.
- Collect Acked-by and Reviewed-by tags from reviewers.
v2: https://lore.kernel.org/[email protected]/
- Move the destructor decision from x86 into pagetable_free(), as suggested by David Hildenbrand.
- Use pagetable_free() for non-reserved RISC-V page-table pages and simplify reserved-page handling.
- Collect David Hildenbrand's Acked-by on the RISC-V PUD fix.
- Use pagetable_free() for arm64 intermediate page-table pages, as suggested by David Hildenbrand.
- Trim and update the commit messages, as requested by Dave Hansen.
v1: https://lore.kernel.org/[email protected]/
Muchun Song (4):
mm: fix missing page-table destructor in pagetable_free() riscv/mm: fix hotplug page-table destructor handling riscv/mm: fix missing destructor for hotplug PUD tables arm64/mm: fix destructor for unconstructed hotplug page tables
arch/arm64/mm/mmu.c | 3 +-- arch/riscv/mm/init.c | 32 +++++++++++++------------------- include/linux/mm.h | 37 ++++++++++++++++++++----------------- 3 files changed, 34 insertions(+), 38 deletions(-)
base-commit: 26d7e34b4226709084461a07bf28d9066fa96d8c
Signature · 1 line
2.54.0
PATCH v3 1/4 mm: fix missing page-table destructor in pagetable_free()
Since commit 49f599666420 ("mm: call ctor/dtor for kernel PTEs"), pte_alloc_one_kernel() runs the page-table constructor for kernel PTE tables.
HVO (HugeTLB Vmemmap Optimization) uses pte_alloc_one_kernel() when splitting a PMD. Restoring the vmemmap backing pages does not collapse the PTE table, so a later memory hot-remove eventually frees it through pagetable_free() on x86-64. That releases the page without running the matching destructor and leaves NR_PAGETABLE elevated.
Make pagetable_free() the common free-side primitive for page-table pages. It checks PageTable() and runs the destructor only when the constructor state is still present. Callers that explicitly ran pagetable_dtor() have already cleared PageTable(), so they are not destructed twice; pages that never ran a constructor are freed directly.
---
v3:
- Explain why pagetable_free() does not run the destructor twice (requested by Kevin Brodsky)
- Add x86 stable prerequisites (suggested by
Jose A. Perez de Azpillaga)
- Collect Acked-by from Dave Hansen and David Hildenbrand
- Collect Reviewed-by from Jose A. Perez de Azpillaga and Kevin Brodsky
v2:
- Move destructor handling from x86 into pagetable_free() (suggested by David Hildenbrand)
- Trim and update the commit message (requested by Dave Hansen)
--- include/linux/mm.h | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-)
include/linux/mm.h +20 -17
@@ -3812,23 +3812,6 @@ static inline void pagetable_free_kernel(struct ptdesc *pt) __pagetable_free(pt); }#endif -/** -* pagetable_free - Free pagetables -* @pt: The page table descriptor -* -* pagetable_free frees the memory of all page tables described by a page -* table descriptor and the memory for the descriptor itself. -*/ -static inline void pagetable_free (struct ptdesc *pt) -{ -if (ptdesc_test_kernel (pt)) { -ptdesc_clear_kernel (pt); -pagetable_free_kernel (pt); - }else { -__pagetable_free (pt); - } -} -#if defined(CONFIG_SPLIT_PTE_PTLOCKS )#if ALLOC_SPLIT_PTLOCKS void __init ptlock_cache_init (void );
@@ -3921,6 +3904,26 @@ static inline void pagetable_dtor(struct ptdesc *ptdesc)lruvec_stat_sub_folio (folio,NR_PAGETABLE ); } +/** +* pagetable_free - Free pagetables +* @pt: The page table descriptor +* +* pagetable_free frees the memory of all page tables described by a page +* table descriptor and the memory for the descriptor itself. +*/ +static inline void pagetable_free (struct ptdesc *pt) +{ +if (PageTable (ptdesc_page (pt))) +pagetable_dtor (pt); + +if (ptdesc_test_kernel (pt)) { +ptdesc_clear_kernel (pt); +pagetable_free_kernel (pt); + }else { +__pagetable_free (pt); + } +} +static inline void pagetable_dtor_free (struct ptdesc *ptdesc) {pagetable_dtor (ptdesc);
Signature · 1 line
2.54.0
Re: [PATCH v3 1/4] mm: fix missing page-table destructor in pagetable_free()
Quoted 16 lines · Muchun Song
Since commit
49f599666420("mm: call ctor/dtor for kernel PTEs"),pte_alloc_one_kernel()runs the page-table constructor for kernel PTE tables.HVO (HugeTLB Vmemmap Optimization) uses
pte_alloc_one_kernel()when splitting a PMD. Restoring the vmemmap backing pages does not collapse the PTE table, so a later memory hot-remove eventually frees it throughpagetable_free()on x86-64. That releases the page without running the matching destructor and leavesNR_PAGETABLEelevated.Make
pagetable_free()the common free-side primitive for page-table pages. It checksPageTable()and runs the destructor only when the constructor state is still present. Callers that explicitly ranpagetable_dtor()have already clearedPageTable(), so they are not destructed twice; pages that never ran a constructor are freed directly.
Quoted 10 lines
Fixes 49f599666420 ("mm: call ctor/dtor for kernel PTEs")Cc [email protected] # bf9e4e30f353: x86/mm: use pagetable_free()Cc [email protected] # 39406c05f8f1: x86/mm: Fix freeing of PMD-sized vmemmap pagesAssisted-by LLMSigned-off-by Muchun Song <[email protected]>Reviewed-by Jose A. Perez de Azpillaga <[email protected]>Reviewed-by Kevin Brodsky <[email protected]>Acked-by Dave Hansen <[email protected]>
Quoted 76 lines
---
v3:
- Explain whypagetable_free()does not run the destructor twice (requested by Kevin Brodsky)
- Add x86 stable prerequisites (suggested by
Jose A. Perez de Azpillaga)
- Collect Acked-by from Dave Hansen and David Hildenbrand
- Collect Reviewed-by from Jose A. Perez de Azpillaga and Kevin Brodskyv2:
- Move destructor handling from x86 intopagetable_free()(suggested by David Hildenbrand)
- Trim and update the commit message (requested by Dave Hansen)--- include/linux/mm.h | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-)include/linux/mm.h +20
-17@@ -3812,23 +3812,6 @@ static inline void pagetable_free_kernel(struct ptdesc *pt) __pagetable_free(pt); }#endif -/** -* pagetable_free - Free pagetables -* @pt: The page table descriptor -* -* pagetable_free frees the memory of all page tables described by a page -* table descriptor and the memory for the descriptor itself. -*/ -static inline void pagetable_free (struct ptdesc *pt) -{ -if (ptdesc_test_kernel (pt)) { -ptdesc_clear_kernel (pt); -pagetable_free_kernel (pt); - }else { -__pagetable_free (pt); - } -} -#if defined(CONFIG_SPLIT_PTE_PTLOCKS )#if ALLOC_SPLIT_PTLOCKS void __init ptlock_cache_init (void );@@ -3921,6 +3904,26 @@ static inline void pagetable_dtor(struct ptdesc *ptdesc)lruvec_stat_sub_folio (folio,NR_PAGETABLE ); } +/** +* pagetable_free - Free pagetables +* @pt: The page table descriptor +* +* pagetable_free frees the memory of all page tables described by a page +* table descriptor and the memory for the descriptor itself. +*/ +static inline void pagetable_free (struct ptdesc *pt) +{ +if (PageTable (ptdesc_page (pt))) +pagetable_dtor (pt); + +if (ptdesc_test_kernel (pt)) { +ptdesc_clear_kernel (pt); +pagetable_free_kernel (pt); + }else { +__pagetable_free (pt); + } +} +static inline void pagetable_dtor_free (struct ptdesc *ptdesc) {pagetable_dtor (ptdesc);Signature · 1 line
2.54.0
PATCH v3 2/4 riscv/mm: fix hotplug page-table destructor handling
In reply to Muchun SongRISC-V uses the same memory-hotplug teardown code for linear-map and vmemmap page tables. Late linear-map allocations run page-table constructors, while vmemmap and early allocations may not.
free_pte_table() unconditionally calls pagetable_dtor(), corrupting NR_PAGETABLE accounting for constructor-free tables. free_pmd_table() avoids that for vmemmap using is_vmemmap, but mapping type is not a reliable proxy for constructor state. In particular, it becomes invalid once runtime vmemmap tables use the normal pgalloc helpers.
Introduce free_pgtable_page() to select free_reserved_page() for reserved pages and pagetable_free() otherwise. The latter runs the destructor only when PageTable() is set, handling both constructor-backed and constructor-free tables.
---
v3:
- Collect Acked-by from David Hildenbrand
- Collect Reviewed-by from Björn Töpel and Kevin Brodsky
v2:
- Use pagetable_free() for non-reserved tables (suggested by David Hildenbrand)
- Simplify reserved-page handling
--- arch/riscv/mm/init.c | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-)
arch/riscv/mm/init.c +12 -15
@@ -1485,10 +1485,17 @@ struct execmem_info __init *execmem_arch_setup(void)#endif /* CONFIG_EXECMEM */ #ifdef CONFIG_MEMORY_HOTPLUG +static void __meminit free_pgtable_page (struct page *page) +{ +if (PageReserved (page)) +free_reserved_page (page); +else +pagetable_free (page_ptdesc (page)); +} +static void __meminit free_pte_table (pte_t *pte_start,pmd_t *pmd) {struct page *page =pmd_page (*pmd); -struct ptdesc *ptdesc =page_ptdesc (page);pte_t *pte;int i;
@@ -1498,18 +1505,13 @@ static void __meminit free_pte_table(pte_t *pte_start, pmd_t *pmd)return ; } -pagetable_dtor (ptdesc); -if (PageReserved (page)) -free_reserved_page (page); -else -pagetable_free (ptdesc); +free_pgtable_page (page);pmd_clear (pmd); } -static void __meminit free_pmd_table (pmd_t *pmd_start,pud_t *pud,bool is_vmemmap) +static void __meminit free_pmd_table (pmd_t *pmd_start,pud_t *pud) {struct page *page =pud_page (*pud); -struct ptdesc *ptdesc =page_ptdesc (page);pmd_t *pmd;int i;
@@ -1519,12 +1521,7 @@ static void __meminit free_pmd_table(pmd_t *pmd_start, pud_t *pud, bool is_vmemmreturn ; } -if (!is_vmemmap) -pagetable_dtor (ptdesc); -if (PageReserved (page)) -free_reserved_page (page); -else -pagetable_free (ptdesc); +free_pgtable_page (page);pud_clear (pud); }
@@ -1644,7 +1641,7 @@ static void __meminit remove_pud_mapping(pud_t *pud_base, unsigned long addr, unremove_pmd_mapping (pmd_base, addr, next, is_vmemmap, altmap);if (pgtable_l4_enabled) -free_pmd_table (pmd_base, pudp, is_vmemmap); +free_pmd_table (pmd_base, pudp); } }
Signature · 1 line
2.54.0
PATCH v3 3/4 riscv/mm: fix missing destructor for hotplug PUD tables
In reply to Muchun SongCommit 8472cc4503eb ("riscv: mm: call PUD/P4D ctor in special kernel pgtable alloc") made alloc_pud_late() run the PUD page-table constructor. However, free_pud_table() still frees non-reserved PUD tables directly without decrementing NR_PAGETABLE.
Free PUD tables through the common free_pgtable_page() helper so that constructor-backed tables run the matching destructor before being freed.
---
v3:
- Collect Reviewed-by from Björn Töpel and Kevin Brodsky
v2:
- Collect Acked-by from David Hildenbrand
--- arch/riscv/mm/init.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)
arch/riscv/mm/init.c +1 -4
@@ -1537,10 +1537,7 @@ static void __meminit free_pud_table(pud_t *pud_start, p4d_t *p4d)return ; } -if (PageReserved (page)) -free_reserved_page (page); -else -__free_pages (page,0 ); +free_pgtable_page (page);p4d_clear (p4d); }
Signature · 1 line
2.54.0
PATCH v3 4/4 arm64/mm: fix destructor for unconstructed hotplug page tables
In reply to Muchun SongCommit c594b83457cc ("arm64: mm: call pagetable dtor when freeing hot-removed page tables") made free_hotplug_pgtable_page() unconditionally run the page-table destructor. This matches page tables allocated by the arm64 mapping code, which runs the corresponding constructors.
However, arm64 also uses the generic sparse-vmemmap population code. Runtime intermediate page tables allocated by that code do not run a page-table constructor. Freeing one during memory hot-remove therefore runs a destructor without a matching constructor and corrupts NR_PAGETABLE accounting.
Free intermediate page-table pages through pagetable_free(). It runs the destructor only for constructor-backed pages and then releases the page through the page-table freeing path.
---
v3:
- Collect Acked-by from David Hildenbrand
- Collect Reviewed-by from Kevin Brodsky
v2:
- Free intermediate tables through pagetable_free() (suggested by David Hildenbrand)
- Update the commit message for the page-table freeing path
--- arch/arm64/mm/mmu.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
arch/arm64/mm/mmu.c +1 -2
@@ -1495,8 +1495,7 @@ static void free_hotplug_page_range(struct page *page, size_t size,static void free_hotplug_pgtable_page (struct page *page) { -pagetable_dtor (page_ptdesc (page)); -free_hotplug_page_range (page,PAGE_SIZE ,NULL ); +pagetable_free (page_ptdesc (page)); } static bool pgtable_range_aligned(unsigned long start, unsigned long end,
Signature · 1 line
2.54.0
Re: [PATCH v3 4/4] arm64/mm: fix destructor for unconstructed hotplug page tables
Quoted 16 lines · Muchun Song
Commit
c594b83457cc("arm64: mm: call pagetable dtor when freeing hot-removed page tables") madefree_hotplug_pgtable_page()unconditionally run the page-table destructor. This matches page tables allocated by the arm64 mapping code, which runs the corresponding constructors.However, arm64 also uses the generic sparse-vmemmap population code. Runtime intermediate page tables allocated by that code do not run a page-table constructor. Freeing one during memory hot-remove therefore runs a destructor without a matching constructor and corrupts
NR_PAGETABLEaccounting.Free intermediate page-table pages through
pagetable_free(). It runs the destructor only for constructor-backed pages and then releases the page through the page-table freeing path.
Quoted 6 lines
Fixes c594b83457cc ("arm64: mm: call pagetable dtor when freeing hot-removed page tables")Assisted-by LLMSigned-off-by Muchun Song <[email protected]>Reviewed-by Kevin Brodsky <[email protected]>
Quoted 30 lines
---
v3:
- Collect Acked-by from David Hildenbrand
- Collect Reviewed-by from Kevin Brodskyv2:
- Free intermediate tables throughpagetable_free()(suggested by David Hildenbrand)
- Update the commit message for the page-table freeing path--- arch/arm64/mm/mmu.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)arch/arm64/mm/mmu.c +1
-2@@ -1495,8 +1495,7 @@ static void free_hotplug_page_range(struct page *page, size_t size,static void free_hotplug_pgtable_page (struct page *page) { -pagetable_dtor (page_ptdesc (page)); -free_hotplug_page_range (page,PAGE_SIZE ,NULL ); +pagetable_free (page_ptdesc (page)); } static bool pgtable_range_aligned(unsigned long start, unsigned long end,Signature · 1 line
2.54.0
Re: [PATCH v3 4/4] arm64/mm: fix destructor for unconstructed hotplug page tables
In reply to Muchun SongQuoted 16 lines · Muchun Song
Commit
c594b83457cc("arm64: mm: call pagetable dtor when freeing hot-removed page tables") madefree_hotplug_pgtable_page()unconditionally run the page-table destructor. This matches page tables allocated by the arm64 mapping code, which runs the corresponding constructors.However, arm64 also uses the generic sparse-vmemmap population code. Runtime intermediate page tables allocated by that code do not run a page-table constructor. Freeing one during memory hot-remove therefore runs a destructor without a matching constructor and corrupts
NR_PAGETABLEaccounting.Free intermediate page-table pages through
pagetable_free(). It runs the destructor only for constructor-backed pages and then releases the page through the page-table freeing path.
Quoted 6 lines
Fixes c594b83457cc ("arm64: mm: call pagetable dtor when freeing hot-removed page tables")Assisted-by LLMSigned-off-by Muchun Song <[email protected]>Reviewed-by Kevin Brodsky <[email protected]>