blob: 2d4ff5353ded618a9b08e1d3b1c9da0fa7d148af [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Ingo Molnar325ea102018-03-03 12:20:47 +01002/*
3 * Auto-group scheduling implementation:
4 */
Peter Zijlstra354d7792018-04-20 15:03:45 +02005#include <linux/nospec.h>
Sergey Senozhatsky25493e52017-12-08 17:24:22 +09006#include "sched.h"
7
Mike Galbraith5091faa2010-11-30 14:18:03 +01008unsigned int __read_mostly sysctl_sched_autogroup_enabled = 1;
9static struct autogroup autogroup_default;
10static atomic_t autogroup_seq_nr;
11
Peter Zijlstra029632f2011-10-25 10:00:11 +020012void __init autogroup_init(struct task_struct *init_task)
Mike Galbraith5091faa2010-11-30 14:18:03 +010013{
Yong Zhang07e06b02011-01-07 15:17:36 +080014 autogroup_default.tg = &root_task_group;
Mike Galbraith5091faa2010-11-30 14:18:03 +010015 kref_init(&autogroup_default.kref);
16 init_rwsem(&autogroup_default.lock);
17 init_task->signal->autogroup = &autogroup_default;
18}
19
Peter Zijlstra029632f2011-10-25 10:00:11 +020020void autogroup_free(struct task_group *tg)
Mike Galbraith5091faa2010-11-30 14:18:03 +010021{
22 kfree(tg->autogroup);
23}
24
25static inline void autogroup_destroy(struct kref *kref)
26{
27 struct autogroup *ag = container_of(kref, struct autogroup, kref);
28
Mike Galbraithf4493772011-01-13 04:54:50 +010029#ifdef CONFIG_RT_GROUP_SCHED
30 /* We've redirected RT tasks to the root task group... */
31 ag->tg->rt_se = NULL;
32 ag->tg->rt_rq = NULL;
33#endif
Li Zefanace783b2013-01-24 14:30:48 +080034 sched_offline_group(ag->tg);
Mike Galbraith5091faa2010-11-30 14:18:03 +010035 sched_destroy_group(ag->tg);
36}
37
38static inline void autogroup_kref_put(struct autogroup *ag)
39{
40 kref_put(&ag->kref, autogroup_destroy);
41}
42
43static inline struct autogroup *autogroup_kref_get(struct autogroup *ag)
44{
45 kref_get(&ag->kref);
46 return ag;
47}
48
Mike Galbraith4f821982010-12-16 15:09:52 +010049static inline struct autogroup *autogroup_task_get(struct task_struct *p)
50{
51 struct autogroup *ag;
52 unsigned long flags;
53
54 if (!lock_task_sighand(p, &flags))
55 return autogroup_kref_get(&autogroup_default);
56
57 ag = autogroup_kref_get(p->signal->autogroup);
58 unlock_task_sighand(p, &flags);
59
60 return ag;
61}
62
Mike Galbraith5091faa2010-11-30 14:18:03 +010063static inline struct autogroup *autogroup_create(void)
64{
65 struct autogroup *ag = kzalloc(sizeof(*ag), GFP_KERNEL);
66 struct task_group *tg;
67
68 if (!ag)
69 goto out_fail;
70
Yong Zhang07e06b02011-01-07 15:17:36 +080071 tg = sched_create_group(&root_task_group);
Mike Galbraith5091faa2010-11-30 14:18:03 +010072 if (IS_ERR(tg))
73 goto out_free;
74
75 kref_init(&ag->kref);
76 init_rwsem(&ag->lock);
77 ag->id = atomic_inc_return(&autogroup_seq_nr);
78 ag->tg = tg;
Mike Galbraithf4493772011-01-13 04:54:50 +010079#ifdef CONFIG_RT_GROUP_SCHED
80 /*
81 * Autogroup RT tasks are redirected to the root task group
82 * so we don't have to move tasks around upon policy change,
83 * or flail around trying to allocate bandwidth on the fly.
84 * A bandwidth exception in __sched_setscheduler() allows
Peter Zijlstra1fe89e12015-02-09 11:53:18 +010085 * the policy change to proceed.
Mike Galbraithf4493772011-01-13 04:54:50 +010086 */
87 free_rt_sched_group(tg);
88 tg->rt_se = root_task_group.rt_se;
89 tg->rt_rq = root_task_group.rt_rq;
90#endif
Mike Galbraith5091faa2010-11-30 14:18:03 +010091 tg->autogroup = ag;
92
Gerald Schaefer41261b62013-05-24 18:07:49 +020093 sched_online_group(tg, &root_task_group);
Mike Galbraith5091faa2010-11-30 14:18:03 +010094 return ag;
95
96out_free:
97 kfree(ag);
98out_fail:
99 if (printk_ratelimit()) {
100 printk(KERN_WARNING "autogroup_create: %s failure.\n",
Anshuman Khandual1e585652017-08-02 14:13:00 +0530101 ag ? "sched_create_group()" : "kzalloc()");
Mike Galbraith5091faa2010-11-30 14:18:03 +0100102 }
103
104 return autogroup_kref_get(&autogroup_default);
105}
106
Peter Zijlstra029632f2011-10-25 10:00:11 +0200107bool task_wants_autogroup(struct task_struct *p, struct task_group *tg)
Mike Galbraith5091faa2010-11-30 14:18:03 +0100108{
109 if (tg != &root_task_group)
110 return false;
Mike Galbraith5091faa2010-11-30 14:18:03 +0100111 /*
Oleg Nesterov18f649e2016-11-14 19:46:09 +0100112 * If we race with autogroup_move_group() the caller can use the old
113 * value of signal->autogroup but in this case sched_move_task() will
114 * be called again before autogroup_kref_put().
Oleg Nesterov8e5bfa82016-11-14 19:46:12 +0100115 *
116 * However, there is no way sched_autogroup_exit_task() could tell us
117 * to avoid autogroup->tg, so we abuse PF_EXITING flag for this case.
Mike Galbraith5091faa2010-11-30 14:18:03 +0100118 */
Oleg Nesterov8e5bfa82016-11-14 19:46:12 +0100119 if (p->flags & PF_EXITING)
120 return false;
121
Mike Galbraith5091faa2010-11-30 14:18:03 +0100122 return true;
123}
124
Oleg Nesterov8e5bfa82016-11-14 19:46:12 +0100125void sched_autogroup_exit_task(struct task_struct *p)
126{
127 /*
128 * We are going to call exit_notify() and autogroup_move_group() can't
129 * see this thread after that: we can no longer use signal->autogroup.
130 * See the PF_EXITING check in task_wants_autogroup().
131 */
132 sched_move_task(p);
133}
134
Mike Galbraith5091faa2010-11-30 14:18:03 +0100135static void
136autogroup_move_group(struct task_struct *p, struct autogroup *ag)
137{
138 struct autogroup *prev;
139 struct task_struct *t;
140 unsigned long flags;
141
142 BUG_ON(!lock_task_sighand(p, &flags));
143
144 prev = p->signal->autogroup;
145 if (prev == ag) {
146 unlock_task_sighand(p, &flags);
147 return;
148 }
149
150 p->signal->autogroup = autogroup_kref_get(ag);
Oleg Nesterov18f649e2016-11-14 19:46:09 +0100151 /*
152 * We can't avoid sched_move_task() after we changed signal->autogroup,
153 * this process can already run with task_group() == prev->tg or we can
154 * race with cgroup code which can read autogroup = prev under rq->lock.
155 * In the latter case for_each_thread() can not miss a migrating thread,
156 * cpu_cgroup_attach() must not be possible after cgroup_exit() and it
157 * can't be removed from thread list, we hold ->siglock.
Oleg Nesterov8e5bfa82016-11-14 19:46:12 +0100158 *
159 * If an exiting thread was already removed from thread list we rely on
160 * sched_autogroup_exit_task().
Oleg Nesterov18f649e2016-11-14 19:46:09 +0100161 */
Oleg Nesterov5aface52014-08-13 21:20:03 +0200162 for_each_thread(p, t)
Mike Galbraith5091faa2010-11-30 14:18:03 +0100163 sched_move_task(t);
Oleg Nesterov18f649e2016-11-14 19:46:09 +0100164
Mike Galbraith5091faa2010-11-30 14:18:03 +0100165 unlock_task_sighand(p, &flags);
166 autogroup_kref_put(prev);
167}
168
Ingo Molnar97fb7a02018-03-03 14:01:12 +0100169/* Allocates GFP_KERNEL, cannot be called under any spinlock: */
Mike Galbraith5091faa2010-11-30 14:18:03 +0100170void sched_autogroup_create_attach(struct task_struct *p)
171{
Ingo Molnarc1ad41f2012-12-11 10:23:45 +0100172 struct autogroup *ag = autogroup_create();
Mike Galbraith5091faa2010-11-30 14:18:03 +0100173
174 autogroup_move_group(p, ag);
Ingo Molnar97fb7a02018-03-03 14:01:12 +0100175
176 /* Drop extra reference added by autogroup_create(): */
Mike Galbraith5091faa2010-11-30 14:18:03 +0100177 autogroup_kref_put(ag);
178}
179EXPORT_SYMBOL(sched_autogroup_create_attach);
180
Ingo Molnar97fb7a02018-03-03 14:01:12 +0100181/* Cannot be called under siglock. Currently has no users: */
Mike Galbraith5091faa2010-11-30 14:18:03 +0100182void sched_autogroup_detach(struct task_struct *p)
183{
184 autogroup_move_group(p, &autogroup_default);
185}
186EXPORT_SYMBOL(sched_autogroup_detach);
187
188void sched_autogroup_fork(struct signal_struct *sig)
189{
Mike Galbraith4f821982010-12-16 15:09:52 +0100190 sig->autogroup = autogroup_task_get(current);
Mike Galbraith5091faa2010-11-30 14:18:03 +0100191}
192
193void sched_autogroup_exit(struct signal_struct *sig)
194{
195 autogroup_kref_put(sig->autogroup);
196}
197
198static int __init setup_autogroup(char *str)
199{
200 sysctl_sched_autogroup_enabled = 0;
201
202 return 1;
203}
Mike Galbraith5091faa2010-11-30 14:18:03 +0100204__setup("noautogroup", setup_autogroup);
205
Ingo Molnarc1ad41f2012-12-11 10:23:45 +0100206#ifdef CONFIG_PROC_FS
207
208int proc_sched_autogroup_set_nice(struct task_struct *p, int nice)
209{
210 static unsigned long next = INITIAL_JIFFIES;
211 struct autogroup *ag;
Mike Galbraith83929cc2016-11-23 11:33:37 +0100212 unsigned long shares;
Peter Zijlstra354d7792018-04-20 15:03:45 +0200213 int err, idx;
Ingo Molnarc1ad41f2012-12-11 10:23:45 +0100214
Dongsheng Yang75e45d52014-02-11 15:34:50 +0800215 if (nice < MIN_NICE || nice > MAX_NICE)
Ingo Molnarc1ad41f2012-12-11 10:23:45 +0100216 return -EINVAL;
217
218 err = security_task_setnice(current, nice);
219 if (err)
220 return err;
221
222 if (nice < 0 && !can_nice(current, nice))
223 return -EPERM;
224
Ingo Molnar97fb7a02018-03-03 14:01:12 +0100225 /* This is a heavy operation, taking global locks.. */
Ingo Molnarc1ad41f2012-12-11 10:23:45 +0100226 if (!capable(CAP_SYS_ADMIN) && time_before(jiffies, next))
227 return -EAGAIN;
228
229 next = HZ / 10 + jiffies;
230 ag = autogroup_task_get(p);
Peter Zijlstra354d7792018-04-20 15:03:45 +0200231
232 idx = array_index_nospec(nice + 20, 40);
233 shares = scale_load(sched_prio_to_weight[idx]);
Ingo Molnarc1ad41f2012-12-11 10:23:45 +0100234
235 down_write(&ag->lock);
Mike Galbraith83929cc2016-11-23 11:33:37 +0100236 err = sched_group_set_shares(ag->tg, shares);
Ingo Molnarc1ad41f2012-12-11 10:23:45 +0100237 if (!err)
238 ag->nice = nice;
239 up_write(&ag->lock);
240
241 autogroup_kref_put(ag);
242
243 return err;
244}
245
246void 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
257out:
258 autogroup_kref_put(ag);
259}
260#endif /* CONFIG_PROC_FS */
261
Mike Galbraith5091faa2010-11-30 14:18:03 +0100262#ifdef CONFIG_SCHED_DEBUG
Peter Zijlstra029632f2011-10-25 10:00:11 +0200263int autogroup_path(struct task_group *tg, char *buf, int buflen)
Mike Galbraith5091faa2010-11-30 14:18:03 +0100264{
Mike Galbraith511f67a2011-02-22 15:02:00 +0100265 if (!task_group_is_autogroup(tg))
Bharata B Rao8ecedd72011-01-11 15:42:57 +0530266 return 0;
267
Mike Galbraith5091faa2010-11-30 14:18:03 +0100268 return snprintf(buf, buflen, "%s-%ld", "/autogroup", tg->autogroup->id);
269}
Ingo Molnar97fb7a02018-03-03 14:01:12 +0100270#endif