Blame view

fs/ioprio.c 4.97 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
22
23
24
   *
   * 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
   *
   */
  #include <linux/kernel.h>
  #include <linux/ioprio.h>
  #include <linux/blkdev.h>
16f7e0fe2   Randy Dunlap   [PATCH] capable/c...
25
  #include <linux/capability.h>
9abdc4cd8   Adrian Bunk   fs/ioprio.c shoul...
26
  #include <linux/syscalls.h>
03e680606   James Morris   [PATCH] lsm: add ...
27
  #include <linux/security.h>
b488893a3   Pavel Emelyanov   pid namespaces: c...
28
  #include <linux/pid_namespace.h>
22e2c507c   Jens Axboe   [PATCH] Update cf...
29
30
31
  
  static int set_task_ioprio(struct task_struct *task, int ioprio)
  {
03e680606   James Morris   [PATCH] lsm: add ...
32
  	int err;
22e2c507c   Jens Axboe   [PATCH] Update cf...
33
34
35
36
37
  	struct io_context *ioc;
  
  	if (task->uid != current->euid &&
  	    task->uid != current->uid && !capable(CAP_SYS_NICE))
  		return -EPERM;
03e680606   James Morris   [PATCH] lsm: add ...
38
39
40
  	err = security_task_setioprio(task, ioprio);
  	if (err)
  		return err;
22e2c507c   Jens Axboe   [PATCH] Update cf...
41
  	task_lock(task);
fd0928df9   Jens Axboe   ioprio: move io p...
42
43
44
45
46
47
  	do {
  		ioc = task->io_context;
  		/* see wmb() in current_io_context() */
  		smp_read_barrier_depends();
  		if (ioc)
  			break;
22e2c507c   Jens Axboe   [PATCH] Update cf...
48

fd0928df9   Jens Axboe   ioprio: move io p...
49
50
51
52
53
54
  		ioc = alloc_io_context(GFP_ATOMIC, -1);
  		if (!ioc) {
  			err = -ENOMEM;
  			break;
  		}
  		task->io_context = ioc;
fd0928df9   Jens Axboe   ioprio: move io p...
55
  	} while (1);
9f83e45eb   Oleg Nesterov   [PATCH] Fix curre...
56

fd0928df9   Jens Axboe   ioprio: move io p...
57
58
  	if (!err) {
  		ioc->ioprio = ioprio;
fc46379da   Jens Axboe   [PATCH] cfq-iosch...
59
  		ioc->ioprio_changed = 1;
fd0928df9   Jens Axboe   ioprio: move io p...
60
  	}
22e2c507c   Jens Axboe   [PATCH] Update cf...
61
62
  
  	task_unlock(task);
fd0928df9   Jens Axboe   ioprio: move io p...
63
  	return err;
22e2c507c   Jens Axboe   [PATCH] Update cf...
64
  }
cf3668088   Anton Blanchard   [PATCH] move iopr...
65
  asmlinkage long sys_ioprio_set(int which, int who, int ioprio)
22e2c507c   Jens Axboe   [PATCH] Update cf...
66
67
68
69
70
  {
  	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...
71
  	struct pid *pgrp;
22e2c507c   Jens Axboe   [PATCH] Update cf...
72
73
74
75
76
77
78
79
80
81
82
83
84
85
  	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...
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;
cf342e52e   Oleg Nesterov   [PATCH] Don't nee...
95
96
97
98
99
100
  	/*
  	 * We want IOPRIO_WHO_PGRP/IOPRIO_WHO_USER to be "atomic",
  	 * so we can't use rcu_read_lock(). See re-copy of ->ioprio
  	 * in copy_process().
  	 */
  	read_lock(&tasklist_lock);
22e2c507c   Jens Axboe   [PATCH] Update cf...
101
102
103
104
105
  	switch (which) {
  		case IOPRIO_WHO_PROCESS:
  			if (!who)
  				p = current;
  			else
228ebcbe6   Pavel Emelyanov   Uninline find_tas...
106
  				p = find_task_by_vpid(who);
22e2c507c   Jens Axboe   [PATCH] Update cf...
107
108
109
110
111
  			if (p)
  				ret = set_task_ioprio(p, ioprio);
  			break;
  		case IOPRIO_WHO_PGRP:
  			if (!who)
41487c65b   Eric W. Biederman   [PATCH] pid: repl...
112
113
  				pgrp = task_pgrp(current);
  			else
b488893a3   Pavel Emelyanov   pid namespaces: c...
114
  				pgrp = find_vpid(who);
41487c65b   Eric W. Biederman   [PATCH] pid: repl...
115
  			do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
22e2c507c   Jens Axboe   [PATCH] Update cf...
116
117
118
  				ret = set_task_ioprio(p, ioprio);
  				if (ret)
  					break;
41487c65b   Eric W. Biederman   [PATCH] pid: repl...
119
  			} while_each_pid_task(pgrp, PIDTYPE_PGID, p);
22e2c507c   Jens Axboe   [PATCH] Update cf...
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
  			break;
  		case IOPRIO_WHO_USER:
  			if (!who)
  				user = current->user;
  			else
  				user = find_user(who);
  
  			if (!user)
  				break;
  
  			do_each_thread(g, p) {
  				if (p->uid != who)
  					continue;
  				ret = set_task_ioprio(p, ioprio);
  				if (ret)
78bd4d484   Oleg Nesterov   [PATCH] sys_iopri...
135
  					goto free_uid;
22e2c507c   Jens Axboe   [PATCH] Update cf...
136
  			} while_each_thread(g, p);
78bd4d484   Oleg Nesterov   [PATCH] sys_iopri...
137
  free_uid:
22e2c507c   Jens Axboe   [PATCH] Update cf...
138
139
140
141
142
143
  			if (who)
  				free_uid(user);
  			break;
  		default:
  			ret = -EINVAL;
  	}
cf342e52e   Oleg Nesterov   [PATCH] Don't nee...
144
  	read_unlock(&tasklist_lock);
22e2c507c   Jens Axboe   [PATCH] Update cf...
145
146
  	return ret;
  }
a1836a42d   David Quigley   [PATCH] SELinux: ...
147
148
149
150
151
152
153
  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...
154
155
156
  	ret = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_NONE, IOPRIO_NORM);
  	if (p->io_context)
  		ret = p->io_context->ioprio;
a1836a42d   David Quigley   [PATCH] SELinux: ...
157
158
159
  out:
  	return ret;
  }
e014ff8d4   Oleg Nesterov   [PATCH] uninline ...
160
161
162
163
  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 ...
164
165
166
167
168
169
170
171
172
173
174
175
  	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;
  }
cf3668088   Anton Blanchard   [PATCH] move iopr...
176
  asmlinkage long sys_ioprio_get(int which, int who)
22e2c507c   Jens Axboe   [PATCH] Update cf...
177
178
179
  {
  	struct task_struct *g, *p;
  	struct user_struct *user;
41487c65b   Eric W. Biederman   [PATCH] pid: repl...
180
  	struct pid *pgrp;
22e2c507c   Jens Axboe   [PATCH] Update cf...
181
  	int ret = -ESRCH;
a1836a42d   David Quigley   [PATCH] SELinux: ...
182
  	int tmpio;
22e2c507c   Jens Axboe   [PATCH] Update cf...
183

cf342e52e   Oleg Nesterov   [PATCH] Don't nee...
184
  	read_lock(&tasklist_lock);
22e2c507c   Jens Axboe   [PATCH] Update cf...
185
186
187
188
189
  	switch (which) {
  		case IOPRIO_WHO_PROCESS:
  			if (!who)
  				p = current;
  			else
228ebcbe6   Pavel Emelyanov   Uninline find_tas...
190
  				p = find_task_by_vpid(who);
22e2c507c   Jens Axboe   [PATCH] Update cf...
191
  			if (p)
a1836a42d   David Quigley   [PATCH] SELinux: ...
192
  				ret = get_task_ioprio(p);
22e2c507c   Jens Axboe   [PATCH] Update cf...
193
194
195
  			break;
  		case IOPRIO_WHO_PGRP:
  			if (!who)
41487c65b   Eric W. Biederman   [PATCH] pid: repl...
196
197
  				pgrp = task_pgrp(current);
  			else
b488893a3   Pavel Emelyanov   pid namespaces: c...
198
  				pgrp = find_vpid(who);
41487c65b   Eric W. Biederman   [PATCH] pid: repl...
199
  			do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
a1836a42d   David Quigley   [PATCH] SELinux: ...
200
201
202
  				tmpio = get_task_ioprio(p);
  				if (tmpio < 0)
  					continue;
22e2c507c   Jens Axboe   [PATCH] Update cf...
203
  				if (ret == -ESRCH)
a1836a42d   David Quigley   [PATCH] SELinux: ...
204
  					ret = tmpio;
22e2c507c   Jens Axboe   [PATCH] Update cf...
205
  				else
a1836a42d   David Quigley   [PATCH] SELinux: ...
206
  					ret = ioprio_best(ret, tmpio);
41487c65b   Eric W. Biederman   [PATCH] pid: repl...
207
  			} while_each_pid_task(pgrp, PIDTYPE_PGID, p);
22e2c507c   Jens Axboe   [PATCH] Update cf...
208
209
210
211
212
213
214
215
216
217
218
219
220
  			break;
  		case IOPRIO_WHO_USER:
  			if (!who)
  				user = current->user;
  			else
  				user = find_user(who);
  
  			if (!user)
  				break;
  
  			do_each_thread(g, p) {
  				if (p->uid != user->uid)
  					continue;
a1836a42d   David Quigley   [PATCH] SELinux: ...
221
222
223
  				tmpio = get_task_ioprio(p);
  				if (tmpio < 0)
  					continue;
22e2c507c   Jens Axboe   [PATCH] Update cf...
224
  				if (ret == -ESRCH)
a1836a42d   David Quigley   [PATCH] SELinux: ...
225
  					ret = tmpio;
22e2c507c   Jens Axboe   [PATCH] Update cf...
226
  				else
a1836a42d   David Quigley   [PATCH] SELinux: ...
227
  					ret = ioprio_best(ret, tmpio);
22e2c507c   Jens Axboe   [PATCH] Update cf...
228
229
230
231
232
233
234
235
  			} while_each_thread(g, p);
  
  			if (who)
  				free_uid(user);
  			break;
  		default:
  			ret = -EINVAL;
  	}
cf342e52e   Oleg Nesterov   [PATCH] Don't nee...
236
  	read_unlock(&tasklist_lock);
22e2c507c   Jens Axboe   [PATCH] Update cf...
237
238
  	return ret;
  }