Blame view
block/ioprio.c
5.1 KB
3dcf60bcb
|
1 |
// SPDX-License-Identifier: GPL-2.0 |
22e2c507c
|
2 3 4 |
/* * fs/ioprio.c * |
0fe234795
|
5 |
* Copyright (C) 2004 Jens Axboe <axboe@kernel.dk> |
22e2c507c
|
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
|
20 |
* See also Documentation/block/ioprio.rst |
22e2c507c
|
21 22 |
* */ |
5a0e3ad6a
|
23 |
#include <linux/gfp.h> |
22e2c507c
|
24 |
#include <linux/kernel.h> |
afeacc8c1
|
25 |
#include <linux/export.h> |
22e2c507c
|
26 |
#include <linux/ioprio.h> |
5b825c3af
|
27 |
#include <linux/cred.h> |
22e2c507c
|
28 |
#include <linux/blkdev.h> |
16f7e0fe2
|
29 |
#include <linux/capability.h> |
8703e8a46
|
30 |
#include <linux/sched/user.h> |
f719ff9bc
|
31 |
#include <linux/sched/task.h> |
9abdc4cd8
|
32 |
#include <linux/syscalls.h> |
03e680606
|
33 |
#include <linux/security.h> |
b488893a3
|
34 |
#include <linux/pid_namespace.h> |
22e2c507c
|
35 |
|
b3881f74b
|
36 |
int set_task_ioprio(struct task_struct *task, int ioprio) |
22e2c507c
|
37 |
{ |
03e680606
|
38 |
int err; |
22e2c507c
|
39 |
struct io_context *ioc; |
c69e8d9c0
|
40 |
const struct cred *cred = current_cred(), *tcred; |
22e2c507c
|
41 |
|
c69e8d9c0
|
42 43 |
rcu_read_lock(); tcred = __task_cred(task); |
8e96e3b7b
|
44 45 |
if (!uid_eq(tcred->uid, cred->euid) && !uid_eq(tcred->uid, cred->uid) && !capable(CAP_SYS_NICE)) { |
c69e8d9c0
|
46 |
rcu_read_unlock(); |
22e2c507c
|
47 |
return -EPERM; |
c69e8d9c0
|
48 49 |
} rcu_read_unlock(); |
22e2c507c
|
50 |
|
03e680606
|
51 52 53 |
err = security_task_setioprio(task, ioprio); if (err) return err; |
6e736be7f
|
54 55 |
ioc = get_task_io_context(task, GFP_ATOMIC, NUMA_NO_NODE); if (ioc) { |
2b566fa55
|
56 |
ioc->ioprio = ioprio; |
11a3122f6
|
57 |
put_io_context(ioc); |
fd0928df9
|
58 |
} |
22e2c507c
|
59 |
|
fd0928df9
|
60 |
return err; |
22e2c507c
|
61 |
} |
b3881f74b
|
62 |
EXPORT_SYMBOL_GPL(set_task_ioprio); |
22e2c507c
|
63 |
|
aa4345779
|
64 |
int ioprio_check_cap(int ioprio) |
22e2c507c
|
65 66 67 |
{ int class = IOPRIO_PRIO_CLASS(ioprio); int data = IOPRIO_PRIO_DATA(ioprio); |
22e2c507c
|
68 69 70 71 72 |
switch (class) { case IOPRIO_CLASS_RT: if (!capable(CAP_SYS_ADMIN)) return -EPERM; |
e29387ebd
|
73 74 |
/* fall through */ /* rt has prio field too */ |
22e2c507c
|
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
|
82 83 84 85 |
case IOPRIO_CLASS_NONE: if (data) return -EINVAL; break; |
22e2c507c
|
86 87 88 |
default: return -EINVAL; } |
aa4345779
|
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
|
103 |
ret = -ESRCH; |
d69b78ba1
|
104 |
rcu_read_lock(); |
22e2c507c
|
105 106 107 108 109 |
switch (which) { case IOPRIO_WHO_PROCESS: if (!who) p = current; else |
228ebcbe6
|
110 |
p = find_task_by_vpid(who); |
22e2c507c
|
111 112 113 114 115 |
if (p) ret = set_task_ioprio(p, ioprio); break; case IOPRIO_WHO_PGRP: if (!who) |
41487c65b
|
116 117 |
pgrp = task_pgrp(current); else |
b488893a3
|
118 |
pgrp = find_vpid(who); |
2d70b68d4
|
119 |
do_each_pid_thread(pgrp, PIDTYPE_PGID, p) { |
22e2c507c
|
120 121 122 |
ret = set_task_ioprio(p, ioprio); if (ret) break; |
2d70b68d4
|
123 |
} while_each_pid_thread(pgrp, PIDTYPE_PGID, p); |
22e2c507c
|
124 125 |
break; case IOPRIO_WHO_USER: |
7b44ab978
|
126 127 128 |
uid = make_kuid(current_user_ns(), who); if (!uid_valid(uid)) break; |
22e2c507c
|
129 |
if (!who) |
86a264abe
|
130 |
user = current_user(); |
22e2c507c
|
131 |
else |
7b44ab978
|
132 |
user = find_user(uid); |
22e2c507c
|
133 134 135 |
if (!user) break; |
612dafabb
|
136 |
for_each_process_thread(g, p) { |
8639b4613
|
137 138 |
if (!uid_eq(task_uid(p), uid) || !task_pid_vnr(p)) |
22e2c507c
|
139 140 141 |
continue; ret = set_task_ioprio(p, ioprio); if (ret) |
78bd4d484
|
142 |
goto free_uid; |
612dafabb
|
143 |
} |
78bd4d484
|
144 |
free_uid: |
22e2c507c
|
145 146 147 148 149 150 |
if (who) free_uid(user); break; default: ret = -EINVAL; } |
d69b78ba1
|
151 |
rcu_read_unlock(); |
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 |
ret = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_NONE, IOPRIO_NORM); |
8ba868210
|
162 |
task_lock(p); |
fd0928df9
|
163 164 |
if (p->io_context) ret = p->io_context->ioprio; |
8ba868210
|
165 |
task_unlock(p); |
a1836a42d
|
166 167 168 |
out: return ret; } |
e014ff8d4
|
169 170 |
int ioprio_best(unsigned short aprio, unsigned short bprio) { |
ece9c72ac
|
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
|
175 |
|
9a87182c4
|
176 |
return min(aprio, bprio); |
e014ff8d4
|
177 |
} |
938bb9f5e
|
178 |
SYSCALL_DEFINE2(ioprio_get, int, which, int, who) |
22e2c507c
|
179 180 181 |
{ struct task_struct *g, *p; struct user_struct *user; |
41487c65b
|
182 |
struct pid *pgrp; |
7b44ab978
|
183 |
kuid_t uid; |
22e2c507c
|
184 |
int ret = -ESRCH; |
a1836a42d
|
185 |
int tmpio; |
22e2c507c
|
186 |
|
d69b78ba1
|
187 |
rcu_read_lock(); |
22e2c507c
|
188 189 190 191 192 |
switch (which) { case IOPRIO_WHO_PROCESS: if (!who) p = current; else |
228ebcbe6
|
193 |
p = find_task_by_vpid(who); |
22e2c507c
|
194 |
if (p) |
a1836a42d
|
195 |
ret = get_task_ioprio(p); |
22e2c507c
|
196 197 198 |
break; case IOPRIO_WHO_PGRP: if (!who) |
41487c65b
|
199 200 |
pgrp = task_pgrp(current); else |
b488893a3
|
201 |
pgrp = find_vpid(who); |
2d70b68d4
|
202 |
do_each_pid_thread(pgrp, PIDTYPE_PGID, p) { |
a1836a42d
|
203 204 205 |
tmpio = get_task_ioprio(p); if (tmpio < 0) continue; |
22e2c507c
|
206 |
if (ret == -ESRCH) |
a1836a42d
|
207 |
ret = tmpio; |
22e2c507c
|
208 |
else |
a1836a42d
|
209 |
ret = ioprio_best(ret, tmpio); |
2d70b68d4
|
210 |
} while_each_pid_thread(pgrp, PIDTYPE_PGID, p); |
22e2c507c
|
211 212 |
break; case IOPRIO_WHO_USER: |
7b44ab978
|
213 |
uid = make_kuid(current_user_ns(), who); |
22e2c507c
|
214 |
if (!who) |
86a264abe
|
215 |
user = current_user(); |
22e2c507c
|
216 |
else |
7b44ab978
|
217 |
user = find_user(uid); |
22e2c507c
|
218 219 220 |
if (!user) break; |
612dafabb
|
221 |
for_each_process_thread(g, p) { |
8639b4613
|
222 223 |
if (!uid_eq(task_uid(p), user->uid) || !task_pid_vnr(p)) |
22e2c507c
|
224 |
continue; |
a1836a42d
|
225 226 227 |
tmpio = get_task_ioprio(p); if (tmpio < 0) continue; |
22e2c507c
|
228 |
if (ret == -ESRCH) |
a1836a42d
|
229 |
ret = tmpio; |
22e2c507c
|
230 |
else |
a1836a42d
|
231 |
ret = ioprio_best(ret, tmpio); |
612dafabb
|
232 |
} |
22e2c507c
|
233 234 235 236 237 238 239 |
if (who) free_uid(user); break; default: ret = -EINVAL; } |
d69b78ba1
|
240 |
rcu_read_unlock(); |
22e2c507c
|
241 242 |
return ret; } |