]>
git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - block/ioprio.c
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
);
123 read_lock(&tasklist_lock
);
124 do_each_pid_thread(pgrp
, PIDTYPE_PGID
, p
) {
125 ret
= set_task_ioprio(p
, ioprio
);
127 read_unlock(&tasklist_lock
);
130 } while_each_pid_thread(pgrp
, PIDTYPE_PGID
, p
);
131 read_unlock(&tasklist_lock
);
134 case IOPRIO_WHO_USER
:
135 uid
= make_kuid(current_user_ns(), who
);
139 user
= current_user();
141 user
= find_user(uid
);
146 for_each_process_thread(g
, p
) {
147 if (!uid_eq(task_uid(p
), uid
) ||
150 ret
= set_task_ioprio(p
, ioprio
);
167 static int get_task_ioprio(struct task_struct
*p
)
171 ret
= security_task_getioprio(p
);
174 ret
= IOPRIO_PRIO_VALUE(IOPRIO_CLASS_NONE
, IOPRIO_NORM
);
177 ret
= p
->io_context
->ioprio
;
183 int ioprio_best(unsigned short aprio
, unsigned short bprio
)
185 if (!ioprio_valid(aprio
))
186 aprio
= IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE
, IOPRIO_NORM
);
187 if (!ioprio_valid(bprio
))
188 bprio
= IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE
, IOPRIO_NORM
);
190 return min(aprio
, bprio
);
193 SYSCALL_DEFINE2(ioprio_get
, int, which
, int, who
)
195 struct task_struct
*g
, *p
;
196 struct user_struct
*user
;
204 case IOPRIO_WHO_PROCESS
:
208 p
= find_task_by_vpid(who
);
210 ret
= get_task_ioprio(p
);
212 case IOPRIO_WHO_PGRP
:
214 pgrp
= task_pgrp(current
);
216 pgrp
= find_vpid(who
);
217 do_each_pid_thread(pgrp
, PIDTYPE_PGID
, p
) {
218 tmpio
= get_task_ioprio(p
);
224 ret
= ioprio_best(ret
, tmpio
);
225 } while_each_pid_thread(pgrp
, PIDTYPE_PGID
, p
);
227 case IOPRIO_WHO_USER
:
228 uid
= make_kuid(current_user_ns(), who
);
230 user
= current_user();
232 user
= find_user(uid
);
237 for_each_process_thread(g
, p
) {
238 if (!uid_eq(task_uid(p
), user
->uid
) ||
241 tmpio
= get_task_ioprio(p
);
247 ret
= ioprio_best(ret
, tmpio
);