Blame view
fs/ioprio.c
5.19 KB
22e2c507c [PATCH] Update cf... |
1 2 3 |
/* * fs/ioprio.c * |
0fe234795 [PATCH] Update ax... |
4 |
* Copyright (C) 2004 Jens Axboe <axboe@kernel.dk> |
22e2c507c [PATCH] Update cf... |
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
* * Helper functions for setting/querying io priorities of processes. The * system calls closely mimmick getpriority/setpriority, see the man page for * those. The prio argument is a composite of prio class and prio data, where * the data argument has meaning within that class. The standard scheduling * classes have 8 distinct prio levels, with 0 being the highest prio and 7 * being the lowest. * * IOW, setting BE scheduling class with prio 2 is done ala: * * unsigned int prio = (IOPRIO_CLASS_BE << IOPRIO_CLASS_SHIFT) | 2; * * ioprio_set(PRIO_PROCESS, pid, prio); * * See also Documentation/block/ioprio.txt * */ |
5a0e3ad6a include cleanup: ... |
22 |
#include <linux/gfp.h> |
22e2c507c [PATCH] Update cf... |
23 24 25 |
#include <linux/kernel.h> #include <linux/ioprio.h> #include <linux/blkdev.h> |
16f7e0fe2 [PATCH] capable/c... |
26 |
#include <linux/capability.h> |
9abdc4cd8 fs/ioprio.c shoul... |
27 |
#include <linux/syscalls.h> |
03e680606 [PATCH] lsm: add ... |
28 |
#include <linux/security.h> |
b488893a3 pid namespaces: c... |
29 |
#include <linux/pid_namespace.h> |
22e2c507c [PATCH] Update cf... |
30 |
|
b3881f74b ext4: Add mount o... |
31 |
int set_task_ioprio(struct task_struct *task, int ioprio) |
22e2c507c [PATCH] Update cf... |
32 |
{ |
03e680606 [PATCH] lsm: add ... |
33 |
int err; |
22e2c507c [PATCH] Update cf... |
34 |
struct io_context *ioc; |
c69e8d9c0 CRED: Use RCU to ... |
35 |
const struct cred *cred = current_cred(), *tcred; |
22e2c507c [PATCH] Update cf... |
36 |
|
c69e8d9c0 CRED: Use RCU to ... |
37 38 39 40 41 |
rcu_read_lock(); tcred = __task_cred(task); if (tcred->uid != cred->euid && tcred->uid != cred->uid && !capable(CAP_SYS_NICE)) { rcu_read_unlock(); |
22e2c507c [PATCH] Update cf... |
42 |
return -EPERM; |
c69e8d9c0 CRED: Use RCU to ... |
43 44 |
} rcu_read_unlock(); |
22e2c507c [PATCH] Update cf... |
45 |
|
03e680606 [PATCH] lsm: add ... |
46 47 48 |
err = security_task_setioprio(task, ioprio); if (err) return err; |
22e2c507c [PATCH] Update cf... |
49 |
task_lock(task); |
fd0928df9 ioprio: move io p... |
50 51 52 53 54 55 |
do { ioc = task->io_context; /* see wmb() in current_io_context() */ smp_read_barrier_depends(); if (ioc) break; |
22e2c507c [PATCH] Update cf... |
56 |
|
fd0928df9 ioprio: move io p... |
57 58 59 60 61 62 |
ioc = alloc_io_context(GFP_ATOMIC, -1); if (!ioc) { err = -ENOMEM; break; } task->io_context = ioc; |
fd0928df9 ioprio: move io p... |
63 |
} while (1); |
9f83e45eb [PATCH] Fix curre... |
64 |
|
fd0928df9 ioprio: move io p... |
65 66 |
if (!err) { ioc->ioprio = ioprio; |
fc46379da [PATCH] cfq-iosch... |
67 |
ioc->ioprio_changed = 1; |
fd0928df9 ioprio: move io p... |
68 |
} |
22e2c507c [PATCH] Update cf... |
69 70 |
task_unlock(task); |
fd0928df9 ioprio: move io p... |
71 |
return err; |
22e2c507c [PATCH] Update cf... |
72 |
} |
b3881f74b ext4: Add mount o... |
73 |
EXPORT_SYMBOL_GPL(set_task_ioprio); |
22e2c507c [PATCH] Update cf... |
74 |
|
938bb9f5e [CVE-2009-0029] S... |
75 |
SYSCALL_DEFINE3(ioprio_set, int, which, int, who, int, ioprio) |
22e2c507c [PATCH] Update cf... |
76 77 78 79 80 |
{ int class = IOPRIO_PRIO_CLASS(ioprio); int data = IOPRIO_PRIO_DATA(ioprio); struct task_struct *p, *g; struct user_struct *user; |
41487c65b [PATCH] pid: repl... |
81 |
struct pid *pgrp; |
22e2c507c [PATCH] Update cf... |
82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
int ret; switch (class) { case IOPRIO_CLASS_RT: if (!capable(CAP_SYS_ADMIN)) return -EPERM; /* fall through, rt has prio field too */ case IOPRIO_CLASS_BE: if (data >= IOPRIO_BE_NR || data < 0) return -EINVAL; break; case IOPRIO_CLASS_IDLE: break; |
8ec680e4c ioprio: allow sys... |
96 97 98 99 |
case IOPRIO_CLASS_NONE: if (data) return -EINVAL; break; |
22e2c507c [PATCH] Update cf... |
100 101 102 103 104 |
default: return -EINVAL; } ret = -ESRCH; |
cf342e52e [PATCH] Don't nee... |
105 106 107 108 109 110 |
/* * 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); |
22e2c507c [PATCH] Update cf... |
111 112 113 114 115 |
switch (which) { case IOPRIO_WHO_PROCESS: if (!who) p = current; else |
228ebcbe6 Uninline find_tas... |
116 |
p = find_task_by_vpid(who); |
22e2c507c [PATCH] Update cf... |
117 118 119 120 121 |
if (p) ret = set_task_ioprio(p, ioprio); break; case IOPRIO_WHO_PGRP: if (!who) |
41487c65b [PATCH] pid: repl... |
122 123 |
pgrp = task_pgrp(current); else |
b488893a3 pid namespaces: c... |
124 |
pgrp = find_vpid(who); |
2d70b68d4 fix setpriority(P... |
125 |
do_each_pid_thread(pgrp, PIDTYPE_PGID, p) { |
22e2c507c [PATCH] Update cf... |
126 127 128 |
ret = set_task_ioprio(p, ioprio); if (ret) break; |
2d70b68d4 fix setpriority(P... |
129 |
} while_each_pid_thread(pgrp, PIDTYPE_PGID, p); |
22e2c507c [PATCH] Update cf... |
130 131 132 |
break; case IOPRIO_WHO_USER: if (!who) |
86a264abe CRED: Wrap curren... |
133 |
user = current_user(); |
22e2c507c [PATCH] Update cf... |
134 135 136 137 138 139 140 |
else user = find_user(who); if (!user) break; do_each_thread(g, p) { |
c69e8d9c0 CRED: Use RCU to ... |
141 |
if (__task_cred(p)->uid != who) |
22e2c507c [PATCH] Update cf... |
142 143 144 |
continue; ret = set_task_ioprio(p, ioprio); if (ret) |
78bd4d484 [PATCH] sys_iopri... |
145 |
goto free_uid; |
22e2c507c [PATCH] Update cf... |
146 |
} while_each_thread(g, p); |
78bd4d484 [PATCH] sys_iopri... |
147 |
free_uid: |
22e2c507c [PATCH] Update cf... |
148 149 150 151 152 153 |
if (who) free_uid(user); break; default: ret = -EINVAL; } |
cf342e52e [PATCH] Don't nee... |
154 |
read_unlock(&tasklist_lock); |
22e2c507c [PATCH] Update cf... |
155 156 |
return ret; } |
a1836a42d [PATCH] SELinux: ... |
157 158 159 160 161 162 163 |
static int get_task_ioprio(struct task_struct *p) { int ret; ret = security_task_getioprio(p); if (ret) goto out; |
fd0928df9 ioprio: move io p... |
164 165 166 |
ret = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_NONE, IOPRIO_NORM); if (p->io_context) ret = p->io_context->ioprio; |
a1836a42d [PATCH] SELinux: ... |
167 168 169 |
out: return ret; } |
e014ff8d4 [PATCH] uninline ... |
170 171 172 173 |
int ioprio_best(unsigned short aprio, unsigned short bprio) { unsigned short aclass = IOPRIO_PRIO_CLASS(aprio); unsigned short bclass = IOPRIO_PRIO_CLASS(bprio); |
e014ff8d4 [PATCH] uninline ... |
174 175 176 177 178 179 180 181 182 183 184 185 |
if (aclass == IOPRIO_CLASS_NONE) aclass = IOPRIO_CLASS_BE; if (bclass == IOPRIO_CLASS_NONE) bclass = IOPRIO_CLASS_BE; if (aclass == bclass) return min(aprio, bprio); if (aclass > bclass) return bprio; else return aprio; } |
938bb9f5e [CVE-2009-0029] S... |
186 |
SYSCALL_DEFINE2(ioprio_get, int, which, int, who) |
22e2c507c [PATCH] Update cf... |
187 188 189 |
{ struct task_struct *g, *p; struct user_struct *user; |
41487c65b [PATCH] pid: repl... |
190 |
struct pid *pgrp; |
22e2c507c [PATCH] Update cf... |
191 |
int ret = -ESRCH; |
a1836a42d [PATCH] SELinux: ... |
192 |
int tmpio; |
22e2c507c [PATCH] Update cf... |
193 |
|
cf342e52e [PATCH] Don't nee... |
194 |
read_lock(&tasklist_lock); |
22e2c507c [PATCH] Update cf... |
195 196 197 198 199 |
switch (which) { case IOPRIO_WHO_PROCESS: if (!who) p = current; else |
228ebcbe6 Uninline find_tas... |
200 |
p = find_task_by_vpid(who); |
22e2c507c [PATCH] Update cf... |
201 |
if (p) |
a1836a42d [PATCH] SELinux: ... |
202 |
ret = get_task_ioprio(p); |
22e2c507c [PATCH] Update cf... |
203 204 205 |
break; case IOPRIO_WHO_PGRP: if (!who) |
41487c65b [PATCH] pid: repl... |
206 207 |
pgrp = task_pgrp(current); else |
b488893a3 pid namespaces: c... |
208 |
pgrp = find_vpid(who); |
2d70b68d4 fix setpriority(P... |
209 |
do_each_pid_thread(pgrp, PIDTYPE_PGID, p) { |
a1836a42d [PATCH] SELinux: ... |
210 211 212 |
tmpio = get_task_ioprio(p); if (tmpio < 0) continue; |
22e2c507c [PATCH] Update cf... |
213 |
if (ret == -ESRCH) |
a1836a42d [PATCH] SELinux: ... |
214 |
ret = tmpio; |
22e2c507c [PATCH] Update cf... |
215 |
else |
a1836a42d [PATCH] SELinux: ... |
216 |
ret = ioprio_best(ret, tmpio); |
2d70b68d4 fix setpriority(P... |
217 |
} while_each_pid_thread(pgrp, PIDTYPE_PGID, p); |
22e2c507c [PATCH] Update cf... |
218 219 220 |
break; case IOPRIO_WHO_USER: if (!who) |
86a264abe CRED: Wrap curren... |
221 |
user = current_user(); |
22e2c507c [PATCH] Update cf... |
222 223 224 225 226 227 228 |
else user = find_user(who); if (!user) break; do_each_thread(g, p) { |
c69e8d9c0 CRED: Use RCU to ... |
229 |
if (__task_cred(p)->uid != user->uid) |
22e2c507c [PATCH] Update cf... |
230 |
continue; |
a1836a42d [PATCH] SELinux: ... |
231 232 233 |
tmpio = get_task_ioprio(p); if (tmpio < 0) continue; |
22e2c507c [PATCH] Update cf... |
234 |
if (ret == -ESRCH) |
a1836a42d [PATCH] SELinux: ... |
235 |
ret = tmpio; |
22e2c507c [PATCH] Update cf... |
236 |
else |
a1836a42d [PATCH] SELinux: ... |
237 |
ret = ioprio_best(ret, tmpio); |
22e2c507c [PATCH] Update cf... |
238 239 240 241 242 243 244 245 |
} while_each_thread(g, p); if (who) free_uid(user); break; default: ret = -EINVAL; } |
cf342e52e [PATCH] Don't nee... |
246 |
read_unlock(&tasklist_lock); |
22e2c507c [PATCH] Update cf... |
247 248 |
return ret; } |