blob: 3569e0ad86a2e0ccc96cef6862b93b1e19f7b14e [file] [log] [blame]
Jens Axboe22e2c502005-06-27 10:55:12 +02001/*
2 * fs/ioprio.c
3 *
Jens Axboe0fe23472006-09-04 15:41:16 +02004 * Copyright (C) 2004 Jens Axboe <axboe@kernel.dk>
Jens Axboe22e2c502005-06-27 10:55:12 +02005 *
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 */
22#include <linux/kernel.h>
23#include <linux/ioprio.h>
24#include <linux/blkdev.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080025#include <linux/capability.h>
Adrian Bunk9abdc4c2005-11-08 16:57:02 +010026#include <linux/syscalls.h>
James Morris03e68062006-06-23 02:03:58 -070027#include <linux/security.h>
Pavel Emelyanovb4888932007-10-18 23:40:14 -070028#include <linux/pid_namespace.h>
Jens Axboe22e2c502005-06-27 10:55:12 +020029
30static int set_task_ioprio(struct task_struct *task, int ioprio)
31{
James Morris03e68062006-06-23 02:03:58 -070032 int err;
Jens Axboe22e2c502005-06-27 10:55:12 +020033 struct io_context *ioc;
David Howellsc69e8d92008-11-14 10:39:19 +110034 const struct cred *cred = current_cred(), *tcred;
Jens Axboe22e2c502005-06-27 10:55:12 +020035
David Howellsc69e8d92008-11-14 10:39:19 +110036 rcu_read_lock();
37 tcred = __task_cred(task);
38 if (tcred->uid != cred->euid &&
39 tcred->uid != cred->uid && !capable(CAP_SYS_NICE)) {
40 rcu_read_unlock();
Jens Axboe22e2c502005-06-27 10:55:12 +020041 return -EPERM;
David Howellsc69e8d92008-11-14 10:39:19 +110042 }
43 rcu_read_unlock();
Jens Axboe22e2c502005-06-27 10:55:12 +020044
James Morris03e68062006-06-23 02:03:58 -070045 err = security_task_setioprio(task, ioprio);
46 if (err)
47 return err;
48
Jens Axboe22e2c502005-06-27 10:55:12 +020049 task_lock(task);
Jens Axboefd0928d2008-01-24 08:52:45 +010050 do {
51 ioc = task->io_context;
52 /* see wmb() in current_io_context() */
53 smp_read_barrier_depends();
54 if (ioc)
55 break;
Jens Axboe22e2c502005-06-27 10:55:12 +020056
Jens Axboefd0928d2008-01-24 08:52:45 +010057 ioc = alloc_io_context(GFP_ATOMIC, -1);
58 if (!ioc) {
59 err = -ENOMEM;
60 break;
61 }
62 task->io_context = ioc;
Jens Axboefd0928d2008-01-24 08:52:45 +010063 } while (1);
Jens Axboe22e2c502005-06-27 10:55:12 +020064
Jens Axboefd0928d2008-01-24 08:52:45 +010065 if (!err) {
66 ioc->ioprio = ioprio;
Jens Axboefc463792006-08-29 09:05:44 +020067 ioc->ioprio_changed = 1;
Jens Axboefd0928d2008-01-24 08:52:45 +010068 }
Jens Axboe22e2c502005-06-27 10:55:12 +020069
70 task_unlock(task);
Jens Axboefd0928d2008-01-24 08:52:45 +010071 return err;
Jens Axboe22e2c502005-06-27 10:55:12 +020072}
73
Anton Blanchardcf366802005-07-07 17:56:13 -070074asmlinkage long sys_ioprio_set(int which, int who, int ioprio)
Jens Axboe22e2c502005-06-27 10:55:12 +020075{
76 int class = IOPRIO_PRIO_CLASS(ioprio);
77 int data = IOPRIO_PRIO_DATA(ioprio);
78 struct task_struct *p, *g;
79 struct user_struct *user;
Eric W. Biederman41487c62007-02-12 00:53:01 -080080 struct pid *pgrp;
Jens Axboe22e2c502005-06-27 10:55:12 +020081 int ret;
82
83 switch (class) {
84 case IOPRIO_CLASS_RT:
85 if (!capable(CAP_SYS_ADMIN))
86 return -EPERM;
87 /* fall through, rt has prio field too */
88 case IOPRIO_CLASS_BE:
89 if (data >= IOPRIO_BE_NR || data < 0)
90 return -EINVAL;
91
92 break;
93 case IOPRIO_CLASS_IDLE:
94 break;
Jens Axboe8ec680e2007-11-07 13:54:07 +010095 case IOPRIO_CLASS_NONE:
96 if (data)
97 return -EINVAL;
98 break;
Jens Axboe22e2c502005-06-27 10:55:12 +020099 default:
100 return -EINVAL;
101 }
102
103 ret = -ESRCH;
Oleg Nesterovcf342e52006-08-29 09:17:41 +0200104 /*
105 * We want IOPRIO_WHO_PGRP/IOPRIO_WHO_USER to be "atomic",
106 * so we can't use rcu_read_lock(). See re-copy of ->ioprio
107 * in copy_process().
108 */
109 read_lock(&tasklist_lock);
Jens Axboe22e2c502005-06-27 10:55:12 +0200110 switch (which) {
111 case IOPRIO_WHO_PROCESS:
112 if (!who)
113 p = current;
114 else
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -0700115 p = find_task_by_vpid(who);
Jens Axboe22e2c502005-06-27 10:55:12 +0200116 if (p)
117 ret = set_task_ioprio(p, ioprio);
118 break;
119 case IOPRIO_WHO_PGRP:
120 if (!who)
Eric W. Biederman41487c62007-02-12 00:53:01 -0800121 pgrp = task_pgrp(current);
122 else
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700123 pgrp = find_vpid(who);
Ken Chen2d70b682008-08-20 14:09:17 -0700124 do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
Jens Axboe22e2c502005-06-27 10:55:12 +0200125 ret = set_task_ioprio(p, ioprio);
126 if (ret)
127 break;
Ken Chen2d70b682008-08-20 14:09:17 -0700128 } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
Jens Axboe22e2c502005-06-27 10:55:12 +0200129 break;
130 case IOPRIO_WHO_USER:
131 if (!who)
David Howells86a264a2008-11-14 10:39:18 +1100132 user = current_user();
Jens Axboe22e2c502005-06-27 10:55:12 +0200133 else
134 user = find_user(who);
135
136 if (!user)
137 break;
138
139 do_each_thread(g, p) {
David Howellsc69e8d92008-11-14 10:39:19 +1100140 if (__task_cred(p)->uid != who)
Jens Axboe22e2c502005-06-27 10:55:12 +0200141 continue;
142 ret = set_task_ioprio(p, ioprio);
143 if (ret)
Oleg Nesterov78bd4d42006-08-21 08:33:23 +0200144 goto free_uid;
Jens Axboe22e2c502005-06-27 10:55:12 +0200145 } while_each_thread(g, p);
Oleg Nesterov78bd4d42006-08-21 08:33:23 +0200146free_uid:
Jens Axboe22e2c502005-06-27 10:55:12 +0200147 if (who)
148 free_uid(user);
149 break;
150 default:
151 ret = -EINVAL;
152 }
153
Oleg Nesterovcf342e52006-08-29 09:17:41 +0200154 read_unlock(&tasklist_lock);
Jens Axboe22e2c502005-06-27 10:55:12 +0200155 return ret;
156}
157
David Quigleya1836a42006-06-30 01:55:49 -0700158static int get_task_ioprio(struct task_struct *p)
159{
160 int ret;
161
162 ret = security_task_getioprio(p);
163 if (ret)
164 goto out;
Jens Axboefd0928d2008-01-24 08:52:45 +0100165 ret = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_NONE, IOPRIO_NORM);
166 if (p->io_context)
167 ret = p->io_context->ioprio;
David Quigleya1836a42006-06-30 01:55:49 -0700168out:
169 return ret;
170}
171
Oleg Nesterove014ff82006-08-21 10:02:50 +0200172int ioprio_best(unsigned short aprio, unsigned short bprio)
173{
174 unsigned short aclass = IOPRIO_PRIO_CLASS(aprio);
175 unsigned short bclass = IOPRIO_PRIO_CLASS(bprio);
176
Oleg Nesterove014ff82006-08-21 10:02:50 +0200177 if (aclass == IOPRIO_CLASS_NONE)
178 aclass = IOPRIO_CLASS_BE;
179 if (bclass == IOPRIO_CLASS_NONE)
180 bclass = IOPRIO_CLASS_BE;
181
182 if (aclass == bclass)
183 return min(aprio, bprio);
184 if (aclass > bclass)
185 return bprio;
186 else
187 return aprio;
188}
189
Anton Blanchardcf366802005-07-07 17:56:13 -0700190asmlinkage long sys_ioprio_get(int which, int who)
Jens Axboe22e2c502005-06-27 10:55:12 +0200191{
192 struct task_struct *g, *p;
193 struct user_struct *user;
Eric W. Biederman41487c62007-02-12 00:53:01 -0800194 struct pid *pgrp;
Jens Axboe22e2c502005-06-27 10:55:12 +0200195 int ret = -ESRCH;
David Quigleya1836a42006-06-30 01:55:49 -0700196 int tmpio;
Jens Axboe22e2c502005-06-27 10:55:12 +0200197
Oleg Nesterovcf342e52006-08-29 09:17:41 +0200198 read_lock(&tasklist_lock);
Jens Axboe22e2c502005-06-27 10:55:12 +0200199 switch (which) {
200 case IOPRIO_WHO_PROCESS:
201 if (!who)
202 p = current;
203 else
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -0700204 p = find_task_by_vpid(who);
Jens Axboe22e2c502005-06-27 10:55:12 +0200205 if (p)
David Quigleya1836a42006-06-30 01:55:49 -0700206 ret = get_task_ioprio(p);
Jens Axboe22e2c502005-06-27 10:55:12 +0200207 break;
208 case IOPRIO_WHO_PGRP:
209 if (!who)
Eric W. Biederman41487c62007-02-12 00:53:01 -0800210 pgrp = task_pgrp(current);
211 else
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700212 pgrp = find_vpid(who);
Ken Chen2d70b682008-08-20 14:09:17 -0700213 do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
David Quigleya1836a42006-06-30 01:55:49 -0700214 tmpio = get_task_ioprio(p);
215 if (tmpio < 0)
216 continue;
Jens Axboe22e2c502005-06-27 10:55:12 +0200217 if (ret == -ESRCH)
David Quigleya1836a42006-06-30 01:55:49 -0700218 ret = tmpio;
Jens Axboe22e2c502005-06-27 10:55:12 +0200219 else
David Quigleya1836a42006-06-30 01:55:49 -0700220 ret = ioprio_best(ret, tmpio);
Ken Chen2d70b682008-08-20 14:09:17 -0700221 } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
Jens Axboe22e2c502005-06-27 10:55:12 +0200222 break;
223 case IOPRIO_WHO_USER:
224 if (!who)
David Howells86a264a2008-11-14 10:39:18 +1100225 user = current_user();
Jens Axboe22e2c502005-06-27 10:55:12 +0200226 else
227 user = find_user(who);
228
229 if (!user)
230 break;
231
232 do_each_thread(g, p) {
David Howellsc69e8d92008-11-14 10:39:19 +1100233 if (__task_cred(p)->uid != user->uid)
Jens Axboe22e2c502005-06-27 10:55:12 +0200234 continue;
David Quigleya1836a42006-06-30 01:55:49 -0700235 tmpio = get_task_ioprio(p);
236 if (tmpio < 0)
237 continue;
Jens Axboe22e2c502005-06-27 10:55:12 +0200238 if (ret == -ESRCH)
David Quigleya1836a42006-06-30 01:55:49 -0700239 ret = tmpio;
Jens Axboe22e2c502005-06-27 10:55:12 +0200240 else
David Quigleya1836a42006-06-30 01:55:49 -0700241 ret = ioprio_best(ret, tmpio);
Jens Axboe22e2c502005-06-27 10:55:12 +0200242 } while_each_thread(g, p);
243
244 if (who)
245 free_uid(user);
246 break;
247 default:
248 ret = -EINVAL;
249 }
250
Oleg Nesterovcf342e52006-08-29 09:17:41 +0200251 read_unlock(&tasklist_lock);
Jens Axboe22e2c502005-06-27 10:55:12 +0200252 return ret;
253}
254