Freezer: make kernel threads nonfreezable by default
[safe/jmp/linux-2.6] / kernel / fork.c
index 4cf8684..ba39bdb 100644 (file)
@@ -14,7 +14,6 @@
 #include <linux/slab.h>
 #include <linux/init.h>
 #include <linux/unistd.h>
-#include <linux/smp_lock.h>
 #include <linux/module.h>
 #include <linux/vmalloc.h>
 #include <linux/completion.h>
 #include <linux/acct.h>
 #include <linux/tsacct_kern.h>
 #include <linux/cn_proc.h>
+#include <linux/freezer.h>
 #include <linux/delayacct.h>
 #include <linux/taskstats_kern.h>
 #include <linux/random.h>
+#include <linux/tty.h>
 
 #include <asm/pgtable.h>
 #include <asm/pgalloc.h>
@@ -106,7 +107,7 @@ static struct kmem_cache *mm_cachep;
 
 void free_task(struct task_struct *tsk)
 {
-       free_thread_info(tsk->thread_info);
+       free_thread_info(tsk->stack);
        rt_mutex_debug_task_free(tsk);
        free_task_struct(tsk);
 }
@@ -176,7 +177,7 @@ static struct task_struct *dup_task_struct(struct task_struct *orig)
        }
 
        *tsk = *orig;
-       tsk->thread_info = ti;
+       tsk->stack = ti;
        setup_thread_stack(tsk, orig);
 
 #ifdef CONFIG_CC_STACKPROTECTOR
@@ -286,6 +287,8 @@ static inline int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm)
                if (retval)
                        goto out;
        }
+       /* a new mm has just been created */
+       arch_dup_mmap(oldmm, mm);
        retval = 0;
 out:
        up_write(&mm->mmap_sem);
@@ -858,7 +861,7 @@ static inline int copy_signal(unsigned long clone_flags, struct task_struct * ts
        init_sigpending(&sig->shared_pending);
        INIT_LIST_HEAD(&sig->posix_timers);
 
-       hrtimer_init(&sig->real_timer, CLOCK_MONOTONIC, HRTIMER_REL);
+       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;
@@ -869,12 +872,13 @@ static inline int copy_signal(unsigned long clone_flags, struct task_struct * ts
        sig->it_prof_incr = cputime_zero;
 
        sig->leader = 0;        /* session leadership doesn't inherit */
-       sig->tty_old_pgrp = 0;
+       sig->tty_old_pgrp = NULL;
 
        sig->utime = sig->stime = sig->cutime = sig->cstime = cputime_zero;
        sig->nvcsw = sig->nivcsw = sig->cnvcsw = sig->cnivcsw = 0;
        sig->min_flt = sig->maj_flt = sig->cmin_flt = sig->cmaj_flt = 0;
-       sig->sched_time = 0;
+       sig->inblock = sig->oublock = sig->cinblock = sig->coublock = 0;
+       sig->sum_sched_runtime = 0;
        INIT_LIST_HEAD(&sig->cpu_timers[0]);
        INIT_LIST_HEAD(&sig->cpu_timers[1]);
        INIT_LIST_HEAD(&sig->cpu_timers[2]);
@@ -894,6 +898,8 @@ static inline int copy_signal(unsigned long clone_flags, struct task_struct * ts
        }
        acct_init_pacct(&sig->pacct);
 
+       tty_audit_fork(sig);
+
        return 0;
 }
 
@@ -917,7 +923,7 @@ static inline void copy_flags(unsigned long clone_flags, struct task_struct *p)
 {
        unsigned long new_flags = p->flags;
 
-       new_flags &= ~(PF_SUPERPRIV | PF_NOFREEZE);
+       new_flags &= ~PF_SUPERPRIV;
        new_flags |= PF_FORKNOEXEC;
        if (!(clone_flags & CLONE_PTRACE))
                p->ptrace = 0;
@@ -933,8 +939,8 @@ asmlinkage long sys_set_tid_address(int __user *tidptr)
 
 static inline void rt_mutex_init_task(struct task_struct *p)
 {
-#ifdef CONFIG_RT_MUTEXES
        spin_lock_init(&p->pi_lock);
+#ifdef CONFIG_RT_MUTEXES
        plist_head_init(&p->pi_waiters, &p->pi_lock);
        p->pi_blocked_on = NULL;
 #endif
@@ -954,7 +960,7 @@ static struct task_struct *copy_process(unsigned long clone_flags,
                                        unsigned long stack_size,
                                        int __user *parent_tidptr,
                                        int __user *child_tidptr,
-                                       int pid)
+                                       struct pid *pid)
 {
        int retval;
        struct task_struct *p = NULL;
@@ -996,7 +1002,7 @@ static struct task_struct *copy_process(unsigned long clone_flags,
        if (atomic_read(&p->user->processes) >=
                        p->signal->rlim[RLIMIT_NPROC].rlim_cur) {
                if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RESOURCE) &&
-                               p->user != &root_user)
+                   p->user != current->nsproxy->user_ns->root_user)
                        goto bad_fork_free;
        }
 
@@ -1021,7 +1027,7 @@ static struct task_struct *copy_process(unsigned long clone_flags,
        p->did_exec = 0;
        delayacct_tsk_init(p);  /* Must remain after dup_task_struct() */
        copy_flags(clone_flags, p);
-       p->pid = pid;
+       p->pid = pid_nr(pid);
        retval = -EFAULT;
        if (clone_flags & CLONE_PARENT_SETTID)
                if (put_user(p->pid, parent_tidptr))
@@ -1037,11 +1043,13 @@ static struct task_struct *copy_process(unsigned long clone_flags,
 
        p->utime = cputime_zero;
        p->stime = cputime_zero;
-       p->sched_time = 0;
+
+#ifdef CONFIG_TASK_XACCT
        p->rchar = 0;           /* I/O counter: bytes read */
        p->wchar = 0;           /* I/O counter: bytes written */
        p->syscr = 0;           /* I/O counter: read syscalls */
        p->syscw = 0;           /* I/O counter: write syscalls */
+#endif
        task_io_accounting_init(p);
        acct_clear_integrals(p);
 
@@ -1054,6 +1062,8 @@ static struct task_struct *copy_process(unsigned long clone_flags,
 
        p->lock_depth = -1;             /* -1 = no lock */
        do_posix_clock_monotonic_gettime(&p->start_time);
+       p->real_start_time = p->start_time;
+       monotonic_to_bootbased(&p->real_start_time);
        p->security = NULL;
        p->io_context = NULL;
        p->io_wait = NULL;
@@ -1248,13 +1258,13 @@ static struct task_struct *copy_process(unsigned long clone_flags,
                        p->signal->tty = current->signal->tty;
                        p->signal->pgrp = process_group(current);
                        set_signal_session(p->signal, process_session(current));
-                       attach_pid(p, PIDTYPE_PGID, process_group(p));
-                       attach_pid(p, PIDTYPE_SID, process_session(p));
+                       attach_pid(p, PIDTYPE_PGID, task_pgrp(current));
+                       attach_pid(p, PIDTYPE_SID, task_session(current));
 
                        list_add_tail_rcu(&p->tasks, &init_task.tasks);
                        __get_cpu_var(process_counts)++;
                }
-               attach_pid(p, PIDTYPE_PID, p->pid);
+               attach_pid(p, PIDTYPE_PID, pid);
                nr_threads++;
        }
 
@@ -1265,7 +1275,7 @@ static struct task_struct *copy_process(unsigned long clone_flags,
        return p;
 
 bad_fork_cleanup_namespaces:
-       put_and_finalize_nsproxy(p->nsproxy);
+       exit_task_namespaces(p);
 bad_fork_cleanup_keys:
        exit_keys(p);
 bad_fork_cleanup_mm:
@@ -1313,12 +1323,13 @@ noinline struct pt_regs * __devinit __attribute__((weak)) idle_regs(struct pt_re
        return regs;
 }
 
-struct task_struct * __devinit fork_idle(int cpu)
+struct task_struct * __cpuinit fork_idle(int cpu)
 {
        struct task_struct *task;
        struct pt_regs regs;
 
-       task = copy_process(CLONE_VM, 0, idle_regs(&regs), 0, NULL, NULL, 0);
+       task = copy_process(CLONE_VM, 0, idle_regs(&regs), 0, NULL, NULL,
+                               &init_struct_pid);
        if (!IS_ERR(task))
                init_idle(task, cpu);
 
@@ -1368,7 +1379,7 @@ long do_fork(unsigned long clone_flags,
                        clone_flags |= CLONE_PTRACE;
        }
 
-       p = copy_process(clone_flags, stack_start, regs, stack_size, parent_tidptr, child_tidptr, nr);
+       p = copy_process(clone_flags, stack_start, regs, stack_size, parent_tidptr, child_tidptr, pid);
        /*
         * Do this prior waking up the new thread - the thread pointer
         * might get invalid after that point, if the thread exits quickly.
@@ -1400,7 +1411,9 @@ long do_fork(unsigned long clone_flags,
                }
 
                if (clone_flags & CLONE_VFORK) {
+                       freezer_do_not_count();
                        wait_for_completion(&vfork);
+                       freezer_count();
                        if (unlikely (current->ptrace & PT_TRACE_VFORK_DONE)) {
                                current->ptrace_message = nr;
                                ptrace_notify ((PTRACE_EVENT_VFORK_DONE << 8) | SIGTRAP);
@@ -1417,13 +1430,13 @@ long do_fork(unsigned long clone_flags,
 #define ARCH_MIN_MMSTRUCT_ALIGN 0
 #endif
 
-static void sighand_ctor(void *data, struct kmem_cache *cachep, unsigned long flags)
+static void sighand_ctor(void *data, struct kmem_cache *cachep,
+                       unsigned long flags)
 {
        struct sighand_struct *sighand = data;
 
-       if ((flags & (SLAB_CTOR_VERIFY | SLAB_CTOR_CONSTRUCTOR)) ==
-                                       SLAB_CTOR_CONSTRUCTOR)
-               spin_lock_init(&sighand->siglock);
+       spin_lock_init(&sighand->siglock);
+       INIT_LIST_HEAD(&sighand->signalfd_list);
 }
 
 void __init proc_caches_init(void)
@@ -1449,7 +1462,6 @@ void __init proc_caches_init(void)
                        SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
 }
 
-
 /*
  * Check constraints on flags passed to the unshare system call and
  * force unsharing of additional process context as appropriate.
@@ -1513,26 +1525,6 @@ static int unshare_fs(unsigned long unshare_flags, struct fs_struct **new_fsp)
 }
 
 /*
- * Unshare the mnt_namespace structure if it is being shared
- */
-static int unshare_mnt_namespace(unsigned long unshare_flags,
-               struct mnt_namespace **new_nsp, struct fs_struct *new_fs)
-{
-       struct mnt_namespace *ns = current->nsproxy->mnt_ns;
-
-       if ((unshare_flags & CLONE_NEWNS) && ns) {
-               if (!capable(CAP_SYS_ADMIN))
-                       return -EPERM;
-
-               *new_nsp = dup_mnt_ns(current, new_fs ? new_fs : current->fs);
-               if (!*new_nsp)
-                       return -ENOMEM;
-       }
-
-       return 0;
-}
-
-/*
  * Unsharing of sighand is not supported yet
  */
 static int unshare_sighand(unsigned long unshare_flags, struct sighand_struct **new_sighp)
@@ -1590,16 +1582,6 @@ static int unshare_semundo(unsigned long unshare_flags, struct sem_undo_list **n
        return 0;
 }
 
-#ifndef CONFIG_IPC_NS
-static inline int unshare_ipcs(unsigned long flags, struct ipc_namespace **ns)
-{
-       if (flags & CLONE_NEWIPC)
-               return -EINVAL;
-
-       return 0;
-}
-#endif
-
 /*
  * unshare allows a process to 'unshare' part of the process
  * context which was originally shared using clone.  copy_*
@@ -1612,14 +1594,11 @@ asmlinkage long sys_unshare(unsigned long unshare_flags)
 {
        int err = 0;
        struct fs_struct *fs, *new_fs = NULL;
-       struct mnt_namespace *ns, *new_ns = NULL;
        struct sighand_struct *new_sigh = NULL;
        struct mm_struct *mm, *new_mm = NULL, *active_mm = NULL;
        struct files_struct *fd, *new_fd = NULL;
        struct sem_undo_list *new_ulist = NULL;
        struct nsproxy *new_nsproxy = NULL, *old_nsproxy = NULL;
-       struct uts_namespace *uts, *new_uts = NULL;
-       struct ipc_namespace *ipc, *new_ipc = NULL;
 
        check_unshare_flags(&unshare_flags);
 
@@ -1627,43 +1606,31 @@ asmlinkage long sys_unshare(unsigned long unshare_flags)
        err = -EINVAL;
        if (unshare_flags & ~(CLONE_THREAD|CLONE_FS|CLONE_NEWNS|CLONE_SIGHAND|
                                CLONE_VM|CLONE_FILES|CLONE_SYSVSEM|
-                               CLONE_NEWUTS|CLONE_NEWIPC))
+                               CLONE_NEWUTS|CLONE_NEWIPC|CLONE_NEWUSER))
                goto bad_unshare_out;
 
        if ((err = unshare_thread(unshare_flags)))
                goto bad_unshare_out;
        if ((err = unshare_fs(unshare_flags, &new_fs)))
                goto bad_unshare_cleanup_thread;
-       if ((err = unshare_mnt_namespace(unshare_flags, &new_ns, new_fs)))
-               goto bad_unshare_cleanup_fs;
        if ((err = unshare_sighand(unshare_flags, &new_sigh)))
-               goto bad_unshare_cleanup_ns;
+               goto bad_unshare_cleanup_fs;
        if ((err = unshare_vm(unshare_flags, &new_mm)))
                goto bad_unshare_cleanup_sigh;
        if ((err = unshare_fd(unshare_flags, &new_fd)))
                goto bad_unshare_cleanup_vm;
        if ((err = unshare_semundo(unshare_flags, &new_ulist)))
                goto bad_unshare_cleanup_fd;
-       if ((err = unshare_utsname(unshare_flags, &new_uts)))
+       if ((err = unshare_nsproxy_namespaces(unshare_flags, &new_nsproxy,
+                       new_fs)))
                goto bad_unshare_cleanup_semundo;
-       if ((err = unshare_ipcs(unshare_flags, &new_ipc)))
-               goto bad_unshare_cleanup_uts;
-
-       if (new_ns || new_uts || new_ipc) {
-               old_nsproxy = current->nsproxy;
-               new_nsproxy = dup_namespaces(old_nsproxy);
-               if (!new_nsproxy) {
-                       err = -ENOMEM;
-                       goto bad_unshare_cleanup_ipc;
-               }
-       }
 
-       if (new_fs || new_ns || new_mm || new_fd || new_ulist ||
-                               new_uts || new_ipc) {
+       if (new_fs ||  new_mm || new_fd || new_ulist || new_nsproxy) {
 
                task_lock(current);
 
                if (new_nsproxy) {
+                       old_nsproxy = current->nsproxy;
                        current->nsproxy = new_nsproxy;
                        new_nsproxy = old_nsproxy;
                }
@@ -1674,12 +1641,6 @@ asmlinkage long sys_unshare(unsigned long unshare_flags)
                        new_fs = fs;
                }
 
-               if (new_ns) {
-                       ns = current->nsproxy->mnt_ns;
-                       current->nsproxy->mnt_ns = new_ns;
-                       new_ns = ns;
-               }
-
                if (new_mm) {
                        mm = current->mm;
                        active_mm = current->active_mm;
@@ -1695,31 +1656,11 @@ asmlinkage long sys_unshare(unsigned long unshare_flags)
                        new_fd = fd;
                }
 
-               if (new_uts) {
-                       uts = current->nsproxy->uts_ns;
-                       current->nsproxy->uts_ns = new_uts;
-                       new_uts = uts;
-               }
-
-               if (new_ipc) {
-                       ipc = current->nsproxy->ipc_ns;
-                       current->nsproxy->ipc_ns = new_ipc;
-                       new_ipc = ipc;
-               }
-
                task_unlock(current);
        }
 
        if (new_nsproxy)
-               put_and_finalize_nsproxy(new_nsproxy);
-
-bad_unshare_cleanup_ipc:
-       if (new_ipc)
-               put_ipc_ns(new_ipc);
-
-bad_unshare_cleanup_uts:
-       if (new_uts)
-               put_uts_ns(new_uts);
+               put_nsproxy(new_nsproxy);
 
 bad_unshare_cleanup_semundo:
 bad_unshare_cleanup_fd:
@@ -1735,10 +1676,6 @@ bad_unshare_cleanup_sigh:
                if (atomic_dec_and_test(&new_sigh->count))
                        kmem_cache_free(sighand_cachep, new_sigh);
 
-bad_unshare_cleanup_ns:
-       if (new_ns)
-               put_mnt_ns(new_ns);
-
 bad_unshare_cleanup_fs:
        if (new_fs)
                put_fs_struct(new_fs);