From nobody Sun Feb 8 19:29:18 2026 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D81211EA0B1; Wed, 7 Aug 2024 13:51:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723038692; cv=none; b=uXgDMdgvDY3bYj6Dc6O7UloqlOb5ppQYAnX698X1D2Qqf2L9RNtj5MOIHC03KOWsalJht5/dTiwCbAQnyy9w0q7Oym7+YjRYEk6jjUu33rBOE1ul/nFDpKkDT8LVQvFs67BRLpXesEEj7BFDLrFo0ScMVNl7u57yYdhdOHUewVc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723038692; c=relaxed/simple; bh=+ArvcJ+SVTSdF3//d0/2Z0W3jmLTRqBB9l1SLvA6A0Y=; h=Date:From:To:Subject:Cc:In-Reply-To:References:MIME-Version: Message-ID:Content-Type; b=MI6bxge4JegK+JzgEUFLNV8moGUP5ixu+l7b2X9E5GptNLuWnQbe7rLpmeW5XxZT2jcgRWbB440AdFBgUTtnfWTN4MUTCaChbMCDOxet7jH3etlSA/3pEEyTuaWnU9pR9mha4mdyEbnVwl4gsF4pKt3S66A6yrBGQCUgKCohUuE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=HNG2Tnon; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=G+0xwcL/; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="HNG2Tnon"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="G+0xwcL/" Date: Wed, 07 Aug 2024 13:51:28 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1723038689; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=WCRJMFO19NouQjK6DK8DSCS11JvrZ2OTclUmHqBf1Ug=; b=HNG2TnonyT5n3GPOWdHmzLYyyGdvsAicv+JVy5okg6WX1Oa8LOmDDiYvimZ3qk3BuWgBE2 xiGpyGi6OPdA4fkkPuxV6C7RSWu5E89gpwtULvrfenJjPoFruk0cg3TpojKzU8sDPbI5Uz uMeEMRyTHhYejn8Y2aghbOYvsSUckBCeEPOiQ53b18Wblm7/kxB4MqYMbKoilOPSwvcNcl tU+KqV2VDWp8qn7qZVu91jZWAS2d281pGc6cNBpC/Nsqjj1/khmXR3EmoMQeqgT7UYYijH qXpfvRjGZa9YqGesZzVAEtdW+TvA971sKcZoFmr2WOBIsH7FEPjSsgK9j6+zTg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1723038689; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=WCRJMFO19NouQjK6DK8DSCS11JvrZ2OTclUmHqBf1Ug=; b=G+0xwcL/WMGJax3p0NKwVr7qdJBHhA4zjtOlsXA4f0d0yPS58zJvw9aj8sn7c5FN5gvV+T uD9IJTlbq5IF42BQ== From: "tip-bot2 for Thomas Gleixner" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: x86/urgent] x86/mm: Fix PTI for i386 some more Cc: Guenter Roeck , Thomas Gleixner , "Peter Zijlstra (Intel)" , x86@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: <20240806184843.GX37996@noisy.programming.kicks-ass.net> References: <20240806184843.GX37996@noisy.programming.kicks-ass.net> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-ID: <172303868832.2215.13077899032495646705.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails Precedence: bulk Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable The following commit has been merged into the x86/urgent branch of tip: Commit-ID: c48b5a4cf3125adb679e28ef093f66ff81368d05 Gitweb: https://git.kernel.org/tip/c48b5a4cf3125adb679e28ef093f66ff8= 1368d05 Author: Thomas Gleixner AuthorDate: Tue, 06 Aug 2024 20:48:43 +02:00 Committer: Peter Zijlstra CommitterDate: Wed, 07 Aug 2024 15:35:01 +02:00 x86/mm: Fix PTI for i386 some more So it turns out that we have to do two passes of pti_clone_entry_text(), once before initcalls, such that device and late initcalls can use user-mode-helper / modprobe and once after free_initmem() / mark_readonly(). Now obviously mark_readonly() can cause PMD splits, and pti_clone_pgtable() doesn't like that much. Allow the late clone to split PMDs so that pagetables stay in sync. [peterz: Changelog and comments] Reported-by: Guenter Roeck Signed-off-by: Thomas Gleixner Signed-off-by: Peter Zijlstra (Intel) Tested-by: Guenter Roeck Link: https://lkml.kernel.org/r/20240806184843.GX37996@noisy.programming.ki= cks-ass.net --- arch/x86/mm/pti.c | 45 +++++++++++++++++++++++++++++---------------- 1 file changed, 29 insertions(+), 16 deletions(-) diff --git a/arch/x86/mm/pti.c b/arch/x86/mm/pti.c index bfdf5f4..851ec8f 100644 --- a/arch/x86/mm/pti.c +++ b/arch/x86/mm/pti.c @@ -241,7 +241,7 @@ static pmd_t *pti_user_pagetable_walk_pmd(unsigned long= address) * * Returns a pointer to a PTE on success, or NULL on failure. */ -static pte_t *pti_user_pagetable_walk_pte(unsigned long address) +static pte_t *pti_user_pagetable_walk_pte(unsigned long address, bool late= _text) { gfp_t gfp =3D (GFP_KERNEL | __GFP_NOTRACK | __GFP_ZERO); pmd_t *pmd; @@ -251,10 +251,15 @@ static pte_t *pti_user_pagetable_walk_pte(unsigned lo= ng address) if (!pmd) return NULL; =20 - /* We can't do anything sensible if we hit a large mapping. */ + /* Large PMD mapping found */ if (pmd_leaf(*pmd)) { - WARN_ON(1); - return NULL; + /* Clear the PMD if we hit a large mapping from the first round */ + if (late_text) { + set_pmd(pmd, __pmd(0)); + } else { + WARN_ON_ONCE(1); + return NULL; + } } =20 if (pmd_none(*pmd)) { @@ -283,7 +288,7 @@ static void __init pti_setup_vsyscall(void) if (!pte || WARN_ON(level !=3D PG_LEVEL_4K) || pte_none(*pte)) return; =20 - target_pte =3D pti_user_pagetable_walk_pte(VSYSCALL_ADDR); + target_pte =3D pti_user_pagetable_walk_pte(VSYSCALL_ADDR, false); if (WARN_ON(!target_pte)) return; =20 @@ -301,7 +306,7 @@ enum pti_clone_level { =20 static void pti_clone_pgtable(unsigned long start, unsigned long end, - enum pti_clone_level level) + enum pti_clone_level level, bool late_text) { unsigned long addr; =20 @@ -390,7 +395,7 @@ pti_clone_pgtable(unsigned long start, unsigned long en= d, return; =20 /* Allocate PTE in the user page-table */ - target_pte =3D pti_user_pagetable_walk_pte(addr); + target_pte =3D pti_user_pagetable_walk_pte(addr, late_text); if (WARN_ON(!target_pte)) return; =20 @@ -452,7 +457,7 @@ static void __init pti_clone_user_shared(void) phys_addr_t pa =3D per_cpu_ptr_to_phys((void *)va); pte_t *target_pte; =20 - target_pte =3D pti_user_pagetable_walk_pte(va); + target_pte =3D pti_user_pagetable_walk_pte(va, false); if (WARN_ON(!target_pte)) return; =20 @@ -475,7 +480,7 @@ static void __init pti_clone_user_shared(void) start =3D CPU_ENTRY_AREA_BASE; end =3D start + (PAGE_SIZE * CPU_ENTRY_AREA_PAGES); =20 - pti_clone_pgtable(start, end, PTI_CLONE_PMD); + pti_clone_pgtable(start, end, PTI_CLONE_PMD, false); } #endif /* CONFIG_X86_64 */ =20 @@ -492,11 +497,11 @@ static void __init pti_setup_espfix64(void) /* * Clone the populated PMDs of the entry text and force it RO. */ -static void pti_clone_entry_text(void) +static void pti_clone_entry_text(bool late) { pti_clone_pgtable((unsigned long) __entry_text_start, (unsigned long) __entry_text_end, - PTI_LEVEL_KERNEL_IMAGE); + PTI_LEVEL_KERNEL_IMAGE, late); } =20 /* @@ -571,7 +576,7 @@ static void pti_clone_kernel_text(void) * pti_set_kernel_image_nonglobal() did to clear the * global bit. */ - pti_clone_pgtable(start, end_clone, PTI_LEVEL_KERNEL_IMAGE); + pti_clone_pgtable(start, end_clone, PTI_LEVEL_KERNEL_IMAGE, false); =20 /* * pti_clone_pgtable() will set the global bit in any PMDs @@ -638,8 +643,15 @@ void __init pti_init(void) =20 /* Undo all global bits from the init pagetables in head_64.S: */ pti_set_kernel_image_nonglobal(); + /* Replace some of the global bits just for shared entry text: */ - pti_clone_entry_text(); + /* + * This is very early in boot. Device and Late initcalls can do + * modprobe before free_initmem() and mark_readonly(). This + * pti_clone_entry_text() allows those user-mode-helpers to function, + * but notably the text is still RW. + */ + pti_clone_entry_text(false); pti_setup_espfix64(); pti_setup_vsyscall(); } @@ -656,10 +668,11 @@ void pti_finalize(void) if (!boot_cpu_has(X86_FEATURE_PTI)) return; /* - * We need to clone everything (again) that maps parts of the - * kernel image. + * This is after free_initmem() (all initcalls are done) and we've done + * mark_readonly(). Text is now NX which might've split some PMDs + * relative to the early clone. */ - pti_clone_entry_text(); + pti_clone_entry_text(true); pti_clone_kernel_text(); =20 debug_checkwx_user();