Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1 | /* |
| 2 | * fs/ioprio.c |
| 3 | * |
Jens Axboe | 0fe2347 | 2006-09-04 15:41:16 +0200 | [diff] [blame] | 4 | * Copyright (C) 2004 Jens Axboe <axboe@kernel.dk> |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 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 | */ |
| 22 | #include <linux/kernel.h> |
| 23 | #include <linux/ioprio.h> |
| 24 | #include <linux/blkdev.h> |
Randy Dunlap | 16f7e0f | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 25 | #include <linux/capability.h> |
Adrian Bunk | 9abdc4c | 2005-11-08 16:57:02 +0100 | [diff] [blame] | 26 | #include <linux/syscalls.h> |
James Morris | 03e6806 | 2006-06-23 02:03:58 -0700 | [diff] [blame] | 27 | #include <linux/security.h> |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 28 | #include <linux/pid_namespace.h> |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 29 | |
Theodore Ts'o | b3881f7 | 2009-01-05 22:46:26 -0500 | [diff] [blame] | 30 | int set_task_ioprio(struct task_struct *task, int ioprio) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 31 | { |
James Morris | 03e6806 | 2006-06-23 02:03:58 -0700 | [diff] [blame] | 32 | int err; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 33 | struct io_context *ioc; |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 34 | const struct cred *cred = current_cred(), *tcred; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 35 | |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 36 | 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 Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 41 | return -EPERM; |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 42 | } |
| 43 | rcu_read_unlock(); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 44 | |
James Morris | 03e6806 | 2006-06-23 02:03:58 -0700 | [diff] [blame] | 45 | err = security_task_setioprio(task, ioprio); |
| 46 | if (err) |
| 47 | return err; |
| 48 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 49 | task_lock(task); |
Jens Axboe | fd0928d | 2008-01-24 08:52:45 +0100 | [diff] [blame] | 50 | do { |
| 51 | ioc = task->io_context; |
| 52 | /* see wmb() in current_io_context() */ |
| 53 | smp_read_barrier_depends(); |
| 54 | if (ioc) |
| 55 | break; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 56 | |
Jens Axboe | fd0928d | 2008-01-24 08:52:45 +0100 | [diff] [blame] | 57 | ioc = alloc_io_context(GFP_ATOMIC, -1); |
| 58 | if (!ioc) { |
| 59 | err = -ENOMEM; |
| 60 | break; |
| 61 | } |
| 62 | task->io_context = ioc; |
Jens Axboe | fd0928d | 2008-01-24 08:52:45 +0100 | [diff] [blame] | 63 | } while (1); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 64 | |
Jens Axboe | fd0928d | 2008-01-24 08:52:45 +0100 | [diff] [blame] | 65 | if (!err) { |
| 66 | ioc->ioprio = ioprio; |
Jens Axboe | fc46379 | 2006-08-29 09:05:44 +0200 | [diff] [blame] | 67 | ioc->ioprio_changed = 1; |
Jens Axboe | fd0928d | 2008-01-24 08:52:45 +0100 | [diff] [blame] | 68 | } |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 69 | |
| 70 | task_unlock(task); |
Jens Axboe | fd0928d | 2008-01-24 08:52:45 +0100 | [diff] [blame] | 71 | return err; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 72 | } |
Theodore Ts'o | b3881f7 | 2009-01-05 22:46:26 -0500 | [diff] [blame] | 73 | EXPORT_SYMBOL_GPL(set_task_ioprio); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 74 | |
Anton Blanchard | cf36680 | 2005-07-07 17:56:13 -0700 | [diff] [blame] | 75 | asmlinkage long sys_ioprio_set(int which, int who, int ioprio) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 76 | { |
| 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. Biederman | 41487c6 | 2007-02-12 00:53:01 -0800 | [diff] [blame] | 81 | struct pid *pgrp; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 82 | 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 Axboe | 8ec680e | 2007-11-07 13:54:07 +0100 | [diff] [blame] | 96 | case IOPRIO_CLASS_NONE: |
| 97 | if (data) |
| 98 | return -EINVAL; |
| 99 | break; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 100 | default: |
| 101 | return -EINVAL; |
| 102 | } |
| 103 | |
| 104 | ret = -ESRCH; |
Oleg Nesterov | cf342e5 | 2006-08-29 09:17:41 +0200 | [diff] [blame] | 105 | /* |
| 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 Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 111 | switch (which) { |
| 112 | case IOPRIO_WHO_PROCESS: |
| 113 | if (!who) |
| 114 | p = current; |
| 115 | else |
Pavel Emelyanov | 228ebcb | 2007-10-18 23:40:16 -0700 | [diff] [blame] | 116 | p = find_task_by_vpid(who); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 117 | if (p) |
| 118 | ret = set_task_ioprio(p, ioprio); |
| 119 | break; |
| 120 | case IOPRIO_WHO_PGRP: |
| 121 | if (!who) |
Eric W. Biederman | 41487c6 | 2007-02-12 00:53:01 -0800 | [diff] [blame] | 122 | pgrp = task_pgrp(current); |
| 123 | else |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 124 | pgrp = find_vpid(who); |
Ken Chen | 2d70b68 | 2008-08-20 14:09:17 -0700 | [diff] [blame] | 125 | do_each_pid_thread(pgrp, PIDTYPE_PGID, p) { |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 126 | ret = set_task_ioprio(p, ioprio); |
| 127 | if (ret) |
| 128 | break; |
Ken Chen | 2d70b68 | 2008-08-20 14:09:17 -0700 | [diff] [blame] | 129 | } while_each_pid_thread(pgrp, PIDTYPE_PGID, p); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 130 | break; |
| 131 | case IOPRIO_WHO_USER: |
| 132 | if (!who) |
David Howells | 86a264a | 2008-11-14 10:39:18 +1100 | [diff] [blame] | 133 | user = current_user(); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 134 | else |
| 135 | user = find_user(who); |
| 136 | |
| 137 | if (!user) |
| 138 | break; |
| 139 | |
| 140 | do_each_thread(g, p) { |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 141 | if (__task_cred(p)->uid != who) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 142 | continue; |
| 143 | ret = set_task_ioprio(p, ioprio); |
| 144 | if (ret) |
Oleg Nesterov | 78bd4d4 | 2006-08-21 08:33:23 +0200 | [diff] [blame] | 145 | goto free_uid; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 146 | } while_each_thread(g, p); |
Oleg Nesterov | 78bd4d4 | 2006-08-21 08:33:23 +0200 | [diff] [blame] | 147 | free_uid: |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 148 | if (who) |
| 149 | free_uid(user); |
| 150 | break; |
| 151 | default: |
| 152 | ret = -EINVAL; |
| 153 | } |
| 154 | |
Oleg Nesterov | cf342e5 | 2006-08-29 09:17:41 +0200 | [diff] [blame] | 155 | read_unlock(&tasklist_lock); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 156 | return ret; |
| 157 | } |
| 158 | |
David Quigley | a1836a4 | 2006-06-30 01:55:49 -0700 | [diff] [blame] | 159 | static 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 Axboe | fd0928d | 2008-01-24 08:52:45 +0100 | [diff] [blame] | 166 | ret = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_NONE, IOPRIO_NORM); |
| 167 | if (p->io_context) |
| 168 | ret = p->io_context->ioprio; |
David Quigley | a1836a4 | 2006-06-30 01:55:49 -0700 | [diff] [blame] | 169 | out: |
| 170 | return ret; |
| 171 | } |
| 172 | |
Oleg Nesterov | e014ff8 | 2006-08-21 10:02:50 +0200 | [diff] [blame] | 173 | int 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 Nesterov | e014ff8 | 2006-08-21 10:02:50 +0200 | [diff] [blame] | 178 | 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 Blanchard | cf36680 | 2005-07-07 17:56:13 -0700 | [diff] [blame] | 191 | asmlinkage long sys_ioprio_get(int which, int who) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 192 | { |
| 193 | struct task_struct *g, *p; |
| 194 | struct user_struct *user; |
Eric W. Biederman | 41487c6 | 2007-02-12 00:53:01 -0800 | [diff] [blame] | 195 | struct pid *pgrp; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 196 | int ret = -ESRCH; |
David Quigley | a1836a4 | 2006-06-30 01:55:49 -0700 | [diff] [blame] | 197 | int tmpio; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 198 | |
Oleg Nesterov | cf342e5 | 2006-08-29 09:17:41 +0200 | [diff] [blame] | 199 | read_lock(&tasklist_lock); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 200 | switch (which) { |
| 201 | case IOPRIO_WHO_PROCESS: |
| 202 | if (!who) |
| 203 | p = current; |
| 204 | else |
Pavel Emelyanov | 228ebcb | 2007-10-18 23:40:16 -0700 | [diff] [blame] | 205 | p = find_task_by_vpid(who); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 206 | if (p) |
David Quigley | a1836a4 | 2006-06-30 01:55:49 -0700 | [diff] [blame] | 207 | ret = get_task_ioprio(p); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 208 | break; |
| 209 | case IOPRIO_WHO_PGRP: |
| 210 | if (!who) |
Eric W. Biederman | 41487c6 | 2007-02-12 00:53:01 -0800 | [diff] [blame] | 211 | pgrp = task_pgrp(current); |
| 212 | else |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 213 | pgrp = find_vpid(who); |
Ken Chen | 2d70b68 | 2008-08-20 14:09:17 -0700 | [diff] [blame] | 214 | do_each_pid_thread(pgrp, PIDTYPE_PGID, p) { |
David Quigley | a1836a4 | 2006-06-30 01:55:49 -0700 | [diff] [blame] | 215 | tmpio = get_task_ioprio(p); |
| 216 | if (tmpio < 0) |
| 217 | continue; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 218 | if (ret == -ESRCH) |
David Quigley | a1836a4 | 2006-06-30 01:55:49 -0700 | [diff] [blame] | 219 | ret = tmpio; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 220 | else |
David Quigley | a1836a4 | 2006-06-30 01:55:49 -0700 | [diff] [blame] | 221 | ret = ioprio_best(ret, tmpio); |
Ken Chen | 2d70b68 | 2008-08-20 14:09:17 -0700 | [diff] [blame] | 222 | } while_each_pid_thread(pgrp, PIDTYPE_PGID, p); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 223 | break; |
| 224 | case IOPRIO_WHO_USER: |
| 225 | if (!who) |
David Howells | 86a264a | 2008-11-14 10:39:18 +1100 | [diff] [blame] | 226 | user = current_user(); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 227 | else |
| 228 | user = find_user(who); |
| 229 | |
| 230 | if (!user) |
| 231 | break; |
| 232 | |
| 233 | do_each_thread(g, p) { |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 234 | if (__task_cred(p)->uid != user->uid) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 235 | continue; |
David Quigley | a1836a4 | 2006-06-30 01:55:49 -0700 | [diff] [blame] | 236 | tmpio = get_task_ioprio(p); |
| 237 | if (tmpio < 0) |
| 238 | continue; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 239 | if (ret == -ESRCH) |
David Quigley | a1836a4 | 2006-06-30 01:55:49 -0700 | [diff] [blame] | 240 | ret = tmpio; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 241 | else |
David Quigley | a1836a4 | 2006-06-30 01:55:49 -0700 | [diff] [blame] | 242 | ret = ioprio_best(ret, tmpio); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 243 | } while_each_thread(g, p); |
| 244 | |
| 245 | if (who) |
| 246 | free_uid(user); |
| 247 | break; |
| 248 | default: |
| 249 | ret = -EINVAL; |
| 250 | } |
| 251 | |
Oleg Nesterov | cf342e5 | 2006-08-29 09:17:41 +0200 | [diff] [blame] | 252 | read_unlock(&tasklist_lock); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 253 | return ret; |
| 254 | } |
| 255 | |