• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 #include "sched.h"
3 
4 #include <linux/proc_fs.h>
5 #include <linux/seq_file.h>
6 #include <linux/kallsyms.h>
7 #include <linux/utsname.h>
8 #include <linux/security.h>
9 #include <linux/export.h>
10 #include <linux/nospec.h>
11 
12 unsigned int __read_mostly sysctl_sched_autogroup_enabled = 1;
13 static struct autogroup autogroup_default;
14 static atomic_t autogroup_seq_nr;
15 
autogroup_init(struct task_struct * init_task)16 void __init autogroup_init(struct task_struct *init_task)
17 {
18 	autogroup_default.tg = &root_task_group;
19 	kref_init(&autogroup_default.kref);
20 	init_rwsem(&autogroup_default.lock);
21 	init_task->signal->autogroup = &autogroup_default;
22 }
23 
autogroup_free(struct task_group * tg)24 void autogroup_free(struct task_group *tg)
25 {
26 	kfree(tg->autogroup);
27 }
28 
autogroup_destroy(struct kref * kref)29 static inline void autogroup_destroy(struct kref *kref)
30 {
31 	struct autogroup *ag = container_of(kref, struct autogroup, kref);
32 
33 #ifdef CONFIG_RT_GROUP_SCHED
34 	/* We've redirected RT tasks to the root task group... */
35 	ag->tg->rt_se = NULL;
36 	ag->tg->rt_rq = NULL;
37 #endif
38 	sched_offline_group(ag->tg);
39 	sched_destroy_group(ag->tg);
40 }
41 
autogroup_kref_put(struct autogroup * ag)42 static inline void autogroup_kref_put(struct autogroup *ag)
43 {
44 	kref_put(&ag->kref, autogroup_destroy);
45 }
46 
autogroup_kref_get(struct autogroup * ag)47 static inline struct autogroup *autogroup_kref_get(struct autogroup *ag)
48 {
49 	kref_get(&ag->kref);
50 	return ag;
51 }
52 
autogroup_task_get(struct task_struct * p)53 static inline struct autogroup *autogroup_task_get(struct task_struct *p)
54 {
55 	struct autogroup *ag;
56 	unsigned long flags;
57 
58 	if (!lock_task_sighand(p, &flags))
59 		return autogroup_kref_get(&autogroup_default);
60 
61 	ag = autogroup_kref_get(p->signal->autogroup);
62 	unlock_task_sighand(p, &flags);
63 
64 	return ag;
65 }
66 
autogroup_create(void)67 static inline struct autogroup *autogroup_create(void)
68 {
69 	struct autogroup *ag = kzalloc(sizeof(*ag), GFP_KERNEL);
70 	struct task_group *tg;
71 
72 	if (!ag)
73 		goto out_fail;
74 
75 	tg = sched_create_group(&root_task_group);
76 	if (IS_ERR(tg))
77 		goto out_free;
78 
79 	kref_init(&ag->kref);
80 	init_rwsem(&ag->lock);
81 	ag->id = atomic_inc_return(&autogroup_seq_nr);
82 	ag->tg = tg;
83 #ifdef CONFIG_RT_GROUP_SCHED
84 	/*
85 	 * Autogroup RT tasks are redirected to the root task group
86 	 * so we don't have to move tasks around upon policy change,
87 	 * or flail around trying to allocate bandwidth on the fly.
88 	 * A bandwidth exception in __sched_setscheduler() allows
89 	 * the policy change to proceed.
90 	 */
91 	free_rt_sched_group(tg);
92 	tg->rt_se = root_task_group.rt_se;
93 	tg->rt_rq = root_task_group.rt_rq;
94 #endif
95 	tg->autogroup = ag;
96 
97 	sched_online_group(tg, &root_task_group);
98 	return ag;
99 
100 out_free:
101 	kfree(ag);
102 out_fail:
103 	if (printk_ratelimit()) {
104 		printk(KERN_WARNING "autogroup_create: %s failure.\n",
105 			ag ? "sched_create_group()" : "kzalloc()");
106 	}
107 
108 	return autogroup_kref_get(&autogroup_default);
109 }
110 
task_wants_autogroup(struct task_struct * p,struct task_group * tg)111 bool task_wants_autogroup(struct task_struct *p, struct task_group *tg)
112 {
113 	if (tg != &root_task_group)
114 		return false;
115 	/*
116 	 * If we race with autogroup_move_group() the caller can use the old
117 	 * value of signal->autogroup but in this case sched_move_task() will
118 	 * be called again before autogroup_kref_put().
119 	 *
120 	 * However, there is no way sched_autogroup_exit_task() could tell us
121 	 * to avoid autogroup->tg, so we abuse PF_EXITING flag for this case.
122 	 */
123 	if (p->flags & PF_EXITING)
124 		return false;
125 
126 	return true;
127 }
128 
sched_autogroup_exit_task(struct task_struct * p)129 void sched_autogroup_exit_task(struct task_struct *p)
130 {
131 	/*
132 	 * We are going to call exit_notify() and autogroup_move_group() can't
133 	 * see this thread after that: we can no longer use signal->autogroup.
134 	 * See the PF_EXITING check in task_wants_autogroup().
135 	 */
136 	sched_move_task(p);
137 }
138 
139 static void
autogroup_move_group(struct task_struct * p,struct autogroup * ag)140 autogroup_move_group(struct task_struct *p, struct autogroup *ag)
141 {
142 	struct autogroup *prev;
143 	struct task_struct *t;
144 	unsigned long flags;
145 
146 	BUG_ON(!lock_task_sighand(p, &flags));
147 
148 	prev = p->signal->autogroup;
149 	if (prev == ag) {
150 		unlock_task_sighand(p, &flags);
151 		return;
152 	}
153 
154 	p->signal->autogroup = autogroup_kref_get(ag);
155 	/*
156 	 * We can't avoid sched_move_task() after we changed signal->autogroup,
157 	 * this process can already run with task_group() == prev->tg or we can
158 	 * race with cgroup code which can read autogroup = prev under rq->lock.
159 	 * In the latter case for_each_thread() can not miss a migrating thread,
160 	 * cpu_cgroup_attach() must not be possible after cgroup_exit() and it
161 	 * can't be removed from thread list, we hold ->siglock.
162 	 *
163 	 * If an exiting thread was already removed from thread list we rely on
164 	 * sched_autogroup_exit_task().
165 	 */
166 	for_each_thread(p, t)
167 		sched_move_task(t);
168 
169 	unlock_task_sighand(p, &flags);
170 	autogroup_kref_put(prev);
171 }
172 
173 /* Allocates GFP_KERNEL, cannot be called under any spinlock */
sched_autogroup_create_attach(struct task_struct * p)174 void sched_autogroup_create_attach(struct task_struct *p)
175 {
176 	struct autogroup *ag = autogroup_create();
177 
178 	autogroup_move_group(p, ag);
179 	/* drop extra reference added by autogroup_create() */
180 	autogroup_kref_put(ag);
181 }
182 EXPORT_SYMBOL(sched_autogroup_create_attach);
183 
184 /* Cannot be called under siglock.  Currently has no users */
sched_autogroup_detach(struct task_struct * p)185 void sched_autogroup_detach(struct task_struct *p)
186 {
187 	autogroup_move_group(p, &autogroup_default);
188 }
189 EXPORT_SYMBOL(sched_autogroup_detach);
190 
sched_autogroup_fork(struct signal_struct * sig)191 void sched_autogroup_fork(struct signal_struct *sig)
192 {
193 	sig->autogroup = autogroup_task_get(current);
194 }
195 
sched_autogroup_exit(struct signal_struct * sig)196 void sched_autogroup_exit(struct signal_struct *sig)
197 {
198 	autogroup_kref_put(sig->autogroup);
199 }
200 
setup_autogroup(char * str)201 static int __init setup_autogroup(char *str)
202 {
203 	sysctl_sched_autogroup_enabled = 0;
204 
205 	return 1;
206 }
207 
208 __setup("noautogroup", setup_autogroup);
209 
210 #ifdef CONFIG_PROC_FS
211 
proc_sched_autogroup_set_nice(struct task_struct * p,int nice)212 int proc_sched_autogroup_set_nice(struct task_struct *p, int nice)
213 {
214 	static unsigned long next = INITIAL_JIFFIES;
215 	struct autogroup *ag;
216 	unsigned long shares;
217 	int err, idx;
218 
219 	if (nice < MIN_NICE || nice > MAX_NICE)
220 		return -EINVAL;
221 
222 	err = security_task_setnice(current, nice);
223 	if (err)
224 		return err;
225 
226 	if (nice < 0 && !can_nice(current, nice))
227 		return -EPERM;
228 
229 	/* this is a heavy operation taking global locks.. */
230 	if (!capable(CAP_SYS_ADMIN) && time_before(jiffies, next))
231 		return -EAGAIN;
232 
233 	next = HZ / 10 + jiffies;
234 	ag = autogroup_task_get(p);
235 
236 	idx = array_index_nospec(nice + 20, 40);
237 	shares = scale_load(sched_prio_to_weight[idx]);
238 
239 	down_write(&ag->lock);
240 	err = sched_group_set_shares(ag->tg, shares);
241 	if (!err)
242 		ag->nice = nice;
243 	up_write(&ag->lock);
244 
245 	autogroup_kref_put(ag);
246 
247 	return err;
248 }
249 
proc_sched_autogroup_show_task(struct task_struct * p,struct seq_file * m)250 void proc_sched_autogroup_show_task(struct task_struct *p, struct seq_file *m)
251 {
252 	struct autogroup *ag = autogroup_task_get(p);
253 
254 	if (!task_group_is_autogroup(ag->tg))
255 		goto out;
256 
257 	down_read(&ag->lock);
258 	seq_printf(m, "/autogroup-%ld nice %d\n", ag->id, ag->nice);
259 	up_read(&ag->lock);
260 
261 out:
262 	autogroup_kref_put(ag);
263 }
264 #endif /* CONFIG_PROC_FS */
265 
autogroup_path(struct task_group * tg,char * buf,int buflen)266 int autogroup_path(struct task_group *tg, char *buf, int buflen)
267 {
268 	if (!task_group_is_autogroup(tg))
269 		return 0;
270 
271 	return snprintf(buf, buflen, "%s-%ld", "/autogroup", tg->autogroup->id);
272 }
273