blob: 5112554fd210a100a4a58ef1aa4e3aea61ed6629 [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;
34
David Howellsb6dff3e2008-11-14 10:39:16 +110035 if (task->cred->uid != current_euid() &&
36 task->cred->uid != current_uid() && !capable(CAP_SYS_NICE))
Jens Axboe22e2c502005-06-27 10:55:12 +020037 return -EPERM;
38
James Morris03e68062006-06-23 02:03:58 -070039 err = security_task_setioprio(task, ioprio);
40 if (err)
41 return err;
42
Jens Axboe22e2c502005-06-27 10:55:12 +020043 task_lock(task);
Jens Axboefd0928d2008-01-24 08:52:45 +010044 do {
45 ioc = task->io_context;
46 /* see wmb() in current_io_context() */
47 smp_read_barrier_depends();
48 if (ioc)
49 break;
Jens Axboe22e2c502005-06-27 10:55:12 +020050
Jens Axboefd0928d2008-01-24 08:52:45 +010051 ioc = alloc_io_context(GFP_ATOMIC, -1);
52 if (!ioc) {
53 err = -ENOMEM;
54 break;
55 }
56 task->io_context = ioc;
Jens Axboefd0928d2008-01-24 08:52:45 +010057 } while (1);
Jens Axboe22e2c502005-06-27 10:55:12 +020058
Jens Axboefd0928d2008-01-24 08:52:45 +010059 if (!err) {
60 ioc->ioprio = ioprio;
Jens Axboefc463792006-08-29 09:05:44 +020061 ioc->ioprio_changed = 1;
Jens Axboefd0928d2008-01-24 08:52:45 +010062 }
Jens Axboe22e2c502005-06-27 10:55:12 +020063
64 task_unlock(task);
Jens Axboefd0928d2008-01-24 08:52:45 +010065 return err;
Jens Axboe22e2c502005-06-27 10:55:12 +020066}
67
Anton Blanchardcf366802005-07-07 17:56:13 -070068asmlinkage long sys_ioprio_set(int which, int who, int ioprio)
Jens Axboe22e2c502005-06-27 10:55:12 +020069{
70 int class = IOPRIO_PRIO_CLASS(ioprio);
71 int data = IOPRIO_PRIO_DATA(ioprio);
72 struct task_struct *p, *g;
73 struct user_struct *user;
Eric W. Biederman41487c62007-02-12 00:53:01 -080074 struct pid *pgrp;
Jens Axboe22e2c502005-06-27 10:55:12 +020075 int ret;
76
77 switch (class) {
78 case IOPRIO_CLASS_RT:
79 if (!capable(CAP_SYS_ADMIN))
80 return -EPERM;
81 /* fall through, rt has prio field too */
82 case IOPRIO_CLASS_BE:
83 if (data >= IOPRIO_BE_NR || data < 0)
84 return -EINVAL;
85
86 break;
87 case IOPRIO_CLASS_IDLE:
88 break;
Jens Axboe8ec680e2007-11-07 13:54:07 +010089 case IOPRIO_CLASS_NONE:
90 if (data)
91 return -EINVAL;
92 break;
Jens Axboe22e2c502005-06-27 10:55:12 +020093 default:
94 return -EINVAL;
95 }
96
97 ret = -ESRCH;
Oleg Nesterovcf342e52006-08-29 09:17:41 +020098 /*
99 * We want IOPRIO_WHO_PGRP/IOPRIO_WHO_USER to be "atomic",
100 * so we can't use rcu_read_lock(). See re-copy of ->ioprio
101 * in copy_process().
102 */
103 read_lock(&tasklist_lock);
Jens Axboe22e2c502005-06-27 10:55:12 +0200104 switch (which) {
105 case IOPRIO_WHO_PROCESS:
106 if (!who)
107 p = current;
108 else
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -0700109 p = find_task_by_vpid(who);
Jens Axboe22e2c502005-06-27 10:55:12 +0200110 if (p)
111 ret = set_task_ioprio(p, ioprio);
112 break;
113 case IOPRIO_WHO_PGRP:
114 if (!who)
Eric W. Biederman41487c62007-02-12 00:53:01 -0800115 pgrp = task_pgrp(current);
116 else
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700117 pgrp = find_vpid(who);
Ken Chen2d70b682008-08-20 14:09:17 -0700118 do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
Jens Axboe22e2c502005-06-27 10:55:12 +0200119 ret = set_task_ioprio(p, ioprio);
120 if (ret)
121 break;
Ken Chen2d70b682008-08-20 14:09:17 -0700122 } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
Jens Axboe22e2c502005-06-27 10:55:12 +0200123 break;
124 case IOPRIO_WHO_USER:
125 if (!who)
David Howells86a264a2008-11-14 10:39:18 +1100126 user = current_user();
Jens Axboe22e2c502005-06-27 10:55:12 +0200127 else
128 user = find_user(who);
129
130 if (!user)
131 break;
132
133 do_each_thread(g, p) {
David Howellsb6dff3e2008-11-14 10:39:16 +1100134 if (p->cred->uid != who)
Jens Axboe22e2c502005-06-27 10:55:12 +0200135 continue;
136 ret = set_task_ioprio(p, ioprio);
137 if (ret)
Oleg Nesterov78bd4d42006-08-21 08:33:23 +0200138 goto free_uid;
Jens Axboe22e2c502005-06-27 10:55:12 +0200139 } while_each_thread(g, p);
Oleg Nesterov78bd4d42006-08-21 08:33:23 +0200140free_uid:
Jens Axboe22e2c502005-06-27 10:55:12 +0200141 if (who)
142 free_uid(user);
143 break;
144 default:
145 ret = -EINVAL;
146 }
147
Oleg Nesterovcf342e52006-08-29 09:17:41 +0200148 read_unlock(&tasklist_lock);
Jens Axboe22e2c502005-06-27 10:55:12 +0200149 return ret;
150}
151
David Quigleya1836a42006-06-30 01:55:49 -0700152static int get_task_ioprio(struct task_struct *p)
153{
154 int ret;
155
156 ret = security_task_getioprio(p);
157 if (ret)
158 goto out;
Jens Axboefd0928d2008-01-24 08:52:45 +0100159 ret = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_NONE, IOPRIO_NORM);
160 if (p->io_context)
161 ret = p->io_context->ioprio;
David Quigleya1836a42006-06-30 01:55:49 -0700162out:
163 return ret;
164}
165
Oleg Nesterove014ff82006-08-21 10:02:50 +0200166int ioprio_best(unsigned short aprio, unsigned short bprio)
167{
168 unsigned short aclass = IOPRIO_PRIO_CLASS(aprio);
169 unsigned short bclass = IOPRIO_PRIO_CLASS(bprio);
170
Oleg Nesterove014ff82006-08-21 10:02:50 +0200171 if (aclass == IOPRIO_CLASS_NONE)
172 aclass = IOPRIO_CLASS_BE;
173 if (bclass == IOPRIO_CLASS_NONE)
174 bclass = IOPRIO_CLASS_BE;
175
176 if (aclass == bclass)
177 return min(aprio, bprio);
178 if (aclass > bclass)
179 return bprio;
180 else
181 return aprio;
182}
183
Anton Blanchardcf366802005-07-07 17:56:13 -0700184asmlinkage long sys_ioprio_get(int which, int who)
Jens Axboe22e2c502005-06-27 10:55:12 +0200185{
186 struct task_struct *g, *p;
187 struct user_struct *user;
Eric W. Biederman41487c62007-02-12 00:53:01 -0800188 struct pid *pgrp;
Jens Axboe22e2c502005-06-27 10:55:12 +0200189 int ret = -ESRCH;
David Quigleya1836a42006-06-30 01:55:49 -0700190 int tmpio;
Jens Axboe22e2c502005-06-27 10:55:12 +0200191
Oleg Nesterovcf342e52006-08-29 09:17:41 +0200192 read_lock(&tasklist_lock);
Jens Axboe22e2c502005-06-27 10:55:12 +0200193 switch (which) {
194 case IOPRIO_WHO_PROCESS:
195 if (!who)
196 p = current;
197 else
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -0700198 p = find_task_by_vpid(who);
Jens Axboe22e2c502005-06-27 10:55:12 +0200199 if (p)
David Quigleya1836a42006-06-30 01:55:49 -0700200 ret = get_task_ioprio(p);
Jens Axboe22e2c502005-06-27 10:55:12 +0200201 break;
202 case IOPRIO_WHO_PGRP:
203 if (!who)
Eric W. Biederman41487c62007-02-12 00:53:01 -0800204 pgrp = task_pgrp(current);
205 else
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700206 pgrp = find_vpid(who);
Ken Chen2d70b682008-08-20 14:09:17 -0700207 do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
David Quigleya1836a42006-06-30 01:55:49 -0700208 tmpio = get_task_ioprio(p);
209 if (tmpio < 0)
210 continue;
Jens Axboe22e2c502005-06-27 10:55:12 +0200211 if (ret == -ESRCH)
David Quigleya1836a42006-06-30 01:55:49 -0700212 ret = tmpio;
Jens Axboe22e2c502005-06-27 10:55:12 +0200213 else
David Quigleya1836a42006-06-30 01:55:49 -0700214 ret = ioprio_best(ret, tmpio);
Ken Chen2d70b682008-08-20 14:09:17 -0700215 } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
Jens Axboe22e2c502005-06-27 10:55:12 +0200216 break;
217 case IOPRIO_WHO_USER:
218 if (!who)
David Howells86a264a2008-11-14 10:39:18 +1100219 user = current_user();
Jens Axboe22e2c502005-06-27 10:55:12 +0200220 else
221 user = find_user(who);
222
223 if (!user)
224 break;
225
226 do_each_thread(g, p) {
David Howellsb6dff3e2008-11-14 10:39:16 +1100227 if (p->cred->uid != user->uid)
Jens Axboe22e2c502005-06-27 10:55:12 +0200228 continue;
David Quigleya1836a42006-06-30 01:55:49 -0700229 tmpio = get_task_ioprio(p);
230 if (tmpio < 0)
231 continue;
Jens Axboe22e2c502005-06-27 10:55:12 +0200232 if (ret == -ESRCH)
David Quigleya1836a42006-06-30 01:55:49 -0700233 ret = tmpio;
Jens Axboe22e2c502005-06-27 10:55:12 +0200234 else
David Quigleya1836a42006-06-30 01:55:49 -0700235 ret = ioprio_best(ret, tmpio);
Jens Axboe22e2c502005-06-27 10:55:12 +0200236 } while_each_thread(g, p);
237
238 if (who)
239 free_uid(user);
240 break;
241 default:
242 ret = -EINVAL;
243 }
244
Oleg Nesterovcf342e52006-08-29 09:17:41 +0200245 read_unlock(&tasklist_lock);
Jens Axboe22e2c502005-06-27 10:55:12 +0200246 return ret;
247}
248