ioprio.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * fs/ioprio.c
  3. *
  4. * Copyright (C) 2004 Jens Axboe <axboe@suse.de>
  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 && ioc->set_ioprio)
  44. ioc->set_ioprio(ioc, ioprio);
  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. read_lock_irq(&tasklist_lock);
  73. switch (which) {
  74. case IOPRIO_WHO_PROCESS:
  75. if (!who)
  76. p = current;
  77. else
  78. p = find_task_by_pid(who);
  79. if (p)
  80. ret = set_task_ioprio(p, ioprio);
  81. break;
  82. case IOPRIO_WHO_PGRP:
  83. if (!who)
  84. who = process_group(current);
  85. do_each_task_pid(who, PIDTYPE_PGID, p) {
  86. ret = set_task_ioprio(p, ioprio);
  87. if (ret)
  88. break;
  89. } while_each_task_pid(who, PIDTYPE_PGID, p);
  90. break;
  91. case IOPRIO_WHO_USER:
  92. if (!who)
  93. user = current->user;
  94. else
  95. user = find_user(who);
  96. if (!user)
  97. break;
  98. do_each_thread(g, p) {
  99. if (p->uid != who)
  100. continue;
  101. ret = set_task_ioprio(p, ioprio);
  102. if (ret)
  103. goto free_uid;
  104. } while_each_thread(g, p);
  105. free_uid:
  106. if (who)
  107. free_uid(user);
  108. break;
  109. default:
  110. ret = -EINVAL;
  111. }
  112. read_unlock_irq(&tasklist_lock);
  113. return ret;
  114. }
  115. static int get_task_ioprio(struct task_struct *p)
  116. {
  117. int ret;
  118. ret = security_task_getioprio(p);
  119. if (ret)
  120. goto out;
  121. ret = p->ioprio;
  122. out:
  123. return ret;
  124. }
  125. int ioprio_best(unsigned short aprio, unsigned short bprio)
  126. {
  127. unsigned short aclass = IOPRIO_PRIO_CLASS(aprio);
  128. unsigned short bclass = IOPRIO_PRIO_CLASS(bprio);
  129. if (!ioprio_valid(aprio))
  130. return bprio;
  131. if (!ioprio_valid(bprio))
  132. return aprio;
  133. if (aclass == IOPRIO_CLASS_NONE)
  134. aclass = IOPRIO_CLASS_BE;
  135. if (bclass == IOPRIO_CLASS_NONE)
  136. bclass = IOPRIO_CLASS_BE;
  137. if (aclass == bclass)
  138. return min(aprio, bprio);
  139. if (aclass > bclass)
  140. return bprio;
  141. else
  142. return aprio;
  143. }
  144. asmlinkage long sys_ioprio_get(int which, int who)
  145. {
  146. struct task_struct *g, *p;
  147. struct user_struct *user;
  148. int ret = -ESRCH;
  149. int tmpio;
  150. read_lock_irq(&tasklist_lock);
  151. switch (which) {
  152. case IOPRIO_WHO_PROCESS:
  153. if (!who)
  154. p = current;
  155. else
  156. p = find_task_by_pid(who);
  157. if (p)
  158. ret = get_task_ioprio(p);
  159. break;
  160. case IOPRIO_WHO_PGRP:
  161. if (!who)
  162. who = process_group(current);
  163. do_each_task_pid(who, PIDTYPE_PGID, p) {
  164. tmpio = get_task_ioprio(p);
  165. if (tmpio < 0)
  166. continue;
  167. if (ret == -ESRCH)
  168. ret = tmpio;
  169. else
  170. ret = ioprio_best(ret, tmpio);
  171. } while_each_task_pid(who, PIDTYPE_PGID, p);
  172. break;
  173. case IOPRIO_WHO_USER:
  174. if (!who)
  175. user = current->user;
  176. else
  177. user = find_user(who);
  178. if (!user)
  179. break;
  180. do_each_thread(g, p) {
  181. if (p->uid != user->uid)
  182. continue;
  183. tmpio = get_task_ioprio(p);
  184. if (tmpio < 0)
  185. continue;
  186. if (ret == -ESRCH)
  187. ret = tmpio;
  188. else
  189. ret = ioprio_best(ret, tmpio);
  190. } while_each_thread(g, p);
  191. if (who)
  192. free_uid(user);
  193. break;
  194. default:
  195. ret = -EINVAL;
  196. }
  197. read_unlock_irq(&tasklist_lock);
  198. return ret;
  199. }