ioprio.c 5.1 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. static 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. asmlinkage long sys_ioprio_set(int which, int who, int ioprio)
  67. {
  68. int class = IOPRIO_PRIO_CLASS(ioprio);
  69. int data = IOPRIO_PRIO_DATA(ioprio);
  70. struct task_struct *p, *g;
  71. struct user_struct *user;
  72. struct pid *pgrp;
  73. int ret;
  74. switch (class) {
  75. case IOPRIO_CLASS_RT:
  76. if (!capable(CAP_SYS_ADMIN))
  77. return -EPERM;
  78. /* fall through, rt has prio field too */
  79. case IOPRIO_CLASS_BE:
  80. if (data >= IOPRIO_BE_NR || data < 0)
  81. return -EINVAL;
  82. break;
  83. case IOPRIO_CLASS_IDLE:
  84. break;
  85. case IOPRIO_CLASS_NONE:
  86. if (data)
  87. return -EINVAL;
  88. break;
  89. default:
  90. return -EINVAL;
  91. }
  92. ret = -ESRCH;
  93. /*
  94. * We want IOPRIO_WHO_PGRP/IOPRIO_WHO_USER to be "atomic",
  95. * so we can't use rcu_read_lock(). See re-copy of ->ioprio
  96. * in copy_process().
  97. */
  98. read_lock(&tasklist_lock);
  99. switch (which) {
  100. case IOPRIO_WHO_PROCESS:
  101. if (!who)
  102. p = current;
  103. else
  104. p = find_task_by_vpid(who);
  105. if (p)
  106. ret = set_task_ioprio(p, ioprio);
  107. break;
  108. case IOPRIO_WHO_PGRP:
  109. if (!who)
  110. pgrp = task_pgrp(current);
  111. else
  112. pgrp = find_vpid(who);
  113. do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
  114. ret = set_task_ioprio(p, ioprio);
  115. if (ret)
  116. break;
  117. } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
  118. break;
  119. case IOPRIO_WHO_USER:
  120. if (!who)
  121. user = current_user();
  122. else
  123. user = find_user(who);
  124. if (!user)
  125. break;
  126. do_each_thread(g, p) {
  127. if (__task_cred(p)->uid != who)
  128. continue;
  129. ret = set_task_ioprio(p, ioprio);
  130. if (ret)
  131. goto free_uid;
  132. } while_each_thread(g, p);
  133. free_uid:
  134. if (who)
  135. free_uid(user);
  136. break;
  137. default:
  138. ret = -EINVAL;
  139. }
  140. read_unlock(&tasklist_lock);
  141. return ret;
  142. }
  143. static int get_task_ioprio(struct task_struct *p)
  144. {
  145. int ret;
  146. ret = security_task_getioprio(p);
  147. if (ret)
  148. goto out;
  149. ret = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_NONE, IOPRIO_NORM);
  150. if (p->io_context)
  151. ret = p->io_context->ioprio;
  152. out:
  153. return ret;
  154. }
  155. int ioprio_best(unsigned short aprio, unsigned short bprio)
  156. {
  157. unsigned short aclass = IOPRIO_PRIO_CLASS(aprio);
  158. unsigned short bclass = IOPRIO_PRIO_CLASS(bprio);
  159. if (aclass == IOPRIO_CLASS_NONE)
  160. aclass = IOPRIO_CLASS_BE;
  161. if (bclass == IOPRIO_CLASS_NONE)
  162. bclass = IOPRIO_CLASS_BE;
  163. if (aclass == bclass)
  164. return min(aprio, bprio);
  165. if (aclass > bclass)
  166. return bprio;
  167. else
  168. return aprio;
  169. }
  170. asmlinkage long sys_ioprio_get(int which, int who)
  171. {
  172. struct task_struct *g, *p;
  173. struct user_struct *user;
  174. struct pid *pgrp;
  175. int ret = -ESRCH;
  176. int tmpio;
  177. read_lock(&tasklist_lock);
  178. switch (which) {
  179. case IOPRIO_WHO_PROCESS:
  180. if (!who)
  181. p = current;
  182. else
  183. p = find_task_by_vpid(who);
  184. if (p)
  185. ret = get_task_ioprio(p);
  186. break;
  187. case IOPRIO_WHO_PGRP:
  188. if (!who)
  189. pgrp = task_pgrp(current);
  190. else
  191. pgrp = find_vpid(who);
  192. do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
  193. tmpio = get_task_ioprio(p);
  194. if (tmpio < 0)
  195. continue;
  196. if (ret == -ESRCH)
  197. ret = tmpio;
  198. else
  199. ret = ioprio_best(ret, tmpio);
  200. } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
  201. break;
  202. case IOPRIO_WHO_USER:
  203. if (!who)
  204. user = current_user();
  205. else
  206. user = find_user(who);
  207. if (!user)
  208. break;
  209. do_each_thread(g, p) {
  210. if (__task_cred(p)->uid != user->uid)
  211. continue;
  212. tmpio = get_task_ioprio(p);
  213. if (tmpio < 0)
  214. continue;
  215. if (ret == -ESRCH)
  216. ret = tmpio;
  217. else
  218. ret = ioprio_best(ret, tmpio);
  219. } while_each_thread(g, p);
  220. if (who)
  221. free_uid(user);
  222. break;
  223. default:
  224. ret = -EINVAL;
  225. }
  226. read_unlock(&tasklist_lock);
  227. return ret;
  228. }