2007-09-19 21:15:16

by Hiroshi Shimamoto

[permalink] [raw]
Subject: [PATCH] sched: fix to use invalid sched_class

Hi Ingo,

I found an issue about the scheduler.
If you need a test case, please let me know.
Here is a patch.

When using rt_mutex, a NULL pointer dereference is occurred at
enqueue_task_rt. Here is a scenario;
1) there are two threads, the thread A is fair_sched_class and
thread B is rt_sched_class.
2) Thread A is boosted up to rt_sched_class, because the thread A
has a rt_mutex lock and the thread B is waiting the lock.
3) At this time, when thread A create a new thread C, the thread
C has a rt_sched_class.
4) When doing wake_up_new_task() for the thread C, the priority
of the thread C is out of the RT priority range, because the
normal priority of thread A is not the RT priority. It makes
data corruption by overflowing the rt_prio_array.
The new thread C should be fair_sched_class.

The new thread should be valid scheduler class before queuing.
This patch fixes to set the suitable scheduler class.

Signed-off-by: Hiroshi Shimamoto <[email protected]>
---

diff --git a/kernel/sched.c b/kernel/sched.c
index deeb1f8..b0c582b 100644
--- a/kernel/sched.c
+++ b/kernel/sched.c
@@ -1682,6 +1682,11 @@ void fastcall wake_up_new_task(struct task_struct *p, unsigned long clone_flags)

p->prio = effective_prio(p);

+ if (rt_prio(p->prio))
+ p->sched_class = &rt_sched_class;
+ else
+ p->sched_class = &fair_sched_class;
+
if (!p->sched_class->task_new || !sysctl_sched_child_runs_first ||
(clone_flags & CLONE_VM) || task_cpu(p) != this_cpu ||
!current->se.on_rq) {


2007-09-19 21:38:18

by Ingo Molnar

[permalink] [raw]
Subject: Re: [PATCH] sched: fix to use invalid sched_class


* Hiroshi Shimamoto <[email protected]> wrote:

> Hi Ingo,
>
> I found an issue about the scheduler.
> If you need a test case, please let me know.
> Here is a patch.
>
> When using rt_mutex, a NULL pointer dereference is occurred at
> enqueue_task_rt. Here is a scenario;
> 1) there are two threads, the thread A is fair_sched_class and
> thread B is rt_sched_class.
> 2) Thread A is boosted up to rt_sched_class, because the thread A
> has a rt_mutex lock and the thread B is waiting the lock.
> 3) At this time, when thread A create a new thread C, the thread
> C has a rt_sched_class.
> 4) When doing wake_up_new_task() for the thread C, the priority
> of the thread C is out of the RT priority range, because the
> normal priority of thread A is not the RT priority. It makes
> data corruption by overflowing the rt_prio_array.
> The new thread C should be fair_sched_class.
>
> The new thread should be valid scheduler class before queuing.
> This patch fixes to set the suitable scheduler class.

Nice fix! It's a 2.6.23 must-have fix - i'll push it out into the
scheduler tree. Thanks!

Ingo

2007-09-19 22:05:55

by Dmitry Adamushko

[permalink] [raw]
Subject: Re: [PATCH] sched: fix to use invalid sched_class

> > Hi Ingo,
> >
> > I found an issue about the scheduler.
> > If you need a test case, please let me know.
> > Here is a patch.
> > [ ... ]
> > The new thread should be valid scheduler class before queuing.
> > This patch fixes to set the suitable scheduler class.
>
> Nice fix! It's a 2.6.23 must-have fix - i'll push it out into the
> scheduler tree. Thanks!

I think, the following approach would be a bit more appropriate
(the missing addition to the already existing "don't leak PI boosting prio"):

(not tested)

---
diff --git a/kernel/sched.c b/kernel/sched.c
index bc1a625..da2139e 100644
--- a/kernel/sched.c
+++ b/kernel/sched.c
@@ -1646,6 +1646,8 @@ void sched_fork(struct task_struct *p, int clone_flags)
* Make sure we do not leak PI boosting priority to the child:
*/
p->prio = current->normal_prio;
+ if (!rt_prio(p->prio))
+ p->sched_class = &fair_sched_class;

#if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT)
if (likely(sched_info_on()))

---



2007-09-20 00:02:01

by Hiroshi Shimamoto

[permalink] [raw]
Subject: Re: [PATCH] sched: fix to use invalid sched_class

Dmitry Adamushko wrote:
>>> Hi Ingo,
>>>
>>> I found an issue about the scheduler.
>>> If you need a test case, please let me know.
>>> Here is a patch.
>>> [ ... ]
>>> The new thread should be valid scheduler class before queuing.
>>> This patch fixes to set the suitable scheduler class.
>> Nice fix! It's a 2.6.23 must-have fix - i'll push it out into the
>> scheduler tree. Thanks!
>
> I think, the following approach would be a bit more appropriate
> (the missing addition to the already existing "don't leak PI boosting prio"):
>
> (not tested)
>
Thank you for reviewing my patch.
Yes, your approach looks better than mine.
I've tested it and it looks working fine in my test case.
I attached the test case.

Will you write a patch to replace mine?

Thanks,
Hiroshi Shimamoto


Attachments:
mutexprio.c (882.00 B)

2007-09-20 07:18:23

by Ingo Molnar

[permalink] [raw]
Subject: Re: [PATCH] sched: fix to use invalid sched_class


* Hiroshi Shimamoto <[email protected]> wrote:

> Dmitry Adamushko wrote:
> >>> Hi Ingo,
> >>>
> >>> I found an issue about the scheduler.
> >>> If you need a test case, please let me know.
> >>> Here is a patch.
> >>> [ ... ]
> >>> The new thread should be valid scheduler class before queuing.
> >>> This patch fixes to set the suitable scheduler class.
> >> Nice fix! It's a 2.6.23 must-have fix - i'll push it out into the
> >> scheduler tree. Thanks!
> >
> > I think, the following approach would be a bit more appropriate
> > (the missing addition to the already existing "don't leak PI boosting prio"):
> >
> > (not tested)
> >
> Thank you for reviewing my patch.
> Yes, your approach looks better than mine.
> I've tested it and it looks working fine in my test case.
> I attached the test case.
>
> Will you write a patch to replace mine?

thanks for testing it - we can queue Dmitry's clean-up for v2.6.24
(please send me a patch for v2.6.23-rc7). The most important step was to
get your fix into v2.6.23.

Ingo