Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Valentin Schneider" <vschneid@redhat.com>,
	linux-mm@kvack.org, x86@kernel.org
Cc: "Thomas Gleixner" <tglx@linutronix.de>,
	"Ingo Molnar" <mingo@redhat.com>,
	"Borislav Petkov" <bp@alien8.de>,
	"Dave Hansen" <dave.hansen@linux.intel.com>,
	"H. Peter Anvin" <hpa@zytor.com>,
	"Andy Lutomirski" <luto@kernel.org>,
	"Peter Zijlstra" <peterz@infradead.org>,
	"Arnaldo Carvalho de Melo" <acme@kernel.org>,
	"Josh Poimboeuf" <jpoimboe@kernel.org>,
	"Paolo Bonzini" <pbonzini@redhat.com>,
	"Arnd Bergmann" <arnd@arndb.de>,
	"Frederic Weisbecker" <frederic@kernel.org>,
	"Paul E. McKenney" <paulmck@kernel.org>,
	"Jason Baron" <jbaron@akamai.com>,
	"Steven Rostedt" <rostedt@goodmis.org>,
	"Ard Biesheuvel" <ardb@kernel.org>,
	"Sami Tolvanen" <samitolvanen@google.com>,
	"David S. Miller" <davem@davemloft.net>,
	"Neeraj Upadhyay" <neeraj.upadhyay@kernel.org>,
	"Joel Fernandes" <joelagnelf@nvidia.com>,
	"Josh Triplett" <josh@joshtriplett.org>,
	"Boqun Feng" <boqun.feng@gmail.com>,
	"Uladzislau Rezki" <urezki@gmail.com>,
	"Mathieu Desnoyers" <mathieu.desnoyers@efficios.com>,
	"Mel Gorman" <mgorman@suse.de>,
	"Andrew Morton" <akpm@linux-foundation.org>,
	"Masahiro Yamada" <masahiroy@kernel.org>,
	"Han Shen" <shenhan@google.com>,
	"Rik van Riel" <riel@surriel.com>, "Jann Horn" <jannh@google.com>,
	"Dan Carpenter" <dan.carpenter@linaro.org>,
	"Oleg Nesterov" <oleg@redhat.com>,
	"Juri Lelli" <juri.lelli@redhat.com>,
	"Clark Williams" <williams@redhat.com>,
	"Tomas Glozar" <tglozar@redhat.com>,
	"Yair Podemsky" <ypodemsk@redhat.com>,
	"Marcelo Tosatti" <mtosatti@redhat.com>,
	"Daniel Wagner" <dwagner@suse.de>,
	"Petr Tesarik" <ptesarik@suse.com>,
	"Shrikanth Hegde" <sshegde@linux.ibm.com>,
	"Dan Beimborn" <Dan.Beimborn@qube-rt.com>,
	"Pak Hung Vincent Chiu" <pak-hung.chiu@qube-rt.com>
Subject: Re: [PATCH v10 09/10] x86/mm/pti: Drive kernel_cr3_loaded
Date: Sat, 28 Nov 2026 20:15:11 +0000	[thread overview]
Message-ID: <sashiko-outbox-239014@kernel.org> (raw)
In-Reply-To: <20261127201221.434876-10-vschneid@redhat.com>

Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [High] x86/entry: runtime text poking of housekeeping_overridden
  crashes entry
- [Medium] x86/entry: NMI in CR3 exit window leaves kernel_cr3_loaded as
  IN_USER
- [Medium] x86/hw_breakpoint: missing within_cpu_entry() check for
  kernel_cr3_loaded
- [Medium] x86/entry: gating CR3 tracking on housekeeping_overridden
  without nohz_full
--

commit 084ab06088ad604a25eb3511c62e665e540e068d
Author: Valentin Schneider <vschneid@redhat.com>

x86/mm/pti: Drive kernel_cr3_loaded

This commit updates the per-CPU kernel_cr3_loaded variable around PTI CR3
switches in the x86 entry and exit paths when CONFIG_TRACK_CR3 is enabled.

> diff --git a/arch/x86/entry/calling.h b/arch/x86/entry/calling.h
> index 77e2d920a6407..e28e41a011394 100644
> --- a/arch/x86/entry/calling.h
> +++ b/arch/x86/entry/calling.h
[ ... ]
> @@ -170,9 +172,74 @@ For 32-bit we have the following conventions - kernel is built with
>  	andq    $(~PTI_USER_PGTABLE_AND_PCID_MASK), \reg
>  .endm
>  
> +.macro TRACK_CR3_KERNEL_ENTRY
> +#ifdef CONFIG_TRACK_CR3
> +	STATIC_BRANCH_FALSE_LIKELY housekeeping_overridden, .Lend_\@
> +	orl $KERNEL_CR3_IN_KERNEL, PER_CPU_VAR(kernel_cr3_loaded)
> +.Lend_\@:
> +#endif // CONFIG_TRACK_CR3
> +.endm

[Severity: Medium]
Does gating these entry and exit macros on housekeeping_overridden enable
per-transition updates to kernel_cr3_loaded even when NO_HZ_FULL is not
active?

housekeeping_init() and housekeeping_update() enable housekeeping_overridden
whenever any housekeeping flag is set, including HK_FLAG_DOMAIN and
HK_FLAG_MANAGED_IRQ from isolcpus= or isolated cpuset partitions.  Since
kernel_cr3_loaded only serves HK_TYPE_KERNEL_NOISE (nohz_full) CPUs, gating
it on a boot-time static key specific to HK_FLAG_KERNEL_NOISE (such as
tick_nohz_full_running) would avoid paying the entry/exit RMW and store cost
on every CPU when only domain or managed_irq isolation is used.

> +
> +.macro TRACK_CR3_KERNEL_EXIT
> +#ifdef CONFIG_TRACK_CR3
> +	STATIC_BRANCH_FALSE_LIKELY housekeeping_overridden, .Lend_\@
> +	movl $KERNEL_CR3_IN_USER, PER_CPU_VAR(kernel_cr3_loaded)
> +.Lend_\@:
> +#endif // CONFIG_TRACK_CR3
> +.endm
> +
> +.macro PARANOID_TRACK_CR3_KERNEL_ENTRY scratch_reg:req save_reg:req
> +#ifdef CONFIG_TRACK_CR3
> +	STATIC_BRANCH_FALSE_LIKELY housekeeping_overridden, .Lend_\@
> +
> +	/*
> +	 * Due to FSGSBASE hilarity, we don't have a viable GS to rely on to
> +	 * do a classic percpu access here.
> +	 * Instead, fetch the percpu address via the CEA.
> +	 */
> +	subq $10, %rsp
> +	sgdt (%rsp)
> +	movq 2(%rsp), \scratch_reg /* GDT address */
> +	addq $10, %rsp
> +
> +	/*
> +	 * We may have just interrupted another entry that has set
> +	 * $KERNEL_CR3_IN_KERNEL, but hasn't written to CR3 yet.
> +	 * Abuse \save_reg to remember that at exit and to leave
> +	 * @kernel_cr3_loaded as it was.
> +	 */
> +	testl $KERNEL_CR3_IN_KERNEL, CPU_ENTRY_AREA_kernel_cr3(\scratch_reg)
> +	jz .Ltrack_\@
> +	bts $X86_CR3_REENTRY_BIT, \save_reg
> +	jmp .Lend_restore_\@
> +.Ltrack_\@:
> +	movl $KERNEL_CR3_IN_KERNEL, CPU_ENTRY_AREA_kernel_cr3(\scratch_reg)
> +.Lend_restore_\@:
> +	/* CEA hackery done; restore @scratch_reg */
> +	mov \save_reg, \scratch_reg
> +	btr $X86_CR3_REENTRY_BIT, \scratch_reg
> +.Lend_\@:
> +#endif // CONFIG_TRACK_CR3
> +.endm
> +
> +.macro PARANOID_TRACK_CR3_KERNEL_EXIT save_reg:req
> +#ifdef CONFIG_TRACK_CR3
> +	STATIC_BRANCH_FALSE_LIKELY housekeeping_overridden, .Lend_\@
> +	/*
> +	 * If we entered with @kernel_cr3_loaded already written to, leave it
> +	 * as is.
> +	 */
> +	btr $X86_CR3_REENTRY_BIT, \save_reg
> +	jc .Lend_\@
> +	movl $KERNEL_CR3_IN_USER, PER_CPU_VAR(kernel_cr3_loaded)
> +.Lend_\@:
> +#endif // CONFIG_TRACK_CR3
> +.endm

[Severity: Medium]
Can placing a hardware data watchpoint on per_cpu(kernel_cr3_loaded, cpu)
cause infinite #DB recursion on the #DB IST stack?

Unlike PARANOID_TRACK_CR3_KERNEL_ENTRY, which writes through the
CPU_ENTRY_AREA alias, TRACK_CR3_KERNEL_ENTRY, TRACK_CR3_KERNEL_EXIT, and
PARANOID_TRACK_CR3_KERNEL_EXIT write directly to
PER_CPU_VAR(kernel_cr3_loaded).  Because within_cpu_entry() in
arch/x86/kernel/hw_breakpoint.c does not reject breakpoints on
per_cpu(kernel_cr3_loaded, cpu), exc_debug_kernel() restores DR7 before
returning to paranoid_exit -> PARANOID_RESTORE_CR3 ->
PARANOID_TRACK_CR3_KERNEL_EXIT while still on DB_stack, where writing
kernel_cr3_loaded immediately re-triggers #DB (and in entry_SYSCALL_64,
TRACK_CR3_KERNEL_ENTRY fires #DB while %rsp holds %cr3).

> +
>  .macro SWITCH_TO_KERNEL_CR3 scratch_reg:req
>  	ALTERNATIVE "jmp .Lend_\@", "", X86_FEATURE_PTI
>  	mov	%cr3, \scratch_reg
> +	TRACK_CR3_KERNEL_ENTRY
>  	ADJUST_KERNEL_CR3 \scratch_reg
>  	mov	\scratch_reg, %cr3
>  .Lend_\@:
> @@ -182,6 +249,7 @@ For 32-bit we have the following conventions - kernel is built with
>  	PER_CPU_VAR(cpu_tlbstate + TLB_STATE_user_pcid_flush_mask)
>  
>  .macro SWITCH_TO_USER_CR3 scratch_reg:req scratch_reg2:req
> +	TRACK_CR3_KERNEL_EXIT
>  	mov	%cr3, \scratch_reg
>  
>  	ALTERNATIVE "jmp .Lwrcr3_\@", "", X86_FEATURE_PCID
> @@ -239,6 +307,7 @@ For 32-bit we have the following conventions - kernel is built with
>  	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
>  
> @@ -257,6 +326,8 @@ For 32-bit we have the following conventions - kernel is built with
>  	bt	$PTI_USER_PGTABLE_BIT, \save_reg
>  	jnc	.Lend_\@
>  
> +	PARANOID_TRACK_CR3_KERNEL_EXIT \save_reg
> +
>  	ALTERNATIVE "jmp .Lwrcr3_\@", "", X86_FEATURE_PCID

[Severity: Medium]
Can an NMI or paranoid exception arriving during the CR3 exit window run
its entire C handler with kernel_cr3_loaded set to KERNEL_CR3_IN_USER?

  SWITCH_TO_USER_CR3 (or PARANOID_RESTORE_CR3)
    TRACK_CR3_KERNEL_EXIT
      kernel_cr3_loaded = KERNEL_CR3_IN_USER
    <NMI before mov %cr3>
      paranoid_entry
        SAVE_AND_SWITCH_TO_KERNEL_CR3
          movq %cr3, \scratch_reg
          bt $PTI_USER_PGTABLE_BIT, \scratch_reg
          jnc .Ldone  <-- taken (%cr3 still has kernel CR3)
      exc_nmi()  <-- runs in kernel with kernel_cr3_loaded == IN_USER

In both SWITCH_TO_USER_CR3 and PARANOID_RESTORE_CR3, kernel_cr3_loaded is
cleared to KERNEL_CR3_IN_USER before the PCID flush check and before the
final write to %cr3.  If an NMI or IST exception fires in that window,
%cr3 still has PTI_USER_PGTABLE_BIT clear, so SAVE_AND_SWITCH_TO_KERNEL_CR3
jumps to .Ldone without running PARANOID_TRACK_CR3_KERNEL_ENTRY.  Throughout
the handler, remote CPUs checking kernel_cr3_loaded will see the CPU as in
userspace and skip text-poke serialization IPIs while it executes kernel
code without a subsequent serializing CR3 write on paranoid_exit.

> diff --git a/kernel/sched/isolation.c b/kernel/sched/isolation.c
> index 156025ef81b75..90640c9e7f811 100644
> --- a/kernel/sched/isolation.c
> +++ b/kernel/sched/isolation.c
> @@ -19,7 +19,14 @@ enum hk_flags {
>  	HK_FLAG_KERNEL_NOISE	= BIT(HK_TYPE_KERNEL_NOISE),
>  };
>  
> -DEFINE_STATIC_KEY_FALSE(housekeeping_overridden);
> +/*
> + * This key is accessed in early entry code to drive the CPU isolation IPI
> + * quiescing mechanism. This happens before a serializing instruction has been
> + * executed in the entry path. This is acceptable as a stale value will only be
> + * observed during transient CPU isolation states, and the updated value will be
> + * acted upon at the next kernel exit.
> + */
> +DEFINE_STATIC_KEY_FALSE_ENTRY(housekeeping_overridden);
>  EXPORT_SYMBOL_GPL(housekeeping_overridden);

[Severity: High]
Can enabling housekeeping_overridden at runtime via housekeeping_update()
crash a CPU executing early entry/exit assembly when text poking places an
INT3 on the static branch?

When a system boots without nohz_full= or isolcpus= and an isolated cpuset
partition is later created at runtime, housekeeping_update() calls
static_branch_enable(&housekeeping_overridden):

kernel/sched/isolation.c:housekeeping_update() {
    ...
	if (!housekeeping.flags)
		static_branch_enable(&housekeeping_overridden);
    ...
}

On x86 SMP, static_branch_enable() patches each
STATIC_BRANCH_FALSE_LIKELY housekeeping_overridden site using an INT3
breakpoint and broadcasts a sync IPI.  Unlike #DB or #NMI, X86_TRAP_BP
(asm_exc_int3) uses DEFAULT_STACK and error_entry; if a CPU hits the INT3 at
CPL 0 inside SWITCH_TO_KERNEL_CR3 (where entry_SYSCALL_64 and
entry_SYSCALL_compat have just executed mov %cr3, %rsp and user page tables
are still loaded), SWITCH_TO_USER_CR3, or SAVE_AND_SWITCH_TO_KERNEL_CR3, the
CPU takes #BP with user CR3, user GSBASE, or %rsp holding %cr3, immediately
double-faulting.  In addition, if PARANOID_TRACK_CR3_KERNEL_ENTRY runs after
its jump is patched (setting X86_CR3_REENTRY_BIT in save_reg because
kernel_cr3_loaded was initialized to 1 at boot) while
PARANOID_TRACK_CR3_KERNEL_EXIT still takes its unpatched jump to .Lend,
btr $X86_CR3_REENTRY_BIT, \save_reg is skipped and PARANOID_RESTORE_CR3
writes reserved bit 60 into %cr3, raising #GP(0).

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261127201221.434876-1-vschneid@redhat.com?part=9


  reply	other threads:[~2026-11-28 20:15 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-27 20:12 [PATCH v10 00/10] x86: Quiesce text_poke() IPIs Valentin Schneider
2026-11-27 20:12 ` [PATCH v10 01/10] objtool: Make validate_call() recognize indirect calls to pv_ops[] Valentin Schneider
2026-11-28 20:15   ` sashiko-bot
2026-11-27 20:12 ` [PATCH v10 02/10] objtool: Flesh out warning related to pv_ops[] calls Valentin Schneider
2026-11-27 20:12 ` [PATCH v10 03/10] objtool: Always pass a section to validate_unwind_hints() Valentin Schneider
2026-11-28 20:15   ` sashiko-bot
2026-11-27 20:12 ` [PATCH v10 04/10] x86/retpoline: Make warn_thunk_thunk .noinstr Valentin Schneider
2026-11-27 20:12 ` [PATCH v10 05/10] jump_label: Add annotations for validating .entry.text key usage Valentin Schneider
2026-11-27 20:12 ` [PATCH v10 06/10] objtool: Add .entry.text validation for static branches Valentin Schneider
2026-11-28 20:15   ` sashiko-bot
2026-11-27 20:12 ` [PATCH v10 07/10] x86/jump_label: Add ASM support for static_branch_likely() Valentin Schneider
2026-11-27 20:12 ` [PATCH v10 08/10] x86/mm/pti: Introduce kernel_cr3_loaded Valentin Schneider
2026-11-28 20:15   ` sashiko-bot
2026-11-27 20:12 ` [PATCH v10 09/10] x86/mm/pti: Drive kernel_cr3_loaded Valentin Schneider
2026-11-28 20:15   ` sashiko-bot [this message]
2026-11-27 20:12 ` [PATCH v10 10/10] context_tracking,x86: Quiesce kernel text patching IPIs when tracking CR3 switches Valentin Schneider
2026-11-28 20:15   ` 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-239014@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=Dan.Beimborn@qube-rt.com \
    --cc=acme@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=ardb@kernel.org \
    --cc=arnd@arndb.de \
    --cc=boqun.feng@gmail.com \
    --cc=bp@alien8.de \
    --cc=dan.carpenter@linaro.org \
    --cc=dave.hansen@linux.intel.com \
    --cc=davem@davemloft.net \
    --cc=dwagner@suse.de \
    --cc=frederic@kernel.org \
    --cc=hpa@zytor.com \
    --cc=jannh@google.com \
    --cc=jbaron@akamai.com \
    --cc=joelagnelf@nvidia.com \
    --cc=josh@joshtriplett.org \
    --cc=jpoimboe@kernel.org \
    --cc=juri.lelli@redhat.com \
    --cc=linux-mm@kvack.org \
    --cc=luto@kernel.org \
    --cc=masahiroy@kernel.org \
    --cc=mathieu.desnoyers@efficios.com \
    --cc=mgorman@suse.de \
    --cc=mingo@redhat.com \
    --cc=mtosatti@redhat.com \
    --cc=neeraj.upadhyay@kernel.org \
    --cc=oleg@redhat.com \
    --cc=pak-hung.chiu@qube-rt.com \
    --cc=paulmck@kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=peterz@infradead.org \
    --cc=ptesarik@suse.com \
    --cc=riel@surriel.com \
    --cc=rostedt@goodmis.org \
    --cc=samitolvanen@google.com \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=shenhan@google.com \
    --cc=sshegde@linux.ibm.com \
    --cc=tglozar@redhat.com \
    --cc=tglx@linutronix.de \
    --cc=urezki@gmail.com \
    --cc=vschneid@redhat.com \
    --cc=williams@redhat.com \
    --cc=x86@kernel.org \
    --cc=ypodemsk@redhat.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