ioprio.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. task_lock(p);
  135. if (p->io_context)
  136. ret = p->io_context->ioprio;
  137. task_unlock(p);
  138. out:
  139. return ret;
  140. }
  141. int ioprio_best(unsigned short aprio, unsigned short bprio)
  142. {
  143. unsigned short aclass;
  144. unsigned short bclass;
  145. if (!ioprio_valid(aprio))
  146. aprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, IOPRIO_NORM);
  147. if (!ioprio_valid(bprio))
  148. bprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, IOPRIO_NORM);
  149. aclass = IOPRIO_PRIO_CLASS(aprio);
  150. bclass = IOPRIO_PRIO_CLASS(bprio);
  151. if (aclass == bclass)
  152. return min(aprio, bprio);
  153. if (aclass > bclass)
  154. return bprio;
  155. else
  156. return aprio;
  157. }
  158. SYSCALL_DEFINE2(ioprio_get, int, which, int, who)
  159. {
  160. struct task_struct *g, *p;
  161. struct user_struct *user;
  162. struct pid *pgrp;
  163. int ret = -ESRCH;
  164. int tmpio;
  165. rcu_read_lock();
  166. switch (which) {
  167. case IOPRIO_WHO_PROCESS:
  168. if (!who)
  169. p = current;
  170. else
  171. p = find_task_by_vpid(who);
  172. if (p)
  173. ret = get_task_ioprio(p);
  174. break;
  175. case IOPRIO_WHO_PGRP:
  176. if (!who)
  177. pgrp = task_pgrp(current);
  178. else
  179. pgrp = find_vpid(who);
  180. do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
  181. tmpio = get_task_ioprio(p);
  182. if (tmpio < 0)
  183. continue;
  184. if (ret == -ESRCH)
  185. ret = tmpio;
  186. else
  187. ret = ioprio_best(ret, tmpio);
  188. } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
  189. break;
  190. case IOPRIO_WHO_USER:
  191. if (!who)
  192. user = current_user();
  193. else
  194. user = find_user(who);
  195. if (!user)
  196. break;
  197. do_each_thread(g, p) {
  198. if (__task_cred(p)->uid != user->uid)
  199. continue;
  200. tmpio = get_task_ioprio(p);
  201. if (tmpio < 0)
  202. continue;
  203. if (ret == -ESRCH)
  204. ret = tmpio;
  205. else
  206. ret = ioprio_best(ret, tmpio);
  207. } while_each_thread(g, p);
  208. if (who)
  209. free_uid(user);
  210. break;
  211. default:
  212. ret = -EINVAL;
  213. }
  214. rcu_read_unlock();
  215. return ret;
  216. }