ioprio.c 4.7 KB

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