]>
Commit | Line | Data |
---|---|---|
3dcf60bc | 1 | // SPDX-License-Identifier: GPL-2.0 |
22e2c507 JA |
2 | /* |
3 | * fs/ioprio.c | |
4 | * | |
0fe23479 | 5 | * Copyright (C) 2004 Jens Axboe <axboe@kernel.dk> |
22e2c507 JA |
6 | * |
7 | * Helper functions for setting/querying io priorities of processes. The | |
8 | * system calls closely mimmick getpriority/setpriority, see the man page for | |
9 | * those. The prio argument is a composite of prio class and prio data, where | |
10 | * the data argument has meaning within that class. The standard scheduling | |
11 | * classes have 8 distinct prio levels, with 0 being the highest prio and 7 | |
12 | * being the lowest. | |
13 | * | |
14 | * IOW, setting BE scheduling class with prio 2 is done ala: | |
15 | * | |
16 | * unsigned int prio = (IOPRIO_CLASS_BE << IOPRIO_CLASS_SHIFT) | 2; | |
17 | * | |
18 | * ioprio_set(PRIO_PROCESS, pid, prio); | |
19 | * | |
898bd37a | 20 | * See also Documentation/block/ioprio.rst |
22e2c507 JA |
21 | * |
22 | */ | |
5a0e3ad6 | 23 | #include <linux/gfp.h> |
22e2c507 | 24 | #include <linux/kernel.h> |
afeacc8c | 25 | #include <linux/export.h> |
22e2c507 | 26 | #include <linux/ioprio.h> |
5b825c3a | 27 | #include <linux/cred.h> |
22e2c507 | 28 | #include <linux/blkdev.h> |
16f7e0fe | 29 | #include <linux/capability.h> |
8703e8a4 | 30 | #include <linux/sched/user.h> |
f719ff9b | 31 | #include <linux/sched/task.h> |
9abdc4cd | 32 | #include <linux/syscalls.h> |
03e68060 | 33 | #include <linux/security.h> |
b488893a | 34 | #include <linux/pid_namespace.h> |
22e2c507 | 35 | |
b3881f74 | 36 | int set_task_ioprio(struct task_struct *task, int ioprio) |
22e2c507 | 37 | { |
03e68060 | 38 | int err; |
22e2c507 | 39 | struct io_context *ioc; |
c69e8d9c | 40 | const struct cred *cred = current_cred(), *tcred; |
22e2c507 | 41 | |
c69e8d9c DH |
42 | rcu_read_lock(); |
43 | tcred = __task_cred(task); | |
8e96e3b7 EB |
44 | if (!uid_eq(tcred->uid, cred->euid) && |
45 | !uid_eq(tcred->uid, cred->uid) && !capable(CAP_SYS_NICE)) { | |
c69e8d9c | 46 | rcu_read_unlock(); |
22e2c507 | 47 | return -EPERM; |
c69e8d9c DH |
48 | } |
49 | rcu_read_unlock(); | |
22e2c507 | 50 | |
03e68060 JM |
51 | err = security_task_setioprio(task, ioprio); |
52 | if (err) | |
53 | return err; | |
54 | ||
6e736be7 TH |
55 | ioc = get_task_io_context(task, GFP_ATOMIC, NUMA_NO_NODE); |
56 | if (ioc) { | |
2b566fa5 | 57 | ioc->ioprio = ioprio; |
11a3122f | 58 | put_io_context(ioc); |
fd0928df | 59 | } |
22e2c507 | 60 | |
fd0928df | 61 | return err; |
22e2c507 | 62 | } |
b3881f74 | 63 | EXPORT_SYMBOL_GPL(set_task_ioprio); |
22e2c507 | 64 | |
aa434577 | 65 | int ioprio_check_cap(int ioprio) |
22e2c507 JA |
66 | { |
67 | int class = IOPRIO_PRIO_CLASS(ioprio); | |
68 | int data = IOPRIO_PRIO_DATA(ioprio); | |
22e2c507 JA |
69 | |
70 | switch (class) { | |
71 | case IOPRIO_CLASS_RT: | |
6b332345 AD |
72 | /* |
73 | * Originally this only checked for CAP_SYS_ADMIN, | |
74 | * which was implicitly allowed for pid 0 by security | |
75 | * modules such as SELinux. Make sure we check | |
76 | * CAP_SYS_ADMIN first to avoid a denial/avc for | |
77 | * possibly missing CAP_SYS_NICE permission. | |
78 | */ | |
79 | if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_NICE)) | |
22e2c507 | 80 | return -EPERM; |
df561f66 | 81 | fallthrough; |
e29387eb | 82 | /* rt has prio field too */ |
22e2c507 | 83 | case IOPRIO_CLASS_BE: |
202bc942 | 84 | if (data >= IOPRIO_NR_LEVELS || data < 0) |
22e2c507 | 85 | return -EINVAL; |
22e2c507 JA |
86 | break; |
87 | case IOPRIO_CLASS_IDLE: | |
88 | break; | |
8ec680e4 JA |
89 | case IOPRIO_CLASS_NONE: |
90 | if (data) | |
91 | return -EINVAL; | |
92 | break; | |
22e2c507 JA |
93 | default: |
94 | return -EINVAL; | |
95 | } | |
96 | ||
aa434577 AM |
97 | return 0; |
98 | } | |
99 | ||
100 | SYSCALL_DEFINE3(ioprio_set, int, which, int, who, int, ioprio) | |
101 | { | |
102 | struct task_struct *p, *g; | |
103 | struct user_struct *user; | |
104 | struct pid *pgrp; | |
105 | kuid_t uid; | |
106 | int ret; | |
107 | ||
108 | ret = ioprio_check_cap(ioprio); | |
109 | if (ret) | |
110 | return ret; | |
111 | ||
22e2c507 | 112 | ret = -ESRCH; |
d69b78ba | 113 | rcu_read_lock(); |
22e2c507 JA |
114 | switch (which) { |
115 | case IOPRIO_WHO_PROCESS: | |
116 | if (!who) | |
117 | p = current; | |
118 | else | |
228ebcbe | 119 | p = find_task_by_vpid(who); |
22e2c507 JA |
120 | if (p) |
121 | ret = set_task_ioprio(p, ioprio); | |
122 | break; | |
123 | case IOPRIO_WHO_PGRP: | |
124 | if (!who) | |
41487c65 EB |
125 | pgrp = task_pgrp(current); |
126 | else | |
b488893a | 127 | pgrp = find_vpid(who); |
40c7fd3f PZ |
128 | |
129 | read_lock(&tasklist_lock); | |
2d70b68d | 130 | do_each_pid_thread(pgrp, PIDTYPE_PGID, p) { |
22e2c507 | 131 | ret = set_task_ioprio(p, ioprio); |
40c7fd3f PZ |
132 | if (ret) { |
133 | read_unlock(&tasklist_lock); | |
134 | goto out; | |
135 | } | |
2d70b68d | 136 | } while_each_pid_thread(pgrp, PIDTYPE_PGID, p); |
40c7fd3f PZ |
137 | read_unlock(&tasklist_lock); |
138 | ||
22e2c507 JA |
139 | break; |
140 | case IOPRIO_WHO_USER: | |
7b44ab97 EB |
141 | uid = make_kuid(current_user_ns(), who); |
142 | if (!uid_valid(uid)) | |
143 | break; | |
22e2c507 | 144 | if (!who) |
86a264ab | 145 | user = current_user(); |
22e2c507 | 146 | else |
7b44ab97 | 147 | user = find_user(uid); |
22e2c507 JA |
148 | |
149 | if (!user) | |
150 | break; | |
151 | ||
612dafab | 152 | for_each_process_thread(g, p) { |
8639b461 BS |
153 | if (!uid_eq(task_uid(p), uid) || |
154 | !task_pid_vnr(p)) | |
22e2c507 JA |
155 | continue; |
156 | ret = set_task_ioprio(p, ioprio); | |
157 | if (ret) | |
78bd4d48 | 158 | goto free_uid; |
612dafab | 159 | } |
78bd4d48 | 160 | free_uid: |
22e2c507 JA |
161 | if (who) |
162 | free_uid(user); | |
163 | break; | |
164 | default: | |
165 | ret = -EINVAL; | |
166 | } | |
167 | ||
40c7fd3f | 168 | out: |
d69b78ba | 169 | rcu_read_unlock(); |
22e2c507 JA |
170 | return ret; |
171 | } | |
172 | ||
a1836a42 DQ |
173 | static int get_task_ioprio(struct task_struct *p) |
174 | { | |
175 | int ret; | |
176 | ||
177 | ret = security_task_getioprio(p); | |
178 | if (ret) | |
179 | goto out; | |
e70344c0 | 180 | ret = IOPRIO_DEFAULT; |
8ba86821 | 181 | task_lock(p); |
fd0928df JA |
182 | if (p->io_context) |
183 | ret = p->io_context->ioprio; | |
8ba86821 | 184 | task_unlock(p); |
a1836a42 DQ |
185 | out: |
186 | return ret; | |
187 | } | |
188 | ||
e014ff8d ON |
189 | int ioprio_best(unsigned short aprio, unsigned short bprio) |
190 | { | |
ece9c72a | 191 | if (!ioprio_valid(aprio)) |
e70344c0 | 192 | aprio = IOPRIO_DEFAULT; |
ece9c72a | 193 | if (!ioprio_valid(bprio)) |
e70344c0 | 194 | bprio = IOPRIO_DEFAULT; |
e014ff8d | 195 | |
9a87182c | 196 | return min(aprio, bprio); |
e014ff8d ON |
197 | } |
198 | ||
938bb9f5 | 199 | SYSCALL_DEFINE2(ioprio_get, int, which, int, who) |
22e2c507 JA |
200 | { |
201 | struct task_struct *g, *p; | |
202 | struct user_struct *user; | |
41487c65 | 203 | struct pid *pgrp; |
7b44ab97 | 204 | kuid_t uid; |
22e2c507 | 205 | int ret = -ESRCH; |
a1836a42 | 206 | int tmpio; |
22e2c507 | 207 | |
d69b78ba | 208 | rcu_read_lock(); |
22e2c507 JA |
209 | switch (which) { |
210 | case IOPRIO_WHO_PROCESS: | |
211 | if (!who) | |
212 | p = current; | |
213 | else | |
228ebcbe | 214 | p = find_task_by_vpid(who); |
22e2c507 | 215 | if (p) |
a1836a42 | 216 | ret = get_task_ioprio(p); |
22e2c507 JA |
217 | break; |
218 | case IOPRIO_WHO_PGRP: | |
219 | if (!who) | |
41487c65 EB |
220 | pgrp = task_pgrp(current); |
221 | else | |
b488893a | 222 | pgrp = find_vpid(who); |
ebfae02f | 223 | read_lock(&tasklist_lock); |
2d70b68d | 224 | do_each_pid_thread(pgrp, PIDTYPE_PGID, p) { |
a1836a42 DQ |
225 | tmpio = get_task_ioprio(p); |
226 | if (tmpio < 0) | |
227 | continue; | |
22e2c507 | 228 | if (ret == -ESRCH) |
a1836a42 | 229 | ret = tmpio; |
22e2c507 | 230 | else |
a1836a42 | 231 | ret = ioprio_best(ret, tmpio); |
2d70b68d | 232 | } while_each_pid_thread(pgrp, PIDTYPE_PGID, p); |
ebfae02f DB |
233 | read_unlock(&tasklist_lock); |
234 | ||
22e2c507 JA |
235 | break; |
236 | case IOPRIO_WHO_USER: | |
7b44ab97 | 237 | uid = make_kuid(current_user_ns(), who); |
22e2c507 | 238 | if (!who) |
86a264ab | 239 | user = current_user(); |
22e2c507 | 240 | else |
7b44ab97 | 241 | user = find_user(uid); |
22e2c507 JA |
242 | |
243 | if (!user) | |
244 | break; | |
245 | ||
612dafab | 246 | for_each_process_thread(g, p) { |
8639b461 BS |
247 | if (!uid_eq(task_uid(p), user->uid) || |
248 | !task_pid_vnr(p)) | |
22e2c507 | 249 | continue; |
a1836a42 DQ |
250 | tmpio = get_task_ioprio(p); |
251 | if (tmpio < 0) | |
252 | continue; | |
22e2c507 | 253 | if (ret == -ESRCH) |
a1836a42 | 254 | ret = tmpio; |
22e2c507 | 255 | else |
a1836a42 | 256 | ret = ioprio_best(ret, tmpio); |
612dafab | 257 | } |
22e2c507 JA |
258 | |
259 | if (who) | |
260 | free_uid(user); | |
261 | break; | |
262 | default: | |
263 | ret = -EINVAL; | |
264 | } | |
265 | ||
d69b78ba | 266 | rcu_read_unlock(); |
22e2c507 JA |
267 | return ret; |
268 | } |