From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796296258; b=06dHTvAtJxgqHkrxI3l5b7IqAuXUBA7WO8gVp7VwwD5iz8zXlhWUsiNz6gbxJpIStZ6S BnDREM7OdTEXwkxXhZyQu3d2fgn5ddLdOpPWyYcZ+p2UEPQsFuifp/KekTN4S2BMeWy3M cnaJfTHlrPXdbwxgZ8CTLjvjMAkMMG/r03Q9F5WgRfYcdITG4OOMlY5tebxM/2CfbXio0 1WtSvEw28nWFE8s1ctfg6QbvFlxovAxL8SfDXdlhlp1zTKQwzTcTcIe3sJQZwPaE5zHyv Fg2XYY5S8KPvDlhqaU2Uwy67Tw9bq4ph8dpyfAOqQRtU3ItOEJszvxexUZ6/3rdFohw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796296258; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=zj2FL9eW61kxTYJsS7zvWvPkIsupXrQ7zf653lUsnDY=; b=YQ/+gsskehqATOZsR+7Od6xZBjnFjlrO4wZ39eJpDBHa/lxexdXjIydjpRdhpjn+D/wT ofeUd4OT/zClPWjcv00nwXByCxyWr07Wn1/DUrJHPTh4Ajv1XXESOJ2/8F5SGZiVbBrTj 2Kh7MFTgXYXigkGmdAxuHvyTSlsWKfahw/2OeOyM097QEDvLYtiRpPqDNegfPrEIJ2knL ED2H/00l2pf2LwHUFAUjFoEzXaie9OpsNDfPvlvRBif+IdhEhITvGb7JOPIRu237TlZsa Oz7K+OHdOguKY12X8Fm19+jDi/SaWKWnSAAJiIbl41tY4jJGwJVtwZTOxZauRZcjn8w== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=OjxxiAam; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B224D606A322 for ; Thu, 3 Dec 2026 11:10:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=OjxxiAam Received: by smtp.kernel.org (Postfix) with ESMTPSA id 175841F000C1; Thu, 3 Dec 2026 11:10:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796296258; bh=zj2FL9eW61kxTYJsS7zvWvPkIsupXrQ7zf653lUsnDY=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=OjxxiAamz1v75FMOaOg4DUV3q+kT9xxPPlZHLOEFyea6vMog9MKSrmFDzKDnEKB6l cMvPt+CNTDYhpJSALtIP5BRXQQfSqExnHlJcNCKBdHKlEHYy66Yrqhjm7En855ydV/ FzRTfTUJjgeHJFnaaIynD/zJKxJuyfQwwDuwt9hLOvXpI1lOso0vrvXMynm9MchAXl 2YQQu+H1faraBdcTaMVhnWrUWcsOLW2kD4vqK4pwYLWy0IZi1tmBu3EnOJ+reCgP0t D/hR9eKaXexJTDGJ59G5iuuwNGzIfWjnEftlDAUhuuLna9ZQdQWM4+ymMxsnNmkWwt wJHFWZvkYeLwA== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH 13/17] s390/pageattr: Collapse kernel mappings after changing page attributes Reply-To: sashiko-reviews@lists.linux.dev To: "Heiko Carstens" Cc: linux-s390@vger.kernel.org, "Vasily Gorbik" , "Alexander Gordeev" , "Christian Borntraeger" In-Reply-To: <20261203103345.1206548-14-hca@linux.ibm.com> References: <20261203103345.1206548-1-hca@linux.ibm.com> <20261203103345.1206548-14-hca@linux.ibm.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Thu, 03 Dec 2026 11:10:57 +0000 X-Mailing-List: linux-s390@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list 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 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, uns= igned long end, > return rc; > } > =20 > +#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 =3D pte_offset_kernel(pmdp, addr); > + first =3D 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 =3D 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 =3D __pmd(pa | prot); > + old =3D 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 =3D pmd_offset(pud, addr) if (pmd_leaf(*pmd)) ... // false ptep =3D pte_offset_kernel(pmd, addr) pmdp_replace_kernel(...) vmem_pte_free(old) // page reused/unmapped pte =3D 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=3Dn). 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=3Dn) when CONFIG_MEMORY_HOTPLUG=3Dn. [ ... ] > +static void collapse_page_attr_one(unsigned long addr) > +{ [ ... ] > + pmdp =3D pmd_offset(pudp, addr); > + pmd =3D 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; > } > =20 > -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(). --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261203103345.1206= 548-1-hca@linux.ibm.com?part=3D13