]>
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: | |
9d3a39a5 | 72 | if (!capable(CAP_SYS_NICE) && !capable(CAP_SYS_ADMIN)) |
22e2c507 | 73 | return -EPERM; |
df561f66 | 74 | fallthrough; |
e29387eb | 75 | /* rt has prio field too */ |
22e2c507 JA |
76 | case IOPRIO_CLASS_BE: |
77 | if (data >= IOPRIO_BE_NR || data < 0) | |
78 | return -EINVAL; | |
79 | ||
80 | break; | |
81 | case IOPRIO_CLASS_IDLE: | |
82 | break; | |
8ec680e4 JA |
83 | case IOPRIO_CLASS_NONE: |
84 | if (data) | |
85 | return -EINVAL; | |
86 | break; | |
22e2c507 JA |
87 | default: |
88 | return -EINVAL; | |
89 | } | |
90 | ||
aa434577 AM |
91 | return 0; |
92 | } | |
93 | ||
94 | SYSCALL_DEFINE3(ioprio_set, int, which, int, who, int, ioprio) | |
95 | { | |
96 | struct task_struct *p, *g; | |
97 | struct user_struct *user; | |
98 | struct pid *pgrp; | |
99 | kuid_t uid; | |
100 | int ret; | |
101 | ||
102 | ret = ioprio_check_cap(ioprio); | |
103 | if (ret) | |
104 | return ret; | |
105 | ||
22e2c507 | 106 | ret = -ESRCH; |
d69b78ba | 107 | rcu_read_lock(); |
22e2c507 JA |
108 | switch (which) { |
109 | case IOPRIO_WHO_PROCESS: | |
110 | if (!who) | |
111 | p = current; | |
112 | else | |
228ebcbe | 113 | p = find_task_by_vpid(who); |
22e2c507 JA |
114 | if (p) |
115 | ret = set_task_ioprio(p, ioprio); | |
116 | break; | |
117 | case IOPRIO_WHO_PGRP: | |
118 | if (!who) | |
41487c65 EB |
119 | pgrp = task_pgrp(current); |
120 | else | |
b488893a | 121 | pgrp = find_vpid(who); |
40c7fd3f PZ |
122 | |
123 | read_lock(&tasklist_lock); | |
2d70b68d | 124 | do_each_pid_thread(pgrp, PIDTYPE_PGID, p) { |
22e2c507 | 125 | ret = set_task_ioprio(p, ioprio); |
40c7fd3f PZ |
126 | if (ret) { |
127 | read_unlock(&tasklist_lock); | |
128 | goto out; | |
129 | } | |
2d70b68d | 130 | } while_each_pid_thread(pgrp, PIDTYPE_PGID, p); |
40c7fd3f PZ |
131 | read_unlock(&tasklist_lock); |
132 | ||
22e2c507 JA |
133 | break; |
134 | case IOPRIO_WHO_USER: | |
7b44ab97 EB |
135 | uid = make_kuid(current_user_ns(), who); |
136 | if (!uid_valid(uid)) | |
137 | break; | |
22e2c507 | 138 | if (!who) |
86a264ab | 139 | user = current_user(); |
22e2c507 | 140 | else |
7b44ab97 | 141 | user = find_user(uid); |
22e2c507 JA |
142 | |
143 | if (!user) | |
144 | break; | |
145 | ||
612dafab | 146 | for_each_process_thread(g, p) { |
8639b461 BS |
147 | if (!uid_eq(task_uid(p), uid) || |
148 | !task_pid_vnr(p)) | |
22e2c507 JA |
149 | continue; |
150 | ret = set_task_ioprio(p, ioprio); | |
151 | if (ret) | |
78bd4d48 | 152 | goto free_uid; |
612dafab | 153 | } |
78bd4d48 | 154 | free_uid: |
22e2c507 JA |
155 | if (who) |
156 | free_uid(user); | |
157 | break; | |
158 | default: | |
159 | ret = -EINVAL; | |
160 | } | |
161 | ||
40c7fd3f | 162 | out: |
d69b78ba | 163 | rcu_read_unlock(); |
22e2c507 JA |
164 | return ret; |
165 | } | |
166 | ||
a1836a42 DQ |
167 | static int get_task_ioprio(struct task_struct *p) |
168 | { | |
169 | int ret; | |
170 | ||
171 | ret = security_task_getioprio(p); | |
172 | if (ret) | |
173 | goto out; | |
fd0928df | 174 | ret = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_NONE, IOPRIO_NORM); |
8ba86821 | 175 | task_lock(p); |
fd0928df JA |
176 | if (p->io_context) |
177 | ret = p->io_context->ioprio; | |
8ba86821 | 178 | task_unlock(p); |
a1836a42 DQ |
179 | out: |
180 | return ret; | |
181 | } | |
182 | ||
e014ff8d ON |
183 | int ioprio_best(unsigned short aprio, unsigned short bprio) |
184 | { | |
ece9c72a JK |
185 | if (!ioprio_valid(aprio)) |
186 | aprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, IOPRIO_NORM); | |
187 | if (!ioprio_valid(bprio)) | |
188 | bprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, IOPRIO_NORM); | |
e014ff8d | 189 | |
9a87182c | 190 | return min(aprio, bprio); |
e014ff8d ON |
191 | } |
192 | ||
938bb9f5 | 193 | SYSCALL_DEFINE2(ioprio_get, int, which, int, who) |
22e2c507 JA |
194 | { |
195 | struct task_struct *g, *p; | |
196 | struct user_struct *user; | |
41487c65 | 197 | struct pid *pgrp; |
7b44ab97 | 198 | kuid_t uid; |
22e2c507 | 199 | int ret = -ESRCH; |
a1836a42 | 200 | int tmpio; |
22e2c507 | 201 | |
d69b78ba | 202 | rcu_read_lock(); |
22e2c507 JA |
203 | switch (which) { |
204 | case IOPRIO_WHO_PROCESS: | |
205 | if (!who) | |
206 | p = current; | |
207 | else | |
228ebcbe | 208 | p = find_task_by_vpid(who); |
22e2c507 | 209 | if (p) |
a1836a42 | 210 | ret = get_task_ioprio(p); |
22e2c507 JA |
211 | break; |
212 | case IOPRIO_WHO_PGRP: | |
213 | if (!who) | |
41487c65 EB |
214 | pgrp = task_pgrp(current); |
215 | else | |
b488893a | 216 | pgrp = find_vpid(who); |
2d70b68d | 217 | do_each_pid_thread(pgrp, PIDTYPE_PGID, p) { |
a1836a42 DQ |
218 | tmpio = get_task_ioprio(p); |
219 | if (tmpio < 0) | |
220 | continue; | |
22e2c507 | 221 | if (ret == -ESRCH) |
a1836a42 | 222 | ret = tmpio; |
22e2c507 | 223 | else |
a1836a42 | 224 | ret = ioprio_best(ret, tmpio); |
2d70b68d | 225 | } while_each_pid_thread(pgrp, PIDTYPE_PGID, p); |
22e2c507 JA |
226 | break; |
227 | case IOPRIO_WHO_USER: | |
7b44ab97 | 228 | uid = make_kuid(current_user_ns(), who); |
22e2c507 | 229 | if (!who) |
86a264ab | 230 | user = current_user(); |
22e2c507 | 231 | else |
7b44ab97 | 232 | user = find_user(uid); |
22e2c507 JA |
233 | |
234 | if (!user) | |
235 | break; | |
236 | ||
612dafab | 237 | for_each_process_thread(g, p) { |
8639b461 BS |
238 | if (!uid_eq(task_uid(p), user->uid) || |
239 | !task_pid_vnr(p)) | |
22e2c507 | 240 | continue; |
a1836a42 DQ |
241 | tmpio = get_task_ioprio(p); |
242 | if (tmpio < 0) | |
243 | continue; | |
22e2c507 | 244 | if (ret == -ESRCH) |
a1836a42 | 245 | ret = tmpio; |
22e2c507 | 246 | else |
a1836a42 | 247 | ret = ioprio_best(ret, tmpio); |
612dafab | 248 | } |
22e2c507 JA |
249 | |
250 | if (who) | |
251 | free_uid(user); | |
252 | break; | |
253 | default: | |
254 | ret = -EINVAL; | |
255 | } | |
256 | ||
d69b78ba | 257 | rcu_read_unlock(); |
22e2c507 JA |
258 | return ret; |
259 | } |