From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7A685606A31C for ; Sat, 28 Nov 2026 20:15:37 +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=Iy9J1gDM Received: by kanga.kvack.org (Postfix) id 4B5D16B05B5; Sat, 28 Nov 2026 15:15:20 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 48D0F6B05B6; Sat, 28 Nov 2026 15:15:20 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3A3A46B05B7; Sat, 28 Nov 2026 15:15:20 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id A94706B05B5 for ; Sat, 28 Nov 2026 15:15:19 -0500 (EST) Received: from smtpin22.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 06BBC14072B for ; Sat, 28 Nov 2026 20:15:19 +0000 (UTC) X-FDA: 85485632358.22.DBB863D Received: from sea.source.kernel.org (sea.source.kernel.org [172.234.252.31]) by imf05.hostedemail.com (Postfix) with ESMTP id 5A53710000F for ; Sat, 28 Nov 2026 20:15:17 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b=Iy9J1gDM; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf05.hostedemail.com: domain of sashiko-bot@kernel.org designates 172.234.252.31 as permitted sender) smtp.mailfrom=sashiko-bot@kernel.org ARC-Seal: i=1; a=rsa-sha256; d=hostedemail.com; s=arc-20220608; cv=none; t=1795896917; b=581RJcVx6qpz1L3SR0hsXn1l4OMeKDal7RGH011pWJ0FFq6b3UjlTsq1MfqofwilPmZn0n tlyBiO/I44Ixtf+brFthJRyjyhP8IoBtNRAnvy8WYzFHFiDzcyJMg8XDI+hV4eKU4J638Q RYZw/fIWhfuU3eZAIoLpx4mxocRXXec= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1795896917; h=from:from:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=HD2lmg+xzyF6h3SrOMmzz4uAR2tkrovuGIgL7PCTWZg=; b=6LxuptXFGQ0J/H9hOuuMXljW6A/yCaLMZdySevp1I4++YuwpF4c8YM7JXlKQtcfv4Hm2XP AXjTsj0/Bz1xV0BT3olw0VLYBTL3CbglY67EBCvDY2mZYSAW+ggtjkL7Wh0LpEwDmMLy3M cHpfd69daWsuRnayvK/VJc08kLeJcgI= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b=Iy9J1gDM; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf05.hostedemail.com: domain of sashiko-bot@kernel.org designates 172.234.252.31 as permitted sender) smtp.mailfrom=sashiko-bot@kernel.org Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by sea.source.kernel.org (Postfix) with ESMTP id A98A940F09; Sat, 28 Nov 2026 20:15:16 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3F4891F000C1; Sat, 28 Nov 2026 20:15:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795896916; bh=HD2lmg+xzyF6h3SrOMmzz4uAR2tkrovuGIgL7PCTWZg=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=Iy9J1gDMCveJ47BCa6nQ13Dko4rFKB03JevV7KZoCgBtpY8xVNwCzSsYiiSykcVj8 aAKhGfJikLv3NUOYu3ElgfWPwNl7lx8ML6gM5bFa5zqxzGZeMZeqVgQko3FtH5bnoC pdde+3rdRNzulQQis5E9CP9yyF+aWAn5UVAe9CNIIvIHj5AipMQktVIZWEDN0PCEI0 5Fw4BFYEZYR4cqsWhleD5aS0dtGFsC+AtSawsSzEQ69BKdH8JzaxqbuGfUQ5pfK7QQ B5WAeVr7h+77e3iVTz13gvELGK7zcG40B3NNJxCxYo+oDhVr0p929/6aTV1tH9dFaR RWBJKOgRAySxA== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH v10 10/10] context_tracking,x86: Quiesce kernel text patching IPIs when tracking CR3 switches Reply-To: sashiko-reviews@lists.linux.dev To: "Valentin Schneider" , linux-mm@kvack.org, x86@kernel.org Cc: =?utf-8?b?UGV0ZXIgWmlqbHN0cmEgKEludGVsKQ==?= , "Nicolas Saenz Julienne" , "Thomas Gleixner" , "Ingo Molnar" , "Borislav Petkov" , "Dave Hansen" , "H. Peter Anvin" , "Andy Lutomirski" , "Arnaldo Carvalho de Melo" , "Josh Poimboeuf" , "Paolo Bonzini" , "Arnd Bergmann" , "Frederic Weisbecker" , "Paul E. McKenney" , "Jason Baron" , "Steven Rostedt" , "Ard Biesheuvel" , "Sami Tolvanen" , "David S. Miller" , "Neeraj Upadhyay" , "Joel Fernandes" , "Josh Triplett" , "Boqun Feng" , "Uladzislau Rezki" , "Mathieu Desnoyers" , "Mel Gorman" , "Andrew Morton" , "Masahiro Yamada" , "Han Shen" , "Rik van Riel" , "Jann Horn" , "Dan Carpenter" , "Oleg Nesterov" , "Juri Lelli" , "Clark Williams" , "Tomas Glozar" , "Yair Podemsky" , "Marcelo Tosatti" , "Daniel Wagner" , "Petr Tesarik" , "Shrikanth Hegde" , "Dan Beimborn" , "Pak Hung Vincent Chiu" In-Reply-To: <20261127201221.434876-11-vschneid@redhat.com> References: <20261127201221.434876-1-vschneid@redhat.com> <20261127201221.434876-11-vschneid@redhat.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Sat, 28 Nov 2026 20:15:13 +0000 X-Rspamd-Queue-Id: 5A53710000F X-Stat-Signature: 8es811ipnpit7m6zg8aoafmmc1qr9bkn X-Rspam-User: X-Spam-Status: No, score=-10.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_PASS,SPF_PASS autolearn=ham autolearn_force=no version=4.0.1 X-Rspamd-Server: rspam07 X-HE-Tag: 1795896917-336145 X-HE-Meta: U2FsdGVkX1+zvoZNZKfD3ilLOg9BOE99w1RCAvBW8WXqBok1gg7v2zaC9CgFGduIi4EgKZ4U0ws692D9FHXAkRe+T4fBcHh+e3XP8YEtfWacyCD2JIvsmD/YrzvySNl62AezTS4i1jMfuacxtRiC317Is3/jwmMr2IlD0fiE+39bdFGn09JQOtgcLfQcMqaGzeYf6JNSBcnMDyIUksBQbtb9/izVSloEE6kp14lxzVOTsBaorZVMOkKwqt/Vmm7Maf0GagjQS8FWyVyVQDItEzlMtWvihXiMiSNjkDdbbIDqPkUUV6LlJXEeLS6aWzxKyeB2ZqsNgcBYs7wBKaH9jV81P8EcfEWdSmvCTey0BX9j8fgCc4rzY3h4iuWgW3S759JCxnwnm2uECl1Oq/Mh7STKhw3p5CjYBJ9LHxIVLeEWpN7056ORnbIshLmAf+1Y+2aB0mLzZgMResTgOrIgeQOQcAlQRhkYuT5+lddceRgcqOwFrcIPBjV7f4+63q/lvHnBWapJoYTn8mVf/YfvlpOf8JNfi1Y+DfDOFMGpBlttF9VruhIxd6NKdhh8dgbHcYYH0UYy2c7gQXL4KIws/i1kh7L6x9e9WnOCyGjjiO4h6Ca8itG84iEU0qJD0wtZuWDjGPdg0WvpwvOt30O63ZmG1Ug4X2fq7DBs+XYBBVgqSkouNgkZ/alRX4BEAseqhEJmF1jjOD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uxGe4xG3ikJKffQrlDIqyCM9QRU37D2rKSS3iF6m0cA9bNt0MWZohofkP5JG8RfT6XIypODOvyzhVEhBh6hnAEa27D1Gk1gGY4VE2JTqTYNG30G/qjfGvjKy915JKWp5BOwb+8ZvSqd4xp4Xdp/AekSjP3FGl+lVbY0C+5Ef39RIAPcZZb4U084TIfoRybr6h buLdUIc2 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 X-Archived-By: Thank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [High] x86/alternative: missed sync_core() IPI during NMI/IST in CR3 exit window - [High] x86/alternative: double fault when smp_text_poke_batch_finish() patches entry - [High] x86/pti: user PTI page tables map pcpu_hot sharing kernel_cr3_loaded page -- commit ec3303ea2095c7cee59dcdba3812d5ac034b296c Author: Valentin Schneider context_tracking,x86: Quiesce kernel text patching IPIs when tracking CR3 s= witches This commit introduces smp_text_poke_sync_each_cpu_quiescable() to skip sending sync_core() IPIs to isolated CPUs that have kernel_cr3_loaded set to user mode, while falling back to broadcasting the IPI when patching .entry.text. > diff --git a/arch/x86/kernel/alternative.c b/arch/x86/kernel/alternative.c > index 582c6d8307d15..283c94394c0b9 100644 > --- a/arch/x86/kernel/alternative.c > +++ b/arch/x86/kernel/alternative.c [ ... ] > @@ -2636,11 +2638,46 @@ static void do_sync_core(void *info) > sync_core(); > } > =20 > +static void __smp_text_poke_sync_each_cpu(smp_cond_func_t cond_func) > +{ > + on_each_cpu_cond(cond_func, do_sync_core, NULL, 1); > +} > + > void smp_text_poke_sync_each_cpu(void) > { > - on_each_cpu(do_sync_core, NULL, 1); > + __smp_text_poke_sync_each_cpu(NULL); > } > =20 > +#ifdef CONFIG_TRACK_CR3 > +/* > + * This returns true if the IPI must be sent to @cpu; > + * > + * do that if @cpu is a housekeeping CPU, or if it is already executing > + * in kernelspace. > + */ > +static bool kernel_cr3_poke_quiesce_cond(int cpu, void *info) > +{ > + if (housekeeping_cpu(cpu, HK_TYPE_KERNEL_NOISE)) > + return true; > + > + return atomic_read(&per_cpu(kernel_cr3_loaded, cpu)) & KERNEL_CR3_IN_KE= RNEL; > +} > + > +void smp_text_poke_sync_each_cpu_quiescable(void) > +{ > + /* > + * Pairs with the MOV CR3 serializing instruction in SWITCH_TO_KERNEL_C= R3 > + * > + * Ensures any previous operations are visible on a remote CPU > + * entering the kernel and updating CR3, if this one decides to quiesce > + * the IPI. > + */ > + smp_mb(); > + > + __smp_text_poke_sync_each_cpu(kernel_cr3_poke_quiesce_cond); > +} > +#endif [Severity: High] Can kernel_cr3_poke_quiesce_cond() skip an isolated CPU while it is executing an NMI or IST handler in kernel mode? In commit "x86/mm/pti: Drive kernel_cr3_loaded", SWITCH_TO_USER_CR3 and PARANOID_RESTORE_CR3 clear kernel_cr3_loaded to KERNEL_CR3_IN_USER before writing the user page table value to %cr3: arch/x86/entry/calling.h:SWITCH_TO_USER_CR3 TRACK_CR3_KERNEL_EXIT mov %cr3, \scratch_reg ... mov \scratch_reg, %cr3 arch/x86/entry/calling.h:SAVE_AND_SWITCH_TO_KERNEL_CR3 bt $PTI_USER_PGTABLE_BIT, \scratch_reg jnc .Ldone_\@ PARANOID_TRACK_CR3_KERNEL_ENTRY \scratch_reg \save_reg ADJUST_KERNEL_CR3 \scratch_reg movq \scratch_reg, %cr3 .Ldone_\@: If an NMI or IST exception arrives during that window, %cr3 still holds the kernel page table (PTI_USER_PGTABLE_BIT is 0): CPU0 CPU1 (isolated) SWITCH_TO_USER_CR3 TRACK_CR3_KERNEL_EXIT kernel_cr3_loaded =3D 0 paranoid_entry SAVE_AND_SWITCH_TO_KERNEL_CR3 bt $PTI_USER_PGTABLE_BIT jnc .Ldone exc_nmi() smp_text_poke_batch_finish() text_poke(...) kernel_cr3_poke_quiesce_cond(1) reads kernel_cr3_loaded =3D=3D 0 skips sync_core() IPI executes patched kernel text <-- unsynchronized icache Because SAVE_AND_SWITCH_TO_KERNEL_CR3 branches to .Ldone without setting KERNEL_CR3_IN_KERNEL or writing %cr3, CPU1 runs the entire NMI or IST C handler with kernel_cr3_loaded =3D=3D 0 and no serializing MOV CR3 on entry= or exit, so it can fetch partially patched instructions. [Severity: High] Does defining and mapping kernel_cr3_loaded in commit "x86/mm/pti: Introduce kernel_cr3_loaded" also expose pcpu_hot in user PTI page tables? arch/x86/mm/pti.c defines kernel_cr3_loaded with DEFINE_PER_CPU_PAGE_ALIGNED(atomic_t, kernel_cr3_loaded), which aligns the start of the 4-byte atomic_t to PAGE_SIZE in .data..percpu..page_aligned without padding its size to a full page. Because arch/x86/mm/pti.o is the last object contributing to .data..percpu..page_aligned and PERCPU_INPUT() in include/asm-generic/vmlinux.lds.h only aligns the next section to a cacheline: include/asm-generic/vmlinux.lds.h:PERCPU_INPUT *(.data..percpu..page_aligned) \ . =3D ALIGN(cacheline); \ __per_cpu_hot_start =3D .; \ *(SORT_BY_ALIGNMENT(.data..percpu..hot.*)) \ __per_cpu_hot_start (including pcpu_hot with __stack_chk_guard, current_task, and cpu_current_top_of_stack) is placed at offset 64 of the same 4KB page as kernel_cr3_loaded, and pti_clone_user_shared() maps that entire 4KB page into the user page tables. > @@ -2808,6 +2845,7 @@ noinstr int smp_text_poke_int3_handler(struct pt_re= gs *regs) > */ > void smp_text_poke_batch_finish(void) > { > + void (*sync_fn)(void) =3D smp_text_poke_sync_each_cpu_quiescable; > unsigned char int3 =3D INT3_INSN_OPCODE; > unsigned int i; > int do_sync; > @@ -2844,11 +2882,20 @@ void smp_text_poke_batch_finish(void) > * First step: add a INT3 trap to the address that will be patched. > */ > for (i =3D 0; i < text_poke_array.nr_entries; i++) { > - text_poke_array.vec[i].old =3D *(u8 *)text_poke_addr(&text_poke_array.= vec[i]); > - text_poke(text_poke_addr(&text_poke_array.vec[i]), &int3, INT3_INSN_SI= ZE); > + void *addr =3D text_poke_addr(&text_poke_array.vec[i]); > + > + /* > + * There's no safe way to defer IPIs for patching text in > + * entry, record whether there is at least one such poke. > + */ > + if (is_kernel_entrytext((unsigned long)addr)) > + sync_fn =3D smp_text_poke_sync_each_cpu; > + > + text_poke_array.vec[i].old =3D *((u8 *)addr); > + text_poke(addr, &int3, INT3_INSN_SIZE); > } > =20 > - smp_text_poke_sync_each_cpu(); > + sync_fn(); [Severity: High] Can patching a static branch in .entry.text at runtime via smp_text_poke_batch_finish() cause a double fault on CPUs entering or exiting the kernel? When housekeeping_update() enables housekeeping_overridden at runtime (for example, when creating an isolated cpuset partition), smp_text_poke_batch_finish() writes INT3 (0xCC) to the static branch sites inside TRACK_CR3_KERNEL_ENTRY, TRACK_CR3_KERNEL_EXIT, PARANOID_TRACK_CR3_KERNEL_ENTRY, and PARANOID_TRACK_CR3_KERNEL_EXIT, and then broadcasts sync_fn(): arch/x86/entry/entry_64.S:entry_SYSCALL_64 swapgs movq %rsp, PER_CPU_VAR(cpu_tss_rw + TSS_sp2) SWITCH_TO_KERNEL_CR3 scratch_reg=3D%rsp movq PER_CPU_VAR(cpu_current_top_of_stack), %rsp arch/x86/entry/calling.h:SWITCH_TO_KERNEL_CR3 ALTERNATIVE "jmp .Lend_\@", "", X86_FEATURE_PTI mov %cr3, \scratch_reg TRACK_CR3_KERNEL_ENTRY ADJUST_KERNEL_CR3 \scratch_reg mov \scratch_reg, %cr3 A CPU that hits this transient INT3 traps into asm_exc_int3 at CPL 0 while %cr3 still points to the user page tables (and in entry_SYSCALL_64 and entry_SYSCALL_compat, while %rsp holds the raw %cr3 value because X86_TRAP_= BP does not use an IST stack). error_entry sees CS at CPL 0 and skips switching to the kernel CR3 or kernel GSBASE, so pushing the exception frame or calling exc_int3() faults immediately and escalates to a double fault. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261127201221.4348= 76-1-vschneid@redhat.com?part=3D10