1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/proc_fs.h>
3 #include <linux/seq_file.h>
4 #include <linux/utsname.h>
5 #include <linux/security.h>
6 #include <linux/export.h>
10 unsigned int __read_mostly sysctl_sched_autogroup_enabled
= 1;
11 static struct autogroup autogroup_default
;
12 static atomic_t autogroup_seq_nr
;
14 void __init
autogroup_init(struct task_struct
*init_task
)
16 autogroup_default
.tg
= &root_task_group
;
17 kref_init(&autogroup_default
.kref
);
18 init_rwsem(&autogroup_default
.lock
);
19 init_task
->signal
->autogroup
= &autogroup_default
;
22 void autogroup_free(struct task_group
*tg
)
27 static inline void autogroup_destroy(struct kref
*kref
)
29 struct autogroup
*ag
= container_of(kref
, struct autogroup
, kref
);
31 #ifdef CONFIG_RT_GROUP_SCHED
32 /* We've redirected RT tasks to the root task group... */
36 sched_offline_group(ag
->tg
);
37 sched_destroy_group(ag
->tg
);
40 static inline void autogroup_kref_put(struct autogroup
*ag
)
42 kref_put(&ag
->kref
, autogroup_destroy
);
45 static inline struct autogroup
*autogroup_kref_get(struct autogroup
*ag
)
51 static inline struct autogroup
*autogroup_task_get(struct task_struct
*p
)
56 if (!lock_task_sighand(p
, &flags
))
57 return autogroup_kref_get(&autogroup_default
);
59 ag
= autogroup_kref_get(p
->signal
->autogroup
);
60 unlock_task_sighand(p
, &flags
);
65 static inline struct autogroup
*autogroup_create(void)
67 struct autogroup
*ag
= kzalloc(sizeof(*ag
), GFP_KERNEL
);
68 struct task_group
*tg
;
73 tg
= sched_create_group(&root_task_group
);
78 init_rwsem(&ag
->lock
);
79 ag
->id
= atomic_inc_return(&autogroup_seq_nr
);
81 #ifdef CONFIG_RT_GROUP_SCHED
83 * Autogroup RT tasks are redirected to the root task group
84 * so we don't have to move tasks around upon policy change,
85 * or flail around trying to allocate bandwidth on the fly.
86 * A bandwidth exception in __sched_setscheduler() allows
87 * the policy change to proceed.
89 free_rt_sched_group(tg
);
90 tg
->rt_se
= root_task_group
.rt_se
;
91 tg
->rt_rq
= root_task_group
.rt_rq
;
95 sched_online_group(tg
, &root_task_group
);
101 if (printk_ratelimit()) {
102 printk(KERN_WARNING
"autogroup_create: %s failure.\n",
103 ag
? "sched_create_group()" : "kzalloc()");
106 return autogroup_kref_get(&autogroup_default
);
109 bool task_wants_autogroup(struct task_struct
*p
, struct task_group
*tg
)
111 if (tg
!= &root_task_group
)
114 * If we race with autogroup_move_group() the caller can use the old
115 * value of signal->autogroup but in this case sched_move_task() will
116 * be called again before autogroup_kref_put().
118 * However, there is no way sched_autogroup_exit_task() could tell us
119 * to avoid autogroup->tg, so we abuse PF_EXITING flag for this case.
121 if (p
->flags
& PF_EXITING
)
127 void sched_autogroup_exit_task(struct task_struct
*p
)
130 * We are going to call exit_notify() and autogroup_move_group() can't
131 * see this thread after that: we can no longer use signal->autogroup.
132 * See the PF_EXITING check in task_wants_autogroup().
138 autogroup_move_group(struct task_struct
*p
, struct autogroup
*ag
)
140 struct autogroup
*prev
;
141 struct task_struct
*t
;
144 BUG_ON(!lock_task_sighand(p
, &flags
));
146 prev
= p
->signal
->autogroup
;
148 unlock_task_sighand(p
, &flags
);
152 p
->signal
->autogroup
= autogroup_kref_get(ag
);
154 * We can't avoid sched_move_task() after we changed signal->autogroup,
155 * this process can already run with task_group() == prev->tg or we can
156 * race with cgroup code which can read autogroup = prev under rq->lock.
157 * In the latter case for_each_thread() can not miss a migrating thread,
158 * cpu_cgroup_attach() must not be possible after cgroup_exit() and it
159 * can't be removed from thread list, we hold ->siglock.
161 * If an exiting thread was already removed from thread list we rely on
162 * sched_autogroup_exit_task().
164 for_each_thread(p
, t
)
167 unlock_task_sighand(p
, &flags
);
168 autogroup_kref_put(prev
);
171 /* Allocates GFP_KERNEL, cannot be called under any spinlock */
172 void sched_autogroup_create_attach(struct task_struct
*p
)
174 struct autogroup
*ag
= autogroup_create();
176 autogroup_move_group(p
, ag
);
177 /* drop extra reference added by autogroup_create() */
178 autogroup_kref_put(ag
);
180 EXPORT_SYMBOL(sched_autogroup_create_attach
);
182 /* Cannot be called under siglock. Currently has no users */
183 void sched_autogroup_detach(struct task_struct
*p
)
185 autogroup_move_group(p
, &autogroup_default
);
187 EXPORT_SYMBOL(sched_autogroup_detach
);
189 void sched_autogroup_fork(struct signal_struct
*sig
)
191 sig
->autogroup
= autogroup_task_get(current
);
194 void sched_autogroup_exit(struct signal_struct
*sig
)
196 autogroup_kref_put(sig
->autogroup
);
199 static int __init
setup_autogroup(char *str
)
201 sysctl_sched_autogroup_enabled
= 0;
206 __setup("noautogroup", setup_autogroup
);
208 #ifdef CONFIG_PROC_FS
210 int proc_sched_autogroup_set_nice(struct task_struct
*p
, int nice
)
212 static unsigned long next
= INITIAL_JIFFIES
;
213 struct autogroup
*ag
;
214 unsigned long shares
;
217 if (nice
< MIN_NICE
|| nice
> MAX_NICE
)
220 err
= security_task_setnice(current
, nice
);
224 if (nice
< 0 && !can_nice(current
, nice
))
227 /* this is a heavy operation taking global locks.. */
228 if (!capable(CAP_SYS_ADMIN
) && time_before(jiffies
, next
))
231 next
= HZ
/ 10 + jiffies
;
232 ag
= autogroup_task_get(p
);
233 shares
= scale_load(sched_prio_to_weight
[nice
+ 20]);
235 down_write(&ag
->lock
);
236 err
= sched_group_set_shares(ag
->tg
, shares
);
241 autogroup_kref_put(ag
);
246 void proc_sched_autogroup_show_task(struct task_struct
*p
, struct seq_file
*m
)
248 struct autogroup
*ag
= autogroup_task_get(p
);
250 if (!task_group_is_autogroup(ag
->tg
))
253 down_read(&ag
->lock
);
254 seq_printf(m
, "/autogroup-%ld nice %d\n", ag
->id
, ag
->nice
);
258 autogroup_kref_put(ag
);
260 #endif /* CONFIG_PROC_FS */
262 #ifdef CONFIG_SCHED_DEBUG
263 int autogroup_path(struct task_group
*tg
, char *buf
, int buflen
)
265 if (!task_group_is_autogroup(tg
))
268 return snprintf(buf
, buflen
, "%s-%ld", "/autogroup", tg
->autogroup
->id
);
270 #endif /* CONFIG_SCHED_DEBUG */