auto_group.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. #ifdef CONFIG_SCHED_AUTOGROUP
  2. #include "sched.h"
  3. #include <linux/proc_fs.h>
  4. #include <linux/seq_file.h>
  5. #include <linux/kallsyms.h>
  6. #include <linux/utsname.h>
  7. #include <linux/security.h>
  8. #include <linux/export.h>
  9. #include <linux/nospec.h>
  10. unsigned int __read_mostly sysctl_sched_autogroup_enabled = 1;
  11. static struct autogroup autogroup_default;
  12. static atomic_t autogroup_seq_nr;
  13. void __init autogroup_init(struct task_struct *init_task)
  14. {
  15. autogroup_default.tg = &root_task_group;
  16. kref_init(&autogroup_default.kref);
  17. init_rwsem(&autogroup_default.lock);
  18. init_task->signal->autogroup = &autogroup_default;
  19. }
  20. void autogroup_free(struct task_group *tg)
  21. {
  22. kfree(tg->autogroup);
  23. }
  24. static inline void autogroup_destroy(struct kref *kref)
  25. {
  26. struct autogroup *ag = container_of(kref, struct autogroup, kref);
  27. #ifdef CONFIG_RT_GROUP_SCHED
  28. /* We've redirected RT tasks to the root task group... */
  29. ag->tg->rt_se = NULL;
  30. ag->tg->rt_rq = NULL;
  31. #endif
  32. sched_destroy_group(ag->tg);
  33. }
  34. static inline void autogroup_kref_put(struct autogroup *ag)
  35. {
  36. kref_put(&ag->kref, autogroup_destroy);
  37. }
  38. static inline struct autogroup *autogroup_kref_get(struct autogroup *ag)
  39. {
  40. kref_get(&ag->kref);
  41. return ag;
  42. }
  43. static inline struct autogroup *autogroup_task_get(struct task_struct *p)
  44. {
  45. struct autogroup *ag;
  46. unsigned long flags;
  47. if (!lock_task_sighand(p, &flags))
  48. return autogroup_kref_get(&autogroup_default);
  49. ag = autogroup_kref_get(p->signal->autogroup);
  50. unlock_task_sighand(p, &flags);
  51. return ag;
  52. }
  53. static inline struct autogroup *autogroup_create(void)
  54. {
  55. struct autogroup *ag = kzalloc(sizeof(*ag), GFP_KERNEL);
  56. struct task_group *tg;
  57. if (!ag)
  58. goto out_fail;
  59. tg = sched_create_group(&root_task_group);
  60. if (IS_ERR(tg))
  61. goto out_free;
  62. kref_init(&ag->kref);
  63. init_rwsem(&ag->lock);
  64. ag->id = atomic_inc_return(&autogroup_seq_nr);
  65. ag->tg = tg;
  66. #ifdef CONFIG_RT_GROUP_SCHED
  67. /*
  68. * Autogroup RT tasks are redirected to the root task group
  69. * so we don't have to move tasks around upon policy change,
  70. * or flail around trying to allocate bandwidth on the fly.
  71. * A bandwidth exception in __sched_setscheduler() allows
  72. * the policy change to proceed. Thereafter, task_group()
  73. * returns &root_task_group, so zero bandwidth is required.
  74. */
  75. free_rt_sched_group(tg);
  76. tg->rt_se = root_task_group.rt_se;
  77. tg->rt_rq = root_task_group.rt_rq;
  78. #endif
  79. tg->autogroup = ag;
  80. return ag;
  81. out_free:
  82. kfree(ag);
  83. out_fail:
  84. if (printk_ratelimit()) {
  85. printk(KERN_WARNING "autogroup_create: %s failure.\n",
  86. ag ? "sched_create_group()" : "kmalloc()");
  87. }
  88. return autogroup_kref_get(&autogroup_default);
  89. }
  90. bool task_wants_autogroup(struct task_struct *p, struct task_group *tg)
  91. {
  92. if (tg != &root_task_group)
  93. return false;
  94. if (p->sched_class != &fair_sched_class)
  95. return false;
  96. /*
  97. * We can only assume the task group can't go away on us if
  98. * autogroup_move_group() can see us on ->thread_group list.
  99. */
  100. if (p->flags & PF_EXITING)
  101. return false;
  102. return true;
  103. }
  104. static void
  105. autogroup_move_group(struct task_struct *p, struct autogroup *ag)
  106. {
  107. struct autogroup *prev;
  108. struct task_struct *t;
  109. unsigned long flags;
  110. BUG_ON(!lock_task_sighand(p, &flags));
  111. prev = p->signal->autogroup;
  112. if (prev == ag) {
  113. unlock_task_sighand(p, &flags);
  114. return;
  115. }
  116. p->signal->autogroup = autogroup_kref_get(ag);
  117. t = p;
  118. do {
  119. sched_move_task(t);
  120. } while_each_thread(p, t);
  121. unlock_task_sighand(p, &flags);
  122. autogroup_kref_put(prev);
  123. }
  124. /* Allocates GFP_KERNEL, cannot be called under any spinlock */
  125. void sched_autogroup_create_attach(struct task_struct *p)
  126. {
  127. struct autogroup *ag = autogroup_create();
  128. autogroup_move_group(p, ag);
  129. /* drop extra reference added by autogroup_create() */
  130. autogroup_kref_put(ag);
  131. }
  132. EXPORT_SYMBOL(sched_autogroup_create_attach);
  133. /* Cannot be called under siglock. Currently has no users */
  134. void sched_autogroup_detach(struct task_struct *p)
  135. {
  136. autogroup_move_group(p, &autogroup_default);
  137. }
  138. EXPORT_SYMBOL(sched_autogroup_detach);
  139. void sched_autogroup_fork(struct signal_struct *sig)
  140. {
  141. sig->autogroup = autogroup_task_get(current);
  142. }
  143. void sched_autogroup_exit(struct signal_struct *sig)
  144. {
  145. autogroup_kref_put(sig->autogroup);
  146. }
  147. static int __init setup_autogroup(char *str)
  148. {
  149. sysctl_sched_autogroup_enabled = 0;
  150. return 1;
  151. }
  152. __setup("noautogroup", setup_autogroup);
  153. #ifdef CONFIG_PROC_FS
  154. int proc_sched_autogroup_set_nice(struct task_struct *p, int nice)
  155. {
  156. static unsigned long next = INITIAL_JIFFIES;
  157. struct autogroup *ag;
  158. unsigned long shares;
  159. int err, idx;
  160. if (nice < -20 || nice > 19)
  161. return -EINVAL;
  162. err = security_task_setnice(current, nice);
  163. if (err)
  164. return err;
  165. if (nice < 0 && !can_nice(current, nice))
  166. return -EPERM;
  167. /* this is a heavy operation taking global locks.. */
  168. if (!capable(CAP_SYS_ADMIN) && time_before(jiffies, next))
  169. return -EAGAIN;
  170. next = HZ / 10 + jiffies;
  171. ag = autogroup_task_get(p);
  172. idx = array_index_nospec(nice + 20, 40);
  173. shares = scale_load(prio_to_weight[idx]);
  174. down_write(&ag->lock);
  175. err = sched_group_set_shares(ag->tg, shares);
  176. if (!err)
  177. ag->nice = nice;
  178. up_write(&ag->lock);
  179. autogroup_kref_put(ag);
  180. return err;
  181. }
  182. void proc_sched_autogroup_show_task(struct task_struct *p, struct seq_file *m)
  183. {
  184. struct autogroup *ag = autogroup_task_get(p);
  185. if (!task_group_is_autogroup(ag->tg))
  186. goto out;
  187. down_read(&ag->lock);
  188. seq_printf(m, "/autogroup-%ld nice %d\n", ag->id, ag->nice);
  189. up_read(&ag->lock);
  190. out:
  191. autogroup_kref_put(ag);
  192. }
  193. #endif /* CONFIG_PROC_FS */
  194. #ifdef CONFIG_SCHED_DEBUG
  195. int autogroup_path(struct task_group *tg, char *buf, int buflen)
  196. {
  197. if (!task_group_is_autogroup(tg))
  198. return 0;
  199. return snprintf(buf, buflen, "%s-%ld", "/autogroup", tg->autogroup->id);
  200. }
  201. #endif /* CONFIG_SCHED_DEBUG */
  202. #endif /* CONFIG_SCHED_AUTOGROUP */