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