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