[PATCH v2 0/3] sched/preempt: Check notrace preempt-count updates

From: Karl Mehltretter

Date: Wed Nov 25 2026 - 21:31:16 EST


With CONFIG_DEBUG_PREEMPT, the notrace preemption macros bypass the
underflow and near-overflow checks used by ordinary operations. Add
checked noinstr updates for their existing callers and share validation
with preempt_count_add() and preempt_count_sub().

Patches 1 and 2 mark the arm64 raw preempt-count and reschedule helpers
__always_inline, matching x86, s390 and asm-generic, so noinstr callers
keep them in their own section.

Patch 3 also disables branch profiling with DEBUG_PREEMPT to prevent
instrumentation from reaching noinstr text, covering both
PROFILE_ANNOTATED_BRANCHES and PROFILE_ALL_BRANCHES.

Patch 1 is extracted from Hongyan Xia's entry RFC. Song Liu also posted
notrace annotations for the same arm64 helpers:

https://lore.kernel.org/r/20261023011049.1164625-10-song@xxxxxxxxxx

Changes in v2:
- Patches 1 and 2: no code changes, added Hongyan Xia's Reviewed-by.
- Patch 3: replace the Makefile CFLAGS override with a !DEBUG_PREEMPT
dependency on the Branch Profiling Kconfig choice (Steven Rostedt).
- Patch 3: remove the unused __preempt_count_inc() and
__preempt_count_dec() macros and clarify the changelog wording
(Hongyan Xia).

Link to v1:

https://lore.kernel.org/r/cover.1794814699.git.kmehltretter@xxxxxxxxx

This series is independently useful and also provides the checked count
updates planned for follow-up IRQ-entry work. It does not change IRQ
entry, trace-recursion lifetime or TRACE_CTX_TRANSITION.

This addresses Peter's suggestion:

https://lore.kernel.org/r/20261029093219.GH1779021@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

The series is based on Linus's tree at commit 4dce97c6c265
("Merge tag 'ext4_for_linus-7.4-rc5' of
git://git.kernel.org/pub/scm/linux/kernel/git/tytso/ext4", after
v7.4-rc4).

Testing:

- Kconfig: x86-64, arm64 and arm builds with Clang 22.1.8 verified both
branch-profiling modes, DEBUG_PREEMPT and non-debug configurations,
!CONFIG_PREEMPTION with PREEMPT_COUNT=y, plus allmodconfig,
allyesconfig and defconfig + debug.config.
- arm64 v2 runtime: Clang 22.1.8, W=1, PREEMPT, DEBUG_PREEMPT,
PROVE_LOCKING, FTRACE and PREEMPT_TRACER. Full Image and external
module builds passed, .noinstr.text disassembly showed no
ftrace_likely_update() or outlined preempt-count calls, and QEMU
boots verified balanced load/unload, underflow rejection and
near-overflow detection at 0xf5.
- Additional v1 testing (C code in arch/arm64/include/asm/preempt.h and
kernel/sched/core.c unchanged): x86-64 Clang 19 objtool noinstr
validation and nested-KVM runs, arm64 GCC 14.2 PREEMPT_RT and
allmodconfig builds plus native Axion boot, and ARC, C-SKY and MIPS
builds and QEMU tests.

I used an LLM to help extract and review the changes, write the test
harnesses and prepare the commit messages.

Hongyan Xia (1):
arm64: Always inline raw preempt count updates

Karl Mehltretter (2):
arm64: Always inline preempt count and reschedule checks
sched/preempt: Check preempt counts in notrace helpers

arch/arm64/include/asm/preempt.h | 10 ++++----
include/linux/preempt.h | 23 +++++++++++++-----
kernel/sched/core.c | 40 ++++++++++++++++++++++++++++----
kernel/trace/Kconfig | 1 +
4 files changed, 59 insertions(+), 15 deletions(-)


base-commit: 4dce97c6c265544342e716e62fd24bbb232590c5
--
2.39.5 (Apple Git-154)