autogroup.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. // SPDX-License-Identifier: GPL-2.0
  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_offline_group(ag->tg);
  33. sched_destroy_group(ag->tg);
  34. }
  35. static inline void autogroup_kref_put(struct autogroup *ag)
  36. {
  37. kref_put(&ag->kref, autogroup_destroy);
  38. }
  39. static inline struct autogroup *autogroup_kref_get(struct autogroup *ag)
  40. {
  41. kref_get(&ag->kref);
  42. return ag;
  43. }
  44. static inline struct autogroup *autogroup_task_get(struct task_struct *p)
  45. {
  46. struct autogroup *ag;
  47. unsigned long flags;
  48. if (!lock_task_sighand(p, &flags))
  49. return autogroup_kref_get(&autogroup_default);
  50. ag = autogroup_kref_get(p->signal->autogroup);
  51. unlock_task_sighand(p, &flags);
  52. return ag;
  53. }
  54. static inline struct autogroup *autogroup_create(void)
  55. {
  56. struct autogroup *ag = kzalloc(sizeof(*ag), GFP_KERNEL);
  57. struct task_group *tg;
  58. if (!ag)
  59. goto out_fail;
  60. tg = sched_create_group(&root_task_group);
  61. if (IS_ERR(tg))
  62. goto out_free;
  63. kref_init(&ag->kref);
  64. init_rwsem(&ag->lock);
  65. ag->id = atomic_inc_return(&autogroup_seq_nr);
  66. ag->tg = tg;
  67. #ifdef CONFIG_RT_GROUP_SCHED
  68. /*
  69. * Autogroup RT tasks are redirected to the root task group
  70. * so we don't have to move tasks around upon policy change,
  71. * or flail around trying to allocate bandwidth on the fly.
  72. * A bandwidth exception in __sched_setscheduler() allows
  73. * the policy change to proceed.
  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. sched_online_group(tg, &root_task_group);
  81. return ag;
  82. out_free:
  83. kfree(ag);
  84. out_fail:
  85. if (printk_ratelimit()) {
  86. printk(KERN_WARNING "autogroup_create: %s failure.\n",
  87. ag ? "sched_create_group()" : "kzalloc()");
  88. }
  89. return autogroup_kref_get(&autogroup_default);
  90. }
  91. bool task_wants_autogroup(struct task_struct *p, struct task_group *tg)
  92. {
  93. if (tg != &root_task_group)
  94. return false;
  95. /*
  96. * If we race with autogroup_move_group() the caller can use the old
  97. * value of signal->autogroup but in this case sched_move_task() will
  98. * be called again before autogroup_kref_put().
  99. *
  100. * However, there is no way sched_autogroup_exit_task() could tell us
  101. * to avoid autogroup->tg, so we abuse PF_EXITING flag for this case.
  102. */
  103. if (p->flags & PF_EXITING)
  104. return false;
  105. return true;
  106. }
  107. void sched_autogroup_exit_task(struct task_struct *p)
  108. {
  109. /*
  110. * We are going to call exit_notify() and autogroup_move_group() can't
  111. * see this thread after that: we can no longer use signal->autogroup.
  112. * See the PF_EXITING check in task_wants_autogroup().
  113. */
  114. sched_move_task(p);
  115. }
  116. static void
  117. autogroup_move_group(struct task_struct *p, struct autogroup *ag)
  118. {
  119. struct autogroup *prev;
  120. struct task_struct *t;
  121. unsigned long flags;
  122. BUG_ON(!lock_task_sighand(p, &flags));
  123. prev = p->signal->autogroup;
  124. if (prev == ag) {
  125. unlock_task_sighand(p, &flags);
  126. return;
  127. }
  128. p->signal->autogroup = autogroup_kref_get(ag);
  129. /*
  130. * We can't avoid sched_move_task() after we changed signal->autogroup,
  131. * this process can already run with task_group() == prev->tg or we can
  132. * race with cgroup code which can read autogroup = prev under rq->lock.
  133. * In the latter case for_each_thread() can not miss a migrating thread,
  134. * cpu_cgroup_attach() must not be possible after cgroup_exit() and it
  135. * can't be removed from thread list, we hold ->siglock.
  136. *
  137. * If an exiting thread was already removed from thread list we rely on
  138. * sched_autogroup_exit_task().
  139. */
  140. for_each_thread(p, t)
  141. sched_move_task(t);
  142. unlock_task_sighand(p, &flags);
  143. autogroup_kref_put(prev);
  144. }
  145. /* Allocates GFP_KERNEL, cannot be called under any spinlock */
  146. void sched_autogroup_create_attach(struct task_struct *p)
  147. {
  148. struct autogroup *ag = autogroup_create();
  149. autogroup_move_group(p, ag);
  150. /* drop extra reference added by autogroup_create() */
  151. autogroup_kref_put(ag);
  152. }
  153. EXPORT_SYMBOL(sched_autogroup_create_attach);
  154. /* Cannot be called under siglock. Currently has no users */
  155. void sched_autogroup_detach(struct task_struct *p)
  156. {
  157. autogroup_move_group(p, &autogroup_default);
  158. }
  159. EXPORT_SYMBOL(sched_autogroup_detach);
  160. void sched_autogroup_fork(struct signal_struct *sig)
  161. {
  162. sig->autogroup = autogroup_task_get(current);
  163. }
  164. void sched_autogroup_exit(struct signal_struct *sig)
  165. {
  166. autogroup_kref_put(sig->autogroup);
  167. }
  168. static int __init setup_autogroup(char *str)
  169. {
  170. sysctl_sched_autogroup_enabled = 0;
  171. return 1;
  172. }
  173. __setup("noautogroup", setup_autogroup);
  174. #ifdef CONFIG_PROC_FS
  175. int proc_sched_autogroup_set_nice(struct task_struct *p, int nice)
  176. {
  177. static unsigned long next = INITIAL_JIFFIES;
  178. struct autogroup *ag;
  179. unsigned long shares;
  180. int err, idx;
  181. if (nice < MIN_NICE || nice > MAX_NICE)
  182. return -EINVAL;
  183. err = security_task_setnice(current, nice);
  184. if (err)
  185. return err;
  186. if (nice < 0 && !can_nice(current, nice))
  187. return -EPERM;
  188. /* this is a heavy operation taking global locks.. */
  189. if (!capable(CAP_SYS_ADMIN) && time_before(jiffies, next))
  190. return -EAGAIN;
  191. next = HZ / 10 + jiffies;
  192. ag = autogroup_task_get(p);
  193. idx = array_index_nospec(nice + 20, 40);
  194. shares = scale_load(sched_prio_to_weight[idx]);
  195. down_write(&ag->lock);
  196. err = sched_group_set_shares(ag->tg, shares);
  197. if (!err)
  198. ag->nice = nice;
  199. up_write(&ag->lock);
  200. autogroup_kref_put(ag);
  201. return err;
  202. }
  203. void proc_sched_autogroup_show_task(struct task_struct *p, struct seq_file *m)
  204. {
  205. struct autogroup *ag = autogroup_task_get(p);
  206. if (!task_group_is_autogroup(ag->tg))
  207. goto out;
  208. down_read(&ag->lock);
  209. seq_printf(m, "/autogroup-%ld nice %d\n", ag->id, ag->nice);
  210. up_read(&ag->lock);
  211. out:
  212. autogroup_kref_put(ag);
  213. }
  214. #endif /* CONFIG_PROC_FS */
  215. int autogroup_path(struct task_group *tg, char *buf, int buflen)
  216. {
  217. if (!task_group_is_autogroup(tg))
  218. return 0;
  219. return snprintf(buf, buflen, "%s-%ld", "/autogroup", tg->autogroup->id);
  220. }