ioprio.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. int ret;
  55. switch (class) {
  56. case IOPRIO_CLASS_RT:
  57. if (!capable(CAP_SYS_ADMIN))
  58. return -EPERM;
  59. /* fall through, rt has prio field too */
  60. case IOPRIO_CLASS_BE:
  61. if (data >= IOPRIO_BE_NR || data < 0)
  62. return -EINVAL;
  63. break;
  64. case IOPRIO_CLASS_IDLE:
  65. if (!capable(CAP_SYS_ADMIN))
  66. return -EPERM;
  67. break;
  68. default:
  69. return -EINVAL;
  70. }
  71. ret = -ESRCH;
  72. /*
  73. * We want IOPRIO_WHO_PGRP/IOPRIO_WHO_USER to be "atomic",
  74. * so we can't use rcu_read_lock(). See re-copy of ->ioprio
  75. * in copy_process().
  76. */
  77. read_lock(&tasklist_lock);
  78. switch (which) {
  79. case IOPRIO_WHO_PROCESS:
  80. if (!who)
  81. p = current;
  82. else
  83. p = find_task_by_pid(who);
  84. if (p)
  85. ret = set_task_ioprio(p, ioprio);
  86. break;
  87. case IOPRIO_WHO_PGRP:
  88. if (!who)
  89. who = process_group(current);
  90. do_each_task_pid(who, PIDTYPE_PGID, p) {
  91. ret = set_task_ioprio(p, ioprio);
  92. if (ret)
  93. break;
  94. } while_each_task_pid(who, PIDTYPE_PGID, p);
  95. break;
  96. case IOPRIO_WHO_USER:
  97. if (!who)
  98. user = current->user;
  99. else
  100. user = find_user(who);
  101. if (!user)
  102. break;
  103. do_each_thread(g, p) {
  104. if (p->uid != who)
  105. continue;
  106. ret = set_task_ioprio(p, ioprio);
  107. if (ret)
  108. goto free_uid;
  109. } while_each_thread(g, p);
  110. free_uid:
  111. if (who)
  112. free_uid(user);
  113. break;
  114. default:
  115. ret = -EINVAL;
  116. }
  117. read_unlock(&tasklist_lock);
  118. return ret;
  119. }
  120. static int get_task_ioprio(struct task_struct *p)
  121. {
  122. int ret;
  123. ret = security_task_getioprio(p);
  124. if (ret)
  125. goto out;
  126. ret = p->ioprio;
  127. out:
  128. return ret;
  129. }
  130. int ioprio_best(unsigned short aprio, unsigned short bprio)
  131. {
  132. unsigned short aclass = IOPRIO_PRIO_CLASS(aprio);
  133. unsigned short bclass = IOPRIO_PRIO_CLASS(bprio);
  134. if (!ioprio_valid(aprio))
  135. return bprio;
  136. if (!ioprio_valid(bprio))
  137. return aprio;
  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. 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. who = process_group(current);
  168. do_each_task_pid(who, PIDTYPE_PGID, p) {
  169. tmpio = get_task_ioprio(p);
  170. if (tmpio < 0)
  171. continue;
  172. if (ret == -ESRCH)
  173. ret = tmpio;
  174. else
  175. ret = ioprio_best(ret, tmpio);
  176. } while_each_task_pid(who, PIDTYPE_PGID, p);
  177. break;
  178. case IOPRIO_WHO_USER:
  179. if (!who)
  180. user = current->user;
  181. else
  182. user = find_user(who);
  183. if (!user)
  184. break;
  185. do_each_thread(g, p) {
  186. if (p->uid != user->uid)
  187. continue;
  188. tmpio = get_task_ioprio(p);
  189. if (tmpio < 0)
  190. continue;
  191. if (ret == -ESRCH)
  192. ret = tmpio;
  193. else
  194. ret = ioprio_best(ret, tmpio);
  195. } while_each_thread(g, p);
  196. if (who)
  197. free_uid(user);
  198. break;
  199. default:
  200. ret = -EINVAL;
  201. }
  202. read_unlock(&tasklist_lock);
  203. return ret;
  204. }