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 13/17] s390/pageattr: Collapse kernel mappings after changing page attributes
Date: Thu, 3 Dec 2026 11:33:41 +0100 [thread overview]
Message-ID: <20261203103345.1206548-14-hca@linux.ibm.com> (raw)
In-Reply-To: <20261203103345.1206548-1-hca@linux.ibm.com>
After a successful set_memory_rox(), replace page tables with 1MB
mappings if alignment and permissions allow. Support for collapsing
into 2GB mappings is added for the sake of completeness, although
this cannot happen at the moment.
Like on x86, only ROX mappings are collapsed. Direct map aliases and
intentional 4k mappings (_PAGE_KERNEL_4K) are left alone.
Signed-off-by: Heiko Carstens <hca@linux.ibm.com>
---
arch/s390/include/asm/pgalloc.h | 1 +
arch/s390/mm/pageattr.c | 119 ++++++++++++++++++++++++++++++++
arch/s390/mm/vmem.c | 2 +-
3 files changed, 121 insertions(+), 1 deletion(-)
diff --git a/arch/s390/include/asm/pgalloc.h b/arch/s390/include/asm/pgalloc.h
index f5adddc10946..9754085399b9 100644
--- a/arch/s390/include/asm/pgalloc.h
+++ b/arch/s390/include/asm/pgalloc.h
@@ -167,6 +167,7 @@ void pte_free_defer(struct mm_struct *mm, pgtable_t pgtable);
void vmem_map_init(void);
void *vmem_crst_alloc(unsigned long val);
+void vmem_crst_free(unsigned long *table);
pte_t *vmem_pte_alloc(void);
void vmem_pte_free(unsigned long *table);
diff --git a/arch/s390/mm/pageattr.c b/arch/s390/mm/pageattr.c
index 28f1e5fb8e01..0a22f8c7bbe1 100644
--- a/arch/s390/mm/pageattr.c
+++ b/arch/s390/mm/pageattr.c
@@ -389,6 +389,119 @@ static int change_page_attr(unsigned long addr, unsigned long end,
return rc;
}
+#define PTE_COLLAPSE_BITS (PAGE_MASK | _PAGE_PROTECT | _PAGE_NOEXEC | \
+ _PAGE_INVALID | _PAGE_KERNEL_4K)
+
+#define PMD_COLLAPSE_BITS (_SEGMENT_ENTRY_ORIGIN_LARGE | _SEGMENT_ENTRY_LARGE | \
+ _SEGMENT_ENTRY_PROTECT | _SEGMENT_ENTRY_NOEXEC | \
+ _SEGMENT_ENTRY_INVALID)
+
+static bool collapse_pmd_page(pmd_t *pmdp, unsigned long addr)
+{
+ unsigned long first, expected, prot, pa;
+ pmd_t old, new;
+ pte_t *ptep;
+ bool ro, nx;
+ int i;
+
+ if (!cpu_has_edat1())
+ return false;
+ ptep = pte_offset_kernel(pmdp, addr);
+ first = pte_val(ptep_get(ptep)) & PTE_COLLAPSE_BITS;
+ if (first & (_PAGE_INVALID | _PAGE_KERNEL_4K))
+ return false;
+ pa = first & PAGE_MASK;
+ if (!IS_ALIGNED(pa, PMD_SIZE))
+ return false;
+ expected = first;
+ for (i = 1, ptep++; i < PTRS_PER_PTE; i++, ptep++) {
+ expected += PAGE_SIZE;
+ if ((pte_val(ptep_get(ptep)) & PTE_COLLAPSE_BITS) != expected)
+ return false;
+ }
+ ro = first & _PAGE_PROTECT;
+ nx = first & _PAGE_NOEXEC;
+ prot = pgprot_val(ro ? SEGMENT_KERNEL_RO : SEGMENT_KERNEL);
+ if (!nx)
+ prot &= ~_SEGMENT_ENTRY_NOEXEC;
+ new = __pmd(pa | prot);
+ old = pmdp_replace_kernel(pmdp, new, addr);
+ vmem_pte_free((unsigned long *)pmd_deref(old));
+ update_direct_map_count(addr, PG_DIRECT_MAP_4K, -PTRS_PER_PTE);
+ update_direct_map_count(addr, PG_DIRECT_MAP_1M, 1);
+ return true;
+}
+
+static void collapse_pud_page(pud_t *pudp, unsigned long addr)
+{
+ unsigned long first, expected, prot, pa;
+ pud_t old, new;
+ pmd_t *pmdp;
+ bool ro, nx;
+ int i;
+
+ if (!cpu_has_edat2())
+ return;
+ pmdp = pmd_offset(pudp, addr & PUD_MASK);
+ first = pmd_val(pmdp_get(pmdp)) & PMD_COLLAPSE_BITS;
+ if (!(first & _SEGMENT_ENTRY_LARGE) || (first & _SEGMENT_ENTRY_INVALID))
+ return;
+ pa = first & _SEGMENT_ENTRY_ORIGIN_LARGE;
+ if (!IS_ALIGNED(pa, PUD_SIZE))
+ return;
+ expected = first;
+ for (i = 1, pmdp++; i < PTRS_PER_PMD; i++, pmdp++) {
+ expected += PMD_SIZE;
+ if ((pmd_val(pmdp_get(pmdp)) & PMD_COLLAPSE_BITS) != expected)
+ return;
+ }
+ ro = first & _SEGMENT_ENTRY_PROTECT;
+ nx = first & _SEGMENT_ENTRY_NOEXEC;
+ prot = pgprot_val(ro ? REGION3_KERNEL_RO : REGION3_KERNEL);
+ if (!nx)
+ prot &= ~_REGION_ENTRY_NOEXEC;
+ new = __pud(pa | prot);
+ old = pudp_replace_kernel(pudp, new, addr);
+ vmem_crst_free((unsigned long *)pud_deref(old));
+ update_direct_map_count(addr, PG_DIRECT_MAP_1M, -PTRS_PER_PMD);
+ update_direct_map_count(addr, PG_DIRECT_MAP_2G, 1);
+}
+
+static void collapse_page_attr_one(unsigned long addr)
+{
+ pgd_t *pgdp;
+ p4d_t *p4dp;
+ pud_t *pudp;
+ pmd_t *pmdp;
+ pmd_t pmd;
+ pud_t pud;
+
+ addr &= PMD_MASK;
+ pgdp = pgd_offset_k(addr);
+ if (pgd_none(pgdp_get(pgdp)))
+ return;
+ p4dp = p4d_offset(pgdp, addr);
+ if (p4d_none(p4dp_get(p4dp)))
+ return;
+ pudp = pud_offset(p4dp, addr);
+ pud = pudp_get(pudp);
+ if (pud_none(pud) || pud_leaf(pud))
+ return;
+ pmdp = pmd_offset(pudp, addr);
+ pmd = pmdp_get(pmdp);
+ if (pmd_none(pmd) || pmd_leaf(pmd))
+ return;
+ if (!collapse_pmd_page(pmdp, addr))
+ return;
+ collapse_pud_page(pudp, addr);
+}
+
+static void collapse_page_attr(unsigned long addr, unsigned long end)
+{
+ for (addr &= PMD_MASK; addr < end; addr += PMD_SIZE)
+ collapse_page_attr_one(addr);
+}
+
static int change_page_attr_alias(unsigned long addr, unsigned long end,
unsigned long flags)
{
@@ -428,8 +541,10 @@ static int change_page_attr_alias(unsigned long addr, unsigned long end,
int __set_memory(unsigned long addr, unsigned long numpages, unsigned long flags)
{
unsigned long end;
+ bool collapse;
int rc;
+ collapse = flags == (SET_MEMORY_RO | SET_MEMORY_X);
if (!cpu_has_nx())
flags &= ~(SET_MEMORY_NX | SET_MEMORY_X);
if (!flags)
@@ -443,6 +558,10 @@ int __set_memory(unsigned long addr, unsigned long numpages, unsigned long flags
if (rc)
goto out;
rc = change_page_attr_alias(addr, end, flags);
+ if (rc)
+ goto out;
+ if (collapse)
+ collapse_page_attr(addr, end);
out:
mmap_write_unlock(&init_mm);
return rc;
diff --git a/arch/s390/mm/vmem.c b/arch/s390/mm/vmem.c
index 56f88fb25784..05f31ac599e6 100644
--- a/arch/s390/mm/vmem.c
+++ b/arch/s390/mm/vmem.c
@@ -73,7 +73,7 @@ void __ref *vmem_crst_alloc(unsigned long val)
return table;
}
-static void vmem_crst_free(unsigned long *table)
+void vmem_crst_free(unsigned long *table)
{
if (PageTable(virt_to_page(table)))
crst_table_free(&init_mm, table);
--
2.53.0
next prev parent reply other threads:[~2026-12-03 10:34 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 ` [PATCH 04/17] s390/vmem: Use generic page table allocators for vmem page tables Heiko Carstens
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 ` Heiko Carstens [this message]
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-14-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®