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;
35 if (task->uid != current->euid &&
36 task->uid != current->uid && !capable(CAP_SYS_NICE))
39 err = security_task_setioprio(task, ioprio);
45 ioc = task->io_context;
46 /* see wmb() in current_io_context() */
47 smp_read_barrier_depends();
51 ioc = alloc_io_context(GFP_ATOMIC, -1);
56 task->io_context = ioc;
61 ioc->ioprio_changed = 1;
68 asmlinkage long sys_ioprio_set(int which, int who, int ioprio)
70 int class = IOPRIO_PRIO_CLASS(ioprio);
71 int data = IOPRIO_PRIO_DATA(ioprio);
72 struct task_struct *p, *g;
73 struct user_struct *user;
79 if (!capable(CAP_SYS_ADMIN))
81 /* fall through, rt has prio field too */
83 if (data >= IOPRIO_BE_NR || data < 0)
87 case IOPRIO_CLASS_IDLE:
88 if (!capable(CAP_SYS_ADMIN))
91 case IOPRIO_CLASS_NONE:
101 * We want IOPRIO_WHO_PGRP/IOPRIO_WHO_USER to be "atomic",
102 * so we can't use rcu_read_lock(). See re-copy of ->ioprio
105 read_lock(&tasklist_lock);
107 case IOPRIO_WHO_PROCESS:
111 p = find_task_by_vpid(who);
113 ret = set_task_ioprio(p, ioprio);
115 case IOPRIO_WHO_PGRP:
117 pgrp = task_pgrp(current);
119 pgrp = find_vpid(who);
120 do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
121 ret = set_task_ioprio(p, ioprio);
124 } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
126 case IOPRIO_WHO_USER:
128 user = current->user;
130 user = find_user(who);
135 do_each_thread(g, p) {
138 ret = set_task_ioprio(p, ioprio);
141 } while_each_thread(g, p);
150 read_unlock(&tasklist_lock);
154 static int get_task_ioprio(struct task_struct *p)
158 ret = security_task_getioprio(p);
161 ret = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_NONE, IOPRIO_NORM);
163 ret = p->io_context->ioprio;
168 int ioprio_best(unsigned short aprio, unsigned short bprio)
170 unsigned short aclass = IOPRIO_PRIO_CLASS(aprio);
171 unsigned short bclass = IOPRIO_PRIO_CLASS(bprio);
173 if (aclass == IOPRIO_CLASS_NONE)
174 aclass = IOPRIO_CLASS_BE;
175 if (bclass == IOPRIO_CLASS_NONE)
176 bclass = IOPRIO_CLASS_BE;
178 if (aclass == bclass)
179 return min(aprio, bprio);
186 asmlinkage long sys_ioprio_get(int which, int who)
188 struct task_struct *g, *p;
189 struct user_struct *user;
194 read_lock(&tasklist_lock);
196 case IOPRIO_WHO_PROCESS:
200 p = find_task_by_vpid(who);
202 ret = get_task_ioprio(p);
204 case IOPRIO_WHO_PGRP:
206 pgrp = task_pgrp(current);
208 pgrp = find_vpid(who);
209 do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
210 tmpio = get_task_ioprio(p);
216 ret = ioprio_best(ret, tmpio);
217 } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
219 case IOPRIO_WHO_USER:
221 user = current->user;
223 user = find_user(who);
228 do_each_thread(g, p) {
229 if (p->uid != user->uid)
231 tmpio = get_task_ioprio(p);
237 ret = ioprio_best(ret, tmpio);
238 } while_each_thread(g, p);
247 read_unlock(&tasklist_lock);