ioprio.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * fs/ioprio.c
  4. *
  5. * Copyright (C) 2004 Jens Axboe <axboe@kernel.dk>
  6. *
  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
  12. * being the lowest.
  13. *
  14. * IOW, setting BE scheduling class with prio 2 is done ala:
  15. *
  16. * unsigned int prio = (IOPRIO_CLASS_BE << IOPRIO_CLASS_SHIFT) | 2;
  17. *
  18. * ioprio_set(PRIO_PROCESS, pid, prio);
  19. *
  20. * See also Documentation/block/ioprio.rst
  21. *
  22. */
  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>
  35. int set_task_ioprio(struct task_struct *task, int ioprio)
  36. {
  37. int err;
  38. struct io_context *ioc;
  39. const struct cred *cred = current_cred(), *tcred;
  40. rcu_read_lock();
  41. tcred = __task_cred(task);
  42. if (!uid_eq(tcred->uid, cred->euid) &&
  43. !uid_eq(tcred->uid, cred->uid) && !capable(CAP_SYS_NICE)) {
  44. rcu_read_unlock();
  45. return -EPERM;
  46. }
  47. rcu_read_unlock();
  48. err = security_task_setioprio(task, ioprio);
  49. if (err)
  50. return err;
  51. ioc = get_task_io_context(task, GFP_ATOMIC, NUMA_NO_NODE);
  52. if (ioc) {
  53. ioc->ioprio = ioprio;
  54. put_io_context(ioc);
  55. }
  56. return err;
  57. }
  58. EXPORT_SYMBOL_GPL(set_task_ioprio);
  59. int ioprio_check_cap(int ioprio)
  60. {
  61. int class = IOPRIO_PRIO_CLASS(ioprio);
  62. int data = IOPRIO_PRIO_DATA(ioprio);
  63. switch (class) {
  64. case IOPRIO_CLASS_RT:
  65. /*
  66. * Originally this only checked for CAP_SYS_ADMIN,
  67. * which was implicitly allowed for pid 0 by security
  68. * modules such as SELinux. Make sure we check
  69. * CAP_SYS_ADMIN first to avoid a denial/avc for
  70. * possibly missing CAP_SYS_NICE permission.
  71. */
  72. if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_NICE))
  73. return -EPERM;
  74. fallthrough;
  75. /* rt has prio field too */
  76. case IOPRIO_CLASS_BE:
  77. if (data >= IOPRIO_BE_NR || data < 0)
  78. return -EINVAL;
  79. break;
  80. case IOPRIO_CLASS_IDLE:
  81. break;
  82. case IOPRIO_CLASS_NONE:
  83. if (data)
  84. return -EINVAL;
  85. break;
  86. default:
  87. return -EINVAL;
  88. }
  89. return 0;
  90. }
  91. SYSCALL_DEFINE3(ioprio_set, int, which, int, who, int, ioprio)
  92. {
  93. struct task_struct *p, *g;
  94. struct user_struct *user;
  95. struct pid *pgrp;
  96. kuid_t uid;
  97. int ret;
  98. ret = ioprio_check_cap(ioprio);
  99. if (ret)
  100. return ret;
  101. ret = -ESRCH;
  102. rcu_read_lock();
  103. switch (which) {
  104. case IOPRIO_WHO_PROCESS:
  105. if (!who)
  106. p = current;
  107. else
  108. p = find_task_by_vpid(who);
  109. if (p)
  110. ret = set_task_ioprio(p, ioprio);
  111. break;
  112. case IOPRIO_WHO_PGRP:
  113. if (!who)
  114. pgrp = task_pgrp(current);
  115. else
  116. pgrp = find_vpid(who);
  117. do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
  118. ret = set_task_ioprio(p, ioprio);
  119. if (ret)
  120. break;
  121. } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
  122. break;
  123. case IOPRIO_WHO_USER:
  124. uid = make_kuid(current_user_ns(), who);
  125. if (!uid_valid(uid))
  126. break;
  127. if (!who)
  128. user = current_user();
  129. else
  130. user = find_user(uid);
  131. if (!user)
  132. break;
  133. for_each_process_thread(g, p) {
  134. if (!uid_eq(task_uid(p), uid) ||
  135. !task_pid_vnr(p))
  136. continue;
  137. ret = set_task_ioprio(p, ioprio);
  138. if (ret)
  139. goto free_uid;
  140. }
  141. free_uid:
  142. if (who)
  143. free_uid(user);
  144. break;
  145. default:
  146. ret = -EINVAL;
  147. }
  148. rcu_read_unlock();
  149. return ret;
  150. }
  151. static int get_task_ioprio(struct task_struct *p)
  152. {
  153. int ret;
  154. ret = security_task_getioprio(p);
  155. if (ret)
  156. goto out;
  157. ret = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_NONE, IOPRIO_NORM);
  158. task_lock(p);
  159. if (p->io_context)
  160. ret = p->io_context->ioprio;
  161. task_unlock(p);
  162. out:
  163. return ret;
  164. }
  165. int ioprio_best(unsigned short aprio, unsigned short bprio)
  166. {
  167. if (!ioprio_valid(aprio))
  168. aprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, IOPRIO_NORM);
  169. if (!ioprio_valid(bprio))
  170. bprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, IOPRIO_NORM);
  171. return min(aprio, bprio);
  172. }
  173. SYSCALL_DEFINE2(ioprio_get, int, which, int, who)
  174. {
  175. struct task_struct *g, *p;
  176. struct user_struct *user;
  177. struct pid *pgrp;
  178. kuid_t uid;
  179. int ret = -ESRCH;
  180. int tmpio;
  181. rcu_read_lock();
  182. switch (which) {
  183. case IOPRIO_WHO_PROCESS:
  184. if (!who)
  185. p = current;
  186. else
  187. p = find_task_by_vpid(who);
  188. if (p)
  189. ret = get_task_ioprio(p);
  190. break;
  191. case IOPRIO_WHO_PGRP:
  192. if (!who)
  193. pgrp = task_pgrp(current);
  194. else
  195. pgrp = find_vpid(who);
  196. read_lock(&tasklist_lock);
  197. do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
  198. tmpio = get_task_ioprio(p);
  199. if (tmpio < 0)
  200. continue;
  201. if (ret == -ESRCH)
  202. ret = tmpio;
  203. else
  204. ret = ioprio_best(ret, tmpio);
  205. } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
  206. read_unlock(&tasklist_lock);
  207. break;
  208. case IOPRIO_WHO_USER:
  209. uid = make_kuid(current_user_ns(), who);
  210. if (!who)
  211. user = current_user();
  212. else
  213. user = find_user(uid);
  214. if (!user)
  215. break;
  216. for_each_process_thread(g, p) {
  217. if (!uid_eq(task_uid(p), user->uid) ||
  218. !task_pid_vnr(p))
  219. continue;
  220. tmpio = get_task_ioprio(p);
  221. if (tmpio < 0)
  222. continue;
  223. if (ret == -ESRCH)
  224. ret = tmpio;
  225. else
  226. ret = ioprio_best(ret, tmpio);
  227. }
  228. if (who)
  229. free_uid(user);
  230. break;
  231. default:
  232. ret = -EINVAL;
  233. }
  234. rcu_read_unlock();
  235. return ret;
  236. }