Skip to content

Commit e0aad5b

Browse files
Peter ZijlstraKAGA-KOKO
authored andcommitted
rtmutex: Fix PI chain order integrity
rt_mutex_waiter::prio is a copy of task_struct::prio which is updated during the PI chain walk, such that the PI chain order isn't messed up by (asynchronous) task state updates. Currently rt_mutex_waiter_less() uses task state for deadline tasks; this is broken, since the task state can, as said above, change asynchronously, causing the RB tree order to change without actual tree update -> FAIL. Fix this by also copying the deadline into the rt_mutex_waiter state and updating it along with its prio field. Ideally we would also force PI chain updates whenever DL tasks update their deadline parameter, but for first approximation this is less broken than it was. Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Cc: juri.lelli@arm.com Cc: bigeasy@linutronix.de Cc: xlpang@redhat.com Cc: rostedt@goodmis.org Cc: mathieu.desnoyers@efficios.com Cc: jdesfossez@efficios.com Cc: bristot@redhat.com Link: http://lkml.kernel.org/r/20170323150216.403992539@infradead.org Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
1 parent b91473f commit e0aad5b

2 files changed

Lines changed: 28 additions & 2 deletions

File tree

‎kernel/locking/rtmutex.c‎

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,7 @@ rt_mutex_waiter_less(struct rt_mutex_waiter *left,
238238
* then right waiter has a dl_prio() too.
239239
*/
240240
if (dl_prio(left->prio))
241-
return dl_time_before(left->task->dl.deadline,
242-
right->task->dl.deadline);
241+
return dl_time_before(left->deadline, right->deadline);
243242

244243
return 0;
245244
}
@@ -650,7 +649,26 @@ static int rt_mutex_adjust_prio_chain(struct task_struct *task,
650649

651650
/* [7] Requeue the waiter in the lock waiter tree. */
652651
rt_mutex_dequeue(lock, waiter);
652+
653+
/*
654+
* Update the waiter prio fields now that we're dequeued.
655+
*
656+
* These values can have changed through either:
657+
*
658+
* sys_sched_set_scheduler() / sys_sched_setattr()
659+
*
660+
* or
661+
*
662+
* DL CBS enforcement advancing the effective deadline.
663+
*
664+
* Even though pi_waiters also uses these fields, and that tree is only
665+
* updated in [11], we can do this here, since we hold [L], which
666+
* serializes all pi_waiters access and rb_erase() does not care about
667+
* the values of the node being removed.
668+
*/
653669
waiter->prio = task->prio;
670+
waiter->deadline = task->dl.deadline;
671+
654672
rt_mutex_enqueue(lock, waiter);
655673

656674
/* [8] Release the task */
@@ -777,6 +795,8 @@ static int rt_mutex_adjust_prio_chain(struct task_struct *task,
777795
static int try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task,
778796
struct rt_mutex_waiter *waiter)
779797
{
798+
lockdep_assert_held(&lock->wait_lock);
799+
780800
/*
781801
* Before testing whether we can acquire @lock, we set the
782802
* RT_MUTEX_HAS_WAITERS bit in @lock->owner. This forces all
@@ -902,6 +922,8 @@ static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
902922
struct rt_mutex *next_lock;
903923
int chain_walk = 0, res;
904924

925+
lockdep_assert_held(&lock->wait_lock);
926+
905927
/*
906928
* Early deadlock detection. We really don't want the task to
907929
* enqueue on itself just to untangle the mess later. It's not
@@ -919,6 +941,7 @@ static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
919941
waiter->task = task;
920942
waiter->lock = lock;
921943
waiter->prio = task->prio;
944+
waiter->deadline = task->dl.deadline;
922945

923946
/* Get the top priority waiter on the lock */
924947
if (rt_mutex_has_waiters(lock))
@@ -1036,6 +1059,8 @@ static void remove_waiter(struct rt_mutex *lock,
10361059
struct task_struct *owner = rt_mutex_owner(lock);
10371060
struct rt_mutex *next_lock;
10381061

1062+
lockdep_assert_held(&lock->wait_lock);
1063+
10391064
raw_spin_lock(&current->pi_lock);
10401065
rt_mutex_dequeue(lock, waiter);
10411066
current->pi_blocked_on = NULL;

‎kernel/locking/rtmutex_common.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ struct rt_mutex_waiter {
3434
struct rt_mutex *deadlock_lock;
3535
#endif
3636
int prio;
37+
u64 deadline;
3738
};
3839

3940
/*

0 commit comments

Comments
 (0)