Commit 84d73786 authored by Sukadev Bhattiprolu's avatar Sukadev Bhattiprolu Committed by Linus Torvalds

[PATCH] add child reaper to pid_namespace

Add a per pid_namespace child-reaper.  This is needed so processes are reaped
within the same pid space and do not spill over to the parent pid space.  Its
also needed so containers preserve existing semantic that pid == 1 would reap
orphaned children.

This is based on Eric Biederman's patch: http://lkml.org/lkml/2006/2/6/285Signed-off-by: default avatarSukadev Bhattiprolu <sukadev@us.ibm.com>
Signed-off-by: default avatarCedric Le Goater <clg@fr.ibm.com>
Cc: Kirill Korotaev <dev@openvz.org>
Cc: Eric W. Biederman <ebiederm@xmission.com>
Cc: Herbert Poetzl <herbert@13thfloor.at>
Signed-off-by: default avatarAndrew Morton <akpm@osdl.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@osdl.org>
parent 6cc1b22a
...@@ -38,6 +38,7 @@ ...@@ -38,6 +38,7 @@
#include <linux/binfmts.h> #include <linux/binfmts.h>
#include <linux/swap.h> #include <linux/swap.h>
#include <linux/utsname.h> #include <linux/utsname.h>
#include <linux/pid_namespace.h>
#include <linux/module.h> #include <linux/module.h>
#include <linux/namei.h> #include <linux/namei.h>
#include <linux/proc_fs.h> #include <linux/proc_fs.h>
...@@ -620,8 +621,8 @@ static int de_thread(struct task_struct *tsk) ...@@ -620,8 +621,8 @@ static int de_thread(struct task_struct *tsk)
* Reparenting needs write_lock on tasklist_lock, * Reparenting needs write_lock on tasklist_lock,
* so it is safe to do it under read_lock. * so it is safe to do it under read_lock.
*/ */
if (unlikely(tsk->group_leader == child_reaper)) if (unlikely(tsk->group_leader == child_reaper(tsk)))
child_reaper = tsk; tsk->nsproxy->pid_ns->child_reaper = tsk;
zap_other_threads(tsk); zap_other_threads(tsk);
read_unlock(&tasklist_lock); read_unlock(&tasklist_lock);
......
...@@ -35,8 +35,9 @@ enum pid_type ...@@ -35,8 +35,9 @@ enum pid_type
* *
* Holding a reference to struct pid solves both of these problems. * Holding a reference to struct pid solves both of these problems.
* It is small so holding a reference does not consume a lot of * It is small so holding a reference does not consume a lot of
* resources, and since a new struct pid is allocated when the numeric * resources, and since a new struct pid is allocated when the numeric pid
* pid value is reused we don't mistakenly refer to new processes. * value is reused (when pids wrap around) we don't mistakenly refer to new
* processes.
*/ */
struct pid struct pid
......
...@@ -19,6 +19,7 @@ struct pid_namespace { ...@@ -19,6 +19,7 @@ struct pid_namespace {
struct kref kref; struct kref kref;
struct pidmap pidmap[PIDMAP_ENTRIES]; struct pidmap pidmap[PIDMAP_ENTRIES];
int last_pid; int last_pid;
struct task_struct *child_reaper;
}; };
extern struct pid_namespace init_pid_ns; extern struct pid_namespace init_pid_ns;
...@@ -36,4 +37,9 @@ static inline void put_pid_ns(struct pid_namespace *ns) ...@@ -36,4 +37,9 @@ static inline void put_pid_ns(struct pid_namespace *ns)
kref_put(&ns->kref, free_pid_ns); kref_put(&ns->kref, free_pid_ns);
} }
static inline struct task_struct *child_reaper(struct task_struct *tsk)
{
return tsk->nsproxy->pid_ns->child_reaper;
}
#endif /* _LINUX_PID_NS_H */ #endif /* _LINUX_PID_NS_H */
...@@ -1400,7 +1400,6 @@ extern NORET_TYPE void do_group_exit(int); ...@@ -1400,7 +1400,6 @@ extern NORET_TYPE void do_group_exit(int);
extern void daemonize(const char *, ...); extern void daemonize(const char *, ...);
extern int allow_signal(int); extern int allow_signal(int);
extern int disallow_signal(int); extern int disallow_signal(int);
extern struct task_struct *child_reaper;
extern int do_execve(char *, char __user * __user *, char __user * __user *, struct pt_regs *); extern int do_execve(char *, char __user * __user *, char __user * __user *, struct pt_regs *);
extern long do_fork(unsigned long, unsigned long, struct pt_regs *, unsigned long, int __user *, int __user *); extern long do_fork(unsigned long, unsigned long, struct pt_regs *, unsigned long, int __user *, int __user *);
......
...@@ -51,6 +51,7 @@ ...@@ -51,6 +51,7 @@
#include <linux/debug_locks.h> #include <linux/debug_locks.h>
#include <linux/lockdep.h> #include <linux/lockdep.h>
#include <linux/utsrelease.h> #include <linux/utsrelease.h>
#include <linux/pid_namespace.h>
#include <linux/compile.h> #include <linux/compile.h>
#include <asm/io.h> #include <asm/io.h>
...@@ -626,8 +627,6 @@ static int __init initcall_debug_setup(char *str) ...@@ -626,8 +627,6 @@ static int __init initcall_debug_setup(char *str)
} }
__setup("initcall_debug", initcall_debug_setup); __setup("initcall_debug", initcall_debug_setup);
struct task_struct *child_reaper = &init_task;
extern initcall_t __initcall_start[], __initcall_end[]; extern initcall_t __initcall_start[], __initcall_end[];
static void __init do_initcalls(void) static void __init do_initcalls(void)
...@@ -727,7 +726,7 @@ static int init(void * unused) ...@@ -727,7 +726,7 @@ static int init(void * unused)
* assumptions about where in the task array this * assumptions about where in the task array this
* can be found. * can be found.
*/ */
child_reaper = current; init_pid_ns.child_reaper = current;
cad_pid = task_pid(current); cad_pid = task_pid(current);
......
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
#include <linux/file.h> #include <linux/file.h>
#include <linux/binfmts.h> #include <linux/binfmts.h>
#include <linux/nsproxy.h> #include <linux/nsproxy.h>
#include <linux/pid_namespace.h>
#include <linux/ptrace.h> #include <linux/ptrace.h>
#include <linux/profile.h> #include <linux/profile.h>
#include <linux/mount.h> #include <linux/mount.h>
...@@ -48,7 +49,6 @@ ...@@ -48,7 +49,6 @@
#include <asm/mmu_context.h> #include <asm/mmu_context.h>
extern void sem_exit (void); extern void sem_exit (void);
extern struct task_struct *child_reaper;
static void exit_mm(struct task_struct * tsk); static void exit_mm(struct task_struct * tsk);
...@@ -260,7 +260,8 @@ static int has_stopped_jobs(int pgrp) ...@@ -260,7 +260,8 @@ static int has_stopped_jobs(int pgrp)
} }
/** /**
* reparent_to_init - Reparent the calling kernel thread to the init task. * reparent_to_init - Reparent the calling kernel thread to the init task
* of the pid space that the thread belongs to.
* *
* If a kernel thread is launched as a result of a system call, or if * If a kernel thread is launched as a result of a system call, or if
* it ever exits, it should generally reparent itself to init so that * it ever exits, it should generally reparent itself to init so that
...@@ -278,8 +279,8 @@ static void reparent_to_init(void) ...@@ -278,8 +279,8 @@ static void reparent_to_init(void)
ptrace_unlink(current); ptrace_unlink(current);
/* Reparent to init */ /* Reparent to init */
remove_parent(current); remove_parent(current);
current->parent = child_reaper; current->parent = child_reaper(current);
current->real_parent = child_reaper; current->real_parent = child_reaper(current);
add_parent(current); add_parent(current);
/* Set the exit signal to SIGCHLD so we signal init on exit */ /* Set the exit signal to SIGCHLD so we signal init on exit */
...@@ -662,7 +663,8 @@ reparent_thread(struct task_struct *p, struct task_struct *father, int traced) ...@@ -662,7 +663,8 @@ reparent_thread(struct task_struct *p, struct task_struct *father, int traced)
* When we die, we re-parent all our children. * When we die, we re-parent all our children.
* Try to give them to another thread in our thread * Try to give them to another thread in our thread
* group, and if no such member exists, give it to * group, and if no such member exists, give it to
* the global child reaper process (ie "init") * the child reaper process (ie "init") in our pid
* space.
*/ */
static void static void
forget_original_parent(struct task_struct *father, struct list_head *to_release) forget_original_parent(struct task_struct *father, struct list_head *to_release)
...@@ -673,7 +675,7 @@ forget_original_parent(struct task_struct *father, struct list_head *to_release) ...@@ -673,7 +675,7 @@ forget_original_parent(struct task_struct *father, struct list_head *to_release)
do { do {
reaper = next_thread(reaper); reaper = next_thread(reaper);
if (reaper == father) { if (reaper == father) {
reaper = child_reaper; reaper = child_reaper(father);
break; break;
} }
} while (reaper->exit_state); } while (reaper->exit_state);
...@@ -859,8 +861,13 @@ fastcall NORET_TYPE void do_exit(long code) ...@@ -859,8 +861,13 @@ fastcall NORET_TYPE void do_exit(long code)
panic("Aiee, killing interrupt handler!"); panic("Aiee, killing interrupt handler!");
if (unlikely(!tsk->pid)) if (unlikely(!tsk->pid))
panic("Attempted to kill the idle task!"); panic("Attempted to kill the idle task!");
if (unlikely(tsk == child_reaper)) if (unlikely(tsk == child_reaper(tsk))) {
if (tsk->nsproxy->pid_ns != &init_pid_ns)
tsk->nsproxy->pid_ns->child_reaper = init_pid_ns.child_reaper;
else
panic("Attempted to kill init!"); panic("Attempted to kill init!");
}
if (unlikely(current->ptrace & PT_TRACE_EXIT)) { if (unlikely(current->ptrace & PT_TRACE_EXIT)) {
current->ptrace_message = code; current->ptrace_message = code;
......
...@@ -65,7 +65,8 @@ struct pid_namespace init_pid_ns = { ...@@ -65,7 +65,8 @@ struct pid_namespace init_pid_ns = {
.pidmap = { .pidmap = {
[ 0 ... PIDMAP_ENTRIES-1] = { ATOMIC_INIT(BITS_PER_PAGE), NULL } [ 0 ... PIDMAP_ENTRIES-1] = { ATOMIC_INIT(BITS_PER_PAGE), NULL }
}, },
.last_pid = 0 .last_pid = 0,
.child_reaper = &init_task
}; };
/* /*
......
...@@ -24,6 +24,9 @@ ...@@ -24,6 +24,9 @@
#include <linux/signal.h> #include <linux/signal.h>
#include <linux/capability.h> #include <linux/capability.h>
#include <linux/freezer.h> #include <linux/freezer.h>
#include <linux/pid_namespace.h>
#include <linux/nsproxy.h>
#include <asm/param.h> #include <asm/param.h>
#include <asm/uaccess.h> #include <asm/uaccess.h>
#include <asm/unistd.h> #include <asm/unistd.h>
...@@ -1877,8 +1880,12 @@ int get_signal_to_deliver(siginfo_t *info, struct k_sigaction *return_ka, ...@@ -1877,8 +1880,12 @@ int get_signal_to_deliver(siginfo_t *info, struct k_sigaction *return_ka,
if (sig_kernel_ignore(signr)) /* Default is nothing. */ if (sig_kernel_ignore(signr)) /* Default is nothing. */
continue; continue;
/* Init gets no signals it doesn't want. */ /*
if (current == child_reaper) * Init of a pid space gets no signals it doesn't want from
* within that pid space. It can of course get signals from
* its parent pid space.
*/
if (current == child_reaper(current))
continue; continue;
if (sig_kernel_stop(signr)) { if (sig_kernel_stop(signr)) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment