Blame view

block/ioprio.c 4.96 KB
22e2c507c   Jens Axboe   [PATCH] Update cf...
1
2
3
  /*
   * fs/ioprio.c
   *
0fe234795   Jens Axboe   [PATCH] Update ax...
4
   * Copyright (C) 2004 Jens Axboe <axboe@kernel.dk>
22e2c507c   Jens Axboe   [PATCH] Update cf...
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
   *
   * 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);
   *
   * See also Documentation/block/ioprio.txt
   *
   */
5a0e3ad6a   Tejun Heo   include cleanup: ...
22
  #include <linux/gfp.h>
22e2c507c   Jens Axboe   [PATCH] Update cf...
23
  #include <linux/kernel.h>
afeacc8c1   Paul Gortmaker   fs: add export.h ...
24
  #include <linux/export.h>
22e2c507c   Jens Axboe   [PATCH] Update cf...
25
  #include <linux/ioprio.h>
5b825c3af   Ingo Molnar   sched/headers: Pr...
26
  #include <linux/cred.h>
22e2c507c   Jens Axboe   [PATCH] Update cf...
27
  #include <linux/blkdev.h>
16f7e0fe2   Randy Dunlap   [PATCH] capable/c...
28
  #include <linux/capability.h>
8703e8a46   Ingo Molnar   sched/headers: Pr...
29
  #include <linux/sched/user.h>
f719ff9bc   Ingo Molnar   sched/headers: Pr...
30
  #include <linux/sched/task.h>
9abdc4cd8   Adrian Bunk   fs/ioprio.c shoul...
31
  #include <linux/syscalls.h>
03e680606   James Morris   [PATCH] lsm: add ...
32
  #include <linux/security.h>
b488893a3   Pavel Emelyanov   pid namespaces: c...
33
  #include <linux/pid_namespace.h>
22e2c507c   Jens Axboe   [PATCH] Update cf...
34

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

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

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

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

938bb9f5e   Heiko Carstens   [CVE-2009-0029] S...
63
  SYSCALL_DEFINE3(ioprio_set, int, which, int, who, int, ioprio)
22e2c507c   Jens Axboe   [PATCH] Update cf...
64
65
66
67
68
  {
  	int class = IOPRIO_PRIO_CLASS(ioprio);
  	int data = IOPRIO_PRIO_DATA(ioprio);
  	struct task_struct *p, *g;
  	struct user_struct *user;
41487c65b   Eric W. Biederman   [PATCH] pid: repl...
69
  	struct pid *pgrp;
7b44ab978   Eric W. Biederman   userns: Disassoci...
70
  	kuid_t uid;
22e2c507c   Jens Axboe   [PATCH] Update cf...
71
72
73
74
75
76
  	int ret;
  
  	switch (class) {
  		case IOPRIO_CLASS_RT:
  			if (!capable(CAP_SYS_ADMIN))
  				return -EPERM;
e29387ebd   Bart Van Assche   block: Add fallth...
77
78
  			/* fall through */
  			/* rt has prio field too */
22e2c507c   Jens Axboe   [PATCH] Update cf...
79
80
81
82
83
84
85
  		case IOPRIO_CLASS_BE:
  			if (data >= IOPRIO_BE_NR || data < 0)
  				return -EINVAL;
  
  			break;
  		case IOPRIO_CLASS_IDLE:
  			break;
8ec680e4c   Jens Axboe   ioprio: allow sys...
86
87
88
89
  		case IOPRIO_CLASS_NONE:
  			if (data)
  				return -EINVAL;
  			break;
22e2c507c   Jens Axboe   [PATCH] Update cf...
90
91
92
93
94
  		default:
  			return -EINVAL;
  	}
  
  	ret = -ESRCH;
d69b78ba1   Greg Thelen   ioprio: grab rcu_...
95
  	rcu_read_lock();
22e2c507c   Jens Axboe   [PATCH] Update cf...
96
97
98
99
100
  	switch (which) {
  		case IOPRIO_WHO_PROCESS:
  			if (!who)
  				p = current;
  			else
228ebcbe6   Pavel Emelyanov   Uninline find_tas...
101
  				p = find_task_by_vpid(who);
22e2c507c   Jens Axboe   [PATCH] Update cf...
102
103
104
105
106
  			if (p)
  				ret = set_task_ioprio(p, ioprio);
  			break;
  		case IOPRIO_WHO_PGRP:
  			if (!who)
41487c65b   Eric W. Biederman   [PATCH] pid: repl...
107
108
  				pgrp = task_pgrp(current);
  			else
b488893a3   Pavel Emelyanov   pid namespaces: c...
109
  				pgrp = find_vpid(who);
2d70b68d4   Ken Chen   fix setpriority(P...
110
  			do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
22e2c507c   Jens Axboe   [PATCH] Update cf...
111
112
113
  				ret = set_task_ioprio(p, ioprio);
  				if (ret)
  					break;
2d70b68d4   Ken Chen   fix setpriority(P...
114
  			} while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
22e2c507c   Jens Axboe   [PATCH] Update cf...
115
116
  			break;
  		case IOPRIO_WHO_USER:
7b44ab978   Eric W. Biederman   userns: Disassoci...
117
118
119
  			uid = make_kuid(current_user_ns(), who);
  			if (!uid_valid(uid))
  				break;
22e2c507c   Jens Axboe   [PATCH] Update cf...
120
  			if (!who)
86a264abe   David Howells   CRED: Wrap curren...
121
  				user = current_user();
22e2c507c   Jens Axboe   [PATCH] Update cf...
122
  			else
7b44ab978   Eric W. Biederman   userns: Disassoci...
123
  				user = find_user(uid);
22e2c507c   Jens Axboe   [PATCH] Update cf...
124
125
126
  
  			if (!user)
  				break;
612dafabb   Tetsuo Handa   block: use for_ea...
127
  			for_each_process_thread(g, p) {
8639b4613   Ben Segall   pidns: fix set/ge...
128
129
  				if (!uid_eq(task_uid(p), uid) ||
  				    !task_pid_vnr(p))
22e2c507c   Jens Axboe   [PATCH] Update cf...
130
131
132
  					continue;
  				ret = set_task_ioprio(p, ioprio);
  				if (ret)
78bd4d484   Oleg Nesterov   [PATCH] sys_iopri...
133
  					goto free_uid;
612dafabb   Tetsuo Handa   block: use for_ea...
134
  			}
78bd4d484   Oleg Nesterov   [PATCH] sys_iopri...
135
  free_uid:
22e2c507c   Jens Axboe   [PATCH] Update cf...
136
137
138
139
140
141
  			if (who)
  				free_uid(user);
  			break;
  		default:
  			ret = -EINVAL;
  	}
d69b78ba1   Greg Thelen   ioprio: grab rcu_...
142
  	rcu_read_unlock();
22e2c507c   Jens Axboe   [PATCH] Update cf...
143
144
  	return ret;
  }
a1836a42d   David Quigley   [PATCH] SELinux: ...
145
146
147
148
149
150
151
  static int get_task_ioprio(struct task_struct *p)
  {
  	int ret;
  
  	ret = security_task_getioprio(p);
  	if (ret)
  		goto out;
fd0928df9   Jens Axboe   ioprio: move io p...
152
  	ret = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_NONE, IOPRIO_NORM);
8ba868210   Omar Sandoval   block: fix use-af...
153
  	task_lock(p);
fd0928df9   Jens Axboe   ioprio: move io p...
154
155
  	if (p->io_context)
  		ret = p->io_context->ioprio;
8ba868210   Omar Sandoval   block: fix use-af...
156
  	task_unlock(p);
a1836a42d   David Quigley   [PATCH] SELinux: ...
157
158
159
  out:
  	return ret;
  }
e014ff8d4   Oleg Nesterov   [PATCH] uninline ...
160
161
  int ioprio_best(unsigned short aprio, unsigned short bprio)
  {
ece9c72ac   Jan Kara   block: Fix comput...
162
163
164
165
  	if (!ioprio_valid(aprio))
  		aprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, IOPRIO_NORM);
  	if (!ioprio_valid(bprio))
  		bprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, IOPRIO_NORM);
e014ff8d4   Oleg Nesterov   [PATCH] uninline ...
166

9a87182c4   Bart Van Assche   block: Optimize i...
167
  	return min(aprio, bprio);
e014ff8d4   Oleg Nesterov   [PATCH] uninline ...
168
  }
938bb9f5e   Heiko Carstens   [CVE-2009-0029] S...
169
  SYSCALL_DEFINE2(ioprio_get, int, which, int, who)
22e2c507c   Jens Axboe   [PATCH] Update cf...
170
171
172
  {
  	struct task_struct *g, *p;
  	struct user_struct *user;
41487c65b   Eric W. Biederman   [PATCH] pid: repl...
173
  	struct pid *pgrp;
7b44ab978   Eric W. Biederman   userns: Disassoci...
174
  	kuid_t uid;
22e2c507c   Jens Axboe   [PATCH] Update cf...
175
  	int ret = -ESRCH;
a1836a42d   David Quigley   [PATCH] SELinux: ...
176
  	int tmpio;
22e2c507c   Jens Axboe   [PATCH] Update cf...
177

d69b78ba1   Greg Thelen   ioprio: grab rcu_...
178
  	rcu_read_lock();
22e2c507c   Jens Axboe   [PATCH] Update cf...
179
180
181
182
183
  	switch (which) {
  		case IOPRIO_WHO_PROCESS:
  			if (!who)
  				p = current;
  			else
228ebcbe6   Pavel Emelyanov   Uninline find_tas...
184
  				p = find_task_by_vpid(who);
22e2c507c   Jens Axboe   [PATCH] Update cf...
185
  			if (p)
a1836a42d   David Quigley   [PATCH] SELinux: ...
186
  				ret = get_task_ioprio(p);
22e2c507c   Jens Axboe   [PATCH] Update cf...
187
188
189
  			break;
  		case IOPRIO_WHO_PGRP:
  			if (!who)
41487c65b   Eric W. Biederman   [PATCH] pid: repl...
190
191
  				pgrp = task_pgrp(current);
  			else
b488893a3   Pavel Emelyanov   pid namespaces: c...
192
  				pgrp = find_vpid(who);
2d70b68d4   Ken Chen   fix setpriority(P...
193
  			do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
a1836a42d   David Quigley   [PATCH] SELinux: ...
194
195
196
  				tmpio = get_task_ioprio(p);
  				if (tmpio < 0)
  					continue;
22e2c507c   Jens Axboe   [PATCH] Update cf...
197
  				if (ret == -ESRCH)
a1836a42d   David Quigley   [PATCH] SELinux: ...
198
  					ret = tmpio;
22e2c507c   Jens Axboe   [PATCH] Update cf...
199
  				else
a1836a42d   David Quigley   [PATCH] SELinux: ...
200
  					ret = ioprio_best(ret, tmpio);
2d70b68d4   Ken Chen   fix setpriority(P...
201
  			} while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
22e2c507c   Jens Axboe   [PATCH] Update cf...
202
203
  			break;
  		case IOPRIO_WHO_USER:
7b44ab978   Eric W. Biederman   userns: Disassoci...
204
  			uid = make_kuid(current_user_ns(), who);
22e2c507c   Jens Axboe   [PATCH] Update cf...
205
  			if (!who)
86a264abe   David Howells   CRED: Wrap curren...
206
  				user = current_user();
22e2c507c   Jens Axboe   [PATCH] Update cf...
207
  			else
7b44ab978   Eric W. Biederman   userns: Disassoci...
208
  				user = find_user(uid);
22e2c507c   Jens Axboe   [PATCH] Update cf...
209
210
211
  
  			if (!user)
  				break;
612dafabb   Tetsuo Handa   block: use for_ea...
212
  			for_each_process_thread(g, p) {
8639b4613   Ben Segall   pidns: fix set/ge...
213
214
  				if (!uid_eq(task_uid(p), user->uid) ||
  				    !task_pid_vnr(p))
22e2c507c   Jens Axboe   [PATCH] Update cf...
215
  					continue;
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);
612dafabb   Tetsuo Handa   block: use for_ea...
223
  			}
22e2c507c   Jens Axboe   [PATCH] Update cf...
224
225
226
227
228
229
230
  
  			if (who)
  				free_uid(user);
  			break;
  		default:
  			ret = -EINVAL;
  	}
d69b78ba1   Greg Thelen   ioprio: grab rcu_...
231
  	rcu_read_unlock();
22e2c507c   Jens Axboe   [PATCH] Update cf...
232
233
  	return ret;
  }