提交 a872ff0c 编写于 作者: O Oleg Nesterov 提交者: Linus Torvalds

[PATCH] simplify/fix first_tid()

first_tid:

	/* If nr exceeds the number of threads there is nothing todo */
	if (nr) {
		if (nr >= get_nr_threads(leader))
			goto done;
	}

This is not reliable: sub-threads can exit after this check, so the
'for' loop below can overlap and proc_task_readdir() can return an
already filldir'ed dirents.

	for (; pos && pid_alive(pos); pos = next_thread(pos)) {
		if (--nr > 0)
			continue;

Off-by-one error, will return 'leader' when nr == 1.

This patch tries to fix these problems and simplify the code.
Signed-off-by: NOleg Nesterov <oleg@tv-sign.ru>
Cc: "Eric W. Biederman" <ebiederm@xmission.com>
Signed-off-by: NAndrew Morton <akpm@osdl.org>
Signed-off-by: NLinus Torvalds <torvalds@osdl.org>
上级 cc288738
...@@ -2227,38 +2227,34 @@ int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir) ...@@ -2227,38 +2227,34 @@ int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
static struct task_struct *first_tid(struct task_struct *leader, static struct task_struct *first_tid(struct task_struct *leader,
int tid, int nr) int tid, int nr)
{ {
struct task_struct *pos = NULL; struct task_struct *pos;
rcu_read_lock(); rcu_read_lock();
/* Attempt to start with the pid of a thread */ /* Attempt to start with the pid of a thread */
if (tid && (nr > 0)) { if (tid && (nr > 0)) {
pos = find_task_by_pid(tid); pos = find_task_by_pid(tid);
if (pos && (pos->group_leader != leader)) if (pos && (pos->group_leader == leader))
pos = NULL; goto found;
if (pos)
nr = 0;
} }
/* If nr exceeds the number of threads there is nothing todo */ /* If nr exceeds the number of threads there is nothing todo */
if (nr) { pos = NULL;
if (nr >= get_nr_threads(leader)) if (nr && nr >= get_nr_threads(leader))
goto done; goto out;
}
/* If we haven't found our starting place yet start with the /* If we haven't found our starting place yet start
* leader and walk nr threads forward. * with the leader and walk nr threads forward.
*/ */
if (!pos && (nr >= 0)) for (pos = leader; nr > 0; --nr) {
pos = leader; pos = next_thread(pos);
if (pos == leader) {
for (; pos && pid_alive(pos); pos = next_thread(pos)) { pos = NULL;
if (--nr > 0) goto out;
continue; }
get_task_struct(pos);
goto done;
} }
pos = NULL; found:
done: get_task_struct(pos);
out:
rcu_read_unlock(); rcu_read_unlock();
return pos; return pos;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册