ioprio.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. default:
  71. return -EINVAL;
  72. }
  73. ret = -ESRCH;
  74. /*
  75. * We want IOPRIO_WHO_PGRP/IOPRIO_WHO_USER to be "atomic",
  76. * so we can't use rcu_read_lock(). See re-copy of ->ioprio
  77. * in copy_process().
  78. */
  79. read_lock(&tasklist_lock);
  80. switch (which) {
  81. case IOPRIO_WHO_PROCESS:
  82. if (!who)
  83. p = current;
  84. else
  85. p = find_task_by_vpid(who);
  86. if (p)
  87. ret = set_task_ioprio(p, ioprio);
  88. break;
  89. case IOPRIO_WHO_PGRP:
  90. if (!who)
  91. pgrp = task_pgrp(current);
  92. else
  93. pgrp = find_vpid(who);
  94. do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
  95. ret = set_task_ioprio(p, ioprio);
  96. if (ret)
  97. break;
  98. } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
  99. break;
  100. case IOPRIO_WHO_USER:
  101. if (!who)
  102. user = current->user;
  103. else
  104. user = find_user(who);
  105. if (!user)
  106. break;
  107. do_each_thread(g, p) {
  108. if (p->uid != who)
  109. continue;
  110. ret = set_task_ioprio(p, ioprio);
  111. if (ret)
  112. goto free_uid;
  113. } while_each_thread(g, p);
  114. free_uid:
  115. if (who)
  116. free_uid(user);
  117. break;
  118. default:
  119. ret = -EINVAL;
  120. }
  121. read_unlock(&tasklist_lock);
  122. return ret;
  123. }
  124. static int get_task_ioprio(struct task_struct *p)
  125. {
  126. int ret;
  127. ret = security_task_getioprio(p);
  128. if (ret)
  129. goto out;
  130. ret = p->ioprio;
  131. out:
  132. return ret;
  133. }
  134. int ioprio_best(unsigned short aprio, unsigned short bprio)
  135. {
  136. unsigned short aclass = IOPRIO_PRIO_CLASS(aprio);
  137. unsigned short bclass = IOPRIO_PRIO_CLASS(bprio);
  138. if (aclass == IOPRIO_CLASS_NONE)
  139. aclass = IOPRIO_CLASS_BE;
  140. if (bclass == IOPRIO_CLASS_NONE)
  141. bclass = IOPRIO_CLASS_BE;
  142. if (aclass == bclass)
  143. return min(aprio, bprio);
  144. if (aclass > bclass)
  145. return bprio;
  146. else
  147. return aprio;
  148. }
  149. asmlinkage long sys_ioprio_get(int which, int who)
  150. {
  151. struct task_struct *g, *p;
  152. struct user_struct *user;
  153. struct pid *pgrp;
  154. int ret = -ESRCH;
  155. int tmpio;
  156. read_lock(&tasklist_lock);
  157. switch (which) {
  158. case IOPRIO_WHO_PROCESS:
  159. if (!who)
  160. p = current;
  161. else
  162. p = find_task_by_vpid(who);
  163. if (p)
  164. ret = get_task_ioprio(p);
  165. break;
  166. case IOPRIO_WHO_PGRP:
  167. if (!who)
  168. pgrp = task_pgrp(current);
  169. else
  170. pgrp = find_vpid(who);
  171. do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
  172. tmpio = get_task_ioprio(p);
  173. if (tmpio < 0)
  174. continue;
  175. if (ret == -ESRCH)
  176. ret = tmpio;
  177. else
  178. ret = ioprio_best(ret, tmpio);
  179. } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
  180. break;
  181. case IOPRIO_WHO_USER:
  182. if (!who)
  183. user = current->user;
  184. else
  185. user = find_user(who);
  186. if (!user)
  187. break;
  188. do_each_thread(g, p) {
  189. if (p->uid != user->uid)
  190. continue;
  191. tmpio = get_task_ioprio(p);
  192. if (tmpio < 0)
  193. continue;
  194. if (ret == -ESRCH)
  195. ret = tmpio;
  196. else
  197. ret = ioprio_best(ret, tmpio);
  198. } while_each_thread(g, p);
  199. if (who)
  200. free_uid(user);
  201. break;
  202. default:
  203. ret = -EINVAL;
  204. }
  205. read_unlock(&tasklist_lock);
  206. return ret;
  207. }