Back to home page

LXR

 
 

    


0001 /*
0002  * fs/ioprio.c
0003  *
0004  * Copyright (C) 2004 Jens Axboe <axboe@kernel.dk>
0005  *
0006  * Helper functions for setting/querying io priorities of processes. The
0007  * system calls closely mimmick getpriority/setpriority, see the man page for
0008  * those. The prio argument is a composite of prio class and prio data, where
0009  * the data argument has meaning within that class. The standard scheduling
0010  * classes have 8 distinct prio levels, with 0 being the highest prio and 7
0011  * being the lowest.
0012  *
0013  * IOW, setting BE scheduling class with prio 2 is done ala:
0014  *
0015  * unsigned int prio = (IOPRIO_CLASS_BE << IOPRIO_CLASS_SHIFT) | 2;
0016  *
0017  * ioprio_set(PRIO_PROCESS, pid, prio);
0018  *
0019  * See also Documentation/block/ioprio.txt
0020  *
0021  */
0022 #include <linux/gfp.h>
0023 #include <linux/kernel.h>
0024 #include <linux/export.h>
0025 #include <linux/ioprio.h>
0026 #include <linux/blkdev.h>
0027 #include <linux/capability.h>
0028 #include <linux/syscalls.h>
0029 #include <linux/security.h>
0030 #include <linux/pid_namespace.h>
0031 
0032 int set_task_ioprio(struct task_struct *task, int ioprio)
0033 {
0034     int err;
0035     struct io_context *ioc;
0036     const struct cred *cred = current_cred(), *tcred;
0037 
0038     rcu_read_lock();
0039     tcred = __task_cred(task);
0040     if (!uid_eq(tcred->uid, cred->euid) &&
0041         !uid_eq(tcred->uid, cred->uid) && !capable(CAP_SYS_NICE)) {
0042         rcu_read_unlock();
0043         return -EPERM;
0044     }
0045     rcu_read_unlock();
0046 
0047     err = security_task_setioprio(task, ioprio);
0048     if (err)
0049         return err;
0050 
0051     ioc = get_task_io_context(task, GFP_ATOMIC, NUMA_NO_NODE);
0052     if (ioc) {
0053         ioc->ioprio = ioprio;
0054         put_io_context(ioc);
0055     }
0056 
0057     return err;
0058 }
0059 EXPORT_SYMBOL_GPL(set_task_ioprio);
0060 
0061 SYSCALL_DEFINE3(ioprio_set, int, which, int, who, int, ioprio)
0062 {
0063     int class = IOPRIO_PRIO_CLASS(ioprio);
0064     int data = IOPRIO_PRIO_DATA(ioprio);
0065     struct task_struct *p, *g;
0066     struct user_struct *user;
0067     struct pid *pgrp;
0068     kuid_t uid;
0069     int ret;
0070 
0071     switch (class) {
0072         case IOPRIO_CLASS_RT:
0073             if (!capable(CAP_SYS_ADMIN))
0074                 return -EPERM;
0075             /* fall through, rt has prio field too */
0076         case IOPRIO_CLASS_BE:
0077             if (data >= IOPRIO_BE_NR || data < 0)
0078                 return -EINVAL;
0079 
0080             break;
0081         case IOPRIO_CLASS_IDLE:
0082             break;
0083         case IOPRIO_CLASS_NONE:
0084             if (data)
0085                 return -EINVAL;
0086             break;
0087         default:
0088             return -EINVAL;
0089     }
0090 
0091     ret = -ESRCH;
0092     rcu_read_lock();
0093     switch (which) {
0094         case IOPRIO_WHO_PROCESS:
0095             if (!who)
0096                 p = current;
0097             else
0098                 p = find_task_by_vpid(who);
0099             if (p)
0100                 ret = set_task_ioprio(p, ioprio);
0101             break;
0102         case IOPRIO_WHO_PGRP:
0103             if (!who)
0104                 pgrp = task_pgrp(current);
0105             else
0106                 pgrp = find_vpid(who);
0107             do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
0108                 ret = set_task_ioprio(p, ioprio);
0109                 if (ret)
0110                     break;
0111             } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
0112             break;
0113         case IOPRIO_WHO_USER:
0114             uid = make_kuid(current_user_ns(), who);
0115             if (!uid_valid(uid))
0116                 break;
0117             if (!who)
0118                 user = current_user();
0119             else
0120                 user = find_user(uid);
0121 
0122             if (!user)
0123                 break;
0124 
0125             do_each_thread(g, p) {
0126                 if (!uid_eq(task_uid(p), uid) ||
0127                     !task_pid_vnr(p))
0128                     continue;
0129                 ret = set_task_ioprio(p, ioprio);
0130                 if (ret)
0131                     goto free_uid;
0132             } while_each_thread(g, p);
0133 free_uid:
0134             if (who)
0135                 free_uid(user);
0136             break;
0137         default:
0138             ret = -EINVAL;
0139     }
0140 
0141     rcu_read_unlock();
0142     return ret;
0143 }
0144 
0145 static int get_task_ioprio(struct task_struct *p)
0146 {
0147     int ret;
0148 
0149     ret = security_task_getioprio(p);
0150     if (ret)
0151         goto out;
0152     ret = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_NONE, IOPRIO_NORM);
0153     task_lock(p);
0154     if (p->io_context)
0155         ret = p->io_context->ioprio;
0156     task_unlock(p);
0157 out:
0158     return ret;
0159 }
0160 
0161 int ioprio_best(unsigned short aprio, unsigned short bprio)
0162 {
0163     unsigned short aclass;
0164     unsigned short bclass;
0165 
0166     if (!ioprio_valid(aprio))
0167         aprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, IOPRIO_NORM);
0168     if (!ioprio_valid(bprio))
0169         bprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, IOPRIO_NORM);
0170 
0171     aclass = IOPRIO_PRIO_CLASS(aprio);
0172     bclass = IOPRIO_PRIO_CLASS(bprio);
0173     if (aclass == bclass)
0174         return min(aprio, bprio);
0175     if (aclass > bclass)
0176         return bprio;
0177     else
0178         return aprio;
0179 }
0180 
0181 SYSCALL_DEFINE2(ioprio_get, int, which, int, who)
0182 {
0183     struct task_struct *g, *p;
0184     struct user_struct *user;
0185     struct pid *pgrp;
0186     kuid_t uid;
0187     int ret = -ESRCH;
0188     int tmpio;
0189 
0190     rcu_read_lock();
0191     switch (which) {
0192         case IOPRIO_WHO_PROCESS:
0193             if (!who)
0194                 p = current;
0195             else
0196                 p = find_task_by_vpid(who);
0197             if (p)
0198                 ret = get_task_ioprio(p);
0199             break;
0200         case IOPRIO_WHO_PGRP:
0201             if (!who)
0202                 pgrp = task_pgrp(current);
0203             else
0204                 pgrp = find_vpid(who);
0205             do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
0206                 tmpio = get_task_ioprio(p);
0207                 if (tmpio < 0)
0208                     continue;
0209                 if (ret == -ESRCH)
0210                     ret = tmpio;
0211                 else
0212                     ret = ioprio_best(ret, tmpio);
0213             } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
0214             break;
0215         case IOPRIO_WHO_USER:
0216             uid = make_kuid(current_user_ns(), who);
0217             if (!who)
0218                 user = current_user();
0219             else
0220                 user = find_user(uid);
0221 
0222             if (!user)
0223                 break;
0224 
0225             do_each_thread(g, p) {
0226                 if (!uid_eq(task_uid(p), user->uid) ||
0227                     !task_pid_vnr(p))
0228                     continue;
0229                 tmpio = get_task_ioprio(p);
0230                 if (tmpio < 0)
0231                     continue;
0232                 if (ret == -ESRCH)
0233                     ret = tmpio;
0234                 else
0235                     ret = ioprio_best(ret, tmpio);
0236             } while_each_thread(g, p);
0237 
0238             if (who)
0239                 free_uid(user);
0240             break;
0241         default:
0242             ret = -EINVAL;
0243     }
0244 
0245     rcu_read_unlock();
0246     return ret;
0247 }