Skip to content

Commit e96a770

Browse files
Xunlei PangKAGA-KOKO
authored andcommitted
sched/rtmutex/deadline: Fix a PI crash for deadline tasks
A crash happened while I was playing with deadline PI rtmutex. BUG: unable to handle kernel NULL pointer dereference at 0000000000000018 IP: [<ffffffff810eeb8f>] rt_mutex_get_top_task+0x1f/0x30 PGD 232a75067 PUD 230947067 PMD 0 Oops: 0000 [raspberrypi#1] SMP CPU: 1 PID: 10994 Comm: a.out Not tainted Call Trace: [<ffffffff810b658c>] enqueue_task+0x2c/0x80 [<ffffffff810ba763>] activate_task+0x23/0x30 [<ffffffff810d0ab5>] pull_dl_task+0x1d5/0x260 [<ffffffff810d0be6>] pre_schedule_dl+0x16/0x20 [<ffffffff8164e783>] __schedule+0xd3/0x900 [<ffffffff8164efd9>] schedule+0x29/0x70 [<ffffffff8165035b>] __rt_mutex_slowlock+0x4b/0xc0 [<ffffffff81650501>] rt_mutex_slowlock+0xd1/0x190 [<ffffffff810eeb33>] rt_mutex_timed_lock+0x53/0x60 [<ffffffff810ecbfc>] futex_lock_pi.isra.18+0x28c/0x390 [<ffffffff810ed8b0>] do_futex+0x190/0x5b0 [<ffffffff810edd50>] SyS_futex+0x80/0x180 This is because rt_mutex_enqueue_pi() and rt_mutex_dequeue_pi() are only protected by pi_lock when operating pi waiters, while rt_mutex_get_top_task(), will access them with rq lock held but not holding pi_lock. In order to tackle it, we introduce new "pi_top_task" pointer cached in task_struct, and add new rt_mutex_update_top_task() to update its value, it can be called by rt_mutex_setprio() which held both owner's pi_lock and rq lock. Thus "pi_top_task" can be safely accessed by enqueue_task_dl() under rq lock. Originally-From: Peter Zijlstra <peterz@infradead.org> Signed-off-by: Xunlei Pang <xlpang@redhat.com> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Acked-by: Steven Rostedt <rostedt@goodmis.org> Reviewed-by: Thomas Gleixner <tglx@linutronix.de> Cc: juri.lelli@arm.com Cc: bigeasy@linutronix.de Cc: mathieu.desnoyers@efficios.com Cc: jdesfossez@efficios.com Cc: bristot@redhat.com Link: http://lkml.kernel.org/r/20170323150216.157682758@infradead.org Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
1 parent 2a1c602 commit e96a770

6 files changed

Lines changed: 28 additions & 8 deletions

File tree

‎include/linux/init_task.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ extern struct cred init_cred;
181181
#ifdef CONFIG_RT_MUTEXES
182182
# define INIT_RT_MUTEXES(tsk) \
183183
.pi_waiters = RB_ROOT, \
184+
.pi_top_task = NULL, \
184185
.pi_waiters_leftmost = NULL,
185186
#else
186187
# define INIT_RT_MUTEXES(tsk)

‎include/linux/sched.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -775,6 +775,8 @@ struct task_struct {
775775
/* PI waiters blocked on a rt_mutex held by this task: */
776776
struct rb_root pi_waiters;
777777
struct rb_node *pi_waiters_leftmost;
778+
/* Updated under owner's pi_lock and rq lock */
779+
struct task_struct *pi_top_task;
778780
/* Deadlock detection and priority inheritance handling: */
779781
struct rt_mutex_waiter *pi_blocked_on;
780782
#endif

‎include/linux/sched/rt.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ static inline int rt_task(struct task_struct *p)
2121
extern int rt_mutex_getprio(struct task_struct *p);
2222
extern void rt_mutex_setprio(struct task_struct *p, int prio);
2323
extern int rt_mutex_get_effective_prio(struct task_struct *task, int newprio);
24+
extern void rt_mutex_update_top_task(struct task_struct *p);
2425
extern struct task_struct *rt_mutex_get_top_task(struct task_struct *task);
2526
extern void rt_mutex_adjust_pi(struct task_struct *p);
2627
static inline bool tsk_is_pi_blocked(struct task_struct *tsk)

‎kernel/fork.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1438,6 +1438,7 @@ static void rt_mutex_init_task(struct task_struct *p)
14381438
#ifdef CONFIG_RT_MUTEXES
14391439
p->pi_waiters = RB_ROOT;
14401440
p->pi_waiters_leftmost = NULL;
1441+
p->pi_top_task = NULL;
14411442
p->pi_blocked_on = NULL;
14421443
#endif
14431444
}

‎kernel/locking/rtmutex.c‎

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,19 @@ rt_mutex_dequeue_pi(struct task_struct *task, struct rt_mutex_waiter *waiter)
322322
RB_CLEAR_NODE(&waiter->pi_tree_entry);
323323
}
324324

325+
/*
326+
* Must hold both p->pi_lock and task_rq(p)->lock.
327+
*/
328+
void rt_mutex_update_top_task(struct task_struct *p)
329+
{
330+
if (!task_has_pi_waiters(p)) {
331+
p->pi_top_task = NULL;
332+
return;
333+
}
334+
335+
p->pi_top_task = task_top_pi_waiter(p)->task;
336+
}
337+
325338
/*
326339
* Calculate task priority from the waiter tree priority
327340
*
@@ -337,12 +350,12 @@ int rt_mutex_getprio(struct task_struct *task)
337350
task->normal_prio);
338351
}
339352

353+
/*
354+
* Must hold either p->pi_lock or task_rq(p)->lock.
355+
*/
340356
struct task_struct *rt_mutex_get_top_task(struct task_struct *task)
341357
{
342-
if (likely(!task_has_pi_waiters(task)))
343-
return NULL;
344-
345-
return task_top_pi_waiter(task)->task;
358+
return task->pi_top_task;
346359
}
347360

348361
/*
@@ -351,12 +364,12 @@ struct task_struct *rt_mutex_get_top_task(struct task_struct *task)
351364
*/
352365
int rt_mutex_get_effective_prio(struct task_struct *task, int newprio)
353366
{
354-
if (!task_has_pi_waiters(task))
367+
struct task_struct *top_task = rt_mutex_get_top_task(task);
368+
369+
if (!top_task)
355370
return newprio;
356371

357-
if (task_top_pi_waiter(task)->task->prio <= newprio)
358-
return task_top_pi_waiter(task)->task->prio;
359-
return newprio;
372+
return min(top_task->prio, newprio);
360373
}
361374

362375
/*

‎kernel/sched/core.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3713,6 +3713,8 @@ void rt_mutex_setprio(struct task_struct *p, int prio)
37133713
goto out_unlock;
37143714
}
37153715

3716+
rt_mutex_update_top_task(p);
3717+
37163718
trace_sched_pi_setprio(p, prio);
37173719
oldprio = p->prio;
37183720

0 commit comments

Comments
 (0)