autogroup.c 7.0 KB

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