blob: 1a39ac3709425bf761ae466ad7529703a4c81c7b [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
Theodore Ts'ob3881f72009-01-05 22:46:26 -050030int set_task_ioprio(struct task_struct *task, int ioprio)
Jens Axboe22e2c502005-06-27 10:55:12 +020031{
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}
Theodore Ts'ob3881f72009-01-05 22:46:26 -050073EXPORT_SYMBOL_GPL(set_task_ioprio);
Jens Axboe22e2c502005-06-27 10:55:12 +020074
Anton Blanchardcf366802005-07-07 17:56:13 -070075asmlinkage long sys_ioprio_set(int which, int who, int ioprio)
Jens Axboe22e2c502005-06-27 10:55:12 +020076{
77 int class = IOPRIO_PRIO_CLASS(ioprio);
78 int data = IOPRIO_PRIO_DATA(ioprio);
79 struct task_struct *p, *g;
80 struct user_struct *user;
Eric W. Biederman41487c62007-02-12 00:53:01 -080081 struct pid *pgrp;
Jens Axboe22e2c502005-06-27 10:55:12 +020082 int ret;
83
84 switch (class) {
85 case IOPRIO_CLASS_RT:
86 if (!capable(CAP_SYS_ADMIN))
87 return -EPERM;
88 /* fall through, rt has prio field too */
89 case IOPRIO_CLASS_BE:
90 if (data >= IOPRIO_BE_NR || data < 0)
91 return -EINVAL;
92
93 break;
94 case IOPRIO_CLASS_IDLE:
95 break;
Jens Axboe8ec680e2007-11-07 13:54:07 +010096 case IOPRIO_CLASS_NONE:
97 if (data)
98 return -EINVAL;
99 break;
Jens Axboe22e2c502005-06-27 10:55:12 +0200100 default:
101 return -EINVAL;
102 }
103
104 ret = -ESRCH;
Oleg Nesterovcf342e52006-08-29 09:17:41 +0200105 /*
106 * We want IOPRIO_WHO_PGRP/IOPRIO_WHO_USER to be "atomic",
107 * so we can't use rcu_read_lock(). See re-copy of ->ioprio
108 * in copy_process().
109 */
110 read_lock(&tasklist_lock);
Jens Axboe22e2c502005-06-27 10:55:12 +0200111 switch (which) {
112 case IOPRIO_WHO_PROCESS:
113 if (!who)
114 p = current;
115 else
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -0700116 p = find_task_by_vpid(who);
Jens Axboe22e2c502005-06-27 10:55:12 +0200117 if (p)
118 ret = set_task_ioprio(p, ioprio);
119 break;
120 case IOPRIO_WHO_PGRP:
121 if (!who)
Eric W. Biederman41487c62007-02-12 00:53:01 -0800122 pgrp = task_pgrp(current);
123 else
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700124 pgrp = find_vpid(who);
Ken Chen2d70b682008-08-20 14:09:17 -0700125 do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
Jens Axboe22e2c502005-06-27 10:55:12 +0200126 ret = set_task_ioprio(p, ioprio);
127 if (ret)
128 break;
Ken Chen2d70b682008-08-20 14:09:17 -0700129 } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
Jens Axboe22e2c502005-06-27 10:55:12 +0200130 break;
131 case IOPRIO_WHO_USER:
132 if (!who)
David Howells86a264a2008-11-14 10:39:18 +1100133 user = current_user();
Jens Axboe22e2c502005-06-27 10:55:12 +0200134 else
135 user = find_user(who);
136
137 if (!user)
138 break;
139
140 do_each_thread(g, p) {
David Howellsc69e8d92008-11-14 10:39:19 +1100141 if (__task_cred(p)->uid != who)
Jens Axboe22e2c502005-06-27 10:55:12 +0200142 continue;
143 ret = set_task_ioprio(p, ioprio);
144 if (ret)
Oleg Nesterov78bd4d42006-08-21 08:33:23 +0200145 goto free_uid;
Jens Axboe22e2c502005-06-27 10:55:12 +0200146 } while_each_thread(g, p);
Oleg Nesterov78bd4d42006-08-21 08:33:23 +0200147free_uid:
Jens Axboe22e2c502005-06-27 10:55:12 +0200148 if (who)
149 free_uid(user);
150 break;
151 default:
152 ret = -EINVAL;
153 }
154
Oleg Nesterovcf342e52006-08-29 09:17:41 +0200155 read_unlock(&tasklist_lock);
Jens Axboe22e2c502005-06-27 10:55:12 +0200156 return ret;
157}
158
David Quigleya1836a42006-06-30 01:55:49 -0700159static int get_task_ioprio(struct task_struct *p)
160{
161 int ret;
162
163 ret = security_task_getioprio(p);
164 if (ret)
165 goto out;
Jens Axboefd0928d2008-01-24 08:52:45 +0100166 ret = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_NONE, IOPRIO_NORM);
167 if (p->io_context)
168 ret = p->io_context->ioprio;
David Quigleya1836a42006-06-30 01:55:49 -0700169out:
170 return ret;
171}
172
Oleg Nesterove014ff82006-08-21 10:02:50 +0200173int ioprio_best(unsigned short aprio, unsigned short bprio)
174{
175 unsigned short aclass = IOPRIO_PRIO_CLASS(aprio);
176 unsigned short bclass = IOPRIO_PRIO_CLASS(bprio);
177
Oleg Nesterove014ff82006-08-21 10:02:50 +0200178 if (aclass == IOPRIO_CLASS_NONE)
179 aclass = IOPRIO_CLASS_BE;
180 if (bclass == IOPRIO_CLASS_NONE)
181 bclass = IOPRIO_CLASS_BE;
182
183 if (aclass == bclass)
184 return min(aprio, bprio);
185 if (aclass > bclass)
186 return bprio;
187 else
188 return aprio;
189}
190
Anton Blanchardcf366802005-07-07 17:56:13 -0700191asmlinkage long sys_ioprio_get(int which, int who)
Jens Axboe22e2c502005-06-27 10:55:12 +0200192{
193 struct task_struct *g, *p;
194 struct user_struct *user;
Eric W. Biederman41487c62007-02-12 00:53:01 -0800195 struct pid *pgrp;
Jens Axboe22e2c502005-06-27 10:55:12 +0200196 int ret = -ESRCH;
David Quigleya1836a42006-06-30 01:55:49 -0700197 int tmpio;
Jens Axboe22e2c502005-06-27 10:55:12 +0200198
Oleg Nesterovcf342e52006-08-29 09:17:41 +0200199 read_lock(&tasklist_lock);
Jens Axboe22e2c502005-06-27 10:55:12 +0200200 switch (which) {
201 case IOPRIO_WHO_PROCESS:
202 if (!who)
203 p = current;
204 else
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -0700205 p = find_task_by_vpid(who);
Jens Axboe22e2c502005-06-27 10:55:12 +0200206 if (p)
David Quigleya1836a42006-06-30 01:55:49 -0700207 ret = get_task_ioprio(p);
Jens Axboe22e2c502005-06-27 10:55:12 +0200208 break;
209 case IOPRIO_WHO_PGRP:
210 if (!who)
Eric W. Biederman41487c62007-02-12 00:53:01 -0800211 pgrp = task_pgrp(current);
212 else
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700213 pgrp = find_vpid(who);
Ken Chen2d70b682008-08-20 14:09:17 -0700214 do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
David Quigleya1836a42006-06-30 01:55:49 -0700215 tmpio = get_task_ioprio(p);
216 if (tmpio < 0)
217 continue;
Jens Axboe22e2c502005-06-27 10:55:12 +0200218 if (ret == -ESRCH)
David Quigleya1836a42006-06-30 01:55:49 -0700219 ret = tmpio;
Jens Axboe22e2c502005-06-27 10:55:12 +0200220 else
David Quigleya1836a42006-06-30 01:55:49 -0700221 ret = ioprio_best(ret, tmpio);
Ken Chen2d70b682008-08-20 14:09:17 -0700222 } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
Jens Axboe22e2c502005-06-27 10:55:12 +0200223 break;
224 case IOPRIO_WHO_USER:
225 if (!who)
David Howells86a264a2008-11-14 10:39:18 +1100226 user = current_user();
Jens Axboe22e2c502005-06-27 10:55:12 +0200227 else
228 user = find_user(who);
229
230 if (!user)
231 break;
232
233 do_each_thread(g, p) {
David Howellsc69e8d92008-11-14 10:39:19 +1100234 if (__task_cred(p)->uid != user->uid)
Jens Axboe22e2c502005-06-27 10:55:12 +0200235 continue;
David Quigleya1836a42006-06-30 01:55:49 -0700236 tmpio = get_task_ioprio(p);
237 if (tmpio < 0)
238 continue;
Jens Axboe22e2c502005-06-27 10:55:12 +0200239 if (ret == -ESRCH)
David Quigleya1836a42006-06-30 01:55:49 -0700240 ret = tmpio;
Jens Axboe22e2c502005-06-27 10:55:12 +0200241 else
David Quigleya1836a42006-06-30 01:55:49 -0700242 ret = ioprio_best(ret, tmpio);
Jens Axboe22e2c502005-06-27 10:55:12 +0200243 } while_each_thread(g, p);
244
245 if (who)
246 free_uid(user);
247 break;
248 default:
249 ret = -EINVAL;
250 }
251
Oleg Nesterovcf342e52006-08-29 09:17:41 +0200252 read_unlock(&tasklist_lock);
Jens Axboe22e2c502005-06-27 10:55:12 +0200253 return ret;
254}
255