]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - kernel/sched/autogroup.c
Merge branch 'clockevents/4.15' of https://git.linaro.org/people/daniel.lezcano/linux...
[mirror_ubuntu-bionic-kernel.git] / kernel / sched / autogroup.c
1 #include "sched.h"
2
3 #include <linux/proc_fs.h>
4 #include <linux/seq_file.h>
5 #include <linux/kallsyms.h>
6 #include <linux/utsname.h>
7 #include <linux/security.h>
8 #include <linux/export.h>
9
10 unsigned int __read_mostly sysctl_sched_autogroup_enabled = 1;
11 static struct autogroup autogroup_default;
12 static atomic_t autogroup_seq_nr;
13
14 void __init autogroup_init(struct task_struct *init_task)
15 {
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;
20 }
21
22 void autogroup_free(struct task_group *tg)
23 {
24 kfree(tg->autogroup);
25 }
26
27 static inline void autogroup_destroy(struct kref *kref)
28 {
29 struct autogroup *ag = container_of(kref, struct autogroup, kref);
30
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);
38 }
39
40 static inline void autogroup_kref_put(struct autogroup *ag)
41 {
42 kref_put(&ag->kref, autogroup_destroy);
43 }
44
45 static inline struct autogroup *autogroup_kref_get(struct autogroup *ag)
46 {
47 kref_get(&ag->kref);
48 return ag;
49 }
50
51 static inline struct autogroup *autogroup_task_get(struct task_struct *p)
52 {
53 struct autogroup *ag;
54 unsigned long flags;
55
56 if (!lock_task_sighand(p, &flags))
57 return autogroup_kref_get(&autogroup_default);
58
59 ag = autogroup_kref_get(p->signal->autogroup);
60 unlock_task_sighand(p, &flags);
61
62 return ag;
63 }
64
65 static inline struct autogroup *autogroup_create(void)
66 {
67 struct autogroup *ag = kzalloc(sizeof(*ag), GFP_KERNEL);
68 struct task_group *tg;
69
70 if (!ag)
71 goto out_fail;
72
73 tg = sched_create_group(&root_task_group);
74 if (IS_ERR(tg))
75 goto out_free;
76
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
82 /*
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.
88 */
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;
94
95 sched_online_group(tg, &root_task_group);
96 return ag;
97
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()");
104 }
105
106 return autogroup_kref_get(&autogroup_default);
107 }
108
109 bool task_wants_autogroup(struct task_struct *p, struct task_group *tg)
110 {
111 if (tg != &root_task_group)
112 return false;
113 /*
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().
117 *
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.
120 */
121 if (p->flags & PF_EXITING)
122 return false;
123
124 return true;
125 }
126
127 void sched_autogroup_exit_task(struct task_struct *p)
128 {
129 /*
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().
133 */
134 sched_move_task(p);
135 }
136
137 static void
138 autogroup_move_group(struct task_struct *p, struct autogroup *ag)
139 {
140 struct autogroup *prev;
141 struct task_struct *t;
142 unsigned long flags;
143
144 BUG_ON(!lock_task_sighand(p, &flags));
145
146 prev = p->signal->autogroup;
147 if (prev == ag) {
148 unlock_task_sighand(p, &flags);
149 return;
150 }
151
152 p->signal->autogroup = autogroup_kref_get(ag);
153 /*
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.
160 *
161 * If an exiting thread was already removed from thread list we rely on
162 * sched_autogroup_exit_task().
163 */
164 for_each_thread(p, t)
165 sched_move_task(t);
166
167 unlock_task_sighand(p, &flags);
168 autogroup_kref_put(prev);
169 }
170
171 /* Allocates GFP_KERNEL, cannot be called under any spinlock */
172 void sched_autogroup_create_attach(struct task_struct *p)
173 {
174 struct autogroup *ag = autogroup_create();
175
176 autogroup_move_group(p, ag);
177 /* drop extra reference added by autogroup_create() */
178 autogroup_kref_put(ag);
179 }
180 EXPORT_SYMBOL(sched_autogroup_create_attach);
181
182 /* Cannot be called under siglock. Currently has no users */
183 void sched_autogroup_detach(struct task_struct *p)
184 {
185 autogroup_move_group(p, &autogroup_default);
186 }
187 EXPORT_SYMBOL(sched_autogroup_detach);
188
189 void sched_autogroup_fork(struct signal_struct *sig)
190 {
191 sig->autogroup = autogroup_task_get(current);
192 }
193
194 void sched_autogroup_exit(struct signal_struct *sig)
195 {
196 autogroup_kref_put(sig->autogroup);
197 }
198
199 static int __init setup_autogroup(char *str)
200 {
201 sysctl_sched_autogroup_enabled = 0;
202
203 return 1;
204 }
205
206 __setup("noautogroup", setup_autogroup);
207
208 #ifdef CONFIG_PROC_FS
209
210 int proc_sched_autogroup_set_nice(struct task_struct *p, int nice)
211 {
212 static unsigned long next = INITIAL_JIFFIES;
213 struct autogroup *ag;
214 unsigned long shares;
215 int err;
216
217 if (nice < MIN_NICE || nice > MAX_NICE)
218 return -EINVAL;
219
220 err = security_task_setnice(current, nice);
221 if (err)
222 return err;
223
224 if (nice < 0 && !can_nice(current, nice))
225 return -EPERM;
226
227 /* this is a heavy operation taking global locks.. */
228 if (!capable(CAP_SYS_ADMIN) && time_before(jiffies, next))
229 return -EAGAIN;
230
231 next = HZ / 10 + jiffies;
232 ag = autogroup_task_get(p);
233 shares = scale_load(sched_prio_to_weight[nice + 20]);
234
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);
240
241 autogroup_kref_put(ag);
242
243 return err;
244 }
245
246 void proc_sched_autogroup_show_task(struct task_struct *p, struct seq_file *m)
247 {
248 struct autogroup *ag = autogroup_task_get(p);
249
250 if (!task_group_is_autogroup(ag->tg))
251 goto out;
252
253 down_read(&ag->lock);
254 seq_printf(m, "/autogroup-%ld nice %d\n", ag->id, ag->nice);
255 up_read(&ag->lock);
256
257 out:
258 autogroup_kref_put(ag);
259 }
260 #endif /* CONFIG_PROC_FS */
261
262 #ifdef CONFIG_SCHED_DEBUG
263 int autogroup_path(struct task_group *tg, char *buf, int buflen)
264 {
265 if (!task_group_is_autogroup(tg))
266 return 0;
267
268 return snprintf(buf, buflen, "%s-%ld", "/autogroup", tg->autogroup->id);
269 }
270 #endif /* CONFIG_SCHED_DEBUG */