Blame view

kernel/sched/autogroup.c 6.58 KB
b24413180   Greg Kroah-Hartman   License cleanup: ...
1
  // SPDX-License-Identifier: GPL-2.0
029632fbb   Peter Zijlstra   sched: Make separ...
2
  #include "sched.h"
5091faa44   Mike Galbraith   sched: Add 'autog...
3
4
5
6
  #include <linux/proc_fs.h>
  #include <linux/seq_file.h>
  #include <linux/kallsyms.h>
  #include <linux/utsname.h>
029632fbb   Peter Zijlstra   sched: Make separ...
7
8
  #include <linux/security.h>
  #include <linux/export.h>
bb0b090d8   Peter Zijlstra   sched/autogroup: ...
9
  #include <linux/nospec.h>
5091faa44   Mike Galbraith   sched: Add 'autog...
10
11
12
13
  
  unsigned int __read_mostly sysctl_sched_autogroup_enabled = 1;
  static struct autogroup autogroup_default;
  static atomic_t autogroup_seq_nr;
029632fbb   Peter Zijlstra   sched: Make separ...
14
  void __init autogroup_init(struct task_struct *init_task)
5091faa44   Mike Galbraith   sched: Add 'autog...
15
  {
07e06b011   Yong Zhang   sched: Consolidat...
16
  	autogroup_default.tg = &root_task_group;
5091faa44   Mike Galbraith   sched: Add 'autog...
17
18
19
20
  	kref_init(&autogroup_default.kref);
  	init_rwsem(&autogroup_default.lock);
  	init_task->signal->autogroup = &autogroup_default;
  }
029632fbb   Peter Zijlstra   sched: Make separ...
21
  void autogroup_free(struct task_group *tg)
5091faa44   Mike Galbraith   sched: Add 'autog...
22
23
24
25
26
27
28
  {
  	kfree(tg->autogroup);
  }
  
  static inline void autogroup_destroy(struct kref *kref)
  {
  	struct autogroup *ag = container_of(kref, struct autogroup, kref);
f44937718   Mike Galbraith   sched, autogroup:...
29
30
31
32
33
  #ifdef CONFIG_RT_GROUP_SCHED
  	/* We've redirected RT tasks to the root task group... */
  	ag->tg->rt_se = NULL;
  	ag->tg->rt_rq = NULL;
  #endif
ace783b9b   Li Zefan   sched: split out ...
34
  	sched_offline_group(ag->tg);
5091faa44   Mike Galbraith   sched: Add 'autog...
35
36
37
38
39
40
41
42
43
44
45
46
47
  	sched_destroy_group(ag->tg);
  }
  
  static inline void autogroup_kref_put(struct autogroup *ag)
  {
  	kref_put(&ag->kref, autogroup_destroy);
  }
  
  static inline struct autogroup *autogroup_kref_get(struct autogroup *ag)
  {
  	kref_get(&ag->kref);
  	return ag;
  }
4f8219875   Mike Galbraith   sched, autogroup:...
48
49
50
51
52
53
54
55
56
57
58
59
60
  static inline struct autogroup *autogroup_task_get(struct task_struct *p)
  {
  	struct autogroup *ag;
  	unsigned long flags;
  
  	if (!lock_task_sighand(p, &flags))
  		return autogroup_kref_get(&autogroup_default);
  
  	ag = autogroup_kref_get(p->signal->autogroup);
  	unlock_task_sighand(p, &flags);
  
  	return ag;
  }
5091faa44   Mike Galbraith   sched: Add 'autog...
61
62
63
64
65
66
67
  static inline struct autogroup *autogroup_create(void)
  {
  	struct autogroup *ag = kzalloc(sizeof(*ag), GFP_KERNEL);
  	struct task_group *tg;
  
  	if (!ag)
  		goto out_fail;
07e06b011   Yong Zhang   sched: Consolidat...
68
  	tg = sched_create_group(&root_task_group);
5091faa44   Mike Galbraith   sched: Add 'autog...
69
70
71
72
73
74
75
  	if (IS_ERR(tg))
  		goto out_free;
  
  	kref_init(&ag->kref);
  	init_rwsem(&ag->lock);
  	ag->id = atomic_inc_return(&autogroup_seq_nr);
  	ag->tg = tg;
f44937718   Mike Galbraith   sched, autogroup:...
76
77
78
79
80
81
  #ifdef CONFIG_RT_GROUP_SCHED
  	/*
  	 * Autogroup RT tasks are redirected to the root task group
  	 * so we don't have to move tasks around upon policy change,
  	 * or flail around trying to allocate bandwidth on the fly.
  	 * A bandwidth exception in __sched_setscheduler() allows
1fe89e1b6   Peter Zijlstra   sched/autogroup: ...
82
  	 * the policy change to proceed.
f44937718   Mike Galbraith   sched, autogroup:...
83
84
85
86
87
  	 */
  	free_rt_sched_group(tg);
  	tg->rt_se = root_task_group.rt_se;
  	tg->rt_rq = root_task_group.rt_rq;
  #endif
5091faa44   Mike Galbraith   sched: Add 'autog...
88
  	tg->autogroup = ag;
41261b6a8   Gerald Schaefer   sched/autogroup: ...
89
  	sched_online_group(tg, &root_task_group);
5091faa44   Mike Galbraith   sched: Add 'autog...
90
91
92
93
94
95
96
97
  	return ag;
  
  out_free:
  	kfree(ag);
  out_fail:
  	if (printk_ratelimit()) {
  		printk(KERN_WARNING "autogroup_create: %s failure.
  ",
1e58565e6   Anshuman Khandual   sched/autogroup: ...
98
  			ag ? "sched_create_group()" : "kzalloc()");
5091faa44   Mike Galbraith   sched: Add 'autog...
99
100
101
102
  	}
  
  	return autogroup_kref_get(&autogroup_default);
  }
029632fbb   Peter Zijlstra   sched: Make separ...
103
  bool task_wants_autogroup(struct task_struct *p, struct task_group *tg)
5091faa44   Mike Galbraith   sched: Add 'autog...
104
105
106
  {
  	if (tg != &root_task_group)
  		return false;
5091faa44   Mike Galbraith   sched: Add 'autog...
107
  	/*
18f649ef3   Oleg Nesterov   sched/autogroup: ...
108
109
110
  	 * If we race with autogroup_move_group() the caller can use the old
  	 * value of signal->autogroup but in this case sched_move_task() will
  	 * be called again before autogroup_kref_put().
8e5bfa8c1   Oleg Nesterov   sched/autogroup: ...
111
112
113
  	 *
  	 * However, there is no way sched_autogroup_exit_task() could tell us
  	 * to avoid autogroup->tg, so we abuse PF_EXITING flag for this case.
5091faa44   Mike Galbraith   sched: Add 'autog...
114
  	 */
8e5bfa8c1   Oleg Nesterov   sched/autogroup: ...
115
116
  	if (p->flags & PF_EXITING)
  		return false;
5091faa44   Mike Galbraith   sched: Add 'autog...
117
118
  	return true;
  }
8e5bfa8c1   Oleg Nesterov   sched/autogroup: ...
119
120
121
122
123
124
125
126
127
  void sched_autogroup_exit_task(struct task_struct *p)
  {
  	/*
  	 * We are going to call exit_notify() and autogroup_move_group() can't
  	 * see this thread after that: we can no longer use signal->autogroup.
  	 * See the PF_EXITING check in task_wants_autogroup().
  	 */
  	sched_move_task(p);
  }
5091faa44   Mike Galbraith   sched: Add 'autog...
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
  static void
  autogroup_move_group(struct task_struct *p, struct autogroup *ag)
  {
  	struct autogroup *prev;
  	struct task_struct *t;
  	unsigned long flags;
  
  	BUG_ON(!lock_task_sighand(p, &flags));
  
  	prev = p->signal->autogroup;
  	if (prev == ag) {
  		unlock_task_sighand(p, &flags);
  		return;
  	}
  
  	p->signal->autogroup = autogroup_kref_get(ag);
18f649ef3   Oleg Nesterov   sched/autogroup: ...
144
145
146
147
148
149
150
  	/*
  	 * We can't avoid sched_move_task() after we changed signal->autogroup,
  	 * this process can already run with task_group() == prev->tg or we can
  	 * race with cgroup code which can read autogroup = prev under rq->lock.
  	 * In the latter case for_each_thread() can not miss a migrating thread,
  	 * cpu_cgroup_attach() must not be possible after cgroup_exit() and it
  	 * can't be removed from thread list, we hold ->siglock.
8e5bfa8c1   Oleg Nesterov   sched/autogroup: ...
151
152
153
  	 *
  	 * If an exiting thread was already removed from thread list we rely on
  	 * sched_autogroup_exit_task().
18f649ef3   Oleg Nesterov   sched/autogroup: ...
154
  	 */
5aface53d   Oleg Nesterov   sched: Change aut...
155
  	for_each_thread(p, t)
5091faa44   Mike Galbraith   sched: Add 'autog...
156
  		sched_move_task(t);
18f649ef3   Oleg Nesterov   sched/autogroup: ...
157

5091faa44   Mike Galbraith   sched: Add 'autog...
158
159
160
161
162
163
164
  	unlock_task_sighand(p, &flags);
  	autogroup_kref_put(prev);
  }
  
  /* Allocates GFP_KERNEL, cannot be called under any spinlock */
  void sched_autogroup_create_attach(struct task_struct *p)
  {
c1ad41f1f   Ingo Molnar   Revert "sched/aut...
165
  	struct autogroup *ag = autogroup_create();
5091faa44   Mike Galbraith   sched: Add 'autog...
166
167
  
  	autogroup_move_group(p, ag);
25985edce   Lucas De Marchi   Fix common misspe...
168
  	/* drop extra reference added by autogroup_create() */
5091faa44   Mike Galbraith   sched: Add 'autog...
169
170
171
172
173
174
175
176
177
178
179
180
181
  	autogroup_kref_put(ag);
  }
  EXPORT_SYMBOL(sched_autogroup_create_attach);
  
  /* Cannot be called under siglock.  Currently has no users */
  void sched_autogroup_detach(struct task_struct *p)
  {
  	autogroup_move_group(p, &autogroup_default);
  }
  EXPORT_SYMBOL(sched_autogroup_detach);
  
  void sched_autogroup_fork(struct signal_struct *sig)
  {
4f8219875   Mike Galbraith   sched, autogroup:...
182
  	sig->autogroup = autogroup_task_get(current);
5091faa44   Mike Galbraith   sched: Add 'autog...
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
  }
  
  void sched_autogroup_exit(struct signal_struct *sig)
  {
  	autogroup_kref_put(sig->autogroup);
  }
  
  static int __init setup_autogroup(char *str)
  {
  	sysctl_sched_autogroup_enabled = 0;
  
  	return 1;
  }
  
  __setup("noautogroup", setup_autogroup);
c1ad41f1f   Ingo Molnar   Revert "sched/aut...
198
199
200
201
202
203
  #ifdef CONFIG_PROC_FS
  
  int proc_sched_autogroup_set_nice(struct task_struct *p, int nice)
  {
  	static unsigned long next = INITIAL_JIFFIES;
  	struct autogroup *ag;
83929cce9   Mike Galbraith   sched/autogroup: ...
204
  	unsigned long shares;
bb0b090d8   Peter Zijlstra   sched/autogroup: ...
205
  	int err, idx;
c1ad41f1f   Ingo Molnar   Revert "sched/aut...
206

75e45d512   Dongsheng Yang   sched: Replace ha...
207
  	if (nice < MIN_NICE || nice > MAX_NICE)
c1ad41f1f   Ingo Molnar   Revert "sched/aut...
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
  		return -EINVAL;
  
  	err = security_task_setnice(current, nice);
  	if (err)
  		return err;
  
  	if (nice < 0 && !can_nice(current, nice))
  		return -EPERM;
  
  	/* this is a heavy operation taking global locks.. */
  	if (!capable(CAP_SYS_ADMIN) && time_before(jiffies, next))
  		return -EAGAIN;
  
  	next = HZ / 10 + jiffies;
  	ag = autogroup_task_get(p);
bb0b090d8   Peter Zijlstra   sched/autogroup: ...
223
224
225
  
  	idx = array_index_nospec(nice + 20, 40);
  	shares = scale_load(sched_prio_to_weight[idx]);
c1ad41f1f   Ingo Molnar   Revert "sched/aut...
226
227
  
  	down_write(&ag->lock);
83929cce9   Mike Galbraith   sched/autogroup: ...
228
  	err = sched_group_set_shares(ag->tg, shares);
c1ad41f1f   Ingo Molnar   Revert "sched/aut...
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
  	if (!err)
  		ag->nice = nice;
  	up_write(&ag->lock);
  
  	autogroup_kref_put(ag);
  
  	return err;
  }
  
  void proc_sched_autogroup_show_task(struct task_struct *p, struct seq_file *m)
  {
  	struct autogroup *ag = autogroup_task_get(p);
  
  	if (!task_group_is_autogroup(ag->tg))
  		goto out;
  
  	down_read(&ag->lock);
  	seq_printf(m, "/autogroup-%ld nice %d
  ", ag->id, ag->nice);
  	up_read(&ag->lock);
  
  out:
  	autogroup_kref_put(ag);
  }
  #endif /* CONFIG_PROC_FS */
5091faa44   Mike Galbraith   sched: Add 'autog...
254
  #ifdef CONFIG_SCHED_DEBUG
029632fbb   Peter Zijlstra   sched: Make separ...
255
  int autogroup_path(struct task_group *tg, char *buf, int buflen)
5091faa44   Mike Galbraith   sched: Add 'autog...
256
  {
511f67a59   Mike Galbraith   sched, autogroup:...
257
  	if (!task_group_is_autogroup(tg))
8ecedd7a0   Bharata B Rao   sched: Display au...
258
  		return 0;
5091faa44   Mike Galbraith   sched: Add 'autog...
259
260
261
  	return snprintf(buf, buflen, "%s-%ld", "/autogroup", tg->autogroup->id);
  }
  #endif /* CONFIG_SCHED_DEBUG */