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/kernel.h>
23 #include <linux/ioprio.h>
24 #include <linux/blkdev.h>
25 #include <linux/capability.h>
26 #include <linux/syscalls.h>
27 #include <linux/security.h>
28 #include <linux/pid_namespace.h>
30 static int set_task_ioprio(struct task_struct
*task
, int ioprio
)
33 struct io_context
*ioc
;
35 if (task
->uid
!= current
->euid
&&
36 task
->uid
!= current
->uid
&& !capable(CAP_SYS_NICE
))
39 err
= security_task_setioprio(task
, ioprio
);
45 ioc
= task
->io_context
;
46 /* see wmb() in current_io_context() */
47 smp_read_barrier_depends();
51 ioc
= alloc_io_context(GFP_ATOMIC
, -1);
56 task
->io_context
= ioc
;
61 ioc
->ioprio_changed
= 1;
68 SYSCALL_DEFINE3(ioprio_set
, int, which
, int, who
, int, ioprio
)
70 int class = IOPRIO_PRIO_CLASS(ioprio
);
71 int data
= IOPRIO_PRIO_DATA(ioprio
);
72 struct task_struct
*p
, *g
;
73 struct user_struct
*user
;
79 if (!capable(CAP_SYS_ADMIN
))
81 /* fall through, rt has prio field too */
83 if (data
>= IOPRIO_BE_NR
|| data
< 0)
87 case IOPRIO_CLASS_IDLE
:
89 case IOPRIO_CLASS_NONE
:
99 * We want IOPRIO_WHO_PGRP/IOPRIO_WHO_USER to be "atomic",
100 * so we can't use rcu_read_lock(). See re-copy of ->ioprio
103 read_lock(&tasklist_lock
);
105 case IOPRIO_WHO_PROCESS
:
109 p
= find_task_by_vpid(who
);
111 ret
= set_task_ioprio(p
, ioprio
);
113 case IOPRIO_WHO_PGRP
:
115 pgrp
= task_pgrp(current
);
117 pgrp
= find_vpid(who
);
118 do_each_pid_thread(pgrp
, PIDTYPE_PGID
, p
) {
119 ret
= set_task_ioprio(p
, ioprio
);
122 } while_each_pid_thread(pgrp
, PIDTYPE_PGID
, p
);
124 case IOPRIO_WHO_USER
:
126 user
= current
->user
;
128 user
= find_user(who
);
133 do_each_thread(g
, p
) {
136 ret
= set_task_ioprio(p
, ioprio
);
139 } while_each_thread(g
, p
);
148 read_unlock(&tasklist_lock
);
152 static int get_task_ioprio(struct task_struct
*p
)
156 ret
= security_task_getioprio(p
);
159 ret
= IOPRIO_PRIO_VALUE(IOPRIO_CLASS_NONE
, IOPRIO_NORM
);
161 ret
= p
->io_context
->ioprio
;
166 int ioprio_best(unsigned short aprio
, unsigned short bprio
)
168 unsigned short aclass
= IOPRIO_PRIO_CLASS(aprio
);
169 unsigned short bclass
= IOPRIO_PRIO_CLASS(bprio
);
171 if (aclass
== IOPRIO_CLASS_NONE
)
172 aclass
= IOPRIO_CLASS_BE
;
173 if (bclass
== IOPRIO_CLASS_NONE
)
174 bclass
= IOPRIO_CLASS_BE
;
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
;
192 read_lock(&tasklist_lock
);
194 case IOPRIO_WHO_PROCESS
:
198 p
= find_task_by_vpid(who
);
200 ret
= get_task_ioprio(p
);
202 case IOPRIO_WHO_PGRP
:
204 pgrp
= task_pgrp(current
);
206 pgrp
= find_vpid(who
);
207 do_each_pid_thread(pgrp
, PIDTYPE_PGID
, p
) {
208 tmpio
= get_task_ioprio(p
);
214 ret
= ioprio_best(ret
, tmpio
);
215 } while_each_pid_thread(pgrp
, PIDTYPE_PGID
, p
);
217 case IOPRIO_WHO_USER
:
219 user
= current
->user
;
221 user
= find_user(who
);
226 do_each_thread(g
, p
) {
227 if (p
->uid
!= user
->uid
)
229 tmpio
= get_task_ioprio(p
);
235 ret
= ioprio_best(ret
, tmpio
);
236 } while_each_thread(g
, p
);
245 read_unlock(&tasklist_lock
);