Skip to contentSkip to conversation

arm64: mm: call pagetable dtor when freeing hot-removed page tables

PATCH Applied Alistair Popple 7 participants 19 messages May 21 – Jun 2

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 arm64: mm: call pagetable dtor when freeing hot-removed page tables

Since 5e8eb9aeeda3 ("arm64: mm: always call PTE/PMD ctor in __create_pgd_mapping()") page-table allocation on ARM64 always calls pagetable_{pte,pmd,pud,p4d}_ctor(). This sets the page_type to PGTY_table, increments NR_PAGETABLE and possible allocates a PTL. However the matching pagetable_dtor() calls were never added.

With DEBUG_VM enabled on kernel versions prior to v6.17 without 2dfcd1608f3a9 ("mm/page_alloc: let page freeing clear any set page type") this leads to the following warning when freeing these pages due to page->page_type sharing page->_mapcount:

  BUG: Bad page state in process ... pfn:284fbb
  page: refcount:0 mapcount:0 mapping:0000000000000000 index:0x0 pfn:0x284fbb
  flags: 0x17fffc000000000(node=0|zone=2|lastcpupid=0x1ffff)
  page_type: f2(table)
  page dumped because: nonzero mapcount
  Call trace:
   bad_page+0x13c/0x160
   __free_frozen_pages+0x6cc/0x860
   ___free_pages+0xf4/0x180
   free_pages+0x54/0x80
   free_hotplug_page_range.part.0+0x58/0x90
   free_empty_tables+0x438/0x500
   __remove_pgd_mapping.constprop.0+0x60/0xa8
   arch_remove_memory+0x48/0x80
   try_remove_memory+0x158/0x1d8
   offline_and_remove_memory+0x138/0x180

It can also lead to leaking the ptl allocation if ALLOC_SPLIT_PTLOCKS is defined and incorrect NR_PAGETABLE stats. Fix this by calling pagetable_dtor() in free_hotplug_pgtable_page() prior to freeing the page to undo the effects of calling pagetable_*_ctor().

Fixes 5e8eb9aeeda3 ("arm64: mm: always call PTE/PMD ctor in __create_pgd_mapping()")
---
 arch/arm64/mm/mmu.c | 1 +
 1 file changed, 1 insertion(+)
arch/arm64/mm/mmu.c +1 -0
index 8e1d80a7033e..0c24fe650e95 100644
@@ -1422,6 +1422,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);
 }
 
Signature · 1 line
2.54.0
maintainer
#1 lore raw
Quoted 28 lines · On Thu, 21 May 2026 13:27:30 +1000 Alistair Popple <[email protected]> wrote:

Since 5e8eb9aeeda3 ("arm64: mm: always call PTE/PMD ctor in __create_pgd_mapping()") page-table allocation on ARM64 always calls pagetable_{pte,pmd,pud,p4d}_ctor(). This sets the page_type to PGTY_table, increments NR_PAGETABLE and possible allocates a PTL. However the matching pagetable_dtor() calls were never added.

With DEBUG_VM enabled on kernel versions prior to v6.17 without 2dfcd1608f3a9 ("mm/page_alloc: let page freeing clear any set page type") this leads to the following warning when freeing these pages due to page->page_type sharing page->_mapcount:

  BUG: Bad page state in process ... pfn:284fbb
  page: refcount:0 mapcount:0 mapping:0000000000000000 index:0x0 pfn:0x284fbb
  flags: 0x17fffc000000000(node=0|zone=2|lastcpupid=0x1ffff)
  page_type: f2(table)
  page dumped because: nonzero mapcount
  Call trace:
   bad_page+0x13c/0x160
   __free_frozen_pages+0x6cc/0x860
   ___free_pages+0xf4/0x180
   free_pages+0x54/0x80
   free_hotplug_page_range.part.0+0x58/0x90
   free_empty_tables+0x438/0x500
   __remove_pgd_mapping.constprop.0+0x60/0xa8
   arch_remove_memory+0x48/0x80
   try_remove_memory+0x158/0x1d8
   offline_and_remove_memory+0x138/0x180
Quoted 6 lines

It can also lead to leaking the ptl allocation if ALLOC_SPLIT_PTLOCKS is defined and incorrect NR_PAGETABLE stats. Fix this by calling pagetable_dtor() in free_hotplug_pgtable_page() prior to freeing the page to undo the effects of calling pagetable_*_ctor().

Fixes 5e8eb9aeeda3 ("arm64: mm: always call PTE/PMD ctor in __create_pgd_mapping()")

6.16+, so I assume we want cc:stable here.

Quoted 8 lines
 arch/arm64/mm/mmu.c | 1 +
 1 file changed, 1 insertion(+)
arch/arm64/mm/mmu.c +0 -0
index 8e1d80a7033e..0c24fe650e95 100644

arch/arm64/mm/mmu.c static void free_hotplug_page_range(struct page *page, size_t size, · line 1427

Quoted 6 lines
arch/arm64/mm/mmu.c +1 -0
@@ -1422,6 +1422,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);
 }

I'd of course prefer that arm maintainers handle this. But 5e8eb9aeeda3 came via myself so convention kinda-dictates that I get to fix it.

#2 lore raw

On 2026-05-22 at 08:31 +1000, Andrew Morton <[email protected]> wrote...

Quoted 30 lines · On Thu, 21 May 2026 13:27:30 +1000 Alistair Popple <[email protected]> wrote:
Quoted 29 lines

Since 5e8eb9aeeda3 ("arm64: mm: always call PTE/PMD ctor in __create_pgd_mapping()") page-table allocation on ARM64 always calls pagetable_{pte,pmd,pud,p4d}_ctor(). This sets the page_type to PGTY_table, increments NR_PAGETABLE and possible allocates a PTL. However the matching pagetable_dtor() calls were never added.

With DEBUG_VM enabled on kernel versions prior to v6.17 without 2dfcd1608f3a9 ("mm/page_alloc: let page freeing clear any set page type") this leads to the following warning when freeing these pages due to page->page_type sharing page->_mapcount:

  BUG: Bad page state in process ... pfn:284fbb
  page: refcount:0 mapcount:0 mapping:0000000000000000 index:0x0 pfn:0x284fbb
  flags: 0x17fffc000000000(node=0|zone=2|lastcpupid=0x1ffff)
  page_type: f2(table)
  page dumped because: nonzero mapcount
  Call trace:
   bad_page+0x13c/0x160
   __free_frozen_pages+0x6cc/0x860
   ___free_pages+0xf4/0x180
   free_pages+0x54/0x80
   free_hotplug_page_range.part.0+0x58/0x90
   free_empty_tables+0x438/0x500
   __remove_pgd_mapping.constprop.0+0x60/0xa8
   arch_remove_memory+0x48/0x80
   try_remove_memory+0x158/0x1d8
   offline_and_remove_memory+0x138/0x180
Quoted 8 lines
Quoted 6 lines

It can also lead to leaking the ptl allocation if ALLOC_SPLIT_PTLOCKS is defined and incorrect NR_PAGETABLE stats. Fix this by calling pagetable_dtor() in free_hotplug_pgtable_page() prior to freeing the page to undo the effects of calling pagetable_*_ctor().

Fixes 5e8eb9aeeda3 ("arm64: mm: always call PTE/PMD ctor in __create_pgd_mapping()")

6.16+, so I assume we want cc:stable here.

Yes indeed. Sorry I forgot to do that but I can see you added it so thanks for that.

- Alistair

Quoted 19 lines
Quoted 8 lines
 arch/arm64/mm/mmu.c | 1 +
 1 file changed, 1 insertion(+)
arch/arm64/mm/mmu.c +0 -0
index 8e1d80a7033e..0c24fe650e95 100644

arch/arm64/mm/mmu.c static void free_hotplug_page_range(struct page *page, size_t size, · line 1427

Quoted 6 lines
arch/arm64/mm/mmu.c +1 -0
@@ -1422,6 +1422,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);
 }

I'd of course prefer that arm maintainers handle this. But 5e8eb9aeeda3 came via myself so convention kinda-dictates that I get to fix it.

maintainer
#3 lore raw
In reply to Andrew Morton
Quoted 47 lines · Andrew Morton
Quoted 28 lines · On Thu, 21 May 2026 13:27:30 +1000 Alistair Popple <[email protected]> wrote:

Since 5e8eb9aeeda3 ("arm64: mm: always call PTE/PMD ctor in __create_pgd_mapping()") page-table allocation on ARM64 always calls pagetable_{pte,pmd,pud,p4d}_ctor(). This sets the page_type to PGTY_table, increments NR_PAGETABLE and possible allocates a PTL. However the matching pagetable_dtor() calls were never added.

With DEBUG_VM enabled on kernel versions prior to v6.17 without 2dfcd1608f3a9 ("mm/page_alloc: let page freeing clear any set page type") this leads to the following warning when freeing these pages due to page->page_type sharing page->_mapcount:

  BUG: Bad page state in process ... pfn:284fbb
  page: refcount:0 mapcount:0 mapping:0000000000000000 index:0x0 pfn:0x284fbb
  flags: 0x17fffc000000000(node=0|zone=2|lastcpupid=0x1ffff)
  page_type: f2(table)
  page dumped because: nonzero mapcount
  Call trace:
   bad_page+0x13c/0x160
   __free_frozen_pages+0x6cc/0x860
   ___free_pages+0xf4/0x180
   free_pages+0x54/0x80
   free_hotplug_page_range.part.0+0x58/0x90
   free_empty_tables+0x438/0x500
   __remove_pgd_mapping.constprop.0+0x60/0xa8
   arch_remove_memory+0x48/0x80
   try_remove_memory+0x158/0x1d8
   offline_and_remove_memory+0x138/0x180
Quoted 6 lines

It can also lead to leaking the ptl allocation if ALLOC_SPLIT_PTLOCKS is defined and incorrect NR_PAGETABLE stats. Fix this by calling pagetable_dtor() in free_hotplug_pgtable_page() prior to freeing the page to undo the effects of calling pagetable_*_ctor().

Fixes 5e8eb9aeeda3 ("arm64: mm: always call PTE/PMD ctor in __create_pgd_mapping()")

6.16+, so I assume we want cc:stable here.

Quoted 9 lines
 arch/arm64/mm/mmu.c | 1 +
 1 file changed, 1 insertion(+)
arch/arm64/mm/mmu.c +0 -0
index 8e1d80a7033e..0c24fe650e95 100644
@@ -1422,6 +1422,7 @@ static void free_hotplug_page_range(struct page *page, size_t size,
 
Quoted 9 lines

arch/arm64/mm/mmu.c static void free_hotplug_page_range(struct page *page, size_t size, · line 1427

Quoted 5 lines
arch/arm64/mm/mmu.c +1 -0
 static void free_hotplug_pgtable_page(struct page *page)
 {
+	pagetable_dtor(page_ptdesc(page));
 	free_hotplug_page_range(page, PAGE_SIZE, NULL);
 }

I'd of course prefer that arm maintainers handle this. But 5e8eb9aeeda3 came via myself so convention kinda-dictates that I get to fix it.

That's fine but Sashiko has some points:

https://sashiko.dev/#/patchset/[email protected]

The __remove_pgd_mapping() path is fine but we also have the vmemmap_free() path where the constructor was never called.

We could pass around a bool dtor argument but I wonder whether we could just check it's a pgtable page:

arch/arm64/mm/mmu.c +3 -0
index 4c8959153ac4..9d42cbddce27 100644
@@ -1441,6 +1441,9 @@ static void free_hotplug_page_range(struct page *page, size_t size,
 
 static void free_hotplug_pgtable_page(struct page *page)
 {
+	if (folio_test_pgtable(page_folio(page)))
+		pagetable_dtor(page_ptdesc(page));
+
 	free_hotplug_page_range(page, PAGE_SIZE, NULL);
 }
 
Signature · 1 line
Catalin
maintainer
#4 lore raw
Quoted 18 lines · Catalin Marinas
Quoted 7 lines · Andrew Morton
Quoted 6 lines · On Thu, 21 May 2026 13:27:30 +1000 Alistair Popple <[email protected]> wrote:
arch/arm64/mm/mmu.c +0 -0
index 8e1d80a7033e..0c24fe650e95 100644
@@ -1422,6 +1422,7 @@ static void free_hotplug_page_range(struct page *page, size_t size,
 
Quoted 9 lines

arch/arm64/mm/mmu.c static void free_hotplug_page_range(struct page *page, size_t size, · line 1448

Quoted 5 lines
arch/arm64/mm/mmu.c +1 -0
 static void free_hotplug_pgtable_page(struct page *page)
 {
+	pagetable_dtor(page_ptdesc(page));
 	free_hotplug_page_range(page, PAGE_SIZE, NULL);
 }

I'd of course prefer that arm maintainers handle this. But 5e8eb9aeeda3 came via myself so convention kinda-dictates that I get to fix it.

Quoted 3 lines

That's fine but Sashiko has some points:

https://sashiko.dev/#/patchset/[email protected]

The other Sashiko find looks like a false positive. vmemmap_*_populate() do not allocate the page table from altmap, only the page pointed at by the vmemmap pte.

Signature · 1 line
Catalin
#5 lore raw
In reply to Catalin Marinas
Quoted 60 lines · Catalin Marinas
Quoted 47 lines · Andrew Morton
Quoted 28 lines · On Thu, 21 May 2026 13:27:30 +1000 Alistair Popple <[email protected]> wrote:

Since 5e8eb9aeeda3 ("arm64: mm: always call PTE/PMD ctor in __create_pgd_mapping()") page-table allocation on ARM64 always calls pagetable_{pte,pmd,pud,p4d}_ctor(). This sets the page_type to PGTY_table, increments NR_PAGETABLE and possible allocates a PTL. However the matching pagetable_dtor() calls were never added.

With DEBUG_VM enabled on kernel versions prior to v6.17 without 2dfcd1608f3a9 ("mm/page_alloc: let page freeing clear any set page type") this leads to the following warning when freeing these pages due to page->page_type sharing page->_mapcount:

  BUG: Bad page state in process ... pfn:284fbb
  page: refcount:0 mapcount:0 mapping:0000000000000000 index:0x0 pfn:0x284fbb
  flags: 0x17fffc000000000(node=0|zone=2|lastcpupid=0x1ffff)
  page_type: f2(table)
  page dumped because: nonzero mapcount
  Call trace:
   bad_page+0x13c/0x160
   __free_frozen_pages+0x6cc/0x860
   ___free_pages+0xf4/0x180
   free_pages+0x54/0x80
   free_hotplug_page_range.part.0+0x58/0x90
   free_empty_tables+0x438/0x500
   __remove_pgd_mapping.constprop.0+0x60/0xa8
   arch_remove_memory+0x48/0x80
   try_remove_memory+0x158/0x1d8
   offline_and_remove_memory+0x138/0x180
Quoted 6 lines

It can also lead to leaking the ptl allocation if ALLOC_SPLIT_PTLOCKS is defined and incorrect NR_PAGETABLE stats. Fix this by calling pagetable_dtor() in free_hotplug_pgtable_page() prior to freeing the page to undo the effects of calling pagetable_*_ctor().

Fixes 5e8eb9aeeda3 ("arm64: mm: always call PTE/PMD ctor in __create_pgd_mapping()")

6.16+, so I assume we want cc:stable here.

Quoted 9 lines
 arch/arm64/mm/mmu.c | 1 +
 1 file changed, 1 insertion(+)
arch/arm64/mm/mmu.c +0 -0
index 8e1d80a7033e..0c24fe650e95 100644
@@ -1422,6 +1422,7 @@ static void free_hotplug_page_range(struct page *page, size_t size,
 
Quoted 9 lines

arch/arm64/mm/mmu.c static void free_hotplug_page_range(struct page *page, size_t size, · line 1448

Quoted 5 lines
arch/arm64/mm/mmu.c +1 -0
 static void free_hotplug_pgtable_page(struct page *page)
 {
+	pagetable_dtor(page_ptdesc(page));
 	free_hotplug_page_range(page, PAGE_SIZE, NULL);
 }

I'd of course prefer that arm maintainers handle this. But 5e8eb9aeeda3 came via myself so convention kinda-dictates that I get to fix it.

That's fine but Sashiko has some points:

Quoted 7 lines

https://sashiko.dev/#/patchset/[email protected]

The __remove_pgd_mapping() path is fine but we also have the vmemmap_free() path where the constructor was never called.

We could pass around a bool dtor argument but I wonder whether we could just check it's a pgtable page:

Free_empty_tables() looks like the only way we'd ever get to free_hotplug_pgtable_page(). I'm a little curious why we can't consolidate unmap_hotplug_range() and free_empty_tables(). I.e. just fold unmap_hotplug_range() into the latter.

arch/arm64/mm/mmu.c static void free_hotplug_page_range(struct page *page, size_t size, · line 1444

Quoted 9 lines
arch/arm64/mm/mmu.c +1 -0
index 4c8959153ac4..9d42cbddce27 100644
@@ -1441,6 +1441,9 @@ static void free_hotplug_page_range(struct page *page, size_t size,
 
 static void free_hotplug_pgtable_page(struct page *page)
 {
+	if (folio_test_pgtable(page_folio(page)))

This should work.

arch/arm64/mm/mmu.c static void free_hotplug_page_range(struct page *page, size_t size, · line 1447

Quoted 3 lines
arch/arm64/mm/mmu.c +2 -0
+		pagetable_dtor(page_ptdesc(page));
+
 	free_hotplug_page_range(page, PAGE_SIZE, NULL);

In the case we presumably have a page table page (ptdesc) at this point, we should really be freeing it with pagetable_free() as well.

Its not a big deal that we don't right now, but losing track of the matching allocation/free sites will become a headache when separately allocating from struct page.

Quoted 5 lines
arch/arm64/mm/mmu.c +0 -0
 }
 
Signature · 2 lines
-- 
Catalin
#6 lore raw
Quoted 11 lines · Vishal Moola

arch/arm64/mm/mmu.c static void free_hotplug_page_range(struct page *page, size_t size, · line 1444

Quoted 9 lines
arch/arm64/mm/mmu.c +1 -0
index 4c8959153ac4..9d42cbddce27 100644
@@ -1441,6 +1441,9 @@ static void free_hotplug_page_range(struct page *page, size_t size,
 
 static void free_hotplug_pgtable_page(struct page *page)
 {
+	if (folio_test_pgtable(page_folio(page)))

This should work.

Quoted 5 lines

arch/arm64/mm/mmu.c static void free_hotplug_page_range(struct page *page, size_t size, · line 1447

Quoted 3 lines
arch/arm64/mm/mmu.c +2 -0
+		pagetable_dtor(page_ptdesc(page));
+
 	free_hotplug_page_range(page, PAGE_SIZE, NULL);

In the case we presumably have a page table page (ptdesc) at this point, we should really be freeing it with pagetable_free() as well.

Agreed, I think this is the right thing to do, something like:

if (folio_test_pgtable(page_folio(page)))
pagetable_dtor_free(page_ptdesc(page)); else
free_hotplug_page_range(page, PAGE_SIZE, NULL);

Strangely enough x86 calls pagetable_free() in both cases.

My series protecting page tables with pkeys has a patch [1] to get vmemmap to allocate page tables with pagetable_alloc(). The diff above will require pagetable_*_ctor() to be called as well, but I think that's the right thing to do anyway. That could be posted as a separate series, but I'm hesitant due to the lack of NUMA awareness in pagetable_alloc().

- Kevin

[1] https://lore.kernel.org/all/[email protected]/

Quoted 5 lines

Its not a big deal that we don't right now, but losing track of the matching allocation/free sites will become a headache when separately allocating from struct page.

Quoted 1 line
arch/arm64/mm/mmu.c +0 -0
 }
#7 lore raw
Quoted 18 lines · Kevin Brodsky
Quoted 11 lines · Vishal Moola

arch/arm64/mm/mmu.c static void free_hotplug_page_range(struct page *page, size_t size, · line 1444

Quoted 9 lines
arch/arm64/mm/mmu.c +1 -0
index 4c8959153ac4..9d42cbddce27 100644
@@ -1441,6 +1441,9 @@ static void free_hotplug_page_range(struct page *page, size_t size,
 
 static void free_hotplug_pgtable_page(struct page *page)
 {
+	if (folio_test_pgtable(page_folio(page)))

This should work.

Quoted 5 lines

arch/arm64/mm/mmu.c static void free_hotplug_page_range(struct page *page, size_t size, · line 1447

Quoted 3 lines
arch/arm64/mm/mmu.c +2 -0
+		pagetable_dtor(page_ptdesc(page));
+
 	free_hotplug_page_range(page, PAGE_SIZE, NULL);

In the case we presumably have a page table page (ptdesc) at this point, we should really be freeing it with pagetable_free() as well.

Quoted 5 lines

Agreed, I think this is the right thing to do, something like:

if (folio_test_pgtable(page_folio(page)))
pagetable_dtor_free(page_ptdesc(page)); else
free_hotplug_page_range(page, PAGE_SIZE, NULL);

That code pattern is wrong.

folio_test_pgtable() shouldn't exist.

In the future, something is either a pgtable or a folio, not both.

So check the type against the page, not the folio.

Signature · 3 lines
Cheers,

David
#8 lore raw
Quoted 23 lines · David Hildenbrand (Arm)
Quoted 17 lines · Kevin Brodsky
Quoted 11 lines · Vishal Moola

arch/arm64/mm/mmu.c static void free_hotplug_page_range(struct page *page, size_t size, · line 1444

Quoted 9 lines
arch/arm64/mm/mmu.c +1 -0
index 4c8959153ac4..9d42cbddce27 100644
@@ -1441,6 +1441,9 @@ static void free_hotplug_page_range(struct page *page, size_t size,
 
 static void free_hotplug_pgtable_page(struct page *page)
 {
+	if (folio_test_pgtable(page_folio(page)))

This should work.

Quoted 5 lines

arch/arm64/mm/mmu.c static void free_hotplug_page_range(struct page *page, size_t size, · line 1447

Quoted 3 lines
arch/arm64/mm/mmu.c +2 -0
+		pagetable_dtor(page_ptdesc(page));
+
 	free_hotplug_page_range(page, PAGE_SIZE, NULL);

In the case we presumably have a page table page (ptdesc) at this point, we should really be freeing it with pagetable_free() as well.

Quoted 5 lines

Agreed, I think this is the right thing to do, something like:

if (folio_test_pgtable(page_folio(page)))
pagetable_dtor_free(page_ptdesc(page)); else
free_hotplug_page_range(page, PAGE_SIZE, NULL);

Quoted 7 lines

That code pattern is wrong.

folio_test_pgtable() shouldn't exist.

In the future, something is either a pgtable or a folio, not both.

So check the type against the page, not the folio.

In other words use PageTable(page) instead? Interestingly I can see a few calls to folio_test_pgtable() across the kernel but none to PageTable(), maybe just an antipattern then? The ctor/dtor also use __folio_{set,clear}_pgtable().

- Kevin

#9 lore raw
Quoted 28 lines · Kevin Brodsky
Quoted 27 lines · David Hildenbrand (Arm)
Quoted 17 lines · Kevin Brodsky
Quoted 11 lines · Vishal Moola

arch/arm64/mm/mmu.c static void free_hotplug_page_range(struct page *page, size_t size, · line 1444

Quoted 9 lines
arch/arm64/mm/mmu.c +1 -0
index 4c8959153ac4..9d42cbddce27 100644
@@ -1441,6 +1441,9 @@ static void free_hotplug_page_range(struct page *page, size_t size,
 
 static void free_hotplug_pgtable_page(struct page *page)
 {
+	if (folio_test_pgtable(page_folio(page)))

This should work.

Quoted 5 lines

arch/arm64/mm/mmu.c static void free_hotplug_page_range(struct page *page, size_t size, · line 1447

Quoted 3 lines
arch/arm64/mm/mmu.c +2 -0
+		pagetable_dtor(page_ptdesc(page));
+
 	free_hotplug_page_range(page, PAGE_SIZE, NULL);

In the case we presumably have a page table page (ptdesc) at this point, we should really be freeing it with pagetable_free() as well.

Quoted 5 lines

Agreed, I think this is the right thing to do, something like:

if (folio_test_pgtable(page_folio(page)))
pagetable_dtor_free(page_ptdesc(page)); else
free_hotplug_page_range(page, PAGE_SIZE, NULL);

That code pattern is wrong.

folio_test_pgtable() shouldn't exist.

Quoted 8 lines
Quoted 3 lines

In the future, something is either a pgtable or a folio, not both.

So check the type against the page, not the folio.

In other words use PageTable(page) instead? Interestingly I can see a few calls to folio_test_pgtable() across the kernel but none to PageTable(), maybe just an antipattern then? The ctor/dtor also use __folio_{set,clear}_pgtable().

If we know for sure we have the head page (which we probably do here), we should use PageTable(page).

I included the folio APIs as a defensive way to ensure large order page table users don't break. We only set the type on the head page, and using a folio guarantees we're accessing that head page. This can go away as soon as someone looks at the architectures that allocate large order ptdescs.

Also, most (if not all) 'pgtables' don't use ptdescs yet, but they probably should... Anyway, in this particular case, it looks like pgtable is just a name symbolizing 'page table' and not our type 'pgtable_t' anyway so I'm just rambling.

#10 lore raw
In reply to Kevin Brodsky
Quoted 11 lines · Kevin Brodsky
Quoted 10 lines · David Hildenbrand (Arm)
Quoted 5 lines · Kevin Brodsky

Agreed, I think this is the right thing to do, something like:

if (folio_test_pgtable(page_folio(page)))
pagetable_dtor_free(page_ptdesc(page)); else
free_hotplug_page_range(page, PAGE_SIZE, NULL);

That code pattern is wrong.

folio_test_pgtable() shouldn't exist.

Quoted 8 lines
Quoted 3 lines

In the future, something is either a pgtable or a folio, not both.

So check the type against the page, not the folio.

In other words use PageTable(page) instead? Interestingly I can see a few calls to folio_test_pgtable() across the kernel but none to PageTable(), maybe just an antipattern then? The ctor/dtor also use __folio_{set,clear}_pgtable().

We started running into the wrong direction, and now have to undo all the wrong things we already did :)

In the current design Willy has in mind, doing a page_folio() will later return NULL if it's not actually a folio.

Signature · 3 lines
Cheers,

David
maintainer
#11 lore raw
In reply to Kevin Brodsky
Quoted 25 lines · Kevin Brodsky
Quoted 11 lines · Vishal Moola

arch/arm64/mm/mmu.c static void free_hotplug_page_range(struct page *page, size_t size, · line 1444

Quoted 9 lines
arch/arm64/mm/mmu.c +1 -0
index 4c8959153ac4..9d42cbddce27 100644
@@ -1441,6 +1441,9 @@ static void free_hotplug_page_range(struct page *page, size_t size,
 
 static void free_hotplug_pgtable_page(struct page *page)
 {
+	if (folio_test_pgtable(page_folio(page)))

This should work.

Quoted 5 lines

arch/arm64/mm/mmu.c static void free_hotplug_page_range(struct page *page, size_t size, · line 1447

Quoted 3 lines
arch/arm64/mm/mmu.c +2 -0
+		pagetable_dtor(page_ptdesc(page));
+
 	free_hotplug_page_range(page, PAGE_SIZE, NULL);

In the case we presumably have a page table page (ptdesc) at this point, we should really be freeing it with pagetable_free() as well.

Agreed, I think this is the right thing to do, something like:

if (folio_test_pgtable(page_folio(page)))
pagetable_dtor_free(page_ptdesc(page)); else
free_hotplug_page_range(page, PAGE_SIZE, NULL);

Quoted 7 lines

Strangely enough x86 calls pagetable_free() in both cases.

My series protecting page tables with pkeys has a patch [1] to get vmemmap to allocate page tables with pagetable_alloc(). The diff above will require pagetable_*_ctor() to be called as well, but I think that's the right thing to do anyway. That could be posted as a separate series, but I'm hesitant due to the lack of NUMA awareness in pagetable_alloc().

I agree that calling the ctor()/dtor() functions consistently is the cleanest approach and that will need something like your patch to call the constructor from vmemmap_alloc_block_zero(). Trying to elide these calls for the page-table pages used to map the altmap just feels odd to me, as there isn't anything particularly special about them afaik.

Will

#12 lore raw
Quoted 32 lines · Will Deacon
Quoted 24 lines · Kevin Brodsky
Quoted 11 lines · Vishal Moola

arch/arm64/mm/mmu.c static void free_hotplug_page_range(struct page *page, size_t size, · line 1444

Quoted 9 lines
arch/arm64/mm/mmu.c +1 -0
index 4c8959153ac4..9d42cbddce27 100644
@@ -1441,6 +1441,9 @@ static void free_hotplug_page_range(struct page *page, size_t size,
 
 static void free_hotplug_pgtable_page(struct page *page)
 {
+	if (folio_test_pgtable(page_folio(page)))

This should work.

Quoted 5 lines

arch/arm64/mm/mmu.c static void free_hotplug_page_range(struct page *page, size_t size, · line 1447

Quoted 3 lines
arch/arm64/mm/mmu.c +2 -0
+		pagetable_dtor(page_ptdesc(page));
+
 	free_hotplug_page_range(page, PAGE_SIZE, NULL);

In the case we presumably have a page table page (ptdesc) at this point, we should really be freeing it with pagetable_free() as well.

Agreed, I think this is the right thing to do, something like:

if (folio_test_pgtable(page_folio(page)))
pagetable_dtor_free(page_ptdesc(page)); else
free_hotplug_page_range(page, PAGE_SIZE, NULL);

Quoted 7 lines

Strangely enough x86 calls pagetable_free() in both cases.

My series protecting page tables with pkeys has a patch [1] to get vmemmap to allocate page tables with pagetable_alloc(). The diff above will require pagetable_*_ctor() to be called as well, but I think that's the right thing to do anyway. That could be posted as a separate series, but I'm hesitant due to the lack of NUMA awareness in pagetable_alloc().

Quoted 5 lines

I agree that calling the ctor()/dtor() functions consistently is the cleanest approach and that will need something like your patch to call the constructor from vmemmap_alloc_block_zero(). Trying to elide these calls for the page-table pages used to map the altmap just feels odd to me, as there isn't anything particularly special about them afaik.

I don't think they're really special either, most likely they just got missed/ignored for the purpose of ctor/dtor like many other kernel page tables (until recently).

I'll prepare a series refactoring that code then - that will also require changing most arch implementations of vmemmap_free() to call pagetable_dtor_free().

In the meantime we should probably use the logic above to avoid the BUG that Alistair reported.

- Kevin

#13 lore raw
Quoted 39 lines · Kevin Brodsky
Quoted 32 lines · Will Deacon
Quoted 24 lines · Kevin Brodsky
Quoted 11 lines · Vishal Moola

arch/arm64/mm/mmu.c static void free_hotplug_page_range(struct page *page, size_t size, · line 1444

Quoted 9 lines
arch/arm64/mm/mmu.c +1 -0
index 4c8959153ac4..9d42cbddce27 100644
@@ -1441,6 +1441,9 @@ static void free_hotplug_page_range(struct page *page, size_t size,
 
 static void free_hotplug_pgtable_page(struct page *page)
 {
+	if (folio_test_pgtable(page_folio(page)))

This should work.

Quoted 5 lines

arch/arm64/mm/mmu.c static void free_hotplug_page_range(struct page *page, size_t size, · line 1447

Quoted 3 lines
arch/arm64/mm/mmu.c +2 -0
+		pagetable_dtor(page_ptdesc(page));
+
 	free_hotplug_page_range(page, PAGE_SIZE, NULL);

In the case we presumably have a page table page (ptdesc) at this point, we should really be freeing it with pagetable_free() as well.

Agreed, I think this is the right thing to do, something like:

if (folio_test_pgtable(page_folio(page)))
pagetable_dtor_free(page_ptdesc(page)); else
free_hotplug_page_range(page, PAGE_SIZE, NULL);

Quoted 7 lines

Strangely enough x86 calls pagetable_free() in both cases.

My series protecting page tables with pkeys has a patch [1] to get vmemmap to allocate page tables with pagetable_alloc(). The diff above will require pagetable_*_ctor() to be called as well, but I think that's the right thing to do anyway. That could be posted as a separate series, but I'm hesitant due to the lack of NUMA awareness in pagetable_alloc().

Quoted 5 lines

I agree that calling the ctor()/dtor() functions consistently is the cleanest approach and that will need something like your patch to call the constructor from vmemmap_alloc_block_zero(). Trying to elide these calls for the page-table pages used to map the altmap just feels odd to me, as there isn't anything particularly special about them afaik.

Quoted 7 lines

I don't think they're really special either, most likely they just got missed/ignored for the purpose of ctor/dtor like many other kernel page tables (until recently).

I'll prepare a series refactoring that code then - that will also require changing most arch implementations of vmemmap_free() to call pagetable_dtor_free().

Take a look at Matthew's series[1]. I think thats the ideal approach for page table accounting. He hasn't had time to iterate on it though. I doubt he'd mind if someone picked it up.

Quoted 2 lines

In the meantime we should probably use the logic above to avoid the BUG that Alistair reported.

Agreed.

[1] https://lore.kernel.org/linux-mm/[email protected]/

#14 lore raw
Quoted 43 lines · Vishal Moola
Quoted 42 lines · Kevin Brodsky
Quoted 32 lines · Will Deacon
Quoted 24 lines · Kevin Brodsky
Quoted 11 lines · Vishal Moola

arch/arm64/mm/mmu.c static void free_hotplug_page_range(struct page *page, size_t size, · line 1444

Quoted 9 lines
arch/arm64/mm/mmu.c +1 -0
index 4c8959153ac4..9d42cbddce27 100644
@@ -1441,6 +1441,9 @@ static void free_hotplug_page_range(struct page *page, size_t size,
 
 static void free_hotplug_pgtable_page(struct page *page)
 {
+	if (folio_test_pgtable(page_folio(page)))

This should work.

Quoted 5 lines

arch/arm64/mm/mmu.c static void free_hotplug_page_range(struct page *page, size_t size, · line 1447

Quoted 3 lines
arch/arm64/mm/mmu.c +2 -0
+		pagetable_dtor(page_ptdesc(page));
+
 	free_hotplug_page_range(page, PAGE_SIZE, NULL);

In the case we presumably have a page table page (ptdesc) at this point, we should really be freeing it with pagetable_free() as well.

Agreed, I think this is the right thing to do, something like:

if (folio_test_pgtable(page_folio(page)))
pagetable_dtor_free(page_ptdesc(page)); else
free_hotplug_page_range(page, PAGE_SIZE, NULL);

Quoted 7 lines

Strangely enough x86 calls pagetable_free() in both cases.

My series protecting page tables with pkeys has a patch [1] to get vmemmap to allocate page tables with pagetable_alloc(). The diff above will require pagetable_*_ctor() to be called as well, but I think that's the right thing to do anyway. That could be posted as a separate series, but I'm hesitant due to the lack of NUMA awareness in pagetable_alloc().

Quoted 5 lines

I agree that calling the ctor()/dtor() functions consistently is the cleanest approach and that will need something like your patch to call the constructor from vmemmap_alloc_block_zero(). Trying to elide these calls for the page-table pages used to map the altmap just feels odd to me, as there isn't anything particularly special about them afaik.

I don't think they're really special either, most likely they just got missed/ignored for the purpose of ctor/dtor like many other kernel page tables (until recently).

Quoted 6 lines
Quoted 3 lines

I'll prepare a series refactoring that code then - that will also require changing most arch implementations of vmemmap_free() to call pagetable_dtor_free().

Take a look at Matthew's series[1]. I think thats the ideal approach for page table accounting. He hasn't had time to iterate on it though. I doubt he'd mind if someone picked it up.

I recall this series. Are you suggesting that we would no longer need to call the ctor/dtor for kernel page tables with this approach? That leaves us with the weird case of ptdesc_set_kernel(), which is called from *_alloc_one() while ptdesc_clear_kernel() is called from pagetable_free(), but that's only an optimisation so we can probably live with it.

If we go down this route, I would suggest we inline what's left of the ctor/dtor, i.e. ptlock, in {pte,pmd}_alloc_one() and {pte,pmd}_free(). This way it is clear that everything applicable to all page tables (kernel+user) should go directly into pagetable_{alloc,free}.

Happy to post something along those lines (patch 2/3 of Matthew's series
+ removing ctor/dtor completely) if that sounds sensible.

- Kevin

#15 lore raw
Quoted 51 lines · Kevin Brodsky
Quoted 43 lines · Vishal Moola
Quoted 42 lines · Kevin Brodsky
Quoted 32 lines · Will Deacon
Quoted 24 lines · Kevin Brodsky
Quoted 11 lines · Vishal Moola

arch/arm64/mm/mmu.c static void free_hotplug_page_range(struct page *page, size_t size, · line 1444

Quoted 9 lines
arch/arm64/mm/mmu.c +1 -0
index 4c8959153ac4..9d42cbddce27 100644
@@ -1441,6 +1441,9 @@ static void free_hotplug_page_range(struct page *page, size_t size,
 
 static void free_hotplug_pgtable_page(struct page *page)
 {
+	if (folio_test_pgtable(page_folio(page)))

This should work.

Quoted 5 lines

arch/arm64/mm/mmu.c static void free_hotplug_page_range(struct page *page, size_t size, · line 1447

Quoted 3 lines
arch/arm64/mm/mmu.c +2 -0
+		pagetable_dtor(page_ptdesc(page));
+
 	free_hotplug_page_range(page, PAGE_SIZE, NULL);

In the case we presumably have a page table page (ptdesc) at this point, we should really be freeing it with pagetable_free() as well.

Agreed, I think this is the right thing to do, something like:

if (folio_test_pgtable(page_folio(page)))
pagetable_dtor_free(page_ptdesc(page)); else
free_hotplug_page_range(page, PAGE_SIZE, NULL);

Quoted 7 lines

Strangely enough x86 calls pagetable_free() in both cases.

My series protecting page tables with pkeys has a patch [1] to get vmemmap to allocate page tables with pagetable_alloc(). The diff above will require pagetable_*_ctor() to be called as well, but I think that's the right thing to do anyway. That could be posted as a separate series, but I'm hesitant due to the lack of NUMA awareness in pagetable_alloc().

Quoted 5 lines

I agree that calling the ctor()/dtor() functions consistently is the cleanest approach and that will need something like your patch to call the constructor from vmemmap_alloc_block_zero(). Trying to elide these calls for the page-table pages used to map the altmap just feels odd to me, as there isn't anything particularly special about them afaik.

I don't think they're really special either, most likely they just got missed/ignored for the purpose of ctor/dtor like many other kernel page tables (until recently).

Quoted 6 lines
Quoted 3 lines

I'll prepare a series refactoring that code then - that will also require changing most arch implementations of vmemmap_free() to call pagetable_dtor_free().

Take a look at Matthew's series[1]. I think thats the ideal approach for page table accounting. He hasn't had time to iterate on it though. I doubt he'd mind if someone picked it up.

Quoted 6 lines

I recall this series. Are you suggesting that we would no longer need to call the ctor/dtor for kernel page tables with this approach? That leaves us with the weird case of ptdesc_set_kernel(), which is called from *_alloc_one() while ptdesc_clear_kernel() is called from pagetable_free(), but that's only an optimisation so we can probably live with it.

Pretty much. The ctor/dtor do things that every single page table should be doing, so it makes sense to move them to the allocation/free sites instead.

Kernel pagetables are a subset of ptdescs, so keeping those where they are makes sense IMO.

Quoted 7 lines

If we go down this route, I would suggest we inline what's left of the ctor/dtor, i.e. ptlock, in {pte,pmd}_alloc_one() and {pte,pmd}_free(). This way it is clear that everything applicable to all page tables (kernel+user) should go directly into pagetable_{alloc,free}.

Happy to post something along those lines (patch 2/3 of Matthew's series
+ removing ctor/dtor completely) if that sounds sensible.

Sounds good to me :)

maintainer
#16 lore raw
In reply to Catalin Marinas
Quoted 29 lines · On Fri, 22 May 2026 08:15:09 +0100 Catalin Marinas <[email protected]>…
Quoted 4 lines
Quoted 4 lines
arch/arm64/mm/mmu.c +0 -0
@@ -1422,6 +1422,7 @@ static void free_hotplug_page_range(struct page *page, size_t size,
 
Quoted 9 lines

arch/arm64/mm/mmu.c static void free_hotplug_page_range(struct page *page, size_t size, · line 1427

Quoted 5 lines
arch/arm64/mm/mmu.c +1 -0
 static void free_hotplug_pgtable_page(struct page *page)
 {
+	pagetable_dtor(page_ptdesc(page));
 	free_hotplug_page_range(page, PAGE_SIZE, NULL);
 }

I'd of course prefer that arm maintainers handle this. But 5e8eb9aeeda3 came via myself so convention kinda-dictates that I get to fix it.

That's fine but Sashiko has some points:

https://sashiko.dev/#/patchset/[email protected]

The __remove_pgd_mapping() path is fine but we also have the vmemmap_free() path where the constructor was never called.

We could pass around a bool dtor argument but I wonder whether we could just check it's a pgtable page:

arch/arm64/mm/mmu.c +0 -0
index 4c8959153ac4..9d42cbddce27 100644

arch/arm64/mm/mmu.c static void free_hotplug_page_range(struct page *page, size_t size, · line 1448

Quoted 8 lines
arch/arm64/mm/mmu.c +3 -0
@@ -1441,6 +1441,9 @@ static void free_hotplug_page_range(struct page *page, size_t size,
 
 static void free_hotplug_pgtable_page(struct page *page)
 {
+	if (folio_test_pgtable(page_folio(page)))
+		pagetable_dtor(page_ptdesc(page));
+
 	free_hotplug_page_range(page, PAGE_SIZE, NULL);
 }

This patch is still floating around in mm.git awaiting some finalization. What to do?

From: Alistair Popple <[email protected]>
Subject: arm64: mm: call pagetable dtor when freeing hot-removed page tables
Date: Thu, 21 May 2026 13:27:30 +1000

Since 5e8eb9aeeda3 ("arm64: mm: always call PTE/PMD ctor in __create_pgd_mapping()") page-table allocation on ARM64 always calls pagetable_{pte,pmd,pud,p4d}_ctor(). This sets the page_type to PGTY_table, increments NR_PAGETABLE and possible allocates a PTL. However the matching pagetable_dtor() calls were never added.

With DEBUG_VM enabled on kernel versions prior to v6.17 without 2dfcd1608f3a9 ("mm/page_alloc: let page freeing clear any set page type") this leads to the following warning when freeing these pages due to page->page_type sharing page->_mapcount:

  BUG: Bad page state in process ... pfn:284fbb
  page: refcount:0 mapcount:0 mapping:0000000000000000 index:0x0 pfn:0x284fbb
  flags: 0x17fffc000000000(node=0|zone=2|lastcpupid=0x1ffff)
  page_type: f2(table)
  page dumped because: nonzero mapcount
  Call trace:
   bad_page+0x13c/0x160
   __free_frozen_pages+0x6cc/0x860
   ___free_pages+0xf4/0x180
   free_pages+0x54/0x80
   free_hotplug_page_range.part.0+0x58/0x90
   free_empty_tables+0x438/0x500
   __remove_pgd_mapping.constprop.0+0x60/0xa8
   arch_remove_memory+0x48/0x80
   try_remove_memory+0x158/0x1d8
   offline_and_remove_memory+0x138/0x180

It can also lead to leaking the ptl allocation if ALLOC_SPLIT_PTLOCKS is defined and incorrect NR_PAGETABLE stats. Fix this by calling pagetable_dtor() in free_hotplug_pgtable_page() prior to freeing the page to undo the effects of calling pagetable_*_ctor().

Fixes 5e8eb9aeeda3 ("arm64: mm: always call PTE/PMD ctor in __create_pgd_mapping()")
Cc Catalin Marinas <[email protected]>
Cc David Hildenbrand <[email protected]>
Cc Will Deacon <[email protected]>

---

 arch/arm64/mm/mmu.c |    1 +
 1 file changed, 1 insertion(+)
arch/arm64/mm/mmu.c renamed from arch/arm64/mm/mmu.c~arm64-mm-call-pagetable-dtor-when-freeing-hot-removed-page-tables +1 -0
@@ -1441,6 +1441,7 @@ static void free_hotplug_page_range(stru
 
 static void free_hotplug_pgtable_page(struct page *page)
 {
+	pagetable_dtor(page_ptdesc(page));
 	free_hotplug_page_range(page, PAGE_SIZE, NULL);
 }
 

_

maintainer
#17 lore raw
Quoted 32 lines · Andrew Morton
Quoted 30 lines · On Fri, 22 May 2026 08:15:09 +0100 Catalin Marinas <[email protected]>…
Quoted 4 lines
Quoted 4 lines
arch/arm64/mm/mmu.c +0 -0
@@ -1422,6 +1422,7 @@ static void free_hotplug_page_range(struct page *page, size_t size,
 
Quoted 9 lines

arch/arm64/mm/mmu.c static void free_hotplug_page_range(stru · line 1446

Quoted 5 lines
arch/arm64/mm/mmu.c +1 -0
 static void free_hotplug_pgtable_page(struct page *page)
 {
+	pagetable_dtor(page_ptdesc(page));
 	free_hotplug_page_range(page, PAGE_SIZE, NULL);
 }

I'd of course prefer that arm maintainers handle this. But 5e8eb9aeeda3 came via myself so convention kinda-dictates that I get to fix it.

That's fine but Sashiko has some points:

https://sashiko.dev/#/patchset/[email protected]

The __remove_pgd_mapping() path is fine but we also have the vmemmap_free() path where the constructor was never called.

We could pass around a bool dtor argument but I wonder whether we could just check it's a pgtable page:

arch/arm64/mm/mmu.c +0 -0
index 4c8959153ac4..9d42cbddce27 100644
@@ -1441,6 +1441,9 @@ static void free_hotplug_page_range(struct page *page, size_t size,
 
Quoted 10 lines

arch/arm64/mm/mmu.c static void free_hotplug_page_range(stru · line 1446

Quoted 7 lines
arch/arm64/mm/mmu.c +3 -0
 static void free_hotplug_pgtable_page(struct page *page)
 {
+	if (folio_test_pgtable(page_folio(page)))
+		pagetable_dtor(page_ptdesc(page));
+
 	free_hotplug_page_range(page, PAGE_SIZE, NULL);
 }

This patch is still floating around in mm.git awaiting some finalization. What to do?

From what I can tell (assuming I've been following along correctly!):

1. This patch improves the situation, so makes sense in its own right
2. There is additional work to be more consistent with the constructors for page-table pages, which Kevin is going to look at but is probably a little more long-term.
3. I'm happy to take this via arm64 but also have no issues with you taking it via mm (so please shout if you want me to pick it up).

Will

maintainer
#18 lore raw
Quoted 11 lines · On Tue, 2 Jun 2026 14:12:17 +0100 Will Deacon <[email protected]> wrote:
Quoted 4 lines

This patch is still floating around in mm.git awaiting some finalization. What to do?

From what I can tell (assuming I've been following along correctly!):

1. This patch improves the situation, so makes sense in its own right
2. There is additional work to be more consistent with the constructors for page-table pages, which Kevin is going to look at but is probably a little more long-term.
3. I'm happy to take this via arm64 but also have no issues with you taking it via mm (so please shout if you want me to pick it up).

OK, thanks, no probs, just checking. I'll include this in the next hotfixes batch for Linus.