]> git.ipfire.org Git - thirdparty/linux.git/blame - block/ioprio.c
Merge tag 'x86-platform-2020-06-01' of git://git.kernel.org/pub/scm/linux/kernel...
[thirdparty/linux.git] / block / ioprio.c
CommitLineData
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 36int 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 63EXPORT_SYMBOL_GPL(set_task_ioprio);
22e2c507 64
aa434577 65int 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:
72 if (!capable(CAP_SYS_ADMIN))
73 return -EPERM;
e29387eb
BVA
74 /* fall through */
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
94SYSCALL_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);
2d70b68d 122 do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
22e2c507
JA
123 ret = set_task_ioprio(p, ioprio);
124 if (ret)
125 break;
2d70b68d 126 } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
22e2c507
JA
127 break;
128 case IOPRIO_WHO_USER:
7b44ab97
EB
129 uid = make_kuid(current_user_ns(), who);
130 if (!uid_valid(uid))
131 break;
22e2c507 132 if (!who)
86a264ab 133 user = current_user();
22e2c507 134 else
7b44ab97 135 user = find_user(uid);
22e2c507
JA
136
137 if (!user)
138 break;
139
612dafab 140 for_each_process_thread(g, p) {
8639b461
BS
141 if (!uid_eq(task_uid(p), uid) ||
142 !task_pid_vnr(p))
22e2c507
JA
143 continue;
144 ret = set_task_ioprio(p, ioprio);
145 if (ret)
78bd4d48 146 goto free_uid;
612dafab 147 }
78bd4d48 148free_uid:
22e2c507
JA
149 if (who)
150 free_uid(user);
151 break;
152 default:
153 ret = -EINVAL;
154 }
155
d69b78ba 156 rcu_read_unlock();
22e2c507
JA
157 return ret;
158}
159
a1836a42
DQ
160static int get_task_ioprio(struct task_struct *p)
161{
162 int ret;
163
164 ret = security_task_getioprio(p);
165 if (ret)
166 goto out;
fd0928df 167 ret = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_NONE, IOPRIO_NORM);
8ba86821 168 task_lock(p);
fd0928df
JA
169 if (p->io_context)
170 ret = p->io_context->ioprio;
8ba86821 171 task_unlock(p);
a1836a42
DQ
172out:
173 return ret;
174}
175
e014ff8d
ON
176int ioprio_best(unsigned short aprio, unsigned short bprio)
177{
ece9c72a
JK
178 if (!ioprio_valid(aprio))
179 aprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, IOPRIO_NORM);
180 if (!ioprio_valid(bprio))
181 bprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, IOPRIO_NORM);
e014ff8d 182
9a87182c 183 return min(aprio, bprio);
e014ff8d
ON
184}
185
938bb9f5 186SYSCALL_DEFINE2(ioprio_get, int, which, int, who)
22e2c507
JA
187{
188 struct task_struct *g, *p;
189 struct user_struct *user;
41487c65 190 struct pid *pgrp;
7b44ab97 191 kuid_t uid;
22e2c507 192 int ret = -ESRCH;
a1836a42 193 int tmpio;
22e2c507 194
d69b78ba 195 rcu_read_lock();
22e2c507
JA
196 switch (which) {
197 case IOPRIO_WHO_PROCESS:
198 if (!who)
199 p = current;
200 else
228ebcbe 201 p = find_task_by_vpid(who);
22e2c507 202 if (p)
a1836a42 203 ret = get_task_ioprio(p);
22e2c507
JA
204 break;
205 case IOPRIO_WHO_PGRP:
206 if (!who)
41487c65
EB
207 pgrp = task_pgrp(current);
208 else
b488893a 209 pgrp = find_vpid(who);
2d70b68d 210 do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
a1836a42
DQ
211 tmpio = get_task_ioprio(p);
212 if (tmpio < 0)
213 continue;
22e2c507 214 if (ret == -ESRCH)
a1836a42 215 ret = tmpio;
22e2c507 216 else
a1836a42 217 ret = ioprio_best(ret, tmpio);
2d70b68d 218 } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
22e2c507
JA
219 break;
220 case IOPRIO_WHO_USER:
7b44ab97 221 uid = make_kuid(current_user_ns(), who);
22e2c507 222 if (!who)
86a264ab 223 user = current_user();
22e2c507 224 else
7b44ab97 225 user = find_user(uid);
22e2c507
JA
226
227 if (!user)
228 break;
229
612dafab 230 for_each_process_thread(g, p) {
8639b461
BS
231 if (!uid_eq(task_uid(p), user->uid) ||
232 !task_pid_vnr(p))
22e2c507 233 continue;
a1836a42
DQ
234 tmpio = get_task_ioprio(p);
235 if (tmpio < 0)
236 continue;
22e2c507 237 if (ret == -ESRCH)
a1836a42 238 ret = tmpio;
22e2c507 239 else
a1836a42 240 ret = ioprio_best(ret, tmpio);
612dafab 241 }
22e2c507
JA
242
243 if (who)
244 free_uid(user);
245 break;
246 default:
247 ret = -EINVAL;
248 }
249
d69b78ba 250 rcu_read_unlock();
22e2c507
JA
251 return ret;
252}