ioprio.c 5.4 KB

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