Skip to contentSkip to conversation

sched: Protect sched_rr_get_param access to task->sched_class

PATCH In review Thomas Gleixner 2 participants 7 messages Dec 8, 2009 – Dec 9, 2009 Scheduler

lore
Share & tools
lore
#0 lore raw

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
Index: linux-2.6-tip/kernel/sched.c
===================================================================
@@ -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, p
 	if (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);
maintainer Acked-by on v1 2/2
#1 lore raw
Quoted 28 lines · Thomas Gleixner

plain text document attachment (sched-fix-sched-get-rr-param-race.patch) 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(+)

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, p
 	if (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);
maintainer
#2 lore raw
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_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(+)

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, p
 	if (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 -0
 	read_unlock(&tasklist_lock);
 	jiffies_to_timespec(time_slice, &t);

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;

}

#3 lore raw
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_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(+)

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, p
 	if (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 -0
 	read_unlock(&tasklist_lock);
 	jiffies_to_timespec(time_slice, &t);

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;
(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

#4 lore raw

[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
Index: linux-2.6-tip/include/linux/sched.h
===================================================================
@@ -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
Index: linux-2.6-tip/kernel/sched.c
===================================================================
@@ -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, p
 	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
Index: linux-2.6-tip/kernel/sched_fair.c
===================================================================
@@ -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
Index: linux-2.6-tip/kernel/sched_idletask.c
===================================================================
@@ -97,7 +97,7 @@ static void prio_changed_idle(struct rq 
 		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
Index: linux-2.6-tip/kernel/sched_rt.c
===================================================================
@@ -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
maintainer Acked-by
#5 lore raw

Re: [patch V2 sched: Protect sched_rr_get_param access to task->sched_class

Quoted 9 lines · Thomas Gleixner

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.

#6 lore raw

[tip:sched/urgent] sched: Protect sched_rr_get_param() access to task->sched_class

In reply to Thomas Gleixner
Commit-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
index 89115ec..9b24027 100644
@@ -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
index c4635f7..68db5a2 100644
@@ -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
index f61837a..613c1c7 100644
@@ -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
index b133a28..33d5384 100644
@@ -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
index 5c5fef3..aecbd9c 100644
@@ -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