ioprio.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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/ioprio.h>
  25. #include <linux/blkdev.h>
  26. #include <linux/capability.h>
  27. #include <linux/syscalls.h>
  28. #include <linux/security.h>
  29. #include <linux/pid_namespace.h>
  30. int set_task_ioprio(struct task_struct *task, int ioprio)
  31. {
  32. int err;
  33. struct io_context *ioc;
  34. const struct cred *cred = current_cred(), *tcred;
  35. rcu_read_lock();
  36. tcred = __task_cred(task);
  37. if (tcred->uid != cred->euid &&
  38. tcred->uid != cred->uid && !capable(CAP_SYS_NICE)) {
  39. rcu_read_unlock();
  40. return -EPERM;
  41. }
  42. rcu_read_unlock();
  43. err = security_task_setioprio(task, ioprio);
  44. if (err)
  45. return err;
  46. task_lock(task);
  47. do {
  48. ioc = task->io_context;
  49. /* see wmb() in current_io_context() */
  50. smp_read_barrier_depends();
  51. if (ioc)
  52. break;
  53. ioc = alloc_io_context(GFP_ATOMIC, -1);
  54. if (!ioc) {
  55. err = -ENOMEM;
  56. break;
  57. }
  58. task->io_context = ioc;
  59. } while (1);
  60. if (!err) {
  61. ioc->ioprio = ioprio;
  62. ioc->ioprio_changed = 1;
  63. }
  64. task_unlock(task);
  65. return err;
  66. }
  67. EXPORT_SYMBOL_GPL(set_task_ioprio);
  68. SYSCALL_DEFINE3(ioprio_set, int, which, int, who, int, ioprio)
  69. {
  70. int class = IOPRIO_PRIO_CLASS(ioprio);
  71. int data = IOPRIO_PRIO_DATA(ioprio);
  72. struct task_struct *p, *g;
  73. struct user_struct *user;
  74. struct pid *pgrp;
  75. int ret;
  76. switch (class) {
  77. case IOPRIO_CLASS_RT:
  78. if (!capable(CAP_SYS_ADMIN))
  79. return -EPERM;
  80. /* fall through, rt has prio field too */
  81. case IOPRIO_CLASS_BE:
  82. if (data >= IOPRIO_BE_NR || data < 0)
  83. return -EINVAL;
  84. break;
  85. case IOPRIO_CLASS_IDLE:
  86. break;
  87. case IOPRIO_CLASS_NONE:
  88. if (data)
  89. return -EINVAL;
  90. break;
  91. default:
  92. return -EINVAL;
  93. }
  94. ret = -ESRCH;
  95. /*
  96. * We want IOPRIO_WHO_PGRP/IOPRIO_WHO_USER to be "atomic",
  97. * so we can't use rcu_read_lock(). See re-copy of ->ioprio
  98. * in copy_process().
  99. */
  100. read_lock(&tasklist_lock);
  101. switch (which) {
  102. case IOPRIO_WHO_PROCESS:
  103. if (!who)
  104. p = current;
  105. else
  106. p = find_task_by_vpid(who);
  107. if (p)
  108. ret = set_task_ioprio(p, ioprio);
  109. break;
  110. case IOPRIO_WHO_PGRP:
  111. if (!who)
  112. pgrp = task_pgrp(current);
  113. else
  114. pgrp = find_vpid(who);
  115. do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
  116. ret = set_task_ioprio(p, ioprio);
  117. if (ret)
  118. break;
  119. } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
  120. break;
  121. case IOPRIO_WHO_USER:
  122. if (!who)
  123. user = current_user();
  124. else
  125. user = find_user(who);
  126. if (!user)
  127. break;
  128. do_each_thread(g, p) {
  129. if (__task_cred(p)->uid != who)
  130. continue;
  131. ret = set_task_ioprio(p, ioprio);
  132. if (ret)
  133. goto free_uid;
  134. } while_each_thread(g, p);
  135. free_uid:
  136. if (who)
  137. free_uid(user);
  138. break;
  139. default:
  140. ret = -EINVAL;
  141. }
  142. read_unlock(&tasklist_lock);
  143. return ret;
  144. }
  145. static int get_task_ioprio(struct task_struct *p)
  146. {
  147. int ret;
  148. ret = security_task_getioprio(p);
  149. if (ret)
  150. goto out;
  151. ret = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_NONE, IOPRIO_NORM);
  152. if (p->io_context)
  153. ret = p->io_context->ioprio;
  154. out:
  155. return ret;
  156. }
  157. int ioprio_best(unsigned short aprio, unsigned short bprio)
  158. {
  159. unsigned short aclass = IOPRIO_PRIO_CLASS(aprio);
  160. unsigned short bclass = IOPRIO_PRIO_CLASS(bprio);
  161. if (aclass == IOPRIO_CLASS_NONE)
  162. aclass = IOPRIO_CLASS_BE;
  163. if (bclass == IOPRIO_CLASS_NONE)
  164. bclass = IOPRIO_CLASS_BE;
  165. if (aclass == bclass)
  166. return min(aprio, bprio);
  167. if (aclass > bclass)
  168. return bprio;
  169. else
  170. return aprio;
  171. }
  172. SYSCALL_DEFINE2(ioprio_get, int, which, int, who)
  173. {
  174. struct task_struct *g, *p;
  175. struct user_struct *user;
  176. struct pid *pgrp;
  177. int ret = -ESRCH;
  178. int tmpio;
  179. read_lock(&tasklist_lock);
  180. switch (which) {
  181. case IOPRIO_WHO_PROCESS:
  182. if (!who)
  183. p = current;
  184. else
  185. p = find_task_by_vpid(who);
  186. if (p)
  187. ret = get_task_ioprio(p);
  188. break;
  189. case IOPRIO_WHO_PGRP:
  190. if (!who)
  191. pgrp = task_pgrp(current);
  192. else
  193. pgrp = find_vpid(who);
  194. do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
  195. tmpio = get_task_ioprio(p);
  196. if (tmpio < 0)
  197. continue;
  198. if (ret == -ESRCH)
  199. ret = tmpio;
  200. else
  201. ret = ioprio_best(ret, tmpio);
  202. } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
  203. break;
  204. case IOPRIO_WHO_USER:
  205. if (!who)
  206. user = current_user();
  207. else
  208. user = find_user(who);
  209. if (!user)
  210. break;
  211. do_each_thread(g, p) {
  212. if (__task_cred(p)->uid != user->uid)
  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. } while_each_thread(g, p);
  222. if (who)
  223. free_uid(user);
  224. break;
  225. default:
  226. ret = -EINVAL;
  227. }
  228. read_unlock(&tasklist_lock);
  229. return ret;
  230. }