Skip to content

Commit 6ce47fd

Browse files
committed
rtmutex: Warn if trylock is called from hard/softirq context
rt_mutex_trylock() must be called from thread context. It can be called from atomic regions (preemption or interrupts disabled), but not from hard/softirq/nmi context. Add a warning to alert abusers. The reasons for this are: 1) There is a potential deadlock in the slowpath 2) Another cpu which blocks on the rtmutex will boost the task which allegedly locked the rtmutex, but that cannot work because the hard/softirq context borrows the task context. Signed-off-by: Thomas Gleixner <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Sebastian Siewior <[email protected]>
1 parent a22e5f5 commit 6ce47fd

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

kernel/locking/rtmutex.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1441,10 +1441,17 @@ EXPORT_SYMBOL_GPL(rt_mutex_timed_lock);
14411441
*
14421442
* @lock: the rt_mutex to be locked
14431443
*
1444+
* This function can only be called in thread context. It's safe to
1445+
* call it from atomic regions, but not from hard interrupt or soft
1446+
* interrupt context.
1447+
*
14441448
* Returns 1 on success and 0 on contention
14451449
*/
14461450
int __sched rt_mutex_trylock(struct rt_mutex *lock)
14471451
{
1452+
if (WARN_ON(in_irq() || in_nmi() || in_serving_softirq()))
1453+
return 0;
1454+
14481455
return rt_mutex_fasttrylock(lock, rt_mutex_slowtrylock);
14491456
}
14501457
EXPORT_SYMBOL_GPL(rt_mutex_trylock);

0 commit comments

Comments
 (0)