autogroup.c 6.4 KB

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