sched: Protect sched_rr_get_param access to task->sched_class
PATCH 2/2 sched: Protect sched_rr_get_param access to task->sched_class
sched_rr_get_param calls task->sched_class->get_rr_interval(task) without protection against a concurrent sched_setscheduler() call which modifies task->sched_class.
Serialize the access with task_rq_lock(task);
--- kernel/sched.c | 4 ++++ 1 file changed, 4 insertions(+)
kernel/sched.c +4 -0
@@ -6887,6 +6887,8 @@ SYSCALL_DEFINE2(sched_rr_get_interval, p {struct task_struct *p;unsigned int time_slice; +unsigned long flags; +struct rq *rq;int retval;struct timespec t;
@@ -6903,7 +6905,9 @@ SYSCALL_DEFINE2(sched_rr_get_interval, pif (retval) goto out_unlock; + rq =task_rq_lock (p, &flags); time_slice = p->sched_class->get_rr_interval (p); +task_rq_unlock (rq, &flags);read_unlock (&tasklist_lock);jiffies_to_timespec (time_slice, &t);
Quoted 28 lines · Thomas Gleixner
plain text document attachment (sched-fix-sched-get-rr-param-race.patch)
sched_rr_get_paramcallstask->sched_class->get_rr_interval(task)without protection against a concurrentsched_setscheduler()call which modifiestask->sched_class.Serialize the access with
task_rq_lock(task);Signed-off-by Thomas Gleixner <[email protected]>--- kernel/sched.c | 4 ++++ 1 file changed, 4 insertions(+)Index:
linux-2.6-tip/kernel/sched.c
===================================================================kernel/sched.c +2
-0@@ -6887,6 +6887,8 @@ SYSCALL_DEFINE2(sched_rr_get_interval, p {struct task_struct *p;unsigned int time_slice; +unsigned long flags; +struct rq *rq;int retval;struct timespec t;
kernel/sched.c SYSCALL_DEFINE2(sched_rr_get_interval, p · line 6913
Quoted 9 lines
kernel/sched.c +2
-0@@ -6903,7 +6905,9 @@ SYSCALL_DEFINE2(sched_rr_get_interval, pif (retval) goto out_unlock; + rq =task_rq_lock (p, &flags); time_slice = p->sched_class->get_rr_interval (p); +task_rq_unlock (rq, &flags);read_unlock (&tasklist_lock);jiffies_to_timespec (time_slice, &t);
Peter Zijlstra
maintainerQuoted 35 lines · Peter Zijlstra
Quoted 34 lines · Thomas Gleixner
plain text document attachment (sched-fix-sched-get-rr-param-race.patch)
sched_rr_get_paramcallstask->sched_class->get_rr_interval(task)without protection against a concurrentsched_setscheduler()call which modifiestask->sched_class.Serialize the access with
task_rq_lock(task);Signed-off-by Thomas Gleixner <[email protected]>--- kernel/sched.c | 4 ++++ 1 file changed, 4 insertions(+)Index:
linux-2.6-tip/kernel/sched.c
===================================================================kernel/sched.c +4
-0@@ -6887,6 +6887,8 @@ SYSCALL_DEFINE2(sched_rr_get_interval, p {struct task_struct *p;unsigned int time_slice; +unsigned long flags; +struct rq *rq;int retval;struct timespec t;@@ -6903,7 +6905,9 @@ SYSCALL_DEFINE2(sched_rr_get_interval, pif (retval) goto out_unlock; + rq =task_rq_lock (p, &flags); time_slice = p->sched_class->get_rr_interval (p); +task_rq_unlock (rq, &flags);
Quoted 5 lines
kernel/sched.c SYSCALL_DEFINE2(sched_rr_get_interval, p · line 6913
Quoted 3 lines
kernel/sched.c +0
-0read_unlock (&tasklist_lock);jiffies_to_timespec (time_slice, &t);Acked-by Peter Zijlstra <[email protected]>
Oh, that was too quick, get_rr_interval_fair() actually takes the rq->lock, so lets add this:
unsigned int get_rr_interval_fair(struct task_struct *task)
{
struct sched_entity *se = &task->se;
- unsigned long flags;
- struct rq *rq;
unsigned int rr_interval = 0;
/*
* Time slice is 0 for SCHED_OTHER tasks that are on an otherwise
* idle runqueue:
*/
- rq = task_rq_lock(task, &flags);
if (rq->cfs.load.weight)
rr_interval = NS_TO_JIFFIES(sched_slice(&rq->cfs, se));
- task_rq_unlock(rq, &flags);
-
return rr_interval;}
Quoted 53 lines · Peter Zijlstra
Quoted 35 lines · Peter Zijlstra
Quoted 34 lines · Thomas Gleixner
plain text document attachment (sched-fix-sched-get-rr-param-race.patch)
sched_rr_get_paramcallstask->sched_class->get_rr_interval(task)without protection against a concurrentsched_setscheduler()call which modifiestask->sched_class.Serialize the access with
task_rq_lock(task);Signed-off-by Thomas Gleixner <[email protected]>--- kernel/sched.c | 4 ++++ 1 file changed, 4 insertions(+)Index:
linux-2.6-tip/kernel/sched.c
===================================================================kernel/sched.c +4
-0@@ -6887,6 +6887,8 @@ SYSCALL_DEFINE2(sched_rr_get_interval, p {struct task_struct *p;unsigned int time_slice; +unsigned long flags; +struct rq *rq;int retval;struct timespec t;@@ -6903,7 +6905,9 @@ SYSCALL_DEFINE2(sched_rr_get_interval, pif (retval) goto out_unlock; + rq =task_rq_lock (p, &flags); time_slice = p->sched_class->get_rr_interval (p); +task_rq_unlock (rq, &flags);Quoted 5 lines
kernel/sched.c SYSCALL_DEFINE2(sched_rr_get_interval, p · line 6913
Quoted 3 lines
kernel/sched.c +0
-0read_unlock (&tasklist_lock);jiffies_to_timespec (time_slice, &t);Acked-by Peter Zijlstra <[email protected]>Oh, that was too quick,
get_rr_interval_fair()actually takes therq->lock, so lets add this:unsigned int
get_rr_interval_fair(struct task_struct *task){ struct sched_entity *se = &task->se;(quoted diff) +0
-2- unsigned long flags; - struct rq *rq; unsigned int rr_interval = 0; /*
Review
Quoted 10 lines
(quoted diff) +0
-3* Time slice is 0 for SCHED_OTHER tasks that are on an otherwise * idle runqueue: */ - rq = task_rq_lock(task, &flags); if (rq->cfs.load.weight) rr_interval = NS_TO_JIFFIES(sched_slice(&rq->cfs, se)); - task_rq_unlock(rq, &flags); - return rr_interval; }
Duh, yes. I looked at that actually and made a mental note to remove it. :(
Thanks for catching that,
tglx
[patch V2 sched: Protect sched_rr_get_param access to task->sched_class
sched_rr_get_param calls task->sched_class->get_rr_interval(task) without protection against a concurrent sched_setscheduler() call which modifies task->sched_class.
Serialize the access with task_rq_lock(task) and hand the rq pointer into get_rr_interval() as it's needed at least in the sched_fair implementation.
--- include/linux/sched.h | 3 ++- kernel/sched.c | 6 +++++- kernel/sched_fair.c | 6 +----- kernel/sched_idletask.c | 2 +- kernel/sched_rt.c | 2 +- 5 files changed, 10 insertions(+), 9 deletions(-)
include/linux/sched.h +2 -1
@@ -1111,7 +1111,8 @@ struct sched_class {void (*prio_changed) (struct rq *this_rq,struct task_struct *task,int oldprio,int running); -unsigned int (*get_rr_interval) (struct task_struct *task); +unsigned int (*get_rr_interval) (struct rq *rq, +struct task_struct *task);#ifdef CONFIG_FAIR_GROUP_SCHED void (*moved_group) (struct task_struct *p);
kernel/sched.c +5 -1
@@ -6887,6 +6887,8 @@ SYSCALL_DEFINE2(sched_rr_get_interval, p {struct task_struct *p;unsigned int time_slice; +unsigned long flags; +struct rq *rq;int retval;struct timespec t;
@@ -6903,7 +6905,9 @@ SYSCALL_DEFINE2(sched_rr_get_interval, pif (retval) goto out_unlock; - time_slice = p->sched_class->get_rr_interval (p); + rq =task_rq_lock (p, &flags); + time_slice = p->sched_class->get_rr_interval (rq, p); +task_rq_unlock (rq, &flags);read_unlock (&tasklist_lock);jiffies_to_timespec (time_slice, &t);
kernel/sched_fair.c +1 -5
@@ -2014,21 +2014,17 @@ static void moved_group_fair(struct task }#endif -unsigned int get_rr_interval_fair (struct task_struct *task) +unsigned int get_rr_interval_fair (struct rq *rq,struct task_struct *task) {struct sched_entity *se = &task->se; -unsigned long flags; -struct rq *rq;unsigned int rr_interval =0 ;/* * Time slice is 0 for SCHED_OTHER tasks that are on an otherwise * idle runqueue: */ - rq =task_rq_lock (task, &flags);if (rq->cfs.load.weight) rr_interval =NS_TO_JIFFIES (sched_slice (&rq->cfs, se)); -task_rq_unlock (rq, &flags);return rr_interval; }
kernel/sched_idletask.c +1 -1
@@ -97,7 +97,7 @@ static void prio_changed_idle(struct rqcheck_preempt_curr (rq, p,0 ); } -unsigned int get_rr_interval_idle (struct task_struct *task) +unsigned int get_rr_interval_idle (struct rq *rq,struct task_struct *task) {return 0 ; }
kernel/sched_rt.c +1 -1
@@ -1721,7 +1721,7 @@ static void set_curr_task_rt(struct rq *dequeue_pushable_task (rq, p); } -unsigned int get_rr_interval_rt (struct task_struct *task) +unsigned int get_rr_interval_rt (struct rq *rq,struct task_struct *task) {/* * Time slice is 0 for SCHED_FIFO tasks
Re: [patch V2 sched: Protect sched_rr_get_param access to task->sched_class
Quoted 9 lines · Thomas Gleixner
sched_rr_get_paramcallstask->sched_class->get_rr_interval(task)without protection against a concurrentsched_setscheduler()call which modifiestask->sched_class.Serialize the access with
task_rq_lock(task)and hand the rq pointer intoget_rr_interval()as it's needed at least in thesched_fairimplementation.Signed-off-by Thomas Gleixner <[email protected]>
[tip:sched/urgent] sched: Protect sched_rr_get_param() access to task->sched_class
In reply to Thomas GleixnerCommit-ID: dba091b9e3522b9d32fc9975e48d3b69633b45f0 Gitweb: http://git.kernel.org/tip/dba091b9e3522b9d32fc9975e48d3b69633b45f0 Author: Thomas Gleixner <[email protected]> AuthorDate: Wed, 9 Dec 2009 09:32:03 +0100 Committer: Ingo Molnar <[email protected]> CommitDate: Wed, 9 Dec 2009 10:01:07 +0100
sched: Protect sched_rr_get_param() access to task->sched_class
sched_rr_get_param calls task->sched_class->get_rr_interval(task) without protection against a concurrent sched_setscheduler() call which modifies task->sched_class.
Serialize the access with task_rq_lock(task) and hand the rq pointer into get_rr_interval() as it's needed at least in the sched_fair implementation.
LKML-Reference: <[email protected]>
--- include/linux/sched.h | 3 ++- kernel/sched.c | 6 +++++- kernel/sched_fair.c | 6 +----- kernel/sched_idletask.c | 2 +- kernel/sched_rt.c | 2 +- 5 files changed, 10 insertions(+), 9 deletions(-)
include/linux/sched.h +2 -1
@@ -1111,7 +1111,8 @@ struct sched_class {void (*prio_changed) (struct rq *this_rq,struct task_struct *task,int oldprio,int running); -unsigned int (*get_rr_interval) (struct task_struct *task); +unsigned int (*get_rr_interval) (struct rq *rq, +struct task_struct *task);#ifdef CONFIG_FAIR_GROUP_SCHED void (*moved_group) (struct task_struct *p);
kernel/sched.c +5 -1
@@ -6887,6 +6887,8 @@ SYSCALL_DEFINE2(sched_rr_get_interval, pid_t, pid, {struct task_struct *p;unsigned int time_slice; +unsigned long flags; +struct rq *rq;int retval;struct timespec t;
@@ -6903,7 +6905,9 @@ SYSCALL_DEFINE2(sched_rr_get_interval, pid_t, pid,if (retval) goto out_unlock; - time_slice = p->sched_class->get_rr_interval (p); + rq =task_rq_lock (p, &flags); + time_slice = p->sched_class->get_rr_interval (rq, p); +task_rq_unlock (rq, &flags);read_unlock (&tasklist_lock);jiffies_to_timespec (time_slice, &t);
kernel/sched_fair.c +1 -5
@@ -2014,21 +2014,17 @@ static void moved_group_fair(struct task_struct *p) }#endif -unsigned int get_rr_interval_fair (struct task_struct *task) +unsigned int get_rr_interval_fair (struct rq *rq,struct task_struct *task) {struct sched_entity *se = &task->se; -unsigned long flags; -struct rq *rq;unsigned int rr_interval =0 ;/* * Time slice is 0 for SCHED_OTHER tasks that are on an otherwise * idle runqueue: */ - rq =task_rq_lock (task, &flags);if (rq->cfs.load.weight) rr_interval =NS_TO_JIFFIES (sched_slice (&rq->cfs, se)); -task_rq_unlock (rq, &flags);return rr_interval; }
kernel/sched_idletask.c +1 -1
@@ -97,7 +97,7 @@ static void prio_changed_idle(struct rq *rq, struct task_struct *p,check_preempt_curr (rq, p,0 ); } -unsigned int get_rr_interval_idle (struct task_struct *task) +unsigned int get_rr_interval_idle (struct rq *rq,struct task_struct *task) {return 0 ; }
kernel/sched_rt.c +1 -1
@@ -1721,7 +1721,7 @@ static void set_curr_task_rt(struct rq *rq)dequeue_pushable_task (rq, p); } -unsigned int get_rr_interval_rt (struct task_struct *task) +unsigned int get_rr_interval_rt (struct rq *rq,struct task_struct *task) {/* * Time slice is 0 for SCHED_FIFO tasks