ioprio.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * fs/ioprio.c
  3. *
  4. * Copyright (C) 2004 Jens Axboe <axboe@kernel.dk>
  5. *
  6. * Helper functions for setting/querying io priorities of processes. The
  7. * system calls closely mimmick getpriority/setpriority, see the man page for
  8. * those. The prio argument is a composite of prio class and prio data, where
  9. * the data argument has meaning within that class. The standard scheduling
  10. * classes have 8 distinct prio levels, with 0 being the highest prio and 7
  11. * being the lowest.
  12. *
  13. * IOW, setting BE scheduling class with prio 2 is done ala:
  14. *
  15. * unsigned int prio = (IOPRIO_CLASS_BE << IOPRIO_CLASS_SHIFT) | 2;
  16. *
  17. * ioprio_set(PRIO_PROCESS, pid, prio);
  18. *
  19. * See also Documentation/block/ioprio.txt
  20. *
  21. */
  22. #include <linux/gfp.h>
  23. #include <linux/kernel.h>
  24. #include <linux/export.h>
  25. #include <linux/ioprio.h>
  26. #include <linux/blkdev.h>
  27. #include <linux/capability.h>
  28. #include <linux/syscalls.h>
  29. #include <linux/security.h>
  30. #include <linux/pid_namespace.h>
  31. int set_task_ioprio(struct task_struct *task, int ioprio)
  32. {
  33. int err;
  34. struct io_context *ioc;
  35. const struct cred *cred = current_cred(), *tcred;
  36. rcu_read_lock();
  37. tcred = __task_cred(task);
  38. if (!uid_eq(tcred->uid, cred->euid) &&
  39. !uid_eq(tcred->uid, cred->uid) && !capable(CAP_SYS_NICE)) {
  40. rcu_read_unlock();
  41. return -EPERM;
  42. }
  43. rcu_read_unlock();
  44. err = security_task_setioprio(task, ioprio);
  45. if (err)
  46. return err;
  47. ioc = get_task_io_context(task, GFP_ATOMIC, NUMA_NO_NODE);
  48. if (ioc) {
  49. ioc->ioprio = ioprio;
  50. put_io_context(ioc);
  51. }
  52. return err;
  53. }
  54. EXPORT_SYMBOL_GPL(set_task_ioprio);
  55. SYSCALL_DEFINE3(ioprio_set, int, which, int, who, int, ioprio)
  56. {
  57. int class = IOPRIO_PRIO_CLASS(ioprio);
  58. int data = IOPRIO_PRIO_DATA(ioprio);
  59. struct task_struct *p, *g;
  60. struct user_struct *user;
  61. struct pid *pgrp;
  62. kuid_t uid;
  63. int ret;
  64. switch (class) {
  65. case IOPRIO_CLASS_RT:
  66. if (!capable(CAP_SYS_ADMIN))
  67. return -EPERM;
  68. /* fall through, rt has prio field too */
  69. case IOPRIO_CLASS_BE:
  70. if (data >= IOPRIO_BE_NR || data < 0)
  71. return -EINVAL;
  72. break;
  73. case IOPRIO_CLASS_IDLE:
  74. break;
  75. case IOPRIO_CLASS_NONE:
  76. if (data)
  77. return -EINVAL;
  78. break;
  79. default:
  80. return -EINVAL;
  81. }
  82. ret = -ESRCH;
  83. rcu_read_lock();
  84. switch (which) {
  85. case IOPRIO_WHO_PROCESS:
  86. if (!who)
  87. p = current;
  88. else
  89. p = find_task_by_vpid(who);
  90. if (p)
  91. ret = set_task_ioprio(p, ioprio);
  92. break;
  93. case IOPRIO_WHO_PGRP:
  94. if (!who)
  95. pgrp = task_pgrp(current);
  96. else
  97. pgrp = find_vpid(who);
  98. do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
  99. ret = set_task_ioprio(p, ioprio);
  100. if (ret)
  101. break;
  102. } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
  103. break;
  104. case IOPRIO_WHO_USER:
  105. uid = make_kuid(current_user_ns(), who);
  106. if (!uid_valid(uid))
  107. break;
  108. if (!who)
  109. user = current_user();
  110. else
  111. user = find_user(uid);
  112. if (!user)
  113. break;
  114. do_each_thread(g, p) {
  115. if (!uid_eq(task_uid(p), uid) ||
  116. !task_pid_vnr(p))
  117. continue;
  118. ret = set_task_ioprio(p, ioprio);
  119. if (ret)
  120. goto free_uid;
  121. } while_each_thread(g, p);
  122. free_uid:
  123. if (who)
  124. free_uid(user);
  125. break;
  126. default:
  127. ret = -EINVAL;
  128. }
  129. rcu_read_unlock();
  130. return ret;
  131. }
  132. static int get_task_ioprio(struct task_struct *p)
  133. {
  134. int ret;
  135. ret = security_task_getioprio(p);
  136. if (ret)
  137. goto out;
  138. ret = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_NONE, IOPRIO_NORM);
  139. task_lock(p);
  140. if (p->io_context)
  141. ret = p->io_context->ioprio;
  142. task_unlock(p);
  143. out:
  144. return ret;
  145. }
  146. int ioprio_best(unsigned short aprio, unsigned short bprio)
  147. {
  148. unsigned short aclass;
  149. unsigned short bclass;
  150. if (!ioprio_valid(aprio))
  151. aprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, IOPRIO_NORM);
  152. if (!ioprio_valid(bprio))
  153. bprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, IOPRIO_NORM);
  154. aclass = IOPRIO_PRIO_CLASS(aprio);
  155. bclass = IOPRIO_PRIO_CLASS(bprio);
  156. if (aclass == bclass)
  157. return min(aprio, bprio);
  158. if (aclass > bclass)
  159. return bprio;
  160. else
  161. return aprio;
  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_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. do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
  187. tmpio = get_task_ioprio(p);
  188. if (tmpio < 0)
  189. continue;
  190. if (ret == -ESRCH)
  191. ret = tmpio;
  192. else
  193. ret = ioprio_best(ret, tmpio);
  194. } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
  195. break;
  196. case IOPRIO_WHO_USER:
  197. uid = make_kuid(current_user_ns(), who);
  198. if (!who)
  199. user = current_user();
  200. else
  201. user = find_user(uid);
  202. if (!user)
  203. break;
  204. do_each_thread(g, p) {
  205. if (!uid_eq(task_uid(p), user->uid) ||
  206. !task_pid_vnr(p))
  207. continue;
  208. tmpio = get_task_ioprio(p);
  209. if (tmpio < 0)
  210. continue;
  211. if (ret == -ESRCH)
  212. ret = tmpio;
  213. else
  214. ret = ioprio_best(ret, tmpio);
  215. } while_each_thread(g, p);
  216. if (who)
  217. free_uid(user);
  218. break;
  219. default:
  220. ret = -EINVAL;
  221. }
  222. rcu_read_unlock();
  223. return ret;
  224. }