Blame view

fs/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
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
  	rcu_read_lock();
  	tcred = __task_cred(task);
8e96e3b7b   Eric W. Biederman   userns: Use uid_e...
40
41
  	if (!uid_eq(tcred->uid, cred->euid) &&
  	    !uid_eq(tcred->uid, cred->uid) && !capable(CAP_SYS_NICE)) {
c69e8d9c0   David Howells   CRED: Use RCU to ...
42
  		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;
6e736be7f   Tejun Heo   block: make ioc g...
50
51
  	ioc = get_task_io_context(task, GFP_ATOMIC, NUMA_NO_NODE);
  	if (ioc) {
2b566fa55   Tejun Heo   block: remove ioc...
52
  		ioc->ioprio = ioprio;
11a3122f6   Tejun Heo   block: strip out ...
53
  		put_io_context(ioc);
fd0928df9   Jens Axboe   ioprio: move io p...
54
  	}
22e2c507c   Jens Axboe   [PATCH] Update cf...
55

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

938bb9f5e   Heiko Carstens   [CVE-2009-0029] S...
60
  SYSCALL_DEFINE3(ioprio_set, int, which, int, who, int, ioprio)
22e2c507c   Jens Axboe   [PATCH] Update cf...
61
62
63
64
65
  {
  	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...
66
  	struct pid *pgrp;
7b44ab978   Eric W. Biederman   userns: Disassoci...
67
  	kuid_t uid;
22e2c507c   Jens Axboe   [PATCH] Update cf...
68
69
70
71
72
73
74
75
76
77
78
79
80
81
  	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...
82
83
84
85
  		case IOPRIO_CLASS_NONE:
  			if (data)
  				return -EINVAL;
  			break;
22e2c507c   Jens Axboe   [PATCH] Update cf...
86
87
88
89
90
  		default:
  			return -EINVAL;
  	}
  
  	ret = -ESRCH;
d69b78ba1   Greg Thelen   ioprio: grab rcu_...
91
  	rcu_read_lock();
22e2c507c   Jens Axboe   [PATCH] Update cf...
92
93
94
95
96
  	switch (which) {
  		case IOPRIO_WHO_PROCESS:
  			if (!who)
  				p = current;
  			else
228ebcbe6   Pavel Emelyanov   Uninline find_tas...
97
  				p = find_task_by_vpid(who);
22e2c507c   Jens Axboe   [PATCH] Update cf...
98
99
100
101
102
  			if (p)
  				ret = set_task_ioprio(p, ioprio);
  			break;
  		case IOPRIO_WHO_PGRP:
  			if (!who)
41487c65b   Eric W. Biederman   [PATCH] pid: repl...
103
104
  				pgrp = task_pgrp(current);
  			else
b488893a3   Pavel Emelyanov   pid namespaces: c...
105
  				pgrp = find_vpid(who);
2d70b68d4   Ken Chen   fix setpriority(P...
106
  			do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
22e2c507c   Jens Axboe   [PATCH] Update cf...
107
108
109
  				ret = set_task_ioprio(p, ioprio);
  				if (ret)
  					break;
2d70b68d4   Ken Chen   fix setpriority(P...
110
  			} while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
22e2c507c   Jens Axboe   [PATCH] Update cf...
111
112
  			break;
  		case IOPRIO_WHO_USER:
7b44ab978   Eric W. Biederman   userns: Disassoci...
113
114
115
  			uid = make_kuid(current_user_ns(), who);
  			if (!uid_valid(uid))
  				break;
22e2c507c   Jens Axboe   [PATCH] Update cf...
116
  			if (!who)
86a264abe   David Howells   CRED: Wrap curren...
117
  				user = current_user();
22e2c507c   Jens Axboe   [PATCH] Update cf...
118
  			else
7b44ab978   Eric W. Biederman   userns: Disassoci...
119
  				user = find_user(uid);
22e2c507c   Jens Axboe   [PATCH] Update cf...
120
121
122
123
124
  
  			if (!user)
  				break;
  
  			do_each_thread(g, p) {
078de5f70   Eric W. Biederman   userns: Store uid...
125
  				if (!uid_eq(task_uid(p), uid))
22e2c507c   Jens Axboe   [PATCH] Update cf...
126
127
128
  					continue;
  				ret = set_task_ioprio(p, ioprio);
  				if (ret)
78bd4d484   Oleg Nesterov   [PATCH] sys_iopri...
129
  					goto free_uid;
22e2c507c   Jens Axboe   [PATCH] Update cf...
130
  			} while_each_thread(g, p);
78bd4d484   Oleg Nesterov   [PATCH] sys_iopri...
131
  free_uid:
22e2c507c   Jens Axboe   [PATCH] Update cf...
132
133
134
135
136
137
  			if (who)
  				free_uid(user);
  			break;
  		default:
  			ret = -EINVAL;
  	}
d69b78ba1   Greg Thelen   ioprio: grab rcu_...
138
  	rcu_read_unlock();
22e2c507c   Jens Axboe   [PATCH] Update cf...
139
140
  	return ret;
  }
a1836a42d   David Quigley   [PATCH] SELinux: ...
141
142
143
144
145
146
147
  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...
148
149
150
  	ret = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_NONE, IOPRIO_NORM);
  	if (p->io_context)
  		ret = p->io_context->ioprio;
a1836a42d   David Quigley   [PATCH] SELinux: ...
151
152
153
  out:
  	return ret;
  }
e014ff8d4   Oleg Nesterov   [PATCH] uninline ...
154
155
156
157
  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 ...
158
159
160
161
162
163
164
165
166
167
168
169
  	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...
170
  SYSCALL_DEFINE2(ioprio_get, int, which, int, who)
22e2c507c   Jens Axboe   [PATCH] Update cf...
171
172
173
  {
  	struct task_struct *g, *p;
  	struct user_struct *user;
41487c65b   Eric W. Biederman   [PATCH] pid: repl...
174
  	struct pid *pgrp;
7b44ab978   Eric W. Biederman   userns: Disassoci...
175
  	kuid_t uid;
22e2c507c   Jens Axboe   [PATCH] Update cf...
176
  	int ret = -ESRCH;
a1836a42d   David Quigley   [PATCH] SELinux: ...
177
  	int tmpio;
22e2c507c   Jens Axboe   [PATCH] Update cf...
178

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