ioprio.c 5.0 KB

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