Skip to content

Commit a4f0b6f

Browse files
Valentin SchneiderPeter Zijlstra
authored andcommitted
locking/mutex: Add p->blocked_on wrappers for correctness checks
This lets us assert mutex::wait_lock is held whenever we access p->blocked_on, as well as warn us for unexpected state changes. [fix conflicts, call in more places] [jstultz: tweaked commit subject, reworked a good bit] Signed-off-by: Valentin Schneider <[email protected]> Signed-off-by: Connor O'Brien <[email protected]> Signed-off-by: John Stultz <[email protected]> Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Tested-by: K Prateek Nayak <[email protected]> Link: https://lkml.kernel.org/r/[email protected]
1 parent 44e4e02 commit a4f0b6f

File tree

4 files changed

+81
-27
lines changed

4 files changed

+81
-27
lines changed

include/linux/sched.h

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include <linux/sched/prio.h>
3535
#include <linux/sched/types.h>
3636
#include <linux/signal_types.h>
37+
#include <linux/spinlock.h>
3738
#include <linux/syscall_user_dispatch_types.h>
3839
#include <linux/mm_types_task.h>
3940
#include <linux/netdevice_xmit.h>
@@ -2129,6 +2130,67 @@ extern int __cond_resched_rwlock_write(rwlock_t *lock);
21292130
__cond_resched_rwlock_write(lock); \
21302131
})
21312132

2133+
#ifndef CONFIG_PREEMPT_RT
2134+
static inline struct mutex *__get_task_blocked_on(struct task_struct *p)
2135+
{
2136+
struct mutex *m = p->blocked_on;
2137+
2138+
if (m)
2139+
lockdep_assert_held_once(&m->wait_lock);
2140+
return m;
2141+
}
2142+
2143+
static inline void __set_task_blocked_on(struct task_struct *p, struct mutex *m)
2144+
{
2145+
WARN_ON_ONCE(!m);
2146+
/* The task should only be setting itself as blocked */
2147+
WARN_ON_ONCE(p != current);
2148+
/* Currently we serialize blocked_on under the mutex::wait_lock */
2149+
lockdep_assert_held_once(&m->wait_lock);
2150+
/*
2151+
* Check ensure we don't overwrite existing mutex value
2152+
* with a different mutex. Note, setting it to the same
2153+
* lock repeatedly is ok.
2154+
*/
2155+
WARN_ON_ONCE(p->blocked_on && p->blocked_on != m);
2156+
p->blocked_on = m;
2157+
}
2158+
2159+
static inline void set_task_blocked_on(struct task_struct *p, struct mutex *m)
2160+
{
2161+
guard(raw_spinlock_irqsave)(&m->wait_lock);
2162+
__set_task_blocked_on(p, m);
2163+
}
2164+
2165+
static inline void __clear_task_blocked_on(struct task_struct *p, struct mutex *m)
2166+
{
2167+
WARN_ON_ONCE(!m);
2168+
/* Currently we serialize blocked_on under the mutex::wait_lock */
2169+
lockdep_assert_held_once(&m->wait_lock);
2170+
/*
2171+
* There may be cases where we re-clear already cleared
2172+
* blocked_on relationships, but make sure we are not
2173+
* clearing the relationship with a different lock.
2174+
*/
2175+
WARN_ON_ONCE(m && p->blocked_on && p->blocked_on != m);
2176+
p->blocked_on = NULL;
2177+
}
2178+
2179+
static inline void clear_task_blocked_on(struct task_struct *p, struct mutex *m)
2180+
{
2181+
guard(raw_spinlock_irqsave)(&m->wait_lock);
2182+
__clear_task_blocked_on(p, m);
2183+
}
2184+
#else
2185+
static inline void __clear_task_blocked_on(struct task_struct *p, struct rt_mutex *m)
2186+
{
2187+
}
2188+
2189+
static inline void clear_task_blocked_on(struct task_struct *p, struct rt_mutex *m)
2190+
{
2191+
}
2192+
#endif /* !CONFIG_PREEMPT_RT */
2193+
21322194
static __always_inline bool need_resched(void)
21332195
{
21342196
return unlikely(tif_need_resched());
@@ -2168,8 +2230,6 @@ extern bool sched_task_on_rq(struct task_struct *p);
21682230
extern unsigned long get_wchan(struct task_struct *p);
21692231
extern struct task_struct *cpu_curr_snapshot(int cpu);
21702232

2171-
#include <linux/spinlock.h>
2172-
21732233
/*
21742234
* In order to reduce various lock holder preemption latencies provide an
21752235
* interface to see if a vCPU is currently running or not.

kernel/locking/mutex-debug.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,13 @@ void debug_mutex_add_waiter(struct mutex *lock, struct mutex_waiter *waiter,
5454
lockdep_assert_held(&lock->wait_lock);
5555

5656
/* Current thread can't be already blocked (since it's executing!) */
57-
DEBUG_LOCKS_WARN_ON(task->blocked_on);
57+
DEBUG_LOCKS_WARN_ON(__get_task_blocked_on(task));
5858
}
5959

6060
void debug_mutex_remove_waiter(struct mutex *lock, struct mutex_waiter *waiter,
6161
struct task_struct *task)
6262
{
63-
struct mutex *blocked_on = READ_ONCE(task->blocked_on);
63+
struct mutex *blocked_on = __get_task_blocked_on(task);
6464

6565
DEBUG_LOCKS_WARN_ON(list_empty(&waiter->list));
6666
DEBUG_LOCKS_WARN_ON(waiter->task != task);

kernel/locking/mutex.c

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -644,8 +644,7 @@ __mutex_lock_common(struct mutex *lock, unsigned int state, unsigned int subclas
644644
goto err_early_kill;
645645
}
646646

647-
WARN_ON(current->blocked_on);
648-
current->blocked_on = lock;
647+
__set_task_blocked_on(current, lock);
649648
set_current_state(state);
650649
trace_contention_begin(lock, LCB_F_MUTEX);
651650
for (;;) {
@@ -685,9 +684,9 @@ __mutex_lock_common(struct mutex *lock, unsigned int state, unsigned int subclas
685684
/*
686685
* As we likely have been woken up by task
687686
* that has cleared our blocked_on state, re-set
688-
* it to the lock we are trying to aquire.
687+
* it to the lock we are trying to acquire.
689688
*/
690-
current->blocked_on = lock;
689+
set_task_blocked_on(current, lock);
691690
set_current_state(state);
692691
/*
693692
* Here we order against unlock; we must either see it change
@@ -699,19 +698,23 @@ __mutex_lock_common(struct mutex *lock, unsigned int state, unsigned int subclas
699698

700699
if (first) {
701700
trace_contention_begin(lock, LCB_F_MUTEX | LCB_F_SPIN);
702-
/* clear blocked_on as mutex_optimistic_spin may schedule() */
703-
current->blocked_on = NULL;
701+
/*
702+
* mutex_optimistic_spin() can call schedule(), so
703+
* clear blocked on so we don't become unselectable
704+
* to run.
705+
*/
706+
clear_task_blocked_on(current, lock);
704707
if (mutex_optimistic_spin(lock, ww_ctx, &waiter))
705708
break;
706-
current->blocked_on = lock;
709+
set_task_blocked_on(current, lock);
707710
trace_contention_begin(lock, LCB_F_MUTEX);
708711
}
709712

710713
raw_spin_lock_irqsave(&lock->wait_lock, flags);
711714
}
712715
raw_spin_lock_irqsave(&lock->wait_lock, flags);
713716
acquired:
714-
current->blocked_on = NULL;
717+
__clear_task_blocked_on(current, lock);
715718
__set_current_state(TASK_RUNNING);
716719

717720
if (ww_ctx) {
@@ -741,11 +744,11 @@ __mutex_lock_common(struct mutex *lock, unsigned int state, unsigned int subclas
741744
return 0;
742745

743746
err:
744-
current->blocked_on = NULL;
747+
__clear_task_blocked_on(current, lock);
745748
__set_current_state(TASK_RUNNING);
746749
__mutex_remove_waiter(lock, &waiter);
747750
err_early_kill:
748-
WARN_ON(current->blocked_on);
751+
WARN_ON(__get_task_blocked_on(current));
749752
trace_contention_end(lock, ret);
750753
raw_spin_unlock_irqrestore_wake(&lock->wait_lock, flags, &wake_q);
751754
debug_mutex_free_waiter(&waiter);
@@ -956,14 +959,7 @@ static noinline void __sched __mutex_unlock_slowpath(struct mutex *lock, unsigne
956959
next = waiter->task;
957960

958961
debug_mutex_wake_waiter(lock, waiter);
959-
/*
960-
* Unlock wakeups can be happening in parallel
961-
* (when optimistic spinners steal and release
962-
* the lock), so blocked_on may already be
963-
* cleared here.
964-
*/
965-
WARN_ON(next->blocked_on && next->blocked_on != lock);
966-
next->blocked_on = NULL;
962+
__clear_task_blocked_on(next, lock);
967963
wake_q_add(&wake_q, next);
968964
}
969965

kernel/locking/ww_mutex.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -283,15 +283,13 @@ __ww_mutex_die(struct MUTEX *lock, struct MUTEX_WAITER *waiter,
283283
if (waiter->ww_ctx->acquired > 0 && __ww_ctx_less(waiter->ww_ctx, ww_ctx)) {
284284
#ifndef WW_RT
285285
debug_mutex_wake_waiter(lock, waiter);
286+
#endif
286287
/*
287288
* When waking up the task to die, be sure to clear the
288289
* blocked_on pointer. Otherwise we can see circular
289290
* blocked_on relationships that can't resolve.
290291
*/
291-
WARN_ON(waiter->task->blocked_on &&
292-
waiter->task->blocked_on != lock);
293-
#endif
294-
waiter->task->blocked_on = NULL;
292+
__clear_task_blocked_on(waiter->task, lock);
295293
wake_q_add(wake_q, waiter->task);
296294
}
297295

@@ -345,7 +343,7 @@ static bool __ww_mutex_wound(struct MUTEX *lock,
345343
* blocked_on pointer. Otherwise we can see circular
346344
* blocked_on relationships that can't resolve.
347345
*/
348-
owner->blocked_on = NULL;
346+
__clear_task_blocked_on(owner, lock);
349347
wake_q_add(wake_q, owner);
350348
}
351349
return true;

0 commit comments

Comments
 (0)