security: tlk_driver: kernel mem type allocation
[linux-3.10.git] / fs / ioprio.c
index 8def14e..31666c9 100644 (file)
@@ -21,6 +21,7 @@
  */
 #include <linux/gfp.h>
 #include <linux/kernel.h>
+#include <linux/export.h>
 #include <linux/ioprio.h>
 #include <linux/blkdev.h>
 #include <linux/capability.h>
@@ -36,8 +37,8 @@ int set_task_ioprio(struct task_struct *task, int ioprio)
 
        rcu_read_lock();
        tcred = __task_cred(task);
-       if (tcred->uid != cred->euid &&
-           tcred->uid != cred->uid && !capable(CAP_SYS_NICE)) {
+       if (!uid_eq(tcred->uid, cred->euid) &&
+           !uid_eq(tcred->uid, cred->uid) && !capable(CAP_SYS_NICE)) {
                rcu_read_unlock();
                return -EPERM;
        }
@@ -47,28 +48,12 @@ int set_task_ioprio(struct task_struct *task, int ioprio)
        if (err)
                return err;
 
-       task_lock(task);
-       do {
-               ioc = task->io_context;
-               /* see wmb() in current_io_context() */
-               smp_read_barrier_depends();
-               if (ioc)
-                       break;
-
-               ioc = alloc_io_context(GFP_ATOMIC, -1);
-               if (!ioc) {
-                       err = -ENOMEM;
-                       break;
-               }
-               task->io_context = ioc;
-       } while (1);
-
-       if (!err) {
+       ioc = get_task_io_context(task, GFP_ATOMIC, NUMA_NO_NODE);
+       if (ioc) {
                ioc->ioprio = ioprio;
-               ioc->ioprio_changed = 1;
+               put_io_context(ioc);
        }
 
-       task_unlock(task);
        return err;
 }
 EXPORT_SYMBOL_GPL(set_task_ioprio);
@@ -80,6 +65,7 @@ SYSCALL_DEFINE3(ioprio_set, int, which, int, who, int, ioprio)
        struct task_struct *p, *g;
        struct user_struct *user;
        struct pid *pgrp;
+       kuid_t uid;
        int ret;
 
        switch (class) {
@@ -103,12 +89,7 @@ SYSCALL_DEFINE3(ioprio_set, int, which, int, who, int, ioprio)
        }
 
        ret = -ESRCH;
-       /*
-        * We want IOPRIO_WHO_PGRP/IOPRIO_WHO_USER to be "atomic",
-        * so we can't use rcu_read_lock(). See re-copy of ->ioprio
-        * in copy_process().
-        */
-       read_lock(&tasklist_lock);
+       rcu_read_lock();
        switch (which) {
                case IOPRIO_WHO_PROCESS:
                        if (!who)
@@ -130,21 +111,19 @@ SYSCALL_DEFINE3(ioprio_set, int, which, int, who, int, ioprio)
                        } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
                        break;
                case IOPRIO_WHO_USER:
+                       uid = make_kuid(current_user_ns(), who);
+                       if (!uid_valid(uid))
+                               break;
                        if (!who)
                                user = current_user();
                        else
-                               user = find_user(who);
+                               user = find_user(uid);
 
                        if (!user)
                                break;
 
                        do_each_thread(g, p) {
-                               int match;
-
-                               rcu_read_lock();
-                               match = __task_cred(p)->uid == who;
-                               rcu_read_unlock();
-                               if (!match)
+                               if (!uid_eq(task_uid(p), uid))
                                        continue;
                                ret = set_task_ioprio(p, ioprio);
                                if (ret)
@@ -158,7 +137,7 @@ free_uid:
                        ret = -EINVAL;
        }
 
-       read_unlock(&tasklist_lock);
+       rcu_read_unlock();
        return ret;
 }
 
@@ -178,14 +157,16 @@ out:
 
 int ioprio_best(unsigned short aprio, unsigned short bprio)
 {
-       unsigned short aclass = IOPRIO_PRIO_CLASS(aprio);
-       unsigned short bclass = IOPRIO_PRIO_CLASS(bprio);
+       unsigned short aclass;
+       unsigned short bclass;
 
-       if (aclass == IOPRIO_CLASS_NONE)
-               aclass = IOPRIO_CLASS_BE;
-       if (bclass == IOPRIO_CLASS_NONE)
-               bclass = IOPRIO_CLASS_BE;
+       if (!ioprio_valid(aprio))
+               aprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, IOPRIO_NORM);
+       if (!ioprio_valid(bprio))
+               bprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, IOPRIO_NORM);
 
+       aclass = IOPRIO_PRIO_CLASS(aprio);
+       bclass = IOPRIO_PRIO_CLASS(bprio);
        if (aclass == bclass)
                return min(aprio, bprio);
        if (aclass > bclass)
@@ -199,10 +180,11 @@ SYSCALL_DEFINE2(ioprio_get, int, which, int, who)
        struct task_struct *g, *p;
        struct user_struct *user;
        struct pid *pgrp;
+       kuid_t uid;
        int ret = -ESRCH;
        int tmpio;
 
-       read_lock(&tasklist_lock);
+       rcu_read_lock();
        switch (which) {
                case IOPRIO_WHO_PROCESS:
                        if (!who)
@@ -228,21 +210,17 @@ SYSCALL_DEFINE2(ioprio_get, int, which, int, who)
                        } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
                        break;
                case IOPRIO_WHO_USER:
+                       uid = make_kuid(current_user_ns(), who);
                        if (!who)
                                user = current_user();
                        else
-                               user = find_user(who);
+                               user = find_user(uid);
 
                        if (!user)
                                break;
 
                        do_each_thread(g, p) {
-                               int match;
-
-                               rcu_read_lock();
-                               match = __task_cred(p)->uid == user->uid;
-                               rcu_read_unlock();
-                               if (!match)
+                               if (!uid_eq(task_uid(p), user->uid))
                                        continue;
                                tmpio = get_task_ioprio(p);
                                if (tmpio < 0)
@@ -260,6 +238,6 @@ SYSCALL_DEFINE2(ioprio_get, int, which, int, who)
                        ret = -EINVAL;
        }
 
-       read_unlock(&tasklist_lock);
+       rcu_read_unlock();
        return ret;
 }