4 * Copyright (C) 2004 Jens Axboe <axboe@kernel.dk>
6 * Helper functions for setting/querying io priorities of processes. The
7 * system calls closely mimmick getpriority/setpriority, see the man page for
8 * those. The prio argument is a composite of prio class and prio data, where
9 * the data argument has meaning within that class. The standard scheduling
10 * classes have 8 distinct prio levels, with 0 being the highest prio and 7
13 * IOW, setting BE scheduling class with prio 2 is done ala:
15 * unsigned int prio = (IOPRIO_CLASS_BE << IOPRIO_CLASS_SHIFT) | 2;
17 * ioprio_set(PRIO_PROCESS, pid, prio);
19 * See also Documentation/block/ioprio.txt
22 #include <linux/kernel.h>
23 #include <linux/ioprio.h>
24 #include <linux/blkdev.h>
25 #include <linux/capability.h>
26 #include <linux/syscalls.h>
27 #include <linux/security.h>
28 #include <linux/pid_namespace.h>
30 static int set_task_ioprio(struct task_struct *task, int ioprio)
33 struct io_context *ioc;
34 const struct cred *cred = current_cred(), *tcred;
37 tcred = __task_cred(task);
38 if (tcred->uid != cred->euid &&
39 tcred->uid != cred->uid && !capable(CAP_SYS_NICE)) {
45 err = security_task_setioprio(task, ioprio);
51 ioc = task->io_context;
52 /* see wmb() in current_io_context() */
53 smp_read_barrier_depends();
57 ioc = alloc_io_context(GFP_ATOMIC, -1);
62 task->io_context = ioc;
67 ioc->ioprio_changed = 1;
74 asmlinkage long sys_ioprio_set(int which, int who, int ioprio)
76 int class = IOPRIO_PRIO_CLASS(ioprio);
77 int data = IOPRIO_PRIO_DATA(ioprio);
78 struct task_struct *p, *g;
79 struct user_struct *user;
85 if (!capable(CAP_SYS_ADMIN))
87 /* fall through, rt has prio field too */
89 if (data >= IOPRIO_BE_NR || data < 0)
93 case IOPRIO_CLASS_IDLE:
95 case IOPRIO_CLASS_NONE:
105 * We want IOPRIO_WHO_PGRP/IOPRIO_WHO_USER to be "atomic",
106 * so we can't use rcu_read_lock(). See re-copy of ->ioprio
109 read_lock(&tasklist_lock);
111 case IOPRIO_WHO_PROCESS:
115 p = find_task_by_vpid(who);
117 ret = set_task_ioprio(p, ioprio);
119 case IOPRIO_WHO_PGRP:
121 pgrp = task_pgrp(current);
123 pgrp = find_vpid(who);
124 do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
125 ret = set_task_ioprio(p, ioprio);
128 } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
130 case IOPRIO_WHO_USER:
132 user = current_user();
134 user = find_user(who);
139 do_each_thread(g, p) {
140 if (__task_cred(p)->uid != who)
142 ret = set_task_ioprio(p, ioprio);
145 } while_each_thread(g, p);
154 read_unlock(&tasklist_lock);
158 static int get_task_ioprio(struct task_struct *p)
162 ret = security_task_getioprio(p);
165 ret = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_NONE, IOPRIO_NORM);
167 ret = p->io_context->ioprio;
172 int ioprio_best(unsigned short aprio, unsigned short bprio)
174 unsigned short aclass = IOPRIO_PRIO_CLASS(aprio);
175 unsigned short bclass = IOPRIO_PRIO_CLASS(bprio);
177 if (aclass == IOPRIO_CLASS_NONE)
178 aclass = IOPRIO_CLASS_BE;
179 if (bclass == IOPRIO_CLASS_NONE)
180 bclass = IOPRIO_CLASS_BE;
182 if (aclass == bclass)
183 return min(aprio, bprio);
190 asmlinkage long sys_ioprio_get(int which, int who)
192 struct task_struct *g, *p;
193 struct user_struct *user;
198 read_lock(&tasklist_lock);
200 case IOPRIO_WHO_PROCESS:
204 p = find_task_by_vpid(who);
206 ret = get_task_ioprio(p);
208 case IOPRIO_WHO_PGRP:
210 pgrp = task_pgrp(current);
212 pgrp = find_vpid(who);
213 do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
214 tmpio = get_task_ioprio(p);
220 ret = ioprio_best(ret, tmpio);
221 } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
223 case IOPRIO_WHO_USER:
225 user = current_user();
227 user = find_user(who);
232 do_each_thread(g, p) {
233 if (__task_cred(p)->uid != user->uid)
235 tmpio = get_task_ioprio(p);
241 ret = ioprio_best(ret, tmpio);
242 } while_each_thread(g, p);
251 read_unlock(&tasklist_lock);