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 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;
73 EXPORT_SYMBOL_GPL(set_task_ioprio);
75 SYSCALL_DEFINE3(ioprio_set, int, which, int, who, int, ioprio)
77 int class = IOPRIO_PRIO_CLASS(ioprio);
78 int data = IOPRIO_PRIO_DATA(ioprio);
79 struct task_struct *p, *g;
80 struct user_struct *user;
86 if (!capable(CAP_SYS_ADMIN))
88 /* fall through, rt has prio field too */
90 if (data >= IOPRIO_BE_NR || data < 0)
94 case IOPRIO_CLASS_IDLE:
96 case IOPRIO_CLASS_NONE:
106 * We want IOPRIO_WHO_PGRP/IOPRIO_WHO_USER to be "atomic",
107 * so we can't use rcu_read_lock(). See re-copy of ->ioprio
110 read_lock(&tasklist_lock);
112 case IOPRIO_WHO_PROCESS:
116 p = find_task_by_vpid(who);
118 ret = set_task_ioprio(p, ioprio);
120 case IOPRIO_WHO_PGRP:
122 pgrp = task_pgrp(current);
124 pgrp = find_vpid(who);
125 do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
126 ret = set_task_ioprio(p, ioprio);
129 } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
131 case IOPRIO_WHO_USER:
133 user = current_user();
135 user = find_user(who);
140 do_each_thread(g, p) {
141 if (__task_cred(p)->uid != who)
143 ret = set_task_ioprio(p, ioprio);
146 } while_each_thread(g, p);
155 read_unlock(&tasklist_lock);
159 static int get_task_ioprio(struct task_struct *p)
163 ret = security_task_getioprio(p);
166 ret = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_NONE, IOPRIO_NORM);
168 ret = p->io_context->ioprio;
173 int ioprio_best(unsigned short aprio, unsigned short bprio)
175 unsigned short aclass = IOPRIO_PRIO_CLASS(aprio);
176 unsigned short bclass = IOPRIO_PRIO_CLASS(bprio);
178 if (aclass == IOPRIO_CLASS_NONE)
179 aclass = IOPRIO_CLASS_BE;
180 if (bclass == IOPRIO_CLASS_NONE)
181 bclass = IOPRIO_CLASS_BE;
183 if (aclass == bclass)
184 return min(aprio, bprio);
191 SYSCALL_DEFINE2(ioprio_get, int, which, int, who)
193 struct task_struct *g, *p;
194 struct user_struct *user;
199 read_lock(&tasklist_lock);
201 case IOPRIO_WHO_PROCESS:
205 p = find_task_by_vpid(who);
207 ret = get_task_ioprio(p);
209 case IOPRIO_WHO_PGRP:
211 pgrp = task_pgrp(current);
213 pgrp = find_vpid(who);
214 do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
215 tmpio = get_task_ioprio(p);
221 ret = ioprio_best(ret, tmpio);
222 } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
224 case IOPRIO_WHO_USER:
226 user = current_user();
228 user = find_user(who);
233 do_each_thread(g, p) {
234 if (__task_cred(p)->uid != user->uid)
236 tmpio = get_task_ioprio(p);
242 ret = ioprio_best(ret, tmpio);
243 } while_each_thread(g, p);
252 read_unlock(&tasklist_lock);