Skip to contentSkip to conversation

sched: Protect task->cpus_allowed access in sched_getaffinity()

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

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 sched: Protect task->cpus_allowed access in sched_getaffinity()

sched_getaffinity() is not protected against a concurrent modification of the tasks affinity.

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
===================================================================
@@ -6631,6 +6631,8 @@ SYSCALL_DEFINE3(sched_setaffinity, pid_t
 long sched_getaffinity(pid_t pid, struct cpumask *mask)
 {
 	struct task_struct *p;
+	unsigned long flags;
+	struct rq *rq;
 	int retval;
 
 	get_online_cpus();
@@ -6645,7 +6647,9 @@ long sched_getaffinity(pid_t pid, struct
 	if (retval)
 		goto out_unlock;
 
+	rq = task_rq_lock(p, &flags);
 	cpumask_and(mask, &p->cpus_allowed, cpu_online_mask);
+	task_rq_unlock(rq, &flags);
 
 out_unlock:
 	read_unlock(&tasklist_lock);
Acked-by on v1 1/2
#1 lore raw
Quoted 27 lines · Thomas Gleixner

plain text document attachment (sched-fix-sched-get-affinity-racy-access.patch) sched_getaffinity() is not protected against a concurrent modification of the tasks affinity.

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
@@ -6631,6 +6631,8 @@ SYSCALL_DEFINE3(sched_setaffinity, pid_t
 long sched_getaffinity(pid_t pid, struct cpumask *mask)
 {
 	struct task_struct *p;
+	unsigned long flags;
+	struct rq *rq;
 	int retval;
 
 	get_online_cpus();

kernel/sched.c long sched_getaffinity(pid_t pid, struct · line 6655

Quoted 9 lines
kernel/sched.c +2 -0
@@ -6645,7 +6647,9 @@ long sched_getaffinity(pid_t pid, struct
 	if (retval)
 		goto out_unlock;
 
+	rq = task_rq_lock(p, &flags);
 	cpumask_and(mask, &p->cpus_allowed, cpu_online_mask);
+	task_rq_unlock(rq, &flags);
 
 out_unlock:
 	read_unlock(&tasklist_lock);

Hehe, I wrote the exact same patch last night :-)

#2 lore raw
In reply to Thomas Gleixner
Commit-ID:  3160568371da441b7f2fb57f2f1225404207e8f2
Gitweb:     http://git.kernel.org/tip/3160568371da441b7f2fb57f2f1225404207e8f2
Author:     Thomas Gleixner <[email protected]>
AuthorDate: Tue, 8 Dec 2009 20:24:16 +0000
Committer:  Ingo Molnar <[email protected]>
CommitDate: Wed, 9 Dec 2009 10:01:06 +0100

sched: Protect task->cpus_allowed access in sched_getaffinity()

sched_getaffinity() is not protected against a concurrent modification of the tasks affinity.

Serialize the access with task_rq_lock(task).

LKML-Reference: <[email protected]>

---
 kernel/sched.c |    4 ++++
 1 files changed, 4 insertions(+), 0 deletions(-)
kernel/sched.c +4 -0
index 281da29..c4635f7 100644
@@ -6631,6 +6631,8 @@ SYSCALL_DEFINE3(sched_setaffinity, pid_t, pid, unsigned int, len,
 long sched_getaffinity(pid_t pid, struct cpumask *mask)
 {
 	struct task_struct *p;
+	unsigned long flags;
+	struct rq *rq;
 	int retval;
 
 	get_online_cpus();
@@ -6645,7 +6647,9 @@ long sched_getaffinity(pid_t pid, struct cpumask *mask)
 	if (retval)
 		goto out_unlock;
 
+	rq = task_rq_lock(p, &flags);
 	cpumask_and(mask, &p->cpus_allowed, cpu_online_mask);
+	task_rq_unlock(rq, &flags);
 
 out_unlock:
 	read_unlock(&tasklist_lock);