Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1491274 > unrolled thread

[PATCH -v2 2/9] sched/rtmutex/deadline: Fix a PI crash for deadline tasks

Started byPeter Zijlstra <peterz@infradead.org>
First post2016-09-26 14:50 +0200
Last post2016-09-29 17:00 +0200
Articles 4 — 3 participants

Back to article view | Back to linux.kernel

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  [PATCH -v2 2/9] sched/rtmutex/deadline: Fix a PI crash for deadline tasks Peter Zijlstra <peterz@infradead.org> - 2016-09-26 14:50 +0200
    Re: [PATCH -v2 2/9] sched/rtmutex/deadline: Fix a PI crash for  deadline tasks Steven Rostedt <rostedt@goodmis.org> - 2016-09-26 17:30 +0200
      Re: [PATCH -v2 2/9] sched/rtmutex/deadline: Fix a PI crash for  deadline tasks Peter Zijlstra <peterz@infradead.org> - 2016-09-26 17:30 +0200
    Re: [PATCH -v2 2/9] sched/rtmutex/deadline: Fix a PI crash for  deadline tasks Thomas Gleixner <tglx@linutronix.de> - 2016-09-29 17:00 +0200

#1491274 — [PATCH -v2 2/9] sched/rtmutex/deadline: Fix a PI crash for deadline tasks

FromPeter Zijlstra <peterz@infradead.org>
Date2016-09-26 14:50 +0200
Subject[PATCH -v2 2/9] sched/rtmutex/deadline: Fix a PI crash for deadline tasks
Message-ID<slDw5-3SS-17@gated-at.bofh.it>
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 [#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>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Juri Lelli <juri.lelli@arm.com>
Acked-by: Steven Rostedt <rostedt@goodmis.org>
Signed-off-by: Xunlei Pang <xlpang@redhat.com>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
---

 include/linux/init_task.h |    1 +
 include/linux/sched.h     |    2 ++
 include/linux/sched/rt.h  |    1 +
 kernel/fork.c             |    1 +
 kernel/locking/rtmutex.c  |   23 +++++++++++++++--------
 kernel/sched/core.c       |    2 ++
 6 files changed, 22 insertions(+), 8 deletions(-)

--- a/include/linux/init_task.h
+++ b/include/linux/init_task.h
@@ -164,6 +164,7 @@ extern struct task_group root_task_group
 #ifdef CONFIG_RT_MUTEXES
 # define INIT_RT_MUTEXES(tsk)						\
 	.pi_waiters = RB_ROOT,						\
+	.pi_top_task = NULL,						\
 	.pi_waiters_leftmost = NULL,
 #else
 # define INIT_RT_MUTEXES(tsk)
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -1716,6 +1716,8 @@ struct task_struct {
 	/* PI waiters blocked on a rt_mutex held by this task */
 	struct rb_root pi_waiters;
 	struct rb_node *pi_waiters_leftmost;
+	/* Updated under owner's pi_lock and rq lock */
+	struct task_struct *pi_top_task;
 	/* Deadlock detection and priority inheritance handling */
 	struct rt_mutex_waiter *pi_blocked_on;
 #endif
--- a/include/linux/sched/rt.h
+++ b/include/linux/sched/rt.h
@@ -19,6 +19,7 @@ static inline int rt_task(struct task_st
 extern int rt_mutex_getprio(struct task_struct *p);
 extern void rt_mutex_setprio(struct task_struct *p, int prio);
 extern int rt_mutex_get_effective_prio(struct task_struct *task, int newprio);
+extern void rt_mutex_update_top_task(struct task_struct *p);
 extern struct task_struct *rt_mutex_get_top_task(struct task_struct *task);
 extern void rt_mutex_adjust_pi(struct task_struct *p);
 static inline bool tsk_is_pi_blocked(struct task_struct *tsk)
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -1397,6 +1397,7 @@ static void rt_mutex_init_task(struct ta
 #ifdef CONFIG_RT_MUTEXES
 	p->pi_waiters = RB_ROOT;
 	p->pi_waiters_leftmost = NULL;
+	p->pi_top_task = NULL;
 	p->pi_blocked_on = NULL;
 #endif
 }
--- a/kernel/locking/rtmutex.c
+++ b/kernel/locking/rtmutex.c
@@ -256,6 +256,16 @@ rt_mutex_dequeue_pi(struct task_struct *
 	RB_CLEAR_NODE(&waiter->pi_tree_entry);
 }
 
+void rt_mutex_update_top_task(struct task_struct *p)
+{
+	if (!task_has_pi_waiters(p)) {
+		p->pi_top_task = NULL;
+		return;
+	}
+
+	p->pi_top_task = task_top_pi_waiter(p)->task;
+}
+
 /*
  * Calculate task priority from the waiter tree priority
  *
@@ -273,10 +283,7 @@ int rt_mutex_getprio(struct task_struct
 
 struct task_struct *rt_mutex_get_top_task(struct task_struct *task)
 {
-	if (likely(!task_has_pi_waiters(task)))
-		return NULL;
-
-	return task_top_pi_waiter(task)->task;
+	return task->pi_top_task;
 }
 
 /*
@@ -285,12 +292,12 @@ struct task_struct *rt_mutex_get_top_tas
  */
 int rt_mutex_get_effective_prio(struct task_struct *task, int newprio)
 {
-	if (!task_has_pi_waiters(task))
+	struct task_struct *top_task = rt_mutex_get_top_task(task);
+
+	if (!top_task)
 		return newprio;
 
-	if (task_top_pi_waiter(task)->task->prio <= newprio)
-		return task_top_pi_waiter(task)->task->prio;
-	return newprio;
+	return min(top_task->prio, newprio);
 }
 
 /*
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -3669,6 +3669,8 @@ void rt_mutex_setprio(struct task_struct
 		goto out_unlock;
 	}
 
+	rt_mutex_update_top_task(p);
+
 	trace_sched_pi_setprio(p, prio);
 	oldprio = p->prio;
 

[toc] | [next] | [standalone]


#1491351 — Re: [PATCH -v2 2/9] sched/rtmutex/deadline: Fix a PI crash for deadline tasks

FromSteven Rostedt <rostedt@goodmis.org>
Date2016-09-26 17:30 +0200
SubjectRe: [PATCH -v2 2/9] sched/rtmutex/deadline: Fix a PI crash for deadline tasks
Message-ID<slG0W-5v6-9@gated-at.bofh.it>
In reply to#1491274
> --- a/kernel/locking/rtmutex.c
> +++ b/kernel/locking/rtmutex.c
> @@ -256,6 +256,16 @@ rt_mutex_dequeue_pi(struct task_struct *
>  	RB_CLEAR_NODE(&waiter->pi_tree_entry);
>  }
>  

Shouldn't we add a comment about what locks are expected to be held
when calling this? Especially if it can be called outside this file.

> +void rt_mutex_update_top_task(struct task_struct *p)
> +{
> +	if (!task_has_pi_waiters(p)) {
> +		p->pi_top_task = NULL;
> +		return;
> +	}
> +
> +	p->pi_top_task = task_top_pi_waiter(p)->task;
> +}
> +
>  /*
>   * Calculate task priority from the waiter tree priority
>   *
> @@ -273,10 +283,7 @@ int rt_mutex_getprio(struct task_struct
>  

Any specific locks that must be held when calling this?

>  struct task_struct *rt_mutex_get_top_task(struct task_struct *task)
>  {
> -	if (likely(!task_has_pi_waiters(task)))
> -		return NULL;
> -
> -	return task_top_pi_waiter(task)->task;
> +	return task->pi_top_task;
>  }

-- Steve

[toc] | [prev] | [next] | [standalone]


#1491359 — Re: [PATCH -v2 2/9] sched/rtmutex/deadline: Fix a PI crash for deadline tasks

FromPeter Zijlstra <peterz@infradead.org>
Date2016-09-26 17:30 +0200
SubjectRe: [PATCH -v2 2/9] sched/rtmutex/deadline: Fix a PI crash for deadline tasks
Message-ID<slG0W-5v6-33@gated-at.bofh.it>
In reply to#1491351
On Mon, Sep 26, 2016 at 11:20:58AM -0400, Steven Rostedt wrote:
> > --- a/kernel/locking/rtmutex.c
> > +++ b/kernel/locking/rtmutex.c
> > @@ -256,6 +256,16 @@ rt_mutex_dequeue_pi(struct task_struct *
> >  	RB_CLEAR_NODE(&waiter->pi_tree_entry);
> >  }
> >  
> 
> Shouldn't we add a comment about what locks are expected to be held
> when calling this? Especially if it can be called outside this file.

Comments are somewhat useless.. I would like to do the below, except I
cannot.

> > +void rt_mutex_update_top_task(struct task_struct *p)
> > +{

	lockdep_assert_held(&p->pi_lock);
	lockdep_assert_held(&task_rq(p)->lock); // except that we cannot access rq :/

> > +	if (!task_has_pi_waiters(p)) {
> > +		p->pi_top_task = NULL;
> > +		return;
> > +	}
> > +
> > +	p->pi_top_task = task_top_pi_waiter(p)->task;
> > +}
> > +
> >  /*
> >   * Calculate task priority from the waiter tree priority
> >   *
> > @@ -273,10 +283,7 @@ int rt_mutex_getprio(struct task_struct
> >  
> 
> Any specific locks that must be held when calling this?

#ifdef CONFIG_LOCKDEP
	WARN_ON_ONCE(debug_locks &&
		!lock_is_held(&p->pi_lock) &&
		!lock_is_held(&task_rq(p)->lock)); // again, cannot do this :/
#endif

> >  struct task_struct *rt_mutex_get_top_task(struct task_struct *task)
> >  {
> > -	if (likely(!task_has_pi_waiters(task)))
> > -		return NULL;
> > -
> > -	return task_top_pi_waiter(task)->task;
> > +	return task->pi_top_task;
> >  }
> 
> -- Steve
> 

[toc] | [prev] | [next] | [standalone]


#1493526 — Re: [PATCH -v2 2/9] sched/rtmutex/deadline: Fix a PI crash for deadline tasks

FromThomas Gleixner <tglx@linutronix.de>
Date2016-09-29 17:00 +0200
SubjectRe: [PATCH -v2 2/9] sched/rtmutex/deadline: Fix a PI crash for deadline tasks
Message-ID<smKYx-5qg-9@gated-at.bofh.it>
In reply to#1491274
On Mon, 26 Sep 2016, Peter Zijlstra wrote:
> 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.

Reviewed-by: Thomas Gleixner <tglx@linutronix.de>

[toc] | [prev] | [standalone]


Back to top | Article view | linux.kernel


csiph-web