1 // SPDX-License-Identifier: GPL-2.0
4 * Auto-group scheduling implementation:
7 unsigned int __read_mostly sysctl_sched_autogroup_enabled = 1;
8 static struct autogroup autogroup_default;
9 static atomic_t autogroup_seq_nr;
12 static struct ctl_table sched_autogroup_sysctls[] = {
14 .procname = "sched_autogroup_enabled",
15 .data = &sysctl_sched_autogroup_enabled,
16 .maxlen = sizeof(unsigned int),
18 .proc_handler = proc_dointvec_minmax,
19 .extra1 = SYSCTL_ZERO,
25 static void __init sched_autogroup_sysctl_init(void)
27 register_sysctl_init("kernel", sched_autogroup_sysctls);
30 #define sched_autogroup_sysctl_init() do { } while (0)
33 void __init autogroup_init(struct task_struct *init_task)
35 autogroup_default.tg = &root_task_group;
36 kref_init(&autogroup_default.kref);
37 init_rwsem(&autogroup_default.lock);
38 init_task->signal->autogroup = &autogroup_default;
39 sched_autogroup_sysctl_init();
42 void autogroup_free(struct task_group *tg)
47 static inline void autogroup_destroy(struct kref *kref)
49 struct autogroup *ag = container_of(kref, struct autogroup, kref);
51 #ifdef CONFIG_RT_GROUP_SCHED
52 /* We've redirected RT tasks to the root task group... */
56 sched_release_group(ag->tg);
57 sched_destroy_group(ag->tg);
60 static inline void autogroup_kref_put(struct autogroup *ag)
62 kref_put(&ag->kref, autogroup_destroy);
65 static inline struct autogroup *autogroup_kref_get(struct autogroup *ag)
71 static inline struct autogroup *autogroup_task_get(struct task_struct *p)
76 if (!lock_task_sighand(p, &flags))
77 return autogroup_kref_get(&autogroup_default);
79 ag = autogroup_kref_get(p->signal->autogroup);
80 unlock_task_sighand(p, &flags);
85 static inline struct autogroup *autogroup_create(void)
87 struct autogroup *ag = kzalloc(sizeof(*ag), GFP_KERNEL);
88 struct task_group *tg;
93 tg = sched_create_group(&root_task_group);
98 init_rwsem(&ag->lock);
99 ag->id = atomic_inc_return(&autogroup_seq_nr);
101 #ifdef CONFIG_RT_GROUP_SCHED
103 * Autogroup RT tasks are redirected to the root task group
104 * so we don't have to move tasks around upon policy change,
105 * or flail around trying to allocate bandwidth on the fly.
106 * A bandwidth exception in __sched_setscheduler() allows
107 * the policy change to proceed.
109 free_rt_sched_group(tg);
110 tg->rt_se = root_task_group.rt_se;
111 tg->rt_rq = root_task_group.rt_rq;
115 sched_online_group(tg, &root_task_group);
121 if (printk_ratelimit()) {
122 printk(KERN_WARNING "autogroup_create: %s failure.\n",
123 ag ? "sched_create_group()" : "kzalloc()");
126 return autogroup_kref_get(&autogroup_default);
129 bool task_wants_autogroup(struct task_struct *p, struct task_group *tg)
131 if (tg != &root_task_group)
134 * If we race with autogroup_move_group() the caller can use the old
135 * value of signal->autogroup but in this case sched_move_task() will
136 * be called again before autogroup_kref_put().
138 * However, there is no way sched_autogroup_exit_task() could tell us
139 * to avoid autogroup->tg, so we abuse PF_EXITING flag for this case.
141 if (p->flags & PF_EXITING)
147 void sched_autogroup_exit_task(struct task_struct *p)
150 * We are going to call exit_notify() and autogroup_move_group() can't
151 * see this thread after that: we can no longer use signal->autogroup.
152 * See the PF_EXITING check in task_wants_autogroup().
158 autogroup_move_group(struct task_struct *p, struct autogroup *ag)
160 struct autogroup *prev;
161 struct task_struct *t;
164 if (WARN_ON_ONCE(!lock_task_sighand(p, &flags)))
167 prev = p->signal->autogroup;
169 unlock_task_sighand(p, &flags);
173 p->signal->autogroup = autogroup_kref_get(ag);
175 * We can't avoid sched_move_task() after we changed signal->autogroup,
176 * this process can already run with task_group() == prev->tg or we can
177 * race with cgroup code which can read autogroup = prev under rq->lock.
178 * In the latter case for_each_thread() can not miss a migrating thread,
179 * cpu_cgroup_attach() must not be possible after cgroup_exit() and it
180 * can't be removed from thread list, we hold ->siglock.
182 * If an exiting thread was already removed from thread list we rely on
183 * sched_autogroup_exit_task().
185 for_each_thread(p, t)
188 unlock_task_sighand(p, &flags);
189 autogroup_kref_put(prev);
192 /* Allocates GFP_KERNEL, cannot be called under any spinlock: */
193 void sched_autogroup_create_attach(struct task_struct *p)
195 struct autogroup *ag = autogroup_create();
197 autogroup_move_group(p, ag);
199 /* Drop extra reference added by autogroup_create(): */
200 autogroup_kref_put(ag);
202 EXPORT_SYMBOL(sched_autogroup_create_attach);
204 /* Cannot be called under siglock. Currently has no users: */
205 void sched_autogroup_detach(struct task_struct *p)
207 autogroup_move_group(p, &autogroup_default);
209 EXPORT_SYMBOL(sched_autogroup_detach);
211 void sched_autogroup_fork(struct signal_struct *sig)
213 sig->autogroup = autogroup_task_get(current);
216 void sched_autogroup_exit(struct signal_struct *sig)
218 autogroup_kref_put(sig->autogroup);
221 static int __init setup_autogroup(char *str)
223 sysctl_sched_autogroup_enabled = 0;
227 __setup("noautogroup", setup_autogroup);
229 #ifdef CONFIG_PROC_FS
231 int proc_sched_autogroup_set_nice(struct task_struct *p, int nice)
233 static unsigned long next = INITIAL_JIFFIES;
234 struct autogroup *ag;
235 unsigned long shares;
238 if (nice < MIN_NICE || nice > MAX_NICE)
241 err = security_task_setnice(current, nice);
245 if (nice < 0 && !can_nice(current, nice))
248 /* This is a heavy operation, taking global locks.. */
249 if (!capable(CAP_SYS_ADMIN) && time_before(jiffies, next))
252 next = HZ / 10 + jiffies;
253 ag = autogroup_task_get(p);
255 idx = array_index_nospec(nice + 20, 40);
256 shares = scale_load(sched_prio_to_weight[idx]);
258 down_write(&ag->lock);
259 err = sched_group_set_shares(ag->tg, shares);
264 autogroup_kref_put(ag);
269 void proc_sched_autogroup_show_task(struct task_struct *p, struct seq_file *m)
271 struct autogroup *ag = autogroup_task_get(p);
273 if (!task_group_is_autogroup(ag->tg))
276 down_read(&ag->lock);
277 seq_printf(m, "/autogroup-%ld nice %d\n", ag->id, ag->nice);
281 autogroup_kref_put(ag);
283 #endif /* CONFIG_PROC_FS */
285 int autogroup_path(struct task_group *tg, char *buf, int buflen)
287 if (!task_group_is_autogroup(tg))
290 return snprintf(buf, buflen, "%s-%ld", "/autogroup", tg->autogroup->id);