Skip to contentSkip to conversation

posix-cpu-timers: rcu_read_lock/unlock protect find_task_by_vpid call

PATCH In review Sergey Senozhatsky 5 participants 14 messages Nov 2, 2010 – Nov 9, 2010

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 posix-cpu-timers: rcu_read_lock/unlock protect find_task_by_vpid call

Commit 4221a9918e38b7494cee341dda7b7b4bb8c04bde "Add RCU check for find_task_by_vpid()" introduced rcu_lockdep_assert to find_task_by_pid_ns. Add rcu_read_lock/rcu_read_unlock to call find_task_by_vpid.

Tetsuo Handa wrote:

Quoting from one of posts in that thead
http://kerneltrap.org/mailarchive/linux-kernel/2010/2/8/4536388

| Usually tasklist gives enough protection, but if copy_process() fails
| it calls free_pid() lockless and does call_rcu(delayed_put_pid().   
| This means, without rcu lock find_pid_ns() can't scan the hash table
| safely.

---

kernel/posix-cpu-timers.c +6 -0
index 6842eeb..855bc53 100644
@@ -38,11 +38,13 @@ static int check_clock(const clockid_t which_clock)
 		return 0;
 
 	read_lock(&tasklist_lock);
+	rcu_read_lock();
 	p = find_task_by_vpid(pid);
 	if (!p || !(CPUCLOCK_PERTHREAD(which_clock) ?
 		   same_thread_group(p, current) : thread_group_leader(p))) {
 		error = -EINVAL;
 	}
+	rcu_read_unlock();
 	read_unlock(&tasklist_lock);
 
 	return error;
@@ -395,17 +397,21 @@ int posix_cpu_timer_create(struct k_itimer *new_timer)
 		if (pid == 0) {
 			p = current;
 		} else {
+			rcu_read_lock();
 			p = find_task_by_vpid(pid);
 			if (p && !same_thread_group(p, current))
 				p = NULL;
+			rcu_read_unlock();
 		}
 	} else {
 		if (pid == 0) {
 			p = current->group_leader;
 		} else {
+			rcu_read_lock();
 			p = find_task_by_vpid(pid);
 			if (p && !thread_group_leader(p))
 				p = NULL;
+			rcu_read_unlock();
 		}
 	}
 	new_timer->it.cpu.task = p;
#1 lore raw
Quoted 6 lines · Sergey Senozhatsky

Commit 4221a9918e38b7494cee341dda7b7b4bb8c04bde "Add RCU check for find_task_by_vpid()" introduced rcu_lockdep_assert to find_task_by_pid_ns. Add rcu_read_lock/rcu_read_unlock to call find_task_by_vpid.

Tetsuo Handa wrote:

Quoted 7 lines

Quoting from one of posts in that thead
http://kerneltrap.org/mailarchive/linux-kernel/2010/2/8/4536388

| Usually tasklist gives enough protection, but if copy_process() fails
| it calls free_pid() lockless and does call_rcu(delayed_put_pid().   
| This means, without rcu lock find_pid_ns() can't scan the hash table
| safely.

We can remove the tasklist_lock while at it. rcu_read_lock is enough.

Thanks,

tglx

Quoted 46 lines

---

kernel/posix-cpu-timers.c +6 -0
index 6842eeb..855bc53 100644
@@ -38,11 +38,13 @@ static int check_clock(const clockid_t which_clock)
 		return 0;
 
 	read_lock(&tasklist_lock);
+	rcu_read_lock();
 	p = find_task_by_vpid(pid);
 	if (!p || !(CPUCLOCK_PERTHREAD(which_clock) ?
 		   same_thread_group(p, current) : thread_group_leader(p))) {
 		error = -EINVAL;
 	}
+	rcu_read_unlock();
 	read_unlock(&tasklist_lock);
 
 	return error;
@@ -395,17 +397,21 @@ int posix_cpu_timer_create(struct k_itimer *new_timer)
 		if (pid == 0) {
 			p = current;
 		} else {
+			rcu_read_lock();
 			p = find_task_by_vpid(pid);
 			if (p && !same_thread_group(p, current))
 				p = NULL;
+			rcu_read_unlock();
 		}
 	} else {
 		if (pid == 0) {
 			p = current->group_leader;
 		} else {
+			rcu_read_lock();
 			p = find_task_by_vpid(pid);
 			if (p && !thread_group_leader(p))
 				p = NULL;
+			rcu_read_unlock();
 		}
 	}
 	new_timer->it.cpu.task = p;
#2 lore raw
Quoted 11 lines · Thomas Gleixner
Quoted 9 lines · Sergey Senozhatsky

Commit 4221a9918e38b7494cee341dda7b7b4bb8c04bde "Add RCU check for find_task_by_vpid()" introduced rcu_lockdep_assert to find_task_by_pid_ns. Add rcu_read_lock/rcu_read_unlock to call find_task_by_vpid.

Tetsuo Handa wrote:

Quoting from one of posts in that thead
http://kerneltrap.org/mailarchive/linux-kernel/2010/2/8/4536388

Quoted 7 lines
Quoted 4 lines
| Usually tasklist gives enough protection, but if copy_process() fails
| it calls free_pid() lockless and does call_rcu(delayed_put_pid().   
| This means, without rcu lock find_pid_ns() can't scan the hash table
| safely.

We can remove the tasklist_lock while at it. rcu_read_lock is enough.

Please kindly review.

---

kernel/posix-cpu-timers.c +4 -4
index 6842eeb..96fe2a3 100644
@@ -37,13 +37,13 @@ static int check_clock(const clockid_t which_clock)
 	if (pid == 0)
 		return 0;
 
-	read_lock(&tasklist_lock);
+	rcu_read_lock();
 	p = find_task_by_vpid(pid);
 	if (!p || !(CPUCLOCK_PERTHREAD(which_clock) ?
 		   same_thread_group(p, current) : thread_group_leader(p))) {
 		error = -EINVAL;
 	}
-	read_unlock(&tasklist_lock);
+	rcu_read_unlock();
 
 	return error;
 }
@@ -390,7 +390,7 @@ int posix_cpu_timer_create(struct k_itimer *new_timer)
 
 	INIT_LIST_HEAD(&new_timer->it.cpu.entry);
 
-	read_lock(&tasklist_lock);
+	rcu_read_lock();
 	if (CPUCLOCK_PERTHREAD(new_timer->it_clock)) {
 		if (pid == 0) {
 			p = current;
@@ -414,7 +414,7 @@ int posix_cpu_timer_create(struct k_itimer *new_timer)
 	} else {
 		ret = -EINVAL;
 	}
-	read_unlock(&tasklist_lock);
+	rcu_read_unlock();
 
 	return ret;
 }
#3 lore raw

Just added Oleg to the cc-list

Quoted 64 lines · Sergey Senozhatsky
Quoted 11 lines · Thomas Gleixner
Quoted 9 lines · Sergey Senozhatsky

Commit 4221a9918e38b7494cee341dda7b7b4bb8c04bde "Add RCU check for find_task_by_vpid()" introduced rcu_lockdep_assert to find_task_by_pid_ns. Add rcu_read_lock/rcu_read_unlock to call find_task_by_vpid.

Tetsuo Handa wrote:

Quoting from one of posts in that thead
http://kerneltrap.org/mailarchive/linux-kernel/2010/2/8/4536388

Quoted 7 lines
Quoted 4 lines
| Usually tasklist gives enough protection, but if copy_process() fails
| it calls free_pid() lockless and does call_rcu(delayed_put_pid().   
| This means, without rcu lock find_pid_ns() can't scan the hash table
| safely.

We can remove the tasklist_lock while at it. rcu_read_lock is enough.

Please kindly review.

---

kernel/posix-cpu-timers.c +4 -4
index 6842eeb..96fe2a3 100644
@@ -37,13 +37,13 @@ static int check_clock(const clockid_t which_clock)
 	if (pid == 0)
 		return 0;
 
-	read_lock(&tasklist_lock);
+	rcu_read_lock();
 	p = find_task_by_vpid(pid);
 	if (!p || !(CPUCLOCK_PERTHREAD(which_clock) ?
 		   same_thread_group(p, current) : thread_group_leader(p))) {
 		error = -EINVAL;
 	}
-	read_unlock(&tasklist_lock);
+	rcu_read_unlock();
 
 	return error;
 }
@@ -390,7 +390,7 @@ int posix_cpu_timer_create(struct k_itimer *new_timer)
 
 	INIT_LIST_HEAD(&new_timer->it.cpu.entry);
 
-	read_lock(&tasklist_lock);
+	rcu_read_lock();
 	if (CPUCLOCK_PERTHREAD(new_timer->it_clock)) {
 		if (pid == 0) {
 			p = current;
@@ -414,7 +414,7 @@ int posix_cpu_timer_create(struct k_itimer *new_timer)
 	} else {
 		ret = -EINVAL;
 	}
-	read_unlock(&tasklist_lock);
+	rcu_read_unlock();
 
 	return ret;
 }
#4 lore raw
Quoted 14 lines · Sergey Senozhatsky
Quoted 13 lines · Thomas Gleixner
Quoted 11 lines · Sergey Senozhatsky
Quoted 10 lines

Commit 4221a9918e38b7494cee341dda7b7b4bb8c04bde "Add RCU check for find_task_by_vpid()" introduced rcu_lockdep_assert to find_task_by_pid_ns. Add rcu_read_lock/rcu_read_unlock to call find_task_by_vpid.

Tetsuo Handa wrote:

Quoting from one of posts in that thead
http://kerneltrap.org/mailarchive/linux-kernel/2010/2/8/4536388

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 4 lines
| Usually tasklist gives enough protection, but if copy_process() fails
| it calls free_pid() lockless and does call_rcu(delayed_put_pid().
| This means, without rcu lock find_pid_ns() can't scan the hash table
| safely.

We can remove the tasklist_lock while at it. rcu_read_lock is enough.

Yes, I believe posix-cpu-timers.c shouldn't use tasklist at all, but it is not trivial to change this code.

Minor nit,

Quoted 4 lines
Quoted 4 lines
kernel/posix-cpu-timers.c +0 -0
@@ -390,7 +390,7 @@ int posix_cpu_timer_create(struct k_itimer *new_timer)
 
 	INIT_LIST_HEAD(&new_timer->it.cpu.entry);
 
Quoted 11 lines

kernel/posix-cpu-timers.c int posix_cpu_timer_create(struct k_itimer *new_timer) · line 417

Quoted 11 lines
kernel/posix-cpu-timers.c +2 -2
-	read_lock(&tasklist_lock);
+	rcu_read_lock();
 	if (CPUCLOCK_PERTHREAD(new_timer->it_clock)) {
 		if (pid == 0) {
 			p = current;
@@ -414,7 +414,7 @@ int posix_cpu_timer_create(struct k_itimer *new_timer)
 	} else {
 		ret = -EINVAL;
 	}
-	read_unlock(&tasklist_lock);
+	rcu_read_unlock();

I think this change is fine, but please note that thread_group_leader() check is not relaible without tasklist. If we race with de_thread() find_task_by_vpid() can find the new leader before it updates its ->group_leader. IOW, posix_cpu_timer_create() can fail when it shouldn't.

Not that I think this really matters, posix_cpu_timer_create() has other problems with de_thread(). But perhaps it makes sense to change posix_cpu_timer_create() to use has_group_leader_pid() instead, just to make this code not look racy and avoid adding new problems.

The real fix, I think, should change cpu_timer_list to use struct pid* instead of task_struct.

Oleg.

#5 lore raw
Quoted 15 lines · Oleg Nesterov
Quoted 4 lines · Sergey Senozhatsky
Quoted 3 lines
Quoted 3 lines

We can remove the tasklist_lock while at it. rcu_read_lock is enough.

Yes, I believe posix-cpu-timers.c shouldn't use tasklist at all, but it is not trivial to change this code.

[..]

I think this change is fine, but please note that thread_group_leader() check is not relaible without tasklist. If we race with de_thread() find_task_by_vpid() can find the new leader before it updates its ->group_leader. IOW, posix_cpu_timer_create() can fail when it shouldn't.

Quoted 8 lines

Not that I think this really matters, posix_cpu_timer_create() has other problems with de_thread(). But perhaps it makes sense to change posix_cpu_timer_create() to use has_group_leader_pid() instead, just to make this code not look racy and avoid adding new problems.

The real fix, I think, should change cpu_timer_list to use struct pid* instead of task_struct.

Hello,
Using has_group_leader_pid instead of thread_group_leader, when tasklist_lock is not aquired (check_clock and posix_cpu_timer_create).

---

kernel/posix-cpu-timers.c +6 -6
index 6842eeb..05bb717 100644
@@ -37,13 +37,13 @@ static int check_clock(const clockid_t which_clock)
 	if (pid == 0)
 		return 0;
 
-	read_lock(&tasklist_lock);
+	rcu_read_lock();
 	p = find_task_by_vpid(pid);
 	if (!p || !(CPUCLOCK_PERTHREAD(which_clock) ?
-		   same_thread_group(p, current) : thread_group_leader(p))) {
+		   same_thread_group(p, current) : has_group_leader_pid(p))) {
 		error = -EINVAL;
 	}
-	read_unlock(&tasklist_lock);
+	rcu_read_unlock();
 
 	return error;
 }
@@ -390,7 +390,7 @@ int posix_cpu_timer_create(struct k_itimer *new_timer)
 
 	INIT_LIST_HEAD(&new_timer->it.cpu.entry);
 
-	read_lock(&tasklist_lock);
+	rcu_read_lock();
 	if (CPUCLOCK_PERTHREAD(new_timer->it_clock)) {
 		if (pid == 0) {
 			p = current;
@@ -404,7 +404,7 @@ int posix_cpu_timer_create(struct k_itimer *new_timer)
 			p = current->group_leader;
 		} else {
 			p = find_task_by_vpid(pid);
-			if (p && !thread_group_leader(p))
+			if (p && !has_group_leader_pid(p))
 				p = NULL;
 		}
 	}
@@ -414,7 +414,7 @@ int posix_cpu_timer_create(struct k_itimer *new_timer)
 	} else {
 		ret = -EINVAL;
 	}
-	read_unlock(&tasklist_lock);
+	rcu_read_unlock();
 
 	return ret;
 }
#6 lore raw

Damn.

Sergey, Thomas, please wait a bit.

Yes, I believe this patch is fine by itself. But looking into posix-cpu-timers.c again, I suspect that those "other problems with de_thread" I already mentioned are much more serious and need the urgent fix.

I'll try to verify this a bit later today.

In any case, I believe someone should find the time to audit/ rewrite posix-cpu-timers.c ;)

Quoted 82 lines · Sergey Senozhatsky
Quoted 15 lines · Oleg Nesterov
Quoted 4 lines · Sergey Senozhatsky
Quoted 3 lines
Quoted 3 lines

We can remove the tasklist_lock while at it. rcu_read_lock is enough.

Yes, I believe posix-cpu-timers.c shouldn't use tasklist at all, but it is not trivial to change this code.

[..]

I think this change is fine, but please note that thread_group_leader() check is not relaible without tasklist. If we race with de_thread() find_task_by_vpid() can find the new leader before it updates its ->group_leader. IOW, posix_cpu_timer_create() can fail when it shouldn't.

Quoted 8 lines

Not that I think this really matters, posix_cpu_timer_create() has other problems with de_thread(). But perhaps it makes sense to change posix_cpu_timer_create() to use has_group_leader_pid() instead, just to make this code not look racy and avoid adding new problems.

The real fix, I think, should change cpu_timer_list to use struct pid* instead of task_struct.

Hello,
Using has_group_leader_pid instead of thread_group_leader, when tasklist_lock is not aquired (check_clock and posix_cpu_timer_create).

---

kernel/posix-cpu-timers.c +6 -6
index 6842eeb..05bb717 100644
@@ -37,13 +37,13 @@ static int check_clock(const clockid_t which_clock)
 	if (pid == 0)
 		return 0;
 
-	read_lock(&tasklist_lock);
+	rcu_read_lock();
 	p = find_task_by_vpid(pid);
 	if (!p || !(CPUCLOCK_PERTHREAD(which_clock) ?
-		   same_thread_group(p, current) : thread_group_leader(p))) {
+		   same_thread_group(p, current) : has_group_leader_pid(p))) {
 		error = -EINVAL;
 	}
-	read_unlock(&tasklist_lock);
+	rcu_read_unlock();
 
 	return error;
 }
@@ -390,7 +390,7 @@ int posix_cpu_timer_create(struct k_itimer *new_timer)
 
 	INIT_LIST_HEAD(&new_timer->it.cpu.entry);
 
-	read_lock(&tasklist_lock);
+	rcu_read_lock();
 	if (CPUCLOCK_PERTHREAD(new_timer->it_clock)) {
 		if (pid == 0) {
 			p = current;
@@ -404,7 +404,7 @@ int posix_cpu_timer_create(struct k_itimer *new_timer)
 			p = current->group_leader;
 		} else {
 			p = find_task_by_vpid(pid);
-			if (p && !thread_group_leader(p))
+			if (p && !has_group_leader_pid(p))
 				p = NULL;
 		}
 	}
@@ -414,7 +414,7 @@ int posix_cpu_timer_create(struct k_itimer *new_timer)
 	} else {
 		ret = -EINVAL;
 	}
-	read_unlock(&tasklist_lock);
+	rcu_read_unlock();
 
 	return ret;
 }
Reviewed-by
#7 lore raw
Quoted 7 lines · Oleg Nesterov

Sergey, Thomas, please wait a bit.

Yes, I believe this patch is fine by itself. But looking into posix-cpu-timers.c again, I suspect that those "other problems with de_thread" I already mentioned are much more serious and need the urgent fix.

Yes. I'll send the patch tomorrow.

However, my initial thinking was wrong, that bug is orthogonal to this patch.

Sergey, how much will you hate me if I ask you to re-send it again?

Quoted 21 lines
Quoted 21 lines · Oleg Nesterov
Quoted 20 lines
Quoted 4 lines · Sergey Senozhatsky
Quoted 3 lines
Quoted 3 lines

We can remove the tasklist_lock while at it. rcu_read_lock is enough.

Yes, I believe posix-cpu-timers.c shouldn't use tasklist at all, but it is not trivial to change this code.

[..]

I think this change is fine, but please note that thread_group_leader() check is not relaible without tasklist. If we race with de_thread() find_task_by_vpid() can find the new leader before it updates its ->group_leader. IOW, posix_cpu_timer_create() can fail when it shouldn't.

Not that I think this really matters, posix_cpu_timer_create() has other problems with de_thread(). But perhaps it makes sense to change posix_cpu_timer_create() to use has_group_leader_pid() instead, just to make this code not look racy and avoid adding new problems.

Quoted 7 lines
Quoted 7 lines
Quoted 3 lines

The real fix, I think, should change cpu_timer_list to use struct pid* instead of task_struct.

Hello,
Using has_group_leader_pid instead of thread_group_leader, when tasklist_lock is not aquired (check_clock and posix_cpu_timer_create).

This doesn't look like a valid changelog ;) I'd suggest you to write the new one without quoting old emails. It should explain that a) tasklist_lock is not enough for find_vpid() and b) it is not needed.

Also, you forgot to add your signed-of-by.

Otherwise,

#8 lore raw
Quoted 9 lines · Oleg Nesterov
Quoted 7 lines · Oleg Nesterov

Sergey, Thomas, please wait a bit.

Yes, I believe this patch is fine by itself. But looking into posix-cpu-timers.c again, I suspect that those "other problems with de_thread" I already mentioned are much more serious and need the urgent fix.

Quoted 8 lines

Yes. I'll send the patch tomorrow.

However, my initial thinking was wrong, that bug is orthogonal to this patch.

Sergey, how much will you hate me if I ask you to re-send it again?

Oleg, not a problem at all. Which one should I resend?

# desc.
1) added rcu_read_lock/unlock
2) removed tasklist_lock
3) added has_group_leader_pid

Sergey

Quoted 40 lines
Quoted 22 lines
Quoted 22 lines · Oleg Nesterov
Quoted 20 lines
Quoted 4 lines · Sergey Senozhatsky
Quoted 3 lines
Quoted 3 lines

We can remove the tasklist_lock while at it. rcu_read_lock is enough.

Yes, I believe posix-cpu-timers.c shouldn't use tasklist at all, but it is not trivial to change this code.

[..]

I think this change is fine, but please note that thread_group_leader() check is not relaible without tasklist. If we race with de_thread() find_task_by_vpid() can find the new leader before it updates its ->group_leader. IOW, posix_cpu_timer_create() can fail when it shouldn't.

Not that I think this really matters, posix_cpu_timer_create() has other problems with de_thread(). But perhaps it makes sense to change posix_cpu_timer_create() to use has_group_leader_pid() instead, just to make this code not look racy and avoid adding new problems.

Quoted 7 lines
Quoted 7 lines
Quoted 3 lines

The real fix, I think, should change cpu_timer_list to use struct pid* instead of task_struct.

Hello,
Using has_group_leader_pid instead of thread_group_leader, when tasklist_lock is not aquired (check_clock and posix_cpu_timer_create).

This doesn't look like a valid changelog ;) I'd suggest you to write the new one without quoting old emails. It should explain that a) tasklist_lock is not enough for find_vpid() and b) it is not needed.

Also, you forgot to add your signed-of-by.

Otherwise,

#9 lore raw

PATCH 1/1 posix-cpu-timers: rcu_read_lock/unlock protect find_task_by_vpid call

In reply to Oleg Nesterov

Commit 4221a9918e38b7494cee341dda7b7b4bb8c04bde "Add RCU check for find_task_by_vpid()" introduced rcu_lockdep_assert to find_task_by_pid_ns. Add rcu_read_lock/rcu_read_unlock to call find_task_by_vpid.

Tetsuo Handa wrote:

| Quoting from one of posts in that thead
| http://kerneltrap.org/mailarchive/linux-kernel/2010/2/8/4536388
|
|| Usually tasklist gives enough protection, but if copy_process() fails
|| it calls free_pid() lockless and does call_rcu(delayed_put_pid().
|| This means, without rcu lock find_pid_ns() can't scan the hash table
|| safely.

Thomas Gleixner wrote:
| We can remove the tasklist_lock while at it. rcu_read_lock is enough.

Patch also replaces thread_group_leader with has_group_leader_pid in accordance to comment by Oleg Nesterov:

| ... thread_group_leader() check is not relaible without 
| tasklist. If we race with de_thread() find_task_by_vpid() can find
| the new leader before it updates its ->group_leader.
|
| perhaps it makes sense to change posix_cpu_timer_create() to use 
| has_group_leader_pid() instead, just to make this code not look racy
| and avoid adding new problems.

---

kernel/posix-cpu-timers.c +6 -6
index 6842eeb..05bb717 100644
@@ -37,13 +37,13 @@ static int check_clock(const clockid_t which_clock)
 	if (pid == 0)
 		return 0;
 
-	read_lock(&tasklist_lock);
+	rcu_read_lock();
 	p = find_task_by_vpid(pid);
 	if (!p || !(CPUCLOCK_PERTHREAD(which_clock) ?
-		   same_thread_group(p, current) : thread_group_leader(p))) {
+		   same_thread_group(p, current) : has_group_leader_pid(p))) {
 		error = -EINVAL;
 	}
-	read_unlock(&tasklist_lock);
+	rcu_read_unlock();
 
 	return error;
 }
@@ -390,7 +390,7 @@ int posix_cpu_timer_create(struct k_itimer *new_timer)
 
 	INIT_LIST_HEAD(&new_timer->it.cpu.entry);
 
-	read_lock(&tasklist_lock);
+	rcu_read_lock();
 	if (CPUCLOCK_PERTHREAD(new_timer->it_clock)) {
 		if (pid == 0) {
 			p = current;
@@ -404,7 +404,7 @@ int posix_cpu_timer_create(struct k_itimer *new_timer)
 			p = current->group_leader;
 		} else {
 			p = find_task_by_vpid(pid);
-			if (p && !thread_group_leader(p))
+			if (p && !has_group_leader_pid(p))
 				p = NULL;
 		}
 	}
@@ -414,7 +414,7 @@ int posix_cpu_timer_create(struct k_itimer *new_timer)
 	} else {
 		ret = -EINVAL;
 	}
-	read_unlock(&tasklist_lock);
+	rcu_read_unlock();
 
 	return ret;
 }
Reviewed-by on v1 1/1
#10 lore raw
Quoted 29 lines · Sergey Senozhatsky

Commit 4221a9918e38b7494cee341dda7b7b4bb8c04bde "Add RCU check for find_task_by_vpid()" introduced rcu_lockdep_assert to find_task_by_pid_ns. Add rcu_read_lock/rcu_read_unlock to call find_task_by_vpid.

Tetsuo Handa wrote:

| Quoting from one of posts in that thead
| http://kerneltrap.org/mailarchive/linux-kernel/2010/2/8/4536388
|
|| Usually tasklist gives enough protection, but if copy_process() fails
|| it calls free_pid() lockless and does call_rcu(delayed_put_pid().
|| This means, without rcu lock find_pid_ns() can't scan the hash table
|| safely.

Thomas Gleixner wrote:
| We can remove the tasklist_lock while at it. rcu_read_lock is enough.

Patch also replaces thread_group_leader with has_group_leader_pid in accordance to comment by Oleg Nesterov:

| ... thread_group_leader() check is not relaible without
| tasklist. If we race with de_thread() find_task_by_vpid() can find
| the new leader before it updates its ->group_leader.
|
| perhaps it makes sense to change posix_cpu_timer_create() to use
| has_group_leader_pid() instead, just to make this code not look racy
| and avoid adding new problems.
Quoted 1 line
Quoted 49 lines
kernel/posix-cpu-timers.c +6 -6
index 6842eeb..05bb717 100644
@@ -37,13 +37,13 @@ static int check_clock(const clockid_t which_clock)
 	if (pid == 0)
 		return 0;
 
-	read_lock(&tasklist_lock);
+	rcu_read_lock();
 	p = find_task_by_vpid(pid);
 	if (!p || !(CPUCLOCK_PERTHREAD(which_clock) ?
-		   same_thread_group(p, current) : thread_group_leader(p))) {
+		   same_thread_group(p, current) : has_group_leader_pid(p))) {
 		error = -EINVAL;
 	}
-	read_unlock(&tasklist_lock);
+	rcu_read_unlock();
 
 	return error;
 }
@@ -390,7 +390,7 @@ int posix_cpu_timer_create(struct k_itimer *new_timer)
 
 	INIT_LIST_HEAD(&new_timer->it.cpu.entry);
 
-	read_lock(&tasklist_lock);
+	rcu_read_lock();
 	if (CPUCLOCK_PERTHREAD(new_timer->it_clock)) {
 		if (pid == 0) {
 			p = current;
@@ -404,7 +404,7 @@ int posix_cpu_timer_create(struct k_itimer *new_timer)
 			p = current->group_leader;
 		} else {
 			p = find_task_by_vpid(pid);
-			if (p && !thread_group_leader(p))
+			if (p && !has_group_leader_pid(p))
 				p = NULL;
 		}
 	}
@@ -414,7 +414,7 @@ int posix_cpu_timer_create(struct k_itimer *new_timer)
 	} else {
 		ret = -EINVAL;
 	}
-	read_unlock(&tasklist_lock);
+	rcu_read_unlock();
 
 	return ret;
 }
#11 lore raw

PATCH 1/1 posix-cpu-timers: workaround to suppress the problems with mt exec

In reply to Oleg Nesterov

posix-cpu-timers.c correctly assumes that the dying process does posix_cpu_timers_exit_group() and removes all !CPUCLOCK_PERTHREAD timers from signal->cpu_timers list.

But, it also assumes that timer->it.cpu.task is always the group leader, and thus the dead ->task means the dead thread group.

This is obviously not true after de_thread() changes the leader. After that almost every posix_cpu_timer_ method has problems.

It is not simple to fix this bug correctly. First of all, I think that timer->it.cpu should use struct pid instead of task_struct. Also, the locking should be reworked completely. In particular, tasklist_lock should not be used at all. This all needs a lot of nontrivial and hard-to-test changes.

Change __exit_signal() to do posix_cpu_timers_exit_group() when the old leader dies during exec. This is not the fix, just the temporary hack to hide the problem for 2.6.37 and stable. IOW, this is obviously wrong but this is what we currently have anyway:
cpu timers do not work after mt exec.

In theory this change adds another race. The exiting leader can detach the timers which were attached to the new leader. However, the window between de_thread() and release_task() is small, we can pretend that sys_timer_create() was called before de_thread().

---

 kernel/exit.c |    8 ++++++++
 1 file changed, 8 insertions(+)
kernel/exit.c renamed from kernel/exit.c~pct_de_thread_race +8 -0
@@ -95,6 +95,14 @@ static void __exit_signal(struct task_st
 		sig->tty = NULL;
 	} else {
 		/*
+		 * This can only happen if the caller is de_thread().
+		 * FIXME: this is the temporary hack, we should teach
+		 * posix-cpu-timers to handle this case correctly.
+		 */
+		if (unlikely(has_group_leader_pid(tsk)))
+			posix_cpu_timers_exit_group(tsk);
+
+		/*
 		 * If there is any task waiting for the group exit
 		 * then notify it:
 		 */
#12 lore raw

Re: [PATCH] posix-cpu-timers: workaround to suppress the problems with mt exec

That seems like a reasonable workaround off hand. But since all the reworkings I am no longer particularly authoritative on this code.

Thanks,
Roland

Reviewed-by on v1 1/1
#13 lore raw

Re: [PATCH] posix-cpu-timers: workaround to suppress the problems with mt exec

In reply to Oleg Nesterov
Quoted 22 lines · Oleg Nesterov

posix-cpu-timers.c correctly assumes that the dying process does posix_cpu_timers_exit_group() and removes all !CPUCLOCK_PERTHREAD timers from signal->cpu_timers list.

But, it also assumes that timer->it.cpu.task is always the group leader, and thus the dead ->task means the dead thread group.

This is obviously not true after de_thread() changes the leader. After that almost every posix_cpu_timer_ method has problems.

It is not simple to fix this bug correctly. First of all, I think that timer->it.cpu should use struct pid instead of task_struct. Also, the locking should be reworked completely. In particular, tasklist_lock should not be used at all. This all needs a lot of nontrivial and hard-to-test changes.

Change __exit_signal() to do posix_cpu_timers_exit_group() when the old leader dies during exec. This is not the fix, just the temporary hack to hide the problem for 2.6.37 and stable. IOW, this is obviously wrong but this is what we currently have anyway:
cpu timers do not work after mt exec.

Quoted 6 lines

In theory this change adds another race. The exiting leader can detach the timers which were attached to the new leader. However, the window between de_thread() and release_task() is small, we can pretend that sys_timer_create() was called before de_thread().