Skip to content

Commit 61c39d8

Browse files
Ryo TakakuraIngo Molnar
authored andcommitted
lockdep: Fix wait context check on softirq for PREEMPT_RT
Since: 0c1d7a2 ("lockdep: Remove softirq accounting on PREEMPT_RT.") the wait context test for mutex usage within "in softirq context" fails as it references @softirq_context: | wait context tests | -------------------------------------------------------------------------- | rcu | raw | spin |mutex | -------------------------------------------------------------------------- in hardirq context: ok | ok | ok | ok | in hardirq context (not threaded): ok | ok | ok | ok | in softirq context: ok | ok | ok |FAILED| As a fix, add lockdep map for BH disabled section. This fixes the issue by letting us catch cases when local_bh_disable() gets called with preemption disabled where local_lock doesn't get acquired. In the case of "in softirq context" selftest, local_bh_disable() was being called with preemption disable as it's early in the boot. [ boqun: Move the lockdep annotations into __local_bh_*() to avoid false positives because of unpaired local_bh_disable() reported by Borislav Petkov and Peter Zijlstra, and make bh_lock_map only exist for PREEMPT_RT. ] [ mingo: Restored authorship and improved the bh_lock_map definition. ] Signed-off-by: Ryo Takakura <ryotkkr98@gmail.com> Signed-off-by: Boqun Feng <boqun.feng@gmail.com> Signed-off-by: Ingo Molnar <mingo@kernel.org> Link: https://lore.kernel.org/r/20250321143322.79651-1-boqun.feng@gmail.com
1 parent 0e1ff67 commit 61c39d8

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

kernel/softirq.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,18 @@ static DEFINE_PER_CPU(struct softirq_ctrl, softirq_ctrl) = {
126126
.lock = INIT_LOCAL_LOCK(softirq_ctrl.lock),
127127
};
128128

129+
#ifdef CONFIG_DEBUG_LOCK_ALLOC
130+
static struct lock_class_key bh_lock_key;
131+
struct lockdep_map bh_lock_map = {
132+
.name = "local_bh",
133+
.key = &bh_lock_key,
134+
.wait_type_outer = LD_WAIT_FREE,
135+
.wait_type_inner = LD_WAIT_CONFIG, /* PREEMPT_RT makes BH preemptible. */
136+
.lock_type = LD_LOCK_PERCPU,
137+
};
138+
EXPORT_SYMBOL_GPL(bh_lock_map);
139+
#endif
140+
129141
/**
130142
* local_bh_blocked() - Check for idle whether BH processing is blocked
131143
*
@@ -148,6 +160,8 @@ void __local_bh_disable_ip(unsigned long ip, unsigned int cnt)
148160

149161
WARN_ON_ONCE(in_hardirq());
150162

163+
lock_map_acquire_read(&bh_lock_map);
164+
151165
/* First entry of a task into a BH disabled section? */
152166
if (!current->softirq_disable_cnt) {
153167
if (preemptible()) {
@@ -211,6 +225,8 @@ void __local_bh_enable_ip(unsigned long ip, unsigned int cnt)
211225
WARN_ON_ONCE(in_hardirq());
212226
lockdep_assert_irqs_enabled();
213227

228+
lock_map_release(&bh_lock_map);
229+
214230
local_irq_save(flags);
215231
curcnt = __this_cpu_read(softirq_ctrl.cnt);
216232

@@ -261,6 +277,8 @@ static inline void ksoftirqd_run_begin(void)
261277
/* Counterpart to ksoftirqd_run_begin() */
262278
static inline void ksoftirqd_run_end(void)
263279
{
280+
/* pairs with the lock_map_acquire_read() in ksoftirqd_run_begin() */
281+
lock_map_release(&bh_lock_map);
264282
__local_bh_enable(SOFTIRQ_OFFSET, true);
265283
WARN_ON_ONCE(in_interrupt());
266284
local_irq_enable();

0 commit comments

Comments
 (0)