ioprio.c 5.1 KB
Newer Older
1 2 3
/*
 * fs/ioprio.c
 *
4
 * Copyright (C) 2004 Jens Axboe <axboe@kernel.dk>
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
 *
 */
22
#include <linux/gfp.h>
23
#include <linux/kernel.h>
24
#include <linux/export.h>
25 26
#include <linux/ioprio.h>
#include <linux/blkdev.h>
27
#include <linux/capability.h>
28
#include <linux/sched/user.h>
29
#include <linux/syscalls.h>
30
#include <linux/security.h>
31
#include <linux/pid_namespace.h>
32

33
int set_task_ioprio(struct task_struct *task, int ioprio)
34
{
35
	int err;
36
	struct io_context *ioc;
37
	const struct cred *cred = current_cred(), *tcred;
38

39 40
	rcu_read_lock();
	tcred = __task_cred(task);
41 42
	if (!uid_eq(tcred->uid, cred->euid) &&
	    !uid_eq(tcred->uid, cred->uid) && !capable(CAP_SYS_NICE)) {
43
		rcu_read_unlock();
44
		return -EPERM;
45 46
	}
	rcu_read_unlock();
47

48 49 50 51
	err = security_task_setioprio(task, ioprio);
	if (err)
		return err;

52 53
	ioc = get_task_io_context(task, GFP_ATOMIC, NUMA_NO_NODE);
	if (ioc) {
T
Tejun Heo 已提交
54
		ioc->ioprio = ioprio;
55
		put_io_context(ioc);
56
	}
57

58
	return err;
59
}
60
EXPORT_SYMBOL_GPL(set_task_ioprio);
61

62
SYSCALL_DEFINE3(ioprio_set, int, which, int, who, int, ioprio)
63 64 65 66 67
{
	int class = IOPRIO_PRIO_CLASS(ioprio);
	int data = IOPRIO_PRIO_DATA(ioprio);
	struct task_struct *p, *g;
	struct user_struct *user;
68
	struct pid *pgrp;
69
	kuid_t uid;
70 71 72 73 74 75 76 77 78 79 80 81 82 83
	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;
84 85 86 87
		case IOPRIO_CLASS_NONE:
			if (data)
				return -EINVAL;
			break;
88 89 90 91 92
		default:
			return -EINVAL;
	}

	ret = -ESRCH;
93
	rcu_read_lock();
94 95 96 97 98
	switch (which) {
		case IOPRIO_WHO_PROCESS:
			if (!who)
				p = current;
			else
99
				p = find_task_by_vpid(who);
100 101 102 103 104
			if (p)
				ret = set_task_ioprio(p, ioprio);
			break;
		case IOPRIO_WHO_PGRP:
			if (!who)
105 106
				pgrp = task_pgrp(current);
			else
107
				pgrp = find_vpid(who);
108
			do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
109 110 111
				ret = set_task_ioprio(p, ioprio);
				if (ret)
					break;
112
			} while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
113 114
			break;
		case IOPRIO_WHO_USER:
115 116 117
			uid = make_kuid(current_user_ns(), who);
			if (!uid_valid(uid))
				break;
118
			if (!who)
119
				user = current_user();
120
			else
121
				user = find_user(uid);
122 123 124 125

			if (!user)
				break;

126
			for_each_process_thread(g, p) {
127 128
				if (!uid_eq(task_uid(p), uid) ||
				    !task_pid_vnr(p))
129 130 131
					continue;
				ret = set_task_ioprio(p, ioprio);
				if (ret)
132
					goto free_uid;
133
			}
134
free_uid:
135 136 137 138 139 140 141
			if (who)
				free_uid(user);
			break;
		default:
			ret = -EINVAL;
	}

142
	rcu_read_unlock();
143 144 145
	return ret;
}

146 147 148 149 150 151 152
static int get_task_ioprio(struct task_struct *p)
{
	int ret;

	ret = security_task_getioprio(p);
	if (ret)
		goto out;
153
	ret = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_NONE, IOPRIO_NORM);
154
	task_lock(p);
155 156
	if (p->io_context)
		ret = p->io_context->ioprio;
157
	task_unlock(p);
158 159 160 161
out:
	return ret;
}

O
Oleg Nesterov 已提交
162 163
int ioprio_best(unsigned short aprio, unsigned short bprio)
{
164 165
	unsigned short aclass;
	unsigned short bclass;
O
Oleg Nesterov 已提交
166

167 168 169 170
	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);
O
Oleg Nesterov 已提交
171

172 173
	aclass = IOPRIO_PRIO_CLASS(aprio);
	bclass = IOPRIO_PRIO_CLASS(bprio);
O
Oleg Nesterov 已提交
174 175 176 177 178 179 180 181
	if (aclass == bclass)
		return min(aprio, bprio);
	if (aclass > bclass)
		return bprio;
	else
		return aprio;
}

182
SYSCALL_DEFINE2(ioprio_get, int, which, int, who)
183 184 185
{
	struct task_struct *g, *p;
	struct user_struct *user;
186
	struct pid *pgrp;
187
	kuid_t uid;
188
	int ret = -ESRCH;
189
	int tmpio;
190

191
	rcu_read_lock();
192 193 194 195 196
	switch (which) {
		case IOPRIO_WHO_PROCESS:
			if (!who)
				p = current;
			else
197
				p = find_task_by_vpid(who);
198
			if (p)
199
				ret = get_task_ioprio(p);
200 201 202
			break;
		case IOPRIO_WHO_PGRP:
			if (!who)
203 204
				pgrp = task_pgrp(current);
			else
205
				pgrp = find_vpid(who);
206
			do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
207 208 209
				tmpio = get_task_ioprio(p);
				if (tmpio < 0)
					continue;
210
				if (ret == -ESRCH)
211
					ret = tmpio;
212
				else
213
					ret = ioprio_best(ret, tmpio);
214
			} while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
215 216
			break;
		case IOPRIO_WHO_USER:
217
			uid = make_kuid(current_user_ns(), who);
218
			if (!who)
219
				user = current_user();
220
			else
221
				user = find_user(uid);
222 223 224 225

			if (!user)
				break;

226
			for_each_process_thread(g, p) {
227 228
				if (!uid_eq(task_uid(p), user->uid) ||
				    !task_pid_vnr(p))
229
					continue;
230 231 232
				tmpio = get_task_ioprio(p);
				if (tmpio < 0)
					continue;
233
				if (ret == -ESRCH)
234
					ret = tmpio;
235
				else
236
					ret = ioprio_best(ret, tmpio);
237
			}
238 239 240 241 242 243 244 245

			if (who)
				free_uid(user);
			break;
		default:
			ret = -EINVAL;
	}

246
	rcu_read_unlock();
247 248
	return ret;
}