1 // SPDX-License-Identifier: GPL-2.0
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
14 * IOW, setting BE scheduling class with prio 2 is done ala:
16 * unsigned int prio = (IOPRIO_CLASS_BE << IOPRIO_CLASS_SHIFT) | 2;
18 * ioprio_set(PRIO_PROCESS, pid, prio);
20 * See also Documentation/block/ioprio.rst
23 #include <linux/gfp.h>
24 #include <linux/kernel.h>
25 #include <linux/export.h>
26 #include <linux/ioprio.h>
27 #include <linux/cred.h>
28 #include <linux/blkdev.h>
29 #include <linux/capability.h>
30 #include <linux/sched/user.h>
31 #include <linux/sched/task.h>
32 #include <linux/syscalls.h>
33 #include <linux/security.h>
34 #include <linux/pid_namespace.h>
36 int set_task_ioprio(struct task_struct *task, int ioprio)
39 struct io_context *ioc;
40 const struct cred *cred = current_cred(), *tcred;
43 tcred = __task_cred(task);
44 if (!uid_eq(tcred->uid, cred->euid) &&
45 !uid_eq(tcred->uid, cred->uid) && !capable(CAP_SYS_NICE)) {
51 err = security_task_setioprio(task, ioprio);
55 ioc = get_task_io_context(task, GFP_ATOMIC, NUMA_NO_NODE);
63 EXPORT_SYMBOL_GPL(set_task_ioprio);
65 int ioprio_check_cap(int ioprio)
67 int class = IOPRIO_PRIO_CLASS(ioprio);
68 int data = IOPRIO_PRIO_DATA(ioprio);
73 * Originally this only checked for CAP_SYS_ADMIN,
74 * which was implicitly allowed for pid 0 by security
75 * modules such as SELinux. Make sure we check
76 * CAP_SYS_ADMIN first to avoid a denial/avc for
77 * possibly missing CAP_SYS_NICE permission.
79 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_NICE))
82 /* rt has prio field too */
84 if (data >= IOPRIO_NR_LEVELS || data < 0)
87 case IOPRIO_CLASS_IDLE:
89 case IOPRIO_CLASS_NONE:
100 SYSCALL_DEFINE3(ioprio_set, int, which, int, who, int, ioprio)
102 struct task_struct *p, *g;
103 struct user_struct *user;
108 ret = ioprio_check_cap(ioprio);
115 case IOPRIO_WHO_PROCESS:
119 p = find_task_by_vpid(who);
121 ret = set_task_ioprio(p, ioprio);
123 case IOPRIO_WHO_PGRP:
125 pgrp = task_pgrp(current);
127 pgrp = find_vpid(who);
129 read_lock(&tasklist_lock);
130 do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
131 ret = set_task_ioprio(p, ioprio);
133 read_unlock(&tasklist_lock);
136 } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
137 read_unlock(&tasklist_lock);
140 case IOPRIO_WHO_USER:
141 uid = make_kuid(current_user_ns(), who);
145 user = current_user();
147 user = find_user(uid);
152 for_each_process_thread(g, p) {
153 if (!uid_eq(task_uid(p), uid) ||
156 ret = set_task_ioprio(p, ioprio);
173 static int get_task_ioprio(struct task_struct *p)
177 ret = security_task_getioprio(p);
180 ret = IOPRIO_DEFAULT;
183 ret = p->io_context->ioprio;
189 int ioprio_best(unsigned short aprio, unsigned short bprio)
191 if (!ioprio_valid(aprio))
192 aprio = IOPRIO_DEFAULT;
193 if (!ioprio_valid(bprio))
194 bprio = IOPRIO_DEFAULT;
196 return min(aprio, bprio);
199 SYSCALL_DEFINE2(ioprio_get, int, which, int, who)
201 struct task_struct *g, *p;
202 struct user_struct *user;
210 case IOPRIO_WHO_PROCESS:
214 p = find_task_by_vpid(who);
216 ret = get_task_ioprio(p);
218 case IOPRIO_WHO_PGRP:
220 pgrp = task_pgrp(current);
222 pgrp = find_vpid(who);
223 do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
224 tmpio = get_task_ioprio(p);
230 ret = ioprio_best(ret, tmpio);
231 } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
233 case IOPRIO_WHO_USER:
234 uid = make_kuid(current_user_ns(), who);
236 user = current_user();
238 user = find_user(uid);
243 for_each_process_thread(g, p) {
244 if (!uid_eq(task_uid(p), user->uid) ||
247 tmpio = get_task_ioprio(p);
253 ret = ioprio_best(ret, tmpio);