entry: Classify very early hardirq entry and return-tail callbacks
Subsystems (10)
v2 cover entry: Classify very early hardirq entry and return-tail callbacks
Tracing still needs a transition recursion allowance because a callback can run after architectural hardirq entry but before generic entry updates preempt_count(). Steven asked whether HARDIRQ_OFFSET could be installed early enough for those callbacks to observe hardirq context. This RFC implements that contract for arm64.
arm64 must first distinguish ordinary interrupts from pseudo-NMIs. For an ordinary interrupt, stage the raw preempt-count addition and establish lockdep state immediately after that decision and before instrumentable entry work. Keep time, tick, RCU and IRQ accounting at the existing generic boundary. With DEBUG_PREEMPT and without separate reschedule bits, an already-negative count preserves the legacy DEBUG_LOCKS_WARN_ON() oops short circuit, attempts deferred reporting at the instrumentable finish, and skips post-add bookkeeping and the matching subtraction when rejected.
The exit side has a second gap. Keep any applied hardirq count and lockdep classification through traceable generic exit work, then remove them before a possible schedule. Carry ordinary-hardirq provenance through that schedule in a compact three-state byte, keeping irqentry_state_t two bytes wide, and restore a classification-only tail around architecture tracing, deferred hrtimer work, IRQ-on preparation and context-tracking preparation. The tail does not repeat RCU, tick or IRQ accounting.
This is a design RFC, not a merge-ready proposal. The questions are:
- Should generic entry expose separate raw-update and bookkeeping phases?
- Are preempt-count and lockdep state sufficient for the return tail?
- Is arm64 a suitable first user of this contract?
The series is based on d135ab453a74 ("Merge tag 'backlight-next-7.4' ofgit://git.kernel.org/pub/scm/linux/kernel/git/lee/backlight"). Patches 1 and 2 include two arm64 noinstr inlining prerequisites so the series applies directly to mainline:
- "arm64: Always inline raw preempt count updates"
provenance: https://lore.kernel.org/all/c3a90368dd6c096af9d918e036a27728696fb8a4.1786603168.git.hongyan.xia@transsion.com/ - "arm64: percpu: Annotate __kern_my_cpu_offset() as '__always_inline'" provenance: https://lore.kernel.org/r/[email protected]
The first is the relevant hunk extracted from Hongyan Xia's larger posted RFC. The second is a current-mainline rebase of Will Deacon's posted patch. They preserve the authorship and links of the original submissions.
Repository: https://github.com/kmehltretter82/linux
prerequisites: steve-early-hardirq-arm64-prereqs-v2-20261027
8bdce63fa17d
generic core: steve-early-hardirq-core-v2-folded-20261027
8651af55ecbc
arm64 RFC: steve-early-hardirq-arm64-rfc-v2-folded-20261027
56016d197e1bThe transition recursion slots remain unchanged. A separate recursion-lifetime series keeps the task recursion bit inside one preemption-disabled interval. A development-only patch removed the slots for a matched experiment. Neither that removal nor the lifetime series is part of this RFC.
All three kernels in the experiment contained this early-hardirq series. On a native 16-vCPU Google Axion system, matched arm64 debug kernels completed 100 deterministic ftrace callback and IPI overlaps per cell:
done litmus mixed collisions
Slots kept, lifetime fix 100/100 0 0 0 Slots removed, no fix 100/100 33 60 13 Slots removed, lifetime 100/100 0 0 0
The litmus and mixed columns count ftrace recursion records. The collisions column counts transition-sensitive scheduler and return-tail records. Complete dmesg captures were clean in all three cells.
The second cell is a counterexample to removing the slots without the lifetime fix in the tested path. The third supports the combined design on this machine. It does not establish every architecture-specific early-entry interval, so this RFC retains the slots and keeps the lifetime work separate.
Before this rebase, the patch-identical tree was validated across a nine-cell cross-architecture W=1 full-link matrix: RISC-V 64, i386, powerpc64le, arm64, x86-64 and ARM32 with GCC 13.3 and Clang/LLD 18.1.3. It also passed 600-second arm64 SMP8 and RISC-V SMP14 stress gates, a RISC-V AIA/IMSIC SMP1/SMP2/SMP8 gate under QEMU 11.1.50, and a focused four-row DEBUG_PREEMPT staged-add oops oracle with 403 of 403 locking selftests.
An ARM64 PREEMPT_RT SMP4 build passed targeted return-tail tasklet and forced-thread timer closure tests, three SMP2 priority-masked pseudo-NMI return runs, and an SMP1 NO_HZ_IDLE and RCU_EQS_DEBUG return gate with 386 Startirq-to-Endirq intervals and 1,229 IRQ-disable/enable pairs. All patches remain unchanged according to git range-diff after the rebase.
Changes since v1:
- rebase onto d135ab453a74 and include the two arm64 noinstr inlining prerequisites as patches 1 and 2 so the series applies directly to mainline
- keep the recursion-lifetime change in a separate series
- carry a scoped hardirq-entry cookie across staged count updates so !HAS_SEPARATE_PREEMPT_RESCHED_BITS && DEBUG_PREEMPT configurations preserve the legacy DEBUG_LOCKS_WARN_ON() oops decision and skip post-add work and teardown when rejected
- encode ordinary-hardirq provenance and staged-count application in a three-state byte so irqentry_state_t remains two bytes wide
- fully initialize the arm64 EL1 debug entry state before provenance is added
- retain irqentry_exit_pcpu_adjust() ordering across the return tail
- accept and test nonzero nested lockdep hardirq depth in the tail
- add a matched native test of the separate recursion-lifetime work and
- tighten the contract and documentation wording
v1: https://lore.kernel.org/all/[email protected]/
Hongyan Xia (1):
arm64: Always inline raw preempt count updates
Karl Mehltretter (11):
sched: Split preempt-count bookkeeping from raw updates
lockdep: Use raw access for pinned hardirq state
entry: Add the pre-counted ordinary-hardirq entry/exit contract
entry: Skip inactive IRQ-return reschedule calls
context_tracking: Split IRQ-exit preparation from the EQS transition
lockdep: Prepare IRQ-on state from a hardirq return tail
entry: Carry ordinary-hardirq provenance through kernel return
entry: Classify ordinary hardirq return-tail work as hardirq context
arm64: Use the pre-counted hardirq lifecycle
Documentation: entry: Describe the pre-counted hardirq lifecycle locking/selftest: Exercise hardirq return-tail preparation
Will Deacon (1):
arm64: percpu: Annotate __kern_my_cpu_offset() as '__always_inline'
Documentation/core-api/entry.rst | 89 +++++++++++-- arch/arm64/include/asm/percpu.h | 2 +- arch/arm64/include/asm/preempt.h | 6 +- arch/arm64/kernel/entry-common.c | 34 +++-- include/linux/context_tracking_irq.h | 32 ++++- include/linux/hardirq.h | 33 +++++ include/linux/interrupt.h | 2 +- include/linux/irq-entry-common.h | 130 +++++++++++++++++-- include/linux/irqentry_hardirq.h | 81 ++++++++++++ include/linux/irqflags.h | 14 ++- kernel/context_tracking.c | 78 +++++++++--- kernel/entry/common.c | 45 ++++++- kernel/locking/lockdep.c | 49 +++++--- kernel/sched/core.c | 101 +++++++++++++-- kernel/softirq.c | 181 +++++++++++++++++++++++---- lib/locking-selftest.c | 53 ++++++++ 16 files changed, 828 insertions(+), 102 deletions(-) create mode 100644 include/linux/irqentry_hardirq.h
base-commit: d135ab453a7400a34930bdda1ef4bdc8acfac80e
Signature · 1 line
2.39.5 (Apple Git-154)
RFC PATCH v2 1/13 arm64: Always inline raw preempt count updates
From: Hongyan Xia <[email protected]>
Clang can outline the raw preempt count helpers from noinstr callers into ordinary instrumentable text. Force the add and subtract operations inline so entry code performs the count update in its own noinstr section.
This is the preempt-count hunk from Hongyan Xia's pending Kprobe noinstr RFC. Keep it as a separate prerequisite so the early-hardirq work neither hides nor claims that fix.
--- arch/arm64/include/asm/preempt.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
arch/arm64/include/asm/preempt.h +2 -2
@@ -41,14 +41,14 @@ static inline bool test_preempt_need_resched(void)return !current_thread_info ()->preempt.need_resched; } -static inline void __preempt_count_add (int val) +static __always_inline void __preempt_count_add(int val) {u32 pc =READ_ONCE (current_thread_info ()->preempt.count); pc += val;WRITE_ONCE (current_thread_info ()->preempt.count, pc); } -static inline void __preempt_count_sub (int val) +static __always_inline void __preempt_count_sub(int val) {u32 pc =READ_ONCE (current_thread_info ()->preempt.count); pc -= val;
Signature · 1 line
2.39.5 (Apple Git-154)
RFC PATCH v2 2/13 arm64: percpu: Annotate __kern_my_cpu_offset() as '__always_inline'
In reply to Karl MehltretterFrom: Will Deacon <[email protected]>
Allow the use of per-cpu variables from noinstr functions by marking __kern_my_cpu_offset() as '__always_inline'. This will later be relied upon to detect stack overflow of the overflow stack from noinstr C code.
--- arch/arm64/include/asm/percpu.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm64/include/asm/percpu.h +1 -1
@@ -37,7 +37,7 @@ static inline unsigned long __hyp_my_cpu_offset(void) "mrs " dst ", tpidr_el2", \ ARM64_HAS_VIRT_HOST_EXTN) -static inline unsigned long __kern_my_cpu_offset (void ) +static __always_inline unsigned long __kern_my_cpu_offset (void ) {unsigned long off;
Signature · 1 line
2.39.5 (Apple Git-154)
RFC PATCH v2 3/13 sched: Split preempt-count bookkeeping from raw updates
In reply to Karl MehltretterVery early hardirq entry code needs to update the preempt count before instrumentable debug and tracing code can run. Conversely, hardirq return code needs to finish instrumentable validation and latency tracing before raw teardown.
Add a small hardirq-entry header with irqentry_hardirq_count_start() to stage the raw HARDIRQ_OFFSET addition and record whether it was applied in an opaque cookie. With DEBUG_PREEMPT and without separate reschedule bits, route an already-negative count through a noinstr slow helper which preserves the existing DEBUG_LOCKS_WARN_ON() oops short circuit. Add irqentry_hardirq_count_finish() to report a rejected addition or complete post-add debug checks and latency tracing, and add irqentry_hardirq_count_sub_prepare() for validation and tracing before a raw subtraction. Keep preempt_count_add() and preempt_count_sub() behavior and ordering unchanged.
Keep latency tracing in each calling path so CALLER_ADDR0 identifies the caller. Force the start and stop helpers inline because trace provenance depends on their stack frames.
--- include/linux/hardirq.h | 1 + include/linux/irqentry_hardirq.h | 81 +++++++++++++++++++++++++ kernel/sched/core.c | 101 +++++++++++++++++++++++++++---- 3 files changed, 171 insertions(+), 12 deletions(-) create mode 100644 include/linux/irqentry_hardirq.h
include/linux/hardirq.h +1 -0
@@ -4,6 +4,7 @@#include <linux/context_tracking_state.h> #include <linux/preempt.h> +#include <linux/irqentry_hardirq.h> #include <linux/lockdep.h> #include <linux/ftrace_irq.h> #include <linux/sched.h>
include/linux/irqentry_hardirq.h new file +81 -0
81 lines not on this page · raw message
kernel/sched/core.c +89 -12
143 lines not on this page · raw message
Signature · 1 line
2.39.5 (Apple Git-154)
Peter Zijlstra
maintainerRe: [RFC PATCH v2 03/13] sched: Split preempt-count bookkeeping from raw updates
Quoted 16 lines · Karl Mehltretter
Very early hardirq entry code needs to update the preempt count before instrumentable debug and tracing code can run. Conversely, hardirq return code needs to finish instrumentable validation and latency tracing before raw teardown.
Add a small hardirq-entry header with
irqentry_hardirq_count_start()to stage the rawHARDIRQ_OFFSETaddition and record whether it was applied in an opaque cookie. WithDEBUG_PREEMPTand without separate reschedule bits, route an already-negative count through a noinstr slow helper which preserves the existingDEBUG_LOCKS_WARN_ON()oops short circuit. Addirqentry_hardirq_count_finish()to report a rejected addition or complete post-add debug checks and latency tracing, and addirqentry_hardirq_count_sub_prepare()for validation and tracing before a raw subtraction. Keeppreempt_count_add()andpreempt_count_sub()behavior and ordering unchanged.
Quoted 3 lines
Keep latency tracing in each calling path so
CALLER_ADDR0identifies the caller. Force the start and stop helpers inline because trace provenance depends on their stack frames.
Urgh, so much crazy.
So WARNs in noinstr code are 'fine'. That is, they should not happen, if they do, bad things have happened and we need the warning out ASAP, if it takes the machine down, too bad. See the instrumentation_{begin,end}() markers in x86 asm/bug.h.
But yes, the tracing needs to go. But moving that around should not be this ugly and also, that will give you the opportunity to fix preempt_{en,dis}able_notrace(), since they currently also skip the bound checks.
Re: [RFC PATCH v2 03/13] sched: Split preempt-count bookkeeping from raw updates
Quoted 11 lines · Peter Zijlstra
Urgh, so much crazy.
So WARNs in noinstr code are 'fine'. That is, they should not happen, if they do, bad things have happened and we need the warning out ASAP, if it takes the machine down, too bad. See the instrumentation_{begin,end}() markers in x86
asm/bug.h.But yes, the tracing needs to go. But moving that around should not be this ugly and also, that will give you the opportunity to fix preempt_{en,dis}able_notrace(), since they currently also skip the bound checks.
Yes, that's a good direction.
I checked the call chain and DEBUG_LOCKS_WARN_ON() is already fine in noinstr. arm64 just needs __always_inline on preempt_count() and should_resched() so Clang doesn't outline them.
I will work on that.
Thanks,
Karl
RFC PATCH v2 4/13 lockdep: Use raw access for pinned hardirq state
In reply to Karl Mehltretterirqentry_nmi_enter() reads lockdep's hardirq state before __nmi_enter() establishes NMI context. Arm64 does the same for its NMI-like EL1 debug entry. Both paths run with interrupts disabled and cannot migrate.
lockdep_hardirqs_enabled() uses this_cpu_read(), whose preemption checks are unnecessary for these callers. Add a raw accessor and use it in the two entry paths and in lockdep_hardirqs_off(). Keep the existing accessor for callers which are not already pinned.
Check the interrupt state before accessing per-CPU state in lockdep_hardirqs_off(), independently of warning suppression.
On arm64, this change relies on __kern_my_cpu_offset() being forced inline by the earlier prerequisite patch so the call chain remains noinstr-safe.
--- arch/arm64/kernel/entry-common.c | 6 +++--- include/linux/irqflags.h | 3 +++ kernel/entry/common.c | 2 +- kernel/locking/lockdep.c | 6 ++++-- 4 files changed, 11 insertions(+), 6 deletions(-)
arch/arm64/kernel/entry-common.c +3 -3
@@ -177,9 +177,9 @@ asmlinkage void noinstr asm_exit_to_user_mode(struct pt_regs *regs)*/ static noinstr irqentry_state_t arm64_enter_el1_dbg(struct pt_regs *regs) { -irqentry_state_t state; - - state.lockdep =lockdep_hardirqs_enabled (); +irqentry_state_t state = { + .lockdep =lockdep_hardirqs_enabled_raw (), + };lockdep_hardirqs_off (CALLER_ADDR0 );ct_nmi_enter ();
include/linux/irqflags.h +3 -0
@@ -52,6 +52,8 @@ extern void trace_hardirqs_off(void);# define lockdep_hardirq_context () (raw_cpu_read(hardirq_context))# define lockdep_softirq_context (p) ((p)->softirq_context)# define lockdep_hardirqs_enabled () (this_cpu_read(hardirqs_enabled)) +/* The caller must already be pinned to the current CPU. */ +# define lockdep_hardirqs_enabled_raw () (raw_cpu_read(hardirqs_enabled))# define lockdep_softirqs_enabled (p) ((p)->softirqs_enabled)# define lockdep_hardirq_enter () \ do { \
@@ -113,6 +115,7 @@ do { \# define lockdep_hardirq_context () 0# define lockdep_softirq_context (p) 0# define lockdep_hardirqs_enabled () 0 +# define lockdep_hardirqs_enabled_raw () 0# define lockdep_softirqs_enabled (p) 0# define lockdep_hardirq_enter () do { } while (0)# define lockdep_hardirq_threaded () do { } while (0)
kernel/entry/common.c +1 -1
@@ -158,7 +158,7 @@ irqentry_state_t noinstr irqentry_nmi_enter(struct pt_regs *regs) {irqentry_state_t irq_state; - irq_state.lockdep =lockdep_hardirqs_enabled (); + irq_state.lockdep =lockdep_hardirqs_enabled_raw ();__nmi_enter ();lockdep_hardirqs_off (CALLER_ADDR0 );
kernel/locking/lockdep.c +4 -2
@@ -4543,10 +4543,12 @@ void noinstr lockdep_hardirqs_off(unsigned long ip)* So we're supposed to get called after you mask local IRQs, but for * some reason the hardware doesn't quite think you did a proper job. */ -if (DEBUG_LOCKS_WARN_ON (!irqs_disabled ())) +if (unlikely (!irqs_disabled ())) { +DEBUG_LOCKS_WARN_ON (1 );return ; + } -if (lockdep_hardirqs_enabled ()) { +if (lockdep_hardirqs_enabled_raw ()) {struct irqtrace_events *trace = ¤t->irqtrace;/*
Signature · 1 line
2.39.5 (Apple Git-154)
RFC PATCH v2 5/13 entry: Add the pre-counted ordinary-hardirq entry/exit contract
In reply to Karl MehltretterArchitectures which identify ordinary hardirqs before generic entry need to install HARDIRQ_OFFSET before context tracking and tracing callbacks run. Lockdep hardirq state must agree with that count. On exit, both states must remain installed until every traceable callback has returned.
Add irqentry_hardirq_enter() to stage the raw count update, record whether it was applied in irqentry_state_t, and establish lockdep state before instrumentable entry work. irq_enter_rcu_preempt_counted() finishes the staged count update while preserving the existing timer and hardirq accounting boundary.
Add a retained exit path which runs IRQ-exit processing and instrumentable teardown preparation before returning to an immediate conditional raw count and lockdep teardown. This keeps function-graph return callbacks in hardirq context.
A return callback after the final softirq drain can raise regular or forced-thread timer work while hardirq state still suppresses the usual wakeup. Recheck ksoftirqd, ktimerd, then ksoftirqd after the last traceable teardown step. Record wake requests made by the main exit path so the closure does not repeat them.
Keep the existing entry and exit interfaces unchanged. Architectures opt in only after distinguishing ordinary hardirqs from NMI-like entry. They must not place instrumentable code between the retained-exit helper and raw teardown.
--- include/linux/hardirq.h | 31 ++++++ include/linux/interrupt.h | 2 +- include/linux/irq-entry-common.h | 62 +++++++++++- include/linux/irqflags.h | 9 +- kernel/entry/common.c | 37 +++++++ kernel/softirq.c | 160 ++++++++++++++++++++++++++----- 6 files changed, 271 insertions(+), 30 deletions(-)
include/linux/hardirq.h +31 -0
@@ -26,6 +26,37 @@ static __always_inline void rcu_irq_enter_check_tick(void)__rcu_irq_enter_check_tick (); } +void irq_enter_rcu_preempt_counted (struct irqentry_hardirq_cookie cookie); +bool irq_exit_rcu_preempt_counted_prepare (struct irqentry_hardirq_cookie cookie); + +/* +* Prepare every potentially instrumentable part of hardirq teardown before +* the raw count and lockdep updates. The caller must invoke the matching +* finish helper without passing through instrumentable code. +*/ +static __always_inline bool +irqentry_hardirq_exit_prepare (struct irqentry_hardirq_cookie cookie) +{ +bool do_sub; + +/* +* Preempt-on tracing can raise deferred work. The caller must perform +* the final worker closure before conditionally removing an applied +* raw hardirq count. +*/ + do_sub =irqentry_hardirq_count_applied (cookie) && +irqentry_hardirq_count_sub_prepare (); +lockdep_hardirq_exit_prepare (); +return do_sub; +} + +static __always_inline void irqentry_hardirq_exit_finish(bool do_sub) +{ +if (do_sub) +__preempt_count_sub (HARDIRQ_OFFSET ); +lockdep_hardirq_exit_finish (); +} +/* * It is safe to do non-atomic ops on ->hardirq_context, * because NMI handlers may not preempt and the ops are
include/linux/interrupt.h +1 -1
@@ -640,7 +640,7 @@ DECLARE_PER_CPU(struct task_struct *, ktimerd); DECLARE_PER_CPU(unsigned long, pending_timer_softirq);void raise_ktimers_thread (unsigned int nr); -static inline unsigned int local_timers_pending_force_th (void ) +static __always_inline unsigned int local_timers_pending_force_th(void) {return __this_cpu_read (pending_timer_softirq); }
include/linux/irq-entry-common.h +58 -4
142 lines not on this page · raw message
include/linux/irqflags.h +8 -1
@@ -64,9 +64,14 @@ do { \do { \ current->hardirq_threaded =1 ; \ } while (0) +# define lockdep_hardirq_exit_prepare () \ + __this_cpu_preempt_check("add") +# define lockdep_hardirq_exit_finish () \ + raw_cpu_dec(hardirq_context)# define lockdep_hardirq_exit () \ do { \ - __this_cpu_dec(hardirq_context); \ + lockdep_hardirq_exit_prepare(); \ + lockdep_hardirq_exit_finish(); \ } while (0)# define lockdep_hrtimer_enter (__hrtimer) \
@@ -119,6 +124,8 @@ do { \# define lockdep_softirqs_enabled (p) 0# define lockdep_hardirq_enter () do { } while (0)# define lockdep_hardirq_threaded () do { } while (0) +# define lockdep_hardirq_exit_prepare () do { } while (0) +# define lockdep_hardirq_exit_finish () do { } while (0)# define lockdep_hardirq_exit () do { } while (0)# define lockdep_softirq_enter () do { } while (0)# define lockdep_softirq_exit () do { } while (0)
kernel/entry/common.c +37 -0
@@ -1,6 +1,7 @@// SPDX-License-Identifier: GPL-2.0 #include <linux/futex.h> +#include <linux/hardirq.h> #include <linux/highmem.h> #include <linux/irq-entry-common.h> #include <linux/jump_label.h>
@@ -120,6 +121,42 @@ noinstr irqentry_state_t irqentry_enter(struct pt_regs *regs)return irqentry_enter_from_kernel_mode (regs); } +/** +* irqentry_hardirq_enter - Establish state for ordinary hardirq entry +* @regs: Pointer to the interrupted register state +* +* Attempt a staged raw hardirq-count addition before anything instrumentable +* can run and record whether it was applied. Then establish IRQ-disabled and +* hardirq lockdep state before context tracking or irqflag tracing can invoke +* callbacks. The caller must have distinguished an ordinary hardirq from +* NMI-like entry and must finish the staged attempt with +* irq_enter_rcu_preempt_counted(). +* +* Returns: An opaque object for the matching irqentry exit operation. +*/ +noinstr irqentry_state_t irqentry_hardirq_enter(struct pt_regs *regs) +{ +struct irqentry_hardirq_cookie cookie; +irqentry_state_t ret; + + cookie =irqentry_hardirq_count_start (); + +if (user_mode (regs)) { + ret = (irqentry_state_t ) { + .exit_rcu = false, + }; +irqentry_hardirq_count_state_set (&ret, cookie); + +__enter_from_user_mode (regs, true); +rseq_note_user_irq_entry (); +return ret; + } + + ret =__irqentry_enter_from_kernel_mode (regs, true); +irqentry_hardirq_count_state_set (&ret, cookie); +return ret; +} +/** * arch_irqentry_exit_need_resched - Architecture specific need resched function *
kernel/softirq.c +136 -24
246 lines not on this page · raw message
Signature · 1 line
2.39.5 (Apple Git-154)
[email protected]
AI reviewRe: [RFC PATCH v2 05/13] entry: Add the pre-counted ordinary-hardirq entry/exit contract
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] entry: irqentry_hardirq_enter() breaks preemptirqsoff critical timing start - [Medium] softirq…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] entry: irqentry_hardirq_enter() breaks preemptirqsoff critical timing start
- [Medium] softirq: invoke_softirq() drops ksoftirqd wake flag after inline handoff --
commit 50eb58fd97f0b74442817ebd3d3092c57116862f
Author: Karl Mehltretter <[email protected]>
entry: Add the pre-counted ordinary-hardirq entry/exit contract
This commit adds staged hardirq entry and retained exit helpers so that ordinary hardirqs can install HARDIRQ_OFFSET and lockdep hardirq state before entry tracing, and keep them installed until all traceable exit callbacks have completed.
Quoted 41 lines
kernel/entry/common.c +26
-0@@ -1,6 +1,7 @@// SPDX-License-Identifier: GPL-2.0 #include <linux/futex.h> +#include <linux/hardirq.h> #include <linux/highmem.h> #include <linux/irq-entry-common.h> #include <linux/jump_label.h> @@ -120,6 +121,42 @@ noinstr irqentry_state_t irqentry_enter(struct pt_regs *regs)return irqentry_enter_from_kernel_mode (regs); } +/** +* irqentry_hardirq_enter - Establish state for ordinary hardirq entry +* @regs: Pointer to the interrupted register state +* +* Attempt a staged raw hardirq-count addition before anything instrumentable +* can run and record whether it was applied. Then establish IRQ-disabled and +* hardirq lockdep state before context tracking or irqflag tracing can invoke +* callbacks. The caller must have distinguished an ordinary hardirq from +* NMI-like entry and must finish the staged attempt with +* irq_enter_rcu_preempt_counted(). +* +* Returns: An opaque object for the matching irqentry exit operation. +*/ +noinstr irqentry_state_t irqentry_hardirq_enter(struct pt_regs *regs) +{ +struct irqentry_hardirq_cookie cookie; +irqentry_state_t ret; + + cookie =irqentry_hardirq_count_start (); + +if (user_mode (regs)) { + ret = (irqentry_state_t ) { + .exit_rcu = false, + }; +irqentry_hardirq_count_state_set (&ret, cookie);
kernel/entry/common.c noinstr irqentry_state_t irqentry_enter(struct pt_regs *regs) · line 158
Quoted 10 lines
kernel/entry/common.c +10
-0+ +__enter_from_user_mode (regs, true); +rseq_note_user_irq_entry (); +return ret; + } + + ret =__irqentry_enter_from_kernel_mode (regs, true); +irqentry_hardirq_count_state_set (&ret, cookie); +return ret; +}
[Severity: Medium]
Does staging HARDIRQ_OFFSET in irqentry_hardirq_enter() before calling __enter_from_user_mode() or __irqentry_enter_from_kernel_mode() prevent the preemptirqsoff latency tracer from starting critical timing when an ordinary hardirq interrupts user mode or preemptible kernel mode?
When trace_type has both TRACER_IRQS_OFF and TRACER_PREEMPT_OFF enabled and an interrupt arrives while preempt_count() is 0:
1. irqentry_hardirq_enter() calls irqentry_hardirq_count_start(), which adds HARDIRQ_OFFSET to preempt_count() while hardware interrupts are disabled.
2. __enter_from_user_mode() or __irqentry_enter_from_kernel_mode() then calls trace_hardirqs_off_finish() -> tracer_hardirqs_off():
kernel/trace/trace_irqsoff.c:tracer_hardirqs_off() {
if (!preempt_trace(preempt_count()) && irq_trace())
start_critical_timing(a0, a1);
}Because HARDIRQ_OFFSET is already present in preempt_count(), preempt_trace(preempt_count()) evaluates to true, so tracer_hardirqs_off() assumes preemption was already disabled before IRQs were turned off and skips start_critical_timing().
3. Later, irq_enter_rcu_preempt_counted() calls irqentry_hardirq_count_finish() -> preempt_latency_start(HARDIRQ_OFFSET) -> trace_preempt_off() -> tracer_preempt_off():
kernel/trace/trace_irqsoff.c:tracer_preempt_off() {
if (preempt_trace(preempt_count()) && !irq_trace())
start_critical_timing(a0, a1);
}Because hardware interrupts are disabled, irq_trace() evaluates to true, so tracer_preempt_off() assumes tracer_hardirqs_off() already started critical timing and also skips start_critical_timing().
As a result, neither callback starts critical timing for the hardirq and any subsequent softirq exit processing.
Quoted 4 lines
kernel/softirq.c +0
-0
[ ... ]
Quoted 17 lines
kernel/softirq.c +2
-2@@ -525,12 +529,12 @@ static inline void ksoftirqd_run_end(void)local_irq_enable (); } -static inline bool should_wake_ksoftirqd (void ) +static __always_inline bool should_wake_ksoftirqd(void) {return true; } -static inline void invoke_softirq (void ) +static inline bool invoke_softirq (void ) {if (!force_irqthreads () || !__this_cpu_read (ksoftirqd)) {#ifdef CONFIG_HAVE_IRQ_EXIT_ON_IRQ_STACK @@ -548,9 +552,11 @@ static inline void invoke_softirq(void)*/
kernel/softirq.c static inline void invoke_softirq(void) · line 560
Quoted 10 lines
kernel/softirq.c +4
-2do_softirq_own_stack ();#endif - }else { -wakeup_softirqd (); +return false; } + +wakeup_softirqd (); +return true; }
[Severity: Medium]
When invoke_softirq() runs __do_softirq() or do_softirq_own_stack() on !CONFIG_PREEMPT_RT, can returning false unconditionally cause irq_exit_wake_late_softirqs() to repeat a wakeup_softirqd() call that was already performed by handle_softirqs()?
Inside handle_softirqs(), if softirqs are re-raised during inline draining and the loop stops because need_resched() is set, the time limit is reached, or max_restart is exhausted, handle_softirqs() wakes ksoftirqd itself while leaving the remaining bits set in local_softirq_pending():
kernel/softirq.c:handle_softirqs() {
...
pending = local_softirq_pending();
if (pending) {
if (time_before(jiffies, end) && !need_resched() &&
--max_restart)
goto restart;
wakeup_softirqd();
}
...
}Because invoke_softirq() still returns false after __do_softirq() or do_softirq_own_stack(), __irq_exit_rcu() does not set IRQ_EXIT_WAKE_KSOFTIRQD in wake_flags.
When irq_exit_rcu_preempt_counted_prepare() then runs:
wake_flags = __irq_exit_rcu(true); do_sub = irqentry_hardirq_exit_prepare(cookie); irq_exit_wake_late_softirqs(wake_flags);
irq_exit_wake_late_softirqs() sees ksoftirqd_wake_requested as false while local_softirq_pending() is still non-zero from the earlier handoff, causing irq_exit_wake_ksoftirqd() to call wakeup_softirqd() a second time after irqentry_hardirq_exit_prepare() has already emitted trace_preempt_on().
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
RFC PATCH v2 6/13 entry: Skip inactive IRQ-return reschedule calls
In reply to Karl Mehltretterirqentry_exit_to_kernel_mode_preempt() calls the reschedule helper when preemption is enabled even if the preempt count is nonzero. The helper returns immediately, but its call and return remain traceable after hardirq context has been removed.
Check the preempt count in the inline caller and avoid the inactive call. Keep the existing check in the helper for its other callers. This does not change whether IRQ return schedules.
Arm64 invokes this caller from noinstr code. Force its preempt_count() implementation inline so Clang cannot outline the new check into instrumentable text.
--- arch/arm64/include/asm/preempt.h | 2 +- include/linux/irq-entry-common.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
arch/arm64/include/asm/preempt.h +1 -1
@@ -7,7 +7,7 @@#define PREEMPT_NEED_RESCHED BIT(32)#define PREEMPT_ENABLED (PREEMPT_NEED_RESCHED) -static inline int preempt_count (void ) +static __always_inline int preempt_count(void) {return READ_ONCE (current_thread_info ()->preempt.count); }
include/linux/irq-entry-common.h +1 -1
@@ -503,7 +503,7 @@ static inline void irqentry_exit_to_kernel_mode_preempt(struct pt_regs *regs,if (regs_irqs_disabled (regs) || state.exit_rcu)return ; -if (IS_ENABLED (CONFIG_PREEMPTION )) +if (IS_ENABLED (CONFIG_PREEMPTION ) && !preempt_count ())irqentry_exit_cond_resched (); }
Signature · 1 line
2.39.5 (Apple Git-154)
[email protected]
AI reviewRe: [RFC PATCH v2 06/13] entry: Skip inactive IRQ-return reschedule calls
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] entry: missing __always_inline on irqentry_exit_to_kernel_mode_preempt() --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] entry: missing __always_inline on irqentry_exit_to_kernel_mode_preempt() --
commit b2284e95ad33bdfe152568730c196eaa50875e40
Author: Karl Mehltretter <[email protected]>
entry: Skip inactive IRQ-return reschedule calls
This commit checks preempt_count() in irqentry_exit_to_kernel_mode_preempt() to avoid calling irqentry_exit_cond_resched() when preemption is disabled, and marks arm64's preempt_count() as __always_inline for noinstr callers.
include/linux/irq-entry-common.h static inline void irqentry_exit_to_kernel_mode_preempt(struct pt_regs *regs, · line 508
Quoted 12 lines
include/linux/irq-entry-common.h +1
-1@@ -503,7 +503,7 @@ static inline void irqentry_exit_to_kernel_mode_preempt(struct pt_regs *regs,if (regs_irqs_disabled (regs) || state.exit_rcu)return ; -if (IS_ENABLED (CONFIG_PREEMPTION )) +if (IS_ENABLED (CONFIG_PREEMPTION ) && !preempt_count ())irqentry_exit_cond_resched (); }
[Severity: Medium]
Should irqentry_exit_to_kernel_mode_preempt() in include/linux/irq-entry-common.h also be marked __always_inline (like its companions irqentry_enter_from_kernel_mode(), irqentry_exit_to_kernel_mode_after_preempt(), and irqentry_exit_to_kernel_mode())?
On arm64, __el1_irq() and arm64_exit_to_kernel_mode() call noinstr function arm64_exit_to_kernel_mode_preempt() in arch/arm64/kernel/entry-common.c, which invokes irqentry_exit_to_kernel_mode_preempt() directly without instrumentation_begin():
static void noinstr arm64_exit_to_kernel_mode_preempt(struct pt_regs *regs,
irqentry_state_t state)
{
irqentry_exit_to_kernel_mode_preempt(regs, state);
__arm64_exit_to_kernel_mode(regs, state);
}While preempt_count() in arch/arm64/include/asm/preempt.h is changed to __always_inline so Clang does not outline the new check into instrumentable text, irqentry_exit_to_kernel_mode_preempt() itself remains static inline:
static inline void irqentry_exit_to_kernel_mode_preempt(struct pt_regs *regs,
irqentry_state_t state)
Because noinstr in include/linux/compiler_types.h strips sanitizer and profiling attributes (__no_kcsan, __no_sanitize_address, __no_profile, __no_sanitize_coverage, __no_sanitize_memory) whereas inline only adds notrace, building with Clang and CONFIG_KASAN=y, CONFIG_KCSAN=y, or CONFIG_GCOV_KERNEL=y can cause LLVM to refuse to inline a non-__always_inline callee due to mismatched attributes.
If that happens, couldn't Clang inline preempt_count() into irqentry_exit_to_kernel_mode_preempt() and then outline irqentry_exit_to_kernel_mode_preempt() itself into instrumentable .text, allowing sanitizer instrumentation or kprobes to run on the noinstr return path even when state.exit_rcu or regs_irqs_disabled(regs) is true?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
RFC PATCH v2 7/13 context_tracking: Split IRQ-exit preparation from the EQS transition
In reply to Karl Mehltretterct_irq_exit() combines traceable nesting and tracepoint work with the final transition to an RCU extended quiescent state. A pre-counted ordinary hardirq return needs to run that preparation before its synthetic hardirq classification is removed, but it must enter the quiescent state only after the raw count and lockdep teardown.
Split IRQ exit into prepare and finish phases. The prepare phase validates and updates interrupt nesting, then emits the tracepoint while RCU is watching. The finish phase performs the raw quiescent-state transition and RCU Tasks bookkeeping.
Keep ct_irq_exit() and ct_nmi_exit() as adjacent prepare and finish wrappers. Existing IRQ and NMI callers retain their ordering and behavior.
--- include/linux/context_tracking_irq.h | 32 +++++++++++- kernel/context_tracking.c | 78 ++++++++++++++++++++++------ 2 files changed, 93 insertions(+), 17 deletions(-)
include/linux/context_tracking_irq.h +31 -1
@@ -2,8 +2,23 @@#ifndef _LINUX_CONTEXT_TRACKING_IRQ_H#define _LINUX_CONTEXT_TRACKING_IRQ_H +#include <linux/types.h> + +/** +* struct ct_irq_exit_state - Opaque context-tracking IRQ-exit state +* @exit_rcu: The interrupted context was in an RCU extended quiescent state +* +* This object is filled in by ct_irq_exit_prepare() and must be passed to the +* matching ct_irq_exit_finish(). Callers must consider its members private. +*/ +struct ct_irq_exit_state { +bool exit_rcu; +}; +#ifdef CONFIG_CONTEXT_TRACKING_IDLE void ct_irq_enter (void ); +struct ct_irq_exit_state ct_irq_exit_prepare (void ); +void ct_irq_exit_finish (struct ct_irq_exit_state state);void ct_irq_exit (void );void ct_irq_enter_irqson (void );void ct_irq_exit_irqson (void );
@@ -11,7 +26,22 @@ void ct_nmi_enter(void);void ct_nmi_exit (void );#else static __always_inline void ct_irq_enter(void) { } -static __always_inline void ct_irq_exit(void) { } +static __always_inline struct ct_irq_exit_state ct_irq_exit_prepare(void) +{ +return (struct ct_irq_exit_state ) { }; +} + +static __always_inline void ct_irq_exit_finish(struct ct_irq_exit_state state) +{ + (void )state; +} + +static __always_inline void ct_irq_exit(void) +{ +struct ct_irq_exit_state state =ct_irq_exit_prepare (); + +ct_irq_exit_finish (state); +}static inline void ct_irq_enter_irqson (void ) { }static inline void ct_irq_exit_irqson (void ) { } static __always_inline void ct_nmi_enter(void) { }
kernel/context_tracking.c +62 -16
116 lines not on this page · raw message
Signature · 1 line
2.39.5 (Apple Git-154)
RFC PATCH v2 8/13 lockdep: Prepare IRQ-on state from a hardirq return tail
In reply to Karl Mehltretterlockdep_hardirqs_on_prepare() rejects any live hardirq context. An ordinary hardirq return tail needs to retain synthetic hardirq nesting through traceable IRQ-on preparation. This keeps callbacks classified as hardirq work.
Factor the common preparation and add a return-tail variant which requires the synthetic hardirq context to be present. An interrupted outer hardirq can contribute additional nesting. Keep the ordinary helper restricted to zero. Neither helper changes IRQ or hardirq state. The caller must remove the tail context before lockdep_hardirqs_on().
--- include/linux/irqflags.h | 2 ++ kernel/locking/lockdep.c | 43 +++++++++++++++++++++++++++++----------- 2 files changed, 33 insertions(+), 12 deletions(-)
include/linux/irqflags.h +2 -0
@@ -25,6 +25,7 @@ struct task_struct;extern void lockdep_softirqs_on (unsigned long ip);extern void lockdep_softirqs_off (unsigned long ip);extern void lockdep_hardirqs_on_prepare (void ); +void lockdep_hardirqs_on_prepare_irqtail (void );extern void lockdep_hardirqs_on (unsigned long ip);extern void lockdep_hardirqs_off (unsigned long ip); extern void lockdep_cleanup_dead_cpu(unsigned int cpu,
@@ -33,6 +34,7 @@ struct task_struct;static inline void lockdep_softirqs_on (unsigned long ip) { }static inline void lockdep_softirqs_off (unsigned long ip) { }static inline void lockdep_hardirqs_on_prepare (void ) { } +static inline void lockdep_hardirqs_on_prepare_irqtail (void ) { }static inline void lockdep_hardirqs_on (unsigned long ip) { }static inline void lockdep_hardirqs_off (unsigned long ip) { } static inline void lockdep_cleanup_dead_cpu(unsigned int cpu,
kernel/locking/lockdep.c +31 -12
64 lines not on this page · raw message
Signature · 1 line
2.39.5 (Apple Git-154)
RFC PATCH v2 9/13 entry: Carry ordinary-hardirq provenance through kernel return
In reply to Karl MehltretterThe entry state can cross hardirq teardown and a possible reschedule before the architecture completes its return tail. At that point preempt_count() no longer identifies whether the original entry was an ordinary hardirq, a synchronous exception, or an NMI-like event.
Initialize the hardirq state in every entry path so irqentry_state_t distinguishes ordinary hardirqs from other entries across kernel return while keeping the two-byte generic state layout. A later return tail can restore hardirq classification only for entries which established the pre-counted hardirq lifecycle.
--- include/linux/irq-entry-common.h | 6 ++++-- kernel/entry/common.c | 6 +++++- 2 files changed, 9 insertions(+), 3 deletions(-)
include/linux/irq-entry-common.h +4 -2
@@ -341,8 +341,8 @@ enum irqentry_hardirq_state {* exit path has to invoke ct_irq_exit(). * @lockdep: Used exclusively in the irqentry_nmi_*() calls; ensures that * lockdep state is restored correctly on exit from nmi. -* @hardirq: Encodes whether the current staged hardirq-count attempt was -* applied. +* @hardirq: Encodes ordinary-hardirq provenance and whether the current +* staged hardirq-count attempt was applied. * * This opaque object is filled in by the irqentry_*_enter() functions and * must be passed back into the corresponding irqentry_*_exit() functions
@@ -412,6 +412,8 @@ __irqentry_enter_from_kernel_mode(struct pt_regs *regs, bool hardirq) {irqentry_state_t ret = { .exit_rcu = false, + .hardirq = hardirq ?IRQENTRY_HARDIRQ_APPLIED : +IRQENTRY_HARDIRQ_NONE , };/*
kernel/entry/common.c +5 -1
@@ -112,6 +112,7 @@ noinstr irqentry_state_t irqentry_enter(struct pt_regs *regs)if (user_mode (regs)) {irqentry_state_t ret = { .exit_rcu = false, + .hardirq =IRQENTRY_HARDIRQ_NONE , };irqentry_enter_from_user_mode (regs);
@@ -144,6 +145,7 @@ noinstr irqentry_state_t irqentry_hardirq_enter(struct pt_regs *regs)if (user_mode (regs)) { ret = (irqentry_state_t ) { .exit_rcu = false, + .hardirq =IRQENTRY_HARDIRQ_APPLIED , };irqentry_hardirq_count_state_set (&ret, cookie);
@@ -193,7 +195,9 @@ noinstr void irqentry_exit(struct pt_regs *regs, irqentry_state_t state) irqentry_state_t noinstr irqentry_nmi_enter(struct pt_regs *regs) { -irqentry_state_t irq_state; +irqentry_state_t irq_state = { + .hardirq =IRQENTRY_HARDIRQ_NONE , + }; irq_state.lockdep =lockdep_hardirqs_enabled_raw ();
Signature · 1 line
2.39.5 (Apple Git-154)
RFC PATCH v2 10/13 entry: Classify ordinary hardirq return-tail work as hardirq context
In reply to Karl MehltretterAn ordinary hardirq removes its original count and lockdep state before the return path may preempt. Traceable return-tail callbacks then appear to run in the interrupted task or softirq context and can collide with recursion state owned by that context.
Use the saved ordinary-hardirq provenance to stage a fresh hardirq-count attempt and restore lockdep classification after the preemption decision. Keep it across traceable return-tail work, deferred hrtimer rearming, IRQ-on preparation, and context-tracking preparation.
Before removing the raw tail state, prepare the conditional count and lockdep teardown and close late regular or forced-thread softirq work. Complete any pending RCU quiescent-state transition only after that raw teardown.
Do not repeat interrupt accounting, RCU or tick entry. Existing non-hardirq return paths remain unchanged.
--- include/linux/hardirq.h | 1 + include/linux/irq-entry-common.h | 64 ++++++++++++++++++++++++++++++-- kernel/softirq.c | 21 +++++++++++ 3 files changed, 83 insertions(+), 3 deletions(-)
include/linux/hardirq.h +1 -0
@@ -28,6 +28,7 @@ static __always_inline void rcu_irq_enter_check_tick(void)void irq_enter_rcu_preempt_counted (struct irqentry_hardirq_cookie cookie);bool irq_exit_rcu_preempt_counted_prepare (struct irqentry_hardirq_cookie cookie); +void noinstr irqentry_hardirq_tail_exit(struct irqentry_hardirq_cookie cookie);/* * Prepare every potentially instrumentable part of hardirq teardown before
include/linux/irq-entry-common.h +61 -3
100 lines not on this page · raw message
kernel/softirq.c +21 -0
@@ -837,6 +837,27 @@ irq_exit_wake_late_softirqs(unsigned int wake_flags)irq_exit_wake_ksoftirqd (); } +/** +* irqentry_hardirq_tail_exit - Close work and remove return-tail state +* @cookie: Staged hardirq-count cookie from irqentry_hardirq_tail_enter() +* +* Run every instrumentable teardown operation, including the final worker +* closure, before conditionally removing any applied raw hardirq count and +* ending lockdep nesting. +*/ +void noinstr +irqentry_hardirq_tail_exit (struct irqentry_hardirq_cookie cookie) +{ +bool do_sub; + +instrumentation_begin (); + do_sub =irqentry_hardirq_exit_prepare (cookie); +irq_exit_wake_late_softirqs (0 ); +instrumentation_end (); + +irqentry_hardirq_exit_finish (do_sub); +} + static __always_inline unsigned int __irq_exit_rcu(bool keep_hardirq_count) {unsigned int wake_flags =0 ;
Signature · 1 line
2.39.5 (Apple Git-154)
[email protected]
AI reviewRe: [RFC PATCH v2 10/13] entry: Classify ordinary hardirq return-tail work as hardirq context
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [High] entry: irqentry_exit_to_kernel_mode_after_preempt() breaks preemptirqsoff - [Medium] softirq…
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] entry: irqentry_exit_to_kernel_mode_after_preempt() breaks preemptirqsoff
- [Medium] softirq: irqentry_hardirq_tail_exit() repeats wakeups with wake_flags = 0
- [Medium] entry: irqentry_hardirq_tail_exit() wakes softirq threads after preempt check --
commit d2af631b0f352ba4a12e0eb8fa80f5ff66476c74
Author: Karl Mehltretter <[email protected]>
entry: Classify ordinary hardirq return-tail work as hardirq context
This commit stages a fresh hardirq-count attempt and restores lockdep classification across traceable return-tail work on return from ordinary hardirqs. It adds irqentry_hardirq_tail_enter() and irqentry_hardirq_tail_exit() around the post-preemption return tail so return-tail callbacks run as hardirq context.
Quoted 33 lines
include/linux/irq-entry-common.h +25
-0@@ -383,6 +383,32 @@ irqentry_hardirq_count_state_set(irqentry_state_t *state, IRQENTRY_HARDIRQ_APPLIED :IRQENTRY_HARDIRQ_REJECTED ; } +/** +* irqentry_hardirq_tail_enter - Stage ordinary-hardirq return-tail state +* @state: Pointer to state returned by the matching irqentry_hardirq_enter() +* +* Make a fresh staged hardirq-count attempt and establish the lockdep +* classification which traceable return-tail work needs. Accounting, RCU and +* tick handling belong to the completed interrupt and must not be repeated +* here. +*/ +static __always_inline void +irqentry_hardirq_tail_enter (irqentry_state_t *state) +{ +struct irqentry_hardirq_cookie cookie; + +if (!state->hardirq) +return ; + + cookie =irqentry_hardirq_count_start (); +lockdep_hardirq_enter (); + +instrumentation_begin (); +irqentry_hardirq_count_finish (cookie); +instrumentation_end (); +irqentry_hardirq_count_state_set (state, cookie); +}
[ ... ]
Quoted 10 lines
include/linux/irq-entry-common.h +6
-0@@ -543,6 +574,31 @@ static __always_inline void irqentry_hrtimer_rearm_deferred(void) static __always_inline voidirqentry_exit_to_kernel_mode_after_preempt (struct pt_regs *regs,irqentry_state_t state) { +struct ct_irq_exit_state ct_state = { }; + +if (state.hardirq) { +if (!regs_irqs_disabled (regs)) { +instrumentation_begin (); +hrtimer_rearm_deferred ();
include/linux/irq-entry-common.h static __always_inline void irqentry_hrtimer_rearm_deferred(void) · line 592
Quoted 10 lines
include/linux/irq-entry-common.h +10
-0+/* Tell the tracer that the return will enable IRQs. */ +trace_hardirqs_on_prepare (); +lockdep_hardirqs_on_prepare_irqtail (); +instrumentation_end (); + } + +if (state.exit_rcu) + ct_state =ct_irq_exit_prepare (); + +irqentry_hardirq_tail_exit (irqentry_hardirq_count_state (state));
[Severity: High]
Does ordering trace_hardirqs_on_prepare() before irqentry_hardirq_tail_exit() cause a regression for the preemptirqsoff tracer when returning to preemptible, IRQ-enabled kernel mode?
In irqentry_exit_to_kernel_mode(), irqentry_hardirq_tail_enter() stages HARDIRQ_OFFSET in preempt_count() and calls:
irqentry_hardirq_count_finish()
preempt_latency_start(HARDIRQ_OFFSET)
trace_preempt_off()
Next, when !regs_irqs_disabled(regs), irqentry_exit_to_kernel_mode_after_preempt() calls trace_hardirqs_on_prepare() while HARDIRQ_OFFSET is still set in preempt_count(). In kernel/trace/trace_irqsoff.c:
void tracer_hardirqs_on(unsigned long a0, unsigned long a1)
{
if (!preempt_trace(preempt_count()) && irq_trace())
stop_critical_timing(a0, a1);
}When the preemptirqsoff tracer is active (both CONFIG_IRQSOFF_TRACER and CONFIG_PREEMPT_TRACER enabled, so trace_type has both TRACER_IRQS_OFF and TRACER_PREEMPT_OFF), preempt_trace(preempt_count()) evaluates to true because preempt_count() contains HARDIRQ_OFFSET. As a result, tracer_hardirqs_on() skips stop_critical_timing(), while trace_hardirqs_on_prepare() still clears tracing_irq_cpu.
Immediately after that, irqentry_hardirq_tail_exit() calls:
irqentry_hardirq_exit_prepare()
irqentry_hardirq_count_sub_prepare()
preempt_latency_stop(HARDIRQ_OFFSET)
trace_preempt_on()
tracer_preempt_on()
In kernel/trace/trace_irqsoff.c:
void tracer_preempt_on(unsigned long a0, unsigned long a1)
{
if (preempt_trace(preempt_count()) && !irq_trace())
stop_critical_timing(a0, a1);
}Because hardware interrupts remain disabled at the CPU until the exception return instruction, irqs_disabled() is still true, so irq_trace() evaluates to true and tracer_preempt_on() also skips stop_critical_timing().
Finally, irqentry_hardirq_exit_finish() removes HARDIRQ_OFFSET via __preempt_count_sub(HARDIRQ_OFFSET). Won't this leave per_cpu(tracing_cpu, cpu) stuck active after returning to preemptible, IRQ-enabled kernel execution, as well as emitting extra preempt_disable, irq_enable, and preempt_enable trace events in inverted order?
Quoted 18 lines
kernel/softirq.c +10
-0@@ -837,6 +837,27 @@ irq_exit_wake_late_softirqs(unsigned int wake_flags)irq_exit_wake_ksoftirqd (); } +/** +* irqentry_hardirq_tail_exit - Close work and remove return-tail state +* @cookie: Staged hardirq-count cookie from irqentry_hardirq_tail_enter() +* +* Run every instrumentable teardown operation, including the final worker +* closure, before conditionally removing any applied raw hardirq count and +* ending lockdep nesting. +*/ +void noinstr +irqentry_hardirq_tail_exit (struct irqentry_hardirq_cookie cookie)
kernel/softirq.c irq_exit_wake_late_softirqs(unsigned int wake_flags) · line 859
Quoted 10 lines
kernel/softirq.c +10
-0+{ +bool do_sub; + +instrumentation_begin (); + do_sub =irqentry_hardirq_exit_prepare (cookie); +irq_exit_wake_late_softirqs (0 ); +instrumentation_end (); + +irqentry_hardirq_exit_finish (do_sub); +}
[Severity: Medium]
Can passing a hardcoded 0 to irq_exit_wake_late_softirqs(0) in irqentry_hardirq_tail_exit() cause redundant wake_up_process() calls for ksoftirqd and ktimerd?
During the earlier main hardirq exit in irq_exit_rcu_preempt_counted_prepare(), __irq_exit_rcu(true) records IRQ_EXIT_WAKE_KSOFTIRQD and IRQ_EXIT_WAKE_KTIMERD in wake_flags so irq_exit_wake_late_softirqs(wake_flags) avoids waking those threads a second time. However, local_softirq_pending() or local_timers_pending_force_th() remains non-zero until the woken thread actually runs on the CPU and clears the pending bits.
If irqentry_exit_to_kernel_mode_preempt() does not switch to the worker thread (for example, on any interrupt from an RCU-idle context where state.exit_rcu is true, when the interrupted kernel context has preemption disabled, when regs_irqs_disabled(regs) is true, or when current has higher priority), irqentry_hardirq_tail_exit() runs with those pending bits still set:
irq_exit_wake_late_softirqs(0)
if (!ksoftirqd_wake_requested)
ksoftirqd_wake_requested = irq_exit_wake_ksoftirqd();
if (!ktimerd_wake_requested)
ktimerd_wake_requested = irq_exit_wake_timersd();Because wake_flags is 0, won't this call wakeup_softirqd() or wake_timersd() a second time even when no return-tail callback raised new work, and do so after trace_hardirqs_on_prepare(), ct_irq_exit_prepare(), and irqentry_hardirq_exit_prepare() have already completed?
[Severity: Medium]
What happens if a return-tail callback raises softirq or forced-thread timer work while returning to a preemptible kernel context?
In irqentry_exit_to_kernel_mode():
instrumentation_begin(); irqentry_exit_to_kernel_mode_preempt(regs, state); instrumentation_end(); irqentry_hardirq_tail_enter(&state); irqentry_exit_to_kernel_mode_after_preempt(regs, state);
the preemption check in irqentry_exit_to_kernel_mode_preempt() runs before the hardirq return tail is entered.
If a callback inside the return tail (such as during hrtimer_rearm_deferred(), trace_hardirqs_on_prepare(), lockdep_hardirqs_on_prepare_irqtail(), or irqentry_hardirq_exit_prepare()) raises softirq or timer work, irq_exit_wake_late_softirqs(0) in irqentry_hardirq_tail_exit() wakes ksoftirqd or ktimerd while HARDIRQ_OFFSET is still set in preempt_count(), setting need_resched() on current. Immediately afterwards, irqentry_hardirq_exit_finish() drops HARDIRQ_OFFSET via __preempt_count_sub(HARDIRQ_OFFSET) without checking for rescheduling.
Could this return to a preemptible kernel task with need_resched() set and miss preempting for the newly woken worker thread (including a higher-priority SCHED_FIFO ktimerd/ksoftirqd under PREEMPT_RT or force_irqthreads)?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=10
RFC PATCH v2 11/13 arm64: Use the pre-counted hardirq lifecycle
In reply to Karl MehltretterOrdinary EL0 and EL1 interrupt paths establish generic entry state before irq_enter_rcu() adds HARDIRQ_OFFSET and lockdep nesting. Callbacks in that early window are therefore not classified as hardirq work. Their exit paths also drop the classification before function-graph return callbacks and the kernel return tail have completed.
After the EL1 pseudo-NMI decision, enter ordinary interrupts through irqentry_hardirq_enter(). Finish the staged count update and delayed timer and accounting work with irq_enter_rcu_preempt_counted(). Use the retained exit preparation followed immediately by conditional raw count and lockdep teardown.
For kernel returns, preserve the existing per-CPU register adjustment, then stage hardirq return-tail classification before MTE exit tracing. Keep the pseudo-NMI lifecycle unchanged.
The noinstr path also depends on the raw preempt-count and __kern_my_cpu_offset() inlining prerequisites earlier in this series.
--- arch/arm64/kernel/entry-common.c | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-)
arch/arm64/kernel/entry-common.c +22 -6
@@ -94,6 +94,8 @@ static void noinstr __arm64_exit_to_kernel_mode(struct pt_regs *regs, {local_daif_mask ();irqentry_exit_pcpu_adjust (regs); +/* arm64 uses the split exit API, so enter the tail before MTE tracing. */ +irqentry_hardirq_tail_enter (&state);mte_check_tfsr_exit ();irqentry_exit_to_kernel_mode_after_preempt (regs, state); }
@@ -560,13 +562,19 @@ static __always_inline void __el1_pnmi(struct pt_regs *regs, static __always_inline void __el1_irq(struct pt_regs *regs,void (*handler)(struct pt_regs *)) { +struct irqentry_hardirq_cookie cookie;irqentry_state_t state; +bool do_sub; - state =arm64_enter_from_kernel_mode (regs); + state =irqentry_hardirq_enter (regs); + cookie =irqentry_hardirq_count_state (state); +mte_check_tfsr_entry (); +mte_disable_tco_entry (current); -irq_enter_rcu (); +irq_enter_rcu_preempt_counted (cookie);do_interrupt_handler (regs, handler); -irq_exit_rcu (); + do_sub =irq_exit_rcu_preempt_counted_prepare (cookie); +irqentry_hardirq_exit_finish (do_sub);arm64_exit_to_kernel_mode_preempt (regs, state); }
@@ -877,16 +885,24 @@ asmlinkage void noinstr el0t_64_sync_handler(struct pt_regs *regs) static void noinstr el0_interrupt(struct pt_regs *regs,void (*handler)(struct pt_regs *)) { -arm64_enter_from_user_mode (regs); +struct irqentry_hardirq_cookie cookie; +irqentry_state_t state; +bool do_sub; + + state =irqentry_hardirq_enter (regs); + cookie =irqentry_hardirq_count_state (state); +mte_disable_tco_entry (current); +sme_enter_from_user_mode ();write_sysreg (DAIF_PROCCTX_NOIRQ , daif);if (regs->pc &BIT (55 ))arm64_apply_bp_hardening (); -irq_enter_rcu (); +irq_enter_rcu_preempt_counted (cookie);do_interrupt_handler (regs, handler); -irq_exit_rcu (); + do_sub =irq_exit_rcu_preempt_counted_prepare (cookie); +irqentry_hardirq_exit_finish (do_sub);arm64_exit_to_user_mode (regs); }
Signature · 1 line
2.39.5 (Apple Git-154)
RFC PATCH v2 12/13 Documentation: entry: Describe the pre-counted hardirq lifecycle
In reply to Karl MehltretterThe entry documentation covers only the traditional irq_enter_rcu() path. It does not explain the early-counted entry contract or the retained and synthetic hardirq exit phases.
Document the staged raw count and lockdep entry, delayed traceable bookkeeping and rejected-add handling, hardirq accounting, hardirq-to-softirq classification switch, retained real IRQ exit, possible preemption and migration, and the classification-only return tail. Also cover late worker closure, conditional raw teardown and context-tracking order.
Record that arm64 excludes pseudo-NMIs from this lifecycle and identify its external noinstr inlining prerequisites.
--- Documentation/core-api/entry.rst | 89 ++++++++++++++++++++++++++++---- 1 file changed, 79 insertions(+), 10 deletions(-)
Documentation/core-api/entry.rst +79 -10
@@ -193,21 +193,14 @@ Note that the invocation of the actual interrupt handler is within a irq_enter_rcu() and irq_exit_rcu() pair. irq_enter_rcu() updates the preemption count which makes in_hardirq() -return true, handles NOHZ tick state and interrupt time accounting. This -means that up to the point where irq_enter_rcu() is invoked in_hardirq() -returns false. +return true, handles NOHZ tick state and interrupt time accounting. On this +entry path, in_hardirq() therefore returns false until irq_enter_rcu() is +invoked. irq_exit_rcu() handles interrupt time accounting, handles soft interrupts if possible, handles the NOHZ tick state and finally undoes the preemption count update. -In theory, the preemption count could be updated in irqentry_enter(). In -practice, deferring this update to irq_enter_rcu() allows the preemption-count -code to be traced, while also maintaining symmetry with irq_exit_rcu() and -irqentry_exit(), which are described in the next paragraph. The only downside -is that the early entry code up to irq_enter_rcu() must be aware that the -preemption count has not yet been updated with the HARDIRQ_OFFSET state. - Note that soft interrupt handlers must run in BH context rather than in hard interrupt context. irq_exit_rcu() therefore replaces HARDIRQ_OFFSET with SOFTIRQ_OFFSET in the preemption count while it handles soft interrupts and
@@ -216,6 +209,82 @@ is still attributed to the interrupt. HARDIRQ_OFFSET is removed before irq_exit_rcu() returns because irqentry_exit() might schedule, which requires that HARDIRQ_OFFSET has been removed from the preemption count. +Pre-counted hardirq lifecycle +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Architectures which need hardirq state before early context-tracking or +tracing callbacks can use the pre-counted lifecycle. It applies only to an +ordinary hard interrupt after the architecture has distinguished it from an +NMI-like event: + + * irqentry_hardirq_enter() attempts to add HARDIRQ_OFFSET with a raw update + and records whether the update was applied. It establishes IRQ-disabled + and lockdep hardirq state before instrumentable entry work. It then runs + the generic entry and context-tracking path, including any required + transition out of an RCU extended quiescent state. + + * irq_enter_rcu_preempt_counted() later finishes the staged attempt. For an + applied update it performs traceable debug and latency bookkeeping. For a + rejected update it attempts deferred reporting and skips post-add + bookkeeping. It also performs the deferred timer repair, NOHZ tick + handling and interrupt accounting from irq_enter_rcu(). + + * irq_exit_rcu_preempt_counted_prepare() performs interrupt accounting, + softirq handling and NOHZ tick handling while carrying any applied + hardirq count across the traceable exit path. When an applied outer + hardirq invokes softirqs, both the preempt-count and lockdep + classifications temporarily switch from hardirq to softirq and back. The + architecture immediately follows the preparation with + irqentry_hardirq_exit_finish(), which performs the conditional raw-count + subtraction and lockdep teardown. + +The staged addition carries an opaque state into its instrumentable finish and +matching teardown. With DEBUG_PREEMPT and no separate reschedule bits, an +already-negative count follows the legacy DEBUG_LOCKS_WARN_ON() rule. During +an oops the warning condition is bypassed and the addition is applied. +Otherwise the attempt is rejected and leaves the count unchanged. Its finish +attempts deferred reporting, which an intervening oops can suppress. A +rejected token always skips post-add bookkeeping and the matching subtraction, +independently of whether reporting produced a warning. + +The real interrupt is complete after that teardown. Return-to-kernel +preemption can schedule and migrate the task before the architecture-specific +tail runs, so the current preemption count cannot identify the original entry +class. irqentry_state_t retains that provenance instead. + +For an ordinary hardirq returning to kernel mode, +irqentry_hardirq_tail_enter() makes a fresh staged attempt to add +HARDIRQ_OFFSET and establishes lockdep hardirq nesting after the preemption +decision. This synthetic classification covers traceable architecture tail +work, deferred timer rearming, IRQ-on preparation and context-tracking +preparation. It does not repeat interrupt accounting, RCU or tick entry. +IRQ-on preparation requires this synthetic lockdep nesting to remain present. +An interrupted outer hardirq can contribute additional nesting. + +irqentry_hardirq_tail_exit() performs the final traceable teardown preparation +and then conditionally removes any applied raw synthetic count. For an applied +staged count at outermost hardirq depth, both retained exit points recheck +regular softirq work and forced-thread timer work raised by late callbacks and +wake the corresponding workers before ending hardirq state. If the interrupted +context was in an RCU extended quiescent state, +ct_irq_exit_prepare() runs before the raw tail teardown and +ct_irq_exit_finish() completes the transition afterwards. + +On arm64 the ordinary lifecycle is selected only after the pseudo-NMI +decision. Pseudo-NMIs continue to use irqentry_nmi_enter() and +irqentry_nmi_exit(). All raw transitions are in noinstr paths, so +preempt_count(), the raw preempt-count update helpers and +__kern_my_cpu_offset() must remain inline. The arm64 conversion includes the +first requirement and depends on the `arm64 raw preempt-count inlining +prerequisite`_ and `arm64 per-CPU offset inlining prerequisite`_ for the +others. + +.. _`arm64 raw preempt-count inlining prerequisite`: + https://lore.kernel.org/all/c3a90368dd6c096af9d918e036a27728696fb8a4.1786603168.git.hongyan.xia@transsion.com/ + +.. _`arm64 per-CPU offset inlining prerequisite`: + https://lore.kernel.org/r/[email protected] + Even though interrupt handlers are expected to run with local interrupts disabled, interrupt nesting is common from an entry/exit perspective. For example, softirq handling happens within an irqentry_{enter,exit}() block with
Signature · 1 line
2.39.5 (Apple Git-154)
Re: [RFC PATCH v2 12/13] Documentation: entry: Describe the pre-counted hardirq lifecycle
Quoted 1 line · Karl Mehltretter
Hi Karl,
Quoted 2 lines
Assisted-by LLM---
Signed-off-by is missing. Is this an oversight? See Documentation/process/submitting-patches.rst.
Also, "Assisted-by: LLM" was removed from the docs by 1e035297ffda. Maybe describe the tool use in the changelog instead, see Documentation/process/generated-content.rst?
Thanks,
Daniel
Steven Rostedt
maintainerRe: [RFC PATCH v2 12/13] Documentation: entry: Describe the pre-counted hardirq lifecycle
Quoted 7 lines · On Mon, 2 Nov 2026 23:39:28 -0300 Daniel Pereira <[email protected]>…
Quoted 1 line · Karl Mehltretter
Hi Karl,
Quoted 2 lines
Assisted-by LLM---
Quoted 6 lines
Signed-off-by is missing. Is this an oversight? See
Documentation/process/submitting-patches.rst.Also, "Assisted-by: LLM" was removed from the docs by
1e035297ffda. Maybe describe the tool use in the changelog instead, seeDocumentation/process/generated-content.rst?
It's not a requirement anymore but there's nothing saying you can't have it.
I personally prefer the tag (even the old one where it said what LLM was used).
-- Steve
Re: [RFC PATCH v2 12/13] Documentation: entry: Describe the pre-counted hardirq lifecycle
Quoted 6 lines · On Tue, 3 Nov 2026 11:24:58 -0300 Steven Rostedt <[email protected]> wrote:
Quoted 3 lines · On Mon, 2 Nov 2026 23:39:28 -0300 Daniel Pereira <[email protected]>…
Signed-off-by is missing. Is this an oversight? See
Documentation/process/submitting-patches.rst.
Quoted 7 lines
Quoted 3 lines
Also, "Assisted-by: LLM" was removed from the docs by
1e035297ffda. Maybe describe the tool use in the changelog instead, seeDocumentation/process/generated-content.rst?It's not a requirement anymore but there's nothing saying you can't have it.
I personally prefer the tag (even the old one where it said what LLM was used).
Hi Steve,
Thanks for clarifying.
My main concern is the missing Signed-off-by line, which this patch needs before it can be considered.
Karl, could you please add it in the next version?
Thanks,
Daniel
RFC PATCH v2 13/13 locking/selftest: Exercise hardirq return-tail preparation
In reply to Karl MehltretterThe return-tail lockdep preparation must see a live hardirq context. A zero depth must fail. Depth one and nested contexts must pass. The ordinary preparation helper must continue to reject a live hardirq context.
Add focused boot-time lockdep cases for those four states. Skip the final IRQ-state transition after an expected preparation failure so that each failure remains attributable to the preparation invariant.
--- lib/locking-selftest.c | 53 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+)
lib/locking-selftest.c +53 -0
@@ -222,6 +222,55 @@ static void init_shared_classes(void)local_irq_enable (); \local_bh_enable (); +static void irqtail_prepare_test_enter (unsigned int nr) +{ +unsigned int i; + +local_irq_disable (); +for (i =0 ; i < nr; i++) +__irq_enter_raw (); +trace_hardirqs_on_prepare (); +} + +static void irqtail_prepare_test_exit (unsigned int nr) +{ +unsigned int i; + +for (i =0 ; i < nr; i++) +__irq_exit_raw (); +if (debug_locks) +lockdep_hardirqs_on (_THIS_IP_); +raw_local_irq_enable (); +} + +static void prepare_irqtail_0 (void ) +{ +irqtail_prepare_test_enter (0 ); +lockdep_hardirqs_on_prepare_irqtail (); +irqtail_prepare_test_exit (0 ); +} + +static void prepare_irqtail_1 (void ) +{ +irqtail_prepare_test_enter (1 ); +lockdep_hardirqs_on_prepare_irqtail (); +irqtail_prepare_test_exit (1 ); +} + +static void prepare_irqtail_2 (void ) +{ +irqtail_prepare_test_enter (2 ); +lockdep_hardirqs_on_prepare_irqtail (); +irqtail_prepare_test_exit (2 ); +} + +static void prepare_regular_1 (void ) +{ +irqtail_prepare_test_enter (1 ); +lockdep_hardirqs_on_prepare (); +irqtail_prepare_test_exit (1 ); +} +/* * Shortcuts for lock/unlock API variants, to keep * the testcases compact:
@@ -2934,6 +2983,10 @@ void locking_selftest(void)DO_TESTCASE_6x2x2RW ("irq read-recursion" , irq_read_recursion);DO_TESTCASE_6x2x2RW ("irq read-recursion #2" , irq_read_recursion2);DO_TESTCASE_6x2x2RW ("irq read-recursion #3" , irq_read_recursion3); +DO_TESTCASE_1B ("IRQ tail prepare" , prepare_irqtail,0 ); +DO_TESTCASE_1 ("IRQ tail prepare" , prepare_irqtail,1 ); +DO_TESTCASE_1 ("IRQ tail prepare" , prepare_irqtail,2 ); +DO_TESTCASE_1B ("regular IRQ prepare" , prepare_regular,1 );ww_tests ();
Signature · 1 line
2.39.5 (Apple Git-154)