1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * fs/ioprio.c 4 * 5 * Copyright (C) 2004 Jens Axboe <axboe@kernel.dk> 6 * 7 * Helper functions for setting/querying io priorities of processes. The 8 * system calls closely mimmick getpriority/setpriority, see the man page for 9 * those. The prio argument is a composite of prio class and prio data, where 10 * the data argument has meaning within that class. The standard scheduling 11 * classes have 8 distinct prio levels, with 0 being the highest prio and 7 12 * being the lowest. 13 * 14 * IOW, setting BE scheduling class with prio 2 is done ala: 15 * 16 * unsigned int prio = (IOPRIO_CLASS_BE << IOPRIO_CLASS_SHIFT) | 2; 17 * 18 * ioprio_set(PRIO_PROCESS, pid, prio); 19 * 20 * See also Documentation/block/ioprio.rst 21 * 22 */ 23 #include <linux/gfp.h> 24 #include <linux/kernel.h> 25 #include <linux/ioprio.h> 26 #include <linux/cred.h> 27 #include <linux/blkdev.h> 28 #include <linux/capability.h> 29 #include <linux/syscalls.h> 30 #include <linux/security.h> 31 #include <linux/pid_namespace.h> 32 33 int ioprio_check_cap(int ioprio) 34 { 35 int class = IOPRIO_PRIO_CLASS(ioprio); 36 int level = IOPRIO_PRIO_LEVEL(ioprio); 37 38 switch (class) { 39 case IOPRIO_CLASS_RT: 40 /* 41 * Originally this only checked for CAP_SYS_ADMIN, 42 * which was implicitly allowed for pid 0 by security 43 * modules such as SELinux. Make sure we check 44 * CAP_SYS_ADMIN first to avoid a denial/avc for 45 * possibly missing CAP_SYS_NICE permission. 46 */ 47 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_NICE)) 48 return -EPERM; 49 break; 50 case IOPRIO_CLASS_BE: 51 case IOPRIO_CLASS_IDLE: 52 break; 53 case IOPRIO_CLASS_NONE: 54 if (level) 55 return -EINVAL; 56 break; 57 case IOPRIO_CLASS_INVALID: 58 default: 59 return -EINVAL; 60 } 61 62 return 0; 63 } 64 65 SYSCALL_DEFINE3(ioprio_set, int, which, int, who, int, ioprio) 66 { 67 struct task_struct *p, *g; 68 struct user_struct *user; 69 struct pid *pgrp; 70 kuid_t uid; 71 int ret; 72 73 ret = ioprio_check_cap(ioprio); 74 if (ret) 75 return ret; 76 77 ret = -ESRCH; 78 rcu_read_lock(); 79 switch (which) { 80 case IOPRIO_WHO_PROCESS: 81 if (!who) 82 p = current; 83 else 84 p = find_task_by_vpid(who); 85 if (p) 86 ret = set_task_ioprio(p, ioprio); 87 break; 88 case IOPRIO_WHO_PGRP: 89 if (!who) 90 pgrp = task_pgrp(current); 91 else 92 pgrp = find_vpid(who); 93 94 read_lock(&tasklist_lock); 95 do_each_pid_thread(pgrp, PIDTYPE_PGID, p) { 96 ret = set_task_ioprio(p, ioprio); 97 if (ret) { 98 read_unlock(&tasklist_lock); 99 goto out; 100 } 101 } while_each_pid_thread(pgrp, PIDTYPE_PGID, p); 102 read_unlock(&tasklist_lock); 103 104 break; 105 case IOPRIO_WHO_USER: 106 uid = make_kuid(current_user_ns(), who); 107 if (!uid_valid(uid)) 108 break; 109 if (!who) 110 user = current_user(); 111 else 112 user = find_user(uid); 113 114 if (!user) 115 break; 116 117 for_each_process_thread(g, p) { 118 if (!uid_eq(task_uid(p), uid) || 119 !task_pid_vnr(p)) 120 continue; 121 ret = set_task_ioprio(p, ioprio); 122 if (ret) 123 goto free_uid; 124 } 125 free_uid: 126 if (who) 127 free_uid(user); 128 break; 129 default: 130 ret = -EINVAL; 131 } 132 133 out: 134 rcu_read_unlock(); 135 return ret; 136 } 137 138 static int get_task_ioprio(struct task_struct *p) 139 { 140 int ret; 141 142 ret = security_task_getioprio(p); 143 if (ret) 144 goto out; 145 task_lock(p); 146 ret = __get_task_ioprio(p); 147 task_unlock(p); 148 out: 149 return ret; 150 } 151 152 /* 153 * Return raw IO priority value as set by userspace. We use this for 154 * ioprio_get(pid, IOPRIO_WHO_PROCESS) so that we keep historical behavior and 155 * also so that userspace can distinguish unset IO priority (which just gets 156 * overriden based on task's nice value) from IO priority set to some value. 157 */ 158 static int get_task_raw_ioprio(struct task_struct *p) 159 { 160 int ret; 161 162 ret = security_task_getioprio(p); 163 if (ret) 164 goto out; 165 task_lock(p); 166 if (p->io_context) 167 ret = p->io_context->ioprio; 168 else 169 ret = IOPRIO_DEFAULT; 170 task_unlock(p); 171 out: 172 return ret; 173 } 174 175 static int ioprio_best(unsigned short aprio, unsigned short bprio) 176 { 177 return min(aprio, bprio); 178 } 179 180 SYSCALL_DEFINE2(ioprio_get, int, which, int, who) 181 { 182 struct task_struct *g, *p; 183 struct user_struct *user; 184 struct pid *pgrp; 185 kuid_t uid; 186 int ret = -ESRCH; 187 int tmpio; 188 189 rcu_read_lock(); 190 switch (which) { 191 case IOPRIO_WHO_PROCESS: 192 if (!who) 193 p = current; 194 else 195 p = find_task_by_vpid(who); 196 if (p) 197 ret = get_task_raw_ioprio(p); 198 break; 199 case IOPRIO_WHO_PGRP: 200 if (!who) 201 pgrp = task_pgrp(current); 202 else 203 pgrp = find_vpid(who); 204 read_lock(&tasklist_lock); 205 do_each_pid_thread(pgrp, PIDTYPE_PGID, p) { 206 tmpio = get_task_ioprio(p); 207 if (tmpio < 0) 208 continue; 209 if (ret == -ESRCH) 210 ret = tmpio; 211 else 212 ret = ioprio_best(ret, tmpio); 213 } while_each_pid_thread(pgrp, PIDTYPE_PGID, p); 214 read_unlock(&tasklist_lock); 215 216 break; 217 case IOPRIO_WHO_USER: 218 uid = make_kuid(current_user_ns(), who); 219 if (!who) 220 user = current_user(); 221 else 222 user = find_user(uid); 223 224 if (!user) 225 break; 226 227 for_each_process_thread(g, p) { 228 if (!uid_eq(task_uid(p), user->uid) || 229 !task_pid_vnr(p)) 230 continue; 231 tmpio = get_task_ioprio(p); 232 if (tmpio < 0) 233 continue; 234 if (ret == -ESRCH) 235 ret = tmpio; 236 else 237 ret = ioprio_best(ret, tmpio); 238 } 239 240 if (who) 241 free_uid(user); 242 break; 243 default: 244 ret = -EINVAL; 245 } 246 247 rcu_read_unlock(); 248 return ret; 249 } 250