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