Exact context tracking (Re: [PATCH v10 00/10] x86: Quiesce text_poke() IPIs)
From: Andy Lutomirski
Date: Fri Dec 04 2026 - 00:11:56 EST
[long email here...]
On Wed, Dec 2, 2026, at 9:08 AM, Andy Lutomirski wrote:
>
> I think this entire sequence [EQS enter + exit] will be cheap enough that we should
> seriously consider enabling it unconditionally. Or *maybe* do it
> heuristically where we only quiesce in the first place if we think it
> makes sense, but I think it should at least be compiled into the kernel
> unconditionally.
>
> If, on the other hand, we don't unify it with RCU, then we're starting
> to multiply the cost by a factor of at least two, and maybe we want it
> to stay conditional. Yuck.
>
> FWIW, this code can (I think) be fully shared with the RCU idle EQS
> code, so long as we can make the same promises about vmap in the idle
> path as we would in the userspace path. That should be possible too
> unless we have truly stupid driver code, and that's where objtool would
> come in, I think.
>
I dug in some more.
Let's start this with a big warning, which I should have remembered earlier since the last time we screwed this up, I was the one who did it!
WE CANNOT DEFER IPIS WHEN FREEING A PAGETABLE. It's not a code problem -- it's a fundamental architectural issue and it is incorrect for us to make this optimization:
https://lore.kernel.org/all/CA+55aFwfOavB9EEnhoXh95wRfCd3uC0SX_Th6s_38jZxQLKDLg@xxxxxxxxxxxxxx/
And let's keep in mind that we used to have a mechanism for queueing deferred work to be done on EQS exit:
https://lore.kernel.org/lkml/CALCETrWNPOOdTrFabTDd=H7+wc6xJ9rJceg6OL1S0rTV5pfSsA@xxxxxxxxxxxxxx/
But we also do keep track of whether we're freeing a pagetable, and we already flushes when not freeing pagetables for idle CPUs, except that we haven't actually wired it up for kernel flushes:
commit 145f573b89a62bf53cfc0144fa9b1c56b0f70b45
Author: Rik van Riel <riel@xxxxxxxxxxx>
Date: Tue Sep 25 23:58:44 2018 -0400
x86/mm/tlb: Make lazy TLB mode lazier
So all this discussion of whether it's safe to run, say, the idle code while we may have stale TLB entries is 8 years out of date. We already do!
As for EQS, everyone seems to think that EQS is expensive, and we use it in idle where latency doesn't matter so much, and we don't use it for usermode except in NOHZ_FULL. And little speedups to idle entry/exit won't show up in benchmarks (and no one really cares that much anyway), and NOHZ_FULL is only used by users who don't care about user->kernel->user latency. So there's a chicken and egg problem here.
And there has been discussion of making EQS faster by weakening the ordering requirements:
https://lore.kernel.org/all/20210728173715.GA9416@paulmck-ThinkPad-P17-Gen-1/
Sadly that never went anywhere.
I think we can go farther than that weakening proposal I linked above, but only if we go all in. So here's what I think we should do.
We introduce a new mode, selected at compile time (probably as an arch opt-in thing) called EXACT_CONTEXT_TRACKING. In this mode:
1. The API becomes ct_enter_eqs() and ct_exit_eqs(). Callers are responsible for never entering EQS when already in EQS and never exiting EQS when not in EQS. Running noinstr code in EQS is illegal.
2. We make a rule: when a CPU is not in EQS, neither remote CPUs nor NMI may modify context_tracking.state. When a CPU is in EQS, neither remote CPUs nor NMI may cause that cpu to stop being in an EQS (except that NMI may exit and reenter EQS). (MCE counts as NMI for this purpose.) As a partial exception, remote CPUs may modify context_tracking.state if the target CPU is somehow locked (e.g. hotplug).
3. The rules above imply that NMI must do, effectively:
bool was_eqs = ct_in_eqs();
if (was_eqs)
ct_exit_eqs();
do actual work;
if (was_eqs)
ct_enter_eqs();
4. The nesting and nmi_nesting fields go away. Nothing needs then in EXACT_CONTEXT_TRACKING mode. (This interferes with rcu_is_cpu_rrupt_from_idle(). Maybe we can just remove that, but maybe we can keep track of whether we interrupted an EQS and tell RCU about it.)
5. We do the weakening thing. ct_enter_eqs() becomes a plain read followed by a store-release. Note that, because of the rule 2, nothing else will modify context_tracking.state and thus cause a malfunction.
6. We change ordinary exit to usermode to enter EQS, unconditionally. We consider making it conditional only if it ends up being a performance problem. (It would be a performance problem if entering EQS always turns off the scheduler tick, so that probably needs to remain conditional. I have not dug in to how coupled the dynticks code is to the EQS code.)
And then we DO NOT ADD DEFERRED WORK. Instead we pause, review the code, let it soak in -next, maybe even hit a real stable release, and catch our breaths.
----------
And now we add deferred work for text_poke because it's simpler than TLB flushes. text_pokes to anything other than noinstr code can be deferred, I think (on the theory that, in the new regime, we will never execute non-noinstr code while in EQS). To comply with rule 2, remote CPUs setting the bit need to use CAS and not do it if the target is not in EQS. But we need to be very careful: for NMI safety, we might need to leave the deferred work bit set until *after* the serialize so that an NMI that hits between the CAS and the serialize can notice the deferred work bit and do an extra serialize. So ct_exit_eqs() does *not* clear the deferred bit. Instead the various non-NMI-like kernel entry paths do something like:
bool was_eqs = ct_in_eqs();
if (was_eqs) {
ct_exit_eqs();
ct_do_and_clear_deferred_work();
}
(keeping in mind that enter-from-usermode, which is very hot, can just assume that usermode was always EQS and can skip the ct_in_eqs() part.)
and the NMI-like ones do:
bool was_eqs = ct_in_eqs();
if (was_eqs) {
ct_exit_eqs();
}
if (context_tracking.state & CT_DEFERRED_WORK) {
do the deferred work;
// DO NOT CLEAR THE DEFERRED BIT -- see rule 2.
// (this means we might serialize twice, which is fine.)
}
(This is totally irrelevant on non-FRED where NMI delivery is a serializing operation (very very clearly so on AMD). But FRED does not obviously document it as serializing.)
HOWEVER!
There is more to the text_poke machinery's IPI than just the serialization. It also synchronizes against smp_text_poke_int3_handler(). But now we come full circle: we don't defer IPIs if we're poking noinstr text, and if we're poking non-noinstr text, we do:
if (do_sync) {
smp_text_poke_sync_each_cpu();
if (we deferred anything)
synchronize_rcu(); // <--- this!
}
/*
* Remove and wait for refs to be zero.
Because we've already defined non-noinstr code as being an RCU-watching state, we know that synchronize_rcu() will wait for it. Or, if this is too slow, we come up with some other clever scheme -- maybe we leave the text_poke descriptor in place until an RCU cycle and we only forcibly synchronize if we try to poke that address a second time. Or we make the int3 handler recover by reading the text to see if it changed. There are lots of solutions but I haven't found a trivial one.
----------
And then, if anyone is still feeling ambitious, we find and update every single code path that frees a kernel page table and all the kernel TLB flush APIs to track freed_tables just like we do for user mode. And we audit the idle driver code and possibly update objtool to help out. And we carefully set up a race-free way to do a non-freed-table TLB flush to a remote CPU that can be deferred (and not forgotten in the NMI path) and we wire it up through the EQS deferral mechanism.
----------
Or we decide that Intel needs to catch up to AMD and that INVLPGB is the future and we don't care about TLB flush deferral or about text_poke serialize deferral. I still think that INVLPGB + TLBSYNC (on the executable mapping, not the temporary text_poke mapping) is adequate for text_poke *if we handle the synchronization with int3 some other way, e.g. using RCU*. Even if we decide this, I still think the EXACT_CONTEXT_TRACKING thing is worthwhile.
(I seem to have convinced ChatGPT 6 Pro that I'm right about INVLPGB + TLBSYNC.)
Anyone want to try this? Valentin? This is a big project. Also, Valentin, there is a lot in here that needs to be dealt with one way or another in any scheme that defers kernel TLB flushes or text_poke serialization.
--Andy