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