Skip to contentSkip to conversation

sched: Replace read_lock(&tasklist_lock) with RCU - the easy part

PATCH In review Thomas Gleixner 1 participant 8 messages Dec 9, 2009 – Dec 14, 2009

lore
Share & tools
lore
#0 lore raw

cover sched: Replace read_lock(&tasklist_lock) with RCU - the easy part

First batch of patches which replace read_lock(&tasklist_lock) with RCU.

Thanks,

tglx

#1 lore raw

PATCH 1/4 sched: Use rcu in sys_sched_getscheduler/sys_sched_getparam()

read_lock(&tasklist_lock) does not protect sys_sched_getscheduler and sys_sched_getparam() against a concurrent update of the policy or scheduler parameters as do_sched_setscheduler() does not take the tasklist_lock. The accessed integers can be retrieved w/o locking and are snapshots anyway.

Using rcu_read_lock() to protect find_task_by_vpid() and prevent the task struct from going away is not changing the above situation.

---
 kernel/sched.c |   10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
kernel/sched.c +5 -5
Index: linux-2.6-tip/kernel/sched.c
===================================================================
@@ -6477,7 +6477,7 @@ SYSCALL_DEFINE1(sched_getscheduler, pid_
 		return -EINVAL;
 
 	retval = -ESRCH;
-	read_lock(&tasklist_lock);
+	rcu_read_lock();
 	p = find_process_by_pid(pid);
 	if (p) {
 		retval = security_task_getscheduler(p);
@@ -6485,7 +6485,7 @@ SYSCALL_DEFINE1(sched_getscheduler, pid_
 			retval = p->policy
 				| (p->sched_reset_on_fork ? SCHED_RESET_ON_FORK : 0);
 	}
-	read_unlock(&tasklist_lock);
+	rcu_read_unlock();
 	return retval;
 }
 
@@ -6503,7 +6503,7 @@ SYSCALL_DEFINE2(sched_getparam, pid_t, p
 	if (!param || pid < 0)
 		return -EINVAL;
 
-	read_lock(&tasklist_lock);
+	rcu_read_lock();
 	p = find_process_by_pid(pid);
 	retval = -ESRCH;
 	if (!p)
@@ -6514,7 +6514,7 @@ SYSCALL_DEFINE2(sched_getparam, pid_t, p
 		goto out_unlock;
 
 	lp.sched_priority = p->rt_priority;
-	read_unlock(&tasklist_lock);
+	rcu_read_unlock();
 
 	/*
 	 * This one might sleep, we cannot do it with a spinlock held ...
@@ -6524,7 +6524,7 @@ SYSCALL_DEFINE2(sched_getparam, pid_t, p
 	return retval;
 
 out_unlock:
-	read_unlock(&tasklist_lock);
+	rcu_read_unlock();
 	return retval;
 }
 
#3 lore raw

PATCH 2/4 sched: Use rcu in sched_get/set_affinity()

In reply to Thomas Gleixner

tasklist_lock is held read locked to protect the find_task_by_vpid() call and to prevent the task going away. sched_setaffinity acquires a task struct ref and drops tasklist lock right away. The access to the cpus_allowed mask is protected by rq->lock.

rcu_read_lock() provides the same protection here.

---
 kernel/sched.c |   16 ++++++----------
 1 file changed, 6 insertions(+), 10 deletions(-)
kernel/sched.c +6 -10
Index: linux-2.6-tip/kernel/sched.c
===================================================================
@@ -6535,22 +6535,18 @@ long sched_setaffinity(pid_t pid, const 
 	int retval;
 
 	get_online_cpus();
-	read_lock(&tasklist_lock);
+	rcu_read_lock();
 
 	p = find_process_by_pid(pid);
 	if (!p) {
-		read_unlock(&tasklist_lock);
+		rcu_read_unlock();
 		put_online_cpus();
 		return -ESRCH;
 	}
 
-	/*
-	 * It is not safe to call set_cpus_allowed with the
-	 * tasklist_lock held. We will bump the task_struct's
-	 * usage count and then drop tasklist_lock.
-	 */
+	/* Prevent p going away */
 	get_task_struct(p);
-	read_unlock(&tasklist_lock);
+	rcu_read_unlock();
 
 	if (!alloc_cpumask_var(&cpus_allowed, GFP_KERNEL)) {
 		retval = -ENOMEM;
@@ -6636,7 +6632,7 @@ long sched_getaffinity(pid_t pid, struct
 	int retval;
 
 	get_online_cpus();
-	read_lock(&tasklist_lock);
+	rcu_read_lock();
 
 	retval = -ESRCH;
 	p = find_process_by_pid(pid);
@@ -6652,7 +6648,7 @@ long sched_getaffinity(pid_t pid, struct
 	task_rq_unlock(rq, &flags);
 
 out_unlock:
-	read_unlock(&tasklist_lock);
+	rcu_read_unlock();
 	put_online_cpus();
 
 	return retval;
#5 lore raw

PATCH 3/4 sched: Rename bogus label in sched_setaffinity()

In reply to Thomas Gleixner

out_unlock: is bogus as there is nothing to unlock. Rename it.

---
 kernel/sched.c |    6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
kernel/sched.c +3 -3
Index: linux-2.6-tip/kernel/sched.c
===================================================================
@@ -6558,11 +6558,11 @@ long sched_setaffinity(pid_t pid, const 
 	}
 	retval = -EPERM;
 	if (!check_same_owner(p) && !capable(CAP_SYS_NICE))
-		goto out_unlock;
+		goto out_free;
 
 	retval = security_task_setscheduler(p, 0, NULL);
 	if (retval)
-		goto out_unlock;
+		goto out_free;
 
 	cpuset_cpus_allowed(p, cpus_allowed);
 	cpumask_and(new_mask, in_mask, cpus_allowed);
@@ -6581,7 +6581,7 @@ long sched_setaffinity(pid_t pid, const 
 			goto again;
 		}
 	}
-out_unlock:
+out_free:
 	free_cpumask_var(new_mask);
 out_free_cpus_allowed:
 	free_cpumask_var(cpus_allowed);
#6 lore raw

PATCH 4/4 sched: Use rcu in sched_get_rr_param

In reply to Thomas Gleixner

read_lock(&tasklist_lock) does not protect sys_sched_get_rr_param() against a concurrent update of the policy or scheduler parameters as do_sched_scheduler() does not take the tasklist_lock.

The access to task->sched_class->get_rr_interval is protected by task_rq_lock(task).

Use rcu_read_lock() to protect find_task_by_vpid() and prevent the task struct from going away.

---
 kernel/sched.c |    6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
kernel/sched.c +3 -3
Index: linux-2.6-tip/kernel/sched.c
===================================================================
@@ -6892,7 +6892,7 @@ SYSCALL_DEFINE2(sched_rr_get_interval, p
 		return -EINVAL;
 
 	retval = -ESRCH;
-	read_lock(&tasklist_lock);
+	rcu_read_lock();
 	p = find_process_by_pid(pid);
 	if (!p)
 		goto out_unlock;
@@ -6905,13 +6905,13 @@ SYSCALL_DEFINE2(sched_rr_get_interval, p
 	time_slice = p->sched_class->get_rr_interval(rq, p);
 	task_rq_unlock(rq, &flags);
 
-	read_unlock(&tasklist_lock);
+	rcu_read_unlock();
 	jiffies_to_timespec(time_slice, &t);
 	retval = copy_to_user(interval, &t, sizeof(t)) ? -EFAULT : 0;
 	return retval;
 
 out_unlock:
-	read_unlock(&tasklist_lock);
+	rcu_read_unlock();
 	return retval;
 }
 
#4 lore raw

[tip:sched/urgent] sched: Use rcu in sched_get/set_affinity()

In reply to Thomas Gleixner
Commit-ID:  23f5d142519621b16cf2b378cf8adf4dcf01a616
Gitweb:     http://git.kernel.org/tip/23f5d142519621b16cf2b378cf8adf4dcf01a616
Author:     Thomas Gleixner <[email protected]>
AuthorDate: Wed, 9 Dec 2009 10:15:01 +0000
Committer:  Ingo Molnar <[email protected]>
CommitDate: Mon, 14 Dec 2009 17:11:35 +0100

sched: Use rcu in sched_get/set_affinity()

tasklist_lock is held read locked to protect the find_task_by_vpid() call and to prevent the task going away. sched_setaffinity acquires a task struct ref and drops tasklist lock right away. The access to the cpus_allowed mask is protected by rq->lock.

rcu_read_lock() provides the same protection here.

LKML-Reference: <[email protected]>

---
 kernel/sched.c |   16 ++++++----------
 1 files changed, 6 insertions(+), 10 deletions(-)
kernel/sched.c +6 -10
index 1782bee..7989312 100644
@@ -6516,22 +6516,18 @@ long sched_setaffinity(pid_t pid, const struct cpumask *in_mask)
 	int retval;
 
 	get_online_cpus();
-	read_lock(&tasklist_lock);
+	rcu_read_lock();
 
 	p = find_process_by_pid(pid);
 	if (!p) {
-		read_unlock(&tasklist_lock);
+		rcu_read_unlock();
 		put_online_cpus();
 		return -ESRCH;
 	}
 
-	/*
-	 * It is not safe to call set_cpus_allowed with the
-	 * tasklist_lock held. We will bump the task_struct's
-	 * usage count and then drop tasklist_lock.
-	 */
+	/* Prevent p going away */
 	get_task_struct(p);
-	read_unlock(&tasklist_lock);
+	rcu_read_unlock();
 
 	if (!alloc_cpumask_var(&cpus_allowed, GFP_KERNEL)) {
 		retval = -ENOMEM;
@@ -6617,7 +6613,7 @@ long sched_getaffinity(pid_t pid, struct cpumask *mask)
 	int retval;
 
 	get_online_cpus();
-	read_lock(&tasklist_lock);
+	rcu_read_lock();
 
 	retval = -ESRCH;
 	p = find_process_by_pid(pid);
@@ -6633,7 +6629,7 @@ long sched_getaffinity(pid_t pid, struct cpumask *mask)
 	task_rq_unlock(rq, &flags);
 
 out_unlock:
-	read_unlock(&tasklist_lock);
+	rcu_read_unlock();
 	put_online_cpus();
 
 	return retval;
#7 lore raw

[tip:sched/urgent] sched: Use rcu in sched_get_rr_param()

In reply to Thomas Gleixner
Commit-ID:  1a551ae715825bb2a2107a2dd68de024a1fa4e32
Gitweb:     http://git.kernel.org/tip/1a551ae715825bb2a2107a2dd68de024a1fa4e32
Author:     Thomas Gleixner <[email protected]>
AuthorDate: Wed, 9 Dec 2009 10:15:11 +0000
Committer:  Ingo Molnar <[email protected]>
CommitDate: Mon, 14 Dec 2009 17:11:35 +0100

sched: Use rcu in sched_get_rr_param()

read_lock(&tasklist_lock) does not protect sys_sched_get_rr_param() against a concurrent update of the policy or scheduler parameters as do_sched_scheduler() does not take the tasklist_lock.

The access to task->sched_class->get_rr_interval is protected by task_rq_lock(task).

Use rcu_read_lock() to protect find_task_by_vpid() and prevent the task struct from going away.

LKML-Reference: <[email protected]>

---
 kernel/sched.c |    6 +++---
 1 files changed, 3 insertions(+), 3 deletions(-)
kernel/sched.c +3 -3
index 7989312..db5c266 100644
@@ -6873,7 +6873,7 @@ SYSCALL_DEFINE2(sched_rr_get_interval, pid_t, pid,
 		return -EINVAL;
 
 	retval = -ESRCH;
-	read_lock(&tasklist_lock);
+	rcu_read_lock();
 	p = find_process_by_pid(pid);
 	if (!p)
 		goto out_unlock;
@@ -6886,13 +6886,13 @@ SYSCALL_DEFINE2(sched_rr_get_interval, pid_t, pid,
 	time_slice = p->sched_class->get_rr_interval(rq, p);
 	task_rq_unlock(rq, &flags);
 
-	read_unlock(&tasklist_lock);
+	rcu_read_unlock();
 	jiffies_to_timespec(time_slice, &t);
 	retval = copy_to_user(interval, &t, sizeof(t)) ? -EFAULT : 0;
 	return retval;
 
 out_unlock:
-	read_unlock(&tasklist_lock);
+	rcu_read_unlock();
 	return retval;
 }
 
#2 lore raw

[tip:sched/urgent] sched: Use rcu in sys_sched_getscheduler/sys_sched_getparam()

In reply to Thomas Gleixner
Commit-ID:  5fe85be081edf0ac92d83f9c39e0ab5c1371eb82
Gitweb:     http://git.kernel.org/tip/5fe85be081edf0ac92d83f9c39e0ab5c1371eb82
Author:     Thomas Gleixner <[email protected]>
AuthorDate: Wed, 9 Dec 2009 10:14:58 +0000
Committer:  Ingo Molnar <[email protected]>
CommitDate: Mon, 14 Dec 2009 17:11:34 +0100

sched: Use rcu in sys_sched_getscheduler/sys_sched_getparam()

read_lock(&tasklist_lock) does not protect sys_sched_getscheduler and sys_sched_getparam() against a concurrent update of the policy or scheduler parameters as do_sched_setscheduler() does not take the tasklist_lock. The accessed integers can be retrieved w/o locking and are snapshots anyway.

Using rcu_read_lock() to protect find_task_by_vpid() and prevent the task struct from going away is not changing the above situation.

LKML-Reference: <[email protected]>

---
 kernel/sched.c |   10 +++++-----
 1 files changed, 5 insertions(+), 5 deletions(-)
kernel/sched.c +5 -5
index 258c73c..1782bee 100644
@@ -6458,7 +6458,7 @@ SYSCALL_DEFINE1(sched_getscheduler, pid_t, pid)
 		return -EINVAL;
 
 	retval = -ESRCH;
-	read_lock(&tasklist_lock);
+	rcu_read_lock();
 	p = find_process_by_pid(pid);
 	if (p) {
 		retval = security_task_getscheduler(p);
@@ -6466,7 +6466,7 @@ SYSCALL_DEFINE1(sched_getscheduler, pid_t, pid)
 			retval = p->policy
 				| (p->sched_reset_on_fork ? SCHED_RESET_ON_FORK : 0);
 	}
-	read_unlock(&tasklist_lock);
+	rcu_read_unlock();
 	return retval;
 }
 
@@ -6484,7 +6484,7 @@ SYSCALL_DEFINE2(sched_getparam, pid_t, pid, struct sched_param __user *, param)
 	if (!param || pid < 0)
 		return -EINVAL;
 
-	read_lock(&tasklist_lock);
+	rcu_read_lock();
 	p = find_process_by_pid(pid);
 	retval = -ESRCH;
 	if (!p)
@@ -6495,7 +6495,7 @@ SYSCALL_DEFINE2(sched_getparam, pid_t, pid, struct sched_param __user *, param)
 		goto out_unlock;
 
 	lp.sched_priority = p->rt_priority;
-	read_unlock(&tasklist_lock);
+	rcu_read_unlock();
 
 	/*
 	 * This one might sleep, we cannot do it with a spinlock held ...
@@ -6505,7 +6505,7 @@ SYSCALL_DEFINE2(sched_getparam, pid_t, pid, struct sched_param __user *, param)
 	return retval;
 
 out_unlock:
-	read_unlock(&tasklist_lock);
+	rcu_read_unlock();
 	return retval;
 }