Blame view
block/ioprio.c
5.1 KB
3dcf60bcb block: add SPDX t... |
1 |
// SPDX-License-Identifier: GPL-2.0 |
22e2c507c [PATCH] Update cf... |
2 3 4 |
/* * fs/ioprio.c * |
0fe234795 [PATCH] Update ax... |
5 |
* Copyright (C) 2004 Jens Axboe <axboe@kernel.dk> |
22e2c507c [PATCH] Update cf... |
6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
* * 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); * |
898bd37a9 docs: block: conv... |
20 |
* See also Documentation/block/ioprio.rst |
22e2c507c [PATCH] Update cf... |
21 22 |
* */ |
5a0e3ad6a include cleanup: ... |
23 |
#include <linux/gfp.h> |
22e2c507c [PATCH] Update cf... |
24 |
#include <linux/kernel.h> |
afeacc8c1 fs: add export.h ... |
25 |
#include <linux/export.h> |
22e2c507c [PATCH] Update cf... |
26 |
#include <linux/ioprio.h> |
5b825c3af sched/headers: Pr... |
27 |
#include <linux/cred.h> |
22e2c507c [PATCH] Update cf... |
28 |
#include <linux/blkdev.h> |
16f7e0fe2 [PATCH] capable/c... |
29 |
#include <linux/capability.h> |
8703e8a46 sched/headers: Pr... |
30 |
#include <linux/sched/user.h> |
f719ff9bc sched/headers: Pr... |
31 |
#include <linux/sched/task.h> |
9abdc4cd8 fs/ioprio.c shoul... |
32 |
#include <linux/syscalls.h> |
03e680606 [PATCH] lsm: add ... |
33 |
#include <linux/security.h> |
b488893a3 pid namespaces: c... |
34 |
#include <linux/pid_namespace.h> |
22e2c507c [PATCH] Update cf... |
35 |
|
b3881f74b ext4: Add mount o... |
36 |
int set_task_ioprio(struct task_struct *task, int ioprio) |
22e2c507c [PATCH] Update cf... |
37 |
{ |
03e680606 [PATCH] lsm: add ... |
38 |
int err; |
22e2c507c [PATCH] Update cf... |
39 |
struct io_context *ioc; |
c69e8d9c0 CRED: Use RCU to ... |
40 |
const struct cred *cred = current_cred(), *tcred; |
22e2c507c [PATCH] Update cf... |
41 |
|
c69e8d9c0 CRED: Use RCU to ... |
42 43 |
rcu_read_lock(); tcred = __task_cred(task); |
8e96e3b7b userns: Use uid_e... |
44 45 |
if (!uid_eq(tcred->uid, cred->euid) && !uid_eq(tcred->uid, cred->uid) && !capable(CAP_SYS_NICE)) { |
c69e8d9c0 CRED: Use RCU to ... |
46 |
rcu_read_unlock(); |
22e2c507c [PATCH] Update cf... |
47 |
return -EPERM; |
c69e8d9c0 CRED: Use RCU to ... |
48 49 |
} rcu_read_unlock(); |
22e2c507c [PATCH] Update cf... |
50 |
|
03e680606 [PATCH] lsm: add ... |
51 52 53 |
err = security_task_setioprio(task, ioprio); if (err) return err; |
6e736be7f block: make ioc g... |
54 55 |
ioc = get_task_io_context(task, GFP_ATOMIC, NUMA_NO_NODE); if (ioc) { |
2b566fa55 block: remove ioc... |
56 |
ioc->ioprio = ioprio; |
11a3122f6 block: strip out ... |
57 |
put_io_context(ioc); |
fd0928df9 ioprio: move io p... |
58 |
} |
22e2c507c [PATCH] Update cf... |
59 |
|
fd0928df9 ioprio: move io p... |
60 |
return err; |
22e2c507c [PATCH] Update cf... |
61 |
} |
b3881f74b ext4: Add mount o... |
62 |
EXPORT_SYMBOL_GPL(set_task_ioprio); |
22e2c507c [PATCH] Update cf... |
63 |
|
aa4345779 block: add ioprio... |
64 |
int ioprio_check_cap(int ioprio) |
22e2c507c [PATCH] Update cf... |
65 66 67 |
{ int class = IOPRIO_PRIO_CLASS(ioprio); int data = IOPRIO_PRIO_DATA(ioprio); |
22e2c507c [PATCH] Update cf... |
68 69 70 |
switch (class) { case IOPRIO_CLASS_RT: |
35cda5af6 Revert "block: gr... |
71 |
if (!capable(CAP_SYS_ADMIN)) |
22e2c507c [PATCH] Update cf... |
72 |
return -EPERM; |
df561f668 treewide: Use fal... |
73 |
fallthrough; |
e29387ebd block: Add fallth... |
74 |
/* rt has prio field too */ |
22e2c507c [PATCH] Update cf... |
75 76 77 78 79 80 81 |
case IOPRIO_CLASS_BE: if (data >= IOPRIO_BE_NR || data < 0) return -EINVAL; break; case IOPRIO_CLASS_IDLE: break; |
8ec680e4c ioprio: allow sys... |
82 83 84 85 |
case IOPRIO_CLASS_NONE: if (data) return -EINVAL; break; |
22e2c507c [PATCH] Update cf... |
86 87 88 |
default: return -EINVAL; } |
aa4345779 block: add ioprio... |
89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
return 0; } SYSCALL_DEFINE3(ioprio_set, int, which, int, who, int, ioprio) { struct task_struct *p, *g; struct user_struct *user; struct pid *pgrp; kuid_t uid; int ret; ret = ioprio_check_cap(ioprio); if (ret) return ret; |
22e2c507c [PATCH] Update cf... |
103 |
ret = -ESRCH; |
d69b78ba1 ioprio: grab rcu_... |
104 |
rcu_read_lock(); |
22e2c507c [PATCH] Update cf... |
105 106 107 108 109 |
switch (which) { case IOPRIO_WHO_PROCESS: if (!who) p = current; else |
228ebcbe6 Uninline find_tas... |
110 |
p = find_task_by_vpid(who); |
22e2c507c [PATCH] Update cf... |
111 112 113 114 115 |
if (p) ret = set_task_ioprio(p, ioprio); break; case IOPRIO_WHO_PGRP: if (!who) |
41487c65b [PATCH] pid: repl... |
116 117 |
pgrp = task_pgrp(current); else |
b488893a3 pid namespaces: c... |
118 |
pgrp = find_vpid(who); |
2d70b68d4 fix setpriority(P... |
119 |
do_each_pid_thread(pgrp, PIDTYPE_PGID, p) { |
22e2c507c [PATCH] Update cf... |
120 121 122 |
ret = set_task_ioprio(p, ioprio); if (ret) break; |
2d70b68d4 fix setpriority(P... |
123 |
} while_each_pid_thread(pgrp, PIDTYPE_PGID, p); |
22e2c507c [PATCH] Update cf... |
124 125 |
break; case IOPRIO_WHO_USER: |
7b44ab978 userns: Disassoci... |
126 127 128 |
uid = make_kuid(current_user_ns(), who); if (!uid_valid(uid)) break; |
22e2c507c [PATCH] Update cf... |
129 |
if (!who) |
86a264abe CRED: Wrap curren... |
130 |
user = current_user(); |
22e2c507c [PATCH] Update cf... |
131 |
else |
7b44ab978 userns: Disassoci... |
132 |
user = find_user(uid); |
22e2c507c [PATCH] Update cf... |
133 134 135 |
if (!user) break; |
612dafabb block: use for_ea... |
136 |
for_each_process_thread(g, p) { |
8639b4613 pidns: fix set/ge... |
137 138 |
if (!uid_eq(task_uid(p), uid) || !task_pid_vnr(p)) |
22e2c507c [PATCH] Update cf... |
139 140 141 |
continue; ret = set_task_ioprio(p, ioprio); if (ret) |
78bd4d484 [PATCH] sys_iopri... |
142 |
goto free_uid; |
612dafabb block: use for_ea... |
143 |
} |
78bd4d484 [PATCH] sys_iopri... |
144 |
free_uid: |
22e2c507c [PATCH] Update cf... |
145 146 147 148 149 150 |
if (who) free_uid(user); break; default: ret = -EINVAL; } |
d69b78ba1 ioprio: grab rcu_... |
151 |
rcu_read_unlock(); |
22e2c507c [PATCH] Update cf... |
152 153 |
return ret; } |
a1836a42d [PATCH] SELinux: ... |
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 ioprio: move io p... |
161 |
ret = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_NONE, IOPRIO_NORM); |
8ba868210 block: fix use-af... |
162 |
task_lock(p); |
fd0928df9 ioprio: move io p... |
163 164 |
if (p->io_context) ret = p->io_context->ioprio; |
8ba868210 block: fix use-af... |
165 |
task_unlock(p); |
a1836a42d [PATCH] SELinux: ... |
166 167 168 |
out: return ret; } |
e014ff8d4 [PATCH] uninline ... |
169 170 |
int ioprio_best(unsigned short aprio, unsigned short bprio) { |
ece9c72ac block: Fix comput... |
171 172 173 174 |
if (!ioprio_valid(aprio)) aprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, IOPRIO_NORM); if (!ioprio_valid(bprio)) bprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, IOPRIO_NORM); |
e014ff8d4 [PATCH] uninline ... |
175 |
|
9a87182c4 block: Optimize i... |
176 |
return min(aprio, bprio); |
e014ff8d4 [PATCH] uninline ... |
177 |
} |
938bb9f5e [CVE-2009-0029] S... |
178 |
SYSCALL_DEFINE2(ioprio_get, int, which, int, who) |
22e2c507c [PATCH] Update cf... |
179 180 181 |
{ struct task_struct *g, *p; struct user_struct *user; |
41487c65b [PATCH] pid: repl... |
182 |
struct pid *pgrp; |
7b44ab978 userns: Disassoci... |
183 |
kuid_t uid; |
22e2c507c [PATCH] Update cf... |
184 |
int ret = -ESRCH; |
a1836a42d [PATCH] SELinux: ... |
185 |
int tmpio; |
22e2c507c [PATCH] Update cf... |
186 |
|
d69b78ba1 ioprio: grab rcu_... |
187 |
rcu_read_lock(); |
22e2c507c [PATCH] Update cf... |
188 189 190 191 192 |
switch (which) { case IOPRIO_WHO_PROCESS: if (!who) p = current; else |
228ebcbe6 Uninline find_tas... |
193 |
p = find_task_by_vpid(who); |
22e2c507c [PATCH] Update cf... |
194 |
if (p) |
a1836a42d [PATCH] SELinux: ... |
195 |
ret = get_task_ioprio(p); |
22e2c507c [PATCH] Update cf... |
196 197 198 |
break; case IOPRIO_WHO_PGRP: if (!who) |
41487c65b [PATCH] pid: repl... |
199 200 |
pgrp = task_pgrp(current); else |
b488893a3 pid namespaces: c... |
201 |
pgrp = find_vpid(who); |
2d70b68d4 fix setpriority(P... |
202 |
do_each_pid_thread(pgrp, PIDTYPE_PGID, p) { |
a1836a42d [PATCH] SELinux: ... |
203 204 205 |
tmpio = get_task_ioprio(p); if (tmpio < 0) continue; |
22e2c507c [PATCH] Update cf... |
206 |
if (ret == -ESRCH) |
a1836a42d [PATCH] SELinux: ... |
207 |
ret = tmpio; |
22e2c507c [PATCH] Update cf... |
208 |
else |
a1836a42d [PATCH] SELinux: ... |
209 |
ret = ioprio_best(ret, tmpio); |
2d70b68d4 fix setpriority(P... |
210 |
} while_each_pid_thread(pgrp, PIDTYPE_PGID, p); |
22e2c507c [PATCH] Update cf... |
211 212 |
break; case IOPRIO_WHO_USER: |
7b44ab978 userns: Disassoci... |
213 |
uid = make_kuid(current_user_ns(), who); |
22e2c507c [PATCH] Update cf... |
214 |
if (!who) |
86a264abe CRED: Wrap curren... |
215 |
user = current_user(); |
22e2c507c [PATCH] Update cf... |
216 |
else |
7b44ab978 userns: Disassoci... |
217 |
user = find_user(uid); |
22e2c507c [PATCH] Update cf... |
218 219 220 |
if (!user) break; |
612dafabb block: use for_ea... |
221 |
for_each_process_thread(g, p) { |
8639b4613 pidns: fix set/ge... |
222 223 |
if (!uid_eq(task_uid(p), user->uid) || !task_pid_vnr(p)) |
22e2c507c [PATCH] Update cf... |
224 |
continue; |
a1836a42d [PATCH] SELinux: ... |
225 226 227 |
tmpio = get_task_ioprio(p); if (tmpio < 0) continue; |
22e2c507c [PATCH] Update cf... |
228 |
if (ret == -ESRCH) |
a1836a42d [PATCH] SELinux: ... |
229 |
ret = tmpio; |
22e2c507c [PATCH] Update cf... |
230 |
else |
a1836a42d [PATCH] SELinux: ... |
231 |
ret = ioprio_best(ret, tmpio); |
612dafabb block: use for_ea... |
232 |
} |
22e2c507c [PATCH] Update cf... |
233 234 235 236 237 238 239 |
if (who) free_uid(user); break; default: ret = -EINVAL; } |
d69b78ba1 ioprio: grab rcu_... |
240 |
rcu_read_unlock(); |
22e2c507c [PATCH] Update cf... |
241 242 |
return ret; } |