ioprio.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 (aclass == IOPRIO_CLASS_NONE)
  135. aclass = IOPRIO_CLASS_BE;
  136. if (bclass == IOPRIO_CLASS_NONE)
  137. bclass = IOPRIO_CLASS_BE;
  138. if (aclass == bclass)
  139. return min(aprio, bprio);
  140. if (aclass > bclass)
  141. return bprio;
  142. else
  143. return aprio;
  144. }
  145. asmlinkage long sys_ioprio_get(int which, int who)
  146. {
  147. struct task_struct *g, *p;
  148. struct user_struct *user;
  149. int ret = -ESRCH;
  150. int tmpio;
  151. read_lock(&tasklist_lock);
  152. switch (which) {
  153. case IOPRIO_WHO_PROCESS:
  154. if (!who)
  155. p = current;
  156. else
  157. p = find_task_by_pid(who);
  158. if (p)
  159. ret = get_task_ioprio(p);
  160. break;
  161. case IOPRIO_WHO_PGRP:
  162. if (!who)
  163. who = process_group(current);
  164. do_each_task_pid(who, PIDTYPE_PGID, p) {
  165. tmpio = get_task_ioprio(p);
  166. if (tmpio < 0)
  167. continue;
  168. if (ret == -ESRCH)
  169. ret = tmpio;
  170. else
  171. ret = ioprio_best(ret, tmpio);
  172. } while_each_task_pid(who, PIDTYPE_PGID, p);
  173. break;
  174. case IOPRIO_WHO_USER:
  175. if (!who)
  176. user = current->user;
  177. else
  178. user = find_user(who);
  179. if (!user)
  180. break;
  181. do_each_thread(g, p) {
  182. if (p->uid != user->uid)
  183. continue;
  184. tmpio = get_task_ioprio(p);
  185. if (tmpio < 0)
  186. continue;
  187. if (ret == -ESRCH)
  188. ret = tmpio;
  189. else
  190. ret = ioprio_best(ret, tmpio);
  191. } while_each_thread(g, p);
  192. if (who)
  193. free_uid(user);
  194. break;
  195. default:
  196. ret = -EINVAL;
  197. }
  198. read_unlock(&tasklist_lock);
  199. return ret;
  200. }