ioprio.c 5.1 KB

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