ioprio.c 5.0 KB

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