pidns: premature pid_ns_release_proc() fixes
A message that is not in the archive (sent off-list or not yet received): [email protected]
cover pidns: premature pid_ns_release_proc() fixes
Damn. Forgot to add lkml, resending. Sorry for spam.
Quoted 14 lines · Oleg Nesterov
Hello,
Our discussion was a bit confusing, I am resending the pidns fixes,
1/2 - Eric's patch (was recently "to-be-updated") + cleanups from me
2/2 - another fix, acked by Eric.
Cough. checkpatch complains about line over 80 characters. Can we sometime ignore this warning? Any change I tried makes the code less readable.
Oleg.
PATCH 1/2 pidns: guarantee that the pidns init will be the last pidns process reaped
From: Eric W. Biederman <[email protected]>
Today we have a two-fold bug. Sometimes release_task on pid == 1 in a pid namespace can run before other processes in a pid namespace have had release task called. With the result that pid_ns_release_proc can be called before the last proc_flus_task() is done using upid->ns->proc_mnt, resulting in the use of a stale pointer. This same set of circumstances can lead to waitpid(...) returning for a processes started with clone(CLONE_NEWPID) before the every process in the pid namespace has actually exited.
To fix this modify zap_pid_ns_processess wait until all other processes in the pid namespace have exited, even EXIT_DEAD zombies.
The delay_group_leader and related tests ensure that the thread gruop leader will be the last thread of a process group to be reaped, or to become EXIT_DEAD and self reap. With the change to zap_pid_ns_processes we get the guarantee that pid == 1 in a pid namespace will be the last task that release_task is called on.
With pid == 1 being the last task to pass through release_task pid_ns_release_proc can no longer be called too early nor can wait return before all of the EXIT_DEAD tasks in a pid namespace have exited.
--- kernel/exit.c | 14 +++++++++++++- kernel/pid_namespace.c | 20 ++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletions(-)
kernel/exit.c +13 -1
@@ -64,7 +64,6 @@ static void exit_mm(struct task_struct * tsk);static void __unhash_process (struct task_struct *p,bool group_dead) { nr_threads--; -detach_pid (p,PIDTYPE_PID );if (group_dead) {detach_pid (p,PIDTYPE_PGID );detach_pid (p,PIDTYPE_SID );
@@ -72,7 +71,20 @@ static void __unhash_process(struct task_struct *p, bool group_dead)list_del_rcu (&p->tasks);list_del_init (&p->sibling);__this_cpu_dec (process_counts); +/* +* If we are the last child process in a pid namespace to be +* reaped, notify the reaper sleeping zap_pid_ns_processes(). +*/ +if (IS_ENABLED (CONFIG_PID_NS )) { +struct task_struct *parent = p->real_parent; + +if ((task_active_pid_ns (p)->child_reaper == parent) && +list_empty (&parent->children) && + (parent->flags &PF_EXITING )) +wake_up_process (parent); + } } +detach_pid (p,PIDTYPE_PID );list_del_rcu (&p->thread_group); }
kernel/pid_namespace.c +20 -0
@@ -179,11 +179,31 @@ void zap_pid_ns_processes(struct pid_namespace *pid_ns) }read_unlock (&tasklist_lock); +/* Firstly reap the EXIT_ZOMBIE children we may have. */ do {clear_thread_flag (TIF_SIGPENDING ); rc =sys_wait4 (-1 ,NULL , __WALL,NULL ); }while (rc != -ECHILD ); +/* +* sys_wait4() above can't reap the TASK_DEAD children. +* Make sure they all go away, see __unhash_process(). +*/ +for (;;) { +bool need_wait = false; + +read_lock (&tasklist_lock); +if (!list_empty (¤t->children)) { +__set_current_state (TASK_UNINTERRUPTIBLE ); + need_wait = true; + } +read_unlock (&tasklist_lock); + +if (!need_wait) +break ; +schedule (); + } +if (pid_ns->reboot) current->signal->group_exit_code = pid_ns->reboot;
Signature · 1 line
1.5.5.1
Re: [PATCH 1/2] pidns: guarantee that the pidns init will be the last pidns process reaped
Quoted 20 lines · Oleg Nesterov
From: Eric W. Biederman <[email protected]>
Today we have a two-fold bug. Sometimes
release_taskon pid == 1 in a pid namespace can run before other processes in a pid namespace have had release task called. With the result thatpid_ns_release_proccan be called before the lastproc_flus_task()is done usingupid->ns->proc_mnt, resulting in the use of a stale pointer. This same set of circumstances can lead to waitpid(...) returning for a processes started withclone(CLONE_NEWPID)before the every process in the pid namespace has actually exited.To fix this modify
zap_pid_ns_processesswait until all other processes in the pid namespace have exited, evenEXIT_DEADzombies.The
delay_group_leaderand related tests ensure that the thread gruop leader will be the last thread of a process group to be reaped, or to becomeEXIT_DEADand self reap. With the change tozap_pid_ns_processeswe get the guarantee that pid == 1 in a pid namespace will be the last task thatrelease_taskis called on.
Quoted 6 lines
With pid == 1 being the last task to pass through
release_taskpid_ns_release_proccan no longer be called too early nor can wait return before all of theEXIT_DEADtasks in a pid namespace have exited.Signed-off-by Eric W. Biederman <[email protected]>Signed-off-by Oleg Nesterov <[email protected]>
Re: [PATCH 1/2] pidns: guarantee that the pidns init will be the last pidns process reaped
In reply to Oleg NesterovQuoted 78 lines · On Wed, 30 May 2012 20:15:00 +0200 Oleg Nesterov <[email protected]> wrote:
From: Eric W. Biederman <[email protected]>
Today we have a two-fold bug. Sometimes
release_taskon pid == 1 in a pid namespace can run before other processes in a pid namespace have had release task called. With the result thatpid_ns_release_proccan be called before the lastproc_flus_task()is done usingupid->ns->proc_mnt, resulting in the use of a stale pointer. This same set of circumstances can lead to waitpid(...) returning for a processes started withclone(CLONE_NEWPID)before the every process in the pid namespace has actually exited.To fix this modify
zap_pid_ns_processesswait until all other processes in the pid namespace have exited, evenEXIT_DEADzombies.The
delay_group_leaderand related tests ensure that the thread gruop leader will be the last thread of a process group to be reaped, or to becomeEXIT_DEADand self reap. With the change tozap_pid_ns_processeswe get the guarantee that pid == 1 in a pid namespace will be the last task thatrelease_taskis called on.With pid == 1 being the last task to pass through
release_taskpid_ns_release_proccan no longer be called too early nor can wait return before all of theEXIT_DEADtasks in a pid namespace have exited....
kernel/exit.c +13
-1@@ -64,7 +64,6 @@ static void exit_mm(struct task_struct * tsk);static void __unhash_process (struct task_struct *p,bool group_dead) { nr_threads--; -detach_pid (p,PIDTYPE_PID );if (group_dead) {detach_pid (p,PIDTYPE_PGID );detach_pid (p,PIDTYPE_SID );@@ -72,7 +71,20 @@ static void __unhash_process(struct task_struct *p, bool group_dead)list_del_rcu (&p->tasks);list_del_init (&p->sibling);__this_cpu_dec (process_counts); +/* +* If we are the last child process in a pid namespace to be +* reaped, notify the reaper sleeping zap_pid_ns_processes(). +*/ +if (IS_ENABLED (CONFIG_PID_NS )) { +struct task_struct *parent = p->real_parent; + +if ((task_active_pid_ns (p)->child_reaper == parent) && +list_empty (&parent->children) && + (parent->flags &PF_EXITING )) +wake_up_process (parent); + } } +detach_pid (p,PIDTYPE_PID );list_del_rcu (&p->thread_group); }kernel/pid_namespace.c +8
-0@@ -179,11 +179,31 @@ void zap_pid_ns_processes(struct pid_namespace *pid_ns) }read_unlock (&tasklist_lock); +/* Firstly reap the EXIT_ZOMBIE children we may have. */ do {clear_thread_flag (TIF_SIGPENDING ); rc =sys_wait4 (-1 ,NULL , __WALL,NULL ); }while (rc != -ECHILD ); +/* +* sys_wait4() above can't reap the TASK_DEAD children. +* Make sure they all go away, see __unhash_process(). +*/ +for (;;) { +bool need_wait = false; +
kernel/pid_namespace.c void zap_pid_ns_processes(struct pid_namespace *pid_ns) · line 204
Quoted 10 lines
kernel/pid_namespace.c +10
-0+read_lock (&tasklist_lock); +if (!list_empty (¤t->children)) { +__set_current_state (TASK_UNINTERRUPTIBLE ); + need_wait = true; + } +read_unlock (&tasklist_lock); + +if (!need_wait) +break ; +schedule ();
This sleep is terminated by the above wake_up_process(), yes?
But that wake_up_process() only happens if CONFIG_PID_NS. It's unobvious (to me) that we can't get stuck in D state if CONFIG_PID_NS=n. If bug, please fix. If not bug, please make obvious to me ;)
<looks at the locking a bit>
<gets distracted>
That tty_kref_put() in __exit_signal() is running with tasklist_lock held, yes? It does a ton of work and calls out to random drivers and none of this needs tasklist_lock. Seems risky.
Re: [PATCH 1/2] pidns: guarantee that the pidns init will be the last pidns process reaped
Quoted 56 lines · Andrew Morton
Quoted 56 lines
kernel/exit.c +13
-1@@ -64,7 +64,6 @@ static void exit_mm(struct task_struct * tsk);static void __unhash_process (struct task_struct *p,bool group_dead) { nr_threads--; -detach_pid (p,PIDTYPE_PID );if (group_dead) {detach_pid (p,PIDTYPE_PGID );detach_pid (p,PIDTYPE_SID );@@ -72,7 +71,20 @@ static void __unhash_process(struct task_struct *p, bool group_dead)list_del_rcu (&p->tasks);list_del_init (&p->sibling);__this_cpu_dec (process_counts); +/* +* If we are the last child process in a pid namespace to be +* reaped, notify the reaper sleeping zap_pid_ns_processes(). +*/ +if (IS_ENABLED (CONFIG_PID_NS )) { +struct task_struct *parent = p->real_parent; + +if ((task_active_pid_ns (p)->child_reaper == parent) && +list_empty (&parent->children) && + (parent->flags &PF_EXITING )) +wake_up_process (parent); + } } +detach_pid (p,PIDTYPE_PID );list_del_rcu (&p->thread_group); }kernel/pid_namespace.c +12
-0@@ -179,11 +179,31 @@ void zap_pid_ns_processes(struct pid_namespace *pid_ns) }read_unlock (&tasklist_lock); +/* Firstly reap the EXIT_ZOMBIE children we may have. */ do {clear_thread_flag (TIF_SIGPENDING ); rc =sys_wait4 (-1 ,NULL , __WALL,NULL ); }while (rc != -ECHILD ); +/* +* sys_wait4() above can't reap the TASK_DEAD children. +* Make sure they all go away, see __unhash_process(). +*/ +for (;;) { +bool need_wait = false; + +read_lock (&tasklist_lock); +if (!list_empty (¤t->children)) { +__set_current_state (TASK_UNINTERRUPTIBLE ); + need_wait = true;
Quoted 8 lines
kernel/pid_namespace.c void zap_pid_ns_processes(struct pid_namespace *pid_ns) · line 204
Quoted 6 lines
kernel/pid_namespace.c +6
-0+ } +read_unlock (&tasklist_lock); + +if (!need_wait) +break ; +schedule ();This sleep is terminated by the above
wake_up_process(), yes?
Yes.
Quoted 4 lines
But that
wake_up_process()only happens ifCONFIG_PID_NS. It's unobvious (to me) that we can't get stuck in D state ifCONFIG_PID_NS=n. If bug, please fix. If not bug, please make obvious to me ;)
The file pid_namespace.c that holds zap_pid_ns_processes is only compiled with CONFIG_PID_NS set. So we can't possibly get stuck with CONFIG_PID_NS=n.
Quoted 7 lines
<looks at the locking a bit>
<gets distracted>
That
tty_kref_put()in__exit_signal()is running withtasklist_lockheld, yes? It does a ton of work and calls out to random drivers and none of this needstasklist_lock. Seems risky.
Interesting. That tty_kref_put does sound like an area where the locking can be simplified. At the same time tty_kref_put does make sense from exit signal. As ttys and signals are intimately intertwined.
Thank you for taking the time looking at this and applying this change.
I have to agree that the tasklist_lock is pretty horrible, and that if we can somehow figure out how to remove it we would be in a much better situation with lock contention. Unfortunately that is an alligator and I am working to drain the swamp.
Eric
Re: [PATCH 1/2] pidns: guarantee that the pidns init will be the last pidns process reaped
Quoted 7 lines · Eric W. Biederman
Quoted 4 lines · Andrew Morton
<looks at the locking a bit>
<gets distracted>
Quoted 7 lines
Quoted 3 lines
That
tty_kref_put()in__exit_signal()is running withtasklist_lockheld, yes? It does a ton of work and calls out to random drivers and none of this needstasklist_lock. Seems risky.Interesting. That
tty_kref_putdoes sound like an area where the locking can be simplified. At the same timetty_kref_putdoes make sense from exit signal. As ttys and signals are intimately intertwined.
This was introduced in 2008, 9c9f4ded90a59eee84e15f5fd38c03d60184e112
Nobody complained so far... but I agree this doesn't look very good. At first glance it is simle to move this kref_put() outside of tasklist_lock. Something like below.
But I'll re-check. And I guess the patch can be simpler/cleaner. Say, __exit_signal() can return tty or group_dead.
Oleg.
kernel/exit.c +7 -7
@@ -79,12 +79,11 @@ static void __unhash_process(struct task/* * This function expects the tasklist_lock write-locked. */ -static void __exit_signal (struct task_struct *tsk) +static void __exit_signal (struct task_struct *tsk,struct tty_struct **ptty) {struct signal_struct *sig = tsk->signal;bool group_dead =thread_group_leader (tsk);struct sighand_struct *sighand; -struct tty_struct *uninitialized_var (tty ); sighand =rcu_dereference_check (tsk->sighand,lockdep_tasklist_lock_is_held ());
@@ -93,7 +92,7 @@ static void __exit_signal(struct task_stposix_cpu_timers_exit (tsk);if (group_dead) {posix_cpu_timers_exit_group (tsk); - tty = sig->tty; + *ptty = sig->tty; sig->tty =NULL ; }else {/*
@@ -149,10 +148,8 @@ static void __exit_signal(struct task_st__cleanup_sighand (sighand);clear_tsk_thread_flag (tsk,TIF_SIGPENDING ); -if (group_dead) { +if (group_dead)flush_sigqueue (&sig->shared_pending); -tty_kref_put (tty); - } }static void delayed_put_task_struct (struct rcu_head *rhp)
@@ -167,6 +164,7 @@ static void delayed_put_task_struct(struvoid release_task (struct task_struct * p) { +struct tty_struct *tty =NULL ;struct task_struct *leader;int zap_leader; repeat:
@@ -180,7 +178,7 @@ repeat:write_lock_irq (&tasklist_lock);ptrace_release_task (p); -__exit_signal (p); +__exit_signal (p, &tty);/* * If we are the last non-leader member of the thread
@@ -207,6 +205,8 @@ repeat: p = leader;if (unlikely (zap_leader)) goto repeat; + +tty_kref_put (tty); }/*
PATCH 2/2 pidns: find_new_reaper() can no longer switch to init_pid_ns.child_reaper
In reply to Oleg Nesterovfind_new_reaper() changes pid_ns->child_reaper, see add0d4df "pid_ns: zap_pid_ns_processes: fix the ->child_reaper changing".
The original reason has gone away after the previous patch, ->children list must be empty after zap_pid_ns_processes().
However now we can not switch to init_pid_ns.child_reaper. __unhash_process() relies on the "->child_reaper == parent" check, but this check does not work if the last exiting task is also the child reaper.
As Eric sugested, we can change __unhash_process() to use the parent's pid_ns and remove this code.
Also, with this change we can move detach_pid(PIDTYPE_PID) back, where it was before the previous fix.
--- kernel/exit.c | 10 ++-------- 1 files changed, 2 insertions(+), 8 deletions(-)
kernel/exit.c +2 -8
@@ -64,6 +64,7 @@ static void exit_mm(struct task_struct * tsk);static void __unhash_process (struct task_struct *p,bool group_dead) { nr_threads--; +detach_pid (p,PIDTYPE_PID );if (group_dead) {detach_pid (p,PIDTYPE_PGID );detach_pid (p,PIDTYPE_SID );
@@ -78,13 +79,12 @@ static void __unhash_process(struct task_struct *p, bool group_dead)if (IS_ENABLED (CONFIG_PID_NS )) {struct task_struct *parent = p->real_parent; -if ((task_active_pid_ns (p)->child_reaper == parent) && +if ((task_active_pid_ns (parent)->child_reaper == parent) &&list_empty (&parent->children) && (parent->flags &PF_EXITING ))wake_up_process (parent); } } -detach_pid (p,PIDTYPE_PID );list_del_rcu (&p->thread_group); }
@@ -731,12 +731,6 @@ static struct task_struct *find_new_reaper(struct task_struct *father)zap_pid_ns_processes (pid_ns);write_lock_irq (&tasklist_lock); -/* -* We can not clear ->child_reaper or leave it alone. -* There may by stealth EXIT_DEAD tasks on ->children, -* forget_original_parent() must move them somewhere. -*/ - pid_ns->child_reaper = init_pid_ns.child_reaper; }else if (father->signal->has_child_subreaper) {struct task_struct *reaper;
Signature · 1 line
1.5.5.1
Re: [PATCH 2/2] pidns: find_new_reaper() can no longer switch to init_pid_ns.child_reaper
Quoted 11 lines · Oleg Nesterov
find_new_reaper()changespid_ns->child_reaper, seeadd0d4df"pid_ns:zap_pid_ns_processes: fix the ->child_reaper changing".The original reason has gone away after the previous patch, ->children list must be empty after
zap_pid_ns_processes().However now we can not switch to init_pid_ns.child_reaper.
__unhash_process()relies on the "->child_reaper == parent" check, but this check does not work if the last exiting task is also the child reaper.
Quoted 8 lines
As Eric sugested, we can change
__unhash_process()to use the parent'spid_nsand remove this code.Also, with this change we can move
detach_pid(PIDTYPE_PID)back, where it was before the previous fix.Signed-off-by Oleg Nesterov <[email protected]>