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