Skip to content

Commit 893cdaa

Browse files
walacPeter Zijlstra
authored andcommitted
sched: avoid false lockdep splat in put_task_struct()
In put_task_struct(), a spin_lock is indirectly acquired under the kernel stock. When running the kernel in real-time (RT) configuration, the operation is dispatched to a preemptible context call to ensure guaranteed preemption. However, if PROVE_RAW_LOCK_NESTING is enabled and __put_task_struct() is called while holding a raw_spinlock, lockdep incorrectly reports an "Invalid lock context" in the stock kernel. This false splat occurs because lockdep is unaware of the different route taken under RT. To address this issue, override the inner wait type to prevent the false lockdep splat. Suggested-by: Oleg Nesterov <[email protected]> Suggested-by: Sebastian Andrzej Siewior <[email protected]> Suggested-by: Peter Zijlstra <[email protected]> Signed-off-by: Wander Lairson Costa <[email protected]> Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent d243b34 commit 893cdaa

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

include/linux/sched/task.h

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,19 @@ static inline void put_task_struct(struct task_struct *t)
125125
if (!refcount_dec_and_test(&t->usage))
126126
return;
127127

128+
/*
129+
* In !RT, it is always safe to call __put_task_struct().
130+
* Under RT, we can only call it in preemptible context.
131+
*/
132+
if (!IS_ENABLED(CONFIG_PREEMPT_RT) || preemptible()) {
133+
static DEFINE_WAIT_OVERRIDE_MAP(put_task_map, LD_WAIT_SLEEP);
134+
135+
lock_map_acquire_try(&put_task_map);
136+
__put_task_struct(t);
137+
lock_map_release(&put_task_map);
138+
return;
139+
}
140+
128141
/*
129142
* under PREEMPT_RT, we can't call put_task_struct
130143
* in atomic context because it will indirectly
@@ -145,10 +158,7 @@ static inline void put_task_struct(struct task_struct *t)
145158
* when it fails to fork a process. Therefore, there is no
146159
* way it can conflict with put_task_struct().
147160
*/
148-
if (IS_ENABLED(CONFIG_PREEMPT_RT) && !preemptible())
149-
call_rcu(&t->rcu, __put_task_struct_rcu_cb);
150-
else
151-
__put_task_struct(t);
161+
call_rcu(&t->rcu, __put_task_struct_rcu_cb);
152162
}
153163

154164
DEFINE_FREE(put_task, struct task_struct *, if (_T) put_task_struct(_T))

0 commit comments

Comments
 (0)