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