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