[v4,14/19] sched, lockdep: Annotate ->pi_lock recursion
diff mbox series

Message ID 20201023102347.406912197@infradead.org
State New, archived
Headers show
Series
  • sched: Migrate disable support
Related show

Commit Message

Peter Zijlstra Oct. 23, 2020, 10:12 a.m. UTC
There's a valid ->pi_lock recursion issue where the actual PI code
tries to wake up the stop task. Make lockdep aware so it doesn't
complain about this.

Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
---
 kernel/sched/core.c |   15 +++++++++++++++
 1 file changed, 15 insertions(+)

Comments

Valentin Schneider Oct. 29, 2020, 4:27 p.m. UTC | #1
On 23/10/20 11:12, Peter Zijlstra wrote:
> @@ -2617,6 +2618,20 @@ void sched_set_stop_task(int cpu, struct
>               sched_setscheduler_nocheck(stop, SCHED_FIFO, &param);
>
>               stop->sched_class = &stop_sched_class;
> +
> +		/*
> +		 * The PI code calls rt_mutex_setprio() with ->pi_lock held to
> +		 * adjust the effective priority of a task. As a result,
> +		 * rt_mutex_setprio() can trigger (RT) balancing operations,
> +		 * which can then trigger wakeups of the stop thread to push
> +		 * around the current task.
> +		 *
> +		 * The stop task itself will never be part of the PI-chain, it
> +		 * never blocks, therefore that ->pi_lock recursion is safe.

Isn't it that the stopper task can only run when preemption is re-enabled,
and the ->pi_lock is dropped before then?

If we were to have an SCA-like function that would kick the stopper but
"forget" to release the pi_lock, then we would very much like lockdep to
complain, right? Or is that something else entirely?

> +		 * Tell lockdep about this by placing the stop->pi_lock in its
> +		 * own class.
> +		 */
> +		lockdep_set_class(&stop->pi_lock, &stop_pi_lock);
>       }
>
>       cpu_rq(cpu)->stop = stop;
Peter Zijlstra Oct. 29, 2020, 5:38 p.m. UTC | #2
On Thu, Oct 29, 2020 at 04:27:16PM +0000, Valentin Schneider wrote:
> 
> On 23/10/20 11:12, Peter Zijlstra wrote:
> > @@ -2617,6 +2618,20 @@ void sched_set_stop_task(int cpu, struct
> >               sched_setscheduler_nocheck(stop, SCHED_FIFO, &param);
> >
> >               stop->sched_class = &stop_sched_class;
> > +
> > +		/*
> > +		 * The PI code calls rt_mutex_setprio() with ->pi_lock held to
> > +		 * adjust the effective priority of a task. As a result,
> > +		 * rt_mutex_setprio() can trigger (RT) balancing operations,
> > +		 * which can then trigger wakeups of the stop thread to push
> > +		 * around the current task.
> > +		 *
> > +		 * The stop task itself will never be part of the PI-chain, it
> > +		 * never blocks, therefore that ->pi_lock recursion is safe.
> 
> Isn't it that the stopper task can only run when preemption is re-enabled,
> and the ->pi_lock is dropped before then?
> 
> If we were to have an SCA-like function that would kick the stopper but
> "forget" to release the pi_lock, then we would very much like lockdep to
> complain, right? Or is that something else entirely?

You've forgotten the other, and original, purpose of ->pi_lock, guarding
the actual PI chain. Please have a look at rt_mutex_adjust_prio_chain()
and its comment.

But no, this isn't about running, this is about doing an actual wakeup
(of the stopper task) while holding an ->pi_lock instance (guaranteed
not the stopper task's). And since wakeup will take ->pi_lock, lockdep
will get all whiny about ->pi_lock self recursion.

> > +		 * Tell lockdep about this by placing the stop->pi_lock in its
> > +		 * own class.
> > +		 */
> > +		lockdep_set_class(&stop->pi_lock, &stop_pi_lock);
> >       }
> >
> >       cpu_rq(cpu)->stop = stop;
Valentin Schneider Oct. 29, 2020, 6:09 p.m. UTC | #3
On 29/10/20 17:38, Peter Zijlstra wrote:
> On Thu, Oct 29, 2020 at 04:27:16PM +0000, Valentin Schneider wrote:
>> 
>> On 23/10/20 11:12, Peter Zijlstra wrote:
>> > @@ -2617,6 +2618,20 @@ void sched_set_stop_task(int cpu, struct
>> >               sched_setscheduler_nocheck(stop, SCHED_FIFO, &param);
>> >
>> >               stop->sched_class = &stop_sched_class;
>> > +
>> > +		/*
>> > +		 * The PI code calls rt_mutex_setprio() with ->pi_lock held to
>> > +		 * adjust the effective priority of a task. As a result,
>> > +		 * rt_mutex_setprio() can trigger (RT) balancing operations,
>> > +		 * which can then trigger wakeups of the stop thread to push
>> > +		 * around the current task.
>> > +		 *
>> > +		 * The stop task itself will never be part of the PI-chain, it
>> > +		 * never blocks, therefore that ->pi_lock recursion is safe.
>> 
>> Isn't it that the stopper task can only run when preemption is re-enabled,
>> and the ->pi_lock is dropped before then?
>> 
>> If we were to have an SCA-like function that would kick the stopper but
>> "forget" to release the pi_lock, then we would very much like lockdep to
>> complain, right? Or is that something else entirely?
>
> You've forgotten the other, and original, purpose of ->pi_lock, guarding
> the actual PI chain. Please have a look at rt_mutex_adjust_prio_chain()
> and its comment.
>
> But no, this isn't about running, this is about doing an actual wakeup
> (of the stopper task) while holding an ->pi_lock instance (guaranteed
> not the stopper task's). And since wakeup will take ->pi_lock, lockdep
> will get all whiny about ->pi_lock self recursion.
>

Gotcha. Thanks, and apologies for the noise.

>> > +		 * Tell lockdep about this by placing the stop->pi_lock in its
>> > +		 * own class.
>> > +		 */
>> > +		lockdep_set_class(&stop->pi_lock, &stop_pi_lock);
>> >       }
>> >
>> >       cpu_rq(cpu)->stop = stop;

Patch
diff mbox series

--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -2602,6 +2602,7 @@  int select_task_rq(struct task_struct *p
 
 void sched_set_stop_task(int cpu, struct task_struct *stop)
 {
+	static struct lock_class_key stop_pi_lock;
 	struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 };
 	struct task_struct *old_stop = cpu_rq(cpu)->stop;
 
@@ -2617,6 +2618,20 @@  void sched_set_stop_task(int cpu, struct
 		sched_setscheduler_nocheck(stop, SCHED_FIFO, &param);
 
 		stop->sched_class = &stop_sched_class;
+
+		/*
+		 * The PI code calls rt_mutex_setprio() with ->pi_lock held to
+		 * adjust the effective priority of a task. As a result,
+		 * rt_mutex_setprio() can trigger (RT) balancing operations,
+		 * which can then trigger wakeups of the stop thread to push
+		 * around the current task.
+		 *
+		 * The stop task itself will never be part of the PI-chain, it
+		 * never blocks, therefore that ->pi_lock recursion is safe.
+		 * Tell lockdep about this by placing the stop->pi_lock in its
+		 * own class.
+		 */
+		lockdep_set_class(&stop->pi_lock, &stop_pi_lock);
 	}
 
 	cpu_rq(cpu)->stop = stop;