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);
72 if (!capable(CAP_SYS_NICE) && !capable(CAP_SYS_ADMIN))
75 /* rt has prio field too */
77 if (data >= IOPRIO_NR_LEVELS || data < 0)
80 case IOPRIO_CLASS_IDLE:
82 case IOPRIO_CLASS_NONE:
93 SYSCALL_DEFINE3(ioprio_set, int, which, int, who, int, ioprio)
95 struct task_struct *p, *g;
96 struct user_struct *user;
101 ret = ioprio_check_cap(ioprio);
108 case IOPRIO_WHO_PROCESS:
112 p = find_task_by_vpid(who);
114 ret = set_task_ioprio(p, ioprio);
116 case IOPRIO_WHO_PGRP:
118 pgrp = task_pgrp(current);
120 pgrp = find_vpid(who);
122 read_lock(&tasklist_lock);
123 do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
124 ret = set_task_ioprio(p, ioprio);
126 read_unlock(&tasklist_lock);
129 } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
130 read_unlock(&tasklist_lock);
133 case IOPRIO_WHO_USER:
134 uid = make_kuid(current_user_ns(), who);
138 user = current_user();
140 user = find_user(uid);
145 for_each_process_thread(g, p) {
146 if (!uid_eq(task_uid(p), uid) ||
149 ret = set_task_ioprio(p, ioprio);
166 static int get_task_ioprio(struct task_struct *p)
170 ret = security_task_getioprio(p);
173 ret = IOPRIO_DEFAULT;
176 ret = p->io_context->ioprio;
182 int ioprio_best(unsigned short aprio, unsigned short bprio)
184 if (!ioprio_valid(aprio))
185 aprio = IOPRIO_DEFAULT;
186 if (!ioprio_valid(bprio))
187 bprio = IOPRIO_DEFAULT;
189 return min(aprio, bprio);
192 SYSCALL_DEFINE2(ioprio_get, int, which, int, who)
194 struct task_struct *g, *p;
195 struct user_struct *user;
203 case IOPRIO_WHO_PROCESS:
207 p = find_task_by_vpid(who);
209 ret = get_task_ioprio(p);
211 case IOPRIO_WHO_PGRP:
213 pgrp = task_pgrp(current);
215 pgrp = find_vpid(who);
216 do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
217 tmpio = get_task_ioprio(p);
223 ret = ioprio_best(ret, tmpio);
224 } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
226 case IOPRIO_WHO_USER:
227 uid = make_kuid(current_user_ns(), who);
229 user = current_user();
231 user = find_user(uid);
236 for_each_process_thread(g, p) {
237 if (!uid_eq(task_uid(p), user->uid) ||
240 tmpio = get_task_ioprio(p);
246 ret = ioprio_best(ret, tmpio);