[PATCH 04/17] s390/vmem: Use generic page table allocators for vmem page tables
From: Heiko Carstens
Date: Thu Dec 03 2026 - 05:34:09 EST
CRST and page tables allocated with __get_free_pages() or memblock in
vmem_crst_alloc() and vmem_pte_alloc() are not accounted as page tables,
and cannot be freed by generic code.
Use generic page table allocators (pXd_alloc_one() and page_table_alloc())
as soon as slab is available. Also update vmem_crst_free() and
vmem_pte_free() to handle both early memblock and generic page table
allocations.
Signed-off-by: Heiko Carstens <hca@xxxxxxxxxxxxx>
---
arch/s390/include/asm/pgalloc.h | 1 +
arch/s390/mm/vmem.c | 52 +++++++++++++++++++++++----------
2 files changed, 37 insertions(+), 16 deletions(-)
diff --git a/arch/s390/include/asm/pgalloc.h b/arch/s390/include/asm/pgalloc.h
index d554c475c61f..f5adddc10946 100644
--- a/arch/s390/include/asm/pgalloc.h
+++ b/arch/s390/include/asm/pgalloc.h
@@ -168,6 +168,7 @@ void pte_free_defer(struct mm_struct *mm, pgtable_t pgtable);
void vmem_map_init(void);
void *vmem_crst_alloc(unsigned long val);
pte_t *vmem_pte_alloc(void);
+void vmem_pte_free(unsigned long *table);
unsigned long base_asce_alloc(unsigned long addr, unsigned long num_pages);
void base_asce_free(unsigned long asce);
diff --git a/arch/s390/mm/vmem.c b/arch/s390/mm/vmem.c
index 66071085f5a3..0436b81d9bc5 100644
--- a/arch/s390/mm/vmem.c
+++ b/arch/s390/mm/vmem.c
@@ -26,15 +26,6 @@
#include <asm/set_memory.h>
#include <asm/physmem_info.h>
-static void __ref *vmem_alloc_pages(unsigned int order)
-{
- unsigned long size = PAGE_SIZE << order;
-
- if (slab_is_available())
- return (void *)__get_free_pages(GFP_KERNEL, order);
- return memblock_alloc(size, size);
-}
-
static void vmem_free_pages(unsigned long addr, int order, struct vmem_altmap *altmap)
{
struct page *page;
@@ -52,11 +43,29 @@ static void vmem_free_pages(unsigned long addr, int order, struct vmem_altmap *a
}
}
-void *vmem_crst_alloc(unsigned long val)
+/*
+ * Allocate a crst table. Use generic page table allocators as soon
+ * as slab is available, so that the tables can be freed by generic
+ * code as well.
+ */
+void __ref *vmem_crst_alloc(unsigned long val)
{
+ unsigned long size = PAGE_SIZE << CRST_ALLOC_ORDER;
unsigned long *table;
- table = vmem_alloc_pages(CRST_ALLOC_ORDER);
+ if (slab_is_available()) {
+ switch (val) {
+ case _REGION2_ENTRY_EMPTY:
+ return p4d_alloc_one(&init_mm, 0);
+ case _REGION3_ENTRY_EMPTY:
+ return pud_alloc_one(&init_mm, 0);
+ case _SEGMENT_ENTRY_EMPTY:
+ return pmd_alloc_one(&init_mm, 0);
+ default:
+ return NULL;
+ }
+ }
+ table = memblock_alloc(size, size);
if (!table)
return NULL;
crst_table_init(table, val);
@@ -64,6 +73,14 @@ void *vmem_crst_alloc(unsigned long val)
return table;
}
+static void vmem_crst_free(unsigned long *table)
+{
+ if (PageTable(virt_to_page(table)))
+ crst_table_free(&init_mm, table);
+ else
+ vmem_free_pages((unsigned long)table, CRST_ALLOC_ORDER, NULL);
+}
+
pte_t __ref *vmem_pte_alloc(void)
{
pte_t *pte;
@@ -79,9 +96,12 @@ pte_t __ref *vmem_pte_alloc(void)
return pte;
}
-static void vmem_pte_free(unsigned long *table)
+void vmem_pte_free(unsigned long *table)
{
- page_table_free(&init_mm, table);
+ if (slab_is_available())
+ page_table_free(&init_mm, table);
+ else
+ memblock_free(table, PAGE_SIZE);
}
#define PAGE_UNUSED 0xFD
@@ -306,7 +326,7 @@ static void try_free_pmd_table(pud_t *pud, unsigned long start)
for (i = 0; i < PTRS_PER_PMD; i++, pmd++)
if (!pmd_none(pmdp_get(pmd)))
return;
- vmem_free_pages(pud_deref(pudp_get(pud)), CRST_ALLOC_ORDER, NULL);
+ vmem_crst_free((unsigned long *)pud_deref(pudp_get(pud)));
pud_clear(pud);
}
@@ -377,7 +397,7 @@ static void try_free_pud_table(p4d_t *p4d, unsigned long start)
if (!pud_none(pudp_get(pud)))
return;
}
- vmem_free_pages(p4d_deref(p4dp_get(p4d)), CRST_ALLOC_ORDER, NULL);
+ vmem_crst_free((unsigned long *)p4d_deref(p4dp_get(p4d)));
p4d_clear(p4d);
}
@@ -423,7 +443,7 @@ static void try_free_p4d_table(pgd_t *pgd, unsigned long start)
if (!p4d_none(p4dp_get(p4d)))
return;
}
- vmem_free_pages(pgd_deref(pgdp_get(pgd)), CRST_ALLOC_ORDER, NULL);
+ vmem_crst_free((unsigned long *)pgd_deref(pgdp_get(pgd)));
pgd_clear(pgd);
}
--
2.53.0