]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * fs/ioprio.c | |
3 | * | |
4 | * Copyright (C) 2004 Jens Axboe <axboe@kernel.dk> | |
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 | */ | |
22 | #include <linux/gfp.h> | |
23 | #include <linux/kernel.h> | |
24 | #include <linux/export.h> | |
25 | #include <linux/ioprio.h> | |
26 | #include <linux/blkdev.h> | |
27 | #include <linux/capability.h> | |
28 | #include <linux/sched/user.h> | |
29 | #include <linux/syscalls.h> | |
30 | #include <linux/security.h> | |
31 | #include <linux/pid_namespace.h> | |
32 | ||
33 | int set_task_ioprio(struct task_struct *task, int ioprio) | |
34 | { | |
35 | int err; | |
36 | struct io_context *ioc; | |
37 | const struct cred *cred = current_cred(), *tcred; | |
38 | ||
39 | rcu_read_lock(); | |
40 | tcred = __task_cred(task); | |
41 | if (!uid_eq(tcred->uid, cred->euid) && | |
42 | !uid_eq(tcred->uid, cred->uid) && !capable(CAP_SYS_NICE)) { | |
43 | rcu_read_unlock(); | |
44 | return -EPERM; | |
45 | } | |
46 | rcu_read_unlock(); | |
47 | ||
48 | err = security_task_setioprio(task, ioprio); | |
49 | if (err) | |
50 | return err; | |
51 | ||
52 | ioc = get_task_io_context(task, GFP_ATOMIC, NUMA_NO_NODE); | |
53 | if (ioc) { | |
54 | ioc->ioprio = ioprio; | |
55 | put_io_context(ioc); | |
56 | } | |
57 | ||
58 | return err; | |
59 | } | |
60 | EXPORT_SYMBOL_GPL(set_task_ioprio); | |
61 | ||
62 | SYSCALL_DEFINE3(ioprio_set, int, which, int, who, int, ioprio) | |
63 | { | |
64 | int class = IOPRIO_PRIO_CLASS(ioprio); | |
65 | int data = IOPRIO_PRIO_DATA(ioprio); | |
66 | struct task_struct *p, *g; | |
67 | struct user_struct *user; | |
68 | struct pid *pgrp; | |
69 | kuid_t uid; | |
70 | int ret; | |
71 | ||
72 | switch (class) { | |
73 | case IOPRIO_CLASS_RT: | |
74 | if (!capable(CAP_SYS_ADMIN)) | |
75 | return -EPERM; | |
76 | /* fall through, rt has prio field too */ | |
77 | case IOPRIO_CLASS_BE: | |
78 | if (data >= IOPRIO_BE_NR || data < 0) | |
79 | return -EINVAL; | |
80 | ||
81 | break; | |
82 | case IOPRIO_CLASS_IDLE: | |
83 | break; | |
84 | case IOPRIO_CLASS_NONE: | |
85 | if (data) | |
86 | return -EINVAL; | |
87 | break; | |
88 | default: | |
89 | return -EINVAL; | |
90 | } | |
91 | ||
92 | ret = -ESRCH; | |
93 | rcu_read_lock(); | |
94 | switch (which) { | |
95 | case IOPRIO_WHO_PROCESS: | |
96 | if (!who) | |
97 | p = current; | |
98 | else | |
99 | p = find_task_by_vpid(who); | |
100 | if (p) | |
101 | ret = set_task_ioprio(p, ioprio); | |
102 | break; | |
103 | case IOPRIO_WHO_PGRP: | |
104 | if (!who) | |
105 | pgrp = task_pgrp(current); | |
106 | else | |
107 | pgrp = find_vpid(who); | |
108 | do_each_pid_thread(pgrp, PIDTYPE_PGID, p) { | |
109 | ret = set_task_ioprio(p, ioprio); | |
110 | if (ret) | |
111 | break; | |
112 | } while_each_pid_thread(pgrp, PIDTYPE_PGID, p); | |
113 | break; | |
114 | case IOPRIO_WHO_USER: | |
115 | uid = make_kuid(current_user_ns(), who); | |
116 | if (!uid_valid(uid)) | |
117 | break; | |
118 | if (!who) | |
119 | user = current_user(); | |
120 | else | |
121 | user = find_user(uid); | |
122 | ||
123 | if (!user) | |
124 | break; | |
125 | ||
126 | for_each_process_thread(g, p) { | |
127 | if (!uid_eq(task_uid(p), uid) || | |
128 | !task_pid_vnr(p)) | |
129 | continue; | |
130 | ret = set_task_ioprio(p, ioprio); | |
131 | if (ret) | |
132 | goto free_uid; | |
133 | } | |
134 | free_uid: | |
135 | if (who) | |
136 | free_uid(user); | |
137 | break; | |
138 | default: | |
139 | ret = -EINVAL; | |
140 | } | |
141 | ||
142 | rcu_read_unlock(); | |
143 | return ret; | |
144 | } | |
145 | ||
146 | static int get_task_ioprio(struct task_struct *p) | |
147 | { | |
148 | int ret; | |
149 | ||
150 | ret = security_task_getioprio(p); | |
151 | if (ret) | |
152 | goto out; | |
153 | ret = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_NONE, IOPRIO_NORM); | |
154 | task_lock(p); | |
155 | if (p->io_context) | |
156 | ret = p->io_context->ioprio; | |
157 | task_unlock(p); | |
158 | out: | |
159 | return ret; | |
160 | } | |
161 | ||
162 | int ioprio_best(unsigned short aprio, unsigned short bprio) | |
163 | { | |
164 | unsigned short aclass; | |
165 | unsigned short bclass; | |
166 | ||
167 | if (!ioprio_valid(aprio)) | |
168 | aprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, IOPRIO_NORM); | |
169 | if (!ioprio_valid(bprio)) | |
170 | bprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, IOPRIO_NORM); | |
171 | ||
172 | aclass = IOPRIO_PRIO_CLASS(aprio); | |
173 | bclass = IOPRIO_PRIO_CLASS(bprio); | |
174 | if (aclass == bclass) | |
175 | return min(aprio, bprio); | |
176 | if (aclass > bclass) | |
177 | return bprio; | |
178 | else | |
179 | return aprio; | |
180 | } | |
181 | ||
182 | SYSCALL_DEFINE2(ioprio_get, int, which, int, who) | |
183 | { | |
184 | struct task_struct *g, *p; | |
185 | struct user_struct *user; | |
186 | struct pid *pgrp; | |
187 | kuid_t uid; | |
188 | int ret = -ESRCH; | |
189 | int tmpio; | |
190 | ||
191 | rcu_read_lock(); | |
192 | switch (which) { | |
193 | case IOPRIO_WHO_PROCESS: | |
194 | if (!who) | |
195 | p = current; | |
196 | else | |
197 | p = find_task_by_vpid(who); | |
198 | if (p) | |
199 | ret = get_task_ioprio(p); | |
200 | break; | |
201 | case IOPRIO_WHO_PGRP: | |
202 | if (!who) | |
203 | pgrp = task_pgrp(current); | |
204 | else | |
205 | pgrp = find_vpid(who); | |
206 | do_each_pid_thread(pgrp, PIDTYPE_PGID, p) { | |
207 | tmpio = get_task_ioprio(p); | |
208 | if (tmpio < 0) | |
209 | continue; | |
210 | if (ret == -ESRCH) | |
211 | ret = tmpio; | |
212 | else | |
213 | ret = ioprio_best(ret, tmpio); | |
214 | } while_each_pid_thread(pgrp, PIDTYPE_PGID, p); | |
215 | break; | |
216 | case IOPRIO_WHO_USER: | |
217 | uid = make_kuid(current_user_ns(), who); | |
218 | if (!who) | |
219 | user = current_user(); | |
220 | else | |
221 | user = find_user(uid); | |
222 | ||
223 | if (!user) | |
224 | break; | |
225 | ||
226 | for_each_process_thread(g, p) { | |
227 | if (!uid_eq(task_uid(p), user->uid) || | |
228 | !task_pid_vnr(p)) | |
229 | continue; | |
230 | tmpio = get_task_ioprio(p); | |
231 | if (tmpio < 0) | |
232 | continue; | |
233 | if (ret == -ESRCH) | |
234 | ret = tmpio; | |
235 | else | |
236 | ret = ioprio_best(ret, tmpio); | |
237 | } | |
238 | ||
239 | if (who) | |
240 | free_uid(user); | |
241 | break; | |
242 | default: | |
243 | ret = -EINVAL; | |
244 | } | |
245 | ||
246 | rcu_read_unlock(); | |
247 | return ret; | |
248 | } |