ioprio.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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/export.h>
  25. #include <linux/ioprio.h>
  26. #include <linux/blkdev.h>
  27. #include <linux/capability.h>
  28. #include <linux/syscalls.h>
  29. #include <linux/security.h>
  30. #include <linux/pid_namespace.h>
  31. int set_task_ioprio(struct task_struct *task, int ioprio)
  32. {
  33. int err;
  34. struct io_context *ioc;
  35. const struct cred *cred = current_cred(), *tcred;
  36. rcu_read_lock();
  37. tcred = __task_cred(task);
  38. if (tcred->uid != cred->euid &&
  39. tcred->uid != cred->uid && !capable(CAP_SYS_NICE)) {
  40. rcu_read_unlock();
  41. return -EPERM;
  42. }
  43. rcu_read_unlock();
  44. err = security_task_setioprio(task, ioprio);
  45. if (err)
  46. return err;
  47. ioc = get_task_io_context(task, GFP_ATOMIC, NUMA_NO_NODE);
  48. if (ioc) {
  49. ioc_ioprio_changed(ioc, ioprio);
  50. put_io_context(ioc);
  51. }
  52. return err;
  53. }
  54. EXPORT_SYMBOL_GPL(set_task_ioprio);
  55. SYSCALL_DEFINE3(ioprio_set, int, which, int, who, int, ioprio)
  56. {
  57. int class = IOPRIO_PRIO_CLASS(ioprio);
  58. int data = IOPRIO_PRIO_DATA(ioprio);
  59. struct task_struct *p, *g;
  60. struct user_struct *user;
  61. struct pid *pgrp;
  62. int ret;
  63. switch (class) {
  64. case IOPRIO_CLASS_RT:
  65. if (!capable(CAP_SYS_ADMIN))
  66. return -EPERM;
  67. /* fall through, rt has prio field too */
  68. case IOPRIO_CLASS_BE:
  69. if (data >= IOPRIO_BE_NR || data < 0)
  70. return -EINVAL;
  71. break;
  72. case IOPRIO_CLASS_IDLE:
  73. break;
  74. case IOPRIO_CLASS_NONE:
  75. if (data)
  76. return -EINVAL;
  77. break;
  78. default:
  79. return -EINVAL;
  80. }
  81. ret = -ESRCH;
  82. rcu_read_lock();
  83. switch (which) {
  84. case IOPRIO_WHO_PROCESS:
  85. if (!who)
  86. p = current;
  87. else
  88. p = find_task_by_vpid(who);
  89. if (p)
  90. ret = set_task_ioprio(p, ioprio);
  91. break;
  92. case IOPRIO_WHO_PGRP:
  93. if (!who)
  94. pgrp = task_pgrp(current);
  95. else
  96. pgrp = find_vpid(who);
  97. do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
  98. ret = set_task_ioprio(p, ioprio);
  99. if (ret)
  100. break;
  101. } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
  102. break;
  103. case IOPRIO_WHO_USER:
  104. if (!who)
  105. user = current_user();
  106. else
  107. user = find_user(who);
  108. if (!user)
  109. break;
  110. do_each_thread(g, p) {
  111. if (__task_cred(p)->uid != who)
  112. continue;
  113. ret = set_task_ioprio(p, ioprio);
  114. if (ret)
  115. goto free_uid;
  116. } while_each_thread(g, p);
  117. free_uid:
  118. if (who)
  119. free_uid(user);
  120. break;
  121. default:
  122. ret = -EINVAL;
  123. }
  124. rcu_read_unlock();
  125. return ret;
  126. }
  127. static int get_task_ioprio(struct task_struct *p)
  128. {
  129. int ret;
  130. ret = security_task_getioprio(p);
  131. if (ret)
  132. goto out;
  133. ret = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_NONE, IOPRIO_NORM);
  134. if (p->io_context)
  135. ret = p->io_context->ioprio;
  136. out:
  137. return ret;
  138. }
  139. int ioprio_best(unsigned short aprio, unsigned short bprio)
  140. {
  141. unsigned short aclass = IOPRIO_PRIO_CLASS(aprio);
  142. unsigned short bclass = IOPRIO_PRIO_CLASS(bprio);
  143. if (aclass == IOPRIO_CLASS_NONE)
  144. aclass = IOPRIO_CLASS_BE;
  145. if (bclass == IOPRIO_CLASS_NONE)
  146. bclass = IOPRIO_CLASS_BE;
  147. if (aclass == bclass)
  148. return min(aprio, bprio);
  149. if (aclass > bclass)
  150. return bprio;
  151. else
  152. return aprio;
  153. }
  154. SYSCALL_DEFINE2(ioprio_get, int, which, int, who)
  155. {
  156. struct task_struct *g, *p;
  157. struct user_struct *user;
  158. struct pid *pgrp;
  159. int ret = -ESRCH;
  160. int tmpio;
  161. rcu_read_lock();
  162. switch (which) {
  163. case IOPRIO_WHO_PROCESS:
  164. if (!who)
  165. p = current;
  166. else
  167. p = find_task_by_vpid(who);
  168. if (p)
  169. ret = get_task_ioprio(p);
  170. break;
  171. case IOPRIO_WHO_PGRP:
  172. if (!who)
  173. pgrp = task_pgrp(current);
  174. else
  175. pgrp = find_vpid(who);
  176. do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
  177. tmpio = get_task_ioprio(p);
  178. if (tmpio < 0)
  179. continue;
  180. if (ret == -ESRCH)
  181. ret = tmpio;
  182. else
  183. ret = ioprio_best(ret, tmpio);
  184. } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
  185. break;
  186. case IOPRIO_WHO_USER:
  187. if (!who)
  188. user = current_user();
  189. else
  190. user = find_user(who);
  191. if (!user)
  192. break;
  193. do_each_thread(g, p) {
  194. if (__task_cred(p)->uid != user->uid)
  195. continue;
  196. tmpio = get_task_ioprio(p);
  197. if (tmpio < 0)
  198. continue;
  199. if (ret == -ESRCH)
  200. ret = tmpio;
  201. else
  202. ret = ioprio_best(ret, tmpio);
  203. } while_each_thread(g, p);
  204. if (who)
  205. free_uid(user);
  206. break;
  207. default:
  208. ret = -EINVAL;
  209. }
  210. rcu_read_unlock();
  211. return ret;
  212. }