ioprio.c 5.2 KB

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