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_BE_NR
|| data
< 0)
81 case IOPRIO_CLASS_IDLE
:
83 case IOPRIO_CLASS_NONE
:
94 SYSCALL_DEFINE3(ioprio_set
, int, which
, int, who
, int, ioprio
)
96 struct task_struct
*p
, *g
;
97 struct user_struct
*user
;
102 ret
= ioprio_check_cap(ioprio
);
109 case IOPRIO_WHO_PROCESS
:
113 p
= find_task_by_vpid(who
);
115 ret
= set_task_ioprio(p
, ioprio
);
117 case IOPRIO_WHO_PGRP
:
119 pgrp
= task_pgrp(current
);
121 pgrp
= find_vpid(who
);
122 do_each_pid_thread(pgrp
, PIDTYPE_PGID
, p
) {
123 ret
= set_task_ioprio(p
, ioprio
);
126 } while_each_pid_thread(pgrp
, PIDTYPE_PGID
, p
);
128 case IOPRIO_WHO_USER
:
129 uid
= make_kuid(current_user_ns(), who
);
133 user
= current_user();
135 user
= find_user(uid
);
140 for_each_process_thread(g
, p
) {
141 if (!uid_eq(task_uid(p
), uid
) ||
144 ret
= set_task_ioprio(p
, ioprio
);
160 static int get_task_ioprio(struct task_struct
*p
)
164 ret
= security_task_getioprio(p
);
167 ret
= IOPRIO_PRIO_VALUE(IOPRIO_CLASS_NONE
, IOPRIO_NORM
);
170 ret
= p
->io_context
->ioprio
;
176 int ioprio_best(unsigned short aprio
, unsigned short bprio
)
178 if (!ioprio_valid(aprio
))
179 aprio
= IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE
, IOPRIO_NORM
);
180 if (!ioprio_valid(bprio
))
181 bprio
= IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE
, IOPRIO_NORM
);
183 return min(aprio
, bprio
);
186 SYSCALL_DEFINE2(ioprio_get
, int, which
, int, who
)
188 struct task_struct
*g
, *p
;
189 struct user_struct
*user
;
197 case IOPRIO_WHO_PROCESS
:
201 p
= find_task_by_vpid(who
);
203 ret
= get_task_ioprio(p
);
205 case IOPRIO_WHO_PGRP
:
207 pgrp
= task_pgrp(current
);
209 pgrp
= find_vpid(who
);
210 do_each_pid_thread(pgrp
, PIDTYPE_PGID
, p
) {
211 tmpio
= get_task_ioprio(p
);
217 ret
= ioprio_best(ret
, tmpio
);
218 } while_each_pid_thread(pgrp
, PIDTYPE_PGID
, p
);
220 case IOPRIO_WHO_USER
:
221 uid
= make_kuid(current_user_ns(), who
);
223 user
= current_user();
225 user
= find_user(uid
);
230 for_each_process_thread(g
, p
) {
231 if (!uid_eq(task_uid(p
), user
->uid
) ||
234 tmpio
= get_task_ioprio(p
);
240 ret
= ioprio_best(ret
, tmpio
);