will_become_orphaned_pgrp: we have threads
Subsystems (6)
PATCH 1/3 will_become_orphaned_pgrp: we have threads
p->exit_state != 0 doesn't mean this process is dead, it may have sub-threads.
However, the new "p->exit_state && thread_group_empty(p)" check is not correct either, this is just the temporary hack. Perhaps we can just remove this check, but I don't understand orphaned process groups magic. At all. However, I think exit_notify() is obviously and completely wrong wrt this helper.
kernel/exit.c renamed from kernel/exit.c~4_orphaned_pgrp +3 -3
@@ -219,9 +219,9 @@ static int will_become_orphaned_pgrp(strint ret =1 ; do_each_pid_task(pgrp, PIDTYPE_PGID, p) { -if (p == ignored_task - || p->exit_state - ||is_global_init (p->real_parent)) +if ((p == ignored_task) || + (p->exit_state &&thread_group_empty (p)) || +is_global_init (p->real_parent))continue ;if (task_pgrp (p->real_parent) != pgrp &&task_session (p->real_parent) ==task_session (p)) {
Quoted 6 lines · Oleg Nesterov
p->exit_state!= 0 doesn't mean this process is dead, it may have sub-threads.However, the new "
p->exit_state&&thread_group_empty(p)" check is not correct either, this is just the temporary hack. Perhaps we can just remove this check, but I don't understand orphaned process groups magic. At all. However, I thinkexit_notify()is obviously and completely wrong wrt this helper.
The problem that orphaned processes groups address is what happens if an entire process group is stopped, and there is not a process that can wake them up.
The rule for an unprivileged process sending a signal to a process group is that it must be in the same session as the process group.
The rule for sending a signal to a process group is that the signal sender must be in the same session.
So we are testing for a process group that does not have a living member with a parent outside of the process that can send the process group a signal.
The test for init seems bogus. /sbin/init rarely if ever starts processes in it's own session which is likely why this has not caused problems. If we keep the test for init we need to make the test is_container_init rather the is global_init.
As for exit_notify I agree. We need a thread_group_exit_notify. That is responsible for performing work when we know the entire thread group has exited. Sending the exit_signal and performing the through group orphaned check look like two of those tasks that need to be performed only at thread group exit.
Oleg what do you see wrong with checking p->exit_state &&thread_group_empty(p)? Since non-leader threads all self reap
that seems to be a valid test for an dead thread group.
Eric
Quoted 17 lines
Signed-off-by Oleg Nesterov <[email protected]>kernel/exit.c renamed from kernel/exit.c~4_orphaned_pgrp +3
-3@@ -219,9 +219,9 @@ static int will_become_orphaned_pgrp(strint ret =1 ; do_each_pid_task(pgrp, PIDTYPE_PGID, p) { -if (p == ignored_task - || p->exit_state - ||is_global_init (p->real_parent)) +if ((p == ignored_task) || + (p->exit_state &&thread_group_empty (p)) || +is_global_init (p->real_parent))continue ;if (task_pgrp (p->real_parent) != pgrp &&task_session (p->real_parent) ==task_session (p)) {
Quoted 17 lines · Eric W. Biederman
Quoted 6 lines · Oleg Nesterov
p->exit_state!= 0 doesn't mean this process is dead, it may have sub-threads.However, the new "
p->exit_state&&thread_group_empty(p)" check is not correct either, this is just the temporary hack. Perhaps we can just remove this check, but I don't understand orphaned process groups magic. At all. However, I thinkexit_notify()is obviously and completely wrong wrt this helper.The problem that orphaned processes groups address is what happens if an entire process group is stopped, and there is not a process that can wake them up.
The rule for an unprivileged process sending a signal to a process group is that it must be in the same session as the process group.
Quoted 6 lines
The rule for sending a signal to a process group is that the signal sender must be in the same session.
So we are testing for a process group that does not have a living member with a parent outside of the process that can send the process group a signal.
Ah, thanks a lot Eric, I am _starting_ to understand this.
Quoted 3 lines
Oleg what do you see wrong with checking
p->exit_state&&thread_group_empty(p)? Since non-leader threads all self reap
that seems to be a valid test for an dead thread group.
There is a window when exit_notify() drops tasklist and before release_task(). Suppose the last (non-leader) thread exits. This means that entire group exits, but thread_group_empty() is not true.
Oleg.
Quoted 31 lines · Oleg Nesterov
Quoted 4 lines · Eric W. Biederman
Quoted 1 line · Oleg Nesterov
p->exit_state!= 0 doesn't mean this process is dead, it may havesub-threads.
Quoted 2 lines
Quoted 2 lines
However, the new "
p->exit_state&&thread_group_empty(p)" check is notcorrect
Quoted 1 line
Quoted 1 line
either, this is just the temporary hack. Perhaps we can just remove this
check,
Quoted 1 line
Quoted 1 line
but I don't understand orphaned process groups magic. At all. However, I
think
Quoted 9 lines
Quoted 1 line
exit_notify()is obviously and completely wrong wrt this helper.The problem that orphaned processes groups address is what happens if an entire process group is stopped, and there is not a process that can wake them up.
The rule for an unprivileged process sending a signal to a process group is that it must be in the same session as the process group.
Quoted 6 lines
The rule for sending a signal to a process group is that the signal sender must be in the same session.
So we are testing for a process group that does not have a living member with a parent outside of the process that can send the process group a signal.
Ah, thanks a lot Eric, I am _starting_ to understand this.
Quoted 7 lines
Quoted 3 lines
Oleg what do you see wrong with checking
p->exit_state&&thread_group_empty(p)? Since non-leader threads all self reap
that seems to be a valid test for an dead thread group.There is a window when
exit_notify()drops tasklist and beforerelease_task(). Suppose the last (non-leader) thread exits. This means that entire group exits, butthread_group_empty()is not true.
And if you are an observer this is important.
Equally messed up is a our status in /proc at that point. Which says our sleeping process is a zombie.
I'm thinking we need to do at least some of the thread group leadership transfer in do_exit, instead of de_thread. Then p->group_leader->exit_state would be sufficient to see if the entire thread group was alive, as the group_leader would be whoever was left alive. The original group_leader might still need to be kept around for it's pid...
I think that would solve most of the problems you have with a dead thread group leader and sending SIG_STOP as well.
Eric
Quoted 3 lines · Eric W. Biederman
Equally messed up is a our status in /proc at that point. Which says our sleeping process is a zombie.
Yes, this is annoying.
Quoted 8 lines
I'm thinking we need to do at least some of the thread group leadership transfer in
do_exit, instead ofde_thread. Thenp->group_leader->exit_statewould be sufficient to see if the entire thread group was alive, as thegroup_leaderwould be whoever was left alive. The originalgroup_leadermight still need to be kept around for it's pid...I think that would solve most of the problems you have with a dead thread group leader and sending
SIG_STOPas well.
Yes I was thinking about that too, but I am not brave enough to even try to to think to the end ;)
As a minimal change, I tried to add "task_struct *leader_proxy" to signal_struct, which points to the next live thread, and changed by exit_notify(). eligible_child() checks it instead of ->exit_signal. But this is so messy...
And in fact, if we are talking about group stop, it is a group operation, why do_wait() uses per-thread ->exit_code but not ->group_exit_code ?
But yes, [PATCH 3/3] adds a visible difference, and I don't know if this difference is good or bad.
$ sleep 1000
[1]+ Stopped sleep 1000
$ strace -p pidof sleep
Process 432 attached - interrupt to quit
Now strace "hangs" in do_wait() because ->exit_code was eaten by the shell. We need SIGCONT.
With the "[PATCH 3/3]" strace proceeds happily.
Oleg.
Quoted 19 lines · Oleg Nesterov
Quoted 3 lines · Eric W. Biederman
Equally messed up is a our status in /proc at that point. Which says our sleeping process is a zombie.
Yes, this is annoying.
Quoted 8 lines
I'm thinking we need to do at least some of the thread group leadership transfer in
do_exit, instead ofde_thread. Thenp->group_leader->exit_statewould be sufficient to see if the entire thread group was alive, as thegroup_leaderwould be whoever was left alive. The originalgroup_leadermight still need to be kept around for it's pid...I think that would solve most of the problems you have with a dead thread group leader and sending
SIG_STOPas well.Yes I was thinking about that too, but I am not brave enough to even try to to think to the end ;)
Quoted 7 lines
As a minimal change, I tried to add "
task_struct*leader_proxy" tosignal_struct, which points to the next live thread, and changed byexit_notify().eligible_child()checks it instead of ->exit_signal. But this is so messy...And in fact, if we are talking about group stop, it is a group operation, why
do_wait()uses per-thread ->exit_code but not ->group_exit_code ?
Good question, we would need a fallback for the case it isn't a group operation like in exit but that might clean something up.
Quoted 9 lines
But yes, [PATCH 3/3] adds a visible difference, and I don't know if this difference is good or bad.
$ sleep 1000[1]+ Stopped sleep 1000
$ strace -ppidof sleep
Process 432 attached - interrupt to quit
Quoted 6 lines
Now strace "hangs" in
do_wait()because ->exit_code was eaten by the shell. We need SIGCONT.With the "[PATCH 3/3]" strace proceeds happily.
Oleg.
Well I got to playing with the idea of actually moving group_leader and it turns out that while it is a pain it isn't actually that bad. The worst part is not really changing the pid of the leader to the pid of the entire thread group. As there are a few cases where we are current referencing the task_pid where we really want task_tgid.
Oleg below is my proof of concept patch, which really needs to be broken up into a whole patch series, so the changes are small enough we can do a thorough audit on them. Anyway take a look and see what you think.
This patch does fix your weird test case without any actual change to the do_wait logic itself.
The key idea is that by not making PIDTYPE_PID a hash chain we can point two different struct pids at the same process allowing two different pids to return the same process from pid_task(pid, PIDTYPE_PID);
Which means most things continue to just work by working on PIDTYPE_PID, although as mentioned previously there are a few things particulary do_notify_parent_cldstop and do_wait that need to be changed to return the tgid instead of the pid.
Oh and in eligible child the PIDTYPE_PID test is now sneaky essentially doing a task lookup and seeing if the result is our target pid, instead of comparing pids.
The funny part is grep pid /proc/<tgid>/status no longer always equals the tgid after the pid exits. Still that seems better then making the entire thread group look like a zombie just because the wrong thread exited.
Subject: [PATCH] All thread group leaders to exit
--- fs/exec.c | 81 ++--------------------- fs/fcntl.c | 20 ++++-- fs/proc/base.c | 6 +- include/linux/init_task.h | 25 ++++---- include/linux/pid.h | 14 ++--- include/linux/sched.h | 43 ++++++------ kernel/exit.c | 157 +++++++++++++++++++++++++++----------------- kernel/fork.c | 2 +- kernel/itimer.c | 2 +- kernel/pid.c | 60 +++++++++++------ kernel/signal.c | 23 ++----- 11 files changed, 204 insertions(+), 229 deletions(-)
fs/exec.c +6 -75
@@ -786,22 +786,6 @@ static int de_thread(struct task_struct *tsk)* Account for the thread group leader hanging around: */ count =1 ; -if (!thread_group_leader (tsk)) { - count =2 ; -/* -* The SIGALRM timer survives the exec, but needs to point -* at us as the new group leader now. We have a race with -* a timer firing now getting the old leader, so we need to -* synchronize with any firing (by calling del_timer_sync) -* before we can safely let the old group leader die. -*/ - sig->tsk = tsk; -spin_unlock_irq (lock); -if (hrtimer_cancel (&sig->real_timer)) -hrtimer_restart (&sig->real_timer); -spin_lock_irq (lock); - } - sig->notify_count = count;while (atomic_read (&sig->count) > count) {__set_current_state (TASK_UNINTERRUPTIBLE );
@@ -811,68 +795,15 @@ static int de_thread(struct task_struct *tsk) }spin_unlock_irq (lock); -/* -* At this point all other threads have exited, all we have to -* do is to wait for the thread group leader to become inactive, -* and to assume its PID: -*/ -if (!thread_group_leader (tsk)) { - leader = tsk->group_leader; - - sig->notify_count =-1 ; -for (;;) { -write_lock_irq (&tasklist_lock); -if (likely (leader->exit_state)) -break ; -__set_current_state (TASK_UNINTERRUPTIBLE ); -write_unlock_irq (&tasklist_lock); -schedule (); +/* If it isn't already force gettid() == getpid() */ +if (sig->tgid != tsk->tid) { +write_lock_irq (&tasklist_lock); +if (sig->tgid != tsk->tid) { +detach_pid (tsk,PIDTYPE_PID ); +attach_pid (tsk,PIDTYPE_PID , sig->tgid); } - -/* -* The only record we have of the real-time age of a -* process, regardless of execs it's done, is start_time. -* All the past CPU time is accumulated in signal_struct -* from sister threads now dead. But in this non-leader -* exec, nothing survives from the original leader thread, -* whose birth marks the true age of this process now. -* When we take on its identity by switching to its PID, we -* also take its birthdate (always earlier than our own). -*/ - tsk->start_time = leader->start_time; - -BUG_ON (!same_thread_group (leader, tsk)); -BUG_ON (has_group_leader_pid (tsk)); -/* -* An exec() starts a new thread group with the -* TGID of the previous thread group. Rehash the -* two threads with a switched PID, and release -* the former thread group leader: -*/ - -/* Become a process group leader with the old leader's pid. -* The old leader becomes a thread of the this thread group. -* Note: The old leader also uses this pid until release_task -* is called. Odd but simple and correct. -*/ -detach_pid (tsk,PIDTYPE_PID ); - tsk->pid = leader->pid; -attach_pid (tsk,PIDTYPE_PID ,task_pid (leader)); -transfer_pid (leader, tsk,PIDTYPE_PGID ); -transfer_pid (leader, tsk,PIDTYPE_SID ); -list_replace_rcu (&leader->tasks, &tsk->tasks); - - tsk->group_leader = tsk; - leader->group_leader = tsk; - - tsk->exit_signal =SIGCHLD ; - -BUG_ON (leader->exit_state !=EXIT_ZOMBIE ); - leader->exit_state =EXIT_DEAD ; -write_unlock_irq (&tasklist_lock); } - sig->group_exit_task =NULL ; sig->notify_count =0 ;
fs/fcntl.c +14 -6
@@ -516,9 +516,13 @@ void send_sigio(struct fown_struct *fown, int fd, int band) goto out_unlock_fown;read_lock (&tasklist_lock); - do_each_pid_task(pid, type, p) { -send_sigio_to_task (p, fown, fd, band); - }while_each_pid_task (pid, type, p); +if (type ==PIDTYPE_PID ) +send_sigio_to_task (pid_task (pid, type), fown, fd, band); +else { + do_each_pid_task(pid, type, p) { +send_sigio_to_task (p, fown, fd, band); + }while_each_pid_task (pid, type, p); + }read_unlock (&tasklist_lock); out_unlock_fown:read_unlock (&fown->lock);
@@ -547,9 +551,13 @@ int send_sigurg(struct fown_struct *fown) ret =1 ;read_lock (&tasklist_lock); - do_each_pid_task(pid, type, p) { -send_sigurg_to_task (p, fown); - }while_each_pid_task (pid, type, p); +if (type ==PIDTYPE_PID ) +send_sigurg_to_task (pid_task (pid, type), fown); +else { + do_each_pid_task(pid, type, p) { +send_sigurg_to_task (p, fown); + }while_each_pid_task (pid, type, p); + }read_unlock (&tasklist_lock); out_unlock_fown:read_unlock (&fown->lock);
fs/proc/base.c +3 -3
@@ -2438,15 +2438,15 @@ retry:* pid of a thread_group_leader. Testing for task * being a thread_group_leader is the obvious thing * todo but there is a window when it fails, due to -* the pid transfer logic in de_thread. +* the pid transfer logic at group leader death. * * So we perform the straight forward test of seeing -* if the pid we have found is the pid of a thread +* if the pid we have found is the pid of the thread * group leader, and don't worry if the task we have * found doesn't happen to be a thread group leader. * As we don't care in the case of readdir. */ -if (!iter.task || !has_group_leader_pid (iter.task)) { +if (!iter.task || pid !=task_tgid (iter.task)) { iter.tgid +=1 ; goto retry; }
include/linux/init_task.h +12 -13
@@ -67,6 +67,9 @@ .posix_timers = LIST_HEAD_INIT(sig.posix_timers), \ .cpu_timers = INIT_CPU_TIMERS(sig.cpu_timers), \ .rlim = INIT_RLIMITS, \ + .tgid = &init_struct_pid, \ + .pids[PIDTYPE_PGID] = &init_struct_pid, \ + .pids[PIDTYPE_SID] = &init_struct_pid, \ }extern struct nsproxy init_nsproxy;
@@ -91,10 +94,10 @@ extern struct group_info init_groups;#define INIT_STRUCT_PID { \ .count = ATOMIC_INIT(1), \ + .tsk = &init_task, \ .tasks = { \ - { .first = &init_task.pids[PIDTYPE_PID].node }, \ - { .first = &init_task.pids[PIDTYPE_PGID].node }, \ - { .first = &init_task.pids[PIDTYPE_SID].node }, \ + { .first = &init_task.pids[PIDTYPE_PGID] }, \ + { .first = &init_task.pids[PIDTYPE_SID] }, \ }, \ .rcu = RCU_HEAD_INIT, \ .level = 0, \
@@ -105,13 +108,10 @@ extern struct group_info init_groups; }, } \ } -#define INIT_PID_LINK (type) \ -{ \ - .node = { \ - .next = NULL, \ - .pprev = &init_struct_pid.tasks[type].first, \ - }, \ - .pid = &init_struct_pid, \ +#define INIT_PID_HLIST_NODE (type) \ +{ \ + .next = NULL, \ + .pprev = &init_struct_pid.tasks[type].first, \ }#ifdef CONFIG_SECURITY_FILE_CAPABILITIES
@@ -179,9 +179,8 @@ extern struct group_info init_groups; .fs_excl = ATOMIC_INIT(0), \ .pi_lock = __SPIN_LOCK_UNLOCKED(tsk.pi_lock), \ .pids = { \ - [PIDTYPE_PID] = INIT_PID_LINK(PIDTYPE_PID), \ - [PIDTYPE_PGID] = INIT_PID_LINK(PIDTYPE_PGID), \ - [PIDTYPE_SID] = INIT_PID_LINK(PIDTYPE_SID), \ + [PIDTYPE_PGID] = INIT_PID_HLIST_NODE(PIDTYPE_PGID), \ + [PIDTYPE_SID] = INIT_PID_HLIST_NODE(PIDTYPE_SID), \ }, \ .dirties =INIT_PROP_LOCAL_SINGLE (dirties), \INIT_TRACE_IRQFLAGS \
include/linux/pid.h +5 -9
@@ -5,9 +5,10 @@enum pid_type { -PIDTYPE_PID ,PIDTYPE_PGID ,PIDTYPE_SID , +PIDTYPE_PID , +#define PIDTYPE_ARRAY_MAX PIDTYPE_PID PIDTYPE_MAX };
@@ -58,7 +59,8 @@ struct pid {atomic_t count;/* lists of tasks that use this pid */ -struct hlist_head tasks[PIDTYPE_MAX ]; +struct task_struct *tsk; +struct hlist_head tasks[PIDTYPE_ARRAY_MAX ];struct rcu_head rcu;int level;struct upid numbers[1 ];
@@ -66,12 +68,6 @@ struct pidextern struct pid init_struct_pid; -struct pid_link -{ -struct hlist_node node; -struct pid *pid; -}; -static inline struct pid *get_pid (struct pid *pid) {if (pid)
@@ -158,7 +154,7 @@ static inline pid_t pid_vnr(struct pid *pid)struct hlist_node *pos___; \if (pid !=NULL ) \hlist_for_each_entry_rcu ((task), pos___, \ - &pid->tasks[type], pids[type].node) { + &pid->tasks[type], pids[type]) {#define while_each_pid_task (pid, type, task) \ } \
include/linux/sched.h +21 -22
104 lines not on this page · raw message
kernel/exit.c +96 -61
303 lines not on this page · raw message
kernel/fork.c +1 -1
@@ -883,7 +883,6 @@ static int copy_signal(unsigned long clone_flags, struct task_struct *tsk)hrtimer_init (&sig->real_timer,CLOCK_MONOTONIC ,HRTIMER_MODE_REL ); sig->it_real_incr.tv64 =0 ; sig->real_timer.function = it_real_fn; - sig->tsk = tsk; sig->it_virt_expires = cputime_zero; sig->it_virt_incr = cputime_zero;
@@ -1308,6 +1307,7 @@ static struct task_struct *copy_process(unsigned long clone_flags,if (clone_flags &CLONE_NEWPID ) p->nsproxy->pid_ns->child_reaper = p; + p->signal->tgid = pid; p->signal->tty = current->signal->tty;set_task_pgrp (p,task_pgrp_nr (current));set_task_session (p,task_session_nr (current));
kernel/itimer.c +1 -1
@@ -132,7 +132,7 @@ enum hrtimer_restart it_real_fn(struct hrtimer *timer)struct signal_struct *sig = container_of(timer, struct signal_struct, real_timer); -send_group_sig_info (SIGALRM ,SEND_SIG_PRIV , sig->tsk); +kill_pid_info (SIGALRM ,SEND_SIG_PRIV , sig->tgid);return HRTIMER_NORESTART ; }
kernel/pid.c +39 -21
93 lines not on this page · raw message
kernel/signal.c +6 -17
@@ -1195,20 +1195,6 @@ send_sig(int sig, struct task_struct *p, int priv)return send_sig_info (sig,__si_special (priv), p); } -/* -* This is the entry point for "process-wide" signals. -* They will go to an appropriate thread in the thread group. -*/ -int -send_group_sig_info (int sig,struct siginfo *info,struct task_struct *p) -{ -int ret; -read_lock (&tasklist_lock); - ret =group_send_sig_info (sig, info, p); -read_unlock (&tasklist_lock); -return ret; -} -void force_sig (int sig,struct task_struct *p) {
@@ -1501,12 +1487,15 @@ static void do_notify_parent_cldstop(struct task_struct *tsk, int why)unsigned long flags;struct task_struct *parent;struct sighand_struct *sighand; +struct pid *pid; -if (tsk->ptrace &PT_PTRACED ) +if (tsk->ptrace &PT_PTRACED ) { parent = tsk->parent; -else { + pid =task_pid (tsk); + }else { tsk = tsk->group_leader; parent = tsk->real_parent; + pid =task_tgid (tsk); } info.si_signo =SIGCHLD ;
@@ -1515,7 +1504,7 @@ static void do_notify_parent_cldstop(struct task_struct *tsk, int why)* see comment in do_notify_parent() abot the following 3 lines */ rcu_read_lock (); - info.si_pid =task_pid_nr_ns (tsk, tsk->parent->nsproxy->pid_ns); + info.si_pid =pid_nr_ns (pid, parent->nsproxy->pid_ns);rcu_read_unlock (); info.si_uid = tsk->uid;
Signature · 1 line
1.5.3.rc6.17.g1911
Quoted 5 lines · Eric W. Biederman
Oleg below is my proof of concept patch, which really needs to be broken up into a whole patch series, so the changes are small enough we can do a thorough audit on them. Anyway take a look and see what you think.
Amazing ;)
This patch certainly needs a time for understanding, so far I have read only the small subset, a couple of random questions.
Quoted 47 lines
include/linux/pid.h +4
-2@@ -5,9 +5,10 @@enum pid_type { -PIDTYPE_PID ,PIDTYPE_PGID ,PIDTYPE_SID , +PIDTYPE_PID , +#define PIDTYPE_ARRAY_MAX PIDTYPE_PID PIDTYPE_MAX };@@ -58,7 +59,8 @@ struct pid {atomic_t count;/* lists of tasks that use this pid */ -struct hlist_head tasks[PIDTYPE_MAX ]; +struct task_struct *tsk; +struct hlist_head tasks[PIDTYPE_ARRAY_MAX ];struct rcu_head rcu;int level;struct upid numbers[1 ];include/linux/sched.h +2
-1@@ -453,7 +453,6 @@ struct signal_struct {/* ITIMER_REAL timer for the process */ struct hrtimer real_timer; -struct task_struct *tsk;ktime_t it_real_incr;/* ITIMER_PROF and ITIMER_VIRTUAL timers for the process */ @@ -461,6 +460,8 @@ struct signal_struct {cputime_t it_prof_incr, it_virt_incr;/* job control IDs */ +struct pid *tgid; +struct pid *pids[PIDTYPE_ARRAY_MAX ];/* * pgrp and session fields are deprecated.
include/linux/sched.h struct task_struct { · line 1040
Quoted 7 lines
include/linux/sched.h +2
-1@@ -1034,8 +1035,9 @@ struct task_struct {struct list_head sibling;/* linkage in my parent's children list */ struct task_struct *group_leader;/* threadgroup leader */ +struct pid *tid;/* PID/PID hash table linkage. */ -struct pid_link pids[PIDTYPE_MAX ]; +struct hlist_node pids[PIDTYPE_ARRAY_MAX ];
OK. It certainly makes sense to move PIDTYPE_PGID/SID pids from task_struct to signal struct.
But can't we go a bit further? With this patch pid->tasks[].first still "points" to leader's task_struct. Suppose we replace pid->tasks[] with pid->signals[], so that pid->signals[].first points to signal_struct. Then we can find the task (group_leader) via signal->tgid.
This means we can remove task_struct->pids, and kill transfer_pid().
include/linux/sched.h static inline void set_task_pgrp(struct task_struct *tsk, pid_t pgrp) · line 1276
Quoted 9 lines
include/linux/sched.h +5
-1static inline struct pid *task_tgid (struct task_struct *task) { -return task->group_leader->pids[PIDTYPE_PID ].pid; +struct signal_struct *sig =rcu_dereference (task->signal); +struct pid *pid =NULL ; +if (sig) + pid = sig->tgid; +return pid; }
Hmm. This is fixable, but note that task->signal is not RCU protected, only ->sighand.
include/linux/sched.h static inline pid_t task_ppid_nr_ns(struct task_struct *tsk, · line 1389
Quoted 5 lines
static inline int
pid_alive(struct task_struct *p){include/linux/sched.h +1
-1-return p->pids[PIDTYPE_PID ].pid !=NULL ; +return p->signal !=NULL ; }
(this change btw is imho good regardless, because pid_alive() currently
means "the task is not unhashed yet" anyway).
include/linux/sched.h static void __unhash_process(struct task_struct *p) · line 67
Quoted 12 lines
static void
__unhash_process(struct task_struct *p){include/linux/sched.h +1
-1nr_threads--; -detach_pid (p,PIDTYPE_PID );if (thread_group_leader (p)) {detach_pid (p,PIDTYPE_PGID );detach_pid (p,PIDTYPE_SID );@@ -65,6 +64,7 @@ static void __unhash_process(struct task_struct *p)list_del_rcu (&p->tasks);__get_cpu_var (process_counts)--; } +detach_pid (p,PIDTYPE_PID );
Not sure why this change is needed... To prevent the premature detach_pid()->free_pid() ? But this doesn't looks possible, if the task is leader, p->tid->tsk == p, and detach_pid() does
if (pid->tsk) // still used, don't free. return;
include/linux/sched.h fastcall NORET_TYPE void do_exit(long code) · line 924
Quoted 6 lines
include/linux/sched.h +2
-0@@ -946,6 +920,48 @@ fastcall NORET_TYPE void do_exit(long code) } tsk->flags |=PF_EXITING ; +/* Transfer thread group leadership */ +if (thread_group_leader (tsk) && !thread_group_empty (tsk)) {
Ah, this is racy without tasklist_lock. Suppose that the current ->group_leader exits right now and elects us as a new leader.
include/linux/sched.h fastcall NORET_TYPE void do_exit(long code) · line 935
Quoted 11 lines
include/linux/sched.h +11
-0+struct task_struct *new_leader, *t; +write_lock_irq (&tasklist_lock); +for (t =next_thread (tsk); t != tsk; t =next_thread (t)) { +if (!(t->flags &PF_EXITING )) +break ; + } +if (t != tsk) { + new_leader = t; + + new_leader->start_time = tsk->start_time; +task_pid (tsk)->tsk = new_leader;
So this pid won't be freed when current does detach_pid(PIDTYPE_PID), from now current->tid->tsk != current, so detach_pid() doesn't clear pid->tsk.
But when it will be freed then?
include/linux/sched.h fastcall NORET_TYPE void do_exit(long code) · line 942
Quoted 7 lines
include/linux/sched.h +7
-0+transfer_pid (tsk, new_leader,PIDTYPE_PGID ); +transfer_pid (tsk, new_leader,PIDTYPE_SID ); +list_replace_rcu (&tsk->tasks, &new_leader->tasks); + +/* Update group_leader on all of the threads... */ + new_leader->group_leader = new_leader; + tsk->group_leader = new_leader;
This is one of the most questionable changes. Now current can't "trust" its ->group_leader without tasklist.
As a random example, let's look at sys_setpgid(). When we take tasklist_lock group_leader can point to the already freed/reused memory.
include/linux/sched.h fastcall NORET_TYPE void do_exit(long code) · line 953
Quoted 11 lines
include/linux/sched.h +11
-0+ }else { +write_unlock_irq (&tasklist_lock); +/* Wait for the other threads to exit before continuing */ +for (;;) { +read_lock (&tasklist_lock); +if (thread_group_empty (tsk)) +break ; +__set_current_state (TASK_UNINTERRUPTIBLE ); +read_unlock (&tasklist_lock); +schedule (); + }
I don't get this... Who will wake us?
Hmm, deadlock with coredump? The coredumping thread may be waiting for us, but we didn't do exit_mm() yet.
include/linux/sched.h static int wait_task_continued(struct task_struct *p, int noreap, · line 1466
Quoted 10 lines
include/linux/sched.h +4
-2@@ -1440,8 +1460,11 @@ static int wait_task_continued(struct task_struct *p, int noreap,if (!noreap) p->signal->flags &= ~SIGNAL_STOP_CONTINUED ;spin_unlock_irq (&p->sighand->siglock); - - pid =task_pid_nr_ns (p, current->nsproxy->pid_ns); +if (p->ptrace &&same_thread_group (current, p->parent)) + pid =task_pid (p); +else + pid =task_tgid (p);
Why do we need "&& same_thread_group()" above? Ptracer needs per-thread pid in any case.
Quoted 5 lines
include/linux/sched.h +1
-0@@ -1501,12 +1487,15 @@ static void do_notify_parent_cldstop(struct task_struct *tsk, int why)unsigned long flags;struct task_struct *parent;struct sighand_struct *sighand; +struct pid *pid;
include/linux/sched.h static void do_notify_parent_cldstop(struct task_struct *tsk, int why) · line 1498
Quoted 10 lines
include/linux/sched.h +4
-2-if (tsk->ptrace &PT_PTRACED ) +if (tsk->ptrace &PT_PTRACED ) { parent = tsk->parent; -else { + pid =task_pid (tsk); + }else { tsk = tsk->group_leader; parent = tsk->real_parent; + pid =task_tgid (tsk);
Hmm, yes... With this patch task_pid(p->leader) != task_tgid(p).
Will continue tomorrow.
Oleg.
Quoted 11 lines · Oleg Nesterov
Quoted 5 lines · Eric W. Biederman
Oleg below is my proof of concept patch, which really needs to be broken up into a whole patch series, so the changes are small enough we can do a thorough audit on them. Anyway take a look and see what you think.
Amazing ;)
This patch certainly needs a time for understanding, so far I have read only the small subset, a couple of random questions.
Well I think it succeeds as a proof of concept and totally fails as a production patch at this point.
Quoted 13 lines
include/linux/sched.h struct task_struct { · line 1040
Quoted 9 lines
include/linux/sched.h +0
-0* pgrp and session fields are deprecated. @@ -1034,8 +1035,9 @@ struct task_struct {struct
list_headsibling; /* linkage in my parent's children list */include/linux/sched.h +2
-1struct task_struct *group_leader;/* threadgroup leader */ +struct pid *tid;/* PID/PID hash table linkage. */ -struct pid_link pids[PIDTYPE_MAX ]; +struct hlist_node pids[PIDTYPE_ARRAY_MAX ];OK. It certainly makes sense to move PIDTYPE_PGID/SID pids from
task_structto signal struct.
Quoted 6 lines
But can't we go a bit further? With this patch pid->tasks[].first still "points" to leader's
task_struct. Suppose we replace pid->tasks[] with pid->signals[], so that pid->signals[].first points tosignal_struct. Then we can find the task (group_leader) viasignal->tgid.This means we can remove
task_struct->pids, and killtransfer_pid().
We need a way to sill implement do_each_pid_task, but otherwise that should work and be a nice clean up all on it's own.
Quoted 4 lines
Quoted 4 lines
static inline struct pid *task_tgid(struct
task_struct*task) {(quoted diff) +1
-1- return task->group_leader->pids[PIDTYPE_PID].pid; + struct signal_struct *sig = rcu_dereference(task->signal);
Quoted 8 lines
include/linux/sched.h static inline void set_task_pgrp(struct task_struct *tsk, pid_t pgrp) · line 1276
Quoted 5 lines
include/linux/sched.h +4
-0+struct pid *pid =NULL ; +if (sig) + pid = sig->tgid; +return pid; }Hmm. This is fixable, but note that
task->signalis not RCU protected, only ->sighand.
Yes. I realized that after I had sent the patch out. We do run those functions with just rcu protection sometimes so something would need to be resolved there.
Quoted 8 lines
include/linux/sched.h static inline pid_t task_ppid_nr_ns(struct task_struct *tsk, · line 1389
Quoted 5 lines
static inline int
pid_alive(struct task_struct *p){include/linux/sched.h +1
-1-return p->pids[PIDTYPE_PID ].pid !=NULL ; +return p->signal !=NULL ; }(this change btw is imho good regardless, because
pid_alive()currently
means "the task is not unhashed yet" anyway).
Yes.
Quoted 8 lines
Quoted 8 lines
static void
__unhash_process(struct task_struct *p){kernel/exit.c +0
-1nr_threads--; -detach_pid (p,PIDTYPE_PID );if (thread_group_leader (p)) {detach_pid (p,PIDTYPE_PGID );detach_pid (p,PIDTYPE_SID );@@ -65,6 +64,7 @@ static void __unhash_process(struct task_struct *p)
Quoted 8 lines
kernel/exit.c static void __unhash_process(struct task_struct *p) · line 67
Quoted 4 lines
kernel/exit.c +1
-0list_del_rcu (&p->tasks);__get_cpu_var (process_counts)--; } +detach_pid (p,PIDTYPE_PID );Not sure why this change is needed... To prevent the premature detach_pid()->free_pid() ? But this doesn't looks possible, if the task is leader,
p->tid->tsk== p, anddetach_pid()does
This is a bit of a relic of how my patch developed.
I had the "if (task->tid != tsk->signal->tgid)" check in there and was assuming the thread group id as my pid so I could clean things up properly. And it worked out nicer if the detach_pid was for PIDTYPE_PID came later as I could reuse the same logic as in de_thread.
Quoted 7 lines
if (pid->tsk) // still used, don't free. return;Quoted 3 lines
(quoted diff) +0
-0@@ -946,6 +920,48 @@ fastcall NORET_TYPE void do_exit(long code) }
Quoted 6 lines
kernel/exit.c fastcall NORET_TYPE void do_exit(long code) · line 924
Quoted 3 lines
kernel/exit.c +2
-0tsk->flags |=PF_EXITING ; +/* Transfer thread group leadership */ +if (thread_group_leader (tsk) && !thread_group_empty (tsk)) {Ah, this is racy without
tasklist_lock. Suppose that the current ->group_leader exits right now and elects us as a new leader.
Hmm. I thought I was redoing that test inside of the lock. Anyway this hunk probably needs the most work as it is brand new code.
Quoted 15 lines
kernel/exit.c fastcall NORET_TYPE void do_exit(long code) · line 935
Quoted 11 lines
kernel/exit.c +11
-0+struct task_struct *new_leader, *t; +write_lock_irq (&tasklist_lock); +for (t =next_thread (tsk); t != tsk; t =next_thread (t)) { +if (!(t->flags &PF_EXITING )) +break ; + } +if (t != tsk) { + new_leader = t; + + new_leader->start_time = tsk->start_time; +task_pid (tsk)->tsk = new_leader;So this pid won't be freed when current does
detach_pid(PIDTYPE_PID), from nowcurrent->tid->tsk!= current, sodetach_pid()doesn't clearpid->tsk.
Quoted 1 line
But when it will be freed then?
When new_leader does detach_pid on it.
Quoted 14 lines
kernel/exit.c fastcall NORET_TYPE void do_exit(long code) · line 942
Quoted 7 lines
kernel/exit.c +7
-0+transfer_pid (tsk, new_leader,PIDTYPE_PGID ); +transfer_pid (tsk, new_leader,PIDTYPE_SID ); +list_replace_rcu (&tsk->tasks, &new_leader->tasks); + +/* Update group_leader on all of the threads... */ + new_leader->group_leader = new_leader; + tsk->group_leader = new_leader;This is one of the most questionable changes. Now current can't "trust" its ->group_leader without tasklist.
As a random example, let's look at
sys_setpgid(). When we taketasklist_lockgroup_leadercan point to the already freed/reused memory.
Quoted 13 lines
kernel/exit.c fastcall NORET_TYPE void do_exit(long code) · line 961
Quoted 11 lines
kernel/exit.c +11
-0+ }else { +write_unlock_irq (&tasklist_lock); +/* Wait for the other threads to exit before continuing */ +for (;;) { +read_lock (&tasklist_lock); +if (thread_group_empty (tsk)) +break ; +__set_current_state (TASK_UNINTERRUPTIBLE ); +read_unlock (&tasklist_lock); +schedule (); + }I don't get this... Who will wake us?
Oh. Duh. I totally spaced that you need a wakeup in cases like this.
Quoted 2 lines
Hmm, deadlock with coredump? The coredumping thread may be waiting for us, but we didn't do
exit_mm()yet.
Yes. That part is pretty ugly.
Quoted 15 lines
Review static int wait_task_continued(struct task_struct *p,
Quoted 2 lines
(quoted diff) +0
-0@@ -1440,8 +1460,11 @@ static int wait_task_continued(struct task_struct *p,int noreap,
kernel/exit.c static int wait_task_continued(struct task_struct *p, int noreap, · line 1466
Quoted 9 lines
kernel/exit.c +4
-2if (!noreap) p->signal->flags &= ~SIGNAL_STOP_CONTINUED ;spin_unlock_irq (&p->sighand->siglock); - - pid =task_pid_nr_ns (p, current->nsproxy->pid_ns); +if (p->ptrace &&same_thread_group (current, p->parent)) + pid =task_pid (p); +else + pid =task_tgid (p);Why do we need "&&
same_thread_group()" above? Ptracer needs per-thread pid in any case.
It isn't easy to tell if our real parent or the ptracer is waiting for us. My thinking was to detect the ptrace waiter by doing current == p->parent, and then generalizing that to same_thread_group. I now see that has a problem if the real parent and the ptracer are the same.
I guess I need to use the state information and simply not tell the real parent that we have stopped for a ptrace stop, or visa versa.
Quoted 7 lines
Review static void do_notify_parent_cldstop(struct
Quoted 1 line
(quoted diff) +0
-0@@ -1501,12 +1487,15 @@ static void do_notify_parent_cldstop(struct
task_struct*tsk, int why)Quoted 5 lines
kernel/signal.c +1
-0unsigned long flags;struct task_struct *parent;struct sighand_struct *sighand; +struct pid *pid;
Quoted 11 lines
kernel/signal.c static void do_notify_parent_cldstop(struct task_struct *tsk, int why) · line 1498
Quoted 9 lines
kernel/signal.c +4
-2-if (tsk->ptrace &PT_PTRACED ) +if (tsk->ptrace &PT_PTRACED ) { parent = tsk->parent; -else { + pid =task_pid (tsk); + }else { tsk = tsk->group_leader; parent = tsk->real_parent; + pid =task_tgid (tsk);Hmm, yes... With this patch
task_pid(p->leader)!=task_tgid(p).
Not necessarily no, and that is a pain.
Quoted 1 line
Will continue tomorrow.
Thanks
Eric