softirq: Preserve interrupt context during IRQ exit
Subsystems (2)
PATCH v4 1/1 softirq: Preserve interrupt context during IRQ exit
On the return from interrupt path, __irq_exit_rcu() removes HARDIRQ_OFFSET from the preemption counter at the very top of the function. Everything after that reports the current context as task instead of hard interrupt. The code in the function itself, such as invoke_softirq(), is aware of this and does not rely on the counter. Everything else which derives the context from preempt_count gets it wrong in that window:
- ftrace, perf and the ring buffer record task context and use the task recursion and context slots.
- KCSAN attributes the accesses to the interrupted task, KMSAN uses and changes its state. KCOV and the printk caller id see a task.
- On PREEMPT_RT can_spin_trylock() and local_trylock() reject hard interrupt context to avoid interfering with PI when the interrupted task is blocked on a lock. That check does not reject calls made in this window. BPF programs attached to sched_waking or sched_wakeup can reach it through kmalloc_nolock().
- An oops kills the interrupted task instead of ending in "Fatal exception in interrupt".
Tracing and the sanitizers see the wrong context in this window. No failure caused by this misclassification is known. The early removal of HARDIRQ_OFFSET predates git. lockdep is not affected because lockdep_hardirq_exit() is the last operation in irq_exit().
Keep HARDIRQ_OFFSET until right before tick_irq_exit(), which needs in_hardirq() to be false for the outermost interrupt. Softirq handlers must not run with HARDIRQ_OFFSET set, so softirq_handle_begin() replaces it with SOFTIRQ_OFFSET and softirq_handle_end() reverts that, each in a single raw preempt_count update. The raw operations keep the preemption disable location recorded by irq_enter_rcu(), and lockdep is updated by hand. softirq_handle_begin() detects the case with in_hardirq() because __do_softirq() is reached through the stack switch in do_softirq_own_stack() and cannot take an argument.
The checks run before HARDIRQ_OFFSET is removed. !in_interrupt() becomes irq_count() == HARDIRQ_OFFSET, as in irq_enter_rcu(). The timer thread check becomes !in_nmi() && hardirq_count() == HARDIRQ_OFFSET. It does not test softirq_count(): the timer thread must also wake when the interrupt hit softirq processing or a section with BHs disabled. A softirq raised in the timer thread wakeup is handled by the timer thread, which handles all pending softirqs.
A softirq raised from a tracepoint on the final preempt_count_sub() waits for the next interrupt exit and can trigger NOHZ tick-stop warnings meanwhile. That is not the normal path and does not justify a check on every interrupt exit. A tracepoint on tick_irq_exit() already behaves the same way.
The number of preempt_count updates and the interrupt time accounting are unchanged. The preemptoff tracer now reports the interrupt and the softirq processing on top of it as one section, and function graph with nofuncgraph-irqs also skips the interrupt exit work, including the __do_softirq() frame.
---
Notes:
Thanks for the review, Sebastian, and for the Reviewed-by, Bradley.
Changes in v4:
- Drop the ksoftirqd wakeup after the final preempt_count_sub(). A tracepoint consumer raising a softirq there is not a path to cater for (Sebastian).
- Reword the first changelog paragraph (Sebastian).
- Rebase on v7.3-rc4, which contains the prerequisites a155ac8f0c52 and 2af470916a20. tip/irq/core does not have them yet.
- Keep the Reviewed-by from v3. The only code change is the removal above.
Testing: the v4 code is the v3 code without the trailing check, which is the state that ran on the x86-64 and arm64 QEMU matrix, the SAM9X75 (also RT) and the Raspberry Pi 500+ (also RT) before v3 was posted. The Pi 400 ran the variant with the check. On v7.3-rc4, base against v4 booted and stressed cleanly in QEMU on arm64 (virt, SMP, lockdep) and arm32 (versatilepb, lockdep).
v3: https://lore.kernel.org/r/[email protected]
v2: https://lore.kernel.org/r/[email protected]
Documentation/core-api/entry.rst | 16 +++++--- kernel/softirq.c | 65 ++++++++++++++++++++++++++------ 2 files changed, 63 insertions(+), 18 deletions(-)
Documentation/core-api/entry.rst +10 -6
@@ -197,8 +197,9 @@ 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. -irq_exit_rcu() handles interrupt time accounting, undoes the preemption -count update and eventually handles soft interrupts and NOHZ tick state. +irq_exit_rcu() handles interrupt time accounting, handles soft interrupts if +possible, undoes the preemption count update and finally handles the NOHZ tick +state. In theory, the preemption count could be updated in irqentry_enter(). In practice, deferring this update to irq_enter_rcu() allows the preemption-count
@@ -207,10 +208,13 @@ 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 irq_exit_rcu() must remove HARDIRQ_OFFSET from the preemption count -before it handles soft interrupts, whose handlers must run in BH context rather -than irq-disabled context. In addition, irqentry_exit() might schedule, which -also requires that HARDIRQ_OFFSET has been removed from the preemption count. +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 +puts HARDIRQ_OFFSET back afterwards, so that the remaining interrupt exit work +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. Even though interrupt handlers are expected to run with local interrupts disabled, interrupt nesting is common from an entry/exit perspective. For
kernel/softirq.c +53 -12
114 lines not on this page · raw message
base-commit: 93f51579e7df248780214094418f205253383cc5
Signature · 1 line
2.39.5 (Apple Git-154)
Quoted 2 lines · Karl Mehltretter
On the return from interrupt path,
__irq_exit_rcu()removesHARDIRQ_OFFSETfrom the preemption counter at the very top of the
…
Quoted 6 lines
Suggested-by Peter Zijlstra <[email protected]>Assisted-by LLMSigned-off-by Karl Mehltretter <[email protected]>Reviewed-by Bradley Morgan <[email protected]>---
but…
Quoted 5 lines
kernel/softirq.c +0
-0@@ -758,10 +788,21 @@ static inline void __irq_exit_rcu(void)invoke_softirq (); }
kernel/softirq.c static inline void __irq_exit_rcu(void) · line 798
Quoted 10 lines
kernel/softirq.c +7
-1+/* +* Wake the timer thread even if the interrupt hit a softirq or a +* section with BHs disabled. Only nested interrupts and NMIs are +* excluded. +*/ if (IS_ENABLED (CONFIG_IRQ_FORCED_THREADING ) &&force_irqthreads () && -local_timers_pending_force_th () && !(in_nmi () |in_hardirq ())) +local_timers_pending_force_th () && + !in_nmi () &&hardirq_count () ==HARDIRQ_OFFSET )
could you please do
(in_nmi() | hardirq_count()) == HARDIRQ_OFFSET)
instead? This will merge NMI check into the hardirq check, dropping one conditional jump away.
kernel/softirq.c static inline void __irq_exit_rcu(void) · line 807
Quoted 9 lines
kernel/softirq.c +5
-0wake_timersd (); +/* +* tick_irq_exit() relies on in_hardirq() being false for the +* outermost interrupt. +*/ +preempt_count_sub (HARDIRQ_OFFSET );tick_irq_exit (); }
Sebastian
Quoted 5 lines · Sebastian Andrzej Siewior
could you please do
(in_nmi() | hardirq_count()) == HARDIRQ_OFFSET)instead? This will merge NMI check into the hardirq check, dropping one conditional jump away.
Hello Sebastian,
Good catch. With GCC it removes one conditional branch on x86-64, arm64 and arm32. On x86-64 it also uses three fewer instructions.
I will send this tweaked version soon.
Thanks for the review!
Karl