ioprio.c 5.3 KB

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