mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Heiko Carstens <hca@linux.ibm.com>
To: Alexander Gordeev <agordeev@linux.ibm.com>,
	Sven Schnelle <svens@linux.ibm.com>,
	Vasily Gorbik <gor@linux.ibm.com>,
	Christian Borntraeger <borntraeger@linux.ibm.com>,
	Mike Rapoport <rppt@kernel.org>,
	Gerald Schaefer <gerald.schaefer@linux.ibm.com>
Cc: linux-kernel@vger.kernel.org, linux-s390@vger.kernel.org
Subject: [PATCH 04/17] s390/vmem: Use generic page table allocators for vmem page tables
Date: Thu,  3 Dec 2026 11:33:32 +0100	[thread overview]
Message-ID: <20261203103345.1206548-5-hca@linux.ibm.com> (raw)
In-Reply-To: <20261203103345.1206548-1-hca@linux.ibm.com>

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@linux.ibm.com>
---
 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


  parent reply	other threads:[~2026-12-03 10:33 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03 10:33 [PATCH 00/17] s390/mm: Large kernel mappings for vmalloc and execmem ROX cache Heiko Carstens
2026-12-03 10:33 ` [PATCH 01/17] s390/boot: Initialize asce_limit for init_mm properly Heiko Carstens
2026-12-03 10:33 ` [PATCH 02/17] s390/pageattr: Only account direct mapping in direct map page counters Heiko Carstens
2026-12-03 10:33 ` [PATCH 03/17] s390/mm: Serialize kernel page table changes with init_mm mmap lock Heiko Carstens
2026-12-03 10:33 ` Heiko Carstens [this message]
2026-12-03 10:33 ` [PATCH 05/17] s390/mm: Show write-combining attribute in page table dump Heiko Carstens
2026-12-03 10:33 ` [PATCH 06/17] s390/mm: Implement HAVE_ARCH_HUGE_VMAP Heiko Carstens
2026-12-03 10:33 ` [PATCH 07/17] s390/mm: Implement HAVE_ARCH_HUGE_VMALLOC Heiko Carstens
2026-12-03 10:33 ` [PATCH 08/17] s390/pageattr: Add helpers to replace kernel page table entries Heiko Carstens
2026-12-03 10:33 ` [PATCH 09/17] s390/mm: Add _SEGMENT_ENTRY_TYPE Heiko Carstens
2026-12-03 10:33 ` [PATCH 10/17] s390/pageattr: Use common helper to change kernel page table permissions Heiko Carstens
2026-12-03 10:33 ` [PATCH 11/17] s390/pageattr: Avoid splitting mappings if permissions do not change Heiko Carstens
2026-12-03 10:33 ` [PATCH 12/17] s390/pageattr: Mark intentional 4k kernel mappings Heiko Carstens
2026-12-03 10:33 ` [PATCH 13/17] s390/pageattr: Collapse kernel mappings after changing page attributes Heiko Carstens
2026-12-03 10:33 ` [PATCH 14/17] s390/mm: Add PAGE_KERNEL_ROX Heiko Carstens
2026-12-03 10:33 ` [PATCH 15/17] s390/execmem: Add EXECMEM_KPROBES range Heiko Carstens
2026-12-03 10:33 ` [PATCH 16/17] s390/execmem: Add EXECMEM_FTRACE range Heiko Carstens
2026-12-03 10:33 ` [PATCH 17/17] s390/execmem: Enable EXECMEM_ARCH_ROX Heiko Carstens

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261203103345.1206548-5-hca@linux.ibm.com \
    --to=hca@linux.ibm.com \
    --cc=agordeev@linux.ibm.com \
    --cc=borntraeger@linux.ibm.com \
    --cc=gerald.schaefer@linux.ibm.com \
    --cc=gor@linux.ibm.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-s390@vger.kernel.org \
    --cc=rppt@kernel.org \
    --cc=svens@linux.ibm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®