Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v10 00/10] x86: Quiesce text_poke() IPIs
@ 2026-11-27 20:12 Valentin Schneider
  2026-11-27 20:12 ` [PATCH v10 01/10] objtool: Make validate_call() recognize indirect calls to pv_ops[] Valentin Schneider
                   ` (9 more replies)
  0 siblings, 10 replies; 11+ messages in thread
From: Valentin Schneider @ 2026-11-27 20:12 UTC (permalink / raw)
  To: linux-kernel, linux-mm, x86
  Cc: Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen,
	H. Peter Anvin, Andy Lutomirski, Peter Zijlstra,
	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

Christmas comes early this year, here's v10!

Context
=======

We've observed within Red Hat that isolated, NOHZ_FULL CPUs running a
pure-userspace application get regularly interrupted by IPIs sent from
housekeeping CPUs. Those IPIs are caused by activity on the housekeeping CPUs
leading to various on_each_cpu() calls; this series is tackling the ones coming
from

  smp_text_poke_*()
  `\
    smp_text_poke_sync_each_cpu()

In the wild we mostly see these from static key updates and BPF JIT'ing. All
sorts of kernel text updates can trigger them:

  $ global -v --result=grep --color=always --path-style=shorter --from-here=3136:arch/x86/kernel/alternative.c -- smp_text_poke_single
  arch/x86/include/asm/text-patching.h:47:extern void smp_text_poke_single(void *addr, const void *opcode, size_t len, const void *emulate);
  arch/x86/kernel/ftrace.c:194:	smp_text_poke_single((void *)ip, new, MCOUNT_INSN_SIZE, NULL);
  arch/x86/kernel/ftrace.c:198:	smp_text_poke_single((void *)ip, new, MCOUNT_INSN_SIZE, NULL);
  arch/x86/kernel/ftrace.c:507:	smp_text_poke_single((void *)ip, new, MCOUNT_INSN_SIZE, NULL);
  arch/x86/kernel/ftrace.c:601:	smp_text_poke_single((void *)ip, new, MCOUNT_INSN_SIZE, NULL);
  arch/x86/kernel/jump_label.c:105:	smp_text_poke_single((void *)jump_entry_code(entry), jlp.code, jlp.size, NULL);
  arch/x86/kernel/kprobes/opt.c:486:		smp_text_poke_single(op->kp.addr, insn_buff, JMP32_INSN_SIZE, NULL);
  arch/x86/kernel/static_call.c:142:	smp_text_poke_single(insn, code, size, emulate);
  arch/x86/net/bpf_jit_comp.c:657:		smp_text_poke_single(ip, new_insn, X86_PATCH_SIZE, NULL);    

  global -v --result=grep --color=always --path-style=shorter --from-here=2846:arch/x86/kernel/alternative.c -- smp_text_poke_batch_finish
  arch/x86/include/asm/text-patching.h:50:extern void smp_text_poke_batch_finish(void);
  arch/x86/kernel/alternative.c:3120:		smp_text_poke_batch_finish();
  arch/x86/kernel/alternative.c:3139:	smp_text_poke_batch_finish();
  arch/x86/kernel/ftrace.c:61:	smp_text_poke_batch_finish();
  arch/x86/kernel/ftrace.c:258:	smp_text_poke_batch_finish();
  arch/x86/kernel/jump_label.c:146:	smp_text_poke_batch_finish();
  
Approach
========

The heart of this series is the thought that while we cannot remove NOHZ_FULL
CPUs from the list of CPUs targeted by these IPIs, they may not have to execute
the callbacks immediately. Anything that only affects kernelspace can wait
until the next user->kernel transition, providing it can be executed "early
enough" in the entry code as a postponed operation.

The original implementation is from Peter [1]. Nicolas then added kernel TLB
invalidation deferral to that [2], and I picked it up from there.

Previous submissions included mitigations for both text_poke() and
vmalloc()/vfree(), this time around it's just text_poke() - I have updated code
for vmalloc()/vfree() but it's really fugly, and the text_poke() stuff is more
pressing. Also, that's enough assembly faff for one patchset.

text_poke() requires executing a serializing instruction on all CPUs, thus its
postponed operation must be a serializing instruction. Turns out we're in luck,
as the kPTI CR3 RMW is just that:

  SDM vol2 chapter 4.3 - Move to/from control registers:
  ```
  MOV CR* instructions, except for MOV CR8, are serializing instructions.
  ```
That means we don't need to do anything extra on kernel entry to handle quiesced
sync_core() IPIs coming from text_poke() (other than leverage kPTI).
  
Kernel entry vs execution of the postponed operation
====================================================

This is what I've referred to as the "Danger Zone" during my LPC24 talk [4].

There is a non-zero length of code that is executed upon kernel entry before the
postponed operation can be itself executed (before we switch to the kernel CR3),
i.e.:

  idtentry
    idtentry_body
      error_entry
        SWITCH_TO_KERNEL_CR3

This danger zone used to be much wider in v7 and earlier (from kernel entry all
the way down to ct_kernel_enter_state()). The objtool instrumentation thus now
targets .entry.text rather than .noinstr as a whole.

kPTI woes
=========

I only learned today that even with kPTI enabled in the Kconfig, a lot of
hardware does not actually require PTI and disables it at boot. You can imagine
my reaction to this if you look at the amount of faffing I've done in the kPTI
SITCH_TO_*_CR3 code.

Buuut there may still be some value to it - I'm using the text_poke() stuff as a
foundation for the tlb_flush quiescing, and that *does* require *at least* a CR3
RMW. Again for that I was relying on kPTI limiting the global pages to not have
to add a pure ASM CR4 sequence. So I should be able to reuse a lot of that code,
but that puts me in a bad spot where I'm submitting quite intrusive code for a
feature that isn't even in the patchset...

So yeah, thoughts welcome here!

Testing
=======

(May require pti=on depending on your hardware setup)

QEMU
++++

Tested with GDB single-stepping to check the ASM static key behaviour.
Used the 'nmi' command to trigger the NMI entry paths, although I couldn't get
NMI nesting to happen.

Real hardware
+++++++++++++

Tested on a dual-socket Intel(R) Xeon(R) CPU E5-2699 system with SMToff,
NOHZ_FULL, 26 isolated CPUs. RHEL10 userspace.

BPF kselftests, the fentry one:

```
cleanup () { kill $(jobs -p); echo on > /sys/devices/system/cpu/smt/control; }
trap "cleanup" INT

echo off > /sys/devices/system/cpu/smt/control

ISOL_CPUS=5-17,23-35
ISOL_CPUS_ARRAY=(5 6 7 8 9 10 11 12 13 14 15 16 17 23 24 25 26 27 28 29 30 31 32 33 34 35)

for cpu in ${ISOL_CPUS_ARRAY[@]}; do
    echo $cpu
    taskset -c $cpu bash -c "while true; do continue; done" &
    chrt -f -p 1 $!
done

trace-cmd record -e "csd_queue_cpu" -f "cpu & CPUS{$ISOL_CPUS}" \
		-e "ipi_send_cpumask" -f "cpumask & CPUS{$ISOL_CPUS}" \
		-e "ipi_send_cpu"     -f "cpu & CPUS{$ISOL_CPUS}" \
		./test_progs -t fentry_test

cleanup
wait
```

*Most* of the smp_text_poke_batch_finish() IPIs are gone with the patches, the
one or two that remain are due to other, preceding IPIs forcing the isolated
CPUs out of userspace, making the quiescing condition fail.

Running rteval I mostly get flooded with =flush_tlb_kernel_range()= IPIs, which
season 2 of this should take care of.
		  
Patches
=======

o Patches 1-4 are standalone objtool cleanups.

o Patches 5-6 add infrastructure for annotating static keys that may be used in
  entry code (courtesy of Josh). 

o Patch 7 adds ASM support for static keys

o Patches 8-10 add the deferral mechanism.

Patches are also available at:
https://gitlab.com/vschneid/linux.git -b redhat/isolirq/defer/v10

Acknowledgements
================

Special thanks to:
o Clark Williams for listening to my ramblings about this and throwing ideas my way
o Josh Poimboeuf for all his help with everything objtool-related
o Dave Hansen for patiently educating me about mm
o All of the folks who attended various (too many?) talks about this and
  provided precious feedback.  

Links
=====

[1]: https://lore.kernel.org/all/20210929151723.162004989@infradead.org/
[2]: https://github.com/vianpl/linux.git -b ct-work-defer-wip
[3]: https://lpc.events/event/19/contributions/2219/
[4]: https://lpc.events/event/18/contributions/1889/

Revisions
=========

v9 -> v10
+++++++++

o Rebased onto v7.3-rc4
o Overall renaming of "IPI deferral" to "IPI quiescing" (nikbor)

o Now handles NMI entry!
o Ditched the flush_tlb_kernel_range() stuff this time again, for now


v8 -> v9
++++++++

o Rebased onto v7.1-rc1-ish (507bd4b66c85)

o Caught up to housekeeping_overridden now being dynamic; revived alternative.c
  code that deals with static keys in early entry code (Shrikanth)

o Messed about ordering of @kernel_cr3_loaded accesses (Frederic)  

v7 -> v8
++++++++

o Rebased onto v6.19

o Fixed objtool --uaccess validation preventing --noinstr validation of
  unwind hints
o Added more objtool --noinstr warning fixes
o Reduced objtool noinstr static key validation to just .entry.text

o I've kept the "simple" __ro_after_init conversions 

o Moved the kernel_cr3_loaded signal update to before writing to CR3

o Ditched context_tracking based deferral
o Ditched the (additionnal) unconditionnal TLB flush upon kernel entry

v6 -> v7
++++++++

o Rebased onto latest v6.18-rc5 (6fa9041b7177f)
o Collected Acks (Sean, Frederic)

o Fixed <asm/context_tracking_work.h> include (Shrikanth)
o Fixed ct_set_cpu_work() CT_RCU_WATCHING logic (Frederic)

o Wrote more verbose comments about NOINSTR static keys and calls (Petr)

o [NEW PATCH] Instrumented one more static key: cpu_bf_vm_clear
o [NEW PATCH] added ASM-accessible static key helpers to gate NO_HZ_FULL logic
  in early entry code (Frederic)

v5 -> v6
++++++++

o Rebased onto v6.17
o Small conflict fixes with cpu_buf_idle_clear smp_text_poke() renaming

o Added the TLB flush craziness

v4 -> v5
++++++++

o Rebased onto v6.15-rc3
o Collected Reviewed-by

o Annotated a few more static keys
o Added proper checking of noinstr sections that are in loadable code such as
  KVM early entry (Sean Christopherson)

o Switched to checking for CT_RCU_WATCHING instead of CT_STATE_KERNEL or
  CT_STATE_IDLE, which means deferral is now behaving sanely for IRQ/NMI
  entry from idle (thanks to Frederic!)

o Ditched the vmap TLB flush deferral (for now)  
  

RFCv3 -> v4
+++++++++++

o Rebased onto v6.13-rc6

o New objtool patches from Josh
o More .noinstr static key/call patches
o Static calls now handled as well (again thanks to Josh)

o Fixed clearing the work bits on kernel exit
o Messed with IRQ hitting an idle CPU vs context tracking
o Various comment and naming cleanups

o Made RCU_DYNTICKS_TORTURE depend on !COMPILE_TEST (PeterZ)
o Fixed the CT_STATE_KERNEL check when setting a deferred work (Frederic)
o Cleaned up the __flush_tlb_all() mess thanks to PeterZ

RFCv2 -> RFCv3
++++++++++++++

o Rebased onto v6.12-rc6

o Added objtool documentation for the new warning (Josh)
o Added low-size RCU watching counter to TREE04 torture scenario (Paul)
o Added FORCEFUL jump label and static key types
o Added noinstr-compliant helpers for tlb flush deferral


RFCv1 -> RFCv2
++++++++++++++

o Rebased onto v6.5-rc1

o Updated the trace filter patches (Steven)

o Fixed __ro_after_init keys used in modules (Peter)
o Dropped the extra context_tracking atomic, squashed the new bits in the
  existing .state field (Peter, Frederic)
  
o Added an RCU_EXPERT config for the RCU dynticks counter size, and added an
  rcutorture case for a low-size counter (Paul) 

o Fixed flush_tlb_kernel_range_deferrable() definition

Josh Poimboeuf (2):
  jump_label: Add annotations for validating .entry.text key usage
  objtool: Add .entry.text validation for static branches

Valentin Schneider (8):
  objtool: Make validate_call() recognize indirect calls to pv_ops[]
  objtool: Flesh out warning related to pv_ops[] calls
  objtool: Always pass a section to validate_unwind_hints()
  x86/retpoline: Make warn_thunk_thunk .noinstr
  x86/jump_label: Add ASM support for static_branch_likely()
  x86/mm/pti: Introduce kernel_cr3_loaded
  x86/mm/pti: Drive kernel_cr3_loaded
  context_tracking,x86: Quiesce kernel text patching IPIs when tracking
    CR3 switches

 arch/x86/Kconfig                            |  14 +++
 arch/x86/entry/calling.h                    |  71 +++++++++++++
 arch/x86/entry/entry.S                      |   3 +-
 arch/x86/include/asm/cpu_entry_area.h       |   4 +
 arch/x86/include/asm/jump_label.h           |  34 +++++-
 arch/x86/include/asm/pti.h                  |  11 ++
 arch/x86/include/asm/text-patching.h        |   5 +
 arch/x86/include/uapi/asm/processor-flags.h |   3 +
 arch/x86/kernel/alternative.c               |  59 +++++++++--
 arch/x86/kernel/asm-offsets.c               |   3 +
 arch/x86/kernel/cpu/bugs.c                  |   2 +-
 arch/x86/kernel/kprobes/core.c              |   4 +-
 arch/x86/kernel/kprobes/opt.c               |   4 +-
 arch/x86/kernel/module.c                    |   2 +-
 arch/x86/mm/cpu_entry_area.c                |   5 +
 arch/x86/mm/pti.c                           |  40 ++++---
 include/asm-generic/sections.h              |  14 +++
 include/linux/jump_label.h                  |  27 ++++-
 include/linux/objtool.h                     |  16 +++
 kernel/sched/isolation.c                    |   9 +-
 tools/objtool/Documentation/objtool.txt     |  15 +++
 tools/objtool/check.c                       | 111 +++++++++++++++++---
 tools/objtool/include/objtool/check.h       |   2 +
 tools/objtool/include/objtool/elf.h         |   3 +-
 tools/objtool/include/objtool/special.h     |   1 +
 tools/objtool/special.c                     |  15 ++-
 26 files changed, 428 insertions(+), 49 deletions(-)

--
2.56.1



^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2026-11-27 20:14 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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-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-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-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-27 20:12 ` [PATCH v10 09/10] x86/mm/pti: Drive kernel_cr3_loaded Valentin Schneider
2026-11-27 20:12 ` [PATCH v10 10/10] context_tracking,x86: Quiesce kernel text patching IPIs when tracking CR3 switches Valentin Schneider

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox