ioprio.c 5 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 24 25
#include <linux/kernel.h>
#include <linux/ioprio.h>
#include <linux/blkdev.h>
26
#include <linux/capability.h>
27
#include <linux/syscalls.h>
28
#include <linux/security.h>
29
#include <linux/pid_namespace.h>
30

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

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

46 47 48 49
	err = security_task_setioprio(task, ioprio);
	if (err)
		return err;

50
	task_lock(task);
51 52 53 54 55 56
	do {
		ioc = task->io_context;
		/* see wmb() in current_io_context() */
		smp_read_barrier_depends();
		if (ioc)
			break;
57

58 59 60 61 62 63 64
		ioc = alloc_io_context(GFP_ATOMIC, -1);
		if (!ioc) {
			err = -ENOMEM;
			break;
		}
		task->io_context = ioc;
	} while (1);
65

66 67
	if (!err) {
		ioc->ioprio = ioprio;
68
		ioc->ioprio_changed = 1;
69
	}
70 71

	task_unlock(task);
72
	return err;
73
}
74
EXPORT_SYMBOL_GPL(set_task_ioprio);
75

76
SYSCALL_DEFINE3(ioprio_set, int, which, int, who, int, ioprio)
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;
82
	struct pid *pgrp;
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;
97 98 99 100
		case IOPRIO_CLASS_NONE:
			if (data)
				return -EINVAL;
			break;
101 102 103 104 105
		default:
			return -EINVAL;
	}

	ret = -ESRCH;
106
	rcu_read_lock();
107 108 109 110 111
	switch (which) {
		case IOPRIO_WHO_PROCESS:
			if (!who)
				p = current;
			else
112
				p = find_task_by_vpid(who);
113 114 115 116 117
			if (p)
				ret = set_task_ioprio(p, ioprio);
			break;
		case IOPRIO_WHO_PGRP:
			if (!who)
118 119
				pgrp = task_pgrp(current);
			else
120
				pgrp = find_vpid(who);
121
			do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
122 123 124
				ret = set_task_ioprio(p, ioprio);
				if (ret)
					break;
125
			} while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
126 127 128
			break;
		case IOPRIO_WHO_USER:
			if (!who)
129
				user = current_user();
130 131 132 133 134 135 136
			else
				user = find_user(who);

			if (!user)
				break;

			do_each_thread(g, p) {
137
				if (__task_cred(p)->uid != who)
138 139 140
					continue;
				ret = set_task_ioprio(p, ioprio);
				if (ret)
141
					goto free_uid;
142
			} while_each_thread(g, p);
143
free_uid:
144 145 146 147 148 149 150
			if (who)
				free_uid(user);
			break;
		default:
			ret = -EINVAL;
	}

151
	rcu_read_unlock();
152 153 154
	return ret;
}

155 156 157 158 159 160 161
static int get_task_ioprio(struct task_struct *p)
{
	int ret;

	ret = security_task_getioprio(p);
	if (ret)
		goto out;
162 163 164
	ret = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_NONE, IOPRIO_NORM);
	if (p->io_context)
		ret = p->io_context->ioprio;
165 166 167 168
out:
	return ret;
}

169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
int ioprio_best(unsigned short aprio, unsigned short bprio)
{
	unsigned short aclass = IOPRIO_PRIO_CLASS(aprio);
	unsigned short bclass = IOPRIO_PRIO_CLASS(bprio);

	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;
}

187
SYSCALL_DEFINE2(ioprio_get, int, which, int, who)
188 189 190
{
	struct task_struct *g, *p;
	struct user_struct *user;
191
	struct pid *pgrp;
192
	int ret = -ESRCH;
193
	int tmpio;
194

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

			if (!user)
				break;

			do_each_thread(g, p) {
230
				if (__task_cred(p)->uid != user->uid)
231
					continue;
232 233 234
				tmpio = get_task_ioprio(p);
				if (tmpio < 0)
					continue;
235
				if (ret == -ESRCH)
236
					ret = tmpio;
237
				else
238
					ret = ioprio_best(ret, tmpio);
239 240 241 242 243 244 245 246 247
			} while_each_thread(g, p);

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

248
	rcu_read_unlock();
249 250
	return ret;
}