Linux 4.16-rc3
[cris-mirror.git] / kernel / sched / autogroup.c
blobbb4b9fe026a15da65dad6a7d83108fc5f4585715
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>
8 #include "sched.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)
24 kfree(tg->autogroup);
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... */
33 ag->tg->rt_se = NULL;
34 ag->tg->rt_rq = NULL;
35 #endif
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)
47 kref_get(&ag->kref);
48 return ag;
51 static inline struct autogroup *autogroup_task_get(struct task_struct *p)
53 struct autogroup *ag;
54 unsigned long flags;
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);
62 return ag;
65 static inline struct autogroup *autogroup_create(void)
67 struct autogroup *ag = kzalloc(sizeof(*ag), GFP_KERNEL);
68 struct task_group *tg;
70 if (!ag)
71 goto out_fail;
73 tg = sched_create_group(&root_task_group);
74 if (IS_ERR(tg))
75 goto out_free;
77 kref_init(&ag->kref);
78 init_rwsem(&ag->lock);
79 ag->id = atomic_inc_return(&autogroup_seq_nr);
80 ag->tg = tg;
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;
92 #endif
93 tg->autogroup = ag;
95 sched_online_group(tg, &root_task_group);
96 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()");
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)
112 return false;
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)
122 return false;
124 return true;
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().
134 sched_move_task(p);
137 static void
138 autogroup_move_group(struct task_struct *p, struct autogroup *ag)
140 struct autogroup *prev;
141 struct task_struct *t;
142 unsigned long flags;
144 BUG_ON(!lock_task_sighand(p, &flags));
146 prev = p->signal->autogroup;
147 if (prev == ag) {
148 unlock_task_sighand(p, &flags);
149 return;
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)
165 sched_move_task(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;
203 return 1;
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;
215 int err;
217 if (nice < MIN_NICE || nice > MAX_NICE)
218 return -EINVAL;
220 err = security_task_setnice(current, nice);
221 if (err)
222 return err;
224 if (nice < 0 && !can_nice(current, nice))
225 return -EPERM;
227 /* this is a heavy operation taking global locks.. */
228 if (!capable(CAP_SYS_ADMIN) && time_before(jiffies, next))
229 return -EAGAIN;
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);
237 if (!err)
238 ag->nice = nice;
239 up_write(&ag->lock);
241 autogroup_kref_put(ag);
243 return err;
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))
251 goto out;
253 down_read(&ag->lock);
254 seq_printf(m, "/autogroup-%ld nice %d\n", ag->id, ag->nice);
255 up_read(&ag->lock);
257 out:
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))
266 return 0;
268 return snprintf(buf, buflen, "%s-%ld", "/autogroup", tg->autogroup->id);
270 #endif /* CONFIG_SCHED_DEBUG */