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