1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 2004 Jens Axboe <axboe@kernel.dk>
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_BE_NR || data < 0)
88 case IOPRIO_CLASS_IDLE:
90 case IOPRIO_CLASS_NONE:
101 SYSCALL_DEFINE3(ioprio_set, int, which, int, who, int, ioprio)
103 struct task_struct *p, *g;
104 struct user_struct *user;
109 ret = ioprio_check_cap(ioprio);
116 case IOPRIO_WHO_PROCESS:
120 p = find_task_by_vpid(who);
122 ret = set_task_ioprio(p, ioprio);
124 case IOPRIO_WHO_PGRP:
126 pgrp = task_pgrp(current);
128 pgrp = find_vpid(who);
129 do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
130 ret = set_task_ioprio(p, ioprio);
133 } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
135 case IOPRIO_WHO_USER:
136 uid = make_kuid(current_user_ns(), who);
140 user = current_user();
142 user = find_user(uid);
147 for_each_process_thread(g, p) {
148 if (!uid_eq(task_uid(p), uid) ||
151 ret = set_task_ioprio(p, ioprio);
167 static int get_task_ioprio(struct task_struct *p)
171 ret = security_task_getioprio(p);
174 ret = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_NONE, IOPRIO_NORM);
177 ret = p->io_context->ioprio;
183 int ioprio_best(unsigned short aprio, unsigned short bprio)
185 if (!ioprio_valid(aprio))
186 aprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, IOPRIO_NORM);
187 if (!ioprio_valid(bprio))
188 bprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, IOPRIO_NORM);
190 return min(aprio, bprio);
193 SYSCALL_DEFINE2(ioprio_get, int, which, int, who)
195 struct task_struct *g, *p;
196 struct user_struct *user;
204 case IOPRIO_WHO_PROCESS:
208 p = find_task_by_vpid(who);
210 ret = get_task_ioprio(p);
212 case IOPRIO_WHO_PGRP:
214 pgrp = task_pgrp(current);
216 pgrp = find_vpid(who);
217 read_lock(&tasklist_lock);
218 do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
219 tmpio = get_task_ioprio(p);
225 ret = ioprio_best(ret, tmpio);
226 } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
227 read_unlock(&tasklist_lock);
230 case IOPRIO_WHO_USER:
231 uid = make_kuid(current_user_ns(), who);
233 user = current_user();
235 user = find_user(uid);
240 for_each_process_thread(g, p) {
241 if (!uid_eq(task_uid(p), user->uid) ||
244 tmpio = get_task_ioprio(p);
250 ret = ioprio_best(ret, tmpio);