1 #ifdef CONFIG_SCHED_AUTOGROUP
3 #include <linux/proc_fs.h>
4 #include <linux/seq_file.h>
5 #include <linux/kallsyms.h>
6 #include <linux/utsname.h>
8 unsigned int __read_mostly sysctl_sched_autogroup_enabled
= 1;
9 static struct autogroup autogroup_default
;
10 static atomic_t autogroup_seq_nr
;
12 static void __init
autogroup_init(struct task_struct
*init_task
)
14 autogroup_default
.tg
= &root_task_group
;
15 root_task_group
.autogroup
= &autogroup_default
;
16 kref_init(&autogroup_default
.kref
);
17 init_rwsem(&autogroup_default
.lock
);
18 init_task
->signal
->autogroup
= &autogroup_default
;
21 static inline void autogroup_free(struct task_group
*tg
)
26 static inline void autogroup_destroy(struct kref
*kref
)
28 struct autogroup
*ag
= container_of(kref
, struct autogroup
, kref
);
30 sched_destroy_group(ag
->tg
);
33 static inline void autogroup_kref_put(struct autogroup
*ag
)
35 kref_put(&ag
->kref
, autogroup_destroy
);
38 static inline struct autogroup
*autogroup_kref_get(struct autogroup
*ag
)
44 static inline struct autogroup
*autogroup_task_get(struct task_struct
*p
)
49 if (!lock_task_sighand(p
, &flags
))
50 return autogroup_kref_get(&autogroup_default
);
52 ag
= autogroup_kref_get(p
->signal
->autogroup
);
53 unlock_task_sighand(p
, &flags
);
58 static inline struct autogroup
*autogroup_create(void)
60 struct autogroup
*ag
= kzalloc(sizeof(*ag
), GFP_KERNEL
);
61 struct task_group
*tg
;
66 tg
= sched_create_group(&root_task_group
);
72 init_rwsem(&ag
->lock
);
73 ag
->id
= atomic_inc_return(&autogroup_seq_nr
);
82 if (printk_ratelimit()) {
83 printk(KERN_WARNING
"autogroup_create: %s failure.\n",
84 ag
? "sched_create_group()" : "kmalloc()");
87 return autogroup_kref_get(&autogroup_default
);
91 task_wants_autogroup(struct task_struct
*p
, struct task_group
*tg
)
93 if (tg
!= &root_task_group
)
96 if (p
->sched_class
!= &fair_sched_class
)
100 * We can only assume the task group can't go away on us if
101 * autogroup_move_group() can see us on ->thread_group list.
103 if (p
->flags
& PF_EXITING
)
109 static inline struct task_group
*
110 autogroup_task_group(struct task_struct
*p
, struct task_group
*tg
)
112 int enabled
= ACCESS_ONCE(sysctl_sched_autogroup_enabled
);
114 if (enabled
&& task_wants_autogroup(p
, tg
))
115 return p
->signal
->autogroup
->tg
;
121 autogroup_move_group(struct task_struct
*p
, struct autogroup
*ag
)
123 struct autogroup
*prev
;
124 struct task_struct
*t
;
127 BUG_ON(!lock_task_sighand(p
, &flags
));
129 prev
= p
->signal
->autogroup
;
131 unlock_task_sighand(p
, &flags
);
135 p
->signal
->autogroup
= autogroup_kref_get(ag
);
140 } while_each_thread(p
, t
);
142 unlock_task_sighand(p
, &flags
);
143 autogroup_kref_put(prev
);
146 /* Allocates GFP_KERNEL, cannot be called under any spinlock */
147 void sched_autogroup_create_attach(struct task_struct
*p
)
149 struct autogroup
*ag
= autogroup_create();
151 autogroup_move_group(p
, ag
);
152 /* drop extra refrence added by autogroup_create() */
153 autogroup_kref_put(ag
);
155 EXPORT_SYMBOL(sched_autogroup_create_attach
);
157 /* Cannot be called under siglock. Currently has no users */
158 void sched_autogroup_detach(struct task_struct
*p
)
160 autogroup_move_group(p
, &autogroup_default
);
162 EXPORT_SYMBOL(sched_autogroup_detach
);
164 void sched_autogroup_fork(struct signal_struct
*sig
)
166 sig
->autogroup
= autogroup_task_get(current
);
169 void sched_autogroup_exit(struct signal_struct
*sig
)
171 autogroup_kref_put(sig
->autogroup
);
174 static int __init
setup_autogroup(char *str
)
176 sysctl_sched_autogroup_enabled
= 0;
181 __setup("noautogroup", setup_autogroup
);
183 #ifdef CONFIG_PROC_FS
185 int proc_sched_autogroup_set_nice(struct task_struct
*p
, int *nice
)
187 static unsigned long next
= INITIAL_JIFFIES
;
188 struct autogroup
*ag
;
191 if (*nice
< -20 || *nice
> 19)
194 err
= security_task_setnice(current
, *nice
);
198 if (*nice
< 0 && !can_nice(current
, *nice
))
201 /* this is a heavy operation taking global locks.. */
202 if (!capable(CAP_SYS_ADMIN
) && time_before(jiffies
, next
))
205 next
= HZ
/ 10 + jiffies
;
206 ag
= autogroup_task_get(p
);
208 down_write(&ag
->lock
);
209 err
= sched_group_set_shares(ag
->tg
, prio_to_weight
[*nice
+ 20]);
214 autogroup_kref_put(ag
);
219 void proc_sched_autogroup_show_task(struct task_struct
*p
, struct seq_file
*m
)
221 struct autogroup
*ag
= autogroup_task_get(p
);
223 down_read(&ag
->lock
);
224 seq_printf(m
, "/autogroup-%ld nice %d\n", ag
->id
, ag
->nice
);
227 autogroup_kref_put(ag
);
229 #endif /* CONFIG_PROC_FS */
231 #ifdef CONFIG_SCHED_DEBUG
232 static inline int autogroup_path(struct task_group
*tg
, char *buf
, int buflen
)
234 return snprintf(buf
, buflen
, "%s-%ld", "/autogroup", tg
->autogroup
->id
);
236 #endif /* CONFIG_SCHED_DEBUG */
238 #endif /* CONFIG_SCHED_AUTOGROUP */