ioprio.c 4.8 KB

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