]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - block/ioprio.c
sched/headers: Prepare header dependency changes, move the <asm/paravirt.h> include...
[mirror_ubuntu-artful-kernel.git] / block / ioprio.c
CommitLineData
22e2c507
JA
1/*
2 * fs/ioprio.c
3 *
0fe23479 4 * Copyright (C) 2004 Jens Axboe <axboe@kernel.dk>
22e2c507
JA
5 *
6 * Helper functions for setting/querying io priorities of processes. The
7 * system calls closely mimmick getpriority/setpriority, see the man page for
8 * those. The prio argument is a composite of prio class and prio data, where
9 * the data argument has meaning within that class. The standard scheduling
10 * classes have 8 distinct prio levels, with 0 being the highest prio and 7
11 * being the lowest.
12 *
13 * IOW, setting BE scheduling class with prio 2 is done ala:
14 *
15 * unsigned int prio = (IOPRIO_CLASS_BE << IOPRIO_CLASS_SHIFT) | 2;
16 *
17 * ioprio_set(PRIO_PROCESS, pid, prio);
18 *
19 * See also Documentation/block/ioprio.txt
20 *
21 */
5a0e3ad6 22#include <linux/gfp.h>
22e2c507 23#include <linux/kernel.h>
afeacc8c 24#include <linux/export.h>
22e2c507
JA
25#include <linux/ioprio.h>
26#include <linux/blkdev.h>
16f7e0fe 27#include <linux/capability.h>
8703e8a4 28#include <linux/sched/user.h>
9abdc4cd 29#include <linux/syscalls.h>
03e68060 30#include <linux/security.h>
b488893a 31#include <linux/pid_namespace.h>
22e2c507 32
b3881f74 33int set_task_ioprio(struct task_struct *task, int ioprio)
22e2c507 34{
03e68060 35 int err;
22e2c507 36 struct io_context *ioc;
c69e8d9c 37 const struct cred *cred = current_cred(), *tcred;
22e2c507 38
c69e8d9c
DH
39 rcu_read_lock();
40 tcred = __task_cred(task);
8e96e3b7
EB
41 if (!uid_eq(tcred->uid, cred->euid) &&
42 !uid_eq(tcred->uid, cred->uid) && !capable(CAP_SYS_NICE)) {
c69e8d9c 43 rcu_read_unlock();
22e2c507 44 return -EPERM;
c69e8d9c
DH
45 }
46 rcu_read_unlock();
22e2c507 47
03e68060
JM
48 err = security_task_setioprio(task, ioprio);
49 if (err)
50 return err;
51
6e736be7
TH
52 ioc = get_task_io_context(task, GFP_ATOMIC, NUMA_NO_NODE);
53 if (ioc) {
2b566fa5 54 ioc->ioprio = ioprio;
11a3122f 55 put_io_context(ioc);
fd0928df 56 }
22e2c507 57
fd0928df 58 return err;
22e2c507 59}
b3881f74 60EXPORT_SYMBOL_GPL(set_task_ioprio);
22e2c507 61
938bb9f5 62SYSCALL_DEFINE3(ioprio_set, int, which, int, who, int, ioprio)
22e2c507
JA
63{
64 int class = IOPRIO_PRIO_CLASS(ioprio);
65 int data = IOPRIO_PRIO_DATA(ioprio);
66 struct task_struct *p, *g;
67 struct user_struct *user;
41487c65 68 struct pid *pgrp;
7b44ab97 69 kuid_t uid;
22e2c507
JA
70 int ret;
71
72 switch (class) {
73 case IOPRIO_CLASS_RT:
74 if (!capable(CAP_SYS_ADMIN))
75 return -EPERM;
76 /* fall through, rt has prio field too */
77 case IOPRIO_CLASS_BE:
78 if (data >= IOPRIO_BE_NR || data < 0)
79 return -EINVAL;
80
81 break;
82 case IOPRIO_CLASS_IDLE:
83 break;
8ec680e4
JA
84 case IOPRIO_CLASS_NONE:
85 if (data)
86 return -EINVAL;
87 break;
22e2c507
JA
88 default:
89 return -EINVAL;
90 }
91
92 ret = -ESRCH;
d69b78ba 93 rcu_read_lock();
22e2c507
JA
94 switch (which) {
95 case IOPRIO_WHO_PROCESS:
96 if (!who)
97 p = current;
98 else
228ebcbe 99 p = find_task_by_vpid(who);
22e2c507
JA
100 if (p)
101 ret = set_task_ioprio(p, ioprio);
102 break;
103 case IOPRIO_WHO_PGRP:
104 if (!who)
41487c65
EB
105 pgrp = task_pgrp(current);
106 else
b488893a 107 pgrp = find_vpid(who);
2d70b68d 108 do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
22e2c507
JA
109 ret = set_task_ioprio(p, ioprio);
110 if (ret)
111 break;
2d70b68d 112 } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
22e2c507
JA
113 break;
114 case IOPRIO_WHO_USER:
7b44ab97
EB
115 uid = make_kuid(current_user_ns(), who);
116 if (!uid_valid(uid))
117 break;
22e2c507 118 if (!who)
86a264ab 119 user = current_user();
22e2c507 120 else
7b44ab97 121 user = find_user(uid);
22e2c507
JA
122
123 if (!user)
124 break;
125
612dafab 126 for_each_process_thread(g, p) {
8639b461
BS
127 if (!uid_eq(task_uid(p), uid) ||
128 !task_pid_vnr(p))
22e2c507
JA
129 continue;
130 ret = set_task_ioprio(p, ioprio);
131 if (ret)
78bd4d48 132 goto free_uid;
612dafab 133 }
78bd4d48 134free_uid:
22e2c507
JA
135 if (who)
136 free_uid(user);
137 break;
138 default:
139 ret = -EINVAL;
140 }
141
d69b78ba 142 rcu_read_unlock();
22e2c507
JA
143 return ret;
144}
145
a1836a42
DQ
146static int get_task_ioprio(struct task_struct *p)
147{
148 int ret;
149
150 ret = security_task_getioprio(p);
151 if (ret)
152 goto out;
fd0928df 153 ret = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_NONE, IOPRIO_NORM);
8ba86821 154 task_lock(p);
fd0928df
JA
155 if (p->io_context)
156 ret = p->io_context->ioprio;
8ba86821 157 task_unlock(p);
a1836a42
DQ
158out:
159 return ret;
160}
161
e014ff8d
ON
162int ioprio_best(unsigned short aprio, unsigned short bprio)
163{
ece9c72a
JK
164 unsigned short aclass;
165 unsigned short bclass;
e014ff8d 166
ece9c72a
JK
167 if (!ioprio_valid(aprio))
168 aprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, IOPRIO_NORM);
169 if (!ioprio_valid(bprio))
170 bprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, IOPRIO_NORM);
e014ff8d 171
ece9c72a
JK
172 aclass = IOPRIO_PRIO_CLASS(aprio);
173 bclass = IOPRIO_PRIO_CLASS(bprio);
e014ff8d
ON
174 if (aclass == bclass)
175 return min(aprio, bprio);
176 if (aclass > bclass)
177 return bprio;
178 else
179 return aprio;
180}
181
938bb9f5 182SYSCALL_DEFINE2(ioprio_get, int, which, int, who)
22e2c507
JA
183{
184 struct task_struct *g, *p;
185 struct user_struct *user;
41487c65 186 struct pid *pgrp;
7b44ab97 187 kuid_t uid;
22e2c507 188 int ret = -ESRCH;
a1836a42 189 int tmpio;
22e2c507 190
d69b78ba 191 rcu_read_lock();
22e2c507
JA
192 switch (which) {
193 case IOPRIO_WHO_PROCESS:
194 if (!who)
195 p = current;
196 else
228ebcbe 197 p = find_task_by_vpid(who);
22e2c507 198 if (p)
a1836a42 199 ret = get_task_ioprio(p);
22e2c507
JA
200 break;
201 case IOPRIO_WHO_PGRP:
202 if (!who)
41487c65
EB
203 pgrp = task_pgrp(current);
204 else
b488893a 205 pgrp = find_vpid(who);
2d70b68d 206 do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
a1836a42
DQ
207 tmpio = get_task_ioprio(p);
208 if (tmpio < 0)
209 continue;
22e2c507 210 if (ret == -ESRCH)
a1836a42 211 ret = tmpio;
22e2c507 212 else
a1836a42 213 ret = ioprio_best(ret, tmpio);
2d70b68d 214 } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
22e2c507
JA
215 break;
216 case IOPRIO_WHO_USER:
7b44ab97 217 uid = make_kuid(current_user_ns(), who);
22e2c507 218 if (!who)
86a264ab 219 user = current_user();
22e2c507 220 else
7b44ab97 221 user = find_user(uid);
22e2c507
JA
222
223 if (!user)
224 break;
225
612dafab 226 for_each_process_thread(g, p) {
8639b461
BS
227 if (!uid_eq(task_uid(p), user->uid) ||
228 !task_pid_vnr(p))
22e2c507 229 continue;
a1836a42
DQ
230 tmpio = get_task_ioprio(p);
231 if (tmpio < 0)
232 continue;
22e2c507 233 if (ret == -ESRCH)
a1836a42 234 ret = tmpio;
22e2c507 235 else
a1836a42 236 ret = ioprio_best(ret, tmpio);
612dafab 237 }
22e2c507
JA
238
239 if (who)
240 free_uid(user);
241 break;
242 default:
243 ret = -EINVAL;
244 }
245
d69b78ba 246 rcu_read_unlock();
22e2c507
JA
247 return ret;
248}