random: use unlocked_ioctl
[safe/jmp/linux-2.6] / kernel / sys.c
index eecf845..e423d0d 100644 (file)
@@ -4,7 +4,6 @@
  *  Copyright (C) 1991, 1992  Linus Torvalds
  */
 
-#include <linux/config.h>
 #include <linux/module.h>
 #include <linux/mm.h>
 #include <linux/utsname.h>
 #include <linux/notifier.h>
 #include <linux/reboot.h>
 #include <linux/prctl.h>
-#include <linux/init.h>
 #include <linux/highuid.h>
 #include <linux/fs.h>
+#include <linux/resource.h>
 #include <linux/kernel.h>
 #include <linux/kexec.h>
 #include <linux/workqueue.h>
+#include <linux/capability.h>
 #include <linux/device.h>
 #include <linux/key.h>
 #include <linux/times.h>
 #include <linux/tty.h>
 #include <linux/signal.h>
 #include <linux/cn_proc.h>
+#include <linux/getcpu.h>
+#include <linux/task_io_accounting_ops.h>
+#include <linux/seccomp.h>
+#include <linux/cpu.h>
 
 #include <linux/compat.h>
 #include <linux/syscalls.h>
 #include <linux/kprobes.h>
+#include <linux/user_namespace.h>
 
 #include <asm/uaccess.h>
 #include <asm/io.h>
 #ifndef GET_FPEXC_CTL
 # define GET_FPEXC_CTL(a,b)    (-EINVAL)
 #endif
+#ifndef GET_ENDIAN
+# define GET_ENDIAN(a,b)       (-EINVAL)
+#endif
+#ifndef SET_ENDIAN
+# define SET_ENDIAN(a,b)       (-EINVAL)
+#endif
+#ifndef GET_TSC_CTL
+# define GET_TSC_CTL(a)                (-EINVAL)
+#endif
+#ifndef SET_TSC_CTL
+# define SET_TSC_CTL(a)                (-EINVAL)
+#endif
 
 /*
  * this is where the system-wide overflow UID and GID are defined, for
@@ -86,142 +103,14 @@ EXPORT_SYMBOL(fs_overflowgid);
  */
 
 int C_A_D = 1;
-int cad_pid = 1;
+struct pid *cad_pid;
+EXPORT_SYMBOL(cad_pid);
 
 /*
- *     Notifier list for kernel code which wants to be called
- *     at shutdown. This is used to stop any idling DMA operations
- *     and the like. 
- */
-
-static struct notifier_block *reboot_notifier_list;
-static DEFINE_RWLOCK(notifier_lock);
-
-/**
- *     notifier_chain_register - Add notifier to a notifier chain
- *     @list: Pointer to root list pointer
- *     @n: New entry in notifier chain
- *
- *     Adds a notifier to a notifier chain.
- *
- *     Currently always returns zero.
+ * If set, this is used for preparing the system to power off.
  */
-int notifier_chain_register(struct notifier_block **list, struct notifier_block *n)
-{
-       write_lock(&notifier_lock);
-       while(*list)
-       {
-               if(n->priority > (*list)->priority)
-                       break;
-               list= &((*list)->next);
-       }
-       n->next = *list;
-       *list=n;
-       write_unlock(&notifier_lock);
-       return 0;
-}
-
-EXPORT_SYMBOL(notifier_chain_register);
-
-/**
- *     notifier_chain_unregister - Remove notifier from a notifier chain
- *     @nl: Pointer to root list pointer
- *     @n: New entry in notifier chain
- *
- *     Removes a notifier from a notifier chain.
- *
- *     Returns zero on success, or %-ENOENT on failure.
- */
-int notifier_chain_unregister(struct notifier_block **nl, struct notifier_block *n)
-{
-       write_lock(&notifier_lock);
-       while((*nl)!=NULL)
-       {
-               if((*nl)==n)
-               {
-                       *nl=n->next;
-                       write_unlock(&notifier_lock);
-                       return 0;
-               }
-               nl=&((*nl)->next);
-       }
-       write_unlock(&notifier_lock);
-       return -ENOENT;
-}
-
-EXPORT_SYMBOL(notifier_chain_unregister);
-
-/**
- *     notifier_call_chain - Call functions in a notifier chain
- *     @n: Pointer to root pointer of notifier chain
- *     @val: Value passed unmodified to notifier function
- *     @v: Pointer passed unmodified to notifier function
- *
- *     Calls each function in a notifier chain in turn.
- *
- *     If the return value of the notifier can be and'd
- *     with %NOTIFY_STOP_MASK, then notifier_call_chain
- *     will return immediately, with the return value of
- *     the notifier function which halted execution.
- *     Otherwise, the return value is the return value
- *     of the last notifier function called.
- */
-int __kprobes notifier_call_chain(struct notifier_block **n, unsigned long val, void *v)
-{
-       int ret=NOTIFY_DONE;
-       struct notifier_block *nb = *n;
-
-       while(nb)
-       {
-               ret=nb->notifier_call(nb,val,v);
-               if(ret&NOTIFY_STOP_MASK)
-               {
-                       return ret;
-               }
-               nb=nb->next;
-       }
-       return ret;
-}
-
-EXPORT_SYMBOL(notifier_call_chain);
-
-/**
- *     register_reboot_notifier - Register function to be called at reboot time
- *     @nb: Info about notifier function to be called
- *
- *     Registers a function with the list of functions
- *     to be called at reboot time.
- *
- *     Currently always returns zero, as notifier_chain_register
- *     always returns zero.
- */
-int register_reboot_notifier(struct notifier_block * nb)
-{
-       return notifier_chain_register(&reboot_notifier_list, nb);
-}
 
-EXPORT_SYMBOL(register_reboot_notifier);
-
-/**
- *     unregister_reboot_notifier - Unregister previously registered reboot notifier
- *     @nb: Hook to be unregistered
- *
- *     Unregisters a previously registered reboot
- *     notifier function.
- *
- *     Returns zero on success, or %-ENOENT on failure.
- */
-int unregister_reboot_notifier(struct notifier_block * nb)
-{
-       return notifier_chain_unregister(&reboot_notifier_list, nb);
-}
-
-EXPORT_SYMBOL(unregister_reboot_notifier);
+void (*pm_power_off_prepare)(void);
 
 static int set_one_prio(struct task_struct *p, int niceval, int error)
 {
@@ -253,8 +142,9 @@ asmlinkage long sys_setpriority(int which, int who, int niceval)
        struct task_struct *g, *p;
        struct user_struct *user;
        int error = -EINVAL;
+       struct pid *pgrp;
 
-       if (which > 2 || which < 0)
+       if (which > PRIO_USER || which < PRIO_PROCESS)
                goto out;
 
        /* normalize: avoid signed division (rounding problems) */
@@ -267,18 +157,21 @@ asmlinkage long sys_setpriority(int which, int who, int niceval)
        read_lock(&tasklist_lock);
        switch (which) {
                case PRIO_PROCESS:
-                       if (!who)
-                               who = current->pid;
-                       p = find_task_by_pid(who);
+                       if (who)
+                               p = find_task_by_vpid(who);
+                       else
+                               p = current;
                        if (p)
                                error = set_one_prio(p, niceval, error);
                        break;
                case PRIO_PGRP:
-                       if (!who)
-                               who = process_group(current);
-                       do_each_task_pid(who, PIDTYPE_PGID, p) {
+                       if (who)
+                               pgrp = find_vpid(who);
+                       else
+                               pgrp = task_pgrp(current);
+                       do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
                                error = set_one_prio(p, niceval, error);
-                       } while_each_task_pid(who, PIDTYPE_PGID, p);
+                       } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
                        break;
                case PRIO_USER:
                        user = current->user;
@@ -313,16 +206,18 @@ asmlinkage long sys_getpriority(int which, int who)
        struct task_struct *g, *p;
        struct user_struct *user;
        long niceval, retval = -ESRCH;
+       struct pid *pgrp;
 
-       if (which > 2 || which < 0)
+       if (which > PRIO_USER || which < PRIO_PROCESS)
                return -EINVAL;
 
        read_lock(&tasklist_lock);
        switch (which) {
                case PRIO_PROCESS:
-                       if (!who)
-                               who = current->pid;
-                       p = find_task_by_pid(who);
+                       if (who)
+                               p = find_task_by_vpid(who);
+                       else
+                               p = current;
                        if (p) {
                                niceval = 20 - task_nice(p);
                                if (niceval > retval)
@@ -330,13 +225,15 @@ asmlinkage long sys_getpriority(int which, int who)
                        }
                        break;
                case PRIO_PGRP:
-                       if (!who)
-                               who = process_group(current);
-                       do_each_task_pid(who, PIDTYPE_PGID, p) {
+                       if (who)
+                               pgrp = find_vpid(who);
+                       else
+                               pgrp = task_pgrp(current);
+                       do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
                                niceval = 20 - task_nice(p);
                                if (niceval > retval)
                                        retval = niceval;
-                       } while_each_task_pid(who, PIDTYPE_PGID, p);
+                       } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
                        break;
                case PRIO_USER:
                        user = current->user;
@@ -377,11 +274,12 @@ void emergency_restart(void)
 }
 EXPORT_SYMBOL_GPL(emergency_restart);
 
-void kernel_restart_prepare(char *cmd)
+static void kernel_restart_prepare(char *cmd)
 {
-       notifier_call_chain(&reboot_notifier_list, SYS_RESTART, cmd);
+       blocking_notifier_call_chain(&reboot_notifier_list, SYS_RESTART, cmd);
        system_state = SYSTEM_RESTART;
        device_shutdown();
+       sysdev_shutdown();
 }
 
 /**
@@ -395,12 +293,10 @@ void kernel_restart_prepare(char *cmd)
 void kernel_restart(char *cmd)
 {
        kernel_restart_prepare(cmd);
-       if (!cmd) {
+       if (!cmd)
                printk(KERN_EMERG "Restarting system.\n");
-       } else {
+       else
                printk(KERN_EMERG "Restarting system with command '%s'.\n", cmd);
-       }
-       printk(".\n");
        machine_restart(cmd);
 }
 EXPORT_SYMBOL_GPL(kernel_restart);
@@ -411,39 +307,40 @@ EXPORT_SYMBOL_GPL(kernel_restart);
  *     Move into place and start executing a preloaded standalone
  *     executable.  If nothing was preloaded return an error.
  */
-void kernel_kexec(void)
+static void kernel_kexec(void)
 {
 #ifdef CONFIG_KEXEC
        struct kimage *image;
-       image = xchg(&kexec_image, 0);
-       if (!image) {
+       image = xchg(&kexec_image, NULL);
+       if (!image)
                return;
-       }
        kernel_restart_prepare(NULL);
        printk(KERN_EMERG "Starting new kernel\n");
        machine_shutdown();
        machine_kexec(image);
 #endif
 }
-EXPORT_SYMBOL_GPL(kernel_kexec);
 
+static void kernel_shutdown_prepare(enum system_states state)
+{
+       blocking_notifier_call_chain(&reboot_notifier_list,
+               (state == SYSTEM_HALT)?SYS_HALT:SYS_POWER_OFF, NULL);
+       system_state = state;
+       device_shutdown();
+}
 /**
  *     kernel_halt - halt the system
  *
  *     Shutdown everything and perform a clean system halt.
  */
-void kernel_halt_prepare(void)
-{
-       notifier_call_chain(&reboot_notifier_list, SYS_HALT, NULL);
-       system_state = SYSTEM_HALT;
-       device_shutdown();
-}
 void kernel_halt(void)
 {
-       kernel_halt_prepare();
+       kernel_shutdown_prepare(SYSTEM_HALT);
+       sysdev_shutdown();
        printk(KERN_EMERG "System halted.\n");
        machine_halt();
 }
+
 EXPORT_SYMBOL_GPL(kernel_halt);
 
 /**
@@ -451,20 +348,17 @@ EXPORT_SYMBOL_GPL(kernel_halt);
  *
  *     Shutdown everything and perform a clean system power_off.
  */
-void kernel_power_off_prepare(void)
-{
-       notifier_call_chain(&reboot_notifier_list, SYS_POWER_OFF, NULL);
-       system_state = SYSTEM_POWER_OFF;
-       device_shutdown();
-}
 void kernel_power_off(void)
 {
-       kernel_power_off_prepare();
+       kernel_shutdown_prepare(SYSTEM_POWER_OFF);
+       if (pm_power_off_prepare)
+               pm_power_off_prepare();
+       disable_nonboot_cpus();
+       sysdev_shutdown();
        printk(KERN_EMERG "Power down.\n");
        machine_power_off();
 }
 EXPORT_SYMBOL_GPL(kernel_power_off);
-
 /*
  * Reboot system call: for obvious reasons only root may call it,
  * and even root needs to set up some magic numbers in the registers
@@ -489,6 +383,12 @@ asmlinkage long sys_reboot(int magic1, int magic2, unsigned int cmd, void __user
                        magic2 != LINUX_REBOOT_MAGIC2C))
                return -EINVAL;
 
+       /* Instead of trying to make the power_off code look like
+        * halt when pm_power_off is not set do it the easy way.
+        */
+       if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !pm_power_off)
+               cmd = LINUX_REBOOT_CMD_HALT;
+
        lock_kernel();
        switch (cmd) {
        case LINUX_REBOOT_CMD_RESTART:
@@ -530,10 +430,10 @@ asmlinkage long sys_reboot(int magic1, int magic2, unsigned int cmd, void __user
                unlock_kernel();
                return -EINVAL;
 
-#ifdef CONFIG_SOFTWARE_SUSPEND
+#ifdef CONFIG_HIBERNATION
        case LINUX_REBOOT_CMD_SW_SUSPEND:
                {
-                       int ret = software_suspend();
+                       int ret = hibernate();
                        unlock_kernel();
                        return ret;
                }
@@ -547,7 +447,7 @@ asmlinkage long sys_reboot(int magic1, int magic2, unsigned int cmd, void __user
        return 0;
 }
 
-static void deferred_cad(void *dummy)
+static void deferred_cad(struct work_struct *dummy)
 {
        kernel_restart(NULL);
 }
@@ -559,15 +459,14 @@ static void deferred_cad(void *dummy)
  */
 void ctrl_alt_del(void)
 {
-       static DECLARE_WORK(cad_work, deferred_cad, NULL);
+       static DECLARE_WORK(cad_work, deferred_cad);
 
        if (C_A_D)
                schedule_work(&cad_work);
        else
-               kill_proc(cad_pid, SIGINT, 1);
+               kill_cad_pid(SIGINT, 1);
 }
        
-
 /*
  * Unprivileged users may change the real gid to the effective gid
  * or vice versa.  (BSD-style)
@@ -612,13 +511,11 @@ asmlinkage long sys_setregid(gid_t rgid, gid_t egid)
                    (current->sgid == egid) ||
                    capable(CAP_SETGID))
                        new_egid = egid;
-               else {
+               else
                        return -EPERM;
-               }
        }
-       if (new_egid != old_egid)
-       {
-               current->mm->dumpable = suid_dumpable;
+       if (new_egid != old_egid) {
+               set_dumpable(current->mm, suid_dumpable);
                smp_wmb();
        }
        if (rgid != (gid_t) -1 ||
@@ -646,20 +543,15 @@ asmlinkage long sys_setgid(gid_t gid)
        if (retval)
                return retval;
 
-       if (capable(CAP_SETGID))
-       {
-               if(old_egid != gid)
-               {
-                       current->mm->dumpable = suid_dumpable;
+       if (capable(CAP_SETGID)) {
+               if (old_egid != gid) {
+                       set_dumpable(current->mm, suid_dumpable);
                        smp_wmb();
                }
                current->gid = current->egid = current->sgid = current->fsgid = gid;
-       }
-       else if ((gid == current->gid) || (gid == current->sgid))
-       {
-               if(old_egid != gid)
-               {
-                       current->mm->dumpable = suid_dumpable;
+       } else if ((gid == current->gid) || (gid == current->sgid)) {
+               if (old_egid != gid) {
+                       set_dumpable(current->mm, suid_dumpable);
                        smp_wmb();
                }
                current->egid = current->fsgid = gid;
@@ -676,22 +568,21 @@ static int set_user(uid_t new_ruid, int dumpclear)
 {
        struct user_struct *new_user;
 
-       new_user = alloc_uid(new_ruid);
+       new_user = alloc_uid(current->nsproxy->user_ns, new_ruid);
        if (!new_user)
                return -EAGAIN;
 
        if (atomic_read(&new_user->processes) >=
                                current->signal->rlim[RLIMIT_NPROC].rlim_cur &&
-                       new_user != &root_user) {
+                       new_user != current->nsproxy->user_ns->root_user) {
                free_uid(new_user);
                return -EAGAIN;
        }
 
        switch_uid(new_user);
 
-       if(dumpclear)
-       {
-               current->mm->dumpable = suid_dumpable;
+       if (dumpclear) {
+               set_dumpable(current->mm, suid_dumpable);
                smp_wmb();
        }
        current->uid = new_ruid;
@@ -746,9 +637,8 @@ asmlinkage long sys_setreuid(uid_t ruid, uid_t euid)
        if (new_ruid != old_ruid && set_user(new_ruid, new_euid != old_euid) < 0)
                return -EAGAIN;
 
-       if (new_euid != old_euid)
-       {
-               current->mm->dumpable = suid_dumpable;
+       if (new_euid != old_euid) {
+               set_dumpable(current->mm, suid_dumpable);
                smp_wmb();
        }
        current->fsuid = current->euid = new_euid;
@@ -779,14 +669,14 @@ asmlinkage long sys_setreuid(uid_t ruid, uid_t euid)
 asmlinkage long sys_setuid(uid_t uid)
 {
        int old_euid = current->euid;
-       int old_ruid, old_suid, new_ruid, new_suid;
+       int old_ruid, old_suid, new_suid;
        int retval;
 
        retval = security_task_setuid(uid, (uid_t)-1, (uid_t)-1, LSM_SETID_ID);
        if (retval)
                return retval;
 
-       old_ruid = new_ruid = current->uid;
+       old_ruid = current->uid;
        old_suid = current->suid;
        new_suid = old_suid;
        
@@ -797,9 +687,8 @@ asmlinkage long sys_setuid(uid_t uid)
        } else if ((uid != current->uid) && (uid != new_suid))
                return -EPERM;
 
-       if (old_euid != uid)
-       {
-               current->mm->dumpable = suid_dumpable;
+       if (old_euid != uid) {
+               set_dumpable(current->mm, suid_dumpable);
                smp_wmb();
        }
        current->fsuid = current->euid = uid;
@@ -843,9 +732,8 @@ asmlinkage long sys_setresuid(uid_t ruid, uid_t euid, uid_t suid)
                        return -EAGAIN;
        }
        if (euid != (uid_t) -1) {
-               if (euid != current->euid)
-               {
-                       current->mm->dumpable = suid_dumpable;
+               if (euid != current->euid) {
+                       set_dumpable(current->mm, suid_dumpable);
                        smp_wmb();
                }
                current->euid = euid;
@@ -894,9 +782,8 @@ asmlinkage long sys_setresgid(gid_t rgid, gid_t egid, gid_t sgid)
                        return -EPERM;
        }
        if (egid != (gid_t) -1) {
-               if (egid != current->egid)
-               {
-                       current->mm->dumpable = suid_dumpable;
+               if (egid != current->egid) {
+                       set_dumpable(current->mm, suid_dumpable);
                        smp_wmb();
                }
                current->egid = egid;
@@ -940,11 +827,9 @@ asmlinkage long sys_setfsuid(uid_t uid)
 
        if (uid == current->uid || uid == current->euid ||
            uid == current->suid || uid == current->fsuid || 
-           capable(CAP_SETUID))
-       {
-               if (uid != old_fsuid)
-               {
-                       current->mm->dumpable = suid_dumpable;
+           capable(CAP_SETUID)) {
+               if (uid != old_fsuid) {
+                       set_dumpable(current->mm, suid_dumpable);
                        smp_wmb();
                }
                current->fsuid = uid;
@@ -959,7 +844,7 @@ asmlinkage long sys_setfsuid(uid_t uid)
 }
 
 /*
- * Samma pÃ¥ svenska..
+ * Samma pÃ¥ svenska..
  */
 asmlinkage long sys_setfsgid(gid_t gid)
 {
@@ -971,11 +856,9 @@ asmlinkage long sys_setfsgid(gid_t gid)
 
        if (gid == current->gid || gid == current->egid ||
            gid == current->sgid || gid == current->fsgid || 
-           capable(CAP_SETGID))
-       {
-               if (gid != old_fsgid)
-               {
-                       current->mm->dumpable = suid_dumpable;
+           capable(CAP_SETGID)) {
+               if (gid != old_fsgid) {
+                       set_dumpable(current->mm, suid_dumpable);
                        smp_wmb();
                }
                current->fsgid = gid;
@@ -995,69 +878,24 @@ asmlinkage long sys_times(struct tms __user * tbuf)
         */
        if (tbuf) {
                struct tms tmp;
+               struct task_struct *tsk = current;
+               struct task_struct *t;
                cputime_t utime, stime, cutime, cstime;
 
-#ifdef CONFIG_SMP
-               if (thread_group_empty(current)) {
-                       /*
-                        * Single thread case without the use of any locks.
-                        *
-                        * We may race with release_task if two threads are
-                        * executing. However, release task first adds up the
-                        * counters (__exit_signal) before  removing the task
-                        * from the process tasklist (__unhash_process).
-                        * __exit_signal also acquires and releases the
-                        * siglock which results in the proper memory ordering
-                        * so that the list modifications are always visible
-                        * after the counters have been updated.
-                        *
-                        * If the counters have been updated by the second thread
-                        * but the thread has not yet been removed from the list
-                        * then the other branch will be executing which will
-                        * block on tasklist_lock until the exit handling of the
-                        * other task is finished.
-                        *
-                        * This also implies that the sighand->siglock cannot
-                        * be held by another processor. So we can also
-                        * skip acquiring that lock.
-                        */
-                       utime = cputime_add(current->signal->utime, current->utime);
-                       stime = cputime_add(current->signal->utime, current->stime);
-                       cutime = current->signal->cutime;
-                       cstime = current->signal->cstime;
-               } else
-#endif
-               {
+               spin_lock_irq(&tsk->sighand->siglock);
+               utime = tsk->signal->utime;
+               stime = tsk->signal->stime;
+               t = tsk;
+               do {
+                       utime = cputime_add(utime, t->utime);
+                       stime = cputime_add(stime, t->stime);
+                       t = next_thread(t);
+               } while (t != tsk);
 
-                       /* Process with multiple threads */
-                       struct task_struct *tsk = current;
-                       struct task_struct *t;
+               cutime = tsk->signal->cutime;
+               cstime = tsk->signal->cstime;
+               spin_unlock_irq(&tsk->sighand->siglock);
 
-                       read_lock(&tasklist_lock);
-                       utime = tsk->signal->utime;
-                       stime = tsk->signal->stime;
-                       t = tsk;
-                       do {
-                               utime = cputime_add(utime, t->utime);
-                               stime = cputime_add(stime, t->stime);
-                               t = next_thread(t);
-                       } while (t != tsk);
-
-                       /*
-                        * While we have tasklist_lock read-locked, no dying thread
-                        * can be updating current->signal->[us]time.  Instead,
-                        * we got their counts included in the live thread loop.
-                        * However, another thread can come in right now and
-                        * do a wait call that updates current->signal->c[us]time.
-                        * To make sure we always see that pair updated atomically,
-                        * we take the siglock around fetching them.
-                        */
-                       spin_lock_irq(&tsk->sighand->siglock);
-                       cutime = tsk->signal->cutime;
-                       cstime = tsk->signal->cstime;
-                       spin_unlock_irq(&tsk->sighand->siglock);
-                       read_unlock(&tasklist_lock);
-               }
                tmp.tms_utime = cputime_to_clock_t(utime);
                tmp.tms_stime = cputime_to_clock_t(stime);
                tmp.tms_cutime = cputime_to_clock_t(cutime);
@@ -1080,14 +918,15 @@ asmlinkage long sys_times(struct tms __user * tbuf)
  * Auch. Had to add the 'did_exec' flag to conform completely to POSIX.
  * LBT 04.03.94
  */
-
 asmlinkage long sys_setpgid(pid_t pid, pid_t pgid)
 {
        struct task_struct *p;
-       int err = -EINVAL;
+       struct task_struct *group_leader = current->group_leader;
+       struct pid *pgrp;
+       int err;
 
        if (!pid)
-               pid = current->pid;
+               pid = task_pid_vnr(group_leader);
        if (!pgid)
                pgid = pid;
        if (pgid < 0)
@@ -1099,7 +938,7 @@ asmlinkage long sys_setpgid(pid_t pid, pid_t pgid)
        write_lock_irq(&tasklist_lock);
 
        err = -ESRCH;
-       p = find_task_by_pid(pid);
+       p = find_task_by_vpid(pid);
        if (!p)
                goto out;
 
@@ -1107,16 +946,16 @@ asmlinkage long sys_setpgid(pid_t pid, pid_t pgid)
        if (!thread_group_leader(p))
                goto out;
 
-       if (p->parent == current || p->real_parent == current) {
+       if (same_thread_group(p->real_parent, group_leader)) {
                err = -EPERM;
-               if (p->signal->session != current->signal->session)
+               if (task_session(p) != task_session(group_leader))
                        goto out;
                err = -EACCES;
                if (p->did_exec)
                        goto out;
        } else {
                err = -ESRCH;
-               if (p != current)
+               if (p != group_leader)
                        goto out;
        }
 
@@ -1124,25 +963,24 @@ asmlinkage long sys_setpgid(pid_t pid, pid_t pgid)
        if (p->signal->leader)
                goto out;
 
+       pgrp = task_pid(p);
        if (pgid != pid) {
-               struct task_struct *p;
+               struct task_struct *g;
 
-               do_each_task_pid(pgid, PIDTYPE_PGID, p) {
-                       if (p->signal->session == current->signal->session)
-                               goto ok_pgid;
-               } while_each_task_pid(pgid, PIDTYPE_PGID, p);
-               goto out;
+               pgrp = find_vpid(pgid);
+               g = pid_task(pgrp, PIDTYPE_PGID);
+               if (!g || task_session(g) != task_session(group_leader))
+                       goto out;
        }
 
-ok_pgid:
        err = security_task_setpgid(p, pgid);
        if (err)
                goto out;
 
-       if (process_group(p) != pgid) {
+       if (task_pgrp(p) != pgrp) {
                detach_pid(p, PIDTYPE_PGID);
-               p->signal->pgrp = pgid;
-               attach_pid(p, PIDTYPE_PGID, pgid);
+               attach_pid(p, PIDTYPE_PGID, pgrp);
+               set_task_pgrp(p, pid_nr(pgrp));
        }
 
        err = 0;
@@ -1154,20 +992,19 @@ out:
 
 asmlinkage long sys_getpgid(pid_t pid)
 {
-       if (!pid) {
-               return process_group(current);
-       else {
+       if (!pid)
+               return task_pgrp_vnr(current);
+       else {
                int retval;
                struct task_struct *p;
 
                read_lock(&tasklist_lock);
-               p = find_task_by_pid(pid);
-
+               p = find_task_by_vpid(pid);
                retval = -ESRCH;
                if (p) {
                        retval = security_task_getpgid(p);
                        if (!retval)
-                               retval = process_group(p);
+                               retval = task_pgrp_vnr(p);
                }
                read_unlock(&tasklist_lock);
                return retval;
@@ -1179,56 +1016,60 @@ asmlinkage long sys_getpgid(pid_t pid)
 asmlinkage long sys_getpgrp(void)
 {
        /* SMP - assuming writes are word atomic this is fine */
-       return process_group(current);
+       return task_pgrp_vnr(current);
 }
 
 #endif
 
 asmlinkage long sys_getsid(pid_t pid)
 {
-       if (!pid) {
-               return current->signal->session;
-       else {
+       if (!pid)
+               return task_session_vnr(current);
+       else {
                int retval;
                struct task_struct *p;
 
-               read_lock(&tasklist_lock);
-               p = find_task_by_pid(pid);
-
+               rcu_read_lock();
+               p = find_task_by_vpid(pid);
                retval = -ESRCH;
-               if(p) {
+               if (p) {
                        retval = security_task_getsid(p);
                        if (!retval)
-                               retval = p->signal->session;
+                               retval = task_session_vnr(p);
                }
-               read_unlock(&tasklist_lock);
+               rcu_read_unlock();
                return retval;
        }
 }
 
 asmlinkage long sys_setsid(void)
 {
-       struct pid *pid;
+       struct task_struct *group_leader = current->group_leader;
+       struct pid *sid = task_pid(group_leader);
+       pid_t session = pid_vnr(sid);
        int err = -EPERM;
 
-       if (!thread_group_leader(current))
-               return -EINVAL;
-
-       down(&tty_sem);
        write_lock_irq(&tasklist_lock);
+       /* Fail if I am already a session leader */
+       if (group_leader->signal->leader)
+               goto out;
 
-       pid = find_pid(PIDTYPE_PGID, current->pid);
-       if (pid)
+       /* Fail if a process group id already exists that equals the
+        * proposed session id.
+        */
+       if (pid_task(sid, PIDTYPE_PGID))
                goto out;
 
-       current->signal->leader = 1;
-       __set_special_pids(current->pid, current->pid);
-       current->signal->tty = NULL;
-       current->signal->tty_old_pgrp = 0;
-       err = process_group(current);
+       group_leader->signal->leader = 1;
+       __set_special_pids(sid);
+
+       spin_lock(&group_leader->sighand->siglock);
+       group_leader->signal->tty = NULL;
+       spin_unlock(&group_leader->sighand->siglock);
+
+       err = session;
 out:
        write_unlock_irq(&tasklist_lock);
-       up(&tty_sem);
        return err;
 }
 
@@ -1255,9 +1096,9 @@ struct group_info *groups_alloc(int gidsetsize)
        group_info->nblocks = nblocks;
        atomic_set(&group_info->usage, 1);
 
-       if (gidsetsize <= NGROUPS_SMALL) {
+       if (gidsetsize <= NGROUPS_SMALL)
                group_info->blocks[0] = group_info->small_block;
-       else {
+       else {
                for (i = 0; i < nblocks; i++) {
                        gid_t *b;
                        b = (void *)__get_free_page(GFP_USER);
@@ -1295,16 +1136,16 @@ static int groups_to_user(gid_t __user *grouplist,
     struct group_info *group_info)
 {
        int i;
-       int count = group_info->ngroups;
+       unsigned int count = group_info->ngroups;
 
        for (i = 0; i < group_info->nblocks; i++) {
-               int cp_count = min(NGROUPS_PER_BLOCK, count);
-               int off = i * NGROUPS_PER_BLOCK;
-               int len = cp_count * sizeof(*grouplist);
+               unsigned int cp_count = min(NGROUPS_PER_BLOCK, count);
+               unsigned int len = cp_count * sizeof(*grouplist);
 
-               if (copy_to_user(grouplist+off, group_info->blocks[i], len))
+               if (copy_to_user(grouplist, group_info->blocks[i], len))
                        return -EFAULT;
 
+               grouplist += NGROUPS_PER_BLOCK;
                count -= cp_count;
        }
        return 0;
@@ -1313,18 +1154,18 @@ static int groups_to_user(gid_t __user *grouplist,
 /* fill a group_info from a user-space array - it must be allocated already */
 static int groups_from_user(struct group_info *group_info,
     gid_t __user *grouplist)
- {
+{
        int i;
-       int count = group_info->ngroups;
+       unsigned int count = group_info->ngroups;
 
        for (i = 0; i < group_info->nblocks; i++) {
-               int cp_count = min(NGROUPS_PER_BLOCK, count);
-               int off = i * NGROUPS_PER_BLOCK;
-               int len = cp_count * sizeof(*grouplist);
+               unsigned int cp_count = min(NGROUPS_PER_BLOCK, count);
+               unsigned int len = cp_count * sizeof(*grouplist);
 
-               if (copy_from_user(group_info->blocks[i], grouplist+off, len))
+               if (copy_from_user(group_info->blocks[i], grouplist, len))
                        return -EFAULT;
 
+               grouplist += NGROUPS_PER_BLOCK;
                count -= cp_count;
        }
        return 0;
@@ -1362,7 +1203,7 @@ static void groups_sort(struct group_info *group_info)
 /* a simple bsearch */
 int groups_search(struct group_info *group_info, gid_t grp)
 {
-       int left, right;
+       unsigned int left, right;
 
        if (!group_info)
                return 0;
@@ -1370,7 +1211,7 @@ int groups_search(struct group_info *group_info, gid_t grp)
        left = 0;
        right = group_info->ngroups;
        while (left < right) {
-               int mid = (left+right)/2;
+               unsigned int mid = (left+right)/2;
                int cmp = grp - GROUP_AT(group_info, mid);
                if (cmp > 0)
                        left = mid + 1;
@@ -1420,7 +1261,6 @@ asmlinkage long sys_getgroups(int gidsetsize, gid_t __user *grouplist)
                return -EINVAL;
 
        /* no need to grab task_lock here; it cannot change */
-       get_group_info(current->group_info);
        i = current->group_info->ngroups;
        if (gidsetsize) {
                if (i > gidsetsize) {
@@ -1433,7 +1273,6 @@ asmlinkage long sys_getgroups(int gidsetsize, gid_t __user *grouplist)
                }
        }
 out:
-       put_group_info(current->group_info);
        return i;
 }
 
@@ -1473,11 +1312,8 @@ asmlinkage long sys_setgroups(int gidsetsize, gid_t __user *grouplist)
 int in_group_p(gid_t grp)
 {
        int retval = 1;
-       if (grp != current->fsgid) {
-               get_group_info(current->group_info);
+       if (grp != current->fsgid)
                retval = groups_search(current->group_info, grp);
-               put_group_info(current->group_info);
-       }
        return retval;
 }
 
@@ -1486,11 +1322,8 @@ EXPORT_SYMBOL(in_group_p);
 int in_egroup_p(gid_t grp)
 {
        int retval = 1;
-       if (grp != current->egid) {
-               get_group_info(current->group_info);
+       if (grp != current->egid)
                retval = groups_search(current->group_info, grp);
-               put_group_info(current->group_info);
-       }
        return retval;
 }
 
@@ -1505,7 +1338,7 @@ asmlinkage long sys_newuname(struct new_utsname __user * name)
        int errno = 0;
 
        down_read(&uts_sem);
-       if (copy_to_user(name,&system_utsname,sizeof *name))
+       if (copy_to_user(name, utsname(), sizeof *name))
                errno = -EFAULT;
        up_read(&uts_sem);
        return errno;
@@ -1523,8 +1356,8 @@ asmlinkage long sys_sethostname(char __user *name, int len)
        down_write(&uts_sem);
        errno = -EFAULT;
        if (!copy_from_user(tmp, name, len)) {
-               memcpy(system_utsname.nodename, tmp, len);
-               system_utsname.nodename[len] = 0;
+               memcpy(utsname()->nodename, tmp, len);
+               utsname()->nodename[len] = 0;
                errno = 0;
        }
        up_write(&uts_sem);
@@ -1540,11 +1373,11 @@ asmlinkage long sys_gethostname(char __user *name, int len)
        if (len < 0)
                return -EINVAL;
        down_read(&uts_sem);
-       i = 1 + strlen(system_utsname.nodename);
+       i = 1 + strlen(utsname()->nodename);
        if (i > len)
                i = len;
        errno = 0;
-       if (copy_to_user(name, system_utsname.nodename, i))
+       if (copy_to_user(name, utsname()->nodename, i))
                errno = -EFAULT;
        up_read(&uts_sem);
        return errno;
@@ -1569,8 +1402,8 @@ asmlinkage long sys_setdomainname(char __user *name, int len)
        down_write(&uts_sem);
        errno = -EFAULT;
        if (!copy_from_user(tmp, name, len)) {
-               memcpy(system_utsname.domainname, tmp, len);
-               system_utsname.domainname[len] = 0;
+               memcpy(utsname()->domainname, tmp, len);
+               utsname()->domainname[len] = 0;
                errno = 0;
        }
        up_write(&uts_sem);
@@ -1605,9 +1438,9 @@ asmlinkage long sys_old_getrlimit(unsigned int resource, struct rlimit __user *r
        task_lock(current->group_leader);
        x = current->signal->rlim[resource];
        task_unlock(current->group_leader);
-       if(x.rlim_cur > 0x7FFFFFFF)
+       if (x.rlim_cur > 0x7FFFFFFF)
                x.rlim_cur = 0x7FFFFFFF;
-       if(x.rlim_max > 0x7FFFFFFF)
+       if (x.rlim_max > 0x7FFFFFFF)
                x.rlim_max = 0x7FFFFFFF;
        return copy_to_user(rlim, &x, sizeof(x))?-EFAULT:0;
 }
@@ -1617,42 +1450,65 @@ asmlinkage long sys_old_getrlimit(unsigned int resource, struct rlimit __user *r
 asmlinkage long sys_setrlimit(unsigned int resource, struct rlimit __user *rlim)
 {
        struct rlimit new_rlim, *old_rlim;
+       unsigned long it_prof_secs;
        int retval;
 
        if (resource >= RLIM_NLIMITS)
                return -EINVAL;
-       if(copy_from_user(&new_rlim, rlim, sizeof(*rlim)))
+       if (copy_from_user(&new_rlim, rlim, sizeof(*rlim)))
                return -EFAULT;
-       if (new_rlim.rlim_cur > new_rlim.rlim_max)
-               return -EINVAL;
+       if (new_rlim.rlim_cur > new_rlim.rlim_max)
+               return -EINVAL;
        old_rlim = current->signal->rlim + resource;
        if ((new_rlim.rlim_max > old_rlim->rlim_max) &&
            !capable(CAP_SYS_RESOURCE))
                return -EPERM;
-       if (resource == RLIMIT_NOFILE && new_rlim.rlim_max > NR_OPEN)
-                       return -EPERM;
+       if (resource == RLIMIT_NOFILE && new_rlim.rlim_max > sysctl_nr_open)
+               return -EPERM;
 
        retval = security_task_setrlimit(resource, &new_rlim);
        if (retval)
                return retval;
 
+       if (resource == RLIMIT_CPU && new_rlim.rlim_cur == 0) {
+               /*
+                * The caller is asking for an immediate RLIMIT_CPU
+                * expiry.  But we use the zero value to mean "it was
+                * never set".  So let's cheat and make it one second
+                * instead
+                */
+               new_rlim.rlim_cur = 1;
+       }
+
        task_lock(current->group_leader);
        *old_rlim = new_rlim;
        task_unlock(current->group_leader);
 
-       if (resource == RLIMIT_CPU && new_rlim.rlim_cur != RLIM_INFINITY &&
-           (cputime_eq(current->signal->it_prof_expires, cputime_zero) ||
-            new_rlim.rlim_cur <= cputime_to_secs(
-                    current->signal->it_prof_expires))) {
-               cputime_t cputime = secs_to_cputime(new_rlim.rlim_cur);
+       if (resource != RLIMIT_CPU)
+               goto out;
+
+       /*
+        * RLIMIT_CPU handling.   Note that the kernel fails to return an error
+        * code if it rejected the user's attempt to set RLIMIT_CPU.  This is a
+        * very long-standing error, and fixing it now risks breakage of
+        * applications, so we live with it
+        */
+       if (new_rlim.rlim_cur == RLIM_INFINITY)
+               goto out;
+
+       it_prof_secs = cputime_to_secs(current->signal->it_prof_expires);
+       if (it_prof_secs == 0 || new_rlim.rlim_cur <= it_prof_secs) {
+               unsigned long rlim_cur = new_rlim.rlim_cur;
+               cputime_t cputime;
+
+               cputime = secs_to_cputime(rlim_cur);
                read_lock(&tasklist_lock);
                spin_lock_irq(&current->sighand->siglock);
-               set_process_cpu_timer(current, CPUCLOCK_PROF,
-                                     &cputime, NULL);
+               set_process_cpu_timer(current, CPUCLOCK_PROF, &cputime, NULL);
                spin_unlock_irq(&current->sighand->siglock);
                read_unlock(&tasklist_lock);
        }
-
+out:
        return 0;
 }
 
@@ -1664,9 +1520,6 @@ asmlinkage long sys_setrlimit(unsigned int resource, struct rlimit __user *rlim)
  * a lot simpler!  (Which we're not doing right now because we're not
  * measuring them yet).
  *
- * This expects to be called with tasklist_lock read-locked or better,
- * and the siglock not locked.  It may momentarily take the siglock.
- *
  * When sampling multiple threads for RUSAGE_SELF, under SMP we might have
  * races with threads incrementing their own counters.  But since word
  * reads are atomic, we either get new values or old values and we don't
@@ -1674,8 +1527,37 @@ asmlinkage long sys_setrlimit(unsigned int resource, struct rlimit __user *rlim)
  * the c* fields from p->signal from races with exit.c updating those
  * fields when reaping, so a sample either gets all the additions of a
  * given child after it's reaped, or none so this sample is before reaping.
+ *
+ * Locking:
+ * We need to take the siglock for CHILDEREN, SELF and BOTH
+ * for  the cases current multithreaded, non-current single threaded
+ * non-current multithreaded.  Thread traversal is now safe with
+ * the siglock held.
+ * Strictly speaking, we donot need to take the siglock if we are current and
+ * single threaded,  as no one else can take our signal_struct away, no one
+ * else can  reap the  children to update signal->c* counters, and no one else
+ * can race with the signal-> fields. If we do not take any lock, the
+ * signal-> fields could be read out of order while another thread was just
+ * exiting. So we should  place a read memory barrier when we avoid the lock.
+ * On the writer side,  write memory barrier is implied in  __exit_signal
+ * as __exit_signal releases  the siglock spinlock after updating the signal->
+ * fields. But we don't do this yet to keep things simple.
+ *
  */
 
+static void accumulate_thread_rusage(struct task_struct *t, struct rusage *r,
+                                    cputime_t *utimep, cputime_t *stimep)
+{
+       *utimep = cputime_add(*utimep, t->utime);
+       *stimep = cputime_add(*stimep, t->stime);
+       r->ru_nvcsw += t->nvcsw;
+       r->ru_nivcsw += t->nivcsw;
+       r->ru_minflt += t->min_flt;
+       r->ru_majflt += t->maj_flt;
+       r->ru_inblock += task_io_get_inblock(t);
+       r->ru_oublock += task_io_get_oublock(t);
+}
+
 static void k_getrusage(struct task_struct *p, int who, struct rusage *r)
 {
        struct task_struct *t;
@@ -1683,73 +1565,73 @@ static void k_getrusage(struct task_struct *p, int who, struct rusage *r)
        cputime_t utime, stime;
 
        memset((char *) r, 0, sizeof *r);
+       utime = stime = cputime_zero;
+
+       if (who == RUSAGE_THREAD) {
+               accumulate_thread_rusage(p, r, &utime, &stime);
+               goto out;
+       }
 
-       if (unlikely(!p->signal))
+       rcu_read_lock();
+       if (!lock_task_sighand(p, &flags)) {
+               rcu_read_unlock();
                return;
+       }
 
        switch (who) {
+               case RUSAGE_BOTH:
                case RUSAGE_CHILDREN:
-                       spin_lock_irqsave(&p->sighand->siglock, flags);
                        utime = p->signal->cutime;
                        stime = p->signal->cstime;
                        r->ru_nvcsw = p->signal->cnvcsw;
                        r->ru_nivcsw = p->signal->cnivcsw;
                        r->ru_minflt = p->signal->cmin_flt;
                        r->ru_majflt = p->signal->cmaj_flt;
-                       spin_unlock_irqrestore(&p->sighand->siglock, flags);
-                       cputime_to_timeval(utime, &r->ru_utime);
-                       cputime_to_timeval(stime, &r->ru_stime);
-                       break;
+                       r->ru_inblock = p->signal->cinblock;
+                       r->ru_oublock = p->signal->coublock;
+
+                       if (who == RUSAGE_CHILDREN)
+                               break;
+
                case RUSAGE_SELF:
-                       spin_lock_irqsave(&p->sighand->siglock, flags);
-                       utime = stime = cputime_zero;
-                       goto sum_group;
-               case RUSAGE_BOTH:
-                       spin_lock_irqsave(&p->sighand->siglock, flags);
-                       utime = p->signal->cutime;
-                       stime = p->signal->cstime;
-                       r->ru_nvcsw = p->signal->cnvcsw;
-                       r->ru_nivcsw = p->signal->cnivcsw;
-                       r->ru_minflt = p->signal->cmin_flt;
-                       r->ru_majflt = p->signal->cmaj_flt;
-               sum_group:
                        utime = cputime_add(utime, p->signal->utime);
                        stime = cputime_add(stime, p->signal->stime);
                        r->ru_nvcsw += p->signal->nvcsw;
                        r->ru_nivcsw += p->signal->nivcsw;
                        r->ru_minflt += p->signal->min_flt;
                        r->ru_majflt += p->signal->maj_flt;
+                       r->ru_inblock += p->signal->inblock;
+                       r->ru_oublock += p->signal->oublock;
                        t = p;
                        do {
-                               utime = cputime_add(utime, t->utime);
-                               stime = cputime_add(stime, t->stime);
-                               r->ru_nvcsw += t->nvcsw;
-                               r->ru_nivcsw += t->nivcsw;
-                               r->ru_minflt += t->min_flt;
-                               r->ru_majflt += t->maj_flt;
+                               accumulate_thread_rusage(t, r, &utime, &stime);
                                t = next_thread(t);
                        } while (t != p);
-                       spin_unlock_irqrestore(&p->sighand->siglock, flags);
-                       cputime_to_timeval(utime, &r->ru_utime);
-                       cputime_to_timeval(stime, &r->ru_stime);
                        break;
+
                default:
                        BUG();
        }
+
+       unlock_task_sighand(p, &flags);
+       rcu_read_unlock();
+
+out:
+       cputime_to_timeval(utime, &r->ru_utime);
+       cputime_to_timeval(stime, &r->ru_stime);
 }
 
 int getrusage(struct task_struct *p, int who, struct rusage __user *ru)
 {
        struct rusage r;
-       read_lock(&tasklist_lock);
        k_getrusage(p, who, &r);
-       read_unlock(&tasklist_lock);
        return copy_to_user(ru, &r, sizeof(r)) ? -EFAULT : 0;
 }
 
 asmlinkage long sys_getrusage(int who, struct rusage __user *ru)
 {
-       if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN)
+       if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN &&
+           who != RUSAGE_THREAD)
                return -EINVAL;
        return getrusage(current, who, ru);
 }
@@ -1759,14 +1641,13 @@ asmlinkage long sys_umask(int mask)
        mask = xchg(&current->fs->umask, mask & S_IRWXUGO);
        return mask;
 }
-    
+
 asmlinkage long sys_prctl(int option, unsigned long arg2, unsigned long arg3,
                          unsigned long arg4, unsigned long arg5)
 {
-       long error;
+       long uninitialized_var(error);
 
-       error = security_task_prctl(option, arg2, arg3, arg4, arg5);
-       if (error)
+       if (security_task_prctl(option, arg2, arg3, arg4, arg5, &error))
                return error;
 
        switch (option) {
@@ -1781,14 +1662,14 @@ asmlinkage long sys_prctl(int option, unsigned long arg2, unsigned long arg3,
                        error = put_user(current->pdeath_signal, (int __user *)arg2);
                        break;
                case PR_GET_DUMPABLE:
-                       error = current->mm->dumpable;
+                       error = get_dumpable(current->mm);
                        break;
                case PR_SET_DUMPABLE:
-                       if (arg2 < 0 || arg2 > 2) {
+                       if (arg2 < 0 || arg2 > 1) {
                                error = -EINVAL;
                                break;
                        }
-                       current->mm->dumpable = arg2;
+                       set_dumpable(current->mm, arg2);
                        break;
 
                case PR_SET_UNALIGN:
@@ -1819,17 +1700,6 @@ asmlinkage long sys_prctl(int option, unsigned long arg2, unsigned long arg3,
                                error = -EINVAL;
                        break;
 
-               case PR_GET_KEEPCAPS:
-                       if (current->keep_capabilities)
-                               error = 1;
-                       break;
-               case PR_SET_KEEPCAPS:
-                       if (arg2 != 0 && arg2 != 1) {
-                               error = -EINVAL;
-                               break;
-                       }
-                       current->keep_capabilities = arg2;
-                       break;
                case PR_SET_NAME: {
                        struct task_struct *me = current;
                        unsigned char ncomm[sizeof(me->comm)];
@@ -1850,9 +1720,98 @@ asmlinkage long sys_prctl(int option, unsigned long arg2, unsigned long arg3,
                                return -EFAULT;
                        return 0;
                }
+               case PR_GET_ENDIAN:
+                       error = GET_ENDIAN(current, arg2);
+                       break;
+               case PR_SET_ENDIAN:
+                       error = SET_ENDIAN(current, arg2);
+                       break;
+
+               case PR_GET_SECCOMP:
+                       error = prctl_get_seccomp();
+                       break;
+               case PR_SET_SECCOMP:
+                       error = prctl_set_seccomp(arg2);
+                       break;
+               case PR_GET_TSC:
+                       error = GET_TSC_CTL(arg2);
+                       break;
+               case PR_SET_TSC:
+                       error = SET_TSC_CTL(arg2);
+                       break;
                default:
                        error = -EINVAL;
                        break;
        }
        return error;
 }
+
+asmlinkage long sys_getcpu(unsigned __user *cpup, unsigned __user *nodep,
+                          struct getcpu_cache __user *unused)
+{
+       int err = 0;
+       int cpu = raw_smp_processor_id();
+       if (cpup)
+               err |= put_user(cpu, cpup);
+       if (nodep)
+               err |= put_user(cpu_to_node(cpu), nodep);
+       return err ? -EFAULT : 0;
+}
+
+char poweroff_cmd[POWEROFF_CMD_PATH_LEN] = "/sbin/poweroff";
+
+static void argv_cleanup(char **argv, char **envp)
+{
+       argv_free(argv);
+}
+
+/**
+ * orderly_poweroff - Trigger an orderly system poweroff
+ * @force: force poweroff if command execution fails
+ *
+ * This may be called from any context to trigger a system shutdown.
+ * If the orderly shutdown fails, it will force an immediate shutdown.
+ */
+int orderly_poweroff(bool force)
+{
+       int argc;
+       char **argv = argv_split(GFP_ATOMIC, poweroff_cmd, &argc);
+       static char *envp[] = {
+               "HOME=/",
+               "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
+               NULL
+       };
+       int ret = -ENOMEM;
+       struct subprocess_info *info;
+
+       if (argv == NULL) {
+               printk(KERN_WARNING "%s failed to allocate memory for \"%s\"\n",
+                      __func__, poweroff_cmd);
+               goto out;
+       }
+
+       info = call_usermodehelper_setup(argv[0], argv, envp);
+       if (info == NULL) {
+               argv_free(argv);
+               goto out;
+       }
+
+       call_usermodehelper_setcleanup(info, argv_cleanup);
+
+       ret = call_usermodehelper_exec(info, UMH_NO_WAIT);
+
+  out:
+       if (ret && force) {
+               printk(KERN_WARNING "Failed to start orderly shutdown: "
+                      "forcing the issue\n");
+
+               /* I guess this should try to kick off some daemon to
+                  sync and poweroff asap.  Or not even bother syncing
+                  if we're doing an emergency shutdown? */
+               emergency_sync();
+               kernel_power_off();
+       }
+
+       return ret;
+}
+EXPORT_SYMBOL_GPL(orderly_poweroff);