xfs: add full xfs_dqblk verifier
[linux/fpc-iii.git] / kernel / sched / autogroup.c
blob6be6c575b6cd1d7c20ea72900849b95404066641
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Auto-group scheduling implementation:
4 */
5 #include "sched.h"
7 unsigned int __read_mostly sysctl_sched_autogroup_enabled = 1;
8 static struct autogroup autogroup_default;
9 static atomic_t autogroup_seq_nr;
11 void __init autogroup_init(struct task_struct *init_task)
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;
19 void autogroup_free(struct task_group *tg)
21 kfree(tg->autogroup);
24 static inline void autogroup_destroy(struct kref *kref)
26 struct autogroup *ag = container_of(kref, struct autogroup, kref);
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);
37 static inline void autogroup_kref_put(struct autogroup *ag)
39 kref_put(&ag->kref, autogroup_destroy);
42 static inline struct autogroup *autogroup_kref_get(struct autogroup *ag)
44 kref_get(&ag->kref);
45 return ag;
48 static inline struct autogroup *autogroup_task_get(struct task_struct *p)
50 struct autogroup *ag;
51 unsigned long flags;
53 if (!lock_task_sighand(p, &flags))
54 return autogroup_kref_get(&autogroup_default);
56 ag = autogroup_kref_get(p->signal->autogroup);
57 unlock_task_sighand(p, &flags);
59 return ag;
62 static inline struct autogroup *autogroup_create(void)
64 struct autogroup *ag = kzalloc(sizeof(*ag), GFP_KERNEL);
65 struct task_group *tg;
67 if (!ag)
68 goto out_fail;
70 tg = sched_create_group(&root_task_group);
71 if (IS_ERR(tg))
72 goto out_free;
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
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.
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;
92 sched_online_group(tg, &root_task_group);
93 return ag;
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()");
103 return autogroup_kref_get(&autogroup_default);
106 bool task_wants_autogroup(struct task_struct *p, struct task_group *tg)
108 if (tg != &root_task_group)
109 return false;
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().
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.
118 if (p->flags & PF_EXITING)
119 return false;
121 return true;
124 void sched_autogroup_exit_task(struct task_struct *p)
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().
131 sched_move_task(p);
134 static void
135 autogroup_move_group(struct task_struct *p, struct autogroup *ag)
137 struct autogroup *prev;
138 struct task_struct *t;
139 unsigned long flags;
141 BUG_ON(!lock_task_sighand(p, &flags));
143 prev = p->signal->autogroup;
144 if (prev == ag) {
145 unlock_task_sighand(p, &flags);
146 return;
149 p->signal->autogroup = autogroup_kref_get(ag);
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.
158 * If an exiting thread was already removed from thread list we rely on
159 * sched_autogroup_exit_task().
161 for_each_thread(p, t)
162 sched_move_task(t);
164 unlock_task_sighand(p, &flags);
165 autogroup_kref_put(prev);
168 /* Allocates GFP_KERNEL, cannot be called under any spinlock: */
169 void sched_autogroup_create_attach(struct task_struct *p)
171 struct autogroup *ag = autogroup_create();
173 autogroup_move_group(p, ag);
175 /* Drop extra reference added by autogroup_create(): */
176 autogroup_kref_put(ag);
178 EXPORT_SYMBOL(sched_autogroup_create_attach);
180 /* Cannot be called under siglock. Currently has no users: */
181 void sched_autogroup_detach(struct task_struct *p)
183 autogroup_move_group(p, &autogroup_default);
185 EXPORT_SYMBOL(sched_autogroup_detach);
187 void sched_autogroup_fork(struct signal_struct *sig)
189 sig->autogroup = autogroup_task_get(current);
192 void sched_autogroup_exit(struct signal_struct *sig)
194 autogroup_kref_put(sig->autogroup);
197 static int __init setup_autogroup(char *str)
199 sysctl_sched_autogroup_enabled = 0;
201 return 1;
203 __setup("noautogroup", setup_autogroup);
205 #ifdef CONFIG_PROC_FS
207 int proc_sched_autogroup_set_nice(struct task_struct *p, int nice)
209 static unsigned long next = INITIAL_JIFFIES;
210 struct autogroup *ag;
211 unsigned long shares;
212 int err;
214 if (nice < MIN_NICE || nice > MAX_NICE)
215 return -EINVAL;
217 err = security_task_setnice(current, nice);
218 if (err)
219 return err;
221 if (nice < 0 && !can_nice(current, nice))
222 return -EPERM;
224 /* This is a heavy operation, taking global locks.. */
225 if (!capable(CAP_SYS_ADMIN) && time_before(jiffies, next))
226 return -EAGAIN;
228 next = HZ / 10 + jiffies;
229 ag = autogroup_task_get(p);
230 shares = scale_load(sched_prio_to_weight[nice + 20]);
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);
238 autogroup_kref_put(ag);
240 return err;
243 void proc_sched_autogroup_show_task(struct task_struct *p, struct seq_file *m)
245 struct autogroup *ag = autogroup_task_get(p);
247 if (!task_group_is_autogroup(ag->tg))
248 goto out;
250 down_read(&ag->lock);
251 seq_printf(m, "/autogroup-%ld nice %d\n", ag->id, ag->nice);
252 up_read(&ag->lock);
254 out:
255 autogroup_kref_put(ag);
257 #endif /* CONFIG_PROC_FS */
259 #ifdef CONFIG_SCHED_DEBUG
260 int autogroup_path(struct task_group *tg, char *buf, int buflen)
262 if (!task_group_is_autogroup(tg))
263 return 0;
265 return snprintf(buf, buflen, "%s-%ld", "/autogroup", tg->autogroup->id);
267 #endif