ioprio.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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/ioprio.h>
  26. #include <linux/cred.h>
  27. #include <linux/blkdev.h>
  28. #include <linux/capability.h>
  29. #include <linux/syscalls.h>
  30. #include <linux/security.h>
  31. #include <linux/pid_namespace.h>
  32. int ioprio_check_cap(int ioprio)
  33. {
  34. int class = IOPRIO_PRIO_CLASS(ioprio);
  35. int level = IOPRIO_PRIO_LEVEL(ioprio);
  36. switch (class) {
  37. case IOPRIO_CLASS_RT:
  38. /*
  39. * Originally this only checked for CAP_SYS_ADMIN,
  40. * which was implicitly allowed for pid 0 by security
  41. * modules such as SELinux. Make sure we check
  42. * CAP_SYS_ADMIN first to avoid a denial/avc for
  43. * possibly missing CAP_SYS_NICE permission.
  44. */
  45. if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_NICE))
  46. return -EPERM;
  47. fallthrough;
  48. /* rt has prio field too */
  49. case IOPRIO_CLASS_BE:
  50. if (level >= IOPRIO_NR_LEVELS)
  51. return -EINVAL;
  52. break;
  53. case IOPRIO_CLASS_IDLE:
  54. break;
  55. case IOPRIO_CLASS_NONE:
  56. if (level)
  57. return -EINVAL;
  58. break;
  59. case IOPRIO_CLASS_INVALID:
  60. default:
  61. return -EINVAL;
  62. }
  63. return 0;
  64. }
  65. SYSCALL_DEFINE3(ioprio_set, int, which, int, who, int, ioprio)
  66. {
  67. struct task_struct *p, *g;
  68. struct user_struct *user;
  69. struct pid *pgrp;
  70. kuid_t uid;
  71. int ret;
  72. ret = ioprio_check_cap(ioprio);
  73. if (ret)
  74. return ret;
  75. ret = -ESRCH;
  76. rcu_read_lock();
  77. switch (which) {
  78. case IOPRIO_WHO_PROCESS:
  79. if (!who)
  80. p = current;
  81. else
  82. p = find_task_by_vpid(who);
  83. if (p)
  84. ret = set_task_ioprio(p, ioprio);
  85. break;
  86. case IOPRIO_WHO_PGRP:
  87. if (!who)
  88. pgrp = task_pgrp(current);
  89. else
  90. pgrp = find_vpid(who);
  91. read_lock(&tasklist_lock);
  92. do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
  93. ret = set_task_ioprio(p, ioprio);
  94. if (ret) {
  95. read_unlock(&tasklist_lock);
  96. goto out;
  97. }
  98. } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
  99. read_unlock(&tasklist_lock);
  100. break;
  101. case IOPRIO_WHO_USER:
  102. uid = make_kuid(current_user_ns(), who);
  103. if (!uid_valid(uid))
  104. break;
  105. if (!who)
  106. user = current_user();
  107. else
  108. user = find_user(uid);
  109. if (!user)
  110. break;
  111. for_each_process_thread(g, p) {
  112. if (!uid_eq(task_uid(p), uid) ||
  113. !task_pid_vnr(p))
  114. continue;
  115. ret = set_task_ioprio(p, ioprio);
  116. if (ret)
  117. goto free_uid;
  118. }
  119. free_uid:
  120. if (who)
  121. free_uid(user);
  122. break;
  123. default:
  124. ret = -EINVAL;
  125. }
  126. out:
  127. rcu_read_unlock();
  128. return ret;
  129. }
  130. static int get_task_ioprio(struct task_struct *p)
  131. {
  132. int ret;
  133. ret = security_task_getioprio(p);
  134. if (ret)
  135. goto out;
  136. task_lock(p);
  137. ret = __get_task_ioprio(p);
  138. task_unlock(p);
  139. out:
  140. return ret;
  141. }
  142. /*
  143. * Return raw IO priority value as set by userspace. We use this for
  144. * ioprio_get(pid, IOPRIO_WHO_PROCESS) so that we keep historical behavior and
  145. * also so that userspace can distinguish unset IO priority (which just gets
  146. * overriden based on task's nice value) from IO priority set to some value.
  147. */
  148. static int get_task_raw_ioprio(struct task_struct *p)
  149. {
  150. int ret;
  151. ret = security_task_getioprio(p);
  152. if (ret)
  153. goto out;
  154. task_lock(p);
  155. if (p->io_context)
  156. ret = p->io_context->ioprio;
  157. else
  158. ret = IOPRIO_DEFAULT;
  159. task_unlock(p);
  160. out:
  161. return ret;
  162. }
  163. static int ioprio_best(unsigned short aprio, unsigned short bprio)
  164. {
  165. return min(aprio, bprio);
  166. }
  167. SYSCALL_DEFINE2(ioprio_get, int, which, int, who)
  168. {
  169. struct task_struct *g, *p;
  170. struct user_struct *user;
  171. struct pid *pgrp;
  172. kuid_t uid;
  173. int ret = -ESRCH;
  174. int tmpio;
  175. rcu_read_lock();
  176. switch (which) {
  177. case IOPRIO_WHO_PROCESS:
  178. if (!who)
  179. p = current;
  180. else
  181. p = find_task_by_vpid(who);
  182. if (p)
  183. ret = get_task_raw_ioprio(p);
  184. break;
  185. case IOPRIO_WHO_PGRP:
  186. if (!who)
  187. pgrp = task_pgrp(current);
  188. else
  189. pgrp = find_vpid(who);
  190. read_lock(&tasklist_lock);
  191. do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
  192. tmpio = get_task_ioprio(p);
  193. if (tmpio < 0)
  194. continue;
  195. if (ret == -ESRCH)
  196. ret = tmpio;
  197. else
  198. ret = ioprio_best(ret, tmpio);
  199. } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
  200. read_unlock(&tasklist_lock);
  201. break;
  202. case IOPRIO_WHO_USER:
  203. uid = make_kuid(current_user_ns(), who);
  204. if (!who)
  205. user = current_user();
  206. else
  207. user = find_user(uid);
  208. if (!user)
  209. break;
  210. for_each_process_thread(g, p) {
  211. if (!uid_eq(task_uid(p), user->uid) ||
  212. !task_pid_vnr(p))
  213. continue;
  214. tmpio = get_task_ioprio(p);
  215. if (tmpio < 0)
  216. continue;
  217. if (ret == -ESRCH)
  218. ret = tmpio;
  219. else
  220. ret = ioprio_best(ret, tmpio);
  221. }
  222. if (who)
  223. free_uid(user);
  224. break;
  225. default:
  226. ret = -EINVAL;
  227. }
  228. rcu_read_unlock();
  229. return ret;
  230. }