All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Heiko Carstens" <hca@linux.ibm.com>
Cc: linux-s390@vger.kernel.org, "Vasily Gorbik" <gor@linux.ibm.com>,
	"Alexander Gordeev" <agordeev@linux.ibm.com>,
	"Christian Borntraeger" <borntraeger@linux.ibm.com>
Subject: Re: [PATCH 13/17] s390/pageattr: Collapse kernel mappings after changing page attributes
Date: Thu, 03 Dec 2026 11:10:57 +0000	[thread overview]
Message-ID: <sashiko-outbox-247649@kernel.org> (raw)
In-Reply-To: <20261203103345.1206548-14-hca@linux.ibm.com>

Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [Medium] s390/vmem: vmem_crst_free() lacks !slab_is_available()
  memblock_free() path
- [Medium] s390/pageattr: collapse_pmd_page() races with lockless
  __vmalloc_to_page()
- [Medium] s390/pageattr: set_pte() clears _PAGE_KERNEL_4K on present
  kernel PTEs
- [Low] s390/mm: collapse_page_attr_one() skips 2GB collapse on 1MB leaf
  PMDs
- [Low] s390/vmem: missing __ref on vmem_pte_free() calling
  __init_memblock
--

commit 28c06261f37ae1ca620f60e322d1e3723878d47d
Author: Heiko Carstens <hca@linux.ibm.com>

s390/pageattr: Collapse kernel mappings after changing page attributes

This commit collapses kernel page tables into 1MB (and 2GB) leaf mappings
after a successful set_memory_rox() when alignment and permissions allow,
leaving direct map aliases and _PAGE_KERNEL_4K mappings alone.

> diff --git a/arch/s390/mm/pageattr.c b/arch/s390/mm/pageattr.c
> index 28f1e5fb8e010..0a22f8c7bbe16 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;

[Severity: Medium]
Can _PAGE_KERNEL_4K ever be set on a present kernel PTE here?

_PAGE_KERNEL_4K is aliased to _PAGE_UNUSED in arch/s390/include/asm/pgtable.h,
and set_pte() unconditionally clears _PAGE_UNUSED whenever pte_present(pte)
is true:

arch/s390/include/asm/pgtable.h:set_pte() {
	if (pte_present(pte))
		pte = clear_pte_bit(pte, __pgprot(_PAGE_UNUSED));
	WRITE_ONCE(*ptep, pte);
}

Every path that sets _PAGE_KERNEL_4K on mapped kernel PTEs -- including
walk_pte_level() for set_memory_4k() and modify_pte_table() for
debug_pagealloc_enabled() -- writes the entry through set_pte(), so the bit
is immediately stripped.

If set_memory_rox() is called on a 1MB-aligned range covering intentional
4k mappings, collapse_pmd_page() will collapse them into a 1MB leaf PMD,
causing later virt_to_kpte() lookups in __kernel_map_pages() to treat the
1MB frame as a page table.

[ ... ]

> +	new = __pmd(pa | prot);
> +	old = pmdp_replace_kernel(pmdp, new, addr);
> +	vmem_pte_free((unsigned long *)pmd_deref(old));

[Severity: Medium]
Can freeing the old PTE table here race with lockless init_mm page table
walkers such as __vmalloc_to_page()?

collapse_pmd_page() runs under mmap_write_lock(&init_mm) and frees the
replaced PTE page immediately via vmem_pte_free() without RCU deferral
(collapse_pud_page() does the same with vmem_crst_free()).

Meanwhile, /proc/kcore reads live vmalloc and module ranges via
read_kcore_iter() -> vread_iter() -> aligned_vread_iter() ->
__vmalloc_to_page() without holding init_mm.mmap_lock:

CPU0                          CPU1
__set_memory()
  mmap_write_lock(&init_mm)
  collapse_pmd_page()
                              aligned_vread_iter()
                                __vmalloc_to_page()
                                  pmd = pmd_offset(pud, addr)
                                  if (pmd_leaf(*pmd)) ... // false
                                  ptep = pte_offset_kernel(pmd, addr)
    pmdp_replace_kernel(...)
    vmem_pte_free(old)
    // page reused/unmapped
                                  pte = ptep_get(ptep)  <-- use-after-free

If pmdp_replace_kernel() instead runs between pmd_leaf(*pmd) and
pte_offset_kernel(pmd, addr), pte_offset_kernel() re-reads *pmd as a 1MB
leaf and reads executable code as a PTE.  Either race can pass a bogus
struct page pointer to copy_page_to_iter_nofault() and crash or leak data.

[Severity: Low]
Does vmem_pte_free() need a __ref annotation?

vmem_pte_free() in arch/s390/mm/vmem.c calls memblock_free() when
slab_is_available() is false, and memblock_free() is marked __init_memblock
(which expands to __init on s390 when CONFIG_MEMORY_HOTPLUG=n).

Unlike vmem_crst_alloc() and vmem_pte_alloc(), vmem_pte_free() lacks __ref,
causing a modpost section mismatch warning (or build failure with
CONFIG_SECTION_MISMATCH_WARN_ONLY=n) when CONFIG_MEMORY_HOTPLUG=n.

[ ... ]

> +static void collapse_page_attr_one(unsigned long addr)
> +{
[ ... ]
> +	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);
> +}

[Severity: Low]
Will returning early on pmd_leaf(pmd) skip 2GB PUD collapse when the
modified PMDs are already 1MB leaf entries?

When set_memory_rox() updates a 1MB-aligned leaf PMD, walk_pmd_level()
replaces it in place via pmdp_replace_kernel() without splitting it to 4KB
PTEs.

Because pmd_leaf(pmd) is already true, collapse_page_attr_one() returns
before calling collapse_pud_page(pudp, addr), leaving a collapsible 2GB PUD
fragmented at 1MB granularity unless a PMD was collapsed from 4KB PTEs.

[ ... ]

> diff --git a/arch/s390/mm/vmem.c b/arch/s390/mm/vmem.c
> index 56f88fb257845..05f31ac599e63 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);

[Severity: Medium]
Is vmem_crst_free() safe to call before slab_is_available() is true?

collapse_pud_page() is reachable during early boot via vmem_map_init() ->
__set_memory_rox() before sparse_init() and kmem_cache_init(), and
vmemmap_populate() error rollback can also call vmem_crst_free() before
slab is available.

Unlike vmem_crst_alloc() and vmem_pte_free(), vmem_crst_free() calls
virt_to_page() and falls back to vmem_free_pages(), which will crash on
unmapped vmemmap or corrupt an uninitialized buddy allocator.

Checking !slab_is_available() to call memblock_free(table,
PAGE_SIZE << CRST_ALLOC_ORDER) and marking vmem_crst_free() __ref would
match vmem_crst_alloc() and vmem_pte_free().

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261203103345.1206548-1-hca@linux.ibm.com?part=13

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

Thread overview: 28+ 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 11:01   ` sashiko-bot
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 11:02   ` sashiko-bot
2026-12-03 18:36   ` kernel test robot
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 11:03   ` sashiko-bot
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 11:00   ` sashiko-bot
2026-12-03 10:33 ` [PATCH 13/17] s390/pageattr: Collapse kernel mappings after changing page attributes Heiko Carstens
2026-12-03 11:10   ` sashiko-bot [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 11:03   ` sashiko-bot
2026-12-03 10:33 ` [PATCH 16/17] s390/execmem: Add EXECMEM_FTRACE range Heiko Carstens
2026-12-03 11:04   ` sashiko-bot
2026-12-03 10:33 ` [PATCH 17/17] s390/execmem: Enable EXECMEM_ARCH_ROX Heiko Carstens
2026-12-03 11:09   ` sashiko-bot
2026-12-03 11:11 ` [PATCH 00/17] s390/mm: Large kernel mappings for vmalloc and execmem ROX cache sashiko-bot

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=sashiko-outbox-247649@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=agordeev@linux.ibm.com \
    --cc=borntraeger@linux.ibm.com \
    --cc=gor@linux.ibm.com \
    --cc=hca@linux.ibm.com \
    --cc=linux-s390@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.