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;
49df561f66SGustavo A. R. Silva fallthrough;
50e29387ebSBart Van Assche /* rt has prio field too */
512667bcbbSJens Axboe case IOPRIO_CLASS_BE:
52eca20409SDamien Le Moal if (level >= IOPRIO_NR_LEVELS)
532667bcbbSJens Axboe return -EINVAL;
542667bcbbSJens Axboe break;
552667bcbbSJens Axboe case IOPRIO_CLASS_IDLE:
562667bcbbSJens Axboe break;
572667bcbbSJens Axboe case IOPRIO_CLASS_NONE:
58eca20409SDamien Le Moal if (level)
592667bcbbSJens Axboe return -EINVAL;
602667bcbbSJens Axboe break;
61*01584c1eSDamien Le Moal case IOPRIO_CLASS_INVALID:
622667bcbbSJens Axboe default:
632667bcbbSJens Axboe return -EINVAL;
642667bcbbSJens Axboe }
652667bcbbSJens Axboe
66aa434577SAdam Manzanares return 0;
67aa434577SAdam Manzanares }
68aa434577SAdam Manzanares
SYSCALL_DEFINE3(ioprio_set,int,which,int,who,int,ioprio)69aa434577SAdam Manzanares SYSCALL_DEFINE3(ioprio_set, int, which, int, who, int, ioprio)
70aa434577SAdam Manzanares {
71aa434577SAdam Manzanares struct task_struct *p, *g;
72aa434577SAdam Manzanares struct user_struct *user;
73aa434577SAdam Manzanares struct pid *pgrp;
74aa434577SAdam Manzanares kuid_t uid;
75aa434577SAdam Manzanares int ret;
76aa434577SAdam Manzanares
77aa434577SAdam Manzanares ret = ioprio_check_cap(ioprio);
78aa434577SAdam Manzanares if (ret)
79aa434577SAdam Manzanares return ret;
80aa434577SAdam Manzanares
812667bcbbSJens Axboe ret = -ESRCH;
822667bcbbSJens Axboe rcu_read_lock();
832667bcbbSJens Axboe switch (which) {
842667bcbbSJens Axboe case IOPRIO_WHO_PROCESS:
852667bcbbSJens Axboe if (!who)
862667bcbbSJens Axboe p = current;
872667bcbbSJens Axboe else
882667bcbbSJens Axboe p = find_task_by_vpid(who);
892667bcbbSJens Axboe if (p)
902667bcbbSJens Axboe ret = set_task_ioprio(p, ioprio);
912667bcbbSJens Axboe break;
922667bcbbSJens Axboe case IOPRIO_WHO_PGRP:
932667bcbbSJens Axboe if (!who)
942667bcbbSJens Axboe pgrp = task_pgrp(current);
952667bcbbSJens Axboe else
962667bcbbSJens Axboe pgrp = find_vpid(who);
9740c7fd3fSPeter Zijlstra
9840c7fd3fSPeter Zijlstra read_lock(&tasklist_lock);
992667bcbbSJens Axboe do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
1002667bcbbSJens Axboe ret = set_task_ioprio(p, ioprio);
10140c7fd3fSPeter Zijlstra if (ret) {
10240c7fd3fSPeter Zijlstra read_unlock(&tasklist_lock);
10340c7fd3fSPeter Zijlstra goto out;
10440c7fd3fSPeter Zijlstra }
1052667bcbbSJens Axboe } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
10640c7fd3fSPeter Zijlstra read_unlock(&tasklist_lock);
10740c7fd3fSPeter Zijlstra
1082667bcbbSJens Axboe break;
1092667bcbbSJens Axboe case IOPRIO_WHO_USER:
1102667bcbbSJens Axboe uid = make_kuid(current_user_ns(), who);
1112667bcbbSJens Axboe if (!uid_valid(uid))
1122667bcbbSJens Axboe break;
1132667bcbbSJens Axboe if (!who)
1142667bcbbSJens Axboe user = current_user();
1152667bcbbSJens Axboe else
1162667bcbbSJens Axboe user = find_user(uid);
1172667bcbbSJens Axboe
1182667bcbbSJens Axboe if (!user)
1192667bcbbSJens Axboe break;
1202667bcbbSJens Axboe
121612dafabSTetsuo Handa for_each_process_thread(g, p) {
1228639b461SBen Segall if (!uid_eq(task_uid(p), uid) ||
1238639b461SBen Segall !task_pid_vnr(p))
1242667bcbbSJens Axboe continue;
1252667bcbbSJens Axboe ret = set_task_ioprio(p, ioprio);
1262667bcbbSJens Axboe if (ret)
1272667bcbbSJens Axboe goto free_uid;
128612dafabSTetsuo Handa }
1292667bcbbSJens Axboe free_uid:
1302667bcbbSJens Axboe if (who)
1312667bcbbSJens Axboe free_uid(user);
1322667bcbbSJens Axboe break;
1332667bcbbSJens Axboe default:
1342667bcbbSJens Axboe ret = -EINVAL;
1352667bcbbSJens Axboe }
1362667bcbbSJens Axboe
13740c7fd3fSPeter Zijlstra out:
1382667bcbbSJens Axboe rcu_read_unlock();
1392667bcbbSJens Axboe return ret;
1402667bcbbSJens Axboe }
1412667bcbbSJens Axboe
142893e5d32SJan Kara /*
143893e5d32SJan Kara * If the task has set an I/O priority, use that. Otherwise, return
144893e5d32SJan Kara * the default I/O priority.
145893e5d32SJan Kara *
146893e5d32SJan Kara * Expected to be called for current task or with task_lock() held to keep
147893e5d32SJan Kara * io_context stable.
148893e5d32SJan Kara */
__get_task_ioprio(struct task_struct * p)149893e5d32SJan Kara int __get_task_ioprio(struct task_struct *p)
150893e5d32SJan Kara {
151893e5d32SJan Kara struct io_context *ioc = p->io_context;
152893e5d32SJan Kara int prio;
153893e5d32SJan Kara
154893e5d32SJan Kara if (p != current)
155893e5d32SJan Kara lockdep_assert_held(&p->alloc_lock);
156893e5d32SJan Kara if (ioc)
157893e5d32SJan Kara prio = ioc->ioprio;
158893e5d32SJan Kara else
159893e5d32SJan Kara prio = IOPRIO_DEFAULT;
160893e5d32SJan Kara
161893e5d32SJan Kara if (IOPRIO_PRIO_CLASS(prio) == IOPRIO_CLASS_NONE)
162893e5d32SJan Kara prio = IOPRIO_PRIO_VALUE(task_nice_ioclass(p),
163893e5d32SJan Kara task_nice_ioprio(p));
164893e5d32SJan Kara return prio;
165893e5d32SJan Kara }
166893e5d32SJan Kara EXPORT_SYMBOL_GPL(__get_task_ioprio);
167893e5d32SJan Kara
get_task_ioprio(struct task_struct * p)1682667bcbbSJens Axboe static int get_task_ioprio(struct task_struct *p)
1692667bcbbSJens Axboe {
1702667bcbbSJens Axboe int ret;
1712667bcbbSJens Axboe
1722667bcbbSJens Axboe ret = security_task_getioprio(p);
1732667bcbbSJens Axboe if (ret)
1742667bcbbSJens Axboe goto out;
1754b838d9eSJan Kara task_lock(p);
1764b838d9eSJan Kara ret = __get_task_ioprio(p);
1774b838d9eSJan Kara task_unlock(p);
1784b838d9eSJan Kara out:
1794b838d9eSJan Kara return ret;
1804b838d9eSJan Kara }
1814b838d9eSJan Kara
1824b838d9eSJan Kara /*
1834b838d9eSJan Kara * Return raw IO priority value as set by userspace. We use this for
1844b838d9eSJan Kara * ioprio_get(pid, IOPRIO_WHO_PROCESS) so that we keep historical behavior and
1854b838d9eSJan Kara * also so that userspace can distinguish unset IO priority (which just gets
1864b838d9eSJan Kara * overriden based on task's nice value) from IO priority set to some value.
1874b838d9eSJan Kara */
get_task_raw_ioprio(struct task_struct * p)1884b838d9eSJan Kara static int get_task_raw_ioprio(struct task_struct *p)
1894b838d9eSJan Kara {
1904b838d9eSJan Kara int ret;
1914b838d9eSJan Kara
1924b838d9eSJan Kara ret = security_task_getioprio(p);
1934b838d9eSJan Kara if (ret)
1944b838d9eSJan Kara goto out;
1958ba86821SOmar Sandoval task_lock(p);
1962667bcbbSJens Axboe if (p->io_context)
1972667bcbbSJens Axboe ret = p->io_context->ioprio;
1984b838d9eSJan Kara else
1994b838d9eSJan Kara ret = IOPRIO_DEFAULT;
2008ba86821SOmar Sandoval task_unlock(p);
2012667bcbbSJens Axboe out:
2022667bcbbSJens Axboe return ret;
2032667bcbbSJens Axboe }
2042667bcbbSJens Axboe
ioprio_best(unsigned short aprio,unsigned short bprio)205fc25545eSJan Kara static int ioprio_best(unsigned short aprio, unsigned short bprio)
2062667bcbbSJens Axboe {
2072667bcbbSJens Axboe return min(aprio, bprio);
2082667bcbbSJens Axboe }
2092667bcbbSJens Axboe
SYSCALL_DEFINE2(ioprio_get,int,which,int,who)2102667bcbbSJens Axboe SYSCALL_DEFINE2(ioprio_get, int, which, int, who)
2112667bcbbSJens Axboe {
2122667bcbbSJens Axboe struct task_struct *g, *p;
2132667bcbbSJens Axboe struct user_struct *user;
2142667bcbbSJens Axboe struct pid *pgrp;
2152667bcbbSJens Axboe kuid_t uid;
2162667bcbbSJens Axboe int ret = -ESRCH;
2172667bcbbSJens Axboe int tmpio;
2182667bcbbSJens Axboe
2192667bcbbSJens Axboe rcu_read_lock();
2202667bcbbSJens Axboe switch (which) {
2212667bcbbSJens Axboe case IOPRIO_WHO_PROCESS:
2222667bcbbSJens Axboe if (!who)
2232667bcbbSJens Axboe p = current;
2242667bcbbSJens Axboe else
2252667bcbbSJens Axboe p = find_task_by_vpid(who);
2262667bcbbSJens Axboe if (p)
2274b838d9eSJan Kara ret = get_task_raw_ioprio(p);
2282667bcbbSJens Axboe break;
2292667bcbbSJens Axboe case IOPRIO_WHO_PGRP:
2302667bcbbSJens Axboe if (!who)
2312667bcbbSJens Axboe pgrp = task_pgrp(current);
2322667bcbbSJens Axboe else
2332667bcbbSJens Axboe pgrp = find_vpid(who);
234e6a59aacSDavidlohr Bueso read_lock(&tasklist_lock);
2352667bcbbSJens Axboe do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
2362667bcbbSJens Axboe tmpio = get_task_ioprio(p);
2372667bcbbSJens Axboe if (tmpio < 0)
2382667bcbbSJens Axboe continue;
2392667bcbbSJens Axboe if (ret == -ESRCH)
2402667bcbbSJens Axboe ret = tmpio;
2412667bcbbSJens Axboe else
2422667bcbbSJens Axboe ret = ioprio_best(ret, tmpio);
2432667bcbbSJens Axboe } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
244e6a59aacSDavidlohr Bueso read_unlock(&tasklist_lock);
245e6a59aacSDavidlohr Bueso
2462667bcbbSJens Axboe break;
2472667bcbbSJens Axboe case IOPRIO_WHO_USER:
2482667bcbbSJens Axboe uid = make_kuid(current_user_ns(), who);
2492667bcbbSJens Axboe if (!who)
2502667bcbbSJens Axboe user = current_user();
2512667bcbbSJens Axboe else
2522667bcbbSJens Axboe user = find_user(uid);
2532667bcbbSJens Axboe
2542667bcbbSJens Axboe if (!user)
2552667bcbbSJens Axboe break;
2562667bcbbSJens Axboe
257612dafabSTetsuo Handa for_each_process_thread(g, p) {
2588639b461SBen Segall if (!uid_eq(task_uid(p), user->uid) ||
2598639b461SBen Segall !task_pid_vnr(p))
2602667bcbbSJens Axboe continue;
2612667bcbbSJens Axboe tmpio = get_task_ioprio(p);
2622667bcbbSJens Axboe if (tmpio < 0)
2632667bcbbSJens Axboe continue;
2642667bcbbSJens Axboe if (ret == -ESRCH)
2652667bcbbSJens Axboe ret = tmpio;
2662667bcbbSJens Axboe else
2672667bcbbSJens Axboe ret = ioprio_best(ret, tmpio);
268612dafabSTetsuo Handa }
2692667bcbbSJens Axboe
2702667bcbbSJens Axboe if (who)
2712667bcbbSJens Axboe free_uid(user);
2722667bcbbSJens Axboe break;
2732667bcbbSJens Axboe default:
2742667bcbbSJens Axboe ret = -EINVAL;
2752667bcbbSJens Axboe }
2762667bcbbSJens Axboe
2772667bcbbSJens Axboe rcu_read_unlock();
2782667bcbbSJens Axboe return ret;
2792667bcbbSJens Axboe }
280