1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _KERNEL_SCHED_AUTOGROUP_H
3 #define _KERNEL_SCHED_AUTOGROUP_H
4 
5 #include "sched.h"
6 
7 #ifdef CONFIG_SCHED_AUTOGROUP
8 
9 struct autogroup {
10 	/*
11 	 * Reference doesn't mean how many threads attach to this
12 	 * autogroup now. It just stands for the number of tasks
13 	 * which could use this autogroup.
14 	 */
15 	struct kref		kref;
16 	struct task_group	*tg;
17 	struct rw_semaphore	lock;
18 	unsigned long		id;
19 	int			nice;
20 };
21 
22 extern void autogroup_init(struct task_struct *init_task);
23 extern void autogroup_free(struct task_group *tg);
24 
task_group_is_autogroup(struct task_group * tg)25 static inline bool task_group_is_autogroup(struct task_group *tg)
26 {
27 	return !!tg->autogroup;
28 }
29 
30 extern bool task_wants_autogroup(struct task_struct *p, struct task_group *tg);
31 
32 static inline struct task_group *
autogroup_task_group(struct task_struct * p,struct task_group * tg)33 autogroup_task_group(struct task_struct *p, struct task_group *tg)
34 {
35 	extern unsigned int sysctl_sched_autogroup_enabled;
36 	int enabled = READ_ONCE(sysctl_sched_autogroup_enabled);
37 
38 	if (enabled && task_wants_autogroup(p, tg))
39 		return p->signal->autogroup->tg;
40 
41 	return tg;
42 }
43 
44 extern int autogroup_path(struct task_group *tg, char *buf, int buflen);
45 
46 #else /* !CONFIG_SCHED_AUTOGROUP: */
47 
autogroup_init(struct task_struct * init_task)48 static inline void autogroup_init(struct task_struct *init_task) {  }
autogroup_free(struct task_group * tg)49 static inline void autogroup_free(struct task_group *tg) { }
task_group_is_autogroup(struct task_group * tg)50 static inline bool task_group_is_autogroup(struct task_group *tg)
51 {
52 	return 0;
53 }
54 
55 static inline struct task_group *
autogroup_task_group(struct task_struct * p,struct task_group * tg)56 autogroup_task_group(struct task_struct *p, struct task_group *tg)
57 {
58 	return tg;
59 }
60 
autogroup_path(struct task_group * tg,char * buf,int buflen)61 static inline int autogroup_path(struct task_group *tg, char *buf, int buflen)
62 {
63 	return 0;
64 }
65 
66 #endif /* !CONFIG_SCHED_AUTOGROUP */
67 
68 #endif /* _KERNEL_SCHED_AUTOGROUP_H */
69