ioprio.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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_changed(ioc, 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. continue;
  117. ret = set_task_ioprio(p, ioprio);
  118. if (ret)
  119. goto free_uid;
  120. } while_each_thread(g, p);
  121. free_uid:
  122. if (who)
  123. free_uid(user);
  124. break;
  125. default:
  126. ret = -EINVAL;
  127. }
  128. rcu_read_unlock();
  129. return ret;
  130. }
  131. static int get_task_ioprio(struct task_struct *p)
  132. {
  133. int ret;
  134. ret = security_task_getioprio(p);
  135. if (ret)
  136. goto out;
  137. ret = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_NONE, IOPRIO_NORM);
  138. task_lock(p);
  139. if (p->io_context)
  140. ret = p->io_context->ioprio;
  141. task_unlock(p);
  142. out:
  143. return ret;
  144. }
  145. int ioprio_best(unsigned short aprio, unsigned short bprio)
  146. {
  147. unsigned short aclass;
  148. unsigned short bclass;
  149. if (!ioprio_valid(aprio))
  150. aprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, IOPRIO_NORM);
  151. if (!ioprio_valid(bprio))
  152. bprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, IOPRIO_NORM);
  153. aclass = IOPRIO_PRIO_CLASS(aprio);
  154. bclass = IOPRIO_PRIO_CLASS(bprio);
  155. if (aclass == bclass)
  156. return min(aprio, bprio);
  157. if (aclass > bclass)
  158. return bprio;
  159. else
  160. return aprio;
  161. }
  162. SYSCALL_DEFINE2(ioprio_get, int, which, int, who)
  163. {
  164. struct task_struct *g, *p;
  165. struct user_struct *user;
  166. struct pid *pgrp;
  167. kuid_t uid;
  168. int ret = -ESRCH;
  169. int tmpio;
  170. rcu_read_lock();
  171. switch (which) {
  172. case IOPRIO_WHO_PROCESS:
  173. if (!who)
  174. p = current;
  175. else
  176. p = find_task_by_vpid(who);
  177. if (p)
  178. ret = get_task_ioprio(p);
  179. break;
  180. case IOPRIO_WHO_PGRP:
  181. if (!who)
  182. pgrp = task_pgrp(current);
  183. else
  184. pgrp = find_vpid(who);
  185. do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
  186. tmpio = get_task_ioprio(p);
  187. if (tmpio < 0)
  188. continue;
  189. if (ret == -ESRCH)
  190. ret = tmpio;
  191. else
  192. ret = ioprio_best(ret, tmpio);
  193. } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
  194. break;
  195. case IOPRIO_WHO_USER:
  196. uid = make_kuid(current_user_ns(), who);
  197. if (!who)
  198. user = current_user();
  199. else
  200. user = find_user(uid);
  201. if (!user)
  202. break;
  203. do_each_thread(g, p) {
  204. if (!uid_eq(task_uid(p), user->uid))
  205. continue;
  206. tmpio = get_task_ioprio(p);
  207. if (tmpio < 0)
  208. continue;
  209. if (ret == -ESRCH)
  210. ret = tmpio;
  211. else
  212. ret = ioprio_best(ret, tmpio);
  213. } while_each_thread(g, p);
  214. if (who)
  215. free_uid(user);
  216. break;
  217. default:
  218. ret = -EINVAL;
  219. }
  220. rcu_read_unlock();
  221. return ret;
  222. }