4 * Copyright (C) 2004 Jens Axboe <axboe@kernel.dk>
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
13 * IOW, setting BE scheduling class with prio 2 is done ala:
15 * unsigned int prio = (IOPRIO_CLASS_BE << IOPRIO_CLASS_SHIFT) | 2;
17 * ioprio_set(PRIO_PROCESS, pid, prio);
19 * See also Documentation/block/ioprio.txt
22 #include <linux/gfp.h>
23 #include <linux/kernel.h>
24 #include <linux/export.h>
25 #include <linux/ioprio.h>
26 #include <linux/blkdev.h>
27 #include <linux/capability.h>
28 #include <linux/sched/user.h>
29 #include <linux/syscalls.h>
30 #include <linux/security.h>
31 #include <linux/pid_namespace.h>
33 int set_task_ioprio(struct task_struct *task, int ioprio)
36 struct io_context *ioc;
37 const struct cred *cred = current_cred(), *tcred;
40 tcred = __task_cred(task);
41 if (!uid_eq(tcred->uid, cred->euid) &&
42 !uid_eq(tcred->uid, cred->uid) && !capable(CAP_SYS_NICE)) {
48 err = security_task_setioprio(task, ioprio);
52 ioc = get_task_io_context(task, GFP_ATOMIC, NUMA_NO_NODE);
60 EXPORT_SYMBOL_GPL(set_task_ioprio);
62 SYSCALL_DEFINE3(ioprio_set, int, which, int, who, int, ioprio)
64 int class = IOPRIO_PRIO_CLASS(ioprio);
65 int data = IOPRIO_PRIO_DATA(ioprio);
66 struct task_struct *p, *g;
67 struct user_struct *user;
74 if (!capable(CAP_SYS_ADMIN))
76 /* fall through, rt has prio field too */
78 if (data >= IOPRIO_BE_NR || data < 0)
82 case IOPRIO_CLASS_IDLE:
84 case IOPRIO_CLASS_NONE:
95 case IOPRIO_WHO_PROCESS:
99 p = find_task_by_vpid(who);
101 ret = set_task_ioprio(p, ioprio);
103 case IOPRIO_WHO_PGRP:
105 pgrp = task_pgrp(current);
107 pgrp = find_vpid(who);
108 do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
109 ret = set_task_ioprio(p, ioprio);
112 } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
114 case IOPRIO_WHO_USER:
115 uid = make_kuid(current_user_ns(), who);
119 user = current_user();
121 user = find_user(uid);
126 for_each_process_thread(g, p) {
127 if (!uid_eq(task_uid(p), uid) ||
130 ret = set_task_ioprio(p, ioprio);
146 static int get_task_ioprio(struct task_struct *p)
150 ret = security_task_getioprio(p);
153 ret = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_NONE, IOPRIO_NORM);
156 ret = p->io_context->ioprio;
162 int ioprio_best(unsigned short aprio, unsigned short bprio)
164 unsigned short aclass;
165 unsigned short bclass;
167 if (!ioprio_valid(aprio))
168 aprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, IOPRIO_NORM);
169 if (!ioprio_valid(bprio))
170 bprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, IOPRIO_NORM);
172 aclass = IOPRIO_PRIO_CLASS(aprio);
173 bclass = IOPRIO_PRIO_CLASS(bprio);
174 if (aclass == bclass)
175 return min(aprio, bprio);
182 SYSCALL_DEFINE2(ioprio_get, int, which, int, who)
184 struct task_struct *g, *p;
185 struct user_struct *user;
193 case IOPRIO_WHO_PROCESS:
197 p = find_task_by_vpid(who);
199 ret = get_task_ioprio(p);
201 case IOPRIO_WHO_PGRP:
203 pgrp = task_pgrp(current);
205 pgrp = find_vpid(who);
206 do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
207 tmpio = get_task_ioprio(p);
213 ret = ioprio_best(ret, tmpio);
214 } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
216 case IOPRIO_WHO_USER:
217 uid = make_kuid(current_user_ns(), who);
219 user = current_user();
221 user = find_user(uid);
226 for_each_process_thread(g, p) {
227 if (!uid_eq(task_uid(p), user->uid) ||
230 tmpio = get_task_ioprio(p);
236 ret = ioprio_best(ret, tmpio);