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 | |
| 30 | static int set_task_ioprio(struct task_struct *task, int ioprio) |
| 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 | } |
| 73 | |
Anton Blanchard | cf36680 | 2005-07-07 17:56:13 -0700 | [diff] [blame] | 74 | asmlinkage long sys_ioprio_set(int which, int who, int ioprio) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 75 | { |
| 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. Biederman | 41487c6 | 2007-02-12 00:53:01 -0800 | [diff] [blame] | 80 | struct pid *pgrp; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 81 | 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 Axboe | 8ec680e | 2007-11-07 13:54:07 +0100 | [diff] [blame] | 95 | case IOPRIO_CLASS_NONE: |
| 96 | if (data) |
| 97 | return -EINVAL; |
| 98 | break; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 99 | default: |
| 100 | return -EINVAL; |
| 101 | } |
| 102 | |
| 103 | ret = -ESRCH; |
Oleg Nesterov | cf342e5 | 2006-08-29 09:17:41 +0200 | [diff] [blame] | 104 | /* |
| 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 Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 110 | switch (which) { |
| 111 | case IOPRIO_WHO_PROCESS: |
| 112 | if (!who) |
| 113 | p = current; |
| 114 | else |
Pavel Emelyanov | 228ebcb | 2007-10-18 23:40:16 -0700 | [diff] [blame] | 115 | p = find_task_by_vpid(who); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 116 | if (p) |
| 117 | ret = set_task_ioprio(p, ioprio); |
| 118 | break; |
| 119 | case IOPRIO_WHO_PGRP: |
| 120 | if (!who) |
Eric W. Biederman | 41487c6 | 2007-02-12 00:53:01 -0800 | [diff] [blame] | 121 | pgrp = task_pgrp(current); |
| 122 | else |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 123 | pgrp = find_vpid(who); |
Ken Chen | 2d70b68 | 2008-08-20 14:09:17 -0700 | [diff] [blame] | 124 | do_each_pid_thread(pgrp, PIDTYPE_PGID, p) { |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 125 | ret = set_task_ioprio(p, ioprio); |
| 126 | if (ret) |
| 127 | break; |
Ken Chen | 2d70b68 | 2008-08-20 14:09:17 -0700 | [diff] [blame] | 128 | } while_each_pid_thread(pgrp, PIDTYPE_PGID, p); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 129 | break; |
| 130 | case IOPRIO_WHO_USER: |
| 131 | if (!who) |
David Howells | 86a264a | 2008-11-14 10:39:18 +1100 | [diff] [blame] | 132 | user = current_user(); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 133 | else |
| 134 | user = find_user(who); |
| 135 | |
| 136 | if (!user) |
| 137 | break; |
| 138 | |
| 139 | do_each_thread(g, p) { |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 140 | if (__task_cred(p)->uid != who) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 141 | continue; |
| 142 | ret = set_task_ioprio(p, ioprio); |
| 143 | if (ret) |
Oleg Nesterov | 78bd4d4 | 2006-08-21 08:33:23 +0200 | [diff] [blame] | 144 | goto free_uid; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 145 | } while_each_thread(g, p); |
Oleg Nesterov | 78bd4d4 | 2006-08-21 08:33:23 +0200 | [diff] [blame] | 146 | free_uid: |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 147 | if (who) |
| 148 | free_uid(user); |
| 149 | break; |
| 150 | default: |
| 151 | ret = -EINVAL; |
| 152 | } |
| 153 | |
Oleg Nesterov | cf342e5 | 2006-08-29 09:17:41 +0200 | [diff] [blame] | 154 | read_unlock(&tasklist_lock); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 155 | return ret; |
| 156 | } |
| 157 | |
David Quigley | a1836a4 | 2006-06-30 01:55:49 -0700 | [diff] [blame] | 158 | static 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 Axboe | fd0928d | 2008-01-24 08:52:45 +0100 | [diff] [blame] | 165 | ret = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_NONE, IOPRIO_NORM); |
| 166 | if (p->io_context) |
| 167 | ret = p->io_context->ioprio; |
David Quigley | a1836a4 | 2006-06-30 01:55:49 -0700 | [diff] [blame] | 168 | out: |
| 169 | return ret; |
| 170 | } |
| 171 | |
Oleg Nesterov | e014ff8 | 2006-08-21 10:02:50 +0200 | [diff] [blame] | 172 | int 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 Nesterov | e014ff8 | 2006-08-21 10:02:50 +0200 | [diff] [blame] | 177 | 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 Blanchard | cf36680 | 2005-07-07 17:56:13 -0700 | [diff] [blame] | 190 | asmlinkage long sys_ioprio_get(int which, int who) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 191 | { |
| 192 | struct task_struct *g, *p; |
| 193 | struct user_struct *user; |
Eric W. Biederman | 41487c6 | 2007-02-12 00:53:01 -0800 | [diff] [blame] | 194 | struct pid *pgrp; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 195 | int ret = -ESRCH; |
David Quigley | a1836a4 | 2006-06-30 01:55:49 -0700 | [diff] [blame] | 196 | int tmpio; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 197 | |
Oleg Nesterov | cf342e5 | 2006-08-29 09:17:41 +0200 | [diff] [blame] | 198 | read_lock(&tasklist_lock); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 199 | switch (which) { |
| 200 | case IOPRIO_WHO_PROCESS: |
| 201 | if (!who) |
| 202 | p = current; |
| 203 | else |
Pavel Emelyanov | 228ebcb | 2007-10-18 23:40:16 -0700 | [diff] [blame] | 204 | p = find_task_by_vpid(who); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 205 | if (p) |
David Quigley | a1836a4 | 2006-06-30 01:55:49 -0700 | [diff] [blame] | 206 | ret = get_task_ioprio(p); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 207 | break; |
| 208 | case IOPRIO_WHO_PGRP: |
| 209 | if (!who) |
Eric W. Biederman | 41487c6 | 2007-02-12 00:53:01 -0800 | [diff] [blame] | 210 | pgrp = task_pgrp(current); |
| 211 | else |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 212 | pgrp = find_vpid(who); |
Ken Chen | 2d70b68 | 2008-08-20 14:09:17 -0700 | [diff] [blame] | 213 | do_each_pid_thread(pgrp, PIDTYPE_PGID, p) { |
David Quigley | a1836a4 | 2006-06-30 01:55:49 -0700 | [diff] [blame] | 214 | tmpio = get_task_ioprio(p); |
| 215 | if (tmpio < 0) |
| 216 | continue; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 217 | if (ret == -ESRCH) |
David Quigley | a1836a4 | 2006-06-30 01:55:49 -0700 | [diff] [blame] | 218 | ret = tmpio; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 219 | else |
David Quigley | a1836a4 | 2006-06-30 01:55:49 -0700 | [diff] [blame] | 220 | ret = ioprio_best(ret, tmpio); |
Ken Chen | 2d70b68 | 2008-08-20 14:09:17 -0700 | [diff] [blame] | 221 | } while_each_pid_thread(pgrp, PIDTYPE_PGID, p); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 222 | break; |
| 223 | case IOPRIO_WHO_USER: |
| 224 | if (!who) |
David Howells | 86a264a | 2008-11-14 10:39:18 +1100 | [diff] [blame] | 225 | user = current_user(); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 226 | else |
| 227 | user = find_user(who); |
| 228 | |
| 229 | if (!user) |
| 230 | break; |
| 231 | |
| 232 | do_each_thread(g, p) { |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 233 | if (__task_cred(p)->uid != user->uid) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 234 | continue; |
David Quigley | a1836a4 | 2006-06-30 01:55:49 -0700 | [diff] [blame] | 235 | tmpio = get_task_ioprio(p); |
| 236 | if (tmpio < 0) |
| 237 | continue; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 238 | if (ret == -ESRCH) |
David Quigley | a1836a4 | 2006-06-30 01:55:49 -0700 | [diff] [blame] | 239 | ret = tmpio; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 240 | else |
David Quigley | a1836a4 | 2006-06-30 01:55:49 -0700 | [diff] [blame] | 241 | ret = ioprio_best(ret, tmpio); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 242 | } while_each_thread(g, p); |
| 243 | |
| 244 | if (who) |
| 245 | free_uid(user); |
| 246 | break; |
| 247 | default: |
| 248 | ret = -EINVAL; |
| 249 | } |
| 250 | |
Oleg Nesterov | cf342e5 | 2006-08-29 09:17:41 +0200 | [diff] [blame] | 251 | read_unlock(&tasklist_lock); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 252 | return ret; |
| 253 | } |
| 254 | |