From: Gabriele Monaco <gmonaco@redhat.com>
To: linux-kernel@vger.kernel.org,
Thomas Gleixner <tglx@linutronix.de>,
Peter Zijlstra <peterz@infradead.org>,
Andy Lutomirski <luto@kernel.org>, Ingo Molnar <mingo@kernel.org>
Cc: Gabriele Monaco <gmonaco@redhat.com>,
Steven Rostedt <rostedt@goodmis.org>,
Masami Hiramatsu <mhiramat@kernel.org>,
linux-trace-kernel@vger.kernel.org
Subject: [PATCH] lockdep: Fix inconsistency in irq tracking on NMIs
Date: Fri, 20 Jun 2025 14:51:13 +0200 [thread overview]
Message-ID: <20250620125112.33978-2-gmonaco@redhat.com> (raw)
The irq_enable/irq_disable tracepoints fire only when there's an actual
transition (enabled->disabled and vice versa), this needs special care
in NMIs, as they could potentially start with IRQs already disabled.
The current implementation takes care of this by tracking the lockdep
state before the NMI (on nmi_entry) and not tracing on nmi_exit in case
IRQs were already disabled, we don't trace on nmi_entry following the
tracing_irq_cpu variable, which can be racy:
local_irq_enable()
void trace_hardirqs_on(void)
{
if (tracing_irq_cpu) {
trace(irq_enable);
tracing_irq_cpu = 0;
}
/*
* NMI here
* tracing_irq_cpu == 0 (done tracing)
* lockdep_hardirqs_enabled == 0 (IRQs still disabled)
*/
irqentry_nmi_enter()
irq_state.lockdep = 0
trace(irq_disable);
irqentry_nmi_exit()
// irq_state.lockdep == 0
// do not trace(irq_enable)
lockdep_hardirqs_on();
}
The error is visible with the sncid RV monitor and particularly likely
on machines with the following setup:
- x86 bare-metal with 40+ CPUs
- tuned throughput-performance (activating regular perf NMIs)
- workload: stress-ng --cpu-sched 21 --timer 11 --signal 11
The presence of the RV monitor is useful to see the error but it is not
necessary to trigger it.
Prevent this scenario by checking lockdep_hardirqs_enabled to trace also
on nmi_entry.
Fixes: ba1f2b2eaa2a ("x86/entry: Fix NMI vs IRQ state tracking")
Cc: Steven Rostedt <rostedt@goodmis.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: linux-trace-kernel@vger.kernel.org
Signed-off-by: Gabriele Monaco <gmonaco@redhat.com>
---
kernel/entry/common.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/kernel/entry/common.c b/kernel/entry/common.c
index a8dd1f27417cf..7369132c00ba4 100644
--- a/kernel/entry/common.c
+++ b/kernel/entry/common.c
@@ -326,13 +326,15 @@ irqentry_state_t noinstr irqentry_nmi_enter(struct pt_regs *regs)
irq_state.lockdep = lockdep_hardirqs_enabled();
__nmi_enter();
- lockdep_hardirqs_off(CALLER_ADDR0);
+ if (irq_state.lockdep)
+ lockdep_hardirqs_off(CALLER_ADDR0);
lockdep_hardirq_enter();
ct_nmi_enter();
instrumentation_begin();
kmsan_unpoison_entry_regs(regs);
- trace_hardirqs_off_finish();
+ if (irq_state.lockdep)
+ trace_hardirqs_off_finish();
ftrace_nmi_enter();
instrumentation_end();
base-commit: 75f5f23f8787c5e184fcb2fbcd02d8e9317dc5e7
--
2.49.0
next reply other threads:[~2025-06-20 12:52 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-06-20 12:51 Gabriele Monaco [this message]
2025-06-20 21:07 ` [PATCH] lockdep: Fix inconsistency in irq tracking on NMIs Thomas Gleixner
2025-06-21 8:57 ` Peter Zijlstra
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250620125112.33978-2-gmonaco@redhat.com \
--to=gmonaco@redhat.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-trace-kernel@vger.kernel.org \
--cc=luto@kernel.org \
--cc=mhiramat@kernel.org \
--cc=mingo@kernel.org \
--cc=peterz@infradead.org \
--cc=rostedt@goodmis.org \
--cc=tglx@linutronix.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).