Skip to contentSkip to conversation

sched: Fix broken setscheduler()

PATCH Superseded Steven Rostedt 5 participants 13 messages Mar 6, 2014 – Mar 12, 2014 Scheduler

lore
Share & tools
lore

Steven Rostedt

author reviewer
#0 lore raw

PATCH 1/1 sched: Fix broken setscheduler()

I decided to run my tests on linux-next, and my wakeup_rt tracer was broken. After running a bisect, I found that the problem commit was:

linux-next commit c365c292d059
"sched: Consider pi boosting in setscheduler()"

And the reason the wake_rt tracer test was failing, was because it had no RT task to trace. I first noticed this when running with sched_switch event and saw that my RT task still had normal SCHED_OTHER priority. Looking at the problem commit, I found:

- p->normal_prio = normal_prio(p);
- p->prio = rt_mutex_getprio(p);

With no

+ p->normal_prio = normal_prio(p);
+ p->prio = rt_mutex_getprio(p);

Reading what the commit is suppose to do, I realize that the p->prio can't be set if the task is boosted with a higher prio, but the p->normal_prio still needs to be set regardless, otherwise, when the task is deboosted, it wont get the new priority.

The p->prio has to be set before "check_class_changed()" is called, otherwise the class wont be changed.

---

kernel/sched/core.c +3 -0
index 4600bca..b1cc871 100644
@@ -3198,6 +3198,7 @@ static void __setscheduler_params(struct task_struct *p,
 	 * getparam()/getattr() don't report silly values for !rt tasks.
 	 */
 	p->rt_priority = attr->sched_priority;
+	p->normal_prio = normal_prio(p);
 	set_load_weight(p);
 }
 
@@ -3207,6 +3208,8 @@ static void __setscheduler(struct rq *rq, struct task_struct *p,
 {
 	__setscheduler_params(p, attr);
 
+	p->prio = rt_mutex_getprio(p);
+
 	if (dl_prio(p->prio))
 		p->sched_class = &dl_sched_class;
 	else if (rt_prio(p->prio))
maintainer
#1 lore raw
Quoted 19 lines · Steven Rostedt

I decided to run my tests on linux-next, and my wakeup_rt tracer was broken. After running a bisect, I found that the problem commit was:

linux-next commit c365c292d059
"sched: Consider pi boosting in setscheduler()"

And the reason the wake_rt tracer test was failing, was because it had no RT task to trace. I first noticed this when running with sched_switch event and saw that my RT task still had normal SCHED_OTHER priority. Looking at the problem commit, I found:

- p->normal_prio = normal_prio(p);
- p->prio = rt_mutex_getprio(p);

With no

+ p->normal_prio = normal_prio(p);
+ p->prio = rt_mutex_getprio(p);

Quoted 7 lines

Reading what the commit is suppose to do, I realize that the p->prio can't be set if the task is boosted with a higher prio, but the p->normal_prio still needs to be set regardless, otherwise, when the task is deboosted, it wont get the new priority.

The p->prio has to be set before "check_class_changed()" is called, otherwise the class wont be changed.

So Juri had a different patch for this problem:

http://lkml.kernel.org/r/[email protected]

kernel/sched/core.c static void __setscheduler_params(struct task_struct *p, · line 3203

Quoted 13 lines

---

kernel/sched/core.c +1 -0
index 4600bca..b1cc871 100644
@@ -3198,6 +3198,7 @@ static void __setscheduler_params(struct task_struct *p,
 	 * getparam()/getattr() don't report silly values for !rt tasks.
 	 */
 	p->rt_priority = attr->sched_priority;
+	p->normal_prio = normal_prio(p);
 	set_load_weight(p);
 }

Now; if I'm reading things right, normal_prio is the unboosted priority of a task. And we should indeed keep setting that, otherwise the unboost doesn't know where it should go.

Juri put that in __setscheduler(), but I think that's wrong because the rt_mutex_check_prio() case in __sched_setscheduler() still needs to update this.

kernel/sched/core.c static void __setscheduler(struct rq *rq, struct task_struct *p, · line 3215

Quoted 10 lines
kernel/sched/core.c +2 -0
@@ -3207,6 +3208,8 @@ static void __setscheduler(struct rq *rq, struct task_struct *p,
 {
 	__setscheduler_params(p, attr);
 
+	p->prio = rt_mutex_getprio(p);
+
 	if (dl_prio(p->prio))
 		p->sched_class = &dl_sched_class;
 	else if (rt_prio(p->prio))

And when we call this we're sure to not be boosted; so this is effectively the same as Juri has:

p->prio = p->normal_prio = normal_prio(p)

Seeing how rt_mutex_getprio() and normal_prio() are the same under these conditions.

#2 lore raw
Quoted 50 lines · On Thu, 6 Mar 2014 12:58:25 +0100 Peter Zijlstra <[email protected]> wrote:
Quoted 19 lines · Steven Rostedt

I decided to run my tests on linux-next, and my wakeup_rt tracer was broken. After running a bisect, I found that the problem commit was:

linux-next commit c365c292d059
"sched: Consider pi boosting in setscheduler()"

And the reason the wake_rt tracer test was failing, was because it had no RT task to trace. I first noticed this when running with sched_switch event and saw that my RT task still had normal SCHED_OTHER priority. Looking at the problem commit, I found:

- p->normal_prio = normal_prio(p);
- p->prio = rt_mutex_getprio(p);

With no

+ p->normal_prio = normal_prio(p);
+ p->prio = rt_mutex_getprio(p);

Quoted 7 lines

Reading what the commit is suppose to do, I realize that the p->prio can't be set if the task is boosted with a higher prio, but the p->normal_prio still needs to be set regardless, otherwise, when the task is deboosted, it wont get the new priority.

The p->prio has to be set before "check_class_changed()" is called, otherwise the class wont be changed.

So Juri had a different patch for this problem:

http://lkml.kernel.org/r/[email protected]

kernel/sched/core.c static void __setscheduler_params(struct task_struct *p, · line 3203

Quoted 13 lines

---

kernel/sched/core.c +1 -0
index 4600bca..b1cc871 100644
@@ -3198,6 +3198,7 @@ static void __setscheduler_params(struct task_struct *p,
 	 * getparam()/getattr() don't report silly values for !rt tasks.
 	 */
 	p->rt_priority = attr->sched_priority;
+	p->normal_prio = normal_prio(p);
 	set_load_weight(p);
 }

Now; if I'm reading things right, normal_prio is the unboosted priority of a task. And we should indeed keep setting that, otherwise the unboost doesn't know where it should go.

Quoted 4 lines

Juri put that in __setscheduler(), but I think that's wrong because the rt_mutex_check_prio() case in __sched_setscheduler() still needs to update this.

Oh, right. Missed that.

Quoted 14 lines

kernel/sched/core.c static void __setscheduler(struct rq *rq, struct task_struct *p, · line 3215

Quoted 10 lines
kernel/sched/core.c +2 -0
@@ -3207,6 +3208,8 @@ static void __setscheduler(struct rq *rq, struct task_struct *p,
 {
 	__setscheduler_params(p, attr);
 
+	p->prio = rt_mutex_getprio(p);
+
 	if (dl_prio(p->prio))
 		p->sched_class = &dl_sched_class;
 	else if (rt_prio(p->prio))

And when we call this we're sure to not be boosted; so this is effectively the same as Juri has:

Quoted 6 lines

p->prio = p->normal_prio = normal_prio(p)

Seeing how rt_mutex_getprio() and normal_prio() are the same under these conditions.

Yes. I think you can go with

p->prio = p->normal_prio

and save a few checks in rt_mutex_getprio().

Thanks,

- Juri

author reviewer
#3 lore raw

PATCH v2 1/1 sched: Fix broken setscheduler()

Quoted 6 lines · On Thu, 6 Mar 2014 13:20:40 +0100 Juri Lelli <[email protected]> wrote:

Yes. I think you can go with

p->prio = p->normal_prio

and save a few checks in rt_mutex_getprio().

You're right! Thanks.

-----

I decided to run my tests on linux-next, and my wakeup_rt tracer was broken. After running a bisect, I found that the problem commit was:

linux-next commit c365c292d059
"sched: Consider pi boosting in setscheduler()"

And the reason the wake_rt tracer test was failing, was because it had no RT task to trace. I first noticed this when running with sched_switch event and saw that my RT task still had normal SCHED_OTHER priority. Looking at the problem commit, I found:

- p->normal_prio = normal_prio(p);
- p->prio = rt_mutex_getprio(p);

With no

+ p->normal_prio = normal_prio(p);
+ p->prio = rt_mutex_getprio(p);

Reading what the commit is suppose to do, I realize that the p->prio can't be set if the task is boosted with a higher prio, but the p->normal_prio still needs to be set regardless, otherwise, when the task is deboosted, it wont get the new priority.

The p->prio has to be set before "check_class_changed()" is called, otherwise the class wont be changed.

---
 kernel/sched/core.c | 7 +++++++
 1 file changed, 7 insertions(+)
kernel/sched/core.c +7 -0
index 4600bca..3134756 100644
@@ -3198,6 +3198,7 @@ static void __setscheduler_params(struct task_struct *p,
 	 * getparam()/getattr() don't report silly values for !rt tasks.
 	 */
 	p->rt_priority = attr->sched_priority;
+	p->normal_prio = normal_prio(p);
 	set_load_weight(p);
 }
 
@@ -3207,6 +3208,12 @@ static void __setscheduler(struct rq *rq, struct task_struct *p,
 {
 	__setscheduler_params(p, attr);
 
+	/*
+	 * If we get here, there was no pi waiters boosting the
+	 * task. It is safe to use the normal prio.
+	 */
+	p->prio = normal_prio(p);
+
 	if (dl_prio(p->prio))
 		p->sched_class = &dl_sched_class;
 	else if (rt_prio(p->prio))
Signature · 1 line
1.8.1.4
#4 lore raw
Quoted 30 lines · Steven Rostedt
Quoted 6 lines · On Thu, 6 Mar 2014 13:20:40 +0100 Juri Lelli <[email protected]> wrote:

Yes. I think you can go with

p->prio = p->normal_prio

and save a few checks in rt_mutex_getprio().

You're right! Thanks.

-----

I decided to run my tests on linux-next, and my wakeup_rt tracer was broken. After running a bisect, I found that the problem commit was:

linux-next commit c365c292d059
"sched: Consider pi boosting in setscheduler()"

And the reason the wake_rt tracer test was failing, was because it had no RT task to trace. I first noticed this when running with sched_switch event and saw that my RT task still had normal SCHED_OTHER priority. Looking at the problem commit, I found:

- p->normal_prio = normal_prio(p);
- p->prio = rt_mutex_getprio(p);

With no

Quoted 7 lines

+ p->normal_prio = normal_prio(p);
+ p->prio = rt_mutex_getprio(p);

Reading what the commit is suppose to do, I realize that the p->prio can't be set if the task is boosted with a higher prio, but the p->normal_prio still needs to be set regardless, otherwise, when the task is deboosted, it wont get the new priority.

Indeed.

Quoted 39 lines

The p->prio has to be set before "check_class_changed()" is called, otherwise the class wont be changed.

---
 kernel/sched/core.c | 7 +++++++
 1 file changed, 7 insertions(+)
kernel/sched/core.c +7 -0
index 4600bca..3134756 100644
@@ -3198,6 +3198,7 @@ static void __setscheduler_params(struct task_struct *p,
 	 * getparam()/getattr() don't report silly values for !rt tasks.
 	 */
 	p->rt_priority = attr->sched_priority;
+	p->normal_prio = normal_prio(p);
 	set_load_weight(p);
 }
 
@@ -3207,6 +3208,12 @@ static void __setscheduler(struct rq *rq, struct task_struct *p,
 {
 	__setscheduler_params(p, attr);
 
+	/*
+	 * If we get here, there was no pi waiters boosting the
+	 * task. It is safe to use the normal prio.
+	 */
+	p->prio = normal_prio(p);
+
 	if (dl_prio(p->prio))
 		p->sched_class = &dl_sched_class;
 	else if (rt_prio(p->prio))
Signature · 1 line
1.8.1.4
author reviewer
#5 lore raw
Quoted 6 lines · On Thu, 6 Mar 2014 21:43:50 +0100 (CET) Thomas Gleixner <[email protected]>…
Quoted 4 lines

Reading what the commit is suppose to do, I realize that the p->prio can't be set if the task is boosted with a higher prio, but the p->normal_prio still needs to be set regardless, otherwise, when the task is deboosted, it wont get the new priority.

Indeed.

Is this an Acked-by?

Peter, can you pull this patch please, it is still broken in tip/master.

Thanks,

-- Steve

#6 lore raw
Quoted 8 lines · Steven Rostedt
Quoted 5 lines · On Thu, 6 Mar 2014 21:43:50 +0100 (CET) Thomas Gleixner <[email protected]>…
Quoted 4 lines

Reading what the commit is suppose to do, I realize that the p->prio can't be set if the task is boosted with a higher prio, but the p->normal_prio still needs to be set regardless, otherwise, when the task is deboosted, it wont get the new priority.

Quoted 5 lines
Quoted 1 line

Indeed.

Is this an Acked-by?

Peter, can you pull this patch please, it is still broken in tip/master.

Lemme look at it tomorrow again with an awake brain. This seems to be some forward porting hickup which needs a closer look. Just look at the 3.10-rt version of this:

@@ -3825,20 +3826,25 @@ static struct task_struct *find_process_by_pid(pid_t pid)
return pid ? find_task_by_vpid(pid) : current;
}

-/* Actually do priority change: must hold rq lock. */
-static void
-__setscheduler(struct rq *rq, struct task_struct *p, int policy, int prio) +static void __setscheduler_params(struct task_struct *p, int policy, int prio)
{
p->policy = policy;
p->rt_priority = prio;
p->normal_prio = normal_prio(p);
+ set_load_weight(p);
+}

That code has changed significantly probably due to the EDF merge. We need to figure out whether there is more damage due to that.

Thanks,

tglx

author reviewer
#7 lore raw
Quoted 2 lines · On Mon, 10 Mar 2014 22:18:56 +0100 (CET) Thomas Gleixner <[email protected]>…

Lemme look at it tomorrow again with an awake brain. This seems to be some forward porting hickup which needs a closer look. Just look at

Yep, I talked with Sebastian on IRC and that seems to be the case.

Quoted 17 lines

the 3.10-rt version of this:

(quoted diff) +3 -3
@@ -3825,20 +3826,25 @@ static struct task_struct *find_process_by_pid(pid_t pid)
 	return pid ? find_task_by_vpid(pid) : current;
 }
 
-/* Actually do priority change: must hold rq lock. */
-static void
-__setscheduler(struct rq *rq, struct task_struct *p, int policy, int prio)
+static void __setscheduler_params(struct task_struct *p, int policy, int prio)
 {
 	p->policy = policy;
 	p->rt_priority = prio;
 	p->normal_prio = normal_prio(p);
+	set_load_weight(p);
+}
 
Quoted 2 lines

That code has changed significantly probably due to the EDF merge. We need to figure out whether there is more damage due to that.

Yeah, when I looked at the -rt version, it appeared to have my fix already. But in reality, the forward port broke it. Here's the problem part of the commit:

+ set_load_weight(p);
+}

- p->normal_prio = normal_prio(p);
- p->prio = rt_mutex_getprio(p);

Your patch never deleted the above two. And it kept them in the locations that I placed them in, in my patch.

-- Steve

+/* Actually do priority change: must hold pi & rq lock. */ +static void __setscheduler(struct rq *rq, struct task_struct *p,
+ const struct sched_attr *attr)
+{

#8 lore raw
Quoted 39 lines · On Mon, 10 Mar 2014 17:37:31 -0400 Steven Rostedt <[email protected]> wrote:
Quoted 2 lines · On Mon, 10 Mar 2014 22:18:56 +0100 (CET) Thomas Gleixner <[email protected]>…

Lemme look at it tomorrow again with an awake brain. This seems to be some forward porting hickup which needs a closer look. Just look at

Yep, I talked with Sebastian on IRC and that seems to be the case.

Quoted 17 lines

the 3.10-rt version of this:

(quoted diff) +3 -3
@@ -3825,20 +3826,25 @@ static struct task_struct *find_process_by_pid(pid_t pid)
 	return pid ? find_task_by_vpid(pid) : current;
 }
 
-/* Actually do priority change: must hold rq lock. */
-static void
-__setscheduler(struct rq *rq, struct task_struct *p, int policy, int prio)
+static void __setscheduler_params(struct task_struct *p, int policy, int prio)
 {
 	p->policy = policy;
 	p->rt_priority = prio;
 	p->normal_prio = normal_prio(p);
+	set_load_weight(p);
+}
 
Quoted 2 lines

That code has changed significantly probably due to the EDF merge. We need to figure out whether there is more damage due to that.

Yeah, when I looked at the -rt version, it appeared to have my fix already. But in reality, the forward port broke it. Here's the problem part of the commit:

(quoted diff) +2 -2
+       set_load_weight(p);
+}
 
-       p->normal_prio = normal_prio(p);
-       p->prio = rt_mutex_getprio(p);
Quoted 3 lines

Your patch never deleted the above two. And it kept them in the locations that I placed them in, in my patch.

Oh, and you have to have also something like this

@@ -3271,7 +3271,8 @@ static int __sched_setscheduler(struct task_struct *p,
const struct sched_attr *attr, bool user)
{
- int newprio = MAX_RT_PRIO - 1 - attr->sched_priority;
+ int newprio = dl_policy(attr->sched_policy) ? MAX_DL_PRIO - 1 :
+ MAX_RT_PRIO - 1 - attr->sched_priority;
int retval, oldprio, oldpolicy = -1, on_rq, running;
int policy = attr->sched_policy;
unsigned long flags;

or you can fail to become DL if you are currently boosted by RT, as attr->sched_priority == 0 for DL tasks. Then rt_mutex_check_prio () returns 1 (just update params) if setting DL params for a task already boosted by another DL. But this seems to be ok, as we are already outside enforcement in this situation. (This is going to be trickier with proxy exec, though :/).

Thanks,

- Juri

Quoted 9 lines

-- Steve

(quoted diff) +4 -0
+/* Actually do priority change: must hold pi & rq lock. */
+static void __setscheduler(struct rq *rq, struct task_struct *p,
+                          const struct sched_attr *attr)
+{
#9 lore raw
Quoted 36 lines · Juri Lelli
Quoted 33 lines · On Mon, 10 Mar 2014 17:37:31 -0400 Steven Rostedt <[email protected]> wrote:
Quoted 2 lines · On Mon, 10 Mar 2014 22:18:56 +0100 (CET) Thomas Gleixner <[email protected]>…

Lemme look at it tomorrow again with an awake brain. This seems to be some forward porting hickup which needs a closer look. Just look at

Yep, I talked with Sebastian on IRC and that seems to be the case.

Quoted 17 lines

the 3.10-rt version of this:

(quoted diff) +3 -3
@@ -3825,20 +3826,25 @@ static struct task_struct *find_process_by_pid(pid_t pid)
 	return pid ? find_task_by_vpid(pid) : current;
 }
 
-/* Actually do priority change: must hold rq lock. */
-static void
-__setscheduler(struct rq *rq, struct task_struct *p, int policy, int prio)
+static void __setscheduler_params(struct task_struct *p, int policy, int prio)
 {
 	p->policy = policy;
 	p->rt_priority = prio;
 	p->normal_prio = normal_prio(p);
+	set_load_weight(p);
+}
 
Quoted 2 lines

That code has changed significantly probably due to the EDF merge. We need to figure out whether there is more damage due to that.

Yeah, when I looked at the -rt version, it appeared to have my fix already. But in reality, the forward port broke it. Here's the problem part of the commit:

Quoted 8 lines
Quoted 8 lines
(quoted diff) +2 -2
+       set_load_weight(p);
+}
 
-       p->normal_prio = normal_prio(p);
-       p->prio = rt_mutex_getprio(p);

Your patch never deleted the above two. And it kept them in the locations that I placed them in, in my patch.

Correct.

Review static int __sched_setscheduler(struct task_struct *p, · line 3275

Quoted 10 lines

Oh, and you have to have also something like this

(quoted diff) +2 -1
@@ -3271,7 +3271,8 @@ static int __sched_setscheduler(struct task_struct *p,
 				const struct sched_attr *attr,
 				bool user)
 {
-	int newprio = MAX_RT_PRIO - 1 - attr->sched_priority;
+	int newprio = dl_policy(attr->sched_policy) ? MAX_DL_PRIO - 1 :
+		      MAX_RT_PRIO - 1 - attr->sched_priority;

Right, you beat me.

Quoted 10 lines
(quoted diff) +0 -0
 	int retval, oldprio, oldpolicy = -1, on_rq, running;
 	int policy = attr->sched_policy;
 	unsigned long flags;

or you can fail to become DL if you are currently boosted by RT, as attr->sched_priority == 0 for DL tasks. Then rt_mutex_check_prio () returns 1 (just update params) if setting DL params for a task already boosted by another DL. But this seems to be ok, as we are already outside enforcement in this situation. (This is going to be trickier with proxy exec, though :/).

Steve, can you please send an updated one?

Thanks,

tglx

author reviewer
#10 lore raw

PATCH v3 1/1 sched: Fix broken setscheduler()

I decided to run my tests on linux-next, and my wakeup_rt tracer was broken. After running a bisect, I found that the problem commit was:

linux-next commit c365c292d059
"sched: Consider pi boosting in setscheduler()"

And the reason the wake_rt tracer test was failing, was because it had no RT task to trace. I first noticed this when running with sched_switch event and saw that my RT task still had normal SCHED_OTHER priority. Looking at the problem commit, I found:

- p->normal_prio = normal_prio(p);
- p->prio = rt_mutex_getprio(p);

With no

+ p->normal_prio = normal_prio(p);
+ p->prio = rt_mutex_getprio(p);

Reading what the commit is suppose to do, I realize that the p->prio can't be set if the task is boosted with a higher prio, but the p->normal_prio still needs to be set regardless, otherwise, when the task is deboosted, it wont get the new priority.

The p->prio has to be set before "check_class_changed()" is called, otherwise the class wont be changed.

Also added fix to newprio to include a check for deadline policy that was missing. This change was suggested by Juri Lelli.

---
 kernel/sched/core.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
kernel/sched/core.c +9 -1
index ee8004c..f00a032 100644
@@ -3197,6 +3197,7 @@ static void __setscheduler_params(struct task_struct *p,
 	 * getparam()/getattr() don't report silly values for !rt tasks.
 	 */
 	p->rt_priority = attr->sched_priority;
+	p->normal_prio = normal_prio(p);
 	set_load_weight(p);
 }
 
@@ -3206,6 +3207,12 @@ static void __setscheduler(struct rq *rq, struct task_struct *p,
 {
 	__setscheduler_params(p, attr);
 
+	/*
+	 * If we get here, there was no pi waiters boosting the
+	 * task. It is safe to use the normal prio.
+	 */
+	p->prio = normal_prio(p);
+
 	if (dl_prio(p->prio))
 		p->sched_class = &dl_sched_class;
 	else if (rt_prio(p->prio))
@@ -3264,7 +3271,8 @@ static int __sched_setscheduler(struct task_struct *p,
 				const struct sched_attr *attr,
 				bool user)
 {
-	int newprio = MAX_RT_PRIO - 1 - attr->sched_priority;
+	int newprio = dl_policy(attr->sched_policy) ? MAX_DL_PRIO - 1 :
+		      MAX_RT_PRIO - 1 - attr->sched_priority;
 	int retval, oldprio, oldpolicy = -1, on_rq, running;
 	int policy = attr->sched_policy;
 	unsigned long flags;
Signature · 1 line
1.8.1.4
Reviewed-by on v3 1/1
#11 lore raw
Quoted 25 lines · Steven Rostedt

I decided to run my tests on linux-next, and my wakeup_rt tracer was broken. After running a bisect, I found that the problem commit was:

linux-next commit c365c292d059
"sched: Consider pi boosting in setscheduler()"

And the reason the wake_rt tracer test was failing, was because it had no RT task to trace. I first noticed this when running with sched_switch event and saw that my RT task still had normal SCHED_OTHER priority. Looking at the problem commit, I found:

- p->normal_prio = normal_prio(p);
- p->prio = rt_mutex_getprio(p);

With no

+ p->normal_prio = normal_prio(p);
+ p->prio = rt_mutex_getprio(p);

Reading what the commit is suppose to do, I realize that the p->prio can't be set if the task is boosted with a higher prio, but the p->normal_prio still needs to be set regardless, otherwise, when the task is deboosted, it wont get the new priority.

Quoted 8 lines

The p->prio has to be set before "check_class_changed()" is called, otherwise the class wont be changed.

Also added fix to newprio to include a check for deadline policy that was missing. This change was suggested by Juri Lelli.

#12 lore raw
In reply to Steven Rostedt
sched: Fix broken setscheduler()
Commit-ID:  383afd0971538b3d77532a56404b24cfe967b5dd
Gitweb:     http://git.kernel.org/tip/383afd0971538b3d77532a56404b24cfe967b5dd
Author:     Steven Rostedt <[email protected]>
AuthorDate: Tue, 11 Mar 2014 19:24:20 -0400
Committer:  Ingo Molnar <[email protected]>
CommitDate: Wed, 12 Mar 2014 10:48:59 +0100

sched: Fix broken setscheduler()

I decided to run my tests on linux-next, and my wakeup_rt tracer was broken. After running a bisect, I found that the problem commit was:

linux-next commit c365c292d059
"sched: Consider pi boosting in setscheduler()"

And the reason the wake_rt tracer test was failing, was because it had no RT task to trace. I first noticed this when running with sched_switch event and saw that my RT task still had normal SCHED_OTHER priority. Looking at the problem commit, I found:

- p->normal_prio = normal_prio(p);
- p->prio = rt_mutex_getprio(p);

With no

+ p->normal_prio = normal_prio(p);
+ p->prio = rt_mutex_getprio(p);

Reading what the commit is suppose to do, I realize that the p->prio can't be set if the task is boosted with a higher prio, but the p->normal_prio still needs to be set regardless, otherwise, when the task is deboosted, it wont get the new priority.

The p->prio has to be set before "check_class_changed()" is called, otherwise the class wont be changed.

Also added fix to newprio to include a check for deadline policy that was missing. This change was suggested by Juri Lelli.

---
 kernel/sched/core.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
kernel/sched/core.c +9 -1
index 9e126a2..ae365aa 100644
@@ -3195,6 +3195,7 @@ static void __setscheduler_params(struct task_struct *p,
 	 * getparam()/getattr() don't report silly values for !rt tasks.
 	 */
 	p->rt_priority = attr->sched_priority;
+	p->normal_prio = normal_prio(p);
 	set_load_weight(p);
 }
 
@@ -3204,6 +3205,12 @@ static void __setscheduler(struct rq *rq, struct task_struct *p,
 {
 	__setscheduler_params(p, attr);
 
+	/*
+	 * If we get here, there was no pi waiters boosting the
+	 * task. It is safe to use the normal prio.
+	 */
+	p->prio = normal_prio(p);
+
 	if (dl_prio(p->prio))
 		p->sched_class = &dl_sched_class;
 	else if (rt_prio(p->prio))
@@ -3262,7 +3269,8 @@ static int __sched_setscheduler(struct task_struct *p,
 				const struct sched_attr *attr,
 				bool user)
 {
-	int newprio = MAX_RT_PRIO - 1 - attr->sched_priority;
+	int newprio = dl_policy(attr->sched_policy) ? MAX_DL_PRIO - 1 :
+		      MAX_RT_PRIO - 1 - attr->sched_priority;
 	int retval, oldprio, oldpolicy = -1, on_rq, running;
 	int policy = attr->sched_policy;
 	unsigned long flags;