Blame view

block/ioprio.c 5.54 KB
3dcf60bcb   Christoph Hellwig   block: add SPDX t...
1
  // SPDX-License-Identifier: GPL-2.0
22e2c507c   Jens Axboe   [PATCH] Update cf...
2
3
4
  /*
   * fs/ioprio.c
   *
0fe234795   Jens Axboe   [PATCH] Update ax...
5
   * Copyright (C) 2004 Jens Axboe <axboe@kernel.dk>
22e2c507c   Jens Axboe   [PATCH] Update cf...
6
7
8
9
10
11
12
13
14
15
16
17
18
19
   *
   * Helper functions for setting/querying io priorities of processes. The
   * system calls closely mimmick getpriority/setpriority, see the man page for
   * those. The prio argument is a composite of prio class and prio data, where
   * the data argument has meaning within that class. The standard scheduling
   * classes have 8 distinct prio levels, with 0 being the highest prio and 7
   * being the lowest.
   *
   * IOW, setting BE scheduling class with prio 2 is done ala:
   *
   * unsigned int prio = (IOPRIO_CLASS_BE << IOPRIO_CLASS_SHIFT) | 2;
   *
   * ioprio_set(PRIO_PROCESS, pid, prio);
   *
898bd37a9   Mauro Carvalho Chehab   docs: block: conv...
20
   * See also Documentation/block/ioprio.rst
22e2c507c   Jens Axboe   [PATCH] Update cf...
21
22
   *
   */
5a0e3ad6a   Tejun Heo   include cleanup: ...
23
  #include <linux/gfp.h>
22e2c507c   Jens Axboe   [PATCH] Update cf...
24
  #include <linux/kernel.h>
afeacc8c1   Paul Gortmaker   fs: add export.h ...
25
  #include <linux/export.h>
22e2c507c   Jens Axboe   [PATCH] Update cf...
26
  #include <linux/ioprio.h>
5b825c3af   Ingo Molnar   sched/headers: Pr...
27
  #include <linux/cred.h>
22e2c507c   Jens Axboe   [PATCH] Update cf...
28
  #include <linux/blkdev.h>
16f7e0fe2   Randy Dunlap   [PATCH] capable/c...
29
  #include <linux/capability.h>
8703e8a46   Ingo Molnar   sched/headers: Pr...
30
  #include <linux/sched/user.h>
f719ff9bc   Ingo Molnar   sched/headers: Pr...
31
  #include <linux/sched/task.h>
9abdc4cd8   Adrian Bunk   fs/ioprio.c shoul...
32
  #include <linux/syscalls.h>
03e680606   James Morris   [PATCH] lsm: add ...
33
  #include <linux/security.h>
b488893a3   Pavel Emelyanov   pid namespaces: c...
34
  #include <linux/pid_namespace.h>
22e2c507c   Jens Axboe   [PATCH] Update cf...
35

b3881f74b   Theodore Ts'o   ext4: Add mount o...
36
  int set_task_ioprio(struct task_struct *task, int ioprio)
22e2c507c   Jens Axboe   [PATCH] Update cf...
37
  {
03e680606   James Morris   [PATCH] lsm: add ...
38
  	int err;
22e2c507c   Jens Axboe   [PATCH] Update cf...
39
  	struct io_context *ioc;
c69e8d9c0   David Howells   CRED: Use RCU to ...
40
  	const struct cred *cred = current_cred(), *tcred;
22e2c507c   Jens Axboe   [PATCH] Update cf...
41

c69e8d9c0   David Howells   CRED: Use RCU to ...
42
43
  	rcu_read_lock();
  	tcred = __task_cred(task);
8e96e3b7b   Eric W. Biederman   userns: Use uid_e...
44
45
  	if (!uid_eq(tcred->uid, cred->euid) &&
  	    !uid_eq(tcred->uid, cred->uid) && !capable(CAP_SYS_NICE)) {
c69e8d9c0   David Howells   CRED: Use RCU to ...
46
  		rcu_read_unlock();
22e2c507c   Jens Axboe   [PATCH] Update cf...
47
  		return -EPERM;
c69e8d9c0   David Howells   CRED: Use RCU to ...
48
49
  	}
  	rcu_read_unlock();
22e2c507c   Jens Axboe   [PATCH] Update cf...
50

03e680606   James Morris   [PATCH] lsm: add ...
51
52
53
  	err = security_task_setioprio(task, ioprio);
  	if (err)
  		return err;
6e736be7f   Tejun Heo   block: make ioc g...
54
55
  	ioc = get_task_io_context(task, GFP_ATOMIC, NUMA_NO_NODE);
  	if (ioc) {
2b566fa55   Tejun Heo   block: remove ioc...
56
  		ioc->ioprio = ioprio;
11a3122f6   Tejun Heo   block: strip out ...
57
  		put_io_context(ioc);
fd0928df9   Jens Axboe   ioprio: move io p...
58
  	}
22e2c507c   Jens Axboe   [PATCH] Update cf...
59

fd0928df9   Jens Axboe   ioprio: move io p...
60
  	return err;
22e2c507c   Jens Axboe   [PATCH] Update cf...
61
  }
b3881f74b   Theodore Ts'o   ext4: Add mount o...
62
  EXPORT_SYMBOL_GPL(set_task_ioprio);
22e2c507c   Jens Axboe   [PATCH] Update cf...
63

aa4345779   Adam Manzanares   block: add ioprio...
64
  int ioprio_check_cap(int ioprio)
22e2c507c   Jens Axboe   [PATCH] Update cf...
65
66
67
  {
  	int class = IOPRIO_PRIO_CLASS(ioprio);
  	int data = IOPRIO_PRIO_DATA(ioprio);
22e2c507c   Jens Axboe   [PATCH] Update cf...
68
69
70
  
  	switch (class) {
  		case IOPRIO_CLASS_RT:
156076367   Alistair Delva   block: Check ADMI...
71
72
73
74
75
76
77
78
  			/*
  			 * Originally this only checked for CAP_SYS_ADMIN,
  			 * which was implicitly allowed for pid 0 by security
  			 * modules such as SELinux. Make sure we check
  			 * CAP_SYS_ADMIN first to avoid a denial/avc for
  			 * possibly missing CAP_SYS_NICE permission.
  			 */
  			if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_NICE))
22e2c507c   Jens Axboe   [PATCH] Update cf...
79
  				return -EPERM;
df561f668   Gustavo A. R. Silva   treewide: Use fal...
80
  			fallthrough;
e29387ebd   Bart Van Assche   block: Add fallth...
81
  			/* rt has prio field too */
22e2c507c   Jens Axboe   [PATCH] Update cf...
82
  		case IOPRIO_CLASS_BE:
202bc942c   Damien Le Moal   block: Introduce ...
83
  			if (data >= IOPRIO_NR_LEVELS || data < 0)
22e2c507c   Jens Axboe   [PATCH] Update cf...
84
  				return -EINVAL;
22e2c507c   Jens Axboe   [PATCH] Update cf...
85
86
87
  			break;
  		case IOPRIO_CLASS_IDLE:
  			break;
8ec680e4c   Jens Axboe   ioprio: allow sys...
88
89
90
91
  		case IOPRIO_CLASS_NONE:
  			if (data)
  				return -EINVAL;
  			break;
22e2c507c   Jens Axboe   [PATCH] Update cf...
92
93
94
  		default:
  			return -EINVAL;
  	}
aa4345779   Adam Manzanares   block: add ioprio...
95
96
97
98
99
100
101
102
103
104
105
106
107
108
  	return 0;
  }
  
  SYSCALL_DEFINE3(ioprio_set, int, which, int, who, int, ioprio)
  {
  	struct task_struct *p, *g;
  	struct user_struct *user;
  	struct pid *pgrp;
  	kuid_t uid;
  	int ret;
  
  	ret = ioprio_check_cap(ioprio);
  	if (ret)
  		return ret;
22e2c507c   Jens Axboe   [PATCH] Update cf...
109
  	ret = -ESRCH;
d69b78ba1   Greg Thelen   ioprio: grab rcu_...
110
  	rcu_read_lock();
22e2c507c   Jens Axboe   [PATCH] Update cf...
111
112
113
114
115
  	switch (which) {
  		case IOPRIO_WHO_PROCESS:
  			if (!who)
  				p = current;
  			else
228ebcbe6   Pavel Emelyanov   Uninline find_tas...
116
  				p = find_task_by_vpid(who);
22e2c507c   Jens Axboe   [PATCH] Update cf...
117
118
119
120
121
  			if (p)
  				ret = set_task_ioprio(p, ioprio);
  			break;
  		case IOPRIO_WHO_PGRP:
  			if (!who)
41487c65b   Eric W. Biederman   [PATCH] pid: repl...
122
123
  				pgrp = task_pgrp(current);
  			else
b488893a3   Pavel Emelyanov   pid namespaces: c...
124
  				pgrp = find_vpid(who);
40c7fd3fd   Peter Zijlstra   block: Fix sys_io...
125
126
  
  			read_lock(&tasklist_lock);
2d70b68d4   Ken Chen   fix setpriority(P...
127
  			do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
22e2c507c   Jens Axboe   [PATCH] Update cf...
128
  				ret = set_task_ioprio(p, ioprio);
40c7fd3fd   Peter Zijlstra   block: Fix sys_io...
129
130
131
132
  				if (ret) {
  					read_unlock(&tasklist_lock);
  					goto out;
  				}
2d70b68d4   Ken Chen   fix setpriority(P...
133
  			} while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
40c7fd3fd   Peter Zijlstra   block: Fix sys_io...
134
  			read_unlock(&tasklist_lock);
22e2c507c   Jens Axboe   [PATCH] Update cf...
135
136
  			break;
  		case IOPRIO_WHO_USER:
7b44ab978   Eric W. Biederman   userns: Disassoci...
137
138
139
  			uid = make_kuid(current_user_ns(), who);
  			if (!uid_valid(uid))
  				break;
22e2c507c   Jens Axboe   [PATCH] Update cf...
140
  			if (!who)
86a264abe   David Howells   CRED: Wrap curren...
141
  				user = current_user();
22e2c507c   Jens Axboe   [PATCH] Update cf...
142
  			else
7b44ab978   Eric W. Biederman   userns: Disassoci...
143
  				user = find_user(uid);
22e2c507c   Jens Axboe   [PATCH] Update cf...
144
145
146
  
  			if (!user)
  				break;
612dafabb   Tetsuo Handa   block: use for_ea...
147
  			for_each_process_thread(g, p) {
8639b4613   Ben Segall   pidns: fix set/ge...
148
149
  				if (!uid_eq(task_uid(p), uid) ||
  				    !task_pid_vnr(p))
22e2c507c   Jens Axboe   [PATCH] Update cf...
150
151
152
  					continue;
  				ret = set_task_ioprio(p, ioprio);
  				if (ret)
78bd4d484   Oleg Nesterov   [PATCH] sys_iopri...
153
  					goto free_uid;
612dafabb   Tetsuo Handa   block: use for_ea...
154
  			}
78bd4d484   Oleg Nesterov   [PATCH] sys_iopri...
155
  free_uid:
22e2c507c   Jens Axboe   [PATCH] Update cf...
156
157
158
159
160
161
  			if (who)
  				free_uid(user);
  			break;
  		default:
  			ret = -EINVAL;
  	}
40c7fd3fd   Peter Zijlstra   block: Fix sys_io...
162
  out:
d69b78ba1   Greg Thelen   ioprio: grab rcu_...
163
  	rcu_read_unlock();
22e2c507c   Jens Axboe   [PATCH] Update cf...
164
165
  	return ret;
  }
a1836a42d   David Quigley   [PATCH] SELinux: ...
166
167
168
169
170
171
172
  static int get_task_ioprio(struct task_struct *p)
  {
  	int ret;
  
  	ret = security_task_getioprio(p);
  	if (ret)
  		goto out;
e70344c05   Damien Le Moal   block: fix defaul...
173
  	ret = IOPRIO_DEFAULT;
8ba868210   Omar Sandoval   block: fix use-af...
174
  	task_lock(p);
fd0928df9   Jens Axboe   ioprio: move io p...
175
176
  	if (p->io_context)
  		ret = p->io_context->ioprio;
8ba868210   Omar Sandoval   block: fix use-af...
177
  	task_unlock(p);
a1836a42d   David Quigley   [PATCH] SELinux: ...
178
179
180
  out:
  	return ret;
  }
e014ff8d4   Oleg Nesterov   [PATCH] uninline ...
181
182
  int ioprio_best(unsigned short aprio, unsigned short bprio)
  {
ece9c72ac   Jan Kara   block: Fix comput...
183
  	if (!ioprio_valid(aprio))
7ad47f414   Jan Kara   block: fix defaul...
184
  		aprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, IOPRIO_BE_NORM);
ece9c72ac   Jan Kara   block: Fix comput...
185
  	if (!ioprio_valid(bprio))
7ad47f414   Jan Kara   block: fix defaul...
186
  		bprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, IOPRIO_BE_NORM);
e014ff8d4   Oleg Nesterov   [PATCH] uninline ...
187

9a87182c4   Bart Van Assche   block: Optimize i...
188
  	return min(aprio, bprio);
e014ff8d4   Oleg Nesterov   [PATCH] uninline ...
189
  }
938bb9f5e   Heiko Carstens   [CVE-2009-0029] S...
190
  SYSCALL_DEFINE2(ioprio_get, int, which, int, who)
22e2c507c   Jens Axboe   [PATCH] Update cf...
191
192
193
  {
  	struct task_struct *g, *p;
  	struct user_struct *user;
41487c65b   Eric W. Biederman   [PATCH] pid: repl...
194
  	struct pid *pgrp;
7b44ab978   Eric W. Biederman   userns: Disassoci...
195
  	kuid_t uid;
22e2c507c   Jens Axboe   [PATCH] Update cf...
196
  	int ret = -ESRCH;
a1836a42d   David Quigley   [PATCH] SELinux: ...
197
  	int tmpio;
22e2c507c   Jens Axboe   [PATCH] Update cf...
198

d69b78ba1   Greg Thelen   ioprio: grab rcu_...
199
  	rcu_read_lock();
22e2c507c   Jens Axboe   [PATCH] Update cf...
200
201
202
203
204
  	switch (which) {
  		case IOPRIO_WHO_PROCESS:
  			if (!who)
  				p = current;
  			else
228ebcbe6   Pavel Emelyanov   Uninline find_tas...
205
  				p = find_task_by_vpid(who);
22e2c507c   Jens Axboe   [PATCH] Update cf...
206
  			if (p)
a1836a42d   David Quigley   [PATCH] SELinux: ...
207
  				ret = get_task_ioprio(p);
22e2c507c   Jens Axboe   [PATCH] Update cf...
208
209
210
  			break;
  		case IOPRIO_WHO_PGRP:
  			if (!who)
41487c65b   Eric W. Biederman   [PATCH] pid: repl...
211
212
  				pgrp = task_pgrp(current);
  			else
b488893a3   Pavel Emelyanov   pid namespaces: c...
213
  				pgrp = find_vpid(who);
d429b302c   Davidlohr Bueso   block: fix ioprio...
214
  			read_lock(&tasklist_lock);
2d70b68d4   Ken Chen   fix setpriority(P...
215
  			do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
a1836a42d   David Quigley   [PATCH] SELinux: ...
216
217
218
  				tmpio = get_task_ioprio(p);
  				if (tmpio < 0)
  					continue;
22e2c507c   Jens Axboe   [PATCH] Update cf...
219
  				if (ret == -ESRCH)
a1836a42d   David Quigley   [PATCH] SELinux: ...
220
  					ret = tmpio;
22e2c507c   Jens Axboe   [PATCH] Update cf...
221
  				else
a1836a42d   David Quigley   [PATCH] SELinux: ...
222
  					ret = ioprio_best(ret, tmpio);
2d70b68d4   Ken Chen   fix setpriority(P...
223
  			} while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
d429b302c   Davidlohr Bueso   block: fix ioprio...
224
  			read_unlock(&tasklist_lock);
22e2c507c   Jens Axboe   [PATCH] Update cf...
225
226
  			break;
  		case IOPRIO_WHO_USER:
7b44ab978   Eric W. Biederman   userns: Disassoci...
227
  			uid = make_kuid(current_user_ns(), who);
22e2c507c   Jens Axboe   [PATCH] Update cf...
228
  			if (!who)
86a264abe   David Howells   CRED: Wrap curren...
229
  				user = current_user();
22e2c507c   Jens Axboe   [PATCH] Update cf...
230
  			else
7b44ab978   Eric W. Biederman   userns: Disassoci...
231
  				user = find_user(uid);
22e2c507c   Jens Axboe   [PATCH] Update cf...
232
233
234
  
  			if (!user)
  				break;
612dafabb   Tetsuo Handa   block: use for_ea...
235
  			for_each_process_thread(g, p) {
8639b4613   Ben Segall   pidns: fix set/ge...
236
237
  				if (!uid_eq(task_uid(p), user->uid) ||
  				    !task_pid_vnr(p))
22e2c507c   Jens Axboe   [PATCH] Update cf...
238
  					continue;
a1836a42d   David Quigley   [PATCH] SELinux: ...
239
240
241
  				tmpio = get_task_ioprio(p);
  				if (tmpio < 0)
  					continue;
22e2c507c   Jens Axboe   [PATCH] Update cf...
242
  				if (ret == -ESRCH)
a1836a42d   David Quigley   [PATCH] SELinux: ...
243
  					ret = tmpio;
22e2c507c   Jens Axboe   [PATCH] Update cf...
244
  				else
a1836a42d   David Quigley   [PATCH] SELinux: ...
245
  					ret = ioprio_best(ret, tmpio);
612dafabb   Tetsuo Handa   block: use for_ea...
246
  			}
22e2c507c   Jens Axboe   [PATCH] Update cf...
247
248
249
250
251
252
253
  
  			if (who)
  				free_uid(user);
  			break;
  		default:
  			ret = -EINVAL;
  	}
d69b78ba1   Greg Thelen   ioprio: grab rcu_...
254
  	rcu_read_unlock();
22e2c507c   Jens Axboe   [PATCH] Update cf...
255
256
  	return ret;
  }