ioprio.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 = ioprio;
  50. ioc->ioprio_changed = 1;
  51. put_io_context(ioc);
  52. }
  53. return err;
  54. }
  55. EXPORT_SYMBOL_GPL(set_task_ioprio);
  56. SYSCALL_DEFINE3(ioprio_set, int, which, int, who, int, ioprio)
  57. {
  58. int class = IOPRIO_PRIO_CLASS(ioprio);
  59. int data = IOPRIO_PRIO_DATA(ioprio);
  60. struct task_struct *p, *g;
  61. struct user_struct *user;
  62. struct pid *pgrp;
  63. int ret;
  64. switch (class) {
  65. case IOPRIO_CLASS_RT:
  66. if (!capable(CAP_SYS_ADMIN))
  67. return -EPERM;
  68. /* fall through, rt has prio field too */
  69. case IOPRIO_CLASS_BE:
  70. if (data >= IOPRIO_BE_NR || data < 0)
  71. return -EINVAL;
  72. break;
  73. case IOPRIO_CLASS_IDLE:
  74. break;
  75. case IOPRIO_CLASS_NONE:
  76. if (data)
  77. return -EINVAL;
  78. break;
  79. default:
  80. return -EINVAL;
  81. }
  82. ret = -ESRCH;
  83. rcu_read_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_thread(pgrp, PIDTYPE_PGID, p) {
  99. ret = set_task_ioprio(p, ioprio);
  100. if (ret)
  101. break;
  102. } while_each_pid_thread(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 (__task_cred(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. rcu_read_unlock();
  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 = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_NONE, IOPRIO_NORM);
  135. if (p->io_context)
  136. ret = p->io_context->ioprio;
  137. out:
  138. return ret;
  139. }
  140. int ioprio_best(unsigned short aprio, unsigned short bprio)
  141. {
  142. unsigned short aclass = IOPRIO_PRIO_CLASS(aprio);
  143. unsigned short bclass = IOPRIO_PRIO_CLASS(bprio);
  144. if (aclass == IOPRIO_CLASS_NONE)
  145. aclass = IOPRIO_CLASS_BE;
  146. if (bclass == IOPRIO_CLASS_NONE)
  147. bclass = IOPRIO_CLASS_BE;
  148. if (aclass == bclass)
  149. return min(aprio, bprio);
  150. if (aclass > bclass)
  151. return bprio;
  152. else
  153. return aprio;
  154. }
  155. SYSCALL_DEFINE2(ioprio_get, int, which, int, who)
  156. {
  157. struct task_struct *g, *p;
  158. struct user_struct *user;
  159. struct pid *pgrp;
  160. int ret = -ESRCH;
  161. int tmpio;
  162. rcu_read_lock();
  163. switch (which) {
  164. case IOPRIO_WHO_PROCESS:
  165. if (!who)
  166. p = current;
  167. else
  168. p = find_task_by_vpid(who);
  169. if (p)
  170. ret = get_task_ioprio(p);
  171. break;
  172. case IOPRIO_WHO_PGRP:
  173. if (!who)
  174. pgrp = task_pgrp(current);
  175. else
  176. pgrp = find_vpid(who);
  177. do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
  178. tmpio = get_task_ioprio(p);
  179. if (tmpio < 0)
  180. continue;
  181. if (ret == -ESRCH)
  182. ret = tmpio;
  183. else
  184. ret = ioprio_best(ret, tmpio);
  185. } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
  186. break;
  187. case IOPRIO_WHO_USER:
  188. if (!who)
  189. user = current_user();
  190. else
  191. user = find_user(who);
  192. if (!user)
  193. break;
  194. do_each_thread(g, p) {
  195. if (__task_cred(p)->uid != user->uid)
  196. continue;
  197. tmpio = get_task_ioprio(p);
  198. if (tmpio < 0)
  199. continue;
  200. if (ret == -ESRCH)
  201. ret = tmpio;
  202. else
  203. ret = ioprio_best(ret, tmpio);
  204. } while_each_thread(g, p);
  205. if (who)
  206. free_uid(user);
  207. break;
  208. default:
  209. ret = -EINVAL;
  210. }
  211. rcu_read_unlock();
  212. return ret;
  213. }