Blame view
fs/ioprio.c
5 KB
22e2c507c
|
1 2 3 |
/* * fs/ioprio.c * |
0fe234795
|
4 |
* Copyright (C) 2004 Jens Axboe <axboe@kernel.dk> |
22e2c507c
|
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
|
22 |
#include <linux/gfp.h> |
22e2c507c
|
23 24 25 |
#include <linux/kernel.h> #include <linux/ioprio.h> #include <linux/blkdev.h> |
16f7e0fe2
|
26 |
#include <linux/capability.h> |
9abdc4cd8
|
27 |
#include <linux/syscalls.h> |
03e680606
|
28 |
#include <linux/security.h> |
b488893a3
|
29 |
#include <linux/pid_namespace.h> |
22e2c507c
|
30 |
|
b3881f74b
|
31 |
int set_task_ioprio(struct task_struct *task, int ioprio) |
22e2c507c
|
32 |
{ |
03e680606
|
33 |
int err; |
22e2c507c
|
34 |
struct io_context *ioc; |
c69e8d9c0
|
35 |
const struct cred *cred = current_cred(), *tcred; |
22e2c507c
|
36 |
|
c69e8d9c0
|
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
|
42 |
return -EPERM; |
c69e8d9c0
|
43 44 |
} rcu_read_unlock(); |
22e2c507c
|
45 |
|
03e680606
|
46 47 48 |
err = security_task_setioprio(task, ioprio); if (err) return err; |
22e2c507c
|
49 |
task_lock(task); |
fd0928df9
|
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
|
56 |
|
fd0928df9
|
57 58 59 60 61 62 |
ioc = alloc_io_context(GFP_ATOMIC, -1); if (!ioc) { err = -ENOMEM; break; } task->io_context = ioc; |
fd0928df9
|
63 |
} while (1); |
9f83e45eb
|
64 |
|
fd0928df9
|
65 66 |
if (!err) { ioc->ioprio = ioprio; |
fc46379da
|
67 |
ioc->ioprio_changed = 1; |
fd0928df9
|
68 |
} |
22e2c507c
|
69 70 |
task_unlock(task); |
fd0928df9
|
71 |
return err; |
22e2c507c
|
72 |
} |
b3881f74b
|
73 |
EXPORT_SYMBOL_GPL(set_task_ioprio); |
22e2c507c
|
74 |
|
938bb9f5e
|
75 |
SYSCALL_DEFINE3(ioprio_set, int, which, int, who, int, ioprio) |
22e2c507c
|
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
|
81 |
struct pid *pgrp; |
22e2c507c
|
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
|
96 97 98 99 |
case IOPRIO_CLASS_NONE: if (data) return -EINVAL; break; |
22e2c507c
|
100 101 102 103 104 |
default: return -EINVAL; } ret = -ESRCH; |
d69b78ba1
|
105 |
rcu_read_lock(); |
22e2c507c
|
106 107 108 109 110 |
switch (which) { case IOPRIO_WHO_PROCESS: if (!who) p = current; else |
228ebcbe6
|
111 |
p = find_task_by_vpid(who); |
22e2c507c
|
112 113 114 115 116 |
if (p) ret = set_task_ioprio(p, ioprio); break; case IOPRIO_WHO_PGRP: if (!who) |
41487c65b
|
117 118 |
pgrp = task_pgrp(current); else |
b488893a3
|
119 |
pgrp = find_vpid(who); |
2d70b68d4
|
120 |
do_each_pid_thread(pgrp, PIDTYPE_PGID, p) { |
22e2c507c
|
121 122 123 |
ret = set_task_ioprio(p, ioprio); if (ret) break; |
2d70b68d4
|
124 |
} while_each_pid_thread(pgrp, PIDTYPE_PGID, p); |
22e2c507c
|
125 126 127 |
break; case IOPRIO_WHO_USER: if (!who) |
86a264abe
|
128 |
user = current_user(); |
22e2c507c
|
129 130 131 132 133 134 135 |
else user = find_user(who); if (!user) break; do_each_thread(g, p) { |
d69b78ba1
|
136 |
if (__task_cred(p)->uid != who) |
22e2c507c
|
137 138 139 |
continue; ret = set_task_ioprio(p, ioprio); if (ret) |
78bd4d484
|
140 |
goto free_uid; |
22e2c507c
|
141 |
} while_each_thread(g, p); |
78bd4d484
|
142 |
free_uid: |
22e2c507c
|
143 144 145 146 147 148 |
if (who) free_uid(user); break; default: ret = -EINVAL; } |
d69b78ba1
|
149 |
rcu_read_unlock(); |
22e2c507c
|
150 151 |
return ret; } |
a1836a42d
|
152 153 154 155 156 157 158 |
static int get_task_ioprio(struct task_struct *p) { int ret; ret = security_task_getioprio(p); if (ret) goto out; |
fd0928df9
|
159 160 161 |
ret = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_NONE, IOPRIO_NORM); if (p->io_context) ret = p->io_context->ioprio; |
a1836a42d
|
162 163 164 |
out: return ret; } |
e014ff8d4
|
165 166 167 168 |
int ioprio_best(unsigned short aprio, unsigned short bprio) { unsigned short aclass = IOPRIO_PRIO_CLASS(aprio); unsigned short bclass = IOPRIO_PRIO_CLASS(bprio); |
e014ff8d4
|
169 170 171 172 173 174 175 176 177 178 179 180 |
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
|
181 |
SYSCALL_DEFINE2(ioprio_get, int, which, int, who) |
22e2c507c
|
182 183 184 |
{ struct task_struct *g, *p; struct user_struct *user; |
41487c65b
|
185 |
struct pid *pgrp; |
22e2c507c
|
186 |
int ret = -ESRCH; |
a1836a42d
|
187 |
int tmpio; |
22e2c507c
|
188 |
|
d69b78ba1
|
189 |
rcu_read_lock(); |
22e2c507c
|
190 191 192 193 194 |
switch (which) { case IOPRIO_WHO_PROCESS: if (!who) p = current; else |
228ebcbe6
|
195 |
p = find_task_by_vpid(who); |
22e2c507c
|
196 |
if (p) |
a1836a42d
|
197 |
ret = get_task_ioprio(p); |
22e2c507c
|
198 199 200 |
break; case IOPRIO_WHO_PGRP: if (!who) |
41487c65b
|
201 202 |
pgrp = task_pgrp(current); else |
b488893a3
|
203 |
pgrp = find_vpid(who); |
2d70b68d4
|
204 |
do_each_pid_thread(pgrp, PIDTYPE_PGID, p) { |
a1836a42d
|
205 206 207 |
tmpio = get_task_ioprio(p); if (tmpio < 0) continue; |
22e2c507c
|
208 |
if (ret == -ESRCH) |
a1836a42d
|
209 |
ret = tmpio; |
22e2c507c
|
210 |
else |
a1836a42d
|
211 |
ret = ioprio_best(ret, tmpio); |
2d70b68d4
|
212 |
} while_each_pid_thread(pgrp, PIDTYPE_PGID, p); |
22e2c507c
|
213 214 215 |
break; case IOPRIO_WHO_USER: if (!who) |
86a264abe
|
216 |
user = current_user(); |
22e2c507c
|
217 218 219 220 221 222 223 |
else user = find_user(who); if (!user) break; do_each_thread(g, p) { |
d69b78ba1
|
224 |
if (__task_cred(p)->uid != user->uid) |
22e2c507c
|
225 |
continue; |
a1836a42d
|
226 227 228 |
tmpio = get_task_ioprio(p); if (tmpio < 0) continue; |
22e2c507c
|
229 |
if (ret == -ESRCH) |
a1836a42d
|
230 |
ret = tmpio; |
22e2c507c
|
231 |
else |
a1836a42d
|
232 |
ret = ioprio_best(ret, tmpio); |
22e2c507c
|
233 234 235 236 237 238 239 240 |
} while_each_thread(g, p); if (who) free_uid(user); break; default: ret = -EINVAL; } |
d69b78ba1
|
241 |
rcu_read_unlock(); |
22e2c507c
|
242 243 |
return ret; } |