]>
git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - block/ioprio.c
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/cred.h>
27 #include <linux/blkdev.h>
28 #include <linux/capability.h>
29 #include <linux/sched/user.h>
30 #include <linux/sched/task.h>
31 #include <linux/syscalls.h>
32 #include <linux/security.h>
33 #include <linux/pid_namespace.h>
35 int set_task_ioprio(struct task_struct
*task
, int ioprio
)
38 struct io_context
*ioc
;
39 const struct cred
*cred
= current_cred(), *tcred
;
42 tcred
= __task_cred(task
);
43 if (!uid_eq(tcred
->uid
, cred
->euid
) &&
44 !uid_eq(tcred
->uid
, cred
->uid
) && !capable(CAP_SYS_NICE
)) {
50 err
= security_task_setioprio(task
, ioprio
);
54 ioc
= get_task_io_context(task
, GFP_ATOMIC
, NUMA_NO_NODE
);
62 EXPORT_SYMBOL_GPL(set_task_ioprio
);
64 SYSCALL_DEFINE3(ioprio_set
, int, which
, int, who
, int, ioprio
)
66 int class = IOPRIO_PRIO_CLASS(ioprio
);
67 int data
= IOPRIO_PRIO_DATA(ioprio
);
68 struct task_struct
*p
, *g
;
69 struct user_struct
*user
;
76 if (!capable(CAP_SYS_ADMIN
))
78 /* fall through, rt has prio field too */
80 if (data
>= IOPRIO_BE_NR
|| data
< 0)
84 case IOPRIO_CLASS_IDLE
:
86 case IOPRIO_CLASS_NONE
:
97 case IOPRIO_WHO_PROCESS
:
101 p
= find_task_by_vpid(who
);
103 ret
= set_task_ioprio(p
, ioprio
);
105 case IOPRIO_WHO_PGRP
:
107 pgrp
= task_pgrp(current
);
109 pgrp
= find_vpid(who
);
110 do_each_pid_thread(pgrp
, PIDTYPE_PGID
, p
) {
111 ret
= set_task_ioprio(p
, ioprio
);
114 } while_each_pid_thread(pgrp
, PIDTYPE_PGID
, p
);
116 case IOPRIO_WHO_USER
:
117 uid
= make_kuid(current_user_ns(), who
);
121 user
= current_user();
123 user
= find_user(uid
);
128 for_each_process_thread(g
, p
) {
129 if (!uid_eq(task_uid(p
), uid
) ||
132 ret
= set_task_ioprio(p
, ioprio
);
148 static int get_task_ioprio(struct task_struct
*p
)
152 ret
= security_task_getioprio(p
);
155 ret
= IOPRIO_PRIO_VALUE(IOPRIO_CLASS_NONE
, IOPRIO_NORM
);
158 ret
= p
->io_context
->ioprio
;
164 int ioprio_best(unsigned short aprio
, unsigned short bprio
)
166 unsigned short aclass
;
167 unsigned short bclass
;
169 if (!ioprio_valid(aprio
))
170 aprio
= IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE
, IOPRIO_NORM
);
171 if (!ioprio_valid(bprio
))
172 bprio
= IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE
, IOPRIO_NORM
);
174 aclass
= IOPRIO_PRIO_CLASS(aprio
);
175 bclass
= IOPRIO_PRIO_CLASS(bprio
);
176 if (aclass
== bclass
)
177 return min(aprio
, bprio
);
184 SYSCALL_DEFINE2(ioprio_get
, int, which
, int, who
)
186 struct task_struct
*g
, *p
;
187 struct user_struct
*user
;
195 case IOPRIO_WHO_PROCESS
:
199 p
= find_task_by_vpid(who
);
201 ret
= get_task_ioprio(p
);
203 case IOPRIO_WHO_PGRP
:
205 pgrp
= task_pgrp(current
);
207 pgrp
= find_vpid(who
);
208 do_each_pid_thread(pgrp
, PIDTYPE_PGID
, p
) {
209 tmpio
= get_task_ioprio(p
);
215 ret
= ioprio_best(ret
, tmpio
);
216 } while_each_pid_thread(pgrp
, PIDTYPE_PGID
, p
);
218 case IOPRIO_WHO_USER
:
219 uid
= make_kuid(current_user_ns(), who
);
221 user
= current_user();
223 user
= find_user(uid
);
228 for_each_process_thread(g
, p
) {
229 if (!uid_eq(task_uid(p
), user
->uid
) ||
232 tmpio
= get_task_ioprio(p
);
238 ret
= ioprio_best(ret
, tmpio
);