xref: /linux/block/ioprio.c (revision f088104d837a991c65e51fa30bb4196169b3244d)
13dcf60bcSChristoph Hellwig // SPDX-License-Identifier: GPL-2.0
22667bcbbSJens Axboe /*
32667bcbbSJens Axboe  * fs/ioprio.c
42667bcbbSJens Axboe  *
52667bcbbSJens Axboe  * Copyright (C) 2004 Jens Axboe <axboe@kernel.dk>
62667bcbbSJens Axboe  *
72667bcbbSJens Axboe  * Helper functions for setting/querying io priorities of processes. The
82667bcbbSJens Axboe  * system calls closely mimmick getpriority/setpriority, see the man page for
92667bcbbSJens Axboe  * those. The prio argument is a composite of prio class and prio data, where
102667bcbbSJens Axboe  * the data argument has meaning within that class. The standard scheduling
112667bcbbSJens Axboe  * classes have 8 distinct prio levels, with 0 being the highest prio and 7
122667bcbbSJens Axboe  * being the lowest.
132667bcbbSJens Axboe  *
142667bcbbSJens Axboe  * IOW, setting BE scheduling class with prio 2 is done ala:
152667bcbbSJens Axboe  *
162667bcbbSJens Axboe  * unsigned int prio = (IOPRIO_CLASS_BE << IOPRIO_CLASS_SHIFT) | 2;
172667bcbbSJens Axboe  *
182667bcbbSJens Axboe  * ioprio_set(PRIO_PROCESS, pid, prio);
192667bcbbSJens Axboe  *
20898bd37aSMauro Carvalho Chehab  * See also Documentation/block/ioprio.rst
212667bcbbSJens Axboe  *
222667bcbbSJens Axboe  */
232667bcbbSJens Axboe #include <linux/gfp.h>
242667bcbbSJens Axboe #include <linux/kernel.h>
252667bcbbSJens Axboe #include <linux/ioprio.h>
265b825c3aSIngo Molnar #include <linux/cred.h>
272667bcbbSJens Axboe #include <linux/blkdev.h>
282667bcbbSJens Axboe #include <linux/capability.h>
292667bcbbSJens Axboe #include <linux/syscalls.h>
302667bcbbSJens Axboe #include <linux/security.h>
312667bcbbSJens Axboe #include <linux/pid_namespace.h>
322667bcbbSJens Axboe 
ioprio_check_cap(int ioprio)33aa434577SAdam Manzanares int ioprio_check_cap(int ioprio)
342667bcbbSJens Axboe {
352667bcbbSJens Axboe 	int class = IOPRIO_PRIO_CLASS(ioprio);
36eca20409SDamien Le Moal 	int level = IOPRIO_PRIO_LEVEL(ioprio);
372667bcbbSJens Axboe 
382667bcbbSJens Axboe 	switch (class) {
392667bcbbSJens Axboe 		case IOPRIO_CLASS_RT:
4094c4b4fdSAlistair Delva 			/*
4194c4b4fdSAlistair Delva 			 * Originally this only checked for CAP_SYS_ADMIN,
4294c4b4fdSAlistair Delva 			 * which was implicitly allowed for pid 0 by security
4394c4b4fdSAlistair Delva 			 * modules such as SELinux. Make sure we check
4494c4b4fdSAlistair Delva 			 * CAP_SYS_ADMIN first to avoid a denial/avc for
4594c4b4fdSAlistair Delva 			 * possibly missing CAP_SYS_NICE permission.
4694c4b4fdSAlistair Delva 			 */
4794c4b4fdSAlistair Delva 			if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_NICE))
482667bcbbSJens Axboe 				return -EPERM;
492667bcbbSJens Axboe 			break;
50*c0d0a9ffSAaron Lu 		case IOPRIO_CLASS_BE:
512667bcbbSJens Axboe 		case IOPRIO_CLASS_IDLE:
522667bcbbSJens Axboe 			break;
532667bcbbSJens Axboe 		case IOPRIO_CLASS_NONE:
54eca20409SDamien Le Moal 			if (level)
552667bcbbSJens Axboe 				return -EINVAL;
562667bcbbSJens Axboe 			break;
5701584c1eSDamien Le Moal 		case IOPRIO_CLASS_INVALID:
582667bcbbSJens Axboe 		default:
592667bcbbSJens Axboe 			return -EINVAL;
602667bcbbSJens Axboe 	}
612667bcbbSJens Axboe 
62aa434577SAdam Manzanares 	return 0;
63aa434577SAdam Manzanares }
64aa434577SAdam Manzanares 
SYSCALL_DEFINE3(ioprio_set,int,which,int,who,int,ioprio)65aa434577SAdam Manzanares SYSCALL_DEFINE3(ioprio_set, int, which, int, who, int, ioprio)
66aa434577SAdam Manzanares {
67aa434577SAdam Manzanares 	struct task_struct *p, *g;
68aa434577SAdam Manzanares 	struct user_struct *user;
69aa434577SAdam Manzanares 	struct pid *pgrp;
70aa434577SAdam Manzanares 	kuid_t uid;
71aa434577SAdam Manzanares 	int ret;
72aa434577SAdam Manzanares 
73aa434577SAdam Manzanares 	ret = ioprio_check_cap(ioprio);
74aa434577SAdam Manzanares 	if (ret)
75aa434577SAdam Manzanares 		return ret;
76aa434577SAdam Manzanares 
772667bcbbSJens Axboe 	ret = -ESRCH;
782667bcbbSJens Axboe 	rcu_read_lock();
792667bcbbSJens Axboe 	switch (which) {
802667bcbbSJens Axboe 		case IOPRIO_WHO_PROCESS:
812667bcbbSJens Axboe 			if (!who)
822667bcbbSJens Axboe 				p = current;
832667bcbbSJens Axboe 			else
842667bcbbSJens Axboe 				p = find_task_by_vpid(who);
852667bcbbSJens Axboe 			if (p)
862667bcbbSJens Axboe 				ret = set_task_ioprio(p, ioprio);
872667bcbbSJens Axboe 			break;
882667bcbbSJens Axboe 		case IOPRIO_WHO_PGRP:
892667bcbbSJens Axboe 			if (!who)
902667bcbbSJens Axboe 				pgrp = task_pgrp(current);
912667bcbbSJens Axboe 			else
922667bcbbSJens Axboe 				pgrp = find_vpid(who);
9340c7fd3fSPeter Zijlstra 
9440c7fd3fSPeter Zijlstra 			read_lock(&tasklist_lock);
952667bcbbSJens Axboe 			do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
962667bcbbSJens Axboe 				ret = set_task_ioprio(p, ioprio);
9740c7fd3fSPeter Zijlstra 				if (ret) {
9840c7fd3fSPeter Zijlstra 					read_unlock(&tasklist_lock);
9940c7fd3fSPeter Zijlstra 					goto out;
10040c7fd3fSPeter Zijlstra 				}
1012667bcbbSJens Axboe 			} while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
10240c7fd3fSPeter Zijlstra 			read_unlock(&tasklist_lock);
10340c7fd3fSPeter Zijlstra 
1042667bcbbSJens Axboe 			break;
1052667bcbbSJens Axboe 		case IOPRIO_WHO_USER:
1062667bcbbSJens Axboe 			uid = make_kuid(current_user_ns(), who);
1072667bcbbSJens Axboe 			if (!uid_valid(uid))
1082667bcbbSJens Axboe 				break;
1092667bcbbSJens Axboe 			if (!who)
1102667bcbbSJens Axboe 				user = current_user();
1112667bcbbSJens Axboe 			else
1122667bcbbSJens Axboe 				user = find_user(uid);
1132667bcbbSJens Axboe 
1142667bcbbSJens Axboe 			if (!user)
1152667bcbbSJens Axboe 				break;
1162667bcbbSJens Axboe 
117612dafabSTetsuo Handa 			for_each_process_thread(g, p) {
1188639b461SBen Segall 				if (!uid_eq(task_uid(p), uid) ||
1198639b461SBen Segall 				    !task_pid_vnr(p))
1202667bcbbSJens Axboe 					continue;
1212667bcbbSJens Axboe 				ret = set_task_ioprio(p, ioprio);
1222667bcbbSJens Axboe 				if (ret)
1232667bcbbSJens Axboe 					goto free_uid;
124612dafabSTetsuo Handa 			}
1252667bcbbSJens Axboe free_uid:
1262667bcbbSJens Axboe 			if (who)
1272667bcbbSJens Axboe 				free_uid(user);
1282667bcbbSJens Axboe 			break;
1292667bcbbSJens Axboe 		default:
1302667bcbbSJens Axboe 			ret = -EINVAL;
1312667bcbbSJens Axboe 	}
1322667bcbbSJens Axboe 
13340c7fd3fSPeter Zijlstra out:
1342667bcbbSJens Axboe 	rcu_read_unlock();
1352667bcbbSJens Axboe 	return ret;
1362667bcbbSJens Axboe }
1372667bcbbSJens Axboe 
get_task_ioprio(struct task_struct * p)1382667bcbbSJens Axboe static int get_task_ioprio(struct task_struct *p)
1392667bcbbSJens Axboe {
1402667bcbbSJens Axboe 	int ret;
1412667bcbbSJens Axboe 
1422667bcbbSJens Axboe 	ret = security_task_getioprio(p);
1432667bcbbSJens Axboe 	if (ret)
1442667bcbbSJens Axboe 		goto out;
1454b838d9eSJan Kara 	task_lock(p);
1464b838d9eSJan Kara 	ret = __get_task_ioprio(p);
1474b838d9eSJan Kara 	task_unlock(p);
1484b838d9eSJan Kara out:
1494b838d9eSJan Kara 	return ret;
1504b838d9eSJan Kara }
1514b838d9eSJan Kara 
1524b838d9eSJan Kara /*
1534b838d9eSJan Kara  * Return raw IO priority value as set by userspace. We use this for
1544b838d9eSJan Kara  * ioprio_get(pid, IOPRIO_WHO_PROCESS) so that we keep historical behavior and
1554b838d9eSJan Kara  * also so that userspace can distinguish unset IO priority (which just gets
1564b838d9eSJan Kara  * overriden based on task's nice value) from IO priority set to some value.
1574b838d9eSJan Kara  */
get_task_raw_ioprio(struct task_struct * p)1584b838d9eSJan Kara static int get_task_raw_ioprio(struct task_struct *p)
1594b838d9eSJan Kara {
1604b838d9eSJan Kara 	int ret;
1614b838d9eSJan Kara 
1624b838d9eSJan Kara 	ret = security_task_getioprio(p);
1634b838d9eSJan Kara 	if (ret)
1644b838d9eSJan Kara 		goto out;
1658ba86821SOmar Sandoval 	task_lock(p);
1662667bcbbSJens Axboe 	if (p->io_context)
1672667bcbbSJens Axboe 		ret = p->io_context->ioprio;
1684b838d9eSJan Kara 	else
1694b838d9eSJan Kara 		ret = IOPRIO_DEFAULT;
1708ba86821SOmar Sandoval 	task_unlock(p);
1712667bcbbSJens Axboe out:
1722667bcbbSJens Axboe 	return ret;
1732667bcbbSJens Axboe }
1742667bcbbSJens Axboe 
ioprio_best(unsigned short aprio,unsigned short bprio)175fc25545eSJan Kara static int ioprio_best(unsigned short aprio, unsigned short bprio)
1762667bcbbSJens Axboe {
1772667bcbbSJens Axboe 	return min(aprio, bprio);
1782667bcbbSJens Axboe }
1792667bcbbSJens Axboe 
SYSCALL_DEFINE2(ioprio_get,int,which,int,who)1802667bcbbSJens Axboe SYSCALL_DEFINE2(ioprio_get, int, which, int, who)
1812667bcbbSJens Axboe {
1822667bcbbSJens Axboe 	struct task_struct *g, *p;
1832667bcbbSJens Axboe 	struct user_struct *user;
1842667bcbbSJens Axboe 	struct pid *pgrp;
1852667bcbbSJens Axboe 	kuid_t uid;
1862667bcbbSJens Axboe 	int ret = -ESRCH;
1872667bcbbSJens Axboe 	int tmpio;
1882667bcbbSJens Axboe 
1892667bcbbSJens Axboe 	rcu_read_lock();
1902667bcbbSJens Axboe 	switch (which) {
1912667bcbbSJens Axboe 		case IOPRIO_WHO_PROCESS:
1922667bcbbSJens Axboe 			if (!who)
1932667bcbbSJens Axboe 				p = current;
1942667bcbbSJens Axboe 			else
1952667bcbbSJens Axboe 				p = find_task_by_vpid(who);
1962667bcbbSJens Axboe 			if (p)
1974b838d9eSJan Kara 				ret = get_task_raw_ioprio(p);
1982667bcbbSJens Axboe 			break;
1992667bcbbSJens Axboe 		case IOPRIO_WHO_PGRP:
2002667bcbbSJens Axboe 			if (!who)
2012667bcbbSJens Axboe 				pgrp = task_pgrp(current);
2022667bcbbSJens Axboe 			else
2032667bcbbSJens Axboe 				pgrp = find_vpid(who);
204e6a59aacSDavidlohr Bueso 			read_lock(&tasklist_lock);
2052667bcbbSJens Axboe 			do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
2062667bcbbSJens Axboe 				tmpio = get_task_ioprio(p);
2072667bcbbSJens Axboe 				if (tmpio < 0)
2082667bcbbSJens Axboe 					continue;
2092667bcbbSJens Axboe 				if (ret == -ESRCH)
2102667bcbbSJens Axboe 					ret = tmpio;
2112667bcbbSJens Axboe 				else
2122667bcbbSJens Axboe 					ret = ioprio_best(ret, tmpio);
2132667bcbbSJens Axboe 			} while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
214e6a59aacSDavidlohr Bueso 			read_unlock(&tasklist_lock);
215e6a59aacSDavidlohr Bueso 
2162667bcbbSJens Axboe 			break;
2172667bcbbSJens Axboe 		case IOPRIO_WHO_USER:
2182667bcbbSJens Axboe 			uid = make_kuid(current_user_ns(), who);
2192667bcbbSJens Axboe 			if (!who)
2202667bcbbSJens Axboe 				user = current_user();
2212667bcbbSJens Axboe 			else
2222667bcbbSJens Axboe 				user = find_user(uid);
2232667bcbbSJens Axboe 
2242667bcbbSJens Axboe 			if (!user)
2252667bcbbSJens Axboe 				break;
2262667bcbbSJens Axboe 
227612dafabSTetsuo Handa 			for_each_process_thread(g, p) {
2288639b461SBen Segall 				if (!uid_eq(task_uid(p), user->uid) ||
2298639b461SBen Segall 				    !task_pid_vnr(p))
2302667bcbbSJens Axboe 					continue;
2312667bcbbSJens Axboe 				tmpio = get_task_ioprio(p);
2322667bcbbSJens Axboe 				if (tmpio < 0)
2332667bcbbSJens Axboe 					continue;
2342667bcbbSJens Axboe 				if (ret == -ESRCH)
2352667bcbbSJens Axboe 					ret = tmpio;
2362667bcbbSJens Axboe 				else
2372667bcbbSJens Axboe 					ret = ioprio_best(ret, tmpio);
238612dafabSTetsuo Handa 			}
2392667bcbbSJens Axboe 
2402667bcbbSJens Axboe 			if (who)
2412667bcbbSJens Axboe 				free_uid(user);
2422667bcbbSJens Axboe 			break;
2432667bcbbSJens Axboe 		default:
2442667bcbbSJens Axboe 			ret = -EINVAL;
2452667bcbbSJens Axboe 	}
2462667bcbbSJens Axboe 
2472667bcbbSJens Axboe 	rcu_read_unlock();
2482667bcbbSJens Axboe 	return ret;
2492667bcbbSJens Axboe }
250