Blame view

fs/ioprio.c 5.03 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
26
  #include <linux/ioprio.h>
  #include <linux/blkdev.h>
16f7e0fe2   Randy Dunlap   [PATCH] capable/c...
27
  #include <linux/capability.h>
9abdc4cd8   Adrian Bunk   fs/ioprio.c shoul...
28
  #include <linux/syscalls.h>
03e680606   James Morris   [PATCH] lsm: add ...
29
  #include <linux/security.h>
b488893a3   Pavel Emelyanov   pid namespaces: c...
30
  #include <linux/pid_namespace.h>
22e2c507c   Jens Axboe   [PATCH] Update cf...
31

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

c69e8d9c0   David Howells   CRED: Use RCU to ...
38
39
40
41
42
  	rcu_read_lock();
  	tcred = __task_cred(task);
  	if (tcred->uid != cred->euid &&
  	    tcred->uid != cred->uid && !capable(CAP_SYS_NICE)) {
  		rcu_read_unlock();
22e2c507c   Jens Axboe   [PATCH] Update cf...
43
  		return -EPERM;
c69e8d9c0   David Howells   CRED: Use RCU to ...
44
45
  	}
  	rcu_read_unlock();
22e2c507c   Jens Axboe   [PATCH] Update cf...
46

03e680606   James Morris   [PATCH] lsm: add ...
47
48
49
  	err = security_task_setioprio(task, ioprio);
  	if (err)
  		return err;
22e2c507c   Jens Axboe   [PATCH] Update cf...
50
  	task_lock(task);
fd0928df9   Jens Axboe   ioprio: move io p...
51
52
53
54
55
56
  	do {
  		ioc = task->io_context;
  		/* see wmb() in current_io_context() */
  		smp_read_barrier_depends();
  		if (ioc)
  			break;
22e2c507c   Jens Axboe   [PATCH] Update cf...
57

fd0928df9   Jens Axboe   ioprio: move io p...
58
59
60
61
62
63
  		ioc = alloc_io_context(GFP_ATOMIC, -1);
  		if (!ioc) {
  			err = -ENOMEM;
  			break;
  		}
  		task->io_context = ioc;
fd0928df9   Jens Axboe   ioprio: move io p...
64
  	} while (1);
9f83e45eb   Oleg Nesterov   [PATCH] Fix curre...
65

fd0928df9   Jens Axboe   ioprio: move io p...
66
67
  	if (!err) {
  		ioc->ioprio = ioprio;
fc46379da   Jens Axboe   [PATCH] cfq-iosch...
68
  		ioc->ioprio_changed = 1;
fd0928df9   Jens Axboe   ioprio: move io p...
69
  	}
22e2c507c   Jens Axboe   [PATCH] Update cf...
70
71
  
  	task_unlock(task);
fd0928df9   Jens Axboe   ioprio: move io p...
72
  	return err;
22e2c507c   Jens Axboe   [PATCH] Update cf...
73
  }
b3881f74b   Theodore Ts'o   ext4: Add mount o...
74
  EXPORT_SYMBOL_GPL(set_task_ioprio);
22e2c507c   Jens Axboe   [PATCH] Update cf...
75

938bb9f5e   Heiko Carstens   [CVE-2009-0029] S...
76
  SYSCALL_DEFINE3(ioprio_set, int, which, int, who, int, ioprio)
22e2c507c   Jens Axboe   [PATCH] Update cf...
77
78
79
80
81
  {
  	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...
82
  	struct pid *pgrp;
22e2c507c   Jens Axboe   [PATCH] Update cf...
83
84
85
86
87
88
89
90
91
92
93
94
95
96
  	int ret;
  
  	switch (class) {
  		case IOPRIO_CLASS_RT:
  			if (!capable(CAP_SYS_ADMIN))
  				return -EPERM;
  			/* fall through, rt has prio field too */
  		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...
97
98
99
100
  		case IOPRIO_CLASS_NONE:
  			if (data)
  				return -EINVAL;
  			break;
22e2c507c   Jens Axboe   [PATCH] Update cf...
101
102
103
104
105
  		default:
  			return -EINVAL;
  	}
  
  	ret = -ESRCH;
d69b78ba1   Greg Thelen   ioprio: grab rcu_...
106
  	rcu_read_lock();
22e2c507c   Jens Axboe   [PATCH] Update cf...
107
108
109
110
111
  	switch (which) {
  		case IOPRIO_WHO_PROCESS:
  			if (!who)
  				p = current;
  			else
228ebcbe6   Pavel Emelyanov   Uninline find_tas...
112
  				p = find_task_by_vpid(who);
22e2c507c   Jens Axboe   [PATCH] Update cf...
113
114
115
116
117
  			if (p)
  				ret = set_task_ioprio(p, ioprio);
  			break;
  		case IOPRIO_WHO_PGRP:
  			if (!who)
41487c65b   Eric W. Biederman   [PATCH] pid: repl...
118
119
  				pgrp = task_pgrp(current);
  			else
b488893a3   Pavel Emelyanov   pid namespaces: c...
120
  				pgrp = find_vpid(who);
2d70b68d4   Ken Chen   fix setpriority(P...
121
  			do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
22e2c507c   Jens Axboe   [PATCH] Update cf...
122
123
124
  				ret = set_task_ioprio(p, ioprio);
  				if (ret)
  					break;
2d70b68d4   Ken Chen   fix setpriority(P...
125
  			} while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
22e2c507c   Jens Axboe   [PATCH] Update cf...
126
127
128
  			break;
  		case IOPRIO_WHO_USER:
  			if (!who)
86a264abe   David Howells   CRED: Wrap curren...
129
  				user = current_user();
22e2c507c   Jens Axboe   [PATCH] Update cf...
130
131
132
133
134
135
136
  			else
  				user = find_user(who);
  
  			if (!user)
  				break;
  
  			do_each_thread(g, p) {
d69b78ba1   Greg Thelen   ioprio: grab rcu_...
137
  				if (__task_cred(p)->uid != who)
22e2c507c   Jens Axboe   [PATCH] Update cf...
138
139
140
  					continue;
  				ret = set_task_ioprio(p, ioprio);
  				if (ret)
78bd4d484   Oleg Nesterov   [PATCH] sys_iopri...
141
  					goto free_uid;
22e2c507c   Jens Axboe   [PATCH] Update cf...
142
  			} while_each_thread(g, p);
78bd4d484   Oleg Nesterov   [PATCH] sys_iopri...
143
  free_uid:
22e2c507c   Jens Axboe   [PATCH] Update cf...
144
145
146
147
148
149
  			if (who)
  				free_uid(user);
  			break;
  		default:
  			ret = -EINVAL;
  	}
d69b78ba1   Greg Thelen   ioprio: grab rcu_...
150
  	rcu_read_unlock();
22e2c507c   Jens Axboe   [PATCH] Update cf...
151
152
  	return ret;
  }
a1836a42d   David Quigley   [PATCH] SELinux: ...
153
154
155
156
157
158
159
  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...
160
161
162
  	ret = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_NONE, IOPRIO_NORM);
  	if (p->io_context)
  		ret = p->io_context->ioprio;
a1836a42d   David Quigley   [PATCH] SELinux: ...
163
164
165
  out:
  	return ret;
  }
e014ff8d4   Oleg Nesterov   [PATCH] uninline ...
166
167
168
169
  int ioprio_best(unsigned short aprio, unsigned short bprio)
  {
  	unsigned short aclass = IOPRIO_PRIO_CLASS(aprio);
  	unsigned short bclass = IOPRIO_PRIO_CLASS(bprio);
e014ff8d4   Oleg Nesterov   [PATCH] uninline ...
170
171
172
173
174
175
176
177
178
179
180
181
  	if (aclass == IOPRIO_CLASS_NONE)
  		aclass = IOPRIO_CLASS_BE;
  	if (bclass == IOPRIO_CLASS_NONE)
  		bclass = IOPRIO_CLASS_BE;
  
  	if (aclass == bclass)
  		return min(aprio, bprio);
  	if (aclass > bclass)
  		return bprio;
  	else
  		return aprio;
  }
938bb9f5e   Heiko Carstens   [CVE-2009-0029] S...
182
  SYSCALL_DEFINE2(ioprio_get, int, which, int, who)
22e2c507c   Jens Axboe   [PATCH] Update cf...
183
184
185
  {
  	struct task_struct *g, *p;
  	struct user_struct *user;
41487c65b   Eric W. Biederman   [PATCH] pid: repl...
186
  	struct pid *pgrp;
22e2c507c   Jens Axboe   [PATCH] Update cf...
187
  	int ret = -ESRCH;
a1836a42d   David Quigley   [PATCH] SELinux: ...
188
  	int tmpio;
22e2c507c   Jens Axboe   [PATCH] Update cf...
189

d69b78ba1   Greg Thelen   ioprio: grab rcu_...
190
  	rcu_read_lock();
22e2c507c   Jens Axboe   [PATCH] Update cf...
191
192
193
194
195
  	switch (which) {
  		case IOPRIO_WHO_PROCESS:
  			if (!who)
  				p = current;
  			else
228ebcbe6   Pavel Emelyanov   Uninline find_tas...
196
  				p = find_task_by_vpid(who);
22e2c507c   Jens Axboe   [PATCH] Update cf...
197
  			if (p)
a1836a42d   David Quigley   [PATCH] SELinux: ...
198
  				ret = get_task_ioprio(p);
22e2c507c   Jens Axboe   [PATCH] Update cf...
199
200
201
  			break;
  		case IOPRIO_WHO_PGRP:
  			if (!who)
41487c65b   Eric W. Biederman   [PATCH] pid: repl...
202
203
  				pgrp = task_pgrp(current);
  			else
b488893a3   Pavel Emelyanov   pid namespaces: c...
204
  				pgrp = find_vpid(who);
2d70b68d4   Ken Chen   fix setpriority(P...
205
  			do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
a1836a42d   David Quigley   [PATCH] SELinux: ...
206
207
208
  				tmpio = get_task_ioprio(p);
  				if (tmpio < 0)
  					continue;
22e2c507c   Jens Axboe   [PATCH] Update cf...
209
  				if (ret == -ESRCH)
a1836a42d   David Quigley   [PATCH] SELinux: ...
210
  					ret = tmpio;
22e2c507c   Jens Axboe   [PATCH] Update cf...
211
  				else
a1836a42d   David Quigley   [PATCH] SELinux: ...
212
  					ret = ioprio_best(ret, tmpio);
2d70b68d4   Ken Chen   fix setpriority(P...
213
  			} while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
22e2c507c   Jens Axboe   [PATCH] Update cf...
214
215
216
  			break;
  		case IOPRIO_WHO_USER:
  			if (!who)
86a264abe   David Howells   CRED: Wrap curren...
217
  				user = current_user();
22e2c507c   Jens Axboe   [PATCH] Update cf...
218
219
220
221
222
223
224
  			else
  				user = find_user(who);
  
  			if (!user)
  				break;
  
  			do_each_thread(g, p) {
d69b78ba1   Greg Thelen   ioprio: grab rcu_...
225
  				if (__task_cred(p)->uid != user->uid)
22e2c507c   Jens Axboe   [PATCH] Update cf...
226
  					continue;
a1836a42d   David Quigley   [PATCH] SELinux: ...
227
228
229
  				tmpio = get_task_ioprio(p);
  				if (tmpio < 0)
  					continue;
22e2c507c   Jens Axboe   [PATCH] Update cf...
230
  				if (ret == -ESRCH)
a1836a42d   David Quigley   [PATCH] SELinux: ...
231
  					ret = tmpio;
22e2c507c   Jens Axboe   [PATCH] Update cf...
232
  				else
a1836a42d   David Quigley   [PATCH] SELinux: ...
233
  					ret = ioprio_best(ret, tmpio);
22e2c507c   Jens Axboe   [PATCH] Update cf...
234
235
236
237
238
239
240
241
  			} while_each_thread(g, p);
  
  			if (who)
  				free_uid(user);
  			break;
  		default:
  			ret = -EINVAL;
  	}
d69b78ba1   Greg Thelen   ioprio: grab rcu_...
242
  	rcu_read_unlock();
22e2c507c   Jens Axboe   [PATCH] Update cf...
243
244
  	return ret;
  }